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 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
182 return flattenExpressionNode(en.getExpression(),tmp);
185 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
186 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
187 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
188 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
189 np.getEnd().addNext(fcn);
190 return new NodePair(np.getBegin(),fcn);
193 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
194 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
195 return new NodePair(fln,fln);
198 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
199 TypeDescriptor td=con.getType();
201 FlatNew fn=new FlatNew(td, out_temp, con.isGlobal());
202 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
205 for(int i=0;i<con.numArgs();i++) {
206 ExpressionNode en=con.getArg(i);
207 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
209 NodePair np=flattenExpressionNode(en, tmp);
210 last.addNext(np.getBegin());
213 MethodDescriptor md=con.getConstructor();
214 //Call to constructor
215 FlatCall fc=new FlatCall(md, null, out_temp, temps);
218 if (td.getClassDesc().hasFlags()) {
219 // if (con.getFlagEffects()!=null) {
220 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
221 FlagEffects fes=con.getFlagEffects();
222 TempDescriptor flagtemp=out_temp;
224 for(int j=0;j<fes.numEffects();j++) {
225 FlagEffect fe=fes.getEffect(j);
226 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
228 for(int j=0;j<fes.numTagEffects();j++) {
229 TagEffect te=fes.getTagEffect(j);
230 TempDescriptor tagtemp=getTempforVar(te.getTag());
232 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
235 ffan.addFlagAction(flagtemp, null, false);
240 return new NodePair(fn,last);
244 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
245 for (int i=0;i<con.numArgs();i++) {
246 ExpressionNode en=con.getArg(i);
247 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
249 NodePair np=flattenExpressionNode(en, tmp);
253 last.addNext(np.getBegin());
256 TempDescriptor tmp2=(i==0)?
258 TempDescriptor.tempFactory("arg",en.getType());
260 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal());
262 if (temps.length>1) {
263 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
264 fn.addNext(np.getBegin());
265 return new NodePair(first,np.getEnd());
267 return new NodePair(first, fn);
271 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
272 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
273 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
274 FlatNop fnop=new FlatNop();//last node
277 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
279 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
281 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
283 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
284 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
285 fcb.setTrueProb(State.TRUEPROB);
288 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
289 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1], isglobal);
290 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
292 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
298 fcb.addFalseNext(fnop);
300 //Recursive call here
301 if ((i+2)<temparray.length) {
302 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
303 fsen.addNext(np2.getBegin());
304 np2.getEnd().addNext(fon);
309 return new NodePair(fln, fnop);
312 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
313 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
316 TempDescriptor thisarg=null;
318 if (min.getExpression()!=null) {
319 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
320 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
326 for(int i=0;i<min.numArgs();i++) {
327 ExpressionNode en=min.getArg(i);
328 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
330 NodePair np=flattenExpressionNode(en, td);
334 last.addNext(np.getBegin());
338 MethodDescriptor md=min.getMethod();
340 //Call to constructor
343 if(md.getReturnType()==null||md.getReturnType().isVoid())
344 fc=new FlatCall(md, null, thisarg, temps);
346 fc=new FlatCall(md, out_temp, thisarg, temps);
351 return new NodePair(first,fc);
354 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
355 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
356 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
357 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
358 npe.getEnd().addNext(fn);
359 return new NodePair(npe.getBegin(),fn);
362 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
363 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
364 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
365 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
366 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
367 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
368 npe.getEnd().addNext(npi.getBegin());
369 npi.getEnd().addNext(fn);
370 return new NodePair(npe.getBegin(),fn);
373 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
375 // left side is variable
376 // left side is field
377 // left side is array
379 Operation base=an.getOperation().getBaseOp();
380 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
383 //rewrite the base operation
384 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
388 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
391 if (an.getSrc()!=null) {
392 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
393 first=np_src.getBegin();
394 last=np_src.getEnd();
396 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
401 if (an.getDest().kind()==Kind.FieldAccessNode) {
402 //We are assigning an object field
404 FieldAccessNode fan=(FieldAccessNode)an.getDest();
405 ExpressionNode en=fan.getExpression();
406 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
407 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
409 first=np_baseexp.getBegin();
411 last.addNext(np_baseexp.getBegin());
412 last=np_baseexp.getEnd();
414 //See if we need to perform an operation
416 //If it is a preinc we need to store the initial value
417 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
418 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
419 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
423 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
424 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
425 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
426 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
431 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
438 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
442 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
446 return new NodePair(first, last);
447 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
448 //We are assigning an array element
451 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
452 ExpressionNode en=aan.getExpression();
453 ExpressionNode enindex=aan.getIndex();
454 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
455 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
456 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
457 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
459 first=np_baseexp.getBegin();
461 last.addNext(np_baseexp.getBegin());
462 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
463 last=np_indexexp.getEnd();
465 //See if we need to perform an operation
467 //If it is a preinc we need to store the initial value
468 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
469 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
471 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
475 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
476 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
477 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
478 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
485 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
493 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
497 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
501 return new NodePair(first, last);
502 } else if (an.getDest().kind()==Kind.NameNode) {
503 //We could be assigning a field or variable
504 NameNode nn=(NameNode)an.getDest();
505 if (nn.getExpression()!=null) {
507 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
508 ExpressionNode en=fan.getExpression();
509 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
510 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
512 first=np_baseexp.getBegin();
514 last.addNext(np_baseexp.getBegin());
515 last=np_baseexp.getEnd();
517 //See if we need to perform an operation
519 //If it is a preinc we need to store the initial value
520 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
521 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
523 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
528 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
529 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
530 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
531 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
536 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
544 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
548 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
552 return new NodePair(first, last);
554 if (nn.getField()!=null) {
558 //See if we need to perform an operation
560 //If it is a preinc we need to store the initial value
561 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
562 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
564 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
573 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
574 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
575 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
576 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
581 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
588 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
596 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
600 return new NodePair(first, last);
603 //See if we need to perform an operation
606 //If it is a preinc we need to store the initial value
607 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
608 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
610 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
619 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
620 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
621 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
622 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
630 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
640 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
644 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
648 return new NodePair(first, last);
655 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
656 if (nn.getExpression()!=null) {
657 /* Hack - use subtree instead */
658 return flattenExpressionNode(nn.getExpression(),out_temp);
659 } else if (nn.getField()!=null) {
660 TempDescriptor tmp=getTempforVar(nn.getVar());
661 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
662 return new NodePair(ffn,ffn);
664 TempDescriptor tmp=getTempforVar(nn.isTag()?nn.getTagVar():nn.getVar());
667 out_temp.setTag(tmp.getTag());
669 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
670 return new NodePair(fon,fon);
674 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
675 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
676 TempDescriptor temp_right=null;
678 Operation op=on.getOp();
680 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
682 if (on.getRight()!=null) {
683 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
684 right=flattenExpressionNode(on.getRight(),temp_right);
686 FlatNop nop=new FlatNop();
687 right=new NodePair(nop,nop);
690 if (op.getOp()==Operation.LOGIC_OR) {
691 /* Need to do shortcircuiting */
692 FlatCondBranch fcb=new FlatCondBranch(temp_left);
693 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
694 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
695 FlatNop fnop=new FlatNop();
696 left.getEnd().addNext(fcb);
697 fcb.addFalseNext(right.getBegin());
698 right.getEnd().addNext(fon2);
700 fcb.addTrueNext(fon1);
702 return new NodePair(left.getBegin(), fnop);
703 } else if (op.getOp()==Operation.LOGIC_AND) {
704 /* Need to do shortcircuiting */
705 FlatCondBranch fcb=new FlatCondBranch(temp_left);
706 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
707 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
708 FlatNop fnop=new FlatNop();
709 left.getEnd().addNext(fcb);
710 fcb.addTrueNext(right.getBegin());
711 right.getEnd().addNext(fon2);
713 fcb.addFalseNext(fon1);
715 return new NodePair(left.getBegin(), fnop);
716 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
717 //We have a string concatenate
718 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
719 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
720 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
721 left.getEnd().addNext(right.getBegin());
722 right.getEnd().addNext(fc);
723 return new NodePair(left.getBegin(), fc);
726 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
727 left.getEnd().addNext(right.getBegin());
728 right.getEnd().addNext(fon);
729 return new NodePair(left.getBegin(),fon);
732 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
734 case Kind.AssignmentNode:
735 return flattenAssignmentNode((AssignmentNode)en,out_temp);
737 return flattenCastNode((CastNode)en,out_temp);
738 case Kind.CreateObjectNode:
739 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
740 case Kind.FieldAccessNode:
741 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
742 case Kind.ArrayAccessNode:
743 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
744 case Kind.LiteralNode:
745 return flattenLiteralNode((LiteralNode)en,out_temp);
746 case Kind.MethodInvokeNode:
747 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
749 return flattenNameNode((NameNode)en,out_temp);
751 return flattenOpNode((OpNode)en,out_temp);
756 private NodePair flattenDeclarationNode(DeclarationNode dn) {
757 VarDescriptor vd=dn.getVarDescriptor();
758 TempDescriptor td=getTempforVar(vd);
759 if (dn.getExpression()!=null)
760 return flattenExpressionNode(dn.getExpression(),td);
762 FlatNop fn=new FlatNop();
763 return new NodePair(fn,fn);
767 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
768 TagVarDescriptor tvd=dn.getTagVarDescriptor();
769 TagDescriptor tag=tvd.getTag();
770 TempDescriptor tmp=getTempforVar(tvd);
771 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
772 return new NodePair(ftd,ftd);
775 private TempDescriptor getTempforParam(Descriptor d) {
776 if (temptovar.containsKey(d))
777 return (TempDescriptor)temptovar.get(d);
779 if (d instanceof VarDescriptor) {
780 VarDescriptor vd=(VarDescriptor)d;
781 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
782 temptovar.put(vd,td);
784 } else if (d instanceof TagVarDescriptor) {
785 TagVarDescriptor tvd=(TagVarDescriptor)d;
786 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
787 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
788 temptovar.put(tvd,td);
790 } else throw new Error("Unreconized Descriptor");
794 private TempDescriptor getTempforVar(Descriptor d) {
795 if (temptovar.containsKey(d))
796 return (TempDescriptor)temptovar.get(d);
798 if (d instanceof VarDescriptor) {
799 VarDescriptor vd=(VarDescriptor)d;
800 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
801 temptovar.put(vd,td);
803 } else if (d instanceof TagVarDescriptor) {
804 TagVarDescriptor tvd=(TagVarDescriptor)d;
805 //BUGFIX TAGTYPE - add next line, modify following
806 //line to tag this new type descriptor, modify
807 //TempDescriptor constructor & factory to set type
808 //using this Type To test, use any program with tags
809 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
810 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
811 temptovar.put(tvd,td);
813 } else throw new Error("Unrecognized Descriptor");
817 private NodePair flattenIfStatementNode(IfStatementNode isn) {
818 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
819 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
820 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
821 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
823 FlatNop nopend=new FlatNop();
825 if (isn.getFalseBlock()!=null)
826 false_np=flattenBlockNode(isn.getFalseBlock());
828 FlatNop nop=new FlatNop();
829 false_np=new NodePair(nop,nop);
832 cond.getEnd().addNext(fcb);
833 fcb.addTrueNext(true_np.getBegin());
834 fcb.addFalseNext(false_np.getBegin());
835 true_np.getEnd().addNext(nopend);
836 false_np.getEnd().addNext(nopend);
837 return new NodePair(cond.getBegin(), nopend);
840 private NodePair flattenLoopNode(LoopNode ln) {
841 if (ln.getType()==LoopNode.FORLOOP) {
842 NodePair initializer=flattenBlockNode(ln.getInitializer());
843 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
844 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
845 NodePair update=flattenBlockNode(ln.getUpdate());
846 NodePair body=flattenBlockNode(ln.getBody());
847 FlatNode begin=initializer.getBegin();
848 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
849 fcb.setTrueProb(State.TRUEPROB);
851 FlatNop nopend=new FlatNop();
852 FlatBackEdge backedge=new FlatBackEdge();
854 FlatNop nop2=new FlatNop();
855 initializer.getEnd().addNext(nop2);
856 nop2.addNext(condition.getBegin());
857 body.getEnd().addNext(update.getBegin());
858 update.getEnd().addNext(backedge);
859 backedge.addNext(condition.getBegin());
860 condition.getEnd().addNext(fcb);
861 fcb.addFalseNext(nopend);
862 fcb.addTrueNext(body.getBegin());
863 return new NodePair(begin,nopend);
864 } else if (ln.getType()==LoopNode.WHILELOOP) {
865 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
866 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
867 NodePair body=flattenBlockNode(ln.getBody());
868 FlatNode begin=condition.getBegin();
869 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
870 fcb.setTrueProb(State.TRUEPROB);
872 FlatNop nopend=new FlatNop();
873 FlatBackEdge backedge=new FlatBackEdge();
875 body.getEnd().addNext(backedge);
876 backedge.addNext(condition.getBegin());
878 condition.getEnd().addNext(fcb);
879 fcb.addFalseNext(nopend);
880 fcb.addTrueNext(body.getBegin());
881 return new NodePair(begin,nopend);
882 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
883 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
884 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
885 NodePair body=flattenBlockNode(ln.getBody());
886 FlatNode begin=body.getBegin();
887 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
888 fcb.setTrueProb(State.TRUEPROB);
890 FlatNop nopend=new FlatNop();
891 FlatBackEdge backedge=new FlatBackEdge();
893 body.getEnd().addNext(condition.getBegin());
894 condition.getEnd().addNext(fcb);
895 fcb.addFalseNext(nopend);
896 fcb.addTrueNext(backedge);
897 backedge.addNext(body.getBegin());
898 return new NodePair(begin,nopend);
899 } else throw new Error();
902 private NodePair flattenReturnNode(ReturnNode rntree) {
903 TempDescriptor retval=null;
905 if (rntree.getReturnExpression()!=null) {
906 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
907 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
910 FlatReturnNode rnflat=new FlatReturnNode(retval);
912 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
913 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
914 TempDescriptor thistd=getTempforVar(currmd.getThis());
915 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
919 if (state.DSM&&currmd.getModifiers().isAtomic()) {
920 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
926 cond.getEnd().addNext(ln);
927 return new NodePair(cond.getBegin(),rnflat);
929 return new NodePair(ln,rnflat);
932 private NodePair flattenTaskExitNode(TaskExitNode ten) {
933 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
934 updateFlagActionNode(ffan, ten.getFlagEffects());
935 NodePair fcn=flattenConstraintCheck(ten.getChecks());
936 ffan.addNext(fcn.getBegin());
937 FlatReturnNode rnflat=new FlatReturnNode(null);
938 fcn.getEnd().addNext(rnflat);
939 return new NodePair(ffan, rnflat);
942 private NodePair flattenConstraintCheck(Vector ccs) {
943 FlatNode begin=new FlatNop();
945 return new NodePair(begin,begin);
947 for(int i=0;i<ccs.size();i++) {
948 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
949 /* Flatten the arguments */
950 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
951 String[] vars=new String[cc.numArgs()];
952 for(int j=0;j<cc.numArgs();j++) {
953 ExpressionNode en=cc.getArg(j);
954 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
956 vars[j]=cc.getVar(j);
957 NodePair np=flattenExpressionNode(en, td);
958 last.addNext(np.getBegin());
962 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
966 return new NodePair(begin,last);
969 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
970 return flattenBlockNode(sbn.getBlockNode());
973 private NodePair flattenAtomicNode(AtomicNode sbn) {
974 NodePair np=flattenBlockNode(sbn.getBlockNode());
975 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
976 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
977 faen.addNext(np.getBegin());
978 np.getEnd().addNext(faexn);
979 return new NodePair(faen, faexn);
982 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
984 case Kind.BlockExpressionNode:
985 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
987 case Kind.DeclarationNode:
988 return flattenDeclarationNode((DeclarationNode)bsn);
990 case Kind.TagDeclarationNode:
991 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
993 case Kind.IfStatementNode:
994 return flattenIfStatementNode((IfStatementNode)bsn);
997 return flattenLoopNode((LoopNode)bsn);
999 case Kind.ReturnNode:
1000 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1002 case Kind.TaskExitNode:
1003 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1005 case Kind.SubBlockNode:
1006 return flattenSubBlockNode((SubBlockNode)bsn);
1008 case Kind.AtomicNode:
1009 return flattenAtomicNode((AtomicNode)bsn);