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

Similar documents
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 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 21, 2016

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

(Practice) Midterm 1

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

Typed Lambda Calculus and Exception Handling

CMSC330 Spring 2017 Midterm 2

CSE505, Fall 2012, Midterm Examination October 30, 2012

CMSC 330 Exam #2 Fall 2008

Midterm 2 Sample solution

CMSC 330: Organization of Programming Languages. Operational Semantics

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.

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

CSE-321 Programming Languages 2010 Final

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

Mutable References. Chapter 1

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

Java: framework overview and in-the-small features

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

JAM 16: The Instruction Set & Sample Programs

CIS 341 Midterm March 2, 2017 SOLUTIONS

Programming Languages Lecture 15: Recursive Types & Subtyping

Lecture 13: Subtyping

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

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Compiling Techniques

Compiler construction 2009

CMSC330 Fall 2009 Final Exam

Harvard School of Engineering and Applied Sciences Computer Science 152

CMSC330 Fall 2017 Final Exam

Programming Languages Lecture 14: Sum, Product, Recursive Types

CMSC330. Objects, Functional Programming, and lambda calculus

Question Points Score Total 100

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

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

Exam 3 Chapters 7 & 9

CMSC430 Spring 2014 Midterm 2 Solutions

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

Type Checking and Type Inference

Exercise 7 Bytecode Verification self-study exercise sheet

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CMSC 430 Introduction to Compilers. Spring Register Allocation

COP5621 Exam 4 - Spring 2005

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Java byte code verification

A Tour of Language Implementation

CS 415 Midterm Exam Spring 2002

Lecture 13 CIS 341: COMPILERS

Dynamic Types, Concurrency, Type and effect system Section and Practice Problems Apr 24 27, 2018

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Programming Languages

Compiler Construction I

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Midterm II CS164, Spring 2006

Qualifying Exam in Programming Languages and Compilers

CSE413 Midterm. Question Max Points Total 100

CMSC 330: Organization of Programming Languages. Lambda Calculus

FORM 2 (Please put your name and form # on the scantron!!!!)

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

Static Program Analysis

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

CMSC 330: Organization of Programming Languages

Operational Semantics of Cool

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

CIS 341 Final Examination 3 May 2011

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

Simply-Typed Lambda Calculus

COMP-520 GoLite Tutorial

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Java Class Loading and Bytecode Verification

CSE 401 Final Exam. December 16, 2010

CMSC 330, Fall 2018 Midterm 2

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Compilers: The goal. Safe. e : ASM

CMSC330 Fall 2017 Final Exam Solution

Sample Final Exam Questions

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

CMSC 330: Organization of Programming Languages

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

Recursive Types and Subtyping

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

cs173: Programming Languages Final Exam

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

Lambda Calculus and Type Inference

CSE-321 Programming Languages 2014 Final

CMSC 330: Organization of Programming Languages

Compilers and computer architecture Code-generation (2): register-machines

CIS 120 Midterm II March 29, 2013 SOLUTIONS

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

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

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CMSC 330: Organization of Programming Languages. Lambda Calculus

CSE 351 Midterm - Winter 2017

Transcription:

Name: Midterm 2 CMSC 430 Introduction to Compilers Fall 2014 November 19, 2014 Instructions This exam contains 10 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 25 2 25 3 10 4 25 5 15 Total 100 1

Question 1. Short Answer (25 points). a. (5 points) Briefly explain what a virtual method table (or vtable) is and what it s used for. It is a collection of methods for a particular class. Each instance of a class has a pointer to the virtual method table for that class. When one of an object s instance methods is invoked, it is resolved by looking it up in the virtual method table. b. (5 points) List three potential goals of optimization in a compiler. Improve running time, decrease memory usage, reduce code size. Other reasonable answers were also acceptable. 2

c. (5 points) Briefly explain what the progress theorem is. A well-typed term is either a value, or it can be reduced a step. d. (5 points) What do mutation and crossover have to do with automated program repair, as discussed in class? Explain very briefly. The automated program repair algorithm presented used mutation (changing a statement in the program) and crossover (combining together two potential repaired programs) to generate candidate repaired programs. 3

e. (5 points) Briefly explain what an activation record is and list 3 items in an activation record. An activation record is a stack frame that contains information about a function invocation, including the parameters, return address, local variables, space to temporarily save registers, and possibly the caller s activation record pointer. 4

Question 2. Type Systems (25 points). a. (8 points) Assume that int < float. Write down every type t such that t int float float, following standard subtyping rules. int float int int float float float float int float float float b. (2 points) Assume that int < float. Write down every type t such that t int ref float ref, following standard subtyping rules. int ref float ref 5

c. (10 points) Fill in the following table with either an untyped (i.e., no type parameter annotations) lambda calculus term (on the left) or its corresponding type according to the type inference algorithm we saw in class (on the right). Term Type 3 int λx.x α α λx.λy.y α β β λx.λy.y x α (α β) β λx.x 3 (int α) α d. (5 points) Recall the simply typed lambda calculus: e ::= n x λx: t.e e 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 Draw a derivation that the following type judgment holds, where A = +: int int int. (You can draw the derivation upward from the judgment, and you can write i instead of int to save time): x: int, A + : int int int x: int, A x : int x: int, A + x : int int A (λx: int. + x) : int int int A (λx: int. + x) 1 : int int A 1 : int A (λx: int. + x) 1 : int int 6

Question 3. Interpreter Implementation (10 points). Below is a snippet of the bytecode interpreter code from 06-codegen-2.ml. type src = [ Const of int Ptr of int ] type reg = [ Reg of int ] type dst = [ Ptr of int ] type symtbl = ( string int ) list type heap = (int, int ) Hashtbl. t type regs = (int, int ) Hashtbl. t type instr = ILoad of reg src ( dst, src ) IStore of dst reg ( dst, src ) IAdd of reg reg reg ( dst, src1, src2 ) IMul of reg reg reg ( dst, src1, src2 ) IIfZero of reg int ( guard, target ) IJmp of int ( target ) IMov of reg reg ( dst, src ) let rec run instr (h:heap) ( rs : regs) = function IAdd ( Reg r1, Reg r2, Reg r3) Hashtbl. replace rs r1 ((Hashtbl. find rs r2) + (Hashtbl. find rs r3 )); None IIfZero ( Reg r, n) if (Hashtbl. find rs r) = 0 then Some n else None... Suppose we make the unfortunate decision to modify our bytecode language to have a special undefined value, like JavaScript. We begin by introducing a new type, intorundef, to stand for either the undefined value or an integer; we add a new instruction, IIfUndef (r, n); and we adjust the types src, heap, and regs appropriately: type intorundef = Undef Int of int type instr =... IIfUndef of reg int type src = [ Const of intorundef Ptr of int ] type heap = (int, intorundef) Hashtbl. t type regs = (int, intorundef) Hashtbl. t The desired semantics is as follows: IIfUndef (r,n) branches by n if r contains Undef, otherwise it falls through. If Undef if used as either argument to addition, the result should be Undef. If Undef is used as the guard of IIfZero, it should be treated as false (i.e., as a non-zero value). Rewrite the cases in run instr for IIfUndef, IAdd, and IIfZero to implement this semantics. You can write a helper function if you want. You do not need to implement any other parts of run instr. let rec run instr (h:heap) ( rs : regs) = function IAdd ( Reg r1, Reg r2, Reg r3) Hashtbl. replace rs r1 (stupid add (Hashtbl. find rs r2 ), (Hashtbl. find rs r3 )); None IIfZero ( Reg r, n) match (Hashtbl.find rs r) with Int 0 Some n None IIfUndef ( Reg r, n) > match (Hashtbl.find rs r) with Undef Some n None... let stupid add = function Int x, Int y x + y Undef 7

Question 4. Code Generation (25 points). Below is more code from 06-codegen-2.ml, showing the input expression and part of the compiler. type expr = EInt of int EPlus of expr expr EMul of expr expr EId of string EAssn of string expr ESeq of expr expr EIfZero of expr expr expr type symtbl = ( string int ) list let rec comp expr (st:symtbl) = function EInt n (r, [ILoad ( Reg r, Const n)]) EPlus (e1, e2) let (r1, p1) = comp expr st e1 in let (r2, p2) = comp expr st e2 in (r, p1 @ p2 @ [IAdd ( Reg r, Reg r1, Reg r2)]) EIfZero (e1, e2, e3) let (r1, p1) = comp expr st e1 in let (r2, p2) = comp expr st e2 in let (r3, p3) = comp expr st e3 in (r, p1 @ [ IIfZero ( Reg r1, (2 + (List. length p3)))] @ p3 @ [IMov ( Reg r, Reg r3); IJmp (1 + (List. length p2))] @ p2 @ [IMov ( Reg r, Reg r2)] ) a. (10 points) Suppose we extend the source language with a repeat-until loop ERepeat(e1, e2), meaning repeat e1 until e2 becomes non-zero. Note that a repeat-until loop always executes the body e1 at least once (so it evaluates e1; checks if e2 is non-zero; if not evaluates e1 again; etc). Write a case of comp expr that compiles ERepeat. The loop itself should evaluate to 0. let rec comp expr (st:symtbl) = function... ERepeat (e1, e2) let (r1, p1) = comp expr st e1 in let (r2, p2) = comp expr st e2 in (r, p1 @ p2 @ [ IIfZero ( L Register r2, ((List. length p1) + (List. length p2) + 1)); ILoad ( L Register r, L Int 0)]) 8

b. (15 points) Now consider again adding an undefined value to the language: type expr = EUndef... Write the EUndef case of comp expr. Also, rewrite the EPlus case of comp expr to implement the Undef semantics without relying on the special IAdd handling that understands Undef. That is, your compiled output code should only call IAdd with integer arguments. let rec comp expr (st:symtbl) = function... EUndef (r, [ILoad(r, Const Undef)])... EPlus (e1, e2) let (r1, p1) = comp expr st e1 in let (r2, p2) = comp expr st e2 in (r, p1 @ p2 @ [ IIfUndef (r1, 3); IIfUndef (r2, 2); IAdd(r, r1, r2 ); IJmp 1 ILoad(r, Const Undef)]) 9

Question 5. Optimization (15 points). In each row of the table, perform the indicated optimization (and only that optimization), writing down the optimized code on the right-hand side of the table. To reduce writing, we write rn instead of Reg n, and we write n instead of Const n. Initial code ILoad (r0, 42) IMov (r1, r0) IAdd (r2, r0, r1) Optimized code Copy propagation ILoad (r0, 42) IMov (r1, r0) IAdd (r2, r0, r0) IAdd (r3, r1, r2) IMul (r4, r1, r2) IAdd (r5, r1, r2) Common subexpression elimination IAdd (r3, r1, r2) IMul (r4, r1, r2) IMov (r5, r3) ILoad (r0, 42) ILoad (r1, 3) IAdd (r3, r0, r1) IMul (r4, r1, r2) IAdd (r5, r1, r3) Constant folding ILoad (r0, 42) ILoad (r1, 3) ILoad (r3, 45) IMul (r4, r1, r2) ILoad (r5, 48) ILoad (r0, 0) ILoad (r1, 1) ILoad (r2, 2) IAdd (r3, r0, r1) IAdd (r4, r0, r2) IAdd (r5, r1, r3) ( assume only r5 is live ) Dead code elimination ILoad (r0, 0) ILoad (r1, 1) IAdd (r3, r0, r1) IAdd (r5, r1, r3) 10