blob: 1c87b888969410db268f9c418b9e6a98b928ed88 [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
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000183<div class="doc_code">
184<pre>
185'a' .. 'z' &mdash; 0 .. 25
186'A' .. 'Z' &mdash; 26 .. 51
187'0' .. '9' &mdash; 52 .. 61
188 '.' &mdash; 62
189 '_' &mdash; 63
190</pre>
191</div>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000192
193<p>This encoding is only suitable for encoding characters and strings that
194consist only of the above characters. It is completely incapable of encoding
195characters not in the set.</p>
196
197</div>
198
199<!-- _______________________________________________________________________ -->
200<div class="doc_subsubsection"> <a name="wordalign">Word Alignment</a></div>
201
202<div class="doc_text">
203
204<p>Occasionally, it is useful to emit zero bits until the bitstream is a
205multiple of 32 bits. This ensures that the bit position in the stream can be
206represented as a multiple of 32-bit words.</p>
207
208</div>
209
210
211<!-- ======================================================================= -->
212<div class="doc_subsection"><a name="abbrevid">Abbreviation IDs</a>
213</div>
214
215<div class="doc_text">
216
217<p>
218A bitstream is a sequential series of <a href="#blocks">Blocks</a> and
219<a href="#datarecord">Data Records</a>. Both of these start with an
220abbreviation ID encoded as a fixed-bitwidth field. The width is specified by
221the current block, as described below. The value of the abbreviation ID
222specifies either a builtin ID (which have special meanings, defined below) or
223one of the abbreviation IDs defined by the stream itself.
224</p>
225
226<p>
227The set of builtin abbrev IDs is:
228</p>
229
230<ul>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000231<li><tt>0 - <a href="#END_BLOCK">END_BLOCK</a></tt> &mdash; This abbrev ID marks
232 the end of the current block.</li>
233<li><tt>1 - <a href="#ENTER_SUBBLOCK">ENTER_SUBBLOCK</a></tt> &mdash; This
234 abbrev ID marks the beginning of a new block.</li>
235<li><tt>2 - <a href="#DEFINE_ABBREV">DEFINE_ABBREV</a></tt> &mdash; This defines
236 a new abbreviation.</li>
237<li><tt>3 - <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a></tt> &mdash; This ID
238 specifies the definition of an unabbreviated record.</li>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000239</ul>
240
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000241<p>Abbreviation IDs 4 and above are defined by the stream itself, and specify
242an <a href="#abbrev_records">abbreviated record encoding</a>.</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000243
244</div>
245
246<!-- ======================================================================= -->
247<div class="doc_subsection"><a name="blocks">Blocks</a>
248</div>
249
250<div class="doc_text">
251
252<p>
253Blocks in a bitstream denote nested regions of the stream, and are identified by
254a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000255function bodies). Block IDs 0-7 are reserved for <a href="#stdblocks">standard blocks</a>
256whose meaning is defined by Bitcode; block IDs 8 and greater are
257application specific. Nested blocks capture the hierachical structure of the data
Chris Lattner3a1716d2007-05-12 05:37:42 +0000258encoded in it, and various properties are associated with blocks as the file is
259parsed. Block definitions allow the reader to efficiently skip blocks
260in constant time if the reader wants a summary of blocks, or if it wants to
261efficiently skip data they do not understand. The LLVM IR reader uses this
262mechanism to skip function bodies, lazily reading them on demand.
263</p>
264
265<p>
266When reading and encoding the stream, several properties are maintained for the
267block. In particular, each block maintains:
268</p>
269
270<ol>
271<li>A current abbrev id width. This value starts at 2, and is set every time a
272 block record is entered. The block entry specifies the abbrev id width for
273 the body of the block.</li>
274
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000275<li>A set of abbreviations. Abbreviations may be defined within a block, in
276 which case they are only defined in that block (neither subblocks nor
277 enclosing blocks see the abbreviation). Abbreviations can also be defined
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000278 inside a <tt><a href="#BLOCKINFO">BLOCKINFO</a></tt> block, in which case
279 they are defined in all blocks that match the ID that the BLOCKINFO block is
280 describing.
Chris Lattner3a1716d2007-05-12 05:37:42 +0000281</li>
282</ol>
283
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000284<p>
285As sub blocks are entered, these properties are saved and the new sub-block has
286its own set of abbreviations, and its own abbrev id width. When a sub-block is
287popped, the saved values are restored.
288</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000289
290</div>
291
292<!-- _______________________________________________________________________ -->
293<div class="doc_subsubsection"> <a name="ENTER_SUBBLOCK">ENTER_SUBBLOCK
294Encoding</a></div>
295
296<div class="doc_text">
297
298<p><tt>[ENTER_SUBBLOCK, blockid<sub>vbr8</sub>, newabbrevlen<sub>vbr4</sub>,
299 &lt;align32bits&gt;, blocklen<sub>32</sub>]</tt></p>
300
301<p>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000302The <tt>ENTER_SUBBLOCK</tt> abbreviation ID specifies the start of a new block
303record. The <tt>blockid</tt> value is encoded as an 8-bit VBR identifier, and
304indicates the type of block being entered, which can be
305a <a href="#stdblocks">standard block</a> or an application-specific block.
306The <tt>newabbrevlen</tt> value is a 4-bit VBR, which specifies the abbrev id
307width for the sub-block. The <tt>blocklen</tt> value is a 32-bit aligned value
308that specifies the size of the subblock in 32-bit words. This value allows the
309reader to skip over the entire block in one jump.
Chris Lattner3a1716d2007-05-12 05:37:42 +0000310</p>
311
312</div>
313
314<!-- _______________________________________________________________________ -->
315<div class="doc_subsubsection"> <a name="END_BLOCK">END_BLOCK
316Encoding</a></div>
317
318<div class="doc_text">
319
320<p><tt>[END_BLOCK, &lt;align32bits&gt;]</tt></p>
321
322<p>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000323The <tt>END_BLOCK</tt> abbreviation ID specifies the end of the current block
324record. Its end is aligned to 32-bits to ensure that the size of the block is
325an even multiple of 32-bits.
326</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000327
328</div>
329
330
331
332<!-- ======================================================================= -->
333<div class="doc_subsection"><a name="datarecord">Data Records</a>
334</div>
335
336<div class="doc_text">
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000337<p>
338Data records consist of a record code and a number of (up to) 64-bit integer
339values. The interpretation of the code and values is application specific and
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000340there are multiple different ways to encode a record (with an unabbrev record or
341with an abbreviation). In the LLVM IR format, for example, there is a record
342which encodes the target triple of a module. The code is
343<tt>MODULE_CODE_TRIPLE</tt>, and the values of the record are the ASCII codes
344for the characters in the string.
345</p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000346
347</div>
348
349<!-- _______________________________________________________________________ -->
350<div class="doc_subsubsection"> <a name="UNABBREV_RECORD">UNABBREV_RECORD
351Encoding</a></div>
352
353<div class="doc_text">
354
355<p><tt>[UNABBREV_RECORD, code<sub>vbr6</sub>, numops<sub>vbr6</sub>,
356 op0<sub>vbr6</sub>, op1<sub>vbr6</sub>, ...]</tt></p>
357
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000358<p>
359An <tt>UNABBREV_RECORD</tt> provides a default fallback encoding, which is both
360completely general and extremely inefficient. It can describe an arbitrary
361record by emitting the code and operands as vbrs.
362</p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000363
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000364<p>
365For example, emitting an LLVM IR target triple as an unabbreviated record
366requires emitting the <tt>UNABBREV_RECORD</tt> abbrevid, a vbr6 for the
367<tt>MODULE_CODE_TRIPLE</tt> code, a vbr6 for the length of the string, which is
368equal to the number of operands, and a vbr6 for each character. Because there
369are no letters with values less than 32, each letter would need to be emitted as
370at least a two-part VBR, which means that each letter would require at least 12
371bits. This is not an efficient encoding, but it is fully general.
372</p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000373
374</div>
375
376<!-- _______________________________________________________________________ -->
377<div class="doc_subsubsection"> <a name="abbrev_records">Abbreviated Record
378Encoding</a></div>
379
380<div class="doc_text">
381
382<p><tt>[&lt;abbrevid&gt;, fields...]</tt></p>
383
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000384<p>
385An abbreviated record is a abbreviation id followed by a set of fields that are
386encoded according to the <a href="#abbreviations">abbreviation definition</a>.
387This allows records to be encoded significantly more densely than records
388encoded with the <tt><a href="#UNABBREV_RECORD">UNABBREV_RECORD</a></tt> type,
389and allows the abbreviation types to be specified in the stream itself, which
390allows the files to be completely self describing. The actual encoding of
391abbreviations is defined below.
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000392</p>
393
394</div>
395
396<!-- ======================================================================= -->
397<div class="doc_subsection"><a name="abbreviations">Abbreviations</a>
398</div>
399
400<div class="doc_text">
401<p>
402Abbreviations are an important form of compression for bitstreams. The idea is
403to specify a dense encoding for a class of records once, then use that encoding
404to emit many records. It takes space to emit the encoding into the file, but
405the space is recouped (hopefully plus some) when the records that use it are
406emitted.
407</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000408
409<p>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000410Abbreviations can be determined dynamically per client, per file. Because the
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000411abbreviations are stored in the bitstream itself, different streams of the same
412format can contain different sets of abbreviations if the specific stream does
413not need it. As a concrete example, LLVM IR files usually emit an abbreviation
414for binary operators. If a specific LLVM module contained no or few binary
415operators, the abbreviation does not need to be emitted.
Chris Lattner3a1716d2007-05-12 05:37:42 +0000416</p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000417</div>
418
419<!-- _______________________________________________________________________ -->
420<div class="doc_subsubsection"><a name="DEFINE_ABBREV">DEFINE_ABBREV
421 Encoding</a></div>
422
423<div class="doc_text">
424
425<p><tt>[DEFINE_ABBREV, numabbrevops<sub>vbr5</sub>, abbrevop0, abbrevop1,
426 ...]</tt></p>
427
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000428<p>
429A <tt>DEFINE_ABBREV</tt> record adds an abbreviation to the list of currently
430defined abbreviations in the scope of this block. This definition only exists
431inside this immediate block &mdash; it is not visible in subblocks or enclosing
432blocks. Abbreviations are implicitly assigned IDs sequentially starting from 4
433(the first application-defined abbreviation ID). Any abbreviations defined in a
434<tt>BLOCKINFO</tt> record receive IDs first, in order, followed by any
435abbreviations defined within the block itself. Abbreviated data records
436reference this ID to indicate what abbreviation they are invoking.
437</p>
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000438
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000439<p>
440An abbreviation definition consists of the <tt>DEFINE_ABBREV</tt> abbrevid
441followed by a VBR that specifies the number of abbrev operands, then the abbrev
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000442operands themselves. Abbreviation operands come in three forms. They all start
443with a single bit that indicates whether the abbrev operand is a literal operand
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000444(when the bit is 1) or an encoding operand (when the bit is 0).
445</p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000446
447<ol>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000448<li>Literal operands &mdash; <tt>[1<sub>1</sub>, litvalue<sub>vbr8</sub>]</tt>
449&mdash; Literal operands specify that the value in the result is always a single
450specific value. This specific value is emitted as a vbr8 after the bit
451indicating that it is a literal operand.</li>
452<li>Encoding info without data &mdash; <tt>[0<sub>1</sub>,
453 encoding<sub>3</sub>]</tt> &mdash; Operand encodings that do not have extra
454 data are just emitted as their code.
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000455</li>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000456<li>Encoding info with data &mdash; <tt>[0<sub>1</sub>, encoding<sub>3</sub>,
457value<sub>vbr5</sub>]</tt> &mdash; Operand encodings that do have extra data are
Chris Lattner7300af52007-05-13 00:59:52 +0000458emitted as their code, followed by the extra data.
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000459</li>
460</ol>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000461
Chris Lattner7300af52007-05-13 00:59:52 +0000462<p>The possible operand encodings are:</p>
463
464<ul>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000465<li>1 &mdash; Fixed &mdash; The field should be emitted as
466 a <a href="#fixedwidth">fixed-width value</a>, whose width is specified by
467 the operand's extra data.</li>
468<li>2 &mdash; VBR &mdash; The field should be emitted as
469 a <a href="#variablewidth">variable-width value</a>, whose width is
470 specified by the operand's extra data.</li>
471<li>3 &mdash; Array &mdash; This field is an array of values. The array operand
472 has no extra data, but expects another operand to follow it which indicates
473 the element type of the array. When reading an array in an abbreviated
474 record, the first integer is a vbr6 that indicates the array length,
475 followed by the encoded elements of the array. An array may only occur as
476 the last operand of an abbreviation (except for the one final operand that
477 gives the array's type).</li>
478<li>4 &mdash; Char6 &mdash; This field should be emitted as
479 a <a href="#char6">char6-encoded value</a>. This operand type takes no
480 extra data.</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000481</ul>
482
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000483<p>
484For example, target triples in LLVM modules are encoded as a record of the
Chris Lattner7300af52007-05-13 00:59:52 +0000485form <tt>[TRIPLE, 'a', 'b', 'c', 'd']</tt>. Consider if the bitstream emitted
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000486the following abbrev entry:
487</p>
Chris Lattner7300af52007-05-13 00:59:52 +0000488
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000489<div class="doc_code">
490<pre>
491[0, Fixed, 4]
492[0, Array]
493[0, Char6]
494</pre>
495</div>
Chris Lattner7300af52007-05-13 00:59:52 +0000496
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000497<p>
498When emitting a record with this abbreviation, the above entry would be emitted
499as:
500</p>
Chris Lattner7300af52007-05-13 00:59:52 +0000501
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000502<div class="doc_code">
503<pre>
504[4<sub>abbrevwidth</sub>, 2<sub>4</sub>, 4<sub>vbr6</sub>, 0<sub>6</sub>, 1<sub>6</sub>, 2<sub>6</sub>, 3<sub>6</sub>]
505</pre>
506</div>
Chris Lattner7300af52007-05-13 00:59:52 +0000507
508<p>These values are:</p>
509
510<ol>
511<li>The first value, 4, is the abbreviation ID for this abbreviation.</li>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000512<li>The second value, 2, is the code for <tt>TRIPLE</tt> in LLVM IR files.</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000513<li>The third value, 4, is the length of the array.</li>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000514<li>The rest of the values are the char6 encoded values
515 for <tt>"abcd"</tt>.</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000516</ol>
517
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000518<p>
519With this abbreviation, the triple is emitted with only 37 bits (assuming a
Chris Lattner7300af52007-05-13 00:59:52 +0000520abbrev id width of 3). Without the abbreviation, significantly more space would
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000521be required to emit the target triple. Also, because the <tt>TRIPLE</tt> value
522is not emitted as a literal in the abbreviation, the abbreviation can also be
523used for any other string value.
Chris Lattner7300af52007-05-13 00:59:52 +0000524</p>
525
Chris Lattner3a1716d2007-05-12 05:37:42 +0000526</div>
527
Chris Lattner7300af52007-05-13 00:59:52 +0000528<!-- ======================================================================= -->
529<div class="doc_subsection"><a name="stdblocks">Standard Blocks</a>
530</div>
531
532<div class="doc_text">
533
534<p>
535In addition to the basic block structure and record encodings, the bitstream
536also defines specific builtin block types. These block types specify how the
537stream is to be decoded or other metadata. In the future, new standard blocks
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000538may be added. Block IDs 0-7 are reserved for standard blocks.
Chris Lattner7300af52007-05-13 00:59:52 +0000539</p>
540
541</div>
542
543<!-- _______________________________________________________________________ -->
544<div class="doc_subsubsection"><a name="BLOCKINFO">#0 - BLOCKINFO
545Block</a></div>
546
547<div class="doc_text">
548
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000549<p>
550The <tt>BLOCKINFO</tt> block allows the description of metadata for other
551blocks. The currently specified records are:
552</p>
553
554<div class="doc_code">
555<pre>
556[SETBID (#1), blockid]
557[DEFINE_ABBREV, ...]
558</pre>
559</div>
Chris Lattner7300af52007-05-13 00:59:52 +0000560
561<p>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000562The <tt>SETBID</tt> record indicates which block ID is being
563described. <tt>SETBID</tt> records can occur multiple times throughout the
564block to change which block ID is being described. There must be
565a <tt>SETBID</tt> record prior to any other records.
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000566</p>
567
568<p>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000569Standard <tt>DEFINE_ABBREV</tt> records can occur inside <tt>BLOCKINFO</tt>
570blocks, but unlike their occurrence in normal blocks, the abbreviation is
571defined for blocks matching the block ID we are describing, <i>not</i> the
572<tt>BLOCKINFO</tt> block itself. The abbreviations defined
573in <tt>BLOCKINFO</tt> blocks receive abbreviation IDs as described
574in <tt><a href="#DEFINE_ABBREV">DEFINE_ABBREV</a></tt>.
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000575</p>
576
577<p>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000578Note that although the data in <tt>BLOCKINFO</tt> blocks is described as
579"metadata," the abbreviations they contain are essential for parsing records
580from the corresponding blocks. It is not safe to skip them.
Chris Lattner7300af52007-05-13 00:59:52 +0000581</p>
582
583</div>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000584
Chris Lattnere9ef4572007-05-12 03:23:40 +0000585<!-- *********************************************************************** -->
Chris Lattner6fa6a322008-07-09 05:14:23 +0000586<div class="doc_section"> <a name="wrapper">Bitcode Wrapper Format</a></div>
587<!-- *********************************************************************** -->
588
589<div class="doc_text">
590
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000591<p>
592Bitcode files for LLVM IR may optionally be wrapped in a simple wrapper
Chris Lattner6fa6a322008-07-09 05:14:23 +0000593structure. This structure contains a simple header that indicates the offset
594and size of the embedded BC file. This allows additional information to be
595stored alongside the BC file. The structure of this file header is:
596</p>
597
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000598<div class="doc_code">
599<pre>
600[Magic<sub>32</sub>, Version<sub>32</sub>, Offset<sub>32</sub>, Size<sub>32</sub>, CPUType<sub>32</sub>]
601</pre>
602</div>
Chris Lattner6fa6a322008-07-09 05:14:23 +0000603
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000604<p>
605Each of the fields are 32-bit fields stored in little endian form (as with
Chris Lattner6fa6a322008-07-09 05:14:23 +0000606the rest of the bitcode file fields). The Magic number is always
607<tt>0x0B17C0DE</tt> and the version is currently always <tt>0</tt>. The Offset
608field is the offset in bytes to the start of the bitcode stream in the file, and
609the Size field is a size in bytes of the stream. CPUType is a target-specific
610value that can be used to encode the CPU of the target.
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000611</p>
Chris Lattner6fa6a322008-07-09 05:14:23 +0000612
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000613</div>
Chris Lattner6fa6a322008-07-09 05:14:23 +0000614
615<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +0000616<div class="doc_section"> <a name="llvmir">LLVM IR Encoding</a></div>
617<!-- *********************************************************************** -->
618
619<div class="doc_text">
620
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000621<p>
622LLVM IR is encoded into a bitstream by defining blocks and records. It uses
Chris Lattner69b3e402007-05-13 01:39:44 +0000623blocks for things like constant pools, functions, symbol tables, etc. It uses
624records for things like instructions, global variable descriptors, type
625descriptions, etc. This document does not describe the set of abbreviations
626that the writer uses, as these are fully self-described in the file, and the
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000627reader is not allowed to build in any knowledge of this.
628</p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000629
630</div>
631
632<!-- ======================================================================= -->
633<div class="doc_subsection"><a name="basics">Basics</a>
634</div>
635
636<!-- _______________________________________________________________________ -->
637<div class="doc_subsubsection"><a name="ir_magic">LLVM IR Magic Number</a></div>
638
639<div class="doc_text">
640
641<p>
642The magic number for LLVM IR files is:
643</p>
644
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000645<div class="doc_code">
646<pre>
647[0x0<sub>4</sub>, 0xC<sub>4</sub>, 0xE<sub>4</sub>, 0xD<sub>4</sub>]
648</pre>
649</div>
Chris Lattner69b3e402007-05-13 01:39:44 +0000650
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000651<p>
652When combined with the bitcode magic number and viewed as bytes, this is
653<tt>"BC&nbsp;0xC0DE"</tt>.
654</p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000655
656</div>
657
658<!-- _______________________________________________________________________ -->
659<div class="doc_subsubsection"><a name="ir_signed_vbr">Signed VBRs</a></div>
660
661<div class="doc_text">
662
663<p>
664<a href="#variablewidth">Variable Width Integers</a> are an efficient way to
665encode arbitrary sized unsigned values, but is an extremely inefficient way to
666encode signed values (as signed values are otherwise treated as maximally large
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000667unsigned values).
668</p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000669
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000670<p>
671As such, signed vbr values of a specific width are emitted as follows:
672</p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000673
674<ul>
675<li>Positive values are emitted as vbrs of the specified width, but with their
676 value shifted left by one.</li>
677<li>Negative values are emitted as vbrs of the specified width, but the negated
678 value is shifted left by one, and the low bit is set.</li>
679</ul>
680
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000681<p>
682With this encoding, small positive and small negative values can both be emitted
683efficiently.
684</p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000685
686</div>
687
688
689<!-- _______________________________________________________________________ -->
690<div class="doc_subsubsection"><a name="ir_blocks">LLVM IR Blocks</a></div>
691
692<div class="doc_text">
693
694<p>
695LLVM IR is defined with the following blocks:
696</p>
697
698<ul>
Bill Wendlingbb7425f2009-04-04 22:27:03 +0000699<li>8 &mdash; <tt>MODULE_BLOCK</tt> &mdash; This is the top-level block that
700 contains the entire module, and describes a variety of per-module
701 information.</li>
702<li>9 &mdash; <tt>PARAMATTR_BLOCK</tt> &mdash; This enumerates the parameter
703 attributes.</li>
704<li>10 &mdash; <tt>TYPE_BLOCK</tt> &mdash; This describes all of the types in
705 the module.</li>
706<li>11 &mdash; <tt>CONSTANTS_BLOCK</tt> &mdash; This describes constants for a
707 module or function.</li>
708<li>12 &mdash; <tt>FUNCTION_BLOCK</tt> &mdash; This describes a function
709 body.</li>
710<li>13 &mdash; <tt>TYPE_SYMTAB_BLOCK</tt> &mdash; This describes the type symbol
711 table.</li>
712<li>14 &mdash; <tt>VALUE_SYMTAB_BLOCK</tt> &mdash; This describes a value symbol
713 table.</li>
Chris Lattner69b3e402007-05-13 01:39:44 +0000714</ul>
715
716</div>
717
718<!-- ======================================================================= -->
719<div class="doc_subsection"><a name="MODULE_BLOCK">MODULE_BLOCK Contents</a>
720</div>
721
722<div class="doc_text">
723
724<p>
725</p>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000726
727</div>
728
729
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000730<!-- *********************************************************************** -->
731<hr>
732<address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000733 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000734<a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000735 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000736 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000737<a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
738Last modified: $Date$
739</address>
740</body>
741</html>