140 likes | 286 Views
Lecture 23 Code Generation. CSCE 531 Compiler Construction. Topics Code Generation Readings: 9. April 12, 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
E N D
Lecture 23 Code Generation CSCE 531 Compiler Construction • Topics • Code Generation • Readings: 9 April 12, 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
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?
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 • int (*currentRoundToInt) ( float); • ... • ival = *currentRoundToInt(f+2.3); • Examples/Functions • pointersToFunctions.c • passingFunctions.c • returningFunctions.c
int chop(float x){ int i; i = (int) x; return i; } int chopEpsilon(float x){ int i; i = (int) (x + .0001); return i; } main(){ int (*currentRoundToInt) ( float); int ival; float x=1.99999999; currentRoundToInt = chopEpsilon; currentRoundToInt = chop; /* ival = *currentRoundToInt(x); * Syntax problem; Precedences */ ival = (*currentRoundToInt)(x); printf("x=%f rounded to %d\n", x, ival); } pointersToFunctions.c
int average(float lowx, float highx, int (*f) ( float) ){ int lowValue, highValue; lowValue = (*f)(lowx); highValue = (*f)(highx); return (lowValue + highValue)/2; } main(){ int (*current) ( float); int ival; float x=1.99999999; current= chopEpsilon; current= chop; ival = average(3.2, 5.7, current); printf("'average' value= %d\n", ival); } passingFunctions.c
/* int (*)(float) ???*/ void * chooseRound(){ static int (*lastChoice) ( float); if(lastChoice == chop) lastChoice = chopEpsilon; else lastChoice = chop; return lastChoice; } main(){ int (*current) ( float); int ival; float x=1.999; current= chooseRound(); ival = (*current)(x); printf("'Rounded one way' value= %d\n", ival); current= chooseRound(); ival = (*current)(x); printf("'Rounded the other way' value= %d\n", ival); } returningFunctions.c
Do people really use pointers to functions ? • 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 • Signal table – table of interrupt service routines • #include <signal.h> • typedef void (*sighandler_t)(int); • sighandler_t signal(int signum, sighandler_t handler);
What do we need in a symbol Table Entry? • Until now (limited types) • Base_type - enumerated type int or float • Indirection_level – • E.g., int **p; • id,placebase_type = int; • id,placelevel = 2; • 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
Questions on Project 5 • Functions returns type • 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 • Offsets for local variables • Output format for Quadruples – What do we print? • id.placename ‘(‘ 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 • We need to add push and pop
Code Generation • Chapter 9 • Issues in Code Generation • Input to code generator • Target programs • Memory management • Instruction selection • Register allocation
Target Machine Architecture • RISC vs CISC • Byte addressable, word addressable? • Byte order? Big Endian vs little • Address Modes supported by architecture • Absolute • Register • Indexed d(R) d + contents(R) • Indirect register *d(R) contents(d + contents(R)) • Cost of address modes in references to memory
Instruction Costs • Floating point costs • FPadd = 2 time units • FPmultiply = 5 time units • FPdivide=9 time units • Page 521