adding thread support w/ locks
[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         FlatNode fn=flattenBlockNode(bn).getBegin();
39         FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE);
40         ffan.addNext(fn);
41         FlatMethod fm=new FlatMethod(td, ffan);
42
43         for(int i=0;i<td.numParameters();i++) {
44             fm.addParameterTemp(getTempforVar(td.getParameter(i)));
45         }
46
47         /* Flatten Vector of Flag Effects */
48         Vector flags=td.getFlagEffects();
49         updateFlagActionNode(ffan,flags);
50
51         state.addFlatCode(td,fm);
52     }
53
54
55     /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
56     private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
57         if (flags==null) // Do nothing if the flag effects vector is empty
58             return;
59
60         for(int i=0;i<flags.size();i++) {
61             FlagEffects fes=(FlagEffects)flags.get(i);
62             TempDescriptor flagtemp=getTempforVar(fes.getVar());
63             for(int j=0;j<fes.numEffects();j++) {
64                 FlagEffect fe=fes.getEffect(j);
65                 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
66             }
67         }
68     }
69
70     private void flattenClass(ClassDescriptor cn) {
71         Iterator methodit=cn.getMethods();
72         while(methodit.hasNext()) {
73             currmd=(MethodDescriptor)methodit.next();
74             BlockNode bn=state.getMethodBody(currmd);
75             NodePair np=flattenBlockNode(bn);
76             FlatNode fn=np.getBegin();
77             if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
78                 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
79                 TempDescriptor thistd=getTempforVar(currmd.getThis());
80                 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
81                 fc.addNext(fn);
82                 fn=fc;
83                 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
84                     MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
85                     FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
86                     np.getEnd().addNext(fcunlock);
87                 }
88             }
89
90             FlatMethod fm=new FlatMethod(currmd, fn);
91             if (!currmd.isStatic())
92                 fm.addParameterTemp(getTempforParam(currmd.getThis()));
93             for(int i=0;i<currmd.numParameters();i++) {
94                 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
95             }
96             state.addFlatCode(currmd,fm);
97         }
98     }
99
100     private NodePair flattenBlockNode(BlockNode bn) {
101         FlatNode begin=null;
102         FlatNode end=null;
103         for(int i=0;i<bn.size();i++) {
104             NodePair np=flattenBlockStatementNode(bn.get(i));
105             FlatNode np_begin=np.getBegin();
106             FlatNode np_end=np.getEnd();
107             if (begin==null) {
108                 begin=np_begin;
109             }
110             if (end==null) {
111                 end=np_end;
112             } else {
113                 end.addNext(np_begin);
114                 end=np_end;
115             }
116         }
117         if (begin==null) {
118             end=begin=new FlatNop();
119         }
120         return new NodePair(begin,end);
121     }
122
123     private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
124         TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
125         return flattenExpressionNode(en.getExpression(),tmp);
126     }
127
128     private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
129         TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
130         NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
131         FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
132         np.getEnd().addNext(fcn);
133         return new NodePair(np.getBegin(),fcn);
134     }
135
136     private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
137         FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
138         return new NodePair(fln,fln);
139     }
140
141     private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
142         TypeDescriptor td=con.getType();
143         if (!td.isArray()) {
144             FlatNew fn=new FlatNew(td, out_temp);
145             TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
146             FlatNode last=fn;
147
148             if (con.getFlagEffects()!=null) {
149                 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
150                 FlagEffects fes=con.getFlagEffects();
151                 TempDescriptor flagtemp=out_temp;
152                 for(int j=0;j<fes.numEffects();j++) {
153                     FlagEffect fe=fes.getEffect(j);
154                     ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
155                 }
156                 last.addNext(ffan);
157                 last=ffan;
158             }
159             //Build arguments
160             for(int i=0;i<con.numArgs();i++) {
161                 ExpressionNode en=con.getArg(i);
162                 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
163                 temps[i]=tmp;
164                 NodePair np=flattenExpressionNode(en, tmp);
165                 last.addNext(np.getBegin());
166                 last=np.getEnd();
167             }
168             MethodDescriptor md=con.getConstructor();
169             //Call to constructor
170             FlatCall fc=new FlatCall(md, null, out_temp, temps);
171             last.addNext(fc);
172             last=fc;
173             return new NodePair(fn,last); 
174         } else {
175             FlatNode first=null;
176             FlatNode last=null;
177             TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
178             for (int i=0;i<con.numArgs();i++) {
179                 ExpressionNode en=con.getArg(i);
180                 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
181                 temps[i]=tmp;           
182                 NodePair np=flattenExpressionNode(en, tmp);
183                 if (first==null)
184                     first=np.getBegin();
185                 else
186                     last.addNext(np.getBegin());
187                 last=np.getEnd();
188                 
189                 TempDescriptor tmp2=(i==0)?
190                     out_temp:
191                 TempDescriptor.tempFactory("arg",en.getType());
192             }
193             FlatNew fn=new FlatNew(td, out_temp, temps[0]);
194             last.addNext(fn);
195             if (temps.length>1) {
196                 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
197                 fn.addNext(np.getBegin());
198                 return new NodePair(first,np.getEnd()); 
199             } else
200                 return new NodePair(first, fn);
201         }
202     }
203
204     private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
205         TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
206         TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
207         FlatNop fnop=new FlatNop();//last node
208
209         //index=0
210         FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
211         //tmpone=1
212         FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
213
214         TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
215
216         FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
217         FlatCondBranch fcb=new FlatCondBranch(tmpbool);
218         //is index<temp[i]
219         TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
220         FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
221         FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
222         // index=index+1
223         FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
224         //jump out
225         fln.addNext(fln2);
226         fln2.addNext(fcomp);
227         fcomp.addNext(fcb);
228         fcb.addTrueNext(fn);
229         fcb.addFalseNext(fnop);
230         fn.addNext(fsen);
231         //Recursive call here
232         if ((i+2)<temparray.length) {
233             NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
234             fsen.addNext(np2.getBegin());
235             np2.getEnd().addNext(fon);
236         } else {
237             fsen.addNext(fon);
238         }
239         fon.addNext(fcomp);
240         return new NodePair(fln, fnop);
241     }
242
243     private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
244         TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
245         FlatNode first=null;
246         FlatNode last=null;
247         TempDescriptor thisarg=null;
248
249         if (min.getExpression()!=null) {
250             thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
251             NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
252             first=np.getBegin();
253             last=np.getEnd();
254         }
255         
256         //Build arguments
257         for(int i=0;i<min.numArgs();i++) {
258             ExpressionNode en=min.getArg(i);
259             TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
260             temps[i]=td;
261             NodePair np=flattenExpressionNode(en, td);
262             if (first==null)
263                 first=np.getBegin();
264             else 
265                 last.addNext(np.getBegin());
266             last=np.getEnd();
267         }
268
269         MethodDescriptor md=min.getMethod();
270         
271         //Call to constructor
272         
273         FlatCall fc;
274         if(md.getReturnType()==null||md.getReturnType().isVoid())
275             fc=new FlatCall(md, null, thisarg, temps);
276         else 
277             fc=new FlatCall(md, out_temp, thisarg, temps);
278         if (first==null) {
279             first=fc;
280         } else
281             last.addNext(fc);
282         return new NodePair(first,fc);
283     }
284
285     private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
286         TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
287         NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
288         FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
289         npe.getEnd().addNext(fn);
290         return new NodePair(npe.getBegin(),fn);
291     }
292
293     private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
294         TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
295         TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
296         NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
297         NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
298         FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
299         npe.getEnd().addNext(npi.getBegin());
300         npi.getEnd().addNext(fn);
301         return new NodePair(npe.getBegin(),fn);
302     }
303
304     private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
305         // Three cases:
306         // left side is variable
307         // left side is field
308         // left side is array
309         
310         Operation base=an.getOperation().getBaseOp();
311         boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
312         
313         if (!pre) {
314             //rewrite the base operation
315             base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
316         }
317         FlatNode first=null;
318         FlatNode last=null;
319         TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
320
321         //Get src value
322         if (an.getSrc()!=null) {
323             NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
324             first=np_src.getBegin();
325             last=np_src.getEnd();
326         } else if (!pre) {
327             FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
328             first=fln;
329             last=fln;
330         }
331         
332         if (an.getDest().kind()==Kind.FieldAccessNode) {
333             //We are assigning an object field
334
335             FieldAccessNode fan=(FieldAccessNode)an.getDest();
336             ExpressionNode en=fan.getExpression();
337             TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
338             NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
339             if (first==null)
340                 first=np_baseexp.getBegin();
341             else
342                 last.addNext(np_baseexp.getBegin());
343             last=np_baseexp.getEnd();
344
345             //See if we need to perform an operation
346             if (base!=null) {
347                 //If it is a preinc we need to store the initial value
348                 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
349                 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
350
351                 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
352                 last.addNext(ffn);
353                 last=ffn;
354                 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
355                 src_tmp=tmp;
356                 last.addNext(fon);
357                 last=fon;
358             }
359
360             FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
361             last.addNext(fsfn);
362             last=fsfn;
363             if (pre) {
364                 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
365                 fsfn.addNext(fon2);
366                 last=fon2;
367             }
368             return new NodePair(first, last);
369         } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
370             //We are assigning an array element
371
372
373             ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
374             ExpressionNode en=aan.getExpression();
375             ExpressionNode enindex=aan.getIndex();
376             TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
377             TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
378             NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
379             NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
380             if (first==null)
381                 first=np_baseexp.getBegin();
382             else
383                 last.addNext(np_baseexp.getBegin());
384             np_baseexp.getEnd().addNext(np_indexexp.getBegin());
385             last=np_indexexp.getEnd();
386
387             //See if we need to perform an operation
388             if (base!=null) {
389                 //If it is a preinc we need to store the initial value
390                 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
391                 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
392
393                 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
394                 last.addNext(fen);
395                 last=fen;
396                 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
397                 src_tmp=tmp;
398                 last.addNext(fon);
399                 last=fon;
400             }
401
402
403             FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
404             last.addNext(fsen);
405             last=fsen;
406             if (pre) {
407                 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
408                 fsen.addNext(fon2);
409                 last=fon2;
410             }
411             return new NodePair(first, last);
412         } else if (an.getDest().kind()==Kind.NameNode) {
413             //We could be assigning a field or variable
414             NameNode nn=(NameNode)an.getDest();
415             if (nn.getExpression()!=null) {
416                 //It is a field
417                 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
418                 ExpressionNode en=fan.getExpression();
419                 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
420                 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
421                 if (first==null)
422                     first=np_baseexp.getBegin();
423                 else
424                     last.addNext(np_baseexp.getBegin());
425                 last=np_baseexp.getEnd();
426
427                 //See if we need to perform an operation
428                 if (base!=null) {
429                     //If it is a preinc we need to store the initial value
430                     TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
431                     TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
432                     
433                     FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
434                     last.addNext(ffn);
435                     last=ffn;
436                     FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
437                     src_tmp=tmp;
438                     last.addNext(fon);
439                     last=fon;
440                 }
441
442
443                 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
444                 last.addNext(fsfn);
445                 last=fsfn;
446                 if (pre) {
447                     FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
448                     fsfn.addNext(fon2);
449                     last=fon2;
450                 }
451                 return new NodePair(first, last);
452             } else {
453                 if (nn.getField()!=null) {
454                     //It is a field
455                     //Get src value
456
457                     //See if we need to perform an operation
458                     if (base!=null) {
459                         //If it is a preinc we need to store the initial value
460                         TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
461                         TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
462                         
463                         FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
464                         if (first==null)
465                             first=ffn;
466                         else {
467                             last.addNext(ffn);
468                         }
469                         last=ffn;
470                         FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
471                         src_tmp=tmp;
472                         last.addNext(fon);
473                         last=fon;
474                     }               
475
476                     FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
477                     if (first==null) {
478                         first=fsfn;
479                     } else {
480                         last.addNext(fsfn);
481                     }
482                     last=fsfn;
483                     if (pre) {
484                         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
485                         fsfn.addNext(fon2);
486                         last=fon2;
487                     }
488                     return new NodePair(first, last);
489                 } else {
490                     //It is a variable
491                     //See if we need to perform an operation
492
493                     if (base!=null) {
494                         //If it is a preinc we need to store the initial value
495                         TempDescriptor src_tmp2=getTempforVar(nn.getVar());
496                         TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
497                         if (!pre) {
498                             FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
499                             if (first==null)
500                                 first=fon;
501                             else
502                                 last.addNext(fon);
503                             last=fon;
504                         }
505
506                         FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
507                         if (first==null) 
508                             first=fon;
509                         else 
510                             last.addNext(fon);
511                         src_tmp=tmp;
512                         last=fon;
513                     }
514
515                     FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
516                     last.addNext(fon);
517                     last=fon;
518                     if (pre) {
519                         FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
520                         fon.addNext(fon2);
521                         last=fon2;
522                     }
523                     return new NodePair(first, last);
524                 }
525             }
526         } 
527         throw new Error();
528     }
529
530     private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
531         if (nn.getExpression()!=null) {
532             /* Hack - use subtree instead */
533             return flattenExpressionNode(nn.getExpression(),out_temp);
534         } else if (nn.getField()!=null) {
535             TempDescriptor tmp=getTempforVar(nn.getVar());
536             FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp); 
537             return new NodePair(ffn,ffn);
538         } else {
539             TempDescriptor tmp=getTempforVar(nn.getVar());
540             FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
541             return new NodePair(fon,fon);
542         }
543     }
544
545     private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
546         TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
547         TempDescriptor temp_right=null;
548
549         Operation op=on.getOp();
550         /* We've moved this to assignment nodes
551
552         if (op.getOp()==Operation.POSTINC||
553             op.getOp()==Operation.POSTDEC||
554             op.getOp()==Operation.PREINC||
555             op.getOp()==Operation.PREDEC) {
556             LiteralNode ln=new LiteralNode("int",new Integer(1));
557             ln.setType(new TypeDescriptor(TypeDescriptor.INT));
558             
559             AssignmentNode an=new AssignmentNode(on.getLeft(),
560                                                  new OpNode(on.getLeft(),ln, 
561                                                             new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
562                                                  );
563             if (op.getOp()==Operation.POSTINC||
564                 op.getOp()==Operation.POSTDEC) {
565                 //Can't do, this could have side effects
566                 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
567                 NodePair assign=flattenAssignmentNode(an,temp_left);
568                 left.getEnd().addNext(assign.getBegin());
569                 return new NodePair(left.getBegin(),assign.getEnd());
570             } else {
571                 NodePair assign=flattenAssignmentNode(an,out_temp);
572                 return assign;
573             }
574             } */
575         
576         NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
577         NodePair right;
578         if (on.getRight()!=null) {
579             temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
580             right=flattenExpressionNode(on.getRight(),temp_right);
581         } else {
582             FlatNop nop=new FlatNop();
583             right=new NodePair(nop,nop);
584         }
585
586         if (op.getOp()==Operation.LOGIC_OR) {
587             /* Need to do shortcircuiting */
588             FlatCondBranch fcb=new FlatCondBranch(temp_left);
589             FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
590             FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
591             FlatNop fnop=new FlatNop();
592             left.getEnd().addNext(fcb);
593             fcb.addFalseNext(right.getBegin());
594             right.getEnd().addNext(fon2);
595             fon2.addNext(fnop);
596             fcb.addTrueNext(fon1);
597             fon1.addNext(fnop);
598             return new NodePair(left.getBegin(), fnop);
599         } else if (op.getOp()==Operation.LOGIC_AND) {
600             /* Need to do shortcircuiting */
601             FlatCondBranch fcb=new FlatCondBranch(temp_left);
602             FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
603             FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
604             FlatNop fnop=new FlatNop();
605             left.getEnd().addNext(fcb);
606             fcb.addTrueNext(right.getBegin());
607             right.getEnd().addNext(fon2);
608             fon2.addNext(fnop);
609             fcb.addFalseNext(fon1);
610             fon1.addNext(fnop);
611             return new NodePair(left.getBegin(), fnop);
612         }
613
614         FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
615         left.getEnd().addNext(right.getBegin());
616         right.getEnd().addNext(fon);
617         return new NodePair(left.getBegin(),fon);
618     }
619
620     private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
621         switch(en.kind()) {
622         case Kind.AssignmentNode:
623             return flattenAssignmentNode((AssignmentNode)en,out_temp);
624         case Kind.CastNode:
625             return flattenCastNode((CastNode)en,out_temp);
626         case Kind.CreateObjectNode:
627             return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
628         case Kind.FieldAccessNode:
629             return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
630         case Kind.ArrayAccessNode:
631             return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
632         case Kind.LiteralNode:
633             return flattenLiteralNode((LiteralNode)en,out_temp);
634         case Kind.MethodInvokeNode:
635             return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
636         case Kind.NameNode:
637             return flattenNameNode((NameNode)en,out_temp);
638         case Kind.OpNode:
639             return flattenOpNode((OpNode)en,out_temp);
640         }
641         throw new Error();
642     }
643
644     private NodePair flattenDeclarationNode(DeclarationNode dn) {
645         VarDescriptor vd=dn.getVarDescriptor();
646         TempDescriptor td=getTempforVar(vd);
647         if (dn.getExpression()!=null)
648             return flattenExpressionNode(dn.getExpression(),td);
649         else {
650             FlatNop fn=new FlatNop();
651             return new NodePair(fn,fn);
652         }
653     }
654
655     private TempDescriptor getTempforParam(VarDescriptor vd) {
656         if (temptovar.containsKey(vd))
657             return (TempDescriptor)temptovar.get(vd);
658         else {
659             TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
660             temptovar.put(vd,td);
661             return td;
662         }
663     }
664
665     private TempDescriptor getTempforVar(VarDescriptor vd) {
666         if (temptovar.containsKey(vd))
667             return (TempDescriptor)temptovar.get(vd);
668         else {
669             TempDescriptor td=TempDescriptor.tempFactory(vd.getName(),vd.getType());
670             temptovar.put(vd,td);
671             return td;
672         }
673     }
674
675     private NodePair flattenIfStatementNode(IfStatementNode isn) {
676         TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
677         NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
678         FlatCondBranch fcb=new FlatCondBranch(cond_temp);
679         NodePair true_np=flattenBlockNode(isn.getTrueBlock());
680         NodePair false_np;
681         FlatNop nopend=new FlatNop();
682
683         if (isn.getFalseBlock()!=null)
684             false_np=flattenBlockNode(isn.getFalseBlock());
685         else {
686             FlatNop nop=new FlatNop();
687             false_np=new NodePair(nop,nop);
688         }
689
690         cond.getEnd().addNext(fcb);
691         fcb.addTrueNext(true_np.getBegin());
692         fcb.addFalseNext(false_np.getBegin());
693         true_np.getEnd().addNext(nopend);
694         false_np.getEnd().addNext(nopend);
695         return new NodePair(cond.getBegin(), nopend);
696     }
697             
698     private NodePair flattenLoopNode(LoopNode ln) {
699         if (ln.getType()==LoopNode.FORLOOP) {
700             NodePair initializer=flattenBlockNode(ln.getInitializer());
701             TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
702             NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
703             NodePair update=flattenBlockNode(ln.getUpdate());
704             NodePair body=flattenBlockNode(ln.getBody());
705             FlatNode begin=initializer.getBegin();
706             FlatCondBranch fcb=new FlatCondBranch(cond_temp);
707             FlatNop nopend=new FlatNop();
708             FlatBackEdge backedge=new FlatBackEdge();
709
710             initializer.getEnd().addNext(condition.getBegin());
711             body.getEnd().addNext(update.getBegin());
712             update.getEnd().addNext(backedge);
713             backedge.addNext(condition.getBegin());
714             condition.getEnd().addNext(fcb);
715             fcb.addFalseNext(nopend);
716             fcb.addTrueNext(body.getBegin());
717             return new NodePair(begin,nopend);
718         } else if (ln.getType()==LoopNode.WHILELOOP) {
719             TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
720             NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
721             NodePair body=flattenBlockNode(ln.getBody());
722             FlatNode begin=condition.getBegin();
723             FlatCondBranch fcb=new FlatCondBranch(cond_temp);
724             FlatNop nopend=new FlatNop();
725             FlatBackEdge backedge=new FlatBackEdge();
726
727             body.getEnd().addNext(backedge);
728             backedge.addNext(condition.getBegin());
729
730             condition.getEnd().addNext(fcb);
731             fcb.addFalseNext(nopend);
732             fcb.addTrueNext(body.getBegin());
733             return new NodePair(begin,nopend);
734         } else if (ln.getType()==LoopNode.DOWHILELOOP) {
735             TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
736             NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
737             NodePair body=flattenBlockNode(ln.getBody());
738             FlatNode begin=body.getBegin();
739             FlatCondBranch fcb=new FlatCondBranch(cond_temp);
740             FlatNop nopend=new FlatNop();
741             FlatBackEdge backedge=new FlatBackEdge();
742
743             body.getEnd().addNext(condition.getBegin());
744             condition.getEnd().addNext(fcb);
745             fcb.addFalseNext(nopend);
746             fcb.addTrueNext(backedge);
747             backedge.addNext(body.getBegin());
748             return new NodePair(begin,nopend);
749         } else throw new Error();
750     }
751             
752     private NodePair flattenReturnNode(ReturnNode rntree) {
753         TempDescriptor retval=null;
754         NodePair cond=null;
755         if (rntree.getReturnExpression()!=null) {
756             retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
757             cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
758         }
759
760         FlatReturnNode rnflat=new FlatReturnNode(retval);
761         FlatNode ln=rnflat;
762         if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
763             MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
764             TempDescriptor thistd=getTempforVar(currmd.getThis());
765             FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
766             fc.addNext(rnflat);
767             ln=fc;
768         }
769
770         if (cond!=null) {
771             cond.getEnd().addNext(ln);
772             return new NodePair(cond.getBegin(),rnflat);
773         } else
774             return new NodePair(ln,rnflat);
775     }
776
777     private NodePair flattenTaskExitNode(TaskExitNode ten) {
778         FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
779         updateFlagActionNode(ffan, ten.getFlagEffects());
780         NodePair fcn=flattenConstraintCheck(ten.getChecks());
781         ffan.addNext(fcn.getBegin());
782         FlatReturnNode rnflat=new FlatReturnNode(null);
783         fcn.getEnd().addNext(rnflat);
784         return new NodePair(ffan, rnflat);
785     }
786
787     private NodePair flattenConstraintCheck(Vector ccs) {
788         FlatNode begin=new FlatNop();
789         if (ccs==null)
790             return new NodePair(begin,begin);
791         FlatNode last=begin;
792         for(int i=0;i<ccs.size();i++) {
793             ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
794             /* Flatten the arguments */
795             TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
796             String[] vars=new String[cc.numArgs()];
797             for(int j=0;j<cc.numArgs();j++) {
798                 ExpressionNode en=cc.getArg(j);
799                 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
800                 temps[j]=td;
801                 vars[j]=cc.getVar(j);
802                 NodePair np=flattenExpressionNode(en, td);
803                 last.addNext(np.getBegin());
804                 last=np.getEnd();
805             }
806             
807             FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
808             last.addNext(fcn);
809             last=fcn;
810         }
811         return new NodePair(begin,last);
812     }
813
814     private NodePair flattenSubBlockNode(SubBlockNode sbn) {
815         return flattenBlockNode(sbn.getBlockNode());
816     }
817
818     private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
819         switch(bsn.kind()) {
820         case Kind.BlockExpressionNode:
821             return flattenBlockExpressionNode((BlockExpressionNode)bsn);
822             
823         case Kind.DeclarationNode:
824             return flattenDeclarationNode((DeclarationNode)bsn);
825             
826         case Kind.IfStatementNode:
827             return flattenIfStatementNode((IfStatementNode)bsn);
828             
829         case Kind.LoopNode:
830             return flattenLoopNode((LoopNode)bsn);
831             
832         case Kind.ReturnNode:
833             return flattenReturnNode((IR.Tree.ReturnNode)bsn);
834
835         case Kind.TaskExitNode:
836             return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
837             
838         case Kind.SubBlockNode:
839             return flattenSubBlockNode((SubBlockNode)bsn);
840             
841         }
842         throw new Error();
843     }
844 }