Intermediate code generation

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
CSC 8505 Compiler Construction Intermediate Representations.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
7/15/2015\course\cpeg621-10F\Topic-1a.ppt1 Intermediate Code Generation Reading List: Aho-Sethi-Ullman: Chapter 2.3 Chapter 6.1 ~ 6.2 Chapter 6.3 ~ 6.10.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
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.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
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
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
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,
Introduction to Code Generation and Intermediate Representations
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Prepared By: Abhisekh Biswas - 04CS3002 Intermediate Code Generation.
Code Generation CPSC 388 Ellen Walker Hiram College.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Lecture 12 Intermediate Code Generation Translating Expressions
CS 404 Introduction to Compiler Design
COMPILER CONSTRUCTION
Intermediate code Jakub Yaghob
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Compilers.
Compiler Construction
Intermediate Code Generation
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 Generations for Boolean Functions
Intermediate Code Generation
Unit IV Code Generation
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Compiler Construction
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine 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.
Presentation transcript:

Intermediate code generation

Intermediate code generation Translating source program into an “intermediate language.” Simple CPU Independent, Benefits Retargeting is facilitated Machine independent Code Optimization can be applied.

Types of Intermediate languages Intermediate language can be many different languages, and the designer of the compiler decides this intermediate language. syntax trees postfix notation three-address code

Syntax Trees a + a * ( b - c ) + ( b - c ) * d Syntax Tree DAG

a:=b*-c+b*-c Syntax Tree DAG assign assign + a + a * * * uminus b

2. Postfix Notation Form Rules: 1. If E is a variable/constant, the PN of E is E itself 2. If E is an expression of the form E1 op E2, the PN of E is E1 ’E2 ’op (E1 ’ and E2 ’ are the PN of E1 and E2, respectively.) 3. If E is a parenthesized expression of form (E1), the PN of E is the same as the PN of E1.

Example (a+b)/(c-d) Postfix: ab+cd-/

3. Three address code Statements of general form x:=y op z No built-up arithmetic expressions are allowed. As a result, x:=y + z * w should be represented as t1:=z * w t2:=y + t1 x:=t2

Example t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5 a:=b*-c+b*-c t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5

Types of Three-Address Statements Assignment Statement: x:=y op z Assignment Statement: x:=op z Copy Statement: x:=z Unconditional Jump: goto L Conditional Jump: if x relop y goto L Stack Operations: Push/pop More Advanced: Procedure: param x1 param x2 … param xn call p,n Index Assignments: x:=y[i] x[i]:=y Address and Pointer Assignments: x:=&y x:=*y *x:=y

Implementations of 3-address statements Quadruples Triples Indirect triples

Quadruples op arg1 arg2 result (0) uminus c t1 (1) * b t2 (2) (3) t3 (4) + t5 (5) := a a:=b*-c+b*-c t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5 Temporary names must be entered into the symbol table as they are created.

Triples a:=b*-c+b*-c op arg1 arg2 (0) uminus c (1) * b (2) (3) (4) + (5) assign a a:=b*-c+b*-c t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5 Temporary names are not entered into the symbol table.

Other types of 3-address statements e.g. ternary operations like x[i]:=y x:=y[i] require two or more entries. e.g. op arg1 arg2 (0) [ ] = x i (1) assign y op arg1 arg2 (0) [ ] = y i (1) assign x

Indirect Triples a:=b*-c+b*-c op (0) (14) (1) (15) (2) (16) (3) (17) (4) (18) (5) (19) op arg1 arg2 (14) uminus c (15) * b (16) (17) (18) + (19) assign a t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5

Assignment Statements S -> id := E { ptr := lookup(id.name); if ptr <> nil then emit(ptr ‘:=‘ E.place) else error} E -> E1 + E2 { E.place := newtemp; emit(E.place ‘:=‘ E1.place ‘+’ E2.place) } E -> E1 * E2 { E.place := newtemp; emit(E.place ‘:=‘ E1.place ‘*’ E2.place) } E -> - E1 { E.place := newtemp; emit(E.place ‘:=‘ ‘uminus’ E1.place)} E -> ( E1 ) { E.place = E1.place } E -> id { ptr := lookup (id.name); E.place = ptr;

Reusing temporaries A simple algorithm: 􀂓 Say we have a counter c, initialized to zero 􀂓 Whenever a temporary name is used, decrement c by 1 􀂓 Whenever a new temporary name is created, use $c and increment c by 1 E.g.: x := a*b + c*d – e*f Statement Value of C ---------------------------------------------- $0 := a*b ; 1 (c incremented by 1) $1 := c*d ; 2 (c incremented by 1) $0 := $0 + $1 ; 1 (c decremented twice, incremented once) $1 := e * f ; 2 (c incremented by 1) $0 := $0 -$1 ; 1 (c decremented twice, incremented once) x := $0 ; 0 (c decremented once)