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