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

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

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

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

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

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

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Project 3 Due October 21, 2015, 11:59:59pm

(Practice) Midterm 1

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

Lecture Compiler Middle-End

CMSC 330 Exam #2 Fall 2008

Problem Score Max Score 1 Syntax directed translation & type

CIS 500 Software Foundations Midterm I

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

CMSC 330, Fall 2018 Midterm 2

Data Flow Information. already computed

Data Flow Analysis. Program Analysis

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC430 Spring 2014 Midterm 2 Solutions

COP5621 Exam 4 - Spring 2005

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Intermediate representation

CIS 500 Software Foundations Fall September 25

More Dataflow Analysis

CSE505, Fall 2012, Midterm Examination October 30, 2012

Lecture 2. Introduction to Data Flow Analysis

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Compiler Optimization and Code Generation

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

Midterm II CS164, Spring 2006

CMSC330 Spring 2017 Midterm 2

Quiz 2 CSCI 1103 Computer Science I Honors KEY. Tuesday November 8, 2016 Instructor Muller Boston College. Fall 2016

CMSC 330, Fall 2018 Midterm 2

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Type Systems Winter Semester 2006

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

Code Optimization. Code Optimization

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Question Points Score Total 100

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Lecture 13 CIS 341: COMPILERS

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

COSE312: Compilers. Lecture 20 Data-Flow Analysis (2)

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

CSE-321 Programming Languages 2012 Midterm

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

CSE-321 Programming Languages 2010 Final

Compiler Optimization Techniques

Summary: Direct Code Generation

COS 471A,COS 471B/ELE 375 Midterm

CSE 401 Final Exam. December 16, 2010

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Control-Flow Graphs & Dataflow Analysis. CS4410: Spring 2013

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

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Dataflow analysis (ctd.)

CMSC330 Fall 2017 Final Exam

CSE-321 Programming Languages 2010 Midterm

Figure 28.1 Position of the Code generator

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The View from 35,000 Feet

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

CMSC430 Spring 2009 Midterm 2 (Solutions)

Compiler Optimisation

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Compiler Optimisation

CMSC330 Fall 2014 Midterm 2 Solutions

Metaprogramming assignment 3

Talen en Compilers. Johan Jeuring , period 2. December 15, Department of Information and Computing Sciences Utrecht University

CMSC330 Fall 2009 Final Exam

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

MIT Introduction to Dataflow Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Exercise 1 (2+2+2 points)

Live Variable Analysis. Work List Iterative Algorithm Rehashed

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

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

CS153: Compilers Lecture 15: Local Optimization

CIS 500 Software Foundations Fall October 2

Lecture 3 Local Optimizations, Intro to SSA

Partial Redundancy Analysis

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

CS 6353 Compiler Construction, Homework #3

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Programming Languages Fall 2014

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Compiler Design Spring 2017

Lecture 4 Introduction to Data Flow Analysis

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

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Declarative Intraprocedural Flow Analysis of Java Source Code

Let Arguments Go First

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

CIS 341 Final Examination 4 May 2017

Transcription:

Name: Midterm 2 CMSC 430 Introduction to Compilers Fall 2016 November 21, 2016 Instructions This exam contains 7 pages, including this one. Make sure you have all the pages. Write your name on the top of this page before starting the exam. Write your answers on the exam sheets. If you finish at least 15 minutes early, bring your exam to the front when you are finished; otherwise, wait until the end of the exam to turn it in. Please be as quiet as possible. If you have a question, raise your hand. If you feel an exam question assumes something that is not written, write it down on your exam sheet. Barring some unforeseen error on the exam, however, you shouldn t need to do this at all, so be careful when making assumptions. Question Score Max 1 20 2 20 3 30 4 20 5 10 Total 100 1

Question 1. Short Answer (20 points). a. (5 points) Briefly describe what an intermediate representation is. Answer: An intermediate representation is a program representation that is at a level of abstraction in between the program source code/abstract syntax tree and the compiler output (typically bytecode or machine code). b. (5 points) List three things that might be stored in an activation record or stack frame. Answer: Parameters, saved registers, the return value, the return address, local variables and temporaries. 2

c. (5 points) Write down the data-flow equations for a backward may analysis, i.e., define in(s) and out(s) for a statement s in terms of gen(s) and kill(s). Write pred(s) for the predecessor of s and succ(s) for the successor of s. in(s) = out(s) = d. (5 points) Briefly explain what it means for a type system to be sound. (Do not simply refer to progress and preservation define soundness in more direct terms.) Answer: Soundness means that a well-typed program either evaluates forever ( diverges ) or eventually reduces to a value. In other words, no mater how long a well-typed program runs, it will never get stuck in the operational semantics sense. 3

Question 2. Code Generation (20 points). Below is part of the RubeVM instruction set, followed by a type representing expression ASTs for a small language. type reg = [ Reg of int ] type value = [ Int of int Str of string Id of string ] type id = [ Id of string ] type instr = I const of reg value ( dst, src ) I mov of reg reg ( dst, src ) I add of reg reg reg ( dst, src1, src2 ) I sub of reg reg reg ( dst, src1, src2 ) I mul of reg reg reg ( dst, src1, src2 ) I div of reg reg reg ( dst, src1, src2 ) I eq of reg reg reg ( dst, src1, src2 ) I lt of reg reg reg ( dst, src1, src2 ) I leq of reg reg reg ( dst, src1, src2 ) I jmp of int ( offset ) I if zero of reg int ( src, offset ) type expr = EInt of int EAdd of expr expr EIfPos of expr expr expr The EInt and EAdd forms are standard. The form EIfPos(e1, e2, e3) evaluates e1. Then if e1 is positive, it evaluates to e2; otherwise it evaluates to e3. For example, EIfPos(5, 1, 2) evaluates to 1, and EIfPos(0, 1, 2) and EIfPos( 5, 1, 2) both evaluate to 2. Write a function comp expr : expr > reg (instr list ) that returns a list of instructions that, when run on RubeVM, will compute the expression s value and store it in the returned reg. Note you may not evaluate the expression as part of code generation, i.e., you have to write a compiler, not an interpreter. You may write as many helper functions as you need. Your code should be self-contained, e.g., if you need next reg you need to implement it, though you may use OCaml standard library functions. Answer: let next reg = let n = ref 0 in fun () > (let temp =!n in n:=!n+1; Reg temp) let rec comp expr = function EInt n > let r = next reg () in (r, [ I const ( Reg r, Int n)]) EAdd (e1, e2) > let r = next reg () in let (r1, p1) = comp expr e1 in let (r2, p2) = comp expr e2 in (r, p1 @ p2 @ [I add (r, r1, r2 )]) EIfPos (e1, e2, e3) > let (r1, p1) = comp expr e1 in let (r2, p2) = comp expr e2 in let (r3, p3) = comp expr e3 in let r = next reg () in (r, p1 @ [ I const (r, Int 0); I leq (r, r1, r ); ( ((r = 1) (r1 0)) and ((r = 0) (r1 > 0)) ) I if zero (r, (2 + (List. length p3)))] @ p3 @ [I mov (r, r3 ); I jmp (1 + (List. length p2))] @ p2 @ [I mov (r, r2 )] ) 4

Question 3. Data flow analysis (30 points). In the following table, show each iteration of available expressions for the control-flow graph on the right. For each iteration, list the statement taken from the worklist in that step, the value of out computed for that statement, and the new worklist at the end of the iteration. You may or may not need all the iterations; you may also add more iterations if needed. Do not add the entry node to the worklist. Note that we branch directly from assignment statements in this CFG to keep the problem shorter. 0. entry 1. x = a + b 2. y = 3 3. t = a + y 7. z = a + y Use for the set of no expressions, and for the set of all expressions. What is? = 4. a = a + 1 5. t = a * b 6. x = a + b 8. exit What are the initial out s for each statement? Stmt 0 1 2 3 4 5 6 7 8 Initial out Iteration 0 1 2 3 4 Stmt taken from worklist out of taken stmt N/A 1 N/A a+b New worklist 1,2,3,4,5,6,7,8 2,3,4,5,6,7,8 Iteration 5 6 7 8 9 Stmt taken from worklist out of taken stmt New worklist Iteration 10 11 12 13 14 Stmt taken from worklist out of taken stmt New worklist 5

Question 4. Optimization (20 points). Fill in the following table so that, from left to right, each row shows a sequence of two optimizations applied to a program written in RubeVM bytecode. Original code const r0, 10 const r1, 20 add r2, r0, r1 mov r3, r2 add r4, r2, r3 (only r4 accessed below here) After copy propagation After dead code elimination Original code After constant folding rd glob r0, x const r1, 5 const r2, 10 add r3, r2, r0 const r4, 10 add r5, r2, r0 After common subexpr. elim. Original code rd glob r0, y const r1, 0 rd glob r2, z if zero r2, 3 add r3, r0, r1 sub r2, r2, r3 jmp -4 After algebraic simplification After loop inv. code motion 6

Question 5. Type Systems (10 points). Here is the simply typed lambda calculus with integers. e ::= v x e e v ::= n λx: t.e t ::= int t t A ::= x : t, A Int A n : int Var A x : A(x) Lam x: t, A e : t A λx: t.e : t t App A e 1 : t t A e 2 : t A e 1 e 2 : t a. (8 points) Draw a derivation showing that the following term is well-typed in the empty type environment, where we use i instead of int to save writing. You need not label the uses of the rules with their names. (λf: i i i.f 3) (λx: i.λy: i.x) Answer: f: i i i f : i i i f: i i i 3 : i y: i, x: i x : i f: i i i (f 3) : i i (λf: i i i.f 3) : (i i i) (i i) x: i λy: i.x : i i (λx: i.λy: i.x) : i i i (λf: i i i.f 3) (λx: i.λy: i.x) : i i b. (2 points) Write down an expression that, according to the type rules above, has type (i i) i. Answer: There are many possible answers such as λx: i i.x 3. 7