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