register & label newtypes

This commit is contained in:
2026-08-28 11:39:37 -06:00
parent 9cb169f9b8
commit 5ccb3f3e1a
4 changed files with 58 additions and 40 deletions
+11 -11
View File
@@ -48,12 +48,12 @@ stackify
=> Env -> Exp -> Eff es BlockBuilder => Env -> Exp -> Eff es BlockBuilder
stackify g (ExpLetRec [(f, AbsKappa kap)] e) = do stackify g (ExpLetRec [(f, AbsKappa kap)] e) = do
stackifyKappa g f kap \g' kap' -> do stackifyKappa g (MkLabel f) kap \g' kap' -> do
emitRoutine kap' emitRoutine kap'
stackify g' e stackify g' e
stackify g (ExpLetRec [(f, AbsLambda lam)] e) = do stackify g (ExpLetRec [(f, AbsLambda lam)] e) = do
stackifyLambda g f lam \g' lam' -> do stackifyLambda g (MkLabel f) lam \g' lam' -> do
emitRoutine lam' emitRoutine lam'
stackify g' e stackify g' e
@@ -81,19 +81,19 @@ stackify g (ExpIf c t f) = do
-- >>= \klbl -> g ^. #liveness . at klbl -- >>= \klbl -> g ^. #liveness . at klbl
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 (MkReg x)) e
pure $ pure $
Code [ Stk.Prim x (stackifyVal g <$> p) ] e' Code [ Stk.Prim (MkReg x) (stackifyVal g <$> p) ] e'
stackify _ e = error [i|unimplemented exp: #{e}|] stackify _ e = error [i|unimplemented exp: #{e}|]
-- affine -- affine
_ValName :: Traversal' Val Name _ValName :: Traversal' Val Name
_ValName = failing #ValVar (#ValImm . #ImmLabel) _ValName = failing #ValVar (#ValImm . #ImmLabel . #MkLabel)
stackifyKappa stackifyKappa
:: (Stackify :> es, GenSym :> es) :: (Stackify :> es, GenSym :> es)
=> Env -> Name -> Kappa => Env -> Label -> Kappa
-> (Env -> Stk.Routine -> Eff es r) -> (Env -> Stk.Routine -> Eff es r)
-> Eff es r -> Eff es r
stackifyKappa g name kap@(MkKappa xs m) w = _ stackifyKappa g name kap@(MkKappa xs m) w = _
@@ -110,13 +110,13 @@ stackifyKappa g name kap@(MkKappa xs m) w = _
stackifyLambda stackifyLambda
:: (Stackify :> es, GenSym :> es) :: (Stackify :> es, GenSym :> es)
=> Env -> Name -> Lambda => Env -> Label -> Lambda
-> (Env -> Stk.Routine -> Eff es r) -> (Env -> Stk.Routine -> Eff es r)
-> Eff es r -> Eff es r
stackifyLambda g name (MkLambda xs k m) w = do stackifyLambda g name (MkLambda xs k m) w = do
let vs = [ (x, Stk.ValReg x) | x <- k:xs ] let vs = [ (x, Stk.ValReg (MkReg x)) | x <- k:xs ]
m' <- stackify (g & #bound <>~ H.fromList vs) m m' <- stackify (g & #bound <>~ H.fromList vs) m
let g' = g & #bound . at name ?~ Stk.ValLabel name let g' = g & #bound . at (name ^. wrappedIso) ?~ Stk.ValLabel name
w g' $ Stk.MkRoutine name (buildBlock m') w g' $ Stk.MkRoutine name (buildBlock m')
stackifyVal :: Env -> Val -> Stk.Val stackifyVal :: Env -> Val -> Stk.Val
@@ -128,10 +128,10 @@ stackifyVal g = \case
var :: Env -> Name -> Stk.Val var :: Env -> Name -> Stk.Val
var g v = case g ^. #bound . at v of var g v = case g ^. #bound . at v of
Just x -> x Just x -> x
Nothing -> Stk.ValLabel v Nothing -> Stk.ValLabel (MkLabel v)
bindReg :: Name -> (Name, Stk.Val) bindReg :: Name -> (Name, Stk.Val)
bindReg x = (x, Stk.ValReg x) bindReg x = (x, Stk.ValReg (MkReg x))
+32 -11
View File
@@ -18,6 +18,8 @@ module Gyehoek.CPS.Syntax
, Imm(..) , Imm(..)
, Obj(..) , Obj(..)
, Hob(..) , Hob(..)
, Label(..)
, Reg(..)
, pattern Halt , pattern Halt
, pattern Halt1 , pattern Halt1
, _MkKappa , _MkKappa
@@ -37,7 +39,6 @@ module Gyehoek.CPS.Syntax
, Free(..) , Free(..)
, pattern ValLabel , pattern ValLabel
, pattern ObjLabel , pattern ObjLabel
, labelName -- don't like that this is part of the api
) )
where where
@@ -53,6 +54,8 @@ import Gyehoek.Prelude hiding (op)
import Gyehoek.Sexp (Datum) import Gyehoek.Sexp (Datum)
import Gyehoek.Sexp (G, (:-)(..)) import Gyehoek.Sexp (G, (:-)(..))
import qualified Data.InvertibleGrammar.Base as IG import qualified Data.InvertibleGrammar.Base as IG
import Gyehoek.GenSym (Gen)
import Data.String (IsString)
-- Data types -- Data types
@@ -61,13 +64,23 @@ data Val
| ValVar Name | ValVar Name
deriving (Show, Generic, Data, Eq) deriving (Show, Generic, Data, Eq)
pattern ValLabel :: Name -> Val pattern ValLabel :: Label -> Val
pattern ValLabel x = ValImm (ImmLabel x) pattern ValLabel x = ValImm (ImmLabel x)
newtype Label = MkLabel { inner :: Name }
deriving stock (Generic, Data)
deriving newtype (Show, Eq, Gen, IsString, Hashable)
deriving anyclass (NFData)
newtype Reg = MkReg { inner :: Name }
deriving stock (Generic, Data)
deriving newtype (Show, Eq, Gen, IsString, Hashable)
deriving anyclass (NFData)
data Imm data Imm
= ImmInt Int = ImmInt Int
| ImmBool Bool | ImmBool Bool
| ImmLabel Name | ImmLabel Label
| ImmUndefined | ImmUndefined
deriving stock (Show, Generic, Data, Eq) deriving stock (Show, Generic, Data, Eq)
deriving anyclass (NFData) deriving anyclass (NFData)
@@ -172,17 +185,25 @@ instance S.DatumIso Imm where
datumIso = S.match datumIso = S.match
$ S.With (. S.int) $ S.With (. S.int)
$ S.With (. S.datumIso) $ S.With (. S.datumIso)
$ S.With (. labelName) $ S.With (. S.datumIso)
$ S.With (. S.unreadable (const "#<undefined>")) $ S.With (. S.unreadable (const "#<undefined>"))
$ S.End $ S.End
labelName :: S.DatumGrammar Name instance S.DatumIso Label where
labelName = S.coproduct datumIso = S.with \g -> S.coproduct
[ S.decorate S.SynConstant >>> S.datumIso @Name >>> S.prismIso [ S.decorate S.SynConstant >>> S.datumIso @Name >>> S.prismIso
(S.expected "label") (S.expected "label")
(prefixed @Name "$") (prefixed @Name "$")
, S.list $ S.el (S.sym "$") >>> S.el (S.datumIso @Name) , S.list $ S.el (S.sym "$") >>> S.el (S.datumIso @Name)
] ]
>>> g
instance S.DatumIso Reg where
datumIso = S.with \g ->
S.decorate S.SynVariable >>> S.datumIso @Name >>> S.prismIso
(S.expected "register")
(prefixed @Name "%")
>>> g
instance S.DatumIso Hob where instance S.DatumIso Hob where
datumIso = S.match datumIso = S.match
+2
View File
@@ -19,6 +19,7 @@ module Gyehoek.Prelude
, (>>>) , (>>>)
, (>=>) , (>=>)
, (<=<) , (<=<)
, wrappedIso
) where ) where
import Control.Lens hiding (List, (:<)) import Control.Lens hiding (List, (:<))
@@ -40,4 +41,5 @@ import Data.List.NonEmpty (NonEmpty((:|)))
import Numeric.Natural (Natural) import Numeric.Natural (Natural)
import Control.Category ((>>>)) import Control.Category ((>>>))
import Control.Monad import Control.Monad
import Data.Generics.Wrapped (Wrapped(..))
+13 -18
View File
@@ -25,13 +25,13 @@ import qualified Gyehoek.Sexp as S
import Gyehoek.Scheme.Syntax (Name(..), Lit(..), Prim(..)) import Gyehoek.Scheme.Syntax (Name(..), Lit(..), Prim(..))
import GHC.Exts (IsList(..)) import GHC.Exts (IsList(..))
import Data.List (intersperse) import Data.List (intersperse)
import Gyehoek.CPS.Syntax (Imm(..), Obj(..), Hob(..), labelName, pattern ObjLabel) import Gyehoek.CPS.Syntax (Imm(..), Obj(..), Hob(..), pattern ObjLabel, Reg, Label)
import Gyehoek.Prelude import Gyehoek.Prelude
import Gyehoek.Sexp ((:-)((:-))) import Gyehoek.Sexp ((:-)((:-)))
newtype Program = MkProgram newtype Program = MkProgram
{ routines :: HashMap Name Routine { routines :: HashMap Label Routine
} }
deriving stock (Show, Generic, Data) deriving stock (Show, Generic, Data)
deriving newtype (Semigroup, Monoid) deriving newtype (Semigroup, Monoid)
@@ -45,7 +45,7 @@ instance IsList Program where
toList = toListOf $ #routines . each toList = toListOf $ #routines . each
data Routine = MkRoutine data Routine = MkRoutine
{ label :: Name { label :: Label
, start :: Block , start :: Block
} }
deriving stock (Show, Generic, Data) deriving stock (Show, Generic, Data)
@@ -70,20 +70,20 @@ data Tail
deriving anyclass (NFData) deriving anyclass (NFData)
data Instr data Instr
= Pop Name = Pop Reg
| Push Val | Push Val
| Load Name | Load Reg
| Prim Name (Prim Val) | Prim Reg (Prim Val)
deriving stock (Show, Generic, Data) deriving stock (Show, Generic, Data)
deriving anyclass (NFData) deriving anyclass (NFData)
data Val data Val
= ValReg Name = ValReg Reg
| ValImm Imm | ValImm Imm
deriving stock (Show, Generic, Data, Eq) deriving stock (Show, Generic, Data, Eq)
deriving anyclass (NFData) deriving anyclass (NFData)
pattern ValLabel :: Name -> Val pattern ValLabel :: Label -> Val
pattern ValLabel x = ValImm (ImmLabel x) pattern ValLabel x = ValImm (ImmLabel x)
@@ -93,10 +93,10 @@ pure []
instance S.DatumIso Instr where instance S.DatumIso Instr where
datumIso = S.match datumIso = S.match
$ S.With (S.headTagged1 "pop!" regName >>>) $ S.With (S.headTagged1 "pop!" S.datumIso >>>)
$ S.With (S.headTagged1 "push!" S.datumIso >>>) $ S.With (S.headTagged1 "push!" S.datumIso >>>)
$ S.With (S.headTagged1 "load!" regName >>>) $ S.With (S.headTagged1 "load!" S.datumIso >>>)
$ S.With (S.headTagged2 "prim" regName S.datumIso >>>) $ S.With (S.headTagged2 "prim" S.datumIso S.datumIso >>>)
$ S.End $ S.End
where where
@@ -126,7 +126,7 @@ instance S.DatumIso Tail where
instance S.DatumIso Val where instance S.DatumIso Val where
datumIso = S.match datumIso = S.match
$ S.With (regName >>>) $ S.With (S.datumIso >>>)
$ S.With (S.datumIso >>>) $ S.With (S.datumIso >>>)
$ S.End $ S.End
@@ -134,16 +134,11 @@ instance S.DatumIso Routine where
datumIso = S.with \rout -> datumIso = S.with \rout ->
S.listWithIndentation (S.NSpecial 1) S.listWithIndentation (S.NSpecial 1)
( S.el (S.decorate S.SynBuiltin >>> S.sym "define") ( S.el (S.decorate S.SynBuiltin >>> S.sym "define")
>>> S.el labelName >>> S.el (S.datumIso @Label)
>>> S.restData (S.dataIso @Block) >>> S.restData (S.dataIso @Block)
) )
>>> rout >>> rout
regName :: S.DatumGrammar Name
regName = S.decorate S.SynVariable >>> S.datumIso @Name >>> S.prismIso
(S.expected "register")
(prefixed @Name "%")
instance S.DataIso Program where instance S.DataIso Program where
dataIso = S.dataIso @(List Routine) >>> S.iso fromList toList dataIso = S.dataIso @(List Routine) >>> S.iso fromList toList