Small refactoring in constant processing

This commit is contained in:
Mikhael Bogdanov
2016-06-01 15:11:30 +03:00
parent 9b3f40ee76
commit c7d97c63a8
6 changed files with 22 additions and 37 deletions
@@ -32,8 +32,6 @@ open class BranchedValue(
val opcode: Int val opcode: Int
) : StackValue(Type.BOOLEAN_TYPE) { ) : StackValue(Type.BOOLEAN_TYPE) {
constructor(or: BranchedValue, opcode: Int) : this(or.arg1, or.arg2, or.operandType, opcode)
override fun putSelector(type: Type, v: InstructionAdapter) { override fun putSelector(type: Type, v: InstructionAdapter) {
val branchJumpLabel = Label() val branchJumpLabel = Label()
condJump(branchJumpLabel, v, true) condJump(branchJumpLabel, v, true)
@@ -63,7 +61,7 @@ open class BranchedValue(
companion object { companion object {
val negatedOperations = hashMapOf<Int, Int>() val negatedOperations = hashMapOf<Int, Int>()
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) { override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) {
if (!jumpIfFalse) { if (!jumpIfFalse) {
v.goTo(jumpLabel) 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) { override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) {
if (jumpIfFalse) { if (jumpIfFalse) {
v.goTo(jumpLabel) v.goTo(jumpLabel)
@@ -770,12 +770,13 @@ public abstract class StackValue {
} }
} }
public static class Constant extends StackValue { private static class Constant extends StackValue {
@Nullable @Nullable
private final Object value; private final Object value;
public Constant(@Nullable Object value, Type type) { public Constant(@Nullable Object value, Type type) {
super(type, false); super(type, false);
assert !Type.BOOLEAN_TYPE.equals(type) : "Boolean constants should be created via 'StackValue.constant'";
this.value = value; this.value = value;
} }
@@ -784,6 +785,9 @@ public abstract class StackValue {
if (value instanceof Integer || value instanceof Byte || value instanceof Short) { if (value instanceof Integer || value instanceof Byte || value instanceof Short) {
v.iconst(((Number) value).intValue()); v.iconst(((Number) value).intValue());
} }
else if (value instanceof Character) {
v.iconst(((Character) value).charValue());
}
else if (value instanceof Long) { else if (value instanceof Long) {
v.lconst((Long) value); v.lconst((Long) value);
} }
@@ -1212,7 +1216,7 @@ public abstract class StackValue {
value = ((Double) value).floatValue(); value = ((Double) value).floatValue();
} }
new Constant(value, this.type).putSelector(type, v); StackValue.constant(value, this.type).putSelector(type, v);
return true; return true;
} }
@@ -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 @NotNull
@Override @Override
public BasicValue merge( public BasicValue merge(
@@ -0,0 +1,6 @@
fun test() {
val z = 'A'
}
//1 BIPUSH 65
//0 LDC A
@@ -65,10 +65,10 @@ fun test() {
// 1 SIPUSH 9000 // 1 SIPUSH 9000
// 1 LDC 59000 // 1 LDC 59000
// 1 BIPUSH -8 // 1 BIPUSH -8
// 1 LDC K // 1 BIPUSH 75
// 1 LDC 100000 // 1 LDC 100000
// 1 SIPUSH 901 // 1 SIPUSH 901
// 1 LDC false // 1 ICONST_0
// 1 LDC 36.6 // 1 LDC 36.6
// 1 LDC 42.4242 // 1 LDC 42.4242
// 1 LDC ":J" // 1 LDC ":J"
@@ -95,6 +95,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName); 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") @TestMetadata("componentEvaluatesOnlyOnce.kt")
public void testComponentEvaluatesOnlyOnce() throws Exception { public void testComponentEvaluatesOnlyOnce() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/componentEvaluatesOnlyOnce.kt"); String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/componentEvaluatesOnlyOnce.kt");