Intermediate Code Generation Mooly Sagiv Schrierber 317 03-640-7606 Wed 10:00-12:00 html://www.math.tau.ac.il/~msagiv/courses/wcc02.html.

Slides:



Advertisements
Similar presentations
Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Advertisements

1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Intermediate Code Generation
The University of Adelaide, School of Computer Science
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Compilation (Semester A, 2013/14) Lecture 14: Compiling Object Oriented Programs Noam Rinetzky Slides credit: Mooly Sagiv 1.
Register Allocation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
1 Compiler Construction Intermediate Code Generation.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber
Activation Records Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter.
Compiler Construction
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Code Generation Mooly Sagiv html:// Chapter 4.
Basic Blocks Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter 8.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Liveness Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Activation Records Mooly Sagiv Schrierber Wed 14:00-15:00 this week only html://
Activation Records Mooly Sagiv html:// Chapter 6.3.
Semantic Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Activation Records Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter.
Compiler Summary Mooly Sagiv html://
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Course Overview Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Textbook:Modern.
Abstract Syntax Mooly Sagiv html://
Semantic Analysis Mooly Sagiv html://
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
COP4020 Programming Languages
CS 2104 Prog. Lang. Concepts Subprograms
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Lexical Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Textbook:Modern.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
Automatic compilation Student name: Eldad Uzman Student ID : Lecturer : DR Itzhak Aviv.
Activation Records (in Tiger) CS 471 October 24, 2007.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Abstract Syntax Mooly Sagiv Schrierber Wed 10:00-12:00 html://
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Introduction to Code Generation and Intermediate Representations
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
COMPILERS Intermediate Code hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Code Generation for Control Flow Mooly Sagiv html:// Chapter
Imperative Programming C. Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands:
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
CS 404 Introduction to Compiler Design
Mooly Sagiv html://
Compilers.
C Basics.
An Overview to Compiler Design
Compiler Construction
Compiler Construction
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Intermediate Code Generation Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter 7 (Chapter 6 next week)

Basic Compiler Phases Source program (string) Fin. Assembly lexical analysis syntax analysis semantic analysis Translate Instruction selection Register Allocation Tokens Abstract syntax tree Intermediate representation Assembly

Why can’t we translate directly into machine language

Why use intermediate languages? Simplify the compilation phase –ultimately leads to a more efficient code Portability of the compiler front-end Reusability of the compiler back-end Java C Pascal C++ ML Pentium MIPS Sparc Java C Pascal C++ ML Pentium MIPS Sparc IR

IR Design Goals Convenient to generate IR from the source Convenient to generate machine code from IR –Missmatches between Source and Target Clear operational meaning Textbook Solution Simple intermediate instructions Tree like expressions

A Grammar for the Tree IR T_stm ::= T_stm T_stm (T_SEQ) T_stm ::= T_label(T_LABEL) T_exp ::=T_exp(T_MEM) T_stm ::= T_exp Temp_labelList ( T_JUMP) T_stm::= T_relop T_exp T_exp Temp_label Temp_label (T_CJUMP) T_stm::=T_exp T_exp(T_MOVE) T_stm ::= T_exp(T_EXP) T_exp ::=T_binop T_exp T_Exp(T_BINOP) T_exp ::= Temp_temp(T_TEMP) T_exp ::= T_stm T_exp (T_ESEQ) T_exp ::= Temp_label(T_LABEL) T_exp ::=int(T_CONST) T_exp::= T_exp T_expList(T_CALL)

/* tree.h */ typedef struct T_exp_ *T_exp; struct T_stm_ { enum {T_SEQ, T_LABEL, T_JUMP, …, T_EXP} kind; union { struct {T_stm left, right;} SEQ; … } u;}; T_stm T_Seq(T_stm left, T_stm right); T_stm T_Label(Temp_label); T_stm T_Jump(T_exp exp, Temp_labelList labels); T_stm T_Cjump(T_relOp op, T_exp left, T_exp right, Temp_label _true, Temp_label _false ); T_stm T_Move(T_exp, T_exp); T_stm T_Exp(T_exp); typedef enum {T_plus, T_minus, T_mul, T_div, T_and, T_or, T_lshift, T_rshift, T_arshift, T_xor} T_binOp ; typedef enum {T_eq, T_ne, T_lt, T_gt, T_le, T_ge, T_ult, T_ule, T_ugt, T_uge} T_relOp; struct T_exp_ { enum {T_BINOP, T_MEM, T_TEMP, …, T_CALL} kind; union {struct {T_binop op; T_exp left; T_exp right;} BINOP; … } u; } ;

Example factorial let function nfactor (n: int): int = if n = 0 then 1 else n * nfactor(n-1) in nfactor(10) end

Abstract Tiger Program letExp(decList( functionDec(fundecList( fundec(nfactor, fieldList( field(n, int, fld-escaped=FALSE), fieldList()), int, ifExp( opExp(EQUAL, varExp(simpleVar(n)), intExp(0)), intExp(1), opExp(TIMES, varExp(simpleVar(n)), callExp(nfactor, expList(opExp(MINUS, varExp(simpleVar(n)), intExp(1)), expList()))))), fundecList())), decList()), seqExp(expList( callExp(nfactor, expList(intExp(10), expList())), expList())))

IR for Main /* prologue of main starts with l1 */ /* body of main */ MOV(TEMP(RV), CALL(NAME(l2), ExpList(CONST(10), null /* next argument */))) /* epilogue of main */

IR for nfact /* Prologue of nfunc starts with l2 */ /* body of nfunc */ MOV(TEMP(RV), ESEQ(SEQ( CJUMP(=, “n”, CONST(0), NAME(l3), NAME(l4)), SEQ(LABEL(l3) /* then-clause */, SEQ(MOV(TEMP(t1), CONST(1)), SEQ(JUMP(NAME(l5)), SEQ(LABEL(l4), /* else-clause */ SEQ(MOV(TEMP(t1), BINOP(MUL, “n”, CALL(NAME(l2), ExpList(BINOP(MINUS, “n”, CONST(1)), null /* next argument */)))), LABEL(l5)))…), TEMP(t1))) /* epilogue of nfunc */

Outline of the Translation (translate.c) Top-down traversal over the abstract syntax tree Generate code to allocate memory for declarations and initializations (next week) Generate code for function declarations: –Prologue –The body expression –Epilogue Generate code for expressions –Value expressions x + y –Location expressions x < y Statements – x := y –Control flow

The rest of this lecture L-values and R-Values Arithmetic expressions Conditionals and Loops Conversions Complex data types –Arrays –Structures Memory Checks

L-values vs. R-values Assignment x := exp is compiled into: –Compute the address of x –Compute the value of exp –Store the value of exp into the address of x Generalization –R-value –L-value

Translating Expressions Straightforward by induction on the abstract expression tree /* translate.c */ Tr_exp Tr_opExp(A_oper oper, Tr_exp left, Tr_exp right) { switch (oper) { case A_plusOp: return Tr_opArithExp(T_plus, left, right); case A_minusOp: return Tr_opArithExp(T_minus, left, right); case A_timesOp: … case A_eqOp: return Tr_opCondExp(T_eq,left,right); case A_neqOp: return Tr_opCondExp(T_ne,left,right); case A_ltOp: … } assert(0); return NULL; }

Conditional Expressions Translating Expressions in Conditions may be tricky Two options –Value computation Compute a value of Boolean Expression –Location computation Compute a label in the code that will be reached if the expression holds Allows shortcut computations

Example C code if (a 7) a = b * c CJUMP(<, “a” CONST(6), l1, l2) LABEL(l1) CJUMP(>, (BINOP(+, “b”, CONST(1)), CONST(7), l3, l2) LABEL(l3) MOVE(“a”, BINOP(*, “b”, “c”) LABEL(l2)

Conditional Expressions in Tiger static Tr_exp Tr_opCondExp( T_relOp oper, Tr_exp left, Tr_exp right) { struct Cx cx; cx.stm = T_Cjump(oper, left, right, NULL, NULL); cx.trues = PatchList(cx.stm->u.CJUMP._true, NULL); cx.falses = PatchList(cx.stm->u.CJUMP._false, NULL); return Tr_Cx(cx.trues, cx.falses, cx.stm); } if a >b then x := 5 SEQ CJUMP GT “a” “b” t NAME f SEQ Code for x:=5 t LABEL f

Loops Similar to if-then else Need to handle break while a >b do S

Conversions Local translation may lead to converting representations –Value-computation  Location-computation Examples if (x+5) then 0 else 1 (a > b) + b x := if (a>b) then a else b x := (a > b) (if a>b then a else b) + 1

Complex Data Types Data types like arrays, strings, and records may require special treatment Important questions –Duration –Static vs. Dynamic size –Structured L-values

Complex Data Types in Tiger Arrays, strings, and record’s fields are long-lived –Usually allocated in the heap –No structured L-values Example: Tiger Record Allocation type foo = { a : ty1, b : ty2}... = foo {a =e1, b = e2} ESEQ (SEQ ( MOV(TEMP r, CALL(NAME MALLOC, CONST 2*W)), SEQ( MOV(MEM(+(0*W, TEMP r)), TransExp(e1))), MOV(MEM(+(1*W, TEMP r)), (TransExp(e2))))), TEMP r)

Example Tiger Arrays let type intArray = array of int var a := intArray[12] of 0 var b := intArray[13] of 7 in a := b SEQ( CONST 0, SEQ( MOVE(TEMP ta, CALL(NAME initArray, CONST 12, CONST 0)), SEQ( MOVE(TEMP tb, CALL(NAME initArray, CONST 13, CONST 7)), MOVE(TEMP ta, TEMP tb)))))

L-values of Arrays and Structures (Tiger) The l-value of a[i] MEM(+(“a”, *(CONST W, “i”))) For a structure s.f MEM(+(“s”, *(CONST W, CONST k f )))

Big L-values In some programming languages, more than one word need to be copied or stored Examples: –C structures –Pascal arrays How can this be handled?

Memory checks Can the compiler guarantee that no invalid memory is referred –At compile-time –At runtime? Examples –Array references Algol, Pascal, Java, PL.1 –Runtime checks C –No checks Ada, C# –User control –Field and pointer dereferences The best solutions combine runtime and compile-time checks

Summary Intermediate code simplifies the translation and increases re-use Tree-like intermediate code simplifies the translation of expressions –No temporaries Abstract syntax helps Memory management is interesting –Mostly next week