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