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

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

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Machine Program: Procedure. Zhaoguo Wang

Areas for growth: I love feedback

Machine Language CS 3330 Samira Khan

CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

Assembly Language: Function Calls

Machine-Level Programming (2)

CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

last time Assembly part 2 / C part 1 condition codes reminder: quiz

COMP 210 Example Question Exam 2 (Solutions at the bottom)

Machine Programming 3: Procedures

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

CSE351 Spring 2018, Midterm Exam April 27, 2018

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

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

Corrections made in this version not seen in first lecture:

x86-64 Programming III & The Stack

CSCI 2021: x86-64 Control Flow

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

How Software Executes

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

CS429: Computer Organization and Architecture

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

Computer Organization: A Programmer's Perspective

CSE P 501 Exam Sample Solution 12/1/11

Credits and Disclaimers

x86-64 Programming III

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Department of Electrical Engineering and Computer Science

Computer Architecture I: Outline and Instruction Set Architecture. CENG331 - Computer Organization. Murat Manguoglu

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

C++ Named Return Value Optimization

Unoptimized Code Generation

Machine-Level Programming III: Procedures

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

Assembly II: Control Flow

Procedures and the Call Stack

CS 3330 Exam 1 Fall 2017 Computing ID:

1 Number Representation(10 points)

Assembly Programming IV

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Machine-level Programs Procedure

Machine/Assembler Language Putting It All Together

CSC 252: Computer Organization Spring 2018: Lecture 11

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Function Calls and Stack

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Exercise 7 Bytecode Verification self-study exercise sheet

Lecture 3 CIS 341: COMPILERS

Brief Assembly Refresher

CSE 351 Midterm - Winter 2017

Machine- Level Representa2on: Procedure

Generation. representation to the machine

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Changelog. Brief Assembly Refresher. a logistics note. last time

Intel x86-64 and Y86-64 Instruction Set Architecture

Machine-Level Programming II: Control

Machine-Level Programming II: Control

CSE 351 Midterm - Winter 2015 Solutions

Credits and Disclaimers

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

University of Washington

The Hardware/Software Interface CSE351 Spring 2013

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

The Stack & Procedures

The von Neumann Machine

Do not turn the page until 5:10.

CS367. Program Control

Assembly Programming IV

The Stack & Procedures

Credits and Disclaimers

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb mrmovq D(rB), ra 5 0 ra rb OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest

How Software Executes

x86-64 Programming II

Java byte code verification

Machine Level Programming: Control

Corrections made in this version not in first posting:

Credits and Disclaimers

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

Optimization part 1 1

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

Changes made in this version not seen in first lecture:

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CSE P 501 Exam 12/1/11

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Structs and Alignment

CSE 351 Midterm Exam Spring 2016 May 2, 2015

The Compilation Process

CSC 252: Computer Organization Spring 2018: Lecture 6

CS 3330 Exam 2 Spring 2017 Name: EXAM KEY Computing ID: KEY

Code Generation Introduction

Static Program Analysis

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 2014 TRIMESTER 1 SWEN 430 Compiler Engineering Time Allowed: THREE HOURS Instructions: Closed Book. 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. No calculators are permitted. Non-electronic Foreign language dictionaries are allowed. Question Topic Marks 1. WHILE Language 30 2. Definite Unassignment 30 3. Java Bytecode 30 4. Machine Code 30 5. Static Single Assignment 30 6. Interprocedural Analysis 30 Total 180 SWEN 430 continued...

Question 1. WHILE Language The λ W is a simplified representation of a WHILE programming language as used in the lectures on semantics: p ::= f 1... f n e programs f ::= T 1 n 1 (T 2 n 2 ) { s } functions e ::= expressions v constants n variables e 1 op e 2 binary e 1 (e 2 ) application s function body s ::= n = e ; return e ; statements v ::= null true false λn.s values... 1 0 1... T ::= bool int null T 1 T 2 types op ::= < <= >= > operators! = == + (a) For each of the following little programs state if it is a syntactically correct program in the λ W language and also state if it is also type safe. Justify your answer: (i) [2 marks] 42 + 33 (ii) [2 marks] 42 + 33 + 56 (iii) [2 marks] int f(bool x) { x = true; return f(true); } 0 (iv) [3 marks] bool f(int x) { return x == false; } 0 (v) [3 marks] bool f(int x) { return x = x = x = return 42;;; } 0 (vi) [3 marks] int f(int x) { x * x; return 3; } 0 (b) [5 marks] What is meant by the operational semantics and why would we want one for a language? (c) [10 marks] Describe the subtyping relation used by the WHILE language by stating the subtyping rules for the types listed above as well as records: {T n}. Don t forget about union types, here are two basic reflexivity and transitivity rules to remind you of the format to use for writing down the rules stating that one type is a subtype of another type : T 1 T 1 T 1 T 2 T 2 T 3 T 1 T 3 SWEN 430 2 continued...

Question 2. Definite Unassignment The definite unassignment phase is used in Java to check that final variables are only assigned once. The following illustrates using a Java bytecode-like language: int amethod() { var i; final var j; i = 0; lab1: if j > 10 goto lab2; i = i+1; j = i; goto lab1; lab2: return j; } true 0: i = 0 1: if(j>10) false 2: i = i+1 3: j = i 4: return j The above method fails definite unassignment because the final variable j may be assigned more than once. The definite unassignment algorithm determines, at each point, which variables may have been assigned at that point. In particular, the right part of the figure uses the Control Flow Graph (CFG) representation of the program on the left. Going back to the WHILE language, imagine that we extended its syntax to include final variables. (a) [10 marks] Please described how you would implement the definite unassignment algorithm following the approach you took in assignment 2 (i.e. without using a CFG). Include a pseudocode for the core of the algorithm and basic descriptions of the data structures used. (b) [10 marks] Now, please describe how would you design and implement a simple dataflow analysis for definite unassignment utilising a CFG. Include a core algorithm in pseudocode and a brief description of the CFG representation that you would use (just names of classes and relationships representing different parts of a CFG would suffice - around one paragraph of writing for the description of CFG representation at most). Discuss whether your algorithm requires multiple iterations - why or why not. (c) [10 marks] Discuss and justify why do you think your algorithm that uses a CFG will terminate and work correctly? Please refer to our class discussion about a lattice that the algorithm operates on. SWEN 430 3 continued...

Question 3. Java Bytecode (a) Consider the following Java bytecode, which fails bytecode verification: boolean method(int); 0: iconst_2 1: istore_2 2: iload_2 3: aload_1 4: if_icmpeq 13 7: iconst_0 8: dup 9: goto 14 13: iconst_1 14: pop 15: pop 16: ireturn (i) [5 marks] Briefly discuss the different ways in which this method fails bytecode verification. (ii) [5 marks] The bytecode verifier employs a least upper bound operation on types. Discuss what this means, and how it relates to bytecode verification. Use examples to illustrate as necessary. (iii) [5 marks] A bytecode method can fail verification because the verifier claims it uses types incorrectly, when at runtime it does not. Using an example to illustrate, discuss how this can occur. (b) [5 marks] Consider the following bytecode, which creates and initialises a new object. 0: new java/lang/integer 3: dup 4: iload_0 5: invokespecial java/lang/integer."<init>":(i)v 8:... An object can only be used once it is initialised (i.e. the <init> method has been called upon it). Briefly discuss why this complicates bytecode verification. (c) [5 marks] Describe the difference between tableswitch and lookupswitch bytecodes and specify which is going to be more appropriate for different kinds of possible switch statements. (d) [5 marks] Answer one of the following questions (it is up to you to decide which one!): What is the difference between invokevirtual and invokedynamic? Describe, by using an example, the difference between single dispatch and multiple dispatch. SWEN 430 4 continued...

Question 4. Machine Code Consider the following function written in x86_64 assembly language: 1 foo: 2 pushq %rbp 3 movq %rsp, %rbp 4 movl %edi, -20(%rbp) 5 movl %esi, -24(%rbp) 6 movl -20(%rbp), %eax 7 cmpl -24(%rbp), %eax 8 jge.l2 9 movl -24(%rbp), %eax 10 movl %eax, -4(%rbp) 11 jmp.l3 12.L2: 13 movl -20(%rbp), %eax 14 movl %eax, -4(%rbp) 15.L3: 16 movl -4(%rbp), %eax 17 popq %rbp 18 ret NOTE: the Appendix on page 9 provides an overview of x86 64 machine instructions for reference. (a) [5 marks] Function parameters are normally passed on the stack or in registers. How are parameters passed in the above function? Justify your answer. (b) [5 marks] Draw the layout of the stack frame for this function using relative offsets (in bytes) for each component. (c) [5 marks] The special flags register is an unusual feature of the x86_64 architecture. Briefly, explain what this register does using examples from above to illustrate. (d) [5 marks] In your own words, explain what this function does. (e) [5 marks] Outline how the above program can be rewritten to reduce the number of machine instructions required. (f) [5 marks] The above program is not particularly efficient. Briefly, discuss why this is. SWEN 430 5 continued...

Question 5. Static Single Assignment (a) [5 marks] Briefly, discuss what static single assignment form is. (b) [5 marks] Briefly, discuss what ϕ-nodes are used for in SSA form. (c) Consider the following WHILE function: int f(int x, int y) { int z; if x < y { z = x; } else { z = y; } } return z + x + y; (i) [5 marks] Draw the control-flow graph of this function. (ii) [5 marks] Translate this function into static single assignment form. (d) Algorithms for computing static single assignment form consider the dominance structure of nodes in the control-flow graph. (i) [5 marks] Briefly, explain what it means for one node to dominate another in a control-flow graph (ii) [5 marks] Briefly, explain how the dominance structure of nodes in a control-flow graph helps in computing static single assignment. SWEN 430 6 continued...

Question 6. Interprocedural Analysis (a) Class Hierarchy Analysis is a technique for approximating a program s call graph. (i) [10 marks] Using examples where appropriate, discuss how Class Hierarchy Analysis works. (ii) [5 marks] Briefly discuss how Class Hierarchy Analysis can be used to improve program performance. (iii) [5 marks] In Java, it is impossible to know exactly which classes can be used by a given program. This is because classes can be loaded at runtime using the ClassLoader. Discuss how this affects the validity of Class Hierarchy Analysis. (b) Variable Type Analysis is another technique for approximating a program s call graph. (i) [5 marks] Variable Type Analysis uses a type propagation graph. Using examples where appropriate, discuss how this works. (ii) [5 marks] Briefly discuss why Variable Type Analysis gives more precise results than Class Hierarchy Analysis. ******************************** SWEN 430 7

This page is intentionally left blank so that you may remove the appendix page. SWEN 430 8 continued...

Appendix movq $c, %rax movq %rax, %rdi addq $c, %rax addq %rax, %rbx subq $c, %rax subq %rax, %rbx cmpq $0, %rdx cmpq %rax, %rdx movq %rax, (%rbx) movq (%rbx),%rax movq 4(%rsp),%rax movq %rdx, (%rsi,%rbx,4) pushq %rax pushq %c popq %rdi jz target jnz target jl target jle target ret Assign constant c to rax register Assign register rax to rdi register Add constant c to rax register Add rax register to rbx register Substract constant c from rax register Subtract rax register from rbx register Compare constant 0 register against rdx register Compare rax register against rdx register Assign rax register to dword at address rbx Assign rax register from dword at address rbx Assign rax register from dword at address rsp+4 Assign rdx register to dword at address rsi+4*rbx Push rax register onto stack Push constant c onto stack Pop qword off stack and assign to register rdi Branch to target if zero flag set. Branch to target if zero flag not set. Branch to target if less than (i.e. sign flag set). Branch to target if less than or equal (i.e. sign or zero flags set). Return from function. SWEN 430 9 continued...