diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt index a3af80ccd..bb456e6eb 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt @@ -44,7 +44,7 @@ fun MST.compileWith(type: KClass, algebra: Algebra): Expression< AsmBuilder.OBJECT_TYPE ), - tArity = 1 + expectedArity = 1 ) } is MST.Binary -> { @@ -64,7 +64,7 @@ fun MST.compileWith(type: KClass, algebra: Algebra): Expression< AsmBuilder.OBJECT_TYPE ), - tArity = 2 + expectedArity = 2 ) } } diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt index a291ba4ee..ebe25e19f 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt @@ -76,7 +76,7 @@ internal class AsmBuilder internal constructor( /** * State if [T] a primitive type, so [AsmBuilder] may generate direct primitive calls. */ - internal var primitiveMode = false + internal var primitiveMode: Boolean = false /** * Primitive type to apple for specific primitive calls. Use [OBJECT_TYPE], if not in [primitiveMode]. @@ -91,7 +91,7 @@ internal class AsmBuilder internal constructor( /** * Stack of useful objects types on stack to verify types. */ - private val typeStack = Stack() + private val typeStack: Stack = Stack() /** * Stack of useful objects types on stack expected by algebra calls. @@ -345,6 +345,7 @@ internal class AsmBuilder internal constructor( } loadConstant(value, boxed) + if (!mustBeBoxed) unbox() else invokeMethodVisitor.checkcast(tType) } @@ -407,11 +408,11 @@ internal class AsmBuilder internal constructor( owner: String, method: String, descriptor: String, - tArity: Int, + expectedArity: Int, opcode: Int = Opcodes.INVOKEINTERFACE ) { run loop@{ - repeat(tArity) { + repeat(expectedArity) { if (typeStack.empty()) return@loop typeStack.pop() } diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/specialization.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/specialization.kt index 252509d59..e54acf6f9 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/specialization.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/specialization.kt @@ -5,11 +5,7 @@ import org.objectweb.asm.Type import scientifik.kmath.operations.Algebra private val methodNameAdapters: Map by lazy { - hashMapOf( - "+" to "add", - "*" to "multiply", - "/" to "divide" - ) + hashMapOf("+" to "add", "*" to "multiply", "/" to "divide") } /** @@ -19,12 +15,10 @@ private val methodNameAdapters: Map by lazy { * @return `true` if contains, else `false`. */ internal fun AsmBuilder.buildExpectationStack(context: Algebra, name: String, arity: Int): Boolean { - val aName = methodNameAdapters[name] ?: name - - val hasSpecific = context.javaClass.methods.find { it.name == aName && it.parameters.size == arity } != null + val theName = methodNameAdapters[name] ?: name + val hasSpecific = context.javaClass.methods.find { it.name == theName && it.parameters.size == arity } != null val t = if (primitiveMode && hasSpecific) primitiveMask else tType repeat(arity) { expectationStack.push(t) } - return hasSpecific } @@ -35,25 +29,24 @@ internal fun AsmBuilder.buildExpectationStack(context: Algebra, name: * @return `true` if contains, else `false`. */ internal fun AsmBuilder.tryInvokeSpecific(context: Algebra, name: String, arity: Int): Boolean { - val aName = methodNameAdapters[name] ?: name + val theName = methodNameAdapters[name] ?: name - val method = - context.javaClass.methods.find { - var suitableSignature = it.name == aName && it.parameters.size == arity + context.javaClass.methods.find { + var suitableSignature = it.name == theName && it.parameters.size == arity - if (primitiveMode && it.isBridge) - suitableSignature = false + if (primitiveMode && it.isBridge) + suitableSignature = false - suitableSignature - } ?: return false + suitableSignature + } ?: return false - val owner = context::class.java.name.replace('.', '/') + val owner = context::class.asm invokeAlgebraOperation( - owner = owner, - method = aName, + owner = owner.internalName, + method = theName, descriptor = Type.getMethodDescriptor(primitiveMaskBoxed, *Array(arity) { primitiveMask }), - tArity = arity, + expectedArity = arity, opcode = Opcodes.INVOKEVIRTUAL )