Refactor ASM generation code #105

Merged
CommanderTvis merged 11 commits from adv-expr-refactor-agc into adv-expr 2020-06-16 10:30:06 +03:00
Showing only changes of commit 4e28ad7d4e - Show all commits

View File

@ -17,7 +17,6 @@ import scientifik.kmath.operations.Algebra
* @param algebra the algebra the applied AsmExpressions use. * @param algebra the algebra the applied AsmExpressions use.
* @param className the unique class name of new loaded class. * @param className the unique class name of new loaded class.
*/ */
@Suppress("UNCHECKED_CAST")
@PublishedApi @PublishedApi
internal class AsmGenerator<T> @PublishedApi internal constructor( internal class AsmGenerator<T> @PublishedApi internal constructor(
private val classOfT: Class<*>, private val classOfT: Class<*>,
@ -45,6 +44,7 @@ internal class AsmGenerator<T> @PublishedApi internal constructor(
private lateinit var invokeMethodVisitor: MethodVisitor private lateinit var invokeMethodVisitor: MethodVisitor
private var generatedInstance: FunctionalCompiledExpression<T>? = null private var generatedInstance: FunctionalCompiledExpression<T>? = null
@Suppress("UNCHECKED_CAST")
fun getInstance(): FunctionalCompiledExpression<T> { fun getInstance(): FunctionalCompiledExpression<T> {
generatedInstance?.let { return it } generatedInstance?.let { return it }
@ -101,15 +101,14 @@ internal class AsmGenerator<T> @PublishedApi internal constructor(
visitEnd() visitEnd()
} }
invokeMethodVisitor = visitMethod( visitMethod(
access = Opcodes.ACC_PUBLIC or Opcodes.ACC_FINAL, access = Opcodes.ACC_PUBLIC or Opcodes.ACC_FINAL,
name = "invoke", name = "invoke",
descriptor = "(L$MAP_CLASS;)L$T_CLASS;", descriptor = "(L$MAP_CLASS;)L$T_CLASS;",
signature = "(L$MAP_CLASS<L$STRING_CLASS;+L$T_CLASS;>;)L$T_CLASS;", signature = "(L$MAP_CLASS<L$STRING_CLASS;+L$T_CLASS;>;)L$T_CLASS;",
exceptions = null exceptions = null
) {} ) {
invokeMethodVisitor = this
invokeMethodVisitor.run {
visitCode() visitCode()
val l0 = Label() val l0 = Label()
visitLabel(l0) visitLabel(l0)
@ -121,7 +120,7 @@ internal class AsmGenerator<T> @PublishedApi internal constructor(
visitLocalVariable( visitLocalVariable(
"this", "this",
"L$slashesClassName;", "L$slashesClassName;",
T_CLASS, null,
l0, l0,
l1, l1,
invokeThisVar invokeThisVar
@ -280,9 +279,7 @@ internal class AsmGenerator<T> @PublishedApi internal constructor(
invokeMethodVisitor.visitCheckCast(T_CLASS) invokeMethodVisitor.visitCheckCast(T_CLASS)
} }
internal fun loadStringConstant(string: String) { internal fun loadStringConstant(string: String): Unit = invokeMethodVisitor.visitLdcInsn(string)
invokeMethodVisitor.visitLdcInsn(string)
}
internal companion object { internal companion object {
private val SIGNATURE_LETTERS: Map<Class<out Any>, String> by lazy { private val SIGNATURE_LETTERS: Map<Class<out Any>, String> by lazy {