210 likes | 410 Views
Run Time Environments. Read Pages 427-449 Homework: Lab 7. Run Time Environments. The compiler must create and maintain a runtime environment to interact with OS and other system software Storage allocation Access to variables and data. Code P : G Static Vars. Frame 1
E N D
Run Time Environments Read Pages 427-449 Homework: Lab 7
Run Time Environments • The compiler must create and maintain a runtime environment to interact with OS and other system software • Storage allocation • Access to variables and data
Code P : G Static Vars. Frame 1 (main) Frame 2 (Fun1) . . . L Frame N (Fun N) : Heap Activation Records • Activation records are also called stack frames • Each frame holds information about a procedure call • Stores the 7 things below, created when the function is called
Procedure Calls • Activation records can be managed by maintaining 2 kinds of links between them • control link: (dynamic link) – points to the activation record of the runtime caller • this would be the L of the calling function • activation record on top is the callee of the one below it, the caller; this has to be, because only one thing can be active at one time • access link: (static link) – is used to implement lexically scoped languages • set compile time • indicates nesting
Procedure Calls • return value – used by the called procedure to return a value to the calling procedure • save machine status – information about the state of the machine before the procedure is called (registers: PC,AC) return address, • actual parameters – used by the calling procedure to supply parameters to he called procedure • local data – data local to the procedure • determined at compile time • keep count of the allocated addresses relative to L • temporary variables – variable that is temporary within the procedure that is used for evaluation results
Static Allocation • static= compile time stuff • dynamic = runtime stuff • stored value is determined at runtime • the ID name is bound to a storage location as the program is compiled • the amount of storage is dependent on the type
Limitations of Statics • size of the data object & constraints on its memory position must be known at compile time • recursive procedures are restricted because all activations of a procedure use the same bindings for local names • data structures can not be allocated dynamically
Stack Allocation • storage is organized as a stack of activation records • pushed & popped as activation begins & ends • values for locals are lost when the activation ends because their storage disappears
Calling Sequence • call sequence – creates an activation record & enters information into its fields • this is done at compile time & appears in the target code • return sequence – restores the state of the machine so the calling procedure can continue execution
A Call Sequence • The caller evaluates the actuals (arguments) • The caller stores a return address & the old value of the top of the stack onto the callee’s activation record • top_sp (L) is set to the activation record of the callee • The callee saves register values and other state information • Callee initializes local data and begins execution
Return Sequence • calleeplaces a return value ‘next to’ the activation record of the caller • using the information in the status field, the callee restores top_sp & other registers & branches to the return address • the caller copies the return value into its own activation record & uses it
Example Symbol Table function M function Pvariables x, y, z function Q function R • nesting indicates containment
Example cont. • Set up access and control links • control links on the stack points to the activation record that called it • access links point to the nearest activation record that indicates the function it was defined in • Shows nesting
Example cont. Activation Stack • Draw the access and control links for the activation stack
Non-Local/Non-Global • Stored in an out nesting but no globally • Ex. intxmain()int y fun1(y)int z fun2(z)y=z //y is not local in fun2() but is not global
Scoping Rules • static scope rule(lexical) – most closely nested • whenever we see ‘lexical’ stuff, make a relation to ‘static’ stuff • dynamic scope rule – determined at runtime by considering current activations
Static Scope • suppose procedure p at level nprefers to procedure x at nesting level nx • case 1: (np < nx) x is declared in p • np = nx - 1 • access link in the called procedure points to the access link in the activation record of the caller • access link and control link are the same
Static Scope • case 2: (np >= nx) compute D = np – nx + 1;then follow D control links from the caller p & set x’s access link to that address • Example: suppose R calls Q and nR= 4, np= 2 • What is P’s access link set to?
Implementing Function Calls • See funcalls handout!