blob: 578550cae2ba03cbcc1c05ab278e97797a492b2b [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.
Narayan Kamathbd48b342016-08-01 17:32:37 +010083#define INSTRUCTION_ENUM(opcode, cname, p, f, 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
108 k32x, // op vAAAA, vBBBB
109 k30t, // op +AAAAAAAA
110 k31t, // op vAA, +BBBBBBBB
111 k31i, // op vAA, #+BBBBBBBB
112 k31c, // op vAA, thing@BBBBBBBB
113 k35c, // op {vC, vD, vE, vF, vG}, thing@BBBB (B: count, A: vG)
114 k3rc, // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
Narayan Kamath8ec3bd22016-08-03 12:46:23 +0100115
116 // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH (A: count)
117 // format: AG op BBBB FEDC HHHH
118 k45cc,
119
120 // op {VCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH (AA: count)
121 // format: AA op BBBB CCCC HHHH
122 k4rcc, // op {VCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH (AA: count)
123
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700124 k51l, // op vAA, #+BBBBBBBBBBBBBBBB
125 };
126
Aart Bikb1f37532015-06-29 11:03:55 -0700127 enum IndexType {
128 kIndexUnknown = 0,
Orion Hodsonb34bb192016-10-18 17:02:58 +0100129 kIndexNone, // has no index
130 kIndexTypeRef, // type reference index
131 kIndexStringRef, // string reference index
132 kIndexMethodRef, // method reference index
133 kIndexFieldRef, // field reference index
134 kIndexFieldOffset, // field offset (for static linked fields)
135 kIndexVtableOffset, // vtable offset (for static linked methods)
136 kIndexMethodAndProtoRef // method and a proto reference index (for invoke-polymorphic)
Aart Bikb1f37532015-06-29 11:03:55 -0700137 };
138
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700139 enum Flags {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700140 kBranch = 0x0000001, // conditional or unconditional branch
141 kContinue = 0x0000002, // flow can continue to next statement
142 kSwitch = 0x0000004, // switch statement
143 kThrow = 0x0000008, // could cause an exception to be thrown
144 kReturn = 0x0000010, // returns, no additional statements
145 kInvoke = 0x0000020, // a flavor of invoke
146 kUnconditional = 0x0000040, // unconditional branch
147 kAdd = 0x0000080, // addition
148 kSubtract = 0x0000100, // subtract
149 kMultiply = 0x0000200, // multiply
150 kDivide = 0x0000400, // division
151 kRemainder = 0x0000800, // remainder
152 kAnd = 0x0001000, // and
153 kOr = 0x0002000, // or
154 kXor = 0x0004000, // xor
155 kShl = 0x0008000, // shl
156 kShr = 0x0010000, // shr
157 kUshr = 0x0020000, // ushr
158 kCast = 0x0040000, // cast
159 kStore = 0x0080000, // store opcode
160 kLoad = 0x0100000, // load opcode
161 kClobber = 0x0200000, // clobbers memory in a big way (not just a write)
162 kRegCFieldOrConstant = 0x0400000, // is the third virtual register a field or literal constant (vC)
163 kRegBFieldOrConstant = 0x0800000, // is the second virtual register a field or literal constant (vB)
164 kExperimental = 0x1000000, // is an experimental opcode
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700165 };
166
jeffhaoba5ebb92011-08-25 17:24:37 -0700167 enum VerifyFlag {
Andreas Gampec3314312014-06-19 18:13:29 -0700168 kVerifyNone = 0x000000,
169 kVerifyRegA = 0x000001,
170 kVerifyRegAWide = 0x000002,
171 kVerifyRegB = 0x000004,
172 kVerifyRegBField = 0x000008,
173 kVerifyRegBMethod = 0x000010,
174 kVerifyRegBNewInstance = 0x000020,
175 kVerifyRegBString = 0x000040,
176 kVerifyRegBType = 0x000080,
177 kVerifyRegBWide = 0x000100,
178 kVerifyRegC = 0x000200,
179 kVerifyRegCField = 0x000400,
180 kVerifyRegCNewArray = 0x000800,
181 kVerifyRegCType = 0x001000,
182 kVerifyRegCWide = 0x002000,
183 kVerifyArrayData = 0x004000,
184 kVerifyBranchTarget = 0x008000,
185 kVerifySwitchTargets = 0x010000,
186 kVerifyVarArg = 0x020000,
187 kVerifyVarArgNonZero = 0x040000,
188 kVerifyVarArgRange = 0x080000,
189 kVerifyVarArgRangeNonZero = 0x100000,
190 kVerifyRuntimeOnly = 0x200000,
191 kVerifyError = 0x400000,
Orion Hodsoncfa325e2016-10-13 10:25:54 +0100192 kVerifyRegHPrototype = 0x800000
jeffhaoba5ebb92011-08-25 17:24:37 -0700193 };
194
Ian Rogers29a26482014-05-02 15:27:29 -0700195 static constexpr uint32_t kMaxVarArgRegs = 5;
jeffhaoba5ebb92011-08-25 17:24:37 -0700196
Elliott Hughesadb8c672012-03-06 16:49:32 -0800197 // Returns the size (in 2 byte code units) of this instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700198 size_t SizeInCodeUnits() const {
199 int result = kInstructionSizeInCodeUnits[Opcode()];
200 if (UNLIKELY(result < 0)) {
201 return SizeInCodeUnitsComplexOpcode();
202 } else {
203 return static_cast<size_t>(result);
204 }
205 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700206
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200207 // Reads an instruction out of the stream at the specified address.
208 static const Instruction* At(const uint16_t* code) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700209 DCHECK(code != nullptr);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200210 return reinterpret_cast<const Instruction*>(code);
211 }
212
213 // Reads an instruction out of the stream from the current address plus an offset.
Ian Rogers7b078e82014-09-10 14:44:24 -0700214 const Instruction* RelativeAt(int32_t offset) const WARN_UNUSED {
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200215 return At(reinterpret_cast<const uint16_t*>(this) + offset);
216 }
217
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700218 // Returns a pointer to the next instruction in the stream.
Ian Rogersa75a0132012-09-28 11:41:42 -0700219 const Instruction* Next() const {
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200220 return RelativeAt(SizeInCodeUnits());
Ian Rogersa75a0132012-09-28 11:41:42 -0700221 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700222
Jeff Hao9cec2472013-05-14 18:17:06 -0700223 // Returns a pointer to the instruction after this 1xx instruction in the stream.
224 const Instruction* Next_1xx() const {
225 DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200226 return RelativeAt(1);
Jeff Hao9cec2472013-05-14 18:17:06 -0700227 }
228
229 // Returns a pointer to the instruction after this 2xx instruction in the stream.
230 const Instruction* Next_2xx() const {
Narayan Kamath14832ef2016-08-05 11:44:32 +0100231 DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k22c);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200232 return RelativeAt(2);
Jeff Hao9cec2472013-05-14 18:17:06 -0700233 }
234
235 // Returns a pointer to the instruction after this 3xx instruction in the stream.
236 const Instruction* Next_3xx() const {
237 DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200238 return RelativeAt(3);
Jeff Hao9cec2472013-05-14 18:17:06 -0700239 }
240
Narayan Kamath8ec3bd22016-08-03 12:46:23 +0100241 // Returns a pointer to the instruction after this 4xx instruction in the stream.
242 const Instruction* Next_4xx() const {
243 DCHECK(FormatOf(Opcode()) >= k45cc && FormatOf(Opcode()) <= k4rcc);
244 return RelativeAt(4);
245 }
246
Jeff Hao9cec2472013-05-14 18:17:06 -0700247 // Returns a pointer to the instruction after this 51l instruction in the stream.
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200248 const Instruction* Next_51l() const {
249 DCHECK(FormatOf(Opcode()) == k51l);
250 return RelativeAt(5);
251 }
Jeff Hao9cec2472013-05-14 18:17:06 -0700252
Elliott Hughesadb8c672012-03-06 16:49:32 -0800253 // Returns the name of this instruction's opcode.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700254 const char* Name() const {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800255 return Instruction::Name(Opcode());
256 }
257
258 // Returns the name of the given opcode.
259 static const char* Name(Code opcode) {
260 return kInstructionNames[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700261 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700262
Sebastien Hertz807a2562013-04-15 09:33:39 +0200263 // VRegA
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700264 bool HasVRegA() const;
Mathieu Chartierde40d472015-10-15 17:47:48 -0700265 ALWAYS_INLINE int32_t VRegA() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200266
267 int8_t VRegA_10t() const {
268 return VRegA_10t(Fetch16(0));
269 }
270 uint8_t VRegA_10x() const {
271 return VRegA_10x(Fetch16(0));
272 }
273 uint4_t VRegA_11n() const {
274 return VRegA_11n(Fetch16(0));
275 }
276 uint8_t VRegA_11x() const {
277 return VRegA_11x(Fetch16(0));
278 }
279 uint4_t VRegA_12x() const {
280 return VRegA_12x(Fetch16(0));
281 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200282 int16_t VRegA_20t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200283 uint8_t VRegA_21c() const {
284 return VRegA_21c(Fetch16(0));
285 }
286 uint8_t VRegA_21h() const {
287 return VRegA_21h(Fetch16(0));
288 }
289 uint8_t VRegA_21s() const {
290 return VRegA_21s(Fetch16(0));
291 }
292 uint8_t VRegA_21t() const {
293 return VRegA_21t(Fetch16(0));
294 }
295 uint8_t VRegA_22b() const {
296 return VRegA_22b(Fetch16(0));
297 }
298 uint4_t VRegA_22c() const {
299 return VRegA_22c(Fetch16(0));
300 }
301 uint4_t VRegA_22s() const {
302 return VRegA_22s(Fetch16(0));
303 }
304 uint4_t VRegA_22t() const {
305 return VRegA_22t(Fetch16(0));
306 }
307 uint8_t VRegA_22x() const {
308 return VRegA_22x(Fetch16(0));
309 }
310 uint8_t VRegA_23x() const {
311 return VRegA_23x(Fetch16(0));
312 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200313 int32_t VRegA_30t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200314 uint8_t VRegA_31c() const {
315 return VRegA_31c(Fetch16(0));
316 }
317 uint8_t VRegA_31i() const {
318 return VRegA_31i(Fetch16(0));
319 }
320 uint8_t VRegA_31t() const {
321 return VRegA_31t(Fetch16(0));
322 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200323 uint16_t VRegA_32x() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200324 uint4_t VRegA_35c() const {
325 return VRegA_35c(Fetch16(0));
326 }
327 uint8_t VRegA_3rc() const {
328 return VRegA_3rc(Fetch16(0));
329 }
330 uint8_t VRegA_51l() const {
331 return VRegA_51l(Fetch16(0));
332 }
Narayan Kamath8ec3bd22016-08-03 12:46:23 +0100333 uint4_t VRegA_45cc() const {
334 return VRegA_45cc(Fetch16(0));
335 }
336 uint8_t VRegA_4rcc() const {
337 return VRegA_4rcc(Fetch16(0));
338 }
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200339
340 // The following methods return the vA operand for various instruction formats. The "inst_data"
341 // parameter holds the first 16 bits of instruction which the returned value is decoded from.
342 int8_t VRegA_10t(uint16_t inst_data) const;
343 uint8_t VRegA_10x(uint16_t inst_data) const;
344 uint4_t VRegA_11n(uint16_t inst_data) const;
345 uint8_t VRegA_11x(uint16_t inst_data) const;
346 uint4_t VRegA_12x(uint16_t inst_data) const;
347 uint8_t VRegA_21c(uint16_t inst_data) const;
348 uint8_t VRegA_21h(uint16_t inst_data) const;
349 uint8_t VRegA_21s(uint16_t inst_data) const;
350 uint8_t VRegA_21t(uint16_t inst_data) const;
351 uint8_t VRegA_22b(uint16_t inst_data) const;
352 uint4_t VRegA_22c(uint16_t inst_data) const;
353 uint4_t VRegA_22s(uint16_t inst_data) const;
354 uint4_t VRegA_22t(uint16_t inst_data) const;
355 uint8_t VRegA_22x(uint16_t inst_data) const;
356 uint8_t VRegA_23x(uint16_t inst_data) const;
357 uint8_t VRegA_31c(uint16_t inst_data) const;
358 uint8_t VRegA_31i(uint16_t inst_data) const;
359 uint8_t VRegA_31t(uint16_t inst_data) const;
360 uint4_t VRegA_35c(uint16_t inst_data) const;
361 uint8_t VRegA_3rc(uint16_t inst_data) const;
362 uint8_t VRegA_51l(uint16_t inst_data) const;
Narayan Kamath8ec3bd22016-08-03 12:46:23 +0100363 uint4_t VRegA_45cc(uint16_t inst_data) const;
364 uint8_t VRegA_4rcc(uint16_t inst_data) const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200365
366 // VRegB
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700367 bool HasVRegB() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700368 int32_t VRegB() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200369
Ian Rogers29a26482014-05-02 15:27:29 -0700370 bool HasWideVRegB() const;
371 uint64_t WideVRegB() const;
372
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200373 int4_t VRegB_11n() const {
374 return VRegB_11n(Fetch16(0));
375 }
376 uint4_t VRegB_12x() const {
377 return VRegB_12x(Fetch16(0));
378 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200379 uint16_t VRegB_21c() const;
380 uint16_t VRegB_21h() const;
381 int16_t VRegB_21s() const;
382 int16_t VRegB_21t() const;
383 uint8_t VRegB_22b() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200384 uint4_t VRegB_22c() const {
385 return VRegB_22c(Fetch16(0));
386 }
387 uint4_t VRegB_22s() const {
388 return VRegB_22s(Fetch16(0));
389 }
390 uint4_t VRegB_22t() const {
391 return VRegB_22t(Fetch16(0));
392 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200393 uint16_t VRegB_22x() const;
394 uint8_t VRegB_23x() const;
395 uint32_t VRegB_31c() const;
396 int32_t VRegB_31i() const;
397 int32_t VRegB_31t() const;
398 uint16_t VRegB_32x() const;
399 uint16_t VRegB_35c() const;
400 uint16_t VRegB_3rc() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700401 uint64_t VRegB_51l() const; // vB_wide
Narayan Kamath8ec3bd22016-08-03 12:46:23 +0100402 uint16_t VRegB_45cc() const;
403 uint16_t VRegB_4rcc() const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200404
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200405 // The following methods return the vB operand for all instruction formats where it is encoded in
406 // the first 16 bits of instruction. The "inst_data" parameter holds these 16 bits. The returned
407 // value is decoded from it.
408 int4_t VRegB_11n(uint16_t inst_data) const;
409 uint4_t VRegB_12x(uint16_t inst_data) const;
410 uint4_t VRegB_22c(uint16_t inst_data) const;
411 uint4_t VRegB_22s(uint16_t inst_data) const;
412 uint4_t VRegB_22t(uint16_t inst_data) const;
413
Sebastien Hertz807a2562013-04-15 09:33:39 +0200414 // VRegC
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700415 bool HasVRegC() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700416 int32_t VRegC() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200417
Sebastien Hertz807a2562013-04-15 09:33:39 +0200418 int8_t VRegC_22b() const;
419 uint16_t VRegC_22c() const;
420 int16_t VRegC_22s() const;
421 int16_t VRegC_22t() const;
422 uint8_t VRegC_23x() const;
423 uint4_t VRegC_35c() const;
424 uint16_t VRegC_3rc() const;
Narayan Kamath8ec3bd22016-08-03 12:46:23 +0100425 uint4_t VRegC_45cc() const;
426 uint16_t VRegC_4rcc() const;
427
428
429 // VRegH
430 bool HasVRegH() const;
431 int32_t VRegH() const;
432 uint16_t VRegH_45cc() const;
433 uint16_t VRegH_4rcc() const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200434
435 // Fills the given array with the 'arg' array of the instruction.
Narayan Kamath14832ef2016-08-05 11:44:32 +0100436 bool HasVarArgs() const;
Ian Rogers29a26482014-05-02 15:27:29 -0700437 void GetVarArgs(uint32_t args[kMaxVarArgRegs], uint16_t inst_data) const;
438 void GetVarArgs(uint32_t args[kMaxVarArgRegs]) const {
439 return GetVarArgs(args, Fetch16(0));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200440 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200441
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200442 // Returns the opcode field of the instruction. The given "inst_data" parameter must be the first
443 // 16 bits of instruction.
444 Code Opcode(uint16_t inst_data) const {
445 DCHECK_EQ(inst_data, Fetch16(0));
446 return static_cast<Code>(inst_data & 0xFF);
447 }
448
449 // Returns the opcode field of the instruction from the first 16 bits of instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700450 Code Opcode() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200451 return Opcode(Fetch16(0));
Ian Rogersa75a0132012-09-28 11:41:42 -0700452 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700453
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200454 void SetOpcode(Code opcode) {
455 DCHECK_LT(static_cast<uint16_t>(opcode), 256u);
456 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
457 insns[0] = (insns[0] & 0xff00) | static_cast<uint16_t>(opcode);
458 }
459
Sebastien Hertz543959c2013-07-03 12:00:19 +0200460 void SetVRegA_10x(uint8_t val) {
461 DCHECK(FormatOf(Opcode()) == k10x);
462 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
463 insns[0] = (val << 8) | (insns[0] & 0x00ff);
464 }
465
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200466 void SetVRegB_3rc(uint16_t val) {
467 DCHECK(FormatOf(Opcode()) == k3rc);
468 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
469 insns[1] = val;
470 }
471
472 void SetVRegB_35c(uint16_t val) {
473 DCHECK(FormatOf(Opcode()) == k35c);
474 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
475 insns[1] = val;
476 }
477
478 void SetVRegC_22c(uint16_t val) {
479 DCHECK(FormatOf(Opcode()) == k22c);
480 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
481 insns[1] = val;
482 }
483
Nicolas Geoffray01b70e82016-11-17 10:58:36 +0000484 void SetVRegA_21c(uint8_t val) {
485 DCHECK(FormatOf(Opcode()) == k21c);
486 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
487 insns[0] = (val << 8) | (insns[0] & 0x00ff);
488 }
489
490 void SetVRegB_21c(uint16_t val) {
491 DCHECK(FormatOf(Opcode()) == k21c);
492 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
493 insns[1] = val;
494 }
495
Elliott Hughesadb8c672012-03-06 16:49:32 -0800496 // Returns the format of the given opcode.
497 static Format FormatOf(Code opcode) {
498 return kInstructionFormats[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700499 }
500
Aart Bikb1f37532015-06-29 11:03:55 -0700501 // Returns the index type of the given opcode.
502 static IndexType IndexTypeOf(Code opcode) {
503 return kInstructionIndexTypes[opcode];
504 }
505
Elliott Hughesadb8c672012-03-06 16:49:32 -0800506 // Returns the flags for the given opcode.
Ian Rogersa75a0132012-09-28 11:41:42 -0700507 static int FlagsOf(Code opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800508 return kInstructionFlags[opcode];
jeffhaobdb76512011-09-07 11:43:16 -0700509 }
510
buzbeeb1f1d642014-02-27 12:55:32 -0800511 // Return the verify flags for the given opcode.
512 static int VerifyFlagsOf(Code opcode) {
513 return kInstructionVerifyFlags[opcode];
514 }
515
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700516 // Returns true if this instruction is a branch.
517 bool IsBranch() const {
518 return (kInstructionFlags[Opcode()] & kBranch) != 0;
519 }
520
TDYa127526643e2012-05-26 01:01:48 -0700521 // Returns true if this instruction is a unconditional branch.
522 bool IsUnconditional() const {
523 return (kInstructionFlags[Opcode()] & kUnconditional) != 0;
524 }
525
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700526 // Returns the branch offset if this instruction is a branch.
527 int32_t GetTargetOffset() const;
528
529 // Returns true if the instruction allows control flow to go to the following instruction.
530 bool CanFlowThrough() const;
531
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000532 // Returns true if the instruction is a quickened instruction.
533 bool IsQuickened() const {
534 return (kInstructionIndexTypes[Opcode()] == kIndexFieldOffset) ||
535 (kInstructionIndexTypes[Opcode()] == kIndexVtableOffset);
536 }
537
jeffhaoba5ebb92011-08-25 17:24:37 -0700538 // Returns true if this instruction is a switch.
539 bool IsSwitch() const {
540 return (kInstructionFlags[Opcode()] & kSwitch) != 0;
541 }
542
543 // Returns true if this instruction can throw.
544 bool IsThrow() const {
545 return (kInstructionFlags[Opcode()] & kThrow) != 0;
546 }
547
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700548 // Determine if the instruction is any of 'return' instructions.
549 bool IsReturn() const {
550 return (kInstructionFlags[Opcode()] & kReturn) != 0;
551 }
552
553 // Determine if this instruction ends execution of its basic block.
554 bool IsBasicBlockEnd() const {
555 return IsBranch() || IsReturn() || Opcode() == THROW;
556 }
557
558 // Determine if this instruction is an invoke.
559 bool IsInvoke() const {
560 return (kInstructionFlags[Opcode()] & kInvoke) != 0;
561 }
562
Igor Murashkin158f35c2015-06-10 15:55:30 -0700563 // Determine if this instruction is experimental.
564 bool IsExperimental() const {
565 return (kInstructionFlags[Opcode()] & kExperimental) != 0;
566 }
567
jeffhaoba5ebb92011-08-25 17:24:37 -0700568 int GetVerifyTypeArgumentA() const {
569 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegA | kVerifyRegAWide));
570 }
571
572 int GetVerifyTypeArgumentB() const {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700573 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegB | kVerifyRegBField |
574 kVerifyRegBMethod | kVerifyRegBNewInstance | kVerifyRegBString | kVerifyRegBType |
575 kVerifyRegBWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700576 }
577
578 int GetVerifyTypeArgumentC() const {
579 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegC | kVerifyRegCField |
Narayan Kamath14832ef2016-08-05 11:44:32 +0100580 kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700581 }
582
Orion Hodsoncfa325e2016-10-13 10:25:54 +0100583 int GetVerifyTypeArgumentH() const {
584 return (kInstructionVerifyFlags[Opcode()] & kVerifyRegHPrototype);
585 }
586
jeffhaoba5ebb92011-08-25 17:24:37 -0700587 int GetVerifyExtraFlags() const {
588 return (kInstructionVerifyFlags[Opcode()] & (kVerifyArrayData | kVerifyBranchTarget |
Andreas Gampec3314312014-06-19 18:13:29 -0700589 kVerifySwitchTargets | kVerifyVarArg | kVerifyVarArgNonZero | kVerifyVarArgRange |
590 kVerifyVarArgRangeNonZero | kVerifyError));
Ian Rogers5fb22a92014-06-13 10:31:28 -0700591 }
592
593 bool GetVerifyIsRuntimeOnly() const {
594 return (kInstructionVerifyFlags[Opcode()] & kVerifyRuntimeOnly) != 0;
jeffhaoba5ebb92011-08-25 17:24:37 -0700595 }
596
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700597 // Get the dex PC of this instruction as a offset in code units from the beginning of insns.
598 uint32_t GetDexPc(const uint16_t* insns) const {
599 return (reinterpret_cast<const uint16_t*>(this) - insns);
600 }
601
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 // Dump decoded version of instruction
Ian Rogers2c8a8572011-10-24 17:11:36 -0700603 std::string DumpString(const DexFile*) const;
604
605 // Dump code_units worth of this instruction, padding to code_units for shorter instructions
606 std::string DumpHex(size_t code_units) const;
Ian Rogersd81871c2011-10-03 13:57:23 -0700607
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800608 // Little-endian dump code_units worth of this instruction, padding to code_units for
609 // shorter instructions
610 std::string DumpHexLE(size_t instr_code_units) const;
611
Sebastien Hertz807a2562013-04-15 09:33:39 +0200612 uint16_t Fetch16(size_t offset) const {
613 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
614 return insns[offset];
615 }
616
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200617 private:
618 size_t SizeInCodeUnitsComplexOpcode() const;
619
Sebastien Hertz807a2562013-04-15 09:33:39 +0200620 uint32_t Fetch32(size_t offset) const {
621 return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16));
622 }
623
624 uint4_t InstA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200625 return InstA(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200626 }
627
628 uint4_t InstB() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200629 return InstB(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200630 }
631
632 uint8_t InstAA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200633 return InstAA(Fetch16(0));
634 }
635
636 uint4_t InstA(uint16_t inst_data) const {
637 DCHECK_EQ(inst_data, Fetch16(0));
638 return static_cast<uint4_t>((inst_data >> 8) & 0x0f);
639 }
640
641 uint4_t InstB(uint16_t inst_data) const {
642 DCHECK_EQ(inst_data, Fetch16(0));
643 return static_cast<uint4_t>(inst_data >> 12);
644 }
645
646 uint8_t InstAA(uint16_t inst_data) const {
647 DCHECK_EQ(inst_data, Fetch16(0));
648 return static_cast<uint8_t>(inst_data >> 8);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200649 }
650
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700651 static const char* const kInstructionNames[];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800652 static Format const kInstructionFormats[];
Aart Bikb1f37532015-06-29 11:03:55 -0700653 static IndexType const kInstructionIndexTypes[];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700654 static int const kInstructionFlags[];
jeffhaoba5ebb92011-08-25 17:24:37 -0700655 static int const kInstructionVerifyFlags[];
Ian Rogersa75a0132012-09-28 11:41:42 -0700656 static int const kInstructionSizeInCodeUnits[];
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700657 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
658};
Ian Rogersa75a0132012-09-28 11:41:42 -0700659std::ostream& operator<<(std::ostream& os, const Instruction::Code& code);
660std::ostream& operator<<(std::ostream& os, const Instruction::Format& format);
661std::ostream& operator<<(std::ostream& os, const Instruction::Flags& flags);
662std::ostream& operator<<(std::ostream& os, const Instruction::VerifyFlag& vflags);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800663
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700664} // namespace art
665
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700666#endif // ART_RUNTIME_DEX_INSTRUCTION_H_