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 if (isNode(type_pn,"class_declaration")) {
28 ClassDescriptor cn=parseTypeDecl(type_pn);
30 } else if (isNode(type_pn,"task_declaration")) {
31 TaskDescriptor td=parseTaskDecl(type_pn);
34 throw new Error(type_pn.getLabel());
40 public TaskDescriptor parseTaskDecl(ParseNode pn) {
41 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
42 ParseNode bodyn=pn.getChild("body");
43 BlockNode bn=parseBlock(bodyn);
44 parseParameterList(td, pn);
45 state.addTreeCode(td,bn);
46 if (pn.getChild("flag_effects_list")!=null)
47 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
51 public Vector parseFlags(ParseNode pn) {
52 Vector vfe=new Vector();
53 ParseNodeVector pnv=pn.getChildren();
54 for(int i=0;i<pnv.size();i++) {
55 ParseNode fn=pnv.elementAt(i);
56 FlagEffects fe=parseFlagEffects(fn);
62 public FlagEffects parseFlagEffects(ParseNode pn) {
63 if (isNode(pn,"flag_effect")) {
64 String flagname=pn.getChild("name").getTerminal();
65 FlagEffects fe=new FlagEffects(flagname);
66 parseFlagEffect(fe, pn.getChild("flag_list"));
68 } else throw new Error();
71 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
72 ParseNodeVector pnv=pn.getChildren();
73 for(int i=0;i<pnv.size();i++) {
75 if (pn.getChild("not")!=null) {
77 pn=pn.getChild("not");
79 String name=pn.getChild("name").getTerminal();
80 fes.addEffect(new FlagEffect(name,status));
84 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
85 if (pn.getChild("or")!=null) {
86 ParseNodeVector pnv=pn.getChild("or").getChildren();
87 ParseNode left=pnv.elementAt(0);
88 ParseNode right=pnv.elementAt(1);
89 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
90 } else if (pn.getChild("and")!=null) {
91 ParseNodeVector pnv=pn.getChild("and").getChildren();
92 ParseNode left=pnv.elementAt(0);
93 ParseNode right=pnv.elementAt(1);
94 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
95 } else if (pn.getChild("not")!=null) {
96 ParseNodeVector pnv=pn.getChild("not").getChildren();
97 ParseNode left=pnv.elementAt(0);
98 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
100 } else if (pn.getChild("name")!=null) {
101 return new FlagNode(pn.getChild("name").getTerminal());
102 } else throw new Error();
105 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
106 ParseNode paramlist=pn.getChild("task_parameter_list");
109 ParseNodeVector pnv=paramlist.getChildren();
110 for(int i=0;i<pnv.size();i++) {
111 ParseNode paramn=pnv.elementAt(i);
112 TypeDescriptor type=parseTypeDescriptor(paramn);
114 ParseNode tmp=paramn;
115 while (tmp.getChild("single")==null) {
116 type=type.makeArray(state);
117 tmp=tmp.getChild("array");
119 String paramname=tmp.getChild("single").getTerminal();
120 FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag"));
122 td.addParameter(type,paramname,fen);
126 public ClassDescriptor parseTypeDecl(ParseNode pn) {
127 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
128 if (!isEmpty(pn.getChild("super").getTerminal())) {
129 /* parse superclass name */
130 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
131 NameDescriptor nd=parseName(snn);
132 cn.setSuper(nd.toString());
134 if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
135 cn.setSuper(TypeUtil.ObjectClass);
137 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
138 parseClassBody(cn, pn.getChild("classbody"));
142 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
143 ParseNode decls=pn.getChild("class_body_declaration_list");
145 ParseNodeVector pnv=decls.getChildren();
146 for(int i=0;i<pnv.size();i++) {
147 ParseNode decl=pnv.elementAt(i);
148 if (isNode(decl,"member")) {
149 parseClassMember(cn,decl);
150 } else if (isNode(decl,"constructor")) {
151 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
152 } else if (isNode(decl,"block")) {
153 } else throw new Error();
158 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
159 ParseNode fieldnode=pn.getChild("field");
161 if (fieldnode!=null) {
162 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
165 ParseNode methodnode=pn.getChild("method");
166 if (methodnode!=null) {
167 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
170 ParseNode flagnode=pn.getChild("flag");
171 if (flagnode!=null) {
172 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
178 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
179 ParseNode tn=pn.getChild("type");
181 String type_st=tn.getTerminal();
182 if(type_st.equals("byte")) {
183 return state.getTypeDescriptor(TypeDescriptor.BYTE);
184 } else if(type_st.equals("short")) {
185 return state.getTypeDescriptor(TypeDescriptor.SHORT);
186 } else if(type_st.equals("boolean")) {
187 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
188 } else if(type_st.equals("int")) {
189 return state.getTypeDescriptor(TypeDescriptor.INT);
190 } else if(type_st.equals("long")) {
191 return state.getTypeDescriptor(TypeDescriptor.LONG);
192 } else if(type_st.equals("char")) {
193 return state.getTypeDescriptor(TypeDescriptor.CHAR);
194 } else if(type_st.equals("float")) {
195 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
196 } else if(type_st.equals("double")) {
197 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
198 } else if(type_st.equals("class")) {
199 ParseNode nn=tn.getChild("class");
200 return state.getTypeDescriptor(parseName(nn.getChild("name")));
201 } else if(type_st.equals("array")) {
202 ParseNode nn=tn.getChild("array");
203 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
204 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
205 for(int i=0;i<numdims.intValue();i++)
206 td=td.makeArray(state);
213 private NameDescriptor parseName(ParseNode nn) {
214 ParseNode base=nn.getChild("base");
215 ParseNode id=nn.getChild("identifier");
217 return new NameDescriptor(id.getTerminal());
218 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
222 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
223 String name=pn.getChild("name").getTerminal();
224 cn.addFlag(new FlagDescriptor(name));
227 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
228 ParseNode mn=pn.getChild("modifier");
229 Modifiers m=parseModifiersList(mn);
231 ParseNode tn=pn.getChild("type");
232 TypeDescriptor t=parseTypeDescriptor(tn);
233 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
234 ParseNodeVector pnv=vn.getChildren();
235 for(int i=0;i<pnv.size();i++) {
236 ParseNode vardecl=pnv.elementAt(i);
239 ParseNode tmp=vardecl;
240 TypeDescriptor arrayt=t;
241 while (tmp.getChild("single")==null) {
242 arrayt=arrayt.makeArray(state);
243 tmp=tmp.getChild("array");
245 String identifier=tmp.getChild("single").getTerminal();
247 ParseNode epn=vardecl.getChild("initializer");
249 ExpressionNode en=null;
251 en=parseExpression(epn.getFirstChild());
253 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
258 private ExpressionNode parseExpression(ParseNode pn) {
259 if (isNode(pn,"assignment"))
260 return parseAssignmentExpression(pn);
261 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
262 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
263 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
264 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
265 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
266 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
267 isNode(pn,"rightshift")||isNode(pn,"sub")||
268 isNode(pn,"add")||isNode(pn,"mult")||
269 isNode(pn,"div")||isNode(pn,"mod")) {
270 ParseNodeVector pnv=pn.getChildren();
271 ParseNode left=pnv.elementAt(0);
272 ParseNode right=pnv.elementAt(1);
273 Operation op=new Operation(pn.getLabel());
274 return new OpNode(parseExpression(left),parseExpression(right),op);
275 } else if (isNode(pn,"unaryplus")||
276 isNode(pn,"unaryminus")||
278 ParseNode left=pn.getFirstChild();
279 Operation op=new Operation(pn.getLabel());
280 return new OpNode(parseExpression(left),op);
281 } else if (isNode(pn,"postinc")||
282 isNode(pn,"postdec")) {
283 ParseNode left=pn.getFirstChild();
284 AssignOperation op=new AssignOperation(pn.getLabel());
285 return new AssignmentNode(parseExpression(left),null,op);
287 } else if (isNode(pn,"preinc")||
288 isNode(pn,"predec")) {
289 ParseNode left=pn.getFirstChild();
290 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
291 return new AssignmentNode(parseExpression(left),
292 new LiteralNode("integer",new Integer(1)),op);
293 } else if (isNode(pn,"literal")) {
294 String literaltype=pn.getTerminal();
295 ParseNode literalnode=pn.getChild(literaltype);
296 Object literal_obj=literalnode.getLiteral();
297 return new LiteralNode(literaltype, literal_obj);
298 } else if (isNode(pn,"createobject")) {
299 TypeDescriptor td=parseTypeDescriptor(pn);
300 Vector args=parseArgumentList(pn);
301 CreateObjectNode con=new CreateObjectNode(td);
302 for(int i=0;i<args.size();i++) {
303 con.addArgument((ExpressionNode)args.get(i));
305 /* Could have flag set here */
306 if (pn.getChild("flag_list")!=null) {
307 FlagEffects fe=new FlagEffects(null);
308 parseFlagEffect(fe, pn.getChild("flag_list"));
309 con.addFlagEffects(fe);
312 } else if (isNode(pn,"createarray")) {
313 System.out.println(pn.PPrint(3,true));
314 TypeDescriptor td=parseTypeDescriptor(pn);
315 Vector args=parseDimExprs(pn);
317 if (pn.getChild("dims_opt").getLiteral()!=null)
318 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
319 for(int i=0;i<(args.size()+num);i++)
320 td=td.makeArray(state);
321 CreateObjectNode con=new CreateObjectNode(td);
322 for(int i=0;i<args.size();i++) {
323 con.addArgument((ExpressionNode)args.get(i));
326 } else if (isNode(pn,"name")) {
327 NameDescriptor nd=parseName(pn);
328 return new NameNode(nd);
329 } else if (isNode(pn,"this")) {
330 NameDescriptor nd=new NameDescriptor("this");
331 return new NameNode(nd);
332 } else if (isNode(pn,"methodinvoke1")) {
333 NameDescriptor nd=parseName(pn.getChild("name"));
334 Vector args=parseArgumentList(pn);
335 MethodInvokeNode min=new MethodInvokeNode(nd);
336 for(int i=0;i<args.size();i++) {
337 min.addArgument((ExpressionNode)args.get(i));
340 } else if (isNode(pn,"methodinvoke2")) {
341 String methodid=pn.getChild("id").getTerminal();
342 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
343 Vector args=parseArgumentList(pn);
344 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
345 for(int i=0;i<args.size();i++) {
346 min.addArgument((ExpressionNode)args.get(i));
349 } else if (isNode(pn,"fieldaccess")) {
350 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
351 return new FieldAccessNode(en,fieldname);
352 } else if (isNode(pn,"arrayaccess")) {
353 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
354 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
355 return new ArrayAccessNode(en,index);
356 } else if (isNode(pn,"cast1")) {
357 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
358 } else if (isNode(pn,"cast2")) {
359 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
361 System.out.println("---------------------");
362 System.out.println(pn.PPrint(3,true));
367 private Vector parseDimExprs(ParseNode pn) {
368 Vector arglist=new Vector();
369 ParseNode an=pn.getChild("dim_exprs");
370 if (an==null) /* No argument list */
372 ParseNodeVector anv=an.getChildren();
373 for(int i=0;i<anv.size();i++) {
374 arglist.add(parseExpression(anv.elementAt(i)));
379 private Vector parseArgumentList(ParseNode pn) {
380 Vector arglist=new Vector();
381 ParseNode an=pn.getChild("argument_list");
382 if (an==null) /* No argument list */
384 ParseNodeVector anv=an.getChildren();
385 for(int i=0;i<anv.size();i++) {
386 arglist.add(parseExpression(anv.elementAt(i)));
391 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
392 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
393 ParseNodeVector pnv=pn.getChild("args").getChildren();
395 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
400 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
401 ParseNode headern=pn.getChild("method_header");
402 ParseNode bodyn=pn.getChild("body");
403 MethodDescriptor md=parseMethodHeader(headern);
404 BlockNode bn=parseBlock(bodyn);
406 state.addTreeCode(md,bn);
409 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
410 ParseNode mn=pn.getChild("modifiers");
411 Modifiers m=parseModifiersList(mn);
412 ParseNode cdecl=pn.getChild("constructor_declarator");
413 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
414 MethodDescriptor md=new MethodDescriptor(m, name);
415 ParseNode paramnode=cdecl.getChild("parameters");
416 parseParameterList(md,paramnode);
417 ParseNode bodyn0=pn.getChild("body");
418 ParseNode bodyn=bodyn0.getChild("constructor_body");
420 BlockNode bn=parseBlock(bodyn);
421 state.addTreeCode(md,bn);
424 public BlockNode parseBlock(ParseNode pn) {
425 if (pn==null||isEmpty(pn.getTerminal()))
426 return new BlockNode();
427 ParseNode bsn=pn.getChild("block_statement_list");
428 return parseBlockHelper(bsn);
431 private BlockNode parseBlockHelper(ParseNode pn) {
432 ParseNodeVector pnv=pn.getChildren();
433 BlockNode bn=new BlockNode();
434 for(int i=0;i<pnv.size();i++) {
435 Vector bsv=parseBlockStatement(pnv.elementAt(i));
436 for(int j=0;j<bsv.size();j++) {
437 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
443 public BlockNode parseSingleBlock(ParseNode pn) {
444 BlockNode bn=new BlockNode();
445 Vector bsv=parseBlockStatement(pn);
446 for(int j=0;j<bsv.size();j++) {
447 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
449 bn.setStyle(BlockNode.NOBRACES);
453 public Vector parseBlockStatement(ParseNode pn) {
454 Vector blockstatements=new Vector();
455 if (isNode(pn,"local_variable_declaration")) {
456 TypeDescriptor t=parseTypeDescriptor(pn);
457 ParseNode vn=pn.getChild("variable_declarators_list");
458 ParseNodeVector pnv=vn.getChildren();
459 for(int i=0;i<pnv.size();i++) {
460 ParseNode vardecl=pnv.elementAt(i);
463 ParseNode tmp=vardecl;
464 TypeDescriptor arrayt=t;
465 while (tmp.getChild("single")==null) {
466 arrayt=arrayt.makeArray(state);
467 tmp=tmp.getChild("array");
469 String identifier=tmp.getChild("single").getTerminal();
471 ParseNode epn=vardecl.getChild("initializer");
474 ExpressionNode en=null;
476 en=parseExpression(epn.getFirstChild());
478 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
480 } else if (isNode(pn,"nop")) {
482 } else if (isNode(pn,"expression")) {
483 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
484 } else if (isNode(pn,"ifstatement")) {
485 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
486 parseSingleBlock(pn.getChild("statement").getFirstChild()),
487 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
488 } else if (isNode(pn,"taskexit")) {
490 if (pn.getChild("flag_effects_list")!=null)
491 vfe=parseFlags(pn.getChild("flag_effects_list"));
492 blockstatements.add(new TaskExitNode(vfe));
493 } else if (isNode(pn,"return")) {
494 if (isEmpty(pn.getTerminal()))
495 blockstatements.add(new ReturnNode());
497 ExpressionNode en=parseExpression(pn.getFirstChild());
498 blockstatements.add(new ReturnNode(en));
500 } else if (isNode(pn,"block_statement_list")) {
501 BlockNode bn=parseBlockHelper(pn);
502 blockstatements.add(new SubBlockNode(bn));
503 } else if (isNode(pn,"empty")) {
505 } else if (isNode(pn,"statement_expression_list")) {
506 ParseNodeVector pnv=pn.getChildren();
507 BlockNode bn=new BlockNode();
508 for(int i=0;i<pnv.size();i++) {
509 ExpressionNode en=parseExpression(pnv.elementAt(i));
510 blockstatements.add(new BlockExpressionNode(en));
512 bn.setStyle(BlockNode.EXPRLIST);
513 } else if (isNode(pn,"forstatement")) {
514 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
515 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
516 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
517 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
518 blockstatements.add(new LoopNode(init,condition,update,body));
519 } else if (isNode(pn,"whilestatement")) {
520 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
521 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
522 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
523 } else if (isNode(pn,"dowhilestatement")) {
524 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
525 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
526 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
528 System.out.println("---------------");
529 System.out.println(pn.PPrint(3,true));
532 return blockstatements;
535 public MethodDescriptor parseMethodHeader(ParseNode pn) {
536 ParseNode mn=pn.getChild("modifiers");
537 Modifiers m=parseModifiersList(mn);
539 ParseNode tn=pn.getChild("returntype");
540 TypeDescriptor returntype;
542 returntype=parseTypeDescriptor(tn);
544 returntype=new TypeDescriptor(TypeDescriptor.VOID);
546 ParseNode pmd=pn.getChild("method_declarator");
547 String name=pmd.getChild("name").getTerminal();
548 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
550 ParseNode paramnode=pmd.getChild("parameters");
551 parseParameterList(md,paramnode);
555 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
556 ParseNode paramlist=pn.getChild("formal_parameter_list");
559 ParseNodeVector pnv=paramlist.getChildren();
560 for(int i=0;i<pnv.size();i++) {
561 ParseNode paramn=pnv.elementAt(i);
562 TypeDescriptor type=parseTypeDescriptor(paramn);
564 ParseNode tmp=paramn;
565 while (tmp.getChild("single")==null) {
566 type=type.makeArray(state);
567 tmp=tmp.getChild("array");
569 String paramname=tmp.getChild("single").getTerminal();
571 md.addParameter(type,paramname);
575 public Modifiers parseModifiersList(ParseNode pn) {
576 Modifiers m=new Modifiers();
577 ParseNode modlist=pn.getChild("modifier_list");
579 ParseNodeVector pnv=modlist.getChildren();
580 for(int i=0;i<pnv.size();i++) {
581 ParseNode modn=pnv.elementAt(i);
582 if (isNode(modn,"public"))
583 m.addModifier(Modifiers.PUBLIC);
584 if (isNode(modn,"protected"))
585 m.addModifier(Modifiers.PROTECTED);
586 if (isNode(modn,"private"))
587 m.addModifier(Modifiers.PRIVATE);
588 if (isNode(modn,"static"))
589 m.addModifier(Modifiers.STATIC);
590 if (isNode(modn,"final"))
591 m.addModifier(Modifiers.FINAL);
592 if (isNode(modn,"native"))
593 m.addModifier(Modifiers.NATIVE);
599 private boolean isNode(ParseNode pn, String label) {
600 if (pn.getLabel().equals(label))
605 private static boolean isEmpty(ParseNode pn) {
606 if (pn.getLabel().equals("empty"))
612 private static boolean isEmpty(String s) {
613 if (s.equals("empty"))
619 /** Throw an exception if something is unexpected */
620 private void check(ParseNode pn, String label) {
622 throw new Error(pn+ "IE: Expected '" + label + "', got null");
624 if (! pn.getLabel().equals(label)) {
625 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");