Lecture 12 Intermediate Code Generation Translating Expressions

Slides:



Advertisements
Similar presentations
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
Advertisements

CS 31003: Compilers Introduction to Phases of Compiler.
8 Intermediate code generation
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
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.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
COP4020 Programming Languages
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction to Code Generation and Intermediate Representations
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Introduction CPSC 388 Ellen Walker Hiram College.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction Vana Doufexi office CS dept.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Compilers Design Chapter1 : Introduction, Structure of a Compiler
COMP 3002: Compiler Construction Pat Morin School of Computer Science.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
A Simple Syntax-Directed Translator
Constructing Precedence Table
8. Symbol Table Chih-Hung Wang
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler design Introduction to code generation
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
Intermediate Code Generation
Compiler Design 21. Intermediate Code Generation
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler Design 21. Intermediate Code Generation
Introduction to Compiler Construction
Presentation transcript:

Lecture 12 Intermediate Code Generation Translating Expressions Compiler Design Lecture 12 Intermediate Code Generation Translating Expressions

Phases of Compiler Source program Intermediate-code Generator Front End Lexical Analyzer (Scanner) Non-optimized Intermediate Code Tokens Back End Intermediate-code Optimizer Syntax Analyzer (Parser) Optimized Intermediate Code Parse tree Target-code Generator and Optimizer Semantic Analyzer Abstract Syntax Tree w/ Attributes Target machine code

Many Source and Target Languages - Problem Common front end S2 T2 Common back end Sn-1 Tm-1 Sn Tm How many compiler needed? N * M e.g. n= 10, m = 20  200 compiler !

Many Source and Target Languages - Solution Intermediate Language Sn-1 Tm-1 Sn Tm How many compiler needed? N (front ends) + M (back ends) e.g. n= 10, m = 20  30 compiler only

Intermediate Language (IR) Benefits Generate code for several machines One front-end Several back-ends Several high-level languages for one machine Several front-ends One back-end Intermediate language can be interpreted by small program written in machine code Example: Java and JVM Code optimization can be done in IR Written only once for all compilers The intermediate form may be more compact than machine code.

Choosing Intermediate Language (IR) Intermediate Language should be: Easy to translate from a high-level language. Easy to translate from the intermediate language to machine code. Suitable for optimization Trade of between first two contradictory issues High-level IR makes it hard for back-ends Low-level IR makes it hard for front-ends Solution use 2 IR; high-level and low-level And a translator between them

Some Intermediate Representations Abstract Syntax Tree (AST) Direct Acyclic Graph (DAG) Postfix Notation Three-Address Code The most common Set of instruction Each has at most 3 addresses in memory

The three-address code is an intermediate form, which consists of a sequence of assembly-like instructions with three operands per instruction. Each operand can act like a register.

1- Each three-address assignment instruction has at most one operator on the right side. Thus, these instructions fix the order in which operations are to be done. 2- The compiler must generate a temporary name to hold the value computed by a three-address instruction. 3-Some "three-address instructions" have fewer than three operands.

Grammar for an Example IR binary operator A transfer from memory relational operator (=, ≠ <, >, ≤ or≥ ). basics_lulu.pdf 151

Example High-level langue Translated to IR: Not optimized! z*(x-y)+w t1 := z t5 := x t6 := y t2 := t5 − t6 t3 := t1 * t2 t4 := w t0 := t3 + t4 Not optimized! No problem, can be optimized later Why? To simplify translation to IR

Translating Expressions The main complication: Expression language is tree-structured IR is flat: sequential The result of every operation should be stored in a temporary variable Every argument of operation should be stored also in a temporary variable

Syntax Directed Definition for Expression Translation done at compile-time At run-time 154 The function transop translates the name of an operator in the expression language into the name of the corresponding operator in the intermediate language.

Expression Example Idz*idx-idy Exp Exp1 - Exp2 id Exp * Exp id id t3=z .place1 = newvar() = t1, .place2 = newvar() = t2 .code1 = exp1.code(t1), .code2 = exp2.code(t2), Exp .code = [t3=z, t4=x,t1 = t3 * t4, t2=y, t0 = t1 – t2] Exp1 - Exp2 .place1 = newvar() = t3, .place2 = newvar() = t4 .code = [t2 = y] Exp * Exp id .code = [t3=z, t4=x, t1 = t3 * t4] .code = [t3 = z] .code = [t4 = x] id id Idz*idx-idy t3=z t4=x t1 = t3 * t4 t2=y t0 = t1 – t2

Translating Expression without Inherited Attributes (Three-Address Code) top denote the current symbol table Function top.get retrieves the entry Attribute E.addr denotes the address that will hold the value of E. new Temp() creates t1 , t2 ,…. gen(x '=‘ y '+' z) represents the three-address instruction x = y + z. Expressions appearing in place of variables like x, y, and z are evaluated when passed to gen Compilers Principles, Techniques, and Tools, 2-Ed - Ahuo, Ulman 379 402 Advantage: no code generated for variable access (last rule) Disadvantage: not good with side effects e.g. x-(x=3)

a = b + - c

Expression Example 3 idx-(idx=3) x*z – (x=3) Exp Exp1 - Exp2 id .place = newvar() = t0 .code1 = exp1.code = ‘’, .code2 = exp2.code = [t1=3, x = t1] Exp .code = [t1 = 3, x = t1, t0 = x – x] Exp1 - Exp2 .place = x .code = ‘’ .place = x .code = [t1=3, x = t1] id Id = Exp .place = newvar() = t1 .code = [t1 = 3] num idx-(idx=3) Always gives 0 t1 = 3 x = t1 t0 = x – x first argument takes the new value! x*z – (x=3) t1 = x * z old value, correct t2 = 3 x = t2 t0 = t1 – x new value

Expression Example 2 idx-(idx=3) Exp Exp1 - Exp2 id Id = Exp num t0 .place1 = newvar() = t1, .place2 = newvar() = t2 .code1 = exp1.code(t1), .code2 = exp2.code(t2) Exp .code = [t1=x, t3=3,x=t3,t2=x,t0=t1-t2] Exp1 - Exp2 .code = [t1 = x] .place1 = newvar() = t3 .code = [t3 = 3, x=t3, t2=x] id Id = Exp .code = [t3 = 3] num idx-(idx=3) t1=x old value t3=3 x=t3 t2=x new value t0 = t1 – t2 t1 holds the old value of x  correct

Translating Function Call

Example 4 3+f(x-y,z) Will be:

References Basics of Compiler Design, Torben Ægidius Mogensen. Published through lulu.com, 2008 Compilers: Principles, Techniques and Tools, Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. 2nd Edition, Addison-Wesley, 2007 (sections: 6.4)