All Downloads are FREE. Search and download functionalities are using the official Maven repository.

.kotlin.kotlin-compiler.1.3.11.source-code.ForInDefinitelySafeSimpleProgressionLoopGenerator.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * 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.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.psi.KtForExpression
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Type

/**
 * Generates "naive" for loop:
 *
 * ```
 *  i = [| startValue |];
 *  [| if (!isStartInclusive) inc i |];
 *  end = [| endValue |];
 *  while ([| cmp(i, end) |]) ... {
 *      [| body |];
 *      inc i;
 *  }
 * ```
 *
 * This generator is suitable for cases where arithmetic overflow is impossible.
 */
class ForInDefinitelySafeSimpleProgressionLoopGenerator(
    codegen: ExpressionCodegen,
    forExpression: KtForExpression,
    private val startValue: StackValue,
    private val isStartInclusive: Boolean,
    private val endValue: StackValue,
    private val isEndInclusive: Boolean,
    step: Int
) : AbstractForInRangeLoopGenerator(codegen, forExpression, step) {

    constructor(
        codegen: ExpressionCodegen,
        forExpression: KtForExpression,
        boundedValue: SimpleBoundedValue,
        step: Int
    ) : this(
        codegen, forExpression,
        startValue = if (step == 1) boundedValue.lowBound else boundedValue.highBound,
        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,
        step = step
    )

    companion object {
        fun fromBoundedValueWithStep1(codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue) =
            ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, 1)

        fun fromBoundedValueWithStepMinus1(codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue) =
            ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, -1)
    }

    override fun storeRangeStartAndEnd() {
        loopParameter().store(startValue, v)
        // Skip 1st element if start is not inclusive.
        if (!isStartInclusive) incrementLoopVariable()

        StackValue.local(endVar, asmElementType).store(endValue, v)
    }

    override fun checkEmptyLoop(loopExit: Label) {
    }

    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)
                }
            }
        } else {
            if (step > 0) {
                if (isEndInclusive) {
                    v.ificmpgt(loopExit)
                } else {
                    v.ificmpge(loopExit)
                }
            } else {
                if (isEndInclusive) {
                    v.ificmplt(loopExit)
                } else {
                    v.ificmple(loopExit)
                }
            }
        }
    }

    override fun checkPostConditionAndIncrement(loopExit: Label) {
        incrementLoopVariable()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy