Run-time Environments Chapter 7 1 Compiler Construction Run-time Environments
Run-time Environment • The static source code requires considerable support to be executed on a computer – Data objects must be created and destroyed – Variables with the same name in different parts of the program may refer to different data objects – The data corresponding to a variable in one context must be copied to a new data object in another context when a method call is performed – Data objects of different types may require different memory representations – A data type may map directly to a data element supported by the hardware (integers usually do), or it may be more complicated and require a custom layout 2 Compiler Construction Run-time Environments
Programs and Procedures Modern programs consist of procedures • Typically a main procedure begins executing first, and it may invoke other procedures to accomplish the task of the program • These procedures are specified by the programmer in the program – Methods in OO languages are really just old-fashioned procedures ( this is the secret parameter in instance methods) 3 Compiler Construction Run-time Environments
Basic Terminology • Procedure definition An element of the source code where the procedure is fully specified: – It is given a name – Its parameters (if any) are specifi ed – Its return type is specifi ed – Its body (statements to execute) is defi ned • Formal parameters ( formals ) Parameters specified in the procedure definition • Actual parameters ( actuals ) Parameters passed by the calling environment when the procedure is invoked (or activated ) 4 Compiler Construction Run-time Environments
Control Flow Among Procedures 1. Control flows sequentially —a sequence of steps Control is at some specific point in the program at each step 2. Each execution of a procedure starts at the beginning of the procedure body and eventually returns control to the point immediately following the place where the procedure was called 5 Compiler Construction Run-time Environments
Procedure Activation • Each execution of a procedure body is known as its activation • The lifetime of an activation of a procedure is the sequence of steps between the first and last steps (inclusive) in the execution of the procedure’s body • If p 1 and p 2 are two procedure activations, their lifetimes are either non- overlapping or nested. If control enters p 2 before p 1 is left, then control must leave p 2 before it leaves p 1 • This is assuming single execution threads 6 Compiler Construction Run-time Environments
Recursion • In a recursive procedure a new activation of the same procedure can begin before an earlier activation has ended activation • A recursive method need not call itself directly 7 Compiler Construction Run-time Environments
Control Stacks • The flow of control in a program corresponds to a depth-first traversal of an activation tree • See the Java version of Figure 7.1 in the Dragon Book and its ensuing output • A control stack keeps track of the procedure activations – Push the node for an activation onto the control stack when the activation begins and pop it off when the activation ends – The contents of the control stack corresponds to a path to the root of the activation tree – When node n is on the top of the control stack, the stack contains the nodes along a path from n to the root 8 Compiler Construction Run-time Environments
Scope • A declaration associates a name with a component of the program – Variable (data) – Procedure • The same name may be used in independent declarations in different parts of a program • Scope rules determine which declaration of a name applies when a name appears in the program text • Our sort program declared the variable i multiple times, and the variable i was used in various places within the program • Variables declared within procedures are local to those procedures; variables declared outside of procedures are nonlocal variables 9 Compiler Construction Run-time Environments
Scope and Symbol Tables At compile time, a symbol table is used to find the declaration that applies to an occurrence of a name • A symbol table entry is created when a declaration is seen by the compiler • As long as we are within the scope of a declaration, a name’s entry is returned when it is looked up in the symbol table 10 Compiler Construction Run-time Environments
Bindings of Names Environment State Name Storage Value Program l −value r −value text Static Notion Dynamic Counterpart Procedure definition Procedure activation Name declaration Name bindings Scope declaration Lifetime of binding 11 Compiler Construction Run-time Environments
Questions to Ask How a compiler organizes its storage and binds names is decided by answering the following questions: 1. May procedures be recursive? 2. What happens to the values of local names when control returns from an activation of a procedure? 3. May a procedure refer to nonlocal names? 4. How are parameters passed when a procedure is invoked? 5. May procedures be passed as parameters? 6. May procedures be returned as results? 7. May storage be allocated dynamically under program control? 8. May storage be explicitly deallocated? The answers to these questions affect the run-time support required for a programming language 12 Compiler Construction Run-time Environments
Run-time Memory An executing program requires storage for its instructions and data objects • Fixed-size requirements: – Code – Static data (e.g.: Java static (class) fi elds; C/C++ global variables, class variables, and static locals) The compiler can compute the space required for these parts of an executing program which remain fixed throughout the program’s execution • Variable-size requirements: – Space required for the data in each procedure activation – Space required for objects dynamically allocated 13 Compiler Construction Run-time Environments
Program Memory Layout Code Static Data Stack Heap 14 Compiler Construction Run-time Environments
Program Memory Layout • Code does not (or should not!) change during program execution • Static data’s storage location or size need not Code change during program execution Static Data • The stack holds Stack – local variables – return value (maybe) – return address (maybe) – temporaries used in expression evaluation Heap • The heap holds the data for dynamically created data (typically objects in an OO language) 15 Compiler Construction Run-time Environments
Stack vs. Heap • Both the stack and the heap can grow and shrink dynamically as a program executes • The stack grows and shrinks in a very regular fashion (LIFO) • The heap can become fragmented as earlier allocated space is freed up before later allocated space • Often the stack and heap grow towards each each other from opposite memory addresses 16 Compiler Construction Run-time Environments
Activation Record • AKA frame • Created on the stack for a procedure activation Return value Actual parameters Control link Access link Saved machine status Local data Temporaries 17 Compiler Construction Run-time Environments
Activation Record Details 1 • Return value—value to be returned by the procedure A register is often used instead (quicker) Return value • Actual parameters—values pushed by the caller Actual parameters Control link Registers may be used in some instances Access link Saved machine status • Control link—points to the activation record of Local data the caller Temporaries • Access link—points to non-local data – Required for langauges like Pascal which allows nested procedures and enclosed procedures have access to the local data in enclosing procedures – In Decaf, could be used to refer to the object in which the method was called ( this ) 18 Compiler Construction Run-time Environments
Activation Record Details 2 • Saved machine status—the contents of the Return value instruction pointer (program counter) and any Actual parameters registers the procedure will modify Control link Access link • Local data—variables local to the procedure Saved machine status Local data • Temporaries—values that arise from the Temporaries evaluation of expressions The sizes of which of these fields can be determined at compile time? 19 Compiler Construction Run-time Environments
Compile-time Local Data Layout • The amount of storage required for a name depends on its type • The local data field in the activation record (AR) can be computed at compile time: int position = address of start of the local data area within the AR; SymbolTable st = the symbol table for the procedure; for each declaration within the procedure of the form <type var> { st.insert(var, position); // Insert identifier and offset AR[position] is reserved to store the value of variable var; position = position + sizeof(type); } 20 Compiler Construction Run-time Environments
Recommend
More recommend