forked from jensnicolay/jipda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountingStore.js
287 lines (253 loc) · 5.74 KB
/
countingStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//"use strict"; //Firefox error
function StoreValue(aval, fresh)
{
this.aval = aval;
this.fresh = (fresh === undefined) ? 1 : fresh;
}
StoreValue.aval =
function (storeValue)
{
return storeValue.aval;
}
StoreValue.prototype.equals =
function (x)
{
if (this === x)
{
return true;
}
return this.aval.equals(x.aval)
&& this.fresh === x.fresh
}
StoreValue.prototype.hashCode =
function ()
{
const prime = 31;
var result = 1;
result = prime * result + this.aval.hashCode();
result = prime * result + this.fresh;
return result;
}
StoreValue.prototype.subsumes =
function (x)
{
return this.aval.subsumes(x.aval);
}
StoreValue.prototype.compareTo =
function (x)
{
return this.aval.compareTo(x.aval);
}
StoreValue.prototype.toString =
function ()
{
return this.aval.toString();
}
StoreValue.prototype.update =
function (aval)
{
if (this.fresh === 1)
{
return this.strongUpdate(aval);
}
return this.weakUpdate(aval);
}
StoreValue.prototype.strongUpdate =
function (aval)
{
return new StoreValue(aval, 1);
}
StoreValue.prototype.weakUpdate =
function (aval)
{
return new StoreValue(this.aval.join(aval), 2);
}
StoreValue.prototype.join =
function (x)
{
if (x === BOT)
{
return this;
}
return new StoreValue(this.aval.join(x.aval), Math.max(this.fresh, x.fresh));
}
//StoreValue.prototype.reset =
// function ()
// {
// return new StoreValue(BOT, 0);
// }
StoreValue.prototype.addresses =
function ()
{
return this.aval.addresses();
}
///////////////
function Store(map)
{
this.map = map;
}
Store.empty =
function ()
{
return new Store(TrieMap.empty());
}
Store.prototype.equals =
function (x)
{
if (this === x)
{
return true;
}
if (!(x instanceof Store))
{
return false;
}
return this.map.equals(x.map);
}
Store.prototype.hashCode =
function ()
{
return this.map.hashCode();
}
//Store.prototype.compareTo =
// function (x)
// {
// return Lattice.subsumeComparison(this, x);
// }
Store.prototype.subsumes =
function (x)
{
if (!(x instanceof Store))
{
return false;
}
return this.map.subsumes(x.map);
}
Store.prototype.diff = // debug
function (x)
{
const diff = [];
const entries = this.map.entries();
for (var i = 0; i < entries.length; i++)
{
const entry = entries[i];
const address = entry[0];
const value = entry[1];
const xvalue = x.map.get(address);
if (xvalue)
{
if (!value.equals(xvalue))
{
// else
// {
diff.push(address + ":\n\t" + value + " (" + value.fresh + ")\n\t" + xvalue + " (" + xvalue.fresh + ")");
// }
if (value.aval.isBenv && xvalue.aval.isBenv)
{
diff.push(value.aval.diff(xvalue.aval))
}
}
}
else
{
diff.push(address + ":\n\t" + value + " (" + value.fresh + ")\n\t<undefined>");
}
}
const xentries = x.map.entries();
for (i = 0; i < xentries.length; i++)
{
const xentry = xentries[i];
const address = xentry[0];
const xvalue = xentry[1];
const value = this.map.get(address);
if (!value)
{
diff.push(address + ":\n\t<undefined>\n\t" + xvalue + " (" + xvalue.fresh + ")");
}
}
return diff.join("\n");
}
Store.prototype.toString =
function ()
{
const entries = this.map.entries();
return "{" + entries.map(
function (entry)
{
return entry[0] + " =" + entry[1].fresh + "=> " + entry[1];
}).join(",") + "}";
}
Store.prototype.nice =
function ()
{
const entries = this.map.entries();
return "\n{\n" + entries.map(
function (entry)
{
return entry[0] + " =" + entry[1].fresh + "=> " + entry[1];
}).join("\n") + "\n}";
}
Store.prototype.lookupAval =
function (address)
{
const value = this.map.get(address);
if (value)
{
return value.aval;
}
throw new Error("Store.lookupAval: no abstract value for address " + address + "\n" + this.nice());
};
Store.prototype.allocAval =
function (address, aval)
{
const value = this.map.get(address);
if (value && value.fresh !== 0)
{
const weaklyUpdatedValue = value.weakUpdate(aval);
const store = new Store(this.map.put(address, weaklyUpdatedValue));
return store;
}
const newValue = new StoreValue(aval);
return new Store(this.map.put(address, newValue));
};
Store.prototype.weakAllocAval =
function (address, aval)
{
const value = this.map.get(address);
const weaklyUpdatedValue = value ? value.weakUpdate(aval) : new StoreValue(aval, 2);
const store = new Store(this.map.put(address, weaklyUpdatedValue));
return store;
};
Store.prototype.updateAval =
function (address, aval)
{
const value = this.map.get(address);
if (value)
{
const updatedValue = value.update(aval);
// print("UPDATED", address, updatedValue);
return new Store(this.map.put(address, updatedValue));
}
throw new Error("Store.updateAval: no abstract value at address " + address);
};
Store.prototype.join =
function (store)
{
return new Store(this.map.join(store.map, BOT));
}
Store.prototype.narrow =
function (addresses)
{
return new Store(this.map.narrow(addresses));
}
Store.prototype.keys =
function ()
{
return this.map.keys();
}
if (typeof module !== 'undefined' && module.exports != null) {
var common = require('./common.js');
var HashMap = common.HashMap;
var TrieMap = common.TrieMap;
exports.Store = Store;
}