blob: 4db88462d7a4c9dd8570a6826501209b7d9682d6 [file] [log] [blame]
Carl Shapiro12eb78e2011-06-24 14:51:06 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_instruction.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -07004
5namespace art {
6
Carl Shapiroe4c1ce42011-07-09 02:31:57 -07007const char* const Instruction::kInstructionNames[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -07008#define INSTRUCTION_NAME(o, c, pname, f, r, i, a, v) pname,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070010 DEX_INSTRUCTION_LIST(INSTRUCTION_NAME)
11#undef DEX_INSTRUCTION_LIST
12#undef INSTRUCTION_NAME
13};
14
15Instruction::InstructionFormat const Instruction::kInstructionFormats[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070016#define INSTRUCTION_FORMAT(o, c, p, format, r, i, a, v) format,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070018 DEX_INSTRUCTION_LIST(INSTRUCTION_FORMAT)
19#undef DEX_INSTRUCTION_LIST
20#undef INSTRUCTION_FORMAT
21};
22
23int const Instruction::kInstructionFlags[] = {
jeffhaoba5ebb92011-08-25 17:24:37 -070024#define INSTRUCTION_FLAGS(o, c, p, f, r, i, flags, v) flags,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "dex_instruction_list.h"
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070026 DEX_INSTRUCTION_LIST(INSTRUCTION_FLAGS)
27#undef DEX_INSTRUCTION_LIST
28#undef INSTRUCTION_FLAGS
29};
30
jeffhaoba5ebb92011-08-25 17:24:37 -070031int const Instruction::kInstructionVerifyFlags[] = {
32#define INSTRUCTION_VERIFY_FLAGS(o, c, p, f, r, i, a, vflags) vflags,
33#include "dex_instruction_list.h"
34 DEX_INSTRUCTION_LIST(INSTRUCTION_VERIFY_FLAGS)
35#undef DEX_INSTRUCTION_LIST
36#undef INSTRUCTION_VERIFY_FLAGS
37};
38
39/*
40 * Handy macros for helping decode instructions.
41 */
42#define FETCH(_offset) (insns[(_offset)])
43#define FETCH_u4(_offset) (fetch_u4_impl((_offset), insns))
44#define INST_A(_insn) (((uint16_t)(_insn) >> 8) & 0x0f)
45#define INST_B(_insn) ((uint16_t)(_insn) >> 12)
46#define INST_AA(_insn) ((_insn) >> 8)
47
48/* Helper for FETCH_u4, above. */
49static inline uint32_t fetch_u4_impl(uint32_t offset, const uint16_t* insns) {
50 return insns[offset] | ((uint32_t) insns[offset+1] << 16);
51}
52
53void Instruction::Decode(uint32_t &vA, uint32_t &vB, uint64_t &vB_wide, uint32_t &vC, uint32_t arg[]) const {
54 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
55 uint16_t insn = *insns;
56 int opcode = insn & 0xFF;
57
58 switch (Format()) {
59 case k10x: // op
60 /* nothing to do; copy the AA bits out for the verifier */
61 vA = INST_AA(insn);
62 break;
63 case k12x: // op vA, vB
64 vA = INST_A(insn);
65 vB = INST_B(insn);
66 break;
67 case k11n: // op vA, #+B
68 vA = INST_A(insn);
69 vB = (int32_t) (INST_B(insn) << 28) >> 28; // sign extend 4-bit value
70 break;
71 case k11x: // op vAA
72 vA = INST_AA(insn);
73 break;
74 case k10t: // op +AA
75 vA = (int8_t) INST_AA(insn); // sign-extend 8-bit value
76 break;
jeffhaoe0cfb6f2011-09-22 16:42:56 -070077 case k20bc: // op AA, kind@BBBB
78 break;
jeffhaoba5ebb92011-08-25 17:24:37 -070079 case k20t: // op +AAAA
80 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
81 break;
82 case k21c: // op vAA, thing@BBBB
83 case k22x: // op vAA, vBBBB
84 vA = INST_AA(insn);
85 vB = FETCH(1);
86 break;
87 case k21s: // op vAA, #+BBBB
88 case k21t: // op vAA, +BBBB
89 vA = INST_AA(insn);
90 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
91 break;
92 case k21h: // op vAA, #+BBBB0000[00000000]
93 vA = INST_AA(insn);
94 /*
95 * The value should be treated as right-zero-extended, but we don't
96 * actually do that here. Among other things, we don't know if it's
97 * the top bits of a 32- or 64-bit value.
98 */
99 vB = FETCH(1);
100 break;
101 case k23x: // op vAA, vBB, vCC
102 vA = INST_AA(insn);
103 vB = FETCH(1) & 0xff;
104 vC = FETCH(1) >> 8;
105 break;
106 case k22b: // op vAA, vBB, #+CC
107 vA = INST_AA(insn);
108 vB = FETCH(1) & 0xff;
109 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
110 break;
111 case k22s: // op vA, vB, #+CCCC
112 case k22t: // op vA, vB, +CCCC
113 vA = INST_A(insn);
114 vB = INST_B(insn);
115 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
116 break;
117 case k22c: // op vA, vB, thing@CCCC
118 vA = INST_A(insn);
119 vB = INST_B(insn);
120 vC = FETCH(1);
121 break;
122 case k30t: // op +AAAAAAAA
123 vA = FETCH_u4(1); // signed 32-bit value
124 break;
125 case k31t: // op vAA, +BBBBBBBB
126 case k31c: // op vAA, string@BBBBBBBB
127 vA = INST_AA(insn);
128 vB = FETCH_u4(1); // 32-bit value
129 break;
130 case k32x: // op vAAAA, vBBBB
131 vA = FETCH(1);
132 vB = FETCH(2);
133 break;
134 case k31i: // op vAA, #+BBBBBBBB
135 vA = INST_AA(insn);
136 vB = FETCH_u4(1); // signed 32-bit value
137 break;
138 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
139 {
140 /*
141 * Note that the fields mentioned in the spec don't appear in
142 * their "usual" positions here compared to most formats. This
143 * was done so that the field names for the argument count and
144 * reference index match between this format and the corresponding
145 * range formats (3rc and friends).
146 *
147 * Bottom line: The argument count is always in vA, and the
148 * method constant (or equivalent) is always in vB.
149 */
150 uint16_t regList;
151 int count;
152
153 vA = INST_B(insn); // This is labeled A in the spec.
154 vB = FETCH(1);
155 regList = FETCH(2);
156
157 count = vA;
158
159 /*
160 * Copy the argument registers into the arg[] array, and
161 * also copy the first argument (if any) into vC. (The
162 * DecodedInstruction structure doesn't have separate
163 * fields for {vD, vE, vF, vG}, so there's no need to make
164 * copies of those.) Note that cases 5..2 fall through.
165 */
166 switch (count) {
167 case 5: arg[4] = INST_A(insn);
168 case 4: arg[3] = (regList >> 12) & 0x0f;
169 case 3: arg[2] = (regList >> 8) & 0x0f;
170 case 2: arg[1] = (regList >> 4) & 0x0f;
171 case 1: vC = arg[0] = regList & 0x0f; break;
172 case 0: break; // Valid, but no need to do anything.
173 default:
174 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
175 return;
176 }
177 }
178 break;
179 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
180 vA = INST_AA(insn);
181 vB = FETCH(1);
182 vC = FETCH(2);
183 break;
184 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
185 vA = INST_AA(insn);
186 vB_wide = FETCH_u4(1) | ((uint64_t) FETCH_u4(3) << 32);
187 break;
188 default:
189 LOG(ERROR) << "Can't decode unexpected format " << (int) Format() << " (op=" << opcode << ")";
190 return;
191 }
192}
193
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700194size_t Instruction::Size() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700195 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700196 if (*insns == kPackedSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700197 return (4 + insns[1] * 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700198 } else if (*insns == kSparseSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700199 return (2 + insns[1] * 4);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700200 } else if (*insns == kArrayDataSignature) {
201 uint16_t element_size = insns[1];
202 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
203 // The plus 1 is to round up for odd size and width.
jeffhaoba5ebb92011-08-25 17:24:37 -0700204 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700205 } else {
206 switch (Format()) {
207 case k10x:
208 case k12x:
209 case k11n:
210 case k11x:
211 case k10t:
jeffhaoba5ebb92011-08-25 17:24:37 -0700212 return 1;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700213 case k20t:
214 case k22x:
215 case k21t:
216 case k21s:
217 case k21h:
218 case k21c:
219 case k23x:
220 case k22b:
221 case k22t:
222 case k22s:
223 case k22c:
jeffhaoba5ebb92011-08-25 17:24:37 -0700224 return 2;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700225 case k32x:
226 case k30t:
227 case k31t:
228 case k31i:
229 case k31c:
230 case k35c:
231 case k3rc:
jeffhaoba5ebb92011-08-25 17:24:37 -0700232 return 3;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700233 case k51l:
jeffhaoba5ebb92011-08-25 17:24:37 -0700234 return 5;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700235 default:
236 LOG(FATAL) << "Unreachable";
237 }
238 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700239 return 0;
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700240}
241
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700242Instruction::Code Instruction::Opcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700243 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700244 int opcode = *insns & 0xFF;
245 return static_cast<Code>(opcode);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700246}
247
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700248const Instruction* Instruction::Next() const {
jeffhaoba5ebb92011-08-25 17:24:37 -0700249 size_t current_size = Size() * sizeof(uint16_t);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700250 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
251 return reinterpret_cast<const Instruction*>(ptr + current_size);
252}
253
254} // namespace art