Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.

Similar presentations


Presentation on theme: "Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction."— Presentation transcript:

1 Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

2 – 2 – CSCE 531 Spring 2006 Overview Last Time Project 3 due tonight Project 4 due March 28 Tuesday Memory Layout of C Processes Runtime – Memory allocation Activation Records Today’s Lecture Boolean HW Trace Problem Parameter Passing Procedure Declaration References: Sections 7 Homework:

3 – 3 – CSCE 531 Spring 2006 Seeing the Activation Record Examples In Examples/ActRec Param.c – to illustrate multiple arguments Param.c – to illustrate multiple arguments Comb.c Comb.c Fib.c Fib.c In Examples/ActRec Param.c – Param.c – Comb.c Comb.c Fib.c Fib.c

4 – 4 – CSCE 531 Spring 2006 Param.[cs] int func(int, int, int); main(){ int i, j, k; int i, j, k; k = func(1, 2.0, 3); k = func(1, 2.0, 3);}int func (int x, int y, int z){ int sum; int sum; sum = x+y+ z; sum = x+y+ z; return sum; return sum;} Main: … pushl $3 pushl $3 pushl $2 pushl $2 pushl $1 pushl $1 call func call func addl $16, %esp addl $16, %esp movl %eax, -12(%ebp) movl %eax, -12(%ebp) …func: pushl %ebp pushl %ebp movl %esp, %ebp movl %esp, %ebp subl $4, %esp subl $4, %esp movl 12(%ebp), %eax movl 12(%ebp), %eax addl 8(%ebp), %eax addl 8(%ebp), %eax addl 16(%ebp), %eax addl 16(%ebp), %eax movl %eax, -4(%ebp) movl %eax, -4(%ebp) movl -4(%ebp), %eax movl -4(%ebp), %eax leave leave ret ret Notes:  Arguments pushed in reverse  Return value ret

5 – 5 – CSCE 531 Spring 2006 Sample “Semantics” Test Problem Provide semantic actions to generate quadruples for the sum expression which is defined below E  sum '(' ID '=' E ';' E '; ' E ')' E  sum '(' ID '=' E ';' E '; ' E ')' which has the semantics that the value of the expression is the sum from the first E on the right hand side to the second E of the third expression. For instance sum(i=1; 10; i*i) = 1*1 + 2*2... 10*10

6 – 6 – CSCE 531 Spring 2006 Proposed solution E  sum '(' ID '=' E ';' E '; ' E ')' Insert Markers

7 – 7 – CSCE 531 Spring 2006 Placing the N marker state 29 // for some grammar this was the lang.output S -> IF B THEN M S. (rule 11) S -> IF B THEN M S. (rule 11) S -> IF B THEN M S. N ELSE M S (rule 12) S -> IF B THEN M S. N ELSE M S (rule 12) ELSE reduce using rule 11 (S) ELSE reduce using rule 11 (S) ELSE [reduce using rule 15 (N)] ELSE [reduce using rule 15 (N)] $default reduce using rule 11 (S) $default reduce using rule 11 (S) N go to state 35 N go to state 35

8 – 8 – CSCE 531 Spring 2006 Placing Run-time Data Structures Classic Organization Code, static, & global data have known size  Use symbolic labels in the code Heap & stack both grow & shrink over time This is a virtual address space Better utilization if stack & heap grow toward each other Very old result (Knuth) Code & data separate or interleaved Uses address space, not allocated memory CodeCode S G t l a & o t b i a c l HeapHeap StackStack Single Logical Address Space 0 high Slide from “Engineering a Compiler” Cooper and Torczon

9 – 9 – CSCE 531 Spring 2006 How Does Virtual Memory Work? The Big Picture CodeCode S G t l a & o t b i a c l HeapHeap StackStack CodeCode S G t l a & o t b i a c l HeapHeap StackStack CodeCode S G t l a & o t b i a c l H e a p StackStack... Hardware’s view Compiler’s view OS’s view Physical address space_ virtual address spaces 0high CodeCode S G t l a & o t b i a c l HeapHeap StackStack Slide from Engr. A Compiler by Cooper and Torzon

10 – 10 – CSCE 531 Spring 2006 Example: params2.c #include #include main(){ int i, j, k; int i, j, k; i = 7; j = 2; k = 3; i = 7; j = 2; k = 3; printf(“f(%d)=%d\n", i, f(i+j, j, k+6)); printf(“f(%d)=%d\n", i, f(i+j, j, k+6));} int f(int n, int m, int p){ int t1, t2; int t1, t2; t1 = n+m; t1 = n+m; t2 = m*p; t2 = m*p; return(2*t1+t2); return(2*t1+t2);}

11 – 11 – CSCE 531 Spring 2006 Gcc –S –O params.c: The Main … header … … header …main: … pushl $10 // Note the optimizer at work! pushl $10 // Note the optimizer at work! pushl $2 pushl $2 pushl $9// Which arg is this? pushl $9// Which arg is this? call f call f

12 – 12 – CSCE 531 Spring 2006 Gcc –S –O params.c: The Function fib: pushl %ebp// Prologue pushl %ebp// Prologue movl %esp, %ebp// movl %esp, %ebp// movl 12(%ebp), %edx movl 12(%ebp), %edx movl 8(%ebp), %eax movl 8(%ebp), %eax addl %edx, %eax addl %edx, %eax imull 16(%ebp), %edx imull 16(%ebp), %edx leal (%edx,%eax,2), %eax leal (%edx,%eax,2), %eax popl %ebp// Epilogue popl %ebp// Epilogue ret// return value in register %eax ret// return value in register %eax

13 – 13 – CSCE 531 Spring 2006 Comments on IA32 Instructions pushl %ebp// esp=esp-4; M[esp]=ebp pushl %ebp// esp=esp-4; M[esp]=ebp movl %esp, %ebp// movl %esp, %ebp// movl 12(%ebp), %edx// %edx  M[ Reg[%ebp]+12] movl 12(%ebp), %edx// %edx  M[ Reg[%ebp]+12] movl 8(%ebp), %eax movl 8(%ebp), %eax addl %edx, %eax addl %edx, %eax imull 16(%ebp), %edx imull 16(%ebp), %edx leal (%edx,%eax,2), %eax leal (%edx,%eax,2), %eax popl %ebp// popl %ebp// ret ret

14 – 14 – CSCE 531 Spring 2006 Stack Support for C in IA32 %esp – stack pointer %ebp - “frame” pointer Note the stack grows down from high memory 0xFFFFF0F0 i = 7; j = 2; k = 3; f(i+j, j, k+6)); … int f(int n, int m, int p){ int t1, t2; int t1, t2; %ebp

15 – 15 – CSCE 531 Spring 2006 Revisiting the Function code fib: pushl %ebp pushl %ebp movl %esp, %ebp movl %esp, %ebp movl 12(%ebp), %edx movl 12(%ebp), %edx movl 8(%ebp), %eax movl 8(%ebp), %eax addl %edx, %eax addl %edx, %eax imull 16(%ebp), %edx imull 16(%ebp), %edx leal (%edx,%eax,2), %eax leal (%edx,%eax,2), %eax popl %ebp popl %ebp ret ret

16 – 16 – CSCE 531 Spring 2006 Grammar for C Functions Function definition funcDef  type ID (parmlist) ‘{‘ Decls L ‘}’ Function invocation (calls) expr  ID ‘(’ arglist ‘)’ Attributes: parmlist – list of id.places arglist - list of Id.places (reverse order)

17 – 17 – CSCE 531 Spring 2006 Semantic Actions for non-nested scopes expr  ID ‘(’ arglist ‘)’ {p = arglist.list; while(p != NULL){ while(p != NULL){ gen(push, -, -, p  place); p = p  link; } gen (call, -, -, ID.place); gen (call, -, -, ID.place);}

18 – 18 – CSCE 531 Spring 2006 Semantic Actions for non-nested scopes funcDef  type ID (parmlist) ‘{‘ Decls L ‘}’ Emit Prologue Emit Body Emit Epilogue

19 – 19 – CSCE 531 Spring 2006 Translating Local Names How does the compiler represent a specific instance of x ? Name is translated into a static coordinate pair “level” is lexical nesting level of the procedure “offset” is unique within that scope Subsequent code will use the static coordinate to generate addresses and references “level” is a function of the table in which x is found Stored in the entry for each x “offset” must be assigned and stored in the symbol table Assigned at compile time Known at compile time Used to generate code that executes at run-time Slide from “Engineering a Compiler” Cooper and Torczon

20 – 20 – CSCE 531 Spring 2006 Storage for Blocks within a Single Procedure Fixed length data can always be at a constant offset from the beginning of a procedure In our example, the a declared at level 0 will always be the first data element, stored at byte 0 in the fixed-length data area The x declared at level 1 will always be the sixth data item, stored at byte 20 in the fixed data area The x declared at level 2 will always be the eighth data item, stored at byte 28 in the fixed data area But what about the a declared in the second block at level 2? B0: { int a, b, c B1: { int v, b, x, w B2:{ int x, y, z ….} B3:{ int x, a, v …}…}…} Slide from “Engineering a Compiler” Cooper and Torczon

21 – 21 – CSCE 531 Spring 2006 Variable-length Data Arrays If size is fixed at compile time, store in fixed-length data area If size is variable, store descriptor in fixed length area, with pointer to variable length area Variable-length data area is assigned at the end of the fixed length area for block in which it is allocated B0: { int a, b … assign value to a B1: { int v(a), b, x B2:{ int x, y(8) ….} abvbxxy(8)v(a) Variable-length data Includes variable length data for all blocks in the procedure …

22 – 22 – CSCE 531 Spring 2006 Activation Record Review parameters register save area return value return address addressability caller’s ARP local variables ARP Space for parameters to the current routine Saved register contents If function, space for return value Address to resume caller Help with non-local access To restore caller’s AR on a return Space for local values & variables (including spills) One AR for each invocation of a procedure

23 – 23 – CSCE 531 Spring 2006 Activation Record Details How does the compiler find the variables? They are at known offsets from the AR pointer The static coordinate leads to a “loadAI” operation Level specifies an ARP, offset is the constant Variable-length data If AR can be extended, put it after local variables Leave a pointer at a known offset from ARP Otherwise, put variable-length data on the heap Initializing local variables Must generate explicit code to store the values Among the procedure’s first actions

24 – 24 – CSCE 531 Spring 2006 Communicating Between Procedures Most languages provide a parameter passing mechanism Expression used at “call site” becomes variable in callee Two common binding mechanisms Call-by-reference passes a pointer to actual parameter Requires slot in the AR (for address of parameter) Multiple names with the same address? Call-by-value passes a copy of its value at time of call Requires slot in the AR Each name gets a unique location (may have same value) Arrays are mostly passed by reference, not value Can always use global variables … call fee(x,x,x);

25 – 25 – CSCE 531 Spring 2006 Establishing Addressability Must create base addresses Global & static variables Construct a label by mangling names (i.e., &_fee) Local variables Convert to static data coordinate and use ARP + offset Local variables of other procedures Convert to static coordinates Find appropriate ARP Use that ARP + offset { Must find the right AR Need links to nameable AR s

26 – 26 – CSCE 531 Spring 2006 Establishing Addressability Using access links Each AR has a pointer to AR of lexical ancestor Lexical ancestor need not be the caller Reference to runs up access link chain to p Cost of access is proportional to lexical distance parameters register save area return value return address access link caller’s ARP local variables A RP parameters register save area return value return address access link caller’s ARP local variables parameters register save area return value return address access link caller’s ARP local variables Some setup cost on each call

27 – 27 – CSCE 531 Spring 2006 Establishing Addressability Using access links Access & maintenance cost varies with level All accesses are relative to ARP (r 0 ) Assume Current lexical level is 2 Access link is at ARP - 4 Maintaining access link Calling level k+1  Use current ARP as link Calling level j < k  Find ARP for j –1  Use that ARP as link

28 – 28 – CSCE 531 Spring 2006 Establishing Addressability Using a display Global array of pointer to nameable AR s Needed ARP is an array access away Reference to looks up p’s ARP in display & adds 16 Cost of access is constant ( ARP + offset) ARP parameters register save area return value return address saved ptr. caller’s ARP local variables parameters register save area return value return address saved ptr. caller’s ARP local variables level 0 level 1 level 2 level 3 Display parameters register save area return value return address saved ptr. caller’s ARP local variables Some setup cost on each call


Download ppt "Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction."

Similar presentations


Ads by Google