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 if (flags==null) // Do nothing if the flag effects vector is empty
57 for(int i=0;i<flags.size();i++) {
58 FlagEffects fes=(FlagEffects)flags.get(i);
59 TempDescriptor flagtemp=getTempforVar(fes.getVar());
60 for(int j=0;j<fes.numEffects();j++) {
61 FlagEffect fe=fes.getEffect(j);
62 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
67 private void flattenClass(ClassDescriptor cn) {
68 Iterator methodit=cn.getMethods();
69 while(methodit.hasNext()) {
70 MethodDescriptor md=(MethodDescriptor)methodit.next();
71 BlockNode bn=state.getMethodBody(md);
72 FlatNode fn=flattenBlockNode(bn).getBegin();
73 FlatMethod fm=new FlatMethod(md, fn);
75 fm.addParameterTemp(getTempforVar(md.getThis()));
76 for(int i=0;i<md.numParameters();i++) {
77 fm.addParameterTemp(getTempforVar(md.getParameter(i)));
79 state.addFlatCode(md,fm);
83 private NodePair flattenBlockNode(BlockNode bn) {
86 for(int i=0;i<bn.size();i++) {
87 NodePair np=flattenBlockStatementNode(bn.get(i));
88 FlatNode np_begin=np.getBegin();
89 FlatNode np_end=np.getEnd();
96 end.addNext(np_begin);
101 end=begin=new FlatNop();
103 return new NodePair(begin,end);
106 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
107 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
108 return flattenExpressionNode(en.getExpression(),tmp);
111 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
112 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
113 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
114 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
115 np.getEnd().addNext(fcn);
116 return new NodePair(np.getBegin(),fcn);
119 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
120 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
121 return new NodePair(fln,fln);
124 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
125 TypeDescriptor td=con.getType();
127 FlatNew fn=new FlatNew(td, out_temp);
128 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
131 if (con.getFlagEffects()!=null) {
132 FlatFlagActionNode ffan=new FlatFlagActionNode(false);
133 FlagEffects fes=con.getFlagEffects();
134 TempDescriptor flagtemp=out_temp;
135 for(int j=0;j<fes.numEffects();j++) {
136 FlagEffect fe=fes.getEffect(j);
137 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
143 for(int i=0;i<con.numArgs();i++) {
144 ExpressionNode en=con.getArg(i);
145 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
147 NodePair np=flattenExpressionNode(en, tmp);
148 last.addNext(np.getBegin());
151 MethodDescriptor md=con.getConstructor();
152 //Call to constructor
153 FlatCall fc=new FlatCall(md, null, out_temp, temps);
156 return new NodePair(fn,last);
160 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
161 for (int i=0;i<con.numArgs();i++) {
162 ExpressionNode en=con.getArg(i);
163 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
165 NodePair np=flattenExpressionNode(en, tmp);
169 last.addNext(np.getBegin());
172 TempDescriptor tmp2=(i==0)?
174 TempDescriptor.tempFactory("arg",en.getType());
176 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
178 if (temps.length>1) {
179 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
180 fn.addNext(np.getBegin());
181 return new NodePair(first,np.getEnd());
183 return new NodePair(first, fn);
187 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
188 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
189 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
190 FlatNop fnop=new FlatNop();//last node
193 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
195 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
197 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
199 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
200 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
202 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
203 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
204 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
206 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
212 fcb.addFalseNext(fnop);
214 //Recursive call here
215 if ((i+2)<temparray.length) {
216 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
217 fsen.addNext(np2.getBegin());
218 np2.getEnd().addNext(fon);
223 return new NodePair(fln, fnop);
226 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
227 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
230 TempDescriptor thisarg=null;
232 if (min.getExpression()!=null) {
233 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
234 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
240 for(int i=0;i<min.numArgs();i++) {
241 ExpressionNode en=min.getArg(i);
242 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
244 NodePair np=flattenExpressionNode(en, td);
248 last.addNext(np.getBegin());
252 MethodDescriptor md=min.getMethod();
254 //Call to constructor
257 if(md.getReturnType()==null||md.getReturnType().isVoid())
258 fc=new FlatCall(md, null, thisarg, temps);
260 fc=new FlatCall(md, out_temp, thisarg, temps);
265 return new NodePair(first,fc);
268 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
269 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
270 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
271 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
272 npe.getEnd().addNext(fn);
273 return new NodePair(npe.getBegin(),fn);
276 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
277 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
278 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
279 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
280 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
281 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
282 npe.getEnd().addNext(npi.getBegin());
283 npi.getEnd().addNext(fn);
284 return new NodePair(npe.getBegin(),fn);
287 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
289 // left side is variable
290 // left side is field
291 // left side is array
293 Operation base=an.getOperation().getBaseOp();
294 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
297 //rewrite the base operation
298 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
302 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
305 if (an.getSrc()!=null) {
306 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
307 first=np_src.getBegin();
308 last=np_src.getEnd();
310 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
315 if (an.getDest().kind()==Kind.FieldAccessNode) {
316 //We are assigning an object field
318 FieldAccessNode fan=(FieldAccessNode)an.getDest();
319 ExpressionNode en=fan.getExpression();
320 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
321 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
323 first=np_baseexp.getBegin();
325 last.addNext(np_baseexp.getBegin());
326 last=np_baseexp.getEnd();
328 //See if we need to perform an operation
330 //If it is a preinc we need to store the initial value
331 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
332 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
334 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
337 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
343 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
347 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
351 return new NodePair(first, last);
352 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
353 //We are assigning an array element
356 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
357 ExpressionNode en=aan.getExpression();
358 ExpressionNode enindex=aan.getIndex();
359 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
360 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
361 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
362 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
364 first=np_baseexp.getBegin();
366 last.addNext(np_baseexp.getBegin());
367 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
368 last=np_indexexp.getEnd();
370 //See if we need to perform an operation
372 //If it is a preinc we need to store the initial value
373 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
374 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
376 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
379 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
386 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
390 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
394 return new NodePair(first, last);
395 } else if (an.getDest().kind()==Kind.NameNode) {
396 //We could be assigning a field or variable
397 NameNode nn=(NameNode)an.getDest();
398 if (nn.getExpression()!=null) {
400 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
401 ExpressionNode en=fan.getExpression();
402 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
403 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
405 first=np_baseexp.getBegin();
407 last.addNext(np_baseexp.getBegin());
408 last=np_baseexp.getEnd();
410 //See if we need to perform an operation
412 //If it is a preinc we need to store the initial value
413 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
414 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
416 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
419 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
426 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
430 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
434 return new NodePair(first, last);
436 if (nn.getField()!=null) {
440 //See if we need to perform an operation
442 //If it is a preinc we need to store the initial value
443 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
444 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
446 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
453 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
459 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
467 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
471 return new NodePair(first, last);
474 //See if we need to perform an operation
477 //If it is a preinc we need to store the initial value
478 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
479 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
481 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
489 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
498 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
502 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
506 return new NodePair(first, last);
513 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
514 if (nn.getExpression()!=null) {
515 /* Hack - use subtree instead */
516 return flattenExpressionNode(nn.getExpression(),out_temp);
517 } else if (nn.getField()!=null) {
518 TempDescriptor tmp=getTempforVar(nn.getVar());
519 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
520 return new NodePair(ffn,ffn);
522 TempDescriptor tmp=getTempforVar(nn.getVar());
523 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
524 return new NodePair(fon,fon);
528 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
529 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
530 TempDescriptor temp_right=null;
532 Operation op=on.getOp();
533 /* We've moved this to assignment nodes
535 if (op.getOp()==Operation.POSTINC||
536 op.getOp()==Operation.POSTDEC||
537 op.getOp()==Operation.PREINC||
538 op.getOp()==Operation.PREDEC) {
539 LiteralNode ln=new LiteralNode("int",new Integer(1));
540 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
542 AssignmentNode an=new AssignmentNode(on.getLeft(),
543 new OpNode(on.getLeft(),ln,
544 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
546 if (op.getOp()==Operation.POSTINC||
547 op.getOp()==Operation.POSTDEC) {
548 //Can't do, this could have side effects
549 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
550 NodePair assign=flattenAssignmentNode(an,temp_left);
551 left.getEnd().addNext(assign.getBegin());
552 return new NodePair(left.getBegin(),assign.getEnd());
554 NodePair assign=flattenAssignmentNode(an,out_temp);
559 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
561 if (on.getRight()!=null) {
562 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
563 right=flattenExpressionNode(on.getRight(),temp_right);
565 FlatNop nop=new FlatNop();
566 right=new NodePair(nop,nop);
569 if (op.getOp()==Operation.LOGIC_OR) {
570 /* Need to do shortcircuiting */
571 FlatCondBranch fcb=new FlatCondBranch(temp_left);
572 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
573 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
574 FlatNop fnop=new FlatNop();
575 left.getEnd().addNext(fcb);
576 fcb.addFalseNext(right.getBegin());
577 right.getEnd().addNext(fon2);
579 fcb.addTrueNext(fon1);
581 return new NodePair(left.getBegin(), fnop);
582 } else if (op.getOp()==Operation.LOGIC_AND) {
583 /* Need to do shortcircuiting */
584 FlatCondBranch fcb=new FlatCondBranch(temp_left);
585 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
586 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
587 FlatNop fnop=new FlatNop();
588 left.getEnd().addNext(fcb);
589 fcb.addTrueNext(right.getBegin());
590 right.getEnd().addNext(fon2);
592 fcb.addFalseNext(fon1);
594 return new NodePair(left.getBegin(), fnop);
597 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
598 left.getEnd().addNext(right.getBegin());
599 right.getEnd().addNext(fon);
600 return new NodePair(left.getBegin(),fon);
603 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
605 case Kind.AssignmentNode:
606 return flattenAssignmentNode((AssignmentNode)en,out_temp);
608 return flattenCastNode((CastNode)en,out_temp);
609 case Kind.CreateObjectNode:
610 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
611 case Kind.FieldAccessNode:
612 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
613 case Kind.ArrayAccessNode:
614 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
615 case Kind.LiteralNode:
616 return flattenLiteralNode((LiteralNode)en,out_temp);
617 case Kind.MethodInvokeNode:
618 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
620 return flattenNameNode((NameNode)en,out_temp);
622 return flattenOpNode((OpNode)en,out_temp);
627 private NodePair flattenDeclarationNode(DeclarationNode dn) {
628 VarDescriptor vd=dn.getVarDescriptor();
629 TempDescriptor td=getTempforVar(vd);
630 if (dn.getExpression()!=null)
631 return flattenExpressionNode(dn.getExpression(),td);
633 FlatNop fn=new FlatNop();
634 return new NodePair(fn,fn);
638 private TempDescriptor getTempforVar(VarDescriptor vd) {
639 if (temptovar.containsKey(vd))
640 return (TempDescriptor)temptovar.get(vd);
642 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
643 temptovar.put(vd,td);
648 private NodePair flattenIfStatementNode(IfStatementNode isn) {
649 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
650 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
651 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
652 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
654 FlatNop nopend=new FlatNop();
656 if (isn.getFalseBlock()!=null)
657 false_np=flattenBlockNode(isn.getFalseBlock());
659 FlatNop nop=new FlatNop();
660 false_np=new NodePair(nop,nop);
663 cond.getEnd().addNext(fcb);
664 fcb.addTrueNext(true_np.getBegin());
665 fcb.addFalseNext(false_np.getBegin());
666 true_np.getEnd().addNext(nopend);
667 false_np.getEnd().addNext(nopend);
668 return new NodePair(cond.getBegin(), nopend);
671 private NodePair flattenLoopNode(LoopNode ln) {
672 if (ln.getType()==LoopNode.FORLOOP) {
673 NodePair initializer=flattenBlockNode(ln.getInitializer());
674 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
675 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
676 NodePair update=flattenBlockNode(ln.getUpdate());
677 NodePair body=flattenBlockNode(ln.getBody());
678 FlatNode begin=initializer.getBegin();
679 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
680 FlatNop nopend=new FlatNop();
682 initializer.getEnd().addNext(condition.getBegin());
683 body.getEnd().addNext(update.getBegin());
684 update.getEnd().addNext(condition.getBegin());
685 condition.getEnd().addNext(fcb);
686 fcb.addFalseNext(nopend);
687 fcb.addTrueNext(body.getBegin());
688 return new NodePair(begin,nopend);
689 } else if (ln.getType()==LoopNode.WHILELOOP) {
690 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
691 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
692 NodePair body=flattenBlockNode(ln.getBody());
693 FlatNode begin=condition.getBegin();
694 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
695 FlatNop nopend=new FlatNop();
697 body.getEnd().addNext(condition.getBegin());
698 condition.getEnd().addNext(fcb);
699 fcb.addFalseNext(nopend);
700 fcb.addTrueNext(body.getBegin());
701 return new NodePair(begin,nopend);
702 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
703 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
704 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
705 NodePair body=flattenBlockNode(ln.getBody());
706 FlatNode begin=body.getBegin();
707 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
708 FlatNop nopend=new FlatNop();
710 body.getEnd().addNext(condition.getBegin());
711 condition.getEnd().addNext(fcb);
712 fcb.addFalseNext(nopend);
713 fcb.addTrueNext(body.getBegin());
714 return new NodePair(begin,nopend);
715 } else throw new Error();
718 private NodePair flattenReturnNode(ReturnNode rntree) {
719 TempDescriptor retval=null;
721 if (rntree.getReturnExpression()!=null) {
722 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
723 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
726 FlatReturnNode rnflat=new FlatReturnNode(retval);
729 cond.getEnd().addNext(rnflat);
730 return new NodePair(cond.getBegin(),rnflat);
732 return new NodePair(rnflat,rnflat);
735 private NodePair flattenTaskExitNode(TaskExitNode ten) {
736 FlatFlagActionNode ffan=new FlatFlagActionNode(true);
737 updateFlagActionNode(ffan, ten.getFlagEffects());
738 NodePair fcn=flattenConstraintCheck(ten.getChecks());
739 ffan.addNext(fcn.getBegin());
740 return new NodePair(ffan, fcn.getEnd());
743 private NodePair flattenConstraintCheck(Vector ccs) {
744 FlatNode begin=new FlatNop();
746 return new NodePair(begin,begin);
748 for(int i=0;i<ccs.size();i++) {
749 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
750 /* Flatten the arguments */
751 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
752 String[] vars=new String[cc.numArgs()];
753 for(int j=0;j<cc.numArgs();j++) {
754 ExpressionNode en=cc.getArg(j);
755 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
757 vars[j]=cc.getVar(j);
758 NodePair np=flattenExpressionNode(en, td);
759 last.addNext(np.getBegin());
763 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
767 return new NodePair(begin,last);
770 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
771 return flattenBlockNode(sbn.getBlockNode());
774 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
776 case Kind.BlockExpressionNode:
777 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
779 case Kind.DeclarationNode:
780 return flattenDeclarationNode((DeclarationNode)bsn);
782 case Kind.IfStatementNode:
783 return flattenIfStatementNode((IfStatementNode)bsn);
786 return flattenLoopNode((LoopNode)bsn);
788 case Kind.ReturnNode:
789 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
791 case Kind.TaskExitNode:
792 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
794 case Kind.SubBlockNode:
795 return flattenSubBlockNode((SubBlockNode)bsn);