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);
42 FlatMethod fm=new FlatMethod(td, ffan);
44 for(int i=0;i<td.numParameters();i++) {
45 VarDescriptor paramvd=td.getParameter(i);
46 fm.addParameterTemp(getTempforVar(paramvd));
47 //Don't need to get temps for tag variables
48 //If they are used, this will be done automatically
49 //If they aren't we won't pass them in
52 /* Flatten Vector of Flag Effects */
53 Vector flags=td.getFlagEffects();
54 updateFlagActionNode(ffan,flags);
56 state.addFlatCode(td,fm);
60 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
61 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
62 if (flags==null) // Do nothing if the flag effects vector is empty
65 for(int i=0;i<flags.size();i++) {
66 FlagEffects fes=(FlagEffects)flags.get(i);
67 TempDescriptor flagtemp=getTempforVar(fes.getVar());
69 for(int j=0;j<fes.numEffects();j++) {
70 FlagEffect fe=fes.getEffect(j);
71 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
74 for(int j=0;j<fes.numTagEffects();j++) {
75 TagEffect te=fes.getTagEffect(j);
76 TempDescriptor tagtemp=getTempforVar(te.getTag());
78 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
83 private void flattenClass(ClassDescriptor cn) {
84 Iterator methodit=cn.getMethods();
85 while(methodit.hasNext()) {
86 currmd=(MethodDescriptor)methodit.next();
87 BlockNode bn=state.getMethodBody(currmd);
88 NodePair np=flattenBlockNode(bn);
89 FlatNode fn=np.getBegin();
90 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
91 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
92 TempDescriptor thistd=getTempforVar(currmd.getThis());
93 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
96 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
97 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
98 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
99 np.getEnd().addNext(fcunlock);
103 FlatMethod fm=new FlatMethod(currmd, fn);
104 if (!currmd.isStatic())
105 fm.addParameterTemp(getTempforParam(currmd.getThis()));
106 for(int i=0;i<currmd.numParameters();i++) {
107 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
109 state.addFlatCode(currmd,fm);
113 private NodePair flattenBlockNode(BlockNode bn) {
116 for(int i=0;i<bn.size();i++) {
117 NodePair np=flattenBlockStatementNode(bn.get(i));
118 FlatNode np_begin=np.getBegin();
119 FlatNode np_end=np.getEnd();
126 end.addNext(np_begin);
131 end=begin=new FlatNop();
133 return new NodePair(begin,end);
136 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
137 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
138 return flattenExpressionNode(en.getExpression(),tmp);
141 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
142 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
143 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
144 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
145 np.getEnd().addNext(fcn);
146 return new NodePair(np.getBegin(),fcn);
149 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
150 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
151 return new NodePair(fln,fln);
154 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
155 TypeDescriptor td=con.getType();
157 FlatNew fn=new FlatNew(td, out_temp);
158 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
160 if (td.getClassDesc().hasFlags()) {
161 // if (con.getFlagEffects()!=null) {
162 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
163 FlagEffects fes=con.getFlagEffects();
164 TempDescriptor flagtemp=out_temp;
166 for(int j=0;j<fes.numEffects();j++) {
167 FlagEffect fe=fes.getEffect(j);
168 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
170 for(int j=0;j<fes.numTagEffects();j++) {
171 TagEffect te=fes.getTagEffect(j);
172 TempDescriptor tagtemp=getTempforVar(te.getTag());
174 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
177 ffan.addFlagAction(flagtemp, null, false);
183 for(int i=0;i<con.numArgs();i++) {
184 ExpressionNode en=con.getArg(i);
185 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
187 NodePair np=flattenExpressionNode(en, tmp);
188 last.addNext(np.getBegin());
191 MethodDescriptor md=con.getConstructor();
192 //Call to constructor
193 FlatCall fc=new FlatCall(md, null, out_temp, temps);
196 return new NodePair(fn,last);
200 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
201 for (int i=0;i<con.numArgs();i++) {
202 ExpressionNode en=con.getArg(i);
203 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
205 NodePair np=flattenExpressionNode(en, tmp);
209 last.addNext(np.getBegin());
212 TempDescriptor tmp2=(i==0)?
214 TempDescriptor.tempFactory("arg",en.getType());
216 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
218 if (temps.length>1) {
219 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
220 fn.addNext(np.getBegin());
221 return new NodePair(first,np.getEnd());
223 return new NodePair(first, fn);
227 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
228 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
229 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
230 FlatNop fnop=new FlatNop();//last node
233 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
235 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
237 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
239 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
240 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
242 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
243 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
244 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
246 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
252 fcb.addFalseNext(fnop);
254 //Recursive call here
255 if ((i+2)<temparray.length) {
256 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
257 fsen.addNext(np2.getBegin());
258 np2.getEnd().addNext(fon);
263 return new NodePair(fln, fnop);
266 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
267 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
270 TempDescriptor thisarg=null;
272 if (min.getExpression()!=null) {
273 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
274 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
280 for(int i=0;i<min.numArgs();i++) {
281 ExpressionNode en=min.getArg(i);
282 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
284 NodePair np=flattenExpressionNode(en, td);
288 last.addNext(np.getBegin());
292 MethodDescriptor md=min.getMethod();
294 //Call to constructor
297 if(md.getReturnType()==null||md.getReturnType().isVoid())
298 fc=new FlatCall(md, null, thisarg, temps);
300 fc=new FlatCall(md, out_temp, thisarg, temps);
305 return new NodePair(first,fc);
308 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
309 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
310 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
311 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
312 npe.getEnd().addNext(fn);
313 return new NodePair(npe.getBegin(),fn);
316 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
317 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
318 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
319 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
320 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
321 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
322 npe.getEnd().addNext(npi.getBegin());
323 npi.getEnd().addNext(fn);
324 return new NodePair(npe.getBegin(),fn);
327 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
329 // left side is variable
330 // left side is field
331 // left side is array
333 Operation base=an.getOperation().getBaseOp();
334 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
337 //rewrite the base operation
338 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
342 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
345 if (an.getSrc()!=null) {
346 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
347 first=np_src.getBegin();
348 last=np_src.getEnd();
350 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
355 if (an.getDest().kind()==Kind.FieldAccessNode) {
356 //We are assigning an object field
358 FieldAccessNode fan=(FieldAccessNode)an.getDest();
359 ExpressionNode en=fan.getExpression();
360 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
361 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
363 first=np_baseexp.getBegin();
365 last.addNext(np_baseexp.getBegin());
366 last=np_baseexp.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 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
377 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
383 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
387 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
391 return new NodePair(first, last);
392 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
393 //We are assigning an array element
396 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
397 ExpressionNode en=aan.getExpression();
398 ExpressionNode enindex=aan.getIndex();
399 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
400 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
401 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
402 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
404 first=np_baseexp.getBegin();
406 last.addNext(np_baseexp.getBegin());
407 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
408 last=np_indexexp.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 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
419 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
426 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
430 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
434 return new NodePair(first, last);
435 } else if (an.getDest().kind()==Kind.NameNode) {
436 //We could be assigning a field or variable
437 NameNode nn=(NameNode)an.getDest();
438 if (nn.getExpression()!=null) {
440 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
441 ExpressionNode en=fan.getExpression();
442 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
443 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
445 first=np_baseexp.getBegin();
447 last.addNext(np_baseexp.getBegin());
448 last=np_baseexp.getEnd();
450 //See if we need to perform an operation
452 //If it is a preinc we need to store the initial value
453 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
454 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
456 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
459 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
466 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
470 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
474 return new NodePair(first, last);
476 if (nn.getField()!=null) {
480 //See if we need to perform an operation
482 //If it is a preinc we need to store the initial value
483 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
484 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
486 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
493 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
499 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
507 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
511 return new NodePair(first, last);
514 //See if we need to perform an operation
517 //If it is a preinc we need to store the initial value
518 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
519 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
521 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
529 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
538 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
542 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
546 return new NodePair(first, last);
553 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
554 if (nn.getExpression()!=null) {
555 /* Hack - use subtree instead */
556 return flattenExpressionNode(nn.getExpression(),out_temp);
557 } else if (nn.getField()!=null) {
558 TempDescriptor tmp=getTempforVar(nn.getVar());
559 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
560 return new NodePair(ffn,ffn);
562 TempDescriptor tmp=getTempforVar(nn.getVar());
563 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
564 return new NodePair(fon,fon);
568 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
569 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
570 TempDescriptor temp_right=null;
572 Operation op=on.getOp();
573 /* We've moved this to assignment nodes
575 if (op.getOp()==Operation.POSTINC||
576 op.getOp()==Operation.POSTDEC||
577 op.getOp()==Operation.PREINC||
578 op.getOp()==Operation.PREDEC) {
579 LiteralNode ln=new LiteralNode("int",new Integer(1));
580 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
582 AssignmentNode an=new AssignmentNode(on.getLeft(),
583 new OpNode(on.getLeft(),ln,
584 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
586 if (op.getOp()==Operation.POSTINC||
587 op.getOp()==Operation.POSTDEC) {
588 //Can't do, this could have side effects
589 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
590 NodePair assign=flattenAssignmentNode(an,temp_left);
591 left.getEnd().addNext(assign.getBegin());
592 return new NodePair(left.getBegin(),assign.getEnd());
594 NodePair assign=flattenAssignmentNode(an,out_temp);
599 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
601 if (on.getRight()!=null) {
602 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
603 right=flattenExpressionNode(on.getRight(),temp_right);
605 FlatNop nop=new FlatNop();
606 right=new NodePair(nop,nop);
609 if (op.getOp()==Operation.LOGIC_OR) {
610 /* Need to do shortcircuiting */
611 FlatCondBranch fcb=new FlatCondBranch(temp_left);
612 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
613 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
614 FlatNop fnop=new FlatNop();
615 left.getEnd().addNext(fcb);
616 fcb.addFalseNext(right.getBegin());
617 right.getEnd().addNext(fon2);
619 fcb.addTrueNext(fon1);
621 return new NodePair(left.getBegin(), fnop);
622 } else if (op.getOp()==Operation.LOGIC_AND) {
623 /* Need to do shortcircuiting */
624 FlatCondBranch fcb=new FlatCondBranch(temp_left);
625 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
626 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
627 FlatNop fnop=new FlatNop();
628 left.getEnd().addNext(fcb);
629 fcb.addTrueNext(right.getBegin());
630 right.getEnd().addNext(fon2);
632 fcb.addFalseNext(fon1);
634 return new NodePair(left.getBegin(), fnop);
637 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
638 left.getEnd().addNext(right.getBegin());
639 right.getEnd().addNext(fon);
640 return new NodePair(left.getBegin(),fon);
643 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
645 case Kind.AssignmentNode:
646 return flattenAssignmentNode((AssignmentNode)en,out_temp);
648 return flattenCastNode((CastNode)en,out_temp);
649 case Kind.CreateObjectNode:
650 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
651 case Kind.FieldAccessNode:
652 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
653 case Kind.ArrayAccessNode:
654 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
655 case Kind.LiteralNode:
656 return flattenLiteralNode((LiteralNode)en,out_temp);
657 case Kind.MethodInvokeNode:
658 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
660 return flattenNameNode((NameNode)en,out_temp);
662 return flattenOpNode((OpNode)en,out_temp);
667 private NodePair flattenDeclarationNode(DeclarationNode dn) {
668 VarDescriptor vd=dn.getVarDescriptor();
669 TempDescriptor td=getTempforVar(vd);
670 if (dn.getExpression()!=null)
671 return flattenExpressionNode(dn.getExpression(),td);
673 FlatNop fn=new FlatNop();
674 return new NodePair(fn,fn);
678 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
679 TagVarDescriptor tvd=dn.getTagVarDescriptor();
680 TagDescriptor tag=tvd.getTag();
681 TempDescriptor tmp=getTempforVar(tvd);
682 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
683 return new NodePair(ftd,ftd);
686 private TempDescriptor getTempforParam(Descriptor d) {
687 if (temptovar.containsKey(d))
688 return (TempDescriptor)temptovar.get(d);
690 if (d instanceof VarDescriptor) {
691 VarDescriptor vd=(VarDescriptor)d;
692 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
693 temptovar.put(vd,td);
695 } else if (d instanceof TagVarDescriptor) {
696 TagVarDescriptor tvd=(TagVarDescriptor)d;
697 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(),tvd.getTag());
698 temptovar.put(tvd,td);
700 } else throw new Error("Unreconized Descriptor");
704 private TempDescriptor getTempforVar(Descriptor d) {
705 if (temptovar.containsKey(d))
706 return (TempDescriptor)temptovar.get(d);
708 if (d instanceof VarDescriptor) {
709 VarDescriptor vd=(VarDescriptor)d;
710 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
711 temptovar.put(vd,td);
713 } else if (d instanceof TagVarDescriptor) {
714 TagVarDescriptor tvd=(TagVarDescriptor)d;
715 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tvd.getTag());
716 temptovar.put(tvd,td);
718 } else throw new Error("Unrecognized Descriptor");
722 private NodePair flattenIfStatementNode(IfStatementNode isn) {
723 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
724 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
725 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
726 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
728 FlatNop nopend=new FlatNop();
730 if (isn.getFalseBlock()!=null)
731 false_np=flattenBlockNode(isn.getFalseBlock());
733 FlatNop nop=new FlatNop();
734 false_np=new NodePair(nop,nop);
737 cond.getEnd().addNext(fcb);
738 fcb.addTrueNext(true_np.getBegin());
739 fcb.addFalseNext(false_np.getBegin());
740 true_np.getEnd().addNext(nopend);
741 false_np.getEnd().addNext(nopend);
742 return new NodePair(cond.getBegin(), nopend);
745 private NodePair flattenLoopNode(LoopNode ln) {
746 if (ln.getType()==LoopNode.FORLOOP) {
747 NodePair initializer=flattenBlockNode(ln.getInitializer());
748 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
749 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
750 NodePair update=flattenBlockNode(ln.getUpdate());
751 NodePair body=flattenBlockNode(ln.getBody());
752 FlatNode begin=initializer.getBegin();
753 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
754 FlatNop nopend=new FlatNop();
755 FlatBackEdge backedge=new FlatBackEdge();
757 initializer.getEnd().addNext(condition.getBegin());
758 body.getEnd().addNext(update.getBegin());
759 update.getEnd().addNext(backedge);
760 backedge.addNext(condition.getBegin());
761 condition.getEnd().addNext(fcb);
762 fcb.addFalseNext(nopend);
763 fcb.addTrueNext(body.getBegin());
764 return new NodePair(begin,nopend);
765 } else if (ln.getType()==LoopNode.WHILELOOP) {
766 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
767 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
768 NodePair body=flattenBlockNode(ln.getBody());
769 FlatNode begin=condition.getBegin();
770 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
771 FlatNop nopend=new FlatNop();
772 FlatBackEdge backedge=new FlatBackEdge();
774 body.getEnd().addNext(backedge);
775 backedge.addNext(condition.getBegin());
777 condition.getEnd().addNext(fcb);
778 fcb.addFalseNext(nopend);
779 fcb.addTrueNext(body.getBegin());
780 return new NodePair(begin,nopend);
781 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
782 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
783 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
784 NodePair body=flattenBlockNode(ln.getBody());
785 FlatNode begin=body.getBegin();
786 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
787 FlatNop nopend=new FlatNop();
788 FlatBackEdge backedge=new FlatBackEdge();
790 body.getEnd().addNext(condition.getBegin());
791 condition.getEnd().addNext(fcb);
792 fcb.addFalseNext(nopend);
793 fcb.addTrueNext(backedge);
794 backedge.addNext(body.getBegin());
795 return new NodePair(begin,nopend);
796 } else throw new Error();
799 private NodePair flattenReturnNode(ReturnNode rntree) {
800 TempDescriptor retval=null;
802 if (rntree.getReturnExpression()!=null) {
803 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
804 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
807 FlatReturnNode rnflat=new FlatReturnNode(retval);
809 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
810 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
811 TempDescriptor thistd=getTempforVar(currmd.getThis());
812 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
818 cond.getEnd().addNext(ln);
819 return new NodePair(cond.getBegin(),rnflat);
821 return new NodePair(ln,rnflat);
824 private NodePair flattenTaskExitNode(TaskExitNode ten) {
825 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
826 updateFlagActionNode(ffan, ten.getFlagEffects());
827 NodePair fcn=flattenConstraintCheck(ten.getChecks());
828 ffan.addNext(fcn.getBegin());
829 FlatReturnNode rnflat=new FlatReturnNode(null);
830 fcn.getEnd().addNext(rnflat);
831 return new NodePair(ffan, rnflat);
834 private NodePair flattenConstraintCheck(Vector ccs) {
835 FlatNode begin=new FlatNop();
837 return new NodePair(begin,begin);
839 for(int i=0;i<ccs.size();i++) {
840 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
841 /* Flatten the arguments */
842 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
843 String[] vars=new String[cc.numArgs()];
844 for(int j=0;j<cc.numArgs();j++) {
845 ExpressionNode en=cc.getArg(j);
846 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
848 vars[j]=cc.getVar(j);
849 NodePair np=flattenExpressionNode(en, td);
850 last.addNext(np.getBegin());
854 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
858 return new NodePair(begin,last);
861 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
862 return flattenBlockNode(sbn.getBlockNode());
865 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
867 case Kind.BlockExpressionNode:
868 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
870 case Kind.DeclarationNode:
871 return flattenDeclarationNode((DeclarationNode)bsn);
873 case Kind.TagDeclarationNode:
874 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
876 case Kind.IfStatementNode:
877 return flattenIfStatementNode((IfStatementNode)bsn);
880 return flattenLoopNode((LoopNode)bsn);
882 case Kind.ReturnNode:
883 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
885 case Kind.TaskExitNode:
886 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
888 case Kind.SubBlockNode:
889 return flattenSubBlockNode((SubBlockNode)bsn);