doc: fix name of export linkage flag
[qbe.git] / doc / il.txt
blob8713e0d3ad1e9c8f2fa5f4bd949344924b8c7db8
1                 ===========================
2                  QBE Intermediate Language
3                 ===========================
7 - Table of Contents
8 -------------------
10   1. <@ Basic Concepts >
11       * <@ Input Files >
12       * <@ BNF Notation >
13       * <@ Sigils >
14   2. <@ Types >
15       * <@ Simple Types >
16       * <@ Subtyping >
17   3. <@ Constants >
18   4. <@ Linkage >
19   5. <@ Definitions >
20       * <@ Aggregate Types >
21       * <@ Data >
22       * <@ Functions >
23   6. <@ Control >
24       * <@ Blocks >
25       * <@ Jumps >
26   7. <@ Instructions >
27       * <@ Arithmetic and Bits >
28       * <@ Memory >
29       * <@ Comparisons >
30       * <@ Conversions >
31       * <@ Cast and Copy >
32       * <@ Call >
33       * <@ Variadic >
34       * <@ Phi >
35   8. <@ Instructions Index >
37 - 1. Basic Concepts
38 -------------------
40 The intermediate language (IL) is a higher-level language
41 than the machine's assembly language.  It smoothes most
42 of the irregularities of the underlying hardware and
43 allows an infinite number of temporaries to be used.
44 This higher abstraction level allows frontend programmers
45 to focus on language design issues.
47 ~ Input Files
48 ~~~~~~~~~~~~~
50 The intermediate language is provided to QBE as text.
51 Usually, one file is generated per each compilation unit from
52 the frontend input language.  An IL file is a sequence of
53 <@ Definitions > for data, functions, and types.  Once
54 processed by QBE, the resulting file can be assembled and
55 linked using a standard toolchain (e.g., GNU binutils).
57 Here is a complete "Hello World" IL file which defines a
58 function that prints to the screen.  Since the string is
59 not a first class object (only the pointer is) it is
60 defined outside the function's body.  Comments start with
61 a # character and finish with the end of the line.
63     # Define the string constant.
64     data $str = { b "hello world", b 0 }
66     function w $main() {
67     @start
68             # Call the puts function with $str as argument.
69             %r =w call $puts(l $str)
70             ret 0
71     }
73 If you have read the LLVM language reference, you might
74 recognize the example above.  In comparison, QBE makes a
75 much lighter use of types and the syntax is terser.
77 ~ BNF Notation
78 ~~~~~~~~~~~~~~
80 The language syntax is vaporously described in the sections
81 below using BNF syntax.  The different BNF constructs used
82 are listed below.
84   * Keywords are enclosed between quotes;
85   * `... | ...` expresses disjunctions;
86   * `[ ... ]` marks some syntax as optional;
87   * `( ... ),` designates a comma-separated list of the
88     enclosed syntax;
89   * `...*` and `...+` are used for arbitrary and
90     at-least-once repetition respectively.
92 ~ Sigils
93 ~~~~~~~~
95 The intermediate language makes heavy use of sigils, all
96 user-defined names are prefixed with a sigil.  This is
97 to avoid keyword conflicts, and also to quickly spot the
98 scope and nature of identifiers.
100  * `:` is for user-defined <@ Aggregate Types>
101  * `$` is for globals (represented by a pointer)
102  * `%` is for function-scope temporaries
103  * `@` is for block labels
105 In this BNF syntax, we use `?IDENT` to designate an identifier
106 starting with the sigil `?`.
108 - 2. Types
109 ----------
111 ~ Simple Types
112 ~~~~~~~~~~~~~~
114     `bnf
115     BASETY := 'w' | 'l' | 's' | 'd'  # Base types
116     EXTTY  := BASETY    | 'b' | 'h'  # Extended types
118 The IL makes minimal use of types.  By design, the types
119 used are restricted to what is necessary for unambiguous
120 compilation to machine code and C interfacing.  Unlike LLVM,
121 QBE is not using types as a means to safety; they are only
122 here for semantic purposes.
124 The four base types are `w` (word), `l` (long), `s` (single),
125 and `d` (double), they stand respectively for 32-bit and
126 64-bit integers, and 32-bit and 64-bit floating-point numbers.
127 There are no pointer types available; pointers are typed
128 by an integer type sufficiently wide to represent all memory
129 addresses (e.g., `l` on 64-bit architectures).  Temporaries
130 in the IL can only have a basic type.
132 Extended types contain base types plus `b` (byte) and `h`
133 (half word), respectively for 8-bit and 16-bit integers.
134 They are used in <@ Aggregate Types> and <@ Data> definitions.
136 For C interfacing, the IL also provides user-defined aggregate
137 types.  The syntax used to designate them is `:foo`.  Details
138 about their definition are given in the <@ Aggregate Types >
139 section.
141 ~ Subtyping
142 ~~~~~~~~~~~
144 The IL has a minimal subtyping feature, for integer types only.
145 Any value of type `l` can be used in a `w` context.  In that
146 case, only the 32 least significant bits of the word value
147 are used.
149 Make note that it is the opposite of the usual subtyping on
150 integers (in C, we can safely use an `int` where a `long`
151 is expected).  A long value cannot be used in word context.
152 The rationale is that a word can be signed or unsigned, so
153 extending it to a long could be done in two ways, either
154 by zero-extension, or by sign-extension.
156 - 3. Constants
157 --------------
159     `bnf
160     CONST :=
161         ['-'] NUMBER  # Decimal integer
162       | 's_' FP       # Single-precision float
163       | 'd_' FP       # Double-precision float
164       | $IDENT        # Global symbol
166 Throughout the IL, constants are specified with a unified
167 syntax and semantics.  Constants are immediates, meaning
168 that they can be used directly in instructions; there is
169 no need for a "load constant" instruction.
171 The representation of integers is two's complement.
172 Floating-point numbers are represented using the
173 single-precision and double-precision formats of the
174 IEEE 754 standard.
176 Constants specify a sequence of bits and are untyped.
177 They are always parsed as 64-bit blobs.  Depending on
178 the context surrounding a constant, only some of its
179 bits are used.  For example, in the program below, the
180 two variables defined have the same value since the first
181 operand of the subtraction is a word (32-bit) context.
183     %x =w sub -1, 0
184     %y =w sub 4294967295, 0
186 Because specifying floating-point constants by their bits
187 makes the code less readable, syntactic sugar is provided
188 to express them.  Standard scientific notation is prefixed
189 with `s_` and `d_` for single and double precision numbers
190 respectively. Once again, the following example defines twice
191 the same double-precision constant.
193     %x =d add d_0, d_-1
194     %y =d add d_0, -4616189618054758400
196 Global symbols can also be used directly as constants;
197 they will be resolved and turned into actual numeric
198 constants by the linker.
200 - 4. Linkage
201 ------------
203     `bnf
204     LINKAGE :=
205         'export'
206       | 'section' SECNAME
207       | 'section' SECNAME SECFLAGS
209     SECNAME  := '"' .... '"'
210     SECFLAGS := '"' .... '"'
212 Function and data definitions (see below) can specify
213 linkage information to be passed to the assembler and
214 eventually to the linker.
216 The `export` linkage flag marks the defined item as
217 visible outside the current file's scope.  If absent,
218 the symbol can only be referred to locally.  Functions
219 compiled by QBE and called from C need to be exported.
221 A `section` flag can be specified to tell the linker to
222 put the defined item in a certain section.  The use of
223 the section flag is platform dependent and we refer the
224 user to the documentation of their assembler and linker
225 for relevant information.
227     export section ".bss"
228     data $zerobuf = { z 1024 }
230 Example uses of the section flag include adding function
231 pointers to a global initialization list, or storing a
232 zeroed object in the BSS section, as depicted above.
234 The section and export linkage flags should each appear
235 at most once in a definition.  If multiple occurrences
236 are present, QBE is free to use any.
238 - 5. Definitions
239 ----------------
241 Definitions are the essential components of an IL file.
242 They can define three types of objects: aggregate types,
243 data, and functions.  Aggregate types are never exported
244 and do not compile to any code.  Data and function
245 definitions have file scope and are mutually recursive
246 (even across IL files).  Their visibility can be controlled
247 using the `export` keyword.
249 ~ Aggregate Types
250 ~~~~~~~~~~~~~~~~~
252     `bnf
253     TYPEDEF :=
254         # Regular type
255         'type' :IDENT '=' ['align' NUMBER]
256         '{'
257             ( SUBTY [NUMBER] ),
258         '}'
259       | # Opaque type
260         'type' :IDENT '=' 'align' NUMBER '{' NUMBER '}'
262     SUBTY := EXTTY | :IDENT
264 Aggregate type definitions start with the `type` keyword.
265 They have file scope, but types must be defined before being
266 referenced.  The inner structure of a type is expressed by a
267 comma-separated list of types enclosed in curly braces.
269     type :fourfloats = { s, s, d, d }
271 For ease of IL generation, a trailing comma is tolerated by
272 the parser.  In case many items of the same type are
273 sequenced (like in a C array), the shorter array syntax
274 can be used.
276     type :abyteandmanywords = { b, w 100 }
278 By default, the alignment of an aggregate type is the
279 maximum alignment of its members.  The alignment can be
280 explicitly specified by the programmer.
282     type :cryptovector = align 16 { w 4 }
284 Opaque types are used when the inner structure of an
285 aggregate cannot be specified; the alignment for opaque
286 types is mandatory.  They are defined simply by enclosing
287 their size between curly braces.
289     type :opaque = align 16 { 32 }
291 ~ Data
292 ~~~~~~
294     `bnf
295     DATADEF :=
296         LINKAGE* 'data' $IDENT '=' ['align' NUMBER]
297         '{'
298             ( EXTTY DATAITEM+
299             | 'z'   NUMBER ),
300         '}'
302     DATAITEM :=
303         $IDENT ['+' NUMBER]  # Symbol and offset
304       |  '"' ... '"'         # String
305       |  CONST               # Constant
307 Data definitions express objects that will be emitted in the
308 compiled file.  Their visibility and location in the compiled
309 artifact are controlled with linkage flags described in the
310 <@ Linkage > section.
312 They define a global identifier (starting with the sigil
313 `$`), that will contain a pointer to the object specified
314 by the definition.
316 Objects are described by a sequence of fields that start with
317 a type letter.  This letter can either be an extended type,
318 or the `z` letter.  If the letter used is an extended type,
319 the data item following specifies the bits to be stored in
320 the field.  When several data items follow a letter, they
321 initialize multiple fields of the same size.
323 The members of a struct will be packed.  This means that
324 padding has to be emitted by the frontend when necessary.
325 Alignment of the whole data objects can be manually specified,
326 and when no alignment is provided, the maximum alignment from
327 the platform is used.
329 When the `z` letter is used the number following indicates
330 the size of the field; the contents of the field are zero
331 initialized.  It can be used to add padding between fields
332 or zero-initialize big arrays.
334 Here are various examples of data definitions.
336     # Three 32-bit values 1, 2, and 3
337     # followed by a 0 byte.
338     data $a = { w 1 2 3, b 0 }
340     # A thousand bytes 0 initialized.
341     data $b = { z 1000 }
343     # An object containing two 64-bit
344     # fields, one with all bits sets and the
345     # other containing a pointer to the
346     # object itself.
347     data $c = { l -1, l $c }
349 ~ Functions
350 ~~~~~~~~~~~
352     `bnf
353     FUNCDEF :=
354         LINKAGE* 'function' [ABITY] $IDENT '(' (PARAM), ')'
355         '{'
356            BLOCK+
357         '}'
359     PARAM :=
360         ABITY %IDENT  # Regular parameter
361       | 'env' %IDENT  # Environment parameter (first)
362       | '...'         # Variadic marker (last)
364     ABITY := BASETY | :IDENT
366 Function definitions contain the actual code to emit in
367 the compiled file.  They define a global symbol that
368 contains a pointer to the function code.  This pointer
369 can be used in `call` instructions or stored in memory.
371 The type given right before the function name is the
372 return type of the function.  All return values of this
373 function must have this return type.  If the return
374 type is missing, the function cannot return any value.
376 The parameter list is a comma separated list of
377 temporary names prefixed by types.  The types are used
378 to correctly implement C compatibility.  When an argument
379 has an aggregate type, a pointer to the aggregate is passed
380 by the caller.  In the example below, we have to use a load
381 instruction to get the value of the first (and only)
382 member of the struct.
384     type :one = { w }
386     function w $getone(:one %p) {
387     @start
388             %val =w loadw %p
389             ret %val
390     }
392 If the parameter list ends with `...`, the function is
393 a variadic function: it can accept a variable number of
394 arguments.  To access the extra arguments provided by
395 the caller, use the `vastart` and `vaarg` instructions
396 described in the <@ Variadic > section.
398 Optionally, the parameter list can start with an
399 environment parameter `env %e`.  This special parameter is
400 a 64-bit integer temporary (i.e., of type `l`).  If the
401 function does not use its environment parameter, callers
402 can safely omit it.  This parameter is invisible to a C
403 caller: for example, the function
405     export function w $add(env %e, w %a, w %b) {
406     @start
407             %c =w add %a, %b
408             ret %c
409     }
411 must be given the C prototype `int add(int, int)`.
412 The intended use of this feature is to pass the
413 environment pointer of closures while retaining a
414 very good compatibility with C.  The <@ Call > section
415 explains how to pass an environment parameter.
417 Since global symbols are defined mutually recursive,
418 there is no need for function declarations: a function
419 can be referenced before its definition.
420 Similarly, functions from other modules can be used
421 without previous declaration.  All the type information
422 is provided in the call instructions.
424 The syntax and semantics for the body of functions
425 are described in the <@ Control > section.
427 - 6. Control
428 ------------
430 The IL represents programs as textual transcriptions of
431 control flow graphs.  The control flow is serialized as
432 a sequence of blocks of straight-line code which are
433 connected using jump instructions.
435 ~ Blocks
436 ~~~~~~~~
438     `bnf
439     BLOCK :=
440         @IDENT    # Block label
441         PHI*      # Phi instructions
442         INST*     # Regular instructions
443         JUMP      # Jump or return
445 All blocks have a name that is specified by a label at
446 their beginning.  Then follows a sequence of instructions
447 that have "fall-through" flow.  Finally one jump terminates
448 the block.  The jump can either transfer control to another
449 block of the same function or return; they are described
450 further below.
452 The first block in a function must not be the target of
453 any jump in the program.  If this is really needed,
454 the frontend could insert an empty prelude block
455 at the beginning of the function.
457 When one block jumps to the next block in the IL file,
458 it is not necessary to give the jump instruction, it
459 will be automatically added by the parser.  For example
460 the start block in the example below jumps directly
461 to the loop block.
463     function $loop() {
464     @start
465     @loop
466             %x =w phi @start 100, @loop %x1
467             %x1 =w sub %x, 1
468             jnz %x1, @loop, @end
469     @end
470             ret
471     }
473 ~ Jumps
474 ~~~~~~~
476     `bnf
477     JUMP :=
478         'jmp' @IDENT               # Unconditional
479       | 'jnz' VAL, @IDENT, @IDENT  # Conditional
480       | 'ret' [VAL]                # Return
482 A jump instruction ends every block and transfers the
483 control to another program location.  The target of
484 a jump must never be the first block in a function.
485 The three kinds of jumps available are described in
486 the following list.
488  1. Unconditional jump.
490     Simply jumps to another block of the same function.
492  2. Conditional jump.
494     When its word argument is non-zero, it jumps to its
495     first label argument; otherwise it jumps to the other
496     label.  The argument must be of word type; because of
497     subtyping a long argument can be passed, but only its
498     least significant 32 bits will be compared to 0.
500  3. Function return.
502     Terminates the execution of the current function,
503     optionally returning a value to the caller.  The value
504     returned must be of the type given in the function
505     prototype.  If the function prototype does not specify
506     a return type, no return value can be used.
508 - 7. Instructions
509 -----------------
511 Instructions are the smallest piece of code in the IL, they
512 form the body of <@ Blocks >.  The IL uses a three-address
513 code, which means that one instruction computes an operation
514 between two operands and assigns the result to a third one.
516 An instruction has both a name and a return type, this
517 return type is a base type that defines the size of the
518 instruction's result.  The type of the arguments can be
519 unambiguously inferred using the instruction name and the
520 return type.  For example, for all arithmetic instructions,
521 the type of the arguments is the same as the return type.
522 The two additions below are valid if `%y` is a word or a long
523 (because of <@ Subtyping >).
525     %x =w add 0, %y
526     %z =w add %x, %x
528 Some instructions, like comparisons and memory loads
529 have operand types that differ from their return types.
530 For instance, two floating points can be compared to give a
531 word result (0 if the comparison succeeds, 1 if it fails).
533     %c =w cgts %a, %b
535 In the example above, both operands have to have single type.
536 This is made explicit by the instruction suffix.
538 The types of instructions are described below using a short
539 type string.  A type string specifies all the valid return
540 types an instruction can have, its arity, and the type of
541 its arguments depending on its return type.
543 Type strings begin with acceptable return types, then
544 follows, in parentheses, the possible types for the arguments.
545 If the N-th return type of the type string is used for an
546 instruction, the arguments must use the N-th type listed for
547 them in the type string.  When an instruction does not have a
548 return type, the type string only contains the types of the
549 arguments.
551 The following abbreviations are used.
553   * `T` stands for `wlsd`
554   * `I` stands for `wl`
555   * `F` stands for `sd`
556   * `m` stands for the type of pointers on the target; on
557     64-bit architectures it is the same as `l`
559 For example, consider the type string `wl(F)`, it mentions
560 that the instruction has only one argument and that if the
561 return type used is long, the argument must be of type double.
563 ~ Arithmetic and Bits
564 ~~~~~~~~~~~~~~~~~~~~~
566   * `add`, `sub`, `div`, `mul` -- `T(T,T)`
567   * `neg` -- `T(T)`
568   * `udiv`, `rem`, `urem` -- `I(I,I)`
569   * `or`, `xor`, `and` -- `I(I,I)`
570   * `sar`, `shr`, `shl` -- `I(I,ww)`
572 The base arithmetic instructions in the first bullet are
573 available for all types, integers and floating points.
575 When `div` is used with word or long return type, the
576 arguments are treated as signed.  The unsigned integral
577 division is available as `udiv` instruction.  When the
578 result of a division is not an integer, it is truncated
579 towards zero.
581 The signed and unsigned remainder operations are available
582 as `rem` and `urem`.  The sign of the remainder is the same
583 as the one of the dividend.  Its magnitude is smaller than
584 the divisor one.  These two instructions and `udiv` are only
585 available with integer arguments and result.
587 Bitwise OR, AND, and XOR operations are available for both
588 integer types.  Logical operations of typical programming
589 languages can be implemented using <@ Comparisons > and
590 <@ Jumps >.
592 Shift instructions `sar`, `shr`, and `shl`, shift right or
593 left their first operand by the amount from the second
594 operand.  The shifting amount is taken modulo the size of
595 the result type.  Shifting right can either preserve the
596 sign of the value (using `sar`), or fill the newly freed
597 bits with zeroes (using `shr`).  Shifting left always
598 fills the freed bits with zeroes.
600 Remark that an arithmetic shift right (`sar`) is only
601 equivalent to a division by a power of two for non-negative
602 numbers.  This is because the shift right "truncates"
603 towards minus infinity, while the division truncates
604 towards zero.
606 ~ Memory
607 ~~~~~~~~
609   * Store instructions.
611       * `stored` -- `(d,m)`
612       * `stores` -- `(s,m)`
613       * `storel` -- `(l,m)`
614       * `storew` -- `(w,m)`
615       * `storeh` -- `(w,m)`
616       * `storeb` -- `(w,m)`
618     Store instructions exist to store a value of any base type
619     and any extended type.  Since halfwords and bytes are not
620     first class in the IL, `storeh` and `storeb` take a word
621     as argument.  Only the first 16 or 8 bits of this word will
622     be stored in memory at the address specified in the second
623     argument.
625   * Load instructions.
627       * `loadd` -- `d(m)`
628       * `loads` -- `s(m)`
629       * `loadl` -- `l(m)`
630       * `loadsw`, `loaduw` -- `I(mm)`
631       * `loadsh`, `loaduh` -- `I(mm)`
632       * `loadsb`, `loadub` -- `I(mm)`
634     For types smaller than long, two variants of the load
635     instruction are available: one will sign extend the loaded
636     value, while the other will zero extend it.  Note that
637     all loads smaller than long can load to either a long or
638     a word.
640     The two instructions `loadsw` and `loaduw` have the same
641     effect when they are used to define a word temporary.
642     A `loadw` instruction is provided as syntactic sugar for
643     `loadsw` to make explicit that the extension mechanism
644     used is irrelevant.
646   * Stack allocation.
648       * `alloc4` -- `m(l)`
649       * `alloc8` -- `m(l)`
650       * `alloc16` -- `m(l)`
652     These instructions allocate a chunk of memory on the
653     stack.  The number ending the instruction name is the
654     alignment required for the allocated slot.  QBE will
655     make sure that the returned address is a multiple of
656     that alignment value.
658     Stack allocation instructions are used, for example,
659     when compiling the C local variables, because their
660     address can be taken.  When compiling Fortran,
661     temporaries can be used directly instead, because
662     it is illegal to take the address of a variable.
664 The following example makes use some of the memory
665 instructions.  Pointers are stored in long temporaries.
667     %A0 =l alloc4 8      # stack allocate an array A of 2 words
668     %A1 =l add %A0, 4
669     storew 43,  %A0      # A[0] <- 43
670     storew 255, %A1      # A[1] <- 255
671     %v1 =w loadw  %A0    # %v1 <- A[0] as word
672     %v2 =w loadsb %A1    # %v2 <- A[1] as signed byte
673     %v3 =w add %v1, %v2  # %v3 is 42 here
675 ~ Comparisons
676 ~~~~~~~~~~~~~
678 Comparison instructions return an integer value (either a word
679 or a long), and compare values of arbitrary types.  The returned
680 value is 1 if the two operands satisfy the comparison
681 relation, or 0 otherwise.  The names of comparisons respect
682 a standard naming scheme in three parts.
684  1. All comparisons start with the letter `c`.
686  2. Then comes a comparison type.  The following
687     types are available for integer comparisons:
689       * `eq` for equality
690       * `ne` for inequality
691       * `sle` for signed lower or equal
692       * `slt` for signed lower
693       * `sge` for signed greater or equal
694       * `sgt` for signed greater
695       * `ule` for unsigned lower or equal
696       * `ult` for unsigned lower
697       * `uge` for unsigned greater or equal
698       * `ugt` for unsigned greater
700     Floating point comparisons use one of these types:
702       * `eq` for equality
703       * `ne` for inequality
704       * `le` for lower or equal
705       * `lt` for lower
706       * `ge` for greater or equal
707       * `gt` for greater
708       * `o` for ordered (no operand is a NaN)
709       * `uo` for unordered (at least one operand is a NaN)
711     Because floating point types always have a sign bit,
712     all the comparisons available are signed.
714  3. Finally, the instruction name is terminated with a
715     basic type suffix precising the type of the operands
716     to be compared.
718 For example, `cod` (`I(dd,dd)`) compares two double-precision
719 floating point numbers and returns 1 if the two floating points
720 are not NaNs, or 0 otherwise.  The `csltw` (`I(ww,ww)`)
721 instruction compares two words representing signed numbers and
722 returns 1 when the first argument is smaller than the second one.
724 ~ Conversions
725 ~~~~~~~~~~~~~
727 Conversion operations allow to change the representation of
728 a value, possibly modifying it if the target type cannot hold
729 the value of the source type.  Conversions can extend the
730 precision of a temporary (e.g., from signed 8-bit to 32-bit),
731 or convert a floating point into an integer and vice versa.
733   * `extsw`, `extuw` -- `l(w)`
734   * `extsh`, `extuh` -- `I(ww)`
735   * `extsb`, `extub` -- `I(ww)`
736   * `exts` -- `d(s)`
737   * `truncd` -- `s(d)`
738   * `stosi` -- `I(ss)`
739   * `stoui` -- `I(ss)`
740   * `dtosi` -- `I(dd)`
741   * `dtoui` -- `I(dd)`
742   * `swtof` -- `F(ww)`
743   * `uwtof` -- `F(ww)`
744   * `sltof` -- `F(ll)`
745   * `ultof` -- `F(ll)`
747 Extending the precision of a temporary is done using the
748 `ext` family of instructions.  Because QBE types do not
749 precise the signedness (like in LLVM), extension instructions
750 exist to sign-extend and zero-extend a value.  For example,
751 `extsb` takes a word argument and sign-extend the 8
752 least-significant bits to a full word or long, depending on
753 the return type.
755 The instructions `exts` and `truncd` are provided to change
756 the precision of a floating point value.  When the double
757 argument of `truncd` cannot be represented as a
758 single-precision floating point, it is truncated towards
759 zero.
761 Converting between signed integers and floating points is done
762 using `stosi` (single to signed integer), `stoui` (single to
763 unsigned integer`, `dtosi` (double to signed integer), `dtoui`
764 (double to unsigned integer), `swtof` (signed word to float),
765 `uwtof` (unsigned word to float), `sltof` (signed long to
766 float) and `ultof` (unsigned long to float).
768 Because of <@ Subtyping >, there is no need to have an
769 instruction to lower the precision of an integer temporary.
771 ~ Cast and Copy
772 ~~~~~~~~~~~~~~~
774 The `cast` and `copy` instructions return the bits of their
775 argument verbatim.  However a `cast` will change an integer
776 into a floating point of the same width and vice versa.
778   * `cast` -- `wlsd(sdwl)`
779   * `copy` -- `T(T)`
781 Casts can be used to make bitwise operations on the
782 representation of floating point numbers.  For example
783 the following program will compute the opposite of the
784 single-precision floating point number `%f` into `%rs`.
786     %b0 =w cast %f
787     %b1 =w xor 2147483648, %b0  # flip the msb
788     %rs =s cast %b1
790 ~ Call
791 ~~~~~~
793     `bnf
794     CALL := [%IDENT '=' ABITY] 'call' VAL '(' (ARG), ')'
796     ARG :=
797         ABITY VAL  # Regular argument
798       | 'env' VAL  # Environment argument (first)
799       | '...'      # Variadic marker
801     ABITY := BASETY | :IDENT
803 The call instruction is special in several ways.  It is not
804 a three-address instruction and requires the type of all
805 its arguments to be given.  Also, the return type can be
806 either a base type or an aggregate type.  These specifics
807 are required to compile calls with C compatibility (i.e.,
808 to respect the ABI).
810 When an aggregate type is used as argument type or return
811 type, the value respectively passed or returned needs to be
812 a pointer to a memory location holding the value.  This is
813 because aggregate types are not first-class citizens of
814 the IL.
816 Unless the called function does not return a value, a
817 return temporary must be specified, even if it is never
818 used afterwards.
820 An environment parameter can be passed as first argument
821 using the `env` keyword.  The passed value must be a 64-bit
822 integer.  If the called function does not expect an environment
823 parameter, it will be safely discarded.  See the <@ Functions >
824 section for more information about environment parameters.
826 When the called function is variadic, there must be a `...`
827 marker separating the named and variadic arguments.
829 ~ Variadic
830 ~~~~~~~~~~
832 The `vastart` and `vaarg` instructions provide a portable
833 way to access the extra parameters of a variadic function.
835   * `vastart` -- `(m)`
836   * `vaarg` -- `T(mmmm)`
838 The `vastart` instruction initializes a *variable argument
839 list* used to access the extra parameters of the enclosing
840 variadic function.  It is safe to call it multiple times.
842 The `vaarg` instruction fetches the next argument from
843 a variable argument list.  It is currently limited to
844 fetching arguments that have a base type.  This instruction
845 is essentially effectful: calling it twice in a row will
846 return two consecutive arguments from the argument list.
848 Both instructions take a pointer to a variable argument
849 list as sole argument.  The size and alignment of variable
850 argument lists depend on the target used.  However, it
851 is possible to conservatively use the maximum size and
852 alignment required by all the targets.
854     type :valist = align 8 { 24 }  # For amd64_sysv
855     type :valist = align 8 { 32 }  # For arm64
857 The following example defines a variadic function adding
858 its first three arguments.
860     function s $add3(s %a, ...) {
861     @start
862             %ap =l alloc8 32
863             vastart %ap
864             %r =s call $vadd(s %a, l %ap)
865             ret %r
866     }
868     function s $vadd(s %a, l %ap) {
869     @start
870             %b =s vaarg %ap
871             %c =s vaarg %ap
872             %d =s add %a, %b
873             %e =s add %d, %c
874             ret %e
875     }
877 ~ Phi
878 ~~~~~
880     `bnf
881     PHI := %IDENT '=' BASETY 'phi' ( @IDENT VAL ),
883 First and foremost, phi instructions are NOT necessary when
884 writing a frontend to QBE.  One solution to avoid having to
885 deal with SSA form is to use stack allocated variables for
886 all source program variables and perform assignments and
887 lookups using <@ Memory > operations.  This is what LLVM
888 users typically do.
890 Another solution is to simply emit code that is not in SSA
891 form!  Contrary to LLVM, QBE is able to fixup programs not
892 in SSA form without requiring the boilerplate of loading
893 and storing in memory.  For example, the following program
894 will be correctly compiled by QBE.
896     @start
897             %x =w copy 100
898             %s =w copy 0
899     @loop
900             %s =w add %s, %x
901             %x =w sub %x, 1
902             jnz %x, @loop, @end
903     @end
904             ret %s
906 Now, if you want to know what phi instructions are and how
907 to use them in QBE, you can read the following.
909 Phi instructions are specific to SSA form.  In SSA form
910 values can only be assigned once, without phi instructions,
911 this requirement is too strong to represent many programs.
912 For example consider the following C program.
914     int f(int x) {
915             int y;
916             if (x)
917                     y = 1;
918             else
919                     y = 2;
920             return y;
921     }
923 The variable `y` is assigned twice, the solution to
924 translate it in SSA form is to insert a phi instruction.
926     @ifstmt
927             jnz %x, @ift, @iff
928     @ift
929             jmp @retstmt
930     @iff
931             jmp @retstmt
932     @retstmt
933             %y =w phi @ift 1, @iff 2
934             ret %y
936 Phi instructions return one of their arguments depending
937 on where the control came from.  In the example, `%y` is
938 set to 1 if the `@ift` branch is taken, or it is set to
939 2 otherwise.
941 An important remark about phi instructions is that QBE
942 assumes that if a variable is defined by a phi it respects
943 all the SSA invariants.  So it is critical to not use phi
944 instructions unless you know exactly what you are doing.
946 - 8. Instructions Index
947 -----------------------
949   * <@ Arithmetic and Bits >:
951       * `add`
952       * `and`
953       * `div`
954       * `mul`
955       * `or`
956       * `rem`
957       * `sar`
958       * `shl`
959       * `shr`
960       * `sub`
961       * `udiv`
962       * `urem`
963       * `xor`
965   * <@ Memory >:
967       * `alloc16`
968       * `alloc4`
969       * `alloc8`
970       * `loadd`
971       * `loadl`
972       * `loads`
973       * `loadsb`
974       * `loadsh`
975       * `loadsw`
976       * `loadub`
977       * `loaduh`
978       * `loaduw`
979       * `loadw`
980       * `storeb`
981       * `stored`
982       * `storeh`
983       * `storel`
984       * `stores`
985       * `storew`
987   * <@ Comparisons >:
989       * `ceqd`
990       * `ceql`
991       * `ceqs`
992       * `ceqw`
993       * `cged`
994       * `cges`
995       * `cgtd`
996       * `cgts`
997       * `cled`
998       * `cles`
999       * `cltd`
1000       * `clts`
1001       * `cned`
1002       * `cnel`
1003       * `cnes`
1004       * `cnew`
1005       * `cod`
1006       * `cos`
1007       * `csgel`
1008       * `csgew`
1009       * `csgtl`
1010       * `csgtw`
1011       * `cslel`
1012       * `cslew`
1013       * `csltl`
1014       * `csltw`
1015       * `cugel`
1016       * `cugew`
1017       * `cugtl`
1018       * `cugtw`
1019       * `culel`
1020       * `culew`
1021       * `cultl`
1022       * `cultw`
1023       * `cuod`
1024       * `cuos`
1026   * <@ Conversions >:
1028       * `dtosi`
1029       * `dtoui`
1030       * `exts`
1031       * `extsb`
1032       * `extsh`
1033       * `extsw`
1034       * `extub`
1035       * `extuh`
1036       * `extuw`
1037       * `sltof`
1038       * `ultof`
1039       * `stosi`
1040       * `stoui`
1041       * `swtof`
1042       * `uwtof`
1043       * `truncd`
1045   * <@ Cast and Copy > :
1047       * `cast`
1048       * `copy`
1050   * <@ Call >:
1052       * `call`
1054   * <@ Variadic >:
1056       * `vastart`
1057       * `vaarg`
1059   * <@ Phi >:
1061       * `phi`
1063   * <@ Jumps >:
1065       * `jmp`
1066       * `jnz`
1067       * `ret`