blob: 407393838695b55c0b4e4aba3a8746d09606a1f3 [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
95 break;
jeffhaoba5ebb92011-08-25 17:24:37 -070096 case k20t: // op +AAAA
97 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
98 break;
99 case k21c: // op vAA, thing@BBBB
100 case k22x: // op vAA, vBBBB
101 vA = INST_AA(insn);
102 vB = FETCH(1);
103 break;
104 case k21s: // op vAA, #+BBBB
105 case k21t: // op vAA, +BBBB
106 vA = INST_AA(insn);
107 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
108 break;
109 case k21h: // op vAA, #+BBBB0000[00000000]
110 vA = INST_AA(insn);
111 /*
112 * The value should be treated as right-zero-extended, but we don't
113 * actually do that here. Among other things, we don't know if it's
114 * the top bits of a 32- or 64-bit value.
115 */
116 vB = FETCH(1);
117 break;
118 case k23x: // op vAA, vBB, vCC
119 vA = INST_AA(insn);
120 vB = FETCH(1) & 0xff;
121 vC = FETCH(1) >> 8;
122 break;
123 case k22b: // op vAA, vBB, #+CC
124 vA = INST_AA(insn);
125 vB = FETCH(1) & 0xff;
126 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
127 break;
128 case k22s: // op vA, vB, #+CCCC
129 case k22t: // op vA, vB, +CCCC
130 vA = INST_A(insn);
131 vB = INST_B(insn);
132 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
133 break;
134 case k22c: // op vA, vB, thing@CCCC
135 vA = INST_A(insn);
136 vB = INST_B(insn);
137 vC = FETCH(1);
138 break;
139 case k30t: // op +AAAAAAAA
140 vA = FETCH_u4(1); // signed 32-bit value
141 break;
142 case k31t: // op vAA, +BBBBBBBB
143 case k31c: // op vAA, string@BBBBBBBB
144 vA = INST_AA(insn);
145 vB = FETCH_u4(1); // 32-bit value
146 break;
147 case k32x: // op vAAAA, vBBBB
148 vA = FETCH(1);
149 vB = FETCH(2);
150 break;
151 case k31i: // op vAA, #+BBBBBBBB
152 vA = INST_AA(insn);
153 vB = FETCH_u4(1); // signed 32-bit value
154 break;
155 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
156 {
157 /*
158 * Note that the fields mentioned in the spec don't appear in
159 * their "usual" positions here compared to most formats. This
160 * was done so that the field names for the argument count and
161 * reference index match between this format and the corresponding
162 * range formats (3rc and friends).
163 *
164 * Bottom line: The argument count is always in vA, and the
165 * method constant (or equivalent) is always in vB.
166 */
167 uint16_t regList;
168 int count;
169
170 vA = INST_B(insn); // This is labeled A in the spec.
171 vB = FETCH(1);
172 regList = FETCH(2);
173
174 count = vA;
175
176 /*
177 * Copy the argument registers into the arg[] array, and
178 * also copy the first argument (if any) into vC. (The
179 * DecodedInstruction structure doesn't have separate
180 * fields for {vD, vE, vF, vG}, so there's no need to make
181 * copies of those.) Note that cases 5..2 fall through.
182 */
183 switch (count) {
184 case 5: arg[4] = INST_A(insn);
185 case 4: arg[3] = (regList >> 12) & 0x0f;
186 case 3: arg[2] = (regList >> 8) & 0x0f;
187 case 2: arg[1] = (regList >> 4) & 0x0f;
188 case 1: vC = arg[0] = regList & 0x0f; break;
189 case 0: break; // Valid, but no need to do anything.
190 default:
191 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
192 return;
193 }
194 }
195 break;
196 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
197 vA = INST_AA(insn);
198 vB = FETCH(1);
199 vC = FETCH(2);
200 break;
201 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
202 vA = INST_AA(insn);
203 vB_wide = FETCH_u4(1) | ((uint64_t) FETCH_u4(3) << 32);
204 break;
205 default:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800206 LOG(ERROR) << "Can't decode unexpected format " << static_cast<int>(FormatOf(Opcode())) << " (op=" << opcode << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700207 return;
208 }
209}
210
Ian Rogersd81871c2011-10-03 13:57:23 -0700211size_t Instruction::SizeInCodeUnits() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700212 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800213 if (*insns == Instruction::kPackedSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700214 return (4 + insns[1] * 2);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800215 } else if (*insns == Instruction::kSparseSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700216 return (2 + insns[1] * 4);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700217 } else if (*insns == kArrayDataSignature) {
218 uint16_t element_size = insns[1];
219 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
220 // The plus 1 is to round up for odd size and width.
jeffhaoba5ebb92011-08-25 17:24:37 -0700221 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700222 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800223 switch (FormatOf(Opcode())) {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700224 case k10x:
225 case k12x:
226 case k11n:
227 case k11x:
228 case k10t:
jeffhaoba5ebb92011-08-25 17:24:37 -0700229 return 1;
Ian Rogers9fdfc182011-10-26 23:12:52 -0700230 case k20bc:
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700231 case k20t:
232 case k22x:
233 case k21t:
234 case k21s:
235 case k21h:
236 case k21c:
237 case k23x:
238 case k22b:
239 case k22t:
240 case k22s:
241 case k22c:
jeffhaoba5ebb92011-08-25 17:24:37 -0700242 return 2;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700243 case k32x:
244 case k30t:
245 case k31t:
246 case k31i:
247 case k31c:
248 case k35c:
249 case k3rc:
jeffhaoba5ebb92011-08-25 17:24:37 -0700250 return 3;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700251 case k51l:
jeffhaoba5ebb92011-08-25 17:24:37 -0700252 return 5;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700253 default:
254 LOG(FATAL) << "Unreachable";
255 }
256 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700257 return 0;
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700258}
259
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700260Instruction::Code Instruction::Opcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700261 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700262 int opcode = *insns & 0xFF;
263 return static_cast<Code>(opcode);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700264}
265
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700266const Instruction* Instruction::Next() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700267 size_t current_size_in_bytes = SizeInCodeUnits() * sizeof(uint16_t);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700268 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
Ian Rogersd81871c2011-10-03 13:57:23 -0700269 return reinterpret_cast<const Instruction*>(ptr + current_size_in_bytes);
270}
271
Ian Rogers2c8a8572011-10-24 17:11:36 -0700272std::string Instruction::DumpHex(size_t code_units) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700273 size_t inst_length = SizeInCodeUnits();
274 if (inst_length > code_units) {
275 inst_length = code_units;
276 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700277 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -0700278 const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
279 for (size_t i = 0; i < inst_length; i++) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700280 os << StringPrintf("0x%04x", insn[i]) << " ";
Ian Rogersd81871c2011-10-03 13:57:23 -0700281 }
282 for (size_t i = inst_length; i < code_units; i++) {
283 os << " ";
284 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700285 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -0700286}
287
Ian Rogers2c8a8572011-10-24 17:11:36 -0700288std::string Instruction::DumpString(const DexFile* file) const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700289 DecodedInstruction insn(this);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700290 std::ostringstream os;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800291 const char* opcode = kInstructionNames[insn.opcode];
292 switch (FormatOf(Opcode())) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800293 case k10x: os << opcode; break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800294 case k12x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
295 case k11n: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
296 case k11x: os << StringPrintf("%s v%d", opcode, insn.vA); break;
297 case k10t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
298 case k20bc: os << StringPrintf("%s %d, kind@%d", opcode, insn.vA, insn.vB); break;
299 case k20t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
300 case k22x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
301 case k21t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
302 case k21s: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
303 case k21h: os << StringPrintf("%s v%d, #%+d00000[00000000]", opcode, insn.vA, insn.vB); break;
304 case k21c: os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB); break;
305 case k23x: os << StringPrintf("%s v%d, v%d, v%d", opcode, insn.vA, insn.vB, insn.vC); break;
306 case k22b: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
307 case k22t: os << StringPrintf("%s v%d, v%d, %+d", opcode, insn.vA, insn.vB, insn.vC); break;
308 case k22s: os << StringPrintf("%s v%d, v%d, #%+d", opcode, insn.vA, insn.vB, insn.vC); break;
309 case k22c: os << StringPrintf("%s v%d, v%d, thing@%d", opcode, insn.vA, insn.vB, insn.vC); break;
310 case k32x: os << StringPrintf("%s v%d, v%d", opcode, insn.vA, insn.vB); break;
311 case k30t: os << StringPrintf("%s %+d", opcode, insn.vA); break;
312 case k31t: os << StringPrintf("%s v%d, %+d", opcode, insn.vA, insn.vB); break;
313 case k31i: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
314 case k31c: os << StringPrintf("%s v%d, thing@%d", opcode, insn.vA, insn.vB); break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700315 case k35c: {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800316 switch (insn.opcode) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700317 case INVOKE_VIRTUAL:
318 case INVOKE_SUPER:
319 case INVOKE_DIRECT:
320 case INVOKE_STATIC:
321 case INVOKE_INTERFACE:
322 if (file != NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800323 const DexFile::MethodId& meth_id = file->GetMethodId(insn.vB);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800324 os << opcode << " {";
Elliott Hughesadb8c672012-03-06 16:49:32 -0800325 for (size_t i = 0; i < insn.vA; ++i) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800326 if (i != 0) {
327 os << ", ";
328 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800329 os << "v" << insn.arg[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800330 }
331 os << "}, "
332 << file->GetMethodDeclaringClassDescriptor(meth_id) << "."
333 << file->GetMethodName(meth_id) << file->GetMethodSignature(meth_id)
Elliott Hughesadb8c672012-03-06 16:49:32 -0800334 << " // method@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700335 break;
336 } // else fall-through
337 default:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800338 os << opcode << " {v" << insn.arg[0] << ", v" << insn.arg[1] << ", v" << insn.arg[2]
339 << ", v" << insn.arg[3] << ", v" << insn.arg[4] << "}, thing@" << insn.vB;
Ian Rogersd81871c2011-10-03 13:57:23 -0700340 break;
341 }
342 break;
343 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800344 case k3rc: os << StringPrintf("%s, {v%d .. v%d}, method@%d", opcode, insn.vC, (insn.vC + insn.vA - 1), insn.vB); break;
345 case k51l: os << StringPrintf("%s v%d, #%+d", opcode, insn.vA, insn.vB); break;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700346 default: os << " unknown format (" << DumpHex(5) << ")"; break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700347 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700348 return os.str();
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700349}
350
Elliott Hughesadb8c672012-03-06 16:49:32 -0800351DecodedInstruction::DecodedInstruction(const Instruction* inst) {
352 inst->Decode(vA, vB, vB_wide, vC, arg);
353 opcode = inst->Opcode();
354}
355
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700356} // namespace art