From 834d1e1397e76943adab584c46a7526c47073da3 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Sat, 13 Jun 2020 15:53:25 +0700 Subject: [PATCH] Move specific optimization functions to Optimization --- .../scientifik/kmath/asm/AsmExpressions.kt | 31 ---------------- .../scientifik/kmath/asm/Optimization.kt | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmExpressions.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmExpressions.kt index 9e6efb66b..c4039da81 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmExpressions.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/AsmExpressions.kt @@ -11,37 +11,6 @@ interface AsmExpression { fun invoke(gen: AsmGenerationContext) } -private val methodNameAdapters: Map = mapOf("+" to "add", "*" to "multiply", "/" to "divide") - -internal fun hasSpecific(context: Algebra, name: String, arity: Int): Boolean { - val aName = methodNameAdapters[name] ?: name - - context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity } - ?: return false - - return true -} - -internal fun AsmGenerationContext.tryInvokeSpecific(context: Algebra, name: String, arity: Int): Boolean { - val aName = methodNameAdapters[name] ?: name - - context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity } - ?: return false - - val owner = context::class.jvmName.replace('.', '/') - - val sig = buildString { - append('(') - repeat(arity) { append("L${AsmGenerationContext.OBJECT_CLASS};") } - append(')') - append("L${AsmGenerationContext.OBJECT_CLASS};") - } - - visitAlgebraOperation(owner = owner, method = aName, descriptor = sig) - - return true -} - internal class AsmUnaryOperation(private val context: Algebra, private val name: String, expr: AsmExpression) : AsmExpression { private val expr: AsmExpression = expr.optimize() diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt index e821bd206..4e094fdd8 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/Optimization.kt @@ -1,5 +1,40 @@ package scientifik.kmath.asm +import scientifik.kmath.operations.Algebra +import kotlin.reflect.full.memberFunctions +import kotlin.reflect.jvm.jvmName + +private val methodNameAdapters: Map = mapOf("+" to "add", "*" to "multiply", "/" to "divide") + +internal fun hasSpecific(context: Algebra, name: String, arity: Int): Boolean { + val aName = methodNameAdapters[name] ?: name + + context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity } + ?: return false + + return true +} + +internal fun AsmGenerationContext.tryInvokeSpecific(context: Algebra, name: String, arity: Int): Boolean { + val aName = methodNameAdapters[name] ?: name + + context::class.memberFunctions.find { it.name == aName && it.parameters.size == arity } + ?: return false + + val owner = context::class.jvmName.replace('.', '/') + + val sig = buildString { + append('(') + repeat(arity) { append("L${AsmGenerationContext.OBJECT_CLASS};") } + append(')') + append("L${AsmGenerationContext.OBJECT_CLASS};") + } + + visitAlgebraOperation(owner = owner, method = aName, descriptor = sig) + + return true +} + @PublishedApi internal fun AsmExpression.optimize(): AsmExpression { val a = tryEvaluate()