introduced static map for asm type constants

This commit is contained in:
Svetlana Isakova
2012-09-05 18:29:43 +04:00
parent ef490ba8f1
commit 38fec5e4cb
6 changed files with 25 additions and 19 deletions
@@ -16,29 +16,20 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type; import org.jetbrains.asm4.Type;
import java.lang.annotation.Annotation; import java.util.Map;
import java.util.Iterator;
/** /**
* @author alex.tkachman * @author alex.tkachman
*/ */
public class AsmTypeConstants { public class AsmTypeConstants {
public static final Type OBJECT_TYPE = Type.getType(Object.class); public static final Type OBJECT_TYPE = Type.getType(Object.class);
public static final Type JAVA_NUMBER_TYPE = Type.getType(Number.class);
public static final Type JAVA_STRING_BUILDER_TYPE = Type.getType(StringBuilder.class);
public static final Type JAVA_STRING_TYPE = Type.getType(String.class); public static final Type JAVA_STRING_TYPE = Type.getType(String.class);
public static final Type JAVA_THROWABLE_TYPE = Type.getType(Throwable.class); public static final Type JAVA_THROWABLE_TYPE = Type.getType(Throwable.class);
public static final Type JAVA_CLASS_TYPE = Type.getType(Class.class);
public static final Type JAVA_BOOLEAN_TYPE = Type.getType(Boolean.class);
public static final Type JAVA_ARRAY_GENERIC_TYPE = Type.getType(Object[].class); public static final Type JAVA_ARRAY_GENERIC_TYPE = Type.getType(Object[].class);
public static final Type JAVA_CHAR_SEQUENCE_TYPE = Type.getType(CharSequence.class);
public static final Type JAVA_COMPARABLE_TYPE = Type.getType(Comparable.class);
public static final Type JAVA_ENUM_TYPE = Type.getType(Enum.class);
public static final Type JAVA_ANNOTATION_TYPE = Type.getType(Annotation.class);
public static final Type JAVA_ITERATOR_TYPE = Type.getType(Iterator.class);
public static final Type JAVA_ITERABLE_TYPE = Type.getType(Iterable.class);
public static final Type JET_NOTHING_TYPE = Type.getObjectType("jet/Nothing"); public static final Type JET_NOTHING_TYPE = Type.getObjectType("jet/Nothing");
public static final Type JET_TUPLE0_TYPE = Type.getObjectType("jet/Tuple0"); public static final Type JET_TUPLE0_TYPE = Type.getObjectType("jet/Tuple0");
@@ -56,6 +47,17 @@ public class AsmTypeConstants {
public static final Type JET_SHARED_LONG_TYPE = Type.getObjectType("jet/runtime/SharedVar$Long"); public static final Type JET_SHARED_LONG_TYPE = Type.getObjectType("jet/runtime/SharedVar$Long");
public static final Type JET_SHARED_BOOLEAN_TYPE = Type.getObjectType("jet/runtime/SharedVar$Boolean"); public static final Type JET_SHARED_BOOLEAN_TYPE = Type.getObjectType("jet/runtime/SharedVar$Boolean");
private static final Map<Class<?>, Type> TYPES_MAP = Maps.newHashMap();
public static Type getType(@NotNull Class<?> javaClass) {
Type type = TYPES_MAP.get(javaClass);
if (type == null) {
type = Type.getType(javaClass);
TYPES_MAP.put(javaClass, type);
}
return type;
}
private AsmTypeConstants() { private AsmTypeConstants() {
} }
} }
@@ -2567,7 +2567,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
} }
public void generateStringBuilderConstructor() { public void generateStringBuilderConstructor() {
Type type = JAVA_STRING_BUILDER_TYPE; Type type = getType(StringBuilder.class);
v.anew(type); v.anew(type);
v.dup(); v.dup();
Method method = new Method("<init>", Type.VOID_TYPE, new Type[0]); Method method = new Method("<init>", Type.VOID_TYPE, new Type[0]);
@@ -2596,7 +2596,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
} }
public void invokeAppendMethod(Type exprType) { public void invokeAppendMethod(Type exprType) {
Method appendDescriptor = new Method("append", JAVA_STRING_BUILDER_TYPE, Method appendDescriptor = new Method("append", getType(StringBuilder.class),
new Type[] {exprType.getSort() == Type.OBJECT ? OBJECT_TYPE : exprType}); new Type[] {exprType.getSort() == Type.OBJECT ? OBJECT_TYPE : exprType});
v.invokevirtual("java/lang/StringBuilder", "append", appendDescriptor.getDescriptor()); v.invokevirtual("java/lang/StringBuilder", "append", appendDescriptor.getDescriptor());
} }
@@ -121,6 +121,10 @@ public class KotlinToJavaTypesMap {
return asmTypes.get(className); return asmTypes.get(className);
} }
private void register(@NotNull ClassDescriptor kotlinDescriptor, @NotNull Class<?> javaClass) {
register(kotlinDescriptor, AsmTypeConstants.getType(javaClass));
}
private void register(@NotNull ClassDescriptor kotlinDescriptor, @NotNull Type javaType) { private void register(@NotNull ClassDescriptor kotlinDescriptor, @NotNull Type javaType) {
FqNameUnsafe fqName = DescriptorUtils.getFQName(kotlinDescriptor); FqNameUnsafe fqName = DescriptorUtils.getFQName(kotlinDescriptor);
ClassName className = new ClassName(fqName.toSafe(), kotlinDescriptor.getDefaultType().getArguments().size()); ClassName className = new ClassName(fqName.toSafe(), kotlinDescriptor.getDefaultType().getArguments().size());
@@ -293,7 +293,7 @@ public abstract class StackValue {
v.checkcast(JvmPrimitiveType.CHAR.getWrapper().getAsmType()); v.checkcast(JvmPrimitiveType.CHAR.getWrapper().getAsmType());
} }
else { else {
v.checkcast(JAVA_NUMBER_TYPE); v.checkcast(getType(Number.class));
} }
} }
unbox(toType, v); unbox(toType, v);
@@ -482,7 +482,7 @@ public abstract class StackValue {
putTuple0Instance(v); putTuple0Instance(v);
return; return;
} }
if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(JAVA_BOOLEAN_TYPE)) { if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(getType(Boolean.class))) {
throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type); throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type);
} }
putAsBoolean(v); putAsBoolean(v);
@@ -571,7 +571,7 @@ public abstract class StackValue {
myOperand.put(type, v); // the operand will remove itself from the stack if needed myOperand.put(type, v); // the operand will remove itself from the stack if needed
return; return;
} }
if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(JAVA_BOOLEAN_TYPE)) { if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(getType(Boolean.class))) {
throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type"); throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type");
} }
putAsBoolean(v); putAsBoolean(v);
@@ -61,6 +61,6 @@ public class JavaClassFunction implements IntrinsicMethod {
else { else {
v.aconst(type); v.aconst(type);
} }
return StackValue.onStack(AsmTypeConstants.JAVA_CLASS_TYPE); return StackValue.onStack(AsmTypeConstants.getType(Class.class));
} }
} }
@@ -53,6 +53,6 @@ public class JavaClassProperty implements IntrinsicMethod {
else { else {
v.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;"); v.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;");
} }
return StackValue.onStack(AsmTypeConstants.JAVA_CLASS_TYPE); return StackValue.onStack(AsmTypeConstants.getType(Class.class));
} }
} }