diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/ArrayIndicesRangeValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/ArrayIndicesRangeValue.kt index 35a386d9ca0..644d5afdde0 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/ArrayIndicesRangeValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/ArrayIndicesRangeValue.kt @@ -19,6 +19,8 @@ package org.jetbrains.kotlin.codegen.range import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.generateCallReceiver +import org.jetbrains.kotlin.codegen.range.comparison.IntComparisonGenerator +import org.jetbrains.kotlin.codegen.range.comparison.getComparisonGeneratorForKotlinType import org.jetbrains.kotlin.codegen.range.forLoop.ForInDefinitelySafeSimpleProgressionLoopGenerator import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator import org.jetbrains.kotlin.codegen.range.forLoop.ForLoopGenerator @@ -36,7 +38,7 @@ class ArrayIndicesRangeValue(rangeCall: ResolvedCall) : override fun getBoundedValue(codegen: ExpressionCodegen) = SimpleBoundedValue( codegen.asmType(rangeCall.resultingDescriptor.returnType!!), - StackValue.constant(0, asmElementType), + StackValue.constant(0, elementType), true, StackValue.operation(Type.INT_TYPE) { v -> codegen.generateCallReceiver(rangeCall).put(codegen.asmType(expectedReceiverType), expectedReceiverType, v) @@ -46,10 +48,12 @@ class ArrayIndicesRangeValue(rangeCall: ResolvedCall) : ) override fun createForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression) = - ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStep1(codegen, forExpression, getBoundedValue(codegen)) + ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStep1( + codegen, forExpression, getBoundedValue(codegen), IntComparisonGenerator + ) override fun createForInReversedLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): ForLoopGenerator = ForInDefinitelySafeSimpleProgressionLoopGenerator.fromBoundedValueWithStepMinus1( - codegen, forExpression, getBoundedValue(codegen) + codegen, forExpression, getBoundedValue(codegen), IntComparisonGenerator ) } \ No newline at end of file diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/CharSequenceIndicesRangeValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/CharSequenceIndicesRangeValue.kt index 42f3e08d17f..dd97f79dd3f 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/CharSequenceIndicesRangeValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/CharSequenceIndicesRangeValue.kt @@ -19,6 +19,8 @@ package org.jetbrains.kotlin.codegen.range import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.generateCallReceiver +import org.jetbrains.kotlin.codegen.range.comparison.IntComparisonGenerator +import org.jetbrains.kotlin.codegen.range.comparison.getComparisonGeneratorForKotlinType import org.jetbrains.kotlin.codegen.range.forLoop.ForInDefinitelySafeSimpleProgressionLoopGenerator import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator import org.jetbrains.kotlin.descriptors.CallableDescriptor @@ -35,7 +37,7 @@ class CharSequenceIndicesRangeValue(rangeCall: ResolvedCall codegen.generateCallReceiver(rangeCall).put(codegen.asmType(expectedReceiverType), expectedReceiverType, v) @@ -45,10 +47,12 @@ class CharSequenceIndicesRangeValue(rangeCall: ResolvedCall codegen.generateCallReceiver(rangeCall).put(codegen.asmType(expectedReceiverType), expectedReceiverType, v) @@ -45,10 +47,12 @@ class CollectionIndicesRangeValue(rangeCall: ResolvedCall ) : CallIntrinsicRangeValue(rangeCall) { - protected val asmElementType = getAsmRangeElementTypeForPrimitiveRangeOrProgression(rangeCall.resultingDescriptor) + + protected val elementKotlinType = + rangeCall.resultingDescriptor.returnType?.let { getRangeOrProgressionElementType(it) } + ?: throw AssertionError("Unexpected range ") + + protected val elementType = getAsmRangeElementTypeForPrimitiveRangeOrProgression(rangeCall.resultingDescriptor) override fun isIntrinsicInCall(resolvedCallForIn: ResolvedCall) = resolvedCallForIn.resultingDescriptor.let { @@ -138,8 +145,9 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue( codegen, forExpression, startValue = startValue, isStartInclusive = isStartInclusive, - endValue = StackValue.integerConstant(endIntValue, asmElementType), + endValue = StackValue.integerConstant(endIntValue, elementType), isEndInclusive = true, + comparisonGenerator = getComparisonGeneratorForKotlinType(elementKotlinType), step = step ) @@ -155,8 +163,9 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue( codegen, forExpression, startValue = startValue, isStartInclusive = isStartInclusive, - endValue = StackValue.constant(endLongValue, asmElementType), + endValue = StackValue.constant(endLongValue, elementType), isEndInclusive = true, + comparisonGenerator = getComparisonGeneratorForKotlinType(elementKotlinType), step = step ) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeLiteralRangeValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeLiteralRangeValue.kt index 58757914011..fd47e2b2cc4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeLiteralRangeValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeLiteralRangeValue.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen.range import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.generateCallReceiver import org.jetbrains.kotlin.codegen.generateCallSingleArgument +import org.jetbrains.kotlin.codegen.range.comparison.getComparisonGeneratorForKotlinType import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator import org.jetbrains.kotlin.codegen.range.forLoop.ForLoopGenerator import org.jetbrains.kotlin.descriptors.CallableDescriptor @@ -51,12 +52,16 @@ class PrimitiveNumberRangeLiteralRangeValue( override fun createForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): ForLoopGenerator = createConstBoundedForInRangeLiteralGenerator(codegen, forExpression) - ?: ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStep1(codegen, forExpression, getBoundedValue(codegen)) + ?: ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStep1( + codegen, forExpression, getBoundedValue(codegen), + getComparisonGeneratorForKotlinType(elementKotlinType) + ) override fun createForInReversedLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): ForLoopGenerator = createConstBoundedRangeForInReversedRangeLiteralGenerator(codegen, forExpression) ?: ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStepMinus1( codegen, forExpression, getBoundedValue(codegen), + getComparisonGeneratorForKotlinType(elementKotlinType), inverseBoundsEvaluationOrder = true ) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt index dd7c117209e..f3e18b7e162 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen.range import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.generateCallReceiver import org.jetbrains.kotlin.codegen.generateCallSingleArgument +import org.jetbrains.kotlin.codegen.range.comparison.getComparisonGeneratorForKotlinType import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator import org.jetbrains.kotlin.codegen.range.forLoop.ForLoopGenerator import org.jetbrains.kotlin.descriptors.CallableDescriptor @@ -39,12 +40,16 @@ class PrimitiveNumberUntilRangeValue(rangeCall: ResolvedCall CharComparisonGenerator - type.isPrimitiveIntOrCoercible() -> IntComparisonGenerator - type == Type.LONG_TYPE -> LongComparisonGenerator - type == Type.FLOAT_TYPE -> FloatComparisonGenerator - type == Type.DOUBLE_TYPE -> DoubleComparisonGenerator - else -> throw UnsupportedOperationException("Unexpected primitive type: " + type) + KotlinBuiltIns.isChar(kotlinType) -> + CharComparisonGenerator + KotlinBuiltIns.isByte(kotlinType) || KotlinBuiltIns.isShort(kotlinType) || KotlinBuiltIns.isInt(kotlinType) -> + IntComparisonGenerator + KotlinBuiltIns.isLong(kotlinType) -> + LongComparisonGenerator + KotlinBuiltIns.isFloat(kotlinType) -> + FloatComparisonGenerator + KotlinBuiltIns.isDouble(kotlinType) -> + DoubleComparisonGenerator + else -> + throw UnsupportedOperationException("Unexpected element type: $kotlinType") } fun getComparisonGeneratorForRangeContainsCall( @@ -60,7 +68,7 @@ fun getComparisonGeneratorForRangeContainsCall( return when { asmElementType == asmValueParameterType -> - getComparisonGeneratorForPrimitiveType(asmElementType) + getComparisonGeneratorForKotlinType(elementType) asmElementType.isPrimitiveIntOrCoercible() && asmValueParameterType.isPrimitiveIntOrCoercible() -> IntComparisonGenerator diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/FloatingPointComparisonGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/FloatingPointComparisonGenerator.kt index a9e5e71fa81..9079e3ba774 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/FloatingPointComparisonGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/FloatingPointComparisonGenerator.kt @@ -20,7 +20,7 @@ import org.jetbrains.org.objectweb.asm.Label import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter -sealed class FloatingPointComparisonGenerator(override val comparedType: Type) : ComparisonGenerator { +class FloatingPointComparisonGenerator(override val comparedType: Type) : ComparisonGenerator { override fun jumpIfGreaterOrEqual(v: InstructionAdapter, label: Label) { v.cmpg(comparedType) v.ifge(label) @@ -42,6 +42,5 @@ sealed class FloatingPointComparisonGenerator(override val comparedType: Type) : } } -object FloatComparisonGenerator : FloatingPointComparisonGenerator(Type.FLOAT_TYPE) - -object DoubleComparisonGenerator : FloatingPointComparisonGenerator(Type.DOUBLE_TYPE) \ No newline at end of file +val FloatComparisonGenerator = FloatingPointComparisonGenerator(Type.FLOAT_TYPE) +val DoubleComparisonGenerator = FloatingPointComparisonGenerator(Type.DOUBLE_TYPE) \ No newline at end of file diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt index 6926efff022..269b4e0d75a 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt @@ -20,7 +20,7 @@ import org.jetbrains.org.objectweb.asm.Label import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter -abstract class IntegerComparisonGenerator(override val comparedType: Type) : ComparisonGenerator { +class IntegerComparisonGenerator(override val comparedType: Type) : ComparisonGenerator { override fun jumpIfGreaterOrEqual(v: InstructionAdapter, label: Label) { v.ificmpge(label) } @@ -38,6 +38,5 @@ abstract class IntegerComparisonGenerator(override val comparedType: Type) : Com } } -object IntComparisonGenerator : IntegerComparisonGenerator(Type.INT_TYPE) - -object CharComparisonGenerator : IntegerComparisonGenerator(Type.CHAR_TYPE) \ No newline at end of file +val IntComparisonGenerator = IntegerComparisonGenerator(Type.INT_TYPE) +val CharComparisonGenerator = IntegerComparisonGenerator(Type.CHAR_TYPE) \ No newline at end of file diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForInRangeLoopGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForInRangeLoopGenerator.kt index d090bb1ee2d..469b1f98ee8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForInRangeLoopGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForInRangeLoopGenerator.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen.range.forLoop import org.jetbrains.kotlin.codegen.AsmUtil.genIncrement import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.StackValue +import org.jetbrains.kotlin.codegen.range.comparison.ComparisonGenerator import org.jetbrains.kotlin.psi.KtForExpression import org.jetbrains.org.objectweb.asm.Label import org.jetbrains.org.objectweb.asm.Type @@ -26,7 +27,8 @@ import org.jetbrains.org.objectweb.asm.Type abstract class AbstractForInRangeLoopGenerator( codegen: ExpressionCodegen, forExpression: KtForExpression, - protected val step: Int + protected val step: Int, + protected val comparisonGenerator: ComparisonGenerator ) : AbstractForInProgressionOrRangeLoopGenerator(codegen, forExpression) { override fun beforeLoop() { @@ -40,19 +42,11 @@ abstract class AbstractForInRangeLoopGenerator( override fun checkEmptyLoop(loopExit: Label) { loopParameter().put(asmElementType, elementType, v) v.load(endVar, asmElementType) - if (asmElementType.sort == Type.LONG) { - v.lcmp() - if (step > 0) { - v.ifgt(loopExit) - } else { - v.iflt(loopExit) - } + + if (step > 0) { + comparisonGenerator.jumpIfGreater(v, loopExit) } else { - if (step > 0) { - v.ificmpgt(loopExit) - } else { - v.ificmplt(loopExit) - } + comparisonGenerator.jumpIfLess(v, loopExit) } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInDefinitelySafeSimpleProgressionLoopGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInDefinitelySafeSimpleProgressionLoopGenerator.kt index 6c1ee27c1e2..4a59578c60b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInDefinitelySafeSimpleProgressionLoopGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInDefinitelySafeSimpleProgressionLoopGenerator.kt @@ -19,9 +19,9 @@ package org.jetbrains.kotlin.codegen.range.forLoop import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.range.SimpleBoundedValue +import org.jetbrains.kotlin.codegen.range.comparison.ComparisonGenerator import org.jetbrains.kotlin.psi.KtForExpression import org.jetbrains.org.objectweb.asm.Label -import org.jetbrains.org.objectweb.asm.Type /** * Generates "naive" for loop: @@ -45,13 +45,15 @@ class ForInDefinitelySafeSimpleProgressionLoopGenerator( private val isStartInclusive: Boolean, private val endValue: StackValue, private val isEndInclusive: Boolean, + comparisonGenerator: ComparisonGenerator, step: Int -) : AbstractForInRangeLoopGenerator(codegen, forExpression, step) { +) : AbstractForInRangeLoopGenerator(codegen, forExpression, step, comparisonGenerator) { constructor( codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue, + comparisonGenerator: ComparisonGenerator, step: Int ) : this( codegen, forExpression, @@ -59,15 +61,26 @@ class ForInDefinitelySafeSimpleProgressionLoopGenerator( isStartInclusive = if (step == 1) boundedValue.isLowInclusive else boundedValue.isHighInclusive, endValue = if (step == 1) boundedValue.highBound else boundedValue.lowBound, isEndInclusive = if (step == 1) boundedValue.isHighInclusive else boundedValue.isLowInclusive, + comparisonGenerator = comparisonGenerator, step = step ) companion object { - fun fromBoundedValueWithStep1(codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue) = - ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, 1) + fun fromBoundedValueWithStep1( + codegen: ExpressionCodegen, + forExpression: KtForExpression, + boundedValue: SimpleBoundedValue, + comparisonGenerator: ComparisonGenerator + ) = + ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, comparisonGenerator, 1) - fun fromBoundedValueWithStepMinus1(codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue) = - ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, -1) + fun fromBoundedValueWithStepMinus1( + codegen: ExpressionCodegen, + forExpression: KtForExpression, + boundedValue: SimpleBoundedValue, + comparisonGenerator: ComparisonGenerator + ) = + ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, comparisonGenerator, -1) } override fun storeRangeStartAndEnd() { @@ -84,35 +97,17 @@ class ForInDefinitelySafeSimpleProgressionLoopGenerator( override fun checkPreCondition(loopExit: Label) { loopParameter().put(asmElementType, elementType, v) v.load(endVar, asmElementType) - if (asmElementType.sort == Type.LONG) { - v.lcmp() - if (step > 0) { - if (isEndInclusive) { - v.ifgt(loopExit) - } else { - v.ifge(loopExit) - } - } else { - if (isEndInclusive) { - v.iflt(loopExit) - } else { - v.ifle(loopExit) - } - } + + if (step > 0) { + if (isEndInclusive) + comparisonGenerator.jumpIfGreater(v, loopExit) + else + comparisonGenerator.jumpIfGreaterOrEqual(v, loopExit) } else { - if (step > 0) { - if (isEndInclusive) { - v.ificmpgt(loopExit) - } else { - v.ificmpge(loopExit) - } - } else { - if (isEndInclusive) { - v.ificmplt(loopExit) - } else { - v.ificmple(loopExit) - } - } + if (isEndInclusive) + comparisonGenerator.jumpIfLess(v, loopExit) + else + comparisonGenerator.jumpIfLessOrEqual(v, loopExit) } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInRangeInstanceLoopGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInRangeInstanceLoopGenerator.kt index c3118e9537c..42740c3144f 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInRangeInstanceLoopGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInRangeInstanceLoopGenerator.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.codegen.range.forLoop import org.jetbrains.kotlin.codegen.ExpressionCodegen +import org.jetbrains.kotlin.codegen.range.comparison.ComparisonGenerator import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.psi.KtForExpression @@ -24,8 +25,9 @@ class ForInRangeInstanceLoopGenerator( codegen: ExpressionCodegen, forExpression: KtForExpression, private val rangeExpression: KtExpression, + comparisonGenerator: ComparisonGenerator, private val reversed: Boolean -) : AbstractForInRangeLoopGenerator(codegen, forExpression, if (reversed) -1 else 1) { +) : AbstractForInRangeLoopGenerator(codegen, forExpression, if (reversed) -1 else 1, comparisonGenerator) { override fun storeRangeStartAndEnd() { val loopRangeType = codegen.bindingContext.getType(rangeExpression)!! diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInSimpleProgressionLoopGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInSimpleProgressionLoopGenerator.kt index e8c14ab9c5c..11f0173383e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInSimpleProgressionLoopGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInSimpleProgressionLoopGenerator.kt @@ -19,9 +19,9 @@ package org.jetbrains.kotlin.codegen.range.forLoop import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.range.SimpleBoundedValue +import org.jetbrains.kotlin.codegen.range.comparison.ComparisonGenerator import org.jetbrains.kotlin.psi.KtForExpression import org.jetbrains.org.objectweb.asm.Label -import org.jetbrains.org.objectweb.asm.Type class ForInSimpleProgressionLoopGenerator( codegen: ExpressionCodegen, @@ -31,14 +31,16 @@ class ForInSimpleProgressionLoopGenerator( private val endValue: StackValue, private val isEndInclusive: Boolean, private val inverseBoundsEvaluationOrder: Boolean, + comparisonGenerator: ComparisonGenerator, step: Int -) : AbstractForInRangeLoopGenerator(codegen, forExpression, step) { +) : AbstractForInRangeLoopGenerator(codegen, forExpression, step, comparisonGenerator) { constructor( codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue, inverseBoundsEvaluationOrder: Boolean, + comparisonGenerator: ComparisonGenerator, step: Int ) : this( codegen, forExpression, @@ -47,6 +49,7 @@ class ForInSimpleProgressionLoopGenerator( endValue = if (step == 1) boundedValue.highBound else boundedValue.lowBound, isEndInclusive = if (step == 1) boundedValue.isHighInclusive else boundedValue.isLowInclusive, inverseBoundsEvaluationOrder = inverseBoundsEvaluationOrder, + comparisonGenerator = comparisonGenerator, step = step ) @@ -55,17 +58,19 @@ class ForInSimpleProgressionLoopGenerator( codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue, + comparisonGenerator: ComparisonGenerator, inverseBoundsEvaluationOrder: Boolean = false ) = - ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, inverseBoundsEvaluationOrder, 1) + ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, inverseBoundsEvaluationOrder, comparisonGenerator, 1) fun fromBoundedValueWithStepMinus1( codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue, + comparisonGenerator: ComparisonGenerator, inverseBoundsEvaluationOrder: Boolean = false ) = - ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, inverseBoundsEvaluationOrder, -1) + ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, inverseBoundsEvaluationOrder, comparisonGenerator, -1) } override fun storeRangeStartAndEnd() { @@ -93,20 +98,10 @@ class ForInSimpleProgressionLoopGenerator( if (!isEndInclusive) { loopParameter().put(asmElementType, elementType, v) v.load(endVar, asmElementType) - if (asmElementType.sort == Type.LONG) { - v.lcmp() - if (step > 0) { - v.ifge(loopExit) - } else { - v.ifle(loopExit) - } - } else { - if (step > 0) { - v.ificmpge(loopExit) - } else { - v.ificmple(loopExit) - } - } + if (step > 0) + comparisonGenerator.jumpIfGreaterOrEqual(v, loopExit) + else + comparisonGenerator.jumpIfLessOrEqual(v, loopExit) } }