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