10. Intermediate Code Generation

Slides:



Advertisements
Similar presentations
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Advertisements

Intermediate Code Generation
The University of Adelaide, School of Computer Science
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Program Representations. Representing programs Goals.
Compilers: Parse Tree/9 1 Compiler Structures Objective – –extend the expressions language compiler to generate a parse tree for the input program,
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
CS 536 Spring Code generation I Lecture 20.
Lecture 8: Intermediate Code CS 540 Spring CS 540 GMU Spring Compiler Architecture Scanner (lexical analysis) Parser (syntax analysis) Code.
COP4020 Programming Languages
Compilers: Attr. Grammars/8 1 Compiler Structures Objective – –describe semantic analysis with attribute grammars, as applied in yacc and recursive.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Compilers: IC/10 1 Compiler Structures Objective – –describe intermediate code generation – –explain a stack-based intermediate code for the expression.
Chapter 8: Intermediate Code Generation
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Code Generation and Intermediate Representations
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Intermediate Code Representations
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Lecture 12 Intermediate Code Generation Translating Expressions
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS 404 Introduction to Compiler Design
Component 1.6.
Compiler Design (40-414) Main Text Book:
Component 1.6.
Computer Architecture Instruction Set Architecture
Intermediate code Jakub Yaghob
A Simple Syntax-Directed Translator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler design Introduction to code generation
An Attribute Grammar for Tiny
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Stacks Chapter 4.
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
An Overview to Compiler Design
Intermediate Code Generation Part I
CS 3304 Comparative Languages
Course supervisor: Lubna Siddiqui
Intermediate Representations Hal Perkins Autumn 2011
Intermediate Representations
Chapter 6 Intermediate-Code Generation
CSE401 Introduction to Compiler Construction
Lecture 30 (based on slides by R. Bodik)
Intermediate Code Generation Part I
The University of Adelaide, School of Computer Science
Intermediate Representations
Designing a Predictive Parser
Compiler design.
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Compiler Structures 8. Attribute Grammars Objectives
CS416 Compiler Design lec00-outline February 23, 2019
Compiler Structures 3. Lex Objectives , Semester 2,
Compiler Structures 4. Syntax Analysis Objectives
Compiler Structures 2. Lexical Analysis Objectives
Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
9. Creating and Evaluating a
Intermediate Code Generation Part I
Compiler Design 21. Intermediate Code Generation
Compiler Structures 1. Overview Objective
Compiler Structures 11. IC Generation: Control Structures Objectives
Presentation transcript:

10. Intermediate Code Generation Compiler Structures 242-437, Semester 2, 2018-2019 10. Intermediate Code Generation Objectives describe intermediate code generation explain a stack-based intermediate code for the expression language

Overview 1. Intermediate Code (IC) Generation 2. IC Examples 3. Expression Translation in SPIM 4. The Expressions Language

In this lecture Front End Back End Source Program Lexical Analyzer Syntax Analyzer Semantic Analyzer In this lecture Int. Code Generator Intermediate Code Code Optimizer Back End As I said earlier, there will be 5 homeworks, each of which will contribute to 5% of your final grade. You will have at least 2 weeks to complete each of the homeworks. Talking about algorithms really helps you learn about them, so I encourage you all to work in small groups. If you don’t have anyone to work with please either e-mail me or stop by my office and I will be sure to match you up with others. PLEASE make sure you all work on each problem; you will only be hurting yourself if you leach off of your partners. Problems are HARD! I will take into account the size of your group when grading your homework. Later in the course I will even have a contest for best algorithm and give prizes out for those who are most clever in their construct. I will allow you one late homework. You *must* write on the top that you are taking your late. Homework 1 comes out next class. Target Code Generator Target Lang. Prog.

1. Intermediate Code (IC) Generation Helps with retargeting e.g. can easily attach a back end for a new machine to an existing front end Enables machine-independent code optimization. Target machine code Front end Back end Intermediate code

Graphical IC Representations Abstract Syntax Trees (AST) retains basic parse tree structure, but with unneeded nodes removed Directed Acyclic Graphs (DAG) compacted AST to avoid duplication smaller memory needs Control Flow Graphs (CFG) used to model control flow

Linear (text-based) ICs Stack-based (postfix) e.g. the JVM Three-address code x := y op z Two-address code: x := op y (the same as x := x op y)

2. IC Examples ASTs and DAGs Stack-based (postfix) Three-address Code SPIM

2.1. ASTs and DAGs a := b *-c + b * -c assign assign a + a + * * * AST Pros: easy restructuring of code and/or expressions for intermediate code optimization Cons: memory intensive

2.2. Stack-based (postfix) a := b * -c + b * -c b c uminus * b c uminus * + a assign (e.g. JVM stack instrs) iload 2 // push b iload 3 // push c ineg // uminus imul // * iload 2 // push b iload 3 // push c ineg // uminus imul // * iadd // + istore 1 // store a Postfix notation represents operations on a stack Pro: easy to generate Cons: stack operations are more difficult to optimize

Translated from the AST 2.3. Three-Address Code a := b * -c + b * -c t1 := - c t2 := b * t1 t3 := - c t4 := b * t3 t5 := t2 + t4 a := t5 t1 := - c t2 := b * t1 t5 := t2 + t2 a := t5 Translated from the AST Translated from the DAG

2.4. SPIM Three address code for a simulator that runs MIPS32 assembly language programs http://www.cs.wisc.edu/~larus/spim.html Loading/Storing lw register,var - loads value into register sw register,var - stores value from register many, many others continued

Binary math ops (reg1 = reg2 op reg3): 8 registers: $t0 - $t7 Binary math ops (reg1 = reg2 op reg3): add reg1,reg2,reg3 sub reg1,reg2,reg3 mul reg1,reg2,reg3 div reg1,reg2,reg3 Unary minus (reg1 = - reg2) neg reg1, reg2

"a := b * -c + b * -c" in SPIM assign a + * * b - b - c c lw $t0,c neg $t1,$t0 lw $t0,b mul $t2, $t1,$t0 mul $t1, $t1,$t0 add $t1,$t2,$t1 sw $t1,a assign a + t1 AST * * t1 t2 b - b - t0 t1 t0 t1 c c t0 t0

a := b * -c + b * -c assign a + * - b c lw $t0,c neg $t1,$t0 lw $t0,b mul $t1, $t1,$t0 add $t2,$t1,$t1 sw $t2,a a + t2 DAG * t1 - b t1 t0 c t0

3. Expression Translation in SPIM Generate: lw $t1,b Grammar: S => id := E E => E + E E => id S E E E E E As we parse, use attributes to pass information about the temporary variables up the tree. E 1 E a := b + c + d + e parse tree --> code using bottom-up evaluation

Each number corresponds to a temporary variable. Generate: lw $t1,b lw $t2,c S E E E E E E 1 E 2 a := b + c + d + e Each number corresponds to a temporary variable.

Each number corresponds to a temporary variable. Generate: lw $t1,b lw $t2,c add $t3,$t1,$t2 S E E E 3 E E E 1 E 2 a := b + c + d + e Each number corresponds to a temporary variable.

S E E E 3 E 4 E E 1 E 2 a := b + c + d + e Generate: lw $t1,b lw $t2,c add $t3,$t1,$t2 lw $t4,d S E E E 3 E 4 E E 1 E 2 a := b + c + d + e

S E 5 E E 3 E 4 E E 1 E 2 a := b + c + d + e Generate: lw t1,b lw t2,c add $t3,$t1,$t2 lw t4,d add $t5,$t3,$t4 S E 5 E E 3 E 4 E E 1 E 2 a := b + c + d + e

S E 5 6 E E 3 E 4 E E 1 E 2 a := b + c + d + e Generate: lw $t1,b lw $t2,c add $t3,$t1,$t2 lw $t4,d add $t5,$t3,$t4 lw $t6,e S E 5 6 E E 3 E 4 E E 1 E 2 a := b + c + d + e

S 7 E 5 6 E E 3 E 4 E E 1 E 2 a := b + c + d + e Generate: lw $t1,b lw $t2,c add $t3,$t1,$t2 lw $t4,d add $t5,$t3,$t4 lw $t6,e add $t7,$t5,$t6 S 7 E 5 6 E E 3 E 4 E E 1 E 2 a := b + c + d + e

Generate: lw $t1,b lw $t2,c add $t3,$t1,$t2 lw $t4,d add $t5,$t3,$t4 lw $t6,e add $t7,$t5,$t6 sw $t7,a S 7 E 5 6 E E 3 E 4 E E 1 E 2 a := b + c + d + e Pro: easy to rearrange code for global optimization Cons: lots of temporaries

Issues when Processing Expressions Type checking/conversion. Address calculation for more complex types (arrays, records, etc.). Expressions in control structures, such as loops and if tests.

4. The Expressions Language exprParse3.c builds a parse tree for the input file (reuses code from exprParse2.c). An intermediate code is generated from the parse tree, and saved to an output file. The input file is not executed by exprParse3.c that is done by a separate emulator.

Usage test1.txt let x = 2 let y = 3 + x stores intermediate > gcc -Wall -o exprParse3 exprParse3.c > ./exprParse3 < test1.txt > cat codeGen.txt PUSH 2 STORE x WRITE PUSH 3 LOAD x ADD STORE y STOP stores intermediate code in codeGen.txt test1.txt exprParse3 codeGen.txt

Emulator Usage codeGen.txt emulator it runs the intermediate code > ./emulator codeGen.txt Reading code from codeGen.txt == 2 == 5 Stop codeGen.txt emulator it runs the intermediate code

4.1. The Instruction Set The instructions in codeGen.txt are executed by a emulator. it emulates (simulates) real hardware The instructions refer to two data structures used in the emulator.

The Emulator's Data Structures a symbol table of IDs and their integer values a stack of integers for evaluating the expressions x 4 stack symbol table 2

The Instructions WRITE // pop top element off stack and print STOP // exit code emulation LOAD ID // get ID value from symbol table, and push onto stack STORE ID // copy stack top into symbol table for ID continued

PUSH integer // push integer onto stack STORE0 ID // push 0 onto stack, and save to table as value for ID ( same as push 0; store ID) MULT // pop two stack values, multiply them, push result back ADD, MINUS, DIV // same for those ops

Intermediate Code Type Since the intermediate code uses a stack to store values rather than registers, then it is a stack-based (postfix) representation.

4.2. exprParse3.c Coding All the parsing code in exprParse3.c is the same as exprParse2.c. The difference is that the parse tree is passed to a generateCode() function to convert it to intermediate code see main()

main() #define CODE_FNM "codeGen.txt" // where to store generated code int main(void) /* parse, print the tree, then generate code which is stored in CODE_FNM */ { Tree *t; nextToken(); t = statements(); match(SCANEOF); printTree(t, 0); generateCode(CODE_FNM, t); return 0; }

Generating the Code void generateCode(char *fnm, Tree *t) /* Open the intermediate code file, fnm, and write to it. */ { FILE *fp; if ((fp = fopen(fnm, "w")) == NULL) { printf("Could not write to %s\n", fnm); exit(1); } else { printf("Writing code to %s\n", fnm); cgTree(fp, t); fprintf(fp, "STOP\n"); // last instruction in file fclose(fp); } // end of generateCode()

void cgTree(FILE. fp, Tree. t) / void cgTree(FILE *fp, Tree *t) /* Recurse over the parse tree looking for non-NEWLINE subtrees to convert into code Each block of code generated for a non-NEWLINE subtree ends with a WRITE instruction, to print out the value of the line. */ { if (t == NULL) return; Token tok = TreeOper(t); if (tok == NEWLINE) { cgTree(fp, TreeLeft(t)); cgTree(fp, TreeRight(t)); } else { codeGen(fp, t); fprintf(fp, "WRITE\n"); // print value at EOL } // end of cgTree()

void codeGen(FILE. fp, Tree. t) / void codeGen(FILE *fp, Tree *t) /* Convert the tree nodes for ID, INT, ASSIGNOP, PLUSOP, MINUSOP, MULTOP, DIVOP into instructions. The load/store instructions: LOAD ID, STORE ID, STORE0 ID, PUSH integer The math instructions: MULT, ADD, MINUS, DIV */ { if (t == NULL) return; : continued

Token tok = TreeOper(t); if (tok == ID) codeGenID(fp, TreeID(t)); else if (tok == INT) fprintf(fp, "PUSH %d\n", TreeValue(t)); else if (tok == ASSIGNOP) { // id = expr char *id = TreeID(TreeLeft(t)); getIDEntry(id); // don't use Symbol info codeGen(fp, TreeRight(t)); fprintf(fp, "STORE %s\n", id); } : continued

else if (tok == PLUSOP) { codeGen(fp, TreeLeft(t)); codeGen(fp, TreeRight(t)); fprintf(fp, "ADD\n"); } else if (tok == MINUSOP) { fprintf(fp, "MINUS\n"); : continued

else if (tok == MULTOP) { codeGen(fp, TreeLeft(t)); codeGen(fp, TreeRight(t)); fprintf(fp, "MULT\n"); } else if (tok == DIVOP) { fprintf(fp, "DIV\n"); } // end of codeGen()

void codeGenID(FILE. fp, char. id) / void codeGenID(FILE *fp, char *id) /* An ID may already be in the symbol table, or be new, which is converted into a LOAD or a STORE0 code operation. */ { SymbolInfo *si = NULL; if ((si = lookupID(id)) != NULL) // already declared fprintf(fp, "LOAD %s\n", id); else { // new, so add to table addID(id, 0); // 0 is default value fprintf(fp, "STORE0 %s\n", id); } } // end of codeGenID()

From Tree to Code \n PUSH 2 STORE x WRITE PUSH 3 LOAD x ADD STORE y let x = 2 let y = 3 + x \n PUSH 2 STORE x WRITE PUSH 3 LOAD x ADD STORE y STOP \n = y NULL = + x 2 3 x symbol table in exprParse3.c x y

4.3. The Emulator > gcc –Wall –o emulator emulator.c > ./emulator codeGen.txt Reading code from codeGen.txt == 2 == 5 Stop

Emulator Data Structures #define MAX_SYMS 15 // max no of vars #define STACK_SIZE 10 // stack data structure int stack[STACK_SIZE]; int stackTop = -1; // symbol table data structures typedef struct SymInfo { char *id; int value; } SymbolInfo; int symNum = 0; // number of symbols stored SymbolInfo syms[MAX_SYMS]; 2 x 4

Evaluating Input Lines void eval(FILE *fp) /* Read in the code file a line at a time and process the lines. An instruction on a line may be a single command (e.g. WRITE) or a instruction name and an argument (e.g. LOAD x). */ { char buf[BUFSIZ]; char cmd[MAX_LEN], arg[MAX_LEN]; int no; : continued

while (fgets(buf, sizeof(buf), fp) != NULL) { no = sscanf(buf, "%s %s\n", cmd, arg); if ((no < 1) || (no > 2)) printf("Unknown format: %s\n", buf); else processCmd(cmd, arg); // process commands as they are read in } } // end of eval()

Processing an Instruction void processCmd(char *cmd, char *arg) { SymbolInfo *si; if (strcmp(cmd, "LOAD") == 0) { if ((si = lookupID(arg)) == NULL) { printf("Error: load cannot find %s\n", arg); exit(1); } push(si->value); else if (strcmp(cmd, "STORE") == 0) addID(arg, topOf()); else if (strcmp(cmd, "STORE0") == 0) { push(0); addID(arg, 0); continued

else if (strcmp(cmd, "PUSH") == 0) push( atoi(arg) ); else if (strcmp(cmd, "MULT") == 0) { int v2 = pop(); int v1 = pop(); push( v1*v2 ); } else if (strcmp(cmd, "ADD") == 0) { push( v1+v2 ); else if (strcmp(cmd, "MINUS") == 0) { push( v1-v2 ); continued

else if (strcmp(cmd, "DIV") == 0) { int v2 = pop(); if (v2 == 0) { printf("Error: div by 0; using 1\n"); v2 = 1; } int v1 = pop(); push( v1/v2 ); else if (strcmp(cmd, "WRITE") == 0) printf("== %d\n", pop()); else if (strcmp(cmd, "STOP") == 0) { printf("Stop\n"); exit(1); continued

else printf("Unknown instruction: %s\n", cmd); } // end of processCmd()

Evaluating the Code for test1.txt codeGen.txt let x = 2 let y = 3 + x PUSH 2 STORE x WRITE PUSH 3 LOAD x ADD STORE y STOP continued

PUSH 2 STORE X WRITE PUSH 3 stack symbol table 2 x x 2 2 x 2 2 x 3 2 continued

LOAD X ADD STORE Y WRITE STOP stack symbol table x 2 2 3 x 2 2+ 3 5 x