blob: 52fd736cdb9a1026fc670c069d6d4452dbda943b [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 Rogersef7d42f2014-01-06 12:55:46 -080019#include <inttypes.h>
20
Ian Rogerscf7f1912014-10-22 22:06:39 -070021#include <ostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070022#include <sstream>
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070026#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070027
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080028namespace art {
29namespace arm {
30
Ian Rogersb23a7722012-10-09 16:54:26 -070031size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
32 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
33 DumpArm(os, begin);
34 return 4;
35 } else {
36 // remove thumb specifier bits
37 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
38 return DumpThumb16(os, begin);
39 }
40}
41
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080042void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
43 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
44 for (const uint8_t* cur = begin; cur < end; cur += 4) {
45 DumpArm(os, cur);
46 }
47 } else {
48 // remove thumb specifier bits
49 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
50 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
51 for (const uint8_t* cur = begin; cur < end;) {
52 cur += DumpThumb16(os, cur);
53 }
54 }
55}
56
Elliott Hughes77405792012-03-15 15:22:12 -070057static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070058 "eq", // 0000 - equal
59 "ne", // 0001 - not-equal
60 "cs", // 0010 - carry-set, greater than, equal or unordered
61 "cc", // 0011 - carry-clear, less than
62 "mi", // 0100 - minus, negative
63 "pl", // 0101 - plus, positive or zero
64 "vs", // 0110 - overflow
65 "vc", // 0111 - no overflow
66 "hi", // 1000 - unsigned higher
67 "ls", // 1001 - unsigned lower or same
68 "ge", // 1010 - signed greater than or equal
69 "lt", // 1011 - signed less than
70 "gt", // 1100 - signed greater than
71 "le", // 1101 - signed less than or equal
72 "", // 1110 - always
73 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080074};
75
76void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
77 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070078 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080079 } else {
80 os << "Unexpected condition: " << cond;
81 }
82}
83
Ian Rogersb122a4b2013-11-19 18:00:50 -080084void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
85 switch (domain) {
Andreas Gampec8ccf682014-09-29 20:07:43 -070086 case 15U /* 0b1111 */: os << "sy"; break;
87 case 14U /* 0b1110 */: os << "st"; break;
88 case 11U /* 0b1011 */: os << "ish"; break;
89 case 10U /* 0b1010 */: os << "ishst"; break;
90 case 7U /* 0b0111 */: os << "nsh"; break;
91 case 6U /* 0b0110 */: os << "nshst"; break;
92 case 3U /* 0b0011 */: os << "osh"; break;
93 case 2U /* 0b0010 */: os << "oshst"; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -080094 }
95}
96
Ian Rogers40627db2012-03-04 17:31:09 -080097void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070098 os << StringPrintf("%+d (", imm32) << FormatInstructionPointer(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080099}
100
101static uint32_t ReadU16(const uint8_t* ptr) {
102 return ptr[0] | (ptr[1] << 8);
103}
104
105static uint32_t ReadU32(const uint8_t* ptr) {
106 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
107}
108
Elliott Hughes77405792012-03-15 15:22:12 -0700109static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700110 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
111 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700112};
113
Ian Rogersad03ef52012-03-18 19:34:47 -0700114static const char* kThumbDataProcessingOperations[] = {
115 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
116 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
117};
118
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100119static const char* const kThumb2ShiftOperations[] = {
120 "lsl", "lsr", "asr", "ror"
121};
122
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100123static const char* kThumbReverseOperations[] = {
124 "rev", "rev16", "rbit", "revsh"
125};
126
Elliott Hughes77405792012-03-15 15:22:12 -0700127struct ArmRegister {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800128 explicit ArmRegister(uint32_t r_in) : r(r_in) { CHECK_LE(r_in, 15U); }
129 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) {
130 CHECK_LE(r, 15U);
131 }
Elliott Hughes77405792012-03-15 15:22:12 -0700132 uint32_t r;
133};
134std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
135 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700136 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700137 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700138 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700139 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700140 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700141 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700142 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700143 }
144 return os;
145}
146
Elliott Hughes630e77d2012-03-22 19:20:56 -0700147struct ThumbRegister : ArmRegister {
148 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700149};
150
151struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700152 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700153 uint32_t shift;
154 ArmRegister rm;
155};
156std::ostream& operator<<(std::ostream& os, const Rm& r) {
157 os << r.rm;
158 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700159 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700160 }
161 return os;
162}
163
Elliott Hughes1ca98492012-04-12 17:21:02 -0700164struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700165 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700166 uint32_t rotate = ((instruction >> 8) & 0xf);
167 uint32_t imm = (instruction & 0xff);
168 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
169 }
170 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700171};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700172std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700173 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700174 return os;
175}
176
177struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700178 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700179 uint32_t register_list;
180};
181std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
182 if (rhs.register_list == 0) {
183 os << "<no register list?>";
184 return os;
185 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700186 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700187 bool first = true;
188 for (size_t i = 0; i < 16; i++) {
189 if ((rhs.register_list & (1 << i)) != 0) {
190 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700191 first = false;
192 } else {
193 os << ", ";
194 }
195 os << ArmRegister(i);
196 }
197 }
198 os << "}";
199 return os;
200}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800201
Vladimir Markodd577a32013-11-07 19:25:24 +0000202struct FpRegister {
203 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
204 size = (instr >> 8) & 1;
205 uint32_t Vn = (instr >> at_bit) & 0xF;
206 uint32_t N = (instr >> extra_at_bit) & 1;
207 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
208 }
Zheng Xue19649a2014-02-27 13:30:55 +0000209 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit,
210 uint32_t forced_size) {
211 size = forced_size;
212 uint32_t Vn = (instr >> at_bit) & 0xF;
213 uint32_t N = (instr >> extra_at_bit) & 1;
214 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
215 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000216 FpRegister(const FpRegister& other, uint32_t offset)
217 : size(other.size), r(other.r + offset) {}
218
219 uint32_t size; // 0 = f32, 1 = f64
220 uint32_t r;
221};
222std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
223 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
224}
225
226struct FpRegisterRange {
227 explicit FpRegisterRange(uint32_t instr)
228 : first(instr, 12, 22), imm8(instr & 0xFF) {}
229 FpRegister first;
230 uint32_t imm8;
231};
232std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
233 os << "{" << rhs.first;
234 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
235 if (count > 1) {
236 os << "-" << FpRegister(rhs.first, count - 1);
237 }
238 if (rhs.imm8 == 0) {
239 os << " (EMPTY)";
240 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
241 os << rhs.first << " (HALF)";
242 }
243 os << "}";
244 return os;
245}
246
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800247void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700248 uint32_t instruction = ReadU32(instr_ptr);
249 uint32_t cond = (instruction >> 28) & 0xf;
250 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700251 std::string opcode;
252 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700253 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700254 switch (op1) {
255 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700256 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700257 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700258 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700259 opcode = "bkpt";
260 uint32_t imm12 = (instruction >> 8) & 0xfff;
261 uint32_t imm4 = (instruction & 0xf);
262 args << '#' << ((imm12 << 4) | imm4);
263 break;
264 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700265 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700266 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700267 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700268 break;
269 }
270 bool i = (instruction & (1 << 25)) != 0;
271 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700272 uint32_t op = (instruction >> 21) & 0xf;
273 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700274 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700275 bool is_mov = op == 13U /* 0b1101 */ || op == 15U /* 0b1111 */;
Dave Allison20dfc792014-06-16 20:44:29 -0700276 if (is_mov) {
277 // Show only Rd and Rm.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700278 if (s) {
Dave Allison20dfc792014-06-16 20:44:29 -0700279 suffixes += 's';
280 }
281 args << ArmRegister(instruction, 12) << ", ";
282 if (i) {
283 args << ShiftedImmediate(instruction);
284 } else {
285 // TODO: Shifted register.
286 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
287 }
Elliott Hughes77405792012-03-15 15:22:12 -0700288 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700289 if (implicit_s) {
290 // Rd is unused (and not shown), and we don't show the 's' suffix either.
291 } else {
292 if (s) {
293 suffixes += 's';
294 }
295 args << ArmRegister(instruction, 12) << ", ";
296 }
297 if (i) {
298 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
299 } else {
300 // TODO: Shifted register.
301 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
302 }
Elliott Hughes77405792012-03-15 15:22:12 -0700303 }
304 }
305 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700306 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700307 {
308 bool p = (instruction & (1 << 24)) != 0;
309 bool b = (instruction & (1 << 22)) != 0;
310 bool w = (instruction & (1 << 21)) != 0;
311 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700312 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700313 args << ArmRegister(instruction, 12) << ", ";
314 ArmRegister rn(instruction, 16);
315 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700316 UNIMPLEMENTED(FATAL) << "literals";
317 } else {
318 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700319 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700320 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700321 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700322 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700323 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700324 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700325 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700326 } else {
327 LOG(FATAL) << p << " " << w;
328 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700329 if (rn.r == 9) {
330 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -0700331 Thread::DumpThreadOffset<4>(args, offset);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700332 }
Elliott Hughes77405792012-03-15 15:22:12 -0700333 }
334 }
335 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700336 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700337 {
338 bool p = (instruction & (1 << 24)) != 0;
339 bool u = (instruction & (1 << 23)) != 0;
340 bool w = (instruction & (1 << 21)) != 0;
341 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700342 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700343 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700344 }
345 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700346 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700347 {
348 bool bl = (instruction & (1 << 24)) != 0;
349 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700350 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700351 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700352 DumpBranchTarget(args, instr_ptr + 8, imm32);
353 }
354 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700355 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700356 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700357 break;
358 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700359 opcode += kConditionCodeNames[cond];
360 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700361 // TODO: a more complete ARM disassembler could generate wider opcodes.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362 os << FormatInstructionPointer(instr_ptr)
363 << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
364 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800365}
366
Ian Rogersa9650dd2013-10-04 08:23:32 -0700367int32_t ThumbExpand(int32_t imm12) {
368 if ((imm12 & 0xC00) == 0) {
369 switch ((imm12 >> 8) & 3) {
370 case 0:
371 return imm12 & 0xFF;
372 case 1:
373 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
374 case 2:
375 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
376 default: // 3
377 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
378 (imm12 & 0xFF);
379 }
380 } else {
381 uint32_t val = 0x80 | (imm12 & 0x7F);
382 int32_t rotate = (imm12 >> 7) & 0x1F;
383 return (val >> rotate) | (val << (32 - rotate));
384 }
385}
386
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100387uint32_t VFPExpand32(uint32_t imm8) {
388 CHECK_EQ(imm8 & 0xffu, imm8);
389 uint32_t bit_a = (imm8 >> 7) & 1;
390 uint32_t bit_b = (imm8 >> 6) & 1;
391 uint32_t slice = imm8 & 0x3f;
392 return (bit_a << 31) | ((1 << 30) - (bit_b << 25)) | (slice << 19);
393}
394
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800395static uint64_t VFPExpand64(uint32_t imm8) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100396 CHECK_EQ(imm8 & 0xffu, imm8);
397 uint64_t bit_a = (imm8 >> 7) & 1;
398 uint64_t bit_b = (imm8 >> 6) & 1;
399 uint64_t slice = imm8 & 0x3f;
400 return (bit_a << 31) | ((UINT64_C(1) << 62) - (bit_b << 54)) | (slice << 48);
401}
402
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800403size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
404 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
405 // |111|1 1|1000000|0000|1111110000000000|
406 // |5 3|2 1|0987654|3 0|5 0 5 0|
407 // |---|---|-------|----|----------------|
408 // |332|2 2|2222222|1111|1111110000000000|
409 // |1 9|8 7|6543210|9 6|5 0 5 0|
410 // |---|---|-------|----|----------------|
411 // |111|op1| op2 | | |
412 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700413 if (op1 == 0) {
414 return DumpThumb16(os, instr_ptr);
415 }
416
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800417 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700418 std::ostringstream opcode;
419 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800420 switch (op1) {
421 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800422 break;
423 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700424 if ((op2 & 0x64) == 0) { // 00x x0xx
425 // |111|11|10|00|0|00|0000|1111110000000000|
426 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
427 // |---|--|--|--|-|--|----|----------------|
428 // |332|22|22|22|2|22|1111|1111110000000000|
429 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
430 // |---|--|--|--|-|--|----|----------------|
431 // |111|01|00|op|0|WL| Rn | |
432 // |111|01| op2 | | |
433 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
434 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
435 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
436 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
437 uint32_t op = (instr >> 23) & 3;
438 uint32_t W = (instr >> 21) & 1;
439 uint32_t L = (instr >> 20) & 1;
440 ArmRegister Rn(instr, 16);
441 if (op == 1 || op == 2) {
442 if (op == 1) {
443 if (L == 0) {
444 opcode << "stm";
445 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800446 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700447 if (Rn.r != 13) {
448 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700449 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700450 } else {
451 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800452 }
453 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700454 } else {
455 if (L == 0) {
456 if (Rn.r != 13) {
457 opcode << "stmdb";
458 args << Rn << (W == 0 ? "" : "!") << ", ";
459 } else {
460 opcode << "push";
461 }
462 } else {
463 opcode << "ldmdb";
464 args << Rn << (W == 0 ? "" : "!") << ", ";
465 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800466 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700467 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800468 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700469 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700470 uint32_t op3 = (instr >> 23) & 3;
471 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700472 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700473 ArmRegister Rn(instr, 16);
474 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700475 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700476 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700477 if ((op3 & 2) == 2) { // 1x
478 int W = (instr >> 21) & 1;
479 int U = (instr >> 23) & 1;
480 int P = (instr >> 24) & 1;
481
482 if ((op4 & 1) == 1) {
483 opcode << "ldrd";
484 } else {
485 opcode << "strd";
486 }
487 args << Rt << "," << Rd << ", [" << Rn;
488 const char *sign = U ? "+" : "-";
489 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000490 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700491 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000492 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700493 if (W == 1) {
494 args << "!";
495 }
496 }
497 } else { // 0x
498 switch (op4) {
499 case 0:
500 if (op3 == 0) { // op3 is 00, op4 is 00
501 opcode << "strex";
502 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000503 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
504 Rd.r == Rn.r || Rd.r == Rt.r) {
505 args << " (UNPREDICTABLE)";
506 }
Dave Allison70202782013-10-22 17:52:19 -0700507 } else { // op3 is 01, op4 is 00
508 // this is one of strexb, strexh or strexd
509 int op5 = (instr >> 4) & 0xf;
510 switch (op5) {
511 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700512 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000513 opcode << ((op5 == 4) ? "strexb" : "strexh");
514 Rd = ArmRegister(instr, 0);
515 args << Rd << ", " << Rt << ", [" << Rn << "]";
516 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
517 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
518 args << " (UNPREDICTABLE)";
519 }
Dave Allison70202782013-10-22 17:52:19 -0700520 break;
521 case 7:
522 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000523 ArmRegister Rt2 = Rd;
524 Rd = ArmRegister(instr, 0);
525 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
526 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
527 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
528 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
529 args << " (UNPREDICTABLE)";
530 }
Dave Allison70202782013-10-22 17:52:19 -0700531 break;
532 }
533 }
534 break;
535 case 1:
536 if (op3 == 0) { // op3 is 00, op4 is 01
537 opcode << "ldrex";
538 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000539 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
540 args << " (UNPREDICTABLE)";
541 }
Dave Allison70202782013-10-22 17:52:19 -0700542 } else { // op3 is 01, op4 is 01
543 // this is one of strexb, strexh or strexd
544 int op5 = (instr >> 4) & 0xf;
545 switch (op5) {
546 case 0:
547 opcode << "tbb";
548 break;
549 case 1:
550 opcode << "tbh";
551 break;
552 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700553 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000554 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
555 args << Rt << ", [" << Rn << "]";
556 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
557 args << " (UNPREDICTABLE)";
558 }
Dave Allison70202782013-10-22 17:52:19 -0700559 break;
560 case 7:
561 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000562 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
563 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
564 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
565 args << " (UNPREDICTABLE)";
566 }
Dave Allison70202782013-10-22 17:52:19 -0700567 break;
568 }
569 }
570 break;
571 case 2: // op3 is 0x, op4 is 10
572 case 3: // op3 is 0x, op4 is 11
573 if (op4 == 2) {
574 opcode << "strd";
575 } else {
576 opcode << "ldrd";
577 }
578 int W = (instr >> 21) & 1;
579 int U = (instr >> 23) & 1;
580 int P = (instr >> 24) & 1;
581
582 args << Rt << "," << Rd << ", [" << Rn;
583 const char *sign = U ? "+" : "-";
584 if (P == 0 && W == 1) {
585 args << "], #" << sign << imm8;
586 } else {
587 args << ", #" << sign << imm8 << "]";
588 if (W == 1) {
589 args << "!";
590 }
591 }
592 break;
593 }
594 }
595
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700596 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
597 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100598 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
599 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
600 // |---|----|----|-|----|----|----|--|--|----|
601 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
602 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
603 // |---|----|----|-|----|----|----|--|--|----|
604 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700605 uint32_t op3 = (instr >> 21) & 0xF;
606 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100607 uint32_t imm3 = ((instr >> 12) & 0x7);
608 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700609 uint32_t imm5 = ((imm3 << 2) | imm2);
610 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700611 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100612 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700613 ArmRegister Rm(instr, 0);
614 switch (op3) {
615 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100616 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700617 opcode << "and";
618 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700619 if (S != 1U) {
620 opcode << "UNKNOWN TST-" << S;
621 break;
622 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700623 opcode << "tst";
624 S = 0; // don't print 's'
625 }
626 break;
627 case 0x1: opcode << "bic"; break;
628 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100629 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700630 opcode << "orr";
631 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100632 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700633 opcode << "mov";
634 }
635 break;
636 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100637 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700638 opcode << "orn";
639 } else {
640 opcode << "mvn";
641 }
642 break;
643 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100644 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700645 opcode << "eor";
646 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700647 if (S != 1U) {
648 opcode << "UNKNOWN TEQ-" << S;
649 break;
650 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700651 opcode << "teq";
652 S = 0; // don't print 's'
653 }
654 break;
655 case 0x6: opcode << "pkh"; break;
656 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100657 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700658 opcode << "add";
659 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700660 if (S != 1U) {
661 opcode << "UNKNOWN CMN-" << S;
662 break;
663 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700664 opcode << "cmn";
665 S = 0; // don't print 's'
666 }
667 break;
668 case 0xA: opcode << "adc"; break;
669 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100670 case 0xD:
671 if (Rd.r != 0xF) {
672 opcode << "sub";
673 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700674 if (S != 1U) {
675 opcode << "UNKNOWN CMP-" << S;
676 break;
677 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100678 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100679 S = 0; // don't print 's'
680 }
681 break;
682 case 0xE: opcode << "rsb"; break;
683 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700684 }
Ian Rogers087b2412012-03-21 01:30:32 -0700685
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700686 if (S == 1) {
687 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700688 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700689 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100690
691 if (Rd.r != 0xF) {
692 args << Rd << ", ";
693 }
694 if (Rn.r != 0xF) {
695 args << Rn << ", ";
696 }
697 args << Rm;
698
699 // Shift operand.
700 bool noShift = (imm5 == 0 && shift_type != 0x3);
701 if (!noShift) {
702 args << ", ";
703 switch (shift_type) {
704 case 0x0: args << "lsl"; break;
705 case 0x1: args << "lsr"; break;
706 case 0x2: args << "asr"; break;
707 case 0x3:
708 if (imm5 == 0) {
709 args << "rrx";
710 } else {
711 args << "ror";
712 }
713 break;
714 }
715 if (shift_type != 0x3 /* rrx */) {
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700716 args << StringPrintf(" #%d", (0 != imm5 || 0 == shift_type) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100717 }
718 }
719
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700720 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
721 // Co-processor instructions
722 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
723 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
724 // |---|-|--|------|----|----|----|---|---|----|
725 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
726 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
727 // |---|-|--|------|----|----|----|---|---|----|
728 // |111| |11| op3 | Rn | |copr| |op4| |
729 uint32_t op3 = (instr >> 20) & 0x3F;
730 uint32_t coproc = (instr >> 8) & 0xF;
731 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700732
Ian Rogersef6a7762013-12-19 17:58:05 -0800733 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000734 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
735 // Extension register load/store instructions
736 // |1111|110|00000|0000|1111|110|0|00000000|
737 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
738 // |----|---|-----|----|----|---|-|--------|
739 // |3322|222|22222|1111|1111|110|0|00000000|
740 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
741 // |----|---|-----|----|----|---|-|--------|
742 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700743 uint32_t P = (instr >> 24) & 1;
744 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700745 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000746 if (P == U && W == 1) {
747 opcode << "UNDEFINED";
748 } else {
749 uint32_t L = (instr >> 20) & 1;
750 uint32_t S = (instr >> 8) & 1;
751 ArmRegister Rn(instr, 16);
752 if (P == 1 && W == 0) { // VLDR
753 FpRegister d(instr, 12, 22);
754 uint32_t imm8 = instr & 0xFF;
755 opcode << (L == 1 ? "vldr" : "vstr");
756 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
757 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800758 if (Rn.r == 15 && U == 1) {
759 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
760 lit_adr = RoundDown(lit_adr, 4) + 4 + (imm8 << 2);
Brian Carlstromc2687ef2014-03-13 15:12:11 -0700761 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (2)));
762 args << StringPrintf(" ; 0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
Ian Rogersef6a7762013-12-19 17:58:05 -0800763 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000764 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
765 opcode << (L == 1 ? "vpop" : "vpush");
766 args << FpRegisterRange(instr);
767 } else { // VLDM
768 opcode << (L == 1 ? "vldm" : "vstm");
769 args << Rn << ((W == 1) ? "!" : "") << ", "
770 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700771 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000772 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700773 }
Dave Allison70202782013-10-22 17:52:19 -0700774 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000775 if ((instr & 0xD0) == 0x10) {
776 // 64bit transfers between ARM core and extension registers.
777 uint32_t L = (instr >> 20) & 1;
778 uint32_t S = (instr >> 8) & 1;
779 ArmRegister Rt2(instr, 16);
780 ArmRegister Rt(instr, 12);
781 FpRegister m(instr, 0, 5);
782 opcode << "vmov" << (S ? ".f64" : ".f32");
783 if (L == 1) {
784 args << Rt << ", " << Rt2 << ", ";
785 }
786 if (S) {
787 args << m;
788 } else {
789 args << m << ", " << FpRegister(m, 1);
790 }
791 if (L == 0) {
792 args << ", " << Rt << ", " << Rt2;
793 }
794 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
795 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
796 args << " (UNPREDICTABLE)";
797 }
798 }
Dave Allison70202782013-10-22 17:52:19 -0700799 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
800 // fp data processing
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100801 // VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
802 // |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
803 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
804 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
805 // |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
806 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
807 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
808 // |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
809 // |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
810 // |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
811 // |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
812 // |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
813 // |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
814 // |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
815 // |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
816 // |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
817 // |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
818 uint32_t S = (instr >> 8) & 1;
819 uint32_t Q = (instr >> 6) & 1;
820 FpRegister d(instr, 12, 22);
821 FpRegister n(instr, 16, 7);
822 FpRegister m(instr, 0, 5);
Zheng Xue19649a2014-02-27 13:30:55 +0000823 if ((op3 & 0xB) == 0) { // 100x00
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100824 opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
Zheng Xue19649a2014-02-27 13:30:55 +0000825 args << d << ", " << n << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100826 } else if ((op3 & 0xB) == 0x2) { // 100x10
827 opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
828 args << d << ", " << n << ", " << m;
829 } else if ((op3 & 0xB) == 0x3) { // 100x11
830 opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
831 args << d << ", " << n << ", " << m;
832 } else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
833 opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
834 args << d << ", " << n << ", " << m;
835 } else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
836 uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
837 opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
838 args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
839 : StringPrintf("0x%08x", VFPExpand32(imm8)));
840 if ((instr & 0xa0) != 0) {
841 args << " (UNPREDICTABLE)";
842 }
843 } else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
844 // VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
845 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
846 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
847 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
848 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
849 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
850 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
851 // |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
852 // |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
853 // |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
854 // |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
855 // |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
856 // |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
857 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
858 uint32_t op5 = (instr >> 16) & 0xF;
859 uint32_t op = (instr >> 7) & 1;
860 // Register types in VCVT instructions rely on the combination of op5 and S.
861 FpRegister Dd(instr, 12, 22, 1);
862 FpRegister Sd(instr, 12, 22, 0);
863 FpRegister Dm(instr, 0, 5, 1);
864 FpRegister Sm(instr, 0, 5, 0);
865 if (op5 == 0) {
866 opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
867 args << d << ", " << m;
868 } else if (op5 == 1) {
869 opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
870 args << d << ", " << m;
871 } else if (op5 == 4) {
872 opcode << "vcmp" << (S != 0 ? ".f64" : ".f32");
873 args << d << ", " << m;
874 if (op != 0) {
875 args << " (quiet nan)";
876 }
877 } else if (op5 == 5) {
878 opcode << "vcmpe" << (S != 0 ? ".f64" : ".f32");
879 args << d << ", #0.0";
880 if (op != 0) {
881 args << " (quiet nan)";
882 }
883 if ((instr & 0x2f) != 0) {
884 args << " (UNPREDICTABLE)";
885 }
886 } else if (op5 == 0xD) {
887 if (S == 1) {
888 // vcvt{r}.s32.f64
889 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
890 args << Sd << ", " << Dm;
891 } else {
892 // vcvt{r}.s32.f32
893 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
894 args << Sd << ", " << Sm;
895 }
896 } else if (op5 == 0xC) {
897 if (S == 1) {
898 // vcvt{r}.u32.f64
899 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
900 args << Sd << ", " << Dm;
901 } else {
902 // vcvt{r}.u32.f32
903 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
904 args << Sd << ", " << Sm;
905 }
906 } else if (op5 == 0x8) {
907 if (S == 1) {
908 // vcvt.f64.<Tm>
909 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
910 args << Dd << ", " << Sm;
911 } else {
912 // vcvt.f32.<Tm>
913 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
914 args << Sd << ", " << Sm;
915 }
916 } else if (op5 == 0x7) {
917 if (op == 1) {
Zheng Xue19649a2014-02-27 13:30:55 +0000918 if (S == 1) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100919 // vcvt.f64.f32
920 opcode << "vcvt.f64.f32";
Zheng Xue19649a2014-02-27 13:30:55 +0000921 args << Dd << ", " << Sm;
922 } else {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100923 // vcvt.f32.f64
924 opcode << "vcvt.f32.f64";
925 args << Sd << ", " << Dm;
Zheng Xue19649a2014-02-27 13:30:55 +0000926 }
927 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100928 } else if ((op5 & 0xa) == 0xa) {
929 opcode << "vcvt";
930 args << "[undecoded: floating <-> fixed]";
Zheng Xue19649a2014-02-27 13:30:55 +0000931 }
932 }
Dave Allison70202782013-10-22 17:52:19 -0700933 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000934 if (coproc == 10 && (op3 & 0xE) == 0) {
935 // VMOV (between ARM core register and single-precision register)
936 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
937 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
938 // |----|----|---|- |----|----|----|-|--|-|----|
939 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
940 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
941 // |----|----|---|- |----|----|----|-|--|-|----|
942 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
943 uint32_t op = op3 & 1;
944 ArmRegister Rt(instr, 12);
945 FpRegister n(instr, 16, 7);
946 opcode << "vmov.f32";
947 if (op) {
948 args << Rt << ", " << n;
949 } else {
950 args << n << ", " << Rt;
951 }
952 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
953 args << " (UNPREDICTABLE)";
954 }
955 } else if (coproc == 10 && op3 == 0x2F) {
956 // VMRS
957 // |1111|11000000|0000|1111|1100|000|0|0000|
958 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
959 // |----|--------|----|----|----|---|-|----|
960 // |3322|22222222|1111|1111|1100|000|0|0000|
961 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
962 // |----|--------|----|----|----|---|-|----|
963 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
964 uint32_t spec_reg = (instr >> 16) & 0xF;
965 ArmRegister Rt(instr, 12);
966 opcode << "vmrs";
967 if (spec_reg == 1) {
968 if (Rt.r == 15) {
969 args << "APSR_nzcv, FPSCR";
970 } else if (Rt.r == 13) {
971 args << Rt << ", FPSCR (UNPREDICTABLE)";
972 } else {
973 args << Rt << ", FPSCR";
974 }
975 } else {
976 args << "(PRIVILEGED)";
977 }
978 } else if (coproc == 11 && (op3 & 0x9) != 8) {
979 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
980 }
Ian Rogers9af89402012-09-07 11:29:35 -0700981 }
Dave Allison70202782013-10-22 17:52:19 -0700982 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800983 }
984 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800985 case 2:
986 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
987 // Data-processing (modified immediate)
988 // |111|11|10|0000|0|0000|1|111|1100|00000000|
989 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
990 // |---|--|--|----|-|----|-|---|----|--------|
991 // |332|22|22|2222|2|1111|1|111|1100|00000000|
992 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
993 // |---|--|--|----|-|----|-|---|----|--------|
994 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
995 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800996 uint32_t i = (instr >> 26) & 1;
997 uint32_t op3 = (instr >> 21) & 0xF;
998 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700999 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -08001000 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001001 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001002 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001003 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
1004 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
1005 if (op3 == 0x2) {
1006 opcode << "mov";
1007 if (S == 1) {
1008 opcode << "s";
1009 }
1010 opcode << ".w";
1011 } else {
1012 opcode << "mvn";
1013 if (S == 1) {
1014 opcode << "s";
1015 }
1016 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001017 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001018 } else if (Rd.r == 0xF && S == 1 &&
1019 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
1020 if (op3 == 0x0) {
1021 opcode << "tst";
1022 } else if (op3 == 0x4) {
1023 opcode << "teq";
1024 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +00001025 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001026 } else {
1027 opcode << "cmp.w";
1028 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001029 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001030 } else {
1031 switch (op3) {
1032 case 0x0: opcode << "and"; break;
1033 case 0x1: opcode << "bic"; break;
1034 case 0x2: opcode << "orr"; break;
1035 case 0x3: opcode << "orn"; break;
1036 case 0x4: opcode << "eor"; break;
1037 case 0x8: opcode << "add"; break;
1038 case 0xA: opcode << "adc"; break;
1039 case 0xB: opcode << "sbc"; break;
1040 case 0xD: opcode << "sub"; break;
1041 case 0xE: opcode << "rsb"; break;
1042 default: opcode << "UNKNOWN DPMI-" << op3; break;
1043 }
1044 if (S == 1) {
1045 opcode << "s";
1046 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001047 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001048 }
Ian Rogers40627db2012-03-04 17:31:09 -08001049 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1050 // Data-processing (plain binary immediate)
1051 // |111|11|10|00000|0000|1|111110000000000|
1052 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1053 // |---|--|--|-----|----|-|---------------|
1054 // |332|22|22|22222|1111|1|111110000000000|
1055 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1056 // |---|--|--|-----|----|-|---------------|
1057 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1058 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001059 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001060 case 0x00: case 0x0A: {
1061 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001062 ArmRegister Rd(instr, 8);
1063 ArmRegister Rn(instr, 16);
1064 uint32_t i = (instr >> 26) & 1;
1065 uint32_t imm3 = (instr >> 12) & 0x7;
1066 uint32_t imm8 = instr & 0xFF;
1067 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1068 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001069 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001070 args << Rd << ", " << Rn << ", #" << imm12;
1071 } else {
1072 opcode << "adr";
1073 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001074 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001075 }
1076 break;
1077 }
Ian Rogers55019132013-02-08 01:05:23 -08001078 case 0x04: case 0x0C: {
1079 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001080 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001081 uint32_t i = (instr >> 26) & 1;
1082 uint32_t imm3 = (instr >> 12) & 0x7;
1083 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001084 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001085 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001086 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001087 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001088 break;
1089 }
jeffhaoeae26912013-01-28 16:29:54 -08001090 case 0x16: {
1091 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
1092 ArmRegister Rd(instr, 8);
1093 ArmRegister Rn(instr, 16);
1094 uint32_t msb = instr & 0x1F;
1095 uint32_t imm2 = (instr >> 6) & 0x3;
1096 uint32_t imm3 = (instr >> 12) & 0x7;
1097 uint32_t lsb = (imm3 << 2) | imm2;
1098 uint32_t width = msb - lsb + 1;
1099 if (Rn.r != 0xF) {
1100 opcode << "bfi";
1101 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1102 } else {
1103 opcode << "bfc";
1104 args << Rd << ", #" << lsb << ", #" << width;
1105 }
1106 break;
1107 }
Ian Rogers40627db2012-03-04 17:31:09 -08001108 default:
1109 break;
1110 }
1111 } else {
1112 // Branches and miscellaneous control
1113 // |111|11|1000000|0000|1|111|1100|00000000|
1114 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1115 // |---|--|-------|----|-|---|----|--------|
1116 // |332|22|2222222|1111|1|111|1100|00000000|
1117 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1118 // |---|--|-------|----|-|---|----|--------|
1119 // |111|10| op2 | |1|op3|op4 | |
1120
1121 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001122 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001123 switch (op3) {
1124 case 0:
1125 if ((op2 & 0x38) != 0x38) {
1126 // Conditional branch
1127 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1128 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1129 // |---|--|-|----|------|-|-|--|-|--|-----------|
1130 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1131 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1132 // |---|--|-|----|------|-|-|--|-|--|-----------|
1133 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1134 uint32_t S = (instr >> 26) & 1;
1135 uint32_t J2 = (instr >> 11) & 1;
1136 uint32_t J1 = (instr >> 13) & 1;
1137 uint32_t imm6 = (instr >> 16) & 0x3F;
1138 uint32_t imm11 = instr & 0x7FF;
1139 uint32_t cond = (instr >> 22) & 0xF;
1140 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1141 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001142 opcode << "b";
1143 DumpCond(opcode, cond);
1144 opcode << ".w";
1145 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001146 } else if (op2 == 0x3B) {
1147 // Miscellaneous control instructions
1148 uint32_t op5 = (instr >> 4) & 0xF;
1149 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001150 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1151 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1152 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001153 }
Ian Rogers40627db2012-03-04 17:31:09 -08001154 }
1155 break;
1156 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001157 if ((op2 & 0x38) == 0x38) {
1158 if (op2 == 0x7F) {
1159 opcode << "udf";
1160 }
1161 break;
1162 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001163 FALLTHROUGH_INTENDED; // Else deliberate fall-through to B.
Ian Rogersd0876a92013-02-08 11:30:38 -08001164 case 1: case 3: {
1165 // B
1166 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1167 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1168 // |---|--|-|----|------|--|--|-|--|-----------|
1169 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1170 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1171 // |---|--|-|----|------|--|--|-|--|-----------|
1172 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1173 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1174 uint32_t S = (instr >> 26) & 1;
1175 uint32_t cond = (instr >> 22) & 0xF;
1176 uint32_t J2 = (instr >> 11) & 1;
1177 uint32_t form = (instr >> 12) & 1;
1178 uint32_t J1 = (instr >> 13) & 1;
1179 uint32_t imm10 = (instr >> 16) & 0x3FF;
1180 uint32_t imm6 = (instr >> 16) & 0x3F;
1181 uint32_t imm11 = instr & 0x7FF;
1182 opcode << "b";
1183 int32_t imm32;
1184 if (form == 0) {
1185 DumpCond(opcode, cond);
1186 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1187 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1188 } else {
1189 uint32_t I1 = ~(J1 ^ S);
1190 uint32_t I2 = ~(J2 ^ S);
1191 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1192 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1193 }
1194 opcode << ".w";
1195 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001196 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001197 }
Ian Rogers40627db2012-03-04 17:31:09 -08001198 case 4: case 6: case 5: case 7: {
1199 // BL, BLX (immediate)
1200 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1201 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1202 // |---|--|-|----------|--|--|-|--|-----------|
1203 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1204 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1205 // |---|--|-|----------|--|--|-|--|-----------|
Dave Allisond6ed6422014-04-09 23:36:15 +00001206 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
Ian Rogers40627db2012-03-04 17:31:09 -08001207 uint32_t S = (instr >> 26) & 1;
1208 uint32_t J2 = (instr >> 11) & 1;
Dave Allisond6ed6422014-04-09 23:36:15 +00001209 uint32_t L = (instr >> 12) & 1;
Ian Rogers40627db2012-03-04 17:31:09 -08001210 uint32_t J1 = (instr >> 13) & 1;
1211 uint32_t imm10 = (instr >> 16) & 0x3FF;
1212 uint32_t imm11 = instr & 0x7FF;
Dave Allisond6ed6422014-04-09 23:36:15 +00001213 if (L == 0) {
1214 opcode << "bx";
Dave Allisonf9487c02014-04-08 23:08:12 +00001215 } else {
Dave Allisond6ed6422014-04-09 23:36:15 +00001216 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001217 }
1218 uint32_t I1 = ~(J1 ^ S);
1219 uint32_t I2 = ~(J2 ^ S);
1220 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1221 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001222 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001223 break;
1224 }
1225 }
1226 }
1227 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001228 case 3:
1229 switch (op2) {
1230 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001231 case 0x08: case 0x09: case 0x0A: case 0x0C: case 0x0E: {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001232 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001233 // |111|11|100|000|0|0000|1111|110000|000000|
1234 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1235 // |---|--|---|---|-|----|----|------|------|
1236 // |332|22|222|222|2|1111|1111|110000|000000|
1237 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1238 // |---|--|---|---|-|----|----|------|------|
1239 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001240 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001241 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001242 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001243 case 0x0: case 0x4: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001244 // {ST,LD}RB Rt,[Rn,#+/-imm12] - 111 11 00 0 1 00 0 nnnn tttt 1 PUWii ii iiii
1245 // {ST,LD}RB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1246 // {ST,LD}RB 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 -07001247 ArmRegister Rn(instr, 16);
1248 ArmRegister Rt(instr, 12);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001249 opcode << (HasBitSet(instr, 20) ? "ldrb" : "strb");
1250 if (HasBitSet(instr, 23)) {
1251 uint32_t imm12 = instr & 0xFFF;
1252 args << Rt << ", [" << Rn << ",#" << imm12 << "]";
1253 } else if ((instr & 0x800) != 0) {
1254 uint32_t imm8 = instr & 0xFF;
1255 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
1256 } else {
1257 uint32_t imm2 = (instr >> 4) & 3;
1258 ArmRegister Rm(instr, 0);
1259 args << Rt << ", [" << Rn << ", " << Rm;
1260 if (imm2 != 0) {
1261 args << ", " << "lsl #" << imm2;
1262 }
1263 args << "]";
1264 }
1265 break;
1266 }
1267 case 0x1: case 0x5: {
1268 // STRH Rt,[Rn,#+/-imm12] - 111 11 00 0 1 01 0 nnnn tttt 1 PUWii ii iiii
1269 // STRH Rt,[Rn,#+/-imm8] - 111 11 00 0 0 01 0 nnnn tttt 1 PUWii ii iiii
1270 // STRH Rt,[Rn,Rm,lsl #imm2] - 111 11 00 0 0 01 0 nnnn tttt 0 00000 ii mmmm
1271 ArmRegister Rn(instr, 16);
1272 ArmRegister Rt(instr, 12);
1273 opcode << "strh";
1274 if (HasBitSet(instr, 23)) {
1275 uint32_t imm12 = instr & 0xFFF;
1276 args << Rt << ", [" << Rn << ",#" << imm12 << "]";
1277 } else if ((instr & 0x800) != 0) {
Ian Rogers087b2412012-03-21 01:30:32 -07001278 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001279 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001280 } else {
1281 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001282 ArmRegister Rm(instr, 0);
1283 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001284 if (imm2 != 0) {
1285 args << ", " << "lsl #" << imm2;
1286 }
1287 args << "]";
1288 }
1289 break;
1290 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001291 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001292 ArmRegister Rn(instr, 16);
1293 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001294 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001295 if ((instr & 0x800) != 0) {
1296 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1297 uint32_t P = (instr >> 10) & 1;
1298 uint32_t U = (instr >> 9) & 1;
1299 uint32_t W = (instr >> 8) & 1;
1300 uint32_t imm8 = instr & 0xFF;
1301 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1302 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1303 opcode << "push";
Dave Allison20dfc792014-06-16 20:44:29 -07001304 args << "{" << Rt << "}";
Ian Rogers66a3fca2012-04-09 19:51:34 -07001305 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1306 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001307 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001308 if (P == 1 && U == 1 && W == 0) {
1309 opcode << "strt";
1310 } else {
1311 opcode << "str";
1312 }
1313 args << Rt << ", [" << Rn;
1314 if (P == 0 && W == 1) {
1315 args << "], #" << imm32;
1316 } else {
1317 args << ", #" << imm32 << "]";
1318 if (W == 1) {
1319 args << "!";
1320 }
Ian Rogers40627db2012-03-04 17:31:09 -08001321 }
1322 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001323 } else {
1324 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
Ian Rogers66a3fca2012-04-09 19:51:34 -07001325 ArmRegister Rm(instr, 0);
1326 uint32_t imm2 = (instr >> 4) & 3;
1327 opcode << "str.w";
1328 args << Rt << ", [" << Rn << ", " << Rm;
1329 if (imm2 != 0) {
1330 args << ", lsl #" << imm2;
1331 }
1332 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001333 }
1334 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001335 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001336 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001337 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001338 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001339 }
Ian Rogers40627db2012-03-04 17:31:09 -08001340 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001341 }
1342 }
1343
1344 break;
1345 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001346 case 0x03: case 0x0B: case 0x11: case 0x13: case 0x19: case 0x1B: { // 00xx011
1347 // Load byte/halfword
jeffhaoeae26912013-01-28 16:29:54 -08001348 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1349 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1350 // |---|--|--|---|--|-|----|----|------|------|
1351 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1352 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1353 // |---|--|--|---|--|-|----|----|------|------|
1354 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1355 // |111|11| op2 | | | imm12 |
1356 uint32_t op3 = (instr >> 23) & 3;
1357 ArmRegister Rn(instr, 16);
1358 ArmRegister Rt(instr, 12);
1359 if (Rt.r != 15) {
1360 if (op3 == 1) {
1361 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1362 uint32_t imm12 = instr & 0xFFF;
1363 opcode << "ldrh.w";
1364 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1365 if (Rn.r == 9) {
1366 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001367 Thread::DumpThreadOffset<4>(args, imm12);
jeffhaoeae26912013-01-28 16:29:54 -08001368 } else if (Rn.r == 15) {
1369 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1370 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
Ian Rogersff093b32014-04-30 19:04:27 -07001371 args << StringPrintf(" ; 0x%08x", *reinterpret_cast<int32_t*>(lit_adr));
jeffhaoeae26912013-01-28 16:29:54 -08001372 }
1373 } else if (op3 == 3) {
1374 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001375 // LDRSB.W Rt, [Rn, #imm12] - 111 11 00 11 001 nnnn tttt iiiiiiiiiiii
jeffhaoeae26912013-01-28 16:29:54 -08001376 uint32_t imm12 = instr & 0xFFF;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001377 opcode << (HasBitSet(instr, 20) ? "ldrsb.w" : "ldrsh.w");
jeffhaoeae26912013-01-28 16:29:54 -08001378 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1379 if (Rn.r == 9) {
1380 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001381 Thread::DumpThreadOffset<4>(args, imm12);
jeffhaoeae26912013-01-28 16:29:54 -08001382 } else if (Rn.r == 15) {
1383 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1384 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
Ian Rogersff093b32014-04-30 19:04:27 -07001385 args << StringPrintf(" ; 0x%08x", *reinterpret_cast<int32_t*>(lit_adr));
jeffhaoeae26912013-01-28 16:29:54 -08001386 }
1387 }
1388 }
1389 break;
1390 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001391 case 0x29: { // 0101001
1392 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1393 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1394 // |---|--|-------|----|----|----|--|---|----|
1395 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1396 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1397 // |---|--|-------|----|----|----|--|---|----|
1398 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1399 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1400 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1401 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1402 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1403 if ((instr & 0xf0c0) == 0xf080) {
1404 uint32_t op3 = (instr >> 4) & 3;
1405 opcode << kThumbReverseOperations[op3];
1406 ArmRegister Rm(instr, 0);
1407 ArmRegister Rd(instr, 8);
1408 args << Rd << ", " << Rm;
1409 ArmRegister Rm2(instr, 16);
1410 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1411 args << " (UNPREDICTABLE)";
1412 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001413 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001414 break;
1415 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001416 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001417 // Load word
1418 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1419 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1420 // |---|--|--|---|--|-|----|----|------|------|
1421 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1422 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1423 // |---|--|--|---|--|-|----|----|------|------|
1424 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1425 // |111|11| op2 | | | imm12 |
1426 uint32_t op3 = (instr >> 23) & 3;
1427 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001428 ArmRegister Rn(instr, 16);
1429 ArmRegister Rt(instr, 12);
1430 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001431 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1432 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1433 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001434 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001435 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001436 if (Rn.r == 9) {
1437 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001438 Thread::DumpThreadOffset<4>(args, imm12);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001439 } else if (Rn.r == 15) {
1440 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1441 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
Ian Rogersff093b32014-04-30 19:04:27 -07001442 args << StringPrintf(" ; 0x%08x", *reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001443 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001444 } else if (op4 == 0) {
1445 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1446 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001447 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001448 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001449 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001450 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001451 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001452 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001453 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001454 } else {
Dave Allison20dfc792014-06-16 20:44:29 -07001455 bool p = (instr & (1 << 10)) != 0;
1456 bool w = (instr & (1 << 8)) != 0;
1457 bool u = (instr & (1 << 9)) != 0;
1458 if (p && u && !w) {
1459 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1460 uint32_t imm8 = instr & 0xFF;
1461 opcode << "ldrt";
1462 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
1463 } else if (Rn.r == 13 && !p && u && w && (instr & 0xff) == 4) {
1464 // POP
1465 opcode << "pop";
1466 args << "{" << Rt << "}";
1467 } else {
1468 bool wback = !p || w;
1469 uint32_t offset = (instr & 0xff);
1470 opcode << "ldr.w";
1471 args << Rt << ",";
1472 if (p && !wback) {
1473 args << "[" << Rn << ", #" << offset << "]";
1474 } else if (p && wback) {
1475 args << "[" << Rn << ", #" << offset << "]!";
1476 } else if (!p && wback) {
1477 args << "[" << Rn << "], #" << offset;
1478 } else {
1479 LOG(FATAL) << p << " " << w;
1480 }
1481 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001482 }
1483 break;
1484 }
Dave Allison70202782013-10-22 17:52:19 -07001485 default: // more formats
1486 if ((op2 >> 4) == 2) { // 010xxxx
1487 // data processing (register)
Vladimir Markoc777e0d2014-04-03 17:59:02 +01001488 if ((instr & 0x0080f0f0) == 0x0000f000) {
1489 // LSL, LSR, ASR, ROR
1490 uint32_t shift_op = (instr >> 21) & 3;
1491 uint32_t S = (instr >> 20) & 1;
1492 ArmRegister Rd(instr, 8);
1493 ArmRegister Rn(instr, 16);
1494 ArmRegister Rm(instr, 0);
1495 opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
1496 args << Rd << ", " << Rn << ", " << Rm;
1497 }
Dave Allison70202782013-10-22 17:52:19 -07001498 } else if ((op2 >> 3) == 6) { // 0110xxx
1499 // Multiply, multiply accumulate, and absolute difference
1500 op1 = (instr >> 20) & 0x7;
Ningsheng Jiana262f772014-11-25 16:48:07 +08001501 op2 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -07001502 ArmRegister Ra(instr, 12);
1503 ArmRegister Rn(instr, 16);
1504 ArmRegister Rm(instr, 0);
1505 ArmRegister Rd(instr, 8);
1506 switch (op1) {
1507 case 0:
1508 if (op2 == 0) {
1509 if (Ra.r == 0xf) {
1510 opcode << "mul";
1511 args << Rd << ", " << Rn << ", " << Rm;
1512 } else {
1513 opcode << "mla";
1514 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1515 }
1516 } else {
1517 opcode << "mls";
1518 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1519 }
1520 break;
1521 case 1:
1522 case 2:
1523 case 3:
1524 case 4:
1525 case 5:
1526 case 6:
1527 break; // do these sometime
1528 }
1529 } else if ((op2 >> 3) == 7) { // 0111xxx
1530 // Long multiply, long multiply accumulate, and divide
1531 op1 = (instr >> 20) & 0x7;
1532 op2 = (instr >> 4) & 0xf;
1533 ArmRegister Rn(instr, 16);
1534 ArmRegister Rm(instr, 0);
1535 ArmRegister Rd(instr, 8);
1536 ArmRegister RdHi(instr, 8);
1537 ArmRegister RdLo(instr, 12);
1538 switch (op1) {
1539 case 0:
1540 opcode << "smull";
1541 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1542 break;
1543 case 1:
1544 opcode << "sdiv";
1545 args << Rd << ", " << Rn << ", " << Rm;
1546 break;
1547 case 2:
1548 opcode << "umull";
1549 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1550 break;
1551 case 3:
1552 opcode << "udiv";
1553 args << Rd << ", " << Rn << ", " << Rm;
1554 break;
1555 case 4:
1556 case 5:
1557 case 6:
1558 break; // TODO: when we generate these...
1559 }
1560 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001561 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001562 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001563 default:
1564 break;
1565 }
Ian Rogers9af89402012-09-07 11:29:35 -07001566
1567 // Apply any IT-block conditions to the opcode if necessary.
1568 if (!it_conditions_.empty()) {
1569 opcode << it_conditions_.back();
1570 it_conditions_.pop_back();
1571 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001572 if (opcode.str().size() == 0) {
1573 opcode << "UNKNOWN " << op2;
1574 }
Ian Rogers9af89402012-09-07 11:29:35 -07001575
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001576 os << FormatInstructionPointer(instr_ptr)
1577 << StringPrintf(": %08x\t%-7s ", instr, opcode.str().c_str())
1578 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001579 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001580} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001581
1582size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1583 uint16_t instr = ReadU16(instr_ptr);
1584 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1585 if (is_32bit) {
1586 return DumpThumb32(os, instr_ptr);
1587 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001588 std::ostringstream opcode;
1589 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001590 uint16_t opcode1 = instr >> 10;
1591 if (opcode1 < 0x10) {
1592 // shift (immediate), add, subtract, move, and compare
1593 uint16_t opcode2 = instr >> 9;
1594 switch (opcode2) {
1595 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1596 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001597 // Logical shift left - 00 000xx iii mmm ddd
1598 // Logical shift right - 00 001xx iii mmm ddd
1599 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001600 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001601 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001602 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001603 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001604 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001605 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001606 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001607 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001608 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001609 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001610 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001611 break;
1612 }
1613 case 0xC: case 0xD: case 0xE: case 0xF: {
1614 // Add register - 00 01100 mmm nnn ddd
1615 // Sub register - 00 01101 mmm nnn ddd
1616 // Add 3-bit immediate - 00 01110 iii nnn ddd
1617 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1618 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001619 ThumbRegister Rn(instr, 3);
1620 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001621 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001622 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001623 } else {
1624 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001625 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001626 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001627 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001628 }
1629 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001630 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001631 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001632 ArmRegister Rm(imm3_or_Rm);
1633 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001634 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001635 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001636 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001637 break;
1638 }
1639 case 0x10: case 0x11: case 0x12: case 0x13:
1640 case 0x14: case 0x15: case 0x16: case 0x17:
1641 case 0x18: case 0x19: case 0x1A: case 0x1B:
1642 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1643 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1644 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1645 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1646 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001647 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 uint16_t imm8 = instr & 0xFF;
1649 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001650 case 4: opcode << "movs"; break;
1651 case 5: opcode << "cmp"; break;
1652 case 6: opcode << "adds"; break;
1653 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001655 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001656 break;
1657 }
1658 default:
1659 break;
1660 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001661 } else if (opcode1 == 0x10) {
1662 // Data-processing
1663 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001664 ThumbRegister rm(instr, 3);
1665 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001666 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001667 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001668 } else if (opcode1 == 0x11) {
1669 // Special data instructions and branch and exchange
1670 uint16_t opcode2 = (instr >> 6) & 0x0F;
1671 switch (opcode2) {
1672 case 0x0: case 0x1: case 0x2: case 0x3: {
1673 // Add low registers - 010001 0000 xxxxxx
1674 // Add high registers - 010001 0001/001x xxxxxx
1675 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001676 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001677 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001678 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001679 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001680 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001681 break;
1682 }
1683 case 0x8: case 0x9: case 0xA: case 0xB: {
1684 // Move low registers - 010001 1000 xxxxxx
1685 // Move high registers - 010001 1001/101x xxxxxx
1686 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001687 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001688 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001689 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001690 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001691 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001692 break;
1693 }
1694 case 0x5: case 0x6: case 0x7: {
1695 // Compare high registers - 010001 0101/011x xxxxxx
1696 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001697 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001698 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001699 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001700 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001701 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001702 break;
1703 }
1704 case 0xC: case 0xD: case 0xE: case 0xF: {
1705 // Branch and exchange - 010001 110x xxxxxx
1706 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001707 ArmRegister rm(instr, 3);
1708 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1709 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001710 break;
1711 }
1712 default:
1713 break;
1714 }
jeffhaoeae26912013-01-28 16:29:54 -08001715 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1716 ThumbRegister Rt(instr, 8);
1717 uint16_t imm8 = instr & 0xFF;
1718 opcode << "ldr";
1719 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001720 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1721 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1722 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1723 // Load/store single data item
1724 uint16_t opA = (instr >> 12) & 0xF;
1725 if (opA == 0x5) {
1726 uint16_t opB = (instr >> 9) & 0x7;
1727 ThumbRegister Rm(instr, 6);
1728 ThumbRegister Rn(instr, 3);
1729 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001730 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001731 case 0: opcode << "str"; break;
1732 case 1: opcode << "strh"; break;
1733 case 2: opcode << "strb"; break;
1734 case 3: opcode << "ldrsb"; break;
1735 case 4: opcode << "ldr"; break;
1736 case 5: opcode << "ldrh"; break;
1737 case 6: opcode << "ldrb"; break;
1738 case 7: opcode << "ldrsh"; break;
1739 }
1740 args << Rt << ", [" << Rn << ", " << Rm << "]";
1741 } else if (opA == 9) {
1742 uint16_t opB = (instr >> 11) & 1;
1743 ThumbRegister Rt(instr, 8);
1744 uint16_t imm8 = instr & 0xFF;
1745 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001746 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001747 } else {
1748 uint16_t imm5 = (instr >> 6) & 0x1F;
1749 uint16_t opB = (instr >> 11) & 1;
1750 ThumbRegister Rn(instr, 3);
1751 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001752 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001753 case 6:
1754 imm5 <<= 2;
1755 opcode << (opB == 0 ? "str" : "ldr");
1756 break;
1757 case 7:
1758 imm5 <<= 0;
1759 opcode << (opB == 0 ? "strb" : "ldrb");
1760 break;
1761 case 8:
1762 imm5 <<= 1;
1763 opcode << (opB == 0 ? "strh" : "ldrh");
1764 break;
1765 }
1766 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1767 }
jeffhaoeae26912013-01-28 16:29:54 -08001768 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001769 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001770 uint32_t cond = (instr >> 8) & 0xF;
1771 opcode << "b";
1772 DumpCond(opcode, cond);
1773 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001774 } else if ((instr & 0xF800) == 0xA800) {
1775 // Generate SP-relative address
1776 ThumbRegister rd(instr, 8);
1777 int imm8 = instr & 0xFF;
1778 opcode << "add";
1779 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001780 } else if ((instr & 0xF000) == 0xB000) {
1781 // Miscellaneous 16-bit instructions
1782 uint16_t opcode2 = (instr >> 5) & 0x7F;
1783 switch (opcode2) {
1784 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1785 // Add immediate to SP - 1011 00000 ii iiiii
1786 // Subtract immediate from SP - 1011 00001 ii iiiii
1787 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001788 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001789 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001790 break;
1791 }
Ian Rogers087b2412012-03-21 01:30:32 -07001792 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001793 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001794 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1795 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001796 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001797 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1798 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1799 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001800 // CBNZ, CBZ
1801 uint16_t op = (instr >> 11) & 1;
1802 uint16_t i = (instr >> 9) & 1;
1803 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001804 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001805 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001806 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001807 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001808 DumpBranchTarget(args, instr_ptr + 4, imm32);
1809 break;
1810 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001811 case 0x50: case 0x51: // 101000x
1812 case 0x52: case 0x53: // 101001x
1813 case 0x56: case 0x57: { // 101011x
1814 uint16_t op = (instr >> 6) & 3;
1815 opcode << kThumbReverseOperations[op];
1816 ThumbRegister Rm(instr, 3);
1817 ThumbRegister Rd(instr, 0);
1818 args << Rd << ", " << Rm;
1819 break;
1820 }
Ian Rogers40627db2012-03-04 17:31:09 -08001821 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1822 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1823 // If-Then, and hints
1824 uint16_t opA = (instr >> 4) & 0xF;
1825 uint16_t opB = instr & 0xF;
1826 if (opB == 0) {
1827 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001828 case 0: opcode << "nop"; break;
1829 case 1: opcode << "yield"; break;
1830 case 2: opcode << "wfe"; break;
1831 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001832 default: break;
1833 }
1834 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001835 uint32_t first_cond = opA;
1836 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001837 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001838
1839 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1840 // and store up the actual condition codes we'll want to add to the next few opcodes.
1841 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001842 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001843 for (size_t i = 0; i < count; ++i) {
1844 bool positive_cond = ((first_cond & 1) != 0);
1845 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1846 if (positive_mask == positive_cond) {
1847 opcode << 't';
1848 it_conditions_[i] = kConditionCodeNames[first_cond];
1849 } else {
1850 opcode << 'e';
1851 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1852 }
1853 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001854 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001855
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001856 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1857 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001858 }
1859 break;
1860 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001861 default:
1862 break;
1863 }
1864 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1865 ((instr & 0xE000) == 0x8000)) {
1866 // Load/store single data item
1867 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001868 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001869 switch (opA) {
1870 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001871 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1872 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001873 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001874 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001875 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001876 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1877 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001878 break;
1879 }
1880 case 0x9: {
1881 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1882 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1883 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001884 ThumbRegister Rt(instr, 8);
1885 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1886 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001887 break;
1888 }
1889 default:
1890 break;
1891 }
Ian Rogers40627db2012-03-04 17:31:09 -08001892 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1893 uint16_t imm11 = instr & 0x7FFF;
1894 int32_t imm32 = imm11 << 1;
1895 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001896 opcode << "b";
1897 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001898 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001899
1900 // Apply any IT-block conditions to the opcode if necessary.
1901 if (!it_conditions_.empty()) {
1902 opcode << it_conditions_.back();
1903 it_conditions_.pop_back();
1904 }
1905
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001906 os << FormatInstructionPointer(instr_ptr)
1907 << StringPrintf(": %04x \t%-7s ", instr, opcode.str().c_str())
1908 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001909 }
1910 return 2;
1911}
1912
1913} // namespace arm
1914} // namespace art