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

Similar documents
EXAMINATIONS 2007 END YEAR COMP471 COMPILER TECHNOLOGIES. Instructions: Read each question carefully before attempting it.

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

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

CMSC430 Spring 2014 Midterm 2 Solutions

Static Program Analysis

Intermediate Code Generation

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Compiler construction 2009

Compiling Techniques

CSC 4181 Handout : JVM

COMP3131/9102: Programming Languages and Compilers

Problem Score Max Score 1 Syntax directed translation & type

Java byte code verification

Mocha: Type Inference for Java

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

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

Program Representations

02 B The Java Virtual Machine

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

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

Exercise 7 Bytecode Verification self-study exercise sheet

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

The Structure of a Syntax-Directed Compiler

Just In Time Compilation

A Tour of Language Implementation

CSCE 314 Programming Languages. Type System

Programming with Java

CIS 341 Final Examination 4 May 2017

Featherweight Java. Lecture 12 CS 565 3/18/08

CSE 431S Final Review. Washington University Spring 2013

A simple syntax-directed

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

Introduction to Programming Using Java (98-388)

Last name:... First name:... Department (if not D-INFK):...

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

CSE 5317 Midterm Examination 4 March Solutions

Compiler construction 2009

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

CSE 431S Code Generation. Washington University Spring 2013

LECTURE 3. Compiler Phases

Running class Timing on Java HotSpot VM, 1

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

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

Compilation 2012 Code Generation

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

The role of semantic analysis in a compiler

CSE P 501 Exam 12/1/11

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

Code Generation Introduction

Syntax Errors; Static Semantics

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

The Java Virtual Machine

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Computer Science 1 Bh

CSCE 314 Programming Languages

Portable Resource Control in Java: Application to Mobile Agent Security

TDDD55- Compilers and Interpreters Lesson 3

Syntax and Grammars 1 / 21

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

CS153: Compilers Lecture 15: Local Optimization

Part VII : Code Generation

EXAMINATIONS 2009 END-OF-YEAR. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering

A brief overview of COSTA

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

JAM 16: The Instruction Set & Sample Programs

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

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

List of Figures. About the Authors. Acknowledgments

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

COMP 261 ALGORITHMS and DATA STRUCTURES

Chapter 2 A Quick Tour

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

The Structure of a Syntax-Directed Compiler

Compiler I (dt. Übersetzer I) Prof. Dr. Uwe Kastens Winter 2001/2002

2001 bei Prof. Dr. Uwe Kastens

(Not Quite) Minijava

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

ECE251 Midterm practice questions, Fall 2010

Improving Java Performance

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

Recap: Printing Trees into Bytecodes

Principles of Compiler Design

O T E Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 1 *** WITH SOLUTIONS ***

Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 1 *** WITH SOLUTIONS ***

Portable Resource Control in Java The J-SEAL2 Approach

Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2014 TRIMESTER 1 *** WITH SOLUTIONS ***

CS5363 Final Review. cs5363 1

Programming Project II

When do We Run a Compiler?

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CSE P 501 Exam Sample Solution 12/1/11

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

The Structure of a Compiler

SWEN 224 Formal Foundations of Programming

MIDTERM EXAM (Solutions)

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Transcription:

T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON EXAMINATIONS 2008 MID-YEAR COMP431 COMPILERS Time Allowed: 3 Hours Instructions: Read each question carefully before attempting it. This examination will be marked out of 180 marks. Answer all questions. You may answer the questions in any order. Make sure you clearly identify the question you are answering. Non-electronic foreign language-english dictionaries are permitted. Reference material and calculators are NOT PERMITTED. Use of mobile phones, laptop computers, PDAs or other electronic devices is NOT PERMITTED. Questions Marks 1. Preliminaries [10] 2. Parsing [18] 3. Type Checking [40] 4. Intermediate Language [25] 5. Peephole Optimisation [30] 6. Register Allocation [20] 7. Dataflow Analysis [25] 8. Static Single Assignmnet (SSA) [12] COMP431 continued...

Intermediate Language In some of the questions, the following language (which is similar to JKil) will be used: c integers v, m, f, C local variable, method, field and class names (respectively) e, e 1, e 2 arithmetic expressions aop binary arithmetic operators (e.g. +,-,*,/) bop binary boolean operators (e.g.!=,==,<,>,<=,>=) L 1, L 2 instruction labels D C, D M class and method declarations (respectively) T Java types S statements D C ::= class C 1 extends C 2 { T f D M D M ::= T m(t v) { T v S T ::= int C S ::= [L 1 ] v = e [L 1 ] v. f = e [L 1 ] v 1 = v 2.m(t 1,..., t n ) [L 1 ] return t [L 1 ] if v bop t goto L 2 [L 1 ] goto L 2 e ::= t 1 aop t 2 v. f new C() t t ::= v c null You may assume that local variables must be declared and defined before being used in a method. An example program in the Intermediate Language is: class Test extends java.lang.object { int amethod(int j) { int i, Test p [0] i=0 [1] if i>10 goto 5 [2] i=i+1 [3] j=j*2 [4] goto 1 [5] j = this.amethod(j) [6] return j Assume the class hierarchy follows Java and java.lang.object is the root. So, for example, java.lang.integer extends java.lang.number, etc. COMP431 2 continued...

Question 1. Preliminaries (a) [2 marks] Describe what is generally meant by a compiler in Computer Science. [10 marks] (b) [8 marks] List all the generic compiler stages (this list should have at least four stages) and give a one sentence description of what happens at each stage. Question 2. Parsing [18 marks] (a) [5 marks] Describe abstract syntax trees (AST s) and outline what they are used for in compilation. (b) [3 marks] Describe the difference between a scanner and a parser. (c) [5 marks] In Pascal, semicolons are used to separate statements and brackets to group statements. This is illustrated in the following examples: ( statement ; statement ; statement ) ( statement ; ( statement ; statement ) ; statement ) Write an unambiguous grammar which describes the above structure (HINT: statement will be one of the terminals). (d) [5 marks] In C, semicolons are used to terminate statements and braces to group statements: { expression; expression; expression; { expression; { expression; expression; expression; Write an unambiguous grammar that describes this structure. COMP431 3 continued...

Question 3. Type Checking [40 marks] Consider the following type system describing a trivial subset of the Java language. Its syntax is as follows: L ::= class C {C f; K K ::= C(C f){this.f = f; e ::= x new C(e) e.f L is a class declaration for a class named C and C f stands for a list of fields each having a type C and a name f. K describes a constructor that must initialise all fields in the class. There are no methods or casts in our language and only three possible expressions: variable (x), class instantiation (new C(e)) and field access (e.f). We provide the following expression type checking rules (where the f ields(c) lookup function provides a list of fields given class C): Γ K = C(C f){this.f = f; Γ class C {C f; K OK (T-CLASS) Γ x : Γ(x) (T-VAR) f ields(c) = C f Γ e : C Γ new C(e) (T-NEW) Γ e 0 : C 0 f ields(c 0 ) = C f Γ e 0.f i : C i (T-FIELD) A program in our language is a list of class declarations followed by an expression. Each class declaration should meet the T-CLASS rule and the expression should meet the expression type rules. Assume that class Object is pre-defined. Also assume that a class table CT maps class names C to class declarations L and an environment Γ maps variables to their types. (a) [10 marks] Construct an example of a program in this language that contains at least one class declaration with at least two fields and an expression that uses each of the valid kinds of expression. (Question 3 continued on next page) COMP431 4 continued...

(Question 3 continued) (b) For each of the following programs show the program derivation if it type checks or show why it does not type check. (i) [8 marks] class A { Object f; A(Object f) { this.f = f; class B { A fa; Object fo; B(A fa, Object fo) { this.fa = fa; this.fo = fo; (new B(new A(new Object()), new Object())).fA.f (ii) [8 marks] class Pair { Object fst; Object snd; Pair(Object fst, Object snd) { this.fst = fst; this.snd = snd; (new Pair(new Object(), new Pair(new Object(), new Object()))).snd.fst (c) [4 marks] In your own words, briefly discuss the purpose of a type system. You may choose to refer to the rules and examples given in this question. (d) [5 marks] Briefly describe the purpose of subject reduction and progress proofs. State what they show for the language type system described in this question. (e) [5 marks] In the description of the language we have presented rules for syntax, type rules, and a f ields lookup function. Explain what other category of rules must also be provided to fully describe a language behavour using a type system? COMP431 5 continued...

Question 4. Intermediate Language [25 marks] (a) [4 marks] Discuss the differences between the Abstract Syntax Tree (AST) and Intermediate Language (or Representation) used in a compiler. (b) Translate the following three Java programs into the Intermediate Language given on Page 2. You should attempt to produce intermediate language programs that are as efficient as possible. (i) [7 marks] class Foo { int f() { Foo foo = new Foo(); if (foo!= null) foo.f(); (ii) [7 marks] class Foo { int f(int a, int b) { if (a <= b && a > 0) { while (b > a) { b -= a; return b; else { return b; (iii) [7 marks] class Foo { int f(int y) { int z=y; if(z > 42) { y = 1 + (z*2); else { y = (2*z) + 2; return z; COMP431 6 continued...

Question 5. Peephole Optimisation (a) [5 marks] Describe and discuss the peephole optimisation technique. Consider the following peephole optimisation rules: [30 marks] Original Form Optimised Form 1..iload X; iconst Y; iadd; istore X;....iinc X,Y;.. 2..iconst Y; iadd; istore X;....istore X; iinc X,Y;.. 3..bipush X; bipush X;....bipush X; dup;.. 4..Tconst X; Tconst Y; Tadd;....Tconst X+Y;.. 5..aconst_null;aload_X; if_acmpne L1;....aload_X; ifnonnull L1.. 6..if_icmpge L1;...; L1: goto L2;....if_icmpge L2;...; L1: goto L2;.. (b) [5 marks] Give at least 2 more rules not listed above. (c) [10 marks] Consider the following bytecodes: iconst_1 istore_1 iload_1 iconst_1 iadd istore_1 bipush 10 newarray int astore_2 iconst_0 istore_3 L0: iload_3 bipush 10 if_icmpge L2 bipush 11 bipush 11 istore_1 iconst_5 istore 4 iload 4 goto L1 L1: iconst_5 iadd istore 4 iinc 3, 1 goto L0 L2: goto L3 L3: return Provide an optimised version of this code using the rules 1... 6 listed above. Be sure to indicate which rules you have used and in which places. (Question 5 continued on next page) COMP431 7 continued...

(Question 5 continued) (d) [5 marks] Consider the following bytecodes: iload 1 ifeq L1 iload 1 iconst 2 iadd L1: istore 1 It might appear that Rule 1 from the above list could be applied here. Why is it unsafe to do so? (e) [5 marks] Consider the following rule: Rule 0:...;Tstore X; Tload X;......;... Discuss why this rule is unsafe to use in general. Give an example to illustrate the problem. Question 6. Register Allocation (a) [5 marks] Describe how register allocation works. (b) [5 marks] Describe the graph colouring approach to register allocation. (c) [5 marks] Given the following code draw the interference graph: [20 marks] a = 1; b = 2; c = a + 3; d = b + 4; e = c + a; a = b + a; (d) [5 marks] Describe linear scan approach to register allocation. COMP431 8 continued...

Question 7. Dataflow Analysis [25 marks] A compiler for the intermediate language given on page 2 should identify situations where a reference variable is known to be non-null. For example, consider the following: int f(string x) String y [0] y = "Hello World" [1] z = y.size() [2] if x == null goto 4 [3] z = z + x.size() [4] return z Here, the compiler infers that x is definitely non-null before statement 3, but may be null before statements 0, 1, 2, and 4. Likewise, variable y is determined to be definitely non-null before statements 1-4, and may be null before statement 0. (a) [20 marks] Design a simple dataflow analysis for determining whether a variable is definitely non-null before a given statement. Be sure to describe the following parts of the analysis: lattice of values (including and ), transfer function, and the generic dataflow equations. To help, a skeleton for the different cases of the transfer function is given below: f T (X, [L 1 ] v = e) =??? f T (X, [L 1 ] v. f = e) = X f T (X, [L 1 ] v 1 = v 2.m(t 1,..., t n )) =??? f T (X, [L 1 ] return t) = f T (X, [L 1 ] if v bop t goto L 2 ) =???, if v bop t is true f T (X, [L 1 ] if v bop t goto L 2 ) =???, if v bop t is false f T (X, [L 1 ] goto L 2 ) =??? isnonnull(x, v) = true, if v X isnonnull(x, v) = false, if v X isnonnull(x, c) = false isnonnull(x, null) =??? isnonnull(x, t 1 aop t 2 ) =??? isnonnull(x, v. f ) =??? isnonnull(x, new C()) = true You need to fill in the missing bits marked with???. Note that there are two cases for the if statement one for the false and one for the true branch. (b) [5 marks] Discuss whether or not your dataflow analysis can safely determine whether a field is definitely non-null. COMP431 9 continued...

Question 8. Static Single Assignment (SSA) [12 marks] (a) [2 marks] State the key requirement relating variables and definitions that is enforced by the SSA form. (b) [5 marks] Draw the control-flow graph of the following program: int f (int i, int j) { int x, y; switch (i) { case 0: x=3; case 1: x=1; case 2: x=4; case 3: x=1; case 4: x=5; default: x=9; switch (j) { case 0: y=x+2; case 1: y=x+7; case 2: y=x+1; case 3: y=x+8; case 4: y=x+2; default: y=x+8; return y; (c) [5 marks] Starting from the CFG in the previous subquestion, convert the program to SSA form. Be sure to include φ-nodes where appropriate. ******************************** COMP431 10