@@ -32,17 +32,33 @@ import io.ksmt.sort.KSort
3232import io.ksmt.sort.KSortVisitor
3333import io.ksmt.sort.KUninterpretedSort
3434import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap
35+ import it.unimi.dsi.fastutil.longs.LongSet
3536import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
3637import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap
37- import org.ksmt.solver.bitwuzla.bindings.Bitwuzla
3838import org.ksmt.solver.bitwuzla.bindings.BitwuzlaNativeException
39+ import org.ksmt.solver.bitwuzla.bindings.BitwuzlaOption
3940import org.ksmt.solver.bitwuzla.bindings.BitwuzlaSort
4041import org.ksmt.solver.bitwuzla.bindings.BitwuzlaTerm
4142import org.ksmt.solver.bitwuzla.bindings.Native
4243
43- open class KBitwuzlaContext (val ctx : KContext , val bitwuzla : Bitwuzla ) : AutoCloseable {
44+ open class KBitwuzlaContext (val ctx : KContext ) : AutoCloseable {
4445 private var isClosed = false
4546
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+
4662 val trueTerm: BitwuzlaTerm = Native .bitwuzlaMkTrue()
4763 val falseTerm: BitwuzlaTerm = Native .bitwuzlaMkFalse()
4864 val boolSort: BitwuzlaSort = Native .bitwuzlaMkBoolSort()
@@ -59,8 +75,6 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
5975 private val declSorts = mkTermCache<KDecl <* >>()
6076 private val bitwuzlaSorts = mkTermReverseCache<KSort >()
6177
62- private val bitwuzlaValues = mkTermReverseCache<KExpr <* >>()
63-
6478 private val exprCacheLevel = Object2IntOpenHashMap <KExpr <* >>().apply {
6579 defaultReturnValue(Int .MAX_VALUE ) // Level which is greater than any possible level
6680 }
@@ -147,17 +161,6 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
147161 return internalizedDeclSort
148162 }
149163
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-
161164 fun findConvertedExpr (expr : BitwuzlaTerm ): KExpr <* >? = bitwuzlaExpressions.get(expr)
162165
163166 fun saveConvertedExpr (expr : BitwuzlaTerm , converted : KExpr <* >) {
@@ -183,8 +186,6 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
183186 return convertedSort
184187 }
185188
186- fun convertValue (value : BitwuzlaTerm ): KExpr <* >? = bitwuzlaValues.get(value)
187-
188189 // Constant is known only if it was previously internalized
189190 fun convertConstantIfKnown (term : BitwuzlaTerm ): KDecl <* >? = bitwuzlaConstants.get(term)
190191
@@ -301,28 +302,38 @@ open class KBitwuzlaContext(val ctx: KContext, val bitwuzla: Bitwuzla) : AutoClo
301302 if (isClosed) return
302303 isClosed = true
303304
304- Native .bitwuzlaTermDecRef(trueTerm)
305- Native .bitwuzlaTermDecRef(falseTerm)
306- Native .bitwuzlaSortDecRef(boolSort)
307-
308305 sorts.clear()
306+ bitwuzlaSorts.keys.bitwuzlaSortDecRefAll()
309307 bitwuzlaSorts.clear()
310308
311309 exprGlobalCache.clear()
310+ bitwuzlaExpressions.keys.bitwuzlaTermDecRefAll()
312311 bitwuzlaExpressions.clear()
313312 constantsGlobalCache.clear()
314313
315314 exprLeveledCache.clear()
316315 exprCurrentLevelCache.clear()
317316
318317 declSorts.clear()
318+ bitwuzlaConstants.keys.bitwuzlaTermDecRefAll()
319319 bitwuzlaConstants.clear()
320+
321+ bitwuzlaVars.keys.bitwuzlaTermDecRefAll()
322+ bitwuzlaVars.clear()
323+
324+ Native .bitwuzlaDelete(bitwuzla)
325+ Native .bitwuzlaOptionsDelete(bitwuzlaOptions)
320326 }
321327
322328 fun ensureActive () {
323329 check(! isClosed) { " The context is already closed." }
324330 }
325331
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+
326337 private class UninterpretedSortRegisterer (
327338 private val register : MutableMap <KUninterpretedSort , HashSet <KDecl <* >>>
328339 ) : KSortVisitor<Unit> {
0 commit comments