Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

Similar presentations


Presentation on theme: "Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture."— Presentation transcript:

1 Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

2 Runtime Organization (Chapter 6) 2 Routines Data Representation: how to represent values of the source language on the target machine Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables ) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

3 Runtime Organization (Chapter 6) 3 Routines We call the assembly language equivalent of procedures “routines”. In the preceding material we already learned some things about the implementation of routines in terms of the stack allocation model: Addressing locals and globals through LB, L1, L2, …, and SB Link data: static link, dynamic link, and return address We have yet to learn how the static link and the L1, L2, etc. registers are set up. We have yet to learn how a routine can receive argument values from its caller, and return results to its caller.

4 Runtime Organization (Chapter 6) 4 Routines We call the assembly language equivalent of procedures “routines”. What are routines? Unlike procedures/functions in higher level languages, they are not directly supported by language constructs. Instead they are modeled in terms of how to use the low-level machine to “emulate” procedures. What behavior needs to be “emulated”? Calling a routine and returning to the caller after completion Passing arguments to a called routine Returning a result from a routine Accessing local and non-local variables

5 Runtime Organization (Chapter 6) 5 Routines Transferring control to and from a routine: Most low-level processors have CALL and RET instructions for transferring control from caller to callee and back. Transmitting arguments and return values: Caller and callee must agree on a method to transfer argument and return values. => This is called the “routine protocol” There are many possible ways to pass argument and return values. => A routine protocol is like a “contract” between the caller and the callee. There are many possible ways to pass argument and return values. => A routine protocol is like a “contract” between the caller and the callee. ! The routine protocol is often dictated by the operating system.

6 Runtime Organization (Chapter 6) 6 Routine Protocol Examples The routine protocol depends on the machine architecture (e.g. stack machine versus register machine). Example 1: A possible routine protocol for a RM - Passing of arguments: first argument in R1, second argument in R2, etc. - Passing of return value: return the result (if any) in R0 Note: this example is simplistic: - What if more arguments than registers? - What if the representation of an argument is larger than can be stored in a register? For RM protocols, the protocol usually also specifies who (caller or callee) is responsible for saving the previous contents of registers.

7 Runtime Organization (Chapter 6) 7 Routine Protocol Examples Example 2: A possible routine protocol for a stack machine - Passing of arguments: pass arguments on the top of the stack. - Passing of return value: leave the return value on the stack top, in place of the arguments. Note: this protocol puts no boundary on the number of arguments and the size of the arguments. Most micro-processors have registers as well as a stack. Such “mixed” machines also often use a protocol like this one. The “Triangle Abstract Machine” also adopts this routine protocol. We now look at it in detail (in TAM).

8 Runtime Organization (Chapter 6) 8 TAM: Routine Protocol SB LB ST globals just before the calljust after return args SB LB ST globals result What happens in between call and return?

9 Runtime Organization (Chapter 6) 9 TAM: Routine Protocol LB ST (1) just before the call args (2) just after entry LB ST args link data Note: Going from (1) to (2) in TAM is the execution of a single CALL instruction.

10 Runtime Organization (Chapter 6) 10 TAM: Routine Protocol (2) just after entry LB ST args link data (3) during execution of routine LB ST args link data local data shrinks and grows during execution (enter/exit blocks, push/pop temporary values)

11 Runtime Organization (Chapter 6) 11 TAM: Routine Protocol (4) just before return LB ST args link data local data result (5) just after return LB ST result Note: Going from (4) to (5) in TAM is the execution of a single RET instruction.

12 Runtime Organization (Chapter 6) 12 TAM: Routine Protocol, Example let var g: Integer; func F(m: Integer, n: Integer) : Integer ~ m*n ; proc W(i:Integer) ~ let const s ~ i*i in begin putint(F(i,s)); putint(F(s,s)) end in begin getint(var g); W(g+1) end Example Triangle Program

13 Runtime Organization (Chapter 6) 13 TAM: Routine Protocol, Example PUSH 1 -- expand globals to make place for g LOADA 0[SB] -- push address of g (pass-by-ref) CALL getint -- read integer into g LOAD 0[SB] -- push value of g CALL succ -- add 1 CALL(SB) W -- call W (using SB as static link) POP 1 -- contract globals PUSH 1 -- expand globals to make place for g LOADA 0[SB] -- push address of g (pass-by-ref) CALL getint -- read integer into g LOAD 0[SB] -- push value of g CALL succ -- add 1 CALL(SB) W -- call W (using SB as static link) POP 1 -- contract globals TAM assembly code: let var g: Integer;... in begin getint(var g); W(g+1) end

14 Runtime Organization (Chapter 6) 14 TAM: Routine Protocol, Example F: LOAD -2[LB] -- push value of argument m LOAD -1[LB] -- push value of argument n CALL mult -- multiply m and n RETURN(1) 2 -- return, replacing 2-word argument pair by 1-word result F: LOAD -2[LB] -- push value of argument m LOAD -1[LB] -- push value of argument n CALL mult -- multiply m and n RETURN(1) 2 -- return, replacing 2-word argument pair by 1-word result func F(m: Integer, n: Integer) : Integer ~ m*n ; Arguments are addressed relative to LB (note the negative offsets!) Size of return value and argument space needed for updating the stack upon return from call.

15 Runtime Organization (Chapter 6) 15 TAM: Routine Protocol, Example W: LOAD -1[LB] -- push value of argument i LOAD -1[LB] -- push value of argument i CALL mult -- multiply: result is value of s ( 3[LB] ) LOAD -1[LB] -- push value of argument i LOAD 3[LB] -- push value of local variable s CALL(SB) F -- call F (using SB as static link) … RETURN(0) 1 -- return, replacing 1-word argument by 0-word result W: LOAD -1[LB] -- push value of argument i LOAD -1[LB] -- push value of argument i CALL mult -- multiply: result is value of s ( 3[LB] ) LOAD -1[LB] -- push value of argument i LOAD 3[LB] -- push value of local variable s CALL(SB) F -- call F (using SB as static link) … RETURN(0) 1 -- return, replacing 1-word argument by 0-word result proc W(i: Integer) ~ let const s~i*i in... F(i,s)...

16 Runtime Organization (Chapter 6) 16 TAM: Routine Protocol, Example let var g: Integer;... in begin getint(var g); W(g+1) end SB g 3 after reading g 3 just before call to W 4 ST SB g ST arg #1

17 Runtime Organization (Chapter 6) 17 TAM: Routine Protocol, Example proc W(i: Integer) ~ let const s~i*i in... F(i,s)... just after entering W 3 4 SB g LB arg #1 ST link data just after computing s 3 4 SB g LB arg i ST link data 16 just before calling F 3 4 SB g LB arg i ST link data 16 4 s arg #1 arg #2 static link dynamic link

18 Runtime Organization (Chapter 6) 18 TAM: Routine Protocol, Example func F(m: Integer, n: Integer) : Integer ~ m*n ; just before calling F 3 4 SB g LB arg i ST link data 16 4 s arg #1 arg #2 just after entering F 3 4 SB g LB arg i ST link data 16 4 s arg m arg n link data just before return from F 3 4 SB g LB arg i ST link data 16 4 s arg m arg n link data 64

19 Runtime Organization (Chapter 6) 19 TAM: Routine Protocol, Example func F(m: Integer, n: Integer) : Integer ~ m*n ; just before return from F 3 4 SB g LB arg i ST link data 16 4 s arg m arg n link data 64 after return from F 3 4 SB g LB arg i ST link data 16 64 s …

20 Runtime Organization (Chapter 6) 20 TAM Routine Protocol: Frame Layout Summary LB ST local variables and intermediate results dynamic link static link return address Local data, grows and shrinks during execution. Link data arguments Arguments for current procedure. They were put here by the caller.

21 Runtime Organization (Chapter 6) 21 Accessing variables, addressing schemas (revised) We now have a complete picture of the different kinds of addresses that are used for accessing variables and formal parameters stored on the stack. Type of variable Global Local Parameter Non-local, 1 level up Non-local, 2 levels up... Load instruction LOAD +offset[SB] LOAD +offset[LB] LOAD -offset[LB] LOAD +offset[L1] LOAD +offset[L2]

22 Runtime Organization (Chapter 6) 22 Static Links We have already seen somewhat how the static link in the call frame is initialized by a parameter of the CALL instruction. But how can the compiler determine this parameter at compile time? To understand this we need to know a few things about the Triangle programming language. Statically scoped, block-structured language. The static link points to the scope where the function was defined. If there are no higher-order funcs or procs, the compiler always knows which func/proc is being called. That func/proc must be defined somewhere within the active scope. So the static link can always be found in one of the display registers!

23 Runtime Organization (Chapter 6) 23 Static Links The static link can always be found in one of the display registers: let proc P( ) ~ let proc Q( ) ~ let proc Q1 Q1body proc Q2 Q2body in Qbody proc S( ) ~ let proc S1 S1body proc S2 S2body in Sbody in Pbody in...Main Program... Call fromto MainP Static link? PQSQS SP S1 S2 Q S SB LB SB LB L1 S2L1 L2 SB S1 S P

24 Runtime Organization (Chapter 6) 24 Arguments We have already discussed how space on the stack is allocated for arguments to routines. We now discuss some specific issues about passing by value versus passing by reference passing of functional and procedural parameters

25 Runtime Organization (Chapter 6) 25 Arguments: by value or by reference Some programming languages allow two kinds of parameter passing to functions/procedures. Example: in Triangle (similar in Pascal or C ++ ) let proc S(var n:Integer, i:Integer) ~ n:=n+i; var today: record y:integer, m:Integer, d:Integer end; in begin b := {y ~ 2002, m ~ 2, d ~ 22}; ! b is non-local S(var b.m, 6); end let proc S(var n:Integer, i:Integer) ~ n:=n+i; var today: record y:integer, m:Integer, d:Integer end; in begin b := {y ~ 2002, m ~ 2, d ~ 22}; ! b is non-local S(var b.m, 6); end Constant/value parameter Var/reference parameter

26 Runtime Organization (Chapter 6) 26 Arguments: by value or by reference Value parameters: At the call site the argument is an expression. The evaluation of that expression leaves some value on the stack. This value is passed to the procedure/function. Typical instructions for putting a value parameter on the stack: LOADL 6LOAD 3[L1] Var/reference parameters : Instead of passing a value on the stack, the address of a memory location is pushed. This implies a restriction that only “variable-like” things can be passed to a var parameter. In Triangle there is an explicit keyword var at the call-site, to signal passing a var parameter. In Pascal and C ++ the reference is created implicitly (but the same restrictions apply). Typical instructions for putting a var parameter on the stack: LOADA 5[LB] LOADA 10[SB]


Download ppt "Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture."

Similar presentations


Ads by Google