3 import java.util.Vector;
7 public BuildIR(State state) {
10 public void buildtree() {
11 ParseNode pn=state.parsetree;
15 /** Parse the classes in this file */
16 public void parseFile(ParseNode pn) {
17 ParseNode tpn=pn.getChild("type_declaration_list");
19 ParseNodeVector pnv=tpn.getChildren();
20 for(int i=0;i<pnv.size();i++) {
21 ParseNode type_pn=pnv.elementAt(i);
22 if (isEmpty(type_pn)) /* Skip the semicolon */
24 ClassNode cn=parseTypeDecl(type_pn);
30 public ClassNode parseTypeDecl(ParseNode pn) {
31 if (isNode(pn, "class_declaration")) {
32 ClassNode cn=new ClassNode();
33 cn.setName(pn.getChild("name").getTerminal());
34 if (!isEmpty(pn.getChild("super").getTerminal())) {
35 /* parse superclass name */
37 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
38 parseClassBody(cn, pn.getChild("classbody"));
40 } else throw new Error();
43 private void parseClassBody(ClassNode cn, ParseNode pn) {
44 ParseNode decls=pn.getChild("class_body_declaration_list");
46 ParseNodeVector pnv=decls.getChildren();
47 for(int i=0;i<pnv.size();i++) {
48 ParseNode decl=pnv.elementAt(i);
49 if (isNode(decl,"member")) {
50 parseClassMember(cn,decl);
51 } else if (isNode(decl,"constructor")) {
52 } else if (isNode(decl,"block")) {
53 } else throw new Error();
58 private void parseClassMember(ClassNode cn, ParseNode pn) {
59 ParseNode fieldnode=pn.getChild("field");
61 if (fieldnode!=null) {
62 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
65 ParseNode methodnode=pn.getChild("method");
66 if (methodnode!=null) {
67 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
73 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
74 ParseNode tn=pn.getChild("type");
75 String type_st=tn.getTerminal();
76 if(type_st.equals("byte")) {
77 return state.getTypeDescriptor(TypeDescriptor.BYTE);
78 } else if(type_st.equals("short")) {
79 return state.getTypeDescriptor(TypeDescriptor.SHORT);
80 } else if(type_st.equals("boolean")) {
81 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
82 } else if(type_st.equals("int")) {
83 return state.getTypeDescriptor(TypeDescriptor.INT);
84 } else if(type_st.equals("long")) {
85 return state.getTypeDescriptor(TypeDescriptor.LONG);
86 } else if(type_st.equals("char")) {
87 return state.getTypeDescriptor(TypeDescriptor.CHAR);
88 } else if(type_st.equals("float")) {
89 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
90 } else if(type_st.equals("double")) {
91 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
92 } else if(type_st.equals("class")) {
93 ParseNode nn=tn.getChild("class");
94 return state.getTypeDescriptor(parseName(nn.getChild("name")));
100 private NameDescriptor parseName(ParseNode nn) {
101 ParseNode base=nn.getChild("base");
102 ParseNode id=nn.getChild("identifier");
104 return new NameDescriptor(id.getTerminal());
105 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
109 private void parseFieldDecl(ClassNode cn,ParseNode pn) {
110 ParseNode mn=pn.getChild("modifier");
111 Modifiers m=parseModifiersList(mn);
113 ParseNode tn=pn.getChild("type");
114 TypeDescriptor t=parseTypeDescriptor(tn);
115 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
116 ParseNodeVector pnv=vn.getChildren();
117 for(int i=0;i<pnv.size();i++) {
118 ParseNode vardecl=pnv.elementAt(i);
119 String identifier=vardecl.getChild("single").getTerminal();
120 ParseNode epn=vardecl.getChild("initializer");
122 ExpressionNode en=null;
124 en=parseExpression(epn.getFirstChild());
126 cn.addField(new FieldDescriptor(m,t,identifier, en));
131 private ExpressionNode parseExpression(ParseNode pn) {
132 if (isNode(pn,"assignment"))
133 return parseAssignmentExpression(pn);
134 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
135 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
136 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
137 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
138 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
139 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
140 isNode(pn,"rightshift")||isNode(pn,"sub")||
141 isNode(pn,"add")||isNode(pn,"mult")||
142 isNode(pn,"div")||isNode(pn,"mod")) {
143 ParseNodeVector pnv=pn.getChildren();
144 ParseNode left=pnv.elementAt(0);
145 ParseNode right=pnv.elementAt(1);
146 Operation op=new Operation(pn.getLabel());
147 return new OpNode(parseExpression(left),parseExpression(right),op);
148 } else if (isNode(pn,"unaryplus")||
149 isNode(pn,"unaryminus")||
150 isNode(pn,"postinc")||
151 isNode(pn,"postdec")||
152 isNode(pn,"preinc")||
153 isNode(pn,"predec")) {
154 ParseNode left=pn.getFirstChild();
155 Operation op=new Operation(pn.getLabel());
156 return new OpNode(parseExpression(left),op);
157 } else if (isNode(pn,"literal")) {
158 String literaltype=pn.getTerminal();
159 ParseNode literalnode=pn.getChild(literaltype);
160 Object literal_obj=literalnode.getLiteral();
161 return new LiteralNode(literaltype, literal_obj);
162 } else if (isNode(pn,"createobject")) {
163 TypeDescriptor td=parseTypeDescriptor(pn);
164 Vector args=parseArgumentList(pn);
165 CreateObjectNode con=new CreateObjectNode(td);
166 for(int i=0;i<args.size();i++) {
167 con.addArgument((ExpressionNode)args.get(i));
170 } else if (isNode(pn,"name")) {
171 NameDescriptor nd=parseName(pn);
172 return new NameNode(nd);
173 } else if (isNode(pn,"this")) {
174 NameDescriptor nd=new NameDescriptor("this");
175 return new NameNode(nd);
176 } else if (isNode(pn,"methodinvoke1")) {
177 NameDescriptor nd=parseName(pn.getChild("name"));
178 Vector args=parseArgumentList(pn);
179 MethodInvokeNode min=new MethodInvokeNode(nd);
180 for(int i=0;i<args.size();i++) {
181 min.addArgument((ExpressionNode)args.get(i));
184 } else if (isNode(pn,"methodinvoke2")) {
185 String methodid=pn.getChild("id").getTerminal();
186 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
187 Vector args=parseArgumentList(pn);
188 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
189 for(int i=0;i<args.size();i++) {
190 min.addArgument((ExpressionNode)args.get(i));
193 } else if (isNode(pn,"fieldaccess")) {
194 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
195 String fieldname=pn.getChild("field").getTerminal();
196 return new FieldAccessNode(en,fieldname);
197 } else if (isNode(pn,"cast1")) {
198 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
199 } else if (isNode(pn,"cast2")) {
200 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
202 System.out.println("---------------------");
203 System.out.println(pn.PPrint(3,true));
208 private Vector parseArgumentList(ParseNode pn) {
209 Vector arglist=new Vector();
210 ParseNode an=pn.getChild("argument_list");
211 if (an==null) /* No argument list */
213 ParseNodeVector anv=an.getChildren();
214 for(int i=0;i<anv.size();i++) {
215 arglist.add(parseExpression(anv.elementAt(i)));
220 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
221 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
222 ParseNodeVector pnv=pn.getChild("args").getChildren();
224 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
229 private void parseMethodDecl(ClassNode cn, ParseNode pn) {
230 ParseNode headern=pn.getChild("method_header");
231 ParseNode bodyn=pn.getChild("body");
232 MethodDescriptor md=parseMethodHeader(headern);
233 BlockNode bn=parseBlock(bodyn);
237 public BlockNode parseBlock(ParseNode pn) {
238 if (isEmpty(pn.getTerminal()))
239 return new BlockNode();
240 ParseNode bsn=pn.getChild("block_statement_list");
241 return parseBlockHelper(bsn);
244 private BlockNode parseBlockHelper(ParseNode pn) {
245 ParseNodeVector pnv=pn.getChildren();
246 BlockNode bn=new BlockNode();
247 for(int i=0;i<pnv.size();i++) {
248 Vector bsv=parseBlockStatement(pnv.elementAt(i));
249 for(int j=0;j<bsv.size();j++) {
250 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
256 public BlockNode parseSingleBlock(ParseNode pn) {
257 BlockNode bn=new BlockNode();
258 Vector bsv=parseBlockStatement(pn);
259 for(int j=0;j<bsv.size();j++) {
260 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
262 bn.setStyle(BlockNode.NOBRACES);
266 public Vector parseBlockStatement(ParseNode pn) {
267 Vector blockstatements=new Vector();
268 if (isNode(pn,"local_variable_declaration")) {
269 TypeDescriptor t=parseTypeDescriptor(pn);
270 ParseNode vn=pn.getChild("variable_declarators_list");
271 ParseNodeVector pnv=vn.getChildren();
272 for(int i=0;i<pnv.size();i++) {
273 ParseNode vardecl=pnv.elementAt(i);
274 String identifier=vardecl.getChild("single").getTerminal();
275 ParseNode epn=vardecl.getChild("initializer");
277 ExpressionNode en=null;
279 en=parseExpression(epn.getFirstChild());
281 blockstatements.add(new DeclarationNode(new VarDescriptor(t,identifier, en)));
283 } else if (isNode(pn,"nop")) {
285 } else if (isNode(pn,"expression")) {
286 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
287 } else if (isNode(pn,"ifstatement")) {
288 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
289 parseSingleBlock(pn.getChild("statement").getFirstChild()),
290 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
291 } else if (isNode(pn,"return")) {
292 if (isEmpty(pn.getTerminal()))
293 blockstatements.add(new ReturnNode());
295 ExpressionNode en=parseExpression(pn.getFirstChild());
296 blockstatements.add(new ReturnNode(en));
298 } else if (isNode(pn,"block_statement_list")) {
299 BlockNode bn=parseBlockHelper(pn);
300 blockstatements.add(new SubBlockNode(bn));
301 } else if (isNode(pn,"empty")) {
303 } else if (isNode(pn,"statement_expression_list")) {
304 ParseNodeVector pnv=pn.getChildren();
305 BlockNode bn=new BlockNode();
306 for(int i=0;i<pnv.size();i++) {
307 ExpressionNode en=parseExpression(pnv.elementAt(i));
308 blockstatements.add(new BlockExpressionNode(en));
310 bn.setStyle(BlockNode.EXPRLIST);
311 } else if (isNode(pn,"forstatement")) {
312 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
313 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
314 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
315 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
316 blockstatements.add(new LoopNode(init,condition,update,body));
318 System.out.println("---------------");
319 System.out.println(pn.PPrint(3,true));
322 return blockstatements;
325 public MethodDescriptor parseMethodHeader(ParseNode pn) {
326 ParseNode mn=pn.getChild("modifiers");
327 Modifiers m=parseModifiersList(mn);
329 ParseNode tn=pn.getChild("returntype");
330 TypeDescriptor returntype;
332 returntype=parseTypeDescriptor(tn);
334 returntype=new TypeDescriptor(TypeDescriptor.VOID);
336 ParseNode pmd=pn.getChild("method_declarator");
337 String name=pmd.getChild("name").getTerminal();
338 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
340 ParseNode paramnode=pmd.getChild("parameters");
341 parseParameterList(md,paramnode);
345 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
346 ParseNode paramlist=pn.getChild("formal_parameter_list");
349 ParseNodeVector pnv=paramlist.getChildren();
350 for(int i=0;i<pnv.size();i++) {
351 ParseNode paramn=pnv.elementAt(i);
352 TypeDescriptor type=parseTypeDescriptor(paramn);
353 String paramname=paramn.getChild("single").getTerminal();
354 md.addParameter(type,paramname);
358 public Modifiers parseModifiersList(ParseNode pn) {
359 Modifiers m=new Modifiers();
360 ParseNode modlist=pn.getChild("modifier_list");
362 ParseNodeVector pnv=modlist.getChildren();
363 for(int i=0;i<pnv.size();i++) {
364 ParseNode modn=pnv.elementAt(i);
365 if (isNode(modn,"public"))
366 m.addModifier(Modifiers.PUBLIC);
367 if (isNode(modn,"protected"))
368 m.addModifier(Modifiers.PROTECTED);
369 if (isNode(modn,"private"))
370 m.addModifier(Modifiers.PRIVATE);
371 if (isNode(modn,"static"))
372 m.addModifier(Modifiers.STATIC);
373 if (isNode(modn,"final"))
374 m.addModifier(Modifiers.FINAL);
375 if (isNode(modn,"native"))
376 m.addModifier(Modifiers.NATIVE);
382 private boolean isNode(ParseNode pn, String label) {
383 if (pn.getLabel().equals(label))
388 private static boolean isEmpty(ParseNode pn) {
389 if (pn.getLabel().equals("empty"))
395 private static boolean isEmpty(String s) {
396 if (s.equals("empty"))
402 /** Throw an exception if something is unexpected */
403 private void check(ParseNode pn, String label) {
405 throw new Error(pn+ "IE: Expected '" + label + "', got null");
407 if (! pn.getLabel().equals(label)) {
408 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");