Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Lecture 19 Nested Scopes 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 4 due March 28 Tuesday Activation Records Again Params.c 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 7 Homework:

3 – 3 – CSCE 531 Spring 2006 Pop Quiz

4 – 4 – CSCE 531 Spring 2006 Question on size of code arrays What happens when you run out of space? Nextquad > arraySize void 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, arraySize); opcode = (int *) realloc(opcode, arraySize); } opcode[nextquad++] = op; opcode[nextquad++] = op;}

5 – 5 – 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 placeList(reverse order)

6 – 6 – CSCE 531 Spring 2006 Semantic Actions for Function Invocations 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);}

7 – 7 – CSCE 531 Spring 2006 Attribute for arglist Attribute – linked list of symbol table pointers struct placeNode { struct nlist *place; struct placeNode *next; }

8 – 8 – CSCE 531 Spring 2006 Semantic Actions for ArgLists arglist  arglist ‘,’ E{ $$ = addArg($1, $3); $$ = addArg($1, $3);} |E{ $$ = addArg(NULL, $1); $$ = addArg(NULL, $1);} |{ $$ = NULL; } ;

9 – 9 – CSCE 531 Spring 2006 AddArg - function

10 – 10 – CSCE 531 Spring 2006 Function Definitions funcDef  type ID (parmlist) ‘{‘ Decls L ‘}’ Attributes 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.place ID.place Parmlist ??? Parmlist ??? X Decls ??? Decls ???

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

12 – 12 – CSCE 531 Spring 2006 Decls revisited – calculating offsets Decls  Decls Dec Attributes? Offset – Offset – size of space needed offset from base pointer Decls  Decls Dec { $$ = $1 + $2; } | Dec { $$ = $1; };

13 – 13 – CSCE 531 Spring 2006 Calculating Offsets Decls  DECLARE intlist{ $$ = $2;} | DECLAREfloatlist{ $$ = $2;} ; intlist  INT ID{ |intlist ‘,’ ID

14 – 14 – CSCE 531 Spring 2006 Nested.c int 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 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 – 15 – CSCE 531 Spring 2006 Nested.s nest: pushl %ebp pushl %ebp movl %esp, %ebp movl %esp, %ebp subl $36, %esp subl $36, %esp movl $1, -4(%ebp) movl $1, -4(%ebp) movl -4(%ebp), %eax movl -4(%ebp), %eax addl $3, %eax addl $3, %eax movl %eax, -8(%ebp) movl %eax, -8(%ebp) movl -8(%ebp), %eax movl -8(%ebp), %eax addl -4(%ebp), %eax addl -4(%ebp), %eax movl %eax, -12(%ebp) movl %eax, -12(%ebp) movl -4(%ebp), %eax movl -4(%ebp), %eax cmpl -8(%ebp), %eax cmpl -8(%ebp), %eax jge.L3 jge.L3 movl -8(%ebp), %eax movl -8(%ebp), %eax addl -4(%ebp), %eax addl -4(%ebp), %eax movl %eax, -16(%ebp) movl %eax, -16(%ebp) Activation Record RetAddr ebp esp

16 – 16 – CSCE 531 Spring 2006 movl -12(%ebp), %eax movl -12(%ebp), %eax movl %eax, -20(%ebp) movl %eax, -20(%ebp) movl -8(%ebp), %eax movl -8(%ebp), %eax cmpl -12(%ebp), %eax cmpl -12(%ebp), %eax jge.L3 jge.L3 movl -12(%ebp), %eax movl -12(%ebp), %eax addl -16(%ebp), %eax addl -16(%ebp), %eax movl %eax, -28(%ebp) movl %eax, -28(%ebp) movl -8(%ebp), %eax movl -8(%ebp), %eax addl -20(%ebp), %eax addl -20(%ebp), %eax movl %eax, -32(%ebp) movl %eax, -32(%ebp) movl -32(%ebp), %eax movl -32(%ebp), %eax addl -28(%ebp), %eax addl -28(%ebp), %eax movl %eax, -4(%ebp) movl %eax, -4(%ebp).L3: movl -8(%ebp), %eax movl -8(%ebp), %eax addl -4(%ebp), %eax addl -4(%ebp), %eax leave leave ret ret

17 – 17 – CSCE 531 Spring 2006 C picture again a simplification Each block can have declarations Each block can have declarations But space is saved for the entire function But space is saved for the entire function And there are no nested functions And there are no nested functions

18 – 18 – CSCE 531 Spring 2006 Fig 7.22 – Pascal with Nested Procedures

19 – 19 – CSCE 531 Spring 2006 Fig 7.23 – Access Links in Act. Records

20 – 20 – CSCE 531 Spring 2006 Parameter Passing Call by value Call by value Call by reference Call by reference Copy-restore Copy-restore Call by name Call 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 – 21 – CSCE 531 Spring 2006


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

Similar presentations


Ads by Google