Skip to content

Commit 351cad5

Browse files
mario-bucevvkuncak
authored andcommitted
Adding int2bv and bv2nat
1 parent cf2382b commit 351cad5

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/it/scala/smtlib/it/TheoriesBuilderTests.scala

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
5959
"%d - %s".format(counter, theoryString)
6060
}
6161

62-
62+
6363
val f1 = GreaterEquals(NumeralLit(42), NumeralLit(12))
6464
mkTest(f1, SatStatus, uniqueName())
6565

@@ -95,7 +95,7 @@ class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
9595
"%d - %s".format(counter, theoryString)
9696
}
9797

98-
98+
9999
val f1 = GreaterEquals(NumeralLit(42), NumeralLit(12))
100100
mkTest(f1, SatStatus, uniqueName())
101101

@@ -119,6 +119,8 @@ class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
119119
}
120120

121121
{
122+
import theories.Core.Equals
123+
import theories.Ints.NumeralLit
122124
import theories.FixedSizeBitVectors._
123125
val theoryString = "Theory of Bit Vectors"
124126
var counter = 0
@@ -127,7 +129,6 @@ class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
127129
"%d - %s".format(counter, theoryString)
128130
}
129131

130-
131132
val f1 = SGreaterEquals(BitVectorConstant(42, 32), BitVectorConstant(12, 32))
132133
mkTest(f1, SatStatus, uniqueName())
133134

@@ -160,5 +161,25 @@ class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
160161

161162
val f11 = UGreaterEquals(BitVectorLit(List(true, false)), BitVectorLit(List(true, false)))
162163
mkTest(f11, SatStatus, uniqueName())
164+
165+
val f12 = Equals(Int2BV(8, NumeralLit(42)), BitVectorConstant(42, 8))
166+
mkTest(f12, SatStatus, uniqueName())
167+
168+
val f13 = Equals(BV2Nat(BitVectorConstant(42, 8)), NumeralLit(42))
169+
mkTest(f13, SatStatus, uniqueName())
170+
171+
val f14 = Equals(Int2BV(8, NumeralLit(24)), BitVectorConstant(123, 8))
172+
mkTest(f14, UnsatStatus, uniqueName())
173+
174+
val f15 = Equals(BV2Nat(BitVectorConstant(42, 8)), NumeralLit(123))
175+
mkTest(f15, UnsatStatus, uniqueName())
176+
177+
// Testing that Int2BV wraps around for integer exceeding the BV size
178+
val f16 = Equals(Int2BV(8, NumeralLit(42 + 256*3)), BitVectorConstant(42, 8))
179+
mkTest(f16, SatStatus, uniqueName())
180+
181+
// Int2BV does not care about the sign
182+
val f17 = Equals(Int2BV(8, NumeralLit(-214)), BitVectorConstant(42, 8))
183+
mkTest(f17, SatStatus, uniqueName())
163184
}
164185
}

src/main/scala/smtlib/printer/PrintingContext.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ class PrintingContext(writer: Writer) {
170170
print(id)
171171
}
172172

173-
case SNumeral(value) => print(value.toString)
173+
case SNumeral(value) =>
174+
if (value >= 0) print(value.toString)
175+
else print(s"(- ${-value})")
174176
case SHexadecimal(value) => print(value.toString)
175177
case SBinary(value) => print("#b" + value.map(if(_) "1" else "0").mkString)
176178
case SDecimal(value) => print(value.toString)
@@ -341,7 +343,7 @@ class PrintingContext(writer: Writer) {
341343
}
342344

343345
protected def printCommandResponse(response: CommandResponse): Unit = response match {
344-
case Success =>
346+
case Success =>
345347
print("success\n")
346348

347349
case Unsupported =>

src/main/scala/smtlib/theories/FixedSizeBitVectors.scala

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,17 @@ object FixedSizeBitVectors {
9090
object LShiftRight extends Operation2 { override val name = "bvlshr" }
9191
object AShiftRight extends Operation2 { override val name = "bvashr" }
9292

93+
object BV2Nat extends Operation1 { override val name = "bv2nat" }
9394

9495
object Extract {
95-
def apply(i: BigInt, j: BigInt, t: Term): Term =
96+
def apply(i: BigInt, j: BigInt, t: Term): Term = {
97+
require(i >= j)
9698
FunctionApplication(
9799
QualifiedIdentifier(Identifier(SSymbol("extract"), Seq(SNumeral(i), SNumeral(j)))),
98100
Seq(t)
99101
)
102+
}
103+
100104
def unapply(term: Term): Option[(BigInt, BigInt, Term)] = term match {
101105
case FunctionApplication(
102106
QualifiedIdentifier(
@@ -197,4 +201,20 @@ object FixedSizeBitVectors {
197201
}
198202
}
199203

204+
object Int2BV {
205+
def apply(m: BigInt, t: Term): Term =
206+
FunctionApplication(
207+
QualifiedIdentifier(Identifier(SSymbol("int2bv"), Seq(SNumeral(m)))),
208+
Seq(t)
209+
)
210+
def unapply(term: Term): Option[(BigInt, Term)] = term match {
211+
case FunctionApplication(
212+
QualifiedIdentifier(
213+
Identifier(SSymbol("int2bv"), Seq(SNumeral(m))),
214+
None
215+
), Seq(t)) => Some((m, t))
216+
case _ => None
217+
}
218+
}
219+
200220
}

0 commit comments

Comments
 (0)