1 package Analysis.Pointer;
5 import Analysis.Pointer.BasicBlock.BBlock;
6 import Analysis.Pointer.AllocFactory.AllocNode;
9 HashMap<FlatMethod, BasicBlock> blockMap;
10 HashMap<BBlock, Graph> bbgraphMap;
11 HashMap<FlatNode, Graph> graphMap;
14 AllocFactory allocFactory;
15 LinkedList<Delta> toprocess;
17 public Pointer(State state, TypeUtil typeUtil) {
19 this.blockMap=new HashMap<FlatMethod, BasicBlock>();
20 this.bbgraphMap=new HashMap<BBlock, Graph>();
21 this.graphMap=new HashMap<FlatNode, Graph>();
22 this.typeUtil=typeUtil;
23 this.allocFactory=new AllocFactory(state, typeUtil);
24 this.toprocess=new LinkedList<Delta>();
27 public BasicBlock getBBlock(FlatMethod fm) {
28 if (!blockMap.containsKey(fm))
29 blockMap.put(fm, BasicBlock.getBBlock(fm));
30 return blockMap.get(fm);
33 Delta buildInitialContext() {
34 MethodDescriptor md=typeUtil.getMain();
35 FlatMethod fm=state.getMethodFlat(md);
36 BasicBlock bb=getBBlock(fm);
37 BBlock start=bb.getStart();
38 Delta delta=new Delta(start, true);
39 HashSet<Edge> arrayset=new HashSet<Edge>();
40 HashSet<Edge> varset=new HashSet<Edge>();
41 arrayset.add(new Edge(allocFactory.StringArray, null, allocFactory.Strings));
42 varset.add(new Edge(fm.getParameter(0), allocFactory.StringArray));
43 delta.heapedgeadd.put(allocFactory.StringArray, arrayset);
44 delta.varedgeadd.put(fm.getParameter(0), varset);
49 toprocess.add(buildInitialContext());
51 while(!toprocess.isEmpty()) {
52 Delta delta=toprocess.remove();
53 BBlock bblock=delta.getBlock();
54 Vector<FlatNode> nodes=bblock.nodes();
56 //Build base graph for entrance to this basic block
57 delta=applyInitDelta(delta, bblock);
58 Graph graph=bbgraphMap.get(bblock);
60 //Compute delta at exit of each node
61 for(int i=0; i<nodes.size();i++) {
62 FlatNode currNode=nodes.get(i);
63 if (!graphMap.containsKey(currNode)) {
64 graphMap.put(currNode, new Graph(graph));
66 Graph nodeGraph=graphMap.get(currNode);
67 delta=processNode(currNode, delta, nodeGraph);
69 //XXXX: Need to generate new delta
73 Delta processNode(FlatNode node, Delta delta, Graph newgraph) {
76 return processNewNode((FlatNew)node, delta, newgraph);
77 case FKind.FlatFieldNode:
78 return processFieldNode((FlatFieldNode)node, delta, newgraph);
80 case FKind.FlatSetFieldNode:
81 case FKind.FlatReturnNode:
82 case FKind.FlatElementNode:
83 case FKind.FlatSetElementNode:
84 case FKind.FlatMethod:
86 case FKind.FlatSESEEnterNode:
87 case FKind.FlatSESEExitNode:
88 case FKind.FlatCastNode:
89 case FKind.FlatOpNode:
90 throw new Error("Unimplemented node:"+node);
92 throw new Error("Unrecognized node:"+node);
96 void applyDiffs(Graph graph, Delta delta) {
97 //Add hidden base edges
98 for(Map.Entry<AllocNode, HashSet<Edge>> e: delta.baseheapedge.entrySet()) {
99 AllocNode node=e.getKey();
100 HashSet<Edge> edges=e.getValue();
101 if (graph.nodeMap.containsKey(node)) {
102 HashSet<Edge> nodeEdges=graph.nodeMap.get(node);
103 nodeEdges.addAll(edges);
108 for(Map.Entry<AllocNode, HashSet<Edge>> e: delta.heapedgeremove.entrySet()) {
109 AllocNode node=e.getKey();
110 HashSet<Edge> edgestoremove=e.getValue();
111 if (graph.nodeMap.containsKey(node)) {
112 //Just apply diff to current map
113 graph.nodeMap.get(node).removeAll(edgestoremove);
115 //Generate diff from parent graph
116 HashSet<Edge> parentedges=graph.parent.nodeMap.get(node);
117 HashSet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
118 graph.nodeMap.put(node, newedgeset);
123 for(Map.Entry<AllocNode, HashSet<Edge>> e: delta.heapedgeadd.entrySet()) {
124 AllocNode node=e.getKey();
125 HashSet<Edge> edgestoadd=e.getValue();
126 //If we have not done a subtract, then
127 if (!graph.nodeMap.containsKey(node)) {
128 //Copy the parent entry
129 graph.nodeMap.put(node, (HashSet<Edge>)graph.parent.nodeMap.get(node).clone());
131 graph.nodeMap.get(node).addAll(edgestoadd);
135 for(Map.Entry<TempDescriptor, HashSet<Edge>> e: delta.varedgeremove.entrySet()) {
136 TempDescriptor tmp=e.getKey();
137 HashSet<Edge> edgestoremove=e.getValue();
139 if (graph.varMap.containsKey(tmp)) {
140 //Just apply diff to current map
141 graph.varMap.get(tmp).removeAll(edgestoremove);
143 //Generate diff from parent graph
144 HashSet<Edge> parentedges=graph.parent.varMap.get(tmp);
145 HashSet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
146 graph.varMap.put(tmp, newedgeset);
151 for(Map.Entry<TempDescriptor, HashSet<Edge>> e: delta.varedgeadd.entrySet()) {
152 TempDescriptor tmp=e.getKey();
153 HashSet<Edge> edgestoadd=e.getValue();
154 graph.varMap.put(tmp, (HashSet<Edge>) edgestoadd.clone());
158 HashSet<AllocNode> getTemps(Graph graph, Delta delta, TempDescriptor tmp) {
159 HashSet<AllocNode> nodes=new HashSet<AllocNode>();
160 HashSet<Edge> removeedges=delta.varedgeremove.get(tmp);
162 for(Edge e:graph.getEdges(tmp)) {
163 if (removeedges==null||!removeedges.contains(e))
166 if (delta.varedgeadd.containsKey(tmp))
167 for(Edge e:delta.varedgeadd.get(tmp)) {
173 Delta processFieldNode(FlatFieldNode node, Delta delta, Graph graph) {
174 TempDescriptor src=node.getSrc();
175 FieldDescriptor fd=node.getField();
176 TempDescriptor dst=node.getDst();
177 if (delta.getInit()) {
178 HashSet<AllocNode> nodes=getTemps(graph, delta, src);
181 applyDiffs(graph, delta);
188 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
189 AllocNode summary=allocFactory.getAllocNode(node, true);
190 AllocNode single=allocFactory.getAllocNode(node, false);
191 TempDescriptor tmp=node.getDst();
193 if (delta.getInit()) {
195 Edge e=new Edge(tmp, single);
197 HashSet<Edge> newedges=new HashSet<Edge>();
199 //Add it into the diffs
200 delta.varedgeadd.put(tmp, newedges);
201 //Remove the old edges
202 delta.varedgeremove.put(tmp, graph.getEdges(tmp));
203 //Apply incoming diffs to graph
204 applyDiffs(graph, delta);
206 /* 1. Fix up the variable edge additions */
208 for(Iterator<Map.Entry<TempDescriptor, HashSet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
209 Map.Entry<TempDescriptor, HashSet<Edge>> entry=entryIt.next();
211 if (entry.getKey()==tmp) {
212 /* Check if this is the tmp we overwrite */
215 /* Otherwise, check if the target of the edge is changed... */
216 rewriteSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
220 /* 2. Fix up the base variable edges */
222 for(Iterator<Map.Entry<TempDescriptor, HashSet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
223 Map.Entry<TempDescriptor, HashSet<Edge>> entry=entryIt.next();
224 TempDescriptor entrytmp=entry.getKey();
226 /* Check is this is the tmp we overwrite, if so add to remove set */
227 if (delta.varedgeremove.containsKey(tmp))
228 delta.varedgeremove.get(tmp).addAll(entry.getValue());
230 delta.varedgeremove.put(tmp, entry.getValue());
232 /* Check if the target of the edge is changed */
233 HashSet<Edge> newset=(HashSet<Edge>)entry.getValue().clone();
234 HashSet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
235 if (delta.varedgeremove.containsKey(entrytmp)) {
236 /* Make sure we do not remove the newly created edges. */
237 delta.varedgeremove.get(entrytmp).removeAll(newset);
238 /* Remove the old edges to the single node */
239 delta.varedgeremove.get(entrytmp).addAll(removeset);
241 /* Remove the old edges to the single node */
242 delta.varedgeremove.put(entrytmp, removeset);
244 if (delta.varedgeadd.containsKey(entrytmp)) {
245 /* Create the new edges */
246 delta.varedgeadd.get(entrytmp).addAll(newset);
248 /* Create the new edges */
249 delta.varedgeadd.put(entrytmp, newset);
255 /* 3. Fix up heap edge additions */
257 HashMap<AllocNode, HashSet<Edge>> addheapedge=new HashMap<AllocNode, HashSet<Edge>>();
258 for(Iterator<Map.Entry<AllocNode, HashSet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
259 Map.Entry<AllocNode, HashSet<Edge>> entry=entryIt.next();
260 HashSet<Edge> edgeset=entry.getValue();
261 AllocNode allocnode=entry.getKey();
262 if (allocnode==single) {
264 rewriteSet(edgeset, graph.nodeMap.get(summary), single, summary);
265 addheapedge.put(summary, edgeset);
267 rewriteSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
273 for(Map.Entry<AllocNode, HashSet<Edge>> entry:addheapedge.entrySet()) {
274 AllocNode allocnode=entry.getKey();
275 if (delta.heapedgeadd.containsKey(allocnode)) {
276 delta.heapedgeadd.get(allocnode).addAll(entry.getValue());
278 delta.heapedgeadd.put(allocnode, entry.getValue());
282 /* 4. Fix up the base heap edges */
284 for(Iterator<Map.Entry<AllocNode, HashSet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
285 Map.Entry<AllocNode, HashSet<Edge>> entry=entryIt.next();
286 HashSet<Edge> edgeset=entry.getValue();
287 AllocNode allocnode=entry.getKey();
288 if (allocnode==single) {
291 AllocNode addnode=(allocnode==single)?summary:allocnode;
293 HashSet<Edge> newset=(HashSet<Edge>) edgeset.clone();
294 HashSet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
295 if (delta.heapedgeadd.containsKey(addnode)) {
296 delta.heapedgeadd.get(addnode).addAll(newset);
298 delta.heapedgeadd.put(addnode, newset);
300 if (delta.heapedgeremove.containsKey(allocnode)) {
301 delta.heapedgeremove.get(allocnode).addAll(removeset);
303 delta.heapedgeremove.put(allocnode, removeset);
307 //Apply incoming diffs to graph
308 applyDiffs(graph, delta);
313 void rewriteSet(HashSet<Edge> edgeset, HashSet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
314 HashSet<Edge> newSet=null;
315 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
316 Edge e=edgeit.next();
317 if (e.dst==oldnode||e.src==oldnode) {
319 newSet=new HashSet<Edge>();
326 if (oldedgeset==null||!oldedgeset.contains(e))
331 edgeset.addAll(newSet);
334 /* Shrinks the incoming set to just include rewritten values.
335 * Returns a set of the original rewritten values */
337 HashSet<Edge> shrinkSet(HashSet<Edge> edgeset, HashSet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
338 HashSet<Edge> newSet=null;
339 HashSet<Edge> removeSet=null;
340 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
341 Edge e=edgeit.next();
343 if (e.dst==oldnode||e.src==oldnode) {
345 newSet=new HashSet<Edge>();
346 removeSet=new HashSet<Edge>();
349 removeSet.add(e.copy());
354 if (oldedgeset==null||!oldedgeset.contains(e))
359 edgeset.addAll(newSet);
363 Delta applyInitDelta(Delta delta, BBlock block) {
364 //Apply delta to graph
365 boolean newGraph=false;
366 if (!bbgraphMap.containsKey(block)) {
367 bbgraphMap.put(block, new Graph(null));
371 Graph graph=bbgraphMap.get(block);
374 newdelta=new Delta(null, true);
375 //Add in heap edges and throw away original diff
376 graph.nodeMap.putAll(delta.heapedgeadd);
377 //Add in var edges and throw away original diff
378 graph.varMap.putAll(delta.varedgeadd);
379 //Record that this is initial set...
381 newdelta=new Delta(null, false);
382 //merge in heap edges and variables
383 mergeHeapEdges(graph, delta, newdelta);
384 mergeVarEdges(graph, delta, newdelta);
385 //Record that this is a diff
386 newdelta.setInit(false);
391 /* This function merges in the heap edges. It updates delta to be
394 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
396 for(Map.Entry<AllocNode, HashSet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
397 AllocNode nsrc=heapedge.getKey();
398 HashSet<Edge> edges=heapedge.getValue();
399 if (!graph.nodeMap.containsKey(nsrc)) {
400 graph.nodeMap.put(nsrc, new HashSet<Edge>());
402 HashSet<Edge> dstedges=graph.nodeMap.get(nsrc);
403 HashSet<Edge> diffedges=new HashSet<Edge>();
405 if (!dstedges.contains(e)) {
411 //Done with edge set...
412 if (diffedges.size()>0) {
414 newdelta.baseheapedge.put(nsrc, diffedges);
419 /* This function merges in the var edges. It updates delta to be
422 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
424 for(Map.Entry<TempDescriptor, HashSet<Edge>> varedge:delta.varedgeadd.entrySet()) {
425 TempDescriptor tmpsrc=varedge.getKey();
426 HashSet<Edge> edges=varedge.getValue();
427 if (!graph.varMap.containsKey(tmpsrc)) {
428 graph.varMap.put(tmpsrc, new HashSet<Edge>());
430 HashSet<Edge> dstedges=graph.varMap.get(tmpsrc);
431 HashSet<Edge> diffedges=new HashSet<Edge>();
433 if (!dstedges.contains(e)) {
439 //Done with edge set...
440 if (diffedges.size()>=0) {
442 newdelta.basevaredge.put(tmpsrc,diffedges);