import IR.Descriptor;
import IR.FieldDescriptor;
import IR.MethodDescriptor;
+import IR.NameDescriptor;
import IR.VarDescriptor;
+import IR.Tree.MethodInvokeNode;
public class FlowGraph {
// DS for the lattice generation
Map<Integer, FlowNode> mapIdxToFlowNode;
+ Map<MethodInvokeNode, FlowReturnNode> mapMethodInvokeNodeToFlowReturnNode;
+
public static int interseed = 0;
boolean debug = true;
this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
+ this.mapMethodInvokeNodeToFlowReturnNode = new HashMap<MethodInvokeNode, FlowReturnNode>();
if (!md.isStatic()) {
// create a node for 'this' varialbe
return newNode;
}
+ public FlowReturnNode getFlowReturnNode(MethodInvokeNode min) {
+ return mapMethodInvokeNodeToFlowReturnNode.get(min);
+ }
+
+ public FlowReturnNode createReturnNode(MethodInvokeNode min) {
+ NTuple<Descriptor> tuple = new NTuple<Descriptor>();
+ NameDescriptor n = new NameDescriptor("RETURNLOC" + (LocationInference.locSeed++));
+ tuple.add(n);
+
+ FlowReturnNode newNode = new FlowReturnNode(tuple, min);
+ mapDescTupleToInferNode.put(tuple, newNode);
+ mapMethodInvokeNodeToFlowReturnNode.put(min, newNode);
+ // nodeSet.add(newNode);
+
+ System.out.println("create new set node= " + newNode);
+
+ return newNode;
+ }
+
private void setupMapIdxToDesc() {
Set<Descriptor> descSet = mapParamDescToIdx.keySet();
return edgeSet;
}
+ public Set<FlowNode> getParamFlowNodeSet() {
+ Set<FlowNode> setParamFlowNode = new HashSet<FlowNode>();
+ setParamFlowNode.addAll(mapIdxToFlowNode.values());
+ return setParamFlowNode;
+ }
+
public Set<FlowNode> getNodeSet() {
Set<FlowNode> set = new HashSet<FlowNode>();
set.addAll(mapDescTupleToInferNode.values());
FlowNode fromNode = getFlowNode(fromDescTuple);
FlowNode toNode = getFlowNode(toDescTuple);
+ if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
+ return;
+ }
+
// System.out.println("create an edge from " + fromNode + " to " + toNode);
int fromTupleSize = fromDescTuple.size();
Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
FlowEdge edge = (FlowEdge) iterator.next();
- FlowNode dstNode = edge.getDst();
+ FlowNode originalDstNode = edge.getDst();
+
+ Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
+ if (originalDstNode instanceof FlowReturnNode) {
+ FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
+ Set<NTuple<Descriptor>> rtupleSet = rnode.getReturnTupleSet();
+ for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+ NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+ dstNodeSet.add(getFlowNode(rtuple));
+ }
+ } else {
+ dstNodeSet.add(originalDstNode);
+ }
- if (!visited.contains(dstNode)) {
- visited.add(dstNode);
- recurLocalReachFlowNodeSet(dstNode, visited);
+ for (Iterator iterator2 = dstNodeSet.iterator(); iterator2.hasNext();) {
+ FlowNode dstNode = (FlowNode) iterator2.next();
+ if (!visited.contains(dstNode)) {
+ visited.add(dstNode);
+ recurLocalReachFlowNodeSet(dstNode, visited);
+ }
}
+
}
}
Set<FlowNode> nodeSet = getNodeSet();
for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
- FlowNode flowNode = (FlowNode) iterator.next();
- if (flowNode.getCurrentDescTuple().startsWith(prefix)) {
- recurReachableSetFrom(flowNode, reachableSet);
+ FlowNode originalSrcNode = (FlowNode) iterator.next();
+
+ Set<FlowNode> srcNodeSet = new HashSet<FlowNode>();
+ if (originalSrcNode instanceof FlowReturnNode) {
+ FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
+ Set<NTuple<Descriptor>> rtupleSet = rnode.getReturnTupleSet();
+ for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+ NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+ if (rtuple.startsWith(prefix)) {
+ System.out.println("rtuple=" + rtuple + " give it to recur=" + originalSrcNode);
+ recurReachableSetFrom(originalSrcNode, reachableSet);
+ }
+ }
+ } else {
+ if (originalSrcNode.getCurrentDescTuple().startsWith(prefix)) {
+ recurReachableSetFrom(originalSrcNode, reachableSet);
+ }
}
+
}
return reachableSet;
Descriptor localDesc = fn.getDescTuple().get(0);
- System.out.println("descTuple=" + descTuple);
-
if (fn.isIntermediate()) {
Location interLoc = new Location(md, localDesc.getSymbol());
interLoc.setLocDescriptor(localDesc);
if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
- if (!visited.contains(incomingNode)) {
- visited.add(incomingNode);
- getIncomingFlowNodeSet(incomingNode, visited);
- }
- }
- }
- }
-
- }
-
- public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
-
- NTuple<Descriptor> dstTuple = fn.getDescTuple();
-
- Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
-
- ClassDescriptor cd = null;
-
- for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
- FlowNode node = (FlowNode) iterator.next();
-
- Set<FlowEdge> edgeSet = getOutEdgeSet(node);
- for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
- FlowEdge flowEdge = (FlowEdge) iterator2.next();
- if (dstTuple.equals(flowEdge.getEndTuple())) {
- NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
- NTuple<Location> locTuple = new NTuple<Location>();
- for (int i = 0; i < initTuple.size(); i++) {
- Descriptor d = initTuple.get(i);
- Location loc;
- if (i == 0) {
- loc = new Location(md, d.getSymbol());
- cd = ((VarDescriptor) d).getType().getClassDesc();
- } else {
- loc = new Location(cd, d.getSymbol());
- cd = ((FieldDescriptor) d).getType().getClassDesc();
+ if (incomingNode instanceof FlowReturnNode) {
+ FlowReturnNode rnode = (FlowReturnNode) incomingNode;
+ Set<NTuple<Descriptor>> nodeTupleSet = rnode.getReturnTupleSet();
+ for (Iterator iterator3 = nodeTupleSet.iterator(); iterator3.hasNext();) {
+ NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
+ FlowNode fn = getFlowNode(nodeTuple);
+ if (!visited.contains(fn)) {
+ visited.add(fn);
+ getIncomingFlowNodeSet(fn, visited);
+ }
+ }
+ } else {
+ if (!visited.contains(incomingNode)) {
+ visited.add(incomingNode);
+ getIncomingFlowNodeSet(incomingNode, visited);
}
- locTuple.add(loc);
}
- set.add(locTuple);
+
}
}
}
- return set;
+
}
public boolean isParameter(NTuple<Descriptor> tuple) {
}
private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
- bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+ if (node instanceof FlowReturnNode) {
+ FlowReturnNode rnode = (FlowReturnNode) node;
+ bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+ } else {
+ bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+ }
+
}
public void writeGraph() throws java.io.IOException {