Languages and Compiler Design II IR Code Generation I

Size: px
Start display at page:

Download "Languages and Compiler Design II IR Code Generation I"

Transcription

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 Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1

2 Agenda Grammar G1 CodeGen Overview Arithmetic Expression Translation Boolean Expression Translation Various Statement Translations PSU CS322 HM 2

3 Grammar G1 Input: Output: Approach: AST representation of MINI source Three-address code or IR tree code Syntax-directed translation Generic Grammar G1, start symbol: S E -> E arithop E E relop E E logicop E E -> - E! E E -> newarray E // new int array size E1 E -> E [ E ] // indexed element E -> id num // end-nodes S -> E := E ; // assignment statement S -> if ( E ) then S else S S -> while ( E ) S S -> print E ; S -> return E ; PSU CS322 HM 3

4 CodeGen Overview Arithmetic Expressions: preserve precedence and associativity Pay attention, whether language requires check for zero-divide Boolean Expressions: define short-circuit evaluation vs. complete evaluation discern bit-wise vs. logical and, or, xor multiple unary NOT allowed? Array definition: 1D is simple for compiler Per dimension, record: element size, low-bound, high-bound, total size, index type and type Array element reference: L-value or r-value? Nested array reference: index expression can in turn be array element Discern pass by value or reference, other Statements: Goto into other scope, out of current scope issue in FTN, C Return: long-jump in C non-trivial Parameters: Function parameters in PL/I and Pascal hard Easy to confuse pointer type parameters with reference parameters (in C) PSU CS322 HM 4

5 Arithmetic Expression Translation Generate tree-address code: get new temp per operation E.s holds statements that evaluate E E.t is temp that holds E s value E -> E1 arithop E2 t = new Temp(); E.s := [ E1.s; E2.s; t := E1.t arithop E2.t; ] E.t := t; E -> unaryop E1 t = new Temp(); E.s := [ E1.s; t := unaryop E1.t; ] E.t := t; PSU CS322 HM 5

6 Arithmetic Expression Translation, Cont d To generate IR trees, embed expression subtrees into current root. Attribute E.tr holds IR tree for E E -> E1 arithop E2 E.tr := ( BINOP arithop E1.tr E2.tr ) E -> unaryop E1 E.tr := ( UNOP unaryop E1.tr null ) b * -c + b * d / e // assume l-2-r associativity of * / % => t1 := -c t2 := b * t1 t3 := b * d t4 := t3 / e t5 := t2 + t4 => (BINOP + (BINOP * b (UNOP c ) ) (BINOP / (BINOP * b d ) e ) ) similar to Polish Postfix notation, after Lukasiewicz, 1920 PSU CS322 HM 6

7 Boolean Expression Translation Rely on target machine with conditional branches Condition can be part of instruction Or condition can be inquired by using machine flags Or condition can be evaluated separately (canonical execution) and then be provided as one of the arguments Operands are: condition, target address, and *+1 CodeGen uses temps for intermediate booleans Or CodeGen uses flow of control, so code locations imply state of boolean subexpressions Or combination of both Target computer may provide boolean or even bitwise operations: And Or Xor Not, etc PSU CS322 HM 7

8 Boolean Expression Translation, Quads Relational operations need to record their result, e.g. in machine flags. Logical operations can be realized through computation proper or via control flow. See sample expression: a < 5 b > 2 a < 5 b > 2 // source => Pure code mapping, with logical and, or, xor instruction: t1 := a < 5 // e.g. encode 0 as false, 1 as true t2 := b > 2 t3 := t1 t2 // generate jump out if t3 is false => Control flow mapping, w/o logical and, or, xor : t1 := 1 // guess t1 is true, override if needed if a < 5 goto l1 // could be quad: Cond_jump_if_less t1 := 0 // guess was wrong, override to false l1:t2 := 1 // guess: set t2 to true initially if b > 2 goto l2 t2 := 0 // wrong guess, set t2 to false l2:t3 := 1 // final guess if t1 goto l3 if t2 goto l3 t3 := 0 // final guess computed as false l3: // use t3 PSU CS322 HM 8

9 Better Representation of Booleans, and IR Use target machine s native logical operations for: and, or, xor; also in sample expression: a < 5 b > 2 t1 := 1 if a < 5 goto l1 t1 := 0 l1: t2 := 1 if b > 2 goto l2 t2 := 0 l2: t3 := t1 or t2 // use t3 MOVE t3 ( (BINOP (ESEQ [ [MOVE t1 (CONST 1) ] [CJUMP < (NAME a) (CONST 5) l1 ] [MOVE t1 (CONST 0) ] [LABEL l1] ] t1 ) (ESEQ [ [MOVE t2 (CONST 1)] [CJUMP > (NAME b) (CONST 2) l2 ] [MOVE t2 (CONST 0) ] [LABEL l2] ] t2 ) ) ) PSU CS322 HM 9

10 Value Representation, Relational E -> E1 relop E2 Three-Address Code: L := new Label(); t := new Temp(); E.s := [ E1.s; E2.s; t := 1; if ( E1.t relop E2.t ) goto L; t := 0; L:... ] E.t := t; IR Tree Code: L := new NAME(); t := new TEMP(); E.tr := ( ESEQ [ [MOVE t (CONST 1 ) ] [ CJUMP relop E1.tr E2.tr L ] [MOVE t (CONST 0) ] [LABEL L] t ) PSU CS322 HM 10

11 Value Representation, Three-Address Code E -> E1 E2 L := new Label(); t := new Temp(); E.s := [ E1.s; E2.s; t := 1; if ( E1.t == 1 ) goto L; if ( E2.t == 1 ) goto L; t := 0; L:... ] E.t := t; E -> E1 && E2 L := new Label(); t := new Temp(); E.s := [ E1.s; E2.s; t := 0; if ( E1.t == 0 ) goto L; if ( E2.t == 0 ) goto L; t := 1; L:... ] E.t := t; E -> E1! E2 t := new Temp(); E.s := [ E1.s; t := 1 E1.t; ] E.t := t; PSU CS322 HM 11

12 Value Representation, IR Tree Code E -> E1 E2 E -> E1 && E2 L = new NAME(); t = new TEMP(); E.tr := (ESEQ [ [MOVE t (CONST 1) ] [CJUMP == E1.tr (CONST 1) L ] [CJUMP == E2.tr (CONST 1) L ] [MOVE t (CONST 0) ] [LABEL L] t ) L = new NAME(); t = new TEMP(); E.tr := (ESEQ [ [MOVE t (CONST 0) ] [CJUMP == E1.tr (CONST 0) L ] [CJUMP == E2.tr (CONST 0) L ] [MOVE t (CONST 1) ] [LABEL L] t ) E -> E1! E2 t = new TEMP(); E.tr := (ESEQ [MOVE t (BINOP (CONST 1) E1.tr)] t ) PSU CS322 HM 12

13 Control-Flow Mapping, Long If Version Booleans used in programs to direct flow of control, e.g. if ( a < 5 b > 2 ) S1; else S2; Frequently, the Boolean result is not needed afterwards. Thus possible to generate positional code. Instead of: // assume ( a < 5 b > 2 ) stored in t3 // code to compute t3, includes boolean OR if ( t3 == 0 ) goto l2 L1: code for S1 goto L3 l2: code for S2 L3:... Successor of if-statement... t3 not needed; if machine flag: overridden by S1, S2 PSU CS322 HM 13

14 Control-Flow Mapping, Shorter If Version if ( a < 5 ) goto L4 if ( b > 2 ) goto L4 goto L5 L4: code for S1 goto L6 l5: code for S2 L6:... Code after if-statement 1. No need to create temps to compute boolean value 2. How does code-gen know where to branch to? Use backpatching! Can be done by buffering code, or after CodeGen. Ramifications would be good Midterm question PSU CS322 HM 14

15 Control-Flow Mapping, Nested If Statements Source Code Skeleton if ( a < 5 ) if ( b > 2 ) S1; else S2; //end if else if ( c < 6 ) S3; else S4; //end if //end if Object Code Skeleton if ( a >= 5 ) goto L8 // back-patch if ( b <= 2 ) goto L7 // back-patch code for S1 goto L10 // back-patch L7: // L7 resolved code for S2 goto L10 // back-patch L8: // L8 resolved if ( c >= 6 ) goto L9 // back-patch code for S3 goto L10 // back-patch L9: // L9 resolved code for S4 L10: // L10 resolved Data structures needed to back-patch? PSU CS322 HM 15

16 Control-Flow Mapping, Elsif Clauses (Ada) Source Code Skeleton if a < 5 then S1; elsif b > 2 then S2; elsif c < 6 then S3; else S4; end if; Object Code Skeleton if ( a >= 5 ) goto L11 code for S1 goto L14 L11: if ( b <= 2 ) goto L12 code for S2 goto L14 L12: if ( c >= 6 ) goto L13 code for S3 goto L14 L13: code for S4 L14: How can linked-list of to-be-back-patched addresses be created? PSU CS322 HM 16

17 Control-Flow Mapping, While Source Code Skeleton while ( i < 10 ) { S; i++; } //end while // assume i NOT needed after // i is pure IV Object Code Skeleton // R1 holds induction variable L15: if ( R1 >= 10 ) goto L16 code for S R1++ goto L15 L16: Are there size (of code) limitations to back-patching? PSU CS322 HM 17

18 Control-Flow Mapping, Repeat (Pascal) Source Code Skeleton //Pascal source repeat S; i++; until i >= 10; // again no use of i after Object Code Skeleton // R1 holds induction variable L15: if ( R1 >= 10 ) goto L16 code for S R1++ goto L15 L16: Fall-Through in Repeat vs. initial test in While PSU CS322 HM 18

19 Control-Flow Mapping, For Source Code Skeleton for( int i=0; i<10; i++ ) { S; } //end for // i is undefined/not used // can be IV in reg Object Code Skeleton mov R1, #0 L17: If ( R1 >= 10 ) goto L18 code for S R1++ goto L17 L18: What happens if i (induction variable AKA IV) is defined outside, and used as loop parameter? What is its value after for loop completion? Can it be referenced? i.e. value be printed? What happens if IV is assigned inside loop? What should happen, if IV value is > end-value at start? PSU CS322 HM 19

20 Back Patching Example if ( a < 5 b > 2 ) S1; else S2; Handling a<5: if (a < 5) goto <Lx>; // <Lx> needs to be patched; addr. insertion Handling b>2: if (b > 2) goto <Ly>; // <Ly> needs to be patched Handling....: if (a < 5) goto <Lx>; //.. else fall through if (b > 2) goto <Lx>; // <Ly> is patched to <Lx> goto <Lz> // <Lz> needs to be patched Handling if.. S1 else S2: if (a < 5) goto L4; // <Lx> is patched to L4 if (b > 2) goto L4; goto L5; // <Lz> is patched to L5 L4: [code for S1] goto L6; // then clause L5: [code for S2] // else clause L6: // end of If Statement PSU CS322 HM 20

21 Back Patching: Jump Labels Three-Address Code: Add two attributes E.true position to jump to when E evaluates to true; E.false position to jump to when E evaluates to false. E -> E1 relop E2 E.s := [ E1.s; E2.s; if ( E1.t relop E2.t ) goto E.true; E.false: ] E -> E1 E2 E1.true := E.true; E1.false := new Label(); E2.true := E.true; E2.false := E.false; E.s := [ E1.s; E1.false: E2.s; ] PSU CS322 HM 21

22 Back Patching: Three-Address Code Cont d E -> E1 && E2 E1.true := new Label(); E1.false := E.false; E2.true := E.true; E2.false := E.false; E.s := [ E1.s; E1.true: E2.s; ] E ->! E1 E1.true := E.false; E1.false := E.true; E.s := E1.s; PSU CS322 HM 22

23 Back Patching: Jump Labels Cont d IR Tree Code: E -> E1 relop E2 E.tr := ( ESEQ [CJUMP relop E1.tr E2.tr E.true ] null ) E -> E1 E2 E1.true := E.true; E1.false := new NAME(); E2.true := E.true; E2.false := E.false; E.tr := (ESEQ [stmt( E1.tr); LABEL( E1.false ); stmt( E2.tr); ] null ) PSU CS322 HM 23

24 Back Patching: IR Tree Cont d E -> E1 && E2 E1.true := new NAME(); E1.false := E.false; E2.true := E.true; E2.false := E.false; E.tr := (ESEQ [stmt( E1.tr ); LABEL( E1.true ); stmt( E2.tr ); ] null) E ->! E1 E1.true := E.false; E1.false := E.true; E.tr := E1.tr; PSU CS322 HM 24

25 Converting Back to Value Actual Boolean value are needed in programs, e.g. boolean x = a < 5 b > 2; We still need to generate a value for the Boolean expression! This can be implemented by patching the two labels E.true and E.false for the Boolean expression E with two assignment statements for assigning 1 and 0, respectively. Boolean expression E t = new Temp(); E.true := new Label(); E.false := new Label(); L := new Label(); E.s := [ E.true: t := 1; goto L; E.false: t := 0; L: ] E.t := t; PSU CS322 HM 25

26 New Arrays E => newarray E1 Storage allocation for E: Follow Java s array storage convention. The length of array is stored as the 0 th element. So storage for a 10-element array actually requires 11 cells Cell initialization All elements automatically initialized to 0; you emit code Pseudo IR Code: L: new Label; t1,t2,t3: new Temps;// wdsize == 4 E.s := [ E1.s; t1 := ( E1.t + 1 ) * wdsize; // number of elements t2 := malloc( t1 ); // t2 points to cell 0 t2[0] := E1.t; // store array length t3 := t2 + ( E1.t * wdsize ); // t3 points to last cell L: t3[0] := 0; // init a cell to 0 t3 := t3 - wdsize; if ( t3 > t2 ) goto L; ] E.t := t2; // move down a cell // loop back PSU CS322 HM 26

27 Arrays Element Reference E => E1 [ E2 ] Calculate address for E: addr( a[i] ) = base a + (i+1) * wdsize Bounds check: i >= 0 and i < num-elements. i is general expression! L1,L2: new Label; t1,t2,t3,t4: new Temps; E.s := [ E1.s; E2.s; t1 := E1.t[ 0 ]; // t1 holds num elements if ( E2.t < 0 ) goto L1; // too low? if ( E2.t >= t1 ) goto L1;// too high? t2 := E2.t + 1; // must be OK t3 := t2 * wdsize; // compute offset t4 := E1.t[ t3 ]; // address = start + offset goto L2; // bypass exception handler L1: param E1.t; param E2.t; call arrayerror, 2; L2: ] // t4 holds final address E.t := t4; PSU CS322 HM 27

28 Statements Assignment Statement S => E1 := E2 ; => S.s :=[ E1.s; E2.s; E1.t := E2.t; ] If Statement with Else Clause S => if ( E ) then S1 else S2 ; => L1, L2, L3: new Labels; E.true := L1; E.false := L2; S.s :=[ E.s; L1: S1.s; goto L3; L2: S2.s; L3: ; ] PSU CS322 HM 28

29 Statements, Cont d While Statement S => while ( E ) S1 ; => L1, L2, L3: new labels; // no explicit jump to L2 E.true := L2; E.false := L3; S.s :=[ L1: E.s; L2: S1.s; goto L1; L3: ] Print Statement with 1 argument S => print E ; => S.s :=[ E.s; param E.t; call print, 1; ] PSU CS322 HM 29

Languages and Compiler Design II IR Code Optimization

Languages and Compiler Design II IR Code Optimization Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda IR Optimization

More information

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (1 Principle of Compilers Lecture VIII: Intermediate Code Generation Alessandro Artale Faculty of Computer Science Free

More information

Formal Languages and Compilers Lecture X Intermediate Code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

CMPT 379 Compilers. Anoop Sarkar.  11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

Compiler Construction 2009/2010: Intermediate Representation

Compiler Construction 2009/2010: Intermediate Representation Compiler Construction 2009/2010: Intermediate Representation Annette Bieniusa November 24, 2009 Outline 1 Contexts 2 Canonical Trees Using Expressions in different Contexts Compare the translation for

More information

LECTURE 17. Expressions and Assignment

LECTURE 17. Expressions and Assignment LECTURE 17 Expressions and Assignment EXPRESSION SYNTAX An expression consists of An atomic object, e.g. number or variable. An operator (or function) applied to a collection of operands (or arguments)

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

Module 27 Switch-case statements and Run-time storage management

Module 27 Switch-case statements and Run-time storage management Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss

More information

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation. UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are

More information

7 Translation to Intermediate Code

7 Translation to Intermediate Code 7 Translation to Intermediate Code ( 7. Translation to Intermediate Code, p. 150) This chpater marks the transition from the source program analysis phase to the target program synthesis phase. All static

More information

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR.

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR. CS 4120 Lecture 14 Syntax-directed translation 26 September 2011 Lecturer: Andrew Myers We want to translate from a high-level programming into an intermediate representation (IR). This lecture introduces

More information

Intermediate Representa.on

Intermediate Representa.on IR Intermediate Representa.on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Intermediate Representation Language Specific Language + Machine Independent Machine Dependent

More information

CS2210: Compiler Construction. Code Generation

CS2210: Compiler Construction. Code Generation Modern Compiler Project Fortran program Fortran s Lexer, Parser, and Static Checker Intermediate Code Generator IR MIPS Code Generator MIPS code C program C s Lexer, Parser, and Static Checker Intermediate

More information

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1 SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different

More information

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures Control Structures

More information

Topic 7: Intermediate Representations

Topic 7: Intermediate Representations Topic 7: Intermediate Representations COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 2 Intermediate Representations 3 Intermediate Representations 4 Intermediate Representations

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

CS 432 Fall Mike Lam, Professor. Code Generation

CS 432 Fall Mike Lam, Professor. Code Generation CS 432 Fall 2015 Mike Lam, Professor Code Generation Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00

More information

Compiler Internals. Reminders. Course infrastructure. Registering for the course

Compiler Internals. Reminders. Course infrastructure. Registering for the course Compiler Internals 15-745 Optimizing Compilers Spring 2006 Peter Lee Reminders Get on the course mailing list Check out the web site http://www.cs.cmu.edu/afs/cs/ academic/class/15745-s06/web subscribe

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

We can emit stack-machine-style code for expressions via recursion

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines Lecture Outline Code Generation Lecture 30 (based on slides by R. Bodik) Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation of the

More information

CMPSC 160 Translation of Programming Languages. Three-Address Code

CMPSC 160 Translation of Programming Languages. Three-Address Code CMPSC 160 Translation of Programming Languages Lectures 16: Code Generation: Three- Address Code Three-Address Code Each instruction can have at most three operands Each operand corresponds to a memory

More information

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers Where we are CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 13: Intermediate Code 25 Sep 09 Source code (character stream) Token stream Abstract syntax tree Abstract syntax tree

More information

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev Fall 2015-2016 Compiler Principles Lecture 6: Intermediate Representation Roman Manevich Ben-Gurion University of the Negev Tentative syllabus Front End Intermediate Representation Optimizations Code Generation

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

More information

Code Generation. Lecture 30

Code Generation. Lecture 30 Code Generation Lecture 30 (based on slides by R. Bodik) 11/14/06 Prof. Hilfinger CS164 Lecture 30 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

Module 26 Backpatching and Procedures

Module 26 Backpatching and Procedures Module 26 Backpatching and Procedures In this module, we would learn to generate three-address code for control flow statements using backpatching. We shall also discuss to combine Boolean expressions

More information

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

Module 25 Control Flow statements and Boolean Expressions

Module 25 Control Flow statements and Boolean Expressions Module 25 Control Flow statements and Boolean Expressions In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 09 IR (ackpatching) THEORY OF COMPILATION Eran Yahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec09.pptx Reference: Dragon 6.2,6.3,6.4,6.6 1 Recap Lexical analysis regular expressions identify

More information

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen COP4020 Programming Languages Control Flow Prof. Robert van Engelen Overview Structured and unstructured flow Goto's Sequencing Selection Iteration and iterators Recursion Nondeterminacy Expressions evaluation

More information

LECTURE 18. Control Flow

LECTURE 18. Control Flow LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a

More information

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Control Structures Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures

More information

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow CSE 452: Programming Languages Expressions and Control Flow Outline of Today s Lecture Expressions and Assignment Statements Arithmetic Expressions Overloaded Operators Type Conversions Relational and

More information

Intermediate Code Generation (ICG)

Intermediate Code Generation (ICG) Intermediate Code Generation (ICG) Transform AST to lower-level intermediate representation Basic Goals: Separation of Concerns Generate efficient code sequences for individual operations Keep it fast

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 21: Generating Pentium Code 10 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 Simple Code Generation Three-address code makes it

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions Agenda CS738: Advanced Compiler Optimizations Data Flow Analysis Amey Karkare karkare@cse.iitk.ac.in http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur Static analysis and compile-time

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer

More information

KU Compilerbau - Programming Assignment

KU Compilerbau - Programming Assignment 716.077 KU Compilerbau - Programming Assignment Univ.-Prof. Dr. Franz Wotawa, Birgit Hofer Institute for Software Technology, Graz University of Technology April 20, 2011 Introduction During this semester

More information

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona CSc 453 Compilers and Systems Software 13 : Intermediate Code I Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Introduction Compiler Phases

More information

Code Generation. CS 1622: Code Generation & Register Allocation. Arrays. Why is Code Generation Hard? Multidimensional Arrays. Array Element Address

Code Generation. CS 1622: Code Generation & Register Allocation. Arrays. Why is Code Generation Hard? Multidimensional Arrays. Array Element Address Code Generation CS 1622: Code Generation & Register Allocation Input: Intermediate representation Output: Target code Example: cond_expr if-statement stmt_list L1: slti $t1, 3, $s0 beq $t1, $zero, L1 addi

More information

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions ICOM 4036 Programming Languages Ch7. Expressions & Assignment Statements Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment

More information

CPSC 411, Fall 2010 Midterm Examination

CPSC 411, Fall 2010 Midterm Examination CPSC 411, Fall 2010 Midterm Examination. Page 1 of 11 CPSC 411, Fall 2010 Midterm Examination Name: Q1: 10 Q2: 15 Q3: 15 Q4: 10 Q5: 10 60 Please do not open this exam until you are told to do so. But please

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation 1 Intermediate Code Generation Translating source program into an intermediate language" Simple CPU Independent, yet, close in spirit to machine language Benefits Retargeting

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

Itree Stmts and Exprs. Back-End Code Generation. Summary: IR -> Machine Code. Side-Effects

Itree Stmts and Exprs. Back-End Code Generation. Summary: IR -> Machine Code. Side-Effects Back-End Code Generation Given a list of itree fragments, how to generate the corresponding assembly code? datatype frag = PROC of {name : Tree.label, function name body : Tree.stm, function body itree

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

Announcements. Project 2: released due this sunday! Midterm date is now set: check newsgroup / website. Chapter 7: Translation to IR

Announcements. Project 2: released due this sunday! Midterm date is now set: check newsgroup / website. Chapter 7: Translation to IR Announcements Project 2: released due this sunday! Midterm date is now set: check newsgroup / website. 1 Translation to Intermediate Code (Chapter 7) This stage converts an AST into an intermediate representation.

More information

Alternatives for semantic processing

Alternatives for semantic processing Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

More information

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction CSc 520 Principles of Programming Languages 26 : Control Structures Introduction Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Translation. From ASTs to IR trees

Translation. From ASTs to IR trees Translation From ASTs to IR trees Copyright c 2010 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax

More information

An Overview of Compilation

An Overview of Compilation An Overview of Compilation (www.cse.iitb.ac.in/ uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay January 2014 cs306 Compilation Overview: Outline 1/18 Outline

More information

Intermediate Representations

Intermediate Representations Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address

More information

Intermediate Representations & Symbol Tables

Intermediate Representations & Symbol Tables Intermediate Representations & Symbol Tables Copyright 2014, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev Fall 2016-2017 Compiler Principles Lecture 5: Intermediate Representation Roman Manevich Ben-Gurion University of the Negev Tentative syllabus Front End Intermediate Representation Optimizations Code Generation

More information

A Short Summary of Javali

A Short Summary of Javali A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler

More information

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below: QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below: This is a template, used for generic programming! QUIZ 2. Why is the vector class called a container? 3. Explain how the

More information

PRINCIPLES OF COMPILER DESIGN

PRINCIPLES OF COMPILER DESIGN PRINCIPLES OF COMPILER DESIGN 2 MARK QUESTIONS WITH ANSWERS UNIT I 1. What is a Complier? A Complier is a program that reads a program written in one language-the source language-and translates it in to

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

IC Language Specification

IC Language Specification CS 301 Spring 2016 IC Language Specification The IC Language For the implementation project, you will build a compiler for an object-oriented language called IC (for Irish Coffee 1 ), which is essentially

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into Intermediate representations source code front end ir opt. ir back end target code front end produce an intermediate representation (IR) for the program. optimizer transforms the code in IR form into an

More information

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1 CSE P 501 Compilers Intermediate Representations Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 G-1 Administrivia Semantics/types/symbol table project due ~2 weeks how goes it? Should be caught up on

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL 6.035 Project 3: Unoptimized Code Generation Jason Ansel MIT - CSAIL Quiz Monday 50 minute quiz Monday Covers everything up to yesterdays lecture Lexical Analysis (REs, DFAs, NFAs) Syntax Analysis (CFGs,

More information

IR trees: Statements. IR trees: Expressions. Translating MiniJava. Kinds of expressions. Local variables: Allocate as a temporary t

IR trees: Statements. IR trees: Expressions. Translating MiniJava. Kinds of expressions. Local variables: Allocate as a temporary t IR trees: Expressions CONST Integer constant i i NAME Symbolic constant n [a code label] n TEMP Temporary t [one of any number of registers ] t BINOP Application of binary operator: e 1 e 2 ADD, SUB, MUL,

More information

CS 314 Principles of Programming Languages. Lecture 9

CS 314 Principles of Programming Languages. Lecture 9 CS 314 Principles of Programming Languages Lecture 9 Zheng Zhang Department of Computer Science Rutgers University Wednesday 5 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Homework

More information

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

More information

Chapter 7 Control I Expressions and Statements

Chapter 7 Control I Expressions and Statements Chapter 7 Control I Expressions and Statements Expressions Conditional Statements and Guards Loops and Variation on WHILE The GOTO Controversy Exception Handling Values and Effects Important Concepts in

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia This is a closed book exam; no notes; no calculators. Answer in the space provided. There are 8 questions on 14 pages,

More information

Implementing Control Flow Constructs Comp 412

Implementing Control Flow Constructs Comp 412 COMP 412 FALL 2018 Implementing Control Flow Constructs Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information

Expressions and Assignment Statements

Expressions and Assignment Statements Expressions and Assignment Statements Introduction Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation, need to be familiar with

More information

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things. A Appendix Grammar There is no worse danger for a teacher than to teach words instead of things. Marc Block Introduction keywords lexical conventions programs expressions statements declarations declarators

More information

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens

More information

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts Compiler Structure Source Code Abstract Syntax Tree Control Flow Graph Object Code CMSC 631 Program Analysis and Understanding Fall 2003 Data Flow Analysis Source code parsed to produce AST AST transformed

More information

Expressions and Assignment

Expressions and Assignment Expressions and Assignment COS 301: Programming Languages Outline Other assignment mechanisms Introduction Expressions: fundamental means of specifying computations Imperative languages: usually RHS of

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements ISBN Chapter 7 Expressions and Assignment Statements ISBN 0-321-33025-0 Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit

More information

intermediate-code Generation

intermediate-code Generation intermediate-code Generation }sequence of intermediate representations source program High Level intermediate Representation Low Level intermediate Representation Target Code e.g. C programming language

More information

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code) Compiler Theory (Intermediate Code Generation Abstract S yntax + 3 Address Code) 006 Why intermediate code? Details of the source language are confined to the frontend (analysis phase) of a compiler, while

More information

CMSC430 Spring 2014 Midterm 2 Solutions

CMSC430 Spring 2014 Midterm 2 Solutions CMSC430 Spring 2014 Midterm 2 Solutions 1. (12 pts) Syntax directed translation & type checking Consider the following grammar fragment for an expression for C--: exp CONST IDENT 1 IDENT 2 [ exp 1 ] Assume

More information

UCB CS61C : Machine Structures

UCB CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 Introduction to MIPS Data Transfer & Decisions I Sr Lecturer SOE Dan Garcia 2014-02-03 Prof Pieter Abbeel s recent research is in

More information

Intermediate Representations

Intermediate Representations Compiler Design 1 Intermediate Representations Compiler Design 2 Front End & Back End The portion of the compiler that does scanning, parsing and static semantic analysis is called the front-end. The translation

More information

Software II: Principles of Programming Languages. Why Expressions?

Software II: Principles of Programming Languages. Why Expressions? Software II: Principles of Programming Languages Lecture 7 Expressions and Assignment Statements Why Expressions? Expressions are the fundamental means of specifying computations in a programming language

More information

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7 CS322 Languages and Compiler Design II Spring 2012 Lecture 7 1 USES OF BOOLEAN EXPRESSIONS Used to drive conditional execution of program sections, e.g. IF (a < 17) OR (b = 12) THEN... ELSE...; WHILE NOT

More information