1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * Dalvik instruction utility functions.
19 *
20 * IMPORTANT NOTE: Much of the contents of this file are generated
21 * automatically by the opcode-gen tool. Any edits to the generated
22 * sections will get wiped out the next time the tool is run.
23 */
24
25 #include "InstrUtils.h"
26 #include <stdlib.h>
27
28 /*
29 * Table that maps each opcode to the full width of instructions that
30 * use that opcode, in (16-bit) code units. Unimplemented opcodes as
31 * well as the "breakpoint" opcode have a width of zero.
32 */
33 static InstructionWidth gInstructionWidthTable[kNumPackedOpcodes] = {
34 // BEGIN(libdex-widths); GENERATED AUTOMATICALLY BY opcode-gen
35 1, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1,
36 1, 1, 1, 2, 3, 2, 2, 3, 5, 2, 2, 3, 2, 1, 1, 2,
37 2, 1, 2, 2, 3, 3, 3, 1, 1, 2, 3, 3, 3, 2, 2, 2,
38 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0,
39 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
40 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
41 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
42 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 0, 1, 1, 1, 1, 1,
43 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
45 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 3, 3,
50 3, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 0,
51 4, 4, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
52 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
53 4, 4, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4,
67 // END(libdex-widths)
68 };
69
70 /*
71 * Table that maps each opcode to the flags associated with that
72 * opcode.
73 */
74 static u1 gOpcodeFlagsTable[kNumPackedOpcodes] = {
75 // BEGIN(libdex-flags); GENERATED AUTOMATICALLY BY opcode-gen
76 kInstrCanContinue,
77 kInstrCanContinue,
78 kInstrCanContinue,
79 kInstrCanContinue,
80 kInstrCanContinue,
81 kInstrCanContinue,
82 kInstrCanContinue,
83 kInstrCanContinue,
84 kInstrCanContinue,
85 kInstrCanContinue,
86 kInstrCanContinue,
87 kInstrCanContinue,
88 kInstrCanContinue,
89 kInstrCanContinue,
90 kInstrCanReturn,
91 kInstrCanReturn,
92 kInstrCanReturn,
93 kInstrCanReturn,
94 kInstrCanContinue,
95 kInstrCanContinue,
96 kInstrCanContinue,
97 kInstrCanContinue,
98 kInstrCanContinue,
99 kInstrCanContinue,
100 kInstrCanContinue,
101 kInstrCanContinue,
102 kInstrCanContinue|kInstrCanThrow,
103 kInstrCanContinue|kInstrCanThrow,
104 kInstrCanContinue|kInstrCanThrow,
105 kInstrCanContinue|kInstrCanThrow,
106 kInstrCanContinue|kInstrCanThrow,
107 kInstrCanContinue|kInstrCanThrow,
108 kInstrCanContinue|kInstrCanThrow,
109 kInstrCanContinue|kInstrCanThrow,
110 kInstrCanContinue|kInstrCanThrow,
111 kInstrCanContinue|kInstrCanThrow,
112 kInstrCanContinue|kInstrCanThrow,
113 kInstrCanContinue|kInstrCanThrow,
114 kInstrCanContinue,
115 kInstrCanThrow,
116 kInstrCanBranch,
117 kInstrCanBranch,
118 kInstrCanBranch,
119 kInstrCanContinue|kInstrCanSwitch,
120 kInstrCanContinue|kInstrCanSwitch,
121 kInstrCanContinue,
122 kInstrCanContinue,
123 kInstrCanContinue,
124 kInstrCanContinue,
125 kInstrCanContinue,
126 kInstrCanContinue|kInstrCanBranch,
127 kInstrCanContinue|kInstrCanBranch,
128 kInstrCanContinue|kInstrCanBranch,
129 kInstrCanContinue|kInstrCanBranch,
130 kInstrCanContinue|kInstrCanBranch,
131 kInstrCanContinue|kInstrCanBranch,
132 kInstrCanContinue|kInstrCanBranch,
133 kInstrCanContinue|kInstrCanBranch,
134 kInstrCanContinue|kInstrCanBranch,
135 kInstrCanContinue|kInstrCanBranch,
136 kInstrCanContinue|kInstrCanBranch,
137 kInstrCanContinue|kInstrCanBranch,
138 0,
139 0,
140 0,
141 0,
142 0,
143 0,
144 kInstrCanContinue|kInstrCanThrow,
145 kInstrCanContinue|kInstrCanThrow,
146 kInstrCanContinue|kInstrCanThrow,
147 kInstrCanContinue|kInstrCanThrow,
148 kInstrCanContinue|kInstrCanThrow,
149 kInstrCanContinue|kInstrCanThrow,
150 kInstrCanContinue|kInstrCanThrow,
151 kInstrCanContinue|kInstrCanThrow,
152 kInstrCanContinue|kInstrCanThrow,
153 kInstrCanContinue|kInstrCanThrow,
154 kInstrCanContinue|kInstrCanThrow,
155 kInstrCanContinue|kInstrCanThrow,
156 kInstrCanContinue|kInstrCanThrow,
157 kInstrCanContinue|kInstrCanThrow,
158 kInstrCanContinue|kInstrCanThrow,
159 kInstrCanContinue|kInstrCanThrow,
160 kInstrCanContinue|kInstrCanThrow,
161 kInstrCanContinue|kInstrCanThrow,
162 kInstrCanContinue|kInstrCanThrow,
163 kInstrCanContinue|kInstrCanThrow,
164 kInstrCanContinue|kInstrCanThrow,
165 kInstrCanContinue|kInstrCanThrow,
166 kInstrCanContinue|kInstrCanThrow,
167 kInstrCanContinue|kInstrCanThrow,
168 kInstrCanContinue|kInstrCanThrow,
169 kInstrCanContinue|kInstrCanThrow,
170 kInstrCanContinue|kInstrCanThrow,
171 kInstrCanContinue|kInstrCanThrow,
172 kInstrCanContinue|kInstrCanThrow,
173 kInstrCanContinue|kInstrCanThrow,
174 kInstrCanContinue|kInstrCanThrow,
175 kInstrCanContinue|kInstrCanThrow,
176 kInstrCanContinue|kInstrCanThrow,
177 kInstrCanContinue|kInstrCanThrow,
178 kInstrCanContinue|kInstrCanThrow,
179 kInstrCanContinue|kInstrCanThrow,
180 kInstrCanContinue|kInstrCanThrow,
181 kInstrCanContinue|kInstrCanThrow,
182 kInstrCanContinue|kInstrCanThrow,
183 kInstrCanContinue|kInstrCanThrow,
184 kInstrCanContinue|kInstrCanThrow,
185 kInstrCanContinue|kInstrCanThrow,
186 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
187 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
188 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
189 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
190 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
191 0,
192 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
193 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
194 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
195 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
196 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
197 0,
198 0,
199 kInstrCanContinue,
200 kInstrCanContinue,
201 kInstrCanContinue,
202 kInstrCanContinue,
203 kInstrCanContinue,
204 kInstrCanContinue,
205 kInstrCanContinue,
206 kInstrCanContinue,
207 kInstrCanContinue,
208 kInstrCanContinue,
209 kInstrCanContinue,
210 kInstrCanContinue,
211 kInstrCanContinue,
212 kInstrCanContinue,
213 kInstrCanContinue,
214 kInstrCanContinue,
215 kInstrCanContinue,
216 kInstrCanContinue,
217 kInstrCanContinue,
218 kInstrCanContinue,
219 kInstrCanContinue,
220 kInstrCanContinue,
221 kInstrCanContinue,
222 kInstrCanContinue,
223 kInstrCanContinue|kInstrCanThrow,
224 kInstrCanContinue|kInstrCanThrow,
225 kInstrCanContinue,
226 kInstrCanContinue,
227 kInstrCanContinue,
228 kInstrCanContinue,
229 kInstrCanContinue,
230 kInstrCanContinue,
231 kInstrCanContinue,
232 kInstrCanContinue,
233 kInstrCanContinue,
234 kInstrCanContinue|kInstrCanThrow,
235 kInstrCanContinue|kInstrCanThrow,
236 kInstrCanContinue,
237 kInstrCanContinue,
238 kInstrCanContinue,
239 kInstrCanContinue,
240 kInstrCanContinue,
241 kInstrCanContinue,
242 kInstrCanContinue,
243 kInstrCanContinue,
244 kInstrCanContinue,
245 kInstrCanContinue,
246 kInstrCanContinue,
247 kInstrCanContinue,
248 kInstrCanContinue,
249 kInstrCanContinue,
250 kInstrCanContinue,
251 kInstrCanContinue,
252 kInstrCanContinue,
253 kInstrCanContinue,
254 kInstrCanContinue,
255 kInstrCanContinue|kInstrCanThrow,
256 kInstrCanContinue|kInstrCanThrow,
257 kInstrCanContinue,
258 kInstrCanContinue,
259 kInstrCanContinue,
260 kInstrCanContinue,
261 kInstrCanContinue,
262 kInstrCanContinue,
263 kInstrCanContinue,
264 kInstrCanContinue,
265 kInstrCanContinue,
266 kInstrCanContinue|kInstrCanThrow,
267 kInstrCanContinue|kInstrCanThrow,
268 kInstrCanContinue,
269 kInstrCanContinue,
270 kInstrCanContinue,
271 kInstrCanContinue,
272 kInstrCanContinue,
273 kInstrCanContinue,
274 kInstrCanContinue,
275 kInstrCanContinue,
276 kInstrCanContinue,
277 kInstrCanContinue,
278 kInstrCanContinue,
279 kInstrCanContinue,
280 kInstrCanContinue,
281 kInstrCanContinue,
282 kInstrCanContinue,
283 kInstrCanContinue,
284 kInstrCanContinue,
285 kInstrCanContinue,
286 kInstrCanContinue,
287 kInstrCanContinue|kInstrCanThrow,
288 kInstrCanContinue|kInstrCanThrow,
289 kInstrCanContinue,
290 kInstrCanContinue,
291 kInstrCanContinue,
292 kInstrCanContinue,
293 kInstrCanContinue,
294 kInstrCanContinue,
295 kInstrCanContinue|kInstrCanThrow,
296 kInstrCanContinue|kInstrCanThrow,
297 kInstrCanContinue,
298 kInstrCanContinue,
299 kInstrCanContinue,
300 kInstrCanContinue,
301 kInstrCanContinue,
302 kInstrCanContinue,
303 kInstrCanContinue|kInstrCanThrow,
304 kInstrCanContinue|kInstrCanThrow,
305 kInstrCanContinue|kInstrCanThrow,
306 kInstrCanContinue|kInstrCanThrow,
307 kInstrCanContinue|kInstrCanThrow,
308 kInstrCanContinue|kInstrCanThrow,
309 kInstrCanContinue|kInstrCanThrow,
310 kInstrCanContinue|kInstrCanThrow,
311 kInstrCanContinue|kInstrCanThrow,
312 0,
313 kInstrCanThrow,
314 kInstrCanContinue|kInstrCanThrow,
315 kInstrCanContinue|kInstrCanThrow,
316 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
317 kInstrCanReturn,
318 kInstrCanContinue|kInstrCanThrow,
319 kInstrCanContinue|kInstrCanThrow,
320 kInstrCanContinue|kInstrCanThrow,
321 kInstrCanContinue|kInstrCanThrow,
322 kInstrCanContinue|kInstrCanThrow,
323 kInstrCanContinue|kInstrCanThrow,
324 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
325 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
326 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
327 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
328 kInstrCanContinue|kInstrCanThrow,
329 kInstrCanContinue|kInstrCanThrow,
330 kInstrCanContinue|kInstrCanThrow,
331 0,
332 kInstrCanContinue|kInstrCanThrow,
333 kInstrCanContinue|kInstrCanThrow,
334 kInstrCanContinue|kInstrCanThrow,
335 kInstrCanContinue|kInstrCanThrow,
336 kInstrCanContinue|kInstrCanThrow,
337 kInstrCanContinue|kInstrCanThrow,
338 kInstrCanContinue|kInstrCanThrow,
339 kInstrCanContinue|kInstrCanThrow,
340 kInstrCanContinue|kInstrCanThrow,
341 kInstrCanContinue|kInstrCanThrow,
342 kInstrCanContinue|kInstrCanThrow,
343 kInstrCanContinue|kInstrCanThrow,
344 kInstrCanContinue|kInstrCanThrow,
345 kInstrCanContinue|kInstrCanThrow,
346 kInstrCanContinue|kInstrCanThrow,
347 kInstrCanContinue|kInstrCanThrow,
348 kInstrCanContinue|kInstrCanThrow,
349 kInstrCanContinue|kInstrCanThrow,
350 kInstrCanContinue|kInstrCanThrow,
351 kInstrCanContinue|kInstrCanThrow,
352 kInstrCanContinue|kInstrCanThrow,
353 kInstrCanContinue|kInstrCanThrow,
354 kInstrCanContinue|kInstrCanThrow,
355 kInstrCanContinue|kInstrCanThrow,
356 kInstrCanContinue|kInstrCanThrow,
357 kInstrCanContinue|kInstrCanThrow,
358 kInstrCanContinue|kInstrCanThrow,
359 kInstrCanContinue|kInstrCanThrow,
360 kInstrCanContinue|kInstrCanThrow,
361 kInstrCanContinue|kInstrCanThrow,
362 kInstrCanContinue|kInstrCanThrow,
363 kInstrCanContinue|kInstrCanThrow,
364 kInstrCanContinue|kInstrCanThrow,
365 kInstrCanContinue|kInstrCanThrow,
366 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
367 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
368 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
369 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
370 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
371 0,
372 0,
373 0,
374 0,
375 0,
376 0,
377 0,
378 0,
379 0,
380 0,
381 0,
382 0,
383 0,
384 0,
385 0,
386 0,
387 0,
388 0,
389 0,
390 0,
391 0,
392 0,
393 0,
394 0,
395 0,
396 0,
397 0,
398 0,
399 0,
400 0,
401 0,
402 0,
403 0,
404 0,
405 0,
406 0,
407 0,
408 0,
409 0,
410 0,
411 0,
412 0,
413 0,
414 0,
415 0,
416 0,
417 0,
418 0,
419 0,
420 0,
421 0,
422 0,
423 0,
424 0,
425 0,
426 0,
427 0,
428 0,
429 0,
430 0,
431 0,
432 0,
433 0,
434 0,
435 0,
436 0,
437 0,
438 0,
439 0,
440 0,
441 0,
442 0,
443 0,
444 0,
445 0,
446 0,
447 0,
448 0,
449 0,
450 0,
451 0,
452 0,
453 0,
454 0,
455 0,
456 0,
457 0,
458 0,
459 0,
460 0,
461 0,
462 0,
463 0,
464 0,
465 0,
466 0,
467 0,
468 0,
469 0,
470 0,
471 0,
472 0,
473 0,
474 0,
475 0,
476 0,
477 0,
478 0,
479 0,
480 0,
481 0,
482 0,
483 0,
484 0,
485 0,
486 0,
487 0,
488 0,
489 0,
490 0,
491 0,
492 0,
493 0,
494 0,
495 0,
496 0,
497 0,
498 0,
499 0,
500 0,
501 0,
502 0,
503 0,
504 0,
505 0,
506 0,
507 0,
508 0,
509 0,
510 0,
511 0,
512 0,
513 0,
514 0,
515 0,
516 0,
517 0,
518 0,
519 0,
520 0,
521 0,
522 0,
523 0,
524 0,
525 0,
526 0,
527 0,
528 0,
529 0,
530 0,
531 0,
532 0,
533 0,
534 0,
535 0,
536 0,
537 0,
538 0,
539 0,
540 0,
541 0,
542 0,
543 0,
544 0,
545 0,
546 0,
547 0,
548 0,
549 0,
550 0,
551 0,
552 0,
553 0,
554 0,
555 0,
556 0,
557 0,
558 0,
559 0,
560 0,
561 0,
562 0,
563 0,
564 0,
565 0,
566 0,
567 0,
568 0,
569 0,
570 0,
571 0,
572 0,
573 0,
574 kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
575 kInstrCanContinue|kInstrCanThrow,
576 kInstrCanContinue|kInstrCanThrow,
577 kInstrCanContinue|kInstrCanThrow,
578 kInstrCanContinue|kInstrCanThrow,
579 kInstrCanContinue|kInstrCanThrow,
580 kInstrCanContinue|kInstrCanThrow,
581 kInstrCanContinue|kInstrCanThrow,
582 kInstrCanContinue|kInstrCanThrow,
583 kInstrCanContinue|kInstrCanThrow,
584 kInstrCanContinue|kInstrCanThrow,
585 kInstrCanContinue|kInstrCanThrow,
586 kInstrCanContinue|kInstrCanThrow,
587 kInstrCanThrow,
588 // END(libdex-flags)
589 };
590
591 /*
592 * Table that maps each opcode to the instruction format associated
593 * that opcode.
594 */
595 static u1 gInstructionFormatTable[kNumPackedOpcodes] = {
596 // BEGIN(libdex-formats); GENERATED AUTOMATICALLY BY opcode-gen
597 kFmt10x, kFmt12x, kFmt22x, kFmt32x, kFmt12x, kFmt22x, kFmt32x,
598 kFmt12x, kFmt22x, kFmt32x, kFmt11x, kFmt11x, kFmt11x, kFmt11x,
599 kFmt10x, kFmt11x, kFmt11x, kFmt11x, kFmt11n, kFmt21s, kFmt31i,
600 kFmt21h, kFmt21s, kFmt31i, kFmt51l, kFmt21h, kFmt21c, kFmt31c,
601 kFmt21c, kFmt11x, kFmt11x, kFmt21c, kFmt22c, kFmt12x, kFmt21c,
602 kFmt22c, kFmt35c, kFmt3rc, kFmt31t, kFmt11x, kFmt10t, kFmt20t,
603 kFmt30t, kFmt31t, kFmt31t, kFmt23x, kFmt23x, kFmt23x, kFmt23x,
604 kFmt23x, kFmt22t, kFmt22t, kFmt22t, kFmt22t, kFmt22t, kFmt22t,
605 kFmt21t, kFmt21t, kFmt21t, kFmt21t, kFmt21t, kFmt21t, kFmt00x,
606 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt23x, kFmt23x,
607 kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x,
608 kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt22c, kFmt22c,
609 kFmt22c, kFmt22c, kFmt22c, kFmt22c, kFmt22c, kFmt22c, kFmt22c,
610 kFmt22c, kFmt22c, kFmt22c, kFmt22c, kFmt22c, kFmt21c, kFmt21c,
611 kFmt21c, kFmt21c, kFmt21c, kFmt21c, kFmt21c, kFmt21c, kFmt21c,
612 kFmt21c, kFmt21c, kFmt21c, kFmt21c, kFmt21c, kFmt35c, kFmt35c,
613 kFmt35c, kFmt35c, kFmt35c, kFmt00x, kFmt3rc, kFmt3rc, kFmt3rc,
614 kFmt3rc, kFmt3rc, kFmt00x, kFmt00x, kFmt12x, kFmt12x, kFmt12x,
615 kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x,
616 kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x,
617 kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt23x, kFmt23x, kFmt23x,
618 kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x,
619 kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x,
620 kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x,
621 kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x, kFmt23x,
622 kFmt23x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x,
623 kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x,
624 kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x,
625 kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x,
626 kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt12x, kFmt22s, kFmt22s,
627 kFmt22s, kFmt22s, kFmt22s, kFmt22s, kFmt22s, kFmt22s, kFmt22b,
628 kFmt22b, kFmt22b, kFmt22b, kFmt22b, kFmt22b, kFmt22b, kFmt22b,
629 kFmt22b, kFmt22b, kFmt22b, kFmt22c, kFmt22c, kFmt21c, kFmt21c,
630 kFmt22c, kFmt22c, kFmt22c, kFmt21c, kFmt21c, kFmt00x, kFmt20bc,
631 kFmt35mi, kFmt3rmi, kFmt35c, kFmt10x, kFmt22cs, kFmt22cs, kFmt22cs,
632 kFmt22cs, kFmt22cs, kFmt22cs, kFmt35ms, kFmt3rms, kFmt35ms, kFmt3rms,
633 kFmt22c, kFmt21c, kFmt21c, kFmt00x, kFmt41c, kFmt41c, kFmt52c,
634 kFmt41c, kFmt52c, kFmt5rc, kFmt52c, kFmt52c, kFmt52c, kFmt52c,
635 kFmt52c, kFmt52c, kFmt52c, kFmt52c, kFmt52c, kFmt52c, kFmt52c,
636 kFmt52c, kFmt52c, kFmt52c, kFmt41c, kFmt41c, kFmt41c, kFmt41c,
637 kFmt41c, kFmt41c, kFmt41c, kFmt41c, kFmt41c, kFmt41c, kFmt41c,
638 kFmt41c, kFmt41c, kFmt41c, kFmt5rc, kFmt5rc, kFmt5rc, kFmt5rc,
639 kFmt5rc, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
640 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
641 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
642 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
643 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
644 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
645 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
646 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
647 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
648 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
649 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
650 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
651 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
652 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
653 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
654 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
655 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
656 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
657 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
658 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
659 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
660 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
661 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
662 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
663 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
664 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
665 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
666 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
667 kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x, kFmt00x,
668 kFmt00x, kFmt5rc, kFmt52c, kFmt52c, kFmt52c, kFmt52c, kFmt52c,
669 kFmt52c, kFmt41c, kFmt41c, kFmt41c, kFmt41c, kFmt41c, kFmt41c,
670 kFmt40sc,
671 // END(libdex-formats)
672 };
673
674 /*
675 * Table that maps each opcode to the index type implied by that
676 * opcode.
677 */
678 static u1 gInstructionIndexTypeTable[kNumPackedOpcodes] = {
679 // BEGIN(libdex-index-types); GENERATED AUTOMATICALLY BY opcode-gen
680 kIndexNone, kIndexNone, kIndexNone,
681 kIndexNone, kIndexNone, kIndexNone,
682 kIndexNone, kIndexNone, kIndexNone,
683 kIndexNone, kIndexNone, kIndexNone,
684 kIndexNone, kIndexNone, kIndexNone,
685 kIndexNone, kIndexNone, kIndexNone,
686 kIndexNone, kIndexNone, kIndexNone,
687 kIndexNone, kIndexNone, kIndexNone,
688 kIndexNone, kIndexNone, kIndexStringRef,
689 kIndexStringRef, kIndexTypeRef, kIndexNone,
690 kIndexNone, kIndexTypeRef, kIndexTypeRef,
691 kIndexNone, kIndexTypeRef, kIndexTypeRef,
692 kIndexTypeRef, kIndexTypeRef, kIndexNone,
693 kIndexNone, kIndexNone, kIndexNone,
694 kIndexNone, kIndexNone, kIndexNone,
695 kIndexNone, kIndexNone, kIndexNone,
696 kIndexNone, kIndexNone, kIndexNone,
697 kIndexNone, kIndexNone, kIndexNone,
698 kIndexNone, kIndexNone, kIndexNone,
699 kIndexNone, kIndexNone, kIndexNone,
700 kIndexNone, kIndexNone, kIndexUnknown,
701 kIndexUnknown, kIndexUnknown, kIndexUnknown,
702 kIndexUnknown, kIndexUnknown, kIndexNone,
703 kIndexNone, kIndexNone, kIndexNone,
704 kIndexNone, kIndexNone, kIndexNone,
705 kIndexNone, kIndexNone, kIndexNone,
706 kIndexNone, kIndexNone, kIndexNone,
707 kIndexNone, kIndexFieldRef, kIndexFieldRef,
708 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
709 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
710 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
711 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
712 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
713 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
714 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
715 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
716 kIndexFieldRef, kIndexFieldRef, kIndexMethodRef,
717 kIndexMethodRef, kIndexMethodRef, kIndexMethodRef,
718 kIndexMethodRef, kIndexUnknown, kIndexMethodRef,
719 kIndexMethodRef, kIndexMethodRef, kIndexMethodRef,
720 kIndexMethodRef, kIndexUnknown, kIndexUnknown,
721 kIndexNone, kIndexNone, kIndexNone,
722 kIndexNone, kIndexNone, kIndexNone,
723 kIndexNone, kIndexNone, kIndexNone,
724 kIndexNone, kIndexNone, kIndexNone,
725 kIndexNone, kIndexNone, kIndexNone,
726 kIndexNone, kIndexNone, kIndexNone,
727 kIndexNone, kIndexNone, kIndexNone,
728 kIndexNone, kIndexNone, kIndexNone,
729 kIndexNone, kIndexNone, kIndexNone,
730 kIndexNone, kIndexNone, kIndexNone,
731 kIndexNone, kIndexNone, kIndexNone,
732 kIndexNone, kIndexNone, kIndexNone,
733 kIndexNone, kIndexNone, kIndexNone,
734 kIndexNone, kIndexNone, kIndexNone,
735 kIndexNone, kIndexNone, kIndexNone,
736 kIndexNone, kIndexNone, kIndexNone,
737 kIndexNone, kIndexNone, kIndexNone,
738 kIndexNone, kIndexNone, kIndexNone,
739 kIndexNone, kIndexNone, kIndexNone,
740 kIndexNone, kIndexNone, kIndexNone,
741 kIndexNone, kIndexNone, kIndexNone,
742 kIndexNone, kIndexNone, kIndexNone,
743 kIndexNone, kIndexNone, kIndexNone,
744 kIndexNone, kIndexNone, kIndexNone,
745 kIndexNone, kIndexNone, kIndexNone,
746 kIndexNone, kIndexNone, kIndexNone,
747 kIndexNone, kIndexNone, kIndexNone,
748 kIndexNone, kIndexNone, kIndexNone,
749 kIndexNone, kIndexNone, kIndexNone,
750 kIndexNone, kIndexNone, kIndexNone,
751 kIndexNone, kIndexNone, kIndexNone,
752 kIndexNone, kIndexNone, kIndexNone,
753 kIndexNone, kIndexNone, kIndexNone,
754 kIndexNone, kIndexNone, kIndexNone,
755 kIndexNone, kIndexNone, kIndexFieldRef,
756 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
757 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
758 kIndexFieldRef, kIndexFieldRef, kIndexUnknown,
759 kIndexVaries, kIndexInlineMethod, kIndexInlineMethod,
760 kIndexMethodRef, kIndexNone, kIndexFieldOffset,
761 kIndexFieldOffset, kIndexFieldOffset, kIndexFieldOffset,
762 kIndexFieldOffset, kIndexFieldOffset, kIndexVtableOffset,
763 kIndexVtableOffset, kIndexVtableOffset, kIndexVtableOffset,
764 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
765 kIndexUnknown, kIndexTypeRef, kIndexTypeRef,
766 kIndexTypeRef, kIndexTypeRef, kIndexTypeRef,
767 kIndexTypeRef, kIndexFieldRef, kIndexFieldRef,
768 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
769 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
770 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
771 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
772 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
773 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
774 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
775 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
776 kIndexFieldRef, kIndexFieldRef, kIndexMethodRef,
777 kIndexMethodRef, kIndexMethodRef, kIndexMethodRef,
778 kIndexMethodRef, kIndexUnknown, kIndexUnknown,
779 kIndexUnknown, kIndexUnknown, kIndexUnknown,
780 kIndexUnknown, kIndexUnknown, kIndexUnknown,
781 kIndexUnknown, kIndexUnknown, kIndexUnknown,
782 kIndexUnknown, kIndexUnknown, kIndexUnknown,
783 kIndexUnknown, kIndexUnknown, kIndexUnknown,
784 kIndexUnknown, kIndexUnknown, kIndexUnknown,
785 kIndexUnknown, kIndexUnknown, kIndexUnknown,
786 kIndexUnknown, kIndexUnknown, kIndexUnknown,
787 kIndexUnknown, kIndexUnknown, kIndexUnknown,
788 kIndexUnknown, kIndexUnknown, kIndexUnknown,
789 kIndexUnknown, kIndexUnknown, kIndexUnknown,
790 kIndexUnknown, kIndexUnknown, kIndexUnknown,
791 kIndexUnknown, kIndexUnknown, kIndexUnknown,
792 kIndexUnknown, kIndexUnknown, kIndexUnknown,
793 kIndexUnknown, kIndexUnknown, kIndexUnknown,
794 kIndexUnknown, kIndexUnknown, kIndexUnknown,
795 kIndexUnknown, kIndexUnknown, kIndexUnknown,
796 kIndexUnknown, kIndexUnknown, kIndexUnknown,
797 kIndexUnknown, kIndexUnknown, kIndexUnknown,
798 kIndexUnknown, kIndexUnknown, kIndexUnknown,
799 kIndexUnknown, kIndexUnknown, kIndexUnknown,
800 kIndexUnknown, kIndexUnknown, kIndexUnknown,
801 kIndexUnknown, kIndexUnknown, kIndexUnknown,
802 kIndexUnknown, kIndexUnknown, kIndexUnknown,
803 kIndexUnknown, kIndexUnknown, kIndexUnknown,
804 kIndexUnknown, kIndexUnknown, kIndexUnknown,
805 kIndexUnknown, kIndexUnknown, kIndexUnknown,
806 kIndexUnknown, kIndexUnknown, kIndexUnknown,
807 kIndexUnknown, kIndexUnknown, kIndexUnknown,
808 kIndexUnknown, kIndexUnknown, kIndexUnknown,
809 kIndexUnknown, kIndexUnknown, kIndexUnknown,
810 kIndexUnknown, kIndexUnknown, kIndexUnknown,
811 kIndexUnknown, kIndexUnknown, kIndexUnknown,
812 kIndexUnknown, kIndexUnknown, kIndexUnknown,
813 kIndexUnknown, kIndexUnknown, kIndexUnknown,
814 kIndexUnknown, kIndexUnknown, kIndexUnknown,
815 kIndexUnknown, kIndexUnknown, kIndexUnknown,
816 kIndexUnknown, kIndexUnknown, kIndexUnknown,
817 kIndexUnknown, kIndexUnknown, kIndexUnknown,
818 kIndexUnknown, kIndexUnknown, kIndexUnknown,
819 kIndexUnknown, kIndexUnknown, kIndexUnknown,
820 kIndexUnknown, kIndexUnknown, kIndexUnknown,
821 kIndexUnknown, kIndexUnknown, kIndexUnknown,
822 kIndexUnknown, kIndexUnknown, kIndexUnknown,
823 kIndexUnknown, kIndexUnknown, kIndexUnknown,
824 kIndexUnknown, kIndexUnknown, kIndexUnknown,
825 kIndexUnknown, kIndexUnknown, kIndexUnknown,
826 kIndexUnknown, kIndexUnknown, kIndexUnknown,
827 kIndexUnknown, kIndexUnknown, kIndexUnknown,
828 kIndexUnknown, kIndexUnknown, kIndexUnknown,
829 kIndexUnknown, kIndexUnknown, kIndexUnknown,
830 kIndexUnknown, kIndexUnknown, kIndexUnknown,
831 kIndexUnknown, kIndexUnknown, kIndexUnknown,
832 kIndexUnknown, kIndexUnknown, kIndexUnknown,
833 kIndexUnknown, kIndexUnknown, kIndexUnknown,
834 kIndexUnknown, kIndexUnknown, kIndexUnknown,
835 kIndexUnknown, kIndexUnknown, kIndexUnknown,
836 kIndexUnknown, kIndexUnknown, kIndexUnknown,
837 kIndexUnknown, kIndexUnknown, kIndexUnknown,
838 kIndexUnknown, kIndexUnknown, kIndexUnknown,
839 kIndexUnknown, kIndexUnknown, kIndexUnknown,
840 kIndexUnknown, kIndexUnknown, kIndexUnknown,
841 kIndexUnknown, kIndexUnknown, kIndexUnknown,
842 kIndexUnknown, kIndexUnknown, kIndexUnknown,
843 kIndexUnknown, kIndexUnknown, kIndexUnknown,
844 kIndexUnknown, kIndexUnknown, kIndexUnknown,
845 kIndexUnknown, kIndexUnknown, kIndexUnknown,
846 kIndexMethodRef, kIndexFieldRef, kIndexFieldRef,
847 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
848 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
849 kIndexFieldRef, kIndexFieldRef, kIndexFieldRef,
850 kIndexFieldRef, kIndexVaries,
851 // END(libdex-index-types)
852 };
853
854 /*
855 * Global InstructionInfoTables struct.
856 */
857 InstructionInfoTables gDexOpcodeInfo = {
858 gInstructionFormatTable,
859 gInstructionIndexTypeTable,
860 gOpcodeFlagsTable,
861 gInstructionWidthTable
862 };
863
864 /*
865 * Handy macros for helping decode instructions.
866 */
867 #define FETCH(_offset) (insns[(_offset)])
868 #define FETCH_u4(_offset) (fetch_u4_impl((_offset), insns))
869 #define INST_A(_inst) (((u2)(_inst) >> 8) & 0x0f)
870 #define INST_B(_inst) ((u2)(_inst) >> 12)
871 #define INST_AA(_inst) ((_inst) >> 8)
872
873 /* Helper for FETCH_u4, above. */
fetch_u4_impl(u4 offset,const u2 * insns)874 static inline u4 fetch_u4_impl(u4 offset, const u2* insns) {
875 return insns[offset] | ((u4) insns[offset+1] << 16);
876 }
877
878 /*
879 * Decode the instruction pointed to by "insns".
880 *
881 * Fills out the pieces of "pDec" that are affected by the current
882 * instruction. Does not touch anything else.
883 */
dexDecodeInstruction(const u2 * insns,DecodedInstruction * pDec)884 void dexDecodeInstruction(const u2* insns, DecodedInstruction* pDec)
885 {
886 u2 inst = *insns;
887 Opcode opcode = dexOpcodeFromCodeUnit(inst);
888 InstructionFormat format = dexGetFormatFromOpcode(opcode);
889
890 pDec->opcode = opcode;
891 pDec->indexType = dexGetIndexTypeFromOpcode(opcode);
892
893 switch (format) {
894 case kFmt10x: // op
895 /* nothing to do; copy the AA bits out for the verifier */
896 pDec->vA = INST_AA(inst);
897 break;
898 case kFmt12x: // op vA, vB
899 pDec->vA = INST_A(inst);
900 pDec->vB = INST_B(inst);
901 break;
902 case kFmt11n: // op vA, #+B
903 pDec->vA = INST_A(inst);
904 pDec->vB = (s4) (INST_B(inst) << 28) >> 28; // sign extend 4-bit value
905 break;
906 case kFmt11x: // op vAA
907 pDec->vA = INST_AA(inst);
908 break;
909 case kFmt10t: // op +AA
910 pDec->vA = (s1) INST_AA(inst); // sign-extend 8-bit value
911 break;
912 case kFmt20t: // op +AAAA
913 pDec->vA = (s2) FETCH(1); // sign-extend 16-bit value
914 break;
915 case kFmt20bc: // [opt] op AA, thing@BBBB
916 case kFmt21c: // op vAA, thing@BBBB
917 case kFmt22x: // op vAA, vBBBB
918 pDec->vA = INST_AA(inst);
919 pDec->vB = FETCH(1);
920 break;
921 case kFmt21s: // op vAA, #+BBBB
922 case kFmt21t: // op vAA, +BBBB
923 pDec->vA = INST_AA(inst);
924 pDec->vB = (s2) FETCH(1); // sign-extend 16-bit value
925 break;
926 case kFmt21h: // op vAA, #+BBBB0000[00000000]
927 pDec->vA = INST_AA(inst);
928 /*
929 * The value should be treated as right-zero-extended, but we don't
930 * actually do that here. Among other things, we don't know if it's
931 * the top bits of a 32- or 64-bit value.
932 */
933 pDec->vB = FETCH(1);
934 break;
935 case kFmt23x: // op vAA, vBB, vCC
936 pDec->vA = INST_AA(inst);
937 pDec->vB = FETCH(1) & 0xff;
938 pDec->vC = FETCH(1) >> 8;
939 break;
940 case kFmt22b: // op vAA, vBB, #+CC
941 pDec->vA = INST_AA(inst);
942 pDec->vB = FETCH(1) & 0xff;
943 pDec->vC = (s1) (FETCH(1) >> 8); // sign-extend 8-bit value
944 break;
945 case kFmt22s: // op vA, vB, #+CCCC
946 case kFmt22t: // op vA, vB, +CCCC
947 pDec->vA = INST_A(inst);
948 pDec->vB = INST_B(inst);
949 pDec->vC = (s2) FETCH(1); // sign-extend 16-bit value
950 break;
951 case kFmt22c: // op vA, vB, thing@CCCC
952 case kFmt22cs: // [opt] op vA, vB, field offset CCCC
953 pDec->vA = INST_A(inst);
954 pDec->vB = INST_B(inst);
955 pDec->vC = FETCH(1);
956 break;
957 case kFmt30t: // op +AAAAAAAA
958 pDec->vA = FETCH_u4(1); // signed 32-bit value
959 break;
960 case kFmt31t: // op vAA, +BBBBBBBB
961 case kFmt31c: // op vAA, string@BBBBBBBB
962 pDec->vA = INST_AA(inst);
963 pDec->vB = FETCH_u4(1); // 32-bit value
964 break;
965 case kFmt32x: // op vAAAA, vBBBB
966 pDec->vA = FETCH(1);
967 pDec->vB = FETCH(2);
968 break;
969 case kFmt31i: // op vAA, #+BBBBBBBB
970 pDec->vA = INST_AA(inst);
971 pDec->vB = FETCH_u4(1); // signed 32-bit value
972 break;
973 case kFmt35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
974 case kFmt35ms: // [opt] invoke-virtual+super
975 case kFmt35mi: // [opt] inline invoke
976 {
977 /*
978 * Note that the fields mentioned in the spec don't appear in
979 * their "usual" positions here compared to most formats. This
980 * was done so that the field names for the argument count and
981 * reference index match between this format and the corresponding
982 * range formats (3rc and friends).
983 *
984 * Bottom line: The argument count is always in vA, and the
985 * method constant (or equivalent) is always in vB.
986 */
987 u2 regList;
988 int i, count;
989
990 pDec->vA = INST_B(inst); // This is labeled A in the spec.
991 pDec->vB = FETCH(1);
992 regList = FETCH(2);
993
994 count = pDec->vA;
995
996 /*
997 * Copy the argument registers into the arg[] array, and
998 * also copy the first argument (if any) into vC. (The
999 * DecodedInstruction structure doesn't have separate
1000 * fields for {vD, vE, vF, vG}, so there's no need to make
1001 * copies of those.) Note that cases 5..2 fall through.
1002 */
1003 switch (count) {
1004 case 5: {
1005 if (format == kFmt35mi) {
1006 /* A fifth arg is verboten for inline invokes. */
1007 LOGW("Invalid arg count in 35mi (5)");
1008 goto bail;
1009 }
1010 /*
1011 * Per note at the top of this format decoder, the
1012 * fifth argument comes from the A field in the
1013 * instruction, but it's labeled G in the spec.
1014 */
1015 pDec->arg[4] = INST_A(inst);
1016 }
1017 case 4: pDec->arg[3] = (regList >> 12) & 0x0f;
1018 case 3: pDec->arg[2] = (regList >> 8) & 0x0f;
1019 case 2: pDec->arg[1] = (regList >> 4) & 0x0f;
1020 case 1: pDec->vC = pDec->arg[0] = regList & 0x0f; break;
1021 case 0: break; // Valid, but no need to do anything.
1022 default:
1023 LOGW("Invalid arg count in 35c/35ms/35mi (%d)", count);
1024 goto bail;
1025 }
1026 }
1027 break;
1028 case kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
1029 case kFmt3rms: // [opt] invoke-virtual+super/range
1030 case kFmt3rmi: // [opt] execute-inline/range
1031 pDec->vA = INST_AA(inst);
1032 pDec->vB = FETCH(1);
1033 pDec->vC = FETCH(2);
1034 break;
1035 case kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB
1036 pDec->vA = INST_AA(inst);
1037 pDec->vB_wide = FETCH_u4(1) | ((u8) FETCH_u4(3) << 32);
1038 break;
1039 case kFmt33x: // exop vAA, vBB, vCCCC
1040 pDec->vA = FETCH(1) & 0xff;
1041 pDec->vB = FETCH(1) >> 8;
1042 pDec->vC = FETCH(2);
1043 break;
1044 case kFmt32s: // exop vAA, vBB, #+CCCC
1045 pDec->vA = FETCH(1) & 0xff;
1046 pDec->vB = FETCH(1) >> 8;
1047 pDec->vC = (s2) FETCH(2); // sign-extend 16-bit value
1048 break;
1049 case kFmt40sc: // [opt] exop AAAA, thing@BBBBBBBB
1050 case kFmt41c: // exop vAAAA, thing@BBBBBBBB
1051 /*
1052 * The order of fields for this format in the spec is {B, A},
1053 * to match formats 21c and 31c.
1054 */
1055 pDec->vB = FETCH_u4(1); // 32-bit value
1056 pDec->vA = FETCH(3);
1057 break;
1058 case kFmt52c: // exop vAAAA, vBBBB, thing@CCCCCCCC
1059 /*
1060 * The order of fields for this format in the spec is {C, A, B},
1061 * to match formats 22c and 22cs.
1062 */
1063 pDec->vC = FETCH_u4(1); // 32-bit value
1064 pDec->vA = FETCH(3);
1065 pDec->vB = FETCH(4);
1066 break;
1067 case kFmt5rc: // exop {vCCCC .. v(CCCC+AAAA-1)}, meth@BBBBBBBB
1068 /*
1069 * The order of fields for this format in the spec is {B, A, C},
1070 * to match formats 3rc and friends.
1071 */
1072 pDec->vB = FETCH_u4(1); // 32-bit value
1073 pDec->vA = FETCH(3);
1074 pDec->vC = FETCH(4);
1075 break;
1076 default:
1077 LOGW("Can't decode unexpected format %d (op=%d)", format, opcode);
1078 assert(false);
1079 break;
1080 }
1081
1082 bail:
1083 ;
1084 }
1085
1086 /*
1087 * Return the width of the specified instruction, or 0 if not defined. Also
1088 * works for special OP_NOP entries, including switch statement data tables
1089 * and array data.
1090 */
dexGetWidthFromInstruction(const u2 * insns)1091 size_t dexGetWidthFromInstruction(const u2* insns)
1092 {
1093 size_t width;
1094
1095 if (*insns == kPackedSwitchSignature) {
1096 width = 4 + insns[1] * 2;
1097 } else if (*insns == kSparseSwitchSignature) {
1098 width = 2 + insns[1] * 4;
1099 } else if (*insns == kArrayDataSignature) {
1100 u2 elemWidth = insns[1];
1101 u4 len = insns[2] | (((u4)insns[3]) << 16);
1102 // The plus 1 is to round up for odd size and width.
1103 width = 4 + (elemWidth * len + 1) / 2;
1104 } else {
1105 width = dexGetWidthFromOpcode(dexOpcodeFromCodeUnit(insns[0]));
1106 }
1107
1108 return width;
1109 }
1110