X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=Robust%2Fsrc%2FIR%2FFlat%2FBuildFlat.java;h=148f089a0b678b9fe10b54b34717e6b0c4be07ed;hb=ab6d009292bac1b2eb2102c3c1926368496a3faa;hp=af56b60afb12f051be320e2b35b7c26c0b69c00b;hpb=81c1c424bfd5abd9ced14e9b75ba8251219e89b5;p=IRC.git diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index af56b60a..148f089a 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -1,1008 +1,1681 @@ package IR.Flat; import IR.*; import IR.Tree.*; + import java.util.*; public class BuildFlat { - State state; - Hashtable temptovar; - MethodDescriptor currmd; - TypeUtil typeutil; + 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 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); + } - public BuildFlat(State st, TypeUtil typeutil) { - state=st; - temptovar=new Hashtable(); - this.typeutil=typeutil; + FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE); + ffan.setNumLine(bn.getNumLine()); + ffan.addNext(fn); + + FlatMethod fm=new FlatMethod(td, fe); + fm.addNext(ffan); + + Hashtable visitedset=new Hashtable(); + + 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; } - - FlatFlagActionNode ffan=new FlatFlagActionNode(FlatFlagActionNode.PRE); - ffan.addNext(fn); - - FlatMethod fm=new FlatMethod(td); - fm.addNext(ffan); - - Hashtable visitedset=new Hashtable(); - - 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 + + //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, con.isGlobal()); - fn.addNext(np.getBegin()); - return new NodePair(first,np.getEnd()); - } else - return new NodePair(first, fn); + FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2); + fen.setNumLine(aan.getNumLine()); + last.addNext(fen); + last=fen; } - } - - 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 - - //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(0.8); - //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); } - private NodePair flattenTagDeclarationNode(TagDeclarationNode dn) { - TagVarDescriptor tvd=dn.getTagVarDescriptor(); - TagDescriptor tag=tvd.getTag(); - TempDescriptor tmp=getTempforVar(tvd); - FlatTagDeclaration ftd=new FlatTagDeclaration(tag, tmp); - return new NodePair(ftd,ftd); - } - - private TempDescriptor getTempforParam(Descriptor d) { - if (temptovar.containsKey(d)) - return (TempDescriptor)temptovar.get(d); - else { - if (d instanceof VarDescriptor) { - VarDescriptor vd=(VarDescriptor)d; - TempDescriptor td=TempDescriptor.paramtempFactory(vd.getName(),vd.getType()); - temptovar.put(vd,td); - return td; - } else if (d instanceof TagVarDescriptor) { - TagVarDescriptor tvd=(TagVarDescriptor)d; - TypeDescriptor tagtype=new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass)); - TempDescriptor td=TempDescriptor.paramtempFactory(tvd.getName(), tagtype, tvd.getTag()); - temptovar.put(tvd,td); - return td; - } else throw new Error("Unreconized Descriptor"); - } + 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