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 TagExpressionList tel=td.getTag(paramvd);
48 HashSet visitedset=new HashSet();
49 //BUG added next line to fix...to test feed in any task program
51 for(int j=0;j<tel.numTags();j++) {
52 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
53 if (!visitedset.contains(tvd)) {
55 TempDescriptor tagtmp=getTempforVar(tvd);
56 fm.addTagTemp(tagtmp);
57 tel.setTemp(j, tagtmp);
62 /* Flatten Vector of Flag Effects */
63 Vector flags=td.getFlagEffects();
64 updateFlagActionNode(ffan,flags);
66 state.addFlatCode(td,fm);
70 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
71 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
72 if (flags==null) // Do nothing if the flag effects vector is empty
75 for(int i=0;i<flags.size();i++) {
76 FlagEffects fes=(FlagEffects)flags.get(i);
77 TempDescriptor flagtemp=getTempforVar(fes.getVar());
79 for(int j=0;j<fes.numEffects();j++) {
80 FlagEffect fe=fes.getEffect(j);
81 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
84 for(int j=0;j<fes.numTagEffects();j++) {
85 TagEffect te=fes.getTagEffect(j);
86 TempDescriptor tagtemp=getTempforVar(te.getTag());
88 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
93 private void flattenClass(ClassDescriptor cn) {
94 Iterator methodit=cn.getMethods();
95 while(methodit.hasNext()) {
96 currmd=(MethodDescriptor)methodit.next();
97 BlockNode bn=state.getMethodBody(currmd);
98 NodePair np=flattenBlockNode(bn);
99 FlatNode fn=np.getBegin();
100 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
101 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
102 TempDescriptor thistd=getTempforVar(currmd.getThis());
103 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
106 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
107 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
108 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
109 np.getEnd().addNext(fcunlock);
113 FlatMethod fm=new FlatMethod(currmd, fn);
114 if (!currmd.isStatic())
115 fm.addParameterTemp(getTempforParam(currmd.getThis()));
116 for(int i=0;i<currmd.numParameters();i++) {
117 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
119 state.addFlatCode(currmd,fm);
123 private NodePair flattenBlockNode(BlockNode bn) {
126 for(int i=0;i<bn.size();i++) {
127 NodePair np=flattenBlockStatementNode(bn.get(i));
128 FlatNode np_begin=np.getBegin();
129 FlatNode np_end=np.getEnd();
136 end.addNext(np_begin);
141 end=begin=new FlatNop();
143 return new NodePair(begin,end);
146 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
147 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
148 return flattenExpressionNode(en.getExpression(),tmp);
151 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
152 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
153 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
154 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
155 np.getEnd().addNext(fcn);
156 return new NodePair(np.getBegin(),fcn);
159 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
160 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
161 return new NodePair(fln,fln);
164 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
165 TypeDescriptor td=con.getType();
167 FlatNew fn=new FlatNew(td, out_temp);
168 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
170 if (td.getClassDesc().hasFlags()) {
171 // if (con.getFlagEffects()!=null) {
172 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
173 FlagEffects fes=con.getFlagEffects();
174 TempDescriptor flagtemp=out_temp;
176 for(int j=0;j<fes.numEffects();j++) {
177 FlagEffect fe=fes.getEffect(j);
178 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
180 for(int j=0;j<fes.numTagEffects();j++) {
181 TagEffect te=fes.getTagEffect(j);
182 TempDescriptor tagtemp=getTempforVar(te.getTag());
184 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
187 ffan.addFlagAction(flagtemp, null, false);
193 for(int i=0;i<con.numArgs();i++) {
194 ExpressionNode en=con.getArg(i);
195 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
197 NodePair np=flattenExpressionNode(en, tmp);
198 last.addNext(np.getBegin());
201 MethodDescriptor md=con.getConstructor();
202 //Call to constructor
203 FlatCall fc=new FlatCall(md, null, out_temp, temps);
206 return new NodePair(fn,last);
210 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
211 for (int i=0;i<con.numArgs();i++) {
212 ExpressionNode en=con.getArg(i);
213 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
215 NodePair np=flattenExpressionNode(en, tmp);
219 last.addNext(np.getBegin());
222 TempDescriptor tmp2=(i==0)?
224 TempDescriptor.tempFactory("arg",en.getType());
226 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
228 if (temps.length>1) {
229 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
230 fn.addNext(np.getBegin());
231 return new NodePair(first,np.getEnd());
233 return new NodePair(first, fn);
237 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
238 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
239 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
240 FlatNop fnop=new FlatNop();//last node
243 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
245 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
247 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
249 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
250 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
252 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
253 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
254 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
256 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
262 fcb.addFalseNext(fnop);
264 //Recursive call here
265 if ((i+2)<temparray.length) {
266 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
267 fsen.addNext(np2.getBegin());
268 np2.getEnd().addNext(fon);
273 return new NodePair(fln, fnop);
276 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
277 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
280 TempDescriptor thisarg=null;
282 if (min.getExpression()!=null) {
283 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
284 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
290 for(int i=0;i<min.numArgs();i++) {
291 ExpressionNode en=min.getArg(i);
292 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
294 NodePair np=flattenExpressionNode(en, td);
298 last.addNext(np.getBegin());
302 MethodDescriptor md=min.getMethod();
304 //Call to constructor
307 if(md.getReturnType()==null||md.getReturnType().isVoid())
308 fc=new FlatCall(md, null, thisarg, temps);
310 fc=new FlatCall(md, out_temp, thisarg, temps);
315 return new NodePair(first,fc);
318 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
319 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
320 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
321 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
322 npe.getEnd().addNext(fn);
323 return new NodePair(npe.getBegin(),fn);
326 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
327 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
328 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
329 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
330 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
331 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
332 npe.getEnd().addNext(npi.getBegin());
333 npi.getEnd().addNext(fn);
334 return new NodePair(npe.getBegin(),fn);
337 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
339 // left side is variable
340 // left side is field
341 // left side is array
343 Operation base=an.getOperation().getBaseOp();
344 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
347 //rewrite the base operation
348 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
352 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
355 if (an.getSrc()!=null) {
356 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
357 first=np_src.getBegin();
358 last=np_src.getEnd();
360 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
365 if (an.getDest().kind()==Kind.FieldAccessNode) {
366 //We are assigning an object field
368 FieldAccessNode fan=(FieldAccessNode)an.getDest();
369 ExpressionNode en=fan.getExpression();
370 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
371 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
373 first=np_baseexp.getBegin();
375 last.addNext(np_baseexp.getBegin());
376 last=np_baseexp.getEnd();
378 //See if we need to perform an operation
380 //If it is a preinc we need to store the initial value
381 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
382 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
384 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
387 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
393 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
397 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
401 return new NodePair(first, last);
402 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
403 //We are assigning an array element
406 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
407 ExpressionNode en=aan.getExpression();
408 ExpressionNode enindex=aan.getIndex();
409 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
410 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
411 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
412 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
414 first=np_baseexp.getBegin();
416 last.addNext(np_baseexp.getBegin());
417 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
418 last=np_indexexp.getEnd();
420 //See if we need to perform an operation
422 //If it is a preinc we need to store the initial value
423 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
424 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
426 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
429 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
436 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
440 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
444 return new NodePair(first, last);
445 } else if (an.getDest().kind()==Kind.NameNode) {
446 //We could be assigning a field or variable
447 NameNode nn=(NameNode)an.getDest();
448 if (nn.getExpression()!=null) {
450 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
451 ExpressionNode en=fan.getExpression();
452 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
453 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
455 first=np_baseexp.getBegin();
457 last.addNext(np_baseexp.getBegin());
458 last=np_baseexp.getEnd();
460 //See if we need to perform an operation
462 //If it is a preinc we need to store the initial value
463 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
464 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
466 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
469 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
476 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
480 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
484 return new NodePair(first, last);
486 if (nn.getField()!=null) {
490 //See if we need to perform an operation
492 //If it is a preinc we need to store the initial value
493 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
494 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
496 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
503 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
509 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
517 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
521 return new NodePair(first, last);
524 //See if we need to perform an operation
527 //If it is a preinc we need to store the initial value
528 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
529 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
531 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
539 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
548 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
552 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
556 return new NodePair(first, last);
563 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
564 if (nn.getExpression()!=null) {
565 /* Hack - use subtree instead */
566 return flattenExpressionNode(nn.getExpression(),out_temp);
567 } else if (nn.getField()!=null) {
568 TempDescriptor tmp=getTempforVar(nn.getVar());
569 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
570 return new NodePair(ffn,ffn);
572 TempDescriptor tmp=getTempforVar(nn.getVar());
573 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
574 return new NodePair(fon,fon);
578 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
579 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
580 TempDescriptor temp_right=null;
582 Operation op=on.getOp();
583 /* We've moved this to assignment nodes
585 if (op.getOp()==Operation.POSTINC||
586 op.getOp()==Operation.POSTDEC||
587 op.getOp()==Operation.PREINC||
588 op.getOp()==Operation.PREDEC) {
589 LiteralNode ln=new LiteralNode("int",new Integer(1));
590 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
592 AssignmentNode an=new AssignmentNode(on.getLeft(),
593 new OpNode(on.getLeft(),ln,
594 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
596 if (op.getOp()==Operation.POSTINC||
597 op.getOp()==Operation.POSTDEC) {
598 //Can't do, this could have side effects
599 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
600 NodePair assign=flattenAssignmentNode(an,temp_left);
601 left.getEnd().addNext(assign.getBegin());
602 return new NodePair(left.getBegin(),assign.getEnd());
604 NodePair assign=flattenAssignmentNode(an,out_temp);
609 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
611 if (on.getRight()!=null) {
612 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
613 right=flattenExpressionNode(on.getRight(),temp_right);
615 FlatNop nop=new FlatNop();
616 right=new NodePair(nop,nop);
619 if (op.getOp()==Operation.LOGIC_OR) {
620 /* Need to do shortcircuiting */
621 FlatCondBranch fcb=new FlatCondBranch(temp_left);
622 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
623 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
624 FlatNop fnop=new FlatNop();
625 left.getEnd().addNext(fcb);
626 fcb.addFalseNext(right.getBegin());
627 right.getEnd().addNext(fon2);
629 fcb.addTrueNext(fon1);
631 return new NodePair(left.getBegin(), fnop);
632 } else if (op.getOp()==Operation.LOGIC_AND) {
633 /* Need to do shortcircuiting */
634 FlatCondBranch fcb=new FlatCondBranch(temp_left);
635 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
636 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
637 FlatNop fnop=new FlatNop();
638 left.getEnd().addNext(fcb);
639 fcb.addTrueNext(right.getBegin());
640 right.getEnd().addNext(fon2);
642 fcb.addFalseNext(fon1);
644 return new NodePair(left.getBegin(), fnop);
647 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
648 left.getEnd().addNext(right.getBegin());
649 right.getEnd().addNext(fon);
650 return new NodePair(left.getBegin(),fon);
653 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
655 case Kind.AssignmentNode:
656 return flattenAssignmentNode((AssignmentNode)en,out_temp);
658 return flattenCastNode((CastNode)en,out_temp);
659 case Kind.CreateObjectNode:
660 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
661 case Kind.FieldAccessNode:
662 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
663 case Kind.ArrayAccessNode:
664 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
665 case Kind.LiteralNode:
666 return flattenLiteralNode((LiteralNode)en,out_temp);
667 case Kind.MethodInvokeNode:
668 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
670 return flattenNameNode((NameNode)en,out_temp);
672 return flattenOpNode((OpNode)en,out_temp);
677 private NodePair flattenDeclarationNode(DeclarationNode dn) {
678 VarDescriptor vd=dn.getVarDescriptor();
679 TempDescriptor td=getTempforVar(vd);
680 if (dn.getExpression()!=null)
681 return flattenExpressionNode(dn.getExpression(),td);
683 FlatNop fn=new FlatNop();
684 return new NodePair(fn,fn);
688 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
689 TagVarDescriptor tvd=dn.getTagVarDescriptor();
690 TagDescriptor tag=tvd.getTag();
691 TempDescriptor tmp=getTempforVar(tvd);
692 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
693 return new NodePair(ftd,ftd);
696 private TempDescriptor getTempforParam(Descriptor d) {
697 if (temptovar.containsKey(d))
698 return (TempDescriptor)temptovar.get(d);
700 if (d instanceof VarDescriptor) {
701 VarDescriptor vd=(VarDescriptor)d;
702 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
703 temptovar.put(vd,td);
705 } else if (d instanceof TagVarDescriptor) {
706 TagVarDescriptor tvd=(TagVarDescriptor)d;
707 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(),tvd.getTag());
708 temptovar.put(tvd,td);
710 } else throw new Error("Unreconized Descriptor");
714 private TempDescriptor getTempforVar(Descriptor d) {
715 if (temptovar.containsKey(d))
716 return (TempDescriptor)temptovar.get(d);
718 if (d instanceof VarDescriptor) {
719 VarDescriptor vd=(VarDescriptor)d;
720 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
721 temptovar.put(vd,td);
723 } else if (d instanceof TagVarDescriptor) {
724 TagVarDescriptor tvd=(TagVarDescriptor)d;
725 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tvd.getTag());
726 temptovar.put(tvd,td);
728 } else throw new Error("Unrecognized Descriptor");
732 private NodePair flattenIfStatementNode(IfStatementNode isn) {
733 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
734 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
735 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
736 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
738 FlatNop nopend=new FlatNop();
740 if (isn.getFalseBlock()!=null)
741 false_np=flattenBlockNode(isn.getFalseBlock());
743 FlatNop nop=new FlatNop();
744 false_np=new NodePair(nop,nop);
747 cond.getEnd().addNext(fcb);
748 fcb.addTrueNext(true_np.getBegin());
749 fcb.addFalseNext(false_np.getBegin());
750 true_np.getEnd().addNext(nopend);
751 false_np.getEnd().addNext(nopend);
752 return new NodePair(cond.getBegin(), nopend);
755 private NodePair flattenLoopNode(LoopNode ln) {
756 if (ln.getType()==LoopNode.FORLOOP) {
757 NodePair initializer=flattenBlockNode(ln.getInitializer());
758 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
759 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
760 NodePair update=flattenBlockNode(ln.getUpdate());
761 NodePair body=flattenBlockNode(ln.getBody());
762 FlatNode begin=initializer.getBegin();
763 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
764 FlatNop nopend=new FlatNop();
765 FlatBackEdge backedge=new FlatBackEdge();
767 initializer.getEnd().addNext(condition.getBegin());
768 body.getEnd().addNext(update.getBegin());
769 update.getEnd().addNext(backedge);
770 backedge.addNext(condition.getBegin());
771 condition.getEnd().addNext(fcb);
772 fcb.addFalseNext(nopend);
773 fcb.addTrueNext(body.getBegin());
774 return new NodePair(begin,nopend);
775 } else if (ln.getType()==LoopNode.WHILELOOP) {
776 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
777 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
778 NodePair body=flattenBlockNode(ln.getBody());
779 FlatNode begin=condition.getBegin();
780 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
781 FlatNop nopend=new FlatNop();
782 FlatBackEdge backedge=new FlatBackEdge();
784 body.getEnd().addNext(backedge);
785 backedge.addNext(condition.getBegin());
787 condition.getEnd().addNext(fcb);
788 fcb.addFalseNext(nopend);
789 fcb.addTrueNext(body.getBegin());
790 return new NodePair(begin,nopend);
791 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
792 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
793 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
794 NodePair body=flattenBlockNode(ln.getBody());
795 FlatNode begin=body.getBegin();
796 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
797 FlatNop nopend=new FlatNop();
798 FlatBackEdge backedge=new FlatBackEdge();
800 body.getEnd().addNext(condition.getBegin());
801 condition.getEnd().addNext(fcb);
802 fcb.addFalseNext(nopend);
803 fcb.addTrueNext(backedge);
804 backedge.addNext(body.getBegin());
805 return new NodePair(begin,nopend);
806 } else throw new Error();
809 private NodePair flattenReturnNode(ReturnNode rntree) {
810 TempDescriptor retval=null;
812 if (rntree.getReturnExpression()!=null) {
813 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
814 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
817 FlatReturnNode rnflat=new FlatReturnNode(retval);
819 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
820 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
821 TempDescriptor thistd=getTempforVar(currmd.getThis());
822 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
828 cond.getEnd().addNext(ln);
829 return new NodePair(cond.getBegin(),rnflat);
831 return new NodePair(ln,rnflat);
834 private NodePair flattenTaskExitNode(TaskExitNode ten) {
835 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
836 updateFlagActionNode(ffan, ten.getFlagEffects());
837 NodePair fcn=flattenConstraintCheck(ten.getChecks());
838 ffan.addNext(fcn.getBegin());
839 FlatReturnNode rnflat=new FlatReturnNode(null);
840 fcn.getEnd().addNext(rnflat);
841 return new NodePair(ffan, rnflat);
844 private NodePair flattenConstraintCheck(Vector ccs) {
845 FlatNode begin=new FlatNop();
847 return new NodePair(begin,begin);
849 for(int i=0;i<ccs.size();i++) {
850 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
851 /* Flatten the arguments */
852 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
853 String[] vars=new String[cc.numArgs()];
854 for(int j=0;j<cc.numArgs();j++) {
855 ExpressionNode en=cc.getArg(j);
856 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
858 vars[j]=cc.getVar(j);
859 NodePair np=flattenExpressionNode(en, td);
860 last.addNext(np.getBegin());
864 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
868 return new NodePair(begin,last);
871 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
872 return flattenBlockNode(sbn.getBlockNode());
875 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
877 case Kind.BlockExpressionNode:
878 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
880 case Kind.DeclarationNode:
881 return flattenDeclarationNode((DeclarationNode)bsn);
883 case Kind.TagDeclarationNode:
884 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
886 case Kind.IfStatementNode:
887 return flattenIfStatementNode((IfStatementNode)bsn);
890 return flattenLoopNode((LoopNode)bsn);
892 case Kind.ReturnNode:
893 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
895 case Kind.TaskExitNode:
896 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
898 case Kind.SubBlockNode:
899 return flattenSubBlockNode((SubBlockNode)bsn);