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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////
// The Abstract domain for determining "constant" expressions
// i.e. It determines which expression are statically binded
/////////////////////////////////////////////////////////////////////////////////
/*
using System;
namespace Microsoft.AbstractInterpretationFramework
{
using Microsoft.Contracts;
using System.Collections.Generic;
using Microsoft.AbstractInterpretationFramework;
/// <summary>
/// This is an abstract domain for inferring constant expressions
/// </summary>
public class ConstantExpressions : Lattice
{
/// <summary>
/// An abstract element is made of two maps:
/// + A map from variables to expressions \cup top ( i.e. for each variable, the expression it is binded )
/// + A map from variables to set of variabes ( i.e. for each variable, the set of variables that depends on its value )
/// </summary>
private class AbstractElement: Element
{
private Dictionary<IVariable!, BindExpr> variableBindings;
private Dictionary<IVariable!, List<IVariable>> variableDependences;
static private AbstractElement! bottom;
static public Element! Bottom
{
get
{
if(bottom == null)
{
bottom = new AbstractElement();
bottom.variableBindings = null;
bottom.variableDependences = null;
}
assert bottom.variableBindings == null && bottom.variableDependences == null;
return bottom;
}
}
static public Element! Top
{
get
{
return new AbstractElement();
}
}
AbstractElement()
{
this.variableBindings = new Dictionary<IVariable!, BindExpr>();
this.variableDependences = new Dictionary<IVariable!, List<IVariable>>();
}
/// <summary>
/// Our abstract element is top if and only if it has any constraint on variables
/// </summary>
public bool IsTop
{
get
{
return this.variableBindings.Keys.Count == 0 && this.variableDependences.Keys.Count == 0;
}
}
/// <summary>
/// Our abstract element is bottom if and only if the maps are null
/// </summary>
public bool IsBottom
{
get
{
assert (this.variableBindings == null) <==> (this.variableDependences == null);
return this.variableBindings == null && this.variableDependences == null;
}
}
/// <summary>
/// The pointwise join...
/// </summary>
public static AbstractElement! Join(AbstractElement! left, AbstractElement! right)
{
AbstractElement! result = new AbstractElement();
// Put all the variables in the left
foreach(IVariable! var in left.variableBindings.Keys)
{
BindExpr leftVal = left.variableBindings[var];
assert leftVal != null;
BindExpr rightVal = right.variableBindings[var];
if(rightVal== null) // the expression is not there
{
result.variableBindings.Add(var, leftVal);
}
else // both abstract elements have a definition for the variable....
{
result.variableBindings.Add(var, BindExpr.Join(leftVal, rightVal));
}
}
// Put all the variables in the right
foreach(IVariable! var in right.variableBindings.Keys)
{
BindExpr rightVal = right.variableBindings[var];
assert rightVal != null;
BindExpr leftVal = left.variableBindings[var];
if(rightVal== null) // the expression is not there
{
result.variableBindings.Add(var, rightVal);
}
else // both abstract elements have a definition for the variable....
{
result.variableBindings.Add(var, BindExpr.Join(rightVal, leftVal));
}
}
// Join the dependencies...
foreach(IVariable! var in left.variableDependences.Keys)
{
List<IVariable> dependencies = left.variableDependences[var];
List<IVariable> dup = new List<IVariable>(dependencies);
result.variableDependences.Add(var, dup);
}
foreach(IVariable! var in right.variableDependences.Keys)
{
if(result.variableDependences.ContainsKey(var))
{
List<IVariable> dependencies = result.variableDependences[var];
dependencies.AddRange(right.variableDependences[var]);
}
else
{
List<IVariable> dependencies = right.variableDependences[var];
List<IVariable> dup = new List<IVariable>(dependencies);
result.variableDependences.Add(var, dup);
}
}
// Normalize... i.e. for the variables such thas they point to an unknown expression (top) we have to update also their values
result.Normalize();
return result;
}
///<summary>
/// Normalize the current abstract element, in that it propagetes the "dynamic" information throughtout the abstract element
///</summary>
public void Normalize()
{
if(this.IsBottom)
return;
if(this.IsTop)
return;
assert this.variableBindings != null;
bool atFixpoint = false;
while(!atFixpoint)
{
atFixpoint = true; // guess that we've got the fixpoint...
foreach(IVariable x in this.variableBindings.Keys)
{
if(this.variableBindings[x].IsTop) // It means that the variable is tied to a dynamic expression
{
foreach(IVariable y in this.variableDependences[x]) // The all the variables that depend on x are also dynamic...
{
assert x != y; // A variable cannot depend on itself...
if(!this.variableBindings[y].IsTop)
{
this.variableBindings[y] = BindExpr.Top;
atFixpoint = false; // the assumption that we were at the fixpoint was false, we have still to propagate some information...
}
}
}
}
}
}
/// <summary>
/// The pointwise meet...
/// </summary>
public static AbstractElement! Meet(AbstractElement! left, AbstractElement! right)
{
AbstractElement! result = new AbstractElement();
// Put the variables that are both in left and right
foreach(IVariable var in left.variableBindings.Keys)
{
if(right.variableBindings.ContainsKey(var))
{
result.variableBindings.Add(var, BindExpr.Meet(left.variableBindings[var], right.variableBindings[var]));
}
}
// Intersect the dependencies
foreach(IVariable var in result.variableBindings.Keys)
{
List<IVariable> depLeft = left.variableDependences[var];
List<IVariable> depRight = right.variableDependences[var];
// Intersect the two sets
result.variableDependences.Add(var, depLeft);
foreach(IVariable v in depRight)
{
if(!result.variableDependences.ContainsKey(v))
{
result.variableDependences.Remove(v);
}
}
}
// Now we remove the dependencies with variables not in variableBindings
List<IVariable>! varsToRemove = new List<IVariable>();
foreach(IVariable var in result.
}
/// <summary>
/// Clone the current abstract element
/// </summary>
public override Element! Clone()
{
AbstractElement cloned = new AbstractElement();
foreach(IVariable var in this.variableBindings.Keys)
{
cloned.variableBindings.Add(var, this.variableBindings[var]);
}
foreach(IVariable var in this.variableDependences.Keys)
{
List<IVariable> dependingVars = this.variableDependences[var];
List<IVariable> clonedDependingVars = new List<IVariable>(dependingVars);
cloned.variableDependences.Add(var, clonedDependingVars);
}
}
/// <summary>
/// Return the variables that have a binding
/// </summary>
public override ICollection<IVariable!>! FreeVariables()
{
List<IVariable!> vars = new List<IVariable!>(this.variableBindings.Keys);
return vars;
}
public override string! ToString()
{
string! retString = "";
retString += "Bindings";
foreach(IVariable var in this.variableBindings.Keys)
{
string! toAdd = var.ToString() + " -> " + this.variableBindings[var];
retString += toAdd + ",";
}
retString += "\nDependencies";
foreach(IVariable var in this.variableDependences.Keys)
{
string! toAdd = var.ToString() + " -> " + this.variableDependences[var];
retString += toAdd + ",";
}
return retString;
}
}
public override Element! Top
{
get
{
return AbstractElement.Top;
}
}
public override Element! Bottom
{
get
{
return AbstractElement.Bottom;
}
}
public override bool IsTop(Element! e)
{
assert e is AbstractElement;
AbstractElement! absElement = (AbstractElement) e;
return absElement.IsTop;
}
public override bool IsBottom(Element! e)
{
assert e is AbstractElement;
AbstractElement absElement = (AbstractElement) e;
return absElement.IsBottom;
}
/// <summary>
/// Perform the pointwise join of the two abstract elements
/// </summary>
public override Element! NontrivialJoin(Element! a, Element! b)
{
assert a is AbstractElement;
assert b is AbstractElement;
AbstractElement! left = (AbstractElement!) a;
AbstractElement! right = (AbstractElement!) b;
return AbstractElement.Join(left, right);
}
/// <summary>
/// Perform the pointwise meet of two abstract elements
/// </summary>
public override Element! NontrivialMeet(Element! a, Element!b)
{
assert a is AbstractElement;
assert b is AbstractElement;
AbstractElement! left = (AbstractElement!) a;
AbstractElement! right = (AbstractElement!) b;
return AbstractElement.Meet(left, right);
}
}
/// <summary>
/// A wrapper in order to have the algebraic datatype BindExpr := IExpr | Top
/// </summary>
abstract class BindExpr
{
/// <summary>
/// True iff this expression is instance of BindExprTop
/// </summary>
public bool IsTop
{
get
{
return this is BindExprTop;
}
}
static public BindExpr Top
{
get
{
return BindExprTop.UniqueTop;
}
}
/// <summary>
/// True iff this expression is instance of BindExprBottom
/// </summary>
public bool IsBottom
{
get
{
return this is BindExprBottom;
}
}
static public BindExpr Bottom
{
get
{
return BindExprBottom.UniqueBottom;
}
}
public static BindExpr! Join(BindExpr! left, BindExpr! right)
{
if(left.IsTop || right.IsTop)
{
return BindExpr.Top;
}
else if(left.IsBottom)
{
return right;
}
else if(right.IsBottom)
{
return left;
}
else if(left.EmbeddedExpr != right.EmbeddedExpr)
{
return BindExpr.Top;
}
else // left.EmbeddedExpr == right.EmbeddedExpr
{
return left;
}
}
public static BindExpr! Meet(BindExpr! left, BindExpr! right)
{
if(left.IsTop)
{
return right;
}
else if(right.IsTop)
{
return right;
}
else if(left.IsBottom || right.IsBottom)
{
return BindExpr.Bottom;
}
else if(left.EmbeddedExpr != right.EmbeddedExpr)
{
return BindExpr.Bottom;
}
else // left.EmbeddedExpr == right.EmbeddedExpr
{
return left;
}
}
abstract public IExpr! EmbeddedExpr
{
get;
}
}
/// <summary>
/// A wrapper for an integer
/// </summary>
class Expr : BindExpr
{
private IExpr! exp;
public Expr(IExpr! exp)
{
this.exp = exp;
}
override public IExpr! EmbeddedExpr
{
get
{
return this.exp;
}
}
public override string! ToString()
{
return this.exp.ToString();
}
}
/// <summary>
/// The dynamic expression
/// </summary>
class BindExprTop : BindExpr
{
private BindExprTop top = new BindExprTop();
static public BindExprTop! UniqueTop
{
get
{
return this.top;
}
}
private BindExprTop() {}
override public IExpr! EmbeddedExpr
{
get
{
assert false; // If we get there, we have an error
}
}
public override string! ToString()
{
return "<dynamic expression>";
}
}
/// <summary>
/// The unreachable expression
/// </summary>
class BindExprBottom : BindExpr
{
private BindExprBottom! bottom = new BindExprBottom();
static public BindExprBottom! UniqueBottom
{
get
{
return this.bottom;
}
}
private BindExprBottom() {}
override public IExpr! EmbeddedExpr
{
get
{
assert false;
}
}
public override string! ToString()
{
return "<unreachable expression>";
}
}
} // end namespace Microsoft.AbstractInterpretationFramework
*/
|