6 public class BuildFlat {
9 MethodDescriptor currmd;
15 public BuildFlat(State st, TypeUtil typeutil) {
17 temptovar=new Hashtable();
18 this.typeutil=typeutil;
19 this.breakset=new HashSet();
20 this.continueset=new HashSet();
23 public Hashtable getMap() {
27 public void buildFlat() {
28 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
30 ClassDescriptor cn=(ClassDescriptor)it.next();
34 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
35 while(task_it.hasNext()) {
36 TaskDescriptor td=(TaskDescriptor)task_it.next();
41 private void flattenTask(TaskDescriptor td) {
42 BlockNode bn=state.getMethodBody(td);
43 NodePair np=flattenBlockNode(bn);
44 FlatNode fn=np.getBegin();
45 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
46 FlatReturnNode rnflat=new FlatReturnNode(null);
47 np.getEnd().addNext(rnflat);
50 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
53 FlatMethod fm=new FlatMethod(td);
56 Hashtable visitedset=new Hashtable();
58 for(int i=0; i<td.numParameters(); i++) {
59 VarDescriptor paramvd=td.getParameter(i);
60 fm.addParameterTemp(getTempforVar(paramvd));
61 TagExpressionList tel=td.getTag(paramvd);
62 //BUG added next line to fix...to test feed in any task program
64 for(int j=0; j<tel.numTags(); j++) {
65 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
66 TempDescriptor tagtmp=getTempforVar(tvd);
67 if (!visitedset.containsKey(tvd.getName())) {
68 visitedset.put(tvd.getName(),tvd.getTag());
69 fm.addTagTemp(tagtmp);
71 TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
72 if (!tmptd.equals(tvd.getTag()))
73 throw new Error("Two different tag types with same name as parameters to:"+td);
75 tel.setTemp(j, tagtmp);
79 /* Flatten Vector of Flag Effects */
80 Vector flags=td.getFlagEffects();
81 updateFlagActionNode(ffan,flags);
83 state.addFlatCode(td,fm);
87 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
88 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
89 if (flags==null) // Do nothing if the flag effects vector is empty
92 for(int i=0; i<flags.size(); i++) {
93 FlagEffects fes=(FlagEffects)flags.get(i);
94 TempDescriptor flagtemp=getTempforVar(fes.getVar());
96 for(int j=0; j<fes.numEffects(); j++) {
97 FlagEffect fe=fes.getEffect(j);
98 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
101 for(int j=0; j<fes.numTagEffects(); j++) {
102 TagEffect te=fes.getTagEffect(j);
103 TempDescriptor tagtemp=getTempforVar(te.getTag());
105 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
110 FlatAtomicEnterNode curran=null;
112 private void flattenClass(ClassDescriptor cn) {
113 Iterator methodit=cn.getMethods();
114 while(methodit.hasNext()) {
115 currmd=(MethodDescriptor)methodit.next();
116 BlockNode bn=state.getMethodBody(currmd);
118 if (state.DSM&&currmd.getModifiers().isAtomic()) {
119 curran=new FlatAtomicEnterNode();
122 NodePair np=flattenBlockNode(bn);
123 FlatNode fn=np.getBegin();
124 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
125 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
126 TempDescriptor thistd=getTempforVar(currmd.getThis());
127 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
130 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
131 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
132 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
133 np.getEnd().addNext(fcunlock);
134 FlatReturnNode rnflat=new FlatReturnNode(null);
135 fcunlock.addNext(rnflat);
137 } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
140 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
141 FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
142 np.getEnd().addNext(aen);
143 FlatReturnNode rnflat=new FlatReturnNode(null);
146 } else if (np.getEnd().kind()!=FKind.FlatReturnNode) {
147 FlatReturnNode rnflat=new FlatReturnNode(null);
148 np.getEnd().addNext(rnflat);
151 FlatMethod fm=new FlatMethod(currmd);
153 if (!currmd.isStatic())
154 fm.addParameterTemp(getTempforParam(currmd.getThis()));
155 for(int i=0; i<currmd.numParameters(); i++) {
156 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
158 state.addFlatCode(currmd,fm);
162 private NodePair flattenBlockNode(BlockNode bn) {
165 for(int i=0; i<bn.size(); i++) {
166 NodePair np=flattenBlockStatementNode(bn.get(i));
167 FlatNode np_begin=np.getBegin();
168 FlatNode np_end=np.getEnd();
175 end.addNext(np_begin);
177 return new NodePair(begin, null);
183 end=begin=new FlatNop();
185 return new NodePair(begin,end);
188 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
189 //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
190 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
191 return flattenExpressionNode(en.getExpression(),tmp);
194 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
195 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
196 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
197 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
198 np.getEnd().addNext(fcn);
199 return new NodePair(np.getBegin(),fcn);
202 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
203 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
204 return new NodePair(fln,fln);
207 private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
208 FlatLiteralNode fln = new FlatLiteralNode(ofn.getType(), ofn, out_temp);
209 return new NodePair(fln, fln);
210 /* Another possible approach
211 ClassDescriptor cd = ofn.getClassDesc();
212 FieldDescriptor fd = ofn.getField();
213 FlatOffsetNode fon = new FlatOffsetNode(fd, cd, out_temp);
214 TypeDescriptor type=new TypeDescriptor(ofn.getType());
215 System.out.println("TempDescriptor out_temp = " + out_temp.toString() + "Type of out_temp = " + out_temp.getType());
216 return new NodePair(fon, fon);
220 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
221 TypeDescriptor td=con.getType();
223 FlatNew fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
224 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
227 for(int i=0; i<con.numArgs(); i++) {
228 ExpressionNode en=con.getArg(i);
229 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
231 NodePair np=flattenExpressionNode(en, tmp);
232 last.addNext(np.getBegin());
235 MethodDescriptor md=con.getConstructor();
236 //Call to constructor
237 FlatCall fc=new FlatCall(md, null, out_temp, temps);
240 if (td.getClassDesc().hasFlags()) {
241 // if (con.getFlagEffects()!=null) {
242 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
243 FlagEffects fes=con.getFlagEffects();
244 TempDescriptor flagtemp=out_temp;
246 for(int j=0; j<fes.numEffects(); j++) {
247 FlagEffect fe=fes.getEffect(j);
248 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
250 for(int j=0; j<fes.numTagEffects(); j++) {
251 TagEffect te=fes.getTagEffect(j);
252 TempDescriptor tagtemp=getTempforVar(te.getTag());
254 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
257 ffan.addFlagAction(flagtemp, null, false);
262 return new NodePair(fn,last);
266 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
267 for (int i=0; i<con.numArgs(); i++) {
268 ExpressionNode en=con.getArg(i);
269 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
271 NodePair np=flattenExpressionNode(en, tmp);
275 last.addNext(np.getBegin());
278 TempDescriptor tmp2=(i==0) ?
280 TempDescriptor.tempFactory("arg",en.getType());
282 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
284 if (temps.length>1) {
285 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
286 fn.addNext(np.getBegin());
287 return new NodePair(first,np.getEnd());
289 return new NodePair(first, fn);
293 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
294 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
295 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
296 FlatNop fnop=new FlatNop(); //last node
299 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
301 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
303 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
305 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
306 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
307 fcb.setTrueProb(State.TRUEPROB);
310 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
311 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1], isglobal);
312 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
314 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
320 fcb.addFalseNext(fnop);
322 //Recursive call here
323 if ((i+2)<temparray.length) {
324 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
325 fsen.addNext(np2.getBegin());
326 np2.getEnd().addNext(fon);
331 return new NodePair(fln, fnop);
334 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
335 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
338 TempDescriptor thisarg=null;
340 if (min.getExpression()!=null) {
341 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
342 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
348 for(int i=0; i<min.numArgs(); i++) {
349 ExpressionNode en=min.getArg(i);
350 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
352 NodePair np=flattenExpressionNode(en, td);
356 last.addNext(np.getBegin());
360 MethodDescriptor md=min.getMethod();
362 //Call to constructor
365 if(md.getReturnType()==null||md.getReturnType().isVoid())
366 fc=new FlatCall(md, null, thisarg, temps);
368 fc=new FlatCall(md, out_temp, thisarg, temps);
373 return new NodePair(first,fc);
376 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
377 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
378 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
379 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
380 npe.getEnd().addNext(fn);
381 return new NodePair(npe.getBegin(),fn);
384 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
385 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
386 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
387 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
388 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
389 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
390 npe.getEnd().addNext(npi.getBegin());
391 npi.getEnd().addNext(fn);
392 return new NodePair(npe.getBegin(),fn);
395 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
397 // left side is variable
398 // left side is field
399 // left side is array
401 Operation base=an.getOperation().getBaseOp();
402 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
405 //rewrite the base operation
406 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
410 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
413 if (an.getSrc()!=null) {
414 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
415 first=np_src.getBegin();
416 last=np_src.getEnd();
418 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
423 if (an.getDest().kind()==Kind.FieldAccessNode) {
424 //We are assigning an object field
426 FieldAccessNode fan=(FieldAccessNode)an.getDest();
427 ExpressionNode en=fan.getExpression();
428 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
429 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
431 first=np_baseexp.getBegin();
433 last.addNext(np_baseexp.getBegin());
434 last=np_baseexp.getEnd();
436 //See if we need to perform an operation
438 //If it is a preinc we need to store the initial value
439 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
440 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
441 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
445 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
446 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
447 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
448 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
453 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
460 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
464 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
468 return new NodePair(first, last);
469 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
470 //We are assigning an array element
473 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
474 ExpressionNode en=aan.getExpression();
475 ExpressionNode enindex=aan.getIndex();
476 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
477 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
478 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
479 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
481 first=np_baseexp.getBegin();
483 last.addNext(np_baseexp.getBegin());
484 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
485 last=np_indexexp.getEnd();
487 //See if we need to perform an operation
489 //If it is a preinc we need to store the initial value
490 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
491 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
493 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
497 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
498 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
499 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
500 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
507 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
515 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
519 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
523 return new NodePair(first, last);
524 } else if (an.getDest().kind()==Kind.NameNode) {
525 //We could be assigning a field or variable
526 NameNode nn=(NameNode)an.getDest();
529 if (nn.getExpression()!=null) {
531 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
532 ExpressionNode en=fan.getExpression();
533 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
534 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
536 first=np_baseexp.getBegin();
538 last.addNext(np_baseexp.getBegin());
539 last=np_baseexp.getEnd();
541 //See if we need to perform an operation
543 //If it is a preinc we need to store the initial value
544 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
545 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
547 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
552 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
553 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
554 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
555 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
560 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
568 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
572 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
576 return new NodePair(first, last);
578 if (nn.getField()!=null) {
582 //See if we need to perform an operation
584 //If it is a preinc we need to store the initial value
585 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
586 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
588 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
597 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
598 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
599 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
600 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
605 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
612 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
620 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
624 return new NodePair(first, last);
627 //See if we need to perform an operation
630 //If it is a preinc we need to store the initial value
631 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
632 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
634 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
643 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
644 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
645 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
646 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
654 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
664 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
669 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
673 return new NodePair(first, last);
681 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
682 if (nn.getExpression()!=null) {
683 /* Hack - use subtree instead */
684 return flattenExpressionNode(nn.getExpression(),out_temp);
685 } else if (nn.getField()!=null) {
686 TempDescriptor tmp=getTempforVar(nn.getVar());
687 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
688 return new NodePair(ffn,ffn);
690 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
693 out_temp.setTag(tmp.getTag());
695 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
696 return new NodePair(fon,fon);
700 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
701 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
702 TempDescriptor temp_right=null;
704 Operation op=on.getOp();
706 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
708 if (on.getRight()!=null) {
709 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
710 right=flattenExpressionNode(on.getRight(),temp_right);
712 FlatNop nop=new FlatNop();
713 right=new NodePair(nop,nop);
716 if (op.getOp()==Operation.LOGIC_OR) {
717 /* Need to do shortcircuiting */
718 FlatCondBranch fcb=new FlatCondBranch(temp_left);
719 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
720 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
721 FlatNop fnop=new FlatNop();
722 left.getEnd().addNext(fcb);
723 fcb.addFalseNext(right.getBegin());
724 right.getEnd().addNext(fon2);
726 fcb.addTrueNext(fon1);
728 return new NodePair(left.getBegin(), fnop);
729 } else if (op.getOp()==Operation.LOGIC_AND) {
730 /* Need to do shortcircuiting */
731 FlatCondBranch fcb=new FlatCondBranch(temp_left);
732 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
733 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
734 FlatNop fnop=new FlatNop();
735 left.getEnd().addNext(fcb);
736 fcb.addTrueNext(right.getBegin());
737 right.getEnd().addNext(fon2);
739 fcb.addFalseNext(fon1);
741 return new NodePair(left.getBegin(), fnop);
742 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
743 //We have a string concatenate
744 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
745 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
746 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
747 left.getEnd().addNext(right.getBegin());
748 right.getEnd().addNext(fc);
749 return new NodePair(left.getBegin(), fc);
752 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
753 left.getEnd().addNext(right.getBegin());
754 right.getEnd().addNext(fon);
755 return new NodePair(left.getBegin(),fon);
758 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
760 case Kind.AssignmentNode:
761 return flattenAssignmentNode((AssignmentNode)en,out_temp);
764 return flattenCastNode((CastNode)en,out_temp);
766 case Kind.CreateObjectNode:
767 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
769 case Kind.FieldAccessNode:
770 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
772 case Kind.ArrayAccessNode:
773 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
775 case Kind.LiteralNode:
776 return flattenLiteralNode((LiteralNode)en,out_temp);
778 case Kind.MethodInvokeNode:
779 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
782 return flattenNameNode((NameNode)en,out_temp);
785 return flattenOpNode((OpNode)en,out_temp);
787 case Kind.OffsetNode:
788 return flattenOffsetNode((OffsetNode)en,out_temp);
790 case Kind.TertiaryNode:
791 return flattenTertiaryNode((TertiaryNode)en,out_temp);
796 private NodePair flattenDeclarationNode(DeclarationNode dn) {
797 VarDescriptor vd=dn.getVarDescriptor();
798 TempDescriptor td=getTempforVar(vd);
799 if (dn.getExpression()!=null)
800 return flattenExpressionNode(dn.getExpression(),td);
802 FlatNop fn=new FlatNop();
803 return new NodePair(fn,fn);
807 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
808 TagVarDescriptor tvd=dn.getTagVarDescriptor();
809 TagDescriptor tag=tvd.getTag();
810 TempDescriptor tmp=getTempforVar(tvd);
811 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
812 return new NodePair(ftd,ftd);
815 private TempDescriptor getTempforParam(Descriptor d) {
816 if (temptovar.containsKey(d))
817 return (TempDescriptor)temptovar.get(d);
819 if (d instanceof VarDescriptor) {
820 VarDescriptor vd=(VarDescriptor)d;
821 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
822 temptovar.put(vd,td);
824 } else if (d instanceof TagVarDescriptor) {
825 TagVarDescriptor tvd=(TagVarDescriptor)d;
826 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
827 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
828 temptovar.put(tvd,td);
830 } else throw new Error("Unreconized Descriptor");
834 private TempDescriptor getTempforVar(Descriptor d) {
835 if (temptovar.containsKey(d))
836 return (TempDescriptor)temptovar.get(d);
838 if (d instanceof VarDescriptor) {
839 VarDescriptor vd=(VarDescriptor)d;
840 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
841 temptovar.put(vd,td);
843 } else if (d instanceof TagVarDescriptor) {
844 TagVarDescriptor tvd=(TagVarDescriptor)d;
845 //BUGFIX TAGTYPE - add next line, modify following
846 //line to tag this new type descriptor, modify
847 //TempDescriptor constructor & factory to set type
848 //using this Type To test, use any program with tags
849 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
850 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
851 temptovar.put(tvd,td);
853 } else throw new Error("Unrecognized Descriptor");
857 private NodePair flattenIfStatementNode(IfStatementNode isn) {
858 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
859 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
860 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
861 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
863 FlatNop nopend=new FlatNop();
865 if (isn.getFalseBlock()!=null)
866 false_np=flattenBlockNode(isn.getFalseBlock());
868 FlatNop nop=new FlatNop();
869 false_np=new NodePair(nop,nop);
872 cond.getEnd().addNext(fcb);
873 fcb.addTrueNext(true_np.getBegin());
874 fcb.addFalseNext(false_np.getBegin());
875 if (true_np.getEnd()!=null)
876 true_np.getEnd().addNext(nopend);
877 if (false_np.getEnd()!=null)
878 false_np.getEnd().addNext(nopend);
879 return new NodePair(cond.getBegin(), nopend);
882 private NodePair flattenLoopNode(LoopNode ln) {
883 if (ln.getType()==LoopNode.FORLOOP) {
884 NodePair initializer=flattenBlockNode(ln.getInitializer());
885 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
886 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
887 NodePair update=flattenBlockNode(ln.getUpdate());
888 NodePair body=flattenBlockNode(ln.getBody());
889 FlatNode begin=initializer.getBegin();
890 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
891 fcb.setTrueProb(State.TRUEPROB);
893 FlatNop nopend=new FlatNop();
894 FlatBackEdge backedge=new FlatBackEdge();
896 FlatNop nop2=new FlatNop();
897 initializer.getEnd().addNext(nop2);
898 nop2.addNext(condition.getBegin());
899 if (body.getEnd()!=null)
900 body.getEnd().addNext(update.getBegin());
901 update.getEnd().addNext(backedge);
902 backedge.addNext(condition.getBegin());
903 condition.getEnd().addNext(fcb);
904 fcb.addFalseNext(nopend);
905 fcb.addTrueNext(body.getBegin());
906 for(Iterator contit=continueset.iterator();contit.hasNext();) {
907 FlatNode fn=(FlatNode)contit.next();
909 fn.addNext(update.getBegin());
911 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
912 FlatNode fn=(FlatNode)breakit.next();
917 return new NodePair(begin,nopend);
918 } else if (ln.getType()==LoopNode.WHILELOOP) {
919 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
920 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
921 NodePair body=flattenBlockNode(ln.getBody());
922 FlatNode begin=condition.getBegin();
923 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
924 fcb.setTrueProb(State.TRUEPROB);
926 FlatNop nopend=new FlatNop();
927 FlatBackEdge backedge=new FlatBackEdge();
929 body.getEnd().addNext(backedge);
930 backedge.addNext(condition.getBegin());
932 condition.getEnd().addNext(fcb);
933 fcb.addFalseNext(nopend);
934 fcb.addTrueNext(body.getBegin());
936 for(Iterator contit=continueset.iterator();contit.hasNext();) {
937 FlatNode fn=(FlatNode)contit.next();
939 fn.addNext(backedge);
941 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
942 FlatNode fn=(FlatNode)breakit.next();
946 return new NodePair(begin,nopend);
947 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
948 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
949 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
950 NodePair body=flattenBlockNode(ln.getBody());
951 FlatNode begin=body.getBegin();
952 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
953 fcb.setTrueProb(State.TRUEPROB);
955 FlatNop nopend=new FlatNop();
956 FlatBackEdge backedge=new FlatBackEdge();
958 body.getEnd().addNext(condition.getBegin());
959 condition.getEnd().addNext(fcb);
960 fcb.addFalseNext(nopend);
961 fcb.addTrueNext(backedge);
962 backedge.addNext(body.getBegin());
964 for(Iterator contit=continueset.iterator();contit.hasNext();) {
965 FlatNode fn=(FlatNode)contit.next();
967 fn.addNext(condition.getBegin());
969 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
970 FlatNode fn=(FlatNode)breakit.next();
974 return new NodePair(begin,nopend);
975 } else throw new Error();
978 private NodePair flattenReturnNode(ReturnNode rntree) {
979 TempDescriptor retval=null;
981 if (rntree.getReturnExpression()!=null) {
982 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
983 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
986 FlatReturnNode rnflat=new FlatReturnNode(retval);
988 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
989 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
990 TempDescriptor thistd=getTempforVar(currmd.getThis());
991 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
995 if (state.DSM&&currmd.getModifiers().isAtomic()) {
996 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1002 cond.getEnd().addNext(ln);
1003 return new NodePair(cond.getBegin(),rnflat);
1005 return new NodePair(ln,rnflat);
1008 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1009 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1010 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1011 updateFlagActionNode(ffan, ten.getFlagEffects());
1012 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1013 ffan.addNext(fcn.getBegin());
1014 FlatReturnNode rnflat=new FlatReturnNode(null);
1015 fcn.getEnd().addNext(rnflat);
1016 return new NodePair(ffan, rnflat);
1019 private NodePair flattenConstraintCheck(Vector ccs) {
1020 FlatNode begin=new FlatNop();
1022 return new NodePair(begin,begin);
1023 FlatNode last=begin;
1024 for(int i=0; i<ccs.size(); i++) {
1025 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1026 /* Flatten the arguments */
1027 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1028 String[] vars=new String[cc.numArgs()];
1029 for(int j=0; j<cc.numArgs(); j++) {
1030 ExpressionNode en=cc.getArg(j);
1031 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1033 vars[j]=cc.getVar(j);
1034 NodePair np=flattenExpressionNode(en, td);
1035 last.addNext(np.getBegin());
1039 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1043 return new NodePair(begin,last);
1046 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1047 return flattenBlockNode(sbn.getBlockNode());
1050 private NodePair flattenAtomicNode(AtomicNode sbn) {
1051 NodePair np=flattenBlockNode(sbn.getBlockNode());
1052 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1053 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1054 faen.addNext(np.getBegin());
1055 np.getEnd().addNext(faexn);
1056 return new NodePair(faen, faexn);
1059 private NodePair flattenSESENode(SESENode sn) {
1060 if( sn.isStart() ) {
1061 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1062 sn.setFlatEnter(fsen);
1063 return new NodePair(fsen, fsen);
1066 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1067 sn.setFlatExit(fsexn);
1068 fsexn.setFlatEnter( sn.getStart().getFlatEnter() );
1069 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1070 return new NodePair(fsexn, fsexn);
1073 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1074 FlatNop fn=new FlatNop();
1078 continueset.add(fn);
1079 return new NodePair(fn,null);
1082 private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1083 TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1084 TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1085 TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1087 NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1088 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1090 NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1091 FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1093 NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1094 FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1096 FlatNop nopend=new FlatNop();
1098 cond.getEnd().addNext(fcb);
1100 fcb.addTrueNext(trueExpr.getBegin());
1101 fcb.addFalseNext(falseExpr.getBegin());
1103 trueExpr.getEnd().addNext(fonT);
1104 fonT.addNext(nopend);
1106 falseExpr.getEnd().addNext(fonF);
1107 fonF.addNext(nopend);
1109 return new NodePair(cond.getBegin(), nopend);
1112 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1113 switch(bsn.kind()) {
1114 case Kind.BlockExpressionNode:
1115 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1117 case Kind.DeclarationNode:
1118 return flattenDeclarationNode((DeclarationNode)bsn);
1120 case Kind.TagDeclarationNode:
1121 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1123 case Kind.IfStatementNode:
1124 return flattenIfStatementNode((IfStatementNode)bsn);
1127 return flattenLoopNode((LoopNode)bsn);
1129 case Kind.ReturnNode:
1130 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1132 case Kind.TaskExitNode:
1133 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1135 case Kind.SubBlockNode:
1136 return flattenSubBlockNode((SubBlockNode)bsn);
1138 case Kind.AtomicNode:
1139 return flattenAtomicNode((AtomicNode)bsn);
1142 return flattenSESENode((SESENode)bsn);
1144 case Kind.ContinueBreakNode:
1145 return flattenContinueBreakNode((ContinueBreakNode)bsn);