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 ClassDescriptor cn=parseTypeDecl(type_pn);
33 public ClassDescriptor parseTypeDecl(ParseNode pn) {
34 if (isNode(pn, "class_declaration")) {
35 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
36 if (!isEmpty(pn.getChild("super").getTerminal())) {
37 /* parse superclass name */
38 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
39 NameDescriptor nd=parseName(snn);
40 cn.setSuper(nd.toString());
42 if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
43 cn.setSuper(TypeUtil.ObjectClass);
45 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
46 parseClassBody(cn, pn.getChild("classbody"));
48 } else throw new Error();
51 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
52 ParseNode decls=pn.getChild("class_body_declaration_list");
54 ParseNodeVector pnv=decls.getChildren();
55 for(int i=0;i<pnv.size();i++) {
56 ParseNode decl=pnv.elementAt(i);
57 if (isNode(decl,"member")) {
58 parseClassMember(cn,decl);
59 } else if (isNode(decl,"constructor")) {
60 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
61 } else if (isNode(decl,"block")) {
62 } else throw new Error();
67 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
68 ParseNode fieldnode=pn.getChild("field");
70 if (fieldnode!=null) {
71 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
74 ParseNode methodnode=pn.getChild("method");
75 if (methodnode!=null) {
76 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
82 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
83 ParseNode tn=pn.getChild("type");
84 String type_st=tn.getTerminal();
85 if(type_st.equals("byte")) {
86 return state.getTypeDescriptor(TypeDescriptor.BYTE);
87 } else if(type_st.equals("short")) {
88 return state.getTypeDescriptor(TypeDescriptor.SHORT);
89 } else if(type_st.equals("boolean")) {
90 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
91 } else if(type_st.equals("int")) {
92 return state.getTypeDescriptor(TypeDescriptor.INT);
93 } else if(type_st.equals("long")) {
94 return state.getTypeDescriptor(TypeDescriptor.LONG);
95 } else if(type_st.equals("char")) {
96 return state.getTypeDescriptor(TypeDescriptor.CHAR);
97 } else if(type_st.equals("float")) {
98 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
99 } else if(type_st.equals("double")) {
100 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
101 } else if(type_st.equals("class")) {
102 ParseNode nn=tn.getChild("class");
103 return state.getTypeDescriptor(parseName(nn.getChild("name")));
104 } else if(type_st.equals("array")) {
105 ParseNode nn=tn.getChild("array");
106 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
107 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
108 for(int i=0;i<numdims.intValue();i++)
109 td=td.makeArray(state);
116 private NameDescriptor parseName(ParseNode nn) {
117 ParseNode base=nn.getChild("base");
118 ParseNode id=nn.getChild("identifier");
120 return new NameDescriptor(id.getTerminal());
121 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
125 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
126 ParseNode mn=pn.getChild("modifier");
127 Modifiers m=parseModifiersList(mn);
129 ParseNode tn=pn.getChild("type");
130 TypeDescriptor t=parseTypeDescriptor(tn);
131 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
132 ParseNodeVector pnv=vn.getChildren();
133 for(int i=0;i<pnv.size();i++) {
134 ParseNode vardecl=pnv.elementAt(i);
137 ParseNode tmp=vardecl;
138 TypeDescriptor arrayt=t;
139 while (tmp.getChild("single")==null) {
140 arrayt=arrayt.makeArray(state);
141 tmp=tmp.getChild("array");
143 String identifier=tmp.getChild("single").getTerminal();
145 ParseNode epn=vardecl.getChild("initializer");
147 ExpressionNode en=null;
149 en=parseExpression(epn.getFirstChild());
151 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
156 private ExpressionNode parseExpression(ParseNode pn) {
157 if (isNode(pn,"assignment"))
158 return parseAssignmentExpression(pn);
159 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
160 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
161 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
162 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
163 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
164 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
165 isNode(pn,"rightshift")||isNode(pn,"sub")||
166 isNode(pn,"add")||isNode(pn,"mult")||
167 isNode(pn,"div")||isNode(pn,"mod")) {
168 ParseNodeVector pnv=pn.getChildren();
169 ParseNode left=pnv.elementAt(0);
170 ParseNode right=pnv.elementAt(1);
171 Operation op=new Operation(pn.getLabel());
172 return new OpNode(parseExpression(left),parseExpression(right),op);
173 } else if (isNode(pn,"unaryplus")||
174 isNode(pn,"unaryminus")||
175 isNode(pn,"postinc")||
176 isNode(pn,"postdec")||
177 isNode(pn,"preinc")||
178 isNode(pn,"predec")) {
179 ParseNode left=pn.getFirstChild();
180 Operation op=new Operation(pn.getLabel());
181 return new OpNode(parseExpression(left),op);
182 } else if (isNode(pn,"literal")) {
183 String literaltype=pn.getTerminal();
184 ParseNode literalnode=pn.getChild(literaltype);
185 Object literal_obj=literalnode.getLiteral();
186 return new LiteralNode(literaltype, literal_obj);
187 } else if (isNode(pn,"createobject")) {
188 TypeDescriptor td=parseTypeDescriptor(pn);
189 Vector args=parseArgumentList(pn);
190 CreateObjectNode con=new CreateObjectNode(td);
191 for(int i=0;i<args.size();i++) {
192 con.addArgument((ExpressionNode)args.get(i));
195 } else if (isNode(pn,"createarray")) {
196 TypeDescriptor td=parseTypeDescriptor(pn);
197 Vector args=parseDimExprs(pn);
199 if (pn.getChild("dims_opt").getLiteral()!=null)
200 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
201 for(int i=0;i<args.size()+num;i++)
202 td=td.makeArray(state);
203 CreateObjectNode con=new CreateObjectNode(td);
204 for(int i=0;i<args.size();i++) {
205 con.addArgument((ExpressionNode)args.get(i));
208 } else if (isNode(pn,"name")) {
209 NameDescriptor nd=parseName(pn);
210 return new NameNode(nd);
211 } else if (isNode(pn,"this")) {
212 NameDescriptor nd=new NameDescriptor("this");
213 return new NameNode(nd);
214 } else if (isNode(pn,"methodinvoke1")) {
215 NameDescriptor nd=parseName(pn.getChild("name"));
216 Vector args=parseArgumentList(pn);
217 MethodInvokeNode min=new MethodInvokeNode(nd);
218 for(int i=0;i<args.size();i++) {
219 min.addArgument((ExpressionNode)args.get(i));
222 } else if (isNode(pn,"methodinvoke2")) {
223 String methodid=pn.getChild("id").getTerminal();
224 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
225 Vector args=parseArgumentList(pn);
226 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
227 for(int i=0;i<args.size();i++) {
228 min.addArgument((ExpressionNode)args.get(i));
231 } else if (isNode(pn,"fieldaccess")) {
232 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
233 return new FieldAccessNode(en,fieldname);
234 } else if (isNode(pn,"arrayaccess")) {
235 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
236 ExpressionNode index=parseExpression(pn.getChild("index"));
237 return new ArrayAccessNode(en,index);
238 } else if (isNode(pn,"cast1")) {
239 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
240 } else if (isNode(pn,"cast2")) {
241 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
243 System.out.println("---------------------");
244 System.out.println(pn.PPrint(3,true));
249 private Vector parseDimExprs(ParseNode pn) {
250 Vector arglist=new Vector();
251 ParseNode an=pn.getChild("dim_exprs");
252 if (an==null) /* No argument list */
254 ParseNodeVector anv=an.getChildren();
255 for(int i=0;i<anv.size();i++) {
256 arglist.add(parseExpression(anv.elementAt(i)));
261 private Vector parseArgumentList(ParseNode pn) {
262 Vector arglist=new Vector();
263 ParseNode an=pn.getChild("argument_list");
264 if (an==null) /* No argument list */
266 ParseNodeVector anv=an.getChildren();
267 for(int i=0;i<anv.size();i++) {
268 arglist.add(parseExpression(anv.elementAt(i)));
273 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
274 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
275 ParseNodeVector pnv=pn.getChild("args").getChildren();
277 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
282 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
283 ParseNode headern=pn.getChild("method_header");
284 ParseNode bodyn=pn.getChild("body");
285 MethodDescriptor md=parseMethodHeader(headern);
286 BlockNode bn=parseBlock(bodyn);
288 state.addTreeCode(md,bn);
291 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
292 ParseNode mn=pn.getChild("modifiers");
293 Modifiers m=parseModifiersList(mn);
294 ParseNode cdecl=pn.getChild("constructor_declarator");
295 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
296 MethodDescriptor md=new MethodDescriptor(m, name);
297 ParseNode paramnode=cdecl.getChild("parameters");
298 parseParameterList(md,paramnode);
299 ParseNode bodyn0=pn.getChild("body");
300 ParseNode bodyn=bodyn0.getChild("constructor_body");
303 BlockNode bn=parseBlock(bodyn);
304 state.addTreeCode(md,bn);
308 public BlockNode parseBlock(ParseNode pn) {
309 if (isEmpty(pn.getTerminal()))
310 return new BlockNode();
311 ParseNode bsn=pn.getChild("block_statement_list");
312 return parseBlockHelper(bsn);
315 private BlockNode parseBlockHelper(ParseNode pn) {
316 ParseNodeVector pnv=pn.getChildren();
317 BlockNode bn=new BlockNode();
318 for(int i=0;i<pnv.size();i++) {
319 Vector bsv=parseBlockStatement(pnv.elementAt(i));
320 for(int j=0;j<bsv.size();j++) {
321 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
327 public BlockNode parseSingleBlock(ParseNode pn) {
328 BlockNode bn=new BlockNode();
329 Vector bsv=parseBlockStatement(pn);
330 for(int j=0;j<bsv.size();j++) {
331 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
333 bn.setStyle(BlockNode.NOBRACES);
337 public Vector parseBlockStatement(ParseNode pn) {
338 Vector blockstatements=new Vector();
339 if (isNode(pn,"local_variable_declaration")) {
340 TypeDescriptor t=parseTypeDescriptor(pn);
341 ParseNode vn=pn.getChild("variable_declarators_list");
342 ParseNodeVector pnv=vn.getChildren();
343 for(int i=0;i<pnv.size();i++) {
344 ParseNode vardecl=pnv.elementAt(i);
347 ParseNode tmp=vardecl;
348 TypeDescriptor arrayt=t;
349 while (tmp.getChild("single")==null) {
350 arrayt=arrayt.makeArray(state);
351 tmp=tmp.getChild("array");
353 String identifier=tmp.getChild("single").getTerminal();
355 ParseNode epn=vardecl.getChild("initializer");
358 ExpressionNode en=null;
360 en=parseExpression(epn.getFirstChild());
362 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
364 } else if (isNode(pn,"nop")) {
366 } else if (isNode(pn,"expression")) {
367 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
368 } else if (isNode(pn,"ifstatement")) {
369 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
370 parseSingleBlock(pn.getChild("statement").getFirstChild()),
371 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
372 } else if (isNode(pn,"return")) {
373 if (isEmpty(pn.getTerminal()))
374 blockstatements.add(new ReturnNode());
376 ExpressionNode en=parseExpression(pn.getFirstChild());
377 blockstatements.add(new ReturnNode(en));
379 } else if (isNode(pn,"block_statement_list")) {
380 BlockNode bn=parseBlockHelper(pn);
381 blockstatements.add(new SubBlockNode(bn));
382 } else if (isNode(pn,"empty")) {
384 } else if (isNode(pn,"statement_expression_list")) {
385 ParseNodeVector pnv=pn.getChildren();
386 BlockNode bn=new BlockNode();
387 for(int i=0;i<pnv.size();i++) {
388 ExpressionNode en=parseExpression(pnv.elementAt(i));
389 blockstatements.add(new BlockExpressionNode(en));
391 bn.setStyle(BlockNode.EXPRLIST);
392 } else if (isNode(pn,"forstatement")) {
393 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
394 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
395 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
396 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
397 blockstatements.add(new LoopNode(init,condition,update,body));
398 } else if (isNode(pn,"whilestatement")) {
399 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
400 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
401 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
402 } else if (isNode(pn,"dowhilestatement")) {
403 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
404 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
405 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
407 System.out.println("---------------");
408 System.out.println(pn.PPrint(3,true));
411 return blockstatements;
414 public MethodDescriptor parseMethodHeader(ParseNode pn) {
415 ParseNode mn=pn.getChild("modifiers");
416 Modifiers m=parseModifiersList(mn);
418 ParseNode tn=pn.getChild("returntype");
419 TypeDescriptor returntype;
421 returntype=parseTypeDescriptor(tn);
423 returntype=new TypeDescriptor(TypeDescriptor.VOID);
425 ParseNode pmd=pn.getChild("method_declarator");
426 String name=pmd.getChild("name").getTerminal();
427 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
429 ParseNode paramnode=pmd.getChild("parameters");
430 parseParameterList(md,paramnode);
434 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
435 ParseNode paramlist=pn.getChild("formal_parameter_list");
438 ParseNodeVector pnv=paramlist.getChildren();
439 for(int i=0;i<pnv.size();i++) {
440 ParseNode paramn=pnv.elementAt(i);
441 TypeDescriptor type=parseTypeDescriptor(paramn);
443 ParseNode tmp=paramn;
444 while (tmp.getChild("single")==null) {
445 type=type.makeArray(state);
446 tmp=tmp.getChild("array");
448 String paramname=tmp.getChild("single").getTerminal();
450 md.addParameter(type,paramname);
454 public Modifiers parseModifiersList(ParseNode pn) {
455 Modifiers m=new Modifiers();
456 ParseNode modlist=pn.getChild("modifier_list");
458 ParseNodeVector pnv=modlist.getChildren();
459 for(int i=0;i<pnv.size();i++) {
460 ParseNode modn=pnv.elementAt(i);
461 if (isNode(modn,"public"))
462 m.addModifier(Modifiers.PUBLIC);
463 if (isNode(modn,"protected"))
464 m.addModifier(Modifiers.PROTECTED);
465 if (isNode(modn,"private"))
466 m.addModifier(Modifiers.PRIVATE);
467 if (isNode(modn,"static"))
468 m.addModifier(Modifiers.STATIC);
469 if (isNode(modn,"final"))
470 m.addModifier(Modifiers.FINAL);
471 if (isNode(modn,"native"))
472 m.addModifier(Modifiers.NATIVE);
478 private boolean isNode(ParseNode pn, String label) {
479 if (pn.getLabel().equals(label))
484 private static boolean isEmpty(ParseNode pn) {
485 if (pn.getLabel().equals("empty"))
491 private static boolean isEmpty(String s) {
492 if (s.equals("empty"))
498 /** Throw an exception if something is unexpected */
499 private void check(ParseNode pn, String label) {
501 throw new Error(pn+ "IE: Expected '" + label + "', got null");
503 if (! pn.getLabel().equals(label)) {
504 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");