Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation. 2 Intermediate languages Runtime environments Declarations Expressions Statements.
Advertisements

Chapter 6 Intermediate Code Generation
Intermediate Code Generation
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
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.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Compiler Designs and Constructions
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Compiler Chapter# 5 Intermediate code generation.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
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,
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Code Generation CPSC 388 Ellen Walker Hiram College.
Code Generation How to produce intermediate or target code.
Chap. 4, Intermediate Code Generation
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Three Address Code Generation of Control Statements continued..
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 404 Introduction to Compiler Design
Intermediate code Jakub Yaghob
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate code generation Jakub Yaghob
Compiler Construction
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
Intermediate Code Generation Part II
Three Address Code Generation Control Statement
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
7.4 Boolean Expression and Control Flow Statements
CSc 453 Intermediate Code Generation
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Intermediate Code Generation
Intermediate Code Generation Part II
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Intermediate Code Generation Part I
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Code generation and data types
Presentation transcript:

Intermediate Code Generation

2 Intermediate languages Declarations Expressions Statements

3 Intermediate Languages Syntax tree Postfix notation a b c - * b c - * + := Three-address code a := b * - c + b * - c := a+ * * - c bb - c

4 Three-Address Code x := y op z where x, y, z are names, constants, or temporaries x + y * z t1 := y * z t2 := x + t1 a := b * -c + b * -c t1 := -c t2 := b * t1 t3 := -c t4 := b * t3 t5 := t2 + t4 a := t5

5 Types of Three-Address Code Assignment statementx := y op z Assignment statementx := op y Copy statementx := y Unconditional jumpgoto L Conditional jumpif x relop y goto L Procedural callparam x call p return y

6 Types of Three-Address Code Indexed assignmentx := y[i] x[i] := y Address and pointer assignment x := &y x := *y *x := y

7 Implementation of Three-Address Code Quadruples oparg1arg2result (0) - c t1 (1) * b t1 t2 (2) - c t3 (3) * b t3 t4 (4) + t2 t4 t5 (5) := t5 a

8 Implementation of Three-Address Code Triples oparg1arg2 (0) - c (1) * b (0) (2) - c (3) * b (2) (4) + (1) (3) (5) := a (4)

9 Implementation of Three-Address Code Indirect Triples statement op arg1 arg2 (0)(14)(14)- c (1)(15)(15)* b (14) (2)(16)(16)- c (3)(17)(17)* b (16) (4)(18)(18)+ (15) (17) (5)(19)(19):= a (18)

10 Comparison Qualdruples – direct access of the location for temporaries – easier for optimization Triples – space efficiency Indirect Triples – easier for optimization – space efficiency

11 New Names and Labels Function newtemp returns a new name for each call Function newlabel returns a new label for each call

12 Assignments S  id “:=” E {emit(id.name ‘:=’ E.place);} E  E 1 “+” E 2 {E.place := newtemp; emit(E.place ‘:=’ E 1.place ‘+’ E 2.place);} E  E 1 “*” E 2 {E.place := newtemp; emit(E.place ‘:=’ E 1.place ‘*’ E 2.place);} E  “-” E 1 {E.place := newtemp; emit(E.place ‘:=’ ‘-’ E 1.place);} E  “(” E 1 “)” {E.place := E 1.place} E  id {E.place := id.name}

13 Array Accesses A[i]: base + (i - low)  w  (i  w) + (base - low  w) A[i 1, i 2 ]: base + ((i 1 - low 1 )  n 2 + i 2 - low 2 )  w  (((i 1  n 2 ) + i 2 )  w) + (base - ((low 1  n 2 ) + low 2 )  w) c(id.place), width(id.place), limit(id.place, i)

14 Array Accesses Use inherited attributes L  id “[” Elist “]” | id Elist  Elist “,” E | E Use synthesized attributes L  Elist “]” | id Elist  Elist “,” E | id “[” E

15 Array Accesses Elist  id “[” E { Elist.place := E.place; Elist.ndim := 1; Elist.array := id.place; } Elist  Elist 1 “,” E { t := newtemp; m := Elist 1.ndim + 1; emit(t ‘:=’ Elist 1.place ‘*’ limit(Elist 1.array, m)); emit(t ‘:=’ t ‘+’ E.place); Elist.array := Elist 1.array; Elist.place := t; Elist.ndim := m; }

16 Array Accesses L  Elist “]” { L.place := newtemp; L.offset := newtemp; emit(L.place ‘:=’ c(Elist.array)); emit(L.offset ‘:=’ Elist.place ‘*’ width(Elist.array)) } L  id { L.place := id.place; L.offset := null }

17 Array Accesses E  L { if L.offset = null then E.place := L.place; else begin E.place := newtemp; emit(E.place ‘:=’ L.place ‘[’ L.offset ‘]’); end } S  L “:=” E { if L.offset = null then emit(L.place ‘:=’ E.place); else emit(L.place ‘[’ L.offset ‘]’ ‘:=’ E.place); }

18 An Example x := A[y, z] n 1 = 10, n 2 = 20, w = 4 c = base A - ((1  20) + 1)  4 = base A - 84 t1 := y * 20 t1 := t1 + z t2 := c t3 := t1 * 4 t4 := t2[t3] x := t4

19 Type Conversion E  E 1 + E 2 {E.place := newtemp; if E 1.type = int and E 2.type = int then begin emit(E.place ‘:=’ E 1.place ‘int+’ E 2.place); E.type := int; end else if E 1.type = real and E 2.type = real then begin emit(E.place ‘:=’ E 1.place ‘real+’ E 2.place); E.type := real; end else if E 1.type = int and E 2.type = real then begin u := newtemp; emit(u ‘:=’ ‘inttoreal’ E 1.place); emit(E.place ‘:=’ u ‘real+’ E 2.place); E.type := real; end else if … }

20 Flow-of-Control Statements S  if E then S 1 | if E then S 1 else S 2 | while E do S 1 | switch E begin case V 1 : S 1 case V 2 : S 2 … case V n-1 : S n-1 default: S n end

21 Conditional Statements S  if E then S 1 { E.true := newlabel; E.false := S.next; S 1.next := S.next; S.code := E.code || gen(E.true ‘:’) || S 1.code } E.code S 1.code E.true: E.false: E.true E.false

22 Conditional Statements S  if E then S 1 else S 2 { E.true := newlabel; E.false := newlabel; S 1.next := S.next; S 2.next := S.next; S.code := E.code || gen(E.true ‘:’) || S 1.code || gen(‘goto’ S.next) || gen(E.false ‘:’) || S 2.code } E.code S 1.code E.true: E.false: E.true E.false goto S.next S 2.code S.next:

23 Loop Statements S  while E do S 1 { S.begin := newlabel; E.true := newlabel; E.false := S.next; S 1.next := S.begin; S.code := gen(S.begin ‘:’) || E.code || gen(E.true ‘:’) || S 1.code || gen(‘goto’ S.begin) } E.code S 1.code E.true: E.false: E.true E.false goto S.begin S.begin:

24 Boolean Expressions E  E 1 or E 2 {E 1.true := E.true; E 1.false := newlabel; E 2.true := E.true; E 2.false := E.false; E.code := E 1.code || gen(E 1.false ‘:’) || E 2.code; } E  E 1 and E 2 {E 1.true := newlabel; E 1.false := E.false; E 2.true := E.true; E 2.false := E.false; E.code := E 1.code || gen(E 1.true ‘:’) || E 2.code; } E  not E 1 {E 1.true := E.false; E 1.false := E.true; E.code := E 1.code; }

25 Boolean Expressions E  “(” E 1 “)” { E 1.true := E.true; E 1.false := E.false; E.code := E 1.code; } E  id 1 relop id 2 { E.code := gen(‘if’ id 1.place relop.op id 2.place ‘goto’ E.true) || gen(‘goto’ E.false); } E  true { E.code := gen(‘goto’ E.true); } E  false { E.code := gen(‘goto’ E.false); }

26 An Example a < b or c < d and e < f if a < b goto Ltrue goto L1 L1: if c < d goto L2 goto Lfalse L2: if e < f goto Ltrue goto Lfalse

27 An Example Lbegin: if a < b goto L1 goto Lnext L1: if c < d goto L2 goto L3 L2: t1 := y + z x := t1 goto Lbegin L3: t2 := y - z x := t2 goto Lbegin Lnext: while a < b do if c < d then x := y + z else x := y - z

28 Case Statements Conditional goto’s – less than 10 cases Jump table – more than 10 cases – dense value range Hash table – more than 10 cases – sparse value range

29 Conditional Goto’s code to evaluate E into t goto test L1: code for S1 goto next … Ln-1: code for Sn-1 goto next Ln: code for Sn goto next test: if t = V1 goto L1 … if t = Vn-1 goto Ln-1 goto Ln next:

30 Jump Table code to evaluate E into t if t < Vmin goto Ldefault if t > Vmax goto Ldefault i := t - Vmin L := jumpTable[i] goto L

31 Hash Table code to evaluate E into t i := hash(t) L := hashTable[i] goto L

32 Procedure Calls S  call id “(” Elist “)” { for each item p on queue do emit(‘param’ p); emit(‘call’ id.place); } Elist  Elist “,” E { append E.place to the end of queue; } Elist  E { initialize queue to contain only E.place; }