refactor: records
All checks were successful
build / build (push) Successful in 27s

This commit is contained in:
2026-04-30 22:20:08 -06:00
parent e61853e7a6
commit ab7cc053a4
2 changed files with 20 additions and 6 deletions

View File

@@ -6,6 +6,7 @@
{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GADTs #-} {-# LANGUAGE GADTs #-}
{-# LANGUAGE DuplicateRecordFields, NoFieldSelectors #-}
{-| {-|
Module : Language.QBE Module : Language.QBE
Description : Types and Pretty instances for the QBE IL Description : Types and Pretty instances for the QBE IL
@@ -278,7 +279,15 @@ instance Pretty Field where
-- TODO use record syntax on long types like this one -- TODO use record syntax on long types like this one
-- | Function definition. The 'Maybe (Ident \'Temporary)' is the environment -- | Function definition. The 'Maybe (Ident \'Temporary)' is the environment
data FuncDef = FuncDef [Linkage] (Maybe AbiTy) (Ident 'Global) (Maybe (Ident 'Temporary)) [Param] Variadic (NonEmpty Block) data FuncDef = FuncDef
{ linkage :: [Linkage]
, returnType :: Maybe AbiTy
, name :: Ident 'Global
, env :: Maybe (Ident 'Temporary)
, params :: [Param]
, variadic :: Variadic
, code :: NonEmpty Block
}
deriving (Show, Eq, Data) deriving (Show, Eq, Data)
instance Pretty FuncDef where instance Pretty FuncDef where
@@ -333,7 +342,12 @@ instance Pretty Val where
pretty (ValGlobal ident) = pretty ident pretty (ValGlobal ident) = pretty ident
-- | Block of instructions beginning with a label and ending with a jump -- | Block of instructions beginning with a label and ending with a jump
data Block = Block (Ident 'Label) [Phi] [Inst] Jump data Block = Block
{ label :: Ident 'Label
, phis :: [Phi]
, insts :: [Inst]
, jump :: Jump
}
deriving (Show, Eq, Data) deriving (Show, Eq, Data)
instance Pretty Block where instance Pretty Block where

View File

@@ -50,12 +50,12 @@ goldenTests = testGroup "golden tests"
, t "function" $ FuncDef [Export] (Just $ AbiAggregateTy "t") "f" , t "function" $ FuncDef [Export] (Just $ AbiAggregateTy "t") "f"
(Just "env") [Param (AbiBaseTy Word) "a", Param (AbiBaseTy Double) "b"] Variadic $ (Just "env") [Param (AbiBaseTy Word) "a", Param (AbiBaseTy Double) "b"] Variadic $
Block "l" [] [] (Ret Nothing) :| [] Block "l" [] [] (Ret Nothing) :| []
, t "val" [valInt 0, ValTemporary "temporary", ValGlobal "global"] , t "val" [valInt 0, ValTemporary "temporary", ValConst (CGlobal "global")]
, t "jmp" $ Jmp "target" , t "jmp" $ Jmp "target"
, t "jnz" $ Jnz (valInt 0) "target1" "target2" , t "jnz" $ Jnz (valInt 0) "target1" "target2"
, t "ret" $ Ret $ Just $ ValTemporary "x" , t "ret" $ Ret $ Just $ ValTemporary "x"
, t "phi" $ Phi (Assignment "a" Word) [PhiArg "b" $ valInt 1, PhiArg "c" $ valInt 2] , t "phi" $ Phi (Assignment "a" Word) [PhiArg "b" $ valInt 1, PhiArg "c" $ valInt 2]
, t "call" $ Call (Just ("r", AbiBaseTy Word)) (ValGlobal "f") (Just $ valInt 1) , t "call" $ Call (Just ("r", AbiBaseTy Word)) (ValConst (CGlobal "f")) (Just $ valInt 1)
[Arg (AbiBaseTy Word) $ valInt 2, Arg (AbiAggregateTy "t") $ ValTemporary "a"] [Arg (AbiBaseTy Word) $ valInt 2, Arg (AbiAggregateTy "t") $ ValTemporary "a"]
[Arg (AbiBaseTy Word) $ valInt 3, Arg (AbiAggregateTy "t1") $ ValTemporary "b"] [Arg (AbiBaseTy Word) $ valInt 3, Arg (AbiAggregateTy "t1") $ ValTemporary "b"]
, t "inst" $ Block "l" [] , t "inst" $ Block "l" []
@@ -112,9 +112,9 @@ helloWorld = Program [] [helloString] [helloMain]
Nothing [] NoVariadic $ Nothing [] NoVariadic $
Block "start" Block "start"
[] []
[ Call (Just ("r", AbiBaseTy Word)) (ValGlobal "puts") [ Call (Just ("r", AbiBaseTy Word)) (ValConst (CGlobal "puts"))
Nothing Nothing
[Arg (AbiBaseTy Long) $ ValGlobal "str"] [Arg (AbiBaseTy Long) $ ValConst (CGlobal "str")]
[] []
] ]
(Ret $ Just $ ValConst $ CInt 0) (Ret $ Just $ ValConst $ CInt 0)