Download presentation
Presentation is loading. Please wait.
1
PSUCS322 HM 1 Languages and Compiler Design II Project 2 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010
2
PSUCS322 HM 2 Agenda Basic Information Method Declarations Method Invocations Variable References New Objects “This” Node
3
PSUCS322 HM 3 Basic Information Infrastructure: same as Project 1, but 2 packages added; you now have: ast --- the source-language’s AST nodes astpsr --- the AST parser program symbol --- the symbol table package checker --- the typechecking package ir --- the IR tree nodes irgen --- the irgen code tst --- a set of test programs New Issues: method declarations method invocations variable references new objects “this” node
4
PSUCS322 HM 4 Method Declarations Creating an unique label for the method by concatenating class name and method name together: –Look up the corresponding MethodRec entry in the symbol table –Call the toString() method –You need track current class scopes. --Recall the use of currClass and currMethod in the typechecking project Computing the value of varCnt –which is the count of local variables –Needed for creating a FUNC node –It’s value can be obtained by calling the localCnt() method of the corresponding MethodRec entry
5
PSUCS322 HM 5 Method Declarations, Cont’d Computing the value of argCnt: which is the maximum number of arguments of all Call/CallStmt nodes (excluding calls to system routines) in the body of the current method Value has to come from the recursive visit to the method’s body. You need to declare a class variable, say maxArgCnt, at the top level, and update the value if necessary every time a Call or CallStmt node is encountered: if( maxArgCnt <= argCnt ) maxArgCnt = argCnt + 1; In the above code, argCnt is the Call node’s arg count, and the extra 1 is for passing the ’access link’
6
PSUCS322 HM 6 Method Invocations Creating an unique label for the method: Again, look up the symbol table for the MethodRec and use the “ toString()” method You cannot do a direct lookup in the current class environment, since the method may not be defined here. The proper steps are: 1. Use setClass() and setMethod(” methods (both are defined in the CheckerVisitor class) to condition the typechecker to the current environment 2. Perform typechecking on the obj component of the Call/CallStmt node, which should return an ObjType representing the class of the object 3. Look up ClassRec using the class id in the ObjType 4. Finally, look up the MethodRec within the ClassRec
7
PSUCS322 HM 7 Method Invocations, Cont’d Setting up an access link by using (PARAM 0): public EXP visit( Call n ) throws Exception { String label =...form the new name... int argCnt =...figure out the number of args... EXPlist el = new EXPlist();...tranlate arguments, and add to el......don’t forget to include an access link......HINT: n.obj is a point to the class object... return new CALL( new NAME( label ), el ); } //end EXP CallStmt is translated similarly, except that it returns a CALLST node
8
PSUCS322 HM 8 Variable Reference Depending on type, variable Id nodes are translated into MEMBER, PARAM, or VAR nodes; all with an index offset from a reference point: A class variable Id is translated into a MEMBER node. The reference point is a pointer to a class object (i.e. the access link) A param Id is translated into a PARAM node, with implicit reference point A local variable Id is translated into a VAR node, with implicit reference point A variable’s index value can be looked in the symbol table by “v.idx()” method: public EXP visit( Id n ) throws Exception { Var v =... get variable’s symbol table entry... switch ( v.kind() ) { case Var.CLASS: return new MEMBER(...); case Var.PARAM: return new PARAM(...); case Var.LOCAL: return new VAR(...);... } //end EXP
9
PSUCS322 HM 9 New Objects 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 its class variables: public EXP visit( NewObject n ) throws Exception { symbol.Class save_class = currClass; currClass =...get the NewObject’s class... int vcnt = currClass.vcnt(); ExpList inits = currClass.initExps();...a call to malloc... for( int i = 0; i < vcnt; i++ ) {...process an init expr, and construct a MOVE node to......copy it to the corresponding class variable’s slot... } //end for currClass = save_class; return new ESEQ( stmts, obj ); } //end EXP
10
PSUCS322 HM 10 “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))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.