8 public BuildIR(State state) {
11 public void buildtree() {
12 for(Iterator it=state.parsetrees.iterator();it.hasNext();) {
13 ParseNode pn=(ParseNode)it.next();
18 /** Parse the classes in this file */
19 public void parseFile(ParseNode pn) {
20 ParseNode tpn=pn.getChild("type_declaration_list");
22 ParseNodeVector pnv=tpn.getChildren();
23 for(int i=0;i<pnv.size();i++) {
24 ParseNode type_pn=pnv.elementAt(i);
25 if (isEmpty(type_pn)) /* Skip the semicolon */
27 ClassDescriptor cn=parseTypeDecl(type_pn);
33 public ClassDescriptor parseTypeDecl(ParseNode pn) {
34 if (isNode(pn, "class_declaration")) {
35 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
36 if (!isEmpty(pn.getChild("super").getTerminal())) {
37 /* parse superclass name */
38 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
39 NameDescriptor nd=parseName(snn);
40 cn.setSuper(nd.toString());
42 if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
43 cn.setSuper(TypeUtil.ObjectClass);
45 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
46 parseClassBody(cn, pn.getChild("classbody"));
48 } else throw new Error();
51 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
52 ParseNode decls=pn.getChild("class_body_declaration_list");
54 ParseNodeVector pnv=decls.getChildren();
55 for(int i=0;i<pnv.size();i++) {
56 ParseNode decl=pnv.elementAt(i);
57 if (isNode(decl,"member")) {
58 parseClassMember(cn,decl);
59 } else if (isNode(decl,"constructor")) {
60 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
61 } else if (isNode(decl,"block")) {
62 } else throw new Error();
67 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
68 ParseNode fieldnode=pn.getChild("field");
70 if (fieldnode!=null) {
71 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
74 ParseNode methodnode=pn.getChild("method");
75 if (methodnode!=null) {
76 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
82 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
83 ParseNode tn=pn.getChild("type");
84 String type_st=tn.getTerminal();
85 if(type_st.equals("byte")) {
86 return state.getTypeDescriptor(TypeDescriptor.BYTE);
87 } else if(type_st.equals("short")) {
88 return state.getTypeDescriptor(TypeDescriptor.SHORT);
89 } else if(type_st.equals("boolean")) {
90 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
91 } else if(type_st.equals("int")) {
92 return state.getTypeDescriptor(TypeDescriptor.INT);
93 } else if(type_st.equals("long")) {
94 return state.getTypeDescriptor(TypeDescriptor.LONG);
95 } else if(type_st.equals("char")) {
96 return state.getTypeDescriptor(TypeDescriptor.CHAR);
97 } else if(type_st.equals("float")) {
98 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
99 } else if(type_st.equals("double")) {
100 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
101 } else if(type_st.equals("class")) {
102 ParseNode nn=tn.getChild("class");
103 return state.getTypeDescriptor(parseName(nn.getChild("name")));
104 } else if(type_st.equals("array")) {
105 ParseNode nn=tn.getChild("array");
106 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
107 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
108 for(int i=0;i<numdims.intValue();i++)
109 td=td.makeArray(state);
116 private NameDescriptor parseName(ParseNode nn) {
117 ParseNode base=nn.getChild("base");
118 ParseNode id=nn.getChild("identifier");
120 return new NameDescriptor(id.getTerminal());
121 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
125 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
126 ParseNode mn=pn.getChild("modifier");
127 Modifiers m=parseModifiersList(mn);
129 ParseNode tn=pn.getChild("type");
130 TypeDescriptor t=parseTypeDescriptor(tn);
131 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
132 ParseNodeVector pnv=vn.getChildren();
133 for(int i=0;i<pnv.size();i++) {
134 ParseNode vardecl=pnv.elementAt(i);
137 ParseNode tmp=vardecl;
138 TypeDescriptor arrayt=t;
139 while (tmp.getChild("single")==null) {
140 arrayt=arrayt.makeArray(state);
141 tmp=tmp.getChild("array");
143 String identifier=tmp.getChild("single").getTerminal();
145 ParseNode epn=vardecl.getChild("initializer");
147 ExpressionNode en=null;
149 en=parseExpression(epn.getFirstChild());
151 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
156 private ExpressionNode parseExpression(ParseNode pn) {
157 if (isNode(pn,"assignment"))
158 return parseAssignmentExpression(pn);
159 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
160 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
161 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
162 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
163 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
164 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
165 isNode(pn,"rightshift")||isNode(pn,"sub")||
166 isNode(pn,"add")||isNode(pn,"mult")||
167 isNode(pn,"div")||isNode(pn,"mod")) {
168 ParseNodeVector pnv=pn.getChildren();
169 ParseNode left=pnv.elementAt(0);
170 ParseNode right=pnv.elementAt(1);
171 Operation op=new Operation(pn.getLabel());
172 return new OpNode(parseExpression(left),parseExpression(right),op);
173 } else if (isNode(pn,"unaryplus")||
174 isNode(pn,"unaryminus")||
175 isNode(pn,"postinc")||
176 isNode(pn,"postdec")||
177 isNode(pn,"preinc")||
178 isNode(pn,"predec")) {
179 ParseNode left=pn.getFirstChild();
180 Operation op=new Operation(pn.getLabel());
181 return new OpNode(parseExpression(left),op);
182 } else if (isNode(pn,"literal")) {
183 String literaltype=pn.getTerminal();
184 ParseNode literalnode=pn.getChild(literaltype);
185 Object literal_obj=literalnode.getLiteral();
186 return new LiteralNode(literaltype, literal_obj);
187 } else if (isNode(pn,"createobject")) {
188 TypeDescriptor td=parseTypeDescriptor(pn);
189 Vector args=parseArgumentList(pn);
190 CreateObjectNode con=new CreateObjectNode(td);
191 for(int i=0;i<args.size();i++) {
192 con.addArgument((ExpressionNode)args.get(i));
195 } else if (isNode(pn,"createarray")) {
196 System.out.println(pn.PPrint(3,true));
197 TypeDescriptor td=parseTypeDescriptor(pn);
198 Vector args=parseDimExprs(pn);
200 if (pn.getChild("dims_opt").getLiteral()!=null)
201 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
202 for(int i=0;i<(args.size()+num);i++)
203 td=td.makeArray(state);
204 CreateObjectNode con=new CreateObjectNode(td);
205 for(int i=0;i<args.size();i++) {
206 con.addArgument((ExpressionNode)args.get(i));
209 } else if (isNode(pn,"name")) {
210 NameDescriptor nd=parseName(pn);
211 return new NameNode(nd);
212 } else if (isNode(pn,"this")) {
213 NameDescriptor nd=new NameDescriptor("this");
214 return new NameNode(nd);
215 } else if (isNode(pn,"methodinvoke1")) {
216 NameDescriptor nd=parseName(pn.getChild("name"));
217 Vector args=parseArgumentList(pn);
218 MethodInvokeNode min=new MethodInvokeNode(nd);
219 for(int i=0;i<args.size();i++) {
220 min.addArgument((ExpressionNode)args.get(i));
223 } else if (isNode(pn,"methodinvoke2")) {
224 String methodid=pn.getChild("id").getTerminal();
225 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
226 Vector args=parseArgumentList(pn);
227 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
228 for(int i=0;i<args.size();i++) {
229 min.addArgument((ExpressionNode)args.get(i));
232 } else if (isNode(pn,"fieldaccess")) {
233 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
234 return new FieldAccessNode(en,fieldname);
235 } else if (isNode(pn,"arrayaccess")) {
236 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
237 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
238 return new ArrayAccessNode(en,index);
239 } else if (isNode(pn,"cast1")) {
240 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
241 } else if (isNode(pn,"cast2")) {
242 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
244 System.out.println("---------------------");
245 System.out.println(pn.PPrint(3,true));
250 private Vector parseDimExprs(ParseNode pn) {
251 Vector arglist=new Vector();
252 ParseNode an=pn.getChild("dim_exprs");
253 if (an==null) /* No argument list */
255 ParseNodeVector anv=an.getChildren();
256 for(int i=0;i<anv.size();i++) {
257 arglist.add(parseExpression(anv.elementAt(i)));
262 private Vector parseArgumentList(ParseNode pn) {
263 Vector arglist=new Vector();
264 ParseNode an=pn.getChild("argument_list");
265 if (an==null) /* No argument list */
267 ParseNodeVector anv=an.getChildren();
268 for(int i=0;i<anv.size();i++) {
269 arglist.add(parseExpression(anv.elementAt(i)));
274 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
275 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
276 ParseNodeVector pnv=pn.getChild("args").getChildren();
278 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
283 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
284 ParseNode headern=pn.getChild("method_header");
285 ParseNode bodyn=pn.getChild("body");
286 MethodDescriptor md=parseMethodHeader(headern);
287 BlockNode bn=parseBlock(bodyn);
289 state.addTreeCode(md,bn);
292 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
293 ParseNode mn=pn.getChild("modifiers");
294 Modifiers m=parseModifiersList(mn);
295 ParseNode cdecl=pn.getChild("constructor_declarator");
296 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
297 MethodDescriptor md=new MethodDescriptor(m, name);
298 ParseNode paramnode=cdecl.getChild("parameters");
299 parseParameterList(md,paramnode);
300 ParseNode bodyn0=pn.getChild("body");
301 ParseNode bodyn=bodyn0.getChild("constructor_body");
304 BlockNode bn=parseBlock(bodyn);
305 state.addTreeCode(md,bn);
309 public BlockNode parseBlock(ParseNode pn) {
310 if (isEmpty(pn.getTerminal()))
311 return new BlockNode();
312 ParseNode bsn=pn.getChild("block_statement_list");
313 return parseBlockHelper(bsn);
316 private BlockNode parseBlockHelper(ParseNode pn) {
317 ParseNodeVector pnv=pn.getChildren();
318 BlockNode bn=new BlockNode();
319 for(int i=0;i<pnv.size();i++) {
320 Vector bsv=parseBlockStatement(pnv.elementAt(i));
321 for(int j=0;j<bsv.size();j++) {
322 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
328 public BlockNode parseSingleBlock(ParseNode pn) {
329 BlockNode bn=new BlockNode();
330 Vector bsv=parseBlockStatement(pn);
331 for(int j=0;j<bsv.size();j++) {
332 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
334 bn.setStyle(BlockNode.NOBRACES);
338 public Vector parseBlockStatement(ParseNode pn) {
339 Vector blockstatements=new Vector();
340 if (isNode(pn,"local_variable_declaration")) {
341 TypeDescriptor t=parseTypeDescriptor(pn);
342 ParseNode vn=pn.getChild("variable_declarators_list");
343 ParseNodeVector pnv=vn.getChildren();
344 for(int i=0;i<pnv.size();i++) {
345 ParseNode vardecl=pnv.elementAt(i);
348 ParseNode tmp=vardecl;
349 TypeDescriptor arrayt=t;
350 while (tmp.getChild("single")==null) {
351 arrayt=arrayt.makeArray(state);
352 tmp=tmp.getChild("array");
354 String identifier=tmp.getChild("single").getTerminal();
356 ParseNode epn=vardecl.getChild("initializer");
359 ExpressionNode en=null;
361 en=parseExpression(epn.getFirstChild());
363 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
365 } else if (isNode(pn,"nop")) {
367 } else if (isNode(pn,"expression")) {
368 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
369 } else if (isNode(pn,"ifstatement")) {
370 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
371 parseSingleBlock(pn.getChild("statement").getFirstChild()),
372 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
373 } else if (isNode(pn,"return")) {
374 if (isEmpty(pn.getTerminal()))
375 blockstatements.add(new ReturnNode());
377 ExpressionNode en=parseExpression(pn.getFirstChild());
378 blockstatements.add(new ReturnNode(en));
380 } else if (isNode(pn,"block_statement_list")) {
381 BlockNode bn=parseBlockHelper(pn);
382 blockstatements.add(new SubBlockNode(bn));
383 } else if (isNode(pn,"empty")) {
385 } else if (isNode(pn,"statement_expression_list")) {
386 ParseNodeVector pnv=pn.getChildren();
387 BlockNode bn=new BlockNode();
388 for(int i=0;i<pnv.size();i++) {
389 ExpressionNode en=parseExpression(pnv.elementAt(i));
390 blockstatements.add(new BlockExpressionNode(en));
392 bn.setStyle(BlockNode.EXPRLIST);
393 } else if (isNode(pn,"forstatement")) {
394 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
395 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
396 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
397 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
398 blockstatements.add(new LoopNode(init,condition,update,body));
399 } else if (isNode(pn,"whilestatement")) {
400 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
401 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
402 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
403 } else if (isNode(pn,"dowhilestatement")) {
404 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
405 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
406 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
408 System.out.println("---------------");
409 System.out.println(pn.PPrint(3,true));
412 return blockstatements;
415 public MethodDescriptor parseMethodHeader(ParseNode pn) {
416 ParseNode mn=pn.getChild("modifiers");
417 Modifiers m=parseModifiersList(mn);
419 ParseNode tn=pn.getChild("returntype");
420 TypeDescriptor returntype;
422 returntype=parseTypeDescriptor(tn);
424 returntype=new TypeDescriptor(TypeDescriptor.VOID);
426 ParseNode pmd=pn.getChild("method_declarator");
427 String name=pmd.getChild("name").getTerminal();
428 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
430 ParseNode paramnode=pmd.getChild("parameters");
431 parseParameterList(md,paramnode);
435 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
436 ParseNode paramlist=pn.getChild("formal_parameter_list");
439 ParseNodeVector pnv=paramlist.getChildren();
440 for(int i=0;i<pnv.size();i++) {
441 ParseNode paramn=pnv.elementAt(i);
442 TypeDescriptor type=parseTypeDescriptor(paramn);
444 ParseNode tmp=paramn;
445 while (tmp.getChild("single")==null) {
446 type=type.makeArray(state);
447 tmp=tmp.getChild("array");
449 String paramname=tmp.getChild("single").getTerminal();
451 md.addParameter(type,paramname);
455 public Modifiers parseModifiersList(ParseNode pn) {
456 Modifiers m=new Modifiers();
457 ParseNode modlist=pn.getChild("modifier_list");
459 ParseNodeVector pnv=modlist.getChildren();
460 for(int i=0;i<pnv.size();i++) {
461 ParseNode modn=pnv.elementAt(i);
462 if (isNode(modn,"public"))
463 m.addModifier(Modifiers.PUBLIC);
464 if (isNode(modn,"protected"))
465 m.addModifier(Modifiers.PROTECTED);
466 if (isNode(modn,"private"))
467 m.addModifier(Modifiers.PRIVATE);
468 if (isNode(modn,"static"))
469 m.addModifier(Modifiers.STATIC);
470 if (isNode(modn,"final"))
471 m.addModifier(Modifiers.FINAL);
472 if (isNode(modn,"native"))
473 m.addModifier(Modifiers.NATIVE);
479 private boolean isNode(ParseNode pn, String label) {
480 if (pn.getLabel().equals(label))
485 private static boolean isEmpty(ParseNode pn) {
486 if (pn.getLabel().equals("empty"))
492 private static boolean isEmpty(String s) {
493 if (s.equals("empty"))
499 /** Throw an exception if something is unexpected */
500 private void check(ParseNode pn, String label) {
502 throw new Error(pn+ "IE: Expected '" + label + "', got null");
504 if (! pn.getLabel().equals(label)) {
505 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");