This commit is contained in:
@@ -109,8 +109,10 @@ convertLambda bs m = do
|
|||||||
pure [cps|(λ (##{bs} #{ktail}) #{m'})|]
|
pure [cps|(λ (##{bs} #{ktail}) #{m'})|]
|
||||||
|
|
||||||
convertProgram :: forall es. (GenSym :> es) => Scm.Program -> Eff es Program
|
convertProgram :: forall es. (GenSym :> es) => Scm.Program -> Eff es Program
|
||||||
convertProgram p =
|
convertProgram p = do
|
||||||
MkProgram <$> telescope (convert @es) (p ^.. each . _Left) (pure . Halt)
|
let nothalt = ExpContinue (ValVar "main-ktail")
|
||||||
|
e <- telescope (convert @es) (p ^.. each . _Left) (pure . nothalt)
|
||||||
|
_
|
||||||
|
|
||||||
convertExp :: forall es. (GenSym :> es) => Scm.Exp -> Eff es Exp
|
convertExp :: forall es. (GenSym :> es) => Scm.Exp -> Eff es Exp
|
||||||
convertExp e = convert e (pure . Halt1)
|
convertExp e = convert e (pure . Halt1)
|
||||||
|
|||||||
@@ -69,27 +69,15 @@ stackify g (ExpIf c t f) = do
|
|||||||
pure . Tail $ Stk.If c' t' f'
|
pure . Tail $ Stk.If c' t' f'
|
||||||
|
|
||||||
stackify g (ExpApply f xs ktail) = pure $
|
stackify g (ExpApply f xs ktail) = pure $
|
||||||
Code [ Stk.PushCont k ] $
|
|
||||||
Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
|
Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
|
||||||
Tail (Stk.TailCall (stackifyVal g f) (stackifyVal g <$> xs))
|
Tail (Stk.PushCall k (stackifyVal g f) (stackifyVal g <$> xs))
|
||||||
where
|
where
|
||||||
k = var g ktail
|
k = var g ktail
|
||||||
ls = fold $ (k ^? #ValImm . #ImmLabel)
|
ls = fold $ (k ^? #ValImm . #ImmLabel)
|
||||||
>>= \klbl -> g ^. #liveness . at klbl
|
>>= \klbl -> g ^. #liveness . at klbl
|
||||||
|
|
||||||
stackify g (ExpContinue k xs) =
|
stackify g (ExpContinue k xs) =
|
||||||
-- return continuations require popping the stack. how do we know
|
pure . Tail $ Stk.TailCall (stackifyVal g k) (stackifyVal g <$> xs)
|
||||||
-- when a continuation is a return continuation? is this a correct
|
|
||||||
-- test?
|
|
||||||
case elemIndex k g.contStack of
|
|
||||||
Nothing -> pure . Tail $ Stk.TailCall (Stk.ValLabel k) xs'
|
|
||||||
Just j -> do
|
|
||||||
ktail <- gensym' @Name $ k ^. _Wrapped'
|
|
||||||
pure $
|
|
||||||
Code (replicate j $ Stk.PopCont "_") $
|
|
||||||
Code [Stk.PopCont ktail] $
|
|
||||||
Tail (Stk.TailCall (Stk.ValReg ktail) xs')
|
|
||||||
where xs' = stackifyVal g <$> xs
|
|
||||||
|
|
||||||
stackify g (ExpPrim p (MkKappa [x] e)) = do
|
stackify g (ExpPrim p (MkKappa [x] e)) = do
|
||||||
e' <- stackify (g & #bound . at x ?~ Stk.ValReg x) e
|
e' <- stackify (g & #bound . at x ?~ Stk.ValReg x) e
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ lowerBlock = _
|
|||||||
|
|
||||||
lowerInstr :: Instr -> Wasm.Expr
|
lowerInstr :: Instr -> Wasm.Expr
|
||||||
lowerInstr = \case
|
lowerInstr = \case
|
||||||
PopCont ktail -> [wat|
|
-- PopCont ktail -> [wat|
|
||||||
|
|
||||||
|]
|
-- |]
|
||||||
|
|
||||||
lowerProgram :: Program -> Eff es Wasm.Module
|
lowerProgram :: Program -> Eff es Wasm.Module
|
||||||
lowerProgram p = pure [watM|
|
lowerProgram p = pure [watM|
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ data Block = MkBlock
|
|||||||
|
|
||||||
data Tail
|
data Tail
|
||||||
= TailCall Val (List Val)
|
= TailCall Val (List Val)
|
||||||
|
| PushCall Val Val (List Val)
|
||||||
| If Val Block Block
|
| If Val Block Block
|
||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
@@ -67,8 +68,6 @@ data Tail
|
|||||||
data Instr
|
data Instr
|
||||||
= Pop Name
|
= Pop Name
|
||||||
| Push Val
|
| Push Val
|
||||||
| PopCont Name
|
|
||||||
| PushCont Val
|
|
||||||
| Prim Name (Prim Val)
|
| Prim Name (Prim Val)
|
||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
@@ -91,8 +90,6 @@ instance S.DatumIso Instr where
|
|||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (S.headTagged1 "pop!" regName >>>)
|
$ S.With (S.headTagged1 "pop!" regName >>>)
|
||||||
$ S.With (S.headTagged1 "push!" S.datumIso >>>)
|
$ S.With (S.headTagged1 "push!" S.datumIso >>>)
|
||||||
$ S.With (S.headTagged1 "pop-cont!" regName >>>)
|
|
||||||
$ S.With (S.headTagged1 "push-cont!" S.datumIso >>>)
|
|
||||||
$ S.With (S.headTagged2 "prim" regName S.datumIso >>>)
|
$ S.With (S.headTagged2 "prim" regName S.datumIso >>>)
|
||||||
$ S.End
|
$ S.End
|
||||||
where
|
where
|
||||||
@@ -108,6 +105,7 @@ instance S.DataIso Block where
|
|||||||
instance S.DatumIso Tail where
|
instance S.DatumIso Tail where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (S.headTagged1' "tail-call" S.datumIso S.datumIso >>>)
|
$ S.With (S.headTagged1' "tail-call" S.datumIso S.datumIso >>>)
|
||||||
|
$ S.With (S.headTagged2' "push-call" S.datumIso S.datumIso S.datumIso >>>)
|
||||||
$ S.With (if_ >>>)
|
$ S.With (if_ >>>)
|
||||||
$ S.End
|
$ S.End
|
||||||
where
|
where
|
||||||
|
|||||||
+4
-10
@@ -17,7 +17,6 @@ import Gyehoek.Prelude
|
|||||||
|
|
||||||
data VM = MkVM
|
data VM = MkVM
|
||||||
{ stack :: List Obj
|
{ stack :: List Obj
|
||||||
, kstack :: List Name
|
|
||||||
, code :: List Instr
|
, code :: List Instr
|
||||||
, tail :: Tail
|
, tail :: Tail
|
||||||
, registers :: HashMap Name Obj
|
, registers :: HashMap Name Obj
|
||||||
@@ -40,8 +39,6 @@ stepI :: Env -> VM -> Instr -> VM
|
|||||||
|
|
||||||
stepI e vm (Push v) = vm & #stack %~ (evalVal e vm v :)
|
stepI e vm (Push v) = vm & #stack %~ (evalVal e vm v :)
|
||||||
|
|
||||||
stepI e vm (PushCont k) = vm & #kstack %~ (evalToLabel e vm k :)
|
|
||||||
|
|
||||||
stepI e vm (Prim r p) = case evalVal e vm <$> p of
|
stepI e vm (Prim r p) = case evalVal e vm <$> p of
|
||||||
PrimZeroP x -> case x of
|
PrimZeroP x -> case x of
|
||||||
ObjImm (ImmInt n) -> ret . ObjImm . ImmBool $ n == 0
|
ObjImm (ImmInt n) -> ret . ObjImm . ImmBool $ n == 0
|
||||||
@@ -74,11 +71,6 @@ stepI e vm (Pop r) = case vm ^. #stack of
|
|||||||
(x:xs) -> vm & #registers . at r ?~ x
|
(x:xs) -> vm & #registers . at r ?~ x
|
||||||
& #stack .~ xs
|
& #stack .~ xs
|
||||||
|
|
||||||
stepI e vm ins@(PopCont r) = case vm ^. #kstack of
|
|
||||||
[] -> error [i|empty cont stack: #{ins}|]
|
|
||||||
(x:xs) -> vm & #registers . at r ?~ ObjImm (ImmLabel x)
|
|
||||||
& #kstack .~ xs
|
|
||||||
|
|
||||||
stepI e vm ins = error [i|unimplemented instruction: #{ins}|]
|
stepI e vm ins = error [i|unimplemented instruction: #{ins}|]
|
||||||
|
|
||||||
stepT :: Env -> VM -> Tail -> VM
|
stepT :: Env -> VM -> Tail -> VM
|
||||||
@@ -95,6 +87,9 @@ stepT g vm (TailCall f xs) =
|
|||||||
Nothing -> error [i|undefined label: #{l}|]
|
Nothing -> error [i|undefined label: #{l}|]
|
||||||
Just x -> x
|
Just x -> x
|
||||||
|
|
||||||
|
stepT g vm (PushCall k f xs) =
|
||||||
|
_
|
||||||
|
|
||||||
stepT g vm (If c t f) = vm & #code .~ branch.code & #tail .~ branch.tail
|
stepT g vm (If c t f) = vm & #code .~ branch.code & #tail .~ branch.tail
|
||||||
where
|
where
|
||||||
branch = case evalVal g vm c of
|
branch = case evalVal g vm c of
|
||||||
@@ -116,9 +111,8 @@ evalVal e vm = \case
|
|||||||
initialVM :: VM
|
initialVM :: VM
|
||||||
initialVM = MkVM
|
initialVM = MkVM
|
||||||
{ stack = []
|
{ stack = []
|
||||||
, kstack = ["halt"]
|
|
||||||
, code = []
|
, code = []
|
||||||
, tail = TailCall (ValLabel "main") []
|
, tail = TailCall (ValLabel "main") [ValLabel "ktail"]
|
||||||
, registers = mempty
|
, registers = mempty
|
||||||
, stdout = ""
|
, stdout = ""
|
||||||
, result = Nothing
|
, result = Nothing
|
||||||
|
|||||||
Reference in New Issue
Block a user