ASM Bytecode Generation to unwrap Expressions of adv-expr API #94
@ -8,7 +8,7 @@ import scientifik.kmath.operations.Algebra
|
|||||||
|
|||||||
import scientifik.kmath.operations.Field
|
import scientifik.kmath.operations.Field
|
||||||
import scientifik.kmath.operations.Space
|
import scientifik.kmath.operations.Space
|
||||||
|
|
||||||
abstract class AsmCompiled<T>(@JvmField val algebra: Algebra<T>, @JvmField val constants: MutableList<T>) {
|
abstract class AsmCompiled<T>(@JvmField val algebra: Algebra<T>, @JvmField val constants: MutableList<out Any>) {
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
abstract fun evaluate(arguments: Map<String, T>): T
|
abstract fun evaluate(arguments: Map<String, T>): T
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ class AsmGenerationContext<T>(classOfT: Class<*>, private val algebra: Algebra<T
|
|||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Space is not actually used. Space is not actually used.
Not used Not used
Not used Not used
|
|||||||
private val evaluateThisVar: Int = 0
|
private val evaluateThisVar: Int = 0
|
||||||
private val evaluateArgumentsVar: Int = 1
|
private val evaluateArgumentsVar: Int = 1
|
||||||
private var maxStack: Int = 0
|
private var maxStack: Int = 0
|
||||||
private lateinit var constants: MutableList<T>
|
private lateinit var constants: MutableList<Any>
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
private lateinit var asmCompiledClassWriter: ClassWriter
|
private lateinit var asmCompiledClassWriter: ClassWriter
|
||||||
private lateinit var evaluateMethodVisitor: MethodVisitor
|
private lateinit var evaluateMethodVisitor: MethodVisitor
|
||||||
private lateinit var evaluateL0: Label
|
private lateinit var evaluateL0: Label
|
||||||
@ -178,7 +178,9 @@ class AsmGenerationContext<T>(classOfT: Class<*>, private val algebra: Algebra<T
|
|||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Space is not actually used. Space is not actually used.
Not used Not used
Not used Not used
|
|||||||
.newInstance(algebra, constants) as AsmCompiled<T>
|
.newInstance(algebra, constants) as AsmCompiled<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
fun visitLoadFromConstants(value: T) {
|
fun visitLoadFromConstants(value: T) = visitLoadAnyFromConstants(value as Any)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
fun visitLoadAnyFromConstants(value: Any) {
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
val idx = if (value in constants) constants.indexOf(value) else constants.apply { add(value) }.lastIndex
|
val idx = if (value in constants) constants.indexOf(value) else constants.apply { add(value) }.lastIndex
|
||||||
maxStack++
|
maxStack++
|
||||||
|
|
||||||
@ -195,7 +197,24 @@ class AsmGenerationContext<T>(classOfT: Class<*>, private val algebra: Algebra<T
|
|||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Space is not actually used. Space is not actually used.
Not used Not used
Not used Not used
|
|||||||
|
|
||||||
fun visitNumberConstant(value: Number) {
|
fun visitNumberConstant(value: Number) {
|
||||||
maxStack++
|
maxStack++
|
||||||
evaluateMethodVisitor.visitBoxedNumberConstant(value)
|
val clazz = value.javaClass
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
val c = clazz.name.replace('.', '/')
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
val sigLetter = SIGNATURE_LETTERS[clazz]
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
if (sigLetter != null) {
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
when (value) {
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
is Int -> evaluateMethodVisitor.visitLdcOrIConstInsn(value)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
is Double -> evaluateMethodVisitor.visitLdcOrDConstInsn(value)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
is Float -> evaluateMethodVisitor.visitLdcOrFConstInsn(value)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
else -> evaluateMethodVisitor.visitLdcInsn(value)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
}
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
evaluateMethodVisitor.visitMethodInsn(INVOKESTATIC, c, "valueOf", "($sigLetter)L${c};", false)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
return
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
}
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
visitLoadAnyFromConstants(value)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun visitLoadFromVariables(name: String, defaultValue: T? = null) = evaluateMethodVisitor.run {
|
fun visitLoadFromVariables(name: String, defaultValue: T? = null) = evaluateMethodVisitor.run {
|
||||||
@ -243,6 +262,15 @@ class AsmGenerationContext<T>(classOfT: Class<*>, private val algebra: Algebra<T
|
|||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Space is not actually used. Space is not actually used.
Not used Not used
Not used Not used
|
|||||||
private fun visitCastToT(): Unit = evaluateMethodVisitor.visitTypeInsn(CHECKCAST, T_CLASS)
|
private fun visitCastToT(): Unit = evaluateMethodVisitor.visitTypeInsn(CHECKCAST, T_CLASS)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private val SIGNATURE_LETTERS = mapOf(
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
java.lang.Byte::class.java to "B",
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
java.lang.Short::class.java to "S",
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
java.lang.Integer::class.java to "I",
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
java.lang.Long::class.java to "J",
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
java.lang.Float::class.java to "F",
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
java.lang.Double::class.java to "D"
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
)
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
|
|
||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Not used Not used
|
|||||||
const val ASM_COMPILED_CLASS = "scientifik/kmath/expressions/AsmCompiled"
|
const val ASM_COMPILED_CLASS = "scientifik/kmath/expressions/AsmCompiled"
|
||||||
const val LIST_CLASS = "java/util/List"
|
const val LIST_CLASS = "java/util/List"
|
||||||
const val MAP_CLASS = "java/util/Map"
|
const val MAP_CLASS = "java/util/Map"
|
||||||
|
|||||||
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Add suppress for unused parameters and documentation. Add suppress for unused parameters and documentation.
Space is not actually used. Space is not actually used.
Space is not actually used. Space is not actually used.
Not used Not used
Not used Not used
|
@ -26,26 +26,3 @@ fun MethodVisitor.visitLdcOrFConstInsn(value: Float) = when (value) {
|
|||||||
2f -> visitInsn(FCONST_2)
|
2f -> visitInsn(FCONST_2)
|
||||||
else -> visitLdcInsn(value)
|
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)
|
|
||||||
}
|
|
||||||
|
Add suppress for unused parameters and documentation.
Add suppress for unused parameters and documentation.
Space is not actually used.
Space is not actually used.
Not used
Not used