1 / 14

Lecture 23 Code Generation

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

payton
Télécharger la présentation

Lecture 23 Code Generation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 23 Code Generation CSCE 531 Compiler Construction • Topics • Code Generation • Readings: 9 April 12, 2006

  2. 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. Fig 8.18 Annotated Parse Tree A[y,z]

  4. 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. 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

  6. 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

  7. 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

  8. /* 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

  9. 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);

  10. 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,placebase_type = int; • id,placelevel = 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

  11. 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.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 • We need to add push and pop

  12. Code Generation • Chapter 9 • Issues in Code Generation • Input to code generator • Target programs • Memory management • Instruction selection • Register allocation

  13. 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

  14. Instruction Costs • Floating point costs • FPadd = 2 time units • FPmultiply = 5 time units • FPdivide=9 time units • Page 521

More Related