blob: 39348ed5ee1662daa6c27d3f16a946f77aefe6b0 [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;
77 case k20t: // op +AAAA
78 vA = (int16_t) FETCH(1); // sign-extend 16-bit value
79 break;
80 case k21c: // op vAA, thing@BBBB
81 case k22x: // op vAA, vBBBB
82 vA = INST_AA(insn);
83 vB = FETCH(1);
84 break;
85 case k21s: // op vAA, #+BBBB
86 case k21t: // op vAA, +BBBB
87 vA = INST_AA(insn);
88 vB = (int16_t) FETCH(1); // sign-extend 16-bit value
89 break;
90 case k21h: // op vAA, #+BBBB0000[00000000]
91 vA = INST_AA(insn);
92 /*
93 * The value should be treated as right-zero-extended, but we don't
94 * actually do that here. Among other things, we don't know if it's
95 * the top bits of a 32- or 64-bit value.
96 */
97 vB = FETCH(1);
98 break;
99 case k23x: // op vAA, vBB, vCC
100 vA = INST_AA(insn);
101 vB = FETCH(1) & 0xff;
102 vC = FETCH(1) >> 8;
103 break;
104 case k22b: // op vAA, vBB, #+CC
105 vA = INST_AA(insn);
106 vB = FETCH(1) & 0xff;
107 vC = (int8_t) (FETCH(1) >> 8); // sign-extend 8-bit value
108 break;
109 case k22s: // op vA, vB, #+CCCC
110 case k22t: // op vA, vB, +CCCC
111 vA = INST_A(insn);
112 vB = INST_B(insn);
113 vC = (int16_t) FETCH(1); // sign-extend 16-bit value
114 break;
115 case k22c: // op vA, vB, thing@CCCC
116 vA = INST_A(insn);
117 vB = INST_B(insn);
118 vC = FETCH(1);
119 break;
120 case k30t: // op +AAAAAAAA
121 vA = FETCH_u4(1); // signed 32-bit value
122 break;
123 case k31t: // op vAA, +BBBBBBBB
124 case k31c: // op vAA, string@BBBBBBBB
125 vA = INST_AA(insn);
126 vB = FETCH_u4(1); // 32-bit value
127 break;
128 case k32x: // op vAAAA, vBBBB
129 vA = FETCH(1);
130 vB = FETCH(2);
131 break;
132 case k31i: // op vAA, #+BBBBBBBB
133 vA = INST_AA(insn);
134 vB = FETCH_u4(1); // signed 32-bit value
135 break;
136 case k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
137 {
138 /*
139 * Note that the fields mentioned in the spec don't appear in
140 * their "usual" positions here compared to most formats. This
141 * was done so that the field names for the argument count and
142 * reference index match between this format and the corresponding
143 * range formats (3rc and friends).
144 *
145 * Bottom line: The argument count is always in vA, and the
146 * method constant (or equivalent) is always in vB.
147 */
148 uint16_t regList;
149 int count;
150
151 vA = INST_B(insn); // This is labeled A in the spec.
152 vB = FETCH(1);
153 regList = FETCH(2);
154
155 count = vA;
156
157 /*
158 * Copy the argument registers into the arg[] array, and
159 * also copy the first argument (if any) into vC. (The
160 * DecodedInstruction structure doesn't have separate
161 * fields for {vD, vE, vF, vG}, so there's no need to make
162 * copies of those.) Note that cases 5..2 fall through.
163 */
164 switch (count) {
165 case 5: arg[4] = INST_A(insn);
166 case 4: arg[3] = (regList >> 12) & 0x0f;
167 case 3: arg[2] = (regList >> 8) & 0x0f;
168 case 2: arg[1] = (regList >> 4) & 0x0f;
169 case 1: vC = arg[0] = regList & 0x0f; break;
170 case 0: break; // Valid, but no need to do anything.
171 default:
172 LOG(ERROR) << "Invalid arg count in 35c (" << count << ")";
173 return;
174 }
175 }
176 break;
177 case k3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
178 vA = INST_AA(insn);
179 vB = FETCH(1);
180 vC = FETCH(2);
181 break;
182 case k51l: // op vAA, #+BBBBBBBBBBBBBBBB
183 vA = INST_AA(insn);
184 vB_wide = FETCH_u4(1) | ((uint64_t) FETCH_u4(3) << 32);
185 break;
186 default:
187 LOG(ERROR) << "Can't decode unexpected format " << (int) Format() << " (op=" << opcode << ")";
188 return;
189 }
190}
191
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700192size_t Instruction::Size() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700193 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700194 if (*insns == kPackedSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700195 return (4 + insns[1] * 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700196 } else if (*insns == kSparseSwitchSignature) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700197 return (2 + insns[1] * 4);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700198 } else if (*insns == kArrayDataSignature) {
199 uint16_t element_size = insns[1];
200 uint32_t length = insns[2] | (((uint32_t)insns[3]) << 16);
201 // The plus 1 is to round up for odd size and width.
jeffhaoba5ebb92011-08-25 17:24:37 -0700202 return (4 + (element_size * length + 1) / 2);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700203 } else {
204 switch (Format()) {
205 case k10x:
206 case k12x:
207 case k11n:
208 case k11x:
209 case k10t:
jeffhaoba5ebb92011-08-25 17:24:37 -0700210 return 1;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700211 case k20t:
212 case k22x:
213 case k21t:
214 case k21s:
215 case k21h:
216 case k21c:
217 case k23x:
218 case k22b:
219 case k22t:
220 case k22s:
221 case k22c:
jeffhaoba5ebb92011-08-25 17:24:37 -0700222 return 2;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700223 case k32x:
224 case k30t:
225 case k31t:
226 case k31i:
227 case k31c:
228 case k35c:
229 case k3rc:
jeffhaoba5ebb92011-08-25 17:24:37 -0700230 return 3;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700231 case k51l:
jeffhaoba5ebb92011-08-25 17:24:37 -0700232 return 5;
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700233 default:
234 LOG(FATAL) << "Unreachable";
235 }
236 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700237 return 0;
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700238}
239
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700240Instruction::Code Instruction::Opcode() const {
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700241 const uint16_t* insns = reinterpret_cast<const uint16_t*>(this);
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700242 int opcode = *insns & 0xFF;
243 return static_cast<Code>(opcode);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700244}
245
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700246const Instruction* Instruction::Next() const {
jeffhaoba5ebb92011-08-25 17:24:37 -0700247 size_t current_size = Size() * sizeof(uint16_t);
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700248 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
249 return reinterpret_cast<const Instruction*>(ptr + current_size);
250}
251
252} // namespace art