1 ==============================
2 PNaCl Bitcode Reference Manual
3 ==============================
13 This document is a reference manual for the PNaCl bitcode format. It describes
14 the bitcode on a *semantic* level; the physical encoding level will be described
15 elsewhere. For the purpose of this document, the textual form of LLVM IR is
16 used to describe instructions and other bitcode constructs.
18 Since the PNaCl bitcode is based to a large extent on LLVM IR as of
19 version 3.3, many sections in this document point to a relevant section
20 of the LLVM language reference manual. Only the changes, restrictions
21 and variations specific to PNaCl are described---full semantic
22 descriptions are not duplicated from the LLVM reference manual.
27 A PNaCl portable executable (**pexe** in short) is a single LLVM IR module.
32 The data model for PNaCl bitcode is fixed at little-endian ILP32: pointers are
33 32 bits in size. 64-bit integer types are also supported natively via the i64
34 type (for example, a front-end can generate these from the C/C++ type
37 Floating point support is fixed at IEEE 754 32-bit and 64-bit values (f32 and
40 .. _bitcode_linkagetypes:
45 `LLVM LangRef: Linkage Types
46 <http://llvm.org/releases/3.3/docs/LangRef.html#linkage>`_
48 The linkage types supported by PNaCl bitcode are ``internal`` and ``external``.
49 A single function in the pexe, named ``_start``, has the linkage type
50 ``external``. All the other functions and globals have the linkage type
56 `LLVM LangRef: Calling Conventions
57 <http://llvm.org/releases/3.3/docs/LangRef.html#callingconv>`_
59 The only calling convention supported by PNaCl bitcode is ``ccc`` - the C
65 `LLVM LangRef: Visibility Styles
66 <http://llvm.org/releases/3.3/docs/LangRef.html#visibility-styles>`_
68 PNaCl bitcode does not support visibility styles.
70 .. _bitcode_globalvariables:
75 `LLVM LangRef: Global Variables
76 <http://llvm.org/releases/3.3/docs/LangRef.html#globalvars>`_
78 Restrictions on global variables:
80 * PNaCl bitcode does not support LLVM IR TLS models. See
81 :ref:`language_support_threading` for more details.
82 * Restrictions on :ref:`linkage types <bitcode_linkagetypes>`.
83 * The ``addrspace``, ``section``, ``unnamed_addr`` and
84 ``externally_initialized`` attributes are not supported.
86 Every global variable must have an initializer. Each initializer must be
87 either a *SimpleElement* or a *CompoundElement*, defined as follows.
89 A *SimpleElement* is one of the following:
91 1) An i8 array literal or ``zeroinitializer``:
97 [SIZE x i8] zeroinitializer
99 2) A reference to a *GlobalValue* (a function or global variable) with an
100 optional 32-bit byte offset added to it (the addend, which may be
106 ptrtoint (TYPE* @GLOBAL to i32)
107 add (i32 ptrtoint (TYPE* @GLOBAL to i32), i32 ADDEND)
109 A *CompoundElement* is a unnamed, packed struct containing more than one
115 `LLVM LangRef: Functions
116 <http://llvm.org/releases/3.3/docs/LangRef.html#functionstructure>`_
118 The restrictions on :ref:`linkage types <bitcode_linkagetypes>`, calling
119 conventions and visibility styles apply to functions. In addition, the following
120 are not supported for functions:
122 * Function attributes (either for the the function itself, its parameters or its
124 * Garbage collector name (``gc``).
125 * Functions with a variable number of arguments (*vararg*).
126 * Alignment (``align``).
131 `LLVM LangRef: Aliases
132 <http://llvm.org/releases/3.3/docs/LangRef.html#aliases>`_
134 PNaCl bitcode does not support aliases.
139 `LLVM LangRef: Named Metadata
140 <http://llvm.org/releases/3.3/docs/LangRef.html#namedmetadatastructure>`_
142 While PNaCl bitcode has provisions for debugging metadata, it is not considered
143 part of the stable ABI. It exists for tool support and should not appear in
146 Other kinds of LLVM metadata are not supported.
148 Module-Level Inline Assembly
149 ----------------------------
151 `LLVM LangRef: Module-Level Inline Assembly
152 <http://llvm.org/releases/3.3/docs/LangRef.html#moduleasm>`_
154 PNaCl bitcode does not support inline assembly.
156 Volatile Memory Accesses
157 ------------------------
159 `LLVM LangRef: Volatile Memory Accesses
160 <http://llvm.org/releases/3.3/docs/LangRef.html#volatile>`_
162 PNaCl bitcode does not support volatile memory accesses. The
163 ``volatile`` attribute on loads and stores is not supported. See the
164 :doc:`pnacl-c-cpp-language-support` for more details.
166 Memory Model for Concurrent Operations
167 --------------------------------------
169 `LLVM LangRef: Memory Model for Concurrent Operations
170 <http://llvm.org/releases/3.3/docs/LangRef.html#memmodel>`_
172 See the :doc:`PNaCl C/C++ Language Support <pnacl-c-cpp-language-support>`
178 `LLVM LangRef: Fast-Math Flags
179 <http://llvm.org/releases/3.3/docs/LangRef.html#fastmath>`_
181 Fast-math mode is not currently supported by the PNaCl bitcode.
186 `LLVM LangRef: Type System
187 <http://llvm.org/releases/3.3/docs/LangRef.html#typesystem>`_
189 The LLVM types allowed in PNaCl bitcode are restricted, as follows:
194 * The only scalar types allowed are integer, float (32-bit floating point),
195 double (64-bit floating point) and void.
197 * The only integer sizes allowed are i1, i8, i16, i32 and i64.
198 * The only integer sizes allowed for function arguments and function return
199 values are i32 and i64.
204 The only vector types allowed are:
206 * 128-bit vectors integers of elements size i8, i16, i32.
207 * 128-bit vectors of float elements.
208 * Vectors of i1 type with element counts corresponding to the allowed
209 element counts listed previously (their width is therefore not
212 Array and struct types
213 ----------------------
215 Array and struct types are only allowed in
216 :ref:`global variable initializers <bitcode_globalvariables>`.
218 .. _bitcode_pointertypes:
223 Only the following pointer types are allowed:
225 * Pointers to valid PNaCl bitcode scalar types, as specified above, except for
227 * Pointers to valid PNaCl bitcode vector types, as specified above, except for
229 * Pointers to functions.
231 In addition, the address space for all pointers must be 0.
233 A pointer is *inherent* when it represents the return value of an ``alloca``
234 instruction, or is an address of a global value.
236 A pointer is *normalized* if it's either:
239 * Is the return value of a ``bitcast`` instruction.
240 * Is the return value of a ``inttoptr`` instruction.
245 `LLVM LangRef: Undefined Values
246 <http://llvm.org/releases/3.3/docs/LangRef.html#undefvalues>`_
248 ``undef`` is only allowed within functions, not in global variable initializers.
253 `LLVM LangRef: Constant Expressions
254 <http://llvm.org/releases/3.3/docs/LangRef.html#constant-expressions>`_
256 Constant expressions are only allowed in
257 :ref:`global variable initializers <bitcode_globalvariables>`.
262 Metadata Nodes and Metadata Strings
263 -----------------------------------
265 `LLVM LangRef: Metadata Nodes and Metadata Strings
266 <http://llvm.org/releases/3.3/docs/LangRef.html#metadata>`_
268 While PNaCl bitcode has provisions for debugging metadata, it is not considered
269 part of the stable ABI. It exists for tool support and should not appear in
272 Other kinds of LLVM metadata are not supported.
274 Intrinsic Global Variables
275 ==========================
277 `LLVM LangRef: Intrinsic Global Variables
278 <http://llvm.org/releases/3.3/docs/LangRef.html#intrinsic-global-variables>`_
280 PNaCl bitcode does not support intrinsic global variables.
284 Errno and errors in arithmetic instructions
285 ===========================================
287 Some arithmetic instructions and intrinsics have the similar semantics to
288 libc math functions, but differ in the treatment of ``errno``. While the
289 libc functions may set ``errno`` for domain errors, the instructions and
290 intrinsics do not. This is because the variable ``errno`` is not special
291 and is not required to be part of the program.
293 Instruction Reference
294 =====================
296 List of allowed instructions
297 ----------------------------
299 This is a list of LLVM instructions supported by PNaCl bitcode. Where
300 applicable, PNaCl-specific restrictions are provided.
302 .. TODO: explain instructions or link in the future
304 The following attributes are disallowed for all instructions:
306 * ``nsw`` and ``nuw``
309 Only the LLVM instructions listed here are supported by PNaCl bitcode.
315 i1 values are disallowed for ``switch``.
317 * ``add``, ``sub``, ``mul``, ``shl``, ``udiv``, ``sdiv``, ``urem``, ``srem``,
320 These arithmetic operations are disallowed on values of type ``i1``.
322 Integer division (``udiv``, ``sdiv``, ``urem``, ``srem``) by zero is
323 guaranteed to trap in PNaCl bitcode.
334 The frem instruction has the semantics of the libc fmod function for
335 computing the floating point remainder. If the numerator is infinity, or
336 denominator is zero, or either are NaN, then the result is NaN.
337 Unlike the libc fmod function, this does not set ``errno`` when the
338 result is NaN (see the :ref:`instructions and errno <ir_and_errno>`
343 See :ref:`alloca instructions <bitcode_allocainst>`.
345 * ``load``, ``store``
347 The pointer argument of these instructions must be a *normalized* pointer (see
348 :ref:`pointer types <bitcode_pointertypes>`). The ``volatile`` and ``atomic``
349 attributes are not supported. Loads and stores of the type ``i1`` and ``<? x
350 i1>`` are not supported.
352 These instructions must follow the following alignment restrictions:
354 * On integer memory accesses: ``align 1``.
355 * On ``float`` memory accesses: ``align 1`` or ``align 4``.
356 * On ``double`` memory accesses: ``align 1`` or ``align 8``.
357 * On vector memory accesses: alignment at the vector's element width, for
358 example ``<4 x i32>`` must be ``align 4``.
372 The pointer argument of a ``ptrtoint`` instruction must be a *normalized*
373 pointer (see :ref:`pointer types <bitcode_pointertypes>`) and the integer
374 argument must be an i32.
378 The integer argument of a ``inttoptr`` instruction must be an i32.
382 The pointer argument of a ``bitcast`` instruction must be a *inherent* pointer
383 (see :ref:`pointer types <bitcode_pointertypes>`).
394 .. _bitcode_allocainst:
399 The only allowed type for ``alloca`` instructions in PNaCl bitcode is i8. The
400 size argument must be an i32. For example:
405 %buf = alloca i8, i32 8, align 4
410 `LLVM LangRef: Intrinsic Functions
411 <http://llvm.org/releases/3.3/docs/LangRef.html#intrinsics>`_
413 List of allowed intrinsics
414 --------------------------
416 The only intrinsics supported by PNaCl bitcode are the following.
422 These intrinsics are only supported with an i32 ``len`` argument.
426 The overloaded ``llvm.bswap`` intrinsic is only supported with the following
427 argument types: i16, i32, i64 (the types supported by C-style GCC builtins).
433 The overloaded ``llvm.ctlz``, ``llvm.cttz``, and ``llvm.ctpop`` intrinsics
434 are only supported with the i32 and i64 argument types (the types
435 supported by C-style GCC builtins).
439 The overloaded ``llvm.fabs`` intrinsic is supported for float, double and
440 ``<4 x float>`` argument types. It returns the absolute value of
441 the argument. Some notable points: it returns ``+0.0`` when given ``-0.0``,
442 ``+inf`` when given ``-inf``, and a positive ``NaN`` when given any
445 NOTE: This intrinsic was introduced in the pepper_42 SDK.
449 The overloaded ``llvm.sqrt`` intrinsic is only supported for float
450 and double arguments types. This has the same semantics as the libc
451 sqrt function, returning ``NaN`` for values less than ``-0.0``.
452 However, this does not set ``errno`` when the result is NaN (see the
453 :ref:`instructions and errno <ir_and_errno>` section).
456 * ``llvm.stackrestore``
458 These intrinsics are used to implement language features like scoped automatic
459 variable sized arrays in C99. ``llvm.stacksave`` returns a value that
460 represents the current state of the stack. This value may only be used as the
461 argument to ``llvm.stackrestore``, which restores the stack to the given
466 This intrinsic is lowered to a target dependent trap instruction, which aborts
469 * ``llvm.nacl.read.tp``
471 See :ref:`thread pointer related intrinsics
472 <bitcode_threadpointerintrinsics>`.
474 * ``llvm.nacl.longjmp``
475 * ``llvm.nacl.setjmp``
477 See :ref:`Setjmp and Longjmp <bitcode_setjmplongjmp>`.
479 * ``llvm.nacl.atomic.store``
480 * ``llvm.nacl.atomic.load``
481 * ``llvm.nacl.atomic.rmw``
482 * ``llvm.nacl.atomic.cmpxchg``
483 * ``llvm.nacl.atomic.fence``
484 * ``llvm.nacl.atomic.fence.all``
485 * ``llvm.nacl.atomic.is.lock.free``
487 See :ref:`atomic intrinsics <bitcode_atomicintrinsics>`.
489 .. _bitcode_threadpointerintrinsics:
491 Thread pointer related intrinsics
492 ---------------------------------
497 declare i8* @llvm.nacl.read.tp()
499 Returns a read-only thread pointer. The value is controlled by the embedding
502 .. _bitcode_setjmplongjmp:
510 declare void @llvm.nacl.longjmp(i8* %jmpbuf, i32)
511 declare i32 @llvm.nacl.setjmp(i8* %jmpbuf)
513 These intrinsics implement the semantics of C11 ``setjmp`` and ``longjmp``. The
514 ``jmpbuf`` pointer must be 64-bit aligned and point to at least 1024 bytes of
517 .. _bitcode_atomicintrinsics:
525 declare iN @llvm.nacl.atomic.load.<size>(
526 iN* <source>, i32 <memory_order>)
527 declare void @llvm.nacl.atomic.store.<size>(
528 iN <operand>, iN* <destination>, i32 <memory_order>)
529 declare iN @llvm.nacl.atomic.rmw.<size>(
530 i32 <computation>, iN* <object>, iN <operand>, i32 <memory_order>)
531 declare iN @llvm.nacl.atomic.cmpxchg.<size>(
532 iN* <object>, iN <expected>, iN <desired>,
533 i32 <memory_order_success>, i32 <memory_order_failure>)
534 declare void @llvm.nacl.atomic.fence(i32 <memory_order>)
535 declare void @llvm.nacl.atomic.fence.all()
537 Each of these intrinsics is overloaded on the ``iN`` argument, which is
538 reflected through ``<size>`` in the overload's name. Integral types of
539 8, 16, 32 and 64-bit width are supported for these arguments.
541 The ``@llvm.nacl.atomic.rmw`` intrinsic implements the following
542 read-modify-write operations, from the general and arithmetic sections
543 of the C11/C++11 standards:
552 For all of these read-modify-write operations, the returned value is
553 that at ``object`` before the computation. The ``computation`` argument
554 must be a compile-time constant.
556 All atomic intrinsics also support C11/C++11 memory orderings, which
557 must be compile-time constants.
559 Integer values for these computations and memory orderings are defined
560 in ``"llvm/IR/NaClAtomicIntrinsics.h"``.
562 The ``@llvm.nacl.atomic.fence.all`` intrinsic is equivalent to the
563 ``@llvm.nacl.atomic.fence`` intrinsic with sequentially consistent
564 ordering and compiler barriers preventing most non-atomic memory
565 accesses from reordering around it.
570 These intrinsics allow PNaCl to support C11/C++11 style atomic
571 operations as well as some legacy GCC-style ``__sync_*`` builtins
572 while remaining stable as the LLVM codebase changes. The user isn't
573 expected to use these intrinsics directly.
578 declare i1 @llvm.nacl.atomic.is.lock.free(i32 <byte_size>, i8* <address>)
580 The ``llvm.nacl.atomic.is.lock.free`` intrinsic is designed to
581 determine at translation time whether atomic operations of a certain
582 ``byte_size`` (a compile-time constant), at a particular ``address``,
583 are lock-free or not. This reflects the C11 ``atomic_is_lock_free``
584 function from header ``<stdatomic.h>`` and the C++11 ``is_lock_free``
585 member function in header ``<atomic>``. It can be used through the
586 ``__nacl_atomic_is_lock_free`` builtin.