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