blob: 11a2d97691e31707e47980b9382f1fa8c8299357 [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"
David Sehr0225f8e2018-01-31 08:52:24 +000027#include "utf.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[] = {
Andreas Gampeae08cc22017-05-15 10:23:02 -070034#define INSTRUCTION_NAME(o, c, pname, f, i, a, e, 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
Andreas Gampeb3937e32017-05-15 15:11:56 -070041static_assert(sizeof(Instruction::InstructionDescriptor) == 8u, "Unexpected descriptor size");
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070042
Andreas Gampeb3937e32017-05-15 15:11:56 -070043static constexpr int8_t InstructionSizeInCodeUnitsByOpcode(Instruction::Code opcode,
44 Instruction::Format format) {
45 if (opcode == Instruction::Code::NOP) {
46 return -1;
47 } else if ((format >= Instruction::Format::k10x) && (format <= Instruction::Format::k10t)) {
48 return 1;
49 } else if ((format >= Instruction::Format::k20t) && (format <= Instruction::Format::k22c)) {
50 return 2;
51 } else if ((format >= Instruction::Format::k32x) && (format <= Instruction::Format::k3rc)) {
52 return 3;
53 } else if ((format >= Instruction::Format::k45cc) && (format <= Instruction::Format::k4rcc)) {
54 return 4;
55 } else if (format == Instruction::Format::k51l) {
56 return 5;
57 } else {
58 return -1;
59 }
60}
Aart Bikb1f37532015-06-29 11:03:55 -070061
Andreas Gampeb3937e32017-05-15 15:11:56 -070062Instruction::InstructionDescriptor const Instruction::kInstructionDescriptors[] = {
63#define INSTRUCTION_DESCR(opcode, c, p, format, index, flags, eflags, vflags) \
64 { vflags, \
65 format, \
66 index, \
67 flags, \
68 InstructionSizeInCodeUnitsByOpcode((c), (format)), \
69 },
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070070#include "dex_instruction_list.h"
Andreas Gampeb3937e32017-05-15 15:11:56 -070071 DEX_INSTRUCTION_LIST(INSTRUCTION_DESCR)
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070072#undef DEX_INSTRUCTION_LIST
Andreas Gampeb3937e32017-05-15 15:11:56 -070073#undef INSTRUCTION_DESCR
Ian Rogersa75a0132012-09-28 11:41:42 -070074};
75
Dragos Sbirlea39f99272013-06-25 13:17:36 -070076int32_t Instruction::GetTargetOffset() const {
77 switch (FormatOf(Opcode())) {
78 // Cases for conditional branches follow.
79 case k22t: return VRegC_22t();
80 case k21t: return VRegB_21t();
81 // Cases for unconditional branches follow.
82 case k10t: return VRegA_10t();
83 case k20t: return VRegA_20t();
84 case k30t: return VRegA_30t();
85 default: LOG(FATAL) << "Tried to access the branch offset of an instruction " << Name() <<
86 " which does not have a target operand.";
87 }
Elliott Hughesc1896c92018-11-29 11:33:18 -080088 UNREACHABLE();
Dragos Sbirlea39f99272013-06-25 13:17:36 -070089}
90
91bool Instruction::CanFlowThrough() const {
92 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
93 uint16_t insn = *insns;
94 Code opcode = static_cast<Code>(insn & 0xFF);
95 return FlagsOf(opcode) & Instruction::kContinue;
96}
97
Ian Rogersa75a0132012-09-28 11:41:42 -070098size_t Instruction::SizeInCodeUnitsComplexOpcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070099 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Ian Rogersa75a0132012-09-28 11:41:42 -0700100 // Handle special NOP encoded variable length sequences.
101 switch (*insns) {
102 case kPackedSwitchSignature:
103 return (4 + insns[1] * 2);
104 case kSparseSwitchSignature:
105 return (2 + insns[1] * 4);
106 case kArrayDataSignature: {
107 uint16_t element_size = insns[1];
108 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
109 // The plus 1 is to round up for odd size and width.
110 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700111 }
Ian Rogersa75a0132012-09-28 11:41:42 -0700112 default:
113 if ((*insns & 0xFF) == 0) {
114 return 1; // NOP.
115 } else {
Ian Rogersfc787ec2014-10-09 21:56:44 -0700116 LOG(FATAL) << "Unreachable: " << DumpString(nullptr);
Ian Rogers2c4257b2014-10-24 14:20:06 -0700117 UNREACHABLE();
Ian Rogersa75a0132012-09-28 11:41:42 -0700118 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700119 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700120}
121
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700122size_t Instruction::CodeUnitsRequiredForSizeOfComplexOpcode() const {
123 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
124 // Handle special NOP encoded variable length sequences.
125 switch (*insns) {
126 case kPackedSwitchSignature:
127 FALLTHROUGH_INTENDED;
128 case kSparseSwitchSignature:
129 return 2;
130 case kArrayDataSignature:
131 return 4;
132 default:
133 if ((*insns & 0xFF) == 0) {
134 return 1; // NOP.
135 } else {
136 LOG(FATAL) << "Unreachable: " << DumpString(nullptr);
137 UNREACHABLE();
138 }
139 }
140}
141
Ian Rogers2c8a8572011-10-24 17:11:36 -0700142std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700143 size_t inst_length = SizeInCodeUnits();
144 if (inst_length > code_units) {
145 inst_length = code_units;
146 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700147 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700148 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
149 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700150 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700151 }
152 for (size_t i = inst_length; i < code_units; i++) {
153 os << " ";
154 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700155 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700156}
157
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800158std::string Instruction::DumpHexLE(size_t instr_code_units) const {
159 size_t inst_length = SizeInCodeUnits();
160 if (inst_length > instr_code_units) {
161 inst_length = instr_code_units;
162 }
163 std::ostringstream os;
164 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
165 for (size_t i = 0; i < inst_length; i++) {
Andreas Gampe4f0be4d2015-02-27 22:16:14 -0800166 os << StringPrintf("%02x%02x", static_cast<uint8_t>(insn[i] & 0x00FF),
167 static_cast<uint8_t>((insn[i] & 0xFF00) >> 8)) << " ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800168 }
169 for (size_t i = inst_length; i < instr_code_units; i++) {
Andreas Gampe4f0be4d2015-02-27 22:16:14 -0800170 os << " ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800171 }
172 return os.str();
173}
174
Ian Rogers2c8a8572011-10-24 17:11:36 -0700175std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700176 std::ostringstream os;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200177 const char* opcode = kInstructionNames[Opcode()];
Elliott Hughesadb8c672012-03-06 16:49:32 -0800178 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800179 case k10x: os << opcode; break;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200180 case k12x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_12x(), VRegB_12x()); break;
181 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_11n(), VRegB_11n()); break;
182 case k11x: os << StringPrintf("%s v%d", opcode, VRegA_11x()); break;
183 case k10t: os << StringPrintf("%s %+d", opcode, VRegA_10t()); break;
184 case k20t: os << StringPrintf("%s %+d", opcode, VRegA_20t()); break;
185 case k22x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_22x(), VRegB_22x()); break;
186 case k21t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_21t(), VRegB_21t()); break;
187 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_21s(), VRegB_21s()); break;
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700188 case k21h: {
189 // op vAA, #+BBBB0000[00000000]
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200190 if (Opcode() == CONST_HIGH16) {
191 uint32_t value = VRegB_21h() << 16;
192 os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, VRegA_21h(), value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700193 } else {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200194 uint64_t value = static_cast<uint64_t>(VRegB_21h()) << 48;
Ian Rogersb574c182014-01-23 19:51:19 -0800195 os << StringPrintf("%s v%d, #long %+" PRId64 " // 0x%" PRIx64, opcode, VRegA_21h(),
196 value, value);
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700197 }
198 }
199 break;
Ian Rogers90334e52012-06-06 20:22:20 -0700200 case k21c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200201 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700202 case CONST_STRING:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700203 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200204 uint32_t string_idx = VRegB_21c();
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600205 if (string_idx < file->NumStringIds()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800206 os << StringPrintf(
207 "const-string v%d, %s // string@%d",
208 VRegA_21c(),
209 PrintableString(file->StringDataByIdx(dex::StringIndex(string_idx))).c_str(),
210 string_idx);
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600211 } else {
212 os << StringPrintf("const-string v%d, <<invalid-string-idx-%d>> // string@%d",
213 VRegA_21c(),
214 string_idx,
215 string_idx);
216 }
Ian Rogers90334e52012-06-06 20:22:20 -0700217 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700218 }
219 FALLTHROUGH_INTENDED;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700220 case CHECK_CAST:
221 case CONST_CLASS:
Ian Rogers90334e52012-06-06 20:22:20 -0700222 case NEW_INSTANCE:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700223 if (file != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800224 dex::TypeIndex type_idx(VRegB_21c());
225 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", "
226 << file->PrettyType(type_idx) << " // type@" << type_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700227 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700228 }
229 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700230 case SGET:
231 case SGET_WIDE:
232 case SGET_OBJECT:
233 case SGET_BOOLEAN:
234 case SGET_BYTE:
235 case SGET_CHAR:
236 case SGET_SHORT:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700237 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200238 uint32_t field_idx = VRegB_21c();
David Sehr709b0702016-10-13 09:12:37 -0700239 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << file->PrettyField(field_idx, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200240 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700241 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700242 }
243 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700244 case SPUT:
245 case SPUT_WIDE:
246 case SPUT_OBJECT:
247 case SPUT_BOOLEAN:
248 case SPUT_BYTE:
249 case SPUT_CHAR:
250 case SPUT_SHORT:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700251 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200252 uint32_t field_idx = VRegB_21c();
David Sehr709b0702016-10-13 09:12:37 -0700253 os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << file->PrettyField(field_idx, true)
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200254 << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700255 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700256 }
257 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700258 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200259 os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_21c(), VRegB_21c());
Ian Rogers90334e52012-06-06 20:22:20 -0700260 break;
261 }
262 break;
263 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200264 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, VRegA_23x(), VRegB_23x(), VRegC_23x()); break;
265 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22b(), VRegB_22b(), VRegC_22b()); break;
266 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, VRegA_22t(), VRegB_22t(), VRegC_22t()); break;
267 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 -0700268 case k22c: {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200269 switch (Opcode()) {
Ian Rogers90334e52012-06-06 20:22:20 -0700270 case IGET:
271 case IGET_WIDE:
272 case IGET_OBJECT:
273 case IGET_BOOLEAN:
274 case IGET_BYTE:
275 case IGET_CHAR:
276 case IGET_SHORT:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700277 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200278 uint32_t field_idx = VRegC_22c();
Dragos Sbirleab43cef32013-05-23 11:59:20 -0700279 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
David Sehr709b0702016-10-13 09:12:37 -0700280 << file->PrettyField(field_idx, true) << " // field@" << field_idx;
Ian Rogers90334e52012-06-06 20:22:20 -0700281 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;
Ian Rogers90334e52012-06-06 20:22:20 -0700298 case INSTANCE_OF:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700299 if (file != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800300 dex::TypeIndex type_idx(VRegC_22c());
301 os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v"
302 << static_cast<int>(VRegB_22c()) << ", " << file->PrettyType(type_idx)
303 << " // type@" << type_idx.index_;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700304 break;
305 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700306 FALLTHROUGH_INTENDED;
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700307 case NEW_ARRAY:
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_;
Ian Rogers90334e52012-06-06 20:22:20 -0700313 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700314 }
315 FALLTHROUGH_INTENDED;
Ian Rogers90334e52012-06-06 20:22:20 -0700316 default:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200317 os << StringPrintf("%s v%d, v%d, thing@%d", opcode, VRegA_22c(), VRegB_22c(), VRegC_22c());
Ian Rogers90334e52012-06-06 20:22:20 -0700318 break;
319 }
320 break;
321 }
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200322 case k32x: os << StringPrintf("%s v%d, v%d", opcode, VRegA_32x(), VRegB_32x()); break;
323 case k30t: os << StringPrintf("%s %+d", opcode, VRegA_30t()); break;
324 case k31t: os << StringPrintf("%s v%d, %+d", opcode, VRegA_31t(), VRegB_31t()); break;
325 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, VRegA_31i(), VRegB_31i()); break;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700326 case k31c:
327 if (Opcode() == CONST_STRING_JUMBO) {
328 uint32_t string_idx = VRegB_31c();
Ian Rogersfc787ec2014-10-09 21:56:44 -0700329 if (file != nullptr) {
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600330 if (string_idx < file->NumStringIds()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800331 os << StringPrintf(
332 "%s v%d, %s // string@%d",
333 opcode,
334 VRegA_31c(),
335 PrintableString(file->StringDataByIdx(dex::StringIndex(string_idx))).c_str(),
336 string_idx);
Serdjuk, Nikolay Y67c5ddd2015-12-07 14:45:44 +0600337 } else {
338 os << StringPrintf("%s v%d, <<invalid-string-idx-%d>> // string@%d",
339 opcode,
340 VRegA_31c(),
341 string_idx,
342 string_idx);
343 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700344 } else {
345 os << StringPrintf("%s v%d, string@%d", opcode, VRegA_31c(), string_idx);
346 }
347 } else {
348 os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_31c(), VRegB_31c()); break;
349 }
350 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700351 case k35c: {
Orion Hodsonac141392017-01-13 11:53:47 +0000352 uint32_t arg[kMaxVarArgRegs];
Ian Rogers29a26482014-05-02 15:27:29 -0700353 GetVarArgs(arg);
Mathieu Chartierbc61cb52017-09-19 16:58:28 -0700354 auto DumpArgs = [&](size_t count) {
355 for (size_t i = 0; i < count; ++i) {
356 if (i != 0) {
357 os << ", ";
358 }
359 os << "v" << arg[i];
360 }
361 };
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200362 switch (Opcode()) {
Andreas Gampe7aca91d2014-03-31 18:10:53 -0700363 case FILLED_NEW_ARRAY:
364 {
Andreas Gampe7aca91d2014-03-31 18:10:53 -0700365 os << opcode << " {";
Mathieu Chartierbc61cb52017-09-19 16:58:28 -0700366 DumpArgs(VRegA_35c());
Andreas Gampe7aca91d2014-03-31 18:10:53 -0700367 os << "}, type@" << VRegB_35c();
368 }
369 break;
370
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 case INVOKE_VIRTUAL:
372 case INVOKE_SUPER:
373 case INVOKE_DIRECT:
374 case INVOKE_STATIC:
375 case INVOKE_INTERFACE:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700376 if (file != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800377 os << opcode << " {";
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200378 uint32_t method_idx = VRegB_35c();
Mathieu Chartierbc61cb52017-09-19 16:58:28 -0700379 DumpArgs(VRegA_35c());
David Sehr709b0702016-10-13 09:12:37 -0700380 os << "}, " << file->PrettyMethod(method_idx) << " // method@" << method_idx;
Ian Rogersd81871c2011-10-03 13:57:23 -0700381 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700382 }
383 FALLTHROUGH_INTENDED;
Orion Hodsonc069a302017-01-18 09:23:12 +0000384 case INVOKE_CUSTOM:
385 if (file != nullptr) {
386 os << opcode << " {";
387 uint32_t call_site_idx = VRegB_35c();
Mathieu Chartierbc61cb52017-09-19 16:58:28 -0700388 DumpArgs(VRegA_35c());
Orion Hodsonc069a302017-01-18 09:23:12 +0000389 os << "}, // call_site@" << call_site_idx;
390 break;
391 }
392 FALLTHROUGH_INTENDED;
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 default:
Mathieu Chartierbc61cb52017-09-19 16:58:28 -0700394 os << opcode << " {";
395 DumpArgs(VRegA_35c());
396 os << "}, thing@" << VRegB_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -0700397 break;
398 }
399 break;
400 }
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700401 case k3rc: {
Orion Hodsonc069a302017-01-18 09:23:12 +0000402 uint16_t first_reg = VRegC_3rc();
403 uint16_t last_reg = VRegC_3rc() + VRegA_3rc() - 1;
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200404 switch (Opcode()) {
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700405 case INVOKE_VIRTUAL_RANGE:
406 case INVOKE_SUPER_RANGE:
407 case INVOKE_DIRECT_RANGE:
408 case INVOKE_STATIC_RANGE:
409 case INVOKE_INTERFACE_RANGE:
Ian Rogersfc787ec2014-10-09 21:56:44 -0700410 if (file != nullptr) {
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200411 uint32_t method_idx = VRegB_3rc();
Orion Hodsonc069a302017-01-18 09:23:12 +0000412 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, first_reg, last_reg)
David Sehr709b0702016-10-13 09:12:37 -0700413 << file->PrettyMethod(method_idx) << " // method@" << method_idx;
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700414 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700415 }
416 FALLTHROUGH_INTENDED;
Orion Hodsonc069a302017-01-18 09:23:12 +0000417 case INVOKE_CUSTOM_RANGE:
418 if (file != nullptr) {
419 uint32_t call_site_idx = VRegB_3rc();
420 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, first_reg, last_reg)
421 << "// call_site@" << call_site_idx;
422 break;
423 }
424 FALLTHROUGH_INTENDED;
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700425 default:
Orion Hodsonc069a302017-01-18 09:23:12 +0000426 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, first_reg, last_reg)
427 << "thing@" << VRegB_3rc();
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700428 break;
429 }
430 break;
431 }
Orion Hodsonac141392017-01-13 11:53:47 +0000432 case k45cc: {
433 uint32_t arg[kMaxVarArgRegs];
434 GetVarArgs(arg);
Orion Hodson06d10a72018-05-14 08:53:38 +0100435 uint16_t method_idx = VRegB_45cc();
436 dex::ProtoIndex proto_idx(VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +0000437 os << opcode << " {";
Orion Hodson06d10a72018-05-14 08:53:38 +0100438 for (uint32_t i = 0; i < VRegA_45cc(); ++i) {
Orion Hodsonac141392017-01-13 11:53:47 +0000439 if (i != 0) {
440 os << ", ";
441 }
442 os << "v" << arg[i];
443 }
444 os << "}";
445 if (file != nullptr) {
Orion Hodson06d10a72018-05-14 08:53:38 +0100446 os << ", " << file->PrettyMethod(method_idx)
447 << ", " << file->GetShorty(proto_idx)
Orion Hodsonac141392017-01-13 11:53:47 +0000448 << " // ";
449 } else {
450 os << ", ";
451 }
452 os << "method@" << method_idx << ", proto@" << proto_idx;
453 break;
454 }
455 case k4rcc:
456 switch (Opcode()) {
457 case INVOKE_POLYMORPHIC_RANGE: {
458 if (file != nullptr) {
Orion Hodson06d10a72018-05-14 08:53:38 +0100459 uint16_t method_idx = VRegB_4rcc();
460 dex::ProtoIndex proto_idx(VRegH_4rcc());
Orion Hodsonac141392017-01-13 11:53:47 +0000461 os << opcode << ", {v" << VRegC_4rcc() << " .. v" << (VRegC_4rcc() + VRegA_4rcc())
Orion Hodson06d10a72018-05-14 08:53:38 +0100462 << "}, " << file->PrettyMethod(method_idx)
463 << ", " << file->GetShorty(dex::ProtoIndex(proto_idx))
Orion Hodsonac141392017-01-13 11:53:47 +0000464 << " // method@" << method_idx << ", proto@" << proto_idx;
465 break;
466 }
467 }
468 FALLTHROUGH_INTENDED;
469 default: {
Orion Hodson06d10a72018-05-14 08:53:38 +0100470 uint16_t method_idx = VRegB_4rcc();
471 dex::ProtoIndex proto_idx(VRegH_4rcc());
Orion Hodsonac141392017-01-13 11:53:47 +0000472 os << opcode << ", {v" << VRegC_4rcc() << " .. v" << (VRegC_4rcc() + VRegA_4rcc())
473 << "}, method@" << method_idx << ", proto@" << proto_idx;
474 }
475 }
476 break;
Ian Rogersb574c182014-01-23 19:51:19 -0800477 case k51l: os << StringPrintf("%s v%d, #%+" PRId64, opcode, VRegA_51l(), VRegB_51l()); break;
David Srbecky436f6c12019-05-22 13:28:42 +0100478 case kInvalidFormat: os << "<invalid-opcode-format>";
Ian Rogersd81871c2011-10-03 13:57:23 -0700479 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700480 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700481}
482
Orion Hodsond8104702017-05-02 12:25:45 +0100483// Add some checks that ensure the flags make sense. We need a subclass to be in the context of
484// Instruction. Otherwise the flags from the instruction list don't work.
485struct InstructionStaticAsserts : private Instruction {
486 #define IMPLIES(a, b) (!(a) || (b))
487
Andreas Gampeae08cc22017-05-15 10:23:02 -0700488 #define VAR_ARGS_CHECK(o, c, pname, f, i, a, e, v) \
Orion Hodsond8104702017-05-02 12:25:45 +0100489 static_assert(IMPLIES((f) == k35c || (f) == k45cc, \
490 ((v) & (kVerifyVarArg | kVerifyVarArgNonZero)) != 0), \
491 "Missing var-arg verification");
492 #include "dex_instruction_list.h"
493 DEX_INSTRUCTION_LIST(VAR_ARGS_CHECK)
494 #undef DEX_INSTRUCTION_LIST
495 #undef VAR_ARGS_CHECK
496
Andreas Gampeae08cc22017-05-15 10:23:02 -0700497 #define VAR_ARGS_RANGE_CHECK(o, c, pname, f, i, a, e, v) \
Orion Hodsond8104702017-05-02 12:25:45 +0100498 static_assert(IMPLIES((f) == k3rc || (f) == k4rcc, \
499 ((v) & (kVerifyVarArgRange | kVerifyVarArgRangeNonZero)) != 0), \
500 "Missing var-arg verification");
501 #include "dex_instruction_list.h"
502 DEX_INSTRUCTION_LIST(VAR_ARGS_RANGE_CHECK)
503 #undef DEX_INSTRUCTION_LIST
504 #undef VAR_ARGS_RANGE_CHECK
Andreas Gampee05cc662017-05-15 10:17:30 -0700505
Andreas Gampeae08cc22017-05-15 10:23:02 -0700506 #define EXPERIMENTAL_CHECK(o, c, pname, f, i, a, e, v) \
Andreas Gampee05cc662017-05-15 10:17:30 -0700507 static_assert(kHaveExperimentalInstructions || (((a) & kExperimental) == 0), \
508 "Unexpected experimental instruction.");
509 #include "dex_instruction_list.h"
510 DEX_INSTRUCTION_LIST(EXPERIMENTAL_CHECK)
511 #undef DEX_INSTRUCTION_LIST
512 #undef EXPERIMENTAL_CHECK
Orion Hodsond8104702017-05-02 12:25:45 +0100513};
514
Vladimir Marko9974e3c2020-06-10 16:27:06 +0100515std::ostream& operator<<(std::ostream& os, Instruction::Code code) {
Ian Rogersa75a0132012-09-28 11:41:42 -0700516 return os << Instruction::Name(code);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800517}
518
Orion Hodson960d4f72017-11-10 15:32:38 +0000519uint32_t RangeInstructionOperands::GetOperand(size_t operand_index) const {
520 DCHECK_LT(operand_index, GetNumberOfOperands());
521 return first_operand_ + operand_index;
522}
523
524uint32_t VarArgsInstructionOperands::GetOperand(size_t operand_index) const {
525 DCHECK_LT(operand_index, GetNumberOfOperands());
526 return operands_[operand_index];
527}
528
Orion Hodson928033d2018-02-07 05:30:54 +0000529uint32_t NoReceiverInstructionOperands::GetOperand(size_t operand_index) const {
530 DCHECK_LT(GetNumberOfOperands(), inner_->GetNumberOfOperands());
531 // The receiver is the first operand and since we're skipping it, we need to
532 // add 1 to the operand_index.
533 return inner_->GetOperand(operand_index + 1);
534}
535
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700536} // namespace art