forked from microsoft/mcBV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariableDB.fs
110 lines (86 loc) · 3.23 KB
/
VariableDB.fs
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
module VariableDB
open Microsoft.Z3
open System.Collections.Generic
open GlobalOptions
open Util
open Literal
open VariableOrder
type VariableDB private (size:int) =
//Handles variables that are currently in use
//Boolean and BitVector variables are mixed up
//Sorts will make the distinction between boolean and bv mvars
let reallocCoefficient = 0.5
new (var2sort:VarType[]) as this =
let size = var2sort.Length - 1
VariableDB (size)
then
this.sorts <- var2sort
for i in 1 .. this.highestVarInUse do
if var2sort.[i] > 0 then
this.varOrder.insert i false
else
this.varOrder.insert i true
member val originalSize = size + 1
member val allocatedSize = size + 1 with get,set
// Dictates the size of the sort array
member val highestVarInUse = size with get, set
member val private inTempMode = false with get, set
member val private snapshot = -1 with get, set
member val varOrder : VariableOrder = new VariableOrder()
member val sorts = [||] with get, set
member private r.isLegal (v:int) = 0 < v && v <= r.highestVarInUse
member private r.reallocate () =
let sizeIncrement = int (reallocCoefficient * (float r.allocatedSize))
r.sorts <- Array.append r.sorts (Array.zeroCreate sizeIncrement)
r.allocatedSize <- r.allocatedSize + sizeIncrement
member r.isBoolean (v:int) =
assert (r.isLegal v)
r.sorts.[v] = 0
member r.isBitVector (v:int) =
assert (r.isLegal v)
r.sorts.[v] > 0
member r.getBitVectorLength (v:int) =
assert (r.isLegal v)
r.sorts.[v]
member r.newBooleanVariable () =
if r.highestVarInUse + 1 = r.allocatedSize then
r.reallocate ()
r.highestVarInUse <- r.highestVarInUse + 1
r.varOrder.insert r.highestVarInUse true
r.highestVarInUse
member r.newBitVectorVariable (len:int) =
if r.highestVarInUse + 1 = r.allocatedSize then
r.reallocate ()
r.highestVarInUse <- r.highestVarInUse + 1
let v = r.highestVarInUse
r.sorts.[v] <- len
r.varOrder.insert v false
v
member r.getSnapshot = r.snapshot
member r.enterTempMode () =
assert(not r.inTempMode)
assert(r.snapshot = -1)
r.inTempMode <- true
r.varOrder.enterTempMode()
r.snapshot <- r.highestVarInUse
member r.leaveTempMode =
assert(r.inTempMode)
assert(r.snapshot >= 0)
// for i in r.snapshot + 1 .. r.highestVarInUse do
// r.varOrder.remove i
r.varOrder.leaveTempMode
r.highestVarInUse <- r.snapshot
r.snapshot <- -1
r.inTempMode <- false
member r.print () =
if DBG then
printfn ""
printfn "-------------------"
printfn "|Variable database|"
printfn "-------------------"
printfn "Allocated: %d MaxVar: %d " r.allocatedSize r.highestVarInUse
for v in 1 .. r.highestVarInUse do
if r.isBoolean v then
printfn "%d : Bool" v
else
printfn "%d : BitVector %d" v r.sorts.[v]