From: yeom Date: Thu, 22 Mar 2012 18:44:29 +0000 (+0000) Subject: changes on inference X-Git-Url: http://plrg.eecs.uci.edu/git/?p=IRC.git;a=commitdiff_plain;h=7bc8bfe860ceea4a366d0e35998ce5386b8226c3 changes on inference --- diff --git a/Robust/src/Analysis/SSJava/InferGraph.java b/Robust/src/Analysis/SSJava/InferGraph.java new file mode 100644 index 00000000..e49b62d8 --- /dev/null +++ b/Robust/src/Analysis/SSJava/InferGraph.java @@ -0,0 +1,71 @@ +package Analysis.SSJava; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import IR.Descriptor; + +public class InferGraph { + + Set nodeSet; + + // having one location for the top location + private static final int topLocationID = 1; + + // unique ID seed + private static int uniqueID = 10000; + + // maps descriptors (field and local var descriptors) to its unique integer id + Map mapDescriptorToUniqueID; + + // maps field/var descriptros to infer nodes + Map mapDescToInferNode; + + boolean debug = true; + + public InferGraph() { + nodeSet = new HashSet(); + mapDescToInferNode = new HashMap(); + mapDescriptorToUniqueID = new HashMap(); + } + + public void addValueFlowEdge(Descriptor fromDesc, Descriptor toDesc) { + + } + + public InferNode getInferNode(Descriptor desc) { + if (mapDescToInferNode.containsKey(desc)) { + + } + return null; + } + + public void assignTopLocationToDescriptor(Descriptor desc) { + mapDescriptorToUniqueID.put(desc, Integer.valueOf((topLocationID))); + debug_uniqueid_print(desc); + } + + public void assignUniqueIDtoDescriptor(Descriptor desc) { + mapDescriptorToUniqueID.put(desc, getUniqueID()); + debug_uniqueid_print(desc); + } + + private int getUniqueID() { + return uniqueID++; + } + + private void debug_uniqueid_print(Descriptor d) { + if (debug) { + int id = mapDescriptorToUniqueID.get(d).intValue(); + System.out.print(d + " -> "); + if (id == topLocationID) { + System.out.println("TOP"); + } else { + System.out.println(id); + } + + } + } +} diff --git a/Robust/src/Analysis/SSJava/InferNode.java b/Robust/src/Analysis/SSJava/InferNode.java new file mode 100644 index 00000000..b00b4e0f --- /dev/null +++ b/Robust/src/Analysis/SSJava/InferNode.java @@ -0,0 +1,26 @@ +package Analysis.SSJava; + +import IR.Descriptor; + +public class InferNode { + + private Descriptor desc; + private int id; + + public Descriptor getDesc() { + return desc; + } + + public void setDesc(Descriptor desc) { + this.desc = desc; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + +} diff --git a/Robust/src/Analysis/SSJava/LocationInference.java b/Robust/src/Analysis/SSJava/LocationInference.java index 267948ad..2bb1330b 100644 --- a/Robust/src/Analysis/SSJava/LocationInference.java +++ b/Robust/src/Analysis/SSJava/LocationInference.java @@ -3,19 +3,40 @@ package Analysis.SSJava; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import IR.ClassDescriptor; +import IR.Descriptor; import IR.FieldDescriptor; import IR.MethodDescriptor; import IR.State; import IR.SymbolTable; import IR.VarDescriptor; +import IR.Tree.ArrayAccessNode; +import IR.Tree.AssignmentNode; +import IR.Tree.BlockExpressionNode; import IR.Tree.BlockNode; import IR.Tree.BlockStatementNode; +import IR.Tree.CastNode; +import IR.Tree.CreateObjectNode; import IR.Tree.DeclarationNode; +import IR.Tree.ExpressionNode; +import IR.Tree.FieldAccessNode; +import IR.Tree.IfStatementNode; import IR.Tree.Kind; +import IR.Tree.LiteralNode; +import IR.Tree.LoopNode; +import IR.Tree.MethodInvokeNode; +import IR.Tree.NameNode; +import IR.Tree.OpNode; +import IR.Tree.ReturnNode; +import IR.Tree.SubBlockNode; +import IR.Tree.SwitchBlockNode; +import IR.Tree.SwitchStatementNode; +import IR.Tree.TertiaryNode; public class LocationInference { @@ -25,11 +46,16 @@ public class LocationInference { List toanalyzeList; List toanalyzeMethodList; + boolean debug = true; + + InferGraph graph; + public LocationInference(SSJavaAnalysis ssjava, State state) { this.ssjava = ssjava; this.state = state; this.toanalyzeList = new ArrayList(); this.toanalyzeMethodList = new ArrayList(); + this.graph = new InferGraph(); } public void setupToAnalyze() { @@ -80,96 +106,168 @@ public class LocationInference { analyzeFieldDeclaration(cd, fd); } else { // for static final, assign top location by default + graph.assignTopLocationToDescriptor(fd); } } } private void analyzeFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) { - // assign a unique ID to field + graph.assignUniqueIDtoDescriptor(fd); } public void inference() { + // 1) assign a unique id to every field & variable setupToAnalyze(); while (!toAnalyzeIsEmpty()) { ClassDescriptor cd = toAnalyzeNext(); + System.out.println("SSJAVA: Location Inference on the class: " + cd); checkDeclarationInClass(cd); setupToAnalazeMethod(cd); while (!toAnalyzeMethodIsEmpty()) { MethodDescriptor md = toAnalyzeMethodNext(); - // need to analyze method declaration for assigning unique id to - // parameters(including 'this' variable) + if (ssjava.needTobeAnnotated(md)) { + // assigns unique id to the method parameters + assignUniqueIDMethodParamteres(cd, md); + + if (state.SSJAVADEBUG) { + System.out.println("SSJAVA: Location Inference on the method: " + md); + } + assignUniqueIDMethodBody(cd, md); + } + } + } + + // 2) construct value flow graph + + setupToAnalyze(); + + while (!toAnalyzeIsEmpty()) { + ClassDescriptor cd = toAnalyzeNext(); + setupToAnalazeMethod(cd); + while (!toAnalyzeMethodIsEmpty()) { + MethodDescriptor md = toAnalyzeMethodNext(); if (ssjava.needTobeAnnotated(md)) { if (state.SSJAVADEBUG) { - System.out.println("SSJAVA: Location Inference: " + md); + System.out.println("SSJAVA: Constructing a flow graph: " + md); } - analyzeMethodBody(cd, md, null); + analyzeMethodBody(cd, md); } } } } - private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md, - CompositeLocation constraints) { + private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) { BlockNode bn = state.getMethodBody(md); - analyzeBlockNode(md, md.getParameterTable(), bn); + // checkLocationFromBlockNode(md, md.getParameterTable(), bn, constraints); } - private CompositeLocation analyzeBlockNode(MethodDescriptor md, SymbolTable nametable, - BlockNode bn) { + private void assignUniqueIDMethodParamteres(ClassDescriptor cd, MethodDescriptor md) { + + List paramList = new ArrayList(); + for (int i = 0; i < md.numParameters(); i++) { + // process annotations on method parameters + VarDescriptor vd = (VarDescriptor) md.getParameter(i); + graph.assignUniqueIDtoDescriptor(vd); + } + + } + + private void assignUniqueIDMethodBody(ClassDescriptor cd, MethodDescriptor md) { + BlockNode bn = state.getMethodBody(md); + assignUniqueIDBlockNode(md, md.getParameterTable(), bn); + } + + private void assignUniqueIDBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) { bn.getVarTable().setParent(nametable); for (int i = 0; i < bn.size(); i++) { BlockStatementNode bsn = bn.get(i); - analyzeBlockStatementNode(md, bn.getVarTable(), bsn); + assignUniqueIDBlockStatementNode(md, bn.getVarTable(), bsn); } - return new CompositeLocation(); } - private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable, + private void assignUniqueIDBlockStatementNode(MethodDescriptor md, SymbolTable nametable, BlockStatementNode bsn) { switch (bsn.kind()) { - case Kind.BlockExpressionNode: - // checkBlockExpressionNode(md,(BlockExpressionNode)bsn); - break; case Kind.DeclarationNode: - analyzeDeclarationNode(md, nametable, (DeclarationNode) bsn); + assignUniqueIDDeclarationNode(md, nametable, (DeclarationNode) bsn); break; case Kind.IfStatementNode: + assignUniqueIDIfStatementNode(md, nametable, (IfStatementNode) bsn); break; case Kind.LoopNode: - break; - - case Kind.ReturnNode: + assignUniqueIDLoopNode(md, nametable, (LoopNode) bsn); break; case Kind.SubBlockNode: + assignUniqueIDSubBlockNode(md, nametable, (SubBlockNode) bsn); break; case Kind.ContinueBreakNode: break; case Kind.SwitchStatementNode: - break; + assignUniqueIDSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn); + } + + } + private void assignUniqueIDSwitchStatementNode(MethodDescriptor md, SymbolTable nametable, + SwitchStatementNode ssn) { + BlockNode sbn = ssn.getSwitchBody(); + for (int i = 0; i < sbn.size(); i++) { + SwitchBlockNode node = (SwitchBlockNode) sbn.get(i); + assignUniqueIDBlockNode(md, nametable, node.getSwitchBlockStatement()); } } - private void analyzeDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) { + private void assignUniqueIDSubBlockNode(MethodDescriptor md, SymbolTable nametable, + SubBlockNode sbn) { + assignUniqueIDBlockNode(md, nametable, sbn.getBlockNode()); + } - VarDescriptor vd = dn.getVarDescriptor(); + private void assignUniqueIDLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) { + if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) { + assignUniqueIDBlockNode(md, nametable, ln.getBody()); + } else { + // check 'for loop' case + BlockNode bn = ln.getInitializer(); + bn.getVarTable().setParent(nametable); + assignUniqueIDBlockNode(md, bn.getVarTable(), ln.getUpdate()); + assignUniqueIDBlockNode(md, bn.getVarTable(), ln.getBody()); + } + + } + + private void assignUniqueIDIfStatementNode(MethodDescriptor md, SymbolTable nametable, + IfStatementNode isn) { + + assignUniqueIDBlockNode(md, nametable, isn.getTrueBlock()); + + if (isn.getFalseBlock() != null) { + assignUniqueIDBlockNode(md, nametable, isn.getFalseBlock()); + } + + } + + private void assignUniqueIDDeclarationNode(MethodDescriptor md, SymbolTable nametable, + DeclarationNode dn) { + + VarDescriptor vd = dn.getVarDescriptor(); + graph.assignUniqueIDtoDescriptor(vd); } } diff --git a/Robust/src/Analysis/SSJava/SSJavaAnalysis.java b/Robust/src/Analysis/SSJava/SSJavaAnalysis.java index 146af4e6..31075ece 100644 --- a/Robust/src/Analysis/SSJava/SSJavaAnalysis.java +++ b/Robust/src/Analysis/SSJava/SSJavaAnalysis.java @@ -119,7 +119,7 @@ public class SSJavaAnalysis { // debugPrint(); } if (state.SSJAVAINFER) { - // inference(); + inference(); } else { parseLocationAnnotation(); doFlowDownCheck();