Minor refactor

This commit is contained in:
Iaroslav 2020-06-05 22:12:39 +07:00
parent 3ea76d56a5
commit fdd2551c3f
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7

View File

@ -7,7 +7,6 @@ import org.objectweb.asm.Opcodes.*
import scientifik.kmath.operations.Algebra 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
import java.io.File
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<T>) {
abstract fun evaluate(arguments: Map<String, T>): T abstract fun evaluate(arguments: Map<String, T>): T
@ -223,15 +222,13 @@ class AsmGenerationContext<T>(classOfT: Class<*>, private val algebra: Algebra<T
evaluateMethodVisitor.visitTypeInsn(CHECKCAST, T_ALGEBRA_CLASS) evaluateMethodVisitor.visitTypeInsn(CHECKCAST, T_ALGEBRA_CLASS)
} }
fun visitInvokeAlgebraOperation(owner: String, method: String, descriptor: String) { fun visitAlgebraOperation(owner: String, method: String, descriptor: String) {
maxStack++ maxStack++
evaluateMethodVisitor.visitMethodInsn(INVOKEINTERFACE, owner, method, descriptor, true) evaluateMethodVisitor.visitMethodInsn(INVOKEINTERFACE, owner, method, descriptor, true)
visitCastToT() visitCastToT()
} }
fun visitCastToT() { fun visitCastToT(): Unit = evaluateMethodVisitor.visitTypeInsn(CHECKCAST, T_CLASS)
evaluateMethodVisitor.visitTypeInsn(CHECKCAST, T_CLASS)
}
companion object { companion object {
const val ASM_COMPILED_CLASS = "scientifik/kmath/expressions/AsmCompiled" const val ASM_COMPILED_CLASS = "scientifik/kmath/expressions/AsmCompiled"
@ -243,6 +240,7 @@ class AsmGenerationContext<T>(classOfT: Class<*>, private val algebra: Algebra<T
const val SPACE_OPERATIONS_CLASS = "scientifik/kmath/operations/SpaceOperations" const val SPACE_OPERATIONS_CLASS = "scientifik/kmath/operations/SpaceOperations"
const val FIELD_CLASS = "scientifik/kmath/operations/Field" const val FIELD_CLASS = "scientifik/kmath/operations/Field"
const val STRING_CLASS = "java/lang/String" const val STRING_CLASS = "java/lang/String"
const val FIELD_OPERATIONS_CLASS = "scientifik/kmath/operations/FieldOperations"
} }
} }
@ -252,16 +250,12 @@ interface AsmExpression<T> {
internal class AsmVariableExpression<T>(val name: String, val default: T? = null) : internal class AsmVariableExpression<T>(val name: String, val default: T? = null) :
AsmExpression<T> { AsmExpression<T> {
override fun invoke(gen: AsmGenerationContext<T>) { override fun invoke(gen: AsmGenerationContext<T>): Unit = gen.visitLoadFromVariables(name, default)
gen.visitLoadFromVariables(name, default)
}
} }
internal class AsmConstantExpression<T>(val value: T) : internal class AsmConstantExpression<T>(val value: T) :
AsmExpression<T> { AsmExpression<T> {
override fun invoke(gen: AsmGenerationContext<T>) { override fun invoke(gen: AsmGenerationContext<T>): Unit = gen.visitLoadFromConstants(value)
gen.visitLoadFromConstants(value)
}
} }
internal class AsmSumExpression<T>( internal class AsmSumExpression<T>(
@ -273,7 +267,7 @@ internal class AsmSumExpression<T>(
first.invoke(gen) first.invoke(gen)
second.invoke(gen) second.invoke(gen)
gen.visitInvokeAlgebraOperation( gen.visitAlgebraOperation(
owner = AsmGenerationContext.SPACE_OPERATIONS_CLASS, owner = AsmGenerationContext.SPACE_OPERATIONS_CLASS,
method = "add", method = "add",
descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};" descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};"
@ -290,8 +284,8 @@ internal class AsmProductExpression<T>(
first.invoke(gen) first.invoke(gen)
second.invoke(gen) second.invoke(gen)
gen.visitInvokeAlgebraOperation( gen.visitAlgebraOperation(
owner = AsmGenerationContext.SPACE_CLASS, owner = AsmGenerationContext.SPACE_OPERATIONS_CLASS,
method = "times", method = "times",
descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};" descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};"
) )
@ -307,7 +301,7 @@ internal class AsmConstProductExpression<T>(
expr.invoke(gen) expr.invoke(gen)
gen.visitNumberConstant(const) gen.visitNumberConstant(const)
gen.visitInvokeAlgebraOperation( gen.visitAlgebraOperation(
owner = AsmGenerationContext.SPACE_CLASS, owner = AsmGenerationContext.SPACE_CLASS,
method = "multiply", method = "multiply",
descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};" descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};"
@ -324,8 +318,8 @@ internal class AsmDivExpression<T>(
expr.invoke(gen) expr.invoke(gen)
second.invoke(gen) second.invoke(gen)
gen.visitInvokeAlgebraOperation( gen.visitAlgebraOperation(
owner = AsmGenerationContext.FIELD_CLASS, owner = AsmGenerationContext.FIELD_OPERATIONS_CLASS,
method = "divide", method = "divide",
descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};" descriptor = "(L${AsmGenerationContext.OBJECT_CLASS};L${AsmGenerationContext.OBJECT_CLASS};)L${AsmGenerationContext.OBJECT_CLASS};"
) )