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 NodePair np=flattenBlockNode(bn);
39 FlatNode fn=np.getBegin();
40 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
41 FlatReturnNode rnflat=new FlatReturnNode(null);
42 np.getEnd().addNext(rnflat);
45 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
48 FlatMethod fm=new FlatMethod(td);
51 Hashtable visitedset=new Hashtable();
53 for(int i=0;i<td.numParameters();i++) {
54 VarDescriptor paramvd=td.getParameter(i);
55 fm.addParameterTemp(getTempforVar(paramvd));
56 TagExpressionList tel=td.getTag(paramvd);
57 //BUG added next line to fix...to test feed in any task program
59 for(int j=0;j<tel.numTags();j++) {
60 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
61 TempDescriptor tagtmp=getTempforVar(tvd);
62 if (!visitedset.containsKey(tvd.getName())) {
63 visitedset.put(tvd.getName(),tvd.getTag());
64 fm.addTagTemp(tagtmp);
66 TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
67 if (!tmptd.equals(tvd.getTag()))
68 throw new Error("Two different tag types with same name as parameters to:"+td);
70 tel.setTemp(j, tagtmp);
74 /* Flatten Vector of Flag Effects */
75 Vector flags=td.getFlagEffects();
76 updateFlagActionNode(ffan,flags);
78 state.addFlatCode(td,fm);
82 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
83 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
84 if (flags==null) // Do nothing if the flag effects vector is empty
87 for(int i=0;i<flags.size();i++) {
88 FlagEffects fes=(FlagEffects)flags.get(i);
89 TempDescriptor flagtemp=getTempforVar(fes.getVar());
91 for(int j=0;j<fes.numEffects();j++) {
92 FlagEffect fe=fes.getEffect(j);
93 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
96 for(int j=0;j<fes.numTagEffects();j++) {
97 TagEffect te=fes.getTagEffect(j);
98 TempDescriptor tagtemp=getTempforVar(te.getTag());
100 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
105 private void flattenClass(ClassDescriptor cn) {
106 Iterator methodit=cn.getMethods();
107 while(methodit.hasNext()) {
108 currmd=(MethodDescriptor)methodit.next();
109 BlockNode bn=state.getMethodBody(currmd);
110 NodePair np=flattenBlockNode(bn);
111 FlatNode fn=np.getBegin();
112 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
113 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
114 TempDescriptor thistd=getTempforVar(currmd.getThis());
115 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
118 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
119 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
120 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
121 np.getEnd().addNext(fcunlock);
122 FlatReturnNode rnflat=new FlatReturnNode(null);
123 fcunlock.addNext(rnflat);
125 } else if (np.getEnd().kind()!=FKind.FlatReturnNode) {
126 FlatReturnNode rnflat=new FlatReturnNode(null);
127 np.getEnd().addNext(rnflat);
130 FlatMethod fm=new FlatMethod(currmd);
132 if (!currmd.isStatic())
133 fm.addParameterTemp(getTempforParam(currmd.getThis()));
134 for(int i=0;i<currmd.numParameters();i++) {
135 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
137 state.addFlatCode(currmd,fm);
141 private NodePair flattenBlockNode(BlockNode bn) {
144 for(int i=0;i<bn.size();i++) {
145 NodePair np=flattenBlockStatementNode(bn.get(i));
146 FlatNode np_begin=np.getBegin();
147 FlatNode np_end=np.getEnd();
154 end.addNext(np_begin);
159 end=begin=new FlatNop();
161 return new NodePair(begin,end);
164 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
165 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
166 return flattenExpressionNode(en.getExpression(),tmp);
169 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
170 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
171 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
172 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
173 np.getEnd().addNext(fcn);
174 return new NodePair(np.getBegin(),fcn);
177 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
178 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
179 return new NodePair(fln,fln);
182 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
183 TypeDescriptor td=con.getType();
185 FlatNew fn=new FlatNew(td, out_temp);
186 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
188 if (td.getClassDesc().hasFlags()) {
189 // if (con.getFlagEffects()!=null) {
190 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
191 FlagEffects fes=con.getFlagEffects();
192 TempDescriptor flagtemp=out_temp;
194 for(int j=0;j<fes.numEffects();j++) {
195 FlagEffect fe=fes.getEffect(j);
196 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
198 for(int j=0;j<fes.numTagEffects();j++) {
199 TagEffect te=fes.getTagEffect(j);
200 TempDescriptor tagtemp=getTempforVar(te.getTag());
202 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
205 ffan.addFlagAction(flagtemp, null, false);
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);
216 last.addNext(np.getBegin());
219 MethodDescriptor md=con.getConstructor();
220 //Call to constructor
221 FlatCall fc=new FlatCall(md, null, out_temp, temps);
224 return new NodePair(fn,last);
228 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
229 for (int i=0;i<con.numArgs();i++) {
230 ExpressionNode en=con.getArg(i);
231 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
233 NodePair np=flattenExpressionNode(en, tmp);
237 last.addNext(np.getBegin());
240 TempDescriptor tmp2=(i==0)?
242 TempDescriptor.tempFactory("arg",en.getType());
244 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
246 if (temps.length>1) {
247 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
248 fn.addNext(np.getBegin());
249 return new NodePair(first,np.getEnd());
251 return new NodePair(first, fn);
255 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
256 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
257 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
258 FlatNop fnop=new FlatNop();//last node
261 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
263 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
265 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
267 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
268 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
270 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
271 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
272 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
274 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
280 fcb.addFalseNext(fnop);
282 //Recursive call here
283 if ((i+2)<temparray.length) {
284 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
285 fsen.addNext(np2.getBegin());
286 np2.getEnd().addNext(fon);
291 return new NodePair(fln, fnop);
294 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
295 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
298 TempDescriptor thisarg=null;
300 if (min.getExpression()!=null) {
301 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
302 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
308 for(int i=0;i<min.numArgs();i++) {
309 ExpressionNode en=min.getArg(i);
310 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
312 NodePair np=flattenExpressionNode(en, td);
316 last.addNext(np.getBegin());
320 MethodDescriptor md=min.getMethod();
322 //Call to constructor
325 if(md.getReturnType()==null||md.getReturnType().isVoid())
326 fc=new FlatCall(md, null, thisarg, temps);
328 fc=new FlatCall(md, out_temp, thisarg, temps);
333 return new NodePair(first,fc);
336 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
337 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
338 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
339 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
340 npe.getEnd().addNext(fn);
341 return new NodePair(npe.getBegin(),fn);
344 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
345 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
346 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
347 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
348 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
349 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
350 npe.getEnd().addNext(npi.getBegin());
351 npi.getEnd().addNext(fn);
352 return new NodePair(npe.getBegin(),fn);
355 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
357 // left side is variable
358 // left side is field
359 // left side is array
361 Operation base=an.getOperation().getBaseOp();
362 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
365 //rewrite the base operation
366 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
370 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
373 if (an.getSrc()!=null) {
374 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
375 first=np_src.getBegin();
376 last=np_src.getEnd();
378 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
383 if (an.getDest().kind()==Kind.FieldAccessNode) {
384 //We are assigning an object field
386 FieldAccessNode fan=(FieldAccessNode)an.getDest();
387 ExpressionNode en=fan.getExpression();
388 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
389 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
391 first=np_baseexp.getBegin();
393 last.addNext(np_baseexp.getBegin());
394 last=np_baseexp.getEnd();
396 //See if we need to perform an operation
398 //If it is a preinc we need to store the initial value
399 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
400 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
402 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
405 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
411 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
415 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
419 return new NodePair(first, last);
420 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
421 //We are assigning an array element
424 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
425 ExpressionNode en=aan.getExpression();
426 ExpressionNode enindex=aan.getIndex();
427 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
428 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
429 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
430 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
432 first=np_baseexp.getBegin();
434 last.addNext(np_baseexp.getBegin());
435 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
436 last=np_indexexp.getEnd();
438 //See if we need to perform an operation
440 //If it is a preinc we need to store the initial value
441 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
442 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
444 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
447 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
454 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
458 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
462 return new NodePair(first, last);
463 } else if (an.getDest().kind()==Kind.NameNode) {
464 //We could be assigning a field or variable
465 NameNode nn=(NameNode)an.getDest();
466 if (nn.getExpression()!=null) {
468 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
469 ExpressionNode en=fan.getExpression();
470 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
471 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
473 first=np_baseexp.getBegin();
475 last.addNext(np_baseexp.getBegin());
476 last=np_baseexp.getEnd();
478 //See if we need to perform an operation
480 //If it is a preinc we need to store the initial value
481 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
482 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
484 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
487 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
494 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
498 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
502 return new NodePair(first, last);
504 if (nn.getField()!=null) {
508 //See if we need to perform an operation
510 //If it is a preinc we need to store the initial value
511 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
512 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
514 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
521 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
527 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
535 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
539 return new NodePair(first, last);
542 //See if we need to perform an operation
545 //If it is a preinc we need to store the initial value
546 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
547 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
549 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
557 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
566 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
570 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
574 return new NodePair(first, last);
581 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
582 if (nn.getExpression()!=null) {
583 /* Hack - use subtree instead */
584 return flattenExpressionNode(nn.getExpression(),out_temp);
585 } else if (nn.getField()!=null) {
586 TempDescriptor tmp=getTempforVar(nn.getVar());
587 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
588 return new NodePair(ffn,ffn);
590 TempDescriptor tmp=getTempforVar(nn.isTag()?nn.getTagVar():nn.getVar());
593 out_temp.setTag(tmp.getTag());
595 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
596 return new NodePair(fon,fon);
600 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
601 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
602 TempDescriptor temp_right=null;
604 Operation op=on.getOp();
605 /* We've moved this to assignment nodes
607 if (op.getOp()==Operation.POSTINC||
608 op.getOp()==Operation.POSTDEC||
609 op.getOp()==Operation.PREINC||
610 op.getOp()==Operation.PREDEC) {
611 LiteralNode ln=new LiteralNode("int",new Integer(1));
612 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
614 AssignmentNode an=new AssignmentNode(on.getLeft(),
615 new OpNode(on.getLeft(),ln,
616 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
618 if (op.getOp()==Operation.POSTINC||
619 op.getOp()==Operation.POSTDEC) {
620 //Can't do, this could have side effects
621 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
622 NodePair assign=flattenAssignmentNode(an,temp_left);
623 left.getEnd().addNext(assign.getBegin());
624 return new NodePair(left.getBegin(),assign.getEnd());
626 NodePair assign=flattenAssignmentNode(an,out_temp);
631 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
633 if (on.getRight()!=null) {
634 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
635 right=flattenExpressionNode(on.getRight(),temp_right);
637 FlatNop nop=new FlatNop();
638 right=new NodePair(nop,nop);
641 if (op.getOp()==Operation.LOGIC_OR) {
642 /* Need to do shortcircuiting */
643 FlatCondBranch fcb=new FlatCondBranch(temp_left);
644 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
645 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
646 FlatNop fnop=new FlatNop();
647 left.getEnd().addNext(fcb);
648 fcb.addFalseNext(right.getBegin());
649 right.getEnd().addNext(fon2);
651 fcb.addTrueNext(fon1);
653 return new NodePair(left.getBegin(), fnop);
654 } else if (op.getOp()==Operation.LOGIC_AND) {
655 /* Need to do shortcircuiting */
656 FlatCondBranch fcb=new FlatCondBranch(temp_left);
657 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
658 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
659 FlatNop fnop=new FlatNop();
660 left.getEnd().addNext(fcb);
661 fcb.addTrueNext(right.getBegin());
662 right.getEnd().addNext(fon2);
664 fcb.addFalseNext(fon1);
666 return new NodePair(left.getBegin(), fnop);
669 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
670 left.getEnd().addNext(right.getBegin());
671 right.getEnd().addNext(fon);
672 return new NodePair(left.getBegin(),fon);
675 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
677 case Kind.AssignmentNode:
678 return flattenAssignmentNode((AssignmentNode)en,out_temp);
680 return flattenCastNode((CastNode)en,out_temp);
681 case Kind.CreateObjectNode:
682 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
683 case Kind.FieldAccessNode:
684 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
685 case Kind.ArrayAccessNode:
686 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
687 case Kind.LiteralNode:
688 return flattenLiteralNode((LiteralNode)en,out_temp);
689 case Kind.MethodInvokeNode:
690 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
692 return flattenNameNode((NameNode)en,out_temp);
694 return flattenOpNode((OpNode)en,out_temp);
699 private NodePair flattenDeclarationNode(DeclarationNode dn) {
700 VarDescriptor vd=dn.getVarDescriptor();
701 TempDescriptor td=getTempforVar(vd);
702 if (dn.getExpression()!=null)
703 return flattenExpressionNode(dn.getExpression(),td);
705 FlatNop fn=new FlatNop();
706 return new NodePair(fn,fn);
710 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
711 TagVarDescriptor tvd=dn.getTagVarDescriptor();
712 TagDescriptor tag=tvd.getTag();
713 TempDescriptor tmp=getTempforVar(tvd);
714 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
715 return new NodePair(ftd,ftd);
718 private TempDescriptor getTempforParam(Descriptor d) {
719 if (temptovar.containsKey(d))
720 return (TempDescriptor)temptovar.get(d);
722 if (d instanceof VarDescriptor) {
723 VarDescriptor vd=(VarDescriptor)d;
724 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
725 temptovar.put(vd,td);
727 } else if (d instanceof TagVarDescriptor) {
728 TagVarDescriptor tvd=(TagVarDescriptor)d;
729 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
730 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
731 temptovar.put(tvd,td);
733 } else throw new Error("Unreconized Descriptor");
737 private TempDescriptor getTempforVar(Descriptor d) {
738 if (temptovar.containsKey(d))
739 return (TempDescriptor)temptovar.get(d);
741 if (d instanceof VarDescriptor) {
742 VarDescriptor vd=(VarDescriptor)d;
743 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
744 temptovar.put(vd,td);
746 } else if (d instanceof TagVarDescriptor) {
747 TagVarDescriptor tvd=(TagVarDescriptor)d;
748 //BUGFIX TAGTYPE - add next line, modify following
749 //line to tag this new type descriptor, modify
750 //TempDescriptor constructor & factory to set type
751 //using this Type To test, use any program with tags
752 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
753 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
754 temptovar.put(tvd,td);
756 } else throw new Error("Unrecognized Descriptor");
760 private NodePair flattenIfStatementNode(IfStatementNode isn) {
761 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
762 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
763 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
764 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
766 FlatNop nopend=new FlatNop();
768 if (isn.getFalseBlock()!=null)
769 false_np=flattenBlockNode(isn.getFalseBlock());
771 FlatNop nop=new FlatNop();
772 false_np=new NodePair(nop,nop);
775 cond.getEnd().addNext(fcb);
776 fcb.addTrueNext(true_np.getBegin());
777 fcb.addFalseNext(false_np.getBegin());
778 true_np.getEnd().addNext(nopend);
779 false_np.getEnd().addNext(nopend);
780 return new NodePair(cond.getBegin(), nopend);
783 private NodePair flattenLoopNode(LoopNode ln) {
784 if (ln.getType()==LoopNode.FORLOOP) {
785 NodePair initializer=flattenBlockNode(ln.getInitializer());
786 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
787 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
788 NodePair update=flattenBlockNode(ln.getUpdate());
789 NodePair body=flattenBlockNode(ln.getBody());
790 FlatNode begin=initializer.getBegin();
791 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
792 FlatNop nopend=new FlatNop();
793 FlatBackEdge backedge=new FlatBackEdge();
795 initializer.getEnd().addNext(condition.getBegin());
796 body.getEnd().addNext(update.getBegin());
797 update.getEnd().addNext(backedge);
798 backedge.addNext(condition.getBegin());
799 condition.getEnd().addNext(fcb);
800 fcb.addFalseNext(nopend);
801 fcb.addTrueNext(body.getBegin());
802 return new NodePair(begin,nopend);
803 } else if (ln.getType()==LoopNode.WHILELOOP) {
804 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
805 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
806 NodePair body=flattenBlockNode(ln.getBody());
807 FlatNode begin=condition.getBegin();
808 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
809 FlatNop nopend=new FlatNop();
810 FlatBackEdge backedge=new FlatBackEdge();
812 body.getEnd().addNext(backedge);
813 backedge.addNext(condition.getBegin());
815 condition.getEnd().addNext(fcb);
816 fcb.addFalseNext(nopend);
817 fcb.addTrueNext(body.getBegin());
818 return new NodePair(begin,nopend);
819 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
820 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
821 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
822 NodePair body=flattenBlockNode(ln.getBody());
823 FlatNode begin=body.getBegin();
824 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
825 FlatNop nopend=new FlatNop();
826 FlatBackEdge backedge=new FlatBackEdge();
828 body.getEnd().addNext(condition.getBegin());
829 condition.getEnd().addNext(fcb);
830 fcb.addFalseNext(nopend);
831 fcb.addTrueNext(backedge);
832 backedge.addNext(body.getBegin());
833 return new NodePair(begin,nopend);
834 } else throw new Error();
837 private NodePair flattenReturnNode(ReturnNode rntree) {
838 TempDescriptor retval=null;
840 if (rntree.getReturnExpression()!=null) {
841 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
842 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
845 FlatReturnNode rnflat=new FlatReturnNode(retval);
847 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
848 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
849 TempDescriptor thistd=getTempforVar(currmd.getThis());
850 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
856 cond.getEnd().addNext(ln);
857 return new NodePair(cond.getBegin(),rnflat);
859 return new NodePair(ln,rnflat);
862 private NodePair flattenTaskExitNode(TaskExitNode ten) {
863 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
864 updateFlagActionNode(ffan, ten.getFlagEffects());
865 NodePair fcn=flattenConstraintCheck(ten.getChecks());
866 ffan.addNext(fcn.getBegin());
867 FlatReturnNode rnflat=new FlatReturnNode(null);
868 fcn.getEnd().addNext(rnflat);
869 return new NodePair(ffan, rnflat);
872 private NodePair flattenConstraintCheck(Vector ccs) {
873 FlatNode begin=new FlatNop();
875 return new NodePair(begin,begin);
877 for(int i=0;i<ccs.size();i++) {
878 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
879 /* Flatten the arguments */
880 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
881 String[] vars=new String[cc.numArgs()];
882 for(int j=0;j<cc.numArgs();j++) {
883 ExpressionNode en=cc.getArg(j);
884 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
886 vars[j]=cc.getVar(j);
887 NodePair np=flattenExpressionNode(en, td);
888 last.addNext(np.getBegin());
892 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
896 return new NodePair(begin,last);
899 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
900 return flattenBlockNode(sbn.getBlockNode());
903 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
905 case Kind.BlockExpressionNode:
906 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
908 case Kind.DeclarationNode:
909 return flattenDeclarationNode((DeclarationNode)bsn);
911 case Kind.TagDeclarationNode:
912 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
914 case Kind.IfStatementNode:
915 return flattenIfStatementNode((IfStatementNode)bsn);
918 return flattenLoopNode((LoopNode)bsn);
920 case Kind.ReturnNode:
921 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
923 case Kind.TaskExitNode:
924 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
926 case Kind.SubBlockNode:
927 return flattenSubBlockNode((SubBlockNode)bsn);