Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_DEX_INSTRUCTION_H_ |
| 18 | #define ART_SRC_DEX_INSTRUCTION_H_ |
| 19 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 22 | #include "globals.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 23 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 24 | typedef uint8_t uint4_t; |
| 25 | typedef int8_t int4_t; |
| 26 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 29 | class DexFile; |
| 30 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 31 | enum { |
| 32 | kNumPackedOpcodes = 0x100 |
| 33 | }; |
| 34 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 35 | class Instruction { |
| 36 | public: |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 37 | // NOP-encoded switch-statement signatures. |
| 38 | enum { |
| 39 | kPackedSwitchSignature = 0x0100, |
| 40 | kSparseSwitchSignature = 0x0200, |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 41 | kArrayDataSignature = 0x0300, |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 44 | struct PACKED(4) PackedSwitchPayload { |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 45 | const uint16_t ident; |
| 46 | const uint16_t case_count; |
| 47 | const int32_t first_key; |
| 48 | const int32_t targets[]; |
| 49 | private: |
| 50 | DISALLOW_COPY_AND_ASSIGN(PackedSwitchPayload); |
| 51 | }; |
| 52 | |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 53 | struct PACKED(4) SparseSwitchPayload { |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 54 | const uint16_t ident; |
| 55 | const uint16_t case_count; |
| 56 | const int32_t keys_and_targets[]; |
| 57 | |
| 58 | public: |
| 59 | const int32_t* GetKeys() const { |
| 60 | return keys_and_targets; |
| 61 | } |
| 62 | |
| 63 | const int32_t* GetTargets() const { |
| 64 | return keys_and_targets + case_count; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | DISALLOW_COPY_AND_ASSIGN(SparseSwitchPayload); |
| 69 | }; |
| 70 | |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 71 | struct PACKED(4) ArrayDataPayload { |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 72 | const uint16_t ident; |
| 73 | const uint16_t element_width; |
| 74 | const uint32_t element_count; |
| 75 | const uint8_t data[]; |
| 76 | private: |
| 77 | DISALLOW_COPY_AND_ASSIGN(ArrayDataPayload); |
| 78 | }; |
| 79 | |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 80 | // TODO: the code layout below is deliberate to avoid this enum being picked up by |
| 81 | // generate-operator-out.py. |
| 82 | enum Code |
| 83 | { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 84 | #define INSTRUCTION_ENUM(opcode, cname, p, f, r, i, a, v) cname = opcode, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 85 | #include "dex_instruction_list.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 86 | DEX_INSTRUCTION_LIST(INSTRUCTION_ENUM) |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 87 | #undef DEX_INSTRUCTION_LIST |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 88 | #undef INSTRUCTION_ENUM |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 89 | } ; |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 90 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 91 | enum Format { |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 92 | k10x, // op |
| 93 | k12x, // op vA, vB |
| 94 | k11n, // op vA, #+B |
| 95 | k11x, // op vAA |
| 96 | k10t, // op +AA |
| 97 | k20t, // op +AAAA |
| 98 | k22x, // op vAA, vBBBB |
| 99 | k21t, // op vAA, +BBBB |
| 100 | k21s, // op vAA, #+BBBB |
| 101 | k21h, // op vAA, #+BBBB00000[00000000] |
| 102 | k21c, // op vAA, thing@BBBB |
| 103 | k23x, // op vAA, vBB, vCC |
| 104 | k22b, // op vAA, vBB, #+CC |
| 105 | k22t, // op vA, vB, +CCCC |
| 106 | k22s, // op vA, vB, #+CCCC |
| 107 | k22c, // op vA, vB, thing@CCCC |
| 108 | k32x, // op vAAAA, vBBBB |
| 109 | k30t, // op +AAAAAAAA |
| 110 | k31t, // op vAA, +BBBBBBBB |
| 111 | k31i, // op vAA, #+BBBBBBBB |
| 112 | k31c, // op vAA, thing@BBBBBBBB |
| 113 | k35c, // op {vC, vD, vE, vF, vG}, thing@BBBB (B: count, A: vG) |
| 114 | k3rc, // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB |
| 115 | k51l, // op vAA, #+BBBBBBBBBBBBBBBB |
| 116 | }; |
| 117 | |
| 118 | enum Flags { |
| 119 | kBranch = 0x01, // conditional or unconditional branch |
| 120 | kContinue = 0x02, // flow can continue to next statement |
| 121 | kSwitch = 0x04, // switch statement |
| 122 | kThrow = 0x08, // could cause an exception to be thrown |
| 123 | kReturn = 0x10, // returns, no additional statements |
| 124 | kInvoke = 0x20, // a flavor of invoke |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 125 | kUnconditional = 0x40, // unconditional branch |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 128 | enum VerifyFlag { |
| 129 | kVerifyNone = 0x00000, |
| 130 | kVerifyRegA = 0x00001, |
| 131 | kVerifyRegAWide = 0x00002, |
| 132 | kVerifyRegB = 0x00004, |
| 133 | kVerifyRegBField = 0x00008, |
| 134 | kVerifyRegBMethod = 0x00010, |
| 135 | kVerifyRegBNewInstance = 0x00020, |
| 136 | kVerifyRegBString = 0x00040, |
| 137 | kVerifyRegBType = 0x00080, |
| 138 | kVerifyRegBWide = 0x00100, |
| 139 | kVerifyRegC = 0x00200, |
| 140 | kVerifyRegCField = 0x00400, |
| 141 | kVerifyRegCNewArray = 0x00800, |
| 142 | kVerifyRegCType = 0x01000, |
| 143 | kVerifyRegCWide = 0x02000, |
| 144 | kVerifyArrayData = 0x04000, |
| 145 | kVerifyBranchTarget = 0x08000, |
| 146 | kVerifySwitchTargets = 0x10000, |
| 147 | kVerifyVarArg = 0x20000, |
| 148 | kVerifyVarArgRange = 0x40000, |
| 149 | kVerifyError = 0x80000, |
| 150 | }; |
| 151 | |
| 152 | // Decodes this instruction, populating its arguments. |
| 153 | void Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const; |
| 154 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 155 | // Returns the size (in 2 byte code units) of this instruction. |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 156 | size_t SizeInCodeUnits() const { |
| 157 | int result = kInstructionSizeInCodeUnits[Opcode()]; |
| 158 | if (UNLIKELY(result < 0)) { |
| 159 | return SizeInCodeUnitsComplexOpcode(); |
| 160 | } else { |
| 161 | return static_cast<size_t>(result); |
| 162 | } |
| 163 | } |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 164 | |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame^] | 165 | // Reads an instruction out of the stream at the specified address. |
| 166 | static const Instruction* At(const uint16_t* code) { |
| 167 | DCHECK(code != NULL); |
| 168 | return reinterpret_cast<const Instruction*>(code); |
| 169 | } |
| 170 | |
| 171 | // Reads an instruction out of the stream from the current address plus an offset. |
| 172 | const Instruction* RelativeAt(int32_t offset) const { |
| 173 | return At(reinterpret_cast<const uint16_t*>(this) + offset); |
| 174 | } |
| 175 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 176 | // Returns a pointer to the next instruction in the stream. |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 177 | const Instruction* Next() const { |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame^] | 178 | return RelativeAt(SizeInCodeUnits()); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 179 | } |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 180 | |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 181 | // Returns a pointer to the instruction after this 1xx instruction in the stream. |
| 182 | const Instruction* Next_1xx() const { |
| 183 | DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t); |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame^] | 184 | return RelativeAt(1); |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Returns a pointer to the instruction after this 2xx instruction in the stream. |
| 188 | const Instruction* Next_2xx() const { |
| 189 | DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k22c); |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame^] | 190 | return RelativeAt(2); |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Returns a pointer to the instruction after this 3xx instruction in the stream. |
| 194 | const Instruction* Next_3xx() const { |
| 195 | DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc); |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame^] | 196 | return RelativeAt(3); |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Returns a pointer to the instruction after this 51l instruction in the stream. |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame^] | 200 | const Instruction* Next_51l() const { |
| 201 | DCHECK(FormatOf(Opcode()) == k51l); |
| 202 | return RelativeAt(5); |
| 203 | } |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 204 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 205 | // Returns the name of this instruction's opcode. |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 206 | const char* Name() const { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 207 | return Instruction::Name(Opcode()); |
| 208 | } |
| 209 | |
| 210 | // Returns the name of the given opcode. |
| 211 | static const char* Name(Code opcode) { |
| 212 | return kInstructionNames[opcode]; |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 213 | } |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 214 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 215 | // VRegA |
| 216 | int8_t VRegA_10t() const; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 217 | uint8_t VRegA_10x() const; |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 218 | uint4_t VRegA_11n() const; |
| 219 | uint8_t VRegA_11x() const; |
| 220 | uint4_t VRegA_12x() const; |
| 221 | int16_t VRegA_20t() const; |
| 222 | uint8_t VRegA_21c() const; |
| 223 | uint8_t VRegA_21h() const; |
| 224 | uint8_t VRegA_21s() const; |
| 225 | uint8_t VRegA_21t() const; |
| 226 | uint8_t VRegA_22b() const; |
| 227 | uint4_t VRegA_22c() const; |
| 228 | uint4_t VRegA_22s() const; |
| 229 | uint4_t VRegA_22t() const; |
| 230 | uint8_t VRegA_22x() const; |
| 231 | uint8_t VRegA_23x() const; |
| 232 | int32_t VRegA_30t() const; |
| 233 | uint8_t VRegA_31c() const; |
| 234 | uint8_t VRegA_31i() const; |
| 235 | uint8_t VRegA_31t() const; |
| 236 | uint16_t VRegA_32x() const; |
| 237 | uint4_t VRegA_35c() const; |
| 238 | uint8_t VRegA_3rc() const; |
| 239 | uint8_t VRegA_51l() const; |
| 240 | |
| 241 | // VRegB |
| 242 | int4_t VRegB_11n() const; |
| 243 | uint4_t VRegB_12x() const; |
| 244 | uint16_t VRegB_21c() const; |
| 245 | uint16_t VRegB_21h() const; |
| 246 | int16_t VRegB_21s() const; |
| 247 | int16_t VRegB_21t() const; |
| 248 | uint8_t VRegB_22b() const; |
| 249 | uint4_t VRegB_22c() const; |
| 250 | uint4_t VRegB_22s() const; |
| 251 | uint4_t VRegB_22t() const; |
| 252 | uint16_t VRegB_22x() const; |
| 253 | uint8_t VRegB_23x() const; |
| 254 | uint32_t VRegB_31c() const; |
| 255 | int32_t VRegB_31i() const; |
| 256 | int32_t VRegB_31t() const; |
| 257 | uint16_t VRegB_32x() const; |
| 258 | uint16_t VRegB_35c() const; |
| 259 | uint16_t VRegB_3rc() const; |
| 260 | uint64_t VRegB_51l() const; // vB_wide |
| 261 | |
| 262 | // VRegC |
| 263 | int8_t VRegC_22b() const; |
| 264 | uint16_t VRegC_22c() const; |
| 265 | int16_t VRegC_22s() const; |
| 266 | int16_t VRegC_22t() const; |
| 267 | uint8_t VRegC_23x() const; |
| 268 | uint4_t VRegC_35c() const; |
| 269 | uint16_t VRegC_3rc() const; |
| 270 | |
| 271 | // Fills the given array with the 'arg' array of the instruction. |
| 272 | void GetArgs(uint32_t args[5]) const; |
| 273 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 274 | // Returns the opcode field of the instruction. |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 275 | Code Opcode() const { |
| 276 | const uint16_t* insns = reinterpret_cast<const uint16_t*>(this); |
| 277 | int opcode = *insns & 0xFF; |
| 278 | return static_cast<Code>(opcode); |
| 279 | } |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 280 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 281 | // Returns the format of the given opcode. |
| 282 | static Format FormatOf(Code opcode) { |
| 283 | return kInstructionFormats[opcode]; |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 286 | // Returns the flags for the given opcode. |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 287 | static int FlagsOf(Code opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 288 | return kInstructionFlags[opcode]; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 291 | // Returns true if this instruction is a branch. |
| 292 | bool IsBranch() const { |
| 293 | return (kInstructionFlags[Opcode()] & kBranch) != 0; |
| 294 | } |
| 295 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 296 | // Returns true if this instruction is a unconditional branch. |
| 297 | bool IsUnconditional() const { |
| 298 | return (kInstructionFlags[Opcode()] & kUnconditional) != 0; |
| 299 | } |
| 300 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 301 | // Returns true if this instruction is a switch. |
| 302 | bool IsSwitch() const { |
| 303 | return (kInstructionFlags[Opcode()] & kSwitch) != 0; |
| 304 | } |
| 305 | |
| 306 | // Returns true if this instruction can throw. |
| 307 | bool IsThrow() const { |
| 308 | return (kInstructionFlags[Opcode()] & kThrow) != 0; |
| 309 | } |
| 310 | |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 311 | // Determine if the instruction is any of 'return' instructions. |
| 312 | bool IsReturn() const { |
| 313 | return (kInstructionFlags[Opcode()] & kReturn) != 0; |
| 314 | } |
| 315 | |
| 316 | // Determine if this instruction ends execution of its basic block. |
| 317 | bool IsBasicBlockEnd() const { |
| 318 | return IsBranch() || IsReturn() || Opcode() == THROW; |
| 319 | } |
| 320 | |
| 321 | // Determine if this instruction is an invoke. |
| 322 | bool IsInvoke() const { |
| 323 | return (kInstructionFlags[Opcode()] & kInvoke) != 0; |
| 324 | } |
| 325 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 326 | int GetVerifyTypeArgumentA() const { |
| 327 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegA | kVerifyRegAWide)); |
| 328 | } |
| 329 | |
| 330 | int GetVerifyTypeArgumentB() const { |
| 331 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegB | kVerifyRegBField | kVerifyRegBMethod | |
| 332 | kVerifyRegBNewInstance | kVerifyRegBString | kVerifyRegBType | kVerifyRegBWide)); |
| 333 | } |
| 334 | |
| 335 | int GetVerifyTypeArgumentC() const { |
| 336 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegC | kVerifyRegCField | |
jeffhao | 3bb3246 | 2012-02-01 16:12:27 -0800 | [diff] [blame] | 337 | kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide)); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | int GetVerifyExtraFlags() const { |
| 341 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyArrayData | kVerifyBranchTarget | |
| 342 | kVerifySwitchTargets | kVerifyVarArg | kVerifyVarArgRange | kVerifyError)); |
| 343 | } |
| 344 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 345 | // Get the dex PC of this instruction as a offset in code units from the beginning of insns. |
| 346 | uint32_t GetDexPc(const uint16_t* insns) const { |
| 347 | return (reinterpret_cast<const uint16_t*>(this) - insns); |
| 348 | } |
| 349 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 350 | // Dump decoded version of instruction |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 351 | std::string DumpString(const DexFile*) const; |
| 352 | |
| 353 | // Dump code_units worth of this instruction, padding to code_units for shorter instructions |
| 354 | std::string DumpHex(size_t code_units) const; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 355 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 356 | private: |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 357 | size_t SizeInCodeUnitsComplexOpcode() const; |
| 358 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 359 | uint16_t Fetch16(size_t offset) const { |
| 360 | const uint16_t* insns = reinterpret_cast<const uint16_t*>(this); |
| 361 | return insns[offset]; |
| 362 | } |
| 363 | |
| 364 | uint32_t Fetch32(size_t offset) const { |
| 365 | return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16)); |
| 366 | } |
| 367 | |
| 368 | uint4_t InstA() const { |
| 369 | return static_cast<uint4_t>((Fetch16(0) >> 8) & 0x0f); |
| 370 | } |
| 371 | |
| 372 | uint4_t InstB() const { |
| 373 | return static_cast<uint4_t>(Fetch16(0) >> 12); |
| 374 | } |
| 375 | |
| 376 | uint8_t InstAA() const { |
| 377 | return static_cast<uint8_t>(Fetch16(0) >> 8); |
| 378 | } |
| 379 | |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 380 | static const char* const kInstructionNames[]; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 381 | static Format const kInstructionFormats[]; |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 382 | static int const kInstructionFlags[]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 383 | static int const kInstructionVerifyFlags[]; |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 384 | static int const kInstructionSizeInCodeUnits[]; |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 385 | DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction); |
| 386 | }; |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 387 | std::ostream& operator<<(std::ostream& os, const Instruction::Code& code); |
| 388 | std::ostream& operator<<(std::ostream& os, const Instruction::Format& format); |
| 389 | std::ostream& operator<<(std::ostream& os, const Instruction::Flags& flags); |
| 390 | std::ostream& operator<<(std::ostream& os, const Instruction::VerifyFlag& vflags); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | * Holds the contents of a decoded instruction. |
| 394 | */ |
| 395 | struct DecodedInstruction { |
| 396 | uint32_t vA; |
| 397 | uint32_t vB; |
| 398 | uint64_t vB_wide; /* for k51l */ |
| 399 | uint32_t vC; |
| 400 | uint32_t arg[5]; /* vC/D/E/F/G in invoke or filled-new-array */ |
| 401 | Instruction::Code opcode; |
| 402 | |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 403 | explicit DecodedInstruction(const Instruction* inst) { |
| 404 | inst->Decode(vA, vB, vB_wide, vC, arg); |
| 405 | opcode = inst->Opcode(); |
| 406 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 407 | }; |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 408 | |
| 409 | } // namespace art |
| 410 | |
| 411 | #endif // ART_SRC_DEX_INSTRUCTION_H_ |