playing with i31
This commit is contained in:
1 parent
f592a4ecbd
commit
fdf3064665
2 files changed
+30
-10
No files matched your search
@@ -31,7 +31,7 @@ import qualified Data.Vector.Strict as V
|
||||
import Data.IntMap.Strict (IntMap)
|
||||
import Data.String.Interpolate
|
||||
import Gyehoek.Wasm qualified as Wasm
|
||||
import Gyehoek.Wasm (i32, ins, sxp)
|
||||
import Gyehoek.Wasm (i32, ins, sxp, eq, ref, i31)
|
||||
|
||||
|
||||
data Env = MkEnv { vars :: Vector Name }
|
||||
@@ -48,17 +48,16 @@ instance Ixed Env where
|
||||
|
||||
|
||||
|
||||
tshow :: Show a => a -> Text
|
||||
tshow = T.pack . show
|
||||
|
||||
|
||||
|
||||
lowerVal :: Env -> Val -> Wasm.Expr
|
||||
|
||||
lowerVal g (ValLit l) =
|
||||
case l of
|
||||
LitInt n -> ins "i32.const" [sxp n]
|
||||
LitBool b -> ins "i32.const" [sxp @Int $ if b then 1 else 0]
|
||||
LitInt n ->
|
||||
ins "i32.const" [sxp n]
|
||||
<> ins "ref.i31" []
|
||||
LitBool b ->
|
||||
ins "i32.const" [sxp @Int $ if b then 1 else 0]
|
||||
<> ins "ref.i31" []
|
||||
_ -> _
|
||||
|
||||
lowerVal g (ValVar x) = ins "local.get" [sxp l]
|
||||
@@ -87,8 +86,13 @@ lowerBinOp
|
||||
-> _ -> _ -> _ -> _ -> _ -> Wasm.Expr
|
||||
lowerBinOp op g x y r e =
|
||||
lowerVal g x
|
||||
<> ins "ref.cast" [sxp $ ref i31]
|
||||
<> ins "i31.get_s" []
|
||||
<> lowerVal g y
|
||||
<> ins "ref.cast" [sxp $ ref i31]
|
||||
<> ins "i31.get_s" []
|
||||
<> ins op []
|
||||
<> ins "ref.i31" []
|
||||
<> ins "local.set" [sxp n]
|
||||
<> lower' g' e
|
||||
where
|
||||
@@ -97,9 +101,11 @@ lowerBinOp op g x y r e =
|
||||
|
||||
|
||||
|
||||
scm = ref eq
|
||||
|
||||
lower :: Exp -> Eff es Text
|
||||
lower e = fmap Wasm.renderModule . Wasm.execGenMod $ do
|
||||
main <- Wasm.defun [] [i32] [i32, i32, i32, i32, i32] \_ ->
|
||||
main <- Wasm.defun [] [scm] [scm, scm, scm, scm, scm] \_ ->
|
||||
lower' emptyEnv e
|
||||
Wasm.export "main" "func" main
|
||||
|
||||
|
||||
Reference in new issue
Block a user