blob: 72802e49115ce7efdbef48aad7e82d007f291994 [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.
38 enum {
39 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 Rogersa75a0132012-09-28 11:41:42 -070082 // TODO: the code layout below is deliberate to avoid this enum being picked up by
83 // generate-operator-out.py.
84 enum Code
Brian Carlstrom7934ac22013-07-26 10:54:15 -070085 { // NOLINT(whitespace/braces)
jeffhaoba5ebb92011-08-25 17:24:37 -070086#define INSTRUCTION_ENUM(opcode, cname, p, f, r, i, a, v) cname = opcode,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087#include "dex_instruction_list.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070088 DEX_INSTRUCTION_LIST(INSTRUCTION_ENUM)
Carl Shapirod84f49c2011-06-29 00:27:46 -070089#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070090#undef INSTRUCTION_ENUM
Ian Rogersf72a11d2014-10-30 15:41:08 -070091 RSUB_INT_LIT16 = RSUB_INT,
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070092 };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070093
Elliott Hughesadb8c672012-03-06 16:49:32 -080094 enum Format {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070095 k10x, // op
96 k12x, // op vA, vB
97 k11n, // op vA, #+B
98 k11x, // op vAA
99 k10t, // op +AA
100 k20t, // op +AAAA
101 k22x, // op vAA, vBBBB
102 k21t, // op vAA, +BBBB
103 k21s, // op vAA, #+BBBB
104 k21h, // op vAA, #+BBBB00000[00000000]
105 k21c, // op vAA, thing@BBBB
106 k23x, // op vAA, vBB, vCC
107 k22b, // op vAA, vBB, #+CC
108 k22t, // op vA, vB, +CCCC
109 k22s, // op vA, vB, #+CCCC
110 k22c, // op vA, vB, thing@CCCC
111 k32x, // op vAAAA, vBBBB
112 k30t, // op +AAAAAAAA
113 k31t, // op vAA, +BBBBBBBB
114 k31i, // op vAA, #+BBBBBBBB
115 k31c, // op vAA, thing@BBBBBBBB
116 k35c, // op {vC, vD, vE, vF, vG}, thing@BBBB (B: count, A: vG)
117 k3rc, // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
118 k51l, // op vAA, #+BBBBBBBBBBBBBBBB
119 };
120
121 enum Flags {
Jean Christophe Beylerc3db20b2014-05-05 21:09:40 -0700122 kBranch = 0x000001, // conditional or unconditional branch
123 kContinue = 0x000002, // flow can continue to next statement
124 kSwitch = 0x000004, // switch statement
125 kThrow = 0x000008, // could cause an exception to be thrown
126 kReturn = 0x000010, // returns, no additional statements
127 kInvoke = 0x000020, // a flavor of invoke
128 kUnconditional = 0x000040, // unconditional branch
129 kAdd = 0x000080, // addition
130 kSubtract = 0x000100, // subtract
131 kMultiply = 0x000200, // multiply
132 kDivide = 0x000400, // division
133 kRemainder = 0x000800, // remainder
134 kAnd = 0x001000, // and
135 kOr = 0x002000, // or
136 kXor = 0x004000, // xor
137 kShl = 0x008000, // shl
138 kShr = 0x010000, // shr
139 kUshr = 0x020000, // ushr
140 kCast = 0x040000, // cast
141 kStore = 0x080000, // store opcode
142 kLoad = 0x100000, // load opcode
143 kClobber = 0x200000, // clobbers memory in a big way (not just a write)
144 kRegCFieldOrConstant = 0x400000, // is the third virtual register a field or literal constant (vC)
145 kRegBFieldOrConstant = 0x800000, // is the second virtual register a field or literal constant (vB)
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700146 };
147
jeffhaoba5ebb92011-08-25 17:24:37 -0700148 enum VerifyFlag {
Andreas Gampec3314312014-06-19 18:13:29 -0700149 kVerifyNone = 0x000000,
150 kVerifyRegA = 0x000001,
151 kVerifyRegAWide = 0x000002,
152 kVerifyRegB = 0x000004,
153 kVerifyRegBField = 0x000008,
154 kVerifyRegBMethod = 0x000010,
155 kVerifyRegBNewInstance = 0x000020,
156 kVerifyRegBString = 0x000040,
157 kVerifyRegBType = 0x000080,
158 kVerifyRegBWide = 0x000100,
159 kVerifyRegC = 0x000200,
160 kVerifyRegCField = 0x000400,
161 kVerifyRegCNewArray = 0x000800,
162 kVerifyRegCType = 0x001000,
163 kVerifyRegCWide = 0x002000,
164 kVerifyArrayData = 0x004000,
165 kVerifyBranchTarget = 0x008000,
166 kVerifySwitchTargets = 0x010000,
167 kVerifyVarArg = 0x020000,
168 kVerifyVarArgNonZero = 0x040000,
169 kVerifyVarArgRange = 0x080000,
170 kVerifyVarArgRangeNonZero = 0x100000,
171 kVerifyRuntimeOnly = 0x200000,
172 kVerifyError = 0x400000,
jeffhaoba5ebb92011-08-25 17:24:37 -0700173 };
174
Ian Rogers29a26482014-05-02 15:27:29 -0700175 static constexpr uint32_t kMaxVarArgRegs = 5;
jeffhaoba5ebb92011-08-25 17:24:37 -0700176
Elliott Hughesadb8c672012-03-06 16:49:32 -0800177 // Returns the size (in 2 byte code units) of this instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700178 size_t SizeInCodeUnits() const {
179 int result = kInstructionSizeInCodeUnits[Opcode()];
180 if (UNLIKELY(result < 0)) {
181 return SizeInCodeUnitsComplexOpcode();
182 } else {
183 return static_cast<size_t>(result);
184 }
185 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700186
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200187 // Reads an instruction out of the stream at the specified address.
188 static const Instruction* At(const uint16_t* code) {
189 DCHECK(code != NULL);
190 return reinterpret_cast<const Instruction*>(code);
191 }
192
193 // Reads an instruction out of the stream from the current address plus an offset.
Ian Rogers7b078e82014-09-10 14:44:24 -0700194 const Instruction* RelativeAt(int32_t offset) const WARN_UNUSED {
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200195 return At(reinterpret_cast<const uint16_t*>(this) + offset);
196 }
197
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700198 // Returns a pointer to the next instruction in the stream.
Ian Rogersa75a0132012-09-28 11:41:42 -0700199 const Instruction* Next() const {
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200200 return RelativeAt(SizeInCodeUnits());
Ian Rogersa75a0132012-09-28 11:41:42 -0700201 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700202
Jeff Hao9cec2472013-05-14 18:17:06 -0700203 // Returns a pointer to the instruction after this 1xx instruction in the stream.
204 const Instruction* Next_1xx() const {
205 DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200206 return RelativeAt(1);
Jeff Hao9cec2472013-05-14 18:17:06 -0700207 }
208
209 // Returns a pointer to the instruction after this 2xx instruction in the stream.
210 const Instruction* Next_2xx() const {
211 DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k22c);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200212 return RelativeAt(2);
Jeff Hao9cec2472013-05-14 18:17:06 -0700213 }
214
215 // Returns a pointer to the instruction after this 3xx instruction in the stream.
216 const Instruction* Next_3xx() const {
217 DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200218 return RelativeAt(3);
Jeff Hao9cec2472013-05-14 18:17:06 -0700219 }
220
221 // Returns a pointer to the instruction after this 51l instruction in the stream.
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200222 const Instruction* Next_51l() const {
223 DCHECK(FormatOf(Opcode()) == k51l);
224 return RelativeAt(5);
225 }
Jeff Hao9cec2472013-05-14 18:17:06 -0700226
Elliott Hughesadb8c672012-03-06 16:49:32 -0800227 // Returns the name of this instruction's opcode.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700228 const char* Name() const {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800229 return Instruction::Name(Opcode());
230 }
231
232 // Returns the name of the given opcode.
233 static const char* Name(Code opcode) {
234 return kInstructionNames[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700235 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700236
Sebastien Hertz807a2562013-04-15 09:33:39 +0200237 // VRegA
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700238 bool HasVRegA() const;
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700239 int32_t VRegA() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200240
241 int8_t VRegA_10t() const {
242 return VRegA_10t(Fetch16(0));
243 }
244 uint8_t VRegA_10x() const {
245 return VRegA_10x(Fetch16(0));
246 }
247 uint4_t VRegA_11n() const {
248 return VRegA_11n(Fetch16(0));
249 }
250 uint8_t VRegA_11x() const {
251 return VRegA_11x(Fetch16(0));
252 }
253 uint4_t VRegA_12x() const {
254 return VRegA_12x(Fetch16(0));
255 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200256 int16_t VRegA_20t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200257 uint8_t VRegA_21c() const {
258 return VRegA_21c(Fetch16(0));
259 }
260 uint8_t VRegA_21h() const {
261 return VRegA_21h(Fetch16(0));
262 }
263 uint8_t VRegA_21s() const {
264 return VRegA_21s(Fetch16(0));
265 }
266 uint8_t VRegA_21t() const {
267 return VRegA_21t(Fetch16(0));
268 }
269 uint8_t VRegA_22b() const {
270 return VRegA_22b(Fetch16(0));
271 }
272 uint4_t VRegA_22c() const {
273 return VRegA_22c(Fetch16(0));
274 }
275 uint4_t VRegA_22s() const {
276 return VRegA_22s(Fetch16(0));
277 }
278 uint4_t VRegA_22t() const {
279 return VRegA_22t(Fetch16(0));
280 }
281 uint8_t VRegA_22x() const {
282 return VRegA_22x(Fetch16(0));
283 }
284 uint8_t VRegA_23x() const {
285 return VRegA_23x(Fetch16(0));
286 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200287 int32_t VRegA_30t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200288 uint8_t VRegA_31c() const {
289 return VRegA_31c(Fetch16(0));
290 }
291 uint8_t VRegA_31i() const {
292 return VRegA_31i(Fetch16(0));
293 }
294 uint8_t VRegA_31t() const {
295 return VRegA_31t(Fetch16(0));
296 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200297 uint16_t VRegA_32x() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200298 uint4_t VRegA_35c() const {
299 return VRegA_35c(Fetch16(0));
300 }
301 uint8_t VRegA_3rc() const {
302 return VRegA_3rc(Fetch16(0));
303 }
304 uint8_t VRegA_51l() const {
305 return VRegA_51l(Fetch16(0));
306 }
307
308 // The following methods return the vA operand for various instruction formats. The "inst_data"
309 // parameter holds the first 16 bits of instruction which the returned value is decoded from.
310 int8_t VRegA_10t(uint16_t inst_data) const;
311 uint8_t VRegA_10x(uint16_t inst_data) const;
312 uint4_t VRegA_11n(uint16_t inst_data) const;
313 uint8_t VRegA_11x(uint16_t inst_data) const;
314 uint4_t VRegA_12x(uint16_t inst_data) const;
315 uint8_t VRegA_21c(uint16_t inst_data) const;
316 uint8_t VRegA_21h(uint16_t inst_data) const;
317 uint8_t VRegA_21s(uint16_t inst_data) const;
318 uint8_t VRegA_21t(uint16_t inst_data) const;
319 uint8_t VRegA_22b(uint16_t inst_data) const;
320 uint4_t VRegA_22c(uint16_t inst_data) const;
321 uint4_t VRegA_22s(uint16_t inst_data) const;
322 uint4_t VRegA_22t(uint16_t inst_data) const;
323 uint8_t VRegA_22x(uint16_t inst_data) const;
324 uint8_t VRegA_23x(uint16_t inst_data) const;
325 uint8_t VRegA_31c(uint16_t inst_data) const;
326 uint8_t VRegA_31i(uint16_t inst_data) const;
327 uint8_t VRegA_31t(uint16_t inst_data) const;
328 uint4_t VRegA_35c(uint16_t inst_data) const;
329 uint8_t VRegA_3rc(uint16_t inst_data) const;
330 uint8_t VRegA_51l(uint16_t inst_data) const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200331
332 // VRegB
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700333 bool HasVRegB() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700334 int32_t VRegB() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200335
Ian Rogers29a26482014-05-02 15:27:29 -0700336 bool HasWideVRegB() const;
337 uint64_t WideVRegB() const;
338
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200339 int4_t VRegB_11n() const {
340 return VRegB_11n(Fetch16(0));
341 }
342 uint4_t VRegB_12x() const {
343 return VRegB_12x(Fetch16(0));
344 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200345 uint16_t VRegB_21c() const;
346 uint16_t VRegB_21h() const;
347 int16_t VRegB_21s() const;
348 int16_t VRegB_21t() const;
349 uint8_t VRegB_22b() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200350 uint4_t VRegB_22c() const {
351 return VRegB_22c(Fetch16(0));
352 }
353 uint4_t VRegB_22s() const {
354 return VRegB_22s(Fetch16(0));
355 }
356 uint4_t VRegB_22t() const {
357 return VRegB_22t(Fetch16(0));
358 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200359 uint16_t VRegB_22x() const;
360 uint8_t VRegB_23x() const;
361 uint32_t VRegB_31c() const;
362 int32_t VRegB_31i() const;
363 int32_t VRegB_31t() const;
364 uint16_t VRegB_32x() const;
365 uint16_t VRegB_35c() const;
366 uint16_t VRegB_3rc() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700367 uint64_t VRegB_51l() const; // vB_wide
Sebastien Hertz807a2562013-04-15 09:33:39 +0200368
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200369 // The following methods return the vB operand for all instruction formats where it is encoded in
370 // the first 16 bits of instruction. The "inst_data" parameter holds these 16 bits. The returned
371 // value is decoded from it.
372 int4_t VRegB_11n(uint16_t inst_data) const;
373 uint4_t VRegB_12x(uint16_t inst_data) const;
374 uint4_t VRegB_22c(uint16_t inst_data) const;
375 uint4_t VRegB_22s(uint16_t inst_data) const;
376 uint4_t VRegB_22t(uint16_t inst_data) const;
377
Sebastien Hertz807a2562013-04-15 09:33:39 +0200378 // VRegC
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700379 bool HasVRegC() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700380 int32_t VRegC() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200381
Sebastien Hertz807a2562013-04-15 09:33:39 +0200382 int8_t VRegC_22b() const;
383 uint16_t VRegC_22c() const;
384 int16_t VRegC_22s() const;
385 int16_t VRegC_22t() const;
386 uint8_t VRegC_23x() const;
387 uint4_t VRegC_35c() const;
388 uint16_t VRegC_3rc() const;
389
390 // Fills the given array with the 'arg' array of the instruction.
Ian Rogers29a26482014-05-02 15:27:29 -0700391 bool HasVarArgs() const;
392 void GetVarArgs(uint32_t args[kMaxVarArgRegs], uint16_t inst_data) const;
393 void GetVarArgs(uint32_t args[kMaxVarArgRegs]) const {
394 return GetVarArgs(args, Fetch16(0));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200395 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200396
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200397 // Returns the opcode field of the instruction. The given "inst_data" parameter must be the first
398 // 16 bits of instruction.
399 Code Opcode(uint16_t inst_data) const {
400 DCHECK_EQ(inst_data, Fetch16(0));
401 return static_cast<Code>(inst_data & 0xFF);
402 }
403
404 // Returns the opcode field of the instruction from the first 16 bits of instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700405 Code Opcode() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200406 return Opcode(Fetch16(0));
Ian Rogersa75a0132012-09-28 11:41:42 -0700407 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700408
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200409 void SetOpcode(Code opcode) {
410 DCHECK_LT(static_cast<uint16_t>(opcode), 256u);
411 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
412 insns[0] = (insns[0] & 0xff00) | static_cast<uint16_t>(opcode);
413 }
414
Sebastien Hertz543959c2013-07-03 12:00:19 +0200415 void SetVRegA_10x(uint8_t val) {
416 DCHECK(FormatOf(Opcode()) == k10x);
417 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
418 insns[0] = (val << 8) | (insns[0] & 0x00ff);
419 }
420
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200421 void SetVRegB_3rc(uint16_t val) {
422 DCHECK(FormatOf(Opcode()) == k3rc);
423 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
424 insns[1] = val;
425 }
426
427 void SetVRegB_35c(uint16_t val) {
428 DCHECK(FormatOf(Opcode()) == k35c);
429 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
430 insns[1] = val;
431 }
432
433 void SetVRegC_22c(uint16_t val) {
434 DCHECK(FormatOf(Opcode()) == k22c);
435 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
436 insns[1] = val;
437 }
438
Elliott Hughesadb8c672012-03-06 16:49:32 -0800439 // Returns the format of the given opcode.
440 static Format FormatOf(Code opcode) {
441 return kInstructionFormats[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700442 }
443
Elliott Hughesadb8c672012-03-06 16:49:32 -0800444 // Returns the flags for the given opcode.
Ian Rogersa75a0132012-09-28 11:41:42 -0700445 static int FlagsOf(Code opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800446 return kInstructionFlags[opcode];
jeffhaobdb76512011-09-07 11:43:16 -0700447 }
448
buzbeeb1f1d642014-02-27 12:55:32 -0800449 // Return the verify flags for the given opcode.
450 static int VerifyFlagsOf(Code opcode) {
451 return kInstructionVerifyFlags[opcode];
452 }
453
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700454 // Returns true if this instruction is a branch.
455 bool IsBranch() const {
456 return (kInstructionFlags[Opcode()] & kBranch) != 0;
457 }
458
TDYa127526643e2012-05-26 01:01:48 -0700459 // Returns true if this instruction is a unconditional branch.
460 bool IsUnconditional() const {
461 return (kInstructionFlags[Opcode()] & kUnconditional) != 0;
462 }
463
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700464 // Returns the branch offset if this instruction is a branch.
465 int32_t GetTargetOffset() const;
466
467 // Returns true if the instruction allows control flow to go to the following instruction.
468 bool CanFlowThrough() const;
469
jeffhaoba5ebb92011-08-25 17:24:37 -0700470 // Returns true if this instruction is a switch.
471 bool IsSwitch() const {
472 return (kInstructionFlags[Opcode()] & kSwitch) != 0;
473 }
474
475 // Returns true if this instruction can throw.
476 bool IsThrow() const {
477 return (kInstructionFlags[Opcode()] & kThrow) != 0;
478 }
479
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700480 // Determine if the instruction is any of 'return' instructions.
481 bool IsReturn() const {
482 return (kInstructionFlags[Opcode()] & kReturn) != 0;
483 }
484
485 // Determine if this instruction ends execution of its basic block.
486 bool IsBasicBlockEnd() const {
487 return IsBranch() || IsReturn() || Opcode() == THROW;
488 }
489
490 // Determine if this instruction is an invoke.
491 bool IsInvoke() const {
492 return (kInstructionFlags[Opcode()] & kInvoke) != 0;
493 }
494
jeffhaoba5ebb92011-08-25 17:24:37 -0700495 int GetVerifyTypeArgumentA() const {
496 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegA | kVerifyRegAWide));
497 }
498
499 int GetVerifyTypeArgumentB() const {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700500 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegB | kVerifyRegBField |
501 kVerifyRegBMethod | kVerifyRegBNewInstance | kVerifyRegBString | kVerifyRegBType |
502 kVerifyRegBWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700503 }
504
505 int GetVerifyTypeArgumentC() const {
506 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegC | kVerifyRegCField |
Ian Rogers5fb22a92014-06-13 10:31:28 -0700507 kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700508 }
509
510 int GetVerifyExtraFlags() const {
511 return (kInstructionVerifyFlags[Opcode()] & (kVerifyArrayData | kVerifyBranchTarget |
Andreas Gampec3314312014-06-19 18:13:29 -0700512 kVerifySwitchTargets | kVerifyVarArg | kVerifyVarArgNonZero | kVerifyVarArgRange |
513 kVerifyVarArgRangeNonZero | kVerifyError));
Ian Rogers5fb22a92014-06-13 10:31:28 -0700514 }
515
516 bool GetVerifyIsRuntimeOnly() const {
517 return (kInstructionVerifyFlags[Opcode()] & kVerifyRuntimeOnly) != 0;
jeffhaoba5ebb92011-08-25 17:24:37 -0700518 }
519
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700520 // Get the dex PC of this instruction as a offset in code units from the beginning of insns.
521 uint32_t GetDexPc(const uint16_t* insns) const {
522 return (reinterpret_cast<const uint16_t*>(this) - insns);
523 }
524
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 // Dump decoded version of instruction
Ian Rogers2c8a8572011-10-24 17:11:36 -0700526 std::string DumpString(const DexFile*) const;
527
528 // Dump code_units worth of this instruction, padding to code_units for shorter instructions
529 std::string DumpHex(size_t code_units) const;
Ian Rogersd81871c2011-10-03 13:57:23 -0700530
Sebastien Hertz807a2562013-04-15 09:33:39 +0200531 uint16_t Fetch16(size_t offset) const {
532 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
533 return insns[offset];
534 }
535
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200536 private:
537 size_t SizeInCodeUnitsComplexOpcode() const;
538
Sebastien Hertz807a2562013-04-15 09:33:39 +0200539 uint32_t Fetch32(size_t offset) const {
540 return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16));
541 }
542
543 uint4_t InstA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200544 return InstA(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200545 }
546
547 uint4_t InstB() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200548 return InstB(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200549 }
550
551 uint8_t InstAA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200552 return InstAA(Fetch16(0));
553 }
554
555 uint4_t InstA(uint16_t inst_data) const {
556 DCHECK_EQ(inst_data, Fetch16(0));
557 return static_cast<uint4_t>((inst_data >> 8) & 0x0f);
558 }
559
560 uint4_t InstB(uint16_t inst_data) const {
561 DCHECK_EQ(inst_data, Fetch16(0));
562 return static_cast<uint4_t>(inst_data >> 12);
563 }
564
565 uint8_t InstAA(uint16_t inst_data) const {
566 DCHECK_EQ(inst_data, Fetch16(0));
567 return static_cast<uint8_t>(inst_data >> 8);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200568 }
569
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700570 static const char* const kInstructionNames[];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800571 static Format const kInstructionFormats[];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700572 static int const kInstructionFlags[];
jeffhaoba5ebb92011-08-25 17:24:37 -0700573 static int const kInstructionVerifyFlags[];
Ian Rogersa75a0132012-09-28 11:41:42 -0700574 static int const kInstructionSizeInCodeUnits[];
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700575 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
576};
Ian Rogersa75a0132012-09-28 11:41:42 -0700577std::ostream& operator<<(std::ostream& os, const Instruction::Code& code);
578std::ostream& operator<<(std::ostream& os, const Instruction::Format& format);
579std::ostream& operator<<(std::ostream& os, const Instruction::Flags& flags);
580std::ostream& operator<<(std::ostream& os, const Instruction::VerifyFlag& vflags);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800581
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700582} // namespace art
583
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700584#endif // ART_RUNTIME_DEX_INSTRUCTION_H_