7 public class BuildFlat {
10 MethodDescriptor currmd;
17 // for synchronized blocks
18 Stack<TempDescriptor> lockStack;
20 public BuildFlat(State st, TypeUtil typeutil) {
22 temptovar=new Hashtable();
23 this.typeutil=typeutil;
24 this.breakset=new HashSet();
25 this.continueset=new HashSet();
26 this.lockStack = new Stack<TempDescriptor>();
29 public Hashtable getMap() {
33 public void buildFlat() {
34 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
36 ClassDescriptor cn=(ClassDescriptor)it.next();
40 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
41 while(task_it.hasNext()) {
42 TaskDescriptor td=(TaskDescriptor)task_it.next();
47 private void flattenTask(TaskDescriptor td) {
48 BlockNode bn=state.getMethodBody(td);
49 NodePair np=flattenBlockNode(bn);
50 FlatNode fn=np.getBegin();
52 FlatNode fn2=np.getEnd();
54 if (fn2!=null&& fn2.kind()!=FKind.FlatReturnNode) {
55 FlatReturnNode rnflat=new FlatReturnNode(null);
60 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
61 ffan.setNumLine(bn.getNumLine());
64 FlatMethod fm=new FlatMethod(td, fe);
67 Hashtable visitedset=new Hashtable();
69 for(int i=0; i<td.numParameters(); i++) {
70 VarDescriptor paramvd=td.getParameter(i);
71 fm.addParameterTemp(getTempforVar(paramvd));
72 TagExpressionList tel=td.getTag(paramvd);
73 //BUG added next line to fix...to test feed in any task program
75 for(int j=0; j<tel.numTags(); j++) {
76 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
77 TempDescriptor tagtmp=getTempforVar(tvd);
78 if (!visitedset.containsKey(tvd.getName())) {
79 visitedset.put(tvd.getName(),tvd.getTag());
80 fm.addTagTemp(tagtmp);
82 TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
83 if (!tmptd.equals(tvd.getTag()))
84 throw new Error("Two different tag types with same name as parameters to:"+td);
86 tel.setTemp(j, tagtmp);
90 /* Flatten Vector of Flag Effects */
91 Vector flags=td.getFlagEffects();
92 updateFlagActionNode(ffan,flags);
94 state.addFlatCode(td,fm);
98 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
99 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
100 if (flags==null) // Do nothing if the flag effects vector is empty
103 for(int i=0; i<flags.size(); i++) {
104 FlagEffects fes=(FlagEffects)flags.get(i);
105 TempDescriptor flagtemp=getTempforVar(fes.getVar());
107 for(int j=0; j<fes.numEffects(); j++) {
108 FlagEffect fe=fes.getEffect(j);
109 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
112 for(int j=0; j<fes.numTagEffects(); j++) {
113 TagEffect te=fes.getTagEffect(j);
114 TempDescriptor tagtemp=getTempforVar(te.getTag());
116 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
121 FlatAtomicEnterNode curran=null;
123 private FlatNode spliceReturn(FlatNode fn) {
124 FlatReturnNode rnflat=null;
125 if (currmd.getReturnType()==null||currmd.getReturnType().isVoid()) {
126 rnflat=new FlatReturnNode(null);
129 TempDescriptor tmp=TempDescriptor.tempFactory("rettmp",currmd.getReturnType());
131 if (currmd.getReturnType().isPtr()) {
133 } else if (currmd.getReturnType().isByte()) {
135 } else if (currmd.getReturnType().isShort()) {
136 o=new Short((short)0);
137 } else if (currmd.getReturnType().isChar()) {
138 o=new Character('\0');
139 } else if (currmd.getReturnType().isInt()) {
141 } else if (currmd.getReturnType().isLong()) {
143 } else if (currmd.getReturnType().isBoolean()) {
144 o=new Boolean(false);
145 } else if (currmd.getReturnType().isFloat()) {
147 } else if (currmd.getReturnType().isDouble()) {
152 FlatLiteralNode fln=new FlatLiteralNode(currmd.getReturnType(),o,tmp);
153 rnflat=new FlatReturnNode(tmp);
160 private void flattenClass(ClassDescriptor cn) {
161 Iterator methodit=cn.getMethods();
162 while(methodit.hasNext()) {
163 currmd=(MethodDescriptor)methodit.next();
165 // if OOOJava is on, splice a special SESE in to
166 // enclose the main method
167 boolean spliceInImplicitMain =
169 currmd.equals( typeutil.getMain() );
171 FlatSESEEnterNode spliceSESE = null;
172 FlatSESEExitNode spliceExit = null;
174 if( spliceInImplicitMain ) {
175 SESENode mainTree = new SESENode( "main" );
176 spliceSESE = new FlatSESEEnterNode( mainTree );
177 spliceExit = new FlatSESEExitNode ( mainTree );
178 spliceSESE.setFlatExit ( spliceExit );
179 spliceExit.setFlatEnter( spliceSESE );
180 spliceSESE.setIsMainSESE();
186 BlockNode bn=state.getMethodBody(currmd);
188 if (state.DSM&&currmd.getModifiers().isAtomic()) {
189 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
190 faen.setNumLine(bn.getNumLine());
194 if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
195 TempDescriptor thistd = null;
196 if(currmd.getModifiers().isStatic()) {
197 // need to lock the Class object
198 thistd=new TempDescriptor("classobj", cn);
201 thistd=getTempforVar(currmd.getThis());
203 if(!this.lockStack.isEmpty()) {
204 throw new Error("The lock stack for synchronized blocks/methods is not empty!");
206 this.lockStack.push(thistd);
208 NodePair np=flattenBlockNode(bn);
209 FlatNode fn=np.getBegin();
210 if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
211 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
212 FlatNode first = null;
216 if (lockStack.size()!=1) {
217 throw new Error("TOO MANY THINGS ON LOCKSTACK");
219 TempDescriptor thistd = this.lockStack.elementAt(0);
220 FlatCall fc = new FlatCall(memd, null, thistd, new TempDescriptor[0]);
221 fc.setNumLine(bn.getNumLine());
228 if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
229 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
230 while(!this.lockStack.isEmpty()) {
231 TempDescriptor thistd = this.lockStack.pop();
232 FlatCall fcunlock = new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
233 fcunlock.setNumLine(bn.getNumLine());
234 end.addNext(fcunlock);
237 FlatNode rnflat=spliceReturn(end);
240 this.lockStack.clear();
242 } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
245 if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
246 FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
247 np.getEnd().addNext(aen);
248 FlatNode rnflat=spliceReturn(aen);
252 } else if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
253 FlatNode rnflat=null;
254 if( spliceInImplicitMain ) {
255 np.getEnd().addNext(spliceExit);
256 rnflat=spliceReturn(spliceExit);
258 rnflat=spliceReturn(np.getEnd());
261 } else if (np.getEnd()!=null) {
262 if( spliceInImplicitMain ) {
263 FlatReturnNode rnflat=(FlatReturnNode)np.getEnd();
264 np.getEnd().addNext(spliceExit);
265 spliceExit.addNext(fe);
269 if( spliceInImplicitMain ) {
270 spliceSESE.addNext(fn);
274 FlatMethod fm=new FlatMethod(currmd, fe);
275 fm.setNumLine(bn.getNumLine());
277 if (!currmd.isStatic())
278 fm.addParameterTemp(getTempforParam(currmd.getThis()));
279 for(int i=0; i<currmd.numParameters(); i++) {
280 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
283 state.addFlatCode(currmd,fm);
287 private NodePair flattenBlockNode(BlockNode bn) {
290 for(int i=0; i<bn.size(); i++) {
291 NodePair np=flattenBlockStatementNode(bn.get(i));
292 FlatNode np_begin=np.getBegin();
293 FlatNode np_end=np.getEnd();
300 end.addNext(np_begin);
302 return new NodePair(begin, null);
308 end=begin=new FlatNop();
310 return new NodePair(begin,end);
313 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
314 //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
315 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
316 return flattenExpressionNode(en.getExpression(),tmp);
319 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
320 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
321 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
322 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
323 fcn.setNumLine(cn.getNumLine());
324 np.getEnd().addNext(fcn);
325 return new NodePair(np.getBegin(),fcn);
328 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
329 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
330 fln.setNumLine(ln.getNumLine());
331 return new NodePair(fln,fln);
334 private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
335 FlatOffsetNode fln = new FlatOffsetNode(ofn.getClassType(), ofn.getField(), out_temp);
336 fln.setNumLine(ofn.getNumLine());
337 return new NodePair(fln, fln);
340 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
341 TypeDescriptor td=con.getType();
343 FlatNode fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
345 //handle wrapper fields
346 ClassDescriptor cd=td.getClassDesc();
347 for(Iterator fieldit=cd.getFields();fieldit.hasNext();) {
348 FieldDescriptor fd=(FieldDescriptor)fieldit.next();
349 if (fd.getType().iswrapper()) {
350 TempDescriptor wrap_tmp=TempDescriptor.tempFactory("wrapper_obj",fd.getType());
351 FlatNode fnwrapper=new FlatNew(fd.getType(), wrap_tmp, con.isGlobal());
352 fnwrapper.setNumLine(con.getNumLine());
353 FlatSetFieldNode fsfn=new FlatSetFieldNode(out_temp, fd, wrap_tmp);
354 fsfn.setNumLine(con.getNumLine());
355 last.addNext(fnwrapper);
356 fnwrapper.addNext(fsfn);
361 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
364 for(int i=0; i<con.numArgs(); i++) {
365 ExpressionNode en=con.getArg(i);
366 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
368 NodePair np=flattenExpressionNode(en, tmp);
369 last.addNext(np.getBegin());
372 MethodDescriptor md=con.getConstructor();
373 //Call to constructor
374 FlatCall fc=new FlatCall(md, null, out_temp, temps);
375 fc.setNumLine(con.getNumLine());
378 if (td.getClassDesc().hasFlags()) {
379 // if (con.getFlagEffects()!=null) {
380 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
381 ffan.setNumLine(con.getNumLine());
382 FlagEffects fes=con.getFlagEffects();
383 TempDescriptor flagtemp=out_temp;
385 for(int j=0; j<fes.numEffects(); j++) {
386 FlagEffect fe=fes.getEffect(j);
387 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
389 for(int j=0; j<fes.numTagEffects(); j++) {
390 TagEffect te=fes.getTagEffect(j);
391 TempDescriptor tagtemp=getTempforVar(te.getTag());
393 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
396 ffan.addFlagAction(flagtemp, null, false);
401 return new NodePair(fn,last);
403 if(con.getArrayInitializer() == null) {
406 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
407 for (int i=0; i<con.numArgs(); i++) {
408 ExpressionNode en=con.getArg(i);
409 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
411 NodePair np=flattenExpressionNode(en, tmp);
415 last.addNext(np.getBegin());
418 TempDescriptor tmp2=(i==0) ?
420 TempDescriptor.tempFactory("arg",en.getType());
422 FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
424 if (temps.length>1) {
425 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
426 fn.addNext(np.getBegin());
427 return new NodePair(first,np.getEnd());
428 } else if (td.isArray()&&td.dereference().iswrapper()) {
429 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
430 fn.addNext(np.getBegin());
431 return new NodePair(first,np.getEnd());
433 return new NodePair(first, fn);
435 // array creation with initializers
436 return flattenArrayInitializerNode(con.getArrayInitializer(), out_temp);
441 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
442 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
443 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
444 FlatNop fnop=new FlatNop(); //last node
447 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
449 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
451 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
453 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
454 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
455 fcb.setTrueProb(State.TRUEPROB);
458 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
459 FlatNew fn=td.iswrapper()?new FlatNew(td, new_tmp, isglobal):new FlatNew(td, new_tmp, temparray[i+1], isglobal);
460 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
462 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
468 fcb.addFalseNext(fnop);
470 //Recursive call here
471 if ((i+2)<temparray.length) {
472 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
473 fsen.addNext(np2.getBegin());
474 np2.getEnd().addNext(fon);
475 } else if (td.isArray()&&td.dereference().iswrapper()) {
476 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
477 fsen.addNext(np2.getBegin());
478 np2.getEnd().addNext(fon);
483 return new NodePair(fln, fnop);
486 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
487 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
490 TempDescriptor thisarg=null;
492 if (min.getExpression()!=null) {
493 TypeDescriptor mtd = min.getExpression().getType();
494 if(mtd.isClass() && mtd.getClassDesc().isEnum()) {
495 mtd = new TypeDescriptor(TypeDescriptor.INT);
497 thisarg=TempDescriptor.tempFactory("thisarg", mtd);
498 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
504 for(int i=0; i<min.numArgs(); i++) {
505 ExpressionNode en=min.getArg(i);
506 TypeDescriptor etd = en.getType();
507 if(etd.isClass() && etd.getClassDesc().isEnum()) {
508 etd = new TypeDescriptor(TypeDescriptor.INT);
510 TempDescriptor td=TempDescriptor.tempFactory("arg", etd);
512 NodePair np=flattenExpressionNode(en, td);
516 last.addNext(np.getBegin());
520 MethodDescriptor md=min.getMethod();
522 //Call to constructor
525 if(md.getReturnType()==null||md.getReturnType().isVoid())
526 fc=new FlatCall(md, null, thisarg, temps, min.getSuper());
528 fc=new FlatCall(md, out_temp, thisarg, temps, min.getSuper());
530 fc.setNumLine(min.getNumLine());
536 return new NodePair(first,fc);
539 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
540 TempDescriptor tmp=null;
541 if(fan.getExpression().getType().isClassNameRef()) {
542 // static field dereference with class name
543 tmp = new TempDescriptor(fan.getExpression().getType().getClassDesc().getSymbol(), fan.getExpression().getType());
544 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
545 fn.setNumLine(fan.getNumLine());
546 return new NodePair(fn,fn);
548 tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
549 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
550 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
551 fn.setNumLine(fan.getNumLine());
552 npe.getEnd().addNext(fn);
553 return new NodePair(npe.getBegin(),fn);
557 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
558 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
559 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
560 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
561 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
562 TempDescriptor arraytmp=out_temp;
563 if (aan.iswrapper()) {
565 arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference());
567 FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp);
568 fn.setNumLine(aan.getNumLine());
569 npe.getEnd().addNext(npi.getBegin());
570 npi.getEnd().addNext(fn);
571 if (aan.iswrapper()) {
572 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp);
573 ffn.setNumLine(aan.getNumLine());
577 return new NodePair(npe.getBegin(),fn);
580 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
582 // left side is variable
583 // left side is field
584 // left side is array
586 Operation base=an.getOperation().getBaseOp();
587 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
590 //rewrite the base operation
591 base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
595 TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
598 if (an.getSrc()!=null) {
599 if(an.getSrc().getEval() != null) {
600 FlatLiteralNode fln=new FlatLiteralNode(an.getSrc().getType(), an.getSrc().getEval().longValue(), src_tmp);
601 fln.setNumLine(an.getSrc().getNumLine());
604 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
605 first=np_src.getBegin();
606 last=np_src.getEnd();
609 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
610 fln.setNumLine(an.getNumLine());
615 if (an.getDest().kind()==Kind.FieldAccessNode) {
616 //We are assigning an object field
618 FieldAccessNode fan=(FieldAccessNode)an.getDest();
619 ExpressionNode en=fan.getExpression();
620 TempDescriptor dst_tmp=null;
621 NodePair np_baseexp=null;
622 if(en.getType().isClassNameRef()) {
623 // static field dereference with class name
624 dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
625 FlatNop nop=new FlatNop();
626 np_baseexp = new NodePair(nop,nop);
628 dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
629 np_baseexp=flattenExpressionNode(en, dst_tmp);
632 first=np_baseexp.getBegin();
634 last.addNext(np_baseexp.getBegin());
635 last=np_baseexp.getEnd();
637 //See if we need to perform an operation
639 //If it is a preinc we need to store the initial value
640 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
641 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
642 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
643 ffn.setNumLine(an.getNumLine());
647 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
648 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
649 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
650 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
651 fc.setNumLine(an.getNumLine());
656 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
657 fon.setNumLine(an.getNumLine());
664 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
665 fsfn.setNumLine(en.getNumLine());
669 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
670 fon2.setNumLine(an.getNumLine());
674 return new NodePair(first, last);
675 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
676 //We are assigning an array element
678 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
679 ExpressionNode en=aan.getExpression();
680 ExpressionNode enindex=aan.getIndex();
681 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
682 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
683 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
684 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
686 first=np_baseexp.getBegin();
688 last.addNext(np_baseexp.getBegin());
689 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
690 last=np_indexexp.getEnd();
692 //See if we need to perform an operation
694 //If it is a preinc we need to store the initial value
695 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
696 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
698 if (aan.iswrapper()) {
699 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
700 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
701 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
702 fen.setNumLine(aan.getNumLine());
703 FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp3,src_tmp2);
704 ffn.setNumLine(aan.getNumLine());
709 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
710 fen.setNumLine(aan.getNumLine());
714 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
715 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
716 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
717 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
718 fc.setNumLine(an.getNumLine());
723 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
724 fon.setNumLine(an.getNumLine());
731 if (aan.iswrapper()) {
732 TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
733 TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
734 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
735 fen.setNumLine(aan.getNumLine());
736 FlatSetFieldNode fsfn=new FlatSetFieldNode(src_tmp3,(FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp);
737 fsfn.setNumLine(aan.getExpression().getNumLine());
742 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
743 fsen.setNumLine(aan.getNumLine());
748 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
749 fon2.setNumLine(an.getNumLine());
753 return new NodePair(first, last);
754 } else if (an.getDest().kind()==Kind.NameNode) {
755 //We could be assigning a field or variable
756 NameNode nn=(NameNode)an.getDest();
759 if (nn.getExpression()!=null) {
761 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
762 ExpressionNode en=fan.getExpression();
763 TempDescriptor dst_tmp=null;
764 NodePair np_baseexp=null;
765 if(en.getType().isClassNameRef()) {
766 // static field dereference with class name
767 dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
768 FlatNop nop=new FlatNop();
769 np_baseexp = new NodePair(nop,nop);
771 dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
772 np_baseexp=flattenExpressionNode(en, dst_tmp);
775 first=np_baseexp.getBegin();
777 last.addNext(np_baseexp.getBegin());
778 last=np_baseexp.getEnd();
780 //See if we need to perform an operation
782 //If it is a preinc we need to store the initial value
783 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
784 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
786 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
787 ffn.setNumLine(an.getNumLine());
792 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
793 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
794 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
795 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
796 fc.setNumLine(an.getNumLine());
801 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
802 fon.setNumLine(an.getNumLine());
810 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
811 fsfn.setNumLine(en.getNumLine());
815 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
816 fon2.setNumLine(an.getNumLine());
820 return new NodePair(first, last);
822 if (nn.getField()!=null) {
826 //See if we need to perform an operation
828 //If it is a preinc we need to store the initial value
829 TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
830 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
832 TempDescriptor ftmp= null;
833 if((nn.getClassDesc() != null)) {
834 // this is a static field
835 ftmp = new TempDescriptor(nn.getClassDesc().getSymbol(), nn.getClassType());
838 ftmp=getTempforVar(nn.getVar());
840 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), ftmp, src_tmp2);
841 ffn.setNumLine(an.getNumLine());
851 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
852 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
853 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
854 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
855 fc.setNumLine(an.getNumLine());
860 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
861 fon.setNumLine(an.getNumLine());
868 FlatSetFieldNode fsfn=null;
869 if(nn.getClassDesc()!=null) {
870 // this is a static field access inside of a static block
871 fsfn=new FlatSetFieldNode(new TempDescriptor("sfsb", nn.getClassType()), nn.getField(), src_tmp);
872 fsfn.setNumLine(nn.getNumLine());
874 fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
875 fsfn.setNumLine(nn.getNumLine());
884 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
885 fon2.setNumLine(an.getNumLine());
889 return new NodePair(first, last);
892 //See if we need to perform an operation
895 //If it is a preinc we need to store the initial value
896 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
897 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
899 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
900 fon.setNumLine(an.getNumLine());
909 if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
910 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
911 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
912 FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
913 fc.setNumLine(an.getNumLine());
921 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
922 fon.setNumLine(an.getNumLine());
932 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
933 fon.setNumLine(an.getNumLine());
938 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
939 fon2.setNumLine(an.getNumLine());
943 return new NodePair(first, last);
951 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
952 if (nn.getExpression()!=null) {
953 /* Hack - use subtree instead */
954 return flattenExpressionNode(nn.getExpression(),out_temp);
955 } else if (nn.getField()!=null) {
956 TempDescriptor tmp= null;
957 if((nn.getClassDesc() != null)) {
958 // this is a static field
959 tmp = new TempDescriptor(nn.getClassDesc().getSymbol(), nn.getClassType());
962 tmp=getTempforVar(nn.getVar());
964 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
965 ffn.setNumLine(nn.getNumLine());
966 return new NodePair(ffn,ffn);
968 TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
971 out_temp.setTag(tmp.getTag());
973 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
974 fon.setNumLine(nn.getNumLine());
975 return new NodePair(fon,fon);
979 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
980 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
981 TempDescriptor temp_right=null;
983 Operation op=on.getOp();
985 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
987 if (on.getRight()!=null) {
988 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
989 right=flattenExpressionNode(on.getRight(),temp_right);
991 FlatNop nop=new FlatNop();
992 right=new NodePair(nop,nop);
995 if (op.getOp()==Operation.LOGIC_OR) {
996 /* Need to do shortcircuiting */
997 FlatCondBranch fcb=new FlatCondBranch(temp_left);
998 fcb.setNumLine(on.getNumLine());
999 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
1000 fon1.setNumLine(on.getNumLine());
1001 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
1002 fon2.setNumLine(on.getNumLine());
1003 FlatNop fnop=new FlatNop();
1004 left.getEnd().addNext(fcb);
1005 fcb.addFalseNext(right.getBegin());
1006 right.getEnd().addNext(fon2);
1008 fcb.addTrueNext(fon1);
1010 return new NodePair(left.getBegin(), fnop);
1011 } else if (op.getOp()==Operation.LOGIC_AND) {
1012 /* Need to do shortcircuiting */
1013 FlatCondBranch fcb=new FlatCondBranch(temp_left);
1014 fcb.setNumLine(on.getNumLine());
1015 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
1016 fon1.setNumLine(on.getNumLine());
1017 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
1018 fon2.setNumLine(on.getNumLine());
1019 FlatNop fnop=new FlatNop();
1020 left.getEnd().addNext(fcb);
1021 fcb.addTrueNext(right.getBegin());
1022 right.getEnd().addNext(fon2);
1024 fcb.addFalseNext(fon1);
1026 return new NodePair(left.getBegin(), fnop);
1027 } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
1028 //We have a string concatenate
1029 ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
1030 MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
1031 FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
1032 fc.setNumLine(on.getNumLine());
1033 left.getEnd().addNext(right.getBegin());
1034 right.getEnd().addNext(fc);
1035 return new NodePair(left.getBegin(), fc);
1038 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
1039 fon.setNumLine(on.getNumLine());
1040 left.getEnd().addNext(right.getBegin());
1041 right.getEnd().addNext(fon);
1042 return new NodePair(left.getBegin(),fon);
1045 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
1047 case Kind.AssignmentNode:
1048 return flattenAssignmentNode((AssignmentNode)en,out_temp);
1051 return flattenCastNode((CastNode)en,out_temp);
1053 case Kind.CreateObjectNode:
1054 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
1056 case Kind.FieldAccessNode:
1057 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
1059 case Kind.ArrayAccessNode:
1060 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
1062 case Kind.LiteralNode:
1063 return flattenLiteralNode((LiteralNode)en,out_temp);
1065 case Kind.MethodInvokeNode:
1066 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
1069 return flattenNameNode((NameNode)en,out_temp);
1072 return flattenOpNode((OpNode)en,out_temp);
1074 case Kind.OffsetNode:
1075 return flattenOffsetNode((OffsetNode)en,out_temp);
1077 case Kind.TertiaryNode:
1078 return flattenTertiaryNode((TertiaryNode)en,out_temp);
1080 case Kind.InstanceOfNode:
1081 return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
1083 case Kind.ArrayInitializerNode:
1084 return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
1089 private NodePair flattenDeclarationNode(DeclarationNode dn) {
1090 VarDescriptor vd=dn.getVarDescriptor();
1091 TempDescriptor td=getTempforVar(vd);
1092 if (dn.getExpression()!=null)
1093 return flattenExpressionNode(dn.getExpression(),td);
1095 FlatNop fn=new FlatNop();
1096 return new NodePair(fn,fn);
1100 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
1101 TagVarDescriptor tvd=dn.getTagVarDescriptor();
1102 TagDescriptor tag=tvd.getTag();
1103 TempDescriptor tmp=getTempforVar(tvd);
1104 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
1105 ftd.setNumLine(dn.getNumLine());
1106 return new NodePair(ftd,ftd);
1109 private TempDescriptor getTempforParam(Descriptor d) {
1110 if (temptovar.containsKey(d))
1111 return (TempDescriptor)temptovar.get(d);
1113 if (d instanceof VarDescriptor) {
1114 VarDescriptor vd=(VarDescriptor)d;
1115 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
1116 temptovar.put(vd,td);
1118 } else if (d instanceof TagVarDescriptor) {
1119 TagVarDescriptor tvd=(TagVarDescriptor)d;
1120 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1121 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
1122 temptovar.put(tvd,td);
1124 } else throw new Error("Unreconized Descriptor");
1128 private TempDescriptor getTempforVar(Descriptor d) {
1129 if (temptovar.containsKey(d))
1130 return (TempDescriptor)temptovar.get(d);
1132 if (d instanceof VarDescriptor) {
1133 VarDescriptor vd=(VarDescriptor)d;
1134 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
1135 temptovar.put(vd,td);
1137 } else if (d instanceof TagVarDescriptor) {
1138 TagVarDescriptor tvd=(TagVarDescriptor)d;
1139 //BUGFIX TAGTYPE - add next line, modify following
1140 //line to tag this new type descriptor, modify
1141 //TempDescriptor constructor & factory to set type
1142 //using this Type To test, use any program with tags
1143 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1144 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
1145 temptovar.put(tvd,td);
1147 } else throw new Error("Unrecognized Descriptor");
1151 private NodePair flattenIfStatementNode(IfStatementNode isn) {
1152 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1153 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
1154 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1155 fcb.setNumLine(isn.getNumLine());
1156 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
1158 FlatNop nopend=new FlatNop();
1160 if (isn.getFalseBlock()!=null)
1161 false_np=flattenBlockNode(isn.getFalseBlock());
1163 FlatNop nop=new FlatNop();
1164 false_np=new NodePair(nop,nop);
1167 cond.getEnd().addNext(fcb);
1168 fcb.addTrueNext(true_np.getBegin());
1169 fcb.addFalseNext(false_np.getBegin());
1170 if (true_np.getEnd()!=null)
1171 true_np.getEnd().addNext(nopend);
1172 if (false_np.getEnd()!=null)
1173 false_np.getEnd().addNext(nopend);
1174 if (nopend.numPrev()==0)
1175 return new NodePair(cond.getBegin(), null);
1177 return new NodePair(cond.getBegin(), nopend);
1180 private NodePair flattenSwitchStatementNode(SwitchStatementNode ssn) {
1181 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.INT));
1182 NodePair cond=flattenExpressionNode(ssn.getCondition(),cond_temp);
1183 NodePair sbody = flattenSwitchBodyNode(ssn.getSwitchBody(), cond_temp);
1185 cond.getEnd().addNext(sbody.getBegin());
1187 return new NodePair(cond.getBegin(), sbody.getEnd());
1190 private NodePair flattenSwitchBodyNode(BlockNode bn, TempDescriptor cond_temp) {
1191 FlatNode begin=null;
1193 NodePair prev_true_branch = null;
1194 NodePair prev_false_branch = null;
1195 for(int i=0; i<bn.size(); i++) {
1196 SwitchBlockNode sbn = (SwitchBlockNode)bn.get(i);
1197 HashSet oldbs=breakset;
1198 breakset=new HashSet();
1200 NodePair body=flattenBlockNode(sbn.getSwitchBlockStatement());
1201 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
1202 FlatNode cond_begin = null;
1203 NodePair prev_fnp = null;
1204 for(int j = 0; j < slnv.size(); j++) {
1205 SwitchLabelNode sln = slnv.elementAt(j);
1206 NodePair left = null;
1207 NodePair false_np = null;
1208 if(sln.isDefault()) {
1211 TempDescriptor cond_tmp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1212 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop", sln.getCondition().getType());
1213 Operation op=new Operation(Operation.EQUAL);
1214 left=flattenExpressionNode(sln.getCondition(), temp_left);
1215 FlatOpNode fon=new FlatOpNode(cond_tmp, temp_left, cond_temp, op);
1216 fon.setNumLine(sln.getNumLine());
1217 left.getEnd().addNext(fon);
1219 FlatCondBranch fcb=new FlatCondBranch(cond_tmp);
1220 fcb.setNumLine(bn.getNumLine());
1221 fcb.setTrueProb(State.TRUEPROB);
1223 FlatNop nop=new FlatNop();
1224 false_np=new NodePair(nop,nop);
1227 fcb.addTrueNext(body.getBegin());
1228 fcb.addFalseNext(false_np.getBegin());
1230 if((prev_fnp != null) && (prev_fnp.getEnd() != null)) {
1231 prev_fnp.getEnd().addNext(left.getBegin());
1233 prev_fnp = false_np;
1236 begin = left.getBegin();
1238 if(cond_begin == null) {
1239 cond_begin = left.getBegin();
1242 if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1243 prev_false_branch.getEnd().addNext(cond_begin);
1245 prev_false_branch = prev_fnp;
1246 if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1247 prev_true_branch.getEnd().addNext(body.getBegin());
1249 prev_true_branch = body;
1250 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1251 FlatNode fn=(FlatNode)breakit.next();
1259 if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1262 prev_true_branch.getEnd().addNext(end);
1264 if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1267 prev_false_branch.getEnd().addNext(end);
1270 end=begin=new FlatNop();
1272 return new NodePair(begin,end);
1275 private NodePair flattenLoopNode(LoopNode ln) {
1276 HashSet oldbs=breakset;
1277 HashSet oldcs=continueset;
1278 breakset=new HashSet();
1279 continueset=new HashSet();
1281 if (ln.getType()==LoopNode.FORLOOP) {
1282 NodePair initializer=flattenBlockNode(ln.getInitializer());
1283 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1284 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1285 NodePair update=flattenBlockNode(ln.getUpdate());
1286 NodePair body=flattenBlockNode(ln.getBody());
1287 FlatNode begin=initializer.getBegin();
1288 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1289 fcb.setNumLine(ln.getNumLine());
1290 fcb.setTrueProb(State.TRUEPROB);
1292 FlatNop nopend=new FlatNop();
1293 FlatBackEdge backedge=new FlatBackEdge();
1295 FlatNop nop2=new FlatNop();
1296 initializer.getEnd().addNext(nop2);
1297 nop2.addNext(condition.getBegin());
1298 if (body.getEnd()!=null)
1299 body.getEnd().addNext(update.getBegin());
1300 update.getEnd().addNext(backedge);
1301 backedge.addNext(condition.getBegin());
1302 condition.getEnd().addNext(fcb);
1303 fcb.addFalseNext(nopend);
1304 fcb.addTrueNext(body.getBegin());
1305 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1306 FlatNode fn=(FlatNode)contit.next();
1308 fn.addNext(update.getBegin());
1310 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1311 FlatNode fn=(FlatNode)breakit.next();
1317 return new NodePair(begin,nopend);
1318 } else if (ln.getType()==LoopNode.WHILELOOP) {
1319 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1320 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1321 NodePair body=flattenBlockNode(ln.getBody());
1322 FlatNode begin=condition.getBegin();
1323 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1324 fcb.setNumLine(ln.getNumLine());
1325 fcb.setTrueProb(State.TRUEPROB);
1327 FlatNop nopend=new FlatNop();
1328 FlatBackEdge backedge=new FlatBackEdge();
1330 if (body.getEnd()!=null)
1331 body.getEnd().addNext(backedge);
1332 backedge.addNext(condition.getBegin());
1334 condition.getEnd().addNext(fcb);
1335 fcb.addFalseNext(nopend);
1336 fcb.addTrueNext(body.getBegin());
1338 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1339 FlatNode fn=(FlatNode)contit.next();
1341 fn.addNext(backedge);
1343 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1344 FlatNode fn=(FlatNode)breakit.next();
1350 return new NodePair(begin,nopend);
1351 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
1352 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1353 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1354 NodePair body=flattenBlockNode(ln.getBody());
1355 FlatNode begin=body.getBegin();
1356 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1357 fcb.setNumLine(ln.getNumLine());
1358 fcb.setTrueProb(State.TRUEPROB);
1360 FlatNop nopend=new FlatNop();
1361 FlatBackEdge backedge=new FlatBackEdge();
1363 if (body.getEnd()!=null)
1364 body.getEnd().addNext(condition.getBegin());
1365 condition.getEnd().addNext(fcb);
1366 fcb.addFalseNext(nopend);
1367 fcb.addTrueNext(backedge);
1368 backedge.addNext(body.getBegin());
1370 for(Iterator contit=continueset.iterator();contit.hasNext();) {
1371 FlatNode fn=(FlatNode)contit.next();
1373 fn.addNext(condition.getBegin());
1375 for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1376 FlatNode fn=(FlatNode)breakit.next();
1382 return new NodePair(begin,nopend);
1383 } else throw new Error();
1386 private NodePair flattenReturnNode(ReturnNode rntree) {
1387 TempDescriptor retval=null;
1389 if (rntree.getReturnExpression()!=null) {
1390 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
1391 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1394 FlatReturnNode rnflat=new FlatReturnNode(retval);
1395 rnflat.setNumLine(rntree.getNumLine());
1398 if ((state.THREAD||state.MGC)&&!this.lockStack.isEmpty()) {
1399 FlatNode end = null;
1400 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1401 for(int j = this.lockStack.size(); j > 0; j--) {
1402 TempDescriptor thistd = this.lockStack.elementAt(j-1);
1403 FlatCall fcunlock = new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
1404 fcunlock.setNumLine(rntree.getNumLine());
1406 end.addNext(fcunlock);
1413 if (state.DSM&&currmd.getModifiers().isAtomic()) {
1414 FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1420 cond.getEnd().addNext(ln);
1421 return new NodePair(cond.getBegin(),null);
1423 return new NodePair(ln,null);
1426 private NodePair flattenTaskExitNode(TaskExitNode ten) {
1427 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1428 ffan.setTaskExitIndex(ten.getTaskExitIndex());
1429 updateFlagActionNode(ffan, ten.getFlagEffects());
1430 NodePair fcn=flattenConstraintCheck(ten.getChecks());
1431 ffan.addNext(fcn.getBegin());
1432 FlatReturnNode rnflat=new FlatReturnNode(null);
1433 rnflat.setNumLine(ten.getNumLine());
1435 fcn.getEnd().addNext(rnflat);
1436 return new NodePair(ffan, null);
1439 private NodePair flattenConstraintCheck(Vector ccs) {
1440 FlatNode begin=new FlatNop();
1442 return new NodePair(begin,begin);
1443 FlatNode last=begin;
1444 for(int i=0; i<ccs.size(); i++) {
1445 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1446 /* Flatten the arguments */
1447 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1448 String[] vars=new String[cc.numArgs()];
1449 for(int j=0; j<cc.numArgs(); j++) {
1450 ExpressionNode en=cc.getArg(j);
1451 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1453 vars[j]=cc.getVar(j);
1454 NodePair np=flattenExpressionNode(en, td);
1455 last.addNext(np.getBegin());
1459 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1463 return new NodePair(begin,last);
1466 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1467 return flattenBlockNode(sbn.getBlockNode());
1470 private NodePair flattenSynchronizedNode(SynchronizedNode sbn) {
1471 TempDescriptor montmp=null;
1472 FlatNode first = null;
1473 FlatNode end = null;
1474 if(sbn.getExpr() instanceof ClassTypeNode) {
1475 montmp=new TempDescriptor("classobj", ((ClassTypeNode)sbn.getExpr()).getType().getClassDesc());
1477 montmp = TempDescriptor.tempFactory("monitor",sbn.getExpr().getType());
1478 NodePair npexp=flattenExpressionNode(sbn.getExpr(), montmp);
1479 first = npexp.getBegin();
1480 end = npexp.getEnd();
1482 this.lockStack.push(montmp);
1483 NodePair npblock=flattenBlockNode(sbn.getBlockNode());
1485 MethodDescriptor menmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
1486 FlatCall fcen=new FlatCall(menmd, null, montmp, new TempDescriptor[0]);
1487 fcen.setNumLine(sbn.getNumLine());
1489 MethodDescriptor mexmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1490 FlatCall fcex=new FlatCall(mexmd, null, montmp, new TempDescriptor[0]);
1491 fcex.setNumLine(sbn.getNumLine());
1493 this.lockStack.pop();
1500 fcen.addNext(npblock.getBegin());
1502 if (npblock.getEnd()!=null&&npblock.getEnd().kind()!=FKind.FlatReturnNode) {
1503 npblock.getEnd().addNext(fcex);
1504 return new NodePair(first, fcex);
1506 return new NodePair(first, null);
1510 private NodePair flattenAtomicNode(AtomicNode sbn) {
1511 NodePair np=flattenBlockNode(sbn.getBlockNode());
1512 FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1513 faen.setNumLine(sbn.getNumLine());
1514 FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1515 faen.addNext(np.getBegin());
1516 np.getEnd().addNext(faexn);
1517 return new NodePair(faen, faexn);
1520 private NodePair flattenGenReachNode( GenReachNode grn ) {
1521 FlatGenReachNode fgrn = new FlatGenReachNode( grn.getGraphName() );
1522 return new NodePair( fgrn, fgrn );
1525 private NodePair flattenSESENode(SESENode sn) {
1526 if( sn.isStart() ) {
1527 FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1528 fsen.setNumLine(sn.getNumLine());
1529 sn.setFlatEnter(fsen);
1530 return new NodePair(fsen, fsen);
1533 FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1534 sn.setFlatExit(fsexn);
1535 FlatSESEEnterNode fsen=sn.getStart().getFlatEnter();
1536 fsexn.setFlatEnter(fsen);
1537 sn.getStart().getFlatEnter().setFlatExit( fsexn );
1539 return new NodePair(fsexn, fsexn);
1542 private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1543 FlatNop fn=new FlatNop();
1547 continueset.add(fn);
1548 return new NodePair(fn,null);
1551 private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1552 TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1553 NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1554 FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1555 fion.setNumLine(tn.getNumLine());
1556 cond.getEnd().addNext(fion);
1557 return new NodePair(cond.getBegin(),fion);
1560 private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1561 boolean isGlobal = false;
1562 String disjointId = null;
1563 // get the type the array to be initialized
1564 TypeDescriptor td = ain.getType();
1566 // create a new array of size equal to the array initializer
1567 FlatNode first=null;
1569 TempDescriptor tmp=TempDescriptor.tempFactory("arg", new TypeDescriptor(TypeDescriptor.INT));
1570 FlatLiteralNode fln_tmp=new FlatLiteralNode(tmp.getType(), new Integer(ain.numVarInitializers()), tmp);
1571 fln_tmp.setNumLine(ain.getNumLine());
1572 first = last=fln_tmp;
1574 // create the new array
1575 FlatNew fn=new FlatNew(td, out_temp, tmp, isGlobal, disjointId);
1579 // initialize the new array
1580 for(int i = 0; i < ain.numVarInitializers(); i++) {
1581 ExpressionNode var_init_node = ain.getVarInitializer(i);
1582 TempDescriptor tmp_toinit = out_temp;
1583 TempDescriptor tmp_init=TempDescriptor.tempFactory("array_init", td.dereference());
1585 TempDescriptor index=TempDescriptor.tempFactory("index", new TypeDescriptor(TypeDescriptor.INT));
1586 FlatLiteralNode fln=new FlatLiteralNode(index.getType(), new Integer(i), index);
1587 fln.setNumLine(ain.getNumLine());
1588 // calculate the initial value
1589 NodePair np_init = flattenExpressionNode(var_init_node, tmp_init);
1590 // TODO wrapper class process is missing now
1591 /*if(td.isArray() && td.dereference().iswrapper()) {
1593 FlatSetElementNode fsen=new FlatSetElementNode(tmp_toinit, index, tmp_init);
1594 fsen.setNumLine(ain.getNumLine());
1596 fln.addNext(np_init.getBegin());
1597 np_init.getEnd().addNext(fsen);
1601 return new NodePair(first, last);
1604 private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1605 TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1606 TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1607 TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1609 NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1610 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1611 fcb.setNumLine(tn.getNumLine());
1613 NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1614 FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1615 fonT.setNumLine(tn.getNumLine());
1617 NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1618 FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1619 fonF.setNumLine(tn.getNumLine());
1621 FlatNop nopend=new FlatNop();
1623 cond.getEnd().addNext(fcb);
1625 fcb.addTrueNext(trueExpr.getBegin());
1626 fcb.addFalseNext(falseExpr.getBegin());
1628 trueExpr.getEnd().addNext(fonT);
1629 fonT.addNext(nopend);
1631 falseExpr.getEnd().addNext(fonF);
1632 fonF.addNext(nopend);
1634 return new NodePair(cond.getBegin(), nopend);
1637 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1638 switch(bsn.kind()) {
1639 case Kind.BlockExpressionNode:
1640 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1642 case Kind.DeclarationNode:
1643 return flattenDeclarationNode((DeclarationNode)bsn);
1645 case Kind.TagDeclarationNode:
1646 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1648 case Kind.IfStatementNode:
1649 return flattenIfStatementNode((IfStatementNode)bsn);
1651 case Kind.SwitchStatementNode:
1652 return flattenSwitchStatementNode((SwitchStatementNode)bsn);
1655 return flattenLoopNode((LoopNode)bsn);
1657 case Kind.ReturnNode:
1658 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1660 case Kind.TaskExitNode:
1661 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1663 case Kind.SubBlockNode:
1664 return flattenSubBlockNode((SubBlockNode)bsn);
1666 case Kind.AtomicNode:
1667 return flattenAtomicNode((AtomicNode)bsn);
1669 case Kind.SynchronizedNode:
1670 return flattenSynchronizedNode((SynchronizedNode)bsn);
1673 return flattenSESENode((SESENode)bsn);
1675 case Kind.GenReachNode:
1676 return flattenGenReachNode((GenReachNode)bsn);
1678 case Kind.ContinueBreakNode:
1679 return flattenContinueBreakNode((ContinueBreakNode)bsn);