Download presentation
Presentation is loading. Please wait.
1
Lecture 23 Code Generation Topics Code Generation Readings: 9 April 12, 2006 CSCE 531 Compiler Construction
2
– 2 – CSCE 531 Spring 2006 Overview Last Time – Lec22 slides 1-14, 15-16 Finishing touches on Arrays in expressions Project 5 Today’s Lecture Questions on Project 5 – Functions Code Generation References: Chapter 9
3
– 3 – CSCE 531 Spring 2006 Fig 8.18 Annotated Parse Tree A[y,z]
4
– 4 – CSCE 531 Spring 2006 Project 5 - Functions Add Functions to core+ Non-nested functions FuncDefList inserted … Parameters passed By value for ugrads By value or ref for grads Symbol Tables Current symbol table; current offset; no global variables Allocate new table when parsing of function definition starts.Types int, float, level of indirection Functions?
5
– 5 – CSCE 531 Spring 2006 Functions as First Class Objects Functions in C – pointer to the start of the code You can pass pointers to functions as argument. Pointer to a function returning an int Pointer to a function returning an int int (*currentRoundToInt) ( float);... ival = *currentRoundToInt(f+2.3); Examples/Functions Examples/Functions pointersToFunctions.c passingFunctions.c returningFunctions.c
6
– 6 – CSCE 531 Spring 2006 pointersToFunctions.c int chop(float x){ int i; int i; i = (int) x; i = (int) x; return i; return i;}int chopEpsilon(float x){ int i; int i; i = (int) (x +.0001); i = (int) (x +.0001); return i; return i;}main(){ int (*currentRoundToInt) ( float); int (*currentRoundToInt) ( float); int ival; int ival; float x=1.99999999; float x=1.99999999; currentRoundToInt = chopEpsilon; currentRoundToInt = chopEpsilon; currentRoundToInt = chop; currentRoundToInt = chop; /* ival = *currentRoundToInt(x); /* ival = *currentRoundToInt(x); * Syntax problem; Precedences * Syntax problem; Precedences */ */ ival = (*currentRoundToInt)(x); ival = (*currentRoundToInt)(x); printf("x=%f rounded to %d\n", x, ival); printf("x=%f rounded to %d\n", x, ival);}
7
– 7 – CSCE 531 Spring 2006 passingFunctions.c int average(float lowx, float highx, int (*f) ( float) ){ int lowValue, highValue; int lowValue, highValue; lowValue = (*f)(lowx); lowValue = (*f)(lowx); highValue = (*f)(highx); highValue = (*f)(highx); return (lowValue + highValue)/2; return (lowValue + highValue)/2;}main(){ int (*current) ( float); int (*current) ( float); int ival; int ival; float x=1.99999999; float x=1.99999999; current= chopEpsilon; current= chopEpsilon; current= chop; current= chop; ival = average(3.2, 5.7, current); ival = average(3.2, 5.7, current); printf("'average' value= %d\n", ival); printf("'average' value= %d\n", ival);}
8
– 8 – CSCE 531 Spring 2006 returningFunctions.c /* int (*)(float) ???*/ void * chooseRound(){ static int (*lastChoice) ( float); static int (*lastChoice) ( float); if(lastChoice == chop) if(lastChoice == chop) lastChoice = chopEpsilon; lastChoice = chopEpsilon; else lastChoice = chop; else lastChoice = chop; return lastChoice; return lastChoice;}main(){ int (*current) ( float); int (*current) ( float); int ival; int ival; float x=1.999; float x=1.999; current= chooseRound(); current= chooseRound(); ival = (*current)(x); ival = (*current)(x); printf("'Rounded one way' value= %d\n", ival); printf("'Rounded one way' value= %d\n", ival); current= chooseRound(); current= chooseRound(); ival = (*current)(x); ival = (*current)(x); printf("'Rounded the other way' value= %d\n", ival); printf("'Rounded the other way' value= %d\n", ival);}
9
– 9 – CSCE 531 Spring 2006 Do people really use pointers to functions ? Yes!, Signal handlers is one of the places that this is commonly used Yes!, Signal handlers is one of the places that this is commonly used A signal is an interrupt to a Unix process sent by the Operating System A signal is an interrupt to a Unix process sent by the Operating System Signal table – table of interrupt service routines Signal table – table of interrupt service routines #include #include typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);
10
– 10 – CSCE 531 Spring 2006 What do we need in a symbol Table Entry? Until now (limited types) Until now (limited types) Base_type - enumerated type int or float Indirection_level – E.g., int **p; id,place base_type = int; id,place level = 2; So what do we add for functions So what do we add for functions Add code for function to enumerated to values for base_type Add two new components to struct nlist for the return type Basetype / level Note the above means no passing functions as parameters or returning functions as the return value Note the above means no passing functions as parameters or returning functions as the return value
11
– 11 – CSCE 531 Spring 2006 Questions on Project 5 Functions returns type Functions returns type Switching Tables Switching Tables currentTable = malloc(HASHTABLESIZE * sizeof (struct nlist *)) currentTable = hashtab; /* global (remove the static restriction */ Parameters looked up in the table for the current function, calculating offsets Parameters looked up in the table for the current function, calculating offsets Offsets for local variables Offsets for local variables Output format for Quadruples – What do we print? Output format for Quadruples – What do we print? id.place name ‘(‘ id.place offset ‘)’ E.g., add x(12) y(-8) z(-16) // add arg2 to a local to get another local variable OPCODES: call, ret, prologue, epilogue OPCODES: call, ret, prologue, epilogue We need to add push and pop
12
– 12 – CSCE 531 Spring 2006 Code Generation Chapter 9 Issues in Code Generation Input to code generator Input to code generator Target programs Target programs Memory management Memory management Instruction selection Instruction selection Register allocation Register allocation
13
– 13 – CSCE 531 Spring 2006 Target Machine Architecture RISC vs CISC Byte addressable, word addressable? Byte order? Big Endian vs little Address Modes supported by architecture Absolute Register Indexedd(R)d + contents(R) Indirect register *d(R)contents(d + contents(R)) Cost of address modes in references to memory Cost of address modes in references to memory
14
– 14 – CSCE 531 Spring 2006 Instruction Costs Floating point costs Floating point costs FPadd = 2 time units FPmultiply = 5 time units FPdivide=9 time units Page 521 Page 521
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.