Skip to content
BytecodeFormat.html 61.7 KiB
Newer Older
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">The number of constant strings that follow.</td>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Zero. This identifies the following "plane"
as containing the constant strings. This is needed to identify it
uniquely from other constant planes that follow. </td>
    <tr>
      <td><a href="#uint24_vbr">uint24_vbr</a>+</td>
      <td class="td_left">Slot number of the constant string's type.
Note that the constant string's type implicitly defines the length of
the string. </td>
    </tr>
  </tbody>
</table>
<p><b>Function Constant Pool Preamble (function types)</b></p>
<p>The structure of the types for functions is identical to the <a
 href="#globaltypes">Global Type Pool</a>. Please refer to that section
for the details. </p>
<p><b>Common Part (other constants)</b></p>
<table>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Number of entries in this type plane.</td>
    </tr>
    <tr>
      <td><a href="#uint24_vbr">uint24_vbr</a></td>
      <td class="td_left">Type slot number of this plane.</td>
      <td><a href="#constant">constant</a>+</td>
      <td class="td_left">The definition of a constant (see below).</td>
    </tr>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"><a name="constant">Constant Field</a></div>
<div class="doc_text">
<p>Constants come in many shapes and flavors. The sections that followe
define the format for each of them. All constants start with a <a
 href="#uint32_vbr">uint32_vbr</a> encoded integer that provides the
number of operands for the constant. For primitive, structure, and
array constants, this will always be zero since those types of
constants have no operands. In this case, we have the following field
definitions:</p>
<ul>
  <li><b>Bool</b>. This is written as an <a href="#uint32_vbr">uint32_vbr</a>
of value 1U or 0U.</li>
  <li><b>Signed Integers (sbyte,short,int,long)</b>. These are written
as an <a href="#int64_vbr">int64_vbr</a> with the corresponding value.</li>
  <li><b>Unsigned Integers (ubyte,ushort,uint,ulong)</b>. These are
written as an <a href="#uint64_vbr">uint64_vbr</a> with the
corresponding value. </li>
  <li><b>Floating Point</b>. Both the float and double types are
written literally in binary format.</li>
  <li><b>Arrays</b>. Arrays are written simply as a list of <a
 href="#uint32_vbr">uint32_vbr</a> encoded slot numbers to the constant
element values.</li>
  <li><b>Structures</b>. Structures are written simply as a list of <a
 href="#uint32_vbr">uint32_vbr</a> encoded slot numbers to the constant
field values of the structure.</li>
</ul>
<p>When the number of operands to the constant is non-zero, we have a
constant expression and its field format is provided in the table below.</p>
<table>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Op code of the instruction for the constant
expression.</td>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">The slot number of the constant value for an
operand.<sup>1</sup></td>
    </tr>
    <tr>
      <td><a href="#uint24_vbr">uint24_vbr</a></td>
      <td class="td_left">The slot number for the type of the constant
value for an operand.<sup>1</sup></td>
  </tbody>
</table>
Notes:
<ol>
  <li>Both these fields are repeatable but only in pairs.</li>
</ol>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="functiondefs">Function Definition</a></div>
<div class="doc_text">
<p>Function definitions contain the linkage, constant pool or
compaction table, instruction list, and symbol table for a function.
The following table shows the structure of a function definition.</p>
<table>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td><a href="#block">block</a><br>
      </td>
      <td class="td_left">Function definition block identifier (0x02) +
size<br>
      </td>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">The linkage type of the function: 0=External,
1=Weak, 2=Appending, 3=Internal, 4=LinkOnce<sup>1</sup></td>
    </tr>
    <tr>
      <td><a href="#block">block</a></td>
      <td class="td_left">The <a href="#constantpool">constant pool</a>
block for this function.<sup>2</sup></td>
    </tr>
    <tr>
      <td><a href="#block">block</a></td>
      <td class="td_left">The <a href="#compactiontable">compaction
table</a> block for the function.<sup>2</sup></td>
    </tr>
    <tr>
      <td><a href="#block">block</a></td>
      <td class="td_left">The <a href="#instructionlist">instruction
list</a> for the function.</td>
    </tr>
    <tr>
      <td><a href="#block">block</a></td>
      <td class="td_left">The function's <a href="#symboltable">symbol
table</a> containing only those symbols pertinent to the function
(mostly block labels).</td>
    </tr>
  </tbody>
</table>
Notes:
<ol>
  <li>Note that if the linkage type is "External" then none of the
other fields will be present as the function is defined elsewhere.</li>
  <li>Note that only one of the constant pool or compaction table will
be written. Compaction tables are only written if they will actually
save bytecode space. If not, then a regular constant pool is written.</li>
</ol>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="compactiontable">Compaction Table</a>
</div>
<div class="doc_text">
<p>Compaction tables are part of a function definition. They are merely
a device for reducing the size of bytecode files. The size of a
bytecode file is dependent on the <em>value</em> of the slot numbers
used because larger values use more bytes in the variable bit rate
encoding scheme. Furthermore, the compressed instruction format
reserves only six bits for the type of the instruction. In large
modules, declaring hundreds or thousands of types, the values of the
slot numbers can be quite large. However, functions may use only a
small fraction of the global types. In such cases a compaction table is
created that maps the global type and value slot numbers to smaller
values used by a function. Functions will contain either a
function-specific constant pool <em>or</em> a compaction table but not
both. Compaction tables have the format shown in the table below.</p>
<table>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">The number of types that follow</td>
    </tr>
    <tr>
      <td><a href="#uint24_vbr">uint24_vbr</a>+</td>
      <td class="td_left">The slot number in the global type plane of
the type that will be referenced in the function with the index of this
entry in the compaction table.</td>
    </tr>
    <tr>
      <td><a href="#type_len">type_len</a></td>
      <td class="td_left">An encoding of the type and number of values
that follow. This field's encoding varies depending on the size of the
type plane. See <a href="#type_len">Type and Length</a> for further
details.</td>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a>+</td>
      <td class="td_left">The slot number in the globals of the value
that will be referenced in the function with the index of this entry in
the compaction table</td>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"><a name="type_len">Type and Length</a></div>
<div class="doc_text">
<p>The type and length of a compaction table type plane is encoded
differently depending on the length of the plane. For planes of length
1 or 2, the length is encoded into bits 0 and 1 of a <a
 href="#uint32_vbr">uint32_vbr</a> and the type is encoded into bits
2-31. Because type numbers are often small, this often saves an extra
byte per plane. If the length of the plane is greater than 2 then the
encoding uses a <a href="#uint32_vbr">uint32_vbr</a> for each of the
length and type, in that order.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="instructionlist">Instruction List</a>
</div>
<p>The instructions in a function are written as a simple list. Basic
blocks are inferred by the terminating instruction types. The format of
the block is given in the following table.</p>
<table>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td><a href="#block">block</a><br>
      </td>
      <td class="td_left">Instruction list identifier (0x07) + size<br>
      </td>
    </tr>
    <tr>
      <td><a href="#instruction">instruction</a>+</td>
      <td class="td_left">An instruction. Instructions have a variety
of formats. See <a href="#instruction">Instructions</a> for details.</td>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"><a name="instruction">Instructions</a></div>
<div class="doc_text">
<p>For brevity, instructions are written in one of four formats,
depending on the number of operands to the instruction. Each
instruction begins with a <a href="#uint32_vbr">uint32_vbr</a> that
encodes the type of the instruction as well as other things. The tables
that follow describe the format of this first word of each instruction.</p>
<p><b>Instruction Format 0</b></p>
<p>This format is used for a few instructions that can't easily be
optimized because they have large numbers of operands (e.g. PHI Node or
getelementptr). Each of the opcode, type, and operand fields is as
successive fields.</p>
<table>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Specifies the opcode of the instruction. Note
that for compatibility with the other instruction formats, the opcode
is shifted left by 2 bits. Bits 0 and 1 must have value zero for this
format.</td>
    </tr>
    <tr>
      <td><a href="#uint24_vbr">uint24_vbr</a></td>
      <td class="td_left">Provides the slot number of the result type
of the instruction</td>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">The number of operands that follow.</td>
      <td><a href="#uint32_vbr">uint32_vbr</a>+</td>
      <td class="td_left">The slot number of the value(s) for the
operand(s). <sup>1</sup></td>
    </tr>
  </tbody>
</table>
Notes:
<ol>
  <li>Note that if the instruction is a getelementptr and the type of
the operand is a sequential type (array or pointer) then the slot
number is shifted up two bits and the low order bits will encode the
type of index used, as follows: 0=uint, 1=int, 2=ulong, 3=long.</li>
</ol>
<p><b>Instruction Format 1</b></p>
<p>This format encodes the opcode, type and a single operand into a
single <a href="#uint32_vbr">uint32_vbr</a> as follows:</p>
<table>
  <tbody>
    <tr>
      <th><b>Bits</b></th>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td>0-1</td>
      <td>constant "1"</td>
      <td class="td_left">These two bits must be the value 1 which
identifies this as an instruction of format 1.</td>
    </tr>
    <tr>
      <td>2-7</td>
      <td><a href="#opcodes">opcode</a></td>
      <td class="td_left">Specifies the opcode of the instruction. Note
that the maximum opcode value is 63.</td>
    </tr>
    <tr>
      <td>8-19</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the type for
this instruction. Maximum slot number is 2<sup>12</sup>-1=4095.</td>
    </tr>
    <tr>
      <td>20-31</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the value for
the first operand. Maximum slot number is 2<sup>12</sup>-1=4095. Note
that the value 2<sup>12</sup>-1 denotes zero operands.</td>
    </tr>
  </tbody>
</table>
<p><b>Instruction Format 2</b></p>
<p>This format encodes the opcode, type and two operands into a single <a
 href="#uint32_vbr">uint32_vbr</a> as follows:</p>
<table>
  <tbody>
    <tr>
      <th><b>Bits</b></th>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td>0-1</td>
      <td>constant "2"</td>
      <td class="td_left">These two bits must be the value 2 which
identifies this as an instruction of format 2.</td>
    </tr>
    <tr>
      <td>2-7</td>
      <td><a href="#opcodes">opcode</a></td>
      <td class="td_left">Specifies the opcode of the instruction. Note
that the maximum opcode value is 63.</td>
    </tr>
    <tr>
      <td>8-15</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the type for
this instruction. Maximum slot number is 2<sup>8</sup>-1=255.</td>
    </tr>
    <tr>
      <td>16-23</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the value for
the first operand. Maximum slot number is 2<sup>8</sup>-1=255.</td>
    </tr>
    <tr>
      <td>24-31</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the value for
the second operand. Maximum slot number is 2<sup>8</sup>-1=255.</td>
    </tr>
  </tbody>
</table>
<p><b>Instruction Format 3</b></p>
<p>This format encodes the opcode, type and three operands into a
single <a href="#uint32_vbr">uint32_vbr</a> as follows:</p>
<table>
  <tbody>
    <tr>
      <th><b>Bits</b></th>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td>0-1</td>
      <td>constant "3"</td>
      <td class="td_left">These two bits must be the value 3 which
identifies this as an instruction of format 3.</td>
    </tr>
    <tr>
      <td>2-7</td>
      <td><a href="#opcodes">opcode</a></td>
      <td class="td_left">Specifies the opcode of the instruction. Note
that the maximum opcode value is 63.</td>
    </tr>
    <tr>
      <td>8-13</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the type for
this instruction. Maximum slot number is 2<sup>6</sup>-1=63.</td>
    </tr>
    <tr>
      <td>14-19</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the value for
the first operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
    </tr>
    <tr>
      <td>20-25</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the value for
the second operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
    </tr>
    <tr>
      <td>26-31</td>
      <td><a href="#unsigned">unsigned</a></td>
      <td class="td_left">Specifies the slot number of the value for
the third operand. Maximum slot number is 2<sup>6</sup>-1=63.</td>
    </tr>
  </tbody>
</table>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="symtab">Symbol Table</a> </div>
<p>A symbol table can be put out in conjunction with a module or a function.
A symbol table is a list of type planes. Each type plane starts with the number
of entries in the plane and the type plane's slot number (so the type
can be looked up in the global type pool). For each entry in a type
plane, the slot number of the value and the name associated with that
value are written. The format is given in the table below. </p>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td><a href="#block">block</a><br>
      </td>
      <td class="td_left">Symbol Table Identifier (0x04)</td>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Number of entries in type plane</td>
    </tr>
    <tr>
      <td><a href="#symtab_entry">symtab_entry</a>*</td>
      <td class="td_left">Provides the slot number of the type and its
name.</td>
    </tr>
    <tr>
      <td><a href="#symtab_plane">symtab_plane</a>*</td>
      <td class="td_left">A type plane containing value slot number and
name for all values of the same type.</td>
    </tr>
  </tbody>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="symtab_plane">Symbol Table
Plane</a>
</div>
<div class="doc_text">
<p>A symbol table plane provides the symbol table entries for all
values of a common type. The encoding is given in the following table:</p>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Number of entries in this plane.</td>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Slot number of type for this plane.</td>
    </tr>
    <tr>
      <td><a href="#symtab_entry">symtab_entry</a>+</td>
      <td class="td_left">The symbol table entries for this plane.</td>
    </tr>
  </tbody>
</table>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="symtab_entry">Symbol Table
Entry</a>
</div>
<div class="doc_text">
<p>A symbol table entry provides the assocation between a type or
value's slot number and the name given to that type or value. The
format is given in the following table:</p>
  <tbody>
    <tr>
      <th><b>Type</b></th>
      <th class="td_left"><b>Field Description</b></th>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint24_vbr</a></td>
      <td class="td_left">Slot number of the type or value being given
a name. </td>
    </tr>
    <tr>
      <td><a href="#uint32_vbr">uint32_vbr</a></td>
      <td class="td_left">Length of the character array that follows.</td>
    </tr>
    <tr>
      <td><a href="#char">char</a>+</td>
      <td class="td_left">The characters of the name.</td>
    </tr>
  </tbody>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="versiondiffs">Version Differences</a>
</div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>This section describes the differences in the Bytecode Format across
LLVM
versions. The versions are listed in reverse order because it assumes
the current version is as documented in the previous sections. Each
section here
describes the differences between that version and the one that <i>follows</i>.
</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="vers12">Version 1.2 Differences
From 1.3</a></div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">Type Derives From Value</div>
<p>In version 1.2, the Type class in the LLVM IR derives from the Value
class. This is not the case in version 1.3. Consequently, in version
1.2 the notion of a "Type Type" was used to write out values that were
Types. The types always occuped plane 12 (corresponding to the
TypeTyID) of any type planed set of values. In 1.3 this representation
is not convenient because the TypeTyID (12) is not present and its
value is now used for LabelTyID. Consequently, the data structures
written that involve types do so by writing all the types first and
then each of the value planes according to those types. In version 1.2,
the types would have been written intermingled with the values.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">Restricted getelementptr Types</div>
<div class="doc_text">
<p>In version 1.2, the getelementptr instruction required a ubyte type
index for accessing a structure field and a long type index for
accessing an array element. Consequently, it was only possible to
access structures of 255 or fewer elements. Starting in version 1.3,
this restriction was lifted. Structures must now be indexed with uint
constants. Arrays may now be indexed with int, uint, long, or ulong
typed values. The consequence of this was that the bytecode format had
to change in order to accommodate the larger range of structure indices.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">Short Block Headers</div>
<div class="doc_text">
<p>In version 1.2, block headers were always 8 bytes being comprised of
both an unsigned integer type and an unsigned integer size. For very
small modules, these block headers turn out to be a large fraction of
the total bytecode file size. In an attempt to make these small files
smaller, the type and size information was encoded into a single
unsigned integer (4 bytes) comprised of 5 bits for the block type
(maximum 31 block types) and 27 bits for the block size (max
~134MBytes). These limits seemed sufficient for any blocks or sizes
forseen in the future. Note that the module block, which encloses all
the other blocks is still written as 8 bytes since bytecode files
larger than 134MBytes might be possible.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">Dependent Libraries and Target Triples</div>
<div class="doc_text">
<p>In version 1.2, the bytecode format does not store module's target
triple or dependent. These fields have been added to the end of the <a
 href="#globalinfo">module global info block</a>. The purpose of these
fields is to allow a front end compiler to specifiy that the generated
module is specific to a particular target triple (operating
system/manufacturer/processor) which makes it non-portable; and to
allow front end compilers to specify the list of libraries that the
module depends on for successful linking.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">Types Restricted to 24-bits</div>
<div class="doc_text">
<p>In version 1.2, type slot identifiers were written as 32-bit VBR
quantities. In 1.3 this has been reduced to 24-bits in order to ensure
that it is not possible to overflow the type field of a global variable
definition. 24-bits for type slot numbers is deemed sufficient for any
practical use of LLVM.</p>
</div>
<!-- _______________________________________________________________________ -->
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="vers11">Version 1.1 Differences
From 1.2 </a></div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">Explicit Primitive Zeros</div>
<div class="doc_text">
<p>In version 1.1, the zero value for primitives was explicitly encoded
into the bytecode format. Since these zero values are constant values
in the LLVM IR and never change, there is no reason to explicitly
encode them. This explicit encoding was removed in version 1.2.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">Inconsistent Module Global Info</div>
<p>In version 1.1, the Module Global Info block was not aligned causing
the next block to be read in on an unaligned boundary. This problem was
corrected in version 1.2.<br>
<br>
</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsection"><a name="vers10">Version 1.0 Differences
From 1.1</a></div>
<p>None. Version 1.0 and 1.1 bytecode formats are identical.</p>
<!-- *********************************************************************** -->
<hr>
<address> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
<a href="http://validator.w3.org/check/referer"><img
 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
<a href="mailto:rspencer@x10sys.com">Reid Spencer</a> and <a
 href="mailto:sabre@nondot.org">Chris Lattner</a><br>
<a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
Last modified: $Date$