X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=Robust%2Fsrc%2FIR%2FFlat%2FBuildFlat.java;h=148f089a0b678b9fe10b54b34717e6b0c4be07ed;hb=ab6d009292bac1b2eb2102c3c1926368496a3faa;hp=be64f97a34e62fc28afb2e04a923950f0a0c7834;hpb=51f40170bab3a0b2bb4869579233ac0f22729dcf;p=IRC.git diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index be64f97a..148f089a 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -1,605 +1,1681 @@ package IR.Flat; import IR.*; import IR.Tree.*; + import java.util.*; public class BuildFlat { - State state; - Hashtable temptovar; + State state; + Hashtable temptovar; + MethodDescriptor currmd; + TypeUtil typeutil; + + HashSet breakset; + HashSet continueset; + FlatExit fe; + + // for synchronized blocks + Stack lockStack; + + public BuildFlat(State st, TypeUtil typeutil) { + state=st; + temptovar=new Hashtable(); + this.typeutil=typeutil; + this.breakset=new HashSet(); + this.continueset=new HashSet(); + this.lockStack = new Stack(); + } + + public Hashtable getMap() { + return temptovar; + } - public BuildFlat(State st) { - state=st; - temptovar=new Hashtable(); + public void buildFlat() { + Iterator it=state.getClassSymbolTable().getDescriptorsIterator(); + while(it.hasNext()) { + ClassDescriptor cn=(ClassDescriptor)it.next(); + flattenClass(cn); } - public Hashtable getMap() { - return temptovar; + Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator(); + while(task_it.hasNext()) { + TaskDescriptor td=(TaskDescriptor)task_it.next(); + flattenTask(td); } + } - public void buildFlat() { - Iterator it=state.getClassSymbolTable().getDescriptorsIterator(); - while(it.hasNext()) { - ClassDescriptor cn=(ClassDescriptor)it.next(); - flattenClass(cn); - } - - Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator(); - while(task_it.hasNext()) { - TaskDescriptor td=(TaskDescriptor)task_it.next(); - flattenTask(td); - } + private void flattenTask(TaskDescriptor td) { + BlockNode bn=state.getMethodBody(td); + NodePair np=flattenBlockNode(bn); + FlatNode fn=np.getBegin(); + fe=new FlatExit(); + FlatNode fn2=np.getEnd(); + + if (fn2!=null&& fn2.kind()!=FKind.FlatReturnNode) { + FlatReturnNode rnflat=new FlatReturnNode(null); + rnflat.addNext(fe); + fn2.addNext(rnflat); } - - private void flattenTask(TaskDescriptor td) { - BlockNode bn=state.getMethodBody(td); - FlatNode fn=flattenBlockNode(bn).getBegin(); - FlatFlagActionNode ffan=new FlatFlagActionNode(false); - ffan.addNext(fn); - FlatMethod fm=new FlatMethod(td, ffan); - - for(int i=0;i inside flattenBlockExpressionNode\n"); + TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType()); + return flattenExpressionNode(en.getExpression(),tmp); + } + + private NodePair flattenCastNode(CastNode cn,TempDescriptor out_temp) { + TempDescriptor tmp=TempDescriptor.tempFactory("tocast",cn.getExpression().getType()); + NodePair np=flattenExpressionNode(cn.getExpression(), tmp); + FlatCastNode fcn=new FlatCastNode(cn.getType(), tmp, out_temp); + fcn.setNumLine(cn.getNumLine()); + np.getEnd().addNext(fcn); + return new NodePair(np.getBegin(),fcn); + } + + private NodePair flattenLiteralNode(LiteralNode ln,TempDescriptor out_temp) { + FlatLiteralNode fln=new FlatLiteralNode(ln.getType(), ln.getValue(), out_temp); + fln.setNumLine(ln.getNumLine()); + return new NodePair(fln,fln); + } + + private NodePair flattenOffsetNode(OffsetNode ofn, TempDescriptor out_temp) { + FlatOffsetNode fln = new FlatOffsetNode(ofn.getClassType(), ofn.getField(), out_temp); + fln.setNumLine(ofn.getNumLine()); + return new NodePair(fln, fln); + } + + private NodePair flattenCreateObjectNode(CreateObjectNode con,TempDescriptor out_temp) { + TypeDescriptor td=con.getType(); + if (!td.isArray()) { + FlatNode fn=new FlatNew(td, out_temp, con.isGlobal(), con.getDisjointId()); + FlatNode last=fn; + //handle wrapper fields + ClassDescriptor cd=td.getClassDesc(); + for(Iterator fieldit=cd.getFields();fieldit.hasNext();) { + FieldDescriptor fd=(FieldDescriptor)fieldit.next(); + if (fd.getType().iswrapper()) { + TempDescriptor wrap_tmp=TempDescriptor.tempFactory("wrapper_obj",fd.getType()); + FlatNode fnwrapper=new FlatNew(fd.getType(), wrap_tmp, con.isGlobal()); + fnwrapper.setNumLine(con.getNumLine()); + FlatSetFieldNode fsfn=new FlatSetFieldNode(out_temp, fd, wrap_tmp); + fsfn.setNumLine(con.getNumLine()); + last.addNext(fnwrapper); + fnwrapper.addNext(fsfn); + last=fsfn; } - if (begin==null) { - end=begin=new FlatNop(); + } + + TempDescriptor[] temps=new TempDescriptor[con.numArgs()]; + + // Build arguments + for(int i=0; i1) { + NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal()); + fn.addNext(np.getBegin()); + return new NodePair(first,np.getEnd()); + } else if (td.isArray()&&td.dereference().iswrapper()) { + NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0, con.isGlobal()); + fn.addNext(np.getBegin()); + return new NodePair(first,np.getEnd()); + } else + return new NodePair(first, fn); + } else if(state.MGC) { + // array creation with initializers + return flattenArrayInitializerNode(con.getArrayInitializer(), out_temp); + } + return null; } + } + + private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i, boolean isglobal) { + TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT)); + TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT)); + FlatNop fnop=new FlatNop(); //last node - private NodePair flattenBlockExpressionNode(BlockExpressionNode en) { - TempDescriptor tmp=TempDescriptor.tempFactory("neverused",en.getExpression().getType()); - return flattenExpressionNode(en.getExpression(),tmp); + //index=0 + FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index); + //tmpone=1 + FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone); + + TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN)); + + FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT)); + FlatCondBranch fcb=new FlatCondBranch(tmpbool); + fcb.setTrueProb(State.TRUEPROB); + fcb.setLoop(); + //is index1) { - NodePair np=generateNewArrayLoop(temps, td.dereference(), out_temp, 0); - fn.addNext(np.getBegin()); - return new NodePair(first,np.getEnd()); - } else - return new NodePair(first, fn); - } + //Call to constructor + + FlatCall fc; + if(md.getReturnType()==null||md.getReturnType().isVoid()) + fc=new FlatCall(md, null, thisarg, temps); + else + fc=new FlatCall(md, out_temp, thisarg, temps); + + fc.setNumLine(min.getNumLine()); + + if (first==null) { + first=fc; + } else + last.addNext(fc); + return new NodePair(first,fc); + } + + private NodePair flattenFieldAccessNode(FieldAccessNode fan,TempDescriptor out_temp) { + TempDescriptor tmp=null; + if(fan.getExpression().getType().isClassNameRef()) { + // static field dereference with class name + tmp = new TempDescriptor(fan.getExpression().getType().getClassDesc().getSymbol(), fan.getExpression().getType()); + FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp); + fn.setNumLine(fan.getNumLine()); + return new NodePair(fn,fn); + } else { + tmp=TempDescriptor.tempFactory("temp",fan.getExpression().getType()); + NodePair npe=flattenExpressionNode(fan.getExpression(),tmp); + FlatFieldNode fn=new FlatFieldNode(fan.getField(),tmp,out_temp); + fn.setNumLine(fan.getNumLine()); + npe.getEnd().addNext(fn); + return new NodePair(npe.getBegin(),fn); } + } + + private NodePair flattenArrayAccessNode(ArrayAccessNode aan,TempDescriptor out_temp) { + TempDescriptor tmp=TempDescriptor.tempFactory("temp",aan.getExpression().getType()); + TempDescriptor tmpindex=TempDescriptor.tempFactory("temp",aan.getIndex().getType()); + NodePair npe=flattenExpressionNode(aan.getExpression(),tmp); + NodePair npi=flattenExpressionNode(aan.getIndex(),tmpindex); + TempDescriptor arraytmp=out_temp; + if (aan.iswrapper()) { + //have wrapper + arraytmp=TempDescriptor.tempFactory("temp", aan.getExpression().getType().dereference()); + } + FlatNode fn=new FlatElementNode(tmp,tmpindex,arraytmp); + fn.setNumLine(aan.getNumLine()); + npe.getEnd().addNext(npi.getBegin()); + npi.getEnd().addNext(fn); + if (aan.iswrapper()) { + FlatFieldNode ffn=new FlatFieldNode((FieldDescriptor)aan.getExpression().getType().dereference().getClassDesc().getFieldTable().get("value") ,arraytmp,out_temp); + ffn.setNumLine(aan.getNumLine()); + fn.addNext(ffn); + fn=ffn; + } + return new NodePair(npe.getBegin(),fn); + } + + private NodePair flattenAssignmentNode(AssignmentNode an,TempDescriptor out_temp) { + // Three cases: + // left side is variable + // left side is field + // left side is array - private NodePair generateNewArrayLoop(TempDescriptor[] temparray, TypeDescriptor td, TempDescriptor tmp, int i) { - TempDescriptor index=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT)); - TempDescriptor tmpone=TempDescriptor.tempFactory("index",new TypeDescriptor(TypeDescriptor.INT)); - FlatNop fnop=new FlatNop();//last node - - //index=0 - FlatLiteralNode fln=new FlatLiteralNode(index.getType(),new Integer(0),index); - //tmpone=1 - FlatLiteralNode fln2=new FlatLiteralNode(tmpone.getType(),new Integer(1),tmpone); - - TempDescriptor tmpbool=TempDescriptor.tempFactory("comp",new TypeDescriptor(TypeDescriptor.BOOLEAN)); - - FlatOpNode fcomp=new FlatOpNode(tmpbool,index,temparray[i],new Operation(Operation.LT)); - FlatCondBranch fcb=new FlatCondBranch(tmpbool); - //is index slnv = sbn.getSwitchConditions(); + FlatNode cond_begin = null; + NodePair prev_fnp = null; + for(int j = 0; j < slnv.size(); j++) { + SwitchLabelNode sln = slnv.elementAt(j); + NodePair left = null; + NodePair false_np = null; + if(sln.isDefault()) { + left = body; + } else { + TempDescriptor cond_tmp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN)); + TempDescriptor temp_left=TempDescriptor.tempFactory("leftop", sln.getCondition().getType()); + Operation op=new Operation(Operation.EQUAL); + left=flattenExpressionNode(sln.getCondition(), temp_left); + FlatOpNode fon=new FlatOpNode(cond_tmp, temp_left, cond_temp, op); + fon.setNumLine(sln.getNumLine()); + left.getEnd().addNext(fon); + + FlatCondBranch fcb=new FlatCondBranch(cond_tmp); + fcb.setNumLine(bn.getNumLine()); + fcb.setTrueProb(State.TRUEPROB); + + FlatNop nop=new FlatNop(); + false_np=new NodePair(nop,nop); + + fon.addNext(fcb); + fcb.addTrueNext(body.getBegin()); + fcb.addFalseNext(false_np.getBegin()); + } + if((prev_fnp != null) && (prev_fnp.getEnd() != null)) { + prev_fnp.getEnd().addNext(left.getBegin()); + } + prev_fnp = false_np; + + if (begin==null) { + begin = left.getBegin(); + } + if(cond_begin == null) { + cond_begin = left.getBegin(); + } + } + if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) { + prev_false_branch.getEnd().addNext(cond_begin); + } + prev_false_branch = prev_fnp; + if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) { + prev_true_branch.getEnd().addNext(body.getBegin()); + } + prev_true_branch = body; + for(Iterator breakit=breakset.iterator();breakit.hasNext();) { + FlatNode fn=(FlatNode)breakit.next(); + breakit.remove(); + fn.addNext(endnode); + } + breakset=oldbs; + } + if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) { + prev_true_branch.getEnd().addNext(endnode); + } + if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) { + prev_false_branch.getEnd().addNext(endnode); + } + if(begin == null) { + end=begin=new FlatNop(); + } + return new NodePair(begin,end); + } + + private NodePair flattenLoopNode(LoopNode ln) { + HashSet oldbs=breakset; + HashSet oldcs=continueset; + breakset=new HashSet(); + continueset=new HashSet(); + + if (ln.getType()==LoopNode.FORLOOP) { + NodePair initializer=flattenBlockNode(ln.getInitializer()); + TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN)); + NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp); + NodePair update=flattenBlockNode(ln.getUpdate()); + NodePair body=flattenBlockNode(ln.getBody()); + FlatNode begin=initializer.getBegin(); + FlatCondBranch fcb=new FlatCondBranch(cond_temp); + fcb.setNumLine(ln.getNumLine()); + fcb.setTrueProb(State.TRUEPROB); + fcb.setLoop(); + FlatNop nopend=new FlatNop(); + FlatBackEdge backedge=new FlatBackEdge(); + + FlatNop nop2=new FlatNop(); + initializer.getEnd().addNext(nop2); + nop2.addNext(condition.getBegin()); + if (body.getEnd()!=null) + body.getEnd().addNext(update.getBegin()); + update.getEnd().addNext(backedge); + backedge.addNext(condition.getBegin()); + condition.getEnd().addNext(fcb); + fcb.addFalseNext(nopend); + fcb.addTrueNext(body.getBegin()); + for(Iterator contit=continueset.iterator();contit.hasNext();) { + FlatNode fn=(FlatNode)contit.next(); + contit.remove(); + fn.addNext(update.getBegin()); + } + for(Iterator breakit=breakset.iterator();breakit.hasNext();) { + FlatNode fn=(FlatNode)breakit.next(); + breakit.remove(); + fn.addNext(nopend); + } + breakset=oldbs; + continueset=oldcs; + return new NodePair(begin,nopend); + } else if (ln.getType()==LoopNode.WHILELOOP) { + TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN)); + NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp); + NodePair body=flattenBlockNode(ln.getBody()); + FlatNode begin=condition.getBegin(); + FlatCondBranch fcb=new FlatCondBranch(cond_temp); + fcb.setNumLine(ln.getNumLine()); + fcb.setTrueProb(State.TRUEPROB); + fcb.setLoop(); + FlatNop nopend=new FlatNop(); + FlatBackEdge backedge=new FlatBackEdge(); + + if (body.getEnd()!=null) + body.getEnd().addNext(backedge); + backedge.addNext(condition.getBegin()); + + condition.getEnd().addNext(fcb); + fcb.addFalseNext(nopend); + fcb.addTrueNext(body.getBegin()); + + for(Iterator contit=continueset.iterator();contit.hasNext();) { + FlatNode fn=(FlatNode)contit.next(); + contit.remove(); + fn.addNext(backedge); + } + for(Iterator breakit=breakset.iterator();breakit.hasNext();) { + FlatNode fn=(FlatNode)breakit.next(); + breakit.remove(); + fn.addNext(nopend); + } + breakset=oldbs; + continueset=oldcs; + return new NodePair(begin,nopend); + } else if (ln.getType()==LoopNode.DOWHILELOOP) { + TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN)); + NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp); + NodePair body=flattenBlockNode(ln.getBody()); + FlatNode begin=body.getBegin(); + FlatCondBranch fcb=new FlatCondBranch(cond_temp); + fcb.setNumLine(ln.getNumLine()); + fcb.setTrueProb(State.TRUEPROB); + fcb.setLoop(); + FlatNop nopend=new FlatNop(); + FlatBackEdge backedge=new FlatBackEdge(); + + if (body.getEnd()!=null) + body.getEnd().addNext(condition.getBegin()); + condition.getEnd().addNext(fcb); + fcb.addFalseNext(nopend); + fcb.addTrueNext(backedge); + backedge.addNext(body.getBegin()); + + for(Iterator contit=continueset.iterator();contit.hasNext();) { + FlatNode fn=(FlatNode)contit.next(); + contit.remove(); + fn.addNext(condition.getBegin()); + } + for(Iterator breakit=breakset.iterator();breakit.hasNext();) { + FlatNode fn=(FlatNode)breakit.next(); + breakit.remove(); + fn.addNext(nopend); + } + breakset=oldbs; + continueset=oldcs; + return new NodePair(begin,nopend); + } else throw new Error(); + } + + private NodePair flattenReturnNode(ReturnNode rntree) { + TempDescriptor retval=null; + NodePair cond=null; + if (rntree.getReturnExpression()!=null) { + retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType()); + cond=flattenExpressionNode(rntree.getReturnExpression(),retval); + } + + FlatReturnNode rnflat=new FlatReturnNode(retval); + rnflat.setNumLine(rntree.getNumLine()); + rnflat.addNext(fe); + FlatNode ln=rnflat; + if ((state.THREAD||state.MGC)&&!this.lockStack.isEmpty()) { + FlatNode end = null; + MethodDescriptor memdex=(MethodDescriptor)typeutil.getClass("Object").getMethodTable().get("MonitorExit"); + for(int j = this.lockStack.size(); j > 0; j--) { + TempDescriptor thistd = this.lockStack.elementAt(j-1); + FlatCall fcunlock = new FlatCall(memdex, null, thistd, new TempDescriptor[0]); + fcunlock.setNumLine(rntree.getNumLine()); + if(end != null) { + end.addNext(fcunlock); + } + end = fcunlock; + } + end.addNext(ln); + ln=end; + } + if (state.DSM&&currmd.getModifiers().isAtomic()) { + FlatAtomicExitNode faen=new FlatAtomicExitNode(curran); + faen.addNext(ln); + ln=faen; + } + + if (cond!=null) { + cond.getEnd().addNext(ln); + return new NodePair(cond.getBegin(),null); + } else + return new NodePair(ln,null); + } + + private NodePair flattenTaskExitNode(TaskExitNode ten) { + FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.TASKEXIT); + ffan.setTaskExitIndex(ten.getTaskExitIndex()); + updateFlagActionNode(ffan, ten.getFlagEffects()); + NodePair fcn=flattenConstraintCheck(ten.getChecks()); + ffan.addNext(fcn.getBegin()); + FlatReturnNode rnflat=new FlatReturnNode(null); + rnflat.setNumLine(ten.getNumLine()); + rnflat.addNext(fe); + fcn.getEnd().addNext(rnflat); + return new NodePair(ffan, null); + } + + private NodePair flattenConstraintCheck(Vector ccs) { + FlatNode begin=new FlatNop(); + if (ccs==null) + return new NodePair(begin,begin); + FlatNode last=begin; + for(int i=0; i