IR Code Generations for Mini Compiler - Project | CS 322, Study Guides, Projects, Research of Computer Science

Material Type: Project; Professor: Li; Class: LANG COMPILER DESIGN; Subject: Computer Science; University: Portland State University; Term: Summer 2001;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/19/2009

koofers-user-28i
koofers-user-28i 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS322 Winter’09: Languages and Compiler Design I I 01/08/09
Prof. Jingke Li (FAB 120-06, [email protected]), Classes: TTh 2-3:50pm, URBAN 304, Office Hours: TTh 10-11am.
Project #1: IR Code Generation (I)
(Due Thursday 01/22/09 @ 5pm)
This project is to implement a first version of an IR code generator for the MINI compiler. The target
IR is the IR tree language discussed in class. The generator is to be implemented as a visitor to the MINI
AST nodes. The visitor interface is defined in TransVI.java in the ast subdirectory.
When the visitor IrgenVisitor1 is invoked on a MINI AST Program node, an IR tree rooted at a PROG
node should be generated. The following is a guide on how to translate specific AST nodes. Since this is a
first version of the IR code generator, some nodes’ translations are simplified.
Declarations
VarDecls If a VarDecl has no initialization expression, no action is needed; otherwise, it is treated
as an assignment statement, and is translated into a MOVE node. All variables retain their names in
this project.
MethodDecls Each MethodDecl node is translated into a FUNC node. To do this,
translate the method’s VarDeclList into a (possibly empty) STMTlist, and
translate the method’s StmtList into a STMTlist.
Merge the two STMTlists and create a FUNC node. (The list classes in our IR tree are defined as
extensions of Java’s Vector class, hence the method addAll could be used to merge two lists.)
The FUNC’s label field gets the Method’s name. Its two other fields, varCnt and argCnt, are not used
in this project, and hence should be set to 0s.
ClassDecls AClassDecl node consists of a list of VarDecls and a list of MethodDecls. VarDecls
are to be ignored here, since they will be handled in NewObject node. Each MethodDecl is translated
into a FUNC node, so a ClassDecl is translated into a FUNClist. The order of elements in a FUNClist
does not affect the program’s correctness.
The class id and its parent pointer are not used in this project.
Program The FUNClists from multiple ClassDecls are merged into a single FUNClist, and is used
to create a PROG node.
Statements
Assign Translate it into a MOVE node.
CallStmt Translate it straightforwardly into a CALLST node. (A Call node similarly translates into
aCALL node.)
If and While Translate each into a CJUMP node, together with other statements, as discussed in
class.
Return Translate it straightforwardly into a RETURN node.
Print Translate it into a CALLST node; the target of call is either prInt or prString.
pf2

Partial preview of the text

Download IR Code Generations for Mini Compiler - Project | CS 322 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS322 Winter’09: Languages and Compiler Design II 01/08/ Prof. Jingke Li (FAB 120-06, [email protected]), Classes: TTh 2-3:50pm, URBAN 304, Office Hours: TTh 10-11am.

Project #1: IR Code Generation (I)

(Due Thursday 01/22/09 @ 5pm)

This project is to implement a first version of an IR code generator for the MINI compiler. The target IR is the IR tree language discussed in class. The generator is to be implemented as a visitor to the MINI AST nodes. The visitor interface is defined in TransVI.java in the ast subdirectory. When the visitor IrgenVisitor1 is invoked on a MINI AST Program node, an IR tree rooted at a PROG node should be generated. The following is a guide on how to translate specific AST nodes. Since this is a first version of the IR code generator, some nodes’ translations are simplified.

Declarations

  • VarDecls — If a VarDecl has no initialization expression, no action is needed; otherwise, it is treated as an assignment statement, and is translated into a MOVE node. All variables retain their names in this project.
  • MethodDecls — Each MethodDecl node is translated into a FUNC node. To do this,
    • translate the method’s VarDeclList into a (possibly empty) STMTlist, and
    • translate the method’s StmtList into a STMTlist.

Merge the two STMTlists and create a FUNC node. (The list classes in our IR tree are defined as extensions of Java’s Vector class, hence the method addAll could be used to merge two lists.) The FUNC’s label field gets the Method’s name. Its two other fields, varCnt and argCnt, are not used in this project, and hence should be set to 0s.

  • ClassDecls — A ClassDecl node consists of a list of VarDecls and a list of MethodDecls. VarDecls are to be ignored here, since they will be handled in NewObject node. Each MethodDecl is translated into a FUNC node, so a ClassDecl is translated into a FUNClist. The order of elements in a FUNClist does not affect the program’s correctness. The class id and its parent pointer are not used in this project.
  • Program — The FUNClists from multiple ClassDecls are merged into a single FUNClist, and is used to create a PROG node.

Statements

  • Assign — Translate it into a MOVE node.
  • CallStmt — Translate it straightforwardly into a CALLST node. (A Call node similarly translates into a CALL node.)
  • If and While — Translate each into a CJUMP node, together with other statements, as discussed in class.
  • Return — Translate it straightforwardly into a RETURN node.
  • Print — Translate it into a CALLST node; the target of call is either prInt or prString.

Expressions

  • Arithmetic operations — Translate them into corresponding BINOP nodes.
  • Boolean expressions — Translate them using the value-representation approach, as studied in class.
  • Identifiers — Simply translate them into NAME nodes (for now).
  • NewArray — Needs to allocate space and initialize elements:
    • create a CALL node to “malloc” space (with one extra cell for holding array length), and a MOVE node to save the return value in a TEMP,
    • create a statement to save the array element count into the first cell,
    • create statements to initialize array elements to 0, and
    • create an ESEQ node to glue the statements together and return the temp.
  • Array Elements — Translate them into MEM nodes with array pointer and an offsets for fetching the stored elements. I.e. a[0] gets translated into (MEM (BINOP + <a’s addr> (BINOP * (CONST 1) (NAME wSZ)). Here wSZ is a predefined constant name, representing the word-size.
  • ArrayLength — Translate it into a MEM node for fetching the stored array length value (always as the first cell of the allocated array object).
  • NewObject — Translate it into a call to "malloc" with a synthetic argument NAME("<class-name> obj size"), where <class-name> is to be substituted with the actual class name appeared in the NewObject node.
  • Member — Translate it into NAME("<class-name> <member-name>").
  • This — Simply translate it into NAME("this") in this project.
  • Text — Translate it into a STRING node.
  • Int and Bool — Translate both into CONST nodes.

Code Organization

A tar file, proj1-code.tar, can be downloaded from the class webpage, which contains

  • ast — the AST node definitions. They are the same as last term except that a new TransVI interface is added to each class.
  • astpsr — the ast parser program.
  • ir — the IR tree node definitions.
  • irgen — this is where your program should be placed; a driver program is already there.
  • tst — some test programs.
  • Makefile — To make your program, say make irgen1.
  • runi1 — a script for running your program.

Create a proj1 directory, and untar the tar file there. You are to write a program, IrgenVisitor1.java, and place it in the irgen subdirectory.

What and How to Turn in Your Program

Submit your program IrgenVisitor1.java to [email protected]. Please keep a copy of your submitted file aside, in case there is a need for re-submission.