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, many sections
19 in this document point to a relevant section of the LLVM language reference
20 manual. Only the changes, restrictions and variations specific to PNaCl are
21 described---full semantic descriptions are not duplicated from the LLVM
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 `PNaCl Developer's Guide <PNaClDeveloperGuide.html>`_ for more
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.
201 Array and struct types
202 ----------------------
204 Array and struct types are only allowed in
205 :ref:`global variable initializers <bitcode_globalvariables>`.
207 .. _bitcode_pointertypes:
212 Only the following pointer types are allowed:
214 * Pointers to valid PNaCl bitcode scalar types, as specified above.
215 * Pointers to functions.
217 In addition, the address space for all pointers must be 0.
219 A pointer is *inherent* when it represents the return value of an ``alloca``
220 instruction, or is an address of a global value.
222 A pointer is *normalized* if it's either:
225 * Is the return value of a ``bitcast`` instruction.
226 * Is the return value of a ``inttoptr`` instruction.
231 `LLVM LangRef: Undefined Values
232 <http://llvm.org/releases/3.3/docs/LangRef.html#undefvalues>`_
234 ``undef`` is only allowed within functions, not in global variable initializers.
239 `LLVM LangRef: Constant Expressions
240 <http://llvm.org/releases/3.3/docs/LangRef.html#constant-expressions>`_
242 Constant expressions are only allowed in
243 :ref:`global variable initializers <bitcode_globalvariables>`.
248 Metadata Nodes and Metadata Strings
249 -----------------------------------
251 `LLVM LangRef: Metadata Nodes and Metadata Strings
252 <http://llvm.org/releases/3.3/docs/LangRef.html#metadata>`_
254 While PNaCl bitcode has provisions for debugging metadata, it is not considered
255 part of the stable ABI. It exists for tool support and should not appear in
258 Other kinds of LLVM metadata are not supported.
260 Intrinsic Global Variables
261 ==========================
263 `LLVM LangRef: Intrinsic Global Variables
264 <http://llvm.org/releases/3.3/docs/LangRef.html#intrinsic-global-variables>`_
266 PNaCl bitcode does not support intrinsic global variables.
268 Instruction Reference
269 =====================
271 List of allowed instructions
272 ----------------------------
274 This is a list of LLVM instructions supported by PNaCl bitcode. Where
275 applicable, PNaCl-specific restrictions are provided.
277 .. TODO: explain instructions or link in the future
279 The following attributes are disallowed for all instructions:
281 * ``nsw`` and ``nuw``
284 Only the LLVM instructions listed here are supported by PNaCl bitcode.
290 i1 values are disallowed for ``switch``.
292 * ``add``, ``sub``, ``mul``, ``shl``, ``udiv``, ``sdiv``, ``urem``, ``srem``,
295 These arithmetic operations are disallowed on values of type ``i1``.
297 Integer division (``udiv``, ``sdiv``, ``urem``, ``srem``) by zero is
298 guaranteed to trap in PNaCl bitcode.
310 See :ref:`alloca instructions <bitcode_allocainst>`.
312 * ``load``, ``store``
314 The pointer argument of these instructions must be a *normalized* pointer (see
315 :ref:`pointer types <bitcode_pointertypes>`). The ``volatile`` and ``atomic``
316 attributes are not supported. Loads and stores of the type ``i1`` are not
319 These instructions must use ``align 1`` on integer memory accesses, ``align 4``
320 for ``float`` accesses and ``align 8`` for ``double`` accesses.
334 The pointer argument of a ``ptrtoint`` instruction must be a *normalized*
335 pointer (see :ref:`pointer types <bitcode_pointertypes>`) and the integer
336 argument must be an i32.
340 The integer argument of a ``inttoptr`` instruction must be an i32.
344 The pointer argument of a ``bitcast`` instruction must be a *inherent* pointer
345 (see :ref:`pointer types <bitcode_pointertypes>`).
353 .. _bitcode_allocainst:
358 The only allowed type for ``alloca`` instructions in PNaCl bitcode is i8. The
359 size argument must be an i32. For example:
364 %buf = alloca i8, i32 8, align 4
369 `LLVM LangRef: Intrinsic Functions
370 <http://llvm.org/releases/3.3/docs/LangRef.html#intrinsics>`_
372 List of allowed intrinsics
373 --------------------------
375 The only intrinsics supported by PNaCl bitcode are the following.
381 These intrinsics are only supported with an i32 ``len`` argument.
385 The overloaded ``llvm.bswap`` intrinsic is only supported with the following
386 argument types: i16, i32, i64 (the types supported by C-style GCC builtins).
392 The overloaded llvm.ctlz, llvm.cttz, and llvm.ctpop intrinsics are only
393 supported with the i32 and i64 argument types (the types supported by
394 C-style GCC builtins).
398 The overloaded ``llvm.sqrt`` intrinsic is only supported for float
399 and double arguments types. Unlike the standard LLVM intrinsic,
400 PNaCl guarantees that llvm.sqrt returns a QNaN for values less than -0.0.
403 * ``llvm.stackrestore``
405 These intrinsics are used to implement language features like scoped automatic
406 variable sized arrays in C99. ``llvm.stacksave`` returns a value that
407 represents the current state of the stack. This value may only be used as the
408 argument to ``llvm.stackrestore``, which restores the stack to the given
413 This intrinsic is lowered to a target dependent trap instruction, which aborts
416 * ``llvm.nacl.read.tp``
418 See :ref:`thread pointer related intrinsics
419 <bitcode_threadpointerintrinsics>`.
421 * ``llvm.nacl.longjmp``
422 * ``llvm.nacl.setjmp``
424 See :ref:`Setjmp and Longjmp <bitcode_setjmplongjmp>`.
426 * ``llvm.nacl.atomic.store``
427 * ``llvm.nacl.atomic.load``
428 * ``llvm.nacl.atomic.rmw``
429 * ``llvm.nacl.atomic.cmpxchg``
430 * ``llvm.nacl.atomic.fence``
431 * ``llvm.nacl.atomic.fence.all``
432 * ``llvm.nacl.atomic.is.lock.free``
434 See :ref:`atomic intrinsics <bitcode_atomicintrinsics>`.
436 .. _bitcode_threadpointerintrinsics:
438 Thread pointer related intrinsics
439 ---------------------------------
444 declare i8* @llvm.nacl.read.tp()
446 Returns a read-only thread pointer. The value is controlled by the embedding
449 .. _bitcode_setjmplongjmp:
457 declare void @llvm.nacl.longjmp(i8* %jmpbuf, i32)
458 declare i32 @llvm.nacl.setjmp(i8* %jmpbuf)
460 These intrinsics implement the semantics of C11 ``setjmp`` and ``longjmp``. The
461 ``jmpbuf`` pointer must be 64-bit aligned and point to at least 1024 bytes of
464 .. _bitcode_atomicintrinsics:
472 declare iN @llvm.nacl.atomic.load.<size>(
473 iN* <source>, i32 <memory_order>)
474 declare void @llvm.nacl.atomic.store.<size>(
475 iN <operand>, iN* <destination>, i32 <memory_order>)
476 declare iN @llvm.nacl.atomic.rmw.<size>(
477 i32 <computation>, iN* <object>, iN <operand>, i32 <memory_order>)
478 declare iN @llvm.nacl.atomic.cmpxchg.<size>(
479 iN* <object>, iN <expected>, iN <desired>,
480 i32 <memory_order_success>, i32 <memory_order_failure>)
481 declare void @llvm.nacl.atomic.fence(i32 <memory_order>)
482 declare void @llvm.nacl.atomic.fence.all()
484 Each of these intrinsics is overloaded on the ``iN`` argument, which is
485 reflected through ``<size>`` in the overload's name. Integral types of
486 8, 16, 32 and 64-bit width are supported for these arguments.
488 The ``@llvm.nacl.atomic.rmw`` intrinsic implements the following
489 read-modify-write operations, from the general and arithmetic sections
490 of the C11/C++11 standards:
499 For all of these read-modify-write operations, the returned value is
500 that at ``object`` before the computation. The ``computation`` argument
501 must be a compile-time constant.
503 All atomic intrinsics also support C11/C++11 memory orderings, which
504 must be compile-time constants.
506 Integer values for these computations and memory orderings are defined
507 in ``"llvm/IR/NaClAtomicIntrinsics.h"``.
509 The ``@llvm.nacl.atomic.fence.all`` intrinsic is equivalent to the
510 ``@llvm.nacl.atomic.fence`` intrinsic with sequentially consistent
511 ordering and compiler barriers preventing most non-atomic memory
512 accesses from reordering around it.
517 These intrinsics allow PNaCl to support C11/C++11 style atomic
518 operations as well as some legacy GCC-style ``__sync_*`` builtins
519 while remaining stable as the LLVM codebase changes. The user isn't
520 expected to use these intrinsics directly.
525 declare i1 @llvm.nacl.atomic.is.lock.free(i32 <byte_size>, i8* <address>)
527 The ``llvm.nacl.atomic.is.lock.free`` intrinsic is designed to
528 determine at translation time whether atomic operations of a certain
529 ``byte_size`` (a compile-time constant), at a particular ``address``,
530 are lock-free or not. This reflects the C11 ``atomic_is_lock_free``
531 function from header ``<stdatomic.h>`` and the C++11 ``is_lock_free``
532 member function in header ``<atomic>``. It can be used through the
533 ``__nacl_atomic_is_lock_free`` builtin.