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