diff --git a/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/AsmExpressions.kt b/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/AsmExpressions.kt index 876e7abe2..1b44903cd 100644 --- a/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/AsmExpressions.kt +++ b/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/AsmExpressions.kt @@ -8,7 +8,7 @@ import scientifik.kmath.operations.Algebra import scientifik.kmath.operations.Field import scientifik.kmath.operations.Space -abstract class AsmCompiled(@JvmField val algebra: Algebra, @JvmField val constants: MutableList) { +abstract class AsmCompiled(@JvmField val algebra: Algebra, @JvmField val constants: MutableList) { abstract fun evaluate(arguments: Map): T } @@ -29,7 +29,7 @@ class AsmGenerationContext(classOfT: Class<*>, private val algebra: Algebra + private lateinit var constants: MutableList private lateinit var asmCompiledClassWriter: ClassWriter private lateinit var evaluateMethodVisitor: MethodVisitor private lateinit var evaluateL0: Label @@ -178,7 +178,9 @@ class AsmGenerationContext(classOfT: Class<*>, private val algebra: Algebra } - fun visitLoadFromConstants(value: T) { + fun visitLoadFromConstants(value: T) = visitLoadAnyFromConstants(value as Any) + + fun visitLoadAnyFromConstants(value: Any) { val idx = if (value in constants) constants.indexOf(value) else constants.apply { add(value) }.lastIndex maxStack++ @@ -195,7 +197,24 @@ class AsmGenerationContext(classOfT: Class<*>, private val algebra: Algebra evaluateMethodVisitor.visitLdcOrIConstInsn(value) + is Double -> evaluateMethodVisitor.visitLdcOrDConstInsn(value) + is Float -> evaluateMethodVisitor.visitLdcOrFConstInsn(value) + else -> evaluateMethodVisitor.visitLdcInsn(value) + } + + evaluateMethodVisitor.visitMethodInsn(INVOKESTATIC, c, "valueOf", "($sigLetter)L${c};", false) + return + } + + visitLoadAnyFromConstants(value) } fun visitLoadFromVariables(name: String, defaultValue: T? = null) = evaluateMethodVisitor.run { @@ -243,6 +262,15 @@ class AsmGenerationContext(classOfT: Class<*>, private val algebra: Algebra visitInsn(FCONST_2) else -> visitLdcInsn(value) } - -private val signatureLetters = mapOf( - java.lang.Byte::class.java to "B", - java.lang.Short::class.java to "S", - java.lang.Integer::class.java to "I", - java.lang.Long::class.java to "J", - java.lang.Float::class.java to "F", - java.lang.Double::class.java to "D" -) - -fun MethodVisitor.visitBoxedNumberConstant(number: Number) { - val clazz = number.javaClass - val c = clazz.name.replace('.', '/') - - when (number) { - is Int -> visitLdcOrIConstInsn(number) - is Double -> visitLdcOrDConstInsn(number) - is Float -> visitLdcOrFConstInsn(number) - else -> visitLdcInsn(number) - } - - visitMethodInsn(INVOKESTATIC, c, "valueOf", "(${signatureLetters[clazz]})L${c};", false) -}