This commit is contained in:
2026-05-06 02:39:24 -06:00
parent 84326faf47
commit 42acdd4e5a

View File

@@ -17,7 +17,7 @@ import Data.List (List)
import Data.Text.IO qualified as TIO import Data.Text.IO qualified as TIO
import Control.Lens import Control.Lens
import Data.Generics.Labels import Data.Generics.Labels
import Data.Vector (Vector) import Data.Vector.Strict (Vector)
import Data.Function (fix) import Data.Function (fix)
import Effectful.Writer.Static.Local import Effectful.Writer.Static.Local
import Gyehoek.Syntax qualified as Lam import Gyehoek.Syntax qualified as Lam
@@ -25,7 +25,7 @@ import Gyehoek.Syntax (Name, Prim(..), Val(..))
import Gyehoek.GenSym import Gyehoek.GenSym
import Control.Monad.Cont import Control.Monad.Cont
import Data.Foldable import Data.Foldable
import Data.List.NonEmpty (NonEmpty((:|))) import Data.List.NonEmpty (NonEmpty((:|)), toList)
import Gyehoek.QBE (FuncDef(FuncDef)) import Gyehoek.QBE (FuncDef(FuncDef))
import Data.Foldable1 import Data.Foldable1
@@ -131,36 +131,54 @@ prims = QBE.Program mempty mempty primfns where
(QBE.Ret (Just (QBE.ValTemporary "r"))) (QBE.Ret (Just (QBE.ValTemporary "r")))
] ]
data BlockBuilder
= Emit (Vector QBE.Inst) !BlockBuilder
| Exit QBE.Jump
deriving (Show)
instance Each BlockBuilder BlockBuilder QBE.Inst QBE.Inst where
each k (Emit is bb) = Emit <$> traverse k is <*> each k bb
each k (Exit j) = pure (Exit j)
evalBlockBuilder :: BlockBuilder -> (Vector QBE.Inst, QBE.Jump)
evalBlockBuilder (Emit is bb) = evalBlockBuilder bb & _1 <>:~ is
evalBlockBuilder (Exit j) = ([],j)
buildBlock :: QBE.Ident QBE.Label -> BlockBuilder -> QBE.Block
buildBlock n bb = QBE.Block n [] (is ^.. each) j
where (is,j) = evalBlockBuilder bb
lowerVal lowerVal
:: forall es. (GenSym :> es, CodeGen :> es) :: forall es. (GenSym :> es)
=> Val => Val
-> (QBE.Val -> Eff es QBE.Block) -> (QBE.Val -> Eff es BlockBuilder)
-> Eff es QBE.Block -> Eff es BlockBuilder
lowerVal (ValInt n) k = k . QBE.ValConst . QBE.CInt . fromIntegral $ n lowerVal (ValInt n) k = k . QBE.ValConst . QBE.CInt . fromIntegral $ n
lowerVal _ k = _ lowerVal _ k = _
lower' lower'
:: forall es. (GenSym :> es, CodeGen :> es) :: forall es. (GenSym :> es)
=> Exp => Exp
-> (QBE.Val -> Eff es QBE.Block) -> (QBE.Val -> Eff es BlockBuilder)
-> Eff es QBE.Block -> Eff es BlockBuilder
lower' (ExpVal v) k = lowerVal v k lower' (ExpVal v) k = lowerVal v k
lower' (ExpLetApply r f xs e) k = lower' (ExpLetApply r f xs e) k =
blah (lowerVal @es <$> (f:|xs)) \(f':xs') -> do blah (lowerVal @es <$> (f:|xs)) \(f':xs') -> do
r <- gensym r <- gensym
emit $ QBE.Call Emit [ QBE.Call
(Just (r, QBE.AbiBaseTy QBE.Long)) (Just (r, QBE.AbiBaseTy QBE.Long))
f' f'
Nothing Nothing
(QBE.Arg (QBE.AbiBaseTy QBE.Long) <$> xs') (QBE.Arg (QBE.AbiBaseTy QBE.Long) <$> xs')
[] []
lower' e k ]
<$> lower' e k
lower' _ k = _ lower' _ k = _
lower e = do -- lower e = do
_ <- runCodeGen (lower' e \r -> _) -- _ <- runCodeGen (lower' e \r -> _)
_ -- _