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 FlatNew fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
225 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
228 for(int i=0; i<con.numArgs(); i++) {
229 ExpressionNode en=con.getArg(i);
230 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
232 NodePair np=flattenExpressionNode(en, tmp);
233 last.addNext(np.getBegin());
236 MethodDescriptor md=con.getConstructor();
237 //Call to constructor
238 FlatCall fc=new FlatCall(md, null, out_temp, temps);
241 if (td.getClassDesc().hasFlags()) {
242 // if (con.getFlagEffects()!=null) {
243 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
244 FlagEffects fes=con.getFlagEffects();
245 TempDescriptor flagtemp=out_temp;
247 for(int j=0; j<fes.numEffects(); j++) {
248 FlagEffect fe=fes.getEffect(j);
249 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
251 for(int j=0; j<fes.numTagEffects(); j++) {
252 TagEffect te=fes.getTagEffect(j);
253 TempDescriptor tagtemp=getTempforVar(te.getTag());
255 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
258 ffan.addFlagAction(flagtemp, null, false);
263 return new NodePair(fn,last);
267 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
268 for (int i=0; i<con.numArgs(); i++) {
269 ExpressionNode en=con.getArg(i);
270 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
272 NodePair np=flattenExpressionNode(en, tmp);
276 last.addNext(np.getBegin());
279 TempDescriptor tmp2=(i==0) ?
281 TempDescriptor.tempFactory("arg",en.getType());
283 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
285 if (temps.length>1) {
286 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
287 fn.addNext(np.getBegin());
288 return new NodePair(first,np.getEnd());
289 } else if (td.isArray()&&td.dereference().iswrapper()) {
290 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
291 fn.addNext(np.getBegin());
292 return new NodePair(first,np.getEnd());
294 return new NodePair(first, fn);
298 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
299 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
300 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
301 FlatNop fnop=new FlatNop(); //last node
304 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
306 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
308 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
310 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
311 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
312 fcb.setTrueProb(State.TRUEPROB);
315 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
316 FlatNew fn=td.iswrapper()?new FlatNew(td, new_tmp, isglobal):new FlatNew(td, new_tmp, temparray[i+1], isglobal);
317 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
319 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
325 fcb.addFalseNext(fnop);
327 //Recursive call here
328 if ((i+2)<temparray.length) {
329 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
330 fsen.addNext(np2.getBegin());
331 np2.getEnd().addNext(fon);
332 } else if (td.isArray()&&td.dereference().iswrapper()) {
333 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
334 fsen.addNext(np2.getBegin());
335 np2.getEnd().addNext(fon);
340 return new NodePair(fln, fnop);
343 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
344 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
347 TempDescriptor thisarg=null;
349 if (min.getExpression()!=null) {
350 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
351 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
357 for(int i=0; i<min.numArgs(); i++) {
358 ExpressionNode en=min.getArg(i);
359 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
361 NodePair np=flattenExpressionNode(en, td);
365 last.addNext(np.getBegin());
369 MethodDescriptor md=min.getMethod();
371 //Call to constructor
374 if(md.getReturnType()==null||md.getReturnType().isVoid())
375 fc=new FlatCall(md, null, thisarg, temps);
377 fc=new FlatCall(md, out_temp, thisarg, temps);
382 return new NodePair(first,fc);
385 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
386 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
387 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
388 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
389 npe.getEnd().addNext(fn);
390 return new NodePair(npe.getBegin(),fn);
393 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
394 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
395 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
396 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
397 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
398 TempDescriptor arraytmp=out_temp;
399 if (aan.iswrapper()) {
401 arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference());
403 FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp);
404 npe.getEnd().addNext(npi.getBegin());
405 npi.getEnd().addNext(fn);
406 if (aan.iswrapper()) {
407 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp);
411 return new NodePair(npe.getBegin(),fn);
414 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
416 // left side is variable
417 // left side is field
418 // left side is array
420 Operation base=an.getOperation().getBaseOp();
421 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
424 //rewrite the base operation
425 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
429 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
432 if (an.getSrc()!=null) {
433 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
434 first=np_src.getBegin();
435 last=np_src.getEnd();
437 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
442 if (an.getDest().kind()==Kind.FieldAccessNode) {
443 //We are assigning an object field
445 FieldAccessNode fan=(FieldAccessNode)an.getDest();
446 ExpressionNode en=fan.getExpression();
447 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
448 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
450 first=np_baseexp.getBegin();
452 last.addNext(np_baseexp.getBegin());
453 last=np_baseexp.getEnd();
455 //See if we need to perform an operation
457 //If it is a preinc we need to store the initial value
458 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
459 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
460 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
464 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
465 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
466 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
467 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
472 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
479 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
483 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
487 return new NodePair(first, last);
488 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
489 //We are assigning an array element
491 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
492 ExpressionNode en=aan.getExpression();
493 ExpressionNode enindex=aan.getIndex();
494 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
495 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
496 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
497 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
499 first=np_baseexp.getBegin();
501 last.addNext(np_baseexp.getBegin());
502 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
503 last=np_indexexp.getEnd();
505 //See if we need to perform an operation
507 //If it is a preinc we need to store the initial value
508 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
509 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
511 if (aan.iswrapper()) {
512 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
513 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
514 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
515 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp3,src_tmp2);
520 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
524 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
525 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
526 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
527 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
532 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
539 if (aan.iswrapper()) {
540 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
541 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
542 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
543 FlatSetFieldNode fsfn=new FlatSetFieldNode(src_tmp3,(FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp);
548 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
553 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
557 return new NodePair(first, last);
558 } else if (an.getDest().kind()==Kind.NameNode) {
559 //We could be assigning a field or variable
560 NameNode nn=(NameNode)an.getDest();
563 if (nn.getExpression()!=null) {
565 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
566 ExpressionNode en=fan.getExpression();
567 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
568 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
570 first=np_baseexp.getBegin();
572 last.addNext(np_baseexp.getBegin());
573 last=np_baseexp.getEnd();
575 //See if we need to perform an operation
577 //If it is a preinc we need to store the initial value
578 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
579 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
581 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
586 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
587 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
588 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
589 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
594 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
602 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
606 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
610 return new NodePair(first, last);
612 if (nn.getField()!=null) {
616 //See if we need to perform an operation
618 //If it is a preinc we need to store the initial value
619 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
620 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
622 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
631 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
632 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
633 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
634 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
639 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
646 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
654 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
658 return new NodePair(first, last);
661 //See if we need to perform an operation
664 //If it is a preinc we need to store the initial value
665 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
666 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
668 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
677 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
678 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
679 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
680 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
688 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
698 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
703 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
707 return new NodePair(first, last);
715 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
716 if (nn.getExpression()!=null) {
717 /* Hack - use subtree instead */
718 return flattenExpressionNode(nn.getExpression(),out_temp);
719 } else if (nn.getField()!=null) {
720 TempDescriptor tmp=getTempforVar(nn.getVar());
721 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
722 return new NodePair(ffn,ffn);
724 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
727 out_temp.setTag(tmp.getTag());
729 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
730 return new NodePair(fon,fon);
734 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
735 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
736 TempDescriptor temp_right=null;
738 Operation op=on.getOp();
740 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
742 if (on.getRight()!=null) {
743 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
744 right=flattenExpressionNode(on.getRight(),temp_right);
746 FlatNop nop=new FlatNop();
747 right=new NodePair(nop,nop);
750 if (op.getOp()==Operation.LOGIC_OR) {
751 /* Need to do shortcircuiting */
752 FlatCondBranch fcb=new FlatCondBranch(temp_left);
753 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
754 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
755 FlatNop fnop=new FlatNop();
756 left.getEnd().addNext(fcb);
757 fcb.addFalseNext(right.getBegin());
758 right.getEnd().addNext(fon2);
760 fcb.addTrueNext(fon1);
762 return new NodePair(left.getBegin(), fnop);
763 } else if (op.getOp()==Operation.LOGIC_AND) {
764 /* Need to do shortcircuiting */
765 FlatCondBranch fcb=new FlatCondBranch(temp_left);
766 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
767 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
768 FlatNop fnop=new FlatNop();
769 left.getEnd().addNext(fcb);
770 fcb.addTrueNext(right.getBegin());
771 right.getEnd().addNext(fon2);
773 fcb.addFalseNext(fon1);
775 return new NodePair(left.getBegin(), fnop);
776 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
777 //We have a string concatenate
778 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
779 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
780 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
781 left.getEnd().addNext(right.getBegin());
782 right.getEnd().addNext(fc);
783 return new NodePair(left.getBegin(), fc);
786 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
787 left.getEnd().addNext(right.getBegin());
788 right.getEnd().addNext(fon);
789 return new NodePair(left.getBegin(),fon);
792 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
794 case Kind.AssignmentNode:
795 return flattenAssignmentNode((AssignmentNode)en,out_temp);
798 return flattenCastNode((CastNode)en,out_temp);
800 case Kind.CreateObjectNode:
801 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
803 case Kind.FieldAccessNode:
804 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
806 case Kind.ArrayAccessNode:
807 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
809 case Kind.LiteralNode:
810 return flattenLiteralNode((LiteralNode)en,out_temp);
812 case Kind.MethodInvokeNode:
813 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
816 return flattenNameNode((NameNode)en,out_temp);
819 return flattenOpNode((OpNode)en,out_temp);
821 case Kind.OffsetNode:
822 return flattenOffsetNode((OffsetNode)en,out_temp);
824 case Kind.TertiaryNode:
825 return flattenTertiaryNode((TertiaryNode)en,out_temp);
827 case Kind.InstanceOfNode:
828 return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
830 case Kind.ArrayInitializerNode:
831 return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
836 private NodePair flattenDeclarationNode(DeclarationNode dn) {
837 VarDescriptor vd=dn.getVarDescriptor();
838 TempDescriptor td=getTempforVar(vd);
839 if (dn.getExpression()!=null)
840 return flattenExpressionNode(dn.getExpression(),td);
842 FlatNop fn=new FlatNop();
843 return new NodePair(fn,fn);
847 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
848 TagVarDescriptor tvd=dn.getTagVarDescriptor();
849 TagDescriptor tag=tvd.getTag();
850 TempDescriptor tmp=getTempforVar(tvd);
851 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
852 return new NodePair(ftd,ftd);
855 private TempDescriptor getTempforParam(Descriptor d) {
856 if (temptovar.containsKey(d))
857 return (TempDescriptor)temptovar.get(d);
859 if (d instanceof VarDescriptor) {
860 VarDescriptor vd=(VarDescriptor)d;
861 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
862 temptovar.put(vd,td);
864 } else if (d instanceof TagVarDescriptor) {
865 TagVarDescriptor tvd=(TagVarDescriptor)d;
866 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
867 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
868 temptovar.put(tvd,td);
870 } else throw new Error("Unreconized Descriptor");
874 private TempDescriptor getTempforVar(Descriptor d) {
875 if (temptovar.containsKey(d))
876 return (TempDescriptor)temptovar.get(d);
878 if (d instanceof VarDescriptor) {
879 VarDescriptor vd=(VarDescriptor)d;
880 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
881 temptovar.put(vd,td);
883 } else if (d instanceof TagVarDescriptor) {
884 TagVarDescriptor tvd=(TagVarDescriptor)d;
885 //BUGFIX TAGTYPE - add next line, modify following
886 //line to tag this new type descriptor, modify
887 //TempDescriptor constructor & factory to set type
888 //using this Type To test, use any program with tags
889 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
890 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
891 temptovar.put(tvd,td);
893 } else throw new Error("Unrecognized Descriptor");
897 private NodePair flattenIfStatementNode(IfStatementNode isn) {
898 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
899 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
900 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
901 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
903 FlatNop nopend=new FlatNop();
905 if (isn.getFalseBlock()!=null)
906 false_np=flattenBlockNode(isn.getFalseBlock());
908 FlatNop nop=new FlatNop();
909 false_np=new NodePair(nop,nop);
912 cond.getEnd().addNext(fcb);
913 fcb.addTrueNext(true_np.getBegin());
914 fcb.addFalseNext(false_np.getBegin());
915 if (true_np.getEnd()!=null)
916 true_np.getEnd().addNext(nopend);
917 if (false_np.getEnd()!=null)
918 false_np.getEnd().addNext(nopend);
919 return new NodePair(cond.getBegin(), nopend);
922 private NodePair flattenLoopNode(LoopNode ln) {
923 HashSet oldbs=breakset;
924 HashSet oldcs=continueset;
925 breakset=new HashSet();
926 continueset=new HashSet();
928 if (ln.getType()==LoopNode.FORLOOP) {
929 NodePair initializer=flattenBlockNode(ln.getInitializer());
930 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
931 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
932 NodePair update=flattenBlockNode(ln.getUpdate());
933 NodePair body=flattenBlockNode(ln.getBody());
934 FlatNode begin=initializer.getBegin();
935 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
936 fcb.setTrueProb(State.TRUEPROB);
938 FlatNop nopend=new FlatNop();
939 FlatBackEdge backedge=new FlatBackEdge();
941 FlatNop nop2=new FlatNop();
942 initializer.getEnd().addNext(nop2);
943 nop2.addNext(condition.getBegin());
944 if (body.getEnd()!=null)
945 body.getEnd().addNext(update.getBegin());
946 update.getEnd().addNext(backedge);
947 backedge.addNext(condition.getBegin());
948 condition.getEnd().addNext(fcb);
949 fcb.addFalseNext(nopend);
950 fcb.addTrueNext(body.getBegin());
951 for(Iterator contit=continueset.iterator();contit.hasNext();) {
952 FlatNode fn=(FlatNode)contit.next();
954 fn.addNext(update.getBegin());
956 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
957 FlatNode fn=(FlatNode)breakit.next();
963 return new NodePair(begin,nopend);
964 } else if (ln.getType()==LoopNode.WHILELOOP) {
965 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
966 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
967 NodePair body=flattenBlockNode(ln.getBody());
968 FlatNode begin=condition.getBegin();
969 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
970 fcb.setTrueProb(State.TRUEPROB);
972 FlatNop nopend=new FlatNop();
973 FlatBackEdge backedge=new FlatBackEdge();
975 if (body.getEnd()!=null)
976 body.getEnd().addNext(backedge);
977 backedge.addNext(condition.getBegin());
979 condition.getEnd().addNext(fcb);
980 fcb.addFalseNext(nopend);
981 fcb.addTrueNext(body.getBegin());
983 for(Iterator contit=continueset.iterator();contit.hasNext();) {
984 FlatNode fn=(FlatNode)contit.next();
986 fn.addNext(backedge);
988 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
989 FlatNode fn=(FlatNode)breakit.next();
995 return new NodePair(begin,nopend);
996 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
997 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
998 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
999 NodePair body=flattenBlockNode(ln.getBody());
1000 FlatNode begin=body.getBegin();
1001 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1002 fcb.setTrueProb(State.TRUEPROB);
1004 FlatNop nopend=new FlatNop();
1005 FlatBackEdge backedge=new FlatBackEdge();
1007 if (body.getEnd()!=null)
1008 body.getEnd().addNext(condition.getBegin());
1009 condition.getEnd().addNext(fcb);
1010 fcb.addFalseNext(nopend);
1011 fcb.addTrueNext(backedge);
1012 backedge.addNext(body.getBegin());
1014 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1015 FlatNode fn=(FlatNode)contit.next();
1017 fn.addNext(condition.getBegin());
1019 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1020 FlatNode fn=(FlatNode)breakit.next();
1026 return new NodePair(begin,nopend);
1027 } else throw new Error();
1030 private NodePair flattenReturnNode(ReturnNode rntree) {
1031 TempDescriptor retval=null;
1033 if (rntree.getReturnExpression()!=null) {
1034 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
1035 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1038 FlatReturnNode rnflat=new FlatReturnNode(retval);
1041 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
1042 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1043 TempDescriptor thistd=getTempforVar(currmd.getThis());
1044 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
1048 if (state.DSM&&currmd.getModifiers().isAtomic()) {
1049 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1055 cond.getEnd().addNext(ln);
1056 return new NodePair(cond.getBegin(),null);
1058 return new NodePair(ln,null);
1061 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1062 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1063 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1064 updateFlagActionNode(ffan, ten.getFlagEffects());
1065 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1066 ffan.addNext(fcn.getBegin());
1067 FlatReturnNode rnflat=new FlatReturnNode(null);
1069 fcn.getEnd().addNext(rnflat);
1070 return new NodePair(ffan, null);
1073 private NodePair flattenConstraintCheck(Vector ccs) {
1074 FlatNode begin=new FlatNop();
1076 return new NodePair(begin,begin);
1077 FlatNode last=begin;
1078 for(int i=0; i<ccs.size(); i++) {
1079 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1080 /* Flatten the arguments */
1081 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1082 String[] vars=new String[cc.numArgs()];
1083 for(int j=0; j<cc.numArgs(); j++) {
1084 ExpressionNode en=cc.getArg(j);
1085 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1087 vars[j]=cc.getVar(j);
1088 NodePair np=flattenExpressionNode(en, td);
1089 last.addNext(np.getBegin());
1093 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1097 return new NodePair(begin,last);
1100 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1101 return flattenBlockNode(sbn.getBlockNode());
1104 private NodePair flattenAtomicNode(AtomicNode sbn) {
1105 NodePair np=flattenBlockNode(sbn.getBlockNode());
1106 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1107 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1108 faen.addNext(np.getBegin());
1109 np.getEnd().addNext(faexn);
1110 return new NodePair(faen, faexn);
1113 private NodePair flattenSESENode(SESENode sn) {
1114 if( sn.isStart() ) {
1115 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1116 sn.setFlatEnter(fsen);
1117 return new NodePair(fsen, fsen);
1120 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1121 sn.setFlatExit(fsexn);
1122 FlatSESEEnterNode fsen=sn.getStart().getFlatEnter();
1123 fsexn.setFlatEnter(fsen);
1124 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1125 return new NodePair(fsexn, fsexn);
1128 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1129 FlatNop fn=new FlatNop();
1133 continueset.add(fn);
1134 return new NodePair(fn,null);
1137 private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1138 TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1139 NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1140 FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1141 cond.getEnd().addNext(fion);
1142 return new NodePair(cond.getBegin(),fion);
1145 private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1147 TempDescriptor expr_temp=TempDescriptor.tempFactory("arry_init",ain.getType());
1149 // create a new array of size equal to the array initializer
1150 //FlatNode first=null;
1151 //FlatNode last=null;
1152 TempDescriptor[] temps=new TempDescriptor[ain.numVarInitializers()];
1154 for (int i=0; i<con.numArgs(); i++) {
1155 ExpressionNode en=con.getArg(i);
1156 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
1158 NodePair np=flattenExpressionNode(en, tmp);
1160 first=np.getBegin();
1162 last.addNext(np.getBegin());
1165 TempDescriptor tmp2=(i==0) ?
1167 TempDescriptor.tempFactory("arg",en.getType());
1169 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
1173 // assign each element of the new array to the flattened expression
1176 FlatOpNode fonAssignArray=new FlatOpNode(out_temp, newarry_temp, null, new Operation(Operation.ASSIGN));
1178 //return new NodePair( , fonAssignArray );
1180 System.out.println( "Array initializers not implemented yet." );
1185 private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1186 TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1187 TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1188 TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1190 NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1191 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1193 NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1194 FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1196 NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1197 FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1199 FlatNop nopend=new FlatNop();
1201 cond.getEnd().addNext(fcb);
1203 fcb.addTrueNext(trueExpr.getBegin());
1204 fcb.addFalseNext(falseExpr.getBegin());
1206 trueExpr.getEnd().addNext(fonT);
1207 fonT.addNext(nopend);
1209 falseExpr.getEnd().addNext(fonF);
1210 fonF.addNext(nopend);
1212 return new NodePair(cond.getBegin(), nopend);
1215 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1216 switch(bsn.kind()) {
1217 case Kind.BlockExpressionNode:
1218 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1220 case Kind.DeclarationNode:
1221 return flattenDeclarationNode((DeclarationNode)bsn);
1223 case Kind.TagDeclarationNode:
1224 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1226 case Kind.IfStatementNode:
1227 return flattenIfStatementNode((IfStatementNode)bsn);
1230 return flattenLoopNode((LoopNode)bsn);
1232 case Kind.ReturnNode:
1233 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1235 case Kind.TaskExitNode:
1236 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1238 case Kind.SubBlockNode:
1239 return flattenSubBlockNode((SubBlockNode)bsn);
1241 case Kind.AtomicNode:
1242 return flattenAtomicNode((AtomicNode)bsn);
1245 return flattenSESENode((SESENode)bsn);
1247 case Kind.ContinueBreakNode:
1248 return flattenContinueBreakNode((ContinueBreakNode)bsn);