Updates to reflect most recent changes to bytecode file format:
[oota-llvm.git] / docs / BytecodeFormat.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4   <title>LLVM Bytecode File Format</title>
5   <link rel="stylesheet" href="llvm.css" type="text/css">
6   <style type="text/css">
7     TR, TD { border: 2px solid gray; padding-left: 4pt; padding-right: 4pt; padding-top: 2pt; padding-bottom: 2pt; }
8     TH { border: 2px solid gray; font-weight: bold; font-size: 105%; }
9     TABLE { text-align: center; border: 2px solid black; 
10             border-collapse: collapse; margin-top: 1em; margin-left: 1em; margin-right: 1em; margin-bottom: 1em; }
11     .td_left { border: 2px solid gray; text-align: left; }
12   </style>
13 </head>
14 <body>
15 <div class="doc_title"> LLVM Bytecode File Format </div>
16 <ol>
17   <li><a href="#abstract">Abstract</a></li>
18   <li><a href="#concepts">Concepts</a>
19     <ol>
20       <li><a href="#blocks">Blocks</a></li>
21       <li><a href="#lists">Lists</a></li>
22       <li><a href="#fields">Fields</a></li>
23       <li><a href="#align">Alignment</a></li>
24       <li><a href="#vbr">Variable Bit-Rate Encoding</a></li>
25       <li><a href="#encoding">Encoding Primitives</a></li>
26       <li><a href="#slots">Slots</a></li>
27     </ol>
28   </li>
29   <li><a href="#general">General Structure</a> </li>
30   <li><a href="#blockdefs">Block Definitions</a>
31     <ol>
32       <li><a href="#signature">Signature Block</a></li>
33       <li><a href="#module">Module Block</a></li>
34       <li><a href="#globaltypes">Global Type Pool</a></li>
35       <li><a href="#globalinfo">Module Info Block</a></li>
36       <li><a href="#constantpool">Global Constant Pool</a></li>
37       <li><a href="#functiondefs">Function Definition</a></li>
38       <li><a href="#compactiontable">Compaction Table</a></li>
39       <li><a href="#instructionlist">Instruction List</a></li>
40       <li><a href="#symtab">Symbol Table</a></li>
41     </ol>
42   </li>
43   <li><a href="#versiondiffs">Version Differences</a>
44     <ol>
45       <li><a href="#vers12">Version 1.2 Differences From 1.3</a></li>
46       <li><a href="#vers11">Version 1.1 Differences From 1.2</a></li>
47       <li><a href="#vers10">Version 1.0 Differences From 1.1</a></li>
48     </ol>
49   </li>
50 </ol>
51 <div class="doc_author">
52 <p>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a>
53 </p>
54 </div>
55 <!-- *********************************************************************** -->
56 <div class="doc_section"> <a name="abstract">Abstract </a></div>
57 <!-- *********************************************************************** -->
58 <div class="doc_text">
59 <p>This document describes the LLVM bytecode file format. It specifies
60 the binary encoding rules of the bytecode file format so that
61 equivalent systems can encode bytecode files correctly. The LLVM
62 bytecode representation is used to store the intermediate
63 representation on disk in compacted form.</p>
64 <p>The LLVM bytecode format may change in the future, but LLVM will
65 always be backwards compatible with older formats. This document will
66 only describe the most current version of the bytecode format. See <a
67  href="#versiondiffs">Version Differences</a> for the details on how
68 the current version is different from previous versions.</p>
69 </div>
70 <!-- *********************************************************************** -->
71 <div class="doc_section"> <a name="concepts">Concepts</a> </div>
72 <!-- *********************************************************************** -->
73 <div class="doc_text">
74 <p>This section describes the general concepts of the bytecode file
75 format without getting into specific layout details. It is recommended
76 that you read this section thoroughly before interpreting the detailed
77 descriptions.</p>
78 </div>
79 <!-- _______________________________________________________________________ -->
80 <div class="doc_subsection"><a name="blocks">Blocks</a> </div>
81 <div class="doc_text">
82 <p>LLVM bytecode files consist simply of a sequence of blocks of bytes
83 using a binary encoding Each block begins with an header of two
84 unsigned integers. The first value identifies the type of block and the
85 second value provides the size of the block in bytes. The block
86 identifier is used because it is possible for entire blocks to be
87 omitted from the file if they are empty. The block identifier helps the
88 reader determine which kind of block is next in the file. Note that
89 blocks can be nested within other blocks.</p>
90 <p> All blocks are variable length, and the block header specifies the
91 size of the block. All blocks begin on a byte index that is aligned to
92 an even 32-bit boundary. That is, the first block is 32-bit aligned
93 because it starts at offset 0. Each block is padded with zero fill
94 bytes to ensure that the next block also starts on a 32-bit boundary.</p>
95 </div>
96 <!-- _______________________________________________________________________ -->
97 <div class="doc_subsection"><a name="lists">Lists</a> </div>
98 <div class="doc_text">
99 <p>LLVM Bytecode blocks often contain lists of things of a similar
100 type. For example, a function contains a list of instructions and a
101 function type contains a list of argument types. There are two basic
102 types of lists: length lists (<a href="#llist">llist</a>), and null
103 terminated lists (<a href="#zlist">zlist</a>), as described below in
104 the <a href="#encoding">Encoding Primitives</a>.</p>
105 </div>
106 <!-- _______________________________________________________________________ -->
107 <div class="doc_subsection"><a name="fields">Fields</a> </div>
108 <div class="doc_text">
109 <p>Fields are units of information that LLVM knows how to write atomically. Most 
110 fields have a uniform length or some kind of length indication built into their 
111 encoding. For example, a constant string (array of bytes) is written simply as 
112 the length followed by the characters. Although this is similar to a list, 
113 constant strings are treated atomically and are thus fields.</p>
114 <p>Fields use a condensed bit format specific to the type of information
115 they must contain. As few bits as possible are written for each field. The
116 sections that follow will provide the details on how these fields are
117 written and how the bits are to be interpreted.</p>
118 </div>
119 <!-- _______________________________________________________________________ -->
120 <div class="doc_subsection"><a name="align">Alignment</a> </div>
121 <div class="doc_text">
122 <p>To support cross-platform differences, the bytecode file is aligned
123 on certain boundaries. This means that a small amount of padding (at
124 most 3 bytes) will be added to ensure that the next entry is aligned to
125 a 32-bit boundary.</p>
126 </div>
127 <!-- _______________________________________________________________________ -->
128 <div class="doc_subsection"><a name="vbr">Variable Bit-Rate Encoding</a>
129 </div>
130 <div class="doc_text">
131 <p>Most of the values written to LLVM bytecode files are small integers. To 
132 minimize the number of bytes written for these quantities, an encoding scheme 
133 similar to UTF-8 is used to write integer data. The scheme is known as
134 variable bit rate (vbr) encoding. In this encoding, the high bit of
135 each byte is used to indicate if more bytes follow. If (byte &amp;
136 0x80) is non-zero in any given byte, it means there is another byte
137 immediately following that also contributes to the value. For the final
138 byte (byte &amp; 0x80) is false (the high bit is not set). In each byte
139 only the low seven bits contribute to the value. Consequently 32-bit
140 quantities can take from one to <em>five</em> bytes to encode. In
141 general, smaller quantities will encode in fewer bytes, as follows:</p>
142 <table>
143   <tbody>
144     <tr>
145       <th>Byte #</th>
146       <th>Significant Bits</th>
147       <th>Maximum Value</th>
148     </tr>
149     <tr>
150       <td>1</td>
151       <td>0-6</td>
152       <td>127</td>
153     </tr>
154     <tr>
155       <td>2</td>
156       <td>7-13</td>
157       <td>16,383</td>
158     </tr>
159     <tr>
160       <td>3</td>
161       <td>14-20</td>
162       <td>2,097,151</td>
163     </tr>
164     <tr>
165       <td>4</td>
166       <td>21-27</td>
167       <td>268,435,455</td>
168     </tr>
169     <tr>
170       <td>5</td>
171       <td>28-34</td>
172       <td>34,359,738,367</td>
173     </tr>
174     <tr>
175       <td>6</td>
176       <td>35-41</td>
177       <td>4,398,046,511,103</td>
178     </tr>
179     <tr>
180       <td>7</td>
181       <td>42-48</td>
182       <td>562,949,953,421,311</td>
183     </tr>
184     <tr>
185       <td>8</td>
186       <td>49-55</td>
187       <td>72,057,594,037,927,935</td>
188     </tr>
189     <tr>
190       <td>9</td>
191       <td>56-62</td>
192       <td>9,223,372,036,854,775,807</td>
193     </tr>
194     <tr>
195       <td>10</td>
196       <td>63-69</td>
197       <td>1,180,591,620,717,411,303,423</td>
198     </tr>
199   </tbody>
200 </table>
201 <p>Note that in practice, the tenth byte could only encode bit 63 since
202 the maximum quantity to use this encoding is a 64-bit integer.</p>
203 <p><em>Signed</em> VBR values are encoded with the standard vbr
204 encoding, but with the sign bit as the low order bit instead of the
205 high order bit. This allows small negative quantities to be encoded
206 efficiently. For example, -3
207 is encoded as "((3 &lt;&lt; 1) | 1)" and 3 is encoded as "(3 &lt;&lt;
208 1) | 0)", emitted with the standard vbr encoding above.</p>
209 </div>
210 <!-- _______________________________________________________________________ -->
211 <div class="doc_subsection"><a name="encoding">Encoding Primitives</a> </div>
212 <div class="doc_text">
213 <p>Each field in the bytecode format is encoded into the file using a
214 small set of primitive formats. The table below defines the encoding
215 rules for the various primitives used and gives them each a type name.
216 The type names used in the descriptions of blocks and fields in the <a
217  href="#details">Detailed Layout</a>next section. Any type name with
218 the suffix <em>_vbr</em> indicates a quantity that is encoded using
219 variable bit rate encoding as described above.</p>
220 <table class="doc_table">
221   <tbody>
222     <tr>
223       <th><b>Type</b></th>
224       <th class="td_left"><b>Rule</b></th>
225     </tr>
226     <tr>
227       <td><a name="unsigned"><b>unsigned</b></a></td>
228       <td class="td_left">A 32-bit unsigned integer that always occupies four 
229       consecutive bytes. The unsigned integer is encoded using LSB first 
230       ordering. That is bits 2<sup>0</sup> through 2<sup>7</sup> are in the 
231       byte with the lowest file offset (little endian).</td>
232     </tr>
233     <tr>
234       <td style="vertical-align: top;"><a name="uint24_vbr"><b>uint24_vbr</b></a></td>
235       <td style="vertical-align: top; text-align: left;">A 24-bit unsigned 
236       integer that occupies from one to four bytes using variable bit rate 
237       encoding.</td>
238     </tr>
239     <tr>
240       <td><a name="uint32_vbr"><b>uint32_vbr</b></a></td>
241       <td class="td_left">A 32-bit unsigned integer that occupies from
242 one to five bytes using variable bit rate encoding.</td>
243     </tr>
244     <tr>
245       <td><a name="uint64_vbr"><b>uint64_vbr</b></a></td>
246       <td class="td_left">A 64-bit unsigned integer that occupies from
247 one to ten bytes using variable bit rate encoding.</td>
248     </tr>
249     <tr>
250       <td><a name="int64_vbr"><b>int64_vbr</b></a></td>
251       <td class="td_left">A 64-bit signed integer that occupies from
252 one to ten bytes using the signed variable bit rate encoding.</td>
253     </tr>
254     <tr>
255       <td><a name="char"><b>char</b></a></td>
256       <td class="td_left">A single unsigned character encoded into one
257 byte</td>
258     </tr>
259     <tr>
260       <td><a name="bit"><b>bit(n-m)</b></a></td>
261       <td class="td_left">A set of bit within some larger integer
262 field. The values of <code>n</code> and <code>m</code> specify the
263 inclusive range of bits that define the subfield. The value for <code>m</code>
264 may be omitted if its the same as <code>n</code>.</td>
265     </tr>
266     <tr>
267       <td style="vertical-align: top;"><b><a name="float"><b>float</b></a></b></td>
268       <td style="vertical-align: top; text-align: left;">A floating
269 point value encoded as a 32-bit IEEE value written in little-endian
270 form.<br>
271       </td>
272     </tr>
273     <tr>
274       <td style="vertical-align: top;"><b><b><a name="double"><b>double</b></a></b></b></td>
275       <td style="vertical-align: top; text-align: left;">A floating
276 point value encoded as a64-bit IEEE value written in little-endian form</td>
277     </tr>
278     <tr>
279       <td><a name="string"><b>string</b></a></td>
280       <td class="td_left">A uint32_vbr indicating the type of the
281 constant string which also includes its length, immediately followed by
282 the characters of the string. There is no terminating null byte in the
283 string.</td>
284     </tr>
285     <tr>
286       <td><a name="data"><b>data</b></a></td>
287       <td class="td_left">An arbitrarily long segment of data to which
288 no interpretation is implied. This is used for constant initializers.<br>
289       </td>
290     </tr>
291     <tr>
292       <td><a name="llist"><b>llist(x)</b></a></td>
293       <td class="td_left">A length list of x. This means the list is
294 encoded as an <a href="#uint32_vbr">uint32_vbr</a> providing the
295 length of the list, followed by a sequence of that many "x" items. This
296 implies that the reader should iterate the number of times provided by
297 the length.</td>
298     </tr>
299     <tr>
300       <td><a name="zlist"><b>zlist(x)</b></a></td>
301       <td class="td_left">A zero-terminated list of x. This means the
302 list is encoded as a sequence of an indeterminate number of "x" items,
303 followed by an <a href="#uint32_vbr">uint32_vbr</a> terminating value.
304 This implies that none of the "x" items can have a zero value (or else
305 the list terminates).</td>
306     </tr>
307     <tr>
308       <td><a name="block"><b>block</b></a></td>
309       <td class="td_left">A block of data that is logically related. A
310 block is an unsigned 32-bit integer that encodes the type of the block
311 in the low 5 bits and the size of the block in the high 27 bits. The
312 length does not include the block header or any alignment bytes at the
313 end of the block. Blocks may compose other blocks. </td>
314     </tr>
315   </tbody>
316 </table>
317 </div>
318 <!-- _______________________________________________________________________ -->
319 <div class="doc_subsection"><a name="notation">Field Notation</a> </div>
320 <div class="doc_text">
321 <p>In the detailed block and field descriptions that follow, a regex
322 like notation is used to describe optional and repeated fields. A very
323 limited subset of regex is used to describe these, as given in the
324 following table: </p>
325 <table class="doc_table">
326   <tbody>
327     <tr>
328       <th><b>Character</b></th>
329       <th class="td_left"><b>Meaning</b></th>
330     </tr>
331     <tr>
332       <td><b><code>?</code></b></td>
333       <td class="td_left">The question mark indicates 0 or 1
334 occurrences of the thing preceding it.</td>
335     </tr>
336     <tr>
337       <td><b><code>*</code></b></td>
338       <td class="td_left">The asterisk indicates 0 or more occurrences
339 of the thing preceding it.</td>
340     </tr>
341     <tr>
342       <td><b><code>+</code></b></td>
343       <td class="td_left">The plus sign indicates 1 or more occurrences
344 of the thing preceding it.</td>
345     </tr>
346     <tr>
347       <td><b><code>()</code></b></td>
348       <td class="td_left">Parentheses are used for grouping.</td>
349     </tr>
350     <tr>
351       <td><b><code>,</code></b></td>
352       <td class="td_left">The comma separates sequential fields.</td>
353     </tr>
354   </tbody>
355 </table>
356 <p>So, for example, consider the following specifications:</p>
357 <div class="doc_code">
358 <ol>
359   <li><code>string?</code></li>
360   <li><code>(uint32_vbr,uin32_vbr)+</code></li>
361   <li><code>(unsigned?,uint32_vbr)*</code></li>
362   <li><code>(llist(unsigned))?</code></li>
363 </ol>
364 </div>
365 <p>with the following interpretations:</p>
366 <ol>
367   <li>An optional string. Matches either nothing or a single string</li>
368   <li>One or more pairs of uint32_vbr.</li>
369   <li>Zero or more occurrences of either an unsigned followed by a
370 uint32_vbr or just a uint32_vbr.</li>
371   <li>An optional length list of unsigned values.</li>
372 </ol>
373 </div>
374 <!-- _______________________________________________________________________ -->
375 <div class="doc_subsection"><a name="slots">Slots</a> </div>
376 <div class="doc_text">
377 <p>The bytecode format uses the notion of a "slot" to reference Types
378 and Values. Since the bytecode file is a <em>direct</em> representation of
379 LLVM's intermediate representation, there is a need to represent pointers in
380 the file.  Slots are used for this purpose. For example, if one has the following
381 assembly:
382 </p>
383 <div class="doc_code"><code> %MyType = type { int, sbyte }<br>
384 %MyVar = external global %MyType
385 </code></div>
386 <p>there are two definitions. The definition of <tt>%MyVar</tt> uses <tt>%MyType</tt>.
387 In the C++ IR this linkage between <tt>%MyVar</tt> and <tt>%MyType</tt>
388 is explicit through the use of C++ pointers. In bytecode, however, there's no
389 ability to store memory addresses. Instead, we compute and write out
390 slot numbers for every Type and Value written to the file.</p>
391 <p>A slot number is simply an unsigned 32-bit integer encoded in the variable
392 bit rate scheme (see <a href="#encoding">encoding</a>). This ensures that
393 low slot numbers are encoded in one byte. Through various bits of magic LLVM
394 attempts to always keep the slot numbers low. The first attempt is to associate
395 slot numbers with their "type plane". That is, Values of the same type
396 are written to the bytecode file in a list (sequentially). Their order in 
397 that list determines their slot number. This means that slot #1 doesn't mean
398 anything unless you also specify for which type you want slot #1. Types are
399 handled specially and are always written to the file first (in the <a
400  href="#globaltypes">Global Type Pool</a>) and in such a way that both forward 
401 and backward references of the types can often be resolved with a single pass 
402 through the type pool. </p>
403 <p>Slot numbers are also kept small by rearranging their order. Because
404 of the structure of LLVM, certain values are much more likely to be used
405 frequently in the body of a function. For this reason, a compaction table is
406 provided in the body of a function if its use would make the function body 
407 smaller.  Suppose you have a function body that uses just the types "int*" and
408 "{double}" but uses them thousands of time. Its worthwhile to ensure that the 
409 slot number for these types are low so they can be encoded in a single byte 
410 (via vbr). This is exactly what the compaction table does.</p>
411 </div>
412 <!-- *********************************************************************** -->
413 <div class="doc_section"> <a name="general">General Structure</a> </div>
414 <!-- *********************************************************************** -->
415 <div class="doc_text">
416 <p>This section provides the general structure of the LLVM bytecode
417 file format. The bytecode file format requires blocks to be in a
418 certain order and nested in a particular way so that an LLVM module can
419 be constructed efficiently from the contents of the file. This ordering
420 defines a general structure for bytecode files as shown below. The
421 table below shows the order in which all block types may appear. Please
422 note that some of the blocks are optional and some may be repeated. The
423 structure is fairly loose because optional blocks, if empty, are
424 completely omitted from the file.</p>
425 <table>
426   <tbody>
427     <tr>
428       <th>ID</th>
429       <th>Parent</th>
430       <th>Optional?</th>
431       <th>Repeated?</th>
432       <th>Level</th>
433       <th>Block Type</th>
434       <th>Description</th>
435     </tr>
436     <tr>
437       <td>N/A</td>
438       <td>File</td>
439       <td>No</td>
440       <td>No</td>
441       <td>0</td>
442       <td class="td_left"><a href="#signature">Signature</a></td>
443       <td class="td_left">This contains the file signature (magic
444 number) that identifies the file as LLVM bytecode.</td>
445     </tr>
446     <tr>
447       <td>0x01</td>
448       <td>File</td>
449       <td>No</td>
450       <td>No</td>
451       <td>0</td>
452       <td class="td_left"><a href="#module">Module</a></td>
453       <td class="td_left">This is the top level block in a bytecode
454 file. It contains all the other blocks. </td>
455     </tr>
456     <tr>
457       <td>0x06</td>
458       <td>Module</td>
459       <td>No</td>
460       <td>No</td>
461       <td>1</td>
462       <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#globaltypes">Global&nbsp;Type&nbsp;Pool</a></td>
463       <td class="td_left">This block contains all the global (module)
464 level types.</td>
465     </tr>
466     <tr>
467       <td>0x05</td>
468       <td>Module</td>
469       <td>No</td>
470       <td>No</td>
471       <td>1</td>
472       <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#globalinfo">Module&nbsp;Globals&nbsp;Info</a></td>
473       <td class="td_left">This block contains the type, constness, and
474 linkage for each of the global variables in the module. It also
475 contains the type of the functions and the constant initializers.</td>
476     </tr>
477     <tr>
478       <td>0x03</td>
479       <td>Module</td>
480       <td>Yes</td>
481       <td>No</td>
482       <td>1</td>
483       <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#constantpool">Module&nbsp;Constant&nbsp;Pool</a></td>
484       <td class="td_left">This block contains all the global constants
485 except function arguments, global values and constant strings.</td>
486     </tr>
487     <tr>
488       <td>0x02</td>
489       <td>Module</td>
490       <td>Yes</td>
491       <td>Yes</td>
492       <td>1</td>
493       <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#functiondefs">Function&nbsp;Definitions</a>*</td>
494       <td class="td_left">One function block is written for each
495 function in the module. The function block contains the instructions,
496 compaction table, type constant pool, and symbol table for the function.</td>
497     </tr>
498     <tr>
499       <td>0x03</td>
500       <td>Function</td>
501       <td>Yes</td>
502       <td>No</td>
503       <td>2</td>
504       <td class="td_left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
505  href="#constantpool">Function&nbsp;Constant&nbsp;Pool</a></td>
506       <td class="td_left">Any constants (including types) used solely
507 within the function are emitted here in the function constant pool. </td>
508     </tr>
509     <tr>
510       <td>0x08</td>
511       <td>Function</td>
512       <td>Yes</td>
513       <td>No</td>
514       <td>2</td>
515       <td class="td_left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
516  href="#compactiontable">Compaction&nbsp;Table</a></td>
517       <td class="td_left">This table reduces bytecode size by providing
518 a funtion-local mapping of type and value slot numbers to their global
519 slot numbers</td>
520     </tr>
521     <tr>
522       <td>0x07</td>
523       <td>Function</td>
524       <td>No</td>
525       <td>No</td>
526       <td>2</td>
527       <td class="td_left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
528  href="#instructionlist">Instruction&nbsp;List</a></td>
529       <td class="td_left">This block contains all the instructions of
530 the function. The basic blocks are inferred by terminating
531 instructions. </td>
532     </tr>
533     <tr>
534       <td>0x04</td>
535       <td>Function</td>
536       <td>Yes</td>
537       <td>No</td>
538       <td>2</td>
539       <td class="td_left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
540  href="#symtab">Function&nbsp;Symbol&nbsp;Table</a></td>
541       <td class="td_left">This symbol table provides the names for the
542 function specific values used (basic block labels mostly).</td>
543     </tr>
544     <tr>
545       <td>0x04</td>
546       <td>Module</td>
547       <td>Yes</td>
548       <td>No</td>
549       <td>1</td>
550       <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#symtab">Module&nbsp;Symbol&nbsp;Table</a></td>
551       <td class="td_left">This symbol table provides the names for the
552 various entries in the file that are not function specific (global
553 vars, and functions mostly).</td>
554     </tr>
555   </tbody>
556 </table>
557 <p>Use the links in the table for details about the contents of each of
558 the block types.</p>
559 </div>
560 <!-- *********************************************************************** -->
561 <div class="doc_section"> <a name="blockdefs">Block Definitions</a> </div>
562 <!-- *********************************************************************** -->
563 <div class="doc_text">
564 <p>This section provides the detailed layout of the individual block
565 types in the LLVM bytecode file format. </p>
566 </div>
567 <!-- _______________________________________________________________________ -->
568 <div class="doc_subsection"><a name="signature">Signature Block</a> </div>
569 <div class="doc_text">
570 <p>The signature occurs in every LLVM bytecode file and is always first.
571 It simply provides a few bytes of data to identify the file as being an LLVM
572 bytecode file. This block is always four bytes in length and differs from the
573 other blocks because there is no identifier and no block length at the start
574 of the block. Essentially, this block is just the "magic number" for the file.
575 </p>
576 <table>
577   <tbody>
578     <tr>
579       <th><b>Type</b></th>
580       <th class="td_left"><b>Field Description</b></th>
581     </tr>
582     <tr>
583       <td><a href="#char">char</a></td>
584       <td class="td_left">Constant "l" (0x6C)</td>
585     </tr>
586     <tr>
587       <td><a href="#char">char</a></td>
588       <td class="td_left">Constant "l" (0x6C)</td>
589     </tr>
590     <tr>
591       <td><a href="#char">char</a></td>
592       <td class="td_left">Constant "v" (0x76)</td>
593     </tr>
594     <tr>
595       <td><a href="#char">char</a></td>
596       <td class="td_left">Constant "m" (0x6D)</td>
597     </tr>
598   </tbody>
599 </table>
600 </div>
601 <!-- _______________________________________________________________________ -->
602 <div class="doc_subsection"><a name="module">Module Block</a> </div>
603 <div class="doc_text">
604 <p>The module block contains a small pre-amble and all the other blocks in
605 the file. The table below shows the structure of the module block. Note that it
606 only provides the module identifier, size of the module block, and the format
607 information. Everything else is contained in other blocks, described in other
608 sections.</p>
609 <table>
610   <tbody>
611     <tr>
612       <th><b>Type</b></th>
613       <th class="td_left"><b>Field Description</b></th>
614     </tr>
615     <tr>
616       <td><a href="#block">block</a><br>
617       </td>
618       <td class="td_left">Module Block Identifier (0x01) and Size<br>
619       </td>
620     </tr>
621     <tr>
622       <td><a href="#uint32_vbr">uint32_vbr</a></td>
623       <td class="td_left"><a href="#format">Format Information</a></td>
624     </tr>
625     <tr>
626       <td><a href="#block">block</a></td>
627       <td class="td_left"><a href="#globaltypes">Global Type Pool</a></td>
628     </tr>
629     <tr>
630       <td><a href="#block">block</a></td>
631       <td class="td_left"><a href="#globalinfo">Module Globals Info</a></td>
632     </tr>
633     <tr>
634       <td><a href="#block">block</a></td>
635       <td class="td_left"><a href="#constantpool">Module Constant Pool</a></td>
636     </tr>
637     <tr>
638       <td><a href="#block">block</a>*</td>
639       <td class="td_left"><a href="#functiondefs">Function Definitions</a></td>
640     </tr>
641     <tr>
642       <td><a href="#block">block</a></td>
643       <td class="td_left"><a href="#symboltable">Module Symbol Table</a></td>
644     </tr>
645   </tbody>
646 </table>
647 </div>
648 <!-- _______________________________________________________________________ -->
649 <div class="doc_subsubsection"><a name="format">Format Information</a></div>
650 <div class="doc_text">
651 <p>The format information field is encoded into a <a href="#uint32_vbr">uint32_vbr</a>
652 as shown in the following table.</p>
653 <table>
654   <tbody>
655     <tr>
656       <th><b>Type</b></th>
657       <th class="td_left"><b>Description</b></th>
658     </tr>
659     <tr>
660       <td><a href="#bit">bit(0)</a></td>
661       <td class="td_left">Target is big endian?</td>
662     </tr>
663     <tr>
664       <td><a href="#bit">bit(1)</a></td>
665       <td class="td_left">On target pointers are 64-bit?</td>
666     </tr>
667     <tr>
668       <td><a href="#bit">bit(2)</a></td>
669       <td class="td_left">Target has no endianess?</td>
670     </tr>
671     <tr>
672       <td><a href="#bit">bit(3)</a></td>
673       <td class="td_left">Target has no pointer size?</td>
674     </tr>
675     <tr>
676       <td><a href="#bit">bit(4-31)</a></td>
677       <td class="td_left">Bytecode format version</td>
678     </tr>
679   </tbody>
680 </table>
681 <p>
682 Of particular note, the bytecode format number is simply a 28-bit
683 monotonically increase integer that identifies the version of the bytecode
684 format (which is not directly related to the LLVM release number). The
685 bytecode versions defined so far are (note that this document only
686 describes the latest version, 1.3):</p>
687 <ul>
688   <li>#0: LLVM 1.0 &amp; 1.1</li>
689   <li>#1: LLVM 1.2</li>
690   <li>#2: LLVM 1.2.5 (not released)</li>
691   <li>#3: LLVM 1.3<br>
692   </li>
693 </ul>
694 <p>Note that we plan to eventually expand the target description
695 capabilities
696 of bytecode files to <a href="http://llvm.cs.uiuc.edu/PR263">target
697 triples</a>.
698 </p>
699 </div>
700 <!-- _______________________________________________________________________ -->
701 <div class="doc_subsection"><a name="globaltypes">Global Type Pool</a> </div>
702 <div class="doc_text">
703 <p>The global type pool consists of type definitions. Their order of appearance
704 in the file determines their slot number (0 based). Slot numbers are
705 used to replace pointers in the intermediate representation. Each slot number 
706 uniquely identifies one entry in a type plane (a collection of values of the
707 same type).  Since all values have types and are associated with the order in 
708 which the type pool is written, the global type pool <em>must</em> be written 
709 as the first block of a module. If it is not, attempts to read the file will
710 fail because both forward and backward type resolution will not be possible.</p>
711 <p>The type pool is simply a list of type definitions, as shown in the
712 table below.</p>
713 <table>
714   <tbody>
715     <tr>
716       <th><b>Type</b></th>
717       <th class="td_left"><b>Field Description</b></th>
718     </tr>
719     <tr>
720       <td><a href="#unsigned">block</a></td>
721       <td class="td_left">Type Pool Identifier (0x06) + Size<br>
722       </td>
723     </tr>
724     <tr>
725       <td><a href="#llist">llist</a>(<a href="#type">type</a>)</td>
726       <td class="td_left">A length list of type definitions.</td>
727     </tr>
728   </tbody>
729 </table>
730 </div>
731 <!-- _______________________________________________________________________ -->
732 <div class="doc_subsubsection"><a name="type">Type Definitions</a></div>
733 <div class="doc_text">
734 <p>Types in the type pool are defined using a different format for each kind
735 of type, as given in the following sections.</p>
736 <h3>Primitive Types</h3>
737 <p>The primitive types encompass the basic integer and floating point
738 types</p>
739 <table>
740   <tbody>
741     <tr>
742       <th><b>Type</b></th>
743       <th class="td_left"><b>Description</b></th>
744     </tr>
745     <tr>
746       <td><a href="#uint24_vbr">uint24_vbr</a></td>
747       <td class="td_left">Type ID for the primitive types (values 1 to
748 11) <sup>1</sup></td>
749     </tr>
750   </tbody>
751 </table>
752 Notes:
753 <ol>
754   <li>The values for the Type IDs for the primitive types are provided
755 by the definition of the <code>llvm::Type::TypeID</code> enumeration
756 in <code>include/llvm/Type.h</code>. The enumeration gives the
757 following mapping:
758     <ol>
759       <li>bool</li>
760       <li>ubyte</li>
761       <li>sbyte</li>
762       <li>ushort</li>
763       <li>short</li>
764       <li>uint</li>
765       <li>int</li>
766       <li>ulong</li>
767       <li>long</li>
768       <li>float</li>
769       <li>double</li>
770     </ol>
771   </li>
772 </ol>
773 <h3>Function Types</h3>
774 <table>
775   <tbody>
776     <tr>
777       <th><b>Type</b></th>
778       <th class="td_left"><b>Description</b></th>
779     </tr>
780     <tr>
781       <td><a href="#uint24_vbr">uint24_vbr</a></td>
782       <td class="td_left">Type ID for function types (13)</td>
783     </tr>
784     <tr>
785       <td><a href="#uint24_vbr">uint24_vbr</a></td>
786       <td class="td_left">Slot number of function's return type.</td>
787     </tr>
788     <tr>
789       <td><a href="#llist">llist</a>(<a href="#uint24_vbr">uint24_vbr</a>)</td>
790       <td class="td_left">Slot number of each argument's type.</td>
791     </tr>
792     <tr>
793       <td><a href="#uint32_vbr">uint32_vbr</a>?</td>
794       <td class="td_left">Value 0 if this is a varargs function,
795 missing otherwise.</td>
796     </tr>
797   </tbody>
798 </table>
799 <h3>Structure Types</h3>
800 <table>
801   <tbody>
802     <tr>
803       <th><b>Type</b></th>
804       <th class="td_left"><b>Description</b></th>
805     </tr>
806     <tr>
807       <td><a href="#uint24_vbr">uint24_vbr</a></td>
808       <td class="td_left">Type ID for structure types (14)</td>
809     </tr>
810     <tr>
811       <td><a href="#zlist">zlist</a>(<a href="#uint24_vbr">uint24_vbr</a>)</td>
812       <td class="td_left">Slot number of each of the element's fields.</td>
813     </tr>
814   </tbody>
815 </table>
816 <h3>Array Types</h3>
817 <table>
818   <tbody>
819     <tr>
820       <th><b>Type</b></th>
821       <th class="td_left"><b>Description</b></th>
822     </tr>
823     <tr>
824       <td><a href="#uint24_vbr">uint24_vbr</a></td>
825       <td class="td_left">Type ID for Array Types (15)</td>
826     </tr>
827     <tr>
828       <td><a href="#uint24_vbr">uint24_vbr</a></td>
829       <td class="td_left">Slot number of array's element type.</td>
830     </tr>
831     <tr>
832       <td><a href="#uint32_vbr">uint32_vbr</a></td>
833       <td class="td_left">The number of elements in the array.</td>
834     </tr>
835   </tbody>
836 </table>
837 <h3>Pointer Types</h3>
838 <table>
839   <tbody>
840     <tr>
841       <th><b>Type</b></th>
842       <th class="td_left"><b>Description</b></th>
843     </tr>
844     <tr>
845       <td><a href="#uint24_vbr">uint24_vbr</a></td>
846       <td class="td_left">Type ID For Pointer Types (16)</td>
847     </tr>
848     <tr>
849       <td><a href="#uint24_vbr">uint24_vbr</a></td>
850       <td class="td_left">Slot number of pointer's element type.</td>
851     </tr>
852   </tbody>
853 </table>
854 <h3>Opaque Types</h3>
855 <table>
856   <tbody>
857     <tr>
858       <th><b>Type</b></th>
859       <th class="td_left"><b>Description</b></th>
860     </tr>
861     <tr>
862       <td><a href="#uint24_vbr">uint24_vbr</a></td>
863       <td class="td_left">Type ID For Opaque Types (17)</td>
864     </tr>
865   </tbody>
866 </table>
867 </div>
868 <!-- _______________________________________________________________________ -->
869 <div class="doc_subsection"><a name="globalinfo">Module Global Info</a>
870 </div>
871 <div class="doc_text">
872 <p>The module global info block contains the definitions of all global
873 variables including their initializers and the <em>declaration</em> of
874 all functions. The format is shown in the table below:</p>
875 <table>
876   <tbody>
877     <tr>
878       <th><b>Type</b></th>
879       <th class="td_left"><b>Field Description</b></th>
880     </tr>
881     <tr>
882       <td><a href="#block">block</a></td>
883       <td class="td_left">Module global info identifier (0x05) + size<br>
884       </td>
885     </tr>
886     <tr>
887       <td><a href="#zlist">zlist</a>(<a href="#globalvar">globalvar</a>)</td>
888       <td class="td_left">A zero terminated list of global var
889 definitions occuring in the module.</td>
890     </tr>
891     <tr>
892       <td><a href="#zlist">zlist</a>(<a href="#uint24_vbr">uint24_vbr</a>)</td>
893       <td class="td_left">A zero terminated list of function types
894 occuring in the module.</td>
895     </tr>
896     <tr>
897       <td style="vertical-align: top;"><a href="#llist">llist</a>(<a
898  href="#string">string</a>)<br>
899       </td>
900       <td style="vertical-align: top; text-align: left;">A length list
901 of strings that specify the names of the libraries that this module
902 depends upon.<br>
903       </td>
904     </tr>
905     <tr>
906       <td style="vertical-align: top;"><a href="#string">string</a><br>
907       </td>
908       <td style="vertical-align: top; text-align: left;">The target
909 triple for the module (blank means no target triple specified, i.e. a
910 platform independent module).<br>
911       </td>
912     </tr>
913   </tbody>
914 </table>
915 </div>
916 <!-- _______________________________________________________________________ -->
917 <div class="doc_subsubsection"><a name="globalvar">Global Variable Field</a>
918 </div>
919 <div class="doc_text">
920 <p>Global variables are written using an <a href="#uint32_vbr">uint32_vbr</a>
921 that encodes information about the global variable and a list of the
922 constant initializers for the global var, if any.</p>
923 <p>The table below provides the bit layout of the first <a
924  href="#uint32_vbr">uint32_vbr</a> that describes the global variable.</p>
925 <table>
926   <tbody>
927     <tr>
928       <th><b>Type</b></th>
929       <th class="td_left"><b>Description</b></th>
930     </tr>
931     <tr>
932       <td><a href="#bit">bit(0)</a></td>
933       <td class="td_left">Is constant?</td>
934     </tr>
935     <tr>
936       <td><a href="#bit">bit(1)</a></td>
937       <td class="td_left">Has initializer? Note that this bit
938 determines whether the constant initializer field (described below)
939 follows. </td>
940     </tr>
941     <tr>
942       <td><a href="#bit">bit(2-4)</a></td>
943       <td class="td_left">Linkage type: 0=External, 1=Weak,
944 2=Appending, 3=Internal, 4=LinkOnce</td>
945     </tr>
946     <tr>
947       <td><a href="#bit">bit(5-31)</a></td>
948       <td class="td_left">Slot number of type for the global variable.</td>
949     </tr>
950   </tbody>
951 </table>
952 <p>The table below provides the format of the constant initializers for
953 the global variable field, if it has one.</p>
954 <table>
955   <tbody>
956     <tr>
957       <th><b>Type</b></th>
958       <th class="td_left"><b>Description</b></th>
959     </tr>
960     <tr>
961       <td>(<a href="#zlist">zlist</a>(<a href="#uint32_vbr">uint32_vbr</a>))?
962       </td>
963       <td class="td_left">An optional zero-terminated list of slot
964 numbers of the global variable's constant initializer.</td>
965     </tr>
966   </tbody>
967 </table>
968 </div>
969 <!-- _______________________________________________________________________ -->
970 <div class="doc_subsection"><a name="constantpool">Constant Pool</a> </div>
971 <div class="doc_text">
972 <p>A constant pool defines as set of constant values. There are
973 actually two types of constant pool blocks: one for modules and one for
974 functions. For modules, the block begins with the constant strings
975 encountered anywhere in the module. For functions, the block begins
976 with types only encountered in the function. In both cases the header
977 is identical. The tables that follow, show the header, module constant
978 pool preamble, function constant pool preamble, and the part common to
979 both function and module constant pools.</p>
980 <p><b>Common Block Header</b></p>
981 <table>
982   <tbody>
983     <tr>
984       <th><b>Type</b></th>
985       <th class="td_left"><b>Field Description</b></th>
986     </tr>
987     <tr>
988       <td><a href="#block">block</a></td>
989       <td class="td_left">Constant pool identifier (0x03) + size<br>
990       </td>
991     </tr>
992   </tbody>
993 </table>
994 <p><b>Module Constant Pool Preamble (constant strings)</b></p>
995 <table>
996   <tbody>
997     <tr>
998       <th><b>Type</b></th>
999       <th class="td_left"><b>Field Description</b></th>
1000     </tr>
1001     <tr>
1002       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1003       <td class="td_left">The number of constant strings that follow.</td>
1004     </tr>
1005     <tr>
1006       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1007       <td class="td_left">Zero. This identifies the following "plane"
1008 as containing the constant strings. This is needed to identify it
1009 uniquely from other constant planes that follow. </td>
1010     </tr>
1011     <tr>
1012       <td><a href="#uint24_vbr">uint24_vbr</a>+</td>
1013       <td class="td_left">Slot number of the constant string's type.
1014 Note that the constant string's type implicitly defines the length of
1015 the string. </td>
1016     </tr>
1017   </tbody>
1018 </table>
1019 <p><b>Function Constant Pool Preamble (function types)</b></p>
1020 <p>The structure of the types for functions is identical to the <a
1021  href="#globaltypes">Global Type Pool</a>. Please refer to that section
1022 for the details. </p>
1023 <p><b>Common Part (other constants)</b></p>
1024 <table>
1025   <tbody>
1026     <tr>
1027       <th><b>Type</b></th>
1028       <th class="td_left"><b>Field Description</b></th>
1029     </tr>
1030     <tr>
1031       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1032       <td class="td_left">Number of entries in this type plane.</td>
1033     </tr>
1034     <tr>
1035       <td><a href="#uint24_vbr">uint24_vbr</a></td>
1036       <td class="td_left">Type slot number of this plane.</td>
1037     </tr>
1038     <tr>
1039       <td><a href="#constant">constant</a>+</td>
1040       <td class="td_left">The definition of a constant (see below).</td>
1041     </tr>
1042   </tbody>
1043 </table>
1044 </div>
1045 <!-- _______________________________________________________________________ -->
1046 <div class="doc_subsubsection"><a name="constant">Constant Field</a></div>
1047 <div class="doc_text">
1048 <p>Constants come in many shapes and flavors. The sections that followe
1049 define the format for each of them. All constants start with a <a
1050  href="#uint32_vbr">uint32_vbr</a> encoded integer that provides the
1051 number of operands for the constant. For primitive, structure, and
1052 array constants, this will always be zero since those types of
1053 constants have no operands. In this case, we have the following field
1054 definitions:</p>
1055 <ul>
1056   <li><b>Bool</b>. This is written as an <a href="#uint32_vbr">uint32_vbr</a>
1057 of value 1U or 0U.</li>
1058   <li><b>Signed Integers (sbyte,short,int,long)</b>. These are written
1059 as an <a href="#int64_vbr">int64_vbr</a> with the corresponding value.</li>
1060   <li><b>Unsigned Integers (ubyte,ushort,uint,ulong)</b>. These are
1061 written as an <a href="#uint64_vbr">uint64_vbr</a> with the
1062 corresponding value. </li>
1063   <li><b>Floating Point</b>. Both the float and double types are
1064 written literally in binary format.</li>
1065   <li><b>Arrays</b>. Arrays are written simply as a list of <a
1066  href="#uint32_vbr">uint32_vbr</a> encoded slot numbers to the constant
1067 element values.</li>
1068   <li><b>Structures</b>. Structures are written simply as a list of <a
1069  href="#uint32_vbr">uint32_vbr</a> encoded slot numbers to the constant
1070 field values of the structure.</li>
1071 </ul>
1072 <p>When the number of operands to the constant is non-zero, we have a
1073 constant expression and its field format is provided in the table below.</p>
1074 <table>
1075   <tbody>
1076     <tr>
1077       <th><b>Type</b></th>
1078       <th class="td_left"><b>Field Description</b></th>
1079     </tr>
1080     <tr>
1081       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1082       <td class="td_left">Op code of the instruction for the constant
1083 expression.</td>
1084     </tr>
1085     <tr>
1086       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1087       <td class="td_left">The slot number of the constant value for an
1088 operand.<sup>1</sup></td>
1089     </tr>
1090     <tr>
1091       <td><a href="#uint24_vbr">uint24_vbr</a></td>
1092       <td class="td_left">The slot number for the type of the constant
1093 value for an operand.<sup>1</sup></td>
1094     </tr>
1095   </tbody>
1096 </table>
1097 Notes:
1098 <ol>
1099   <li>Both these fields are repeatable but only in pairs.</li>
1100 </ol>
1101 </div>
1102 <!-- _______________________________________________________________________ -->
1103 <div class="doc_subsection"><a name="functiondefs">Function Definition</a></div>
1104 <div class="doc_text">
1105 <p>Function definitions contain the linkage, constant pool or
1106 compaction table, instruction list, and symbol table for a function.
1107 The following table shows the structure of a function definition.</p>
1108 <table>
1109   <tbody>
1110     <tr>
1111       <th><b>Type</b></th>
1112       <th class="td_left"><b>Field Description</b></th>
1113     </tr>
1114     <tr>
1115       <td><a href="#block">block</a><br>
1116       </td>
1117       <td class="td_left">Function definition block identifier (0x02) +
1118 size<br>
1119       </td>
1120     </tr>
1121     <tr>
1122       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1123       <td class="td_left">The linkage type of the function: 0=External,
1124 1=Weak, 2=Appending, 3=Internal, 4=LinkOnce<sup>1</sup></td>
1125     </tr>
1126     <tr>
1127       <td><a href="#block">block</a></td>
1128       <td class="td_left">The <a href="#constantpool">constant pool</a>
1129 block for this function.<sup>2</sup></td>
1130     </tr>
1131     <tr>
1132       <td><a href="#block">block</a></td>
1133       <td class="td_left">The <a href="#compactiontable">compaction
1134 table</a> block for the function.<sup>2</sup></td>
1135     </tr>
1136     <tr>
1137       <td><a href="#block">block</a></td>
1138       <td class="td_left">The <a href="#instructionlist">instruction
1139 list</a> for the function.</td>
1140     </tr>
1141     <tr>
1142       <td><a href="#block">block</a></td>
1143       <td class="td_left">The function's <a href="#symboltable">symbol
1144 table</a> containing only those symbols pertinent to the function
1145 (mostly block labels).</td>
1146     </tr>
1147   </tbody>
1148 </table>
1149 Notes:
1150 <ol>
1151   <li>Note that if the linkage type is "External" then none of the
1152 other fields will be present as the function is defined elsewhere.</li>
1153   <li>Note that only one of the constant pool or compaction table will
1154 be written. Compaction tables are only written if they will actually
1155 save bytecode space. If not, then a regular constant pool is written.</li>
1156 </ol>
1157 </div>
1158 <!-- _______________________________________________________________________ -->
1159 <div class="doc_subsection"><a name="compactiontable">Compaction Table</a>
1160 </div>
1161 <div class="doc_text">
1162 <p>Compaction tables are part of a function definition. They are merely
1163 a device for reducing the size of bytecode files. The size of a
1164 bytecode file is dependent on the <em>value</em> of the slot numbers
1165 used because larger values use more bytes in the variable bit rate
1166 encoding scheme. Furthermore, the compressed instruction format
1167 reserves only six bits for the type of the instruction. In large
1168 modules, declaring hundreds or thousands of types, the values of the
1169 slot numbers can be quite large. However, functions may use only a
1170 small fraction of the global types. In such cases a compaction table is
1171 created that maps the global type and value slot numbers to smaller
1172 values used by a function. Functions will contain either a
1173 function-specific constant pool <em>or</em> a compaction table but not
1174 both. Compaction tables have the format shown in the table below.</p>
1175 <table>
1176   <tbody>
1177     <tr>
1178       <th><b>Type</b></th>
1179       <th class="td_left"><b>Field Description</b></th>
1180     </tr>
1181     <tr>
1182       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1183       <td class="td_left">The number of types that follow</td>
1184     </tr>
1185     <tr>
1186       <td><a href="#uint24_vbr">uint24_vbr</a>+</td>
1187       <td class="td_left">The slot number in the global type plane of
1188 the type that will be referenced in the function with the index of this
1189 entry in the compaction table.</td>
1190     </tr>
1191     <tr>
1192       <td><a href="#type_len">type_len</a></td>
1193       <td class="td_left">An encoding of the type and number of values
1194 that follow. This field's encoding varies depending on the size of the
1195 type plane. See <a href="#type_len">Type and Length</a> for further
1196 details.</td>
1197     </tr>
1198     <tr>
1199       <td><a href="#uint32_vbr">uint32_vbr</a>+</td>
1200       <td class="td_left">The slot number in the globals of the value
1201 that will be referenced in the function with the index of this entry in
1202 the compaction table</td>
1203     </tr>
1204   </tbody>
1205 </table>
1206 </div>
1207 <!-- _______________________________________________________________________ -->
1208 <div class="doc_subsubsection"><a name="type_len">Type and Length</a></div>
1209 <div class="doc_text">
1210 <p>The type and length of a compaction table type plane is encoded
1211 differently depending on the length of the plane. For planes of length
1212 1 or 2, the length is encoded into bits 0 and 1 of a <a
1213  href="#uint32_vbr">uint32_vbr</a> and the type is encoded into bits
1214 2-31. Because type numbers are often small, this often saves an extra
1215 byte per plane. If the length of the plane is greater than 2 then the
1216 encoding uses a <a href="#uint32_vbr">uint32_vbr</a> for each of the
1217 length and type, in that order.</p>
1218 </div>
1219 <!-- _______________________________________________________________________ -->
1220 <div class="doc_subsection"><a name="instructionlist">Instruction List</a>
1221 </div>
1222 <div class="doc_text">
1223 <p>The instructions in a function are written as a simple list. Basic
1224 blocks are inferred by the terminating instruction types. The format of
1225 the block is given in the following table.</p>
1226 <table>
1227   <tbody>
1228     <tr>
1229       <th><b>Type</b></th>
1230       <th class="td_left"><b>Field Description</b></th>
1231     </tr>
1232     <tr>
1233       <td><a href="#block">block</a><br>
1234       </td>
1235       <td class="td_left">Instruction list identifier (0x07) + size<br>
1236       </td>
1237     </tr>
1238     <tr>
1239       <td><a href="#instruction">instruction</a>+</td>
1240       <td class="td_left">An instruction. Instructions have a variety
1241 of formats. See <a href="#instruction">Instructions</a> for details.</td>
1242     </tr>
1243   </tbody>
1244 </table>
1245 </div>
1246 <!-- _______________________________________________________________________ -->
1247 <div class="doc_subsubsection"><a name="instruction">Instructions</a></div>
1248 <div class="doc_text">
1249 <p>For brevity, instructions are written in one of four formats,
1250 depending on the number of operands to the instruction. Each
1251 instruction begins with a <a href="#uint32_vbr">uint32_vbr</a> that
1252 encodes the type of the instruction as well as other things. The tables
1253 that follow describe the format of this first word of each instruction.</p>
1254 <p><b>Instruction Format 0</b></p>
1255 <p>This format is used for a few instructions that can't easily be
1256 optimized because they have large numbers of operands (e.g. PHI Node or
1257 getelementptr). Each of the opcode, type, and operand fields is as
1258 successive fields.</p>
1259 <table>
1260   <tbody>
1261     <tr>
1262       <th><b>Type</b></th>
1263       <th class="td_left"><b>Field Description</b></th>
1264     </tr>
1265     <tr>
1266       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1267       <td class="td_left">Specifies the opcode of the instruction. Note
1268 that for compatibility with the other instruction formats, the opcode
1269 is shifted left by 2 bits. Bits 0 and 1 must have value zero for this
1270 format.</td>
1271     </tr>
1272     <tr>
1273       <td><a href="#uint24_vbr">uint24_vbr</a></td>
1274       <td class="td_left">Provides the slot number of the result type
1275 of the instruction</td>
1276     </tr>
1277     <tr>
1278       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1279       <td class="td_left">The number of operands that follow.</td>
1280     </tr>
1281     <tr>
1282       <td><a href="#uint32_vbr">uint32_vbr</a>+</td>
1283       <td class="td_left">The slot number of the value(s) for the
1284 operand(s). <sup>1</sup></td>
1285     </tr>
1286   </tbody>
1287 </table>
1288 Notes:
1289 <ol>
1290   <li>Note that if the instruction is a getelementptr and the type of
1291 the operand is a sequential type (array or pointer) then the slot
1292 number is shifted up two bits and the low order bits will encode the
1293 type of index used, as follows: 0=uint, 1=int, 2=ulong, 3=long.</li>
1294 </ol>
1295 <p><b>Instruction Format 1</b></p>
1296 <p>This format encodes the opcode, type and a single operand into a
1297 single <a href="#uint32_vbr">uint32_vbr</a> as follows:</p>
1298 <table>
1299   <tbody>
1300     <tr>
1301       <th><b>Bits</b></th>
1302       <th><b>Type</b></th>
1303       <th class="td_left"><b>Field Description</b></th>
1304     </tr>
1305     <tr>
1306       <td>0-1</td>
1307       <td>constant "1"</td>
1308       <td class="td_left">These two bits must be the value 1 which
1309 identifies this as an instruction of format 1.</td>
1310     </tr>
1311     <tr>
1312       <td>2-7</td>
1313       <td><a href="#opcodes">opcode</a></td>
1314       <td class="td_left">Specifies the opcode of the instruction. Note
1315 that the maximum opcode value is 63.</td>
1316     </tr>
1317     <tr>
1318       <td>8-19</td>
1319       <td><a href="#unsigned">unsigned</a></td>
1320       <td class="td_left">Specifies the slot number of the type for
1321 this instruction. Maximum slot number is 2<sup>12</sup>-1=4095.</td>
1322     </tr>
1323     <tr>
1324       <td>20-31</td>
1325       <td><a href="#unsigned">unsigned</a></td>
1326       <td class="td_left">Specifies the slot number of the value for
1327 the first operand. Maximum slot number is 2<sup>12</sup>-1=4095. Note
1328 that the value 2<sup>12</sup>-1 denotes zero operands.</td>
1329     </tr>
1330   </tbody>
1331 </table>
1332 <p><b>Instruction Format 2</b></p>
1333 <p>This format encodes the opcode, type and two operands into a single <a
1334  href="#uint32_vbr">uint32_vbr</a> as follows:</p>
1335 <table>
1336   <tbody>
1337     <tr>
1338       <th><b>Bits</b></th>
1339       <th><b>Type</b></th>
1340       <th class="td_left"><b>Field Description</b></th>
1341     </tr>
1342     <tr>
1343       <td>0-1</td>
1344       <td>constant "2"</td>
1345       <td class="td_left">These two bits must be the value 2 which
1346 identifies this as an instruction of format 2.</td>
1347     </tr>
1348     <tr>
1349       <td>2-7</td>
1350       <td><a href="#opcodes">opcode</a></td>
1351       <td class="td_left">Specifies the opcode of the instruction. Note
1352 that the maximum opcode value is 63.</td>
1353     </tr>
1354     <tr>
1355       <td>8-15</td>
1356       <td><a href="#unsigned">unsigned</a></td>
1357       <td class="td_left">Specifies the slot number of the type for
1358 this instruction. Maximum slot number is 2<sup>8</sup>-1=255.</td>
1359     </tr>
1360     <tr>
1361       <td>16-23</td>
1362       <td><a href="#unsigned">unsigned</a></td>
1363       <td class="td_left">Specifies the slot number of the value for
1364 the first operand. Maximum slot number is 2<sup>8</sup>-1=255.</td>
1365     </tr>
1366     <tr>
1367       <td>24-31</td>
1368       <td><a href="#unsigned">unsigned</a></td>
1369       <td class="td_left">Specifies the slot number of the value for
1370 the second operand. Maximum slot number is 2<sup>8</sup>-1=255.</td>
1371     </tr>
1372   </tbody>
1373 </table>
1374 <p><b>Instruction Format 3</b></p>
1375 <p>This format encodes the opcode, type and three operands into a
1376 single <a href="#uint32_vbr">uint32_vbr</a> as follows:</p>
1377 <table>
1378   <tbody>
1379     <tr>
1380       <th><b>Bits</b></th>
1381       <th><b>Type</b></th>
1382       <th class="td_left"><b>Field Description</b></th>
1383     </tr>
1384     <tr>
1385       <td>0-1</td>
1386       <td>constant "3"</td>
1387       <td class="td_left">These two bits must be the value 3 which
1388 identifies this as an instruction of format 3.</td>
1389     </tr>
1390     <tr>
1391       <td>2-7</td>
1392       <td><a href="#opcodes">opcode</a></td>
1393       <td class="td_left">Specifies the opcode of the instruction. Note
1394 that the maximum opcode value is 63.</td>
1395     </tr>
1396     <tr>
1397       <td>8-13</td>
1398       <td><a href="#unsigned">unsigned</a></td>
1399       <td class="td_left">Specifies the slot number of the type for
1400 this instruction. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1401     </tr>
1402     <tr>
1403       <td>14-19</td>
1404       <td><a href="#unsigned">unsigned</a></td>
1405       <td class="td_left">Specifies the slot number of the value for
1406 the first operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1407     </tr>
1408     <tr>
1409       <td>20-25</td>
1410       <td><a href="#unsigned">unsigned</a></td>
1411       <td class="td_left">Specifies the slot number of the value for
1412 the second operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1413     </tr>
1414     <tr>
1415       <td>26-31</td>
1416       <td><a href="#unsigned">unsigned</a></td>
1417       <td class="td_left">Specifies the slot number of the value for
1418 the third operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1419     </tr>
1420   </tbody>
1421 </table>
1422 </div>
1423 <!-- _______________________________________________________________________ -->
1424 <div class="doc_subsection"><a name="symtab">Symbol Table</a> </div>
1425 <div class="doc_text">
1426 <p>A symbol table can be put out in conjunction with a module or a function.
1427 A symbol table is a list of type planes. Each type plane starts with the number
1428 of entries in the plane and the type plane's slot number (so the type
1429 can be looked up in the global type pool). For each entry in a type
1430 plane, the slot number of the value and the name associated with that
1431 value are written. The format is given in the table below. </p>
1432 <table>
1433   <tbody>
1434     <tr>
1435       <th><b>Type</b></th>
1436       <th class="td_left"><b>Field Description</b></th>
1437     </tr>
1438     <tr>
1439       <td><a href="#block">block</a><br>
1440       </td>
1441       <td class="td_left">Symbol Table Identifier (0x04)</td>
1442     </tr>
1443     <tr>
1444       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1445       <td class="td_left">Number of entries in type plane</td>
1446     </tr>
1447     <tr>
1448       <td><a href="#symtab_entry">symtab_entry</a>*</td>
1449       <td class="td_left">Provides the slot number of the type and its
1450 name.</td>
1451     </tr>
1452     <tr>
1453       <td><a href="#symtab_plane">symtab_plane</a>*</td>
1454       <td class="td_left">A type plane containing value slot number and
1455 name for all values of the same type.</td>
1456     </tr>
1457   </tbody>
1458 </table>
1459 </div>
1460 <!-- _______________________________________________________________________ -->
1461 <div class="doc_subsubsection"> <a name="symtab_plane">Symbol Table
1462 Plane</a>
1463 </div>
1464 <div class="doc_text">
1465 <p>A symbol table plane provides the symbol table entries for all
1466 values of a common type. The encoding is given in the following table:</p>
1467 <table>
1468   <tbody>
1469     <tr>
1470       <th><b>Type</b></th>
1471       <th class="td_left"><b>Field Description</b></th>
1472     </tr>
1473     <tr>
1474       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1475       <td class="td_left">Number of entries in this plane.</td>
1476     </tr>
1477     <tr>
1478       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1479       <td class="td_left">Slot number of type for this plane.</td>
1480     </tr>
1481     <tr>
1482       <td><a href="#symtab_entry">symtab_entry</a>+</td>
1483       <td class="td_left">The symbol table entries for this plane.</td>
1484     </tr>
1485   </tbody>
1486 </table>
1487 </div>
1488 <!-- _______________________________________________________________________ -->
1489 <div class="doc_subsubsection"> <a name="symtab_entry">Symbol Table
1490 Entry</a>
1491 </div>
1492 <div class="doc_text">
1493 <p>A symbol table entry provides the assocation between a type or
1494 value's slot number and the name given to that type or value. The
1495 format is given in the following table:</p>
1496 <table>
1497   <tbody>
1498     <tr>
1499       <th><b>Type</b></th>
1500       <th class="td_left"><b>Field Description</b></th>
1501     </tr>
1502     <tr>
1503       <td><a href="#uint32_vbr">uint24_vbr</a></td>
1504       <td class="td_left">Slot number of the type or value being given
1505 a name. </td>
1506     </tr>
1507     <tr>
1508       <td><a href="#uint32_vbr">uint32_vbr</a></td>
1509       <td class="td_left">Length of the character array that follows.</td>
1510     </tr>
1511     <tr>
1512       <td><a href="#char">char</a>+</td>
1513       <td class="td_left">The characters of the name.</td>
1514     </tr>
1515   </tbody>
1516 </table>
1517 </div>
1518 <!-- *********************************************************************** -->
1519 <div class="doc_section"> <a name="versiondiffs">Version Differences</a>
1520 </div>
1521 <!-- *********************************************************************** -->
1522 <div class="doc_text">
1523 <p>This section describes the differences in the Bytecode Format across
1524 LLVM
1525 versions. The versions are listed in reverse order because it assumes
1526 the current version is as documented in the previous sections. Each
1527 section here
1528 describes the differences between that version and the one that <i>follows</i>.
1529 </p>
1530 </div>
1531 <!-- _______________________________________________________________________ -->
1532 <div class="doc_subsection"><a name="vers12">Version 1.2 Differences
1533 From 1.3</a></div>
1534 <!-- _______________________________________________________________________ -->
1535 <div class="doc_subsubsection">Type Derives From Value</div>
1536 <div class="doc_text">
1537 <p>In version 1.2, the Type class in the LLVM IR derives from the Value
1538 class. This is not the case in version 1.3. Consequently, in version
1539 1.2 the notion of a "Type Type" was used to write out values that were
1540 Types. The types always occuped plane 12 (corresponding to the
1541 TypeTyID) of any type planed set of values. In 1.3 this representation
1542 is not convenient because the TypeTyID (12) is not present and its
1543 value is now used for LabelTyID. Consequently, the data structures
1544 written that involve types do so by writing all the types first and
1545 then each of the value planes according to those types. In version 1.2,
1546 the types would have been written intermingled with the values.</p>
1547 </div>
1548 <!-- _______________________________________________________________________ -->
1549 <div class="doc_subsubsection">Restricted getelementptr Types</div>
1550 <div class="doc_text">
1551 <p>In version 1.2, the getelementptr instruction required a ubyte type
1552 index for accessing a structure field and a long type index for
1553 accessing an array element. Consequently, it was only possible to
1554 access structures of 255 or fewer elements. Starting in version 1.3,
1555 this restriction was lifted. Structures must now be indexed with uint
1556 constants. Arrays may now be indexed with int, uint, long, or ulong
1557 typed values. The consequence of this was that the bytecode format had
1558 to change in order to accommodate the larger range of structure indices.</p>
1559 </div>
1560 <!-- _______________________________________________________________________ -->
1561 <div class="doc_subsubsection">Short Block Headers</div>
1562 <div class="doc_text">
1563 <p>In version 1.2, block headers were always 8 bytes being comprised of
1564 both an unsigned integer type and an unsigned integer size. For very
1565 small modules, these block headers turn out to be a large fraction of
1566 the total bytecode file size. In an attempt to make these small files
1567 smaller, the type and size information was encoded into a single
1568 unsigned integer (4 bytes) comprised of 5 bits for the block type
1569 (maximum 31 block types) and 27 bits for the block size (max
1570 ~134MBytes). These limits seemed sufficient for any blocks or sizes
1571 forseen in the future. Note that the module block, which encloses all
1572 the other blocks is still written as 8 bytes since bytecode files
1573 larger than 134MBytes might be possible.</p>
1574 </div>
1575 <!-- _______________________________________________________________________ -->
1576 <div class="doc_subsubsection">Dependent Libraries and Target Triples</div>
1577 <div class="doc_text">
1578 <p>In version 1.2, the bytecode format does not store module's target
1579 triple or dependent. These fields have been added to the end of the <a
1580  href="#globalinfo">module global info block</a>. The purpose of these
1581 fields is to allow a front end compiler to specifiy that the generated
1582 module is specific to a particular target triple (operating
1583 system/manufacturer/processor) which makes it non-portable; and to
1584 allow front end compilers to specify the list of libraries that the
1585 module depends on for successful linking.</p>
1586 </div>
1587 <!-- _______________________________________________________________________ -->
1588 <div class="doc_subsubsection">Types Restricted to 24-bits</div>
1589 <div class="doc_text">
1590 <p>In version 1.2, type slot identifiers were written as 32-bit VBR
1591 quantities. In 1.3 this has been reduced to 24-bits in order to ensure
1592 that it is not possible to overflow the type field of a global variable
1593 definition. 24-bits for type slot numbers is deemed sufficient for any
1594 practical use of LLVM.</p>
1595 </div>
1596 <!-- _______________________________________________________________________ -->
1597 <!-- _______________________________________________________________________ -->
1598 <div class="doc_subsection"><a name="vers11">Version 1.1 Differences
1599 From 1.2 </a></div>
1600 <!-- _______________________________________________________________________ -->
1601 <div class="doc_subsubsection">Explicit Primitive Zeros</div>
1602 <div class="doc_text">
1603 <p>In version 1.1, the zero value for primitives was explicitly encoded
1604 into the bytecode format. Since these zero values are constant values
1605 in the LLVM IR and never change, there is no reason to explicitly
1606 encode them. This explicit encoding was removed in version 1.2.</p>
1607 </div>
1608 <!-- _______________________________________________________________________ -->
1609 <div class="doc_subsubsection">Inconsistent Module Global Info</div>
1610 <div class="doc_text">
1611 <p>In version 1.1, the Module Global Info block was not aligned causing
1612 the next block to be read in on an unaligned boundary. This problem was
1613 corrected in version 1.2.<br>
1614 <br>
1615 </p>
1616 </div>
1617 <!-- _______________________________________________________________________ -->
1618 <div class="doc_subsection"><a name="vers10">Version 1.0 Differences
1619 From 1.1</a></div>
1620 <div class="doc_text">
1621 <p>None. Version 1.0 and 1.1 bytecode formats are identical.</p>
1622 </div>
1623 <!-- *********************************************************************** -->
1624 <hr>
1625 <address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1626  src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1627 <a href="http://validator.w3.org/check/referer"><img
1628  src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1629 <a href="mailto:rspencer@x10sys.com">Reid Spencer</a> and <a
1630  href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1631 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
1632 Last modified: $Date$
1633 </address>
1634 <!-- vim: sw=2
1635 -->
1636 </body>
1637 </html>