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 Storage Allocation 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 Static Storage Allocation Example: Global variables in Triangle let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; in... let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; in... Exist as long as program is running Compiler can: compute exactly how much memory is needed for all globals. allocate memory at a fixed position for each global variable.

4 Runtime Organization (Chapter 6) 4 Static Storage Allocation address[a] = 0 address[b] = 3 address[c] = 4 address[t] = 5 a[0] a[1] a[2] a b c t.y t.m t.d t let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; Example: Global variables in Triangle

5 Runtime Organization (Chapter 6) 5 Stack Storage Allocation let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end Example: When do the variables in this program “exist” as long as the program is running when procedure Y is active when procedure Z is active Now we will look at allocation of local variables

6 Runtime Organization (Chapter 6) 6 Stack Storage Allocation Start of programEnd of program time call depth global Y Z 1 2 Y Z 1. Procedure activation behaves like a stack (LIFO). 2. The local variables “live” only as long as the procedure in which they are declared. Therefore => Allocation of locals on the “call stack” is a good model. A “picture” of our program running:

7 Runtime Organization (Chapter 6) 7 Stack Storage Allocation: Accessing locals/globals First time around, we assume that in a procedure only local variables declared in that procedure and global variables are accessible. (Essentially, a “flat” block structure.) Later we will extend this idea to include nested scopes and parameters. A stack allocation model (under the above assumption): Globals are allocated at the base of the stack. Stack contains “frames”. Each frame corresponds to a currently active procedure. (often called an “activation frame”) When a procedure is called (activated) a frame is pushed onto the stack When a procedure returns, its frame is popped off the stack

8 Runtime Organization (Chapter 6) 8 Stack Storage Allocation: Accessing locals/globals SB LB ST call frame SB = Stack base LB = Locals base ST = Stack top call frame Dynamic link globals

9 Runtime Organization (Chapter 6) 9 What’s in a Frame? Each frame contains Dynamic link: points to next frame on the stack (the frame of the caller) Return address Local variables for the current activation return address locals Link data Local data LB ST dynamic link

10 Runtime Organization (Chapter 6) 10 What Happens when a Procedure is called? LB ST SB = Stack base LB = Locals base ST = Stack top call frame call frame When procedure f( ) is called push new f( ) call frame on top of stack. Make dynamic link in new frame point to old LB Update LB (becomes old ST ) new call frame for f( )

11 Runtime Organization (Chapter 6) 11 What Happens when the Procedure returns? LB ST When procedure f( ) returns Update LB (from dynamic link) Update ST (to old LB ) current call frame for f( ) current call frame for f( ) call frame Note: updating the ST implicitly “destroys” or “pops” the frame.

12 Runtime Organization (Chapter 6) 12 Accessing global/local variables Q: Is the stack frame for a particular procedure always at the same position in the stack? A: No, look at picture of procedure activation below. Imagine what the stack looks like at each point in time. time call depth global Y Z 1 2 Y Z G Y G Z Y

13 Runtime Organization (Chapter 6) 13 Accessing global/local variables The global frame is always at the same place in the stack. => Address global variables relative to SB A typical instruction to access a global variable: LOAD 4[SB] Frames are not always at the same position in the stack. Depends on the number of frames already on the stack. => Address local variables relative to LB A typical instruction to access a local variable: LOAD 3[LB] Recall: We are still working under the assumption of a “flat” block structure How do we access global and local variables on the stack?

14 Runtime Organization (Chapter 6) 14 Accessing global/local variables Example: Compute the addresses of the variables in this program let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end Var Size Address abcdefabcdef 3 1 1 [0]SB [3]SB [4]SB 1 ? 1 [2]LB [3]LB [2]LB

15 Runtime Organization (Chapter 6) 15 Accessing non-local variables Recap: We have discussed Stack allocation of locals in the call frames of procedures Some other things stored in frames: A dynamic link: points to the previous frame on the stack => corresponds to the “caller” of the current procedure. A return address: points to the next instruction of the caller. Addressing global variables relative to SB (stack base) Addressing local variables relative to LB (locals base) Next, we will look at accessing non-local variables. Or in other words, we will attempt to answer the question: How does lexical scoping work?

16 Runtime Organization (Chapter 6) 16 Accessing non-local variables: what is this about? Example: How to access p1, p2 from within Q or S? let var g1: array 3 of Integer; var g2: Boolean; proc P( ) ~ let var p1, p2 proc Q( ) ~ let var q:... in... ; proc S( ) ~ let var s: Integer; in... let var g1: array 3 of Integer; var g2: Boolean; proc P( ) ~ let var p1, p2 proc Q( ) ~ let var q:... in... ; proc S( ) ~ let var s: Integer; in... Scope Structure var g1,g2 proc P( ) var p1,p2 proc Q( ) proc S( ) var q Q: When inside Q, does the dynamic link always point to frame of P? var s

17 Runtime Organization (Chapter 6) 17 Accessing non-local variables Q: When inside Q, does the dynamic link always point to frame of P? A: No! Consider following scenarios: var g1,g2 proc P( ) var p1,p2 proc Q( ) proc S( ) var q var s time G 1 P S Q G P 1 2 3 Q 2 3 P S Q P Q

18 Runtime Organization (Chapter 6) 18 Accessing non-local variables So, we can not rely on the dynamic link to get to the lexically scoped frame(s) of a procedure. => Another item is added in the link data: the static link. The static link in a frame points to the next lexically scoped frame, which must be somewhere earlier on the stack. Registers L1, L2, etc. are used to point to the lexically scoped frames. (L1 is more local than L2, so L0 would just be the same as LB.) Typical instructions for accessing a non-local variable: LOAD [4]L1 LOAD [3]L2 These and LB and SB are called the display registers

19 Runtime Organization (Chapter 6) 19 What’s in a Frame (revised)? Each frame contains Static link: points to the frame of the enclosing procedure Dynamic link: points to next frame on the stack (the frame of the caller) Return address Local variables for the current activation static link locals Link data Local data LB ST dynamic link return address

20 Runtime Organization (Chapter 6) 20 Accessing non-local variables proc P( ) proc Q( )... proc S( )... time G P S Q LB ST P( ) frame globals SB S( ) frame Q( ) frame Dynamic L. Static Link L1

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


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