7 public class BuildFlat {
10 MethodDescriptor currmd;
17 public BuildFlat(State st, TypeUtil typeutil) {
19 temptovar=new Hashtable();
20 this.typeutil=typeutil;
21 this.breakset=new HashSet();
22 this.continueset=new HashSet();
25 public Hashtable getMap() {
29 public void buildFlat() {
30 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
32 ClassDescriptor cn=(ClassDescriptor)it.next();
36 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
37 while(task_it.hasNext()) {
38 TaskDescriptor td=(TaskDescriptor)task_it.next();
43 private void flattenTask(TaskDescriptor td) {
44 BlockNode bn=state.getMethodBody(td);
45 NodePair np=flattenBlockNode(bn);
46 FlatNode fn=np.getBegin();
48 FlatNode fn2=np.getEnd();
50 if (fn2!=null&& fn2.kind()!=FKind.FlatReturnNode) {
51 FlatReturnNode rnflat=new FlatReturnNode(null);
56 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
59 FlatMethod fm=new FlatMethod(td, fe);
62 Hashtable visitedset=new Hashtable();
64 for(int i=0; i<td.numParameters(); i++) {
65 VarDescriptor paramvd=td.getParameter(i);
66 fm.addParameterTemp(getTempforVar(paramvd));
67 TagExpressionList tel=td.getTag(paramvd);
68 //BUG added next line to fix...to test feed in any task program
70 for(int j=0; j<tel.numTags(); j++) {
71 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
72 TempDescriptor tagtmp=getTempforVar(tvd);
73 if (!visitedset.containsKey(tvd.getName())) {
74 visitedset.put(tvd.getName(),tvd.getTag());
75 fm.addTagTemp(tagtmp);
77 TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
78 if (!tmptd.equals(tvd.getTag()))
79 throw new Error("Two different tag types with same name as parameters to:"+td);
81 tel.setTemp(j, tagtmp);
85 /* Flatten Vector of Flag Effects */
86 Vector flags=td.getFlagEffects();
87 updateFlagActionNode(ffan,flags);
89 state.addFlatCode(td,fm);
93 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
94 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
95 if (flags==null) // Do nothing if the flag effects vector is empty
98 for(int i=0; i<flags.size(); i++) {
99 FlagEffects fes=(FlagEffects)flags.get(i);
100 TempDescriptor flagtemp=getTempforVar(fes.getVar());
102 for(int j=0; j<fes.numEffects(); j++) {
103 FlagEffect fe=fes.getEffect(j);
104 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
107 for(int j=0; j<fes.numTagEffects(); j++) {
108 TagEffect te=fes.getTagEffect(j);
109 TempDescriptor tagtemp=getTempforVar(te.getTag());
111 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
116 FlatAtomicEnterNode curran=null;
118 private FlatNode spliceReturn(FlatNode fn) {
119 FlatReturnNode rnflat=null;
120 if (currmd.getReturnType()==null||currmd.getReturnType().isVoid()) {
121 rnflat=new FlatReturnNode(null);
124 TempDescriptor tmp=TempDescriptor.tempFactory("rettmp",currmd.getReturnType());
126 if (currmd.getReturnType().isPtr()) {
128 } else if (currmd.getReturnType().isByte()) {
130 } else if (currmd.getReturnType().isShort()) {
131 o=new Short((short)0);
132 } else if (currmd.getReturnType().isChar()) {
133 o=new Character('\0');
134 } else if (currmd.getReturnType().isInt()) {
136 } else if (currmd.getReturnType().isLong()) {
138 } else if (currmd.getReturnType().isBoolean()) {
139 o=new Boolean(false);
140 } else if (currmd.getReturnType().isFloat()) {
142 } else if (currmd.getReturnType().isDouble()) {
147 FlatLiteralNode fln=new FlatLiteralNode(currmd.getReturnType(),o,tmp);
148 rnflat=new FlatReturnNode(tmp);
155 private void flattenClass(ClassDescriptor cn) {
156 Iterator methodit=cn.getMethods();
157 while(methodit.hasNext()) {
158 currmd=(MethodDescriptor)methodit.next();
160 // if MLP is on, splice a special SESE in to
161 // enclose the main method, and a special SESE
162 // in around every other method that statically
163 // represents the SESE instance that will call
164 // that method at runtime
165 FlatSESEEnterNode spliceSESE = null;
166 FlatSESEExitNode spliceExit = null;
167 if( state.MLP || state.OOOJAVA ) {
168 if( currmd.equals( typeutil.getMain() ) ) {
169 SESENode mainTree = new SESENode( "main" );
170 spliceSESE = new FlatSESEEnterNode( mainTree );
171 spliceExit = new FlatSESEExitNode ( mainTree );
172 spliceSESE.setFlatExit ( spliceExit );
173 spliceExit.setFlatEnter( spliceSESE );
175 SESENode callerSESETree = new SESENode( "caller SESE placeholder" );
176 spliceSESE = new FlatSESEEnterNode( callerSESETree );
177 spliceSESE.setCallerSESEplaceholder();
178 spliceExit = new FlatSESEExitNode ( callerSESETree );
179 spliceSESE.setFlatExit ( spliceExit );
180 spliceExit.setFlatEnter( spliceSESE );
186 BlockNode bn=state.getMethodBody(currmd);
188 if (state.DSM&&currmd.getModifiers().isAtomic()) {
189 curran=new FlatAtomicEnterNode();
192 NodePair np=flattenBlockNode(bn);
193 FlatNode fn=np.getBegin();
194 if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
195 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
196 TempDescriptor thistd=getTempforVar(currmd.getThis());
197 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
200 if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
201 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
202 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
203 np.getEnd().addNext(fcunlock);
204 FlatNode rnflat=spliceReturn(fcunlock);
207 } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
210 if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
211 FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
212 np.getEnd().addNext(aen);
213 FlatNode rnflat=spliceReturn(aen);
217 } else if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
218 FlatNode rnflat=null;
219 // splice implicit SESE exit after method body
220 if( state.MLP || state.OOOJAVA ) {
221 np.getEnd().addNext(spliceExit);
222 rnflat=spliceReturn(spliceExit);
224 rnflat=spliceReturn(np.getEnd());
227 } else if (np.getEnd()!=null) {
228 // splice implicit SESE exit after method body
229 if( state.MLP || state.OOOJAVA ) {
230 FlatReturnNode rnflat=(FlatReturnNode)np.getEnd();
231 np.getEnd().addNext(spliceExit);
232 spliceExit.addNext(fe);
236 // splice an implicit SESE enter before method body
237 if( state.MLP || state.OOOJAVA ) {
238 spliceSESE.addNext(fn);
242 FlatMethod fm=new FlatMethod(currmd, fe);
244 if (!currmd.isStatic())
245 fm.addParameterTemp(getTempforParam(currmd.getThis()));
246 for(int i=0; i<currmd.numParameters(); i++) {
247 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
250 state.addFlatCode(currmd,fm);
254 private NodePair flattenBlockNode(BlockNode bn) {
257 for(int i=0; i<bn.size(); i++) {
258 NodePair np=flattenBlockStatementNode(bn.get(i));
259 FlatNode np_begin=np.getBegin();
260 FlatNode np_end=np.getEnd();
267 end.addNext(np_begin);
269 return new NodePair(begin, null);
275 end=begin=new FlatNop();
277 return new NodePair(begin,end);
280 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
281 //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
282 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
283 return flattenExpressionNode(en.getExpression(),tmp);
286 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
287 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
288 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
289 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
290 np.getEnd().addNext(fcn);
291 return new NodePair(np.getBegin(),fcn);
294 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
295 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
296 return new NodePair(fln,fln);
299 private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
300 FlatOffsetNode fln = new FlatOffsetNode(ofn.getClassType(), ofn.getField(), out_temp);
301 return new NodePair(fln, fln);
304 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
305 TypeDescriptor td=con.getType();
307 FlatNode fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
309 //handle wrapper fields
310 ClassDescriptor cd=td.getClassDesc();
311 for(Iterator fieldit=cd.getFields();fieldit.hasNext();) {
312 FieldDescriptor fd=(FieldDescriptor)fieldit.next();
313 if (fd.getType().iswrapper()) {
314 TempDescriptor wrap_tmp=TempDescriptor.tempFactory("wrapper_obj",fd.getType());
315 FlatNode fnwrapper=new FlatNew(fd.getType(), wrap_tmp, con.isGlobal());
316 FlatSetFieldNode fsfn=new FlatSetFieldNode(out_temp, fd, wrap_tmp);
317 last.addNext(fnwrapper);
318 fnwrapper.addNext(fsfn);
323 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
326 for(int i=0; i<con.numArgs(); i++) {
327 ExpressionNode en=con.getArg(i);
328 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
330 NodePair np=flattenExpressionNode(en, tmp);
331 last.addNext(np.getBegin());
334 MethodDescriptor md=con.getConstructor();
335 //Call to constructor
336 FlatCall fc=new FlatCall(md, null, out_temp, temps);
339 if (td.getClassDesc().hasFlags()) {
340 // if (con.getFlagEffects()!=null) {
341 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
342 FlagEffects fes=con.getFlagEffects();
343 TempDescriptor flagtemp=out_temp;
345 for(int j=0; j<fes.numEffects(); j++) {
346 FlagEffect fe=fes.getEffect(j);
347 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
349 for(int j=0; j<fes.numTagEffects(); j++) {
350 TagEffect te=fes.getTagEffect(j);
351 TempDescriptor tagtemp=getTempforVar(te.getTag());
353 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
356 ffan.addFlagAction(flagtemp, null, false);
361 return new NodePair(fn,last);
365 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
366 for (int i=0; i<con.numArgs(); i++) {
367 ExpressionNode en=con.getArg(i);
368 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
370 NodePair np=flattenExpressionNode(en, tmp);
374 last.addNext(np.getBegin());
377 TempDescriptor tmp2=(i==0) ?
379 TempDescriptor.tempFactory("arg",en.getType());
381 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
383 if (temps.length>1) {
384 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
385 fn.addNext(np.getBegin());
386 return new NodePair(first,np.getEnd());
387 } else if (td.isArray()&&td.dereference().iswrapper()) {
388 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
389 fn.addNext(np.getBegin());
390 return new NodePair(first,np.getEnd());
392 return new NodePair(first, fn);
396 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
397 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
398 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
399 FlatNop fnop=new FlatNop(); //last node
402 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
404 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
406 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
408 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
409 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
410 fcb.setTrueProb(State.TRUEPROB);
413 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
414 FlatNew fn=td.iswrapper()?new FlatNew(td, new_tmp, isglobal):new FlatNew(td, new_tmp, temparray[i+1], isglobal);
415 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
417 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
423 fcb.addFalseNext(fnop);
425 //Recursive call here
426 if ((i+2)<temparray.length) {
427 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
428 fsen.addNext(np2.getBegin());
429 np2.getEnd().addNext(fon);
430 } else if (td.isArray()&&td.dereference().iswrapper()) {
431 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
432 fsen.addNext(np2.getBegin());
433 np2.getEnd().addNext(fon);
438 return new NodePair(fln, fnop);
441 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
442 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
445 TempDescriptor thisarg=null;
447 if (min.getExpression()!=null) {
448 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
449 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
455 for(int i=0; i<min.numArgs(); i++) {
456 ExpressionNode en=min.getArg(i);
457 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
459 NodePair np=flattenExpressionNode(en, td);
463 last.addNext(np.getBegin());
467 MethodDescriptor md=min.getMethod();
469 //Call to constructor
472 if(md.getReturnType()==null||md.getReturnType().isVoid())
473 fc=new FlatCall(md, null, thisarg, temps);
475 fc=new FlatCall(md, out_temp, thisarg, temps);
480 return new NodePair(first,fc);
483 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
484 TempDescriptor tmp=null;
485 if(fan.getExpression().getType().isStatic()) {
486 // static field dereference with class name
487 tmp = new TempDescriptor(fan.getExpression().getType().getClassDesc().getSymbol(), fan.getExpression().getType());
488 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
489 return new NodePair(fn,fn);
491 tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
492 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
493 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
494 npe.getEnd().addNext(fn);
495 return new NodePair(npe.getBegin(),fn);
499 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
500 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
501 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
502 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
503 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
504 TempDescriptor arraytmp=out_temp;
505 if (aan.iswrapper()) {
507 arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference());
509 FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp);
510 npe.getEnd().addNext(npi.getBegin());
511 npi.getEnd().addNext(fn);
512 if (aan.iswrapper()) {
513 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp);
517 return new NodePair(npe.getBegin(),fn);
520 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
522 // left side is variable
523 // left side is field
524 // left side is array
526 Operation base=an.getOperation().getBaseOp();
527 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
530 //rewrite the base operation
531 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
535 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
538 if (an.getSrc()!=null) {
539 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
540 first=np_src.getBegin();
541 last=np_src.getEnd();
543 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
548 if (an.getDest().kind()==Kind.FieldAccessNode) {
549 //We are assigning an object field
551 FieldAccessNode fan=(FieldAccessNode)an.getDest();
552 ExpressionNode en=fan.getExpression();
553 TempDescriptor dst_tmp=null;
554 NodePair np_baseexp=null;
555 if(en.getType().isStatic()) {
556 // static field dereference with class name
557 dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
558 FlatNop nop=new FlatNop();
559 np_baseexp = new NodePair(nop,nop);
561 dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
562 np_baseexp=flattenExpressionNode(en, dst_tmp);
565 first=np_baseexp.getBegin();
567 last.addNext(np_baseexp.getBegin());
568 last=np_baseexp.getEnd();
570 //See if we need to perform an operation
572 //If it is a preinc we need to store the initial value
573 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
574 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
575 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
579 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
580 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
581 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
582 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
587 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
594 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
598 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
602 return new NodePair(first, last);
603 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
604 //We are assigning an array element
606 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
607 ExpressionNode en=aan.getExpression();
608 ExpressionNode enindex=aan.getIndex();
609 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
610 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
611 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
612 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
614 first=np_baseexp.getBegin();
616 last.addNext(np_baseexp.getBegin());
617 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
618 last=np_indexexp.getEnd();
620 //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=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
624 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
626 if (aan.iswrapper()) {
627 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
628 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
629 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
630 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp3,src_tmp2);
635 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
639 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
640 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
641 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
642 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
647 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
654 if (aan.iswrapper()) {
655 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
656 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
657 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
658 FlatSetFieldNode fsfn=new FlatSetFieldNode(src_tmp3,(FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp);
663 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
668 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
672 return new NodePair(first, last);
673 } else if (an.getDest().kind()==Kind.NameNode) {
674 //We could be assigning a field or variable
675 NameNode nn=(NameNode)an.getDest();
678 if (nn.getExpression()!=null) {
680 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
681 ExpressionNode en=fan.getExpression();
682 TempDescriptor dst_tmp=null;
683 NodePair np_baseexp=null;
684 if(en.getType().isStatic()) {
685 // static field dereference with class name
686 dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
687 FlatNop nop=new FlatNop();
688 np_baseexp = new NodePair(nop,nop);
690 dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
691 np_baseexp=flattenExpressionNode(en, dst_tmp);
694 first=np_baseexp.getBegin();
696 last.addNext(np_baseexp.getBegin());
697 last=np_baseexp.getEnd();
699 //See if we need to perform an operation
701 //If it is a preinc we need to store the initial value
702 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
703 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
705 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
710 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
711 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
712 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
713 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
718 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
726 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
730 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
734 return new NodePair(first, last);
736 if (nn.getField()!=null) {
740 //See if we need to perform an operation
742 //If it is a preinc we need to store the initial value
743 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
744 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
746 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
755 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
756 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
757 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
758 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
763 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
770 FlatSetFieldNode fsfn=null;
771 if(nn.getClassDesc()!=null) {
772 // this is a static field access inside of a static block
773 fsfn=new FlatSetFieldNode(new TempDescriptor("sfsb", nn.getClassType()), nn.getField(), src_tmp);
775 fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
784 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
788 return new NodePair(first, last);
791 //See if we need to perform an operation
794 //If it is a preinc we need to store the initial value
795 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
796 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
798 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
807 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
808 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
809 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
810 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
818 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
828 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
833 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
837 return new NodePair(first, last);
845 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
846 if (nn.getExpression()!=null) {
847 /* Hack - use subtree instead */
848 return flattenExpressionNode(nn.getExpression(),out_temp);
849 } else if (nn.getField()!=null) {
850 TempDescriptor tmp=getTempforVar(nn.getVar());
851 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
852 return new NodePair(ffn,ffn);
854 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
857 out_temp.setTag(tmp.getTag());
859 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
860 return new NodePair(fon,fon);
864 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
865 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
866 TempDescriptor temp_right=null;
868 Operation op=on.getOp();
870 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
872 if (on.getRight()!=null) {
873 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
874 right=flattenExpressionNode(on.getRight(),temp_right);
876 FlatNop nop=new FlatNop();
877 right=new NodePair(nop,nop);
880 if (op.getOp()==Operation.LOGIC_OR) {
881 /* Need to do shortcircuiting */
882 FlatCondBranch fcb=new FlatCondBranch(temp_left);
883 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
884 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
885 FlatNop fnop=new FlatNop();
886 left.getEnd().addNext(fcb);
887 fcb.addFalseNext(right.getBegin());
888 right.getEnd().addNext(fon2);
890 fcb.addTrueNext(fon1);
892 return new NodePair(left.getBegin(), fnop);
893 } else if (op.getOp()==Operation.LOGIC_AND) {
894 /* Need to do shortcircuiting */
895 FlatCondBranch fcb=new FlatCondBranch(temp_left);
896 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
897 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
898 FlatNop fnop=new FlatNop();
899 left.getEnd().addNext(fcb);
900 fcb.addTrueNext(right.getBegin());
901 right.getEnd().addNext(fon2);
903 fcb.addFalseNext(fon1);
905 return new NodePair(left.getBegin(), fnop);
906 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
907 //We have a string concatenate
908 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
909 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
910 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
911 left.getEnd().addNext(right.getBegin());
912 right.getEnd().addNext(fc);
913 return new NodePair(left.getBegin(), fc);
916 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
917 left.getEnd().addNext(right.getBegin());
918 right.getEnd().addNext(fon);
919 return new NodePair(left.getBegin(),fon);
922 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
924 case Kind.AssignmentNode:
925 return flattenAssignmentNode((AssignmentNode)en,out_temp);
928 return flattenCastNode((CastNode)en,out_temp);
930 case Kind.CreateObjectNode:
931 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
933 case Kind.FieldAccessNode:
934 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
936 case Kind.ArrayAccessNode:
937 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
939 case Kind.LiteralNode:
940 return flattenLiteralNode((LiteralNode)en,out_temp);
942 case Kind.MethodInvokeNode:
943 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
946 return flattenNameNode((NameNode)en,out_temp);
949 return flattenOpNode((OpNode)en,out_temp);
951 case Kind.OffsetNode:
952 return flattenOffsetNode((OffsetNode)en,out_temp);
954 case Kind.TertiaryNode:
955 return flattenTertiaryNode((TertiaryNode)en,out_temp);
957 case Kind.InstanceOfNode:
958 return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
960 case Kind.ArrayInitializerNode:
961 return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
966 private NodePair flattenDeclarationNode(DeclarationNode dn) {
967 VarDescriptor vd=dn.getVarDescriptor();
968 TempDescriptor td=getTempforVar(vd);
969 if (dn.getExpression()!=null)
970 return flattenExpressionNode(dn.getExpression(),td);
972 FlatNop fn=new FlatNop();
973 return new NodePair(fn,fn);
977 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
978 TagVarDescriptor tvd=dn.getTagVarDescriptor();
979 TagDescriptor tag=tvd.getTag();
980 TempDescriptor tmp=getTempforVar(tvd);
981 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
982 return new NodePair(ftd,ftd);
985 private TempDescriptor getTempforParam(Descriptor d) {
986 if (temptovar.containsKey(d))
987 return (TempDescriptor)temptovar.get(d);
989 if (d instanceof VarDescriptor) {
990 VarDescriptor vd=(VarDescriptor)d;
991 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
992 temptovar.put(vd,td);
994 } else if (d instanceof TagVarDescriptor) {
995 TagVarDescriptor tvd=(TagVarDescriptor)d;
996 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
997 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
998 temptovar.put(tvd,td);
1000 } else throw new Error("Unreconized Descriptor");
1004 private TempDescriptor getTempforVar(Descriptor d) {
1005 if (temptovar.containsKey(d))
1006 return (TempDescriptor)temptovar.get(d);
1008 if (d instanceof VarDescriptor) {
1009 VarDescriptor vd=(VarDescriptor)d;
1010 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
1011 temptovar.put(vd,td);
1013 } else if (d instanceof TagVarDescriptor) {
1014 TagVarDescriptor tvd=(TagVarDescriptor)d;
1015 //BUGFIX TAGTYPE - add next line, modify following
1016 //line to tag this new type descriptor, modify
1017 //TempDescriptor constructor & factory to set type
1018 //using this Type To test, use any program with tags
1019 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1020 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
1021 temptovar.put(tvd,td);
1023 } else throw new Error("Unrecognized Descriptor");
1027 private NodePair flattenIfStatementNode(IfStatementNode isn) {
1028 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1029 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
1030 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1031 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
1033 FlatNop nopend=new FlatNop();
1035 if (isn.getFalseBlock()!=null)
1036 false_np=flattenBlockNode(isn.getFalseBlock());
1038 FlatNop nop=new FlatNop();
1039 false_np=new NodePair(nop,nop);
1042 cond.getEnd().addNext(fcb);
1043 fcb.addTrueNext(true_np.getBegin());
1044 fcb.addFalseNext(false_np.getBegin());
1045 if (true_np.getEnd()!=null)
1046 true_np.getEnd().addNext(nopend);
1047 if (false_np.getEnd()!=null)
1048 false_np.getEnd().addNext(nopend);
1049 if (nopend.numPrev()==0)
1050 return new NodePair(cond.getBegin(), null);
1052 return new NodePair(cond.getBegin(), nopend);
1055 private NodePair flattenSwitchStatementNode(SwitchStatementNode ssn) {
1056 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.INT));
1057 NodePair cond=flattenExpressionNode(ssn.getCondition(),cond_temp);
1058 FlatNop nopend=new FlatNop();
1059 NodePair sbody = flattenSwitchBodyNode(ssn.getSwitchBody(), cond_temp, nopend);
1061 cond.getEnd().addNext(sbody.getBegin());
1063 return new NodePair(cond.getBegin(), sbody.getEnd());
1066 private NodePair flattenSwitchBodyNode(BlockNode bn, TempDescriptor cond_temp, FlatNode endnode) {
1067 FlatNode begin=null;
1068 FlatNode end=endnode;
1069 NodePair prev_true_branch = null;
1070 NodePair prev_false_branch = null;
1071 for(int i=0; i<bn.size(); i++) {
1072 SwitchBlockNode sbn = (SwitchBlockNode)bn.get(i);
1073 HashSet oldbs=breakset;
1074 breakset=new HashSet();
1076 NodePair body=flattenBlockNode(sbn.getSwitchBlockStatement());
1077 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
1078 FlatNode cond_begin = null;
1079 NodePair prev_fnp = null;
1080 for(int j = 0; j < slnv.size(); j++) {
1081 SwitchLabelNode sln = slnv.elementAt(j);
1082 NodePair left = null;
1083 NodePair false_np = null;
1084 if(sln.isDefault()) {
1087 TempDescriptor cond_tmp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1088 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop", sln.getCondition().getType());
1089 Operation op=new Operation(Operation.EQUAL);
1090 left=flattenExpressionNode(sln.getCondition(), temp_left);
1091 FlatOpNode fon=new FlatOpNode(cond_tmp, temp_left, cond_temp, op);
1092 left.getEnd().addNext(fon);
1094 FlatCondBranch fcb=new FlatCondBranch(cond_tmp);
1095 fcb.setTrueProb(State.TRUEPROB);
1097 FlatNop nop=new FlatNop();
1098 false_np=new NodePair(nop,nop);
1101 fcb.addTrueNext(body.getBegin());
1102 fcb.addFalseNext(false_np.getBegin());
1104 if((prev_fnp != null) && (prev_fnp.getEnd() != null)) {
1105 prev_fnp.getEnd().addNext(left.getBegin());
1107 prev_fnp = false_np;
1110 begin = left.getBegin();
1112 if(cond_begin == null) {
1113 cond_begin = left.getBegin();
1116 if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1117 prev_false_branch.getEnd().addNext(cond_begin);
1119 prev_false_branch = prev_fnp;
1120 if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1121 prev_true_branch.getEnd().addNext(body.getBegin());
1123 prev_true_branch = body;
1124 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1125 FlatNode fn=(FlatNode)breakit.next();
1127 fn.addNext(endnode);
1131 if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1132 prev_true_branch.getEnd().addNext(endnode);
1134 if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1135 prev_false_branch.getEnd().addNext(endnode);
1138 end=begin=new FlatNop();
1140 return new NodePair(begin,end);
1143 private NodePair flattenLoopNode(LoopNode ln) {
1144 HashSet oldbs=breakset;
1145 HashSet oldcs=continueset;
1146 breakset=new HashSet();
1147 continueset=new HashSet();
1149 if (ln.getType()==LoopNode.FORLOOP) {
1150 NodePair initializer=flattenBlockNode(ln.getInitializer());
1151 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1152 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1153 NodePair update=flattenBlockNode(ln.getUpdate());
1154 NodePair body=flattenBlockNode(ln.getBody());
1155 FlatNode begin=initializer.getBegin();
1156 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1157 fcb.setTrueProb(State.TRUEPROB);
1159 FlatNop nopend=new FlatNop();
1160 FlatBackEdge backedge=new FlatBackEdge();
1162 FlatNop nop2=new FlatNop();
1163 initializer.getEnd().addNext(nop2);
1164 nop2.addNext(condition.getBegin());
1165 if (body.getEnd()!=null)
1166 body.getEnd().addNext(update.getBegin());
1167 update.getEnd().addNext(backedge);
1168 backedge.addNext(condition.getBegin());
1169 condition.getEnd().addNext(fcb);
1170 fcb.addFalseNext(nopend);
1171 fcb.addTrueNext(body.getBegin());
1172 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1173 FlatNode fn=(FlatNode)contit.next();
1175 fn.addNext(update.getBegin());
1177 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1178 FlatNode fn=(FlatNode)breakit.next();
1184 return new NodePair(begin,nopend);
1185 } else if (ln.getType()==LoopNode.WHILELOOP) {
1186 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1187 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1188 NodePair body=flattenBlockNode(ln.getBody());
1189 FlatNode begin=condition.getBegin();
1190 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1191 fcb.setTrueProb(State.TRUEPROB);
1193 FlatNop nopend=new FlatNop();
1194 FlatBackEdge backedge=new FlatBackEdge();
1196 if (body.getEnd()!=null)
1197 body.getEnd().addNext(backedge);
1198 backedge.addNext(condition.getBegin());
1200 condition.getEnd().addNext(fcb);
1201 fcb.addFalseNext(nopend);
1202 fcb.addTrueNext(body.getBegin());
1204 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1205 FlatNode fn=(FlatNode)contit.next();
1207 fn.addNext(backedge);
1209 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1210 FlatNode fn=(FlatNode)breakit.next();
1216 return new NodePair(begin,nopend);
1217 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
1218 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1219 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1220 NodePair body=flattenBlockNode(ln.getBody());
1221 FlatNode begin=body.getBegin();
1222 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1223 fcb.setTrueProb(State.TRUEPROB);
1225 FlatNop nopend=new FlatNop();
1226 FlatBackEdge backedge=new FlatBackEdge();
1228 if (body.getEnd()!=null)
1229 body.getEnd().addNext(condition.getBegin());
1230 condition.getEnd().addNext(fcb);
1231 fcb.addFalseNext(nopend);
1232 fcb.addTrueNext(backedge);
1233 backedge.addNext(body.getBegin());
1235 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1236 FlatNode fn=(FlatNode)contit.next();
1238 fn.addNext(condition.getBegin());
1240 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1241 FlatNode fn=(FlatNode)breakit.next();
1247 return new NodePair(begin,nopend);
1248 } else throw new Error();
1251 private NodePair flattenReturnNode(ReturnNode rntree) {
1252 TempDescriptor retval=null;
1254 if (rntree.getReturnExpression()!=null) {
1255 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
1256 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1259 FlatReturnNode rnflat=new FlatReturnNode(retval);
1262 if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
1263 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1264 TempDescriptor thistd=getTempforVar(currmd.getThis());
1265 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
1269 if (state.DSM&&currmd.getModifiers().isAtomic()) {
1270 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1276 cond.getEnd().addNext(ln);
1277 return new NodePair(cond.getBegin(),null);
1279 return new NodePair(ln,null);
1282 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1283 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1284 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1285 updateFlagActionNode(ffan, ten.getFlagEffects());
1286 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1287 ffan.addNext(fcn.getBegin());
1288 FlatReturnNode rnflat=new FlatReturnNode(null);
1290 fcn.getEnd().addNext(rnflat);
1291 return new NodePair(ffan, null);
1294 private NodePair flattenConstraintCheck(Vector ccs) {
1295 FlatNode begin=new FlatNop();
1297 return new NodePair(begin,begin);
1298 FlatNode last=begin;
1299 for(int i=0; i<ccs.size(); i++) {
1300 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1301 /* Flatten the arguments */
1302 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1303 String[] vars=new String[cc.numArgs()];
1304 for(int j=0; j<cc.numArgs(); j++) {
1305 ExpressionNode en=cc.getArg(j);
1306 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1308 vars[j]=cc.getVar(j);
1309 NodePair np=flattenExpressionNode(en, td);
1310 last.addNext(np.getBegin());
1314 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1318 return new NodePair(begin,last);
1321 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1322 return flattenBlockNode(sbn.getBlockNode());
1325 private NodePair flattenSynchronizedNode(SynchronizedNode sbn) {
1326 TempDescriptor montmp=TempDescriptor.tempFactory("monitor",sbn.getExpr().getType());
1327 NodePair npexp=flattenExpressionNode(sbn.getExpr(), montmp);
1328 NodePair npblock=flattenBlockNode(sbn.getBlockNode());
1330 MethodDescriptor menmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
1331 FlatCall fcen=new FlatCall(menmd, null, montmp, new TempDescriptor[0]);
1333 MethodDescriptor mexmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1334 FlatCall fcex=new FlatCall(mexmd, null, montmp, new TempDescriptor[0]);
1336 npexp.getEnd().addNext(fcen);
1337 fcen.addNext(npblock.getBegin());
1338 npblock.getEnd().addNext(fcex);
1339 return new NodePair(npexp.getBegin(), fcex);
1342 private NodePair flattenAtomicNode(AtomicNode sbn) {
1343 NodePair np=flattenBlockNode(sbn.getBlockNode());
1344 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1345 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1346 faen.addNext(np.getBegin());
1347 np.getEnd().addNext(faexn);
1348 return new NodePair(faen, faexn);
1351 private NodePair flattenGenReachNode( GenReachNode grn ) {
1352 FlatGenReachNode fgrn = new FlatGenReachNode( grn.getGraphName() );
1353 return new NodePair( fgrn, fgrn );
1356 private NodePair flattenSESENode(SESENode sn) {
1357 if( sn.isStart() ) {
1358 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1359 sn.setFlatEnter(fsen);
1360 return new NodePair(fsen, fsen);
1363 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1364 sn.setFlatExit(fsexn);
1365 FlatSESEEnterNode fsen=sn.getStart().getFlatEnter();
1366 fsexn.setFlatEnter(fsen);
1367 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1369 return new NodePair(fsexn, fsexn);
1372 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1373 FlatNop fn=new FlatNop();
1377 continueset.add(fn);
1378 return new NodePair(fn,null);
1381 private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1382 TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1383 NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1384 FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1385 cond.getEnd().addNext(fion);
1386 return new NodePair(cond.getBegin(),fion);
1389 private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1391 TempDescriptor expr_temp=TempDescriptor.tempFactory("arry_init",ain.getType());
1393 // create a new array of size equal to the array initializer
1394 //FlatNode first=null;
1395 //FlatNode last=null;
1396 TempDescriptor[] temps=new TempDescriptor[ain.numVarInitializers()];
1398 for (int i=0; i<con.numArgs(); i++) {
1399 ExpressionNode en=con.getArg(i);
1400 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
1402 NodePair np=flattenExpressionNode(en, tmp);
1404 first=np.getBegin();
1406 last.addNext(np.getBegin());
1409 TempDescriptor tmp2=(i==0) ?
1411 TempDescriptor.tempFactory("arg",en.getType());
1413 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
1417 // assign each element of the new array to the flattened expression
1420 FlatOpNode fonAssignArray=new FlatOpNode(out_temp, newarry_temp, null, new Operation(Operation.ASSIGN));
1422 //return new NodePair( , fonAssignArray );
1424 System.out.println( "Array initializers not implemented yet." );
1429 private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1430 TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1431 TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1432 TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1434 NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1435 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1437 NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1438 FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1440 NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1441 FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1443 FlatNop nopend=new FlatNop();
1445 cond.getEnd().addNext(fcb);
1447 fcb.addTrueNext(trueExpr.getBegin());
1448 fcb.addFalseNext(falseExpr.getBegin());
1450 trueExpr.getEnd().addNext(fonT);
1451 fonT.addNext(nopend);
1453 falseExpr.getEnd().addNext(fonF);
1454 fonF.addNext(nopend);
1456 return new NodePair(cond.getBegin(), nopend);
1459 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1460 switch(bsn.kind()) {
1461 case Kind.BlockExpressionNode:
1462 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1464 case Kind.DeclarationNode:
1465 return flattenDeclarationNode((DeclarationNode)bsn);
1467 case Kind.TagDeclarationNode:
1468 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1470 case Kind.IfStatementNode:
1471 return flattenIfStatementNode((IfStatementNode)bsn);
1473 case Kind.SwitchStatementNode:
1474 return flattenSwitchStatementNode((SwitchStatementNode)bsn);
1477 return flattenLoopNode((LoopNode)bsn);
1479 case Kind.ReturnNode:
1480 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1482 case Kind.TaskExitNode:
1483 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1485 case Kind.SubBlockNode:
1486 return flattenSubBlockNode((SubBlockNode)bsn);
1488 case Kind.AtomicNode:
1489 return flattenAtomicNode((AtomicNode)bsn);
1491 case Kind.SynchronizedNode:
1492 return flattenSynchronizedNode((SynchronizedNode)bsn);
1495 return flattenSESENode((SESENode)bsn);
1497 case Kind.GenReachNode:
1498 return flattenGenReachNode((GenReachNode)bsn);
1500 case Kind.ContinueBreakNode:
1501 return flattenContinueBreakNode((ContinueBreakNode)bsn);