531 Summer04 Lec8 Bottom Up Parsing

CSCE 531 Compiler Construction
Lecture 18
Procedures
Topics


Procedural Abstraction
Activation records
Readings: 7.4
March 20, 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:
–2–
CSCE 531 Spring 2006
Seeing the Activation Record Examples
In Examples/ActRec
In Examples/ActRec
 Param.c – to illustrate
multiple arguments
 Param.c –
 Comb.c
 Comb.c
 Fib.c
 Fib.c
–3–
CSCE 531 Spring 2006
Main: …
Notes:
Param.[cs]
pushl $3
int func(int, int, int);
pushl $1
Arguments pushed
in reverse
2.
Return value ret
pushl $2
call
func
addl
$16, %esp
int i, j, k;
movl
%eax, -12(%ebp)
k = func(1, 2.0, 3);
…
main(){
1.
func:
}
pushl %ebp
int
movl
%esp, %ebp
func (int x, int y, int z){
subl
$4, %esp
int sum;
movl
12(%ebp), %eax
sum = x+y+ z;
addl
8(%ebp), %eax
return sum;
addl
16(%ebp), %eax
movl
%eax, -4(%ebp)
movl
-4(%ebp), %eax
}
leave
ret
–4–
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 ')'
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
–5–
CSCE 531 Spring 2006
Proposed solution
E  sum '(' ID '=' E ';' E '; ' E ')'
Insert Markers
–6–
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 . 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
–7–
go to state 35
CSCE 531 Spring 2006
Placing Run-time Data Structures
Classic Organization
C
o
d
e
S G
t l
a&o
t b
i a
c l
H
e
a
p
• Better utilization if
stack & heap grow
toward each other
S
t
a
c
k
• Very old result (Knuth)
• Code & data separate or
interleaved
high
0
Single Logical Address Space
• 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
–8–
Slide from “Engineering a Compiler”
Cooper and Torczon
CSCE 531 Spring 2006
How Does Virtual Memory Work?
The Big Picture
virtual address
spaces
Compiler’s view
C
o
d
e
S G
t l
a&o
t b
i a
c l
H
e
a
p
OS’s view
S
t
a
c
k
C
o
d
e
S G
t l
a&o
t b
i a
c l
H
e
a
p
S
t
a
c
k
C
o
d
e
S G
t l
a&o
t b
i a
c l
H
e
a
p
S
t
a
c
k
S G
t l
a&o
t b
i a
c l
H
e
a
p
S
t
a
c
k
...
0
high
Hardware’s view
–9–
...
C
o
d
e
Slide from Engr. A Compiler by Cooper and Torzon
Physical address
space_
CSCE 531 Spring 2006
Example: params2.c
#include <stdio.h>
main(){
int i, j, k;
i = 7; j = 2; k = 3;
printf(“f(%d)=%d\n", i, f(i+j, j, k+6));
}
int f(int n, int m, int p){
int t1, t2;
t1 = n+m;
t2 = m*p;
return(2*t1+t2);
}
– 10 –
CSCE 531 Spring 2006
Gcc –S –O params.c: The Main
… header …
main:
…
pushl $10
// Note the optimizer at work!
pushl $2
pushl $9
call
– 11 –
// Which arg is this?
f
CSCE 531 Spring 2006
Gcc –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
popl
ret
– 12 –
(%edx,%eax,2), %eax
%ebp
// Epilogue
// return value in register %eax
CSCE 531 Spring 2006
Comments on IA32 Instructions
pushl %ebp
// esp=esp-4; M[esp]=ebp
movl
%esp, %ebp
//
movl
12(%ebp), %edx
// %edx  M[ Reg[%ebp]+12]
movl
8(%ebp), %eax
addl
%edx, %eax
imull 16(%ebp), %edx
leal
popl
(%edx,%eax,2), %eax
%ebp
//
ret
– 13 –
CSCE 531 Spring 2006
Stack Support for C in IA32
%esp – stack pointer
%ebp - “frame” pointer
Note the stack grows down from high memory
i = 7; j = 2; k = 3;
%ebp
0xFFFFF0F0
f(i+j, j, k+6));
…
int f(int n, int m, int p){
int t1, t2;
– 14 –
CSCE 531 Spring 2006
Revisiting 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
popl
(%edx,%eax,2), %eax
%ebp
ret
– 15 –
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
– 16 –
(reverse order)
CSCE 531 Spring 2006
Semantic 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);
}
– 17 –
CSCE 531 Spring 2006
Semantic Actions for non-nested scopes
funcDef  type ID (parmlist) ‘{‘ Decls L ‘}’
Emit Prologue
Emit Body
Emit Epilogue
– 18 –
CSCE 531 Spring 2006
Translating Local Names
How does the compiler represent a specific instance of x ?
Name is translated into a static coordinate

< level,offset > 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



– 19 –
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
CSCE 531 Spring 2006
Storage 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
…
}
…
}
…
}
– 20 –
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
CSCE 531 Spring 2006
Variable-length Data
B0: {
int a, b
… assign value
to a
B1:
{
int v(a), b, x
B2:
{
int x, y(8)
….
}
a
– 21 –
b
v
b
x
x
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
y(8)
Includes variable length data for
all blocks in the procedure …
v(a)
Variable-length data
CSCE 531 Spring 2006
Activation Record Review
parameters
register
save area
return value
return address
ARP
addressability
caller’s ARP
local
variables
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
– 22 –
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
– 23 –
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 fee(x,x,x);
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 …
– 24 –
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



– 25 –
{
Convert to static coordinates
Find appropriate ARP
Use that ARP + offset
Must find the right AR
Need links to nameable ARs
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
parameters
register
save area
return value
return address
A
RP
access link
caller’s ARP
local
variables
parameters
register
save area
return value
return address
access link
caller’s ARP
local
variables
parameters
register
save area
Some setup cost
on each call
return value
return address
access link
caller’s ARP
local
variables
Reference to <p,16> runs up access link chain to p
Cost of access is proportional to lexical distance
– 26 –
CSCE 531 Spring 2006
Establishing Addressability
Using access links
SC
<2,8>
Generated Code
loadAI r0, 8  r2
<1,12> loadAI r0, -4  r1
loadAI r1, 12  r2
<0,16> loadAI r0, -4  r1
loadAI r1, -4  r1
loadAI r1, 16  r2
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
Access & maintenance cost varies with level
All accesses are relative to ARP
– 27 –
(r0 )
CSCE 531 Spring 2006
Establishing Addressability
Using a display
Global array of pointer to nameable ARs
Needed ARP is an array access away
Some setup cost
on each call
Display
level 0
level 1
level 2
level 3
ARP
parameters
parameters
parameters
register
save area
return value
register
save area
register
save area
return value
return address
return value
return address
saved ptr.
return address
saved ptr.
caller’s ARP
saved ptr.
caller’s ARP
caller’s ARP
local
variables
local
variables
local
variables
Reference to <p,16> looks up p’s ARP in display & adds
16
CSCE 531 Spring 2006
of access is constant
(ARP + offset)
– Cost
28 –