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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
using System;
using System.Collections.Generic;
using Microsoft.Boogie;
using System.Diagnostics;
namespace Microsoft.Boogie {
// Build an equivalence relation over the set of program expressions
// Assignment, equality, function calls and procedure calls are the polymorphic operations
// that can be applied to both bitvectors and all other types.
// Otherwise all bitvector operations are separate
public class BitVectorAnalysis : StandardVisitor {
class DisjointSet {
DisjointSet parent;
int rank;
public DisjointSet() {
this.parent = this;
this.rank = 0;
}
public virtual void Union(DisjointSet that) {
DisjointSet xRoot = this.Find();
DisjointSet yRoot = that.Find();
if (xRoot == yRoot)
return;
// x and y are not already in same set. Merge them.
if (xRoot.rank < yRoot.rank) {
xRoot.parent = yRoot;
}
else if (xRoot.rank > yRoot.rank) {
yRoot.parent = xRoot;
}
else {
yRoot.parent = xRoot;
xRoot.rank = xRoot.rank + 1;
}
}
public DisjointSet Find() {
if (parent == this) {
return this;
}
else {
parent = parent.Find();
return parent;
}
}
}
class MapDisjointSet : DisjointSet {
private DisjointSet[] args;
public DisjointSet Args(int i) {
return args[i];
}
private DisjointSet result;
public DisjointSet Result { get { return result; } }
public MapDisjointSet(DisjointSet[] args, DisjointSet result) {
this.args = args;
this.result = result;
}
public override void Union(DisjointSet that) {
base.Union(that);
MapDisjointSet thatMap = that as MapDisjointSet;
Debug.Assert(thatMap != null);
Debug.Assert(this.args.Length == thatMap.args.Length);
// unify args
for (int i = 0; i < this.args.Length; i++) {
if (this.args[i] == null) {
this.args[i] = thatMap.args[i];
}
else if (thatMap.args[i] == null) {
thatMap.args[i] = this.args[i];
}
else {
this.args[i].Union(thatMap.args[i]);
}
}
// unify result
if (this.result == null) {
this.result = thatMap.Result;
}
else if (thatMap.result == null) {
thatMap.result = this.result;
}
else {
this.result.Union(thatMap.result);
}
}
}
private Dictionary<Expr, DisjointSet> exprToDisjointSet = new Dictionary<Expr, DisjointSet>();
private Dictionary<Variable, DisjointSet> varToDisjointSet = new Dictionary<Variable, DisjointSet>();
private readonly DisjointSet uniqueBv32Set = new DisjointSet();
public int Bits(Expr expr) {
DisjointSet disjointSet = MakeDisjointSet(expr);
if (disjointSet.Find() == uniqueBv32Set.Find())
return 32;
return 0;
}
public Type NewType(Variable var) {
DisjointSet disjointSet = MakeDisjointSet(var);
return NewType(var.TypedIdent.Type, disjointSet);
}
public bool IsBv32(Expr expr) {
DisjointSet disjointSet = MakeDisjointSet(expr);
return (disjointSet.Find() == uniqueBv32Set.Find());
}
private Type NewType(Type type, DisjointSet disjointSet) {
MapType mapType = type as MapType;
if (mapType == null) {
if (disjointSet.Find() == uniqueBv32Set.Find())
return new BvType(32);
else
return type;
}
else {
MapDisjointSet mapDisjointSet = disjointSet as MapDisjointSet;
Debug.Assert(mapDisjointSet != null);
TypeSeq newArguments = new TypeSeq();
Type result = NewType(mapType.Result, mapDisjointSet.Result);
bool newTypeNeeded = (result != mapType.Result);
for (int i = 0; i < mapType.Arguments.Length; i++) {
if (mapDisjointSet.Args(i).Find() == uniqueBv32Set.Find()) {
newArguments.Add(new BvType(32));
newTypeNeeded = true;
}
else {
newArguments.Add(mapType.Arguments[i]);
}
}
if (newTypeNeeded) {
return new MapType(mapType.tok, mapType.TypeParameters, newArguments, result);
}
else {
return mapType;
}
}
}
private DisjointSet MakeDisjointSet(Type type) {
MapType mapType = type as MapType;
if (mapType == null) {
return new DisjointSet();
}
DisjointSet[] args = new DisjointSet[mapType.Arguments.Length];
for (int i = 0; i < args.Length; i++) {
args[i] = MakeDisjointSet(mapType.Arguments[i]);
}
DisjointSet result = MakeDisjointSet(mapType.Result);
return new MapDisjointSet(args, result);
}
private DisjointSet MakeDisjointSet(Expr expr) {
if (!exprToDisjointSet.ContainsKey(expr)) {
Debug.Assert(expr.Type != null);
exprToDisjointSet[expr] = MakeDisjointSet(expr.Type);
}
return exprToDisjointSet[expr];
}
private DisjointSet MakeDisjointSet(Variable var) {
if (!varToDisjointSet.ContainsKey(var)) {
varToDisjointSet[var] = MakeDisjointSet(var.TypedIdent.Type);
}
return varToDisjointSet[var];
}
public static void DoBitVectorAnalysis(Program program) {
DuplicateLiteralExpr duplicateLiteralExpr = new DuplicateLiteralExpr();
duplicateLiteralExpr.Visit(program);
BitVectorAnalysis bvAnalyzer = new BitVectorAnalysis();
bvAnalyzer.Visit(program);
IntToBvRewriter intToBvRewriter = new IntToBvRewriter(program, bvAnalyzer);
intToBvRewriter.Visit(program);
program.TopLevelDeclarations.Add(intToBvRewriter.bv32Id);
}
public override Axiom VisitAxiom(Axiom node) {
return node;
}
public override Implementation VisitImplementation(Implementation node) {
for (int i = 0; i < node.InParams.Length; i++) {
DisjointSet a = MakeDisjointSet(node.InParams[i]);
DisjointSet b = MakeDisjointSet(node.Proc.InParams[i]);
a.Union(b);
}
for (int i = 0; i < node.OutParams.Length; i++) {
DisjointSet a = MakeDisjointSet(node.OutParams[i]);
DisjointSet b = MakeDisjointSet(node.Proc.OutParams[i]);
a.Union(b);
}
return base.VisitImplementation(node);
}
public override Cmd VisitAssignCmd(AssignCmd node) {
AssignCmd simpleAssignCmd = node.AsSimpleAssignCmd;
List<AssignLhs> lhss = simpleAssignCmd.Lhss;
List<Expr> rhss = simpleAssignCmd.Rhss;
foreach (Expr rhs in rhss) {
VisitExpr(rhs);
}
for (int i = 0; i < lhss.Count; i++) {
SimpleAssignLhs lhs = (SimpleAssignLhs) lhss[i];
DisjointSet lhsSet = MakeDisjointSet(lhs.AsExpr);
lhsSet.Union(MakeDisjointSet(rhss[i]));
}
return base.VisitAssignCmd(node);
}
public override Cmd VisitCallCmd(CallCmd node) {
for (int i = 0; i < node.Ins.Count; i++) {
DisjointSet actual = MakeDisjointSet(node.Ins[i]);
DisjointSet formal = MakeDisjointSet(node.Proc.InParams[i]);
actual.Union(formal);
}
for (int i = 0; i < node.Outs.Count; i++) {
DisjointSet actual = MakeDisjointSet(node.Outs[i]);
DisjointSet formal = MakeDisjointSet(node.Proc.OutParams[i]);
actual.Union(formal);
}
return base.VisitCallCmd(node);
}
public static bool IsComparisonIntegerFunction(Function func) {
if (func.Name == "INT_LT")
return true;
if (func.Name == "INT_ULT")
return true;
if (func.Name == "INT_LEQ")
return true;
if (func.Name == "INT_ULEQ")
return true;
if (func.Name == "INT_GT")
return true;
if (func.Name == "INT_UGT")
return true;
if (func.Name == "INT_GEQ")
return true;
if (func.Name == "INT_UGEQ")
return true;
return false;
}
public static bool IsIntegerFunction(Function func) {
if (func.Name == "INT_EQ")
return true;
if (func.Name == "INT_NEQ")
return true;
if (func.Name == "INT_ADD")
return true;
if (func.Name == "INT_SUB")
return true;
if (func.Name == "INT_MULT")
return true;
if (func.Name == "INT_DIV")
return true;
if (func.Name == "INT_LT")
return true;
if (func.Name == "INT_ULT")
return true;
if (func.Name == "INT_LEQ")
return true;
if (func.Name == "INT_ULEQ")
return true;
if (func.Name == "INT_GT")
return true;
if (func.Name == "INT_UGT")
return true;
if (func.Name == "INT_GEQ")
return true;
if (func.Name == "INT_UGEQ")
return true;
if (func.Name == "INT_AND")
return true;
if (func.Name == "INT_OR")
return true;
if (func.Name == "INT_XOR")
return true;
if (func.Name == "INT_NOT")
return true;
if (func.Name == "INT_MINUS_BOTH_PTR_OR_BOTH_INT")
return true;
if (func.Name == "INT_MINUS_LEFT_PTR")
return true;
if (func.Name == "INT_PLUS")
return true;
if (func.Name == "INT_BINARY_BOTH_INT")
return true;
return false;
}
public static bool IsBv32Function(Function func) {
if (func.Name == "BV32_EQ")
return true;
if (func.Name == "BV32_NEQ")
return true;
if (func.Name == "BV32_ADD")
return true;
if (func.Name == "BV32_SUB")
return true;
if (func.Name == "BV32_MULT")
return true;
if (func.Name == "BV32_DIV")
return true;
if (func.Name == "BV32_LT")
return true;
if (func.Name == "BV32_ULT")
return true;
if (func.Name == "BV32_LEQ")
return true;
if (func.Name == "BV32_ULEQ")
return true;
if (func.Name == "BV32_GT")
return true;
if (func.Name == "BV32_UGT")
return true;
if (func.Name == "BV32_GEQ")
return true;
if (func.Name == "BV32_UGEQ")
return true;
if (func.Name == "BV32_AND")
return true;
if (func.Name == "BV32_OR")
return true;
if (func.Name == "BV32_XOR")
return true;
if (func.Name == "BV32_NOT")
return true;
if (func.Name == "BV32_MINUS_BOTH_PTR_OR_BOTH_INT")
return true;
if (func.Name == "BV32_MINUS_LEFT_PTR")
return true;
if (func.Name == "BV32_PLUS")
return true;
if (func.Name == "BV32_BINARY_BOTH_INT")
return true;
return false;
}
public override Expr VisitNAryExpr(NAryExpr node) {
BinaryOperator op = node.Fun as BinaryOperator;
if (op != null) {
Debug.Assert(node.Args.Length == 2);
MakeDisjointSet(node.Args[0]).Union(MakeDisjointSet(node.Args[1]));
}
FunctionCall fcall = node.Fun as FunctionCall;
if (fcall != null) {
Function func = fcall.Func;
if (IsIntegerFunction(func)) {
DisjointSet x;
if (IsComparisonIntegerFunction(func))
x = MakeDisjointSet(node.Args[0]);
else
x = MakeDisjointSet(node);
for (int i = 0; i < node.Args.Length; i++) {
DisjointSet actual = MakeDisjointSet(node.Args[i]);
actual.Union(x);
}
}
if (func.Name == "intToBv32") {
DisjointSet arg0 = MakeDisjointSet(node.Args[0]);
arg0.Union(uniqueBv32Set);
}
if (func.Name == "bv32ToInt") {
DisjointSet result = MakeDisjointSet(node);
result.Union(uniqueBv32Set);
}
if (func.Name == "INT_AND" || func.Name == "INT_OR" || func.Name == "INT_XOR" || func.Name == "INT_NOT") {
DisjointSet result = MakeDisjointSet(node);
result.Union(uniqueBv32Set);
}
}
MapSelect select = node.Fun as MapSelect;
if (select != null) {
int i = 0;
MapDisjointSet mapDisjointSet = (MapDisjointSet)MakeDisjointSet(node.Args[i]);
i++;
DisjointSet[] args = new DisjointSet[node.Args.Length - 1];
for (; i < node.Args.Length; i++) {
args[i - 1] = MakeDisjointSet(node.Args[i]);
}
mapDisjointSet.Union(new MapDisjointSet(args, MakeDisjointSet(node)));
}
MapStore store = node.Fun as MapStore;
if (store != null) {
int i = 0;
MapDisjointSet mapDisjointSet = (MapDisjointSet)MakeDisjointSet(node.Args[i]);
i++;
DisjointSet[] args = new DisjointSet[node.Args.Length - 2];
for (; i < node.Args.Length - 1; i++) {
args[i - 1] = MakeDisjointSet(node.Args[i]);
}
mapDisjointSet.Union(new MapDisjointSet(args, MakeDisjointSet(node.Args[i])));
mapDisjointSet.Union(MakeDisjointSet(node));
}
return base.VisitNAryExpr(node);
}
public override Expr VisitIdentifierExpr(IdentifierExpr node) {
DisjointSet a = MakeDisjointSet(node);
DisjointSet b = MakeDisjointSet(node.Decl);
a.Union(b);
return base.VisitIdentifierExpr(node);
}
}
class MyLiteralExpr : LiteralExpr {
public MyLiteralExpr(IToken tok, bool b) : base(tok, b) { }
public MyLiteralExpr(IToken tok, Basetypes.BigNum v) : base(tok, v) { }
public MyLiteralExpr(IToken tok, Basetypes.BigNum v, int b) : base(tok, v, b) { }
public override bool Equals(object obj) {
return this == obj;
}
public override int GetHashCode() {
return base.GetHashCode();
}
}
class DuplicateLiteralExpr : StandardVisitor {
public override LiteralExpr VisitLiteralExpr(LiteralExpr node) {
LiteralExpr litExpr;
if (node.Val is bool) {
litExpr = new MyLiteralExpr(Token.NoToken, (bool)node.Val);
} else if (node.Val is Basetypes.BigNum) {
litExpr = new MyLiteralExpr(Token.NoToken, (Basetypes.BigNum)node.Val);
} else {
BvConst bvconst = (BvConst) node.Val;
litExpr = new MyLiteralExpr(Token.NoToken, bvconst.Value, bvconst.Bits);
}
litExpr.Typecheck(new TypecheckingContext(null));
return litExpr;
}
}
class IntToBvRewriter : StandardVisitor {
private BitVectorAnalysis bvAnalyzer;
public Function bv32Id;
Dictionary<string, Function> intFunctions;
Dictionary<string, Function> bv32Functions;
void DiscoverIntAndBv32Functions(Program program) {
intFunctions = new Dictionary<string, Function>();
bv32Functions = new Dictionary<string, Function>();
foreach (Declaration d in program.TopLevelDeclarations) {
Function func = d as Function;
if (func == null) continue;
if (BitVectorAnalysis.IsIntegerFunction(func)) {
intFunctions.Add(func.Name, func);
}
if (BitVectorAnalysis.IsBv32Function(func)) {
bv32Functions.Add(func.Name, func);
}
}
}
string IntToBv32(string name) {
if (name == "INT_EQ")
return "BV32_EQ";
if (name == "INT_NEQ")
return "BV32_NEQ";
if (name == "INT_ADD")
return "BV32_ADD";
if (name == "INT_SUB")
return "BV32_SUB";
if (name == "INT_MULT")
return "BV32_MULT";
if (name == "INT_DIV")
return "BV32_DIV";
if (name == "INT_LT")
return "BV32_LT";
if (name == "INT_ULT")
return "BV32_ULT";
if (name == "INT_LEQ")
return "BV32_LEQ";
if (name == "INT_ULEQ")
return "BV32_ULEQ";
if (name == "INT_GT")
return "BV32_GT";
if (name == "INT_UGT")
return "BV32_UGT";
if (name == "INT_GEQ")
return "BV32_GEQ";
if (name == "INT_UGEQ")
return "BV32_UGEQ";
if (name == "INT_AND")
return "BV32_AND";
if (name == "INT_OR")
return "BV32_OR";
if (name == "INT_XOR")
return "BV32_XOR";
if (name == "INT_NOT")
return "BV32_NOT";
if (name == "INT_MINUS_BOTH_PTR_OR_BOTH_INT")
return "BV32_MINUS_BOTH_PTR_OR_BOTH_INT";
if (name == "INT_MINUS_LEFT_PTR")
return "BV32_MINUS_LEFT_PTR";
if (name == "INT_PLUS")
return "BV32_PLUS";
if (name == "INT_BINARY_BOTH_INT")
return "BV32_BINARY_BOTH_INT";
System.Diagnostics.Debug.Assert(false);
return "";
}
Function IntToBv32(Function func) {
return bv32Functions[IntToBv32(func.Name)];
}
public IntToBvRewriter(Program program, BitVectorAnalysis bvAnalyzer) {
this.bvAnalyzer = bvAnalyzer;
DiscoverIntAndBv32Functions(program);
BvType bv32Type = new BvType(32);
VariableSeq bv32In = new VariableSeq(new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "in", bv32Type), true));
Formal bv32Out = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "out", bv32Type), false);
bv32Id = new Function(Token.NoToken, "bv32Id", bv32In, bv32Out);
bv32Id.Body = new IdentifierExpr(Token.NoToken, bv32In[0]);
bv32Id.AddAttribute("inline");
}
public override Constant VisitConstant(Constant node) {
node.TypedIdent.Type = bvAnalyzer.NewType(node);
return node;
}
public override Variable VisitVariable(Variable node) {
node.TypedIdent.Type = bvAnalyzer.NewType(node);
return node;
}
public override Implementation VisitImplementation(Implementation node) {
this.VisitVariableSeq(node.LocVars);
this.VisitVariableSeq(node.InParams);
this.VisitVariableSeq(node.OutParams);
return base.VisitImplementation(node);
}
public override Procedure VisitProcedure(Procedure node) {
this.VisitVariableSeq(node.InParams);
this.VisitVariableSeq(node.OutParams);
return node;
}
public override Expr VisitNAryExpr(NAryExpr node) {
FunctionCall fcall = node.Fun as FunctionCall;
if (fcall != null) {
Function func = fcall.Func;
if (func.Name == "intToBv32" || func.Name == "bv32ToInt") {
node.Fun = new FunctionCall(bv32Id);
}
else if (BitVectorAnalysis.IsIntegerFunction(func) && bvAnalyzer.IsBv32(node.Args[0])) {
node.Fun = new FunctionCall(IntToBv32(func));
}
}
return base.VisitNAryExpr(node);
}
public override LiteralExpr VisitLiteralExpr(LiteralExpr node) {
int numBits = bvAnalyzer.Bits(node);
if (numBits > 0 && node.Val is Basetypes.BigNum) {
return new LiteralExpr(Token.NoToken, (Basetypes.BigNum)node.Val, numBits);
}
else {
return node;
}
}
}
}
|