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);
70 } else throw new Error();
73 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
74 if (pn.getChild("or")!=null) {
75 ParseNodeVector pnv=pn.getChild("or").getChildren();
76 ParseNode left=pnv.elementAt(0);
77 ParseNode right=pnv.elementAt(1);
78 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
79 } else if (pn.getChild("and")!=null) {
80 ParseNodeVector pnv=pn.getChild("and").getChildren();
81 ParseNode left=pnv.elementAt(0);
82 ParseNode right=pnv.elementAt(1);
83 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
84 } else if (pn.getChild("not")!=null) {
85 ParseNodeVector pnv=pn.getChild("not").getChildren();
86 ParseNode left=pnv.elementAt(0);
87 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
89 } else if (pn.getChild("name")!=null) {
90 return new FlagNode(pn.getChild("name").getTerminal());
91 } else throw new Error();
94 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
95 ParseNode paramlist=pn.getChild("task_parameter_list");
98 ParseNodeVector pnv=paramlist.getChildren();
99 for(int i=0;i<pnv.size();i++) {
100 ParseNode paramn=pnv.elementAt(i);
101 TypeDescriptor type=parseTypeDescriptor(paramn);
103 ParseNode tmp=paramn;
104 while (tmp.getChild("single")==null) {
105 type=type.makeArray(state);
106 tmp=tmp.getChild("array");
108 String paramname=tmp.getChild("single").getTerminal();
109 FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag"));
111 td.addParameter(type,paramname,fen);
115 public ClassDescriptor parseTypeDecl(ParseNode pn) {
116 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
117 if (!isEmpty(pn.getChild("super").getTerminal())) {
118 /* parse superclass name */
119 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
120 NameDescriptor nd=parseName(snn);
121 cn.setSuper(nd.toString());
123 if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
124 cn.setSuper(TypeUtil.ObjectClass);
126 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
127 parseClassBody(cn, pn.getChild("classbody"));
131 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
132 ParseNode decls=pn.getChild("class_body_declaration_list");
134 ParseNodeVector pnv=decls.getChildren();
135 for(int i=0;i<pnv.size();i++) {
136 ParseNode decl=pnv.elementAt(i);
137 if (isNode(decl,"member")) {
138 parseClassMember(cn,decl);
139 } else if (isNode(decl,"constructor")) {
140 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
141 } else if (isNode(decl,"block")) {
142 } else throw new Error();
147 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
148 ParseNode fieldnode=pn.getChild("field");
150 if (fieldnode!=null) {
151 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
154 ParseNode methodnode=pn.getChild("method");
155 if (methodnode!=null) {
156 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
162 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
163 ParseNode tn=pn.getChild("type");
164 String type_st=tn.getTerminal();
165 if(type_st.equals("byte")) {
166 return state.getTypeDescriptor(TypeDescriptor.BYTE);
167 } else if(type_st.equals("short")) {
168 return state.getTypeDescriptor(TypeDescriptor.SHORT);
169 } else if(type_st.equals("boolean")) {
170 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
171 } else if(type_st.equals("int")) {
172 return state.getTypeDescriptor(TypeDescriptor.INT);
173 } else if(type_st.equals("long")) {
174 return state.getTypeDescriptor(TypeDescriptor.LONG);
175 } else if(type_st.equals("char")) {
176 return state.getTypeDescriptor(TypeDescriptor.CHAR);
177 } else if(type_st.equals("float")) {
178 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
179 } else if(type_st.equals("double")) {
180 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
181 } else if(type_st.equals("class")) {
182 ParseNode nn=tn.getChild("class");
183 return state.getTypeDescriptor(parseName(nn.getChild("name")));
184 } else if(type_st.equals("array")) {
185 ParseNode nn=tn.getChild("array");
186 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
187 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
188 for(int i=0;i<numdims.intValue();i++)
189 td=td.makeArray(state);
196 private NameDescriptor parseName(ParseNode nn) {
197 ParseNode base=nn.getChild("base");
198 ParseNode id=nn.getChild("identifier");
200 return new NameDescriptor(id.getTerminal());
201 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
205 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
206 ParseNode mn=pn.getChild("modifier");
207 Modifiers m=parseModifiersList(mn);
209 ParseNode tn=pn.getChild("type");
210 TypeDescriptor t=parseTypeDescriptor(tn);
211 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
212 ParseNodeVector pnv=vn.getChildren();
213 for(int i=0;i<pnv.size();i++) {
214 ParseNode vardecl=pnv.elementAt(i);
217 ParseNode tmp=vardecl;
218 TypeDescriptor arrayt=t;
219 while (tmp.getChild("single")==null) {
220 arrayt=arrayt.makeArray(state);
221 tmp=tmp.getChild("array");
223 String identifier=tmp.getChild("single").getTerminal();
225 ParseNode epn=vardecl.getChild("initializer");
227 ExpressionNode en=null;
229 en=parseExpression(epn.getFirstChild());
231 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
236 private ExpressionNode parseExpression(ParseNode pn) {
237 if (isNode(pn,"assignment"))
238 return parseAssignmentExpression(pn);
239 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
240 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
241 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
242 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
243 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
244 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
245 isNode(pn,"rightshift")||isNode(pn,"sub")||
246 isNode(pn,"add")||isNode(pn,"mult")||
247 isNode(pn,"div")||isNode(pn,"mod")) {
248 ParseNodeVector pnv=pn.getChildren();
249 ParseNode left=pnv.elementAt(0);
250 ParseNode right=pnv.elementAt(1);
251 Operation op=new Operation(pn.getLabel());
252 return new OpNode(parseExpression(left),parseExpression(right),op);
253 } else if (isNode(pn,"unaryplus")||
254 isNode(pn,"unaryminus")||
255 isNode(pn,"postinc")||
256 isNode(pn,"postdec")||
257 isNode(pn,"preinc")||
259 isNode(pn,"predec")) {
260 ParseNode left=pn.getFirstChild();
261 Operation op=new Operation(pn.getLabel());
262 return new OpNode(parseExpression(left),op);
263 } else if (isNode(pn,"literal")) {
264 String literaltype=pn.getTerminal();
265 ParseNode literalnode=pn.getChild(literaltype);
266 Object literal_obj=literalnode.getLiteral();
267 return new LiteralNode(literaltype, literal_obj);
268 } else if (isNode(pn,"createobject")) {
269 TypeDescriptor td=parseTypeDescriptor(pn);
270 Vector args=parseArgumentList(pn);
271 CreateObjectNode con=new CreateObjectNode(td);
272 for(int i=0;i<args.size();i++) {
273 con.addArgument((ExpressionNode)args.get(i));
276 } else if (isNode(pn,"createarray")) {
277 System.out.println(pn.PPrint(3,true));
278 TypeDescriptor td=parseTypeDescriptor(pn);
279 Vector args=parseDimExprs(pn);
281 if (pn.getChild("dims_opt").getLiteral()!=null)
282 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
283 for(int i=0;i<(args.size()+num);i++)
284 td=td.makeArray(state);
285 CreateObjectNode con=new CreateObjectNode(td);
286 for(int i=0;i<args.size();i++) {
287 con.addArgument((ExpressionNode)args.get(i));
290 } else if (isNode(pn,"name")) {
291 NameDescriptor nd=parseName(pn);
292 return new NameNode(nd);
293 } else if (isNode(pn,"this")) {
294 NameDescriptor nd=new NameDescriptor("this");
295 return new NameNode(nd);
296 } else if (isNode(pn,"methodinvoke1")) {
297 NameDescriptor nd=parseName(pn.getChild("name"));
298 Vector args=parseArgumentList(pn);
299 MethodInvokeNode min=new MethodInvokeNode(nd);
300 for(int i=0;i<args.size();i++) {
301 min.addArgument((ExpressionNode)args.get(i));
304 } else if (isNode(pn,"methodinvoke2")) {
305 String methodid=pn.getChild("id").getTerminal();
306 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
307 Vector args=parseArgumentList(pn);
308 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
309 for(int i=0;i<args.size();i++) {
310 min.addArgument((ExpressionNode)args.get(i));
313 } else if (isNode(pn,"fieldaccess")) {
314 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
315 return new FieldAccessNode(en,fieldname);
316 } else if (isNode(pn,"arrayaccess")) {
317 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
318 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
319 return new ArrayAccessNode(en,index);
320 } else if (isNode(pn,"cast1")) {
321 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
322 } else if (isNode(pn,"cast2")) {
323 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
325 System.out.println("---------------------");
326 System.out.println(pn.PPrint(3,true));
331 private Vector parseDimExprs(ParseNode pn) {
332 Vector arglist=new Vector();
333 ParseNode an=pn.getChild("dim_exprs");
334 if (an==null) /* No argument list */
336 ParseNodeVector anv=an.getChildren();
337 for(int i=0;i<anv.size();i++) {
338 arglist.add(parseExpression(anv.elementAt(i)));
343 private Vector parseArgumentList(ParseNode pn) {
344 Vector arglist=new Vector();
345 ParseNode an=pn.getChild("argument_list");
346 if (an==null) /* No argument list */
348 ParseNodeVector anv=an.getChildren();
349 for(int i=0;i<anv.size();i++) {
350 arglist.add(parseExpression(anv.elementAt(i)));
355 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
356 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
357 ParseNodeVector pnv=pn.getChild("args").getChildren();
359 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
364 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
365 ParseNode headern=pn.getChild("method_header");
366 ParseNode bodyn=pn.getChild("body");
367 MethodDescriptor md=parseMethodHeader(headern);
368 BlockNode bn=parseBlock(bodyn);
370 state.addTreeCode(md,bn);
373 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
374 ParseNode mn=pn.getChild("modifiers");
375 Modifiers m=parseModifiersList(mn);
376 ParseNode cdecl=pn.getChild("constructor_declarator");
377 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
378 MethodDescriptor md=new MethodDescriptor(m, name);
379 ParseNode paramnode=cdecl.getChild("parameters");
380 parseParameterList(md,paramnode);
381 ParseNode bodyn0=pn.getChild("body");
382 ParseNode bodyn=bodyn0.getChild("constructor_body");
385 BlockNode bn=parseBlock(bodyn);
386 state.addTreeCode(md,bn);
390 public BlockNode parseBlock(ParseNode pn) {
391 if (isEmpty(pn.getTerminal()))
392 return new BlockNode();
393 ParseNode bsn=pn.getChild("block_statement_list");
394 return parseBlockHelper(bsn);
397 private BlockNode parseBlockHelper(ParseNode pn) {
398 ParseNodeVector pnv=pn.getChildren();
399 BlockNode bn=new BlockNode();
400 for(int i=0;i<pnv.size();i++) {
401 Vector bsv=parseBlockStatement(pnv.elementAt(i));
402 for(int j=0;j<bsv.size();j++) {
403 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
409 public BlockNode parseSingleBlock(ParseNode pn) {
410 BlockNode bn=new BlockNode();
411 Vector bsv=parseBlockStatement(pn);
412 for(int j=0;j<bsv.size();j++) {
413 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
415 bn.setStyle(BlockNode.NOBRACES);
419 public Vector parseBlockStatement(ParseNode pn) {
420 Vector blockstatements=new Vector();
421 if (isNode(pn,"local_variable_declaration")) {
422 TypeDescriptor t=parseTypeDescriptor(pn);
423 ParseNode vn=pn.getChild("variable_declarators_list");
424 ParseNodeVector pnv=vn.getChildren();
425 for(int i=0;i<pnv.size();i++) {
426 ParseNode vardecl=pnv.elementAt(i);
429 ParseNode tmp=vardecl;
430 TypeDescriptor arrayt=t;
431 while (tmp.getChild("single")==null) {
432 arrayt=arrayt.makeArray(state);
433 tmp=tmp.getChild("array");
435 String identifier=tmp.getChild("single").getTerminal();
437 ParseNode epn=vardecl.getChild("initializer");
440 ExpressionNode en=null;
442 en=parseExpression(epn.getFirstChild());
444 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
446 } else if (isNode(pn,"nop")) {
448 } else if (isNode(pn,"expression")) {
449 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
450 } else if (isNode(pn,"ifstatement")) {
451 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
452 parseSingleBlock(pn.getChild("statement").getFirstChild()),
453 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
454 } else if (isNode(pn,"taskexit")) {
456 if (pn.getChild("flag_effects_list")!=null)
457 vfe=parseFlags(pn.getChild("flag_effects_list"));
458 blockstatements.add(new TaskExitNode(vfe));
459 } else if (isNode(pn,"return")) {
460 if (isEmpty(pn.getTerminal()))
461 blockstatements.add(new ReturnNode());
463 ExpressionNode en=parseExpression(pn.getFirstChild());
464 blockstatements.add(new ReturnNode(en));
466 } else if (isNode(pn,"block_statement_list")) {
467 BlockNode bn=parseBlockHelper(pn);
468 blockstatements.add(new SubBlockNode(bn));
469 } else if (isNode(pn,"empty")) {
471 } else if (isNode(pn,"statement_expression_list")) {
472 ParseNodeVector pnv=pn.getChildren();
473 BlockNode bn=new BlockNode();
474 for(int i=0;i<pnv.size();i++) {
475 ExpressionNode en=parseExpression(pnv.elementAt(i));
476 blockstatements.add(new BlockExpressionNode(en));
478 bn.setStyle(BlockNode.EXPRLIST);
479 } else if (isNode(pn,"forstatement")) {
480 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
481 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
482 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
483 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
484 blockstatements.add(new LoopNode(init,condition,update,body));
485 } else if (isNode(pn,"whilestatement")) {
486 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
487 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
488 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
489 } else if (isNode(pn,"dowhilestatement")) {
490 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
491 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
492 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
494 System.out.println("---------------");
495 System.out.println(pn.PPrint(3,true));
498 return blockstatements;
501 public MethodDescriptor parseMethodHeader(ParseNode pn) {
502 ParseNode mn=pn.getChild("modifiers");
503 Modifiers m=parseModifiersList(mn);
505 ParseNode tn=pn.getChild("returntype");
506 TypeDescriptor returntype;
508 returntype=parseTypeDescriptor(tn);
510 returntype=new TypeDescriptor(TypeDescriptor.VOID);
512 ParseNode pmd=pn.getChild("method_declarator");
513 String name=pmd.getChild("name").getTerminal();
514 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
516 ParseNode paramnode=pmd.getChild("parameters");
517 parseParameterList(md,paramnode);
521 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
522 ParseNode paramlist=pn.getChild("formal_parameter_list");
525 ParseNodeVector pnv=paramlist.getChildren();
526 for(int i=0;i<pnv.size();i++) {
527 ParseNode paramn=pnv.elementAt(i);
528 TypeDescriptor type=parseTypeDescriptor(paramn);
530 ParseNode tmp=paramn;
531 while (tmp.getChild("single")==null) {
532 type=type.makeArray(state);
533 tmp=tmp.getChild("array");
535 String paramname=tmp.getChild("single").getTerminal();
537 md.addParameter(type,paramname);
541 public Modifiers parseModifiersList(ParseNode pn) {
542 Modifiers m=new Modifiers();
543 ParseNode modlist=pn.getChild("modifier_list");
545 ParseNodeVector pnv=modlist.getChildren();
546 for(int i=0;i<pnv.size();i++) {
547 ParseNode modn=pnv.elementAt(i);
548 if (isNode(modn,"public"))
549 m.addModifier(Modifiers.PUBLIC);
550 if (isNode(modn,"protected"))
551 m.addModifier(Modifiers.PROTECTED);
552 if (isNode(modn,"private"))
553 m.addModifier(Modifiers.PRIVATE);
554 if (isNode(modn,"static"))
555 m.addModifier(Modifiers.STATIC);
556 if (isNode(modn,"final"))
557 m.addModifier(Modifiers.FINAL);
558 if (isNode(modn,"native"))
559 m.addModifier(Modifiers.NATIVE);
565 private boolean isNode(ParseNode pn, String label) {
566 if (pn.getLabel().equals(label))
571 private static boolean isEmpty(ParseNode pn) {
572 if (pn.getLabel().equals("empty"))
578 private static boolean isEmpty(String s) {
579 if (s.equals("empty"))
585 /** Throw an exception if something is unexpected */
586 private void check(ParseNode pn, String label) {
588 throw new Error(pn+ "IE: Expected '" + label + "', got null");
590 if (! pn.getLabel().equals(label)) {
591 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");