aboutsummaryrefslogtreecommitdiff
path: root/docs/BytecodeFormat.html
blob: 9902ecdf5edacc820c308cead08d1885408b05c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>LLVM Bytecode File Format</title>
  <link rel="stylesheet" href="llvm.css" type="text/css">
  <style type="css">
    table, tr, td { border: 2px solid gray }
    th { border: 2px solid gray; font-weight: bold; }
    table { border-collapse: collapse; margin-top: 1em margin-bottom: 1em }
  </style>
</head>
<body>
  <div class="doc_title"> LLVM Bytecode File Format </div>
<ol>
  <li><a href="#abstract">Abstract</a></li>
  <li><a href="#general">General Concepts</a>
    <ol>
      <li><a href="#blocks">Blocks</a></li>
      <li><a href="#lists">Lists</a></li>
      <li><a href="#fields">Fields</a></li>
      <li><a href="#slots">Slots</a></li>
      <li><a href="#encoding">Encoding Rules</a></li>
      <li><a href="#align">Alignment</a></li>
    </ol>
  </li>
  <li><a href="#details">Detailed Layout</a>
    <ol>
      <li><a href="#notation">Notation</a></li>
      <li><a href="#blocktypes">Blocks Types</a></li>
      <li><a href="#signature">Signature Block</a></li>
      <li><a href="#module">Module Block</a></li>
      <li><a href="#typeool">Global Type Pool</a></li>
      <li><a href="#modinfo">Module Info Block</a></li>
      <li><a href="#constants">Global Constant Pool</a></li>
      <li><a href="#functions">Function Blocks</a></li>
      <li><a href="#symtab">Module Symbol Table</a></li>
    </ol>
  </li>
</ol>
<div class="doc_author">
<p>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a>
</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="abstract">Abstract </a></div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>This document describes the LLVM bytecode
file format. It specifies the binary encoding rules of the bytecode file format
so that equivalent systems can encode bytecode files correctly.  The LLVM 
bytecode representation is used to store the intermediate representation on 
disk in compacted form.
</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="general">General Concepts</a> </div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>This section describes the general concepts of the bytecode file format 
without getting into bit and byte level specifics.  Note that the LLVM bytecode
format may change in the future, but will always be backwards compatible with
older formats.  This document only describes the most current version of the
bytecode format.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="blocks">Blocks</a> </div>
<div class="doc_text">
<p>LLVM bytecode files consist simply of a sequence of blocks of bytes. 
Each block begins with an identification value that determines the type of 
the next block.  The possible types of blocks are described below in the section 
<a href="#blocktypes">Block Types</a>. The block identifier is used because
it is possible for entire blocks to be omitted from the file if they are
empty. The block identifier helps the reader determine which kind of block is
next in the file.</p>
<p>The following block identifiers are currently in use 
(from llvm/Bytecode/Format.h):</p>
<ol>
  <li><b>Module (0x01)</b>.</li>
  <li><b>Function (0x11)</b>.</li>
  <li><b>ConstantPool (0x12)</b>.</li>
  <li><b>SymbolTable (0x13)</b>.</li>
  <li><b>ModuleGlobalInfo (0x14)</b>.</li>
  <li><b>GlobalTypePlane (0x15)</b>.</li>
  <li><b>BasicBlock (0x31)</b>.</li>
  <li><b>InstructionList (0x32)</b>.</li>
  <li><b>CompactionTable (0x33)</b>.</li>
</ol>
<p> All blocks are variable length, and the block header specifies the size of 
the block.  All blocks are rounded aligned to even 32-bit boundaries, so they 
always start and end of this boundary.  Each block begins with an integer 
identifier and the length of the block, which does not include the padding 
bytes needed for alignment.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="lists">Lists</a> </div>
<div class="doc_text">
<p>Most blocks are constructed of lists of information. Lists can be constructed
of other lists, etc. This decomposition of information follows the containment
hierarchy of the LLVM Intermediate Representation. For example, a function 
contains a list of instructions (the terminator instructions implicitly define 
the end of the basic blocks).</p>
<p>A list is encoded into the file simply by encoding the number of entries as
an integer followed by each of the entries. The reader knows when the list is
done because it will have filled the list with the required numbe of entries.
</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="fields">Fields</a> </div>
<div class="doc_text">
<p>Fields are units of information that LLVM knows how to write atomically.
Most fields have a uniform length or some kind of length indication built into
their encoding. For example, a constant string (array of bytes) is
written simply as the length followed by the characters. Although this is 
similar to a list, constant strings are treated atomically and are thus
fields.</p>
<p>Fields use a condensed bit format specific to the type of information
they must contain. As few bits as possible are written for each field. The
sections that follow will provide the details on how these fields are 
written and how the bits are to be interpreted.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="slots">Slots</a> </div>
<div class="doc_text">
<p>The bytecode format uses the notion of a "slot" to reference Types and
Values. Since the bytecode file is a <em>direct</em> representation of LLVM's
intermediate representation, there is a need to represent pointers in the file.
Slots are used for this purpose. For example, if one has the following assembly:
</p>

<div class="doc_code">
  %MyType = type { int, sbyte }<br>
  %MyVar = external global %MyType
</div>

<p>there are two definitions. The definition of <tt>%MyVar</tt> uses 
<tt>%MyType</tt>. In the C++ IR this linkage between <tt>%MyVar</tt> and 
<tt>%MyType</tt> is
explicit through the use of C++ pointers. In bytecode, however, there's no
ability to store memory addresses. Instead, we compute and write out slot 
numbers for every type and Value written to the file.</p>
<p>A slot number is simply an unsigned 32-bit integer encoded in the variable
bit rate scheme (see <a href="#encoding">encoding</a> below). This ensures that
low slot numbers are encoded in one byte. Through various bits of magic LLVM
attempts to always keep the slot numbers low. The first attempt is to associate
slot numbers with their "type plane". That is, Values of the same type are 
written to the bytecode file in a list (sequentially). Their order in that list
determines their slot number. This means that slot #1 doesn't mean anything
unless you also specify for which type you want slot #1. Types are handled
specially and are always written to the file first (in the Global Type Pool) and
in such a way that both forward and backward references of the types can often be
resolved with a single pass through the type pool. </p>
<p>Slot numbers are also kept small by rearranging their order. Because of the
structure of LLVM, certain values are much more likely to be used frequently
in the body of a function. For this reason, a compaction table is provided in
the body of a function if its use would make the function body smaller. 
Suppose you have a function body that uses just the types "int*" and "{double}"
but uses them thousands of time. Its worthwhile to ensure that the slot number
for these types are low so they can be encoded in a single byte (via vbr).
This is exactly what the compaction table does.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="encoding">Encoding Primitives</a> </div>
<div class="doc_text">
<p>Each field that can be put out is encoded into the file using a small set 
of primitives. The rules for these primitives are described below.</p>
<h3>Variable Bit Rate Encoding</h3>
<p>Most of the values written to LLVM bytecode files are small integers.  To 
minimize the number of bytes written for these quantities, an encoding
scheme similar to UTF-8 is used to write integer data. The scheme is known as
variable bit rate (vbr) encoding.  In this encoding, the high bit of each 
byte is used to indicate if more bytes follow. If (byte &amp; 0x80) is non-zero 
in any given byte, it means there is another byte immediately following that 
also contributes to the value. For the final byte (byte &amp; 0x80) is false 
(the high bit is not set). In each byte only the low seven bits contribute to 
the value. Consequently 32-bit quantities can take from one to <em>five</em> 
bytes to encode. In general, smaller quantities will encode in fewer bytes, 
as follows:</p>
<table class="doc_table_nw">
  <tr>
    <th>Byte #</th>
    <th>Significant Bits</th>
    <th>Maximum Value</th>
  </tr>
  <tr><td>1</td><td>0-6</td><td>127</td></tr>
  <tr><td>2</td><td>7-13</td><td>16,383</td></tr>
  <tr><td>3</td><td>14-20</td><td>2,097,151</td></tr>
  <tr><td>4</td><td>21-27</td><td>268,435,455</td></tr>
  <tr><td>5</td><td>28-34</td><td>34,359,738,367</td></tr>
  <tr><td>6</td><td>35-41</td><td>4,398,046,511,103</td></tr>
  <tr><td>7</td><td>42-48</td><td>562,949,953,421,311</td></tr>
  <tr><td>8</td><td>49-55</td><td>72,057,594,037,927,935</td></tr>
  <tr><td>9</td><td>56-62</td><td>9,223,372,036,854,775,807</td></tr>
  <tr><td>10</td><td>63-69</td><td>1,180,591,620,717,411,303,423</td></tr>
</table>
<p