Add Program

This commit is contained in:
Francesco Gazzetta
2022-07-03 17:13:00 +02:00
parent f41ddeca97
commit 1405020497
3 changed files with 44 additions and 5 deletions

9
golden/hello_world.qbe Normal file
View File

@@ -0,0 +1,9 @@
data $str =
{b "hello world", b 0}
export
function w $main
()
{@start
%r =w call $puts (l $str)
ret 0}

View File

@@ -85,6 +85,7 @@ instance Pretty ExtTy where
-------------- --------------
data Const data Const
-- MAYBE just use a signed type
= CInt Bool Word64 -- ^ The 'Bool' is whether to negate = CInt Bool Word64 -- ^ The 'Bool' is whether to negate
| CSingle Float | CSingle Float
| CDouble Double | CDouble Double
@@ -122,13 +123,13 @@ type Amount = Word64
-- ** Aggregate types -- ** Aggregate types
--------------------- ---------------------
data Typedef data TypeDef
= Typedef (Ident 'AggregateTy) (Maybe Alignment) [(SubTy, Maybe Amount)] = TypeDef (Ident 'AggregateTy) (Maybe Alignment) [(SubTy, Maybe Amount)]
| Opaque (Ident 'AggregateTy) Alignment Size | Opaque (Ident 'AggregateTy) Alignment Size
deriving (Show, Eq) deriving (Show, Eq)
instance Pretty Typedef where instance Pretty TypeDef where
pretty (Typedef ident alignment def) = pretty (TypeDef ident alignment def) =
"type" <+> pretty ident <+> equals "type" <+> pretty ident <+> equals
<> maybe mempty (\x -> space <> pretty x) alignment <> maybe mempty (\x -> space <> pretty x) alignment
<+> braced (prettyItem <$> def) <+> braced (prettyItem <$> def)
@@ -458,6 +459,19 @@ data Arg = Arg AbiTy Val
instance Pretty Arg where instance Pretty Arg where
pretty (Arg abiTy val) = pretty abiTy <+> pretty val pretty (Arg abiTy val) = pretty abiTy <+> pretty val
-- * Program
------------
data Program = Program [TypeDef] [DataDef] [FuncDef]
deriving (Show, Eq)
instance Pretty Program where
pretty (Program typeDefs dataDefs funcDefs) = vsep $ concat
[ pretty <$> typeDefs
, pretty <$> dataDefs
, pretty <$> funcDefs
]
-- * Utilities -- * Utilities
-------------- --------------

View File

@@ -38,7 +38,7 @@ goldenTests = testGroup "golden tests"
, CGlobal "global" , CGlobal "global"
] ]
, t "linkage" (Export, Section "secName" Nothing, Section "secName" $ Just "flag1 flag2") , t "linkage" (Export, Section "secName" Nothing, Section "secName" $ Just "flag1 flag2")
, t "typedef" $ Typedef "t" (Just 8) , t "typedef" $ TypeDef "t" (Just 8)
[ (SubExtTy HalfWord, Just 16) [ (SubExtTy HalfWord, Just 16)
, (SubAggregateTy "t1", Nothing) , (SubAggregateTy "t1", Nothing)
] ]
@@ -82,6 +82,7 @@ goldenTests = testGroup "golden tests"
, VaArg assignA "va" , VaArg assignA "va"
] ]
(Ret Nothing) (Ret Nothing)
, t "hello_world" helloWorld
] ]
where where
t name value = goldenVsAction t name value = goldenVsAction
@@ -100,3 +101,18 @@ two = valInt 2
assignA :: Assignment assignA :: Assignment
assignA = Assignment "a" Word assignA = Assignment "a" Word
helloWorld :: Program
helloWorld = Program [] [helloString] [helloMain]
where
helloString = DataDef [] "str" Nothing
[ FieldExtTy Byte $ String "hello world" :| []
, FieldExtTy Byte $ Const (CInt False 0) :| []
]
helloMain = FuncDef [Export] (Just $ AbiBaseTy Word) "main"
Nothing [] NoVariadic $
Block "start"
[]
[Call (Just ("r", AbiBaseTy Word)) (ValGlobal "puts") Nothing [Arg (AbiBaseTy Long) $ ValGlobal "str"] []]
(Ret $ Just $ ValConst $ CInt False 0)
:| []