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