diff --git a/jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/RawInstListBuilder.kt b/jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/RawInstListBuilder.kt index 9883387ae..fff166c0a 100644 --- a/jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/RawInstListBuilder.kt +++ b/jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/RawInstListBuilder.kt @@ -1123,12 +1123,12 @@ class RawInstListBuilder( val expr = when (opcode) { Opcodes.IFNULL -> JcRawEqExpr(boolTypeName, rhv, JcRawNull()) Opcodes.IFNONNULL -> JcRawNeqExpr(boolTypeName, rhv, JcRawNull()) - Opcodes.IFEQ -> JcRawEqExpr(boolTypeName, JcRawZero(rhv.typeName), rhv) - Opcodes.IFNE -> JcRawNeqExpr(boolTypeName, JcRawZero(rhv.typeName), rhv) - Opcodes.IFLT -> JcRawLtExpr(boolTypeName, JcRawZero(rhv.typeName), rhv) - Opcodes.IFGE -> JcRawGeExpr(boolTypeName, JcRawZero(rhv.typeName), rhv) - Opcodes.IFGT -> JcRawGtExpr(boolTypeName, JcRawZero(rhv.typeName), rhv) - Opcodes.IFLE -> JcRawLeExpr(boolTypeName, JcRawZero(rhv.typeName), rhv) + Opcodes.IFEQ -> JcRawEqExpr(boolTypeName, rhv, JcRawZero(rhv.typeName)) + Opcodes.IFNE -> JcRawNeqExpr(boolTypeName, rhv, JcRawZero(rhv.typeName)) + Opcodes.IFLT -> JcRawLtExpr(boolTypeName, rhv, JcRawZero(rhv.typeName)) + Opcodes.IFGE -> JcRawGeExpr(boolTypeName, rhv, JcRawZero(rhv.typeName)) + Opcodes.IFGT -> JcRawGtExpr(boolTypeName, rhv, JcRawZero(rhv.typeName)) + Opcodes.IFLE -> JcRawLeExpr(boolTypeName, rhv, JcRawZero(rhv.typeName)) Opcodes.IF_ICMPEQ -> JcRawEqExpr(boolTypeName, pop(), rhv) Opcodes.IF_ICMPNE -> JcRawNeqExpr(boolTypeName, pop(), rhv) Opcodes.IF_ICMPLT -> JcRawLtExpr(boolTypeName, pop(), rhv) diff --git a/jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/InstructionsTest.kt b/jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/InstructionsTest.kt index c04f69e18..8709c579b 100644 --- a/jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/InstructionsTest.kt +++ b/jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/InstructionsTest.kt @@ -22,7 +22,12 @@ import org.jacodb.api.JcClassOrInterface import org.jacodb.api.JcClassProcessingTask import org.jacodb.api.JcMethod import org.jacodb.api.RegisteredLocation +import org.jacodb.api.cfg.JcArgument import org.jacodb.api.cfg.JcAssignInst +import org.jacodb.api.cfg.JcGeExpr +import org.jacodb.api.cfg.JcGtExpr +import org.jacodb.api.cfg.JcIfInst +import org.jacodb.api.cfg.JcInt import org.jacodb.api.cfg.JcLocalVar import org.jacodb.api.ext.cfg.callExpr import org.jacodb.api.ext.cfg.locals @@ -58,6 +63,27 @@ class InstructionsTest : BaseTest() { assertEquals("%1", (assign.rhv as JcLocalVar).name) } + @Test + fun `cmp insts`() { + val clazz = cp.findClass() + val method = clazz.declaredMethods.first { it.name == "main" } + val instructions = method.instList.instructions + val cmpExprs = instructions.filterIsInstance().map { it.condition } + assertEquals(4, cmpExprs.size) + + val geZero = cmpExprs[0] as JcGeExpr + assertEquals(0 to 0, (geZero.lhv as JcArgument).index to (geZero.rhv as JcInt).value) + + val gtZero = cmpExprs[1] as JcGtExpr + assertEquals(0 to 0, (gtZero.lhv as JcArgument).index to (gtZero.rhv as JcInt).value) + + val geOther = cmpExprs[2] as JcGeExpr + assertEquals(0 to 1, (geOther.lhv as JcArgument).index to (geOther.rhv as JcArgument).index) + + val gtOther = cmpExprs[3] as JcGtExpr + assertEquals(0 to 1, (gtOther.lhv as JcArgument).index to (gtOther.rhv as JcArgument).index) + } + @Test fun `null ref test`() { val clazz = cp.findClass() diff --git a/jacodb-core/src/testFixtures/java/org/jacodb/testing/cfg/Conditionals.java b/jacodb-core/src/testFixtures/java/org/jacodb/testing/cfg/Conditionals.java new file mode 100644 index 000000000..a8166af1a --- /dev/null +++ b/jacodb-core/src/testFixtures/java/org/jacodb/testing/cfg/Conditionals.java @@ -0,0 +1,34 @@ +/* + * Copyright 2022 UnitTestBot contributors (utbot.org) + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jacodb.testing.cfg; + +public class Conditionals { + void main(int x, int y) { + if (x < 0) { + System.out.println("< 0"); + } + if (x <= 0) { + System.out.println("<= 0"); + } + if (x < y) { + System.out.println("<"); + } + if (x <= y) { + System.out.println("<="); + } + } +}