1 / 15

ANNAJAH NATIONAL UNIVERCITY COMPUTER DEPARTMENT COMPILERS DONE BY : AHMED KHALED AL_MOSLEH

ANNAJAH NATIONAL UNIVERCITY COMPUTER DEPARTMENT COMPILERS DONE BY : AHMED KHALED AL_MOSLEH SUPJECT : COMPILERS LEC#(19). DATE : 3 / 3 /2004. __Program Variable Allocation__. ◘ Dragon:7,1-7.5 , fisher 9.1 , 9.2 ,9.6 ◘ where are variables kept?? □ Depends on language’s scope rules.

dreama
Download Presentation

ANNAJAH NATIONAL UNIVERCITY COMPUTER DEPARTMENT COMPILERS DONE BY : AHMED KHALED AL_MOSLEH

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ANNAJAH NATIONAL UNIVERCITY COMPUTER DEPARTMENT COMPILERS DONE BY : AHMED KHALED AL_MOSLEH SUPJECT : COMPILERS LEC#(19). DATE : 3 / 3 /2004

  2. __Program Variable Allocation__ ◘ Dragon:7,1-7.5 , fisher 9.1 , 9.2 ,9.6 ◘ where are variables kept?? □ Depends on language’s scope rules. □ Also depends on compilers optimization. ◘ In general prefer to keep frequently used variables in registers. □ Faster to access registers than memory □ many computers are load/store without operations on memory. ◘ Can always keep variables in memory □ Unbounded size , unlike limited registers. □ Easier to find variables from surrounding scope if kept in memory

  3. ____C Storage Allocation_____ ◘ Global variables in C programs are allocated in data segment □ Compiler knows size of variables and its name ● Generate a static allocation count : .word ● Linker resolves references to this Word ◘ Local ( aka automatic ) variables are located on stack. □ Each subroutine invocation create an activation record to hold its local variables ●e.g. P calls U calls R calls S ●compiler may keep variables from currently- executing routine in registers

  4. _______Active Records________ ◘ information stored on the stack for a subroutine invocation is called a stack frame or active record. ◘ Designed for particular language and computer □ Want minimum cost to crate , use ,and discard ◘ Has 4 parts □ Incoming arguments (actual for routine). □ Local variables □ Dynamically allocated data □ Outgoing arguments

  5. _Creating an activation Record_ ◘ Arguments and variables are stored in a fixed offset from beginning from stack frame ◘ Create activation record when routine is called. □ Set $fp to point to first argument ● addiu $sp , $sp , Asize □ Make room for local variables ● addiu $sp , $sp , -Vsize ◘ Discard record when leaving function □ Pop frame of stack ● move $sp , $fp □ Restore $fp’s value in previous frame ● Register must have been saved during call ● Caller or callee could save

  6. ____Simple Subroutine Call___ ◘ A simple call by value routine invocation □ Push the arguments onto the stack ● addiu $sp , $sp , -4 sw <arg 1> 4($sp) addiu $sp , $sp, -4 sw <arg 2> 4($sp) □ Save registers (see next slide) □ Call the routine ● jal routine □ When routine returns (callee save) ● $sp points after arguments ● $fp is unchanged

  7. _______Register Saving______ ◘ Contents of registers must be saved on stack during a call and restored upon return ◘ Tow conventions □ Function executing the call saves/restores registers (caller-saved). □ Invoked functions saves /restores registers (callee-saved) ◘ Both have advantages and disadvantages □ MIPS uses combination of the two

  8. _______Caller-Saved________ ◘ Function executing call saves registers before invoking the routine and restores them after □ Only have to save registers that contain useful (live) values □ But don’t know if registers will be used in callee □ Adds many instructions at each call site ◘ MIPS registers St0 - st8 are caller-saved □ Calee can write them without saving values ◘ Caller must have space in its stack frame to save registers

  9. ________Callee-Saved_______ ◘ Upon invocation , subroutine saves the registers that it will use □ Only saves registers that will be used □ But don’t know if registers have live values □ Registers save/store code not duplicated at call sites ◘ MIPS registers $s0 - $s7 are callee-saved □ Routine cannot use them without saving values ◘ Also $sp and $fp are callee-saved on MIPS ◘ Callee must have space in its stack frame to save registers

  10. __Subroutine Calls on MIPS__ ◘ Caller □ Push arguments on stack ● MIPS passes first four arguments in $a0 - $a3 □ Save caller registers ( $t0 - $t8) □ Execute a jal instruction ● Stores return address in $ ra ($31) ◘ Callee □ Set up stack frame ● $sp = $sp – frame_size ● Save $ fp ● $fp = $sp + 4* n_args + frame size □ Save callee-saved registers ● $fp already saved and $sp’s value is $fp ● $ ra and $ s0 - $ s7

  11. _Procedure returns on MIPS__ ◘ Callee □ Result returned in register $v0 □ Return to address in $ra ● jr $ra □ Restore callee-saved registers ● $ra and $s0 - $s7 ● $sp = $ sp – frame_size ● $sp ◘ Caller □ Restore caller-saved registers

  12. ________EXAMPLE________ ◘ Main() { fact(2); } int fact (in x) { if ( x==1) return 1; else return ( x* fact(x-1)); ◘ main : addiu $sp , $sp ,-8 sw $fp , 8( $sp) addiu $fp , $sp , 8 sw $ra , -4 ($fp) li $a0 , 2 jal fact lw $ra , -4($fp) lw $fp , 0($fp) addiu $sp , $sp ,8 jr $ra

  13. _____EXAMPLE ,cont’d_____ ◘ fact : addiu $sp , $ sp , -12 sw $fp , 0($sp). addiu $sp , $ sp , 16 sw $ra , -8($fp) sw $s0 , -12($fp) bneq $a0 , 1 , L1 li $v0 , 1 bneq L2 L1: move $s0 , $a0 sub $a0 , $a0 , 1 jal fact mult $v0 , $v0,$s0 L2: lw $ra , -8($fp) lw $s0 , -12($fp) lw $fp , 4($fp) addiu $sp , $sp, 12 jr $ra

  14. ____Local Variables in C____ ◘ Variables in C are associated with begin-end block □ Created upon entry and destroyed upon exit ◘ Two ways to allocate □ Allocate the maximum amount of storage upon subroutine entry ● single push is cheap □ allocate appropriate space upon block entry ● keep stack frame smaller ,but requires more work ◘ e.g. for ( int a , int b) { Int x; If ( a > x) { int y = b + x ; } else { int z= a + b ; } }

  15. _____Register Allocation_____ ◘ Compilers try to keep variables in computers registers □ Faster to access and more flexibility ◘ Limited number of registers □ Process of fitting variables to registers is called register allocation ● Goal is to reduce program’s execution cost by making effective use of registers ◘ CS536 compiler will keep variables in memory □ Later , we will discuss how to register allocate

More Related