Specify type explicitly, minor implementation refactor
This commit is contained in:
parent
c8fbc2d8be
commit
e2cc3c8efe
@ -44,7 +44,7 @@ fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<
|
||||
AsmBuilder.OBJECT_TYPE
|
||||
),
|
||||
|
||||
tArity = 1
|
||||
expectedArity = 1
|
||||
)
|
||||
}
|
||||
is MST.Binary -> {
|
||||
@ -64,7 +64,7 @@ fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<
|
||||
AsmBuilder.OBJECT_TYPE
|
||||
),
|
||||
|
||||
tArity = 2
|
||||
expectedArity = 2
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ internal class AsmBuilder<T> 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<T> internal constructor(
|
||||
/**
|
||||
* Stack of useful objects types on stack to verify types.
|
||||
*/
|
||||
private val typeStack = Stack<Type>()
|
||||
private val typeStack: Stack<Type> = Stack()
|
||||
|
||||
/**
|
||||
* Stack of useful objects types on stack expected by algebra calls.
|
||||
@ -345,6 +345,7 @@ internal class AsmBuilder<T> internal constructor(
|
||||
}
|
||||
|
||||
loadConstant(value, boxed)
|
||||
|
||||
if (!mustBeBoxed) unbox()
|
||||
else invokeMethodVisitor.checkcast(tType)
|
||||
}
|
||||
@ -407,11 +408,11 @@ internal class AsmBuilder<T> 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()
|
||||
}
|
||||
|
@ -5,11 +5,7 @@ import org.objectweb.asm.Type
|
||||
import scientifik.kmath.operations.Algebra
|
||||
|
||||
private val methodNameAdapters: Map<String, String> by lazy {
|
||||
hashMapOf(
|
||||
"+" to "add",
|
||||
"*" to "multiply",
|
||||
"/" to "divide"
|
||||
)
|
||||
hashMapOf("+" to "add", "*" to "multiply", "/" to "divide")
|
||||
}
|
||||
|
||||
/**
|
||||
@ -19,12 +15,10 @@ private val methodNameAdapters: Map<String, String> by lazy {
|
||||
* @return `true` if contains, else `false`.
|
||||
*/
|
||||
internal fun <T> AsmBuilder<T>.buildExpectationStack(context: Algebra<T>, 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,11 +29,10 @@ internal fun <T> AsmBuilder<T>.buildExpectationStack(context: Algebra<T>, name:
|
||||
* @return `true` if contains, else `false`.
|
||||
*/
|
||||
internal fun <T> AsmBuilder<T>.tryInvokeSpecific(context: Algebra<T>, 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
|
||||
var suitableSignature = it.name == theName && it.parameters.size == arity
|
||||
|
||||
if (primitiveMode && it.isBridge)
|
||||
suitableSignature = false
|
||||
@ -47,13 +40,13 @@ internal fun <T> AsmBuilder<T>.tryInvokeSpecific(context: Algebra<T>, name: Stri
|
||||
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
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user