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