Homework I - Solution

Similar documents
In Java we have the keyword null, which is the value of an uninitialized reference type

Heap Arrays. Steven R. Bagley

Class Information ANNOUCEMENTS

Pointer Analysis for C/C++ with cclyzer. George Balatsouras University of Athens

CS 432 Fall Mike Lam, Professor. Code Generation

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Short Notes of CS201

Name: CIS 341 Final Examination 10 December 2008

CS201 - Introduction to Programming Glossary By

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

Heap Arrays and Linked Lists. Steven R. Bagley

CS201- Introduction to Programming Current Quizzes

CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley

CS61, Fall 2012 Section 2 Notes

Lecture 3: C Programm

Dynamic Allocation in C

CSE351 Winter 2016, Final Examination March 16, 2016

Review of the C Programming Language for Principles of Operating Systems

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Compiler Construction: LLVMlite

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Midterm II CS164, Spring 2006

Intermediate Code Generation

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

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

COMP 524 Spring 2018 Midterm Thursday, March 1

Review of the C Programming Language

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

Name, Scope, and Binding. Outline [1]

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

CSE 504: Compiler Design. Runtime Environments

Names, Scope, and Bindings

CSC C69: OPERATING SYSTEMS

CIS 341 Final Examination 3 May 2011

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Lexical Considerations

A program execution is memory safe so long as memory access errors never occur:

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CS143 Compilers - Written Assignment 4

Inheritance, Polymorphism and the Object Memory Model

COMP-520 GoLite Tutorial

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

CS143 Final Spring 2016

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Code Generation. Lecture 12

ESC101N: Fundamentals of Computing End-sem st semester

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

211: Computer Architecture Summer 2016

CS 2113 Software Engineering

CSE351 Spring 2018, Final Exam June 6, 2018

CSC 211 Intermediate Programming. Arrays & Pointers

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Lecture 2: C Programm

Dynamic Data Structures. CSCI 112: Programming in C


11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Sequence 5.1 Building stack frames in LLVM

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Quiz 0 Review Session. October 13th, 2014

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Lecture 23 CIS 341: COMPILERS

Names, Scope, and Bindings

Dynamic Allocation in C

Java and C CSE 351 Spring

C++ Programming: Polymorphism

Static Program Analysis Part 1 the TIP language

CS143 - Written Assignment 4 Reference Solutions

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

2. Reachability in garbage collection is just an approximation of garbage.

Code Generation. Lecture 19

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

C Programming Basics II

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

LLVM and IR Construction

Java Internals. Frank Yellin Tim Lindholm JavaSoft

Qualifying Exam in Programming Languages and Compilers

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

Dynamically Allocated Memory in C

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CSc 453 Interpreters & Interpretation

Dynamic Memory Allocation

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017

CSE 401 Final Exam. December 16, 2010

CS 11 C track: lecture 5

Java byte code verification

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Homework #3 CS2255 Fall 2012


Memory management. Johan Montelius KTH

C++ for Java Programmers

CA31-1K DIS. Pointers. TA: You Lu

Transcription:

CS 426 Fall 2017 1 Homework I - Solution Homework I - Solution CS 426 Compiler Construction Fall Semester 2017 1. (50 points) Intermediate representations: (a) (25 points) Construct a Control-Flow Graph (CFG) for code fragments 2 and 3. Use maximallength basic blocks, i.e., each basic block should include as many statements as possible. You do not need to show the statement code within the basic blocks; just label each block with the line number of the first statement. If more than one basic blocks start on the same line L, you may label the first statement of each basic block and use the label in conjuction with L. You may use any other notation, as long as it is clear. (b) (25 points) Convert code fragments 1 and 2 into a Static Single Assignment (SSA) representation. Assume SSA form uses minimal (but not pruned) φ functions. You don t need to show the CFG, but remember the cardinal rule: always draw the CFG when converting non-trivial code to SSA form (1) // Code Fragment 1 (C) (2) X = 1; (3) Y = 0; (4) Z = 0; (5) do { (6) Z = Z + 1; (7) Y = Y + Z; (8) if (Z < X) (9) continue; (10) do { (11) Z = Z - 1; (12) if (Z > 0) (13) continue; (14) break; // exits innermost do while loop (15) } (16) while ( Z <= 65535); (17) if (X > 1024) (18) break; // exits innermost do while loop (19) X = X * 2; (20) } (21) while (Y <= 65535); (22) printf("%d\n", Y); SSA for code fragment 1: Entry: X0 = 1 Y0 = 0 Z0 = 0 Loop1: X1 = Phi(X0, X3) Y1 = Phi(Y0, Y2) Z1 = Phi(Z0, Z5) Z2 = Z1 + 1 Y2 = Y1 + Z2

CS 426 Fall 2017 2 Homework I - Solution jmp if (Z2 < X1) Test1 else Loop2 Loop2: Z3 = Phi(Z2, Z4) Z4 = Z3-1 jmp if (Z4 > 0) Test2 else Done2 Test2: jmp if (Z4 <= 65535) Loop2 else Done2 Done2: jmp if (X1 > 1024) Done1 else Branch1 Branch1: X2 = X1 * 2 Test1: X3 = Phi(X1, X2) Z5 = Phi(Z2, Z4) jmp if (Y2 <= 65535) Loop1 else Done1 Done1: X4 = Phi(X3, X1) Z6 = Phi(Z5, Z4) printf("%d\n", Y2) We assumed that the predecessors of Done1 are Test1 and Done2 in this order. (1) // Code Fragment 2 (C) (2) i = 0; (3) j = 1024; (4) k = 0; (5) for(i = 0; i < j; ++i){ (6) switch(i%3) { (7) case 0: (8) k += i; (9) break; (10) case 1: (11) k += i*2; (12) break; (13) default: (14) printf("%d\n", i); (15) break; (16) } (17) continue; (18) i *= 4; (19) }

CS 426 Fall 2017 3 Homework I - Solution (1) // Code Fragment 3 (Java) (2) int[] array = new int[3]; (3) try{ (4) for(int i=0;i<4;++i){ (5) array[i] = i; (6) } (7) System.out.println(array); (8) } (9) catch(arrayindexoutofboundsexception e){ (10) System.out.println("Array out of bounds!"); (11) } (12) finally{ (13) System.out.println(array); (14) }

CS 426 Fall 2017 4 Homework I - Solution 2. (20 points) Runtime environments: For this question, you must identify the user-visible memory objects created by the code below, along with their storage classes and creation times. The user-visible memory objects in a C program include functions, variables, and dynamically allocated memory blocks created by alloca() or malloc(). Some user-visible objects that may not be obvious are noted in the comments below. You should list every memory object created when compiling, linking, loading, or executing this program. Omit any objects outside the code that is shown. Give your answer in the form of a table with one row per object, and with the following 3 columns: (1) Objects : The name of the object. Make sure you distinguish a pointer variable from an object it may point to, e.g., ptr vs. Objects2. (2) Memory Region : The memory region in which the object is allocated. The choices: { Code, Initialized Globals, Uninitialized Globals, Stack, Heap } (3) Creation Time : The step in the compilation/execution process at which the object is created and initialized. Any object that must be included in the executable file is created at link time. Distinguish objects created at function entry from those created when executing a particular statement within the function. Therefore, the choices are: { Link time, Load time, Function entry, Statement runtime }. Note #1: Ignore all optimizations performed by the compiler that may affect the answers. Note #2: You must provide answers for all user-visible objects, and not just the ones identified in comments.

CS 426 Fall 2017 5 Homework I - Solution #define FixedSize 100 int Size = FixedSize; double GlobalStore[FixedSize]; void dummy(struct Bag* B, int N) { /* dummy */ static double SaveSum = 0.0; int i; double x; double(*fp)(double); struct Contents *lists, *nextsptr, *nexthptr; fp = B->F; x = fp(globalstore[n]); lists = (struct Contents*) alloca(sizeof(struct Contents)); /* Object1 */ nexthptr = 0; nextsptr = 0;... for (i=0; i < N; ++i) { struct Contents *ptr; /* ptr */ ptr = (struct Contents*) malloc(sizeof(struct Contents)); /* Objects2 */ ptr->next = nexthptr; nexthptr = ptr; } ptr = (struct Contents*) alloca(sizeof(struct Contents)); /* Objects3 */ ptr->next = nextsptr; nextsptr = ptr; }... B->list = nexthptr;... Object Name Memory Region Creation Time Size global initialized link time GlobalStore global uninitialized link time or load time* Dummy code link time B stack function entry N stack function entry SaveSum global initialized** link time i stack function entry x stack function entry fp stack function entry lists stack function entry nextsptr stack function entry nexthptr stack function entry Object1 stack statement runtime ptr stack function entry Object2 heap statement runtime Object3 stack statement runtime Last two objects are allocated each time through the loop. * The address is created at link time, but the actual memory is not laid out until load time. We accepted either answer. ** Initializer is all zero bytes, so may be allocated in uninitialized memory.

CS 426 Fall 2017 6 Homework I - Solution 3. (18 points) Code generation and OO dispatch: During dynamic method dispatch in COOL, several operations are performed. Some properties of these operations depend on the static type of the receiver reference, some depend on the dynamic type of the object, and some are independent of the type (i.e., identical for all classes). Ignore SELF TYPE throughout this question; it actually does not affect your answers. Assume no special optimizations are performed for method dispatch, i.e., focus on the code generated by Intermediate Code Generation. For each of the following properties, state which it depends on: Static Type, Dynamic Type, Neither (a) The numbers and types of arguments for a method Static Type (b) The address of the function to call Dynamic Type (c) The vtable to use for the call Dynamic Type (d) The offset (or field index in LLVM) of the vtable pointer within the object struct Neither (e) The offset (or field index in LLVM) of the function pointer entry within the vtable struct Static Type (f) The number of load instructions that must be executed before making the call Neither 4. (12 points) Compiler Construction: For each of the following actions, state whether it happens at compiler construction time (i.e., when developing the compiler), at compile time (i.e., when compiling or linking a program), or at run-time (i.e., when executing the code generated by the compiler). In some cases, multiple answers are possible (i.e,. an action can happen at either of two different times in different situations). For such cases, state why in the space provided. For example, Evaluating an arithmetic expression can happen: at compile-time, if all operands have known constant values, at runtime otherwise. There are no trick questions here. Consider only normal scenarios discussed in class. (a) Assign address range for a global variable. Compile time (b) Compute address of a local variable. Runtime (c) Encounter a shift-reduce conflict. Compiler construction time (d) Choose in which area of memory (static data, heap, or stack) a particular variable will be stored. Compile time (e) In a language with strong type safety (like Java), check that the type of an operand can be correctly converted (i.e., type-cast) to the type of the result variable for a particular operation. Compile-time, if it is an upcast OR if the dynamic type of the operand object can be inferred. Run-time, otherwise

CS 426 Fall 2017 7 Homework I - Solution (f) Allocate storage for an activation record. Runtime