Code Generation Example

Slides:



Advertisements
Similar presentations
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Advertisements

Symbol Table.
Intermediate Code Generation
The University of Adelaide, School of Computer Science
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms The C Programming Language.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
SPIM and MIPS programming
CSE 303 Lecture 16 Multi-file (larger) programs
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
MIPS Assembly Language Programming
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Pointers *, &, array similarities, functions, sizeof.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 1.2 Introduction to C++ Programming
Week 3-4 Control flow (review) Function definition Program Structures
Chapter 1.2 Introduction to C++ Programming
Chapter 7: User-Defined Functions II
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
A bit of C programming Lecture 3 Uli Raich.
LESSON 3 IO, Variables and Operators
Programming Languages and Paradigms
Programming Paradigms
C Basics.
CMPE 152: Compiler Design December 5 Class Meeting
ARM Assembly Programming
Pointers, Dynamic Data, and Reference Types
Chapter 6 Intermediate-Code Generation
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Govt. Polytechnic,Dhangar
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSCE 3110 Data Structures & Algorithm Analysis
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Pointers, Dynamic Data, and Reference Types
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Code Generation Example 薛智文 cwhsueh@csie.ntu.edu.tw http://www.csie.ntu.edu.tw/~cwhsueh/ 96 Spring

Warnings This set of slides contain a “pseudo code” for a very simple compiler. This is an example, not the standard solution. Assume only integers. Other solutions exist. This example does not pass LEX, YACC or GCC. Usage of this example is entirely to illustrate the high level ideas. Not responsible for any syntax errors. Please read LEX, YACC and GCC manuals carefully to avoid programming mistakes and conflicts. web sites example: http://dinosaur.compilertools.net/ libraries · · · 19:15 /12

Error Handling char *error_message[MAXMSG]; /* tests for error messages */ void error_msg(int line_no; char * file_name; int index; char *msg1; char *msg2) { switch index { /* some messages needed to be specially treated */ case 3: /* insert *msg1 into error message */ /* variable ‘‘name’’ undefined */ printf("line %d in file %s, error %d, %s\n", line_no,file_name,index,error_message[index]); ... default: if(index >= MAXMSG){ printf("internal error, wrong error index %d\n",index); } else { } exit(1); 19:15 /12

Global Constants and Variables /* tags for where variables are stored */ #define GLOBAL_VAR 1 #define LOCAL_VAR 2 #define TEMP_VAR 3 #define REG_VAR 4 #define PARA_VAR 5 #define NON_LOCAL_VAR 6 #define CONSTANT_VAR 7 ... int global_start; /* the starting address to put global variables */ int local_start; /* the starting address to put local variables */ int temp_start; /* the starting address to put temp variables */ 19:15 /12

Type Definitions typedef struct place_typ { char tag; /* where this var is stored */ int offset; int depth_diff; /* difference in nesting depth */ } PLACE_TYPE; typedef struct var_typ { char type_tag; /* procedure, variable, int constant ... */ PLACE_TYPE place; char *name; /* for a procedure, store the following */ int temp_start,local_start,param_start,...; ... } VAR_TYPE; 19:15 /12

Emit (1/2) /* op: operator, opni: operand # i */ void emitASSIGN(char operator; PLACE_TYPE opn1,opn2,opn3) { switch operator { case ’+’: gen_r_address(opn2,3); /* load into register I__3 */ gen_r_address(opn3,4); /* load into register I__4 */ fprintf(code_file,"I__3 = I__3+ I__4;\n"); gen_l_address(opn1,3); break; case ’-’: ... case ’*’: ... case ’/’: ... default: printf("internal error in code generation, operator %c is undefined\n",operator); exit(1); } 19:15 /12

Emit (2/2) void emitCOND_JUMP(...) { ... } void emitIO(...) void emitCALL(...) 19:15 /12

Generate r-address /* load the value at ‘‘where’’ into register # result_r */ void gen_r_address(PLACE_TYPE where; int result_r) { switch where.tag { case GLOBAL_VAR: fprintf(code_file,"I__%1d = AVAL__S(%d);\n", result_r,global_start+where.offset); break; case LOCAL_VAR: /* make sure I__9 is not used for other purposes */ fprintf(code_file,"I__9=I__%1d+%d\n",FP,local_start+where.offset); fprintf(code_file,"I__%1d = AVAL__S(I__9);\n",result_r,); case CONSTANT_VAR: fprintf(code_file,"I__%1d = %d;\n",result_r,where.offset); break; ... } 19:15 /12

Generate l-address /* store the value at register # result_r into the place ‘‘where’’ */ void gen_l_address(PLACE_TYPE where; int result_r) { switch where.tag { case GLOBAL_VAR: fprintf(code_file,"ASSET__S(%d,I__%1d);\n", global_start+where.offset,result_r); break; case LOCAL_VAR: /* make sure I__9 is not used for other purposes */ fprintf(code_file,"I__9=I__%1d+%d\n",FP,local_start+where.offset); fprintf(code_file,"ASSET__S(I__9,I__%1d);\n",result_r); case TEMP_VAR: ... default: /* internal error */ } 19:15 /12

TEMP Space Management (1/2) #define MAXTEMP 1000 char temp_map[MAXTEMP]; static int max_temp_used; /* max number of temps used */ void freeALLtemp(void) { int i; max_temp_used = 0; for (i=0;i<MAXTEMP;i++) temp_map[i] = 0; } void freetemp(PLACE_TYPE vplace) { if (vplace.tag == TEMP_VAR) temp_map[vplace.offset] = 0; 19:15 /12

TEMP Space Management (2/2) /* very simple first fit allocation algorithm */ int newtemp(void) { int i=0; while (i < MAXTEMP && temp_map[i] > 0) i++; if (i >= MAXTEMP) { /* internal error, allocate more temps */ ... } else { /* record the max number of temp space used in a procedure */ if (i > max_temp_used) max_temp_used = i; return(i); } Use the same bit map technique to record the set of registers used/unused. Check for free register first, then temp space. 19:15 /12

Label Management static int current_label; /* current label number */ void initLABELS(void) { current_label = 0; } int newLABEL(void) { return(current_label++); void emitLABEL(int label) { fprintf(code_file,"LAB%d:\n",label); void emitJUMP(int label) { fprintf(code_file,"goto LAB%d;\n",label); 19:15 /12

YACC Rules %union{ int ival; double fval; VAR_TYPE vval; } /* define the return type of ‘‘expr’’ to be VAR_TYPE */ %type <vval> expr ... expr : expr ’+’ expr { $$.place.offset = newtemp(); $$.place.tag = TEMP_VAR; emitASSIGN(’+’,$$.place,$1.place,$2.place); freetemp($1.place); freetemp($2.place); | expr ’-’ expr {...} | id {...} 19:15 /12