blob: 874432626e493f878409d5ae90176f05418ba949 [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
56/*
57 * Handy macros for helping decode instructions.
58 */
59#define FETCH(_offset) (insns[(_offset)])
60#define FETCH_u4(_offset) (fetch_u4_impl((_offset), insns))
61#define INST_A(_insn) (((uint16_t)(_insn) >> 8) & 0x0f)
62#define INST_B(_insn) ((uint16_t)(_insn) >> 12)
63#define INST_AA(_insn) ((_insn) >> 8)
64
65/* Helper for FETCH_u4, above. */
66static inline uint32_t fetch_u4_impl(uint32_t offset, const uint16_t* insns) {
67 return insns[offset] | ((uint32_t) insns[offset+1] << 16);
68}
69
70void Instruction::Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const {
71 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
72 uint16_t insn = *insns;
73 int opcode = insn & 0xFF;
74
Elliott Hughesadb8c672012-03-06 16:49:32 -080075 switch (FormatOf(Opcode())) {
jeffhaoba5ebb92011-08-25 17:24:37 -070076 case k10x: // op
77 /* nothing to do; copy the AA bits out for the verifier */
78 vA = INST_AA(insn);
79 break;
80 case k12x: // op vA, vB
81 vA = INST_A(insn);
82 vB = INST_B(insn);
83 break;
84 case k11n: // op vA, #+B
85 vA = INST_A(insn);
86 vB = (int32_t) (INST_B(insn) << 28) >> 28; // sign extend 4-bit value
87 break;
88 case k11x: // op vAA
89 vA = INST_AA(insn);
90 break;
91 case k10t: // op +AA
92 vA = (int8_t) INST_AA(insn); // sign-extend 8-bit value
93 break;
94 case k20t: // op +AAAA
95 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
96 break;
jeffhaocfe8db42012-03-13 12:37:36 -070097 case k20bc: // op AA, kind@BBBB
jeffhaoba5ebb92011-08-25 17:24:37 -070098 case k21c: // op vAA, thing@BBBB
99 case k22x: // op vAA, vBBBB
100 vA = INST_AA(insn);
101 vB = FETCH(1);
102 break;
103 case k21s: // op vAA, #+BBBB
104 case k21t: // op vAA, +BBBB
105 vA = INST_AA(insn);
106 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
107 break;
108 case k21h: // op vAA, #+BBBB0000[00000000]
109 vA = INST_AA(insn);
110 /*
111 * The value should be treated as right-zero-extended, but we don't
112 * actually do that here. Among other things, we don't know if it's
113 * the top bits of a 32- or 64-bit value.
114 */
115 vB = FETCH(1);
116 break;
117 case k23x: // op vAA, vBB, vCC
118 vA = INST_AA(insn);
119 vB = FETCH(1) & 0xff;
120 vC = FETCH(1) >> 8;
121 break;
122 case k22b: // op vAA, vBB, #+CC
123 vA = INST_AA(insn);
124 vB = FETCH(1) & 0xff;
125 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
126 break;
127 case k22s: // op vA, vB, #+CCCC
128 case k22t: // op vA, vB, +CCCC
129 vA = INST_A(insn);
130 vB = INST_B(insn);
131 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
132 break;
133 case k22c: // op vA, vB, thing@CCCC
134 vA = INST_A(insn);
135 vB = INST_B(insn);
136 vC = FETCH(1);
137 break;
138 case k30t: // op +AAAAAAAA
139 vA = FETCH_u4(1); // signed 32-bit value
140 break;
141 case k31t: // op vAA, +BBBBBBBB
142 case k31c: // op vAA, string@BBBBBBBB
143 vA = INST_AA(insn);
144 vB = FETCH_u4(1); // 32-bit value
145 break;
146 case k32x: // op vAAAA, vBBBB
147 vA = FETCH(1);
148 vB = FETCH(2);
149 break;
150 case k31i: // op vAA, #+BBBBBBBB
151 vA = INST_AA(insn);
152 vB = FETCH_u4(1); // signed 32-bit value
153 break;
154 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
155 {
156 /*
157 * Note that the fields mentioned in the spec don't appear in
158 * their "usual" positions here compared to most formats. This
159 * was done so that the field names for the argument count and
160 * reference index match between this format and the corresponding
161 * range formats (3rc and friends).
162 *
163 * Bottom line: The argument count is always in vA, and the
164 * method constant (or equivalent) is always in vB.
165 */
166 uint16_t regList;
167 int count;
168
169 vA = INST_B(insn); // This is labeled A in the spec.
170 vB = FETCH(1);
171 regList = FETCH(2);
172
173 count = vA;
174
175 /*
176 * Copy the argument registers into the arg[] array, and
177 * also copy the first argument (if any) into vC. (The
178 * DecodedInstruction structure doesn't have separate
179 * fields for {vD, vE, vF, vG}, so there's no need to make
180 * copies of those.) Note that cases 5..2 fall through.
181 */
182 switch (count) {
183 case 5: arg[4] = INST_A(insn);
184 case 4: arg[3] = (regList >> 12) & 0x0f;
185 case 3: arg[2] = (regList >> 8) & 0x0f;
186 case 2: arg[1] = (regList >> 4) & 0x0f;
187 case 1: vC = arg[0] = regList & 0x0f; break;
188 case 0: break; // Valid, but no need to do anything.
189 default:
190 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
191 return;
192 }
193 }
194 break;
195 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
196 vA = INST_AA(insn);
197 vB = FETCH(1);
198 vC = FETCH(2);
199 break;
200 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
201 vA = INST_AA(insn);
202 vB_wide = FETCH_u4(1) | ((uint64_t) FETCH_u4(3) << 32);
203 break;
204 default:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800205 LOG(ERROR) << "Can't decode unexpected format " << static_cast<int>(FormatOf(Opcode())) << " (op=" << opcode << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700206 return;
207 }
208}
209
Ian Rogersd81871c2011-10-03 13:57:23 -0700210size_t Instruction::SizeInCodeUnits() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700211 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800212 if (*insns == Instruction::kPackedSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700213 return (4 + insns[1] * 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800214 } else if (*insns == Instruction::kSparseSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700215 return (2 + insns[1] * 4);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700216 } else if (*insns == kArrayDataSignature) {
217 uint16_t element_size = insns[1];
218 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
219 // The plus 1 is to round up for odd size and width.
jeffhaoba5ebb92011-08-25 17:24:37 -0700220 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700221 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800222 switch (FormatOf(Opcode())) {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700223 case k10x:
224 case k12x:
225 case k11n:
226 case k11x:
227 case k10t:
jeffhaoba5ebb92011-08-25 17:24:37 -0700228 return 1;
Ian Rogers9fdfc182011-10-26 23:12:52 -0700229 case k20bc:
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700230 case k20t:
231 case k22x:
232 case k21t:
233 case k21s:
234 case k21h:
235 case k21c:
236 case k23x:
237 case k22b:
238 case k22t:
239 case k22s:
240 case k22c:
jeffhaoba5ebb92011-08-25 17:24:37 -0700241 return 2;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700242 case k32x:
243 case k30t:
244 case k31t:
245 case k31i:
246 case k31c:
247 case k35c:
248 case k3rc:
jeffhaoba5ebb92011-08-25 17:24:37 -0700249 return 3;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700250 case k51l:
jeffhaoba5ebb92011-08-25 17:24:37 -0700251 return 5;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700252 default:
253 LOG(FATAL) << "Unreachable";
254 }
255 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700256 return 0;
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700257}
258
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700259Instruction::Code Instruction::Opcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700260 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700261 int opcode = *insns & 0xFF;
262 return static_cast<Code>(opcode);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700263}
264
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700265const Instruction* Instruction::Next() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700266 size_t current_size_in_bytes = SizeInCodeUnits() * sizeof(uint16_t);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700267 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
Ian Rogersd81871c2011-10-03 13:57:23 -0700268 return reinterpret_cast<const Instruction*>(ptr + current_size_in_bytes);
269}
270
Ian Rogers2c8a8572011-10-24 17:11:36 -0700271std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700272 size_t inst_length = SizeInCodeUnits();
273 if (inst_length > code_units) {
274 inst_length = code_units;
275 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700276 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700277 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
278 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700279 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700280 }
281 for (size_t i = inst_length; i < code_units; i++) {
282 os << " ";
283 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700284 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700285}
286
Ian Rogers2c8a8572011-10-24 17:11:36 -0700287std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700288 DecodedInstruction insn(this);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700289 std::ostringstream os;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800290 const char* opcode = kInstructionNames[insn.opcode];
291 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800292 case k10x: os << opcode; break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800293 case k12x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
294 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
295 case k11x: os << StringPrintf("%s v%d", opcode, insn.vA); break;
296 case k10t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
297 case k20bc: os << StringPrintf("%s %d, kind@%d", opcode, insn.vA, insn.vB); break;
298 case k20t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
299 case k22x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
300 case k21t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
301 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
Elliott Hughes1b3d6ca2012-04-25 13:00:14 -0700302 case k21h: {
303 // op vAA, #+BBBB0000[00000000]
304 if (insn.opcode == CONST_HIGH16) {
305 uint32_t value = insn.vB << 16;
306 os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, insn.vA, value, value);
307 } else {
308 uint64_t value = static_cast<uint64_t>(insn.vB) << 48;
309 os << StringPrintf("%s v%d, #long %+lld // 0x%llx", opcode, insn.vA, value, value);
310 }
311 }
312 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800313 case k21c: os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB); break;
314 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, insn.vA, insn.vB, insn.vC); break;
315 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
316 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, insn.vA, insn.vB, insn.vC); break;
317 case k22s: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
318 case k22c: os << StringPrintf("%s v%d, v%d, thing@%d", opcode, insn.vA, insn.vB, insn.vC); break;
319 case k32x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
320 case k30t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
321 case k31t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
322 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
323 case k31c: os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700324 case k35c: {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800325 switch (insn.opcode) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700326 case INVOKE_VIRTUAL:
327 case INVOKE_SUPER:
328 case INVOKE_DIRECT:
329 case INVOKE_STATIC:
330 case INVOKE_INTERFACE:
331 if (file != NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800332 const DexFile::MethodId& meth_id = file->GetMethodId(insn.vB);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800333 os << opcode << " {";
Elliott Hughesadb8c672012-03-06 16:49:32 -0800334 for (size_t i = 0; i < insn.vA; ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800335 if (i != 0) {
336 os << ", ";
337 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800338 os << "v" << insn.arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800339 }
340 os << "}, "
341 << file->GetMethodDeclaringClassDescriptor(meth_id) << "."
342 << file->GetMethodName(meth_id) << file->GetMethodSignature(meth_id)
Elliott Hughesadb8c672012-03-06 16:49:32 -0800343 << " // method@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700344 break;
345 } // else fall-through
346 default:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800347 os << opcode << " {v" << insn.arg[0] << ", v" << insn.arg[1] << ", v" << insn.arg[2]
348 << ", v" << insn.arg[3] << ", v" << insn.arg[4] << "}, thing@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700349 break;
350 }
351 break;
352 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800353 case k3rc: os << StringPrintf("%s, {v%d .. v%d}, method@%d", opcode, insn.vC, (insn.vC + insn.vA - 1), insn.vB); break;
354 case k51l: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700355 default: os << " unknown format (" << DumpHex(5) << ")"; break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700356 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700357 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700358}
359
Elliott Hughesadb8c672012-03-06 16:49:32 -0800360DecodedInstruction::DecodedInstruction(const Instruction* inst) {
361 inst->Decode(vA, vB, vB_wide, vC, arg);
362 opcode = inst->Opcode();
363}
364
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700365} // namespace art