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