blob: 0de752387e672d05061007558272ee554eb77e52 [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_instruction.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070018
Ian Rogersd81871c2011-10-03 13:57:23 -070019#include "dex_file.h"
20#include <iomanip>
21
Carl Shapiro12eb78e2011-06-24 14:51:06 -070022namespace art {
23
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070024const char* const Instruction::kInstructionNames[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070025#define INSTRUCTION_NAME(o, c, pname, f, r, i, a, v) pname,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070027 DEX_INSTRUCTION_LIST(INSTRUCTION_NAME)
28#undef DEX_INSTRUCTION_LIST
29#undef INSTRUCTION_NAME
30};
31
Elliott Hughesadb8c672012-03-06 16:49:32 -080032Instruction::Format const Instruction::kInstructionFormats[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070033#define INSTRUCTION_FORMAT(o, c, p, format, r, i, a, v) format,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070035 DEX_INSTRUCTION_LIST(INSTRUCTION_FORMAT)
36#undef DEX_INSTRUCTION_LIST
37#undef INSTRUCTION_FORMAT
38};
39
40int const Instruction::kInstructionFlags[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070041#define INSTRUCTION_FLAGS(o, c, p, f, r, i, flags, v) flags,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070043 DEX_INSTRUCTION_LIST(INSTRUCTION_FLAGS)
44#undef DEX_INSTRUCTION_LIST
45#undef INSTRUCTION_FLAGS
46};
47
jeffhaoba5ebb92011-08-25 17:24:37 -070048int const Instruction::kInstructionVerifyFlags[] = {
49#define INSTRUCTION_VERIFY_FLAGS(o, c, p, f, r, i, a, vflags) vflags,
50#include "dex_instruction_list.h"
51 DEX_INSTRUCTION_LIST(INSTRUCTION_VERIFY_FLAGS)
52#undef DEX_INSTRUCTION_LIST
53#undef INSTRUCTION_VERIFY_FLAGS
54};
55
Ian Rogersa75a0132012-09-28 11:41:42 -070056int const Instruction::kInstructionSizeInCodeUnits[] = {
57#define INSTRUCTION_SIZE(opcode, c, p, format, r, i, a, v) \
58 (( opcode == NOP ) ? -1 : \
59 ((format >= k10x) && (format <= k10t)) ? 1 : \
60 ((format >= k20t) && (format <= k22c)) ? 2 : \
61 ((format >= k32x) && (format <= k3rc)) ? 3 : \
62 ( format == k51l ) ? 5 : -1 \
63 ),
64#include "dex_instruction_list.h"
65 DEX_INSTRUCTION_LIST(INSTRUCTION_SIZE)
66#undef DEX_INSTRUCTION_LIST
67#undef INSTRUCTION_SIZE
68};
69
jeffhaoba5ebb92011-08-25 17:24:37 -070070/*
71 * Handy macros for helping decode instructions.
72 */
73#define FETCH(_offset) (insns[(_offset)])
74#define FETCH_u4(_offset) (fetch_u4_impl((_offset), insns))
75#define INST_A(_insn) (((uint16_t)(_insn) >> 8) & 0x0f)
76#define INST_B(_insn) ((uint16_t)(_insn) >> 12)
77#define INST_AA(_insn) ((_insn) >> 8)
78
79/* Helper for FETCH_u4, above. */
80static inline uint32_t fetch_u4_impl(uint32_t offset, const uint16_t* insns) {
81 return insns[offset] | ((uint32_t) insns[offset+1] << 16);
82}
83
84void Instruction::Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const {
85 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
86 uint16_t insn = *insns;
Ian Rogersa75a0132012-09-28 11:41:42 -070087 Code opcode = static_cast<Code>(insn & 0xFF);
jeffhaoba5ebb92011-08-25 17:24:37 -070088
Ian Rogersa75a0132012-09-28 11:41:42 -070089 switch (FormatOf(opcode)) {
jeffhaoba5ebb92011-08-25 17:24:37 -070090 case k10x: // op
91 /* nothing to do; copy the AA bits out for the verifier */
92 vA = INST_AA(insn);
93 break;
94 case k12x: // op vA, vB
95 vA = INST_A(insn);
96 vB = INST_B(insn);
97 break;
98 case k11n: // op vA, #+B
99 vA = INST_A(insn);
100 vB = (int32_t) (INST_B(insn) << 28) >> 28; // sign extend 4-bit value
101 break;
102 case k11x: // op vAA
103 vA = INST_AA(insn);
104 break;
105 case k10t: // op +AA
106 vA = (int8_t) INST_AA(insn); // sign-extend 8-bit value
107 break;
108 case k20t: // op +AAAA
109 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
110 break;
111 case k21c: // op vAA, thing@BBBB
112 case k22x: // op vAA, vBBBB
113 vA = INST_AA(insn);
114 vB = FETCH(1);
115 break;
116 case k21s: // op vAA, #+BBBB
117 case k21t: // op vAA, +BBBB
118 vA = INST_AA(insn);
119 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
120 break;
121 case k21h: // op vAA, #+BBBB0000[00000000]
122 vA = INST_AA(insn);
123 /*
124 * The value should be treated as right-zero-extended, but we don't
125 * actually do that here. Among other things, we don't know if it's
126 * the top bits of a 32- or 64-bit value.
127 */
128 vB = FETCH(1);
129 break;
130 case k23x: // op vAA, vBB, vCC
131 vA = INST_AA(insn);
132 vB = FETCH(1) & 0xff;
133 vC = FETCH(1) >> 8;
134 break;
135 case k22b: // op vAA, vBB, #+CC
136 vA = INST_AA(insn);
137 vB = FETCH(1) & 0xff;
138 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
139 break;
140 case k22s: // op vA, vB, #+CCCC
141 case k22t: // op vA, vB, +CCCC
142 vA = INST_A(insn);
143 vB = INST_B(insn);
144 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
145 break;
146 case k22c: // op vA, vB, thing@CCCC
147 vA = INST_A(insn);
148 vB = INST_B(insn);
149 vC = FETCH(1);
150 break;
151 case k30t: // op +AAAAAAAA
152 vA = FETCH_u4(1); // signed 32-bit value
153 break;
154 case k31t: // op vAA, +BBBBBBBB
155 case k31c: // op vAA, string@BBBBBBBB
156 vA = INST_AA(insn);
157 vB = FETCH_u4(1); // 32-bit value
158 break;
159 case k32x: // op vAAAA, vBBBB
160 vA = FETCH(1);
161 vB = FETCH(2);
162 break;
163 case k31i: // op vAA, #+BBBBBBBB
164 vA = INST_AA(insn);
165 vB = FETCH_u4(1); // signed 32-bit value
166 break;
167 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
168 {
169 /*
170 * Note that the fields mentioned in the spec don't appear in
171 * their "usual" positions here compared to most formats. This
172 * was done so that the field names for the argument count and
173 * reference index match between this format and the corresponding
174 * range formats (3rc and friends).
175 *
176 * Bottom line: The argument count is always in vA, and the
177 * method constant (or equivalent) is always in vB.
178 */
179 uint16_t regList;
180 int count;
181
182 vA = INST_B(insn); // This is labeled A in the spec.
183 vB = FETCH(1);
184 regList = FETCH(2);
185
186 count = vA;
187
188 /*
189 * Copy the argument registers into the arg[] array, and
190 * also copy the first argument (if any) into vC. (The
191 * DecodedInstruction structure doesn't have separate
192 * fields for {vD, vE, vF, vG}, so there's no need to make
193 * copies of those.) Note that cases 5..2 fall through.
194 */
195 switch (count) {
196 case 5: arg[4] = INST_A(insn);
197 case 4: arg[3] = (regList >> 12) & 0x0f;
198 case 3: arg[2] = (regList >> 8) & 0x0f;
199 case 2: arg[1] = (regList >> 4) & 0x0f;
200 case 1: vC = arg[0] = regList & 0x0f; break;
201 case 0: break; // Valid, but no need to do anything.
202 default:
203 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
204 return;
205 }
206 }
207 break;
208 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
209 vA = INST_AA(insn);
210 vB = FETCH(1);
211 vC = FETCH(2);
212 break;
213 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
214 vA = INST_AA(insn);
215 vB_wide = FETCH_u4(1) | ((uint64_t) FETCH_u4(3) << 32);
216 break;
217 default:
Ian Rogersa75a0132012-09-28 11:41:42 -0700218 LOG(ERROR) << "Can't decode unexpected format " << FormatOf(opcode) << " (op=" << opcode << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700219 return;
220 }
221}
222
Ian Rogersa75a0132012-09-28 11:41:42 -0700223size_t Instruction::SizeInCodeUnitsComplexOpcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700224 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Ian Rogersa75a0132012-09-28 11:41:42 -0700225 // Handle special NOP encoded variable length sequences.
226 switch (*insns) {
227 case kPackedSwitchSignature:
228 return (4 + insns[1] * 2);
229 case kSparseSwitchSignature:
230 return (2 + insns[1] * 4);
231 case kArrayDataSignature: {
232 uint16_t element_size = insns[1];
233 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
234 // The plus 1 is to round up for odd size and width.
235 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700236 }
Ian Rogersa75a0132012-09-28 11:41:42 -0700237 default:
238 if ((*insns & 0xFF) == 0) {
239 return 1; // NOP.
240 } else {
241 LOG(FATAL) << "Unreachable: " << DumpString(NULL);
242 return 0;
243 }
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700244 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700245}
246
Ian Rogers2c8a8572011-10-24 17:11:36 -0700247std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700248 size_t inst_length = SizeInCodeUnits();
249 if (inst_length > code_units) {
250 inst_length = code_units;
251 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700252 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700253 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
254 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700255 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700256 }
257 for (size_t i = inst_length; i < code_units; i++) {
258 os << " ";
259 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700260 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700261}
262
Ian Rogers2c8a8572011-10-24 17:11:36 -0700263std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700264 DecodedInstruction insn(this);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700265 std::ostringstream os;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800266 const char* opcode = kInstructionNames[insn.opcode];
267 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800268 case k10x: os << opcode; break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800269 case k12x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
270 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
271 case k11x: os << StringPrintf("%s v%d", opcode, insn.vA); break;
272 case k10t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800273 case k20t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
274 case k22x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
275 case k21t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
276 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700277 case k21h: {
278 // op vAA, #+BBBB0000[00000000]
279 if (insn.opcode == CONST_HIGH16) {
280 uint32_t value = insn.vB << 16;
281 os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, insn.vA, value, value);
282 } else {
283 uint64_t value = static_cast<uint64_t>(insn.vB) << 48;
284 os << StringPrintf("%s v%d, #long %+lld // 0x%llx", opcode, insn.vA, value, value);
285 }
286 }
287 break;
Ian Rogers90334e52012-06-06 20:22:20 -0700288 case k21c: {
289 switch (insn.opcode) {
Ian Rogers90334e52012-06-06 20:22:20 -0700290 case CONST_STRING:
291 if (file != NULL) {
292 os << StringPrintf("const-string v%d, \"%s\" // string@%d", insn.vA,
293 file->StringDataByIdx(insn.vB), insn.vB);
294 break;
295 } // else fall-through
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700296 case CHECK_CAST:
297 case CONST_CLASS:
Ian Rogers90334e52012-06-06 20:22:20 -0700298 case NEW_INSTANCE:
299 if (file != NULL) {
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700300 os << opcode << " " << PrettyType(insn.vB, *file) << " // type@" << insn.vB;
Ian Rogers90334e52012-06-06 20:22:20 -0700301 break;
302 } // else fall-through
303 case SGET:
304 case SGET_WIDE:
305 case SGET_OBJECT:
306 case SGET_BOOLEAN:
307 case SGET_BYTE:
308 case SGET_CHAR:
309 case SGET_SHORT:
310 if (file != NULL) {
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700311 os << opcode << " " << PrettyField(insn.vB, *file, true) << " // field@" << insn.vB;
Ian Rogers90334e52012-06-06 20:22:20 -0700312 break;
313 } // else fall-through
314 case SPUT:
315 case SPUT_WIDE:
316 case SPUT_OBJECT:
317 case SPUT_BOOLEAN:
318 case SPUT_BYTE:
319 case SPUT_CHAR:
320 case SPUT_SHORT:
321 if (file != NULL) {
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700322 os << opcode << " " << PrettyField(insn.vB, *file, true) << " // field@" << insn.vB;
Ian Rogers90334e52012-06-06 20:22:20 -0700323 break;
324 } // else fall-through
325 default:
326 os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB);
327 break;
328 }
329 break;
330 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800331 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, insn.vA, insn.vB, insn.vC); break;
332 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
333 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, insn.vA, insn.vB, insn.vC); break;
334 case k22s: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
Ian Rogers90334e52012-06-06 20:22:20 -0700335 case k22c: {
336 switch (insn.opcode) {
337 case IGET:
338 case IGET_WIDE:
339 case IGET_OBJECT:
340 case IGET_BOOLEAN:
341 case IGET_BYTE:
342 case IGET_CHAR:
343 case IGET_SHORT:
344 if (file != NULL) {
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700345 os << PrettyField(insn.vC, *file, true) << " // field@" << insn.vC;
Ian Rogers90334e52012-06-06 20:22:20 -0700346 break;
347 } // else fall-through
348 case IPUT:
349 case IPUT_WIDE:
350 case IPUT_OBJECT:
351 case IPUT_BOOLEAN:
352 case IPUT_BYTE:
353 case IPUT_CHAR:
354 case IPUT_SHORT:
355 if (file != NULL) {
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700356 os << opcode << " " << PrettyField(insn.vC, *file, true) << " // field@" << insn.vB;
Ian Rogers90334e52012-06-06 20:22:20 -0700357 break;
358 } // else fall-through
359 case INSTANCE_OF:
360 if (file != NULL) {
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700361 os << opcode << " " << PrettyType(insn.vC, *file) << " // type@" << insn.vC;
362 break;
363 }
364 case NEW_ARRAY:
365 if (file != NULL) {
366 os << opcode << " " << PrettyType(insn.vC, *file) << " // type@" << insn.vC;
Ian Rogers90334e52012-06-06 20:22:20 -0700367 break;
368 } // else fall-through
369 default:
370 os << StringPrintf("%s v%d, v%d, thing@%d", opcode, insn.vA, insn.vB, insn.vC);
371 break;
372 }
373 break;
374 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800375 case k32x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
376 case k30t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
377 case k31t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
378 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
379 case k31c: os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700380 case k35c: {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800381 switch (insn.opcode) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700382 case INVOKE_VIRTUAL:
383 case INVOKE_SUPER:
384 case INVOKE_DIRECT:
385 case INVOKE_STATIC:
386 case INVOKE_INTERFACE:
387 if (file != NULL) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800388 os << opcode << " {";
Elliott Hughesadb8c672012-03-06 16:49:32 -0800389 for (size_t i = 0; i < insn.vA; ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800390 if (i != 0) {
391 os << ", ";
392 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800393 os << "v" << insn.arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800394 }
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700395 os << "}, " << PrettyMethod(insn.vB, *file) << " // method@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700396 break;
397 } // else fall-through
398 default:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800399 os << opcode << " {v" << insn.arg[0] << ", v" << insn.arg[1] << ", v" << insn.arg[2]
400 << ", v" << insn.arg[3] << ", v" << insn.arg[4] << "}, thing@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700401 break;
402 }
403 break;
404 }
Ian Rogers4c5dd5a2012-09-07 11:27:28 -0700405 case k3rc: {
406 switch (insn.opcode) {
407 case INVOKE_VIRTUAL_RANGE:
408 case INVOKE_SUPER_RANGE:
409 case INVOKE_DIRECT_RANGE:
410 case INVOKE_STATIC_RANGE:
411 case INVOKE_INTERFACE_RANGE:
412 if (file != NULL) {
413 os << StringPrintf("%s, {v%d .. v%d}, ", opcode, insn.vC, (insn.vC + insn.vA - 1))
414 << PrettyMethod(insn.vB, *file) << " // method@" << insn.vB;
415 break;
416 } // else fall-through
417 default:
418 os << StringPrintf("%s, {v%d .. v%d}, thing@%d", opcode, insn.vC, (insn.vC + insn.vA - 1),
419 insn.vB);
420 break;
421 }
422 break;
423 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800424 case k51l: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700425 default: os << " unknown format (" << DumpHex(5) << ")"; break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700426 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700427 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700428}
429
Ian Rogersa75a0132012-09-28 11:41:42 -0700430std::ostream& operator<<(std::ostream& os, const Instruction::Code& code) {
431 return os << Instruction::Name(code);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800432}
433
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700434} // namespace art