1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
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">
10 <div class="doc_title"> LLVM Bitcode File Format </div>
12 <li><a href="#abstract">Abstract</a></li>
13 <li><a href="#overview">Overview</a></li>
14 <li><a href="#bitstream">Bitstream Format</a>
16 <li><a href="#magic">Magic Numbers</a></li>
17 <li><a href="#primitives">Primitives</a></li>
18 <li><a href="#abbrevid">Abbreviation IDs</a></li>
19 <li><a href="#blocks">Blocks</a></li>
20 <li><a href="#datarecord">Data Records</a></li>
23 <li><a href="#llvmir">LLVM IR Encoding</a></li>
25 <div class="doc_author">
26 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>.
30 <!-- *********************************************************************** -->
31 <div class="doc_section"> <a name="abstract">Abstract</a></div>
32 <!-- *********************************************************************** -->
34 <div class="doc_text">
36 <p>This document describes the LLVM bitstream file format and the encoding of
37 the LLVM IR into it.</p>
41 <!-- *********************************************************************** -->
42 <div class="doc_section"> <a name="overview">Overview</a></div>
43 <!-- *********************************************************************** -->
45 <div class="doc_text">
48 What is commonly known as the LLVM bitcode file format (also, sometimes
49 anachronistically known as bytecode) is actually two things: a <a
50 href="#bitstream">bitstream container format</a>
51 and an <a href="#llvmir">encoding of LLVM IR</a> into the container format.</p>
54 The bitstream format is an abstract encoding of structured data, like very
55 similar to XML in some ways. Like XML, bitstream files contain tags, and nested
56 structures, and you can parse the file without having to understand the tags.
57 Unlike XML, the bitstream format is a binary encoding, and unlike XML it
58 provides a mechanism for the file to self-describe "abbreviations", which are
59 effectively size optimizations for the content.</p>
61 <p>This document first describes the LLVM bitstream format, then describes the
62 record structure used by LLVM IR files.
67 <!-- *********************************************************************** -->
68 <div class="doc_section"> <a name="bitstream">Bitstream Format</a></div>
69 <!-- *********************************************************************** -->
71 <div class="doc_text">
74 The bitstream format is literally a stream of bits, with a very simple
75 structure. This structure consists of the following concepts:
79 <li>A "<a href="#magic">magic number</a>" that identifies the contents of
81 <li>Encoding <a href="#primitives">primitives</a> like variable bit-rate
83 <li><a href="#blocks">Blocks</a>, which define nested content.</li>
84 <li><a href="#datarecord">Data Records</a>, which describe entities within the
86 <li>Abbreviations, which specify compression optimizations for the file.</li>
90 href="CommandGuide/html/llvm-bcanalyzer.html">llvm-bcanalyzer</a> tool can be
91 used to dump and inspect arbitrary bitstreams, which is very useful for
92 understanding the encoding.</p>
96 <!-- ======================================================================= -->
97 <div class="doc_subsection"><a name="magic">Magic Numbers</a>
100 <div class="doc_text">
102 <p>The first four bytes of the stream identify the encoding of the file. This
103 is used by a reader to know what is contained in the file.</p>
107 <!-- ======================================================================= -->
108 <div class="doc_subsection"><a name="primitives">Primitives</a>
111 <div class="doc_text">
114 A bitstream literally consists of a stream of bits. This stream is made up of a
115 number of primitive values that encode a stream of integer values. These
116 integers are are encoded in two ways: either as <a href="#fixedwidth">Fixed
117 Width Integers</a> or as <a href="#variablewidth">Variable Width
123 <!-- _______________________________________________________________________ -->
124 <div class="doc_subsubsection"> <a name="fixedwidth">Fixed Width Integers</a>
127 <div class="doc_text">
129 <p>Fixed-width integer values have their low bits emitted directly to the file.
130 For example, a 3-bit integer value encodes 1 as 001. Fixed width integers
131 are used when there are a well-known number of options for a field. For
132 example, boolean values are usually encoded with a 1-bit wide integer.
137 <!-- _______________________________________________________________________ -->
138 <div class="doc_subsubsection"> <a name="variablewidth">Variable Width
141 <div class="doc_text">
143 <p>Variable-width integer (VBR) values encode values of arbitrary size,
144 optimizing for the case where the values are small. Given a 4-bit VBR field,
145 any 3-bit value (0 through 7) is encoded directly, with the high bit set to
146 zero. Values larger than N-1 bits emit their bits in a series of N-1 bit
147 chunks, where all but the last set the high bit.</p>
149 <p>For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a
150 vbr4 value. The first set of four bits indicates the value 3 (011) with a
151 continuation piece (indicated by a high bit of 1). The next word indicates a
152 value of 24 (011 << 3) with no continuation. The sum (3+24) yields the value
158 <!-- _______________________________________________________________________ -->
159 <div class="doc_subsubsection"> <a name="char6">6-bit characters</a></div>
161 <div class="doc_text">
163 <p>6-bit characters encode common characters into a fixed 6-bit field. They
164 represent the following characters with the following 6-bit values:<s/p>
167 <li>'a' .. 'z' - 0 .. 25</li>
168 <li>'A' .. 'Z' - 26 .. 52</li>
169 <li>'0' .. '9' - 53 .. 61</li>
174 <p>This encoding is only suitable for encoding characters and strings that
175 consist only of the above characters. It is completely incapable of encoding
176 characters not in the set.</p>
180 <!-- _______________________________________________________________________ -->
181 <div class="doc_subsubsection"> <a name="wordalign">Word Alignment</a></div>
183 <div class="doc_text">
185 <p>Occasionally, it is useful to emit zero bits until the bitstream is a
186 multiple of 32 bits. This ensures that the bit position in the stream can be
187 represented as a multiple of 32-bit words.</p>
192 <!-- ======================================================================= -->
193 <div class="doc_subsection"><a name="abbrevid">Abbreviation IDs</a>
196 <div class="doc_text">
199 A bitstream is a sequential series of <a href="#blocks">Blocks</a> and
200 <a href="#datarecord">Data Records</a>. Both of these start with an
201 abbreviation ID encoded as a fixed-bitwidth field. The width is specified by
202 the current block, as described below. The value of the abbreviation ID
203 specifies either a builtin ID (which have special meanings, defined below) or
204 one of the abbreviation IDs defined by the stream itself.
208 The set of builtin abbrev IDs is:
212 <li>0 - <a href="#END_BLOCK">END_BLOCK</a> - This abbrev ID marks the end of the
214 <li>1 - <a href="#ENTER_SUBBLOCK">ENTER_SUBBLOCK</a> - This abbrev ID marks the
215 beginning of a new block.</li>
216 <li>2 - DEFINE_ABBREV - This defines a new abbreviation.</li>
217 <li>3 - UNABBREV_RECORD - This ID specifies the definition of an unabbreviated
221 <p>Abbreviation IDs 4 and above are defined by the stream itself.</p>
225 <!-- ======================================================================= -->
226 <div class="doc_subsection"><a name="blocks">Blocks</a>
229 <div class="doc_text">
232 Blocks in a bitstream denote nested regions of the stream, and are identified by
233 a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
234 function bodies). Nested blocks capture the hierachical structure of the data
235 encoded in it, and various properties are associated with blocks as the file is
236 parsed. Block definitions allow the reader to efficiently skip blocks
237 in constant time if the reader wants a summary of blocks, or if it wants to
238 efficiently skip data they do not understand. The LLVM IR reader uses this
239 mechanism to skip function bodies, lazily reading them on demand.
243 When reading and encoding the stream, several properties are maintained for the
244 block. In particular, each block maintains:
248 <li>A current abbrev id width. This value starts at 2, and is set every time a
249 block record is entered. The block entry specifies the abbrev id width for
250 the body of the block.</li>
252 <li>A set of abbreviations. Abbreviations may be defined within a block, or
253 they may be associated with all blocks of a particular ID.
257 <p>As sub blocks are entered, these properties are saved and the new sub-block
258 has its own set of abbreviations, and its own abbrev id width. When a sub-block
259 is popped, the saved values are restored.</p>
263 <!-- _______________________________________________________________________ -->
264 <div class="doc_subsubsection"> <a name="ENTER_SUBBLOCK">ENTER_SUBBLOCK
267 <div class="doc_text">
269 <p><tt>[ENTER_SUBBLOCK, blockid<sub>vbr8</sub>, newabbrevlen<sub>vbr4</sub>,
270 <align32bits>, blocklen<sub>32</sub>]</tt></p>
273 The ENTER_SUBBLOCK abbreviation ID specifies the start of a new block record.
274 The <tt>blockid</tt> value is encoded as a 8-bit VBR identifier, and indicates
275 the type of block being entered (which is application specific). The
276 <tt>newabbrevlen</tt> value is a 4-bit VBR which specifies the
277 abbrev id width for the sub-block. The <tt>blocklen</tt> is a 32-bit aligned
278 value that specifies the size of the subblock, in 32-bit words. This value
279 allows the reader to skip over the entire block in one jump.
284 <!-- _______________________________________________________________________ -->
285 <div class="doc_subsubsection"> <a name="END_BLOCK">END_BLOCK
288 <div class="doc_text">
290 <p><tt>[END_BLOCK, <align32bits>]</tt></p>
293 The END_BLOCK abbreviation ID specifies the end of the current block record.
294 Its end is aligned to 32-bits to ensure that the size of the block is an even
295 multiple of 32-bits.</p>
301 <!-- ======================================================================= -->
302 <div class="doc_subsection"><a name="datarecord">Data Records</a>
305 <div class="doc_text">
314 <!-- *********************************************************************** -->
315 <div class="doc_section"> <a name="llvmir">LLVM IR Encoding</a></div>
316 <!-- *********************************************************************** -->
318 <div class="doc_text">
325 <!-- *********************************************************************** -->
327 <address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
328 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
329 <a href="http://validator.w3.org/check/referer"><img
330 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
331 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
332 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
333 Last modified: $Date$