blob: c5901aa34b9547f8aee30e0b0d405eae1a0cc7ca [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) \
59 (( 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 \
64 ),
65#include "dex_instruction_list.h"
66 DEX_INSTRUCTION_LIST(INSTRUCTION_SIZE)
67#undef DEX_INSTRUCTION_LIST
68#undef INSTRUCTION_SIZE
69};
70
jeffhaoba5ebb92011-08-25 17:24:37 -070071/*
72 * Handy macros for helping decode instructions.
73 */
74#define FETCH(_offset) (insns[(_offset)])
Sebastien Hertz807a2562013-04-15 09:33:39 +020075#define FETCH_uint32(_offset) (fetch_uint32_impl((_offset), insns))
jeffhaoba5ebb92011-08-25 17:24:37 -070076#define INST_A(_insn) (((uint16_t)(_insn) >> 8) & 0x0f)
77#define INST_B(_insn) ((uint16_t)(_insn) >> 12)
78#define INST_AA(_insn) ((_insn) >> 8)
79
Sebastien Hertz807a2562013-04-15 09:33:39 +020080/* Helper for FETCH_uint32, above. */
81static inline uint32_t fetch_uint32_impl(uint32_t offset, const uint16_t* insns) {
jeffhaoba5ebb92011-08-25 17:24:37 -070082 return insns[offset] | ((uint32_t) insns[offset+1] << 16);
83}
84
Dragos Sbirlea39f99272013-06-25 13:17:36 -070085int32_t Instruction::VRegC() const {
86 switch (FormatOf(Opcode())) {
87 case k22b: return VRegC_22b();
88 case k22c: return VRegC_22c();
89 case k22s: return VRegC_22s();
90 case k22t: return VRegC_22t();
91 case k23x: return VRegC_23x();
92 case k35c: return VRegC_35c();
93 case k3rc: return VRegC_3rc();
94 default: LOG(FATAL) << "Tried to access vC of instruction " << Name() <<
95 " which has no C operand.";
96 }
97 return 0;
98}
99
100int32_t Instruction::VRegB() const {
101 switch (FormatOf(Opcode())) {
102 case k11n: return VRegB_11n();
103 case k12x: return VRegB_12x();
104 case k21c: return VRegB_21c();
105 case k21h: return VRegB_21h();
106 case k21t: return VRegB_21t();
107 case k22b: return VRegB_22b();
108 case k22c: return VRegB_22c();
109 case k22s: return VRegB_22s();
110 case k22t: return VRegB_22t();
111 case k22x: return VRegB_22x();
112 case k31c: return VRegB_31c();
113 case k31i: return VRegB_31i();
114 case k31t: return VRegB_31t();
115 case k32x: return VRegB_32x();
116 case k35c: return VRegB_35c();
117 case k3rc: return VRegB_3rc();
118 case k51l: return VRegB_51l();
119 default: LOG(FATAL) << "Tried to access vB of instruction " << Name() <<
120 " which has no B operand.";
121 }
122 return 0;
123}
124
125int32_t Instruction::GetTargetOffset() const {
126 switch (FormatOf(Opcode())) {
127 // Cases for conditional branches follow.
128 case k22t: return VRegC_22t();
129 case k21t: return VRegB_21t();
130 // Cases for unconditional branches follow.
131 case k10t: return VRegA_10t();
132 case k20t: return VRegA_20t();
133 case k30t: return VRegA_30t();
134 default: LOG(FATAL) << "Tried to access the branch offset of an instruction " << Name() <<
135 " which does not have a target operand.";
136 }
137 return 0;
138}
139
140bool Instruction::CanFlowThrough() const {
141 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
142 uint16_t insn = *insns;
143 Code opcode = static_cast<Code>(insn & 0xFF);
144 return FlagsOf(opcode) & Instruction::kContinue;
145}
146
jeffhaoba5ebb92011-08-25 17:24:37 -0700147void Instruction::Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const {
148 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
149 uint16_t insn = *insns;
Ian Rogersa75a0132012-09-28 11:41:42 -0700150 Code opcode = static_cast<Code>(insn & 0xFF);
jeffhaoba5ebb92011-08-25 17:24:37 -0700151
Ian Rogersa75a0132012-09-28 11:41:42 -0700152 switch (FormatOf(opcode)) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700153 case k10x: // op
154 /* nothing to do; copy the AA bits out for the verifier */
155 vA = INST_AA(insn);
156 break;
157 case k12x: // op vA, vB
158 vA = INST_A(insn);
159 vB = INST_B(insn);
160 break;
161 case k11n: // op vA, #+B
162 vA = INST_A(insn);
163 vB = (int32_t) (INST_B(insn) << 28) >> 28; // sign extend 4-bit value
164 break;
165 case k11x: // op vAA
166 vA = INST_AA(insn);
167 break;
168 case k10t: // op +AA
169 vA = (int8_t) INST_AA(insn); // sign-extend 8-bit value
170 break;
171 case k20t: // op +AAAA
172 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
173 break;
174 case k21c: // op vAA, thing@BBBB
175 case k22x: // op vAA, vBBBB
176 vA = INST_AA(insn);
177 vB = FETCH(1);
178 break;
179 case k21s: // op vAA, #+BBBB
180 case k21t: // op vAA, +BBBB
181 vA = INST_AA(insn);
182 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
183 break;
184 case k21h: // op vAA, #+BBBB0000[00000000]
185 vA = INST_AA(insn);
186 /*
187 * The value should be treated as right-zero-extended, but we don't
188 * actually do that here. Among other things, we don't know if it's
189 * the top bits of a 32- or 64-bit value.
190 */
191 vB = FETCH(1);
192 break;
193 case k23x: // op vAA, vBB, vCC
194 vA = INST_AA(insn);
195 vB = FETCH(1) & 0xff;
196 vC = FETCH(1) >> 8;
197 break;
198 case k22b: // op vAA, vBB, #+CC
199 vA = INST_AA(insn);
200 vB = FETCH(1) & 0xff;
201 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
202 break;
203 case k22s: // op vA, vB, #+CCCC
204 case k22t: // op vA, vB, +CCCC
205 vA = INST_A(insn);
206 vB = INST_B(insn);
207 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
208 break;
209 case k22c: // op vA, vB, thing@CCCC
210 vA = INST_A(insn);
211 vB = INST_B(insn);
212 vC = FETCH(1);
213 break;
214 case k30t: // op +AAAAAAAA
Sebastien Hertz807a2562013-04-15 09:33:39 +0200215 vA = FETCH_uint32(1); // signed 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700216 break;
217 case k31t: // op vAA, +BBBBBBBB
218 case k31c: // op vAA, string@BBBBBBBB
219 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200220 vB = FETCH_uint32(1); // 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700221 break;
222 case k32x: // op vAAAA, vBBBB
223 vA = FETCH(1);
224 vB = FETCH(2);
225 break;
226 case k31i: // op vAA, #+BBBBBBBB
227 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200228 vB = FETCH_uint32(1); // signed 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700229 break;
230 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
231 {
232 /*
233 * Note that the fields mentioned in the spec don't appear in
234 * their "usual" positions here compared to most formats. This
235 * was done so that the field names for the argument count and
236 * reference index match between this format and the corresponding
237 * range formats (3rc and friends).
238 *
239 * Bottom line: The argument count is always in vA, and the
240 * method constant (or equivalent) is always in vB.
241 */
242 uint16_t regList;
243 int count;
244
245 vA = INST_B(insn); // This is labeled A in the spec.
246 vB = FETCH(1);
247 regList = FETCH(2);
248
249 count = vA;
250
251 /*
252 * Copy the argument registers into the arg[] array, and
253 * also copy the first argument (if any) into vC. (The
254 * DecodedInstruction structure doesn't have separate
255 * fields for {vD, vE, vF, vG}, so there's no need to make
256 * copies of those.) Note that cases 5..2 fall through.
257 */
258 switch (count) {
259 case 5: arg[4] = INST_A(insn);
260 case 4: arg[3] = (regList >> 12) & 0x0f;
261 case 3: arg[2] = (regList >> 8) & 0x0f;
262 case 2: arg[1] = (regList >> 4) & 0x0f;
263 case 1: vC = arg[0] = regList & 0x0f; break;
264 case 0: break; // Valid, but no need to do anything.
265 default:
266 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
267 return;
268 }
269 }
270 break;
271 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
272 vA = INST_AA(insn);
273 vB = FETCH(1);
274 vC = FETCH(2);
275 break;
276 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
277 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200278 vB_wide = FETCH_uint32(1) | ((uint64_t) FETCH_uint32(3) << 32);
jeffhaoba5ebb92011-08-25 17:24:37 -0700279 break;
280 default:
Ian Rogersa75a0132012-09-28 11:41:42 -0700281 LOG(ERROR) << "Can't decode unexpected format " << FormatOf(opcode) << " (op=" << opcode << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700282 return;
283 }
284}
285
Ian Rogersa75a0132012-09-28 11:41:42 -0700286size_t Instruction::SizeInCodeUnitsComplexOpcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700287 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Ian Rogersa75a0132012-09-28 11:41:42 -0700288 // Handle special NOP encoded variable length sequences.
289 switch (*insns) {
290 case kPackedSwitchSignature:
291 return (4 + insns[1] * 2);
292 case kSparseSwitchSignature:
293 return (2 + insns[1] * 4);
294 case kArrayDataSignature: {
295 uint16_t element_size = insns[1];
296 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
297 // The plus 1 is to round up for odd size and width.
298 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700299 }
Ian Rogersa75a0132012-09-28 11:41:42 -0700300 default:
301 if ((*insns & 0xFF) == 0) {
302 return 1; // NOP.
303 } else {
304 LOG(FATAL) << "Unreachable: " << DumpString(NULL);
305 return 0;
306 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700307 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700308}
309
Ian Rogers2c8a8572011-10-24 17:11:36 -0700310std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700311 size_t inst_length = SizeInCodeUnits();
312 if (inst_length > code_units) {
313 inst_length = code_units;
314 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700315 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700316 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
317 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700318 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 }
320 for (size_t i = inst_length; i < code_units; i++) {
321 os << " ";
322 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700323 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700324}
325
Ian Rogers2c8a8572011-10-24 17:11:36 -0700326std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700327 std::ostringstream os;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200328 const char* opcode = kInstructionNames[Opcode()];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800329 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800330 case k10x: os << opcode; break;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200331 case k12x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_12x(), VRegB_12x()); break;
332 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_11n(), VRegB_11n()); break;
333 case k11x: os << StringPrintf("%s v%d", opcode, VRegA_11x()); break;
334 case k10t: os << StringPrintf("%s %+d", opcode, VRegA_10t()); break;
335 case k20t: os << StringPrintf("%s %+d", opcode, VRegA_20t()); break;
336 case k22x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_22x(), VRegB_22x()); break;
337 case k21t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_21t(), VRegB_21t()); break;
338 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_21s(), VRegB_21s()); break;
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700339 case k21h: {
340 // op vAA, #+BBBB0000[00000000]
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200341 if (Opcode() == CONST_HIGH16) {
342 uint32_t value = VRegB_21h() << 16;
343 os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700344 } else {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200345 uint64_t value = static_cast<uint64_t>(VRegB_21h()) << 48;
346 os << StringPrintf("%s v%d, #long %+lld // 0x%llx", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700347 }
348 }
349 break;
Ian Rogers90334e52012-06-06 20:22:20 -0700350 case k21c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200351 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700352 case CONST_STRING:
353 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200354 uint32_t string_idx = VRegB_21c();
355 os << StringPrintf("const-string v%d, %s // string@%d", VRegA_21c(),
356 PrintableString(file->StringDataByIdx(string_idx)).c_str(), string_idx);
Ian Rogers90334e52012-06-06 20:22:20 -0700357 break;
358 } // else fall-through
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700359 case CHECK_CAST:
360 case CONST_CLASS:
Ian Rogers90334e52012-06-06 20:22:20 -0700361 case NEW_INSTANCE:
362 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200363 uint32_t type_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700364 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyType(type_idx, *file)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200365 << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700366 break;
367 } // else fall-through
368 case SGET:
369 case SGET_WIDE:
370 case SGET_OBJECT:
371 case SGET_BOOLEAN:
372 case SGET_BYTE:
373 case SGET_CHAR:
374 case SGET_SHORT:
375 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200376 uint32_t field_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700377 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyField(field_idx, *file, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200378 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700379 break;
380 } // else fall-through
381 case SPUT:
382 case SPUT_WIDE:
383 case SPUT_OBJECT:
384 case SPUT_BOOLEAN:
385 case SPUT_BYTE:
386 case SPUT_CHAR:
387 case SPUT_SHORT:
388 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200389 uint32_t field_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700390 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyField(field_idx, *file, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200391 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700392 break;
393 } // else fall-through
394 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200395 os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_21c(), VRegB_21c());
Ian Rogers90334e52012-06-06 20:22:20 -0700396 break;
397 }
398 break;
399 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200400 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, VRegA_23x(), VRegB_23x(), VRegC_23x()); break;
401 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22b(), VRegB_22b(), VRegC_22b()); break;
402 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, VRegA_22t(), VRegB_22t(), VRegC_22t()); break;
403 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 -0700404 case k22c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200405 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700406 case IGET:
407 case IGET_WIDE:
408 case IGET_OBJECT:
409 case IGET_BOOLEAN:
410 case IGET_BYTE:
411 case IGET_CHAR:
412 case IGET_SHORT:
413 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200414 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700415 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200416 << PrettyField(field_idx, *file, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700417 break;
418 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200419 case IGET_QUICK:
420 case IGET_OBJECT_QUICK:
421 if (file != NULL) {
422 uint32_t field_idx = VRegC_22c();
423 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
424 << "// offset@" << field_idx;
425 break;
426 } // else fall-through
Ian Rogers90334e52012-06-06 20:22:20 -0700427 case IPUT:
428 case IPUT_WIDE:
429 case IPUT_OBJECT:
430 case IPUT_BOOLEAN:
431 case IPUT_BYTE:
432 case IPUT_CHAR:
433 case IPUT_SHORT:
434 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200435 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700436 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200437 << PrettyField(field_idx, *file, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700438 break;
439 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200440 case IPUT_QUICK:
441 case IPUT_OBJECT_QUICK:
442 if (file != NULL) {
443 uint32_t field_idx = VRegC_22c();
444 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
445 << "// offset@" << field_idx;
446 break;
447 } // else fall-through
Ian Rogers90334e52012-06-06 20:22:20 -0700448 case INSTANCE_OF:
449 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200450 uint32_t type_idx = VRegC_22c();
Dragos Sbirlead4e868a2013-05-23 09:44:17 -0700451 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200452 << PrettyType(type_idx, *file) << " // type@" << type_idx;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700453 break;
454 }
455 case NEW_ARRAY:
456 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200457 uint32_t type_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700458 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200459 << PrettyType(type_idx, *file) << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700460 break;
461 } // else fall-through
462 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200463 os << StringPrintf("%s v%d, v%d, thing@%d", opcode, VRegA_22c(), VRegB_22c(), VRegC_22c());
Ian Rogers90334e52012-06-06 20:22:20 -0700464 break;
465 }
466 break;
467 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200468 case k32x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_32x(), VRegB_32x()); break;
469 case k30t: os << StringPrintf("%s %+d", opcode, VRegA_30t()); break;
470 case k31t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_31t(), VRegB_31t()); break;
471 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_31i(), VRegB_31i()); break;
472 case k31c: os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_31c(), VRegB_31c()); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700473 case k35c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200474 uint32_t arg[5];
475 GetArgs(arg);
476 switch (Opcode()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700477 case INVOKE_VIRTUAL:
478 case INVOKE_SUPER:
479 case INVOKE_DIRECT:
480 case INVOKE_STATIC:
481 case INVOKE_INTERFACE:
482 if (file != NULL) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800483 os << opcode << " {";
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200484 uint32_t method_idx = VRegB_35c();
485 for (size_t i = 0; i < VRegA_35c(); ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800486 if (i != 0) {
487 os << ", ";
488 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200489 os << "v" << arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800490 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200491 os << "}, " << PrettyMethod(method_idx, *file) << " // method@" << method_idx;
Ian Rogersd81871c2011-10-03 13:57:23 -0700492 break;
493 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200494 case INVOKE_VIRTUAL_QUICK:
495 if (file != NULL) {
496 os << opcode << " {";
497 uint32_t method_idx = VRegB_35c();
498 for (size_t i = 0; i < VRegA_35c(); ++i) {
499 if (i != 0) {
500 os << ", ";
501 }
502 os << "v" << arg[i];
503 }
504 os << "}, // vtable@" << method_idx;
505 break;
506 } // else fall-through
Ian Rogersd81871c2011-10-03 13:57:23 -0700507 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200508 os << opcode << " {v" << arg[0] << ", v" << arg[1] << ", v" << arg[2]
509 << ", v" << arg[3] << ", v" << arg[4] << "}, thing@" << VRegB_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 break;
511 }
512 break;
513 }
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700514 case k3rc: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200515 switch (Opcode()) {
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700516 case INVOKE_VIRTUAL_RANGE:
517 case INVOKE_SUPER_RANGE:
518 case INVOKE_DIRECT_RANGE:
519 case INVOKE_STATIC_RANGE:
520 case INVOKE_INTERFACE_RANGE:
521 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200522 uint32_t method_idx = VRegB_3rc();
523 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
524 << PrettyMethod(method_idx, *file) << " // method@" << method_idx;
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700525 break;
526 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200527 case INVOKE_VIRTUAL_RANGE_QUICK:
528 if (file != NULL) {
529 uint32_t method_idx = VRegB_3rc();
530 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
531 << "// vtable@" << method_idx;
532 break;
533 } // else fall-through
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700534 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200535 os << StringPrintf("%s, {v%d .. v%d}, thing@%d", opcode, VRegC_3rc(),
536 (VRegC_3rc() + VRegA_3rc() - 1), VRegB_3rc());
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700537 break;
538 }
539 break;
540 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200541 case k51l: os << StringPrintf("%s v%d, #%+lld", opcode, VRegA_51l(), VRegB_51l()); break;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700542 default: os << " unknown format (" << DumpHex(5) << ")"; break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700544 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700545}
546
Ian Rogersa75a0132012-09-28 11:41:42 -0700547std::ostream& operator<<(std::ostream& os, const Instruction::Code& code) {
548 return os << Instruction::Name(code);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800549}
550
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700551} // namespace art