From 6373c3115a40840650219e1772ad3221a3c9ac9f Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Thu, 11 May 2017 13:25:10 +0200 Subject: [PATCH] Convert LocalVarRemapper to Kotlin --- .../kotlin/codegen/inline/LocalVarRemapper.kt | 165 +++++++++--------- 1 file changed, 80 insertions(+), 85 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LocalVarRemapper.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LocalVarRemapper.kt index 1cb5b515b58..48a3beee5d4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LocalVarRemapper.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/LocalVarRemapper.kt @@ -14,151 +14,146 @@ * limitations under the License. */ -package org.jetbrains.kotlin.codegen.inline; +package org.jetbrains.kotlin.codegen.inline -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.codegen.StackValue; -import org.jetbrains.kotlin.resolve.jvm.AsmTypes; -import org.jetbrains.org.objectweb.asm.Label; -import org.jetbrains.org.objectweb.asm.MethodVisitor; -import org.jetbrains.org.objectweb.asm.Opcodes; -import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter; +import org.jetbrains.kotlin.codegen.StackValue +import org.jetbrains.kotlin.resolve.jvm.AsmTypes +import org.jetbrains.org.objectweb.asm.Label +import org.jetbrains.org.objectweb.asm.MethodVisitor +import org.jetbrains.org.objectweb.asm.Opcodes +import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter -import static org.jetbrains.kotlin.codegen.inline.LocalVarRemapper.RemapStatus.*; +import org.jetbrains.kotlin.codegen.inline.LocalVarRemapper.RemapStatus.* -public class LocalVarRemapper { - private final Parameters params; - private final int actualParamsSize; - private final StackValue[] remapValues; - private final int additionalShift; +class LocalVarRemapper(private val params: Parameters, private val additionalShift: Int) { + private val actualParamsSize: Int + private val remapValues: Array = arrayOfNulls(params.argsSizeOnStack) - public LocalVarRemapper(@NotNull Parameters params, int additionalShift) { - this.additionalShift = additionalShift; - this.params = params; - - remapValues = new StackValue[params.getArgsSizeOnStack()]; - - int realSize = 0; - for (ParameterInfo info : params) { - Integer shift = params.getDeclarationSlot(info); - if (!info.isSkippedOrRemapped()) { - remapValues[shift] = StackValue.local(realSize, AsmTypes.OBJECT_TYPE); - realSize += info.getType().getSize(); + init { + var realSize = 0 + for (info in params) { + val shift = params.getDeclarationSlot(info) + if (!info.isSkippedOrRemapped) { + remapValues[shift] = StackValue.local(realSize, AsmTypes.OBJECT_TYPE) + realSize += info.getType().size } else { - remapValues[shift] = info.isRemapped() ? info.getRemapValue() : null; + remapValues[shift] = if (info.isRemapped) info.remapValue else null if (CapturedParamInfo.isSynthetic(info)) { - realSize += info.getType().getSize(); + realSize += info.getType().size } } } - actualParamsSize = realSize; + actualParamsSize = realSize } - @NotNull - private RemapInfo doRemap(int index) { - int remappedIndex; + private fun doRemap(index: Int): RemapInfo { + val remappedIndex: Int - if (index < params.getArgsSizeOnStack()) { - ParameterInfo info = params.getParameterByDeclarationSlot(index); - StackValue remapped = remapValues[index]; + if (index < params.argsSizeOnStack) { + val info = params.getParameterByDeclarationSlot(index) + val remapped = remapValues[index] if (info.isSkipped || remapped == null) { - return new RemapInfo(info); + return RemapInfo(info) } - if (info.isRemapped()) { - return new RemapInfo(remapped, info, REMAPPED); + if (info.isRemapped) { + return RemapInfo(remapped, info, REMAPPED) } else { - remappedIndex = ((StackValue.Local) remapped).index; + remappedIndex = (remapped as StackValue.Local).index } } else { //captured params are not used directly in this inlined method, they are used in closure //except captured ones for default lambdas, they are generated in default body - remappedIndex = actualParamsSize - params.getArgsSizeOnStack() + index; + remappedIndex = actualParamsSize - params.argsSizeOnStack + index } - return new RemapInfo(StackValue.local(remappedIndex + additionalShift, AsmTypes.OBJECT_TYPE), null, SHIFT); + return RemapInfo(StackValue.local(remappedIndex + additionalShift, AsmTypes.OBJECT_TYPE), null, SHIFT) } - @NotNull - public RemapInfo remap(int index) { - RemapInfo info = doRemap(index); + fun remap(index: Int): RemapInfo { + val info = doRemap(index) if (FAIL == info.status) { - assert info.parameterInfo != null : "Parameter info should be not null"; - throw new RuntimeException("Trying to access skipped parameter: " + info.parameterInfo.type + " at " +index); + assert(info.parameterInfo != null) { "Parameter info should be not null" } + throw RuntimeException("Trying to access skipped parameter: " + info.parameterInfo!!.type + " at " + index) } - return info; + return info } - public void visitIincInsn(int var, int increment, @NotNull MethodVisitor mv) { - RemapInfo remap = remap(var); - assert remap.value instanceof StackValue.Local : "Remapped value should be a local: " + remap.value; - mv.visitIincInsn(((StackValue.Local) remap.value).index, increment); + fun visitIincInsn(`var`: Int, increment: Int, mv: MethodVisitor) { + val remap = remap(`var`) + assert(remap.value is StackValue.Local) { "Remapped value should be a local: " + remap.value!! } + mv.visitIincInsn((remap.value as StackValue.Local).index, increment) } - public void visitLocalVariable( - @NotNull String name, - @NotNull String desc, - @Nullable String signature, - @NotNull Label start, - @NotNull Label end, - int index, - MethodVisitor mv + fun visitLocalVariable( + name: String, + desc: String, + signature: String?, + start: Label, + end: Label, + index: Int, + mv: MethodVisitor ) { - RemapInfo info = doRemap(index); + val info = doRemap(index) //add entries only for shifted vars if (SHIFT == info.status) { - mv.visitLocalVariable(name, desc, signature, start, end, ((StackValue.Local) info.value).index); + mv.visitLocalVariable(name, desc, signature, start, end, (info.value as StackValue.Local).index) } } - public void visitVarInsn(int opcode, int var, @NotNull InstructionAdapter mv) { - RemapInfo remapInfo = remap(var); - StackValue value = remapInfo.value; - if (value instanceof StackValue.Local) { - boolean isStore = InlineCodegenUtil.isStoreInstruction(opcode); + fun visitVarInsn(opcode: Int, `var`: Int, mv: InstructionAdapter) { + var opcode = opcode + val remapInfo = remap(`var`) + val value = remapInfo.value + if (value is StackValue.Local) { + val isStore = InlineCodegenUtil.isStoreInstruction(opcode) if (remapInfo.parameterInfo != null) { //All remapped value parameters can't be rewritten except case of default ones. //On remapping default parameter to actual value there is only one instruction that writes to it according to mask value //but if such parameter remapped then it passed and this mask branch code never executed //TODO add assertion about parameter default value: descriptor is required - opcode = value.type.getOpcode(isStore ? Opcodes.ISTORE : Opcodes.ILOAD); + opcode = value.type.getOpcode(if (isStore) Opcodes.ISTORE else Opcodes.ILOAD) } - mv.visitVarInsn(opcode, ((StackValue.Local) value).index); + mv.visitVarInsn(opcode, value.index) if (remapInfo.parameterInfo != null && !isStore) { - StackValue.coerce(value.type, remapInfo.parameterInfo.type, mv); + StackValue.coerce(value.type, remapInfo.parameterInfo.type, mv) } } else { - assert remapInfo.parameterInfo != null : "Non local value should have parameter info"; - value.put(remapInfo.parameterInfo.type, mv); + assert(remapInfo.parameterInfo != null) { "Non local value should have parameter info" } + value!!.put(remapInfo.parameterInfo!!.type, mv) } } - public enum RemapStatus { + enum class RemapStatus { SHIFT, REMAPPED, FAIL } - public static class RemapInfo { - public final StackValue value; - public final ParameterInfo parameterInfo; - public final RemapStatus status; + class RemapInfo { + @JvmField + val value: StackValue? - public RemapInfo(@NotNull StackValue value, @Nullable ParameterInfo parameterInfo, @NotNull RemapStatus remapStatus) { - this.value = value; - this.parameterInfo = parameterInfo; - this.status = remapStatus; + @JvmField + val parameterInfo: ParameterInfo? + + @JvmField + val status: RemapStatus + + constructor(value: StackValue, parameterInfo: ParameterInfo?, remapStatus: RemapStatus) { + this.value = value + this.parameterInfo = parameterInfo + this.status = remapStatus } - public RemapInfo(@NotNull ParameterInfo parameterInfo) { - this.value = null; - this.parameterInfo = parameterInfo; - this.status = FAIL; + constructor(parameterInfo: ParameterInfo) { + this.value = null + this.parameterInfo = parameterInfo + this.status = FAIL } } }