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