summaryrefslogtreecommitdiff
path: root/docs/instruction-formats.html
blob: ada1bb26cdd2c85410fae864b76b57177f5e4d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title>Dalvik VM Instruction Formats</title>
<link rel=stylesheet href="instruction-formats.css">
</head>

<body>

<h1>Dalvik VM Instruction Formats</h1>
<p>Copyright &copy; 2007 The Android Open Source Project

<h2>Introduction and Overview</h2>

<p>This document lists the instruction formats used by Dalvik bytecode
and is meant to be used in conjunction with the
<a href="dalvik-bytecode.html">bytecode reference document</a>.</p>

<h3>Bitwise descriptions</h3>

<p>The first column in the format table lists the bitwise layout of
the format. It consists of one or more space-separated "words" each of
which describes a 16-bit code unit. Each character in a word
represents four bits, read from high bits to low, with vertical bars
("<code>|</code>") interspersed to aid in reading. Uppercase letters
in sequence from "<code>A</code>" are used to indicate fields within
the format (which then get defined further by the syntax column). The term
"<code>op</code>" is used to indicate the position of an eight-bit
opcode within the format, and similarly "<code>exop</code>" is used
to indicate an extended sixteen-bit opcode. A slashed zero
("<code>&Oslash;</code>") is used to indicate that all bits must be
zero in the indicated position.</p>

<p>For the most part, lettering proceeds from earlier code units to
later code units, and low-order to high-order within a code unit.
However, there are a few exceptions to this general rule, which are
done in order to make the naming of similar-meaning parts be the same
across different instruction formats. These cases are noted explicitly
in the format descriptions.</p>

<p>For example, the format "<code>B|A|<i>op</i> CCCC</code>" indicates
that the format consists of two 16-bit code units. The first word
consists of the opcode in the low eight bits and a pair of four-bit
values in the high eight bits; and the second word consists of a single
16-bit value.</p>

<h3>Format IDs</h3>

<p>The second column in the format table indicates the short identifier
for the format, which is used in other documents and in code to identify
the format.</p>

<p>Most format IDs consist of three characters, two digits followed by a
letter. The first digit indicates the number of 16-bit code units in the
format. The second digit indicates the maximum number of registers that the
format contains (maximum, since some formats can accomodate a variable
number of registers), with the special designation "<code>r</code>" indicating
that a range of registers is encoded. The final letter semi-mnemonically
indicates the type of any extra data encoded by the format. For example,
format "<code>21t</code>" is of length two, contains one register reference,
and additionally contains a branch target.</p>

<p>Suggested static linking formats have an additional
"<code>s</code>" suffix, making them four characters total. Similarly,
suggested "inline" linking formats have an additional "<code>i</code>"
suffix. (In this context, inline linking is like static linking,
except with more direct ties into a virtual machine's implementation.) 
Finally, a couple oddball suggested formats (e.g.,
"<code>20bc</code>") include two pieces of data which are both
represented in its format ID.</p>

<p>The full list of typecode letters are as follows. Note that some
forms have different sizes, depending on the format:</p>

<table class="letters">
<thead>
<tr>
  <th>Mnemonic</th>
  <th>Bit Sizes</th>
  <th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
  <td>b</td>
  <td>8</td>
  <td>immediate signed <b>b</b>yte</td>
</tr>
<tr>
  <td>c</td>
  <td>16, 32</td>
  <td><b>c</b>onstant pool index</td>
</tr>
<tr>
  <td>f</td>
  <td>16</td>
  <td>inter<b>f</b>ace constants (only used in statically linked formats)
  </td>
</tr>
<tr>
  <td>h</td>
  <td>16</td>
  <td>immediate signed <b>h</b>at (high-order bits of a 32- or 64-bit
    value; low-order bits are all <code>0</code>)
  </td>
</tr>
<tr>
  <td>i</td>
  <td>32</td>
  <td>immediate signed <b>i</b>nt, or 32-bit float</td>
</tr>
<tr>
  <td>l</td>
  <td>64</td>
  <td>immediate signed <b>l</b>ong, or 64-bit double</td>
</tr>
<tr>
  <td>m</td>
  <td>16</td>
  <td><b>m</b>ethod constants (only used in statically linked formats)</td>
</tr>
<tr>
  <td>n</td>
  <td>4</td>
  <td>immediate signed <b>n</b>ibble</td>
</tr>
<tr>
  <td>s</td>
  <td>16</td>
  <td>immediate signed <b>s</b>hort</td>
</tr>
<tr>
  <td>t</td>
  <td>8, 16, 32</td>
  <td>branch <b>t</b>arget</td>
</tr>
<tr>
  <td>x</td>
  <td>0</td>
  <td>no additional data</td>
</tr>
</tbody>
</table>

<h3>Syntax</h3>

<p>The third column of the format table indicates the human-oriented
syntax for instructions which use the indicated format. Each instruction
starts with the named opcode and is optionally followed by one or
more arguments, themselves separated with commas.</p>

<p>Wherever an argument refers to a field from the first column, the
letter for that field is indicated in the syntax, repeated once for
each four bits of the field. For example, an eight-bit field labeled
"<code>BB</code>" in the first column would also be labeled
"<code>BB</code>" in the syntax column.</p>

<p>Arguments which name a register have the form "<code>v<i>X</i></code>".
The prefix "<code>v</code>" was chosen instead of the more common
"<code>r</code>" exactly to avoid conflicting with (non-virtual) architectures
on which a Dalvik virtual machine might be implemented which themselves
use the prefix "<code>r</code>" for their registers. (That is, this
decision makes it possible to talk about both virtual and real registers
together without the need for circumlocution.)</p>

<p>Arguments which indicate a literal value have the form
"<code>#+<i>X</i></code>". Some formats indicate literals that only
have non-zero bits in their high-order bits; for these, the zeroes
are represented explicitly in the syntax, even though they do not
appear in the bitwise representation.</p>

<p>Arguments which indicate a relative instruction address offset have the
form "<code>+<i>X</i></code>".</p>

<p>Arguments which indicate a literal constant pool index have the form
"<code><i>kind</i>@<i>X</i></code>", where "<code><i>kind</i></code>"
indicates which constant pool is being referred to. Each opcode that
uses such a format explicitly allows only one kind of constant; see
the opcode reference to figure out the correspondence. The four
kinds of constant pool are "<code>string</code>" (string pool index),
"<code>type</code>" (type pool index), "<code>field</code>" (field
pool index), and "<code>meth</code>" (method pool index).</p>

<p>Similar to the representation of constant pool indices, there are
also suggested (optional) forms that indicate prelinked offsets or
indices. There are two types of suggested prelinked value: vtable offsets
(indicated as "<code>vtaboff</code>") and field offsets (indicated as
"<code>fieldoff</code>").</p>

<p>In the cases where a format value isn't explictly part of the syntax
but instead picks a variant, each variant is listed with the prefix
"<code>[<i>X</i>=<i>N</i>]</code>" (e.g., "<code>[A=2]</code>") to indicate
the correspondence.</p>

<h2>The Formats</h2>

<table class="format">
<thead>
<tr>
  <th>Format</th>
  <th>ID</th>
  <th>Syntax</th>
  <th>Notable Opcodes Covered</th>
</tr>
</thead>
<tbody>
<tr>
  <td><i>N/A</i></td>
  <td>00x</td>
  <td><i><code>N/A</code></i></td>
  <td><i>pseudo-format used for unused opcodes; suggested for use as the
    nominal format for a breakpoint opcode</i></td>
</tr>
<tr>
  <td>&Oslash;&Oslash;|<i>op</i></td>
  <td>10x</td>
  <td><i><code>op</code></i></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td rowspan="2">B|A|<i>op</i></td>
  <td>12x</td>
  <td><i><code>op</code></i> vA, vB</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>11n</td>
  <td><i><code>op</code></i> vA, #+B</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td rowspan="2">AA|<i>op</i></td>
  <td>11x</td>
  <td><i><code>op</code></i> vAA</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>10t</td>
  <td><i><code>op</code></i> +AA</td>
  <td>goto</td>
</tr>
<tr>
  <td>&Oslash;&Oslash;|<i>op</i> AAAA</td></td>
  <td>20t</td>
  <td><i><code>op</code></i> +AAAA</td>
  <td>goto/16</td>
</tr>
<tr>
  <td>AA|<i>op</i> BBBB</td></td>
  <td>20bc</td>
  <td><i><code>op</code></i> AA, kind@BBBB</td>
  <td><i>suggested format for statically determined verification errors;
    A is the type of error and B is an index into a type-appropriate
    table (e.g. method references for a no-such-method error)</i></td>
</tr>
<tr>
  <td rowspan="5">AA|<i>op</i> BBBB</td>
  <td>22x</td>
  <td><i><code>op</code></i> vAA, vBBBB</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>21t</td>
  <td><i><code>op</code></i> vAA, +BBBB</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>21s</td>
  <td><i><code>op</code></i> vAA, #+BBBB</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>21h</td>
  <td><i><code>op</code></i> vAA, #+BBBB0000<br/>
    <i><code>op</code></i> vAA, #+BBBB000000000000
  </td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>21c</td>
  <td><i><code>op</code></i> vAA, type@BBBB<br/>
    <i><code>op</code></i> vAA, field@BBBB<br/>
    <i><code>op</code></i> vAA, string@BBBB
  </td>
  <td>check-cast<br/>
    const-class<br/>
    const-string
  </td>
</tr>
<tr>
  <td rowspan="2">AA|<i>op</i> CC|BB</td>
  <td>23x</td>
  <td><i><code>op</code></i> vAA, vBB, vCC</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>22b</td>
  <td><i><code>op</code></i> vAA, vBB, #+CC</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td rowspan="4">B|A|<i>op</i> CCCC</td>
  <td>22t</td>
  <td><i><code>op</code></i> vA, vB, +CCCC</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>22s</td>
  <td><i><code>op</code></i> vA, vB, #+CCCC</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>22c</td>
  <td><i><code>op</code></i> vA, vB, type@CCCC<br/>
    <i><code>op</code></i> vA, vB, field@CCCC
  </td>
  <td>instance-of</td>
</tr>
<tr>
  <td>22cs</td>
  <td><i><code>op</code></i> vA, vB, fieldoff@CCCC</td>
  <td><i>suggested format for statically linked field access instructions of
    format 22c</i>
  </td>
</tr>
<tr>
  <td>&Oslash;&Oslash;|<i>op</i> AAAA<sub>lo</sub> AAAA<sub>hi</sub></td></td>
  <td>30t</td>
  <td><i><code>op</code></i> +AAAAAAAA</td>
  <td>goto/32</td>
</tr>
<tr>
  <td>&Oslash;&Oslash;|<i>op</i> AAAA BBBB</td>
  <td>32x</td>
  <td><i><code>op</code></i> vAAAA, vBBBB</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td rowspan="3">AA|<i>op</i> BBBB<sub>lo</sub> BBBB<sub>hi</sub></td>
  <td>31i</td>
  <td><i><code>op</code></i> vAA, #+BBBBBBBB</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>31t</td>
  <td><i><code>op</code></i> vAA, +BBBBBBBB</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>31c</td>
  <td><i><code>op</code></i> vAA, string@BBBBBBBB</td>
  <td>const-string/jumbo</td>
</tr>
<tr>
  <td rowspan="3">A|G|<i>op</i> BBBB F|E|D|C</td>
  <td>35c</td>
  <td><i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG},
    meth@BBBB<br/>
    <i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG},
    type@BBBB<br/>
    <i>[<code>A=4</code>] <code>op</code></i> {vC, vD, vE, vF},
    <i><code>kind</code></i>@BBBB<br/>
    <i>[<code>A=3</code>] <code>op</code></i> {vC, vD, vE},
    <i><code>kind</code></i>@BBBB<br/>
    <i>[<code>A=2</code>] <code>op</code></i> {vC, vD},
    <i><code>kind</code></i>@BBBB<br/>
    <i>[<code>A=1</code>] <code>op</code></i> {vC},
    <i><code>kind</code></i>@BBBB<br/>
    <i>[<code>A=0</code>] <code>op</code></i> {},
    <i><code>kind</code></i>@BBBB<br/>
    <p><i>The unusual choice in lettering here reflects a desire to make
    the count and the reference index have the same label as in format
    3rc.</i></p>
  </td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>35ms</td>
  <td><i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG},
    vtaboff@BBBB<br/>
    <i>[<code>A=4</code>] <code>op</code></i> {vC, vD, vE, vF},
    vtaboff@BBBB<br/>
    <i>[<code>A=3</code>] <code>op</code></i> {vC, vD, vE},
    vtaboff@BBBB<br/>
    <i>[<code>A=2</code>] <code>op</code></i> {vC, vD},
    vtaboff@BBBB<br/>
    <i>[<code>A=1</code>] <code>op</code></i> {vC},
    vtaboff@BBBB<br/>
    <p><i>The unusual choice in lettering here reflects a desire to make
    the count and the reference index have the same label as in format
    3rms.</i></p>
  </td>
  <td><i>suggested format for statically linked <code>invoke-virtual</code>
    and <code>invoke-super</code> instructions of format 35c</i>
  </td>
</tr>
<tr>
  <td>35mi</td>
  <td><i>[<code>A=5</code>] <code>op</code></i> {vC, vD, vE, vF, vG},
    inline@BBBB<br/>
    <i>[<code>A=4</code>] <code>op</code></i> {vC, vD, vE, vF},
    inline@BBBB<br/>
    <i>[<code>A=3</code>] <code>op</code></i> {vC, vD, vE},
    inline@BBBB<br/>
    <i>[<code>A=2</code>] <code>op</code></i> {vC, vD},
    inline@BBBB<br/>
    <i>[<code>A=1</code>] <code>op</code></i> {vC},
    inline@BBBB<br/>
    <p><i>The unusual choice in lettering here reflects a desire to make
    the count and the reference index have the same label as in format
    3rmi.</i></p>
  </td>
  <td><i>suggested format for inline linked <code>invoke-static</code>
    and <code>invoke-virtual</code> instructions of format 35c</i>
  </td>
</tr>
<tr>
  <td rowspan="3">AA|<i>op</i> BBBB CCCC</td>
  <td>3rc</td>
  <td><i><code>op</code></i> {vCCCC .. vNNNN}, meth@BBBB<br/>
    <i><code>op</code></i> {vCCCC .. vNNNN}, type@BBBB<br/>
    <p><i>where <code>NNNN = CCCC+AA-1</code>, that is <code>A</code>
    determines the count <code>0..255</code>, and <code>C</code>
    determines the first register</i></p>
  </td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>3rms</td>
  <td><i><code>op</code></i> {vCCCC .. vNNNN}, vtaboff@BBBB<br/>
    <p><i>where <code>NNNN = CCCC+AA-1</code>, that is <code>A</code>
    determines the count <code>0..255</code>, and <code>C</code>
    determines the first register</i></p>
  </td>
  <td><i>suggested format for statically linked <code>invoke-virtual</code>
    and <code>invoke-super</code> instructions of format <code>3rc</code></i>
  </td>
</tr>
<tr>
  <td>3rmi</td>
  <td><i><code>op</code></i> {vCCCC .. vNNNN}, inline@BBBB<br/>
    <p><i>where <code>NNNN = CCCC+AA-1</code>, that is <code>A</code>
    determines the count <code>0..255</code>, and <code>C</code>
    determines the first register</i></p>
  </td>
  <td><i>suggested format for inline linked <code>invoke-static</code>
    and <code>invoke-virtual</code> instructions of format 3rc</i>
  </td>
</tr>
<tr>
  <td>AA|<i>op</i> BBBB<sub>lo</sub> BBBB BBBB BBBB<sub>hi</sub></td>
  <td>51l</td>
  <td><i><code>op</code></i> vAA, #+BBBBBBBBBBBBBBBB</td>
  <td>const-wide</td>
</tr>
<tr>
  <td rowspan="2"><i>exop</i> BB|AA CCCC</td>
  <td>33x</td>
  <td><i><code>exop</code></i> vAA, vBB, vCCCC</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>32s</td>
  <td><i><code>exop</code></i> vAA, vBB, #+CCCC</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><i>exop</i> BBBB<sub>lo</sub> BBBB<sub>hi</sub> AAAA</td></td>
  <td>40sc</td>
  <td><i><code>exop</code></i> AAAA, kind@BBBBBBBB</td>
  <td><i>suggested format for statically determined verification errors;
    see <code>20bc</code>, above</i></td>
</tr>
<tr>
  <td><i>exop</i> BBBB<sub>lo</sub> BBBB<sub>hi</sub> AAAA
  <td>41c</td>
  <td><i><code>exop</code></i> vAAAA, field@BBBBBBBB<br/>
    <i><code>exop</code></i> vAAAA, type@BBBBBBBB
    <p><i>The unusual choice in lettering here reflects a desire to make
    the letters match their use in related formats 21c and 31c.</i></p>
  </td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><i>exop</i> CCCC<sub>lo</sub> CCCC<sub>hi</sub>
    AAAA BBBB</td>
  <td>52c</td>
  <td><i><code>exop</code></i> vAAAA, vBBBB, field@CCCCCCCC<br/>
    <i><code>exop</code></i> vAAAA, vBBBB, type@CCCCCCCC
    <p><i>The unusual choice in lettering here reflects a desire to make
    the letters match their use in related formats 22c and 22cs.</i></p>
  </td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><i>exop</i> BBBB<sub>lo</sub> BBBB<sub>hi</sub>
    AAAA CCCC</td>
  <td>5rc</td>
  <td><i><code>exop</code></i> {vCCCC .. vNNNN}, meth@BBBBBBBB<br/>
    <i><code>exop</code></i> {vCCCC .. vNNNN}, type@BBBBBBBB<br/>
    <p><i>where <code>NNNN = CCCC+AAAA-1</code>, that is <code>A</code>
    determines the count <code>0..65535</code>, and <code>C</code>
    determines the first register</i></p>
    <p><i>The unusual choice in lettering here reflects a desire to make
    the letters match their use in related formats 3rc, 3rms, and 3rmi.</i></p>
  </td>
  <td>&nbsp;</td>
</tr>
</tbody>
</table>

</body>
</html>