JVM: Fix constant folding for unsigned values
This commit is contained in:
committed by
Dmitry Petrov
parent
732405895f
commit
d3d4e94cd6
+6
@@ -7172,6 +7172,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
|
||||
+4
-2
@@ -12,6 +12,7 @@ import com.intellij.psi.util.TypeConversionUtil
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.builtins.UnsignedType
|
||||
import org.jetbrains.kotlin.builtins.UnsignedTypes
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
@@ -401,7 +402,7 @@ private class ConstantExpressionEvaluatorVisitor(
|
||||
return when (constantValue) {
|
||||
is ErrorValue, is EnumValue -> return null
|
||||
is NullValue -> StringValue("null")
|
||||
else -> StringValue(constantValue.stringTemplateValue())
|
||||
else -> StringValue(constantValue.boxedValue().toString())
|
||||
}.wrap(compileTimeConstant.parameters)
|
||||
}
|
||||
|
||||
@@ -984,7 +985,8 @@ private class ConstantExpressionEvaluatorVisitor(
|
||||
): OperationArgument? {
|
||||
val compileTimeConstant = constantExpressionEvaluator.evaluateExpression(expression, trace, parameterType) ?: return null
|
||||
if (compileTimeConstant is TypedCompileTimeConstant && !compileTimeConstant.type.isSubtypeOf(parameterType)) return null
|
||||
val evaluationResult = compileTimeConstant.getValue(parameterType) ?: return null
|
||||
val constantValue = compileTimeConstant.toConstantValue(parameterType)
|
||||
val evaluationResult = (if (compileTimeType == ANY) constantValue.boxedValue() else constantValue.value) ?: return null
|
||||
return OperationArgument(evaluationResult, compileTimeType, expression)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
// IGNORE_BACKEND: JVM
|
||||
// IGNORE_BACKEND: WASM
|
||||
|
||||
val a = "INT " + 0x8fffffffU
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// WITH_RUNTIME
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// IGNORE_BACKEND: WASM
|
||||
|
||||
const val a = "INT " + 0x8fffffffU
|
||||
const val b = "BYTE " + 0x8ffU
|
||||
const val c = "LONG " + 0xffff_ffff_ffffU
|
||||
|
||||
const val uint = 0x8fffffffU
|
||||
const val ubyte = 0x8ffU
|
||||
const val ulong = 0xffff_ffff_ffffU
|
||||
|
||||
const val aa = "INT " + uint
|
||||
const val bb = "BYTE " + ubyte
|
||||
const val cc = "LONG " + ulong
|
||||
|
||||
|
||||
fun box(): String {
|
||||
if (a != "INT 2415919103") {
|
||||
return "FAIL 0: $a"
|
||||
}
|
||||
if (aa != "INT 2415919103") {
|
||||
return "FAIL 1: $aa"
|
||||
}
|
||||
|
||||
if (b != "BYTE 2303") {
|
||||
return "FAIL 2: $b"
|
||||
}
|
||||
if (bb != "BYTE 2303") {
|
||||
return "FAIL 3: $bb"
|
||||
}
|
||||
|
||||
|
||||
if (c != "LONG 281474976710655") {
|
||||
return "FAIL 4: $c"
|
||||
}
|
||||
if (cc != "LONG 281474976710655") {
|
||||
return "FAIL 5: $cc"
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+6
@@ -7172,6 +7172,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
|
||||
+6
@@ -7172,6 +7172,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
|
||||
+10
-5
@@ -5387,11 +5387,6 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Constants extends AbstractLightAnalysisModeTest {
|
||||
@TestMetadata("foldingBinaryOpsUnsigned.kt")
|
||||
public void ignoreFoldingBinaryOpsUnsigned() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
|
||||
}
|
||||
@@ -5430,6 +5425,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/constants/float.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("foldingBinaryOpsUnsigned.kt")
|
||||
public void testFoldingBinaryOpsUnsigned() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/kt9532.kt");
|
||||
|
||||
@@ -43,7 +43,7 @@ abstract class ConstantValue<out T>(open val value: T) {
|
||||
|
||||
override fun toString(): String = value.toString()
|
||||
|
||||
open fun stringTemplateValue(): String = value.toString()
|
||||
open fun boxedValue(): Any? = value
|
||||
}
|
||||
|
||||
abstract class IntegerValueConstant<out T> protected constructor(value: T) : ConstantValue<T>(value)
|
||||
@@ -269,7 +269,7 @@ class UByteValue(byteValue: Byte) : UnsignedValueConstant<Byte>(byteValue) {
|
||||
|
||||
override fun toString() = "$value.toUByte()"
|
||||
|
||||
override fun stringTemplateValue(): String = (value.toInt() and 0xFF).toString()
|
||||
override fun boxedValue(): Any = value.toUByte()
|
||||
}
|
||||
|
||||
class UShortValue(shortValue: Short) : UnsignedValueConstant<Short>(shortValue) {
|
||||
@@ -282,7 +282,7 @@ class UShortValue(shortValue: Short) : UnsignedValueConstant<Short>(shortValue)
|
||||
|
||||
override fun toString() = "$value.toUShort()"
|
||||
|
||||
override fun stringTemplateValue(): String = (value.toInt() and 0xFFFF).toString()
|
||||
override fun boxedValue(): Any = value.toUShort()
|
||||
}
|
||||
|
||||
class UIntValue(intValue: Int) : UnsignedValueConstant<Int>(intValue) {
|
||||
@@ -295,7 +295,7 @@ class UIntValue(intValue: Int) : UnsignedValueConstant<Int>(intValue) {
|
||||
|
||||
override fun toString() = "$value.toUInt()"
|
||||
|
||||
override fun stringTemplateValue(): String = (value.toLong() and 0xFFFFFFFFL).toString()
|
||||
override fun boxedValue(): Any = value.toUInt()
|
||||
}
|
||||
|
||||
class ULongValue(longValue: Long) : UnsignedValueConstant<Long>(longValue) {
|
||||
@@ -308,12 +308,5 @@ class ULongValue(longValue: Long) : UnsignedValueConstant<Long>(longValue) {
|
||||
|
||||
override fun toString() = "$value.toULong()"
|
||||
|
||||
override fun stringTemplateValue(): String {
|
||||
if (value >= 0) return value.toString()
|
||||
|
||||
val div10 = (value ushr 1) / 5
|
||||
val mod10 = value - 10 * div10
|
||||
|
||||
return "$div10$mod10"
|
||||
}
|
||||
override fun boxedValue(): Any = value.toULong()
|
||||
}
|
||||
|
||||
js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java
Generated
+5
@@ -4839,6 +4839,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/kt9532.kt");
|
||||
|
||||
Generated
+5
@@ -4250,6 +4250,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/kt9532.kt");
|
||||
|
||||
Generated
+5
@@ -4250,6 +4250,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt9532.kt")
|
||||
public void testKt9532() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/kt9532.kt");
|
||||
|
||||
js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java
Generated
+5
@@ -3061,6 +3061,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsigned.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("foldingBinaryOpsUnsignedConst.kt")
|
||||
public void testFoldingBinaryOpsUnsignedConst() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/foldingBinaryOpsUnsignedConst.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("long.kt")
|
||||
public void testLong() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/constants/long.kt");
|
||||
|
||||
Reference in New Issue
Block a user