Skip to content

Commit c7d97c6

Browse files
author
Mikhael Bogdanov
committed
Small refactoring in constant processing
1 parent 9b3f40e commit c7d97c6

File tree

6 files changed

+22
-37
lines changed

6 files changed

+22
-37
lines changed

compiler/backend/src/org/jetbrains/kotlin/codegen/BranchedValue.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ open class BranchedValue(
3232
val opcode: Int
3333
) : StackValue(Type.BOOLEAN_TYPE) {
3434

35-
constructor(or: BranchedValue, opcode: Int) : this(or.arg1, or.arg2, or.operandType, opcode)
36-
3735
override fun putSelector(type: Type, v: InstructionAdapter) {
3836
val branchJumpLabel = Label()
3937
condJump(branchJumpLabel, v, true)
@@ -63,7 +61,7 @@ open class BranchedValue(
6361
companion object {
6462
val negatedOperations = hashMapOf<Int, Int>()
6563

66-
val TRUE: BranchedValue = object : BranchedValue(StackValue.Constant(true, Type.BOOLEAN_TYPE), null, Type.BOOLEAN_TYPE, IFEQ) {
64+
val TRUE: BranchedValue = object : BranchedValue(StackValue.none()/*not used*/, null, Type.BOOLEAN_TYPE, IFEQ) {
6765
override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) {
6866
if (!jumpIfFalse) {
6967
v.goTo(jumpLabel)
@@ -85,7 +83,7 @@ open class BranchedValue(
8583
}
8684
}
8785

88-
val FALSE: BranchedValue = object : BranchedValue(StackValue.Constant(false, Type.BOOLEAN_TYPE), null, Type.BOOLEAN_TYPE, IFEQ) {
86+
val FALSE: BranchedValue = object : BranchedValue(StackValue.none()/*not used*/, null, Type.BOOLEAN_TYPE, IFEQ) {
8987
override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) {
9088
if (jumpIfFalse) {
9189
v.goTo(jumpLabel)

compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,12 +770,13 @@ else if (size == 2) {
770770
}
771771
}
772772

773-
public static class Constant extends StackValue {
773+
private static class Constant extends StackValue {
774774
@Nullable
775775
private final Object value;
776776

777777
public Constant(@Nullable Object value, Type type) {
778778
super(type, false);
779+
assert !Type.BOOLEAN_TYPE.equals(type) : "Boolean constants should be created via 'StackValue.constant'";
779780
this.value = value;
780781
}
781782

@@ -784,6 +785,9 @@ public void putSelector(@NotNull Type type, @NotNull InstructionAdapter v) {
784785
if (value instanceof Integer || value instanceof Byte || value instanceof Short) {
785786
v.iconst(((Number) value).intValue());
786787
}
788+
else if (value instanceof Character) {
789+
v.iconst(((Character) value).charValue());
790+
}
787791
else if (value instanceof Long) {
788792
v.lconst((Long) value);
789793
}
@@ -1212,7 +1216,7 @@ private boolean inlineJavaConstantIfNeeded(@NotNull Type type, @NotNull Instruct
12121216
value = ((Double) value).floatValue();
12131217
}
12141218

1215-
new Constant(value, this.type).putSelector(type, v);
1219+
StackValue.constant(value, this.type).putSelector(type, v);
12161220

12171221
return true;
12181222
}

compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/OptimizationBasicInterpreter.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,6 @@ public BasicValue newValue(@Nullable Type type) {
5757
}
5858
}
5959

60-
@Override
61-
public BasicValue newOperation(@NotNull AbstractInsnNode insn) throws AnalyzerException {
62-
if (insn.getOpcode() == Opcodes.LDC) {
63-
Object cst = ((LdcInsnNode) insn).cst;
64-
65-
if (cst instanceof Long) {
66-
return BasicValue.LONG_VALUE;
67-
}
68-
69-
if (cst instanceof Boolean ||
70-
cst instanceof Integer ||
71-
cst instanceof Short ||
72-
cst instanceof Byte ||
73-
cst instanceof Character) {
74-
return BasicValue.INT_VALUE;
75-
}
76-
77-
if (cst instanceof Float) {
78-
return BasicValue.FLOAT_VALUE;
79-
}
80-
81-
if (cst instanceof Double) {
82-
return BasicValue.DOUBLE_VALUE;
83-
}
84-
}
85-
86-
return super.newOperation(insn);
87-
}
88-
8960
@NotNull
9061
@Override
9162
public BasicValue merge(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fun test() {
2+
val z = 'A'
3+
}
4+
5+
//1 BIPUSH 65
6+
//0 LDC A

compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ fun test() {
6565
// 1 SIPUSH 9000
6666
// 1 LDC 59000
6767
// 1 BIPUSH -8
68-
// 1 LDC K
68+
// 1 BIPUSH 75
6969
// 1 LDC 100000
7070
// 1 SIPUSH 901
71-
// 1 LDC false
71+
// 1 ICONST_0
7272
// 1 LDC 36.6
7373
// 1 LDC 42.4242
7474
// 1 LDC ":J"

compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ public void testCallableReferenceInline() throws Exception {
9595
doTest(fileName);
9696
}
9797

98+
@TestMetadata("charConstant.kt")
99+
public void testCharConstant() throws Exception {
100+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/charConstant.kt");
101+
doTest(fileName);
102+
}
103+
98104
@TestMetadata("componentEvaluatesOnlyOnce.kt")
99105
public void testComponentEvaluatesOnlyOnce() throws Exception {
100106
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/componentEvaluatesOnlyOnce.kt");

0 commit comments

Comments
 (0)