blob: 42aa3ec62f60523bb5be8be3a20aa344dd54c333 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 */
Andy McFadden3a1aedb2009-05-07 13:30:23 -070016
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080017/*
18 * Dalvik instruction utility functions.
19 */
20#include "InstrUtils.h"
21
22#include <stdlib.h>
23
24
25/*
26 * Generate a table that holds the width of all instructions.
27 *
28 * Standard instructions have positive values, optimizer instructions
29 * have negative values, unimplemented instructions have a width of zero.
30 *
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
34 * handled" message.
35 *
36 * (To save space in the binary we could generate a static table with a
37 * command-line utility.)
38 */
39InstructionWidth* dexCreateInstrWidthTable(void)
40{
41 InstructionWidth* instrWidth;
42 int i;
43
44 instrWidth = malloc(sizeof(InstructionWidth) * kNumDalvikInstructions);
45 if (instrWidth == NULL)
46 return NULL;
47
48 for (i = 0; i < kNumDalvikInstructions; i++) {
49 OpCode opc = (OpCode) i;
50 int width = 0;
51
52 switch (opc) {
Andy McFadden3a1aedb2009-05-07 13:30:23 -070053 case OP_NOP: /* note data for e.g. switch-* encoded "inside" a NOP */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080054 case OP_MOVE:
55 case OP_MOVE_WIDE:
56 case OP_MOVE_OBJECT:
57 case OP_MOVE_RESULT:
58 case OP_MOVE_RESULT_WIDE:
59 case OP_MOVE_RESULT_OBJECT:
60 case OP_MOVE_EXCEPTION:
61 case OP_RETURN_VOID:
62 case OP_RETURN:
63 case OP_RETURN_WIDE:
64 case OP_RETURN_OBJECT:
65 case OP_CONST_4:
66 case OP_MONITOR_ENTER:
67 case OP_MONITOR_EXIT:
68 case OP_ARRAY_LENGTH:
69 case OP_THROW:
70 case OP_GOTO:
71 case OP_NEG_INT:
72 case OP_NOT_INT:
73 case OP_NEG_LONG:
74 case OP_NOT_LONG:
75 case OP_NEG_FLOAT:
76 case OP_NEG_DOUBLE:
77 case OP_INT_TO_LONG:
78 case OP_INT_TO_FLOAT:
79 case OP_INT_TO_DOUBLE:
80 case OP_LONG_TO_INT:
81 case OP_LONG_TO_FLOAT:
82 case OP_LONG_TO_DOUBLE:
83 case OP_FLOAT_TO_INT:
84 case OP_FLOAT_TO_LONG:
85 case OP_FLOAT_TO_DOUBLE:
86 case OP_DOUBLE_TO_INT:
87 case OP_DOUBLE_TO_LONG:
88 case OP_DOUBLE_TO_FLOAT:
89 case OP_INT_TO_BYTE:
90 case OP_INT_TO_CHAR:
91 case OP_INT_TO_SHORT:
92 case OP_ADD_INT_2ADDR:
93 case OP_SUB_INT_2ADDR:
94 case OP_MUL_INT_2ADDR:
95 case OP_DIV_INT_2ADDR:
96 case OP_REM_INT_2ADDR:
97 case OP_AND_INT_2ADDR:
98 case OP_OR_INT_2ADDR:
99 case OP_XOR_INT_2ADDR:
100 case OP_SHL_INT_2ADDR:
101 case OP_SHR_INT_2ADDR:
102 case OP_USHR_INT_2ADDR:
103 case OP_ADD_LONG_2ADDR:
104 case OP_SUB_LONG_2ADDR:
105 case OP_MUL_LONG_2ADDR:
106 case OP_DIV_LONG_2ADDR:
107 case OP_REM_LONG_2ADDR:
108 case OP_AND_LONG_2ADDR:
109 case OP_OR_LONG_2ADDR:
110 case OP_XOR_LONG_2ADDR:
111 case OP_SHL_LONG_2ADDR:
112 case OP_SHR_LONG_2ADDR:
113 case OP_USHR_LONG_2ADDR:
114 case OP_ADD_FLOAT_2ADDR:
115 case OP_SUB_FLOAT_2ADDR:
116 case OP_MUL_FLOAT_2ADDR:
117 case OP_DIV_FLOAT_2ADDR:
118 case OP_REM_FLOAT_2ADDR:
119 case OP_ADD_DOUBLE_2ADDR:
120 case OP_SUB_DOUBLE_2ADDR:
121 case OP_MUL_DOUBLE_2ADDR:
122 case OP_DIV_DOUBLE_2ADDR:
123 case OP_REM_DOUBLE_2ADDR:
124 width = 1;
125 break;
126
127 case OP_MOVE_FROM16:
128 case OP_MOVE_WIDE_FROM16:
129 case OP_MOVE_OBJECT_FROM16:
130 case OP_CONST_16:
131 case OP_CONST_HIGH16:
132 case OP_CONST_WIDE_16:
133 case OP_CONST_WIDE_HIGH16:
134 case OP_CONST_STRING:
135 case OP_CONST_CLASS:
136 case OP_CHECK_CAST:
137 case OP_INSTANCE_OF:
138 case OP_NEW_INSTANCE:
139 case OP_NEW_ARRAY:
140 case OP_CMPL_FLOAT:
141 case OP_CMPG_FLOAT:
142 case OP_CMPL_DOUBLE:
143 case OP_CMPG_DOUBLE:
144 case OP_CMP_LONG:
145 case OP_GOTO_16:
146 case OP_IF_EQ:
147 case OP_IF_NE:
148 case OP_IF_LT:
149 case OP_IF_GE:
150 case OP_IF_GT:
151 case OP_IF_LE:
152 case OP_IF_EQZ:
153 case OP_IF_NEZ:
154 case OP_IF_LTZ:
155 case OP_IF_GEZ:
156 case OP_IF_GTZ:
157 case OP_IF_LEZ:
158 case OP_AGET:
159 case OP_AGET_WIDE:
160 case OP_AGET_OBJECT:
161 case OP_AGET_BOOLEAN:
162 case OP_AGET_BYTE:
163 case OP_AGET_CHAR:
164 case OP_AGET_SHORT:
165 case OP_APUT:
166 case OP_APUT_WIDE:
167 case OP_APUT_OBJECT:
168 case OP_APUT_BOOLEAN:
169 case OP_APUT_BYTE:
170 case OP_APUT_CHAR:
171 case OP_APUT_SHORT:
172 case OP_IGET:
173 case OP_IGET_WIDE:
174 case OP_IGET_OBJECT:
175 case OP_IGET_BOOLEAN:
176 case OP_IGET_BYTE:
177 case OP_IGET_CHAR:
178 case OP_IGET_SHORT:
179 case OP_IPUT:
180 case OP_IPUT_WIDE:
181 case OP_IPUT_OBJECT:
182 case OP_IPUT_BOOLEAN:
183 case OP_IPUT_BYTE:
184 case OP_IPUT_CHAR:
185 case OP_IPUT_SHORT:
186 case OP_SGET:
187 case OP_SGET_WIDE:
188 case OP_SGET_OBJECT:
189 case OP_SGET_BOOLEAN:
190 case OP_SGET_BYTE:
191 case OP_SGET_CHAR:
192 case OP_SGET_SHORT:
193 case OP_SPUT:
194 case OP_SPUT_WIDE:
195 case OP_SPUT_OBJECT:
196 case OP_SPUT_BOOLEAN:
197 case OP_SPUT_BYTE:
198 case OP_SPUT_CHAR:
199 case OP_SPUT_SHORT:
200 case OP_ADD_INT:
201 case OP_SUB_INT:
202 case OP_MUL_INT:
203 case OP_DIV_INT:
204 case OP_REM_INT:
205 case OP_AND_INT:
206 case OP_OR_INT:
207 case OP_XOR_INT:
208 case OP_SHL_INT:
209 case OP_SHR_INT:
210 case OP_USHR_INT:
211 case OP_ADD_LONG:
212 case OP_SUB_LONG:
213 case OP_MUL_LONG:
214 case OP_DIV_LONG:
215 case OP_REM_LONG:
216 case OP_AND_LONG:
217 case OP_OR_LONG:
218 case OP_XOR_LONG:
219 case OP_SHL_LONG:
220 case OP_SHR_LONG:
221 case OP_USHR_LONG:
222 case OP_ADD_FLOAT:
223 case OP_SUB_FLOAT:
224 case OP_MUL_FLOAT:
225 case OP_DIV_FLOAT:
226 case OP_REM_FLOAT:
227 case OP_ADD_DOUBLE:
228 case OP_SUB_DOUBLE:
229 case OP_MUL_DOUBLE:
230 case OP_DIV_DOUBLE:
231 case OP_REM_DOUBLE:
232 case OP_ADD_INT_LIT16:
233 case OP_RSUB_INT:
234 case OP_MUL_INT_LIT16:
235 case OP_DIV_INT_LIT16:
236 case OP_REM_INT_LIT16:
237 case OP_AND_INT_LIT16:
238 case OP_OR_INT_LIT16:
239 case OP_XOR_INT_LIT16:
240 case OP_ADD_INT_LIT8:
241 case OP_RSUB_INT_LIT8:
242 case OP_MUL_INT_LIT8:
243 case OP_DIV_INT_LIT8:
244 case OP_REM_INT_LIT8:
245 case OP_AND_INT_LIT8:
246 case OP_OR_INT_LIT8:
247 case OP_XOR_INT_LIT8:
248 case OP_SHL_INT_LIT8:
249 case OP_SHR_INT_LIT8:
250 case OP_USHR_INT_LIT8:
251 width = 2;
252 break;
253
254 case OP_MOVE_16:
255 case OP_MOVE_WIDE_16:
256 case OP_MOVE_OBJECT_16:
257 case OP_CONST:
258 case OP_CONST_WIDE_32:
259 case OP_CONST_STRING_JUMBO:
260 case OP_GOTO_32:
261 case OP_FILLED_NEW_ARRAY:
262 case OP_FILLED_NEW_ARRAY_RANGE:
263 case OP_FILL_ARRAY_DATA:
264 case OP_PACKED_SWITCH:
265 case OP_SPARSE_SWITCH:
266 case OP_INVOKE_VIRTUAL:
267 case OP_INVOKE_SUPER:
268 case OP_INVOKE_DIRECT:
269 case OP_INVOKE_STATIC:
270 case OP_INVOKE_INTERFACE:
271 case OP_INVOKE_VIRTUAL_RANGE:
272 case OP_INVOKE_SUPER_RANGE:
273 case OP_INVOKE_DIRECT_RANGE:
274 case OP_INVOKE_STATIC_RANGE:
275 case OP_INVOKE_INTERFACE_RANGE:
276 width = 3;
277 break;
278
279 case OP_CONST_WIDE:
280 width = 5;
281 break;
282
283 /*
284 * Optimized instructions. We return negative size values for these
285 * to distinguish them.
286 */
287 case OP_IGET_QUICK:
288 case OP_IGET_WIDE_QUICK:
289 case OP_IGET_OBJECT_QUICK:
290 case OP_IPUT_QUICK:
291 case OP_IPUT_WIDE_QUICK:
292 case OP_IPUT_OBJECT_QUICK:
Andy McFadden3a1aedb2009-05-07 13:30:23 -0700293 case OP_THROW_VERIFICATION_ERROR:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800294 width = -2;
295 break;
296 case OP_INVOKE_VIRTUAL_QUICK:
297 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
298 case OP_INVOKE_SUPER_QUICK:
299 case OP_INVOKE_SUPER_QUICK_RANGE:
300 case OP_EXECUTE_INLINE:
301 case OP_INVOKE_DIRECT_EMPTY:
302 width = -3;
303 break;
304
305 /* these should never appear */
306 case OP_UNUSED_3E:
307 case OP_UNUSED_3F:
308 case OP_UNUSED_40:
309 case OP_UNUSED_41:
310 case OP_UNUSED_42:
311 case OP_UNUSED_43:
312 case OP_UNUSED_73:
313 case OP_UNUSED_79:
314 case OP_UNUSED_7A:
315 case OP_UNUSED_E3:
316 case OP_UNUSED_E4:
317 case OP_UNUSED_E5:
318 case OP_UNUSED_E6:
319 case OP_UNUSED_E7:
320 case OP_UNUSED_E8:
321 case OP_UNUSED_E9:
322 case OP_UNUSED_EA:
323 case OP_UNUSED_EB:
324 case OP_UNUSED_EC:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800325 case OP_UNUSED_EF:
326 case OP_UNUSED_F1:
327 case OP_UNUSED_FC:
328 case OP_UNUSED_FD:
329 case OP_UNUSED_FE:
330 case OP_UNUSED_FF:
331 assert(width == 0);
332 break;
333
334 /*
335 * DO NOT add a "default" clause here. Without it the compiler will
336 * complain if an instruction is missing (which is desirable).
337 */
338 }
339
340 instrWidth[opc] = width;
341 }
342
343 return instrWidth;
344}
345
346/*
347 * Generate a table that holds instruction flags.
348 */
349InstructionFlags* dexCreateInstrFlagsTable(void)
350{
351 InstructionFlags* instrFlags;
352 int i;
353
354 instrFlags = malloc(sizeof(InstructionFlags) * kNumDalvikInstructions);
355 if (instrFlags == NULL)
356 return NULL;
357
358 for (i = 0; i < kNumDalvikInstructions; i++) {
359 OpCode opc = (OpCode) i;
360 InstructionFlags flags = 0;
361
362 switch (opc) {
363 /* these don't affect the PC and can't cause an exception */
364 case OP_NOP:
365 case OP_MOVE:
366 case OP_MOVE_FROM16:
367 case OP_MOVE_16:
368 case OP_MOVE_WIDE:
369 case OP_MOVE_WIDE_FROM16:
370 case OP_MOVE_WIDE_16:
371 case OP_MOVE_OBJECT:
372 case OP_MOVE_OBJECT_FROM16:
373 case OP_MOVE_OBJECT_16:
374 case OP_MOVE_RESULT:
375 case OP_MOVE_RESULT_WIDE:
376 case OP_MOVE_RESULT_OBJECT:
377 case OP_MOVE_EXCEPTION:
378 case OP_CONST_4:
379 case OP_CONST_16:
380 case OP_CONST:
381 case OP_CONST_HIGH16:
382 case OP_CONST_WIDE_16:
383 case OP_CONST_WIDE_32:
384 case OP_CONST_WIDE:
385 case OP_CONST_WIDE_HIGH16:
386 case OP_FILL_ARRAY_DATA:
387 case OP_CMPL_FLOAT:
388 case OP_CMPG_FLOAT:
389 case OP_CMPL_DOUBLE:
390 case OP_CMPG_DOUBLE:
391 case OP_CMP_LONG:
392 case OP_NEG_INT:
393 case OP_NOT_INT:
394 case OP_NEG_LONG:
395 case OP_NOT_LONG:
396 case OP_NEG_FLOAT:
397 case OP_NEG_DOUBLE:
398 case OP_INT_TO_LONG:
399 case OP_INT_TO_FLOAT:
400 case OP_INT_TO_DOUBLE:
401 case OP_LONG_TO_INT:
402 case OP_LONG_TO_FLOAT:
403 case OP_LONG_TO_DOUBLE:
404 case OP_FLOAT_TO_INT:
405 case OP_FLOAT_TO_LONG:
406 case OP_FLOAT_TO_DOUBLE:
407 case OP_DOUBLE_TO_INT:
408 case OP_DOUBLE_TO_LONG:
409 case OP_DOUBLE_TO_FLOAT:
410 case OP_INT_TO_BYTE:
411 case OP_INT_TO_CHAR:
412 case OP_INT_TO_SHORT:
413 case OP_ADD_INT:
414 case OP_SUB_INT:
415 case OP_MUL_INT:
416 case OP_AND_INT:
417 case OP_OR_INT:
418 case OP_XOR_INT:
419 case OP_SHL_INT:
420 case OP_SHR_INT:
421 case OP_USHR_INT:
422 case OP_ADD_LONG:
423 case OP_SUB_LONG:
424 case OP_MUL_LONG:
425 case OP_AND_LONG:
426 case OP_OR_LONG:
427 case OP_XOR_LONG:
428 case OP_SHL_LONG:
429 case OP_SHR_LONG:
430 case OP_USHR_LONG:
431 case OP_ADD_FLOAT:
432 case OP_SUB_FLOAT:
433 case OP_MUL_FLOAT:
434 case OP_DIV_FLOAT:
435 case OP_REM_FLOAT:
436 case OP_ADD_DOUBLE:
437 case OP_SUB_DOUBLE:
438 case OP_MUL_DOUBLE:
439 case OP_DIV_DOUBLE: // div by zero just returns NaN
440 case OP_REM_DOUBLE:
441 case OP_ADD_INT_2ADDR:
442 case OP_SUB_INT_2ADDR:
443 case OP_MUL_INT_2ADDR:
444 case OP_AND_INT_2ADDR:
445 case OP_OR_INT_2ADDR:
446 case OP_XOR_INT_2ADDR:
447 case OP_SHL_INT_2ADDR:
448 case OP_SHR_INT_2ADDR:
449 case OP_USHR_INT_2ADDR:
450 case OP_ADD_LONG_2ADDR:
451 case OP_SUB_LONG_2ADDR:
452 case OP_MUL_LONG_2ADDR:
453 case OP_AND_LONG_2ADDR:
454 case OP_OR_LONG_2ADDR:
455 case OP_XOR_LONG_2ADDR:
456 case OP_SHL_LONG_2ADDR:
457 case OP_SHR_LONG_2ADDR:
458 case OP_USHR_LONG_2ADDR:
459 case OP_ADD_FLOAT_2ADDR:
460 case OP_SUB_FLOAT_2ADDR:
461 case OP_MUL_FLOAT_2ADDR:
462 case OP_DIV_FLOAT_2ADDR:
463 case OP_REM_FLOAT_2ADDR:
464 case OP_ADD_DOUBLE_2ADDR:
465 case OP_SUB_DOUBLE_2ADDR:
466 case OP_MUL_DOUBLE_2ADDR:
467 case OP_DIV_DOUBLE_2ADDR:
468 case OP_REM_DOUBLE_2ADDR:
469 case OP_ADD_INT_LIT16:
470 case OP_RSUB_INT:
471 case OP_MUL_INT_LIT16:
472 case OP_AND_INT_LIT16:
473 case OP_OR_INT_LIT16:
474 case OP_XOR_INT_LIT16:
475 case OP_ADD_INT_LIT8:
476 case OP_RSUB_INT_LIT8:
477 case OP_MUL_INT_LIT8:
478 case OP_AND_INT_LIT8:
479 case OP_OR_INT_LIT8:
480 case OP_XOR_INT_LIT8:
481 case OP_SHL_INT_LIT8:
482 case OP_SHR_INT_LIT8:
483 case OP_USHR_INT_LIT8:
484 flags = kInstrCanContinue;
485 break;
486
487 /* these don't affect the PC, but can cause exceptions */
488 case OP_CONST_STRING:
489 case OP_CONST_STRING_JUMBO:
490 case OP_CONST_CLASS:
491 case OP_MONITOR_ENTER:
492 case OP_MONITOR_EXIT:
493 case OP_CHECK_CAST:
494 case OP_INSTANCE_OF:
495 case OP_ARRAY_LENGTH:
496 case OP_NEW_INSTANCE:
497 case OP_NEW_ARRAY:
498 case OP_FILLED_NEW_ARRAY:
499 case OP_FILLED_NEW_ARRAY_RANGE:
500 case OP_AGET:
501 case OP_AGET_BOOLEAN:
502 case OP_AGET_BYTE:
503 case OP_AGET_CHAR:
504 case OP_AGET_SHORT:
505 case OP_AGET_WIDE:
506 case OP_AGET_OBJECT:
507 case OP_APUT:
508 case OP_APUT_BOOLEAN:
509 case OP_APUT_BYTE:
510 case OP_APUT_CHAR:
511 case OP_APUT_SHORT:
512 case OP_APUT_WIDE:
513 case OP_APUT_OBJECT:
514 case OP_IGET:
515 case OP_IGET_BOOLEAN:
516 case OP_IGET_BYTE:
517 case OP_IGET_CHAR:
518 case OP_IGET_SHORT:
519 case OP_IGET_WIDE:
520 case OP_IGET_OBJECT:
521 case OP_IPUT:
522 case OP_IPUT_BOOLEAN:
523 case OP_IPUT_BYTE:
524 case OP_IPUT_CHAR:
525 case OP_IPUT_SHORT:
526 case OP_IPUT_WIDE:
527 case OP_IPUT_OBJECT:
528 case OP_SGET:
529 case OP_SGET_BOOLEAN:
530 case OP_SGET_BYTE:
531 case OP_SGET_CHAR:
532 case OP_SGET_SHORT:
533 case OP_SGET_WIDE:
534 case OP_SGET_OBJECT:
535 case OP_SPUT:
536 case OP_SPUT_BOOLEAN:
537 case OP_SPUT_BYTE:
538 case OP_SPUT_CHAR:
539 case OP_SPUT_SHORT:
540 case OP_SPUT_WIDE:
541 case OP_SPUT_OBJECT:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800542 case OP_DIV_INT:
543 case OP_REM_INT:
544 case OP_DIV_LONG:
545 case OP_REM_LONG:
546 case OP_DIV_INT_2ADDR:
547 case OP_REM_INT_2ADDR:
548 case OP_DIV_LONG_2ADDR:
549 case OP_REM_LONG_2ADDR:
550 case OP_DIV_INT_LIT16:
551 case OP_REM_INT_LIT16:
552 case OP_DIV_INT_LIT8:
553 case OP_REM_INT_LIT8:
554 flags = kInstrCanContinue | kInstrCanThrow;
555 break;
556
Ben Chengba4fc8b2009-06-01 13:00:29 -0700557 case OP_INVOKE_VIRTUAL:
558 case OP_INVOKE_VIRTUAL_RANGE:
559 case OP_INVOKE_SUPER:
560 case OP_INVOKE_SUPER_RANGE:
561 case OP_INVOKE_DIRECT:
562 case OP_INVOKE_DIRECT_RANGE:
563 case OP_INVOKE_STATIC:
564 case OP_INVOKE_STATIC_RANGE:
565 case OP_INVOKE_INTERFACE:
566 case OP_INVOKE_INTERFACE_RANGE:
567 flags = kInstrCanContinue | kInstrCanThrow | kInstrInvoke;
568 break;
569
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800570 case OP_RETURN_VOID:
571 case OP_RETURN:
572 case OP_RETURN_WIDE:
573 case OP_RETURN_OBJECT:
574 flags = kInstrCanReturn;
575 break;
576
577 case OP_THROW:
578 flags = kInstrCanThrow;
579 break;
580
581 /* unconditional branches */
582 case OP_GOTO:
583 case OP_GOTO_16:
584 case OP_GOTO_32:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700585 flags = kInstrCanBranch | kInstrUnconditional;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800586 break;
587
588 /* conditional branches */
589 case OP_IF_EQ:
590 case OP_IF_NE:
591 case OP_IF_LT:
592 case OP_IF_GE:
593 case OP_IF_GT:
594 case OP_IF_LE:
595 case OP_IF_EQZ:
596 case OP_IF_NEZ:
597 case OP_IF_LTZ:
598 case OP_IF_GEZ:
599 case OP_IF_GTZ:
600 case OP_IF_LEZ:
601 flags = kInstrCanBranch | kInstrCanContinue;
602 break;
603
604 /* switch statements; if value not in switch, it continues */
605 case OP_PACKED_SWITCH:
606 case OP_SPARSE_SWITCH:
607 flags = kInstrCanSwitch | kInstrCanContinue;
608 break;
609
Andy McFaddenb51ea112009-05-08 16:50:17 -0700610 /* verifier/optimizer-generated instructions */
Andy McFadden3a1aedb2009-05-07 13:30:23 -0700611 case OP_THROW_VERIFICATION_ERROR:
612 flags = kInstrCanThrow;
613 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800614 case OP_EXECUTE_INLINE:
615 flags = kInstrCanContinue;
616 break;
617 case OP_IGET_QUICK:
618 case OP_IGET_WIDE_QUICK:
619 case OP_IGET_OBJECT_QUICK:
620 case OP_IPUT_QUICK:
621 case OP_IPUT_WIDE_QUICK:
622 case OP_IPUT_OBJECT_QUICK:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700623 flags = kInstrCanContinue | kInstrCanThrow;
624 break;
625
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800626 case OP_INVOKE_VIRTUAL_QUICK:
627 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
628 case OP_INVOKE_SUPER_QUICK:
629 case OP_INVOKE_SUPER_QUICK_RANGE:
630 case OP_INVOKE_DIRECT_EMPTY:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700631 flags = kInstrCanContinue | kInstrCanThrow | kInstrInvoke;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800632 break;
633
634 /* these should never appear */
635 case OP_UNUSED_3E:
636 case OP_UNUSED_3F:
637 case OP_UNUSED_40:
638 case OP_UNUSED_41:
639 case OP_UNUSED_42:
640 case OP_UNUSED_43:
641 case OP_UNUSED_73:
642 case OP_UNUSED_79:
643 case OP_UNUSED_7A:
644 case OP_UNUSED_E3:
645 case OP_UNUSED_E4:
646 case OP_UNUSED_E5:
647 case OP_UNUSED_E6:
648 case OP_UNUSED_E7:
649 case OP_UNUSED_E8:
650 case OP_UNUSED_E9:
651 case OP_UNUSED_EA:
652 case OP_UNUSED_EB:
653 case OP_UNUSED_EC:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800654 case OP_UNUSED_EF:
655 case OP_UNUSED_F1:
656 case OP_UNUSED_FC:
657 case OP_UNUSED_FD:
658 case OP_UNUSED_FE:
659 case OP_UNUSED_FF:
660 break;
661
662 /*
663 * DO NOT add a "default" clause here. Without it the compiler will
664 * complain if an instruction is missing (which is desirable).
665 */
666 }
667
668 instrFlags[opc] = flags;
669 }
670
671 return instrFlags;
672}
673
674/*
675 * Allocate and populate a 256-element array with instruction formats.
676 * Used in conjunction with dexDecodeInstruction.
677 */
678InstructionFormat* dexCreateInstrFormatTable(void)
679{
680 InstructionFormat* instFmt;
681 int i;
682
683 instFmt = malloc(sizeof(InstructionFormat) * kNumDalvikInstructions);
684 if (instFmt == NULL)
685 return NULL;
686
687 for (i = 0; i < kNumDalvikInstructions; i++) {
688 OpCode opc = (OpCode) i;
689 InstructionFormat fmt = kFmtUnknown;
690
691 switch (opc) {
692 case OP_GOTO:
693 fmt = kFmt10t;
694 break;
695 case OP_NOP:
696 case OP_RETURN_VOID:
697 fmt = kFmt10x;
698 break;
699 case OP_CONST_4:
700 fmt = kFmt11n;
701 break;
702 case OP_CONST_HIGH16:
703 case OP_CONST_WIDE_HIGH16:
704 fmt = kFmt21h;
705 break;
706 case OP_MOVE_RESULT:
707 case OP_MOVE_RESULT_WIDE:
708 case OP_MOVE_RESULT_OBJECT:
709 case OP_MOVE_EXCEPTION:
710 case OP_RETURN:
711 case OP_RETURN_WIDE:
712 case OP_RETURN_OBJECT:
713 case OP_MONITOR_ENTER:
714 case OP_MONITOR_EXIT:
715 case OP_THROW:
716 fmt = kFmt11x;
717 break;
718 case OP_MOVE:
719 case OP_MOVE_WIDE:
720 case OP_MOVE_OBJECT:
721 case OP_ARRAY_LENGTH:
722 case OP_NEG_INT:
723 case OP_NOT_INT:
724 case OP_NEG_LONG:
725 case OP_NOT_LONG:
726 case OP_NEG_FLOAT:
727 case OP_NEG_DOUBLE:
728 case OP_INT_TO_LONG:
729 case OP_INT_TO_FLOAT:
730 case OP_INT_TO_DOUBLE:
731 case OP_LONG_TO_INT:
732 case OP_LONG_TO_FLOAT:
733 case OP_LONG_TO_DOUBLE:
734 case OP_FLOAT_TO_INT:
735 case OP_FLOAT_TO_LONG:
736 case OP_FLOAT_TO_DOUBLE:
737 case OP_DOUBLE_TO_INT:
738 case OP_DOUBLE_TO_LONG:
739 case OP_DOUBLE_TO_FLOAT:
740 case OP_INT_TO_BYTE:
741 case OP_INT_TO_CHAR:
742 case OP_INT_TO_SHORT:
743 case OP_ADD_INT_2ADDR:
744 case OP_SUB_INT_2ADDR:
745 case OP_MUL_INT_2ADDR:
746 case OP_DIV_INT_2ADDR:
747 case OP_REM_INT_2ADDR:
748 case OP_AND_INT_2ADDR:
749 case OP_OR_INT_2ADDR:
750 case OP_XOR_INT_2ADDR:
751 case OP_SHL_INT_2ADDR:
752 case OP_SHR_INT_2ADDR:
753 case OP_USHR_INT_2ADDR:
754 case OP_ADD_LONG_2ADDR:
755 case OP_SUB_LONG_2ADDR:
756 case OP_MUL_LONG_2ADDR:
757 case OP_DIV_LONG_2ADDR:
758 case OP_REM_LONG_2ADDR:
759 case OP_AND_LONG_2ADDR:
760 case OP_OR_LONG_2ADDR:
761 case OP_XOR_LONG_2ADDR:
762 case OP_SHL_LONG_2ADDR:
763 case OP_SHR_LONG_2ADDR:
764 case OP_USHR_LONG_2ADDR:
765 case OP_ADD_FLOAT_2ADDR:
766 case OP_SUB_FLOAT_2ADDR:
767 case OP_MUL_FLOAT_2ADDR:
768 case OP_DIV_FLOAT_2ADDR:
769 case OP_REM_FLOAT_2ADDR:
770 case OP_ADD_DOUBLE_2ADDR:
771 case OP_SUB_DOUBLE_2ADDR:
772 case OP_MUL_DOUBLE_2ADDR:
773 case OP_DIV_DOUBLE_2ADDR:
774 case OP_REM_DOUBLE_2ADDR:
775 fmt = kFmt12x;
776 break;
777 case OP_GOTO_16:
778 fmt = kFmt20t;
779 break;
780 case OP_GOTO_32:
781 fmt = kFmt30t;
782 break;
783 case OP_CONST_STRING:
784 case OP_CONST_CLASS:
785 case OP_CHECK_CAST:
786 case OP_NEW_INSTANCE:
787 case OP_SGET:
788 case OP_SGET_WIDE:
789 case OP_SGET_OBJECT:
790 case OP_SGET_BOOLEAN:
791 case OP_SGET_BYTE:
792 case OP_SGET_CHAR:
793 case OP_SGET_SHORT:
794 case OP_SPUT:
795 case OP_SPUT_WIDE:
796 case OP_SPUT_OBJECT:
797 case OP_SPUT_BOOLEAN:
798 case OP_SPUT_BYTE:
799 case OP_SPUT_CHAR:
800 case OP_SPUT_SHORT:
801 fmt = kFmt21c;
802 break;
803 case OP_CONST_16:
804 case OP_CONST_WIDE_16:
805 fmt = kFmt21s;
806 break;
807 case OP_IF_EQZ:
808 case OP_IF_NEZ:
809 case OP_IF_LTZ:
810 case OP_IF_GEZ:
811 case OP_IF_GTZ:
812 case OP_IF_LEZ:
813 fmt = kFmt21t;
814 break;
815 case OP_FILL_ARRAY_DATA:
816 case OP_PACKED_SWITCH:
817 case OP_SPARSE_SWITCH:
818 fmt = kFmt31t;
819 break;
820 case OP_ADD_INT_LIT8:
821 case OP_RSUB_INT_LIT8:
822 case OP_MUL_INT_LIT8:
823 case OP_DIV_INT_LIT8:
824 case OP_REM_INT_LIT8:
825 case OP_AND_INT_LIT8:
826 case OP_OR_INT_LIT8:
827 case OP_XOR_INT_LIT8:
828 case OP_SHL_INT_LIT8:
829 case OP_SHR_INT_LIT8:
830 case OP_USHR_INT_LIT8:
831 fmt = kFmt22b;
832 break;
833 case OP_INSTANCE_OF:
834 case OP_NEW_ARRAY:
835 case OP_IGET:
836 case OP_IGET_WIDE:
837 case OP_IGET_OBJECT:
838 case OP_IGET_BOOLEAN:
839 case OP_IGET_BYTE:
840 case OP_IGET_CHAR:
841 case OP_IGET_SHORT:
842 case OP_IPUT:
843 case OP_IPUT_WIDE:
844 case OP_IPUT_OBJECT:
845 case OP_IPUT_BOOLEAN:
846 case OP_IPUT_BYTE:
847 case OP_IPUT_CHAR:
848 case OP_IPUT_SHORT:
849 fmt = kFmt22c;
850 break;
851 case OP_ADD_INT_LIT16:
852 case OP_RSUB_INT:
853 case OP_MUL_INT_LIT16:
854 case OP_DIV_INT_LIT16:
855 case OP_REM_INT_LIT16:
856 case OP_AND_INT_LIT16:
857 case OP_OR_INT_LIT16:
858 case OP_XOR_INT_LIT16:
859 fmt = kFmt22s;
860 break;
861 case OP_IF_EQ:
862 case OP_IF_NE:
863 case OP_IF_LT:
864 case OP_IF_GE:
865 case OP_IF_GT:
866 case OP_IF_LE:
867 fmt = kFmt22t;
868 break;
869 case OP_MOVE_FROM16:
870 case OP_MOVE_WIDE_FROM16:
871 case OP_MOVE_OBJECT_FROM16:
872 fmt = kFmt22x;
873 break;
874 case OP_CMPL_FLOAT:
875 case OP_CMPG_FLOAT:
876 case OP_CMPL_DOUBLE:
877 case OP_CMPG_DOUBLE:
878 case OP_CMP_LONG:
879 case OP_AGET:
880 case OP_AGET_WIDE:
881 case OP_AGET_OBJECT:
882 case OP_AGET_BOOLEAN:
883 case OP_AGET_BYTE:
884 case OP_AGET_CHAR:
885 case OP_AGET_SHORT:
886 case OP_APUT:
887 case OP_APUT_WIDE:
888 case OP_APUT_OBJECT:
889 case OP_APUT_BOOLEAN:
890 case OP_APUT_BYTE:
891 case OP_APUT_CHAR:
892 case OP_APUT_SHORT:
893 case OP_ADD_INT:
894 case OP_SUB_INT:
895 case OP_MUL_INT:
896 case OP_DIV_INT:
897 case OP_REM_INT:
898 case OP_AND_INT:
899 case OP_OR_INT:
900 case OP_XOR_INT:
901 case OP_SHL_INT:
902 case OP_SHR_INT:
903 case OP_USHR_INT:
904 case OP_ADD_LONG:
905 case OP_SUB_LONG:
906 case OP_MUL_LONG:
907 case OP_DIV_LONG:
908 case OP_REM_LONG:
909 case OP_AND_LONG:
910 case OP_OR_LONG:
911 case OP_XOR_LONG:
912 case OP_SHL_LONG:
913 case OP_SHR_LONG:
914 case OP_USHR_LONG:
915 case OP_ADD_FLOAT:
916 case OP_SUB_FLOAT:
917 case OP_MUL_FLOAT:
918 case OP_DIV_FLOAT:
919 case OP_REM_FLOAT:
920 case OP_ADD_DOUBLE:
921 case OP_SUB_DOUBLE:
922 case OP_MUL_DOUBLE:
923 case OP_DIV_DOUBLE:
924 case OP_REM_DOUBLE:
925 fmt = kFmt23x;
926 break;
927 case OP_CONST:
928 case OP_CONST_WIDE_32:
929 fmt = kFmt31i;
930 break;
931 case OP_CONST_STRING_JUMBO:
932 fmt = kFmt31c;
933 break;
934 case OP_MOVE_16:
935 case OP_MOVE_WIDE_16:
936 case OP_MOVE_OBJECT_16:
937 fmt = kFmt32x;
938 break;
939 case OP_FILLED_NEW_ARRAY:
940 case OP_INVOKE_VIRTUAL:
941 case OP_INVOKE_SUPER:
942 case OP_INVOKE_DIRECT:
943 case OP_INVOKE_STATIC:
944 case OP_INVOKE_INTERFACE:
945 fmt = kFmt35c;
946 break;
947 case OP_FILLED_NEW_ARRAY_RANGE:
948 case OP_INVOKE_VIRTUAL_RANGE:
949 case OP_INVOKE_SUPER_RANGE:
950 case OP_INVOKE_DIRECT_RANGE:
951 case OP_INVOKE_STATIC_RANGE:
952 case OP_INVOKE_INTERFACE_RANGE:
953 fmt = kFmt3rc;
954 break;
955 case OP_CONST_WIDE:
956 fmt = kFmt51l;
957 break;
958
959 /*
960 * Optimized instructions.
961 */
Andy McFadden3a1aedb2009-05-07 13:30:23 -0700962 case OP_THROW_VERIFICATION_ERROR:
963 fmt = kFmt20bc;
964 break;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800965 case OP_IGET_QUICK:
966 case OP_IGET_WIDE_QUICK:
967 case OP_IGET_OBJECT_QUICK:
968 case OP_IPUT_QUICK:
969 case OP_IPUT_WIDE_QUICK:
970 case OP_IPUT_OBJECT_QUICK:
971 fmt = kFmt22cs;
972 break;
973 case OP_INVOKE_VIRTUAL_QUICK:
974 case OP_INVOKE_SUPER_QUICK:
975 fmt = kFmt35ms;
976 break;
977 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
978 case OP_INVOKE_SUPER_QUICK_RANGE:
979 fmt = kFmt3rms;
980 break;
981 case OP_EXECUTE_INLINE:
982 fmt = kFmt3inline;
983 break;
984 case OP_INVOKE_DIRECT_EMPTY:
985 fmt = kFmt35c;
986 break;
987
988 /* these should never appear */
989 case OP_UNUSED_3E:
990 case OP_UNUSED_3F:
991 case OP_UNUSED_40:
992 case OP_UNUSED_41:
993 case OP_UNUSED_42:
994 case OP_UNUSED_43:
995 case OP_UNUSED_73:
996 case OP_UNUSED_79:
997 case OP_UNUSED_7A:
998 case OP_UNUSED_E3:
999 case OP_UNUSED_E4:
1000 case OP_UNUSED_E5:
1001 case OP_UNUSED_E6:
1002 case OP_UNUSED_E7:
1003 case OP_UNUSED_E8:
1004 case OP_UNUSED_E9:
1005 case OP_UNUSED_EA:
1006 case OP_UNUSED_EB:
1007 case OP_UNUSED_EC:
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001008 case OP_UNUSED_EF:
1009 case OP_UNUSED_F1:
1010 case OP_UNUSED_FC:
1011 case OP_UNUSED_FD:
1012 case OP_UNUSED_FE:
1013 case OP_UNUSED_FF:
1014 fmt = kFmtUnknown;
1015 break;
1016
1017 /*
1018 * DO NOT add a "default" clause here. Without it the compiler will
1019 * complain if an instruction is missing (which is desirable).
1020 */
1021 }
1022
1023 instFmt[opc] = fmt;
1024 }
1025
1026 return instFmt;
1027}
1028
1029/*
1030 * Copied from InterpCore.h. Used for instruction decoding.
1031 */
1032#define FETCH(_offset) (insns[(_offset)])
1033#define INST_INST(_inst) ((_inst) & 0xff)
1034#define INST_A(_inst) (((u2)(_inst) >> 8) & 0x0f)
1035#define INST_B(_inst) ((u2)(_inst) >> 12)
1036#define INST_AA(_inst) ((_inst) >> 8)
1037
1038/*
1039 * Decode the instruction pointed to by "insns".
1040 *
1041 * Fills out the pieces of "pDec" that are affected by the current
1042 * instruction. Does not touch anything else.
1043 */
1044void dexDecodeInstruction(const InstructionFormat* fmts, const u2* insns,
1045 DecodedInstruction* pDec)
1046{
1047 u2 inst = *insns;
1048
1049 pDec->opCode = (OpCode) INST_INST(inst);
1050
1051 switch (dexGetInstrFormat(fmts, pDec->opCode)) {
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001052 case kFmt10x: // op
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001053 /* nothing to do; copy the AA bits out for the verifier */
1054 pDec->vA = INST_AA(inst);
1055 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001056 case kFmt12x: // op vA, vB
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001057 pDec->vA = INST_A(inst);
1058 pDec->vB = INST_B(inst);
1059 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001060 case kFmt11n: // op vA, #+B
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001061 pDec->vA = INST_A(inst);
1062 pDec->vB = (s4) (INST_B(inst) << 28) >> 28; // sign extend 4-bit value
1063 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001064 case kFmt11x: // op vAA
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001065 pDec->vA = INST_AA(inst);
1066 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001067 case kFmt10t: // op +AA
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001068 pDec->vA = (s1) INST_AA(inst); // sign-extend 8-bit value
1069 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001070 case kFmt20t: // op +AAAA
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001071 pDec->vA = (s2) FETCH(1); // sign-extend 16-bit value
1072 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001073 case kFmt20bc: // op AA, thing@BBBB
1074 case kFmt21c: // op vAA, thing@BBBB
1075 case kFmt22x: // op vAA, vBBBB
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001076 pDec->vA = INST_AA(inst);
1077 pDec->vB = FETCH(1);
1078 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001079 case kFmt21s: // op vAA, #+BBBB
1080 case kFmt21t: // op vAA, +BBBB
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001081 pDec->vA = INST_AA(inst);
1082 pDec->vB = (s2) FETCH(1); // sign-extend 16-bit value
1083 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001084 case kFmt21h: // op vAA, #+BBBB0000[00000000]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001085 pDec->vA = INST_AA(inst);
1086 /*
1087 * The value should be treated as right-zero-extended, but we don't
1088 * actually do that here. Among other things, we don't know if it's
1089 * the top bits of a 32- or 64-bit value.
1090 */
1091 pDec->vB = FETCH(1);
1092 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001093 case kFmt23x: // op vAA, vBB, vCC
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001094 pDec->vA = INST_AA(inst);
1095 pDec->vB = FETCH(1) & 0xff;
1096 pDec->vC = FETCH(1) >> 8;
1097 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001098 case kFmt22b: // op vAA, vBB, #+CC
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001099 pDec->vA = INST_AA(inst);
1100 pDec->vB = FETCH(1) & 0xff;
1101 pDec->vC = (s1) (FETCH(1) >> 8); // sign-extend 8-bit value
1102 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001103 case kFmt22s: // op vA, vB, #+CCCC
1104 case kFmt22t: // op vA, vB, +CCCC
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001105 pDec->vA = INST_A(inst);
1106 pDec->vB = INST_B(inst);
1107 pDec->vC = (s2) FETCH(1); // sign-extend 16-bit value
1108 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001109 case kFmt22c: // op vA, vB, thing@CCCC
1110 case kFmt22cs: // [opt] op vA, vB, field offset CCCC
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001111 pDec->vA = INST_A(inst);
1112 pDec->vB = INST_B(inst);
1113 pDec->vC = FETCH(1);
1114 break;
1115 case kFmt30t: // op +AAAAAAAA
1116 pDec->vA = FETCH(1) | ((u4) FETCH(2) << 16); // signed 32-bit value
1117 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001118 case kFmt31t: // op vAA, +BBBBBBBB
1119 case kFmt31c: // op vAA, thing@BBBBBBBB
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001120 pDec->vA = INST_AA(inst);
1121 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16); // 32-bit value
1122 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001123 case kFmt32x: // op vAAAA, vBBBB
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001124 pDec->vA = FETCH(1);
1125 pDec->vB = FETCH(2);
1126 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001127 case kFmt31i: // op vAA, #+BBBBBBBB
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001128 pDec->vA = INST_AA(inst);
1129 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16);
1130 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001131 case kFmt35c: // op vB, {vD..vG,vA}, thing@CCCC
1132 case kFmt35ms: // [opt] invoke-virtual+super
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001133 {
1134 /*
1135 * The lettering changes that came about when we went from 4 args
1136 * to 5 made the "range" versions of the calls different from
1137 * the non-range versions. We have the choice between decoding
1138 * them the way the spec shows and having lots of conditionals
1139 * in the verifier, or mapping the values onto their original
1140 * registers and leaving the verifier intact.
1141 *
1142 * Current plan is to leave the verifier alone. We can fix it
1143 * later if it's architecturally unbearable.
1144 *
1145 * Bottom line: method constant is always in vB.
1146 */
1147 u2 regList;
1148 int i, count;
1149
1150 pDec->vA = INST_B(inst);
1151 pDec->vB = FETCH(1);
1152 regList = FETCH(2);
1153
1154 if (pDec->vA > 5) {
1155 LOGW("Invalid arg count in 35c/35ms (%d)\n", pDec->vA);
1156 goto bail;
1157 }
1158 count = pDec->vA;
1159 if (count == 5) {
1160 /* 5th arg comes from A field in instruction */
1161 pDec->arg[4] = INST_A(inst);
1162 count--;
1163 }
1164 for (i = 0; i < count; i++) {
1165 pDec->arg[i] = regList & 0x0f;
1166 regList >>= 4;
1167 }
1168 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */
1169 if (pDec->vA > 0)
1170 pDec->vC = pDec->arg[0];
1171 }
1172 break;
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001173 case kFmt3inline: // [opt] inline invoke
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001174 {
1175 u2 regList;
1176 int i;
1177
1178 pDec->vA = INST_B(inst);
1179 pDec->vB = FETCH(1);
1180 regList = FETCH(2);
1181
1182 if (pDec->vA > 4) {
1183 LOGW("Invalid arg count in 3inline (%d)\n", pDec->vA);
1184 goto bail;
1185 }
1186 for (i = 0; i < (int) pDec->vA; i++) {
1187 pDec->arg[i] = regList & 0x0f;
1188 regList >>= 4;
1189 }
1190 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */
1191 if (pDec->vA > 0)
1192 pDec->vC = pDec->arg[0];
1193 }
1194 break;
1195 case kFmt35fs: // [opt] invoke-interface
1196 assert(false); // TODO
1197 break;
1198 case kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
1199 case kFmt3rms: // [opt] invoke-virtual+super/range
1200 pDec->vA = INST_AA(inst);
1201 pDec->vB = FETCH(1);
1202 pDec->vC = FETCH(2);
1203 break;
1204 case kFmt3rfs: // [opt] invoke-interface/range
1205 assert(false); // TODO
1206 break;
1207 case kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB
1208 pDec->vA = INST_AA(inst);
1209 pDec->vB_wide = FETCH(1);
1210 pDec->vB_wide |= (u8)FETCH(2) << 16;
1211 pDec->vB_wide |= (u8)FETCH(3) << 32;
1212 pDec->vB_wide |= (u8)FETCH(4) << 48;
1213 break;
1214 default:
1215 LOGW("Can't decode unexpected format %d (op=%d)\n",
1216 dexGetInstrFormat(fmts, pDec->opCode), pDec->opCode);
1217 assert(false);
1218 break;
1219 }
1220
1221bail:
1222 ;
1223}
1224
1225/*
1226 * Return the width of the specified instruction, or 0 if not defined. Also
1227 * works for special OP_NOP entries, including switch statement data tables
1228 * and array data.
1229 */
1230int dexGetInstrOrTableWidthAbs(const InstructionWidth* widths, const u2* insns)
1231{
1232 int width;
1233
1234 if (*insns == kPackedSwitchSignature) {
1235 width = 4 + insns[1] * 2;
1236 } else if (*insns == kSparseSwitchSignature) {
1237 width = 2 + insns[1] * 4;
1238 } else if (*insns == kArrayDataSignature) {
1239 u2 elemWidth = insns[1];
1240 u4 len = insns[2] | (((u4)insns[3]) << 16);
1241 width = 4 + (elemWidth * len + 1) / 2;
1242 } else {
1243 width = dexGetInstrWidthAbs(widths, INST_INST(insns[0]));
1244 }
1245 return width;
1246}