Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take operator into account when evaluating conditional expressions #1528

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ open class ValueEvaluator(
"<" -> handleLess(lhsValue, rhsValue, expr)
"<=" -> handleLEq(lhsValue, rhsValue, expr)
"==" -> handleEq(lhsValue, rhsValue, expr)
"!=" -> handleNEq(lhsValue, rhsValue, expr)
else -> cannotEvaluate(expr as Node, this)
}
}
Expand Down Expand Up @@ -298,6 +299,14 @@ open class ValueEvaluator(
}
}

private fun handleNEq(lhsValue: Any?, rhsValue: Any?, expr: Expression?): Any? {
return if (lhsValue is Number && rhsValue is Number) {
lhsValue.compareTo(rhsValue) != 0
} else {
cannotEvaluate(expr, this)
}
}

/**
* We handle some basic unary operators. These also affect pointers and dereferences for
* languages that support them.
Expand Down Expand Up @@ -361,12 +370,17 @@ open class ValueEvaluator(
}

protected open fun handleConditionalExpression(expr: ConditionalExpression, depth: Int): Any? {
var condition = expr.condition

// Assume that condition is a binary operator
if (expr.condition is BinaryOperator) {
val lhs = evaluateInternal((expr.condition as? BinaryOperator)?.lhs, depth)
val rhs = evaluateInternal((expr.condition as? BinaryOperator)?.rhs, depth)
if (condition is BinaryOperator) {
val lhs = evaluateInternal(condition.lhs, depth)
val rhs = evaluateInternal(condition.rhs, depth)

// Compute the effect of the comparison
val comparison = computeBinaryOpEffect(lhs, rhs, condition)

return if (lhs == rhs) {
return if (comparison == true) {
evaluateInternal(expr.thenExpression, depth + 1)
} else {
evaluateInternal(expr.elseExpression, depth + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class ValueEvaluatorTest {
val main = mainClass.functions["main"]
assertNotNull(main)

val s = main.refs("s").last()
val s = main.refs("s").lastOrNull()
assertNotNull(s)

var value = s.evaluate()
Expand All @@ -186,7 +186,7 @@ class ValueEvaluatorTest {
value = s.evaluate(MultiValueEvaluator())
assertEquals(setOf("big!?", "small!?"), value)

val i = main.refs("i").last()
val i = main.refs("i").lastOrNull()
assertNotNull(i)

value = i.evaluate()
Expand Down Expand Up @@ -741,15 +741,15 @@ class ValueEvaluatorTest {

@Test
fun testHandleUnary() {
with(TestHandler(TestLanguageFrontend())) {
val neg = newUnaryOperator("-", false, true)
with(TestLanguageFrontend()) {
val neg = newUnaryOperator("-", postfix = false, prefix = true)
neg.input = newLiteral(3, primitiveType("int"))
assertEquals(-3, ValueEvaluator().evaluate(neg))

neg.input = newLiteral(3.5, primitiveType("double"))
assertEquals(-3.5, ValueEvaluator().evaluate(neg))

val plusplus = newUnaryOperator("++", true, false)
val plusplus = newUnaryOperator("++", postfix = true, prefix = false)
plusplus.input = newLiteral(3, primitiveType("int"))
assertEquals(4, ValueEvaluator().evaluate(plusplus))

Expand All @@ -759,7 +759,7 @@ class ValueEvaluatorTest {
plusplus.input = newLiteral(3.5f, primitiveType("float"))
assertEquals(4.5f, ValueEvaluator().evaluate(plusplus))

val minusminus = newUnaryOperator("--", true, false)
val minusminus = newUnaryOperator("--", postfix = true, prefix = false)
minusminus.input = newLiteral(3, primitiveType("int"))
assertEquals(2, ValueEvaluator().evaluate(minusminus))

Expand All @@ -770,4 +770,23 @@ class ValueEvaluatorTest {
assertEquals(2.5f, ValueEvaluator().evaluate(minusminus))
}
}

@Test
fun handleConditionalExpression() {
with(TestLanguageFrontend()) {
val a = newVariableDeclaration("a")
a.initializer = newLiteral(1)

val aRef = newReference("a")
aRef.refersTo = a
aRef.prevDFG = mutableSetOf(a)

val comparison = newBinaryOperator("!=")
comparison.lhs = aRef
comparison.rhs = newLiteral(1)

val cond = newConditionalExpression(comparison, newLiteral(2), aRef)
oxisto marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(1, cond.evaluate())
}
}
}
Loading