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