1% SPDX-FileCopyrightText: 2015-2024 Quentin Carbonneaux <quentin@c9x.me>2% SPDX-FileCopyrightText: 2025-2026 Sören Tempel <soeren+git@soeren-tempel.net>3%4% SPDX-License-Identifier: MIT AND GPL-3.0-only56\documentclass{article}7%include polycode.fmt89%subst blankline = "\\[5mm]"1011% See https://github.com/kosmikus/lhs2tex/issues/5812%format <$> = "\mathbin{\langle\$\rangle}"13%format <&> = "\mathbin{\langle\&\rangle}"14%format <|> = "\mathbin{\langle\:\vline\:\rangle}"15%format <?> = "\mathbin{\langle?\rangle}"16%format <*> = "\mathbin{\langle*\rangle}"17%format <* = "\mathbin{\langle*}"18%format *> = "\mathbin{*\rangle}"1920\long\def\ignore#1{}2122\usepackage{hyperref}23\hypersetup{24 colorlinks = true,25}2627\begin{document}2829\title{QBE Intermediate Language\vspace{-2em}}30\date{}31\maketitle32\frenchspacing3334\ignore{35\begin{code}36module Language.QBE.Parser37 ( skipInitComments,38 dataDef,39 typeDef,40 funcDef,41 fileDef42 )43where4445import Control.Monad (foldM)46import Data.Char (chr)47import Data.Word (Word64)48import Data.Functor ((<&>))49import Data.List (singleton)50import Data.Map (Map)51import Data.Map qualified as Map52import qualified Language.QBE.Types as Q53import Language.QBE.Util (bind, decNumber, octNumber, float)54import Text.ParserCombinators.Parsec55 ( Parser,56 alphaNum,57 anyChar,58 between,59 char,60 choice,61 letter,62 many,63 many1,64 manyTill,65 newline,66 noneOf,67 oneOf,68 optional,69 optionMaybe,70 sepBy,71 sepBy1,72 skipMany,73 skipMany1,74 string,75 try,76 (<?>),77 (<|>),78 )79\end{code}80}8182This an executable description of the83\href{https://c9x.me/compile/doc/il-v1.2.html}{QBE intermediate language},84specified through \href{https://hackage.haskell.org/package/parsec}{Parsec}85parser combinators and generated from a literate Haskell file. The description86is derived from the original QBE IL documentation, licensed under MIT.87Presently, this implementation targets version 1.2 of the QBE intermediate88language and aims to be equivalent with the original specification.8990\section{Basic Concepts}9192The intermediate language (IL) is a higher-level language than the93machine's assembly language. It smoothes most of the94irregularities of the underlying hardware and allows an infinite number95of temporaries to be used. This higher abstraction level lets frontend96programmers focus on language design issues.9798\subsection{Input Files}99100The intermediate language is provided to QBE as text. Usually, one file101is generated per each compilation unit from the frontend input language.102An IL file is a sequence of \nameref{sec:definitions} for103data, functions, and types. Once processed by QBE, the resulting file104can be assembled and linked using a standard toolchain (e.g., GNU105binutils).106107\begin{code}108comment :: Parser ()109comment = skipMany blankNL >> comment' >> skipMany blankNL110 where111 comment' = char '#' >> manyTill anyChar newline112\end{code}113114\ignore{115\begin{code}116skipNoCode :: Parser () -> Parser ()117skipNoCode blankP = try (skipMany1 comment <?> "comments") <|> blankP118\end{code}119}120121Here is a complete "Hello World" IL file which defines a function that122prints to the screen. Since the string is not a first class object (only123the pointer is) it is defined outside the function\textquotesingle s124body. Comments start with a \# character and finish with the end of the125line.126127\begin{verbatim}128data $str = { b "hello world", b 0 }129130export function w $main() {131@start132 # Call the puts function with $str as argument.133 %r =w call $puts(l $str)134 ret 0135}136\end{verbatim}137138If you have read the LLVM language reference, you might recognize the139example above. In comparison, QBE makes a much lighter use of types and140the syntax is terser.141142\subsection{Parser Combinators}143144\ignore{145\begin{code}146bracesNL :: Parser a -> Parser a147bracesNL = between (wsNL $ char '{') (wsNL $ char '}')148149quoted :: Parser a -> Parser a150quoted = let q = char '"' in between q q151152sepByTrail1 :: Parser a -> Parser sep -> Parser [a]153sepByTrail1 p sep = do154 x <- p155 xs <- many (try $ sep >> p)156 _ <- optional sep157 return (x:xs)158159sepByTrail :: Parser a -> Parser sep -> Parser [a]160sepByTrail p sep = sepByTrail1 p sep <|> return []161162parenLst :: Parser a -> Parser [a]163parenLst p = between (ws $ char '(') (char ')') inner164 where165 inner = sepBy (ws p) (ws $ char ',')166167unaryInstr :: (Q.Value -> Q.Instr) -> String -> Parser Q.Instr168unaryInstr conc keyword = do169 _ <- ws (string keyword)170 conc <$> ws val171172binaryInstr :: (Q.Value -> Q.Value -> Q.Instr) -> String -> Parser Q.Instr173binaryInstr conc keyword = do174 _ <- ws (string keyword)175 vfst <- ws val <* ws (char ',')176 conc vfst <$> ws val177178-- Can only appear in data and type definitions and hence allows newlines.179alignAny :: Parser Word64180alignAny = (ws1 (string "align")) >> wsNL decNumber181182-- Returns true if it is signed.183signageChar :: Parser Bool184signageChar = (char 's' <|> char 'u') <&> (== 's')185\end{code}186}187188The original QBE specification defines the syntax using a BNF grammar. In189contrast, this document defines it using Parsec parser combinators. As such,190this specification is less formal but more accurate as the parsing code is191actually executable. Consequently, this specification also captures constructs192omitted in the original specification (e.g., \nameref{sec:identifiers}, or193\nameref{sec:strlit}). Nonetheless, the formal language recognized by these194combinators aims to be equivalent to the one of the BNF grammar.195196\subsection{Identifiers}197\label{sec:identifiers}198199% Ident is not documented in the original QBE specification.200% See https://c9x.me/git/qbe.git/tree/parse.c?h=v1.2#n304201202\begin{code}203ident :: Parser String204ident = do205 start <- letter <|> oneOf "._"206 rest <- many (alphaNum <|> oneOf "$._")207 return $ start : rest208\end{code}209210Identifiers for data, types, and functions can start with any ASCII letter or211the special characters \texttt{.} and \texttt{\_}. This initial character can212be followed by a sequence of zero or more alphanumeric characters and the213special characters \texttt{\$}, \texttt{.}, and \texttt{\_}.214215\subsection{Sigils}216217\begin{code}218userDef :: Parser Q.UserIdent219userDef = Q.UserIdent <$> (char ':' >> ident)220221global :: Parser Q.GlobalIdent222global = Q.GlobalIdent <$> (char '$' >> ident)223224local :: Parser Q.LocalIdent225local = Q.LocalIdent <$> (char '%' >> ident)226227label :: Parser Q.BlockIdent228label = Q.BlockIdent <$> (char '@' >> ident)229\end{code}230231The intermediate language makes heavy use of sigils, all user-defined232names are prefixed with a sigil. This is to avoid keyword conflicts, and233also to quickly spot the scope and nature of identifiers.234235\begin{itemize}236 \item \texttt{:} is for user-defined \nameref{sec:aggregate-types}237 \item \texttt{\$} is for globals (represented by a pointer)238 \item \texttt{\%} is for function-scope temporaries239 \item \texttt{@@} is for block labels240\end{itemize}241242\subsection{Spacing}243244\begin{code}245blank :: Parser Char246blank = oneOf "\t " <?> "blank"247248blankNL :: Parser Char249blankNL = oneOf "\n\t " <?> "blank or newline"250\end{code}251252Individual tokens in IL files must be separated by one or more spacing253characters. Both spaces and tabs are recognized as spacing characters.254In data and type definitions, newlines may also be used as spaces to255prevent overly long lines. When exactly one of two consecutive tokens is256a symbol (for example \texttt{,} or \texttt{=} or \texttt{\{}), spacing may be omitted.257258\ignore{259\begin{code}260ws :: Parser a -> Parser a261ws p = p <* skipMany blank262263ws1 :: Parser a -> Parser a264ws1 p = p <* skipMany1 blank265266wsNL :: Parser a -> Parser a267wsNL p = p <* skipNoCode (skipMany blankNL)268269wsNL1 :: Parser a -> Parser a270wsNL1 p = p <* skipNoCode (skipMany1 blankNL)271272-- Only intended to be used to skip comments at the start of a file.273skipInitComments :: Parser ()274skipInitComments = skipNoCode (skipMany blankNL)275\end{code}276}277278\subsection{String Literals}279\label{sec:strlit}280281% The string literal is not documented in the original QBE specification.282% See https://c9x.me/git/qbe.git/tree/parse.c?h=v1.2#n287283284\begin{code}285strLit :: Parser String286strLit = concat <$> quoted (many strChr)287 where288 strChr :: Parser [Char]289 strChr = (singleton <$> noneOf "\"\\") <|> escSeq290291 -- TODO: not documnted in the QBE BNF.292 octEsc :: Parser Char293 octEsc = do294 n <- octNumber295 pure $ chr (fromIntegral n)296297 escSeq :: Parser [Char]298 escSeq = try $ do299 esc <- char '\\'300 (singleton <$> octEsc) <|> (anyChar <&> (\c -> [esc, c]))301\end{code}302303Strings are enclosed by double quotes and are, for example, used to specify a304section name as part of the \nameref{sec:linkage} information. Within a string,305a double quote can be escaped using a \texttt{\textbackslash} character. All306escape sequences, including double quote escaping, are passed through as-is to307the generated assembly file.308309\section{Types}310311\subsection{Simple Types}312313The IL makes minimal use of types. By design, the types used are314restricted to what is necessary for unambiguous compilation to machine315code and C interfacing. Unlike LLVM, QBE is not using types as a means316to safety; they are only here for semantic purposes.317318\begin{code}319baseType :: Parser Q.BaseType320baseType = choice321 [ bind "w" Q.Word322 , bind "l" Q.Long323 , bind "s" Q.Single324 , bind "d" Q.Double ]325\end{code}326327The four base types are \texttt{w} (word), \texttt{l} (long), \texttt{s} (single), and \texttt{d}328(double), they stand respectively for 32-bit and 64-bit integers, and32932-bit and 64-bit floating-point numbers. There are no pointer types330available; pointers are typed by an integer type sufficiently wide to331represent all memory addresses (e.g., \texttt{l} on 64-bit architectures).332Temporaries in the IL can only have a base type.333334\begin{code}335extType :: Parser Q.ExtType336extType = (Q.Base <$> baseType)337 <|> bind "b" Q.Byte338 <|> bind "h" Q.HalfWord339\end{code}340341Extended types contain base types plus \texttt{b} (byte) and \texttt{h} (half word),342respectively for 8-bit and 16-bit integers. They are used in \nameref{sec:aggregate-types}343and \nameref{sec:data} definitions.344345For C interfacing, the IL also provides user-defined aggregate types as346well as signed and unsigned variants of the sub-word extended types.347Read more about these types in the \nameref{sec:aggregate-types}348and \nameref{sec:functions} sections.349350\subsection{Subtyping}351\label{sec:subtyping}352353The IL has a minimal subtyping feature, for integer types only. Any354value of type \texttt{l} can be used in a \texttt{w} context. In that case, only the35532 least significant bits of the word value are used.356357Make note that it is the opposite of the usual subtyping on integers (in358C, we can safely use an \texttt{int} where a \texttt{long} is expected). A long value359cannot be used in word context. The rationale is that a word can be360signed or unsigned, so extending it to a long could be done in two ways,361either by zero-extension, or by sign-extension.362363\subsection{Constants and Vals}364\label{sec:constants-and-vals}365366\begin{code}367dynConst :: Parser Q.DynConst368dynConst =369 (Q.Const <$> constant)370 <|> (Q.Thread <$> (key "thread" >> global))371 <|> (Q.Common <$> (key "common" >> global))372 <|> (Q.Extern <$> try (key "extern" >> global))373 <|> (Q.ExternThread <$> (key "extern" >> key "thread" >> global))374 <?> "dynconst"375 where376 key s = ws1 $ string s377\end{code}378379Constants come in two kinds: compile-time constants and dynamic380constants. Dynamic constants include compile-time constants and other381symbol variants that are only known at program-load time or execution382time. Consequently, dynamic constants can only occur in function bodies.383384When the \texttt{extern} keyword prefixes a symbol name, the symbol is385accessed indirectly through a table edited by the dynamic linker (e.g.,386GOT/PLT). This enables PIE/PIC code generation. When \texttt{extern} is387combined with \texttt{thread}, the symbol is accessed using the388initial-exec TLS model, suitable for thread-local variables defined in389shared objects available at startup time (i.e., not loaded through390dlopen).391392The representation of integers is two's complement.393Floating-point numbers are represented using the single-precision and394double-precision formats of the IEEE 754 standard.395396\begin{code}397constant :: Parser Q.Const398constant =399 (Q.Number <$> decNumber)400 <|> (Q.SFP <$> sfp)401 <|> (Q.DFP <$> dfp)402 <|> (Q.Global <$> global)403 <?> "const"404 where405 sfp = string "s_" >> float406 dfp = string "d_" >> float407\end{code}408409Constants specify a sequence of bits and are untyped. They are always410parsed as 64-bit blobs. Depending on the context surrounding a constant,411only some of its bits are used. For example, in the program below, the412two variables defined have the same value since the first operand of the413subtraction is a word (32-bit) context.414415\begin{verbatim}416%x =w sub -1, 0 %y =w sub 4294967295, 0417\end{verbatim}418419Because specifying floating-point constants by their bits makes the code420less readable, syntactic sugar is provided to express them. Standard421scientific notation is prefixed with \texttt{s\_} and \texttt{d\_} for single and422double precision numbers respectively. Once again, the following example423defines twice the same double-precision constant.424425\begin{verbatim}426%x =d add d_0, d_-1427%y =d add d_0, -4616189618054758400428\end{verbatim}429430Global symbols can also be used directly as constants; they will be431resolved and turned into actual numeric constants by the linker.432433When the \texttt{thread} keyword prefixes a symbol name, the434symbol\textquotesingle s numeric value is resolved at runtime in the435thread-local storage.436437\begin{code}438val :: Parser Q.Value439val =440 (Q.VConst <$> dynConst)441 <|> (Q.VLocal <$> local)442 <?> "val"443\end{code}444445Vals are used as arguments in regular, phi, and jump instructions within446function definitions. They are either constants or function-scope447temporaries.448449\subsection{Linkage}450\label{sec:linkage}451452\begin{code}453linkage :: Parser Q.Linkage454linkage =455 wsNL (bind "export" Q.LExport)456 <|> wsNL (bind "thread" Q.LThread)457 <|> do458 _ <- ws1 $ string "section"459 (try secWithFlags) <|> sec460 where461 sec :: Parser Q.Linkage462 sec = wsNL strLit <&> (`Q.LSection` Nothing)463464 secWithFlags :: Parser Q.Linkage465 secWithFlags = do466 n <- ws1 strLit467 wsNL strLit <&> Q.LSection n . Just468\end{code}469470Function and data definitions (see below) can specify linkage471information to be passed to the assembler and eventually to the linker.472473The \texttt{export} linkage flag marks the defined item as visible outside the474current file\textquotesingle s scope. If absent, the symbol can only be475referred to locally. Functions compiled by QBE and called from C need to476be exported.477478The \texttt{thread} linkage flag can only qualify data definitions. It mandates479that the object defined is stored in thread-local storage. Each time a480runtime thread starts, the supporting platform runtime is in charge of481making a new copy of the object for the fresh thread. Objects in482thread-local storage must be accessed using the \texttt{thread \$IDENT} syntax,483as specified in the \nameref{sec:constants-and-vals} section.484485A \texttt{section} flag can be specified to tell the linker to put the defined486item in a certain section. The use of the section flag is platform487dependent and we refer the user to the documentation of their assembler488and linker for relevant information.489490\begin{verbatim}491section ".init_array" data $.init.f = { l $f }492\end{verbatim}493494The section flag can be used to add function pointers to a global495initialization list, as depicted above. Note that some platforms provide496a BSS section that can be used to minimize the footprint of uniformly497zeroed data. When this section is available, QBE will automatically make498use of it and no section flag is required.499500The section and export linkage flags should each appear at most once in501a definition. If multiple occurrences are present, QBE is free to use502any.503504\subsection{Definitions}505\label{sec:definitions}506507Definitions are the essential components of an IL file. They can define508three types of objects: aggregate types, data, and functions. Aggregate509types are never exported and do not compile to any code. Data and510function definitions have file scope and are mutually recursive (even511across IL files). Their visibility can be controlled using linkage512flags.513514\subsubsection{Aggregate Types}515\label{sec:aggregate-types}516517\begin{code}518typeDef :: Parser Q.TypeDef519typeDef = do520 _ <- wsNL1 (string "type")521 i <- wsNL1 userDef522 _ <- wsNL1 (char '=')523 a <- optionMaybe alignAny524 bracesNL (opaqueType <|> unionType <|> regularType) <&> Q.TypeDef i a525\end{code}526527Aggregate type definitions start with the \texttt{type} keyword. They have file528scope, but types must be defined before being referenced. The inner529structure of a type is expressed by a comma-separated list of fields.530531\begin{code}532subType :: Parser Q.SubType533subType =534 (Q.SExtType <$> extType)535 <|> (Q.SUserDef <$> userDef)536537field :: Parser Q.Field538field = do539 -- TODO: newline is required if there is a number argument540 f <- wsNL subType541 s <- ws $ optionMaybe decNumber542 pure (f, s)543544fields :: Bool -> Parser [Q.Field]545fields allowEmpty =546 (if allowEmpty then sepByTrail else sepByTrail1) field (wsNL $ char ',')547\end{code}548549A field consists of a subtype, either an extended type or a user-defined type,550and an optional number expressing the value of this field. In case many items551of the same type are sequenced (like in a C array), the shorter array syntax552can be used.553554\begin{code}555regularType :: Parser Q.AggType556regularType = Q.ARegular <$> fields True557\end{code}558559Three different kinds of aggregate types are presentl ysupported: regular560types, union types and opaque types. The fields of regular types will be561packed. By default, the alignment of an aggregate type is the maximum alignment562of its members. The alignment can be explicitly specified by the programmer.563564\begin{code}565unionType :: Parser Q.AggType566unionType = Q.AUnion <$> many1 (wsNL unionType')567 where568 unionType' :: Parser [Q.Field]569 unionType' = bracesNL $ fields False570\end{code}571572Union types allow the same chunk of memory to be used with different layouts. They are defined by enclosing multiple regular aggregate type bodies in a pair of curly braces. Size and alignment of union types are set to the maximum size and alignment of each variation or, in the case of alignment, can be explicitly specified.573574\begin{code}575opaqueType :: Parser Q.AggType576opaqueType = Q.AOpaque <$> wsNL decNumber577\end{code}578579Opaque types are used when the inner structure of an aggregate cannot be specified; the alignment for opaque types is mandatory. They are defined simply by enclosing their size between curly braces.580581\subsubsection{Data}582\label{sec:data}583584\begin{code}585dataDef :: Parser Q.DataDef586dataDef = do587 link <- many linkage588 name <- wsNL1 (string "data") >> wsNL global589 _ <- wsNL (char '=')590 alignment <- optionMaybe alignAny591 bracesNL dataObjs <&> Q.DataDef link name alignment592 where593 -- TODO: sepByTrail is not documented in the QBE BNF.594 dataObjs = sepByTrail dataObj (wsNL $ char ',')595\end{code}596597Data definitions express objects that will be emitted in the compiled598file. Their visibility and location in the compiled artifact are599controlled with linkage flags described in the \nameref{sec:linkage}600section.601602They define a global identifier (starting with the sigil \texttt{\$}), that603will contain a pointer to the object specified by the definition.604605\begin{code}606dataObj :: Parser Q.DataObj607dataObj =608 (Q.OZeroFill <$> (wsNL1 (char 'z') >> wsNL decNumber))609 <|> do610 t <- wsNL1 extType611 i <- many1 (wsNL dataItem)612 return $ Q.OItem t i613\end{code}614615Objects are described by a sequence of fields that start with a type616letter. This letter can either be an extended type, or the \texttt{z} letter.617If the letter used is an extended type, the data item following618specifies the bits to be stored in the field.619620\begin{code}621dataItem :: Parser Q.DataItem622dataItem =623 (Q.DString <$> strLit)624 <|> try625 ( do626 i <- ws global627 off <- (ws $ char '+') >> ws decNumber628 return $ Q.DSymOff i off629 )630 <|> (Q.DConst <$> constant)631\end{code}632633Within each object, several items can be defined. When several data items634follow a letter, they initialize multiple fields of the same size.635636\begin{code}637allocSize :: Parser Q.AllocSize638allocSize =639 choice640 [ bind "4" Q.AllocWord,641 bind "8" Q.AllocLong,642 bind "16" Q.AllocLongLong643 ]644\end{code}645646The members of a struct will be packed. This means that padding has to647be emitted by the frontend when necessary. Alignment of the whole data648objects can be manually specified, and when no alignment is provided,649the maximum alignment from the platform is used.650651When the \texttt{z} letter is used the number following indicates the size of652the field; the contents of the field are zero initialized. It can be653used to add padding between fields or zero-initialize big arrays.654655\subsubsection{Functions}656\label{sec:functions}657658\begin{code}659funcDef :: Parser Q.FuncDef660funcDef = do661 link <- many linkage662 _ <- ws1 (string "function")663 retTy <- optionMaybe (ws1 abity)664 name <- ws global665 args <- wsNL params666 body <- between (wsNL1 $ char '{') (wsNL $ char '}') $ many1 block667668 case (insertJumps body) of669 Nothing -> fail $ "invalid fallthrough in " ++ show name670 Just [] -> error "unreachable" -- TODO: Use NonEmpty671 Just blocks@(startBlk:_) ->672 return $673 Q.FuncDef {674 Q.fLinkage = link,675 Q.fName = name,676 Q.fStart = Q.label startBlk,677 Q.fAbity = retTy,678 Q.fParams = args,679 Q.fBlock = blkMap blocks680 }681\end{code}682683Function definitions contain the actual code to emit in the compiled684file. They define a global symbol that contains a pointer to the685function code. This pointer can be used in \texttt{call} instructions or stored686in memory.687688\begin{code}689subWordType :: Parser Q.SubWordType690subWordType = choice691 [ try $ bind "sb" Q.SignedByte692 , try $ bind "ub" Q.UnsignedByte693 , bind "sh" Q.SignedHalf694 , bind "uh" Q.UnsignedHalf ]695696abity :: Parser Q.Abity697abity = try (Q.ASubWordType <$> subWordType)698 <|> (Q.ABase <$> baseType)699 <|> (Q.AUserDef <$> userDef)700\end{code}701702The type given right before the function name is the return type of the703function. All return values of this function must have this return type.704If the return type is missing, the function must not return any value.705706\begin{code}707param :: Parser Q.FuncParam708param = (Q.Env <$> (ws1 (string "env") >> local))709 <|> (string "..." >> pure Q.Variadic)710 <|> do711 ty <- ws1 abity712 Q.Regular ty <$> local713714params :: Parser [Q.FuncParam]715params = parenLst param716\end{code}717718The parameter list is a comma separated list of temporary names prefixed719by types. The types are used to correctly implement C compatibility.720When an argument has an aggregate type, a pointer to the aggregate is721passed by thea caller. In the example below, we have to use a load722instruction to get the value of the first (and only) member of the723struct.724725\begin{verbatim}726type :one = { w }727728function w $getone(:one %p) {729@start730 %val =w loadw %p731 ret %val732}733\end{verbatim}734735If a function accepts or returns values that are smaller than a word,736such as \texttt{signed char} or \texttt{unsigned short} in C, one of the sub-word type737must be used. The sub-word types \texttt{sb}, \texttt{ub}, \texttt{sh}, and \texttt{uh} stand,738respectively, for signed and unsigned 8-bit values, and signed and739unsigned 16-bit values. Parameters associated with a sub-word type of740bit width N only have their N least significant bits set and have base741type \texttt{w}. For example, the function742743\begin{verbatim}744function w $addbyte(w %a, sb %b) {745@start746 %bw =w extsb %b747 %val =w add %a, %bw748 ret %val749}750\end{verbatim}751752needs to sign-extend its second argument before the addition. Dually,753return values with sub-word types do not need to be sign or zero754extended.755756If the parameter list ends with \texttt{...}, the function is a variadic757function: it can accept a variable number of arguments. To access the758extra arguments provided by the caller, use the \texttt{vastart} and \texttt{vaarg}759instructions described in the \nameref{sec:variadic} section.760761Optionally, the parameter list can start with an environment parameter762\texttt{env \%e}. This special parameter is a 64-bit integer temporary (i.e.,763of type \texttt{l}). If the function does not use its environment parameter,764callers can safely omit it. This parameter is invisible to a C caller:765for example, the function766767\begin{verbatim}768export function w $add(env %e, w %a, w %b) {769@start770 %c =w add %a, %b771 ret %c772}773\end{verbatim}774775must be given the C prototype \texttt{int add(int, int)}. The intended use of776this feature is to pass the environment pointer of closures while777retaining a very good compatibility with C. The \nameref{sec:call}778section explains how to pass an environment parameter.779780Since global symbols are defined mutually recursive, there is no need781for function declarations: a function can be referenced before its782definition. Similarly, functions from other modules can be used without783previous declaration. All the type information necessary to compile a784call is in the instruction itself.785786The syntax and semantics for the body of functions are described in the787\nameref{sec:control} section.788789\section{Control}790\label{sec:control}791792The IL represents programs as textual transcriptions of control flow793graphs. The control flow is serialized as a sequence of blocks of794straight-line code which are connected using jump instructions.795796\subsection{Blocks}797\label{sec:blocks}798799\ignore{800\begin{code}801-- Basic block abstraction with optional exit points. The 'insertJumps'802-- function takes care of inserting fallthrough for omitted jumps.803data Block'804 = Block'805 { label' :: Q.BlockIdent,806 phi' :: [Q.Phi],807 stmt' :: [Q.Statement],808 term' :: Maybe Q.JumpInstr809 }810 deriving (Show, Eq)811812blkMap :: [Q.Block] -> Map Q.BlockIdent Q.Block813blkMap = Map.fromList . map (\b -> (Q.label b, b))814815insertJumps :: [Block'] -> Maybe [Q.Block]816insertJumps xs = foldM go [] $ zipWithNext xs817 where818 zipWithNext :: [a] -> [(a, Maybe a)]819 zipWithNext [] = []820 zipWithNext lst@(_ : t) = zip lst $ map Just t ++ [Nothing]821822 fromBlock' :: Block' -> Q.JumpInstr -> Q.Block823 fromBlock' (Block' l p s _) = Q.Block l p s824825 go :: [Q.Block] -> (Block', Maybe Block') -> Maybe [Q.Block]826 go acc (x@Block' {term' = Just ji}, _) =827 Just (acc ++ [fromBlock' x ji])828 go acc (x@Block' {term' = Nothing}, Just nxt) =829 Just (acc ++ [fromBlock' x (Q.Jump $ label' nxt)])830 go _ (Block' {term' = Nothing}, Nothing) =831 Nothing832\end{code}833}834835\begin{code}836block :: Parser Block'837block = do838 l <- wsNL1 label839 p <- many (wsNL1 $ try phiInstr)840 s <- many (wsNL1 statement)841 Block' l p s <$> (optionMaybe $ wsNL1 jumpInstr)842\end{code}843844All blocks have a name that is specified by a label at their beginning.845Then follows a sequence of instructions that have "fall-through" flow.846Finally one jump terminates the block. The jump can either transfer847control to another block of the same function or return; jumps are848described further below.849850The first block in a function must not be the target of any jump in the851program. If a jump to the function start is needed, the frontend must852insert an empty prelude block at the beginning of the function.853854When one block jumps to the next block in the IL file, it is not855necessary to write the jump instruction, it will be automatically added856by the parser. For example the start block in the example below jumps857directly to the loop block.858859\subsection{Jumps}860\label{sec:jumps}861862\begin{code}863jumpInstr :: Parser Q.JumpInstr864jumpInstr = (string "hlt" >> pure Q.Halt)865 -- TODO: Return requires a space if there is an optionMaybe866 <|> Q.Return <$> ((ws $ string "ret") >> optionMaybe val)867 <|> try (Q.Jump <$> ((ws1 $ string "jmp") >> label))868 <|> do869 _ <- ws1 $ string "jnz"870 v <- ws val <* ws (char ',')871 l1 <- ws label <* ws (char ',')872 l2 <- ws label873 return $ Q.Jnz v l1 l2874\end{code}875876A jump instruction ends every block and transfers the control to another877program location. The target of a jump must never be the first block in878a function. The three kinds of jumps available are described in the879following list.880881\begin{enumerate}882 \item \textbf{Unconditional jump.} Jumps to another block of the same function.883 \item \textbf{Conditional jump.} When its word argument is non-zero, it jumps to its first label argument; otherwise it jumps to the other label. The argument must be of word type; because of subtyping a long argument can be passed, but only its least significant 32 bits will be compared to 0.884 \item \textbf{Function return.} Terminates the execution of the current function, optionally returning a value to the caller. The value returned must be of the type given in the function prototype. If the function prototype does not specify a return type, no return value can be used.885 \item \textbf{Program termination.} Terminates the execution of the program with a target-dependent error. This instruction can be used when it is expected that the execution never reaches the end of the block it closes; for example, after having called a function such as \texttt{exit()}.886\end{enumerate}887888\section{Instructions}889\label{sec:instructions}890891\begin{code}892instr :: Parser Q.Instr893instr =894 choice895 [ try $ binaryInstr Q.Add "add",896 try $ binaryInstr Q.Sub "sub",897 try $ binaryInstr Q.Mul "mul",898 try $ binaryInstr Q.Div "div",899 try $ binaryInstr Q.URem "urem",900 try $ binaryInstr Q.Rem "rem",901 try $ binaryInstr Q.UDiv "udiv",902 try $ binaryInstr Q.Or "or",903 try $ binaryInstr Q.Xor "xor",904 try $ binaryInstr Q.And "and",905 try $ binaryInstr Q.Sar "sar",906 try $ binaryInstr Q.Shr "shr",907 try $ binaryInstr Q.Shl "shl",908 try $ unaryInstr Q.Neg "neg",909 try $ unaryInstr Q.Cast "cast",910 try $ unaryInstr Q.Copy "copy",911 try $ unaryInstr Q.VAArg "vaarg",912 try $ loadInstr,913 try $ allocInstr,914 try $ compareInstr,915 try $ extInstr,916 try $ truncInstr,917 try $ fromFloatInstr,918 try $ toFloatInstr919 ]920\end{code}921922Instructions are the smallest piece of code in the IL, they form the body of923\nameref{sec:blocks}. This specification distinguishes instructions and924volatile instructions, the latter do not return a value. For the former, the IL925uses a three-address code, which means that one instruction computes an926operation between two operands and assigns the result to a third one.927928\begin{code}929assign :: Parser Q.Statement930assign = do931 n <- ws local932 t <- ws (char '=') >> ws1 baseType933 Q.Assign n t <$> instr934935volatileInstr :: Parser Q.Statement936volatileInstr =937 Q.Volatile <$>938 (storeInstr <|> blitInstr <|> vastartInstr <|> dbglocInstr)939940-- TODO: Not documented in the QBE BNF.941statement :: Parser Q.Statement942statement = (try callInstr) <|> assign <|> volatileInstr943\end{code}944945An instruction has both a name and a return type, this return type is a base946type that defines the size of the instruction's result. The type of the947arguments can be unambiguously inferred using the instruction name and the948return type. For example, for all arithmetic instructions, the type of the949arguments is the same as the return type. The two additions below are valid if950\texttt{\%y} is a word or a long (because of \nameref{sec:subtyping}).951952\begin{verbatim}953%x =w add 0, %y954%z =w add %x, %x955\end{verbatim}956957Some instructions, like comparisons and memory loads have operand types958that differ from their return types. For instance, two floating points959can be compared to give a word result (0 if the comparison succeeds, 1960if it fails).961962\begin{verbatim}963%c =w cgts %a, %b964\end{verbatim}965966In the example above, both operands have to have single type. This is967made explicit by the instruction suffix.968969\subsection{Arithmetic and Bits}970971\begin{quote}972\begin{itemize}973\item \texttt{add}, \texttt{sub}, \texttt{div}, \texttt{mul}974\item \texttt{neg}975\item \texttt{udiv}, \texttt{rem}, \texttt{urem}976\item \texttt{or}, \texttt{xor}, \texttt{and}977\item \texttt{sar}, \texttt{shr}, \texttt{shl}978\end{itemize}979\end{quote}980981The base arithmetic instructions in the first bullet are available for982all types, integers and floating points.983984When \texttt{div} is used with word or long return type, the arguments are985treated as signed. The unsigned integral division is available as \texttt{udiv}986instruction. When the result of a division is not an integer, it is truncated987towards zero.988989The signed and unsigned remainder operations are available as \texttt{rem} and990\texttt{urem}. The sign of the remainder is the same as the one of the991dividend. Its magnitude is smaller than the divisor one. These two instructions992and \texttt{udiv} are only available with integer arguments and result.993994Bitwise OR, AND, and XOR operations are available for both integer995types. Logical operations of typical programming languages can be996implemented using \nameref{sec:comparisions} and \nameref{sec:jumps}.997998Shift instructions \texttt{sar}, \texttt{shr}, and \texttt{shl}, shift right or999left their first operand by the amount from the second operand. The shifting1000amount is taken modulo the size of the result type. Shifting right can either1001preserve the sign of the value (using \texttt{sar}), or fill the newly freed1002bits with zeroes (using \texttt{shr}). Shifting left always fills the freed1003bits with zeroes.10041005Remark that an arithmetic shift right (\texttt{sar}) is only equivalent to a1006division by a power of two for non-negative numbers. This is because the shift1007right "truncates" towards minus infinity, while the division truncates towards1008zero.10091010\subsection{Memory}1011\label{sec:memory}10121013The following sections discuss instructions for interacting with values stored in memory.10141015\subsubsection{Store instructions}10161017\begin{code}1018storeInstr :: Parser Q.VolatileInstr1019storeInstr = do1020 t <- string "store" >> ws1 extType1021 v <- ws val1022 _ <- ws $ char ','1023 ws val <&> Q.Store t v1024\end{code}10251026Store instructions exist to store a value of any base type and any extended1027type. Since halfwords and bytes are not first class in the IL, \texttt{storeh}1028and \texttt{storeb} take a word as argument. Only the first 16 or 8 bits of1029this word will be stored in memory at the address specified in the second1030argument.10311032\subsubsection{Load instructions}10331034\begin{code}1035loadInstr :: Parser Q.Instr1036loadInstr = do1037 _ <- string "load"1038 t <- ws1 $ choice1039 [ try $ bind "sw" (Q.LBase Q.Word),1040 try $ bind "uw" (Q.LBase Q.Word),1041 try $ Q.LSubWord <$> subWordType,1042 Q.LBase <$> baseType1043 ]1044 ws val <&> Q.Load t1045\end{code}10461047For types smaller than long, two variants of the load instruction are1048available: one will sign extend the loaded value, while the other will zero1049extend it. Note that all loads smaller than long can load to either a long or a1050word.10511052The two instructions \texttt{loadsw} and \texttt{loaduw} have the same effect1053when they are used to define a word temporary. A \texttt{loadw} instruction is1054provided as syntactic sugar for \texttt{loadsw} to make explicit that the1055extension mechanism used is irrelevant.10561057\subsubsection{Blits}10581059\begin{code}1060blitInstr :: Parser Q.VolatileInstr1061blitInstr = do1062 v1 <- (ws1 $ string "blit") >> ws val <* (ws $ char ',')1063 v2 <- ws val <* (ws $ char ',')1064 nb <- decNumber1065 return $ Q.Blit v1 v2 nb1066\end{code}10671068The blit instruction copies in-memory data from its first address argument to1069its second address argument. The third argument is the number of bytes to copy.1070The source and destination spans are required to be either non-overlapping, or1071fully overlapping (source address identical to the destination address). The1072byte count argument must be a nonnegative numeric constant; it cannot be a1073temporary.10741075One blit instruction may generate a number of instructions proportional to its1076byte count argument, consequently, it is recommended to keep this argument1077relatively small. If large copies are necessary, it is preferable that1078frontends generate calls to a supporting \texttt{memcpy} function.10791080\subsubsection{Stack Allocation}10811082\begin{code}1083allocInstr :: Parser Q.Instr1084allocInstr = do1085 siz <- (ws $ string "alloc") >> (ws1 allocSize)1086 val <&> Q.Alloc siz1087\end{code}10881089These instructions allocate a chunk of memory on the stack. The number ending1090the instruction name is the alignment required for the allocated slot. QBE will1091make sure that the returned address is a multiple of that alignment value.10921093Stack allocation instructions are used, for example, when compiling the C local1094variables, because their address can be taken. When compiling Fortran,1095temporaries can be used directly instead, because it is illegal to take the1096address of a variable.10971098\subsection{Comparisons}1099\label{sec:comparisions}11001101\begin{code}1102compareInstr :: Parser Q.Instr1103compareInstr = do1104 _ <- char 'c'1105 (try intCompare) <|> floatCompare11061107compareArgs :: Parser (Q.Value, Q.Value)1108compareArgs = do1109 lhs <- ws val <* ws (char ',')1110 rhs <- ws val1111 pure (lhs, rhs)11121113intCompare :: Parser Q.Instr1114intCompare = do1115 op <- compareIntOp1116 ty <- ws1 intArg11171118 (lhs, rhs) <- compareArgs1119 pure $ Q.CompareInt ty op lhs rhs11201121floatCompare :: Parser Q.Instr1122floatCompare = do1123 op <- compareFloatOp1124 ty <- ws1 floatArg11251126 (lhs, rhs) <- compareArgs1127 pure $ Q.CompareFloat ty op lhs rhs1128\end{code}11291130Comparison instructions return an integer value (either a word or a long), and1131compare values of arbitrary types. The returned value is 1 if the two operands1132satisfy the comparison relation, or 0 otherwise. The names of comparisons1133respect a standard naming scheme in three parts:11341135\begin{enumerate}1136 \item All comparisons start with the letter \texttt{c}.1137 \item Then comes a comparison type.1138 \item Finally, the instruction name is terminated with a basic type suffix precising the type of the operands to be compared.1139\end{enumerate}11401141The following instruction are available for integer comparisons:11421143\begin{code}1144compareIntOp :: Parser Q.IntCmpOp1145compareIntOp = choice1146 [ bind "eq" Q.IEq1147 , bind "ne" Q.INe1148 , try $ bind "sle" Q.ISle1149 , try $ bind "slt" Q.ISlt1150 , try $ bind "sge" Q.ISge1151 , try $ bind "sgt" Q.ISgt1152 , try $ bind "ule" Q.IUle1153 , try $ bind "ult" Q.IUlt1154 , try $ bind "uge" Q.IUge1155 , try $ bind "ugt" Q.IUgt ]1156\end{code}11571158For floating point comparisons use one of these instructions:11591160\begin{code}1161compareFloatOp :: Parser Q.FloatCmpOp1162compareFloatOp = choice1163 [ bind "eq" Q.FEq1164 , bind "ne" Q.FNe1165 , try $ bind "le" Q.FLe1166 , bind "lt" Q.FLt1167 , try $ bind "ge" Q.FGe1168 , bind "gt" Q.FGt1169 , bind "o" Q.FOrd1170 , bind "uo" Q.FUnord ]1171\end{code}11721173For example, \texttt{cod} compares two double-precision floating point numbers1174and returns 1 if the two floating points are not NaNs, or 0 otherwise. The1175\texttt{csltw} instruction compares two words representing signed numbers and1176returns 1 when the first argument is smaller than the second one.11771178\subsection{Conversions}11791180Conversion operations change the representation of a value, possibly modifying1181it if the target type cannot hold the value of the source type. Conversions can1182extend the precision of a temporary (e.g., from signed 8-bit to 32-bit), or1183convert a floating point into an integer and vice versa.11841185\begin{code}1186extInstr :: Parser Q.Instr1187extInstr = do1188 _ <- string "ext"1189 ty <- ws1 extArg1190 ws val <&> Q.Ext ty1191 where1192 extArg :: Parser Q.ExtArg1193 extArg = try (Q.ExtSubWord <$> subWordType)1194 <|> try (bind "sw" Q.ExtSignedWord)1195 <|> bind "s" Q.ExtSingle1196 <|> bind "uw" Q.ExtUnsignedWord1197\end{code}11981199Extending the precision of a temporary is done using the \texttt{ext} family of1200instructions. Because QBE types do not specify the signedness (like in LLVM),1201extension instructions exist to sign-extend and zero-extend a value. For1202example, \texttt{extsb} takes a word argument and sign-extends the 81203least-significant bits to a full word or long, depending on the return type.12041205\begin{code}1206truncInstr :: Parser Q.Instr1207truncInstr = do1208 _ <- ws1 $ string "truncd"1209 ws val <&> Q.TruncDouble1210\end{code}12111212The instructions \texttt{exts} (extend single) and \texttt{truncd} (truncate1213double) are provided to change the precision of a floating point value. When1214the double argument of truncd cannot be represented as a single-precision1215floating point, it is truncated towards zero.12161217\begin{code}1218floatArg :: Parser Q.FloatArg1219floatArg = bind "d" Q.FDouble <|> bind "s" Q.FSingle12201221fromFloatInstr :: Parser Q.Instr1222fromFloatInstr = do1223 arg <- floatArg <* string "to"1224 isSigned <- signageChar1225 _ <- ws1 $ char 'i'1226 ws val <&> Q.FloatToInt arg isSigned12271228intArg :: Parser Q.IntArg1229intArg = bind "w" Q.IWord <|> bind "l" Q.ILong12301231toFloatInstr :: Parser Q.Instr1232toFloatInstr = do1233 isSigned <- signageChar1234 arg <- intArg1235 _ <- ws1 $ string "tof"1236 ws val <&> Q.IntToFloat arg isSigned1237\end{code}12381239Converting between signed integers and floating points is done using1240\texttt{stosi} (single to signed integer), \texttt{stoui} (single to unsigned1241integer), \texttt{dtosi} (double to signed integer), \texttt{dtoui} (double to1242unsigned integer), \texttt{swtof} (signed word to float), \texttt{uwtof}1243(unsigned word to float), \texttt{sltof} (signed long to float) and1244\texttt{ultof} (unsigned long to float).12451246\subsection{Cast and Copy}12471248The \texttt{cast} and \texttt{copy} instructions return the bits of their1249argument verbatim. However a cast will change an integer into a floating point1250of the same width and vice versa.12511252Casts can be used to make bitwise operations on the representation of floating1253point numbers. For example the following program will compute the opposite of1254the single-precision floating point number \texttt{\%f} into \texttt{\%rs}.12551256\begin{verbatim}1257%b0 =w cast %f1258%b1 =w xor 2147483648, %b0 # flip the msb1259%rs =s cast %b11260\end{verbatim}12611262\subsection{Call}1263\label{sec:call}12641265\begin{code}1266-- TODO: Code duplication with 'param'.1267callArg :: Parser Q.FuncArg1268callArg = (Q.ArgEnv <$> (ws1 (string "env") >> val))1269 <|> (string "..." >> pure Q.ArgVar)1270 <|> do1271 ty <- ws1 abity1272 Q.ArgReg ty <$> val12731274callArgs :: Parser [Q.FuncArg]1275callArgs = parenLst callArg12761277callInstr :: Parser Q.Statement1278callInstr = do1279 retValue <- optionMaybe $ do1280 i <- ws local <* ws (char '=')1281 a <- ws1 abity1282 return (i, a)1283 toCall <- ws1 (string "call") >> ws val1284 fnArgs <- callArgs1285 return $ Q.Call retValue toCall fnArgs1286\end{code}12871288The call instruction is special in several ways. It is not a three-address1289instruction and requires the type of all its arguments to be given. Also, the1290return type can be either a base type or an aggregate type. These specifics are1291required to compile calls with C compatibility (i.e., to respect the ABI).12921293When an aggregate type is used as argument type or return type, the value1294respectively passed or returned needs to be a pointer to a memory location1295holding the value. This is because aggregate types are not first-class1296citizens of the IL.12971298Sub-word types are used for arguments and return values of width less than a1299word. Details on these types are presented in the \nameref{sec:functions} section.1300Arguments with sub-word types need not be sign or zero extended according to1301their type. Calls with a sub-word return type define a temporary of base type1302\texttt{w} with its most significant bits unspecified.13031304Unless the called function does not return a value, a return temporary must be1305specified, even if it is never used afterwards.13061307An environment parameter can be passed as first argument using the \texttt{env}1308keyword. The passed value must be a 64-bit integer. If the called function does1309not expect an environment parameter, it will be safely discarded. See the1310\nameref{sec:functions} section for more information about environment1311parameters.13121313When the called function is variadic, there must be a \texttt{...} marker1314separating the named and variadic arguments.13151316\subsection{Variadic}1317\label{sec:variadic}13181319\begin{code}1320vastartInstr :: Parser Q.VolatileInstr1321vastartInstr = do1322 _ <- ws1 (string "vastart")1323 Q.VAStart <$> ws val1324\end{code}13251326The \texttt{vastart} and \texttt{vaarg} instructions provide a portable way to1327access the extra parameters of a variadic function.13281329\begin{enumerate}1330 \item \texttt{vastart} -- \texttt{(m)}1331 \item \texttt{vaarg} -- \texttt{T(mmmm)}1332\end{enumerate}13331334The \texttt{vastart} instruction initializes a variable argument list used to1335access the extra parameters of the enclosing variadic function. It is safe to1336call it multiple times.13371338The \texttt{vaarg} instruction fetches the next argument from a variable1339argument list. It is currently limited to fetching arguments that have a base1340type. This instruction is essentially effectful: calling it twice in a row will1341return two consecutive arguments from the argument list.13421343Both instructions take a pointer to a variable argument list as the sole argument.1344The size and alignment of the variable argument lists depends on the target used.13451346\subsection{Phi}13471348\begin{code}1349phiBranch :: Parser (Q.BlockIdent, Q.Value)1350phiBranch = do1351 n <- ws1 label1352 v <- val1353 pure (n, v)13541355phiInstr :: Parser Q.Phi1356phiInstr = do1357 -- TODO: code duplication with 'assign'1358 n <- ws local1359 t <- ws (char '=') >> ws1 baseType13601361 _ <- ws1 (string "phi")1362 -- TODO: combinator for sepBy1363 p <- Map.fromList <$> sepBy1 (ws phiBranch) (ws $ char ',')1364 return $ Q.Phi n t p1365\end{code}13661367First and foremost, phi instructions are NOT necessary when writing a frontend1368to QBE. One solution to avoid having to deal with SSA form is to use stack1369allocated variables for all source program variables and perform assignments1370and lookups using \nameref{sec:memory} operations. This is what LLVM users1371typically do.13721373Another solution is to simply emit code that is not in SSA form! Contrary to1374LLVM, QBE is able to fixup programs not in SSA form without requiring the1375boilerplate of loading and storing in memory. For example, the following1376program will be correctly compiled by QBE.13771378\begin{verbatim}1379@start1380 %x =w copy 1001381 %s =w copy 01382@loop1383 %s =w add %s, %x1384 %x =w sub %x, 11385 jnz %x, @loop, @end1386@end1387 ret %s1388\end{verbatim}13891390Now, if you want to know what phi instructions are and how to use them in QBE,1391you can read the following.13921393Phi instructions are specific to SSA form. In SSA form values can only be1394assigned once, without phi instructions, this requirement is too strong to1395represent many programs. For example consider the following C program.13961397\begin{verbatim}1398int f(int x) {1399 int y;1400 if (x)1401 y = 1;1402 else1403 y = 2;1404 return y;1405}1406\end{verbatim}14071408The variable \texttt{y} is assigned twice, the solution to translate it in SSA1409form is to insert a phi instruction.14101411\begin{verbatim}1412@ifstmt1413 jnz %x, @ift, @iff1414@ift1415 jmp @retstmt1416@iff1417 jmp @retstmt1418@retstmt1419 %y =w phi @ift 1, @iff 21420 ret %y1421\end{verbatim}14221423Phi instructions return one of their arguments depending on where the control1424came from. In the example, \texttt{\%y} is set to 1 if the1425\texttt{\textbackslash{}ift} branch is taken, or it is set to 2 otherwise.14261427An important remark about phi instructions is that QBE assumes that if a1428variable is defined by a phi it respects all the SSA invariants. So it is1429critical to not use phi instructions unless you know exactly what you are1430doing.14311432\subsection{Debug Information}14331434QBE supports the inclusion of debug information. Specifically, it allows1435defining from which source file type, data, and function definitions originated.1436For this purpose, it provides the \texttt{dbgfile} definition, which receives a1437file name (string literal) as its sole argument. Every type, data and function1438definition thereafter are assumed to originate in this file.14391440\begin{code}1441-- TODO: not documnted in the QBE BNF.1442fileDef :: Parser String1443fileDef = do1444 _ <- ws1 $ string "dbgfile"1445 wsNL1 strLit1446\end{code}14471448Further, instructions within a function can be associated with a specific line1449and column number of a previously defined \texttt{dbgfile}. The1450\texttt{dbgfile} is referenced by index using the first argument to1451\texttt{dbgloc}. The second argument represents the line number, the third1452(optional) argument the column number.14531454\begin{code}1455-- TODO: not documnted in the QBE BNF.1456dbglocInstr :: Parser Q.VolatileInstr1457dbglocInstr = do1458 _ <- ws1 $ string "dbgloc"1459 file <- ws decNumber <* ws (char ',')1460 line <- ws decNumber1461 col <- optionMaybe (ws (char ',') >> ws decNumber)1462 return $ Q.DBGLoc file line col1463\end{code}14641465\end{document}