blob: df2d3799ab278e78b372fe5340da2d59f2d9c212 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro12eb78e2011-06-24 14:51:06 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_INSTRUCTION_H_
18#define ART_RUNTIME_DEX_INSTRUCTION_H_
Carl Shapiro12eb78e2011-06-24 14:51:06 -070019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "globals.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070023
Sebastien Hertz807a2562013-04-15 09:33:39 +020024typedef uint8_t uint4_t;
25typedef int8_t int4_t;
26
Carl Shapiro12eb78e2011-06-24 14:51:06 -070027namespace art {
28
Ian Rogersd81871c2011-10-03 13:57:23 -070029class DexFile;
30
Elliott Hughesadb8c672012-03-06 16:49:32 -080031enum {
32 kNumPackedOpcodes = 0x100
33};
34
Carl Shapiro12eb78e2011-06-24 14:51:06 -070035class Instruction {
36 public:
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070037 // NOP-encoded switch-statement signatures.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070038 enum Signatures {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070039 kPackedSwitchSignature = 0x0100,
40 kSparseSwitchSignature = 0x0200,
Elliott Hughesadb8c672012-03-06 16:49:32 -080041 kArrayDataSignature = 0x0300,
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070042 };
43
Ian Rogersdf1ce912012-11-27 17:07:11 -080044 struct PACKED(4) PackedSwitchPayload {
Logan Chien19c350a2012-05-01 19:21:32 +080045 const uint16_t ident;
46 const uint16_t case_count;
47 const int32_t first_key;
48 const int32_t targets[];
Dragos Sbirlea39f99272013-06-25 13:17:36 -070049
Logan Chien19c350a2012-05-01 19:21:32 +080050 private:
51 DISALLOW_COPY_AND_ASSIGN(PackedSwitchPayload);
52 };
53
Ian Rogersdf1ce912012-11-27 17:07:11 -080054 struct PACKED(4) SparseSwitchPayload {
Logan Chien19c350a2012-05-01 19:21:32 +080055 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 Rogersdf1ce912012-11-27 17:07:11 -080072 struct PACKED(4) ArrayDataPayload {
Logan Chien19c350a2012-05-01 19:21:32 +080073 const uint16_t ident;
74 const uint16_t element_width;
75 const uint32_t element_count;
76 const uint8_t data[];
Dragos Sbirlea39f99272013-06-25 13:17:36 -070077
Logan Chien19c350a2012-05-01 19:21:32 +080078 private:
79 DISALLOW_COPY_AND_ASSIGN(ArrayDataPayload);
80 };
81
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082 enum Code { // private marker to avoid generate-operator-out.py from processing.
jeffhaoba5ebb92011-08-25 17:24:37 -070083#define INSTRUCTION_ENUM(opcode, cname, p, f, r, i, a, v) cname = opcode,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070084#include "dex_instruction_list.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070085 DEX_INSTRUCTION_LIST(INSTRUCTION_ENUM)
Carl Shapirod84f49c2011-06-29 00:27:46 -070086#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070087#undef INSTRUCTION_ENUM
Ian Rogersf72a11d2014-10-30 15:41:08 -070088 RSUB_INT_LIT16 = RSUB_INT,
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070089 };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070090
Elliott Hughesadb8c672012-03-06 16:49:32 -080091 enum Format {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070092 k10x, // op
93 k12x, // op vA, vB
94 k11n, // op vA, #+B
95 k11x, // op vAA
96 k10t, // op +AA
97 k20t, // op +AAAA
98 k22x, // op vAA, vBBBB
99 k21t, // op vAA, +BBBB
100 k21s, // op vAA, #+BBBB
101 k21h, // op vAA, #+BBBB00000[00000000]
102 k21c, // op vAA, thing@BBBB
103 k23x, // op vAA, vBB, vCC
104 k22b, // op vAA, vBB, #+CC
105 k22t, // op vA, vB, +CCCC
106 k22s, // op vA, vB, #+CCCC
107 k22c, // op vA, vB, thing@CCCC
Igor Murashkin158f35c2015-06-10 15:55:30 -0700108 k25x, // op vC, {vD, vE, vF, vG} (B: count)
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700109 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
116 k51l, // op vAA, #+BBBBBBBBBBBBBBBB
117 };
118
Aart Bikb1f37532015-06-29 11:03:55 -0700119 enum IndexType {
120 kIndexUnknown = 0,
121 kIndexNone, // has no index
122 kIndexTypeRef, // type reference index
123 kIndexStringRef, // string reference index
124 kIndexMethodRef, // method reference index
125 kIndexFieldRef, // field reference index
126 kIndexFieldOffset, // field offset (for static linked fields)
127 kIndexVtableOffset // vtable offset (for static linked methods)
128 };
129
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700130 enum Flags {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700131 kBranch = 0x0000001, // conditional or unconditional branch
132 kContinue = 0x0000002, // flow can continue to next statement
133 kSwitch = 0x0000004, // switch statement
134 kThrow = 0x0000008, // could cause an exception to be thrown
135 kReturn = 0x0000010, // returns, no additional statements
136 kInvoke = 0x0000020, // a flavor of invoke
137 kUnconditional = 0x0000040, // unconditional branch
138 kAdd = 0x0000080, // addition
139 kSubtract = 0x0000100, // subtract
140 kMultiply = 0x0000200, // multiply
141 kDivide = 0x0000400, // division
142 kRemainder = 0x0000800, // remainder
143 kAnd = 0x0001000, // and
144 kOr = 0x0002000, // or
145 kXor = 0x0004000, // xor
146 kShl = 0x0008000, // shl
147 kShr = 0x0010000, // shr
148 kUshr = 0x0020000, // ushr
149 kCast = 0x0040000, // cast
150 kStore = 0x0080000, // store opcode
151 kLoad = 0x0100000, // load opcode
152 kClobber = 0x0200000, // clobbers memory in a big way (not just a write)
153 kRegCFieldOrConstant = 0x0400000, // is the third virtual register a field or literal constant (vC)
154 kRegBFieldOrConstant = 0x0800000, // is the second virtual register a field or literal constant (vB)
155 kExperimental = 0x1000000, // is an experimental opcode
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700156 };
157
jeffhaoba5ebb92011-08-25 17:24:37 -0700158 enum VerifyFlag {
Andreas Gampec3314312014-06-19 18:13:29 -0700159 kVerifyNone = 0x000000,
160 kVerifyRegA = 0x000001,
161 kVerifyRegAWide = 0x000002,
162 kVerifyRegB = 0x000004,
163 kVerifyRegBField = 0x000008,
164 kVerifyRegBMethod = 0x000010,
165 kVerifyRegBNewInstance = 0x000020,
166 kVerifyRegBString = 0x000040,
167 kVerifyRegBType = 0x000080,
168 kVerifyRegBWide = 0x000100,
169 kVerifyRegC = 0x000200,
170 kVerifyRegCField = 0x000400,
171 kVerifyRegCNewArray = 0x000800,
172 kVerifyRegCType = 0x001000,
173 kVerifyRegCWide = 0x002000,
174 kVerifyArrayData = 0x004000,
175 kVerifyBranchTarget = 0x008000,
176 kVerifySwitchTargets = 0x010000,
177 kVerifyVarArg = 0x020000,
178 kVerifyVarArgNonZero = 0x040000,
179 kVerifyVarArgRange = 0x080000,
180 kVerifyVarArgRangeNonZero = 0x100000,
181 kVerifyRuntimeOnly = 0x200000,
182 kVerifyError = 0x400000,
jeffhaoba5ebb92011-08-25 17:24:37 -0700183 };
184
Ian Rogers29a26482014-05-02 15:27:29 -0700185 static constexpr uint32_t kMaxVarArgRegs = 5;
jeffhaoba5ebb92011-08-25 17:24:37 -0700186
Elliott Hughesadb8c672012-03-06 16:49:32 -0800187 // Returns the size (in 2 byte code units) of this instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700188 size_t SizeInCodeUnits() const {
189 int result = kInstructionSizeInCodeUnits[Opcode()];
190 if (UNLIKELY(result < 0)) {
191 return SizeInCodeUnitsComplexOpcode();
192 } else {
193 return static_cast<size_t>(result);
194 }
195 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700196
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200197 // Reads an instruction out of the stream at the specified address.
198 static const Instruction* At(const uint16_t* code) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700199 DCHECK(code != nullptr);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200200 return reinterpret_cast<const Instruction*>(code);
201 }
202
203 // Reads an instruction out of the stream from the current address plus an offset.
Ian Rogers7b078e82014-09-10 14:44:24 -0700204 const Instruction* RelativeAt(int32_t offset) const WARN_UNUSED {
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200205 return At(reinterpret_cast<const uint16_t*>(this) + offset);
206 }
207
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700208 // Returns a pointer to the next instruction in the stream.
Ian Rogersa75a0132012-09-28 11:41:42 -0700209 const Instruction* Next() const {
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200210 return RelativeAt(SizeInCodeUnits());
Ian Rogersa75a0132012-09-28 11:41:42 -0700211 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700212
Jeff Hao9cec2472013-05-14 18:17:06 -0700213 // Returns a pointer to the instruction after this 1xx instruction in the stream.
214 const Instruction* Next_1xx() const {
215 DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200216 return RelativeAt(1);
Jeff Hao9cec2472013-05-14 18:17:06 -0700217 }
218
219 // Returns a pointer to the instruction after this 2xx instruction in the stream.
220 const Instruction* Next_2xx() const {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700221 DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k25x);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200222 return RelativeAt(2);
Jeff Hao9cec2472013-05-14 18:17:06 -0700223 }
224
225 // Returns a pointer to the instruction after this 3xx instruction in the stream.
226 const Instruction* Next_3xx() const {
227 DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200228 return RelativeAt(3);
Jeff Hao9cec2472013-05-14 18:17:06 -0700229 }
230
231 // Returns a pointer to the instruction after this 51l instruction in the stream.
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200232 const Instruction* Next_51l() const {
233 DCHECK(FormatOf(Opcode()) == k51l);
234 return RelativeAt(5);
235 }
Jeff Hao9cec2472013-05-14 18:17:06 -0700236
Elliott Hughesadb8c672012-03-06 16:49:32 -0800237 // Returns the name of this instruction's opcode.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700238 const char* Name() const {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800239 return Instruction::Name(Opcode());
240 }
241
242 // Returns the name of the given opcode.
243 static const char* Name(Code opcode) {
244 return kInstructionNames[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700245 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700246
Sebastien Hertz807a2562013-04-15 09:33:39 +0200247 // VRegA
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700248 bool HasVRegA() const;
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700249 int32_t VRegA() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200250
251 int8_t VRegA_10t() const {
252 return VRegA_10t(Fetch16(0));
253 }
254 uint8_t VRegA_10x() const {
255 return VRegA_10x(Fetch16(0));
256 }
257 uint4_t VRegA_11n() const {
258 return VRegA_11n(Fetch16(0));
259 }
260 uint8_t VRegA_11x() const {
261 return VRegA_11x(Fetch16(0));
262 }
263 uint4_t VRegA_12x() const {
264 return VRegA_12x(Fetch16(0));
265 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200266 int16_t VRegA_20t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200267 uint8_t VRegA_21c() const {
268 return VRegA_21c(Fetch16(0));
269 }
270 uint8_t VRegA_21h() const {
271 return VRegA_21h(Fetch16(0));
272 }
273 uint8_t VRegA_21s() const {
274 return VRegA_21s(Fetch16(0));
275 }
276 uint8_t VRegA_21t() const {
277 return VRegA_21t(Fetch16(0));
278 }
279 uint8_t VRegA_22b() const {
280 return VRegA_22b(Fetch16(0));
281 }
282 uint4_t VRegA_22c() const {
283 return VRegA_22c(Fetch16(0));
284 }
285 uint4_t VRegA_22s() const {
286 return VRegA_22s(Fetch16(0));
287 }
288 uint4_t VRegA_22t() const {
289 return VRegA_22t(Fetch16(0));
290 }
291 uint8_t VRegA_22x() const {
292 return VRegA_22x(Fetch16(0));
293 }
294 uint8_t VRegA_23x() const {
295 return VRegA_23x(Fetch16(0));
296 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200297 int32_t VRegA_30t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200298 uint8_t VRegA_31c() const {
299 return VRegA_31c(Fetch16(0));
300 }
301 uint8_t VRegA_31i() const {
302 return VRegA_31i(Fetch16(0));
303 }
304 uint8_t VRegA_31t() const {
305 return VRegA_31t(Fetch16(0));
306 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200307 uint16_t VRegA_32x() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200308 uint4_t VRegA_35c() const {
309 return VRegA_35c(Fetch16(0));
310 }
311 uint8_t VRegA_3rc() const {
312 return VRegA_3rc(Fetch16(0));
313 }
314 uint8_t VRegA_51l() const {
315 return VRegA_51l(Fetch16(0));
316 }
317
318 // The following methods return the vA operand for various instruction formats. The "inst_data"
319 // parameter holds the first 16 bits of instruction which the returned value is decoded from.
320 int8_t VRegA_10t(uint16_t inst_data) const;
321 uint8_t VRegA_10x(uint16_t inst_data) const;
322 uint4_t VRegA_11n(uint16_t inst_data) const;
323 uint8_t VRegA_11x(uint16_t inst_data) const;
324 uint4_t VRegA_12x(uint16_t inst_data) const;
325 uint8_t VRegA_21c(uint16_t inst_data) const;
326 uint8_t VRegA_21h(uint16_t inst_data) const;
327 uint8_t VRegA_21s(uint16_t inst_data) const;
328 uint8_t VRegA_21t(uint16_t inst_data) const;
329 uint8_t VRegA_22b(uint16_t inst_data) const;
330 uint4_t VRegA_22c(uint16_t inst_data) const;
331 uint4_t VRegA_22s(uint16_t inst_data) const;
332 uint4_t VRegA_22t(uint16_t inst_data) const;
333 uint8_t VRegA_22x(uint16_t inst_data) const;
334 uint8_t VRegA_23x(uint16_t inst_data) const;
335 uint8_t VRegA_31c(uint16_t inst_data) const;
336 uint8_t VRegA_31i(uint16_t inst_data) const;
337 uint8_t VRegA_31t(uint16_t inst_data) const;
338 uint4_t VRegA_35c(uint16_t inst_data) const;
339 uint8_t VRegA_3rc(uint16_t inst_data) const;
340 uint8_t VRegA_51l(uint16_t inst_data) const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200341
342 // VRegB
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700343 bool HasVRegB() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700344 int32_t VRegB() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200345
Ian Rogers29a26482014-05-02 15:27:29 -0700346 bool HasWideVRegB() const;
347 uint64_t WideVRegB() const;
348
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200349 int4_t VRegB_11n() const {
350 return VRegB_11n(Fetch16(0));
351 }
352 uint4_t VRegB_12x() const {
353 return VRegB_12x(Fetch16(0));
354 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200355 uint16_t VRegB_21c() const;
356 uint16_t VRegB_21h() const;
357 int16_t VRegB_21s() const;
358 int16_t VRegB_21t() const;
359 uint8_t VRegB_22b() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200360 uint4_t VRegB_22c() const {
361 return VRegB_22c(Fetch16(0));
362 }
363 uint4_t VRegB_22s() const {
364 return VRegB_22s(Fetch16(0));
365 }
366 uint4_t VRegB_22t() const {
367 return VRegB_22t(Fetch16(0));
368 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200369 uint16_t VRegB_22x() const;
370 uint8_t VRegB_23x() const;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700371 uint4_t VRegB_25x() const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200372 uint32_t VRegB_31c() const;
373 int32_t VRegB_31i() const;
374 int32_t VRegB_31t() const;
375 uint16_t VRegB_32x() const;
376 uint16_t VRegB_35c() const;
377 uint16_t VRegB_3rc() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700378 uint64_t VRegB_51l() const; // vB_wide
Sebastien Hertz807a2562013-04-15 09:33:39 +0200379
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200380 // The following methods return the vB operand for all instruction formats where it is encoded in
381 // the first 16 bits of instruction. The "inst_data" parameter holds these 16 bits. The returned
382 // value is decoded from it.
383 int4_t VRegB_11n(uint16_t inst_data) const;
384 uint4_t VRegB_12x(uint16_t inst_data) const;
385 uint4_t VRegB_22c(uint16_t inst_data) const;
386 uint4_t VRegB_22s(uint16_t inst_data) const;
387 uint4_t VRegB_22t(uint16_t inst_data) const;
388
Sebastien Hertz807a2562013-04-15 09:33:39 +0200389 // VRegC
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700390 bool HasVRegC() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700391 int32_t VRegC() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200392
Sebastien Hertz807a2562013-04-15 09:33:39 +0200393 int8_t VRegC_22b() const;
394 uint16_t VRegC_22c() const;
395 int16_t VRegC_22s() const;
396 int16_t VRegC_22t() const;
397 uint8_t VRegC_23x() const;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700398 uint4_t VRegC_25x() const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200399 uint4_t VRegC_35c() const;
400 uint16_t VRegC_3rc() const;
401
402 // Fills the given array with the 'arg' array of the instruction.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700403 bool HasVarArgs35c() const;
404 bool HasVarArgs25x() const;
405
406 // TODO(iam): Make this name more consistent with GetAllArgs25x by including the opcode format.
Ian Rogers29a26482014-05-02 15:27:29 -0700407 void GetVarArgs(uint32_t args[kMaxVarArgRegs], uint16_t inst_data) const;
408 void GetVarArgs(uint32_t args[kMaxVarArgRegs]) const {
409 return GetVarArgs(args, Fetch16(0));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200410 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700411 void GetAllArgs25x(uint32_t args[kMaxVarArgRegs]) const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200412
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200413 // Returns the opcode field of the instruction. The given "inst_data" parameter must be the first
414 // 16 bits of instruction.
415 Code Opcode(uint16_t inst_data) const {
416 DCHECK_EQ(inst_data, Fetch16(0));
417 return static_cast<Code>(inst_data & 0xFF);
418 }
419
420 // Returns the opcode field of the instruction from the first 16 bits of instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700421 Code Opcode() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200422 return Opcode(Fetch16(0));
Ian Rogersa75a0132012-09-28 11:41:42 -0700423 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700424
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200425 void SetOpcode(Code opcode) {
426 DCHECK_LT(static_cast<uint16_t>(opcode), 256u);
427 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
428 insns[0] = (insns[0] & 0xff00) | static_cast<uint16_t>(opcode);
429 }
430
Sebastien Hertz543959c2013-07-03 12:00:19 +0200431 void SetVRegA_10x(uint8_t val) {
432 DCHECK(FormatOf(Opcode()) == k10x);
433 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
434 insns[0] = (val << 8) | (insns[0] & 0x00ff);
435 }
436
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200437 void SetVRegB_3rc(uint16_t val) {
438 DCHECK(FormatOf(Opcode()) == k3rc);
439 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
440 insns[1] = val;
441 }
442
443 void SetVRegB_35c(uint16_t val) {
444 DCHECK(FormatOf(Opcode()) == k35c);
445 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
446 insns[1] = val;
447 }
448
449 void SetVRegC_22c(uint16_t val) {
450 DCHECK(FormatOf(Opcode()) == k22c);
451 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
452 insns[1] = val;
453 }
454
Elliott Hughesadb8c672012-03-06 16:49:32 -0800455 // Returns the format of the given opcode.
456 static Format FormatOf(Code opcode) {
457 return kInstructionFormats[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700458 }
459
Aart Bikb1f37532015-06-29 11:03:55 -0700460 // Returns the index type of the given opcode.
461 static IndexType IndexTypeOf(Code opcode) {
462 return kInstructionIndexTypes[opcode];
463 }
464
Elliott Hughesadb8c672012-03-06 16:49:32 -0800465 // Returns the flags for the given opcode.
Ian Rogersa75a0132012-09-28 11:41:42 -0700466 static int FlagsOf(Code opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800467 return kInstructionFlags[opcode];
jeffhaobdb76512011-09-07 11:43:16 -0700468 }
469
buzbeeb1f1d642014-02-27 12:55:32 -0800470 // Return the verify flags for the given opcode.
471 static int VerifyFlagsOf(Code opcode) {
472 return kInstructionVerifyFlags[opcode];
473 }
474
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700475 // Returns true if this instruction is a branch.
476 bool IsBranch() const {
477 return (kInstructionFlags[Opcode()] & kBranch) != 0;
478 }
479
TDYa127526643e2012-05-26 01:01:48 -0700480 // Returns true if this instruction is a unconditional branch.
481 bool IsUnconditional() const {
482 return (kInstructionFlags[Opcode()] & kUnconditional) != 0;
483 }
484
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700485 // Returns the branch offset if this instruction is a branch.
486 int32_t GetTargetOffset() const;
487
488 // Returns true if the instruction allows control flow to go to the following instruction.
489 bool CanFlowThrough() const;
490
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000491 // Returns true if the instruction is a quickened instruction.
492 bool IsQuickened() const {
493 return (kInstructionIndexTypes[Opcode()] == kIndexFieldOffset) ||
494 (kInstructionIndexTypes[Opcode()] == kIndexVtableOffset);
495 }
496
jeffhaoba5ebb92011-08-25 17:24:37 -0700497 // Returns true if this instruction is a switch.
498 bool IsSwitch() const {
499 return (kInstructionFlags[Opcode()] & kSwitch) != 0;
500 }
501
502 // Returns true if this instruction can throw.
503 bool IsThrow() const {
504 return (kInstructionFlags[Opcode()] & kThrow) != 0;
505 }
506
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700507 // Determine if the instruction is any of 'return' instructions.
508 bool IsReturn() const {
509 return (kInstructionFlags[Opcode()] & kReturn) != 0;
510 }
511
512 // Determine if this instruction ends execution of its basic block.
513 bool IsBasicBlockEnd() const {
514 return IsBranch() || IsReturn() || Opcode() == THROW;
515 }
516
517 // Determine if this instruction is an invoke.
518 bool IsInvoke() const {
519 return (kInstructionFlags[Opcode()] & kInvoke) != 0;
520 }
521
Igor Murashkin158f35c2015-06-10 15:55:30 -0700522 // Determine if this instruction is experimental.
523 bool IsExperimental() const {
524 return (kInstructionFlags[Opcode()] & kExperimental) != 0;
525 }
526
jeffhaoba5ebb92011-08-25 17:24:37 -0700527 int GetVerifyTypeArgumentA() const {
528 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegA | kVerifyRegAWide));
529 }
530
531 int GetVerifyTypeArgumentB() const {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700532 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegB | kVerifyRegBField |
533 kVerifyRegBMethod | kVerifyRegBNewInstance | kVerifyRegBString | kVerifyRegBType |
534 kVerifyRegBWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700535 }
536
537 int GetVerifyTypeArgumentC() const {
538 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegC | kVerifyRegCField |
Ian Rogers5fb22a92014-06-13 10:31:28 -0700539 kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700540 }
541
542 int GetVerifyExtraFlags() const {
543 return (kInstructionVerifyFlags[Opcode()] & (kVerifyArrayData | kVerifyBranchTarget |
Andreas Gampec3314312014-06-19 18:13:29 -0700544 kVerifySwitchTargets | kVerifyVarArg | kVerifyVarArgNonZero | kVerifyVarArgRange |
545 kVerifyVarArgRangeNonZero | kVerifyError));
Ian Rogers5fb22a92014-06-13 10:31:28 -0700546 }
547
548 bool GetVerifyIsRuntimeOnly() const {
549 return (kInstructionVerifyFlags[Opcode()] & kVerifyRuntimeOnly) != 0;
jeffhaoba5ebb92011-08-25 17:24:37 -0700550 }
551
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700552 // Get the dex PC of this instruction as a offset in code units from the beginning of insns.
553 uint32_t GetDexPc(const uint16_t* insns) const {
554 return (reinterpret_cast<const uint16_t*>(this) - insns);
555 }
556
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 // Dump decoded version of instruction
Ian Rogers2c8a8572011-10-24 17:11:36 -0700558 std::string DumpString(const DexFile*) const;
559
560 // Dump code_units worth of this instruction, padding to code_units for shorter instructions
561 std::string DumpHex(size_t code_units) const;
Ian Rogersd81871c2011-10-03 13:57:23 -0700562
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800563 // Little-endian dump code_units worth of this instruction, padding to code_units for
564 // shorter instructions
565 std::string DumpHexLE(size_t instr_code_units) const;
566
Sebastien Hertz807a2562013-04-15 09:33:39 +0200567 uint16_t Fetch16(size_t offset) const {
568 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
569 return insns[offset];
570 }
571
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200572 private:
573 size_t SizeInCodeUnitsComplexOpcode() const;
574
Sebastien Hertz807a2562013-04-15 09:33:39 +0200575 uint32_t Fetch32(size_t offset) const {
576 return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16));
577 }
578
579 uint4_t InstA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200580 return InstA(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200581 }
582
583 uint4_t InstB() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200584 return InstB(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200585 }
586
587 uint8_t InstAA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200588 return InstAA(Fetch16(0));
589 }
590
591 uint4_t InstA(uint16_t inst_data) const {
592 DCHECK_EQ(inst_data, Fetch16(0));
593 return static_cast<uint4_t>((inst_data >> 8) & 0x0f);
594 }
595
596 uint4_t InstB(uint16_t inst_data) const {
597 DCHECK_EQ(inst_data, Fetch16(0));
598 return static_cast<uint4_t>(inst_data >> 12);
599 }
600
601 uint8_t InstAA(uint16_t inst_data) const {
602 DCHECK_EQ(inst_data, Fetch16(0));
603 return static_cast<uint8_t>(inst_data >> 8);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200604 }
605
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700606 static const char* const kInstructionNames[];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607 static Format const kInstructionFormats[];
Aart Bikb1f37532015-06-29 11:03:55 -0700608 static IndexType const kInstructionIndexTypes[];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700609 static int const kInstructionFlags[];
jeffhaoba5ebb92011-08-25 17:24:37 -0700610 static int const kInstructionVerifyFlags[];
Ian Rogersa75a0132012-09-28 11:41:42 -0700611 static int const kInstructionSizeInCodeUnits[];
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700612 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
613};
Ian Rogersa75a0132012-09-28 11:41:42 -0700614std::ostream& operator<<(std::ostream& os, const Instruction::Code& code);
615std::ostream& operator<<(std::ostream& os, const Instruction::Format& format);
616std::ostream& operator<<(std::ostream& os, const Instruction::Flags& flags);
617std::ostream& operator<<(std::ostream& os, const Instruction::VerifyFlag& vflags);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800618
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700619} // namespace art
620
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700621#endif // ART_RUNTIME_DEX_INSTRUCTION_H_