blob: b204c837695349428d5034f02c83f116199881d9 [file] [log] [blame]
Reid Spencer9bd2be22004-07-29 00:13:04 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Reid Spencer50026612004-05-22 02:28:36 +00002<html>
3<head>
4 <title>LLVM Bytecode File Format</title>
5 <link rel="stylesheet" href="llvm.css" type="text/css">
Reid Spencer1ab929c2004-07-05 08:18:07 +00006 <style type="text/css">
Reid Spencer2de29992004-08-03 20:21:05 +00007 TR, TD { border: 2px solid gray; padding-left: 4pt; padding-right: 4pt;
8 padding-top: 2pt; padding-bottom: 2pt; }
Reid Spencer1ab929c2004-07-05 08:18:07 +00009 TH { border: 2px solid gray; font-weight: bold; font-size: 105%; }
Reid Spencer2cc36152004-07-05 19:04:27 +000010 TABLE { text-align: center; border: 2px solid black;
Reid Spencer2de29992004-08-03 20:21:05 +000011 border-collapse: collapse; margin-top: 1em; margin-left: 1em;
12 margin-right: 1em; margin-bottom: 1em; }
Reid Spencer2cc36152004-07-05 19:04:27 +000013 .td_left { border: 2px solid gray; text-align: left; }
Reid Spencer50026612004-05-22 02:28:36 +000014 </style>
15</head>
16<body>
Reid Spencer9bd2be22004-07-29 00:13:04 +000017<div class="doc_title"> LLVM Bytecode File Format </div>
Reid Spencer50026612004-05-22 02:28:36 +000018<ol>
19 <li><a href="#abstract">Abstract</a></li>
Reid Spencer1ab929c2004-07-05 08:18:07 +000020 <li><a href="#concepts">Concepts</a>
Reid Spencer50026612004-05-22 02:28:36 +000021 <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>
Reid Spencer82c46712004-07-07 13:34:26 +000026 <li><a href="#vbr">Variable Bit-Rate Encoding</a></li>
Reid Spencer1ab929c2004-07-05 08:18:07 +000027 <li><a href="#encoding">Encoding Primitives</a></li>
28 <li><a href="#slots">Slots</a></li>
29 </ol>
30 </li>
Reid Spencer51f31e02004-07-05 22:28:02 +000031 <li><a href="#general">General Structure</a> </li>
32 <li><a href="#blockdefs">Block Definitions</a>
Reid Spencer1ab929c2004-07-05 08:18:07 +000033 <ol>
Reid Spencerb39021b2004-05-23 17:05:09 +000034 <li><a href="#signature">Signature Block</a></li>
35 <li><a href="#module">Module Block</a></li>
Reid Spencer1ab929c2004-07-05 08:18:07 +000036 <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>
Reid Spencer50026612004-05-22 02:28:36 +000043 </ol>
44 </li>
Reid Spencer7c76d332004-06-08 07:41:41 +000045 <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>
Reid Spencer50026612004-05-22 02:28:36 +000052</ol>
Chris Lattner8dabb502004-05-25 17:44:58 +000053<div class="doc_author">
54<p>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a>
55</p>
Reid Spencer50026612004-05-22 02:28:36 +000056</div>
57<!-- *********************************************************************** -->
58<div class="doc_section"> <a name="abstract">Abstract </a></div>
59<!-- *********************************************************************** -->
60<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +000061<p>This document describes the LLVM bytecode file format. It specifies
62the binary encoding rules of the bytecode file format so that
63equivalent systems can encode bytecode files correctly. The LLVM
64bytecode representation is used to store the intermediate
65representation on disk in compacted form.</p>
66<p>The LLVM bytecode format may change in the future, but LLVM will
67always be backwards compatible with older formats. This document will
68only describe the most current version of the bytecode format. See <a
69 href="#versiondiffs">Version Differences</a> for the details on how
70the current version is different from previous versions.</p>
Reid Spencer50026612004-05-22 02:28:36 +000071</div>
72<!-- *********************************************************************** -->
Reid Spencer1ab929c2004-07-05 08:18:07 +000073<div class="doc_section"> <a name="concepts">Concepts</a> </div>
Reid Spencer50026612004-05-22 02:28:36 +000074<!-- *********************************************************************** -->
75<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +000076<p>This section describes the general concepts of the bytecode file
77format without getting into specific layout details. It is recommended
78that you read this section thoroughly before interpreting the detailed
79descriptions.</p>
Reid Spencer50026612004-05-22 02:28:36 +000080</div>
81<!-- _______________________________________________________________________ -->
82<div class="doc_subsection"><a name="blocks">Blocks</a> </div>
83<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +000084<p>LLVM bytecode files consist simply of a sequence of blocks of bytes
85using a binary encoding Each block begins with an header of two
86unsigned integers. The first value identifies the type of block and the
87second value provides the size of the block in bytes. The block
88identifier is used because it is possible for entire blocks to be
89omitted from the file if they are empty. The block identifier helps the
90reader determine which kind of block is next in the file. Note that
91blocks can be nested within other blocks.</p>
92<p> All blocks are variable length, and the block header specifies the
93size of the block. All blocks begin on a byte index that is aligned to
94an even 32-bit boundary. That is, the first block is 32-bit aligned
95because it starts at offset 0. Each block is padded with zero fill
96bytes to ensure that the next block also starts on a 32-bit boundary.</p>
Reid Spencer50026612004-05-22 02:28:36 +000097</div>
98<!-- _______________________________________________________________________ -->
99<div class="doc_subsection"><a name="lists">Lists</a> </div>
100<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000101<p>LLVM Bytecode blocks often contain lists of things of a similar
102type. For example, a function contains a list of instructions and a
103function type contains a list of argument types. There are two basic
104types of lists: length lists (<a href="#llist">llist</a>), and null
105terminated lists (<a href="#zlist">zlist</a>), as described below in
106the <a href="#encoding">Encoding Primitives</a>.</p>
Reid Spencer50026612004-05-22 02:28:36 +0000107</div>
108<!-- _______________________________________________________________________ -->
109<div class="doc_subsection"><a name="fields">Fields</a> </div>
110<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000111<p>Fields are units of information that LLVM knows how to write atomically. Most
112fields have a uniform length or some kind of length indication built into their
113encoding. For example, a constant string (array of bytes) is written simply as
114the length followed by the characters. Although this is similar to a list,
115constant strings are treated atomically and are thus fields.</p>
Reid Spencer50026612004-05-22 02:28:36 +0000116<p>Fields use a condensed bit format specific to the type of information
117they must contain. As few bits as possible are written for each field. The
Reid Spencer9bd2be22004-07-29 00:13:04 +0000118sections that follow will provide the details on how these fields are
Reid Spencer50026612004-05-22 02:28:36 +0000119written and how the bits are to be interpreted.</p>
120</div>
121<!-- _______________________________________________________________________ -->
Reid Spencer1ab929c2004-07-05 08:18:07 +0000122<div class="doc_subsection"><a name="align">Alignment</a> </div>
Reid Spencer7aa940d2004-05-25 15:47:57 +0000123<div class="doc_text">
Reid Spencer267660f2004-08-03 20:33:56 +0000124 <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>
Chris Lattner8dabb502004-05-25 17:44:58 +0000128</div>
Reid Spencer7aa940d2004-05-25 15:47:57 +0000129<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +0000130<div class="doc_subsection"><a name="vbr">Variable Bit-Rate Encoding</a>
Reid Spencer82c46712004-07-07 13:34:26 +0000131</div>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000132<div class="doc_text">
133<p>Most of the values written to LLVM bytecode files are small integers. To
134minimize the number of bytes written for these quantities, an encoding scheme
135similar to UTF-8 is used to write integer data. The scheme is known as
136variable bit rate (vbr) encoding. In this encoding, the high bit of
137each byte is used to indicate if more bytes follow. If (byte &amp;
1380x80) is non-zero in any given byte, it means there is another byte
139immediately following that also contributes to the value. For the final
140byte (byte &amp; 0x80) is false (the high bit is not set). In each byte
141only the low seven bits contribute to the value. Consequently 32-bit
142quantities can take from one to <em>five</em> bytes to encode. In
143general, 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
204the 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
206encoding, but with the sign bit as the low order bit instead of the
207high order bit. This allows small negative quantities to be encoded
208efficiently. For example, -3
209is encoded as "((3 &lt;&lt; 1) | 1)" and 3 is encoded as "(3 &lt;&lt;
2101) | 0)", emitted with the standard vbr encoding above.</p>
211</div>
Reid Spencer82c46712004-07-07 13:34:26 +0000212<!-- _______________________________________________________________________ -->
213<div class="doc_subsection"><a name="encoding">Encoding Primitives</a> </div>
214<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000215<p>Each field in the bytecode format is encoded into the file using a
216small set of primitive formats. The table below defines the encoding
217rules for the various primitives used and gives them each a type name.
218The 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
220the suffix <em>_vbr</em> indicates a quantity that is encoded using
221variable 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
Reid Spencerb39021b2004-05-23 17:05:09 +0000231 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>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000234 </tr>
235 <tr>
Reid Spencer301fe482004-08-03 20:57:56 +0000236 <td style="vertical-align: top;"><a name="uint24_vbr">
237 <b>uint24_vbr</b></a></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000238 <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>
Reid Spencerf08561f2004-08-03 19:20:18 +0000244 <td class="td_left">A 32-bit unsigned integer that occupies from one to
245 five bytes using variable bit rate encoding.</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000246 </tr>
247 <tr>
248 <td><a name="uint64_vbr"><b>uint64_vbr</b></a></td>
Reid Spencerf08561f2004-08-03 19:20:18 +0000249 <td class="td_left">A 64-bit unsigned integer that occupies from one to ten
250 bytes using variable bit rate encoding.</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000251 </tr>
252 <tr>
253 <td><a name="int64_vbr"><b>int64_vbr</b></a></td>
Reid Spencerf08561f2004-08-03 19:20:18 +0000254 <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>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000256 </tr>
257 <tr>
Reid Spencer8996e552004-08-17 00:49:03 +0000258 <td><a name="opcode"><b>opcode</b></a></td>
259 <td class="td_left">An enumerated integer value used in the instruction
260 format that identifies the LLVM instruction opcode referenced. The
261 specific values used depend on the version of LLVM you're using. See the
262 <a
263 href="http://llvm.org/cvsweb/cvsweb.cgi/llvm/include/llvm/Instruction.def">
264 <tt>include/llvm/Instruction.def</tt></a> file for the definitive set of
265 opcode values used for your release. The opcode values are the first
266 argument to the various <tt>HANDLE_*_INST</tt> macros.
267 </td>
268 <tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000269 <td><a name="char"><b>char</b></a></td>
Reid Spencerf08561f2004-08-03 19:20:18 +0000270 <td class="td_left">A single unsigned character encoded into one byte</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000271 </tr>
272 <tr>
273 <td><a name="bit"><b>bit(n-m)</b></a></td>
Reid Spencerf08561f2004-08-03 19:20:18 +0000274 <td class="td_left">A set of bit within some larger integer field. The values
275 of <code>n</code> and <code>m</code> specify the inclusive range of bits
276 that define the subfield. The value for <code>m</code> may be omitted if
277 its the same as <code>n</code>.</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000278 </tr>
279 <tr>
280 <td style="vertical-align: top;"><b><a name="float"><b>float</b></a></b></td>
Reid Spencerf08561f2004-08-03 19:20:18 +0000281 <td style="vertical-align: top; text-align: left;">A floating point value encoded
282 as a 32-bit IEEE value written in little-endian form.<br>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000283 </td>
284 </tr>
285 <tr>
286 <td style="vertical-align: top;"><b><b><a name="double"><b>double</b></a></b></b></td>
Reid Spencerf08561f2004-08-03 19:20:18 +0000287 <td style="vertical-align: top; text-align: left;">A floating point value encoded
288 as a64-bit IEEE value written in little-endian form</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000289 </tr>
290 <tr>
291 <td><a name="string"><b>string</b></a></td>
292 <td class="td_left">A uint32_vbr indicating the type of the
293constant string which also includes its length, immediately followed by
294the characters of the string. There is no terminating null byte in the
295string.</td>
296 </tr>
297 <tr>
298 <td><a name="data"><b>data</b></a></td>
299 <td class="td_left">An arbitrarily long segment of data to which
300no interpretation is implied. This is used for constant initializers.<br>
301 </td>
302 </tr>
303 <tr>
304 <td><a name="llist"><b>llist(x)</b></a></td>
305 <td class="td_left">A length list of x. This means the list is
306encoded as an <a href="#uint32_vbr">uint32_vbr</a> providing the
307length of the list, followed by a sequence of that many "x" items. This
308implies that the reader should iterate the number of times provided by
309the length.</td>
310 </tr>
311 <tr>
312 <td><a name="zlist"><b>zlist(x)</b></a></td>
313 <td class="td_left">A zero-terminated list of x. This means the
314list is encoded as a sequence of an indeterminate number of "x" items,
315followed by an <a href="#uint32_vbr">uint32_vbr</a> terminating value.
316This implies that none of the "x" items can have a zero value (or else
317the list terminates).</td>
318 </tr>
319 <tr>
320 <td><a name="block"><b>block</b></a></td>
321 <td class="td_left">A block of data that is logically related. A
322block is an unsigned 32-bit integer that encodes the type of the block
323in the low 5 bits and the size of the block in the high 27 bits. The
324length does not include the block header or any alignment bytes at the
325end of the block. Blocks may compose other blocks. </td>
326 </tr>
327 </tbody>
Reid Spencerb39021b2004-05-23 17:05:09 +0000328</table>
329</div>
330<!-- _______________________________________________________________________ -->
Reid Spencer82c46712004-07-07 13:34:26 +0000331<div class="doc_subsection"><a name="notation">Field Notation</a> </div>
332<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000333<p>In the detailed block and field descriptions that follow, a regex
334like notation is used to describe optional and repeated fields. A very
335limited subset of regex is used to describe these, as given in the
336following table: </p>
337<table class="doc_table">
338 <tbody>
Reid Spencer82c46712004-07-07 13:34:26 +0000339 <tr>
340 <th><b>Character</b></th>
341 <th class="td_left"><b>Meaning</b></th>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000342 </tr>
343 <tr>
Reid Spencer82c46712004-07-07 13:34:26 +0000344 <td><b><code>?</code></b></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000345 <td class="td_left">The question mark indicates 0 or 1
346occurrences of the thing preceding it.</td>
347 </tr>
348 <tr>
Reid Spencer82c46712004-07-07 13:34:26 +0000349 <td><b><code>*</code></b></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000350 <td class="td_left">The asterisk indicates 0 or more occurrences
351of the thing preceding it.</td>
352 </tr>
353 <tr>
Reid Spencer82c46712004-07-07 13:34:26 +0000354 <td><b><code>+</code></b></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000355 <td class="td_left">The plus sign indicates 1 or more occurrences
356of the thing preceding it.</td>
357 </tr>
358 <tr>
Reid Spencer82c46712004-07-07 13:34:26 +0000359 <td><b><code>()</code></b></td>
360 <td class="td_left">Parentheses are used for grouping.</td>
Reid Spencer82c46712004-07-07 13:34:26 +0000361 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000362 <tr>
363 <td><b><code>,</code></b></td>
364 <td class="td_left">The comma separates sequential fields.</td>
365 </tr>
366 </tbody>
367</table>
368<p>So, for example, consider the following specifications:</p>
369<div class="doc_code">
370<ol>
371 <li><code>string?</code></li>
372 <li><code>(uint32_vbr,uin32_vbr)+</code></li>
373 <li><code>(unsigned?,uint32_vbr)*</code></li>
374 <li><code>(llist(unsigned))?</code></li>
375</ol>
Reid Spencer82c46712004-07-07 13:34:26 +0000376</div>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000377<p>with the following interpretations:</p>
378<ol>
379 <li>An optional string. Matches either nothing or a single string</li>
380 <li>One or more pairs of uint32_vbr.</li>
381 <li>Zero or more occurrences of either an unsigned followed by a
382uint32_vbr or just a uint32_vbr.</li>
383 <li>An optional length list of unsigned values.</li>
384</ol>
385</div>
Reid Spencer82c46712004-07-07 13:34:26 +0000386<!-- _______________________________________________________________________ -->
Reid Spencer1ab929c2004-07-05 08:18:07 +0000387<div class="doc_subsection"><a name="slots">Slots</a> </div>
Reid Spencer50026612004-05-22 02:28:36 +0000388<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000389<p>The bytecode format uses the notion of a "slot" to reference Types
390and Values. Since the bytecode file is a <em>direct</em> representation of
391LLVM's intermediate representation, there is a need to represent pointers in
392the file. Slots are used for this purpose. For example, if one has the following
393assembly:
Reid Spencer1ab929c2004-07-05 08:18:07 +0000394</p>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000395<div class="doc_code"><code> %MyType = type { int, sbyte }<br>
396%MyVar = external global %MyType
Reid Spencer82c46712004-07-07 13:34:26 +0000397</code></div>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000398<p>there are two definitions. The definition of <tt>%MyVar</tt> uses <tt>%MyType</tt>.
399In the C++ IR this linkage between <tt>%MyVar</tt> and <tt>%MyType</tt>
400is explicit through the use of C++ pointers. In bytecode, however, there's no
401ability to store memory addresses. Instead, we compute and write out
402slot numbers for every Type and Value written to the file.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000403<p>A slot number is simply an unsigned 32-bit integer encoded in the variable
404bit rate scheme (see <a href="#encoding">encoding</a>). This ensures that
405low slot numbers are encoded in one byte. Through various bits of magic LLVM
406attempts to always keep the slot numbers low. The first attempt is to associate
Reid Spencer9bd2be22004-07-29 00:13:04 +0000407slot numbers with their "type plane". That is, Values of the same type
408are written to the bytecode file in a list (sequentially). Their order in
409that list determines their slot number. This means that slot #1 doesn't mean
410anything unless you also specify for which type you want slot #1. Types are
411handled specially and are always written to the file first (in the <a
412 href="#globaltypes">Global Type Pool</a>) and in such a way that both forward
413and backward references of the types can often be resolved with a single pass
414through the type pool. </p>
415<p>Slot numbers are also kept small by rearranging their order. Because
416of the structure of LLVM, certain values are much more likely to be used
417frequently in the body of a function. For this reason, a compaction table is
418provided in the body of a function if its use would make the function body
419smaller. Suppose you have a function body that uses just the types "int*" and
420"{double}" but uses them thousands of time. Its worthwhile to ensure that the
421slot number for these types are low so they can be encoded in a single byte
422(via vbr). This is exactly what the compaction table does.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000423</div>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000424<!-- *********************************************************************** -->
Reid Spencer51f31e02004-07-05 22:28:02 +0000425<div class="doc_section"> <a name="general">General Structure</a> </div>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000426<!-- *********************************************************************** -->
427<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000428<p>This section provides the general structure of the LLVM bytecode
429file format. The bytecode file format requires blocks to be in a
430certain order and nested in a particular way so that an LLVM module can
431be constructed efficiently from the contents of the file. This ordering
432defines a general structure for bytecode files as shown below. The
433table below shows the order in which all block types may appear. Please
434note that some of the blocks are optional and some may be repeated. The
435structure is fairly loose because optional blocks, if empty, are
436completely omitted from the file.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000437<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000438 <tbody>
439 <tr>
440 <th>ID</th>
441 <th>Parent</th>
442 <th>Optional?</th>
443 <th>Repeated?</th>
444 <th>Level</th>
445 <th>Block Type</th>
446 <th>Description</th>
447 </tr>
448 <tr>
449 <td>N/A</td>
450 <td>File</td>
451 <td>No</td>
452 <td>No</td>
453 <td>0</td>
454 <td class="td_left"><a href="#signature">Signature</a></td>
455 <td class="td_left">This contains the file signature (magic
456number) that identifies the file as LLVM bytecode.</td>
457 </tr>
458 <tr>
459 <td>0x01</td>
460 <td>File</td>
461 <td>No</td>
462 <td>No</td>
463 <td>0</td>
464 <td class="td_left"><a href="#module">Module</a></td>
465 <td class="td_left">This is the top level block in a bytecode
466file. It contains all the other blocks. </td>
467 </tr>
468 <tr>
469 <td>0x06</td>
470 <td>Module</td>
471 <td>No</td>
472 <td>No</td>
473 <td>1</td>
474 <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#globaltypes">Global&nbsp;Type&nbsp;Pool</a></td>
475 <td class="td_left">This block contains all the global (module)
476level types.</td>
477 </tr>
478 <tr>
479 <td>0x05</td>
480 <td>Module</td>
481 <td>No</td>
482 <td>No</td>
483 <td>1</td>
484 <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#globalinfo">Module&nbsp;Globals&nbsp;Info</a></td>
485 <td class="td_left">This block contains the type, constness, and
486linkage for each of the global variables in the module. It also
487contains the type of the functions and the constant initializers.</td>
488 </tr>
489 <tr>
490 <td>0x03</td>
491 <td>Module</td>
492 <td>Yes</td>
493 <td>No</td>
494 <td>1</td>
495 <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#constantpool">Module&nbsp;Constant&nbsp;Pool</a></td>
496 <td class="td_left">This block contains all the global constants
497except function arguments, global values and constant strings.</td>
498 </tr>
499 <tr>
500 <td>0x02</td>
501 <td>Module</td>
502 <td>Yes</td>
503 <td>Yes</td>
504 <td>1</td>
505 <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#functiondefs">Function&nbsp;Definitions</a>*</td>
506 <td class="td_left">One function block is written for each
507function in the module. The function block contains the instructions,
508compaction table, type constant pool, and symbol table for the function.</td>
509 </tr>
510 <tr>
511 <td>0x03</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="#constantpool">Function&nbsp;Constant&nbsp;Pool</a></td>
518 <td class="td_left">Any constants (including types) used solely
519within the function are emitted here in the function constant pool. </td>
520 </tr>
521 <tr>
522 <td>0x08</td>
523 <td>Function</td>
524 <td>Yes</td>
525 <td>No</td>
526 <td>2</td>
527 <td class="td_left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
528 href="#compactiontable">Compaction&nbsp;Table</a></td>
529 <td class="td_left">This table reduces bytecode size by providing
530a funtion-local mapping of type and value slot numbers to their global
531slot numbers</td>
532 </tr>
533 <tr>
534 <td>0x07</td>
535 <td>Function</td>
536 <td>No</td>
537 <td>No</td>
538 <td>2</td>
539 <td class="td_left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
540 href="#instructionlist">Instruction&nbsp;List</a></td>
541 <td class="td_left">This block contains all the instructions of
542the function. The basic blocks are inferred by terminating
543instructions. </td>
544 </tr>
545 <tr>
546 <td>0x04</td>
547 <td>Function</td>
548 <td>Yes</td>
549 <td>No</td>
550 <td>2</td>
551 <td class="td_left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
552 href="#symtab">Function&nbsp;Symbol&nbsp;Table</a></td>
553 <td class="td_left">This symbol table provides the names for the
554function specific values used (basic block labels mostly).</td>
555 </tr>
556 <tr>
557 <td>0x04</td>
558 <td>Module</td>
559 <td>Yes</td>
560 <td>No</td>
561 <td>1</td>
562 <td class="td_left">&nbsp;&nbsp;&nbsp;<a href="#symtab">Module&nbsp;Symbol&nbsp;Table</a></td>
563 <td class="td_left">This symbol table provides the names for the
564various entries in the file that are not function specific (global
565vars, and functions mostly).</td>
566 </tr>
567 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000568</table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000569<p>Use the links in the table for details about the contents of each of
570the block types.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000571</div>
Reid Spencer50026612004-05-22 02:28:36 +0000572<!-- *********************************************************************** -->
Reid Spencer51f31e02004-07-05 22:28:02 +0000573<div class="doc_section"> <a name="blockdefs">Block Definitions</a> </div>
Reid Spencer50026612004-05-22 02:28:36 +0000574<!-- *********************************************************************** -->
575<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000576<p>This section provides the detailed layout of the individual block
577types in the LLVM bytecode file format. </p>
Reid Spencer50026612004-05-22 02:28:36 +0000578</div>
Reid Spencer50026612004-05-22 02:28:36 +0000579<!-- _______________________________________________________________________ -->
Reid Spencerb39021b2004-05-23 17:05:09 +0000580<div class="doc_subsection"><a name="signature">Signature Block</a> </div>
Reid Spencer50026612004-05-22 02:28:36 +0000581<div class="doc_text">
Chris Lattner2b905652004-05-24 05:35:17 +0000582<p>The signature occurs in every LLVM bytecode file and is always first.
Reid Spencerb39021b2004-05-23 17:05:09 +0000583It simply provides a few bytes of data to identify the file as being an LLVM
584bytecode file. This block is always four bytes in length and differs from the
585other blocks because there is no identifier and no block length at the start
586of the block. Essentially, this block is just the "magic number" for the file.
Reid Spencer9bd2be22004-07-29 00:13:04 +0000587</p>
Reid Spencer2cc36152004-07-05 19:04:27 +0000588<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000589 <tbody>
590 <tr>
591 <th><b>Type</b></th>
592 <th class="td_left"><b>Field Description</b></th>
593 </tr>
594 <tr>
595 <td><a href="#char">char</a></td>
596 <td class="td_left">Constant "l" (0x6C)</td>
597 </tr>
598 <tr>
599 <td><a href="#char">char</a></td>
600 <td class="td_left">Constant "l" (0x6C)</td>
601 </tr>
602 <tr>
603 <td><a href="#char">char</a></td>
604 <td class="td_left">Constant "v" (0x76)</td>
605 </tr>
606 <tr>
607 <td><a href="#char">char</a></td>
608 <td class="td_left">Constant "m" (0x6D)</td>
609 </tr>
610 </tbody>
Reid Spencerb39021b2004-05-23 17:05:09 +0000611</table>
612</div>
613<!-- _______________________________________________________________________ -->
614<div class="doc_subsection"><a name="module">Module Block</a> </div>
615<div class="doc_text">
616<p>The module block contains a small pre-amble and all the other blocks in
Reid Spencer1ab929c2004-07-05 08:18:07 +0000617the file. The table below shows the structure of the module block. Note that it
618only provides the module identifier, size of the module block, and the format
619information. Everything else is contained in other blocks, described in other
620sections.</p>
Reid Spencer2cc36152004-07-05 19:04:27 +0000621<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000622 <tbody>
623 <tr>
624 <th><b>Type</b></th>
625 <th class="td_left"><b>Field Description</b></th>
626 </tr>
627 <tr>
Reid Spencer5bc74d52004-08-16 19:24:36 +0000628 <td><a href="#unsigned">unsigned</a><br></td>
629 <td class="td_left"><a href="#mod_header">Module Block Identifier
630 (0x01)</a></td>
631 </tr>
632 <tr>
633 <td><a href="#unsigned">unsigned</a></td>
634 <td class="td_left"><a href="#mod_header">Module Block Size</a></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000635 </tr>
636 <tr>
637 <td><a href="#uint32_vbr">uint32_vbr</a></td>
638 <td class="td_left"><a href="#format">Format Information</a></td>
639 </tr>
640 <tr>
641 <td><a href="#block">block</a></td>
642 <td class="td_left"><a href="#globaltypes">Global Type Pool</a></td>
643 </tr>
644 <tr>
645 <td><a href="#block">block</a></td>
646 <td class="td_left"><a href="#globalinfo">Module Globals Info</a></td>
647 </tr>
648 <tr>
649 <td><a href="#block">block</a></td>
650 <td class="td_left"><a href="#constantpool">Module Constant Pool</a></td>
651 </tr>
652 <tr>
653 <td><a href="#block">block</a>*</td>
654 <td class="td_left"><a href="#functiondefs">Function Definitions</a></td>
655 </tr>
656 <tr>
657 <td><a href="#block">block</a></td>
Reid Spencer8996e552004-08-17 00:49:03 +0000658 <td class="td_left"><a href="#symtab">Module Symbol Table</a></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000659 </tr>
660 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000661</table>
662</div>
Reid Spencer5bc74d52004-08-16 19:24:36 +0000663
664<!-- _______________________________________________________________________ -->
665<div class="doc_subsubsection"><a name="mod_header">Module Block Header</a></div>
666<div class="doc_text">
667 <p>The block header for the module block uses a longer format than the other
668 blocks in a bytecode file. Specifically, instead of encoding the type and size
669 of the block into a 32-bit integer with 5-bits for type and 27-bits for size,
670 the module block header uses two 32-bit unsigned values, one for type, and one
671 for size. While the 2<sup>27</sup> byte limit on block size is sufficient for the blocks
672 contained in the module, it isn't sufficient for the module block itself
673 because we want to ensure that bytecode files as large as 2<sup>32</sup> bytes
674 are possible. For this reason, the module block (and only the module block)
675 uses a long format header.</p>
676</div>
677
Reid Spencer1ab929c2004-07-05 08:18:07 +0000678<!-- _______________________________________________________________________ -->
679<div class="doc_subsubsection"><a name="format">Format Information</a></div>
680<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000681<p>The format information field is encoded into a <a href="#uint32_vbr">uint32_vbr</a>
682as shown in the following table.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000683<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000684 <tbody>
685 <tr>
686 <th><b>Type</b></th>
687 <th class="td_left"><b>Description</b></th>
688 </tr>
689 <tr>
690 <td><a href="#bit">bit(0)</a></td>
691 <td class="td_left">Target is big endian?</td>
692 </tr>
693 <tr>
694 <td><a href="#bit">bit(1)</a></td>
695 <td class="td_left">On target pointers are 64-bit?</td>
696 </tr>
697 <tr>
698 <td><a href="#bit">bit(2)</a></td>
699 <td class="td_left">Target has no endianess?</td>
700 </tr>
701 <tr>
702 <td><a href="#bit">bit(3)</a></td>
703 <td class="td_left">Target has no pointer size?</td>
704 </tr>
705 <tr>
706 <td><a href="#bit">bit(4-31)</a></td>
707 <td class="td_left">Bytecode format version</td>
708 </tr>
709 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000710</table>
711<p>
712Of particular note, the bytecode format number is simply a 28-bit
713monotonically increase integer that identifies the version of the bytecode
Reid Spencer9bd2be22004-07-29 00:13:04 +0000714format (which is not directly related to the LLVM release number). The
715bytecode versions defined so far are (note that this document only
716describes the latest version, 1.3):</p>
Chris Lattner2b905652004-05-24 05:35:17 +0000717<ul>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000718 <li>#0: LLVM 1.0 &amp; 1.1</li>
719 <li>#1: LLVM 1.2</li>
720 <li>#2: LLVM 1.2.5 (not released)</li>
721 <li>#3: LLVM 1.3<br>
722 </li>
Chris Lattner2b905652004-05-24 05:35:17 +0000723</ul>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000724<p>Note that we plan to eventually expand the target description
725capabilities
726of bytecode files to <a href="http://llvm.cs.uiuc.edu/PR263">target
727triples</a>.
Reid Spencer1ab929c2004-07-05 08:18:07 +0000728</p>
Reid Spencer50026612004-05-22 02:28:36 +0000729</div>
730<!-- _______________________________________________________________________ -->
Reid Spencer1ab929c2004-07-05 08:18:07 +0000731<div class="doc_subsection"><a name="globaltypes">Global Type Pool</a> </div>
Reid Spencer50026612004-05-22 02:28:36 +0000732<div class="doc_text">
Chris Lattner2b905652004-05-24 05:35:17 +0000733<p>The global type pool consists of type definitions. Their order of appearance
Reid Spencer9bd2be22004-07-29 00:13:04 +0000734in the file determines their slot number (0 based). Slot numbers are
735used to replace pointers in the intermediate representation. Each slot number
736uniquely identifies one entry in a type plane (a collection of values of the
737same type). Since all values have types and are associated with the order in
738which the type pool is written, the global type pool <em>must</em> be written
739as the first block of a module. If it is not, attempts to read the file will
740fail because both forward and backward type resolution will not be possible.</p>
741<p>The type pool is simply a list of type definitions, as shown in the
742table below.</p>
Reid Spencer2cc36152004-07-05 19:04:27 +0000743<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000744 <tbody>
745 <tr>
746 <th><b>Type</b></th>
747 <th class="td_left"><b>Field Description</b></th>
748 </tr>
749 <tr>
750 <td><a href="#unsigned">block</a></td>
751 <td class="td_left">Type Pool Identifier (0x06) + Size<br>
752 </td>
753 </tr>
754 <tr>
755 <td><a href="#llist">llist</a>(<a href="#type">type</a>)</td>
756 <td class="td_left">A length list of type definitions.</td>
757 </tr>
758 </tbody>
Reid Spencerb39021b2004-05-23 17:05:09 +0000759</table>
Reid Spencer50026612004-05-22 02:28:36 +0000760</div>
761<!-- _______________________________________________________________________ -->
Reid Spencer1ab929c2004-07-05 08:18:07 +0000762<div class="doc_subsubsection"><a name="type">Type Definitions</a></div>
763<div class="doc_text">
Reid Spencer82c46712004-07-07 13:34:26 +0000764<p>Types in the type pool are defined using a different format for each kind
765of type, as given in the following sections.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000766<h3>Primitive Types</h3>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000767<p>The primitive types encompass the basic integer and floating point
768types</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000769<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000770 <tbody>
771 <tr>
772 <th><b>Type</b></th>
773 <th class="td_left"><b>Description</b></th>
774 </tr>
775 <tr>
776 <td><a href="#uint24_vbr">uint24_vbr</a></td>
777 <td class="td_left">Type ID for the primitive types (values 1 to
77811) <sup>1</sup></td>
779 </tr>
780 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000781</table>
Reid Spencer2cc36152004-07-05 19:04:27 +0000782Notes:
783<ol>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000784 <li>The values for the Type IDs for the primitive types are provided
785by the definition of the <code>llvm::Type::TypeID</code> enumeration
786in <code>include/llvm/Type.h</code>. The enumeration gives the
787following mapping:
788 <ol>
789 <li>bool</li>
790 <li>ubyte</li>
791 <li>sbyte</li>
792 <li>ushort</li>
793 <li>short</li>
794 <li>uint</li>
795 <li>int</li>
796 <li>ulong</li>
797 <li>long</li>
798 <li>float</li>
799 <li>double</li>
800 </ol>
801 </li>
Reid Spencer2cc36152004-07-05 19:04:27 +0000802</ol>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000803<h3>Function Types</h3>
804<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000805 <tbody>
806 <tr>
807 <th><b>Type</b></th>
808 <th class="td_left"><b>Description</b></th>
809 </tr>
810 <tr>
811 <td><a href="#uint24_vbr">uint24_vbr</a></td>
812 <td class="td_left">Type ID for function types (13)</td>
813 </tr>
814 <tr>
815 <td><a href="#uint24_vbr">uint24_vbr</a></td>
816 <td class="td_left">Slot number of function's return type.</td>
817 </tr>
818 <tr>
819 <td><a href="#llist">llist</a>(<a href="#uint24_vbr">uint24_vbr</a>)</td>
Reid Spencer82c46712004-07-07 13:34:26 +0000820 <td class="td_left">Slot number of each argument's type.</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000821 </tr>
822 <tr>
823 <td><a href="#uint32_vbr">uint32_vbr</a>?</td>
824 <td class="td_left">Value 0 if this is a varargs function,
825missing otherwise.</td>
826 </tr>
827 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000828</table>
829<h3>Structure Types</h3>
830<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000831 <tbody>
832 <tr>
833 <th><b>Type</b></th>
834 <th class="td_left"><b>Description</b></th>
835 </tr>
836 <tr>
837 <td><a href="#uint24_vbr">uint24_vbr</a></td>
838 <td class="td_left">Type ID for structure types (14)</td>
839 </tr>
840 <tr>
841 <td><a href="#zlist">zlist</a>(<a href="#uint24_vbr">uint24_vbr</a>)</td>
842 <td class="td_left">Slot number of each of the element's fields.</td>
843 </tr>
844 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000845</table>
846<h3>Array Types</h3>
847<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000848 <tbody>
849 <tr>
850 <th><b>Type</b></th>
851 <th class="td_left"><b>Description</b></th>
852 </tr>
853 <tr>
854 <td><a href="#uint24_vbr">uint24_vbr</a></td>
855 <td class="td_left">Type ID for Array Types (15)</td>
856 </tr>
857 <tr>
858 <td><a href="#uint24_vbr">uint24_vbr</a></td>
859 <td class="td_left">Slot number of array's element type.</td>
860 </tr>
861 <tr>
862 <td><a href="#uint32_vbr">uint32_vbr</a></td>
863 <td class="td_left">The number of elements in the array.</td>
864 </tr>
865 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000866</table>
867<h3>Pointer Types</h3>
868<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000869 <tbody>
870 <tr>
871 <th><b>Type</b></th>
872 <th class="td_left"><b>Description</b></th>
873 </tr>
874 <tr>
875 <td><a href="#uint24_vbr">uint24_vbr</a></td>
876 <td class="td_left">Type ID For Pointer Types (16)</td>
877 </tr>
878 <tr>
879 <td><a href="#uint24_vbr">uint24_vbr</a></td>
880 <td class="td_left">Slot number of pointer's element type.</td>
881 </tr>
882 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000883</table>
884<h3>Opaque Types</h3>
885<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000886 <tbody>
887 <tr>
888 <th><b>Type</b></th>
889 <th class="td_left"><b>Description</b></th>
890 </tr>
891 <tr>
892 <td><a href="#uint24_vbr">uint24_vbr</a></td>
893 <td class="td_left">Type ID For Opaque Types (17)</td>
894 </tr>
895 </tbody>
Reid Spencer1ab929c2004-07-05 08:18:07 +0000896</table>
897</div>
898<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +0000899<div class="doc_subsection"><a name="globalinfo">Module Global Info</a>
900</div>
Reid Spencer50026612004-05-22 02:28:36 +0000901<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000902<p>The module global info block contains the definitions of all global
903variables including their initializers and the <em>declaration</em> of
904all functions. The format is shown in the table below:</p>
905<table>
906 <tbody>
Reid Spencer2cc36152004-07-05 19:04:27 +0000907 <tr>
908 <th><b>Type</b></th>
909 <th class="td_left"><b>Field Description</b></th>
Reid Spencer2cc36152004-07-05 19:04:27 +0000910 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000911 <tr>
912 <td><a href="#block">block</a></td>
913 <td class="td_left">Module global info identifier (0x05) + size<br>
914 </td>
915 </tr>
916 <tr>
917 <td><a href="#zlist">zlist</a>(<a href="#globalvar">globalvar</a>)</td>
918 <td class="td_left">A zero terminated list of global var
919definitions occuring in the module.</td>
920 </tr>
921 <tr>
922 <td><a href="#zlist">zlist</a>(<a href="#uint24_vbr">uint24_vbr</a>)</td>
923 <td class="td_left">A zero terminated list of function types
924occuring in the module.</td>
925 </tr>
926 <tr>
927 <td style="vertical-align: top;"><a href="#llist">llist</a>(<a
928 href="#string">string</a>)<br>
929 </td>
930 <td style="vertical-align: top; text-align: left;">A length list
931of strings that specify the names of the libraries that this module
932depends upon.<br>
933 </td>
934 </tr>
935 <tr>
936 <td style="vertical-align: top;"><a href="#string">string</a><br>
937 </td>
938 <td style="vertical-align: top; text-align: left;">The target
939triple for the module (blank means no target triple specified, i.e. a
940platform independent module).<br>
941 </td>
942 </tr>
943 </tbody>
944</table>
Reid Spencer50026612004-05-22 02:28:36 +0000945</div>
Reid Spencer2cc36152004-07-05 19:04:27 +0000946<!-- _______________________________________________________________________ -->
947<div class="doc_subsubsection"><a name="globalvar">Global Variable Field</a>
948</div>
949<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +0000950<p>Global variables are written using an <a href="#uint32_vbr">uint32_vbr</a>
951that encodes information about the global variable and a list of the
952constant initializers for the global var, if any.</p>
953<p>The table below provides the bit layout of the first <a
954 href="#uint32_vbr">uint32_vbr</a> that describes the global variable.</p>
955<table>
956 <tbody>
Reid Spencer82c46712004-07-07 13:34:26 +0000957 <tr>
958 <th><b>Type</b></th>
959 <th class="td_left"><b>Description</b></th>
Reid Spencer82c46712004-07-07 13:34:26 +0000960 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +0000961 <tr>
962 <td><a href="#bit">bit(0)</a></td>
963 <td class="td_left">Is constant?</td>
964 </tr>
965 <tr>
966 <td><a href="#bit">bit(1)</a></td>
967 <td class="td_left">Has initializer? Note that this bit
968determines whether the constant initializer field (described below)
969follows. </td>
970 </tr>
971 <tr>
972 <td><a href="#bit">bit(2-4)</a></td>
973 <td class="td_left">Linkage type: 0=External, 1=Weak,
9742=Appending, 3=Internal, 4=LinkOnce</td>
975 </tr>
976 <tr>
977 <td><a href="#bit">bit(5-31)</a></td>
978 <td class="td_left">Slot number of type for the global variable.</td>
979 </tr>
980 </tbody>
981</table>
982<p>The table below provides the format of the constant initializers for
983the global variable field, if it has one.</p>
984<table>
985 <tbody>
986 <tr>
987 <th><b>Type</b></th>
988 <th class="td_left"><b>Description</b></th>
989 </tr>
990 <tr>
991 <td>(<a href="#zlist">zlist</a>(<a href="#uint32_vbr">uint32_vbr</a>))?
992 </td>
993 <td class="td_left">An optional zero-terminated list of slot
994numbers of the global variable's constant initializer.</td>
995 </tr>
996 </tbody>
997</table>
Reid Spencer2cc36152004-07-05 19:04:27 +0000998</div>
Reid Spencer50026612004-05-22 02:28:36 +0000999<!-- _______________________________________________________________________ -->
Reid Spencer1ab929c2004-07-05 08:18:07 +00001000<div class="doc_subsection"><a name="constantpool">Constant Pool</a> </div>
Reid Spencer50026612004-05-22 02:28:36 +00001001<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001002<p>A constant pool defines as set of constant values. There are
1003actually two types of constant pool blocks: one for modules and one for
1004functions. For modules, the block begins with the constant strings
1005encountered anywhere in the module. For functions, the block begins
1006with types only encountered in the function. In both cases the header
1007is identical. The tables that follow, show the header, module constant
1008pool preamble, function constant pool preamble, and the part common to
1009both function and module constant pools.</p>
1010<p><b>Common Block Header</b></p>
1011<table>
1012 <tbody>
Reid Spencer2cc36152004-07-05 19:04:27 +00001013 <tr>
1014 <th><b>Type</b></th>
1015 <th class="td_left"><b>Field Description</b></th>
Reid Spencer2cc36152004-07-05 19:04:27 +00001016 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001017 <tr>
1018 <td><a href="#block">block</a></td>
1019 <td class="td_left">Constant pool identifier (0x03) + size<br>
1020 </td>
1021 </tr>
1022 </tbody>
1023</table>
1024<p><b>Module Constant Pool Preamble (constant strings)</b></p>
1025<table>
1026 <tbody>
Reid Spencer2cc36152004-07-05 19:04:27 +00001027 <tr>
1028 <th><b>Type</b></th>
1029 <th class="td_left"><b>Field Description</b></th>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001030 </tr>
1031 <tr>
Reid Spencer2cc36152004-07-05 19:04:27 +00001032 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1033 <td class="td_left">The number of constant strings that follow.</td>
Reid Spencer2cc36152004-07-05 19:04:27 +00001034 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001035 <tr>
1036 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1037 <td class="td_left">Zero. This identifies the following "plane"
1038as containing the constant strings. This is needed to identify it
1039uniquely from other constant planes that follow. </td>
1040 </tr>
1041 <tr>
1042 <td><a href="#uint24_vbr">uint24_vbr</a>+</td>
1043 <td class="td_left">Slot number of the constant string's type.
1044Note that the constant string's type implicitly defines the length of
1045the string. </td>
1046 </tr>
1047 </tbody>
1048</table>
1049<p><b>Function Constant Pool Preamble (function types)</b></p>
1050<p>The structure of the types for functions is identical to the <a
1051 href="#globaltypes">Global Type Pool</a>. Please refer to that section
1052for the details. </p>
1053<p><b>Common Part (other constants)</b></p>
1054<table>
1055 <tbody>
Reid Spencer2cc36152004-07-05 19:04:27 +00001056 <tr>
1057 <th><b>Type</b></th>
1058 <th class="td_left"><b>Field Description</b></th>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001059 </tr>
1060 <tr>
Reid Spencer2cc36152004-07-05 19:04:27 +00001061 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1062 <td class="td_left">Number of entries in this type plane.</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001063 </tr>
1064 <tr>
1065 <td><a href="#uint24_vbr">uint24_vbr</a></td>
Reid Spencer2cc36152004-07-05 19:04:27 +00001066 <td class="td_left">Type slot number of this plane.</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001067 </tr>
1068 <tr>
Reid Spencer82c46712004-07-07 13:34:26 +00001069 <td><a href="#constant">constant</a>+</td>
Reid Spencer2cc36152004-07-05 19:04:27 +00001070 <td class="td_left">The definition of a constant (see below).</td>
1071 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001072 </tbody>
1073</table>
Reid Spencer2cc36152004-07-05 19:04:27 +00001074</div>
1075<!-- _______________________________________________________________________ -->
1076<div class="doc_subsubsection"><a name="constant">Constant Field</a></div>
1077<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001078<p>Constants come in many shapes and flavors. The sections that followe
1079define the format for each of them. All constants start with a <a
1080 href="#uint32_vbr">uint32_vbr</a> encoded integer that provides the
1081number of operands for the constant. For primitive, structure, and
1082array constants, this will always be zero since those types of
1083constants have no operands. In this case, we have the following field
1084definitions:</p>
1085<ul>
1086 <li><b>Bool</b>. This is written as an <a href="#uint32_vbr">uint32_vbr</a>
1087of value 1U or 0U.</li>
1088 <li><b>Signed Integers (sbyte,short,int,long)</b>. These are written
1089as an <a href="#int64_vbr">int64_vbr</a> with the corresponding value.</li>
1090 <li><b>Unsigned Integers (ubyte,ushort,uint,ulong)</b>. These are
1091written as an <a href="#uint64_vbr">uint64_vbr</a> with the
1092corresponding value. </li>
1093 <li><b>Floating Point</b>. Both the float and double types are
1094written literally in binary format.</li>
1095 <li><b>Arrays</b>. Arrays are written simply as a list of <a
1096 href="#uint32_vbr">uint32_vbr</a> encoded slot numbers to the constant
1097element values.</li>
1098 <li><b>Structures</b>. Structures are written simply as a list of <a
1099 href="#uint32_vbr">uint32_vbr</a> encoded slot numbers to the constant
1100field values of the structure.</li>
1101</ul>
1102<p>When the number of operands to the constant is non-zero, we have a
1103constant expression and its field format is provided in the table below.</p>
1104<table>
1105 <tbody>
Reid Spencer2cc36152004-07-05 19:04:27 +00001106 <tr>
1107 <th><b>Type</b></th>
1108 <th class="td_left"><b>Field Description</b></th>
Reid Spencer2cc36152004-07-05 19:04:27 +00001109 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001110 <tr>
1111 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1112 <td class="td_left">Op code of the instruction for the constant
1113expression.</td>
1114 </tr>
1115 <tr>
1116 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1117 <td class="td_left">The slot number of the constant value for an
1118operand.<sup>1</sup></td>
1119 </tr>
1120 <tr>
1121 <td><a href="#uint24_vbr">uint24_vbr</a></td>
1122 <td class="td_left">The slot number for the type of the constant
1123value for an operand.<sup>1</sup></td>
1124 </tr>
1125 </tbody>
1126</table>
1127Notes:
1128<ol>
1129 <li>Both these fields are repeatable but only in pairs.</li>
1130</ol>
Reid Spencer50026612004-05-22 02:28:36 +00001131</div>
1132<!-- _______________________________________________________________________ -->
Reid Spencer51f31e02004-07-05 22:28:02 +00001133<div class="doc_subsection"><a name="functiondefs">Function Definition</a></div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001134<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001135<p>Function definitions contain the linkage, constant pool or
1136compaction table, instruction list, and symbol table for a function.
1137The following table shows the structure of a function definition.</p>
1138<table>
1139 <tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001140 <tr>
1141 <th><b>Type</b></th>
1142 <th class="td_left"><b>Field Description</b></th>
Reid Spencer51f31e02004-07-05 22:28:02 +00001143 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001144 <tr>
1145 <td><a href="#block">block</a><br>
1146 </td>
1147 <td class="td_left">Function definition block identifier (0x02) +
1148size<br>
1149 </td>
1150 </tr>
1151 <tr>
1152 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1153 <td class="td_left">The linkage type of the function: 0=External,
11541=Weak, 2=Appending, 3=Internal, 4=LinkOnce<sup>1</sup></td>
1155 </tr>
1156 <tr>
1157 <td><a href="#block">block</a></td>
1158 <td class="td_left">The <a href="#constantpool">constant pool</a>
1159block for this function.<sup>2</sup></td>
1160 </tr>
1161 <tr>
1162 <td><a href="#block">block</a></td>
1163 <td class="td_left">The <a href="#compactiontable">compaction
1164table</a> block for the function.<sup>2</sup></td>
1165 </tr>
1166 <tr>
1167 <td><a href="#block">block</a></td>
1168 <td class="td_left">The <a href="#instructionlist">instruction
1169list</a> for the function.</td>
1170 </tr>
1171 <tr>
1172 <td><a href="#block">block</a></td>
Reid Spencer8996e552004-08-17 00:49:03 +00001173 <td class="td_left">The function's <a href="#symtab">symbol
Reid Spencer9bd2be22004-07-29 00:13:04 +00001174table</a> containing only those symbols pertinent to the function
1175(mostly block labels).</td>
1176 </tr>
1177 </tbody>
1178</table>
1179Notes:
1180<ol>
1181 <li>Note that if the linkage type is "External" then none of the
1182other fields will be present as the function is defined elsewhere.</li>
1183 <li>Note that only one of the constant pool or compaction table will
1184be written. Compaction tables are only written if they will actually
1185save bytecode space. If not, then a regular constant pool is written.</li>
1186</ol>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001187</div>
1188<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001189<div class="doc_subsection"><a name="compactiontable">Compaction Table</a>
1190</div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001191<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001192<p>Compaction tables are part of a function definition. They are merely
1193a device for reducing the size of bytecode files. The size of a
1194bytecode file is dependent on the <em>value</em> of the slot numbers
1195used because larger values use more bytes in the variable bit rate
1196encoding scheme. Furthermore, the compressed instruction format
1197reserves only six bits for the type of the instruction. In large
1198modules, declaring hundreds or thousands of types, the values of the
1199slot numbers can be quite large. However, functions may use only a
1200small fraction of the global types. In such cases a compaction table is
1201created that maps the global type and value slot numbers to smaller
1202values used by a function. Functions will contain either a
1203function-specific constant pool <em>or</em> a compaction table but not
1204both. Compaction tables have the format shown in the table below.</p>
1205<table>
1206 <tbody>
Reid Spencer2cc36152004-07-05 19:04:27 +00001207 <tr>
1208 <th><b>Type</b></th>
1209 <th class="td_left"><b>Field Description</b></th>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001210 </tr>
1211 <tr>
Reid Spencer2cc36152004-07-05 19:04:27 +00001212 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1213 <td class="td_left">The number of types that follow</td>
Reid Spencer2cc36152004-07-05 19:04:27 +00001214 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001215 <tr>
1216 <td><a href="#uint24_vbr">uint24_vbr</a>+</td>
1217 <td class="td_left">The slot number in the global type plane of
1218the type that will be referenced in the function with the index of this
1219entry in the compaction table.</td>
1220 </tr>
1221 <tr>
1222 <td><a href="#type_len">type_len</a></td>
1223 <td class="td_left">An encoding of the type and number of values
1224that follow. This field's encoding varies depending on the size of the
1225type plane. See <a href="#type_len">Type and Length</a> for further
1226details.</td>
1227 </tr>
1228 <tr>
1229 <td><a href="#uint32_vbr">uint32_vbr</a>+</td>
1230 <td class="td_left">The slot number in the globals of the value
1231that will be referenced in the function with the index of this entry in
1232the compaction table</td>
1233 </tr>
1234 </tbody>
1235</table>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001236</div>
Reid Spencer2cc36152004-07-05 19:04:27 +00001237<!-- _______________________________________________________________________ -->
1238<div class="doc_subsubsection"><a name="type_len">Type and Length</a></div>
1239<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001240<p>The type and length of a compaction table type plane is encoded
1241differently depending on the length of the plane. For planes of length
12421 or 2, the length is encoded into bits 0 and 1 of a <a
1243 href="#uint32_vbr">uint32_vbr</a> and the type is encoded into bits
12442-31. Because type numbers are often small, this often saves an extra
1245byte per plane. If the length of the plane is greater than 2 then the
1246encoding uses a <a href="#uint32_vbr">uint32_vbr</a> for each of the
1247length and type, in that order.</p>
Reid Spencer2cc36152004-07-05 19:04:27 +00001248</div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001249<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001250<div class="doc_subsection"><a name="instructionlist">Instruction List</a>
1251</div>
Reid Spencer50026612004-05-22 02:28:36 +00001252<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001253<p>The instructions in a function are written as a simple list. Basic
1254blocks are inferred by the terminating instruction types. The format of
1255the block is given in the following table.</p>
1256<table>
1257 <tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001258 <tr>
1259 <th><b>Type</b></th>
1260 <th class="td_left"><b>Field Description</b></th>
Reid Spencer51f31e02004-07-05 22:28:02 +00001261 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001262 <tr>
1263 <td><a href="#block">block</a><br>
1264 </td>
1265 <td class="td_left">Instruction list identifier (0x07) + size<br>
1266 </td>
1267 </tr>
1268 <tr>
1269 <td><a href="#instruction">instruction</a>+</td>
1270 <td class="td_left">An instruction. Instructions have a variety
1271of formats. See <a href="#instruction">Instructions</a> for details.</td>
1272 </tr>
1273 </tbody>
1274</table>
Reid Spencer50026612004-05-22 02:28:36 +00001275</div>
Reid Spencer51f31e02004-07-05 22:28:02 +00001276<!-- _______________________________________________________________________ -->
1277<div class="doc_subsubsection"><a name="instruction">Instructions</a></div>
1278<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001279<p>For brevity, instructions are written in one of four formats,
1280depending on the number of operands to the instruction. Each
1281instruction begins with a <a href="#uint32_vbr">uint32_vbr</a> that
1282encodes the type of the instruction as well as other things. The tables
Reid Spencer8996e552004-08-17 00:49:03 +00001283that follow describe the format of this first part of each instruction.</p>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001284<p><b>Instruction Format 0</b></p>
1285<p>This format is used for a few instructions that can't easily be
Reid Spencer8996e552004-08-17 00:49:03 +00001286shortened because they have large numbers of operands (e.g. PHI Node or
1287getelementptr). Each of the opcode, type, and operand fields is found in
Reid Spencer9bd2be22004-07-29 00:13:04 +00001288successive fields.</p>
1289<table>
1290 <tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001291 <tr>
1292 <th><b>Type</b></th>
1293 <th class="td_left"><b>Field Description</b></th>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001294 </tr>
1295 <tr>
Reid Spencer51f31e02004-07-05 22:28:02 +00001296 <td><a href="#uint32_vbr">uint32_vbr</a></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001297 <td class="td_left">Specifies the opcode of the instruction. Note
1298that for compatibility with the other instruction formats, the opcode
1299is shifted left by 2 bits. Bits 0 and 1 must have value zero for this
1300format.</td>
1301 </tr>
1302 <tr>
1303 <td><a href="#uint24_vbr">uint24_vbr</a></td>
1304 <td class="td_left">Provides the slot number of the result type
1305of the instruction</td>
1306 </tr>
1307 <tr>
Reid Spencer51f31e02004-07-05 22:28:02 +00001308 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1309 <td class="td_left">The number of operands that follow.</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001310 </tr>
1311 <tr>
Reid Spencer82c46712004-07-07 13:34:26 +00001312 <td><a href="#uint32_vbr">uint32_vbr</a>+</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001313 <td class="td_left">The slot number of the value(s) for the
1314operand(s). <sup>1</sup></td>
Reid Spencer51f31e02004-07-05 22:28:02 +00001315 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001316 </tbody>
1317</table>
1318Notes:
1319<ol>
1320 <li>Note that if the instruction is a getelementptr and the type of
1321the operand is a sequential type (array or pointer) then the slot
1322number is shifted up two bits and the low order bits will encode the
1323type of index used, as follows: 0=uint, 1=int, 2=ulong, 3=long.</li>
1324</ol>
1325<p><b>Instruction Format 1</b></p>
1326<p>This format encodes the opcode, type and a single operand into a
1327single <a href="#uint32_vbr">uint32_vbr</a> as follows:</p>
1328<table>
1329 <tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001330 <tr>
1331 <th><b>Bits</b></th>
1332 <th><b>Type</b></th>
1333 <th class="td_left"><b>Field Description</b></th>
Reid Spencer51f31e02004-07-05 22:28:02 +00001334 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001335 <tr>
1336 <td>0-1</td>
1337 <td>constant "1"</td>
1338 <td class="td_left">These two bits must be the value 1 which
1339identifies this as an instruction of format 1.</td>
1340 </tr>
1341 <tr>
1342 <td>2-7</td>
Reid Spencer8996e552004-08-17 00:49:03 +00001343 <td><a href="#opcode">opcode</a></td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001344 <td class="td_left">Specifies the opcode of the instruction. Note
1345that the maximum opcode value is 63.</td>
1346 </tr>
1347 <tr>
1348 <td>8-19</td>
1349 <td><a href="#unsigned">unsigned</a></td>
1350 <td class="td_left">Specifies the slot number of the type for
1351this instruction. Maximum slot number is 2<sup>12</sup>-1=4095.</td>
1352 </tr>
1353 <tr>
1354 <td>20-31</td>
1355 <td><a href="#unsigned">unsigned</a></td>
1356 <td class="td_left">Specifies the slot number of the value for
1357the first operand. Maximum slot number is 2<sup>12</sup>-1=4095. Note
1358that the value 2<sup>12</sup>-1 denotes zero operands.</td>
1359 </tr>
1360 </tbody>
1361</table>
1362<p><b>Instruction Format 2</b></p>
1363<p>This format encodes the opcode, type and two operands into a single <a
1364 href="#uint32_vbr">uint32_vbr</a> as follows:</p>
1365<table>
1366 <tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001367 <tr>
1368 <th><b>Bits</b></th>
1369 <th><b>Type</b></th>
1370 <th class="td_left"><b>Field Description</b></th>
Reid Spencer51f31e02004-07-05 22:28:02 +00001371 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001372 <tr>
1373 <td>0-1</td>
1374 <td>constant "2"</td>
1375 <td class="td_left">These two bits must be the value 2 which
1376identifies this as an instruction of format 2.</td>
1377 </tr>
1378 <tr>
1379 <td>2-7</td>
1380 <td><a href="#opcodes">opcode</a></td>
1381 <td class="td_left">Specifies the opcode of the instruction. Note
1382that the maximum opcode value is 63.</td>
1383 </tr>
1384 <tr>
1385 <td>8-15</td>
1386 <td><a href="#unsigned">unsigned</a></td>
1387 <td class="td_left">Specifies the slot number of the type for
1388this instruction. Maximum slot number is 2<sup>8</sup>-1=255.</td>
1389 </tr>
1390 <tr>
1391 <td>16-23</td>
1392 <td><a href="#unsigned">unsigned</a></td>
1393 <td class="td_left">Specifies the slot number of the value for
1394the first operand. Maximum slot number is 2<sup>8</sup>-1=255.</td>
1395 </tr>
1396 <tr>
1397 <td>24-31</td>
1398 <td><a href="#unsigned">unsigned</a></td>
1399 <td class="td_left">Specifies the slot number of the value for
1400the second operand. Maximum slot number is 2<sup>8</sup>-1=255.</td>
1401 </tr>
1402 </tbody>
1403</table>
1404<p><b>Instruction Format 3</b></p>
1405<p>This format encodes the opcode, type and three operands into a
1406single <a href="#uint32_vbr">uint32_vbr</a> as follows:</p>
1407<table>
1408 <tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001409 <tr>
1410 <th><b>Bits</b></th>
1411 <th><b>Type</b></th>
1412 <th class="td_left"><b>Field Description</b></th>
Reid Spencer51f31e02004-07-05 22:28:02 +00001413 </tr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001414 <tr>
1415 <td>0-1</td>
1416 <td>constant "3"</td>
1417 <td class="td_left">These two bits must be the value 3 which
1418identifies this as an instruction of format 3.</td>
1419 </tr>
1420 <tr>
1421 <td>2-7</td>
1422 <td><a href="#opcodes">opcode</a></td>
1423 <td class="td_left">Specifies the opcode of the instruction. Note
1424that the maximum opcode value is 63.</td>
1425 </tr>
1426 <tr>
1427 <td>8-13</td>
1428 <td><a href="#unsigned">unsigned</a></td>
1429 <td class="td_left">Specifies the slot number of the type for
1430this instruction. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1431 </tr>
1432 <tr>
1433 <td>14-19</td>
1434 <td><a href="#unsigned">unsigned</a></td>
1435 <td class="td_left">Specifies the slot number of the value for
1436the first operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1437 </tr>
1438 <tr>
1439 <td>20-25</td>
1440 <td><a href="#unsigned">unsigned</a></td>
1441 <td class="td_left">Specifies the slot number of the value for
1442the second operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1443 </tr>
1444 <tr>
1445 <td>26-31</td>
1446 <td><a href="#unsigned">unsigned</a></td>
1447 <td class="td_left">Specifies the slot number of the value for
1448the third operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
1449 </tr>
1450 </tbody>
1451</table>
Reid Spencer51f31e02004-07-05 22:28:02 +00001452</div>
Reid Spencer50026612004-05-22 02:28:36 +00001453<!-- _______________________________________________________________________ -->
Reid Spencerb39021b2004-05-23 17:05:09 +00001454<div class="doc_subsection"><a name="symtab">Symbol Table</a> </div>
Reid Spencer50026612004-05-22 02:28:36 +00001455<div class="doc_text">
Reid Spencerb39021b2004-05-23 17:05:09 +00001456<p>A symbol table can be put out in conjunction with a module or a function.
1457A symbol table is a list of type planes. Each type plane starts with the number
Reid Spencer9bd2be22004-07-29 00:13:04 +00001458of entries in the plane and the type plane's slot number (so the type
1459can be looked up in the global type pool). For each entry in a type
1460plane, the slot number of the value and the name associated with that
1461value are written. The format is given in the table below. </p>
Reid Spencer2cc36152004-07-05 19:04:27 +00001462<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001463 <tbody>
1464 <tr>
1465 <th><b>Type</b></th>
1466 <th class="td_left"><b>Field Description</b></th>
1467 </tr>
1468 <tr>
1469 <td><a href="#block">block</a><br>
1470 </td>
1471 <td class="td_left">Symbol Table Identifier (0x04)</td>
1472 </tr>
1473 <tr>
Reid Spencer8996e552004-08-17 00:49:03 +00001474 <td><a href="#llist">llist</a>(<a href="#symtab_entry">symtab_entry</a>)</td>
1475 <td class="td_left">A length list of symbol table entries for
1476 <tt>Type</tt>s
1477 </td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001478 </tr>
1479 <tr>
Reid Spencer8996e552004-08-17 00:49:03 +00001480 <td><a href="#zlist">llist</a>(<a href="#symtab_plane">symtab_plane</a>)</td>
1481 <td class="td_left">A length list of planes of symbol table
1482 entries for <tt>Value</tt>s</td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001483 </tr>
1484 </tbody>
Reid Spencerb39021b2004-05-23 17:05:09 +00001485</table>
Reid Spencer50026612004-05-22 02:28:36 +00001486</div>
Reid Spencer51f31e02004-07-05 22:28:02 +00001487<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001488<div class="doc_subsubsection"> <a name="symtab_plane">Symbol Table
1489Plane</a>
Reid Spencer51f31e02004-07-05 22:28:02 +00001490</div>
1491<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001492<p>A symbol table plane provides the symbol table entries for all
1493values of a common type. The encoding is given in the following table:</p>
Reid Spencer51f31e02004-07-05 22:28:02 +00001494<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001495 <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">uint32_vbr</a></td>
1502 <td class="td_left">Number of entries in this plane.</td>
1503 </tr>
1504 <tr>
1505 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1506 <td class="td_left">Slot number of type for this plane.</td>
1507 </tr>
1508 <tr>
1509 <td><a href="#symtab_entry">symtab_entry</a>+</td>
1510 <td class="td_left">The symbol table entries for this plane.</td>
1511 </tr>
1512 </tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001513</table>
1514</div>
Reid Spencer51f31e02004-07-05 22:28:02 +00001515<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001516<div class="doc_subsubsection"> <a name="symtab_entry">Symbol Table
1517Entry</a>
Reid Spencer51f31e02004-07-05 22:28:02 +00001518</div>
1519<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001520<p>A symbol table entry provides the assocation between a type or
1521value's slot number and the name given to that type or value. The
1522format is given in the following table:</p>
Reid Spencer51f31e02004-07-05 22:28:02 +00001523<table>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001524 <tbody>
1525 <tr>
1526 <th><b>Type</b></th>
1527 <th class="td_left"><b>Field Description</b></th>
1528 </tr>
1529 <tr>
1530 <td><a href="#uint32_vbr">uint24_vbr</a></td>
Reid Spencer8996e552004-08-17 00:49:03 +00001531 <td class="td_left">Slot number of the type or value being given a name.
1532 </td>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001533 </tr>
1534 <tr>
1535 <td><a href="#uint32_vbr">uint32_vbr</a></td>
1536 <td class="td_left">Length of the character array that follows.</td>
1537 </tr>
1538 <tr>
1539 <td><a href="#char">char</a>+</td>
1540 <td class="td_left">The characters of the name.</td>
1541 </tr>
1542 </tbody>
Reid Spencer51f31e02004-07-05 22:28:02 +00001543</table>
1544</div>
Reid Spencer7c76d332004-06-08 07:41:41 +00001545<!-- *********************************************************************** -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001546<div class="doc_section"> <a name="versiondiffs">Version Differences</a>
1547</div>
Reid Spencer7c76d332004-06-08 07:41:41 +00001548<!-- *********************************************************************** -->
1549<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001550<p>This section describes the differences in the Bytecode Format across
1551LLVM
1552versions. The versions are listed in reverse order because it assumes
1553the current version is as documented in the previous sections. Each
1554section here
Chris Lattner1cc070c2004-07-05 18:05:48 +00001555describes the differences between that version and the one that <i>follows</i>.
Reid Spencer7c76d332004-06-08 07:41:41 +00001556</p>
1557</div>
1558<!-- _______________________________________________________________________ -->
Reid Spencercf549e12004-08-17 07:43:43 +00001559<div class="doc_subsection"><a name="vers12">Version 1.3 Differences From
1560 1.4</a></div>
1561<!-- _______________________________________________________________________ -->
1562<div class="doc_subsubsection">Aligned Data</div>
1563<div class="doc_text">
1564 <p>In version 1.3, certain data items were aligned to 32-bit boundaries. In
1565 version 1.4, alignment of data was done away with completely. The need for
1566 alignment has gone away and the only thing it adds is bytecode file size
1567 overhead. In most cases this overhead was small. However, in functions with
1568 large numbers of format 0 instructions (GEPs and PHIs with lots of parameters)
1569 or regular instructions with large valued operands (e.g. because there's just
1570 a lot of instructions in the function) the overhead can be extreme. In one
1571 test case, the overhead was 44,000 bytes (34% of the total file size).
1572 Consequently in release 1.4, the decision was made to eliminate alignment
1573 altogether.</p>
1574 <p>In version 1.3 format, the following bytecode constructs were aligned (i.e.
1575 they were followed by one to three bytes of padding):</p>
1576 <ul>
1577 <li>All blocks.</li>
1578 <li>Instructions using the long format (format 0).</li>
1579 <li>All call instructions that called a var args function.</li>
1580 <li>The target triple (a string field at the end of the module block).</li>
1581 <li>The version field (immediately following the signature).</li>
1582 </ul>
1583 <p>None of these constructs are aligned in version 1.4</p>
1584</div>
1585
1586<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001587<div class="doc_subsection"><a name="vers12">Version 1.2 Differences
1588From 1.3</a></div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001589<!-- _______________________________________________________________________ -->
1590<div class="doc_subsubsection">Type Derives From Value</div>
Reid Spencer7c76d332004-06-08 07:41:41 +00001591<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001592<p>In version 1.2, the Type class in the LLVM IR derives from the Value
1593class. This is not the case in version 1.3. Consequently, in version
15941.2 the notion of a "Type Type" was used to write out values that were
1595Types. The types always occuped plane 12 (corresponding to the
1596TypeTyID) of any type planed set of values. In 1.3 this representation
1597is not convenient because the TypeTyID (12) is not present and its
1598value is now used for LabelTyID. Consequently, the data structures
1599written that involve types do so by writing all the types first and
1600then each of the value planes according to those types. In version 1.2,
1601the types would have been written intermingled with the values.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001602</div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001603<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001604<div class="doc_subsubsection">Restricted getelementptr Types</div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001605<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001606<p>In version 1.2, the getelementptr instruction required a ubyte type
1607index for accessing a structure field and a long type index for
1608accessing an array element. Consequently, it was only possible to
1609access structures of 255 or fewer elements. Starting in version 1.3,
1610this restriction was lifted. Structures must now be indexed with uint
1611constants. Arrays may now be indexed with int, uint, long, or ulong
1612typed values. The consequence of this was that the bytecode format had
1613to change in order to accommodate the larger range of structure indices.</p>
Reid Spencer7c76d332004-06-08 07:41:41 +00001614</div>
Reid Spencer7c76d332004-06-08 07:41:41 +00001615<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001616<div class="doc_subsubsection">Short Block Headers</div>
1617<div class="doc_text">
1618<p>In version 1.2, block headers were always 8 bytes being comprised of
1619both an unsigned integer type and an unsigned integer size. For very
1620small modules, these block headers turn out to be a large fraction of
1621the total bytecode file size. In an attempt to make these small files
1622smaller, the type and size information was encoded into a single
1623unsigned integer (4 bytes) comprised of 5 bits for the block type
1624(maximum 31 block types) and 27 bits for the block size (max
1625~134MBytes). These limits seemed sufficient for any blocks or sizes
1626forseen in the future. Note that the module block, which encloses all
1627the other blocks is still written as 8 bytes since bytecode files
1628larger than 134MBytes might be possible.</p>
1629</div>
1630<!-- _______________________________________________________________________ -->
1631<div class="doc_subsubsection">Dependent Libraries and Target Triples</div>
1632<div class="doc_text">
1633<p>In version 1.2, the bytecode format does not store module's target
1634triple or dependent. These fields have been added to the end of the <a
1635 href="#globalinfo">module global info block</a>. The purpose of these
1636fields is to allow a front end compiler to specifiy that the generated
1637module is specific to a particular target triple (operating
1638system/manufacturer/processor) which makes it non-portable; and to
1639allow front end compilers to specify the list of libraries that the
1640module depends on for successful linking.</p>
1641</div>
1642<!-- _______________________________________________________________________ -->
1643<div class="doc_subsubsection">Types Restricted to 24-bits</div>
1644<div class="doc_text">
1645<p>In version 1.2, type slot identifiers were written as 32-bit VBR
1646quantities. In 1.3 this has been reduced to 24-bits in order to ensure
1647that it is not possible to overflow the type field of a global variable
1648definition. 24-bits for type slot numbers is deemed sufficient for any
1649practical use of LLVM.</p>
1650</div>
1651<!-- _______________________________________________________________________ -->
1652<!-- _______________________________________________________________________ -->
1653<div class="doc_subsection"><a name="vers11">Version 1.1 Differences
1654From 1.2 </a></div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001655<!-- _______________________________________________________________________ -->
1656<div class="doc_subsubsection">Explicit Primitive Zeros</div>
Reid Spencer7c76d332004-06-08 07:41:41 +00001657<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001658<p>In version 1.1, the zero value for primitives was explicitly encoded
1659into the bytecode format. Since these zero values are constant values
1660in the LLVM IR and never change, there is no reason to explicitly
1661encode them. This explicit encoding was removed in version 1.2.</p>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001662</div>
Reid Spencer1ab929c2004-07-05 08:18:07 +00001663<!-- _______________________________________________________________________ -->
1664<div class="doc_subsubsection">Inconsistent Module Global Info</div>
1665<div class="doc_text">
Reid Spencer9bd2be22004-07-29 00:13:04 +00001666<p>In version 1.1, the Module Global Info block was not aligned causing
1667the next block to be read in on an unaligned boundary. This problem was
1668corrected in version 1.2.<br>
1669<br>
1670</p>
Reid Spencer7c76d332004-06-08 07:41:41 +00001671</div>
Reid Spencer7c76d332004-06-08 07:41:41 +00001672<!-- _______________________________________________________________________ -->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001673<div class="doc_subsection"><a name="vers10">Version 1.0 Differences
1674From 1.1</a></div>
Reid Spencer7c76d332004-06-08 07:41:41 +00001675<div class="doc_text">
Reid Spencer1ab929c2004-07-05 08:18:07 +00001676<p>None. Version 1.0 and 1.1 bytecode formats are identical.</p>
Reid Spencer7c76d332004-06-08 07:41:41 +00001677</div>
Reid Spencer50026612004-05-22 02:28:36 +00001678<!-- *********************************************************************** -->
1679<hr>
Reid Spencer9bd2be22004-07-29 00:13:04 +00001680<address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1681 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1682<a href="http://validator.w3.org/check/referer"><img
1683 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1684<a href="mailto:rspencer@x10sys.com">Reid Spencer</a> and <a
1685 href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1686<a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
1687Last modified: $Date$
Reid Spencer50026612004-05-22 02:28:36 +00001688</address>
Reid Spencer50026612004-05-22 02:28:36 +00001689<!-- vim: sw=2
1690-->
Reid Spencer9bd2be22004-07-29 00:13:04 +00001691</body>
1692</html>