Reid Spencer | 5002661 | 2004-05-22 02:28:36 +0000 | [diff] [blame^] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
| 5 | <title>LLVM Bytecode File Format</title> |
| 6 | <link rel="stylesheet" href="llvm.css" type="text/css"> |
| 7 | <style> |
| 8 | table, tr, td { border: 2px solid gray } |
| 9 | th { border: 2px sold gray; font-weight: bold; } |
| 10 | table { border-collapse: collapse; margin-top: 1em margin-bottom: 1em } |
| 11 | </style> |
| 12 | </head> |
| 13 | <body> |
| 14 | <div class="doc_title"> LLVM Bytecode File Format </div> |
| 15 | <ol> |
| 16 | <li><a href="#abstract">Abstract</a></li> |
| 17 | <li><a href="#general">General Concepts</a></li> |
| 18 | <ol> |
| 19 | <li><a href="#blocks">Blocks</a></li> |
| 20 | <li><a href="#lists">Lists</a></li> |
| 21 | <li><a href="#fields">Fields</a></li> |
| 22 | <li><a href="#align">Alignment</a></li> |
| 23 | </ol> |
| 24 | <li><a href="#details">Detailed Layout</a> |
| 25 | <ol> |
| 26 | <li><a href="#notation">Notation</a></li> |
| 27 | <li><a href="#blocktypes">Blocks Types</a></li> |
| 28 | <li><a href="#header">Header Block</a></li> |
| 29 | <li><a href="#typeool">Global Type Pool</a></li> |
| 30 | <li><a href="#modinfo">Module Info Block</a></li> |
| 31 | <li><a href="#constants">Global Constant Pool</a></li> |
| 32 | <li><a href="#functions">Function Blocks</a><li> |
| 33 | <li><a href="#symtab">Module Symbol Table</a><li> |
| 34 | </ol> |
| 35 | </li> |
| 36 | </ol> |
| 37 | <div class="doc_text"> |
| 38 | <p><b>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a> |
| 39 | and <a href="mailto:sabre@nondot.org">Chris Lattner</a></b></p> |
| 40 | <p> </p> |
| 41 | </div> |
| 42 | <!-- *********************************************************************** --> |
| 43 | <div class="doc_section"> <a name="abstract">Abstract </a></div> |
| 44 | <!-- *********************************************************************** --> |
| 45 | <div class="doc_text"> |
| 46 | <p>This document is an (after the fact) specification of the LLVM bytecode |
| 47 | file format. It documents the binary encoding rules of the bytecode file format |
| 48 | so that equivalent systems can encode bytecode files correctly. The LLVM |
| 49 | bytecode representation is used to store the intermediate representation on |
| 50 | disk in compacted form. |
| 51 | </p> |
| 52 | </div> |
| 53 | <!-- *********************************************************************** --> |
| 54 | <div class="doc_section"> <a name="general">General Concepts</a> </div> |
| 55 | <!-- *********************************************************************** --> |
| 56 | <div class="doc_text"> |
| 57 | <p>This section describes the general concepts of the bytecode file format |
| 58 | without getting into bit and byte level specifics.</p> |
| 59 | </div> |
| 60 | <!-- _______________________________________________________________________ --> |
| 61 | <div class="doc_subsection"><a name="blocks">Blocks</a> </div> |
| 62 | <div class="doc_text"> |
| 63 | <p>LLVM bytecode files consist simply of a sequence of blocks of bytes. |
| 64 | Each block begins with an identification value that determines the type of |
| 65 | the next block. The possible types of blocks are described below in the section |
| 66 | <a href="#blockstypes">Block Types</a>. The block identifier is used because |
| 67 | it is possible for entire blocks to be omitted from the file if they are |
| 68 | empty. The block identifier helps the reader determine which kind of block is |
| 69 | next in the file.</p> |
| 70 | <p> |
| 71 | Except for the <a href="#header">Header Block</a> all blocks are variable |
| 72 | length. The consume just enough bytes to express their contents.</p> |
| 73 | </div> |
| 74 | <!-- _______________________________________________________________________ --> |
| 75 | <div class="doc_subsection"><a name="lists">Lists</a> </div> |
| 76 | <div class="doc_text"> |
| 77 | <p>Most blocks are constructed of lists of information. Lists can be constructed |
| 78 | of other lists, etc. This decomposition of information follows the containment |
| 79 | hierarchy of the LLVM Intermediate Representation. For example, a function is |
| 80 | composed of a list of basic blocks. Each basic block is composed of a set of |
| 81 | instructions. This list of list nesting and hierarchy is maintained in the |
| 82 | bytecode file.</p> |
| 83 | <p>A list is encoded into the file simply by encoding the number of entries as |
| 84 | an integer followed by each of the entries. The reader knows when the list is |
| 85 | done because it will have filled the list with the required numbe of entries. |
| 86 | </p> |
| 87 | </div> |
| 88 | <!-- _______________________________________________________________________ --> |
| 89 | <div class="doc_subsection"><a name="fields">Fields</a> </div> |
| 90 | <div class="doc_text"> |
| 91 | <p>Fields are units of information that LLVM knows how to write atomically. |
| 92 | Most fields have a uniform length or some kind of length indication built into |
| 93 | their encoding. For example, a constant string (array of SByte or UByte) is |
| 94 | written simply as the length followed by the characters. Although this is |
| 95 | similar to a list, constant strings are treated atomically and are thus |
| 96 | fields.</p> |
| 97 | <p>Fields use a condensed bit format specific to the type of information |
| 98 | they must contain. As few bits as possible are written for each field. The |
| 99 | sections that follow will provide the details on how these fields are |
| 100 | written and how the bits are to be interpreted.</p> |
| 101 | </div> |
| 102 | <!-- _______________________________________________________________________ --> |
| 103 | <div class="doc_subsection"><a name="align">Alignment</a> </div> |
| 104 | <div class="doc_text"> |
| 105 | <p>To support cross-platform differences, the bytecode file is aligned on |
| 106 | certain boundaries. This means that a small amount of padding (at most 3 bytes) |
| 107 | will be added to ensure that the next entry is aligned to a 32-bit boundary. |
| 108 | </p> |
| 109 | </div> |
| 110 | <!-- *********************************************************************** --> |
| 111 | <div class="doc_section"> <a name="details">Detailed Layout</a> </div> |
| 112 | <!-- *********************************************************************** --> |
| 113 | <div class="doc_text"> |
| 114 | <p>This section provides the detailed layout of the LLVM bytecode file format. |
| 115 | bit and byte level specifics.</p> |
| 116 | </div> |
| 117 | <!-- _______________________________________________________________________ --> |
| 118 | <div class="doc_subsection"><a name="notation">Notation</a></div> |
| 119 | <div class="doc_text"> |
| 120 | <p>The descriptions of the bytecode format that follow describe the bit |
| 121 | fields in detail. These descriptions are provided in tabular form. Each table |
| 122 | has four columns that specify:</p> |
| 123 | <ol> |
| 124 | <li><b>Byte(s)</b>. The offset in bytes of the field from the start of |
| 125 | its container (block, list, other field).<li> |
| 126 | <li><b>Bit(s)</b>. The offset in bits of the field from the start of |
| 127 | the byte field. Bits are always little endian. That is, bit addresses with |
| 128 | smaller values have smaller address (i.e. 2^0 is at bit 0, 2^1 at 1, etc.) |
| 129 | </li> |
| 130 | <li><b>Align?</b> Indicates if this field is aligned to 32 bits or not. |
| 131 | This indicates where the <em>next</em> field starts, always on a 32 bit |
| 132 | boundary.</li> |
| 133 | <li><b>Description</b>. Descripts the contents of the field.</li> |
| 134 | </ol> |
| 135 | </div> |
| 136 | <!-- _______________________________________________________________________ --> |
| 137 | <div class="doc_subsection"><a name="blocktypes">Block Types</a></div> |
| 138 | <div class="doc_text"> |
| 139 | <p>The bytecode format encodes the intermediate representation into groups |
| 140 | of bytes known as blocks. The blocks are written sequentially to the file in |
| 141 | the following order:</p> |
| 142 | <ol> |
| 143 | <li><a href="#header">Header</a>. This block contains the file signature |
| 144 | (magic number), machine description and file format version (not LLVM |
| 145 | version).</li> |
| 146 | <li><a href="#gtypepool">Global Type Pool</a>. This block contains all the |
| 147 | global (module) level types.</li> |
| 148 | <li><a href="#modinfo">Module Info</a>. This block contains the types of the |
| 149 | global variables and functions in the module as well as the constant |
| 150 | initializers for the global variables</li> |
| 151 | <li><a href="#constants">Constants</a>. This block contains all the global |
| 152 | constants except function arguments, global values and constant strings.</li> |
| 153 | <li><a href="#functions">Functions</a>. One function block is written for |
| 154 | each function in the module. </li> |
| 155 | <li><a href="$symtab">Symbol Table</a>. The module level symbol table that |
| 156 | provides names for the various other entries in the file is the final block |
| 157 | written.</li> |
| 158 | </ol> |
| 159 | </div> |
| 160 | <!-- _______________________________________________________________________ --> |
| 161 | <div class="doc_subsection"><a name="header">Header Block</a> </div> |
| 162 | <div class="doc_text"> |
| 163 | <p>The Header Block occurs in every LLVM bytecode file and is always first. It |
| 164 | simply provides a few bytes of data to identify the file, its format, and the |
| 165 | bytecode version. This block is fixed length and always eight bytes, as follows: |
| 166 | <table class="doc_table" width="90%"> |
| 167 | <tr> |
| 168 | <th><b>Byte(s)</b></th> |
| 169 | <th><b>Bit(s)</b></th> |
| 170 | <th><b>Align?</b></th> |
| 171 | <th align="left"><b>Field Description</b></th> |
| 172 | </tr> |
| 173 | <tr> |
| 174 | <td>00</td><td>00-07</td><td>No</td> |
| 175 | <td align="left">Constant "l"</td> |
| 176 | </tr> |
| 177 | <tr> |
| 178 | <td>01</td><td>00-07</td><td>No</td> |
| 179 | <td align="left">Constant "l"</td> |
| 180 | </tr> |
| 181 | <tr> |
| 182 | <td>02</td><td>00-07</td><td>No</td> |
| 183 | <td align="left">Constant "v"</td> |
| 184 | </tr> |
| 185 | <tr> |
| 186 | <td>03</td><td>00-07</td><td>No</td> |
| 187 | <td align="left">Constant "m"</td> |
| 188 | </tr> |
| 189 | <tr> |
| 190 | <td>04-07</td><td>00</td><td>No</td> |
| 191 | <td align="left">Target is big endian</td> |
| 192 | </tr> |
| 193 | <tr> |
| 194 | <td>04-07</td><td>01</td><td>No</td> |
| 195 | <td align="left">Target has long pointers</td> |
| 196 | </tr> |
| 197 | <tr> |
| 198 | <td>04-07</td><td>02</td><td>No</td> |
| 199 | <td align="left">Target has no endianess</td> |
| 200 | </tr> |
| 201 | <tr> |
| 202 | <td>04-07</td><td>03</td><td>No</td> |
| 203 | <td align="left">Target has no pointer size</td> |
| 204 | </tr> |
| 205 | <tr> |
| 206 | <td>04-07</td><td>04-31</td><td>Yes</td> |
| 207 | <td align="left">The LLVM bytecode format version number</td> |
| 208 | </tr> |
| 209 | </table> |
| 210 | </div> |
| 211 | <!-- _______________________________________________________________________ --> |
| 212 | <div class="doc_subsection"><a name="gtypepool">Global Type Pool</a> </div> |
| 213 | <div class="doc_text"> |
| 214 | </div> |
| 215 | <!-- _______________________________________________________________________ --> |
| 216 | <div class="doc_subsection"><a name="modinfo">Module Info</a> </div> |
| 217 | <div class="doc_text"> |
| 218 | </div> |
| 219 | <!-- _______________________________________________________________________ --> |
| 220 | <div class="doc_subsection"><a name="constants">Constants</a> </div> |
| 221 | <div class="doc_text"> |
| 222 | </div> |
| 223 | <!-- _______________________________________________________________________ --> |
| 224 | <div class="doc_subsection"><a name="functions">Functions</a> </div> |
| 225 | <div class="doc_text"> |
| 226 | </div> |
| 227 | <!-- _______________________________________________________________________ --> |
| 228 | <div class="doc_subsection"><a name="symtab">Module Symbol Table</a> </div> |
| 229 | <div class="doc_text"> |
| 230 | </div> |
| 231 | |
| 232 | <!-- *********************************************************************** --> |
| 233 | <hr> |
| 234 | <address> |
| 235 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img |
| 236 | src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a> |
| 237 | <a href="http://validator.w3.org/check/referer"><img |
| 238 | src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a> |
| 239 | |
| 240 | <a href="mailto:rspencer@x10sys.com">Reid Spencer</a> and |
| 241 | <a href="mailto:sabre@nondot.org">Chris Lattner</a><br> |
| 242 | <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br> |
| 243 | Last modified: $Date$ |
| 244 | </address> |
| 245 | </body> |
| 246 | </html> |
| 247 | <!-- vim: sw=2 |
| 248 | --> |