diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 11b67c8aa0a..44e5db4fe5e 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -49,7 +49,7 @@ public class ExpressionCodegen extends JetVisitor { private int myLastLineNumber = -1; - private final InstructionAdapterEx v; + private final InstructionAdapter v; private final FrameMap myMap; private final JetTypeMapper typeMapper; private final GenerationState state; @@ -68,7 +68,7 @@ public class ExpressionCodegen extends JetVisitor { this.typeMapper = state.getTypeMapper(); this.returnType = returnType; this.state = state; - this.v = new InstructionAdapterEx(v); + this.v = new InstructionAdapter(v); this.bindingContext = state.getBindingContext(); this.context = context; this.intrinsics = state.getIntrinsics(); @@ -773,7 +773,7 @@ public class ExpressionCodegen extends JetVisitor { return myMap.getIndex(descriptor); } - public void invokeFunctionNoParams(FunctionDescriptor functionDescriptor, Type type, InstructionAdapterEx v) { + public void invokeFunctionNoParams(FunctionDescriptor functionDescriptor, Type type, InstructionAdapter v) { DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration(); boolean isStatic = containingDeclaration instanceof NamespaceDescriptorImpl; functionDescriptor = functionDescriptor.getOriginal(); @@ -1085,7 +1085,7 @@ public class ExpressionCodegen extends JetVisitor { Type type = propValue.type; propValue.put(type, v); if(JetTypeMapper.isPrimitive(type) && !type.equals(Type.VOID_TYPE)) { - v.valueOf(type); + StackValue.valueOf(v, type); type = JetTypeMapper.boxType(type); } v.goTo(end); @@ -1265,10 +1265,10 @@ public class ExpressionCodegen extends JetVisitor { if(JetTypeMapper.isPrimitive(leftType) != JetTypeMapper.isPrimitive(rightType)) { gen(left, leftType); - v.valueOf(leftType); + StackValue.valueOf(v, leftType); leftType = JetTypeMapper.boxType(leftType); gen(right, rightType); - v.valueOf(rightType); + StackValue.valueOf(v, rightType); rightType = JetTypeMapper.boxType(rightType); } else { diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java index 15b1f823c5b..173fb5309f8 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java @@ -55,17 +55,18 @@ public class FunctionCodegen { mv.visitCode(); Type[] argTypes = function.getArgumentTypes(); - InstructionAdapterEx iv = new InstructionAdapterEx(mv); + InstructionAdapter iv = new InstructionAdapter(mv); iv.load(0, JetTypeMapper.TYPE_OBJECT); for (int i = 0, reg = 1; i < argTypes.length; i++) { Type argType = argTypes[i]; iv.load(reg, argType); + //noinspection AssignmentToForLoopParameter reg += argType.getSize(); } iv.invokevirtual(state.getTypeMapper().jvmName((ClassDescriptor) owner.getContextDescriptor(), OwnerKind.IMPLEMENTATION), function.getName(), function.getDescriptor()); if(JetTypeMapper.isPrimitive(function.getReturnType()) && !JetTypeMapper.isPrimitive(overriden.getReturnType())) - iv.valueOf(function.getReturnType()); + StackValue.valueOf(iv, function.getReturnType()); if(function.getReturnType() == Type.VOID_TYPE) iv.aconst(null); iv.areturn(overriden.getReturnType()); diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/InstructionAdapterEx.java b/compiler/backend/src/org/jetbrains/jet/codegen/InstructionAdapterEx.java deleted file mode 100644 index f78095819d2..00000000000 --- a/compiler/backend/src/org/jetbrains/jet/codegen/InstructionAdapterEx.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.jetbrains.jet.codegen; - -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; -import org.objectweb.asm.commons.InstructionAdapter; -import org.objectweb.asm.commons.Method; - -public class InstructionAdapterEx extends InstructionAdapter { - private static final Method BOOLEAN_VALUE = Method.getMethod("boolean booleanValue()"); - - private static final Method CHAR_VALUE = Method.getMethod("char charValue()"); - - private static final Method INT_VALUE = Method.getMethod("int intValue()"); - - private static final Method FLOAT_VALUE = Method.getMethod("float floatValue()"); - - private static final Method LONG_VALUE = Method.getMethod("long longValue()"); - - private static final Method DOUBLE_VALUE = Method.getMethod("double doubleValue()"); - - public InstructionAdapterEx(MethodVisitor methodVisitor) { - super(methodVisitor); - } - - private static Type getBoxedType(final Type type) { - switch (type.getSort()) { - case Type.BYTE: - return JetTypeMapper.JL_BYTE_TYPE; - case Type.BOOLEAN: - return JetTypeMapper.JL_BOOLEAN_TYPE; - case Type.SHORT: - return JetTypeMapper.JL_SHORT_TYPE; - case Type.CHAR: - return JetTypeMapper.JL_CHAR_TYPE; - case Type.INT: - return JetTypeMapper.JL_INTEGER_TYPE; - case Type.FLOAT: - return JetTypeMapper.JL_FLOAT_TYPE; - case Type.LONG: - return JetTypeMapper.JL_LONG_TYPE; - case Type.DOUBLE: - return JetTypeMapper.JL_DOUBLE_TYPE; - } - return type; - } - - public void valueOf(final Type type) { - if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) { - return; - } - if (type == Type.VOID_TYPE) { - aconst(null); - } else { - Type boxed = getBoxedType(type); - invokestatic(boxed.getInternalName(), "valueOf", "(" + type.getDescriptor() + ")" + boxed.getDescriptor()); - } - } - - public void unbox(final Type type) { - Type t = JetTypeMapper.JL_NUMBER_TYPE; - Method sig = null; - switch (type.getSort()) { - case Type.VOID: - return; - case Type.CHAR: - t = JetTypeMapper.JL_CHAR_TYPE; - sig = CHAR_VALUE; - break; - case Type.BOOLEAN: - t = JetTypeMapper.JL_BOOLEAN_TYPE; - sig = BOOLEAN_VALUE; - break; - case Type.DOUBLE: - sig = DOUBLE_VALUE; - break; - case Type.FLOAT: - sig = FLOAT_VALUE; - break; - case Type.LONG: - sig = LONG_VALUE; - break; - case Type.INT: - case Type.SHORT: - case Type.BYTE: - sig = INT_VALUE; - } - - checkcast(t); - invokevirtual(t.getInternalName(), sig.getName(), sig.getDescriptor()); - } -} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java index a29f79823c9..b6ab8153e38 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java @@ -173,6 +173,28 @@ public class JetTypeMapper { return result; } + public static Type getBoxedType(final Type type) { + switch (type.getSort()) { + case Type.BYTE: + return JL_BYTE_TYPE; + case Type.BOOLEAN: + return JL_BOOLEAN_TYPE; + case Type.SHORT: + return JL_SHORT_TYPE; + case Type.CHAR: + return JL_CHAR_TYPE; + case Type.INT: + return JL_INTEGER_TYPE; + case Type.FLOAT: + return JL_FLOAT_TYPE; + case Type.LONG: + return JL_LONG_TYPE; + case Type.DOUBLE: + return JL_DOUBLE_TYPE; + } + return type; + } + public String jvmName(ClassDescriptor jetClass, OwnerKind kind) { PsiElement declaration = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, jetClass); if (declaration instanceof PsiClass) { diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java index b5e599721b4..01f0c5abf69 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java @@ -3,7 +3,6 @@ package org.jetbrains.jet.codegen; import com.intellij.psi.PsiFile; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; -import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.java.JavaClassDescriptor; @@ -69,14 +68,14 @@ public class NamespaceCodegen { } } - if (hasNonConstantPropertyInitializers(namespace, context)) { - generateStaticInitializers(namespace, context); + if (hasNonConstantPropertyInitializers(namespace)) { + generateStaticInitializers(namespace); } generateTypeInfoFields(namespace, context); } - private void generateStaticInitializers(JetNamespace namespace, ClassContext context) { + private void generateStaticInitializers(JetNamespace namespace) { MethodVisitor mv = v.visitMethod(ACC_PUBLIC | ACC_STATIC, "", "()V", null, null); mv.visitCode(); @@ -88,7 +87,7 @@ public class NamespaceCodegen { if (declaration instanceof JetProperty) { final JetExpression initializer = ((JetProperty) declaration).getInitializer(); if (initializer != null && !(initializer instanceof JetConstantExpression)) { - final PropertyDescriptor descriptor = (PropertyDescriptor) state.getBindingContext().get(BindingContext.VARIABLE, (JetProperty) declaration); + final PropertyDescriptor descriptor = (PropertyDescriptor) state.getBindingContext().get(BindingContext.VARIABLE, declaration); codegen.genToJVMStack(initializer); codegen.intermediateValueForProperty(descriptor, true, false).store(new InstructionAdapter(mv)); } @@ -108,7 +107,7 @@ public class NamespaceCodegen { v.visitField(ACC_PRIVATE|ACC_STATIC|ACC_SYNTHETIC, fieldName, "Ljet/typeinfo/TypeInfo;", null, null); MethodVisitor mmv = v.visitMethod(ACC_PUBLIC|ACC_STATIC|ACC_SYNTHETIC, "$getCachedTypeInfo$" + e.getValue(), "()Ljet/typeinfo/TypeInfo;", null, null); - InstructionAdapterEx v = new InstructionAdapterEx(mmv); + InstructionAdapter v = new InstructionAdapter(mmv); v.visitFieldInsn(GETSTATIC, jvmClassName, fieldName, "Ljet/typeinfo/TypeInfo;"); v.visitInsn(DUP); Label end = new Label(); @@ -127,7 +126,7 @@ public class NamespaceCodegen { } } - private void generateTypeInfo(ClassContext context, InstructionAdapterEx v, JetType jetType, JetTypeMapper typeMapper, JetType root) { + private static void generateTypeInfo(ClassContext context, InstructionAdapter v, JetType jetType, JetTypeMapper typeMapper, JetType root) { String knownTypeInfo = typeMapper.isKnownTypeInfo(jetType); if(knownTypeInfo != null) { v.getstatic("jet/typeinfo/TypeInfo", knownTypeInfo, "Ljet/typeinfo/TypeInfo;"); @@ -172,7 +171,7 @@ public class NamespaceCodegen { } } - private static boolean hasNonConstantPropertyInitializers(JetNamespace namespace, ClassContext context) { + private static boolean hasNonConstantPropertyInitializers(JetNamespace namespace) { for (JetDeclaration declaration : namespace.getDeclarations()) { if (declaration instanceof JetProperty) { final JetExpression initializer = ((JetProperty) declaration).getInitializer(); diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java index b657161eb12..9daa14e79cd 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java @@ -19,6 +19,18 @@ public abstract class StackValue { this.type = type; } + public static void valueOf(InstructionAdapter instructionAdapter, final Type type) { + if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) { + return; + } + if (type == Type.VOID_TYPE) { + instructionAdapter.aconst(null); + } else { + Type boxed = JetTypeMapper.getBoxedType(type); + instructionAdapter.invokestatic(boxed.getInternalName(), "valueOf", "(" + type.getDescriptor() + ")" + boxed.getDescriptor()); + } + } + public abstract void put(Type type, InstructionAdapter v); public void store(InstructionAdapter v) {