CS143 Handout 25 Summer 2012 August 6 th, 2011 Programming Project 4: TAC Generation

Similar documents
PP6: Register Allocation - Optimization

Decaf PP2: Syntax Analysis

CS143 Handout 13 Summer 2011 July 1 st, 2011 Programming Project 2: Syntax Analysis

CS143 Handout 05 Summer 2011 June 22, 2011 Programming Project 1: Lexical Analysis

PP3-4: Semantic Analysis

Programming Project 4: COOL Code Generation

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

Programming Assignment IV Due Thursday, June 1, 2017 at 11:59pm

Three-Address Code IR

IR Generation. May 13, Monday, May 13, 13

6.001 Notes: Section 15.1

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

IRIX is moving in the n32 direction, and n32 is now the default, but the toolchain still supports o32. When we started supporting native mode o32 was

Programming Assignment III

Programming Assignment IV Due Monday, November 8 (with an automatic extension until Friday, November 12, noon)

CS 553 Compiler Construction Fall 2007 Project #1 Adding floats to MiniJava Due August 31, 2005

Programming Assignment IV

CS 6353 Compiler Construction Project Assignments

Important Project Dates

Project 5 - The Meta-Circular Evaluator

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Project 4 Due 11:59:59pm Thu, May 10, 2012

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS131 Compilers: Programming Assignment 2 Due Tuesday, April 4, 2017 at 11:59pm

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

Compilers Project 3: Semantic Analyzer

EECS483 D6: Project 3 Overview

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

CS 2210 Programming Project (Part IV)

COMP 105 Homework: Type Systems

Project 5 Due 11:59:59pm Tue, Dec 11, 2012

CS 6353 Compiler Construction Project Assignments

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Final Project: LC-3 Simulator

Project Compiler. CS031 TA Help Session November 28, 2011

Programming Assignment Multi-Threading and Debugging 2

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

CS 553 Compiler Construction Fall 2009 Project #1 Adding doubles to MiniJava Due September 8, 2009

Project 5 - The Meta-Circular Evaluator

6.001 Notes: Section 6.1

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

Programming Assignment I Due Thursday, October 7, 2010 at 11:59pm

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

P2: Collaborations. CSE 335, Spring 2009

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CSE 401/M501 Compilers

Project Data: Manipulating Bits

A Tour of the Cool Support Code

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

6.001 Notes: Section 8.1

Programming Project II

CIS 505: Software Systems

The print queue was too long. The print queue is always too long shortly before assignments are due. Print your documentation

Slide 1 CS 170 Java Programming 1 Testing Karel

Chapter 1 Getting Started

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

HOT-Compilation: Garbage Collection

Introduction to Programming System Design CSCI 455x (4 Units)

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Code Generation. Lecture 12

Notes to Instructors Concerning the BLITZ Projects

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

a translator to convert your AST representation to a TAC intermediate representation; and

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

INTERMEDIATE REPRESENTATIONS RTL EXAMPLE

COMP 40 Assignment: Interfaces, Implementations, and Images

Short Notes of CS201

Introduction to Programming Using Java (98-388)

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Practical 2: Ray Tracing

Lab 4: On Lists and Light Sabers

CS201 - Introduction to Programming Glossary By

printf( Please enter another number: ); scanf( %d, &num2);

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

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Programming Assignment I Due Thursday, October 9, 2008 at 11:59pm

Lab 4 - Linked Lists

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

Code Generation. Lecture 19

Skill 1: Multiplying Polynomials

Who are we? Andre Platzer Out of town the first week GHC TAs Alex Crichton, senior in CS and ECE Ian Gillis, senior in CS

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

CSCI 2321 (Computer Design), Spring 2018 Homework 3

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

CS 553 Compiler Construction Fall 2006 Project #4 Garbage Collection Due November 27, 2005

EE 352 Lab 3 The Search Is On

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

Midterm II CS164, Spring 2006

C152 Laboratory Exercise 1

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Important Project Dates

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

Project 5: Extensions to the MiniJava Compiler

CS164: Midterm Exam 2

Transcription:

CS143 Handout 25 Summer 2012 August 6 th, 2011 Programming Project 4: TAC Generation The Goal In the final project, you will implement a back end for your compiler that will generate code to be executed on the SPIM simulator. Finally, you get to the true product of all your labor running Decaf programs! This pass of your compiler will traverse the abstract syntax tree, stringing together the appropriate TAC instructions for each subtree to assign a variable, call a function, or whatever is needed. Those TAC instructions are then translated into MIPS assembly via a translator class we provide that deals with the more grungy details of the machine code. Your finished compiler will do code generation for all of the Decaf language (with a few minor omissions) as well as reporting link and run time errors. Students generally find PP4 to be close to PP3 in terms of difficulty and time consumption. We tried to give you a hand by giving you some of the pieces pre written and littered our code with debug printing and helpful assertions. The debugging can be intense at times since you may need to drop down and examine the MIPS assembly to sort out the errors. By the time you're done, you'll have a pretty thorough understanding of the runtime environment for Decaf programs and will even gain a little reading familiarity with MIPS assembly. The back end of the compiler is where the systems specific portion of the work comes in, so this is the project many of you have been waiting for. I think this is definitely the most fun of the projects; it s an awesome feeling when you finally get to the stage where you can compile Decaf programs and execute them. Try not to get so distracted playing Blackjack that you forget to submit your assignment on time! Starter Files Due: Saturday, August 18 th at 11:30AM No Late Submissions The starting files are in the /usr/class/cs143/assignments/pp4 directory. The project contains the following files (the boldface entries are the ones you are most likely to modify, although depending on your strategy you may modify others as well):

Makefile main.cc scanner.h/l parser.y ast.h/.cc ast_type.h/.cc ast_decl.h/.cc ast_expr.h/.cc ast_stmt.h/.cc codegen.h/.cc tac.h/.cc mips.h/.cc errors.h/.cc hashtable.h/.cc list.h location.h utility.h/.cc samples/ run builds project main and some helper functions our scanner interface/implementation bison parser; replace with your own interface/implementation of base AST node class interface/implementation of AST type classes interface/implementation of AST declaration classes interface/implementation of AST expression classes interface/implementation of AST statement classes interface/implementation of CodeGenerator class interface/implementation of Tac class and subclasses interface/implementation of our provided TAC to MIPS translator error reporting class for you to use simple hashtable template class simple list template class utilities for handling locations, yylloc/yyltype interface/implementation of our provided utility functions directory of test input files script to compile and execute result on SPIM simulator Copy the entire directory to your home directory. Use make to build the project. It reads input from stdin and you can use standard UNIX file redirection to read from a file and/or save the output to a file: % dcc < samples/program.decaf > program.asm The output is MIPS assembly that can be executed on the SPIM simulator. The spim executable is in /usr/class/cs143/bin. There is also an xspim visual version. You can either invoke using the full path or add our bin directory to your path to use the short name. The -file argument to either allows you to specify a file of MIPS assembly to execute. For your convenience, we provide a run script that will do the steps in sequence. You invoke it with one argument, the path to the Decaf input file:

3 -- dcc < samples/t1.decaf >tmp.asm -- spim -file tmp.asm SPIM Version 6.3a of January 14, 2001 Copyright 1990-2000 by James R. Larus (larus@cs.wisc.edu). All Rights Reserved. See the file README for a full copyright notice. Loaded: /usr/class/cs143/lib/trap.handler hello world As always, the first thing to do is to carefully read all the files we give you and make sure you understand the lay of the land. This is particularly important here since there is a chunk of new code in the project. A few notes on the starter files: You are given the same parse tree node classes as before. Your job is to add Emit instructions to the nodes, implemented using the same virtual method business you used for Print and Check in previous programming projects. You can decide how much of your PP3 semantic analysis you want to incorporate into your pp4 project. We will not test on Decaf programs with semantic errors, so you are free to disable or remove your semantic checks so as to allow you to concentrate on your new task without the clutter. You are to replace parser.y with your own parser (or our reference parser from PP3). You should not need to make changes to the parser or rearrange the grammar, but you can if you like. We have removed doubles from the types of Decaf for this project. In the generated code, we treat bools just like ordinary 4 byte integers, which evaluate to 0 or not 0. The only strings are string constants, which will be referred to via pointers. Arrays and objects (variables of class type) are also implemented as pointers. That means all variables/parameters are 4 bytes in size that simplifies calculating offsets and sizes. Interfaces are removed for code generation to simplify management of the vtable and dynamic dispatch. You are not required to generate code for method calls on objects upcasted to interface types. However, getting this working (perhaps by using multiple vtables and vtable deltas) would be an excellent way to earn some extra credit! The CodeGenerator class has a variety of methods that can be called to create TAC instructions and append them to the list so far. Each instruction is an object of one of the subclasses declared in tac.h. The CodeGenerator supports some basic instructions, but you will need to augment it to generate instruction sequences for the fancier operations (array indexing, dynamic dispatch, etc.) The Mips class, which we provide, is responsible for converting the list of TAC instruction objects as part of final code generation. This class encapsulates the details of the machine registers and instruction set and can translate each instruction into its MIPS equivalent. You will not likely need to make changes to this class.

4 CodeGenerator Implementation For the suggested checkpoint, you need to implement code generation for a single main function without arrays or objects. Here is a quick sketch of a reasonable order of attack: Before you begin, go back over the TAC instructions and examples in the TAC Handout to ensure you have a good grasp on TAC. Also read the comments in the starter files to familiarize yourself with the CodeGenerator, Tac, and Mips classes we provide. Plot out your strategy for assigning locations to variables. A Location object is used to identify a variable's runtime memory location, i.e., which segment (stack vs. global) and the offset relative to the segment base. Every variable, be it global or local, a parameter or a temporary, will need to have an assigned location. Figure out how/when you will make the assignment. As a first step, you may want to print out each location before doing any code generation and verify all is well. If you aren't sure you have the correct locations before you move on to generating code, you're setting yourself up for trouble. Once you have assigned locations for all variables located within the stack frame, you can calculate the frame size for the entire function, and backpatch that size into BeginFunc instruction. The label for the main function has be exactly the string "main" in order for spim to start execution properly. Start by generating code to load constants and add support for the built in Print so you can verify you've got this part working. Simple variables and assignment make a good next step. Generate instructions for arithmetic, relational, and logical operators. Note that TAC only has a limited number of operators, so you must simulate the others from the available primitives. In the past, students seem tempted toward complex implementations involving strange branches and IfZ/Goto, but there are simple, straightforward solutions if you think carefully about it. Generating code for the control structures (if/while/for) will teach you about labels and branches. Correct use of the break statement should work for exiting while and for loops. Take note of what Decaf built ins are available and how each is used. A few trouble spots in the past for students have been making sure Booleans print as true/false (not 0/1) and that == on strings compares the characters for equality, not just the pointers. At this point, you should be able to handle any sequence of statements in a single main function (not including arrays and objects). Try finishing everything up to this point by Friday, August 10.

Going on you will finish the rest of code generation, which includes these tasks: To handle multiple functions, you now need to assign locations to the function parameters and figure out your strategy for assigning function labels. We suggest using the function name prefixed with some number of underscores as the function label and for classes to further prefix with the class name. You're welcome to use any scheme you like as long as it works (i.e., assigns unique labels with no confusion). Be careful not to have name collisions with our built in routines! Also, be careful not to use labels that have the same name as a MIPS instruction; if you add prefixes to all of the function names, this should not be a problem. Plan your array layout. Remember that your generated code is responsible for tracking the array length. Where will you store that? When is the length set? How do you access it? Once you have a strategy, implement the built in NewArray and the length() accessor. Add a runtime check that rejects an attempt to create an array without a positive number of elements, printing the message Decaf runtime error: Array size is <= 0 and halting execution. (Templates for the error messages are provided in errors.h) Code generation for array elements requires computing offsets and dereferencing. Be careful to consider both the case when the array element is being read and the case when it is being written. Include a runtime check for array subscripting that verifies that the index is in bounds for the array. If an attempt is made to access an out of bounds element, at runtime you should print the message Decaf runtime error: Array subscript out of bounds and halt execution. Now consider how you will configure objects in memory in particularly, think through how you will access instance variables and implement dynamic dispatch. Sketch some pictures and be sure to consider how inheritance will be supported. With your plan in hand, figure out how you will assign locations to instance variables and methods. Add code to generate the class vtable. Add implementation for the new built in, taking care to generate the necessary code to set up the new object's vtable pointer. Code generation for instance variable access is somewhat similar to array element access in that it involves loads and stores with offsets. It might help to suspend semantic processing while testing (i.e. act as though all object fields are public) so that you can directly read and write the fields of an object from the main function. Method calls are handled similarly to function calls, but dynamic dispatch and the hidden receiver argument adds some complication. Refer back to the earlier object pictures on the lecture slides to ensure you understand what code must be generated to jump to the correct method implementation. Remember that there is an 5

additional argument "this" that needs to be passed as a behind the scenes parameter when generating code for a method call. In the context of a method body, you will need to synthesize a location for the identifier "this" at the offset for where the parameter can be found. You should add one piece of "linker" like functionality to verify that there is a definition for the global function main. The error reported when the program contains no main is: *** Linker: function 'main' not defined If there is a link error, no code should be emitted. 6 Hints and suggestions Just a few details that didn t fit anywhere else: The debug key tac can be used to skip final MIPS code generation and just print the TAC instructions. The debug flag can be set with d tac when invoking the program or programmatically via SetDebugForKey. This is quite useful when you are developing. Note that it is not expected that your instruction sequence exactly match ours. Depending on your strategy, you can get many functionally equivalent results from different sequences. We will not be using diff on the TAC or MIPS sequences, only on the spim output. There are links to documentation and resources for the SPIM simulator on the right hand side of the CS143 website. We included comments in the header files to give an overview of the functionality in our provided classes but if you find that you need to know more details, don't be shy about opening up the.cc file and reading through the implementation to figure it out. You can learn a lot by just tracing through the code, but if you can't seem to make sense of it on your own, you can send us email or come to office hours. Testing There are various test files that are provided for your testing pleasure in the samples directory. For each Decaf input test file, we also have provided the output from executing that program s object code under spim. There are many different correct ways of sequencing the instructions, so it s not helpful to compare TAC/MIPS outputs, but the runtime output should match. Be sure to test your program thoroughly, which will certainly involving making up your own additional tests. We will test your compiler only on syntactically and semantically valid input. We will expect your final submission to report errors only for the runtime and linking errors specified above.

Grading The final submission is worth 20% of your overall grade in the course. We will thoroughly test your submission against many samples. We will run the object code produced your compiler on the spim simulator and diff against the correct runtime output. 7 Once you have finished this assignment, you'll have built a complete working compiler in a fast paced eight weeks. Congratulations this is no mean feat! There are many opportunities for extra credit on this assignment, and we'd be more than happy to reward you for going above and beyond what's required. Here are a few extensions you can add into your code generator that would be really, really cool: Support interfaces. We don't ask you to implement interfaces because of the complexities involved in adjusting object pointers. However, coding up support for interfaces will give you a much deeper understanding of how object layouts work in major programming languages. Add optimization. Your generated code needn't be efficient for this assignment, but that doesn't mean that you can't try to optimize your generated IR. Any optimizations that you do, provided that they are correct, will earn extra credit, as long as you document it in your README. Improve our code generator. Our code generator does a reasonable job managing registers and the stack, but perhaps you can do better! Feel free to tweak our code generator to make it more awesome. For major extra credit and lasting fame, see if you can tweak it to output x86 assembly. Support switch and postfix expressions. In pp2 we asked you to add in a switch statement and postfix ++ and operators. Why not add them to the code generator? Support doubles. We removed the double type from this assignment to simplify the code generator logic. Try adding them back in! Good luck!