Upload
susanna-christina-haynes
View
216
Download
0
Embed Size (px)
Citation preview
Lecture 19 Nested Scopes
Lecture 19 Nested Scopes
Topics Topics Procedural Abstraction Activation records
Readings: 7.4Readings: 7.4
March 20, 2006
CSCE 531 Compiler Construction
– 2 – CSCE 531 Spring 2006
OverviewOverviewLast TimeLast Time
Project 4 due March 28 Tuesday Activation Records Again Params.c
Today’s Lecture Today’s Lecture Question on size of code arrays Review Semantics actions for function invocation Review Semantics actions for arglist
Attribute placeList – list of identifiers Handling Function Definitions Passing Parameters
Array.c Nested Scope
Nested.c
References: Chapter 7References: Chapter 7
Homework:Homework:
– 3 – CSCE 531 Spring 2006
Pop QuizPop Quiz
– 4 – CSCE 531 Spring 2006
Question on size of code arraysQuestion on size of code arrays
What happens What happens when you run when you run out of space?out of space?
Nextquad > arraySizeNextquad > arraySize
voidvoid
gen(int op) {gen(int op) {
static int *opcode=NULL;static int *opcode=NULL;
static int arraySize = 0; static int arraySize = 0;
static int nextquad = 0;static int nextquad = 0;
if (nextquad >= arraySize){if (nextquad >= arraySize){
arraySize += ARRAY_INCREMENT;arraySize += ARRAY_INCREMENT;
opcode = (int *) realloc(opcode, opcode = (int *) realloc(opcode, arraySize);arraySize);
}}
opcode[nextquad++] = op; opcode[nextquad++] = op;
}}
– 5 – CSCE 531 Spring 2006
Grammar for C FunctionsGrammar for C Functions
Function definitionFunction definition
funcDef funcDef type ID (parmlist) ‘{‘ Decls L ‘}’ type ID (parmlist) ‘{‘ Decls L ‘}’
Function invocation (calls)Function invocation (calls)
expr expr ID ‘(’ arglist ‘)’ ID ‘(’ arglist ‘)’
Attributes:Attributes:
parmlist – list of id.placesparmlist – list of id.places
arglist - list of Id.places placeListarglist - list of Id.places placeList (reverse order)(reverse order)
– 6 – CSCE 531 Spring 2006
Semantic Actions for Function InvocationsSemantic Actions for Function Invocations
expr expr ID ‘(’ arglist ‘)’ {p = arglist.list; ID ‘(’ arglist ‘)’ {p = arglist.list;
while(p != NULL){while(p != NULL){
gen(push, -, -, gen(push, -, -, ppplace);place);
p = pp = plink;link;
} }
gen (call, -, -, ID.place);gen (call, -, -, ID.place);
}}
– 7 – CSCE 531 Spring 2006
Attribute for arglistAttribute for arglist
Attribute – linked list of symbol table pointersAttribute – linked list of symbol table pointers
struct placeNode {struct placeNode {
struct nlist *place;struct nlist *place;
struct placeNode *next;struct placeNode *next;
}}
– 8 – CSCE 531 Spring 2006
Semantic Actions for ArgListsSemantic Actions for ArgLists
arglist arglist arglist ‘,’ E arglist ‘,’ E {{
$$ = addArg($1, $3);$$ = addArg($1, $3);
}}
|| EE {{
$$ = addArg(NULL, $1);$$ = addArg(NULL, $1);
}}
|| { $$ = NULL; }{ $$ = NULL; }
;;
– 9 – CSCE 531 Spring 2006
AddArg - functionAddArg - function
– 10 – CSCE 531 Spring 2006
Function DefinitionsFunction Definitions
funcDef funcDef type ID (parmlist) ‘{‘ Decls L ‘}’ type ID (parmlist) ‘{‘ Decls L ‘}’
AttributesAttributes
Type.tval - int or float / level for now Type.tval - int or float / level for now Float/2 - a pointer to a pointer to a float later we will expand
ID.placeID.place
Parmlist ???Parmlist ??? X
Decls ???Decls ???
– 11 – CSCE 531 Spring 2006
Semantic Actions for non-nested scopesSemantic Actions for non-nested scopes
funcDef funcDef type ID (parmlist) ‘{‘ Decls L ‘}’ type ID (parmlist) ‘{‘ Decls L ‘}’
Emit PrologueEmit Prologue
Emit BodyEmit Body
Emit EpilogueEmit Epilogue
Markers – P – generate PrologueQ – generate Epilogue
– 12 – CSCE 531 Spring 2006
Decls revisited – calculating offsetsDecls revisited – calculating offsets
Decls Decls Decls Dec Decls Dec
Attributes?Attributes?
Offset – Offset – size of space needed offset from base pointer
Decls Decls Decls Dec { Decls Dec {
$$ = $1 + $$ = $1 + $2;$2;
}}
| Dec {| Dec {
$$ = $1;$$ = $1;
}}
;;
– 13 – CSCE 531 Spring 2006
Calculating OffsetsCalculating Offsets
Decls Decls DECLARE intlistDECLARE intlist { $$ = $2;}{ $$ = $2;}
| | DECLAREDECLARE floatlistfloatlist { $$ = $2;}{ $$ = $2;}
;;
intlistintlist INT IDINT ID { {
|| intlist ‘,’ IDintlist ‘,’ ID
– 14 – CSCE 531 Spring 2006
Nested.cNested.c int int
nest(int x, int y, int z){nest(int x, int y, int z){
int a, b, c;int a, b, c;
a = 1;a = 1;
b = a+3;b = a+3;
c = a+b;c = a+b;
if ( a < b) { /* Block */if ( a < b) { /* Block */
int e, f, g;int e, f, g;
e = a+b;e = a+b;
f = c;f = c;
if ( b < c) { /* Block2 */if ( b < c) { /* Block2 */
int h, i, j;int h, i, j;
h = e + c;h = e + c;
i = f + b;i = f + b;
a = h + i;a = h + i;
}}
}}
return a + b;return a + b;
}}
int int
nest(int x, int y, int z){nest(int x, int y, int z){
int a, b, c;int a, b, c;
… …
if ( a < b) { /* Block */if ( a < b) { /* Block */
int e, f, g;int e, f, g;
… …
if ( b < c) { /* Block2 */if ( b < c) { /* Block2 */
int h, i, j;int h, i, j;
… …
}}
}}
}}
– 15 – CSCE 531 Spring 2006
Nested.sNested.snest:nest:
pushl %ebppushl %ebp
movl %esp, %ebpmovl %esp, %ebp
subl $36, %espsubl $36, %esp
movl $1, -4(%ebp)movl $1, -4(%ebp)
movl -4(%ebp), %eaxmovl -4(%ebp), %eax
addl $3, %eaxaddl $3, %eax
movl %eax, -8(%ebp)movl %eax, -8(%ebp)
movl -8(%ebp), %eaxmovl -8(%ebp), %eax
addl -4(%ebp), %eaxaddl -4(%ebp), %eax
movl %eax, -12(%ebp)movl %eax, -12(%ebp)
movl -4(%ebp), %eaxmovl -4(%ebp), %eax
cmpl -8(%ebp), %eaxcmpl -8(%ebp), %eax
jge .L3jge .L3
movl -8(%ebp), %eaxmovl -8(%ebp), %eax
addl -4(%ebp), %eaxaddl -4(%ebp), %eax
movl %eax, -16(%ebp)movl %eax, -16(%ebp)
Activation RecordActivation Record
RetAddrRetAddr
ebp
esp
– 16 – CSCE 531 Spring 2006
movl -12(%ebp), %eaxmovl -12(%ebp), %eax
movl %eax, -20(%ebp)movl %eax, -20(%ebp)
movl -8(%ebp), %eaxmovl -8(%ebp), %eax
cmpl -12(%ebp), %eaxcmpl -12(%ebp), %eax
jge .L3jge .L3
movl -12(%ebp), %eaxmovl -12(%ebp), %eax
addl -16(%ebp), %eaxaddl -16(%ebp), %eax
movl %eax, -28(%ebp)movl %eax, -28(%ebp)
movl -8(%ebp), %eaxmovl -8(%ebp), %eax
addl -20(%ebp), %eaxaddl -20(%ebp), %eax
movl %eax, -32(%ebp)movl %eax, -32(%ebp)
movl -32(%ebp), %eaxmovl -32(%ebp), %eax
addl -28(%ebp), %eaxaddl -28(%ebp), %eax
movl %eax, -4(%ebp)movl %eax, -4(%ebp)
.L3:.L3:
movl -8(%ebp), %eaxmovl -8(%ebp), %eax
addl -4(%ebp), %eaxaddl -4(%ebp), %eax
leaveleave
retret
– 17 – CSCE 531 Spring 2006
C picture again a simplificationC picture again a simplification
Each block can have declarationsEach block can have declarations
But space is saved for the entire functionBut space is saved for the entire function
And there are no nested functions And there are no nested functions
– 18 – CSCE 531 Spring 2006
Fig 7.22 – Pascal with Nested ProceduresFig 7.22 – Pascal with Nested Procedures
– 19 – CSCE 531 Spring 2006
Fig 7.23 – Access Links in Act. RecordsFig 7.23 – Access Links in Act. Records
– 20 – CSCE 531 Spring 2006
Parameter PassingParameter Passing
Call by valueCall by value
Call by referenceCall by reference
Copy-restoreCopy-restore
Call by nameCall by name Logically the procedure is treated as a macro with the
arguments substituted for the formal parameters Actually code passed for evaluating the argument is passed
to the functions
– 21 – CSCE 531 Spring 2006