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, dllstorageclass]`` 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 * deprecated : code 13 692 * deprecated : code 14 693 694* alignment*: The logarithm base 2 of the variable's requested alignment, plus 1 695 696* *section*: If non-zero, the 1-based section index in the table of 697 `MODULE_CODE_SECTIONNAME`_ entries. 698 699.. _visibility: 700 701* *visibility*: If present, an encoding of the visibility of this variable: 702 * ``default``: code 0 703 * ``hidden``: code 1 704 * ``protected``: code 2 705 706* *threadlocal*: If present, an encoding of the thread local storage mode of the 707 variable: 708 * ``not thread local``: code 0 709 * ``thread local; default TLS model``: code 1 710 * ``localdynamic``: code 2 711 * ``initialexec``: code 3 712 * ``localexec``: code 4 713 714* *unnamed_addr*: If present and non-zero, indicates that the variable has 715 ``unnamed_addr`` 716 717.. _dllstorageclass: 718 719* *dllstorageclass*: If present, an encoding of the DLL storage class of this variable: 720 721 * ``default``: code 0 722 * ``dllimport``: code 1 723 * ``dllexport``: code 2 724 725.. _FUNCTION: 726 727MODULE_CODE_FUNCTION Record 728^^^^^^^^^^^^^^^^^^^^^^^^^^^ 729 730``[FUNCTION, type, callingconv, isproto, linkage, paramattr, alignment, section, visibility, gc, prefix, dllstorageclass]`` 731 732The ``FUNCTION`` record (code 8) marks the declaration or definition of a 733function. The operand fields are: 734 735* *type*: The type index of the function type describing this function 736 737* *callingconv*: The calling convention number: 738 * ``ccc``: code 0 739 * ``fastcc``: code 8 740 * ``coldcc``: code 9 741 * ``webkit_jscc``: code 12 742 * ``anyregcc``: code 13 743 * ``preserve_mostcc``: code 14 744 * ``preserve_allcc``: code 15 745 * ``x86_stdcallcc``: code 64 746 * ``x86_fastcallcc``: code 65 747 * ``arm_apcscc``: code 66 748 * ``arm_aapcscc``: code 67 749 * ``arm_aapcs_vfpcc``: code 68 750 751* isproto*: Non-zero if this entry represents a declaration rather than a 752 definition 753 754* *linkage*: An encoding of the `linkage type`_ for this function 755 756* *paramattr*: If nonzero, the 1-based parameter attribute index into the table 757 of `PARAMATTR_CODE_ENTRY`_ entries. 758 759* *alignment*: The logarithm base 2 of the function's requested alignment, plus 760 1 761 762* *section*: If non-zero, the 1-based section index in the table of 763 `MODULE_CODE_SECTIONNAME`_ entries. 764 765* *visibility*: An encoding of the `visibility`_ of this function 766 767* *gc*: If present and nonzero, the 1-based garbage collector index in the table 768 of `MODULE_CODE_GCNAME`_ entries. 769 770* *unnamed_addr*: If present and non-zero, indicates that the function has 771 ``unnamed_addr`` 772 773* *prefix*: If non-zero, the value index of the prefix data for this function, 774 plus 1. 775 776* *dllstorageclass*: An encoding of the `dllstorageclass`_ of this function 777 778MODULE_CODE_ALIAS Record 779^^^^^^^^^^^^^^^^^^^^^^^^ 780 781``[ALIAS, alias type, aliasee val#, linkage, visibility, dllstorageclass]`` 782 783The ``ALIAS`` record (code 9) marks the definition of an alias. The operand 784fields are 785 786* *alias type*: The type index of the alias 787 788* *aliasee val#*: The value index of the aliased value 789 790* *linkage*: An encoding of the `linkage type`_ for this alias 791 792* *visibility*: If present, an encoding of the `visibility`_ of the alias 793 794* *dllstorageclass*: If present, an encoding of the `dllstorageclass`_ of the alias 795 796MODULE_CODE_PURGEVALS Record 797^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 798 799``[PURGEVALS, numvals]`` 800 801The ``PURGEVALS`` record (code 10) resets the module-level value list to the 802size given by the single operand value. Module-level value list items are added 803by ``GLOBALVAR``, ``FUNCTION``, and ``ALIAS`` records. After a ``PURGEVALS`` 804record is seen, new value indices will start from the given *numvals* value. 805 806.. _MODULE_CODE_GCNAME: 807 808MODULE_CODE_GCNAME Record 809^^^^^^^^^^^^^^^^^^^^^^^^^ 810 811``[GCNAME, ...string...]`` 812 813The ``GCNAME`` record (code 11) contains a variable number of values 814representing the bytes of a single garbage collector name string. There should 815be one ``GCNAME`` record for each garbage collector name referenced in function 816``gc`` attributes within the module. These records can be referenced by 1-based 817index in the *gc* fields of ``FUNCTION`` records. 818 819.. _PARAMATTR_BLOCK: 820 821PARAMATTR_BLOCK Contents 822------------------------ 823 824The ``PARAMATTR_BLOCK`` block (id 9) contains a table of entries describing the 825attributes of function parameters. These entries are referenced by 1-based index 826in the *paramattr* field of module block `FUNCTION`_ records, or within the 827*attr* field of function block ``INST_INVOKE`` and ``INST_CALL`` records. 828 829Entries within ``PARAMATTR_BLOCK`` are constructed to ensure that each is unique 830(i.e., no two indicies represent equivalent attribute lists). 831 832.. _PARAMATTR_CODE_ENTRY: 833 834PARAMATTR_CODE_ENTRY Record 835^^^^^^^^^^^^^^^^^^^^^^^^^^^ 836 837``[ENTRY, paramidx0, attr0, paramidx1, attr1...]`` 838 839The ``ENTRY`` record (code 1) contains an even number of values describing a 840unique set of function parameter attributes. Each *paramidx* value indicates 841which set of attributes is represented, with 0 representing the return value 842attributes, 0xFFFFFFFF representing function attributes, and other values 843representing 1-based function parameters. Each *attr* value is a bitmap with the 844following interpretation: 845 846* bit 0: ``zeroext`` 847* bit 1: ``signext`` 848* bit 2: ``noreturn`` 849* bit 3: ``inreg`` 850* bit 4: ``sret`` 851* bit 5: ``nounwind`` 852* bit 6: ``noalias`` 853* bit 7: ``byval`` 854* bit 8: ``nest`` 855* bit 9: ``readnone`` 856* bit 10: ``readonly`` 857* bit 11: ``noinline`` 858* bit 12: ``alwaysinline`` 859* bit 13: ``optsize`` 860* bit 14: ``ssp`` 861* bit 15: ``sspreq`` 862* bits 16-31: ``align n`` 863* bit 32: ``nocapture`` 864* bit 33: ``noredzone`` 865* bit 34: ``noimplicitfloat`` 866* bit 35: ``naked`` 867* bit 36: ``inlinehint`` 868* bits 37-39: ``alignstack n``, represented as the logarithm 869 base 2 of the requested alignment, plus 1 870 871.. _TYPE_BLOCK: 872 873TYPE_BLOCK Contents 874------------------- 875 876The ``TYPE_BLOCK`` block (id 10) contains records which constitute a table of 877type operator entries used to represent types referenced within an LLVM 878module. Each record (with the exception of `NUMENTRY`_) generates a single type 879table entry, which may be referenced by 0-based index from instructions, 880constants, metadata, type symbol table entries, or other type operator records. 881 882Entries within ``TYPE_BLOCK`` are constructed to ensure that each entry is 883unique (i.e., no two indicies represent structurally equivalent types). 884 885.. _TYPE_CODE_NUMENTRY: 886.. _NUMENTRY: 887 888TYPE_CODE_NUMENTRY Record 889^^^^^^^^^^^^^^^^^^^^^^^^^ 890 891``[NUMENTRY, numentries]`` 892 893The ``NUMENTRY`` record (code 1) contains a single value which indicates the 894total number of type code entries in the type table of the module. If present, 895``NUMENTRY`` should be the first record in the block. 896 897TYPE_CODE_VOID Record 898^^^^^^^^^^^^^^^^^^^^^ 899 900``[VOID]`` 901 902The ``VOID`` record (code 2) adds a ``void`` type to the type table. 903 904TYPE_CODE_HALF Record 905^^^^^^^^^^^^^^^^^^^^^ 906 907``[HALF]`` 908 909The ``HALF`` record (code 10) adds a ``half`` (16-bit floating point) type to 910the type table. 911 912TYPE_CODE_FLOAT Record 913^^^^^^^^^^^^^^^^^^^^^^ 914 915``[FLOAT]`` 916 917The ``FLOAT`` record (code 3) adds a ``float`` (32-bit floating point) type to 918the type table. 919 920TYPE_CODE_DOUBLE Record 921^^^^^^^^^^^^^^^^^^^^^^^ 922 923``[DOUBLE]`` 924 925The ``DOUBLE`` record (code 4) adds a ``double`` (64-bit floating point) type to 926the type table. 927 928TYPE_CODE_LABEL Record 929^^^^^^^^^^^^^^^^^^^^^^ 930 931``[LABEL]`` 932 933The ``LABEL`` record (code 5) adds a ``label`` type to the type table. 934 935TYPE_CODE_OPAQUE Record 936^^^^^^^^^^^^^^^^^^^^^^^ 937 938``[OPAQUE]`` 939 940The ``OPAQUE`` record (code 6) adds an ``opaque`` type to the type table. Note 941that distinct ``opaque`` types are not unified. 942 943TYPE_CODE_INTEGER Record 944^^^^^^^^^^^^^^^^^^^^^^^^ 945 946``[INTEGER, width]`` 947 948The ``INTEGER`` record (code 7) adds an integer type to the type table. The 949single *width* field indicates the width of the integer type. 950 951TYPE_CODE_POINTER Record 952^^^^^^^^^^^^^^^^^^^^^^^^ 953 954``[POINTER, pointee type, address space]`` 955 956The ``POINTER`` record (code 8) adds a pointer type to the type table. The 957operand fields are 958 959* *pointee type*: The type index of the pointed-to type 960 961* *address space*: If supplied, the target-specific numbered address space where 962 the pointed-to object resides. Otherwise, the default address space is zero. 963 964TYPE_CODE_FUNCTION Record 965^^^^^^^^^^^^^^^^^^^^^^^^^ 966 967``[FUNCTION, vararg, ignored, retty, ...paramty... ]`` 968 969The ``FUNCTION`` record (code 9) adds a function type to the type table. The 970operand fields are 971 972* *vararg*: Non-zero if the type represents a varargs function 973 974* *ignored*: This value field is present for backward compatibility only, and is 975 ignored 976 977* *retty*: The type index of the function's return type 978 979* *paramty*: Zero or more type indices representing the parameter types of the 980 function 981 982TYPE_CODE_STRUCT Record 983^^^^^^^^^^^^^^^^^^^^^^^ 984 985``[STRUCT, ispacked, ...eltty...]`` 986 987The ``STRUCT`` record (code 10) adds a struct type to the type table. The 988operand fields are 989 990* *ispacked*: Non-zero if the type represents a packed structure 991 992* *eltty*: Zero or more type indices representing the element types of the 993 structure 994 995TYPE_CODE_ARRAY Record 996^^^^^^^^^^^^^^^^^^^^^^ 997 998``[ARRAY, numelts, eltty]`` 999 1000The ``ARRAY`` record (code 11) adds an array type to the type table. The 1001operand fields are 1002 1003* *numelts*: The number of elements in arrays of this type 1004 1005* *eltty*: The type index of the array element type 1006 1007TYPE_CODE_VECTOR Record 1008^^^^^^^^^^^^^^^^^^^^^^^ 1009 1010``[VECTOR, numelts, eltty]`` 1011 1012The ``VECTOR`` record (code 12) adds a vector type to the type table. The 1013operand fields are 1014 1015* *numelts*: The number of elements in vectors of this type 1016 1017* *eltty*: The type index of the vector element type 1018 1019TYPE_CODE_X86_FP80 Record 1020^^^^^^^^^^^^^^^^^^^^^^^^^ 1021 1022``[X86_FP80]`` 1023 1024The ``X86_FP80`` record (code 13) adds an ``x86_fp80`` (80-bit floating point) 1025type to the type table. 1026 1027TYPE_CODE_FP128 Record 1028^^^^^^^^^^^^^^^^^^^^^^ 1029 1030``[FP128]`` 1031 1032The ``FP128`` record (code 14) adds an ``fp128`` (128-bit floating point) type 1033to the type table. 1034 1035TYPE_CODE_PPC_FP128 Record 1036^^^^^^^^^^^^^^^^^^^^^^^^^^ 1037 1038``[PPC_FP128]`` 1039 1040The ``PPC_FP128`` record (code 15) adds a ``ppc_fp128`` (128-bit floating point) 1041type to the type table. 1042 1043TYPE_CODE_METADATA Record 1044^^^^^^^^^^^^^^^^^^^^^^^^^ 1045 1046``[METADATA]`` 1047 1048The ``METADATA`` record (code 16) adds a ``metadata`` type to the type table. 1049 1050.. _CONSTANTS_BLOCK: 1051 1052CONSTANTS_BLOCK Contents 1053------------------------ 1054 1055The ``CONSTANTS_BLOCK`` block (id 11) ... 1056 1057.. _FUNCTION_BLOCK: 1058 1059FUNCTION_BLOCK Contents 1060----------------------- 1061 1062The ``FUNCTION_BLOCK`` block (id 12) ... 1063 1064In addition to the record types described below, a ``FUNCTION_BLOCK`` block may 1065contain the following sub-blocks: 1066 1067* `CONSTANTS_BLOCK`_ 1068* `VALUE_SYMTAB_BLOCK`_ 1069* `METADATA_ATTACHMENT`_ 1070 1071.. _TYPE_SYMTAB_BLOCK: 1072 1073TYPE_SYMTAB_BLOCK Contents 1074-------------------------- 1075 1076The ``TYPE_SYMTAB_BLOCK`` block (id 13) contains entries which map between 1077module-level named types and their corresponding type indices. 1078 1079.. _TST_CODE_ENTRY: 1080 1081TST_CODE_ENTRY Record 1082^^^^^^^^^^^^^^^^^^^^^ 1083 1084``[ENTRY, typeid, ...string...]`` 1085 1086The ``ENTRY`` record (code 1) contains a variable number of values, with the 1087first giving the type index of the designated type, and the remaining values 1088giving the character codes of the type name. Each entry corresponds to a single 1089named type. 1090 1091.. _VALUE_SYMTAB_BLOCK: 1092 1093VALUE_SYMTAB_BLOCK Contents 1094--------------------------- 1095 1096The ``VALUE_SYMTAB_BLOCK`` block (id 14) ... 1097 1098.. _METADATA_BLOCK: 1099 1100METADATA_BLOCK Contents 1101----------------------- 1102 1103The ``METADATA_BLOCK`` block (id 15) ... 1104 1105.. _METADATA_ATTACHMENT: 1106 1107METADATA_ATTACHMENT Contents 1108---------------------------- 1109 1110The ``METADATA_ATTACHMENT`` block (id 16) ... 1111