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