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 FlatNode fn2=np.getEnd();
49 if (fn2!=null&& fn2.kind()!=FKind.FlatReturnNode) {
50 FlatReturnNode rnflat=new FlatReturnNode(null);
55 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
58 FlatMethod fm=new FlatMethod(td, fe);
61 Hashtable visitedset=new Hashtable();
63 for(int i=0; i<td.numParameters(); i++) {
64 VarDescriptor paramvd=td.getParameter(i);
65 fm.addParameterTemp(getTempforVar(paramvd));
66 TagExpressionList tel=td.getTag(paramvd);
67 //BUG added next line to fix...to test feed in any task program
69 for(int j=0; j<tel.numTags(); j++) {
70 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
71 TempDescriptor tagtmp=getTempforVar(tvd);
72 if (!visitedset.containsKey(tvd.getName())) {
73 visitedset.put(tvd.getName(),tvd.getTag());
74 fm.addTagTemp(tagtmp);
76 TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
77 if (!tmptd.equals(tvd.getTag()))
78 throw new Error("Two different tag types with same name as parameters to:"+td);
80 tel.setTemp(j, tagtmp);
84 /* Flatten Vector of Flag Effects */
85 Vector flags=td.getFlagEffects();
86 updateFlagActionNode(ffan,flags);
88 state.addFlatCode(td,fm);
92 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
93 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
94 if (flags==null) // Do nothing if the flag effects vector is empty
97 for(int i=0; i<flags.size(); i++) {
98 FlagEffects fes=(FlagEffects)flags.get(i);
99 TempDescriptor flagtemp=getTempforVar(fes.getVar());
101 for(int j=0; j<fes.numEffects(); j++) {
102 FlagEffect fe=fes.getEffect(j);
103 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
106 for(int j=0; j<fes.numTagEffects(); j++) {
107 TagEffect te=fes.getTagEffect(j);
108 TempDescriptor tagtemp=getTempforVar(te.getTag());
110 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
115 FlatAtomicEnterNode curran=null;
117 private void flattenClass(ClassDescriptor cn) {
118 Iterator methodit=cn.getMethods();
119 while(methodit.hasNext()) {
121 currmd=(MethodDescriptor)methodit.next();
122 BlockNode bn=state.getMethodBody(currmd);
124 if (state.DSM&&currmd.getModifiers().isAtomic()) {
125 curran=new FlatAtomicEnterNode();
128 NodePair np=flattenBlockNode(bn);
129 FlatNode fn=np.getBegin();
130 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
131 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
132 TempDescriptor thistd=getTempforVar(currmd.getThis());
133 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
136 if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
137 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
138 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
139 np.getEnd().addNext(fcunlock);
140 FlatReturnNode rnflat=new FlatReturnNode(null);
141 fcunlock.addNext(rnflat);
144 } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
147 if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
148 FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
149 np.getEnd().addNext(aen);
150 FlatReturnNode rnflat=new FlatReturnNode(null);
154 } else if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
155 FlatReturnNode rnflat=new FlatReturnNode(null);
156 np.getEnd().addNext(rnflat);
160 FlatMethod fm=new FlatMethod(currmd, fe);
162 if (!currmd.isStatic())
163 fm.addParameterTemp(getTempforParam(currmd.getThis()));
164 for(int i=0; i<currmd.numParameters(); i++) {
165 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
167 state.addFlatCode(currmd,fm);
171 private NodePair flattenBlockNode(BlockNode bn) {
174 for(int i=0; i<bn.size(); i++) {
175 NodePair np=flattenBlockStatementNode(bn.get(i));
176 FlatNode np_begin=np.getBegin();
177 FlatNode np_end=np.getEnd();
184 end.addNext(np_begin);
186 return new NodePair(begin, null);
192 end=begin=new FlatNop();
194 return new NodePair(begin,end);
197 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
198 //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
199 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
200 return flattenExpressionNode(en.getExpression(),tmp);
203 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
204 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
205 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
206 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
207 np.getEnd().addNext(fcn);
208 return new NodePair(np.getBegin(),fcn);
211 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
212 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
213 return new NodePair(fln,fln);
216 private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
217 FlatOffsetNode fln = new FlatOffsetNode(ofn.getClassType(), ofn.getField(), out_temp);
218 return new NodePair(fln, fln);
221 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
222 TypeDescriptor td=con.getType();
224 FlatNode fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
226 //handle wrapper fields
227 ClassDescriptor cd=td.getClassDesc();
228 for(Iterator fieldit=cd.getFields();fieldit.hasNext();) {
229 FieldDescriptor fd=(FieldDescriptor)fieldit.next();
230 if (fd.getType().iswrapper()) {
231 TempDescriptor wrap_tmp=TempDescriptor.tempFactory("wrapper_obj",fd.getType());
232 FlatNode fnwrapper=new FlatNew(fd.getType(), wrap_tmp, con.isGlobal());
233 FlatSetFieldNode fsfn=new FlatSetFieldNode(out_temp, fd, wrap_tmp);
234 last.addNext(fnwrapper);
235 fnwrapper.addNext(fsfn);
240 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
243 for(int i=0; i<con.numArgs(); i++) {
244 ExpressionNode en=con.getArg(i);
245 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
247 NodePair np=flattenExpressionNode(en, tmp);
248 last.addNext(np.getBegin());
251 MethodDescriptor md=con.getConstructor();
252 //Call to constructor
253 FlatCall fc=new FlatCall(md, null, out_temp, temps);
256 if (td.getClassDesc().hasFlags()) {
257 // if (con.getFlagEffects()!=null) {
258 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
259 FlagEffects fes=con.getFlagEffects();
260 TempDescriptor flagtemp=out_temp;
262 for(int j=0; j<fes.numEffects(); j++) {
263 FlagEffect fe=fes.getEffect(j);
264 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
266 for(int j=0; j<fes.numTagEffects(); j++) {
267 TagEffect te=fes.getTagEffect(j);
268 TempDescriptor tagtemp=getTempforVar(te.getTag());
270 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
273 ffan.addFlagAction(flagtemp, null, false);
278 return new NodePair(fn,last);
282 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
283 for (int i=0; i<con.numArgs(); i++) {
284 ExpressionNode en=con.getArg(i);
285 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
287 NodePair np=flattenExpressionNode(en, tmp);
291 last.addNext(np.getBegin());
294 TempDescriptor tmp2=(i==0) ?
296 TempDescriptor.tempFactory("arg",en.getType());
298 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
300 if (temps.length>1) {
301 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
302 fn.addNext(np.getBegin());
303 return new NodePair(first,np.getEnd());
304 } else if (td.isArray()&&td.dereference().iswrapper()) {
305 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
306 fn.addNext(np.getBegin());
307 return new NodePair(first,np.getEnd());
309 return new NodePair(first, fn);
313 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
314 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
315 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
316 FlatNop fnop=new FlatNop(); //last node
319 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
321 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
323 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
325 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
326 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
327 fcb.setTrueProb(State.TRUEPROB);
330 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
331 FlatNew fn=td.iswrapper()?new FlatNew(td, new_tmp, isglobal):new FlatNew(td, new_tmp, temparray[i+1], isglobal);
332 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
334 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
340 fcb.addFalseNext(fnop);
342 //Recursive call here
343 if ((i+2)<temparray.length) {
344 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
345 fsen.addNext(np2.getBegin());
346 np2.getEnd().addNext(fon);
347 } else if (td.isArray()&&td.dereference().iswrapper()) {
348 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
349 fsen.addNext(np2.getBegin());
350 np2.getEnd().addNext(fon);
355 return new NodePair(fln, fnop);
358 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
359 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
362 TempDescriptor thisarg=null;
364 if (min.getExpression()!=null) {
365 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
366 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
372 for(int i=0; i<min.numArgs(); i++) {
373 ExpressionNode en=min.getArg(i);
374 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
376 NodePair np=flattenExpressionNode(en, td);
380 last.addNext(np.getBegin());
384 MethodDescriptor md=min.getMethod();
386 //Call to constructor
389 if(md.getReturnType()==null||md.getReturnType().isVoid())
390 fc=new FlatCall(md, null, thisarg, temps);
392 fc=new FlatCall(md, out_temp, thisarg, temps);
397 return new NodePair(first,fc);
400 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
401 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
402 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
403 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
404 npe.getEnd().addNext(fn);
405 return new NodePair(npe.getBegin(),fn);
408 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
409 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
410 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
411 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
412 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
413 TempDescriptor arraytmp=out_temp;
414 if (aan.iswrapper()) {
416 arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference());
418 FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp);
419 npe.getEnd().addNext(npi.getBegin());
420 npi.getEnd().addNext(fn);
421 if (aan.iswrapper()) {
422 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp);
426 return new NodePair(npe.getBegin(),fn);
429 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
431 // left side is variable
432 // left side is field
433 // left side is array
435 Operation base=an.getOperation().getBaseOp();
436 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
439 //rewrite the base operation
440 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
444 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
447 if (an.getSrc()!=null) {
448 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
449 first=np_src.getBegin();
450 last=np_src.getEnd();
452 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
457 if (an.getDest().kind()==Kind.FieldAccessNode) {
458 //We are assigning an object field
460 FieldAccessNode fan=(FieldAccessNode)an.getDest();
461 ExpressionNode en=fan.getExpression();
462 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
463 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
465 first=np_baseexp.getBegin();
467 last.addNext(np_baseexp.getBegin());
468 last=np_baseexp.getEnd();
470 //See if we need to perform an operation
472 //If it is a preinc we need to store the initial value
473 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
474 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
475 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
479 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
480 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
481 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
482 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
487 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
494 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
498 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
502 return new NodePair(first, last);
503 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
504 //We are assigning an array element
506 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
507 ExpressionNode en=aan.getExpression();
508 ExpressionNode enindex=aan.getIndex();
509 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
510 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
511 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
512 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
514 first=np_baseexp.getBegin();
516 last.addNext(np_baseexp.getBegin());
517 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
518 last=np_indexexp.getEnd();
520 //See if we need to perform an operation
522 //If it is a preinc we need to store the initial value
523 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
524 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
526 if (aan.iswrapper()) {
527 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
528 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
529 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
530 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp3,src_tmp2);
535 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
539 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
540 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
541 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
542 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
547 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
554 if (aan.iswrapper()) {
555 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
556 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
557 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
558 FlatSetFieldNode fsfn=new FlatSetFieldNode(src_tmp3,(FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp);
563 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
568 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
572 return new NodePair(first, last);
573 } else if (an.getDest().kind()==Kind.NameNode) {
574 //We could be assigning a field or variable
575 NameNode nn=(NameNode)an.getDest();
578 if (nn.getExpression()!=null) {
580 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
581 ExpressionNode en=fan.getExpression();
582 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
583 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
585 first=np_baseexp.getBegin();
587 last.addNext(np_baseexp.getBegin());
588 last=np_baseexp.getEnd();
590 //See if we need to perform an operation
592 //If it is a preinc we need to store the initial value
593 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
594 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
596 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
601 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
602 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
603 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
604 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
609 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
617 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
621 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
625 return new NodePair(first, last);
627 if (nn.getField()!=null) {
631 //See if we need to perform an operation
633 //If it is a preinc we need to store the initial value
634 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
635 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
637 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
646 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
647 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
648 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
649 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
654 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
661 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
669 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
673 return new NodePair(first, last);
676 //See if we need to perform an operation
679 //If it is a preinc we need to store the initial value
680 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
681 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
683 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
692 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
693 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
694 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
695 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
703 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
713 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
718 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
722 return new NodePair(first, last);
730 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
731 if (nn.getExpression()!=null) {
732 /* Hack - use subtree instead */
733 return flattenExpressionNode(nn.getExpression(),out_temp);
734 } else if (nn.getField()!=null) {
735 TempDescriptor tmp=getTempforVar(nn.getVar());
736 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
737 return new NodePair(ffn,ffn);
739 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
742 out_temp.setTag(tmp.getTag());
744 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
745 return new NodePair(fon,fon);
749 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
750 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
751 TempDescriptor temp_right=null;
753 Operation op=on.getOp();
755 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
757 if (on.getRight()!=null) {
758 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
759 right=flattenExpressionNode(on.getRight(),temp_right);
761 FlatNop nop=new FlatNop();
762 right=new NodePair(nop,nop);
765 if (op.getOp()==Operation.LOGIC_OR) {
766 /* Need to do shortcircuiting */
767 FlatCondBranch fcb=new FlatCondBranch(temp_left);
768 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
769 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
770 FlatNop fnop=new FlatNop();
771 left.getEnd().addNext(fcb);
772 fcb.addFalseNext(right.getBegin());
773 right.getEnd().addNext(fon2);
775 fcb.addTrueNext(fon1);
777 return new NodePair(left.getBegin(), fnop);
778 } else if (op.getOp()==Operation.LOGIC_AND) {
779 /* Need to do shortcircuiting */
780 FlatCondBranch fcb=new FlatCondBranch(temp_left);
781 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
782 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
783 FlatNop fnop=new FlatNop();
784 left.getEnd().addNext(fcb);
785 fcb.addTrueNext(right.getBegin());
786 right.getEnd().addNext(fon2);
788 fcb.addFalseNext(fon1);
790 return new NodePair(left.getBegin(), fnop);
791 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
792 //We have a string concatenate
793 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
794 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
795 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
796 left.getEnd().addNext(right.getBegin());
797 right.getEnd().addNext(fc);
798 return new NodePair(left.getBegin(), fc);
801 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
802 left.getEnd().addNext(right.getBegin());
803 right.getEnd().addNext(fon);
804 return new NodePair(left.getBegin(),fon);
807 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
809 case Kind.AssignmentNode:
810 return flattenAssignmentNode((AssignmentNode)en,out_temp);
813 return flattenCastNode((CastNode)en,out_temp);
815 case Kind.CreateObjectNode:
816 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
818 case Kind.FieldAccessNode:
819 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
821 case Kind.ArrayAccessNode:
822 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
824 case Kind.LiteralNode:
825 return flattenLiteralNode((LiteralNode)en,out_temp);
827 case Kind.MethodInvokeNode:
828 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
831 return flattenNameNode((NameNode)en,out_temp);
834 return flattenOpNode((OpNode)en,out_temp);
836 case Kind.OffsetNode:
837 return flattenOffsetNode((OffsetNode)en,out_temp);
839 case Kind.TertiaryNode:
840 return flattenTertiaryNode((TertiaryNode)en,out_temp);
842 case Kind.InstanceOfNode:
843 return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
845 case Kind.ArrayInitializerNode:
846 return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
851 private NodePair flattenDeclarationNode(DeclarationNode dn) {
852 VarDescriptor vd=dn.getVarDescriptor();
853 TempDescriptor td=getTempforVar(vd);
854 if (dn.getExpression()!=null)
855 return flattenExpressionNode(dn.getExpression(),td);
857 FlatNop fn=new FlatNop();
858 return new NodePair(fn,fn);
862 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
863 TagVarDescriptor tvd=dn.getTagVarDescriptor();
864 TagDescriptor tag=tvd.getTag();
865 TempDescriptor tmp=getTempforVar(tvd);
866 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
867 return new NodePair(ftd,ftd);
870 private TempDescriptor getTempforParam(Descriptor d) {
871 if (temptovar.containsKey(d))
872 return (TempDescriptor)temptovar.get(d);
874 if (d instanceof VarDescriptor) {
875 VarDescriptor vd=(VarDescriptor)d;
876 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
877 temptovar.put(vd,td);
879 } else if (d instanceof TagVarDescriptor) {
880 TagVarDescriptor tvd=(TagVarDescriptor)d;
881 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
882 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
883 temptovar.put(tvd,td);
885 } else throw new Error("Unreconized Descriptor");
889 private TempDescriptor getTempforVar(Descriptor d) {
890 if (temptovar.containsKey(d))
891 return (TempDescriptor)temptovar.get(d);
893 if (d instanceof VarDescriptor) {
894 VarDescriptor vd=(VarDescriptor)d;
895 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
896 temptovar.put(vd,td);
898 } else if (d instanceof TagVarDescriptor) {
899 TagVarDescriptor tvd=(TagVarDescriptor)d;
900 //BUGFIX TAGTYPE - add next line, modify following
901 //line to tag this new type descriptor, modify
902 //TempDescriptor constructor & factory to set type
903 //using this Type To test, use any program with tags
904 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
905 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
906 temptovar.put(tvd,td);
908 } else throw new Error("Unrecognized Descriptor");
912 private NodePair flattenIfStatementNode(IfStatementNode isn) {
913 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
914 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
915 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
916 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
918 FlatNop nopend=new FlatNop();
920 if (isn.getFalseBlock()!=null)
921 false_np=flattenBlockNode(isn.getFalseBlock());
923 FlatNop nop=new FlatNop();
924 false_np=new NodePair(nop,nop);
927 cond.getEnd().addNext(fcb);
928 fcb.addTrueNext(true_np.getBegin());
929 fcb.addFalseNext(false_np.getBegin());
930 if (true_np.getEnd()!=null)
931 true_np.getEnd().addNext(nopend);
932 if (false_np.getEnd()!=null)
933 false_np.getEnd().addNext(nopend);
934 return new NodePair(cond.getBegin(), nopend);
937 private NodePair flattenLoopNode(LoopNode ln) {
938 HashSet oldbs=breakset;
939 HashSet oldcs=continueset;
940 breakset=new HashSet();
941 continueset=new HashSet();
943 if (ln.getType()==LoopNode.FORLOOP) {
944 NodePair initializer=flattenBlockNode(ln.getInitializer());
945 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
946 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
947 NodePair update=flattenBlockNode(ln.getUpdate());
948 NodePair body=flattenBlockNode(ln.getBody());
949 FlatNode begin=initializer.getBegin();
950 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
951 fcb.setTrueProb(State.TRUEPROB);
953 FlatNop nopend=new FlatNop();
954 FlatBackEdge backedge=new FlatBackEdge();
956 FlatNop nop2=new FlatNop();
957 initializer.getEnd().addNext(nop2);
958 nop2.addNext(condition.getBegin());
959 if (body.getEnd()!=null)
960 body.getEnd().addNext(update.getBegin());
961 update.getEnd().addNext(backedge);
962 backedge.addNext(condition.getBegin());
963 condition.getEnd().addNext(fcb);
964 fcb.addFalseNext(nopend);
965 fcb.addTrueNext(body.getBegin());
966 for(Iterator contit=continueset.iterator();contit.hasNext();) {
967 FlatNode fn=(FlatNode)contit.next();
969 fn.addNext(update.getBegin());
971 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
972 FlatNode fn=(FlatNode)breakit.next();
978 return new NodePair(begin,nopend);
979 } else if (ln.getType()==LoopNode.WHILELOOP) {
980 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
981 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
982 NodePair body=flattenBlockNode(ln.getBody());
983 FlatNode begin=condition.getBegin();
984 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
985 fcb.setTrueProb(State.TRUEPROB);
987 FlatNop nopend=new FlatNop();
988 FlatBackEdge backedge=new FlatBackEdge();
990 if (body.getEnd()!=null)
991 body.getEnd().addNext(backedge);
992 backedge.addNext(condition.getBegin());
994 condition.getEnd().addNext(fcb);
995 fcb.addFalseNext(nopend);
996 fcb.addTrueNext(body.getBegin());
998 for(Iterator contit=continueset.iterator();contit.hasNext();) {
999 FlatNode fn=(FlatNode)contit.next();
1001 fn.addNext(backedge);
1003 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1004 FlatNode fn=(FlatNode)breakit.next();
1010 return new NodePair(begin,nopend);
1011 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
1012 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1013 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1014 NodePair body=flattenBlockNode(ln.getBody());
1015 FlatNode begin=body.getBegin();
1016 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1017 fcb.setTrueProb(State.TRUEPROB);
1019 FlatNop nopend=new FlatNop();
1020 FlatBackEdge backedge=new FlatBackEdge();
1022 if (body.getEnd()!=null)
1023 body.getEnd().addNext(condition.getBegin());
1024 condition.getEnd().addNext(fcb);
1025 fcb.addFalseNext(nopend);
1026 fcb.addTrueNext(backedge);
1027 backedge.addNext(body.getBegin());
1029 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1030 FlatNode fn=(FlatNode)contit.next();
1032 fn.addNext(condition.getBegin());
1034 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1035 FlatNode fn=(FlatNode)breakit.next();
1041 return new NodePair(begin,nopend);
1042 } else throw new Error();
1045 private NodePair flattenReturnNode(ReturnNode rntree) {
1046 TempDescriptor retval=null;
1048 if (rntree.getReturnExpression()!=null) {
1049 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
1050 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1053 FlatReturnNode rnflat=new FlatReturnNode(retval);
1056 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
1057 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1058 TempDescriptor thistd=getTempforVar(currmd.getThis());
1059 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
1063 if (state.DSM&&currmd.getModifiers().isAtomic()) {
1064 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1070 cond.getEnd().addNext(ln);
1071 return new NodePair(cond.getBegin(),null);
1073 return new NodePair(ln,null);
1076 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1077 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1078 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1079 updateFlagActionNode(ffan, ten.getFlagEffects());
1080 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1081 ffan.addNext(fcn.getBegin());
1082 FlatReturnNode rnflat=new FlatReturnNode(null);
1084 fcn.getEnd().addNext(rnflat);
1085 return new NodePair(ffan, null);
1088 private NodePair flattenConstraintCheck(Vector ccs) {
1089 FlatNode begin=new FlatNop();
1091 return new NodePair(begin,begin);
1092 FlatNode last=begin;
1093 for(int i=0; i<ccs.size(); i++) {
1094 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1095 /* Flatten the arguments */
1096 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1097 String[] vars=new String[cc.numArgs()];
1098 for(int j=0; j<cc.numArgs(); j++) {
1099 ExpressionNode en=cc.getArg(j);
1100 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1102 vars[j]=cc.getVar(j);
1103 NodePair np=flattenExpressionNode(en, td);
1104 last.addNext(np.getBegin());
1108 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1112 return new NodePair(begin,last);
1115 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1116 return flattenBlockNode(sbn.getBlockNode());
1119 private NodePair flattenAtomicNode(AtomicNode sbn) {
1120 NodePair np=flattenBlockNode(sbn.getBlockNode());
1121 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1122 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1123 faen.addNext(np.getBegin());
1124 np.getEnd().addNext(faexn);
1125 return new NodePair(faen, faexn);
1128 private NodePair flattenSESENode(SESENode sn) {
1129 if( sn.isStart() ) {
1130 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1131 sn.setFlatEnter(fsen);
1132 return new NodePair(fsen, fsen);
1135 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1136 sn.setFlatExit(fsexn);
1137 FlatSESEEnterNode fsen=sn.getStart().getFlatEnter();
1138 fsexn.setFlatEnter(fsen);
1139 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1140 return new NodePair(fsexn, fsexn);
1143 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1144 FlatNop fn=new FlatNop();
1148 continueset.add(fn);
1149 return new NodePair(fn,null);
1152 private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1153 TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1154 NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1155 FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1156 cond.getEnd().addNext(fion);
1157 return new NodePair(cond.getBegin(),fion);
1160 private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1162 TempDescriptor expr_temp=TempDescriptor.tempFactory("arry_init",ain.getType());
1164 // create a new array of size equal to the array initializer
1165 //FlatNode first=null;
1166 //FlatNode last=null;
1167 TempDescriptor[] temps=new TempDescriptor[ain.numVarInitializers()];
1169 for (int i=0; i<con.numArgs(); i++) {
1170 ExpressionNode en=con.getArg(i);
1171 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
1173 NodePair np=flattenExpressionNode(en, tmp);
1175 first=np.getBegin();
1177 last.addNext(np.getBegin());
1180 TempDescriptor tmp2=(i==0) ?
1182 TempDescriptor.tempFactory("arg",en.getType());
1184 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
1188 // assign each element of the new array to the flattened expression
1191 FlatOpNode fonAssignArray=new FlatOpNode(out_temp, newarry_temp, null, new Operation(Operation.ASSIGN));
1193 //return new NodePair( , fonAssignArray );
1195 System.out.println( "Array initializers not implemented yet." );
1200 private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1201 TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1202 TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1203 TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1205 NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1206 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1208 NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1209 FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1211 NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1212 FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1214 FlatNop nopend=new FlatNop();
1216 cond.getEnd().addNext(fcb);
1218 fcb.addTrueNext(trueExpr.getBegin());
1219 fcb.addFalseNext(falseExpr.getBegin());
1221 trueExpr.getEnd().addNext(fonT);
1222 fonT.addNext(nopend);
1224 falseExpr.getEnd().addNext(fonF);
1225 fonF.addNext(nopend);
1227 return new NodePair(cond.getBegin(), nopend);
1230 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1231 switch(bsn.kind()) {
1232 case Kind.BlockExpressionNode:
1233 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1235 case Kind.DeclarationNode:
1236 return flattenDeclarationNode((DeclarationNode)bsn);
1238 case Kind.TagDeclarationNode:
1239 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1241 case Kind.IfStatementNode:
1242 return flattenIfStatementNode((IfStatementNode)bsn);
1245 return flattenLoopNode((LoopNode)bsn);
1247 case Kind.ReturnNode:
1248 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1250 case Kind.TaskExitNode:
1251 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1253 case Kind.SubBlockNode:
1254 return flattenSubBlockNode((SubBlockNode)bsn);
1256 case Kind.AtomicNode:
1257 return flattenAtomicNode((AtomicNode)bsn);
1260 return flattenSESENode((SESENode)bsn);
1262 case Kind.ContinueBreakNode:
1263 return flattenContinueBreakNode((ContinueBreakNode)bsn);