1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>LLVM Bytecode File Format</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
8 table, tr, td { border: 2px solid gray }
9 th { border: 2px solid gray; font-weight: bold; }
10 table { border-collapse: collapse; margin-top: 1em margin-bottom: 1em }
14 <div class="doc_title"> LLVM Bytecode File Format </div>
16 <li><a href="#abstract">Abstract</a></li>
17 <li><a href="#general">General Concepts</a>
19 <li><a href="#blocks">Blocks</a></li>
20 <li><a href="#lists">Lists</a></li>
21 <li><a href="#fields">Fields</a></li>
22 <li><a href="#slots">Slots</a></li>
23 <li><a href="#encoding">Encoding Rules</a></li>
24 <li><a href="#align">Alignment</a></li>
27 <li><a href="#details">Detailed Layout</a>
29 <li><a href="#notation">Notation</a></li>
30 <li><a href="#blocktypes">Blocks Types</a></li>
31 <li><a href="#signature">Signature Block</a></li>
32 <li><a href="#module">Module Block</a></li>
33 <li><a href="#typeool">Global Type Pool</a></li>
34 <li><a href="#modinfo">Module Info Block</a></li>
35 <li><a href="#constants">Global Constant Pool</a></li>
36 <li><a href="#functions">Function Blocks</a></li>
37 <li><a href="#symtab">Module Symbol Table</a></li>
41 <div class="doc_author">
42 <p>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a>
45 <div class="doc_warning">
46 <p>Warning: This is a work in progress.</p>
48 <!-- *********************************************************************** -->
49 <div class="doc_section"> <a name="abstract">Abstract </a></div>
50 <!-- *********************************************************************** -->
51 <div class="doc_text">
52 <p>This document describes the LLVM bytecode
53 file format. It specifies the binary encoding rules of the bytecode file format
54 so that equivalent systems can encode bytecode files correctly. The LLVM
55 bytecode representation is used to store the intermediate representation on
56 disk in compacted form.
59 <!-- *********************************************************************** -->
60 <div class="doc_section"> <a name="general">General Concepts</a> </div>
61 <!-- *********************************************************************** -->
62 <div class="doc_text">
63 <p>This section describes the general concepts of the bytecode file format
64 without getting into bit and byte level specifics. Note that the LLVM bytecode
65 format may change in the future, but will always be backwards compatible with
66 older formats. This document only describes the most current version of the
69 <!-- _______________________________________________________________________ -->
70 <div class="doc_subsection"><a name="blocks">Blocks</a> </div>
71 <div class="doc_text">
72 <p>LLVM bytecode files consist simply of a sequence of blocks of bytes.
73 Each block begins with an identification value that determines the type of
74 the next block. The possible types of blocks are described below in the section
75 <a href="#blocktypes">Block Types</a>. The block identifier is used because
76 it is possible for entire blocks to be omitted from the file if they are
77 empty. The block identifier helps the reader determine which kind of block is
79 <p>The following block identifiers are currently in use
80 (from llvm/Bytecode/Format.h):</p>
82 <li><b>Module (0x01)</b>.</li>
83 <li><b>Function (0x11)</b>.</li>
84 <li><b>ConstantPool (0x12)</b>.</li>
85 <li><b>SymbolTable (0x13)</b>.</li>
86 <li><b>ModuleGlobalInfo (0x14)</b>.</li>
87 <li><b>GlobalTypePlane (0x15)</b>.</li>
88 <li><b>BasicBlock (0x31)</b>.</li>
89 <li><b>InstructionList (0x32)</b>.</li>
90 <li><b>CompactionTable (0x33)</b>.</li>
92 <p> All blocks are variable length, and the block header specifies the size of
93 the block. All blocks are rounded aligned to even 32-bit boundaries, so they
94 always start and end of this boundary. Each block begins with an integer
95 identifier and the length of the block, which does not include the padding
96 bytes needed for alignment.</p>
98 <!-- _______________________________________________________________________ -->
99 <div class="doc_subsection"><a name="lists">Lists</a> </div>
100 <div class="doc_text">
101 <p>Most blocks are constructed of lists of information. Lists can be constructed
102 of other lists, etc. This decomposition of information follows the containment
103 hierarchy of the LLVM Intermediate Representation. For example, a function
104 contains a list of instructions (the terminator instructions implicitly define
105 the end of the basic blocks).</p>
106 <p>A list is encoded into the file simply by encoding the number of entries as
107 an integer followed by each of the entries. The reader knows when the list is
108 done because it will have filled the list with the required numbe of entries.
111 <!-- _______________________________________________________________________ -->
112 <div class="doc_subsection"><a name="fields">Fields</a> </div>
113 <div class="doc_text">
114 <p>Fields are units of information that LLVM knows how to write atomically.
115 Most fields have a uniform length or some kind of length indication built into
116 their encoding. For example, a constant string (array of bytes) is
117 written simply as the length followed by the characters. Although this is
118 similar to a list, constant strings are treated atomically and are thus
120 <p>Fields use a condensed bit format specific to the type of information
121 they must contain. As few bits as possible are written for each field. The
122 sections that follow will provide the details on how these fields are
123 written and how the bits are to be interpreted.</p>
125 <!-- _______________________________________________________________________ -->
126 <div class="doc_subsection"><a name="slots">Slots</a> </div>
127 <div class="doc_text">
128 <p>The bytecode format uses the notion of a "slot" to reference Types and
129 Values. Since the bytecode file is a <em>direct</em> representation of LLVM's
130 intermediate representation, there is a need to represent pointers in the file.
131 Slots are used for this purpose. For example, if one has the following assembly:
134 <div class="doc_code">
135 %MyType = type { int, sbyte }<br>
136 %MyVar = external global %MyType
139 <p>there are two definitions. The definition of <tt>%MyVar</tt> uses
140 <tt>%MyType</tt>. In the C++ IR this linkage between <tt>%MyVar</tt> and
142 explicit through the use of C++ pointers. In bytecode, however, there's no
143 ability to store memory addresses. Instead, we compute and write out slot
144 numbers for every type and Value written to the file.</p>
145 <p>A slot number is simply an unsigned 32-bit integer encoded in the variable
146 bit rate scheme (see <a href="#encoding">encoding</a> below). This ensures that
147 low slot numbers are encoded in one byte. Through various bits of magic LLVM
148 attempts to always keep the slot numbers low. The first attempt is to associate
149 slot numbers with their "type plane". That is, Values of the same type are
150 written to the bytecode file in a list (sequentially). Their order in that list
151 determines their slot number. This means that slot #1 doesn't mean anything
152 unless you also specify for which type you want slot #1. Types are handled
153 specially and are always written to the file first (in the Global Type Pool) and
154 in such a way that both forward and backward references of the types can often be
155 resolved with a single pass through the type pool. </p>
156 <p>Slot numbers are also kept small by rearranging their order. Because of the
157 structure of LLVM, certain values are much more likely to be used frequently
158 in the body of a function. For this reason, a compaction table is provided in
159 the body of a function if its use would make the function body smaller.
160 Suppose you have a function body that uses just the types "int*" and "{double}"
161 but uses them thousands of time. Its worthwhile to ensure that the slot number
162 for these types are low so they can be encoded in a single byte (via vbr).
163 This is exactly what the compaction table does.</p>
165 <!-- _______________________________________________________________________ -->
166 <div class="doc_subsection"><a name="encoding">Encoding Primitives</a> </div>
167 <div class="doc_text">
168 <p>Each field that can be put out is encoded into the file using a small set
169 of primitives. The rules for these primitives are described below.</p>
170 <h3>Variable Bit Rate Encoding</h3>
171 <p>Most of the values written to LLVM bytecode files are small integers. To
172 minimize the number of bytes written for these quantities, an encoding
173 scheme similar to UTF-8 is used to write integer data. The scheme is known as
174 variable bit rate (vbr) encoding. In this encoding, the high bit of each
175 byte is used to indicate if more bytes follow. If (byte & 0x80) is non-zero
176 in any given byte, it means there is another byte immediately following that
177 also contributes to the value. For the final byte (byte & 0x80) is false
178 (the high bit is not set). In each byte only the low seven bits contribute to
179 the value. Consequently 32-bit quantities can take from one to <em>five</em>
180 bytes to encode. In general, smaller quantities will encode in fewer bytes,
182 <table class="doc_table_nw">
185 <th>Significant Bits</th>
186 <th>Maximum Value</th>
188 <tr><td>1</td><td>0-6</td><td>127</td></tr>
189 <tr><td>2</td><td>7-13</td><td>16,383</td></tr>
190 <tr><td>3</td><td>14-20</td><td>2,097,151</td></tr>
191 <tr><td>4</td><td>21-27</td><td>268,435,455</td></tr>
192 <tr><td>5</td><td>28-34</td><td>34,359,738,367</td></tr>
193 <tr><td>6</td><td>35-41</td><td>4,398,046,511,103</td></tr>
194 <tr><td>7</td><td>42-48</td><td>562,949,953,421,311</td></tr>
195 <tr><td>8</td><td>49-55</td><td>72,057,594,037,927,935</td></tr>
196 <tr><td>9</td><td>56-62</td><td>9,223,372,036,854,775,807</td></tr>
197 <tr><td>10</td><td>63-69</td><td>1,180,591,620,717,411,303,423</td></tr>
199 <p>Note that in practice, the tenth byte could only encode bit 63
200 since the maximum quantity to use this encoding is a 64-bit integer.</p>
202 <p><em>Signed</em> VBR values are encoded with the standard vbr encoding, but
203 with the sign bit as the low order bit instead of the high order bit. This
204 allows small negative quantities to be encoded efficiently. For example, -3
205 is encoded as "((3 << 1) | 1)" and 3 is encoded as "(3 << 1) |
206 0)", emitted with the standard vbr encoding above.</p>
208 <p>The table below defines the encoding rules for type names used in the
209 descriptions of blocks and fields in the next section. Any type name with
210 the suffix <em>_vbr</em> indicate a quantity that is encoded using
211 variable bit rate encoding as described above.</p>
212 <table class="doc_table" >
215 <th align="left"><b>Rule</b></th>
219 <td align="left">A 32-bit unsigned integer that always occupies four
220 consecutive bytes. The unsigned integer is encoded using LSB first
221 ordering. That is bits 2<sup>0</sup> through 2<sup>7</sup> are in the
222 byte with the lowest file offset (little endian).</td>
225 <td align="left">A 32-bit unsigned integer that occupies from one to five
226 bytes using variable bit rate encoding.</td>
229 <td align="left">A 64-bit unsigned integer that occupies from one to ten
230 bytes using variable bit rate encoding.</td>
233 <td align="left">A 64-bit signed integer that occupies from one to ten
234 bytes using the signed variable bit rate encoding.</td>
237 <td align="left">A single unsigned character encoded into one byte</td>
240 <td align="left">A single bit within a byte.</td>
243 <td align="left">A uint_vbr indicating the length of the character string
244 immediately followed by the characters of the string. There is no
245 terminating null byte in the string.</td>
248 <td align="left">An arbitrarily long segment of data to which no
249 interpretation is implied. This is used for float, double, and constant
254 <!-- _______________________________________________________________________ -->
255 <div class="doc_subsection"><a name="align">Alignment</a> </div>
256 <div class="doc_text">
257 <p>To support cross-platform differences, the bytecode file is aligned on
258 certain boundaries. This means that a small amount of padding (at most 3 bytes)
259 will be added to ensure that the next entry is aligned to a 32-bit boundary.
262 <!-- *********************************************************************** -->
263 <div class="doc_section"> <a name="details">Detailed Layout</a> </div>
264 <!-- *********************************************************************** -->
265 <div class="doc_text">
266 <p>This section provides the detailed layout of the LLVM bytecode file format.
267 bit and byte level specifics.</p>
269 <!-- _______________________________________________________________________ -->
270 <div class="doc_subsection"><a name="notation">Notation</a></div>
271 <div class="doc_text">
272 <p>The descriptions of the bytecode format that follow describe the bit
273 fields in detail. These descriptions are provided in tabular form. Each table
274 has four columns that specify:</p>
276 <li><b>Byte(s)</b>: The offset in bytes of the field from the start of
277 its container (block, list, other field).</li>
278 <li><b>Bit(s)</b>: The offset in bits of the field from the start of
279 the byte field. Bits are always little endian. That is, bit addresses with
280 smaller values have smaller address (i.e. 2<sup>0</sup> is at bit 0,
281 2<sup>1</sup> at 1, etc.)
283 <li><b>Align?</b>: Indicates if this field is aligned to 32 bits or not.
284 This indicates where the <em>next</em> field starts, always on a 32 bit
286 <li><b>Type</b>: The basic type of information contained in the field.</li>
287 <li><b>Description</b>: Describes the contents of the field.</li>
290 <!-- _______________________________________________________________________ -->
291 <div class="doc_subsection"><a name="blocktypes">Block Types</a></div>
292 <div class="doc_text">
293 <p>The bytecode format encodes the intermediate representation into groups
294 of bytes known as blocks. The blocks are written sequentially to the file in
295 the following order:</p>
297 <li><a href="#signature">Signature</a>: This contains the file signature
298 (magic number) that identifies the file as LLVM bytecode and the bytecode
300 <li><a href="#module">Module Block</a>: This is the top level block in a
301 bytecode file. It contains all the other blocks.</li>
302 <li><a href="#gtypepool">Global Type Pool</a>: This block contains all the
303 global (module) level types.</li>
304 <li><a href="#modinfo">Module Info</a>: This block contains the types of the
305 global variables and functions in the module as well as the constant
306 initializers for the global variables</li>
307 <li><a href="#constants">Constants</a>: This block contains all the global
308 constants except function arguments, global values and constant strings.</li>
309 <li><a href="#functions">Functions</a>: One function block is written for
310 each function in the module. </li>
311 <li><a href="$symtab">Symbol Table</a>: The module level symbol table that
312 provides names for the various other entries in the file is the final block
316 <!-- _______________________________________________________________________ -->
317 <div class="doc_subsection"><a name="signature">Signature Block</a> </div>
318 <div class="doc_text">
319 <p>The signature occurs in every LLVM bytecode file and is always first.
320 It simply provides a few bytes of data to identify the file as being an LLVM
321 bytecode file. This block is always four bytes in length and differs from the
322 other blocks because there is no identifier and no block length at the start
323 of the block. Essentially, this block is just the "magic number" for the file.
324 <table class="doc_table_nw" >
326 <th><b>Byte(s)</b></th>
327 <th><b>Bit(s)</b></th>
328 <th><b>Align?</b></th>
330 <th align="left"><b>Field Description</b></th>
332 <td>00</td><td>-</td><td>No</td><td>char</td>
333 <td align="left">Constant "l" (0x6C)</td>
335 <td>01</td><td>-</td><td>No</td><td>char</td>
336 <td align="left">Constant "l" (0x6C)</td>
338 <td>02</td><td>-</td><td>No</td><td>char</td>
339 <td align="left">Constant "v" (0x76)</td>
341 <td>03</td><td>-</td><td>No</td><td>char</td>
342 <td align="left">Constant "m" (0x6D)</td>
346 <!-- _______________________________________________________________________ -->
347 <div class="doc_subsection"><a name="module">Module Block</a> </div>
348 <div class="doc_text">
349 <p>The module block contains a small pre-amble and all the other blocks in
350 the file. Of particular note, the bytecode format number is simply a 28-bit
351 monotonically increase integer that identifiers the version of the bytecode
352 format (which is not directly related to the LLVM release number). The
353 bytecode versions defined so far are (note that this document only describes
354 the latest version): </p>
357 <li>#0: LLVM 1.0 & 1.1</li>
358 <li>#1: LLVM 1.2</li>
359 <li>#2: LLVM 1.3</li>
362 <p>The table below shows the format of the module block header. It is defined
363 by blocks described in other sections.</p>
364 <table class="doc_table_nw" >
366 <th><b>Byte(s)</b></th>
367 <th><b>Bit(s)</b></th>
368 <th><b>Align?</b></th>
370 <th align="left"><b>Field Description</b></th>
372 <td>04-07</td><td>-</td><td>No</td><td>unsigned</td>
373 <td align="left">Module Identifier (0x01)</td>
375 <td>08-11</td><td>-</td><td>No</td><td>unsigned</td>
376 <td align="left">Size of the module block in bytes</td>
378 <td>12-15</td><td>00</td><td>Yes</td><td>uint32_vbr</td>
379 <td align="left">Format Information</td>
381 <td>''</td><td>0</td><td>-</td><td>bit</td>
382 <td align="left">Big Endian?</td>
384 <td>''</td><td>1</td><td>-</td><td>bit</td>
385 <td align="left">Pointers Are 64-bit?</td>
387 <td>''</td><td>2</td><td>-</td><td>bit</td>
388 <td align="left">Has No Endianess?</td>
390 <td>''</td><td>3</td><td>-</td><td>bit</td>
391 <td align="left">Has No Pointer Size?</td>
393 <td>''</td><td>4-31</td><td>-</td><td>bit</td>
394 <td align="left">Bytecode Format Version</td>
396 <td>16-end</td><td>-</td><td>-</td><td>blocks</td>
397 <td align="left">The remaining bytes in the block consist
398 solely of other block types in sequence.</td>
402 <p>Note that we plan to eventually expand the target description capabilities
403 of bytecode files to <a href="http://llvm.cs.uiuc.edu/PR263">target
408 <!-- _______________________________________________________________________ -->
409 <div class="doc_subsection"><a name="gtypepool">Global Type Pool</a> </div>
410 <div class="doc_text">
411 <p>The global type pool consists of type definitions. Their order of appearance
412 in the file determines their slot number (0 based). Slot numbers are used to
413 replace pointers in the intermediate representation. Each slot number uniquely
414 identifies one entry in a type plane (a collection of values of the same type).
415 Since all values have types and are associated with the order in which the type
416 pool is written, the global type pool <em>must</em> be written as the first
417 block of a module. If it is not, attempts to read the file will fail because
418 both forward and backward type resolution will not be possible.</p>
419 <p>The type pool is simply a list of types definitions, as shown in the table
421 <table class="doc_table_nw" >
423 <th><b>Byte(s)</b></th>
424 <th><b>Bit(s)</b></th>
425 <th><b>Align?</b></th>
427 <th align="left"><b>Field Description</b></th>
429 <td>00-03</td><td>-</td><td>No</td><td>unsigned</td>
430 <td align="left">Type Pool Identifier (0x13)</td>
432 <td>04-07</td><td>-</td><td>No</td><td>unsigned</td>
433 <td align="left">Size in bytes of the symbol table block.</td>
435 <td>08-11<sup>1</sup></td><td>-</td><td>No</td><td>uint32_vbr</td>
436 <td align="left">Number of entries in type plane</td>
438 <td>12-15<sup>1</sup></td><td>-</td><td>No</td><td>uint32_vbr</td>
439 <td align="left">Type plane index for following entries</td>
441 <td>16-end<sup>1,2</sup></td><td>-</td><td>No</td><td>type</td>
442 <td align="left">Each of the type definitions.</td>
444 <td align="left" colspan="5"><sup>1</sup>Maximum length shown,
445 may be smaller<br><sup>2</sup>Repeated field.
449 <!-- _______________________________________________________________________ -->
450 <div class="doc_subsection"><a name="modinfo">Module Info</a> </div>
451 <div class="doc_text">
452 <p>To be determined.</p>
454 <!-- _______________________________________________________________________ -->
455 <div class="doc_subsection"><a name="constants">Constants</a> </div>
456 <div class="doc_text">
457 <p>To be determined.</p>
459 <!-- _______________________________________________________________________ -->
460 <div class="doc_subsection"><a name="functions">Functions</a> </div>
461 <div class="doc_text">
462 <p>To be determined.</p>
464 <!-- _______________________________________________________________________ -->
465 <div class="doc_subsection"><a name="symtab">Symbol Table</a> </div>
466 <div class="doc_text">
467 <p>A symbol table can be put out in conjunction with a module or a function.
468 A symbol table is a list of type planes. Each type plane starts with the number
469 of entries in the plane and the type plane's slot number (so the type can be
470 looked up in the global type pool). For each entry in a type plane, the slot
471 number of the value and the name associated with that value are written. The
472 format is given in the table below. </p>
473 <table class="doc_table_nw" >
475 <th><b>Byte(s)</b></th>
476 <th><b>Bit(s)</b></th>
477 <th><b>Align?</b></th>
479 <th align="left"><b>Field Description</b></th>
481 <td>00-03</td><td>-</td><td>No</td><td>unsigned</td>
482 <td align="left">Symbol Table Identifier (0x13)</td>
484 <td>04-07</td><td>-</td><td>No</td><td>unsigned</td>
485 <td align="left">Size in bytes of the symbol table block.</td>
487 <td>08-11<sup>1</sup></td><td>-</td><td>No</td><td>uint32_vbr</td>
488 <td align="left">Number of entries in type plane</td>
490 <td>12-15<sup>1</sup></td><td>-</td><td>No</td><td>uint32_vbr</td>
491 <td align="left">Type plane index for following entries</td>
493 <td>16-19<sup>1,2</sup></td><td>-</td><td>No</td><td>uint32_vbr</td>
494 <td align="left">Slot number of a value.</td>
496 <td>variable<sup>1,2</sup></td><td>-</td><td>No</td><td>string</td>
497 <td align="left">Name of the value in the symbol table.</td>
500 <td align="left" colspan="5"><sup>1</sup>Maximum length shown,
501 may be smaller<br><sup>2</sup>Repeated field.
506 <!-- *********************************************************************** -->
509 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
510 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
511 <a href="http://validator.w3.org/check/referer"><img
512 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
514 <a href="mailto:rspencer@x10sys.com">Reid Spencer</a> and
515 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
516 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
517 Last modified: $Date$