blob: 4adf75e91b56f39649cf38f1b2bd57abf8c1385d [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 Lattner69b3e402007-05-13 01:39:44 +000025 <li><a href="#llvmir">LLVM IR Encoding</a>
26 <ol>
27 <li><a href="#basics">Basics</a></li>
28 </ol>
29 </li>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000030</ol>
31<div class="doc_author">
Chris Lattnerf19b8e42007-10-08 18:42:45 +000032 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
33 and <a href="http://www.reverberate.org">Joshua Haberman</a>.
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000034</p>
35</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +000036
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000037<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000038<div class="doc_section"> <a name="abstract">Abstract</a></div>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000039<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000040
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000041<div class="doc_text">
Chris Lattnere9ef4572007-05-12 03:23:40 +000042
43<p>This document describes the LLVM bitstream file format and the encoding of
44the LLVM IR into it.</p>
45
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000046</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +000047
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000048<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000049<div class="doc_section"> <a name="overview">Overview</a></div>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000050<!-- *********************************************************************** -->
Chris Lattnere9ef4572007-05-12 03:23:40 +000051
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000052<div class="doc_text">
Chris Lattnere9ef4572007-05-12 03:23:40 +000053
54<p>
55What is commonly known as the LLVM bitcode file format (also, sometimes
56anachronistically known as bytecode) is actually two things: a <a
57href="#bitstream">bitstream container format</a>
58and an <a href="#llvmir">encoding of LLVM IR</a> into the container format.</p>
59
60<p>
Reid Spencer58d05472007-05-12 08:01:52 +000061The bitstream format is an abstract encoding of structured data, very
Chris Lattnere9ef4572007-05-12 03:23:40 +000062similar to XML in some ways. Like XML, bitstream files contain tags, and nested
63structures, and you can parse the file without having to understand the tags.
64Unlike XML, the bitstream format is a binary encoding, and unlike XML it
65provides a mechanism for the file to self-describe "abbreviations", which are
66effectively size optimizations for the content.</p>
67
68<p>This document first describes the LLVM bitstream format, then describes the
69record structure used by LLVM IR files.
70</p>
71
Reid Spencer2c1ce4f2007-01-20 23:21:08 +000072</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +000073
74<!-- *********************************************************************** -->
75<div class="doc_section"> <a name="bitstream">Bitstream Format</a></div>
76<!-- *********************************************************************** -->
77
78<div class="doc_text">
79
80<p>
81The bitstream format is literally a stream of bits, with a very simple
82structure. This structure consists of the following concepts:
83</p>
84
85<ul>
Chris Lattner3a1716d2007-05-12 05:37:42 +000086<li>A "<a href="#magic">magic number</a>" that identifies the contents of
87 the stream.</li>
88<li>Encoding <a href="#primitives">primitives</a> like variable bit-rate
89 integers.</li>
90<li><a href="#blocks">Blocks</a>, which define nested content.</li>
91<li><a href="#datarecord">Data Records</a>, which describe entities within the
92 file.</li>
Chris Lattnere9ef4572007-05-12 03:23:40 +000093<li>Abbreviations, which specify compression optimizations for the file.</li>
94</ul>
95
96<p>Note that the <a
97href="CommandGuide/html/llvm-bcanalyzer.html">llvm-bcanalyzer</a> tool can be
98used to dump and inspect arbitrary bitstreams, which is very useful for
99understanding the encoding.</p>
100
101</div>
102
103<!-- ======================================================================= -->
104<div class="doc_subsection"><a name="magic">Magic Numbers</a>
105</div>
106
107<div class="doc_text">
108
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000109<p>The first two bytes of a bitcode file are 'BC' (0x42, 0x43).
110The second two bytes are an application-specific magic number. Generic
111bitcode tools can look at only the first two bytes to verify the file is
112bitcode, while application-specific programs will want to look at all four.</p>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000113
114</div>
115
Chris Lattner3a1716d2007-05-12 05:37:42 +0000116<!-- ======================================================================= -->
117<div class="doc_subsection"><a name="primitives">Primitives</a>
118</div>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000119
120<div class="doc_text">
121
Chris Lattner3a1716d2007-05-12 05:37:42 +0000122<p>
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000123A bitstream literally consists of a stream of bits, which are read in order
124starting with the least significant bit of each byte. The stream is made up of a
Chris Lattner69b3e402007-05-13 01:39:44 +0000125number of primitive values that encode a stream of unsigned integer values.
126These
Chris Lattner3a1716d2007-05-12 05:37:42 +0000127integers are are encoded in two ways: either as <a href="#fixedwidth">Fixed
128Width Integers</a> or as <a href="#variablewidth">Variable Width
129Integers</a>.
Chris Lattnere9ef4572007-05-12 03:23:40 +0000130</p>
131
132</div>
133
Chris Lattner3a1716d2007-05-12 05:37:42 +0000134<!-- _______________________________________________________________________ -->
135<div class="doc_subsubsection"> <a name="fixedwidth">Fixed Width Integers</a>
136</div>
137
138<div class="doc_text">
139
140<p>Fixed-width integer values have their low bits emitted directly to the file.
141 For example, a 3-bit integer value encodes 1 as 001. Fixed width integers
142 are used when there are a well-known number of options for a field. For
143 example, boolean values are usually encoded with a 1-bit wide integer.
144</p>
145
146</div>
147
148<!-- _______________________________________________________________________ -->
149<div class="doc_subsubsection"> <a name="variablewidth">Variable Width
150Integers</a></div>
151
152<div class="doc_text">
153
154<p>Variable-width integer (VBR) values encode values of arbitrary size,
155optimizing for the case where the values are small. Given a 4-bit VBR field,
156any 3-bit value (0 through 7) is encoded directly, with the high bit set to
157zero. Values larger than N-1 bits emit their bits in a series of N-1 bit
158chunks, where all but the last set the high bit.</p>
159
160<p>For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a
161vbr4 value. The first set of four bits indicates the value 3 (011) with a
162continuation piece (indicated by a high bit of 1). The next word indicates a
163value of 24 (011 << 3) with no continuation. The sum (3+24) yields the value
16427.
165</p>
166
167</div>
168
169<!-- _______________________________________________________________________ -->
170<div class="doc_subsubsection"> <a name="char6">6-bit characters</a></div>
171
172<div class="doc_text">
173
174<p>6-bit characters encode common characters into a fixed 6-bit field. They
Chris Lattnerf1d64e92007-05-12 07:50:14 +0000175represent the following characters with the following 6-bit values:</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000176
177<ul>
178<li>'a' .. 'z' - 0 .. 25</li>
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000179<li>'A' .. 'Z' - 26 .. 51</li>
180<li>'0' .. '9' - 52 .. 61</li>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000181<li>'.' - 62</li>
182<li>'_' - 63</li>
183</ul>
184
185<p>This encoding is only suitable for encoding characters and strings that
186consist only of the above characters. It is completely incapable of encoding
187characters not in the set.</p>
188
189</div>
190
191<!-- _______________________________________________________________________ -->
192<div class="doc_subsubsection"> <a name="wordalign">Word Alignment</a></div>
193
194<div class="doc_text">
195
196<p>Occasionally, it is useful to emit zero bits until the bitstream is a
197multiple of 32 bits. This ensures that the bit position in the stream can be
198represented as a multiple of 32-bit words.</p>
199
200</div>
201
202
203<!-- ======================================================================= -->
204<div class="doc_subsection"><a name="abbrevid">Abbreviation IDs</a>
205</div>
206
207<div class="doc_text">
208
209<p>
210A bitstream is a sequential series of <a href="#blocks">Blocks</a> and
211<a href="#datarecord">Data Records</a>. Both of these start with an
212abbreviation ID encoded as a fixed-bitwidth field. The width is specified by
213the current block, as described below. The value of the abbreviation ID
214specifies either a builtin ID (which have special meanings, defined below) or
215one of the abbreviation IDs defined by the stream itself.
216</p>
217
218<p>
219The set of builtin abbrev IDs is:
220</p>
221
222<ul>
223<li>0 - <a href="#END_BLOCK">END_BLOCK</a> - This abbrev ID marks the end of the
224 current block.</li>
225<li>1 - <a href="#ENTER_SUBBLOCK">ENTER_SUBBLOCK</a> - This abbrev ID marks the
226 beginning of a new block.</li>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000227<li>2 - <a href="#DEFINE_ABBREV">DEFINE_ABBREV</a> - This defines a new
228 abbreviation.</li>
229<li>3 - <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a> - This ID specifies the
230 definition of an unabbreviated record.</li>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000231</ul>
232
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000233<p>Abbreviation IDs 4 and above are defined by the stream itself, and specify
234an <a href="#abbrev_records">abbreviated record encoding</a>.</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000235
236</div>
237
238<!-- ======================================================================= -->
239<div class="doc_subsection"><a name="blocks">Blocks</a>
240</div>
241
242<div class="doc_text">
243
244<p>
245Blocks in a bitstream denote nested regions of the stream, and are identified by
246a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000247function bodies). Block IDs 0-7 are reserved for <a href="#stdblocks">standard blocks</a>
248whose meaning is defined by Bitcode; block IDs 8 and greater are
249application specific. Nested blocks capture the hierachical structure of the data
Chris Lattner3a1716d2007-05-12 05:37:42 +0000250encoded in it, and various properties are associated with blocks as the file is
251parsed. Block definitions allow the reader to efficiently skip blocks
252in constant time if the reader wants a summary of blocks, or if it wants to
253efficiently skip data they do not understand. The LLVM IR reader uses this
254mechanism to skip function bodies, lazily reading them on demand.
255</p>
256
257<p>
258When reading and encoding the stream, several properties are maintained for the
259block. In particular, each block maintains:
260</p>
261
262<ol>
263<li>A current abbrev id width. This value starts at 2, and is set every time a
264 block record is entered. The block entry specifies the abbrev id width for
265 the body of the block.</li>
266
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000267<li>A set of abbreviations. Abbreviations may be defined within a block, in
268 which case they are only defined in that block (neither subblocks nor
269 enclosing blocks see the abbreviation). Abbreviations can also be defined
270 inside a <a href="#BLOCKINFO">BLOCKINFO</a> block, in which case they are
271 defined in all blocks that match the ID that the BLOCKINFO block is describing.
Chris Lattner3a1716d2007-05-12 05:37:42 +0000272</li>
273</ol>
274
275<p>As sub blocks are entered, these properties are saved and the new sub-block
276has its own set of abbreviations, and its own abbrev id width. When a sub-block
277is popped, the saved values are restored.</p>
278
279</div>
280
281<!-- _______________________________________________________________________ -->
282<div class="doc_subsubsection"> <a name="ENTER_SUBBLOCK">ENTER_SUBBLOCK
283Encoding</a></div>
284
285<div class="doc_text">
286
287<p><tt>[ENTER_SUBBLOCK, blockid<sub>vbr8</sub>, newabbrevlen<sub>vbr4</sub>,
288 &lt;align32bits&gt;, blocklen<sub>32</sub>]</tt></p>
289
290<p>
291The ENTER_SUBBLOCK abbreviation ID specifies the start of a new block record.
292The <tt>blockid</tt> value is encoded as a 8-bit VBR identifier, and indicates
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000293the type of block being entered (which can be a <a href="#stdblocks">standard
294block</a> or an application-specific block). The
Chris Lattner3a1716d2007-05-12 05:37:42 +0000295<tt>newabbrevlen</tt> value is a 4-bit VBR which specifies the
296abbrev id width for the sub-block. The <tt>blocklen</tt> is a 32-bit aligned
297value that specifies the size of the subblock, in 32-bit words. This value
298allows the reader to skip over the entire block in one jump.
299</p>
300
301</div>
302
303<!-- _______________________________________________________________________ -->
304<div class="doc_subsubsection"> <a name="END_BLOCK">END_BLOCK
305Encoding</a></div>
306
307<div class="doc_text">
308
309<p><tt>[END_BLOCK, &lt;align32bits&gt;]</tt></p>
310
311<p>
312The END_BLOCK abbreviation ID specifies the end of the current block record.
313Its end is aligned to 32-bits to ensure that the size of the block is an even
314multiple of 32-bits.</p>
315
316</div>
317
318
319
320<!-- ======================================================================= -->
321<div class="doc_subsection"><a name="datarecord">Data Records</a>
322</div>
323
324<div class="doc_text">
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000325<p>
326Data records consist of a record code and a number of (up to) 64-bit integer
327values. The interpretation of the code and values is application specific and
328there are multiple different ways to encode a record (with an unabbrev record
329or with an abbreviation). In the LLVM IR format, for example, there is a record
330which encodes the target triple of a module. The code is MODULE_CODE_TRIPLE,
331and the values of the record are the ascii codes for the characters in the
332string.</p>
333
334</div>
335
336<!-- _______________________________________________________________________ -->
337<div class="doc_subsubsection"> <a name="UNABBREV_RECORD">UNABBREV_RECORD
338Encoding</a></div>
339
340<div class="doc_text">
341
342<p><tt>[UNABBREV_RECORD, code<sub>vbr6</sub>, numops<sub>vbr6</sub>,
343 op0<sub>vbr6</sub>, op1<sub>vbr6</sub>, ...]</tt></p>
344
345<p>An UNABBREV_RECORD provides a default fallback encoding, which is both
346completely general and also extremely inefficient. It can describe an arbitrary
347record, by emitting the code and operands as vbrs.</p>
348
349<p>For example, emitting an LLVM IR target triple as an unabbreviated record
350requires emitting the UNABBREV_RECORD abbrevid, a vbr6 for the
351MODULE_CODE_TRIPLE code, a vbr6 for the length of the string (which is equal to
352the number of operands), and a vbr6 for each character. Since there are no
353letters with value less than 32, each letter would need to be emitted as at
354least a two-part VBR, which means that each letter would require at least 12
355bits. This is not an efficient encoding, but it is fully general.</p>
356
357</div>
358
359<!-- _______________________________________________________________________ -->
360<div class="doc_subsubsection"> <a name="abbrev_records">Abbreviated Record
361Encoding</a></div>
362
363<div class="doc_text">
364
365<p><tt>[&lt;abbrevid&gt;, fields...]</tt></p>
366
367<p>An abbreviated record is a abbreviation id followed by a set of fields that
368are encoded according to the <a href="#abbreviations">abbreviation
369definition</a>. This allows records to be encoded significantly more densely
370than records encoded with the <a href="#UNABBREV_RECORD">UNABBREV_RECORD</a>
371type, and allows the abbreviation types to be specified in the stream itself,
372which allows the files to be completely self describing. The actual encoding
373of abbreviations is defined below.
374</p>
375
376</div>
377
378<!-- ======================================================================= -->
379<div class="doc_subsection"><a name="abbreviations">Abbreviations</a>
380</div>
381
382<div class="doc_text">
383<p>
384Abbreviations are an important form of compression for bitstreams. The idea is
385to specify a dense encoding for a class of records once, then use that encoding
386to emit many records. It takes space to emit the encoding into the file, but
387the space is recouped (hopefully plus some) when the records that use it are
388emitted.
389</p>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000390
391<p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000392Abbreviations can be determined dynamically per client, per file. Since the
393abbreviations are stored in the bitstream itself, different streams of the same
394format can contain different sets of abbreviations if the specific stream does
395not need it. As a concrete example, LLVM IR files usually emit an abbreviation
396for binary operators. If a specific LLVM module contained no or few binary
397operators, the abbreviation does not need to be emitted.
Chris Lattner3a1716d2007-05-12 05:37:42 +0000398</p>
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000399</div>
400
401<!-- _______________________________________________________________________ -->
402<div class="doc_subsubsection"><a name="DEFINE_ABBREV">DEFINE_ABBREV
403 Encoding</a></div>
404
405<div class="doc_text">
406
407<p><tt>[DEFINE_ABBREV, numabbrevops<sub>vbr5</sub>, abbrevop0, abbrevop1,
408 ...]</tt></p>
409
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000410<p>A DEFINE_ABBREV record adds an abbreviation to the list of currently
411defined abbreviations in the scope of this block. This definition only
412exists inside this immediate block -- it is not visible in subblocks or
413enclosing blocks.
414Abbreviations are implicitly assigned IDs
415sequentially starting from 4 (the first application-defined abbreviation ID).
416Any abbreviations defined in a BLOCKINFO record receive IDs first, in order,
417followed by any abbreviations defined within the block itself.
418Abbreviated data records reference this ID to indicate what abbreviation
419they are invoking.</p>
420
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000421<p>An abbreviation definition consists of the DEFINE_ABBREV abbrevid followed
422by a VBR that specifies the number of abbrev operands, then the abbrev
423operands themselves. Abbreviation operands come in three forms. They all start
424with a single bit that indicates whether the abbrev operand is a literal operand
425(when the bit is 1) or an encoding operand (when the bit is 0).</p>
426
427<ol>
428<li>Literal operands - <tt>[1<sub>1</sub>, litvalue<sub>vbr8</sub>]</tt> -
429Literal operands specify that the value in the result
430is always a single specific value. This specific value is emitted as a vbr8
431after the bit indicating that it is a literal operand.</li>
432<li>Encoding info without data - <tt>[0<sub>1</sub>, encoding<sub>3</sub>]</tt>
Chris Lattner7300af52007-05-13 00:59:52 +0000433 - Operand encodings that do not have extra data are just emitted as their code.
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000434</li>
435<li>Encoding info with data - <tt>[0<sub>1</sub>, encoding<sub>3</sub>,
Chris Lattner7300af52007-05-13 00:59:52 +0000436value<sub>vbr5</sub>]</tt> - Operand encodings that do have extra data are
437emitted as their code, followed by the extra data.
Chris Lattnerdaeb63c2007-05-12 07:49:15 +0000438</li>
439</ol>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000440
Chris Lattner7300af52007-05-13 00:59:52 +0000441<p>The possible operand encodings are:</p>
442
443<ul>
444<li>1 - Fixed - The field should be emitted as a <a
445 href="#fixedwidth">fixed-width value</a>, whose width
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000446 is specified by the operand's extra data.</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000447<li>2 - VBR - The field should be emitted as a <a
448 href="#variablewidth">variable-width value</a>, whose width
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000449 is specified by the operand's extra data.</li>
450<li>3 - Array - This field is an array of values. The array operand has no
451 extra data, but expects another operand to follow it which indicates the
452 element type of the array. When reading an array in an abbreviated record,
453 the first integer is a vbr6 that indicates the array length, followed by
454 the encoded elements of the array. An array may only occur as the last
455 operand of an abbreviation (except for the one final operand that gives
456 the array's type).</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000457<li>4 - Char6 - This field should be emitted as a <a href="#char6">char6-encoded
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000458 value</a>. This operand type takes no extra data.</li>
Chris Lattner7300af52007-05-13 00:59:52 +0000459</ul>
460
461<p>For example, target triples in LLVM modules are encoded as a record of the
462form <tt>[TRIPLE, 'a', 'b', 'c', 'd']</tt>. Consider if the bitstream emitted
463the following abbrev entry:</p>
464
465<ul>
466<li><tt>[0, Fixed, 4]</tt></li>
467<li><tt>[0, Array]</tt></li>
468<li><tt>[0, Char6]</tt></li>
469</ul>
470
471<p>When emitting a record with this abbreviation, the above entry would be
472emitted as:</p>
473
474<p><tt>[4<sub>abbrevwidth</sub>, 2<sub>4</sub>, 4<sub>vbr6</sub>,
475 0<sub>6</sub>, 1<sub>6</sub>, 2<sub>6</sub>, 3<sub>6</sub>]</tt></p>
476
477<p>These values are:</p>
478
479<ol>
480<li>The first value, 4, is the abbreviation ID for this abbreviation.</li>
481<li>The second value, 2, is the code for TRIPLE in LLVM IR files.</li>
482<li>The third value, 4, is the length of the array.</li>
483<li>The rest of the values are the char6 encoded values for "abcd".</li>
484</ol>
485
486<p>With this abbreviation, the triple is emitted with only 37 bits (assuming a
487abbrev id width of 3). Without the abbreviation, significantly more space would
488be required to emit the target triple. Also, since the TRIPLE value is not
489emitted as a literal in the abbreviation, the abbreviation can also be used for
490any other string value.
491</p>
492
Chris Lattner3a1716d2007-05-12 05:37:42 +0000493</div>
494
Chris Lattner7300af52007-05-13 00:59:52 +0000495<!-- ======================================================================= -->
496<div class="doc_subsection"><a name="stdblocks">Standard Blocks</a>
497</div>
498
499<div class="doc_text">
500
501<p>
502In addition to the basic block structure and record encodings, the bitstream
503also defines specific builtin block types. These block types specify how the
504stream is to be decoded or other metadata. In the future, new standard blocks
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000505may be added. Block IDs 0-7 are reserved for standard blocks.
Chris Lattner7300af52007-05-13 00:59:52 +0000506</p>
507
508</div>
509
510<!-- _______________________________________________________________________ -->
511<div class="doc_subsubsection"><a name="BLOCKINFO">#0 - BLOCKINFO
512Block</a></div>
513
514<div class="doc_text">
515
516<p>The BLOCKINFO block allows the description of metadata for other blocks. The
517 currently specified records are:</p>
518
519<ul>
520<li><tt>[SETBID (#1), blockid]</tt></li>
521<li><tt>[DEFINE_ABBREV, ...]</tt></li>
522</ul>
523
524<p>
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000525The SETBID record indicates which block ID is being described. SETBID
526records can occur multiple times throughout the block to change which
527block ID is being described. There must be a SETBID record prior to
528any other records.
529</p>
530
531<p>
532Standard DEFINE_ABBREV records can occur inside BLOCKINFO blocks, but unlike
533their occurrence in normal blocks, the abbreviation is defined for blocks
534matching the block ID we are describing, <i>not</i> the BLOCKINFO block itself.
535The abbreviations defined in BLOCKINFO blocks receive abbreviation ids
536as described in <a href="#DEFINE_ABBREV">DEFINE_ABBREV</a>.
537</p>
538
539<p>
540Note that although the data in BLOCKINFO blocks is described as "metadata," the
541abbreviations they contain are essential for parsing records from the
542corresponding blocks. It is not safe to skip them.
Chris Lattner7300af52007-05-13 00:59:52 +0000543</p>
544
545</div>
Chris Lattner3a1716d2007-05-12 05:37:42 +0000546
Chris Lattnere9ef4572007-05-12 03:23:40 +0000547<!-- *********************************************************************** -->
548<div class="doc_section"> <a name="llvmir">LLVM IR Encoding</a></div>
549<!-- *********************************************************************** -->
550
551<div class="doc_text">
552
Chris Lattner69b3e402007-05-13 01:39:44 +0000553<p>LLVM IR is encoded into a bitstream by defining blocks and records. It uses
554blocks for things like constant pools, functions, symbol tables, etc. It uses
555records for things like instructions, global variable descriptors, type
556descriptions, etc. This document does not describe the set of abbreviations
557that the writer uses, as these are fully self-described in the file, and the
558reader is not allowed to build in any knowledge of this.</p>
559
560</div>
561
562<!-- ======================================================================= -->
563<div class="doc_subsection"><a name="basics">Basics</a>
564</div>
565
566<!-- _______________________________________________________________________ -->
567<div class="doc_subsubsection"><a name="ir_magic">LLVM IR Magic Number</a></div>
568
569<div class="doc_text">
570
571<p>
572The magic number for LLVM IR files is:
573</p>
574
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000575<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 +0000576
Chris Lattnerf19b8e42007-10-08 18:42:45 +0000577<p>When combined with the bitcode magic number and viewed as bytes, this is "BC 0xC0DE".</p>
Chris Lattner69b3e402007-05-13 01:39:44 +0000578
579</div>
580
581<!-- _______________________________________________________________________ -->
582<div class="doc_subsubsection"><a name="ir_signed_vbr">Signed VBRs</a></div>
583
584<div class="doc_text">
585
586<p>
587<a href="#variablewidth">Variable Width Integers</a> are an efficient way to
588encode arbitrary sized unsigned values, but is an extremely inefficient way to
589encode signed values (as signed values are otherwise treated as maximally large
590unsigned values).</p>
591
592<p>As such, signed vbr values of a specific width are emitted as follows:</p>
593
594<ul>
595<li>Positive values are emitted as vbrs of the specified width, but with their
596 value shifted left by one.</li>
597<li>Negative values are emitted as vbrs of the specified width, but the negated
598 value is shifted left by one, and the low bit is set.</li>
599</ul>
600
601<p>With this encoding, small positive and small negative values can both be
602emitted efficiently.</p>
603
604</div>
605
606
607<!-- _______________________________________________________________________ -->
608<div class="doc_subsubsection"><a name="ir_blocks">LLVM IR Blocks</a></div>
609
610<div class="doc_text">
611
612<p>
613LLVM IR is defined with the following blocks:
614</p>
615
616<ul>
617<li>8 - MODULE_BLOCK - This is the top-level block that contains the
618 entire module, and describes a variety of per-module information.</li>
619<li>9 - PARAMATTR_BLOCK - This enumerates the parameter attributes.</li>
620<li>10 - TYPE_BLOCK - This describes all of the types in the module.</li>
621<li>11 - CONSTANTS_BLOCK - This describes constants for a module or
622 function.</li>
623<li>12 - FUNCTION_BLOCK - This describes a function body.</li>
624<li>13 - TYPE_SYMTAB_BLOCK - This describes the type symbol table.</li>
625<li>14 - VALUE_SYMTAB_BLOCK - This describes a value symbol table.</li>
626</ul>
627
628</div>
629
630<!-- ======================================================================= -->
631<div class="doc_subsection"><a name="MODULE_BLOCK">MODULE_BLOCK Contents</a>
632</div>
633
634<div class="doc_text">
635
636<p>
637</p>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000638
639</div>
640
641
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000642<!-- *********************************************************************** -->
643<hr>
644<address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
645 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
646<a href="http://validator.w3.org/check/referer"><img
647 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
Chris Lattnere9ef4572007-05-12 03:23:40 +0000648 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer2c1ce4f2007-01-20 23:21:08 +0000649<a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
650Last modified: $Date$
651</address>
652</body>
653</html>