wip: stack frames
This commit is contained in:
+27
-35
@@ -63,31 +63,22 @@ stackify g (ExpIf c t f) = do
|
||||
f' <- buildBlock <$> stackify g f
|
||||
pure . Tail $ Stk.If c' t' f'
|
||||
|
||||
stackify g (ExpApply f xs ktail) = pure $
|
||||
Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
|
||||
Tail (Stk.TailCall (stackifyVal g f) (k : (stackifyVal g <$> xs)))
|
||||
where
|
||||
k = var g ktail
|
||||
ls = fold $ (k ^? #ValImm . #ImmLabel)
|
||||
>>= \klbl -> g ^. #liveness . at klbl
|
||||
-- stackify g (ExpApply f xs ktail) = pure $
|
||||
-- Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
|
||||
-- Tail (Stk.TailCall (stackifyVal g f) (k : (stackifyVal g <$> xs)))
|
||||
-- where
|
||||
-- k = var g ktail
|
||||
-- ls = fold $ (k ^? #ValImm . #ImmLabel)
|
||||
-- >>= \klbl -> g ^. #liveness . at klbl
|
||||
|
||||
stackify g e@(ExpContinue k xs) = do
|
||||
pure $
|
||||
Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
|
||||
Tail (Stk.TailCall k' (stackifyVal g <$> xs))
|
||||
where
|
||||
k' = stackifyVal g k
|
||||
ls = fold $ (k' ^? #ValImm . #ImmLabel)
|
||||
>>= \klbl -> g ^. #liveness . at klbl
|
||||
|
||||
-- stackify g (ExpPrim (PrimCallCC withcc) cc) = do
|
||||
-- cc_l <- gensym' "cc"
|
||||
-- rcc_l <- gensym' "reified-cc"
|
||||
-- stackifyKappa g cc_l cc \g' rt -> do
|
||||
-- emitRoutine rt
|
||||
-- pure $
|
||||
-- Code [ Stk.Prim rcc_l PrimCaptureCC ] $
|
||||
-- Tail (Stk.TailCall (stackifyVal g' withcc) [Stk.ValLabel rcc_l])
|
||||
-- stackify g e@(ExpContinue k xs) = do
|
||||
-- pure $
|
||||
-- Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
|
||||
-- Tail (Stk.TailCall k' (stackifyVal g <$> xs))
|
||||
-- where
|
||||
-- k' = stackifyVal g k
|
||||
-- ls = fold $ (k' ^? #ValImm . #ImmLabel)
|
||||
-- >>= \klbl -> g ^. #liveness . at klbl
|
||||
|
||||
stackify g (ExpPrim p (MkKappa [x] e)) = do
|
||||
e' <- stackify (g & #bound . at x ?~ Stk.ValReg x) e
|
||||
@@ -105,16 +96,17 @@ stackifyKappa
|
||||
=> Env -> Name -> Kappa
|
||||
-> (Env -> Stk.Routine -> Eff es r)
|
||||
-> Eff es r
|
||||
stackifyKappa g name kap@(MkKappa xs m) w = do
|
||||
let vs = (name, Stk.ValLabel name) : (bindReg <$> xs)
|
||||
let ls = live g kap
|
||||
m' <- stackify (g & #bound <>~ H.fromList (vs ++ (bindReg <$> ls))) m
|
||||
let g' = g & #bound . at name ?~ Stk.ValLabel name
|
||||
& #liveness . at name ?~ live g kap
|
||||
let rt = Stk.MkRoutine name xs . buildBlock $
|
||||
-- pop in the opposite order we push
|
||||
Code [Stk.Pop x | x <- reverse ls] m'
|
||||
w g' rt
|
||||
stackifyKappa g name kap@(MkKappa xs m) w = _
|
||||
-- stackifyKappa g name kap@(MkKappa xs m) w = do
|
||||
-- let vs = (name, Stk.ValLabel name) : (bindReg <$> xs)
|
||||
-- let ls = live g kap
|
||||
-- m' <- stackify (g & #bound <>~ H.fromList (vs ++ (bindReg <$> ls))) m
|
||||
-- let g' = g & #bound . at name ?~ Stk.ValLabel name
|
||||
-- & #liveness . at name ?~ live g kap
|
||||
-- let rt = Stk.MkRoutine name xs . buildBlock $
|
||||
-- -- pop in the opposite order we push
|
||||
-- Code [Stk.Pop x | x <- reverse ls] m'
|
||||
-- w g' rt
|
||||
|
||||
stackifyLambda
|
||||
:: (Stackify :> es, GenSym :> es)
|
||||
@@ -125,7 +117,7 @@ stackifyLambda g name (MkLambda xs k m) w = do
|
||||
let vs = [ (x, Stk.ValReg x) | x <- k:xs ]
|
||||
m' <- stackify (g & #bound <>~ H.fromList vs) m
|
||||
let g' = g & #bound . at name ?~ Stk.ValLabel name
|
||||
w g' $ Stk.MkRoutine name (k:xs) (buildBlock m')
|
||||
w g' $ Stk.MkRoutine name (buildBlock m')
|
||||
|
||||
stackifyVal :: Env -> Val -> Stk.Val
|
||||
stackifyVal g = \case
|
||||
|
||||
@@ -45,7 +45,6 @@ instance IsList Program where
|
||||
|
||||
data Routine = MkRoutine
|
||||
{ label :: Name
|
||||
, params :: List Name
|
||||
, start :: Block
|
||||
}
|
||||
deriving stock (Show, Generic, Data)
|
||||
@@ -59,10 +58,12 @@ data Block = MkBlock
|
||||
deriving anyclass (NFData)
|
||||
|
||||
data Tail
|
||||
= TailCall Val (List Val)
|
||||
-- | call the procedure at stack index `n` supplied with `n`
|
||||
-- arguments on top of the stack, then return by calling the
|
||||
-- continuation at stack index `n+1`.
|
||||
= TailCall Int
|
||||
| If Val Block Block
|
||||
-- | Invoke a reified continuation.
|
||||
| InvokeC Val
|
||||
| Return
|
||||
deriving stock (Show, Generic, Data)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
@@ -105,11 +106,12 @@ instance S.DataIso Block where
|
||||
|
||||
instance S.DatumIso Tail where
|
||||
datumIso = S.match
|
||||
$ S.With (S.headTagged1' "tail-call" S.datumIso S.datumIso >>>)
|
||||
$ S.With (S.headTagged1 "tail-call" S.datumIso >>>)
|
||||
$ S.With (if_ >>>)
|
||||
$ S.With (S.headTagged1 "invoke/c" S.datumIso >>>)
|
||||
$ S.With (S.headTagged0 "return" >>>)
|
||||
$ S.End
|
||||
where
|
||||
-- if_ = S.ifLike "if" (S.datumIso @Val) S.datumIso S.datumIso
|
||||
if_ = S.ifLike "if" (S.datumIso @Val) (branch "then") (branch "else")
|
||||
branch :: Text -> S.DatumGrammar Block
|
||||
branch s =
|
||||
@@ -127,7 +129,7 @@ instance S.DatumIso Routine where
|
||||
datumIso = S.with \rout ->
|
||||
S.listWithIndentation (S.NSpecial 1)
|
||||
( S.el (S.decorate S.SynBuiltin >>> S.sym "define")
|
||||
>>> S.el (S.list $ S.el labelName >>> S.rest regName)
|
||||
>>> S.el labelName
|
||||
>>> S.restData (S.dataIso @Block)
|
||||
)
|
||||
>>> rout
|
||||
|
||||
+94
-18
@@ -12,7 +12,7 @@ module Gyehoek.Stack.VM
|
||||
import Gyehoek.Stack.Syntax
|
||||
import Control.Lens
|
||||
import qualified Data.HashMap.Strict as H
|
||||
import Data.List (unfoldr, intersperse)
|
||||
import Data.List (unfoldr, intersperse, compareLength)
|
||||
import Gyehoek.Prelude
|
||||
import qualified Data.List.NonEmpty as NE
|
||||
import Lucid
|
||||
@@ -36,8 +36,18 @@ data DebugVM = MkDebugVM
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
data Frame = MkFrame
|
||||
{ returnAddress :: Name
|
||||
, procedure :: Name
|
||||
, locals :: List Obj
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
newtype Stack = MkStack (NonEmpty Frame)
|
||||
deriving (Show, Generic)
|
||||
|
||||
data VM = MkVM
|
||||
{ stack :: List Obj
|
||||
{ frames :: NonEmpty Frame
|
||||
, code :: List Instr
|
||||
, tail :: Tail
|
||||
, registers :: HashMap Name Obj
|
||||
@@ -47,6 +57,14 @@ data VM = MkVM
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
_NonEmpty :: Iso (NonEmpty a) (NonEmpty b) (a, List a) (b, List b)
|
||||
_NonEmpty = iso
|
||||
(\(x:|xs) -> (x,xs))
|
||||
(\(x,xs) -> x:|xs)
|
||||
|
||||
stack :: Lens' VM (List Obj)
|
||||
stack = #frames . _NonEmpty . _1 . #locals
|
||||
|
||||
data Env = MkEnv
|
||||
{ labels :: HashMap Name Routine
|
||||
}
|
||||
@@ -62,7 +80,7 @@ vmerror = throwError . VMError
|
||||
|
||||
stepI :: Jalmot :> es => Env -> VM -> Instr -> Eff es VM
|
||||
|
||||
stepI e vm (Push v) = traverseOf #stack push vm
|
||||
stepI e vm (Push v) = traverseOf stack push vm
|
||||
where push xs = (:) <$> evalVal e vm v <*> pure xs
|
||||
|
||||
stepI e vm (Prim r p) = traverse (evalVal e vm) p >>= \case
|
||||
@@ -99,27 +117,52 @@ stepI e vm (Prim r p) = traverse (evalVal e vm) p >>= \case
|
||||
ret $ ObjImm (ImmInt (op x y))
|
||||
arith_binop _ x y = vmerror [i|bad arith: #{x}, #{y}|]
|
||||
|
||||
stepI e vm (Pop r) = case vm ^. #stack of
|
||||
stepI e vm (Pop r) = case vm ^. stack of
|
||||
[] -> vmerror "empty stack"
|
||||
(x:xs) -> pure $ vm & #registers . at r ?~ x
|
||||
& #stack .~ xs
|
||||
& stack .~ xs
|
||||
|
||||
stepI e vm ins = vmerror [i|unimplemented instruction: #{ins}|]
|
||||
|
||||
stepT :: Jalmot :> es => Env -> VM -> Tail -> Eff es VM
|
||||
|
||||
stepT g vm (TailCall f xs) = do
|
||||
xs' <- traverse (evalVal g vm) xs
|
||||
evalToLabel g vm f >>= \case
|
||||
"halt" -> pure $ vm & #result ?~ xs'
|
||||
l -> do
|
||||
rt <- case g ^. #labels . at l of
|
||||
Nothing -> vmerror [i|undefined label: #{l}|]
|
||||
Just x -> pure x
|
||||
pure $ vm & #code .~ rt.start.code
|
||||
stepT g vm Return = vmerror "aajak"
|
||||
|
||||
stepT g vm tc@(TailCall nargs) =
|
||||
case setupCall nargs (vm ^. stack) of
|
||||
Nothing -> vmerror [i|bad stack at #{tc}|]
|
||||
Just (xs,f,rest) ->
|
||||
case f of
|
||||
"halt" -> pure $ vm & #result ?~ xs
|
||||
l -> do
|
||||
rt <- case g ^. #labels . at l of
|
||||
Nothing -> vmerror [i|undefined label: #{l}|]
|
||||
Just x -> pure x
|
||||
let newFrame = MkFrame
|
||||
{ returnAddress =
|
||||
vm ^. #frames . _NonEmpty . _1 . #returnAddress
|
||||
, procedure = f
|
||||
, locals = xs
|
||||
}
|
||||
pure $ vm
|
||||
& #code .~ rt.start.code
|
||||
& #tail .~ rt.start.tail
|
||||
& #registers .~ H.fromList (rt.params `zip` xs')
|
||||
& stack .~ rest
|
||||
& #frames %~ NE.cons newFrame
|
||||
-- it is not essential we clear the registers, but it'll
|
||||
-- make bugs more obvious.
|
||||
& #registers .~ mempty
|
||||
& #debug . #currentRoutine .~ rt.label
|
||||
-- evalToLabel g vm f >>= \case
|
||||
-- "halt" -> pure $ vm & #result ?~ xs'
|
||||
-- l -> do
|
||||
-- rt <- case g ^. #labels . at l of
|
||||
-- Nothing -> vmerror [i|undefined label: #{l}|]
|
||||
-- Just x -> pure x
|
||||
-- pure $ vm & #code .~ rt.start.code
|
||||
-- & #tail .~ rt.start.tail
|
||||
-- & #registers .~ H.fromList (rt.params `zip` xs')
|
||||
-- & #debug . #currentRoutine .~ rt.label
|
||||
|
||||
stepT g vm (If c t f) = do
|
||||
branch <- evalVal g vm c <&> \case
|
||||
@@ -140,11 +183,39 @@ evalVal e vm = \case
|
||||
Just x -> pure x
|
||||
Nothing -> vmerror [i|undefined register: #{r}|]
|
||||
|
||||
splitAtExact :: Int -> List a -> Maybe (List a, List a)
|
||||
splitAtExact n xs = case compareLength xs n of
|
||||
(EQ;GT) -> Just $ splitAt n xs
|
||||
LT -> Nothing
|
||||
|
||||
setupCall :: Int -> List Obj -> Maybe (List Obj, Name, Name, List Obj)
|
||||
setupCall n stk = do
|
||||
(xs,stk') <- splitAtExact (n+2) stk
|
||||
case splitAtExact n xs of
|
||||
Just (args, [p,ret]) -> case (p,ret) of
|
||||
(ObjImm (ImmLabel p'),ObjImm (ImmLabel ret')) -> Just (args,p',ret',stk')
|
||||
_ -> Nothing
|
||||
_ -> error "unreachable"
|
||||
|
||||
setupTailCall :: Int -> List Obj -> Maybe (List Obj, Name, List Obj)
|
||||
setupTailCall n stk = do
|
||||
(xs,stk') <- splitAtExact (n+1) stk
|
||||
case xs ^? _Snoc of
|
||||
Just (args, [p,ret]) -> case (p,ret) of
|
||||
(ObjImm (ImmLabel p'),ObjImm (ImmLabel ret')) -> Just (args,p',stk')
|
||||
_ -> Nothing
|
||||
_ -> error "unreachable"
|
||||
|
||||
initialVM :: VM
|
||||
initialVM = MkVM
|
||||
{ stack = []
|
||||
{ frames = NE.singleton $ MkFrame
|
||||
{ returnAddress = "halt"
|
||||
, procedure = "<nowhere at all>"
|
||||
, locals = [ ObjImm (ImmLabel "start")
|
||||
]
|
||||
}
|
||||
, tail = TailCall 0
|
||||
, code = []
|
||||
, tail = TailCall (ValLabel "start") [ValLabel "halt"]
|
||||
, registers = mempty
|
||||
, stdout = ""
|
||||
, result = Nothing
|
||||
@@ -288,10 +359,15 @@ ppVM vm = do
|
||||
td_ do
|
||||
code_ curi
|
||||
td_ do
|
||||
let xs = code_ . ppDatum <$> (vm ^. #stack)
|
||||
let xs = code_ . ppDatum <$> (vm ^. stack)
|
||||
sequence_ $ intersperse " | " xs
|
||||
where
|
||||
curi = vm ^?! failing (#code . _head . to ppDatum) (#tail . to ppDatum)
|
||||
|
||||
ppDatum :: S.DatumIso a => a -> Html ()
|
||||
ppDatum = htmlDatum . runJalmotUnsafe . S.toDatum S.datumIso
|
||||
|
||||
blah = [stkP|
|
||||
(define $start
|
||||
(return))
|
||||
|]
|
||||
|
||||
Reference in New Issue
Block a user