6 public class BuildFlat {
9 MethodDescriptor currmd;
12 public BuildFlat(State st, TypeUtil typeutil) {
14 temptovar=new Hashtable();
15 this.typeutil=typeutil;
18 public Hashtable getMap() {
22 public void buildFlat() {
23 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
25 ClassDescriptor cn=(ClassDescriptor)it.next();
29 Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();
30 while(task_it.hasNext()) {
31 TaskDescriptor td=(TaskDescriptor)task_it.next();
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);
42 FlatMethod fm=new FlatMethod(td, ffan);
44 HashSet visitedset=new HashSet();
46 for(int i=0;i<td.numParameters();i++) {
47 VarDescriptor paramvd=td.getParameter(i);
48 fm.addParameterTemp(getTempforVar(paramvd));
49 TagExpressionList tel=td.getTag(paramvd);
50 //BUG added next line to fix...to test feed in any task program
52 for(int j=0;j<tel.numTags();j++) {
53 TagVarDescriptor tvd=(TagVarDescriptor) td.getParameterTable().getFromSameScope(tel.getName(j));
54 TempDescriptor tagtmp=getTempforVar(tvd);
55 if (!visitedset.contains(tvd)) {
57 fm.addTagTemp(tagtmp);
59 tel.setTemp(j, tagtmp);
63 /* Flatten Vector of Flag Effects */
64 Vector flags=td.getFlagEffects();
65 updateFlagActionNode(ffan,flags);
67 state.addFlatCode(td,fm);
71 /* This method transforms a vector of FlagEffects into the FlatFlagActionNode */
72 private void updateFlagActionNode(FlatFlagActionNode ffan, Vector flags) {
73 if (flags==null) // Do nothing if the flag effects vector is empty
76 for(int i=0;i<flags.size();i++) {
77 FlagEffects fes=(FlagEffects)flags.get(i);
78 TempDescriptor flagtemp=getTempforVar(fes.getVar());
80 for(int j=0;j<fes.numEffects();j++) {
81 FlagEffect fe=fes.getEffect(j);
82 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
85 for(int j=0;j<fes.numTagEffects();j++) {
86 TagEffect te=fes.getTagEffect(j);
87 TempDescriptor tagtemp=getTempforVar(te.getTag());
89 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
94 private void flattenClass(ClassDescriptor cn) {
95 Iterator methodit=cn.getMethods();
96 while(methodit.hasNext()) {
97 currmd=(MethodDescriptor)methodit.next();
98 BlockNode bn=state.getMethodBody(currmd);
99 NodePair np=flattenBlockNode(bn);
100 FlatNode fn=np.getBegin();
101 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
102 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorEnter");
103 TempDescriptor thistd=getTempforVar(currmd.getThis());
104 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
107 if (np.getEnd().kind()!=FKind.FlatReturnNode) {
108 MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
109 FlatCall fcunlock=new FlatCall(memdex, null, thistd, new TempDescriptor[0]);
110 np.getEnd().addNext(fcunlock);
114 FlatMethod fm=new FlatMethod(currmd, fn);
115 if (!currmd.isStatic())
116 fm.addParameterTemp(getTempforParam(currmd.getThis()));
117 for(int i=0;i<currmd.numParameters();i++) {
118 fm.addParameterTemp(getTempforParam(currmd.getParameter(i)));
120 state.addFlatCode(currmd,fm);
124 private NodePair flattenBlockNode(BlockNode bn) {
127 for(int i=0;i<bn.size();i++) {
128 NodePair np=flattenBlockStatementNode(bn.get(i));
129 FlatNode np_begin=np.getBegin();
130 FlatNode np_end=np.getEnd();
137 end.addNext(np_begin);
142 end=begin=new FlatNop();
144 return new NodePair(begin,end);
147 private NodePair flattenBlockExpressionNode(BlockExpressionNode en) {
148 TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType());
149 return flattenExpressionNode(en.getExpression(),tmp);
152 private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) {
153 TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType());
154 NodePair np=flattenExpressionNode(cn.getExpression(), tmp);
155 FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp);
156 np.getEnd().addNext(fcn);
157 return new NodePair(np.getBegin(),fcn);
160 private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) {
161 FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp);
162 return new NodePair(fln,fln);
165 private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) {
166 TypeDescriptor td=con.getType();
168 FlatNew fn=new FlatNew(td, out_temp);
169 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
171 if (td.getClassDesc().hasFlags()) {
172 // if (con.getFlagEffects()!=null) {
173 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.NEWOBJECT);
174 FlagEffects fes=con.getFlagEffects();
175 TempDescriptor flagtemp=out_temp;
177 for(int j=0;j<fes.numEffects();j++) {
178 FlagEffect fe=fes.getEffect(j);
179 ffan.addFlagAction(flagtemp, fe.getFlag(), fe.getStatus());
181 for(int j=0;j<fes.numTagEffects();j++) {
182 TagEffect te=fes.getTagEffect(j);
183 TempDescriptor tagtemp=getTempforVar(te.getTag());
185 ffan.addTagAction(flagtemp, te.getTag().getTag(), tagtemp, te.getStatus());
188 ffan.addFlagAction(flagtemp, null, false);
194 for(int i=0;i<con.numArgs();i++) {
195 ExpressionNode en=con.getArg(i);
196 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
198 NodePair np=flattenExpressionNode(en, tmp);
199 last.addNext(np.getBegin());
202 MethodDescriptor md=con.getConstructor();
203 //Call to constructor
204 FlatCall fc=new FlatCall(md, null, out_temp, temps);
207 return new NodePair(fn,last);
211 TempDescriptor[] temps=new TempDescriptor[con.numArgs()];
212 for (int i=0;i<con.numArgs();i++) {
213 ExpressionNode en=con.getArg(i);
214 TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
216 NodePair np=flattenExpressionNode(en, tmp);
220 last.addNext(np.getBegin());
223 TempDescriptor tmp2=(i==0)?
225 TempDescriptor.tempFactory("arg",en.getType());
227 FlatNew fn=new FlatNew(td, out_temp, temps[0]);
229 if (temps.length>1) {
230 NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0);
231 fn.addNext(np.getBegin());
232 return new NodePair(first,np.getEnd());
234 return new NodePair(first, fn);
238 private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) {
239 TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
240 TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT));
241 FlatNop fnop=new FlatNop();//last node
244 FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index);
246 FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone);
248 TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN));
250 FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT));
251 FlatCondBranch fcb=new FlatCondBranch(tmpbool);
253 TempDescriptor new_tmp=TempDescriptor.tempFactory("tmp",td);
254 FlatNew fn=new FlatNew(td, new_tmp, temparray[i+1]);
255 FlatSetElementNode fsen=new FlatSetElementNode(tmp,index,new_tmp);
257 FlatOpNode fon=new FlatOpNode(index,index,tmpone,new Operation(Operation.ADD));
263 fcb.addFalseNext(fnop);
265 //Recursive call here
266 if ((i+2)<temparray.length) {
267 NodePair np2=generateNewArrayLoop(temparray, td.dereference(), new_tmp, i+1);
268 fsen.addNext(np2.getBegin());
269 np2.getEnd().addNext(fon);
274 return new NodePair(fln, fnop);
277 private NodePair flattenMethodInvokeNode(MethodInvokeNode min,TempDescriptor out_temp) {
278 TempDescriptor[] temps=new TempDescriptor[min.numArgs()];
281 TempDescriptor thisarg=null;
283 if (min.getExpression()!=null) {
284 thisarg=TempDescriptor.tempFactory("thisarg",min.getExpression().getType());
285 NodePair np=flattenExpressionNode(min.getExpression(),thisarg);
291 for(int i=0;i<min.numArgs();i++) {
292 ExpressionNode en=min.getArg(i);
293 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
295 NodePair np=flattenExpressionNode(en, td);
299 last.addNext(np.getBegin());
303 MethodDescriptor md=min.getMethod();
305 //Call to constructor
308 if(md.getReturnType()==null||md.getReturnType().isVoid())
309 fc=new FlatCall(md, null, thisarg, temps);
311 fc=new FlatCall(md, out_temp, thisarg, temps);
316 return new NodePair(first,fc);
319 private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) {
320 TempDescriptor tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType());
321 NodePair npe=flattenExpressionNode(fan.getExpression(),tmp);
322 FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp);
323 npe.getEnd().addNext(fn);
324 return new NodePair(npe.getBegin(),fn);
327 private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) {
328 TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType());
329 TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType());
330 NodePair npe=flattenExpressionNode(aan.getExpression(),tmp);
331 NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex);
332 FlatElementNode fn=new FlatElementNode(tmp,tmpindex,out_temp);
333 npe.getEnd().addNext(npi.getBegin());
334 npi.getEnd().addNext(fn);
335 return new NodePair(npe.getBegin(),fn);
338 private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) {
340 // left side is variable
341 // left side is field
342 // left side is array
344 Operation base=an.getOperation().getBaseOp();
345 boolean pre=base==null||(base.getOp()!=Operation.POSTINC&&base.getOp()!=Operation.POSTDEC);
348 //rewrite the base operation
349 base=base.getOp()==Operation.POSTINC?new Operation(Operation.ADD):new Operation(Operation.SUB);
353 TempDescriptor src_tmp=an.getSrc()==null?TempDescriptor.tempFactory("srctmp",an.getDest().getType()):TempDescriptor.tempFactory("srctmp",an.getSrc().getType());
356 if (an.getSrc()!=null) {
357 NodePair np_src=flattenExpressionNode(an.getSrc(),src_tmp);
358 first=np_src.getBegin();
359 last=np_src.getEnd();
361 FlatLiteralNode fln=new FlatLiteralNode(new TypeDescriptor(TypeDescriptor.INT) ,new Integer(1),src_tmp);
366 if (an.getDest().kind()==Kind.FieldAccessNode) {
367 //We are assigning an object field
369 FieldAccessNode fan=(FieldAccessNode)an.getDest();
370 ExpressionNode en=fan.getExpression();
371 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
372 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
374 first=np_baseexp.getBegin();
376 last.addNext(np_baseexp.getBegin());
377 last=np_baseexp.getEnd();
379 //See if we need to perform an operation
381 //If it is a preinc we need to store the initial value
382 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
383 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
385 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
388 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
394 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
398 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
402 return new NodePair(first, last);
403 } else if (an.getDest().kind()==Kind.ArrayAccessNode) {
404 //We are assigning an array element
407 ArrayAccessNode aan=(ArrayAccessNode)an.getDest();
408 ExpressionNode en=aan.getExpression();
409 ExpressionNode enindex=aan.getIndex();
410 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
411 TempDescriptor index_tmp=TempDescriptor.tempFactory("index",enindex.getType());
412 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
413 NodePair np_indexexp=flattenExpressionNode(enindex, index_tmp);
415 first=np_baseexp.getBegin();
417 last.addNext(np_baseexp.getBegin());
418 np_baseexp.getEnd().addNext(np_indexexp.getBegin());
419 last=np_indexexp.getEnd();
421 //See if we need to perform an operation
423 //If it is a preinc we need to store the initial value
424 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
425 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
427 FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2);
430 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
437 FlatSetElementNode fsen=new FlatSetElementNode(dst_tmp, index_tmp, src_tmp);
441 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
445 return new NodePair(first, last);
446 } else if (an.getDest().kind()==Kind.NameNode) {
447 //We could be assigning a field or variable
448 NameNode nn=(NameNode)an.getDest();
449 if (nn.getExpression()!=null) {
451 FieldAccessNode fan=(FieldAccessNode)nn.getExpression();
452 ExpressionNode en=fan.getExpression();
453 TempDescriptor dst_tmp=TempDescriptor.tempFactory("dst",en.getType());
454 NodePair np_baseexp=flattenExpressionNode(en, dst_tmp);
456 first=np_baseexp.getBegin();
458 last.addNext(np_baseexp.getBegin());
459 last=np_baseexp.getEnd();
461 //See if we need to perform an operation
463 //If it is a preinc we need to store the initial value
464 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
465 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
467 FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2);
470 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
477 FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp);
481 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
485 return new NodePair(first, last);
487 if (nn.getField()!=null) {
491 //See if we need to perform an operation
493 //If it is a preinc we need to store the initial value
494 TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp;
495 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
497 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), getTempforVar(nn.getVar()), src_tmp2);
504 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
510 FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp);
518 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
522 return new NodePair(first, last);
525 //See if we need to perform an operation
528 //If it is a preinc we need to store the initial value
529 TempDescriptor src_tmp2=getTempforVar(nn.getVar());
530 TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType());
532 FlatOpNode fon=new FlatOpNode(out_temp, src_tmp2, null, new Operation(Operation.ASSIGN));
540 FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base);
549 FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN));
553 FlatOpNode fon2=new FlatOpNode(out_temp, src_tmp, null, new Operation(Operation.ASSIGN));
557 return new NodePair(first, last);
564 private NodePair flattenNameNode(NameNode nn,TempDescriptor out_temp) {
565 if (nn.getExpression()!=null) {
566 /* Hack - use subtree instead */
567 return flattenExpressionNode(nn.getExpression(),out_temp);
568 } else if (nn.getField()!=null) {
569 TempDescriptor tmp=getTempforVar(nn.getVar());
570 FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp);
571 return new NodePair(ffn,ffn);
573 TempDescriptor tmp=getTempforVar(nn.getVar());
574 FlatOpNode fon=new FlatOpNode(out_temp, tmp, null, new Operation(Operation.ASSIGN));
575 return new NodePair(fon,fon);
579 private NodePair flattenOpNode(OpNode on,TempDescriptor out_temp) {
580 TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
581 TempDescriptor temp_right=null;
583 Operation op=on.getOp();
584 /* We've moved this to assignment nodes
586 if (op.getOp()==Operation.POSTINC||
587 op.getOp()==Operation.POSTDEC||
588 op.getOp()==Operation.PREINC||
589 op.getOp()==Operation.PREDEC) {
590 LiteralNode ln=new LiteralNode("int",new Integer(1));
591 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
593 AssignmentNode an=new AssignmentNode(on.getLeft(),
594 new OpNode(on.getLeft(),ln,
595 new Operation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?Operation.PLUS:Operation.MINUS))
597 if (op.getOp()==Operation.POSTINC||
598 op.getOp()==Operation.POSTDEC) {
599 //Can't do, this could have side effects
600 NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
601 NodePair assign=flattenAssignmentNode(an,temp_left);
602 left.getEnd().addNext(assign.getBegin());
603 return new NodePair(left.getBegin(),assign.getEnd());
605 NodePair assign=flattenAssignmentNode(an,out_temp);
610 NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
612 if (on.getRight()!=null) {
613 temp_right=TempDescriptor.tempFactory("rightop",on.getRight().getType());
614 right=flattenExpressionNode(on.getRight(),temp_right);
616 FlatNop nop=new FlatNop();
617 right=new NodePair(nop,nop);
620 if (op.getOp()==Operation.LOGIC_OR) {
621 /* Need to do shortcircuiting */
622 FlatCondBranch fcb=new FlatCondBranch(temp_left);
623 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
624 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
625 FlatNop fnop=new FlatNop();
626 left.getEnd().addNext(fcb);
627 fcb.addFalseNext(right.getBegin());
628 right.getEnd().addNext(fon2);
630 fcb.addTrueNext(fon1);
632 return new NodePair(left.getBegin(), fnop);
633 } else if (op.getOp()==Operation.LOGIC_AND) {
634 /* Need to do shortcircuiting */
635 FlatCondBranch fcb=new FlatCondBranch(temp_left);
636 FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
637 FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
638 FlatNop fnop=new FlatNop();
639 left.getEnd().addNext(fcb);
640 fcb.addTrueNext(right.getBegin());
641 right.getEnd().addNext(fon2);
643 fcb.addFalseNext(fon1);
645 return new NodePair(left.getBegin(), fnop);
648 FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
649 left.getEnd().addNext(right.getBegin());
650 right.getEnd().addNext(fon);
651 return new NodePair(left.getBegin(),fon);
654 private NodePair flattenExpressionNode(ExpressionNode en, TempDescriptor out_temp) {
656 case Kind.AssignmentNode:
657 return flattenAssignmentNode((AssignmentNode)en,out_temp);
659 return flattenCastNode((CastNode)en,out_temp);
660 case Kind.CreateObjectNode:
661 return flattenCreateObjectNode((CreateObjectNode)en,out_temp);
662 case Kind.FieldAccessNode:
663 return flattenFieldAccessNode((FieldAccessNode)en,out_temp);
664 case Kind.ArrayAccessNode:
665 return flattenArrayAccessNode((ArrayAccessNode)en,out_temp);
666 case Kind.LiteralNode:
667 return flattenLiteralNode((LiteralNode)en,out_temp);
668 case Kind.MethodInvokeNode:
669 return flattenMethodInvokeNode((MethodInvokeNode)en,out_temp);
671 return flattenNameNode((NameNode)en,out_temp);
673 return flattenOpNode((OpNode)en,out_temp);
678 private NodePair flattenDeclarationNode(DeclarationNode dn) {
679 VarDescriptor vd=dn.getVarDescriptor();
680 TempDescriptor td=getTempforVar(vd);
681 if (dn.getExpression()!=null)
682 return flattenExpressionNode(dn.getExpression(),td);
684 FlatNop fn=new FlatNop();
685 return new NodePair(fn,fn);
689 private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) {
690 TagVarDescriptor tvd=dn.getTagVarDescriptor();
691 TagDescriptor tag=tvd.getTag();
692 TempDescriptor tmp=getTempforVar(tvd);
693 FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp);
694 return new NodePair(ftd,ftd);
697 private TempDescriptor getTempforParam(Descriptor d) {
698 if (temptovar.containsKey(d))
699 return (TempDescriptor)temptovar.get(d);
701 if (d instanceof VarDescriptor) {
702 VarDescriptor vd=(VarDescriptor)d;
703 TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType());
704 temptovar.put(vd,td);
706 } else if (d instanceof TagVarDescriptor) {
707 TagVarDescriptor tvd=(TagVarDescriptor)d;
708 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
709 TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag());
710 temptovar.put(tvd,td);
712 } else throw new Error("Unreconized Descriptor");
716 private TempDescriptor getTempforVar(Descriptor d) {
717 if (temptovar.containsKey(d))
718 return (TempDescriptor)temptovar.get(d);
720 if (d instanceof VarDescriptor) {
721 VarDescriptor vd=(VarDescriptor)d;
722 TempDescriptor td=TempDescriptor.tempFactory(vd.getName(), vd.getType());
723 temptovar.put(vd,td);
725 } else if (d instanceof TagVarDescriptor) {
726 TagVarDescriptor tvd=(TagVarDescriptor)d;
727 //BUGFIX TAGTYPE - add next line, modify following
728 //line to tag this new type descriptor, modify
729 //TempDescriptor constructor & factory to set type
730 //using this Type To test, use any program with tags
731 TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass));
732 TempDescriptor td=TempDescriptor.tempFactory(tvd.getName(),tagtype, tvd.getTag());
733 temptovar.put(tvd,td);
735 } else throw new Error("Unrecognized Descriptor");
739 private NodePair flattenIfStatementNode(IfStatementNode isn) {
740 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.BOOLEAN));
741 NodePair cond=flattenExpressionNode(isn.getCondition(),cond_temp);
742 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
743 NodePair true_np=flattenBlockNode(isn.getTrueBlock());
745 FlatNop nopend=new FlatNop();
747 if (isn.getFalseBlock()!=null)
748 false_np=flattenBlockNode(isn.getFalseBlock());
750 FlatNop nop=new FlatNop();
751 false_np=new NodePair(nop,nop);
754 cond.getEnd().addNext(fcb);
755 fcb.addTrueNext(true_np.getBegin());
756 fcb.addFalseNext(false_np.getBegin());
757 true_np.getEnd().addNext(nopend);
758 false_np.getEnd().addNext(nopend);
759 return new NodePair(cond.getBegin(), nopend);
762 private NodePair flattenLoopNode(LoopNode ln) {
763 if (ln.getType()==LoopNode.FORLOOP) {
764 NodePair initializer=flattenBlockNode(ln.getInitializer());
765 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
766 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
767 NodePair update=flattenBlockNode(ln.getUpdate());
768 NodePair body=flattenBlockNode(ln.getBody());
769 FlatNode begin=initializer.getBegin();
770 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
771 FlatNop nopend=new FlatNop();
772 FlatBackEdge backedge=new FlatBackEdge();
774 initializer.getEnd().addNext(condition.getBegin());
775 body.getEnd().addNext(update.getBegin());
776 update.getEnd().addNext(backedge);
777 backedge.addNext(condition.getBegin());
778 condition.getEnd().addNext(fcb);
779 fcb.addFalseNext(nopend);
780 fcb.addTrueNext(body.getBegin());
781 return new NodePair(begin,nopend);
782 } else if (ln.getType()==LoopNode.WHILELOOP) {
783 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
784 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
785 NodePair body=flattenBlockNode(ln.getBody());
786 FlatNode begin=condition.getBegin();
787 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
788 FlatNop nopend=new FlatNop();
789 FlatBackEdge backedge=new FlatBackEdge();
791 body.getEnd().addNext(backedge);
792 backedge.addNext(condition.getBegin());
794 condition.getEnd().addNext(fcb);
795 fcb.addFalseNext(nopend);
796 fcb.addTrueNext(body.getBegin());
797 return new NodePair(begin,nopend);
798 } else if (ln.getType()==LoopNode.DOWHILELOOP) {
799 TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
800 NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
801 NodePair body=flattenBlockNode(ln.getBody());
802 FlatNode begin=body.getBegin();
803 FlatCondBranch fcb=new FlatCondBranch(cond_temp);
804 FlatNop nopend=new FlatNop();
805 FlatBackEdge backedge=new FlatBackEdge();
807 body.getEnd().addNext(condition.getBegin());
808 condition.getEnd().addNext(fcb);
809 fcb.addFalseNext(nopend);
810 fcb.addTrueNext(backedge);
811 backedge.addNext(body.getBegin());
812 return new NodePair(begin,nopend);
813 } else throw new Error();
816 private NodePair flattenReturnNode(ReturnNode rntree) {
817 TempDescriptor retval=null;
819 if (rntree.getReturnExpression()!=null) {
820 retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
821 cond=flattenExpressionNode(rntree.getReturnExpression(),retval);
824 FlatReturnNode rnflat=new FlatReturnNode(retval);
826 if (state.THREAD&&currmd.getModifiers().isSynchronized()) {
827 MethodDescriptor memd=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit");
828 TempDescriptor thistd=getTempforVar(currmd.getThis());
829 FlatCall fc=new FlatCall(memd, null, thistd, new TempDescriptor[0]);
835 cond.getEnd().addNext(ln);
836 return new NodePair(cond.getBegin(),rnflat);
838 return new NodePair(ln,rnflat);
841 private NodePair flattenTaskExitNode(TaskExitNode ten) {
842 FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT);
843 updateFlagActionNode(ffan, ten.getFlagEffects());
844 NodePair fcn=flattenConstraintCheck(ten.getChecks());
845 ffan.addNext(fcn.getBegin());
846 FlatReturnNode rnflat=new FlatReturnNode(null);
847 fcn.getEnd().addNext(rnflat);
848 return new NodePair(ffan, rnflat);
851 private NodePair flattenConstraintCheck(Vector ccs) {
852 FlatNode begin=new FlatNop();
854 return new NodePair(begin,begin);
856 for(int i=0;i<ccs.size();i++) {
857 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
858 /* Flatten the arguments */
859 TempDescriptor[] temps=new TempDescriptor[cc.numArgs()];
860 String[] vars=new String[cc.numArgs()];
861 for(int j=0;j<cc.numArgs();j++) {
862 ExpressionNode en=cc.getArg(j);
863 TempDescriptor td=TempDescriptor.tempFactory("arg",en.getType());
865 vars[j]=cc.getVar(j);
866 NodePair np=flattenExpressionNode(en, td);
867 last.addNext(np.getBegin());
871 FlatCheckNode fcn=new FlatCheckNode(cc.getSpec(), vars, temps);
875 return new NodePair(begin,last);
878 private NodePair flattenSubBlockNode(SubBlockNode sbn) {
879 return flattenBlockNode(sbn.getBlockNode());
882 private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
884 case Kind.BlockExpressionNode:
885 return flattenBlockExpressionNode((BlockExpressionNode)bsn);
887 case Kind.DeclarationNode:
888 return flattenDeclarationNode((DeclarationNode)bsn);
890 case Kind.TagDeclarationNode:
891 return flattenTagDeclarationNode((TagDeclarationNode)bsn);
893 case Kind.IfStatementNode:
894 return flattenIfStatementNode((IfStatementNode)bsn);
897 return flattenLoopNode((LoopNode)bsn);
899 case Kind.ReturnNode:
900 return flattenReturnNode((IR.Tree.ReturnNode)bsn);
902 case Kind.TaskExitNode:
903 return flattenTaskExitNode((IR.Tree.TaskExitNode)bsn);
905 case Kind.SubBlockNode:
906 return flattenSubBlockNode((SubBlockNode)bsn);