Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

Similar documents
Homework #3: CMPT-379

1 Lexical Considerations

Lexical Considerations

Lexical Considerations

Compilers. Compiler Construction Tutorial The Front-end

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

The Decaf Language. 1 Lexical considerations

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

MIPS Assembly Language Programming

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

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

MIPS Assembly Language Programming

Course Administration

Decaf Language Reference Manual

Assignment 1: Pipelining Implementation at SPIM Simulator

SPIM & MIPS Programming

Course Administration

Compiling Techniques

CMPT 379 Compilers. Anoop Sarkar.

Decaf Language Reference

The PCAT Programming Language Reference Manual

MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

The SPL Programming Language Reference Manual

The Decaf language 1

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

CA4003 Compiler Construction Assignment Language Definition

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

CENG3420 Lab 1-1: MIPS assembly language programing

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

ECE 30 Introduction to Computer Engineering

Compiler construction in4020 lecture 5

CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Project 2 Interpreter for Snail. 2 The Snail Programming Language

G Programming Languages - Fall 2012

Writing Evaluators MIF08. Laure Gonnord

MIPS function continued

We will study the MIPS assembly language as an exemplar of the concept.

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

EC 413 Computer Organization

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

SPIM & MIPS. Department of Information and Management. Computer Organization and Structure 年 9 月 29 日星期 一

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

Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field

Programming Languages

Lexical and Syntax Analysis

Operational Semantics of Cool

COMP2421 COMPUTER ORGANZATION. Lab 3

Project Compiler. CS031 TA Help Session November 28, 2011

Intermediate Code Generation

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

CS153: Compilers Lecture 8: Compiling Calls

Compiler construction 2002 week 5

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

ECE 250 / CS 250 Computer Architecture. Procedures

Intermediate Code Generation

ECS 142 Project: Code generation hints

KU Compilerbau - Programming Assignment

CS2104 Prog. Lang. Concepts

Compiling Code, Procedures and Stacks

Type Checking Binary Operators

CSE-304 Compiler Design

Context-free grammars (CFG s)

Semantic Analysis and Type Checking

MIPS Assembly (Functions)

Compiler construction 2005 lecture 5

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

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

YOLOP Language Reference Manual

Functions. Arash Rafiey. September 26, 2017

CMPT 379 Compilers. Parse trees

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Module 27 Switch-case statements and Run-time storage management

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

Mid-Term 2 Grades

Compiler II: Code Generation Human Thought

PP6: Register Allocation - Optimization

Lexical and Syntax Analysis. Abstract Syntax

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

Three-Address Code IR

CSE P 501 Exam 8/5/04

PART ONE Fundamentals of Compilation

CS 2210 Programming Project (Part IV)

Typescript on LLVM Language Reference Manual

ECE260: Fundamentals of Computer Engineering

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

MIPS Assembly (FuncDons)

MIPS Assembly Programming

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Lecture 5: Procedure Calls

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Context-sensitive Analysis

CSCI Compiler Design

Transcription:

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar anoop@cs.sfu.ca Only submit answers for questions marked with. Provide a makefile such that make compiles all your programs, and make test runs each program, and supplies the necessary input files. (1) Introduction to MIPS assembly: The target of the code generation step for the compiler will be MIPS R2000 assembly language. MIPS is a reduced instruction set (RISC) machine. We will treat MIPS assembly code as a virtual machine and use a simulator for MIPS assembly called spim that takes MIPS assembly and simulates (runs) it on x86 Linux. spim is available in the location mentioned on the course web page. Your task for this homework is to convert the following Decaf program called catalan.decaf by hand into MIPS assembly. class Catalan { void main() { int i, j; i = callout("read_int"); j = cat(i); callout("print_int", j); callout("print_str", "\n"); // catalan number of n int cat(int n) { int t; t = fact(n); return( fact(2*n) / (t * t * (n+1)) ); // factorial of n int fact(int n) { if (n == 1) { return(1); else { return(n*fact(n-1)); Provide the MIPS assembly program in a file called catalan.mips which should run on the simulator spim as follows: spim -file catalan.mips When the MIPS program is run on the spim simulator, it should wait for an integer input n from the user, and then print out the result of the catalan function for n followed by a newline character. The MIPS program must be a direct translation of the Decaf program. Comment your MIPS code heavily. Compare your generated code to the parse tree and reflect on automating code generation (topic of a future homework). This exercise will familiarize you with MIPS assembly. Read the documentation provided on the course web page that introduces you to MIPS assembly, including a detailed tutorial on passing parameters on the stack frame for procedure calls in MIPS, and a tutorial on how to use spim. It assumes some familiarity with assembly language. Ask for background reading if you are not familiar with any of the terms used in the MIPS documentation. You have to manage the register names used in the output assembly code. For this homework, you can ignore some of the complexities of code generation by assuming that we have a sufficient number of temporary registers at hand. A few facts that might be useful: in MIPS assembly, upto four arguments can be passed directly to a subroutine in the registers $a0-$a3, and $s0-$s7 are temporary registers that 1

retain values during a function call, while temporary registers $t0-$t7 do not retain their values. The standard input-output library is provided through the interface (compiled into spim). I/O library service code Arguments Result print int 1 $a0 = integer print string 4 $a0 = string read int 5 integer in $v0 read string 8 $a0 = buffer, $a1 = length exit 10 Below is an example in MIPS that uses the interface above to read an integer from standard input using read int, and then prints it out to standard output using print int, and then prints out a newline using print string: nl:.data.asciiz "\n".text li $v0, 5 move $a0, $v0 li $v0, 4 la $a0, nl I/O should be done only using the service. Do not use the jal printf idiom used in some examples in the MIPS/spim documentation. Below is a simple Decaf program that computes a simple expression and the corresponding MIPS translation (it shows how to use temporary registers and how to store and use a global string constant). class Expr { void main() { int x; x = 2*3+5; callout("print_int", x); callout("print_string", "\n");.data str0:.asciiz "\n" #-------------------------.text.globl main li $t0, 2 li $t1, 3 mul $t2, $t0, $t1 li $t0, 5 addu $t1, $t0, $t2 move $a0, $t1 li $v0, 4 la $a0, str0 2

(2) Implement a symbol table. A symbol table is a mapping from identifiers to any information that the compiler needs for code generation. A symbol table is easily implemented using hash tables or maps, e.g. here is a declaration of a symbol table using STL in C++: typedef map<string, descriptor* > symbol_table; where a descriptor is a structure or class which contains a type, a register destination, a memory address location, and a variable spilled indicating if the value is to be found in a register or in memory (note that we will not use memory locations for variables until later). In Decaf we are allowed to shadow a variable declaration (see Q. (4) for an example). This means that a new definition for an identifier in a block will cause a new descriptor to be associated with the identifier, but once the block terminates the previous descriptor for the identifier has to be restored. A simple way to implement this notion of local scoping is to specify that each block can create a new symbol table in a list: typedef list<symbol_table > symbol_table_list; symbol_table_list symtbl; If a variable has a local definition that shadows another definition of the same variable name, we pick up the most recently defined descriptor for that variable by simply scanning the list of symbol tables starting from the most recent one: descriptor* access_symtbl(string ident) { for (symbol_table_list::iterator i = symtbl.begin(); i!= symtbl.end(); ++i) { symbol_table::iterator find_ident; if ((find_ident = i->find(ident))!= i->end()) return find_ident->second; return NULL; This is just one way to implement a symbol table. You can implement it any way you like, as long as it can handle shadowing of variables. For this question, you can assume that the identifiers are variables, but in later homeworks, the symbol table will also store information about function names, global variables, etc., and additional information will have to be added to the descriptor. (3) For the following context-free grammar: block { var-decl-list var-decl-list var-decl var-decl-list ɛ var-decl type id-comma-list ; id-comma-list id, id-comma-list id type int bool Provide a yacc program that passes the type information for each variable by using inherited attributes. The program should enter each variable name into a symbol table along with its type information. (4) Implement the following modified fragment of Decaf syntax. The syntax has been changed a little to allow statements like x; which are not allowed in Decaf. block { var-decl statement var-decl type { id +, ; type int bool statement id ; block 3

The implementation should enter information about each variable definition into a symbol table. For each instance when an identifier is used in a statement, your yacc program should introduce a comment line into the Decaf program that specifies the line number of the variable definition for that identifier. For example, for the input on the left column, your program should produce the output in the right column. The line numbers refer to lines in the original source code. { int x, y; { int p, q; { int y; x; y; { int x; p; x; y; { int x, y; { int p, q; { int y; x; // using decl on line: 1 y; // using decl on line: 3 { int x; p; // using decl on line: 2 x; // using decl on line: 5 y; // using decl on line: 3 (5) Code Generation for Expressions In this homework, you make the first step towards full code generation in Decaf. In the first stage, implement the steps listed below for the following fragment of Decaf syntax: block { var-decl statement var-decl type { id +, ; type int bool statement assign ; method-call ; block assign lvalue = expr method-call callout ( stringconstant [{, { callout-arg +, callout-arg expr stringconstant lvalue id expr lvalue method-call constant expr bin-op expr - expr! expr ( expr ) bin-op arith-op rel-op eq-op cond-op arith-op + - * / << >> % rot rel-op < > <= >= eq-op ==!= cond-op && constant intconstant charconstant bool-constant bool-constant true false ] ) This fragment of Decaf represents the expression-level sub-grammar. Note that you should augment your yacc implementation of this sub-grammar from your previous homework and extend it to handle code generation. 4

For example, the input program (from the sub-grammar) in the left column should produce the MIPS code in the right column. As you can see, each component of the expression parse tree produces a value that is stored in a MIPS register, making sure that you use as many registers as necessary. Other examples of input and output are provided in the homework directory. { callout("print_int", 2+2*3+5);.text.globl main li $t0, 2 li $t1, 2 li $t2, 3 mul $t1, $t1, $t2 addu $t0, $t0, $t1 li $t1, 5 addu $t0, $t0, $t1 move $a0, $t0 The target of the code generation step will be MIPS R2000 assembly language. We will treat MIPS assembly code as a virtual machine and use an simulator for MIPS assembly called spim that takes MIPS assembly and simulates (runs) it on x86 Linux. spim is available for your use from the location mentioned on the course web page. Read Section 8.6 from the Dragon book for an algorithm that specifies how to allocate register names. You must reuse registers as far as possible. By exploiting the parse tree, only a few registers are typically used even for very complex expressions. For this question, once your program is reusing registers as much as possible, you can ignore some of the complexities of code generation by assuming that you have a sufficient number of temporary registers at hand. The MIPS target machine allows the use of the following registers: $a0-$a3, $t0-$t9, $s0-$s7. If your program, while using this simple code generation algorithm, runs out of registers to use, your program can exit with an error message. You should print a boolean as an integer: 0=false and 1=true. You will have to implement a symbol table which will store information about the variables in the Decaf program. Submit a program which accepts Decaf code and produces MIPS assembly. Save the MIPS assembly program to file filename.mips and run the simulator spim as follows: spim -file <filename.mips> (6) After finishing Q. (5), change your program so that it can deal with the actual set of registers available in MIPS assembly. Use the heap to store values associated with identifiers. Store the value of an identifier to a memory location on the heap. When the value is needed it should be loaded into a register. The symbol table should store the memory location of the identifier. This will allow the re-use of registers (using register spilling to the heap) when the registers run out during code generation. See the MIPS file using-heap.mips in the homework directory for an example of how to spill a register to the heap and then load the value back into a register. 5