note where to get steens-aa and ds-aa
[oota-llvm.git] / docs / BitCodeFormat.html
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="#llvmir">LLVM IR Encoding</a>
26     <ol>
27     <li><a href="#basics">Basics</a></li>
28     </ol>
29   </li>
30 </ol>
31 <div class="doc_author">
32   <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>.
33 </p>
34 </div>
35
36 <!-- *********************************************************************** -->
37 <div class="doc_section"> <a name="abstract">Abstract</a></div>
38 <!-- *********************************************************************** -->
39
40 <div class="doc_text">
41
42 <p>This document describes the LLVM bitstream file format and the encoding of
43 the LLVM IR into it.</p>
44
45 </div>
46
47 <!-- *********************************************************************** -->
48 <div class="doc_section"> <a name="overview">Overview</a></div>
49 <!-- *********************************************************************** -->
50
51 <div class="doc_text">
52
53 <p>
54 What is commonly known as the LLVM bitcode file format (also, sometimes
55 anachronistically known as bytecode) is actually two things: a <a 
56 href="#bitstream">bitstream container format</a>
57 and an <a href="#llvmir">encoding of LLVM IR</a> into the container format.</p>
58
59 <p>
60 The bitstream format is an abstract encoding of structured data, very
61 similar to XML in some ways.  Like XML, bitstream files contain tags, and nested
62 structures, and you can parse the file without having to understand the tags.
63 Unlike XML, the bitstream format is a binary encoding, and unlike XML it
64 provides a mechanism for the file to self-describe "abbreviations", which are
65 effectively size optimizations for the content.</p>
66
67 <p>This document first describes the LLVM bitstream format, then describes the
68 record structure used by LLVM IR files.
69 </p>
70
71 </div>
72
73 <!-- *********************************************************************** -->
74 <div class="doc_section"> <a name="bitstream">Bitstream Format</a></div>
75 <!-- *********************************************************************** -->
76
77 <div class="doc_text">
78
79 <p>
80 The bitstream format is literally a stream of bits, with a very simple
81 structure.  This structure consists of the following concepts:
82 </p>
83
84 <ul>
85 <li>A "<a href="#magic">magic number</a>" that identifies the contents of
86     the stream.</li>
87 <li>Encoding <a href="#primitives">primitives</a> like variable bit-rate
88     integers.</li> 
89 <li><a href="#blocks">Blocks</a>, which define nested content.</li> 
90 <li><a href="#datarecord">Data Records</a>, which describe entities within the
91     file.</li> 
92 <li>Abbreviations, which specify compression optimizations for the file.</li> 
93 </ul>
94
95 <p>Note that the <a 
96 href="CommandGuide/html/llvm-bcanalyzer.html">llvm-bcanalyzer</a> tool can be
97 used to dump and inspect arbitrary bitstreams, which is very useful for
98 understanding the encoding.</p>
99
100 </div>
101
102 <!-- ======================================================================= -->
103 <div class="doc_subsection"><a name="magic">Magic Numbers</a>
104 </div>
105
106 <div class="doc_text">
107
108 <p>The first four bytes of the stream identify the encoding of the file.  This
109 is used by a reader to know what is contained in the file.</p>
110
111 </div>
112
113 <!-- ======================================================================= -->
114 <div class="doc_subsection"><a name="primitives">Primitives</a>
115 </div>
116
117 <div class="doc_text">
118
119 <p>
120 A bitstream literally consists of a stream of bits.  This stream is made up of a
121 number of primitive values that encode a stream of unsigned integer values.
122 These
123 integers are are encoded in two ways: either as <a href="#fixedwidth">Fixed
124 Width Integers</a> or as <a href="#variablewidth">Variable Width
125 Integers</a>.
126 </p>
127
128 </div>
129
130 <!-- _______________________________________________________________________ -->
131 <div class="doc_subsubsection"> <a name="fixedwidth">Fixed Width Integers</a>
132 </div>
133
134 <div class="doc_text">
135
136 <p>Fixed-width integer values have their low bits emitted directly to the file.
137    For example, a 3-bit integer value encodes 1 as 001.  Fixed width integers
138    are used when there are a well-known number of options for a field.  For
139    example, boolean values are usually encoded with a 1-bit wide integer. 
140 </p>
141
142 </div>
143
144 <!-- _______________________________________________________________________ -->
145 <div class="doc_subsubsection"> <a name="variablewidth">Variable Width
146 Integers</a></div>
147
148 <div class="doc_text">
149
150 <p>Variable-width integer (VBR) values encode values of arbitrary size,
151 optimizing for the case where the values are small.  Given a 4-bit VBR field,
152 any 3-bit value (0 through 7) is encoded directly, with the high bit set to
153 zero.  Values larger than N-1 bits emit their bits in a series of N-1 bit
154 chunks, where all but the last set the high bit.</p>
155
156 <p>For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a
157 vbr4 value.  The first set of four bits indicates the value 3 (011) with a
158 continuation piece (indicated by a high bit of 1).  The next word indicates a
159 value of 24 (011 << 3) with no continuation.  The sum (3+24) yields the value
160 27.
161 </p>
162
163 </div>
164
165 <!-- _______________________________________________________________________ -->
166 <div class="doc_subsubsection"> <a name="char6">6-bit characters</a></div>
167
168 <div class="doc_text">
169
170 <p>6-bit characters encode common characters into a fixed 6-bit field.  They
171 represent the following characters with the following 6-bit values:</p>
172
173 <ul>
174 <li>'a' .. 'z' - 0 .. 25</li>
175 <li>'A' .. 'Z' - 26 .. 52</li>
176 <li>'0' .. '9' - 53 .. 61</li>
177 <li>'.' - 62</li>
178 <li>'_' - 63</li>
179 </ul>
180
181 <p>This encoding is only suitable for encoding characters and strings that
182 consist only of the above characters.  It is completely incapable of encoding
183 characters not in the set.</p>
184
185 </div>
186
187 <!-- _______________________________________________________________________ -->
188 <div class="doc_subsubsection"> <a name="wordalign">Word Alignment</a></div>
189
190 <div class="doc_text">
191
192 <p>Occasionally, it is useful to emit zero bits until the bitstream is a
193 multiple of 32 bits.  This ensures that the bit position in the stream can be
194 represented as a multiple of 32-bit words.</p>
195
196 </div>
197
198
199 <!-- ======================================================================= -->
200 <div class="doc_subsection"><a name="abbrevid">Abbreviation IDs</a>
201 </div>
202
203 <div class="doc_text">
204
205 <p>
206 A bitstream is a sequential series of <a href="#blocks">Blocks</a> and
207 <a href="#datarecord">Data Records</a>.  Both of these start with an
208 abbreviation ID encoded as a fixed-bitwidth field.  The width is specified by
209 the current block, as described below.  The value of the abbreviation ID
210 specifies either a builtin ID (which have special meanings, defined below) or
211 one of the abbreviation IDs defined by the stream itself.
212 </p>
213
214 <p>
215 The set of builtin abbrev IDs is:
216 </p>
217
218 <ul>
219 <li>0 - <a href="#END_BLOCK">END_BLOCK</a> - This abbrev ID marks the end of the
220     current block.</li>
221 <li>1 - <a href="#ENTER_SUBBLOCK">ENTER_SUBBLOCK</a> - This abbrev ID marks the
222     beginning of a new block.</li>
223 <li>2 - <a href="#DEFINE_ABBREV">DEFINE_ABBREV</a> - This defines a new
224     abbreviation.</li>
225 <li>3 - <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a> - This ID specifies the
226     definition of an unabbreviated record.</li>
227 </ul>
228
229 <p>Abbreviation IDs 4 and above are defined by the stream itself, and specify
230 an <a href="#abbrev_records">abbreviated record encoding</a>.</p>
231
232 </div>
233
234 <!-- ======================================================================= -->
235 <div class="doc_subsection"><a name="blocks">Blocks</a>
236 </div>
237
238 <div class="doc_text">
239
240 <p>
241 Blocks in a bitstream denote nested regions of the stream, and are identified by
242 a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
243 function bodies).  Nested blocks capture the hierachical structure of the data
244 encoded in it, and various properties are associated with blocks as the file is
245 parsed.  Block definitions allow the reader to efficiently skip blocks
246 in constant time if the reader wants a summary of blocks, or if it wants to
247 efficiently skip data they do not understand.  The LLVM IR reader uses this
248 mechanism to skip function bodies, lazily reading them on demand.
249 </p>
250
251 <p>
252 When reading and encoding the stream, several properties are maintained for the
253 block.  In particular, each block maintains:
254 </p>
255
256 <ol>
257 <li>A current abbrev id width.  This value starts at 2, and is set every time a
258     block record is entered.  The block entry specifies the abbrev id width for
259     the body of the block.</li>
260
261 <li>A set of abbreviations.  Abbreviations may be defined within a block, or
262     they may be associated with all blocks of a particular ID.
263 </li>
264 </ol>
265
266 <p>As sub blocks are entered, these properties are saved and the new sub-block
267 has its own set of abbreviations, and its own abbrev id width.  When a sub-block
268 is popped, the saved values are restored.</p>
269
270 </div>
271
272 <!-- _______________________________________________________________________ -->
273 <div class="doc_subsubsection"> <a name="ENTER_SUBBLOCK">ENTER_SUBBLOCK
274 Encoding</a></div>
275
276 <div class="doc_text">
277
278 <p><tt>[ENTER_SUBBLOCK, blockid<sub>vbr8</sub>, newabbrevlen<sub>vbr4</sub>,
279      &lt;align32bits&gt;, blocklen<sub>32</sub>]</tt></p>
280
281 <p>
282 The ENTER_SUBBLOCK abbreviation ID specifies the start of a new block record.
283 The <tt>blockid</tt> value is encoded as a 8-bit VBR identifier, and indicates
284 the type of block being entered (which is application specific).  The
285 <tt>newabbrevlen</tt> value is a 4-bit VBR which specifies the
286 abbrev id width for the sub-block.  The <tt>blocklen</tt> is a 32-bit aligned
287 value that specifies the size of the subblock, in 32-bit words.  This value
288 allows the reader to skip over the entire block in one jump.
289 </p>
290
291 </div>
292
293 <!-- _______________________________________________________________________ -->
294 <div class="doc_subsubsection"> <a name="END_BLOCK">END_BLOCK
295 Encoding</a></div>
296
297 <div class="doc_text">
298
299 <p><tt>[END_BLOCK, &lt;align32bits&gt;]</tt></p>
300
301 <p>
302 The END_BLOCK abbreviation ID specifies the end of the current block record.
303 Its end is aligned to 32-bits to ensure that the size of the block is an even
304 multiple of 32-bits.</p>
305
306 </div>
307
308
309
310 <!-- ======================================================================= -->
311 <div class="doc_subsection"><a name="datarecord">Data Records</a>
312 </div>
313
314 <div class="doc_text">
315 <p>
316 Data records consist of a record code and a number of (up to) 64-bit integer
317 values.  The interpretation of the code and values is application specific and
318 there are multiple different ways to encode a record (with an unabbrev record
319 or with an abbreviation).  In the LLVM IR format, for example, there is a record
320 which encodes the target triple of a module.  The code is MODULE_CODE_TRIPLE,
321 and the values of the record are the ascii codes for the characters in the
322 string.</p>
323
324 </div>
325
326 <!-- _______________________________________________________________________ -->
327 <div class="doc_subsubsection"> <a name="UNABBREV_RECORD">UNABBREV_RECORD
328 Encoding</a></div>
329
330 <div class="doc_text">
331
332 <p><tt>[UNABBREV_RECORD, code<sub>vbr6</sub>, numops<sub>vbr6</sub>,
333        op0<sub>vbr6</sub>, op1<sub>vbr6</sub>, ...]</tt></p>
334
335 <p>An UNABBREV_RECORD provides a default fallback encoding, which is both
336 completely general and also extremely inefficient.  It can describe an arbitrary
337 record, by emitting the code and operands as vbrs.</p>
338
339 <p>For example, emitting an LLVM IR target triple as an unabbreviated record
340 requires emitting the UNABBREV_RECORD abbrevid, a vbr6 for the
341 MODULE_CODE_TRIPLE code, a vbr6 for the length of the string (which is equal to
342 the number of operands), and a vbr6 for each character.  Since there are no
343 letters with value less than 32, each letter would need to be emitted as at
344 least a two-part VBR, which means that each letter would require at least 12
345 bits.  This is not an efficient encoding, but it is fully general.</p>
346
347 </div>
348
349 <!-- _______________________________________________________________________ -->
350 <div class="doc_subsubsection"> <a name="abbrev_records">Abbreviated Record
351 Encoding</a></div>
352
353 <div class="doc_text">
354
355 <p><tt>[&lt;abbrevid&gt;, fields...]</tt></p>
356
357 <p>An abbreviated record is a abbreviation id followed by a set of fields that
358 are encoded according to the <a href="#abbreviations">abbreviation 
359 definition</a>.  This allows records to be encoded significantly more densely
360 than records encoded with the <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a>
361 type, and allows the abbreviation types to be specified in the stream itself,
362 which allows the files to be completely self describing.  The actual encoding
363 of abbreviations is defined below.
364 </p>
365
366 </div>
367
368 <!-- ======================================================================= -->
369 <div class="doc_subsection"><a name="abbreviations">Abbreviations</a>
370 </div>
371
372 <div class="doc_text">
373 <p>
374 Abbreviations are an important form of compression for bitstreams.  The idea is
375 to specify a dense encoding for a class of records once, then use that encoding
376 to emit many records.  It takes space to emit the encoding into the file, but
377 the space is recouped (hopefully plus some) when the records that use it are
378 emitted.
379 </p>
380
381 <p>
382 Abbreviations can be determined dynamically per client, per file.  Since the
383 abbreviations are stored in the bitstream itself, different streams of the same
384 format can contain different sets of abbreviations if the specific stream does
385 not need it.  As a concrete example, LLVM IR files usually emit an abbreviation
386 for binary operators.  If a specific LLVM module contained no or few binary
387 operators, the abbreviation does not need to be emitted.
388 </p>
389 </div>
390
391 <!-- _______________________________________________________________________ -->
392 <div class="doc_subsubsection"><a name="DEFINE_ABBREV">DEFINE_ABBREV
393  Encoding</a></div>
394
395 <div class="doc_text">
396
397 <p><tt>[DEFINE_ABBREV, numabbrevops<sub>vbr5</sub>, abbrevop0, abbrevop1,
398  ...]</tt></p>
399
400 <p>An abbreviation definition consists of the DEFINE_ABBREV abbrevid followed
401 by a VBR that specifies the number of abbrev operands, then the abbrev
402 operands themselves.  Abbreviation operands come in three forms.  They all start
403 with a single bit that indicates whether the abbrev operand is a literal operand
404 (when the bit is 1) or an encoding operand (when the bit is 0).</p>
405
406 <ol>
407 <li>Literal operands - <tt>[1<sub>1</sub>, litvalue<sub>vbr8</sub>]</tt> -
408 Literal operands specify that the value in the result
409 is always a single specific value.  This specific value is emitted as a vbr8
410 after the bit indicating that it is a literal operand.</li>
411 <li>Encoding info without data - <tt>[0<sub>1</sub>, encoding<sub>3</sub>]</tt>
412  - Operand encodings that do not have extra data are just emitted as their code.
413 </li>
414 <li>Encoding info with data - <tt>[0<sub>1</sub>, encoding<sub>3</sub>, 
415 value<sub>vbr5</sub>]</tt> - Operand encodings that do have extra data are
416 emitted as their code, followed by the extra data.
417 </li>
418 </ol>
419
420 <p>The possible operand encodings are:</p>
421
422 <ul>
423 <li>1 - Fixed - The field should be emitted as a <a 
424     href="#fixedwidth">fixed-width value</a>, whose width
425     is specified by the encoding operand.</li>
426 <li>2 - VBR - The field should be emitted as a <a 
427     href="#variablewidth">variable-width value</a>, whose width
428     is specified by the encoding operand.</li>
429 <li>3 - Array - This field is an array of values.  The element type of the array
430     is specified by the next encoding operand.</li>
431 <li>4 - Char6 - This field should be emitted as a <a href="#char6">char6-encoded
432     value</a>.</li>
433 </ul>
434
435 <p>For example, target triples in LLVM modules are encoded as a record of the
436 form <tt>[TRIPLE, 'a', 'b', 'c', 'd']</tt>.  Consider if the bitstream emitted
437 the following abbrev entry:</p>
438
439 <ul>
440 <li><tt>[0, Fixed, 4]</tt></li>
441 <li><tt>[0, Array]</tt></li>
442 <li><tt>[0, Char6]</tt></li>
443 </ul>
444
445 <p>When emitting a record with this abbreviation, the above entry would be
446 emitted as:</p>
447
448 <p><tt>[4<sub>abbrevwidth</sub>, 2<sub>4</sub>, 4<sub>vbr6</sub>,
449    0<sub>6</sub>, 1<sub>6</sub>, 2<sub>6</sub>, 3<sub>6</sub>]</tt></p>
450
451 <p>These values are:</p>
452
453 <ol>
454 <li>The first value, 4, is the abbreviation ID for this abbreviation.</li>
455 <li>The second value, 2, is the code for TRIPLE in LLVM IR files.</li>
456 <li>The third value, 4, is the length of the array.</li>
457 <li>The rest of the values are the char6 encoded values for "abcd".</li>
458 </ol>
459
460 <p>With this abbreviation, the triple is emitted with only 37 bits (assuming a
461 abbrev id width of 3).  Without the abbreviation, significantly more space would
462 be required to emit the target triple.  Also, since the TRIPLE value is not
463 emitted as a literal in the abbreviation, the abbreviation can also be used for
464 any other string value.
465 </p>
466
467 </div>
468
469 <!-- ======================================================================= -->
470 <div class="doc_subsection"><a name="stdblocks">Standard Blocks</a>
471 </div>
472
473 <div class="doc_text">
474
475 <p>
476 In addition to the basic block structure and record encodings, the bitstream
477 also defines specific builtin block types.  These block types specify how the
478 stream is to be decoded or other metadata.  In the future, new standard blocks
479 may be added.
480 </p>
481
482 </div>
483
484 <!-- _______________________________________________________________________ -->
485 <div class="doc_subsubsection"><a name="BLOCKINFO">#0 - BLOCKINFO
486 Block</a></div>
487
488 <div class="doc_text">
489
490 <p>The BLOCKINFO block allows the description of metadata for other blocks.  The
491   currently specified records are:</p>
492  
493 <ul>
494 <li><tt>[SETBID (#1), blockid]</tt></li>
495 <li><tt>[DEFINE_ABBREV, ...]</tt></li>
496 </ul>
497
498 <p>
499 The SETBID record indicates which block ID is being described.  The standard
500 DEFINE_ABBREV record specifies an abbreviation.  The abbreviation is associated
501 with the record ID, and any records with matching ID automatically get the
502 abbreviation. 
503 </p>
504
505 </div>
506
507 <!-- *********************************************************************** -->
508 <div class="doc_section"> <a name="llvmir">LLVM IR Encoding</a></div>
509 <!-- *********************************************************************** -->
510
511 <div class="doc_text">
512
513 <p>LLVM IR is encoded into a bitstream by defining blocks and records.  It uses
514 blocks for things like constant pools, functions, symbol tables, etc.  It uses
515 records for things like instructions, global variable descriptors, type
516 descriptions, etc.  This document does not describe the set of abbreviations
517 that the writer uses, as these are fully self-described in the file, and the
518 reader is not allowed to build in any knowledge of this.</p>
519
520 </div>
521
522 <!-- ======================================================================= -->
523 <div class="doc_subsection"><a name="basics">Basics</a>
524 </div>
525
526 <!-- _______________________________________________________________________ -->
527 <div class="doc_subsubsection"><a name="ir_magic">LLVM IR Magic Number</a></div>
528
529 <div class="doc_text">
530
531 <p>
532 The magic number for LLVM IR files is:
533 </p>
534
535 <p><tt>['B'<sub>8</sub>, 'C'<sub>8</sub>, 0x0<sub>4</sub>, 0xC<sub>4</sub>,
536 0xE<sub>4</sub>, 0xD<sub>4</sub>]</tt></p>
537
538 <p>When viewed as bytes, this is "BC 0xC0DE".</p>
539
540 </div>
541
542 <!-- _______________________________________________________________________ -->
543 <div class="doc_subsubsection"><a name="ir_signed_vbr">Signed VBRs</a></div>
544
545 <div class="doc_text">
546
547 <p>
548 <a href="#variablewidth">Variable Width Integers</a> are an efficient way to
549 encode arbitrary sized unsigned values, but is an extremely inefficient way to
550 encode signed values (as signed values are otherwise treated as maximally large
551 unsigned values).</p>
552
553 <p>As such, signed vbr values of a specific width are emitted as follows:</p>
554
555 <ul>
556 <li>Positive values are emitted as vbrs of the specified width, but with their
557     value shifted left by one.</li>
558 <li>Negative values are emitted as vbrs of the specified width, but the negated
559     value is shifted left by one, and the low bit is set.</li>
560 </ul>
561
562 <p>With this encoding, small positive and small negative values can both be
563 emitted efficiently.</p>
564
565 </div>
566
567
568 <!-- _______________________________________________________________________ -->
569 <div class="doc_subsubsection"><a name="ir_blocks">LLVM IR Blocks</a></div>
570
571 <div class="doc_text">
572
573 <p>
574 LLVM IR is defined with the following blocks:
575 </p>
576
577 <ul>
578 <li>8  - MODULE_BLOCK - This is the top-level block that contains the
579     entire module, and describes a variety of per-module information.</li>
580 <li>9  - PARAMATTR_BLOCK - This enumerates the parameter attributes.</li>
581 <li>10 - TYPE_BLOCK - This describes all of the types in the module.</li>
582 <li>11 - CONSTANTS_BLOCK - This describes constants for a module or
583     function.</li>
584 <li>12 - FUNCTION_BLOCK - This describes a function body.</li>
585 <li>13 - TYPE_SYMTAB_BLOCK - This describes the type symbol table.</li>
586 <li>14 - VALUE_SYMTAB_BLOCK - This describes a value symbol table.</li>
587 </ul>
588
589 </div>
590
591 <!-- ======================================================================= -->
592 <div class="doc_subsection"><a name="MODULE_BLOCK">MODULE_BLOCK Contents</a>
593 </div>
594
595 <div class="doc_text">
596
597 <p>
598 </p>
599
600 </div>
601
602
603 <!-- *********************************************************************** -->
604 <hr>
605 <address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
606  src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
607 <a href="http://validator.w3.org/check/referer"><img
608  src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
609  <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
610 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
611 Last modified: $Date$
612 </address>
613 </body>
614 </html>