Title: Lecture 18 Procedures
1Lecture 18 Procedures
CSCE 531 Compiler Construction
- Topics
- Procedural Abstraction
- Activation records
- Readings 7.4
March 20, 2006
2Overview
- Last Time
- Project 3 due tonight
- Project 4 due March 28 Tuesday
- Memory Layout of C Processes
- Runtime Memory allocation
- Activation Records
- Todays Lecture
- Boolean HW
- Trace Problem
- Parameter Passing
- Procedure Declaration
-
- References Sections 7
- Homework
3Seeing the Activation Record Examples
- In Examples/ActRec
- Param.c to illustrate multiple arguments
- Comb.c
- Fib.c
- In Examples/ActRec
- Param.c
- Comb.c
- Fib.c
4Param.cs
- Main
- pushl 3
- pushl 2
- pushl 1
- call func
- addl 16, esp
- movl eax, -12(ebp)
-
- func
- pushl ebp
- movl esp, ebp
- subl 4, esp
- movl 12(ebp), eax
- addl 8(ebp), eax
- addl 16(ebp), eax
- movl eax, -4(ebp)
- movl -4(ebp), eax
- leave
- ret
- Notes
- Arguments pushed in reverse
- Return value ret
- int func(int, int, int)
- main()
- int i, j, k
- k func(1, 2.0, 3)
-
- int
- func (int x, int y, int z)
- int sum
- sum xy z
- return sum
5Sample Semantics Test Problem
- Provide semantic actions to generate quadruples
for the sum expression which is defined below - 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(i1 10 ii) 11 22 ...
1010
6Proposed solution
- E ? sum '(' ID '' E '' E ' ' E
')' - Insert Markers
7Placing the N marker
- state 29 // for some grammar this was the
lang.output - S -gt IF B THEN M S . (rule 11)
- S -gt IF B THEN M S . N ELSE M S (rule 12)
- ELSE reduce using rule 11 (S)
- ELSE reduce using rule 15 (N)
- default reduce using rule 11 (S)
- N go to state 35
8Placing Run-time Data Structures
- Better utilization if
- stack heap grow
- toward each other
- Very old result (Knuth)
- Code data separate or
- interleaved
- Uses address space,
- not allocated memory
- 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
Slide from Engineering a Compiler Cooper and
Torczon
9How Does Virtual Memory Work?
virtual address spaces
Compilers view
...
OSs view
0
high
Physical address space_
Hardwares view
Slide from Engr. A Compiler by Cooper and Torzon
10Example params2.c
- include ltstdio.hgt
- main()
- int i, j, k
- i 7 j 2 k 3
- printf(f(d)d\n", i, f(ij, j, k6))
-
- int f(int n, int m, int p)
- int t1, t2
- t1 nm
- t2 mp
- return(2t1t2)
11Gcc S O params.c The Main
- header
- main
-
- pushl 10 // Note the optimizer
at work! - pushl 2
- pushl 9 // Which arg is this?
- call f
12Gcc S O params.c The Function
- fib
- pushl ebp // Prologue
- movl esp, ebp //
- movl 12(ebp), edx
- movl 8(ebp), eax
- addl edx, eax
- imull 16(ebp), edx
- leal (edx,eax,2), eax
- popl ebp // Epilogue
- ret // return value in register eax
13Comments on IA32 Instructions
- pushl ebp // espesp-4 Mespebp
- movl esp, ebp //
- movl 12(ebp), edx // edx ? M
Regebp12 - movl 8(ebp), eax
- addl edx, eax
- imull 16(ebp), edx
- leal (edx,eax,2), eax
- popl ebp //
- ret
14Stack Support for C in IA32
- esp stack pointer
- ebp - frame pointer
- Note the stack grows down from high memory
ebp
i 7 j 2 k 3 f(ij, j, k6)) int
f(int n, int m, int p) int t1, t2
15Revisiting the Function code
- fib
- pushl ebp
- movl esp, ebp
- movl 12(ebp), edx
- movl 8(ebp), eax
- addl edx, eax
- imull 16(ebp), edx
- leal (edx,eax,2), eax
- popl ebp
- ret
16Grammar 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)
17Semantic Actions for non-nested scopes
- expr ? ID ( arglist ) p arglist.list
- while(p ! NULL)
- gen(push, -, -, p?place)
- p p?link
-
- gen (call, -, -, ID.place)
-
18Semantic Actions for non-nested scopes
- funcDef ? type ID (parmlist) Decls L
- Emit Prologue
- Emit Body
- Emit Epilogue
19Translating Local Names
- How does the compiler represent a specific
instance of x ? - Name is translated into a static coordinate
- lt level,offset gt 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
20Storage for Blocks within a Single Procedure
B0 int a, b, c B1 int v, b, x,
w B2 int x, y, z . B3
int x, a, v
- 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?
Slide from Engineering a Compiler Cooper and
Torczon
21Variable-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) .
a
b
v
b
x
x
y(8)
v(a)
Variable-length data
Includes variable length data for all blocks in
the procedure
22Activation Record Review
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 callers AR on a return
Space for local values variables (including
spills)
One AR for each invocation of a procedure
23Activation 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 procedures first actions
24Communicating 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)
25Establishing 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
26Establishing Addressability
- Using access links
- Each AR has a pointer to AR of lexical ancestor
- Lexical ancestor need not be the caller
- Reference to ltp,16gt runs up access link chain to
p - Cost of access is proportional to lexical distance
Some setup cost on each call
27Establishing Addressability
- Using access links
- Access maintenance cost varies with level
- All accesses are relative to ARP (r0 )
- Assume
- Current lexical level is 2
- Access link is at ARP - 4
- Maintaining access link
- Calling level k1
- Use current ARP as link
- Calling level j lt k
- Find ARP for j 1
- Use that ARP as link
28Establishing Addressability
- Using a display
- Global array of pointer to nameable ARs
- Needed ARP is an array access away
- Reference to ltp,16gt looks up ps ARP in display
adds 16 - Cost of access is constant (ARP
offset)
Some setup cost on each call