6 public class BuildFlat {
9 MethodDescriptor currmd;
16 public BuildFlat(State st, TypeUtil typeutil) {
18 temptovar=new Hashtable();
19 this.typeutil=typeutil;
20 this.breakset=new HashSet();
21 this.continueset=new HashSet();
24 public Hashtable getMap() {
28 public void buildFlat() {
29 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
31 ClassDescriptor cn=(ClassDescriptor)it.next();
35 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
36 while(task_it.hasNext()) {
37 TaskDescriptor td=(TaskDescriptor)task_it.next();
42 private void flattenTask(TaskDescriptor td) {
43 BlockNode bn=state.getMethodBody(td);
44 NodePair np=flattenBlockNode(bn);
45 FlatNode fn=np.getBegin();
47 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
48 FlatReturnNode rnflat=new FlatReturnNode(null);
49 np.getEnd().addNext(rnflat);
53 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
56 FlatMethod fm=new FlatMethod(td);
59 Hashtable visitedset=new Hashtable();
61 for(int i=0; i<td.numParameters(); i++) {
62 VarDescriptor paramvd=td.getParameter(i);
63 fm.addParameterTemp(getTempforVar(paramvd));
64 TagExpressionList tel=td.getTag(paramvd);
65 //BUG added next line to fix...to test feed in any task program
67 for(int j=0; j<tel.numTags(); j++) {
68 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
69 TempDescriptor tagtmp=getTempforVar(tvd);
70 if (!visitedset.containsKey(tvd.getName())) {
71 visitedset.put(tvd.getName(),tvd.getTag());
72 fm.addTagTemp(tagtmp);
74 TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
75 if (!tmptd.equals(tvd.getTag()))
76 throw new Error("Two different tag types with same name as parameters to:"+td);
78 tel.setTemp(j, tagtmp);
82 /* Flatten Vector of Flag Effects */
83 Vector flags=td.getFlagEffects();
84 updateFlagActionNode(ffan,flags);
86 state.addFlatCode(td,fm);
90 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
91 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
92 if (flags==null) // Do nothing if the flag effects vector is empty
95 for(int i=0; i<flags.size(); i++) {
96 FlagEffects fes=(FlagEffects)flags.get(i);
97 TempDescriptor flagtemp=getTempforVar(fes.getVar());
99 for(int j=0; j<fes.numEffects(); j++) {
100 FlagEffect fe=fes.getEffect(j);
101 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
104 for(int j=0; j<fes.numTagEffects(); j++) {
105 TagEffect te=fes.getTagEffect(j);
106 TempDescriptor tagtemp=getTempforVar(te.getTag());
108 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
113 FlatAtomicEnterNode curran=null;
115 private void flattenClass(ClassDescriptor cn) {
116 Iterator methodit=cn.getMethods();
117 while(methodit.hasNext()) {
119 currmd=(MethodDescriptor)methodit.next();
120 BlockNode bn=state.getMethodBody(currmd);
122 if (state.DSM&&currmd.getModifiers().isAtomic()) {
123 curran=new FlatAtomicEnterNode();
126 NodePair np=flattenBlockNode(bn);
127 FlatNode fn=np.getBegin();
128 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
129 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
130 TempDescriptor thistd=getTempforVar(currmd.getThis());
131 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
134 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
135 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
136 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
137 np.getEnd().addNext(fcunlock);
138 FlatReturnNode rnflat=new FlatReturnNode(null);
139 fcunlock.addNext(rnflat);
142 } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
145 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
146 FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
147 np.getEnd().addNext(aen);
148 FlatReturnNode rnflat=new FlatReturnNode(null);
152 } else if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
153 FlatReturnNode rnflat=new FlatReturnNode(null);
154 np.getEnd().addNext(rnflat);
158 FlatMethod fm=new FlatMethod(currmd);
160 if (!currmd.isStatic())
161 fm.addParameterTemp(getTempforParam(currmd.getThis()));
162 for(int i=0; i<currmd.numParameters(); i++) {
163 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
165 state.addFlatCode(currmd,fm);
169 private NodePair flattenBlockNode(BlockNode bn) {
172 for(int i=0; i<bn.size(); i++) {
173 NodePair np=flattenBlockStatementNode(bn.get(i));
174 FlatNode np_begin=np.getBegin();
175 FlatNode np_end=np.getEnd();
182 end.addNext(np_begin);
184 return new NodePair(begin, null);
190 end=begin=new FlatNop();
192 return new NodePair(begin,end);
195 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
196 //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
197 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
198 return flattenExpressionNode(en.getExpression(),tmp);
201 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
202 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
203 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
204 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
205 np.getEnd().addNext(fcn);
206 return new NodePair(np.getBegin(),fcn);
209 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
210 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
211 return new NodePair(fln,fln);
214 private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
215 FlatOffsetNode fln = new FlatOffsetNode(ofn.getClassType(), ofn.getField(), out_temp);
216 return new NodePair(fln, fln);
219 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
220 TypeDescriptor td=con.getType();
222 FlatNew fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
223 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
226 for(int i=0; i<con.numArgs(); i++) {
227 ExpressionNode en=con.getArg(i);
228 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
230 NodePair np=flattenExpressionNode(en, tmp);
231 last.addNext(np.getBegin());
234 MethodDescriptor md=con.getConstructor();
235 //Call to constructor
236 FlatCall fc=new FlatCall(md, null, out_temp, temps);
239 if (td.getClassDesc().hasFlags()) {
240 // if (con.getFlagEffects()!=null) {
241 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
242 FlagEffects fes=con.getFlagEffects();
243 TempDescriptor flagtemp=out_temp;
245 for(int j=0; j<fes.numEffects(); j++) {
246 FlagEffect fe=fes.getEffect(j);
247 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
249 for(int j=0; j<fes.numTagEffects(); j++) {
250 TagEffect te=fes.getTagEffect(j);
251 TempDescriptor tagtemp=getTempforVar(te.getTag());
253 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
256 ffan.addFlagAction(flagtemp, null, false);
261 return new NodePair(fn,last);
265 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
266 for (int i=0; i<con.numArgs(); i++) {
267 ExpressionNode en=con.getArg(i);
268 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
270 NodePair np=flattenExpressionNode(en, tmp);
274 last.addNext(np.getBegin());
277 TempDescriptor tmp2=(i==0) ?
279 TempDescriptor.tempFactory("arg",en.getType());
281 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
283 if (temps.length>1) {
284 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
285 fn.addNext(np.getBegin());
286 return new NodePair(first,np.getEnd());
288 return new NodePair(first, fn);
292 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
293 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
294 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
295 FlatNop fnop=new FlatNop(); //last node
298 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
300 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
302 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
304 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
305 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
306 fcb.setTrueProb(State.TRUEPROB);
309 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
310 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1], isglobal);
311 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
313 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
319 fcb.addFalseNext(fnop);
321 //Recursive call here
322 if ((i+2)<temparray.length) {
323 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
324 fsen.addNext(np2.getBegin());
325 np2.getEnd().addNext(fon);
330 return new NodePair(fln, fnop);
333 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
334 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
337 TempDescriptor thisarg=null;
339 if (min.getExpression()!=null) {
340 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
341 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
347 for(int i=0; i<min.numArgs(); i++) {
348 ExpressionNode en=min.getArg(i);
349 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
351 NodePair np=flattenExpressionNode(en, td);
355 last.addNext(np.getBegin());
359 MethodDescriptor md=min.getMethod();
361 //Call to constructor
364 if(md.getReturnType()==null||md.getReturnType().isVoid())
365 fc=new FlatCall(md, null, thisarg, temps);
367 fc=new FlatCall(md, out_temp, thisarg, temps);
372 return new NodePair(first,fc);
375 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
376 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
377 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
378 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
379 npe.getEnd().addNext(fn);
380 return new NodePair(npe.getBegin(),fn);
383 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
384 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
385 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
386 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
387 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
388 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
389 npe.getEnd().addNext(npi.getBegin());
390 npi.getEnd().addNext(fn);
391 return new NodePair(npe.getBegin(),fn);
394 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
396 // left side is variable
397 // left side is field
398 // left side is array
400 Operation base=an.getOperation().getBaseOp();
401 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
404 //rewrite the base operation
405 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
409 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
412 if (an.getSrc()!=null) {
413 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
414 first=np_src.getBegin();
415 last=np_src.getEnd();
417 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
422 if (an.getDest().kind()==Kind.FieldAccessNode) {
423 //We are assigning an object field
425 FieldAccessNode fan=(FieldAccessNode)an.getDest();
426 ExpressionNode en=fan.getExpression();
427 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
428 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
430 first=np_baseexp.getBegin();
432 last.addNext(np_baseexp.getBegin());
433 last=np_baseexp.getEnd();
435 //See if we need to perform an operation
437 //If it is a preinc we need to store the initial value
438 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
439 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
440 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
444 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
445 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
446 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
447 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
452 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
459 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
463 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
467 return new NodePair(first, last);
468 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
469 //We are assigning an array element
472 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
473 ExpressionNode en=aan.getExpression();
474 ExpressionNode enindex=aan.getIndex();
475 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
476 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
477 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
478 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
480 first=np_baseexp.getBegin();
482 last.addNext(np_baseexp.getBegin());
483 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
484 last=np_indexexp.getEnd();
486 //See if we need to perform an operation
488 //If it is a preinc we need to store the initial value
489 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
490 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
492 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
496 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
497 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
498 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
499 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
506 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
514 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
518 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
522 return new NodePair(first, last);
523 } else if (an.getDest().kind()==Kind.NameNode) {
524 //We could be assigning a field or variable
525 NameNode nn=(NameNode)an.getDest();
528 if (nn.getExpression()!=null) {
530 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
531 ExpressionNode en=fan.getExpression();
532 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
533 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
535 first=np_baseexp.getBegin();
537 last.addNext(np_baseexp.getBegin());
538 last=np_baseexp.getEnd();
540 //See if we need to perform an operation
542 //If it is a preinc we need to store the initial value
543 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
544 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
546 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
551 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
552 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
553 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
554 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
559 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
567 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
571 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
575 return new NodePair(first, last);
577 if (nn.getField()!=null) {
581 //See if we need to perform an operation
583 //If it is a preinc we need to store the initial value
584 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
585 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
587 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
596 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
597 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
598 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
599 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
604 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
611 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
619 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
623 return new NodePair(first, last);
626 //See if we need to perform an operation
629 //If it is a preinc we need to store the initial value
630 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
631 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
633 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
642 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
643 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
644 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
645 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
653 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
663 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
668 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
672 return new NodePair(first, last);
680 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
681 if (nn.getExpression()!=null) {
682 /* Hack - use subtree instead */
683 return flattenExpressionNode(nn.getExpression(),out_temp);
684 } else if (nn.getField()!=null) {
685 TempDescriptor tmp=getTempforVar(nn.getVar());
686 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
687 return new NodePair(ffn,ffn);
689 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
692 out_temp.setTag(tmp.getTag());
694 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
695 return new NodePair(fon,fon);
699 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
700 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
701 TempDescriptor temp_right=null;
703 Operation op=on.getOp();
705 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
707 if (on.getRight()!=null) {
708 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
709 right=flattenExpressionNode(on.getRight(),temp_right);
711 FlatNop nop=new FlatNop();
712 right=new NodePair(nop,nop);
715 if (op.getOp()==Operation.LOGIC_OR) {
716 /* Need to do shortcircuiting */
717 FlatCondBranch fcb=new FlatCondBranch(temp_left);
718 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
719 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
720 FlatNop fnop=new FlatNop();
721 left.getEnd().addNext(fcb);
722 fcb.addFalseNext(right.getBegin());
723 right.getEnd().addNext(fon2);
725 fcb.addTrueNext(fon1);
727 return new NodePair(left.getBegin(), fnop);
728 } else if (op.getOp()==Operation.LOGIC_AND) {
729 /* Need to do shortcircuiting */
730 FlatCondBranch fcb=new FlatCondBranch(temp_left);
731 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
732 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
733 FlatNop fnop=new FlatNop();
734 left.getEnd().addNext(fcb);
735 fcb.addTrueNext(right.getBegin());
736 right.getEnd().addNext(fon2);
738 fcb.addFalseNext(fon1);
740 return new NodePair(left.getBegin(), fnop);
741 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
742 //We have a string concatenate
743 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
744 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
745 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
746 left.getEnd().addNext(right.getBegin());
747 right.getEnd().addNext(fc);
748 return new NodePair(left.getBegin(), fc);
751 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
752 left.getEnd().addNext(right.getBegin());
753 right.getEnd().addNext(fon);
754 return new NodePair(left.getBegin(),fon);
757 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
759 case Kind.AssignmentNode:
760 return flattenAssignmentNode((AssignmentNode)en,out_temp);
763 return flattenCastNode((CastNode)en,out_temp);
765 case Kind.CreateObjectNode:
766 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
768 case Kind.FieldAccessNode:
769 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
771 case Kind.ArrayAccessNode:
772 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
774 case Kind.LiteralNode:
775 return flattenLiteralNode((LiteralNode)en,out_temp);
777 case Kind.MethodInvokeNode:
778 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
781 return flattenNameNode((NameNode)en,out_temp);
784 return flattenOpNode((OpNode)en,out_temp);
786 case Kind.OffsetNode:
787 return flattenOffsetNode((OffsetNode)en,out_temp);
789 case Kind.TertiaryNode:
790 return flattenTertiaryNode((TertiaryNode)en,out_temp);
792 case Kind.InstanceOfNode:
793 return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
795 case Kind.ArrayInitializerNode:
796 return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
801 private NodePair flattenDeclarationNode(DeclarationNode dn) {
802 VarDescriptor vd=dn.getVarDescriptor();
803 TempDescriptor td=getTempforVar(vd);
804 if (dn.getExpression()!=null)
805 return flattenExpressionNode(dn.getExpression(),td);
807 FlatNop fn=new FlatNop();
808 return new NodePair(fn,fn);
812 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
813 TagVarDescriptor tvd=dn.getTagVarDescriptor();
814 TagDescriptor tag=tvd.getTag();
815 TempDescriptor tmp=getTempforVar(tvd);
816 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
817 return new NodePair(ftd,ftd);
820 private TempDescriptor getTempforParam(Descriptor d) {
821 if (temptovar.containsKey(d))
822 return (TempDescriptor)temptovar.get(d);
824 if (d instanceof VarDescriptor) {
825 VarDescriptor vd=(VarDescriptor)d;
826 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
827 temptovar.put(vd,td);
829 } else if (d instanceof TagVarDescriptor) {
830 TagVarDescriptor tvd=(TagVarDescriptor)d;
831 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
832 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
833 temptovar.put(tvd,td);
835 } else throw new Error("Unreconized Descriptor");
839 private TempDescriptor getTempforVar(Descriptor d) {
840 if (temptovar.containsKey(d))
841 return (TempDescriptor)temptovar.get(d);
843 if (d instanceof VarDescriptor) {
844 VarDescriptor vd=(VarDescriptor)d;
845 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
846 temptovar.put(vd,td);
848 } else if (d instanceof TagVarDescriptor) {
849 TagVarDescriptor tvd=(TagVarDescriptor)d;
850 //BUGFIX TAGTYPE - add next line, modify following
851 //line to tag this new type descriptor, modify
852 //TempDescriptor constructor & factory to set type
853 //using this Type To test, use any program with tags
854 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
855 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
856 temptovar.put(tvd,td);
858 } else throw new Error("Unrecognized Descriptor");
862 private NodePair flattenIfStatementNode(IfStatementNode isn) {
863 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
864 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
865 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
866 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
868 FlatNop nopend=new FlatNop();
870 if (isn.getFalseBlock()!=null)
871 false_np=flattenBlockNode(isn.getFalseBlock());
873 FlatNop nop=new FlatNop();
874 false_np=new NodePair(nop,nop);
877 cond.getEnd().addNext(fcb);
878 fcb.addTrueNext(true_np.getBegin());
879 fcb.addFalseNext(false_np.getBegin());
880 if (true_np.getEnd()!=null)
881 true_np.getEnd().addNext(nopend);
882 if (false_np.getEnd()!=null)
883 false_np.getEnd().addNext(nopend);
884 return new NodePair(cond.getBegin(), nopend);
887 private NodePair flattenLoopNode(LoopNode ln) {
888 HashSet oldbs=breakset;
889 HashSet oldcs=continueset;
890 breakset=new HashSet();
891 continueset=new HashSet();
893 if (ln.getType()==LoopNode.FORLOOP) {
894 NodePair initializer=flattenBlockNode(ln.getInitializer());
895 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
896 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
897 NodePair update=flattenBlockNode(ln.getUpdate());
898 NodePair body=flattenBlockNode(ln.getBody());
899 FlatNode begin=initializer.getBegin();
900 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
901 fcb.setTrueProb(State.TRUEPROB);
903 FlatNop nopend=new FlatNop();
904 FlatBackEdge backedge=new FlatBackEdge();
906 FlatNop nop2=new FlatNop();
907 initializer.getEnd().addNext(nop2);
908 nop2.addNext(condition.getBegin());
909 if (body.getEnd()!=null)
910 body.getEnd().addNext(update.getBegin());
911 update.getEnd().addNext(backedge);
912 backedge.addNext(condition.getBegin());
913 condition.getEnd().addNext(fcb);
914 fcb.addFalseNext(nopend);
915 fcb.addTrueNext(body.getBegin());
916 for(Iterator contit=continueset.iterator();contit.hasNext();) {
917 FlatNode fn=(FlatNode)contit.next();
919 fn.addNext(update.getBegin());
921 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
922 FlatNode fn=(FlatNode)breakit.next();
928 return new NodePair(begin,nopend);
929 } else if (ln.getType()==LoopNode.WHILELOOP) {
930 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
931 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
932 NodePair body=flattenBlockNode(ln.getBody());
933 FlatNode begin=condition.getBegin();
934 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
935 fcb.setTrueProb(State.TRUEPROB);
937 FlatNop nopend=new FlatNop();
938 FlatBackEdge backedge=new FlatBackEdge();
940 body.getEnd().addNext(backedge);
941 backedge.addNext(condition.getBegin());
943 condition.getEnd().addNext(fcb);
944 fcb.addFalseNext(nopend);
945 fcb.addTrueNext(body.getBegin());
947 for(Iterator contit=continueset.iterator();contit.hasNext();) {
948 FlatNode fn=(FlatNode)contit.next();
950 fn.addNext(backedge);
952 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
953 FlatNode fn=(FlatNode)breakit.next();
959 return new NodePair(begin,nopend);
960 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
961 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
962 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
963 NodePair body=flattenBlockNode(ln.getBody());
964 FlatNode begin=body.getBegin();
965 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
966 fcb.setTrueProb(State.TRUEPROB);
968 FlatNop nopend=new FlatNop();
969 FlatBackEdge backedge=new FlatBackEdge();
971 if (body.getEnd()!=null)
972 body.getEnd().addNext(condition.getBegin());
973 condition.getEnd().addNext(fcb);
974 fcb.addFalseNext(nopend);
975 fcb.addTrueNext(backedge);
976 backedge.addNext(body.getBegin());
978 for(Iterator contit=continueset.iterator();contit.hasNext();) {
979 FlatNode fn=(FlatNode)contit.next();
981 fn.addNext(condition.getBegin());
983 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
984 FlatNode fn=(FlatNode)breakit.next();
990 return new NodePair(begin,nopend);
991 } else throw new Error();
994 private NodePair flattenReturnNode(ReturnNode rntree) {
995 TempDescriptor retval=null;
997 if (rntree.getReturnExpression()!=null) {
998 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
999 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1002 FlatReturnNode rnflat=new FlatReturnNode(retval);
1005 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
1006 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1007 TempDescriptor thistd=getTempforVar(currmd.getThis());
1008 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
1012 if (state.DSM&&currmd.getModifiers().isAtomic()) {
1013 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1019 cond.getEnd().addNext(ln);
1020 return new NodePair(cond.getBegin(),null);
1022 return new NodePair(ln,null);
1025 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1026 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1027 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1028 updateFlagActionNode(ffan, ten.getFlagEffects());
1029 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1030 ffan.addNext(fcn.getBegin());
1031 FlatReturnNode rnflat=new FlatReturnNode(null);
1033 fcn.getEnd().addNext(rnflat);
1034 return new NodePair(ffan, null);
1037 private NodePair flattenConstraintCheck(Vector ccs) {
1038 FlatNode begin=new FlatNop();
1040 return new NodePair(begin,begin);
1041 FlatNode last=begin;
1042 for(int i=0; i<ccs.size(); i++) {
1043 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1044 /* Flatten the arguments */
1045 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1046 String[] vars=new String[cc.numArgs()];
1047 for(int j=0; j<cc.numArgs(); j++) {
1048 ExpressionNode en=cc.getArg(j);
1049 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1051 vars[j]=cc.getVar(j);
1052 NodePair np=flattenExpressionNode(en, td);
1053 last.addNext(np.getBegin());
1057 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1061 return new NodePair(begin,last);
1064 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1065 return flattenBlockNode(sbn.getBlockNode());
1068 private NodePair flattenAtomicNode(AtomicNode sbn) {
1069 NodePair np=flattenBlockNode(sbn.getBlockNode());
1070 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1071 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1072 faen.addNext(np.getBegin());
1073 np.getEnd().addNext(faexn);
1074 return new NodePair(faen, faexn);
1077 private NodePair flattenSESENode(SESENode sn) {
1078 if( sn.isStart() ) {
1079 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1080 sn.setFlatEnter(fsen);
1081 return new NodePair(fsen, fsen);
1084 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1085 sn.setFlatExit(fsexn);
1086 fsexn.setFlatEnter( sn.getStart().getFlatEnter() );
1087 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1088 return new NodePair(fsexn, fsexn);
1091 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1092 FlatNop fn=new FlatNop();
1096 continueset.add(fn);
1097 return new NodePair(fn,null);
1100 private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1101 TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1102 NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1103 FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1104 cond.getEnd().addNext(fion);
1105 return new NodePair(cond.getBegin(),fion);
1108 private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1110 TempDescriptor expr_temp=TempDescriptor.tempFactory("arry_init",ain.getType());
1112 // create a new array of size equal to the array initializer
1113 //FlatNode first=null;
1114 //FlatNode last=null;
1115 TempDescriptor[] temps=new TempDescriptor[ain.numVarInitializers()];
1117 for (int i=0; i<con.numArgs(); i++) {
1118 ExpressionNode en=con.getArg(i);
1119 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
1121 NodePair np=flattenExpressionNode(en, tmp);
1123 first=np.getBegin();
1125 last.addNext(np.getBegin());
1128 TempDescriptor tmp2=(i==0) ?
1130 TempDescriptor.tempFactory("arg",en.getType());
1132 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
1136 // assign each element of the new array to the flattened expression
1139 FlatOpNode fonAssignArray=new FlatOpNode(out_temp, newarry_temp, null, new Operation(Operation.ASSIGN));
1141 //return new NodePair( , fonAssignArray );
1143 System.out.println( "Array initializers not implemented yet." );
1148 private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1149 TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1150 TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1151 TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1153 NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1154 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1156 NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1157 FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1159 NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1160 FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1162 FlatNop nopend=new FlatNop();
1164 cond.getEnd().addNext(fcb);
1166 fcb.addTrueNext(trueExpr.getBegin());
1167 fcb.addFalseNext(falseExpr.getBegin());
1169 trueExpr.getEnd().addNext(fonT);
1170 fonT.addNext(nopend);
1172 falseExpr.getEnd().addNext(fonF);
1173 fonF.addNext(nopend);
1175 return new NodePair(cond.getBegin(), nopend);
1178 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1179 switch(bsn.kind()) {
1180 case Kind.BlockExpressionNode:
1181 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1183 case Kind.DeclarationNode:
1184 return flattenDeclarationNode((DeclarationNode)bsn);
1186 case Kind.TagDeclarationNode:
1187 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1189 case Kind.IfStatementNode:
1190 return flattenIfStatementNode((IfStatementNode)bsn);
1193 return flattenLoopNode((LoopNode)bsn);
1195 case Kind.ReturnNode:
1196 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1198 case Kind.TaskExitNode:
1199 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1201 case Kind.SubBlockNode:
1202 return flattenSubBlockNode((SubBlockNode)bsn);
1204 case Kind.AtomicNode:
1205 return flattenAtomicNode((AtomicNode)bsn);
1208 return flattenSESENode((SESENode)bsn);
1210 case Kind.ContinueBreakNode:
1211 return flattenContinueBreakNode((ContinueBreakNode)bsn);