more changes for rangePrefetch
[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   public BuildFlat(State st, TypeUtil typeutil) {
13     state=st;
14     temptovar=new Hashtable();
15     this.typeutil=typeutil;
16   }
17
18   public Hashtable getMap() {
19     return temptovar;
20   }
21
22   public void buildFlat() {
23     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
24     while(it.hasNext()) {
25       ClassDescriptor cn=(ClassDescriptor)it.next();
26       flattenClass(cn);
27     }
28
29     Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
30     while(task_it.hasNext()) {
31       TaskDescriptor td=(TaskDescriptor)task_it.next();
32       flattenTask(td);
33     }
34   }
35
36   private void flattenTask(TaskDescriptor td) {
37     BlockNode bn=state.getMethodBody(td);
38     NodePair np=flattenBlockNode(bn);
39     FlatNode fn=np.getBegin();
40     if (np.getEnd().kind()!=FKind.FlatReturnNode) {
41       FlatReturnNode rnflat=new FlatReturnNode(null);
42       np.getEnd().addNext(rnflat);
43     }
44
45     FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
46     ffan.addNext(fn);
47
48     FlatMethod fm=new FlatMethod(td);
49     fm.addNext(ffan);
50
51     Hashtable visitedset=new Hashtable();
52
53     for(int i=0; i<td.numParameters(); i++) {
54       VarDescriptor paramvd=td.getParameter(i);
55       fm.addParameterTemp(getTempforVar(paramvd));
56       TagExpressionList tel=td.getTag(paramvd);
57       //BUG added next line to fix...to test feed in any task program
58       if (tel!=null)
59         for(int j=0; j<tel.numTags(); j++) {
60           TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
61           TempDescriptor tagtmp=getTempforVar(tvd);
62           if (!visitedset.containsKey(tvd.getName())) {
63             visitedset.put(tvd.getName(),tvd.getTag());
64             fm.addTagTemp(tagtmp);
65           } else {
66             TagDescriptor tmptd=(TagDescriptor) visitedset.get(tvd.getName());
67             if (!tmptd.equals(tvd.getTag()))
68               throw new Error("Two different tag types with same name as parameters to:"+td);
69           }
70           tel.setTemp(j, tagtmp);
71         }
72     }
73
74     /* Flatten Vector of Flag Effects */
75     Vector flags=td.getFlagEffects();
76     updateFlagActionNode(ffan,flags);
77
78     state.addFlatCode(td,fm);
79   }
80
81
82   /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
83   private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
84     if (flags==null)     // Do nothing if the flag effects vector is empty
85       return;
86
87     for(int i=0; i<flags.size(); i++) {
88       FlagEffects fes=(FlagEffects)flags.get(i);
89       TempDescriptor flagtemp=getTempforVar(fes.getVar());
90       // Process the flags
91       for(int j=0; j<fes.numEffects(); j++) {
92         FlagEffect fe=fes.getEffect(j);
93         ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
94       }
95       // Process the tags
96       for(int j=0; j<fes.numTagEffects(); j++) {
97         TagEffect te=fes.getTagEffect(j);
98         TempDescriptor tagtemp=getTempforVar(te.getTag());
99
100         ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
101       }
102     }
103   }
104
105   FlatAtomicEnterNode curran=null;
106
107   private void flattenClass(ClassDescriptor cn) {
108     Iterator methodit=cn.getMethods();
109     while(methodit.hasNext()) {
110       currmd=(MethodDescriptor)methodit.next();
111       BlockNode bn=state.getMethodBody(currmd);
112
113       if (state.DSM&&currmd.getModifiers().isAtomic()) {
114         curran=new FlatAtomicEnterNode();
115       } else
116         curran=null;
117       NodePair np=flattenBlockNode(bn);
118       FlatNode fn=np.getBegin();
119       if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
120         MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
121         TempDescriptor thistd=getTempforVar(currmd.getThis());
122         FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
123         fc.addNext(fn);
124         fn=fc;
125         if (np.getEnd().kind()!=FKind.FlatReturnNode) {
126           MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
127           FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
128           np.getEnd().addNext(fcunlock);
129           FlatReturnNode rnflat=new FlatReturnNode(null);
130           fcunlock.addNext(rnflat);
131         }
132       } else if (state.DSM&&currmd.getModifiers().isAtomic()) {
133         curran.addNext(fn);
134         fn=curran;
135         if (np.getEnd().kind()!=FKind.FlatReturnNode) {
136           FlatAtomicExitNode aen=new FlatAtomicExitNode(curran);
137           np.getEnd().addNext(aen);
138           FlatReturnNode rnflat=new FlatReturnNode(null);
139           aen.addNext(rnflat);
140         }
141       } else if (np.getEnd().kind()!=FKind.FlatReturnNode) {
142         FlatReturnNode rnflat=new FlatReturnNode(null);
143         np.getEnd().addNext(rnflat);
144       }
145
146       FlatMethod fm=new FlatMethod(currmd);
147       fm.addNext(fn);
148       if (!currmd.isStatic())
149         fm.addParameterTemp(getTempforParam(currmd.getThis()));
150       for(int i=0; i<currmd.numParameters(); i++) {
151         fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
152       }
153       state.addFlatCode(currmd,fm);
154     }
155   }
156
157   private NodePair flattenBlockNode(BlockNode bn) {
158     FlatNode begin=null;
159     FlatNode end=null;
160     for(int i=0; i<bn.size(); i++) {
161       NodePair np=flattenBlockStatementNode(bn.get(i));
162       FlatNode np_begin=np.getBegin();
163       FlatNode np_end=np.getEnd();
164       if (begin==null) {
165         begin=np_begin;
166       }
167       if (end==null) {
168         end=np_end;
169       } else {
170         end.addNext(np_begin);
171         end=np_end;
172       }
173     }
174     if (begin==null) {
175       end=begin=new FlatNop();
176     }
177     return new NodePair(begin,end);
178   }
179
180   private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
181     //System.out.println("DEBUG -> inside flattenBlockExpressionNode\n");
182     TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
183     return flattenExpressionNode(en.getExpression(),tmp);
184   }
185
186   private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
187     TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
188     NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
189     FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
190     np.getEnd().addNext(fcn);
191     return new NodePair(np.getBegin(),fcn);
192   }
193
194   private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
195     FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
196     return new NodePair(fln,fln);
197   }
198
199   private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) {
200     FlatLiteralNode fln = new FlatLiteralNode(ofn.getType(), ofn, out_temp);
201     return new NodePair(fln, fln);
202     /* Another possible approach
203        ClassDescriptor cd = ofn.getClassDesc();
204        FieldDescriptor fd = ofn.getField();
205        FlatOffsetNode fon = new FlatOffsetNode(fd, cd, out_temp);
206        TypeDescriptor type=new TypeDescriptor(ofn.getType());
207        System.out.println("TempDescriptor out_temp = " + out_temp.toString() + "Type of out_temp = " + out_temp.getType());
208        return new NodePair(fon, fon);
209      */
210   }
211
212   private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
213     TypeDescriptor td=con.getType();
214     if (!td.isArray()) {
215       FlatNew fn=new FlatNew(td, out_temp, con.isGlobal());
216       TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
217       FlatNode last=fn;
218       // Build arguments
219       for(int i=0; i<con.numArgs(); i++) {
220         ExpressionNode en=con.getArg(i);
221         TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
222         temps[i]=tmp;
223         NodePair np=flattenExpressionNode(en, tmp);
224         last.addNext(np.getBegin());
225         last=np.getEnd();
226       }
227       MethodDescriptor md=con.getConstructor();
228       //Call to constructor
229       FlatCall fc=new FlatCall(md, null, out_temp, temps);
230       last.addNext(fc);
231       last=fc;
232       if (td.getClassDesc().hasFlags()) {
233         //          if (con.getFlagEffects()!=null) {
234         FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
235         FlagEffects fes=con.getFlagEffects();
236         TempDescriptor flagtemp=out_temp;
237         if (fes!=null) {
238           for(int j=0; j<fes.numEffects(); j++) {
239             FlagEffect fe=fes.getEffect(j);
240             ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
241           }
242           for(int j=0; j<fes.numTagEffects(); j++) {
243             TagEffect te=fes.getTagEffect(j);
244             TempDescriptor tagtemp=getTempforVar(te.getTag());
245
246             ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
247           }
248         } else {
249           ffan.addFlagAction(flagtemp, null, false);
250         }
251         last.addNext(ffan);
252         last=ffan;
253       }
254       return new NodePair(fn,last);
255     } else {
256       FlatNode first=null;
257       FlatNode last=null;
258       TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
259       for (int i=0; i<con.numArgs(); i++) {
260         ExpressionNode en=con.getArg(i);
261         TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
262         temps[i]=tmp;
263         NodePair np=flattenExpressionNode(en, tmp);
264         if (first==null)
265           first=np.getBegin();
266         else
267           last.addNext(np.getBegin());
268         last=np.getEnd();
269
270         TempDescriptor tmp2=(i==0) ?
271                              out_temp :
272                              TempDescriptor.tempFactory("arg",en.getType());
273       }
274       FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal());
275       last.addNext(fn);
276       if (temps.length>1) {
277         NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal());
278         fn.addNext(np.getBegin());
279         return new NodePair(first,np.getEnd());
280       } else
281         return new NodePair(first, fn);
282     }
283   }
284
285   private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) {
286     TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
287     TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
288     FlatNop fnop=new FlatNop();    //last node
289
290     //index=0
291     FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
292     //tmpone=1
293     FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
294
295     TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
296
297     FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
298     FlatCondBranch fcb=new FlatCondBranch(tmpbool);
299     fcb.setTrueProb(State.TRUEPROB);
300     fcb.setLoop();
301     //is index<temp[i]
302     TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
303     FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1], isglobal);
304     FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
305     // index=index+1
306     FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
307     //jump out
308     fln.addNext(fln2);
309     fln2.addNext(fcomp);
310     fcomp.addNext(fcb);
311     fcb.addTrueNext(fn);
312     fcb.addFalseNext(fnop);
313     fn.addNext(fsen);
314     //Recursive call here
315     if ((i+2)<temparray.length) {
316       NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1, isglobal);
317       fsen.addNext(np2.getBegin());
318       np2.getEnd().addNext(fon);
319     } else {
320       fsen.addNext(fon);
321     }
322     fon.addNext(fcomp);
323     return new NodePair(fln, fnop);
324   }
325
326   private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
327     TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
328     FlatNode first=null;
329     FlatNode last=null;
330     TempDescriptor thisarg=null;
331
332     if (min.getExpression()!=null) {
333       thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
334       NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
335       first=np.getBegin();
336       last=np.getEnd();
337     }
338
339     //Build arguments
340     for(int i=0; i<min.numArgs(); i++) {
341       ExpressionNode en=min.getArg(i);
342       TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
343       temps[i]=td;
344       NodePair np=flattenExpressionNode(en, td);
345       if (first==null)
346         first=np.getBegin();
347       else
348         last.addNext(np.getBegin());
349       last=np.getEnd();
350     }
351
352     MethodDescriptor md=min.getMethod();
353
354     //Call to constructor
355
356     FlatCall fc;
357     if(md.getReturnType()==null||md.getReturnType().isVoid())
358       fc=new FlatCall(md, null, thisarg, temps);
359     else
360       fc=new FlatCall(md, out_temp, thisarg, temps);
361     if (first==null) {
362       first=fc;
363     } else
364       last.addNext(fc);
365     return new NodePair(first,fc);
366   }
367
368   private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
369     TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
370     NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
371     FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
372     npe.getEnd().addNext(fn);
373     return new NodePair(npe.getBegin(),fn);
374   }
375
376   private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
377     TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
378     TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
379     NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
380     NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
381     FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
382     npe.getEnd().addNext(npi.getBegin());
383     npi.getEnd().addNext(fn);
384     return new NodePair(npe.getBegin(),fn);
385   }
386
387   private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
388     // Three cases:
389     // left side is variable
390     // left side is field
391     // left side is array
392
393     Operation base=an.getOperation().getBaseOp();
394     boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
395
396     if (!pre) {
397       //rewrite the base operation
398       base=base.getOp()==Operation.POSTINC ? new Operation(Operation.ADD) : new Operation(Operation.SUB);
399     }
400     FlatNode first=null;
401     FlatNode last=null;
402     TempDescriptor src_tmp = src_tmp=an.getSrc()==null ? TempDescriptor.tempFactory("srctmp",an.getDest().getType()) : TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
403
404     //Get src value
405     if (an.getSrc()!=null) {
406       NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
407       first=np_src.getBegin();
408       last=np_src.getEnd();
409     } else if (!pre) {
410       FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT),new Integer(1),src_tmp);
411       first=fln;
412       last=fln;
413     }
414
415     if (an.getDest().kind()==Kind.FieldAccessNode) {
416       //We are assigning an object field
417
418       FieldAccessNode fan=(FieldAccessNode)an.getDest();
419       ExpressionNode en=fan.getExpression();
420       TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
421       NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
422       if (first==null)
423         first=np_baseexp.getBegin();
424       else
425         last.addNext(np_baseexp.getBegin());
426       last=np_baseexp.getEnd();
427
428       //See if we need to perform an operation
429       if (base!=null) {
430         //If it is a preinc we need to store the initial value
431         TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
432         TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
433         FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
434         last.addNext(ffn);
435         last=ffn;
436
437         if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
438           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
439           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
440           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
441           src_tmp=tmp;
442           last.addNext(fc);
443           last=fc;
444         } else {
445           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
446           src_tmp=tmp;
447           last.addNext(fon);
448           last=fon;
449         }
450       }
451
452       FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
453       last.addNext(fsfn);
454       last=fsfn;
455       if (pre) {
456         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
457         fsfn.addNext(fon2);
458         last=fon2;
459       }
460       return new NodePair(first, last);
461     } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
462       //We are assigning an array element
463
464
465       ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
466       ExpressionNode en=aan.getExpression();
467       ExpressionNode enindex=aan.getIndex();
468       TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
469       TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
470       NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
471       NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
472       if (first==null)
473         first=np_baseexp.getBegin();
474       else
475         last.addNext(np_baseexp.getBegin());
476       np_baseexp.getEnd().addNext(np_indexexp.getBegin());
477       last=np_indexexp.getEnd();
478
479       //See if we need to perform an operation
480       if (base!=null) {
481         //If it is a preinc we need to store the initial value
482         TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
483         TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
484
485         FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
486         last.addNext(fen);
487         last=fen;
488
489         if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
490           ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
491           MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
492           FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
493           src_tmp=tmp;
494           last.addNext(fc);
495           last=fc;
496         } else {
497
498
499           FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
500           src_tmp=tmp;
501           last.addNext(fon);
502           last=fon;
503         }
504       }
505
506
507       FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
508       last.addNext(fsen);
509       last=fsen;
510       if (pre) {
511         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
512         fsen.addNext(fon2);
513         last=fon2;
514       }
515       return new NodePair(first, last);
516     } else if (an.getDest().kind()==Kind.NameNode) {
517       //We could be assigning a field or variable
518       NameNode nn=(NameNode)an.getDest();
519
520
521       if (nn.getExpression()!=null) {
522         //It is a field
523         FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
524         ExpressionNode en=fan.getExpression();
525         TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
526         NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
527         if (first==null)
528           first=np_baseexp.getBegin();
529         else
530           last.addNext(np_baseexp.getBegin());
531         last=np_baseexp.getEnd();
532
533         //See if we need to perform an operation
534         if (base!=null) {
535           //If it is a preinc we need to store the initial value
536           TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
537           TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
538
539           FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
540           last.addNext(ffn);
541           last=ffn;
542
543
544           if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
545             ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
546             MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
547             FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
548             src_tmp=tmp;
549             last.addNext(fc);
550             last=fc;
551           } else {
552             FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
553             src_tmp=tmp;
554             last.addNext(fon);
555             last=fon;
556           }
557         }
558
559
560         FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
561         last.addNext(fsfn);
562         last=fsfn;
563         if (pre) {
564           FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
565           fsfn.addNext(fon2);
566           last=fon2;
567         }
568         return new NodePair(first, last);
569       } else {
570         if (nn.getField()!=null) {
571           //It is a field
572           //Get src value
573
574           //See if we need to perform an operation
575           if (base!=null) {
576             //If it is a preinc we need to store the initial value
577             TempDescriptor src_tmp2=pre ? TempDescriptor.tempFactory("src",an.getDest().getType()) : out_temp;
578             TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
579
580             FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
581             if (first==null)
582               first=ffn;
583             else {
584               last.addNext(ffn);
585             }
586             last=ffn;
587
588
589             if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
590               ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
591               MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
592               FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
593               src_tmp=tmp;
594               last.addNext(fc);
595               last=fc;
596             } else {
597               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
598               src_tmp=tmp;
599               last.addNext(fon);
600               last=fon;
601             }
602           }
603
604           FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
605           if (first==null) {
606             first=fsfn;
607           } else {
608             last.addNext(fsfn);
609           }
610           last=fsfn;
611           if (pre) {
612             FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
613             fsfn.addNext(fon2);
614             last=fon2;
615           }
616           return new NodePair(first, last);
617         } else {
618           //It is a variable
619           //See if we need to perform an operation
620
621           if (base!=null) {
622             //If it is a preinc we need to store the initial value
623             TempDescriptor src_tmp2=getTempforVar(nn.getVar());
624             TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3_",an.getDest().getType());
625             if (!pre) {
626               FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
627               if (first==null)
628                 first=fon;
629               else
630                 last.addNext(fon);
631               last=fon;
632             }
633
634
635             if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) {
636               ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
637               MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)});
638               FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp});
639               if (first==null)
640                 first=fc;
641               else
642                 last.addNext(fc);
643               src_tmp=tmp;
644               last=fc;
645             } else {
646               FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
647               if (first==null)
648                 first=fon;
649               else
650                 last.addNext(fon);
651               src_tmp=tmp;
652               last=fon;
653             }
654           }
655
656           FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
657
658           last.addNext(fon);
659           last=fon;
660           if (pre) {
661             FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
662             fon.addNext(fon2);
663             last=fon2;
664           }
665           return new NodePair(first, last);
666         } //end of else
667       }
668     }
669
670     throw new Error();
671   }
672
673   private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
674     if (nn.getExpression()!=null) {
675       /* Hack - use subtree instead */
676       return flattenExpressionNode(nn.getExpression(),out_temp);
677     } else if (nn.getField()!=null) {
678       TempDescriptor tmp=getTempforVar(nn.getVar());
679       FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
680       return new NodePair(ffn,ffn);
681     } else {
682       TempDescriptor tmp=getTempforVar(nn.isTag() ? nn.getTagVar() : nn.getVar());
683       if (nn.isTag()) {
684         //propagate tag
685         out_temp.setTag(tmp.getTag());
686       }
687       FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
688       return new NodePair(fon,fon);
689     }
690   }
691
692   private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
693     TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
694     TempDescriptor temp_right=null;
695
696     Operation op=on.getOp();
697
698     NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
699     NodePair right;
700     if (on.getRight()!=null) {
701       temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
702       right=flattenExpressionNode(on.getRight(),temp_right);
703     } else {
704       FlatNop nop=new FlatNop();
705       right=new NodePair(nop,nop);
706     }
707
708     if (op.getOp()==Operation.LOGIC_OR) {
709       /* Need to do shortcircuiting */
710       FlatCondBranch fcb=new FlatCondBranch(temp_left);
711       FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
712       FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
713       FlatNop fnop=new FlatNop();
714       left.getEnd().addNext(fcb);
715       fcb.addFalseNext(right.getBegin());
716       right.getEnd().addNext(fon2);
717       fon2.addNext(fnop);
718       fcb.addTrueNext(fon1);
719       fon1.addNext(fnop);
720       return new NodePair(left.getBegin(), fnop);
721     } else if (op.getOp()==Operation.LOGIC_AND) {
722       /* Need to do shortcircuiting */
723       FlatCondBranch fcb=new FlatCondBranch(temp_left);
724       FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
725       FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
726       FlatNop fnop=new FlatNop();
727       left.getEnd().addNext(fcb);
728       fcb.addTrueNext(right.getBegin());
729       right.getEnd().addNext(fon2);
730       fon2.addNext(fnop);
731       fcb.addFalseNext(fon1);
732       fon1.addNext(fnop);
733       return new NodePair(left.getBegin(), fnop);
734     } else if (op.getOp()==Operation.ADD&&on.getLeft().getType().isString()) {
735       //We have a string concatenate
736       ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass);
737       MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat", new TypeDescriptor[] {new TypeDescriptor(stringcd)});
738       FlatCall fc=new FlatCall(concatmd, out_temp, temp_left, new TempDescriptor[] {temp_right});
739       left.getEnd().addNext(right.getBegin());
740       right.getEnd().addNext(fc);
741       return new NodePair(left.getBegin(), fc);
742     }
743
744     FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
745     left.getEnd().addNext(right.getBegin());
746     right.getEnd().addNext(fon);
747     return new NodePair(left.getBegin(),fon);
748   }
749
750   private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
751     switch(en.kind()) {
752     case Kind.AssignmentNode:
753       return flattenAssignmentNode((AssignmentNode)en,out_temp);
754
755     case Kind.CastNode:
756       return flattenCastNode((CastNode)en,out_temp);
757
758     case Kind.CreateObjectNode:
759       return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
760
761     case Kind.FieldAccessNode:
762       return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
763
764     case Kind.ArrayAccessNode:
765       return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
766
767     case Kind.LiteralNode:
768       return flattenLiteralNode((LiteralNode)en,out_temp);
769
770     case Kind.MethodInvokeNode:
771       return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
772
773     case Kind.NameNode:
774       return flattenNameNode((NameNode)en,out_temp);
775
776     case Kind.OpNode:
777       return flattenOpNode((OpNode)en,out_temp);
778
779     case Kind.OffsetNode:
780       return flattenOffsetNode((OffsetNode)en,out_temp);
781     }
782     throw new Error();
783   }
784
785   private NodePair flattenDeclarationNode(DeclarationNode dn) {
786     VarDescriptor vd=dn.getVarDescriptor();
787     TempDescriptor td=getTempforVar(vd);
788     if (dn.getExpression()!=null)
789       return flattenExpressionNode(dn.getExpression(),td);
790     else {
791       FlatNop fn=new FlatNop();
792       return new NodePair(fn,fn);
793     }
794   }
795
796   private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
797     TagVarDescriptor tvd=dn.getTagVarDescriptor();
798     TagDescriptor tag=tvd.getTag();
799     TempDescriptor tmp=getTempforVar(tvd);
800     FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
801     return new NodePair(ftd,ftd);
802   }
803
804   private TempDescriptor getTempforParam(Descriptor d) {
805     if (temptovar.containsKey(d))
806       return (TempDescriptor)temptovar.get(d);
807     else {
808       if (d instanceof VarDescriptor) {
809         VarDescriptor vd=(VarDescriptor)d;
810         TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
811         temptovar.put(vd,td);
812         return td;
813       } else if (d instanceof TagVarDescriptor) {
814         TagVarDescriptor tvd=(TagVarDescriptor)d;
815         TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
816         TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
817         temptovar.put(tvd,td);
818         return td;
819       } else throw new Error("Unreconized Descriptor");
820     }
821   }
822
823   private TempDescriptor getTempforVar(Descriptor d) {
824     if (temptovar.containsKey(d))
825       return (TempDescriptor)temptovar.get(d);
826     else {
827       if (d instanceof VarDescriptor) {
828         VarDescriptor vd=(VarDescriptor)d;
829         TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
830         temptovar.put(vd,td);
831         return td;
832       } else if (d instanceof TagVarDescriptor) {
833         TagVarDescriptor tvd=(TagVarDescriptor)d;
834         //BUGFIX TAGTYPE - add next line, modify following
835         //line to tag this new type descriptor, modify
836         //TempDescriptor constructor & factory to set type
837         //using this Type To test, use any program with tags
838         TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
839         TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
840         temptovar.put(tvd,td);
841         return td;
842       } else throw new Error("Unrecognized Descriptor");
843     }
844   }
845
846   private NodePair flattenIfStatementNode(IfStatementNode isn) {
847     TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
848     NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
849     FlatCondBranch fcb=new FlatCondBranch(cond_temp);
850     NodePair true_np=flattenBlockNode(isn.getTrueBlock());
851     NodePair false_np;
852     FlatNop nopend=new FlatNop();
853
854     if (isn.getFalseBlock()!=null)
855       false_np=flattenBlockNode(isn.getFalseBlock());
856     else {
857       FlatNop nop=new FlatNop();
858       false_np=new NodePair(nop,nop);
859     }
860
861     cond.getEnd().addNext(fcb);
862     fcb.addTrueNext(true_np.getBegin());
863     fcb.addFalseNext(false_np.getBegin());
864     true_np.getEnd().addNext(nopend);
865     false_np.getEnd().addNext(nopend);
866     return new NodePair(cond.getBegin(), nopend);
867   }
868
869   private NodePair flattenLoopNode(LoopNode ln) {
870     if (ln.getType()==LoopNode.FORLOOP) {
871       NodePair initializer=flattenBlockNode(ln.getInitializer());
872       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
873       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
874       NodePair update=flattenBlockNode(ln.getUpdate());
875       NodePair body=flattenBlockNode(ln.getBody());
876       FlatNode begin=initializer.getBegin();
877       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
878       fcb.setTrueProb(State.TRUEPROB);
879       fcb.setLoop();
880       FlatNop nopend=new FlatNop();
881       FlatBackEdge backedge=new FlatBackEdge();
882
883       FlatNop nop2=new FlatNop();
884       initializer.getEnd().addNext(nop2);
885       nop2.addNext(condition.getBegin());
886       body.getEnd().addNext(update.getBegin());
887       update.getEnd().addNext(backedge);
888       backedge.addNext(condition.getBegin());
889       condition.getEnd().addNext(fcb);
890       fcb.addFalseNext(nopend);
891       fcb.addTrueNext(body.getBegin());
892       return new NodePair(begin,nopend);
893     } else if (ln.getType()==LoopNode.WHILELOOP) {
894       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
895       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
896       NodePair body=flattenBlockNode(ln.getBody());
897       FlatNode begin=condition.getBegin();
898       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
899       fcb.setTrueProb(State.TRUEPROB);
900       fcb.setLoop();
901       FlatNop nopend=new FlatNop();
902       FlatBackEdge backedge=new FlatBackEdge();
903
904       body.getEnd().addNext(backedge);
905       backedge.addNext(condition.getBegin());
906
907       condition.getEnd().addNext(fcb);
908       fcb.addFalseNext(nopend);
909       fcb.addTrueNext(body.getBegin());
910       return new NodePair(begin,nopend);
911     } else if (ln.getType()==LoopNode.DOWHILELOOP) {
912       TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
913       NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
914       NodePair body=flattenBlockNode(ln.getBody());
915       FlatNode begin=body.getBegin();
916       FlatCondBranch fcb=new FlatCondBranch(cond_temp);
917       fcb.setTrueProb(State.TRUEPROB);
918       fcb.setLoop();
919       FlatNop nopend=new FlatNop();
920       FlatBackEdge backedge=new FlatBackEdge();
921
922       body.getEnd().addNext(condition.getBegin());
923       condition.getEnd().addNext(fcb);
924       fcb.addFalseNext(nopend);
925       fcb.addTrueNext(backedge);
926       backedge.addNext(body.getBegin());
927       return new NodePair(begin,nopend);
928     } else throw new Error();
929   }
930
931   private NodePair flattenReturnNode(ReturnNode rntree) {
932     TempDescriptor retval=null;
933     NodePair cond=null;
934     if (rntree.getReturnExpression()!=null) {
935       retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
936       cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
937     }
938
939     FlatReturnNode rnflat=new FlatReturnNode(retval);
940     FlatNode ln=rnflat;
941     if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
942       MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
943       TempDescriptor thistd=getTempforVar(currmd.getThis());
944       FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
945       fc.addNext(ln);
946       ln=fc;
947     }
948     if (state.DSM&&currmd.getModifiers().isAtomic()) {
949       FlatAtomicExitNode faen=new FlatAtomicExitNode(curran);
950       faen.addNext(ln);
951       ln=faen;
952     }
953
954     if (cond!=null) {
955       cond.getEnd().addNext(ln);
956       return new NodePair(cond.getBegin(),rnflat);
957     } else
958       return new NodePair(ln,rnflat);
959   }
960
961   private NodePair flattenTaskExitNode(TaskExitNode ten) {
962     FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
963     ffan.setTaskExitIndex(ten.getTaskExitIndex());
964     updateFlagActionNode(ffan, ten.getFlagEffects());
965     NodePair fcn=flattenConstraintCheck(ten.getChecks());
966     ffan.addNext(fcn.getBegin());
967     FlatReturnNode rnflat=new FlatReturnNode(null);
968     fcn.getEnd().addNext(rnflat);
969     return new NodePair(ffan, rnflat);
970   }
971
972   private NodePair flattenConstraintCheck(Vector ccs) {
973     FlatNode begin=new FlatNop();
974     if (ccs==null)
975       return new NodePair(begin,begin);
976     FlatNode last=begin;
977     for(int i=0; i<ccs.size(); i++) {
978       ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
979       /* Flatten the arguments */
980       TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
981       String[] vars=new String[cc.numArgs()];
982       for(int j=0; j<cc.numArgs(); j++) {
983         ExpressionNode en=cc.getArg(j);
984         TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
985         temps[j]=td;
986         vars[j]=cc.getVar(j);
987         NodePair np=flattenExpressionNode(en, td);
988         last.addNext(np.getBegin());
989         last=np.getEnd();
990       }
991
992       FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
993       last.addNext(fcn);
994       last=fcn;
995     }
996     return new NodePair(begin,last);
997   }
998
999   private NodePair flattenSubBlockNode(SubBlockNode sbn) {
1000     return flattenBlockNode(sbn.getBlockNode());
1001   }
1002
1003   private NodePair flattenAtomicNode(AtomicNode sbn) {
1004     NodePair np=flattenBlockNode(sbn.getBlockNode());
1005     FlatAtomicEnterNode faen=new FlatAtomicEnterNode();
1006     FlatAtomicExitNode faexn=new FlatAtomicExitNode(faen);
1007     faen.addNext(np.getBegin());
1008     np.getEnd().addNext(faexn);
1009     return new NodePair(faen, faexn);
1010   }
1011
1012   private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
1013     switch(bsn.kind()) {
1014     case Kind.BlockExpressionNode:
1015       return flattenBlockExpressionNode((BlockExpressionNode)bsn);
1016
1017     case Kind.DeclarationNode:
1018       return flattenDeclarationNode((DeclarationNode)bsn);
1019
1020     case Kind.TagDeclarationNode:
1021       return flattenTagDeclarationNode((TagDeclarationNode)bsn);
1022
1023     case Kind.IfStatementNode:
1024       return flattenIfStatementNode((IfStatementNode)bsn);
1025
1026     case Kind.LoopNode:
1027       return flattenLoopNode((LoopNode)bsn);
1028
1029     case Kind.ReturnNode:
1030       return flattenReturnNode((IR.Tree.ReturnNode)bsn);
1031
1032     case Kind.TaskExitNode:
1033       return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
1034
1035     case Kind.SubBlockNode:
1036       return flattenSubBlockNode((SubBlockNode)bsn);
1037
1038     case Kind.AtomicNode:
1039       return flattenAtomicNode((AtomicNode)bsn);
1040
1041     }
1042     throw new Error();
1043   }
1044 }