blob: 218acb6fe10ee884344911c0924f5c82a550723f [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
17#ifndef ART_SRC_DEX_INSTRUCTION_H_
18#define ART_SRC_DEX_INSTRUCTION_H_
19
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[];
49 private:
50 DISALLOW_COPY_AND_ASSIGN(PackedSwitchPayload);
51 };
52
Ian Rogersdf1ce912012-11-27 17:07:11 -080053 struct PACKED(4) SparseSwitchPayload {
Logan Chien19c350a2012-05-01 19:21:32 +080054 const uint16_t ident;
55 const uint16_t case_count;
56 const int32_t keys_and_targets[];
57
58 public:
59 const int32_t* GetKeys() const {
60 return keys_and_targets;
61 }
62
63 const int32_t* GetTargets() const {
64 return keys_and_targets + case_count;
65 }
66
67 private:
68 DISALLOW_COPY_AND_ASSIGN(SparseSwitchPayload);
69 };
70
Ian Rogersdf1ce912012-11-27 17:07:11 -080071 struct PACKED(4) ArrayDataPayload {
Logan Chien19c350a2012-05-01 19:21:32 +080072 const uint16_t ident;
73 const uint16_t element_width;
74 const uint32_t element_count;
75 const uint8_t data[];
76 private:
77 DISALLOW_COPY_AND_ASSIGN(ArrayDataPayload);
78 };
79
Ian Rogersa75a0132012-09-28 11:41:42 -070080 // TODO: the code layout below is deliberate to avoid this enum being picked up by
81 // generate-operator-out.py.
82 enum Code
83 {
jeffhaoba5ebb92011-08-25 17:24:37 -070084#define INSTRUCTION_ENUM(opcode, cname, p, f, r, i, a, v) cname = opcode,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070085#include "dex_instruction_list.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070086 DEX_INSTRUCTION_LIST(INSTRUCTION_ENUM)
Carl Shapirod84f49c2011-06-29 00:27:46 -070087#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070088#undef INSTRUCTION_ENUM
Ian Rogersa75a0132012-09-28 11:41:42 -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
115 k51l, // op vAA, #+BBBBBBBBBBBBBBBB
116 };
117
118 enum Flags {
119 kBranch = 0x01, // conditional or unconditional branch
120 kContinue = 0x02, // flow can continue to next statement
121 kSwitch = 0x04, // switch statement
122 kThrow = 0x08, // could cause an exception to be thrown
123 kReturn = 0x10, // returns, no additional statements
124 kInvoke = 0x20, // a flavor of invoke
TDYa127526643e2012-05-26 01:01:48 -0700125 kUnconditional = 0x40, // unconditional branch
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700126 };
127
jeffhaoba5ebb92011-08-25 17:24:37 -0700128 enum VerifyFlag {
129 kVerifyNone = 0x00000,
130 kVerifyRegA = 0x00001,
131 kVerifyRegAWide = 0x00002,
132 kVerifyRegB = 0x00004,
133 kVerifyRegBField = 0x00008,
134 kVerifyRegBMethod = 0x00010,
135 kVerifyRegBNewInstance = 0x00020,
136 kVerifyRegBString = 0x00040,
137 kVerifyRegBType = 0x00080,
138 kVerifyRegBWide = 0x00100,
139 kVerifyRegC = 0x00200,
140 kVerifyRegCField = 0x00400,
141 kVerifyRegCNewArray = 0x00800,
142 kVerifyRegCType = 0x01000,
143 kVerifyRegCWide = 0x02000,
144 kVerifyArrayData = 0x04000,
145 kVerifyBranchTarget = 0x08000,
146 kVerifySwitchTargets = 0x10000,
147 kVerifyVarArg = 0x20000,
148 kVerifyVarArgRange = 0x40000,
149 kVerifyError = 0x80000,
150 };
151
152 // Decodes this instruction, populating its arguments.
153 void Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const;
154
Elliott Hughesadb8c672012-03-06 16:49:32 -0800155 // Returns the size (in 2 byte code units) of this instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700156 size_t SizeInCodeUnits() const {
157 int result = kInstructionSizeInCodeUnits[Opcode()];
158 if (UNLIKELY(result < 0)) {
159 return SizeInCodeUnitsComplexOpcode();
160 } else {
161 return static_cast<size_t>(result);
162 }
163 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700164
165 // Returns a pointer to the next instruction in the stream.
Ian Rogersa75a0132012-09-28 11:41:42 -0700166 const Instruction* Next() const {
167 size_t current_size_in_bytes = SizeInCodeUnits() * sizeof(uint16_t);
168 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
169 return reinterpret_cast<const Instruction*>(ptr + current_size_in_bytes);
170 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700171
Jeff Hao9cec2472013-05-14 18:17:06 -0700172 // Returns a pointer to the instruction after this 1xx instruction in the stream.
173 const Instruction* Next_1xx() const {
174 DCHECK(FormatOf(Opcode()) >= k10x && FormatOf(Opcode()) <= k10t);
175 size_t current_size_in_bytes = 1 * sizeof(uint16_t);
176 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
177 return reinterpret_cast<const Instruction*>(ptr + current_size_in_bytes);
178 }
179
180 // Returns a pointer to the instruction after this 2xx instruction in the stream.
181 const Instruction* Next_2xx() const {
182 DCHECK(FormatOf(Opcode()) >= k20t && FormatOf(Opcode()) <= k22c);
183 size_t current_size_in_bytes = 2 * sizeof(uint16_t);
184 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
185 return reinterpret_cast<const Instruction*>(ptr + current_size_in_bytes);
186 }
187
188 // Returns a pointer to the instruction after this 3xx instruction in the stream.
189 const Instruction* Next_3xx() const {
190 DCHECK(FormatOf(Opcode()) >= k32x && FormatOf(Opcode()) <= k3rc);
191 size_t current_size_in_bytes = 3 * sizeof(uint16_t);
192 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
193 return reinterpret_cast<const Instruction*>(ptr + current_size_in_bytes);
194 }
195
196 // Returns a pointer to the instruction after this 51l instruction in the stream.
197 const Instruction* Next_51l() const;
198
Elliott Hughesadb8c672012-03-06 16:49:32 -0800199 // Returns the name of this instruction's opcode.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700200 const char* Name() const {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800201 return Instruction::Name(Opcode());
202 }
203
204 // Returns the name of the given opcode.
205 static const char* Name(Code opcode) {
206 return kInstructionNames[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700207 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700208
Sebastien Hertz807a2562013-04-15 09:33:39 +0200209 // VRegA
210 int8_t VRegA_10t() const;
211 uint4_t VRegA_11n() const;
212 uint8_t VRegA_11x() const;
213 uint4_t VRegA_12x() const;
214 int16_t VRegA_20t() const;
215 uint8_t VRegA_21c() const;
216 uint8_t VRegA_21h() const;
217 uint8_t VRegA_21s() const;
218 uint8_t VRegA_21t() const;
219 uint8_t VRegA_22b() const;
220 uint4_t VRegA_22c() const;
221 uint4_t VRegA_22s() const;
222 uint4_t VRegA_22t() const;
223 uint8_t VRegA_22x() const;
224 uint8_t VRegA_23x() const;
225 int32_t VRegA_30t() const;
226 uint8_t VRegA_31c() const;
227 uint8_t VRegA_31i() const;
228 uint8_t VRegA_31t() const;
229 uint16_t VRegA_32x() const;
230 uint4_t VRegA_35c() const;
231 uint8_t VRegA_3rc() const;
232 uint8_t VRegA_51l() const;
233
234 // VRegB
235 int4_t VRegB_11n() const;
236 uint4_t VRegB_12x() const;
237 uint16_t VRegB_21c() const;
238 uint16_t VRegB_21h() const;
239 int16_t VRegB_21s() const;
240 int16_t VRegB_21t() const;
241 uint8_t VRegB_22b() const;
242 uint4_t VRegB_22c() const;
243 uint4_t VRegB_22s() const;
244 uint4_t VRegB_22t() const;
245 uint16_t VRegB_22x() const;
246 uint8_t VRegB_23x() const;
247 uint32_t VRegB_31c() const;
248 int32_t VRegB_31i() const;
249 int32_t VRegB_31t() const;
250 uint16_t VRegB_32x() const;
251 uint16_t VRegB_35c() const;
252 uint16_t VRegB_3rc() const;
253 uint64_t VRegB_51l() const; // vB_wide
254
255 // VRegC
256 int8_t VRegC_22b() const;
257 uint16_t VRegC_22c() const;
258 int16_t VRegC_22s() const;
259 int16_t VRegC_22t() const;
260 uint8_t VRegC_23x() const;
261 uint4_t VRegC_35c() const;
262 uint16_t VRegC_3rc() const;
263
264 // Fills the given array with the 'arg' array of the instruction.
265 void GetArgs(uint32_t args[5]) const;
266
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700267 // Returns the opcode field of the instruction.
Ian Rogersa75a0132012-09-28 11:41:42 -0700268 Code Opcode() const {
269 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
270 int opcode = *insns & 0xFF;
271 return static_cast<Code>(opcode);
272 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700273
274 // Reads an instruction out of the stream at the specified address.
Ian Rogersd81871c2011-10-03 13:57:23 -0700275 static const Instruction* At(const uint16_t* code) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700276 CHECK(code != NULL);
jeffhaoba5ebb92011-08-25 17:24:37 -0700277 return reinterpret_cast<const Instruction*>(code);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700278 }
279
Elliott Hughesadb8c672012-03-06 16:49:32 -0800280 // Returns the format of the given opcode.
281 static Format FormatOf(Code opcode) {
282 return kInstructionFormats[opcode];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700283 }
284
Elliott Hughesadb8c672012-03-06 16:49:32 -0800285 // Returns the flags for the given opcode.
Ian Rogersa75a0132012-09-28 11:41:42 -0700286 static int FlagsOf(Code opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800287 return kInstructionFlags[opcode];
jeffhaobdb76512011-09-07 11:43:16 -0700288 }
289
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700290 // Returns true if this instruction is a branch.
291 bool IsBranch() const {
292 return (kInstructionFlags[Opcode()] & kBranch) != 0;
293 }
294
TDYa127526643e2012-05-26 01:01:48 -0700295 // Returns true if this instruction is a unconditional branch.
296 bool IsUnconditional() const {
297 return (kInstructionFlags[Opcode()] & kUnconditional) != 0;
298 }
299
jeffhaoba5ebb92011-08-25 17:24:37 -0700300 // Returns true if this instruction is a switch.
301 bool IsSwitch() const {
302 return (kInstructionFlags[Opcode()] & kSwitch) != 0;
303 }
304
305 // Returns true if this instruction can throw.
306 bool IsThrow() const {
307 return (kInstructionFlags[Opcode()] & kThrow) != 0;
308 }
309
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700310 // Determine if the instruction is any of 'return' instructions.
311 bool IsReturn() const {
312 return (kInstructionFlags[Opcode()] & kReturn) != 0;
313 }
314
315 // Determine if this instruction ends execution of its basic block.
316 bool IsBasicBlockEnd() const {
317 return IsBranch() || IsReturn() || Opcode() == THROW;
318 }
319
320 // Determine if this instruction is an invoke.
321 bool IsInvoke() const {
322 return (kInstructionFlags[Opcode()] & kInvoke) != 0;
323 }
324
jeffhaoba5ebb92011-08-25 17:24:37 -0700325 int GetVerifyTypeArgumentA() const {
326 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegA | kVerifyRegAWide));
327 }
328
329 int GetVerifyTypeArgumentB() const {
330 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegB | kVerifyRegBField | kVerifyRegBMethod |
331 kVerifyRegBNewInstance | kVerifyRegBString | kVerifyRegBType | kVerifyRegBWide));
332 }
333
334 int GetVerifyTypeArgumentC() const {
335 return (kInstructionVerifyFlags[Opcode()] & (kVerifyRegC | kVerifyRegCField |
jeffhao3bb32462012-02-01 16:12:27 -0800336 kVerifyRegCNewArray | kVerifyRegCType | kVerifyRegCWide));
jeffhaoba5ebb92011-08-25 17:24:37 -0700337 }
338
339 int GetVerifyExtraFlags() const {
340 return (kInstructionVerifyFlags[Opcode()] & (kVerifyArrayData | kVerifyBranchTarget |
341 kVerifySwitchTargets | kVerifyVarArg | kVerifyVarArgRange | kVerifyError));
342 }
343
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700344 // Get the dex PC of this instruction as a offset in code units from the beginning of insns.
345 uint32_t GetDexPc(const uint16_t* insns) const {
346 return (reinterpret_cast<const uint16_t*>(this) - insns);
347 }
348
Ian Rogersd81871c2011-10-03 13:57:23 -0700349 // Dump decoded version of instruction
Ian Rogers2c8a8572011-10-24 17:11:36 -0700350 std::string DumpString(const DexFile*) const;
351
352 // Dump code_units worth of this instruction, padding to code_units for shorter instructions
353 std::string DumpHex(size_t code_units) const;
Ian Rogersd81871c2011-10-03 13:57:23 -0700354
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700355 private:
Ian Rogersa75a0132012-09-28 11:41:42 -0700356 size_t SizeInCodeUnitsComplexOpcode() const;
357
Sebastien Hertz807a2562013-04-15 09:33:39 +0200358 uint16_t Fetch16(size_t offset) const {
359 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
360 return insns[offset];
361 }
362
363 uint32_t Fetch32(size_t offset) const {
364 return (Fetch16(offset) | ((uint32_t) Fetch16(offset + 1) << 16));
365 }
366
367 uint4_t InstA() const {
368 return static_cast<uint4_t>((Fetch16(0) >> 8) & 0x0f);
369 }
370
371 uint4_t InstB() const {
372 return static_cast<uint4_t>(Fetch16(0) >> 12);
373 }
374
375 uint8_t InstAA() const {
376 return static_cast<uint8_t>(Fetch16(0) >> 8);
377 }
378
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700379 static const char* const kInstructionNames[];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800380 static Format const kInstructionFormats[];
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700381 static int const kInstructionFlags[];
jeffhaoba5ebb92011-08-25 17:24:37 -0700382 static int const kInstructionVerifyFlags[];
Ian Rogersa75a0132012-09-28 11:41:42 -0700383 static int const kInstructionSizeInCodeUnits[];
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700384 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
385};
Ian Rogersa75a0132012-09-28 11:41:42 -0700386std::ostream& operator<<(std::ostream& os, const Instruction::Code& code);
387std::ostream& operator<<(std::ostream& os, const Instruction::Format& format);
388std::ostream& operator<<(std::ostream& os, const Instruction::Flags& flags);
389std::ostream& operator<<(std::ostream& os, const Instruction::VerifyFlag& vflags);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800390
391/*
392 * Holds the contents of a decoded instruction.
393 */
394struct DecodedInstruction {
395 uint32_t vA;
396 uint32_t vB;
397 uint64_t vB_wide; /* for k51l */
398 uint32_t vC;
399 uint32_t arg[5]; /* vC/D/E/F/G in invoke or filled-new-array */
400 Instruction::Code opcode;
401
Ian Rogersa75a0132012-09-28 11:41:42 -0700402 explicit DecodedInstruction(const Instruction* inst) {
403 inst->Decode(vA, vB, vB_wide, vC, arg);
404 opcode = inst->Opcode();
405 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800406};
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700407
408} // namespace art
409
410#endif // ART_SRC_DEX_INSTRUCTION_H_