From c7d97c63a8f7c0359b0469894ccba13c4eb748d6 Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Wed, 1 Jun 2016 15:11:30 +0300 Subject: [PATCH] Small refactoring in constant processing --- .../jetbrains/kotlin/codegen/BranchedValue.kt | 6 ++-- .../jetbrains/kotlin/codegen/StackValue.java | 8 +++-- .../common/OptimizationBasicInterpreter.java | 29 ------------------- .../codegen/bytecodeText/charConstant.kt | 6 ++++ .../bytecodeText/inlineJavaStaticFields.kt | 4 +-- .../codegen/BytecodeTextTestGenerated.java | 6 ++++ 6 files changed, 22 insertions(+), 37 deletions(-) create mode 100644 compiler/testData/codegen/bytecodeText/charConstant.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/BranchedValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/BranchedValue.kt index 5855be28cbd..a026a064759 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/BranchedValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/BranchedValue.kt @@ -32,8 +32,6 @@ open class BranchedValue( val opcode: Int ) : StackValue(Type.BOOLEAN_TYPE) { - constructor(or: BranchedValue, opcode: Int) : this(or.arg1, or.arg2, or.operandType, opcode) - override fun putSelector(type: Type, v: InstructionAdapter) { val branchJumpLabel = Label() condJump(branchJumpLabel, v, true) @@ -63,7 +61,7 @@ open class BranchedValue( companion object { val negatedOperations = hashMapOf() - val TRUE: BranchedValue = object : BranchedValue(StackValue.Constant(true, Type.BOOLEAN_TYPE), null, Type.BOOLEAN_TYPE, IFEQ) { + val TRUE: BranchedValue = object : BranchedValue(StackValue.none()/*not used*/, null, Type.BOOLEAN_TYPE, IFEQ) { override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) { if (!jumpIfFalse) { v.goTo(jumpLabel) @@ -85,7 +83,7 @@ open class BranchedValue( } } - val FALSE: BranchedValue = object : BranchedValue(StackValue.Constant(false, Type.BOOLEAN_TYPE), null, Type.BOOLEAN_TYPE, IFEQ) { + val FALSE: BranchedValue = object : BranchedValue(StackValue.none()/*not used*/, null, Type.BOOLEAN_TYPE, IFEQ) { override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) { if (jumpIfFalse) { v.goTo(jumpLabel) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java index 88cb4b9f083..c0d17868bd8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java @@ -770,12 +770,13 @@ public abstract class StackValue { } } - public static class Constant extends StackValue { + private static class Constant extends StackValue { @Nullable private final Object value; public Constant(@Nullable Object value, Type type) { super(type, false); + assert !Type.BOOLEAN_TYPE.equals(type) : "Boolean constants should be created via 'StackValue.constant'"; this.value = value; } @@ -784,6 +785,9 @@ public abstract class StackValue { if (value instanceof Integer || value instanceof Byte || value instanceof Short) { v.iconst(((Number) value).intValue()); } + else if (value instanceof Character) { + v.iconst(((Character) value).charValue()); + } else if (value instanceof Long) { v.lconst((Long) value); } @@ -1212,7 +1216,7 @@ public abstract class StackValue { value = ((Double) value).floatValue(); } - new Constant(value, this.type).putSelector(type, v); + StackValue.constant(value, this.type).putSelector(type, v); return true; } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/OptimizationBasicInterpreter.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/OptimizationBasicInterpreter.java index a74c20ecf5d..514de0a164d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/OptimizationBasicInterpreter.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/OptimizationBasicInterpreter.java @@ -57,35 +57,6 @@ public class OptimizationBasicInterpreter extends BasicInterpreter { } } - @Override - public BasicValue newOperation(@NotNull AbstractInsnNode insn) throws AnalyzerException { - if (insn.getOpcode() == Opcodes.LDC) { - Object cst = ((LdcInsnNode) insn).cst; - - if (cst instanceof Long) { - return BasicValue.LONG_VALUE; - } - - if (cst instanceof Boolean || - cst instanceof Integer || - cst instanceof Short || - cst instanceof Byte || - cst instanceof Character) { - return BasicValue.INT_VALUE; - } - - if (cst instanceof Float) { - return BasicValue.FLOAT_VALUE; - } - - if (cst instanceof Double) { - return BasicValue.DOUBLE_VALUE; - } - } - - return super.newOperation(insn); - } - @NotNull @Override public BasicValue merge( diff --git a/compiler/testData/codegen/bytecodeText/charConstant.kt b/compiler/testData/codegen/bytecodeText/charConstant.kt new file mode 100644 index 00000000000..961b4235fae --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/charConstant.kt @@ -0,0 +1,6 @@ +fun test() { + val z = 'A' +} + +//1 BIPUSH 65 +//0 LDC A \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt b/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt index 72c0ae99bce..4d864d0b62a 100644 --- a/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt +++ b/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt @@ -65,10 +65,10 @@ fun test() { // 1 SIPUSH 9000 // 1 LDC 59000 // 1 BIPUSH -8 -// 1 LDC K +// 1 BIPUSH 75 // 1 LDC 100000 // 1 SIPUSH 901 -// 1 LDC false +// 1 ICONST_0 // 1 LDC 36.6 // 1 LDC 42.4242 // 1 LDC ":J" diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 0592ebee743..ac82ecabe8f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -95,6 +95,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { doTest(fileName); } + @TestMetadata("charConstant.kt") + public void testCharConstant() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/charConstant.kt"); + doTest(fileName); + } + @TestMetadata("componentEvaluatesOnlyOnce.kt") public void testComponentEvaluatesOnlyOnce() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/componentEvaluatesOnlyOnce.kt");