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);
793 private NodePair flattenDeclarationNode(DeclarationNode dn) {
794 VarDescriptor vd=dn.getVarDescriptor();
795 TempDescriptor td=getTempforVar(vd);
796 if (dn.getExpression()!=null)
797 return flattenExpressionNode(dn.getExpression(),td);
799 FlatNop fn=new FlatNop();
800 return new NodePair(fn,fn);
804 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
805 TagVarDescriptor tvd=dn.getTagVarDescriptor();
806 TagDescriptor tag=tvd.getTag();
807 TempDescriptor tmp=getTempforVar(tvd);
808 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
809 return new NodePair(ftd,ftd);
812 private TempDescriptor getTempforParam(Descriptor d) {
813 if (temptovar.containsKey(d))
814 return (TempDescriptor)temptovar.get(d);
816 if (d instanceof VarDescriptor) {
817 VarDescriptor vd=(VarDescriptor)d;
818 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
819 temptovar.put(vd,td);
821 } else if (d instanceof TagVarDescriptor) {
822 TagVarDescriptor tvd=(TagVarDescriptor)d;
823 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
824 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
825 temptovar.put(tvd,td);
827 } else throw new Error("Unreconized Descriptor");
831 private TempDescriptor getTempforVar(Descriptor d) {
832 if (temptovar.containsKey(d))
833 return (TempDescriptor)temptovar.get(d);
835 if (d instanceof VarDescriptor) {
836 VarDescriptor vd=(VarDescriptor)d;
837 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
838 temptovar.put(vd,td);
840 } else if (d instanceof TagVarDescriptor) {
841 TagVarDescriptor tvd=(TagVarDescriptor)d;
842 //BUGFIX TAGTYPE - add next line, modify following
843 //line to tag this new type descriptor, modify
844 //TempDescriptor constructor & factory to set type
845 //using this Type To test, use any program with tags
846 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
847 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
848 temptovar.put(tvd,td);
850 } else throw new Error("Unrecognized Descriptor");
854 private NodePair flattenIfStatementNode(IfStatementNode isn) {
855 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
856 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
857 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
858 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
860 FlatNop nopend=new FlatNop();
862 if (isn.getFalseBlock()!=null)
863 false_np=flattenBlockNode(isn.getFalseBlock());
865 FlatNop nop=new FlatNop();
866 false_np=new NodePair(nop,nop);
869 cond.getEnd().addNext(fcb);
870 fcb.addTrueNext(true_np.getBegin());
871 fcb.addFalseNext(false_np.getBegin());
872 if (true_np.getEnd()!=null)
873 true_np.getEnd().addNext(nopend);
874 if (false_np.getEnd()!=null)
875 false_np.getEnd().addNext(nopend);
876 return new NodePair(cond.getBegin(), nopend);
879 private NodePair flattenLoopNode(LoopNode ln) {
880 if (ln.getType()==LoopNode.FORLOOP) {
881 NodePair initializer=flattenBlockNode(ln.getInitializer());
882 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
883 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
884 NodePair update=flattenBlockNode(ln.getUpdate());
885 NodePair body=flattenBlockNode(ln.getBody());
886 FlatNode begin=initializer.getBegin();
887 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
888 fcb.setTrueProb(State.TRUEPROB);
890 FlatNop nopend=new FlatNop();
891 FlatBackEdge backedge=new FlatBackEdge();
893 FlatNop nop2=new FlatNop();
894 initializer.getEnd().addNext(nop2);
895 nop2.addNext(condition.getBegin());
896 if (body.getEnd()!=null)
897 body.getEnd().addNext(update.getBegin());
898 update.getEnd().addNext(backedge);
899 backedge.addNext(condition.getBegin());
900 condition.getEnd().addNext(fcb);
901 fcb.addFalseNext(nopend);
902 fcb.addTrueNext(body.getBegin());
903 for(Iterator contit=continueset.iterator();contit.hasNext();) {
904 FlatNode fn=(FlatNode)contit.next();
906 fn.addNext(update.getBegin());
908 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
909 FlatNode fn=(FlatNode)breakit.next();
914 return new NodePair(begin,nopend);
915 } else if (ln.getType()==LoopNode.WHILELOOP) {
916 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
917 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
918 NodePair body=flattenBlockNode(ln.getBody());
919 FlatNode begin=condition.getBegin();
920 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
921 fcb.setTrueProb(State.TRUEPROB);
923 FlatNop nopend=new FlatNop();
924 FlatBackEdge backedge=new FlatBackEdge();
926 body.getEnd().addNext(backedge);
927 backedge.addNext(condition.getBegin());
929 condition.getEnd().addNext(fcb);
930 fcb.addFalseNext(nopend);
931 fcb.addTrueNext(body.getBegin());
933 for(Iterator contit=continueset.iterator();contit.hasNext();) {
934 FlatNode fn=(FlatNode)contit.next();
936 fn.addNext(backedge);
938 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
939 FlatNode fn=(FlatNode)breakit.next();
943 return new NodePair(begin,nopend);
944 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
945 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
946 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
947 NodePair body=flattenBlockNode(ln.getBody());
948 FlatNode begin=body.getBegin();
949 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
950 fcb.setTrueProb(State.TRUEPROB);
952 FlatNop nopend=new FlatNop();
953 FlatBackEdge backedge=new FlatBackEdge();
955 body.getEnd().addNext(condition.getBegin());
956 condition.getEnd().addNext(fcb);
957 fcb.addFalseNext(nopend);
958 fcb.addTrueNext(backedge);
959 backedge.addNext(body.getBegin());
961 for(Iterator contit=continueset.iterator();contit.hasNext();) {
962 FlatNode fn=(FlatNode)contit.next();
964 fn.addNext(condition.getBegin());
966 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
967 FlatNode fn=(FlatNode)breakit.next();
971 return new NodePair(begin,nopend);
972 } else throw new Error();
975 private NodePair flattenReturnNode(ReturnNode rntree) {
976 TempDescriptor retval=null;
978 if (rntree.getReturnExpression()!=null) {
979 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
980 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
983 FlatReturnNode rnflat=new FlatReturnNode(retval);
985 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
986 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
987 TempDescriptor thistd=getTempforVar(currmd.getThis());
988 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
992 if (state.DSM&&currmd.getModifiers().isAtomic()) {
993 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
999 cond.getEnd().addNext(ln);
1000 return new NodePair(cond.getBegin(),rnflat);
1002 return new NodePair(ln,rnflat);
1005 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1006 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1007 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1008 updateFlagActionNode(ffan, ten.getFlagEffects());
1009 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1010 ffan.addNext(fcn.getBegin());
1011 FlatReturnNode rnflat=new FlatReturnNode(null);
1012 fcn.getEnd().addNext(rnflat);
1013 return new NodePair(ffan, rnflat);
1016 private NodePair flattenConstraintCheck(Vector ccs) {
1017 FlatNode begin=new FlatNop();
1019 return new NodePair(begin,begin);
1020 FlatNode last=begin;
1021 for(int i=0; i<ccs.size(); i++) {
1022 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1023 /* Flatten the arguments */
1024 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1025 String[] vars=new String[cc.numArgs()];
1026 for(int j=0; j<cc.numArgs(); j++) {
1027 ExpressionNode en=cc.getArg(j);
1028 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1030 vars[j]=cc.getVar(j);
1031 NodePair np=flattenExpressionNode(en, td);
1032 last.addNext(np.getBegin());
1036 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1040 return new NodePair(begin,last);
1043 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1044 return flattenBlockNode(sbn.getBlockNode());
1047 private NodePair flattenAtomicNode(AtomicNode sbn) {
1048 NodePair np=flattenBlockNode(sbn.getBlockNode());
1049 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1050 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1051 faen.addNext(np.getBegin());
1052 np.getEnd().addNext(faexn);
1053 return new NodePair(faen, faexn);
1056 private NodePair flattenSESENode(SESENode sn) {
1057 if( sn.isStart() ) {
1058 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1059 sn.setFlatEnter(fsen);
1060 return new NodePair(fsen, fsen);
1063 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1064 sn.setFlatExit(fsexn);
1065 fsexn.setFlatEnter( sn.getStart().getFlatEnter() );
1066 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1067 return new NodePair(fsexn, fsexn);
1070 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1071 FlatNop fn=new FlatNop();
1075 continueset.add(fn);
1076 return new NodePair(fn,null);
1079 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1080 switch(bsn.kind()) {
1081 case Kind.BlockExpressionNode:
1082 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1084 case Kind.DeclarationNode:
1085 return flattenDeclarationNode((DeclarationNode)bsn);
1087 case Kind.TagDeclarationNode:
1088 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1090 case Kind.IfStatementNode:
1091 return flattenIfStatementNode((IfStatementNode)bsn);
1094 return flattenLoopNode((LoopNode)bsn);
1096 case Kind.ReturnNode:
1097 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1099 case Kind.TaskExitNode:
1100 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1102 case Kind.SubBlockNode:
1103 return flattenSubBlockNode((SubBlockNode)bsn);
1105 case Kind.AtomicNode:
1106 return flattenAtomicNode((AtomicNode)bsn);
1109 return flattenSESENode((SESENode)bsn);
1111 case Kind.ContinueBreakNode:
1112 return flattenContinueBreakNode((ContinueBreakNode)bsn);