X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FBitCodeFormat.html;h=ed9bd082b56ad351a320c4d5831ef05ec8e98a97;hb=e7de7e3574245fe4cdee3ea895c3aeabca04db63;hp=0c4cd63f84426b9e80111c913a059e109a516cb6;hpb=58d054783d0c6a0c313da91b91f8ba7c8137cefe;p=oota-llvm.git diff --git a/docs/BitCodeFormat.html b/docs/BitCodeFormat.html index 0c4cd63f844..ed9bd082b56 100644 --- a/docs/BitCodeFormat.html +++ b/docs/BitCodeFormat.html @@ -19,12 +19,20 @@
This document first describes the LLVM bitstream format, then describes the -record structure used by LLVM IR files. +
LLVM IR files may be optionally embedded into a wrapper structure that makes it easy to embed extra data +along with LLVM IR files.
+ +This document first describes the LLVM bitstream format, describes the +wrapper format, then describes the record structure used by LLVM IR files.
@@ -100,8 +112,10 @@ understanding the encoding.The first four bytes of the stream identify the encoding of the file. This -is used by a reader to know what is contained in the file.
+The first two bytes of a bitcode file are 'BC' (0x42, 0x43). +The second two bytes are an application-specific magic number. Generic +bitcode tools can look at only the first two bytes to verify the file is +bitcode, while application-specific programs will want to look at all four.
-A bitstream literally consists of a stream of bits. This stream is made up of a -number of primitive values that encode a stream of integer values. These +A bitstream literally consists of a stream of bits, which are read in order +starting with the least significant bit of each byte. The stream is made up of a +number of primitive values that encode a stream of unsigned integer values. +These integers are are encoded in two ways: either as Fixed Width Integers or as Variable Width Integers. @@ -166,8 +182,8 @@ represent the following characters with the following 6-bit values:
Blocks in a bitstream denote nested regions of the stream, and are identified by a content-specific id number (for example, LLVM IR uses an ID of 12 to represent -function bodies). Nested blocks capture the hierachical structure of the data +function bodies). Block IDs 0-7 are reserved for standard blocks +whose meaning is defined by Bitcode; block IDs 8 and greater are +application specific. Nested blocks capture the hierachical structure of the data encoded in it, and various properties are associated with blocks as the file is parsed. Block definitions allow the reader to efficiently skip blocks in constant time if the reader wants a summary of blocks, or if it wants to @@ -252,8 +270,11 @@ block. In particular, each block maintains: block record is entered. The block entry specifies the abbrev id width for the body of the block. -
The ENTER_SUBBLOCK abbreviation ID specifies the start of a new block record. The blockid value is encoded as a 8-bit VBR identifier, and indicates -the type of block being entered (which is application specific). The +the type of block being entered (which can be a standard +block or an application-specific block). The newabbrevlen value is a 4-bit VBR which specifies the abbrev id width for the sub-block. The blocklen is a 32-bit aligned value that specifies the size of the subblock, in 32-bit words. This value @@ -391,6 +413,17 @@ operators, the abbreviation does not need to be emitted.
[DEFINE_ABBREV, numabbrevopsvbr5, abbrevop0, abbrevop1, ...]
+A DEFINE_ABBREV record adds an abbreviation to the list of currently +defined abbreviations in the scope of this block. This definition only +exists inside this immediate block -- it is not visible in subblocks or +enclosing blocks. +Abbreviations are implicitly assigned IDs +sequentially starting from 4 (the first application-defined abbreviation ID). +Any abbreviations defined in a BLOCKINFO record receive IDs first, in order, +followed by any abbreviations defined within the block itself. +Abbreviated data records reference this ID to indicate what abbreviation +they are invoking.
+An abbreviation definition consists of the DEFINE_ABBREV abbrevid followed by a VBR that specifies the number of abbrev operands, then the abbrev operands themselves. Abbreviation operands come in three forms. They all start @@ -403,14 +436,147 @@ Literal operands specify that the value in the result is always a single specific value. This specific value is emitted as a vbr8 after the bit indicating that it is a literal operand.
The possible operand encodings are:
+ +For example, target triples in LLVM modules are encoded as a record of the +form [TRIPLE, 'a', 'b', 'c', 'd']. Consider if the bitstream emitted +the following abbrev entry:
+ +When emitting a record with this abbreviation, the above entry would be +emitted as:
+ +[4abbrevwidth, 24, 4vbr6, + 06, 16, 26, 36]
+ +These values are:
+ +With this abbreviation, the triple is emitted with only 37 bits (assuming a +abbrev id width of 3). Without the abbreviation, significantly more space would +be required to emit the target triple. Also, since the TRIPLE value is not +emitted as a literal in the abbreviation, the abbreviation can also be used for +any other string value. +
+ + + + + + ++In addition to the basic block structure and record encodings, the bitstream +also defines specific builtin block types. These block types specify how the +stream is to be decoded or other metadata. In the future, new standard blocks +may be added. Block IDs 0-7 are reserved for standard blocks. +
+ +The BLOCKINFO block allows the description of metadata for other blocks. The + currently specified records are:
+ ++The SETBID record indicates which block ID is being described. SETBID +records can occur multiple times throughout the block to change which +block ID is being described. There must be a SETBID record prior to +any other records. +
+ ++Standard DEFINE_ABBREV records can occur inside BLOCKINFO blocks, but unlike +their occurrence in normal blocks, the abbreviation is defined for blocks +matching the block ID we are describing, not the BLOCKINFO block itself. +The abbreviations defined in BLOCKINFO blocks receive abbreviation ids +as described in DEFINE_ABBREV. +
+ ++Note that although the data in BLOCKINFO blocks is described as "metadata," the +abbreviations they contain are essential for parsing records from the +corresponding blocks. It is not safe to skip them. +
+ +Bitcode files for LLVM IR may optionally be wrapped in a simple wrapper +structure. This structure contains a simple header that indicates the offset +and size of the embedded BC file. This allows additional information to be +stored alongside the BC file. The structure of this file header is: +
+ ++
+[Magic32, + Version32, + Offset32, + Size32, + CPUType32] ++ +
Each of the fields are 32-bit fields stored in little endian form (as with +the rest of the bitcode file fields). The Magic number is always +0x0B17C0DE and the version is currently always 0. The Offset +field is the offset in bytes to the start of the bitcode stream in the file, and +the Size field is a size in bytes of the stream. CPUType is a target-specific +value that can be used to encode the CPU of the target.
LLVM IR is encoded into a bitstream by defining blocks and records. It uses +blocks for things like constant pools, functions, symbol tables, etc. It uses +records for things like instructions, global variable descriptors, type +descriptions, etc. This document does not describe the set of abbreviations +that the writer uses, as these are fully self-described in the file, and the +reader is not allowed to build in any knowledge of this.
+ ++The magic number for LLVM IR files is: +
+ +[0x04, 0xC4, 0xE4, 0xD4]
+ +When combined with the bitcode magic number and viewed as bytes, this is "BC 0xC0DE".
+ ++Variable Width Integers are an efficient way to +encode arbitrary sized unsigned values, but is an extremely inefficient way to +encode signed values (as signed values are otherwise treated as maximally large +unsigned values).
+ +As such, signed vbr values of a specific width are emitted as follows:
+ +With this encoding, small positive and small negative values can both be +emitted efficiently.
+ ++LLVM IR is defined with the following blocks: +
+ ++