blob: 37f3ac92e99a890aa3f3e5081721768ab75152a1 [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 Rogersb574c182014-01-23 19:51:19 -080019#include <inttypes.h>
20
21#include <iomanip>
Ian Rogersc7dd2952014-10-21 23:31:19 -070022#include <sstream>
Ian Rogersb574c182014-01-23 19:51:19 -080023
Andreas Gampe46ee31b2016-12-14 10:11:49 -080024#include "android-base/stringprintf.h"
25
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "utils.h"
Ian Rogersd81871c2011-10-03 13:57:23 -070028
Carl Shapiro12eb78e2011-06-24 14:51:06 -070029namespace art {
30
Andreas Gampe46ee31b2016-12-14 10:11:49 -080031using android::base::StringPrintf;
32
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070033const char* const Instruction::kInstructionNames[] = {
Narayan Kamathbd48b342016-08-01 17:32:37 +010034#define INSTRUCTION_NAME(o, c, pname, f, i, a, v) pname,
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_NAME)
37#undef DEX_INSTRUCTION_LIST
38#undef INSTRUCTION_NAME
39};
40
Elliott Hughesadb8c672012-03-06 16:49:32 -080041Instruction::Format const Instruction::kInstructionFormats[] = {
Narayan Kamathbd48b342016-08-01 17:32:37 +010042#define INSTRUCTION_FORMAT(o, c, p, format, i, a, v) format,
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_FORMAT)
45#undef DEX_INSTRUCTION_LIST
46#undef INSTRUCTION_FORMAT
47};
48
Aart Bikb1f37532015-06-29 11:03:55 -070049Instruction::IndexType const Instruction::kInstructionIndexTypes[] = {
Narayan Kamathbd48b342016-08-01 17:32:37 +010050#define INSTRUCTION_INDEX_TYPE(o, c, p, f, index, a, v) index,
Aart Bikb1f37532015-06-29 11:03:55 -070051#include "dex_instruction_list.h"
52 DEX_INSTRUCTION_LIST(INSTRUCTION_INDEX_TYPE)
53#undef DEX_INSTRUCTION_LIST
54#undef INSTRUCTION_FLAGS
55};
56
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070057int const Instruction::kInstructionFlags[] = {
Narayan Kamathbd48b342016-08-01 17:32:37 +010058#define INSTRUCTION_FLAGS(o, c, p, f, i, flags, v) flags,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070059#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070060 DEX_INSTRUCTION_LIST(INSTRUCTION_FLAGS)
61#undef DEX_INSTRUCTION_LIST
62#undef INSTRUCTION_FLAGS
63};
64
jeffhaoba5ebb92011-08-25 17:24:37 -070065int const Instruction::kInstructionVerifyFlags[] = {
Narayan Kamathbd48b342016-08-01 17:32:37 +010066#define INSTRUCTION_VERIFY_FLAGS(o, c, p, f, i, a, vflags) vflags,
jeffhaoba5ebb92011-08-25 17:24:37 -070067#include "dex_instruction_list.h"
68 DEX_INSTRUCTION_LIST(INSTRUCTION_VERIFY_FLAGS)
69#undef DEX_INSTRUCTION_LIST
70#undef INSTRUCTION_VERIFY_FLAGS
71};
72
Ian Rogersa75a0132012-09-28 11:41:42 -070073int const Instruction::kInstructionSizeInCodeUnits[] = {
Narayan Kamathbd48b342016-08-01 17:32:37 +010074#define INSTRUCTION_SIZE(opcode, c, p, format, i, a, v) \
Narayan Kamath8ec3bd22016-08-03 12:46:23 +010075 (((opcode) == NOP) ? -1 : \
76 (((format) >= k10x) && ((format) <= k10t)) ? 1 : \
77 (((format) >= k20t) && ((format) <= k22c)) ? 2 : \
78 (((format) >= k32x) && ((format) <= k3rc)) ? 3 : \
79 (((format) >= k45cc) && ((format) <= k4rcc)) ? 4 : \
80 ((format) == k51l) ? 5 : -1),
Ian Rogersa75a0132012-09-28 11:41:42 -070081#include "dex_instruction_list.h"
82 DEX_INSTRUCTION_LIST(INSTRUCTION_SIZE)
83#undef DEX_INSTRUCTION_LIST
84#undef INSTRUCTION_SIZE
85};
86
Dragos Sbirlea39f99272013-06-25 13:17:36 -070087int32_t Instruction::GetTargetOffset() const {
88 switch (FormatOf(Opcode())) {
89 // Cases for conditional branches follow.
90 case k22t: return VRegC_22t();
91 case k21t: return VRegB_21t();
92 // Cases for unconditional branches follow.
93 case k10t: return VRegA_10t();
94 case k20t: return VRegA_20t();
95 case k30t: return VRegA_30t();
96 default: LOG(FATAL) << "Tried to access the branch offset of an instruction " << Name() <<
97 " which does not have a target operand.";
98 }
99 return 0;
100}
101
102bool Instruction::CanFlowThrough() const {
103 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
104 uint16_t insn = *insns;
105 Code opcode = static_cast<Code>(insn & 0xFF);
106 return FlagsOf(opcode) & Instruction::kContinue;
107}
108
Ian Rogersa75a0132012-09-28 11:41:42 -0700109size_t Instruction::SizeInCodeUnitsComplexOpcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700110 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Ian Rogersa75a0132012-09-28 11:41:42 -0700111 // Handle special NOP encoded variable length sequences.
112 switch (*insns) {
113 case kPackedSwitchSignature:
114 return (4 + insns[1] * 2);
115 case kSparseSwitchSignature:
116 return (2 + insns[1] * 4);
117 case kArrayDataSignature: {
118 uint16_t element_size = insns[1];
119 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
120 // The plus 1 is to round up for odd size and width.
121 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700122 }
Ian Rogersa75a0132012-09-28 11:41:42 -0700123 default:
124 if ((*insns & 0xFF) == 0) {
125 return 1; // NOP.
126 } else {
Ian Rogersfc787ec2014-10-09 21:56:44 -0700127 LOG(FATAL) << "Unreachable: " << DumpString(nullptr);
Ian Rogers2c4257b2014-10-24 14:20:06 -0700128 UNREACHABLE();
Ian Rogersa75a0132012-09-28 11:41:42 -0700129 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700130 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700131}
132
Ian Rogers2c8a8572011-10-24 17:11:36 -0700133std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700134 size_t inst_length = SizeInCodeUnits();
135 if (inst_length > code_units) {
136 inst_length = code_units;
137 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700138 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700139 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
140 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700141 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700142 }
143 for (size_t i = inst_length; i < code_units; i++) {
144 os << " ";
145 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700146 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700147}
148
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800149std::string Instruction::DumpHexLE(size_t instr_code_units) const {
150 size_t inst_length = SizeInCodeUnits();
151 if (inst_length > instr_code_units) {
152 inst_length = instr_code_units;
153 }
154 std::ostringstream os;
155 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
156 for (size_t i = 0; i < inst_length; i++) {
Andreas Gampe4f0be4d2015-02-27 22:16:14 -0800157 os << StringPrintf("%02x%02x", static_cast<uint8_t>(insn[i] & 0x00FF),
158 static_cast<uint8_t>((insn[i] & 0xFF00) >> 8)) << " ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800159 }
160 for (size_t i = inst_length; i < instr_code_units; i++) {
Andreas Gampe4f0be4d2015-02-27 22:16:14 -0800161 os << " ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800162 }
163 return os.str();
164}
165
Ian Rogers2c8a8572011-10-24 17:11:36 -0700166std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700167 std::ostringstream os;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200168 const char* opcode = kInstructionNames[Opcode()];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800169 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800170 case k10x: os << opcode; break;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200171 case k12x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_12x(), VRegB_12x()); break;
172 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_11n(), VRegB_11n()); break;
173 case k11x: os << StringPrintf("%s v%d", opcode, VRegA_11x()); break;
174 case k10t: os << StringPrintf("%s %+d", opcode, VRegA_10t()); break;
175 case k20t: os << StringPrintf("%s %+d", opcode, VRegA_20t()); break;
176 case k22x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_22x(), VRegB_22x()); break;
177 case k21t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_21t(), VRegB_21t()); break;
178 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_21s(), VRegB_21s()); break;
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700179 case k21h: {
180 // op vAA, #+BBBB0000[00000000]
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200181 if (Opcode() == CONST_HIGH16) {
182 uint32_t value = VRegB_21h() << 16;
183 os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700184 } else {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200185 uint64_t value = static_cast<uint64_t>(VRegB_21h()) << 48;
Ian Rogersb574c182014-01-23 19:51:19 -0800186 os << StringPrintf("%s v%d, #long %+" PRId64 " // 0x%" PRIx64, opcode, VRegA_21h(),
187 value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700188 }
189 }
190 break;
Ian Rogers90334e52012-06-06 20:22:20 -0700191 case k21c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200192 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700193 case CONST_STRING:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700194 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200195 uint32_t string_idx = VRegB_21c();
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600196 if (string_idx < file->NumStringIds()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800197 os << StringPrintf(
198 "const-string v%d, %s // string@%d",
199 VRegA_21c(),
200 PrintableString(file->StringDataByIdx(dex::StringIndex(string_idx))).c_str(),
201 string_idx);
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600202 } else {
203 os << StringPrintf("const-string v%d, <<invalid-string-idx-%d>> // string@%d",
204 VRegA_21c(),
205 string_idx,
206 string_idx);
207 }
Ian Rogers90334e52012-06-06 20:22:20 -0700208 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700209 }
210 FALLTHROUGH_INTENDED;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700211 case CHECK_CAST:
212 case CONST_CLASS:
Ian Rogers90334e52012-06-06 20:22:20 -0700213 case NEW_INSTANCE:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700214 if (file != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800215 dex::TypeIndex type_idx(VRegB_21c());
216 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", "
217 << file->PrettyType(type_idx) << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700218 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700219 }
220 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700221 case SGET:
222 case SGET_WIDE:
223 case SGET_OBJECT:
224 case SGET_BOOLEAN:
225 case SGET_BYTE:
226 case SGET_CHAR:
227 case SGET_SHORT:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700228 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200229 uint32_t field_idx = VRegB_21c();
David Sehr709b0702016-10-13 09:12:37 -0700230 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << file->PrettyField(field_idx, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200231 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700232 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700233 }
234 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700235 case SPUT:
236 case SPUT_WIDE:
237 case SPUT_OBJECT:
238 case SPUT_BOOLEAN:
239 case SPUT_BYTE:
240 case SPUT_CHAR:
241 case SPUT_SHORT:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700242 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200243 uint32_t field_idx = VRegB_21c();
David Sehr709b0702016-10-13 09:12:37 -0700244 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << file->PrettyField(field_idx, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200245 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700246 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700247 }
248 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700249 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200250 os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_21c(), VRegB_21c());
Ian Rogers90334e52012-06-06 20:22:20 -0700251 break;
252 }
253 break;
254 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200255 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, VRegA_23x(), VRegB_23x(), VRegC_23x()); break;
256 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22b(), VRegB_22b(), VRegC_22b()); break;
257 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, VRegA_22t(), VRegB_22t(), VRegC_22t()); break;
258 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 -0700259 case k22c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200260 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700261 case IGET:
262 case IGET_WIDE:
263 case IGET_OBJECT:
264 case IGET_BOOLEAN:
265 case IGET_BYTE:
266 case IGET_CHAR:
267 case IGET_SHORT:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700268 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200269 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700270 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
David Sehr709b0702016-10-13 09:12:37 -0700271 << file->PrettyField(field_idx, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700272 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700273 }
274 FALLTHROUGH_INTENDED;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200275 case IGET_QUICK:
276 case IGET_OBJECT_QUICK:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700277 if (file != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200278 uint32_t field_idx = VRegC_22c();
279 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
280 << "// offset@" << field_idx;
281 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700282 }
283 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700284 case IPUT:
285 case IPUT_WIDE:
286 case IPUT_OBJECT:
287 case IPUT_BOOLEAN:
288 case IPUT_BYTE:
289 case IPUT_CHAR:
290 case IPUT_SHORT:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700291 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200292 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700293 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
David Sehr709b0702016-10-13 09:12:37 -0700294 << file->PrettyField(field_idx, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700295 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700296 }
297 FALLTHROUGH_INTENDED;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200298 case IPUT_QUICK:
299 case IPUT_OBJECT_QUICK:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700300 if (file != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200301 uint32_t field_idx = VRegC_22c();
302 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
303 << "// offset@" << field_idx;
304 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700305 }
306 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700307 case INSTANCE_OF:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700308 if (file != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800309 dex::TypeIndex type_idx(VRegC_22c());
310 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v"
311 << static_cast<int>(VRegB_22c()) << ", " << file->PrettyType(type_idx)
312 << " // type@" << type_idx.index_;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700313 break;
314 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700315 FALLTHROUGH_INTENDED;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700316 case NEW_ARRAY:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700317 if (file != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800318 dex::TypeIndex type_idx(VRegC_22c());
319 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v"
320 << static_cast<int>(VRegB_22c()) << ", " << file->PrettyType(type_idx)
321 << " // type@" << type_idx.index_;
Ian Rogers90334e52012-06-06 20:22:20 -0700322 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700323 }
324 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700325 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200326 os << StringPrintf("%s v%d, v%d, thing@%d", opcode, VRegA_22c(), VRegB_22c(), VRegC_22c());
Ian Rogers90334e52012-06-06 20:22:20 -0700327 break;
328 }
329 break;
330 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200331 case k32x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_32x(), VRegB_32x()); break;
332 case k30t: os << StringPrintf("%s %+d", opcode, VRegA_30t()); break;
333 case k31t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_31t(), VRegB_31t()); break;
334 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_31i(), VRegB_31i()); break;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700335 case k31c:
336 if (Opcode() == CONST_STRING_JUMBO) {
337 uint32_t string_idx = VRegB_31c();
Ian Rogersfc787ec2014-10-09 21:56:44 -0700338 if (file != nullptr) {
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600339 if (string_idx < file->NumStringIds()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800340 os << StringPrintf(
341 "%s v%d, %s // string@%d",
342 opcode,
343 VRegA_31c(),
344 PrintableString(file->StringDataByIdx(dex::StringIndex(string_idx))).c_str(),
345 string_idx);
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600346 } else {
347 os << StringPrintf("%s v%d, <<invalid-string-idx-%d>> // string@%d",
348 opcode,
349 VRegA_31c(),
350 string_idx,
351 string_idx);
352 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700353 } else {
354 os << StringPrintf("%s v%d, string@%d", opcode, VRegA_31c(), string_idx);
355 }
356 } else {
357 os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_31c(), VRegB_31c()); break;
358 }
359 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700360 case k35c: {
Orion Hodson02e30922016-12-01 10:33:51 +0000361 uint32_t arg[kMaxVarArgRegs];
Ian Rogers29a26482014-05-02 15:27:29 -0700362 GetVarArgs(arg);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200363 switch (Opcode()) {
Andreas Gampe7aca91d2014-03-31 18:10:53 -0700364 case FILLED_NEW_ARRAY:
365 {
366 const int32_t a = VRegA_35c();
367 os << opcode << " {";
368 for (int i = 0; i < a; ++i) {
369 if (i > 0) {
370 os << ", ";
371 }
372 os << "v" << arg[i];
373 }
374 os << "}, type@" << VRegB_35c();
375 }
376 break;
377
Ian Rogersd81871c2011-10-03 13:57:23 -0700378 case INVOKE_VIRTUAL:
379 case INVOKE_SUPER:
380 case INVOKE_DIRECT:
381 case INVOKE_STATIC:
382 case INVOKE_INTERFACE:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700383 if (file != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800384 os << opcode << " {";
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200385 uint32_t method_idx = VRegB_35c();
386 for (size_t i = 0; i < VRegA_35c(); ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800387 if (i != 0) {
388 os << ", ";
389 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200390 os << "v" << arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800391 }
David Sehr709b0702016-10-13 09:12:37 -0700392 os << "}, " << file->PrettyMethod(method_idx) << " // method@" << method_idx;
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700394 }
395 FALLTHROUGH_INTENDED;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200396 case INVOKE_VIRTUAL_QUICK:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700397 if (file != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398 os << opcode << " {";
399 uint32_t method_idx = VRegB_35c();
400 for (size_t i = 0; i < VRegA_35c(); ++i) {
401 if (i != 0) {
402 os << ", ";
403 }
404 os << "v" << arg[i];
405 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700406 os << "}, // vtable@" << method_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200407 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700408 }
409 FALLTHROUGH_INTENDED;
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200411 os << opcode << " {v" << arg[0] << ", v" << arg[1] << ", v" << arg[2]
412 << ", v" << arg[3] << ", v" << arg[4] << "}, thing@" << VRegB_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -0700413 break;
414 }
415 break;
416 }
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700417 case k3rc: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200418 switch (Opcode()) {
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700419 case INVOKE_VIRTUAL_RANGE:
420 case INVOKE_SUPER_RANGE:
421 case INVOKE_DIRECT_RANGE:
422 case INVOKE_STATIC_RANGE:
423 case INVOKE_INTERFACE_RANGE:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700424 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200425 uint32_t method_idx = VRegB_3rc();
426 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
David Sehr709b0702016-10-13 09:12:37 -0700427 << file->PrettyMethod(method_idx) << " // method@" << method_idx;
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700428 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700429 }
430 FALLTHROUGH_INTENDED;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200431 case INVOKE_VIRTUAL_RANGE_QUICK:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700432 if (file != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200433 uint32_t method_idx = VRegB_3rc();
434 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, VRegC_3rc(), (VRegC_3rc() + VRegA_3rc() - 1))
435 << "// vtable@" << method_idx;
436 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700437 }
438 FALLTHROUGH_INTENDED;
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700439 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200440 os << StringPrintf("%s, {v%d .. v%d}, thing@%d", opcode, VRegC_3rc(),
441 (VRegC_3rc() + VRegA_3rc() - 1), VRegB_3rc());
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700442 break;
443 }
444 break;
445 }
Orion Hodson02e30922016-12-01 10:33:51 +0000446 case k45cc: {
447 uint32_t arg[kMaxVarArgRegs];
448 GetVarArgs(arg);
449 uint32_t method_idx = VRegB_45cc();
450 uint32_t proto_idx = VRegH_45cc();
451 os << opcode << " {";
452 for (int i = 0; i < VRegA_45cc(); ++i) {
453 if (i != 0) {
454 os << ", ";
455 }
456 os << "v" << arg[i];
457 }
458 os << "}";
459 if (file != nullptr) {
460 os << ", " << file->PrettyMethod(method_idx) << ", " << file->GetShorty(proto_idx)
461 << " // ";
462 } else {
463 os << ", ";
464 }
465 os << "method@" << method_idx << ", proto@" << proto_idx;
466 break;
467 }
468 case k4rcc:
469 switch (Opcode()) {
470 case INVOKE_POLYMORPHIC_RANGE: {
471 if (file != nullptr) {
472 uint32_t method_idx = VRegB_4rcc();
473 uint32_t proto_idx = VRegH_4rcc();
474 os << opcode << ", {v" << VRegC_4rcc() << " .. v" << (VRegC_4rcc() + VRegA_4rcc())
475 << "}, " << file->PrettyMethod(method_idx) << ", " << file->GetShorty(proto_idx)
476 << " // method@" << method_idx << ", proto@" << proto_idx;
477 break;
478 }
479 }
480 FALLTHROUGH_INTENDED;
481 default: {
482 uint32_t method_idx = VRegB_4rcc();
483 uint32_t proto_idx = VRegH_4rcc();
484 os << opcode << ", {v" << VRegC_4rcc() << " .. v" << (VRegC_4rcc() + VRegA_4rcc())
485 << "}, method@" << method_idx << ", proto@" << proto_idx;
486 }
487 }
488 break;
Ian Rogersb574c182014-01-23 19:51:19 -0800489 case k51l: os << StringPrintf("%s v%d, #%+" PRId64, opcode, VRegA_51l(), VRegB_51l()); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700491 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700492}
493
Ian Rogersa75a0132012-09-28 11:41:42 -0700494std::ostream& operator<<(std::ostream& os, const Instruction::Code& code) {
495 return os << Instruction::Name(code);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800496}
497
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700498} // namespace art