Copyright | (c) Francesco Gazzetta 2022 |
---|---|
License | BSD-3-Clause |
Maintainer | Francesco Gazzetta <fgaz@fgaz.me> |
Safe Haskell | None |
Language | Haskell2010 |
This module contains datatypes representing the various structures of the intermediate language of the QBE compiler backend.
All datatypes also have Pretty
instances from
the prettyprinter
library.
You can render QBE IL source files, or any part of them, with something like:
render :: Pretty a => a -> Text render = renderStrict . layoutPretty defaultLayoutOptions . pretty
>>>
render $ Ret $ Just $ ValTemporary "a"
"ret %a">>>
Text.putStrLn $ render $ Program [] [] [FuncDef [] Nothing "main" …
function w $main () { @start ⋮
Synopsis
- type RawIdent = ShortText
- data Sigil
- = AggregateTy
- | Global
- | Temporary
- | Label
- newtype Ident (t :: Sigil) = Ident RawIdent
- data BaseTy
- data ExtTy
- data Const
- data Linkage
- type Alignment = Word64
- type Size = Word64
- type Amount = Word64
- data TypeDef
- data SubTy
- data DataDef = DataDef [Linkage] (Ident 'Global) (Maybe Alignment) [Field]
- data DataItem
- data Field
- data FuncDef = FuncDef [Linkage] (Maybe AbiTy) (Ident 'Global) (Maybe (Ident 'Temporary)) [Param] Variadic (NonEmpty Block)
- data AbiTy
- data Param = Param AbiTy (Ident 'Temporary)
- data Variadic
- prettyVariadic :: Variadic -> Maybe (Doc a)
- data Val
- data Block = Block (Ident 'Label) [Phi] [Inst] Jump
- data Jump
- data Phi = Phi Assignment [PhiArg]
- data PhiArg = PhiArg (Ident 'Label) Val
- data Inst
- = BinaryOp Assignment BinaryOp Val Val
- | Neg Assignment Val
- | Store ExtTy Val Val
- | Load Assignment BaseTy Val
- | LoadW Assignment IntRepr Val
- | LoadH Assignment IntRepr Val
- | LoadB Assignment IntRepr Val
- | Compare Assignment Comparison BaseTy Val Val
- | ExtW Assignment IntRepr Val
- | ExtH Assignment IntRepr Val
- | ExtB Assignment IntRepr Val
- | ExtS (Ident 'Temporary) Val
- | TruncD (Ident 'Temporary) Val
- | StoI Assignment IntRepr Val
- | DtoI Assignment IntRepr Val
- | WtoF Assignment IntRepr Val
- | LtoF Assignment IntRepr Val
- | Cast Assignment Val
- | Copy Assignment Val
- | Call (Maybe (Ident 'Temporary, AbiTy)) Val (Maybe Val) [Arg] [Arg]
- | VaStart (Ident 'Temporary)
- | VaArg Assignment (Ident 'Temporary)
- data Assignment = Assignment (Ident 'Temporary) BaseTy
- pattern (:=) :: Ident 'Temporary -> BaseTy -> Assignment
- data IntRepr
- data BinaryOp
- data Comparison
- data Arg = Arg AbiTy Val
- data Program = Program [TypeDef] [DataDef] [FuncDef]
Identifiers
Sigils are used to differentiate the verious types of Ident
ifier.
newtype Ident (t :: Sigil) Source #
QBE identifiers. The sigil is represented at the type level, so that mixing incompatible identifiers is impossible.
>>>
:set -XOverloadedStrings
>>>
:set -XDataKinds
>>>
:set -XTypeApplications
>>>
pretty $ Jmp $ Ident @'Label "a"
jmp @a>>>
pretty $ Jmp $ Ident @'Global "a"
<interactive>:5:16: error: • Couldn't match type ‘'Global’ with ‘'Label’ Expected: Ident 'Label Actual: Ident 'Global • In the second argument of ‘($)’, namely ‘Ident @'Global "a"’ In the second argument of ‘($)’, namely ‘Jmp $ Ident @'Global "a"’ In the expression: pretty $ Jmp $ Ident @'Global "a"
Instances
Eq (Ident t) Source # | |
Ord (Ident t) Source # | |
Show (Ident t) Source # | |
IsString (Ident t) Source # | |
Defined in Language.QBE fromString :: String -> Ident t # | |
NFData (Ident t) Source # | |
Defined in Language.QBE | |
Hashable (Ident t) Source # | |
Defined in Language.QBE | |
Pretty (Ident 'AggregateTy) Source # | |
Defined in Language.QBE pretty :: Ident 'AggregateTy -> Doc ann # prettyList :: [Ident 'AggregateTy] -> Doc ann # | |
Pretty (Ident 'Global) Source # | |
Pretty (Ident 'Temporary) Source # | |
Pretty (Ident 'Label) Source # | |
Types
Base types
Extended types
Constants
Constant/immediate
CInt Bool Word64 | 64 bit integer. The |
CSingle Float | Single-precision float |
CDouble Double | Double-precision float |
CGlobal (Ident 'Global) | Global symbol |
Linkage
Export | Marks the defined item as visible outside the current file's scope |
Section ShortText (Maybe Text) | Section name, with optional linker flags |
Definitions
Aggregate types
Aggregate type
TypeDef (Ident 'AggregateTy) (Maybe Alignment) [(SubTy, Maybe Amount)] | |
Opaque (Ident 'AggregateTy) Alignment Size |
A type that can be part of an aggregate type
Data
Global object definition
Functions
Function definition. The 'Maybe (Ident 'Temporary)' is the environment
FuncDef [Linkage] (Maybe AbiTy) (Ident 'Global) (Maybe (Ident 'Temporary)) [Param] Variadic (NonEmpty Block) |
Function parameter
Indicates the presence or absence of a variadic marker
prettyVariadic :: Variadic -> Maybe (Doc a) Source #
Variadic
→ Just "..."
NoVariadic
→ Nothing
Control
Value, either an immediate or a global or temporary identifier.
Block of instructions beginning with a label and ending with a jump
Jump instructions
Jmp (Ident 'Label) | Unconditional jump |
Jnz Val (Ident 'Label) (Ident 'Label) | Conditional jump |
Ret (Maybe Val) | Function return |
Instructions
Phi instruction
Instruction
data Assignment Source #
Represents the %x =t
part of an instruction.
Instances
Eq Assignment Source # | |
Defined in Language.QBE (==) :: Assignment -> Assignment -> Bool # (/=) :: Assignment -> Assignment -> Bool # | |
Show Assignment Source # | |
Defined in Language.QBE showsPrec :: Int -> Assignment -> ShowS # show :: Assignment -> String # showList :: [Assignment] -> ShowS # | |
Pretty Assignment Source # | |
Defined in Language.QBE pretty :: Assignment -> Doc ann # prettyList :: [Assignment] -> Doc ann # |
pattern (:=) :: Ident 'Temporary -> BaseTy -> Assignment Source #
Infix synonym of Assignment
Integer representation
Binary arithmetic and bit operations
Add | add |
Sub | sub |
Div IntRepr |
|
Mul | mul |
Rem IntRepr |
|
Or | or |
Xor | xor |
And | and |
Sar | sar |
Shr | shr |
Shl | shl |
data Comparison Source #
Comparison operators.
Where there's a
, Maybe
IntRepr
Nothing
means floating point
(le
, lt
, ge
, gt
), while
means integer
(Just
rsle
, ule
, slt
, ult
...)
Eq | equality |
Ne | inequality |
Le (Maybe IntRepr) | lower or equal |
Lt (Maybe IntRepr) | lower |
Ge (Maybe IntRepr) | greater or equal |
Gt (Maybe IntRepr) | greater Floating point only comparison |
O | ordered (no operand is a NaN) (floating point only) |
Uo | unordered (at least one operand is a NaN) (floating point only) |
Instances
Eq Comparison Source # | |
Defined in Language.QBE (==) :: Comparison -> Comparison -> Bool # (/=) :: Comparison -> Comparison -> Bool # | |
Show Comparison Source # | |
Defined in Language.QBE showsPrec :: Int -> Comparison -> ShowS # show :: Comparison -> String # showList :: [Comparison] -> ShowS # | |
Pretty Comparison Source # | |
Defined in Language.QBE pretty :: Comparison -> Doc ann # prettyList :: [Comparison] -> Doc ann # |
Function argument