5 public class ArrayAnalysis {
6 Termination termination;
9 public ArrayAnalysis(State state, Termination t) {
12 this.set=new Hashtable();
13 this.leftrelation=new Hashtable();
14 this.rightrelation=new Hashtable();
16 assert termination.exactsize!=null;
21 Hashtable leftrelation;
22 Hashtable rightrelation;
24 public AccessPath getSet(SetDescriptor sd) {
25 if (set.containsKey(sd))
26 return (AccessPath)set.get(sd);
31 public AccessPath getDomain(RelationDescriptor rd) {
32 if (leftrelation.containsKey(rd))
33 return (AccessPath)leftrelation.get(rd);
37 public AccessPath getRange(RelationDescriptor rd) {
38 if (rightrelation.containsKey(rd))
39 return (AccessPath)rightrelation.get(rd);
43 private void doAnalysis() {
44 Vector rules=state.vRules;
45 for(int i=0;i<rules.size();i++) {
46 Rule r=(Rule)rules.get(i);
47 Inclusion inc=r.getInclusion();
48 if (inc instanceof SetInclusion) {
49 SetInclusion si=(SetInclusion)inc;
51 AccessPath oldap=set.containsKey(si.getSet())?(AccessPath)set.get(si.getSet()):null;
52 AccessPath newap=analyzeExpr(r,si.getExpr());
54 set.put(si.getSet(),newap);
56 if (!oldap.equal(newap))
57 set.put(si.getSet(),AccessPath.NONE);
59 } else if (inc instanceof RelationInclusion) {
60 RelationInclusion ri=(RelationInclusion)inc;
62 AccessPath oldapl=leftrelation.containsKey(ri.getRelation())?(AccessPath)leftrelation.get(ri.getRelation()):null;
63 AccessPath newapl=analyzeExpr(r,ri.getLeftExpr());
65 leftrelation.put(ri.getRelation(),newapl);
67 if (!oldapl.equal(newapl))
68 leftrelation.put(ri.getRelation(),AccessPath.NONE);
71 AccessPath oldapr=rightrelation.containsKey(ri.getRelation())?(AccessPath)rightrelation.get(ri.getRelation()):null;
72 AccessPath newapr=analyzeExpr(r,ri.getRightExpr());
74 rightrelation.put(ri.getRelation(),newapr);
76 if (!oldapr.equal(newapr))
77 rightrelation.put(ri.getRelation(),AccessPath.NONE);
79 } else throw new Error();
83 public AccessPath analyzeExpr(Rule r,Expr e) {
84 Vector dotvector=new Vector();
86 while (ptr instanceof CastExpr)
87 ptr=((CastExpr)ptr).getExpr();
90 /* Does something other than a dereference? */
92 if (!(ptr instanceof DotExpr)) {
93 return AccessPath.NONE;
96 DotExpr de=(DotExpr)ptr;
99 while (ptr instanceof CastExpr)
100 ptr=((CastExpr)ptr).getExpr();
102 if (ptr instanceof VarExpr) {
103 VarExpr ve=(VarExpr)ptr;
104 VarDescriptor vd=ve.getVar();
105 AccessPath ap=new AccessPath();
109 for(int i=0;i<r.numQuantifiers();i++) {
110 Quantifier q=r.getQuantifier(i);
111 if ((q instanceof SetQuantifier)&&
112 ((SetQuantifier)q).getVar()==vd) {
113 SetDescriptor sd=((SetQuantifier)q).getSet();
114 int size=termination.exactsize.getsize(sd);
119 return AccessPath.NONE;
124 return AccessPath.NONE;
127 /* Starting point finished - parse dereferences */
128 boolean foundarray=false;
129 for(int j=dotvector.size()-1;j>=0;j--) {
130 DotExpr de2=(DotExpr) dotvector.get(j);
131 FieldDescriptor fd=de2.getField();
132 if (fd instanceof ArrayDescriptor) {
134 if (((ArrayDescriptor)fd).getField().getPtr()) {
135 return AccessPath.NONE;
138 if (foundarray&&fd.getPtr()) {
139 return AccessPath.NONE;
149 public static class AccessPath {
150 public static final AccessPath NONE=new AccessPath();
152 public AccessPath() {
156 SetDescriptor startset;
157 VarDescriptor startvar;
159 public boolean isSet() {
163 public SetDescriptor getSet() {
167 public VarDescriptor getVar() {
172 public void startSet(SetDescriptor sd) {
177 public void startVar(VarDescriptor vd) {
178 assert vd.isGlobal();
184 public void addField(FieldDescriptor fd) {
188 public int numFields() {
192 public FieldDescriptor getField(int i) {
193 return (FieldDescriptor)path.get(i);
196 public String toString() {
203 for(int i=0;i<numFields();i++) {
209 public boolean equal(AccessPath ap) {
212 if (setStart&&this.startset!=ap.startset)
214 if ((!setStart)&&this.startvar!=ap.startvar)
216 if (this.path.size()!=ap.path.size())
218 for(int i=0;i<this.path.size();i++) {
219 if (this.path.get(i)!=ap.path.get(i))