1.. role:: raw-html(raw)
2   :format: html
3
4========================
5LLVM Bitcode File Format
6========================
7
8.. contents::
9   :local:
10
11Abstract
12========
13
14This document describes the LLVM bitstream file format and the encoding of the
15LLVM IR into it.
16
17Overview
18========
19
20What is commonly known as the LLVM bitcode file format (also, sometimes
21anachronistically known as bytecode) is actually two things: a `bitstream
22container format`_ and an `encoding of LLVM IR`_ into the container format.
23
24The bitstream format is an abstract encoding of structured data, very similar to
25XML in some ways.  Like XML, bitstream files contain tags, and nested
26structures, and you can parse the file without having to understand the tags.
27Unlike XML, the bitstream format is a binary encoding, and unlike XML it
28provides a mechanism for the file to self-describe "abbreviations", which are
29effectively size optimizations for the content.
30
31LLVM IR files may be optionally embedded into a `wrapper`_ structure that makes
32it easy to embed extra data along with LLVM IR files.
33
34This document first describes the LLVM bitstream format, describes the wrapper
35format, then describes the record structure used by LLVM IR files.
36
37.. _bitstream container format:
38
39Bitstream Format
40================
41
42The bitstream format is literally a stream of bits, with a very simple
43structure.  This structure consists of the following concepts:
44
45* A "`magic number`_" that identifies the contents of the stream.
46
47* Encoding `primitives`_ like variable bit-rate integers.
48
49* `Blocks`_, which define nested content.
50
51* `Data Records`_, which describe entities within the file.
52
53* Abbreviations, which specify compression optimizations for the file.
54
55Note that the :doc:`llvm-bcanalyzer <CommandGuide/llvm-bcanalyzer>` tool can be
56used to dump and inspect arbitrary bitstreams, which is very useful for
57understanding the encoding.
58
59.. _magic number:
60
61Magic Numbers
62-------------
63
64The first two bytes of a bitcode file are 'BC' (``0x42``, ``0x43``).  The second
65two bytes are an application-specific magic number.  Generic bitcode tools can
66look at only the first two bytes to verify the file is bitcode, while
67application-specific programs will want to look at all four.
68
69.. _primitives:
70
71Primitives
72----------
73
74A bitstream literally consists of a stream of bits, which are read in order
75starting with the least significant bit of each byte.  The stream is made up of
76a number of primitive values that encode a stream of unsigned integer values.
77These integers are encoded in two ways: either as `Fixed Width Integers`_ or as
78`Variable Width Integers`_.
79
80.. _Fixed Width Integers:
81.. _fixed-width value:
82
83Fixed Width Integers
84^^^^^^^^^^^^^^^^^^^^
85
86Fixed-width integer values have their low bits emitted directly to the file.
87For example, a 3-bit integer value encodes 1 as 001.  Fixed width integers are
88used when there are a well-known number of options for a field.  For example,
89boolean values are usually encoded with a 1-bit wide integer.
90
91.. _Variable Width Integers:
92.. _Variable Width Integer:
93.. _variable-width value:
94
95Variable Width Integers
96^^^^^^^^^^^^^^^^^^^^^^^
97
98Variable-width integer (VBR) values encode values of arbitrary size, optimizing
99for the case where the values are small.  Given a 4-bit VBR field, any 3-bit
100value (0 through 7) is encoded directly, with the high bit set to zero.  Values
101larger than N-1 bits emit their bits in a series of N-1 bit chunks, where all
102but the last set the high bit.
103
104For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a vbr4
105value.  The first set of four bits indicates the value 3 (011) with a
106continuation piece (indicated by a high bit of 1).  The next word indicates a
107value of 24 (011 << 3) with no continuation.  The sum (3+24) yields the value
10827.
109
110.. _char6-encoded value:
111
1126-bit characters
113^^^^^^^^^^^^^^^^
114
1156-bit characters encode common characters into a fixed 6-bit field.  They
116represent the following characters with the following 6-bit values:
117
118::
119
120  'a' .. 'z' ---  0 .. 25
121  'A' .. 'Z' --- 26 .. 51
122  '0' .. '9' --- 52 .. 61
123         '.' --- 62
124         '_' --- 63
125
126This encoding is only suitable for encoding characters and strings that consist
127only of the above characters.  It is completely incapable of encoding characters
128not in the set.
129
130Word Alignment
131^^^^^^^^^^^^^^
132
133Occasionally, it is useful to emit zero bits until the bitstream is a multiple
134of 32 bits.  This ensures that the bit position in the stream can be represented
135as a multiple of 32-bit words.
136
137Abbreviation IDs
138----------------
139
140A bitstream is a sequential series of `Blocks`_ and `Data Records`_.  Both of
141these start with an abbreviation ID encoded as a fixed-bitwidth field.  The
142width is specified by the current block, as described below.  The value of the
143abbreviation ID specifies either a builtin ID (which have special meanings,
144defined below) or one of the abbreviation IDs defined for the current block by
145the stream itself.
146
147The set of builtin abbrev IDs is:
148
149* 0 - `END_BLOCK`_ --- This abbrev ID marks the end of the current block.
150
151* 1 - `ENTER_SUBBLOCK`_ --- This abbrev ID marks the beginning of a new
152  block.
153
154* 2 - `DEFINE_ABBREV`_ --- This defines a new abbreviation.
155
156* 3 - `UNABBREV_RECORD`_ --- This ID specifies the definition of an
157  unabbreviated record.
158
159Abbreviation IDs 4 and above are defined by the stream itself, and specify an
160`abbreviated record encoding`_.
161
162.. _Blocks:
163
164Blocks
165------
166
167Blocks in a bitstream denote nested regions of the stream, and are identified by
168a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
169function bodies).  Block IDs 0-7 are reserved for `standard blocks`_ whose
170meaning is defined by Bitcode; block IDs 8 and greater are application
171specific. Nested blocks capture the hierarchical structure of the data encoded
172in it, and various properties are associated with blocks as the file is parsed.
173Block definitions allow the reader to efficiently skip blocks in constant time
174if the reader wants a summary of blocks, or if it wants to efficiently skip data
175it does not understand.  The LLVM IR reader uses this mechanism to skip function
176bodies, lazily reading them on demand.
177
178When reading and encoding the stream, several properties are maintained for the
179block.  In particular, each block maintains:
180
181#. A current abbrev id width.  This value starts at 2 at the beginning of the
182   stream, and is set every time a block record is entered.  The block entry
183   specifies the abbrev id width for the body of the block.
184
185#. A set of abbreviations.  Abbreviations may be defined within a block, in
186   which case they are only defined in that block (neither subblocks nor
187   enclosing blocks see the abbreviation).  Abbreviations can also be defined
188   inside a `BLOCKINFO`_ block, in which case they are defined in all blocks
189   that match the ID that the ``BLOCKINFO`` block is describing.
190
191As sub blocks are entered, these properties are saved and the new sub-block has
192its own set of abbreviations, and its own abbrev id width.  When a sub-block is
193popped, the saved values are restored.
194
195.. _ENTER_SUBBLOCK:
196
197ENTER_SUBBLOCK Encoding
198^^^^^^^^^^^^^^^^^^^^^^^
199
200:raw-html:`<tt>`
201[ENTER_SUBBLOCK, blockid\ :sub:`vbr8`, newabbrevlen\ :sub:`vbr4`, <align32bits>, blocklen_32]
202:raw-html:`</tt>`
203
204The ``ENTER_SUBBLOCK`` abbreviation ID specifies the start of a new block
205record.  The ``blockid`` value is encoded as an 8-bit VBR identifier, and
206indicates the type of block being entered, which can be a `standard block`_ or
207an application-specific block.  The ``newabbrevlen`` value is a 4-bit VBR, which
208specifies the abbrev id width for the sub-block.  The ``blocklen`` value is a
20932-bit aligned value that specifies the size of the subblock in 32-bit
210words. This value allows the reader to skip over the entire block in one jump.
211
212.. _END_BLOCK:
213
214END_BLOCK Encoding
215^^^^^^^^^^^^^^^^^^
216
217``[END_BLOCK, <align32bits>]``
218
219The ``END_BLOCK`` abbreviation ID specifies the end of the current block record.
220Its end is aligned to 32-bits to ensure that the size of the block is an even
221multiple of 32-bits.
222
223.. _Data Records:
224
225Data Records
226------------
227
228Data records consist of a record code and a number of (up to) 64-bit integer
229values.  The interpretation of the code and values is application specific and
230may vary between different block types.  Records can be encoded either using an
231unabbrev record, or with an abbreviation.  In the LLVM IR format, for example,
232there is a record which encodes the target triple of a module.  The code is
233``MODULE_CODE_TRIPLE``, and the values of the record are the ASCII codes for the
234characters in the string.
235
236.. _UNABBREV_RECORD:
237
238UNABBREV_RECORD Encoding
239^^^^^^^^^^^^^^^^^^^^^^^^
240
241:raw-html:`<tt>`
242[UNABBREV_RECORD, code\ :sub:`vbr6`, numops\ :sub:`vbr6`, op0\ :sub:`vbr6`, op1\ :sub:`vbr6`, ...]
243:raw-html:`</tt>`
244
245An ``UNABBREV_RECORD`` provides a default fallback encoding, which is both
246completely general and extremely inefficient.  It can describe an arbitrary
247record by emitting the code and operands as VBRs.
248
249For example, emitting an LLVM IR target triple as an unabbreviated record
250requires emitting the ``UNABBREV_RECORD`` abbrevid, a vbr6 for the
251``MODULE_CODE_TRIPLE`` code, a vbr6 for the length of the string, which is equal
252to the number of operands, and a vbr6 for each character.  Because there are no
253letters with values less than 32, each letter would need to be emitted as at
254least a two-part VBR, which means that each letter would require at least 12
255bits.  This is not an efficient encoding, but it is fully general.
256
257.. _abbreviated record encoding:
258
259Abbreviated Record Encoding
260^^^^^^^^^^^^^^^^^^^^^^^^^^^
261
262``[<abbrevid>, fields...]``
263
264An abbreviated record is a abbreviation id followed by a set of fields that are
265encoded according to the `abbreviation definition`_.  This allows records to be
266encoded significantly more densely than records encoded with the
267`UNABBREV_RECORD`_ type, and allows the abbreviation types to be specified in
268the stream itself, which allows the files to be completely self describing.  The
269actual encoding of abbreviations is defined below.
270
271The record code, which is the first field of an abbreviated record, may be
272encoded in the abbreviation definition (as a literal operand) or supplied in the
273abbreviated record (as a Fixed or VBR operand value).
274
275.. _abbreviation definition:
276
277Abbreviations
278-------------
279
280Abbreviations are an important form of compression for bitstreams.  The idea is
281to specify a dense encoding for a class of records once, then use that encoding
282to emit many records.  It takes space to emit the encoding into the file, but
283the space is recouped (hopefully plus some) when the records that use it are
284emitted.
285
286Abbreviations can be determined dynamically per client, per file. Because the
287abbreviations are stored in the bitstream itself, different streams of the same
288format can contain different sets of abbreviations according to the needs of the
289specific stream.  As a concrete example, LLVM IR files usually emit an
290abbreviation for binary operators.  If a specific LLVM module contained no or
291few binary operators, the abbreviation does not need to be emitted.
292
293.. _DEFINE_ABBREV:
294
295DEFINE_ABBREV Encoding
296^^^^^^^^^^^^^^^^^^^^^^
297
298:raw-html:`<tt>`
299[DEFINE_ABBREV, numabbrevops\ :sub:`vbr5`, abbrevop0, abbrevop1, ...]
300:raw-html:`</tt>`
301
302A ``DEFINE_ABBREV`` record adds an abbreviation to the list of currently defined
303abbreviations in the scope of this block.  This definition only exists inside
304this immediate block --- it is not visible in subblocks or enclosing blocks.
305Abbreviations are implicitly assigned IDs sequentially starting from 4 (the
306first application-defined abbreviation ID).  Any abbreviations defined in a
307``BLOCKINFO`` record for the particular block type receive IDs first, in order,
308followed by any abbreviations defined within the block itself.  Abbreviated data
309records reference this ID to indicate what abbreviation they are invoking.
310
311An abbreviation definition consists of the ``DEFINE_ABBREV`` abbrevid followed
312by a VBR that specifies the number of abbrev operands, then the abbrev operands
313themselves.  Abbreviation operands come in three forms.  They all start with a
314single bit that indicates whether the abbrev operand is a literal operand (when
315the bit is 1) or an encoding operand (when the bit is 0).
316
317#. Literal operands --- :raw-html:`<tt>` [1\ :sub:`1`, litvalue\
318   :sub:`vbr8`] :raw-html:`</tt>` --- Literal operands specify that the value in
319   the result is always a single specific value.  This specific value is emitted
320   as a vbr8 after the bit indicating that it is a literal operand.
321
322#. Encoding info without data --- :raw-html:`<tt>` [0\ :sub:`1`, encoding\
323   :sub:`3`] :raw-html:`</tt>` --- Operand encodings that do not have extra data
324   are just emitted as their code.
325
326#. Encoding info with data --- :raw-html:`<tt>` [0\ :sub:`1`, encoding\
327   :sub:`3`, value\ :sub:`vbr5`] :raw-html:`</tt>` --- Operand encodings that do
328   have extra data are emitted as their code, followed by the extra data.
329
330The possible operand encodings are:
331
332* Fixed (code 1): The field should be emitted as a `fixed-width value`_, whose
333  width is specified by the operand's extra data.
334
335* VBR (code 2): The field should be emitted as a `variable-width value`_, whose
336  width is specified by the operand's extra data.
337
338* Array (code 3): This field is an array of values.  The array operand has no
339  extra data, but expects another operand to follow it, indicating the element
340  type of the array.  When reading an array in an abbreviated record, the first
341  integer is a vbr6 that indicates the array length, followed by the encoded
342  elements of the array.  An array may only occur as the last operand of an
343  abbreviation (except for the one final operand that gives the array's
344  type).
345
346* Char6 (code 4): This field should be emitted as a `char6-encoded value`_.
347  This operand type takes no extra data. Char6 encoding is normally used as an
348  array element type.
349
350* Blob (code 5): This field is emitted as a vbr6, followed by padding to a
351  32-bit boundary (for alignment) and an array of 8-bit objects.  The array of
352  bytes is further followed by tail padding to ensure that its total length is a
353  multiple of 4 bytes.  This makes it very efficient for the reader to decode
354  the data without having to make a copy of it: it can use a pointer to the data
355  in the mapped in file and poke directly at it.  A blob may only occur as the
356  last operand of an abbreviation.
357
358For example, target triples in LLVM modules are encoded as a record of the form
359``[TRIPLE, 'a', 'b', 'c', 'd']``.  Consider if the bitstream emitted the
360following abbrev entry:
361
362::
363
364  [0, Fixed, 4]
365  [0, Array]
366  [0, Char6]
367
368When emitting a record with this abbreviation, the above entry would be emitted
369as:
370
371:raw-html:`<tt><blockquote>`
372[4\ :sub:`abbrevwidth`, 2\ :sub:`4`, 4\ :sub:`vbr6`, 0\ :sub:`6`, 1\ :sub:`6`, 2\ :sub:`6`, 3\ :sub:`6`]
373:raw-html:`</blockquote></tt>`
374
375These values are:
376
377#. The first value, 4, is the abbreviation ID for this abbreviation.
378
379#. The second value, 2, is the record code for ``TRIPLE`` records within LLVM IR
380   file ``MODULE_BLOCK`` blocks.
381
382#. The third value, 4, is the length of the array.
383
384#. The rest of the values are the char6 encoded values for ``"abcd"``.
385
386With this abbreviation, the triple is emitted with only 37 bits (assuming a
387abbrev id width of 3).  Without the abbreviation, significantly more space would
388be required to emit the target triple.  Also, because the ``TRIPLE`` value is
389not emitted as a literal in the abbreviation, the abbreviation can also be used
390for any other string value.
391
392.. _standard blocks:
393.. _standard block:
394
395Standard Blocks
396---------------
397
398In addition to the basic block structure and record encodings, the bitstream
399also defines specific built-in block types.  These block types specify how the
400stream is to be decoded or other metadata.  In the future, new standard blocks
401may be added.  Block IDs 0-7 are reserved for standard blocks.
402
403.. _BLOCKINFO:
404
405#0 - BLOCKINFO Block
406^^^^^^^^^^^^^^^^^^^^
407
408The ``BLOCKINFO`` block allows the description of metadata for other blocks.
409The currently specified records are:
410
411::
412
413  [SETBID (#1), blockid]
414  [DEFINE_ABBREV, ...]
415  [BLOCKNAME, ...name...]
416  [SETRECORDNAME, RecordID, ...name...]
417
418The ``SETBID`` record (code 1) indicates which block ID is being described.
419``SETBID`` records can occur multiple times throughout the block to change which
420block ID is being described.  There must be a ``SETBID`` record prior to any
421other records.
422
423Standard ``DEFINE_ABBREV`` records can occur inside ``BLOCKINFO`` blocks, but
424unlike their occurrence in normal blocks, the abbreviation is defined for blocks
425matching the block ID we are describing, *not* the ``BLOCKINFO`` block
426itself.  The abbreviations defined in ``BLOCKINFO`` blocks receive abbreviation
427IDs as described in `DEFINE_ABBREV`_.
428
429The ``BLOCKNAME`` record (code 2) can optionally occur in this block.  The
430elements of the record are the bytes of the string name of the block.
431llvm-bcanalyzer can use this to dump out bitcode files symbolically.
432
433The ``SETRECORDNAME`` record (code 3) can also optionally occur in this block.
434The first operand value is a record ID number, and the rest of the elements of
435the record are the bytes for the string name of the record.  llvm-bcanalyzer can
436use this to dump out bitcode files symbolically.
437
438Note that although the data in ``BLOCKINFO`` blocks is described as "metadata,"
439the abbreviations they contain are essential for parsing records from the
440corresponding blocks.  It is not safe to skip them.
441
442.. _wrapper:
443
444Bitcode Wrapper Format
445======================
446
447Bitcode files for LLVM IR may optionally be wrapped in a simple wrapper
448structure.  This structure contains a simple header that indicates the offset
449and size of the embedded BC file.  This allows additional information to be
450stored alongside the BC file.  The structure of this file header is:
451
452:raw-html:`<tt><blockquote>`
453[Magic\ :sub:`32`, Version\ :sub:`32`, Offset\ :sub:`32`, Size\ :sub:`32`, CPUType\ :sub:`32`]
454:raw-html:`</blockquote></tt>`
455
456Each of the fields are 32-bit fields stored in little endian form (as with the
457rest of the bitcode file fields).  The Magic number is always ``0x0B17C0DE`` and
458the version is currently always ``0``.  The Offset field is the offset in bytes
459to the start of the bitcode stream in the file, and the Size field is the size
460in bytes of the stream. CPUType is a target-specific value that can be used to
461encode the CPU of the target.
462
463.. _encoding of LLVM IR:
464
465LLVM IR Encoding
466================
467
468LLVM IR is encoded into a bitstream by defining blocks and records.  It uses
469blocks for things like constant pools, functions, symbol tables, etc.  It uses
470records for things like instructions, global variable descriptors, type
471descriptions, etc.  This document does not describe the set of abbreviations
472that the writer uses, as these are fully self-described in the file, and the
473reader is not allowed to build in any knowledge of this.
474
475Basics
476------
477
478LLVM IR Magic Number
479^^^^^^^^^^^^^^^^^^^^
480
481The magic number for LLVM IR files is:
482
483:raw-html:`<tt><blockquote>`
484[0x0\ :sub:`4`, 0xC\ :sub:`4`, 0xE\ :sub:`4`, 0xD\ :sub:`4`]
485:raw-html:`</blockquote></tt>`
486
487When combined with the bitcode magic number and viewed as bytes, this is
488``"BC 0xC0DE"``.
489
490.. _Signed VBRs:
491
492Signed VBRs
493^^^^^^^^^^^
494
495`Variable Width Integer`_ encoding is an efficient way to encode arbitrary sized
496unsigned values, but is an extremely inefficient for encoding signed values, as
497signed values are otherwise treated as maximally large unsigned values.
498
499As such, signed VBR values of a specific width are emitted as follows:
500
501* Positive values are emitted as VBRs of the specified width, but with their
502  value shifted left by one.
503
504* Negative values are emitted as VBRs of the specified width, but the negated
505  value is shifted left by one, and the low bit is set.
506
507With this encoding, small positive and small negative values can both be emitted
508efficiently. Signed VBR encoding is used in ``CST_CODE_INTEGER`` and
509``CST_CODE_WIDE_INTEGER`` records within ``CONSTANTS_BLOCK`` blocks.
510It is also used for phi instruction operands in `MODULE_CODE_VERSION`_ 1.
511
512LLVM IR Blocks
513^^^^^^^^^^^^^^
514
515LLVM IR is defined with the following blocks:
516
517* 8 --- `MODULE_BLOCK`_ --- This is the top-level block that contains the entire
518  module, and describes a variety of per-module information.
519
520* 9 --- `PARAMATTR_BLOCK`_ --- This enumerates the parameter attributes.
521
522* 10 --- `TYPE_BLOCK`_ --- This describes all of the types in the module.
523
524* 11 --- `CONSTANTS_BLOCK`_ --- This describes constants for a module or
525  function.
526
527* 12 --- `FUNCTION_BLOCK`_ --- This describes a function body.
528
529* 13 --- `TYPE_SYMTAB_BLOCK`_ --- This describes the type symbol table.
530
531* 14 --- `VALUE_SYMTAB_BLOCK`_ --- This describes a value symbol table.
532
533* 15 --- `METADATA_BLOCK`_ --- This describes metadata items.
534
535* 16 --- `METADATA_ATTACHMENT`_ --- This contains records associating metadata
536  with function instruction values.
537
538.. _MODULE_BLOCK:
539
540MODULE_BLOCK Contents
541---------------------
542
543The ``MODULE_BLOCK`` block (id 8) is the top-level block for LLVM bitcode files,
544and each bitcode file must contain exactly one. In addition to records
545(described below) containing information about the module, a ``MODULE_BLOCK``
546block may contain the following sub-blocks:
547
548* `BLOCKINFO`_
549* `PARAMATTR_BLOCK`_
550* `TYPE_BLOCK`_
551* `TYPE_SYMTAB_BLOCK`_
552* `VALUE_SYMTAB_BLOCK`_
553* `CONSTANTS_BLOCK`_
554* `FUNCTION_BLOCK`_
555* `METADATA_BLOCK`_
556
557.. _MODULE_CODE_VERSION:
558
559MODULE_CODE_VERSION Record
560^^^^^^^^^^^^^^^^^^^^^^^^^^
561
562``[VERSION, version#]``
563
564The ``VERSION`` record (code 1) contains a single value indicating the format
565version. Versions 0 and 1 are supported at this time. The difference between
566version 0 and 1 is in the encoding of instruction operands in
567each `FUNCTION_BLOCK`_.
568
569In version 0, each value defined by an instruction is assigned an ID
570unique to the function. Function-level value IDs are assigned starting from
571``NumModuleValues`` since they share the same namespace as module-level
572values. The value enumerator resets after each function. When a value is
573an operand of an instruction, the value ID is used to represent the operand.
574For large functions or large modules, these operand values can be large.
575
576The encoding in version 1 attempts to avoid large operand values
577in common cases. Instead of using the value ID directly, operands are
578encoded as relative to the current instruction. Thus, if an operand
579is the value defined by the previous instruction, the operand
580will be encoded as 1.
581
582For example, instead of
583
584.. code-block:: llvm
585
586  #n = load #n-1
587  #n+1 = icmp eq #n, #const0
588  br #n+1, label #(bb1), label #(bb2)
589
590version 1 will encode the instructions as
591
592.. code-block:: llvm
593
594  #n = load #1
595  #n+1 = icmp eq #1, (#n+1)-#const0
596  br #1, label #(bb1), label #(bb2)
597
598Note in the example that operands which are constants also use
599the relative encoding, while operands like basic block labels
600do not use the relative encoding.
601
602Forward references will result in a negative value.
603This can be inefficient, as operands are normally encoded
604as unsigned VBRs. However, forward references are rare, except in the
605case of phi instructions. For phi instructions, operands are encoded as
606`Signed VBRs`_ to deal with forward references.
607
608
609MODULE_CODE_TRIPLE Record
610^^^^^^^^^^^^^^^^^^^^^^^^^
611
612``[TRIPLE, ...string...]``
613
614The ``TRIPLE`` record (code 2) contains a variable number of values representing
615the bytes of the ``target triple`` specification string.
616
617MODULE_CODE_DATALAYOUT Record
618^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
619
620``[DATALAYOUT, ...string...]``
621
622The ``DATALAYOUT`` record (code 3) contains a variable number of values
623representing the bytes of the ``target datalayout`` specification string.
624
625MODULE_CODE_ASM Record
626^^^^^^^^^^^^^^^^^^^^^^
627
628``[ASM, ...string...]``
629
630The ``ASM`` record (code 4) contains a variable number of values representing
631the bytes of ``module asm`` strings, with individual assembly blocks separated
632by newline (ASCII 10) characters.
633
634.. _MODULE_CODE_SECTIONNAME:
635
636MODULE_CODE_SECTIONNAME Record
637^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
638
639``[SECTIONNAME, ...string...]``
640
641The ``SECTIONNAME`` record (code 5) contains a variable number of values
642representing the bytes of a single section name string. There should be one
643``SECTIONNAME`` record for each section name referenced (e.g., in global
644variable or function ``section`` attributes) within the module. These records
645can be referenced by the 1-based index in the *section* fields of ``GLOBALVAR``
646or ``FUNCTION`` records.
647
648MODULE_CODE_DEPLIB Record
649^^^^^^^^^^^^^^^^^^^^^^^^^
650
651``[DEPLIB, ...string...]``
652
653The ``DEPLIB`` record (code 6) contains a variable number of values representing
654the bytes of a single dependent library name string, one of the libraries
655mentioned in a ``deplibs`` declaration.  There should be one ``DEPLIB`` record
656for each library name referenced.
657
658MODULE_CODE_GLOBALVAR Record
659^^^^^^^^^^^^^^^^^^^^^^^^^^^^
660
661``[GLOBALVAR, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal, unnamed_addr]``
662
663The ``GLOBALVAR`` record (code 7) marks the declaration or definition of a
664global variable. The operand fields are:
665
666* *pointer type*: The type index of the pointer type used to point to this
667  global variable
668
669* *isconst*: Non-zero if the variable is treated as constant within the module,
670  or zero if it is not
671
672* *initid*: If non-zero, the value index of the initializer for this variable,
673  plus 1.
674
675.. _linkage type:
676
677* *linkage*: An encoding of the linkage type for this variable:
678  * ``external``: code 0
679  * ``weak``: code 1
680  * ``appending``: code 2
681  * ``internal``: code 3
682  * ``linkonce``: code 4
683  * ``dllimport``: code 5
684  * ``dllexport``: code 6
685  * ``extern_weak``: code 7
686  * ``common``: code 8
687  * ``private``: code 9
688  * ``weak_odr``: code 10
689  * ``linkonce_odr``: code 11
690  * ``available_externally``: code 12
691  * ``linker_private``: code 13
692
693* alignment*: The logarithm base 2 of the variable's requested alignment, plus 1
694
695* *section*: If non-zero, the 1-based section index in the table of
696  `MODULE_CODE_SECTIONNAME`_ entries.
697
698.. _visibility:
699
700* *visibility*: If present, an encoding of the visibility of this variable:
701  * ``default``: code 0
702  * ``hidden``: code 1
703  * ``protected``: code 2
704
705* *threadlocal*: If present, an encoding of the thread local storage mode of the
706  variable:
707  * ``not thread local``: code 0
708  * ``thread local; default TLS model``: code 1
709  * ``localdynamic``: code 2
710  * ``initialexec``: code 3
711  * ``localexec``: code 4
712
713* *unnamed_addr*: If present and non-zero, indicates that the variable has
714  ``unnamed_addr``
715
716.. _FUNCTION:
717
718MODULE_CODE_FUNCTION Record
719^^^^^^^^^^^^^^^^^^^^^^^^^^^
720
721``[FUNCTION, type, callingconv, isproto, linkage, paramattr, alignment, section, visibility, gc]``
722
723The ``FUNCTION`` record (code 8) marks the declaration or definition of a
724function. The operand fields are:
725
726* *type*: The type index of the function type describing this function
727
728* *callingconv*: The calling convention number:
729  * ``ccc``: code 0
730  * ``fastcc``: code 8
731  * ``coldcc``: code 9
732  * ``x86_stdcallcc``: code 64
733  * ``x86_fastcallcc``: code 65
734  * ``arm_apcscc``: code 66
735  * ``arm_aapcscc``: code 67
736  * ``arm_aapcs_vfpcc``: code 68
737
738* isproto*: Non-zero if this entry represents a declaration rather than a
739  definition
740
741* *linkage*: An encoding of the `linkage type`_ for this function
742
743* *paramattr*: If nonzero, the 1-based parameter attribute index into the table
744  of `PARAMATTR_CODE_ENTRY`_ entries.
745
746* *alignment*: The logarithm base 2 of the function's requested alignment, plus
747  1
748
749* *section*: If non-zero, the 1-based section index in the table of
750  `MODULE_CODE_SECTIONNAME`_ entries.
751
752* *visibility*: An encoding of the `visibility`_ of this function
753
754* *gc*: If present and nonzero, the 1-based garbage collector index in the table
755  of `MODULE_CODE_GCNAME`_ entries.
756
757* *unnamed_addr*: If present and non-zero, indicates that the function has
758  ``unnamed_addr``
759
760MODULE_CODE_ALIAS Record
761^^^^^^^^^^^^^^^^^^^^^^^^
762
763``[ALIAS, alias type, aliasee val#, linkage, visibility]``
764
765The ``ALIAS`` record (code 9) marks the definition of an alias. The operand
766fields are
767
768* *alias type*: The type index of the alias
769
770* *aliasee val#*: The value index of the aliased value
771
772* *linkage*: An encoding of the `linkage type`_ for this alias
773
774* *visibility*: If present, an encoding of the `visibility`_ of the alias
775
776MODULE_CODE_PURGEVALS Record
777^^^^^^^^^^^^^^^^^^^^^^^^^^^^
778
779``[PURGEVALS, numvals]``
780
781The ``PURGEVALS`` record (code 10) resets the module-level value list to the
782size given by the single operand value. Module-level value list items are added
783by ``GLOBALVAR``, ``FUNCTION``, and ``ALIAS`` records.  After a ``PURGEVALS``
784record is seen, new value indices will start from the given *numvals* value.
785
786.. _MODULE_CODE_GCNAME:
787
788MODULE_CODE_GCNAME Record
789^^^^^^^^^^^^^^^^^^^^^^^^^
790
791``[GCNAME, ...string...]``
792
793The ``GCNAME`` record (code 11) contains a variable number of values
794representing the bytes of a single garbage collector name string. There should
795be one ``GCNAME`` record for each garbage collector name referenced in function
796``gc`` attributes within the module. These records can be referenced by 1-based
797index in the *gc* fields of ``FUNCTION`` records.
798
799.. _PARAMATTR_BLOCK:
800
801PARAMATTR_BLOCK Contents
802------------------------
803
804The ``PARAMATTR_BLOCK`` block (id 9) contains a table of entries describing the
805attributes of function parameters. These entries are referenced by 1-based index
806in the *paramattr* field of module block `FUNCTION`_ records, or within the
807*attr* field of function block ``INST_INVOKE`` and ``INST_CALL`` records.
808
809Entries within ``PARAMATTR_BLOCK`` are constructed to ensure that each is unique
810(i.e., no two indicies represent equivalent attribute lists).
811
812.. _PARAMATTR_CODE_ENTRY:
813
814PARAMATTR_CODE_ENTRY Record
815^^^^^^^^^^^^^^^^^^^^^^^^^^^
816
817``[ENTRY, paramidx0, attr0, paramidx1, attr1...]``
818
819The ``ENTRY`` record (code 1) contains an even number of values describing a
820unique set of function parameter attributes. Each *paramidx* value indicates
821which set of attributes is represented, with 0 representing the return value
822attributes, 0xFFFFFFFF representing function attributes, and other values
823representing 1-based function parameters. Each *attr* value is a bitmap with the
824following interpretation:
825
826* bit 0: ``zeroext``
827* bit 1: ``signext``
828* bit 2: ``noreturn``
829* bit 3: ``inreg``
830* bit 4: ``sret``
831* bit 5: ``nounwind``
832* bit 6: ``noalias``
833* bit 7: ``byval``
834* bit 8: ``nest``
835* bit 9: ``readnone``
836* bit 10: ``readonly``
837* bit 11: ``noinline``
838* bit 12: ``alwaysinline``
839* bit 13: ``optsize``
840* bit 14: ``ssp``
841* bit 15: ``sspreq``
842* bits 16-31: ``align n``
843* bit 32: ``nocapture``
844* bit 33: ``noredzone``
845* bit 34: ``noimplicitfloat``
846* bit 35: ``naked``
847* bit 36: ``inlinehint``
848* bits 37-39: ``alignstack n``, represented as the logarithm
849  base 2 of the requested alignment, plus 1
850
851.. _TYPE_BLOCK:
852
853TYPE_BLOCK Contents
854-------------------
855
856The ``TYPE_BLOCK`` block (id 10) contains records which constitute a table of
857type operator entries used to represent types referenced within an LLVM
858module. Each record (with the exception of `NUMENTRY`_) generates a single type
859table entry, which may be referenced by 0-based index from instructions,
860constants, metadata, type symbol table entries, or other type operator records.
861
862Entries within ``TYPE_BLOCK`` are constructed to ensure that each entry is
863unique (i.e., no two indicies represent structurally equivalent types).
864
865.. _TYPE_CODE_NUMENTRY:
866.. _NUMENTRY:
867
868TYPE_CODE_NUMENTRY Record
869^^^^^^^^^^^^^^^^^^^^^^^^^
870
871``[NUMENTRY, numentries]``
872
873The ``NUMENTRY`` record (code 1) contains a single value which indicates the
874total number of type code entries in the type table of the module. If present,
875``NUMENTRY`` should be the first record in the block.
876
877TYPE_CODE_VOID Record
878^^^^^^^^^^^^^^^^^^^^^
879
880``[VOID]``
881
882The ``VOID`` record (code 2) adds a ``void`` type to the type table.
883
884TYPE_CODE_HALF Record
885^^^^^^^^^^^^^^^^^^^^^
886
887``[HALF]``
888
889The ``HALF`` record (code 10) adds a ``half`` (16-bit floating point) type to
890the type table.
891
892TYPE_CODE_FLOAT Record
893^^^^^^^^^^^^^^^^^^^^^^
894
895``[FLOAT]``
896
897The ``FLOAT`` record (code 3) adds a ``float`` (32-bit floating point) type to
898the type table.
899
900TYPE_CODE_DOUBLE Record
901^^^^^^^^^^^^^^^^^^^^^^^
902
903``[DOUBLE]``
904
905The ``DOUBLE`` record (code 4) adds a ``double`` (64-bit floating point) type to
906the type table.
907
908TYPE_CODE_LABEL Record
909^^^^^^^^^^^^^^^^^^^^^^
910
911``[LABEL]``
912
913The ``LABEL`` record (code 5) adds a ``label`` type to the type table.
914
915TYPE_CODE_OPAQUE Record
916^^^^^^^^^^^^^^^^^^^^^^^
917
918``[OPAQUE]``
919
920The ``OPAQUE`` record (code 6) adds an ``opaque`` type to the type table. Note
921that distinct ``opaque`` types are not unified.
922
923TYPE_CODE_INTEGER Record
924^^^^^^^^^^^^^^^^^^^^^^^^
925
926``[INTEGER, width]``
927
928The ``INTEGER`` record (code 7) adds an integer type to the type table. The
929single *width* field indicates the width of the integer type.
930
931TYPE_CODE_POINTER Record
932^^^^^^^^^^^^^^^^^^^^^^^^
933
934``[POINTER, pointee type, address space]``
935
936The ``POINTER`` record (code 8) adds a pointer type to the type table. The
937operand fields are
938
939* *pointee type*: The type index of the pointed-to type
940
941* *address space*: If supplied, the target-specific numbered address space where
942  the pointed-to object resides. Otherwise, the default address space is zero.
943
944TYPE_CODE_FUNCTION Record
945^^^^^^^^^^^^^^^^^^^^^^^^^
946
947``[FUNCTION, vararg, ignored, retty, ...paramty... ]``
948
949The ``FUNCTION`` record (code 9) adds a function type to the type table. The
950operand fields are
951
952* *vararg*: Non-zero if the type represents a varargs function
953
954* *ignored*: This value field is present for backward compatibility only, and is
955  ignored
956
957* *retty*: The type index of the function's return type
958
959* *paramty*: Zero or more type indices representing the parameter types of the
960  function
961
962TYPE_CODE_STRUCT Record
963^^^^^^^^^^^^^^^^^^^^^^^
964
965``[STRUCT, ispacked, ...eltty...]``
966
967The ``STRUCT`` record (code 10) adds a struct type to the type table. The
968operand fields are
969
970* *ispacked*: Non-zero if the type represents a packed structure
971
972* *eltty*: Zero or more type indices representing the element types of the
973  structure
974
975TYPE_CODE_ARRAY Record
976^^^^^^^^^^^^^^^^^^^^^^
977
978``[ARRAY, numelts, eltty]``
979
980The ``ARRAY`` record (code 11) adds an array type to the type table.  The
981operand fields are
982
983* *numelts*: The number of elements in arrays of this type
984
985* *eltty*: The type index of the array element type
986
987TYPE_CODE_VECTOR Record
988^^^^^^^^^^^^^^^^^^^^^^^
989
990``[VECTOR, numelts, eltty]``
991
992The ``VECTOR`` record (code 12) adds a vector type to the type table.  The
993operand fields are
994
995* *numelts*: The number of elements in vectors of this type
996
997* *eltty*: The type index of the vector element type
998
999TYPE_CODE_X86_FP80 Record
1000^^^^^^^^^^^^^^^^^^^^^^^^^
1001
1002``[X86_FP80]``
1003
1004The ``X86_FP80`` record (code 13) adds an ``x86_fp80`` (80-bit floating point)
1005type to the type table.
1006
1007TYPE_CODE_FP128 Record
1008^^^^^^^^^^^^^^^^^^^^^^
1009
1010``[FP128]``
1011
1012The ``FP128`` record (code 14) adds an ``fp128`` (128-bit floating point) type
1013to the type table.
1014
1015TYPE_CODE_PPC_FP128 Record
1016^^^^^^^^^^^^^^^^^^^^^^^^^^
1017
1018``[PPC_FP128]``
1019
1020The ``PPC_FP128`` record (code 15) adds a ``ppc_fp128`` (128-bit floating point)
1021type to the type table.
1022
1023TYPE_CODE_METADATA Record
1024^^^^^^^^^^^^^^^^^^^^^^^^^
1025
1026``[METADATA]``
1027
1028The ``METADATA`` record (code 16) adds a ``metadata`` type to the type table.
1029
1030.. _CONSTANTS_BLOCK:
1031
1032CONSTANTS_BLOCK Contents
1033------------------------
1034
1035The ``CONSTANTS_BLOCK`` block (id 11) ...
1036
1037.. _FUNCTION_BLOCK:
1038
1039FUNCTION_BLOCK Contents
1040-----------------------
1041
1042The ``FUNCTION_BLOCK`` block (id 12) ...
1043
1044In addition to the record types described below, a ``FUNCTION_BLOCK`` block may
1045contain the following sub-blocks:
1046
1047* `CONSTANTS_BLOCK`_
1048* `VALUE_SYMTAB_BLOCK`_
1049* `METADATA_ATTACHMENT`_
1050
1051.. _TYPE_SYMTAB_BLOCK:
1052
1053TYPE_SYMTAB_BLOCK Contents
1054--------------------------
1055
1056The ``TYPE_SYMTAB_BLOCK`` block (id 13) contains entries which map between
1057module-level named types and their corresponding type indices.
1058
1059.. _TST_CODE_ENTRY:
1060
1061TST_CODE_ENTRY Record
1062^^^^^^^^^^^^^^^^^^^^^
1063
1064``[ENTRY, typeid, ...string...]``
1065
1066The ``ENTRY`` record (code 1) contains a variable number of values, with the
1067first giving the type index of the designated type, and the remaining values
1068giving the character codes of the type name. Each entry corresponds to a single
1069named type.
1070
1071.. _VALUE_SYMTAB_BLOCK:
1072
1073VALUE_SYMTAB_BLOCK Contents
1074---------------------------
1075
1076The ``VALUE_SYMTAB_BLOCK`` block (id 14) ... 
1077
1078.. _METADATA_BLOCK:
1079
1080METADATA_BLOCK Contents
1081-----------------------
1082
1083The ``METADATA_BLOCK`` block (id 15) ...
1084
1085.. _METADATA_ATTACHMENT:
1086
1087METADATA_ATTACHMENT Contents
1088----------------------------
1089
1090The ``METADATA_ATTACHMENT`` block (id 16) ...
1091