blob: d768c0b3e74d6adcd9c898b60233e4d5c13c8720 [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;
jeffhaoe0cfb6f2011-09-22 16:42:56 -070094 case k20bc: // op AA, kind@BBBB
jeffhaoe89df502012-03-07 12:15:46 -080095 vA = INST_A(insn);
96 vB = INST_B(insn);
jeffhaoe0cfb6f2011-09-22 16:42:56 -070097 break;
jeffhaoba5ebb92011-08-25 17:24:37 -070098 case k20t: // op +AAAA
99 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
100 break;
101 case k21c: // op vAA, thing@BBBB
102 case k22x: // op vAA, vBBBB
103 vA = INST_AA(insn);
104 vB = FETCH(1);
105 break;
106 case k21s: // op vAA, #+BBBB
107 case k21t: // op vAA, +BBBB
108 vA = INST_AA(insn);
109 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
110 break;
111 case k21h: // op vAA, #+BBBB0000[00000000]
112 vA = INST_AA(insn);
113 /*
114 * The value should be treated as right-zero-extended, but we don't
115 * actually do that here. Among other things, we don't know if it's
116 * the top bits of a 32- or 64-bit value.
117 */
118 vB = FETCH(1);
119 break;
120 case k23x: // op vAA, vBB, vCC
121 vA = INST_AA(insn);
122 vB = FETCH(1) & 0xff;
123 vC = FETCH(1) >> 8;
124 break;
125 case k22b: // op vAA, vBB, #+CC
126 vA = INST_AA(insn);
127 vB = FETCH(1) & 0xff;
128 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
129 break;
130 case k22s: // op vA, vB, #+CCCC
131 case k22t: // op vA, vB, +CCCC
132 vA = INST_A(insn);
133 vB = INST_B(insn);
134 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
135 break;
136 case k22c: // op vA, vB, thing@CCCC
137 vA = INST_A(insn);
138 vB = INST_B(insn);
139 vC = FETCH(1);
140 break;
141 case k30t: // op +AAAAAAAA
142 vA = FETCH_u4(1); // signed 32-bit value
143 break;
144 case k31t: // op vAA, +BBBBBBBB
145 case k31c: // op vAA, string@BBBBBBBB
146 vA = INST_AA(insn);
147 vB = FETCH_u4(1); // 32-bit value
148 break;
149 case k32x: // op vAAAA, vBBBB
150 vA = FETCH(1);
151 vB = FETCH(2);
152 break;
153 case k31i: // op vAA, #+BBBBBBBB
154 vA = INST_AA(insn);
155 vB = FETCH_u4(1); // signed 32-bit value
156 break;
157 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
158 {
159 /*
160 * Note that the fields mentioned in the spec don't appear in
161 * their "usual" positions here compared to most formats. This
162 * was done so that the field names for the argument count and
163 * reference index match between this format and the corresponding
164 * range formats (3rc and friends).
165 *
166 * Bottom line: The argument count is always in vA, and the
167 * method constant (or equivalent) is always in vB.
168 */
169 uint16_t regList;
170 int count;
171
172 vA = INST_B(insn); // This is labeled A in the spec.
173 vB = FETCH(1);
174 regList = FETCH(2);
175
176 count = vA;
177
178 /*
179 * Copy the argument registers into the arg[] array, and
180 * also copy the first argument (if any) into vC. (The
181 * DecodedInstruction structure doesn't have separate
182 * fields for {vD, vE, vF, vG}, so there's no need to make
183 * copies of those.) Note that cases 5..2 fall through.
184 */
185 switch (count) {
186 case 5: arg[4] = INST_A(insn);
187 case 4: arg[3] = (regList >> 12) & 0x0f;
188 case 3: arg[2] = (regList >> 8) & 0x0f;
189 case 2: arg[1] = (regList >> 4) & 0x0f;
190 case 1: vC = arg[0] = regList & 0x0f; break;
191 case 0: break; // Valid, but no need to do anything.
192 default:
193 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
194 return;
195 }
196 }
197 break;
198 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
199 vA = INST_AA(insn);
200 vB = FETCH(1);
201 vC = FETCH(2);
202 break;
203 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
204 vA = INST_AA(insn);
205 vB_wide = FETCH_u4(1) | ((uint64_t) FETCH_u4(3) << 32);
206 break;
207 default:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800208 LOG(ERROR) << "Can't decode unexpected format " << static_cast<int>(FormatOf(Opcode())) << " (op=" << opcode << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700209 return;
210 }
211}
212
Ian Rogersd81871c2011-10-03 13:57:23 -0700213size_t Instruction::SizeInCodeUnits() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700214 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800215 if (*insns == Instruction::kPackedSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700216 return (4 + insns[1] * 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800217 } else if (*insns == Instruction::kSparseSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700218 return (2 + insns[1] * 4);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700219 } else if (*insns == kArrayDataSignature) {
220 uint16_t element_size = insns[1];
221 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
222 // The plus 1 is to round up for odd size and width.
jeffhaoba5ebb92011-08-25 17:24:37 -0700223 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700224 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800225 switch (FormatOf(Opcode())) {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700226 case k10x:
227 case k12x:
228 case k11n:
229 case k11x:
230 case k10t:
jeffhaoba5ebb92011-08-25 17:24:37 -0700231 return 1;
Ian Rogers9fdfc182011-10-26 23:12:52 -0700232 case k20bc:
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700233 case k20t:
234 case k22x:
235 case k21t:
236 case k21s:
237 case k21h:
238 case k21c:
239 case k23x:
240 case k22b:
241 case k22t:
242 case k22s:
243 case k22c:
jeffhaoba5ebb92011-08-25 17:24:37 -0700244 return 2;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700245 case k32x:
246 case k30t:
247 case k31t:
248 case k31i:
249 case k31c:
250 case k35c:
251 case k3rc:
jeffhaoba5ebb92011-08-25 17:24:37 -0700252 return 3;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700253 case k51l:
jeffhaoba5ebb92011-08-25 17:24:37 -0700254 return 5;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700255 default:
256 LOG(FATAL) << "Unreachable";
257 }
258 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700259 return 0;
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700260}
261
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700262Instruction::Code Instruction::Opcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700263 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700264 int opcode = *insns & 0xFF;
265 return static_cast<Code>(opcode);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700266}
267
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700268const Instruction* Instruction::Next() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700269 size_t current_size_in_bytes = SizeInCodeUnits() * sizeof(uint16_t);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700270 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
Ian Rogersd81871c2011-10-03 13:57:23 -0700271 return reinterpret_cast<const Instruction*>(ptr + current_size_in_bytes);
272}
273
Ian Rogers2c8a8572011-10-24 17:11:36 -0700274std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700275 size_t inst_length = SizeInCodeUnits();
276 if (inst_length > code_units) {
277 inst_length = code_units;
278 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700279 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700280 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
281 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700282 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700283 }
284 for (size_t i = inst_length; i < code_units; i++) {
285 os << " ";
286 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700287 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700288}
289
Ian Rogers2c8a8572011-10-24 17:11:36 -0700290std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700291 DecodedInstruction insn(this);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700292 std::ostringstream os;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800293 const char* opcode = kInstructionNames[insn.opcode];
294 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800295 case k10x: os << opcode; break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800296 case k12x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
297 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
298 case k11x: os << StringPrintf("%s v%d", opcode, insn.vA); break;
299 case k10t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
300 case k20bc: os << StringPrintf("%s %d, kind@%d", opcode, insn.vA, insn.vB); break;
301 case k20t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
302 case k22x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
303 case k21t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
304 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
305 case k21h: os << StringPrintf("%s v%d, #%+d00000[00000000]", opcode, insn.vA, insn.vB); break;
306 case k21c: os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB); break;
307 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, insn.vA, insn.vB, insn.vC); break;
308 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
309 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, insn.vA, insn.vB, insn.vC); break;
310 case k22s: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
311 case k22c: os << StringPrintf("%s v%d, v%d, thing@%d", opcode, insn.vA, insn.vB, insn.vC); break;
312 case k32x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
313 case k30t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
314 case k31t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
315 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
316 case k31c: os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700317 case k35c: {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800318 switch (insn.opcode) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 case INVOKE_VIRTUAL:
320 case INVOKE_SUPER:
321 case INVOKE_DIRECT:
322 case INVOKE_STATIC:
323 case INVOKE_INTERFACE:
324 if (file != NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800325 const DexFile::MethodId& meth_id = file->GetMethodId(insn.vB);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800326 os << opcode << " {";
Elliott Hughesadb8c672012-03-06 16:49:32 -0800327 for (size_t i = 0; i < insn.vA; ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800328 if (i != 0) {
329 os << ", ";
330 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800331 os << "v" << insn.arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800332 }
333 os << "}, "
334 << file->GetMethodDeclaringClassDescriptor(meth_id) << "."
335 << file->GetMethodName(meth_id) << file->GetMethodSignature(meth_id)
Elliott Hughesadb8c672012-03-06 16:49:32 -0800336 << " // method@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700337 break;
338 } // else fall-through
339 default:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800340 os << opcode << " {v" << insn.arg[0] << ", v" << insn.arg[1] << ", v" << insn.arg[2]
341 << ", v" << insn.arg[3] << ", v" << insn.arg[4] << "}, thing@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700342 break;
343 }
344 break;
345 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800346 case k3rc: os << StringPrintf("%s, {v%d .. v%d}, method@%d", opcode, insn.vC, (insn.vC + insn.vA - 1), insn.vB); break;
347 case k51l: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700348 default: os << " unknown format (" << DumpHex(5) << ")"; break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700349 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700350 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700351}
352
Elliott Hughesadb8c672012-03-06 16:49:32 -0800353DecodedInstruction::DecodedInstruction(const Instruction* inst) {
354 inst->Decode(vA, vB, vB_wide, vC, arg);
355 opcode = inst->Opcode();
356}
357
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700358} // namespace art