+56
-25
@@ -50,11 +50,12 @@ instance Ixed Env where
|
|||||||
ix i = #vars . ix (fromIntegral i)
|
ix i = #vars . ix (fromIntegral i)
|
||||||
|
|
||||||
data Runtime = MkRuntime
|
data Runtime = MkRuntime
|
||||||
{ argArrayIdx :: Idx
|
{ argArrayType :: Idx
|
||||||
|
, argArray :: Idx
|
||||||
, contType :: Idx
|
, contType :: Idx
|
||||||
, contStackType :: Idx
|
, contStackType :: Idx
|
||||||
, contStackIndexIdx :: Idx
|
, contStackTop :: Idx
|
||||||
, contStackIdx :: Idx
|
, contStack :: Idx
|
||||||
}
|
}
|
||||||
deriving (Show, Generic)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
@@ -64,11 +65,31 @@ data Runtime = MkRuntime
|
|||||||
-- of the stack into the SCM unitype.
|
-- of the stack into the SCM unitype.
|
||||||
makeSmallFixnum :: Wasm.Expr
|
makeSmallFixnum :: Wasm.Expr
|
||||||
makeSmallFixnum = mconcat
|
makeSmallFixnum = mconcat
|
||||||
[ ins "i32.const" [sxp @Int 2]
|
[ ins "i32.const" [sxp @Int 1]
|
||||||
, ins "i32.shl" []
|
, ins "i32.shl" []
|
||||||
, ins "ref.i31" []
|
, ins "ref.i31" []
|
||||||
]
|
]
|
||||||
|
|
||||||
|
-- | Given an expression @e@ leaving a @ref eq@ atop the stack,
|
||||||
|
-- @pushArg rt n e@ sets the nth slot of the arg-passing array to the
|
||||||
|
-- result of @e@.
|
||||||
|
pushArg :: Runtime -> Int -> Wasm.Expr -> Wasm.Expr
|
||||||
|
pushArg (MkRuntime {argArrayType,argArray}) n e = mconcat
|
||||||
|
[ ins "global.get" [sxp argArray]
|
||||||
|
, ins "i32.const" [sxp n]
|
||||||
|
, e
|
||||||
|
, ins "array.set" [sxp argArrayType]
|
||||||
|
]
|
||||||
|
|
||||||
|
-- | Pop the nth arg from the arg-passing array onto the stack.
|
||||||
|
popArg :: Runtime -> Int -> Wasm.Expr
|
||||||
|
popArg (MkRuntime {argArrayType,argArray}) n = mconcat
|
||||||
|
[ ins "global.get" [sxp argArray]
|
||||||
|
, ins "i32.const" [sxp n]
|
||||||
|
, ins "array.get" [sxp argArrayType]
|
||||||
|
, ins "ref.as_non_null" []
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
lowerVal :: Env -> Val -> Wasm.Expr
|
lowerVal :: Env -> Val -> Wasm.Expr
|
||||||
@@ -104,23 +125,33 @@ lower' g (ExpIf c t f) = do
|
|||||||
pure $ lowerVal g c
|
pure $ lowerVal g c
|
||||||
<> Wasm.if' (Wasm.result [i32]) t' f'
|
<> Wasm.if' (Wasm.result [i32]) t' f'
|
||||||
|
|
||||||
|
lower' g (ExpContinue k [x]) = pure . mconcat $
|
||||||
|
[ pushArg rt 0 (lowerVal g x)
|
||||||
|
, ins "i32.const" [sxp @Int 1] -- nargs
|
||||||
|
-- get the return continuation.
|
||||||
|
, ins "global.get" [sxp rt.contStack]
|
||||||
|
, ins "global.get" [sxp rt.contStackTop]
|
||||||
|
, ins "array.get" [sxp rt.contStackType]
|
||||||
|
, ins "ref.as_non_null" []
|
||||||
|
-- decrement contStackTop, completing the "pop."
|
||||||
|
, ins "global.get" [sxp rt.contStackTop]
|
||||||
|
, ins "i32.const" [sxp @Int 1]
|
||||||
|
, ins "i32.sub" []
|
||||||
|
, ins "global.set" [sxp rt.contStackTop]
|
||||||
|
, ins "return_call_ref" [sxp rt.contType]
|
||||||
|
]
|
||||||
|
where rt = g.runtime
|
||||||
|
|
||||||
lower' g (ExpLet [(r,MkLambda xs ktail m)] e) = do
|
lower' g (ExpLet [(r,MkLambda xs ktail m)] e) = do
|
||||||
_ <- defun [i32] [] [] \_ -> do
|
idx <- defun [i32] [] (replicate 5 scm) \_ -> lower' g m
|
||||||
let stack = g.runtime.contStackIdx
|
let g' = g & #vars <>~ []
|
||||||
let index = g.runtime.contStackIndexIdx
|
let n = length g.vars
|
||||||
m' <- lower' g m
|
e' <- lower' g' e
|
||||||
pure . mconcat $
|
pure . mconcat $
|
||||||
[ m'
|
[ ins "ref.func" [sxp idx]
|
||||||
, ins "global.get" [sxp index]
|
, ins "local.set" [sxp n]
|
||||||
, ins "i32.const" [sxp @Int 1]
|
, e'
|
||||||
, ins "i32.sub" []
|
]
|
||||||
, ins "global.set" [sxp index]
|
|
||||||
, ins "global.get" [sxp stack]
|
|
||||||
, ins "global.get" [sxp index]
|
|
||||||
, ins "array.get" [sxp g.runtime.contStackType]
|
|
||||||
, ins "return_call_ref" [sxp g.runtime.contType]
|
|
||||||
]
|
|
||||||
_
|
|
||||||
|
|
||||||
lower' g e = error . show $ e
|
lower' g e = error . show $ e
|
||||||
|
|
||||||
@@ -158,17 +189,17 @@ emitRuntime = do
|
|||||||
-- cont stack
|
-- cont stack
|
||||||
contType <- Wasm.deftype $ Wasm.func [i32] []
|
contType <- Wasm.deftype $ Wasm.func [i32] []
|
||||||
contStackType <- Wasm.deftype $ array (refnull (fromIdx contType))
|
contStackType <- Wasm.deftype $ array (refnull (fromIdx contType))
|
||||||
contStackIndexIdx <- Wasm.defglobal i32 $ ins "i32.const" [sxp @Int 0]
|
contStackTop <- Wasm.defglobal i32 $ ins "i32.const" [sxp @Int 0]
|
||||||
contStackIdx <- Wasm.defglobal (ref (Wasm.fromIdx contStackType)) $
|
contStack <- Wasm.defglobal (ref (Wasm.fromIdx contStackType)) $
|
||||||
ins "i32.const" [sxp @Int 128]
|
ins "i32.const" [sxp @Int 128]
|
||||||
<> ins "array.new_default" [sxp contStackType]
|
<> ins "array.new_default" [sxp contStackType]
|
||||||
-- arg array
|
-- arg array
|
||||||
argArrayType <- Wasm.deftype $ Wasm.array scm
|
argArrayType <- Wasm.deftype $ Wasm.array scm
|
||||||
argArrayIdx <- Wasm.defglobal (ref (Wasm.fromIdx argArrayType)) _
|
argArray <- Wasm.defglobal (ref (Wasm.fromIdx argArrayType)) _
|
||||||
-- consIdx <- Wasm.defun _ _ _ _
|
-- consIdx <- Wasm.defun _ _ _ _
|
||||||
pure $ MkRuntime
|
pure $ MkRuntime
|
||||||
{argArrayIdx
|
{argArray
|
||||||
,contStackIdx,contStackIndexIdx,contStackType,contType}
|
,contStack,contStackTop,contStackType,contType}
|
||||||
-- pure $ error "todo"
|
-- pure $ error "todo"
|
||||||
|
|
||||||
lower :: Exp -> Eff es Text
|
lower :: Exp -> Eff es Text
|
||||||
|
|||||||
+23
-1
@@ -47,6 +47,7 @@ module Gyehoek.Wasm
|
|||||||
, FromIdx(..)
|
, FromIdx(..)
|
||||||
, func
|
, func
|
||||||
, refnull
|
, refnull
|
||||||
|
, declareFuncref
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
@@ -85,6 +86,7 @@ import Data.Functor (void)
|
|||||||
data Module = MkModule
|
data Module = MkModule
|
||||||
{ types :: Vector RecType
|
{ types :: Vector RecType
|
||||||
, functions :: Vector Function
|
, functions :: Vector Function
|
||||||
|
, funcrefs :: Vector Funcref
|
||||||
, start :: Maybe Idx
|
, start :: Maybe Idx
|
||||||
, exports :: Vector Export
|
, exports :: Vector Export
|
||||||
, globals :: Vector Global
|
, globals :: Vector Global
|
||||||
@@ -97,10 +99,15 @@ instance Semigroup Module where
|
|||||||
, functions = m1.functions <> m2.functions
|
, functions = m1.functions <> m2.functions
|
||||||
, start = m2.start <|> m1.start
|
, start = m2.start <|> m1.start
|
||||||
, exports = m1.exports <> m2.exports
|
, exports = m1.exports <> m2.exports
|
||||||
|
, funcrefs = m1.funcrefs <> m2.funcrefs
|
||||||
|
, globals = m1.globals <> m2.globals
|
||||||
}
|
}
|
||||||
|
|
||||||
instance Monoid Module where
|
instance Monoid Module where
|
||||||
mempty = MkModule mempty mempty Nothing mempty mempty
|
mempty = MkModule mempty mempty mempty Nothing mempty mempty
|
||||||
|
|
||||||
|
newtype Funcref = MkFuncref { inner :: Idx }
|
||||||
|
deriving (Show, Generic)
|
||||||
|
|
||||||
data Global = MkGlobal
|
data Global = MkGlobal
|
||||||
{ ty :: Type
|
{ ty :: Type
|
||||||
@@ -144,6 +151,7 @@ data GenMod :: Effect where
|
|||||||
Start :: Idx -> GenMod m ()
|
Start :: Idx -> GenMod m ()
|
||||||
Export :: Text -> Text -> Idx -> GenMod m ()
|
Export :: Text -> Text -> Idx -> GenMod m ()
|
||||||
DefGlobal :: Type -> Expr -> GenMod m Idx
|
DefGlobal :: Type -> Expr -> GenMod m Idx
|
||||||
|
DeclareFuncref :: Idx -> GenMod m ()
|
||||||
|
|
||||||
type instance DispatchOf GenMod = Dynamic
|
type instance DispatchOf GenMod = Dynamic
|
||||||
|
|
||||||
@@ -174,6 +182,9 @@ defun
|
|||||||
-> Eff es Idx
|
-> Eff es Idx
|
||||||
defun params res locals code = send $ Defun params res locals code
|
defun params res locals code = send $ Defun params res locals code
|
||||||
|
|
||||||
|
declareFuncref :: GenMod :> es => Idx -> Eff es ()
|
||||||
|
declareFuncref = send . DeclareFuncref
|
||||||
|
|
||||||
-- defun
|
-- defun
|
||||||
-- :: (GenMod :> es)
|
-- :: (GenMod :> es)
|
||||||
-- => List Type -> List Type -> List Type
|
-- => List Type -> List Type -> List Type
|
||||||
@@ -211,6 +222,7 @@ runGenMod =
|
|||||||
let prev_n = IdxNumeric . fromIntegral . V.length $ m.globals
|
let prev_n = IdxNumeric . fromIntegral . V.length $ m.globals
|
||||||
m' = m & #globals <>~ V.singleton (MkGlobal t e)
|
m' = m & #globals <>~ V.singleton (MkGlobal t e)
|
||||||
in (prev_n, m')
|
in (prev_n, m')
|
||||||
|
_ (DeclareFuncref idx) -> #funcrefs <>= V.singleton (MkFuncref idx)
|
||||||
|
|
||||||
execGenMod = fmap snd . runGenMod
|
execGenMod = fmap snd . runGenMod
|
||||||
|
|
||||||
@@ -346,9 +358,19 @@ instance SexpIso Module where
|
|||||||
ParenList $
|
ParenList $
|
||||||
[ Symbol "module" ]
|
[ Symbol "module" ]
|
||||||
<> (m ^.. #types . each . to sxp)
|
<> (m ^.. #types . each . to sxp)
|
||||||
|
<> (m ^.. #funcrefs . each . to sxp)
|
||||||
<> (m ^.. #functions . each . to sxp)
|
<> (m ^.. #functions . each . to sxp)
|
||||||
<> (m ^.. #exports . each . to sxp)
|
<> (m ^.. #exports . each . to sxp)
|
||||||
|
|
||||||
|
instance SexpIso Funcref where
|
||||||
|
sexpIso = with \funcref ->
|
||||||
|
list ( el (sym "elem")
|
||||||
|
>>> el (sym "declare")
|
||||||
|
>>> el (sym "funcref")
|
||||||
|
>>> el (list $ el (sym "ref.func") >>> el (sexpIso @Idx))
|
||||||
|
)
|
||||||
|
>>> funcref
|
||||||
|
|
||||||
instance SexpIso Sexp where
|
instance SexpIso Sexp where
|
||||||
sexpIso = Control.Category.id
|
sexpIso = Control.Category.id
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user