blob: a7be5290010ac42b7e12230422df57e8c484a517 [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
Sebastien Hertz807a2562013-04-15 09:33:39 +020017#include "dex_instruction-inl.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070018
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070019#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "utils.h"
Ian Rogersd81871c2011-10-03 13:57:23 -070021#include <iomanip>
22
Carl Shapiro12eb78e2011-06-24 14:51:06 -070023namespace art {
24
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070025const char* const Instruction::kInstructionNames[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070026#define INSTRUCTION_NAME(o, c, pname, f, r, i, a, v) pname,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070028 DEX_INSTRUCTION_LIST(INSTRUCTION_NAME)
29#undef DEX_INSTRUCTION_LIST
30#undef INSTRUCTION_NAME
31};
32
Elliott Hughesadb8c672012-03-06 16:49:32 -080033Instruction::Format const Instruction::kInstructionFormats[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070034#define INSTRUCTION_FORMAT(o, c, p, format, r, i, a, v) format,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070036 DEX_INSTRUCTION_LIST(INSTRUCTION_FORMAT)
37#undef DEX_INSTRUCTION_LIST
38#undef INSTRUCTION_FORMAT
39};
40
41int const Instruction::kInstructionFlags[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070042#define INSTRUCTION_FLAGS(o, c, p, f, r, i, flags, v) flags,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070043#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070044 DEX_INSTRUCTION_LIST(INSTRUCTION_FLAGS)
45#undef DEX_INSTRUCTION_LIST
46#undef INSTRUCTION_FLAGS
47};
48
jeffhaoba5ebb92011-08-25 17:24:37 -070049int const Instruction::kInstructionVerifyFlags[] = {
50#define INSTRUCTION_VERIFY_FLAGS(o, c, p, f, r, i, a, vflags) vflags,
51#include "dex_instruction_list.h"
52 DEX_INSTRUCTION_LIST(INSTRUCTION_VERIFY_FLAGS)
53#undef DEX_INSTRUCTION_LIST
54#undef INSTRUCTION_VERIFY_FLAGS
55};
56
Ian Rogersa75a0132012-09-28 11:41:42 -070057int const Instruction::kInstructionSizeInCodeUnits[] = {
58#define INSTRUCTION_SIZE(opcode, c, p, format, r, i, a, v) \
Brian Carlstrom6f485c62013-07-18 15:35:35 -070059 ((opcode == NOP) ? -1 : \
60 ((format >= k10x) && (format <= k10t)) ? 1 : \
61 ((format >= k20t) && (format <= k22c)) ? 2 : \
62 ((format >= k32x) && (format <= k3rc)) ? 3 : \
63 (format == k51l) ? 5 : -1),
Ian Rogersa75a0132012-09-28 11:41:42 -070064#include "dex_instruction_list.h"
65 DEX_INSTRUCTION_LIST(INSTRUCTION_SIZE)
66#undef DEX_INSTRUCTION_LIST
67#undef INSTRUCTION_SIZE
68};
69
jeffhaoba5ebb92011-08-25 17:24:37 -070070/*
71 * Handy macros for helping decode instructions.
72 */
73#define FETCH(_offset) (insns[(_offset)])
Sebastien Hertz807a2562013-04-15 09:33:39 +020074#define FETCH_uint32(_offset) (fetch_uint32_impl((_offset), insns))
jeffhaoba5ebb92011-08-25 17:24:37 -070075#define INST_A(_insn) (((uint16_t)(_insn) >> 8) & 0x0f)
76#define INST_B(_insn) ((uint16_t)(_insn) >> 12)
77#define INST_AA(_insn) ((_insn) >> 8)
78
Sebastien Hertz807a2562013-04-15 09:33:39 +020079/* Helper for FETCH_uint32, above. */
80static inline uint32_t fetch_uint32_impl(uint32_t offset, const uint16_t* insns) {
jeffhaoba5ebb92011-08-25 17:24:37 -070081 return insns[offset] | ((uint32_t) insns[offset+1] << 16);
82}
83
Dragos Sbirlea8cc51622013-06-21 09:20:34 -070084
85bool Instruction::HasVRegC() const {
86 switch (FormatOf(Opcode())) {
87 case k23x: return true;
88 case k35c: return true;
89 case k3rc: return true;
90 default: return false;
91 }
92}
93
94bool Instruction::HasVRegB() const {
95 switch (FormatOf(Opcode())) {
96 case k12x: return true;
97 case k22b: return true;
98 case k22c: return true;
99 case k22s: return true;
100 case k22t: return true;
101 case k22x: return true;
102 case k32x: return true;
103 default: return false;
104 }
105}
106
107bool Instruction::HasVRegA() const {
108 switch (FormatOf(Opcode())) {
109 case k11n: return true;
110 case k11x: return true;
111 case k12x: return true;
112 case k21c: return true;
113 case k21h: return true;
114 case k21s: return true;
115 case k21t: return true;
116 case k22b: return true;
117 case k22c: return true;
118 case k22s: return true;
119 case k22t: return true;
120 case k22x: return true;
121 case k23x: return true;
122 case k31c: return true;
123 case k31i: return true;
124 case k31t: return true;
125 case k32x: return true;
126 case k51l: return true;
127 default: return false;
128 }
129}
130
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700131int32_t Instruction::VRegC() const {
132 switch (FormatOf(Opcode())) {
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700133 case k23x: return VRegC_23x();
134 case k35c: return VRegC_35c();
135 case k3rc: return VRegC_3rc();
136 default: LOG(FATAL) << "Tried to access vC of instruction " << Name() <<
137 " which has no C operand.";
138 }
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700139 return -1;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700140}
141
142int32_t Instruction::VRegB() const {
143 switch (FormatOf(Opcode())) {
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700144 case k12x: return VRegB_12x();
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700145 case k22b: return VRegB_22b();
146 case k22c: return VRegB_22c();
147 case k22s: return VRegB_22s();
148 case k22t: return VRegB_22t();
149 case k22x: return VRegB_22x();
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700150 case k32x: return VRegB_32x();
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700151 default: LOG(FATAL) << "Tried to access vB of instruction " << Name() <<
152 " which has no B operand.";
153 }
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700154 return -1;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700155}
156
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700157int32_t Instruction::VRegA() const {
158 switch (FormatOf(Opcode())) {
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700159 case k11n: return VRegA_11n();
160 case k11x: return VRegA_11x();
161 case k12x: return VRegA_12x();
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700162 case k21c: return VRegA_21c();
163 case k21h: return VRegA_21h();
164 case k21s: return VRegA_21s();
165 case k21t: return VRegA_21t();
166 case k22b: return VRegA_22b();
167 case k22c: return VRegA_22c();
168 case k22s: return VRegA_22s();
169 case k22t: return VRegA_22t();
170 case k22x: return VRegA_22x();
171 case k23x: return VRegA_23x();
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700172 case k31c: return VRegA_31c();
173 case k31i: return VRegA_31i();
174 case k31t: return VRegA_31t();
175 case k32x: return VRegA_32x();
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700176 case k51l: return VRegA_51l();
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700177 default: LOG(FATAL) << "Tried to access vA of instruction " << Name() <<
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700178 " which has no A operand.";
179 }
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700180 return -1;
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700181}
182
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700183int32_t Instruction::GetTargetOffset() const {
184 switch (FormatOf(Opcode())) {
185 // Cases for conditional branches follow.
186 case k22t: return VRegC_22t();
187 case k21t: return VRegB_21t();
188 // Cases for unconditional branches follow.
189 case k10t: return VRegA_10t();
190 case k20t: return VRegA_20t();
191 case k30t: return VRegA_30t();
192 default: LOG(FATAL) << "Tried to access the branch offset of an instruction " << Name() <<
193 " which does not have a target operand.";
194 }
195 return 0;
196}
197
198bool Instruction::CanFlowThrough() const {
199 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
200 uint16_t insn = *insns;
201 Code opcode = static_cast<Code>(insn & 0xFF);
202 return FlagsOf(opcode) & Instruction::kContinue;
203}
204
jeffhaoba5ebb92011-08-25 17:24:37 -0700205void Instruction::Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const {
206 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
207 uint16_t insn = *insns;
Ian Rogersa75a0132012-09-28 11:41:42 -0700208 Code opcode = static_cast<Code>(insn & 0xFF);
jeffhaoba5ebb92011-08-25 17:24:37 -0700209
Ian Rogersa75a0132012-09-28 11:41:42 -0700210 switch (FormatOf(opcode)) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700211 case k10x: // op
212 /* nothing to do; copy the AA bits out for the verifier */
213 vA = INST_AA(insn);
214 break;
215 case k12x: // op vA, vB
216 vA = INST_A(insn);
217 vB = INST_B(insn);
218 break;
219 case k11n: // op vA, #+B
220 vA = INST_A(insn);
221 vB = (int32_t) (INST_B(insn) << 28) >> 28; // sign extend 4-bit value
222 break;
223 case k11x: // op vAA
224 vA = INST_AA(insn);
225 break;
226 case k10t: // op +AA
227 vA = (int8_t) INST_AA(insn); // sign-extend 8-bit value
228 break;
229 case k20t: // op +AAAA
230 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
231 break;
232 case k21c: // op vAA, thing@BBBB
233 case k22x: // op vAA, vBBBB
234 vA = INST_AA(insn);
235 vB = FETCH(1);
236 break;
237 case k21s: // op vAA, #+BBBB
238 case k21t: // op vAA, +BBBB
239 vA = INST_AA(insn);
240 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
241 break;
242 case k21h: // op vAA, #+BBBB0000[00000000]
243 vA = INST_AA(insn);
244 /*
245 * The value should be treated as right-zero-extended, but we don't
246 * actually do that here. Among other things, we don't know if it's
247 * the top bits of a 32- or 64-bit value.
248 */
249 vB = FETCH(1);
250 break;
251 case k23x: // op vAA, vBB, vCC
252 vA = INST_AA(insn);
253 vB = FETCH(1) & 0xff;
254 vC = FETCH(1) >> 8;
255 break;
256 case k22b: // op vAA, vBB, #+CC
257 vA = INST_AA(insn);
258 vB = FETCH(1) & 0xff;
259 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
260 break;
261 case k22s: // op vA, vB, #+CCCC
262 case k22t: // op vA, vB, +CCCC
263 vA = INST_A(insn);
264 vB = INST_B(insn);
265 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
266 break;
267 case k22c: // op vA, vB, thing@CCCC
268 vA = INST_A(insn);
269 vB = INST_B(insn);
270 vC = FETCH(1);
271 break;
272 case k30t: // op +AAAAAAAA
Sebastien Hertz807a2562013-04-15 09:33:39 +0200273 vA = FETCH_uint32(1); // signed 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700274 break;
275 case k31t: // op vAA, +BBBBBBBB
276 case k31c: // op vAA, string@BBBBBBBB
277 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200278 vB = FETCH_uint32(1); // 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700279 break;
280 case k32x: // op vAAAA, vBBBB
281 vA = FETCH(1);
282 vB = FETCH(2);
283 break;
284 case k31i: // op vAA, #+BBBBBBBB
285 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200286 vB = FETCH_uint32(1); // signed 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700287 break;
288 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
289 {
290 /*
291 * Note that the fields mentioned in the spec don't appear in
292 * their "usual" positions here compared to most formats. This
293 * was done so that the field names for the argument count and
294 * reference index match between this format and the corresponding
295 * range formats (3rc and friends).
296 *
297 * Bottom line: The argument count is always in vA, and the
298 * method constant (or equivalent) is always in vB.
299 */
300 uint16_t regList;
301 int count;
302
303 vA = INST_B(insn); // This is labeled A in the spec.
304 vB = FETCH(1);
305 regList = FETCH(2);
306
307 count = vA;
308
309 /*
310 * Copy the argument registers into the arg[] array, and
311 * also copy the first argument (if any) into vC. (The
312 * DecodedInstruction structure doesn't have separate
313 * fields for {vD, vE, vF, vG}, so there's no need to make
314 * copies of those.) Note that cases 5..2 fall through.
315 */
316 switch (count) {
317 case 5: arg[4] = INST_A(insn);
318 case 4: arg[3] = (regList >> 12) & 0x0f;
319 case 3: arg[2] = (regList >> 8) & 0x0f;
320 case 2: arg[1] = (regList >> 4) & 0x0f;
321 case 1: vC = arg[0] = regList & 0x0f; break;
322 case 0: break; // Valid, but no need to do anything.
323 default:
324 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
325 return;
326 }
327 }
328 break;
329 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
330 vA = INST_AA(insn);
331 vB = FETCH(1);
332 vC = FETCH(2);
333 break;
334 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
335 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200336 vB_wide = FETCH_uint32(1) | ((uint64_t) FETCH_uint32(3) << 32);
jeffhaoba5ebb92011-08-25 17:24:37 -0700337 break;
338 default:
Ian Rogersa75a0132012-09-28 11:41:42 -0700339 LOG(ERROR) << "Can't decode unexpected format " << FormatOf(opcode) << " (op=" << opcode << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700340 return;
341 }
342}
343
Ian Rogersa75a0132012-09-28 11:41:42 -0700344size_t Instruction::SizeInCodeUnitsComplexOpcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700345 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Ian Rogersa75a0132012-09-28 11:41:42 -0700346 // Handle special NOP encoded variable length sequences.
347 switch (*insns) {
348 case kPackedSwitchSignature:
349 return (4 + insns[1] * 2);
350 case kSparseSwitchSignature:
351 return (2 + insns[1] * 4);
352 case kArrayDataSignature: {
353 uint16_t element_size = insns[1];
354 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
355 // The plus 1 is to round up for odd size and width.
356 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700357 }
Ian Rogersa75a0132012-09-28 11:41:42 -0700358 default:
359 if ((*insns & 0xFF) == 0) {
360 return 1; // NOP.
361 } else {
362 LOG(FATAL) << "Unreachable: " << DumpString(NULL);
363 return 0;
364 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700365 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700366}
367
Ian Rogers2c8a8572011-10-24 17:11:36 -0700368std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700369 size_t inst_length = SizeInCodeUnits();
370 if (inst_length > code_units) {
371 inst_length = code_units;
372 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700373 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700374 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
375 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700376 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 }
378 for (size_t i = inst_length; i < code_units; i++) {
379 os << " ";
380 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700381 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700382}
383
Ian Rogers2c8a8572011-10-24 17:11:36 -0700384std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700385 std::ostringstream os;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200386 const char* opcode = kInstructionNames[Opcode()];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800387 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800388 case k10x: os << opcode; break;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200389 case k12x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_12x(), VRegB_12x()); break;
390 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_11n(), VRegB_11n()); break;
391 case k11x: os << StringPrintf("%s v%d", opcode, VRegA_11x()); break;
392 case k10t: os << StringPrintf("%s %+d", opcode, VRegA_10t()); break;
393 case k20t: os << StringPrintf("%s %+d", opcode, VRegA_20t()); break;
394 case k22x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_22x(), VRegB_22x()); break;
395 case k21t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_21t(), VRegB_21t()); break;
396 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_21s(), VRegB_21s()); break;
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700397 case k21h: {
398 // op vAA, #+BBBB0000[00000000]
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200399 if (Opcode() == CONST_HIGH16) {
400 uint32_t value = VRegB_21h() << 16;
401 os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700402 } else {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200403 uint64_t value = static_cast<uint64_t>(VRegB_21h()) << 48;
404 os << StringPrintf("%s v%d, #long %+lld // 0x%llx", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700405 }
406 }
407 break;
Ian Rogers90334e52012-06-06 20:22:20 -0700408 case k21c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200409 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700410 case CONST_STRING:
411 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200412 uint32_t string_idx = VRegB_21c();
413 os << StringPrintf("const-string v%d, %s // string@%d", VRegA_21c(),
414 PrintableString(file->StringDataByIdx(string_idx)).c_str(), string_idx);
Ian Rogers90334e52012-06-06 20:22:20 -0700415 break;
416 } // else fall-through
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700417 case CHECK_CAST:
418 case CONST_CLASS:
Ian Rogers90334e52012-06-06 20:22:20 -0700419 case NEW_INSTANCE:
420 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200421 uint32_t type_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700422 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyType(type_idx, *file)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200423 << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700424 break;
425 } // else fall-through
426 case SGET:
427 case SGET_WIDE:
428 case SGET_OBJECT:
429 case SGET_BOOLEAN:
430 case SGET_BYTE:
431 case SGET_CHAR:
432 case SGET_SHORT:
433 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200434 uint32_t field_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700435 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyField(field_idx, *file, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200436 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700437 break;
438 } // else fall-through
439 case SPUT:
440 case SPUT_WIDE:
441 case SPUT_OBJECT:
442 case SPUT_BOOLEAN:
443 case SPUT_BYTE:
444 case SPUT_CHAR:
445 case SPUT_SHORT:
446 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200447 uint32_t field_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700448 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyField(field_idx, *file, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200449 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700450 break;
451 } // else fall-through
452 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200453 os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_21c(), VRegB_21c());
Ian Rogers90334e52012-06-06 20:22:20 -0700454 break;
455 }
456 break;
457 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200458 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, VRegA_23x(), VRegB_23x(), VRegC_23x()); break;
459 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22b(), VRegB_22b(), VRegC_22b()); break;
460 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, VRegA_22t(), VRegB_22t(), VRegC_22t()); break;
461 case k22s: os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22s(), VRegB_22s(), VRegC_22s()); break;
Ian Rogers90334e52012-06-06 20:22:20 -0700462 case k22c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200463 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700464 case IGET:
465 case IGET_WIDE:
466 case IGET_OBJECT:
467 case IGET_BOOLEAN:
468 case IGET_BYTE:
469 case IGET_CHAR:
470 case IGET_SHORT:
471 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200472 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700473 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200474 << PrettyField(field_idx, *file, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700475 break;
476 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200477 case IGET_QUICK:
478 case IGET_OBJECT_QUICK:
479 if (file != NULL) {
480 uint32_t field_idx = VRegC_22c();
481 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
482 << "// offset@" << field_idx;
483 break;
484 } // else fall-through
Ian Rogers90334e52012-06-06 20:22:20 -0700485 case IPUT:
486 case IPUT_WIDE:
487 case IPUT_OBJECT:
488 case IPUT_BOOLEAN:
489 case IPUT_BYTE:
490 case IPUT_CHAR:
491 case IPUT_SHORT:
492 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200493 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700494 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200495 << PrettyField(field_idx, *file, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700496 break;
497 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200498 case IPUT_QUICK:
499 case IPUT_OBJECT_QUICK:
500 if (file != NULL) {
501 uint32_t field_idx = VRegC_22c();
502 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
503 << "// offset@" << field_idx;
504 break;
505 } // else fall-through
Ian Rogers90334e52012-06-06 20:22:20 -0700506 case INSTANCE_OF:
507 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200508 uint32_t type_idx = VRegC_22c();
Dragos Sbirlead4e868a2013-05-23 09:44:17 -0700509 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200510 << PrettyType(type_idx, *file) << " // type@" << type_idx;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700511 break;
512 }
513 case NEW_ARRAY:
514 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200515 uint32_t type_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700516 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200517 << PrettyType(type_idx, *file) << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700518 break;
519 } // else fall-through
520 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200521 os << StringPrintf("%s v%d, v%d, thing@%d", opcode, VRegA_22c(), VRegB_22c(), VRegC_22c());
Ian Rogers90334e52012-06-06 20:22:20 -0700522 break;
523 }
524 break;
525 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200526 case k32x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_32x(), VRegB_32x()); break;
527 case k30t: os << StringPrintf("%s %+d", opcode, VRegA_30t()); break;
528 case k31t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_31t(), VRegB_31t()); break;
529 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_31i(), VRegB_31i()); break;
530 case k31c: os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_31c(), VRegB_31c()); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 case k35c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200532 uint32_t arg[5];
533 GetArgs(arg);
534 switch (Opcode()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 case INVOKE_VIRTUAL:
536 case INVOKE_SUPER:
537 case INVOKE_DIRECT:
538 case INVOKE_STATIC:
539 case INVOKE_INTERFACE:
540 if (file != NULL) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800541 os << opcode << " {";
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200542 uint32_t method_idx = VRegB_35c();
543 for (size_t i = 0; i < VRegA_35c(); ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 if (i != 0) {
545 os << ", ";
546 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200547 os << "v" << arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800548 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200549 os << "}, " << PrettyMethod(method_idx, *file) << " // method@" << method_idx;
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 break;
551 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200552 case INVOKE_VIRTUAL_QUICK:
553 if (file != NULL) {
554 os << opcode << " {";
555 uint32_t method_idx = VRegB_35c();
556 for (size_t i = 0; i < VRegA_35c(); ++i) {
557 if (i != 0) {
558 os << ", ";
559 }
560 os << "v" << arg[i];
561 }
562 os << "}, // vtable@" << method_idx;
563 break;
564 } // else fall-through
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200566 os << opcode << " {v" << arg[0] << ", v" << arg[1] << ", v" << arg[2]
567 << ", v" << arg[3] << ", v" << arg[4] << "}, thing@" << VRegB_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 break;
569 }
570 break;
571 }
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700572 case k3rc: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200573 switch (Opcode()) {
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700574 case INVOKE_VIRTUAL_RANGE:
575 case INVOKE_SUPER_RANGE:
576 case INVOKE_DIRECT_RANGE:
577 case INVOKE_STATIC_RANGE:
578 case INVOKE_INTERFACE_RANGE:
579 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200580 uint32_t method_idx = VRegB_3rc();
581 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
582 << PrettyMethod(method_idx, *file) << " // method@" << method_idx;
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700583 break;
584 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200585 case INVOKE_VIRTUAL_RANGE_QUICK:
586 if (file != NULL) {
587 uint32_t method_idx = VRegB_3rc();
588 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
589 << "// vtable@" << method_idx;
590 break;
591 } // else fall-through
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700592 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200593 os << StringPrintf("%s, {v%d .. v%d}, thing@%d", opcode, VRegC_3rc(),
594 (VRegC_3rc() + VRegA_3rc() - 1), VRegB_3rc());
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700595 break;
596 }
597 break;
598 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200599 case k51l: os << StringPrintf("%s v%d, #%+lld", opcode, VRegA_51l(), VRegB_51l()); break;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700600 default: os << " unknown format (" << DumpHex(5) << ")"; break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700602 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700603}
604
Ian Rogersa75a0132012-09-28 11:41:42 -0700605std::ostream& operator<<(std::ostream& os, const Instruction::Code& code) {
606 return os << Instruction::Name(code);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607}
608
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700609} // namespace art