Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

Similar documents
Intermediate representation

CMPSC 160 Translation of Programming Languages. Three-Address Code

Syntax-Directed Translation

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

CFG (Control flow graph)

Module 25 Control Flow statements and Boolean Expressions

CS 6353 Compiler Construction, Homework #3

Intermediate Code Generation

Intermediate Representations

CSc 453 Intermediate Code Generation

Decision Making in C

Intermediate Representation (IR)

CSCI Compiler Design

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

CS2210: Compiler Construction. Code Generation

Intermediate Code Generation

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Formal Languages and Compilers Lecture X Intermediate Code Generation

6. Intermediate Representation!

CSCI565 Compiler Design

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

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

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

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

CSCI Compiler Design

Lecture 3 Local Optimizations, Intro to SSA

Intermediate Code Generation

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

CSCI565 Compiler Design

6. Intermediate Representation

Intermediate Code Generation

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

Compiler Construction 2016/2017 Loop Optimizations

Lecture 4. More on Data Flow: Constant Propagation, Speed, Loops

What If. Static Single Assignment Form. Phi Functions. What does φ(v i,v j ) Mean?

A Practical and Fast Iterative Algorithm for φ-function Computation Using DJ Graphs

Intermediate Code Generation

Control flow graphs and loop optimizations. Thursday, October 24, 13

Intermediate Code Generation

Compiler Construction 2010/2011 Loop Optimizations

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

8. Static Single Assignment Form. Marcus Denker

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

CSCI Compiler Design

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

CS293S Redundancy Removal. Yufei Ding

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

Programming Language Processor Theory

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

Introduction to Programming Using Java (98-388)

THEORY OF COMPILATION

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Lecture 23 CIS 341: COMPILERS

Midterm II CS164, Spring 2006

Syntax-Directed Translation Part II

Syntax Directed Translation

Compiler Construction 2009/2010 SSA Static Single Assignment Form

ECS 142 Project: Code generation hints (2)

Induction Variable Identification (cont)

This test is not formatted for your answers. Submit your answers via to:

Intermediate Code Generation

Tour of common optimizations

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Lecture Notes on Loop-Invariant Code Motion

CS415 Compilers. Intermediate Represeation & Code Generation

CS 406/534 Compiler Construction Putting It All Together

Principles of Compiler Design

intermediate-code Generation

Dixita Kagathara Page 1

Lab Work Playing with Dot 1

Introduction to Machine-Independent Optimizations - 6

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

CSE302: Compiler Design

Example. Example. Constant Propagation in SSA

Lecture Notes on Intermediate Representation

CS4215 Programming Language Implementation

Combining Analyses, Combining Optimizations - Summary

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

SCITL: Side Channel Impedance Through Linearization. Sahil Madeka, Nitish Paradkar, Nicholas Rifel, Zelalem Aweke, Fangzhou Xing

Common Problems on Homework

Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc.

Problem Score Max Score 1 Syntax directed translation & type

EECS 583 Class 2 Control Flow Analysis LLVM Introduction

COP5621 Exam 4 - Spring 2005

CS 314 Principles of Programming Languages. Lecture 3

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

Alternatives for semantic processing

Homework I - Solution

CS153: Compilers Lecture 23: Static Single Assignment Form

Computational Physics - Fortran February 1997

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

CS 314 Principles of Programming Languages

Time : 1 Hour Max Marks : 30

Compilers. Compiler Construction Tutorial The Front-end

Module 26 Backpatching and Procedures

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

Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

Loops. Lather, Rinse, Repeat. CS4410: Spring 2013

Transcription:

Homework 1 Answers CS 322 Compiler Construction Winter Quarter 2006 Problem 1 m := 0 i := 0 L1: if i<n == false goto L2 s := 0 j := i L3: if j<n == false goto L4 x := j s := s+x if s>m == false goto L5 m := s L5: // end of if-statement j := j+1 goto L3 L4: // end of inner for-loop i := i+1 goto L1 L2: // end of outer for-loop return m The reason some labels are on lines of their own is to emphasize the fact that they are not generated at the same time as the code that follows them. Think about how an IR code generator would work. Given a for-statement, it would first emit code for the initializer (i:=0). Then, it would generate a label (L1:) and emit the condition (if i<n==false). If the condition is false, control will continue at some other point, so it should generate a label for that deatination and emit a goto statement (goto L2). Then, go on to generate code for the body of the loop. At the end of that, place the destination label (L2:). What comes next is the code for the statement following the loop and will be generated separately. See the SDT for the while-loop in problem 2 for more info. This is very similar to what you will have to do in PA4. The first step in the SSA conversion is to find out in which blocks each variable is defined and insert a φ-function in the dominance frontiers of those blocks. The φ functions are also definitions of the variable. This information is shown in figure 2.

entry B1 m := 0 i := 0 B1 B2 B2 i < n B2 B3 s := 0 j := i return m B9 B2 B3 B9 B4 j < n exit B4 B2, B4 B5 i := i+1 x := j s := s+x s > m B6 B2 B5 B6 B4 B7 m := s B8 j := j+1 B8 B7 B8 B4 Figure 1: The CFG and dominator tree. The nodes in the dominator tree are labeled with their dominance frontiers. Variable BB where it is defined BB with phi functions m B1, B7, B8, B4, B2 B8, B4, B2 i B1, B5, B2 B2 s B3, B2, B6, B4, B2, B4 j B3, B2, B8, B4 B2, B4 x B6, B4, B2 B4, B2 Figure 2: Finding where φ functions should be placed

After inserting the φ functions, we need to rename. The final result is shown in figure 3. entry B4 B3 B5 i2 := i1+1 m0 := 0 i0 := 0 m1 := (m2, m0) i1 := (i2, i0) s0 := (s2, s#) j0 := (j2, j#) x0 := (x1, x#) i1 < n# s1 := 0 j1 := i1 m2 := (m1, m4) s2 := (s1, s3) j2 := (j1, j3) x1 := (x0, x2) j2 < n# B7 m3 := s3 B8 B1 B6 x2 := j2 s3 := s2+x1 s3 > m2 m4 = (m3, m2) j3 := j2+1 B2 B9 return m1 exit m0 := 0 i0 := 0 m1 := m0 i1 := i0 s0 := s# j0 := j# x0 := x# L1:if i1<n# == false goto L2 s1 := 0 j1 := i1 m2 := m1 s2 := s1 j2 := j1 x1 := x0 L3: if j2<n# == false goto L4 x 2:= j2 s3 := s2+x1 m4 := m2 if s3>m2 == false goto L5 m3 := s3 m4 := m3 L5:j3 := j2+1 m2 := m4 s2 := s3 j2 := j3 x1 := x2 goto L3 L4:i2 := i1+1 m1 := m2 i1 := i2 s0 := s2 j0 := j2 x0 := x1 goto L1 L2:return m1 Figure 3: Final SSA Form and conversion back to MIR Note how each argument i of a φ function is taken from the corresponding predecessor i as ordered in the figure. Furthermore, n doesn t have an initial definition, so we use the pound symbol as a subscript. The conversion from SSA back to MIR is shown in figure 3. m4 := m2 provides an interesting challenge. Technically, it should be at the end of the block, right after the evaluation of s3>m2 and before the goto. Nevertheless, its current location is safe. A better representation would have created a temporary variable to hold the result of the test and then used that variable in a branching statement.

Problem 2 In the SDT that follows, stands for concatenate and newt emp() is a function that generates a new temporary variable every time it is called. The SDT follows. < S > while < E > do < S 1 > S.begin = genlabel() S.end = genlabel() S.code = S.begin : E.code if E.temp == FALSE goto S.end S 1.code goto S.begin S.end : < S > if < E > then < S 1 > S.end = genlabel() S.code = E.code if E.temp == FALSE goto S.end S 1.code S.end : < S > < S 1 > ; < S 2 > S.code = S 1.code S 2.code < S > < V > := < E > S.code = E.code V.name := E.temp < E > < E 1 > == < E 2 > E.temp = newtemp() E.code = E 1.code E2.code E.temp := E 1.temp == E 2.temp

< E > < E 1 > + < E 2 > E.temp = newtemp() E.code = E 1.code E 2.code E.temp := E 1.temp + E 2.temp < E > id E.temp = id.name E.code = Now let us consider the addition of a break statement and how this will affect the code. A sample syntax tree for a possible nested-loop configuration is shown in figure 4. while loop S1 S1.end E1 S2 S3 while loop S4 E2 S5 S6 S7 break break Figure 4: The blue arrows show how the value of attribute S 1.end needs to travel in the parse tree The first break statement (S5) should generate a branch just outside loop S3. The label used in this goto statement is S3.end. Things become more interesting when we consider the other break (S6). This should generate a goto S1.end. The label S1.end was created when we started generating code for S1, yet now we need it in S6. This piece of information needs to pass from S1 to S2 to S4 to S6. Clearly, it s an inherited attribute that will have to be passed along all kinds of statements that may consist of other statements (eg. S S S). Below are the rules we need to add to the existing SDT:

< S > while < E > do < S 1 > S 1.in = S.end < S > if < E > then < S 1 > S 1.in = S.in < S > < S 1 > ; < S 2 > S 1.in = S.in S 2.in = S.in < S > break S.code = goto S.in