Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: native_client_sdk/src/doc/reference/pnacl-bitcode-manual.rst

Issue 725333002: Initial draft of PNaCl bitcode files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 ===============================
2 Contents Of PNaCl Bitcode Files
3 ===============================
4
5 .. contents::
6 :local:
7 :backlinks: none
8 :depth: 3
9
10
11 Introduction
12 ============
13
14 This document is a reference manual for the contents of PNaCl bitcode files. We
15 define bitcode files via three layers. The first layer is presented using
16 assembly language *PNaClAsm*, and defines the textual form of the bitcode
17 file. The textual form is then lowered to a sequence of :ref:`PNaCl
18 records<link_for_pnacl_records>`. The final layer applies abbreviations that
19 convert each PNaCl record into a corresponding sequence of bits.
20
21 .. image:: /images/PNaClBitcodeFlow.png
22
23 PNaClAsm uses a *static single assignment* (SSA) based representation that
24 requires generated results to have a single (assignment) source.
25
26 PNaClAsm focuses on the semantic content of the file, not the bit-encoding of
27 that content. However, it does provide annotations that allow one to specify how
28 the :ref:`abbreviations<link_for_abbreviations_section>` are used to convert
29 PNaCl records into the sequence of bits.
30
31 Each construct in PNaClAsm defines a corresponding :ref:`PNaCl
32 record<link_for_pnacl_records>`. A PNaCl bitcode file is simply a sequence of
33 PNaCl records. The goal of PNaClAsm is to make records easier to read, and not
34 to define a high-level user programming language.
35
36 PNaCl records are an abstract encoding of structured data, similar to XML. Like
37 XML, A PNaCl record has a notion of a tag (i.e. the first element in a record,
38 called a *code*). PNaCl records can be nested. Nesting is defined by a
39 corresponding :ref:`enter<link_for_enter_block_record_section>` and
40 :ref:`exit<link_for_exit_block_record_section>` block record.
41
42 These block records must be used like balanced parentheses to define the block
43 structure that is imposed on top of records. Each exit record must be preceded
44 by a corresponding enter record. Blocks can be nested by nesting enter/exit
45 records appropriately.
46
47 The *PNaCl bitcode writer* takes the sequence of records, defined by a PNaClAsm
48 program, and converts each record into a (variable-length) sequence of bits. The
49 output of each bit sequence is appended together. The resulting generated
50 sequence of bits is the contents of the PNaCl bitcode file.
51
52 For every kind of record, there is a method for converting records into bit
53 sequences. These methods correspond to a notion of
54 :ref:`abbreviations<link_for_abbreviations_section>`. Each abbreviation defines
55 a specific bit sequence conversion to be applied.
56
57 Abbreviations can be user defined, but there are also predefined defaults. All
58 user-specified abbreviations are included in the generated bitcode
59 file. Predefined defaults are not.
60
61 Each abbreviation defines how a record is converted to a bit sequence. The
62 :ref:`PNaCl translator<link_for_pnacl_translator>` uses these abbreviations
63 to convert the bit sequence back to the corresponding sequence of PNaCl records.
64 As a result, all records have an abbreviation (user or default) associated with
65 them.
66
67 Conceptually, abbreviations are used to define how to pack the contents of
68 records into bit sequences. The main reason for defining abbreviations is to
69 save space. The default abbreviations are simplistic and are intended to handle
70 all possible records. The default abbreviations do not really worry about being
71 efficient, in terms of the number of bits generated.
72
73 By separating the concepts of PNaCl records and abbreviations, the notion of
74 data compression is cleanly separated from semantic content. This allows
75 different use cases to decide how much effort should be spent on compressing
76 records.
77
78 For a JIT compiler that produces bitcode, little (if any) compression should be
79 applied. In fact, the API to the JIT may just be the records themselves. The
80 goal of a JIT is to perform the final translation to machine code as quickly as
81 possible.
82
83 On the other hand, when delivering across the web, one may want to compress the
84 sequence of bits considerably, to reduce costs in delivering web pages. Note
85 that :ref:`pnacl-compress<pnacl_compress>` is provided as part of the SDK to do
86 this job.
87
88 Data Model
89 ==========
90
91 The data model for PNaCl bitcode is fixed at little-endian ILP32: pointers are
92 32 bits in size. 64-bit integer types are also supported natively via the i64
93 type (for example, a front-end can generate these from the C/C++ type ``long
94 long``).
95
96 Integers are assumed to be modeled using two's complement. Floating point
97 support is fixed at :ref:`IEEE 754<c_cpp_floating_point>` 32-bit and 64-bit
98 values (float and double, respectively).
99
100 PNaCl Blocks
101 ============
102
103 Blocks are used to organize records in the bitcode file. The kinds of blocks
104 defined in PNaClAsm are:
105
106 Module block
107 A top-level block defining the program. The :ref:`module
108 block<link_for_module_block>` defines global information used by the program,
109 followed by function blocks defining the implementation of functions within
110 the program. All other blocks (listed below) must appear within a module
111 block.
112
113 Types block
114 The :ref:`types block<link_for_types_block_section>` defines the set of types
115 used by the program. All types used in the program must be defined in the
116 types block. These types consist of primitive types as well as high level
117 constructs such as vectors and function signatures.
118
119 Globals block
120 The :ref:`globals block<link_for_globals_block_section>` defines the set of
121 global addresses of global variables and constants used by the program. It
Jim Stichnoth 2014/11/18 02:24:42 "global addresses of global variables" - is the fi
Karl 2014/11/19 20:28:53 Yes. All global variables and addresses must appea
Jim Stichnoth 2014/11/19 21:12:00 Per our offline discussion, I think we both agree
122 also defines how each global (associated with the global address) is
123 initialized.
124
125 Valuesymtab block
126 The :ref:`valuesymtab block<link_for_valuesymtab_block_section>` defines
127 textual names for external function addresses.
128
129 Function block
130 Each function (implemented) in a program has its own :ref:`function
131 block<link_for_function_blocks_section>` that defines the implementation of
132 the corresponding function.
133
134 Constants block
135 Each implemented function that uses constants in its instructions defines a
136 :ref:`constants block<link_for_constants_block_section>`. Constants blocks
137 appear within the corresponding function block of the implemented function.
138
139 Abbreviations block
140 Defines global abbreviations that are used to compress PNaCl records. The
141 :ref:`abbreviations block<link_for_abbreviations_block_section>` is segmented
142 into multiple sections, one section for each kind of block. This block appears
143 at the beginning of the module block.
144
145 This section is only intended as a high-level discussion of blocks. Later
146 sections will dive more deeply into the constraints on how blocks must be laid
147 out. This section only presents the overall concepts of what kinds of data are
148 stored in each of the blocks.
149
150 A PNaCl program consists of a :ref:`header
151 record<link_for_header_record_section>` and a :ref:`module
152 block<link_for_module_block>`. The header record defines a sequence of bytes
153 uniquely identifying the file as a bitcode file. The module block defines the
154 program to run.
155
156 Each block, within a bitcode file, defines values. These values are associated
157 with IDs. Each type of block defines different kinds of IDs. The
158 :ref:`module<link_for_module_block>`,
159 :ref:`types<link_for_types_block_section>`,
160 :ref:`globals<link_for_globals_block_section>`, and
161 :ref:`abbreviations<link_for_abbreviations_block_section>` blocks define global
162 identifiers, and only a single instance can appear. The
163 :ref:`function<link_for_function_blocks_section>` and
164 :ref:`constant<link_for_constants_block_section>` blocks define local
165 identifiers, and can have multiple instances (one for each implemented
166 function).
167
168 The only records in the module block that define values, are :ref:`function
169 address<link_for_function_address_section>` records. Each function address
170 record defines a different function address, and the :ref:`type
171 signature<link_for_function_type>` associated with that function address.
172
173 Each :ref:`function block<link_for_function_blocks_section>` defines the
174 implementation of a single function. Each function block defines the
175 intermediate representation of the function, consisting of basic blocks and
176 instructions. If constants are used within instructions, they are defined in a
177 :ref:`constants block<link_for_constants_block_section>`, nested within the
178 corresponding function block.
179
180 All function blocks are associated with a corresponding function address. This
181 association is positional rather than explicit. That is, the Nth function block
182 in a module block corresponds to the Nth
183 :ref:`defining<link_for_function_address_section>` (rather than declared)
184 function address record in the module block.
185
186 Hence, within a function block, there is no explicit reference to the function
187 address the block defines. For readability, PNaClAsm uses the corresponding
188 function signature, associated with the corresponding function address record,
189 even though that data does not appear in the corresponding records.
190
191 .. _link_for_pnacl_records:
192
193 PNaCl Records
194 =============
195
196 A PNaCl record is a non-empty sequence of unsigned, 64-bit, integers. A record
197 is identified by the record *code*, which is the first element in the
198 sequence. Record codes are unique within a specific kind of block, but are not
199 necessarily unique across different kinds of blocks. The record code acts as the
200 variant discriminator (i.e. tag) within a block, to identify what kind of record
201 it is.
202
203 Record codes that are local to a specific kind of block are small values
204 (starting from zero). In an ideal world, they would be a consecutive sequence of
205 integers, starting at zero. However, the reality is that PNaCl records evolved
206 over time (and actually started as `LLVM records
207 <http://llvm.org/docs/BitCodeFormat.html>`_). For backward compatibility,
208 obsolete numbers have not been reused, leaving gaps in the actual record code
209 values used.
210
211 Global record codes are record codes that have the same meaning in multiple
212 kinds of blocks. To separate global record codes from local record codes, large
213 values are used. Currently there are four :ref:`global record
214 codes<link_for_global_record_codes>`. To make these cases clear, and to leave
215 ample room for future growth in PNaClAsm, these special records have record
216 codes close to the value 2\ :sup:`16`\ . Note: Well-formed PNaCl bitcode files
217 do not have record codes >= 2\ :sup:`16`\ .
218
219 A PNaCl record is denoted as follows: ::
220
221 a: <v0, v1, ... , vN>
222
223 The value ``v0`` is the record code. The remaining values, ``v1`` through
224 ``vN``, are parameters that fill in additional information needed by the
225 construct it represents. All records must have a record code. Hence, empty PNaCl
226 records are not allowed. ``a`` is the index to the abbreviation used to convert
227 the record to a bit sequence.
228
229 While most records (for a given record code) have the same length, it is not
230 true of all record codes. Some record codes can have arbitrary length. In
231 particular, function type signatures, call instructions, phi nodes, switch
Jim Stichnoth 2014/11/18 02:24:42 phi instructions? (instead of nodes)
Karl 2014/11/19 20:28:53 Done.
232 instructions, and global variable initialization records all have variable
233 length. The expected length is predefined and part of the PNaClAsm language. See
234 the corresponding construct (associated with the record) to determine the
235 expected length.
236
237 The ``PNaCl bitstream writer``, which converts records to bit sequences, does
Jim Stichnoth 2014/11/18 02:24:43 The `` `` formatting doesn't look right in the htm
Karl 2014/11/19 20:28:53 Done.
238 this by writing out the abbreviation index used to encode the record, followed
239 by the contents of the record. The details of this are left to the section on
240 :ref:`abbreviations<link_for_abbreviations_section>`. However, at the record
241 level, one important aspect of this appears in ::ref:`block
Jim Stichnoth 2014/11/18 02:24:42 ::ref: ==> :ref:
Karl 2014/11/19 20:28:52 Done.
242 enter<link_for_enter_block_record_section>` records. These records must define
243 how many bits are required to hold abbreviation indices associated with records
244 of that block.
245
246 .. _link_for_default_abbreviations:
247
248 Default Abbreviations
249 =====================
250
251 There are 4 predefined (default) abbreviation indices, used as the default
252 abbreviations for PNaCl records. They are:
253
254 0
255 Abbreviation index for the abbreviation used to bit-encode an exit block
256 record.
257
258 1
259 Abbreviation index for the abbreviation used to bit-encode an enter block
260 record.
261
262 2
263 Abbreviation index for the abbreviation used to bit-encode a user-defined
264 abbreviation. Note: User defined abbreviations are also encoded as records,
Jim Stichnoth 2014/11/18 02:24:42 "User defined" ==> "User-defined"
Karl 2014/11/19 20:28:52 Done.
265 and hence need an abbreviation index to bit-encode them.
266
267 3
268 Abbreviation index for the default abbreviation to bit-encode all other
269 records in the bitcode file.
270
271 A block may, in addition, define a list of block specific, user-defined,
272 abbreviations (of length ``U``). The number of bits ``B`` specified for an enter
273 record must be sufficiently large such that::
274
275 2**B >= U + 4
276
277 In addition, the upper limit for ``B`` is ``16``.
278
279 PNaClAsm requires specifying the number of bits needed to read abbreviations as
280 part of the enter block record. This allows the PNaCl bitcode reader/writer to
281 use the specified number of bits to encode abbreviation indices.
282
283 PNaCl Identifiers
284 =================
285
286 A program is defined by a :ref:`module block<link_for_module_block>`. Blocks can
287 be nested within other blocks, including the module block. Each block defines a
288 sequence of records.
289
290 Most of the records, within a block, also define unique values. Each unique
291 value is given a corresponding unique identifier (i.e. *ID*). In PNaClAsm, each
292 kind of block defines its own kind of identifiers. The names of these
293 identifiers are defined by concatenating a prefix character (``'@'`` or
294 ``'%'``), the kind of block (a single character), and a suffix index. The suffix
295 index is defined by the positional location of the defined value within the
296 records of the corresponding block. The indices are all zero based, meaning that
297 the first defined value (within a block) is defined using index 0.
298
299 Identifiers are categorized into two types, *local* and *global*. Local
300 identifiers are identifiers that are associated with the implementation of a
301 single function. In that sense, they are local to the block they appear in.
302
303 All other identifiers are global, and can appear in multiple blocks. This split
304 is intentional. Global identifiers are used by multiple functions, and therefore
305 must be known in all function implementations. Local identifiers only apply to a
306 single function, and can be reused between functions. The :ref:`PNaCl
307 translator<link_for_pnacl_translator>` uses this separation to parallelize the
308 compilation of functions.
309
310 Note that local abbreviation identifiers are unique to the block they appear
311 in. Global abbreviation identifiers are only unique to the block type they are
312 defined for. Different block types can reuse global abbreviation identifiers.
313
314 Global identifiers use the prefix character ``'@'`` while local identifiers use
315 the prefix character ``'%'``.
316
317 Note that by using positional location to define identifiers (within a block),
318 the values defined in PNaCl bitcode files need not be explicitly included in the
319 bitcode file. Rather, they are inferred by the (ordered) position of the record
320 in the block. This is also intentional. It is used to reduce the amount of data
321 that must be (explicitly) passed to the :ref:`PNaCl
322 translator<link_for_pnacl_translator>`, and downloaded from the cloud.
Jim Stichnoth 2014/11/18 02:24:42 "the cloud"? yuck...
Karl 2014/11/19 20:28:52 How about simply "when downloaded into Chrome".
323
324 In general, most of the records within blocks are assumed to be topologically
325 sorted, putting value definitions before their uses. This implies that records
326 do not need to encode data if they can deduce the corresponding information from
327 their uses.
328
329 The most common use of this is that many instructions use the type of their
330 operands to determine the type of the instruction. Again, this is
331 intentional. It allows less information to be stored.
332
333 However, for function blocks (which define instructions), a topological sort may
334 not exist. Loop carried value dependencies simply do not allow topologically
335 sorting. To deal with this, function blocks have a notion of (instruction value)
336 :ref:`forward type
337 declarations<link_for_forward_type_declaration_section>`. These declarations
338 must appear before any of the uses of that value, if the (instruction) value is
339 defined later in the function than its first use.
340
341 The kinds of identifiers used in PNaClAsm are:
342
343 @a
344 Global abbreviation identifier.
345
346 %a
347 Local abbreviation identifier.
348
349 %b
350 Function basic block identifier.
351
352 %c
353 Function constant identifier.
354
355 @f
356 Global function address identifier.
357
358 @g
359 Global variable/constant address identifier.
360
361 %p
362 Function parameter identifier.
363
364 @t
365 Global type identifier.
366
367 %v
368 Value generated by an instruction in a function block.
369
370
371 Conventions For Describing Records
372 ==================================
373
374 PNaClAsm is the textual representation of :ref:`PNaCl
375 records<link_for_pnacl_records>`. Each PNaCl record is described by a
376 corresponding PNaClAsm construct. These constructs are described using syntax
377 rules, and semantics on how they are converted to records. Along with the rules,
378 is a notion of :ref:`global state<link_for_global_state_section>`. The global
379 state is updated by syntax rules. The purpose of the global state is to track
380 positional dependencies between records.
381
382 For each PNaCl construct, we define multiple subsections. The **Syntax**
383 subsection defines a syntax rule for the construct. The **Record** subsection
Jim Stichnoth 2014/11/18 02:24:42 I think most instances of "subsection" can be chan
Karl 2014/11/19 20:28:52 Done.
384 defines the corresponding record associated with the syntax rule. The
385 **Semantics** subsection describes the semantics associated with the record, in
386 terms of data within the global state and the corresponding syntax. It also
387 includes other high-level semantics, when appropriate.
388
389 The **Constraints** subsection (if present) defines any constraints associated
390 with the construct, including the global state. The **Updates** subsection (if
391 present) defines how the global state is updated when the construct is
392 processed. The **Examples** subsection gives one or more examples of using the
393 corresponding PNaClAsm construct.
394
395 Some semantics subsections use functions to compute values. The meaning of
396 functions can be found in :ref:`support
397 functions<link_for_support_functions_section>`.
398
399 The syntax rule may include the
400 :ref:`abbreviation<link_for_abbreviations_section>` to use, when converting to a
401 bit-sequence. These abbreviations, if allowed, are at the end of the construct,
402 and enclosed in ``<`` and ``>`` brackets. These abbreviations are optional in
403 the syntax, and can be omitted. If they are used, the abbreviation brackets are
404 part of the actual syntax of the construct. If the abbreviation is omitted, the
405 default abbreviation index is used. To make it clear that abbreviations are
406 optional, syntax rules separate abbreviations using plenty of whitespace.
407
408 Within a syntax rule, lowercase characters are literal values. Sequences of
Jim Stichnoth 2014/11/18 02:24:42 "lower case" for consistency with other uses, incl
Karl 2014/11/19 20:28:52 Done.
409 upper case alphanumeric characters are named values. If we mix lower and upper
410 case letters within a name appearing in a syntax rule, the lower case letters
411 are literal while the upper case sequence of alphanumeric characters denote rule
412 specific values. The valid values for each of these names will be defined in
413 the corresponding semantics and constraints subsections.
414
415 For example, consider the following syntax rule::
416
417 %vN = add T O1, O2; <A>
418
419 This rule defines a PNaClAsm add instruction. This construct defines an
420 instruction that adds two values (``O1`` and ``O2``) to generate instruction
421 value ``%vN``. The types of the arguments, and the result, are all of type
422 ``T``. If abbreviation ID ``A`` is present, the record is encoded using that
423 abbreviation. Otherwise the corresponding default abbreviation (3) is used.
Jim Stichnoth 2014/11/18 02:24:42 Can you briefly where the "3" comes from? E.g.
Karl 2014/11/19 20:28:53 Replaces with reference to "default abbreviation i
424
425 To be concrete, the syntactic rule above defines the structure of the following
426 PNaClAsm examples::
427
428 %v10 = add i32 %v1, %v2; <@a5>
429 %v11 = add i32 %v10, %v3;
430
431 In addition to specifying the syntax, each syntax rule can also also specify the
432 contents of the corresponding record in the corresponding record subsection. In
433 simple cases, the elements of the corresponding record are predefined (literal)
434 constants. Otherwise the record element is an identifier from another subsection
435 associated with the construct.
436
437 Factorial Example
438 =================
439
440 This section provides a simple example of a PNaCl bitcode file. Its contents
441 describe a bitcode file that only defines a function to compute the factorial
442 value of a number.
443
444 In C, the factorial function can be defined as::
445
446 int fact(int n) {
447 if (n == 1) return 1;
448 return n * fact(n-1);
449 }
450
451 Compiling this into a PNaCl bitcode file, and dumping out its contents with
452 utility :ref:`pnacl-bcdis<pnacl-bcdis>`, the corresponding output is::
453
454 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, 88, 69)
455 | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2
456 | 0> |
457 16:0|1: <65535, 8, 2> |module { // BlockID = 8
458 24:0| 3: <1, 1> | version 1;
459 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID = 0
460 36:0| 0: <65534> | }
461 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
462 48:0| 3: <1, 4> | count 4;
463 50:4| 3: <7, 32> | @t0 = i32;
464 53:6| 3: <2> | @t1 = void;
465 55:4| 3: <21, 0, 0, 0> | @t2 = i32 (i32);
466 59:4| 3: <7, 1> | @t3 = i1;
467 62:0| 0: <65534> | }
468 64:0| 3: <8, 2, 0, 0, 0> | define external i32 @f0(i32);
469 68:6| 1: <65535, 19, 2> | globals { // BlockID = 19
470 76:0| 3: <5, 0> | count 0;
471 78:4| 0: <65534> | }
472 80:0| 1: <65535, 14, 2> | valuesymtab { // BlockID = 14
473 88:0| 3: <1, 0, 102, 97, 99, | @f0 : "fact";
474 | 116> |
475 96:4| 0: <65534> | }
476 100:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0) {
477 | | // BlockID = 12
478 108:0| 3: <1, 3> | blocks 3;
479 110:4| 1: <65535, 11, 2> | constants { // BlockID = 11
480 120:0| 3: <1, 0> | i32:
481 122:4| 3: <4, 2> | %c0 = i32 1;
482 125:0| 0: <65534> | }
483 | | %b0:
484 128:0| 3: <28, 2, 1, 32> | %v0 = icmp eq i32 %p0, %c0;
485 132:6| 3: <11, 1, 2, 1> | br i1 %v0, label %b1, label %b2;
486 | | %b1:
487 136:6| 3: <10, 2> | ret i32 %c0;
488 | | %b2:
489 139:2| 3: <2, 3, 2, 1> | %v1 = sub i32 %p0, %c0;
490 143:2| 3: <34, 0, 5, 1> | %v2 = call i32 @f0(i32 %v1);
491 148:0| 3: <2, 5, 1, 2> | %v3 = mul i32 %p0, %v2;
492 152:0| 3: <10, 1> | ret i32 %v3;
493 154:4| 0: <65534> | }
494 156:0|0: <65534> |}
495
496 Note that there are three columns in this output. The first column contains the
497 bit positions of the records within the bitcode file. The second column contains
498 the sequence of records within the bitcode file. The third column contains the
499 corresponding PNaClAsm program.
500
501 Bit positions are defined by a pair ``B:N``. ``B`` is the number of bytes, while
502 ``N`` is the bit offset within the ``B``-th byte. Hence, the bit position (in
503 bits) is::
504
505 B*8 + N
506
507 Hence, the first record is at bit offset ``0`` (``0*8+0``). The second record is
508 at bit offset ``128`` (``16*8+0``). The third record is at bit offset ``192``
509 (``24*8+0``). The fourth record is at bit offset ``212`` (``26*8+4``).
510
511 The :ref:`header record<link_for_header_record_section>` is a sequence of 16
512 bytes, defining the contents of the first 16 bytes of the bitcode file. These
513 bytes never change, and are expected for all version 2, PNaCl bitcode files. The
514 first four bytes define the magic number of the file, i.e. 'PEXE'. All PEXE
515 bitcode files begin with these four bytes.
516
517 All but the header record has an abbreviation index associated with it. Since no
518 user-defined abbreviations are provided, all records were converted to
519 bit sequences using default abbreviations.
520
521 The types block (starting at bit address ``40:0``), defines 4 types: ``i1``,
522 ``i32``, ``void``, and function signature ``i32 (i32)``.
523
524 Bit address ``64:0`` declares the factorial function address ``@f0``, and its
525 corresponding type signature. Bit address ``88:0`` associates the name ``fact``
526 with function address ``@f0``.
527
528 Bit address ``100:0`` defines the function block that implements function
529 ``fact``. The entry point is ``%b0`` (at bit address ``128:0``). It uses the
530 32-bit integer constant ``1`` (defined at bit addresses ``122:4``). Bit address
531 ``128:0`` defines an equality comparison of the argument ``%p0`` with ``1``
532 (constant ``%c0``). Bit address ``132:6`` defines a conditional branch. If the
533 result of the previous comparison (``%v0``) is true, the program will branch to
534 block ``%b1``. Otherwise it will branch to block ``%b2``.
535
536 Bit address ``136:6`` returns constant ``1`` (``%c0``) when the input parameter
537 is 1. Instructions between bit address ``139:2`` and ``154:4`` compute and
538 return ``n * fact(n-1)``.
539
540 Road Map
541 ========
542
543 At this point, this document transitions from basic concepts to the details
544 of how records should be formatted. This section defines the road map to
545 the remaining sections in this document.
546
547 Many records have implicit information associated with them, and must be
548 maintained across records. :ref:`Global state<link_for_global_state_section>`
549 describes how this implicit information is modeled. In addition, there are
550 various :ref:`support functions<link_for_support_functions_section>` that are
551 used to define the semantics of records, and how they update the global state.
552
553 There are just a handful of global records (records that either don't appear in
554 any block, or can appear in all blocks). :ref:`Global
555 records<link_for_global_record_codes>` describes these records. This includes
556 the block delimiter records :ref:`enter<link_for_enter_block_record_section>`
557 and :ref:`exit<link_for_exit_block_record_section>` that define block
558 boundaries.
559
560 PNaClAsm is a strongly typed language, and most block values are typed.
561 :ref:`types<link_for_types_block_section>` describes the set of legal types, and
562 how to define types.
563
564 Global variables and their initializers are presented in the :ref:`globals
565 block<link_for_globals_block_section>`. :ref:`Function
566 addresses<link_for_function_address_section>` are part of the :ref:`module
567 block<link_for_module_block>`, but must be defined before any global variables.
568
569 Names to be associated with global variables and function addresses, are defined
570 in the :ref:`valuesymtab block<link_for_valuesymtab_block_section>`, and must
571 appear after the :ref:`globals block<link_for_globals_block_section>`, but
572 before any :ref:`function definition<link_for_function_blocks_section>`.
573
574 The :ref:`module block<link_for_module_block>` is the top-most block, and all
575 other blocks must appear within the module block. The module block defines the
576 executable in the bitcode file.
577
578 Constants used within a :ref:`function
579 definition<link_for_function_blocks_section>` must be defined using a
580 :ref:`constants block<link_for_constants_block_section>`. Each function
581 definition is defined by a :ref:`function
582 block<link_for_function_blocks_section>` and constant blocks can only appear
583 within function blocks. Constants defined within a constant block can only be
584 used in the enclosing function block.
585
586 Function definitions are defined by a sequence of instructions. There are
587 several types of instructions.
588
589 A :ref:`terminator instruction<link_for_terminator_instruction_section>` is the
590 last instruction in a :ref:`basic block<link_for_function_blocks_section>`, and
591 is a branch/return instruction.
Jim Stichnoth 2014/11/18 02:24:42 What about unreachable? That's kind of distinct f
Karl 2014/11/19 20:28:52 Done.
592
593 There are :ref:`integer<link_for_integer_binary_instructions>` and
594 :ref:`floating point<link_for_floating_point_binary_instructions>` binary
595 operations. Integer binary instructions include both arithmetic and logical
596 operations. Floating point instructions define arithmetic operations.
597
598 There are also :ref:`memory
599 access<link_for_memory_creation_and_access_instructions>` instructions that
600 allow one to load and store values. That section also includes how to define
601 local variables using the :ref:`alloca
602 instruction<link_for_alloca_instruction>`.
603
604 One can also convert integer and floating point values using :ref:`conversion
605 instructions<link_for_conversion_instructions>`.
606
607 :ref:`Comparison instructions<link_for_compare_instructions>`
608 allow you to compare values.
609
610 :ref:`Vector instructions<link_for_vector_instructions>` allow you to build and
611 update vectors. Corresponding :ref:`intrinsic
612 functions<link_for_intrinsic_functions_section>`, as well as
613 :ref:`integer<link_for_integer_binary_instructions>` and :ref:`floating
614 point<link_for_floating_point_binary_instructions>` binary instructions allow
615 you to apply operations to vectors.
616
617 In addition, :ref:`other instructions<link_for_other_pnaclasm_instructions>` are
618 available. This includes function and procedure calls.
619
620 There are also :ref:`memory
621 alignment<link_for_memory_blocks_and_alignment_section>` issues that should be
622 considered for global and local variables, as well as load and store
623 instructions.
624
625 Finally, how to pack records is described in the
626 :ref:`abbreviations<link_for_abbreviations_section>` section.
627
628 .. _link_for_global_state_section:
629
630 Global State
631 ============
632
633 This section describes the global state associated with PNaClAsm. It is used to
634 define contextual data that is carried between records.
635
636 In particular, PNaClAsm is a strongly typed language, and hence, we must track
637 the type associated with values. Subsection :ref:`link_to_typing_functions`
638 describes the functions used to maintain typing information associated with
639 values.
640
641 Values are implicitly ordered within a block, and the indices associated with
642 the values do not appear in records. Rather, ID counters are used to figure out
643 what corresponding ID name is associated with a value generating record.
644 Subsection :ref:`link_to_ID_Counters` defines counters maintained in the global
645 state.
646
647 In several blocks, one of the first records in the block defines how many values
648 are defined in in the block. The main purpose of these counts is to communicate
649 to the :ref:`PNaCl translator<link_for_pnacl_translator>` space requirements, or
650 a limit so that it can detect bad references to values. Subsection
651 :ref:`link_for_Size_Variables` defines variables that hold size definitions in
652 the corresponding records.
653
654 Finally, the function and constants block contain implicit context between
655 records in those blocks. Subsection :ref:`link_to_Other_Variables` defines the
656 variables that contain this implicit context.
657
658 .. _link_to_typing_functions:
659
660 Typing Functions
661 ----------------
662
663 Associated with most identifiers is a type. This type defines what type the
664 corresponding value has. It is defined by the (initially empty) map::
665
666 TypeOf: ID -> Type
667
668 For each type in the :ref:`types block<link_for_types_block_section>`, a
669 corresponding inverse map::
670
671 TypeID: Type -> ID
672
673 is maintained to convert syntactic types to the corresponding type ID.
674
675 Note: This document assumes that map ``TypeID`` is automatically maintained
676 during updates to map ``TypeOf`` (when given a type ``ID``). Hence, *Updates*
677 subsections will not contain assignments to this map.
678
679 Associated with each function identifier is its :ref:`type
680 signature<link_for_function_type>`. This is different than the type of the
681 function identifier, since function identifiers represent the function address
682 which is a pointer (and pointers are always implemented as a 32-bit integer
683 following the ILP32 data model).
684
685 Function type signatures are maintained using::
686
687 TypeOfFcn: ID -> Type
688
689 In addition, if a function address has an implementing block, there is a
690 corresponding implementation associated with the function address. To indicate
691 which function addresses have implementations, we use the set::
692
693 DefiningFcnIDs: set(ID)
694
695 .. _link_to_ID_Counters:
696
697 ID Counters
698 -----------
699
700 Each block defines one or more kinds of values. Value indices are generated
701 sequentially, starting at zero. To capture this, the following counters are
702 defined:
703
704 NumTypes
705 The number of types defined so far (in the :ref:`types
706 block<link_for_types_block_section>`).
707
708 NumFuncAddresses
709 The number of function addresses defined so far (in the :ref:`module
710 block<link_for_module_block>`).
711
712 NumGlobalAddresses
713 The number of global variable/constant addresses defined so far (in the
714 :ref:`globals block<link_for_globals_block_section>`).
715
716 NumParams
717 The number of parameters defined for a function. Note: Unlike other counters,
718 this value is set once, at the beginning of the corresponding :ref:`function
719 block<link_for_function_blocks_section>`, based on the type signature
720 associated with the function.
721
722 NumFcnConsts
723 The number of constants defined in a function so far (in the corresponding
724 nested :ref:`constants block<link_for_constants_block_section>`).
725
726 NumBasicBlocks
727 The number of basic blocks defined so far (within a :ref:`function
728 block<link_for_function_blocks_section>`).
729
730 NumValuedInsts
731 The number of instructions, generating values, defined so far (within a
732 :ref:`function block<link_for_function_blocks_section>`).
733
734 .. _link_for_Size_Variables:
735
736 Size Variables
737 --------------
738
739 A number of blocks define expected sizes of constructs. These sizes are recorded
740 in the following size variables:
741
742 ExpectedBasicBlocks
743 The expected :ref:`number of basic blocks<link_for_basic_blocks_count>` within
744 a function implementation.
745
746 ExpectedTypes
747 The expected :ref:`number of types<link_for_types_count_record>` defined in
748 the types block.
749
750 ExpectedGlobals
751 The expected :ref:`number of global variable/constant
752 addresses<link_for_globals_count_record>` in the globals block.
753
754 ExpectedInitializers
755 The expected :ref:`number of initializers<link_for_compound_initializer>` for
756 a global variable/constant address in the globals block.
757
758 It is assumed that the corresponding :ref:`ID counters<link_to_ID_counters>` are
759 always smaller than the corresponding size variables (except
760 ExpectedInitializers). That is:
761
762 NumBasicBlocks < ExpectedBasicBlocks
Jim Stichnoth 2014/11/18 02:24:42 Maybe this paragraph should have some special form
Karl 2014/11/19 20:28:53 Oops, I forgot the "::" after "That is". Fixing.
763 NumTypes < ExpectedTypes
764 NumGlobalAddresses < ExpectedGlobals
765
766 .. _link_to_Other_Variables:
767
768 Other Variables
769 ---------------
770
771 EnclosingFcnID
772 The function ID of the function block being processed.
773
Jim Stichnoth 2014/11/18 02:24:42 Remove this blank line for consistency
Karl 2014/11/19 20:28:52 Actually, the consistent form is to leave a blank
774 ConstantsSetType
775
Jim Stichnoth 2014/11/18 02:24:42 Remove this blank line
Karl 2014/11/19 20:28:53 Done.
776 Holds the type associated with the last :ref:`set type
777 record<link_for_constants_set_type_record>` in the constants block. Note: at
778 the beginning of each constants block, this variable is set to type void.
779
780 .. _link_for_global_record_codes:
781
782 Global Records
783 ==============
784
785 Global records are records that can appear in any block. These records have
786 the same meaning in multiple kinds of blocks.
787
788 There are four global PNaCl records, each having its own record code. These
789 global records are:
790
791 Header
792 The :ref:`header record<link_for_header_record_section>` is the first record
793 of a PNaCl bitcode file, and identifies the file's magic number, as well as
794 the bitcode version it uses. The record defines the sequence of bytes that
795 make up the header and uniquely identifies the file as a PNaCl bitcode file.
796
797 Enter
798 An :ref:`enter record<link_for_enter_block_record_section>` defines the
799 beginning of a block. Since blocks can be nested, one can appear inside other
800 blocks, as well as at the top level.
801
802 Exit
803 An :ref:`exit record<link_for_exit_block_record_section>` defines the end of a
804 block. Hence, it must appear in every block, to end the block.
805
806 Abbreviation
807 An :ref:`abbreviation record<link_for_abbreviation_record>` defines a
808 user-defined abbreviation to be applied to records within blocks.
809 Abbreviation records appearing in the abbreviations block define global
810 abbreviations. All other abbreviations are local to the block they appear in,
811 and can only be used in that block.
812
813 All global records can't have user-defined abbreviations associated with
814 them. The :ref:`default abbreviation<link_for_default_abbreviations>` is always
815 used.
816
817 .. _link_for_header_record_section:
818
819 Header Record
820 -------------
821
822 The header record must be the first record in the file. It is the only record in
823 the bitcode file that doesn't have a corresponding construct in PNaClAsm. In
824 addition, no abbreviation index is associated with it.
825
826 **Syntax**:
827
828 There is no syntax for header records in PNaClAsm.
829
830 **Record**::
831
832 <65532, 80, 69, 88, 69, 1, 0, 8, 0, 17, 0, 4, 0, 2, 0, 0, 0>
833
834 **Semantics**:
835
836 The header record defines the initial sequence of bytes that must appear at the
837 beginning of all (PNaCl bitcode version 2) files. That sequence is the list of
838 bytes inside the record (excluding the record code). As such, it uniquely
839 identifies all PNaCl bitcode files.
840
841 **Examples**::
842
843 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, 88, 69)
844 | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2
845 | 0> |
846
847 .. _link_for_enter_block_record_section:
848
849 Enter Block Record
850 ------------------
851
852 Block records can be top-level, as well as nested in other blocks. Blocks must
853 begin with an *enter* record, and end with an
854 :ref:`exit<link_for_exit_block_record_section>` record.
855
856 **Syntax**::
857
858 N { <B>
859
860 **Record**::
861
862 1: <65535, ID, B>
863
864 **Semantics**:
865
866 Enter block records define the beginning of a block. ``B``, if present, is the
867 number of bits needed to represent all possible abbreviation indices used within
868 the block. If omitted, ``B=2`` is assumed.
869
870 The block ``ID`` value is dependent on the name ``N``. Valid names and correspon ding
Jim Stichnoth 2014/11/17 18:54:56 Find all 80-char violations: grep -n '^.\{81\}'
Karl 2014/11/17 20:17:28 Done.
871 ``BlockID`` values are defined as follows:
872
873 ============= ========
874 N Block ID
875 ============= ========
876 abbreviations 0
877 constants 11
878 function 12
879 globals 19
880 module 8
881 types 17
882 valuesymtab 14
883 ============= ========
884
885 Note: For readability, PNaClAsm defines a more readable form of a function block
886 enter record. See :ref:`function blocks<link_for_function_blocks_section>` for
887 more details.
888
889 **Examples**::
890
891 16:0|1: <65535, 8, 2> |module { // BlockID = 8
892 24:0| 3: <1, 1> | version 1;
893 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID = 0
894 36:0| 0: <65534> | }
895 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
896 48:0| 3: <1, 2> | count 2;
897 50:4| 3: <2> | @t0 = void;
898 52:2| 3: <21, 0, 0> | @t1 = void ();
899 55:4| 0: <65534> | }
900 56:0| 3: <8, 1, 0, 1, 0> | declare external void @f0();
901 60:6| 1: <65535, 19, 2> | globals { // BlockID = 19
902 68:0| 3: <5, 0> | count 0;
903 70:4| 0: <65534> | }
904 72:0|0: <65534> |}
905
906 .. _link_for_exit_block_record_section:
907
908 Exit Block Record
909 -----------------
910
911 Block records can be top-level, as well as nested, records. Blocks must begin
912 with an :ref:`enter<link_for_enter_block_record_section>` record, and end with a n
913 *exit* record.
914
915 **Syntax**::
916
917 }
918
919 **Record**::
920
921 0: <65534>
922
923 **Semantics**:
924
925 All exit records are identical, no matter what block they are ending. An exit
926 record defines the end of the block.
927
928 **Examples**::
929
930 16:0|1: <65535, 8, 2> |module { // BlockID = 8
931 24:0| 3: <1, 1> | version 1;
932 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID = 0
933 36:0| 0: <65534> | }
934 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
935 48:0| 3: <1, 2> | count 2;
936 50:4| 3: <2> | @t0 = void;
937 52:2| 3: <21, 0, 0> | @t1 = void ();
938 55:4| 0: <65534> | }
939 56:0| 3: <8, 1, 0, 1, 0> | declare external void @f0();
940 60:6| 1: <65535, 19, 2> | globals { // BlockID = 19
941 68:0| 3: <5, 0> | count 0;
942 70:4| 0: <65534> | }
943 72:0|0: <65534> |}
944
945 .. _link_for_abbreviation_record:
946
947 Abbreviation Record
948 -------------------
949
950 Abbreviation records define abbreviations. See
951 :ref:`abbreviations<link_for_abbreviations_section>` for details on how abbrevia tions should be
952 written. This section only presents the mechanical details for converting
953 an abbreviation into a PNaCl record.
954
955 **Syntax**::
956
957 A = abbrev <E1, ... , EM>;
958
959 **Record**::
960
961 2: <65533, M, EE1, ... , EEM>
962
963 **Semantics**:
964
965 Defines an abbreviation ``A`` as the sequence of encodings ``E1`` through
966 ``EM``. If the abbreviation appears within the :ref:`abbreviations
967 block<link_for_abbreviations_block_section>`, ``A`` must be a global
968 abbreviation. Otherwise, ``A`` must be a local abbreviation.
969
970 Abbreviations within a block (or a section within the abbreviations block), must
971 be enumerated in order, starting at index ``0``.
972
973 Valid encodings ``Ei``, and the corresponding sequence of (unsigned) integers
974 ``EEi``, ( for ``1 <= i <= M``) are defined by the following table:
975
976 ========= ======= ============================================================== =
977 Ei EEi Form
978 ========= ======= ============================================================== =
979 C 1, C Literal C in corresponding position in record.
980 fixed(N) 0, 1, N Encode value as a fixed sequence of N bits.
981 vbr(N) 0, 2, N Encode value using a variable bit rate of N.
982 char6 0, 4 Encode value as 6-bit char containing characters [a-zA-Z0-9._] .
983 array(EM) 0, 3 Allow zero or more of the enclosed encoding.
984 ========= ======= ============================================================== =
985
986 Notationally, Array(EM) encloses the encoding EM, and must appear at the end of
Jim Stichnoth 2014/11/18 02:24:42 Should Array(EM) be formatted consistently with ``
Karl 2014/11/19 20:28:52 Done.
987 the abbreviation. When encoding ``array(EM), array and EM`` are the last two
988 entries in an abbreviation, the trailing EM is omitted from the syntax, since it
989 is redundant.
990
991 **Examples**::
992
993 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, 88, 69)
994 | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2
995 | 0> |
996 16:0|1: <65535, 8, 2> |module { // BlockID = 8
997 24:0| 3: <1, 1> | version 1;
998 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID = 0
999 36:0| 1: <1, 14> | valuesymtab:
1000 38:4| 2: <65533, 4, 0, 1, 3, 0,| @a0 = abbrev <fixed(3), vbr(8),
1001 | 2, 8, 0, 3, 0, 1, 8> | array(fixed(8))>;
1002 43:2| 2: <65533, 4, 1, 1, 0, 2,| @a1 = abbrev <1, vbr(8),
1003 | 8, 0, 3, 0, 1, 7> | array(fixed(7))>;
1004 48:0| 2: <65533, 4, 1, 1, 0, 2,| @a2 = abbrev <1, vbr(8),
1005 | 8, 0, 3, 0, 4> | array(char6)>;
1006 52:1| 2: <65533, 4, 1, 2, 0, 2,| @a3 = abbrev <2, vbr(8),
1007 | 8, 0, 3, 0, 4> | array(char6)>;
1008 56:2| 1: <1, 11> | constants:
1009 58:6| 2: <65533, 2, 1, 1, 0, 1,| @a0 = abbrev <1, fixed(2)>;
1010 | 2> |
1011 61:7| 2: <65533, 2, 1, 4, 0, 2,| @a1 = abbrev <4, vbr(8)>;
1012 | 8> |
1013 65:0| 2: <65533, 2, 1, 4, 1, 0>| @a2 = abbrev <4, 0>;
1014 68:1| 2: <65533, 2, 1, 6, 0, 2,| @a3 = abbrev <6, vbr(8)>;
1015 | 8> |
1016 71:2| 1: <1, 12> | function:
1017 73:6| 2: <65533, 4, 1, 20, 0, | @a0 = abbrev <20, vbr(6), vbr(4),
1018 | 2, 6, 0, 2, 4, 0, 2, | vbr(4)>;
1019 | 4> |
1020 79:1| 2: <65533, 4, 1, 2, 0, 2,| @a1 = abbrev <2, vbr(6), vbr(6),
1021 | 6, 0, 2, 6, 0, 1, 4> | fixed(4)>;
1022 84:4| 2: <65533, 4, 1, 3, 0, 2,| @a2 = abbrev <3, vbr(6),
1023 | 6, 0, 1, 2, 0, 1, 4> | fixed(2), fixed(4)>;
1024 89:7| 2: <65533, 1, 1, 10> | @a3 = abbrev <10>;
1025 91:7| 2: <65533, 2, 1, 10, 0, | @a4 = abbrev <10, vbr(6)>;
1026 | 2, 6> |
1027 95:0| 2: <65533, 1, 1, 15> | @a5 = abbrev <15>;
1028 97:0| 2: <65533, 3, 1, 43, 0, | @a6 = abbrev <43, vbr(6),
1029 | 2, 6, 0, 1, 2> | fixed(2)>;
1030 101:2| 2: <65533, 4, 1, 24, 0, | @a7 = abbrev <24, vbr(6), vbr(6),
1031 | 2, 6, 0, 2, 6, 0, 2, | vbr(4)>;
1032 | 4> |
1033 106:5| 1: <1, 19> | globals:
1034 109:1| 2: <65533, 3, 1, 0, 0, 2,| @a0 = abbrev <0, vbr(6),
1035 | 6, 0, 1, 1> | fixed(1)>;
1036 113:3| 2: <65533, 2, 1, 1, 0, 2,| @a1 = abbrev <1, vbr(8)>;
1037 | 8> |
1038 116:4| 2: <65533, 2, 1, 2, 0, 2,| @a2 = abbrev <2, vbr(8)>;
1039 | 8> |
1040 119:5| 2: <65533, 3, 1, 3, 0, 3,| @a3 = abbrev <3, array(fixed(8))>
1041 | 0, 1, 8> | ;
1042 123:2| 2: <65533, 2, 1, 4, 0, 2,| @a4 = abbrev <4, vbr(6)>;
1043 | 6> |
1044 126:3| 2: <65533, 3, 1, 4, 0, 2,| @a5 = abbrev <4, vbr(6), vbr(6)>;
1045 | 6, 0, 2, 6> |
1046 130:5| 0: <65534> | }
1047 132:0| 1: <65535, 17, 3> | types { // BlockID = 17
1048 140:0| 2: <65533, 4, 1, 21, 0, | %a0 = abbrev <21, fixed(1),
1049 | 1, 1, 0, 3, 0, 1, 2> | array(fixed(2))>;
1050 144:7| 3: <1, 3> | count 3;
1051 147:4| 3: <7, 32> | @t0 = i32;
1052 150:7| 4: <21, 0, 0, 0, 0> | @t1 = i32 (i32, i32); <%a0>
1053 152:7| 3: <2> | @t2 = void;
1054 154:6| 0: <65534> | }
1055 156:0| 3: <8, 1, 0, 0, 0> | define external i32 @f0(i32, i32);
1056 160:6| 1: <65535, 19, 4> | globals { // BlockID = 19
1057 168:0| 3: <5, 0> | count 0;
1058 170:6| 0: <65534> | }
1059 172:0| 1: <65535, 14, 3> | valuesymtab { // BlockID = 14
1060 180:0| 6: <1, 0, 102> | @f0 : "f"; <@a2>
1061 182:7| 0: <65534> | }
1062 184:0| 1: <65535, 12, 4> | function i32 @f0(i32 %p0, i32 %p1) {
1063 | | // BlockID = 12
1064 192:0| 3: <1, 1> | blocks 1;
1065 | | %b0:
1066 194:6| 5: <2, 2, 1, 0> | %v0 = add i32 %p0, %p1; <@a1>
1067 197:2| 5: <2, 3, 1, 0> | %v1 = add i32 %p0, %v0; <@a1>
1068 199:6| 8: <10, 1> | ret i32 %v1; <@a4>
1069 201:0| 0: <65534> | }
1070 204:0|0: <65534> |}
1071
1072 Note that the example above shows the standard abbreviations used by
1073 *pnacl-finalize*.
1074
1075 .. _link_for_types_block_section:
1076
1077 Types Block
1078 ===========
1079
1080 The types block defines all types used in a program. It must appear in the
1081 :ref:`module block<link_for_module_block>`, before any :ref:`function
1082 address<link_for_function_address_section>` records, the :ref:`globals
1083 block<link_for_globals_block_section>`, the :ref:`valuesymtab
1084 block<link_for_valuesymtab_block_section>`, and any :ref:`function
1085 blocks<link_for_function_blocks_section>`.
1086
1087 All types used in a program must be defined in the types block. Many PNaClAsm
1088 constructs allow one to use explicit type names, rather than the type
1089 identifiers defined by this block. However, they are internally converted to the
1090 corresponding type identifier in the types block. Hence, the requirement that
1091 the types block must appear early in the module block.
1092
1093 Each record in the types block defines a type used by the program. Types can be
1094 broken into the following groups:
1095
1096 Primitive value types
1097 Defines the set of base types for values. This includes various sizes of
1098 integer and floating point types.
1099
1100 Void type
1101 A primitive type that doesn't represent any value and has no size.
1102
1103 Function types
1104 The type signatures of functions.
1105
1106 Vector type
1107 Defines vectors of primitive types.
1108
1109 In addition, any type that is not defined using another type is a primitive
1110 type. All other types (i.e. function and vector) are composite types.
1111
1112 Types must be defined in a topological order, causing primitive types to appear
1113 before the composite types that use them. Each type must be unique. There are no
1114 additional restrictions on the order that types can be defined in a types block.
1115
1116 The following subsections introduce each valid PNaClAsm type, and the
1117 corresponding PNaClAsm construct that defines the type. Types not defined in the
1118 types block, can't be used in a PNaCl program.
1119
1120 The first record of a types block must be a :ref:`count
1121 record<link_for_types_count_record>`, defining how many types are defined by the
1122 types block. All remaining records defines a type. The following subsections
1123 defines valid records within a types block. The order of type records is
1124 important. The position of each defining record implicitly defines the type ID
1125 that will be used to denote that type, within other PNaCl records of the bitcode
1126 file.
1127
1128 To make this more concrete, consider the following example types block::
1129
1130 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1131 48:0| 3: <1, 4> | count 4;
1132 50:4| 3: <7, 32> | @t0 = i32;
1133 53:6| 3: <3> | @t1 = float;
1134 55:4| 3: <2> | @t2 = void;
1135 57:2| 3: <21, 0, 2, 0, 1> | @t3 = void (i32, float);
1136 62:0| 0: <65534> | }
1137
1138 This example defines a types block that defines four type IDs:
1139
1140 @t0
1141 A 32-bit integer type.
1142 @t1
1143 A 32-bit floating point type.
1144 @t2
1145 The void type.
1146 @t3
1147 A function, taking 32-bit integer and float point arguments that returns
1148 void.
1149
1150 .. _link_for_types_count_record:
1151
1152 Count Record
1153 ------------
1154
1155 The *count record* defines how many types are defined in the types
1156 block. Following the types count record are records that define types used by
1157 the PNaCl program.
1158
1159 **Syntax**::
1160
1161 count N; <A>
1162
1163 **Record**::
1164
1165 AA: <1, N>
1166
1167 **Semantics**:
1168
1169 This construct defines the number of types used by the PNaCl program. ``N`` is
1170 the number of types defined in the types block. It is an error to define more
1171 (or fewer) types than value ``N``, within the enclosing types block.
1172
1173 **Constraints**::
1174
1175 AA == AbbrevIndex(A) &
1176 0 == NumTypes
1177
1178 **Updates**:
Jim Stichnoth 2014/11/18 02:24:43 ::
Karl 2014/11/19 20:28:52 Done.
1179
1180 ExpectedTypes = N;
1181
1182 **Examples**::
1183
1184 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1185 48:0| 3: <1, 4> | count 4;
1186 50:4| 3: <7, 32> | @t0 = i32;
1187 53:6| 3: <3> | @t1 = float;
1188 55:4| 3: <2> | @t2 = void;
1189 57:2| 3: <21, 0, 2, 0, 1> | @t3 = void (i32, float);
1190 62:0| 0: <65534> | }
1191
1192 Void Type
1193 ---------
1194
1195 The *void* type record defines the void type, which corresponds to the type that
1196 doesn't define any value, and has no size.
1197
1198 **Syntax**::
1199
1200 @tN = void; <A>
1201
1202 **Record**::
1203
1204 AA: <2>
1205
1206 **Semantics**:
1207
1208 The void type record defines the type that has no values and has no size.
1209
1210 **Constraints**::
1211
1212 AA == AbbrevIndex(A) &
1213 N == NumTypes
1214
1215 **Updates**::
1216
1217 ++NumTypes;
1218 TypeOf(@tN) = void;
1219
1220 **Examples**::
1221
1222 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1223 48:0| 3: <1, 4> | count 4;
1224 50:4| 3: <7, 32> | @t0 = i32;
1225 53:6| 3: <3> | @t1 = float;
1226 55:4| 3: <2> | @t2 = void;
1227 62:0| 0: <65534> | }
1228
1229 Integer Types
1230 -------------
1231
1232 PNaClAsm allows integer types for various bit sizes. Valid bit sizes are 1, 8,
1233 16, 32, and 64. Integers can be signed or unsigned, but the signed component of
1234 an integer is not specified by the type. Rather, individual instructions
1235 determine whether the value is assumed to be signed or unsigned.
1236
1237 It should be noted that in PNaClAsm, all pointers are implemented as 32-bit
1238 (unsigned) integers. There isn't a separate type for pointers. The only way to
1239 tell that a 32-bit integer is a pointer, is when it is used in an instruction
1240 that requires a pointer (such as load and store instructions).
1241
1242 **Syntax**::
1243
1244 @tN = iB; <A>
1245
1246 **Record**::
1247
1248 AA: <7, B>
1249
1250 **Semantics**:
1251
1252 An integer type record defines an integer type. ``B`` defines the number of bits
1253 of the integer type.
1254
1255 **Constraints**::
1256
1257 AA == AbbrevIndex(A) &
1258 N == NumTypes &
1259 B in {1, 8, 16, 32, 64}
1260
1261 **Updates**::
1262
1263 ++NumTypes;
1264 TypeOf(@tN) = iB;
1265
1266 **Examples**::
1267
1268 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1269 48:0| 3: <1, 7> | count 7;
1270 50:4| 3: <7, 64> | @t0 = i64;
1271 53:6| 3: <7, 1> | @t1 = i1;
1272 56:2| 3: <7, 8> | @t2 = i8;
1273 58:6| 3: <7, 16> | @t3 = i16;
1274 61:2| 3: <7, 32> | @t4 = i32;
1275 64:4| 3: <21, 0, 0, 1> | @t5 = i64 (i1);
1276 68:4| 3: <2> | @t6 = void;
1277 70:2| 0: <65534> | }
1278
1279 32-Bit Floating Point Type
1280 --------------------------
1281
1282 PNaClAsm allows computation on 32-bit floating point values. A floating point
1283 type record defines the 32-bit floating point type.
1284
1285 **Syntax**::
1286
1287 @tN = float; <A>
1288
1289 **Record**::
1290
1291 AA: <3>
1292
1293 **Semantics**:
1294
1295 A floating point type record defines the 32-bit floating point type.
1296
1297 **Constraints**::
1298
1299 AA == AbbrevIndex(A) &
1300 N == NumTypes
1301
1302 **Updates**::
1303
1304 ++NumTypes;
1305 TypeOf(@tN) = float;
1306
1307 **Examples**::
1308
1309 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1310 48:0| 3: <1, 4> | count 4;
1311 50:4| 3: <4> | @t0 = double;
1312 52:2| 3: <3> | @t1 = float;
1313 54:0| 3: <21, 0, 0, 1> | @t2 = double (float);
1314 58:0| 3: <2> | @t3 = void;
1315 59:6| 0: <65534> | }
1316
1317 64-bit Floating Point Type
1318 --------------------------
1319
1320 PNaClAsm allows computation on 64-bit floating point values. A 64-bit floating
1321 type record defines the 64-bit floating point type.
1322
1323 **Syntax**::
1324
1325 @tN = double; <A>
1326
1327 **Record**::
1328
1329 AA: <4>
1330
1331 **Semantics**:
1332
1333 A double type record defines the 64-bit floating point type.
1334
1335 **Constraints**::
1336
1337 AA == AbbrevIndex(A) &
1338 N == NumTypes
1339
1340 **Updates**::
1341
1342 ++NumTypes;
1343 TypeOf(@tN) = double;
1344
1345 **Examples**::
1346
1347 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1348 48:0| 3: <1, 4> | count 4;
1349 50:4| 3: <4> | @t0 = double;
1350 52:2| 3: <3> | @t1 = float;
1351 54:0| 3: <21, 0, 0, 1> | @t2 = double (float);
1352 58:0| 3: <2> | @t3 = void;
1353 59:6| 0: <65534> | }
1354
1355 Vector Types
1356 ------------
1357
1358 A vector type is a derived type that represents a vector of elements. Vector
1359 types are used when multiple primitive data values are operated in parallel
1360 using a single (SIMD) :ref:`vector instruction<link_for_vector_instructions>`. A
1361 vector type requires a size (number of elements) and an underlying primitive
1362 data type.
1363
1364 **Syntax**::
1365
1366 @tN = < E x T > <A>
1367
1368 **Record**::
1369
1370 AA: <12, E, TT>
1371
1372 **Semantics**:
1373
1374 The vector type defines a vector of elements. ``T`` is the type of each
1375 element. ``E`` is the number of elements in the vector.
1376
1377 Vector types can only be defined on ``i1``, ``i8``, ``i16``, ``i32``, and
1378 ``float``. All vector types, except those on ``i1``, must contain exactly 128
1379 bits. The valid element sizes are restricted as follows:
1380
1381 ====== ===================
1382 Type Valid element sizes
1383 ====== ===================
1384 i1 4, 8, 16
1385 i8 16
1386 i16 8
1387 i32 4
1388 float 4
1389 ====== ===================
1390
1391 **Constraints**::
1392
1393 AA == AbbrevIndex(A) &
1394 TT == AbsoluteIndex(TypeID(T)) &
1395 N == NumTypes
1396
1397 **Updates**::
1398
1399 ++NumTypes
1400 TypeOf(@tN) = <E x T>
1401
1402 **Examples**::
1403
1404 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1405 48:0| 3: <1, 14> | count 14;
1406 50:4| 3: <7, 32> | @t0 = i32;
1407 53:6| 3: <7, 1> | @t1 = i1;
1408 56:2| 3: <2> | @t2 = void;
1409 58:0| 3: <12, 4, 1> | @t3 = <4 x i1>;
1410 61:2| 3: <12, 8, 1> | @t4 = <8 x i1>;
1411 64:4| 3: <12, 16, 1> | @t5 = <16 x i1>;
1412 67:6| 3: <7, 8> | @t6 = i8;
1413 70:2| 3: <12, 16, 6> | @t7 = <16 x i8>;
1414 73:4| 3: <7, 16> | @t8 = i16;
1415 76:0| 3: <12, 8, 8> | @t9 = <8 x i16>;
1416 79:2| 3: <12, 4, 0> | @t10 = <4 x i32>;
1417 82:4| 3: <3> | @t11 = float;
1418 84:2| 3: <12, 4, 11> | @t12 = <4 x float>;
1419 87:4| 3: <21, 0, 2> | @t13 = void ();
1420 90:6| 0: <65534> | }
1421
1422 .. _link_for_function_type:
1423
1424 Function Type
1425 -------------
1426
1427 The *function* type can be thought of as a function signature. It consists of a
1428 return type, and a (possibly empty) list of formal parameter types.
1429
1430 **Syntax**::
1431
1432 %tN = RT (T1, ... , TM) <A>
1433
1434 **Record**::
1435
1436 AA: <21, 0, IRT, IT1, ... , ITM>
1437
1438 **Semantics**:
1439
1440 The function type defines the signature of a function. ``RT`` is the return type
1441 of the function, while types ``T1`` through ``TM`` are the types of the
1442 arguments. Indices to the corresponding type identifiers are stored in the
1443 corresponding record.
1444
1445 The return value must either be a primitive type, type ``void``, or a vector
1446 type. Parameter types can be a primitive or vector type.
1447
1448 For ordinary functions, the only valid integer types that can be used for a
1449 return or parameter type are ``i32`` and ``i64``. All other integer types are
1450 not allowed.
1451
1452 For :ref:`intrinsic functions<link_for_intrinsic_functions_section>`, all
1453 integer types are allowed for both return and parameter types.
1454
1455 **Constraints**::
1456
1457 AA == AbbrevIndex(A) &
1458 M >= 0 &
1459 IRT == AbsoluteIndex(TypeID(RT)) &
1460 IT1 == AbsoluteIndex(TypeID(T1)) &
1461 ...
1462 ITM == AbsoluteIndex(TypeID(TM)) &
1463 N == NumTypes
1464
1465 **Updates**::
1466
1467 ++NumTypes
1468 TypeOf(@tN) = RT (T1, ... , TM)
1469
1470 **Examples**::
1471
1472 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1473 48:0| 3: <1, 7> | count 7;
1474 50:4| 3: <7, 32> | @t0 = i32;
1475 53:6| 3: <3> | @t1 = float;
1476 55:4| 3: <4> | @t2 = double;
1477 57:2| 3: <21, 0, 2, 1> | @t3 = double (float);
1478 61:2| 3: <2> | @t4 = void;
1479 63:0| 3: <21, 0, 4> | @t5 = void ();
1480 66:2| 3: <21, 0, 0, 0, 1, 0, 2>| @t6 =
1481 | | i32 (i32, float, i32, double);
1482 72:4| 0: <65534> | }
1483
1484 .. _link_for_globals_block_section:
1485
1486 Globals Block
1487 =============
1488
1489 The globals block defines global addresses of variables and constants, used by
1490 the PNaCl program. It also defines the memory associated with the global
1491 addresses, and how to initialize each global variable/constant. It must appear
1492 in the :ref:`module block<link_for_module_block>`. It must appear after the
1493 :ref:`types block<link_for_types_block_section>`, as well as after all
1494 :ref:`function address<link_for_function_address_section>` records. But, it must
1495 also appear before the :ref:`valuesymtab
1496 block<link_for_valuesymtab_block_section>`, and any
1497 :ref:`function blocks<link_for_function_blocks_section>`.
1498
1499 The globals block begins with a :ref:`count
1500 record<link_for_globals_count_record>`, defining how many global addresses are
1501 defined by the PNaCl program. It is then followed by a sequence of records that
1502 defines each global address, and how each global address is initialized.
1503
1504 The standard sequence, for defining global addresses, begins with a global
1505 address record. It is then followed by a sequence of records defining how the
1506 global address is initialized. If the initializer is simple, a single record is
1507 used. Otherwise, the initializer is preceded with a :ref:`compound
1508 record<link_for_compound_initializer>`, specifying a number *N*, followed by
1509 sequence of *N* simple initializer records.
1510
1511 The size of the memory referenced by each global address is defined by its
1512 initializer records. All simple initializer records define a sequence of
1513 bytes. A compound initializer defines the sequence of bytes by concatenating the
1514 corresponding sequence of bytes for each of its simple initializer records.
1515
1516 For notational convenience, PNaClAsm begins a compound record with a "{", and
1517 inserts a "}" after the last initializer record associated with the compound
1518 record. This latter "}" does not correspond to any record. It is implicitly
1519 assumed by the size specified in the compound record, and is added only to
1520 improve readability.
1521
1522 Explicit alignment is specified for global addresses, and must be a power of
1523 2. See :ref:`memory blocks and
1524 alignment<link_for_memory_blocks_and_alignment_section>` for a more detailed
1525 discussion on how to define alignment.
1526
1527 For example, consider the following pnacl-bcdis output snippet::
1528
1529 52:0| 1: <65535, 19, 2> | globals { // BlockID = 19
1530 60:0| 3: <5, 2> | count 2;
1531 62:4| 3: <0, 1, 1> | const @g0, align 1,
1532 65:6| 3: <2, 8> | zerofill 8;
1533 68:2| 3: <0, 1, 0> | var @g1, align 1,
1534 71:4| 3: <1, 2> | initializers 2 {
1535 74:0| 3: <3, 1, 2, 3, 4> | { 1, 2, 3, 4}
1536 78:6| 3: <2, 2> | zerofill 2;
1537 | | }
1538 81:2| 0: <65534> | }
1539
1540 This snippet defines the global constant ``@g0``, and the global variable
1541 ``@g1``. ``@g0`` is 8 bytes long, and initialized to zero. ``@g1`` is
1542 initialized with 6 bytes: ``1 2 3 4 0 0``.
1543
1544 .. _link_for_globals_count_record:
1545
1546 Count Record
1547 ------------
1548
1549 The count record defines the number of global addresses used by the PNaCl
1550 program.
1551
1552 **Syntax**::
1553
1554 count N; <A>
1555
1556 **Record**::
1557
1558 AA: <5, N>
1559
1560 **Semantics**:
1561
1562 This record must appear first in the globals block. The count record defines
1563 the number of global addresses used by the program.
1564
1565 **Constraints**::
1566
1567 AA == AbbrevIndex(A)
1568
1569 **Updates**::
1570
1571 ExpectedGlobals = N;
1572 ExpectedInitializers = 0;
1573
1574 **Examples**::
1575
1576 52:0| 1: <65535, 19, 2> | globals { // BlockID = 19
1577 60:0| 3: <5, 2> | count 2;
1578 62:4| 3: <0, 1, 1> | const @g0, align 1,
1579 65:6| 3: <2, 8> | zerofill 8;
1580 68:2| 3: <0, 1, 0> | var @g1, align 1,
1581 71:4| 3: <1, 2> | initializers 2 {
1582 74:0| 3: <3, 1, 2, 3, 4> | { 1, 2, 3, 4}
1583 78:6| 3: <2, 2> | zerofill 2;
1584 | | }
1585 81:2| 0: <65534> | }
1586
1587 .. _link_for_global_variable_address:
1588
1589 Global Variable Addresses
1590 -------------------------
1591
1592 A global variable address record defines a global address to global data. The
1593 global variable address record must be immediately followed by initializer
1594 record(s) that define how the corresponding global variable is initialized.
1595
1596 **Syntax**::
1597
1598 var @gN, align V, <A>
1599
1600 **Record**::
1601
1602 AA: <0, VV, 0>
1603
1604 **Semantics**:
1605
1606 A global variable address record defines a global address for a global variable.
1607 ``V`` is the :ref:`memory
1608 alignment<link_for_memory_blocks_and_alignment_section>` for the global variable
1609 address, and is a power of 2.
1610
1611 It is assumed that the memory, referenced by the global variable address, can be
1612 both read and written to.
1613
1614 **Constraints**::
1615
1616 AA == AbbrevIndex(A) &
1617 N == NumGlobalAddresses &
1618 ExpectedInitializers == 0 &
1619 VV == Log2(V+1)
1620
1621 **Updates**::
1622
1623 ++NumGlobalAddresses;
1624 ExpectedInitializers = 1;
1625 TypeOf(@gN) = i32;
1626
1627 **Examples**::
1628
1629 52:0| 1: <65535, 19, 2> | globals { // BlockID = 19
1630 60:0| 3: <5, 2> | count 2;
1631 62:4| 3: <0, 3, 0> | var @g0, align 4,
1632 65:6| 3: <2, 8> | zerofill 8;
1633 68:2| 3: <0, 1, 0> | var @g1, align 1,
1634 71:4| 3: <3, 1, 2, 3, 4> | { 1, 2, 3, 4}
1635 76:2| 0: <65534> | }
1636 80:0|0: <65534> |}
1637
1638 .. _link_for_global_constant_address:
1639
1640 Global Constant Addresses
1641 -------------------------
1642
1643 A global constant address record defines an address corresponding to a global
1644 constant that can't be modified by the program. The global constant address
1645 record must be immediately followed by initializer record(s) that define how
1646 the corresponding global constant is initialized.
1647
1648 **Syntax**::
1649
1650 const @gN, align V, <A>
1651
1652 **Record**::
1653
1654 AA: <0, VV, 1>
1655
1656 **Semantics**:
1657
1658 A global constant address record defines a global address for a global constant.
1659 ``V`` is the :ref:`memory
1660 alignment<link_for_memory_blocks_and_alignment_section>` for the global constant
1661 address, and is a power of 2.
1662
1663 It is assumed that the memory, referenced by the global constant address, is
1664 only read, and can't be written to.
1665
1666 Note that the only difference between a global variable address and a global
1667 constant address record is the third element of the record. If the value is
1668 zero, it defines a global variable address. If the value is one, it defines a
1669 global constant address.
1670
1671 **Constraints**::
1672
1673 AA == AbbrevIndex(A) &
1674 N == NumGlobalAddresses &
1675 ExpectedInitializers == 0 &
1676 VV == Log2(V+1)
1677
1678 **Updates**::
1679
1680 ++NumGlobalAddresses;
1681 ExpectedInitializers = 1;
1682 TypeOf(@gN) = i32;
1683
1684 **Examples**::
1685
1686 52:0| 1: <65535, 19, 2> | globals { // BlockID = 19
1687 60:0| 3: <5, 2> | count 2;
1688 62:4| 3: <0, 3, 1> | const @g0, align 4,
1689 65:6| 3: <2, 8> | zerofill 8;
1690 68:2| 3: <0, 1, 1> | const @g1, align 1,
1691 71:4| 3: <3, 1, 2, 3, 4> | { 1, 2, 3, 4}
1692 76:2| 0: <65534> | }
1693
1694 Zerofill Initializer
1695 --------------------
1696
1697 The zerofill initializer record initializes a sequence of bytes, associated with
1698 a global address, with zeros.
1699
1700 **Syntax**::
1701
1702 zerofill N; <A>
1703
1704 **Record**::
1705
1706 AA: <2, N>
1707
1708 **Semantics**:
1709
1710 A zerofill initializer record initializes a sequence of bytes, associated with a
1711 global address, with zeros. The number of bytes initialized to zero is ``N``.
1712
1713 **Constraints**::
1714
1715 AA == AbbrevIndex(A) &
1716 ExpectedInitializers > 0
1717
1718 **Updates**::
1719
1720 --ExpectedInitializers;
1721
1722 **Examples**::
1723
1724 52:0| 1: <65535, 19, 2> | globals { // BlockID = 19
1725 60:0| 3: <5, 2> | count 2;
1726 62:4| 3: <0, 3, 1> | const @g0, align 4,
1727 65:6| 3: <2, 8> | zerofill 8;
1728 68:2| 3: <0, 1, 0> | var @g1, align 1,
1729 71:4| 3: <2, 4> | zerofill 4;
1730 74:0| 0: <65534> | }
1731
1732 Data Initializer
1733 ----------------
1734
1735 Data records define a sequence of bytes. These bytes define the initial value of
1736 the contents of the corresponding memory.
1737
1738 **Syntax**::
1739
1740 { B1 , .... , BN } <A>
1741
1742 **Record**::
1743
1744 AA: <3, B1, ..., BN>
1745
1746 **Semantics**:
1747
1748 A data record defines a sequence of (unsigned) bytes ``B1`` through ``BN``, that
1749 initialize ``N`` bytes of memory.
1750
1751 **Constraints**::
1752
1753 AA == AbbrevIndex(A) &
1754 ExpectedInitializers > 0
1755
1756 **Updates**::
1757
1758 --ExpectedInitializers;
1759
1760 **Examples**::
1761
1762 56:0| 3: <8, 1, 0, 1, 0> | declare external void @f0();
1763 60:6| 1: <65535, 19, 2> | globals { // BlockID = 19
1764 68:0| 3: <5, 2> | count 2;
1765 70:4| 3: <0, 1, 1> | const @g0, align 1,
1766 73:6| 3: <3, 1, 2, 97, 36, 44, | { 1, 2, 97, 36, 44, 88,
1767 | 88, 44, 50> | 44, 50}
1768 86:0| 3: <0, 1, 1> | const @g1, align 1,
1769 89:2| 3: <1, 3> | initializers 3 {
1770 91:6| 3: <3, 1, 2, 3, 4> | { 1, 2, 3, 4}
1771 96:4| 3: <4, 0> | reloc @f0;
1772 99:0| 3: <3, 99, 66, 22, 12> | { 99, 66, 22, 12}
1773 | | }
1774 105:2| 0: <65534> | }
1775
1776 Relocation Initializer
1777 ----------------------
1778
1779 A relocation initializer record allows one to define the initial value of a
1780 global address with the value of another global address (i.e. either
1781 :ref:`function<link_for_function_address_section>`,
1782 :ref:`variable<link_for_global_variable_address>`, or
1783 :ref:`constant<link_for_global_constant_address>`). Since addresses are
1784 pointers, a relocation initializer record defines 4 bytes of memory.
1785
1786 **Syntax**::
1787
1788 reloc V; <A>
1789
1790 **Record**::
1791
1792 AA: <4, VV>
1793
1794 **Semantics**:
1795
1796 A relocation initializer record defines a 4-byte value containing the specified
1797 global address ``V``.
1798
1799 **Constraints**::
1800
1801 AA == AbbrevIndex(A) &
1802 VV == AbsoluteIndex(V) &
1803 VV >= NumFuncAddresses &
1804 VV < NumFuncAddresses + ExpectedGlobals &
1805 ExpectedInitializers > 0
1806
1807 **Updates**:
Jim Stichnoth 2014/11/18 02:24:42 ::
Karl 2014/11/19 20:28:52 Done.
1808
1809 --ExpectedInitializers;
1810
1811 **Examples**::
1812
1813 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1814 48:0| 3: <1, 2> | count 2;
1815 50:4| 3: <2> | @t0 = void;
1816 52:2| 3: <21, 0, 0> | @t1 = void ();
1817 55:4| 0: <65534> | }
1818 56:0| 3: <8, 1, 0, 1, 0> | declare external void @f0();
1819 60:6| 1: <65535, 19, 2> | globals { // BlockID = 19
1820 68:0| 3: <5, 2> | count 2;
1821 70:4| 3: <0, 1, 0> | var @g0, align 1,
1822 73:6| 3: <1, 3> | initializers 3 {
1823 76:2| 3: <4, 0> | reloc @f0;
1824 78:6| 3: <4, 1> | reloc @g0;
1825 81:2| 3: <4, 2> | reloc @g1;
1826 | | }
1827 83:6| 3: <0, 3, 0> | var @g1, align 4,
1828 87:0| 3: <2, 4> | zerofill 4;
1829 89:4| 0: <65534> | }
1830
1831 This example defines global address ``@g0`` and ``@g1``. ``@g0`` defines 12
1832 bytes of memory, and is initialized with three addresses ``@f1``, ``@g0``, and
1833 ``@g1``. Note that all global addresses can be used in a relocation
1834 initialization record, even if it isn't defined yet.
1835
1836 Subfield Relocation Initializer
1837 -------------------------------
1838
1839 A subfield relocation initializer record allows one to define the initial value
1840 of a global address with the value of another (non-function) global address
1841 (i.e. either :ref:`variable<link_for_global_variable_address>` or
1842 :ref:`constant<link_for_global_constant_address>` address), plus a
1843 constant. Since addresses are pointers, a relocation initializer record defines
1844 4 bytes of memory.
1845
1846 **Syntax**::
1847
1848 reloc V + X; <A>
1849 reloc V - X; <A>
1850
1851 **Record**::
1852
1853 AA: <4, VV, XXX>
1854
1855 **Semantics**:
1856
1857 A subfield relocation initializer record defines a 4-byte value containing the
1858 specified global (non-function) address ``V``, modified by the unsigned offset
1859 ``X``. ``XX`` is the corresponding signed offset. In the first form, ``XX ==
1860 X``. In the second form, ``XX == -X``.
1861
1862 **Constraints**::
1863
1864 AA == AbbrevIndex(A)
1865 VV == AbsoluteIndex(V)
1866 VV >= NumFuncAddresses
1867 VV < NumFuncAddresses + ExpectedGlobals
1868 ExpectedInitializers > 0
1869 XXX == SignRotate(XX)
1870
1871 **Updates**::
1872
1873 --ExpectedInitializers;
1874
1875 **Examples**::
1876
1877 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1878 48:0| 3: <1, 0> | count 0;
1879 50:4| 0: <65534> | }
1880 52:0| 1: <65535, 19, 2> | globals { // BlockID = 19
1881 60:0| 3: <5, 3> | count 3;
1882 62:4| 3: <0, 1, 0> | var @g0, align 1,
1883 65:6| 3: <1, 3> | initializers 3 {
1884 68:2| 3: <4, 0, 1> | reloc @g0 + 1;
1885 71:4| 3: <4, 1, 4294967295> | reloc @g1 - 1;
1886 79:2| 3: <4, 2, 4> | reloc @g2 + 4;
1887 | | }
1888 82:4| 3: <0, 3, 0> | var @g1, align 4,
1889 85:6| 3: <2, 4> | zerofill 4;
1890 88:2| 3: <0, 3, 0> | var @g2, align 4,
1891 91:4| 3: <2, 8> | zerofill 8;
1892 94:0| 0: <65534> | }
1893
1894 .. _link_for_compound_initializer:
1895
1896 Compound Initializer
1897 --------------------
1898
1899 The compound initializer record must immediately follow a global
1900 :ref:`variable<link_for_global_variable_address>` or
1901 :ref:`constant<link_for_global_constant_address>` address record. It defines how
1902 many simple initializer records are used to define the initializer. The size of
1903 the corresponding memory is the sum of the bytes needed for each of the
1904 succeeding initializers.
1905
1906 Note that a compound initializer can't be used as a simple initializer of
1907 another compound initializer (i.e. nested compound initializers are not
1908 allowed).
1909
1910 **Syntax**::
1911
1912 initializers N { <A>
1913 ...
1914 }
1915
1916 **Record**::
1917
1918 AA: <1, N>
1919
1920 **Semantics**:
1921
1922 Defines that the next `N` initializers should be associated with the global
1923 address of the previous record.
1924
1925 **Constraints**::
1926
1927 AA == AbbrevIndex(A) &
1928 ExpectedInitializers == 1
1929
1930 **Updates**::
1931
1932 ExpectedInitializers = N;
1933
1934 **Examples**::
1935
1936 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
1937 48:0| 3: <1, 0> | count 0;
1938 50:4| 0: <65534> | }
1939 52:0| 1: <65535, 19, 2> | globals { // BlockID = 19
1940 60:0| 3: <5, 2> | count 2;
1941 62:4| 3: <0, 0, 1> | const @g0, align 0,
1942 65:6| 3: <1, 2> | initializers 2 {
1943 68:2| 3: <2, 8> | zerofill 8;
1944 70:6| 3: <3, 3, 2, 1, 0> | { 3, 2, 1, 0}
1945 | | }
1946 75:4| 3: <0, 0, 0> | var @g1, align 0,
1947 78:6| 3: <1, 2> | initializers 2 {
1948 81:2| 3: <3, 1, 2, 3, 4> | { 1, 2, 3, 4}
1949 86:0| 3: <2, 2> | zerofill 2;
1950 | | }
1951 88:4| 0: <65534> | }
1952
1953 .. _link_for_valuesymtab_block_section:
1954
1955 Valuesymtab Block
1956 =================
1957
1958 The valuesymtab block does not define any values. Its only goal is to associate
1959 text names with external :ref:`function
1960 addresses<link_for_function_address_section>`. Each association is defined by a
1961 record in the valuesymtab block. Currently, only
1962 :ref:`intrinsic<link_for_intrinsic_functions_section>` function addresses and
1963 the (external) start function (``_start``) can be named. All named function
1964 addresses must be external. Each record in the valuesymtab block is a *entry*
1965 record, defining a single name association.
1966
1967 Entry Record
1968 ------------
1969
1970 The *entry* record defines a name for a function address.
1971
1972 **Syntax**::
1973
1974 V : "NAME"; <A>
1975
1976 **Record**::
1977
1978 AA: <1, B1, ... , BN>
1979
1980 **Semantics**:
1981
1982 The *entry* record defines a name ``NAME`` for function address ``V``. ``NAME`` is a
1983 sequence of ANSCII characters ``B1`` through ``BN``.
Jim Stichnoth 2014/11/17 18:54:56 ASCII
Karl 2014/11/17 20:17:28 Done.
1984
1985 **Examples**::
1986
1987 72:0| 3: <8, 4, 0, 1, 0> | declare external
1988 | | void @f0(i32, i32, i32, i32, i1);
1989 76:6| 3: <8, 4, 0, 1, 0> | declare external
1990 | | void @f1(i32, i32, i32, i32, i1);
1991 81:4| 3: <8, 5, 0, 0, 0> | define external void @f2(i32);
1992 86:2| 1: <65535, 19, 2> | globals { // BlockID = 19
1993 92:0| 3: <5, 0> | count 0;
1994 94:4| 0: <65534> | }
1995 96:0| 1: <65535, 14, 2> | valuesymtab { // BlockID = 14
1996 104:0| 3: <1, 1, 108, 108, 118, | @f1 : "llvm.memmove.p0i8.p0i8.i32";
1997 | 109, 46, 109, 101, |
1998 | 109, 109, 111, 118, |
1999 | 101, 46, 112, 48, |
2000 | 105, 56, 46, 112, 48,|
2001 | 105, 56, 46, 105, 51,|
2002 | 50> |
2003 145:4| 3: <1, 2, 95, 115, 116, | @f2 : "_start";
2004 | 97, 114, 116> |
2005 157:0| 3: <1, 0, 108, 108, 118, | @f0 : "llvm.memcpy.p0i8.p0i8.i32";
2006 | 109, 46, 109, 101, |
2007 | 109, 99, 112, 121, |
2008 | 46, 112, 48, 105, 56,|
2009 | 46, 112, 48, 105, 56,|
2010 | 46, 105, 51, 50> |
2011 197:0| 0: <65534> | }
2012
2013 .. _link_for_module_block:
2014
2015 Module Block
2016 ============
2017
2018 The module block, like all blocks, is enclosed in a pair of
2019 :ref:`enter<link_for_enter_block_record_section>` /
2020 :ref:`exit<link_for_exit_block_record_section>` records, using block ID 8. A
2021 well-formed module block consists of the following records (in order):
2022
2023 A version record
2024 The :ref:`version record<link_for_version_record>` communicates which version
2025 of the PNaCl bitcode reader/writer should be used. Note that this is
2026 different than the PNaCl bitcode (ABI) version. The PNaCl bitcode (ABI)
2027 version defines what is expected in records, and is defined in the header
2028 record of the bitcode file. The version record defines the version of the
2029 PNaCl bitcode reader/writer to use to convert records into bit sequences.
2030
2031 Optional local abbreviations
2032 Defines a list of local :ref:`abbreviations<link_for_abbreviations_section>`
2033 to use for records within the module block.
2034
2035 An abbreviations block
2036 The :ref:`abbreviations block<link_for_abbreviations_block_section>` defines
2037 user-defined, global abbreviations that are used to convert PNaCl records to
2038 bit sequences in blocks following the abbreviations block.
2039
2040 A types block
2041 The :ref:`types block<link_for_types_block_section>` defines the set of all
2042 types used in the program.
2043
2044 A non-empty sequence of function address records
2045 Each record defines a :ref:`function
2046 address<link_for_function_address_section>` used by the program. Function
2047 addresses must either be external, or defined internally by the program. If
2048 they are defined by the program, there must be a :ref:`function
2049 block<link_for_function_blocks_section>` (appearing later in the module) that
2050 defines the sequence of instructions for each defined function.
2051
2052 A globals block defining the global variables.
2053 This :ref:`block<link_for_globals_block_section>` defines the set of
2054 global :ref:`variable<link_for_global_variable_address>` and
2055 :ref:`constant<link_for_global_constant_address>` addresses used by the
2056 program. In addition to the addresses, each global variable also defines how
2057 the corresponding global variable is initialized.
2058
2059 An optional value symbol table block.
2060 This :ref:`block<link_for_valuesymtab_block_section>`, if defined, provides
2061 textual names for :ref:`function
2062 addresses<link_for_function_address_section>` (previously defined in the
2063 module). Note that only names for intrinsic functions and the start function
2064 are specified.
2065
2066 A sequence of function blocks.
2067 Each :ref:`function block<link_for_Function_blocks_section>` defines the
2068 corresponding intermediate representation for each defined function. The
2069 order of function blocks is used to associate them with :ref:`function
2070 addresses<link_for_function_address_section>`. The order of the defined
2071 function blocks must follow the same order as the corresponding function
2072 addresses defined in the module block.
2073
2074 Descriptions of the :ref:`abbreviations<link_for_abbreviations_section>`,
2075 :ref:`types<link_for_types_block_section>`,
2076 :ref:`globals<link_for_globals_block_section>`, :ref:`value symbol
2077 table<link_for_valuesymtab_block_section>`, and
2078 :ref:`function<link_for_function_blocks_section>` blocks are not provided
2079 here. See the appropriate reference for more details. The following subsections
2080 describe each of the records that can appear in a module block.
2081
2082 .. _link_for_version_record:
2083
2084 Version Record
2085 --------------
2086
2087 The version record defines the implementation of the PNaCl bitstream
2088 reader/writer to use. That is, the implementation that converts PNaCl records to
2089 bit sequences, and converts them back to PNaCl records. Note that this is
2090 different than the PNaCl version of the bitcode file (encoded in the header
2091 record of the bitcode file). The PNaCl version defines the valid forms of PNaCl
2092 records. The version record is specific to the PNaCl version, and may have
2093 different values for different PNaCl versions.
2094
2095 Note that currently, only PNaCl bitcode version 2, and version record value 1 is
2096 defined.
2097
2098 **Syntax**::
2099
2100 version N; <A>
2101
2102 **Record**::
2103
2104 AA: <1, N>
2105
2106 **Semantics**:
2107
2108 The version record defines which PNaCl reader/writer rules should be
2109 followed. ``N`` is the version number. Currently ``N`` must be 1. Future
2110 versions of PNaCl may define additional legal values.
2111
2112 **Constraints**::
2113
2114 AA == AbbrevIndex(A)
2115
2116 *Examples*::
2117
2118 16:0|1: <65535, 8, 2> |module { // BlockID = 8
2119 24:0| 3: <1, 1> | version 1;
2120 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID = 0
2121 36:0| 0: <65534> | }
2122
2123 .. _link_for_function_address_section:
2124
2125 Function Address
2126 ----------------
2127
2128 A function address record describes a function address. *Defined* function
2129 addresses define :ref:`implementations<link_for_function_blocks_section>` while
2130 *declared* function addresses do not.
2131
2132 Since a PNaCl program is assumed to be a complete (statically linked)
2133 executable, All functions should be *defined* and *internal*. The exception to
2134 this are :ref:`intrinsic functions<link_for_intrinsic_functions_section>`, which
2135 should only be *declared* and *external*, since intrinsic functions will be
2136 automatically converted to appropriate code by the :ref:`PNaCl
2137 translator<link_for_pnacl_translator>`.
2138
2139 The implementation of a *defined* function address is provided by a
2140 corresponding function block, appearing later in the module block. The
2141 association of a *defined* function address with the corresponding function
2142 block is based on position. The *Nth* defined function address record, in the
2143 module block, has its implementation in the *Nth* function block of that module
2144 block.
2145
2146 **Syntax**::
2147
2148 PN LN T0 @fN ( T1 , ... , TM ); <A>
2149
2150 **Record**::
2151
2152 AA: <8, T, C, P, L>
2153
2154 **Semantics**:
2155
2156 Describes the function address ``@fN``. ``PN`` is the name that specifies the
2157 prototype value ``P`` associated with the function. A function address is
2158 *defined* only if ``P == 0``. Otherwise, it is only *declared*. The type of the
2159 function is :ref:`function type<link_for_function_type>` ``@tT``. ``L`` is the
2160 linkage specification corresponding to name ``LN``. ``C`` is the calling
2161 convention used by the function.
2162
2163 Note that function signature must be defined by a function type in the types
2164 block. Hence, the return value must either be a primitive type, type ``void``,
2165 or a vector type.
2166
2167 For ordinary functions, integer parameter and types can only be ``i32`` and
2168 ``i64``. All other integer types are not allowed. For intrinsic functions, all
2169 integer types are allowed.
2170
2171 Valid prototype names ``PN``, and corresponding ``P`` values, are:
2172
2173 = =======
2174 P PN
2175 = =======
2176 1 declare
2177 0 define
2178 = =======
2179
2180 Valid linkage names ``LN``, and corresponding ``L`` values, are:
2181
2182 = ========
2183 L LN
2184 = ========
2185 3 internal
2186 0 external
2187 = ========
2188
2189 Currently, only one calling convention ``C`` is supported:
2190
2191 = ====================
2192 C Calling Convention
2193 = ====================
2194 0 C calling convention
2195 = ====================
2196
2197 **Constraints**::
2198
2199 AA = AbbrevIndex(A) &
2200 T = TypeID(TypeOf(T0 ( T1 , ... , TN ))) &
2201 N = NumFuncAddresses
2202
2203 **Updates**::
2204
2205 ++NumFuncAddresses;
2206 TypeOf(@fN) = TypeOf(TypeID(i32));
2207 TypeOfFcn(@fN) = TypeOf(@tT);
2208
2209 if PN == 0:
2210 DefiningFcnIDs += @FN;
2211 ++NumDefinedFunctionAddresses;
2212
2213 **Examples**::
2214
2215 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
2216 48:0| 3: <1, 7> | count 7;
2217 50:4| 3: <7, 32> | @t0 = i32;
2218 53:6| 3: <3> | @t1 = float;
2219 55:4| 3: <4> | @t2 = double;
2220 57:2| 3: <2> | @t3 = void;
2221 59:0| 3: <21, 0, 2, 1> | @t4 = double (float);
2222 63:0| 3: <21, 0, 0, 0, 1, 0, 2>| @t5 =
2223 | | i32 (i32, float, i32, double);
2224 69:2| 3: <21, 0, 3> | @t6 = void ();
2225 72:4| 0: <65534> | }
2226 76:0| 3: <8, 4, 0, 1, 0> | declare external double @f0(float);
2227 80:6| 3: <8, 5, 0, 1, 0> | declare external
2228 | | i32 @f1(i32, float, i32, double);
2229 85:4| 3: <8, 6, 0, 0, 0> | define external void @f2();
2230
2231 .. _link_for_constants_block_section:
2232
2233 Constants Blocks
2234 ================
2235
2236 Constants blocks define literal constants used within each function. Its intent
2237 it to define them once, before instructions. A constants block can only appear
Jim Stichnoth 2014/11/18 02:24:42 is to define
Karl 2014/11/19 20:28:53 Done.
2238 in a :ref:`function block<link_for_function_blocks_section>`, and must appear
2239 before any instructions in the function block.
2240
2241 Currently, only integer literals, floating point literals, and undefined vector
2242 constants can be defined.
2243
2244 To minimize type information put in a constants block, the type information is
2245 separated from the constants. This allows a sequence of constants to be given
2246 the same type. This is done by defining a :ref:`set type
2247 record<link_for_constants_set_type_record>`, followed by a sequence of literal
2248 constants. These literal constants all get converted to the type of the
2249 preceding set type record.
2250
2251 Note that constants that are used for switch case selectors should not be added
2252 to the constants block, since the switch instruction contains the constants used
2253 for case selectors. All other constants in the function block must be put into a
2254 constants block, so that instructions can use them.
2255
2256 To make this more concrete, consider the following example constants block::
2257
2258 106:4| 1: <65535, 11, 2> | constants { // BlockID = 11
2259 116:0| 3: <1, 0> | i32:
2260 118:4| 3: <4, 2> | %c0 = i32 1;
2261 121:0| 3: <4, 4> | %c1 = i32 2;
2262 123:4| 3: <1, 2> | i8:
2263 126:0| 3: <4, 8> | %c2 = i8 4;
2264 128:4| 3: <4, 6> | %c3 = i8 3;
2265 131:0| 3: <1, 1> | float:
2266 133:4| 3: <6, 1065353216> | %c4 = float 1;
2267 139:6| 0: <65534> | }
2268
2269 .. _link_for_constants_set_type_record:
2270
2271 Set Type Record
2272 ---------------
2273
2274 The *set type* record defines the type to use for the (immediately) succeeding
2275 literals.
2276
2277 **Syntax**::
2278
2279 T: <A>
2280
2281 **Record**::
2282
2283 AA: <1, TT>
2284
2285 **Semantics**:
2286
2287 The *set type* record defines type ``T`` to be used to type the (immediately)
2288 succeeding literals. ``T`` must be a non-void primitive value type or a vector
2289 type.
2290
2291 **Constraints**::
2292
2293 TT == TypeID(T)
2294
2295 **Updates**::
2296
2297 ConstantsSetType = T;
2298
2299 **Examples**::
2300
2301 106:4| 1: <65535, 11, 2> | constants { // BlockID = 11
2302 116:0| 3: <1, 0> | i32:
2303 118:4| 3: <4, 2> | %c0 = i32 1;
2304 121:0| 3: <4, 4> | %c1 = i32 2;
2305 123:4| 3: <1, 2> | i8:
2306 126:0| 3: <4, 8> | %c2 = i8 4;
2307 128:4| 3: <4, 6> | %c3 = i8 3;
2308 131:0| 3: <1, 1> | float:
2309 133:4| 3: <6, 1065353216> | %c4 = float 1;
2310 139:6| 0: <65534> | }
2311
2312 .. _link_for_undefined_literal:
2313
2314 Undefined Literal
2315 -----------------
2316
2317 The *undefined* literal record creates an undefined literal for the type *T*
2318 defined by the preceding *set type* record.
2319
2320 Note: See :ref:`insert element
2321 instruction<link_for_insert_element_instruction_section>` for an example of how
2322 you would use the undefined literal with vector types.
2323
2324 **Syntax**::
2325
2326 %cN = T undef; <50>
2327
2328 **Record**::
2329
2330 AA: <3>
2331
2332 **Semantics**:
2333
2334 The *undefined* literal record creates an undefined literal constant ``%cN`` for
2335 type ``T``. ``T`` must be the type defined by the preceding *set type* record, and
2336 be a primitive value type or a vector type.
2337
2338 **Constraints**::
2339
2340 N == NumFcnConsts &
2341 T == ConstantsSetType &
2342 IsPrimitive(T) or IsVector(T)
2343
2344 **Updates**::
2345
2346 ++NumFcnConsts;
2347 TypeOf(%cN) = T;
2348
2349 **Examples**::
2350
2351 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
2352 48:0| 3: <1, 5> | count 5;
2353 50:4| 3: <7, 32> | @t0 = i32;
2354 53:6| 3: <3> | @t1 = float;
2355 55:4| 3: <2> | @t2 = void;
2356 57:2| 3: <12, 4, 0> | @t3 = <4 x i32>;
2357 60:4| 3: <21, 0, 2> | @t4 = void ();
2358 63:6| 0: <65534> | }
2359 ...
2360 106:4| 1: <65535, 11, 2> | constants { // BlockID = 11
2361 116:0| 3: <1, 0> | i32:
2362 118:4| 3: <3> | %c0 = i32 undef;
2363 120:2| 3: <4, 2> | %c1 = i32 1;
2364 122:6| 3: <1, 3> | <4 x i32>:
2365 125:2| 3: <3> | %c2 = <4 x i32> undef;
2366 127:0| 3: <1, 1> | float:
2367 129:4| 3: <3> | %c3 = float undef;
2368 131:2| 0: <65534> | }
2369
2370 .. _link_for_integer_literal:
2371
2372 Integer Literal
2373 ---------------
2374
2375 The *integer literal* record creates an integer literal for the integer type *T*
2376 defined by the preceding *set type* record.
2377
2378 **Syntax**::
2379
2380 %cN = T V; <A>
2381
2382 **Record**::
2383
2384 AA: <4, VV>
2385
2386 **Semantics**:
2387
2388 The *integer literal* record creates an integer literal constant ``%cN`` for
2389 type ``T``. ``T`` must be the type defined by the preceding *set type* record,
2390 and an integer type. The literal ``V`` can be signed, but must be definable by
2391 type ``T``.
2392
2393 **Constraints**::
2394
2395 N == NumFcnConsts &
2396 T == ConstantsSetType &
2397 VV == SignRotate(V) &
2398 IsInteger(T)
2399
2400 **Updates**::
2401
2402 TypeOf(%cN) = T;
2403
2404 **Examples**::
2405
2406 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
2407 48:0| 3: <1, 7> | count 7;
2408 50:4| 3: <7, 8> | @t0 = i8;
2409 53:0| 3: <7, 16> | @t1 = i16;
2410 55:4| 3: <7, 32> | @t2 = i32;
2411 58:6| 3: <7, 64> | @t3 = i64;
2412 62:0| 3: <7, 1> | @t4 = i1;
2413 64:4| 3: <2> | @t5 = void;
2414 66:2| 3: <21, 0, 5> | @t6 = void ();
2415 69:4| 0: <65534> | }
2416 ...
2417 114:4| 1: <65535, 11, 2> | constants { // BlockID = 11
2418 124:0| 3: <1, 0> | i8:
2419 126:4| 3: <4, 2> | %c0 = i8 1;
2420 129:0| 3: <4, 4> | %c1 = i8 2;
2421 131:4| 3: <1, 1> | i16:
2422 134:0| 3: <4, 6> | %c2 = i16 3;
2423 136:4| 3: <4, 8> | %c3 = i16 4;
2424 139:0| 3: <1, 2> | i32:
2425 141:4| 3: <4, 10> | %c4 = i32 5;
2426 144:0| 3: <4, 12> | %c5 = i32 6;
2427 146:4| 3: <1, 3> | i64:
2428 149:0| 3: <4, 3> | %c6 = i64 -1;
2429 151:4| 3: <4, 5> | %c7 = i64 -2;
2430 154:0| 3: <1, 4> | i1:
2431 156:4| 3: <4, 3> | %c8 = i1 1;
2432 159:0| 3: <4, 0> | %c9 = i1 0;
2433 161:4| 0: <65534> | }
2434
2435 Floating Point Literal
2436 ----------------------
2437
2438 The *floating point literal* record creates a floating point literal for the
2439 floating point type *T* defined by the preceding *set type* record.
2440
2441 **Syntax**::
2442
2443 %cN = T V; <A>
2444
2445 **Record**::
2446
2447 AA: <6, VV>
2448
2449 **Semantics**:
2450
2451 The *floating point literal* record creates a floating point literal constant
2452 ``%cN`` for type ``T``. ``T`` must the type type defined by the preceding *set
2453 type* record, and be a floating point type. The literal ``V`` is the floating
2454 value to be defined. The value ``VV`` if the corresponding IEEE unsigned integer
2455 that defines value ``V``. That is, the literal ``VV`` must be a valid IEEE 754
2456 32-bit (unsigned integer) value if ``T`` is ``float``, and a valid IEEE 754
2457 64-bit (unsigned integer) value if ``T`` is ``double``.
2458
2459 **Constraints**::
2460
2461 N == NumFcnConsts
2462 T == ConstantsSetType
2463 IsFloat(T)
2464
2465 **Updates**::
2466
2467 TypeOf(%cN) = T;
2468
2469 **Examples**::
2470
2471 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
2472 48:0| 3: <1, 4> | count 4;
2473 50:4| 3: <3> | @t0 = float;
2474 52:2| 3: <4> | @t1 = double;
2475 54:0| 3: <2> | @t2 = void;
2476 55:6| 3: <21, 0, 2> | @t3 = void ();
2477 59:0| 0: <65534> | }
2478 ...
2479 102:4| 1: <65535, 11, 2> | constants { // BlockID = 11
2480 112:0| 3: <1, 0> | float:
2481 114:4| 3: <6, 0> | %c0 = float 0;
2482 117:0| 3: <6, 1065353216> | %c1 = float 1;
2483 123:2| 3: <6, 1088421888> | %c2 = float 7;
2484 130:2| 3: <6, 1090519040> | %c3 = float 8;
2485 137:2| 3: <3> | %c4 = float undef;
2486 139:0| 3: <6, 2143289344> | %c5 = float nan;
2487 146:0| 3: <6, 2139095040> | %c6 = float inf;
2488 153:0| 3: <6, 4286578688> | %c7 = float -inf;
2489 160:0| 3: <1, 1> | double:
2490 162:4| 3: <6, | %c8 = double 1;
2491 | 4607182418800017408> |
2492 174:0| 3: <6, 0> | %c9 = double 0;
2493 176:4| 3: <6, | %c10 = double 5;
2494 | 4617315517961601024> |
2495 188:0| 3: <6, | %c11 = double 6;
2496 | 4618441417868443648> |
2497 199:4| 3: <6, | %c12 = double nan;
2498 | 9221120237041090560> |
2499 211:0| 3: <6, | %c13 = double inf;
2500 | 9218868437227405312> |
2501 222:4| 3: <6, | %c14 = double -inf;
2502 | 18442240474082181120>|
2503 234:0| 0: <65534> | }
2504
2505 .. _link_for_function_blocks_section:
2506
2507 Function Blocks
2508 ===============
2509
2510 A function block defines the implementation of a defined :ref:`function
2511 address<link_for_function_address_section>`. The function address it defines is
2512 based on the position of the corresponding defined function address. The Nth
2513 defined function address always corresponds to the Nth function block in the
2514 module block.
2515
2516 A function implementation contains a list of basic blocks, forming the control
2517 flow graph. Each *basic block* contains a list of instructions, and ends with a
2518 :ref:`terminator instruction<link_for_terminator_instruction_section>`
2519 (e.g. branch).
2520
2521 Basic blocks are not represented by records. Rather, context is implicit. The
2522 first basic block begins with the first instruction record in the function
2523 block. Block boundaries are determined by terminator instructions. The
2524 instruction that follows a terminator instruction begins a new basic block.
2525
2526 The first basic block in a function is special in two ways: it is immediately
2527 executed on entrance to the function, and it is not allowed to have predecessor
2528 basic blocks (i.e. there can't be any branches to the entry block of a
2529 function). Because the entry block has no predecessors, it also can't have any
2530 :ref:`phi<link_for_phi_instruction_section>` instructions.
2531
2532 The parameters are implied by the type of the corresponding function
2533 address. One parameter is defined for each argument of the function :ref:`type
2534 signature<link_for_function_type>` of the corresponding :ref:`function
2535 address<link_for_function_address_section>`.
2536
2537 The number of basic blocks is defined by the :ref:`count
2538 record<link_for_basic_blocks_count>`. Each ::ref::`terminator
Jim Stichnoth 2014/11/18 02:24:43 ::ref:: ==> :ref: (I think)
Karl 2014/11/19 20:28:52 Done.
2539 instruction<link_for_terminator_instruction_section>` ends the current basic
2540 block, and the next instruction begins a new basic block. Basic blocks are
2541 numbered by the order they appear (starting with index 0). Basic block IDs have
2542 the form ``%bN``, where ``N`` corresponds to the position of the basic block
2543 within the function block.
2544
2545 Each instruction, within a function block, corresponds to a corresponding PNaCl
2546 record. The layout of a function block is the (basic block) count record,
2547 followed by a sequence of instruction records.
2548
2549 For readability, PNaClAsm introduces basic block IDs. These basic block IDs do
2550 not correspond to PNaCl records, since basic block boundaries are defined
2551 implicitly, after terminator instructions. They appear only for readability.
2552
2553 Operands of instructions are defined using an :ref:`absolute
2554 index<link_for_absolute_index_section>`. This absolute index implicitly encodes
2555 function addresses, global addresses, parameters, constants, and instructions
2556 that generate values. The encoding takes advantage of the implied ordering of
2557 these values in the bitcode file, defining a contiguous sequence of indices for
2558 each kind of identifier. That is, indices are ordered by putting function
2559 address identifiers first, followed by global address identifiers, followed by
2560 parameter identifiers, followed by constant identifiers, and lastly instruction
2561 value identifiers.
2562
2563 To save space in the encoded bitcode file, most operands are encoded using a
2564 :ref:`relative index<link_for_relative_index>` value, rather than
2565 :ref:`absolute<link_for_absolute_index_section>`. This
2566 is done because most instruction operands refer to values defined earlier in the
2567 (same) basic block. As a result, the relative distance (back) from the next
2568 value defining instruction is frequently a small number. Small numbers tend to
2569 require fewer bits when they are converted to bit sequences.
2570
2571 Note that instructions that can appear in a function block are defined in sectio ns
2572 :ref:`link_for_terminator_instruction_section`,
2573 :ref:`link_for_integer_binary_instructions`,
2574 :ref:`link_for_floating_point_binary_instructions`,
2575 :ref:`link_for_memory_creation_and_access_instructions`,
2576 :ref:`link_for_conversion_instructions`,
2577 :ref:`link_for_compare_instructions`,
2578 :ref:`link_for_vector_instructions`, and
2579 :ref:`link_for_other_pnaclasm_instructions`.
2580
2581 The following subsections define the remaining records that can appear in a
2582 function block.
2583
2584 Function Enter
2585 --------------
2586
2587 PNaClAsm defines a function enter block construct. The corresponding record is
2588 simply an :ref:`enter block<link_for_enter_block_record_section>` record, with
2589 BlockID value ``12``. All context about the defining address is implicit by the
2590 position of the function block, and the corresponding defining :ref:`function
2591 address<link_for_function_address_section>`. To improve readability, PNaClAsm
2592 includes the function signature into the syntax rule.
2593
2594 **Syntax**::
2595
2596 function TR @fN ( T0 %p0, ... , TM %pM ) { <B>
2597
2598 **Record**::
2599
2600 1: <65535, 12, B>
2601
2602 **Semantics**:
2603
2604 ``B`` is the number of bits reserved for abbreviations in the block. If it is
2605 omitted, 2 is assumed. See :ref:`enter<link_for_enter_block_record_section>`
2606 block records for more details.
2607
2608 The value of ``N`` corresponds to the positional index of the corresponding
2609 defining function address this block is associated with. ``M`` is the number of
2610 defined parameters (minus one) in the function heading.
2611
2612 **Constraints**::
2613
2614 N == NumFcnImpls &
2615 @fN in DefiningFcnIDs &
2616 TypeOfFcn(@fN) == TypeOf(TypeID(TR (T0, ... , TM)))
2617
2618 **Updates**::
2619
2620 ++NumFcnImpls;
2621 EnclosingFcnID = @fN;
2622 NumBasicBlocks = 0;
2623 ExpectedBlocks = 0;
2624 NumParams = M;
2625 for I in [0..M]:
2626 TypeOf(%pI) = TypeOf(TypeID(TI));
2627
2628 **Examples**::
2629
2630 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
2631 48:0| 3: <1, 4> | count 4;
2632 50:4| 3: <7, 32> | @t0 = i32;
2633 53:6| 3: <2> | @t1 = void;
2634 55:4| 3: <21, 0, 1> | @t2 = void ();
2635 58:6| 3: <21, 0, 0, 0> | @t3 = i32 (i32);
2636 62:6| 0: <65534> | }
2637 ...
2638 104:0| 1: <65535, 12, 2> | function void @f0() {
2639 | | // BlockID = 12
2640 112:0| 3: <1, 1> | blocks 1;
2641 | | %b0:
2642 114:4| 3: <10> | ret void;
2643 116:2| 0: <65534> | }
2644 120:0| 1: <65535, 12, 2> | function i32 @f1(i32 %p0) {
2645 | | // BlockID = 12
2646 128:0| 3: <1, 1> | blocks 1;
2647 | | %b0:
2648 130:4| 3: <10, 1> | ret i32 %p0;
2649 133:0| 0: <65534> | }
2650
2651 .. _link_for_basic_blocks_count:
2652
2653 Count Record
2654 ------------
2655
2656 The count record, within a function block, defines the number of basic blocks
2657 used to define the function implementation. It must be the first record in the
2658 function block.
2659
2660 **Syntax**::
2661
2662 blocks: N; <A>
2663 %b0:
2664
2665 **Record**::
2666
2667 AA: <1, N>
2668
2669 **Semantics**:
2670
2671 The count record defines the number ``N`` of basic blocks in the implemented
2672 function.
2673
2674 **Constraints**::
2675
2676 AA == AbbrevIndex(A) &
2677 ExpectedBasicBlocks == N &
2678 NumBasicBlocks == 0
2679
2680 **Updates**::
2681
2682 104:0| 1: <65535, 12, 2> | function void @f0() {
2683 | | // BlockID = 12
2684 112:0| 3: <1, 1> | blocks 1;
2685 | | %b0:
2686 114:4| 3: <10> | ret void;
2687 116:2| 0: <65534> | }
2688 120:0| 1: <65535, 12, 2> | function i32 @f1(i32 %p0) {
2689 | | // BlockID = 12
2690 128:0| 3: <1, 1> | blocks 1;
2691 | | %b0:
2692 130:4| 3: <10, 1> | ret i32 %p0;
2693 133:0| 0: <65534> | }
2694
2695 .. _link_for_terminator_instruction_section:
2696
2697 Terminator Instructions
2698 =======================
2699
2700 Terminator instructions are instructions that appear in a :ref:`function
2701 block<link_for_function_blocks_section>`, and define the end of the current
2702 basic block. A terminator instruction indicates which block should be executed
2703 after the current block is finished. The function block is well formed only if
2704 the number of terminator instructions, in the function block, corresponds to the
2705 value defined by the corresponding function basic block :ref:`count
2706 record<link_for_basic_blocks_count>`.
2707
2708 Note that any branch instruction to label ``%bN``, where ``N >=
2709 ExpectedBasicBlocks``, is illegal. For ease of readability, this constraint
2710 hasn't been put on branch instructions. Rather it is only implied.
2711
2712 In addition, it must be the case that ``NumBasicBlocks < ExpectedBasicBlocks``,
2713 and will not be listed as a constraint. Further, if ``B = NumBasicBlocks + 1``
2714 is the number associated with the next basic block. Label `%bB:` only appears
2715 if::
2716
2717 B < ExpectedBasicBlocks
2718
2719 That is, the label is omitted only if this terminator instruction is the last
2720 instruction in the function block.
2721
2722 Return Void Instruction
2723 -----------------------
2724
2725 The return void instruction is used to return control from a function back to
2726 the caller, without returning any value.
2727
2728 **Syntax**::
2729
2730 ret void; <A>
2731 %bB:
2732
2733 **Record**::
2734
2735 AA: <10>
2736
2737 **Semantics**:
2738
2739 The return void instruction returns control to the calling function.
2740
2741 **Constraints**::
2742
2743 AA == AbbrevIndex(A) &
2744 B == NumBasicBlocks + 1 &
2745 ReturnType(TypeOf(EnclosingFcnID)) == void
2746
2747 **Updates**::
2748
2749 ++NumBasicBlocks;
2750
2751 **Examples**::
2752
2753 104:0| 1: <65535, 12, 2> | function void @f0() {
2754 | | // BlockID = 12
2755 112:0| 3: <1, 1> | blocks 1;
2756 | | %b0:
2757 114:4| 3: <10> | ret void;
2758 116:2| 0: <65534> | }
2759
2760 Return Value Instruction
2761 ------------------------
2762
2763 The return value instruction is used to return control from a function back to
2764 the caller, including a value. The value must correspond to the return type of
2765 the enclosing function.
2766
2767 **Syntax**::
2768
2769 ret T V; <A>
2770 %bB:
2771
2772 **Record**::
2773
2774 AA: <10, VV>
2775
2776 **Semantics**:
2777
2778 The return value instruction returns control to the calling function, returning
2779 the provided value.
2780
2781 ``V`` is the value to return. Type ``T`` must be of the type returned by the
2782 function. It must also be the type associated with value ``V``.
2783
2784 The return type ``T`` must either be a (non-void) primitive type, or a vector
2785 type. If the function block is implementing an ordinary function, and the return
2786 type is an integer type, it must be either ``i32`` or ``i64``.
2787
2788 **Constraints**::
2789
2790 AA == AbbrevIndex(A) &
2791 VV == RelativeIndex(V) &
2792 B == NumBasicBlocks + 1 &
2793 T == TypeOf(V) == ReturnType(TypeOf(EnclosingFcnID))
2794
2795 **Updates**::
2796
2797 ++NumBasicBlocks;
2798
2799 **Examples**::
2800
2801 120:0| 1: <65535, 12, 2> | function i32 @f1(i32 %p0) {
2802 | | // BlockID = 12
2803 128:0| 3: <1, 1> | blocks 1;
2804 | | %b0:
2805 130:4| 3: <10, 1> | ret i32 %p0;
2806
2807 Unconditional Branch Instruction
2808 --------------------------------
2809
2810 The unconditional branch instruction is used to cause control flow to transfer
2811 to a different basic block of the function.
2812
2813 **Syntax**::
2814
2815 br %bN; <A>
2816 %bB:
2817
2818 **Record**::
2819
2820 AA: <11, N>
2821
2822 **Semantics**:
2823
2824 The unconditional branch instruction causes control flow to transfer to basic
2825 block ``N``.
2826
2827 **Constraints**::
2828
2829 AA == AbbrevIndex(A) &
2830 B == NumBasicBlocks + 1 &
2831 0 < N &
2832 N < ExpectedBasicBlocks
2833
2834 **Updates**::
2835
2836 ++NumBasicBlocks;
2837
2838 **Examples**::
2839
2840 88:0| 1: <65535, 12, 2> | function void @f0() {
2841 | | // BlockID = 12
2842 96:0| 3: <1, 5> | blocks 5;
2843 | | %b0:
2844 98:4| 3: <11, 3> | br label %b3;
2845 | | %b1:
2846 101:0| 3: <11, 4> | br label %b4;
2847 | | %b2:
2848 103:4| 3: <11, 1> | br label %b1;
2849 | | %b3:
2850 106:0| 3: <11, 2> | br label %b2;
2851 | | %b4:
2852 108:4| 3: <10> | ret void;
2853 110:2| 0: <65534> | }
2854
2855 Conditional Branch Instruction
2856 ------------------------------
2857
2858 The conditional branch instruction is used to cause control flow to transfer to
2859 a different basic block of the function, based on a boolean test condition.
2860
2861 **Syntax**::
2862
2863 br i1 C, %bT, %bBF; <A>
2864 %bB:
2865
2866 **Record**::
2867
2868 AA: <11, T, F, CC>
2869
2870 **Semantics**:
2871
2872 Upon execution of a conditional branch instruction, the *i1* (boolean) argument
2873 ``C`` is evaluated. If the value is ``true``, control flows to basic block
2874 ``%bT``. Otherwise control flows to basic block ``%bF``.
2875
2876 **Constraints**::
2877
2878 AA == AbbrevIndex(A) &
2879 CC == RelativeIndex(C) &
2880 B == NumBasicBlocks + 1 &
2881 0 < T &
2882 B1 < ExpectedBasicBlocks &
2883 0 < F &
2884 B2 < ExpectedBasicBlocks &
2885 TypeOf(C) == i1
2886
2887 **Updates**::
2888
2889 ++NumBasicBlocks;
2890
2891 **Examples**::
2892
2893 92:0| 1: <65535, 12, 2> | function void @f0() {
2894 | | // BlockID = 12
2895 100:0| 3: <1, 5> | blocks 5;
2896 102:4| 1: <65535, 11, 2> | constants { // BlockID = 11
2897 112:0| 3: <1, 1> | i1:
2898 114:4| 3: <4, 3> | %c0 = i1 1;
2899 117:0| 3: <4, 0> | %c1 = i1 0;
2900 119:4| 0: <65534> | }
2901 | | %b0:
2902 120:0| 3: <11, 3> | br label %b3;
2903 | | %b1:
2904 122:4| 3: <11, 2, 4, 2> | br i1 %c0, label %b2, label %b4;
2905 | | %b2:
2906 126:4| 3: <11, 3> | br label %b3;
2907 | | %b3:
2908 129:0| 3: <10> | ret void;
2909 | | %b4:
2910 130:6| 3: <11, 2, 3, 1> | br i1 %c1, label %b2, label %b3;
2911 134:6| 0: <65534> | }
2912
2913 Unreachable
2914 -----------
2915
2916 The unreachable instruction has no defined semantics. The instruction is used to
2917 inform the :ref:`PNaCl translator<link_for_pnacl_translator>` that control
2918 can't reach this instruction.
2919
2920 **Syntax**::
2921
2922 unreachable; <A>
2923 %bB:
2924
2925 **Record**::
2926
2927 AA: <15>
2928
2929 **Semantics**:
2930
2931 Directive to the :ref:`PNaCl translator<link_for_pnacl_translator>` that
2932 this instruction is unreachable.
2933
2934 **Constraints**::
2935
2936 AA == AbbrevIndex(A)
2937 B == NumBasicBlocks + 1 &
2938
2939 **Updates**::
2940
2941 ++NumBasicBlocks;
2942
2943 **Examples**::
2944
2945 108:0| 1: <65535, 12, 2> | function void @f0(i32 %p0) {
2946 | | // BlockID = 12
2947 116:0| 3: <1, 5> | blocks 5;
2948 118:4| 1: <65535, 11, 2> | constants { // BlockID = 11
2949 128:0| 3: <1, 2> | i1:
2950 130:4| 3: <4, 3> | %c0 = i1 1;
2951 133:0| 3: <4, 0> | %c1 = i1 0;
2952 135:4| 0: <65534> | }
2953 | | %b0:
2954 136:0| 3: <11, 1, 2, 2> | br i1 %c0, label %b1, label %b2;
2955 | | %b1:
2956 140:0| 3: <11, 3, 4, 1> | br i1 %c1, label %b3, label %b4;
2957 | | %b2:
2958 144:0| 3: <15> | unreachable;
2959 | | %b3:
2960 145:6| 3: <15> | unreachable;
2961 | | %b4:
2962 147:4| 3: <10> | ret void;
2963 149:2| 0: <65534> | }
2964
2965 Switch Instruction
2966 ------------------
2967
2968 The *switch* instruction transfers control flow to one of several different
2969 places, based on a selector value. It is a generalization of the conditional
2970 branch instruction.
2971
2972 **Syntax**::
2973
2974 switch T V0 {
2975 default: br label %bB0;
2976 T V1: br label %bB1;
2977 ...
2978 T VN: br label %bBN;
2979 } <A>
2980 %bB:
2981
2982 **Record**::
2983
2984 AA: <12, TT, B0, N, (1, 1, VVI, BI | 1 <= i <= N)>
2985
2986 **Semantics**:
2987
2988 The switch instruction transfers control to a basic block in ``B0`` through
2989 ``BN``. Value ``V`` is used to conditionally select which block to branch
2990 to. ``T`` is the type of ``V`` and ``V1`` through ``VN``, and must be an integer
2991 type. Value ``V1`` through ``VN`` are integers to compare against ``V``. If sele ctor
2992 ``V`` matches ``VI`` (for some ``I``, ``1 <= I <= N``), then the instruction bra nches to
2993 block ``BI``. If ``V`` is not in ``V1`` through ``VN``, the instruction branches to
2994 block ``B0``.
2995
2996 **Constraints**::
2997
2998 AA == AbbrevIndex(A) &
2999 B == NumBasicBlocks + 1 &
3000 TT == TypeID(T) &
3001 VI == SignRotate(VI) for all I, 1 <= I <= N &
3002
3003 **Updates**::
3004
3005 ++NumBasicBlocks;
3006
3007 **Examples**::
3008
3009 116:0| 1: <65535, 12, 2> | function void @f0(i32 %p0) {
3010 | | // BlockID = 12
3011 124:0| 3: <1, 6> | blocks 6;
3012 | | %b0:
3013 126:4| 3: <12, 1, 1, 2, 4, 1, 1,| switch i32 %p0 {
3014 | 2, 3, 1, 1, 4, 3, 1, | default: br label %b2;
3015 | 1, 8, 4, 1, 1, 10, 4>| i32 1: br label %b3;
3016 | | i32 2: br label %b3;
3017 | | i32 4: br label %b4;
3018 | | i32 5: br label %b4;
3019 | | }
3020 | | %b1:
3021 143:2| 3: <11, 5> | br label %b5;
3022 | | %b2:
3023 145:6| 3: <11, 5> | br label %b5;
3024 | | %b3:
3025 148:2| 3: <11, 5> | br label %b5;
3026 | | %b4:
3027 150:6| 3: <11, 5> | br label %b5;
3028 | | %b5:
3029 153:2| 3: <10> | ret void;
3030 155:0| 0: <65534> | }
3031 156:0| 1: <65535, 12, 2> | function void @f1(i64 %p0) {
3032 | | // BlockID = 12
3033 164:0| 3: <1, 6> | blocks 6;
3034 | | %b0:
3035 166:4| 3: <12, 2, 1, 2, 4, 1, 1,| switch i64 %p0 {
3036 | 2, 3, 1, 1, 4, 3, 1, | default: br label %b2;
3037 | 1, 8, 4, 1, 1, | i64 1: br label %b3;
3038 | 39777555332, 4> | i64 2: br label %b3;
3039 | | i64 4: br label %b4;
3040 | | i64 19888777666: br label %b4;
3041 | | }
3042 | | %b1:
3043 188:4| 3: <11, 5> | br label %b5;
3044 | | %b2:
3045 191:0| 3: <11, 5> | br label %b5;
3046 | | %b3:
3047 193:4| 3: <11, 5> | br label %b5;
3048 | | %b4:
3049 196:0| 3: <11, 5> | br label %b5;
3050 | | %b5:
3051 198:4| 3: <10> | ret void;
3052 200:2| 0: <65534> | }
3053
3054 .. _link_for_integer_binary_instructions:
3055
3056 Integer Binary Instructions
3057 ===========================
3058
3059 Binary instructions are used to do most of the computation in a program. This
3060 section focuses on binary instructions that operator on integer values, or
3061 vectors of integer values.
3062
3063 All binary operations require two operands of the same type, execute an
3064 operation on them, and produce a value. The value may represent multiple values
3065 if the type is a vector type. The result value always has the same type as its
3066 operands.
3067
3068 Some integer binary operations can be applied to both signed and unsigned
3069 integers. Others, the sign is significant. In general, if the sign plays a role
3070 in the instruction, the sign information is encoded into the name of the
3071 instruction.
3072
3073 For most binary operations (except some of the logical operations), integer
3074 type i1 is disallowed.
3075
3076 Integer Add
3077 -----------
3078
3079 The integer add instruction returns the sum of its two arguments. Both arguments
3080 and the result must be of the same type. That type must be integer, or an
3081 integer vector type.
3082
3083 **Syntax**::
3084
3085 %vN = add T V1, V2; <A>
3086
3087 **Record**::
3088
3089 AA: <2, VV1, VV2, 0>
3090
3091 **Semantics**:
3092
3093 The integer add instruction returns the sum of its two arguments. Arguments
3094 ``V1`` and ``V2``, and the result ``%vN``, must be of type ``T``. ``T`` must be
3095 an integer type, or an integer vector type. ``N`` is defined by the record
3096 position, defining the corresponding value generated by the instruction.
3097
3098 The result returned is the mathematical result modulo 2\ :sup:`n`\ , where ``n`` is
3099 the bitwidth of the integer result.
Jim Stichnoth 2014/11/17 18:54:55 Could you make this "bit width" or "bit-width", he
Karl 2014/11/17 20:17:28 Done.
3100
3101 Because integers are assumed to use a two's complement representation,
3102 this instruction is appropriate for both signed and unsigned integers.
3103
3104 In the add instruction, integer type ``i1`` (and a vector of integer type
3105 ``i1``) is disallowed.
3106
3107 **Constraints**::
3108
3109 AA == AbbrevIndex(A) &
3110 VV1 == RelativeIndex(V1) &
3111 VV2 == RelativeIndex(V2) &
3112 T == TypeOf(V1) == TypeOf(V2) &
3113 IsInteger(UnderlyingType(T)) &
3114 UnderlyingType(T) != i1 &
3115 N == NumValuedInsts
3116
3117 **Updates**::
3118
3119 ++NumValuedInsts;
3120 TypeOf(%vN) = T
3121
3122 **Examples**::
3123
3124 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3125 | | // BlockID = 12
3126 104:0| 3: <1, 1> | blocks 1;
3127 | | %b0:
3128 106:4| 3: <2, 2, 1, 0> | %v0 = add i32 %p0, %p1;
3129 110:4| 3: <2, 3, 1, 0> | %v1 = add i32 %p0, %v0;
3130 114:4| 3: <10, 1> | ret i32 %v1;
3131 117:0| 0: <65534> | }
3132
3133 Integer Subtract
3134 ----------------
3135
3136 The integer subtract instruction returns the difference of its two arguments.
3137 Both arguments and the result must be of the same type. That type must be
3138 integer, or an integer vector type.
3139
3140 Note: Since there isn't a negate instruction, subtraction from constant zero
3141 should be used to negate values.
3142
3143 **Syntax**::
3144
3145 %vN = sub T V1, V2; <A>
3146
3147 **Record**::
3148
3149 AA: <2, VV1, VV2, 1>
3150
3151 **Semantics**:
3152
3153 The integer subtract returns the difference of its two arguments. Arguments ``V1 ``
3154 and ``V2``, and the result ``%vN`` must be of type ``T``. ``T`` must be an integ er
3155 type, or an integer vector type. ``N`` is defined by the record position,
3156 defining the corresponding value generated by the instruction.
3157
3158 The result returned is the mathematical result modulo 2\ :sup:`n`\ , where ``n`` is
3159 the bitwidth of the integer result.
3160
3161 Because integers are assumed to use a two's complement representation,
3162 this instruction is appropriate for both signed and unsigned integers.
3163
3164 In the subtract instruction, integer type ``i1`` (and a vector of integer type
3165 ``i1``) is disallowed.
3166
3167 **Constraints**::
3168
3169 AA == AbbrevIndex(A) &
3170 VV1 == RelativeIndex(V1) &
3171 VV2 == RelativeIndex(V2) &
3172 T == TypeOf(V1) == TypeOf(V2) &
3173 IsInteger(UnderlyingType(T)) &
3174 UnderlyingType(T) != i1 &
3175 N == NumValuedInsts
3176
3177 **Updates**::
3178
3179 ++NumValuedInsts;
3180 TypeOf(%vN) = T
3181
3182 **Examples**::
3183
3184 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3185 | | // BlockID = 12
3186 104:0| 3: <1, 1> | blocks 1;
3187 | | %b0:
3188 106:4| 3: <2, 2, 1, 1> | %v0 = sub i32 %p0, %p1;
3189 110:4| 3: <2, 3, 1, 1> | %v1 = sub i32 %p0, %v0;
3190 114:4| 3: <10, 1> | ret i32 %v1;
3191 117:0| 0: <65534> | }
3192
3193 Integer Multiply
3194 ----------------
3195
3196 The integer multiply instruction returns the product of its two arguments. Both
3197 arguments and the result must be of the same type. That type must be integer,
3198 or an integer based vector type.
3199
3200 **Syntax**::
3201
3202 &vN = mul T V1, V2; <A>
3203
3204 **Record**::
3205
3206 AA: <2, VV1, VV2, 2>
3207
3208 **Semantics**:
3209
3210 The integer multiply instruction returns the product of its two
3211 arguments. Arguments ``V1`` and ``V2``, and the result ``%vN``, must be of type
3212 ``T``. ``T`` must be an integer type, or an integer vector type. ``N`` is
3213 defined by the record position, defining the corresponding value generated by
3214 the instruction.
3215
3216 The result returned is the mathematical result modulo 2\ :sup:`n`\ , where ``n`` is
3217 the bitwidth of the integer result.
3218
3219 Because integers are assumed to use a two's complement representation,
3220 this instruction is appropriate for both signed and unsigned integers.
3221
3222 In the subtract instruction, integer type ``i1`` (or a vector on integer type
3223 ``i1``) is disallowed.
3224
3225 **Constraints**::
3226
3227 AA == AbbrevIndex(A) &
3228 VV1 == RelativeIndex(V1) &
3229 VV2 == RelativeIndex(V2) &
3230 T == TypeOf(V1) == TypeOf(V2) &
3231 IsInteger(UnderlyingType(T)) &
3232 UnderlyingType(T) != i1 &
3233 N == NumValuedInsts
3234
3235 **Updates**::
3236
3237 ++NumValuedInsts;
3238 TypeOf(%vN) = T
3239
3240 **Examples**::
3241
3242 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3243 | | // BlockID = 12
3244 104:0| 3: <1, 1> | blocks 1;
3245 | | %b0:
3246 106:4| 3: <2, 2, 1, 2> | %v0 = mul i32 %p0, %p1;
3247 110:4| 3: <2, 1, 3, 2> | %v1 = mul i32 %v0, %p0;
3248 114:4| 3: <10, 1> | ret i32 %v1;
3249 117:0| 0: <65534> | }
3250
3251 Signed Integer Divide
3252 ---------------------
3253
3254 The signed integer divide instruction returns the quotient of its two arguments.
3255 Both arguments and the result must be of the same type. That type must be
3256 integer, or an integer vector type.
3257
3258 **Syntax**::
3259
3260 %vN = sdiv T V1, V2; <A>
3261
3262 **Record**::
3263
3264 AA: <2, VV1, VV2, 4>
3265
3266 **Semantics**:
3267
3268 The signed integer divide instruction returns the quotient of its two
3269 arguments. Arguments ``V1`` and ``V2``, and the result ``%vN``, must be of type
3270 ``T``. ``T`` must be a integer type, or an integer vector type. ``N`` is defined
3271 by the record position, defining the corresponding value generated by the
3272 instruction.
3273
3274 Signed values are assumed. Note that signed and unsigned integer division are
3275 distinct operations. For unsigned integer division use the unsigned integer
3276 divide instruction (udiv).
3277
3278 In the signed integer divide instruction, integer type ``i1`` (and a vector of
3279 integer type ``i1``) is disallowed. Integer division by zero is guaranteed to
3280 trap.
3281
3282 Note that overflow can happen with this instruction when dividing the maximum
3283 negative integer by ``-1``. The behavior for this case is currently undefined.
3284
3285 **Constraints**::
3286
3287 AA == AbbrevIndex(A) &
3288 VV1 == RelativeIndex(V1) &
3289 VV2 == RelativeIndex(V2) &
3290 T == TypeOf(V1) == TypeOf(V2) &
3291 IsInteger(UnderlyingType(T)) &
3292 UnderlyingType(T) != i1 &
3293 N == NumValuedInsts
3294
3295 **Updates**::
3296
3297 ++NumValuedInsts;
3298 TypeOf(%vN) = T
3299
3300 **Examples**::
3301
3302 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3303 | | // BlockID = 12
3304 104:0| 3: <1, 1> | blocks 1;
3305 | | %b0:
3306 106:4| 3: <2, 2, 1, 4> | %v0 = sdiv i32 %p0, %p1;
3307 110:4| 3: <2, 1, 2, 4> | %v1 = sdiv i32 %v0, %p1;
3308 114:4| 3: <10, 1> | ret i32 %v1;
3309 117:0| 0: <65534> | }
3310
3311 Unsigned Integer Divide
3312 -----------------------
3313
3314 The unsigned integer divide instruction returns the quotient of its two
3315 arguments. Both the arguments and the result must be of the same type. That type
3316 must be integer, or an integer vector type.
3317
3318 **Syntax**::
3319
3320 %vN = udiv T V1, V2; <a>
3321
3322 **Record**::
3323
3324 AA: <2, A1, A2, 3>
3325
3326 **Semantics**:
3327
3328 The unsigned integer divide instruction returns the quotient of its two
3329 arguments. Arguments ``V1`` and ``V2``, and the result ``%vN``, must be of type
3330 ``T``. ``T`` must be an integer type, or an integer vector type. ``N`` is
3331 defined by the record position, defining the corresponding value generated by
3332 the instruction.
3333
3334 Unsigned integer values are assumed. Note that signed and unsigned integer
3335 division are distinct operations. For signed integer division use the signed
3336 integer divide instruction (sdiv).
3337
3338 In the unsigned integer divide instruction, integer type ``i1`` (and a vector of
3339 integer type ``i1``) is disallowed. Division by zero is guaranteed to trap.
3340
3341 **Constraints**::
3342
3343 AA == AbbrevIndex(A) &
3344 VV1 == RelativeIndex(V1) &
3345 VV2 == RelativeIndex(V2) &
3346 T == TypeOf(V1) == TypeOf(V2) &
3347 IsInteger(UnderlyingType(T)) &
3348 UnderlyingType(T) != i1 &
3349 N == NumValuedInsts
3350
3351 **Updates**::
3352
3353 ++NumValuedInsts;
3354 TypeOf(%vN) = T
3355
3356 **Examples**::
3357
3358 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3359 | | // BlockID = 12
3360 104:0| 3: <1, 1> | blocks 1;
3361 | | %b0:
3362 106:4| 3: <2, 2, 1, 3> | %v0 = udiv i32 %p0, %p1;
3363 110:4| 3: <2, 1, 2, 3> | %v1 = udiv i32 %v0, %p1;
3364 114:4| 3: <10, 1> | ret i32 %v1;
3365 117:0| 0: <65534> | }
3366
3367 Signed Integer Remainder
3368 ------------------------
3369
3370 The signed integer remainder instruction returns the remainder of the quotient
3371 of its two arguments. Both arguments and the result must be of the same
3372 type. That type must be integer, or an integer based vector type.
3373
3374 **Syntax**::
3375
3376 %vN = srem T V1, V2; <A>
3377
3378 **Record**::
3379
3380 AA: <2, VV1, VV2, 6>
3381
3382 **Semantics**:
3383
3384 The signed integer remainder instruction returns the remainder of the quotient
3385 of its two arguments. Arguments ``V1`` and ``V2``, and the result ``%vN``, must
3386 be of type ``T``. ``T`` must be a integer type, or an integer vector type. ``N``
3387 is defined by the record position, defining the corresponding value generated by
3388 the instruction.
3389
3390 Signed values are assumed. Note that signed and unsigned integer division are
3391 distinct operations. For unsigned integer division use the unsigned integer
3392 remainder instruction (urem).
3393
3394 In the signed integer remainder instruction, integer type ``i1`` (and a vector o f
3395 integer type ``i1``) is disallowed. Division by zero is guaranteed to trap.
3396
3397 Note that overflow can happen with this instruction when dividing the maximum
3398 negative integer by ``-1``. The behavior for this case is currently undefined.
3399
3400 **Constraints**::
3401
3402 AA == AbbrevIndex(A) &
3403 VV1 == RelativeIndex(V1) &
3404 VV2 == RelativeIndex(V2) &
3405 T == TypeOf(V1) == TypeOf(V2) &
3406 IsInteger(UnderlyingType(T)) &
3407 UnderlyingType(T) != i1 &
3408 N == NumValuedInsts
3409
3410 **Updates**::
3411
3412 ++NumValuedInsts;
3413 TypeOf(%vN) = T
3414
3415 **Examples**::
3416
3417 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3418 | | // BlockID = 12
3419 104:0| 3: <1, 1> | blocks 1;
3420 | | %b0:
3421 106:4| 3: <2, 2, 1, 6> | %v0 = srem i32 %p0, %p1;
3422 110:4| 3: <2, 1, 2, 6> | %v1 = srem i32 %v0, %p1;
3423 114:4| 3: <10, 1> | ret i32 %v1;
3424 117:0| 0: <65534> | }
3425
3426 Unsigned Integer Remainder Instruction
3427 --------------------------------------
3428
3429 The unsigned integer remainder instruction returns the remainder of the quotient
3430 of its two arguments. Both the arguments and the result must be of the same
3431 type. The type must be integer, or an integer vector type.
3432
3433 **Syntax**::
3434
3435 %vN = urem T V1, V2; <A>
3436
3437 **Record**::
3438
3439 AA: <2, A1, A2, 5>
3440
3441 **Semantics**:
3442
3443 The unsigned integer remainder instruction returns the remainder of the quotient
3444 of its two arguments. Arguments ``V1`` and ``V2``, and the result ``%vN``, must
3445 be of type ``T``. ``T`` must be an integer type, or an integer vector type.
3446 ``N`` is defined by the record position, defining the corresponding value
3447 generated by the instruction.
3448
3449 Unsigned values are assumed. Note that signed and unsigned integer division are
3450 distinct operations. For signed integer division use the remainder instruction
3451 (srem).
3452
3453 In the unsigned integer remainder instruction, integer type ``i1`` (and a vector of
3454 integer type ``i1``) is disallowed. Division by zero is guaranteed to trap.
3455
3456 **Constraints**::
3457
3458 AA == AbbrevIndex(A) &
3459 VV1 == RelativeIndex(V1) &
3460 VV2 == RelativeIndex(V2) &
3461 T == TypeOf(V1) == TypeOf(V2) &
3462 IsInteger(UnderlyingType(T)) &
3463 UnderlyingType(T) != i1 &
3464 N == NumValuedInsts
3465
3466 **Updates**::
3467
3468 ++NumValuedInsts;
3469 TypeOf(%vN) = T
3470
3471 **Examples**::
3472
3473 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3474 | | // BlockID = 12
3475 104:0| 3: <1, 1> | blocks 1;
3476 | | %b0:
3477 106:4| 3: <2, 2, 1, 5> | %v0 = urem i32 %p0, %p1;
3478 110:4| 3: <2, 1, 2, 5> | %v1 = urem i32 %v0, %p1;
3479 114:4| 3: <10, 1> | ret i32 %v1;
3480 117:0| 0: <65534> | }
3481
3482 Shift Left
3483 ----------
3484
3485 The (integer) shift left instruction returns the first operand, shifted to the
3486 left a specified number of bits with zero fill. The shifted value must be
3487 integer, or an integer vector type.
3488
3489 **Syntax**::
3490
3491 %vN = shl T V1, V2; <A>
3492
3493 **Record**::
3494
3495 AA: <2, VV1, VV2, 7>
3496
3497 **Semantics**:
3498
3499 This instruction performs a shift left operation. Arguments ``V1`` and ``V2``
3500 and the result ``%vN`` must be of type ``T``. ``T`` must be an integer, or a
3501 vector of integers. ``N`` is defined by the record position, defining the
3502 corresponding value generated by the instruction.
3503
3504 ``V2`` is assumed to be unsigned. The least significant bits of the result will
3505 be filled with zero bits after the shift. If ``V2`` is (statically or
3506 dynamically) negative or equal to or larger than the number of bits in
3507 ``V1``, the result is undefined. If the arguments are vectors, each vector
3508 element of ``V1`` is shifted by the corresponding shift amount in ``V2``.
3509
3510 In the shift left instruction, integer type ``i1`` (and a vector of integer type
3511 ``i1``) is disallowed.
3512
3513 **Constraints**::
3514
3515 AA == AbbrevIndex(A) &
3516 VV1 == RelativeIndex(V1) &
3517 VV2 == RelativeIndex(V2) &
3518 T == TypeOf(V1) == TypeOf(V2) &
3519 IsInteger(UnderlyingType(T)) &
3520 UnderlyingType(T) != i1 &
3521 N == NumValuedInsts
3522
3523 **Updates**::
3524
3525 ++NumValuedInsts;
3526 TypeOf(%vN) = T
3527
3528 **Examples**::
3529
3530 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3531 | | // BlockID = 12
3532 104:0| 3: <1, 1> | blocks 1;
3533 | | %b0:
3534 106:4| 3: <2, 2, 1, 7> | %v0 = shl i32 %p0, %p1;
3535 110:4| 3: <2, 1, 2, 7> | %v1 = shl i32 %v0, %p1;
3536 114:4| 3: <10, 1> | ret i32 %v1;
3537 117:0| 0: <65534> | }
3538
3539 Logical Shift Right
3540 -------------------
3541
3542 The logical shift right instruction returns the first operand, shifted to the
3543 right a specified number of bits with zero fill.
3544
3545 **Syntax**::
3546
3547 %vN = lshr T V1, V2; <A>
3548
3549 **Record**::
3550
3551 AA: <2, VV1, VV2, 8>
3552
3553 **Semantics**:
3554
3555 This instruction performs a logical shift right operation. Arguments ``V1`` and
3556 ``V2`` and the result ``%vN`` must be of type ``T``. ``T`` must be an integer,
3557 or a vector of integers. ``N`` is defined by the record position, defining the
3558 corresponding value generated by the instruction.
3559
3560 ``V2`` is assumed to be unsigned. The most significant bits of the result will b e
3561 filled with zero bits after the shift. If ``V2`` is (statically or dynamically)
3562 negative or equal to or larger than the number of bits in ``V1``, the result is
3563 undefined. If the arguments are vectors, each vector element of ``V1`` is shifte d
3564 by the corresponding shift amount in ``V2``.
3565
3566 In the logical shift right instruction, integer type ``i1`` (and a vector of
3567 integer type ``i1``) is disallowed.
3568
3569 **Constraints**::
3570
3571 AA == AbbrevIndex(A) &
3572 VV1 == RelativeIndex(V1) &
3573 VV2 == RelativeIndex(V2) &
3574 T == TypeOf(V1) == TypeOf(V2) &
3575 IsInteger(UnderlyingType(T)) &
3576 UnderlyingType(T) != i1 &
3577 N == NumValuedInsts
3578
3579 **Updates**::
3580
3581 ++NumValuedInsts;
3582 TypeOf(%vN) = T
3583
3584 **Examples**::
3585
3586 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3587 | | // BlockID = 12
3588 104:0| 3: <1, 1> | blocks 1;
3589 | | %b0:
3590 106:4| 3: <2, 2, 1, 8> | %v0 = lshr i32 %p0, %p1;
3591 110:4| 3: <2, 1, 2, 8> | %v1 = lshr i32 %v0, %p1;
3592 114:4| 3: <10, 1> | ret i32 %v1;
3593 117:0| 0: <65534> | }
3594
3595 Arithmetic Shift Right
3596 ----------------------
3597
3598 The arithmetic shift right instruction returns the first operand, shifted to the
3599 right a specified number of bits with sign extension.
3600
3601 **Syntax**::
3602
3603 %vN = ashr T V1, V2; <A>
3604
3605 **Record**::
3606
3607 AA: <2, VV1, VVA2, 9>
3608
3609 **Semantics**:
3610
3611 This instruction performs an arithmetic shift right operation. Arguments ``V1``
3612 and ``V2`` and and the result ``%vN`` must be of type ``T``. ``T`` must be an
3613 integer, or a vector of integers. ``N`` is defined by the record position,
3614 defining the corresponding value generated by the instruction.
3615
3616 ``V2`` is assumed to be unsigned. The most significant bits of the result will b e
3617 filled with the sign bit of ``V1``. If ``V2`` is (statically or dynamically)
3618 negative or equal to or larger than the number of bits in ``V1``, the result is
3619 undefined. If the arguments are vectors, each vector element of ``V1`` is shifte d
3620 by the corresponding shift amount in ``V2``.
3621
3622 In the arithmetic shift right instruction, integer type ``i1`` (and a vector of
3623 integral type ``i1``) is disallowed.
Jim Stichnoth 2014/11/18 02:24:42 Hmm, the html version says "integrl" here, so I gu
Karl 2014/11/19 20:28:52 Will regenerate to verify.
3624
3625 **Constraints**::
3626
3627 AA == AbbrevIndex(A) &
3628 VV1 == RelativeIndex(V1) &
3629 VV2 == RelativeIndex(V2) &
3630 T == TypeOf(V1) == TypeOf(V2) &
3631 IsInteger(UnderlyingType(T)) &
3632 UnderlyingType(T) != i1 &
3633 N == NumValuedInsts
3634
3635 **Updates**::
3636
3637 ++NumValuedInsts;
3638 TypeOf(%vN) = T
3639
3640 **Examples**::
3641
3642 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3643 | | // BlockID = 12
3644 104:0| 3: <1, 1> | blocks 1;
3645 | | %b0:
3646 106:4| 3: <2, 2, 1, 9> | %v0 = ashr i32 %p0, %p1;
3647 110:4| 3: <2, 1, 2, 9> | %v1 = ashr i32 %v0, %p1;
3648 114:4| 3: <10, 1> | ret i32 %v1;
3649 117:0| 0: <65534> | }
3650
3651 Logical And
3652 -----------
3653
3654 The *and* instruction returns the bitwise logical and of its two operands.
3655
3656 **Syntax**::
3657
3658 %vN = and T V1, V2; <A>
3659
3660 **Record**::
3661
3662 AA: <2, VV1, VV2, 10>
3663
3664 **Semantics**:
3665
3666 This instruction performs a bitwise logical and of its arguments. Arguments
3667 ``V1`` and ``V2``, and the result ``%vN`` must be of type ``T``. ``T`` must be a n
3668 integer, or a vector of integers. ``N`` is defined by the record position,
3669 defining the corresponding value generated by the instruction. ``A`` is the
3670 (optional) abbreviation associated with the corresponding record.
3671
3672 The truth table used for the *and* instruction is:
3673
3674 ===== ===== ======
3675 Arg 1 Arg 2 Result
3676 ===== ===== ======
3677 0 0 0
3678 0 1 0
3679 1 0 0
3680 1 1 1
3681 ===== ===== ======
3682
3683 **Constraints**::
3684
3685 AA == AbbrevIndex(A) &
3686 VV1 == RelativeIndex(V1) &
3687 VV2 == RelativeIndex(V2) &
3688 T == TypeOf(V1) == TypeOf(V2) &
3689 IsInteger(UnderlyingType(T))) &
3690 N == NumValuedInsts
3691
3692 **Updates**::
3693
3694 ++NumValuedInsts;
3695 TypeOf(%vN) = T
3696
3697 **Examples**::
3698
3699 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3700 | | // BlockID = 12
3701 104:0| 3: <1, 1> | blocks 1;
3702 | | %b0:
3703 106:4| 3: <2, 2, 1, 10> | %v0 = and i32 %p0, %p1;
3704 110:4| 3: <2, 1, 2, 10> | %v1 = and i32 %v0, %p1;
3705 114:4| 3: <10, 1> | ret i32 %v1;
3706 117:0| 0: <65534> | }
3707
3708 Logical Or
3709 ----------
3710
3711 The *or* instruction returns the bitwise logical inclusive or of its
3712 two operands.
3713
3714 **Syntax**::
3715
3716 %vN = or T V1, V2; <A>
3717
3718 **Record**::
3719
3720 AA: <2, VV1, VV2, 11>
3721
3722 **Semantics**:
3723
3724 This instruction performs a bitwise logical inclusive or of its arguments.
3725 Arguments ``V1`` and ``V2``, and the result ``%vN`` must be of type ``T``. ``T``
3726 must be an integer, or a vector of integers. ``N`` is defined by the record
3727 position, defining the corresponding value generated by the instruction.
3728
3729 The truth table used for the *or* instruction is:
3730
3731 ===== ===== ======
3732 Arg 1 Arg 2 Result
3733 ===== ===== ======
3734 0 0 0
3735 0 1 1
3736 1 0 1
3737 1 1 1
3738 ===== ===== ======
3739
3740 **Constraints**::
3741
3742 AA == AbbrevIndex(A) &
3743 VV1 == RelativeIndex(V1) &
3744 VV2 == RelativeIndex(V2) &
3745 T == TypeOf(V1) == TypeOf(V2) &
3746 IsInteger(UnderlyingType(T))) &
3747 N == NumValuedInsts
3748
3749 **Updates**::
3750
3751 ++NumValuedInsts;
3752 TypeOf(%vN) = T
3753
3754 **Examples**::
3755
3756 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3757 | | // BlockID = 12
3758 104:0| 3: <1, 1> | blocks 1;
3759 | | %b0:
3760 106:4| 3: <2, 2, 1, 11> | %v0 = or i32 %p0, %p1;
3761 110:4| 3: <2, 1, 2, 11> | %v1 = or i32 %v0, %p1;
3762 114:4| 3: <10, 1> | ret i32 %v1;
3763 117:0| 0: <65534> | }
3764
3765 Logical Xor
3766 -----------
3767
3768 The *xor* instruction returns the bitwise logical exclusive or of its
3769 two operands.
3770
3771 **Syntax**::
3772
3773 %vN = xor T V1, V2; <A>
3774
3775 **Record**::
3776
3777 AA: <2, VV1, VV2, 12>
3778
3779 **Semantics**:
3780
3781 This instruction performs a bitwise logical exclusive or of its arguments.
3782 Arguments ``V1`` and ``V2``, and the result ``%vN`` must be of type ``T``. ``T``
3783 must be an integer, or a vector of integers. ``N`` is defined by the record
3784 position, defining the corresponding value generated by the instruction.
3785
3786 The truth table used for the *or* instruction is:
Jim Stichnoth 2014/11/18 02:24:42 xor
Karl 2014/11/19 20:28:53 Done.
3787
3788 ===== ===== ======
3789 Arg 1 Arg 2 Result
3790 ===== ===== ======
3791 0 0 0
3792 0 1 1
3793 1 0 1
3794 1 1 0
3795 ===== ===== ======
3796
3797 **Constraints**::
3798
3799 AA == AbbrevIndex(A) &
3800 A1 == RelativeIndex(V1) &
3801 A2 == RelativeIndex(V2) &
3802 T == TypeOf(V1) == TypeOf(V2) &
3803 IsInteger(UnderlyingType(T))) &
3804 N == NumValuedInsts
3805
3806 **Updates**::
3807
3808 ++NumValuedInsts;
3809 TypeOf(%vN) = T
3810
3811 **Examples**::
3812
3813 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
3814 | | // BlockID = 12
3815 104:0| 3: <1, 1> | blocks 1;
3816 | | %b0:
3817 106:4| 3: <2, 2, 1, 12> | %v0 = xor i32 %p0, %p1;
3818 110:4| 3: <2, 1, 2, 12> | %v1 = xor i32 %v0, %p1;
3819 114:4| 3: <10, 1> | ret i32 %v1;
3820 117:0| 0: <65534> | }
3821
3822 .. _link_for_floating_point_binary_instructions:
3823
3824 Floating Point Binary Instructions
3825 ==================================
3826
3827 Floating point binary instructions require two operands of the same type,
3828 execute an operation on them, and produce a value. The value may represent
3829 multiple values if the type is a vector type. The result value always has the
3830 same type as its operands.
3831
3832 Floating Point Add
3833 ------------------
3834
3835 The floating point add instruction returns the sum of its two arguments. Both
3836 arguments and the result must be of the same type. That type must be a floating
3837 point type, or a vector of a floating point type.
3838
3839 **Syntax**::
3840
3841 %vN = fadd T V1, V2; <A>
3842
3843 **Record**::
3844
3845 AA: <2, VV1, VV2, 0>
3846
3847 **Semantics**:
3848
3849 The floating point add instruction returns the sum of its two arguments.
3850 Arguments ``V1`` and ``V2`` and the result ``%vN`` must be of type ``T``. ``T``
3851 must be a floating point type, or a vector of a floating point type. ``N`` is
3852 defined by the record position, defining the corresponding value generated by
3853 the instruction.
3854
3855 **Constraints**::
3856
3857 AA == AbbrevIndex(A) &
3858 VV1 == RelativeIndex(V1) &
3859 VV2 == RelativeIndex(V2) &
3860 T == TypeOf(V1) == TypeOf(V2) &
3861 IsFloat(UnderlyingType(T)) &
3862 N == NumValuedInsts
3863
3864 **Updates**::
3865
3866 ++NumValuedInsts;
3867 TypeOf(%vN) = T
3868
3869 **Examples**::
3870
3871 92:0| 1: <65535, 12, 2> | function
3872 | | float @f0(float %p0, float %p1) {
3873 | | // BlockID = 12
3874 100:0| 3: <1, 1> | blocks 1;
3875 | | %b0:
3876 102:4| 3: <2, 2, 1, 0> | %v0 = fadd float %p0, %p1;
3877 106:4| 3: <2, 3, 1, 0> | %v1 = fadd float %p0, %v0;
3878 110:4| 3: <10, 1> | ret float %v1;
3879 113:0| 0: <65534> | }
3880
3881 Floating Point Subtract
3882 -----------------------
3883
3884 The floating point subtract instruction returns the difference of its two
3885 arguments. Both arguments and the result must be of the same type. That type
3886 must be a floating point type, or a vector of a floating point type.
3887
3888 **Syntax**::
3889
3890 %vN = fsub T V1, V2; <a>
3891
3892 **Record**::
3893
3894 AA: <2, VV1, VV2, 1>
3895
3896 **Semantics**:
3897
3898 The floating point subtract instruction returns the difference of its two
3899 arguments. Arguments ``V1`` and ``V2``, and the result ``%vN`` must be of type
3900 ``T``. ``T`` must be a floating point type, or a vector of a floating point
3901 type. ``N`` is defined by the record position, defining the corresponding value
3902 generated by the instruction.
3903
3904 **Constraints**::
3905
3906 AA == AbbrevIndex(A) &
3907 VV1 == RelativeIndex(V1) &
3908 VV2 == RelativeIndex(V2) &
3909 T == TypeOf(V1) == TypeOf(V2) &
3910 IsFloat(UnderlyingType(T)) &
3911 N == NumValuedInsts
3912
3913 **Updates**::
3914
3915 ++NumValuedInsts;
3916 TypeOf(%vN) = T
3917
3918 **Examples**::
3919
3920 92:0| 1: <65535, 12, 2> | function
3921 | | float @f0(float %p0, float %p1) {
3922 | | // BlockID = 12
3923 100:0| 3: <1, 1> | blocks 1;
3924 | | %b0:
3925 102:4| 3: <2, 2, 1, 1> | %v0 = fsub float %p0, %p1;
3926 106:4| 3: <2, 3, 1, 1> | %v1 = fsub float %p0, %v0;
3927 110:4| 3: <10, 1> | ret float %v1;
3928 113:0| 0: <65534> | }
3929
3930 Floating Point Multiply
3931 -----------------------
3932
3933 The floating point multiply instruction returns the product of its two
3934 arguments. Both arguments and the result must be of the same type. That type
3935 must be a floating point type, or a vector of a floating point type.
3936
3937 **Syntax**::
3938
3939 &vN = fmul T V1, V2; <A>
3940
3941 **Record**::
3942
3943 AA: <2, VV1, VV2, 2>
3944
3945 **Semantics**:
3946
3947 The floating point multiply instruction returns the product of its two
3948 arguments. Arguments ``V1`` and ``V2``, and the result ``%vN`` must be of type
3949 ``T``. ``T`` must be a floating point type, or a vector of a floating point
3950 type. ``N`` is defined by the record position, defining the corresponding value
3951 generated by the instruction.
3952
3953 **Constraints**::
3954
3955 AA == AbbrevIndex(A) &
3956 VV1 == RelativeIndex(V1) &
3957 VV2 == RelativeIndex(V2) &
3958 T == TypeOf(V1) == TypeOf(V2) &
3959 IsFloat(UnderlyingType(T)) &
3960 N == NumValuedInsts
3961
3962 **Updates**::
3963
3964 ++NumValuedInsts;
3965 TypeOf(%vN) = T
3966
3967 **Examples**::
3968
3969 92:0| 1: <65535, 12, 2> | function
3970 | | float @f0(float %p0, float %p1) {
3971 | | // BlockID = 12
3972 100:0| 3: <1, 1> | blocks 1;
3973 | | %b0:
3974 102:4| 3: <2, 2, 1, 2> | %v0 = fmul float %p0, %p1;
3975 106:4| 3: <2, 3, 1, 2> | %v1 = fmul float %p0, %v0;
3976 110:4| 3: <10, 1> | ret float %v1;
3977 113:0| 0: <65534> | }
3978
3979 Floating Point Divide
3980 ---------------------
3981
3982 The floating point divide instruction returns the quotient of its two
3983 arguments. Both arguments and the result must be of the same type. That type
3984 must be a floating point type, or a vector of a floating point type.
3985
3986 **Syntax**::
3987
3988 %vN = fdiv T V1, V2; <A>
3989
3990 **Record**::
3991
3992 AA: <2, V1, V2, 4>
3993
3994 **Semantics**:
3995
3996 The floating point divide instruction returns the quotient of its two
3997 arguments. Arguments ``V1`` and ``V2``, and the result ``%vN`` must be of type
3998 ``T``. ``T`` must be a floating point type, or a vector of a floating point
3999 type. ``N`` is defined by the record position, defining the corresponding value
4000 generated by the instruction.
4001
4002 **Constraints**::
4003
4004 AA == AbbrevIndex(A) &
4005 VV1 == RelativeIndex(V1) &
4006 VV22 == RelativeIndex(V2) &
4007 T == TypeOf(V1) == TypeOf(V2) &
4008 IsFloat(UnderlyingType(T)) &
4009 N == NumValuedInsts
4010
4011 **Updates**::
4012
4013 ++NumValuedInsts;
4014 TypeOf(%vN) = T;
4015
4016 **Examples**::
4017
4018 92:0| 1: <65535, 12, 2> | function
4019 | | double
4020 | | @f0(double %p0, double %p1) {
4021 | | // BlockID = 12
4022 100:0| 3: <1, 1> | blocks 1;
4023 | | %b0:
4024 102:4| 3: <2, 2, 1, 4> | %v0 = fdiv double %p0, %p1;
4025 106:4| 3: <2, 3, 1, 4> | %v1 = fdiv double %p0, %v0;
4026 110:4| 3: <10, 1> | ret double %v1;
4027 113:0| 0: <65534> | }
4028
4029 Floating Point Remainder
4030 ------------------------
4031
4032 The floating point remainder instruction returns the remainder of the quotient
4033 of its two arguments. Both arguments and the result must be of the same
4034 type. That type must be a floating point type, or a vector of a floating point
4035 type.
4036
4037 **Syntax**::
4038
4039 %vN = frem T V1, V2; <A>
4040
4041 **Record**::
4042
4043 AA: <2, VV1, VV2, 6>
4044
4045 **Semantics**:
4046
4047 The floating point remainder instruction returns the remainder of the quotient
4048 of its two arguments. Arguments ``V1`` and ``V2``, and the result ``%vN`` must b e of
4049 type ``T``. ``T`` must be a floating point type, or a vector of a floating point
4050 type. ``N`` is defined by the record position, defining the corresponding value
4051 generated by the instruction.
4052
4053 **Constraints**::
4054
4055 AA == AbbrevIndex(A) &
4056 VV1 == RelativeIndex(V1) &
4057 VV2 == RelativeIndex(V2) &
4058 T == TypeOf(V1) == TypeOf(V2) &
4059 IsFloat(UnderlyingType(T)) &
4060 N == NumValuedInsts
4061
4062 **Updates**::
4063
4064 ++NumValuedInsts;
4065 TypeOf(%vN) = T
4066
4067 **Examples**::
4068
4069 92:0| 1: <65535, 12, 2> | function
4070 | | double
4071 | | @f0(double %p0, double %p1) {
4072 | | // BlockID = 12
4073 100:0| 3: <1, 1> | blocks 1;
4074 | | %b0:
4075 102:4| 3: <2, 2, 1, 6> | %v0 = frem double %p0, %p1;
4076 106:4| 3: <2, 3, 1, 6> | %v1 = frem double %p0, %v0;
4077 110:4| 3: <10, 1> | ret double %v1;
4078 113:0| 0: <65534> | }
4079
4080 .. _link_for_memory_creation_and_access_instructions:
4081
4082 Memory Creation and Access Instructions
4083 =======================================
4084
4085 A key design point of SSA-based representation is how it represents
4086 memory. In PNaCl bitcode files, no memory locations are in SSA
4087 form. This makes things very simple.
4088
4089 .. _link_for_alloca_instruction:
4090
4091 Alloca Instruction
4092 ------------------
4093
4094 The *alloca* instruction allocates memory on the stack frame of the
4095 currently executing function. This memory is automatically released
4096 when the function returns to its caller.
4097
4098 **Syntax**::
4099
4100 %vN = alloca i8, i32 S, align V; <A>
4101
4102 **Record**::
4103
4104 AA: <19, SS, VV>
4105
4106 **Semantics**:
4107
4108 The *alloca* instruction allocates memory on the stack frame of the currently
4109 executing function. The resulting value is a pointer to the allocated memory
4110 (i.e. of type i32). ``S`` is the number of bytes that are allocated on the
4111 stack. ``S`` must be of integer type i32. ``V`` is the alignment of the generate d
4112 stack address.
4113
4114 Alignment must be a power of 2. See :ref:`memory blocks and
4115 alignment<link_for_memory_blocks_and_alignment_section>` for a more detailed
4116 discussion on how to define alignment.
4117
4118 **Constraints**::
4119
4120 AA == AbbrevIndex(A) &
4121 VV == Log2(V+1) &
4122 SS == RelativeIndex(S) &
4123 i32 == TypeOf(S) &
4124 N == NumValuedInsts
4125
4126 **Updates**::
4127
4128 ++NumValuedInsts;
4129 TypeOf(%vN) = i32;
4130
4131 **Examples**::
4132
4133 112:0| 1: <65535, 12, 2> | function void @f1() {
4134 | | // BlockID = 12
4135 120:0| 3: <1, 1> | blocks 1;
4136 122:4| 1: <65535, 11, 2> | constants { // BlockID = 11
4137 132:0| 3: <1, 0> | i32:
4138 134:4| 3: <4, 4> | %c0 = i32 2;
4139 137:0| 3: <4, 8> | %c1 = i32 4;
4140 139:4| 3: <4, 16> | %c2 = i32 8;
4141 142:0| 0: <65534> | }
4142 | | %b0:
4143 144:0| 3: <19, 3, 1> | %v0 = alloca i8, i32 %c0, align 1;
4144 147:2| 3: <19, 3, 3> | %v1 = alloca i8, i32 %c1, align 4;
4145 150:4| 3: <19, 3, 4> | %v2 = alloca i8, i32 %c2, align 8;
4146 153:6| 3: <10> | ret void;
4147 155:4| 0: <65534> | }
4148
4149 Load Instruction
4150 ----------------
4151
4152 The *load* instruction is used to read from memory.
4153
4154 **Syntax**::
4155
4156 %vN = load T* P, align V; <A>
4157
4158 **Record**::
4159
4160 AA: <20, PP, VV, TT>
4161
4162 **Semantics**:
4163
4164 The load instruction is used to read from memory. ``P`` is the identifier of the
4165 memory address to read. The type of ``P`` must be an ``i32``. ``T`` is the type
4166 of value to read. ``V`` is the alignment of the memory address.
4167
4168 Type ``T`` must be a vector, integer, or floating point type. Both ``float`` and
4169 ``double`` types are allowed for floating point types. All integer types except i1
4170 are allowed.
4171
4172 Alignment must be a power of 2. See :ref:`memory blocks and
4173 alignment<link_for_memory_blocks_and_alignment_section>` for a more detailed
4174 discussion on how to define alignment.
4175
4176 **Constraints**::
4177
4178 AA == AbbrevIndex(A) &
4179 i32 == TypeOf(P) &
4180 PP == RelativeIndex(P) &
4181 VV == Log2(V+1) &
4182 %tTT == TypeID(T) &
4183 N == NumValuedInsts
4184
4185 **Updates**::
4186
4187 ++NumValuedInsts;
4188 TypeOf(%vN) = T;
4189
4190 **Examples**::
4191
4192 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4193 48:0| 3: <1, 4> | count 4;
4194 50:4| 3: <7, 32> | @t0 = i32;
4195 53:6| 3: <2> | @t1 = void;
4196 55:4| 3: <4> | @t2 = double;
4197 57:2| 3: <21, 0, 1, 0> | @t3 = void (i32);
4198 61:2| 0: <65534> | }
4199 ...
4200 96:0| 1: <65535, 12, 2> | function void @f0(i32 %p0) {
4201 | | // BlockID = 12
4202 104:0| 3: <1, 1> | blocks 1;
4203 | | %b0:
4204 106:4| 3: <20, 1, 1, 0> | %v0 = load i32* %p0, align 1;
4205 110:4| 3: <20, 1, 4, 2> | %v1 = load double* %v0, align 8;
4206 114:4| 3: <10> | ret void;
4207 116:2| 0: <65534> | }
4208
4209 Store Instruction
4210 -----------------
4211
4212 The *store* instruction is used to write to memory.
4213
4214 **Syntax**::
4215
4216 store T S, T* P, align V; <A>
4217
4218 **Record**::
4219
4220 AA: <24, PP, SS, VV>
4221
4222 **Semantics**:
4223
4224 The store instruction is used to write to memory. ``P`` is the identifier of the
4225 memory address to write to. The type of ``P`` must be an i32 integer. ``T`` is
4226 the type of value to store. ``S`` is the value to store, and must be of type
4227 ``T``. ``V`` is the alignment of the memory address. ``A`` is the (optional)
4228 abbreviation index associated with the record.
4229
4230 Type ``T`` must be an integer or floating point type. Both ``float`` and
4231 ``double`` types are allowed for floating point types. All integer types except
4232 i1 are allowed.
4233
4234 Alignment must be a power of 2. See :ref:`memory blocks and
4235 alignment<link_for_memory_Blocks_and_alignment_section>` for a more detailed
4236 discussion on how to define alignment.
4237
4238 **Constraints**::
4239
4240 AA == AbbrevIndex(A) &
4241 i32 == TypeOf(P) &
4242 PP == RelativeIndex(P) &
4243 VV == Log2(V+1)
4244
4245 **Examples**::
4246
4247 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4248 48:0| 3: <1, 4> | count 4;
4249 50:4| 3: <7, 32> | @t0 = i32;
4250 53:6| 3: <2> | @t1 = void;
4251 55:4| 3: <4> | @t2 = double;
4252 57:2| 3: <21, 0, 1, 0, 0, 0, 2>| @t3 = void (i32, i32, i32, double);
4253 63:4| 0: <65534> | }
4254 ...
4255 96:0| 1: <65535, 12, 2> | function
4256 | | void
4257 | | @f0(i32 %p0, i32 %p1, i32 %p2,
4258 | | double %p3) {
4259 | | // BlockID = 12
4260 104:0| 3: <1, 1> | blocks 1;
4261 | | %b0:
4262 106:4| 3: <24, 4, 3, 1> | store i32 %p1, i32* %p0, align 1;
4263 110:4| 3: <24, 2, 1, 4> | store double %p3, double* %p2,
4264 | | align 8;
4265 114:4| 3: <10> | ret void;
4266 116:2| 0: <65534> | }
4267
4268 .. _link_for_conversion_instructions:
4269
4270 Conversion Instructions
4271 =======================
4272
4273 Conversion instructions all take a single operand and a type. The value is
4274 converted to the corresponding type.
4275
4276 Integer Truncating Instruction
4277 ------------------------------
4278
4279 The integer truncating instruction takes a value to truncate, and a type
4280 defining the truncated type. Both types must be integer types, or integer
4281 vectors with the same number of elements. The bit size of the value must be
4282 larger than the bit size of the destination type. Equal sized types are not
4283 allowed.
4284
4285 **Syntax**::
4286
4287 %vN = trunc T1 V to T2; <A>
4288
4289 **Record**::
4290
4291 AA: <3, VV, TT2, 0>
4292
4293 **Semantics**:
4294
4295 The integer truncating instruction takes a value ``V``, and truncates to type
4296 ``T2``. Both ``T1`` and ``T2`` must be integer types, or integer vectors with th e
4297 same number of elements. ``T1`` has to be wider than ``T2``. If the value doesn 't
4298 fit in in ``T2``, then the higher order bits are dropped.
4299
4300 **Constraints**::
4301
4302 AA == AbbrevIndex(A) &
4303 TypeOf(V) == T1 &
4304 VV == RelativeIndex(V) &
4305 %tTT2 == TypeID(T2) &
4306 BitSizeOf(UnderlyingType(T1)) > BitSizeOf(UnderlyingType(T2)) &
4307 UnderlyingCount(T1) == UnderlyingCount(T2) &
4308 IsInteger(UnderlyingType(T1)) &
4309 IsInteger(UnderlyingType(T2)) &
4310 N == NumValuedInsts
4311
4312 **Updates**::
4313
4314 ++NumValuedInsts;
4315 TypeOf(%vN) = T2;
4316
4317 **Examples**::
4318
4319 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4320 48:0| 3: <1, 5> | count 5;
4321 50:4| 3: <7, 32> | @t0 = i32;
4322 53:6| 3: <2> | @t1 = void;
4323 55:4| 3: <7, 16> | @t2 = i16;
4324 58:0| 3: <21, 0, 1, 0> | @t3 = void (i32);
4325 62:0| 3: <7, 8> | @t4 = i8;
4326 64:4| 0: <65534> | }
4327 ...
4328 100:0| 1: <65535, 12, 2> | function void @f0(i32 %p0) {
4329 | | // BlockID = 12
4330 108:0| 3: <1, 1> | blocks 1;
4331 | | %b0:
4332 110:4| 3: <3, 1, 2, 0> | %v0 = trunc i32 %p0 to i16;
4333 114:4| 3: <3, 1, 4, 0> | %v1 = trunc i16 %v0 to i8;
4334 118:4| 3: <10> | ret void;
4335 120:2| 0: <65534> | }
4336
4337 Floating Point Truncating Instruction
4338 --------------------------------------
4339
4340 The floating point truncating instruction takes a value to truncate, and a type
4341 defining the truncated type. Both types must be floating point types, or
4342 floating point vectors with the same number of elements. The source must be
4343 ``double`` while the destination is ``float``. If the source is a vector, the
4344 destination must also be vector with the same size as the source.
4345
4346 **Syntax**::
4347
4348 %vN = fptrunc T1 V to T2; <A>
4349
4350 **Record**::
4351
4352 AA: <3, VV, TT2, 7>
4353
4354 **Semantics**
4355
4356 The floating point truncating instruction takes a value ``V``, and truncates to
4357 type ``T2``. Both ``T1`` and ``T2`` must be floating point types, or floating
4358 point vectors with the same number of elements. ``T1`` must be defined on
4359 ``double`` while ``T2`` is defined on ``float``. If the value can't fit within
4360 the destination type ``T2``, the results are undefined.
4361
4362 **Constraints**::
4363
4364 TypeOf(V) == T1 &
4365 double == UnderlyingType(T1) &
4366 float == UnderlyingType(T2) &
4367 VV == RelativeIndex(V) &
4368 %tTT2 == TypeID(T2) &
4369 BitSizeOf(UnderlyingType(T1)) > BitSizeOf(UnderlyingType(T2)) &
4370 UnderlyingCount(T1) == UnderlyingCount(T2) &
4371 IsFloat(UnderlyingType(T1)) &
4372 IsFloat(UnderlyingType(T2)) &
4373 N == NumValuedInsts
4374
4375 **Updates**::
4376
4377 ++NumValuedInsts;
4378 TypeOf(%vN) = T2;
4379
4380 **Examples**::
4381
4382 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4383 48:0| 3: <1, 4> | count 4;
4384 50:4| 3: <3> | @t0 = float;
4385 52:2| 3: <4> | @t1 = double;
4386 54:0| 3: <21, 0, 0, 1> | @t2 = float (double);
4387 58:0| 3: <2> | @t3 = void;
4388 59:6| 0: <65534> | }
4389 ...
4390 92:0| 1: <65535, 12, 2> | function float @f0(double %p0) {
4391 | | // BlockID = 12
4392 100:0| 3: <1, 1> | blocks 1;
4393 | | %b0:
4394 102:4| 3: <3, 1, 0, 7> | %v0 = fptrunc double %p0 to float;
4395 106:4| 3: <10, 1> | ret float %v0;
4396 109:0| 0: <65534> | }
4397
4398
4399 Zero Extending Instruction
4400 --------------------------
4401
4402 The zero extending instruction takes a value to extend, and a type to extend it
4403 to. Both types must be integer types, or integer vectors with the same number
4404 of elements. The bit size of the source type must be smaller than the bit size
4405 of the destination type. Equal sized types are not allowed.
4406
4407 **Syntax**::
4408
4409 %vN = zext T1 V to T2; <A>
4410
4411 **Record**::
4412
4413 AA: <3, VV, TT2, 1>
4414
4415
4416 **Semantics**:
4417
4418 The zero extending instruction takes a value ``V``, and expands it to type
4419 ``T2``. Both ``T1`` and ``T2`` must be integer types, or integer vectors with th e
4420 same number of elements. ``T2`` must be wider than ``T1``.
4421
4422 The instruction fills the high order bits of the value with zero bits until it
4423 reaches the size of the destination type. When zero extending from i1, the
4424 result will always be either 0 or 1.
4425
4426 **Constraints**::
4427
4428 AA == AbbrevIndex(A) &
4429 TypeOf(V) == T1 &
4430 VV == RelativeIndex(V) &
4431 %tTT2 == TypeID(T2) &
4432 BitSizeOf(UnderlyingType(T1)) < BitSizeOf(UnderlyingType(T2)) &
4433 UnderlyingCount(T1) == UnderlyingCount(T2) &
4434 IsInteger(UnderlyingType(T1)) &
4435 IsInteger(UnderlyingType(T2)) &
4436 N == NumValuedInsts
4437
4438 **Updates**::
4439
4440 ++NumValuedInsts;
4441 TypeOf(%vN) = T2;
4442
4443 **Examples**::
4444
4445 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4446 48:0| 3: <1, 5> | count 5;
4447 50:4| 3: <7, 64> | @t0 = i64;
4448 53:6| 3: <7, 32> | @t1 = i32;
4449 57:0| 3: <21, 0, 0> | @t2 = i64 ();
4450 60:2| 3: <7, 8> | @t3 = i8;
4451 62:6| 3: <2> | @t4 = void;
4452 64:4| 0: <65534> | }
4453 ...
4454 100:0| 1: <65535, 12, 2> | function i64 @f0() { // BlockID = 12
4455 108:0| 3: <1, 1> | blocks 1;
4456 110:4| 1: <65535, 11, 2> | constants { // BlockID = 11
4457 120:0| 3: <1, 3> | i8:
4458 122:4| 3: <4, 2> | %c0 = i8 1;
4459 125:0| 0: <65534> | }
4460 | | %b0:
4461 128:0| 3: <3, 1, 1, 1> | %v0 = zext i8 %c0 to i32;
4462 132:0| 3: <3, 1, 0, 1> | %v1 = zext i32 %v0 to i64;
4463 136:0| 3: <10, 1> | ret i64 %v1;
4464 138:4| 0: <65534> | }
4465
4466 Sign Extending Instruction
4467 --------------------------
4468
4469 The sign extending instruction takes a value to cast, and a type to extend it
4470 to. Both types must be integer types, or integral vectors with the same number
4471 of elements. The bit size of the source type must be smaller than the bit size
4472 of the destination type. Equal sized types are not allowed.
4473
4474 **Syntax**::
4475
4476 %vN = sext T1 V to T2; <A>
4477
4478 **Record**::
4479
4480 AA: <3, VV, TT2, 2>
4481
4482 **Semantics**:
4483
4484 The sign extending instruction takes a value ``V``, and expands it to type
4485 ``T2``. Both ``T1`` and ``T2`` must be integer types, or integer vectors with th e
4486 same number of integers. ``T2`` has to be wider than ``T1``.
4487
4488 When sign extending, the instruction fills the high order bits of the value with
4489 the (current) high order bit of the value. When sign extending from i1, the
4490 extension always results in -1 or 0.
4491
4492 **Constraints**::
4493
4494 AA == AbbrevIndex(A) &
4495 TypeOf(V) == T1 &
4496 VV == RelativeIndex(V) &
4497 %tTT2 == TypeID(T2) &
4498 BitSizeOf(UnderlyingType(T1)) < BitSizeOf(UnderlyingType(T2)) &
4499 UnderlyingCount(T1) == UnderlyingCount(T2) &
4500 IsInteger(UnderlyingType(T1)) &
4501 IsInteger(UnderlyingType(T2)) &
4502 N == NumValuedInsts
4503
4504 **Updates**::
4505
4506 ++NumValuedInsts;
4507 TypeOf(%vN) = T2;
4508
4509 **Examples**::
4510
4511 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4512 48:0| 3: <1, 5> | count 5;
4513 50:4| 3: <7, 64> | @t0 = i64;
4514 53:6| 3: <7, 32> | @t1 = i32;
4515 57:0| 3: <21, 0, 0> | @t2 = i64 ();
4516 60:2| 3: <7, 8> | @t3 = i8;
4517 62:6| 3: <2> | @t4 = void;
4518 64:4| 0: <65534> | }
4519 ...
4520 100:0| 1: <65535, 12, 2> | function i64 @f0() { // BlockID = 12
4521 108:0| 3: <1, 1> | blocks 1;
4522 110:4| 1: <65535, 11, 2> | constants { // BlockID = 11
4523 120:0| 3: <1, 3> | i8:
4524 122:4| 3: <4, 3> | %c0 = i8 -1;
4525 125:0| 0: <65534> | }
4526 | | %b0:
4527 128:0| 3: <3, 1, 1, 2> | %v0 = sext i8 %c0 to i32;
4528 132:0| 3: <3, 1, 0, 2> | %v1 = sext i32 %v0 to i64;
4529 136:0| 3: <10, 1> | ret i64 %v1;
4530 138:4| 0: <65534> | }
4531
4532 Floating Point Extending Instruction
4533 ------------------------------------
4534
4535 The floating point extending instruction takes a value to extend, and a type to
4536 extend it to. Both types must either be floating point types, or vectors of
4537 floating point types with the same number of elements. The source value must be
4538 ``float`` while the destination is ``double``. If the source is a vector, the
4539 destination must also be vector with the same size as the source.
4540
4541 **Syntax**::
4542
4543 %vN = fpext T1 V to T2; <A>
4544
4545 **Record**::
4546
4547 AA: <3, VV, TT2, 8>
4548
4549 **Semantics**:
4550
4551 The floating point extending instruction converts floating point values.
4552 ``V`` is the value to extend, and ``T2`` is the type to extend it
4553 to. Both ``T1`` and ``T2`` must be floating point types, or floating point
4554 vector types with the same number of floating point values. ``T1`` contains
4555 ``float`` while ``T2`` contains ``double``.
4556
4557 **Constraints**::
4558
4559 AA == AbbrevIndex(A) &
4560 TypeOf(V) == T1 &
4561 VV == RelativeIndex(V) &
4562 %tTT2 == TypeID(T2) &
4563 BitSizeOf(UnderlyingType(T1)) < BitSizeOf(UnderlyingType(T2)) &
4564 UnderlyingCount(T1) == UnderlyingCount(T2) &
4565 IsFloat(UnderlyingType(T1)) &
4566 IsFloat(UnderlyingType(T2)) &
4567 N == NumValuedInsts
4568
4569 **Updates**::
4570
4571 ++NumValuedInsts;
4572 TypeOf(%vN) = T2;
4573
4574 **Examples**::
4575
4576 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4577 48:0| 3: <1, 4> | count 4;
4578 50:4| 3: <4> | @t0 = double;
4579 52:2| 3: <3> | @t1 = float;
4580 54:0| 3: <21, 0, 0, 1> | @t2 = double (float);
4581 58:0| 3: <2> | @t3 = void;
4582 59:6| 0: <65534> | }
4583 ...
4584 92:0| 1: <65535, 12, 2> | function double @f0(float %p0) {
4585 | | // BlockID = 12
4586 100:0| 3: <1, 1> | blocks 1;
4587 | | %b0:
4588 102:4| 3: <3, 1, 0, 8> | %v0 = fpext float %p0 to double;
4589 106:4| 3: <10, 1> | ret double %v0;
4590 109:0| 0: <65534> | }
4591
4592 Floating Point to Unsigned Integer Instruction
4593 ----------------------------------------------
4594
4595 The floating point to unsigned integer instruction converts floating point
4596 values to unsigned integers.
4597
4598 **Syntax**::
4599
4600 %vN = fptoui T1 V to T2; <A>
4601
4602 **Record**::
4603
4604 AA: <3, VV, TT2, 3>
4605
4606 **Semantics**:
4607
4608 The floating point to unsigned integer instruction converts floating point
4609 value(s) in ``V`` to its unsigned integer equivalent of type ``T2``. ``T1`` must
4610 be a floating point type, or a floating point vector type. ``T2`` must be an
4611 integer type, or an integer vector type. If either type is a vector type, they
4612 both must have the same number of elements.
4613
4614 **Constraints**::
4615
4616 AA == AbbrevIndex(A) &
4617 TypeOf(V) == T1 &
4618 VV == RelativeIndex(V) &
4619 %tTT2 == TypeID(T2) &
4620 UnderlyingCount(T1) == UnderlyingCount(T2) &
4621 IsFloat(UnderlyingType(T1)) &
4622 IsInteger(UnderlyingType(T2)) &
4623 N == NumValuedInsts
4624
4625 **Updates**::
4626
4627 ++NumValuedInsts;
4628 TypeOf(%vN) = T2;
4629
4630 **Examples**::
4631
4632 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4633 48:0| 3: <1, 6> | count 6;
4634 50:4| 3: <3> | @t0 = float;
4635 52:2| 3: <4> | @t1 = double;
4636 54:0| 3: <2> | @t2 = void;
4637 55:6| 3: <21, 0, 2, 0, 1> | @t3 = void (float, double);
4638 60:4| 3: <7, 32> | @t4 = i32;
4639 63:6| 3: <7, 16> | @t5 = i16;
4640 66:2| 0: <65534> | }
4641 ...
4642 100:0| 1: <65535, 12, 2> | function
4643 | | void @f0(float %p0, double %p1) {
4644 | | // BlockID = 12
4645 108:0| 3: <1, 1> | blocks 1;
4646 | | %b0:
4647 110:4| 3: <3, 2, 4, 3> | %v0 = fptoui float %p0 to i32;
4648 114:4| 3: <3, 2, 5, 3> | %v1 = fptoui double %p1 to i16;
4649 118:4| 3: <10> | ret void;
4650 120:2| 0: <65534> | }
4651
4652 Floating Point to Signed Integer Instruction
4653 --------------------------------------------
4654
4655 The floating point to signed integer instruction converts floating point
4656 values to signed integers.
4657
4658 **Syntax**::
4659
4660 %vN = fptosi T1 V to T2; <A>
4661
4662 **Record**::
4663
4664 AA: <3, VV, TT2, 4>
4665
4666 **Semantics**:
4667
4668 The floating point to signed integer instruction converts floating point value(s )
4669 in ``V`` to its signed integer equivalent of type ``T2``. ``T1`` must be a
4670 floating point type, or a floating point vector type. ``T2`` must be an integer
4671 type, or an integer vector type. If either type is a vector type, they both must
4672 have the same number of elements.
4673
4674 **Constraints**::
4675
4676 AA == AbbrevIndex(A) &
4677 TypeOf(V) == T1 &
4678 VV == RelativeIndex(V) &
4679 %tTT2 = TypeID(T2) &
4680 UnderlyingCount(T1) = UnderlyingCount(T2) &
4681 IsFloat(UnderlyingType(T1)) &
4682 IsInteger(UnderlyingType(T2)) &
4683 N = NumValuedInsts
4684
4685 **Updates**::
4686
4687 ++NumValuedInsts;
4688 TypeOf(%vN) = T2;
4689
4690 **Examples**::
4691
4692 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4693 48:0| 3: <1, 6> | count 6;
4694 50:4| 3: <3> | @t0 = float;
4695 52:2| 3: <4> | @t1 = double;
4696 54:0| 3: <2> | @t2 = void;
4697 55:6| 3: <21, 0, 2, 0, 1> | @t3 = void (float, double);
4698 60:4| 3: <7, 8> | @t4 = i8;
4699 63:0| 3: <7, 16> | @t5 = i16;
4700 65:4| 0: <65534> | }
4701 ...
4702 100:0| 1: <65535, 12, 2> | function
4703 | | void @f0(float %p0, double %p1) {
4704 | | // BlockID = 12
4705 108:0| 3: <1, 1> | blocks 1;
4706 | | %b0:
4707 110:4| 3: <3, 2, 4, 4> | %v0 = fptosi float %p0 to i8;
4708 114:4| 3: <3, 2, 5, 4> | %v1 = fptosi double %p1 to i16;
4709 118:4| 3: <10> | ret void;
4710 120:2| 0: <65534> | }
4711
4712 Unsigned Integer to Floating Point Instruction
4713 ----------------------------------------------
4714
4715 The unsigned integer to floating point instruction converts unsigned integers to
4716 floating point values.
4717
4718 **Syntax**::
4719
4720 %vN = uitofp T1 V to T2; <A>
4721
4722 **Record**::
4723
4724 AA: <3, VV, TT2, 5>
4725
4726 **Semantics**:
4727
4728 The unsigned integer to floating point instruction converts unsigned integer(s) to
4729 its floating point equivalent of type ``T2``. ``T1`` must be an integer type, or
4730 a integer vector type. ``T2`` must be a floating point type, or a floating point
4731 vector type. If either type is a vector type, they both must have the
4732 same number of elements.
4733
4734 **Constraints**::
4735
4736 AA == AbbrevIndex(A) &
4737 TypeOf(V) == T1 &
4738 VV == RelativeIndex(V) &
4739 %tTT2 = TypeID(T2) &
4740 UnderlyingCount(T1) == UnderlyingCount(T2) &
4741 IsInteger(UnderlyingType(T1)) &
4742 IsFloat(UnderlyingType(T2)) &
4743 N == NumValuedInsts
4744
4745 **Updates**::
4746
4747 ++NumValuedInsts;
4748 TypeOf(%vN) == T2;
4749
4750 **Examples**::
4751
4752 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4753 48:0| 3: <1, 7> | count 7;
4754 50:4| 3: <7, 32> | @t0 = i32;
4755 53:6| 3: <7, 64> | @t1 = i64;
4756 57:0| 3: <2> | @t2 = void;
4757 58:6| 3: <3> | @t3 = float;
4758 60:4| 3: <21, 0, 2, 0, 1> | @t4 = void (i32, i64);
4759 65:2| 3: <7, 1> | @t5 = i1;
4760 67:6| 3: <4> | @t6 = double;
4761 69:4| 0: <65534> | }
4762 ...
4763 104:0| 1: <65535, 12, 2> | function void @f0(i32 %p0, i64 %p1) {
4764 | | // BlockID = 12
4765 112:0| 3: <1, 1> | blocks 1;
4766 114:4| 1: <65535, 11, 2> | constants { // BlockID = 11
4767 124:0| 3: <1, 5> | i1:
4768 126:4| 3: <4, 3> | %c0 = i1 1;
4769 129:0| 0: <65534> | }
4770 | | %b0:
4771 132:0| 3: <3, 1, 6, 5> | %v0 = uitofp i1 %c0 to double;
4772 136:0| 3: <3, 4, 3, 5> | %v1 = uitofp i32 %p0 to float;
4773 140:0| 3: <3, 4, 3, 5> | %v2 = uitofp i64 %p1 to float;
4774 144:0| 3: <10> | ret void;
4775 145:6| 0: <65534> | }
4776
4777 Signed Integer to Floating Point Instruction
4778 --------------------------------------------
4779
4780 The signed integer to floating point instruction converts signed integers to
4781 floating point values.
4782
4783 **Syntax**::
4784
4785 %vN = sitofp T1 V to T2; <A>
4786
4787 **Record**::
4788
4789 AA: <3, VV, TT2, 6>
4790
4791 **Semantics**:
4792
4793 The signed integer to floating point instruction converts signed integer(s) to i ts
4794 floating point equivalent of type ``T2``. ``T1`` must be an integer type, or a
4795 integer vector type. ``T2`` must be a floating point type, or a floating point
4796 vector type. If either type is a vector type, they both must have the
4797 same number of elements.
4798
4799 **Constraints**::
4800
4801 AA == AbbrevIndex(A) &
4802 TypeOf(V) == T1 &
4803 VV == RelativeIndex(V) &
4804 %tTT2 = TypeID(T2) &
4805 UnderlyingCount(T1) == UnderlyingCount(T2) &
4806 IsInteger(UnderlyingType(T1)) &
4807 IsFloat(UnderlyingType(T2)) &
4808 N == NumValuedInsts
4809
4810 **Updates**::
4811
4812 ++NumValuedInsts;
4813 TypeOf(%vN) = T2;
4814
4815 **Examples**::
4816
4817 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4818 48:0| 3: <1, 7> | count 7;
4819 50:4| 3: <7, 32> | @t0 = i32;
4820 53:6| 3: <7, 64> | @t1 = i64;
4821 57:0| 3: <2> | @t2 = void;
4822 58:6| 3: <3> | @t3 = float;
4823 60:4| 3: <21, 0, 2, 0, 1> | @t4 = void (i32, i64);
4824 65:2| 3: <7, 8> | @t5 = i8;
4825 67:6| 3: <4> | @t6 = double;
4826 69:4| 0: <65534> | }
4827 ...
4828 104:0| 1: <65535, 12, 2> | function void @f0(i32 %p0, i64 %p1) {
4829 | | // BlockID = 12
4830 112:0| 3: <1, 1> | blocks 1;
4831 114:4| 1: <65535, 11, 2> | constants { // BlockID = 11
4832 124:0| 3: <1, 5> | i8:
4833 126:4| 3: <4, 3> | %c0 = i8 -1;
4834 129:0| 0: <65534> | }
4835 | | %b0:
4836 132:0| 3: <3, 1, 6, 6> | %v0 = sitofp i8 %c0 to double;
4837 136:0| 3: <3, 4, 3, 6> | %v1 = sitofp i32 %p0 to float;
4838 140:0| 3: <3, 4, 3, 6> | %v2 = sitofp i64 %p1 to float;
4839 144:0| 3: <10> | ret void;
4840 145:6| 0: <65534> | }
4841
4842 Bitcast Instruction
4843 -------------------
4844
4845 The bitcast instruction converts the type of the value without changing the bit
4846 contents of the value. The bit size of the type of the value must be the same as
4847 the bit size of the cast type.
4848
4849 **Syntax**::
4850
4851 %vN = bitcast T1 V to T2; <A>
4852
4853 **Record**::
4854
4855 AA: <3, VV, TT2, 11>
4856
4857 **Semantics**:
4858
4859 The bitcast instruction converts the type of value ``V`` to type ``T2``. ``T1``
4860 and ``T2`` must be primitive types or vectors, and define the same number of
4861 bits.
4862
4863 **Constraints**::
4864
4865 AA == AbbrevIndex(A) &
4866 TypeOf(V) == T1 &
4867 VV = RelativeIndex(V) &
4868 %tTT2 = TypeID(T2) &
4869 BitSizeOf(T1) == BitSizeOf(T2) &
4870 N == NumValuedInsts
4871
4872 **Updates**::
4873
4874 ++NumValuedInsts;
4875 TypeOf(%vN) = T2;
4876
4877 **Examples**::
4878
4879 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4880 48:0| 3: <1, 6> | count 6;
4881 50:4| 3: <3> | @t0 = float;
4882 52:2| 3: <7, 64> | @t1 = i64;
4883 55:4| 3: <2> | @t2 = void;
4884 57:2| 3: <21, 0, 2, 0, 1> | @t3 = void (float, i64);
4885 62:0| 3: <7, 32> | @t4 = i32;
4886 65:2| 3: <4> | @t5 = double;
4887 67:0| 0: <65534> | }
4888 ...
4889 100:0| 1: <65535, 12, 2> | function void @f0(float %p0, i64 %p1)
4890 | | { // BlockID = 12
4891 108:0| 3: <1, 1> | blocks 1;
4892 | | %b0:
4893 110:4| 3: <3, 2, 4, 11> | %v0 = bitcast float %p0 to i32;
4894 114:4| 3: <3, 2, 5, 11> | %v1 = bitcast i64 %p1 to double;
4895 118:4| 3: <10> | ret void;
4896 120:2| 0: <65534> | }
4897
4898 .. _link_for_compare_instructions:
4899
4900 Comparison Instructions
4901 =======================
4902
4903 The comparison instructions compares values returns a boolean (i1) result for
Jim Stichnoth 2014/11/18 02:24:42 ... compare ... ... and return ...
Karl 2014/11/19 20:28:52 Done.
4904 each pair of compared values. There are different comparison operations for
4905 integer and floating point values.
4906
4907
4908 Integer Comparison Instructions
4909 -------------------------------
4910
4911 The integer comparison instruction compares integer values and returns a
4912 boolean (i1) result for each pair of compared values.
4913
4914 **Syntax**::
4915
4916 %vN = icmp C T V1, V2; <A>
4917
4918 **Record**::
4919
4920 AA: <9, VV1, VV2, CC>
4921
4922 **Semantics**:
4923
4924 The integer comparison instruction compares integer values and returns a boolean
4925 (i1) result for each pair of compared values in ``V1`` and ``V2``. ``V1`` and
4926 ``V2`` must be of type ``T``. ``T`` must be an integer type, or an integer
4927 vector type. Condition code ``C`` is the condition applied to all elements in
4928 ``V1`` and ``V2``. Each comparison always yields an i1. If ``T`` is a primitive
4929 type, the resulting type is i1. If ``T`` is a vector, then the resulting type is
4930 a vector of i1 with the same size as ``T``.
4931
4932 Legal test conditions are:
4933
4934 === == ==============================
4935 C CC Operator
4936 === == ==============================
4937 eq 32 equal
4938 ne 33 not equal
4939 ugt 34 unsigned greater than
4940 uge 35 unsigned greater than or equal
4941 ult 36 unsigned less than
4942 ule 37 unsigned less than or equal
4943 sgt 38 signed greater than
4944 sge 39 signed greater than or equal
4945 slt 40 signed less than
4946 sle 41 signed less than or equal
4947 === == ==============================
4948
4949 **Constraints**::
4950
4951 AA == AbbrevIndex(A) &
4952 IsInteger(UnderlyingType(T) &
4953 T == TypeOf(V1) == TypeOf(V2) &
4954 N == NumValuedInsts
4955
4956 **Updates**::
4957
4958 ++NumValuedInsts;
4959 if IsVector(T) then
4960 TypeOf(%vN) = <UnderlyingCount(T), i1>
4961 else
4962 TypeOf(%vN) = i1
4963 endif
4964
4965 **Examples**::
4966
4967 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
4968 48:0| 3: <1, 4> | count 4;
4969 50:4| 3: <7, 32> | @t0 = i32;
4970 53:6| 3: <7, 1> | @t1 = i1;
4971 56:2| 3: <2> | @t2 = void;
4972 58:0| 3: <21, 0, 2> | @t3 = void ();
4973 61:2| 0: <65534> | }
4974 ...
4975 108:0| 1: <65535, 12, 2> | function void @f0() {
4976 | | // BlockID = 12
4977 116:0| 3: <1, 1> | blocks 1;
4978 118:4| 1: <65535, 11, 2> | constants { // BlockID = 11
4979 128:0| 3: <1, 0> | i32:
4980 130:4| 3: <4, 0> | %c0 = i32 0;
4981 133:0| 3: <4, 2> | %c1 = i32 1;
4982 135:4| 0: <65534> | }
4983 | | %b0:
4984 136:0| 3: <28, 2, 1, 32> | %v0 = icmp eq i32 %c0, %c1;
4985 140:6| 3: <28, 3, 2, 33> | %v1 = icmp ne i32 %c0, %c1;
4986 145:4| 3: <28, 4, 3, 34> | %v2 = icmp ugt i32 %c0, %c1;
4987 150:2| 3: <28, 5, 4, 36> | %v3 = icmp ult i32 %c0, %c1;
4988 155:0| 3: <28, 6, 5, 37> | %v4 = icmp ule i32 %c0, %c1;
4989 159:6| 3: <28, 7, 6, 38> | %v5 = icmp sgt i32 %c0, %c1;
4990 164:4| 3: <28, 8, 7, 38> | %v6 = icmp sgt i32 %c0, %c1;
4991 169:2| 3: <28, 9, 8, 39> | %v7 = icmp sge i32 %c0, %c1;
4992 174:0| 3: <28, 10, 9, 40> | %v8 = icmp slt i32 %c0, %c1;
4993 178:6| 3: <28, 11, 10, 41> | %v9 = icmp sle i32 %c0, %c1;
4994 183:4| 3: <10> | ret void;
4995 185:2| 0: <65534> | }
4996
4997
4998 Floating Point Comparison Instructions
4999 --------------------------------------
5000
5001 The floating point comparison instruction compares floating point values and
5002 returns a boolean (i1) result for each pair of compared values.
5003
5004 **Syntax**::
5005
5006 %vN = fcmp C T V1, V2; <A>
5007
5008 **Record**::
5009
5010 AA: <9, VV1, VV2, CC>
5011
5012 **Semantics**:
5013
5014 The floating point comparison instruction compares floating point values and
5015 returns a boolean (i1) result for each pair of compared values in ``V1`` and
5016 ``V2``. ``V1`` and ``V2`` must be of type ``T``. ``T`` must be a floating point
5017 type, or a floating point vector type. Condition code ``C`` is the condition
5018 applied to all elements in ``V1`` and ``V2``. Each comparison always yields an
5019 i1. If ``T`` is a primitive type, the resulting type is i1. If ``T`` is a
5020 vector, then the resulting type is a vector of i1 with the same size as ``T``.
5021
5022 Legal test conditions are:
5023
5024 ===== == ==================================
5025 C CC Operator
5026 ===== == ==================================
5027 false 0 Always false
5028 oeq 1 Ordered and equal
5029 ogt 2 Ordered and greater than
5030 oge 3 Ordered and greater than or equal
5031 olt 4 Ordered and less than
5032 ole 5 Ordered and less than or equal
5033 one 6 Ordered and not equal
5034 ord 7 Ordered (no nans)
Jim Stichnoth 2014/11/17 18:54:56 For these two lines, maybe write "NaNs" instead of
Karl 2014/11/17 20:17:28 Done.
5035 uno 8 Unordered (either nans)
5036 ueq 9 Unordered or equal
5037 ugt 10 Unordered or greater than
5038 uge 11 Unordered or greater than or equal
5039 ult 12 Unordered or less than
5040 ule 13 Unordered or less than or equal
5041 une 14 Unordered or not equal
5042 true 15 Always true
5043 ===== == ==================================
5044
5045 **Constraints**::
5046
5047 AA == AbbrevIndex(A) &
5048 IsFloat(UnderlyingType(T) &
5049 T == TypeOf(V1) == TypeOf(V2) &
5050 N == NumValuedInsts
5051
5052 **Updates**::
5053
5054 ++NumValuedInsts;
5055 if IsVector(T) then
5056 TypeOf(%vN) = <UnderlyingCount(T), i1>
5057 else
5058 TypeOf(%vN) = i1
5059 endif
5060
5061 **Examples**::
5062
5063 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
5064 48:0| 3: <1, 4> | count 4;
5065 50:4| 3: <3> | @t0 = float;
5066 52:2| 3: <7, 1> | @t1 = i1;
5067 54:6| 3: <2> | @t2 = void;
5068 56:4| 3: <21, 0, 2> | @t3 = void ();
5069 59:6| 0: <65534> | }
5070 ...
5071 108:0| 1: <65535, 12, 2> | function void @f0() {
5072 | | // BlockID = 12
5073 116:0| 3: <1, 1> | blocks 1;
5074 118:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5075 128:0| 3: <1, 0> | float:
5076 130:4| 3: <6, 0> | %c0 = float 0;
5077 133:0| 3: <6, 1065353216> | %c1 = float 1;
5078 139:2| 0: <65534> | }
5079 | | %b0:
5080 140:0| 3: <28, 2, 1, 0> | %v0 = fcmp false float %c0, %c1;
5081 144:0| 3: <28, 3, 2, 1> | %v1 = fcmp oeq float %c0, %c1;
5082 148:0| 3: <28, 4, 3, 2> | %v2 = fcmp ogt float %c0, %c1;
5083 152:0| 3: <28, 5, 4, 3> | %v3 = fcmp oge float %c0, %c1;
5084 156:0| 3: <28, 6, 5, 4> | %v4 = fcmp olt float %c0, %c1;
5085 160:0| 3: <28, 7, 6, 5> | %v5 = fcmp ole float %c0, %c1;
5086 164:0| 3: <28, 8, 7, 6> | %v6 = fcmp one float %c0, %c1;
5087 168:0| 3: <28, 9, 8, 7> | %v7 = fcmp ord float %c0, %c1;
5088 172:0| 3: <28, 10, 9, 9> | %v8 = fcmp ueq float %c0, %c1;
5089 176:0| 3: <28, 11, 10, 10> | %v9 = fcmp ugt float %c0, %c1;
5090 180:0| 3: <28, 12, 11, 11> | %v10 = fcmp uge float %c0, %c1;
5091 184:0| 3: <28, 13, 12, 12> | %v11 = fcmp ult float %c0, %c1;
5092 188:0| 3: <28, 14, 13, 13> | %v12 = fcmp ule float %c0, %c1;
5093 192:0| 3: <28, 15, 14, 14> | %v13 = fcmp une float %c0, %c1;
5094 196:0| 3: <28, 16, 15, 8> | %v14 = fcmp uno float %c0, %c1;
5095 200:0| 3: <28, 17, 16, 15> | %v15 = fcmp true float %c0, %c1;
5096 204:0| 3: <10> | ret void;
5097 205:6| 0: <65534> | }
5098 208:0|0: <65534> |}
5099
5100 .. _link_for_vector_instructions:
5101
5102 Vector Instructions
5103 ===================
5104
5105 PNaClAsm supports several instructions that process vectors. This includes the
5106 :ref:`integer<link_for_integer_binary_instructions>` and :ref:`floating
5107 point<link_for_floating_point_binary_instructions>` binary instructions as well
5108 as :ref:`compare<link_for_compare_instructions>` instructions. These
5109 instructions work with vectors and generate resulting (new) vectors. This
5110 section introduces the instructions to construct vectors and extract results.
5111
5112 .. _link_for_insert_element_instruction_section:
5113
5114 Insert Element Instruction
5115 --------------------------
5116
5117 The *insert element* instruction inserts a scalar value into a vector at a
5118 specified index. The *insert element* instruction takes an existing vector and
5119 puts a scalar value in one of the elements of the vector.
5120
5121 The *insert element* instruction can be used to construct a vector, one element
5122 at a time. At first glance, it may appear that one can't construct a vector,
5123 since the *insert element* instruction needs a vector to insert elements into.
5124
5125 The key to understanding vector construction is understand that one can create
5126 an :ref:`undefined<link_for_undefined_literal>` vector literal. Using that
5127 constant as a starting point, one can built up the wanted vector by a sequence
5128 of *insert element* instructions.
5129
5130 **Syntax**::
5131
5132 %vN = insertelement TV V, TE E, i32 I; <A>
5133
5134 **Record**::
5135
5136 AA: <7, VV, EE, II>
5137
5138 **Semantics**:
5139
5140 The *insert element* instruction inserts scalar value ``E`` into index ``I`` of
5141 vector ``V``. ``%vN`` holds the updated vector. Type ``TV`` is the type of
5142 vector. It is also the type of updated vector ``%vN``. Type ``TE`` is the type
5143 of scalar value ``E`` and must be the element type of vector ``V``. ``I`` must
5144 be an :ref:`i32 literal<link_for_integer_literal>`.
5145
5146 If ``I`` exceeds the length of ``V``, the result is undefined.
5147
5148 **Constraints**::
5149
5150 AA == AbbrevIndex(A) &
5151 IsVector(TV) &
5152 TypeOf(V) == TV &
5153 UnderlyingType(TV) == TE &
5154 TypeOf(I) == i32 &
5155 N == NumValuedInsts
5156
5157 **Updates**::
5158
5159 ++NumValuedInsts;
5160 TypeOf(%vN) = TV;
5161
5162 **Examples**::
5163
5164 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
5165 48:0| 3: <1, 5> | count 5;
5166 50:4| 3: <7, 1> | @t0 = i1;
5167 53:0| 3: <12, 4, 0> | @t1 = <4 x i1>;
5168 56:2| 3: <7, 32> | @t2 = i32;
5169 59:4| 3: <2> | @t3 = void;
5170 61:2| 3: <21, 0, 3> | @t4 = void ();
5171 64:4| 0: <65534> | }
5172 ...
5173 116:0| 1: <65535, 12, 2> | function void @f0() {
5174 | | // BlockID = 12
5175 124:0| 3: <1, 1> | blocks 1;
5176 126:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5177 136:0| 3: <1, 0> | i1:
5178 138:4| 3: <4, 0> | %c0 = i1 0;
5179 141:0| 3: <4, 3> | %c1 = i1 1;
5180 143:4| 3: <1, 1> | <4 x i1>:
5181 146:0| 3: <3> | %c2 = <4 x i1> undef;
5182 147:6| 3: <1, 2> | i32:
5183 150:2| 3: <4, 0> | %c3 = i32 0;
5184 152:6| 3: <4, 2> | %c4 = i32 1;
5185 155:2| 3: <4, 4> | %c5 = i32 2;
5186 157:6| 3: <4, 6> | %c6 = i32 3;
5187 160:2| 0: <65534> | }
5188 | | %b0:
5189 164:0| 3: <7, 5, 7, 4> | %v0 = insertelement <4 x i1> %c2,
5190 | | i1 %c0, i32 %c3;
5191 168:0| 3: <7, 1, 7, 4> | %v1 = insertelement <4 x i1> %v0,
5192 | | i1 %c1, i32 %c4;
5193 172:0| 3: <7, 1, 9, 4> | %v2 = insertelement <4 x i1> %v1,
5194 | | i1 %c0, i32 %c5;
5195 176:0| 3: <7, 1, 9, 4> | %v3 = insertelement <4 x i1> %v2,
5196 | | i1 %c1, i32 %c6;
5197 180:0| 3: <10> | ret void;
5198 181:6| 0: <65534> | }
5199
5200 Extract Element Instruction
5201 ---------------------------
5202
5203 The *extract element* instruction extracts a single scalar value from a vector
5204 at a specified index.
5205
5206 **Syntax**::
5207
5208 %vN = extractelement TV V, i32 I; <A>
5209
5210 **Record**::
5211
5212 AA: <6, VV, II>
5213
5214 **Semantics**:
5215
5216 The *extract element* instruction extracts the scalar value at index ``I`` from
5217 vector ``V``. The extracted value is assigned to ``%vN``. Type ``TV`` is the
5218 type of vector ``V``. ``I`` must be an :ref:`i32
5219 literal<link_for_integer_literal>`. The type of ``vN`` must be the element type
5220 of vector ``V``.
5221
5222 If ``I`` exceeds the length of ``V``, the result is undefined.
5223
5224 **Constraints**::
5225
5226 AA == AbbrevIndex(A) &
5227 IsVector(TV) &
5228 TypeOf(V) == TV &
5229 TypeOf(I) == i32 &
5230 N == NumValuedInsts
5231
5232 **Updates**::
5233
5234 ++NumValuedInsts;
5235 TypeOf(%vN) = UnderlyingType(TV);
5236
5237 **Examples**::
5238
5239 96:0| 1: <65535, 12, 2> | function void @f0(<4 x i32> %p0) {
5240 | | // BlockID = 12
5241 104:0| 3: <1, 1> | blocks 1;
5242 106:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5243 116:0| 3: <1, 0> | i32:
5244 118:4| 3: <4, 0> | %c0 = i32 0;
5245 121:0| 0: <65534> | }
5246 | | %b0:
5247 124:0| 3: <6, 2, 1> | %v0 =
5248 | | extractelement <4 x i32> %p0,
5249 | | i32 %c0;
5250 127:2| 3: <10> | ret void;
5251 129:0| 0: <65534> | }
5252
5253 .. _link_for_other_pnaclasm_instructions:
5254
5255 Other Instructions
5256 ==================
5257
5258 This section defines miscellaneous instructions which defy better
5259 classification.
5260
5261 .. _link_for_forward_type_declaration_section:
5262
5263 Forward Type Declaration
5264 ------------------------
5265
5266 The forward type declaration exists to deal with the fact that all instruction
5267 values must have a type associated with them before they are used. For some
5268 simple functions one can easily topologically sort instructions so that
5269 instruction values are defined before they are used. However, if the
5270 implementation contains loops, the loop induced values can't be defined before
5271 they are used.
5272
5273 The solution is to forward declare the type of an instruction value. One could
5274 forward declare the types of all instructions at the beginning of the function
5275 block. However, this would make the corresponding file size considerably
5276 larger. Rather, one should only generate these forward type declarations
5277 sparingly and only when needed.
5278
5279 **Syntax**::
5280
5281 declare T %vN; <A>
5282
5283 **Record**::
5284
5285 AA: <43, N, TT>
5286
5287 **Semantics**:
5288
5289 The forward declare type declaration defines the type to be associated with a
5290 (not yet defined) instruction value ``%vN``. ``T`` is the type of the value
5291 generated by the ``Nth`` value generating instruction in the function block.
5292
5293 Note: It is an error to define the type of ``%vN`` with a different type than
5294 will be generated by the ``Nth`` value generating instruction in the function
5295 block.
5296
5297 Also note that this construct is a declaration and not considered an
5298 instruction, even though it appears in the list of instruction records. Hence,
5299 they may appear before and between :ref:`phi<link_for_phi_instruction_section>`
5300 instructions in a basic block.
5301
5302 **Constraints**::
5303
5304 AA = AbbrevIndex(A) &
5305 TT = TypeID(T)
5306
5307 **Updates**::
5308
5309 TypeOf(%vN) = T;
5310
5311 **Examples**::
5312
5313 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
5314 48:0| 3: <1, 4> | count 4;
5315 50:4| 3: <7, 32> | @t0 = i32;
5316 53:6| 3: <2> | @t1 = void;
5317 55:4| 3: <7, 1> | @t2 = i1;
5318 58:0| 3: <21, 0, 1, 0> | @t3 = void (i32);
5319 62:0| 0: <65534> | }
5320 ...
5321 108:0| 1: <65535, 12, 2> | function void @f0(i32 %p0) {
5322 | | // BlockID = 12
5323 116:0| 3: <1, 7> | blocks 7;
5324 118:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5325 128:0| 3: <1, 2> | i1:
5326 130:4| 3: <4, 3> | %c0 = i1 1;
5327 133:0| 0: <65534> | }
5328 | | %b0:
5329 136:0| 3: <11, 4> | br label %b4;
5330 | | %b1:
5331 138:4| 3: <43, 6, 0> | declare i32 %v3;
5332 142:4| 3: <2, 2, 4294967293, 0> | %v0 = add i32 %p0, %v3;
5333 151:0| 3: <11, 6> | br label %b6;
5334 | | %b2:
5335 153:4| 3: <43, 7, 0> | declare i32 %v4;
5336 157:4| 3: <2, 3, 4294967293, 0> | %v1 = add i32 %p0, %v4;
5337 166:0| 3: <11, 6> | br label %b6;
5338 | | %b3:
5339 168:4| 3: <2, 4, 4294967295, 0> | %v2 = add i32 %p0, %v3;
5340 177:0| 3: <11, 6> | br label %b6;
5341 | | %b4:
5342 179:4| 3: <2, 5, 5, 0> | %v3 = add i32 %p0, %p0;
5343 183:4| 3: <11, 1, 5, 5> | br i1 %c0, label %b1, label %b5;
5344 | | %b5:
5345 187:4| 3: <2, 1, 6, 0> | %v4 = add i32 %v3, %p0;
5346 191:4| 3: <11, 2, 3, 6> | br i1 %c0, label %b2, label %b3;
5347 | | %b6:
5348 195:4| 3: <10> | ret void;
5349 197:2| 0: <65534> | }
5350
5351 .. _link_for_phi_instruction_section:
5352
5353 Phi Instruction
5354 ---------------
5355
5356 The *phi* instruction is used to implement phi nodes in the SSA graph
5357 representing the function. Phi instructions can only appear at the beginning of
5358 a basic block. There must be no non-phi instructions (other than forward type
5359 declarations) between the start of the basic block and the *phi* instruction.
5360
5361 To clarify the origin of each incoming value, the incoming value is associated
5362 with the incoming edge from the corresponding predecessor block that the
5363 incoming value comes from.
5364
5365 **Syntax**::
5366
5367 %vN = phi T [V1, %bB1], ... , [VM, %bBM]; <A>
5368
5369 **Record**::
5370
5371 AA: <16, TT, VV1, B1, ..., VVM, BM>
5372
5373 **Semantics**:
5374
5375 The phi instruction is used to implement phi nodes in the SSA graph representing
5376 the function. ``%vN`` is the resulting value of the corresponding phi node. ``T` ` is
5377 the type of the phi node. Values ``V1`` through ``VM`` are the reaching definiti ons
5378 for the phi node while ``%bB1`` through ``%bBM`` are the corresponding predecess or
5379 blocks. Each ``VI`` reaches via the incoming predecessor edge from block ``%bBI ``
5380 (for 1 <= I <= M). Type ``T`` must be the type associated with each ``VI``.
5381
5382 **Constraints**::
5383
5384 AA == AbbrevIndex(A) &
5385 M > 1 &
5386 TT == TypeID(T) &
5387 T = TypeOf(VI) for all I, 1 <= I <= M &
5388 BI < ExpectedBasicBlocks for all I, 1 <= I <= M &
5389 VVI = SignRotate(RelativeIndex(VI)) for all I, 1 <= I <= M &
5390 N == NumValuedInsts
5391
5392 **Updates**::
5393
5394 ++NumValuedInsts;
5395 TypeOf(%vN) = T;
5396
5397 **Examples**::
5398
5399 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
5400 48:0| 3: <1, 4> | count 4;
5401 50:4| 3: <7, 32> | @t0 = i32;
5402 53:6| 3: <2> | @t1 = void;
5403 55:4| 3: <21, 0, 1> | @t2 = void ();
5404 58:6| 3: <7, 1> | @t3 = i1;
5405 61:2| 0: <65534> | }
5406 ...
5407 112:0| 1: <65535, 12, 2> | function void @f0() {
5408 | | // BlockID = 12
5409 120:0| 3: <1, 4> | blocks 4;
5410 122:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5411 132:0| 3: <1, 0> | i32:
5412 134:4| 3: <4, 2> | %c0 = i32 1;
5413 137:0| 3: <1, 3> | i1:
5414 139:4| 3: <4, 0> | %c1 = i1 0;
5415 142:0| 0: <65534> | }
5416 | | %b0:
5417 144:0| 3: <11, 1, 2, 1> | br i1 %c1, label %b1, label %b2;
5418 | | %b1:
5419 148:0| 3: <2, 2, 2, 0> | %v0 = add i32 %c0, %c0;
5420 152:0| 3: <2, 3, 3, 1> | %v1 = sub i32 %c0, %c0;
5421 156:0| 3: <11, 3> | br label %b3;
5422 | | %b2:
5423 158:4| 3: <2, 4, 4, 2> | %v2 = mul i32 %c0, %c0;
5424 162:4| 3: <2, 5, 5, 3> | %v3 = udiv i32 %c0, %c0;
5425 166:4| 3: <11, 3> | br label %b3;
5426 | | %b3:
5427 169:0| 3: <16, 0, 8, 1, 4, 2> | %v4 = phi i32 [%v0, %b1],
5428 | | [%v2, %b2];
5429 174:4| 3: <16, 0, 8, 1, 4, 2> | %v5 = phi i32 [%v1, %b1],
5430 | | [%v3, %b2];
5431 180:0| 3: <10> | ret void;
5432 181:6| 0: <65534> | }
5433
5434 Select Instruction
5435 ------------------
5436
5437 The *select* instruction is used to choose between pairs of values, based on a
5438 condition, without PNaClAsm-level branching.
5439
5440 **Syntax**::
5441
5442 %vN = select CT C, T V1, T V2; <A>
5443
5444 **Record**::
5445
5446 AA: <29, VV1, VV2, CC>
5447
5448 **Semantics**:
5449
5450 The *select* instruction chooses pairs of values ``V1`` and ``V2``, based on
5451 condition value ``C``. The type ``CT`` of value ``C`` must either be an i1, or a
5452 vector of type i1. The type of values ``V1`` and ``V2`` must be of type ``T``. T ype
5453 ``T`` must either be a primitive type, or a vector of a primitive type.
5454
5455 Both ``CT`` and ``T`` must be primitive types, or both must be vector types of t he
5456 same size. When the contents of ``C`` is 1, the corresponding value from ``V1`` will
5457 be chosen. Otherwise the corresponding value from ``V2`` will be chosen.
5458
5459 **Constraints**::
5460
5461 AA == AbbrevIndex(A) &
5462 CC == RelativeIndex(C) &
5463 VV1 == RelativeIndex(V1) &
5464 VV2 == RelativeIndex(V2) &
5465 T == TypeOf(V1) == TypeOf(V2) &
5466 UnderlyingType(CT) == i1 &
5467 IsInteger(UnderlyingType(T)) or IsFloat(UnderlyingType(T)) &
5468 UnderlyingCount(C) == UnderlyingCount(T) &
5469 N == NumValuedInsts
5470
5471 **Updates**::
5472
5473 ++NumValuedInsts;
5474 TypeOf(%vN) = T;
5475
5476 **Examples**::
5477
5478 96:0| 1: <65535, 12, 2> | function i32 @f0(i32 %p0, i32 %p1) {
5479 | | // BlockID = 12
5480 104:0| 3: <1, 1> | blocks 1;
5481 106:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5482 116:0| 3: <1, 2> | i1:
5483 118:4| 3: <4, 3> | %c0 = i1 1;
5484 121:0| 0: <65534> | }
5485 | | %b0:
5486 124:0| 3: <29, 3, 2, 1> | %v0 = select i1 %c0, i32 %p0,
5487 | | i32 %p1;
5488 128:0| 3: <10, 1> | ret i32 %v0;
5489 130:4| 0: <65534> | }
5490
5491
5492 Call Instructions
5493 -----------------
5494
5495 The *call* instruction does a function call. The call instruction is used to
5496 cause control flow to transfer to a specified routine, with its incoming
5497 arguments bound to the specified values. When a return instruction in the called
5498 function is reached, control flow continues with the instruction after the
5499 function call. If the call is to a function, the returned value is the value
5500 generated by the call instruction. Otherwise no result is defined by the call.
5501
5502 If the *tail* flag is associated with the call instruction, then the :ref:`PNaCl
5503 translator<link_for_pnacl_translator>` is free to perform tail call
5504 optimization. That is, the *tail* flag is a hint that may be ignored by the
5505 PNaCl translator.
5506
5507 There are two kinds of calls: *direct* and *indirect*. A *direct* call calls a
5508 defined :ref:`function address<link_for_function_address_section>` (i.e. a
5509 reference to a bitcode ID of the form ``%fF``). All other calls are *indirect*.
5510
5511 Direct Procedure Call
5512 ^^^^^^^^^^^^^^^^^^^^^
5513
5514 The direct procedure call calls a defined :ref:`function
5515 address<link_for_function_address_section>` whose :ref:`type
5516 signature<link_for_function_type>` returns type void.
5517
5518 **Syntax**::
5519
5520 TAIL call void @fF (T1 A1, ... , TN AN); <A>
5521
5522 **Record**::
5523
5524 AA: <34, CC, F, AA1, ... , AAN>
5525
5526 **Semantics**:
5527
5528 The direct procedure call calls a define function address ``%fF`` whose type
5529 signature return type is void. The arguments ``A1`` through ``AN`` are passed
5530 in the order specified. The type of argument ``AI`` must be type ``TI`` (for all I,
5531 1 <=I <= N). Flag ``TAIL`` is optional. If it is included, it must be the
5532 literal ``tail``.
5533
5534 The types of the arguments must match the corresponding types of the function
5535 signature associated with ``%fF``. The return type of ``%f`` must be void.
5536
5537 TAIL is encoded into calling convention value ``CC`` as follows:
5538
5539 ====== ==
5540 TAIL CC
5541 ====== ==
5542 "" 0
5543 "tail" 1
5544 ====== ==
5545
5546 **Constraints**::
5547
5548 AA == AbbrevIndex(A) &
5549 N >= 0 &
5550 TypeOfFcn(%fF) == void (T1, ... , TN) &
5551 TypeOf(AI) == TI for all I, 1 <= I <= N
5552
5553 **Updates**::
5554
5555 ++NumValuedInsts;
5556
5557 **Examples**::
5558
5559 72:0| 3: <8, 3, 0, 1, 0> | declare external
5560 | | void @f0(i32, i64, i32);
5561 ...
5562 116:0| 1: <65535, 12, 2> | function void @f1(i32 %p0) {
5563 | | // BlockID = 12
5564 124:0| 3: <1, 1> | blocks 1;
5565 126:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5566 136:0| 3: <1, 2> | i64:
5567 138:4| 3: <4, 2> | %c0 = i64 1;
5568 141:0| 0: <65534> | }
5569 | | %b0:
5570 144:0| 3: <34, 0, 4, 2, 1, 2> | call void
5571 | | @f0(i32 %p0, i64 %c0, i32 %p0);
5572 150:2| 3: <10> | ret void;
5573 152:0| 0: <65534> | }
5574
5575 Direct Function Call
5576 ^^^^^^^^^^^^^^^^^^^^
5577
5578 The direct function call calls a defined function address whose type signature
5579 returns a value.
5580
5581 **Syntax**::
5582
5583 %vN = TAIL call RT %fF (T1 A1, ... , TM AM); <A>
5584
5585
5586 **Record**::
5587
5588 AA: <34, CC, F, AA1, ... , AAM>
5589
5590 **Semantics**:
5591
5592 The direct function call calls a defined function address ``%fF`` whose type
5593 signature returned is not type void. The arguments ``A1`` through ``AM`` are
5594 passed in the order specified. The type of argument ``AI`` must be type ``TI``
5595 (for all I, 1 <= I <= N). Flag ``TAIL`` is optional. If it is included, it must
5596 be the literal ``tail``.
5597
5598 The types of the arguments must match the corresponding types of the function
5599 signature associated with ``%fF``. The return type must match ``RT``.
5600
5601 Each parameter type ``TI``, and return type ``RT``, must either be a primitive t ype,
5602 or a vector type. If the parameter type is an integer type, it must either be
5603 i32 or i64.
5604
5605 TAIL is encoded into calling convention value ``CC`` as follows:
5606
5607 ====== ==
5608 TAIL CC
5609 ====== ==
5610 "" 0
5611 "tail" 1
5612 ====== ==
5613
5614 **Constraints**::
5615
5616 AA == AbbrevIndex(A) &
5617 N >= 0 &
5618 TypeOfFcn(%fF) == RT (T1, ... , TN) &
5619 TypeOf(AI) == TI for all I, 1 <= I <= M &
5620 IsFcnArgType(TI) for all I, 1 <= I <= M &
5621 IsFcnArgType(RT) &
5622 N == NumValuedInsts
5623
5624 **Updates**::
5625
5626 ++NumValuedInsts;
5627 TypeOf(%vN) = RT;
5628
5629 **Examples**::
5630
5631 72:0| 3: <8, 2, 0, 1, 0> | declare external
5632 | | i32 @f0(i32, i64, i32);
5633 ...
5634 116:0| 1: <65535, 12, 2> | function i32 @f1(i32 %p0) {
5635 | | // BlockID = 12
5636 124:0| 3: <1, 1> | blocks 1;
5637 126:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5638 136:0| 3: <1, 1> | i64:
5639 138:4| 3: <4, 2> | %c0 = i64 1;
5640 141:0| 0: <65534> | }
5641 | | %b0:
5642 144:0| 3: <34, 0, 4, 2, 1, 2> | %v0 = call i32
5643 | | @f0(i32 %p0, i64 %c0, i32 %p0);
5644 150:2| 3: <34, 1, 4, 1> | %v1 = tail call i32 @f1(i32 %v0);
5645 155:0| 3: <10, 2> | ret i32 %v0;
5646 157:4| 0: <65534> | }
5647
5648 Indirect Procedure Call
5649 ^^^^^^^^^^^^^^^^^^^^^^^
5650
5651 The indirect procedure call calls a function using an indirect function address,
5652 and whose type signature is assumed to return type void. It is different from
5653 the direct procedure call because we can't use the type signature of the
5654 corresponding direct function address to type check the construct.
5655
5656 **Syntax**::
5657
5658 TAIL call void V (T1 A1, ... , TN AN); <A>
5659
5660 **Record**::
5661
5662 AA: <44, CC, TV, VV, AA1, ... , AAN>
5663
5664 **Semantics**:
5665
5666 The indirect call procedure calls a function using value ``V`` that is an indire ct
5667 function address, and whose type signature is assumed to return type void. The
5668 arguments ``A1`` through ``AN`` are passed in the order specified. The type of
5669 argument ``AI`` must be type ``TI`` (for all I, 1 <= I <= N). Flag ``TAIL`` is
5670 optional. If it is included, it must be the literal ``tail``.
5671
5672 Each parameter type ``TI`` (1 <= I <= N) must either be a primitive type, or a
5673 vector type. If the parameter type is an integer type, it must either be i32
5674 or i64.
5675
5676 TAIL is encoded into calling convention value ``CC`` as follows:
5677
5678 ====== ==
5679 TAIL CC
5680 ====== ==
5681 "" 0
5682 "tail" 1
5683 ====== ==
5684
5685 The type signature of the called procedure is assumed to be::
5686
5687 void (T1, ... , TN)
5688
5689 It isn't necessary to define this type in the :ref:`types
5690 block<link_for_types_block_section>`, since the type is inferred rather than
5691 used.
5692
5693 **Constraints**::
5694
5695 AA == AbbrevIndex(A) &
5696 N >= 0 &
5697 TV = TypeID(void) &
5698 AbsoluteIndex(V) >= NumFuncAddresses &
5699 TypeOf(AI) == TI for all I, 1 <= I <= N &
5700 IsFcnArgType(TI) for all I, 1 <= I <= N
5701
5702 **Updates**::
5703
5704 ++NumValuedInsts;
5705
5706 **Examples**::
5707
5708 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
5709 48:0| 3: <1, 3> | count 3;
5710 50:4| 3: <2> | @t0 = void;
5711 52:2| 3: <7, 32> | @t1 = i32;
5712 55:4| 3: <21, 0, 0, 1> | @t2 = void (i32);
5713 59:4| 0: <65534> | }
5714 ...
5715 92:0| 1: <65535, 12, 2> | function void @f0(i32 %p0) {
5716 | | // BlockID = 12
5717 100:0| 3: <1, 1> | blocks 1;
5718 102:4| 1: <65535, 11, 2> | constants { // BlockID = 11
5719 112:0| 3: <1, 1> | i32:
5720 114:4| 3: <4, 2> | %c0 = i32 1;
5721 117:0| 0: <65534> | }
5722 | | %b0:
5723 120:0| 3: <44, 0, 2, 0, 1> | call void %p0(i32 %c0);
5724 125:4| 3: <10> | ret void;
5725 127:2| 0: <65534> | }
5726
5727 Indirect Function Call
5728 ^^^^^^^^^^^^^^^^^^^^^^
5729
5730 The indirect function call calls a function using a value that is an indirect
5731 function address. It is different from the direct function call because we can't
5732 use the type signature of the corresponding literal function address to type
5733 check the construct.
5734
5735 **Syntax**::
5736
5737 %vN = TAIL call RT V (T1 A1, ... , TM AM); <A>
5738
5739 **Record**::
5740
5741 AA: <34, CC, RRT, VV, AA1, ... , AAM>
5742
5743 **Semantics**:
5744
5745 The indirect function call calls a function using a value ``V`` that is an
5746 indirect function address, and is assumed to return type ``RT``. The arguments
5747 ``A1`` through ``AM`` are passed in the order specified. The type of argument `` AI``
5748 must be type ``TI`` (for all I, 1 <= I <= N). Flag ``TAIL`` is optional. If it is
5749 included, it must be the literal ``tail``.
5750
5751 Each parameter type ``TI`` (1 <= I <= M), and return type ``RT``, must either be a
5752 primitive type, or a vector type. If the parameter type is an integer type, it
5753 must either be i32 or i64.
5754
5755 TAIL is encoded into calling convention value ``CC`` as follows:
5756
5757 ====== ==
5758 TAIL CC
5759 ====== ==
5760 '' 0
5761 'tail' 1
5762 ====== ==
5763
5764 The type signature of the called function is assumed to be::
5765
5766 RT (T1, ... , TN)
5767
5768 It isn't necessary to define this type in the :ref:`types
5769 block<link_for_types_block_section>`, since the type is inferred rather than
5770 used.
5771
5772 **Constraints**::
5773
5774 AA == AbbrevIndex(A) &
5775 RRT = TypeID(RT) &
5776 VV = RelativeIndex(V) &
5777 M >= 0 &
5778 AbsoluteIndex(V) >= NumFcnAddresses &
5779 TypeOf(AI) == TI for all I, 1 <= I <= M &
5780 IsFcnArgType(TI) for all I, 1 <= I <= M &
5781 IsFcnArgType(RT) &
5782 N == NumValuedInsts
5783
5784 **Updates**::
5785
5786 ++NumValuedInsts;
5787 TypeOf(%vN) = RT;
5788
5789 **Examples**::
5790
5791 40:0| 1: <65535, 17, 2> | types { // BlockID = 17
5792 48:0| 3: <1, 6> | count 6;
5793 50:4| 3: <7, 32> | @t0 = i32;
5794 53:6| 3: <3> | @t1 = float;
5795 55:4| 3: <4> | @t2 = double;
5796 57:2| 3: <21, 0, 0, 0, 1, 2> | @t3 = i32 (i32, float, double);
5797 62:6| 3: <21, 0, 0, 1, 2> | @t4 = i32 (float, double);
5798 67:4| 3: <2> | @t5 = void;
5799 69:2| 0: <65534> | }
5800 ...
5801 104:0| 1: <65535, 12, 2> | function
5802 | | i32
5803 | | @f0(i32 %p0, float %p1,
5804 | | double %p2) {
5805 | | // BlockID = 12
5806 112:0| 3: <1, 1> | blocks 1;
5807 | | %b0:
5808 114:4| 3: <44, 0, 3, 0, 2, 1> | %v0 = call i32
5809 | | %p0(float %p1, double %p2);
5810 120:6| 3: <10, 1> | ret i32 %v0;
5811 123:2| 0: <65534> | }
5812
5813 .. _link_for_memory_blocks_and_alignment_section:
5814
5815 Memory Blocks and Alignment
5816 ===========================
5817
5818 In general, variable and heap allocated data are represented as byte addressable
5819 memory blocks. Alignment is always a power of 2, and defines an expectation on
5820 the memory address. That is, an alignment is met if the memory address is
5821 (evenly) divisible by the alignment. Note that alignment of 0 is never allowed.
5822
5823 Alignment plays a role at two points:
5824
5825 * When you create a local/global variable
5826
5827 * When you load/store data using a pointer.
5828
5829 PNaClAsm allows most types to be placed at any address, and therefore can
5830 have alignment of 1. However, many architectures can load more efficiently
5831 if the data has an alignment that is larger than 1. As such, choosing a larger
5832 alignment can make load/stores more efficient.
5833
5834 On loads and stores, the alignment in the instruction is used to communicate
5835 what assumptions the :ref:`PNaCl translator<link_for_pnacl_translator>` can
5836 make when choosing the appropriate machine instructions. If the alignment is 1,
5837 it can't assume anything about the memory address used by the instruction. When
5838 the alignment is greater than one, it can use that information to potentially
5839 chose a more efficient sequence of instructions to do the load/store.
5840
5841 When laying out data within a variable, one also considers alignment. The reason
5842 for this is that if you want an address to be aligned, within the bytes defining
5843 the variable, you must choose an alignment for the variable that guarantees that
5844 alignment.
5845
5846 In PNaClAsm, the valid load/store alignments are:
5847
5848 =========== ==============
5849 Type Alignment
5850 =========== ==============
5851 i1 1
5852 i8 1
5853 i16 1
5854 i32 1
5855 i64 1
5856 Float 1, 4
5857 Double 1, 8
5858 <4 x i1> not applicable
5859 <8 x i1> not applicable
5860 <16 x i1> not applicable
5861 <16 x i8> 1
5862 <8 x i16> 2
5863 <4 x i32> 4
5864 <4 x float> 4
5865 =========== ==============
5866
5867 Note that only vectors do not have an alignment value of 1. Hence, they can't be
5868 placed at an arbitrary memory address. Also, since vectors on ``i1`` can't be
5869 loaded/stored, the alignment is not applicable for these types.
5870
5871 .. _link_for_intrinsic_functions_section:
5872
5873 Intrinsic Functions
5874 ===================
5875
5876 Intrinsic functions are special in PNaClAsm. They are implemented as specially
5877 named (external) function calls. The purpose of these intrinsic functions is to
5878 extend the PNaClAsm instruction set with additional functionality that is
5879 architecture specific. Hence, they either can't be implemented within PNaClAsm,
5880 or a non-architecture specific implementation may be too slow on some
5881 architectures. In such cases, the :ref:`PNaCl
5882 translator<link_for_pnacl_translator>` must fill in the corresponding
5883 implementation, since only it knows the architecture it is compiling down to.
5884
5885 Examples of intrinsic function calls are for concurrent operations, atomic
5886 operations, bulk memory moves, thread pointer operations, and long jumps.
5887
5888 It should be noted that calls to intrinsic functions do not have the same
5889 calling type constraints as ordinary functions. That is, an intrinsic can use
5890 any integer type for arguments/results, unlike ordinary functions (which
5891 restrict integer types to ``i32`` and ``i64``).
5892
5893 See the :doc:`PNaCl bitcode reference manual<pnacl-bitcode-abi>` for the full
5894 set of intrinsic functions allowed. Note that in PNaClAsm, all pointer types to
5895 an (LLVM) intrinsic function is converted to type i32.
5896
5897 .. _link_for_support_functions_section:
5898
5899 Support Functions
5900 =================
5901
5902 Defines functions used to convert syntactic representation to values in the
5903 corresponding record.
5904
5905 SignRotate
5906 ----------
5907
5908 The SignRotate function encodes a signed integer in an easily compressible
5909 form. This is done by rotating the sign bit to the rightmost bit, rather than
5910 the leftmost bit. By doing this rotation, both small positive and negative
5911 integers are small (unsigned) integers. Therefore, all small integers can be
5912 encoded as a small (unsigned) integers.
5913
5914 The definition of SignRotate(N) is:
5915
5916 ======== ============= =========
5917 Argument Value Condition
5918 ======== ============= =========
5919 N abs(N)<<1 N >= 0
5920 N abs(N)<<1 + 1 N < 0
5921 ======== ============= =========
5922
5923 .. _link_for_absolute_index_section:
5924
5925 AbsoluteIndex
5926 -------------
5927
5928 Bitcode IDs of the forms ``@fN``, ``@gN``, ``%pN``, ``%cN``, and ``%vN``, are
5929 combined into a single index space. This can be done because of the ordering
5930 imposed by PNaClAsm. All function address bitcode IDs must be defined before any
5931 of the other forms of bitcode IDs. All global address bitcode IDs must be
5932 defined before any local bitcode IDs. Within a function block, the parameter
5933 bitcode IDs must be defined before constant IDs, and constant IDs must be
5934 defined before instruction value IDs.
5935
5936 Hence, within a function block, it is safe to refer to all of these
5937 bitcode IDs using a single *absolute* index. The absolute index for
5938 each kind of bitcode ID is computed as follows:
5939
5940 ========== ===================================================================
5941 Bitcode ID AbsoluteIndex
5942 ========== ===================================================================
5943 @tN N
5944 @fN N
5945 @gN N + NumFcnAddresses
5946 @pN N + NumFcnAddresses + NumGlobalAddresses
5947 @cN N + NumFcnAddresses + NumGlobalAddresses + NumParams
5948 @vN N + NumFcnAddresses + NumGlobalAddresses + NumParams + NumFcnConsts
5949 ========== ===================================================================
5950
5951 .. _link_for_relative_index:
5952
5953 RelativeIndex
5954 -------------
5955
5956 Relative indices are used to refer to values within instructions of a function.
5957 The relative index of an ID is always defined in terms of the index associated
5958 with the next value generating instruction. It is defined as follows::
5959
5960 RelativeIndex(J) = AbsoluteIndex(%vN) - AbsoluteIndex(J)
5961
5962 where::
5963
5964 N = NumValuedInsts
5965
5966 AbbrevIndex
5967 -----------
5968
5969 This function converts user-defined abbreviation indices to the corresponding
5970 internal abbreviation index saved in the bitcode file. It adds 4 to its
5971 argument, since there are 4 predefined internal abbreviation indices (0, 1, 2,
5972 and 3).
5973
5974 ========= ==============
5975 N AbbrevIndex(N)
5976 ========= ==============
5977 undefined 3
5978 %aA A + 4
5979 @aA A + 4
5980 ========= ==============
5981
5982 Log2
5983 ----
5984
5985 This is the 32-bit log2 value of its argument.
5986
5987 BitSizeOf
5988 ---------
5989
5990 Returns the number of bits needed to represent its argument (a type).
5991
5992 ======= ================
5993 T BitSizeOf
5994 ======= ================
5995 i1 1
5996 i8 8
5997 i16 16
5998 i32 32
5999 i64 64
6000 float 32
6001 double 64
6002 <N X T> N * BitSizeOf(T)
6003 ======= ================
6004
6005 UnderlyingType
6006 --------------
6007
6008 Returns the primitive type of the type construct. For primitive types, the
6009 *UnderlyingType* is itself. For vector types, the base type of the vector is the
6010 underlying type.
6011
6012 UnderlyingCount
6013 ---------------
6014
6015 Returns the size of the vector if given a vector, and 0 for primitive types.
6016 Note that this function is used to check if two vectors are of the same size.
6017 It is also used to test if two types are either primitive (i.e. UnderlyingCount
6018 returns 0 for both types) or are vectors of the same size (i.e. UnderlyingCount
6019 returns the same non-zero value).
6020
6021 IsInteger
6022 ---------
6023
6024 Returns true if the argument is in {i1, i8, i16, i32, i64}.
6025
6026 IsFloat
6027 -------
6028
6029 Returns true if the argument is in {``float``, ``double``}.
6030
6031 IsVector
6032 --------
6033
6034 Returns true if the argument is a vector type.
6035
6036 IsPrimitive
6037 -----------
6038
6039 Returns true if the argument is a primitive type. That is::
6040
6041 IsPrimitive(T) == IsInteger(T) or IsFloat(T)
6042
6043 IsFcnArgType
6044 ------------
6045
6046 Returns true if the argument is a primitive type or a vector type. Further,
6047 if it is an integer type, it must be i32 or i64. That is::
6048
6049 IsFcnArgType(T) = (IsInteger(T) and (i32 = BitSizeOf(T)
6050 or i64 == BitSizeOf(T)))
6051 or IsFloat(T) or IsVector(T)
6052
6053 .. _link_for_abbreviations_section:
6054
6055 Abbreviations
6056 =============
6057
6058 Abbreviations are used to convert PNaCl records to a sequence of bits. PNaCl
6059 uses the same strategy as `LLVM's bitcode file format
6060 <http://llvm.org/docs/BitCodeFormat.html>`_. See that document for more
6061 details.
6062
6063 It should be noted that we replace LLVM's header (called the *Bitcode Wrapper
6064 Format*) with the bytes of the :ref:`PNaCl record
6065 header<link_for_header_record_section>`. In addition, PNaCl bitcode files do
6066 not allow *blob* abbreviation.
6067
6068 .. _link_for_abbreviations_block_section:
6069
6070 Abbreviations Block
6071 -------------------
6072
6073 The abbreviations block is the first block in the module build. The
6074 block is divided into sections. Each section is a sequence of records. Each
6075 record in the sequence defines a user-defined abbreviation. Each section
6076 defines abbreviations that can be applied to all (succeeding) blocks of a
6077 particular kind. These abbreviations are denoted by the (global) ID of the form
6078 *@aN*.
6079
6080 In terms of `LLVM's bitcode file format
6081 <http://llvm.org/docs/BitCodeFormat.html>`_, the abbreviations block is called a
6082 *BLOCKINFO* block. Records *SETBID* and *DEFINE_ABBREV* are the only records
6083 allowed in PNaCl's abbreviation block (i.e. it doesn't allow *BLOCKNAME* and
6084 *SETRECORDNAME* records).
6085
6086 TODO
6087 ----
6088
6089 Extend this document to describe PNaCl's bitcode bit sequencer
6090 without requiring the reader to refer to `LLVM's bitcode file
6091 format <http://llvm.org/docs/BitCodeFormat.html>`_.
OLDNEW
« no previous file with comments | « native_client_sdk/src/doc/overview.rst ('k') | native_client_sdk/src/doc/reference/pnacl-c-cpp-language-support.rst » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698