qbe-1.1.0.0: Types and prettyprinter for the IL of the QBE compiler backend
Copyright(c) Francesco Gazzetta 2022
LicenseBSD-3-Clause
MaintainerFrancesco Gazzetta <fgaz@fgaz.me>
Safe HaskellNone
LanguageHaskell2010

Language.QBE

Description

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

Identifiers

type RawIdent = ShortText Source #

A raw identifier string, with no sigil information attached

data Sigil Source #

Sigils are used to differentiate the verious types of Identifier.

Instances

Instances details
Eq Sigil Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Sigil -> Sigil -> Bool #

(/=) :: Sigil -> Sigil -> Bool #

Show Sigil Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Sigil -> ShowS #

show :: Sigil -> String #

showList :: [Sigil] -> ShowS #

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"

Constructors

Ident RawIdent 

Instances

Instances details
Eq (Ident t) Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Ident t -> Ident t -> Bool #

(/=) :: Ident t -> Ident t -> Bool #

Ord (Ident t) Source # 
Instance details

Defined in Language.QBE

Methods

compare :: Ident t -> Ident t -> Ordering #

(<) :: Ident t -> Ident t -> Bool #

(<=) :: Ident t -> Ident t -> Bool #

(>) :: Ident t -> Ident t -> Bool #

(>=) :: Ident t -> Ident t -> Bool #

max :: Ident t -> Ident t -> Ident t #

min :: Ident t -> Ident t -> Ident t #

Show (Ident t) Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Ident t -> ShowS #

show :: Ident t -> String #

showList :: [Ident t] -> ShowS #

IsString (Ident t) Source # 
Instance details

Defined in Language.QBE

Methods

fromString :: String -> Ident t #

NFData (Ident t) Source # 
Instance details

Defined in Language.QBE

Methods

rnf :: Ident t -> () #

Hashable (Ident t) Source # 
Instance details

Defined in Language.QBE

Methods

hashWithSalt :: Int -> Ident t -> Int #

hash :: Ident t -> Int #

Pretty (Ident 'AggregateTy) Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Ident 'AggregateTy -> Doc ann #

prettyList :: [Ident 'AggregateTy] -> Doc ann #

Pretty (Ident 'Global) Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Ident 'Global -> Doc ann #

prettyList :: [Ident 'Global] -> Doc ann #

Pretty (Ident 'Temporary) Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Ident 'Temporary -> Doc ann #

prettyList :: [Ident 'Temporary] -> Doc ann #

Pretty (Ident 'Label) Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Ident 'Label -> Doc ann #

prettyList :: [Ident 'Label] -> Doc ann #

Types

data BaseTy Source #

Base types

Constructors

Word
w
Long
l
Single
s
Double
d

Instances

Instances details
Eq BaseTy Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: BaseTy -> BaseTy -> Bool #

(/=) :: BaseTy -> BaseTy -> Bool #

Show BaseTy Source # 
Instance details

Defined in Language.QBE

Pretty BaseTy Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: BaseTy -> Doc ann #

prettyList :: [BaseTy] -> Doc ann #

data ExtTy Source #

Extended types

Constructors

BaseTy BaseTy 
Byte
b
HalfWord
h

Instances

Instances details
Eq ExtTy Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: ExtTy -> ExtTy -> Bool #

(/=) :: ExtTy -> ExtTy -> Bool #

Show ExtTy Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> ExtTy -> ShowS #

show :: ExtTy -> String #

showList :: [ExtTy] -> ShowS #

Pretty ExtTy Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: ExtTy -> Doc ann #

prettyList :: [ExtTy] -> Doc ann #

Constants

data Const Source #

Constant/immediate

Constructors

CInt Bool Word64

64 bit integer. The Bool is whether to negate.

CSingle Float

Single-precision float

CDouble Double

Double-precision float

CGlobal (Ident 'Global)

Global symbol

Instances

Instances details
Eq Const Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Const -> Const -> Bool #

(/=) :: Const -> Const -> Bool #

Show Const Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Const -> ShowS #

show :: Const -> String #

showList :: [Const] -> ShowS #

Pretty Const Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Const -> Doc ann #

prettyList :: [Const] -> Doc ann #

Linkage

data Linkage Source #

Constructors

Export

Marks the defined item as visible outside the current file's scope

Section ShortText (Maybe Text)

Section name, with optional linker flags

Instances

Instances details
Eq Linkage Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Linkage -> Linkage -> Bool #

(/=) :: Linkage -> Linkage -> Bool #

Show Linkage Source # 
Instance details

Defined in Language.QBE

Pretty Linkage Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Linkage -> Doc ann #

prettyList :: [Linkage] -> Doc ann #

Definitions

Aggregate types

data TypeDef Source #

Aggregate type

Instances

Instances details
Eq TypeDef Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: TypeDef -> TypeDef -> Bool #

(/=) :: TypeDef -> TypeDef -> Bool #

Show TypeDef Source # 
Instance details

Defined in Language.QBE

Pretty TypeDef Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: TypeDef -> Doc ann #

prettyList :: [TypeDef] -> Doc ann #

data SubTy Source #

A type that can be part of an aggregate type

Instances

Instances details
Eq SubTy Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: SubTy -> SubTy -> Bool #

(/=) :: SubTy -> SubTy -> Bool #

Show SubTy Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> SubTy -> ShowS #

show :: SubTy -> String #

showList :: [SubTy] -> ShowS #

Pretty SubTy Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: SubTy -> Doc ann #

prettyList :: [SubTy] -> Doc ann #

Data

data DataDef Source #

Global object definition

Instances

Instances details
Eq DataDef Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: DataDef -> DataDef -> Bool #

(/=) :: DataDef -> DataDef -> Bool #

Show DataDef Source # 
Instance details

Defined in Language.QBE

Pretty DataDef Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: DataDef -> Doc ann #

prettyList :: [DataDef] -> Doc ann #

data DataItem Source #

Instances

Instances details
Eq DataItem Source # 
Instance details

Defined in Language.QBE

Show DataItem Source # 
Instance details

Defined in Language.QBE

Pretty DataItem Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: DataItem -> Doc ann #

prettyList :: [DataItem] -> Doc ann #

data Field Source #

Instances

Instances details
Eq Field Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Field -> Field -> Bool #

(/=) :: Field -> Field -> Bool #

Show Field Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Field -> ShowS #

show :: Field -> String #

showList :: [Field] -> ShowS #

Pretty Field Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Field -> Doc ann #

prettyList :: [Field] -> Doc ann #

Functions

data FuncDef Source #

Function definition. The 'Maybe (Ident 'Temporary)' is the environment

Instances

Instances details
Eq FuncDef Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: FuncDef -> FuncDef -> Bool #

(/=) :: FuncDef -> FuncDef -> Bool #

Show FuncDef Source # 
Instance details

Defined in Language.QBE

Pretty FuncDef Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: FuncDef -> Doc ann #

prettyList :: [FuncDef] -> Doc ann #

data AbiTy Source #

Instances

Instances details
Eq AbiTy Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: AbiTy -> AbiTy -> Bool #

(/=) :: AbiTy -> AbiTy -> Bool #

Show AbiTy Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> AbiTy -> ShowS #

show :: AbiTy -> String #

showList :: [AbiTy] -> ShowS #

Pretty AbiTy Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: AbiTy -> Doc ann #

prettyList :: [AbiTy] -> Doc ann #

data Param Source #

Function parameter

Constructors

Param AbiTy (Ident 'Temporary) 

Instances

Instances details
Eq Param Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Param -> Param -> Bool #

(/=) :: Param -> Param -> Bool #

Show Param Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Param -> ShowS #

show :: Param -> String #

showList :: [Param] -> ShowS #

Pretty Param Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Param -> Doc ann #

prettyList :: [Param] -> Doc ann #

data Variadic Source #

Indicates the presence or absence of a variadic marker

Constructors

Variadic 
NoVariadic 

Instances

Instances details
Eq Variadic Source # 
Instance details

Defined in Language.QBE

Show Variadic Source # 
Instance details

Defined in Language.QBE

prettyVariadic :: Variadic -> Maybe (Doc a) Source #

VariadicJust "..." NoVariadicNothing

Control

data Val Source #

Value, either an immediate or a global or temporary identifier.

Instances

Instances details
Eq Val Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Val -> Val -> Bool #

(/=) :: Val -> Val -> Bool #

Show Val Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Val -> ShowS #

show :: Val -> String #

showList :: [Val] -> ShowS #

Pretty Val Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Val -> Doc ann #

prettyList :: [Val] -> Doc ann #

data Block Source #

Block of instructions beginning with a label and ending with a jump

Constructors

Block (Ident 'Label) [Phi] [Inst] Jump 

Instances

Instances details
Eq Block Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Block -> Block -> Bool #

(/=) :: Block -> Block -> Bool #

Show Block Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Block -> ShowS #

show :: Block -> String #

showList :: [Block] -> ShowS #

Pretty Block Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Block -> Doc ann #

prettyList :: [Block] -> Doc ann #

data Jump Source #

Jump instructions

Constructors

Jmp (Ident 'Label)

Unconditional jump

Jnz Val (Ident 'Label) (Ident 'Label)

Conditional jump

Ret (Maybe Val)

Function return

Instances

Instances details
Eq Jump Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Jump -> Jump -> Bool #

(/=) :: Jump -> Jump -> Bool #

Show Jump Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Jump -> ShowS #

show :: Jump -> String #

showList :: [Jump] -> ShowS #

Pretty Jump Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Jump -> Doc ann #

prettyList :: [Jump] -> Doc ann #

Instructions

data Phi Source #

Phi instruction

Constructors

Phi Assignment [PhiArg] 

Instances

Instances details
Eq Phi Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Phi -> Phi -> Bool #

(/=) :: Phi -> Phi -> Bool #

Show Phi Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Phi -> ShowS #

show :: Phi -> String #

showList :: [Phi] -> ShowS #

Pretty Phi Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Phi -> Doc ann #

prettyList :: [Phi] -> Doc ann #

data PhiArg Source #

Phi instruction argument, associating a Val to a Label

Constructors

PhiArg (Ident 'Label) Val 

Instances

Instances details
Eq PhiArg Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: PhiArg -> PhiArg -> Bool #

(/=) :: PhiArg -> PhiArg -> Bool #

Show PhiArg Source # 
Instance details

Defined in Language.QBE

Pretty PhiArg Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: PhiArg -> Doc ann #

prettyList :: [PhiArg] -> Doc ann #

data Inst Source #

Instruction

Constructors

BinaryOp Assignment BinaryOp Val Val

Binary arithmetic and bit operations

Neg Assignment Val

neg Memory

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

extsw/extuw

ExtH Assignment IntRepr Val

extsh/extuh

ExtB Assignment IntRepr Val

extsb/extub

ExtS (Ident 'Temporary) Val

exts. There is only one possible instruction type, so there's only an Ident instead of a full Assignment

TruncD (Ident 'Temporary) Val

truncd. There is only one possible instruction type, so there's only an Ident instead of a full Assignment

StoI Assignment IntRepr Val

stosi/stoui

DtoI Assignment IntRepr Val

dtosi/dtoui

WtoF Assignment IntRepr Val

swtof/uwtof

LtoF Assignment IntRepr Val

sltof/ultof

Cast Assignment Val 
Copy Assignment Val 
Call (Maybe (Ident 'Temporary, AbiTy)) Val (Maybe Val) [Arg] [Arg]

the fields are: assignment, function name, environment, arguments, variadic arguments

VaStart (Ident 'Temporary) 
VaArg Assignment (Ident 'Temporary) 

Instances

Instances details
Eq Inst Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Inst -> Inst -> Bool #

(/=) :: Inst -> Inst -> Bool #

Show Inst Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Inst -> ShowS #

show :: Inst -> String #

showList :: [Inst] -> ShowS #

Pretty Inst Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Inst -> Doc ann #

prettyList :: [Inst] -> Doc ann #

data Assignment Source #

Represents the %x =t part of an instruction.

Constructors

Assignment (Ident 'Temporary) BaseTy 

Instances

Instances details
Eq Assignment Source # 
Instance details

Defined in Language.QBE

Show Assignment Source # 
Instance details

Defined in Language.QBE

Pretty Assignment Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Assignment -> Doc ann #

prettyList :: [Assignment] -> Doc ann #

pattern (:=) :: Ident 'Temporary -> BaseTy -> Assignment Source #

Infix synonym of Assignment

data IntRepr Source #

Integer representation

Constructors

Signed 
Unsigned 

Instances

Instances details
Eq IntRepr Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: IntRepr -> IntRepr -> Bool #

(/=) :: IntRepr -> IntRepr -> Bool #

Show IntRepr Source # 
Instance details

Defined in Language.QBE

Pretty IntRepr Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: IntRepr -> Doc ann #

prettyList :: [IntRepr] -> Doc ann #

data BinaryOp Source #

Binary arithmetic and bit operations

Constructors

Add
add
Sub
sub
Div IntRepr

div/udiv. Div Signed gets translated to div, so it will work also on floats

Mul
mul
Rem IntRepr

rem/urem

Or
or
Xor
xor
And
and
Sar
sar
Shr
shr
Shl
shl

Instances

Instances details
Eq BinaryOp Source # 
Instance details

Defined in Language.QBE

Show BinaryOp Source # 
Instance details

Defined in Language.QBE

Pretty BinaryOp Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: BinaryOp -> Doc ann #

prettyList :: [BinaryOp] -> Doc ann #

data Comparison Source #

Comparison operators. Where there's a Maybe IntRepr, Nothing means floating point (le, lt, ge, gt), while Just r means integer (sle, ule, slt, ult...)

Constructors

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

Instances details
Eq Comparison Source # 
Instance details

Defined in Language.QBE

Show Comparison Source # 
Instance details

Defined in Language.QBE

Pretty Comparison Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Comparison -> Doc ann #

prettyList :: [Comparison] -> Doc ann #

data Arg Source #

Function argument

Constructors

Arg AbiTy Val 

Instances

Instances details
Eq Arg Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Arg -> Arg -> Bool #

(/=) :: Arg -> Arg -> Bool #

Show Arg Source # 
Instance details

Defined in Language.QBE

Methods

showsPrec :: Int -> Arg -> ShowS #

show :: Arg -> String #

showList :: [Arg] -> ShowS #

Pretty Arg Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Arg -> Doc ann #

prettyList :: [Arg] -> Doc ann #

Program

data Program Source #

Datatypre representing a QBE IL source file

Constructors

Program [TypeDef] [DataDef] [FuncDef] 

Instances

Instances details
Eq Program Source # 
Instance details

Defined in Language.QBE

Methods

(==) :: Program -> Program -> Bool #

(/=) :: Program -> Program -> Bool #

Show Program Source # 
Instance details

Defined in Language.QBE

Pretty Program Source # 
Instance details

Defined in Language.QBE

Methods

pretty :: Program -> Doc ann #

prettyList :: [Program] -> Doc ann #