CMSC430 Spring 2014 Midterm 2 Solutions

Similar documents
Problem Score Max Score 1 Syntax directed translation & type

CMSC430 Spring 2009 Midterm 2 (Solutions)

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

COP5621 Exam 4 - Spring 2005

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

LECTURE 3. Compiler Phases

Recap: Printing Trees into Bytecodes

Intermediate representation

EXAMINATIONS 2008 MID-YEAR COMP431 COMPILERS. Instructions: Read each question carefully before attempting it.

A Simple Syntax-Directed Translator

A Tour of Language Implementation

Compiler construction 2009

CSCE 314 Programming Languages. Type System

Syntax-Directed Translation. Lecture 14

Compilers. Compiler Construction Tutorial The Front-end

LECTURE 18. Control Flow

Topics. Structured Computer Organization. Assembly language. IJVM instruction set. Mic-1 simulator programming

Project 2 Interpreter for Snail. 2 The Snail Programming Language

1 Lexical Considerations

Principles of Compiler Design

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

CS 536 Midterm Exam Spring 2013

Intermediate Code Generation

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 11, 2015

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS5363 Final Review. cs5363 1

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

The Structure of a Syntax-Directed Compiler

CMSC330 Fall 2014 Midterm 2 Solutions

Introduction to Programming Using Java (98-388)

CSE 431S Code Generation. Washington University Spring 2013

Midterm II CS164, Spring 2006

CMSC330 Spring 2014 Midterm 2 Solutions

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler construction 2009

Introduction to Parsing. Lecture 8

COMP3131/9102: Programming Languages and Compilers

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

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

Intermediate Representations

Translating JVM Code to MIPS Code 1 / 43

When do We Run a Compiler?

Syntax-Directed Translation Part I

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

MIDTERM EXAM (Solutions)

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

CSE302: Compiler Design

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Syntax and Grammars 1 / 21

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 432 Fall Mike Lam, Professor. Code Generation

Java: framework overview and in-the-small features

Midterm 2. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: April 18, 2012

Intermediate Representation (IR)

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Static Program Analysis

CSE 431S Final Review. Washington University Spring 2013

CSE 401 Final Exam. December 16, 2010

Tutorial 3: Code Generation

Concepts Introduced in Chapter 6

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CSE 401/M501 Compilers

CSC 4181 Handout : JVM

TACi: Three-Address Code Interpreter (version 1.0)

JoeQ Framework CS243, Winter 20156

ELEC 876: Software Reengineering

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

CSc 453 Interpreters & Interpretation

Part VII : Code Generation

MIDTERM EXAMINATION - CS130 - Spring 2003

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CSCI565 Compiler Design

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

G Programming Languages - Fall 2012

Alternatives for semantic processing

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Exercise 7 Bytecode Verification self-study exercise sheet

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

COP5621 Exam 3 - Spring 2005

Comments. Comments: /* This is a comment */

Languages and Compiler Design II IR Code Generation I

CS 139 Practice Midterm Questions #2

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

The role of semantic analysis in a compiler

Intermediate Code Generation

CFG (Control flow graph)

Control Flow Analysis

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

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CSCI565 Compiler Design

Foundations: Syntax, Semantics, and Graphs

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Lecture Compiler Middle-End

Transcription:

CMSC430 Spring 2014 Midterm 2 Solutions 1. (12 pts) Syntax directed translation & type checking Consider the following grammar fragment for an expression for C--: exp CONST IDENT 1 IDENT 2 [ exp 1 ] Assume that the nonterminal exp and terminals IDENT and CONST have an attribute type that can be assigned a range of type values, including typeboolean, typeint, typeerror, etc. In addition, the terminal IDENT has an attribute isarray that can be true or false. Add syntax-directed type checking rules to the production exp IDENT 2 [ exp 1 ] to enforce the following: a. (2 pts) IDENT 2 must be an array. Otherwise call parser.msg( Misuse of array ). b. (2 pts) The subscript expression exp 1 must be typeint. Otherwise call parser.msg( Integer required ). c. (2 pts) The expression type is assigned the type of IDENT 2 if both (a) and (b) are true, else it is assigned typeerror. Answer: exp IDENT 2 [ exp 1 ] {: exp.type = IDENT 2.type; if (!IDENT 2.isArray) { parser.msg( Misuse of array ); exp.type = typeerror; } if (exp 1.type!= typeint) { parser.msg( Integer required ); exp.type = typeerror; } :}

d. (6 pts) Given the parse tree for x[y[2]], where x is a BOOL array, y is an INT array, and 2 is an INT, show how syntax directed translation can calculate the type of the expression using your type checking rules by annotating the parse tree. Show the value calculated for each attribute, and draw arrows linking the attributes used in the calculation. 2. (4 pts) Symbol tables Show the state of lexically nested symbol tables when the compiler reaches the point marked HERE in the following code fragment: int a,b; void foo( ) { int b; { int c;} } void bar( ) { int a, { int d,e;} /* HERE */ }

3. (12 pts) Intermediate Representations Translate the following arithmetic expression: x = b ( a * a ) (2 pts) abstract syntax tree (AST) (2 pts) directed acyclic graph (DAG) (4 pts) 3-address code load R1 b load R2 a mult R3 R2 R2 sub R4 R1 R3 store x R4 (4 pts) Java stack code iload index(b) iload index(a) iload index(a) // or dup imult isub istore index(x)

if stack is X Y ( Y is at top of stack ) then if_icmpeq L pop X, Y from stack, jump to L if X = Y if_icmpgt L pop X, Y from stack, jump to L if X > Y if_icmplt L pop X, Y from stack, jump to L if X < Y 4. (26 pts) Code generation. Your compiler generates Java byte codes using attributes (code) and helper functions (geninst, append) found in the table on previous page. Add support in your compiler for a Ruby style STEP loop, where x.step(y,z) { } will iterate with index starting at x, incrementing index by z each iteration, and repeating while index y. Note the loop will not be executed if x > y. Write syntax-directed actions needed to generate code for a Ruby style STEP loop: stmt exp 1. STEP ( exp 2, exp 3 ) { stmtlist } {: stmt.code =?? :} 3.step(2,2) { puts x } 3.step(3,2) { puts x } x 3.step(4,2) { puts x } x 3.step(5,2) { puts x } xx 3.step(6,2) { puts x } xx stmt exp 1. STEP ( exp 2, exp 3 ) { stmtlist } {: Handle h1 = geninst( NOP ); // end of STEP loop Handle h2 = geninst( NOP ); // beginning of STEP loop stmt.code = append( exp 1.code, // 1 copy of index on stack h2, geninst( DUP() ), // 2 copies of index on stack exp 2.code, // exp 2 = upper bound geninst( ICMPGT( h1 ) ), // branch if index > exp 2 exp 3.code, // exp 3 = step geninst( IADD () ), // increment index by exp 3 stmtlist.code, // body of loop geninst( GOTO( h2 ) ), h1, geninst( POP( ) ) ); // pop last copy of index :}

5. (10 pts) Control flow analysis Consider the following code: <I1> a := b <I2> L1: b := c <I3> L2: if (...) goto L4 <I4> c := b <I5> L3: d := a <I6> L4: goto L1 <I7> L5: b := a <I8> L6: if (...) goto L2 <I9> c := a a. (8 pts) Find basic blocks and draw the control flow graph (CFG). b. (2 pts) What is the compiler term used to describe statement I7, I8, and I9? Unreachable / dead code. 6. (12 pts) CFG ordering, dominators, loops Consider the following control flow graph (CFG), where B1 is the start of the program. a. (2 pts) List all basic blocks dominated by B3. B3, B5. b. (2 pts) List all basic blocks dominated by B4. B4, B6. c. (4 pts) List all the loops in the control flow graph. For each loop list all basic blocks in the loop, as well any back edges in the loop. The only loop is B4, B6, with loop back edge B6->B4. Other cycles at B2 and B3 are not loops since no node dominates all other nodes in the loop (can enter loop bypassing loop header). d. (4 pts) Give TWO reverse Postorder numberings of the CFG for Problem 7 on the next page. Possible rpostorder traversals: 1,2,3,5,4,6 1,2,3,5,6,4 1,2,5,6,3,4 B1 I1: a = B2 I2: c = a I3: if ( ) goto L4 B3 I4: a = b B5 I7: b = a I8: if ( ) goto L2 B4 I5: b = a+c I6: goto L1 B6 I9: a = b

B1 I1: a = 7. (24 pts) Dataflow analysis Calculate Live Variables for the following code: <I1> a := <I2> L1: c := a <I3> if (...) goto L4 <I4> a := b <I5> L2: b := a+c <I6> goto L1 <I7> L4: b := a <I8> if (...) goto L2 <I9> a := b a. (8 pts) Calculate GEN/KILL for each basic block b. (16 pts) Initializing IN/OUT to, solve live variables in the order B6, B5, B4, B3, B2, B1, showing IN/OUT for each pass. Stop when a fixed point is reached. Basic GEN KILL Block B1 a B2 a c B3 b a B4 a, c b B5 a b B6 b a Basic Block B6 B5 B4 B3 B2 B1 B3 B2 I4: a = b B4 I2: c = a I3: if ( ) goto L4 B5 I5: b = a+c I6: goto L1 Initial Pass 1 Pass 2 Pass 3 Pass 4 OUT IN b b b OUT b a, b, c a, b, c IN a a, c a, c OUT a, b a, b IN a, c a, c a, c OUT a, c a, c a, c IN b, c b, c b, c OUT a, b, c a, b, c a, b, c IN a, b a, b a, b OUT a, b a, b a, b IN b b b I7: b = a I8: if ( ) goto L2 B6 I9: a = b