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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_INSTRUCTION_H_ |
| 18 | #define ART_RUNTIME_DEX_INSTRUCTION_H_ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 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[]; |
Dragos Sbirlea | 39f9927 | 2013-06-25 13:17:36 -0700 | [diff] [blame] | 49 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 50 | private: |
| 51 | DISALLOW_COPY_AND_ASSIGN(PackedSwitchPayload); |
| 52 | }; |
| 53 | |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 54 | struct PACKED(4) SparseSwitchPayload { |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 55 | const uint16_t ident; |
| 56 | const uint16_t case_count; |
| 57 | const int32_t keys_and_targets[]; |
| 58 | |
| 59 | public: |
| 60 | const int32_t* GetKeys() const { |
| 61 | return keys_and_targets; |
| 62 | } |
| 63 | |
| 64 | const int32_t* GetTargets() const { |
| 65 | return keys_and_targets + case_count; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | DISALLOW_COPY_AND_ASSIGN(SparseSwitchPayload); |
| 70 | }; |
| 71 | |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 72 | struct PACKED(4) ArrayDataPayload { |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 73 | const uint16_t ident; |
| 74 | const uint16_t element_width; |
| 75 | const uint32_t element_count; |
| 76 | const uint8_t data[]; |
Dragos Sbirlea | 39f9927 | 2013-06-25 13:17:36 -0700 | [diff] [blame] | 77 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 78 | private: |
| 79 | DISALLOW_COPY_AND_ASSIGN(ArrayDataPayload); |
| 80 | }; |
| 81 | |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 82 | // TODO: the code layout below is deliberate to avoid this enum being picked up by |
| 83 | // generate-operator-out.py. |
| 84 | enum Code |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 85 | { // NOLINT(whitespace/braces) |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 86 | #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] | 87 | #include "dex_instruction_list.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 88 | DEX_INSTRUCTION_LIST(INSTRUCTION_ENUM) |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 89 | #undef DEX_INSTRUCTION_LIST |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 90 | #undef INSTRUCTION_ENUM |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 91 | }; |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 92 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 93 | enum Format { |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 94 | k10x, // op |
| 95 | k12x, // op vA, vB |
| 96 | k11n, // op vA, #+B |
| 97 | k11x, // op vAA |
| 98 | k10t, // op +AA |
| 99 | k20t, // op +AAAA |
| 100 | k22x, // op vAA, vBBBB |
| 101 | k21t, // op vAA, +BBBB |
| 102 | k21s, // op vAA, #+BBBB |
| 103 | k21h, // op vAA, #+BBBB00000[00000000] |
| 104 | k21c, // op vAA, thing@BBBB |
| 105 | k23x, // op vAA, vBB, vCC |
| 106 | k22b, // op vAA, vBB, #+CC |
| 107 | k22t, // op vA, vB, +CCCC |
| 108 | k22s, // op vA, vB, #+CCCC |
| 109 | k22c, // op vA, vB, thing@CCCC |
| 110 | k32x, // op vAAAA, vBBBB |
| 111 | k30t, // op +AAAAAAAA |
| 112 | k31t, // op vAA, +BBBBBBBB |
| 113 | k31i, // op vAA, #+BBBBBBBB |
| 114 | k31c, // op vAA, thing@BBBBBBBB |
| 115 | k35c, // op {vC, vD, vE, vF, vG}, thing@BBBB (B: count, A: vG) |
| 116 | k3rc, // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB |
| 117 | k51l, // op vAA, #+BBBBBBBBBBBBBBBB |
| 118 | }; |
| 119 | |
| 120 | enum Flags { |
| 121 | kBranch = 0x01, // conditional or unconditional branch |
| 122 | kContinue = 0x02, // flow can continue to next statement |
| 123 | kSwitch = 0x04, // switch statement |
| 124 | kThrow = 0x08, // could cause an exception to be thrown |
| 125 | kReturn = 0x10, // returns, no additional statements |
| 126 | kInvoke = 0x20, // a flavor of invoke |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 127 | kUnconditional = 0x40, // unconditional branch |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 130 | enum VerifyFlag { |
| 131 | kVerifyNone = 0x00000, |
| 132 | kVerifyRegA = 0x00001, |
| 133 | kVerifyRegAWide = 0x00002, |
| 134 | kVerifyRegB = 0x00004, |
| 135 | kVerifyRegBField = 0x00008, |
| 136 | kVerifyRegBMethod = 0x00010, |
| 137 | kVerifyRegBNewInstance = 0x00020, |
| 138 | kVerifyRegBString = 0x00040, |
| 139 | kVerifyRegBType = 0x00080, |
| 140 | kVerifyRegBWide = 0x00100, |
| 141 | kVerifyRegC = 0x00200, |
| 142 | kVerifyRegCField = 0x00400, |
| 143 | kVerifyRegCNewArray = 0x00800, |
| 144 | kVerifyRegCType = 0x01000, |
| 145 | kVerifyRegCWide = 0x02000, |
| 146 | kVerifyArrayData = 0x04000, |
| 147 | kVerifyBranchTarget = 0x08000, |
| 148 | kVerifySwitchTargets = 0x10000, |
| 149 | kVerifyVarArg = 0x20000, |
| 150 | kVerifyVarArgRange = 0x40000, |
| 151 | kVerifyError = 0x80000, |
| 152 | }; |
| 153 | |
| 154 | // Decodes this instruction, populating its arguments. |
| 155 | void Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const; |
| 156 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 157 | // Returns the size (in 2 byte code units) of this instruction. |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 158 | size_t SizeInCodeUnits() const { |
| 159 | int result = kInstructionSizeInCodeUnits[Opcode()]; |
| 160 | if (UNLIKELY(result < 0)) { |
| 161 | return SizeInCodeUnitsComplexOpcode(); |
| 162 | } else { |
| 163 | return static_cast<size_t>(result); |
| 164 | } |
| 165 | } |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 166 | |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame] | 167 | // Reads an instruction out of the stream at the specified address. |
| 168 | static const Instruction* At(const uint16_t* code) { |
| 169 | DCHECK(code != NULL); |
| 170 | return reinterpret_cast<const Instruction*>(code); |
| 171 | } |
| 172 | |
| 173 | // Reads an instruction out of the stream from the current address plus an offset. |
| 174 | const Instruction* RelativeAt(int32_t offset) const { |
| 175 | return At(reinterpret_cast<const uint16_t*>(this) + offset); |
| 176 | } |
| 177 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 178 | // Returns a pointer to the next instruction in the stream. |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 179 | const Instruction* Next() const { |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame] | 180 | return RelativeAt(SizeInCodeUnits()); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 181 | } |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 182 | |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 183 | // Returns a pointer to the instruction after this 1xx instruction in the stream. |
| 184 | const Instruction* Next_1xx() const { |
| 185 | DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t); |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame] | 186 | return RelativeAt(1); |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Returns a pointer to the instruction after this 2xx instruction in the stream. |
| 190 | const Instruction* Next_2xx() const { |
| 191 | DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k22c); |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame] | 192 | return RelativeAt(2); |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Returns a pointer to the instruction after this 3xx instruction in the stream. |
| 196 | const Instruction* Next_3xx() const { |
| 197 | DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc); |
Sebastien Hertz | 92c607f | 2013-06-04 16:18:52 +0200 | [diff] [blame] | 198 | return RelativeAt(3); |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // 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] | 202 | const Instruction* Next_51l() const { |
| 203 | DCHECK(FormatOf(Opcode()) == k51l); |
| 204 | return RelativeAt(5); |
| 205 | } |
Jeff Hao | 9cec247 | 2013-05-14 18:17:06 -0700 | [diff] [blame] | 206 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 207 | // Returns the name of this instruction's opcode. |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 208 | const char* Name() const { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 209 | return Instruction::Name(Opcode()); |
| 210 | } |
| 211 | |
| 212 | // Returns the name of the given opcode. |
| 213 | static const char* Name(Code opcode) { |
| 214 | return kInstructionNames[opcode]; |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 215 | } |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 216 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 217 | // VRegA |
Dragos Sbirlea | 8cc5162 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 218 | bool HasVRegA() const; |
Dragos Sbirlea | d25de7a | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 219 | int32_t VRegA() const; |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 220 | |
| 221 | int8_t VRegA_10t() const { |
| 222 | return VRegA_10t(Fetch16(0)); |
| 223 | } |
| 224 | uint8_t VRegA_10x() const { |
| 225 | return VRegA_10x(Fetch16(0)); |
| 226 | } |
| 227 | uint4_t VRegA_11n() const { |
| 228 | return VRegA_11n(Fetch16(0)); |
| 229 | } |
| 230 | uint8_t VRegA_11x() const { |
| 231 | return VRegA_11x(Fetch16(0)); |
| 232 | } |
| 233 | uint4_t VRegA_12x() const { |
| 234 | return VRegA_12x(Fetch16(0)); |
| 235 | } |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 236 | int16_t VRegA_20t() const; |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 237 | uint8_t VRegA_21c() const { |
| 238 | return VRegA_21c(Fetch16(0)); |
| 239 | } |
| 240 | uint8_t VRegA_21h() const { |
| 241 | return VRegA_21h(Fetch16(0)); |
| 242 | } |
| 243 | uint8_t VRegA_21s() const { |
| 244 | return VRegA_21s(Fetch16(0)); |
| 245 | } |
| 246 | uint8_t VRegA_21t() const { |
| 247 | return VRegA_21t(Fetch16(0)); |
| 248 | } |
| 249 | uint8_t VRegA_22b() const { |
| 250 | return VRegA_22b(Fetch16(0)); |
| 251 | } |
| 252 | uint4_t VRegA_22c() const { |
| 253 | return VRegA_22c(Fetch16(0)); |
| 254 | } |
| 255 | uint4_t VRegA_22s() const { |
| 256 | return VRegA_22s(Fetch16(0)); |
| 257 | } |
| 258 | uint4_t VRegA_22t() const { |
| 259 | return VRegA_22t(Fetch16(0)); |
| 260 | } |
| 261 | uint8_t VRegA_22x() const { |
| 262 | return VRegA_22x(Fetch16(0)); |
| 263 | } |
| 264 | uint8_t VRegA_23x() const { |
| 265 | return VRegA_23x(Fetch16(0)); |
| 266 | } |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 267 | int32_t VRegA_30t() const; |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 268 | uint8_t VRegA_31c() const { |
| 269 | return VRegA_31c(Fetch16(0)); |
| 270 | } |
| 271 | uint8_t VRegA_31i() const { |
| 272 | return VRegA_31i(Fetch16(0)); |
| 273 | } |
| 274 | uint8_t VRegA_31t() const { |
| 275 | return VRegA_31t(Fetch16(0)); |
| 276 | } |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 277 | uint16_t VRegA_32x() const; |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 278 | uint4_t VRegA_35c() const { |
| 279 | return VRegA_35c(Fetch16(0)); |
| 280 | } |
| 281 | uint8_t VRegA_3rc() const { |
| 282 | return VRegA_3rc(Fetch16(0)); |
| 283 | } |
| 284 | uint8_t VRegA_51l() const { |
| 285 | return VRegA_51l(Fetch16(0)); |
| 286 | } |
| 287 | |
| 288 | // The following methods return the vA operand for various instruction formats. The "inst_data" |
| 289 | // parameter holds the first 16 bits of instruction which the returned value is decoded from. |
| 290 | int8_t VRegA_10t(uint16_t inst_data) const; |
| 291 | uint8_t VRegA_10x(uint16_t inst_data) const; |
| 292 | uint4_t VRegA_11n(uint16_t inst_data) const; |
| 293 | uint8_t VRegA_11x(uint16_t inst_data) const; |
| 294 | uint4_t VRegA_12x(uint16_t inst_data) const; |
| 295 | uint8_t VRegA_21c(uint16_t inst_data) const; |
| 296 | uint8_t VRegA_21h(uint16_t inst_data) const; |
| 297 | uint8_t VRegA_21s(uint16_t inst_data) const; |
| 298 | uint8_t VRegA_21t(uint16_t inst_data) const; |
| 299 | uint8_t VRegA_22b(uint16_t inst_data) const; |
| 300 | uint4_t VRegA_22c(uint16_t inst_data) const; |
| 301 | uint4_t VRegA_22s(uint16_t inst_data) const; |
| 302 | uint4_t VRegA_22t(uint16_t inst_data) const; |
| 303 | uint8_t VRegA_22x(uint16_t inst_data) const; |
| 304 | uint8_t VRegA_23x(uint16_t inst_data) const; |
| 305 | uint8_t VRegA_31c(uint16_t inst_data) const; |
| 306 | uint8_t VRegA_31i(uint16_t inst_data) const; |
| 307 | uint8_t VRegA_31t(uint16_t inst_data) const; |
| 308 | uint4_t VRegA_35c(uint16_t inst_data) const; |
| 309 | uint8_t VRegA_3rc(uint16_t inst_data) const; |
| 310 | uint8_t VRegA_51l(uint16_t inst_data) const; |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 311 | |
| 312 | // VRegB |
Dragos Sbirlea | 8cc5162 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 313 | bool HasVRegB() const; |
Dragos Sbirlea | 39f9927 | 2013-06-25 13:17:36 -0700 | [diff] [blame] | 314 | int32_t VRegB() const; |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 315 | |
| 316 | int4_t VRegB_11n() const { |
| 317 | return VRegB_11n(Fetch16(0)); |
| 318 | } |
| 319 | uint4_t VRegB_12x() const { |
| 320 | return VRegB_12x(Fetch16(0)); |
| 321 | } |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 322 | uint16_t VRegB_21c() const; |
| 323 | uint16_t VRegB_21h() const; |
| 324 | int16_t VRegB_21s() const; |
| 325 | int16_t VRegB_21t() const; |
| 326 | uint8_t VRegB_22b() const; |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 327 | uint4_t VRegB_22c() const { |
| 328 | return VRegB_22c(Fetch16(0)); |
| 329 | } |
| 330 | uint4_t VRegB_22s() const { |
| 331 | return VRegB_22s(Fetch16(0)); |
| 332 | } |
| 333 | uint4_t VRegB_22t() const { |
| 334 | return VRegB_22t(Fetch16(0)); |
| 335 | } |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 336 | uint16_t VRegB_22x() const; |
| 337 | uint8_t VRegB_23x() const; |
| 338 | uint32_t VRegB_31c() const; |
| 339 | int32_t VRegB_31i() const; |
| 340 | int32_t VRegB_31t() const; |
| 341 | uint16_t VRegB_32x() const; |
| 342 | uint16_t VRegB_35c() const; |
| 343 | uint16_t VRegB_3rc() const; |
Dragos Sbirlea | 39f9927 | 2013-06-25 13:17:36 -0700 | [diff] [blame] | 344 | uint64_t VRegB_51l() const; // vB_wide |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 345 | |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 346 | // The following methods return the vB operand for all instruction formats where it is encoded in |
| 347 | // the first 16 bits of instruction. The "inst_data" parameter holds these 16 bits. The returned |
| 348 | // value is decoded from it. |
| 349 | int4_t VRegB_11n(uint16_t inst_data) const; |
| 350 | uint4_t VRegB_12x(uint16_t inst_data) const; |
| 351 | uint4_t VRegB_22c(uint16_t inst_data) const; |
| 352 | uint4_t VRegB_22s(uint16_t inst_data) const; |
| 353 | uint4_t VRegB_22t(uint16_t inst_data) const; |
| 354 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 355 | // VRegC |
Dragos Sbirlea | 8cc5162 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 356 | bool HasVRegC() const; |
Dragos Sbirlea | 39f9927 | 2013-06-25 13:17:36 -0700 | [diff] [blame] | 357 | int32_t VRegC() const; |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 358 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 359 | int8_t VRegC_22b() const; |
| 360 | uint16_t VRegC_22c() const; |
| 361 | int16_t VRegC_22s() const; |
| 362 | int16_t VRegC_22t() const; |
| 363 | uint8_t VRegC_23x() const; |
| 364 | uint4_t VRegC_35c() const; |
| 365 | uint16_t VRegC_3rc() const; |
| 366 | |
| 367 | // Fills the given array with the 'arg' array of the instruction. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 368 | void GetArgs(uint32_t args[5], uint16_t inst_data) const; |
| 369 | void GetArgs(uint32_t args[5]) const { |
| 370 | return GetArgs(args, Fetch16(0)); |
| 371 | } |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 372 | |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 373 | // Returns the opcode field of the instruction. The given "inst_data" parameter must be the first |
| 374 | // 16 bits of instruction. |
| 375 | Code Opcode(uint16_t inst_data) const { |
| 376 | DCHECK_EQ(inst_data, Fetch16(0)); |
| 377 | return static_cast<Code>(inst_data & 0xFF); |
| 378 | } |
| 379 | |
| 380 | // 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] | 381 | Code Opcode() const { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 382 | return Opcode(Fetch16(0)); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 383 | } |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 384 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 385 | void SetOpcode(Code opcode) { |
| 386 | DCHECK_LT(static_cast<uint16_t>(opcode), 256u); |
| 387 | uint16_t* insns = reinterpret_cast<uint16_t*>(this); |
| 388 | insns[0] = (insns[0] & 0xff00) | static_cast<uint16_t>(opcode); |
| 389 | } |
| 390 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 391 | void SetVRegA_10x(uint8_t val) { |
| 392 | DCHECK(FormatOf(Opcode()) == k10x); |
| 393 | uint16_t* insns = reinterpret_cast<uint16_t*>(this); |
| 394 | insns[0] = (val << 8) | (insns[0] & 0x00ff); |
| 395 | } |
| 396 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 397 | void SetVRegB_3rc(uint16_t val) { |
| 398 | DCHECK(FormatOf(Opcode()) == k3rc); |
| 399 | uint16_t* insns = reinterpret_cast<uint16_t*>(this); |
| 400 | insns[1] = val; |
| 401 | } |
| 402 | |
| 403 | void SetVRegB_35c(uint16_t val) { |
| 404 | DCHECK(FormatOf(Opcode()) == k35c); |
| 405 | uint16_t* insns = reinterpret_cast<uint16_t*>(this); |
| 406 | insns[1] = val; |
| 407 | } |
| 408 | |
| 409 | void SetVRegC_22c(uint16_t val) { |
| 410 | DCHECK(FormatOf(Opcode()) == k22c); |
| 411 | uint16_t* insns = reinterpret_cast<uint16_t*>(this); |
| 412 | insns[1] = val; |
| 413 | } |
| 414 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 415 | // Returns the format of the given opcode. |
| 416 | static Format FormatOf(Code opcode) { |
| 417 | return kInstructionFormats[opcode]; |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 420 | // Returns the flags for the given opcode. |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 421 | static int FlagsOf(Code opcode) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 422 | return kInstructionFlags[opcode]; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 423 | } |
| 424 | |
buzbee | b1f1d64 | 2014-02-27 12:55:32 -0800 | [diff] [blame] | 425 | // Return the verify flags for the given opcode. |
| 426 | static int VerifyFlagsOf(Code opcode) { |
| 427 | return kInstructionVerifyFlags[opcode]; |
| 428 | } |
| 429 | |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 430 | // Returns true if this instruction is a branch. |
| 431 | bool IsBranch() const { |
| 432 | return (kInstructionFlags[Opcode()] & kBranch) != 0; |
| 433 | } |
| 434 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 435 | // Returns true if this instruction is a unconditional branch. |
| 436 | bool IsUnconditional() const { |
| 437 | return (kInstructionFlags[Opcode()] & kUnconditional) != 0; |
| 438 | } |
| 439 | |
Dragos Sbirlea | 39f9927 | 2013-06-25 13:17:36 -0700 | [diff] [blame] | 440 | // Returns the branch offset if this instruction is a branch. |
| 441 | int32_t GetTargetOffset() const; |
| 442 | |
| 443 | // Returns true if the instruction allows control flow to go to the following instruction. |
| 444 | bool CanFlowThrough() const; |
| 445 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 446 | // Returns true if this instruction is a switch. |
| 447 | bool IsSwitch() const { |
| 448 | return (kInstructionFlags[Opcode()] & kSwitch) != 0; |
| 449 | } |
| 450 | |
| 451 | // Returns true if this instruction can throw. |
| 452 | bool IsThrow() const { |
| 453 | return (kInstructionFlags[Opcode()] & kThrow) != 0; |
| 454 | } |
| 455 | |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 456 | // Determine if the instruction is any of 'return' instructions. |
| 457 | bool IsReturn() const { |
| 458 | return (kInstructionFlags[Opcode()] & kReturn) != 0; |
| 459 | } |
| 460 | |
| 461 | // Determine if this instruction ends execution of its basic block. |
| 462 | bool IsBasicBlockEnd() const { |
| 463 | return IsBranch() || IsReturn() || Opcode() == THROW; |
| 464 | } |
| 465 | |
| 466 | // Determine if this instruction is an invoke. |
| 467 | bool IsInvoke() const { |
| 468 | return (kInstructionFlags[Opcode()] & kInvoke) != 0; |
| 469 | } |
| 470 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 471 | int GetVerifyTypeArgumentA() const { |
| 472 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegA | kVerifyRegAWide)); |
| 473 | } |
| 474 | |
| 475 | int GetVerifyTypeArgumentB() const { |
| 476 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegB | kVerifyRegBField | kVerifyRegBMethod | |
| 477 | kVerifyRegBNewInstance | kVerifyRegBString | kVerifyRegBType | kVerifyRegBWide)); |
| 478 | } |
| 479 | |
| 480 | int GetVerifyTypeArgumentC() const { |
| 481 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegC | kVerifyRegCField | |
jeffhao | 3bb3246 | 2012-02-01 16:12:27 -0800 | [diff] [blame] | 482 | kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide)); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | int GetVerifyExtraFlags() const { |
| 486 | return (kInstructionVerifyFlags[Opcode()] & (kVerifyArrayData | kVerifyBranchTarget | |
| 487 | kVerifySwitchTargets | kVerifyVarArg | kVerifyVarArgRange | kVerifyError)); |
| 488 | } |
| 489 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 490 | // Get the dex PC of this instruction as a offset in code units from the beginning of insns. |
| 491 | uint32_t GetDexPc(const uint16_t* insns) const { |
| 492 | return (reinterpret_cast<const uint16_t*>(this) - insns); |
| 493 | } |
| 494 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 495 | // Dump decoded version of instruction |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 496 | std::string DumpString(const DexFile*) const; |
| 497 | |
| 498 | // Dump code_units worth of this instruction, padding to code_units for shorter instructions |
| 499 | std::string DumpHex(size_t code_units) const; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 500 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 501 | uint16_t Fetch16(size_t offset) const { |
| 502 | const uint16_t* insns = reinterpret_cast<const uint16_t*>(this); |
| 503 | return insns[offset]; |
| 504 | } |
| 505 | |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 506 | private: |
| 507 | size_t SizeInCodeUnitsComplexOpcode() const; |
| 508 | |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 509 | uint32_t Fetch32(size_t offset) const { |
| 510 | return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16)); |
| 511 | } |
| 512 | |
| 513 | uint4_t InstA() const { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 514 | return InstA(Fetch16(0)); |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | uint4_t InstB() const { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 518 | return InstB(Fetch16(0)); |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | uint8_t InstAA() const { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 522 | return InstAA(Fetch16(0)); |
| 523 | } |
| 524 | |
| 525 | uint4_t InstA(uint16_t inst_data) const { |
| 526 | DCHECK_EQ(inst_data, Fetch16(0)); |
| 527 | return static_cast<uint4_t>((inst_data >> 8) & 0x0f); |
| 528 | } |
| 529 | |
| 530 | uint4_t InstB(uint16_t inst_data) const { |
| 531 | DCHECK_EQ(inst_data, Fetch16(0)); |
| 532 | return static_cast<uint4_t>(inst_data >> 12); |
| 533 | } |
| 534 | |
| 535 | uint8_t InstAA(uint16_t inst_data) const { |
| 536 | DCHECK_EQ(inst_data, Fetch16(0)); |
| 537 | return static_cast<uint8_t>(inst_data >> 8); |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 540 | static const char* const kInstructionNames[]; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 541 | static Format const kInstructionFormats[]; |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 542 | static int const kInstructionFlags[]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 543 | static int const kInstructionVerifyFlags[]; |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 544 | static int const kInstructionSizeInCodeUnits[]; |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 545 | DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction); |
| 546 | }; |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 547 | std::ostream& operator<<(std::ostream& os, const Instruction::Code& code); |
| 548 | std::ostream& operator<<(std::ostream& os, const Instruction::Format& format); |
| 549 | std::ostream& operator<<(std::ostream& os, const Instruction::Flags& flags); |
| 550 | std::ostream& operator<<(std::ostream& os, const Instruction::VerifyFlag& vflags); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 551 | |
| 552 | /* |
| 553 | * Holds the contents of a decoded instruction. |
| 554 | */ |
| 555 | struct DecodedInstruction { |
| 556 | uint32_t vA; |
| 557 | uint32_t vB; |
| 558 | uint64_t vB_wide; /* for k51l */ |
| 559 | uint32_t vC; |
| 560 | uint32_t arg[5]; /* vC/D/E/F/G in invoke or filled-new-array */ |
| 561 | Instruction::Code opcode; |
| 562 | |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 563 | explicit DecodedInstruction(const Instruction* inst) { |
| 564 | inst->Decode(vA, vB, vB_wide, vC, arg); |
| 565 | opcode = inst->Opcode(); |
| 566 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 567 | }; |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 568 | |
| 569 | } // namespace art |
| 570 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 571 | #endif // ART_RUNTIME_DEX_INSTRUCTION_H_ |