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);
38 public TaskDescriptor parseTaskDecl(ParseNode pn) {
39 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
40 ParseNode bodyn=pn.getChild("body");
41 BlockNode bn=parseBlock(bodyn);
42 parseParameterList(td, pn);
43 state.addTreeCode(td,bn);
44 if (pn.getChild("flag_effects_list")!=null)
45 td.addFlagEffects(parseFlagEffects(pn.getChild("flag_effects_list")));
49 public FlagEffects parseFlagEffects(ParseNode pn) {
50 ParseNodeVector pnv=pn.getChildren();
51 FlagEffects fes=new FlagEffects();
52 for(int i=0;i<pnv.size();i++) {
53 ParseNode fn=pnv.elementAt(i);
54 Vector vfe=parseFlagEffect(fn);
55 for (int j=0;j<vfe.size();j++) {
56 FlagEffect fe=(FlagEffect)vfe.get(j);
63 public Vector parseFlagEffect(ParseNode pn) {
65 if (pn.isNode("flag_effect")) {
66 Vector v=new Vector();
67 String flagname=pn.getChild("name").getTerminal();
71 } else throw new Error();
74 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
75 if (pn.getChild("or")!=null) {
76 ParseNodeVector pnv=pn.getChild("or").getChildren();
77 ParseNode left=pnv.elementAt(0);
78 ParseNode right=pnv.elementAt(1);
79 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
80 } else if (pn.getChild("and")!=null) {
81 ParseNodeVector pnv=pn.getChild("and").getChildren();
82 ParseNode left=pnv.elementAt(0);
83 ParseNode right=pnv.elementAt(1);
84 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
85 } else if (pn.getChild("not")!=null) {
86 ParseNodeVector pnv=pn.getChild("not").getChildren();
87 ParseNode left=pnv.elementAt(0);
88 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
90 } else if (pn.getChild("name")!=null) {
91 return new FlagNode(pn.getChild("name").getTerminal());
92 } else throw new Error();
95 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
96 ParseNode paramlist=pn.getChild("task_parameter_list");
99 ParseNodeVector pnv=paramlist.getChildren();
100 for(int i=0;i<pnv.size();i++) {
101 ParseNode paramn=pnv.elementAt(i);
102 TypeDescriptor type=parseTypeDescriptor(paramn);
104 ParseNode tmp=paramn;
105 while (tmp.getChild("single")==null) {
106 type=type.makeArray(state);
107 tmp=tmp.getChild("array");
109 String paramname=tmp.getChild("single").getTerminal();
110 FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag"));
112 td.addParameter(type,paramname,fen);
116 public ClassDescriptor parseTypeDecl(ParseNode pn) {
117 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
118 if (!isEmpty(pn.getChild("super").getTerminal())) {
119 /* parse superclass name */
120 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
121 NameDescriptor nd=parseName(snn);
122 cn.setSuper(nd.toString());
124 if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
125 cn.setSuper(TypeUtil.ObjectClass);
127 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
128 parseClassBody(cn, pn.getChild("classbody"));
132 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
133 ParseNode decls=pn.getChild("class_body_declaration_list");
135 ParseNodeVector pnv=decls.getChildren();
136 for(int i=0;i<pnv.size();i++) {
137 ParseNode decl=pnv.elementAt(i);
138 if (isNode(decl,"member")) {
139 parseClassMember(cn,decl);
140 } else if (isNode(decl,"constructor")) {
141 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
142 } else if (isNode(decl,"block")) {
143 } else throw new Error();
148 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
149 ParseNode fieldnode=pn.getChild("field");
151 if (fieldnode!=null) {
152 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
155 ParseNode methodnode=pn.getChild("method");
156 if (methodnode!=null) {
157 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
163 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
164 ParseNode tn=pn.getChild("type");
165 String type_st=tn.getTerminal();
166 if(type_st.equals("byte")) {
167 return state.getTypeDescriptor(TypeDescriptor.BYTE);
168 } else if(type_st.equals("short")) {
169 return state.getTypeDescriptor(TypeDescriptor.SHORT);
170 } else if(type_st.equals("boolean")) {
171 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
172 } else if(type_st.equals("int")) {
173 return state.getTypeDescriptor(TypeDescriptor.INT);
174 } else if(type_st.equals("long")) {
175 return state.getTypeDescriptor(TypeDescriptor.LONG);
176 } else if(type_st.equals("char")) {
177 return state.getTypeDescriptor(TypeDescriptor.CHAR);
178 } else if(type_st.equals("float")) {
179 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
180 } else if(type_st.equals("double")) {
181 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
182 } else if(type_st.equals("class")) {
183 ParseNode nn=tn.getChild("class");
184 return state.getTypeDescriptor(parseName(nn.getChild("name")));
185 } else if(type_st.equals("array")) {
186 ParseNode nn=tn.getChild("array");
187 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
188 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
189 for(int i=0;i<numdims.intValue();i++)
190 td=td.makeArray(state);
197 private NameDescriptor parseName(ParseNode nn) {
198 ParseNode base=nn.getChild("base");
199 ParseNode id=nn.getChild("identifier");
201 return new NameDescriptor(id.getTerminal());
202 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
206 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
207 ParseNode mn=pn.getChild("modifier");
208 Modifiers m=parseModifiersList(mn);
210 ParseNode tn=pn.getChild("type");
211 TypeDescriptor t=parseTypeDescriptor(tn);
212 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
213 ParseNodeVector pnv=vn.getChildren();
214 for(int i=0;i<pnv.size();i++) {
215 ParseNode vardecl=pnv.elementAt(i);
218 ParseNode tmp=vardecl;
219 TypeDescriptor arrayt=t;
220 while (tmp.getChild("single")==null) {
221 arrayt=arrayt.makeArray(state);
222 tmp=tmp.getChild("array");
224 String identifier=tmp.getChild("single").getTerminal();
226 ParseNode epn=vardecl.getChild("initializer");
228 ExpressionNode en=null;
230 en=parseExpression(epn.getFirstChild());
232 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
237 private ExpressionNode parseExpression(ParseNode pn) {
238 if (isNode(pn,"assignment"))
239 return parseAssignmentExpression(pn);
240 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
241 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
242 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
243 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
244 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
245 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
246 isNode(pn,"rightshift")||isNode(pn,"sub")||
247 isNode(pn,"add")||isNode(pn,"mult")||
248 isNode(pn,"div")||isNode(pn,"mod")) {
249 ParseNodeVector pnv=pn.getChildren();
250 ParseNode left=pnv.elementAt(0);
251 ParseNode right=pnv.elementAt(1);
252 Operation op=new Operation(pn.getLabel());
253 return new OpNode(parseExpression(left),parseExpression(right),op);
254 } else if (isNode(pn,"unaryplus")||
255 isNode(pn,"unaryminus")||
256 isNode(pn,"postinc")||
257 isNode(pn,"postdec")||
258 isNode(pn,"preinc")||
260 isNode(pn,"predec")) {
261 ParseNode left=pn.getFirstChild();
262 Operation op=new Operation(pn.getLabel());
263 return new OpNode(parseExpression(left),op);
264 } else if (isNode(pn,"literal")) {
265 String literaltype=pn.getTerminal();
266 ParseNode literalnode=pn.getChild(literaltype);
267 Object literal_obj=literalnode.getLiteral();
268 return new LiteralNode(literaltype, literal_obj);
269 } else if (isNode(pn,"createobject")) {
270 TypeDescriptor td=parseTypeDescriptor(pn);
271 Vector args=parseArgumentList(pn);
272 CreateObjectNode con=new CreateObjectNode(td);
273 for(int i=0;i<args.size();i++) {
274 con.addArgument((ExpressionNode)args.get(i));
277 } else if (isNode(pn,"createarray")) {
278 System.out.println(pn.PPrint(3,true));
279 TypeDescriptor td=parseTypeDescriptor(pn);
280 Vector args=parseDimExprs(pn);
282 if (pn.getChild("dims_opt").getLiteral()!=null)
283 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
284 for(int i=0;i<(args.size()+num);i++)
285 td=td.makeArray(state);
286 CreateObjectNode con=new CreateObjectNode(td);
287 for(int i=0;i<args.size();i++) {
288 con.addArgument((ExpressionNode)args.get(i));
291 } else if (isNode(pn,"name")) {
292 NameDescriptor nd=parseName(pn);
293 return new NameNode(nd);
294 } else if (isNode(pn,"this")) {
295 NameDescriptor nd=new NameDescriptor("this");
296 return new NameNode(nd);
297 } else if (isNode(pn,"methodinvoke1")) {
298 NameDescriptor nd=parseName(pn.getChild("name"));
299 Vector args=parseArgumentList(pn);
300 MethodInvokeNode min=new MethodInvokeNode(nd);
301 for(int i=0;i<args.size();i++) {
302 min.addArgument((ExpressionNode)args.get(i));
305 } else if (isNode(pn,"methodinvoke2")) {
306 String methodid=pn.getChild("id").getTerminal();
307 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
308 Vector args=parseArgumentList(pn);
309 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
310 for(int i=0;i<args.size();i++) {
311 min.addArgument((ExpressionNode)args.get(i));
314 } else if (isNode(pn,"fieldaccess")) {
315 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
316 return new FieldAccessNode(en,fieldname);
317 } else if (isNode(pn,"arrayaccess")) {
318 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
319 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
320 return new ArrayAccessNode(en,index);
321 } else if (isNode(pn,"cast1")) {
322 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
323 } else if (isNode(pn,"cast2")) {
324 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
326 System.out.println("---------------------");
327 System.out.println(pn.PPrint(3,true));
332 private Vector parseDimExprs(ParseNode pn) {
333 Vector arglist=new Vector();
334 ParseNode an=pn.getChild("dim_exprs");
335 if (an==null) /* No argument list */
337 ParseNodeVector anv=an.getChildren();
338 for(int i=0;i<anv.size();i++) {
339 arglist.add(parseExpression(anv.elementAt(i)));
344 private Vector parseArgumentList(ParseNode pn) {
345 Vector arglist=new Vector();
346 ParseNode an=pn.getChild("argument_list");
347 if (an==null) /* No argument list */
349 ParseNodeVector anv=an.getChildren();
350 for(int i=0;i<anv.size();i++) {
351 arglist.add(parseExpression(anv.elementAt(i)));
356 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
357 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
358 ParseNodeVector pnv=pn.getChild("args").getChildren();
360 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
365 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
366 ParseNode headern=pn.getChild("method_header");
367 ParseNode bodyn=pn.getChild("body");
368 MethodDescriptor md=parseMethodHeader(headern);
369 BlockNode bn=parseBlock(bodyn);
371 state.addTreeCode(md,bn);
374 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
375 ParseNode mn=pn.getChild("modifiers");
376 Modifiers m=parseModifiersList(mn);
377 ParseNode cdecl=pn.getChild("constructor_declarator");
378 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
379 MethodDescriptor md=new MethodDescriptor(m, name);
380 ParseNode paramnode=cdecl.getChild("parameters");
381 parseParameterList(md,paramnode);
382 ParseNode bodyn0=pn.getChild("body");
383 ParseNode bodyn=bodyn0.getChild("constructor_body");
386 BlockNode bn=parseBlock(bodyn);
387 state.addTreeCode(md,bn);
391 public BlockNode parseBlock(ParseNode pn) {
392 if (isEmpty(pn.getTerminal()))
393 return new BlockNode();
394 ParseNode bsn=pn.getChild("block_statement_list");
395 return parseBlockHelper(bsn);
398 private BlockNode parseBlockHelper(ParseNode pn) {
399 ParseNodeVector pnv=pn.getChildren();
400 BlockNode bn=new BlockNode();
401 for(int i=0;i<pnv.size();i++) {
402 Vector bsv=parseBlockStatement(pnv.elementAt(i));
403 for(int j=0;j<bsv.size();j++) {
404 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
410 public BlockNode parseSingleBlock(ParseNode pn) {
411 BlockNode bn=new BlockNode();
412 Vector bsv=parseBlockStatement(pn);
413 for(int j=0;j<bsv.size();j++) {
414 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
416 bn.setStyle(BlockNode.NOBRACES);
420 public Vector parseBlockStatement(ParseNode pn) {
421 Vector blockstatements=new Vector();
422 if (isNode(pn,"local_variable_declaration")) {
423 TypeDescriptor t=parseTypeDescriptor(pn);
424 ParseNode vn=pn.getChild("variable_declarators_list");
425 ParseNodeVector pnv=vn.getChildren();
426 for(int i=0;i<pnv.size();i++) {
427 ParseNode vardecl=pnv.elementAt(i);
430 ParseNode tmp=vardecl;
431 TypeDescriptor arrayt=t;
432 while (tmp.getChild("single")==null) {
433 arrayt=arrayt.makeArray(state);
434 tmp=tmp.getChild("array");
436 String identifier=tmp.getChild("single").getTerminal();
438 ParseNode epn=vardecl.getChild("initializer");
441 ExpressionNode en=null;
443 en=parseExpression(epn.getFirstChild());
445 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
447 } else if (isNode(pn,"nop")) {
449 } else if (isNode(pn,"expression")) {
450 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
451 } else if (isNode(pn,"ifstatement")) {
452 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
453 parseSingleBlock(pn.getChild("statement").getFirstChild()),
454 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
455 } else if (isNode(pn,"taskexit")) {
457 if (pn.getChild("flag_effects_list")!=null)
458 fe=parseFlagEffects(pn.getChild("flag_effects_list"));
459 blockstatements.add(new TaskExitNode(fe));
460 } else if (isNode(pn,"return")) {
461 if (isEmpty(pn.getTerminal()))
462 blockstatements.add(new ReturnNode());
464 ExpressionNode en=parseExpression(pn.getFirstChild());
465 blockstatements.add(new ReturnNode(en));
467 } else if (isNode(pn,"block_statement_list")) {
468 BlockNode bn=parseBlockHelper(pn);
469 blockstatements.add(new SubBlockNode(bn));
470 } else if (isNode(pn,"empty")) {
472 } else if (isNode(pn,"statement_expression_list")) {
473 ParseNodeVector pnv=pn.getChildren();
474 BlockNode bn=new BlockNode();
475 for(int i=0;i<pnv.size();i++) {
476 ExpressionNode en=parseExpression(pnv.elementAt(i));
477 blockstatements.add(new BlockExpressionNode(en));
479 bn.setStyle(BlockNode.EXPRLIST);
480 } else if (isNode(pn,"forstatement")) {
481 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
482 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
483 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
484 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
485 blockstatements.add(new LoopNode(init,condition,update,body));
486 } else if (isNode(pn,"whilestatement")) {
487 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
488 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
489 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
490 } else if (isNode(pn,"dowhilestatement")) {
491 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
492 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
493 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
495 System.out.println("---------------");
496 System.out.println(pn.PPrint(3,true));
499 return blockstatements;
502 public MethodDescriptor parseMethodHeader(ParseNode pn) {
503 ParseNode mn=pn.getChild("modifiers");
504 Modifiers m=parseModifiersList(mn);
506 ParseNode tn=pn.getChild("returntype");
507 TypeDescriptor returntype;
509 returntype=parseTypeDescriptor(tn);
511 returntype=new TypeDescriptor(TypeDescriptor.VOID);
513 ParseNode pmd=pn.getChild("method_declarator");
514 String name=pmd.getChild("name").getTerminal();
515 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
517 ParseNode paramnode=pmd.getChild("parameters");
518 parseParameterList(md,paramnode);
522 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
523 ParseNode paramlist=pn.getChild("formal_parameter_list");
526 ParseNodeVector pnv=paramlist.getChildren();
527 for(int i=0;i<pnv.size();i++) {
528 ParseNode paramn=pnv.elementAt(i);
529 TypeDescriptor type=parseTypeDescriptor(paramn);
531 ParseNode tmp=paramn;
532 while (tmp.getChild("single")==null) {
533 type=type.makeArray(state);
534 tmp=tmp.getChild("array");
536 String paramname=tmp.getChild("single").getTerminal();
538 md.addParameter(type,paramname);
542 public Modifiers parseModifiersList(ParseNode pn) {
543 Modifiers m=new Modifiers();
544 ParseNode modlist=pn.getChild("modifier_list");
546 ParseNodeVector pnv=modlist.getChildren();
547 for(int i=0;i<pnv.size();i++) {
548 ParseNode modn=pnv.elementAt(i);
549 if (isNode(modn,"public"))
550 m.addModifier(Modifiers.PUBLIC);
551 if (isNode(modn,"protected"))
552 m.addModifier(Modifiers.PROTECTED);
553 if (isNode(modn,"private"))
554 m.addModifier(Modifiers.PRIVATE);
555 if (isNode(modn,"static"))
556 m.addModifier(Modifiers.STATIC);
557 if (isNode(modn,"final"))
558 m.addModifier(Modifiers.FINAL);
559 if (isNode(modn,"native"))
560 m.addModifier(Modifiers.NATIVE);
566 private boolean isNode(ParseNode pn, String label) {
567 if (pn.getLabel().equals(label))
572 private static boolean isEmpty(ParseNode pn) {
573 if (pn.getLabel().equals("empty"))
579 private static boolean isEmpty(String s) {
580 if (s.equals("empty"))
586 /** Throw an exception if something is unexpected */
587 private void check(ParseNode pn, String label) {
589 throw new Error(pn+ "IE: Expected '" + label + "', got null");
591 if (! pn.getLabel().equals(label)) {
592 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");