Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 9 Runtime Environment. 2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need.

Similar presentations


Presentation on theme: "1 Lecture 9 Runtime Environment. 2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need."— Presentation transcript:

1 1 Lecture 9 Runtime Environment

2 2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need to understand how a program executes at run time before we can generate code for it

3 3 Basic Execution Model l MIPS as an example l CPU »ALU Unit »Registers »Control Unit l Memory »program »data Memory Registers Control ALU

4 4 Arithmetic and Logic Unit l Performs most of the data operations Has the form: OP R dest, R src1, R src2 l Operations are: »Arithmetic operations (add, sub, mulo [mult with overflow]) »Logical operations (and, sll, srl) »Comparison operations (seq, sge, slt [set to 1 if less than]) Memory Registers Control ALU

5 5 Arithmetic and Logic Unit l Many arithmetic operations can cause an exception »overflow and underflow l Can operate on different data types »8, 16, 32 bits »signed and unsigned arithmetic »Floating-point operations (separate ALU) »Instructions to convert between formats (cvt.s.d) Memory Registers Control ALU

6 6 Control l Handles the instruction sequencing l Executing instructions »All instructions are in memory »Fetch the instruction pointed by the PC and execute it »For general instructions, increment the PC to point to the next location in memory Memory RegistersALU Control

7 7 l Unconditional Branches »Fetch the next instruction from a different location »Unconditional jump to a given address j label »Unconditional jump to an address in a register jr r src »To handle procedure calls, do an unconditional jump, but save the next address in the current stream in a register jal label [jump and link] jalr r src Memory RegistersALU Control

8 8 l Conditional Branches »Perform a test, if successful fetch instructions from a new address, otherwise fetch the next instruction »Instructions are of the form: brelop R src1, R src2, label »‘ relop ’ is of the form: ‘’, ‘eq’, ‘ne’, ‘gt’, ‘ge’, ‘lt’, ‘le’ Memory RegistersALU Control

9 9 l Control transfer in special (rare) cases »traps and exceptions »Mechanism –Save the next(or current) instruction location –find the address to jump to (from an exception vector) –jump to that location Memory RegistersALU Control

10 10 Memory l Flat Address Space »composed of words »byte addressable l Need to store »Program »Local variables »Global variables and data »Stack »Heap Memory RegistersALU Control

11 11 Memory RegistersALU Control Stack Generated Code Heap Objects Arrays locals (parameters)

12 12 Registers l Load/store architecture »All operations are on register values »Need to bring data in-to/out-of registers »la R dest, address [load address] »lw R dest, address [load word] »li, R dest, imm [load imm] »sw R src, address [store word ] »mv R dest, R src [ move ] »address has the from value(R) l Important for performance »limited in number ALU Control Memory Registers

13 13 Registers (of MIPS processors)

14 14 Other interactions l Other operations »Input/Output »Privilege / secure operations »Handling special hardware –TLBs, Caches etc. l Mostly via system calls »hand-coded in assembly »compiler can treat them as a normal function call ALU Control Memory Registers

15 15 The MIPS ISA and MIPS Processor l One of the earliest RISC processors »Has evolved from 1980 ’ s »ISA has also evolved –Always backward compatible, I.e. add more to the ISA –MIPS-I, MIPS-II ….MIPS-V »Many processor incarnation –From a simple 5-stage pipeline to an out-of-order superscalar –R2000, R4000, R8000, R10000 …..

16 16 Procedure Abstraction l Decomposition of programs into callable procedures. l Issues: »calling /returning mechanism »parameter passing »local variables (scope) »private execution context » private storage for each procedure invocation » Encapsulate information about control flow & data abstractions The procedure abstraction is a social contract.

17 17 Benefits of procedure abstraction 1. control abstraction »well defined entry, exits »mechanism to pass parameters, return values 2. name space » new name space within procedure » local names are protected from outside 3. external interface »accessed by procedure name, parameters »protection for both caller and callee » enables software libraries, systems 4. separate compilation »compile procedures independently »keeps compile times reasonable »allows us to build large programs

18 18 Procedure Abstraction l procedure abstraction leads... »multiple procedures »library calls »compiled by many compilers, written in different languages, hand-written assembly l For a compiler, we need to worry about »Memory layout »Registers »Stack

19 19 Registers (of MIPS processors)

20 20 Parameter passing disciplines l Many different methods »call by reference »call by value »call by value-result l How do you pass the parameters? »via. the stack »via. the registers »or a combination

21 21 Homes for Variables? l A Simplistic model »Allocate a data area for each distinct scope »One data area per “sheaf” in scoped table l What about recursion? »Need a data area per invocation (or activation) of a scope »We call this the scope’s activation record »The compiler can also store control information there ! l More complex scheme »One activation record ( AR ) per procedure instance »All the procedure’s scopes share a single AR »Use a stack to keep the activation records

22 22 Memory Layout l Start of the stack l Heap management »free lists l starting location in the text segment Stack Text segment Heap Objects Arrays locals (parameters) 0x7fffffff 0x400000 Reserved Data segment

23 23 Run-time Resources l Execution of a program is initially under the control of the operating system l When a program is invoked: »The OS allocates space for the program »The code is loaded into part of the space »The OS jumps to the entry point (i.e., “ main ” )

24 24 Memory Layout Low Address High Address Memory Code Other Space

25 25 Notes l Our pictures of machine organization have: »Low address at the bottom »High address at the top »Lines delimiting areas for different kinds of data l These pictures are simplifications »E.g., not all memory need be contiguous l In some textbooks Higher addresses are at bottom

26 26 What is Other Space? l Holds all data for the program l Other Space = Data Space l Compiler is responsible for: »Generating code »Orchestrating use of the data area

27 27 Code Generation Goals l Two goals: »Correctness »Speed l Most complications in code generation come from trying to be fast as well as correct

28 28 Assumptions about Execution 1. Execution is sequential; control moves from one point in a program to another in a well-defined order 2. When a procedure is called, control eventually returns to the point immediately after the call Do these assumptions always hold?

29 29 Activations l An invocation of procedure P is an activation of P l The lifetime of an activation of P is »All the steps to execute P »Including all the steps in procedures that P calls

30 30 Lifetimes of Variables l The lifetime of a variable x is the portion of execution in which x is defined l Note that »Lifetime is a dynamic (run-time) concept »Scope is a static concept

31 31 Activation Trees l Assumption (2) requires that when P calls Q, then Q returns before P does l Lifetimes of procedure activations are properly nested l Activation lifetimes can be depicted as a tree

32 32 Example Class Main { void g() { return ; } void f() { g() ; } void m() { g(); f(); }; } m f g g m enter g enter g return f enter g enter g return f return m return activation tree for m() Lifetime of invocations

33 33 Example 2 Class Main { int g() { return 1; }; int f(int x){ if( x == 0) return g(); else return f(x - 1); } int m(){ return f(3) } } What is the activation tree for this example?

34 34 Example 2 m() enter f(3) enter f(2) enter f(1) enter f(0) enter g() enter g() return f(0) return f(1) return f(2) return f(3) return m() return m() f(3) f(2) f(1) g()

35 35 Notes l The activation tree depends on run-time behavior l The activation tree may be different for every program input l Since activations are properly nested, a stack can track currently active procedures

36 36 Example Class Main { g() { return; }; f() { g() }; m() { g(); f(); }; } mStack m

37 37 Example Class Main { g() { return; } f(): Int { g() } m() { g(); f(); } } m g Stack m g

38 38 Example Class Main { g() { return } f() { g() } m() { g(); f(); } } m g f Stack m f

39 39 Example Class Main { g() { return; } f() { g(); } m(){ g(); f(); } } m f g g Stack m f g

40 40 Revised Memory Layout Low Address High Address Memory Code Stack

41 41 Activation Records l On many machine the stack starts at high- addresses and grows towards lower addresses l The information needed to manage one procedure activation is called an activation record (AR) or frame If procedure F calls G, then G ’ s activation record contains a mix of info about F and G.

42 42 What is in G ’ s AR when F calls G? F is “ suspended ” until G completes, at which point F resumes. G ’ s AR contains information needed to resume execution of F. G ’ s AR may also contain: »Actual parameters to G (supplied by F) »G ’ s return value (needed by F) »Space for G ’ s local variables

43 43 The Contents of a Typical AR for G Space for G ’ s return value l Actual parameters l Pointer to the previous activation record »The control link; points to AR of caller of G l Machine status prior to calling G »Contents of registers & program counter »Local variables l Other temporary values

44 44 Example 2, Revisited Class Main { int g() { return 1; }; int f(int x) {if (x==0) return g(); else return f(x - 1); (**) }; void main() { f(3); (*)} AR for f: result argument control link return address

45 45 Stack After Two Calls to f main result 3 (**) f(3) result 2 (*)(*) f(2) Stack fp for f(1) fp for f(2)

46 46 Notes l main has no argument or local variables and its result is never used; its AR is uninteresting l (*) and (**) are return addresses of the invocations of f »The return address is where execution resumes after a procedure call finishes l This is only one of many possible AR designs »Would also work for C, Pascal, FORTRAN, etc.

47 47 The Main Point The compiler must determine, at compile-time, the layout of activation records and generate code that correctly accesses locations in the activation record Thus, the AR layout and the code generator must be designed together!

48 48 Discussion l The advantage of placing the return value 1st in a frame is that the caller can find it at a fixed offset from its own frame l There is nothing magic about this organization »Can rearrange order of frame elements »Can divide caller/callee responsibilities differently »An organization is better if it improves execution speed or simplifies code generation

49 49 Discussion (Cont.) l Real compilers hold as much of the frame as possible in registers »Especially the method result and arguments

50 50 Globals l All references to a global variable point to the same object »Can ’ t store a global in an activation record l Globals are assigned a fixed address once »Variables with fixed address are “ statically allocated ” l Depending on the language, there may be other statically allocated values

51 51 Memory Layout with Static Data Low Address High Address Memory Code Stack Static Data

52 52 Heap Storage l A value that outlives the procedure that creates it cannot be kept in the AR void foo(Foo f) { f.bar = new Bar(); } The Bar object must survive deallocation of foo ’ s AR l Languages with dynamically allocated data use a heap to store dynamic data

53 53 Notes l The code area contains object code »For most languages, fixed size and read only l The static area contains data (not code) with fixed addresses (e.g., global data) »Fixed size, may be readable or writable l The stack contains an AR for each currently active procedure »Each AR usually fixed size, contains locals l Heap contains all other data »In C, heap is managed by malloc and free

54 54 Notes (Cont.) l Both the heap and the stack grow Must take care that they don ’ t grow into each other l Solution: start heap and stack at opposite ends of memory and let the grow towards each other

55 55 Memory Layout with Heap Low Address High Address Memory Code Heap Static Data Stack

56 56 Data Layout l Low-level details of machine architecture are important in laying out data for correct code and maximum performance l Chief among these concerns is alignment

57 57 Alignment l Most modern machines are (still) 32 bit »8 bits in a byte »4 bytes in a word »Machines are either byte or word addressable l Data is word aligned if it begins at a word boundary l Most machines have some alignment restrictions »Or performance penalties for poor alignment

58 58 Alignment (Cont.) l Example: A string “ Hello ” Takes 5 characters (without a terminating \0) To word align next datum, add 3 “ padding ” characters to the string The padding is not part of the string, it ’ s just unused memory

59 59 Procedure Linkage and Activation Records

60 60 Procedure linkages l The linkage convention is the interface used for performing procedure calls »on entry, establish p's environment »at a call, preserve p's environment »after a call, restore p’s environment »on exit, tear down p's environment »in between, handle addressability and lifetimes l Ensures each procedure inherits from caller a valid run-time environment and also restores one for its caller

61 61 Procedure Linkages Standard procedure linkage procedure p prolog epilog pre-call post-return procedure q prolog epilog Procedure has standard prolog standard epilog Each call involves a pre-call sequence post-return sequence

62 62 return address old frame pointer Stack Local variables Calliee saved registers Stack temporaries... argument 5 argument 4 Dynamic area Caller saved registers arguments fp sp l When calling a new procedure, caller: »push any t0-t9 that has a live value on the stack »put arguments 1-4 on a0- a3 »push rest of the arguments on the stack »do a jal or jalr Pre-Call

63 63 return address old frame pointer return address old frame pointer Stack Local variables Callee saved registers Stack temporaries... argument 5 argument 4 Dynamic area Local variables Callee saved registers Caller saved registers arguments Dynamic area fp sp l In a procedure call, the callee at the beginning: »push $fp on the stack »copy $sp+4 to $fp »push $ra on the stack »if any s0-s7 is used in the procedure save it on the stack »create space for local variables on the stack »execute the callee... Prolog stack frame

64 64 return address old frame pointer Stack Local variables Callee saved registers Stack temporaries... argument 5 argument 4 Dynamic area Caller saved registers arguments l In a procedure call, the callee at the end: »put return values on v0,v1 »update $sp using $fp ($fp-8) -... »Pop the callee saved registers from stack »restore $ra from stack »restore $fp from stack »execute jr ra and return to caller fp sp Epilog

65 65 Stack l On return from a procedure call, the caller: »Update $sp to ignore arguments »pop the caller saved registers »Continue... return address old frame pointer Local variables Calliee saved registers Stack temporaries... argument 5 argument 4 Dynamic area fp sp Post-call

66 66 Argument 5: bx (0) Example Program class auxmath { int sum3d(int ax, int ay, int az, int bx, int by, int bz) { int dx, dy, dz; if(ax > ay) dx = ax - bx; else dx = bx - ax; … retrun dx + dy + dz; } … int px, py, pz; px = 10; py = 20; pz = 30; auxmath am; am.sum3d(px, py, pz, 0, 1, -1); return address old frame pointer Dynamic area Caller saved registers Argument 7: bz (-1) fp sp Argument 6: by (1) Local variable dx (??) Local variable dy (??) Local variable dz (??)

67 67 Caller/Callee Responsibility Caller Callee pre-call sequence prolog code allocate basic frame save registers, state evaluate & store params. extend basic frame Call store return address (for local data) store FP find static data area set FP for child initialize locals jump to child fall through to code post-return sequence epilog code copy return value store return value Return deallocate basic frame restore state restore params.(?) unextend basic frame (call-by-value-save) restore parent's FP jump to return address


Download ppt "1 Lecture 9 Runtime Environment. 2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need."

Similar presentations


Ads by Google