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 */ 16 17 #ifndef ART_LIBDEXFILE_DEX_DEX_INSTRUCTION_H_ 18 #define ART_LIBDEXFILE_DEX_DEX_INSTRUCTION_H_ 19 20 #include <android-base/logging.h> 21 22 #include "base/globals.h" 23 #include "base/macros.h" 24 #include "dex_instruction_list.h" 25 26 using uint4_t = uint8_t; 27 using int4_t = int8_t; 28 29 namespace art { 30 31 class DexFile; 32 33 // The number of Dalvik opcodes. 34 static constexpr size_t kNumPackedOpcodes = 0x100; 35 36 class Instruction { 37 public: 38 // NOP-encoded switch-statement signatures. 39 enum Signatures { 40 kPackedSwitchSignature = 0x0100, 41 kSparseSwitchSignature = 0x0200, 42 kArrayDataSignature = 0x0300, 43 }; 44 45 struct PACKED(4) PackedSwitchPayload { 46 const uint16_t ident; 47 const uint16_t case_count; 48 const int32_t first_key; 49 const int32_t targets[]; 50 51 private: 52 DISALLOW_COPY_AND_ASSIGN(PackedSwitchPayload); 53 }; 54 55 struct PACKED(4) SparseSwitchPayload { 56 const uint16_t ident; 57 const uint16_t case_count; 58 const int32_t keys_and_targets[]; 59 60 public: GetKeysSparseSwitchPayload61 const int32_t* GetKeys() const { 62 return keys_and_targets; 63 } 64 GetTargetsSparseSwitchPayload65 const int32_t* GetTargets() const { 66 return keys_and_targets + case_count; 67 } 68 69 private: 70 DISALLOW_COPY_AND_ASSIGN(SparseSwitchPayload); 71 }; 72 73 struct PACKED(4) ArrayDataPayload { 74 const uint16_t ident; 75 const uint16_t element_width; 76 const uint32_t element_count; 77 const uint8_t data[]; 78 79 private: 80 DISALLOW_COPY_AND_ASSIGN(ArrayDataPayload); 81 }; 82 83 enum Code { // private marker to avoid generate-operator-out.py from processing. 84 #define INSTRUCTION_ENUM(opcode, cname, p, f, i, a, e, v) cname = (opcode), 85 DEX_INSTRUCTION_LIST(INSTRUCTION_ENUM) 86 #undef INSTRUCTION_ENUM 87 RSUB_INT_LIT16 = RSUB_INT, 88 }; 89 90 enum Format : uint8_t { 91 k10x, // op 92 k12x, // op vA, vB 93 k11n, // op vA, #+B 94 k11x, // op vAA 95 k10t, // op +AA 96 k20t, // op +AAAA 97 k22x, // op vAA, vBBBB 98 k21t, // op vAA, +BBBB 99 k21s, // op vAA, #+BBBB 100 k21h, // op vAA, #+BBBB00000[00000000] 101 k21c, // op vAA, thing@BBBB 102 k23x, // op vAA, vBB, vCC 103 k22b, // op vAA, vBB, #+CC 104 k22t, // op vA, vB, +CCCC 105 k22s, // op vA, vB, #+CCCC 106 k22c, // op vA, vB, thing@CCCC 107 k32x, // op vAAAA, vBBBB 108 k30t, // op +AAAAAAAA 109 k31t, // op vAA, +BBBBBBBB 110 k31i, // op vAA, #+BBBBBBBB 111 k31c, // op vAA, thing@BBBBBBBB 112 k35c, // op {vC, vD, vE, vF, vG}, thing@BBBB (B: count, A: vG) 113 k3rc, // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB 114 115 // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH (A: count) 116 // format: AG op BBBB FEDC HHHH 117 k45cc, 118 119 // op {VCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH (AA: count) 120 // format: AA op BBBB CCCC HHHH 121 k4rcc, // op {VCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH (AA: count) 122 123 k51l, // op vAA, #+BBBBBBBBBBBBBBBB 124 kInvalidFormat, 125 }; 126 127 enum IndexType : uint8_t { 128 kIndexUnknown = 0, 129 kIndexNone, // has no index 130 kIndexTypeRef, // type reference index 131 kIndexStringRef, // string reference index 132 kIndexMethodRef, // method reference index 133 kIndexFieldRef, // field reference index 134 kIndexMethodAndProtoRef, // method and a proto reference index (for invoke-polymorphic) 135 kIndexCallSiteRef, // call site reference index 136 kIndexMethodHandleRef, // constant method handle reference index 137 kIndexProtoRef, // prototype reference index 138 }; 139 140 enum Flags : uint8_t { // private marker to avoid generate-operator-out.py from processing. 141 kBranch = 0x01, // conditional or unconditional branch 142 kContinue = 0x02, // flow can continue to next statement 143 kSwitch = 0x04, // switch statement 144 kThrow = 0x08, // could cause an exception to be thrown 145 kReturn = 0x10, // returns, no additional statements 146 kInvoke = 0x20, // a flavor of invoke 147 kUnconditional = 0x40, // unconditional branch 148 kExperimental = 0x80, // is an experimental opcode 149 }; 150 151 // Old flags. Keeping them around in case we might need them again some day. 152 enum ExtendedFlags : uint32_t { 153 kAdd = 0x0000080, // addition 154 kSubtract = 0x0000100, // subtract 155 kMultiply = 0x0000200, // multiply 156 kDivide = 0x0000400, // division 157 kRemainder = 0x0000800, // remainder 158 kAnd = 0x0001000, // and 159 kOr = 0x0002000, // or 160 kXor = 0x0004000, // xor 161 kShl = 0x0008000, // shl 162 kShr = 0x0010000, // shr 163 kUshr = 0x0020000, // ushr 164 kCast = 0x0040000, // cast 165 kStore = 0x0080000, // store opcode 166 kLoad = 0x0100000, // load opcode 167 kClobber = 0x0200000, // clobbers memory in a big way (not just a write) 168 kRegCFieldOrConstant = 0x0400000, // is the third virtual register a field or literal constant (vC) 169 kRegBFieldOrConstant = 0x0800000, // is the second virtual register a field or literal constant (vB) 170 }; 171 172 enum VerifyFlag : uint32_t { // private marker to avoid generate-operator-out.py from processing. 173 kVerifyNothing = 0x0000000, 174 kVerifyRegA = 0x0000001, 175 kVerifyRegAWide = 0x0000002, 176 kVerifyRegB = 0x0000004, 177 kVerifyRegBField = 0x0000008, 178 kVerifyRegBMethod = 0x0000010, 179 kVerifyRegBNewInstance = 0x0000020, 180 kVerifyRegBString = 0x0000040, 181 kVerifyRegBType = 0x0000080, 182 kVerifyRegBWide = 0x0000100, 183 kVerifyRegC = 0x0000200, 184 kVerifyRegCField = 0x0000400, 185 kVerifyRegCNewArray = 0x0000800, 186 kVerifyRegCType = 0x0001000, 187 kVerifyRegCWide = 0x0002000, 188 kVerifyArrayData = 0x0004000, 189 kVerifyBranchTarget = 0x0008000, 190 kVerifySwitchTargets = 0x0010000, 191 kVerifyVarArg = 0x0020000, 192 kVerifyVarArgNonZero = 0x0040000, 193 kVerifyVarArgRange = 0x0080000, 194 kVerifyVarArgRangeNonZero = 0x0100000, 195 kVerifyError = 0x0200000, 196 kVerifyRegHPrototype = 0x0400000, 197 kVerifyRegBCallSite = 0x0800000, 198 kVerifyRegBMethodHandle = 0x1000000, 199 kVerifyRegBPrototype = 0x2000000, 200 }; 201 202 // Collect the enums in a struct for better locality. 203 struct InstructionDescriptor { 204 uint32_t verify_flags; // Set of VerifyFlag. 205 Format format; 206 IndexType index_type; 207 uint8_t flags; // Set of Flags. 208 int8_t size_in_code_units; 209 }; 210 211 static constexpr uint32_t kMaxVarArgRegs = 5; 212 213 static constexpr bool kHaveExperimentalInstructions = false; 214 215 // Returns the size (in 2 byte code units) of this instruction. SizeInCodeUnits()216 size_t SizeInCodeUnits() const { 217 int8_t result = InstructionDescriptorOf(Opcode()).size_in_code_units; 218 if (UNLIKELY(result < 0)) { 219 return SizeInCodeUnitsComplexOpcode(); 220 } else { 221 return static_cast<size_t>(result); 222 } 223 } 224 225 // Returns the size (in 2 byte code units) of the given instruction format. 226 ALWAYS_INLINE static constexpr size_t SizeInCodeUnits(Format format); 227 228 // Code units required to calculate the size of the instruction. CodeUnitsRequiredForSizeComputation()229 size_t CodeUnitsRequiredForSizeComputation() const { 230 const int8_t result = InstructionDescriptorOf(Opcode()).size_in_code_units; 231 return UNLIKELY(result < 0) ? CodeUnitsRequiredForSizeOfComplexOpcode() : 1; 232 } 233 234 // Reads an instruction out of the stream at the specified address. At(const uint16_t * code)235 static const Instruction* At(const uint16_t* code) { 236 DCHECK(code != nullptr); 237 return reinterpret_cast<const Instruction*>(code); 238 } 239 240 // Reads an instruction out of the stream from the current address plus an offset. RelativeAt(int32_t offset)241 const Instruction* RelativeAt(int32_t offset) const WARN_UNUSED { 242 return At(reinterpret_cast<const uint16_t*>(this) + offset); 243 } 244 245 // Returns a pointer to the next instruction in the stream. Next()246 const Instruction* Next() const { 247 return RelativeAt(SizeInCodeUnits()); 248 } 249 250 // Returns a pointer to the instruction after this 1xx instruction in the stream. Next_1xx()251 const Instruction* Next_1xx() const { 252 DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t); 253 return RelativeAt(1); 254 } 255 256 // Returns a pointer to the instruction after this 2xx instruction in the stream. Next_2xx()257 const Instruction* Next_2xx() const { 258 DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k22c); 259 return RelativeAt(2); 260 } 261 262 // Returns a pointer to the instruction after this 3xx instruction in the stream. Next_3xx()263 const Instruction* Next_3xx() const { 264 DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc); 265 return RelativeAt(3); 266 } 267 268 // Returns a pointer to the instruction after this 4xx instruction in the stream. Next_4xx()269 const Instruction* Next_4xx() const { 270 DCHECK(FormatOf(Opcode()) >= k45cc && FormatOf(Opcode()) <= k4rcc); 271 return RelativeAt(4); 272 } 273 274 // Returns a pointer to the instruction after this 51l instruction in the stream. Next_51l()275 const Instruction* Next_51l() const { 276 DCHECK(FormatOf(Opcode()) == k51l); 277 return RelativeAt(5); 278 } 279 280 // Returns the name of this instruction's opcode. Name()281 const char* Name() const { 282 return Instruction::Name(Opcode()); 283 } 284 285 // Returns the name of the given opcode. Name(Code opcode)286 static const char* Name(Code opcode) { 287 return kInstructionNames[opcode]; 288 } 289 290 // VRegA 291 bool HasVRegA() const; 292 ALWAYS_INLINE int32_t VRegA() const; 293 ALWAYS_INLINE int32_t VRegA(Format format, uint16_t inst_data) const; 294 VRegA_10t()295 int8_t VRegA_10t() const { 296 return VRegA_10t(Fetch16(0)); 297 } VRegA_10x()298 uint8_t VRegA_10x() const { 299 return VRegA_10x(Fetch16(0)); 300 } VRegA_11n()301 uint4_t VRegA_11n() const { 302 return VRegA_11n(Fetch16(0)); 303 } VRegA_11x()304 uint8_t VRegA_11x() const { 305 return VRegA_11x(Fetch16(0)); 306 } VRegA_12x()307 uint4_t VRegA_12x() const { 308 return VRegA_12x(Fetch16(0)); 309 } 310 int16_t VRegA_20t() const; VRegA_21c()311 uint8_t VRegA_21c() const { 312 return VRegA_21c(Fetch16(0)); 313 } VRegA_21h()314 uint8_t VRegA_21h() const { 315 return VRegA_21h(Fetch16(0)); 316 } VRegA_21s()317 uint8_t VRegA_21s() const { 318 return VRegA_21s(Fetch16(0)); 319 } VRegA_21t()320 uint8_t VRegA_21t() const { 321 return VRegA_21t(Fetch16(0)); 322 } VRegA_22b()323 uint8_t VRegA_22b() const { 324 return VRegA_22b(Fetch16(0)); 325 } VRegA_22c()326 uint4_t VRegA_22c() const { 327 return VRegA_22c(Fetch16(0)); 328 } VRegA_22s()329 uint4_t VRegA_22s() const { 330 return VRegA_22s(Fetch16(0)); 331 } VRegA_22t()332 uint4_t VRegA_22t() const { 333 return VRegA_22t(Fetch16(0)); 334 } VRegA_22x()335 uint8_t VRegA_22x() const { 336 return VRegA_22x(Fetch16(0)); 337 } VRegA_23x()338 uint8_t VRegA_23x() const { 339 return VRegA_23x(Fetch16(0)); 340 } 341 int32_t VRegA_30t() const; VRegA_31c()342 uint8_t VRegA_31c() const { 343 return VRegA_31c(Fetch16(0)); 344 } VRegA_31i()345 uint8_t VRegA_31i() const { 346 return VRegA_31i(Fetch16(0)); 347 } VRegA_31t()348 uint8_t VRegA_31t() const { 349 return VRegA_31t(Fetch16(0)); 350 } 351 uint16_t VRegA_32x() const; VRegA_35c()352 uint4_t VRegA_35c() const { 353 return VRegA_35c(Fetch16(0)); 354 } VRegA_3rc()355 uint8_t VRegA_3rc() const { 356 return VRegA_3rc(Fetch16(0)); 357 } VRegA_51l()358 uint8_t VRegA_51l() const { 359 return VRegA_51l(Fetch16(0)); 360 } VRegA_45cc()361 uint4_t VRegA_45cc() const { 362 return VRegA_45cc(Fetch16(0)); 363 } VRegA_4rcc()364 uint8_t VRegA_4rcc() const { 365 return VRegA_4rcc(Fetch16(0)); 366 } 367 368 // The following methods return the vA operand for various instruction formats. The "inst_data" 369 // parameter holds the first 16 bits of instruction which the returned value is decoded from. 370 int8_t VRegA_10t(uint16_t inst_data) const; 371 uint8_t VRegA_10x(uint16_t inst_data) const; 372 uint4_t VRegA_11n(uint16_t inst_data) const; 373 uint8_t VRegA_11x(uint16_t inst_data) const; 374 uint4_t VRegA_12x(uint16_t inst_data) const; 375 uint8_t VRegA_21c(uint16_t inst_data) const; 376 uint8_t VRegA_21h(uint16_t inst_data) const; 377 uint8_t VRegA_21s(uint16_t inst_data) const; 378 uint8_t VRegA_21t(uint16_t inst_data) const; 379 uint8_t VRegA_22b(uint16_t inst_data) const; 380 uint4_t VRegA_22c(uint16_t inst_data) const; 381 uint4_t VRegA_22s(uint16_t inst_data) const; 382 uint4_t VRegA_22t(uint16_t inst_data) const; 383 uint8_t VRegA_22x(uint16_t inst_data) const; 384 uint8_t VRegA_23x(uint16_t inst_data) const; 385 uint8_t VRegA_31c(uint16_t inst_data) const; 386 uint8_t VRegA_31i(uint16_t inst_data) const; 387 uint8_t VRegA_31t(uint16_t inst_data) const; 388 uint4_t VRegA_35c(uint16_t inst_data) const; 389 uint8_t VRegA_3rc(uint16_t inst_data) const; 390 uint8_t VRegA_51l(uint16_t inst_data) const; 391 uint4_t VRegA_45cc(uint16_t inst_data) const; 392 uint8_t VRegA_4rcc(uint16_t inst_data) const; 393 394 // VRegB 395 bool HasVRegB() const; 396 ALWAYS_INLINE int32_t VRegB() const; 397 ALWAYS_INLINE int32_t VRegB(Format format, uint16_t inst_data) const; 398 399 bool HasWideVRegB() const; 400 uint64_t WideVRegB() const; 401 VRegB_11n()402 int4_t VRegB_11n() const { 403 return VRegB_11n(Fetch16(0)); 404 } VRegB_12x()405 uint4_t VRegB_12x() const { 406 return VRegB_12x(Fetch16(0)); 407 } 408 uint16_t VRegB_21c() const; 409 uint16_t VRegB_21h() const; 410 int16_t VRegB_21s() const; 411 int16_t VRegB_21t() const; 412 uint8_t VRegB_22b() const; VRegB_22c()413 uint4_t VRegB_22c() const { 414 return VRegB_22c(Fetch16(0)); 415 } VRegB_22s()416 uint4_t VRegB_22s() const { 417 return VRegB_22s(Fetch16(0)); 418 } VRegB_22t()419 uint4_t VRegB_22t() const { 420 return VRegB_22t(Fetch16(0)); 421 } 422 uint16_t VRegB_22x() const; 423 uint8_t VRegB_23x() const; 424 uint32_t VRegB_31c() const; 425 int32_t VRegB_31i() const; 426 int32_t VRegB_31t() const; 427 uint16_t VRegB_32x() const; 428 uint16_t VRegB_35c() const; 429 uint16_t VRegB_3rc() const; 430 uint64_t VRegB_51l() const; // vB_wide 431 uint16_t VRegB_45cc() const; 432 uint16_t VRegB_4rcc() const; 433 434 // The following methods return the vB operand for all instruction formats where it is encoded in 435 // the first 16 bits of instruction. The "inst_data" parameter holds these 16 bits. The returned 436 // value is decoded from it. 437 int4_t VRegB_11n(uint16_t inst_data) const; 438 uint4_t VRegB_12x(uint16_t inst_data) const; 439 uint4_t VRegB_22c(uint16_t inst_data) const; 440 uint4_t VRegB_22s(uint16_t inst_data) const; 441 uint4_t VRegB_22t(uint16_t inst_data) const; 442 443 // VRegC 444 bool HasVRegC() const; 445 ALWAYS_INLINE int32_t VRegC() const; 446 ALWAYS_INLINE int32_t VRegC(Format format) const; 447 448 int8_t VRegC_22b() const; 449 uint16_t VRegC_22c() const; 450 int16_t VRegC_22s() const; 451 int16_t VRegC_22t() const; 452 uint8_t VRegC_23x() const; 453 uint4_t VRegC_35c() const; 454 uint16_t VRegC_3rc() const; 455 uint4_t VRegC_45cc() const; 456 uint16_t VRegC_4rcc() const; 457 458 459 // VRegH 460 bool HasVRegH() const; 461 int32_t VRegH() const; 462 ALWAYS_INLINE int32_t VRegH(Format format) const; 463 uint16_t VRegH_45cc() const; 464 uint16_t VRegH_4rcc() const; 465 466 // Fills the given array with the 'arg' array of the instruction. 467 bool HasVarArgs() const; 468 uint32_t GetVarArgs(uint32_t args[kMaxVarArgRegs], uint16_t inst_data) const; GetVarArgs(uint32_t args[kMaxVarArgRegs])469 uint32_t GetVarArgs(uint32_t args[kMaxVarArgRegs]) const { 470 return GetVarArgs(args, Fetch16(0)); 471 } 472 473 // Returns the opcode field of the instruction. The given "inst_data" parameter must be the first 474 // 16 bits of instruction. Opcode(uint16_t inst_data)475 Code Opcode(uint16_t inst_data) const { 476 DCHECK_EQ(inst_data, Fetch16(0)); 477 return static_cast<Code>(inst_data & 0xFF); 478 } 479 480 // Returns the opcode field of the instruction from the first 16 bits of instruction. Opcode()481 Code Opcode() const { 482 return Opcode(Fetch16(0)); 483 } 484 SetOpcode(Code opcode)485 void SetOpcode(Code opcode) { 486 DCHECK_LT(static_cast<uint16_t>(opcode), 256u); 487 uint16_t* insns = reinterpret_cast<uint16_t*>(this); 488 insns[0] = (insns[0] & 0xff00) | static_cast<uint16_t>(opcode); 489 } 490 SetVRegA_10x(uint8_t val)491 void SetVRegA_10x(uint8_t val) { 492 DCHECK(FormatOf(Opcode()) == k10x); 493 uint16_t* insns = reinterpret_cast<uint16_t*>(this); 494 insns[0] = (val << 8) | (insns[0] & 0x00ff); 495 } 496 SetVRegB_3rc(uint16_t val)497 void SetVRegB_3rc(uint16_t val) { 498 DCHECK(FormatOf(Opcode()) == k3rc); 499 uint16_t* insns = reinterpret_cast<uint16_t*>(this); 500 insns[1] = val; 501 } 502 SetVRegB_35c(uint16_t val)503 void SetVRegB_35c(uint16_t val) { 504 DCHECK(FormatOf(Opcode()) == k35c); 505 uint16_t* insns = reinterpret_cast<uint16_t*>(this); 506 insns[1] = val; 507 } 508 SetVRegC_22c(uint16_t val)509 void SetVRegC_22c(uint16_t val) { 510 DCHECK(FormatOf(Opcode()) == k22c); 511 uint16_t* insns = reinterpret_cast<uint16_t*>(this); 512 insns[1] = val; 513 } 514 SetVRegA_21c(uint8_t val)515 void SetVRegA_21c(uint8_t val) { 516 DCHECK(FormatOf(Opcode()) == k21c); 517 uint16_t* insns = reinterpret_cast<uint16_t*>(this); 518 insns[0] = (val << 8) | (insns[0] & 0x00ff); 519 } 520 SetVRegB_21c(uint16_t val)521 void SetVRegB_21c(uint16_t val) { 522 DCHECK(FormatOf(Opcode()) == k21c); 523 uint16_t* insns = reinterpret_cast<uint16_t*>(this); 524 insns[1] = val; 525 } 526 527 // Returns the format of the given opcode. FormatOf(Code opcode)528 static constexpr Format FormatOf(Code opcode) { 529 return InstructionDescriptorOf(opcode).format; 530 } 531 532 // Returns the index type of the given opcode. IndexTypeOf(Code opcode)533 static constexpr IndexType IndexTypeOf(Code opcode) { 534 return InstructionDescriptorOf(opcode).index_type; 535 } 536 537 // Returns the flags for the given opcode. FlagsOf(Code opcode)538 static constexpr uint8_t FlagsOf(Code opcode) { 539 return InstructionDescriptorOf(opcode).flags; 540 } 541 542 // Return the verify flags for the given opcode. VerifyFlagsOf(Code opcode)543 static constexpr uint32_t VerifyFlagsOf(Code opcode) { 544 return InstructionDescriptorOf(opcode).verify_flags; 545 } 546 547 // Returns true if this instruction is a branch. IsBranch()548 bool IsBranch() const { 549 return (InstructionDescriptorOf(Opcode()).flags & kBranch) != 0; 550 } 551 552 // Returns true if this instruction is a unconditional branch. IsUnconditional()553 bool IsUnconditional() const { 554 return (InstructionDescriptorOf(Opcode()).flags & kUnconditional) != 0; 555 } 556 557 // Returns the branch offset if this instruction is a branch. 558 int32_t GetTargetOffset() const; 559 560 // Returns true if the instruction allows control flow to go to the following instruction. 561 bool CanFlowThrough() const; 562 563 // Returns true if this instruction is a switch. IsSwitch()564 bool IsSwitch() const { 565 return (InstructionDescriptorOf(Opcode()).flags & kSwitch) != 0; 566 } 567 568 // Returns true if this instruction can throw. IsThrow()569 bool IsThrow() const { 570 return (InstructionDescriptorOf(Opcode()).flags & kThrow) != 0; 571 } 572 573 // Determine if the instruction is any of 'return' instructions. IsReturn(Code opcode)574 static constexpr bool IsReturn(Code opcode) { 575 return (InstructionDescriptorOf(opcode).flags & kReturn) != 0; 576 } IsReturn()577 bool IsReturn() const { 578 return IsReturn(Opcode()); 579 } 580 581 // Determine if this instruction ends execution of its basic block. IsBasicBlockEnd()582 bool IsBasicBlockEnd() const { 583 return IsBranch() || IsReturn() || Opcode() == THROW; 584 } 585 586 // Determine if this instruction is an invoke. IsInvoke()587 bool IsInvoke() const { 588 return (InstructionDescriptorOf(Opcode()).flags & kInvoke) != 0; 589 } 590 591 // Determine if this instruction is experimental. IsExperimental()592 bool IsExperimental() const { 593 return (InstructionDescriptorOf(Opcode()).flags & kExperimental) != 0; 594 } 595 GetVerifyTypeArgumentAOf(Code opcode)596 static constexpr uint32_t GetVerifyTypeArgumentAOf(Code opcode) { 597 constexpr uint32_t kMask = kVerifyRegA | kVerifyRegAWide; 598 return VerifyFlagsOf(opcode) & kMask; 599 } 600 GetVerifyTypeArgumentA()601 uint32_t GetVerifyTypeArgumentA() const { 602 return GetVerifyTypeArgumentAOf(Opcode()); 603 } 604 GetVerifyTypeArgumentBOf(Code opcode)605 static constexpr uint32_t GetVerifyTypeArgumentBOf(Code opcode) { 606 constexpr uint32_t kMask = 607 kVerifyRegB | 608 kVerifyRegBField | 609 kVerifyRegBMethod | 610 kVerifyRegBNewInstance | 611 kVerifyRegBString | 612 kVerifyRegBType | 613 kVerifyRegBWide; 614 return VerifyFlagsOf(opcode) & kMask; 615 } 616 GetVerifyTypeArgumentB()617 uint32_t GetVerifyTypeArgumentB() const { 618 return GetVerifyTypeArgumentBOf(Opcode()); 619 } 620 GetVerifyTypeArgumentCOf(Code opcode)621 static constexpr uint32_t GetVerifyTypeArgumentCOf(Code opcode) { 622 constexpr uint32_t kMask = 623 kVerifyRegC | kVerifyRegCField | kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide; 624 return VerifyFlagsOf(opcode) & kMask; 625 } 626 GetVerifyTypeArgumentC()627 uint32_t GetVerifyTypeArgumentC() const { 628 return GetVerifyTypeArgumentCOf(Opcode()); 629 } 630 GetVerifyTypeArgumentHOf(Code opcode)631 static constexpr uint32_t GetVerifyTypeArgumentHOf(Code opcode) { 632 constexpr uint32_t kMask = kVerifyRegHPrototype; 633 return VerifyFlagsOf(opcode) & kMask; 634 } 635 GetVerifyTypeArgumentH()636 uint32_t GetVerifyTypeArgumentH() const { 637 return GetVerifyTypeArgumentHOf(Opcode()); 638 } 639 GetVerifyExtraFlagsOf(Code opcode)640 static constexpr uint32_t GetVerifyExtraFlagsOf(Code opcode) { 641 constexpr uint32_t kMask = 642 kVerifyArrayData | 643 kVerifyBranchTarget | 644 kVerifySwitchTargets | 645 kVerifyVarArg | 646 kVerifyVarArgNonZero | 647 kVerifyVarArgRange | 648 kVerifyVarArgRangeNonZero | 649 kVerifyError; 650 return VerifyFlagsOf(opcode) & kMask; 651 } 652 GetVerifyExtraFlags()653 uint32_t GetVerifyExtraFlags() const { 654 return GetVerifyExtraFlagsOf(Opcode()); 655 } 656 657 // Get the dex PC of this instruction as a offset in code units from the beginning of insns. GetDexPc(const uint16_t * insns)658 uint32_t GetDexPc(const uint16_t* insns) const { 659 return (reinterpret_cast<const uint16_t*>(this) - insns); 660 } 661 662 // Dump decoded version of instruction 663 std::string DumpString(const DexFile*) const; 664 665 // Dump code_units worth of this instruction, padding to code_units for shorter instructions 666 std::string DumpHex(size_t code_units) const; 667 668 // Little-endian dump code_units worth of this instruction, padding to code_units for 669 // shorter instructions 670 std::string DumpHexLE(size_t instr_code_units) const; 671 Fetch16(size_t offset)672 uint16_t Fetch16(size_t offset) const { 673 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this); 674 return insns[offset]; 675 } 676 677 size_t SizeInCodeUnitsComplexOpcode() const; 678 679 private: InstructionDescriptorOf(Code opcode)680 static constexpr const InstructionDescriptor& InstructionDescriptorOf(Code opcode) { 681 return kInstructionDescriptors[opcode]; 682 } 683 684 // Return how many code unit words are required to compute the size of the opcode. 685 size_t CodeUnitsRequiredForSizeOfComplexOpcode() const; 686 Fetch32(size_t offset)687 uint32_t Fetch32(size_t offset) const { 688 return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16)); 689 } 690 InstA()691 uint4_t InstA() const { 692 return InstA(Fetch16(0)); 693 } 694 InstB()695 uint4_t InstB() const { 696 return InstB(Fetch16(0)); 697 } 698 InstAA()699 uint8_t InstAA() const { 700 return InstAA(Fetch16(0)); 701 } 702 InstA(uint16_t inst_data)703 uint4_t InstA(uint16_t inst_data) const { 704 DCHECK_EQ(inst_data, Fetch16(0)); 705 return static_cast<uint4_t>((inst_data >> 8) & 0x0f); 706 } 707 InstB(uint16_t inst_data)708 uint4_t InstB(uint16_t inst_data) const { 709 DCHECK_EQ(inst_data, Fetch16(0)); 710 return static_cast<uint4_t>(inst_data >> 12); 711 } 712 InstAA(uint16_t inst_data)713 uint8_t InstAA(uint16_t inst_data) const { 714 DCHECK_EQ(inst_data, Fetch16(0)); 715 return static_cast<uint8_t>(inst_data >> 8); 716 } 717 718 static const char* const kInstructionNames[]; 719 720 static constexpr std::array<InstructionDescriptor, 256> kInstructionDescriptors = []() constexpr { 721 auto InstructionSizeInCodeUnitsByOpcode = [](Instruction::Code opcode, 722 Instruction::Format format) constexpr -> int8_t { 723 if (opcode == Instruction::Code::NOP) { 724 return -1; 725 } else if ((format >= Instruction::Format::k10x) && (format <= Instruction::Format::k10t)) { 726 return 1; 727 } else if ((format >= Instruction::Format::k20t) && (format <= Instruction::Format::k22c)) { 728 return 2; 729 } else if ((format >= Instruction::Format::k32x) && (format <= Instruction::Format::k3rc)) { 730 return 3; 731 } else if ((format >= Instruction::Format::k45cc) && (format <= Instruction::Format::k4rcc)) { 732 return 4; 733 } else if (format == Instruction::Format::k51l) { 734 return 5; 735 } else { 736 return -1; 737 } 738 }; 739 740 std::array<InstructionDescriptor, 256> result; 741 #define INSTRUCTION_DESCR(opcode, c, p, format, index, flags, eflags, vflags) \ 742 result[opcode] = { \ 743 vflags, \ 744 format, \ 745 index, \ 746 flags, \ 747 InstructionSizeInCodeUnitsByOpcode((c), (format)), \ 748 }; 749 DEX_INSTRUCTION_LIST(INSTRUCTION_DESCR) 750 #undef INSTRUCTION_DESCR 751 return result; 752 }(); 753 754 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction); 755 }; 756 std::ostream& operator<<(std::ostream& os, Instruction::Code code); 757 std::ostream& operator<<(std::ostream& os, Instruction::Format format); 758 759 // Base class for accessing instruction operands. Unifies operand 760 // access for instructions that have range and varargs forms 761 // (e.g. invoke-polymoprhic/range and invoke-polymorphic). 762 class InstructionOperands { 763 public: InstructionOperands(size_t num_operands)764 explicit InstructionOperands(size_t num_operands) : num_operands_(num_operands) {} ~InstructionOperands()765 virtual ~InstructionOperands() {} 766 virtual uint32_t GetOperand(size_t index) const = 0; GetNumberOfOperands()767 size_t GetNumberOfOperands() const { return num_operands_; } 768 769 private: 770 const size_t num_operands_; 771 772 DISALLOW_IMPLICIT_CONSTRUCTORS(InstructionOperands); 773 }; 774 775 // Class for accessing operands for instructions with a range format 776 // (e.g. 3rc and 4rcc). 777 class RangeInstructionOperands final : public InstructionOperands { 778 public: RangeInstructionOperands(uint32_t first_operand,size_t num_operands)779 RangeInstructionOperands(uint32_t first_operand, size_t num_operands) 780 : InstructionOperands(num_operands), first_operand_(first_operand) {} ~RangeInstructionOperands()781 ~RangeInstructionOperands() {} 782 uint32_t GetOperand(size_t operand_index) const override; 783 784 private: 785 const uint32_t first_operand_; 786 787 DISALLOW_IMPLICIT_CONSTRUCTORS(RangeInstructionOperands); 788 }; 789 790 // Class for accessing operands for instructions with a variable 791 // number of arguments format (e.g. 35c and 45cc). 792 class VarArgsInstructionOperands final : public InstructionOperands { 793 public: VarArgsInstructionOperands(const uint32_t (& operands)[Instruction::kMaxVarArgRegs],size_t num_operands)794 VarArgsInstructionOperands(const uint32_t (&operands)[Instruction::kMaxVarArgRegs], 795 size_t num_operands) 796 : InstructionOperands(num_operands), operands_(operands) {} ~VarArgsInstructionOperands()797 ~VarArgsInstructionOperands() {} 798 uint32_t GetOperand(size_t operand_index) const override; 799 800 private: 801 const uint32_t (&operands_)[Instruction::kMaxVarArgRegs]; 802 803 DISALLOW_IMPLICIT_CONSTRUCTORS(VarArgsInstructionOperands); 804 }; 805 806 // Class for accessing operands without the receiver by wrapping an 807 // existing InstructionOperands instance. 808 class NoReceiverInstructionOperands final : public InstructionOperands { 809 public: NoReceiverInstructionOperands(const InstructionOperands * const inner)810 explicit NoReceiverInstructionOperands(const InstructionOperands* const inner) 811 : InstructionOperands(inner->GetNumberOfOperands() - 1), inner_(inner) {} ~NoReceiverInstructionOperands()812 ~NoReceiverInstructionOperands() {} 813 uint32_t GetOperand(size_t operand_index) const override; 814 815 private: 816 const InstructionOperands* const inner_; 817 818 DISALLOW_IMPLICIT_CONSTRUCTORS(NoReceiverInstructionOperands); 819 }; 820 821 } // namespace art 822 823 #endif // ART_LIBDEXFILE_DEX_DEX_INSTRUCTION_H_ 824