adding a test case
[IRC.git] / Robust / src / Analysis / Locality / LocalityBinding.java
1 package Analysis.Locality;
2 import IR.MethodDescriptor;
3
4 public class LocalityBinding {
5   private MethodDescriptor md;
6   private Integer[] isglobal;
7   private boolean isatomic;
8   private Integer isglobalreturn;
9   private Integer isglobalthis;
10   private LocalityBinding parent;
11   private boolean hasatomic;
12
13   public LocalityBinding(MethodDescriptor md, boolean atomic) {
14     this.md=md;
15     isglobal=new Integer[md.numParameters()];
16     isatomic=atomic;
17   }
18
19   public void setHasAtomic() {
20     hasatomic=true;
21   }
22
23   public boolean getHasAtomic() {
24     return hasatomic;
25   }
26
27   private static String globalToString(Integer g) {
28     if (g==null)
29       return "";
30     else if (g==LocalityAnalysis.GLOBAL)
31       return "G";
32     else if (g==LocalityAnalysis.LOCAL)
33       return "L";
34     else if (g==LocalityAnalysis.EITHER)
35       return "E";
36     else if (g==LocalityAnalysis.CONFLICT)
37       return "C";
38     else if (g==LocalityAnalysis.SCRATCH)
39       return "S";
40     else if (g==LocalityAnalysis.NORMAL)
41       return "R";
42     else if (g==(LocalityAnalysis.STMCONFLICT))
43       return "C";
44     else if (g==(LocalityAnalysis.STMEITHER))
45       return "E";
46     else throw new Error("unknown value"+g);
47   }
48
49   public String getSignature() {
50     if (md.getModifiers().isNative())
51       return "";
52     String st="_";
53     if (isatomic) {
54       st+="A";
55     } else
56       st+="N";
57     if (isglobalthis==null)
58       st+="N";
59     else
60       st+=globalToString(isglobalthis);
61     for(int i=0; i<isglobal.length; i++) {
62       st+=globalToString(isglobal[i]);
63     }
64     st+="_";
65     return st;
66   }
67
68   /* Use this for an explanation */
69   public void setParent(LocalityBinding lb) {
70     parent=lb;
71   }
72
73   public String getExplanation() {
74     if (parent==null)
75       return toString();
76     else
77       return parent.getExplanation()+"\n"+toString();
78   }
79
80   public String toString() {
81     String st=md.toString()+" ";
82     if (isglobalthis==null) {
83       st+="[static] ";
84     } else {
85       if (isglobalthis.equals(LocalityAnalysis.LOCAL))
86         st+="[local] ";
87       else if (isglobalthis.equals(LocalityAnalysis.GLOBAL))
88         st+="[global] ";
89       else if (isglobalthis.equals(LocalityAnalysis.EITHER))
90         st+="[either] ";
91       else if (isglobalthis.equals(LocalityAnalysis.CONFLICT))
92         st+="[conflict] ";
93       else
94         st+="[this="+isglobalthis+"]";
95     }
96     for(int i=0; i<isglobal.length; i++)
97       if (isglobal[i]==null)
98         st+="NULL";
99       else if (isglobal[i].equals(LocalityAnalysis.LOCAL))
100         st+="local ";
101       else if (isglobal[i].equals(LocalityAnalysis.GLOBAL))
102         st+="global ";
103       else if (isglobal[i].equals(LocalityAnalysis.EITHER))
104         st+="either ";
105       else if (isglobal[i].equals(LocalityAnalysis.CONFLICT))
106         st+="conflict ";
107       else
108         st+="["+isglobal[i]+"]";
109     return st;
110   }
111
112   public void setGlobal(int i, Integer global) {
113     isglobal[i]=global;
114   }
115
116   public Integer isGlobal(int i) {
117     return isglobal[i];
118   }
119
120   public void setGlobalReturn(Integer global) {
121     isglobalreturn=global;
122   }
123
124   public Integer getGlobalReturn() {
125     return isglobalreturn;
126   }
127
128   public void setGlobalThis(Integer global) {
129     isglobalthis=global;
130   }
131
132   public Integer getGlobalThis() {
133     return isglobalthis;
134   }
135
136   public MethodDescriptor getMethod() {
137     return md;
138   }
139
140   public boolean isAtomic() {
141     return isatomic;
142   }
143
144   public boolean contextMatches(LocalityBinding lb) {
145     if (isglobal.length!=lb.isglobal.length)
146       return false;
147     for(int i=0; i<isglobal.length; i++)
148       if (!equiv(isglobal[i],lb.isglobal[i]))
149         return false;
150
151     if (!equiv(isglobalthis, lb.isglobalthis))
152       return false;
153     return (isatomic==lb.isatomic);
154   }
155
156   public static boolean equiv(Integer a, Integer b) {
157     if (a==null) {
158       return b==null;
159     } else if (b==null) {
160       return a==null;
161     } else return a.equals(b);
162   }
163
164   public boolean equals(Object o) {
165     if (o instanceof LocalityBinding) {
166       LocalityBinding lb=(LocalityBinding)o;
167       if (md!=lb.md)
168         return false;
169
170       for(int i=0; i<isglobal.length; i++)
171         if (!equiv(isglobal[i], lb.isglobal[i]))
172           return false;
173
174       if (!equiv(isglobalthis, lb.isglobalthis))
175         return false;
176       return (isatomic==lb.isatomic);
177     }
178     return false;
179   }
180
181   public int hashCode() {
182     int hashcode=md.hashCode();
183     for(int i=0; i<isglobal.length; i++) {
184       if (isglobal[i]!=null)
185         hashcode=hashcode*31+(isglobal[i].intValue());
186     }
187     hashcode=hashcode*31+(isatomic?1:0);
188     return hashcode;
189   }
190 }