Introduce AbstractForInExclusiveRangeLoopGenerator

This commit is contained in:
Dmitry Petrov
2017-05-16 10:45:08 +03:00
parent 8903504334
commit 793fb65666
2 changed files with 59 additions and 24 deletions
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen.forLoop
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Type
abstract class AbstractForInExclusiveRangeLoopGenerator(
codegen: ExpressionCodegen,
forExpression: KtForExpression
) : AbstractForInRangeLoopGenerator(codegen, forExpression) {
protected abstract fun generateFrom(): StackValue
protected abstract fun generateTo(): StackValue
override fun storeRangeStartAndEnd() {
loopParameter().store(generateFrom(), v)
StackValue.local(endVar, asmElementType).store(generateTo(), v)
}
override fun checkEmptyLoop(loopExit: Label) {}
override fun checkPreCondition(loopExit: Label) {
loopParameter().put(asmElementType, v)
v.load(endVar, asmElementType)
if (asmElementType.sort == Type.LONG) {
v.lcmp()
v.ifge(loopExit)
}
else {
v.ificmpge(loopExit)
}
}
override fun checkPostConditionAndIncrement(loopExit: Label) {
incrementLoopVariable()
}
}
@@ -22,37 +22,18 @@ import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Type
class ForInUntilRangeLoopGenerator(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
loopRangeCall: ResolvedCall<*>
) : AbstractForInRangeLoopGenerator(codegen, forExpression) {
) : AbstractForInExclusiveRangeLoopGenerator(codegen, forExpression) {
private val from: ReceiverValue = loopRangeCall.extensionReceiver!!
private val to: KtExpression = ExpressionCodegen.getSingleArgumentExpression(loopRangeCall)!!
override fun storeRangeStartAndEnd() {
loopParameter().store(codegen.generateReceiverValue(from, false), v)
StackValue.local(endVar, asmElementType).store(codegen.gen(to), v)
}
override fun generateFrom(): StackValue =
codegen.generateReceiverValue(from, false)
override fun checkEmptyLoop(loopExit: Label) {}
override fun checkPreCondition(loopExit: Label) {
loopParameter().put(asmElementType, v)
v.load(endVar, asmElementType)
if (asmElementType.sort == Type.LONG) {
v.lcmp()
v.ifge(loopExit)
}
else {
v.ificmpge(loopExit)
}
}
override fun checkPostConditionAndIncrement(loopExit: Label) {
incrementLoopVariable()
}
override fun generateTo(): StackValue =
codegen.gen(to)
}