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