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++) {
74 ParseNode pn2=pnv.elementAt(i);
76 if (isNode(pn2,"not")) {
78 pn2=pn2.getChild("name");
80 String name=pn2.getTerminal();
81 fes.addEffect(new FlagEffect(name,status));
85 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
86 if (pn.getChild("or")!=null) {
87 ParseNodeVector pnv=pn.getChild("or").getChildren();
88 ParseNode left=pnv.elementAt(0);
89 ParseNode right=pnv.elementAt(1);
90 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
91 } else if (pn.getChild("and")!=null) {
92 ParseNodeVector pnv=pn.getChild("and").getChildren();
93 ParseNode left=pnv.elementAt(0);
94 ParseNode right=pnv.elementAt(1);
95 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
96 } else if (pn.getChild("not")!=null) {
97 ParseNodeVector pnv=pn.getChild("not").getChildren();
98 ParseNode left=pnv.elementAt(0);
99 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
101 } else if (pn.getChild("name")!=null) {
102 return new FlagNode(pn.getChild("name").getTerminal());
103 } else throw new Error();
106 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
107 ParseNode paramlist=pn.getChild("task_parameter_list");
110 ParseNodeVector pnv=paramlist.getChildren();
111 for(int i=0;i<pnv.size();i++) {
112 ParseNode paramn=pnv.elementAt(i);
113 TypeDescriptor type=parseTypeDescriptor(paramn);
115 ParseNode tmp=paramn;
116 while (tmp.getChild("single")==null) {
117 type=type.makeArray(state);
118 tmp=tmp.getChild("array");
120 String paramname=tmp.getChild("single").getTerminal();
121 FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag"));
123 td.addParameter(type,paramname,fen);
127 public ClassDescriptor parseTypeDecl(ParseNode pn) {
128 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
129 if (!isEmpty(pn.getChild("super").getTerminal())) {
130 /* parse superclass name */
131 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
132 NameDescriptor nd=parseName(snn);
133 cn.setSuper(nd.toString());
135 if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
136 cn.setSuper(TypeUtil.ObjectClass);
138 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
139 parseClassBody(cn, pn.getChild("classbody"));
143 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
144 ParseNode decls=pn.getChild("class_body_declaration_list");
146 ParseNodeVector pnv=decls.getChildren();
147 for(int i=0;i<pnv.size();i++) {
148 ParseNode decl=pnv.elementAt(i);
149 if (isNode(decl,"member")) {
150 parseClassMember(cn,decl);
151 } else if (isNode(decl,"constructor")) {
152 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
153 } else if (isNode(decl,"block")) {
154 } else throw new Error();
159 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
160 ParseNode fieldnode=pn.getChild("field");
162 if (fieldnode!=null) {
163 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
166 ParseNode methodnode=pn.getChild("method");
167 if (methodnode!=null) {
168 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
171 ParseNode flagnode=pn.getChild("flag");
172 if (flagnode!=null) {
173 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
179 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
180 ParseNode tn=pn.getChild("type");
182 String type_st=tn.getTerminal();
183 if(type_st.equals("byte")) {
184 return state.getTypeDescriptor(TypeDescriptor.BYTE);
185 } else if(type_st.equals("short")) {
186 return state.getTypeDescriptor(TypeDescriptor.SHORT);
187 } else if(type_st.equals("boolean")) {
188 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
189 } else if(type_st.equals("int")) {
190 return state.getTypeDescriptor(TypeDescriptor.INT);
191 } else if(type_st.equals("long")) {
192 return state.getTypeDescriptor(TypeDescriptor.LONG);
193 } else if(type_st.equals("char")) {
194 return state.getTypeDescriptor(TypeDescriptor.CHAR);
195 } else if(type_st.equals("float")) {
196 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
197 } else if(type_st.equals("double")) {
198 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
199 } else if(type_st.equals("class")) {
200 ParseNode nn=tn.getChild("class");
201 return state.getTypeDescriptor(parseName(nn.getChild("name")));
202 } else if(type_st.equals("array")) {
203 ParseNode nn=tn.getChild("array");
204 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
205 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
206 for(int i=0;i<numdims.intValue();i++)
207 td=td.makeArray(state);
214 private NameDescriptor parseName(ParseNode nn) {
215 ParseNode base=nn.getChild("base");
216 ParseNode id=nn.getChild("identifier");
218 return new NameDescriptor(id.getTerminal());
219 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
223 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
224 String name=pn.getChild("name").getTerminal();
225 cn.addFlag(new FlagDescriptor(name));
228 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
229 ParseNode mn=pn.getChild("modifier");
230 Modifiers m=parseModifiersList(mn);
232 ParseNode tn=pn.getChild("type");
233 TypeDescriptor t=parseTypeDescriptor(tn);
234 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
235 ParseNodeVector pnv=vn.getChildren();
236 for(int i=0;i<pnv.size();i++) {
237 ParseNode vardecl=pnv.elementAt(i);
240 ParseNode tmp=vardecl;
241 TypeDescriptor arrayt=t;
242 while (tmp.getChild("single")==null) {
243 arrayt=arrayt.makeArray(state);
244 tmp=tmp.getChild("array");
246 String identifier=tmp.getChild("single").getTerminal();
248 ParseNode epn=vardecl.getChild("initializer");
250 ExpressionNode en=null;
252 en=parseExpression(epn.getFirstChild());
254 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
259 private ExpressionNode parseExpression(ParseNode pn) {
260 if (isNode(pn,"assignment"))
261 return parseAssignmentExpression(pn);
262 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
263 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
264 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
265 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
266 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
267 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
268 isNode(pn,"rightshift")||isNode(pn,"sub")||
269 isNode(pn,"add")||isNode(pn,"mult")||
270 isNode(pn,"div")||isNode(pn,"mod")) {
271 ParseNodeVector pnv=pn.getChildren();
272 ParseNode left=pnv.elementAt(0);
273 ParseNode right=pnv.elementAt(1);
274 Operation op=new Operation(pn.getLabel());
275 return new OpNode(parseExpression(left),parseExpression(right),op);
276 } else if (isNode(pn,"unaryplus")||
277 isNode(pn,"unaryminus")||
279 ParseNode left=pn.getFirstChild();
280 Operation op=new Operation(pn.getLabel());
281 return new OpNode(parseExpression(left),op);
282 } else if (isNode(pn,"postinc")||
283 isNode(pn,"postdec")) {
284 ParseNode left=pn.getFirstChild();
285 AssignOperation op=new AssignOperation(pn.getLabel());
286 return new AssignmentNode(parseExpression(left),null,op);
288 } else if (isNode(pn,"preinc")||
289 isNode(pn,"predec")) {
290 ParseNode left=pn.getFirstChild();
291 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
292 return new AssignmentNode(parseExpression(left),
293 new LiteralNode("integer",new Integer(1)),op);
294 } else if (isNode(pn,"literal")) {
295 String literaltype=pn.getTerminal();
296 ParseNode literalnode=pn.getChild(literaltype);
297 Object literal_obj=literalnode.getLiteral();
298 return new LiteralNode(literaltype, literal_obj);
299 } else if (isNode(pn,"createobject")) {
300 TypeDescriptor td=parseTypeDescriptor(pn);
301 Vector args=parseArgumentList(pn);
302 CreateObjectNode con=new CreateObjectNode(td);
303 for(int i=0;i<args.size();i++) {
304 con.addArgument((ExpressionNode)args.get(i));
306 /* Could have flag set here */
307 if (pn.getChild("flag_list")!=null) {
308 FlagEffects fe=new FlagEffects(null);
309 parseFlagEffect(fe, pn.getChild("flag_list"));
310 con.addFlagEffects(fe);
313 } else if (isNode(pn,"createarray")) {
314 System.out.println(pn.PPrint(3,true));
315 TypeDescriptor td=parseTypeDescriptor(pn);
316 Vector args=parseDimExprs(pn);
318 if (pn.getChild("dims_opt").getLiteral()!=null)
319 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
320 for(int i=0;i<(args.size()+num);i++)
321 td=td.makeArray(state);
322 CreateObjectNode con=new CreateObjectNode(td);
323 for(int i=0;i<args.size();i++) {
324 con.addArgument((ExpressionNode)args.get(i));
327 } else if (isNode(pn,"name")) {
328 NameDescriptor nd=parseName(pn);
329 return new NameNode(nd);
330 } else if (isNode(pn,"this")) {
331 NameDescriptor nd=new NameDescriptor("this");
332 return new NameNode(nd);
333 } else if (isNode(pn,"methodinvoke1")) {
334 NameDescriptor nd=parseName(pn.getChild("name"));
335 Vector args=parseArgumentList(pn);
336 MethodInvokeNode min=new MethodInvokeNode(nd);
337 for(int i=0;i<args.size();i++) {
338 min.addArgument((ExpressionNode)args.get(i));
341 } else if (isNode(pn,"methodinvoke2")) {
342 String methodid=pn.getChild("id").getTerminal();
343 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
344 Vector args=parseArgumentList(pn);
345 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
346 for(int i=0;i<args.size();i++) {
347 min.addArgument((ExpressionNode)args.get(i));
350 } else if (isNode(pn,"fieldaccess")) {
351 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
352 return new FieldAccessNode(en,fieldname);
353 } else if (isNode(pn,"arrayaccess")) {
354 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
355 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
356 return new ArrayAccessNode(en,index);
357 } else if (isNode(pn,"cast1")) {
358 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
359 } else if (isNode(pn,"cast2")) {
360 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
362 System.out.println("---------------------");
363 System.out.println(pn.PPrint(3,true));
368 private Vector parseDimExprs(ParseNode pn) {
369 Vector arglist=new Vector();
370 ParseNode an=pn.getChild("dim_exprs");
371 if (an==null) /* No argument list */
373 ParseNodeVector anv=an.getChildren();
374 for(int i=0;i<anv.size();i++) {
375 arglist.add(parseExpression(anv.elementAt(i)));
380 private Vector parseArgumentList(ParseNode pn) {
381 Vector arglist=new Vector();
382 ParseNode an=pn.getChild("argument_list");
383 if (an==null) /* No argument list */
385 ParseNodeVector anv=an.getChildren();
386 for(int i=0;i<anv.size();i++) {
387 arglist.add(parseExpression(anv.elementAt(i)));
392 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
393 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
394 ParseNodeVector pnv=pn.getChild("args").getChildren();
396 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
401 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
402 ParseNode headern=pn.getChild("method_header");
403 ParseNode bodyn=pn.getChild("body");
404 MethodDescriptor md=parseMethodHeader(headern);
405 BlockNode bn=parseBlock(bodyn);
407 state.addTreeCode(md,bn);
410 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
411 ParseNode mn=pn.getChild("modifiers");
412 Modifiers m=parseModifiersList(mn);
413 ParseNode cdecl=pn.getChild("constructor_declarator");
414 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
415 MethodDescriptor md=new MethodDescriptor(m, name);
416 ParseNode paramnode=cdecl.getChild("parameters");
417 parseParameterList(md,paramnode);
418 ParseNode bodyn0=pn.getChild("body");
419 ParseNode bodyn=bodyn0.getChild("constructor_body");
421 BlockNode bn=parseBlock(bodyn);
422 state.addTreeCode(md,bn);
425 public BlockNode parseBlock(ParseNode pn) {
426 if (pn==null||isEmpty(pn.getTerminal()))
427 return new BlockNode();
428 ParseNode bsn=pn.getChild("block_statement_list");
429 return parseBlockHelper(bsn);
432 private BlockNode parseBlockHelper(ParseNode pn) {
433 ParseNodeVector pnv=pn.getChildren();
434 BlockNode bn=new BlockNode();
435 for(int i=0;i<pnv.size();i++) {
436 Vector bsv=parseBlockStatement(pnv.elementAt(i));
437 for(int j=0;j<bsv.size();j++) {
438 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
444 public BlockNode parseSingleBlock(ParseNode pn) {
445 BlockNode bn=new BlockNode();
446 Vector bsv=parseBlockStatement(pn);
447 for(int j=0;j<bsv.size();j++) {
448 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
450 bn.setStyle(BlockNode.NOBRACES);
454 public Vector parseBlockStatement(ParseNode pn) {
455 Vector blockstatements=new Vector();
456 if (isNode(pn,"local_variable_declaration")) {
457 TypeDescriptor t=parseTypeDescriptor(pn);
458 ParseNode vn=pn.getChild("variable_declarators_list");
459 ParseNodeVector pnv=vn.getChildren();
460 for(int i=0;i<pnv.size();i++) {
461 ParseNode vardecl=pnv.elementAt(i);
464 ParseNode tmp=vardecl;
465 TypeDescriptor arrayt=t;
466 while (tmp.getChild("single")==null) {
467 arrayt=arrayt.makeArray(state);
468 tmp=tmp.getChild("array");
470 String identifier=tmp.getChild("single").getTerminal();
472 ParseNode epn=vardecl.getChild("initializer");
475 ExpressionNode en=null;
477 en=parseExpression(epn.getFirstChild());
479 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
481 } else if (isNode(pn,"nop")) {
483 } else if (isNode(pn,"expression")) {
484 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
485 } else if (isNode(pn,"ifstatement")) {
486 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
487 parseSingleBlock(pn.getChild("statement").getFirstChild()),
488 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
489 } else if (isNode(pn,"taskexit")) {
491 if (pn.getChild("flag_effects_list")!=null)
492 vfe=parseFlags(pn.getChild("flag_effects_list"));
493 blockstatements.add(new TaskExitNode(vfe));
494 } else if (isNode(pn,"return")) {
495 if (isEmpty(pn.getTerminal()))
496 blockstatements.add(new ReturnNode());
498 ExpressionNode en=parseExpression(pn.getFirstChild());
499 blockstatements.add(new ReturnNode(en));
501 } else if (isNode(pn,"block_statement_list")) {
502 BlockNode bn=parseBlockHelper(pn);
503 blockstatements.add(new SubBlockNode(bn));
504 } else if (isNode(pn,"empty")) {
506 } else if (isNode(pn,"statement_expression_list")) {
507 ParseNodeVector pnv=pn.getChildren();
508 BlockNode bn=new BlockNode();
509 for(int i=0;i<pnv.size();i++) {
510 ExpressionNode en=parseExpression(pnv.elementAt(i));
511 blockstatements.add(new BlockExpressionNode(en));
513 bn.setStyle(BlockNode.EXPRLIST);
514 } else if (isNode(pn,"forstatement")) {
515 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
516 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
517 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
518 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
519 blockstatements.add(new LoopNode(init,condition,update,body));
520 } else if (isNode(pn,"whilestatement")) {
521 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
522 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
523 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
524 } else if (isNode(pn,"dowhilestatement")) {
525 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
526 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
527 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
529 System.out.println("---------------");
530 System.out.println(pn.PPrint(3,true));
533 return blockstatements;
536 public MethodDescriptor parseMethodHeader(ParseNode pn) {
537 ParseNode mn=pn.getChild("modifiers");
538 Modifiers m=parseModifiersList(mn);
540 ParseNode tn=pn.getChild("returntype");
541 TypeDescriptor returntype;
543 returntype=parseTypeDescriptor(tn);
545 returntype=new TypeDescriptor(TypeDescriptor.VOID);
547 ParseNode pmd=pn.getChild("method_declarator");
548 String name=pmd.getChild("name").getTerminal();
549 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
551 ParseNode paramnode=pmd.getChild("parameters");
552 parseParameterList(md,paramnode);
556 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
557 ParseNode paramlist=pn.getChild("formal_parameter_list");
560 ParseNodeVector pnv=paramlist.getChildren();
561 for(int i=0;i<pnv.size();i++) {
562 ParseNode paramn=pnv.elementAt(i);
563 TypeDescriptor type=parseTypeDescriptor(paramn);
565 ParseNode tmp=paramn;
566 while (tmp.getChild("single")==null) {
567 type=type.makeArray(state);
568 tmp=tmp.getChild("array");
570 String paramname=tmp.getChild("single").getTerminal();
572 md.addParameter(type,paramname);
576 public Modifiers parseModifiersList(ParseNode pn) {
577 Modifiers m=new Modifiers();
578 ParseNode modlist=pn.getChild("modifier_list");
580 ParseNodeVector pnv=modlist.getChildren();
581 for(int i=0;i<pnv.size();i++) {
582 ParseNode modn=pnv.elementAt(i);
583 if (isNode(modn,"public"))
584 m.addModifier(Modifiers.PUBLIC);
585 if (isNode(modn,"protected"))
586 m.addModifier(Modifiers.PROTECTED);
587 if (isNode(modn,"private"))
588 m.addModifier(Modifiers.PRIVATE);
589 if (isNode(modn,"static"))
590 m.addModifier(Modifiers.STATIC);
591 if (isNode(modn,"final"))
592 m.addModifier(Modifiers.FINAL);
593 if (isNode(modn,"native"))
594 m.addModifier(Modifiers.NATIVE);
600 private boolean isNode(ParseNode pn, String label) {
601 if (pn.getLabel().equals(label))
606 private static boolean isEmpty(ParseNode pn) {
607 if (pn.getLabel().equals("empty"))
613 private static boolean isEmpty(String s) {
614 if (s.equals("empty"))
620 /** Throw an exception if something is unexpected */
621 private void check(ParseNode pn, String label) {
623 throw new Error(pn+ "IE: Expected '" + label + "', got null");
625 if (! pn.getLabel().equals(label)) {
626 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");