IntrinsicCallable clean

This commit is contained in:
Michael Bogdanov
2015-04-06 18:45:30 +03:00
parent c72bae5081
commit a201cb3258
17 changed files with 36 additions and 25 deletions
@@ -24,7 +24,7 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
public class ArrayGet : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable =
createMappedCallable(method) {
createIntrinsicCallable(method) {
val type = correctElementType(calcReceiverType())
it.aload(type)
}
@@ -44,7 +44,7 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
public class ArrayIterator : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return createUnaryCallable(method) {
return createUnaryIntrinsicCallable(method, needPrimitiveCheck = false) {
val methodSignature = "(${method.owner.getDescriptor()})${returnType.getDescriptor()}"
it.invokestatic("kotlin/jvm/internal/InternalPackage", "iterator", methodSignature, false)
}
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.codegen.CallableMethod
public class ArraySize : IntrinsicMethod() {
public override fun toCallable(method: CallableMethod): Callable {
return createUnaryCallable(method) { adapter ->
return createUnaryIntrinsicCallable(method) { adapter ->
adapter.arraylength()
}
}
@@ -36,7 +36,7 @@ public class BinaryOp(private val opcode: Int) : IntrinsicMethod() {
val operandType = numberFunctionOperandType(returnType)
val paramType = if (shift()) Type.INT_TYPE else operandType
return binaryIntrinsic(operandType, paramType, operandType) {
return createBinaryIntrinsicCallable(operandType, paramType, operandType) {
v -> v.visitInsn(returnType.getOpcode(opcode))
}
}
@@ -24,7 +24,7 @@ import org.jetbrains.org.objectweb.asm.Opcodes
public class Clone : IntrinsicMethod() {
override fun toCallable(method: CallableMethod, isSuperCall: Boolean): Callable {
return createUnaryCallable(method, OBJECT_TYPE) {
return createUnaryIntrinsicCallable(method, OBJECT_TYPE, needPrimitiveCheck = false) {
val opcodes: Int = if (isSuperCall) Opcodes.INVOKESPECIAL else Opcodes.INVOKEVIRTUAL
it.visitMethodInsn(opcodes, "java/lang/Object", "clone", "()Ljava/lang/Object;", false)
}
@@ -35,8 +35,8 @@ public class CompareTo : IntrinsicMethod() {
}
override fun toCallable(method: CallableMethod): Callable {
val parameterType = comparisonOperandType(method.thisType ?: method.receiverType, method.parameterTypes.single())
return binaryIntrinsic(method.returnType, parameterType, parameterType, null) {
val parameterType = comparisonOperandType(method.thisType ?: method.receiverType, method.argumentTypes.single())
return createBinaryIntrinsicCallable(method.returnType, parameterType, parameterType, null) {
genInvoke(parameterType, it)
}
}
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.resolve.jvm.AsmTypes.OBJECT_TYPE
public class Equals : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return binaryIntrinsic(method.returnType, OBJECT_TYPE, nullOrObject(method.thisType), nullOrObject(method.receiverType)) {
return createBinaryIntrinsicCallable(method.returnType, OBJECT_TYPE, nullOrObject(method.thisType), nullOrObject(method.receiverType)) {
AsmUtil.genAreEqualCall(it)
}
}
@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
public class Increment(private val myDelta: Int) : IntrinsicMethod() {
override fun toCallable(method: CallableMethod, isSuper: Boolean, resolvedCall: ResolvedCall<*>): Callable {
return createMappedCallable(method) {
return createIntrinsicCallable(method) {
val jetExpression = resolvedCall.getCall().getCalleeExpression()
assert(jetExpression !is JetPrefixExpression) { "There should be postfix increment ${jetExpression!!.getText()}" }
genIncrement(returnType, myDelta, it)
@@ -64,7 +64,7 @@ public abstract class IntrinsicCallable(override val returnType: Type,
}
}
fun binaryIntrinsic(returnType: Type, valueParameterType: Type, thisType: Type? = null, receiverType: Type? = null, lambda: IntrinsicCallable.(i: InstructionAdapter) -> Unit): IntrinsicCallable {
fun createBinaryIntrinsicCallable(returnType: Type, valueParameterType: Type, thisType: Type? = null, receiverType: Type? = null, lambda: IntrinsicCallable.(i: InstructionAdapter) -> Unit): IntrinsicCallable {
assert(AsmUtil.isPrimitive(returnType)) { "Return type of BinaryOp intrinsic should be of primitive type : " + returnType }
return object : IntrinsicCallable(returnType, listOf(valueParameterType), thisType, receiverType) {
@@ -74,14 +74,19 @@ fun binaryIntrinsic(returnType: Type, valueParameterType: Type, thisType: Type?
}
}
public class UnaryIntrinsic(val callable: CallableMethod, val newReturnType: Type? = null, needPrimitiveCheck: Boolean = false, val newThisType: Type? = null, val invoke: UnaryIntrinsic.(v: InstructionAdapter) -> Unit) :
IntrinsicCallable(newReturnType ?: callable.returnType, callable.valueParameterTypes, newThisType ?: callable.thisType, callable.receiverType) {
public class UnaryIntrinsic(
callable: CallableMethod,
newReturnType: Type? = null,
needPrimitiveCheck: Boolean = true,
newThisType: Type? = null,
private val invoke: UnaryIntrinsic.(v: InstructionAdapter) -> Unit
) : IntrinsicCallable(newReturnType ?: callable.returnType, callable.valueParameterTypes, newThisType ?: callable.thisType, callable.receiverType) {
init {
if (needPrimitiveCheck) {
assert(AsmUtil.isPrimitive(returnType)) { "Return type of UnaryPlus intrinsic should be of primitive type : " + returnType }
assert(AsmUtil.isPrimitive(returnType)) { "Return type of UnaryPlus intrinsic should be of primitive type: $returnType" }
}
assert(valueParameterTypes.isEmpty(), "Unary operation should not have any parameters")
assert(valueParameterTypes.isEmpty()) { "Unary operation should not have any parameters" }
}
override fun invokeIntrinsic(v: InstructionAdapter) {
@@ -89,8 +94,14 @@ public class UnaryIntrinsic(val callable: CallableMethod, val newReturnType: Typ
}
}
public fun createUnaryCallable(callable: CallableMethod, newReturnType: Type? = null, needPrimitiveCheck: Boolean = false, invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit) : IntrinsicCallable {
return UnaryIntrinsic(callable, newReturnType, needPrimitiveCheck, invoke = invoke)
public fun createUnaryIntrinsicCallable(
callable: CallableMethod,
newReturnType: Type? = null,
needPrimitiveCheck: Boolean = true,
newThisType: Type? = null,
invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit
) : IntrinsicCallable {
return UnaryIntrinsic(callable, newReturnType, needPrimitiveCheck, newThisType, invoke = invoke)
}
public open class MappedCallable(val callable: CallableMethod, val invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit = {}) :
@@ -101,6 +112,6 @@ public open class MappedCallable(val callable: CallableMethod, val invoke: Intri
}
}
public fun createMappedCallable(callable: CallableMethod, invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit) : IntrinsicCallable {
public fun createIntrinsicCallable(callable: CallableMethod, invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit) : IntrinsicCallable {
return MappedCallable(callable, invoke)
}
@@ -25,7 +25,7 @@ public class Inv : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
val type = numberFunctionOperandType(method.returnType)
return UnaryIntrinsic(method, newThisType = type) {
return createUnaryIntrinsicCallable(method, newThisType = type) {
if (returnType == Type.LONG_TYPE) {
it.lconst(-1)
}
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.codegen.CallableMethod
public class JavaClassArray : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return createMappedCallable(method) {
return createIntrinsicCallable(method) {
//do nothing all generated as vararg
}
}
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.codegen.StackValue
public class NumberCast : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return createUnaryCallable(method) {
return createUnaryIntrinsicCallable(method) {
StackValue.coerce(calcReceiverType()!!, returnType, it)
}
}
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.codegen.CallableMethod
public class StringGetChar : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return createMappedCallable(method) {
return createIntrinsicCallable(method) {
it.invokevirtual("java/lang/String", "charAt", "(I)C", false)
}
}
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.codegen.CallableMethod
public class StringPlus : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return createMappedCallable(method) {
return createIntrinsicCallable(method) {
it.invokestatic("kotlin/jvm/internal/Intrinsics", "stringPlus", "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;", false)
}
}
@@ -24,7 +24,7 @@ public class ToString : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
val type = AsmUtil.stringValueOfType(method.thisType ?: method.receiverType)
return UnaryIntrinsic(method, newThisType = type) {
return createUnaryIntrinsicCallable(method, newThisType = type) {
it.invokestatic("java/lang/String", "valueOf", "(" + type.getDescriptor() + ")Ljava/lang/String;", false)
}
}
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.codegen.CallableMethod
public class UnaryMinus : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return createUnaryCallable(method, numberFunctionOperandType(method.returnType), needPrimitiveCheck = true) {
return createUnaryIntrinsicCallable(method, numberFunctionOperandType(method.returnType), needPrimitiveCheck = true) {
it.neg(returnType)
}
}
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.codegen.CallableMethod
public class UnaryPlus : IntrinsicMethod() {
override fun toCallable(method: CallableMethod): Callable {
return createUnaryCallable(method, needPrimitiveCheck = true) {
return createUnaryIntrinsicCallable(method, needPrimitiveCheck = true) {
//nothing
}
}