blob: dfaacf2e2a8398a964ffb6bcc7b22ecef1826584 [file] [log] [blame]
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001/*
2 * Copyright (C) 2012 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 */
16
17#include "disassembler_arm.h"
18
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080019#include <iostream>
20
Elliott Hughes0f3c5532012-03-30 14:51:51 -070021#include "logging.h"
22#include "stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070023#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080025namespace art {
26namespace arm {
27
28DisassemblerArm::DisassemblerArm() {
29}
30
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080031void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
32 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
33 for (const uint8_t* cur = begin; cur < end; cur += 4) {
34 DumpArm(os, cur);
35 }
36 } else {
37 // remove thumb specifier bits
38 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
39 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
40 for (const uint8_t* cur = begin; cur < end;) {
41 cur += DumpThumb16(os, cur);
42 }
43 }
44}
45
Elliott Hughes77405792012-03-15 15:22:12 -070046static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070047 "eq", // 0000 - equal
48 "ne", // 0001 - not-equal
49 "cs", // 0010 - carry-set, greater than, equal or unordered
50 "cc", // 0011 - carry-clear, less than
51 "mi", // 0100 - minus, negative
52 "pl", // 0101 - plus, positive or zero
53 "vs", // 0110 - overflow
54 "vc", // 0111 - no overflow
55 "hi", // 1000 - unsigned higher
56 "ls", // 1001 - unsigned lower or same
57 "ge", // 1010 - signed greater than or equal
58 "lt", // 1011 - signed less than
59 "gt", // 1100 - signed greater than
60 "le", // 1101 - signed less than or equal
61 "", // 1110 - always
62 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080063};
64
65void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
66 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070067 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080068 } else {
69 os << "Unexpected condition: " << cond;
70 }
71}
72
Ian Rogers40627db2012-03-04 17:31:09 -080073void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Elliott Hughes1ca98492012-04-12 17:21:02 -070074 os << StringPrintf("%+d (%p)", imm32, instr_ptr + imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080075}
76
77static uint32_t ReadU16(const uint8_t* ptr) {
78 return ptr[0] | (ptr[1] << 8);
79}
80
81static uint32_t ReadU32(const uint8_t* ptr) {
82 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
83}
84
Elliott Hughes77405792012-03-15 15:22:12 -070085static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070086 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
87 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -070088};
89
Ian Rogersad03ef52012-03-18 19:34:47 -070090static const char* kThumbDataProcessingOperations[] = {
91 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
92 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
93};
94
Elliott Hughes77405792012-03-15 15:22:12 -070095struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -070096 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -070097 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -070098 uint32_t r;
99};
100std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
101 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700102 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700103 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700104 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700105 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700106 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700107 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700108 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700109 }
110 return os;
111}
112
Elliott Hughes630e77d2012-03-22 19:20:56 -0700113struct ThumbRegister : ArmRegister {
114 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700115};
116
117struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700118 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700119 uint32_t shift;
120 ArmRegister rm;
121};
122std::ostream& operator<<(std::ostream& os, const Rm& r) {
123 os << r.rm;
124 if (r.shift != 0) {
125 os << "-shift-" << r.shift; // TODO
126 }
127 return os;
128}
129
Elliott Hughes1ca98492012-04-12 17:21:02 -0700130struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700131 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700132 uint32_t rotate = ((instruction >> 8) & 0xf);
133 uint32_t imm = (instruction & 0xff);
134 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
135 }
136 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700137};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700138std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700139 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700140 return os;
141}
142
143struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700144 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700145 uint32_t register_list;
146};
147std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
148 if (rhs.register_list == 0) {
149 os << "<no register list?>";
150 return os;
151 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700152 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700153 bool first = true;
154 for (size_t i = 0; i < 16; i++) {
155 if ((rhs.register_list & (1 << i)) != 0) {
156 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700157 first = false;
158 } else {
159 os << ", ";
160 }
161 os << ArmRegister(i);
162 }
163 }
164 os << "}";
165 return os;
166}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800167
168void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700169 uint32_t instruction = ReadU32(instr_ptr);
170 uint32_t cond = (instruction >> 28) & 0xf;
171 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700172 std::string opcode;
173 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700174 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700175 switch (op1) {
176 case 0:
177 case 1: // Data processing instructions.
178 {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700179 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
180 opcode = "bkpt";
181 uint32_t imm12 = (instruction >> 8) & 0xfff;
182 uint32_t imm4 = (instruction & 0xf);
183 args << '#' << ((imm12 << 4) | imm4);
184 break;
185 }
Elliott Hughes77405792012-03-15 15:22:12 -0700186 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700187 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700188 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700189 break;
190 }
191 bool i = (instruction & (1 << 25)) != 0;
192 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700193 uint32_t op = (instruction >> 21) & 0xf;
194 opcode = kDataProcessingOperations[op];
195 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
196 if (implicit_s) {
197 // Rd is unused (and not shown), and we don't show the 's' suffix either.
198 } else {
199 if (s) {
200 suffixes += 's';
201 }
202 args << ArmRegister(instruction, 12) << ", ";
203 }
Elliott Hughes77405792012-03-15 15:22:12 -0700204 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700205 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700206 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700207 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700208 }
209 }
210 break;
211 case 2: // Load/store word and unsigned byte.
212 {
213 bool p = (instruction & (1 << 24)) != 0;
214 bool b = (instruction & (1 << 22)) != 0;
215 bool w = (instruction & (1 << 21)) != 0;
216 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700217 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700218 args << ArmRegister(instruction, 12) << ", ";
219 ArmRegister rn(instruction, 16);
220 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700221 UNIMPLEMENTED(FATAL) << "literals";
222 } else {
223 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700224 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700225 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700226 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700227 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700228 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700229 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700230 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700231 } else {
232 LOG(FATAL) << p << " " << w;
233 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700234 if (rn.r == 9) {
235 args << " ; ";
Elliott Hughes1ca98492012-04-12 17:21:02 -0700236 Thread::DumpThreadOffset(args, offset, 4);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700237 }
Elliott Hughes77405792012-03-15 15:22:12 -0700238 }
239 }
240 break;
241 case 4: // Load/store multiple.
242 {
243 bool p = (instruction & (1 << 24)) != 0;
244 bool u = (instruction & (1 << 23)) != 0;
245 bool w = (instruction & (1 << 21)) != 0;
246 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700247 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700248 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700249 }
250 break;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700251 case 5: // Branch/branch with link.
252 {
253 bool bl = (instruction & (1 << 24)) != 0;
254 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700255 int32_t imm26 = (instruction & 0xffffff) << 2;
256 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700257 DumpBranchTarget(args, instr_ptr + 8, imm32);
258 }
259 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700260 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700261 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700262 break;
263 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700264 opcode += kConditionCodeNames[cond];
265 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700266 // TODO: a more complete ARM disassembler could generate wider opcodes.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700267 os << StringPrintf("\t\t\t%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800268}
269
270size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
271 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
272 // |111|1 1|1000000|0000|1111110000000000|
273 // |5 3|2 1|0987654|3 0|5 0 5 0|
274 // |---|---|-------|----|----------------|
275 // |332|2 2|2222222|1111|1111110000000000|
276 // |1 9|8 7|6543210|9 6|5 0 5 0|
277 // |---|---|-------|----|----------------|
278 // |111|op1| op2 | | |
279 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700280 if (op1 == 0) {
281 return DumpThumb16(os, instr_ptr);
282 }
283
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800284 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700285 std::ostringstream opcode;
286 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800287 switch (op1) {
288 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800289 break;
290 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700291 if ((op2 & 0x64) == 0) { // 00x x0xx
292 // |111|11|10|00|0|00|0000|1111110000000000|
293 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
294 // |---|--|--|--|-|--|----|----------------|
295 // |332|22|22|22|2|22|1111|1111110000000000|
296 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
297 // |---|--|--|--|-|--|----|----------------|
298 // |111|01|00|op|0|WL| Rn | |
299 // |111|01| op2 | | |
300 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
301 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
302 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
303 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
304 uint32_t op = (instr >> 23) & 3;
305 uint32_t W = (instr >> 21) & 1;
306 uint32_t L = (instr >> 20) & 1;
307 ArmRegister Rn(instr, 16);
308 if (op == 1 || op == 2) {
309 if (op == 1) {
310 if (L == 0) {
311 opcode << "stm";
312 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800313 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700314 if (Rn.r != 13) {
315 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700316 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700317 } else {
318 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800319 }
320 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700321 } else {
322 if (L == 0) {
323 if (Rn.r != 13) {
324 opcode << "stmdb";
325 args << Rn << (W == 0 ? "" : "!") << ", ";
326 } else {
327 opcode << "push";
328 }
329 } else {
330 opcode << "ldmdb";
331 args << Rn << (W == 0 ? "" : "!") << ", ";
332 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800333 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700334 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800335 }
Ian Rogers9af89402012-09-07 11:29:35 -0700336 } else if ((op2 & 0x64) == 4) { // 00x x1xx
337 uint32_t op3 = (instr >> 23) & 3;
338 uint32_t op4 = (instr >> 20) & 3;
339 //uint32_t op5 = (instr >> 4) & 0xF;
340 ArmRegister Rn(instr, 16);
341 ArmRegister Rt(instr, 12);
342 uint32_t imm8 = instr & 0xFF;
343 if (op3 == 0 && op4 == 0) { // STREX
344 ArmRegister Rd(instr, 8);
345 opcode << "strex";
346 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
347 } else if (op3 == 0 && op4 == 1) { // LDREX
348 opcode << "ldrex";
349 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
350 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700351 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
352 // Data-processing (shifted register)
353 // |111|1110|0000|0|0000|1111|1100|0000|0000|
354 // |5 3|2109|8765|4|3 0|5 |10 8|7 5 |3 0|
355 // |---|----|----|-|----|----|----|----|----|
356 // |332|2222|2222|2|1111|1111|1100|0000|0000|
357 // |1 9|8765|4321|0|9 6|5 |10 8|7 5 |3 0|
358 // |---|----|----|-|----|----|----|----|----|
359 // |111|0101| op3|S| Rn | | Rd | | Rm |
360 uint32_t op3 = (instr >> 21) & 0xF;
361 uint32_t S = (instr >> 20) & 1;
362 uint32_t Rn = (instr >> 16) & 0xF;
363 ArmRegister Rd(instr, 8);
364 ArmRegister Rm(instr, 0);
365 switch (op3) {
366 case 0x0:
367 if (Rn != 0xF) {
368 opcode << "and";
369 } else {
370 opcode << "tst";
371 S = 0; // don't print 's'
372 }
373 break;
374 case 0x1: opcode << "bic"; break;
375 case 0x2:
376 if (Rn != 0xF) {
377 opcode << "orr";
378 } else {
379 opcode << "mov";
380 }
381 break;
382 case 0x3:
383 if (Rn != 0xF) {
384 opcode << "orn";
385 } else {
386 opcode << "mvn";
387 }
388 break;
389 case 0x4:
390 if (Rn != 0xF) {
391 opcode << "eor";
392 } else {
393 opcode << "teq";
394 S = 0; // don't print 's'
395 }
396 break;
397 case 0x6: opcode << "pkh"; break;
398 case 0x8:
399 if (Rn != 0xF) {
400 opcode << "add";
401 } else {
402 opcode << "cmn";
403 S = 0; // don't print 's'
404 }
405 break;
406 case 0xA: opcode << "adc"; break;
407 case 0xB: opcode << "sbc"; break;
408 }
Ian Rogers087b2412012-03-21 01:30:32 -0700409
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700410 if (S == 1) {
411 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700412 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700413 opcode << ".w";
414 args << Rd << ", " << Rm;
415 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
416 // Co-processor instructions
417 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
418 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
419 // |---|-|--|------|----|----|----|---|---|----|
420 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
421 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
422 // |---|-|--|------|----|----|----|---|---|----|
423 // |111| |11| op3 | Rn | |copr| |op4| |
424 uint32_t op3 = (instr >> 20) & 0x3F;
425 uint32_t coproc = (instr >> 8) & 0xF;
426 uint32_t op4 = (instr >> 4) & 0x1;
Ian Rogers9af89402012-09-07 11:29:35 -0700427 if ((op3 == 2 || op3 == 2 || op3 == 6 || op3 == 7) || // 00x1x
428 (op3 >= 8 && op3 <= 15) || (op3 >= 16 && op3 <= 31)) { // 001xxx, 01xxxx
429 // Extension register load/store instructions
430 // |111|1|110|00000|0000|1111|110|000000000|
431 // |5 3|2|109|87654|3 0|54 2|10 |87 54 0|
432 // |---|-|---|-----|----|----|---|---------|
433 // |332|2|222|22222|1111|1111|110|000000000|
434 // |1 9|8|765|43210|9 6|54 2|10 |87 54 0|
435 // |---|-|---|-----|----|----|---|---------|
436 // |111|T|110| op3 | Rn | |101| |
437 // 111 0 110 01001 0011 0000 101 000000011 - ec930a03
438 if (op3 == 9 || op3 == 0xD) { // VLDM
439 // 1110 110 PUDW1 nnnn dddd 101S iiii iiii
440 uint32_t P = (instr >> 24) & 1;
441 uint32_t U = (instr >> 23) & 1;
442 uint32_t D = (instr >> 22) & 1;
443 uint32_t W = (instr >> 21) & 1;
444 uint32_t S = (instr >> 8) & 1;
445 ArmRegister Rn(instr, 16);
446 uint32_t Vd = (instr >> 12) & 0xF;
447 uint32_t imm8 = instr & 0xFF;
448 uint32_t d = (S == 0 ? ((Vd << 1) | D) : (Vd | (D << 4)));
449 if (P == 0 && U == 0 && W == 0) {
450 // TODO: 64bit transfers between ARM core and extension registers.
451 } else if (P == 0 && U == 1 && Rn.r == 13) { // VPOP
452 opcode << "vpop" << (S == 0 ? ".f64" : ".f32");
453 args << d << " .. " << (d + imm8);
454 } else if (P == 1 && W == 0) { // VLDR
455 opcode << "vldr" << (S == 0 ? ".f64" : ".f32");
456 args << d << ", [" << Rn << ", #" << imm8 << "]";
457 } else { // VLDM
458 opcode << "vldm" << (S == 0 ? ".f64" : ".f32");
459 args << Rn << ", " << d << " .. " << (d + imm8);
460 }
461 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700462 } else if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700463 if ((coproc & 0xE) == 0xA) {
464 // VFP data-processing instructions
465 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
466 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
467 // |---|-|----|----|----|----|---|-|----|-|-|----|
468 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
469 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
470 // |---|-|----|----|----|----|---|-|----|-|-|----|
471 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
472 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
473 uint32_t opc1 = (instr >> 20) & 0xF;
474 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700475 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700476 if ((opc1 & 0xB) == 0xB) { // 1x11
477 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700478 uint32_t D = (instr >> 22) & 0x1;
479 uint32_t Vd = (instr >> 12) & 0xF;
480 uint32_t sz = (instr >> 8) & 1;
481 uint32_t M = (instr >> 5) & 1;
482 uint32_t Vm = instr & 0xF;
483 bool dp_operation = sz == 1;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700484 switch (opc2) {
Ian Rogers0183dd72012-09-17 23:06:51 -0700485 case 0x1: // Vneg/Vsqrt
486 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
487 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (dp_operation ? ".f64" : ".f32");
488 if (dp_operation) {
489 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
490 } else {
491 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
492 }
493 break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700494 case 0x4: case 0x5: { // Vector compare
495 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Ian Rogers0183dd72012-09-17 23:06:51 -0700496 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (dp_operation ? ".f64" : ".f32");
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700497 if (dp_operation) {
498 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
499 } else {
500 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
501 }
502 break;
503 }
504 }
505 }
506 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700507 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
508 // Advanced SIMD
509 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
510 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
511 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
512 uint32_t D = (instr >> 22) & 1;
513 uint32_t Vd = (instr >> 12) & 0xF;
514 uint32_t sz = (instr >> 8) & 1;
515 uint32_t M = (instr >> 5) & 1;
516 uint32_t Vm = instr & 0xF;
517 bool dp_operation = sz == 1;
518 opcode << "vsqrt" << (dp_operation ? ".f64" : ".f32");
519 if (dp_operation) {
520 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
521 } else {
522 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
523 }
524 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700525 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800526 }
527 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800528 case 2:
529 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
530 // Data-processing (modified immediate)
531 // |111|11|10|0000|0|0000|1|111|1100|00000000|
532 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
533 // |---|--|--|----|-|----|-|---|----|--------|
534 // |332|22|22|2222|2|1111|1|111|1100|00000000|
535 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
536 // |---|--|--|----|-|----|-|---|----|--------|
537 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
538 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800539 uint32_t i = (instr >> 26) & 1;
540 uint32_t op3 = (instr >> 21) & 0xF;
541 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700542 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800543 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700544 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800545 uint32_t imm8 = instr & 0xFF;
546 int32_t imm32 = (i << 12) | (imm3 << 8) | imm8;
547 switch (op3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700548 case 0x0: opcode << "and"; break;
549 case 0x1: opcode << "bic"; break;
550 case 0x2: opcode << "orr"; break;
551 case 0x3: opcode << "orn"; break;
552 case 0x4: opcode << "eor"; break;
553 case 0x8: opcode << "add"; break;
554 case 0xA: opcode << "adc"; break;
555 case 0xB: opcode << "sbc"; break;
556 case 0xD: opcode << "sub"; break;
557 case 0xE: opcode << "rsb"; break;
558 default: opcode << "UNKNOWN DPMI-" << op3; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800559 }
560 if (S == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700561 opcode << "s";
Ian Rogers40627db2012-03-04 17:31:09 -0800562 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700563 args << Rd << ", " << Rn << ", ThumbExpand(" << imm32 << ")";
Ian Rogers40627db2012-03-04 17:31:09 -0800564 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
565 // Data-processing (plain binary immediate)
566 // |111|11|10|00000|0000|1|111110000000000|
567 // |5 3|21|09|87654|3 0|5|4 0 5 0|
568 // |---|--|--|-----|----|-|---------------|
569 // |332|22|22|22222|1111|1|111110000000000|
570 // |1 9|87|65|43210|9 6|5|4 0 5 0|
571 // |---|--|--|-----|----|-|---------------|
572 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
573 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800574 switch (op3) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700575 case 0x00: {
576 ArmRegister Rd(instr, 8);
577 ArmRegister Rn(instr, 16);
578 uint32_t i = (instr >> 26) & 1;
579 uint32_t imm3 = (instr >> 12) & 0x7;
580 uint32_t imm8 = instr & 0xFF;
581 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
582 if (Rn.r != 0xF) {
583 opcode << "addw";
584 args << Rd << ", " << Rn << ", #" << imm12;
585 } else {
586 opcode << "adr";
587 args << Rd << ", ";
588 DumpBranchTarget(args, instr_ptr + 4, imm12);
589 }
590 break;
591 }
Ian Rogers40627db2012-03-04 17:31:09 -0800592 case 0x04: {
593 // MOVW Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700594 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800595 uint32_t i = (instr >> 26) & 1;
596 uint32_t imm3 = (instr >> 12) & 0x7;
597 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700598 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800599 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700600 opcode << "movw";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700601 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800602 break;
603 }
604 case 0x0A: {
605 // SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700606 ArmRegister Rd(instr, 8);
607 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800608 uint32_t i = (instr >> 26) & 1;
609 uint32_t imm3 = (instr >> 12) & 0x7;
610 uint32_t imm8 = instr & 0xFF;
611 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700612 opcode << "sub.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700613 args << Rd << ", " << Rn << ", #" << imm12;
Ian Rogers40627db2012-03-04 17:31:09 -0800614 break;
615 }
616 default:
617 break;
618 }
619 } else {
620 // Branches and miscellaneous control
621 // |111|11|1000000|0000|1|111|1100|00000000|
622 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
623 // |---|--|-------|----|-|---|----|--------|
624 // |332|22|2222222|1111|1|111|1100|00000000|
625 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
626 // |---|--|-------|----|-|---|----|--------|
627 // |111|10| op2 | |1|op3|op4 | |
628
629 uint32_t op3 = (instr >> 12) & 7;
630 //uint32_t op4 = (instr >> 8) & 0xF;
631 switch (op3) {
632 case 0:
633 if ((op2 & 0x38) != 0x38) {
634 // Conditional branch
635 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
636 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
637 // |---|--|-|----|------|-|-|--|-|--|-----------|
638 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
639 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
640 // |---|--|-|----|------|-|-|--|-|--|-----------|
641 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
642 uint32_t S = (instr >> 26) & 1;
643 uint32_t J2 = (instr >> 11) & 1;
644 uint32_t J1 = (instr >> 13) & 1;
645 uint32_t imm6 = (instr >> 16) & 0x3F;
646 uint32_t imm11 = instr & 0x7FF;
647 uint32_t cond = (instr >> 22) & 0xF;
648 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
649 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700650 opcode << "b";
651 DumpCond(opcode, cond);
652 opcode << ".w";
653 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -0700654 } else if (op2 == 0x3B) {
655 // Miscellaneous control instructions
656 uint32_t op5 = (instr >> 4) & 0xF;
657 switch (op5) {
658 case 4: opcode << "dsb"; break;
659 case 5: opcode << "dmb"; break;
660 case 6: opcode << "isb"; break;
661 }
Ian Rogers40627db2012-03-04 17:31:09 -0800662 }
663 break;
664 case 2:
665 case 1: case 3:
666 break;
667 case 4: case 6: case 5: case 7: {
668 // BL, BLX (immediate)
669 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
670 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
671 // |---|--|-|----------|--|--|-|--|-----------|
672 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
673 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
674 // |---|--|-|----------|--|--|-|--|-----------|
675 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
676 uint32_t S = (instr >> 26) & 1;
677 uint32_t J2 = (instr >> 11) & 1;
678 uint32_t L = (instr >> 12) & 1;
679 uint32_t J1 = (instr >> 13) & 1;
680 uint32_t imm10 = (instr >> 16) & 0x3FF;
681 uint32_t imm11 = instr & 0x7FF;
682 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700683 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800684 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700685 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800686 }
687 uint32_t I1 = ~(J1 ^ S);
688 uint32_t I2 = ~(J2 ^ S);
689 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
690 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700691 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800692 break;
693 }
694 }
695 }
696 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800697 case 3:
698 switch (op2) {
699 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
700 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
701 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800702 // |111|11|100|000|0|0000|1111|110000|000000|
703 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
704 // |---|--|---|---|-|----|----|------|------|
705 // |332|22|222|222|2|1111|1111|110000|000000|
706 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
707 // |---|--|---|---|-|----|----|------|------|
708 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800709 uint32_t op3 = (instr >> 21) & 7;
710 //uint32_t op4 = (instr >> 6) & 0x3F;
711 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700712 case 0x0: case 0x4: {
713 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
714 // STRB Rt,[Rn,Rm,lsl #imm2] - 111 11 00 0 0 00 0 nnnn tttt 0 00000 ii mmmm
Elliott Hughes630e77d2012-03-22 19:20:56 -0700715 ArmRegister Rn(instr, 16);
716 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700717 opcode << "strb";
718 if ((instr & 0x800) != 0) {
719 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700720 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700721 } else {
722 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700723 ArmRegister Rm(instr, 0);
724 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700725 if (imm2 != 0) {
726 args << ", " << "lsl #" << imm2;
727 }
728 args << "]";
729 }
730 break;
731 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800732 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700733 ArmRegister Rn(instr, 16);
734 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800735 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700736 if ((instr & 0x800) != 0) {
737 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
738 uint32_t P = (instr >> 10) & 1;
739 uint32_t U = (instr >> 9) & 1;
740 uint32_t W = (instr >> 8) & 1;
741 uint32_t imm8 = instr & 0xFF;
742 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
743 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
744 opcode << "push";
745 args << Rt;
746 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
747 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800748 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700749 if (P == 1 && U == 1 && W == 0) {
750 opcode << "strt";
751 } else {
752 opcode << "str";
753 }
754 args << Rt << ", [" << Rn;
755 if (P == 0 && W == 1) {
756 args << "], #" << imm32;
757 } else {
758 args << ", #" << imm32 << "]";
759 if (W == 1) {
760 args << "!";
761 }
Ian Rogers40627db2012-03-04 17:31:09 -0800762 }
763 }
Ian Rogers66a3fca2012-04-09 19:51:34 -0700764 } else {
765 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
766 ArmRegister Rn(instr, 16);
767 ArmRegister Rt(instr, 12);
768 ArmRegister Rm(instr, 0);
769 uint32_t imm2 = (instr >> 4) & 3;
770 opcode << "str.w";
771 args << Rt << ", [" << Rn << ", " << Rm;
772 if (imm2 != 0) {
773 args << ", lsl #" << imm2;
774 }
775 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800776 }
777 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700778 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800779 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700780 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700781 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800782 }
Ian Rogers40627db2012-03-04 17:31:09 -0800783 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800784 }
785 }
786
787 break;
788 }
789 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
790 // Load word
791 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
792 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
793 // |---|--|--|---|--|-|----|----|------|------|
794 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
795 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
796 // |---|--|--|---|--|-|----|----|------|------|
797 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
798 // |111|11| op2 | | | imm12 |
799 uint32_t op3 = (instr >> 23) & 3;
800 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700801 ArmRegister Rn(instr, 16);
802 ArmRegister Rt(instr, 12);
803 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800804 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
805 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
806 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700807 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700808 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700809 if (Rn.r == 9) {
810 args << " ; ";
811 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -0700812 } else if (Rn.r == 15) {
813 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
814 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
815 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700816 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800817 } else if (op4 == 0) {
818 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
819 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700820 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700821 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700822 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800823 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700824 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800825 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700826 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800827 } else {
828 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
829 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700830 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700831 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800832 }
833 break;
834 }
835 }
836 default:
837 break;
838 }
Ian Rogers9af89402012-09-07 11:29:35 -0700839
840 // Apply any IT-block conditions to the opcode if necessary.
841 if (!it_conditions_.empty()) {
842 opcode << it_conditions_.back();
843 it_conditions_.pop_back();
844 }
845
Elliott Hughescbf0b612012-03-15 16:23:47 -0700846 os << StringPrintf("\t\t\t%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800847 return 4;
848}
849
850size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
851 uint16_t instr = ReadU16(instr_ptr);
852 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
853 if (is_32bit) {
854 return DumpThumb32(os, instr_ptr);
855 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700856 std::ostringstream opcode;
857 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800858 uint16_t opcode1 = instr >> 10;
859 if (opcode1 < 0x10) {
860 // shift (immediate), add, subtract, move, and compare
861 uint16_t opcode2 = instr >> 9;
862 switch (opcode2) {
863 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
864 case 0x8: case 0x9: case 0xA: case 0xB: {
865 // Logical shift left - 00 000xx xxxxxxxxx
866 // Logical shift right - 00 001xx xxxxxxxxx
867 // Arithmetic shift right - 00 010xx xxxxxxxxx
868 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700869 ThumbRegister rm(instr, 3);
870 ThumbRegister Rd(instr, 7);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800871 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700872 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800873 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700874 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800875 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700876 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800877 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700878 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800879 break;
880 }
881 case 0xC: case 0xD: case 0xE: case 0xF: {
882 // Add register - 00 01100 mmm nnn ddd
883 // Sub register - 00 01101 mmm nnn ddd
884 // Add 3-bit immediate - 00 01110 iii nnn ddd
885 // Sub 3-bit immediate - 00 01111 iii nnn ddd
886 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700887 ThumbRegister Rn(instr, 3);
888 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800889 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700890 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800891 } else {
892 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700893 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800894 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700895 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800896 }
897 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700898 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800899 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700900 ArmRegister Rm(imm3_or_Rm);
901 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800902 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700903 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800904 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800905 break;
906 }
907 case 0x10: case 0x11: case 0x12: case 0x13:
908 case 0x14: case 0x15: case 0x16: case 0x17:
909 case 0x18: case 0x19: case 0x1A: case 0x1B:
910 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
911 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
912 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
913 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
914 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700915 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800916 uint16_t imm8 = instr & 0xFF;
917 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700918 case 4: opcode << "movs"; break;
919 case 5: opcode << "cmp"; break;
920 case 6: opcode << "adds"; break;
921 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800922 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700923 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800924 break;
925 }
926 default:
927 break;
928 }
Ian Rogersad03ef52012-03-18 19:34:47 -0700929 } else if (opcode1 == 0x10) {
930 // Data-processing
931 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700932 ThumbRegister rm(instr, 3);
933 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -0700934 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700935 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800936 } else if (opcode1 == 0x11) {
937 // Special data instructions and branch and exchange
938 uint16_t opcode2 = (instr >> 6) & 0x0F;
939 switch (opcode2) {
940 case 0x0: case 0x1: case 0x2: case 0x3: {
941 // Add low registers - 010001 0000 xxxxxx
942 // Add high registers - 010001 0001/001x xxxxxx
943 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700944 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800945 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700946 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700947 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700948 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800949 break;
950 }
951 case 0x8: case 0x9: case 0xA: case 0xB: {
952 // Move low registers - 010001 1000 xxxxxx
953 // Move high registers - 010001 1001/101x xxxxxx
954 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700955 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800956 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700957 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700958 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700959 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800960 break;
961 }
962 case 0x5: case 0x6: case 0x7: {
963 // Compare high registers - 010001 0101/011x xxxxxx
964 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700965 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800966 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700967 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700968 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700969 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800970 break;
971 }
972 case 0xC: case 0xD: case 0xE: case 0xF: {
973 // Branch and exchange - 010001 110x xxxxxx
974 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -0700975 ArmRegister rm(instr, 3);
976 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
977 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800978 break;
979 }
980 default:
981 break;
982 }
Ian Rogersd83bc362012-09-07 17:43:13 -0700983 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
984 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
985 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
986 // Load/store single data item
987 uint16_t opA = (instr >> 12) & 0xF;
988 if (opA == 0x5) {
989 uint16_t opB = (instr >> 9) & 0x7;
990 ThumbRegister Rm(instr, 6);
991 ThumbRegister Rn(instr, 3);
992 ThumbRegister Rt(instr, 0);
993 switch(opB) {
994 case 0: opcode << "str"; break;
995 case 1: opcode << "strh"; break;
996 case 2: opcode << "strb"; break;
997 case 3: opcode << "ldrsb"; break;
998 case 4: opcode << "ldr"; break;
999 case 5: opcode << "ldrh"; break;
1000 case 6: opcode << "ldrb"; break;
1001 case 7: opcode << "ldrsh"; break;
1002 }
1003 args << Rt << ", [" << Rn << ", " << Rm << "]";
1004 } else if (opA == 9) {
1005 uint16_t opB = (instr >> 11) & 1;
1006 ThumbRegister Rt(instr, 8);
1007 uint16_t imm8 = instr & 0xFF;
1008 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001009 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001010 } else {
1011 uint16_t imm5 = (instr >> 6) & 0x1F;
1012 uint16_t opB = (instr >> 11) & 1;
1013 ThumbRegister Rn(instr, 3);
1014 ThumbRegister Rt(instr, 0);
1015 switch(opA) {
1016 case 6:
1017 imm5 <<= 2;
1018 opcode << (opB == 0 ? "str" : "ldr");
1019 break;
1020 case 7:
1021 imm5 <<= 0;
1022 opcode << (opB == 0 ? "strb" : "ldrb");
1023 break;
1024 case 8:
1025 imm5 <<= 1;
1026 opcode << (opB == 0 ? "strh" : "ldrh");
1027 break;
1028 }
1029 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1030 }
Ian Rogers9af89402012-09-07 11:29:35 -07001031 } else if ((instr & 0xF800) == 0xA800) {
1032 // Generate SP-relative address
1033 ThumbRegister rd(instr, 8);
1034 int imm8 = instr & 0xFF;
1035 opcode << "add";
1036 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001037 } else if ((instr & 0xF000) == 0xB000) {
1038 // Miscellaneous 16-bit instructions
1039 uint16_t opcode2 = (instr >> 5) & 0x7F;
1040 switch (opcode2) {
1041 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1042 // Add immediate to SP - 1011 00000 ii iiiii
1043 // Subtract immediate from SP - 1011 00001 ii iiiii
1044 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001045 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001046 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001047 break;
1048 }
Ian Rogers087b2412012-03-21 01:30:32 -07001049 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001050 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
1051 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
1052 case 0x4C: case 0x4D: case 0x4E: case 0x4F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001053 // CBNZ, CBZ
1054 uint16_t op = (instr >> 11) & 1;
1055 uint16_t i = (instr >> 9) & 1;
1056 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001057 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001058 opcode << (op != 0 ? "cbnz" : "cbz");
1059 uint32_t imm32 = (i << 7) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001060 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001061 DumpBranchTarget(args, instr_ptr + 4, imm32);
1062 break;
1063 }
Ian Rogers40627db2012-03-04 17:31:09 -08001064 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1065 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1066 // If-Then, and hints
1067 uint16_t opA = (instr >> 4) & 0xF;
1068 uint16_t opB = instr & 0xF;
1069 if (opB == 0) {
1070 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001071 case 0: opcode << "nop"; break;
1072 case 1: opcode << "yield"; break;
1073 case 2: opcode << "wfe"; break;
1074 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001075 default: break;
1076 }
1077 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001078 uint32_t first_cond = opA;
1079 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001080 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001081
1082 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1083 // and store up the actual condition codes we'll want to add to the next few opcodes.
1084 size_t count = 3 - CTZ(mask);
1085 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
1086 for (size_t i = 0; i < count; ++i) {
1087 bool positive_cond = ((first_cond & 1) != 0);
1088 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1089 if (positive_mask == positive_cond) {
1090 opcode << 't';
1091 it_conditions_[i] = kConditionCodeNames[first_cond];
1092 } else {
1093 opcode << 'e';
1094 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1095 }
1096 }
1097 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
1098
1099 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1100 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001101 }
1102 break;
1103 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001104 default:
1105 break;
1106 }
1107 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1108 ((instr & 0xE000) == 0x8000)) {
1109 // Load/store single data item
1110 uint16_t opA = instr >> 12;
1111 //uint16_t opB = (instr >> 9) & 7;
1112 switch (opA) {
1113 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001114 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1115 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001116 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001117 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001118 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001119 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1120 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001121 break;
1122 }
1123 case 0x9: {
1124 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1125 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1126 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001127 ThumbRegister Rt(instr, 8);
1128 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1129 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001130 break;
1131 }
1132 default:
1133 break;
1134 }
Ian Rogers40627db2012-03-04 17:31:09 -08001135 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1136 uint16_t imm11 = instr & 0x7FFF;
1137 int32_t imm32 = imm11 << 1;
1138 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001139 opcode << "b";
1140 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001141 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001142
1143 // Apply any IT-block conditions to the opcode if necessary.
1144 if (!it_conditions_.empty()) {
1145 opcode << it_conditions_.back();
1146 it_conditions_.pop_back();
1147 }
1148
Elliott Hughescbf0b612012-03-15 16:23:47 -07001149 os << StringPrintf("\t\t\t%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001150 }
1151 return 2;
1152}
1153
1154} // namespace arm
1155} // namespace art