6 public class BuildFlat {
10 public BuildFlat(State st) {
12 temptovar=new Hashtable();
15 public Hashtable getMap() {
19 public void buildFlat() {
20 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
22 ClassDescriptor cn=(ClassDescriptor)it.next();
26 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
27 while(task_it.hasNext()) {
28 TaskDescriptor td=(TaskDescriptor)task_it.next();
33 private void flattenTask(TaskDescriptor td) {
34 BlockNode bn=state.getMethodBody(td);
35 FlatNode fn=flattenBlockNode(bn).getBegin();
36 FlatFlagActionNode ffan=new FlatFlagActionNode(false);
38 FlatMethod fm=new FlatMethod(td, ffan);
40 for(int i=0;i<td.numParameters();i++) {
41 fm.addParameterTemp(getTempforVar(td.getParameter(i)));
44 /* Flatten Vector of Flag Effects */
45 Vector flags=td.getFlagEffects();
46 updateFlagActionNode(ffan,flags);
48 state.addFlatCode(td,fm);
52 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
53 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
54 for(int i=0;i<flags.size();i++) {
55 FlagEffects fes=(FlagEffects)flags.get(i);
56 TempDescriptor flagtemp=getTempforVar(fes.getVar());
57 for(int j=0;j<fes.numEffects();j++) {
58 FlagEffect fe=fes.getEffect(j);
59 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
64 private void flattenClass(ClassDescriptor cn) {
65 Iterator methodit=cn.getMethods();
66 while(methodit.hasNext()) {
67 MethodDescriptor md=(MethodDescriptor)methodit.next();
68 BlockNode bn=state.getMethodBody(md);
69 FlatNode fn=flattenBlockNode(bn).getBegin();
70 FlatMethod fm=new FlatMethod(md, fn);
72 fm.addParameterTemp(getTempforVar(md.getThis()));
73 for(int i=0;i<md.numParameters();i++) {
74 fm.addParameterTemp(getTempforVar(md.getParameter(i)));
76 System.out.println(fm.printMethod());
77 state.addFlatCode(md,fm);
81 private NodePair flattenBlockNode(BlockNode bn) {
84 for(int i=0;i<bn.size();i++) {
85 NodePair np=flattenBlockStatementNode(bn.get(i));
86 FlatNode np_begin=np.getBegin();
87 FlatNode np_end=np.getEnd();
94 end.addNext(np_begin);
99 end=begin=new FlatNop();
101 return new NodePair(begin,end);
104 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
105 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
106 return flattenExpressionNode(en.getExpression(),tmp);
109 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
110 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
111 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
112 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
113 np.getEnd().addNext(fcn);
114 return new NodePair(np.getBegin(),fcn);
117 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
118 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
119 return new NodePair(fln,fln);
122 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
123 TypeDescriptor td=con.getType();
125 FlatNew fn=new FlatNew(td, out_temp);
126 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
129 if (con.getFlagEffects()!=null) {
130 FlatFlagActionNode ffan=new FlatFlagActionNode(false);
131 FlagEffects fes=con.getFlagEffects();
132 TempDescriptor flagtemp=out_temp;
133 for(int j=0;j<fes.numEffects();j++) {
134 FlagEffect fe=fes.getEffect(j);
135 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
141 for(int i=0;i<con.numArgs();i++) {
142 ExpressionNode en=con.getArg(i);
143 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
145 NodePair np=flattenExpressionNode(en, tmp);
146 last.addNext(np.getBegin());
149 MethodDescriptor md=con.getConstructor();
150 //Call to constructor
151 FlatCall fc=new FlatCall(md, null, out_temp, temps);
154 return new NodePair(fn,last);
158 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
159 for (int i=0;i<con.numArgs();i++) {
160 ExpressionNode en=con.getArg(i);
161 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
163 NodePair np=flattenExpressionNode(en, tmp);
167 last.addNext(np.getBegin());
170 TempDescriptor tmp2=(i==0)?
172 TempDescriptor.tempFactory("arg",en.getType());
174 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
176 if (temps.length>1) {
177 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
178 fn.addNext(np.getBegin());
179 return new NodePair(first,np.getEnd());
181 return new NodePair(first, fn);
185 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
186 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
187 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
188 FlatNop fnop=new FlatNop();//last node
191 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
193 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
195 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
197 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
198 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
200 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
201 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
202 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
204 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
210 fcb.addFalseNext(fnop);
212 //Recursive call here
213 if ((i+2)<temparray.length) {
214 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
215 fsen.addNext(np2.getBegin());
216 np2.getEnd().addNext(fon);
221 return new NodePair(fln, fnop);
224 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
225 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
228 TempDescriptor thisarg=null;
230 if (min.getExpression()!=null) {
231 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
232 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
238 for(int i=0;i<min.numArgs();i++) {
239 ExpressionNode en=min.getArg(i);
240 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
242 NodePair np=flattenExpressionNode(en, td);
246 last.addNext(np.getBegin());
250 MethodDescriptor md=min.getMethod();
252 //Call to constructor
255 if(md.getReturnType()==null||md.getReturnType().isVoid())
256 fc=new FlatCall(md, null, thisarg, temps);
258 fc=new FlatCall(md, out_temp, thisarg, temps);
263 return new NodePair(first,fc);
266 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
267 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
268 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
269 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
270 npe.getEnd().addNext(fn);
271 return new NodePair(npe.getBegin(),fn);
274 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
275 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
276 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
277 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
278 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
279 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
280 npe.getEnd().addNext(npi.getBegin());
281 npi.getEnd().addNext(fn);
282 return new NodePair(npe.getBegin(),fn);
285 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
287 // left side is variable
288 // left side is field
290 Operation base=an.getOperation().getBaseOp();
291 TempDescriptor src_tmp=TempDescriptor.tempFactory("src",an.getSrc().getType());
292 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
293 FlatNode last=np_src.getEnd();
295 TempDescriptor src_tmp2=TempDescriptor.tempFactory("tmp", an.getDest().getType());
296 NodePair np_dst_init=flattenExpressionNode(an.getDest(),src_tmp2);
297 last.addNext(np_dst_init.getBegin());
298 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst_tmp",an.getDest().getType());
299 FlatOpNode fon=new FlatOpNode(dst_tmp, src_tmp,src_tmp2, base);
300 np_dst_init.getEnd().addNext(fon);
305 if (an.getDest().kind()==Kind.FieldAccessNode) {
306 FieldAccessNode fan=(FieldAccessNode)an.getDest();
307 ExpressionNode en=fan.getExpression();
308 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
309 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
310 last.addNext(np_baseexp.getBegin());
311 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
312 np_baseexp.getEnd().addNext(fsfn);
313 return new NodePair(np_src.getBegin(), fsfn);
314 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
315 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
316 ExpressionNode en=aan.getExpression();
317 ExpressionNode enindex=aan.getIndex();
318 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
319 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
320 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
321 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
322 last.addNext(np_baseexp.getBegin());
323 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
324 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
325 np_indexexp.getEnd().addNext(fsen);
326 return new NodePair(np_src.getBegin(), fsen);
327 } else if (an.getDest().kind()==Kind.NameNode) {
328 NameNode nn=(NameNode)an.getDest();
329 if (nn.getExpression()!=null) {
330 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
331 ExpressionNode en=fan.getExpression();
332 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
333 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
334 last.addNext(np_baseexp.getBegin());
335 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
336 np_baseexp.getEnd().addNext(fsfn);
337 return new NodePair(np_src.getBegin(), fsfn);
339 if (nn.getField()!=null) {
340 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
342 return new NodePair(np_src.getBegin(), fsfn);
344 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
346 return new NodePair(np_src.getBegin(),fon);
353 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
354 if (nn.getExpression()!=null) {
355 /* Hack - use subtree instead */
356 return flattenExpressionNode(nn.getExpression(),out_temp);
357 } else if (nn.getField()!=null) {
358 TempDescriptor tmp=getTempforVar(nn.getVar());
359 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
360 return new NodePair(ffn,ffn);
362 TempDescriptor tmp=getTempforVar(nn.getVar());
363 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
364 return new NodePair(fon,fon);
368 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
369 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
370 TempDescriptor temp_right=null;
372 Operation op=on.getOp();
373 if (op.getOp()==Operation.POSTINC||
374 op.getOp()==Operation.POSTDEC||
375 op.getOp()==Operation.PREINC||
376 op.getOp()==Operation.PREDEC) {
377 LiteralNode ln=new LiteralNode("int",new Integer(1));
378 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
379 AssignmentNode an=new AssignmentNode(on.getLeft(),ln,
380 new AssignOperation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?AssignOperation.PLUSEQ:AssignOperation.MINUSEQ));
381 if (op.getOp()==Operation.POSTINC||
382 op.getOp()==Operation.POSTDEC) {
383 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
384 NodePair assign=flattenAssignmentNode(an,temp_left);
385 left.getEnd().addNext(assign.getBegin());
386 return new NodePair(left.getBegin(),assign.getEnd());
388 NodePair assign=flattenAssignmentNode(an,out_temp);
393 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
395 if (on.getRight()!=null) {
396 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
397 right=flattenExpressionNode(on.getRight(),temp_right);
399 FlatNop nop=new FlatNop();
400 right=new NodePair(nop,nop);
403 if (op.getOp()==Operation.LOGIC_OR) {
404 /* Need to do shortcircuiting */
405 FlatCondBranch fcb=new FlatCondBranch(temp_left);
406 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
407 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
408 FlatNop fnop=new FlatNop();
409 left.getEnd().addNext(fcb);
410 fcb.addFalseNext(right.getBegin());
411 right.getEnd().addNext(fon2);
413 fcb.addTrueNext(fon1);
415 return new NodePair(left.getBegin(), fnop);
416 } else if (op.getOp()==Operation.LOGIC_AND) {
417 /* Need to do shortcircuiting */
418 FlatCondBranch fcb=new FlatCondBranch(temp_left);
419 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
420 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
421 FlatNop fnop=new FlatNop();
422 left.getEnd().addNext(fcb);
423 fcb.addTrueNext(right.getBegin());
424 right.getEnd().addNext(fon2);
426 fcb.addFalseNext(fon1);
428 return new NodePair(left.getBegin(), fnop);
431 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
432 left.getEnd().addNext(right.getBegin());
433 right.getEnd().addNext(fon);
434 return new NodePair(left.getBegin(),fon);
437 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
439 case Kind.AssignmentNode:
440 return flattenAssignmentNode((AssignmentNode)en,out_temp);
442 return flattenCastNode((CastNode)en,out_temp);
443 case Kind.CreateObjectNode:
444 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
445 case Kind.FieldAccessNode:
446 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
447 case Kind.ArrayAccessNode:
448 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
449 case Kind.LiteralNode:
450 return flattenLiteralNode((LiteralNode)en,out_temp);
451 case Kind.MethodInvokeNode:
452 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
454 return flattenNameNode((NameNode)en,out_temp);
456 return flattenOpNode((OpNode)en,out_temp);
461 private NodePair flattenDeclarationNode(DeclarationNode dn) {
462 VarDescriptor vd=dn.getVarDescriptor();
463 TempDescriptor td=getTempforVar(vd);
464 if (dn.getExpression()!=null)
465 return flattenExpressionNode(dn.getExpression(),td);
467 FlatNop fn=new FlatNop();
468 return new NodePair(fn,fn);
472 private TempDescriptor getTempforVar(VarDescriptor vd) {
473 if (temptovar.containsKey(vd))
474 return (TempDescriptor)temptovar.get(vd);
476 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
477 temptovar.put(vd,td);
482 private NodePair flattenIfStatementNode(IfStatementNode isn) {
483 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
484 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
485 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
486 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
488 FlatNop nopend=new FlatNop();
490 if (isn.getFalseBlock()!=null)
491 false_np=flattenBlockNode(isn.getFalseBlock());
493 FlatNop nop=new FlatNop();
494 false_np=new NodePair(nop,nop);
497 cond.getEnd().addNext(fcb);
498 fcb.addTrueNext(true_np.getBegin());
499 fcb.addFalseNext(false_np.getBegin());
500 true_np.getEnd().addNext(nopend);
501 false_np.getEnd().addNext(nopend);
502 return new NodePair(cond.getBegin(), nopend);
505 private NodePair flattenLoopNode(LoopNode ln) {
506 if (ln.getType()==LoopNode.FORLOOP) {
507 NodePair initializer=flattenBlockNode(ln.getInitializer());
508 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
509 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
510 NodePair update=flattenBlockNode(ln.getUpdate());
511 NodePair body=flattenBlockNode(ln.getBody());
512 FlatNode begin=initializer.getBegin();
513 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
514 FlatNop nopend=new FlatNop();
516 initializer.getEnd().addNext(condition.getBegin());
517 body.getEnd().addNext(update.getBegin());
518 update.getEnd().addNext(condition.getBegin());
519 condition.getEnd().addNext(fcb);
520 fcb.addFalseNext(nopend);
521 fcb.addTrueNext(body.getBegin());
522 return new NodePair(begin,nopend);
523 } else if (ln.getType()==LoopNode.WHILELOOP) {
524 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
525 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
526 NodePair body=flattenBlockNode(ln.getBody());
527 FlatNode begin=condition.getBegin();
528 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
529 FlatNop nopend=new FlatNop();
531 body.getEnd().addNext(condition.getBegin());
532 condition.getEnd().addNext(fcb);
533 fcb.addFalseNext(nopend);
534 fcb.addTrueNext(body.getBegin());
535 return new NodePair(begin,nopend);
536 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
537 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
538 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
539 NodePair body=flattenBlockNode(ln.getBody());
540 FlatNode begin=body.getBegin();
541 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
542 FlatNop nopend=new FlatNop();
544 body.getEnd().addNext(condition.getBegin());
545 condition.getEnd().addNext(fcb);
546 fcb.addFalseNext(nopend);
547 fcb.addTrueNext(body.getBegin());
548 return new NodePair(begin,nopend);
549 } else throw new Error();
552 private NodePair flattenReturnNode(ReturnNode rntree) {
553 TempDescriptor retval=null;
555 if (rntree.getReturnExpression()!=null) {
556 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
557 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
560 FlatReturnNode rnflat=new FlatReturnNode(retval);
563 cond.getEnd().addNext(rnflat);
564 return new NodePair(cond.getBegin(),rnflat);
566 return new NodePair(rnflat,rnflat);
569 private NodePair flattenTaskExitNode(TaskExitNode ten) {
570 FlatFlagActionNode ffan=new FlatFlagActionNode(true);
571 updateFlagActionNode(ffan, ten.getFlagEffects());
572 return new NodePair(ffan, ffan);
575 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
576 return flattenBlockNode(sbn.getBlockNode());
579 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
581 case Kind.BlockExpressionNode:
582 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
584 case Kind.DeclarationNode:
585 return flattenDeclarationNode((DeclarationNode)bsn);
587 case Kind.IfStatementNode:
588 return flattenIfStatementNode((IfStatementNode)bsn);
591 return flattenLoopNode((LoopNode)bsn);
593 case Kind.ReturnNode:
594 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
596 case Kind.TaskExitNode:
597 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
599 case Kind.SubBlockNode:
600 return flattenSubBlockNode((SubBlockNode)bsn);