adding a test case
[IRC.git] / Robust / src / Analysis / Pointer / AllocFactory.java
1 package Analysis.Pointer;
2
3 import Analysis.Disjoint.Alloc;
4 import java.util.*;
5 import IR.*;
6 import IR.Flat.*;
7
8 public class AllocFactory {
9   public static AllocSiteNode dummySite=new AllocSiteNode(-1, null);
10   public static AllocNode dummyNode=new AllocNode(-1, null, false, dummySite);
11
12   public static class AllocNode {
13     int allocsite;
14     boolean summary;
15     FlatNew node;
16     AllocSiteNode as;
17
18     public AllocNode(int allocsite, FlatNew node, boolean summary) {
19       this.allocsite=allocsite;
20       this.summary=summary;
21       this.node=node;
22     }
23
24     public AllocNode(int allocsite, FlatNew node, boolean summary, AllocSiteNode as) {
25       this.allocsite=allocsite;
26       this.summary=summary;
27       this.node=node;
28       this.as=as;
29     }
30
31     public AllocSiteNode getAllocSite() {
32       return as;
33     }
34
35     public TypeDescriptor getType() {
36       return node.getType();
37     }
38
39     public FlatNew getFlatNew() {
40       return node;
41     }
42
43     public int getUniqueAllocSiteID() {
44       return allocsite;
45     }
46
47     public boolean isSummary() {
48       return summary;
49     }
50
51     public int hashCode() {
52       return allocsite<<1^(summary?0:1);
53     }
54
55     public boolean equals(Object o) {
56       if (o instanceof AllocNode) {
57         AllocNode an=(AllocNode)o;
58         return (allocsite==an.allocsite)&&(summary==an.summary);
59       }
60       return false;
61     }
62
63     public String toString() {
64       return getID();
65     }
66
67     public String getID() {
68       if (summary)
69         return "SUM"+allocsite;
70       else
71         return "SING"+allocsite;
72     }
73   }
74
75   public static class AllocSiteNode implements Alloc {
76     int allocsite;
77     FlatNew node;
78
79     public AllocSiteNode(int allocsite, FlatNew node) {
80       this.allocsite=allocsite;
81       this.node=node;
82     }
83
84     public TypeDescriptor getType() {
85       return node.getType();
86     }
87
88     public FlatNew getFlatNew() {
89       return node;
90     }
91
92     public int getUniqueAllocSiteID() {
93       return allocsite;
94     }
95
96     public int hashCode() {
97       return allocsite;
98     }
99
100     public boolean equals(Object o) {
101       if (o instanceof AllocSiteNode) {
102         AllocSiteNode an=(AllocSiteNode)o;
103         return (allocsite==an.allocsite);
104       }
105       return false;
106     }
107
108     public String toStringBrief() {
109       return getID();
110     }
111
112     public String toString() {
113       return getID();
114     }
115
116     public String getID() {
117       return "N"+allocsite;
118     }
119   }
120
121   public AllocFactory(State state, TypeUtil typeUtil) {
122     allocMap=new HashMap<FlatNew, Integer>();
123     allocNodeMap=new HashMap<AllocNode, AllocNode>();
124     allocSiteMap=new HashMap<AllocSiteNode, AllocSiteNode>();
125     this.typeUtil=typeUtil;
126     ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.StringClass);
127     TypeDescriptor stringtd=new TypeDescriptor(stringcd);
128     TypeDescriptor stringarraytd=stringtd.makeArray(state);
129     StringArray=new AllocNode(0, new FlatNew(stringarraytd, null, false), false);
130     StringArray.as=getAllocSite(StringArray);
131     Strings=new AllocNode(1, new FlatNew(stringtd, null, false), true);
132     Strings.as=getAllocSite(Strings);
133   }
134
135   public int getSiteNumber(FlatNew node) {
136     if (allocMap.containsKey(node))
137       return allocMap.get(node);
138     int index=siteCounter++;
139     allocMap.put(node, index);
140     return index;
141   }
142
143   public AllocNode getAllocNode(FlatNew node, boolean isSummary) {
144     int site=getSiteNumber(node);
145     AllocNode key=new AllocNode(site, node, isSummary);
146     if (!allocNodeMap.containsKey(key)) {
147       allocNodeMap.put(key, key);
148       key.as=getAllocSite(key);
149       return key;
150     } else
151       return allocNodeMap.get(key);
152   }
153
154   public AllocNode getAllocNode(AllocNode node, boolean isSummary) {
155     int site=node.allocsite;
156     AllocNode key=new AllocNode(site, node.node, isSummary);
157     if (!allocNodeMap.containsKey(key)) {
158       allocNodeMap.put(key, key);
159       key.as=getAllocSite(key);
160       return key;
161     } else
162       return allocNodeMap.get(key);
163   }
164
165   public AllocSiteNode getAllocSite(AllocNode node) {
166     AllocSiteNode as=new AllocSiteNode(node.allocsite, node.node);
167     if (!allocSiteMap.containsKey(as)) {
168       allocSiteMap.put(as, as);
169       return as;
170     } else
171       return allocSiteMap.get(as);
172   }
173
174   HashMap<AllocNode, AllocNode> allocNodeMap;
175   HashMap<AllocSiteNode, AllocSiteNode> allocSiteMap;
176   HashMap<FlatNew, Integer> allocMap;
177   TypeUtil typeUtil;
178   int siteCounter=2;
179
180   public AllocNode StringArray;
181   public AllocNode Strings;
182 }