From ab7cc053a4d58fde841e910f251b8e48b54466ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madeleine=20Sydney=20=C5=9Alaga?= Date: Thu, 30 Apr 2026 22:20:08 -0600 Subject: [PATCH] refactor: records --- src/Language/QBE.hs | 18 ++++++++++++++++-- test/Main.hs | 8 ++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Language/QBE.hs b/src/Language/QBE.hs index 00c137d..13b1c44 100644 --- a/src/Language/QBE.hs +++ b/src/Language/QBE.hs @@ -6,6 +6,7 @@ {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE DuplicateRecordFields, NoFieldSelectors #-} {-| Module : Language.QBE 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 -- | 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) instance Pretty FuncDef where @@ -333,7 +342,12 @@ instance Pretty Val where pretty (ValGlobal ident) = pretty ident -- | 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) instance Pretty Block where diff --git a/test/Main.hs b/test/Main.hs index 090de61..444460d 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -50,12 +50,12 @@ goldenTests = testGroup "golden tests" , t "function" $ FuncDef [Export] (Just $ AbiAggregateTy "t") "f" (Just "env") [Param (AbiBaseTy Word) "a", Param (AbiBaseTy Double) "b"] Variadic $ 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 "jnz" $ Jnz (valInt 0) "target1" "target2" , t "ret" $ Ret $ Just $ ValTemporary "x" , 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 3, Arg (AbiAggregateTy "t1") $ ValTemporary "b"] , t "inst" $ Block "l" [] @@ -112,9 +112,9 @@ helloWorld = Program [] [helloString] [helloMain] Nothing [] NoVariadic $ Block "start" [] - [ Call (Just ("r", AbiBaseTy Word)) (ValGlobal "puts") + [ Call (Just ("r", AbiBaseTy Word)) (ValConst (CGlobal "puts")) Nothing - [Arg (AbiBaseTy Long) $ ValGlobal "str"] + [Arg (AbiBaseTy Long) $ ValConst (CGlobal "str")] [] ] (Ret $ Just $ ValConst $ CInt 0)