CS322 Languages and Compiler Design II Assignment 2: IR Code Generation (II)

$30.00

Category: Tags: , , , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (3 votes)

In this assignment, you are going to implement a second IR code generator. This time, the focuses are on (1) classes and objects, and (2) methods and instance variables. The assignment carries a total of 10 points. Download and unzip the file hw2.zip. You’ll see a hw2 directory with the following items: hw2.pdf — this document ast/Ast.java, ast/.java — AST definition and its parser code ir/IR1.java — IR definition IRGen0.java — a starter version of the code-gen program IRInterp.jar — an interpreter for the IR language tst/ — contains a set of tests Makefile — for compiling your program gen, run — scripts for generating and running IR programs Check to make sure that you have Java 1.8 in your environment (use “java -version”). If not, you should add it in by running addpkg (and select java8). The Input Language The input language to the code generator is the full version of the miniJava AST language. However, we are skipping arrays and all operation forms of expressions. The following is the portion of the AST language that is relevant to this assignment: Program -> {ClassDecl} ClassDecl -> “ClassDecl” [] {VarDecl} {MethodDecl} VarDecl -> “VarDecl” Type Exp MethodDecl -> “MethodDecl” Type “(” {Param} “)” {VarDecl} {Stmt} Param -> “(” Type “)” Type -> “void” | “IntType” | “BoolType” | “(” “ObjType” “)” Stmt -> “{” {Stmt} “}” | “Assign” Exp Exp | “CallStmt” Exp “(” {Exp} “)” | “If” Exp Stmt [ “Else” Stmt ] | “While” Exp Stmt | “Print” Exp | “Return” [Exp] Exp -> “(” “Call” Exp “(” {Exp} “)” “)” | “(” “NewObj” “)” | “(” “Field” Exp “)” | “This” | | | | Furthermore, we make the following simplification assumptions: 1 • No static data or methods other than the main method. • Methods are implemented with static binding. (Hence there is no need to create class descriptors in IR code.) • No init routines for new objects. (Hence class fields’ init values in source program are ignored.) • In source program, base classes are defined before their subclasses. (Hence a simple sequential processing of class decls is sufficient.) As in Assignment 1, the IR code-gen program will work on the AST language’s internal representation, where every AST node is represented by a Java class. These class definitions are in the file ast/Ast.java. The Target Language The target language is simply called IR. It is an extension to the IR1 language used in Assignment 1. IR has the capacity to handle all the features of our source language, miniJava. Here are its highlights. (The features that are not needed for this assignment are noted.) • A program consists of a set of data sections followed by a set of function definitions: Program -> {Data} {Func} The data sections are for holding static class information, such as the vtables. (Note: They are not needed for this assignment.) • Function definitions and variable declarations are exactly the same as in IR1: Func -> VarList [VarList] “{” {Inst} “}” VarList -> “(” [ {“,” }] “)” = _[A-Za-z][A-Za-z0-9]* • IR has the usual set of instructions: Inst -> Dest “=” Src BOP Src // Binop | Dest “=” UOP Src // Unop | Dest “=” Src // Move | Dest “=” Addr Type // Load | Addr Type “=” Src // Store | [Dest “=”] “call” [“*”] CallTgt ArgList // Call | “return” [Src] // Return [val] | “if” Src ROP Src “goto”