lecture 19 nested scopes topics procedural abstraction activation records readings: 7.4 march 20,...

21
Lecture 19 Nested Scopes Topics Topics Procedural Abstraction Activation records Readings: 7.4 Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

Upload: susanna-christina-haynes

Post on 15-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

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

Page 2: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 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:

Page 3: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 3 – CSCE 531 Spring 2006

Pop QuizPop Quiz

Page 4: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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;

}}

Page 5: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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)

Page 6: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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);

}}

Page 7: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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;

}}

Page 8: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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; }

;;

Page 9: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 9 – CSCE 531 Spring 2006

AddArg - functionAddArg - function

Page 10: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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 ???

Page 11: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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

Page 12: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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;

}}

;;

Page 13: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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

Page 14: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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;

… …

}}

}}

}}

Page 15: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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

Page 16: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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

Page 17: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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

Page 18: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 18 – CSCE 531 Spring 2006

Fig 7.22 – Pascal with Nested ProceduresFig 7.22 – Pascal with Nested Procedures

Page 19: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 19 – CSCE 531 Spring 2006

Fig 7.23 – Access Links in Act. RecordsFig 7.23 – Access Links in Act. Records

Page 20: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 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

Page 21: Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction

– 21 – CSCE 531 Spring 2006