6 public class BuildFlat {
9 MethodDescriptor currmd;
12 public BuildFlat(State st, TypeUtil typeutil) {
14 temptovar=new Hashtable();
15 this.typeutil=typeutil;
18 public Hashtable getMap() {
22 public void buildFlat() {
23 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
25 ClassDescriptor cn=(ClassDescriptor)it.next();
29 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
30 while(task_it.hasNext()) {
31 TaskDescriptor td=(TaskDescriptor)task_it.next();
36 private void flattenTask(TaskDescriptor td) {
37 BlockNode bn=state.getMethodBody(td);
38 FlatNode fn=flattenBlockNode(bn).getBegin();
39 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
41 FlatMethod fm=new FlatMethod(td, ffan);
43 for(int i=0;i<td.numParameters();i++) {
44 fm.addParameterTemp(getTempforVar(td.getParameter(i)));
47 /* Flatten Vector of Flag Effects */
48 Vector flags=td.getFlagEffects();
49 updateFlagActionNode(ffan,flags);
51 state.addFlatCode(td,fm);
55 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
56 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
57 if (flags==null) // Do nothing if the flag effects vector is empty
60 for(int i=0;i<flags.size();i++) {
61 FlagEffects fes=(FlagEffects)flags.get(i);
62 TempDescriptor flagtemp=getTempforVar(fes.getVar());
63 for(int j=0;j<fes.numEffects();j++) {
64 FlagEffect fe=fes.getEffect(j);
65 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
70 private void flattenClass(ClassDescriptor cn) {
71 Iterator methodit=cn.getMethods();
72 while(methodit.hasNext()) {
73 currmd=(MethodDescriptor)methodit.next();
74 BlockNode bn=state.getMethodBody(currmd);
75 NodePair np=flattenBlockNode(bn);
76 FlatNode fn=np.getBegin();
77 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
78 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
79 TempDescriptor thistd=getTempforVar(currmd.getThis());
80 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
83 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
84 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
85 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
86 np.getEnd().addNext(fcunlock);
90 FlatMethod fm=new FlatMethod(currmd, fn);
91 if (!currmd.isStatic())
92 fm.addParameterTemp(getTempforParam(currmd.getThis()));
93 for(int i=0;i<currmd.numParameters();i++) {
94 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
96 state.addFlatCode(currmd,fm);
100 private NodePair flattenBlockNode(BlockNode bn) {
103 for(int i=0;i<bn.size();i++) {
104 NodePair np=flattenBlockStatementNode(bn.get(i));
105 FlatNode np_begin=np.getBegin();
106 FlatNode np_end=np.getEnd();
113 end.addNext(np_begin);
118 end=begin=new FlatNop();
120 return new NodePair(begin,end);
123 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
124 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
125 return flattenExpressionNode(en.getExpression(),tmp);
128 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
129 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
130 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
131 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
132 np.getEnd().addNext(fcn);
133 return new NodePair(np.getBegin(),fcn);
136 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
137 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
138 return new NodePair(fln,fln);
141 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
142 TypeDescriptor td=con.getType();
144 FlatNew fn=new FlatNew(td, out_temp);
145 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
147 if (td.getClassDesc().hasFlags()) {
148 // if (con.getFlagEffects()!=null) {
149 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
150 FlagEffects fes=con.getFlagEffects();
151 TempDescriptor flagtemp=out_temp;
153 for(int j=0;j<fes.numEffects();j++) {
154 FlagEffect fe=fes.getEffect(j);
155 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
157 ffan.addFlagAction(flagtemp, null, false);
164 for(int i=0;i<con.numArgs();i++) {
165 ExpressionNode en=con.getArg(i);
166 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
168 NodePair np=flattenExpressionNode(en, tmp);
169 last.addNext(np.getBegin());
172 MethodDescriptor md=con.getConstructor();
173 //Call to constructor
174 FlatCall fc=new FlatCall(md, null, out_temp, temps);
177 return new NodePair(fn,last);
181 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
182 for (int i=0;i<con.numArgs();i++) {
183 ExpressionNode en=con.getArg(i);
184 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
186 NodePair np=flattenExpressionNode(en, tmp);
190 last.addNext(np.getBegin());
193 TempDescriptor tmp2=(i==0)?
195 TempDescriptor.tempFactory("arg",en.getType());
197 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
199 if (temps.length>1) {
200 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
201 fn.addNext(np.getBegin());
202 return new NodePair(first,np.getEnd());
204 return new NodePair(first, fn);
208 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
209 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
210 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
211 FlatNop fnop=new FlatNop();//last node
214 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
216 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
218 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
220 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
221 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
223 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
224 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
225 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
227 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
233 fcb.addFalseNext(fnop);
235 //Recursive call here
236 if ((i+2)<temparray.length) {
237 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
238 fsen.addNext(np2.getBegin());
239 np2.getEnd().addNext(fon);
244 return new NodePair(fln, fnop);
247 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
248 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
251 TempDescriptor thisarg=null;
253 if (min.getExpression()!=null) {
254 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
255 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
261 for(int i=0;i<min.numArgs();i++) {
262 ExpressionNode en=min.getArg(i);
263 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
265 NodePair np=flattenExpressionNode(en, td);
269 last.addNext(np.getBegin());
273 MethodDescriptor md=min.getMethod();
275 //Call to constructor
278 if(md.getReturnType()==null||md.getReturnType().isVoid())
279 fc=new FlatCall(md, null, thisarg, temps);
281 fc=new FlatCall(md, out_temp, thisarg, temps);
286 return new NodePair(first,fc);
289 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
290 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
291 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
292 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
293 npe.getEnd().addNext(fn);
294 return new NodePair(npe.getBegin(),fn);
297 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
298 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
299 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
300 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
301 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
302 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
303 npe.getEnd().addNext(npi.getBegin());
304 npi.getEnd().addNext(fn);
305 return new NodePair(npe.getBegin(),fn);
308 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
310 // left side is variable
311 // left side is field
312 // left side is array
314 Operation base=an.getOperation().getBaseOp();
315 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
318 //rewrite the base operation
319 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
323 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
326 if (an.getSrc()!=null) {
327 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
328 first=np_src.getBegin();
329 last=np_src.getEnd();
331 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
336 if (an.getDest().kind()==Kind.FieldAccessNode) {
337 //We are assigning an object field
339 FieldAccessNode fan=(FieldAccessNode)an.getDest();
340 ExpressionNode en=fan.getExpression();
341 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
342 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
344 first=np_baseexp.getBegin();
346 last.addNext(np_baseexp.getBegin());
347 last=np_baseexp.getEnd();
349 //See if we need to perform an operation
351 //If it is a preinc we need to store the initial value
352 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
353 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
355 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
358 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
364 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
368 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
372 return new NodePair(first, last);
373 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
374 //We are assigning an array element
377 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
378 ExpressionNode en=aan.getExpression();
379 ExpressionNode enindex=aan.getIndex();
380 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
381 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
382 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
383 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
385 first=np_baseexp.getBegin();
387 last.addNext(np_baseexp.getBegin());
388 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
389 last=np_indexexp.getEnd();
391 //See if we need to perform an operation
393 //If it is a preinc we need to store the initial value
394 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
395 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
397 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
400 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
407 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
411 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
415 return new NodePair(first, last);
416 } else if (an.getDest().kind()==Kind.NameNode) {
417 //We could be assigning a field or variable
418 NameNode nn=(NameNode)an.getDest();
419 if (nn.getExpression()!=null) {
421 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
422 ExpressionNode en=fan.getExpression();
423 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
424 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
426 first=np_baseexp.getBegin();
428 last.addNext(np_baseexp.getBegin());
429 last=np_baseexp.getEnd();
431 //See if we need to perform an operation
433 //If it is a preinc we need to store the initial value
434 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
435 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
437 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
440 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
447 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
451 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
455 return new NodePair(first, last);
457 if (nn.getField()!=null) {
461 //See if we need to perform an operation
463 //If it is a preinc we need to store the initial value
464 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
465 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
467 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
474 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
480 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
488 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
492 return new NodePair(first, last);
495 //See if we need to perform an operation
498 //If it is a preinc we need to store the initial value
499 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
500 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
502 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
510 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
519 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
523 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
527 return new NodePair(first, last);
534 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
535 if (nn.getExpression()!=null) {
536 /* Hack - use subtree instead */
537 return flattenExpressionNode(nn.getExpression(),out_temp);
538 } else if (nn.getField()!=null) {
539 TempDescriptor tmp=getTempforVar(nn.getVar());
540 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
541 return new NodePair(ffn,ffn);
543 TempDescriptor tmp=getTempforVar(nn.getVar());
544 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
545 return new NodePair(fon,fon);
549 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
550 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
551 TempDescriptor temp_right=null;
553 Operation op=on.getOp();
554 /* We've moved this to assignment nodes
556 if (op.getOp()==Operation.POSTINC||
557 op.getOp()==Operation.POSTDEC||
558 op.getOp()==Operation.PREINC||
559 op.getOp()==Operation.PREDEC) {
560 LiteralNode ln=new LiteralNode("int",new Integer(1));
561 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
563 AssignmentNode an=new AssignmentNode(on.getLeft(),
564 new OpNode(on.getLeft(),ln,
565 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
567 if (op.getOp()==Operation.POSTINC||
568 op.getOp()==Operation.POSTDEC) {
569 //Can't do, this could have side effects
570 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
571 NodePair assign=flattenAssignmentNode(an,temp_left);
572 left.getEnd().addNext(assign.getBegin());
573 return new NodePair(left.getBegin(),assign.getEnd());
575 NodePair assign=flattenAssignmentNode(an,out_temp);
580 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
582 if (on.getRight()!=null) {
583 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
584 right=flattenExpressionNode(on.getRight(),temp_right);
586 FlatNop nop=new FlatNop();
587 right=new NodePair(nop,nop);
590 if (op.getOp()==Operation.LOGIC_OR) {
591 /* Need to do shortcircuiting */
592 FlatCondBranch fcb=new FlatCondBranch(temp_left);
593 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
594 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
595 FlatNop fnop=new FlatNop();
596 left.getEnd().addNext(fcb);
597 fcb.addFalseNext(right.getBegin());
598 right.getEnd().addNext(fon2);
600 fcb.addTrueNext(fon1);
602 return new NodePair(left.getBegin(), fnop);
603 } else if (op.getOp()==Operation.LOGIC_AND) {
604 /* Need to do shortcircuiting */
605 FlatCondBranch fcb=new FlatCondBranch(temp_left);
606 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
607 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
608 FlatNop fnop=new FlatNop();
609 left.getEnd().addNext(fcb);
610 fcb.addTrueNext(right.getBegin());
611 right.getEnd().addNext(fon2);
613 fcb.addFalseNext(fon1);
615 return new NodePair(left.getBegin(), fnop);
618 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
619 left.getEnd().addNext(right.getBegin());
620 right.getEnd().addNext(fon);
621 return new NodePair(left.getBegin(),fon);
624 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
626 case Kind.AssignmentNode:
627 return flattenAssignmentNode((AssignmentNode)en,out_temp);
629 return flattenCastNode((CastNode)en,out_temp);
630 case Kind.CreateObjectNode:
631 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
632 case Kind.FieldAccessNode:
633 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
634 case Kind.ArrayAccessNode:
635 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
636 case Kind.LiteralNode:
637 return flattenLiteralNode((LiteralNode)en,out_temp);
638 case Kind.MethodInvokeNode:
639 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
641 return flattenNameNode((NameNode)en,out_temp);
643 return flattenOpNode((OpNode)en,out_temp);
648 private NodePair flattenDeclarationNode(DeclarationNode dn) {
649 VarDescriptor vd=dn.getVarDescriptor();
650 TempDescriptor td=getTempforVar(vd);
651 if (dn.getExpression()!=null)
652 return flattenExpressionNode(dn.getExpression(),td);
654 FlatNop fn=new FlatNop();
655 return new NodePair(fn,fn);
659 private TempDescriptor getTempforParam(VarDescriptor vd) {
660 if (temptovar.containsKey(vd))
661 return (TempDescriptor)temptovar.get(vd);
663 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
664 temptovar.put(vd,td);
669 private TempDescriptor getTempforVar(VarDescriptor vd) {
670 if (temptovar.containsKey(vd))
671 return (TempDescriptor)temptovar.get(vd);
673 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
674 temptovar.put(vd,td);
679 private NodePair flattenIfStatementNode(IfStatementNode isn) {
680 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
681 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
682 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
683 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
685 FlatNop nopend=new FlatNop();
687 if (isn.getFalseBlock()!=null)
688 false_np=flattenBlockNode(isn.getFalseBlock());
690 FlatNop nop=new FlatNop();
691 false_np=new NodePair(nop,nop);
694 cond.getEnd().addNext(fcb);
695 fcb.addTrueNext(true_np.getBegin());
696 fcb.addFalseNext(false_np.getBegin());
697 true_np.getEnd().addNext(nopend);
698 false_np.getEnd().addNext(nopend);
699 return new NodePair(cond.getBegin(), nopend);
702 private NodePair flattenLoopNode(LoopNode ln) {
703 if (ln.getType()==LoopNode.FORLOOP) {
704 NodePair initializer=flattenBlockNode(ln.getInitializer());
705 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
706 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
707 NodePair update=flattenBlockNode(ln.getUpdate());
708 NodePair body=flattenBlockNode(ln.getBody());
709 FlatNode begin=initializer.getBegin();
710 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
711 FlatNop nopend=new FlatNop();
712 FlatBackEdge backedge=new FlatBackEdge();
714 initializer.getEnd().addNext(condition.getBegin());
715 body.getEnd().addNext(update.getBegin());
716 update.getEnd().addNext(backedge);
717 backedge.addNext(condition.getBegin());
718 condition.getEnd().addNext(fcb);
719 fcb.addFalseNext(nopend);
720 fcb.addTrueNext(body.getBegin());
721 return new NodePair(begin,nopend);
722 } else if (ln.getType()==LoopNode.WHILELOOP) {
723 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
724 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
725 NodePair body=flattenBlockNode(ln.getBody());
726 FlatNode begin=condition.getBegin();
727 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
728 FlatNop nopend=new FlatNop();
729 FlatBackEdge backedge=new FlatBackEdge();
731 body.getEnd().addNext(backedge);
732 backedge.addNext(condition.getBegin());
734 condition.getEnd().addNext(fcb);
735 fcb.addFalseNext(nopend);
736 fcb.addTrueNext(body.getBegin());
737 return new NodePair(begin,nopend);
738 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
739 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
740 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
741 NodePair body=flattenBlockNode(ln.getBody());
742 FlatNode begin=body.getBegin();
743 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
744 FlatNop nopend=new FlatNop();
745 FlatBackEdge backedge=new FlatBackEdge();
747 body.getEnd().addNext(condition.getBegin());
748 condition.getEnd().addNext(fcb);
749 fcb.addFalseNext(nopend);
750 fcb.addTrueNext(backedge);
751 backedge.addNext(body.getBegin());
752 return new NodePair(begin,nopend);
753 } else throw new Error();
756 private NodePair flattenReturnNode(ReturnNode rntree) {
757 TempDescriptor retval=null;
759 if (rntree.getReturnExpression()!=null) {
760 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
761 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
764 FlatReturnNode rnflat=new FlatReturnNode(retval);
766 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
767 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
768 TempDescriptor thistd=getTempforVar(currmd.getThis());
769 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
775 cond.getEnd().addNext(ln);
776 return new NodePair(cond.getBegin(),rnflat);
778 return new NodePair(ln,rnflat);
781 private NodePair flattenTaskExitNode(TaskExitNode ten) {
782 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
783 updateFlagActionNode(ffan, ten.getFlagEffects());
784 NodePair fcn=flattenConstraintCheck(ten.getChecks());
785 ffan.addNext(fcn.getBegin());
786 FlatReturnNode rnflat=new FlatReturnNode(null);
787 fcn.getEnd().addNext(rnflat);
788 return new NodePair(ffan, rnflat);
791 private NodePair flattenConstraintCheck(Vector ccs) {
792 FlatNode begin=new FlatNop();
794 return new NodePair(begin,begin);
796 for(int i=0;i<ccs.size();i++) {
797 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
798 /* Flatten the arguments */
799 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
800 String[] vars=new String[cc.numArgs()];
801 for(int j=0;j<cc.numArgs();j++) {
802 ExpressionNode en=cc.getArg(j);
803 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
805 vars[j]=cc.getVar(j);
806 NodePair np=flattenExpressionNode(en, td);
807 last.addNext(np.getBegin());
811 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
815 return new NodePair(begin,last);
818 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
819 return flattenBlockNode(sbn.getBlockNode());
822 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
824 case Kind.BlockExpressionNode:
825 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
827 case Kind.DeclarationNode:
828 return flattenDeclarationNode((DeclarationNode)bsn);
830 case Kind.IfStatementNode:
831 return flattenIfStatementNode((IfStatementNode)bsn);
834 return flattenLoopNode((LoopNode)bsn);
836 case Kind.ReturnNode:
837 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
839 case Kind.TaskExitNode:
840 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
842 case Kind.SubBlockNode:
843 return flattenSubBlockNode((SubBlockNode)bsn);