blob: 560e5ff8c23b7dfe88c964de12ec9b895fb832b3 [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
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070091 };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070092
Elliott Hughesadb8c672012-03-06 16:49:32 -080093 enum Format {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070094 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 Carlstrom7934ac22013-07-26 10:54:15 -0700127 kUnconditional = 0x40, // unconditional branch
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700128 };
129
jeffhaoba5ebb92011-08-25 17:24:37 -0700130 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
Ian Rogers29a26482014-05-02 15:27:29 -0700154 static constexpr uint32_t kMaxVarArgRegs = 5;
jeffhaoba5ebb92011-08-25 17:24:37 -0700155
Elliott Hughesadb8c672012-03-06 16:49:32 -0800156 // Returns the size (in 2 byte code units) of this instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700157 size_t SizeInCodeUnits() const {
158 int result = kInstructionSizeInCodeUnits[Opcode()];
159 if (UNLIKELY(result < 0)) {
160 return SizeInCodeUnitsComplexOpcode();
161 } else {
162 return static_cast<size_t>(result);
163 }
164 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700165
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200166 // Reads an instruction out of the stream at the specified address.
167 static const Instruction* At(const uint16_t* code) {
168 DCHECK(code != NULL);
169 return reinterpret_cast<const Instruction*>(code);
170 }
171
172 // Reads an instruction out of the stream from the current address plus an offset.
173 const Instruction* RelativeAt(int32_t offset) const {
174 return At(reinterpret_cast<const uint16_t*>(this) + offset);
175 }
176
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700177 // Returns a pointer to the next instruction in the stream.
Ian Rogersa75a0132012-09-28 11:41:42 -0700178 const Instruction* Next() const {
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200179 return RelativeAt(SizeInCodeUnits());
Ian Rogersa75a0132012-09-28 11:41:42 -0700180 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700181
Jeff Hao9cec2472013-05-14 18:17:06 -0700182 // Returns a pointer to the instruction after this 1xx instruction in the stream.
183 const Instruction* Next_1xx() const {
184 DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200185 return RelativeAt(1);
Jeff Hao9cec2472013-05-14 18:17:06 -0700186 }
187
188 // Returns a pointer to the instruction after this 2xx instruction in the stream.
189 const Instruction* Next_2xx() const {
190 DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k22c);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200191 return RelativeAt(2);
Jeff Hao9cec2472013-05-14 18:17:06 -0700192 }
193
194 // Returns a pointer to the instruction after this 3xx instruction in the stream.
195 const Instruction* Next_3xx() const {
196 DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc);
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200197 return RelativeAt(3);
Jeff Hao9cec2472013-05-14 18:17:06 -0700198 }
199
200 // Returns a pointer to the instruction after this 51l instruction in the stream.
Sebastien Hertz92c607f2013-06-04 16:18:52 +0200201 const Instruction* Next_51l() const {
202 DCHECK(FormatOf(Opcode()) == k51l);
203 return RelativeAt(5);
204 }
Jeff Hao9cec2472013-05-14 18:17:06 -0700205
Elliott Hughesadb8c672012-03-06 16:49:32 -0800206 // Returns the name of this instruction's opcode.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700207 const char* Name() const {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800208 return Instruction::Name(Opcode());
209 }
210
211 // Returns the name of the given opcode.
212 static const char* Name(Code opcode) {
213 return kInstructionNames[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700214 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700215
Sebastien Hertz807a2562013-04-15 09:33:39 +0200216 // VRegA
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700217 bool HasVRegA() const;
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700218 int32_t VRegA() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200219
220 int8_t VRegA_10t() const {
221 return VRegA_10t(Fetch16(0));
222 }
223 uint8_t VRegA_10x() const {
224 return VRegA_10x(Fetch16(0));
225 }
226 uint4_t VRegA_11n() const {
227 return VRegA_11n(Fetch16(0));
228 }
229 uint8_t VRegA_11x() const {
230 return VRegA_11x(Fetch16(0));
231 }
232 uint4_t VRegA_12x() const {
233 return VRegA_12x(Fetch16(0));
234 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200235 int16_t VRegA_20t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200236 uint8_t VRegA_21c() const {
237 return VRegA_21c(Fetch16(0));
238 }
239 uint8_t VRegA_21h() const {
240 return VRegA_21h(Fetch16(0));
241 }
242 uint8_t VRegA_21s() const {
243 return VRegA_21s(Fetch16(0));
244 }
245 uint8_t VRegA_21t() const {
246 return VRegA_21t(Fetch16(0));
247 }
248 uint8_t VRegA_22b() const {
249 return VRegA_22b(Fetch16(0));
250 }
251 uint4_t VRegA_22c() const {
252 return VRegA_22c(Fetch16(0));
253 }
254 uint4_t VRegA_22s() const {
255 return VRegA_22s(Fetch16(0));
256 }
257 uint4_t VRegA_22t() const {
258 return VRegA_22t(Fetch16(0));
259 }
260 uint8_t VRegA_22x() const {
261 return VRegA_22x(Fetch16(0));
262 }
263 uint8_t VRegA_23x() const {
264 return VRegA_23x(Fetch16(0));
265 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200266 int32_t VRegA_30t() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200267 uint8_t VRegA_31c() const {
268 return VRegA_31c(Fetch16(0));
269 }
270 uint8_t VRegA_31i() const {
271 return VRegA_31i(Fetch16(0));
272 }
273 uint8_t VRegA_31t() const {
274 return VRegA_31t(Fetch16(0));
275 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200276 uint16_t VRegA_32x() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200277 uint4_t VRegA_35c() const {
278 return VRegA_35c(Fetch16(0));
279 }
280 uint8_t VRegA_3rc() const {
281 return VRegA_3rc(Fetch16(0));
282 }
283 uint8_t VRegA_51l() const {
284 return VRegA_51l(Fetch16(0));
285 }
286
287 // The following methods return the vA operand for various instruction formats. The "inst_data"
288 // parameter holds the first 16 bits of instruction which the returned value is decoded from.
289 int8_t VRegA_10t(uint16_t inst_data) const;
290 uint8_t VRegA_10x(uint16_t inst_data) const;
291 uint4_t VRegA_11n(uint16_t inst_data) const;
292 uint8_t VRegA_11x(uint16_t inst_data) const;
293 uint4_t VRegA_12x(uint16_t inst_data) const;
294 uint8_t VRegA_21c(uint16_t inst_data) const;
295 uint8_t VRegA_21h(uint16_t inst_data) const;
296 uint8_t VRegA_21s(uint16_t inst_data) const;
297 uint8_t VRegA_21t(uint16_t inst_data) const;
298 uint8_t VRegA_22b(uint16_t inst_data) const;
299 uint4_t VRegA_22c(uint16_t inst_data) const;
300 uint4_t VRegA_22s(uint16_t inst_data) const;
301 uint4_t VRegA_22t(uint16_t inst_data) const;
302 uint8_t VRegA_22x(uint16_t inst_data) const;
303 uint8_t VRegA_23x(uint16_t inst_data) const;
304 uint8_t VRegA_31c(uint16_t inst_data) const;
305 uint8_t VRegA_31i(uint16_t inst_data) const;
306 uint8_t VRegA_31t(uint16_t inst_data) const;
307 uint4_t VRegA_35c(uint16_t inst_data) const;
308 uint8_t VRegA_3rc(uint16_t inst_data) const;
309 uint8_t VRegA_51l(uint16_t inst_data) const;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200310
311 // VRegB
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700312 bool HasVRegB() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700313 int32_t VRegB() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200314
Ian Rogers29a26482014-05-02 15:27:29 -0700315 bool HasWideVRegB() const;
316 uint64_t WideVRegB() const;
317
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200318 int4_t VRegB_11n() const {
319 return VRegB_11n(Fetch16(0));
320 }
321 uint4_t VRegB_12x() const {
322 return VRegB_12x(Fetch16(0));
323 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200324 uint16_t VRegB_21c() const;
325 uint16_t VRegB_21h() const;
326 int16_t VRegB_21s() const;
327 int16_t VRegB_21t() const;
328 uint8_t VRegB_22b() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200329 uint4_t VRegB_22c() const {
330 return VRegB_22c(Fetch16(0));
331 }
332 uint4_t VRegB_22s() const {
333 return VRegB_22s(Fetch16(0));
334 }
335 uint4_t VRegB_22t() const {
336 return VRegB_22t(Fetch16(0));
337 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200338 uint16_t VRegB_22x() const;
339 uint8_t VRegB_23x() const;
340 uint32_t VRegB_31c() const;
341 int32_t VRegB_31i() const;
342 int32_t VRegB_31t() const;
343 uint16_t VRegB_32x() const;
344 uint16_t VRegB_35c() const;
345 uint16_t VRegB_3rc() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700346 uint64_t VRegB_51l() const; // vB_wide
Sebastien Hertz807a2562013-04-15 09:33:39 +0200347
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200348 // The following methods return the vB operand for all instruction formats where it is encoded in
349 // the first 16 bits of instruction. The "inst_data" parameter holds these 16 bits. The returned
350 // value is decoded from it.
351 int4_t VRegB_11n(uint16_t inst_data) const;
352 uint4_t VRegB_12x(uint16_t inst_data) const;
353 uint4_t VRegB_22c(uint16_t inst_data) const;
354 uint4_t VRegB_22s(uint16_t inst_data) const;
355 uint4_t VRegB_22t(uint16_t inst_data) const;
356
Sebastien Hertz807a2562013-04-15 09:33:39 +0200357 // VRegC
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700358 bool HasVRegC() const;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700359 int32_t VRegC() const;
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200360
Sebastien Hertz807a2562013-04-15 09:33:39 +0200361 int8_t VRegC_22b() const;
362 uint16_t VRegC_22c() const;
363 int16_t VRegC_22s() const;
364 int16_t VRegC_22t() const;
365 uint8_t VRegC_23x() const;
366 uint4_t VRegC_35c() const;
367 uint16_t VRegC_3rc() const;
368
369 // Fills the given array with the 'arg' array of the instruction.
Ian Rogers29a26482014-05-02 15:27:29 -0700370 bool HasVarArgs() const;
371 void GetVarArgs(uint32_t args[kMaxVarArgRegs], uint16_t inst_data) const;
372 void GetVarArgs(uint32_t args[kMaxVarArgRegs]) const {
373 return GetVarArgs(args, Fetch16(0));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200374 }
Sebastien Hertz807a2562013-04-15 09:33:39 +0200375
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200376 // Returns the opcode field of the instruction. The given "inst_data" parameter must be the first
377 // 16 bits of instruction.
378 Code Opcode(uint16_t inst_data) const {
379 DCHECK_EQ(inst_data, Fetch16(0));
380 return static_cast<Code>(inst_data & 0xFF);
381 }
382
383 // Returns the opcode field of the instruction from the first 16 bits of instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700384 Code Opcode() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200385 return Opcode(Fetch16(0));
Ian Rogersa75a0132012-09-28 11:41:42 -0700386 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700387
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200388 void SetOpcode(Code opcode) {
389 DCHECK_LT(static_cast<uint16_t>(opcode), 256u);
390 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
391 insns[0] = (insns[0] & 0xff00) | static_cast<uint16_t>(opcode);
392 }
393
Sebastien Hertz543959c2013-07-03 12:00:19 +0200394 void SetVRegA_10x(uint8_t val) {
395 DCHECK(FormatOf(Opcode()) == k10x);
396 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
397 insns[0] = (val << 8) | (insns[0] & 0x00ff);
398 }
399
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200400 void SetVRegB_3rc(uint16_t val) {
401 DCHECK(FormatOf(Opcode()) == k3rc);
402 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
403 insns[1] = val;
404 }
405
406 void SetVRegB_35c(uint16_t val) {
407 DCHECK(FormatOf(Opcode()) == k35c);
408 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
409 insns[1] = val;
410 }
411
412 void SetVRegC_22c(uint16_t val) {
413 DCHECK(FormatOf(Opcode()) == k22c);
414 uint16_t* insns = reinterpret_cast<uint16_t*>(this);
415 insns[1] = val;
416 }
417
Elliott Hughesadb8c672012-03-06 16:49:32 -0800418 // Returns the format of the given opcode.
419 static Format FormatOf(Code opcode) {
420 return kInstructionFormats[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700421 }
422
Elliott Hughesadb8c672012-03-06 16:49:32 -0800423 // Returns the flags for the given opcode.
Ian Rogersa75a0132012-09-28 11:41:42 -0700424 static int FlagsOf(Code opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800425 return kInstructionFlags[opcode];
jeffhaobdb76512011-09-07 11:43:16 -0700426 }
427
buzbeeb1f1d642014-02-27 12:55:32 -0800428 // Return the verify flags for the given opcode.
429 static int VerifyFlagsOf(Code opcode) {
430 return kInstructionVerifyFlags[opcode];
431 }
432
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700433 // Returns true if this instruction is a branch.
434 bool IsBranch() const {
435 return (kInstructionFlags[Opcode()] & kBranch) != 0;
436 }
437
TDYa127526643e2012-05-26 01:01:48 -0700438 // Returns true if this instruction is a unconditional branch.
439 bool IsUnconditional() const {
440 return (kInstructionFlags[Opcode()] & kUnconditional) != 0;
441 }
442
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700443 // Returns the branch offset if this instruction is a branch.
444 int32_t GetTargetOffset() const;
445
446 // Returns true if the instruction allows control flow to go to the following instruction.
447 bool CanFlowThrough() const;
448
jeffhaoba5ebb92011-08-25 17:24:37 -0700449 // Returns true if this instruction is a switch.
450 bool IsSwitch() const {
451 return (kInstructionFlags[Opcode()] & kSwitch) != 0;
452 }
453
454 // Returns true if this instruction can throw.
455 bool IsThrow() const {
456 return (kInstructionFlags[Opcode()] & kThrow) != 0;
457 }
458
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700459 // Determine if the instruction is any of 'return' instructions.
460 bool IsReturn() const {
461 return (kInstructionFlags[Opcode()] & kReturn) != 0;
462 }
463
464 // Determine if this instruction ends execution of its basic block.
465 bool IsBasicBlockEnd() const {
466 return IsBranch() || IsReturn() || Opcode() == THROW;
467 }
468
469 // Determine if this instruction is an invoke.
470 bool IsInvoke() const {
471 return (kInstructionFlags[Opcode()] & kInvoke) != 0;
472 }
473
jeffhaoba5ebb92011-08-25 17:24:37 -0700474 int GetVerifyTypeArgumentA() const {
475 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegA | kVerifyRegAWide));
476 }
477
478 int GetVerifyTypeArgumentB() const {
479 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegB | kVerifyRegBField | kVerifyRegBMethod |
480 kVerifyRegBNewInstance | kVerifyRegBString | kVerifyRegBType | kVerifyRegBWide));
481 }
482
483 int GetVerifyTypeArgumentC() const {
484 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegC | kVerifyRegCField |
jeffhao3bb32462012-02-01 16:12:27 -0800485 kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700486 }
487
488 int GetVerifyExtraFlags() const {
489 return (kInstructionVerifyFlags[Opcode()] & (kVerifyArrayData | kVerifyBranchTarget |
490 kVerifySwitchTargets | kVerifyVarArg | kVerifyVarArgRange | kVerifyError));
491 }
492
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700493 // Get the dex PC of this instruction as a offset in code units from the beginning of insns.
494 uint32_t GetDexPc(const uint16_t* insns) const {
495 return (reinterpret_cast<const uint16_t*>(this) - insns);
496 }
497
Ian Rogersd81871c2011-10-03 13:57:23 -0700498 // Dump decoded version of instruction
Ian Rogers2c8a8572011-10-24 17:11:36 -0700499 std::string DumpString(const DexFile*) const;
500
501 // Dump code_units worth of this instruction, padding to code_units for shorter instructions
502 std::string DumpHex(size_t code_units) const;
Ian Rogersd81871c2011-10-03 13:57:23 -0700503
Sebastien Hertz807a2562013-04-15 09:33:39 +0200504 uint16_t Fetch16(size_t offset) const {
505 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
506 return insns[offset];
507 }
508
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200509 private:
510 size_t SizeInCodeUnitsComplexOpcode() const;
511
Sebastien Hertz807a2562013-04-15 09:33:39 +0200512 uint32_t Fetch32(size_t offset) const {
513 return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16));
514 }
515
516 uint4_t InstA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200517 return InstA(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200518 }
519
520 uint4_t InstB() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200521 return InstB(Fetch16(0));
Sebastien Hertz807a2562013-04-15 09:33:39 +0200522 }
523
524 uint8_t InstAA() const {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200525 return InstAA(Fetch16(0));
526 }
527
528 uint4_t InstA(uint16_t inst_data) const {
529 DCHECK_EQ(inst_data, Fetch16(0));
530 return static_cast<uint4_t>((inst_data >> 8) & 0x0f);
531 }
532
533 uint4_t InstB(uint16_t inst_data) const {
534 DCHECK_EQ(inst_data, Fetch16(0));
535 return static_cast<uint4_t>(inst_data >> 12);
536 }
537
538 uint8_t InstAA(uint16_t inst_data) const {
539 DCHECK_EQ(inst_data, Fetch16(0));
540 return static_cast<uint8_t>(inst_data >> 8);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200541 }
542
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700543 static const char* const kInstructionNames[];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800544 static Format const kInstructionFormats[];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700545 static int const kInstructionFlags[];
jeffhaoba5ebb92011-08-25 17:24:37 -0700546 static int const kInstructionVerifyFlags[];
Ian Rogersa75a0132012-09-28 11:41:42 -0700547 static int const kInstructionSizeInCodeUnits[];
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700548 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
549};
Ian Rogersa75a0132012-09-28 11:41:42 -0700550std::ostream& operator<<(std::ostream& os, const Instruction::Code& code);
551std::ostream& operator<<(std::ostream& os, const Instruction::Format& format);
552std::ostream& operator<<(std::ostream& os, const Instruction::Flags& flags);
553std::ostream& operator<<(std::ostream& os, const Instruction::VerifyFlag& vflags);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800554
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700555} // namespace art
556
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700557#endif // ART_RUNTIME_DEX_INSTRUCTION_H_