CS 2210 Programming Project (Part IV)

Similar documents
We can emit stack-machine-style code for expressions via recursion

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Course Administration

CA Compiler Construction

Code Generation. Lecture 19

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Compilers and computer architecture: A realistic compiler to MIPS

CSE 504: Compiler Design. Runtime Environments

Assignment 11: functions, calling conventions, and the stack

CS 6353 Compiler Construction Project Assignments

Implementing Procedure Calls

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Run-time Environment

ECS 142 Project: Code generation hints

LAB C Translating Utility Classes

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Code Generation. Lecture 12

CS153: Compilers Lecture 8: Compiling Calls

Project Compiler. CS031 TA Help Session November 28, 2011

Chapter 2A Instructions: Language of the Computer

Functions in MIPS. Functions in MIPS 1

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

The Activation Record (AR)

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Compilers CS S-08 Code Generation

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

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Storage in Programs. largest. address. address

G Programming Languages - Fall 2012

Processor design - MIPS

EE 361 University of Hawaii Fall

Chapter 9 :: Subroutines and Control Abstraction

Programming Project 4: COOL Code Generation

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

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

Supplement for MIPS (Section 4.14 of the textbook)

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Lecture 5. Announcements: Today: Finish up functions in MIPS

Compiler Construction I

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Three-Address Code IR

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Optimization Prof. James L. Frankel Harvard University

CS61C : Machine Structures

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Thomas Polzer Institut für Technische Informatik

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

MIPS Functions and Instruction Formats

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Instruction Set Architectures (4)

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

Project 5: Extensions to the MiniJava Compiler

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Shift and Rotate Instructions

Retargeting GCC to Spim: A Recap

Computer Architecture

CS401 - Computer Architecture and Assembly Language Programming Glossary By

Instruction Set Architecture

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

Compiling Code, Procedures and Stacks

CS61C : Machine Structures

Subprograms, Subroutines, and Functions

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

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

Concepts Introduced in Chapter 7

Functions and Procedures

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

Today. Putting it all together

Instructions: Assembly Language

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

Programming Languages

Systems I. Machine-Level Programming V: Procedures

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Anne Bracy CS 3410 Computer Science Cornell University

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

CS 61c: Great Ideas in Computer Architecture

CSE Lecture In Class Example Handout

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

ECE232: Hardware Organization and Design

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

CSE 401/M501 Compilers

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Procedure Call and Return Procedure call

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

CS143 - Written Assignment 4 Reference Solutions

200 points total. Start early! Update March 27: Problem 2 updated, Problem 8 is now a study problem.

Practical Malware Analysis

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

55:132/22C:160, HPCA Spring 2011

Winter Compiler Construction T10 IR part 3 + Activation records. Today. LIR language

Lecture 5: Procedure Calls

Code Generation. Lecture 30

Procedure and Object- Oriented Abstraction

Transcription:

CS 2210 Programming Project (Part IV) April 25, 2018 Code Generation This project is intended to give you experience in writing a code generator as well as bring together the various issues of code generation discussed in the text and in class. Due date The assignment is due April 29th, 2018 11:59pm. This is the hard deadline and no extensions will be given for this project. Project Summary Your task is to write a code generator, the final phase of your compiler. It produces (target) assembly code for the MIPS R2000 architecture. It takes as input the augmented AST and symbol table produced by the previous phases of your compiler. The generated code will be executed using SPIM S20, a simulator for the MIPS R2000. Code generation will consist of assigning memory addresses for each variable used in the MINI- JAVA program and translating subtrees of the AST (intermediate language representation) into sequences of assembly instructions that perform the same task. Code Generation This is the only phase of your compiler which is machine dependent. Examples of assembly code programs will be provided on the class webpage. The important/interesting issues in generating code for MINI-JAVA are discussed in the following paragraphs. Please refer to chapter 7, 8 and class notes for further details on these issues, You can make the following assumptions to simplify the project. Code should be generated for AST nodes on a node-by-node basis without taking into account the context of the node (i.e. the code should work regardless of where that node occurs in the AST). Code should be generated for the AST nodes in the order that they occur within the AST when doing depth-first left-right traversal (i.e. program order). You may take advantage of any special instruction in the machine when choosing target instructions to implement a given node. You do not have to do any fancy register allocation or optimization on your target code. (i.e. your code can be inefficient as long as it is correct.) 1

Computing Memory Addresses Since address information has not been computed and entered into the symbol table by earlier phases, the first task of the code generator is to compute the offsets of each variable name (both global and local); that is, the address of each local data object and formal parameter within the activation record for the block in which they are declared. This can be done by initializing a variable offset at the start of each declaration section, and as each declaration is processed, the current value of offset is entered as an attribute of that symbol, and offset is then incremented by the total width of that data object (depending on its type). The program execution begins with a method called main() being called. All data for classes are instantiated when program execution begins. Thus, all storage for all classes is allocated statically in data sections inlined in code. The offsets of variables within classes should be computed and stored as an attribute of the variable name, typically relative to the start of the class. For simplicity, declarations within a method do not contain any objects whose types are classes. That is, local variables can only be of integer type or integer array type. Call-by-value parameters will have a width dependent on the type of the parameter (remember we are using only integer parameters), whereas call-by-reference parameters will have a width equal to ONE word to store an address. The machine architecture must be take into account when computing these widths, that is, an integer in the MIPS processor is 4 bytes. Offsets of locals can be implemented as a negative offset from the frame pointer while offsets of parameters can be positive from the frame pointer. Thus, the computation of offsets of arguments and local variables can be done independently of each other. These offsets will be used for generating code for every reference to variables, in addition to being used in the allocation of storage for activation records. Handling Structure Data Types Storage for an array is allocated as a consecutive block of memory. Access to individual elements of an array is handled by generating an address calculation using the base address of the array, the index of the desired element, and the size of the elements. You are free to choose the layout of elements of an array in your implementation (e.g., row major or column major order). Simple Control Flow Code for simple control statements, namely ifs and loops in MINI-JAVA, can be generated according to the semantics of conventional programming languages using the compare and branch instructions of the assembly language. Unique target labels will also have to be generated. You do not need to implement short circuiting when computing boolean expressions and just evaluate the entire expression. Method Invocation, Prologues, and Epilogues Recursion in MINI-JAVA prevents the use of a static storage allocation strategy. However, the language has no features that prevent the deallocation of activation records in a last-in-first-out manner. That is, activation records containing data local to an execution of a method, actual parameters, saved machine status, and other information needed to manage the activation of the method can be stored on a run-time stack. The MIPS assembly language provides the subroutine call mechanisms to manipulate the run-time user stack. 2

An activation record for a method is pushed onto the stack upon a call to that method, while the activation record for the method is popped from the stack when execution of the method is finished and control is to be returned to the caller. As MINI-JAVA does not allow dynamic classes and arrays, the sizes of all activation records are known at compile time. Method calls result in the generation of a calling sequence. Upon a call, an activation record for the callee must be set up and control must be transferred to the callee after saving the appropriate information in the activation record. For each method, the generated code sequence will consist of a prologue, the code for the statements of the method, and the epilogue. Typically, the prologue saves the registers upon a method call and allocates space on the stack for local variables, whereas the epilogue consists of restoring the saved machine status and returning control to the point in the caller immediately after the point of call. The SPIM manual on the course website explains the instructions used to implement these actions. Parameter passing In order to correctly handle the formal parameters within the body of the callee, the symbol table entry for each formal parameter must include an attribute that indicates the parameter passing mode, that is, by-value or by-reference. Remember that we do not pass arrays as parameters. On a method invocation, call-by-value parameters are handled by allocating the local store for the size of the object in the activation record of the callee and then evaluating the actual parameter and initializing the local store within the callee with the value of the actual parameter. All accesses to that formal parameter will change the value in the local space, with no effect on the caller. On a return, no values are copied back to the caller. Call-by-reference parameters are handled by allocating local space in the callee s activation record for the address of the actual parameter and then copying the address of the actual parameter into that local space. All accesses to that formal parameter during execution of the callee are indirect accesses through this address, having a direct effect on the caller. On return, no action is taken other than reclaiming the space. Note that MIPS has a convention that the first 4 arguments of a method call are passed in register $a0-$a3. You have the option to follow this convention. Register Usage In the MIPS processor, certain registers are typically reserved for special purposes. You should abide by these conventions. Possible Functions You may want to write the following functions to help in the code generation. 1. emit call(func name, num arg)/*emit a call instruction; func name: function id lexeme pointer; num arg: number of arguments */ 2. emit label(l num)/*emit a definition of a label; l num: label number; example: L=102, code generated = L 102 */ 3. emit goto(operator, l num) /*emit unconditional and conditional jump instructions; operator: an operator in the branch-jump group; l num: label number */ 3

4. emit data(name, type, size) /* emit one data line, which is used for STATIC allocation; name: data object id lexeme pointer; type: type width; size: number of elements of above type */ 5. emit str(name, str) /* emit a string constant definition line; name: pointer to the name lexeme; str: pointer to the str */ 6. emit most(operator, type, num op, op1, op2, op3) /* emit most of the instructions; operator: one of the instructions in the general group; type: data type; num op: number of operands 1, 2 and/or 3; op1...3: operands, op2 and op3 can be omitted depending on num op */ Run-time Error Detection You do not need to generate any run-time checks. Thus you can assume that array bounds are within range and scalars are within range. Testing your code The code generated is MIPS assembly code and should follow the descriptions specified in the handout SPIM S20. Samples of generated assembly code will be put on the class webpage. Please note: the example code is by no means the only way to generate code. This is only an example and you can come up with your own generation schemes. In fact, no two compilers generate the same assembly for a given source code. You can run the generated assembly code on the simulator and check the results. However, correct output does not guarantee that your code is completely correct. You should examine your generated code carefully. proj4> codegen < sample1.java proj4> spim.linux -asm -file code.s...... or proj4> spim.linux -asm (spim) load code.s (spim) step [0x00400000] 0x8fa40000 lw $4,0($29) (spim) run...... * For the above to work, the file trap.handler must be in your working directory. * Read the SPIM debugger manual on course website to learn more about debugger commands Assignment submission Please call your code gen.mk or makefile, and put your external documentation in a file called readme.txt. The submission should be a compressed file that contains your project source code and report (no executable please). On Linux, this can be done with the command tar zcvf USER- NAME proj4.tar.gz *. Copy your archive to the directory: wahn/submit/2210/. Please write all your implementation for this project in two files: proj4.h and proj4.c, for easy grading. Also, the TA will be using Moss to detect plagiarism so please keep away from others codes! 4

Reference implementation For those of you who were not able to submit project 3 properly, I can provide a reference implementation of project 3 such that you can at least complete project 4. Grading Policy The grading policy is as follows. (1) empty declarations, empty main 40% (2) sequential code generation. ---> several ALU operations 10% (3) flow of control code ---> if-statement 5 % ---> while loop 5 % (4) input/output ---> input 5 % ---> output 5 % ---> correct string print 5 % (5) parameter passing ---> procedural call (no parameter) 5 % ---> value argument 5 % ---> argument 5 % (6) memory allocation ---> array allocation 5 % ---> object declaration of a class inside another class (similar to src9) 5 % 5