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
289 // left side is array
291 Operation base=an.getOperation().getBaseOp();
292 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
295 //rewrite the base operation
296 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
300 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
303 if (an.getSrc()!=null) {
304 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
305 first=np_src.getBegin();
306 last=np_src.getEnd();
308 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
313 if (an.getDest().kind()==Kind.FieldAccessNode) {
314 //We are assigning an object field
316 FieldAccessNode fan=(FieldAccessNode)an.getDest();
317 ExpressionNode en=fan.getExpression();
318 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
319 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
321 first=np_baseexp.getBegin();
323 last.addNext(np_baseexp.getBegin());
324 last=np_baseexp.getEnd();
326 //See if we need to perform an operation
328 //If it is a preinc we need to store the initial value
329 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
330 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
332 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
335 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
341 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
345 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
349 return new NodePair(first, last);
350 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
351 //We are assigning an array element
354 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
355 ExpressionNode en=aan.getExpression();
356 ExpressionNode enindex=aan.getIndex();
357 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
358 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
359 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
360 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
362 first=np_baseexp.getBegin();
364 last.addNext(np_baseexp.getBegin());
365 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
366 last=np_indexexp.getEnd();
368 //See if we need to perform an operation
370 //If it is a preinc we need to store the initial value
371 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
372 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
374 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
377 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
384 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
388 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
392 return new NodePair(first, last);
393 } else if (an.getDest().kind()==Kind.NameNode) {
394 //We could be assigning a field or variable
395 NameNode nn=(NameNode)an.getDest();
396 if (nn.getExpression()!=null) {
398 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
399 ExpressionNode en=fan.getExpression();
400 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
401 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
403 first=np_baseexp.getBegin();
405 last.addNext(np_baseexp.getBegin());
406 last=np_baseexp.getEnd();
408 //See if we need to perform an operation
410 //If it is a preinc we need to store the initial value
411 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
412 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
414 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
417 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
424 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
428 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
432 return new NodePair(first, last);
434 if (nn.getField()!=null) {
438 //See if we need to perform an operation
440 //If it is a preinc we need to store the initial value
441 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
442 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
444 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
451 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
457 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
465 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
469 return new NodePair(first, last);
472 //See if we need to perform an operation
475 //If it is a preinc we need to store the initial value
476 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
477 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
479 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
487 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
496 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
500 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
504 return new NodePair(first, last);
511 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
512 if (nn.getExpression()!=null) {
513 /* Hack - use subtree instead */
514 return flattenExpressionNode(nn.getExpression(),out_temp);
515 } else if (nn.getField()!=null) {
516 TempDescriptor tmp=getTempforVar(nn.getVar());
517 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
518 return new NodePair(ffn,ffn);
520 TempDescriptor tmp=getTempforVar(nn.getVar());
521 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
522 return new NodePair(fon,fon);
526 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
527 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
528 TempDescriptor temp_right=null;
530 Operation op=on.getOp();
531 /* We've moved this to assignment nodes
533 if (op.getOp()==Operation.POSTINC||
534 op.getOp()==Operation.POSTDEC||
535 op.getOp()==Operation.PREINC||
536 op.getOp()==Operation.PREDEC) {
537 LiteralNode ln=new LiteralNode("int",new Integer(1));
538 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
540 AssignmentNode an=new AssignmentNode(on.getLeft(),
541 new OpNode(on.getLeft(),ln,
542 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
544 if (op.getOp()==Operation.POSTINC||
545 op.getOp()==Operation.POSTDEC) {
546 //Can't do, this could have side effects
547 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
548 NodePair assign=flattenAssignmentNode(an,temp_left);
549 left.getEnd().addNext(assign.getBegin());
550 return new NodePair(left.getBegin(),assign.getEnd());
552 NodePair assign=flattenAssignmentNode(an,out_temp);
557 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
559 if (on.getRight()!=null) {
560 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
561 right=flattenExpressionNode(on.getRight(),temp_right);
563 FlatNop nop=new FlatNop();
564 right=new NodePair(nop,nop);
567 if (op.getOp()==Operation.LOGIC_OR) {
568 /* Need to do shortcircuiting */
569 FlatCondBranch fcb=new FlatCondBranch(temp_left);
570 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
571 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
572 FlatNop fnop=new FlatNop();
573 left.getEnd().addNext(fcb);
574 fcb.addFalseNext(right.getBegin());
575 right.getEnd().addNext(fon2);
577 fcb.addTrueNext(fon1);
579 return new NodePair(left.getBegin(), fnop);
580 } else if (op.getOp()==Operation.LOGIC_AND) {
581 /* Need to do shortcircuiting */
582 FlatCondBranch fcb=new FlatCondBranch(temp_left);
583 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
584 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
585 FlatNop fnop=new FlatNop();
586 left.getEnd().addNext(fcb);
587 fcb.addTrueNext(right.getBegin());
588 right.getEnd().addNext(fon2);
590 fcb.addFalseNext(fon1);
592 return new NodePair(left.getBegin(), fnop);
595 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
596 left.getEnd().addNext(right.getBegin());
597 right.getEnd().addNext(fon);
598 return new NodePair(left.getBegin(),fon);
601 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
603 case Kind.AssignmentNode:
604 return flattenAssignmentNode((AssignmentNode)en,out_temp);
606 return flattenCastNode((CastNode)en,out_temp);
607 case Kind.CreateObjectNode:
608 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
609 case Kind.FieldAccessNode:
610 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
611 case Kind.ArrayAccessNode:
612 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
613 case Kind.LiteralNode:
614 return flattenLiteralNode((LiteralNode)en,out_temp);
615 case Kind.MethodInvokeNode:
616 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
618 return flattenNameNode((NameNode)en,out_temp);
620 return flattenOpNode((OpNode)en,out_temp);
625 private NodePair flattenDeclarationNode(DeclarationNode dn) {
626 VarDescriptor vd=dn.getVarDescriptor();
627 TempDescriptor td=getTempforVar(vd);
628 if (dn.getExpression()!=null)
629 return flattenExpressionNode(dn.getExpression(),td);
631 FlatNop fn=new FlatNop();
632 return new NodePair(fn,fn);
636 private TempDescriptor getTempforVar(VarDescriptor vd) {
637 if (temptovar.containsKey(vd))
638 return (TempDescriptor)temptovar.get(vd);
640 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
641 temptovar.put(vd,td);
646 private NodePair flattenIfStatementNode(IfStatementNode isn) {
647 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
648 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
649 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
650 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
652 FlatNop nopend=new FlatNop();
654 if (isn.getFalseBlock()!=null)
655 false_np=flattenBlockNode(isn.getFalseBlock());
657 FlatNop nop=new FlatNop();
658 false_np=new NodePair(nop,nop);
661 cond.getEnd().addNext(fcb);
662 fcb.addTrueNext(true_np.getBegin());
663 fcb.addFalseNext(false_np.getBegin());
664 true_np.getEnd().addNext(nopend);
665 false_np.getEnd().addNext(nopend);
666 return new NodePair(cond.getBegin(), nopend);
669 private NodePair flattenLoopNode(LoopNode ln) {
670 if (ln.getType()==LoopNode.FORLOOP) {
671 NodePair initializer=flattenBlockNode(ln.getInitializer());
672 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
673 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
674 NodePair update=flattenBlockNode(ln.getUpdate());
675 NodePair body=flattenBlockNode(ln.getBody());
676 FlatNode begin=initializer.getBegin();
677 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
678 FlatNop nopend=new FlatNop();
680 initializer.getEnd().addNext(condition.getBegin());
681 body.getEnd().addNext(update.getBegin());
682 update.getEnd().addNext(condition.getBegin());
683 condition.getEnd().addNext(fcb);
684 fcb.addFalseNext(nopend);
685 fcb.addTrueNext(body.getBegin());
686 return new NodePair(begin,nopend);
687 } else if (ln.getType()==LoopNode.WHILELOOP) {
688 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
689 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
690 NodePair body=flattenBlockNode(ln.getBody());
691 FlatNode begin=condition.getBegin();
692 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
693 FlatNop nopend=new FlatNop();
695 body.getEnd().addNext(condition.getBegin());
696 condition.getEnd().addNext(fcb);
697 fcb.addFalseNext(nopend);
698 fcb.addTrueNext(body.getBegin());
699 return new NodePair(begin,nopend);
700 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
701 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
702 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
703 NodePair body=flattenBlockNode(ln.getBody());
704 FlatNode begin=body.getBegin();
705 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
706 FlatNop nopend=new FlatNop();
708 body.getEnd().addNext(condition.getBegin());
709 condition.getEnd().addNext(fcb);
710 fcb.addFalseNext(nopend);
711 fcb.addTrueNext(body.getBegin());
712 return new NodePair(begin,nopend);
713 } else throw new Error();
716 private NodePair flattenReturnNode(ReturnNode rntree) {
717 TempDescriptor retval=null;
719 if (rntree.getReturnExpression()!=null) {
720 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
721 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
724 FlatReturnNode rnflat=new FlatReturnNode(retval);
727 cond.getEnd().addNext(rnflat);
728 return new NodePair(cond.getBegin(),rnflat);
730 return new NodePair(rnflat,rnflat);
733 private NodePair flattenTaskExitNode(TaskExitNode ten) {
734 FlatFlagActionNode ffan=new FlatFlagActionNode(true);
735 updateFlagActionNode(ffan, ten.getFlagEffects());
736 return new NodePair(ffan, ffan);
739 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
740 return flattenBlockNode(sbn.getBlockNode());
743 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
745 case Kind.BlockExpressionNode:
746 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
748 case Kind.DeclarationNode:
749 return flattenDeclarationNode((DeclarationNode)bsn);
751 case Kind.IfStatementNode:
752 return flattenIfStatementNode((IfStatementNode)bsn);
755 return flattenLoopNode((LoopNode)bsn);
757 case Kind.ReturnNode:
758 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
760 case Kind.TaskExitNode:
761 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
763 case Kind.SubBlockNode:
764 return flattenSubBlockNode((SubBlockNode)bsn);