blob: 427baf2d51da9624b4db4924fb7df42da55145e1 [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 Sbirlea8cc51622013-06-21 09:20:34 -070085
86bool Instruction::HasVRegC() const {
87 switch (FormatOf(Opcode())) {
88 case k23x: return true;
89 case k35c: return true;
90 case k3rc: return true;
91 default: return false;
92 }
93}
94
95bool Instruction::HasVRegB() const {
96 switch (FormatOf(Opcode())) {
97 case k12x: return true;
98 case k22b: return true;
99 case k22c: return true;
100 case k22s: return true;
101 case k22t: return true;
102 case k22x: return true;
103 case k32x: return true;
104 default: return false;
105 }
106}
107
108bool Instruction::HasVRegA() const {
109 switch (FormatOf(Opcode())) {
110 case k11n: return true;
111 case k11x: return true;
112 case k12x: return true;
113 case k21c: return true;
114 case k21h: return true;
115 case k21s: return true;
116 case k21t: return true;
117 case k22b: return true;
118 case k22c: return true;
119 case k22s: return true;
120 case k22t: return true;
121 case k22x: return true;
122 case k23x: return true;
123 case k31c: return true;
124 case k31i: return true;
125 case k31t: return true;
126 case k32x: return true;
127 case k51l: return true;
128 default: return false;
129 }
130}
131
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700132int32_t Instruction::VRegC() const {
133 switch (FormatOf(Opcode())) {
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700134 case k23x: return VRegC_23x();
135 case k35c: return VRegC_35c();
136 case k3rc: return VRegC_3rc();
137 default: LOG(FATAL) << "Tried to access vC of instruction " << Name() <<
138 " which has no C operand.";
139 }
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700140 return -1;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700141}
142
143int32_t Instruction::VRegB() const {
144 switch (FormatOf(Opcode())) {
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700145 case k12x: return VRegB_12x();
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700146 case k22b: return VRegB_22b();
147 case k22c: return VRegB_22c();
148 case k22s: return VRegB_22s();
149 case k22t: return VRegB_22t();
150 case k22x: return VRegB_22x();
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700151 case k32x: return VRegB_32x();
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700152 default: LOG(FATAL) << "Tried to access vB of instruction " << Name() <<
153 " which has no B operand.";
154 }
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700155 return -1;
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700156}
157
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700158int32_t Instruction::VRegA() const {
159 switch (FormatOf(Opcode())) {
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700160 case k11n: return VRegA_11n();
161 case k11x: return VRegA_11x();
162 case k12x: return VRegA_12x();
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700163 case k21c: return VRegA_21c();
164 case k21h: return VRegA_21h();
165 case k21s: return VRegA_21s();
166 case k21t: return VRegA_21t();
167 case k22b: return VRegA_22b();
168 case k22c: return VRegA_22c();
169 case k22s: return VRegA_22s();
170 case k22t: return VRegA_22t();
171 case k22x: return VRegA_22x();
172 case k23x: return VRegA_23x();
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700173 case k31c: return VRegA_31c();
174 case k31i: return VRegA_31i();
175 case k31t: return VRegA_31t();
176 case k32x: return VRegA_32x();
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700177 case k51l: return VRegA_51l();
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700178 default: LOG(FATAL) << "Tried to access vA of instruction " << Name() <<
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700179 " which has no A operand.";
180 }
Dragos Sbirlea8cc51622013-06-21 09:20:34 -0700181 return -1;
Dragos Sbirlead25de7a2013-06-21 09:20:34 -0700182}
183
Dragos Sbirlea39f99272013-06-25 13:17:36 -0700184int32_t Instruction::GetTargetOffset() const {
185 switch (FormatOf(Opcode())) {
186 // Cases for conditional branches follow.
187 case k22t: return VRegC_22t();
188 case k21t: return VRegB_21t();
189 // Cases for unconditional branches follow.
190 case k10t: return VRegA_10t();
191 case k20t: return VRegA_20t();
192 case k30t: return VRegA_30t();
193 default: LOG(FATAL) << "Tried to access the branch offset of an instruction " << Name() <<
194 " which does not have a target operand.";
195 }
196 return 0;
197}
198
199bool Instruction::CanFlowThrough() const {
200 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
201 uint16_t insn = *insns;
202 Code opcode = static_cast<Code>(insn & 0xFF);
203 return FlagsOf(opcode) & Instruction::kContinue;
204}
205
jeffhaoba5ebb92011-08-25 17:24:37 -0700206void Instruction::Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const {
207 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
208 uint16_t insn = *insns;
Ian Rogersa75a0132012-09-28 11:41:42 -0700209 Code opcode = static_cast<Code>(insn & 0xFF);
jeffhaoba5ebb92011-08-25 17:24:37 -0700210
Ian Rogersa75a0132012-09-28 11:41:42 -0700211 switch (FormatOf(opcode)) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700212 case k10x: // op
213 /* nothing to do; copy the AA bits out for the verifier */
214 vA = INST_AA(insn);
215 break;
216 case k12x: // op vA, vB
217 vA = INST_A(insn);
218 vB = INST_B(insn);
219 break;
220 case k11n: // op vA, #+B
221 vA = INST_A(insn);
222 vB = (int32_t) (INST_B(insn) << 28) >> 28; // sign extend 4-bit value
223 break;
224 case k11x: // op vAA
225 vA = INST_AA(insn);
226 break;
227 case k10t: // op +AA
228 vA = (int8_t) INST_AA(insn); // sign-extend 8-bit value
229 break;
230 case k20t: // op +AAAA
231 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
232 break;
233 case k21c: // op vAA, thing@BBBB
234 case k22x: // op vAA, vBBBB
235 vA = INST_AA(insn);
236 vB = FETCH(1);
237 break;
238 case k21s: // op vAA, #+BBBB
239 case k21t: // op vAA, +BBBB
240 vA = INST_AA(insn);
241 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
242 break;
243 case k21h: // op vAA, #+BBBB0000[00000000]
244 vA = INST_AA(insn);
245 /*
246 * The value should be treated as right-zero-extended, but we don't
247 * actually do that here. Among other things, we don't know if it's
248 * the top bits of a 32- or 64-bit value.
249 */
250 vB = FETCH(1);
251 break;
252 case k23x: // op vAA, vBB, vCC
253 vA = INST_AA(insn);
254 vB = FETCH(1) & 0xff;
255 vC = FETCH(1) >> 8;
256 break;
257 case k22b: // op vAA, vBB, #+CC
258 vA = INST_AA(insn);
259 vB = FETCH(1) & 0xff;
260 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
261 break;
262 case k22s: // op vA, vB, #+CCCC
263 case k22t: // op vA, vB, +CCCC
264 vA = INST_A(insn);
265 vB = INST_B(insn);
266 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
267 break;
268 case k22c: // op vA, vB, thing@CCCC
269 vA = INST_A(insn);
270 vB = INST_B(insn);
271 vC = FETCH(1);
272 break;
273 case k30t: // op +AAAAAAAA
Sebastien Hertz807a2562013-04-15 09:33:39 +0200274 vA = FETCH_uint32(1); // signed 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700275 break;
276 case k31t: // op vAA, +BBBBBBBB
277 case k31c: // op vAA, string@BBBBBBBB
278 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200279 vB = FETCH_uint32(1); // 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700280 break;
281 case k32x: // op vAAAA, vBBBB
282 vA = FETCH(1);
283 vB = FETCH(2);
284 break;
285 case k31i: // op vAA, #+BBBBBBBB
286 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200287 vB = FETCH_uint32(1); // signed 32-bit value
jeffhaoba5ebb92011-08-25 17:24:37 -0700288 break;
289 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
290 {
291 /*
292 * Note that the fields mentioned in the spec don't appear in
293 * their "usual" positions here compared to most formats. This
294 * was done so that the field names for the argument count and
295 * reference index match between this format and the corresponding
296 * range formats (3rc and friends).
297 *
298 * Bottom line: The argument count is always in vA, and the
299 * method constant (or equivalent) is always in vB.
300 */
301 uint16_t regList;
302 int count;
303
304 vA = INST_B(insn); // This is labeled A in the spec.
305 vB = FETCH(1);
306 regList = FETCH(2);
307
308 count = vA;
309
310 /*
311 * Copy the argument registers into the arg[] array, and
312 * also copy the first argument (if any) into vC. (The
313 * DecodedInstruction structure doesn't have separate
314 * fields for {vD, vE, vF, vG}, so there's no need to make
315 * copies of those.) Note that cases 5..2 fall through.
316 */
317 switch (count) {
318 case 5: arg[4] = INST_A(insn);
319 case 4: arg[3] = (regList >> 12) & 0x0f;
320 case 3: arg[2] = (regList >> 8) & 0x0f;
321 case 2: arg[1] = (regList >> 4) & 0x0f;
322 case 1: vC = arg[0] = regList & 0x0f; break;
323 case 0: break; // Valid, but no need to do anything.
324 default:
325 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
326 return;
327 }
328 }
329 break;
330 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
331 vA = INST_AA(insn);
332 vB = FETCH(1);
333 vC = FETCH(2);
334 break;
335 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
336 vA = INST_AA(insn);
Sebastien Hertz807a2562013-04-15 09:33:39 +0200337 vB_wide = FETCH_uint32(1) | ((uint64_t) FETCH_uint32(3) << 32);
jeffhaoba5ebb92011-08-25 17:24:37 -0700338 break;
339 default:
Ian Rogersa75a0132012-09-28 11:41:42 -0700340 LOG(ERROR) << "Can't decode unexpected format " << FormatOf(opcode) << " (op=" << opcode << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700341 return;
342 }
343}
344
Ian Rogersa75a0132012-09-28 11:41:42 -0700345size_t Instruction::SizeInCodeUnitsComplexOpcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700346 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Ian Rogersa75a0132012-09-28 11:41:42 -0700347 // Handle special NOP encoded variable length sequences.
348 switch (*insns) {
349 case kPackedSwitchSignature:
350 return (4 + insns[1] * 2);
351 case kSparseSwitchSignature:
352 return (2 + insns[1] * 4);
353 case kArrayDataSignature: {
354 uint16_t element_size = insns[1];
355 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
356 // The plus 1 is to round up for odd size and width.
357 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700358 }
Ian Rogersa75a0132012-09-28 11:41:42 -0700359 default:
360 if ((*insns & 0xFF) == 0) {
361 return 1; // NOP.
362 } else {
363 LOG(FATAL) << "Unreachable: " << DumpString(NULL);
364 return 0;
365 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700366 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700367}
368
Ian Rogers2c8a8572011-10-24 17:11:36 -0700369std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700370 size_t inst_length = SizeInCodeUnits();
371 if (inst_length > code_units) {
372 inst_length = code_units;
373 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700374 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700375 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
376 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700377 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700378 }
379 for (size_t i = inst_length; i < code_units; i++) {
380 os << " ";
381 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700382 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700383}
384
Ian Rogers2c8a8572011-10-24 17:11:36 -0700385std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700386 std::ostringstream os;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200387 const char* opcode = kInstructionNames[Opcode()];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800388 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800389 case k10x: os << opcode; break;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200390 case k12x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_12x(), VRegB_12x()); break;
391 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_11n(), VRegB_11n()); break;
392 case k11x: os << StringPrintf("%s v%d", opcode, VRegA_11x()); break;
393 case k10t: os << StringPrintf("%s %+d", opcode, VRegA_10t()); break;
394 case k20t: os << StringPrintf("%s %+d", opcode, VRegA_20t()); break;
395 case k22x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_22x(), VRegB_22x()); break;
396 case k21t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_21t(), VRegB_21t()); break;
397 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_21s(), VRegB_21s()); break;
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700398 case k21h: {
399 // op vAA, #+BBBB0000[00000000]
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200400 if (Opcode() == CONST_HIGH16) {
401 uint32_t value = VRegB_21h() << 16;
402 os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700403 } else {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200404 uint64_t value = static_cast<uint64_t>(VRegB_21h()) << 48;
405 os << StringPrintf("%s v%d, #long %+lld // 0x%llx", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700406 }
407 }
408 break;
Ian Rogers90334e52012-06-06 20:22:20 -0700409 case k21c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200410 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700411 case CONST_STRING:
412 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200413 uint32_t string_idx = VRegB_21c();
414 os << StringPrintf("const-string v%d, %s // string@%d", VRegA_21c(),
415 PrintableString(file->StringDataByIdx(string_idx)).c_str(), string_idx);
Ian Rogers90334e52012-06-06 20:22:20 -0700416 break;
417 } // else fall-through
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700418 case CHECK_CAST:
419 case CONST_CLASS:
Ian Rogers90334e52012-06-06 20:22:20 -0700420 case NEW_INSTANCE:
421 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200422 uint32_t type_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700423 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyType(type_idx, *file)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200424 << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700425 break;
426 } // else fall-through
427 case SGET:
428 case SGET_WIDE:
429 case SGET_OBJECT:
430 case SGET_BOOLEAN:
431 case SGET_BYTE:
432 case SGET_CHAR:
433 case SGET_SHORT:
434 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200435 uint32_t field_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700436 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyField(field_idx, *file, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200437 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700438 break;
439 } // else fall-through
440 case SPUT:
441 case SPUT_WIDE:
442 case SPUT_OBJECT:
443 case SPUT_BOOLEAN:
444 case SPUT_BYTE:
445 case SPUT_CHAR:
446 case SPUT_SHORT:
447 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200448 uint32_t field_idx = VRegB_21c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700449 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << PrettyField(field_idx, *file, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200450 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700451 break;
452 } // else fall-through
453 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200454 os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_21c(), VRegB_21c());
Ian Rogers90334e52012-06-06 20:22:20 -0700455 break;
456 }
457 break;
458 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200459 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, VRegA_23x(), VRegB_23x(), VRegC_23x()); break;
460 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22b(), VRegB_22b(), VRegC_22b()); break;
461 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, VRegA_22t(), VRegB_22t(), VRegC_22t()); break;
462 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 -0700463 case k22c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200464 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700465 case IGET:
466 case IGET_WIDE:
467 case IGET_OBJECT:
468 case IGET_BOOLEAN:
469 case IGET_BYTE:
470 case IGET_CHAR:
471 case IGET_SHORT:
472 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200473 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700474 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200475 << PrettyField(field_idx, *file, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700476 break;
477 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200478 case IGET_QUICK:
479 case IGET_OBJECT_QUICK:
480 if (file != NULL) {
481 uint32_t field_idx = VRegC_22c();
482 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
483 << "// offset@" << field_idx;
484 break;
485 } // else fall-through
Ian Rogers90334e52012-06-06 20:22:20 -0700486 case IPUT:
487 case IPUT_WIDE:
488 case IPUT_OBJECT:
489 case IPUT_BOOLEAN:
490 case IPUT_BYTE:
491 case IPUT_CHAR:
492 case IPUT_SHORT:
493 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200494 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700495 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200496 << PrettyField(field_idx, *file, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700497 break;
498 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200499 case IPUT_QUICK:
500 case IPUT_OBJECT_QUICK:
501 if (file != NULL) {
502 uint32_t field_idx = VRegC_22c();
503 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
504 << "// offset@" << field_idx;
505 break;
506 } // else fall-through
Ian Rogers90334e52012-06-06 20:22:20 -0700507 case INSTANCE_OF:
508 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200509 uint32_t type_idx = VRegC_22c();
Dragos Sbirlead4e868a2013-05-23 09:44:17 -0700510 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200511 << PrettyType(type_idx, *file) << " // type@" << type_idx;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700512 break;
513 }
514 case NEW_ARRAY:
515 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200516 uint32_t type_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700517 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200518 << PrettyType(type_idx, *file) << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700519 break;
520 } // else fall-through
521 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200522 os << StringPrintf("%s v%d, v%d, thing@%d", opcode, VRegA_22c(), VRegB_22c(), VRegC_22c());
Ian Rogers90334e52012-06-06 20:22:20 -0700523 break;
524 }
525 break;
526 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200527 case k32x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_32x(), VRegB_32x()); break;
528 case k30t: os << StringPrintf("%s %+d", opcode, VRegA_30t()); break;
529 case k31t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_31t(), VRegB_31t()); break;
530 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_31i(), VRegB_31i()); break;
531 case k31c: os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_31c(), VRegB_31c()); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700532 case k35c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200533 uint32_t arg[5];
534 GetArgs(arg);
535 switch (Opcode()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 case INVOKE_VIRTUAL:
537 case INVOKE_SUPER:
538 case INVOKE_DIRECT:
539 case INVOKE_STATIC:
540 case INVOKE_INTERFACE:
541 if (file != NULL) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800542 os << opcode << " {";
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200543 uint32_t method_idx = VRegB_35c();
544 for (size_t i = 0; i < VRegA_35c(); ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800545 if (i != 0) {
546 os << ", ";
547 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200548 os << "v" << arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800549 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200550 os << "}, " << PrettyMethod(method_idx, *file) << " // method@" << method_idx;
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 break;
552 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200553 case INVOKE_VIRTUAL_QUICK:
554 if (file != NULL) {
555 os << opcode << " {";
556 uint32_t method_idx = VRegB_35c();
557 for (size_t i = 0; i < VRegA_35c(); ++i) {
558 if (i != 0) {
559 os << ", ";
560 }
561 os << "v" << arg[i];
562 }
563 os << "}, // vtable@" << method_idx;
564 break;
565 } // else fall-through
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200567 os << opcode << " {v" << arg[0] << ", v" << arg[1] << ", v" << arg[2]
568 << ", v" << arg[3] << ", v" << arg[4] << "}, thing@" << VRegB_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -0700569 break;
570 }
571 break;
572 }
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700573 case k3rc: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200574 switch (Opcode()) {
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700575 case INVOKE_VIRTUAL_RANGE:
576 case INVOKE_SUPER_RANGE:
577 case INVOKE_DIRECT_RANGE:
578 case INVOKE_STATIC_RANGE:
579 case INVOKE_INTERFACE_RANGE:
580 if (file != NULL) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200581 uint32_t method_idx = VRegB_3rc();
582 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
583 << PrettyMethod(method_idx, *file) << " // method@" << method_idx;
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700584 break;
585 } // else fall-through
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200586 case INVOKE_VIRTUAL_RANGE_QUICK:
587 if (file != NULL) {
588 uint32_t method_idx = VRegB_3rc();
589 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
590 << "// vtable@" << method_idx;
591 break;
592 } // else fall-through
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700593 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200594 os << StringPrintf("%s, {v%d .. v%d}, thing@%d", opcode, VRegC_3rc(),
595 (VRegC_3rc() + VRegA_3rc() - 1), VRegB_3rc());
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700596 break;
597 }
598 break;
599 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200600 case k51l: os << StringPrintf("%s v%d, #%+lld", opcode, VRegA_51l(), VRegB_51l()); break;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700601 default: os << " unknown format (" << DumpHex(5) << ")"; break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700603 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700604}
605
Ian Rogersa75a0132012-09-28 11:41:42 -0700606std::ostream& operator<<(std::ostream& os, const Instruction::Code& code) {
607 return os << Instruction::Name(code);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800608}
609
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700610} // namespace art