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()];
148 if (con.getFlagEffects()!=null) {
149 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
150 FlagEffects fes=con.getFlagEffects();
151 TempDescriptor flagtemp=out_temp;
152 for(int j=0;j<fes.numEffects();j++) {
153 FlagEffect fe=fes.getEffect(j);
154 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
160 for(int i=0;i<con.numArgs();i++) {
161 ExpressionNode en=con.getArg(i);
162 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
164 NodePair np=flattenExpressionNode(en, tmp);
165 last.addNext(np.getBegin());
168 MethodDescriptor md=con.getConstructor();
169 //Call to constructor
170 FlatCall fc=new FlatCall(md, null, out_temp, temps);
173 return new NodePair(fn,last);
177 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
178 for (int i=0;i<con.numArgs();i++) {
179 ExpressionNode en=con.getArg(i);
180 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
182 NodePair np=flattenExpressionNode(en, tmp);
186 last.addNext(np.getBegin());
189 TempDescriptor tmp2=(i==0)?
191 TempDescriptor.tempFactory("arg",en.getType());
193 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
195 if (temps.length>1) {
196 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
197 fn.addNext(np.getBegin());
198 return new NodePair(first,np.getEnd());
200 return new NodePair(first, fn);
204 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
205 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
206 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
207 FlatNop fnop=new FlatNop();//last node
210 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
212 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
214 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
216 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
217 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
219 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
220 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
221 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
223 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
229 fcb.addFalseNext(fnop);
231 //Recursive call here
232 if ((i+2)<temparray.length) {
233 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
234 fsen.addNext(np2.getBegin());
235 np2.getEnd().addNext(fon);
240 return new NodePair(fln, fnop);
243 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
244 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
247 TempDescriptor thisarg=null;
249 if (min.getExpression()!=null) {
250 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
251 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
257 for(int i=0;i<min.numArgs();i++) {
258 ExpressionNode en=min.getArg(i);
259 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
261 NodePair np=flattenExpressionNode(en, td);
265 last.addNext(np.getBegin());
269 MethodDescriptor md=min.getMethod();
271 //Call to constructor
274 if(md.getReturnType()==null||md.getReturnType().isVoid())
275 fc=new FlatCall(md, null, thisarg, temps);
277 fc=new FlatCall(md, out_temp, thisarg, temps);
282 return new NodePair(first,fc);
285 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
286 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
287 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
288 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
289 npe.getEnd().addNext(fn);
290 return new NodePair(npe.getBegin(),fn);
293 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
294 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
295 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
296 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
297 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
298 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
299 npe.getEnd().addNext(npi.getBegin());
300 npi.getEnd().addNext(fn);
301 return new NodePair(npe.getBegin(),fn);
304 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
306 // left side is variable
307 // left side is field
308 // left side is array
310 Operation base=an.getOperation().getBaseOp();
311 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
314 //rewrite the base operation
315 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
319 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
322 if (an.getSrc()!=null) {
323 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
324 first=np_src.getBegin();
325 last=np_src.getEnd();
327 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
332 if (an.getDest().kind()==Kind.FieldAccessNode) {
333 //We are assigning an object field
335 FieldAccessNode fan=(FieldAccessNode)an.getDest();
336 ExpressionNode en=fan.getExpression();
337 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
338 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
340 first=np_baseexp.getBegin();
342 last.addNext(np_baseexp.getBegin());
343 last=np_baseexp.getEnd();
345 //See if we need to perform an operation
347 //If it is a preinc we need to store the initial value
348 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
349 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
351 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
354 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
360 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
364 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
368 return new NodePair(first, last);
369 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
370 //We are assigning an array element
373 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
374 ExpressionNode en=aan.getExpression();
375 ExpressionNode enindex=aan.getIndex();
376 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
377 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
378 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
379 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
381 first=np_baseexp.getBegin();
383 last.addNext(np_baseexp.getBegin());
384 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
385 last=np_indexexp.getEnd();
387 //See if we need to perform an operation
389 //If it is a preinc we need to store the initial value
390 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
391 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
393 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
396 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
403 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
407 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
411 return new NodePair(first, last);
412 } else if (an.getDest().kind()==Kind.NameNode) {
413 //We could be assigning a field or variable
414 NameNode nn=(NameNode)an.getDest();
415 if (nn.getExpression()!=null) {
417 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
418 ExpressionNode en=fan.getExpression();
419 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
420 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
422 first=np_baseexp.getBegin();
424 last.addNext(np_baseexp.getBegin());
425 last=np_baseexp.getEnd();
427 //See if we need to perform an operation
429 //If it is a preinc we need to store the initial value
430 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
431 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
433 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
436 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
443 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
447 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
451 return new NodePair(first, last);
453 if (nn.getField()!=null) {
457 //See if we need to perform an operation
459 //If it is a preinc we need to store the initial value
460 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
461 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
463 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
470 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
476 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
484 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
488 return new NodePair(first, last);
491 //See if we need to perform an operation
494 //If it is a preinc we need to store the initial value
495 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
496 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
498 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
506 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
515 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
519 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
523 return new NodePair(first, last);
530 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
531 if (nn.getExpression()!=null) {
532 /* Hack - use subtree instead */
533 return flattenExpressionNode(nn.getExpression(),out_temp);
534 } else if (nn.getField()!=null) {
535 TempDescriptor tmp=getTempforVar(nn.getVar());
536 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
537 return new NodePair(ffn,ffn);
539 TempDescriptor tmp=getTempforVar(nn.getVar());
540 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
541 return new NodePair(fon,fon);
545 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
546 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
547 TempDescriptor temp_right=null;
549 Operation op=on.getOp();
550 /* We've moved this to assignment nodes
552 if (op.getOp()==Operation.POSTINC||
553 op.getOp()==Operation.POSTDEC||
554 op.getOp()==Operation.PREINC||
555 op.getOp()==Operation.PREDEC) {
556 LiteralNode ln=new LiteralNode("int",new Integer(1));
557 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
559 AssignmentNode an=new AssignmentNode(on.getLeft(),
560 new OpNode(on.getLeft(),ln,
561 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
563 if (op.getOp()==Operation.POSTINC||
564 op.getOp()==Operation.POSTDEC) {
565 //Can't do, this could have side effects
566 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
567 NodePair assign=flattenAssignmentNode(an,temp_left);
568 left.getEnd().addNext(assign.getBegin());
569 return new NodePair(left.getBegin(),assign.getEnd());
571 NodePair assign=flattenAssignmentNode(an,out_temp);
576 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
578 if (on.getRight()!=null) {
579 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
580 right=flattenExpressionNode(on.getRight(),temp_right);
582 FlatNop nop=new FlatNop();
583 right=new NodePair(nop,nop);
586 if (op.getOp()==Operation.LOGIC_OR) {
587 /* Need to do shortcircuiting */
588 FlatCondBranch fcb=new FlatCondBranch(temp_left);
589 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
590 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
591 FlatNop fnop=new FlatNop();
592 left.getEnd().addNext(fcb);
593 fcb.addFalseNext(right.getBegin());
594 right.getEnd().addNext(fon2);
596 fcb.addTrueNext(fon1);
598 return new NodePair(left.getBegin(), fnop);
599 } else if (op.getOp()==Operation.LOGIC_AND) {
600 /* Need to do shortcircuiting */
601 FlatCondBranch fcb=new FlatCondBranch(temp_left);
602 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
603 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
604 FlatNop fnop=new FlatNop();
605 left.getEnd().addNext(fcb);
606 fcb.addTrueNext(right.getBegin());
607 right.getEnd().addNext(fon2);
609 fcb.addFalseNext(fon1);
611 return new NodePair(left.getBegin(), fnop);
614 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
615 left.getEnd().addNext(right.getBegin());
616 right.getEnd().addNext(fon);
617 return new NodePair(left.getBegin(),fon);
620 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
622 case Kind.AssignmentNode:
623 return flattenAssignmentNode((AssignmentNode)en,out_temp);
625 return flattenCastNode((CastNode)en,out_temp);
626 case Kind.CreateObjectNode:
627 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
628 case Kind.FieldAccessNode:
629 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
630 case Kind.ArrayAccessNode:
631 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
632 case Kind.LiteralNode:
633 return flattenLiteralNode((LiteralNode)en,out_temp);
634 case Kind.MethodInvokeNode:
635 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
637 return flattenNameNode((NameNode)en,out_temp);
639 return flattenOpNode((OpNode)en,out_temp);
644 private NodePair flattenDeclarationNode(DeclarationNode dn) {
645 VarDescriptor vd=dn.getVarDescriptor();
646 TempDescriptor td=getTempforVar(vd);
647 if (dn.getExpression()!=null)
648 return flattenExpressionNode(dn.getExpression(),td);
650 FlatNop fn=new FlatNop();
651 return new NodePair(fn,fn);
655 private TempDescriptor getTempforParam(VarDescriptor vd) {
656 if (temptovar.containsKey(vd))
657 return (TempDescriptor)temptovar.get(vd);
659 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
660 temptovar.put(vd,td);
665 private TempDescriptor getTempforVar(VarDescriptor vd) {
666 if (temptovar.containsKey(vd))
667 return (TempDescriptor)temptovar.get(vd);
669 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
670 temptovar.put(vd,td);
675 private NodePair flattenIfStatementNode(IfStatementNode isn) {
676 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
677 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
678 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
679 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
681 FlatNop nopend=new FlatNop();
683 if (isn.getFalseBlock()!=null)
684 false_np=flattenBlockNode(isn.getFalseBlock());
686 FlatNop nop=new FlatNop();
687 false_np=new NodePair(nop,nop);
690 cond.getEnd().addNext(fcb);
691 fcb.addTrueNext(true_np.getBegin());
692 fcb.addFalseNext(false_np.getBegin());
693 true_np.getEnd().addNext(nopend);
694 false_np.getEnd().addNext(nopend);
695 return new NodePair(cond.getBegin(), nopend);
698 private NodePair flattenLoopNode(LoopNode ln) {
699 if (ln.getType()==LoopNode.FORLOOP) {
700 NodePair initializer=flattenBlockNode(ln.getInitializer());
701 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
702 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
703 NodePair update=flattenBlockNode(ln.getUpdate());
704 NodePair body=flattenBlockNode(ln.getBody());
705 FlatNode begin=initializer.getBegin();
706 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
707 FlatNop nopend=new FlatNop();
708 FlatBackEdge backedge=new FlatBackEdge();
710 initializer.getEnd().addNext(condition.getBegin());
711 body.getEnd().addNext(update.getBegin());
712 update.getEnd().addNext(backedge);
713 backedge.addNext(condition.getBegin());
714 condition.getEnd().addNext(fcb);
715 fcb.addFalseNext(nopend);
716 fcb.addTrueNext(body.getBegin());
717 return new NodePair(begin,nopend);
718 } else if (ln.getType()==LoopNode.WHILELOOP) {
719 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
720 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
721 NodePair body=flattenBlockNode(ln.getBody());
722 FlatNode begin=condition.getBegin();
723 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
724 FlatNop nopend=new FlatNop();
725 FlatBackEdge backedge=new FlatBackEdge();
727 body.getEnd().addNext(backedge);
728 backedge.addNext(condition.getBegin());
730 condition.getEnd().addNext(fcb);
731 fcb.addFalseNext(nopend);
732 fcb.addTrueNext(body.getBegin());
733 return new NodePair(begin,nopend);
734 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
735 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
736 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
737 NodePair body=flattenBlockNode(ln.getBody());
738 FlatNode begin=body.getBegin();
739 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
740 FlatNop nopend=new FlatNop();
741 FlatBackEdge backedge=new FlatBackEdge();
743 body.getEnd().addNext(condition.getBegin());
744 condition.getEnd().addNext(fcb);
745 fcb.addFalseNext(nopend);
746 fcb.addTrueNext(backedge);
747 backedge.addNext(body.getBegin());
748 return new NodePair(begin,nopend);
749 } else throw new Error();
752 private NodePair flattenReturnNode(ReturnNode rntree) {
753 TempDescriptor retval=null;
755 if (rntree.getReturnExpression()!=null) {
756 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
757 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
760 FlatReturnNode rnflat=new FlatReturnNode(retval);
762 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
763 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
764 TempDescriptor thistd=getTempforVar(currmd.getThis());
765 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
771 cond.getEnd().addNext(ln);
772 return new NodePair(cond.getBegin(),rnflat);
774 return new NodePair(ln,rnflat);
777 private NodePair flattenTaskExitNode(TaskExitNode ten) {
778 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
779 updateFlagActionNode(ffan, ten.getFlagEffects());
780 NodePair fcn=flattenConstraintCheck(ten.getChecks());
781 ffan.addNext(fcn.getBegin());
782 FlatReturnNode rnflat=new FlatReturnNode(null);
783 fcn.getEnd().addNext(rnflat);
784 return new NodePair(ffan, rnflat);
787 private NodePair flattenConstraintCheck(Vector ccs) {
788 FlatNode begin=new FlatNop();
790 return new NodePair(begin,begin);
792 for(int i=0;i<ccs.size();i++) {
793 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
794 /* Flatten the arguments */
795 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
796 String[] vars=new String[cc.numArgs()];
797 for(int j=0;j<cc.numArgs();j++) {
798 ExpressionNode en=cc.getArg(j);
799 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
801 vars[j]=cc.getVar(j);
802 NodePair np=flattenExpressionNode(en, td);
803 last.addNext(np.getBegin());
807 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
811 return new NodePair(begin,last);
814 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
815 return flattenBlockNode(sbn.getBlockNode());
818 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
820 case Kind.BlockExpressionNode:
821 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
823 case Kind.DeclarationNode:
824 return flattenDeclarationNode((DeclarationNode)bsn);
826 case Kind.IfStatementNode:
827 return flattenIfStatementNode((IfStatementNode)bsn);
830 return flattenLoopNode((LoopNode)bsn);
832 case Kind.ReturnNode:
833 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
835 case Kind.TaskExitNode:
836 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
838 case Kind.SubBlockNode:
839 return flattenSubBlockNode((SubBlockNode)bsn);