@@ -32,17 +32,33 @@ import io.ksmt.sort.KSort
32
32
import io.ksmt.sort.KSortVisitor
33
33
import io.ksmt.sort.KUninterpretedSort
34
34
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap
35
+ import it.unimi.dsi.fastutil.longs.LongSet
35
36
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
36
37
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap
37
- import org.ksmt.solver.bitwuzla.bindings.Bitwuzla
38
38
import org.ksmt.solver.bitwuzla.bindings.BitwuzlaNativeException
39
+ import org.ksmt.solver.bitwuzla.bindings.BitwuzlaOption
39
40
import org.ksmt.solver.bitwuzla.bindings.BitwuzlaSort
40
41
import org.ksmt.solver.bitwuzla.bindings.BitwuzlaTerm
41
42
import org.ksmt.solver.bitwuzla.bindings.Native
42
43
43
- open class KBitwuzlaContext (val ctx : KContext , val bitwuzla : Bitwuzla ) : AutoCloseable {
44
+ open class KBitwuzlaContext (val ctx : KContext ) : AutoCloseable {
44
45
private var isClosed = false
45
46
47
+ val bitwuzlaOptions = Native .bitwuzlaOptionsNew()
48
+
49
+ val bitwuzla by lazy { Native .bitwuzlaNew(bitwuzlaOptions).also { bitwuzlaInitialized = true } }
50
+ private var bitwuzlaInitialized = false
51
+
52
+ init {
53
+ Native .bitwuzlaSetOption(bitwuzlaOptions, BitwuzlaOption .BITWUZLA_OPTION_PRODUCE_MODELS , value = 1 )
54
+ Native .bitwuzlaSetOption(bitwuzlaOptions, BitwuzlaOption .BITWUZLA_OPTION_PRODUCE_UNSAT_ASSUMPTIONS , value = 1 )
55
+ Native .bitwuzlaSetOption(bitwuzlaOptions, BitwuzlaOption .BITWUZLA_OPTION_PRODUCE_UNSAT_CORES , value = 1 )
56
+ }
57
+
58
+ val bv1Sort = Native .bitwuzlaMkBvSort(1 )
59
+ val bv1OneTerm: BitwuzlaTerm = Native .bitwuzlaMkBvOne(bv1Sort)
60
+ val bv1ZeroTerm: BitwuzlaTerm = Native .bitwuzlaMkBvZero(bv1Sort)
61
+
46
62
val trueTerm: BitwuzlaTerm = Native .bitwuzlaMkTrue()
47
63
val falseTerm: BitwuzlaTerm = Native .bitwuzlaMkFalse()
48
64
val boolSort: BitwuzlaSort = Native .bitwuzlaMkBoolSort()
@@ -59,8 +75,6 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
59
75
private val declSorts = mkTermCache<KDecl <* >>()
60
76
private val bitwuzlaSorts = mkTermReverseCache<KSort >()
61
77
62
- private val bitwuzlaValues = mkTermReverseCache<KExpr <* >>()
63
-
64
78
private val exprCacheLevel = Object2IntOpenHashMap <KExpr <* >>().apply {
65
79
defaultReturnValue(Int .MAX_VALUE ) // Level which is greater than any possible level
66
80
}
@@ -147,17 +161,6 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
147
161
return internalizedDeclSort
148
162
}
149
163
150
- /* *
151
- * Internalize and reverse cache Bv value to support Bv values conversion.
152
- *
153
- * Since [Native.bitwuzlaGetBvValue] is only available after check-sat call
154
- * we must reverse cache Bv values to be able to convert all previously internalized
155
- * expressions.
156
- * */
157
- fun saveInternalizedValue (expr : KExpr <* >, term : BitwuzlaTerm ) {
158
- bitwuzlaValues.put(term, expr)
159
- }
160
-
161
164
fun findConvertedExpr (expr : BitwuzlaTerm ): KExpr <* >? = bitwuzlaExpressions.get(expr)
162
165
163
166
fun saveConvertedExpr (expr : BitwuzlaTerm , converted : KExpr <* >) {
@@ -183,8 +186,6 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
183
186
return convertedSort
184
187
}
185
188
186
- fun convertValue (value : BitwuzlaTerm ): KExpr <* >? = bitwuzlaValues.get(value)
187
-
188
189
// Constant is known only if it was previously internalized
189
190
fun convertConstantIfKnown (term : BitwuzlaTerm ): KDecl <* >? = bitwuzlaConstants.get(term)
190
191
@@ -301,28 +302,38 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
301
302
if (isClosed) return
302
303
isClosed = true
303
304
304
- Native .bitwuzlaTermDecRef(trueTerm)
305
- Native .bitwuzlaTermDecRef(falseTerm)
306
- Native .bitwuzlaSortDecRef(boolSort)
307
-
308
305
sorts.clear()
306
+ bitwuzlaSorts.keys.bitwuzlaSortDecRefAll()
309
307
bitwuzlaSorts.clear()
310
308
311
309
exprGlobalCache.clear()
310
+ bitwuzlaExpressions.keys.bitwuzlaTermDecRefAll()
312
311
bitwuzlaExpressions.clear()
313
312
constantsGlobalCache.clear()
314
313
315
314
exprLeveledCache.clear()
316
315
exprCurrentLevelCache.clear()
317
316
318
317
declSorts.clear()
318
+ bitwuzlaConstants.keys.bitwuzlaTermDecRefAll()
319
319
bitwuzlaConstants.clear()
320
+
321
+ bitwuzlaVars.keys.bitwuzlaTermDecRefAll()
322
+ bitwuzlaVars.clear()
323
+
324
+ Native .bitwuzlaDelete(bitwuzla)
325
+ Native .bitwuzlaOptionsDelete(bitwuzlaOptions)
320
326
}
321
327
322
328
fun ensureActive () {
323
329
check(! isClosed) { " The context is already closed." }
324
330
}
325
331
332
+ fun ensureBitwuzlaNotInitialized () = check(! bitwuzlaInitialized) { " Bitwuzla has already initialized." }
333
+
334
+ private fun LongSet.bitwuzlaTermDecRefAll () = forEach(Native ::bitwuzlaTermDecRef)
335
+ private fun LongSet.bitwuzlaSortDecRefAll () = forEach(Native ::bitwuzlaSortDecRef)
336
+
326
337
private class UninterpretedSortRegisterer (
327
338
private val register : MutableMap <KUninterpretedSort , HashSet <KDecl <* >>>
328
339
) : KSortVisitor<Unit> {
0 commit comments