some bug fix
[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       thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
454       NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
455       first=np.getBegin();
456       last=np.getEnd();
457     }
458
459     //Build arguments
460     for(int i=0; i<min.numArgs(); i++) {
461       ExpressionNode en=min.getArg(i);
462       TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
463       temps[i]=td;
464       NodePair np=flattenExpressionNode(en, td);
465       if (first==null)
466         first=np.getBegin();
467       else
468         last.addNext(np.getBegin());
469       last=np.getEnd();
470     }
471
472     MethodDescriptor md=min.getMethod();
473
474     //Call to constructor
475
476     FlatCall fc;
477     if(md.getReturnType()==null||md.getReturnType().isVoid())
478       fc=new FlatCall(md, null, thisarg, temps);
479     else
480       fc=new FlatCall(md, out_temp, thisarg, temps);
481     if (first==null) {
482       first=fc;
483     } else
484       last.addNext(fc);
485     return new NodePair(first,fc);
486   }
487
488   private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
489     TempDescriptor tmp=null;
490     if(fan.getExpression().getType().isStatic()) {
491       // static field dereference with class name
492       tmp = new TempDescriptor(fan.getExpression().getType().getClassDesc().getSymbol(), fan.getExpression().getType());
493       FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
494       return new NodePair(fn,fn);
495     } else {
496       tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
497       NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
498       FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
499       npe.getEnd().addNext(fn);
500       return new NodePair(npe.getBegin(),fn);
501     }
502   }
503
504   private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
505     TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
506     TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
507     NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
508     NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
509     TempDescriptor arraytmp=out_temp;
510     if (aan.iswrapper()) {
511       //have wrapper
512       arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference());
513     }
514     FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp);
515     npe.getEnd().addNext(npi.getBegin());
516     npi.getEnd().addNext(fn);
517     if (aan.iswrapper()) {
518       FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp);
519       fn.addNext(ffn);
520       fn=ffn;
521     }
522     return new NodePair(npe.getBegin(),fn);
523   }
524
525   private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
526     // Three cases:
527     // left side is variable
528     // left side is field
529     // left side is array
530
531     Operation base=an.getOperation().getBaseOp();
532     boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
533
534     if (!pre) {
535       //rewrite the base operation
536       base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
537     }
538     FlatNode first=null;
539     FlatNode last=null;
540     TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
541
542     //Get src value
543     if (an.getSrc()!=null) {
544       if(an.getSrc().getEval() != null) {
545         first = last = new FlatLiteralNode(an.getSrc().getType(), an.getSrc().getEval().longValue(), src_tmp);
546       } else {
547         NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
548         first=np_src.getBegin();
549         last=np_src.getEnd();
550       }
551     } else if (!pre) {
552       FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
553       first=fln;
554       last=fln;
555     }
556
557     if (an.getDest().kind()==Kind.FieldAccessNode) {
558       //We are assigning an object field
559
560       FieldAccessNode fan=(FieldAccessNode)an.getDest();
561       ExpressionNode en=fan.getExpression();
562       TempDescriptor dst_tmp=null;
563       NodePair np_baseexp=null;
564       if(en.getType().isStatic()) {
565         // static field dereference with class name
566         dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
567         FlatNop nop=new FlatNop();
568         np_baseexp = new NodePair(nop,nop);
569       } else {
570         dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
571         np_baseexp=flattenExpressionNode(en, dst_tmp);
572       }
573       if (first==null)
574         first=np_baseexp.getBegin();
575       else
576         last.addNext(np_baseexp.getBegin());
577       last=np_baseexp.getEnd();
578
579       //See if we need to perform an operation
580       if (base!=null) {
581         //If it is a preinc we need to store the initial value
582         TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
583         TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
584         FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
585         last.addNext(ffn);
586         last=ffn;
587
588         if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
589           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
590           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
591           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
592           src_tmp=tmp;
593           last.addNext(fc);
594           last=fc;
595         } else {
596           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
597           src_tmp=tmp;
598           last.addNext(fon);
599           last=fon;
600         }
601       }
602
603       FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
604       last.addNext(fsfn);
605       last=fsfn;
606       if (pre) {
607         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
608         fsfn.addNext(fon2);
609         last=fon2;
610       }
611       return new NodePair(first, last);
612     } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
613       //We are assigning an array element
614
615       ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
616       ExpressionNode en=aan.getExpression();
617       ExpressionNode enindex=aan.getIndex();
618       TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
619       TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
620       NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
621       NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
622       if (first==null)
623         first=np_baseexp.getBegin();
624       else
625         last.addNext(np_baseexp.getBegin());
626       np_baseexp.getEnd().addNext(np_indexexp.getBegin());
627       last=np_indexexp.getEnd();
628
629       //See if we need to perform an operation
630       if (base!=null) {
631         //If it is a preinc we need to store the initial value
632         TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
633         TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
634
635         if (aan.iswrapper()) {
636           TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
637           TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
638           FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
639           FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp3,src_tmp2);
640           last.addNext(fen);
641           fen.addNext(ffn);
642           last=ffn;
643         } else {
644           FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
645           last.addNext(fen);
646           last=fen;
647         }
648         if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
649           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
650           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
651           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
652           src_tmp=tmp;
653           last.addNext(fc);
654           last=fc;
655         } else {
656           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
657           src_tmp=tmp;
658           last.addNext(fon);
659           last=fon;
660         }
661       }
662
663       if (aan.iswrapper()) { 
664         TypeDescriptor arrayeltype=aan.getExpression().getType().dereference();
665         TempDescriptor src_tmp3=TempDescriptor.tempFactory("src3",arrayeltype);
666         FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp3);
667         FlatSetFieldNode fsfn=new FlatSetFieldNode(src_tmp3,(FieldDescriptor)arrayeltype.getClassDesc().getFieldTable().get("value"),src_tmp);
668         last.addNext(fen);
669         fen.addNext(fsfn);
670         last=fsfn;
671       } else { 
672         FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
673         last.addNext(fsen);
674         last=fsen;
675       }
676       if (pre) {
677         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
678         last.addNext(fon2);
679         last=fon2;
680       }
681       return new NodePair(first, last);
682     } else if (an.getDest().kind()==Kind.NameNode) {
683       //We could be assigning a field or variable
684       NameNode nn=(NameNode)an.getDest();
685
686
687       if (nn.getExpression()!=null) {
688         //It is a field
689         FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
690         ExpressionNode en=fan.getExpression();
691     TempDescriptor dst_tmp=null;
692     NodePair np_baseexp=null;
693     if(en.getType().isStatic()) {
694       // static field dereference with class name
695       dst_tmp = new TempDescriptor(en.getType().getClassDesc().getSymbol(), en.getType());
696       FlatNop nop=new FlatNop();
697       np_baseexp = new NodePair(nop,nop);
698     } else {
699       dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
700       np_baseexp=flattenExpressionNode(en, dst_tmp);
701     }
702         if (first==null)
703           first=np_baseexp.getBegin();
704         else
705           last.addNext(np_baseexp.getBegin());
706         last=np_baseexp.getEnd();
707
708         //See if we need to perform an operation
709         if (base!=null) {
710           //If it is a preinc we need to store the initial value
711           TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
712           TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
713
714           FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
715           last.addNext(ffn);
716           last=ffn;
717
718
719           if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
720             ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
721             MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
722             FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
723             src_tmp=tmp;
724             last.addNext(fc);
725             last=fc;
726           } else {
727             FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
728             src_tmp=tmp;
729             last.addNext(fon);
730             last=fon;
731           }
732         }
733
734
735         FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
736         last.addNext(fsfn);
737         last=fsfn;
738         if (pre) {
739           FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
740           fsfn.addNext(fon2);
741           last=fon2;
742         }
743         return new NodePair(first, last);
744       } else {
745         if (nn.getField()!=null) {
746           //It is a field
747           //Get src value
748
749           //See if we need to perform an operation
750           if (base!=null) {
751             //If it is a preinc we need to store the initial value
752             TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
753             TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
754
755             FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
756             if (first==null)
757               first=ffn;
758             else {
759               last.addNext(ffn);
760             }
761             last=ffn;
762
763
764             if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
765               ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
766               MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
767               FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
768               src_tmp=tmp;
769               last.addNext(fc);
770               last=fc;
771             } else {
772               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
773               src_tmp=tmp;
774               last.addNext(fon);
775               last=fon;
776             }
777           }
778
779       FlatSetFieldNode fsfn=null;
780       if(nn.getClassDesc()!=null) {
781         // this is a static field access inside of a static block
782         fsfn=new FlatSetFieldNode(new TempDescriptor("sfsb", nn.getClassType()), nn.getField(), src_tmp);
783       } else {
784         fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
785       }
786           if (first==null) {
787             first=fsfn;
788           } else {
789             last.addNext(fsfn);
790           }
791           last=fsfn;
792           if (pre) {
793             FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
794             fsfn.addNext(fon2);
795             last=fon2;
796           }
797           return new NodePair(first, last);
798         } else {
799           //It is a variable
800           //See if we need to perform an operation
801
802           if (base!=null) {
803             //If it is a preinc we need to store the initial value
804             TempDescriptor src_tmp2=getTempforVar(nn.getVar());
805             TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
806             if (!pre) {
807               FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
808               if (first==null)
809                 first=fon;
810               else
811                 last.addNext(fon);
812               last=fon;
813             }
814
815
816             if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
817               ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
818               MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
819               FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
820               if (first==null)
821                 first=fc;
822               else
823                 last.addNext(fc);
824               src_tmp=tmp;
825               last=fc;
826             } else {
827               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
828               if (first==null)
829                 first=fon;
830               else
831                 last.addNext(fon);
832               src_tmp=tmp;
833               last=fon;
834             }
835           }
836
837           FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
838
839           last.addNext(fon);
840           last=fon;
841           if (pre) {
842             FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
843             fon.addNext(fon2);
844             last=fon2;
845           }
846           return new NodePair(first, last);
847         } //end of else
848       }
849     }
850
851     throw new Error();
852   }
853
854   private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
855     if (nn.getExpression()!=null) {
856       /* Hack - use subtree instead */
857       return flattenExpressionNode(nn.getExpression(),out_temp);
858     } else if (nn.getField()!=null) {
859       TempDescriptor tmp= null;
860       if(state.MGC && (nn.getClassDesc() != null)) {
861         // this is a static field
862         tmp = new TempDescriptor(nn.getClassDesc().getSymbol(), nn.getClassType());
863         
864       } else {
865       tmp=getTempforVar(nn.getVar());
866       }
867       FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
868       return new NodePair(ffn,ffn);
869     } else {
870       TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
871       if (nn.isTag()) {
872         //propagate tag
873         out_temp.setTag(tmp.getTag());
874       }
875       FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
876       return new NodePair(fon,fon);
877     }
878   }
879
880   private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
881     TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
882     TempDescriptor temp_right=null;
883
884     Operation op=on.getOp();
885
886     NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
887     NodePair right;
888     if (on.getRight()!=null) {
889       temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
890       right=flattenExpressionNode(on.getRight(),temp_right);
891     } else {
892       FlatNop nop=new FlatNop();
893       right=new NodePair(nop,nop);
894     }
895
896     if (op.getOp()==Operation.LOGIC_OR) {
897       /* Need to do shortcircuiting */
898       FlatCondBranch fcb=new FlatCondBranch(temp_left);
899       FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
900       FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
901       FlatNop fnop=new FlatNop();
902       left.getEnd().addNext(fcb);
903       fcb.addFalseNext(right.getBegin());
904       right.getEnd().addNext(fon2);
905       fon2.addNext(fnop);
906       fcb.addTrueNext(fon1);
907       fon1.addNext(fnop);
908       return new NodePair(left.getBegin(), fnop);
909     } else if (op.getOp()==Operation.LOGIC_AND) {
910       /* Need to do shortcircuiting */
911       FlatCondBranch fcb=new FlatCondBranch(temp_left);
912       FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
913       FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
914       FlatNop fnop=new FlatNop();
915       left.getEnd().addNext(fcb);
916       fcb.addTrueNext(right.getBegin());
917       right.getEnd().addNext(fon2);
918       fon2.addNext(fnop);
919       fcb.addFalseNext(fon1);
920       fon1.addNext(fnop);
921       return new NodePair(left.getBegin(), fnop);
922     } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
923       //We have a string concatenate
924       ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
925       MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
926       FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
927       left.getEnd().addNext(right.getBegin());
928       right.getEnd().addNext(fc);
929       return new NodePair(left.getBegin(), fc);
930     }
931
932     FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
933     left.getEnd().addNext(right.getBegin());
934     right.getEnd().addNext(fon);
935     return new NodePair(left.getBegin(),fon);
936   }
937
938   private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
939     switch(en.kind()) {
940     case Kind.AssignmentNode:
941       return flattenAssignmentNode((AssignmentNode)en,out_temp);
942
943     case Kind.CastNode:
944       return flattenCastNode((CastNode)en,out_temp);
945
946     case Kind.CreateObjectNode:
947       return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
948
949     case Kind.FieldAccessNode:
950       return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
951
952     case Kind.ArrayAccessNode:
953       return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
954
955     case Kind.LiteralNode:
956       return flattenLiteralNode((LiteralNode)en,out_temp);
957
958     case Kind.MethodInvokeNode:
959       return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
960
961     case Kind.NameNode:
962       return flattenNameNode((NameNode)en,out_temp);
963
964     case Kind.OpNode:
965       return flattenOpNode((OpNode)en,out_temp);      
966
967     case Kind.OffsetNode:
968       return flattenOffsetNode((OffsetNode)en,out_temp);
969
970     case Kind.TertiaryNode:
971       return flattenTertiaryNode((TertiaryNode)en,out_temp);
972
973     case Kind.InstanceOfNode:
974       return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
975
976     case Kind.ArrayInitializerNode:
977       return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
978     }
979     throw new Error();
980   }
981
982   private NodePair flattenDeclarationNode(DeclarationNode dn) {
983     VarDescriptor vd=dn.getVarDescriptor();
984     TempDescriptor td=getTempforVar(vd);
985     if (dn.getExpression()!=null)
986       return flattenExpressionNode(dn.getExpression(),td);
987     else {
988       FlatNop fn=new FlatNop();
989       return new NodePair(fn,fn);
990     }
991   }
992
993   private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
994     TagVarDescriptor tvd=dn.getTagVarDescriptor();
995     TagDescriptor tag=tvd.getTag();
996     TempDescriptor tmp=getTempforVar(tvd);
997     FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
998     return new NodePair(ftd,ftd);
999   }
1000
1001   private TempDescriptor getTempforParam(Descriptor d) {
1002     if (temptovar.containsKey(d))
1003       return (TempDescriptor)temptovar.get(d);
1004     else {
1005       if (d instanceof VarDescriptor) {
1006         VarDescriptor vd=(VarDescriptor)d;
1007         TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
1008         temptovar.put(vd,td);
1009         return td;
1010       } else if (d instanceof TagVarDescriptor) {
1011         TagVarDescriptor tvd=(TagVarDescriptor)d;
1012         TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1013         TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
1014         temptovar.put(tvd,td);
1015         return td;
1016       } else throw new Error("Unreconized Descriptor");
1017     }
1018   }
1019
1020   private TempDescriptor getTempforVar(Descriptor d) {
1021     if (temptovar.containsKey(d))
1022       return (TempDescriptor)temptovar.get(d);
1023     else {
1024       if (d instanceof VarDescriptor) {
1025         VarDescriptor vd=(VarDescriptor)d;
1026         TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
1027         temptovar.put(vd,td);
1028         return td;
1029       } else if (d instanceof TagVarDescriptor) {
1030         TagVarDescriptor tvd=(TagVarDescriptor)d;
1031         //BUGFIX TAGTYPE - add next line, modify following
1032         //line to tag this new type descriptor, modify
1033         //TempDescriptor constructor & factory to set type
1034         //using this Type To test, use any program with tags
1035         TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
1036         TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
1037         temptovar.put(tvd,td);
1038         return td;
1039       } else throw new Error("Unrecognized Descriptor");
1040     }
1041   }
1042
1043   private NodePair flattenIfStatementNode(IfStatementNode isn) {
1044     TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1045     NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
1046     FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1047     NodePair true_np=flattenBlockNode(isn.getTrueBlock());
1048     NodePair false_np;
1049     FlatNop nopend=new FlatNop();
1050
1051     if (isn.getFalseBlock()!=null)
1052       false_np=flattenBlockNode(isn.getFalseBlock());
1053     else {
1054       FlatNop nop=new FlatNop();
1055       false_np=new NodePair(nop,nop);
1056     }
1057
1058     cond.getEnd().addNext(fcb);
1059     fcb.addTrueNext(true_np.getBegin());
1060     fcb.addFalseNext(false_np.getBegin());
1061     if (true_np.getEnd()!=null)
1062         true_np.getEnd().addNext(nopend);
1063     if (false_np.getEnd()!=null)
1064         false_np.getEnd().addNext(nopend);
1065     if (nopend.numPrev()==0)
1066       return new NodePair(cond.getBegin(), null);
1067
1068     return new NodePair(cond.getBegin(), nopend);
1069   }
1070   
1071   private NodePair flattenSwitchStatementNode(SwitchStatementNode ssn) {
1072     TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.INT));
1073     NodePair cond=flattenExpressionNode(ssn.getCondition(),cond_temp);
1074     FlatNop nopend=new FlatNop();
1075     NodePair sbody = flattenSwitchBodyNode(ssn.getSwitchBody(), cond_temp, nopend);
1076     
1077     cond.getEnd().addNext(sbody.getBegin());
1078
1079     return new NodePair(cond.getBegin(), sbody.getEnd());
1080   }
1081   
1082   private NodePair flattenSwitchBodyNode(BlockNode bn, TempDescriptor cond_temp, FlatNode endnode) {
1083     FlatNode begin=null;
1084     FlatNode end=endnode;
1085     NodePair prev_true_branch = null;
1086     NodePair prev_false_branch = null;
1087     for(int i=0; i<bn.size(); i++) {
1088       SwitchBlockNode sbn = (SwitchBlockNode)bn.get(i);
1089       HashSet oldbs=breakset;
1090       breakset=new HashSet();
1091       
1092       NodePair body=flattenBlockNode(sbn.getSwitchBlockStatement());
1093       Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
1094       FlatNode cond_begin = null;
1095       NodePair prev_fnp = null;
1096       for(int j = 0; j < slnv.size(); j++) {
1097         SwitchLabelNode sln = slnv.elementAt(j);
1098         NodePair left = null;
1099         NodePair false_np = null;
1100         if(sln.isDefault()) {
1101           left = body;
1102         } else {
1103           TempDescriptor cond_tmp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1104           TempDescriptor temp_left=TempDescriptor.tempFactory("leftop", sln.getCondition().getType());
1105           Operation op=new Operation(Operation.EQUAL);
1106           left=flattenExpressionNode(sln.getCondition(), temp_left);
1107           FlatOpNode fon=new FlatOpNode(cond_tmp, temp_left, cond_temp, op);
1108           left.getEnd().addNext(fon);
1109
1110           FlatCondBranch fcb=new FlatCondBranch(cond_tmp);
1111           fcb.setTrueProb(State.TRUEPROB);
1112
1113           FlatNop nop=new FlatNop();
1114           false_np=new NodePair(nop,nop);
1115
1116           fon.addNext(fcb);
1117           fcb.addTrueNext(body.getBegin());
1118           fcb.addFalseNext(false_np.getBegin());
1119         }
1120         if((prev_fnp != null) && (prev_fnp.getEnd() != null)) {
1121           prev_fnp.getEnd().addNext(left.getBegin());
1122         }
1123         prev_fnp = false_np;
1124         
1125         if (begin==null) {
1126           begin = left.getBegin();
1127         }
1128         if(cond_begin == null) {
1129           cond_begin = left.getBegin();
1130         }
1131       }
1132       if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1133         prev_false_branch.getEnd().addNext(cond_begin);
1134       }
1135       prev_false_branch = prev_fnp;
1136       if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1137         prev_true_branch.getEnd().addNext(body.getBegin());
1138       }
1139       prev_true_branch = body;
1140       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1141         FlatNode fn=(FlatNode)breakit.next();
1142         breakit.remove();
1143         fn.addNext(endnode);
1144       }
1145       breakset=oldbs;
1146     }
1147     if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
1148       prev_true_branch.getEnd().addNext(endnode);
1149     }
1150     if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
1151       prev_false_branch.getEnd().addNext(endnode);
1152     }
1153     if(begin == null) {
1154       end=begin=new FlatNop();
1155     }
1156     return new NodePair(begin,end);
1157   }
1158   
1159   private NodePair flattenLoopNode(LoopNode ln) {
1160     HashSet oldbs=breakset;
1161     HashSet oldcs=continueset;
1162     breakset=new HashSet();
1163     continueset=new HashSet();
1164     
1165     if (ln.getType()==LoopNode.FORLOOP) {
1166       NodePair initializer=flattenBlockNode(ln.getInitializer());
1167       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1168       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1169       NodePair update=flattenBlockNode(ln.getUpdate());
1170       NodePair body=flattenBlockNode(ln.getBody());
1171       FlatNode begin=initializer.getBegin();
1172       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1173       fcb.setTrueProb(State.TRUEPROB);
1174       fcb.setLoop();
1175       FlatNop nopend=new FlatNop();
1176       FlatBackEdge backedge=new FlatBackEdge();
1177
1178       FlatNop nop2=new FlatNop();
1179       initializer.getEnd().addNext(nop2);
1180       nop2.addNext(condition.getBegin());
1181       if (body.getEnd()!=null)
1182           body.getEnd().addNext(update.getBegin());
1183       update.getEnd().addNext(backedge);
1184       backedge.addNext(condition.getBegin());
1185       condition.getEnd().addNext(fcb);
1186       fcb.addFalseNext(nopend);
1187       fcb.addTrueNext(body.getBegin());
1188       for(Iterator contit=continueset.iterator();contit.hasNext();) {
1189           FlatNode fn=(FlatNode)contit.next();
1190           contit.remove();
1191           fn.addNext(update.getBegin());
1192       }
1193       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1194           FlatNode fn=(FlatNode)breakit.next();
1195           breakit.remove();
1196           fn.addNext(nopend);
1197       }
1198       breakset=oldbs;
1199       continueset=oldcs;
1200       return new NodePair(begin,nopend);
1201     } else if (ln.getType()==LoopNode.WHILELOOP) {
1202       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1203       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1204       NodePair body=flattenBlockNode(ln.getBody());
1205       FlatNode begin=condition.getBegin();
1206       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1207       fcb.setTrueProb(State.TRUEPROB);
1208       fcb.setLoop();
1209       FlatNop nopend=new FlatNop();
1210       FlatBackEdge backedge=new FlatBackEdge();
1211
1212       if (body.getEnd()!=null)
1213         body.getEnd().addNext(backedge);
1214       backedge.addNext(condition.getBegin());
1215
1216       condition.getEnd().addNext(fcb);
1217       fcb.addFalseNext(nopend);
1218       fcb.addTrueNext(body.getBegin());
1219
1220       for(Iterator contit=continueset.iterator();contit.hasNext();) {
1221           FlatNode fn=(FlatNode)contit.next();
1222           contit.remove();
1223           fn.addNext(backedge);
1224       }
1225       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1226           FlatNode fn=(FlatNode)breakit.next();
1227           breakit.remove();
1228           fn.addNext(nopend);
1229       }
1230       breakset=oldbs;
1231       continueset=oldcs;
1232       return new NodePair(begin,nopend);
1233     } else if (ln.getType()==LoopNode.DOWHILELOOP) {
1234       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
1235       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
1236       NodePair body=flattenBlockNode(ln.getBody());
1237       FlatNode begin=body.getBegin();
1238       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1239       fcb.setTrueProb(State.TRUEPROB);
1240       fcb.setLoop();
1241       FlatNop nopend=new FlatNop();
1242       FlatBackEdge backedge=new FlatBackEdge();
1243
1244       if (body.getEnd()!=null)
1245         body.getEnd().addNext(condition.getBegin());
1246       condition.getEnd().addNext(fcb);
1247       fcb.addFalseNext(nopend);
1248       fcb.addTrueNext(backedge);
1249       backedge.addNext(body.getBegin());
1250
1251       for(Iterator contit=continueset.iterator();contit.hasNext();) {
1252           FlatNode fn=(FlatNode)contit.next();
1253           contit.remove();
1254           fn.addNext(condition.getBegin());
1255       }
1256       for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
1257           FlatNode fn=(FlatNode)breakit.next();
1258           breakit.remove();
1259           fn.addNext(nopend);
1260       }
1261       breakset=oldbs;
1262       continueset=oldcs;
1263       return new NodePair(begin,nopend);
1264     } else throw new Error();
1265   }
1266
1267   private NodePair flattenReturnNode(ReturnNode rntree) {
1268     TempDescriptor retval=null;
1269     NodePair cond=null;
1270     if (rntree.getReturnExpression()!=null) {
1271       retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
1272       cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
1273     }
1274
1275     FlatReturnNode rnflat=new FlatReturnNode(retval);
1276     rnflat.addNext(fe);
1277     FlatNode ln=rnflat;
1278     if ((state.THREAD||state.MGC)&&currmd.getModifiers().isSynchronized()) {
1279       MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1280       TempDescriptor thistd=null;
1281       if(currmd.getModifiers().isStatic()) {
1282         // need to lock the Class object
1283         thistd=new TempDescriptor("classobj", currmd.getClassDesc());
1284       } else {
1285         // lock this object
1286         thistd=getTempforVar(currmd.getThis());
1287       }
1288       FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
1289       fc.addNext(ln);
1290       ln=fc;
1291     }
1292     if (state.DSM&&currmd.getModifiers().isAtomic()) {
1293       FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
1294       faen.addNext(ln);
1295       ln=faen;
1296     }
1297
1298     if (cond!=null) {
1299       cond.getEnd().addNext(ln);
1300       return new NodePair(cond.getBegin(),null);
1301     } else
1302       return new NodePair(ln,null);
1303   }
1304
1305   private NodePair flattenTaskExitNode(TaskExitNode ten) {
1306     FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
1307     ffan.setTaskExitIndex(ten.getTaskExitIndex());
1308     updateFlagActionNode(ffan, ten.getFlagEffects());
1309     NodePair fcn=flattenConstraintCheck(ten.getChecks());
1310     ffan.addNext(fcn.getBegin());
1311     FlatReturnNode rnflat=new FlatReturnNode(null);
1312     rnflat.addNext(fe);
1313     fcn.getEnd().addNext(rnflat);
1314     return new NodePair(ffan, null);
1315   }
1316
1317   private NodePair flattenConstraintCheck(Vector ccs) {
1318     FlatNode begin=new FlatNop();
1319     if (ccs==null)
1320       return new NodePair(begin,begin);
1321     FlatNode last=begin;
1322     for(int i=0; i<ccs.size(); i++) {
1323       ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
1324       /* Flatten the arguments */
1325       TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
1326       String[] vars=new String[cc.numArgs()];
1327       for(int j=0; j<cc.numArgs(); j++) {
1328         ExpressionNode en=cc.getArg(j);
1329         TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
1330         temps[j]=td;
1331         vars[j]=cc.getVar(j);
1332         NodePair np=flattenExpressionNode(en, td);
1333         last.addNext(np.getBegin());
1334         last=np.getEnd();
1335       }
1336
1337       FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
1338       last.addNext(fcn);
1339       last=fcn;
1340     }
1341     return new NodePair(begin,last);
1342   }
1343
1344   private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1345     return flattenBlockNode(sbn.getBlockNode());
1346   }
1347
1348   private NodePair flattenSynchronizedNode(SynchronizedNode sbn) {
1349     TempDescriptor montmp=null;
1350     FlatNode first = null;
1351     FlatNode end = null;
1352     if(sbn.getExpr() instanceof ClassTypeNode) {
1353       montmp=new TempDescriptor("classobj", ((ClassTypeNode)sbn.getExpr()).getType().getClassDesc());
1354     } else {
1355       montmp = TempDescriptor.tempFactory("monitor",sbn.getExpr().getType());
1356       NodePair npexp=flattenExpressionNode(sbn.getExpr(), montmp);
1357       first = npexp.getBegin();
1358       end = npexp.getEnd();
1359     }
1360     NodePair npblock=flattenBlockNode(sbn.getBlockNode());
1361
1362     MethodDescriptor menmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
1363     FlatCall fcen=new FlatCall(menmd, null, montmp, new TempDescriptor[0]);
1364
1365     MethodDescriptor mexmd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
1366     FlatCall fcex=new FlatCall(mexmd, null, montmp, new TempDescriptor[0]);
1367
1368     if(first != null) {
1369       end.addNext(fcen);
1370     } else {
1371       first = fcen;
1372     }
1373     fcen.addNext(npblock.getBegin());
1374     npblock.getEnd().addNext(fcex);
1375     return new NodePair(first, fcex);
1376   }
1377
1378   private NodePair flattenAtomicNode(AtomicNode sbn) {
1379     NodePair np=flattenBlockNode(sbn.getBlockNode());
1380     FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1381     FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1382     faen.addNext(np.getBegin());
1383     np.getEnd().addNext(faexn);
1384     return new NodePair(faen, faexn);
1385   }
1386
1387   private NodePair flattenGenReachNode( GenReachNode grn ) {
1388     FlatGenReachNode fgrn = new FlatGenReachNode( grn.getGraphName() );
1389     return new NodePair( fgrn, fgrn );
1390   }
1391
1392   private NodePair flattenSESENode(SESENode sn) {
1393     if( sn.isStart() ) {
1394       FlatSESEEnterNode fsen=new FlatSESEEnterNode(sn);
1395       sn.setFlatEnter(fsen);
1396       return new NodePair(fsen, fsen);
1397     }
1398
1399     FlatSESEExitNode fsexn=new FlatSESEExitNode(sn);
1400     sn.setFlatExit(fsexn);
1401     FlatSESEEnterNode fsen=sn.getStart().getFlatEnter();
1402     fsexn.setFlatEnter(fsen);    
1403     sn.getStart().getFlatEnter().setFlatExit( fsexn );
1404     
1405     return new NodePair(fsexn, fsexn);
1406   }
1407
1408   private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) {
1409       FlatNop fn=new FlatNop();
1410       if (cbn.isBreak())
1411           breakset.add(fn);
1412       else
1413           continueset.add(fn);
1414       return new NodePair(fn,null);
1415   }
1416
1417   private NodePair flattenInstanceOfNode(InstanceOfNode tn, TempDescriptor out_temp) {
1418     TempDescriptor expr_temp=TempDescriptor.tempFactory("expr",tn.getExpr().getType());
1419     NodePair cond=flattenExpressionNode(tn.getExpr(), expr_temp);
1420     FlatInstanceOfNode fion=new FlatInstanceOfNode(tn.getExprType(), expr_temp, out_temp);
1421     cond.getEnd().addNext(fion);
1422     return new NodePair(cond.getBegin(),fion);
1423   }
1424
1425   private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
1426     boolean isGlobal = false;
1427     String disjointId = null;
1428     // get the type the array to be initialized
1429     TypeDescriptor td = ain.getType();
1430
1431     // create a new array of size equal to the array initializer
1432     FlatNode first=null;
1433     FlatNode last=null;
1434     TempDescriptor tmp=TempDescriptor.tempFactory("arg", new TypeDescriptor(TypeDescriptor.INT));
1435     FlatLiteralNode fln_tmp=new FlatLiteralNode(tmp.getType(), new Integer(ain.numVarInitializers()), tmp);
1436     first = last=fln_tmp;
1437     
1438     // create the new array
1439     FlatNew fn=new FlatNew(td, out_temp, tmp, isGlobal, disjointId);
1440     last.addNext(fn);
1441     last = fn;
1442     
1443     // initialize the new array
1444     for(int i = 0; i < ain.numVarInitializers(); i++) {
1445       ExpressionNode var_init_node = ain.getVarInitializer(i);
1446       TempDescriptor tmp_toinit = out_temp;
1447       TempDescriptor tmp_init=TempDescriptor.tempFactory("array_init", td.dereference());
1448       // index=i
1449       TempDescriptor index=TempDescriptor.tempFactory("index", new TypeDescriptor(TypeDescriptor.INT));
1450       FlatLiteralNode fln=new FlatLiteralNode(index.getType(), new Integer(i), index);
1451       // calculate the initial value
1452       NodePair np_init = flattenExpressionNode(var_init_node, tmp_init);
1453       // TODO wrapper class process is missing now
1454       /*if(td.isArray() && td.dereference().iswrapper()) {
1455       }*/
1456       FlatSetElementNode fsen=new FlatSetElementNode(tmp_toinit, index, tmp_init);
1457       last.addNext(fln);
1458       fln.addNext(np_init.getBegin());
1459       np_init.getEnd().addNext(fsen);
1460       last = fsen;
1461     }
1462     
1463     return new NodePair(first, last);
1464   }
1465
1466   private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
1467     TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
1468     TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
1469     TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
1470
1471     NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
1472     FlatCondBranch fcb=new FlatCondBranch(cond_temp);
1473
1474     NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
1475     FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
1476
1477     NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
1478     FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
1479
1480     FlatNop nopend=new FlatNop();
1481
1482     cond.getEnd().addNext(fcb);
1483
1484     fcb.addTrueNext(trueExpr.getBegin());
1485     fcb.addFalseNext(falseExpr.getBegin());
1486
1487     trueExpr.getEnd().addNext(fonT);
1488     fonT.addNext(nopend);
1489
1490     falseExpr.getEnd().addNext(fonF);
1491     fonF.addNext(nopend);
1492     
1493     return new NodePair(cond.getBegin(), nopend);
1494   }
1495
1496   private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1497     switch(bsn.kind()) {
1498     case Kind.BlockExpressionNode:
1499       return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1500
1501     case Kind.DeclarationNode:
1502       return flattenDeclarationNode((DeclarationNode)bsn);
1503
1504     case Kind.TagDeclarationNode:
1505       return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1506
1507     case Kind.IfStatementNode:
1508       return flattenIfStatementNode((IfStatementNode)bsn);
1509       
1510     case Kind.SwitchStatementNode:
1511       return flattenSwitchStatementNode((SwitchStatementNode)bsn);
1512
1513     case Kind.LoopNode:
1514       return flattenLoopNode((LoopNode)bsn);
1515
1516     case Kind.ReturnNode:
1517       return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1518
1519     case Kind.TaskExitNode:
1520       return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1521
1522     case Kind.SubBlockNode:
1523       return flattenSubBlockNode((SubBlockNode)bsn);
1524
1525     case Kind.AtomicNode:
1526       return flattenAtomicNode((AtomicNode)bsn);
1527
1528     case Kind.SynchronizedNode:
1529       return flattenSynchronizedNode((SynchronizedNode)bsn);
1530
1531     case Kind.SESENode:
1532       return flattenSESENode((SESENode)bsn);
1533
1534     case Kind.GenReachNode:
1535       return flattenGenReachNode((GenReachNode)bsn);
1536
1537     case Kind.ContinueBreakNode:
1538       return flattenContinueBreakNode((ContinueBreakNode)bsn);
1539     }
1540     throw new Error();
1541   }
1542 }