CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Similar documents
Intermediate Code Generation

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

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

Chapter 6 Intermediate Code Generation

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Concepts Introduced in Chapter 6

Intermediate Representations & Symbol Tables

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

Intermediate Representations

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

6. Intermediate Representation

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

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

intermediate-code Generation

Alternatives for semantic processing

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Concepts Introduced in Chapter 6

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Intermediate Representa.on

Intermediate Code Generation

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Intermediate Representations

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Intermediate Code Generation

Three-address code (TAC) TDT4205 Lecture 16

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Intermediate Representations

Intermediate representation

6. Intermediate Representation!

Intermediate Representations

CS2210: Compiler Construction. Code Generation

CS415 Compilers. Intermediate Represeation & Code Generation

Principles of Compiler Design

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Page No 1 (Please look at the next page )

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Intermediate Representations Part II

Intermediate Representation (IR)

UNIT IV INTERMEDIATE CODE GENERATION

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

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Intermediate-Code Generat ion

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

Intermediate Representations

CSE410 aka CSE306 Software Quality

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

Intermediate Representations

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Intermediate Code Generation

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Figure 28.1 Position of the Code generator

Dixita Kagathara Page 1

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

OO software systems are systems of interacting objects.

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Recap: Functions as first-class values

Advanced C Programming

TACi: Three-Address Code Interpreter (version 1.0)

CSE 401/M501 Compilers

Formal Languages and Compilers Lecture X Intermediate Code Generation

COP5621 Exam 3 - Spring 2005

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Scope. Chapter Ten Modern Programming Languages 1

CSc 453 Intermediate Code Generation

Compiler Optimization Intermediate Representation

CMPT 379 Compilers. Parse trees

Time : 1 Hour Max Marks : 30

Functions & First Class Function Values

Winter Compiler Construction Who. Mailing list and forum

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

2.2 Syntax Definition

Three-Address Code IR

CODE GENERATION Monday, May 31, 2010

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Intermediate representations IR #1: CPS/L 3. Code example. Advanced Compiler Construction Michel Schinz

The PCAT Programming Language Reference Manual

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

tokens parser 1. postfix notation, 2. graphical representation (such as syntax tree or dag), 3. three address code

Intermediate Code Generation

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

IR Generation. May 13, Monday, May 13, 13

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Transcription:

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei

Announcements Be sure to go to recitation today - Amy has important feedback. We will institute team meetings with me to facilitate communication.

Phases of a compiler Intermediate Representation (IR): specification and generation Figure 1.6, page 5 of text

Intermediate Representations

Directed Acyclic Graph (DAG) Similar to a syntax tree No repeated nodes: structure sharing

Ex. 6.1 [p 359] a + a * ( b - c ) + ( b - c ) * d + + * a * - d a - b c b c

Ex. 6.1 [p 359] a + a * ( b - c ) + ( b - c ) * d + + * a * - d a - b c b c

Ex. 6.1 [p 359] a + a * ( b - c ) + ( b - c ) * d + + * * d a - b c

Revisiting 6.1 see construction steps in figure 6.5 [p. 360] 1 id > to ST entry for a 2 id > to ST entry for b 3 id > to ST entry for c 4-2 3 5 * 1 4 6 + 1 5 7 id > to ST entry for d 8 * 4 7 9 + 6 8

Motivating threeaddress code The DAG does not say anything about how the computation should be carried out. For example, there could be one instruction to do this computation: x+y*z as in, t 1 = x + y * z

Motivating threeaddress code In three-address code instructions can have no more than one operator on the right of an assignment. x+y*z must be broken into two instructions: t 1 = y * z t 2 = x + t 1

Three address code representation "Three-address code is a linearized representation of a DAG in which explicit names correspond to the interior nodes of the graph." [p. 363] t 1 = b - c t 2 = a * t 1 t 3 = a + t 2 t 4 = t 1 * d t 5 = t 3 + t 4 t + 3 t 4 + * t 2 * t 1 a - t 5 d b c

Three address code instructions (see 6.2.1, pages 364-5) 1. x = y op z 2. x = op y (treat i2r and r2i as unary ops) 3. x = y 4. goto L 5. if x goto L / iffalse x goto L 6. if x relop y goto L 7. function calls: - param x - call p, n - y = call p - return y 8. x = y[i] and x[i] = y 9. x = &y, x = *y, *x = y

Representation options "The description of three-address instructions specifies the components of each type of instruction, but it does not specify the representation of these instructions in a data structure." [p. 366]

Quadruples Instructions have four fields: op, arg1, arg2, result Example: t 3 = a + t 2 is represented as op arg1 arg2 result + a t 2 t 3 Example: t 4 = - c is represented as op arg1 arg2 result minus c t 4

Quadruples Identifiers would be pointers to symbol table entries. Compilerintroduced temporaries can be added to the symbol table. op arg1 arg2 result + > entry for a > entry for t 2 > entry for t 3

Triples Instructions have three fields: op, arg1, arg2 Example: t 2 = t 3 = a + t 2 is represented as line op arg1 arg2 5 computation of t 2 6 + a (5)

Indirect triples Because order matters (due to embedded references instead of explicit variables) it is more challenging to rearrange instructions with triples than with quadruples. Indirect triples allow for easier reordering (see page 369).

Static Single Assignment (SSA) an additional constraint on the three address code 1) Each variable is assigned to exactly once. x = r + 1 y = s * 2 x 1 = r + 1 y 1 = s * 2 x = 2 * x + y y = y + 1 x 2 = 2 * x 1 + y 1 y 2 = y 1 + 1

Static Single Assignment (SSA) an additional constraint on the three address code 1) Each variable is assigned to exactly once. 2) Need φ function to merge split variables: if (e) then { x = a } else { x = b } y = x With SSA: if (e) then { x 1 = a } else { x 2 = b } y = φ( x 1, x 2 )

φ function implementation In y = φ(x1,x2) simply let y, x1 and x2 be bound to the same address.

Type equivalence Name equivalence: two types are equivalent if and only if they have the same name. Structural equivalence: two types are equivalent if and only if they have the same structure. A type is structurally equivalent to itself (i.e. int is both name equivalent and structurally equivalent to int)

Name equivalence int x = 3; int y = 5; int z = x * y; The type of z is int. The type of x * y is int. The names of the types are the same, so the assignment is legal.

Structural equivalence struct S { int v; double w; }; struct T { int v; double w; }; types, names and order of fields all align int main() { struct S x; x.v = 1; x.w = 4.5; struct T y; x = y; return 0; } Under name equivalence the assignment is disallowed. Under structural equivalence the assignment is permitted. What does C do?

C does not allow the assignment bash-3.2$ gcc type.c type.c:9:5: error: assigning to 'struct S' from incompatible type 'struct T' x = y; ^ ~ 1 error generated.

Structural equivalence struct S { int v; double w; }; struct T { int a; double b; }; types and order of fields align, but names differ int main() { struct S x; x.v = 1; x.w = 4.5; struct T y; x = y; return 0; } Should this be allowed?

Consider struct Rectangular { double x; double y; }; struct Polar { double r; double theta; }; int main() { struct Rectangular p; p.x = 3.14; x.y = 3.14; struct Polar q; q = p; Should this be allowed? return 0; }

Interpretation matters polar interpretation rectangular interpretation

Our language (use name equivalence) primitive types: integer, real, Boolean, character, string user-defined types: record types have names type rec : ( real x y ; ) : ( x := 0 y := 0) array types have names type arr : 2 -> string function types have names type fun : ( real : x ) -> rec

Recursive records Recursive functions A record type must allow a component to be of the same type as the type itself: type Node: ( integer datum:=0 ; Node rest:=null )