6 public class BuildFlat {
9 MethodDescriptor currmd;
12 public BuildFlat(State st, TypeUtil typeutil) {
14 temptovar=new Hashtable();
15 this.typeutil=typeutil;
18 public Hashtable getMap() {
22 public void buildFlat() {
23 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
25 ClassDescriptor cn=(ClassDescriptor)it.next();
29 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
30 while(task_it.hasNext()) {
31 TaskDescriptor td=(TaskDescriptor)task_it.next();
36 private void flattenTask(TaskDescriptor td) {
37 BlockNode bn=state.getMethodBody(td);
38 NodePair np=flattenBlockNode(bn);
39 FlatNode fn=np.getBegin();
40 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
41 FlatReturnNode rnflat=new FlatReturnNode(null);
42 np.getEnd().addNext(rnflat);
45 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
48 FlatMethod fm=new FlatMethod(td);
51 Hashtable visitedset=new Hashtable();
53 for(int i=0; i<td.numParameters(); i++) {
54 VarDescriptor paramvd=td.getParameter(i);
55 fm.addParameterTemp(getTempforVar(paramvd));
56 TagExpressionList tel=td.getTag(paramvd);
57 //BUG added next line to fix...to test feed in any task program
59 for(int j=0; j<tel.numTags(); j++) {
60 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
61 TempDescriptor tagtmp=getTempforVar(tvd);
62 if (!visitedset.containsKey(tvd.getName())) {
63 visitedset.put(tvd.getName(),tvd.getTag());
64 fm.addTagTemp(tagtmp);
66 TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
67 if (!tmptd.equals(tvd.getTag()))
68 throw new Error("Two different tag types with same name as parameters to:"+td);
70 tel.setTemp(j, tagtmp);
74 /* Flatten Vector of Flag Effects */
75 Vector flags=td.getFlagEffects();
76 updateFlagActionNode(ffan,flags);
78 state.addFlatCode(td,fm);
82 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
83 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
84 if (flags==null) // Do nothing if the flag effects vector is empty
87 for(int i=0; i<flags.size(); i++) {
88 FlagEffects fes=(FlagEffects)flags.get(i);
89 TempDescriptor flagtemp=getTempforVar(fes.getVar());
91 for(int j=0; j<fes.numEffects(); j++) {
92 FlagEffect fe=fes.getEffect(j);
93 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
96 for(int j=0; j<fes.numTagEffects(); j++) {
97 TagEffect te=fes.getTagEffect(j);
98 TempDescriptor tagtemp=getTempforVar(te.getTag());
100 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
105 FlatAtomicEnterNode curran=null;
107 private void flattenClass(ClassDescriptor cn) {
108 Iterator methodit=cn.getMethods();
109 while(methodit.hasNext()) {
110 currmd=(MethodDescriptor)methodit.next();
111 BlockNode bn=state.getMethodBody(currmd);
113 if (state.DSM&&currmd.getModifiers().isAtomic()) {
114 curran=new FlatAtomicEnterNode();
117 NodePair np=flattenBlockNode(bn);
118 FlatNode fn=np.getBegin();
119 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
120 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
121 TempDescriptor thistd=getTempforVar(currmd.getThis());
122 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
125 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
126 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
127 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
128 np.getEnd().addNext(fcunlock);
129 FlatReturnNode rnflat=new FlatReturnNode(null);
130 fcunlock.addNext(rnflat);
132 } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
135 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
136 FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
137 np.getEnd().addNext(aen);
138 FlatReturnNode rnflat=new FlatReturnNode(null);
141 } else if (np.getEnd().kind()!=FKind.FlatReturnNode) {
142 FlatReturnNode rnflat=new FlatReturnNode(null);
143 np.getEnd().addNext(rnflat);
146 FlatMethod fm=new FlatMethod(currmd);
148 if (!currmd.isStatic())
149 fm.addParameterTemp(getTempforParam(currmd.getThis()));
150 for(int i=0; i<currmd.numParameters(); i++) {
151 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
153 state.addFlatCode(currmd,fm);
157 private NodePair flattenBlockNode(BlockNode bn) {
160 for(int i=0; i<bn.size(); i++) {
161 NodePair np=flattenBlockStatementNode(bn.get(i));
162 FlatNode np_begin=np.getBegin();
163 FlatNode np_end=np.getEnd();
170 end.addNext(np_begin);
175 end=begin=new FlatNop();
177 return new NodePair(begin,end);
180 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
181 //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
182 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
183 return flattenExpressionNode(en.getExpression(),tmp);
186 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
187 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
188 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
189 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
190 np.getEnd().addNext(fcn);
191 return new NodePair(np.getBegin(),fcn);
194 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
195 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
196 return new NodePair(fln,fln);
199 private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
200 FlatLiteralNode fln = new FlatLiteralNode(ofn.getType(), ofn, out_temp);
201 return new NodePair(fln, fln);
202 /* Another possible approach
203 ClassDescriptor cd = ofn.getClassDesc();
204 FieldDescriptor fd = ofn.getField();
205 FlatOffsetNode fon = new FlatOffsetNode(fd, cd, out_temp);
206 TypeDescriptor type=new TypeDescriptor(ofn.getType());
207 System.out.println("TempDescriptor out_temp = " + out_temp.toString() + "Type of out_temp = " + out_temp.getType());
208 return new NodePair(fon, fon);
212 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
213 TypeDescriptor td=con.getType();
215 FlatNew fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
216 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
219 for(int i=0; i<con.numArgs(); i++) {
220 ExpressionNode en=con.getArg(i);
221 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
223 NodePair np=flattenExpressionNode(en, tmp);
224 last.addNext(np.getBegin());
227 MethodDescriptor md=con.getConstructor();
228 //Call to constructor
229 FlatCall fc=new FlatCall(md, null, out_temp, temps);
232 if (td.getClassDesc().hasFlags()) {
233 // if (con.getFlagEffects()!=null) {
234 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
235 FlagEffects fes=con.getFlagEffects();
236 TempDescriptor flagtemp=out_temp;
238 for(int j=0; j<fes.numEffects(); j++) {
239 FlagEffect fe=fes.getEffect(j);
240 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
242 for(int j=0; j<fes.numTagEffects(); j++) {
243 TagEffect te=fes.getTagEffect(j);
244 TempDescriptor tagtemp=getTempforVar(te.getTag());
246 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
249 ffan.addFlagAction(flagtemp, null, false);
254 return new NodePair(fn,last);
258 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
259 for (int i=0; i<con.numArgs(); i++) {
260 ExpressionNode en=con.getArg(i);
261 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
263 NodePair np=flattenExpressionNode(en, tmp);
267 last.addNext(np.getBegin());
270 TempDescriptor tmp2=(i==0) ?
272 TempDescriptor.tempFactory("arg",en.getType());
274 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
276 if (temps.length>1) {
277 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
278 fn.addNext(np.getBegin());
279 return new NodePair(first,np.getEnd());
281 return new NodePair(first, fn);
285 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
286 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
287 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
288 FlatNop fnop=new FlatNop(); //last node
291 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
293 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
295 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
297 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
298 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
299 fcb.setTrueProb(State.TRUEPROB);
302 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
303 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1], isglobal);
304 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
306 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
312 fcb.addFalseNext(fnop);
314 //Recursive call here
315 if ((i+2)<temparray.length) {
316 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
317 fsen.addNext(np2.getBegin());
318 np2.getEnd().addNext(fon);
323 return new NodePair(fln, fnop);
326 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
327 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
330 TempDescriptor thisarg=null;
332 if (min.getExpression()!=null) {
333 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
334 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
340 for(int i=0; i<min.numArgs(); i++) {
341 ExpressionNode en=min.getArg(i);
342 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
344 NodePair np=flattenExpressionNode(en, td);
348 last.addNext(np.getBegin());
352 MethodDescriptor md=min.getMethod();
354 //Call to constructor
357 if(md.getReturnType()==null||md.getReturnType().isVoid())
358 fc=new FlatCall(md, null, thisarg, temps);
360 fc=new FlatCall(md, out_temp, thisarg, temps);
365 return new NodePair(first,fc);
368 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
369 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
370 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
371 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
372 npe.getEnd().addNext(fn);
373 return new NodePair(npe.getBegin(),fn);
376 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
377 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
378 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
379 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
380 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
381 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
382 npe.getEnd().addNext(npi.getBegin());
383 npi.getEnd().addNext(fn);
384 return new NodePair(npe.getBegin(),fn);
387 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
389 // left side is variable
390 // left side is field
391 // left side is array
393 Operation base=an.getOperation().getBaseOp();
394 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
397 //rewrite the base operation
398 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
402 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
405 if (an.getSrc()!=null) {
406 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
407 first=np_src.getBegin();
408 last=np_src.getEnd();
410 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
415 if (an.getDest().kind()==Kind.FieldAccessNode) {
416 //We are assigning an object field
418 FieldAccessNode fan=(FieldAccessNode)an.getDest();
419 ExpressionNode en=fan.getExpression();
420 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
421 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
423 first=np_baseexp.getBegin();
425 last.addNext(np_baseexp.getBegin());
426 last=np_baseexp.getEnd();
428 //See if we need to perform an operation
430 //If it is a preinc we need to store the initial value
431 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
432 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
433 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
437 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
438 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
439 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
440 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
445 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
452 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
456 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
460 return new NodePair(first, last);
461 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
462 //We are assigning an array element
465 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
466 ExpressionNode en=aan.getExpression();
467 ExpressionNode enindex=aan.getIndex();
468 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
469 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
470 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
471 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
473 first=np_baseexp.getBegin();
475 last.addNext(np_baseexp.getBegin());
476 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
477 last=np_indexexp.getEnd();
479 //See if we need to perform an operation
481 //If it is a preinc we need to store the initial value
482 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
483 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
485 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
489 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
490 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
491 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
492 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
499 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
507 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
511 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
515 return new NodePair(first, last);
516 } else if (an.getDest().kind()==Kind.NameNode) {
517 //We could be assigning a field or variable
518 NameNode nn=(NameNode)an.getDest();
521 if (nn.getExpression()!=null) {
523 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
524 ExpressionNode en=fan.getExpression();
525 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
526 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
528 first=np_baseexp.getBegin();
530 last.addNext(np_baseexp.getBegin());
531 last=np_baseexp.getEnd();
533 //See if we need to perform an operation
535 //If it is a preinc we need to store the initial value
536 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
537 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
539 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
544 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
545 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
546 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
547 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
552 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
560 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
564 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
568 return new NodePair(first, last);
570 if (nn.getField()!=null) {
574 //See if we need to perform an operation
576 //If it is a preinc we need to store the initial value
577 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
578 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
580 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
589 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
590 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
591 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
592 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
597 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
604 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
612 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
616 return new NodePair(first, last);
619 //See if we need to perform an operation
622 //If it is a preinc we need to store the initial value
623 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
624 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
626 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
635 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
636 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
637 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
638 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
646 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
656 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
661 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
665 return new NodePair(first, last);
673 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
674 if (nn.getExpression()!=null) {
675 /* Hack - use subtree instead */
676 return flattenExpressionNode(nn.getExpression(),out_temp);
677 } else if (nn.getField()!=null) {
678 TempDescriptor tmp=getTempforVar(nn.getVar());
679 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
680 return new NodePair(ffn,ffn);
682 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
685 out_temp.setTag(tmp.getTag());
687 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
688 return new NodePair(fon,fon);
692 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
693 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
694 TempDescriptor temp_right=null;
696 Operation op=on.getOp();
698 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
700 if (on.getRight()!=null) {
701 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
702 right=flattenExpressionNode(on.getRight(),temp_right);
704 FlatNop nop=new FlatNop();
705 right=new NodePair(nop,nop);
708 if (op.getOp()==Operation.LOGIC_OR) {
709 /* Need to do shortcircuiting */
710 FlatCondBranch fcb=new FlatCondBranch(temp_left);
711 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
712 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
713 FlatNop fnop=new FlatNop();
714 left.getEnd().addNext(fcb);
715 fcb.addFalseNext(right.getBegin());
716 right.getEnd().addNext(fon2);
718 fcb.addTrueNext(fon1);
720 return new NodePair(left.getBegin(), fnop);
721 } else if (op.getOp()==Operation.LOGIC_AND) {
722 /* Need to do shortcircuiting */
723 FlatCondBranch fcb=new FlatCondBranch(temp_left);
724 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
725 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
726 FlatNop fnop=new FlatNop();
727 left.getEnd().addNext(fcb);
728 fcb.addTrueNext(right.getBegin());
729 right.getEnd().addNext(fon2);
731 fcb.addFalseNext(fon1);
733 return new NodePair(left.getBegin(), fnop);
734 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
735 //We have a string concatenate
736 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
737 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
738 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
739 left.getEnd().addNext(right.getBegin());
740 right.getEnd().addNext(fc);
741 return new NodePair(left.getBegin(), fc);
744 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
745 left.getEnd().addNext(right.getBegin());
746 right.getEnd().addNext(fon);
747 return new NodePair(left.getBegin(),fon);
750 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
752 case Kind.AssignmentNode:
753 return flattenAssignmentNode((AssignmentNode)en,out_temp);
756 return flattenCastNode((CastNode)en,out_temp);
758 case Kind.CreateObjectNode:
759 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
761 case Kind.FieldAccessNode:
762 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
764 case Kind.ArrayAccessNode:
765 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
767 case Kind.LiteralNode:
768 return flattenLiteralNode((LiteralNode)en,out_temp);
770 case Kind.MethodInvokeNode:
771 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
774 return flattenNameNode((NameNode)en,out_temp);
777 return flattenOpNode((OpNode)en,out_temp);
779 case Kind.OffsetNode:
780 return flattenOffsetNode((OffsetNode)en,out_temp);
785 private NodePair flattenDeclarationNode(DeclarationNode dn) {
786 VarDescriptor vd=dn.getVarDescriptor();
787 TempDescriptor td=getTempforVar(vd);
788 if (dn.getExpression()!=null)
789 return flattenExpressionNode(dn.getExpression(),td);
791 FlatNop fn=new FlatNop();
792 return new NodePair(fn,fn);
796 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
797 TagVarDescriptor tvd=dn.getTagVarDescriptor();
798 TagDescriptor tag=tvd.getTag();
799 TempDescriptor tmp=getTempforVar(tvd);
800 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
801 return new NodePair(ftd,ftd);
804 private TempDescriptor getTempforParam(Descriptor d) {
805 if (temptovar.containsKey(d))
806 return (TempDescriptor)temptovar.get(d);
808 if (d instanceof VarDescriptor) {
809 VarDescriptor vd=(VarDescriptor)d;
810 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
811 temptovar.put(vd,td);
813 } else if (d instanceof TagVarDescriptor) {
814 TagVarDescriptor tvd=(TagVarDescriptor)d;
815 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
816 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
817 temptovar.put(tvd,td);
819 } else throw new Error("Unreconized Descriptor");
823 private TempDescriptor getTempforVar(Descriptor d) {
824 if (temptovar.containsKey(d))
825 return (TempDescriptor)temptovar.get(d);
827 if (d instanceof VarDescriptor) {
828 VarDescriptor vd=(VarDescriptor)d;
829 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
830 temptovar.put(vd,td);
832 } else if (d instanceof TagVarDescriptor) {
833 TagVarDescriptor tvd=(TagVarDescriptor)d;
834 //BUGFIX TAGTYPE - add next line, modify following
835 //line to tag this new type descriptor, modify
836 //TempDescriptor constructor & factory to set type
837 //using this Type To test, use any program with tags
838 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
839 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
840 temptovar.put(tvd,td);
842 } else throw new Error("Unrecognized Descriptor");
846 private NodePair flattenIfStatementNode(IfStatementNode isn) {
847 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
848 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
849 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
850 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
852 FlatNop nopend=new FlatNop();
854 if (isn.getFalseBlock()!=null)
855 false_np=flattenBlockNode(isn.getFalseBlock());
857 FlatNop nop=new FlatNop();
858 false_np=new NodePair(nop,nop);
861 cond.getEnd().addNext(fcb);
862 fcb.addTrueNext(true_np.getBegin());
863 fcb.addFalseNext(false_np.getBegin());
864 true_np.getEnd().addNext(nopend);
865 false_np.getEnd().addNext(nopend);
866 return new NodePair(cond.getBegin(), nopend);
869 private NodePair flattenLoopNode(LoopNode ln) {
870 if (ln.getType()==LoopNode.FORLOOP) {
871 NodePair initializer=flattenBlockNode(ln.getInitializer());
872 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
873 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
874 NodePair update=flattenBlockNode(ln.getUpdate());
875 NodePair body=flattenBlockNode(ln.getBody());
876 FlatNode begin=initializer.getBegin();
877 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
878 fcb.setTrueProb(State.TRUEPROB);
880 FlatNop nopend=new FlatNop();
881 FlatBackEdge backedge=new FlatBackEdge();
883 FlatNop nop2=new FlatNop();
884 initializer.getEnd().addNext(nop2);
885 nop2.addNext(condition.getBegin());
886 body.getEnd().addNext(update.getBegin());
887 update.getEnd().addNext(backedge);
888 backedge.addNext(condition.getBegin());
889 condition.getEnd().addNext(fcb);
890 fcb.addFalseNext(nopend);
891 fcb.addTrueNext(body.getBegin());
892 return new NodePair(begin,nopend);
893 } else if (ln.getType()==LoopNode.WHILELOOP) {
894 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
895 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
896 NodePair body=flattenBlockNode(ln.getBody());
897 FlatNode begin=condition.getBegin();
898 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
899 fcb.setTrueProb(State.TRUEPROB);
901 FlatNop nopend=new FlatNop();
902 FlatBackEdge backedge=new FlatBackEdge();
904 body.getEnd().addNext(backedge);
905 backedge.addNext(condition.getBegin());
907 condition.getEnd().addNext(fcb);
908 fcb.addFalseNext(nopend);
909 fcb.addTrueNext(body.getBegin());
910 return new NodePair(begin,nopend);
911 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
912 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
913 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
914 NodePair body=flattenBlockNode(ln.getBody());
915 FlatNode begin=body.getBegin();
916 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
917 fcb.setTrueProb(State.TRUEPROB);
919 FlatNop nopend=new FlatNop();
920 FlatBackEdge backedge=new FlatBackEdge();
922 body.getEnd().addNext(condition.getBegin());
923 condition.getEnd().addNext(fcb);
924 fcb.addFalseNext(nopend);
925 fcb.addTrueNext(backedge);
926 backedge.addNext(body.getBegin());
927 return new NodePair(begin,nopend);
928 } else throw new Error();
931 private NodePair flattenReturnNode(ReturnNode rntree) {
932 TempDescriptor retval=null;
934 if (rntree.getReturnExpression()!=null) {
935 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
936 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
939 FlatReturnNode rnflat=new FlatReturnNode(retval);
941 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
942 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
943 TempDescriptor thistd=getTempforVar(currmd.getThis());
944 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
948 if (state.DSM&&currmd.getModifiers().isAtomic()) {
949 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
955 cond.getEnd().addNext(ln);
956 return new NodePair(cond.getBegin(),rnflat);
958 return new NodePair(ln,rnflat);
961 private NodePair flattenTaskExitNode(TaskExitNode ten) {
962 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
963 ffan.setTaskExitIndex(ten.getTaskExitIndex());
964 updateFlagActionNode(ffan, ten.getFlagEffects());
965 NodePair fcn=flattenConstraintCheck(ten.getChecks());
966 ffan.addNext(fcn.getBegin());
967 FlatReturnNode rnflat=new FlatReturnNode(null);
968 fcn.getEnd().addNext(rnflat);
969 return new NodePair(ffan, rnflat);
972 private NodePair flattenConstraintCheck(Vector ccs) {
973 FlatNode begin=new FlatNop();
975 return new NodePair(begin,begin);
977 for(int i=0; i<ccs.size(); i++) {
978 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
979 /* Flatten the arguments */
980 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
981 String[] vars=new String[cc.numArgs()];
982 for(int j=0; j<cc.numArgs(); j++) {
983 ExpressionNode en=cc.getArg(j);
984 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
986 vars[j]=cc.getVar(j);
987 NodePair np=flattenExpressionNode(en, td);
988 last.addNext(np.getBegin());
992 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
996 return new NodePair(begin,last);
999 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1000 return flattenBlockNode(sbn.getBlockNode());
1003 private NodePair flattenAtomicNode(AtomicNode sbn) {
1004 NodePair np=flattenBlockNode(sbn.getBlockNode());
1005 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1006 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1007 faen.addNext(np.getBegin());
1008 np.getEnd().addNext(faexn);
1009 return new NodePair(faen, faexn);
1012 private NodePair flattenSESENode(SESENode sn) {
1013 if( sn.isStart() ) {
1014 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1015 sn.setFlatEnter(fsen);
1016 return new NodePair(fsen, fsen);
1019 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1020 sn.setFlatExit(fsexn);
1021 fsexn.setFlatEnter( sn.getStart().getFlatEnter() );
1022 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1023 return new NodePair(fsexn, fsexn);
1026 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1027 switch(bsn.kind()) {
1028 case Kind.BlockExpressionNode:
1029 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1031 case Kind.DeclarationNode:
1032 return flattenDeclarationNode((DeclarationNode)bsn);
1034 case Kind.TagDeclarationNode:
1035 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1037 case Kind.IfStatementNode:
1038 return flattenIfStatementNode((IfStatementNode)bsn);
1041 return flattenLoopNode((LoopNode)bsn);
1043 case Kind.ReturnNode:
1044 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1046 case Kind.TaskExitNode:
1047 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1049 case Kind.SubBlockNode:
1050 return flattenSubBlockNode((SubBlockNode)bsn);
1052 case Kind.AtomicNode:
1053 return flattenAtomicNode((AtomicNode)bsn);
1056 return flattenSESENode((SESENode)bsn);