blob: 243c1cfbc93253ee75d4735b1073445d48f71476 [file] [log] [blame]
Chris Lattner3a1716d2007-05-12 05:37:42 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
Reid Spencer2c1ce4f2007-01-20 23:21:08 +00003<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">
Reid Spencer2c1ce4f2007-01-20 23:21:08 +00008</head>
9<body>
10<div class="doc_title"> LLVM Bitcode File Format </div>
11<ol>
12 <li><a href="#abstract">Abstract</a></li>
Chris Lattnere9ef4572007-05-12 03:23:40 +000013 <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>
Chris Lattner3a1716d2007-05-12 05:37:42 +000017 <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>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +000021 <li><a href="#abbreviations">Abbreviations</a></li>
Chris Lattner7300af52007-05-13 00:59:52 +000022 <li><a href="#stdblocks">Standard Blocks</a></li>
Chris Lattnere9ef4572007-05-12 03:23:40 +000023 </ol>
24 </li>
Chris Lattner6fa6a322008-07-09 05:14:23 +000025 <li><a href="#wrapper">Bitcode Wrapper Format</a>
26 </li>
Chris Lattner69b3e402007-05-13 01:39:44 +000027 <li><a href="#llvmir">LLVM IR Encoding</a>
28 <ol>
29 <li><a href="#basics">Basics</a></li>
30 </ol>
31 </li>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000032</ol>
33<div class="doc_author">
Chris Lattnerf19b8e42007-10-08 18:42:45 +000034 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
35 and <a href="http://www.reverberate.org">Joshua Haberman</a>.
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000036</p>
37</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +000038
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000039<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000040<div class="doc_section"> <a name="abstract">Abstract</a></div>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000041<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000042
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000043<div class="doc_text">
Chris Lattnere9ef4572007-05-12 03:23:40 +000044
45<p>This document describes the LLVM bitstream file format and the encoding of
46the LLVM IR into it.</p>
47
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000048</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +000049
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000050<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000051<div class="doc_section"> <a name="overview">Overview</a></div>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000052<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000053
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000054<div class="doc_text">
Chris Lattnere9ef4572007-05-12 03:23:40 +000055
56<p>
57What is commonly known as the LLVM bitcode file format (also, sometimes
58anachronistically known as bytecode) is actually two things: a <a
59href="#bitstream">bitstream container format</a>
60and an <a href="#llvmir">encoding of LLVM IR</a> into the container format.</p>
61
62<p>
Reid Spencer58d05472007-05-12 08:01:52 +000063The bitstream format is an abstract encoding of structured data, very
Chris Lattnere9ef4572007-05-12 03:23:40 +000064similar to XML in some ways. Like XML, bitstream files contain tags, and nested
65structures, and you can parse the file without having to understand the tags.
66Unlike XML, the bitstream format is a binary encoding, and unlike XML it
67provides a mechanism for the file to self-describe "abbreviations", which are
68effectively size optimizations for the content.</p>
69
Chris Lattner6fa6a322008-07-09 05:14:23 +000070<p>LLVM IR files may be optionally embedded into a <a
71href="#wrapper">wrapper</a> structure that makes it easy to embed extra data
72along with LLVM IR files.</p>
73
74<p>This document first describes the LLVM bitstream format, describes the
75wrapper format, then describes the record structure used by LLVM IR files.
Chris Lattnere9ef4572007-05-12 03:23:40 +000076</p>
77
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000078</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +000079
80<!-- *********************************************************************** -->
81<div class="doc_section"> <a name="bitstream">Bitstream Format</a></div>
82<!-- *********************************************************************** -->
83
84<div class="doc_text">
85
86<p>
87The bitstream format is literally a stream of bits, with a very simple
88structure. This structure consists of the following concepts:
89</p>
90
91<ul>
Chris Lattner3a1716d2007-05-12 05:37:42 +000092<li>A "<a href="#magic">magic number</a>" that identifies the contents of
93 the stream.</li>
94<li>Encoding <a href="#primitives">primitives</a> like variable bit-rate
95 integers.</li>
96<li><a href="#blocks">Blocks</a>, which define nested content.</li>
97<li><a href="#datarecord">Data Records</a>, which describe entities within the
98 file.</li>
Chris Lattnere9ef4572007-05-12 03:23:40 +000099<li>Abbreviations, which specify compression optimizations for the file.</li>
100</ul>
101
102<p>Note that the <a
103href="CommandGuide/html/llvm-bcanalyzer.html">llvm-bcanalyzer</a> tool can be
104used to dump and inspect arbitrary bitstreams, which is very useful for
105understanding the encoding.</p>
106
107</div>
108
109<!-- ======================================================================= -->
110<div class="doc_subsection"><a name="magic">Magic Numbers</a>
111</div>
112
113<div class="doc_text">
114
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000115<p>The first two bytes of a bitcode file are 'BC' (0x42, 0x43).
116The second two bytes are an application-specific magic number. Generic
117bitcode tools can look at only the first two bytes to verify the file is
118bitcode, while application-specific programs will want to look at all four.</p>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000119
120</div>
121
Chris Lattner3a1716d2007-05-12 05:37:42 +0000122<!-- ======================================================================= -->
123<div class="doc_subsection"><a name="primitives">Primitives</a>
124</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000125
126<div class="doc_text">
127
Chris Lattner3a1716d2007-05-12 05:37:42 +0000128<p>
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000129A bitstream literally consists of a stream of bits, which are read in order
130starting with the least significant bit of each byte. The stream is made up of a
Chris Lattner69b3e402007-05-13 01:39:44 +0000131number of primitive values that encode a stream of unsigned integer values.
132These
Chris Lattner3a1716d2007-05-12 05:37:42 +0000133integers are are encoded in two ways: either as <a href="#fixedwidth">Fixed
134Width Integers</a> or as <a href="#variablewidth">Variable Width
135Integers</a>.
Chris Lattnere9ef4572007-05-12 03:23:40 +0000136</p>
137
138</div>
139
Chris Lattner3a1716d2007-05-12 05:37:42 +0000140<!-- _______________________________________________________________________ -->
141<div class="doc_subsubsection"> <a name="fixedwidth">Fixed Width Integers</a>
142</div>
143
144<div class="doc_text">
145
146<p>Fixed-width integer values have their low bits emitted directly to the file.
147 For example, a 3-bit integer value encodes 1 as 001. Fixed width integers
148 are used when there are a well-known number of options for a field. For
149 example, boolean values are usually encoded with a 1-bit wide integer.
150</p>
151
152</div>
153
154<!-- _______________________________________________________________________ -->
155<div class="doc_subsubsection"> <a name="variablewidth">Variable Width
156Integers</a></div>
157
158<div class="doc_text">
159
160<p>Variable-width integer (VBR) values encode values of arbitrary size,
161optimizing for the case where the values are small. Given a 4-bit VBR field,
162any 3-bit value (0 through 7) is encoded directly, with the high bit set to
163zero. Values larger than N-1 bits emit their bits in a series of N-1 bit
164chunks, where all but the last set the high bit.</p>
165
166<p>For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a
167vbr4 value. The first set of four bits indicates the value 3 (011) with a
168continuation piece (indicated by a high bit of 1). The next word indicates a
169value of 24 (011 << 3) with no continuation. The sum (3+24) yields the value
17027.
171</p>
172
173</div>
174
175<!-- _______________________________________________________________________ -->
176<div class="doc_subsubsection"> <a name="char6">6-bit characters</a></div>
177
178<div class="doc_text">
179
180<p>6-bit characters encode common characters into a fixed 6-bit field. They
Chris Lattnerf1d64e92007-05-12 07:50:14 +0000181represent the following characters with the following 6-bit values:</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000182
183<ul>
184<li>'a' .. 'z' - 0 .. 25</li>
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000185<li>'A' .. 'Z' - 26 .. 51</li>
186<li>'0' .. '9' - 52 .. 61</li>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000187<li>'.' - 62</li>
188<li>'_' - 63</li>
189</ul>
190
191<p>This encoding is only suitable for encoding characters and strings that
192consist only of the above characters. It is completely incapable of encoding
193characters not in the set.</p>
194
195</div>
196
197<!-- _______________________________________________________________________ -->
198<div class="doc_subsubsection"> <a name="wordalign">Word Alignment</a></div>
199
200<div class="doc_text">
201
202<p>Occasionally, it is useful to emit zero bits until the bitstream is a
203multiple of 32 bits. This ensures that the bit position in the stream can be
204represented as a multiple of 32-bit words.</p>
205
206</div>
207
208
209<!-- ======================================================================= -->
210<div class="doc_subsection"><a name="abbrevid">Abbreviation IDs</a>
211</div>
212
213<div class="doc_text">
214
215<p>
216A bitstream is a sequential series of <a href="#blocks">Blocks</a> and
217<a href="#datarecord">Data Records</a>. Both of these start with an
218abbreviation ID encoded as a fixed-bitwidth field. The width is specified by
219the current block, as described below. The value of the abbreviation ID
220specifies either a builtin ID (which have special meanings, defined below) or
221one of the abbreviation IDs defined by the stream itself.
222</p>
223
224<p>
225The set of builtin abbrev IDs is:
226</p>
227
228<ul>
229<li>0 - <a href="#END_BLOCK">END_BLOCK</a> - This abbrev ID marks the end of the
230 current block.</li>
231<li>1 - <a href="#ENTER_SUBBLOCK">ENTER_SUBBLOCK</a> - This abbrev ID marks the
232 beginning of a new block.</li>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000233<li>2 - <a href="#DEFINE_ABBREV">DEFINE_ABBREV</a> - This defines a new
234 abbreviation.</li>
235<li>3 - <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a> - This ID specifies the
236 definition of an unabbreviated record.</li>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000237</ul>
238
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000239<p>Abbreviation IDs 4 and above are defined by the stream itself, and specify
240an <a href="#abbrev_records">abbreviated record encoding</a>.</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000241
242</div>
243
244<!-- ======================================================================= -->
245<div class="doc_subsection"><a name="blocks">Blocks</a>
246</div>
247
248<div class="doc_text">
249
250<p>
251Blocks in a bitstream denote nested regions of the stream, and are identified by
252a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000253function bodies). Block IDs 0-7 are reserved for <a href="#stdblocks">standard blocks</a>
254whose meaning is defined by Bitcode; block IDs 8 and greater are
255application specific. Nested blocks capture the hierachical structure of the data
Chris Lattner3a1716d2007-05-12 05:37:42 +0000256encoded in it, and various properties are associated with blocks as the file is
257parsed. Block definitions allow the reader to efficiently skip blocks
258in constant time if the reader wants a summary of blocks, or if it wants to
259efficiently skip data they do not understand. The LLVM IR reader uses this
260mechanism to skip function bodies, lazily reading them on demand.
261</p>
262
263<p>
264When reading and encoding the stream, several properties are maintained for the
265block. In particular, each block maintains:
266</p>
267
268<ol>
269<li>A current abbrev id width. This value starts at 2, and is set every time a
270 block record is entered. The block entry specifies the abbrev id width for
271 the body of the block.</li>
272
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000273<li>A set of abbreviations. Abbreviations may be defined within a block, in
274 which case they are only defined in that block (neither subblocks nor
275 enclosing blocks see the abbreviation). Abbreviations can also be defined
276 inside a <a href="#BLOCKINFO">BLOCKINFO</a> block, in which case they are
277 defined in all blocks that match the ID that the BLOCKINFO block is describing.
Chris Lattner3a1716d2007-05-12 05:37:42 +0000278</li>
279</ol>
280
281<p>As sub blocks are entered, these properties are saved and the new sub-block
282has its own set of abbreviations, and its own abbrev id width. When a sub-block
283is popped, the saved values are restored.</p>
284
285</div>
286
287<!-- _______________________________________________________________________ -->
288<div class="doc_subsubsection"> <a name="ENTER_SUBBLOCK">ENTER_SUBBLOCK
289Encoding</a></div>
290
291<div class="doc_text">
292
293<p><tt>[ENTER_SUBBLOCK, blockid<sub>vbr8</sub>, newabbrevlen<sub>vbr4</sub>,
294 &lt;align32bits&gt;, blocklen<sub>32</sub>]</tt></p>
295
296<p>
297The ENTER_SUBBLOCK abbreviation ID specifies the start of a new block record.
298The <tt>blockid</tt> value is encoded as a 8-bit VBR identifier, and indicates
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000299the type of block being entered (which can be a <a href="#stdblocks">standard
300block</a> or an application-specific block). The
Chris Lattner3a1716d2007-05-12 05:37:42 +0000301<tt>newabbrevlen</tt> value is a 4-bit VBR which specifies the
302abbrev id width for the sub-block. The <tt>blocklen</tt> is a 32-bit aligned
303value that specifies the size of the subblock, in 32-bit words. This value
304allows the reader to skip over the entire block in one jump.
305</p>
306
307</div>
308
309<!-- _______________________________________________________________________ -->
310<div class="doc_subsubsection"> <a name="END_BLOCK">END_BLOCK
311Encoding</a></div>
312
313<div class="doc_text">
314
315<p><tt>[END_BLOCK, &lt;align32bits&gt;]</tt></p>
316
317<p>
318The END_BLOCK abbreviation ID specifies the end of the current block record.
319Its end is aligned to 32-bits to ensure that the size of the block is an even
320multiple of 32-bits.</p>
321
322</div>
323
324
325
326<!-- ======================================================================= -->
327<div class="doc_subsection"><a name="datarecord">Data Records</a>
328</div>
329
330<div class="doc_text">
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000331<p>
332Data records consist of a record code and a number of (up to) 64-bit integer
333values. The interpretation of the code and values is application specific and
334there are multiple different ways to encode a record (with an unabbrev record
335or with an abbreviation). In the LLVM IR format, for example, there is a record
336which encodes the target triple of a module. The code is MODULE_CODE_TRIPLE,
337and the values of the record are the ascii codes for the characters in the
338string.</p>
339
340</div>
341
342<!-- _______________________________________________________________________ -->
343<div class="doc_subsubsection"> <a name="UNABBREV_RECORD">UNABBREV_RECORD
344Encoding</a></div>
345
346<div class="doc_text">
347
348<p><tt>[UNABBREV_RECORD, code<sub>vbr6</sub>, numops<sub>vbr6</sub>,
349 op0<sub>vbr6</sub>, op1<sub>vbr6</sub>, ...]</tt></p>
350
351<p>An UNABBREV_RECORD provides a default fallback encoding, which is both
352completely general and also extremely inefficient. It can describe an arbitrary
353record, by emitting the code and operands as vbrs.</p>
354
355<p>For example, emitting an LLVM IR target triple as an unabbreviated record
356requires emitting the UNABBREV_RECORD abbrevid, a vbr6 for the
357MODULE_CODE_TRIPLE code, a vbr6 for the length of the string (which is equal to
358the number of operands), and a vbr6 for each character. Since there are no
359letters with value less than 32, each letter would need to be emitted as at
360least a two-part VBR, which means that each letter would require at least 12
361bits. This is not an efficient encoding, but it is fully general.</p>
362
363</div>
364
365<!-- _______________________________________________________________________ -->
366<div class="doc_subsubsection"> <a name="abbrev_records">Abbreviated Record
367Encoding</a></div>
368
369<div class="doc_text">
370
371<p><tt>[&lt;abbrevid&gt;, fields...]</tt></p>
372
373<p>An abbreviated record is a abbreviation id followed by a set of fields that
374are encoded according to the <a href="#abbreviations">abbreviation
375definition</a>. This allows records to be encoded significantly more densely
376than records encoded with the <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a>
377type, and allows the abbreviation types to be specified in the stream itself,
378which allows the files to be completely self describing. The actual encoding
379of abbreviations is defined below.
380</p>
381
382</div>
383
384<!-- ======================================================================= -->
385<div class="doc_subsection"><a name="abbreviations">Abbreviations</a>
386</div>
387
388<div class="doc_text">
389<p>
390Abbreviations are an important form of compression for bitstreams. The idea is
391to specify a dense encoding for a class of records once, then use that encoding
392to emit many records. It takes space to emit the encoding into the file, but
393the space is recouped (hopefully plus some) when the records that use it are
394emitted.
395</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000396
397<p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000398Abbreviations can be determined dynamically per client, per file. Since the
399abbreviations are stored in the bitstream itself, different streams of the same
400format can contain different sets of abbreviations if the specific stream does
401not need it. As a concrete example, LLVM IR files usually emit an abbreviation
402for binary operators. If a specific LLVM module contained no or few binary
403operators, the abbreviation does not need to be emitted.
Chris Lattner3a1716d2007-05-12 05:37:42 +0000404</p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000405</div>
406
407<!-- _______________________________________________________________________ -->
408<div class="doc_subsubsection"><a name="DEFINE_ABBREV">DEFINE_ABBREV
409 Encoding</a></div>
410
411<div class="doc_text">
412
413<p><tt>[DEFINE_ABBREV, numabbrevops<sub>vbr5</sub>, abbrevop0, abbrevop1,
414 ...]</tt></p>
415
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000416<p>A DEFINE_ABBREV record adds an abbreviation to the list of currently
417defined abbreviations in the scope of this block. This definition only
418exists inside this immediate block -- it is not visible in subblocks or
419enclosing blocks.
420Abbreviations are implicitly assigned IDs
421sequentially starting from 4 (the first application-defined abbreviation ID).
422Any abbreviations defined in a BLOCKINFO record receive IDs first, in order,
423followed by any abbreviations defined within the block itself.
424Abbreviated data records reference this ID to indicate what abbreviation
425they are invoking.</p>
426
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000427<p>An abbreviation definition consists of the DEFINE_ABBREV abbrevid followed
428by a VBR that specifies the number of abbrev operands, then the abbrev
429operands themselves. Abbreviation operands come in three forms. They all start
430with a single bit that indicates whether the abbrev operand is a literal operand
431(when the bit is 1) or an encoding operand (when the bit is 0).</p>
432
433<ol>
434<li>Literal operands - <tt>[1<sub>1</sub>, litvalue<sub>vbr8</sub>]</tt> -
435Literal operands specify that the value in the result
436is always a single specific value. This specific value is emitted as a vbr8
437after the bit indicating that it is a literal operand.</li>
438<li>Encoding info without data - <tt>[0<sub>1</sub>, encoding<sub>3</sub>]</tt>
Chris Lattner7300af52007-05-13 00:59:52 +0000439 - Operand encodings that do not have extra data are just emitted as their code.
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000440</li>
441<li>Encoding info with data - <tt>[0<sub>1</sub>, encoding<sub>3</sub>,
Chris Lattner7300af52007-05-13 00:59:52 +0000442value<sub>vbr5</sub>]</tt> - Operand encodings that do have extra data are
443emitted as their code, followed by the extra data.
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000444</li>
445</ol>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000446
Chris Lattner7300af52007-05-13 00:59:52 +0000447<p>The possible operand encodings are:</p>
448
449<ul>
450<li>1 - Fixed - The field should be emitted as a <a
451 href="#fixedwidth">fixed-width value</a>, whose width
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000452 is specified by the operand's extra data.</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000453<li>2 - VBR - The field should be emitted as a <a
454 href="#variablewidth">variable-width value</a>, whose width
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000455 is specified by the operand's extra data.</li>
456<li>3 - Array - This field is an array of values. The array operand has no
457 extra data, but expects another operand to follow it which indicates the
458 element type of the array. When reading an array in an abbreviated record,
459 the first integer is a vbr6 that indicates the array length, followed by
460 the encoded elements of the array. An array may only occur as the last
461 operand of an abbreviation (except for the one final operand that gives
462 the array's type).</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000463<li>4 - Char6 - This field should be emitted as a <a href="#char6">char6-encoded
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000464 value</a>. This operand type takes no extra data.</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000465</ul>
466
467<p>For example, target triples in LLVM modules are encoded as a record of the
468form <tt>[TRIPLE, 'a', 'b', 'c', 'd']</tt>. Consider if the bitstream emitted
469the following abbrev entry:</p>
470
471<ul>
472<li><tt>[0, Fixed, 4]</tt></li>
473<li><tt>[0, Array]</tt></li>
474<li><tt>[0, Char6]</tt></li>
475</ul>
476
477<p>When emitting a record with this abbreviation, the above entry would be
478emitted as:</p>
479
480<p><tt>[4<sub>abbrevwidth</sub>, 2<sub>4</sub>, 4<sub>vbr6</sub>,
481 0<sub>6</sub>, 1<sub>6</sub>, 2<sub>6</sub>, 3<sub>6</sub>]</tt></p>
482
483<p>These values are:</p>
484
485<ol>
486<li>The first value, 4, is the abbreviation ID for this abbreviation.</li>
487<li>The second value, 2, is the code for TRIPLE in LLVM IR files.</li>
488<li>The third value, 4, is the length of the array.</li>
489<li>The rest of the values are the char6 encoded values for "abcd".</li>
490</ol>
491
492<p>With this abbreviation, the triple is emitted with only 37 bits (assuming a
493abbrev id width of 3). Without the abbreviation, significantly more space would
494be required to emit the target triple. Also, since the TRIPLE value is not
495emitted as a literal in the abbreviation, the abbreviation can also be used for
496any other string value.
497</p>
498
Chris Lattner3a1716d2007-05-12 05:37:42 +0000499</div>
500
Chris Lattner7300af52007-05-13 00:59:52 +0000501<!-- ======================================================================= -->
502<div class="doc_subsection"><a name="stdblocks">Standard Blocks</a>
503</div>
504
505<div class="doc_text">
506
507<p>
508In addition to the basic block structure and record encodings, the bitstream
509also defines specific builtin block types. These block types specify how the
510stream is to be decoded or other metadata. In the future, new standard blocks
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000511may be added. Block IDs 0-7 are reserved for standard blocks.
Chris Lattner7300af52007-05-13 00:59:52 +0000512</p>
513
514</div>
515
516<!-- _______________________________________________________________________ -->
517<div class="doc_subsubsection"><a name="BLOCKINFO">#0 - BLOCKINFO
518Block</a></div>
519
520<div class="doc_text">
521
522<p>The BLOCKINFO block allows the description of metadata for other blocks. The
523 currently specified records are:</p>
524
525<ul>
526<li><tt>[SETBID (#1), blockid]</tt></li>
527<li><tt>[DEFINE_ABBREV, ...]</tt></li>
528</ul>
529
530<p>
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000531The SETBID record indicates which block ID is being described. SETBID
532records can occur multiple times throughout the block to change which
533block ID is being described. There must be a SETBID record prior to
534any other records.
535</p>
536
537<p>
538Standard DEFINE_ABBREV records can occur inside BLOCKINFO blocks, but unlike
539their occurrence in normal blocks, the abbreviation is defined for blocks
540matching the block ID we are describing, <i>not</i> the BLOCKINFO block itself.
541The abbreviations defined in BLOCKINFO blocks receive abbreviation ids
542as described in <a href="#DEFINE_ABBREV">DEFINE_ABBREV</a>.
543</p>
544
545<p>
546Note that although the data in BLOCKINFO blocks is described as "metadata," the
547abbreviations they contain are essential for parsing records from the
548corresponding blocks. It is not safe to skip them.
Chris Lattner7300af52007-05-13 00:59:52 +0000549</p>
550
551</div>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000552
Chris Lattnere9ef4572007-05-12 03:23:40 +0000553<!-- *********************************************************************** -->
Chris Lattner6fa6a322008-07-09 05:14:23 +0000554<div class="doc_section"> <a name="wrapper">Bitcode Wrapper Format</a></div>
555<!-- *********************************************************************** -->
556
557<div class="doc_text">
558
559<p>Bitcode files for LLVM IR may optionally be wrapped in a simple wrapper
560structure. This structure contains a simple header that indicates the offset
561and size of the embedded BC file. This allows additional information to be
562stored alongside the BC file. The structure of this file header is:
563</p>
564
565<p>
566<pre>
567[Magic<sub>32</sub>,
568 Version<sub>32</sub>,
569 Offset<sub>32</sub>,
570 Size<sub>32</sub>,
571 CPUType<sub>32</sub>]
572</pre></p>
573
574<p>Each of the fields are 32-bit fields stored in little endian form (as with
575the rest of the bitcode file fields). The Magic number is always
576<tt>0x0B17C0DE</tt> and the version is currently always <tt>0</tt>. The Offset
577field is the offset in bytes to the start of the bitcode stream in the file, and
578the Size field is a size in bytes of the stream. CPUType is a target-specific
579value that can be used to encode the CPU of the target.
580</div>
581
582
583<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +0000584<div class="doc_section"> <a name="llvmir">LLVM IR Encoding</a></div>
585<!-- *********************************************************************** -->
586
587<div class="doc_text">
588
Chris Lattner69b3e402007-05-13 01:39:44 +0000589<p>LLVM IR is encoded into a bitstream by defining blocks and records. It uses
590blocks for things like constant pools, functions, symbol tables, etc. It uses
591records for things like instructions, global variable descriptors, type
592descriptions, etc. This document does not describe the set of abbreviations
593that the writer uses, as these are fully self-described in the file, and the
594reader is not allowed to build in any knowledge of this.</p>
595
596</div>
597
598<!-- ======================================================================= -->
599<div class="doc_subsection"><a name="basics">Basics</a>
600</div>
601
602<!-- _______________________________________________________________________ -->
603<div class="doc_subsubsection"><a name="ir_magic">LLVM IR Magic Number</a></div>
604
605<div class="doc_text">
606
607<p>
608The magic number for LLVM IR files is:
609</p>
610
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000611<p><tt>[0x0<sub>4</sub>, 0xC<sub>4</sub>, 0xE<sub>4</sub>, 0xD<sub>4</sub>]</tt></p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000612
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000613<p>When combined with the bitcode magic number and viewed as bytes, this is "BC 0xC0DE".</p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000614
615</div>
616
617<!-- _______________________________________________________________________ -->
618<div class="doc_subsubsection"><a name="ir_signed_vbr">Signed VBRs</a></div>
619
620<div class="doc_text">
621
622<p>
623<a href="#variablewidth">Variable Width Integers</a> are an efficient way to
624encode arbitrary sized unsigned values, but is an extremely inefficient way to
625encode signed values (as signed values are otherwise treated as maximally large
626unsigned values).</p>
627
628<p>As such, signed vbr values of a specific width are emitted as follows:</p>
629
630<ul>
631<li>Positive values are emitted as vbrs of the specified width, but with their
632 value shifted left by one.</li>
633<li>Negative values are emitted as vbrs of the specified width, but the negated
634 value is shifted left by one, and the low bit is set.</li>
635</ul>
636
637<p>With this encoding, small positive and small negative values can both be
638emitted efficiently.</p>
639
640</div>
641
642
643<!-- _______________________________________________________________________ -->
644<div class="doc_subsubsection"><a name="ir_blocks">LLVM IR Blocks</a></div>
645
646<div class="doc_text">
647
648<p>
649LLVM IR is defined with the following blocks:
650</p>
651
652<ul>
653<li>8 - MODULE_BLOCK - This is the top-level block that contains the
654 entire module, and describes a variety of per-module information.</li>
655<li>9 - PARAMATTR_BLOCK - This enumerates the parameter attributes.</li>
656<li>10 - TYPE_BLOCK - This describes all of the types in the module.</li>
657<li>11 - CONSTANTS_BLOCK - This describes constants for a module or
658 function.</li>
659<li>12 - FUNCTION_BLOCK - This describes a function body.</li>
660<li>13 - TYPE_SYMTAB_BLOCK - This describes the type symbol table.</li>
661<li>14 - VALUE_SYMTAB_BLOCK - This describes a value symbol table.</li>
662</ul>
663
664</div>
665
666<!-- ======================================================================= -->
667<div class="doc_subsection"><a name="MODULE_BLOCK">MODULE_BLOCK Contents</a>
668</div>
669
670<div class="doc_text">
671
672<p>
673</p>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000674
675</div>
676
677
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000678<!-- *********************************************************************** -->
679<hr>
680<address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000681 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000682<a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000683 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000684 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000685<a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
686Last modified: $Date$
687</address>
688</body>
689</html>