2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Dalvik instruction utility functions.
20 #include "InstrUtils.h"
26 * Generate a table that holds the width of all instructions.
28 * Standard instructions have positive values, optimizer instructions
29 * have negative values, unimplemented instructions have a width of zero.
31 * I'm doing it with a giant switch statement because it's easier to
32 * maintain and update than a static table with 256 unadorned integers,
33 * and if we're missing a case gcc emits a "warning: enumeration value not
36 * (To save space in the binary we could generate a static table with a
37 * command-line utility.)
39 * TODO: it doesn't look like we're using the negative values anymore.
40 * Consider switching to only positive values.
42 InstructionWidth* dexCreateInstrWidthTable(void)
44 #ifdef __ARM_ARCH_7A__
45 /* hack to work around mysterious problem on emulator */
46 LOGD("creating instr width table\n");
48 InstructionWidth* instrWidth;
51 instrWidth = malloc(sizeof(InstructionWidth) * kNumDalvikInstructions);
52 if (instrWidth == NULL)
55 for (i = 0; i < kNumDalvikInstructions; i++) {
56 OpCode opc = (OpCode) i;
60 case OP_NOP: /* note data for e.g. switch-* encoded "inside" a NOP */
65 case OP_MOVE_RESULT_WIDE:
66 case OP_MOVE_RESULT_OBJECT:
67 case OP_MOVE_EXCEPTION:
71 case OP_RETURN_OBJECT:
73 case OP_MONITOR_ENTER:
86 case OP_INT_TO_DOUBLE:
88 case OP_LONG_TO_FLOAT:
89 case OP_LONG_TO_DOUBLE:
91 case OP_FLOAT_TO_LONG:
92 case OP_FLOAT_TO_DOUBLE:
93 case OP_DOUBLE_TO_INT:
94 case OP_DOUBLE_TO_LONG:
95 case OP_DOUBLE_TO_FLOAT:
99 case OP_ADD_INT_2ADDR:
100 case OP_SUB_INT_2ADDR:
101 case OP_MUL_INT_2ADDR:
102 case OP_DIV_INT_2ADDR:
103 case OP_REM_INT_2ADDR:
104 case OP_AND_INT_2ADDR:
105 case OP_OR_INT_2ADDR:
106 case OP_XOR_INT_2ADDR:
107 case OP_SHL_INT_2ADDR:
108 case OP_SHR_INT_2ADDR:
109 case OP_USHR_INT_2ADDR:
110 case OP_ADD_LONG_2ADDR:
111 case OP_SUB_LONG_2ADDR:
112 case OP_MUL_LONG_2ADDR:
113 case OP_DIV_LONG_2ADDR:
114 case OP_REM_LONG_2ADDR:
115 case OP_AND_LONG_2ADDR:
116 case OP_OR_LONG_2ADDR:
117 case OP_XOR_LONG_2ADDR:
118 case OP_SHL_LONG_2ADDR:
119 case OP_SHR_LONG_2ADDR:
120 case OP_USHR_LONG_2ADDR:
121 case OP_ADD_FLOAT_2ADDR:
122 case OP_SUB_FLOAT_2ADDR:
123 case OP_MUL_FLOAT_2ADDR:
124 case OP_DIV_FLOAT_2ADDR:
125 case OP_REM_FLOAT_2ADDR:
126 case OP_ADD_DOUBLE_2ADDR:
127 case OP_SUB_DOUBLE_2ADDR:
128 case OP_MUL_DOUBLE_2ADDR:
129 case OP_DIV_DOUBLE_2ADDR:
130 case OP_REM_DOUBLE_2ADDR:
135 case OP_MOVE_WIDE_FROM16:
136 case OP_MOVE_OBJECT_FROM16:
138 case OP_CONST_HIGH16:
139 case OP_CONST_WIDE_16:
140 case OP_CONST_WIDE_HIGH16:
141 case OP_CONST_STRING:
145 case OP_NEW_INSTANCE:
168 case OP_AGET_BOOLEAN:
175 case OP_APUT_BOOLEAN:
182 case OP_IGET_BOOLEAN:
189 case OP_IPUT_BOOLEAN:
196 case OP_SGET_BOOLEAN:
203 case OP_SPUT_BOOLEAN:
239 case OP_ADD_INT_LIT16:
241 case OP_MUL_INT_LIT16:
242 case OP_DIV_INT_LIT16:
243 case OP_REM_INT_LIT16:
244 case OP_AND_INT_LIT16:
245 case OP_OR_INT_LIT16:
246 case OP_XOR_INT_LIT16:
247 case OP_ADD_INT_LIT8:
248 case OP_RSUB_INT_LIT8:
249 case OP_MUL_INT_LIT8:
250 case OP_DIV_INT_LIT8:
251 case OP_REM_INT_LIT8:
252 case OP_AND_INT_LIT8:
254 case OP_XOR_INT_LIT8:
255 case OP_SHL_INT_LIT8:
256 case OP_SHR_INT_LIT8:
257 case OP_USHR_INT_LIT8:
262 case OP_MOVE_WIDE_16:
263 case OP_MOVE_OBJECT_16:
265 case OP_CONST_WIDE_32:
266 case OP_CONST_STRING_JUMBO:
268 case OP_FILLED_NEW_ARRAY:
269 case OP_FILLED_NEW_ARRAY_RANGE:
270 case OP_FILL_ARRAY_DATA:
271 case OP_PACKED_SWITCH:
272 case OP_SPARSE_SWITCH:
273 case OP_INVOKE_VIRTUAL:
274 case OP_INVOKE_SUPER:
275 case OP_INVOKE_DIRECT:
276 case OP_INVOKE_STATIC:
277 case OP_INVOKE_INTERFACE:
278 case OP_INVOKE_VIRTUAL_RANGE:
279 case OP_INVOKE_SUPER_RANGE:
280 case OP_INVOKE_DIRECT_RANGE:
281 case OP_INVOKE_STATIC_RANGE:
282 case OP_INVOKE_INTERFACE_RANGE:
291 * Optimized instructions. We return negative size values for these
292 * to distinguish them.
294 case OP_RETURN_VOID_BARRIER:
298 case OP_IGET_WIDE_QUICK:
299 case OP_IGET_OBJECT_QUICK:
301 case OP_IPUT_WIDE_QUICK:
302 case OP_IPUT_OBJECT_QUICK:
303 case OP_IGET_VOLATILE:
304 case OP_IPUT_VOLATILE:
305 case OP_SGET_VOLATILE:
306 case OP_SPUT_VOLATILE:
307 case OP_IGET_OBJECT_VOLATILE:
308 case OP_IPUT_OBJECT_VOLATILE:
309 case OP_SGET_OBJECT_VOLATILE:
310 case OP_SPUT_OBJECT_VOLATILE:
311 case OP_IGET_WIDE_VOLATILE:
312 case OP_IPUT_WIDE_VOLATILE:
313 case OP_SGET_WIDE_VOLATILE:
314 case OP_SPUT_WIDE_VOLATILE:
315 case OP_THROW_VERIFICATION_ERROR:
318 case OP_INVOKE_VIRTUAL_QUICK:
319 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
320 case OP_INVOKE_SUPER_QUICK:
321 case OP_INVOKE_SUPER_QUICK_RANGE:
322 case OP_EXECUTE_INLINE:
323 case OP_EXECUTE_INLINE_RANGE:
324 case OP_INVOKE_DIRECT_EMPTY:
328 /* these should never appear when scanning bytecode */
344 * DO NOT add a "default" clause here. Without it the compiler will
345 * complain if an instruction is missing (which is desirable).
349 instrWidth[opc] = width;
356 * Generate a table that holds instruction flags.
358 InstructionFlags* dexCreateInstrFlagsTable(void)
360 InstructionFlags* instrFlags;
363 instrFlags = malloc(sizeof(InstructionFlags) * kNumDalvikInstructions);
364 if (instrFlags == NULL)
367 for (i = 0; i < kNumDalvikInstructions; i++) {
368 OpCode opc = (OpCode) i;
369 InstructionFlags flags = 0;
372 /* these don't affect the PC and can't cause an exception */
378 case OP_MOVE_WIDE_FROM16:
379 case OP_MOVE_WIDE_16:
381 case OP_MOVE_OBJECT_FROM16:
382 case OP_MOVE_OBJECT_16:
384 case OP_MOVE_RESULT_WIDE:
385 case OP_MOVE_RESULT_OBJECT:
386 case OP_MOVE_EXCEPTION:
390 case OP_CONST_HIGH16:
391 case OP_CONST_WIDE_16:
392 case OP_CONST_WIDE_32:
394 case OP_CONST_WIDE_HIGH16:
395 case OP_FILL_ARRAY_DATA:
408 case OP_INT_TO_FLOAT:
409 case OP_INT_TO_DOUBLE:
411 case OP_LONG_TO_FLOAT:
412 case OP_LONG_TO_DOUBLE:
413 case OP_FLOAT_TO_INT:
414 case OP_FLOAT_TO_LONG:
415 case OP_FLOAT_TO_DOUBLE:
416 case OP_DOUBLE_TO_INT:
417 case OP_DOUBLE_TO_LONG:
418 case OP_DOUBLE_TO_FLOAT:
421 case OP_INT_TO_SHORT:
448 case OP_DIV_DOUBLE: // div by zero just returns NaN
450 case OP_ADD_INT_2ADDR:
451 case OP_SUB_INT_2ADDR:
452 case OP_MUL_INT_2ADDR:
453 case OP_AND_INT_2ADDR:
454 case OP_OR_INT_2ADDR:
455 case OP_XOR_INT_2ADDR:
456 case OP_SHL_INT_2ADDR:
457 case OP_SHR_INT_2ADDR:
458 case OP_USHR_INT_2ADDR:
459 case OP_ADD_LONG_2ADDR:
460 case OP_SUB_LONG_2ADDR:
461 case OP_MUL_LONG_2ADDR:
462 case OP_AND_LONG_2ADDR:
463 case OP_OR_LONG_2ADDR:
464 case OP_XOR_LONG_2ADDR:
465 case OP_SHL_LONG_2ADDR:
466 case OP_SHR_LONG_2ADDR:
467 case OP_USHR_LONG_2ADDR:
468 case OP_ADD_FLOAT_2ADDR:
469 case OP_SUB_FLOAT_2ADDR:
470 case OP_MUL_FLOAT_2ADDR:
471 case OP_DIV_FLOAT_2ADDR:
472 case OP_REM_FLOAT_2ADDR:
473 case OP_ADD_DOUBLE_2ADDR:
474 case OP_SUB_DOUBLE_2ADDR:
475 case OP_MUL_DOUBLE_2ADDR:
476 case OP_DIV_DOUBLE_2ADDR:
477 case OP_REM_DOUBLE_2ADDR:
478 case OP_ADD_INT_LIT16:
480 case OP_MUL_INT_LIT16:
481 case OP_AND_INT_LIT16:
482 case OP_OR_INT_LIT16:
483 case OP_XOR_INT_LIT16:
484 case OP_ADD_INT_LIT8:
485 case OP_RSUB_INT_LIT8:
486 case OP_MUL_INT_LIT8:
487 case OP_AND_INT_LIT8:
489 case OP_XOR_INT_LIT8:
490 case OP_SHL_INT_LIT8:
491 case OP_SHR_INT_LIT8:
492 case OP_USHR_INT_LIT8:
493 flags = kInstrCanContinue;
496 /* these don't affect the PC, but can cause exceptions */
497 case OP_CONST_STRING:
498 case OP_CONST_STRING_JUMBO:
500 case OP_MONITOR_ENTER:
501 case OP_MONITOR_EXIT:
504 case OP_ARRAY_LENGTH:
505 case OP_NEW_INSTANCE:
507 case OP_FILLED_NEW_ARRAY:
508 case OP_FILLED_NEW_ARRAY_RANGE:
510 case OP_AGET_BOOLEAN:
517 case OP_APUT_BOOLEAN:
524 case OP_IGET_BOOLEAN:
531 case OP_IPUT_BOOLEAN:
538 case OP_SGET_BOOLEAN:
545 case OP_SPUT_BOOLEAN:
555 case OP_DIV_INT_2ADDR:
556 case OP_REM_INT_2ADDR:
557 case OP_DIV_LONG_2ADDR:
558 case OP_REM_LONG_2ADDR:
559 case OP_DIV_INT_LIT16:
560 case OP_REM_INT_LIT16:
561 case OP_DIV_INT_LIT8:
562 case OP_REM_INT_LIT8:
563 flags = kInstrCanContinue | kInstrCanThrow;
566 case OP_INVOKE_VIRTUAL:
567 case OP_INVOKE_VIRTUAL_RANGE:
568 case OP_INVOKE_SUPER:
569 case OP_INVOKE_SUPER_RANGE:
570 case OP_INVOKE_DIRECT:
571 case OP_INVOKE_DIRECT_RANGE:
572 case OP_INVOKE_STATIC:
573 case OP_INVOKE_STATIC_RANGE:
574 case OP_INVOKE_INTERFACE:
575 case OP_INVOKE_INTERFACE_RANGE:
576 flags = kInstrCanContinue | kInstrCanThrow | kInstrInvoke;
582 case OP_RETURN_OBJECT:
583 flags = kInstrCanReturn;
587 flags = kInstrCanThrow;
590 /* unconditional branches */
594 flags = kInstrCanBranch | kInstrUnconditional;
597 /* conditional branches */
610 flags = kInstrCanBranch | kInstrCanContinue;
613 /* switch statements; if value not in switch, it continues */
614 case OP_PACKED_SWITCH:
615 case OP_SPARSE_SWITCH:
616 flags = kInstrCanSwitch | kInstrCanContinue;
619 /* verifier/optimizer-generated instructions */
620 case OP_THROW_VERIFICATION_ERROR:
621 flags = kInstrCanThrow;
623 case OP_EXECUTE_INLINE:
624 case OP_EXECUTE_INLINE_RANGE:
625 flags = kInstrCanContinue | kInstrCanThrow;
628 case OP_IGET_WIDE_QUICK:
629 case OP_IGET_OBJECT_QUICK:
631 case OP_IPUT_WIDE_QUICK:
632 case OP_IPUT_OBJECT_QUICK:
633 case OP_IGET_VOLATILE:
634 case OP_IPUT_VOLATILE:
635 case OP_SGET_VOLATILE:
636 case OP_SPUT_VOLATILE:
637 case OP_IGET_OBJECT_VOLATILE:
638 case OP_IPUT_OBJECT_VOLATILE:
639 case OP_SGET_OBJECT_VOLATILE:
640 case OP_SPUT_OBJECT_VOLATILE:
641 case OP_IGET_WIDE_VOLATILE:
642 case OP_IPUT_WIDE_VOLATILE:
643 case OP_SGET_WIDE_VOLATILE:
644 case OP_SPUT_WIDE_VOLATILE:
645 flags = kInstrCanContinue | kInstrCanThrow;
647 case OP_INVOKE_VIRTUAL_QUICK:
648 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
649 case OP_INVOKE_SUPER_QUICK:
650 case OP_INVOKE_SUPER_QUICK_RANGE:
651 case OP_INVOKE_DIRECT_EMPTY:
652 flags = kInstrCanContinue | kInstrCanThrow | kInstrInvoke;
654 case OP_RETURN_VOID_BARRIER:
655 flags = kInstrCanReturn;
658 /* these should never appear when scanning code */
673 * DO NOT add a "default" clause here. Without it the compiler will
674 * complain if an instruction is missing (which is desirable).
678 instrFlags[opc] = flags;
685 * Allocate and populate a 256-element array with instruction formats.
686 * Used in conjunction with dexDecodeInstruction.
688 InstructionFormat* dexCreateInstrFormatTable(void)
690 InstructionFormat* instFmt;
693 instFmt = malloc(sizeof(InstructionFormat) * kNumDalvikInstructions);
697 for (i = 0; i < kNumDalvikInstructions; i++) {
698 OpCode opc = (OpCode) i;
699 InstructionFormat fmt = kFmtUnknown;
712 case OP_CONST_HIGH16:
713 case OP_CONST_WIDE_HIGH16:
717 case OP_MOVE_RESULT_WIDE:
718 case OP_MOVE_RESULT_OBJECT:
719 case OP_MOVE_EXCEPTION:
722 case OP_RETURN_OBJECT:
723 case OP_MONITOR_ENTER:
724 case OP_MONITOR_EXIT:
731 case OP_ARRAY_LENGTH:
739 case OP_INT_TO_FLOAT:
740 case OP_INT_TO_DOUBLE:
742 case OP_LONG_TO_FLOAT:
743 case OP_LONG_TO_DOUBLE:
744 case OP_FLOAT_TO_INT:
745 case OP_FLOAT_TO_LONG:
746 case OP_FLOAT_TO_DOUBLE:
747 case OP_DOUBLE_TO_INT:
748 case OP_DOUBLE_TO_LONG:
749 case OP_DOUBLE_TO_FLOAT:
752 case OP_INT_TO_SHORT:
753 case OP_ADD_INT_2ADDR:
754 case OP_SUB_INT_2ADDR:
755 case OP_MUL_INT_2ADDR:
756 case OP_DIV_INT_2ADDR:
757 case OP_REM_INT_2ADDR:
758 case OP_AND_INT_2ADDR:
759 case OP_OR_INT_2ADDR:
760 case OP_XOR_INT_2ADDR:
761 case OP_SHL_INT_2ADDR:
762 case OP_SHR_INT_2ADDR:
763 case OP_USHR_INT_2ADDR:
764 case OP_ADD_LONG_2ADDR:
765 case OP_SUB_LONG_2ADDR:
766 case OP_MUL_LONG_2ADDR:
767 case OP_DIV_LONG_2ADDR:
768 case OP_REM_LONG_2ADDR:
769 case OP_AND_LONG_2ADDR:
770 case OP_OR_LONG_2ADDR:
771 case OP_XOR_LONG_2ADDR:
772 case OP_SHL_LONG_2ADDR:
773 case OP_SHR_LONG_2ADDR:
774 case OP_USHR_LONG_2ADDR:
775 case OP_ADD_FLOAT_2ADDR:
776 case OP_SUB_FLOAT_2ADDR:
777 case OP_MUL_FLOAT_2ADDR:
778 case OP_DIV_FLOAT_2ADDR:
779 case OP_REM_FLOAT_2ADDR:
780 case OP_ADD_DOUBLE_2ADDR:
781 case OP_SUB_DOUBLE_2ADDR:
782 case OP_MUL_DOUBLE_2ADDR:
783 case OP_DIV_DOUBLE_2ADDR:
784 case OP_REM_DOUBLE_2ADDR:
793 case OP_CONST_STRING:
796 case OP_NEW_INSTANCE:
800 case OP_SGET_BOOLEAN:
807 case OP_SPUT_BOOLEAN:
814 case OP_CONST_WIDE_16:
825 case OP_FILL_ARRAY_DATA:
826 case OP_PACKED_SWITCH:
827 case OP_SPARSE_SWITCH:
830 case OP_ADD_INT_LIT8:
831 case OP_RSUB_INT_LIT8:
832 case OP_MUL_INT_LIT8:
833 case OP_DIV_INT_LIT8:
834 case OP_REM_INT_LIT8:
835 case OP_AND_INT_LIT8:
837 case OP_XOR_INT_LIT8:
838 case OP_SHL_INT_LIT8:
839 case OP_SHR_INT_LIT8:
840 case OP_USHR_INT_LIT8:
848 case OP_IGET_BOOLEAN:
855 case OP_IPUT_BOOLEAN:
861 case OP_ADD_INT_LIT16:
863 case OP_MUL_INT_LIT16:
864 case OP_DIV_INT_LIT16:
865 case OP_REM_INT_LIT16:
866 case OP_AND_INT_LIT16:
867 case OP_OR_INT_LIT16:
868 case OP_XOR_INT_LIT16:
880 case OP_MOVE_WIDE_FROM16:
881 case OP_MOVE_OBJECT_FROM16:
892 case OP_AGET_BOOLEAN:
899 case OP_APUT_BOOLEAN:
938 case OP_CONST_WIDE_32:
941 case OP_CONST_STRING_JUMBO:
945 case OP_MOVE_WIDE_16:
946 case OP_MOVE_OBJECT_16:
949 case OP_FILLED_NEW_ARRAY:
950 case OP_INVOKE_VIRTUAL:
951 case OP_INVOKE_SUPER:
952 case OP_INVOKE_DIRECT:
953 case OP_INVOKE_STATIC:
954 case OP_INVOKE_INTERFACE:
957 case OP_FILLED_NEW_ARRAY_RANGE:
958 case OP_INVOKE_VIRTUAL_RANGE:
959 case OP_INVOKE_SUPER_RANGE:
960 case OP_INVOKE_DIRECT_RANGE:
961 case OP_INVOKE_STATIC_RANGE:
962 case OP_INVOKE_INTERFACE_RANGE:
970 * Optimized instructions.
972 case OP_THROW_VERIFICATION_ERROR:
975 case OP_IGET_WIDE_VOLATILE:
976 case OP_IPUT_WIDE_VOLATILE:
977 case OP_IGET_VOLATILE:
978 case OP_IPUT_VOLATILE:
979 case OP_IGET_OBJECT_VOLATILE:
980 case OP_IPUT_OBJECT_VOLATILE:
983 case OP_SGET_OBJECT_VOLATILE:
984 case OP_SPUT_OBJECT_VOLATILE:
985 case OP_SGET_VOLATILE:
986 case OP_SPUT_VOLATILE:
987 case OP_SGET_WIDE_VOLATILE:
988 case OP_SPUT_WIDE_VOLATILE:
992 case OP_IGET_WIDE_QUICK:
993 case OP_IGET_OBJECT_QUICK:
995 case OP_IPUT_WIDE_QUICK:
996 case OP_IPUT_OBJECT_QUICK:
999 case OP_INVOKE_VIRTUAL_QUICK:
1000 case OP_INVOKE_SUPER_QUICK:
1003 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
1004 case OP_INVOKE_SUPER_QUICK_RANGE:
1007 case OP_EXECUTE_INLINE:
1010 case OP_EXECUTE_INLINE_RANGE:
1013 case OP_INVOKE_DIRECT_EMPTY:
1016 case OP_RETURN_VOID_BARRIER:
1020 /* these should never appear when scanning code */
1036 * DO NOT add a "default" clause here. Without it the compiler will
1037 * complain if an instruction is missing (which is desirable).
1048 * Copied from InterpCore.h. Used for instruction decoding.
1050 #define FETCH(_offset) (insns[(_offset)])
1051 #define INST_INST(_inst) ((_inst) & 0xff)
1052 #define INST_A(_inst) (((u2)(_inst) >> 8) & 0x0f)
1053 #define INST_B(_inst) ((u2)(_inst) >> 12)
1054 #define INST_AA(_inst) ((_inst) >> 8)
1057 * Decode the instruction pointed to by "insns".
1059 * Fills out the pieces of "pDec" that are affected by the current
1060 * instruction. Does not touch anything else.
1062 void dexDecodeInstruction(const InstructionFormat* fmts, const u2* insns,
1063 DecodedInstruction* pDec)
1067 pDec->opCode = (OpCode) INST_INST(inst);
1069 switch (dexGetInstrFormat(fmts, pDec->opCode)) {
1071 /* nothing to do; copy the AA bits out for the verifier */
1072 pDec->vA = INST_AA(inst);
1074 case kFmt12x: // op vA, vB
1075 pDec->vA = INST_A(inst);
1076 pDec->vB = INST_B(inst);
1078 case kFmt11n: // op vA, #+B
1079 pDec->vA = INST_A(inst);
1080 pDec->vB = (s4) (INST_B(inst) << 28) >> 28; // sign extend 4-bit value
1082 case kFmt11x: // op vAA
1083 pDec->vA = INST_AA(inst);
1085 case kFmt10t: // op +AA
1086 pDec->vA = (s1) INST_AA(inst); // sign-extend 8-bit value
1088 case kFmt20t: // op +AAAA
1089 pDec->vA = (s2) FETCH(1); // sign-extend 16-bit value
1091 case kFmt20bc: // [opt] op AA, thing@BBBB
1092 case kFmt21c: // op vAA, thing@BBBB
1093 case kFmt22x: // op vAA, vBBBB
1094 pDec->vA = INST_AA(inst);
1095 pDec->vB = FETCH(1);
1097 case kFmt21s: // op vAA, #+BBBB
1098 case kFmt21t: // op vAA, +BBBB
1099 pDec->vA = INST_AA(inst);
1100 pDec->vB = (s2) FETCH(1); // sign-extend 16-bit value
1102 case kFmt21h: // op vAA, #+BBBB0000[00000000]
1103 pDec->vA = INST_AA(inst);
1105 * The value should be treated as right-zero-extended, but we don't
1106 * actually do that here. Among other things, we don't know if it's
1107 * the top bits of a 32- or 64-bit value.
1109 pDec->vB = FETCH(1);
1111 case kFmt23x: // op vAA, vBB, vCC
1112 pDec->vA = INST_AA(inst);
1113 pDec->vB = FETCH(1) & 0xff;
1114 pDec->vC = FETCH(1) >> 8;
1116 case kFmt22b: // op vAA, vBB, #+CC
1117 pDec->vA = INST_AA(inst);
1118 pDec->vB = FETCH(1) & 0xff;
1119 pDec->vC = (s1) (FETCH(1) >> 8); // sign-extend 8-bit value
1121 case kFmt22s: // op vA, vB, #+CCCC
1122 case kFmt22t: // op vA, vB, +CCCC
1123 pDec->vA = INST_A(inst);
1124 pDec->vB = INST_B(inst);
1125 pDec->vC = (s2) FETCH(1); // sign-extend 16-bit value
1127 case kFmt22c: // op vA, vB, thing@CCCC
1128 case kFmt22cs: // [opt] op vA, vB, field offset CCCC
1129 pDec->vA = INST_A(inst);
1130 pDec->vB = INST_B(inst);
1131 pDec->vC = FETCH(1);
1133 case kFmt30t: // op +AAAAAAAA
1134 pDec->vA = FETCH(1) | ((u4) FETCH(2) << 16); // signed 32-bit value
1136 case kFmt31t: // op vAA, +BBBBBBBB
1137 case kFmt31c: // op vAA, string@BBBBBBBB
1138 pDec->vA = INST_AA(inst);
1139 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16); // 32-bit value
1141 case kFmt32x: // op vAAAA, vBBBB
1142 pDec->vA = FETCH(1);
1143 pDec->vB = FETCH(2);
1145 case kFmt31i: // op vAA, #+BBBBBBBB
1146 pDec->vA = INST_AA(inst);
1147 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16);
1149 case kFmt35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
1150 case kFmt35ms: // [opt] invoke-virtual+super
1153 * Note that the fields mentioned in the spec don't appear in
1154 * their "usual" positions here compared to most formats. This
1155 * was done so that the field names for the argument count and
1156 * reference index match between this format and the corresponding
1157 * range formats (3rc and friends).
1159 * Bottom line: The argument count is always in vA, and the
1160 * method constant is always in vB.
1165 pDec->vA = INST_B(inst); // This is labeled A in the spec.
1166 pDec->vB = FETCH(1);
1170 LOGW("Invalid arg count in 35c/35ms (%d)\n", pDec->vA);
1176 * Per note above, the 5th arg comes from the A field in the
1177 * instruction, but it's labeled G in the spec.
1179 pDec->arg[4] = INST_A(inst);
1182 for (i = 0; i < count; i++) {
1183 pDec->arg[i] = regList & 0x0f;
1186 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */
1188 pDec->vC = pDec->arg[0];
1191 case kFmt35mi: // [opt] inline invoke
1193 /* See note under case 35c, above. */
1197 pDec->vA = INST_B(inst); // This is labeled A in the spec.
1198 pDec->vB = FETCH(1);
1202 LOGW("Invalid arg count in 3inline (%d)\n", pDec->vA);
1205 for (i = 0; i < (int) pDec->vA; i++) {
1206 pDec->arg[i] = regList & 0x0f;
1209 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */
1211 pDec->vC = pDec->arg[0];
1214 case kFmt35fs: // [opt] invoke-interface
1215 assert(false); // TODO
1217 case kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
1218 case kFmt3rms: // [opt] invoke-virtual+super/range
1219 case kFmt3rmi: // [opt] execute-inline/range
1220 pDec->vA = INST_AA(inst);
1221 pDec->vB = FETCH(1);
1222 pDec->vC = FETCH(2);
1224 case kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB
1225 pDec->vA = INST_AA(inst);
1226 pDec->vB_wide = FETCH(1);
1227 pDec->vB_wide |= (u8)FETCH(2) << 16;
1228 pDec->vB_wide |= (u8)FETCH(3) << 32;
1229 pDec->vB_wide |= (u8)FETCH(4) << 48;
1232 LOGW("Can't decode unexpected format %d (op=%d)\n",
1233 dexGetInstrFormat(fmts, pDec->opCode), pDec->opCode);
1243 * Return the width of the specified instruction, or 0 if not defined. Also
1244 * works for special OP_NOP entries, including switch statement data tables
1247 size_t dexGetInstrOrTableWidthAbs(const InstructionWidth* widths,
1252 if (*insns == kPackedSwitchSignature) {
1253 width = 4 + insns[1] * 2;
1254 } else if (*insns == kSparseSwitchSignature) {
1255 width = 2 + insns[1] * 4;
1256 } else if (*insns == kArrayDataSignature) {
1257 u2 elemWidth = insns[1];
1258 u4 len = insns[2] | (((u4)insns[3]) << 16);
1259 width = 4 + (elemWidth * len + 1) / 2;
1261 width = dexGetInstrWidthAbs(widths, INST_INST(insns[0]));