import Analysis.OoOJava.ConflictEdge;
import Analysis.OoOJava.ConflictNode;
import IR.Descriptor;
+import IR.FieldDescriptor;
import IR.MethodDescriptor;
+import IR.VarDescriptor;
public class FlowGraph {
MethodDescriptor md;
Set<FlowNode> nodeSet;
+ Set<FlowNode> returnNodeSet;
FlowNode thisVarNode;
// maps the composite representation of field/var descriptors to infer nodes
// maps an infer node to the set of neighbors which is pointed by the node
Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
+ // maps a paramter descriptor to its index
+ Map<Descriptor, Integer> mapParamDescToIdx;
boolean debug = true;
- public FlowGraph(MethodDescriptor md) {
+ public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
this.md = md;
- nodeSet = new HashSet<FlowNode>();
- mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
- mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
+ this.nodeSet = new HashSet<FlowNode>();
+ this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
+ this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
+ this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
+ this.mapParamDescToIdx.putAll(mapParamDescToIdx);
+ this.returnNodeSet = new HashSet<FlowNode>();
// create a node for 'this' varialbe
NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
thisDescTuple.add(md.getThis());
- FlowNode thisNode = new FlowNode(thisDescTuple);
+ FlowNode thisNode = new FlowNode(thisDescTuple, true);
NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
thisVarTuple.add(md.getThis());
- mapDescTupleToInferNode.put(thisVarTuple, thisNode);
+ createNewFlowNode(thisVarTuple);
thisVarNode = thisNode;
}
+ public Set<FlowNode> getNodeSet() {
+ return nodeSet;
+ }
+
+ public Set<FlowNode> getParameterNodeSet() {
+ Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode fn = (FlowNode) iterator.next();
+ if (fn.isParameter()) {
+ paramNodeSet.add(fn);
+ }
+ }
+ return paramNodeSet;
+ }
+
public void addNeighbor(FlowNode node, FlowNode neighbor) {
Set<FlowNode> set = mapNodeToNeighborSet.get(node);
if (set == null) {
System.out.println("add a new neighbor " + neighbor + " to " + node);
}
+ public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
+
+ FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
+ FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
+
+ Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
+ for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
+ FlowEdge flowEdge = (FlowEdge) iterator.next();
+ if (flowEdge.getDst().equals(toNode)) {
+ return true;
+ } else {
+ if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
NTuple<Descriptor> endTuple) {
FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
-
fromNode.addOutEdge(edge);
System.out.println("add a new edge=" + edge);
if (mapDescTupleToInferNode.containsKey(descTuple)) {
return mapDescTupleToInferNode.get(descTuple);
} else {
- FlowNode node = new FlowNode(descTuple);
- mapDescTupleToInferNode.put(descTuple, node);
+ FlowNode node = createNewFlowNode(descTuple);
return node;
}
}
return thisVarNode;
}
- public void createNewFlowNode(NTuple<Descriptor> tuple) {
+ public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
if (!mapDescTupleToInferNode.containsKey(tuple)) {
- FlowNode node = new FlowNode(tuple);
+
+ FlowNode node = new FlowNode(tuple, isParamter(tuple));
mapDescTupleToInferNode.put(tuple, node);
nodeSet.add(node);
}
System.out.println("Creating new node=" + node);
+ return node;
+ } else {
+ return mapDescTupleToInferNode.get(tuple);
}
}
- public void writeGraph() throws java.io.IOException {
+ public void setReturnFlowNode(NTuple<Descriptor> tuple) {
- String graphName = md.toString();
- graphName = graphName.replaceAll("[\\W]", "");
+ if (!mapDescTupleToInferNode.containsKey(tuple)) {
+ createNewFlowNode(tuple);
+ }
- BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
- bw.write("digraph " + graphName + " {\n");
- bw.write("compound=true;\n");
+ FlowNode node = mapDescTupleToInferNode.get(tuple);
+ node.setReturn(true);
- // then visit every flow node
+ returnNodeSet.add(node);
+ }
- Iterator<FlowNode> iter = nodeSet.iterator();
+ public Set<FlowNode> getReturnNodeSet() {
+ return returnNodeSet;
+ }
- Set<FlowEdge> addedSet = new HashSet<FlowEdge>();
+ public boolean isParamter(NTuple<Descriptor> tuple) {
+ // return true if a descriptor tuple is started with a parameter descriptor
+ Descriptor firstIdxDesc = tuple.get(0);
+ return mapParamDescToIdx.containsKey(firstIdxDesc);
+ }
- while (iter.hasNext()) {
- FlowNode node = iter.next();
+ public int getParamIdx(NTuple<Descriptor> tuple) {
+ Descriptor firstDesc = tuple.get(0);
+ return mapParamDescToIdx.get(firstDesc).intValue();
+ }
- if (node.getFieldNodeSet().size() > 0) {
- drawSubgraph(node, bw);
- }
+ private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
+ Set<FlowEdge> addedEdgeSet) throws IOException {
- String attributes = " [";
+ Set<FlowEdge> edgeSet = node.getOutEdgeSet();
- attributes += "label=\"" + node.getID() + "\"]";
+ for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
+ FlowEdge flowEdge = iterator.next();
- bw.write(node.getID() + attributes + ";\n");
+ FlowNode u = flowEdge.getSrc();
+ FlowNode v = flowEdge.getDst();
- Set<FlowEdge> edgeSet = node.getOutEdgeSet();
+ if (u.getDescTuple().equals(flowEdge.getInitTuple())
+ && v.getDescTuple().equals(flowEdge.getEndTuple())) {
+ // only draw an edge of the actual value flow
- for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
- FlowEdge flowEdge = iterator.next();
+ if (!addedEdgeSet.contains(flowEdge)) {
- FlowNode u = flowEdge.getSrc();
- FlowNode v = flowEdge.getDst();
+ if (!addedNodeSet.contains(u)) {
+ drawNode(u, bw);
+ addedNodeSet.add(u);
+ }
+ if (!addedNodeSet.contains(v)) {
+ drawNode(v, bw);
+ addedNodeSet.add(v);
+ }
- if (!addedSet.contains(flowEdge)) {
bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
- addedSet.add(flowEdge);
+ addedEdgeSet.add(flowEdge);
}
-
}
+
}
- bw.write("graphTitle[label=\"" + graphName + "\",shape=box];\n");
+ }
+
+ private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
+ bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+ }
+
+ public void writeGraph() throws java.io.IOException {
+
+ String graphName = "flowgraph_" + md.toString();
+ graphName = graphName.replaceAll("[\\W]", "");
+
+ BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
+ bw.write("digraph " + graphName + " {\n");
+ bw.write("compound=true;\n");
+
+ // then visit every flow node
+
+ Iterator<FlowNode> iter = nodeSet.iterator();
+
+ Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
+ Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
+
+ while (iter.hasNext()) {
+ FlowNode node = iter.next();
+
+ if (node.getDescTuple().size() == 1) {
+ // here, we just care about the local variable
+ if (node.getFieldNodeSet().size() > 0) {
+ drawSubgraph(node, bw, addedEdgeSet);
+ }
+ }
+ drawEdges(node, bw, addedNodeSet, addedEdgeSet);
+
+ }
bw.write("}\n");
bw.close();
}
- private void drawSubgraph(FlowNode node, BufferedWriter bw) throws IOException {
+ public boolean constainsNode(FlowNode node) {
+ return nodeSet.contains(node);
+ }
+
+ private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
+ throws IOException {
- bw.write(" subgraph sg" + node.getID() + "{\n");
- // bw.write(" color=gray;\n");
- bw.write(" label=\"" + node.getID() + "\";\n");
+ bw.write("subgraph cluster_" + node.getID() + "{\n");
+ bw.write("label=\"" + node.getPrettyID() + "\";\n");
Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
FlowNode fieldNode = (FlowNode) iterator.next();
- String attribute = fieldNode.getID() + ";\n";
- bw.write(" " + attribute);
+ if (fieldNode.getFieldNodeSet().size() > 0) {
+ drawSubgraph(fieldNode, bw, addedSet);
+ } else {
+ Descriptor desc = fieldNode.getDescTuple().getLastElement();
+ if (desc instanceof VarDescriptor) {
+ VarDescriptor varDesc = (VarDescriptor) desc;
+ if (varDesc.getType().isPrimitive()) {
+ bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
+ }
+ } else if (desc instanceof FieldDescriptor) {
+ FieldDescriptor fieldDesc = (FieldDescriptor) desc;
+ if (fieldDesc.getType().isPrimitive()) {
+ bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
+ }
+ }
+ }
}
- bw.write(" }\n");
+ bw.write("}\n");
}
}
\ No newline at end of file