diff --git a/docs/BytecodeFormat.html b/docs/BytecodeFormat.html index 48b9f09e2a3..287efd13213 100644 --- a/docs/BytecodeFormat.html +++ b/docs/BytecodeFormat.html @@ -6,7 +6,7 @@ @@ -19,13 +19,15 @@
  • Blocks
  • Lists
  • Fields
  • +
  • Encoding Rules
  • Alignment
  • Detailed Layout
    1. Notation
    2. Blocks Types
    3. -
    4. Header Block
    5. +
    6. Signature Block
    7. +
    8. Module Block
    9. Global Type Pool
    10. Module Info Block
    11. Global Constant Pool
    12. @@ -63,7 +65,7 @@ without getting into bit and byte level specifics.

      LLVM bytecode files consist simply of a sequence of blocks of bytes. Each block begins with an identification value that determines the type of the next block. The possible types of blocks are described below in the section -Block Types. The block identifier is used because +Block Types. The block identifier is used because it is possible for entire blocks to be omitted from the file if they are empty. The block identifier helps the reader determine which kind of block is next in the file.

      @@ -80,8 +82,9 @@ next in the file.

    13. InstructionList (0x32).
    14. CompactionTable (0x33).
    -

    Except for the Header Block all blocks are variable -length. They consume just enough bytes to express their contents.

    +

    All blocks are variable length. They consume just enough bytes to express +their contents. Each block begins with an integer identifier and the length +of the block.

    Lists
    @@ -112,6 +115,88 @@ sections that follow will provide the details on how these fields are written and how the bits are to be interpreted.

    +
    Encoding Primitives
    +
    +

    Each field that can be put out is encoded into the file using a small set +of primitives. The rules for these primitives are described below.

    +

    Variable Bit Rate Encoding

    +

    To minimize the number of bytes written for small quantities, an encoding +scheme similar to UTF-8 is used to write integer data. The scheme is known as +variable bit rate (vbr) encoding. In this encoding, the high bit of each +byte is used to indicate if more bytes follow. If (byte & 0x80) is non-zero +in any given byte, it means there is another byte immediately following that +also contributes to the value. For the final byte (byte & 0x80) is false +(the high bit is not set). In each byte only the low seven bits contribute to +the value. Consequently 32-bit quantities can take from one to five +bytes to encode. In general, smaller quantities will encode in fewer bytes, +as follows:

    + + + + + + + + + + + + + + + + +
    Byte #Significant BitsMaximum Value
    10-6127
    27-1316,383
    314-202,097,151
    421-27268,435,455
    528-3434,359,738,367
    635-414,398,046,511,103
    742-48562,949,953,421,311
    849-5572,057,594,037,927,935
    956-629,223,372,036,854,775,807
    1063-691,180,591,620,717,411,303,423
    +

    Note that in practice, the tenth byte could only encode bits 63 and 64 +since the maximum quantity to use this encoding is a 64-bit integer.

    +

    The table below defines the encoding rules for type names used in the +descriptions of blocks and fields in the next section. Any type name with +the suffix _vbr indicate a quantity that is encoded using +variable bit rate encoding as described above.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    TypeRule
    unsignedA 32-bit unsigned integer that always occupies four + consecutive bytes. The unsigned integer is encoded using LSB first + ordering. That is bits 20 through 27 are in the + byte with the lowest file offset (little endian).
    uint_vbrA 32-bit unsigned integer that occupies from one to five + bytes using variable bit rate encoding.
    uint64_vbrA 64-bit unsigned integer that occupies from one to ten + bytes using variable bit rate encoding.
    int64_vbrA 64-bit signed integer that occupies from one to ten + bytes using variable bit rate encoding.
    charA single unsigned character encoded into one byte
    bitA single bit within a byte.
    stringA uint_vbr indicating the length of the character string + immediately followed by the characters of the string. There is no + terminating null byte in the string. Characters are interpreted as unsigned + char and are generally US-ASCII encoded.
    dataAn arbitrarily long segment of data to which no + interpretation is implied. This is used for float, double, and constant + initializers.
    +
    +
    Alignment

    To support cross-platform differences, the bytecode file is aligned on @@ -123,8 +208,8 @@ will be added to ensure that the next entry is aligned to a 32-bit boundary.

    -

    This section provides the detailed layout of the LLVM bytecode file format. - bit and byte level specifics.

    +

    This section provides the detailed layout of the LLVM bytecode file format. +bit and byte level specifics.

    @@ -153,9 +238,10 @@ will be added to ensure that the next entry is aligned to a 32-bit boundary. of bytes known as blocks. The blocks are written sequentially to the file in the following order:

      -
    1. Header. This block contains the file signature - (magic number), machine description and file format version (not LLVM - version).
    2. +
    3. Signature. This block contains the file signature + (magic number) that identifies the file as LLVM bytecode.
    4. +
    5. Module Block. This is the top level block in a + bytecode file. It contains all the other blocks.
    6. Global Type Pool. This block contains all the global (module) level types.
    7. Module Info. This block contains the types of the @@ -171,77 +257,181 @@ will be added to ensure that the next entry is aligned to a 32-bit boundary.
    -
    Header Block
    +
    Signature Block
    -

    The Header Block occurs in every LLVM bytecode file and is always first. It -simply provides a few bytes of data to identify the file, its format, and the -bytecode version. This block is fixed length and always eight bytes, as follows: - +

    The signature block occurs in every LLVM bytecode file and is always first. +It simply provides a few bytes of data to identify the file as being an LLVM +bytecode file. This block is always four bytes in length and differs from the +other blocks because there is no identifier and no block length at the start +of the block. Essentially, this block is just the "magic number" for the file. +

    - - - + + - - - + + - - - + + - - - + + +
    Byte(s) Bit(s) Align? Type Field Description
    0000-07NoChar
    00-Nochar Constant "l" (0x6C)
    0100-07NoChar
    01-Nochar Constant "l" (0x6C)
    0200-07NoChar
    02-Nochar Constant "v" (0x76)
    0300-07NoChar
    03-Nochar Constant "m" (0x6D)
    +

    + +
    Module Block
    +
    +

    The module block contains a small pre-amble and all the other blocks in +the file. Of particular note, the bytecode format number is simply a 28-bit +monotonically increase integer that identifiers the version of the bytecode +format. While the bytecode format version is not related to the LLVM release +(it doesn't automatically get increased with each new LLVM release), there is +a definite correspondence between the bytecode format version and the LLVM +release.

    +

    The table below shows the format of the module block header. The blocks it +contains are detailed in other sections.

    + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    04-0700NoBoolTarget is big endian?
    04-0701NoBoolTarget has long pointers?
    04-0702NoBoolTarget has no endianess?
    04-0703NoBoolTarget has no pointer size?
    04-0704-31YesUnsignedThe LLVM bytecode format version numberByte(s)Bit(s)Align?TypeField Description
    04-07-NounsignedModule Identifier (0x01)
    08-11-NounsignedSize of the module block in bytes
    12-1500Yesuint32_vbrFormat Information
    ''0-bitBig Endian?
    ''1-bitPointers Are 64-bit?
    ''2-bitHas No Endianess?
    ''3-bitHas No Pointer Size?
    ''4-31-bitBytecode Format Version
    16-end--blocksThe remaining bytes in the block consist + solely of other block types in sequence.
    Global Type Pool
    +

    The global type pool consists of type definitions. Their order of appearnce +in the file determines their slot number (0 based). Slot numbers are used to +replace pointers in the intermediate representation. Each slot number uniquely +identifies one entry in a type plane (a collection of values of the same type). +Since all values have types and are associated with the order in which the type +pool is written, the global type pool must be written as the first +block of a module. If it is not, attempts to read the file will fail because +both forward and backward type resolution will not be possible.

    +

    The type pool is simply a list of types definitions, as shown in the table +below.

    + + + + + + + + + + + + + + + + + + + + + + + + +
    Byte(s)Bit(s)Align?TypeField Description
    00-03-NounsignedType Pool Identifier (0x13)
    04-07-NounsignedSize in bytes of the symbol table block.
    08-111-Nouint32_vbrNumber of entries in type plane
    12-151-Nouint32_vbrType plane index for following entries
    16-end1,2-NotypeEach of the type definitions.
    1Maximum length shown, + may be smaller
    2Repeated field. +
    Module Info
    +

    To be determined.

    Constants
    +

    To be determined.

    Functions
    +

    To be determined.

    -
    Module Symbol Table
    +
    Symbol Table
    -

    The module symbol table is a list of +

    A symbol table can be put out in conjunction with a module or a function. +A symbol table is a list of type planes. Each type plane starts with the number +of entries in the plane and the type plane's slot number (so the type can be +looked up in the global type pool). For each entry in a type plane, the slot +number of the value and the name associated with that value are written. The +format is given in the table below.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Byte(s)Bit(s)Align?TypeField Description
    00-03-NounsignedSymbol Table Identifier (0x13)
    04-07-NounsignedSize in bytes of the symbol table block.
    08-111-Nouint32_vbrNumber of entries in type plane
    12-151-Nouint32_vbrType plane index for following entries
    16-191,2-Nouint32_vbrSlot number of a value.
    variable1,2-NostringName of the value in the symbol table.
    1Maximum length shown, + may be smaller
    2Repeated field. +