Max stuff calculation
This commit is contained in:
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.asm4.ClassVisitor;
|
import org.jetbrains.asm4.ClassVisitor;
|
||||||
|
import org.jetbrains.asm4.MethodVisitor;
|
||||||
import org.jetbrains.asm4.Opcodes;
|
import org.jetbrains.asm4.Opcodes;
|
||||||
import org.jetbrains.asm4.Type;
|
import org.jetbrains.asm4.Type;
|
||||||
import org.jetbrains.asm4.commons.Method;
|
import org.jetbrains.asm4.commons.Method;
|
||||||
@@ -163,15 +164,16 @@ public class InlineCodegen implements ParentCodegenAware, Inliner {
|
|||||||
jvmSignature.getGenericsSignature(),
|
jvmSignature.getGenericsSignature(),
|
||||||
null);
|
null);
|
||||||
|
|
||||||
FunctionCodegen.generateMethodBody(node, functionDescriptor, context.getParentContext().intoFunction(functionDescriptor),
|
//for maxLocals calculation
|
||||||
|
MethodVisitor adapter = InlineCodegenUtil.wrapWithMaxLocalCalc(node);
|
||||||
|
FunctionCodegen.generateMethodBody(adapter, functionDescriptor, context.getParentContext().intoFunction(functionDescriptor),
|
||||||
jvmSignature,
|
jvmSignature,
|
||||||
new FunctionGenerationStrategy.FunctionDefault(state,
|
new FunctionGenerationStrategy.FunctionDefault(state,
|
||||||
functionDescriptor,
|
functionDescriptor,
|
||||||
(JetDeclarationWithBody) element),
|
(JetDeclarationWithBody) element),
|
||||||
getParentCodegen());
|
getParentCodegen());
|
||||||
//TODO
|
adapter.visitMaxs(-1, -1);
|
||||||
node.visitMaxs(30, 30);
|
adapter.visitEnd();
|
||||||
node.visitEnd();
|
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -196,7 +198,6 @@ public class InlineCodegen implements ParentCodegenAware, Inliner {
|
|||||||
VarRemapper.ParamRemapper remapper = new VarRemapper.ParamRemapper(parameters, initialFrameSize);
|
VarRemapper.ParamRemapper remapper = new VarRemapper.ParamRemapper(parameters, initialFrameSize);
|
||||||
|
|
||||||
inliner.doTransformAndMerge(codegen.getInstructionAdapter(), remapper);
|
inliner.doTransformAndMerge(codegen.getInstructionAdapter(), remapper);
|
||||||
generateClosuresBodies();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -219,9 +220,7 @@ public class InlineCodegen implements ParentCodegenAware, Inliner {
|
|||||||
Method asmMethod = jvmMethodSignature.getAsmMethod();
|
Method asmMethod = jvmMethodSignature.getAsmMethod();
|
||||||
MethodNode methodNode = new MethodNode(Opcodes.ASM4, getMethodAsmFlags(descriptor, context.getContextKind()), asmMethod.getName(), asmMethod.getDescriptor(), jvmMethodSignature.getGenericsSignature(), null);
|
MethodNode methodNode = new MethodNode(Opcodes.ASM4, getMethodAsmFlags(descriptor, context.getContextKind()), asmMethod.getName(), asmMethod.getDescriptor(), jvmMethodSignature.getGenericsSignature(), null);
|
||||||
|
|
||||||
|
MethodVisitor adapter = InlineCodegenUtil.wrapWithMaxLocalCalc(methodNode);
|
||||||
//AnalyzerAdapter adapter = new AnalyzerAdapter("fake", methodNode.access, methodNode.name, methodNode.desc, methodNode);
|
|
||||||
MethodNode adapter = methodNode;
|
|
||||||
|
|
||||||
FunctionCodegen.generateMethodBody(adapter, descriptor, context, jvmMethodSignature, new FunctionGenerationStrategy.FunctionDefault(state, descriptor, declaration) {
|
FunctionCodegen.generateMethodBody(adapter, descriptor, context, jvmMethodSignature, new FunctionGenerationStrategy.FunctionDefault(state, descriptor, declaration) {
|
||||||
@Override
|
@Override
|
||||||
@@ -229,7 +228,7 @@ public class InlineCodegen implements ParentCodegenAware, Inliner {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, codegen.getParentCodegen());
|
}, codegen.getParentCodegen());
|
||||||
adapter.visitMaxs(30, 30);
|
adapter.visitMaxs(-1, -1);
|
||||||
|
|
||||||
return methodNode;
|
return methodNode;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -240,4 +240,9 @@ public class InlineCodegenUtil {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static MaxCalcNode wrapWithMaxLocalCalc(@NotNull MethodNode methodNode) {
|
||||||
|
return new MaxCalcNode(methodNode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2014 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.jet.codegen.asm;
|
||||||
|
|
||||||
|
import org.jetbrains.asm4.MethodVisitor;
|
||||||
|
import org.jetbrains.asm4.Opcodes;
|
||||||
|
import org.jetbrains.asm4.Type;
|
||||||
|
import org.jetbrains.asm4.tree.AbstractInsnNode;
|
||||||
|
import org.jetbrains.asm4.tree.MethodNode;
|
||||||
|
|
||||||
|
import java.util.ListIterator;
|
||||||
|
|
||||||
|
public class MaxCalcNode extends MethodVisitor {
|
||||||
|
|
||||||
|
private int maxLocal;
|
||||||
|
|
||||||
|
private final MethodNode node;
|
||||||
|
|
||||||
|
public MaxCalcNode(MethodNode node)
|
||||||
|
{
|
||||||
|
super(Opcodes.ASM4, node);
|
||||||
|
this.node = node;
|
||||||
|
int paramsSize = (node.access & Opcodes.ACC_STATIC) == 0 ? 1 : 0;
|
||||||
|
|
||||||
|
Type[] types = Type.getArgumentTypes(node.desc);
|
||||||
|
for (Type type : types) {
|
||||||
|
paramsSize += type.getSize();
|
||||||
|
}
|
||||||
|
maxLocal = paramsSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitVarInsn(int opcode, int var) {
|
||||||
|
super.visitVarInsn(opcode, var);
|
||||||
|
int size = opcode == Opcodes.LLOAD || opcode == Opcodes.DLOAD || opcode == Opcodes.LSTORE || opcode == Opcodes.DSTORE ? 2 : 1;
|
||||||
|
updateMaxLocal(var, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitIincInsn(int var, int increment) {
|
||||||
|
super.visitIincInsn(var, increment);
|
||||||
|
updateMaxLocal(var, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateMaxLocal(int index, int size) {
|
||||||
|
maxLocal = Math.max(maxLocal, index + size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitMaxs(int maxStack, int maxLocals) {
|
||||||
|
//NB: it's hack for fast maxStack calculation cause it performed only in MethodWriter
|
||||||
|
//temporary solution: maxStack = instruction size (without labels and line numbers) * 2 (cause 1 instruction could put value of size 2)
|
||||||
|
int size = 0;
|
||||||
|
ListIterator<AbstractInsnNode> iterator = node.instructions.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
AbstractInsnNode next = iterator.next();
|
||||||
|
int type = next.getType();
|
||||||
|
if (type != AbstractInsnNode.LINE && type != AbstractInsnNode.LABEL) {
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.visitMaxs(Math.max(size * 2, maxStack), Math.max(maxLocals, this.maxLocal));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,9 +20,7 @@ import org.jetbrains.jet.lang.resolve.java.AsmTypeConstants;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.jetbrains.jet.codegen.asm.InlineCodegenUtil.isLambdaClass;
|
import static org.jetbrains.jet.codegen.asm.InlineCodegenUtil.*;
|
||||||
import static org.jetbrains.jet.codegen.asm.InlineCodegenUtil.isLambdaConstructorCall;
|
|
||||||
import static org.jetbrains.jet.codegen.asm.InlineCodegenUtil.isInvokeOnInlinable;
|
|
||||||
|
|
||||||
public class MethodInliner {
|
public class MethodInliner {
|
||||||
|
|
||||||
@@ -213,6 +211,7 @@ public class MethodInliner {
|
|||||||
ArrayList<Type> capturedTypes = parameters.getCapturedTypes();
|
ArrayList<Type> capturedTypes = parameters.getCapturedTypes();
|
||||||
Type[] allTypes = ArrayUtil.mergeArrays(types, capturedTypes.toArray(new Type[capturedTypes.size()]));
|
Type[] allTypes = ArrayUtil.mergeArrays(types, capturedTypes.toArray(new Type[capturedTypes.size()]));
|
||||||
|
|
||||||
|
node.instructions.resetLabels();
|
||||||
MethodNode transformedNode = new MethodNode(node.access, node.name, Type.getMethodDescriptor(returnType, allTypes), node.signature, null) {
|
MethodNode transformedNode = new MethodNode(node.access, node.name, Type.getMethodDescriptor(returnType, allTypes), node.signature, null) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -236,9 +235,14 @@ public class MethodInliner {
|
|||||||
}
|
}
|
||||||
super.visitIincInsn(newIndex, increment);
|
super.visitIincInsn(newIndex, increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitMaxs(int maxStack, int maxLocals) {
|
||||||
|
super.visitMaxs(maxStack, maxLocals + capturedParamsSize);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
node.accept(transformedNode);
|
node.accept(transformedNode);
|
||||||
transformedNode.visitMaxs(30, 30);
|
|
||||||
|
|
||||||
if (lambdaInfo != null) {
|
if (lambdaInfo != null) {
|
||||||
transformCaptured(transformedNode, parameters, lambdaInfo, lambdaFieldRemapper);
|
transformCaptured(transformedNode, parameters, lambdaInfo, lambdaFieldRemapper);
|
||||||
|
|||||||
Reference in New Issue
Block a user