Parameters refactoring
This commit is contained in:
+1
-1
@@ -251,7 +251,7 @@ public class AnonymousObjectTransformer {
|
||||
List<Type> descTypes = new ArrayList<Type>();
|
||||
|
||||
Parameters constructorParams = constructorInlineBuilder.buildParameters();
|
||||
int [] capturedIndexes = new int [constructorParams.totalSize()];
|
||||
int [] capturedIndexes = new int [constructorParams.getReal().size() + constructorParams.getCaptured().size()];
|
||||
int index = 0;
|
||||
int size = 0;
|
||||
|
||||
|
||||
@@ -289,7 +289,7 @@ public class InlineCodegen extends CallGenerator {
|
||||
};
|
||||
|
||||
List<MethodInliner.PointForExternalFinallyBlocks> infos = MethodInliner.processReturns(adapter, labelOwner, true, null);
|
||||
generateAndInsertFinallyBlocks(adapter, infos, ((StackValue.Local)remapper.remap(parameters.totalSize() + 1).value).index);
|
||||
generateAndInsertFinallyBlocks(adapter, infos, ((StackValue.Local)remapper.remap(parameters.getArgsSizeOnStack() + 1).value).index);
|
||||
removeFinallyMarkers(adapter);
|
||||
|
||||
adapter.accept(new InliningInstructionAdapter(codegen.v));
|
||||
|
||||
@@ -38,18 +38,16 @@ public class LocalVarRemapper {
|
||||
|
||||
public LocalVarRemapper(Parameters params, int additionalShift) {
|
||||
this.additionalShift = additionalShift;
|
||||
this.allParamsSize = params.totalSize();
|
||||
this.allParamsSize = params.getArgsSizeOnStack();
|
||||
this.params = params;
|
||||
|
||||
remapValues = new StackValue [params.totalSize()];
|
||||
Integer [] declIndexesToActual = new Integer [params.totalSize()];
|
||||
Integer [] actualDeclShifts = new Integer [params.totalSize()];
|
||||
remapValues = new StackValue [params.getArgsSizeOnStack()];
|
||||
Integer [] declIndexesToActual = new Integer [params.getArgsSizeOnStack()];
|
||||
Integer [] actualDeclShifts = new Integer [params.getArgsSizeOnStack()];
|
||||
|
||||
int index = 0;
|
||||
for (ParameterInfo param : params) {
|
||||
if (param != ParameterInfo.STUB && param != CapturedParamInfo.STUB) {
|
||||
declIndexesToActual[param.declarationIndex] = index;
|
||||
}
|
||||
declIndexesToActual[param.declarationIndex] = index;
|
||||
index++;
|
||||
}
|
||||
|
||||
@@ -64,14 +62,12 @@ public class LocalVarRemapper {
|
||||
|
||||
realSize = 0;
|
||||
for (ParameterInfo info : params) {
|
||||
if (info != ParameterInfo.STUB && info != CapturedParamInfo.STUB) {
|
||||
if (!info.isSkippedOrRemapped()) {
|
||||
remapValues[actualDeclShifts[info.declarationIndex]] = StackValue.local(realSize, AsmTypes.OBJECT_TYPE);
|
||||
realSize += info.getType().getSize();
|
||||
}
|
||||
else {
|
||||
remapValues[actualDeclShifts[info.declarationIndex]] = info.isRemapped() ? info.getRemapValue() : null;
|
||||
}
|
||||
if (!info.isSkippedOrRemapped()) {
|
||||
remapValues[actualDeclShifts[info.declarationIndex]] = StackValue.local(realSize, AsmTypes.OBJECT_TYPE);
|
||||
realSize += info.getType().getSize();
|
||||
}
|
||||
else {
|
||||
remapValues[actualDeclShifts[info.declarationIndex]] = info.isRemapped() ? info.getRemapValue() : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +89,7 @@ public class LocalVarRemapper {
|
||||
remappedIndex = ((StackValue.Local)remapped).index;
|
||||
}
|
||||
} else {
|
||||
remappedIndex = actualParamsSize - params.totalSize() + index; //captured params not used directly in this inlined method, they used in closure
|
||||
remappedIndex = actualParamsSize - params.getArgsSizeOnStack() + index; //captured params not used directly in this inlined method, they used in closure
|
||||
}
|
||||
|
||||
return new RemapInfo(StackValue.local(remappedIndex + additionalShift, AsmTypes.OBJECT_TYPE), null, SHIFT);
|
||||
|
||||
@@ -133,7 +133,7 @@ public class MethodInliner {
|
||||
resultNode.visitLabel(end);
|
||||
|
||||
if (inliningContext.isRoot()) {
|
||||
InternalFinallyBlockInliner.processInlineFunFinallyBlocks(resultNode, lambdasFinallyBlocks, ((StackValue.Local)remapper.remap(parameters.totalSize() + 1).value).index);
|
||||
InternalFinallyBlockInliner.processInlineFunFinallyBlocks(resultNode, lambdasFinallyBlocks, ((StackValue.Local)remapper.remap(parameters.getArgsSizeOnStack() + 1).value).index);
|
||||
}
|
||||
|
||||
processReturns(resultNode, labelOwner, remapReturn, end);
|
||||
@@ -155,7 +155,7 @@ public class MethodInliner {
|
||||
RemappingMethodAdapter remappingMethodAdapter = new RemappingMethodAdapter(resultNode.access, resultNode.desc, resultNode,
|
||||
new TypeRemapper(currentTypeMapping));
|
||||
|
||||
InlineAdapter lambdaInliner = new InlineAdapter(remappingMethodAdapter, parameters.totalSize(), sourceMapper) {
|
||||
InlineAdapter lambdaInliner = new InlineAdapter(remappingMethodAdapter, parameters.getArgsSizeOnStack(), sourceMapper) {
|
||||
|
||||
private AnonymousObjectGeneration anonymousObjectGen;
|
||||
private void handleAnonymousObjectGeneration() {
|
||||
@@ -307,8 +307,8 @@ public class MethodInliner {
|
||||
|
||||
@NotNull
|
||||
public MethodNode prepareNode(@NotNull MethodNode node, int finallyDeepShift) {
|
||||
final int capturedParamsSize = parameters.getCaptured().size();
|
||||
final int realParametersSize = parameters.getReal().size();
|
||||
final int capturedParamsSize = parameters.getCapturedArgsSizeOnStack();
|
||||
final int realParametersSize = parameters.getRealArgsSizeOnStack();
|
||||
Type[] types = Type.getArgumentTypes(node.desc);
|
||||
Type returnType = Type.getReturnType(node.desc);
|
||||
|
||||
@@ -558,7 +558,7 @@ public class MethodInliner {
|
||||
}
|
||||
|
||||
private LambdaInfo getLambdaIfExists(int varIndex) {
|
||||
if (varIndex < parameters.totalSize()) {
|
||||
if (varIndex < parameters.getArgsSizeOnStack()) {
|
||||
return parameters.getByByteCodeIndex(varIndex).getLambda();
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -19,13 +19,10 @@ 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.Type;
|
||||
|
||||
class ParameterInfo {
|
||||
|
||||
public static final ParameterInfo STUB = new ParameterInfo(AsmTypes.OBJECT_TYPE, true, -1, -1, -1);
|
||||
|
||||
protected final int index;
|
||||
|
||||
protected final int declarationIndex;
|
||||
|
||||
@@ -14,115 +14,81 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen.inline;
|
||||
package org.jetbrains.kotlin.codegen.inline
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.org.objectweb.asm.Type;
|
||||
import com.google.common.collect.Iterables
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList
|
||||
|
||||
//All parameters with gaps
|
||||
public class Parameters implements Iterable<ParameterInfo> {
|
||||
private final List<ParameterInfo> real;
|
||||
private final List<CapturedParamInfo> captured;
|
||||
class Parameters(val real: List<ParameterInfo>, val captured: List<CapturedParamInfo>) : Iterable<ParameterInfo> {
|
||||
|
||||
private final Integer [] declIndexesToActual;
|
||||
private final ParameterInfo [] actualDeclShifts;
|
||||
private val declIndexesToActual: Array<Int?>
|
||||
private val actualDeclShifts: Array<ParameterInfo?>
|
||||
|
||||
public Parameters(List<ParameterInfo> real, List<CapturedParamInfo> captured) {
|
||||
this.real = real;
|
||||
this.captured = captured;
|
||||
public val realArgsSizeOnStack = real.fold(0, { a, v -> a + v.type.size})
|
||||
public val capturedArgsSizeOnStack = captured.fold(0, { a, v -> a + v.type.size})
|
||||
|
||||
declIndexesToActual = new Integer [totalSize()];
|
||||
public val argsSizeOnStack = realArgsSizeOnStack + capturedArgsSizeOnStack
|
||||
|
||||
int index = 0;
|
||||
for (ParameterInfo param : this) {
|
||||
if (param != ParameterInfo.STUB && param != CapturedParamInfo.STUB) {
|
||||
declIndexesToActual[param.declarationIndex] = index;
|
||||
}
|
||||
index++;
|
||||
init {
|
||||
declIndexesToActual = arrayOfNulls<Int>(argsSizeOnStack)
|
||||
withIndex().forEach { it ->
|
||||
declIndexesToActual[it.value.declarationIndex] = it.index
|
||||
}
|
||||
|
||||
actualDeclShifts = new ParameterInfo [totalSize()];
|
||||
int realSize = 0;
|
||||
for (int i = 0; i < declIndexesToActual.length; i++) {
|
||||
Integer declIndexToActual = declIndexesToActual[i];
|
||||
actualDeclShifts = arrayOfNulls<ParameterInfo>(argsSizeOnStack)
|
||||
var realSize = 0
|
||||
for (i in declIndexesToActual.indices) {
|
||||
val declIndexToActual = declIndexesToActual[i]
|
||||
if (declIndexToActual != null) {
|
||||
actualDeclShifts[realSize] = getByDeclarationIndex(i);
|
||||
realSize += get(declIndexToActual).getType().getSize();
|
||||
val byDeclarationIndex = getByDeclarationIndex(i)
|
||||
actualDeclShifts[realSize] = byDeclarationIndex
|
||||
realSize += byDeclarationIndex.type.size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<ParameterInfo> getReal() {
|
||||
return real;
|
||||
fun getByDeclarationIndex(index: Int): ParameterInfo {
|
||||
if (index < realArgsSizeOnStack) {
|
||||
return real.get(declIndexesToActual[index]!!)
|
||||
}
|
||||
return captured.get(declIndexesToActual[index]!! - real.size())
|
||||
}
|
||||
|
||||
public List<CapturedParamInfo> getCaptured() {
|
||||
return captured;
|
||||
fun getByByteCodeIndex(index: Int): ParameterInfo {
|
||||
return actualDeclShifts[index]!!
|
||||
}
|
||||
|
||||
public int totalSize() {
|
||||
return real.size() + captured.size();
|
||||
}
|
||||
|
||||
public ParameterInfo getByDeclarationIndex(int index) {
|
||||
fun get(index: Int): ParameterInfo {
|
||||
if (index < real.size()) {
|
||||
return real.get(declIndexesToActual[index]);
|
||||
return real.get(index)
|
||||
}
|
||||
return captured.get(index - real.size());
|
||||
return captured.get(index - real.size())
|
||||
}
|
||||
|
||||
public ParameterInfo getByByteCodeIndex(int index) {
|
||||
return actualDeclShifts[index];
|
||||
override fun iterator(): Iterator<ParameterInfo> {
|
||||
return Iterables.concat(real, captured).iterator()
|
||||
}
|
||||
|
||||
public ParameterInfo get(int index) {
|
||||
if (index < real.size()) {
|
||||
return real.get(index);
|
||||
}
|
||||
return captured.get(index - real.size());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<ParameterInfo> iterator() {
|
||||
return Iterables.concat(real, captured).iterator();
|
||||
}
|
||||
|
||||
public static List<CapturedParamInfo> shiftAndAddStubs(List<CapturedParamInfo> capturedParams, int realSize) {
|
||||
List<CapturedParamInfo> result = new ArrayList<CapturedParamInfo>();
|
||||
for (CapturedParamInfo capturedParamInfo : capturedParams) {
|
||||
CapturedParamInfo newInfo = capturedParamInfo.newIndex(result.size() + realSize);
|
||||
result.add(newInfo);
|
||||
if (capturedParamInfo.getType().getSize() == 2) {
|
||||
result.add(CapturedParamInfo.STUB);
|
||||
val capturedTypes: ArrayList<Type>
|
||||
get() {
|
||||
val result = ArrayList<Type>()
|
||||
for (info in captured) {
|
||||
result.add(info.getType())
|
||||
}
|
||||
return result
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<ParameterInfo> addStubs(List<ParameterInfo> params) {
|
||||
List<ParameterInfo> result = new ArrayList<ParameterInfo>();
|
||||
for (ParameterInfo newInfo : params) {
|
||||
result.add(newInfo);
|
||||
if (newInfo.getType().getSize() == 2) {
|
||||
result.add(ParameterInfo.STUB);
|
||||
companion object {
|
||||
fun shift(capturedParams: List<CapturedParamInfo>, realSize: Int): List<CapturedParamInfo> {
|
||||
val result = ArrayList<CapturedParamInfo>()
|
||||
for (capturedParamInfo in capturedParams) {
|
||||
val newInfo = capturedParamInfo.newIndex(result.size() + realSize)
|
||||
result.add(newInfo)
|
||||
}
|
||||
return result
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public ArrayList<Type> getCapturedTypes() {
|
||||
ArrayList<Type> result = new ArrayList<Type>();
|
||||
for (CapturedParamInfo info : captured) {
|
||||
if(info != CapturedParamInfo.STUB) {
|
||||
result.add(info.getType());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.codegen.inline
|
||||
|
||||
import org.jetbrains.kotlin.codegen.StackValue
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import java.lang.Deprecated
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collections
|
||||
@@ -111,26 +112,15 @@ class ParametersBuilder private constructor(){
|
||||
return Collections.unmodifiableList(capturedParams)
|
||||
}
|
||||
|
||||
/*TODO use Parameters instead*/
|
||||
fun listAllParams(): List<ParameterInfo> {
|
||||
return valueAndHiddenParams + capturedParams
|
||||
}
|
||||
|
||||
private fun buildWithStubs(): List<ParameterInfo> {
|
||||
return Parameters.addStubs(listNotCaptured())
|
||||
}
|
||||
|
||||
private fun buildCapturedWithStubs(): List<CapturedParamInfo> {
|
||||
return Parameters.shiftAndAddStubs(listCaptured(), nextValueParameterIndex)
|
||||
}
|
||||
|
||||
fun buildParameters(): Parameters {
|
||||
return Parameters(buildWithStubs(), buildCapturedWithStubs())
|
||||
return Parameters(listNotCaptured(), Parameters.shift(listCaptured(), nextValueParameterIndex))
|
||||
}
|
||||
|
||||
// public fun getValueParameter(index: Int): ParameterInfo {
|
||||
// return valueAndHiddenParams[index + valueParamStart]
|
||||
// }
|
||||
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
|
||||
Reference in New Issue
Block a user