Chapter 8 Run-time environments Course “Compiler Construction” Martin Steffen Spring 2018
Section Targets Chapter 8 “Run-time environments” Course “Compiler Construction” Martin Steffen Spring 2018
Chapter 8 Learning Targets of Chapter “Run-time environ- ments”. 1. memory management 2. run-time environment 3. run-time stack 4. stack frames and their layout 5. heap
Chapter 8 Outline of Chapter “Run-time environments”. Targets Intro Static layout Stack-based runtime environments Stack-based RTE with nested procedures Functions as parameters Parameter passing Virtual methods in OO Garbage collection
Section Intro Chapter 8 “Run-time environments” Course “Compiler Construction” Martin Steffen Spring 2018
Static & dynamic memory layout at runtime INF5110 – Compiler Construction code area global/static area Targets typical memory layout : for languages (as Targets & Outline nowadays basically all) with stack Intro • static memory Static layout • dynamic memory: free space Stack-based runtime • stack environments • heap Stack-based RTE heap with nested procedures Memory Functions as parameters Parameter passing Virtual methods in OO Garbage collection 8-6
Translated program code • code segment: almost INF5110 – Compiler always considered as Construction statically allocated ⇒ neither moved nor Targets proc. 1 code for procedure 1 changed at runtime Targets & Outline code for procedure 2 proc. 2 Intro • compiler aware of all ⋮ Static layout addresses of “chunks” of Stack-based code: entry points of the runtime code for procedure n proc. n environments procedures Stack-based RTE with nested • but: Code memory procedures • generated code often Functions as parameters relocatable • final, absolute adresses Parameter passing given by linker / loader Virtual methods in OO Garbage collection 8-7
Activation record INF5110 – Compiler Construction space for arg’s (parameters) • schematic organization of Targets activation records/activation space for bookkeeping Targets & Outline info, including return block/stack frame . . . address Intro • goal: realize Static layout • parameter passing Stack-based space for local data • scoping rules /local variables runtime environments treatment Stack-based RTE space for local temporaries • prepare for call/return behavior with nested procedures • calling conventions on a platform Schematic activation record Functions as parameters Parameter passing Virtual methods in OO Garbage collection 8-8
Section Static layout Chapter 8 “Run-time environments” Course “Compiler Construction” Martin Steffen Spring 2018
Full static layout INF5110 – Compiler • static addresses of all of memory Construction known to the compiler code for main proc. • executable code Targets • variables code for proc. 1 Targets & Outline • all forms of auxiliary data (for ⋮ Intro instance big constants in the Static layout code for proc. n program, e.g., string literals) Stack-based global data area runtime • for instance: (old) Fortran environments act. record of main proc. Stack-based RTE • nowadays rather seldom (or special activation record of proc. 1 with nested procedures ⋮ applications like safety critical Functions as activation record of proc. n embedded systems) parameters Parameter passing Virtual methods in OO Garbage collection 8-10
Fortran example PROGRAM TEST INF5110 – C O M M O N MAXSIZE Compiler INTEGER MAXSIZE Construction REAL TABLE(10) ,TEMP MAXSIZE = 10 READ ∗ , TABLE(1) ,TABLE(2 ) ,TABLE(3) Targets CALL QUADMEAN(TABLE, 3 ,TEMP) Targets & Outline PRINT ∗ ,TEMP Intro END Static layout SUBROUTINE QUADMEAN(A, SIZE ,QMEAN) Stack-based C O M M O N MAXSIZE runtime environments INTEGERMAXSIZE , SIZE REAL A( SIZE ) ,QMEAN, TEMP Stack-based RTE with nested INTEGER K procedures TEMP = 0.0 IF (( SIZE . GT . MAXSIZE ) . OR . ( SIZE . LT . 1 ) ) GOTO 99 Functions as parameters DO 10 K = 1 , SIZE TEMP = TEMP + A(K)∗A(K) Parameter passing 10 CONTINUE Virtual methods in 99 QMEAN = SQRT (TEMP/ SIZE ) OO RETURN Garbage collection END 8-11
Static memory layout example/runtime environment INF5110 – Compiler global area MAXSIZE Construction TABLE (1) (2) Targets . . . main’s act. (10) Targets & Outline record Intro TEMP Static layout 3 Stack-based A runtime environments SIZE Stack-based RTE QMEAN with nested procedures return address Act. record of Functions as QUADMEAN TEMP parameters K Parameter passing Virtual methods in “scratch area” OO Garbage collection 8-12
Static memory layout example/runtime environment INF5110 – Compiler Construction in Fortan (here Fortran77) Targets • parameter passing as pointers to the actual parameters Targets & Outline Intro • activation record for QUADMEAN contains place for Static layout intermediate results, compiler calculates, how much is Stack-based needed. runtime environments • note: one possible memory layout for FORTRAN 77, Stack-based RTE with nested details vary, other implementations exists as do more procedures modern versions of Fortran Functions as parameters Parameter passing Virtual methods in OO Garbage collection 8-13
Section Stack-based runtime environments Chapter 8 “Run-time environments” Course “Compiler Construction” Martin Steffen Spring 2018
Stack-based runtime environments • so far: no(!) recursion • everything static, incl. placement of activation records INF5110 – Compiler ⇒ also return addresses statically known Construction • ancient and restrictive arrangement of the run-time envs Targets • calls and returns (also without recursion) follow at Targets & Outline runtime a LIFO (= stack-like) discipline Intro Static layout Stack of activation records Stack-based runtime • procedures as abstractions with own local data environments Stack-based RTE ⇒ run-time memory arrangement where procedure-local with nested procedures data together with other info (arrange proper returns, Functions as parameter passing) is organized as stack. parameters Parameter passing Virtual methods in • AKA: call stack , runtime stack OO Garbage collection • AR: exact format depends on language and platform 8-15
Situation in languages without local procedures INF5110 – Compiler Construction • recursion, but all procedures are global Targets • C-like languages Targets & Outline Intro Activation record info (besides local data, see later) Static layout Stack-based • frame pointer runtime environments • control link (or dynamic link ) 1 Stack-based RTE with nested • (optional): stack pointer procedures • return address Functions as parameters Parameter passing Virtual methods in OO Garbage collection 8-16 1 Later, we’ll encounter also static links (aka access links).
Euclid’s recursive gcd algo INF5110 – Compiler Construction #i n c l u d e <s t d i o . h> Targets i n t x , y ; Targets & Outline i n t gcd ( i n t u , i n t v ) Intro { i f ( v==0) return u ; Static layout e l s e return gcd ( v , u % v ) ; } Stack-based runtime environments i n t main () Stack-based RTE { s c a n f ( "%d%d" ,&x ,&y ) ; with nested p r i n t f ( "%d\n" , gcd ( x , y ) ) ; procedures return 0; Functions as } parameters Parameter passing Virtual methods in OO Garbage collection 8-17
Stack gcd INF5110 – Compiler • control link Construction x:15 • aka: dynamic link global/static area y:10 • refers to caller’s FP Targets “AR of main” x:15 • frame pointer FP Targets & Outline y:10 a-record (1st. call) control link Intro • points to a fixed location return address x:10 Static layout in the current a-record y:5 a-record (2nd. call) Stack-based control link • stack pointer (SP) runtime return address environments x:5 • border of current stack y:0 a-record (3rd. call) Stack-based RTE control link and unused memory fp with nested return address sp procedures ↓ • return address: Functions as program-address of call-site parameters Parameter passing Virtual methods in OO Garbage collection 8-18
Local and global variables and scoping i n t x = 2 ; /∗ g l o b a l var ∗/ void g ( i n t ) ; /∗ prototype INF5110 – Compiler ∗/ Construction void f ( i n t n ) { s t a t i c i n t x = 1; Targets g ( n ) ; Targets & Outline x −− ; • global variable x Intro } • but: (different) x local to Static layout void g ( i n t m) f Stack-based { i n t y = m − 1; runtime • remember C: i f ( y > 0) environments { f ( y ) ; Stack-based RTE • call by value x −− ; with nested • static lexical scoping procedures g ( y ) ; } Functions as parameters } Parameter passing i n t main () Virtual methods in { g ( x ) ; OO return 0; Garbage collection } 8-19
Recommend
More recommend