From 51945bf58c0bc5b877a2d4bf34fc86031d3994e1 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 7 Dec 2020 02:29:00 +0700 Subject: [PATCH] Add public API for WASM codegen --- .../kmath/{ast => wasm/internal}/Initial.kt | 2 +- .../internal/WasmBuilder.kt} | 2 +- .../jsMain/kotlin/kscience/kmath/wasm/wasm.kt | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) rename kmath-ast/src/jsMain/kotlin/kscience/kmath/{ast => wasm/internal}/Initial.kt (99%) rename kmath-ast/src/jsMain/kotlin/kscience/kmath/{ast/RealWasmBuilder.kt => wasm/internal/WasmBuilder.kt} (99%) create mode 100644 kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/wasm.kt diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/ast/Initial.kt b/kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/internal/Initial.kt similarity index 99% rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/ast/Initial.kt rename to kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/internal/Initial.kt index 2178b23d3..c50ad553d 100644 --- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/ast/Initial.kt +++ b/kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/internal/Initial.kt @@ -1,4 +1,4 @@ -package kscience.kmath.ast +package kscience.kmath.wasm.internal internal val f64StandardFunctions by lazy { Base64.toUint8Array(B) } diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/ast/RealWasmBuilder.kt b/kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/internal/WasmBuilder.kt similarity index 99% rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/ast/RealWasmBuilder.kt rename to kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/internal/WasmBuilder.kt index 20964e837..b5aa1a66e 100644 --- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/ast/RealWasmBuilder.kt +++ b/kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/internal/WasmBuilder.kt @@ -1,4 +1,4 @@ -package kscience.kmath.ast +package kscience.kmath.wasm.internal import WebAssembly.Instance import binaryen.* diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/wasm.kt b/kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/wasm.kt new file mode 100644 index 000000000..1357fc3fe --- /dev/null +++ b/kmath-ast/src/jsMain/kotlin/kscience/kmath/wasm/wasm.kt @@ -0,0 +1,41 @@ +package kscience.kmath.wasm + +import kscience.kmath.ast.MST +import kscience.kmath.ast.MstExpression +import kscience.kmath.expressions.Expression +import kscience.kmath.operations.IntRing +import kscience.kmath.operations.RealField +import kscience.kmath.wasm.internal.IntWasmBuilder +import kscience.kmath.wasm.internal.RealWasmBuilder + +/** + * Compiles an [MST] to WASM in the context of reals. + * + * @author Iaroslav Postovalov. + */ +public fun RealField.expression(mst: MST): Expression = + RealWasmBuilder(mst).instance + +/** + * Compiles an [MST] to WASM in the context of integers. + * + * @author Iaroslav Postovalov. + */ +public fun IntRing.expression(mst: MST): Expression = + IntWasmBuilder(mst).instance + +/** + * Optimizes performance of an [MstExpression] using WASM codegen in the context of reals. + * + * @author Iaroslav Postovalov. + */ +public fun MstExpression.compile(): Expression = + RealWasmBuilder(mst).instance + +/** + * Optimizes performance of an [MstExpression] using WASM codegen in the context of integers. + * + * @author Iaroslav Postovalov. + */ +public fun MstExpression.compile(): Expression = + IntWasmBuilder(mst).instance