Skip to content

Commit 0fb2fce

Browse files
committed
Approximate Math.floor
1 parent 36c11f1 commit 0fb2fce

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

usvm-ts/src/main/kotlin/org/usvm/machine/expr/CallApproximations.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.usvm.machine.expr
22

3+
import io.ksmt.expr.KFpRoundingMode
34
import io.ksmt.utils.asExpr
45
import mu.KotlinLogging
56
import org.jacodb.ets.model.EtsArrayType
@@ -76,6 +77,13 @@ internal fun TsExprResolver.tryApproximateInstanceCall(
7677
}
7778
}
7879

80+
// Handle `Math` method calls
81+
if (expr.instance.name == "Math") {
82+
if (expr.callee.name == "floor") {
83+
return from(handleMathFloor(expr))
84+
}
85+
}
86+
7987
val instance = scope.calcOnState { resolve(expr.instance)?.asExpr(addressSort) }
8088
?: return TsExprApproximationResult.ResolveFailure
8189

@@ -255,6 +263,27 @@ private fun TsExprResolver.handlePromiseResolveReject(expr: EtsInstanceCallExpr)
255263
promise
256264
}
257265

266+
private fun TsExprResolver.handleMathFloor(expr: EtsInstanceCallExpr): UExpr<*>? = with(ctx) {
267+
check(expr.args.size == 1) {
268+
"Math.floor() should have exactly one argument, but got ${expr.args.size}"
269+
}
270+
val arg = resolve(expr.args.single()) ?: return null
271+
272+
when (arg.sort) {
273+
fp64Sort -> mkFpRoundToIntegralExpr(
274+
roundingMode = mkFpRoundingModeExpr(KFpRoundingMode.RoundTowardNegative),
275+
value = arg.asExpr(fp64Sort),
276+
)
277+
278+
sizeSort -> arg.asExpr(sizeSort)
279+
280+
else -> {
281+
logger.warn { "Unsupported argument sort for Math.floor(): ${arg.sort}" }
282+
null
283+
}
284+
}
285+
}
286+
258287
/**
259288
* Handles the `Array.push(...items)` method call.
260289
* Appends the specified `items` to the end of the array.

0 commit comments

Comments
 (0)