Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours

Similar documents
Lab 1. Warm-up : discovering the target machine, LC-3

Writing Evaluators MIF08. Laure Gonnord

Mosig M1 - PLSCD Written exam

Application: Programming Language Semantics

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON. Instructor: Rahul Nayar TAs: Annie Lin, Mohit Verma

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Program Representations

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Memory Usage in Programs

ECE/CS 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Induction and Semantics in Dafny

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

appendix a The LC-3 ISA A.1 Overview

CMSC 330: Organization of Programming Languages. Operational Semantics

ECE/CS 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

1 Introduction. 3 Syntax

Intermediate Representations

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Trap Vector Table. Interrupt Vector Table. Operating System and Supervisor Stack. Available for User Programs. Device Register Addresses

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING COMPUTER SCIENCES DEPARTMENT UNIVERSITY OF WISCONSIN-MADISON

Compilers and computer architecture: Semantic analysis

Compilers. Compiler Construction Tutorial The Front-end

Foundations: Syntax, Semantics, and Graphs

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON. Instructor: Rahul Nayar TAs: Mohit Verma, Annie Lin

CSCI Compiler Design

INFOB3TC Exam 2. Sean Leather. Monday, 30 January 2012, 17:00 20:00

CSCI Compiler Design

Variables. Substitution

Syntax and Grammars 1 / 21

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Intermediate Code Generation

INFOB3TC Solutions for Exam 2

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

COP4020 Spring 2011 Midterm Exam

COS 320. Compiling Techniques

CSE505, Fall 2012, Midterm Examination October 30, 2012

The LC-3 Instruction Set Architecture. ISA Overview Operate instructions Data Movement instructions Control Instructions LC-3 data path

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

LC-2 Programmer s Reference and User Guide

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING COMPUTER SCIENCES DEPARTMENT UNIVERSITY OF WISCONSIN-MADISON

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

CS4215 Programming Language Implementation. Martin Henz

A Simple Syntax-Directed Translator

CIS 341 Final Examination 4 May 2017

UNIVERSITY OF WISCONSIN MADISON

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Defining syntax using CFGs

Advanced Algorithms and Computational Models (module A)

Types, Expressions, and States

Comp 311: Sample Midterm Examination

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

5. Syntax-Directed Definitions & Type Analysis

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

Handout 9: Imperative Programs and State

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

Meeting14:Denotations

ECE 411 Exam 1. This exam has 5 problems. Make sure you have a complete exam before you begin.

Assignment 4: Semantics

Chapter 3 (part 3) Describing Syntax and Semantics

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

2 Introduction to operational semantics

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

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

CSCI 2121 Computer Organization and Assembly Language PRACTICE QUESTION BANK

CS 432 Fall Mike Lam, Professor. Code Generation

LC-3 Instruction Set Architecture

Typed Racket: Racket with Static Types

CS 2461: Computer Architecture I

Project 5 Due 11:59:59pm Tue, Dec 11, 2012

10/31/2016. The LC-3 ISA Has Three Kinds of Opcodes. ECE 120: Introduction to Computing. ADD and AND Have Two Addressing Modes

CS422 - Programming Language Design

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

LC-3. 3 bits for condition codes (more later) Fixed-length instructions, with 4-bit opcodes. IIT CS 350 S 18- Hale

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

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

3.7 Denotational Semantics

CS 270 Fall 2013 Mock Final

Lecture 3: Recursion; Structural Induction

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

Types and Static Type Checking (Introducing Micro-Haskell)

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Lab 1. Warm-up : discovering the target machine, LEIA

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Proving Properties on Programs From the Coq Tutorial at ITP 2015

Lecture Notes on Static Semantics

Part VI. Imperative Functional Programming

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Operational Semantics of Cool

Meeting13:Denotations

CSE 413 Winter 2001 Final Exam Sample Solution

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

Transcription:

http://laure.gonnord.org/pro/ CAP, ENSL, 2016/2017 Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours Only one a4 sheet (10pt, recto/verso) is autorized. Instructions : 1. We give some typing/operational/code generation rules as examples inside the exercises. 2. Explain your results! 3. We give indicative timing. 4. Vous avez le droit de répondre en Français. Page 1 on 11

Exercise #1 Attributes (10min) Let us consider the following grammar for lists : L numl { L}L ε Question #1.1 Draw the derivation tree for the string : {{12{17}}}. Question #1.2 Write a syntax-directed attribution (pseudo-code) that computes the product of all numeric elements of a given list. Solution: The derivation tree (or Parse Tree) is : Be careful a parse tree is not an AST, each internal node denotes a rule application where the node is a non terminal and each of its son is either a terminal or a non terminal. Leaves are non terminal There is no real choice for the attribution, let us invent an attribute val, of type int. In the epsilon rule, L.val<-1. In the concat rule L.val<-L_1.val*L_2.val (rename properly before). In the num rule L.val<-L_1.val*int(num). For attributions, I expect a type for the attribute as well as the (recursive) rules to compute this attribution. Exercise #2 Hand Assembling (10 min) Page 2 on 11

To answer the following questions, you will need the simplied LC3 instruction set depicted in Table 1. Question #2.1 Assemble by hand the following instruction in LC3 assembly code (with intermediate steps) : 1 AND r0 r3 #2 ; Solution: We want the corresponding code for ADD DR SR1 + SEXT(Imm5), AND = 0101. r0 = 000, r3 = 011. 2 = 00010. We get : 0101 000 011 1 00010. Question #2.2 Disassemble by hand the following instruction, given in binary : 0000110000000111 ; Solution: BRP nz label=+7 Nothing dicult here. syntaxe action NZP codage opcode arguments F E D C B A 9 8 7 6 5 4 3 2 1 0 NOT DR,SR DR not SR * 1 0 0 1 DR SR 1 1 1 1 1 1 ADD DR,SR1,SR2 DR SR1 + SR2 * 0 0 0 1 DR SR1 0 0 0 SR2 ADD DR,SR1,Imm5 DR SR1 + SEXT(Imm5) * 0 0 0 1 DR SR1 1 Imm5 AND DR,SR1,SR2 DR SR1 and SR2 * 0 1 0 1 DR SR1 0 0 0 SR2 AND DR,SR1,Imm5 DR SR1 and SEXT(Imm5) * 0 1 0 1 DR SR1 1 Imm5 LEA DR,label DR PC + SEXT(PCoset9) * 1 1 1 0 DR PCoset9 LD DR,label DR mem[pc + SEXT(PCoset9)] * 0 0 1 0 DR PCoset9 ST SR,label mem[pc + SEXT(PCoset9)] SR 0 0 1 1 SR PCoset9 LDR DR,BaseR,Oset6 DR mem[baser + SEXT(Oset6)] * 0 1 1 0 DR BaseR Oset6 STR SR,BaseR,Oset6 mem[baser + SEXT(Oset6)] SR 0 1 1 1 SR BaseR Oset6 BR[n][z][p] label Si (cond) PC PC + SEXT(PCoset9) 0 0 0 0 n z p PCoset9 NOP No Operation 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 RET (JMP R7) PC R7 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 JSR label R7 PC ; PC PC + SEXT(PCoset11) 0 1 0 0 1 PCoset11 Table 1 LC3 simplied instruction set Exercise #3 A new case instruction for Mini-While (30min) The abstract grammar for statements of Mini-While is augmented with a new construction : S(Smt) ::= x := e assign skip do nothing S 1 ; S 2 sequence if e then S 1 else S 2 test while e do S done loop case e of LS endcase case! Page 3 on 11

with the following denition for LS : LS ::= n : S n : S, LS with n integer LS is a list of commands labeled by integers (n N), separated by colons (','). Here is the informal semantics of this new construction : the expression e is evaluated in an integer value v. If v is equal to one label n of the case, then the associated command is executed ; else the case behaves like a skip. Hence, in the following program : x := 3 ; y := 2 ; case x-y of 1 : x := x+y, 0 : x := 2, 3 : y := 0 endcase the command which will be executed is the one labeled by the integer 1 (as the current value of x-y is 1 when the execution ow gets into the case). The memory after the execution of the program will be : σ = [x 5, y 2]. Such a construction is well-formed if all labels are distinct integers. Question #3.1 Complete the following denition of the B D attribution that constructs a list of labels dened in the (LS) list, while verifying that all labels are distinct. Use pseudo-code for lists with the following constructors : List.empty() constructs an empty list, List.add(el,list) adds an element in the list (with side-eect), and the predicate List.mem(el,list) returns true i the element el is in the list. If there exists a double denition, return an error with an exception. B D (n : S) =?? B D (n : S, LS) =?? Solution: No real diculty, for the base case the list [n] is constructed, then for the concat case we search for existance,... Be careful to use names, and if you invent data structures, to specify if they are global or an attribute to be returned Question #3.2 Explain in less than a paragraph how would be the implementation of such two rules inside a ANTLR-Python visitor. Solution: To use visitors, we have to instrument the names of the two rules in the.g4 le (say #basebd and #concatbd here). In a class that inherits from the grammar's generic visitor, we would have to implement : visitbasebd(self,ctx) Page 4 on 11

and visitconcatbd(self,ctx) These two visit methods could return the list dened in the previous question. For the second, we have to make a recursive call to the sublist LS, which can be done by something like visitconcatbd(self,ctx.ls()) I gave some points to people explaining about datastructures. I was however expecting an explanation of the recursive calls' mechanism. Question #3.3 From now on, we suppose that LS are well-formed. Give natural semantic rules (big steps semantics) for the case construction. To help you, we recall here some of the semantics rules for (some) other Mini-While statements. (x := a, σ) σ[x A[a]σ] (skip, σ) σ if B[b]σ = true : (S, σ) σ, (while b do S, σ ) σ (while b do S, σ) σ Solution: For instance (needs more explanation) : if A[e]σ n : (case e of n : S endcase, σ) σ if A[e]σ = n : if A[e]σ = n : if A[e]σ n : (S, σ) σ (case e of n : S endcase, σ) σ (S, σ) σ (case e of n : S, LS endcase, σ) σ (case e of n : LS endcase, σ) σ (case e of n : S, LS endcase, σ) σ Question #3.4 Apply these rules on the example. Solution: No diculty. Exercise #4 Mini-While : typing + code generation (30 min) Here is a program in the Mini-While language seen in the course : x := 8; y := -1; if (x<(19+y)) then x := 42; z := x; Page 5 on 11

Partial Exam CAP - 2016 Question #4.1 Γ(x) = Γ(y) = Γ(z) = int. To help you, we recall here some of the typing rules for expressions and statements : Show that this program is well-typed, under the following entry typing context : Γ ` e1 : int Γ ` e2 : int Γ ` e1 + e2 : int Γ(x) = t t {int, bool} Γ`x:t Γ ` e : t Γ ` x : t t {int, bool} Γ ` x := e : void Solution: Γ ` b : bool Γ ` S : void Γ ` while b do S done : void Here it is : Question #4.2 Generate the LC3 3-address code (LC3 + temporaries/virtual registers) for the given program. Recursive calls, auxiliary temporaries, code, must be separated and clearly described. To help you we provide some generation rules in Figure 1 and 2. Solution: Here it is : Page 6 on 11

(constant expression) c (expression e1 < e2) #not valid if c is too big dr <-newtemp() code.add(instructionand(dr, dr, O)) code.add(instructionadd(dr, dr, c)) return dr dr <-newtemp() t1 <- GenCodeExpr (e1-e2) #last write in register (lfalse,lend) <- newlabels() code.add(instructionbrzp(lfalse)) #if =0 or >0 jump! code.add(instructionand(dr, dr, O)) code.add(instructionadd(dr, dr, 1)) #dr <- true code.add(instructionbr(lend)) code.addlabel(lfalse) code.add(instructionand(dr, dr, O)) #dr <- false code.addlabel(lend) return dr Figure 1 3-address code generation rules 1/2 (Stm) x := e (Stm)if b then S1 else S2 dr <- GenCodeExpr(e) #a code to compute e has been generated if x has a location loc: code.add(instructionadd(loc,dr,0)) else: storelocation(x,dr) dr <-GenCodeExpr(b) #dr is the last written register lfalse,lendif=newlabels() code.add(instructionbrz(lfalse) #if 0 jump to execute S2 GenCodeSmt(S1) #else (execute S1 code.add(instructionbr(lendif)) #and jump to end) code.addlabel(lfalse) GenCodeSmt(S2) code.addlabel(lendif) Figure 2 3-address code generation rules 2/2 Page 7 on 11

Partial Exam CAP - 2016 I was expecting real code generation, thus the recursive calls have to be instantiated and produce real code. I gave very few points to code coming with no exaplanation. Be careful to the application of the while rule, there is some code redundancy in computing the test and then branching. Page 8 on 11

Exercise #5 A new Type System for Mini-While (40 min) Adapted from oldies used in Grenoble a long time ago. In this exercice, we replace the abstract grammar for the mini-while expressions by nexp ::= p positive or nul constant n stricly negative constant x variable nexp + nexp addition nexp nexp substraction nexp nexp multiplication... for arithmetic expressions (now there is a distinction between negative and positive constants), and for boolean expressions : Statements are unchanged : bexp ::= true constant f alse constant bexp or bexp logical or e < e comparaison S(Smt) ::= x := expr assign (bexpr or expr) skip do nothing S 1 ; S 2 sequence if bexp then S 1 else S 2 test while bexp do S done loop Now we dene three types for numerical expressions : Pos, Neg, Int, a type for boolean expressions ok The idea is now to propagate the sign information for arithmetic expression : the type is its sign (or Int if we cannot conclude). Γ denotes now the typing environment. We give rules for contants, variables, addition : (Γ, p) Pos (Γ, n) Neg (Γ, x) Γ(x) (Γ, a 1 ) Pos (Γ, a 2 ) Pos (Γ, a 1 + a 2 ) Pos (Γ, a 1 ) Neg (Γ, a 2 ) Neg (Γ, a 1 + a 2 ) Neg (Γ, a 1 ) Neg (Γ, a 2 ) Pos (Γ, a 1 + a 2 ) Int These rules have the following meaning : adding two positive integers gives a positive integer, but adding a positive and a negative integer gives an integer (we cannot conclude... ). Rules for boolean expressions always give the type ok if the expression is well typed : (Γ, true) ok (Γ, a 1 ) τ (Γ, a 2 ) τ (Γ, a 1 = a 2 ) ok (Γ, b 1 ) ok (Γ, b 2 ) ok (Γ, b 1 b 2 ) ok Finally, here are rules for statements : Page 9 on 11

(Γ, a) τ (Γ, x := a) Γ[x τ] (Γ, skip) Γ (Γ, S) Γ (Γ, while b do S) Γ Γ Let us clarify a bit the notation. For a given variable, for the while, we have to merge the information coming from before the while and the one coming from S. If they are both Pos, or both Neg, then the result of the merge is the very same type. If the two values do not coincide, then the merge of the two types are Int. Then the denition of is extended pointwise. The while rule itself needs a little bit more explanation. This rule uses the fact that, in this particular type system, you cannot infer any more information by applying more than one loop : this should have been proven before, and comes from the fact that a given variable cannot changes its type more that twice. This is not the case in general typing systems, in the more general case we should have to test that Γ is stable by S. Question #5.1 Type the expression : ( 2 + x) + 8 under the context Γ(x) = Neg. Solution: Type tree like in the course. Do not forget to close the leaves properly. Question #5.2 Give rules for substraction and multiplication. Solution: For sub, two cases propagate information : Neg - Plus and Plus - Neg : (Γ, a 1 ) Neg (Γ, a 2 ) Pos (Γ, a 1 a 2 ) Neg (Γ, a 1 ) Neg (Γ, a 2 ) Neg (Γ, a 1 a 2 ) Int For multiplication the result is more precise : (Γ, a 1 ) Neg (Γ, a 2 ) Neg (Γ, a 1 a 2 ) Pos (Γ, a 1 ) Pos (Γ, a 2 ) Neg (Γ, a 1 a 2 ) Pos (Γ, a 1 ) Pos (Γ, a 2 ) Pos (Γ, a 1 a 2 ) Int (Γ, a 1 ) Pos (Γ, a 2 ) Pos (Γ, a 1 a 2 ) Pos (Γ, a 1 ) Neg (Γ, a 2 ) Pos (Γ, a 1 a 2 ) Neg (Γ, a 1 ) Pos (Γ, a 2 ) Neg (Γ, a 1 a 2 ) Neg there is a slight problem for multiplication with Pos, as the operand might be 0, Neg*Pos! should give Int. I gave the whole number of points for both solutions. Question #5.3 Give rules for the sequence and test (if). Solution: For seq, the typing informations are transmitted from the rst to the second part : (Γ, S 1 ) Γ 1 (Γ 1, S 2 ) Γ 2 (Γ, S 1 ; S 2 ) Γ 2 For if then else, we have to merge the information from the two branches : (Γ, b) ok (Γ, S 1 ) Γ 1 (Γ, S 2 ) Γ 2 (Γ, if b then S 1 else S 2 ) Γ 1 Γ 2 Page 10 on 11

Since I had not dened the meaning of properly, this question was not very interesting. However, even with an informal notion of merge information, the environnement before the if should not be copied at the bottom right hand side. Given a type environment Γ and a memory σ (like in the SOS rules of the course, the memory assigns values to variables), we dene the following relation : (Γ, σ) R i x [(Γ(x) = Pos σ(x) 0) (Γ(x) = Neg σ(x) < 0) Γ(x) = Int] Question #5.4 Show that for all (Γ, σ) R, if (Γ, a) Pos then A[a]σ 0 and if (Γ, a) Neg, then A[a]σ < 0. Solution: By induction on a : if a is a positive constant, then (Γ, a) Pos) and A[a]σ = p 0. same for a negative constant. if x is a variable, either she is positive or null, either she is positive, which is given by the value during execution : A[x]σ if a = a 1 + a 2, WLOG let us suppose that (Γ, a) Pos. From the typing rule we have (Γ, a 1 ) Pos and (Γ, a 2 ) Pos), thus we can imply the induction hypothesis on both these arguments, thus A[a 1 ]σ 0 and A[a 2 ]σ 0. Then, as A[a 1 + a 2 ]σ = A[a 1 ]σ + A[a 2 ]σ, A[a 1 + a 2 ]σ is positive. all the other cases are similar. Question #5.5 By induction, show that if (Γ, σ) R and (S, σ) σ, then there exists Γ such that (Γ, S) Γ and (Γ, σ ) R. Solution: Left to the reader. The only diculties are tests and while. For the test, the stability comes from the union, and for the while, there is more work to do... Question #5.6 What goes that mean for our typing system? Solution: This means each time we have a transition in the concrete world we have a transition in the abstract world, this is progression. Together with question 5.4 this means that the type system is safe. Page 11 on 11