Docsity

CS322 Winter'09: IR Code Generation (II) Project Instructions - Prof. Jingke Li, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CS322 Winter’09: Languages and Compiler Design I I 01/22/09
Prof. Jingke Li (FAB 120-06, [email protected]), Classes: TTh 2-3:50pm, URBAN 304, Office Hours: TTh 10-11am.
Project #2: IR Code Generation (II)
(Due Thursday 02/05/09 @ 5pm)
This project is to complete the IR code generator for the MINI compiler. The new areas are method
declaration and invocation,class member,variable reference,new object, and “this” node.
Symbol Table and Typechecker
The symbol table and typechecker are both needed in this version of IR code generator. They are passed in
as parameters to the constructor:
public IrgenVisitor(Table tab, CheckerVisitor tv) {
...
}
Method Declaration
There are two issues to handle for a MethodDecl node. The first is that you need to construct a unique
label for the method by concatenate class name and method name together. The second is to figure out the
values of varCnt and argCnt, which are needed to construct a correct FUNC node.
Luckily, the MethodRec entry in the symbol table already includes methods for computing the unique
label (’toString()’) and varCnt (’localCnt()’). All you need to do is to keep track of class and method
scopes (recall the use of currClass and currMethod in the typechecking project).
For argCnt, the value has to come from the recursive visiting to the method’s body, since it represents
the maximum number of arguments of all calls that appear inside the method body. You need to declare
a class variable at the top level to keep track of this value, and update the value if necessary every time a
Call or CallStmt node is encountered.
Method Invocation
The handling of method invocation (i.e. Call and CallStmt node) also involves two issues:
•Constructing an unique label — Again, you should look up the symbol table for the MethodRec and use
the toString() method. However, you can not do a direct lookup in the current class environment,
since the method may not be defined here. The proper steps are:
–Use ’setClass()’ and ’setMethod()’ methods to condition the typechecker to the current
environment;
–Perform typechecking on the obj component of the Call/CallStmt node, which should return an
ObjType representing the class of the object;
–Look up ClassRec using the class id in the ObjType;
–Finally, look up the MethodRec within the ClassRec.
•Setting up an access link — To provide a link for accessing class variables from within a method’s body,
the pointer to the class object record needs to be passed into the method’s invocation. This pointer
can be obtained by processing the obj component of Call/CallStmt nodes. It is then translated into a
special zeroth parameter (corresponding to (PARAM 0) when accessed), to go along with other regular
parameters.
pf2

Partial preview of the text

Download CS322 Winter'09: IR Code Generation (II) Project Instructions - Prof. Jingke Li and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

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

Project #2: IR Code Generation (II)

(Due Thursday 02/05/09 @ 5pm)

This project is to complete the IR code generator for the MINI compiler. The new areas are method declaration and invocation, class member, variable reference, new object, and “this” node.

Symbol Table and Typechecker

The symbol table and typechecker are both needed in this version of IR code generator. They are passed in as parameters to the constructor:

public IrgenVisitor(Table tab, CheckerVisitor tv) { ... }

Method Declaration

There are two issues to handle for a MethodDecl node. The first is that you need to construct a unique label for the method by concatenate class name and method name together. The second is to figure out the values of varCnt and argCnt, which are needed to construct a correct FUNC node. Luckily, the MethodRec entry in the symbol table already includes methods for computing the unique label (’toString()’) and varCnt (’localCnt()’). All you need to do is to keep track of class and method scopes (recall the use of currClass and currMethod in the typechecking project). For argCnt, the value has to come from the recursive visiting to the method’s body, since it represents the maximum number of arguments of all calls that appear inside the method body. You need to declare a class variable at the top level to keep track of this value, and update the value if necessary every time a Call or CallStmt node is encountered.

Method Invocation

The handling of method invocation (i.e. Call and CallStmt node) also involves two issues:

  • Constructing an unique label — Again, you should look up the symbol table for the MethodRec and use the toString() method. However, you can not do a direct lookup in the current class environment, since the method may not be defined here. The proper steps are: - Use ’setClass()’ and ’setMethod()’ methods to condition the typechecker to the current environment; - Perform typechecking on the obj component of the Call/CallStmt node, which should return an ObjType representing the class of the object; - Look up ClassRec using the class id in the ObjType; - Finally, look up the MethodRec within the ClassRec.
  • Setting up an access link — To provide a link for accessing class variables from within a method’s body, the pointer to the class object record needs to be passed into the method’s invocation. This pointer can be obtained by processing the obj component of Call/CallStmt nodes. It is then translated into a special zeroth parameter (corresponding to (PARAM 0) when accessed), to go along with other regular parameters.

Class Member

Class member in the form of Member node is translated into a MEMBER node

(MEMBER <var’s idx>)

where is a pointer to the class object record, which can be obtained by translating the obj component of the Member node.

Variable Reference

In the IR tree representation, variables (i.e. Id nodes) are no longer referenced through their names. The correct forms for them dependent on their types:

  • Class variables — They are stored in an object record (which is allocated when a new object is created), and are accessed through offsets from the record’s main pointer (i.e. starting address). A class variable’s offset can be computed from its index (available in the variable’s symbol table entry). When an Id is determined to be a class variable, it should be translated into the following form:

(MEMBER (MEM PARAM(0)) <var’s idx>)

  • Local variables — A method’s local variables are stored in an abstract array VAR. Their indices corre- spond to their positions in the method declaration: the first variable is (VAR 1), the second is (VAR 2), and so on.
  • Method’s parameters — They are stored in an abstract array PARAM, and indexed by their positions in the method declaration as well: (PARAM 1), (PARAM 2), and so on.

New Object

A NewObject node should be translated into a CALL to "malloc" with a single argument representing the size of the object, followed by a sequence of statements for initializing the class variables. A special note: the initial expressions saved in the the symbol table entry for this purpose are AST expressions, and they need to be translated into IR expressions. Furthermore, this translation should be conducted in the proper scope environment — the IrgenVisitor variable currClass needs to point to the object’s class.

“This” Node

“This” node should point to the current object. We assume it is only used within a method’s body. Hence it should be translated into:

(MEM PARAM(0))

Code Organization

Code organization is the same as last project, except that two additional modules, symbol and checker are included. Your program should be called IrgenVisitor.java. The run script is called runi.

What and How to Turn in Your Program

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