

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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.
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.
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) { ... }
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.
The handling of method invocation (i.e. Call and CallStmt node) also involves two issues:
Class member in the form of Member node is translated into a MEMBER node
(MEMBER
where
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:
(MEMBER (MEM PARAM(0)) <var’s idx>)
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 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 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.
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.