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);
363 if(con.getArrayInitializer() == null) {
366 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
367 for (int i=0; i<con.numArgs(); i++) {
368 ExpressionNode en=con.getArg(i);
369 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
371 NodePair np=flattenExpressionNode(en, tmp);
375 last.addNext(np.getBegin());
378 TempDescriptor tmp2=(i==0) ?
380 TempDescriptor.tempFactory("arg",en.getType());
382 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
384 if (temps.length>1) {
385 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
386 fn.addNext(np.getBegin());
387 return new NodePair(first,np.getEnd());
388 } else if (td.isArray()&&td.dereference().iswrapper()) {
389 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
390 fn.addNext(np.getBegin());
391 return new NodePair(first,np.getEnd());
393 return new NodePair(first, fn);
394 } else if(state.MGC) {
395 // array creation with initializers
396 return flattenArrayInitializerNode(con.getArrayInitializer(), out_temp);
402 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
403 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
404 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
405 FlatNop fnop=new FlatNop(); //last node
408 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
410 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
412 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
414 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
415 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
416 fcb.setTrueProb(State.TRUEPROB);
419 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
420 FlatNew fn=td.iswrapper()?new FlatNew(td, new_tmp, isglobal):new FlatNew(td, new_tmp, temparray[i+1], isglobal);
421 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
423 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
429 fcb.addFalseNext(fnop);
431 //Recursive call here
432 if ((i+2)<temparray.length) {
433 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
434 fsen.addNext(np2.getBegin());
435 np2.getEnd().addNext(fon);
436 } else if (td.isArray()&&td.dereference().iswrapper()) {
437 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
438 fsen.addNext(np2.getBegin());
439 np2.getEnd().addNext(fon);
444 return new NodePair(fln, fnop);
447 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
448 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
451 TempDescriptor thisarg=null;
453 if (min.getExpression()!=null) {
454 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
455 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
461 for(int i=0; i<min.numArgs(); i++) {
462 ExpressionNode en=min.getArg(i);
463 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
465 NodePair np=flattenExpressionNode(en, td);
469 last.addNext(np.getBegin());
473 MethodDescriptor md=min.getMethod();
475 //Call to constructor
478 if(md.getReturnType()==null||md.getReturnType().isVoid())
479 fc=new FlatCall(md, null, thisarg, temps);
481 fc=new FlatCall(md, out_temp, thisarg, temps);
486 return new NodePair(first,fc);
489 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
490 TempDescriptor tmp=null;
491 if(fan.getExpression().getType().isStatic()) {
492 // static field dereference with class name
493 tmp = new TempDescriptor(fan.getExpression().getType().getClassDesc().getSymbol(), fan.getExpression().getType());
494 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
495 return new NodePair(fn,fn);
497 tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
498 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
499 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
500 npe.getEnd().addNext(fn);
501 return new NodePair(npe.getBegin(),fn);
505 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
506 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
507 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
508 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
509 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
510 TempDescriptor arraytmp=out_temp;
511 if (aan.iswrapper()) {
513 arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference());
515 FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp);
516 npe.getEnd().addNext(npi.getBegin());
517 npi.getEnd().addNext(fn);
518 if (aan.iswrapper()) {
519 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp);
523 return new NodePair(npe.getBegin(),fn);
526 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
528 // left side is variable
529 // left side is field
530 // left side is array
532 Operation base=an.getOperation().getBaseOp();
533 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
536 //rewrite the base operation
537 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
541 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
544 if (an.getSrc()!=null) {
545 if(an.getSrc().getEval() != null) {
546 first = last = new FlatLiteralNode(an.getSrc().getType(), an.getSrc().getEval().longValue(), src_tmp);
548 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
549 first=np_src.getBegin();
550 last=np_src.getEnd();
553 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
558 if (an.getDest().kind()==Kind.FieldAccessNode) {
559 //We are assigning an object field
561 FieldAccessNode fan=(FieldAccessNode)an.getDest();
562 ExpressionNode en=fan.getExpression();
563 TempDescriptor dst_tmp=null;
564 NodePair np_baseexp=null;
565 if(en.getType().isStatic()) {
566 // static field dereference with class name
567 dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
568 FlatNop nop=new FlatNop();
569 np_baseexp = new NodePair(nop,nop);
571 dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
572 np_baseexp=flattenExpressionNode(en, dst_tmp);
575 first=np_baseexp.getBegin();
577 last.addNext(np_baseexp.getBegin());
578 last=np_baseexp.getEnd();
580 //See if we need to perform an operation
582 //If it is a preinc we need to store the initial value
583 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
584 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
585 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, 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(dst_tmp, fan.getField(), src_tmp);
608 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
612 return new NodePair(first, last);
613 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
614 //We are assigning an array element
616 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
617 ExpressionNode en=aan.getExpression();
618 ExpressionNode enindex=aan.getIndex();
619 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
620 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
621 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
622 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
624 first=np_baseexp.getBegin();
626 last.addNext(np_baseexp.getBegin());
627 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
628 last=np_indexexp.getEnd();
630 //See if we need to perform an operation
632 //If it is a preinc we need to store the initial value
633 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
634 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
636 if (aan.iswrapper()) {
637 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
638 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
639 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
640 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp3,src_tmp2);
645 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
649 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
650 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
651 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
652 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
657 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
664 if (aan.iswrapper()) {
665 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
666 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
667 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
668 FlatSetFieldNode fsfn=new FlatSetFieldNode(src_tmp3,(FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp);
673 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
678 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
682 return new NodePair(first, last);
683 } else if (an.getDest().kind()==Kind.NameNode) {
684 //We could be assigning a field or variable
685 NameNode nn=(NameNode)an.getDest();
688 if (nn.getExpression()!=null) {
690 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
691 ExpressionNode en=fan.getExpression();
692 TempDescriptor dst_tmp=null;
693 NodePair np_baseexp=null;
694 if(en.getType().isStatic()) {
695 // static field dereference with class name
696 dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
697 FlatNop nop=new FlatNop();
698 np_baseexp = new NodePair(nop,nop);
700 dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
701 np_baseexp=flattenExpressionNode(en, dst_tmp);
704 first=np_baseexp.getBegin();
706 last.addNext(np_baseexp.getBegin());
707 last=np_baseexp.getEnd();
709 //See if we need to perform an operation
711 //If it is a preinc we need to store the initial value
712 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
713 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
715 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
720 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
721 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
722 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
723 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
728 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
736 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
740 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
744 return new NodePair(first, last);
746 if (nn.getField()!=null) {
750 //See if we need to perform an operation
752 //If it is a preinc we need to store the initial value
753 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
754 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
756 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
765 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
766 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
767 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
768 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
773 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
780 FlatSetFieldNode fsfn=null;
781 if(nn.getClassDesc()!=null) {
782 // this is a static field access inside of a static block
783 fsfn=new FlatSetFieldNode(new TempDescriptor("sfsb", nn.getClassType()), nn.getField(), src_tmp);
785 fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
794 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
798 return new NodePair(first, last);
801 //See if we need to perform an operation
804 //If it is a preinc we need to store the initial value
805 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
806 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
808 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
817 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
818 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
819 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
820 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
828 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
838 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
843 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
847 return new NodePair(first, last);
855 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
856 if (nn.getExpression()!=null) {
857 /* Hack - use subtree instead */
858 return flattenExpressionNode(nn.getExpression(),out_temp);
859 } else if (nn.getField()!=null) {
860 TempDescriptor tmp=getTempforVar(nn.getVar());
861 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
862 return new NodePair(ffn,ffn);
864 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
867 out_temp.setTag(tmp.getTag());
869 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
870 return new NodePair(fon,fon);
874 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
875 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
876 TempDescriptor temp_right=null;
878 Operation op=on.getOp();
880 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
882 if (on.getRight()!=null) {
883 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
884 right=flattenExpressionNode(on.getRight(),temp_right);
886 FlatNop nop=new FlatNop();
887 right=new NodePair(nop,nop);
890 if (op.getOp()==Operation.LOGIC_OR) {
891 /* Need to do shortcircuiting */
892 FlatCondBranch fcb=new FlatCondBranch(temp_left);
893 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
894 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
895 FlatNop fnop=new FlatNop();
896 left.getEnd().addNext(fcb);
897 fcb.addFalseNext(right.getBegin());
898 right.getEnd().addNext(fon2);
900 fcb.addTrueNext(fon1);
902 return new NodePair(left.getBegin(), fnop);
903 } else if (op.getOp()==Operation.LOGIC_AND) {
904 /* Need to do shortcircuiting */
905 FlatCondBranch fcb=new FlatCondBranch(temp_left);
906 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
907 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
908 FlatNop fnop=new FlatNop();
909 left.getEnd().addNext(fcb);
910 fcb.addTrueNext(right.getBegin());
911 right.getEnd().addNext(fon2);
913 fcb.addFalseNext(fon1);
915 return new NodePair(left.getBegin(), fnop);
916 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
917 //We have a string concatenate
918 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
919 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
920 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
921 left.getEnd().addNext(right.getBegin());
922 right.getEnd().addNext(fc);
923 return new NodePair(left.getBegin(), fc);
926 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
927 left.getEnd().addNext(right.getBegin());
928 right.getEnd().addNext(fon);
929 return new NodePair(left.getBegin(),fon);
932 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
934 case Kind.AssignmentNode:
935 return flattenAssignmentNode((AssignmentNode)en,out_temp);
938 return flattenCastNode((CastNode)en,out_temp);
940 case Kind.CreateObjectNode:
941 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
943 case Kind.FieldAccessNode:
944 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
946 case Kind.ArrayAccessNode:
947 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
949 case Kind.LiteralNode:
950 return flattenLiteralNode((LiteralNode)en,out_temp);
952 case Kind.MethodInvokeNode:
953 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
956 return flattenNameNode((NameNode)en,out_temp);
959 return flattenOpNode((OpNode)en,out_temp);
961 case Kind.OffsetNode:
962 return flattenOffsetNode((OffsetNode)en,out_temp);
964 case Kind.TertiaryNode:
965 return flattenTertiaryNode((TertiaryNode)en,out_temp);
967 case Kind.InstanceOfNode:
968 return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
970 case Kind.ArrayInitializerNode:
971 return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
976 private NodePair flattenDeclarationNode(DeclarationNode dn) {
977 VarDescriptor vd=dn.getVarDescriptor();
978 TempDescriptor td=getTempforVar(vd);
979 if (dn.getExpression()!=null)
980 return flattenExpressionNode(dn.getExpression(),td);
982 FlatNop fn=new FlatNop();
983 return new NodePair(fn,fn);
987 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
988 TagVarDescriptor tvd=dn.getTagVarDescriptor();
989 TagDescriptor tag=tvd.getTag();
990 TempDescriptor tmp=getTempforVar(tvd);
991 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
992 return new NodePair(ftd,ftd);
995 private TempDescriptor getTempforParam(Descriptor d) {
996 if (temptovar.containsKey(d))
997 return (TempDescriptor)temptovar.get(d);
999 if (d instanceof VarDescriptor) {
1000 VarDescriptor vd=(VarDescriptor)d;
1001 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
1002 temptovar.put(vd,td);
1004 } else if (d instanceof TagVarDescriptor) {
1005 TagVarDescriptor tvd=(TagVarDescriptor)d;
1006 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1007 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
1008 temptovar.put(tvd,td);
1010 } else throw new Error("Unreconized Descriptor");
1014 private TempDescriptor getTempforVar(Descriptor d) {
1015 if (temptovar.containsKey(d))
1016 return (TempDescriptor)temptovar.get(d);
1018 if (d instanceof VarDescriptor) {
1019 VarDescriptor vd=(VarDescriptor)d;
1020 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
1021 temptovar.put(vd,td);
1023 } else if (d instanceof TagVarDescriptor) {
1024 TagVarDescriptor tvd=(TagVarDescriptor)d;
1025 //BUGFIX TAGTYPE - add next line, modify following
1026 //line to tag this new type descriptor, modify
1027 //TempDescriptor constructor & factory to set type
1028 //using this Type To test, use any program with tags
1029 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1030 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
1031 temptovar.put(tvd,td);
1033 } else throw new Error("Unrecognized Descriptor");
1037 private NodePair flattenIfStatementNode(IfStatementNode isn) {
1038 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1039 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
1040 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1041 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
1043 FlatNop nopend=new FlatNop();
1045 if (isn.getFalseBlock()!=null)
1046 false_np=flattenBlockNode(isn.getFalseBlock());
1048 FlatNop nop=new FlatNop();
1049 false_np=new NodePair(nop,nop);
1052 cond.getEnd().addNext(fcb);
1053 fcb.addTrueNext(true_np.getBegin());
1054 fcb.addFalseNext(false_np.getBegin());
1055 if (true_np.getEnd()!=null)
1056 true_np.getEnd().addNext(nopend);
1057 if (false_np.getEnd()!=null)
1058 false_np.getEnd().addNext(nopend);
1059 if (nopend.numPrev()==0)
1060 return new NodePair(cond.getBegin(), null);
1062 return new NodePair(cond.getBegin(), nopend);
1065 private NodePair flattenSwitchStatementNode(SwitchStatementNode ssn) {
1066 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.INT));
1067 NodePair cond=flattenExpressionNode(ssn.getCondition(),cond_temp);
1068 FlatNop nopend=new FlatNop();
1069 NodePair sbody = flattenSwitchBodyNode(ssn.getSwitchBody(), cond_temp, nopend);
1071 cond.getEnd().addNext(sbody.getBegin());
1073 return new NodePair(cond.getBegin(), sbody.getEnd());
1076 private NodePair flattenSwitchBodyNode(BlockNode bn, TempDescriptor cond_temp, FlatNode endnode) {
1077 FlatNode begin=null;
1078 FlatNode end=endnode;
1079 NodePair prev_true_branch = null;
1080 NodePair prev_false_branch = null;
1081 for(int i=0; i<bn.size(); i++) {
1082 SwitchBlockNode sbn = (SwitchBlockNode)bn.get(i);
1083 HashSet oldbs=breakset;
1084 breakset=new HashSet();
1086 NodePair body=flattenBlockNode(sbn.getSwitchBlockStatement());
1087 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
1088 FlatNode cond_begin = null;
1089 NodePair prev_fnp = null;
1090 for(int j = 0; j < slnv.size(); j++) {
1091 SwitchLabelNode sln = slnv.elementAt(j);
1092 NodePair left = null;
1093 NodePair false_np = null;
1094 if(sln.isDefault()) {
1097 TempDescriptor cond_tmp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1098 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop", sln.getCondition().getType());
1099 Operation op=new Operation(Operation.EQUAL);
1100 left=flattenExpressionNode(sln.getCondition(), temp_left);
1101 FlatOpNode fon=new FlatOpNode(cond_tmp, temp_left, cond_temp, op);
1102 left.getEnd().addNext(fon);
1104 FlatCondBranch fcb=new FlatCondBranch(cond_tmp);
1105 fcb.setTrueProb(State.TRUEPROB);
1107 FlatNop nop=new FlatNop();
1108 false_np=new NodePair(nop,nop);
1111 fcb.addTrueNext(body.getBegin());
1112 fcb.addFalseNext(false_np.getBegin());
1114 if((prev_fnp != null) && (prev_fnp.getEnd() != null)) {
1115 prev_fnp.getEnd().addNext(left.getBegin());
1117 prev_fnp = false_np;
1120 begin = left.getBegin();
1122 if(cond_begin == null) {
1123 cond_begin = left.getBegin();
1126 if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1127 prev_false_branch.getEnd().addNext(cond_begin);
1129 prev_false_branch = prev_fnp;
1130 if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1131 prev_true_branch.getEnd().addNext(body.getBegin());
1133 prev_true_branch = body;
1134 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1135 FlatNode fn=(FlatNode)breakit.next();
1137 fn.addNext(endnode);
1141 if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1142 prev_true_branch.getEnd().addNext(endnode);
1144 if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1145 prev_false_branch.getEnd().addNext(endnode);
1148 end=begin=new FlatNop();
1150 return new NodePair(begin,end);
1153 private NodePair flattenLoopNode(LoopNode ln) {
1154 HashSet oldbs=breakset;
1155 HashSet oldcs=continueset;
1156 breakset=new HashSet();
1157 continueset=new HashSet();
1159 if (ln.getType()==LoopNode.FORLOOP) {
1160 NodePair initializer=flattenBlockNode(ln.getInitializer());
1161 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1162 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1163 NodePair update=flattenBlockNode(ln.getUpdate());
1164 NodePair body=flattenBlockNode(ln.getBody());
1165 FlatNode begin=initializer.getBegin();
1166 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1167 fcb.setTrueProb(State.TRUEPROB);
1169 FlatNop nopend=new FlatNop();
1170 FlatBackEdge backedge=new FlatBackEdge();
1172 FlatNop nop2=new FlatNop();
1173 initializer.getEnd().addNext(nop2);
1174 nop2.addNext(condition.getBegin());
1175 if (body.getEnd()!=null)
1176 body.getEnd().addNext(update.getBegin());
1177 update.getEnd().addNext(backedge);
1178 backedge.addNext(condition.getBegin());
1179 condition.getEnd().addNext(fcb);
1180 fcb.addFalseNext(nopend);
1181 fcb.addTrueNext(body.getBegin());
1182 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1183 FlatNode fn=(FlatNode)contit.next();
1185 fn.addNext(update.getBegin());
1187 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1188 FlatNode fn=(FlatNode)breakit.next();
1194 return new NodePair(begin,nopend);
1195 } else if (ln.getType()==LoopNode.WHILELOOP) {
1196 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1197 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1198 NodePair body=flattenBlockNode(ln.getBody());
1199 FlatNode begin=condition.getBegin();
1200 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1201 fcb.setTrueProb(State.TRUEPROB);
1203 FlatNop nopend=new FlatNop();
1204 FlatBackEdge backedge=new FlatBackEdge();
1206 if (body.getEnd()!=null)
1207 body.getEnd().addNext(backedge);
1208 backedge.addNext(condition.getBegin());
1210 condition.getEnd().addNext(fcb);
1211 fcb.addFalseNext(nopend);
1212 fcb.addTrueNext(body.getBegin());
1214 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1215 FlatNode fn=(FlatNode)contit.next();
1217 fn.addNext(backedge);
1219 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1220 FlatNode fn=(FlatNode)breakit.next();
1226 return new NodePair(begin,nopend);
1227 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
1228 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1229 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1230 NodePair body=flattenBlockNode(ln.getBody());
1231 FlatNode begin=body.getBegin();
1232 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1233 fcb.setTrueProb(State.TRUEPROB);
1235 FlatNop nopend=new FlatNop();
1236 FlatBackEdge backedge=new FlatBackEdge();
1238 if (body.getEnd()!=null)
1239 body.getEnd().addNext(condition.getBegin());
1240 condition.getEnd().addNext(fcb);
1241 fcb.addFalseNext(nopend);
1242 fcb.addTrueNext(backedge);
1243 backedge.addNext(body.getBegin());
1245 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1246 FlatNode fn=(FlatNode)contit.next();
1248 fn.addNext(condition.getBegin());
1250 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1251 FlatNode fn=(FlatNode)breakit.next();
1257 return new NodePair(begin,nopend);
1258 } else throw new Error();
1261 private NodePair flattenReturnNode(ReturnNode rntree) {
1262 TempDescriptor retval=null;
1264 if (rntree.getReturnExpression()!=null) {
1265 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
1266 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1269 FlatReturnNode rnflat=new FlatReturnNode(retval);
1272 if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
1273 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1274 TempDescriptor thistd=getTempforVar(currmd.getThis());
1275 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
1279 if (state.DSM&&currmd.getModifiers().isAtomic()) {
1280 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1286 cond.getEnd().addNext(ln);
1287 return new NodePair(cond.getBegin(),null);
1289 return new NodePair(ln,null);
1292 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1293 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1294 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1295 updateFlagActionNode(ffan, ten.getFlagEffects());
1296 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1297 ffan.addNext(fcn.getBegin());
1298 FlatReturnNode rnflat=new FlatReturnNode(null);
1300 fcn.getEnd().addNext(rnflat);
1301 return new NodePair(ffan, null);
1304 private NodePair flattenConstraintCheck(Vector ccs) {
1305 FlatNode begin=new FlatNop();
1307 return new NodePair(begin,begin);
1308 FlatNode last=begin;
1309 for(int i=0; i<ccs.size(); i++) {
1310 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1311 /* Flatten the arguments */
1312 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1313 String[] vars=new String[cc.numArgs()];
1314 for(int j=0; j<cc.numArgs(); j++) {
1315 ExpressionNode en=cc.getArg(j);
1316 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1318 vars[j]=cc.getVar(j);
1319 NodePair np=flattenExpressionNode(en, td);
1320 last.addNext(np.getBegin());
1324 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1328 return new NodePair(begin,last);
1331 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1332 return flattenBlockNode(sbn.getBlockNode());
1335 private NodePair flattenSynchronizedNode(SynchronizedNode sbn) {
1336 TempDescriptor montmp=TempDescriptor.tempFactory("monitor",sbn.getExpr().getType());
1337 NodePair npexp=flattenExpressionNode(sbn.getExpr(), montmp);
1338 NodePair npblock=flattenBlockNode(sbn.getBlockNode());
1340 MethodDescriptor menmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
1341 FlatCall fcen=new FlatCall(menmd, null, montmp, new TempDescriptor[0]);
1343 MethodDescriptor mexmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1344 FlatCall fcex=new FlatCall(mexmd, null, montmp, new TempDescriptor[0]);
1346 npexp.getEnd().addNext(fcen);
1347 fcen.addNext(npblock.getBegin());
1348 npblock.getEnd().addNext(fcex);
1349 return new NodePair(npexp.getBegin(), fcex);
1352 private NodePair flattenAtomicNode(AtomicNode sbn) {
1353 NodePair np=flattenBlockNode(sbn.getBlockNode());
1354 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1355 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1356 faen.addNext(np.getBegin());
1357 np.getEnd().addNext(faexn);
1358 return new NodePair(faen, faexn);
1361 private NodePair flattenGenReachNode( GenReachNode grn ) {
1362 FlatGenReachNode fgrn = new FlatGenReachNode( grn.getGraphName() );
1363 return new NodePair( fgrn, fgrn );
1366 private NodePair flattenSESENode(SESENode sn) {
1367 if( sn.isStart() ) {
1368 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1369 sn.setFlatEnter(fsen);
1370 return new NodePair(fsen, fsen);
1373 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1374 sn.setFlatExit(fsexn);
1375 FlatSESEEnterNode fsen=sn.getStart().getFlatEnter();
1376 fsexn.setFlatEnter(fsen);
1377 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1379 return new NodePair(fsexn, fsexn);
1382 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1383 FlatNop fn=new FlatNop();
1387 continueset.add(fn);
1388 return new NodePair(fn,null);
1391 private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1392 TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1393 NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1394 FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1395 cond.getEnd().addNext(fion);
1396 return new NodePair(cond.getBegin(),fion);
1399 private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1400 boolean isGlobal = false;
1401 String disjointId = null;
1402 // get the type the array to be initialized
1403 TypeDescriptor td = ain.getType();
1405 // create a new array of size equal to the array initializer
1406 FlatNode first=null;
1408 TempDescriptor tmp=TempDescriptor.tempFactory("arg", new TypeDescriptor(TypeDescriptor.INT));
1409 FlatLiteralNode fln_tmp=new FlatLiteralNode(tmp.getType(), new Integer(ain.numVarInitializers()), tmp);
1410 first = last=fln_tmp;
1412 // create the new array
1413 FlatNew fn=new FlatNew(td, out_temp, tmp, isGlobal, disjointId);
1417 // initialize the new array
1418 for(int i = 0; i < ain.numVarInitializers(); i++) {
1419 ExpressionNode var_init_node = ain.getVarInitializer(i);
1420 TempDescriptor tmp_toinit = out_temp;
1421 TempDescriptor tmp_init=TempDescriptor.tempFactory("array_init", td.dereference());
1423 TempDescriptor index=TempDescriptor.tempFactory("index", new TypeDescriptor(TypeDescriptor.INT));
1424 FlatLiteralNode fln=new FlatLiteralNode(index.getType(), new Integer(i), index);
1425 // calculate the initial value
1426 NodePair np_init = flattenExpressionNode(var_init_node, tmp_init);
1427 // TODO wrapper class process is missing now
1428 /*if(td.isArray() && td.dereference().iswrapper()) {
1430 FlatSetElementNode fsen=new FlatSetElementNode(tmp_toinit, index, tmp_init);
1432 fln.addNext(np_init.getBegin());
1433 np_init.getEnd().addNext(fsen);
1437 return new NodePair(first, last);
1440 private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1441 TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1442 TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1443 TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1445 NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1446 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1448 NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1449 FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1451 NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1452 FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1454 FlatNop nopend=new FlatNop();
1456 cond.getEnd().addNext(fcb);
1458 fcb.addTrueNext(trueExpr.getBegin());
1459 fcb.addFalseNext(falseExpr.getBegin());
1461 trueExpr.getEnd().addNext(fonT);
1462 fonT.addNext(nopend);
1464 falseExpr.getEnd().addNext(fonF);
1465 fonF.addNext(nopend);
1467 return new NodePair(cond.getBegin(), nopend);
1470 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1471 switch(bsn.kind()) {
1472 case Kind.BlockExpressionNode:
1473 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1475 case Kind.DeclarationNode:
1476 return flattenDeclarationNode((DeclarationNode)bsn);
1478 case Kind.TagDeclarationNode:
1479 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1481 case Kind.IfStatementNode:
1482 return flattenIfStatementNode((IfStatementNode)bsn);
1484 case Kind.SwitchStatementNode:
1485 return flattenSwitchStatementNode((SwitchStatementNode)bsn);
1488 return flattenLoopNode((LoopNode)bsn);
1490 case Kind.ReturnNode:
1491 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1493 case Kind.TaskExitNode:
1494 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1496 case Kind.SubBlockNode:
1497 return flattenSubBlockNode((SubBlockNode)bsn);
1499 case Kind.AtomicNode:
1500 return flattenAtomicNode((AtomicNode)bsn);
1502 case Kind.SynchronizedNode:
1503 return flattenSynchronizedNode((SynchronizedNode)bsn);
1506 return flattenSESENode((SESENode)bsn);
1508 case Kind.GenReachNode:
1509 return flattenGenReachNode((GenReachNode)bsn);
1511 case Kind.ContinueBreakNode:
1512 return flattenContinueBreakNode((ContinueBreakNode)bsn);