Fix typo.
[llvm.git] / docs / BitCodeFormat.html
blobf1ddefdea9afefe7664c6a7433234a1364dde6e8
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>LLVM Bitcode File Format</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
8 </head>
9 <body>
10 <div class="doc_title"> LLVM Bitcode File Format </div>
11 <ol>
12 <li><a href="#abstract">Abstract</a></li>
13 <li><a href="#overview">Overview</a></li>
14 <li><a href="#bitstream">Bitstream Format</a>
15 <ol>
16 <li><a href="#magic">Magic Numbers</a></li>
17 <li><a href="#primitives">Primitives</a></li>
18 <li><a href="#abbrevid">Abbreviation IDs</a></li>
19 <li><a href="#blocks">Blocks</a></li>
20 <li><a href="#datarecord">Data Records</a></li>
21 <li><a href="#abbreviations">Abbreviations</a></li>
22 <li><a href="#stdblocks">Standard Blocks</a></li>
23 </ol>
24 </li>
25 <li><a href="#wrapper">Bitcode Wrapper Format</a>
26 </li>
27 <li><a href="#llvmir">LLVM IR Encoding</a>
28 <ol>
29 <li><a href="#basics">Basics</a></li>
30 <li><a href="#MODULE_BLOCK">MODULE_BLOCK Contents</a></li>
31 <li><a href="#PARAMATTR_BLOCK">PARAMATTR_BLOCK Contents</a></li>
32 <li><a href="#TYPE_BLOCK">TYPE_BLOCK Contents</a></li>
33 <li><a href="#CONSTANTS_BLOCK">CONSTANTS_BLOCK Contents</a></li>
34 <li><a href="#FUNCTION_BLOCK">FUNCTION_BLOCK Contents</a></li>
35 <li><a href="#TYPE_SYMTAB_BLOCK">TYPE_SYMTAB_BLOCK Contents</a></li>
36 <li><a href="#VALUE_SYMTAB_BLOCK">VALUE_SYMTAB_BLOCK Contents</a></li>
37 <li><a href="#METADATA_BLOCK">METADATA_BLOCK Contents</a></li>
38 <li><a href="#METADATA_ATTACHMENT">METADATA_ATTACHMENT Contents</a></li>
39 </ol>
40 </li>
41 </ol>
42 <div class="doc_author">
43 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
44 and <a href="http://www.reverberate.org">Joshua Haberman</a>.
45 </p>
46 </div>
48 <!-- *********************************************************************** -->
49 <div class="doc_section"> <a name="abstract">Abstract</a></div>
50 <!-- *********************************************************************** -->
52 <div class="doc_text">
54 <p>This document describes the LLVM bitstream file format and the encoding of
55 the LLVM IR into it.</p>
57 </div>
59 <!-- *********************************************************************** -->
60 <div class="doc_section"> <a name="overview">Overview</a></div>
61 <!-- *********************************************************************** -->
63 <div class="doc_text">
65 <p>
66 What is commonly known as the LLVM bitcode file format (also, sometimes
67 anachronistically known as bytecode) is actually two things: a <a
68 href="#bitstream">bitstream container format</a>
69 and an <a href="#llvmir">encoding of LLVM IR</a> into the container format.</p>
71 <p>
72 The bitstream format is an abstract encoding of structured data, very
73 similar to XML in some ways. Like XML, bitstream files contain tags, and nested
74 structures, and you can parse the file without having to understand the tags.
75 Unlike XML, the bitstream format is a binary encoding, and unlike XML it
76 provides a mechanism for the file to self-describe "abbreviations", which are
77 effectively size optimizations for the content.</p>
79 <p>LLVM IR files may be optionally embedded into a <a
80 href="#wrapper">wrapper</a> structure that makes it easy to embed extra data
81 along with LLVM IR files.</p>
83 <p>This document first describes the LLVM bitstream format, describes the
84 wrapper format, then describes the record structure used by LLVM IR files.
85 </p>
87 </div>
89 <!-- *********************************************************************** -->
90 <div class="doc_section"> <a name="bitstream">Bitstream Format</a></div>
91 <!-- *********************************************************************** -->
93 <div class="doc_text">
95 <p>
96 The bitstream format is literally a stream of bits, with a very simple
97 structure. This structure consists of the following concepts:
98 </p>
100 <ul>
101 <li>A "<a href="#magic">magic number</a>" that identifies the contents of
102 the stream.</li>
103 <li>Encoding <a href="#primitives">primitives</a> like variable bit-rate
104 integers.</li>
105 <li><a href="#blocks">Blocks</a>, which define nested content.</li>
106 <li><a href="#datarecord">Data Records</a>, which describe entities within the
107 file.</li>
108 <li>Abbreviations, which specify compression optimizations for the file.</li>
109 </ul>
111 <p>Note that the <a
112 href="CommandGuide/html/llvm-bcanalyzer.html">llvm-bcanalyzer</a> tool can be
113 used to dump and inspect arbitrary bitstreams, which is very useful for
114 understanding the encoding.</p>
116 </div>
118 <!-- ======================================================================= -->
119 <div class="doc_subsection"><a name="magic">Magic Numbers</a>
120 </div>
122 <div class="doc_text">
124 <p>The first two bytes of a bitcode file are 'BC' (0x42, 0x43).
125 The second two bytes are an application-specific magic number. Generic
126 bitcode tools can look at only the first two bytes to verify the file is
127 bitcode, while application-specific programs will want to look at all four.</p>
129 </div>
131 <!-- ======================================================================= -->
132 <div class="doc_subsection"><a name="primitives">Primitives</a>
133 </div>
135 <div class="doc_text">
138 A bitstream literally consists of a stream of bits, which are read in order
139 starting with the least significant bit of each byte. The stream is made up of a
140 number of primitive values that encode a stream of unsigned integer values.
141 These integers are encoded in two ways: either as <a href="#fixedwidth">Fixed
142 Width Integers</a> or as <a href="#variablewidth">Variable Width
143 Integers</a>.
144 </p>
146 </div>
148 <!-- _______________________________________________________________________ -->
149 <div class="doc_subsubsection"> <a name="fixedwidth">Fixed Width Integers</a>
150 </div>
152 <div class="doc_text">
154 <p>Fixed-width integer values have their low bits emitted directly to the file.
155 For example, a 3-bit integer value encodes 1 as 001. Fixed width integers
156 are used when there are a well-known number of options for a field. For
157 example, boolean values are usually encoded with a 1-bit wide integer.
158 </p>
160 </div>
162 <!-- _______________________________________________________________________ -->
163 <div class="doc_subsubsection"> <a name="variablewidth">Variable Width
164 Integers</a></div>
166 <div class="doc_text">
168 <p>Variable-width integer (VBR) values encode values of arbitrary size,
169 optimizing for the case where the values are small. Given a 4-bit VBR field,
170 any 3-bit value (0 through 7) is encoded directly, with the high bit set to
171 zero. Values larger than N-1 bits emit their bits in a series of N-1 bit
172 chunks, where all but the last set the high bit.</p>
174 <p>For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a
175 vbr4 value. The first set of four bits indicates the value 3 (011) with a
176 continuation piece (indicated by a high bit of 1). The next word indicates a
177 value of 24 (011 << 3) with no continuation. The sum (3+24) yields the value
179 </p>
181 </div>
183 <!-- _______________________________________________________________________ -->
184 <div class="doc_subsubsection"> <a name="char6">6-bit characters</a></div>
186 <div class="doc_text">
188 <p>6-bit characters encode common characters into a fixed 6-bit field. They
189 represent the following characters with the following 6-bit values:</p>
191 <div class="doc_code">
192 <pre>
193 'a' .. 'z' &mdash; 0 .. 25
194 'A' .. 'Z' &mdash; 26 .. 51
195 '0' .. '9' &mdash; 52 .. 61
196 '.' &mdash; 62
197 '_' &mdash; 63
198 </pre>
199 </div>
201 <p>This encoding is only suitable for encoding characters and strings that
202 consist only of the above characters. It is completely incapable of encoding
203 characters not in the set.</p>
205 </div>
207 <!-- _______________________________________________________________________ -->
208 <div class="doc_subsubsection"> <a name="wordalign">Word Alignment</a></div>
210 <div class="doc_text">
212 <p>Occasionally, it is useful to emit zero bits until the bitstream is a
213 multiple of 32 bits. This ensures that the bit position in the stream can be
214 represented as a multiple of 32-bit words.</p>
216 </div>
219 <!-- ======================================================================= -->
220 <div class="doc_subsection"><a name="abbrevid">Abbreviation IDs</a>
221 </div>
223 <div class="doc_text">
226 A bitstream is a sequential series of <a href="#blocks">Blocks</a> and
227 <a href="#datarecord">Data Records</a>. Both of these start with an
228 abbreviation ID encoded as a fixed-bitwidth field. The width is specified by
229 the current block, as described below. The value of the abbreviation ID
230 specifies either a builtin ID (which have special meanings, defined below) or
231 one of the abbreviation IDs defined for the current block by the stream itself.
232 </p>
235 The set of builtin abbrev IDs is:
236 </p>
238 <ul>
239 <li><tt>0 - <a href="#END_BLOCK">END_BLOCK</a></tt> &mdash; This abbrev ID marks
240 the end of the current block.</li>
241 <li><tt>1 - <a href="#ENTER_SUBBLOCK">ENTER_SUBBLOCK</a></tt> &mdash; This
242 abbrev ID marks the beginning of a new block.</li>
243 <li><tt>2 - <a href="#DEFINE_ABBREV">DEFINE_ABBREV</a></tt> &mdash; This defines
244 a new abbreviation.</li>
245 <li><tt>3 - <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a></tt> &mdash; This ID
246 specifies the definition of an unabbreviated record.</li>
247 </ul>
249 <p>Abbreviation IDs 4 and above are defined by the stream itself, and specify
250 an <a href="#abbrev_records">abbreviated record encoding</a>.</p>
252 </div>
254 <!-- ======================================================================= -->
255 <div class="doc_subsection"><a name="blocks">Blocks</a>
256 </div>
258 <div class="doc_text">
261 Blocks in a bitstream denote nested regions of the stream, and are identified by
262 a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
263 function bodies). Block IDs 0-7 are reserved for <a href="#stdblocks">standard blocks</a>
264 whose meaning is defined by Bitcode; block IDs 8 and greater are
265 application specific. Nested blocks capture the hierarchical structure of the data
266 encoded in it, and various properties are associated with blocks as the file is
267 parsed. Block definitions allow the reader to efficiently skip blocks
268 in constant time if the reader wants a summary of blocks, or if it wants to
269 efficiently skip data it does not understand. The LLVM IR reader uses this
270 mechanism to skip function bodies, lazily reading them on demand.
271 </p>
274 When reading and encoding the stream, several properties are maintained for the
275 block. In particular, each block maintains:
276 </p>
278 <ol>
279 <li>A current abbrev id width. This value starts at 2 at the beginning of
280 the stream, and is set every time a
281 block record is entered. The block entry specifies the abbrev id width for
282 the body of the block.</li>
284 <li>A set of abbreviations. Abbreviations may be defined within a block, in
285 which case they are only defined in that block (neither subblocks nor
286 enclosing blocks see the abbreviation). Abbreviations can also be defined
287 inside a <tt><a href="#BLOCKINFO">BLOCKINFO</a></tt> block, in which case
288 they are defined in all blocks that match the ID that the BLOCKINFO block is
289 describing.
290 </li>
291 </ol>
294 As sub blocks are entered, these properties are saved and the new sub-block has
295 its own set of abbreviations, and its own abbrev id width. When a sub-block is
296 popped, the saved values are restored.
297 </p>
299 </div>
301 <!-- _______________________________________________________________________ -->
302 <div class="doc_subsubsection"> <a name="ENTER_SUBBLOCK">ENTER_SUBBLOCK
303 Encoding</a></div>
305 <div class="doc_text">
307 <p><tt>[ENTER_SUBBLOCK, blockid<sub>vbr8</sub>, newabbrevlen<sub>vbr4</sub>,
308 &lt;align32bits&gt;, blocklen<sub>32</sub>]</tt></p>
311 The <tt>ENTER_SUBBLOCK</tt> abbreviation ID specifies the start of a new block
312 record. The <tt>blockid</tt> value is encoded as an 8-bit VBR identifier, and
313 indicates the type of block being entered, which can be
314 a <a href="#stdblocks">standard block</a> or an application-specific block.
315 The <tt>newabbrevlen</tt> value is a 4-bit VBR, which specifies the abbrev id
316 width for the sub-block. The <tt>blocklen</tt> value is a 32-bit aligned value
317 that specifies the size of the subblock in 32-bit words. This value allows the
318 reader to skip over the entire block in one jump.
319 </p>
321 </div>
323 <!-- _______________________________________________________________________ -->
324 <div class="doc_subsubsection"> <a name="END_BLOCK">END_BLOCK
325 Encoding</a></div>
327 <div class="doc_text">
329 <p><tt>[END_BLOCK, &lt;align32bits&gt;]</tt></p>
332 The <tt>END_BLOCK</tt> abbreviation ID specifies the end of the current block
333 record. Its end is aligned to 32-bits to ensure that the size of the block is
334 an even multiple of 32-bits.
335 </p>
337 </div>
341 <!-- ======================================================================= -->
342 <div class="doc_subsection"><a name="datarecord">Data Records</a>
343 </div>
345 <div class="doc_text">
347 Data records consist of a record code and a number of (up to) 64-bit
348 integer values. The interpretation of the code and values is
349 application specific and may vary between different block types.
350 Records can be encoded either using an unabbrev record, or with an
351 abbreviation. In the LLVM IR format, for example, there is a record
352 which encodes the target triple of a module. The code is
353 <tt>MODULE_CODE_TRIPLE</tt>, and the values of the record are the
354 ASCII codes for the characters in the string.
355 </p>
357 </div>
359 <!-- _______________________________________________________________________ -->
360 <div class="doc_subsubsection"> <a name="UNABBREV_RECORD">UNABBREV_RECORD
361 Encoding</a></div>
363 <div class="doc_text">
365 <p><tt>[UNABBREV_RECORD, code<sub>vbr6</sub>, numops<sub>vbr6</sub>,
366 op0<sub>vbr6</sub>, op1<sub>vbr6</sub>, ...]</tt></p>
369 An <tt>UNABBREV_RECORD</tt> provides a default fallback encoding, which is both
370 completely general and extremely inefficient. It can describe an arbitrary
371 record by emitting the code and operands as VBRs.
372 </p>
375 For example, emitting an LLVM IR target triple as an unabbreviated record
376 requires emitting the <tt>UNABBREV_RECORD</tt> abbrevid, a vbr6 for the
377 <tt>MODULE_CODE_TRIPLE</tt> code, a vbr6 for the length of the string, which is
378 equal to the number of operands, and a vbr6 for each character. Because there
379 are no letters with values less than 32, each letter would need to be emitted as
380 at least a two-part VBR, which means that each letter would require at least 12
381 bits. This is not an efficient encoding, but it is fully general.
382 </p>
384 </div>
386 <!-- _______________________________________________________________________ -->
387 <div class="doc_subsubsection"> <a name="abbrev_records">Abbreviated Record
388 Encoding</a></div>
390 <div class="doc_text">
392 <p><tt>[&lt;abbrevid&gt;, fields...]</tt></p>
395 An abbreviated record is a abbreviation id followed by a set of fields that are
396 encoded according to the <a href="#abbreviations">abbreviation definition</a>.
397 This allows records to be encoded significantly more densely than records
398 encoded with the <tt><a href="#UNABBREV_RECORD">UNABBREV_RECORD</a></tt> type,
399 and allows the abbreviation types to be specified in the stream itself, which
400 allows the files to be completely self describing. The actual encoding of
401 abbreviations is defined below.
402 </p>
404 <p>The record code, which is the first field of an abbreviated record,
405 may be encoded in the abbreviation definition (as a literal
406 operand) or supplied in the abbreviated record (as a Fixed or VBR
407 operand value).</p>
409 </div>
411 <!-- ======================================================================= -->
412 <div class="doc_subsection"><a name="abbreviations">Abbreviations</a>
413 </div>
415 <div class="doc_text">
417 Abbreviations are an important form of compression for bitstreams. The idea is
418 to specify a dense encoding for a class of records once, then use that encoding
419 to emit many records. It takes space to emit the encoding into the file, but
420 the space is recouped (hopefully plus some) when the records that use it are
421 emitted.
422 </p>
425 Abbreviations can be determined dynamically per client, per file. Because the
426 abbreviations are stored in the bitstream itself, different streams of the same
427 format can contain different sets of abbreviations according to the needs
428 of the specific stream.
429 As a concrete example, LLVM IR files usually emit an abbreviation
430 for binary operators. If a specific LLVM module contained no or few binary
431 operators, the abbreviation does not need to be emitted.
432 </p>
433 </div>
435 <!-- _______________________________________________________________________ -->
436 <div class="doc_subsubsection"><a name="DEFINE_ABBREV">DEFINE_ABBREV
437 Encoding</a></div>
439 <div class="doc_text">
441 <p><tt>[DEFINE_ABBREV, numabbrevops<sub>vbr5</sub>, abbrevop0, abbrevop1,
442 ...]</tt></p>
445 A <tt>DEFINE_ABBREV</tt> record adds an abbreviation to the list of currently
446 defined abbreviations in the scope of this block. This definition only exists
447 inside this immediate block &mdash; it is not visible in subblocks or enclosing
448 blocks. Abbreviations are implicitly assigned IDs sequentially starting from 4
449 (the first application-defined abbreviation ID). Any abbreviations defined in a
450 <tt>BLOCKINFO</tt> record for the particular block type
451 receive IDs first, in order, followed by any
452 abbreviations defined within the block itself. Abbreviated data records
453 reference this ID to indicate what abbreviation they are invoking.
454 </p>
457 An abbreviation definition consists of the <tt>DEFINE_ABBREV</tt> abbrevid
458 followed by a VBR that specifies the number of abbrev operands, then the abbrev
459 operands themselves. Abbreviation operands come in three forms. They all start
460 with a single bit that indicates whether the abbrev operand is a literal operand
461 (when the bit is 1) or an encoding operand (when the bit is 0).
462 </p>
464 <ol>
465 <li>Literal operands &mdash; <tt>[1<sub>1</sub>, litvalue<sub>vbr8</sub>]</tt>
466 &mdash; Literal operands specify that the value in the result is always a single
467 specific value. This specific value is emitted as a vbr8 after the bit
468 indicating that it is a literal operand.</li>
469 <li>Encoding info without data &mdash; <tt>[0<sub>1</sub>,
470 encoding<sub>3</sub>]</tt> &mdash; Operand encodings that do not have extra
471 data are just emitted as their code.
472 </li>
473 <li>Encoding info with data &mdash; <tt>[0<sub>1</sub>, encoding<sub>3</sub>,
474 value<sub>vbr5</sub>]</tt> &mdash; Operand encodings that do have extra data are
475 emitted as their code, followed by the extra data.
476 </li>
477 </ol>
479 <p>The possible operand encodings are:</p>
481 <ul>
482 <li>Fixed (code 1): The field should be emitted as
483 a <a href="#fixedwidth">fixed-width value</a>, whose width is specified by
484 the operand's extra data.</li>
485 <li>VBR (code 2): The field should be emitted as
486 a <a href="#variablewidth">variable-width value</a>, whose width is
487 specified by the operand's extra data.</li>
488 <li>Array (code 3): This field is an array of values. The array operand
489 has no extra data, but expects another operand to follow it, indicating
490 the element type of the array. When reading an array in an abbreviated
491 record, the first integer is a vbr6 that indicates the array length,
492 followed by the encoded elements of the array. An array may only occur as
493 the last operand of an abbreviation (except for the one final operand that
494 gives the array's type).</li>
495 <li>Char6 (code 4): This field should be emitted as
496 a <a href="#char6">char6-encoded value</a>. This operand type takes no
497 extra data. Char6 encoding is normally used as an array element type.
498 </li>
499 <li>Blob (code 5): This field is emitted as a vbr6, followed by padding to a
500 32-bit boundary (for alignment) and an array of 8-bit objects. The array of
501 bytes is further followed by tail padding to ensure that its total length is
502 a multiple of 4 bytes. This makes it very efficient for the reader to
503 decode the data without having to make a copy of it: it can use a pointer to
504 the data in the mapped in file and poke directly at it. A blob may only
505 occur as the last operand of an abbreviation.</li>
506 </ul>
509 For example, target triples in LLVM modules are encoded as a record of the
510 form <tt>[TRIPLE, 'a', 'b', 'c', 'd']</tt>. Consider if the bitstream emitted
511 the following abbrev entry:
512 </p>
514 <div class="doc_code">
515 <pre>
516 [0, Fixed, 4]
517 [0, Array]
518 [0, Char6]
519 </pre>
520 </div>
523 When emitting a record with this abbreviation, the above entry would be emitted
525 </p>
527 <div class="doc_code">
529 <tt>[4<sub>abbrevwidth</sub>, 2<sub>4</sub>, 4<sub>vbr6</sub>, 0<sub>6</sub>,
530 1<sub>6</sub>, 2<sub>6</sub>, 3<sub>6</sub>]</tt>
531 </p>
532 </div>
534 <p>These values are:</p>
536 <ol>
537 <li>The first value, 4, is the abbreviation ID for this abbreviation.</li>
538 <li>The second value, 2, is the record code for <tt>TRIPLE</tt> records within LLVM IR file <tt>MODULE_BLOCK</tt> blocks.</li>
539 <li>The third value, 4, is the length of the array.</li>
540 <li>The rest of the values are the char6 encoded values
541 for <tt>"abcd"</tt>.</li>
542 </ol>
545 With this abbreviation, the triple is emitted with only 37 bits (assuming a
546 abbrev id width of 3). Without the abbreviation, significantly more space would
547 be required to emit the target triple. Also, because the <tt>TRIPLE</tt> value
548 is not emitted as a literal in the abbreviation, the abbreviation can also be
549 used for any other string value.
550 </p>
552 </div>
554 <!-- ======================================================================= -->
555 <div class="doc_subsection"><a name="stdblocks">Standard Blocks</a>
556 </div>
558 <div class="doc_text">
561 In addition to the basic block structure and record encodings, the bitstream
562 also defines specific built-in block types. These block types specify how the
563 stream is to be decoded or other metadata. In the future, new standard blocks
564 may be added. Block IDs 0-7 are reserved for standard blocks.
565 </p>
567 </div>
569 <!-- _______________________________________________________________________ -->
570 <div class="doc_subsubsection"><a name="BLOCKINFO">#0 - BLOCKINFO
571 Block</a></div>
573 <div class="doc_text">
576 The <tt>BLOCKINFO</tt> block allows the description of metadata for other
577 blocks. The currently specified records are:
578 </p>
580 <div class="doc_code">
581 <pre>
582 [SETBID (#1), blockid]
583 [DEFINE_ABBREV, ...]
584 [BLOCKNAME, ...name...]
585 [SETRECORDNAME, RecordID, ...name...]
586 </pre>
587 </div>
590 The <tt>SETBID</tt> record (code 1) indicates which block ID is being
591 described. <tt>SETBID</tt> records can occur multiple times throughout the
592 block to change which block ID is being described. There must be
593 a <tt>SETBID</tt> record prior to any other records.
594 </p>
597 Standard <tt>DEFINE_ABBREV</tt> records can occur inside <tt>BLOCKINFO</tt>
598 blocks, but unlike their occurrence in normal blocks, the abbreviation is
599 defined for blocks matching the block ID we are describing, <i>not</i> the
600 <tt>BLOCKINFO</tt> block itself. The abbreviations defined
601 in <tt>BLOCKINFO</tt> blocks receive abbreviation IDs as described
602 in <tt><a href="#DEFINE_ABBREV">DEFINE_ABBREV</a></tt>.
603 </p>
605 <p>The <tt>BLOCKNAME</tt> record (code 2) can optionally occur in this block. The elements of
606 the record are the bytes of the string name of the block. llvm-bcanalyzer can use
607 this to dump out bitcode files symbolically.</p>
609 <p>The <tt>SETRECORDNAME</tt> record (code 3) can also optionally occur in this block. The
610 first operand value is a record ID number, and the rest of the elements of the record are
611 the bytes for the string name of the record. llvm-bcanalyzer can use
612 this to dump out bitcode files symbolically.</p>
615 Note that although the data in <tt>BLOCKINFO</tt> blocks is described as
616 "metadata," the abbreviations they contain are essential for parsing records
617 from the corresponding blocks. It is not safe to skip them.
618 </p>
620 </div>
622 <!-- *********************************************************************** -->
623 <div class="doc_section"> <a name="wrapper">Bitcode Wrapper Format</a></div>
624 <!-- *********************************************************************** -->
626 <div class="doc_text">
629 Bitcode files for LLVM IR may optionally be wrapped in a simple wrapper
630 structure. This structure contains a simple header that indicates the offset
631 and size of the embedded BC file. This allows additional information to be
632 stored alongside the BC file. The structure of this file header is:
633 </p>
635 <div class="doc_code">
637 <tt>[Magic<sub>32</sub>, Version<sub>32</sub>, Offset<sub>32</sub>,
638 Size<sub>32</sub>, CPUType<sub>32</sub>]</tt>
639 </p>
640 </div>
643 Each of the fields are 32-bit fields stored in little endian form (as with
644 the rest of the bitcode file fields). The Magic number is always
645 <tt>0x0B17C0DE</tt> and the version is currently always <tt>0</tt>. The Offset
646 field is the offset in bytes to the start of the bitcode stream in the file, and
647 the Size field is the size in bytes of the stream. CPUType is a target-specific
648 value that can be used to encode the CPU of the target.
649 </p>
651 </div>
653 <!-- *********************************************************************** -->
654 <div class="doc_section"> <a name="llvmir">LLVM IR Encoding</a></div>
655 <!-- *********************************************************************** -->
657 <div class="doc_text">
660 LLVM IR is encoded into a bitstream by defining blocks and records. It uses
661 blocks for things like constant pools, functions, symbol tables, etc. It uses
662 records for things like instructions, global variable descriptors, type
663 descriptions, etc. This document does not describe the set of abbreviations
664 that the writer uses, as these are fully self-described in the file, and the
665 reader is not allowed to build in any knowledge of this.
666 </p>
668 </div>
670 <!-- ======================================================================= -->
671 <div class="doc_subsection"><a name="basics">Basics</a>
672 </div>
674 <!-- _______________________________________________________________________ -->
675 <div class="doc_subsubsection"><a name="ir_magic">LLVM IR Magic Number</a></div>
677 <div class="doc_text">
680 The magic number for LLVM IR files is:
681 </p>
683 <div class="doc_code">
685 <tt>[0x0<sub>4</sub>, 0xC<sub>4</sub>, 0xE<sub>4</sub>, 0xD<sub>4</sub>]</tt>
686 </p>
687 </div>
690 When combined with the bitcode magic number and viewed as bytes, this is
691 <tt>"BC&nbsp;0xC0DE"</tt>.
692 </p>
694 </div>
696 <!-- _______________________________________________________________________ -->
697 <div class="doc_subsubsection"><a name="ir_signed_vbr">Signed VBRs</a></div>
699 <div class="doc_text">
702 <a href="#variablewidth">Variable Width Integer</a> encoding is an efficient way to
703 encode arbitrary sized unsigned values, but is an extremely inefficient for
704 encoding signed values, as signed values are otherwise treated as maximally large
705 unsigned values.
706 </p>
709 As such, signed VBR values of a specific width are emitted as follows:
710 </p>
712 <ul>
713 <li>Positive values are emitted as VBRs of the specified width, but with their
714 value shifted left by one.</li>
715 <li>Negative values are emitted as VBRs of the specified width, but the negated
716 value is shifted left by one, and the low bit is set.</li>
717 </ul>
720 With this encoding, small positive and small negative values can both
721 be emitted efficiently. Signed VBR encoding is used in
722 <tt>CST_CODE_INTEGER</tt> and <tt>CST_CODE_WIDE_INTEGER</tt> records
723 within <tt>CONSTANTS_BLOCK</tt> blocks.
724 </p>
726 </div>
729 <!-- _______________________________________________________________________ -->
730 <div class="doc_subsubsection"><a name="ir_blocks">LLVM IR Blocks</a></div>
732 <div class="doc_text">
735 LLVM IR is defined with the following blocks:
736 </p>
738 <ul>
739 <li>8 &mdash; <a href="#MODULE_BLOCK"><tt>MODULE_BLOCK</tt></a> &mdash; This is the top-level block that
740 contains the entire module, and describes a variety of per-module
741 information.</li>
742 <li>9 &mdash; <a href="#PARAMATTR_BLOCK"><tt>PARAMATTR_BLOCK</tt></a> &mdash; This enumerates the parameter
743 attributes.</li>
744 <li>10 &mdash; <a href="#TYPE_BLOCK"><tt>TYPE_BLOCK</tt></a> &mdash; This describes all of the types in
745 the module.</li>
746 <li>11 &mdash; <a href="#CONSTANTS_BLOCK"><tt>CONSTANTS_BLOCK</tt></a> &mdash; This describes constants for a
747 module or function.</li>
748 <li>12 &mdash; <a href="#FUNCTION_BLOCK"><tt>FUNCTION_BLOCK</tt></a> &mdash; This describes a function
749 body.</li>
750 <li>13 &mdash; <a href="#TYPE_SYMTAB_BLOCK"><tt>TYPE_SYMTAB_BLOCK</tt></a> &mdash; This describes the type symbol
751 table.</li>
752 <li>14 &mdash; <a href="#VALUE_SYMTAB_BLOCK"><tt>VALUE_SYMTAB_BLOCK</tt></a> &mdash; This describes a value symbol
753 table.</li>
754 <li>15 &mdash; <a href="#METADATA_BLOCK"><tt>METADATA_BLOCK</tt></a> &mdash; This describes metadata items.</li>
755 <li>16 &mdash; <a href="#METADATA_ATTACHMENT"><tt>METADATA_ATTACHMENT</tt></a> &mdash; This contains records associating metadata with function instruction values.</li>
756 </ul>
758 </div>
760 <!-- ======================================================================= -->
761 <div class="doc_subsection"><a name="MODULE_BLOCK">MODULE_BLOCK Contents</a>
762 </div>
764 <div class="doc_text">
766 <p>The <tt>MODULE_BLOCK</tt> block (id 8) is the top-level block for LLVM
767 bitcode files, and each bitcode file must contain exactly one. In
768 addition to records (described below) containing information
769 about the module, a <tt>MODULE_BLOCK</tt> block may contain the
770 following sub-blocks:
771 </p>
773 <ul>
774 <li><a href="#BLOCKINFO"><tt>BLOCKINFO</tt></a></li>
775 <li><a href="#PARAMATTR_BLOCK"><tt>PARAMATTR_BLOCK</tt></a></li>
776 <li><a href="#TYPE_BLOCK"><tt>TYPE_BLOCK</tt></a></li>
777 <li><a href="#TYPE_SYMTAB_BLOCK"><tt>TYPE_SYMTAB_BLOCK</tt></a></li>
778 <li><a href="#VALUE_SYMTAB_BLOCK"><tt>VALUE_SYMTAB_BLOCK</tt></a></li>
779 <li><a href="#CONSTANTS_BLOCK"><tt>CONSTANTS_BLOCK</tt></a></li>
780 <li><a href="#FUNCTION_BLOCK"><tt>FUNCTION_BLOCK</tt></a></li>
781 <li><a href="#METADATA_BLOCK"><tt>METADATA_BLOCK</tt></a></li>
782 </ul>
784 </div>
786 <!-- _______________________________________________________________________ -->
787 <div class="doc_subsubsection"><a name="MODULE_CODE_VERSION">MODULE_CODE_VERSION Record</a>
788 </div>
790 <div class="doc_text">
792 <p><tt>[VERSION, version#]</tt></p>
794 <p>The <tt>VERSION</tt> record (code 1) contains a single value
795 indicating the format version. Only version 0 is supported at this
796 time.</p>
797 </div>
799 <!-- _______________________________________________________________________ -->
800 <div class="doc_subsubsection"><a name="MODULE_CODE_TRIPLE">MODULE_CODE_TRIPLE Record</a>
801 </div>
803 <div class="doc_text">
804 <p><tt>[TRIPLE, ...string...]</tt></p>
806 <p>The <tt>TRIPLE</tt> record (code 2) contains a variable number of
807 values representing the bytes of the <tt>target triple</tt>
808 specification string.</p>
809 </div>
811 <!-- _______________________________________________________________________ -->
812 <div class="doc_subsubsection"><a name="MODULE_CODE_DATALAYOUT">MODULE_CODE_DATALAYOUT Record</a>
813 </div>
815 <div class="doc_text">
816 <p><tt>[DATALAYOUT, ...string...]</tt></p>
818 <p>The <tt>DATALAYOUT</tt> record (code 3) contains a variable number of
819 values representing the bytes of the <tt>target datalayout</tt>
820 specification string.</p>
821 </div>
823 <!-- _______________________________________________________________________ -->
824 <div class="doc_subsubsection"><a name="MODULE_CODE_ASM">MODULE_CODE_ASM Record</a>
825 </div>
827 <div class="doc_text">
828 <p><tt>[ASM, ...string...]</tt></p>
830 <p>The <tt>ASM</tt> record (code 4) contains a variable number of
831 values representing the bytes of <tt>module asm</tt> strings, with
832 individual assembly blocks separated by newline (ASCII 10) characters.</p>
833 </div>
835 <!-- _______________________________________________________________________ -->
836 <div class="doc_subsubsection"><a name="MODULE_CODE_SECTIONNAME">MODULE_CODE_SECTIONNAME Record</a>
837 </div>
839 <div class="doc_text">
840 <p><tt>[SECTIONNAME, ...string...]</tt></p>
842 <p>The <tt>SECTIONNAME</tt> record (code 5) contains a variable number
843 of values representing the bytes of a single section name
844 string. There should be one <tt>SECTIONNAME</tt> record for each
845 section name referenced (e.g., in global variable or function
846 <tt>section</tt> attributes) within the module. These records can be
847 referenced by the 1-based index in the <i>section</i> fields of
848 <tt>GLOBALVAR</tt> or <tt>FUNCTION</tt> records.</p>
849 </div>
851 <!-- _______________________________________________________________________ -->
852 <div class="doc_subsubsection"><a name="MODULE_CODE_DEPLIB">MODULE_CODE_DEPLIB Record</a>
853 </div>
855 <div class="doc_text">
856 <p><tt>[DEPLIB, ...string...]</tt></p>
858 <p>The <tt>DEPLIB</tt> record (code 6) contains a variable number of
859 values representing the bytes of a single dependent library name
860 string, one of the libraries mentioned in a <tt>deplibs</tt>
861 declaration. There should be one <tt>DEPLIB</tt> record for each
862 library name referenced.</p>
863 </div>
865 <!-- _______________________________________________________________________ -->
866 <div class="doc_subsubsection"><a name="MODULE_CODE_GLOBALVAR">MODULE_CODE_GLOBALVAR Record</a>
867 </div>
869 <div class="doc_text">
870 <p><tt>[GLOBALVAR, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal]</tt></p>
872 <p>The <tt>GLOBALVAR</tt> record (code 7) marks the declaration or
873 definition of a global variable. The operand fields are:</p>
875 <ul>
876 <li><i>pointer type</i>: The type index of the pointer type used to point to
877 this global variable</li>
879 <li><i>isconst</i>: Non-zero if the variable is treated as constant within
880 the module, or zero if it is not</li>
882 <li><i>initid</i>: If non-zero, the value index of the initializer for this
883 variable, plus 1.</li>
885 <li><a name="linkage"><i>linkage</i></a>: An encoding of the linkage
886 type for this variable:
887 <ul>
888 <li><tt>external</tt>: code 0</li>
889 <li><tt>weak</tt>: code 1</li>
890 <li><tt>appending</tt>: code 2</li>
891 <li><tt>internal</tt>: code 3</li>
892 <li><tt>linkonce</tt>: code 4</li>
893 <li><tt>dllimport</tt>: code 5</li>
894 <li><tt>dllexport</tt>: code 6</li>
895 <li><tt>extern_weak</tt>: code 7</li>
896 <li><tt>common</tt>: code 8</li>
897 <li><tt>private</tt>: code 9</li>
898 <li><tt>weak_odr</tt>: code 10</li>
899 <li><tt>linkonce_odr</tt>: code 11</li>
900 <li><tt>available_externally</tt>: code 12</li>
901 <li><tt>linker_private</tt>: code 13</li>
902 </ul>
903 </li>
905 <li><i>alignment</i>: The logarithm base 2 of the variable's requested
906 alignment, plus 1</li>
908 <li><i>section</i>: If non-zero, the 1-based section index in the
909 table of <a href="#MODULE_CODE_SECTIONNAME">MODULE_CODE_SECTIONNAME</a>
910 entries.</li>
912 <li><a name="visibility"><i>visibility</i></a>: If present, an
913 encoding of the visibility of this variable:
914 <ul>
915 <li><tt>default</tt>: code 0</li>
916 <li><tt>hidden</tt>: code 1</li>
917 <li><tt>protected</tt>: code 2</li>
918 </ul>
919 </li>
921 <li><i>threadlocal</i>: If present and non-zero, indicates that the variable
922 is <tt>thread_local</tt></li>
924 </ul>
925 </div>
927 <!-- _______________________________________________________________________ -->
928 <div class="doc_subsubsection"><a name="MODULE_CODE_FUNCTION">MODULE_CODE_FUNCTION Record</a>
929 </div>
931 <div class="doc_text">
933 <p><tt>[FUNCTION, type, callingconv, isproto, linkage, paramattr, alignment, section, visibility, gc]</tt></p>
935 <p>The <tt>FUNCTION</tt> record (code 8) marks the declaration or
936 definition of a function. The operand fields are:</p>
938 <ul>
939 <li><i>type</i>: The type index of the function type describing this function</li>
941 <li><i>callingconv</i>: The calling convention number:
942 <ul>
943 <li><tt>ccc</tt>: code 0</li>
944 <li><tt>fastcc</tt>: code 8</li>
945 <li><tt>coldcc</tt>: code 9</li>
946 <li><tt>x86_stdcallcc</tt>: code 64</li>
947 <li><tt>x86_fastcallcc</tt>: code 65</li>
948 <li><tt>arm_apcscc</tt>: code 66</li>
949 <li><tt>arm_aapcscc</tt>: code 67</li>
950 <li><tt>arm_aapcs_vfpcc</tt>: code 68</li>
951 </ul>
952 </li>
954 <li><i>isproto</i>: Non-zero if this entry represents a declaration
955 rather than a definition</li>
957 <li><i>linkage</i>: An encoding of the <a href="#linkage">linkage type</a>
958 for this function</li>
960 <li><i>paramattr</i>: If nonzero, the 1-based parameter attribute index
961 into the table of <a href="#PARAMATTR_CODE_ENTRY">PARAMATTR_CODE_ENTRY</a>
962 entries.</li>
964 <li><i>alignment</i>: The logarithm base 2 of the function's requested
965 alignment, plus 1</li>
967 <li><i>section</i>: If non-zero, the 1-based section index in the
968 table of <a href="#MODULE_CODE_SECTIONNAME">MODULE_CODE_SECTIONNAME</a>
969 entries.</li>
971 <li><i>visibility</i>: An encoding of the <a href="#visibility">visibility</a>
972 of this function</li>
974 <li><i>gc</i>: If present and nonzero, the 1-based garbage collector
975 index in the table of
976 <a href="#MODULE_CODE_GCNAME">MODULE_CODE_GCNAME</a> entries.</li>
977 </ul>
978 </div>
980 <!-- _______________________________________________________________________ -->
981 <div class="doc_subsubsection"><a name="MODULE_CODE_ALIAS">MODULE_CODE_ALIAS Record</a>
982 </div>
984 <div class="doc_text">
986 <p><tt>[ALIAS, alias type, aliasee val#, linkage, visibility]</tt></p>
988 <p>The <tt>ALIAS</tt> record (code 9) marks the definition of an
989 alias. The operand fields are</p>
991 <ul>
992 <li><i>alias type</i>: The type index of the alias</li>
994 <li><i>aliasee val#</i>: The value index of the aliased value</li>
996 <li><i>linkage</i>: An encoding of the <a href="#linkage">linkage type</a>
997 for this alias</li>
999 <li><i>visibility</i>: If present, an encoding of the
1000 <a href="#visibility">visibility</a> of the alias</li>
1002 </ul>
1003 </div>
1005 <!-- _______________________________________________________________________ -->
1006 <div class="doc_subsubsection"><a name="MODULE_CODE_PURGEVALS">MODULE_CODE_PURGEVALS Record</a>
1007 </div>
1009 <div class="doc_text">
1010 <p><tt>[PURGEVALS, numvals]</tt></p>
1012 <p>The <tt>PURGEVALS</tt> record (code 10) resets the module-level
1013 value list to the size given by the single operand value. Module-level
1014 value list items are added by <tt>GLOBALVAR</tt>, <tt>FUNCTION</tt>,
1015 and <tt>ALIAS</tt> records. After a <tt>PURGEVALS</tt> record is seen,
1016 new value indices will start from the given <i>numvals</i> value.</p>
1017 </div>
1019 <!-- _______________________________________________________________________ -->
1020 <div class="doc_subsubsection"><a name="MODULE_CODE_GCNAME">MODULE_CODE_GCNAME Record</a>
1021 </div>
1023 <div class="doc_text">
1024 <p><tt>[GCNAME, ...string...]</tt></p>
1026 <p>The <tt>GCNAME</tt> record (code 11) contains a variable number of
1027 values representing the bytes of a single garbage collector name
1028 string. There should be one <tt>GCNAME</tt> record for each garbage
1029 collector name referenced in function <tt>gc</tt> attributes within
1030 the module. These records can be referenced by 1-based index in the <i>gc</i>
1031 fields of <tt>FUNCTION</tt> records.</p>
1032 </div>
1034 <!-- ======================================================================= -->
1035 <div class="doc_subsection"><a name="PARAMATTR_BLOCK">PARAMATTR_BLOCK Contents</a>
1036 </div>
1038 <div class="doc_text">
1040 <p>The <tt>PARAMATTR_BLOCK</tt> block (id 9) ...
1041 </p>
1043 </div>
1046 <!-- _______________________________________________________________________ -->
1047 <div class="doc_subsubsection"><a name="PARAMATTR_CODE_ENTRY">PARAMATTR_CODE_ENTRY Record</a>
1048 </div>
1050 <div class="doc_text">
1052 <p><tt>[ENTRY, paramidx0, attr0, paramidx1, attr1...]</tt></p>
1054 <p>The <tt>ENTRY</tt> record (code 1) ...
1055 </p>
1056 </div>
1058 <!-- ======================================================================= -->
1059 <div class="doc_subsection"><a name="TYPE_BLOCK">TYPE_BLOCK Contents</a>
1060 </div>
1062 <div class="doc_text">
1064 <p>The <tt>TYPE_BLOCK</tt> block (id 10) ...
1065 </p>
1067 </div>
1070 <!-- ======================================================================= -->
1071 <div class="doc_subsection"><a name="CONSTANTS_BLOCK">CONSTANTS_BLOCK Contents</a>
1072 </div>
1074 <div class="doc_text">
1076 <p>The <tt>CONSTANTS_BLOCK</tt> block (id 11) ...
1077 </p>
1079 </div>
1082 <!-- ======================================================================= -->
1083 <div class="doc_subsection"><a name="FUNCTION_BLOCK">FUNCTION_BLOCK Contents</a>
1084 </div>
1086 <div class="doc_text">
1088 <p>The <tt>FUNCTION_BLOCK</tt> block (id 12) ...
1089 </p>
1091 <p>In addition to the record types described below, a
1092 <tt>FUNCTION_BLOCK</tt> block may contain the following sub-blocks:
1093 </p>
1095 <ul>
1096 <li><a href="#CONSTANTS_BLOCK"><tt>CONSTANTS_BLOCK</tt></a></li>
1097 <li><a href="#VALUE_SYMTAB_BLOCK"><tt>VALUE_SYMTAB_BLOCK</tt></a></li>
1098 <li><a href="#METADATA_ATTACHMENT"><tt>METADATA_ATTACHMENT</tt></a></li>
1099 </ul>
1101 </div>
1104 <!-- ======================================================================= -->
1105 <div class="doc_subsection"><a name="TYPE_SYMTAB_BLOCK">TYPE_SYMTAB_BLOCK Contents</a>
1106 </div>
1108 <div class="doc_text">
1110 <p>The <tt>TYPE_SYMTAB_BLOCK</tt> block (id 13) ...
1111 </p>
1113 </div>
1116 <!-- ======================================================================= -->
1117 <div class="doc_subsection"><a name="VALUE_SYMTAB_BLOCK">VALUE_SYMTAB_BLOCK Contents</a>
1118 </div>
1120 <div class="doc_text">
1122 <p>The <tt>VALUE_SYMTAB_BLOCK</tt> block (id 14) ...
1123 </p>
1125 </div>
1128 <!-- ======================================================================= -->
1129 <div class="doc_subsection"><a name="METADATA_BLOCK">METADATA_BLOCK Contents</a>
1130 </div>
1132 <div class="doc_text">
1134 <p>The <tt>METADATA_BLOCK</tt> block (id 15) ...
1135 </p>
1137 </div>
1140 <!-- ======================================================================= -->
1141 <div class="doc_subsection"><a name="METADATA_ATTACHMENT">METADATA_ATTACHMENT Contents</a>
1142 </div>
1144 <div class="doc_text">
1146 <p>The <tt>METADATA_ATTACHMENT</tt> block (id 16) ...
1147 </p>
1149 </div>
1152 <!-- *********************************************************************** -->
1153 <hr>
1154 <address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1155 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
1156 <a href="http://validator.w3.org/check/referer"><img
1157 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
1158 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1159 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1160 Last modified: $Date$
1161 </address>
1162 </body>
1163 </html>