KT-2124 Fixed : Problem with reusement of local variables in bytecode for shared var
This commit is contained in:
@@ -27,6 +27,8 @@ import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Collection;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
@@ -36,6 +38,8 @@ public class CodegenUtil {
|
||||
private CodegenUtil() {
|
||||
}
|
||||
|
||||
private static final Random RANDOM = new Random(55L);
|
||||
|
||||
public static boolean isInterface(DeclarationDescriptor descriptor) {
|
||||
return descriptor instanceof ClassDescriptor && ((ClassDescriptor)descriptor).getKind() == ClassKind.TRAIT;
|
||||
}
|
||||
@@ -85,4 +89,16 @@ public class CodegenUtil {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String generateTmpVariableName(Collection<String> existingNames) {
|
||||
String prefix = "tmp";
|
||||
int i = RANDOM.nextInt(Integer.MAX_VALUE);
|
||||
String name = prefix + i;
|
||||
while (existingNames.contains(name)) {
|
||||
i++;
|
||||
name = prefix + i;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
||||
private final CodegenContext context;
|
||||
|
||||
private final Stack<BlockStackElement> blockStackElements = new Stack<BlockStackElement>();
|
||||
private final Collection<String> localVariableNames = new HashSet<String>();
|
||||
|
||||
static class BlockStackElement {
|
||||
}
|
||||
@@ -109,7 +110,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
||||
this.typeMapper = state.getInjector().getJetTypeMapper();
|
||||
this.returnType = returnType;
|
||||
this.state = state;
|
||||
this.v = new InstructionAdapter(v);
|
||||
this.v = new InstructionAdapter(v) {
|
||||
@Override
|
||||
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
|
||||
super.visitLocalVariable(name, desc, signature, start, end,
|
||||
index);
|
||||
localVariableNames.add(name);
|
||||
}
|
||||
};
|
||||
this.bindingContext = state.getBindingContext();
|
||||
this.context = context;
|
||||
}
|
||||
@@ -138,6 +146,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
||||
return bindingContext;
|
||||
}
|
||||
|
||||
public Collection<String> getLocalVariableNamesForExpression() {
|
||||
return localVariableNames;
|
||||
}
|
||||
|
||||
public void addTypeParameter(TypeParameterDescriptor typeParameter, StackValue expression) {
|
||||
typeParameterExpressions.put(typeParameter, expression);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
@@ -37,8 +38,7 @@ import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.commons.InstructionAdapter;
|
||||
import org.objectweb.asm.commons.Method;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
@@ -203,6 +203,8 @@ public class FunctionCodegen {
|
||||
throw new IllegalStateException("mismatching kind in " + functionDescriptor);
|
||||
}
|
||||
|
||||
Map<Name, Label> mapLabelsToDivideLocalVarVisibilityForSharedVar = new HashMap<Name, Label>();
|
||||
|
||||
if (kind instanceof OwnerKind.DelegateKind) {
|
||||
OwnerKind.DelegateKind dk = (OwnerKind.DelegateKind) kind;
|
||||
InstructionAdapter iv = new InstructionAdapter(mv);
|
||||
@@ -227,6 +229,11 @@ public class FunctionCodegen {
|
||||
mv.visitMethodInsn(INVOKESPECIAL, sharedVarType.getInternalName(), "<init>", "()V");
|
||||
mv.visitVarInsn(localVarType.getOpcode(ILOAD), index);
|
||||
mv.visitFieldInsn(PUTFIELD, sharedVarType.getInternalName(), "ref", StackValue.refType(localVarType).getDescriptor());
|
||||
|
||||
Label labelToDivideLocalVarForSharedVarVisibility = new Label();
|
||||
mv.visitLabel(labelToDivideLocalVarForSharedVarVisibility);
|
||||
mapLabelsToDivideLocalVarVisibilityForSharedVar.put(parameter.getName(), labelToDivideLocalVarForSharedVarVisibility);
|
||||
|
||||
mv.visitVarInsn(sharedVarType.getOpcode(ISTORE), index);
|
||||
}
|
||||
}
|
||||
@@ -237,6 +244,12 @@ public class FunctionCodegen {
|
||||
Label methodEnd = new Label();
|
||||
mv.visitLabel(methodEnd);
|
||||
|
||||
Collection<String> localVariableNames = new HashSet<String>();
|
||||
localVariableNames.addAll(codegen.getLocalVariableNamesForExpression());
|
||||
for (ValueParameterDescriptor parameterDescriptor : paramDescrs) {
|
||||
localVariableNames.add(parameterDescriptor.getName().getName());
|
||||
}
|
||||
|
||||
int k = 0;
|
||||
|
||||
if(expectedThisObject.exists()) {
|
||||
@@ -260,8 +273,22 @@ public class FunctionCodegen {
|
||||
for (ValueParameterDescriptor parameter : paramDescrs) {
|
||||
Type type = state.getInjector().getJetTypeMapper().mapType(parameter.getType(), MapTypeMode.VALUE);
|
||||
// TODO: specify signature
|
||||
mv.visitLocalVariable(parameter.getName().getName(), type.getDescriptor(), null, methodBegin, methodEnd, k);
|
||||
k += type.getSize();
|
||||
|
||||
Label divideLabel = mapLabelsToDivideLocalVarVisibilityForSharedVar.get(parameter.getName());
|
||||
String parameterName = parameter.getName().getName();
|
||||
if(divideLabel != null) {
|
||||
Type sharedVarType = state.getInjector().getJetTypeMapper().getSharedVarType(parameter);
|
||||
mv.visitLocalVariable(parameterName, type.getDescriptor(), null, methodBegin, divideLabel, k);
|
||||
|
||||
String nameForSharedVar = CodegenUtil.generateTmpVariableName(localVariableNames);
|
||||
localVariableNames.add(nameForSharedVar);
|
||||
|
||||
mv.visitLocalVariable(nameForSharedVar, sharedVarType.getDescriptor(), null, divideLabel, methodEnd, k);
|
||||
k += Math.max(type.getSize(), sharedVarType.getSize());
|
||||
} else {
|
||||
mv.visitLocalVariable(parameter.getName().getName(), type.getDescriptor(), null, methodBegin, methodEnd, k);
|
||||
k += type.getSize();
|
||||
}
|
||||
}
|
||||
|
||||
endVisit(mv, null, fun);
|
||||
|
||||
Reference in New Issue
Block a user