1 page.title=Dalvik VM Instruction Formats 2 @jd:body 3 4 <!-- 5 Copyright 2010 The Android Open Source Project 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 --> 19 <p>Copyright © 2007 The Android Open Source Project 20 21 <h2>Introduction and Overview</h2> 22 23 <p>This document lists the instruction formats used by Dalvik bytecode 24 and is meant to be used in conjunction with the 25 <a href="dalvik-bytecode.html">bytecode reference document</a>.</p> 26 27 <h3>Bitwise descriptions</h3> 28 29 <p>The first column in the format table lists the bitwise layout of 30 the format. It consists of one or more space-separated "words" each of 31 which describes a 16-bit code unit. Each character in a word 32 represents four bits, read from high bits to low, with vertical bars 33 ("<code>|</code>") interspersed to aid in reading. Uppercase letters 34 in sequence from "<code>A</code>" are used to indicate fields within 35 the format (which then get defined further by the syntax column). The term 36 "<code>op</code>" is used to indicate the position of an eight-bit 37 opcode within the format. A slashed zero 38 ("<code>Ø</code>") is used to indicate that all bits must be 39 zero in the indicated position.</p> 40 41 <p>For the most part, lettering proceeds from earlier code units to 42 later code units, and low-order to high-order within a code unit. 43 However, there are a few exceptions to this general rule, which are 44 done in order to make the naming of similar-meaning parts be the same 45 across different instruction formats. These cases are noted explicitly 46 in the format descriptions.</p> 47 48 <p>For example, the format "<code>B|A|<i>op</i> CCCC</code>" indicates 49 that the format consists of two 16-bit code units. The first word 50 consists of the opcode in the low eight bits and a pair of four-bit 51 values in the high eight bits; and the second word consists of a single 52 16-bit value.</p> 53 54 <h3>Format IDs</h3> 55 56 <p>The second column in the format table indicates the short identifier 57 for the format, which is used in other documents and in code to identify 58 the format.</p> 59 60 <p>Most format IDs consist of three characters, two digits followed by a 61 letter. The first digit indicates the number of 16-bit code units in the 62 format. The second digit indicates the maximum number of registers that the 63 format contains (maximum, since some formats can accomodate a variable 64 number of registers), with the special designation "<code>r</code>" indicating 65 that a range of registers is encoded. The final letter semi-mnemonically 66 indicates the type of any extra data encoded by the format. For example, 67 format "<code>21t</code>" is of length two, contains one register reference, 68 and additionally contains a branch target.</p> 69 70 <p>Suggested static linking formats have an additional 71 "<code>s</code>" suffix, making them four characters total. Similarly, 72 suggested "inline" linking formats have an additional "<code>i</code>" 73 suffix. (In this context, inline linking is like static linking, 74 except with more direct ties into a virtual machine's implementation.) 75 Finally, a couple oddball suggested formats (e.g., 76 "<code>20bc</code>") include two pieces of data which are both 77 represented in its format ID.</p> 78 79 <p>The full list of typecode letters are as follows. Note that some 80 forms have different sizes, depending on the format:</p> 81 82 <table class="letters"> 83 <thead> 84 <tr> 85 <th>Mnemonic</th> 86 <th>Bit Sizes</th> 87 <th>Meaning</th> 88 </tr> 89 </thead> 90 <tbody> 91 <tr> 92 <td>b</td> 93 <td>8</td> 94 <td>immediate signed <b>b</b>yte</td> 95 </tr> 96 <tr> 97 <td>c</td> 98 <td>16, 32</td> 99 <td><b>c</b>onstant pool index</td> 100 </tr> 101 <tr> 102 <td>f</td> 103 <td>16</td> 104 <td>inter<b>f</b>ace constants (only used in statically linked formats) 105 </td> 106 </tr> 107 <tr> 108 <td>h</td> 109 <td>16</td> 110 <td>immediate signed <b>h</b>at (high-order bits of a 32- or 64-bit 111 value; low-order bits are all <code>0</code>) 112 </td> 113 </tr> 114 <tr> 115 <td>i</td> 116 <td>32</td> 117 <td>immediate signed <b>i</b>nt, or 32-bit float</td> 118 </tr> 119 <tr> 120 <td>l</td> 121 <td>64</td> 122 <td>immediate signed <b>l</b>ong, or 64-bit double</td> 123 </tr> 124 <tr> 125 <td>m</td> 126 <td>16</td> 127 <td><b>m</b>ethod constants (only used in statically linked formats)</td> 128 </tr> 129 <tr> 130 <td>n</td> 131 <td>4</td> 132 <td>immediate signed <b>n</b>ibble</td> 133 </tr> 134 <tr> 135 <td>s</td> 136 <td>16</td> 137 <td>immediate signed <b>s</b>hort</td> 138 </tr> 139 <tr> 140 <td>t</td> 141 <td>8, 16, 32</td> 142 <td>branch <b>t</b>arget</td> 143 </tr> 144 <tr> 145 <td>x</td> 146 <td>0</td> 147 <td>no additional data</td> 148 </tr> 149 </tbody> 150 </table> 151 152 <h3>Syntax</h3> 153 154 <p>The third column of the format table indicates the human-oriented 155 syntax for instructions which use the indicated format. Each instruction 156 starts with the named opcode and is optionally followed by one or 157 more arguments, themselves separated with commas.</p> 158 159 <p>Wherever an argument refers to a field from the first column, the 160 letter for that field is indicated in the syntax, repeated once for 161 each four bits of the field. For example, an eight-bit field labeled 162 "<code>BB</code>" in the first column would also be labeled 163 "<code>BB</code>" in the syntax column.</p> 164 165 <p>Arguments which name a register have the form "<code>v<i>X</i></code>". 166 The prefix "<code>v</code>" was chosen instead of the more common 167 "<code>r</code>" exactly to avoid conflicting with (non-virtual) architectures 168 on which a Dalvik virtual machine might be implemented which themselves 169 use the prefix "<code>r</code>" for their registers. (That is, this 170 decision makes it possible to talk about both virtual and real registers 171 together without the need for circumlocution.)</p> 172 173 <p>Arguments which indicate a literal value have the form 174 "<code>#+<i>X</i></code>". Some formats indicate literals that only 175 have non-zero bits in their high-order bits; for these, the zeroes 176 are represented explicitly in the syntax, even though they do not 177 appear in the bitwise representation.</p> 178 179 <p>Arguments which indicate a relative instruction address offset have the 180 form "<code>+<i>X</i></code>".</p> 181 182 <p>Arguments which indicate a literal constant pool index have the form 183 "<code><i>kind</i>@<i>X</i></code>", where "<code><i>kind</i></code>" 184 indicates which constant pool is being referred to. Each opcode that 185 uses such a format explicitly allows only one kind of constant; see 186 the opcode reference to figure out the correspondence. The four 187 kinds of constant pool are "<code>string</code>" (string pool index), 188 "<code>type</code>" (type pool index), "<code>field</code>" (field 189 pool index), and "<code>meth</code>" (method pool index).</p> 190 191 <p>Similar to the representation of constant pool indices, there are 192 also suggested (optional) forms that indicate prelinked offsets or 193 indices. There are two types of suggested prelinked value: vtable offsets 194 (indicated as "<code>vtaboff</code>") and field offsets (indicated as 195 "<code>fieldoff</code>").</p> 196 197 <p>In the cases where a format value isn't explictly part of the syntax 198 but instead picks a variant, each variant is listed with the prefix 199 "<code>[<i>X</i>=<i>N</i>]</code>" (e.g., "<code>[A=2]</code>") to indicate 200 the correspondence.</p> 201 202 <h2>The Formats</h2> 203 204 <table class="format"> 205 <thead> 206 <tr> 207 <th>Format</th> 208 <th>ID</th> 209 <th>Syntax</th> 210 <th>Notable Opcodes Covered</th> 211 </tr> 212 </thead> 213 <tbody> 214 <tr> 215 <td><i>N/A</i></td> 216 <td>00x</td> 217 <td><i><code>N/A</code></i></td> 218 <td><i>pseudo-format used for unused opcodes; suggested for use as the 219 nominal format for a breakpoint opcode</i></td> 220 </tr> 221 <tr> 222 <td>ØØ|<i>op</i></td> 223 <td>10x</td> 224 <td><i><code>op</code></i></td> 225 <td> </td> 226 </tr> 227 <tr> 228 <td rowspan="2">B|A|<i>op</i></td> 229 <td>12x</td> 230 <td><i><code>op</code></i> vA, vB</td> 231 <td> </td> 232 </tr> 233 <tr> 234 <td>11n</td> 235 <td><i><code>op</code></i> vA, #+B</td> 236 <td> </td> 237 </tr> 238 <tr> 239 <td rowspan="2">AA|<i>op</i></td> 240 <td>11x</td> 241 <td><i><code>op</code></i> vAA</td> 242 <td> </td> 243 </tr> 244 <tr> 245 <td>10t</td> 246 <td><i><code>op</code></i> +AA</td> 247 <td>goto</td> 248 </tr> 249 <tr> 250 <td>ØØ|<i>op</i> AAAA</td></td> 251 <td>20t</td> 252 <td><i><code>op</code></i> +AAAA</td> 253 <td>goto/16</td> 254 </tr> 255 <tr> 256 <td>AA|<i>op</i> BBBB</td></td> 257 <td>20bc</td> 258 <td><i><code>op</code></i> AA, kind@BBBB</td> 259 <td><i>suggested format for statically determined verification errors; 260 A is the type of error and B is an index into a type-appropriate 261 table (e.g. method references for a no-such-method error)</i></td> 262 </tr> 263 <tr> 264 <td rowspan="5">AA|<i>op</i> BBBB</td> 265 <td>22x</td> 266 <td><i><code>op</code></i> vAA, vBBBB</td> 267 <td> </td> 268 </tr> 269 <tr> 270 <td>21t</td> 271 <td><i><code>op</code></i> vAA, +BBBB</td> 272 <td> </td> 273 </tr> 274 <tr> 275 <td>21s</td> 276 <td><i><code>op</code></i> vAA, #+BBBB</td> 277 <td> </td> 278 </tr> 279 <tr> 280 <td>21h</td> 281 <td><i><code>op</code></i> vAA, #+BBBB0000<br/> 282 <i><code>op</code></i> vAA, #+BBBB000000000000 283 </td> 284 <td> </td> 285 </tr> 286 <tr> 287 <td>21c</td> 288 <td><i><code>op</code></i> vAA, type@BBBB<br/> 289 <i><code>op</code></i> vAA, field@BBBB<br/> 290 <i><code>op</code></i> vAA, string@BBBB 291 </td> 292 <td>check-cast<br/> 293 const-class<br/> 294 const-string 295 </td> 296 </tr> 297 <tr> 298 <td rowspan="2">AA|<i>op</i> CC|BB</td> 299 <td>23x</td> 300 <td><i><code>op</code></i> vAA, vBB, vCC</td> 301 <td> </td> 302 </tr> 303 <tr> 304 <td>22b</td> 305 <td><i><code>op</code></i> vAA, vBB, #+CC</td> 306 <td> </td> 307 </tr> 308 <tr> 309 <td rowspan="4">B|A|<i>op</i> CCCC</td> 310 <td>22t</td> 311 <td><i><code>op</code></i> vA, vB, +CCCC</td> 312 <td> </td> 313 </tr> 314 <tr> 315 <td>22s</td> 316 <td><i><code>op</code></i> vA, vB, #+CCCC</td> 317 <td> </td> 318 </tr> 319 <tr> 320 <td>22c</td> 321 <td><i><code>op</code></i> vA, vB, type@CCCC<br/> 322 <i><code>op</code></i> vA, vB, field@CCCC 323 </td> 324 <td>instance-of</td> 325 </tr> 326 <tr> 327 <td>22cs</td> 328 <td><i><code>op</code></i> vA, vB, fieldoff@CCCC</td> 329 <td><i>suggested format for statically linked field access instructions of 330 format 22c</i> 331 </td> 332 </tr> 333 <tr> 334 <td>ØØ|<i>op</i> AAAA<sub>lo</sub> AAAA<sub>hi</sub></td></td> 335 <td>30t</td> 336 <td><i><code>op</code></i> +AAAAAAAA</td> 337 <td>goto/32</td> 338 </tr> 339 <tr> 340 <td>ØØ|<i>op</i> AAAA BBBB</td> 341 <td>32x</td> 342 <td><i><code>op</code></i> vAAAA, vBBBB</td> 343 <td> </td> 344 </tr> 345 <tr> 346 <td rowspan="3">AA|<i>op</i> BBBB<sub>lo</sub> BBBB<sub>hi</sub></td> 347 <td>31i</td> 348 <td><i><code>op</code></i> vAA, #+BBBBBBBB</td> 349 <td> </td> 350 </tr> 351 <tr> 352 <td>31t</td> 353 <td><i><code>op</code></i> vAA, +BBBBBBBB</td> 354 <td> </td> 355 </tr> 356 <tr> 357 <td>31c</td> 358 <td><i><code>op</code></i> vAA, string@BBBBBBBB</td> 359 <td>const-string/jumbo</td> 360 </tr> 361 <tr> 362 <td rowspan="3">A|G|<i>op</i> BBBB F|E|D|C</td> 363 <td>35c</td> 364 <td><i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG}, 365 meth@BBBB<br/> 366 <i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG}, 367 type@BBBB<br/> 368 <i>[<code>A=4</code>] <code>op</code></i> {vC, vD, vE, vF}, 369 <i><code>kind</code></i>@BBBB<br/> 370 <i>[<code>A=3</code>] <code>op</code></i> {vC, vD, vE}, 371 <i><code>kind</code></i>@BBBB<br/> 372 <i>[<code>A=2</code>] <code>op</code></i> {vC, vD}, 373 <i><code>kind</code></i>@BBBB<br/> 374 <i>[<code>A=1</code>] <code>op</code></i> {vC}, 375 <i><code>kind</code></i>@BBBB<br/> 376 <i>[<code>A=0</code>] <code>op</code></i> {}, 377 <i><code>kind</code></i>@BBBB<br/> 378 <p><i>The unusual choice in lettering here reflects a desire to make 379 the count and the reference index have the same label as in format 380 3rc.</i></p> 381 </td> 382 <td> </td> 383 </tr> 384 <tr> 385 <td>35ms</td> 386 <td><i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG}, 387 vtaboff@BBBB<br/> 388 <i>[<code>A=4</code>] <code>op</code></i> {vC, vD, vE, vF}, 389 vtaboff@BBBB<br/> 390 <i>[<code>A=3</code>] <code>op</code></i> {vC, vD, vE}, 391 vtaboff@BBBB<br/> 392 <i>[<code>A=2</code>] <code>op</code></i> {vC, vD}, 393 vtaboff@BBBB<br/> 394 <i>[<code>A=1</code>] <code>op</code></i> {vC}, 395 vtaboff@BBBB<br/> 396 <p><i>The unusual choice in lettering here reflects a desire to make 397 the count and the reference index have the same label as in format 398 3rms.</i></p> 399 </td> 400 <td><i>suggested format for statically linked <code>invoke-virtual</code> 401 and <code>invoke-super</code> instructions of format 35c</i> 402 </td> 403 </tr> 404 <tr> 405 <td>35mi</td> 406 <td><i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG}, 407 inline@BBBB<br/> 408 <i>[<code>A=4</code>] <code>op</code></i> {vC, vD, vE, vF}, 409 inline@BBBB<br/> 410 <i>[<code>A=3</code>] <code>op</code></i> {vC, vD, vE}, 411 inline@BBBB<br/> 412 <i>[<code>A=2</code>] <code>op</code></i> {vC, vD}, 413 inline@BBBB<br/> 414 <i>[<code>A=1</code>] <code>op</code></i> {vC}, 415 inline@BBBB<br/> 416 <p><i>The unusual choice in lettering here reflects a desire to make 417 the count and the reference index have the same label as in format 418 3rmi.</i></p> 419 </td> 420 <td><i>suggested format for inline linked <code>invoke-static</code> 421 and <code>invoke-virtual</code> instructions of format 35c</i> 422 </td> 423 </tr> 424 <tr> 425 <td rowspan="3">AA|<i>op</i> BBBB CCCC</td> 426 <td>3rc</td> 427 <td><i><code>op</code></i> {vCCCC .. vNNNN}, meth@BBBB<br/> 428 <i><code>op</code></i> {vCCCC .. vNNNN}, type@BBBB<br/> 429 <p><i>where <code>NNNN = CCCC+AA-1</code>, that is <code>A</code> 430 determines the count <code>0..255</code>, and <code>C</code> 431 determines the first register</i></p> 432 </td> 433 <td> </td> 434 </tr> 435 <tr> 436 <td>3rms</td> 437 <td><i><code>op</code></i> {vCCCC .. vNNNN}, vtaboff@BBBB<br/> 438 <p><i>where <code>NNNN = CCCC+AA-1</code>, that is <code>A</code> 439 determines the count <code>0..255</code>, and <code>C</code> 440 determines the first register</i></p> 441 </td> 442 <td><i>suggested format for statically linked <code>invoke-virtual</code> 443 and <code>invoke-super</code> instructions of format <code>3rc</code></i> 444 </td> 445 </tr> 446 <tr> 447 <td>3rmi</td> 448 <td><i><code>op</code></i> {vCCCC .. vNNNN}, inline@BBBB<br/> 449 <p><i>where <code>NNNN = CCCC+AA-1</code>, that is <code>A</code> 450 determines the count <code>0..255</code>, and <code>C</code> 451 determines the first register</i></p> 452 </td> 453 <td><i>suggested format for inline linked <code>invoke-static</code> 454 and <code>invoke-virtual</code> instructions of format 3rc</i> 455 </td> 456 </tr> 457 <tr> 458 <td>AA|<i>op</i> BBBB<sub>lo</sub> BBBB BBBB BBBB<sub>hi</sub></td> 459 <td>51l</td> 460 <td><i><code>op</code></i> vAA, #+BBBBBBBBBBBBBBBB</td> 461 <td>const-wide</td> 462 </tr> 463 </tbody> 464 </table>