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(parseFlags(pn.getChild("flag_effects_list")));
49 public Vector parseFlags(ParseNode pn) {
50 Vector vfe=new Vector();
51 ParseNodeVector pnv=pn.getChildren();
52 for(int i=0;i<pnv.size();i++) {
53 ParseNode fn=pnv.elementAt(i);
54 FlagEffects fe=parseFlagEffects(fn);
60 public FlagEffects parseFlagEffects(ParseNode pn) {
61 if (isNode(pn,"flag_effect")) {
62 String flagname=pn.getChild("name").getTerminal();
63 FlagEffects fe=new FlagEffects(flagname);
68 } else throw new Error();
71 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
72 if (pn.getChild("or")!=null) {
73 ParseNodeVector pnv=pn.getChild("or").getChildren();
74 ParseNode left=pnv.elementAt(0);
75 ParseNode right=pnv.elementAt(1);
76 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
77 } else if (pn.getChild("and")!=null) {
78 ParseNodeVector pnv=pn.getChild("and").getChildren();
79 ParseNode left=pnv.elementAt(0);
80 ParseNode right=pnv.elementAt(1);
81 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
82 } else if (pn.getChild("not")!=null) {
83 ParseNodeVector pnv=pn.getChild("not").getChildren();
84 ParseNode left=pnv.elementAt(0);
85 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
87 } else if (pn.getChild("name")!=null) {
88 return new FlagNode(pn.getChild("name").getTerminal());
89 } else throw new Error();
92 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
93 ParseNode paramlist=pn.getChild("task_parameter_list");
96 ParseNodeVector pnv=paramlist.getChildren();
97 for(int i=0;i<pnv.size();i++) {
98 ParseNode paramn=pnv.elementAt(i);
99 TypeDescriptor type=parseTypeDescriptor(paramn);
101 ParseNode tmp=paramn;
102 while (tmp.getChild("single")==null) {
103 type=type.makeArray(state);
104 tmp=tmp.getChild("array");
106 String paramname=tmp.getChild("single").getTerminal();
107 FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag"));
109 td.addParameter(type,paramname,fen);
113 public ClassDescriptor parseTypeDecl(ParseNode pn) {
114 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
115 if (!isEmpty(pn.getChild("super").getTerminal())) {
116 /* parse superclass name */
117 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
118 NameDescriptor nd=parseName(snn);
119 cn.setSuper(nd.toString());
121 if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
122 cn.setSuper(TypeUtil.ObjectClass);
124 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
125 parseClassBody(cn, pn.getChild("classbody"));
129 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
130 ParseNode decls=pn.getChild("class_body_declaration_list");
132 ParseNodeVector pnv=decls.getChildren();
133 for(int i=0;i<pnv.size();i++) {
134 ParseNode decl=pnv.elementAt(i);
135 if (isNode(decl,"member")) {
136 parseClassMember(cn,decl);
137 } else if (isNode(decl,"constructor")) {
138 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
139 } else if (isNode(decl,"block")) {
140 } else throw new Error();
145 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
146 ParseNode fieldnode=pn.getChild("field");
148 if (fieldnode!=null) {
149 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
152 ParseNode methodnode=pn.getChild("method");
153 if (methodnode!=null) {
154 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
160 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
161 ParseNode tn=pn.getChild("type");
162 String type_st=tn.getTerminal();
163 if(type_st.equals("byte")) {
164 return state.getTypeDescriptor(TypeDescriptor.BYTE);
165 } else if(type_st.equals("short")) {
166 return state.getTypeDescriptor(TypeDescriptor.SHORT);
167 } else if(type_st.equals("boolean")) {
168 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
169 } else if(type_st.equals("int")) {
170 return state.getTypeDescriptor(TypeDescriptor.INT);
171 } else if(type_st.equals("long")) {
172 return state.getTypeDescriptor(TypeDescriptor.LONG);
173 } else if(type_st.equals("char")) {
174 return state.getTypeDescriptor(TypeDescriptor.CHAR);
175 } else if(type_st.equals("float")) {
176 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
177 } else if(type_st.equals("double")) {
178 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
179 } else if(type_st.equals("class")) {
180 ParseNode nn=tn.getChild("class");
181 return state.getTypeDescriptor(parseName(nn.getChild("name")));
182 } else if(type_st.equals("array")) {
183 ParseNode nn=tn.getChild("array");
184 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
185 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
186 for(int i=0;i<numdims.intValue();i++)
187 td=td.makeArray(state);
194 private NameDescriptor parseName(ParseNode nn) {
195 ParseNode base=nn.getChild("base");
196 ParseNode id=nn.getChild("identifier");
198 return new NameDescriptor(id.getTerminal());
199 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
203 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
204 ParseNode mn=pn.getChild("modifier");
205 Modifiers m=parseModifiersList(mn);
207 ParseNode tn=pn.getChild("type");
208 TypeDescriptor t=parseTypeDescriptor(tn);
209 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
210 ParseNodeVector pnv=vn.getChildren();
211 for(int i=0;i<pnv.size();i++) {
212 ParseNode vardecl=pnv.elementAt(i);
215 ParseNode tmp=vardecl;
216 TypeDescriptor arrayt=t;
217 while (tmp.getChild("single")==null) {
218 arrayt=arrayt.makeArray(state);
219 tmp=tmp.getChild("array");
221 String identifier=tmp.getChild("single").getTerminal();
223 ParseNode epn=vardecl.getChild("initializer");
225 ExpressionNode en=null;
227 en=parseExpression(epn.getFirstChild());
229 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
234 private ExpressionNode parseExpression(ParseNode pn) {
235 if (isNode(pn,"assignment"))
236 return parseAssignmentExpression(pn);
237 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
238 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
239 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
240 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
241 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
242 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
243 isNode(pn,"rightshift")||isNode(pn,"sub")||
244 isNode(pn,"add")||isNode(pn,"mult")||
245 isNode(pn,"div")||isNode(pn,"mod")) {
246 ParseNodeVector pnv=pn.getChildren();
247 ParseNode left=pnv.elementAt(0);
248 ParseNode right=pnv.elementAt(1);
249 Operation op=new Operation(pn.getLabel());
250 return new OpNode(parseExpression(left),parseExpression(right),op);
251 } else if (isNode(pn,"unaryplus")||
252 isNode(pn,"unaryminus")||
253 isNode(pn,"postinc")||
254 isNode(pn,"postdec")||
255 isNode(pn,"preinc")||
257 isNode(pn,"predec")) {
258 ParseNode left=pn.getFirstChild();
259 Operation op=new Operation(pn.getLabel());
260 return new OpNode(parseExpression(left),op);
261 } else if (isNode(pn,"literal")) {
262 String literaltype=pn.getTerminal();
263 ParseNode literalnode=pn.getChild(literaltype);
264 Object literal_obj=literalnode.getLiteral();
265 return new LiteralNode(literaltype, literal_obj);
266 } else if (isNode(pn,"createobject")) {
267 TypeDescriptor td=parseTypeDescriptor(pn);
268 Vector args=parseArgumentList(pn);
269 CreateObjectNode con=new CreateObjectNode(td);
270 for(int i=0;i<args.size();i++) {
271 con.addArgument((ExpressionNode)args.get(i));
274 } else if (isNode(pn,"createarray")) {
275 System.out.println(pn.PPrint(3,true));
276 TypeDescriptor td=parseTypeDescriptor(pn);
277 Vector args=parseDimExprs(pn);
279 if (pn.getChild("dims_opt").getLiteral()!=null)
280 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
281 for(int i=0;i<(args.size()+num);i++)
282 td=td.makeArray(state);
283 CreateObjectNode con=new CreateObjectNode(td);
284 for(int i=0;i<args.size();i++) {
285 con.addArgument((ExpressionNode)args.get(i));
288 } else if (isNode(pn,"name")) {
289 NameDescriptor nd=parseName(pn);
290 return new NameNode(nd);
291 } else if (isNode(pn,"this")) {
292 NameDescriptor nd=new NameDescriptor("this");
293 return new NameNode(nd);
294 } else if (isNode(pn,"methodinvoke1")) {
295 NameDescriptor nd=parseName(pn.getChild("name"));
296 Vector args=parseArgumentList(pn);
297 MethodInvokeNode min=new MethodInvokeNode(nd);
298 for(int i=0;i<args.size();i++) {
299 min.addArgument((ExpressionNode)args.get(i));
302 } else if (isNode(pn,"methodinvoke2")) {
303 String methodid=pn.getChild("id").getTerminal();
304 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
305 Vector args=parseArgumentList(pn);
306 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
307 for(int i=0;i<args.size();i++) {
308 min.addArgument((ExpressionNode)args.get(i));
311 } else if (isNode(pn,"fieldaccess")) {
312 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
313 return new FieldAccessNode(en,fieldname);
314 } else if (isNode(pn,"arrayaccess")) {
315 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
316 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
317 return new ArrayAccessNode(en,index);
318 } else if (isNode(pn,"cast1")) {
319 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
320 } else if (isNode(pn,"cast2")) {
321 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
323 System.out.println("---------------------");
324 System.out.println(pn.PPrint(3,true));
329 private Vector parseDimExprs(ParseNode pn) {
330 Vector arglist=new Vector();
331 ParseNode an=pn.getChild("dim_exprs");
332 if (an==null) /* No argument list */
334 ParseNodeVector anv=an.getChildren();
335 for(int i=0;i<anv.size();i++) {
336 arglist.add(parseExpression(anv.elementAt(i)));
341 private Vector parseArgumentList(ParseNode pn) {
342 Vector arglist=new Vector();
343 ParseNode an=pn.getChild("argument_list");
344 if (an==null) /* No argument list */
346 ParseNodeVector anv=an.getChildren();
347 for(int i=0;i<anv.size();i++) {
348 arglist.add(parseExpression(anv.elementAt(i)));
353 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
354 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
355 ParseNodeVector pnv=pn.getChild("args").getChildren();
357 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
362 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
363 ParseNode headern=pn.getChild("method_header");
364 ParseNode bodyn=pn.getChild("body");
365 MethodDescriptor md=parseMethodHeader(headern);
366 BlockNode bn=parseBlock(bodyn);
368 state.addTreeCode(md,bn);
371 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
372 ParseNode mn=pn.getChild("modifiers");
373 Modifiers m=parseModifiersList(mn);
374 ParseNode cdecl=pn.getChild("constructor_declarator");
375 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
376 MethodDescriptor md=new MethodDescriptor(m, name);
377 ParseNode paramnode=cdecl.getChild("parameters");
378 parseParameterList(md,paramnode);
379 ParseNode bodyn0=pn.getChild("body");
380 ParseNode bodyn=bodyn0.getChild("constructor_body");
383 BlockNode bn=parseBlock(bodyn);
384 state.addTreeCode(md,bn);
388 public BlockNode parseBlock(ParseNode pn) {
389 if (isEmpty(pn.getTerminal()))
390 return new BlockNode();
391 ParseNode bsn=pn.getChild("block_statement_list");
392 return parseBlockHelper(bsn);
395 private BlockNode parseBlockHelper(ParseNode pn) {
396 ParseNodeVector pnv=pn.getChildren();
397 BlockNode bn=new BlockNode();
398 for(int i=0;i<pnv.size();i++) {
399 Vector bsv=parseBlockStatement(pnv.elementAt(i));
400 for(int j=0;j<bsv.size();j++) {
401 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
407 public BlockNode parseSingleBlock(ParseNode pn) {
408 BlockNode bn=new BlockNode();
409 Vector bsv=parseBlockStatement(pn);
410 for(int j=0;j<bsv.size();j++) {
411 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
413 bn.setStyle(BlockNode.NOBRACES);
417 public Vector parseBlockStatement(ParseNode pn) {
418 Vector blockstatements=new Vector();
419 if (isNode(pn,"local_variable_declaration")) {
420 TypeDescriptor t=parseTypeDescriptor(pn);
421 ParseNode vn=pn.getChild("variable_declarators_list");
422 ParseNodeVector pnv=vn.getChildren();
423 for(int i=0;i<pnv.size();i++) {
424 ParseNode vardecl=pnv.elementAt(i);
427 ParseNode tmp=vardecl;
428 TypeDescriptor arrayt=t;
429 while (tmp.getChild("single")==null) {
430 arrayt=arrayt.makeArray(state);
431 tmp=tmp.getChild("array");
433 String identifier=tmp.getChild("single").getTerminal();
435 ParseNode epn=vardecl.getChild("initializer");
438 ExpressionNode en=null;
440 en=parseExpression(epn.getFirstChild());
442 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
444 } else if (isNode(pn,"nop")) {
446 } else if (isNode(pn,"expression")) {
447 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
448 } else if (isNode(pn,"ifstatement")) {
449 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
450 parseSingleBlock(pn.getChild("statement").getFirstChild()),
451 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
452 } else if (isNode(pn,"taskexit")) {
454 if (pn.getChild("flag_effects_list")!=null)
455 vfe=parseFlags(pn.getChild("flag_effects_list"));
456 blockstatements.add(new TaskExitNode(vfe));
457 } else if (isNode(pn,"return")) {
458 if (isEmpty(pn.getTerminal()))
459 blockstatements.add(new ReturnNode());
461 ExpressionNode en=parseExpression(pn.getFirstChild());
462 blockstatements.add(new ReturnNode(en));
464 } else if (isNode(pn,"block_statement_list")) {
465 BlockNode bn=parseBlockHelper(pn);
466 blockstatements.add(new SubBlockNode(bn));
467 } else if (isNode(pn,"empty")) {
469 } else if (isNode(pn,"statement_expression_list")) {
470 ParseNodeVector pnv=pn.getChildren();
471 BlockNode bn=new BlockNode();
472 for(int i=0;i<pnv.size();i++) {
473 ExpressionNode en=parseExpression(pnv.elementAt(i));
474 blockstatements.add(new BlockExpressionNode(en));
476 bn.setStyle(BlockNode.EXPRLIST);
477 } else if (isNode(pn,"forstatement")) {
478 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
479 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
480 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
481 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
482 blockstatements.add(new LoopNode(init,condition,update,body));
483 } else if (isNode(pn,"whilestatement")) {
484 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
485 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
486 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
487 } else if (isNode(pn,"dowhilestatement")) {
488 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
489 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
490 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
492 System.out.println("---------------");
493 System.out.println(pn.PPrint(3,true));
496 return blockstatements;
499 public MethodDescriptor parseMethodHeader(ParseNode pn) {
500 ParseNode mn=pn.getChild("modifiers");
501 Modifiers m=parseModifiersList(mn);
503 ParseNode tn=pn.getChild("returntype");
504 TypeDescriptor returntype;
506 returntype=parseTypeDescriptor(tn);
508 returntype=new TypeDescriptor(TypeDescriptor.VOID);
510 ParseNode pmd=pn.getChild("method_declarator");
511 String name=pmd.getChild("name").getTerminal();
512 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
514 ParseNode paramnode=pmd.getChild("parameters");
515 parseParameterList(md,paramnode);
519 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
520 ParseNode paramlist=pn.getChild("formal_parameter_list");
523 ParseNodeVector pnv=paramlist.getChildren();
524 for(int i=0;i<pnv.size();i++) {
525 ParseNode paramn=pnv.elementAt(i);
526 TypeDescriptor type=parseTypeDescriptor(paramn);
528 ParseNode tmp=paramn;
529 while (tmp.getChild("single")==null) {
530 type=type.makeArray(state);
531 tmp=tmp.getChild("array");
533 String paramname=tmp.getChild("single").getTerminal();
535 md.addParameter(type,paramname);
539 public Modifiers parseModifiersList(ParseNode pn) {
540 Modifiers m=new Modifiers();
541 ParseNode modlist=pn.getChild("modifier_list");
543 ParseNodeVector pnv=modlist.getChildren();
544 for(int i=0;i<pnv.size();i++) {
545 ParseNode modn=pnv.elementAt(i);
546 if (isNode(modn,"public"))
547 m.addModifier(Modifiers.PUBLIC);
548 if (isNode(modn,"protected"))
549 m.addModifier(Modifiers.PROTECTED);
550 if (isNode(modn,"private"))
551 m.addModifier(Modifiers.PRIVATE);
552 if (isNode(modn,"static"))
553 m.addModifier(Modifiers.STATIC);
554 if (isNode(modn,"final"))
555 m.addModifier(Modifiers.FINAL);
556 if (isNode(modn,"native"))
557 m.addModifier(Modifiers.NATIVE);
563 private boolean isNode(ParseNode pn, String label) {
564 if (pn.getLabel().equals(label))
569 private static boolean isEmpty(ParseNode pn) {
570 if (pn.getLabel().equals("empty"))
576 private static boolean isEmpty(String s) {
577 if (s.equals("empty"))
583 /** Throw an exception if something is unexpected */
584 private void check(ParseNode pn, String label) {
586 throw new Error(pn+ "IE: Expected '" + label + "', got null");
588 if (! pn.getLabel().equals(label)) {
589 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");