The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
Andy McFadden | 3a1aedb | 2009-05-07 13:30:23 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Dalvik instruction utility functions. |
| 19 | */ |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 20 | #ifndef LIBDEX_INSTRUTILS_H_ |
| 21 | #define LIBDEX_INSTRUTILS_H_ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | |
| 23 | #include "DexFile.h" |
Dan Bornstein | df4daaf | 2010-12-01 14:23:44 -0800 | [diff] [blame] | 24 | #include "DexOpcodes.h" |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | |
| 26 | /* |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 27 | * Possible instruction formats associated with Dalvik opcodes. |
Carl Shapiro | de75089 | 2010-06-08 16:37:12 -0700 | [diff] [blame] | 28 | * |
Dan Bornstein | 758a673 | 2010-11-30 10:45:02 -0800 | [diff] [blame] | 29 | * See the file opcode-gen/README.txt for information about updating |
| 30 | * opcodes and instruction formats. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 31 | */ |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 32 | enum InstructionFormat { |
Dan Bornstein | 8424432 | 2010-11-17 12:05:04 -0800 | [diff] [blame] | 33 | kFmt00x = 0, // unknown format (also used for "breakpoint" opcode) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | kFmt10x, // op |
| 35 | kFmt12x, // op vA, vB |
| 36 | kFmt11n, // op vA, #+B |
| 37 | kFmt11x, // op vAA |
| 38 | kFmt10t, // op +AA |
Andy McFadden | d325011 | 2010-11-03 14:32:42 -0700 | [diff] [blame] | 39 | kFmt20bc, // [opt] op AA, thing@BBBB |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 40 | kFmt20t, // op +AAAA |
| 41 | kFmt22x, // op vAA, vBBBB |
| 42 | kFmt21t, // op vAA, +BBBB |
| 43 | kFmt21s, // op vAA, #+BBBB |
| 44 | kFmt21h, // op vAA, #+BBBB00000[00000000] |
| 45 | kFmt21c, // op vAA, thing@BBBB |
| 46 | kFmt23x, // op vAA, vBB, vCC |
| 47 | kFmt22b, // op vAA, vBB, #+CC |
| 48 | kFmt22t, // op vA, vB, +CCCC |
| 49 | kFmt22s, // op vA, vB, #+CCCC |
| 50 | kFmt22c, // op vA, vB, thing@CCCC |
| 51 | kFmt22cs, // [opt] op vA, vB, field offset CCCC |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 52 | kFmt30t, // op +AAAAAAAA |
Andy McFadden | d325011 | 2010-11-03 14:32:42 -0700 | [diff] [blame] | 53 | kFmt32x, // op vAAAA, vBBBB |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 54 | kFmt31i, // op vAA, #+BBBBBBBB |
Andy McFadden | d325011 | 2010-11-03 14:32:42 -0700 | [diff] [blame] | 55 | kFmt31t, // op vAA, +BBBBBBBB |
| 56 | kFmt31c, // op vAA, string@BBBBBBBB |
Dan Bornstein | 7b3e9b0 | 2010-11-09 17:15:10 -0800 | [diff] [blame] | 57 | kFmt35c, // op {vC,vD,vE,vF,vG}, thing@BBBB |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 58 | kFmt35ms, // [opt] invoke-virtual+super |
Dan Bornstein | 4a6b482 | 2010-11-11 12:26:58 -0800 | [diff] [blame] | 59 | kFmt3rc, // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 60 | kFmt3rms, // [opt] invoke-virtual+super/range |
Andy McFadden | d325011 | 2010-11-03 14:32:42 -0700 | [diff] [blame] | 61 | kFmt51l, // op vAA, #+BBBBBBBBBBBBBBBB |
Dan Bornstein | 7b3e9b0 | 2010-11-09 17:15:10 -0800 | [diff] [blame] | 62 | kFmt35mi, // [opt] inline invoke |
| 63 | kFmt3rmi, // [opt] inline invoke/range |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 64 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 65 | |
| 66 | /* |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 67 | * Types of indexed reference that are associated with opcodes whose |
| 68 | * formats include such an indexed reference (e.g., 21c and 35c). |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 69 | */ |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 70 | enum InstructionIndexType { |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 71 | kIndexUnknown = 0, |
| 72 | kIndexNone, // has no index |
| 73 | kIndexVaries, // "It depends." Used for throw-verification-error |
Dan Bornstein | 1530c3e | 2010-11-12 12:51:35 -0800 | [diff] [blame] | 74 | kIndexTypeRef, // type reference index |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 75 | kIndexStringRef, // string reference index |
| 76 | kIndexMethodRef, // method reference index |
| 77 | kIndexFieldRef, // field reference index |
| 78 | kIndexInlineMethod, // inline method index (for inline linked methods) |
| 79 | kIndexVtableOffset, // vtable offset (for static linked methods) |
| 80 | kIndexFieldOffset // field offset (for static linked fields) |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 81 | }; |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 82 | |
| 83 | /* |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 84 | * Instruction width implied by an opcode's format; a value in the |
| 85 | * range 0 to 5. Note that there are special "pseudo-instructions" |
| 86 | * which are used to encode switch and data tables, and these don't |
| 87 | * have a fixed width. See dexGetWidthFromInstruction(), below. |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 88 | */ |
| 89 | typedef u1 InstructionWidth; |
| 90 | |
| 91 | /* |
| 92 | * Opcode control flow flags, used by the verifier and JIT. |
| 93 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 94 | typedef u1 OpcodeFlags; |
| 95 | enum OpcodeFlagsBits { |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 96 | kInstrCanBranch = 1, // conditional or unconditional branch |
| 97 | kInstrCanContinue = 1 << 1, // flow can continue to next statement |
| 98 | kInstrCanSwitch = 1 << 2, // switch statement |
| 99 | kInstrCanThrow = 1 << 3, // could cause an exception to be thrown |
| 100 | kInstrCanReturn = 1 << 4, // returns, no additional statements |
| 101 | kInstrInvoke = 1 << 5, // a flavor of invoke |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 105 | * Struct that includes a pointer to each of the opcode information |
| 106 | * tables. |
| 107 | * |
| 108 | * Note: We use "u1*" here instead of the names of the enumerated |
| 109 | * types to guarantee that elements don't use much space. We hold out |
| 110 | * hope for a standard way to indicate the size of an enumerated type |
| 111 | * that works for both C and C++, but in the mean time, this will |
| 112 | * suffice. |
| 113 | */ |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 114 | struct InstructionInfoTables { |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 115 | u1* formats; /* InstructionFormat elements */ |
| 116 | u1* indexTypes; /* InstructionIndexType elements */ |
| 117 | OpcodeFlags* flags; |
Dan Bornstein | 0f737c5 | 2010-12-02 15:15:21 -0800 | [diff] [blame] | 118 | InstructionWidth* widths; |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 119 | }; |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Global InstructionInfoTables struct. |
| 123 | */ |
| 124 | extern InstructionInfoTables gDexOpcodeInfo; |
| 125 | |
| 126 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | * Holds the contents of a decoded instruction. |
| 128 | */ |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 129 | struct DecodedInstruction { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 130 | u4 vA; |
| 131 | u4 vB; |
| 132 | u8 vB_wide; /* for kFmt51l */ |
| 133 | u4 vC; |
| 134 | u4 arg[5]; /* vC/D/E/F/G in invoke or filled-new-array */ |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 135 | Opcode opcode; |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 136 | InstructionIndexType indexType; |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 137 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 138 | |
| 139 | /* |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 140 | * Return the instruction width of the specified opcode, or 0 if not defined. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 141 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 142 | DEX_INLINE size_t dexGetWidthFromOpcode(Opcode opcode) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 143 | { |
Dan Bornstein | 44209fa | 2010-12-03 17:24:34 -0800 | [diff] [blame] | 144 | assert((u4) opcode < kNumPackedOpcodes); |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 145 | return gDexOpcodeInfo.widths[opcode]; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Return the width of the specified instruction, or 0 if not defined. Also |
| 150 | * works for special OP_NOP entries, including switch statement data tables |
| 151 | * and array data. |
| 152 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 153 | size_t dexGetWidthFromInstruction(const u2* insns); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * Returns the flags for the specified opcode. |
| 157 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 158 | DEX_INLINE OpcodeFlags dexGetFlagsFromOpcode(Opcode opcode) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 159 | { |
Dan Bornstein | 44209fa | 2010-12-03 17:24:34 -0800 | [diff] [blame] | 160 | assert((u4) opcode < kNumPackedOpcodes); |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 161 | return gDexOpcodeInfo.flags[opcode]; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 162 | } |
| 163 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | /* |
Dan Bornstein | c2b486f | 2010-11-12 16:07:16 -0800 | [diff] [blame] | 165 | * Returns true if the given flags represent a goto (unconditional branch). |
| 166 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 167 | DEX_INLINE bool dexIsGoto(OpcodeFlags flags) |
Dan Bornstein | c2b486f | 2010-11-12 16:07:16 -0800 | [diff] [blame] | 168 | { |
| 169 | return (flags & (kInstrCanBranch | kInstrCanContinue)) == kInstrCanBranch; |
| 170 | } |
| 171 | |
| 172 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 173 | * Return the instruction format for the specified opcode. |
| 174 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 175 | DEX_INLINE InstructionFormat dexGetFormatFromOpcode(Opcode opcode) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 176 | { |
Dan Bornstein | 44209fa | 2010-12-03 17:24:34 -0800 | [diff] [blame] | 177 | assert((u4) opcode < kNumPackedOpcodes); |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 178 | return (InstructionFormat) gDexOpcodeInfo.formats[opcode]; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 182 | * Return the instruction index type for the specified opcode. |
| 183 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 184 | DEX_INLINE InstructionIndexType dexGetIndexTypeFromOpcode(Opcode opcode) |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 185 | { |
Dan Bornstein | 44209fa | 2010-12-03 17:24:34 -0800 | [diff] [blame] | 186 | assert((u4) opcode < kNumPackedOpcodes); |
Dan Bornstein | 4b6e9ba | 2010-12-02 10:35:48 -0800 | [diff] [blame] | 187 | return (InstructionIndexType) gDexOpcodeInfo.indexTypes[opcode]; |
Dan Bornstein | 44a38f4 | 2010-11-10 17:34:32 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 191 | * Decode the instruction pointed to by "insns". |
| 192 | */ |
Dan Bornstein | 5432239 | 2010-11-17 14:16:56 -0800 | [diff] [blame] | 193 | void dexDecodeInstruction(const u2* insns, DecodedInstruction* pDec); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 194 | |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 195 | #endif // LIBDEX_INSTRUTILS_H_ |