CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Similar documents
CS164: Programming Assignment 4 includes Written Assignment 7! SkimDecaf Semantic Analysis and Code Generation

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

1 Lexical Considerations

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

Lexical Considerations

Run-time Environments

Run-time Environments

A Short Summary of Javali

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Assignment IV Due Thursday, November 18th, 2010 at 11:59 PM

Class Information ANNOUCEMENTS

Intel assembly language using gcc

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Project 5: Extensions to the MiniJava Compiler

CIT Week13 Lecture

Assignment 11: functions, calling conventions, and the stack

Lexical Considerations

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

x86 assembly CS449 Fall 2017

The Decaf Language. 1 Lexical considerations

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Short Notes of CS201

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Assembly Language: Function Calls" Goals of this Lecture"

You may work with a partner on this quiz; both of you must submit your answers.

CS201 - Introduction to Programming Glossary By

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Final CSE 131B Spring 2004

The SPL Programming Language Reference Manual

CS558 Programming Languages

Assembly Language: Function Calls" Goals of this Lecture"

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CS241 Computer Organization Spring 2015 IA

The PCAT Programming Language Reference Manual

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

Code Generation & Parameter Passing

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

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Ch. 11: References & the Copy-Constructor. - continued -

Programming Project 4: COOL Code Generation

Semantic Analysis. Lecture 9. February 7, 2018

CS164: Midterm Exam 2

Semantic Analysis and Type Checking

Object-oriented features

Assembly Language: Function Calls

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CS 314 Principles of Programming Languages. Lecture 11

CSE-304 Programming Assignment #4

Operational Semantics. One-Slide Summary. Lecture Outline

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

KU Compilerbau - Programming Assignment

CSE P 501 Exam 8/5/04

Java Primer 1: Types, Classes and Operators

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Important Project Dates

Compiler construction 2009

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

x86 assembly CS449 Spring 2016

Examining the Code. [Reading assignment: Chapter 6, pp ]

CS 6353 Compiler Construction Project Assignments

CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Reference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.

CSE 401/M501 Compilers

CS201- Introduction to Programming Current Quizzes

CS164: Final Exam. Fall 2003

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

CS61 Section Solutions 3

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Digital Forensics Lecture 3 - Reverse Engineering

CS4120/4121/5120/5121 Spring /6 Programming Assignment 4

CSE 401 Final Exam. December 16, 2010

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

Lecture 3: C Programm

// the current object. functioninvocation expression. identifier (expressionlist ) // call of an inner function

Run-time Environments - 3

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

CS 2505 Computer Organization I

The Decaf language 1

IC Language Specification

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Winter Compiler Construction Who. Mailing list and forum

Java: introduction to object-oriented features

G Programming Languages - Fall 2012

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

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

Compiler construction assignments

CS558 Programming Languages

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Procedure and Object- Oriented Abstraction

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Transcription:

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation Assigned: Sunday, November 14, 2004 Due: Thursday, Dec 9, 2004, at 11:59pm No solution will be accepted after Sunday, Dec 12, 11:59pm Start early, or even earlier! December 3, 2004 1 Introduction Overview In this assignment, you will complete the implementation of Decaf. Building on your solution from PA4, where you implemented a SkimDecaf compiler, you will now implement both the semantic analyzer and the code generator for the object-oriented features of Decaf. The assignment has again been broken into several steps, however, you don t need to adhere to these steps religiously. Still, implementing the Decaf features in the suggested order will improve your score if you happen to run out of time (PA5 this is the most complex of the five assignments, and, as in PA4, we are giving you virtually no starter kit). The recommended steps are: 1. You will first implement object creation (the new operator). 2. Next, you will implement access to instance (i.e., non-static) fields. 3. After that, you should implement instance (i.e., non-static) methods. 4. Next, you should add support for inheritance. 5. Finally, you should add semantic checks to handle incorrect Decaf programs. On this project, you will work in pairs or alone. You are allowed to form different teams than those in previous programming assignments, but you are not allowed to disband a team in the middle of a project assignment. You must submit a file named PARTNERS, which should list the login names of the both partners. The PARTNERS file has a specific format, which you must follow exactly. See Handing in the Assignment at the end of this document for details. The failure to submit the PARTNERS file will result in a (small) penalty. 1

Optimania. As promised in PA4 handout, PA5 will include an optional Optimania contest in which you can earn extra credits for fast-running code (only if this code is also correct). You will submit your optimizing compiler for the contest as a separate assignment (called PA6) a day or two after PA5 is due (details to follow). Be careful: Grading of PA5 is going to be very strict as to the correctness of the generated code. So, if your optimizer and optimized code generator is not fully debugged, don t submit it as part of your PA5 solution. Note: To participate in Optimania, we will require that your compiler supports an input language construct, which reads an integer value from the standard input (assume that the input will contain exactly one integer per line.) The input operator behaves like a call to a static method: int x; x = input();. To implement the input operator, use the C function scanf. You need to generate code similar to the x86 code fragment shown below. In case you are curious, the fragment below comes from code generated by gcc from the C program main() int temporaryvar; scanf("%d", &temporaryvar);..lc0:.section.string "%d"....rodata leal movl movl call -4(%ebp), %eax %eax, 4(%esp) $.LC0, (%esp) scanf Some explanation is in order. scanf takes two arguments: the first is a format string (as in printf, which you used to implement Decaf s print); the second is an address of a memory location, where scanf will store the integer read from the standard input. In the code above, this locations -4(%ebp); you will want to replace -4(%ebp) with the address of a temporary generated by your compiler. What you are going to learn and why it is useful First, you are going to learn how to generate code for an object-oriented program. In doing so, you will learn why object-oriented programs run slower than their imperative counterparts. This knowledge is useful for each modern programmer, as it allows her to write a more efficient Java code. Second, you will learn how an object-oriented compiler typechecks the program. This will reinforce your understanding of what kind of programming mistakes the compiler is capable of detecting (at compile time). 2

2 Getting the Project Files The starter kit is available on the web at: http://www-inst.eecs.berkeley.edu/ cs164/starters/pa5.zip You can also get the starter kit from your Unix account at: cs164/public html/starters/pa5.zip. Usage of the starter kit is the same as PA1. Please to refer to the PA1 handout for information on how to import the zip file into a new project. The starter kit is minimal. Essentially, it contains only a file with print statements for the semantic checks. Note that we are not providing a solution to PA4. Please use the remote tester to fix the bugs in your PA4 solutions. You can find the solutions to PA1 3 in the start kit for PA4. 3 Decaf Language Specification In this programming assignment, you are going to implement a complete compiler for Decaf. You will build upon your SkimDecaf compiler from PA4. In PA5, you will implement those Decaf features that are outside the SkimDecaf language. This section defines the language features that you are to implement in this assignment. First, we outline Decaf s syntactic features that SkimDecaf didn t support and hence you have to implement them now. For details of Decaf grammar, refer to the Decaf grammar in PA3. A Decaf program may contain multiple classes. Decaf supports both static and instance fields. Same for methods. Recall that the Decaf grammar in PA3 doesn t allow static methods; the support for static methods was added to SkimDecaf and Decaf in PA4. A Decaf program may contain the dot operator, which is used to access fields and to invoke methods. Decaf supports the new T() operator, which creates a new object of type T on the heap. Note that Decaf doesn t support constructors. Decaf supports the this keyword. The keyword null is supported in Decaf. Decaf semantics. We now clarify some semantic properties of Decaf programs that are not specified by the Decaf grammar. Semantics not stated here is as in Java; when in doubt, ask the course staff. 3

As in SkimDecaf, a Decaf program s start point is the main method in class Program. A class name can be used before it is defined. All methods are assumed to be public. All fields are assumed to be protected. All instance methods have the implicit argument this, which takes the value of the receiver object. Argument evaluation in method calls proceeds left to right. If you implemented the opposite evaluation order in PA4 (because you push the last argument first), a relatively easy fix is to reverse the order of how you push arguments on the stack (pushing the first argument first is acceptable in Decaf, as Decaf doesn t support a variable number of arguments). A reference to an instance field of a class is legal only in instance methods of class, and is internally translated to this. A reference to an static field of a class is legal in both instance and static methods of class (and its subclasses), and is internally translated to T. The operands of EQOP s must have conforming types, but cannot be of type String. The type of the expression this is the lexically enclosing type. The null pointer is represented as a memory address with the value 0. Object fields are initialized to their default values when an object is constructed using new. Object locations (i.e., object references) have a default value of null, integers have default value of zero, and booleans have a default value of false. (Local variable are are initialized, too; see the handout for PA4.) Methods (both instance and static) cannot be overloaded. That is, no two methods in the same class can have the same name, regardless of their argument types and return types. Instance methods can be overridden. Instance fields cannot be overridden. Static fields and static methods are not inherited. If the same static or instance field name is defined in in multiple ancestor classes, the access to that field must refer the field defined in closest ancestor. class A { int x; } class B extends A { static int x; void foo() { x = 10; // accesses B s field x B.x = 5; // accesses the same field } } Also see Step 5 for semantic checks that you will have to implement. 4

4 Project Steps As in PA4, the starter kit contains nearly no skeleton code. The starter kit provides only one class, named DecafSemanticError, that you will have to use for printing type-checker s error messages. All provided files are described below. Requirements We are providing you with a Main class that should simplify the task of making your compiler run with the remote tester. For the record, what we require from the Main class file is that your compiler compiles a Decaf file into an assembly file. Specifically, when your compiler is invoked as java edu.berkeley.cs164.compiler.main decafpath assemblypath it must compile the file named decafpath into a file assemblypath. The former will typically have an extension.decaf, and the latter.s. In all steps, except the last one, assume that the input is a legal (well-typed) Decaf program. The last step will implement semantic checks to ensure that your compiler or generated code doesn t misbehave. Step 1 Object creation. Implement object creation, using the new operator. To create an object on the heap, use the C function malloc, which accepts as its only argument the number of bytes to allocate and returns the address of the allocated block of memory. It is the Decaf programmer (not the Decaf compiler or runtime) who is responsible for checking whether the return value of new is null. Decaf doesn t support memory management (automatic or manual), so you don t need to deallocate garbage objects. Step 2 Field access. Next, you will implement access to instance (i.e., non-static) fields. To access an instance field of an object, you may want to generate code similar to the following example, which computes the value of x.field, assuming that field is at byte offset 12 within its object and that x resides in memory location 8(%ebp) (i.e., this memory location contains the address of the object): movl movl addl 8(%ebp), %eax 12(%eax), %eax $13, %eax 5

Step 3 Non-static method invocation. Next, you should implement invocation instance (i.e., non-static) methods. To invoke an instance method of an object, you may want to generate code similar to the following example, which executes x.f(), assuming that f resides at byte offset 4 in the dispatch table and that x resides in memory location -4(%ebp): // move x to %eax movl -4(%ebp), %eax // load the address of the dispatch table into %edx movl (%eax), %edx // compute the address of f in the dispatch table addl $4, %edx // push the implicit argument (this) movl -4(%ebp), %eax movl %eax, (%esp) // get the address of f movl (%edx), %eax // call the procedure whose address is stored in %eax call *%eax To generate the dispatch table, use the.long directives, as shown in the code below, where ZTV3Foo is a label generated by the gcc compiler to refer to the address of the dispatch table for class Foo (when creating objects of type Foo), and ZN3Foo1fEv and ZN3Foo1gEv are labels generated by the gcc compiler for the instance methods f and g of the class Foo. _ZTV3Foo:.long.long _ZN3Foo1fEv _ZN3Foo1gEv Step 4 Inheritance. Next, you should add support for inheritance. If you implemented the previous three steps correctly, you should have to implement no new functionality here. Just make sure you test your compiler extensively on many (sufficiently) different Decaf programs that define subclasses. Step 5 Semantic checks. Finally, you should add semantic checks to handle incorrect Decaf programs. You will have to implement the semantic checks shown below. Recall that the grammar symbols denoted with SYMBOL refer to the Decaf grammar in the PA3 handout. A note on SkimDecaf semantic checks: Your PA5 compiler will have to perform also the SkimDecaf checks listed in the PA4 handout. Some of the PA4 checks will need to be refined! In particular, the 6

meaning of type conforms to type is different in PA5, because Decaf is an object-oriented language. We define a type as conforming to another type if or if is a (transitive) subclass of (PA4 defined conformance as ). The Decaf compile-time semantic checks (that are not supported by SkimDecaf ): 1. For all method invocations of the form simple expr. id () (a) If simple expr is a name of a class, then id must name one of s static methods. (b) If the type of simple expr is some class type,, then id must name one of s instance methods. (c) Otherwise, the expression simple expr. id () is not well typed. 2. For all locations of the form simple expr. id (a) If the type of simple expr is some class type,, then id must name one of s instance fields. (b) If simple expr is a name of a class, then id must name one of s static fields. (c) Otherwise the expression simple expr. id is not well-typed. 3. An instance or static field of class T can be accessed only from methods of T and from methods of subclasses of T (i.e., all fields are protected). 4. this can be used (implicitly or explicitly) only in instance methods. 5. An id used as a location must name a declared variable or field (e.g., it s illegal to use a method s name as a location). 6. In calls to new T(), T must be the name of a class. 7. The inheritance graph cannot contain a cycle. 8. The same name cannot be used for an instance field in two separate classes when one class is an ancestor of the other in the inheritance graph. 9. If T2 is a subclass of T1 and id is an instance method name defined in both T1 and T2, then the method must have exactly the same signature in T1 and T2. The method signature consists of the method s return type and the types of arguments, in the appropriate order. Note that if id is a static method, the signatures in T1 and T2 need not match. 10. The operands of EQOP s cannot be of type String. Printing the error messages. To print the error messages detected by your semantic checker, use the provided class DecafSemanticError. The run-time semantic check. In Decaf, one semantic check will be delayed to run time: 1. The value of a location must be non-null whenever it is dereferenced using the. operator. When this run-time errors occurs, your program must output to the standard output a message Null pointer dereference., after which the program terminates. Make sure that the message is spelled as shown above. You can terminate the program by calling the C function exit. 7

Handing in the Assignment The submission will proceed as in the previous assignments. Please check the web site for updates. Good luck! 8