Bug fixes for MGC
[IRC.git] / Robust / src / IR / Flat / BuildFlat.java
1 package IR.Flat;
2 import IR.*;
3 import IR.Tree.*;
4
5 import java.util.*;
6
7 public class BuildFlat {
8   State state;
9   Hashtable temptovar;
10   MethodDescriptor currmd;
11   TypeUtil typeutil;
12
13   HashSet breakset;
14   HashSet continueset;
15   FlatExit fe;
16
17   public BuildFlat(State st, TypeUtil typeutil) {
18     state=st;
19     temptovar=new Hashtable();
20     this.typeutil=typeutil;
21     this.breakset=new HashSet();
22     this.continueset=new HashSet();
23   }
24
25   public Hashtable getMap() {
26     return temptovar;
27   }
28
29   public void buildFlat() {
30     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
31     while(it.hasNext()) {
32       ClassDescriptor cn=(ClassDescriptor)it.next();
33       flattenClass(cn);
34     }
35
36     Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
37     while(task_it.hasNext()) {
38       TaskDescriptor td=(TaskDescriptor)task_it.next();
39       flattenTask(td);
40     }
41   }
42
43   private void flattenTask(TaskDescriptor td) {
44     BlockNode bn=state.getMethodBody(td);
45     NodePair np=flattenBlockNode(bn);
46     FlatNode fn=np.getBegin();
47     fe=new FlatExit();
48     FlatNode fn2=np.getEnd();
49
50     if (fn2!=null&& fn2.kind()!=FKind.FlatReturnNode) {
51       FlatReturnNode rnflat=new FlatReturnNode(null);
52       rnflat.addNext(fe);
53       fn2.addNext(rnflat);
54     }
55
56     FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
57     ffan.addNext(fn);
58
59     FlatMethod fm=new FlatMethod(td, fe);
60     fm.addNext(ffan);
61
62     Hashtable visitedset=new Hashtable();
63
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
69       if (tel!=null)
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);
76           } else {
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);
80           }
81           tel.setTemp(j, tagtmp);
82         }
83     }
84
85     /* Flatten Vector of Flag Effects */
86     Vector flags=td.getFlagEffects();
87     updateFlagActionNode(ffan,flags);
88
89     state.addFlatCode(td,fm);
90   }
91
92
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
96       return;
97
98     for(int i=0; i<flags.size(); i++) {
99       FlagEffects fes=(FlagEffects)flags.get(i);
100       TempDescriptor flagtemp=getTempforVar(fes.getVar());
101       // Process the flags
102       for(int j=0; j<fes.numEffects(); j++) {
103         FlagEffect fe=fes.getEffect(j);
104         ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
105       }
106       // Process the tags
107       for(int j=0; j<fes.numTagEffects(); j++) {
108         TagEffect te=fes.getTagEffect(j);
109         TempDescriptor tagtemp=getTempforVar(te.getTag());
110
111         ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
112       }
113     }
114   }
115
116   FlatAtomicEnterNode curran=null;
117
118   private FlatNode spliceReturn(FlatNode fn) {
119     FlatReturnNode rnflat=null;
120     if (currmd.getReturnType()==null||currmd.getReturnType().isVoid()) {
121       rnflat=new FlatReturnNode(null);
122       fn.addNext(rnflat);
123     } else {
124       TempDescriptor tmp=TempDescriptor.tempFactory("rettmp",currmd.getReturnType());
125       Object o=null;
126       if (currmd.getReturnType().isPtr()) {
127         o=null;
128       } else if (currmd.getReturnType().isByte()) {
129         o=new Byte((byte)0);
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()) {
135         o=new Integer(0);
136       } else if (currmd.getReturnType().isLong()) {
137         o=new Long(0);
138       } else if (currmd.getReturnType().isBoolean()) {
139         o=new Boolean(false);
140       } else if (currmd.getReturnType().isFloat()) {
141         o=new Float(0.0);
142       } else if (currmd.getReturnType().isDouble()) {
143         o=new Double(0.0);
144     }
145
146
147       FlatLiteralNode fln=new FlatLiteralNode(currmd.getReturnType(),o,tmp);
148       rnflat=new FlatReturnNode(tmp);
149       fln.addNext(rnflat);
150       fn.addNext(fln);      
151     }
152     return rnflat;
153   }
154
155   private void flattenClass(ClassDescriptor cn) {
156     Iterator methodit=cn.getMethods();
157     while(methodit.hasNext()) {     
158       currmd=(MethodDescriptor)methodit.next();
159       
160       // if OOOJava is on, splice a special SESE in to
161       // enclose the main method
162       boolean spliceInImplicitMain = 
163         state.OOOJAVA &&
164         currmd.equals( typeutil.getMain() );
165
166       FlatSESEEnterNode spliceSESE = null;
167       FlatSESEExitNode  spliceExit = null;
168
169       if( spliceInImplicitMain ) {
170         SESENode mainTree = new SESENode( "main" );
171         spliceSESE = new FlatSESEEnterNode( mainTree );
172         spliceExit = new FlatSESEExitNode ( mainTree );
173         spliceSESE.setFlatExit ( spliceExit );
174         spliceExit.setFlatEnter( spliceSESE );
175         spliceSESE.setIsMainSESE();
176       } 
177
178
179       fe=new FlatExit();
180
181       BlockNode bn=state.getMethodBody(currmd);
182
183       if (state.DSM&&currmd.getModifiers().isAtomic()) {
184         curran=new FlatAtomicEnterNode();
185       } else
186         curran=null;
187       NodePair np=flattenBlockNode(bn);
188       FlatNode fn=np.getBegin();
189       if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
190         MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
191     TempDescriptor thistd=null;
192     if(currmd.getModifiers().isStatic()) {
193       // need to lock the Class object
194       thistd=new TempDescriptor("classobj", cn);
195     } else {
196       // lock this object
197       thistd=getTempforVar(currmd.getThis());
198     }
199     FlatCall fc = new FlatCall(memd, null, thistd, new TempDescriptor[0]);
200         fc.addNext(fn);
201         fn=fc;
202         if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
203           MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
204           FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
205           np.getEnd().addNext(fcunlock);
206           FlatNode rnflat=spliceReturn(fcunlock);
207           rnflat.addNext(fe);
208         }
209       } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
210         curran.addNext(fn);
211         fn=curran;
212         if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
213           FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
214           np.getEnd().addNext(aen);
215           FlatNode rnflat=spliceReturn(aen);
216           rnflat.addNext(fe);
217         }       
218
219       } else if (np.getEnd()!=null&&np.getEnd().kind()!=FKind.FlatReturnNode) {
220         FlatNode rnflat=null;
221         if( spliceInImplicitMain ) {
222           np.getEnd().addNext(spliceExit);
223           rnflat=spliceReturn(spliceExit);
224         } else {
225           rnflat=spliceReturn(np.getEnd());
226         }
227         rnflat.addNext(fe);
228       } else if (np.getEnd()!=null) {
229         if( spliceInImplicitMain ) {
230           FlatReturnNode rnflat=(FlatReturnNode)np.getEnd();
231           np.getEnd().addNext(spliceExit);
232           spliceExit.addNext(fe);
233         }
234       }
235
236       if( spliceInImplicitMain ) {
237         spliceSESE.addNext(fn);
238         fn=spliceSESE;   
239       }
240
241       FlatMethod fm=new FlatMethod(currmd, fe);
242       fm.addNext(fn);
243       if (!currmd.isStatic())
244         fm.addParameterTemp(getTempforParam(currmd.getThis()));
245       for(int i=0; i<currmd.numParameters(); i++) {
246         fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
247       }
248
249       state.addFlatCode(currmd,fm);
250     }
251   }
252
253   private NodePair flattenBlockNode(BlockNode bn) {
254     FlatNode begin=null;
255     FlatNode end=null;
256     for(int i=0; i<bn.size(); i++) {
257       NodePair np=flattenBlockStatementNode(bn.get(i));
258       FlatNode np_begin=np.getBegin();
259       FlatNode np_end=np.getEnd();
260       if (begin==null) {
261         begin=np_begin;
262       }
263       if (end==null) {
264         end=np_end;
265       } else {
266         end.addNext(np_begin);
267         if (np_end==null) {
268             return new NodePair(begin, null);
269         } else
270             end=np_end;
271       }
272     }
273     if (begin==null) {
274       end=begin=new FlatNop();
275     }
276     return new NodePair(begin,end);
277   }
278
279   private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
280     //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
281     TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
282     return flattenExpressionNode(en.getExpression(),tmp);
283   }
284
285   private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
286     TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
287     NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
288     FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
289     np.getEnd().addNext(fcn);
290     return new NodePair(np.getBegin(),fcn);
291   }
292
293   private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
294     FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
295     return new NodePair(fln,fln);
296   }
297
298   private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
299     FlatOffsetNode fln = new FlatOffsetNode(ofn.getClassType(), ofn.getField(), out_temp);
300     return new NodePair(fln, fln);
301   }
302
303   private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
304     TypeDescriptor td=con.getType();
305     if (!td.isArray()) {
306       FlatNode fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId());
307       FlatNode last=fn;
308       //handle wrapper fields
309       ClassDescriptor cd=td.getClassDesc();
310       for(Iterator fieldit=cd.getFields();fieldit.hasNext();) {
311         FieldDescriptor fd=(FieldDescriptor)fieldit.next();
312         if (fd.getType().iswrapper()) {
313           TempDescriptor wrap_tmp=TempDescriptor.tempFactory("wrapper_obj",fd.getType());
314           FlatNode fnwrapper=new FlatNew(fd.getType(), wrap_tmp, con.isGlobal());
315           FlatSetFieldNode fsfn=new FlatSetFieldNode(out_temp, fd, wrap_tmp);
316           last.addNext(fnwrapper);
317           fnwrapper.addNext(fsfn);
318           last=fsfn;
319         }
320       }
321
322       TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
323
324       // Build arguments
325       for(int i=0; i<con.numArgs(); i++) {
326         ExpressionNode en=con.getArg(i);
327         TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
328         temps[i]=tmp;
329         NodePair np=flattenExpressionNode(en, tmp);
330         last.addNext(np.getBegin());
331         last=np.getEnd();
332       }
333       MethodDescriptor md=con.getConstructor();
334       //Call to constructor
335       FlatCall fc=new FlatCall(md, null, out_temp, temps);
336       last.addNext(fc);
337       last=fc;
338       if (td.getClassDesc().hasFlags()) {
339         //          if (con.getFlagEffects()!=null) {
340         FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
341         FlagEffects fes=con.getFlagEffects();
342         TempDescriptor flagtemp=out_temp;
343         if (fes!=null) {
344           for(int j=0; j<fes.numEffects(); j++) {
345             FlagEffect fe=fes.getEffect(j);
346             ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
347           }
348           for(int j=0; j<fes.numTagEffects(); j++) {
349             TagEffect te=fes.getTagEffect(j);
350             TempDescriptor tagtemp=getTempforVar(te.getTag());
351
352             ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
353           }
354         } else {
355           ffan.addFlagAction(flagtemp, null, false);
356         }
357         last.addNext(ffan);
358         last=ffan;
359       }
360       return new NodePair(fn,last);
361     } else {
362       if(con.getArrayInitializer() == null) {
363       FlatNode first=null;
364       FlatNode last=null;
365       TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
366       for (int i=0; i<con.numArgs(); i++) {
367         ExpressionNode en=con.getArg(i);
368         TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
369         temps[i]=tmp;
370         NodePair np=flattenExpressionNode(en, tmp);
371         if (first==null)
372           first=np.getBegin();
373         else
374           last.addNext(np.getBegin());
375         last=np.getEnd();
376
377         TempDescriptor tmp2=(i==0) ?
378                              out_temp :
379                              TempDescriptor.tempFactory("arg",en.getType());
380       }
381       FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
382       last.addNext(fn);
383       if (temps.length>1) {
384         NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
385         fn.addNext(np.getBegin());
386         return new NodePair(first,np.getEnd());
387       } else if (td.isArray()&&td.dereference().iswrapper()) {
388         NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
389         fn.addNext(np.getBegin());
390         return new NodePair(first,np.getEnd());
391       } else
392         return new NodePair(first, fn);
393       } else if(state.MGC) {
394       // array creation with initializers
395         return flattenArrayInitializerNode(con.getArrayInitializer(), out_temp);
396       }
397       return null;
398     }
399   }
400
401   private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
402     TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
403     TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
404     FlatNop fnop=new FlatNop();    //last node
405
406     //index=0
407     FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
408     //tmpone=1
409     FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
410
411     TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
412
413     FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
414     FlatCondBranch fcb=new FlatCondBranch(tmpbool);
415     fcb.setTrueProb(State.TRUEPROB);
416     fcb.setLoop();
417     //is index<temp[i]
418     TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
419     FlatNew fn=td.iswrapper()?new FlatNew(td, new_tmp, isglobal):new FlatNew(td, new_tmp, temparray[i+1], isglobal);
420     FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
421     // index=index+1
422     FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
423     //jump out
424     fln.addNext(fln2);
425     fln2.addNext(fcomp);
426     fcomp.addNext(fcb);
427     fcb.addTrueNext(fn);
428     fcb.addFalseNext(fnop);
429     fn.addNext(fsen);
430     //Recursive call here
431     if ((i+2)<temparray.length) {
432       NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
433       fsen.addNext(np2.getBegin());
434       np2.getEnd().addNext(fon);
435     } else if (td.isArray()&&td.dereference().iswrapper()) {
436       NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
437       fsen.addNext(np2.getBegin());
438       np2.getEnd().addNext(fon);      
439     } else {
440       fsen.addNext(fon);
441     }
442     fon.addNext(fcomp);
443     return new NodePair(fln, fnop);
444   }
445
446   private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
447     TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
448     FlatNode first=null;
449     FlatNode last=null;
450     TempDescriptor thisarg=null;
451
452     if (min.getExpression()!=null) {
453       TypeDescriptor mtd = min.getExpression().getType();
454       if(state.MGC && mtd.isClass() && mtd.getClassDesc().isEnum()) {
455         mtd = new TypeDescriptor(TypeDescriptor.INT);
456       }
457       thisarg=TempDescriptor.tempFactory("thisarg", mtd);
458       NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
459       first=np.getBegin();
460       last=np.getEnd();
461     }
462
463     //Build arguments
464     for(int i=0; i<min.numArgs(); i++) {
465       ExpressionNode en=min.getArg(i);
466       TypeDescriptor etd = en.getType();
467       if(state.MGC && etd.isClass() && etd.getClassDesc().isEnum()) {
468         etd = new TypeDescriptor(TypeDescriptor.INT);
469       }
470       TempDescriptor td=TempDescriptor.tempFactory("arg", etd);
471       temps[i]=td;
472       NodePair np=flattenExpressionNode(en, td);
473       if (first==null)
474         first=np.getBegin();
475       else
476         last.addNext(np.getBegin());
477       last=np.getEnd();
478     }
479
480     MethodDescriptor md=min.getMethod();
481
482     //Call to constructor
483
484     FlatCall fc;
485     if(md.getReturnType()==null||md.getReturnType().isVoid())
486       fc=new FlatCall(md, null, thisarg, temps);
487     else
488       fc=new FlatCall(md, out_temp, thisarg, temps);
489     if (first==null) {
490       first=fc;
491     } else
492       last.addNext(fc);
493     return new NodePair(first,fc);
494   }
495
496   private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
497     TempDescriptor tmp=null;
498     if(fan.getExpression().getType().isClassNameRef()) {
499       // static field dereference with class name
500       tmp = new TempDescriptor(fan.getExpression().getType().getClassDesc().getSymbol(), fan.getExpression().getType());
501       FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
502       return new NodePair(fn,fn);
503     } else {
504       tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
505       NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
506       FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
507       npe.getEnd().addNext(fn);
508       return new NodePair(npe.getBegin(),fn);
509     }
510   }
511
512   private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
513     TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
514     TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
515     NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
516     NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
517     TempDescriptor arraytmp=out_temp;
518     if (aan.iswrapper()) {
519       //have wrapper
520       arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference());
521     }
522     FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp);
523     npe.getEnd().addNext(npi.getBegin());
524     npi.getEnd().addNext(fn);
525     if (aan.iswrapper()) {
526       FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp);
527       fn.addNext(ffn);
528       fn=ffn;
529     }
530     return new NodePair(npe.getBegin(),fn);
531   }
532
533   private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
534     // Three cases:
535     // left side is variable
536     // left side is field
537     // left side is array
538
539     Operation base=an.getOperation().getBaseOp();
540     boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
541
542     if (!pre) {
543       //rewrite the base operation
544       base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
545     }
546     FlatNode first=null;
547     FlatNode last=null;
548     TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
549
550     //Get src value
551     if (an.getSrc()!=null) {
552       if(an.getSrc().getEval() != null) {
553         first = last = new FlatLiteralNode(an.getSrc().getType(), an.getSrc().getEval().longValue(), src_tmp);
554       } else {
555         NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
556         first=np_src.getBegin();
557         last=np_src.getEnd();
558       }
559     } else if (!pre) {
560       FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
561       first=fln;
562       last=fln;
563     }
564
565     if (an.getDest().kind()==Kind.FieldAccessNode) {
566       //We are assigning an object field
567
568       FieldAccessNode fan=(FieldAccessNode)an.getDest();
569       ExpressionNode en=fan.getExpression();
570       TempDescriptor dst_tmp=null;
571       NodePair np_baseexp=null;
572       if(en.getType().isClassNameRef()) {
573         // static field dereference with class name
574         dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
575         FlatNop nop=new FlatNop();
576         np_baseexp = new NodePair(nop,nop);
577       } else {
578         dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
579         np_baseexp=flattenExpressionNode(en, dst_tmp);
580       }
581       if (first==null)
582         first=np_baseexp.getBegin();
583       else
584         last.addNext(np_baseexp.getBegin());
585       last=np_baseexp.getEnd();
586
587       //See if we need to perform an operation
588       if (base!=null) {
589         //If it is a preinc we need to store the initial value
590         TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
591         TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
592         FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
593         last.addNext(ffn);
594         last=ffn;
595
596         if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
597           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
598           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
599           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
600           src_tmp=tmp;
601           last.addNext(fc);
602           last=fc;
603         } else {
604           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
605           src_tmp=tmp;
606           last.addNext(fon);
607           last=fon;
608         }
609       }
610
611       FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
612       last.addNext(fsfn);
613       last=fsfn;
614       if (pre) {
615         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
616         fsfn.addNext(fon2);
617         last=fon2;
618       }
619       return new NodePair(first, last);
620     } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
621       //We are assigning an array element
622
623       ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
624       ExpressionNode en=aan.getExpression();
625       ExpressionNode enindex=aan.getIndex();
626       TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
627       TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
628       NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
629       NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
630       if (first==null)
631         first=np_baseexp.getBegin();
632       else
633         last.addNext(np_baseexp.getBegin());
634       np_baseexp.getEnd().addNext(np_indexexp.getBegin());
635       last=np_indexexp.getEnd();
636
637       //See if we need to perform an operation
638       if (base!=null) {
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
643         if (aan.iswrapper()) {
644           TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
645           TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
646           FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
647           FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp3,src_tmp2);
648           last.addNext(fen);
649           fen.addNext(ffn);
650           last=ffn;
651         } else {
652           FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
653           last.addNext(fen);
654           last=fen;
655         }
656         if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
657           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
658           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
659           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
660           src_tmp=tmp;
661           last.addNext(fc);
662           last=fc;
663         } else {
664           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
665           src_tmp=tmp;
666           last.addNext(fon);
667           last=fon;
668         }
669       }
670
671       if (aan.iswrapper()) { 
672         TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
673         TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
674         FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
675         FlatSetFieldNode fsfn=new FlatSetFieldNode(src_tmp3,(FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp);
676         last.addNext(fen);
677         fen.addNext(fsfn);
678         last=fsfn;
679       } else { 
680         FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
681         last.addNext(fsen);
682         last=fsen;
683       }
684       if (pre) {
685         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
686         last.addNext(fon2);
687         last=fon2;
688       }
689       return new NodePair(first, last);
690     } else if (an.getDest().kind()==Kind.NameNode) {
691       //We could be assigning a field or variable
692       NameNode nn=(NameNode)an.getDest();
693
694
695       if (nn.getExpression()!=null) {
696         //It is a field
697         FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
698         ExpressionNode en=fan.getExpression();
699     TempDescriptor dst_tmp=null;
700     NodePair np_baseexp=null;
701     if(en.getType().isClassNameRef()) {
702       // static field dereference with class name
703       dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
704       FlatNop nop=new FlatNop();
705       np_baseexp = new NodePair(nop,nop);
706     } else {
707       dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
708       np_baseexp=flattenExpressionNode(en, dst_tmp);
709     }
710         if (first==null)
711           first=np_baseexp.getBegin();
712         else
713           last.addNext(np_baseexp.getBegin());
714         last=np_baseexp.getEnd();
715
716         //See if we need to perform an operation
717         if (base!=null) {
718           //If it is a preinc we need to store the initial value
719           TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
720           TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
721
722           FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
723           last.addNext(ffn);
724           last=ffn;
725
726
727           if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
728             ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
729             MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
730             FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
731             src_tmp=tmp;
732             last.addNext(fc);
733             last=fc;
734           } else {
735             FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
736             src_tmp=tmp;
737             last.addNext(fon);
738             last=fon;
739           }
740         }
741
742
743         FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
744         last.addNext(fsfn);
745         last=fsfn;
746         if (pre) {
747           FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
748           fsfn.addNext(fon2);
749           last=fon2;
750         }
751         return new NodePair(first, last);
752       } else {
753         if (nn.getField()!=null) {
754           //It is a field
755           //Get src value
756
757           //See if we need to perform an operation
758           if (base!=null) {
759             //If it is a preinc we need to store the initial value
760             TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
761             TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
762         
763         TempDescriptor ftmp= null;
764         if(state.MGC && (nn.getClassDesc() != null)) {
765           // this is a static field
766           ftmp = new TempDescriptor(nn.getClassDesc().getSymbol(), nn.getClassType());
767
768         } else {
769           ftmp=getTempforVar(nn.getVar());
770         }
771         FlatFieldNode ffn=new FlatFieldNode(nn.getField(), ftmp, src_tmp2);
772
773             if (first==null)
774               first=ffn;
775             else {
776               last.addNext(ffn);
777             }
778             last=ffn;
779
780
781             if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
782               ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
783               MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
784               FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
785               src_tmp=tmp;
786               last.addNext(fc);
787               last=fc;
788             } else {
789               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
790               src_tmp=tmp;
791               last.addNext(fon);
792               last=fon;
793             }
794           }
795
796       FlatSetFieldNode fsfn=null;
797       if(nn.getClassDesc()!=null) {
798         // this is a static field access inside of a static block
799         fsfn=new FlatSetFieldNode(new TempDescriptor("sfsb", nn.getClassType()), nn.getField(), src_tmp);
800       } else {
801         fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
802       }
803           if (first==null) {
804             first=fsfn;
805           } else {
806             last.addNext(fsfn);
807           }
808           last=fsfn;
809           if (pre) {
810             FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
811             fsfn.addNext(fon2);
812             last=fon2;
813           }
814           return new NodePair(first, last);
815         } else {
816           //It is a variable
817           //See if we need to perform an operation
818
819           if (base!=null) {
820             //If it is a preinc we need to store the initial value
821             TempDescriptor src_tmp2=getTempforVar(nn.getVar());
822             TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
823             if (!pre) {
824               FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
825               if (first==null)
826                 first=fon;
827               else
828                 last.addNext(fon);
829               last=fon;
830             }
831
832
833             if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
834               ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
835               MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
836               FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
837               if (first==null)
838                 first=fc;
839               else
840                 last.addNext(fc);
841               src_tmp=tmp;
842               last=fc;
843             } else {
844               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
845               if (first==null)
846                 first=fon;
847               else
848                 last.addNext(fon);
849               src_tmp=tmp;
850               last=fon;
851             }
852           }
853
854           FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
855
856           last.addNext(fon);
857           last=fon;
858           if (pre) {
859             FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
860             fon.addNext(fon2);
861             last=fon2;
862           }
863           return new NodePair(first, last);
864         } //end of else
865       }
866     }
867
868     throw new Error();
869   }
870
871   private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
872     if (nn.getExpression()!=null) {
873       /* Hack - use subtree instead */
874       return flattenExpressionNode(nn.getExpression(),out_temp);
875     } else if (nn.getField()!=null) {
876       TempDescriptor tmp= null;
877       if(state.MGC && (nn.getClassDesc() != null)) {
878         // this is a static field
879         tmp = new TempDescriptor(nn.getClassDesc().getSymbol(), nn.getClassType());
880         
881       } else {
882       tmp=getTempforVar(nn.getVar());
883       }
884       FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
885       return new NodePair(ffn,ffn);
886     } else {
887       TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
888       if (nn.isTag()) {
889         //propagate tag
890         out_temp.setTag(tmp.getTag());
891       }
892       FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
893       return new NodePair(fon,fon);
894     }
895   }
896
897   private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
898     TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
899     TempDescriptor temp_right=null;
900
901     Operation op=on.getOp();
902
903     NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
904     NodePair right;
905     if (on.getRight()!=null) {
906       temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
907       right=flattenExpressionNode(on.getRight(),temp_right);
908     } else {
909       FlatNop nop=new FlatNop();
910       right=new NodePair(nop,nop);
911     }
912
913     if (op.getOp()==Operation.LOGIC_OR) {
914       /* Need to do shortcircuiting */
915       FlatCondBranch fcb=new FlatCondBranch(temp_left);
916       FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
917       FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
918       FlatNop fnop=new FlatNop();
919       left.getEnd().addNext(fcb);
920       fcb.addFalseNext(right.getBegin());
921       right.getEnd().addNext(fon2);
922       fon2.addNext(fnop);
923       fcb.addTrueNext(fon1);
924       fon1.addNext(fnop);
925       return new NodePair(left.getBegin(), fnop);
926     } else if (op.getOp()==Operation.LOGIC_AND) {
927       /* Need to do shortcircuiting */
928       FlatCondBranch fcb=new FlatCondBranch(temp_left);
929       FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
930       FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
931       FlatNop fnop=new FlatNop();
932       left.getEnd().addNext(fcb);
933       fcb.addTrueNext(right.getBegin());
934       right.getEnd().addNext(fon2);
935       fon2.addNext(fnop);
936       fcb.addFalseNext(fon1);
937       fon1.addNext(fnop);
938       return new NodePair(left.getBegin(), fnop);
939     } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
940       //We have a string concatenate
941       ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
942       MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
943       FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
944       left.getEnd().addNext(right.getBegin());
945       right.getEnd().addNext(fc);
946       return new NodePair(left.getBegin(), fc);
947     }
948
949     FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
950     left.getEnd().addNext(right.getBegin());
951     right.getEnd().addNext(fon);
952     return new NodePair(left.getBegin(),fon);
953   }
954
955   private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
956     switch(en.kind()) {
957     case Kind.AssignmentNode:
958       return flattenAssignmentNode((AssignmentNode)en,out_temp);
959
960     case Kind.CastNode:
961       return flattenCastNode((CastNode)en,out_temp);
962
963     case Kind.CreateObjectNode:
964       return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
965
966     case Kind.FieldAccessNode:
967       return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
968
969     case Kind.ArrayAccessNode:
970       return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
971
972     case Kind.LiteralNode:
973       return flattenLiteralNode((LiteralNode)en,out_temp);
974
975     case Kind.MethodInvokeNode:
976       return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
977
978     case Kind.NameNode:
979       return flattenNameNode((NameNode)en,out_temp);
980
981     case Kind.OpNode:
982       return flattenOpNode((OpNode)en,out_temp);      
983
984     case Kind.OffsetNode:
985       return flattenOffsetNode((OffsetNode)en,out_temp);
986
987     case Kind.TertiaryNode:
988       return flattenTertiaryNode((TertiaryNode)en,out_temp);
989
990     case Kind.InstanceOfNode:
991       return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
992
993     case Kind.ArrayInitializerNode:
994       return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
995     }
996     throw new Error();
997   }
998
999   private NodePair flattenDeclarationNode(DeclarationNode dn) {
1000     VarDescriptor vd=dn.getVarDescriptor();
1001     TempDescriptor td=getTempforVar(vd);
1002     if (dn.getExpression()!=null)
1003       return flattenExpressionNode(dn.getExpression(),td);
1004     else {
1005       FlatNop fn=new FlatNop();
1006       return new NodePair(fn,fn);
1007     }
1008   }
1009
1010   private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
1011     TagVarDescriptor tvd=dn.getTagVarDescriptor();
1012     TagDescriptor tag=tvd.getTag();
1013     TempDescriptor tmp=getTempforVar(tvd);
1014     FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
1015     return new NodePair(ftd,ftd);
1016   }
1017
1018   private TempDescriptor getTempforParam(Descriptor d) {
1019     if (temptovar.containsKey(d))
1020       return (TempDescriptor)temptovar.get(d);
1021     else {
1022       if (d instanceof VarDescriptor) {
1023         VarDescriptor vd=(VarDescriptor)d;
1024         TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
1025         temptovar.put(vd,td);
1026         return td;
1027       } else if (d instanceof TagVarDescriptor) {
1028         TagVarDescriptor tvd=(TagVarDescriptor)d;
1029         TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1030         TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
1031         temptovar.put(tvd,td);
1032         return td;
1033       } else throw new Error("Unreconized Descriptor");
1034     }
1035   }
1036
1037   private TempDescriptor getTempforVar(Descriptor d) {
1038     if (temptovar.containsKey(d))
1039       return (TempDescriptor)temptovar.get(d);
1040     else {
1041       if (d instanceof VarDescriptor) {
1042         VarDescriptor vd=(VarDescriptor)d;
1043         TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
1044         temptovar.put(vd,td);
1045         return td;
1046       } else if (d instanceof TagVarDescriptor) {
1047         TagVarDescriptor tvd=(TagVarDescriptor)d;
1048         //BUGFIX TAGTYPE - add next line, modify following
1049         //line to tag this new type descriptor, modify
1050         //TempDescriptor constructor & factory to set type
1051         //using this Type To test, use any program with tags
1052         TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1053         TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
1054         temptovar.put(tvd,td);
1055         return td;
1056       } else throw new Error("Unrecognized Descriptor");
1057     }
1058   }
1059
1060   private NodePair flattenIfStatementNode(IfStatementNode isn) {
1061     TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1062     NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
1063     FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1064     NodePair true_np=flattenBlockNode(isn.getTrueBlock());
1065     NodePair false_np;
1066     FlatNop nopend=new FlatNop();
1067
1068     if (isn.getFalseBlock()!=null)
1069       false_np=flattenBlockNode(isn.getFalseBlock());
1070     else {
1071       FlatNop nop=new FlatNop();
1072       false_np=new NodePair(nop,nop);
1073     }
1074
1075     cond.getEnd().addNext(fcb);
1076     fcb.addTrueNext(true_np.getBegin());
1077     fcb.addFalseNext(false_np.getBegin());
1078     if (true_np.getEnd()!=null)
1079         true_np.getEnd().addNext(nopend);
1080     if (false_np.getEnd()!=null)
1081         false_np.getEnd().addNext(nopend);
1082     if (nopend.numPrev()==0)
1083       return new NodePair(cond.getBegin(), null);
1084
1085     return new NodePair(cond.getBegin(), nopend);
1086   }
1087   
1088   private NodePair flattenSwitchStatementNode(SwitchStatementNode ssn) {
1089     TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.INT));
1090     NodePair cond=flattenExpressionNode(ssn.getCondition(),cond_temp);
1091     FlatNop nopend=new FlatNop();
1092     NodePair sbody = flattenSwitchBodyNode(ssn.getSwitchBody(), cond_temp, nopend);
1093     
1094     cond.getEnd().addNext(sbody.getBegin());
1095
1096     return new NodePair(cond.getBegin(), sbody.getEnd());
1097   }
1098   
1099   private NodePair flattenSwitchBodyNode(BlockNode bn, TempDescriptor cond_temp, FlatNode endnode) {
1100     FlatNode begin=null;
1101     FlatNode end=endnode;
1102     NodePair prev_true_branch = null;
1103     NodePair prev_false_branch = null;
1104     for(int i=0; i<bn.size(); i++) {
1105       SwitchBlockNode sbn = (SwitchBlockNode)bn.get(i);
1106       HashSet oldbs=breakset;
1107       breakset=new HashSet();
1108       
1109       NodePair body=flattenBlockNode(sbn.getSwitchBlockStatement());
1110       Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
1111       FlatNode cond_begin = null;
1112       NodePair prev_fnp = null;
1113       for(int j = 0; j < slnv.size(); j++) {
1114         SwitchLabelNode sln = slnv.elementAt(j);
1115         NodePair left = null;
1116         NodePair false_np = null;
1117         if(sln.isDefault()) {
1118           left = body;
1119         } else {
1120           TempDescriptor cond_tmp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1121           TempDescriptor temp_left=TempDescriptor.tempFactory("leftop", sln.getCondition().getType());
1122           Operation op=new Operation(Operation.EQUAL);
1123           left=flattenExpressionNode(sln.getCondition(), temp_left);
1124           FlatOpNode fon=new FlatOpNode(cond_tmp, temp_left, cond_temp, op);
1125           left.getEnd().addNext(fon);
1126
1127           FlatCondBranch fcb=new FlatCondBranch(cond_tmp);
1128           fcb.setTrueProb(State.TRUEPROB);
1129
1130           FlatNop nop=new FlatNop();
1131           false_np=new NodePair(nop,nop);
1132
1133           fon.addNext(fcb);
1134           fcb.addTrueNext(body.getBegin());
1135           fcb.addFalseNext(false_np.getBegin());
1136         }
1137         if((prev_fnp != null) && (prev_fnp.getEnd() != null)) {
1138           prev_fnp.getEnd().addNext(left.getBegin());
1139         }
1140         prev_fnp = false_np;
1141         
1142         if (begin==null) {
1143           begin = left.getBegin();
1144         }
1145         if(cond_begin == null) {
1146           cond_begin = left.getBegin();
1147         }
1148       }
1149       if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1150         prev_false_branch.getEnd().addNext(cond_begin);
1151       }
1152       prev_false_branch = prev_fnp;
1153       if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1154         prev_true_branch.getEnd().addNext(body.getBegin());
1155       }
1156       prev_true_branch = body;
1157       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1158         FlatNode fn=(FlatNode)breakit.next();
1159         breakit.remove();
1160         fn.addNext(endnode);
1161       }
1162       breakset=oldbs;
1163     }
1164     if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1165       prev_true_branch.getEnd().addNext(endnode);
1166     }
1167     if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1168       prev_false_branch.getEnd().addNext(endnode);
1169     }
1170     if(begin == null) {
1171       end=begin=new FlatNop();
1172     }
1173     return new NodePair(begin,end);
1174   }
1175   
1176   private NodePair flattenLoopNode(LoopNode ln) {
1177     HashSet oldbs=breakset;
1178     HashSet oldcs=continueset;
1179     breakset=new HashSet();
1180     continueset=new HashSet();
1181     
1182     if (ln.getType()==LoopNode.FORLOOP) {
1183       NodePair initializer=flattenBlockNode(ln.getInitializer());
1184       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1185       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1186       NodePair update=flattenBlockNode(ln.getUpdate());
1187       NodePair body=flattenBlockNode(ln.getBody());
1188       FlatNode begin=initializer.getBegin();
1189       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1190       fcb.setTrueProb(State.TRUEPROB);
1191       fcb.setLoop();
1192       FlatNop nopend=new FlatNop();
1193       FlatBackEdge backedge=new FlatBackEdge();
1194
1195       FlatNop nop2=new FlatNop();
1196       initializer.getEnd().addNext(nop2);
1197       nop2.addNext(condition.getBegin());
1198       if (body.getEnd()!=null)
1199           body.getEnd().addNext(update.getBegin());
1200       update.getEnd().addNext(backedge);
1201       backedge.addNext(condition.getBegin());
1202       condition.getEnd().addNext(fcb);
1203       fcb.addFalseNext(nopend);
1204       fcb.addTrueNext(body.getBegin());
1205       for(Iterator contit=continueset.iterator();contit.hasNext();) {
1206           FlatNode fn=(FlatNode)contit.next();
1207           contit.remove();
1208           fn.addNext(update.getBegin());
1209       }
1210       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1211           FlatNode fn=(FlatNode)breakit.next();
1212           breakit.remove();
1213           fn.addNext(nopend);
1214       }
1215       breakset=oldbs;
1216       continueset=oldcs;
1217       return new NodePair(begin,nopend);
1218     } else if (ln.getType()==LoopNode.WHILELOOP) {
1219       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1220       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1221       NodePair body=flattenBlockNode(ln.getBody());
1222       FlatNode begin=condition.getBegin();
1223       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1224       fcb.setTrueProb(State.TRUEPROB);
1225       fcb.setLoop();
1226       FlatNop nopend=new FlatNop();
1227       FlatBackEdge backedge=new FlatBackEdge();
1228
1229       if (body.getEnd()!=null)
1230         body.getEnd().addNext(backedge);
1231       backedge.addNext(condition.getBegin());
1232
1233       condition.getEnd().addNext(fcb);
1234       fcb.addFalseNext(nopend);
1235       fcb.addTrueNext(body.getBegin());
1236
1237       for(Iterator contit=continueset.iterator();contit.hasNext();) {
1238           FlatNode fn=(FlatNode)contit.next();
1239           contit.remove();
1240           fn.addNext(backedge);
1241       }
1242       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1243           FlatNode fn=(FlatNode)breakit.next();
1244           breakit.remove();
1245           fn.addNext(nopend);
1246       }
1247       breakset=oldbs;
1248       continueset=oldcs;
1249       return new NodePair(begin,nopend);
1250     } else if (ln.getType()==LoopNode.DOWHILELOOP) {
1251       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1252       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1253       NodePair body=flattenBlockNode(ln.getBody());
1254       FlatNode begin=body.getBegin();
1255       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1256       fcb.setTrueProb(State.TRUEPROB);
1257       fcb.setLoop();
1258       FlatNop nopend=new FlatNop();
1259       FlatBackEdge backedge=new FlatBackEdge();
1260
1261       if (body.getEnd()!=null)
1262         body.getEnd().addNext(condition.getBegin());
1263       condition.getEnd().addNext(fcb);
1264       fcb.addFalseNext(nopend);
1265       fcb.addTrueNext(backedge);
1266       backedge.addNext(body.getBegin());
1267
1268       for(Iterator contit=continueset.iterator();contit.hasNext();) {
1269           FlatNode fn=(FlatNode)contit.next();
1270           contit.remove();
1271           fn.addNext(condition.getBegin());
1272       }
1273       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1274           FlatNode fn=(FlatNode)breakit.next();
1275           breakit.remove();
1276           fn.addNext(nopend);
1277       }
1278       breakset=oldbs;
1279       continueset=oldcs;
1280       return new NodePair(begin,nopend);
1281     } else throw new Error();
1282   }
1283
1284   private NodePair flattenReturnNode(ReturnNode rntree) {
1285     TempDescriptor retval=null;
1286     NodePair cond=null;
1287     if (rntree.getReturnExpression()!=null) {
1288       retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
1289       cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1290     }
1291
1292     FlatReturnNode rnflat=new FlatReturnNode(retval);
1293     rnflat.addNext(fe);
1294     FlatNode ln=rnflat;
1295     if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
1296       MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1297       TempDescriptor thistd=null;
1298       if(currmd.getModifiers().isStatic()) {
1299         // need to lock the Class object
1300         thistd=new TempDescriptor("classobj", currmd.getClassDesc());
1301       } else {
1302         // lock this object
1303         thistd=getTempforVar(currmd.getThis());
1304       }
1305       FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
1306       fc.addNext(ln);
1307       ln=fc;
1308     }
1309     if (state.DSM&&currmd.getModifiers().isAtomic()) {
1310       FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1311       faen.addNext(ln);
1312       ln=faen;
1313     }
1314
1315     if (cond!=null) {
1316       cond.getEnd().addNext(ln);
1317       return new NodePair(cond.getBegin(),null);
1318     } else
1319       return new NodePair(ln,null);
1320   }
1321
1322   private NodePair flattenTaskExitNode(TaskExitNode ten) {
1323     FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1324     ffan.setTaskExitIndex(ten.getTaskExitIndex());
1325     updateFlagActionNode(ffan, ten.getFlagEffects());
1326     NodePair fcn=flattenConstraintCheck(ten.getChecks());
1327     ffan.addNext(fcn.getBegin());
1328     FlatReturnNode rnflat=new FlatReturnNode(null);
1329     rnflat.addNext(fe);
1330     fcn.getEnd().addNext(rnflat);
1331     return new NodePair(ffan, null);
1332   }
1333
1334   private NodePair flattenConstraintCheck(Vector ccs) {
1335     FlatNode begin=new FlatNop();
1336     if (ccs==null)
1337       return new NodePair(begin,begin);
1338     FlatNode last=begin;
1339     for(int i=0; i<ccs.size(); i++) {
1340       ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1341       /* Flatten the arguments */
1342       TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1343       String[] vars=new String[cc.numArgs()];
1344       for(int j=0; j<cc.numArgs(); j++) {
1345         ExpressionNode en=cc.getArg(j);
1346         TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1347         temps[j]=td;
1348         vars[j]=cc.getVar(j);
1349         NodePair np=flattenExpressionNode(en, td);
1350         last.addNext(np.getBegin());
1351         last=np.getEnd();
1352       }
1353
1354       FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1355       last.addNext(fcn);
1356       last=fcn;
1357     }
1358     return new NodePair(begin,last);
1359   }
1360
1361   private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1362     return flattenBlockNode(sbn.getBlockNode());
1363   }
1364
1365   private NodePair flattenSynchronizedNode(SynchronizedNode sbn) {
1366     TempDescriptor montmp=null;
1367     FlatNode first = null;
1368     FlatNode end = null;
1369     if(sbn.getExpr() instanceof ClassTypeNode) {
1370       montmp=new TempDescriptor("classobj", ((ClassTypeNode)sbn.getExpr()).getType().getClassDesc());
1371     } else {
1372       montmp = TempDescriptor.tempFactory("monitor",sbn.getExpr().getType());
1373       NodePair npexp=flattenExpressionNode(sbn.getExpr(), montmp);
1374       first = npexp.getBegin();
1375       end = npexp.getEnd();
1376     }
1377     NodePair npblock=flattenBlockNode(sbn.getBlockNode());
1378
1379     MethodDescriptor menmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
1380     FlatCall fcen=new FlatCall(menmd, null, montmp, new TempDescriptor[0]);
1381
1382     MethodDescriptor mexmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1383     FlatCall fcex=new FlatCall(mexmd, null, montmp, new TempDescriptor[0]);
1384
1385     if(first != null) {
1386       end.addNext(fcen);
1387     } else {
1388       first = fcen;
1389     }
1390     fcen.addNext(npblock.getBegin());
1391     if(npblock.getEnd() != null) {
1392       npblock.getEnd().addNext(fcex);
1393     }
1394     return new NodePair(first, fcex);
1395   }
1396
1397   private NodePair flattenAtomicNode(AtomicNode sbn) {
1398     NodePair np=flattenBlockNode(sbn.getBlockNode());
1399     FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1400     FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1401     faen.addNext(np.getBegin());
1402     np.getEnd().addNext(faexn);
1403     return new NodePair(faen, faexn);
1404   }
1405
1406   private NodePair flattenGenReachNode( GenReachNode grn ) {
1407     FlatGenReachNode fgrn = new FlatGenReachNode( grn.getGraphName() );
1408     return new NodePair( fgrn, fgrn );
1409   }
1410
1411   private NodePair flattenSESENode(SESENode sn) {
1412     if( sn.isStart() ) {
1413       FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1414       sn.setFlatEnter(fsen);
1415       return new NodePair(fsen, fsen);
1416     }
1417
1418     FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1419     sn.setFlatExit(fsexn);
1420     FlatSESEEnterNode fsen=sn.getStart().getFlatEnter();
1421     fsexn.setFlatEnter(fsen);    
1422     sn.getStart().getFlatEnter().setFlatExit( fsexn );
1423     
1424     return new NodePair(fsexn, fsexn);
1425   }
1426
1427   private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1428       FlatNop fn=new FlatNop();
1429       if (cbn.isBreak())
1430           breakset.add(fn);
1431       else
1432           continueset.add(fn);
1433       return new NodePair(fn,null);
1434   }
1435
1436   private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1437     TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1438     NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1439     FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1440     cond.getEnd().addNext(fion);
1441     return new NodePair(cond.getBegin(),fion);
1442   }
1443
1444   private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1445     boolean isGlobal = false;
1446     String disjointId = null;
1447     // get the type the array to be initialized
1448     TypeDescriptor td = ain.getType();
1449
1450     // create a new array of size equal to the array initializer
1451     FlatNode first=null;
1452     FlatNode last=null;
1453     TempDescriptor tmp=TempDescriptor.tempFactory("arg", new TypeDescriptor(TypeDescriptor.INT));
1454     FlatLiteralNode fln_tmp=new FlatLiteralNode(tmp.getType(), new Integer(ain.numVarInitializers()), tmp);
1455     first = last=fln_tmp;
1456     
1457     // create the new array
1458     FlatNew fn=new FlatNew(td, out_temp, tmp, isGlobal, disjointId);
1459     last.addNext(fn);
1460     last = fn;
1461     
1462     // initialize the new array
1463     for(int i = 0; i < ain.numVarInitializers(); i++) {
1464       ExpressionNode var_init_node = ain.getVarInitializer(i);
1465       TempDescriptor tmp_toinit = out_temp;
1466       TempDescriptor tmp_init=TempDescriptor.tempFactory("array_init", td.dereference());
1467       // index=i
1468       TempDescriptor index=TempDescriptor.tempFactory("index", new TypeDescriptor(TypeDescriptor.INT));
1469       FlatLiteralNode fln=new FlatLiteralNode(index.getType(), new Integer(i), index);
1470       // calculate the initial value
1471       NodePair np_init = flattenExpressionNode(var_init_node, tmp_init);
1472       // TODO wrapper class process is missing now
1473       /*if(td.isArray() && td.dereference().iswrapper()) {
1474       }*/
1475       FlatSetElementNode fsen=new FlatSetElementNode(tmp_toinit, index, tmp_init);
1476       last.addNext(fln);
1477       fln.addNext(np_init.getBegin());
1478       np_init.getEnd().addNext(fsen);
1479       last = fsen;
1480     }
1481     
1482     return new NodePair(first, last);
1483   }
1484
1485   private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1486     TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1487     TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1488     TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1489
1490     NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1491     FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1492
1493     NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1494     FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1495
1496     NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1497     FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1498
1499     FlatNop nopend=new FlatNop();
1500
1501     cond.getEnd().addNext(fcb);
1502
1503     fcb.addTrueNext(trueExpr.getBegin());
1504     fcb.addFalseNext(falseExpr.getBegin());
1505
1506     trueExpr.getEnd().addNext(fonT);
1507     fonT.addNext(nopend);
1508
1509     falseExpr.getEnd().addNext(fonF);
1510     fonF.addNext(nopend);
1511     
1512     return new NodePair(cond.getBegin(), nopend);
1513   }
1514
1515   private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1516     switch(bsn.kind()) {
1517     case Kind.BlockExpressionNode:
1518       return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1519
1520     case Kind.DeclarationNode:
1521       return flattenDeclarationNode((DeclarationNode)bsn);
1522
1523     case Kind.TagDeclarationNode:
1524       return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1525
1526     case Kind.IfStatementNode:
1527       return flattenIfStatementNode((IfStatementNode)bsn);
1528       
1529     case Kind.SwitchStatementNode:
1530       return flattenSwitchStatementNode((SwitchStatementNode)bsn);
1531
1532     case Kind.LoopNode:
1533       return flattenLoopNode((LoopNode)bsn);
1534
1535     case Kind.ReturnNode:
1536       return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1537
1538     case Kind.TaskExitNode:
1539       return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1540
1541     case Kind.SubBlockNode:
1542       return flattenSubBlockNode((SubBlockNode)bsn);
1543
1544     case Kind.AtomicNode:
1545       return flattenAtomicNode((AtomicNode)bsn);
1546
1547     case Kind.SynchronizedNode:
1548       return flattenSynchronizedNode((SynchronizedNode)bsn);
1549
1550     case Kind.SESENode:
1551       return flattenSESENode((SESENode)bsn);
1552
1553     case Kind.GenReachNode:
1554       return flattenGenReachNode((GenReachNode)bsn);
1555
1556     case Kind.ContinueBreakNode:
1557       return flattenContinueBreakNode((ContinueBreakNode)bsn);
1558     }
1559     throw new Error();
1560   }
1561 }