Get rid of redundant intrinsic

Also drop 'safe' parameter from TypeIntrinsics.checkcast,
because it's supposed to work as JVM checkcast (i.e. accepting nulls)
This commit is contained in:
Denis Zharkov
2015-12-28 08:39:46 +03:00
parent 39bbd7c795
commit 87b6374351
3 changed files with 21 additions and 29 deletions
+1
View File
@@ -1,6 +1,7 @@
<component name="ProjectDictionaryState"> <component name="ProjectDictionaryState">
<dictionary name="dzharkov"> <dictionary name="dzharkov">
<words> <words>
<w>checkcast</w>
<w>insn</w> <w>insn</w>
</words> </words>
</dictionary> </dictionary>
@@ -78,26 +78,28 @@ public object TypeIntrinsics {
instanceofInsn.desc = asmType.internalName instanceofInsn.desc = asmType.internalName
} }
public @JvmStatic fun checkcast(v: InstructionAdapter, jetType: KotlinType, asmType: Type, safe: Boolean) { @JvmStatic
public fun checkcast(
v: InstructionAdapter,
kotlinType: KotlinType, asmType: Type,
// This parameter is just for sake of optimization:
// when we generate 'as?' we do necessary intrinsic checks
// when calling TypeIntrinsics.instanceOf, so here we can just make checkcast
safe: Boolean) {
if (safe) { if (safe) {
v.checkcast(asmType) v.checkcast(asmType)
return return
} }
val functionTypeArity = getFunctionTypeArity(jetType) val functionTypeArity = getFunctionTypeArity(kotlinType)
if (functionTypeArity >= 0) { if (functionTypeArity >= 0) {
v.iconst(functionTypeArity) v.iconst(functionTypeArity)
if (safe) { v.typeIntrinsic(BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY, BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR)
v.typeIntrinsic(BEFORE_SAFE_CHECKCAST_TO_FUNCTION_OF_ARITY, BEFORE_SAFE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR)
}
else {
v.typeIntrinsic(BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY, BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR)
}
v.checkcast(asmType) v.checkcast(asmType)
return return
} }
val asMutableCollectionMethodName = getAsMutableCollectionMethodName(jetType) val asMutableCollectionMethodName = getAsMutableCollectionMethodName(kotlinType)
if (asMutableCollectionMethodName != null) { if (asMutableCollectionMethodName != null) {
v.typeIntrinsic(asMutableCollectionMethodName, getAsMutableCollectionDescriptor(asmType)) v.typeIntrinsic(asMutableCollectionMethodName, getAsMutableCollectionDescriptor(asmType))
return return
@@ -106,20 +108,22 @@ public object TypeIntrinsics {
v.checkcast(asmType) v.checkcast(asmType)
} }
public @JvmStatic fun checkcast(checkcastInsn: TypeInsnNode, instructions: InsnList, jetType: KotlinType, asmType: Type, safe: Boolean) { @JvmStatic
public fun checkcast(
checkcastInsn: TypeInsnNode,
instructions: InsnList,
kotlinType: KotlinType, asmType: Type,
safe: Boolean) {
if (safe) { if (safe) {
checkcastInsn.desc = asmType.internalName checkcastInsn.desc = asmType.internalName
return return
} }
val functionTypeArity = getFunctionTypeArity(jetType) val functionTypeArity = getFunctionTypeArity(kotlinType)
if (functionTypeArity >= 0) { if (functionTypeArity >= 0) {
instructions.insertBefore(checkcastInsn, iconstNode(functionTypeArity)) instructions.insertBefore(checkcastInsn, iconstNode(functionTypeArity))
val beforeCheckcast = if (safe) val beforeCheckcast = typeIntrinsicNode(BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY, BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR)
typeIntrinsicNode(BEFORE_SAFE_CHECKCAST_TO_FUNCTION_OF_ARITY, BEFORE_SAFE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR)
else
typeIntrinsicNode(BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY, BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR)
instructions.insertBefore(checkcastInsn, beforeCheckcast) instructions.insertBefore(checkcastInsn, beforeCheckcast)
instructions.insertBefore(checkcastInsn, TypeInsnNode(Opcodes.CHECKCAST, asmType.internalName)) instructions.insertBefore(checkcastInsn, TypeInsnNode(Opcodes.CHECKCAST, asmType.internalName))
@@ -127,7 +131,7 @@ public object TypeIntrinsics {
return return
} }
val asMutableCollectionMethodName = getAsMutableCollectionMethodName(jetType) val asMutableCollectionMethodName = getAsMutableCollectionMethodName(kotlinType)
if (asMutableCollectionMethodName != null) { if (asMutableCollectionMethodName != null) {
instructions.insertBefore(checkcastInsn, instructions.insertBefore(checkcastInsn,
typeIntrinsicNode(asMutableCollectionMethodName, getAsMutableCollectionDescriptor(asmType))) typeIntrinsicNode(asMutableCollectionMethodName, getAsMutableCollectionDescriptor(asmType)))
@@ -211,9 +215,4 @@ public object TypeIntrinsics {
private val BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR = private val BEFORE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR =
Type.getMethodDescriptor(OBJECT_TYPE, OBJECT_TYPE, Type.INT_TYPE) Type.getMethodDescriptor(OBJECT_TYPE, OBJECT_TYPE, Type.INT_TYPE)
private val BEFORE_SAFE_CHECKCAST_TO_FUNCTION_OF_ARITY = "beforeSafeCheckcastToFunctionOfArity"
private val BEFORE_SAFE_CHECKCAST_TO_FUNCTION_OF_ARITY_DESCRIPTOR =
Type.getMethodDescriptor(OBJECT_TYPE, OBJECT_TYPE, Type.INT_TYPE)
} }
@@ -361,12 +361,4 @@ public class TypeIntrinsics {
} }
return obj; return obj;
} }
public static Object beforeSafeCheckcastToFunctionOfArity(Object obj, int arity) {
// TODO should we instead inline bytecode for this in TypeIntrinsics.kt?
if (obj == null || !isFunctionOfArity(obj, arity)) {
return null;
}
return obj;
}
} }