From: Reid Spencer This document describes the LLVM bytecode file format as of version 1.3.
-It specifies the binary encoding rules of the bytecode file format
-so that equivalent systems can encode bytecode files correctly. The LLVM
-bytecode representation is used to store the intermediate representation on
-disk in compacted form.
+ This document describes the LLVM bytecode file format. It specifies the
+ binary encoding rules of the bytecode file format so that equivalent systems
+ can encode bytecode files correctly. The LLVM bytecode representation is
+ used to store the intermediate representation on disk in compacted form. The LLVM bytecode format may change in the future, but LLVM will always be
+ backwards compatible with older formats. This document will only describe
+ the most current version of the bytecode format. See
+ Version Differences for the details on how the
+ current version is different from previous versions. This section describes the general concepts of the bytecode file format
-without getting into bit and byte level specifics. Note that the LLVM bytecode
-format may change in the future, but will always be backwards compatible with
-older formats. This document only describes the most current version of the
-bytecode format. This section describes the general concepts of the bytecode file format
+ without getting into specific layout details. It is recommended that you read
+ this section thoroughly before interpreting the detailed descriptions. LLVM bytecode files consist simply of a sequence of blocks of bytes.
-Each block begins with an header of two unsigned integers. The first value
-identifies the type of block and the second value provides the size of the
-block in bytes. 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. Note that blocks
-can be nested within other blocks. All blocks are variable length, and the block header specifies the size of
-the block. All blocks begin on a byte index that is aligned to an even 32-bit
-boundary. That is, the first block is 32-bit aligned because it starts at offset
-0. Each block is padded with zero fill bytes to ensure that the next block also
-starts on a 32-bit boundary. LLVM bytecode files consist simply of a sequence of blocks of bytes using
+ a binary encoding Each block begins with an header of two unsigned integers.
+ The first value identifies the type of block and the second value provides
+ the size of the block in bytes. 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. Note that blocks can be nested within other blocks. All blocks are variable length, and the block header specifies the size
+ of the block. All blocks begin on a byte index that is aligned to an even
+ 32-bit boundary. That is, the first block is 32-bit aligned because it
+ starts at offset 0. Each block is padded with zero fill bytes to ensure that
+ the next block also starts on a 32-bit boundary.
LLVM Bytecode blocks often contain lists of things of a similar type. For example, a function contains a list of instructions and a function type contains a list of argument types. There are two basic types of lists: - length lists, and null terminated lists, as described here:
-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.
-Most of the values written to LLVM bytecode files are small integers. To minimize the number of bytes written for these quantities, an encoding scheme similar to UTF-8 is used to write integer data. The scheme is known as @@ -177,52 +170,74 @@ with the sign bit as the low order bit instead of the high order bit. This allows small negative quantities to be encoded efficiently. For example, -3 is encoded as "((3 << 1) | 1)" and 3 is encoded as "(3 << 1) | 0)", emitted with the standard vbr encoding above.
+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.
+ + +Each field in the bytecode format is encoded into the file using a small + set of primitive formats. The table below defines the encoding rules for the + various primitives used and gives them each a type name. The type names used + in the descriptions of blocks and fields in the Detailed + Layoutnext section. Any type name with the suffix _vbr indicates + a quantity that is encoded using variable bit rate encoding as described + above.
Type | Rule | ||
---|---|---|---|
unsigned | +unsigned | A 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). | |
uint32_vbr | +uint32_vbr | A 32-bit unsigned integer that occupies from one to five bytes using variable bit rate encoding. | |
uint64_vbr | +uint64_vbr | A 64-bit unsigned integer that occupies from one to ten bytes using variable bit rate encoding. | |
int64_vbr | +int64_vbr | A 64-bit signed integer that occupies from one to ten bytes using the signed variable bit rate encoding. | |
char | +char | A single unsigned character encoded into one byte | |
bit | -A single bit within some larger integer field. | +bit(n-m) | +A set of bit within some larger integer field. The
+ values of n and m specify the inclusive range
+ of bits that define the subfield. The value for m may be
+ omitted if its the same as n . |
string | +string | A uint32_vbr indicating the type of the constant string which also includes its length, immediately followed by the characters of the string. There is no terminating null byte in the string. | |
data | +data | An arbitrarily long segment of data to which no interpretation is implied. This is used for float, double, and constant initializers. | |
block | +llist(x) | +A length list of x. This means the list is encoded as + an uint32_vbr providing the length of the list, + followed by a sequence of that many "x" items. This implies that the reader + should iterate the number of times provided by the length. | +|
zlist(x) | +A zero-terminated list of x. This means the list is encoded + as a sequence of an indeterminate number of "x" items, followed by an + uint32_vbr terminating value. This implies that none + of the "x" items can have a zero value (or else the list terminates). | +||
block | A block of data that is logically related. A block begins with an unsigned that provides the block identifier (constant value) and an unsigned that @@ -232,6 +247,56 @@ variable bit rate encoding as described above. |
In the detailed block and field descriptions that follow, a regex like + notation is used to describe optional and repeated fields. A very limited + subset of regex is used to describe these, as given in the following table: +
+Character | +Meaning | +
---|---|
? |
+ The question mark indicates 0 or 1 occurrences of + the thing preceding it. | +
* |
+ The asterisk indicates 0 or more occurrences of the + thing preceding it. | +
+ |
+ The plus sign indicates 1 or more occurrences of the + thing preceding it. | +
() |
+ Parentheses are used for grouping. | +
, |
+ The comma separates sequential fields. | +
So, for example, consider the following specifications:
+string?
(uint32_vbr,uin32_vbr)+
(unsigned?,uint32_vbr)*
(llist(unsigned))?
with the following interpretations:
+
%MyType = type { int, sbyte }
%MyVar = external global %MyType
-
there are two definitions. The definition of %MyVar uses %MyType. In the C++ IR this linkage between %MyVar and %MyType is @@ -276,7 +341,7 @@ This is exactly what the compaction table does.
This section provides the general structur of the LLVM bytecode file +
This section provides the general structure of the LLVM bytecode file format. The bytecode file format requires blocks to be in a certain order and nested in a particular way so that an LLVM module can be constructed efficiently from the contents of the file. This ordering defines a general @@ -321,7 +386,7 @@ This is exactly what the compaction table does.
except function arguments, global values and constant strings.Use the links in the table or see Block Types for -details about the contents of each of the block types.
+Use the links in the table for details about the contents of each of the block types.
Bit(s) | Type | Description | ||
---|---|---|---|---|
0 | bit | -Big Endian? | +bit(0) | +Target is big endian? |
1 | bit | -Pointers Are 64-bit? | +bit(1) | +On target pointers are 64-bit? |
2 | bit | -Has No Endianess? | +bit(2) | +Target has no endianess? |
3 | bit | -Has No Pointer Size? | +bit(3) | +Target has no pointer size? |
4-31 | bit | -Bytecode Format Version | +bit(4-31) | +Bytecode format version |
@@ -503,24 +566,16 @@ below.
Types in the type pool are defined using a different format for each -basic type of type as given in the following sections.
+Types in the type pool are defined using a different format for each kind +of type, as given in the following sections.
The primitive types encompass the basic integer and floating point types
Type | Description | ||
---|---|---|---|
uint32_vbr | -Type ID For The Primitive (1-11)1 | +uint32_vbr | +Type ID for the primitive types (values 1 to 11) + 1 |
llvm::Type::TypeID
enumeration in
+ include/llvm/Type.h
. The enumeration gives the following
+ mapping:Type | Description | ||
---|---|---|---|
uint32_vbr | +uint32_vbr | Type ID for function types (13) | |
uint32_vbr | +uint32_vbr | Slot number of function's return type. | |
uint32_vbr | -The number of arguments in the function. | -||
uint32_vbr | -Slot number of each argument's type.1 | +llist(uint32_vbr) | +Slot number of each argument's type. |
uint32_vbr | -Value 0 if this is a varargs function.2 | +uint32_vbr? | +Value 0 if this is a varargs function, missing otherwise. |
Type | Description | ||
---|---|---|---|
uint32_vbr | +uint32_vbr | Type ID for structure types (14) | |
uint32_vbr | -Slot number of each of the element's fields.1 | -||
uint32_vbr | -Null Terminator (VoidTy type id) | +zlist(uint32_vbr) | +Slot number of each of the element's fields. |
Type | Description | |
---|---|---|
uint32_vbr | +uint32_vbr | Type ID for Array Types (15) |
uint32_vbr | +uint32_vbr | Slot number of array's element type. |
uint32_vbr | +uint32_vbr | The number of elements in the array. |
Global variables are written using a single - uint32_vbr that encodes information about the global - variable. The table below provides the bit layout of the value written for - each global variable.
+Global variables are written using an uint32_vbr + that encodes information about the global variable and a list of the constant + initializers for the global var, if any.
+The table below provides the bit layout of the first + uint32_vbr that describes the global variable.
Bit(s) | Type | Description | ||
---|---|---|---|---|
0 | bit | +bit(0) | Is constant? | |
1 | bit | -Has initializer?1 | +bit(1) | +Has initializer? Note that this bit determines whether + the constant initializer field (described below) follows. |
2-4 | enumeration | +bit(2-4) | Linkage type: 0=External, 1=Weak, 2=Appending, 3=Internal, 4=LinkOnce | |
5-31 | type slot | +bit(5-31) | Slot number of type for the global variable. |
The table below provides the format of the constant initializers for the + global variable field, if it has one.
+Type | +Description | +
---|---|
(zlist(uint32_vbr))? + + | +An optional zero-terminated list of slot numbers of + the global variable's constant initializer. | +
Common Block Header
@@ -725,6 +770,9 @@ Notes:Module Constant Pool Preamble (constant strings)
@@ -738,19 +786,17 @@ Notes:Function Constant Pool Preamble (function types)
The structure of the types for functions is identical to the Global Type Pool. Please refer to that section @@ -767,7 +813,7 @@ Notes:
To be determined.
+Function definitions contain the linkage, constant pool or compaction + table, instruction list, and symbol table for a function. The following table + shows the structure of a function definition.
Type | Field Description | +||
---|---|---|---|
unsigned | +Function definition block identifier (0x11) | +||
unsigned | +Size in bytes of the function definition block. | ||
uint32_vbr | The linkage type of the function: 0=External, 1=Weak, 2=Appending, 3=Internal, 4=LinkOnce1 | ||
constant pool | -The constant pool block for this function. - 2 - | +block | +The constant pool block + for this function.2 |
compaction table | -The compaction table block for the function. - 2 - | +block | +The compaction table + block for the function.2 |
instruction list | -The list of instructions in the function. | +block | +The instruction list + for the function. |
symbol table | -The function's slot table containing only those - symbols pertinent to the function (mostly block labels). - | +block | +The function's symbol table + containing only those symbols pertinent to the function (mostly + block labels). |
Type | @@ -884,26 +938,23 @@ Notes:uint32_vbr | The number of types that follow |
---|---|---|
uint32_vbr | +uint32_vbr+ | The slot number in the global type plane of the type that will be referenced in the function with the index of - this entry in the compaction table.1 | + this entry in the compaction table.
type_len | An encoding of the type and number of values that - follow.2 | + follow. This field's encoding varies depending on the size of + the type plane. See Type and Length for + further details.|
uint32_vbr | +uint32_vbr+ | The slot number in the globals of the value that will be referenced in the function with the index of this entry in - the compaction table1 | + the compaction table