compiler construction
play

Compiler construction Martin Steffen March 22, 2017 Contents 1 - PDF document

Compiler construction Martin Steffen March 22, 2017 Contents 1 Abstract 1 1.1 Run-time environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.1 Intro . . . . . . . . . . . . . . . . . . . . . .


  1. Compiler construction Martin Steffen March 22, 2017 Contents 1 Abstract 1 1.1 Run-time environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.1 Intro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.2 Static layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.3 Stack-based runtime environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1.4 Stack-based RTE with nested procedures . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.1.5 Functions as parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.1.6 Virtual methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.1.7 Parameter passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.1.8 Garbage collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 1.1.9 Additional material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2 Reference 38 1 Abstract Abstract This is the handout version of the slides. It contains basically the same content, only in a way which allows more compact printing. Sometimes, the overlays, which make sense in a presentation, are not fully rendered here. Besides the material of the slides, the handout versions may also contain additional remarks and background information which may or may not be helpful in getting the bigger picture. 1.1 Run-time environments 18. 01. 2017 1.1.1 Intro Static & dynamic memory layout at runtime 1. Picture code area global/static area stack free space heap Memory 1

  2. 2. Text typical memory layout : for languages (as nowadays basically all) with • static memory • dynamic memory: – stack – heap Translated program code 1. Picture code for procedure 1 proc. 1 code for procedure 2 proc. 2 ⋮ proc. n code for procedure n Code memory 2. Text • code segment: almost always considered as statically allocated ⇒ neither moved nor changed at runtime • compiler aware of all addresses of “chunks” of code: entry points of the procedures • but: – generated code often relocatable – final, absolute adresses given by linker / loader Activation record 1. Picture space for arg’s (parameters) space for bookkeeping info, including return address space for local data space for local temporaries Schematic activation record 2. Text • schematic organization of activation records/activation block/stack frame . . . • goal: realize – parameter passing – scoping rules /local variables treatment – prepare for call/return behavior • calling conventions on a platform 2

  3. 1.1.2 Static layout Full static layout 1. Picture code for main proc. code for proc. 1 ⋮ code for proc. n global data area act. record of main proc. activation record of proc. 1 ⋮ activation record of proc. n 2. Text • static addresses of all of memory known to the compiler – executable code – variables – all forms of auxiliary data (for instance big constants in the program, e.g., string literals) • for instance: Fortran • nowadays rather seldom (or special applications like safety critical embedded systems) Fortran example P R O G R A M TEST C O M M O N MAXSIZE I N T E G E R MAXSIZE R E A L TABLE( 1 0 ) ,TEMP MAXSIZE = 10 R E A D ∗ , TABLE( 1 ) ,TABLE( 2 ) ,TABLE(3) CALL Q U A D M E A N(TABLE, 3 ,TEMP) PRINT ∗ ,TEMP E N D E Q U A D M E A N(A, SIZE ,Q M E A N) S U B R O U T I N N MAXSIZE C O M M O INTEGERMAXSIZE, SIZE L A( SIZE ) ,QMEAN, TEMP R E A R K I N T E G E TEMP = 0.0 ( ( SIZE . G T .MAXSIZE) . O R . ( SIZE . LT . 1 ) ) G O 99 IF O T O 10 K = 1 , SIZE D TEMP = TEMP + A(K)∗A(K) 10 C O N T I N U E 99 Q M E A N = S Q R T (TEMP/ SIZE ) R E T U R N E N D Static memory layout example/runtime environment 1. Picture 3

  4. global area MAXSIZE (1) TABLE (2) . . . main’s act. (10) record TEMP 3 A SIZE QMEAN return address Act. record of QUADMEAN TEMP K “scratch area” 2. Text in Fortan (here Fortran77) • parameter passing as pointers to the actual parameters • activation record for QUADMEAN contains place for intermediate results, compiler calculates, how much is needed. • note: one possible memory layout for FORTRAN 77, details vary, other implementations exists as do more modern versions of Fortran 1.1.3 Stack-based runtime environments Stack-based runtime environments • so far: no recursion • everything static, including placement of activation records ⇒ also return addresses statically known • a very ancient and restrictive arrangement of the run-time envs • calls and returns (also without recursion) follow at runtime a LIFO (= stack-like ) discipline 1. Stack of activation records • procedures as abstractions with own local data ⇒ run-time memory arrangement where procedure-local data together with other info (arrange proper returns, parameter passing) is organized as stack. 2. Rest • AKA: call stack , runtime stack • AR: exact format depends on language and platform Situation in languages without local procedures • recursion, but all procedures are global • C-like languages 1. Activation record info (besides local data, see later) 4

  5. • frame pointer • control link (or dynamic link ) 1 • (optional): stack pointer • return address Euclid’s recursive gcd algo #include <s t d i o . h> int x , y ; int gcd ( int u , int v ) { i f ( v==0) return u ; else return gcd (v , u % v ) ; } main ( ) int { s c a n f ( "%d%d",&x,&y ) ; p r i n t f ( "%d\n" , gcd (x , y ) ) ; 0 ; return } Stack gcd 1. Picture x:15 global/static area y:10 “AR of main” x:15 y:10 a-record (1st. call) control link return address x:10 y:5 a-record (2nd. call) control link return address x:5 y:0 a-record (3rd. call) control link fp return address sp ↓ 2. Text • control link – aka: dynamic link – refers to caller’s FP • frame pointer FP – points to a fixed location in the current a-record • stack pointer (SP) – border of current stack and unused memory • return address : program-address of call-site 1 Later, we’ll encounter also static links (aka access links). 5

  6. Local and global variables and scoping 1. Code x = 2 ; /∗ g l o b a l var ∗/ int g ( int ) ; /∗ prototype ∗/ void void f ( int n) { static int x = 1 ; g (n ) ; x − − ; } void g ( int m) { int y = m − 1; i f ( y > 0) { f ( y ) ; x − − ; g ( y ) ; } } int main ( ) { g ( x ) ; return 0 ; } 2. Text • global variable x • but: (different) x local to f • remember C: – call by value – static lexical scoping Activation records and activation trees • activation of a function: corresponds to the call of a function • activation record – data structure for run-time system – holds all relevant data for a function call and control-info in “standardized” form – control-behavior of functions: LIFO – if data cannot outlive activation of a function ⇒ activation records can be arranged in as stack (like here) – in this case: activation record AKA stack frame 1. GCD main() gcd(15,10) gcd(10,5) gcd(5,0) 2. f and g example main g(2) f(1) g(1) g(1) 6

  7. Variable access and design of ARs 1. Layout g • fp : frame pointer • m (in this example): parameter of g 2. Possible arrangement of g’s AR • AR’s: structurally uniform per language (or at least compiler) / platform • different function defs, different size of AR ⇒ frames on the stack differently sized • note: FP points – not to the “top” of the frame/stack, but – to a well-chosen, well-defined position in the frame – other local data (local vars) accessible relative to that • conventions – higher addresses “higher up” – stack “grows” towards lower addresses – in the picture: “pointers” to the “bottom” of the meant slot (e.g.: fp points to the control link) Layout for arrays of statically known size 1. Code f ( int x , c ) void char { int a [ 1 0 ] ; double y ; . . } name offset x +5 c +4 -24 a -32 y (a) access of c and y c : 4( fp ) y : − 32( fp ) (b) access for A[i] ( − 24+2∗ i ) ( fp ) 2. Layout 7

  8. Back to the C code again (global and local variables) x = 2 ; /∗ g l o b a l var ∗/ int g ( int ) ; /∗ prototype ∗/ void f ( int n) void { static int x = 1 ; g (n ) ; x − − ; } void g ( int m) { int y = m − 1; i f ( y > 0) { f ( y ) ; x − − ; g ( y ) ; } } int main ( ) { g ( x ) ; return 0 ; } 2 snapshots of the call stack 1. Picture x:2 static x:1 (@f) main m:2 control link g return address y:1 n:1 f control link return address m:1 control link g fp return address y:0 sp ... 8

Recommend


More recommend