CS 352 Compilers: Principles and Practice Final Examination, 05/02/05

Similar documents
Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

2) Write in detail the issues in the design of code generator.

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

C Compiler Targeting the Java Virtual Machine

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Semantic Analysis: Types and Type Checking

Technical paper review. Program visualization and explanation for novice C programmers by Matthew Heinsen Egan and Chris McDonald.

CSCI 3136 Principles of Programming Languages

Semester Review. CSC 301, Fall 2015

Stack Allocation. Run-Time Data Structures. Static Structures

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Compiler Construction

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Static Taint-Analysis on Binary Executables

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Machine-Code Generation for Functions

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Write Barrier Removal by Static Analysis

Introduction to Data-flow analysis

Optimizing compilers. CS Modern Compilers: Theory and Practise. Optimization. Compiler structure. Overview of different optimizations

Lecture 9. Semantic Analysis Scoping and Symbol Table

Language Processing Systems

Intermediate Code. Intermediate Code Generation

Symbol Tables. Introduction

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Glossary of Object Oriented Terms

CA Compiler Construction

X86-64 Architecture Guide

Moving from CS 61A Scheme to CS 61B Java

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Upcompiling Legacy Code to Java

PES Institute of Technology-BSC QUESTION BANK

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Explain the relationship between a class and an object. Which is general and which is specific?

Programming Languages CIS 443

Intel Assembler. Project administration. Non-standard project. Project administration: Repository

Obfuscation: know your enemy

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Part I. Multiple Choice Questions (2 points each):

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

How To Trace

CS506 Web Design and Development Solved Online Quiz No. 01

Compiler I: Syntax Analysis Human Thought

CSE 504: Compiler Design. Data Flow Analysis

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Module 2 Stacks and Queues: Abstract Data Types

Computer Programming I

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Python for Rookies. Example Examination Paper

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

AP Computer Science Java Mr. Clausen Program 9A, 9B

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

CompSci-61B, Data Structures Final Exam

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

10CS35: Data Structures Using C

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Performance Improvement In Java Application

Unit Storage Structures 1. Storage Structures. Unit 4.3

02 B The Java Virtual Machine

Departamento de Investigación. LaST: Language Study Tool. Nº 143 Edgard Lindner y Enrique Molinari Coordinación: Graciela Matich

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Applying Clang Static Analyzer to Linux Kernel

Modeling Kahn Process Networks in the Dynamic Dataflow Formalism

Static Analysis. Find the Bug! : Analysis of Software Artifacts. Jonathan Aldrich. disable interrupts. ERROR: returning with interrupts disabled

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

DATA STRUCTURES USING C

Data Structures and Algorithms Written Examination

Habanero Extreme Scale Software Research Project

PROBLEMS (Cap. 4 - Istruzioni macchina)

1/20/2016 INTRODUCTION

Programming Language Pragmatics

Using Eclipse CDT/PTP for Static Analysis

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Introduction to programming

Compiler Construction

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Syntaktická analýza. Ján Šturc. Zima 208

CS 106 Introduction to Computer Science I

Chapter 13: Query Processing. Basic Steps in Query Processing

LINKED DATA STRUCTURES

03 - Lexical Analysis

CS / IT B.E. VII Semester Examination, December 2013 Cloud Computing Time : Three Hours Maximum Marks : 70

[Refer Slide Time: 05:10]

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Transcription:

CS 352 Compilers: Principles and Practice Final Examination, 05/02/05 Instructions: Read carefully through the whole exam first and plan your time. Note the relative weight of each question and part (as a percentage of the score for the whole exam). The total points is 100 (your grade will be the percentage of your answers that are correct). This exam is closed book, closed notes. You may not refer to any book or other materials. You have two hours to complete all four (4) questions. Write your answers on this paper (use both sides if necessary). Name: Student Number: Signature:

1. (Compiler phases; 10%) The MiniJava compiler you worked on this semester manipulates several representations of a program as it is compiled. The initial input is a MiniJava source program. For each of the following program representations name the compiler passes (there may be more than one!) that take that representation as input: (a) program source text (b) tokens scanning (lexical analysis) parsing (syntactic analysis) (c) abstract syntax trees (ASTs) type checking (semantic analysis) translation (d) intermediate code trees canonicalization (e) intermediate code statements (ie, tuples) instruction selection (code generation) (f) assembly language instructions control flow analysis (g) control flow graph (CFG) data flow (liveness) analysis (h) interference graph graph coloring register allocation (i) colored interference graph + assembly language instructions code emission 2

2. (Runtime management; 25%) Consider the MiniJava program given below: class Tree { public static void main (String[] a) throws java.io.ioexception { Tree root = null; for (Tree n = Tree.readint(); n!= null; n = Tree.readint()) root = n.insert (root); if (root!= null) root.print (); System.out.println(""); int value; Tree left, right; Tree insert(tree root) { if (root == null) return this; if (this.value <= root.value) root.left = this.insert (root.left); else root.right = this.insert(root.right); return root; void print() { System.out.write( ( ); if (this.left!= null) this.left.print(); Tree.printint(this.value); if (this.right!= null) this.right.print(); System.out.write( ) ); static Tree readint () throws java.io.ioexception { Tree result = null; int buffer = System.in.read(); while (buffer == ) buffer = System.in.read(); if (buffer >= 0 && buffer <= 9 ) { result = new Tree(); result.value = 0; while (buffer >= 0 && buffer <= 9 ) { result.value = result.value * 10 + buffer - 0 ; buffer = System.in.read(); return result; static void printint(int i) { if (i > 0) { Tree.printint(i/10); System.out.write(i-i/10*10+ 0 ); 3

(a) (5%) Given input: 45 33 24 22 22 10 what output does this program produce? It prints out: ((((((10)22)22)24)33)45) 4

(b) (20%) Assuming the same input, show a diagram of PowerPC stack frames at the point where printint has returned to print having printed out the string 10 to standard output. Indicate where all the local and heap variables are (assume all local variables are stored in memory in the activation records, not in registers, and that all arguments are passed in the stack, not in registers); show the value of all integer variables, as well as variables containing references to the heap, and the object they refer to. STACK HEAP +---------+ main: a --+-------------------> array of String root --+--------------+---------------------> 45... / \ +---------+ +-----------------> 33 null print: this --+--------------+ / \... +-------------> 24 null +---------+ / \ print: this --+----------------+ +---------> 22 null... / \ +---------+ +-----> 22 null print: this --+------------------+ / \... +-> 10 null +---------+ print: this --+--------------------+... +---------+ print: this --+----------------------+... +---------+ print: this --+------------------------+... +---------+ 5

3. (IR trees, canonicalization, instruction selection; 25%) In this question you may assume that the word size of the target machine is 4 bytes and that any result is returned in (PowerPC register) temporary r3. You may use named temporaries for each of the local variables or formal parameters in the method (eg, parameter b would be allocated to temporary b). For unnamed intermediate results use numbered temporaries (t0, t1, etc.). Do not worry about checking for run-time errors such as array index out of bounds or null pointer dereference. Consider the following MiniJava program: class A { String name; A init() { this.name = "A"; return this; String name() { return this.name; class B extends A { String name; B init() { super.init(); this.name = "B"; return this; String name() { return this.name; class Main { static String Aname(A a) { return a.name(); static String Bname(B b) { return b.name(); public static void main (String[] args) { A a = new A().init(); System.out.println(a.name); System.out.println(Main.Aname(a)); B b = new B().init(); System.out.println(b.name); System.out.println(Main.Bname(b)); a = b; System.out.println(a.name); System.out.println(Main.Aname(a)); 6

(a) (5%) What output does this program produce? A A B B A B 7

(b) (10%) Draw an intermediate code tree for each of the following methods: i. (2%) Methodname in classa(ie,a.name). MOVE(TEMP r3, MEM(TEMP _this, 0)) ii. (2%) Methodname in classb(ie,b.name). MOVE(TEMP r3, MEM(TEMP _this, 4)) iii. (3%) MethodAname in classmain (ie,main.aname). MOVE(TEMP r3, CALL(MEM(MEM(TEMP _a, -4), 4), TEMP _a)) iv. (3%) MethodBname in classmain (ie,main.bname). MOVE(TEMP r3, CALL(MEM(MEM(TEMP _b, -4), 4), TEMP _b)) 8

(c) (10%) For the following, generate PowerPC instructions using maximal munch, circling the tiles and numbering them in the order that they are munched (ie, the order that instructions are emitted for the tile). Leave the temporary names in place (do not assign registers). i. (4%) Generate instructions for the tree of Question 3(b)ii. [2 = MOVE(TEMP r3, [1 = MEM(TEMP _this, 4)])] 1: lwz t0,4(_this) 2: mr r3,t0 ii. (6%) Generate instructions for the tree of Question 3(b)iv. [4 = MOVE(TEMP r3, [3 = CALL([2 = MEM([1 = MEM(TEMP _b, -4)], 4)], TEMP _b)])] 1: lwz t0,-4(_b) 2: lwz t1,4(t0) 3: mr r3,_b mtctr t1 bctrl 4: mr r3,r3 9

4. (Control flow graphs, liveness analysis, register allocation; 40%) The following program has been compiled for a machine with 2 registers: r 1 : a callee-save register r 2 : a caller-save argument/result register L 0 : L 1 : s r 1 a r 2 i a i i 1 if i < 0goto L 1 a i r 2 a call f (user 2,defr 2 ) i i 1 if i 0goto L 0 r 1 s return (user 1,r 2 ) (a) (5%) Draw the control flow graph for this program, with nodes that are the program s basic blocks (ie, not individual instructions) and with edges representing the flow of control between the basic blocks. s := r1 a := r2 i := a i := i - 1 if i < 0 goto L1 -----+ v L0: a := i <-----------+ r2 := a call f i := i - 1 if i >= 0 goto L0 --+ v L1: r1 := s <-------------+ return 10

(b) (10%) Annotate each instruction with the variables/registers live-out at that instruction. L 0 : L 1 : s r 1 a r 2 i a i i 1 if i < 0goto L 1 a i r 2 a call f i i 1 if i 0goto L 0 r 1 s return Def Use LiveOut Def Use LiveOut s r 1 s r 1 sr 2 a r 2 a r 2 sar 2 i a i a sir 2 i i 1 i i sir 2 if i < 0goto L 1 i sir 2 L 0 : a i a i sai r 2 a r 2 a sir 2 call f r 2 r 2 sir 2 i i 1 i i sir 2 if i 0goto L 0 i sir 2 L 1 : r 1 s r 1 s r 1 r 2 return r 1 r 2 11

(c) (10%) Fill in the following adjacency table representing the interference graph for the program; an entry in the table should contain an if the variable in the left column interferes with the corresponding variable/register in the top row. Since machine registers are pre-colored, we choose to omit adjacency information for them. Naturally, you must still record if a non-precolored node interferes with a pre-colored node; the columns for pre-colored nodes are there for that purpose. Also, record the unconstrained move-related nodes in the table by placing an in any empty entry where the variable in the left column is the source or target of any move involving the variable/register in the top row. Remember that nodes that are moverelated should not interfere if their live ranges overlap only starting at the move and neither is subsequently redefined. s i a r 1 r 2 s i a r 1 r 2 s i a s i a 12

(d) (15%) Show the steps of a coalescing graph-coloring register allocator as it assigns registers to the variables in the program. Use the George criterion for coalescing nodes: node a can be coalesced with node b only if all significant-degree (ie, degree >= K) neighbors of a already interfere with b. Show the final program, noting any redundant moves. The flow diagram for iterated register coalescing is included here for your reference. SSA constant propagation (optional) build simplify conservative coalesce freeze potential spill select done actual spill spills any i. Coalesce a with r 2 (or a with i) using George criterion (all of a s neighbors already conflict withr 2 ): r 1 r 2 a s i s i ii. Cannot coalesce s since i does not interfere withr 1 ; cannot freeze a since it is high-degree; so potential spill s (fewest uses/defs) r 1 r 2 a i i iii. Simplify i 13

iv. Select i r 1 r 1 r 2 a i i v. No color for s actual spill. Rewrite code, retaining coalescences from before spill: Def Use LiveOut M [loc s ] r 1 r 1 r 2 i r 2 i r 2 ir 2 i i 1 i i ir 2 if i < 0goto L 1 i ir 2 L 0 : r 2 i r 2 i ir 2 call f r 2 r 2 ir 2 i i 1 i i ir 2 if i 0goto L 0 i ir 2 L 1 : r 1 M [loc s ] r 1 r 1 r 2 return r 1 r 2 vi. New adjacency table: r 1 r 2 i i vii. Simplify i viii. Select i r 1 ix. Resulting code: M [loc s ] r 1 r 1 r 2 r 1 r 1 1 ifr 1 < 0goto L 1 L 0 : r 2 r 1 call f r 1 r 1 1 ifr 1 0goto L 0 L 1 : r 1 M [loc s ] return 14

15

16