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