blob: 03fac18850fe45a9097fcde60315cf78d7bdb865 [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
Vladimir Marko55d7c182015-01-05 15:17:01 +000024#include "arch/arm/registers_arm.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080026#include "base/stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070027#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070028
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080029namespace art {
30namespace arm {
31
Ian Rogersb23a7722012-10-09 16:54:26 -070032size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
33 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
34 DumpArm(os, begin);
35 return 4;
36 } else {
37 // remove thumb specifier bits
38 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
39 return DumpThumb16(os, begin);
40 }
41}
42
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080043void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
44 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
45 for (const uint8_t* cur = begin; cur < end; cur += 4) {
46 DumpArm(os, cur);
47 }
48 } else {
49 // remove thumb specifier bits
50 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
51 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
52 for (const uint8_t* cur = begin; cur < end;) {
53 cur += DumpThumb16(os, cur);
54 }
55 }
56}
57
Elliott Hughes77405792012-03-15 15:22:12 -070058static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070059 "eq", // 0000 - equal
60 "ne", // 0001 - not-equal
61 "cs", // 0010 - carry-set, greater than, equal or unordered
62 "cc", // 0011 - carry-clear, less than
63 "mi", // 0100 - minus, negative
64 "pl", // 0101 - plus, positive or zero
65 "vs", // 0110 - overflow
66 "vc", // 0111 - no overflow
67 "hi", // 1000 - unsigned higher
68 "ls", // 1001 - unsigned lower or same
69 "ge", // 1010 - signed greater than or equal
70 "lt", // 1011 - signed less than
71 "gt", // 1100 - signed greater than
72 "le", // 1101 - signed less than or equal
73 "", // 1110 - always
74 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080075};
76
77void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
78 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070079 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080080 } else {
81 os << "Unexpected condition: " << cond;
82 }
83}
84
Ian Rogersb122a4b2013-11-19 18:00:50 -080085void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
86 switch (domain) {
Andreas Gampec8ccf682014-09-29 20:07:43 -070087 case 15U /* 0b1111 */: os << "sy"; break;
88 case 14U /* 0b1110 */: os << "st"; break;
89 case 11U /* 0b1011 */: os << "ish"; break;
90 case 10U /* 0b1010 */: os << "ishst"; break;
91 case 7U /* 0b0111 */: os << "nsh"; break;
92 case 6U /* 0b0110 */: os << "nshst"; break;
93 case 3U /* 0b0011 */: os << "osh"; break;
94 case 2U /* 0b0010 */: os << "oshst"; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -080095 }
96}
97
Ian Rogers40627db2012-03-04 17:31:09 -080098void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070099 os << StringPrintf("%+d (", imm32) << FormatInstructionPointer(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800100}
101
102static uint32_t ReadU16(const uint8_t* ptr) {
103 return ptr[0] | (ptr[1] << 8);
104}
105
106static uint32_t ReadU32(const uint8_t* ptr) {
107 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
108}
109
Elliott Hughes77405792012-03-15 15:22:12 -0700110static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700111 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
112 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700113};
114
Ian Rogersad03ef52012-03-18 19:34:47 -0700115static const char* kThumbDataProcessingOperations[] = {
116 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
117 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
118};
119
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100120static const char* const kThumb2ShiftOperations[] = {
121 "lsl", "lsr", "asr", "ror"
122};
123
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100124static const char* kThumbReverseOperations[] = {
125 "rev", "rev16", "rbit", "revsh"
126};
127
Elliott Hughes77405792012-03-15 15:22:12 -0700128struct ArmRegister {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800129 explicit ArmRegister(uint32_t r_in) : r(r_in) { CHECK_LE(r_in, 15U); }
130 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) {
131 CHECK_LE(r, 15U);
132 }
Elliott Hughes77405792012-03-15 15:22:12 -0700133 uint32_t r;
134};
135std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
136 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700137 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700138 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700139 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700140 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700141 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700142 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700143 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700144 }
145 return os;
146}
147
Elliott Hughes630e77d2012-03-22 19:20:56 -0700148struct ThumbRegister : ArmRegister {
149 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700150};
151
Vladimir Marko55d7c182015-01-05 15:17:01 +0000152struct RmLslImm2 {
153 explicit RmLslImm2(uint32_t instr) : imm2((instr >> 4) & 0x3), rm(instr & 0xf) {}
154 uint32_t imm2;
Elliott Hughes77405792012-03-15 15:22:12 -0700155 ArmRegister rm;
156};
Vladimir Marko55d7c182015-01-05 15:17:01 +0000157std::ostream& operator<<(std::ostream& os, const RmLslImm2& r) {
Elliott Hughes77405792012-03-15 15:22:12 -0700158 os << r.rm;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000159 if (r.imm2 != 0) {
160 os << ", lsl #" << r.imm2;
Elliott Hughes77405792012-03-15 15:22:12 -0700161 }
162 return os;
163}
164
Elliott Hughes1ca98492012-04-12 17:21:02 -0700165struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700166 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700167 uint32_t rotate = ((instruction >> 8) & 0xf);
168 uint32_t imm = (instruction & 0xff);
169 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
170 }
171 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700172};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700173std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700174 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700175 return os;
176}
177
178struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700179 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700180 uint32_t register_list;
181};
182std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
183 if (rhs.register_list == 0) {
184 os << "<no register list?>";
185 return os;
186 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700187 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700188 bool first = true;
189 for (size_t i = 0; i < 16; i++) {
190 if ((rhs.register_list & (1 << i)) != 0) {
191 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700192 first = false;
193 } else {
194 os << ", ";
195 }
196 os << ArmRegister(i);
197 }
198 }
199 os << "}";
200 return os;
201}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800202
Vladimir Markodd577a32013-11-07 19:25:24 +0000203struct FpRegister {
Roland Levillain3887c462015-08-12 18:15:42 +0100204 FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
Vladimir Markodd577a32013-11-07 19:25:24 +0000205 size = (instr >> 8) & 1;
206 uint32_t Vn = (instr >> at_bit) & 0xF;
207 uint32_t N = (instr >> extra_at_bit) & 1;
208 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
209 }
Roland Levillain3887c462015-08-12 18:15:42 +0100210 FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit, uint32_t forced_size) {
Zheng Xue19649a2014-02-27 13:30:55 +0000211 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;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000400 return (bit_a << 63) | ((UINT64_C(1) << 62) - (bit_b << 54)) | (slice << 48);
401}
402
403enum T2LitType {
404 kT2LitInvalid,
405 kT2LitUByte,
406 kT2LitSByte,
407 kT2LitUHalf,
408 kT2LitSHalf,
409 kT2LitUWord,
410 kT2LitSWord,
411 kT2LitHexWord,
412 kT2LitULong,
413 kT2LitSLong,
414 kT2LitHexLong,
415};
416std::ostream& operator<<(std::ostream& os, T2LitType type) {
417 return os << static_cast<int>(type);
418}
419
420void DumpThumb2Literal(std::ostream& args, const uint8_t* instr_ptr, uint32_t U, uint32_t imm32,
421 T2LitType type) {
422 // Literal offsets (imm32) are not required to be aligned so we may need unaligned access.
423 typedef const int16_t unaligned_int16_t __attribute__ ((aligned (1)));
424 typedef const uint16_t unaligned_uint16_t __attribute__ ((aligned (1)));
425 typedef const int32_t unaligned_int32_t __attribute__ ((aligned (1)));
426 typedef const uint32_t unaligned_uint32_t __attribute__ ((aligned (1)));
427 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (1)));
428 typedef const uint64_t unaligned_uint64_t __attribute__ ((aligned (1)));
429
430 uintptr_t pc = RoundDown(reinterpret_cast<intptr_t>(instr_ptr) + 4, 4);
431 uintptr_t lit_adr = U ? pc + imm32 : pc - imm32;
432 args << " ; ";
433 switch (type) {
434 case kT2LitUByte:
435 args << *reinterpret_cast<const uint8_t*>(lit_adr);
436 break;
437 case kT2LitSByte:
438 args << *reinterpret_cast<const int8_t*>(lit_adr);
439 break;
440 case kT2LitUHalf:
441 args << *reinterpret_cast<const unaligned_uint16_t*>(lit_adr);
442 break;
443 case kT2LitSHalf:
444 args << *reinterpret_cast<const unaligned_int16_t*>(lit_adr);
445 break;
446 case kT2LitUWord:
447 args << *reinterpret_cast<const unaligned_uint32_t*>(lit_adr);
448 break;
449 case kT2LitSWord:
450 args << *reinterpret_cast<const unaligned_int32_t*>(lit_adr);
451 break;
452 case kT2LitHexWord:
453 args << StringPrintf("0x%08x", *reinterpret_cast<const unaligned_uint32_t*>(lit_adr));
454 break;
455 case kT2LitULong:
456 args << *reinterpret_cast<const unaligned_uint64_t*>(lit_adr);
457 break;
458 case kT2LitSLong:
459 args << *reinterpret_cast<const unaligned_int64_t*>(lit_adr);
460 break;
461 case kT2LitHexLong:
462 args << StringPrintf("0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
463 break;
464 default:
465 LOG(FATAL) << "Invalid type: " << type;
466 break;
467 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100468}
469
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800470size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
471 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
472 // |111|1 1|1000000|0000|1111110000000000|
473 // |5 3|2 1|0987654|3 0|5 0 5 0|
474 // |---|---|-------|----|----------------|
475 // |332|2 2|2222222|1111|1111110000000000|
476 // |1 9|8 7|6543210|9 6|5 0 5 0|
477 // |---|---|-------|----|----------------|
478 // |111|op1| op2 | | |
479 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700480 if (op1 == 0) {
481 return DumpThumb16(os, instr_ptr);
482 }
483
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700485 std::ostringstream opcode;
486 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800487 switch (op1) {
488 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800489 break;
490 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700491 if ((op2 & 0x64) == 0) { // 00x x0xx
492 // |111|11|10|00|0|00|0000|1111110000000000|
493 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
494 // |---|--|--|--|-|--|----|----------------|
495 // |332|22|22|22|2|22|1111|1111110000000000|
496 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
497 // |---|--|--|--|-|--|----|----------------|
498 // |111|01|00|op|0|WL| Rn | |
499 // |111|01| op2 | | |
500 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
501 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
502 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
503 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
504 uint32_t op = (instr >> 23) & 3;
505 uint32_t W = (instr >> 21) & 1;
506 uint32_t L = (instr >> 20) & 1;
507 ArmRegister Rn(instr, 16);
508 if (op == 1 || op == 2) {
509 if (op == 1) {
510 if (L == 0) {
511 opcode << "stm";
512 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700514 if (Rn.r != 13) {
515 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700516 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700517 } else {
518 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800519 }
520 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700521 } else {
522 if (L == 0) {
523 if (Rn.r != 13) {
524 opcode << "stmdb";
525 args << Rn << (W == 0 ? "" : "!") << ", ";
526 } else {
527 opcode << "push";
528 }
529 } else {
530 opcode << "ldmdb";
531 args << Rn << (W == 0 ? "" : "!") << ", ";
532 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700534 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700536 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700537 uint32_t op3 = (instr >> 23) & 3;
538 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700539 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700540 ArmRegister Rn(instr, 16);
541 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700542 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700543 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700544 if ((op3 & 2) == 2) { // 1x
545 int W = (instr >> 21) & 1;
546 int U = (instr >> 23) & 1;
547 int P = (instr >> 24) & 1;
548
549 if ((op4 & 1) == 1) {
550 opcode << "ldrd";
551 } else {
552 opcode << "strd";
553 }
554 args << Rt << "," << Rd << ", [" << Rn;
555 const char *sign = U ? "+" : "-";
556 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000557 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700558 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000559 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700560 if (W == 1) {
561 args << "!";
562 }
563 }
564 } else { // 0x
565 switch (op4) {
566 case 0:
567 if (op3 == 0) { // op3 is 00, op4 is 00
568 opcode << "strex";
569 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000570 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
571 Rd.r == Rn.r || Rd.r == Rt.r) {
572 args << " (UNPREDICTABLE)";
573 }
Dave Allison70202782013-10-22 17:52:19 -0700574 } else { // op3 is 01, op4 is 00
575 // this is one of strexb, strexh or strexd
576 int op5 = (instr >> 4) & 0xf;
577 switch (op5) {
578 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700579 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000580 opcode << ((op5 == 4) ? "strexb" : "strexh");
581 Rd = ArmRegister(instr, 0);
582 args << Rd << ", " << Rt << ", [" << Rn << "]";
583 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
584 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
585 args << " (UNPREDICTABLE)";
586 }
Dave Allison70202782013-10-22 17:52:19 -0700587 break;
588 case 7:
589 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000590 ArmRegister Rt2 = Rd;
591 Rd = ArmRegister(instr, 0);
592 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
593 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
594 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
595 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
596 args << " (UNPREDICTABLE)";
597 }
Dave Allison70202782013-10-22 17:52:19 -0700598 break;
599 }
600 }
601 break;
602 case 1:
603 if (op3 == 0) { // op3 is 00, op4 is 01
604 opcode << "ldrex";
605 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000606 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
607 args << " (UNPREDICTABLE)";
608 }
Dave Allison70202782013-10-22 17:52:19 -0700609 } else { // op3 is 01, op4 is 01
610 // this is one of strexb, strexh or strexd
611 int op5 = (instr >> 4) & 0xf;
612 switch (op5) {
613 case 0:
614 opcode << "tbb";
615 break;
616 case 1:
617 opcode << "tbh";
618 break;
619 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700620 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000621 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
622 args << Rt << ", [" << Rn << "]";
623 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
624 args << " (UNPREDICTABLE)";
625 }
Dave Allison70202782013-10-22 17:52:19 -0700626 break;
627 case 7:
628 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000629 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
630 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
631 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
632 args << " (UNPREDICTABLE)";
633 }
Dave Allison70202782013-10-22 17:52:19 -0700634 break;
635 }
636 }
637 break;
638 case 2: // op3 is 0x, op4 is 10
639 case 3: // op3 is 0x, op4 is 11
640 if (op4 == 2) {
641 opcode << "strd";
642 } else {
643 opcode << "ldrd";
644 }
645 int W = (instr >> 21) & 1;
646 int U = (instr >> 23) & 1;
647 int P = (instr >> 24) & 1;
648
649 args << Rt << "," << Rd << ", [" << Rn;
650 const char *sign = U ? "+" : "-";
651 if (P == 0 && W == 1) {
652 args << "], #" << sign << imm8;
653 } else {
654 args << ", #" << sign << imm8 << "]";
655 if (W == 1) {
656 args << "!";
657 }
658 }
659 break;
660 }
661 }
662
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700663 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
664 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100665 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
666 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
667 // |---|----|----|-|----|----|----|--|--|----|
668 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
669 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
670 // |---|----|----|-|----|----|----|--|--|----|
671 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700672 uint32_t op3 = (instr >> 21) & 0xF;
673 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100674 uint32_t imm3 = ((instr >> 12) & 0x7);
675 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700676 uint32_t imm5 = ((imm3 << 2) | imm2);
677 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700678 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100679 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700680 ArmRegister Rm(instr, 0);
681 switch (op3) {
682 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100683 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700684 opcode << "and";
685 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700686 if (S != 1U) {
687 opcode << "UNKNOWN TST-" << S;
688 break;
689 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700690 opcode << "tst";
691 S = 0; // don't print 's'
692 }
693 break;
694 case 0x1: opcode << "bic"; break;
695 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100696 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700697 opcode << "orr";
698 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100699 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700700 opcode << "mov";
701 }
702 break;
703 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100704 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700705 opcode << "orn";
706 } else {
707 opcode << "mvn";
708 }
709 break;
710 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100711 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700712 opcode << "eor";
713 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700714 if (S != 1U) {
715 opcode << "UNKNOWN TEQ-" << S;
716 break;
717 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700718 opcode << "teq";
719 S = 0; // don't print 's'
720 }
721 break;
722 case 0x6: opcode << "pkh"; break;
723 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100724 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700725 opcode << "add";
726 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700727 if (S != 1U) {
728 opcode << "UNKNOWN CMN-" << S;
729 break;
730 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700731 opcode << "cmn";
732 S = 0; // don't print 's'
733 }
734 break;
735 case 0xA: opcode << "adc"; break;
736 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100737 case 0xD:
738 if (Rd.r != 0xF) {
739 opcode << "sub";
740 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700741 if (S != 1U) {
742 opcode << "UNKNOWN CMP-" << S;
743 break;
744 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100745 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100746 S = 0; // don't print 's'
747 }
748 break;
749 case 0xE: opcode << "rsb"; break;
750 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700751 }
Ian Rogers087b2412012-03-21 01:30:32 -0700752
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700753 if (S == 1) {
754 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700755 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700756 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100757
758 if (Rd.r != 0xF) {
759 args << Rd << ", ";
760 }
761 if (Rn.r != 0xF) {
762 args << Rn << ", ";
763 }
764 args << Rm;
765
766 // Shift operand.
767 bool noShift = (imm5 == 0 && shift_type != 0x3);
768 if (!noShift) {
769 args << ", ";
770 switch (shift_type) {
771 case 0x0: args << "lsl"; break;
772 case 0x1: args << "lsr"; break;
773 case 0x2: args << "asr"; break;
774 case 0x3:
775 if (imm5 == 0) {
776 args << "rrx";
777 } else {
778 args << "ror";
779 }
780 break;
781 }
782 if (shift_type != 0x3 /* rrx */) {
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700783 args << StringPrintf(" #%d", (0 != imm5 || 0 == shift_type) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100784 }
785 }
786
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700787 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
788 // Co-processor instructions
789 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
790 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
791 // |---|-|--|------|----|----|----|---|---|----|
792 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
793 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
794 // |---|-|--|------|----|----|----|---|---|----|
795 // |111| |11| op3 | Rn | |copr| |op4| |
796 uint32_t op3 = (instr >> 20) & 0x3F;
797 uint32_t coproc = (instr >> 8) & 0xF;
798 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700799
Ian Rogersef6a7762013-12-19 17:58:05 -0800800 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000801 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
802 // Extension register load/store instructions
803 // |1111|110|00000|0000|1111|110|0|00000000|
804 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
805 // |----|---|-----|----|----|---|-|--------|
806 // |3322|222|22222|1111|1111|110|0|00000000|
807 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
808 // |----|---|-----|----|----|---|-|--------|
809 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700810 uint32_t P = (instr >> 24) & 1;
811 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700812 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000813 if (P == U && W == 1) {
814 opcode << "UNDEFINED";
815 } else {
816 uint32_t L = (instr >> 20) & 1;
817 uint32_t S = (instr >> 8) & 1;
818 ArmRegister Rn(instr, 16);
819 if (P == 1 && W == 0) { // VLDR
820 FpRegister d(instr, 12, 22);
821 uint32_t imm8 = instr & 0xFF;
822 opcode << (L == 1 ? "vldr" : "vstr");
823 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
824 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800825 if (Rn.r == 15 && U == 1) {
Vladimir Marko55d7c182015-01-05 15:17:01 +0000826 DumpThumb2Literal(args, instr_ptr, U, imm8 << 2, kT2LitHexLong);
Ian Rogersef6a7762013-12-19 17:58:05 -0800827 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000828 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
829 opcode << (L == 1 ? "vpop" : "vpush");
830 args << FpRegisterRange(instr);
831 } else { // VLDM
832 opcode << (L == 1 ? "vldm" : "vstm");
833 args << Rn << ((W == 1) ? "!" : "") << ", "
834 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700835 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000836 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700837 }
Dave Allison70202782013-10-22 17:52:19 -0700838 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000839 if ((instr & 0xD0) == 0x10) {
840 // 64bit transfers between ARM core and extension registers.
841 uint32_t L = (instr >> 20) & 1;
842 uint32_t S = (instr >> 8) & 1;
843 ArmRegister Rt2(instr, 16);
844 ArmRegister Rt(instr, 12);
845 FpRegister m(instr, 0, 5);
846 opcode << "vmov" << (S ? ".f64" : ".f32");
847 if (L == 1) {
848 args << Rt << ", " << Rt2 << ", ";
849 }
850 if (S) {
851 args << m;
852 } else {
853 args << m << ", " << FpRegister(m, 1);
854 }
855 if (L == 0) {
856 args << ", " << Rt << ", " << Rt2;
857 }
858 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
859 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
860 args << " (UNPREDICTABLE)";
861 }
862 }
Dave Allison70202782013-10-22 17:52:19 -0700863 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
864 // fp data processing
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100865 // VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
866 // |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
867 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
868 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
869 // |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
870 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
871 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
872 // |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
873 // |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
874 // |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
875 // |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
876 // |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
877 // |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
878 // |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
879 // |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
880 // |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
881 // |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
882 uint32_t S = (instr >> 8) & 1;
883 uint32_t Q = (instr >> 6) & 1;
884 FpRegister d(instr, 12, 22);
885 FpRegister n(instr, 16, 7);
886 FpRegister m(instr, 0, 5);
Zheng Xue19649a2014-02-27 13:30:55 +0000887 if ((op3 & 0xB) == 0) { // 100x00
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100888 opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
Zheng Xue19649a2014-02-27 13:30:55 +0000889 args << d << ", " << n << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100890 } else if ((op3 & 0xB) == 0x2) { // 100x10
891 opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
892 args << d << ", " << n << ", " << m;
893 } else if ((op3 & 0xB) == 0x3) { // 100x11
894 opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
895 args << d << ", " << n << ", " << m;
896 } else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
897 opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
898 args << d << ", " << n << ", " << m;
899 } else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
900 uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
901 opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
902 args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
903 : StringPrintf("0x%08x", VFPExpand32(imm8)));
904 if ((instr & 0xa0) != 0) {
905 args << " (UNPREDICTABLE)";
906 }
907 } else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
908 // VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
909 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
910 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
911 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
912 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
913 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
914 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
915 // |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
916 // |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
917 // |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
918 // |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
919 // |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
920 // |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
921 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
922 uint32_t op5 = (instr >> 16) & 0xF;
923 uint32_t op = (instr >> 7) & 1;
924 // Register types in VCVT instructions rely on the combination of op5 and S.
925 FpRegister Dd(instr, 12, 22, 1);
926 FpRegister Sd(instr, 12, 22, 0);
927 FpRegister Dm(instr, 0, 5, 1);
928 FpRegister Sm(instr, 0, 5, 0);
929 if (op5 == 0) {
930 opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
931 args << d << ", " << m;
932 } else if (op5 == 1) {
933 opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
934 args << d << ", " << m;
935 } else if (op5 == 4) {
936 opcode << "vcmp" << (S != 0 ? ".f64" : ".f32");
937 args << d << ", " << m;
938 if (op != 0) {
939 args << " (quiet nan)";
940 }
941 } else if (op5 == 5) {
942 opcode << "vcmpe" << (S != 0 ? ".f64" : ".f32");
943 args << d << ", #0.0";
944 if (op != 0) {
945 args << " (quiet nan)";
946 }
947 if ((instr & 0x2f) != 0) {
948 args << " (UNPREDICTABLE)";
949 }
950 } else if (op5 == 0xD) {
951 if (S == 1) {
952 // vcvt{r}.s32.f64
953 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
954 args << Sd << ", " << Dm;
955 } else {
956 // vcvt{r}.s32.f32
957 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
958 args << Sd << ", " << Sm;
959 }
960 } else if (op5 == 0xC) {
961 if (S == 1) {
962 // vcvt{r}.u32.f64
963 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
964 args << Sd << ", " << Dm;
965 } else {
966 // vcvt{r}.u32.f32
967 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
968 args << Sd << ", " << Sm;
969 }
970 } else if (op5 == 0x8) {
971 if (S == 1) {
972 // vcvt.f64.<Tm>
973 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
974 args << Dd << ", " << Sm;
975 } else {
976 // vcvt.f32.<Tm>
977 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
978 args << Sd << ", " << Sm;
979 }
980 } else if (op5 == 0x7) {
981 if (op == 1) {
Zheng Xue19649a2014-02-27 13:30:55 +0000982 if (S == 1) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100983 // vcvt.f64.f32
984 opcode << "vcvt.f64.f32";
Zheng Xue19649a2014-02-27 13:30:55 +0000985 args << Dd << ", " << Sm;
986 } else {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100987 // vcvt.f32.f64
988 opcode << "vcvt.f32.f64";
989 args << Sd << ", " << Dm;
Zheng Xue19649a2014-02-27 13:30:55 +0000990 }
991 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100992 } else if ((op5 & 0xa) == 0xa) {
993 opcode << "vcvt";
994 args << "[undecoded: floating <-> fixed]";
Zheng Xue19649a2014-02-27 13:30:55 +0000995 }
996 }
Dave Allison70202782013-10-22 17:52:19 -0700997 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000998 if (coproc == 10 && (op3 & 0xE) == 0) {
999 // VMOV (between ARM core register and single-precision register)
1000 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
1001 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
1002 // |----|----|---|- |----|----|----|-|--|-|----|
1003 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
1004 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
1005 // |----|----|---|- |----|----|----|-|--|-|----|
1006 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
1007 uint32_t op = op3 & 1;
1008 ArmRegister Rt(instr, 12);
1009 FpRegister n(instr, 16, 7);
1010 opcode << "vmov.f32";
1011 if (op) {
1012 args << Rt << ", " << n;
1013 } else {
1014 args << n << ", " << Rt;
1015 }
1016 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
1017 args << " (UNPREDICTABLE)";
1018 }
1019 } else if (coproc == 10 && op3 == 0x2F) {
1020 // VMRS
1021 // |1111|11000000|0000|1111|1100|000|0|0000|
1022 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
1023 // |----|--------|----|----|----|---|-|----|
1024 // |3322|22222222|1111|1111|1100|000|0|0000|
1025 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
1026 // |----|--------|----|----|----|---|-|----|
1027 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
1028 uint32_t spec_reg = (instr >> 16) & 0xF;
1029 ArmRegister Rt(instr, 12);
1030 opcode << "vmrs";
1031 if (spec_reg == 1) {
1032 if (Rt.r == 15) {
1033 args << "APSR_nzcv, FPSCR";
1034 } else if (Rt.r == 13) {
1035 args << Rt << ", FPSCR (UNPREDICTABLE)";
1036 } else {
1037 args << Rt << ", FPSCR";
1038 }
1039 } else {
1040 args << "(PRIVILEGED)";
1041 }
1042 } else if (coproc == 11 && (op3 & 0x9) != 8) {
1043 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
1044 }
Ian Rogers9af89402012-09-07 11:29:35 -07001045 }
Dave Allison70202782013-10-22 17:52:19 -07001046 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001047 }
1048 break;
Ian Rogers40627db2012-03-04 17:31:09 -08001049 case 2:
1050 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
1051 // Data-processing (modified immediate)
1052 // |111|11|10|0000|0|0000|1|111|1100|00000000|
1053 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
1054 // |---|--|--|----|-|----|-|---|----|--------|
1055 // |332|22|22|2222|2|1111|1|111|1100|00000000|
1056 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
1057 // |---|--|--|----|-|----|-|---|----|--------|
1058 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
1059 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -08001060 uint32_t i = (instr >> 26) & 1;
1061 uint32_t op3 = (instr >> 21) & 0xF;
1062 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001063 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -08001064 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001065 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001066 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001067 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
1068 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
1069 if (op3 == 0x2) {
1070 opcode << "mov";
1071 if (S == 1) {
1072 opcode << "s";
1073 }
1074 opcode << ".w";
1075 } else {
1076 opcode << "mvn";
1077 if (S == 1) {
1078 opcode << "s";
1079 }
1080 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001081 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001082 } else if (Rd.r == 0xF && S == 1 &&
1083 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
1084 if (op3 == 0x0) {
1085 opcode << "tst";
1086 } else if (op3 == 0x4) {
1087 opcode << "teq";
1088 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +00001089 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001090 } else {
1091 opcode << "cmp.w";
1092 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001093 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001094 } else {
1095 switch (op3) {
1096 case 0x0: opcode << "and"; break;
1097 case 0x1: opcode << "bic"; break;
1098 case 0x2: opcode << "orr"; break;
1099 case 0x3: opcode << "orn"; break;
1100 case 0x4: opcode << "eor"; break;
1101 case 0x8: opcode << "add"; break;
1102 case 0xA: opcode << "adc"; break;
1103 case 0xB: opcode << "sbc"; break;
1104 case 0xD: opcode << "sub"; break;
1105 case 0xE: opcode << "rsb"; break;
1106 default: opcode << "UNKNOWN DPMI-" << op3; break;
1107 }
1108 if (S == 1) {
1109 opcode << "s";
1110 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001111 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001112 }
Ian Rogers40627db2012-03-04 17:31:09 -08001113 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1114 // Data-processing (plain binary immediate)
1115 // |111|11|10|00000|0000|1|111110000000000|
1116 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1117 // |---|--|--|-----|----|-|---------------|
1118 // |332|22|22|22222|1111|1|111110000000000|
1119 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1120 // |---|--|--|-----|----|-|---------------|
1121 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1122 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001123 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001124 case 0x00: case 0x0A: {
1125 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001126 ArmRegister Rd(instr, 8);
1127 ArmRegister Rn(instr, 16);
1128 uint32_t i = (instr >> 26) & 1;
1129 uint32_t imm3 = (instr >> 12) & 0x7;
1130 uint32_t imm8 = instr & 0xFF;
1131 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1132 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001133 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001134 args << Rd << ", " << Rn << ", #" << imm12;
1135 } else {
1136 opcode << "adr";
1137 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001138 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001139 }
1140 break;
1141 }
Ian Rogers55019132013-02-08 01:05:23 -08001142 case 0x04: case 0x0C: {
1143 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001144 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001145 uint32_t i = (instr >> 26) & 1;
1146 uint32_t imm3 = (instr >> 12) & 0x7;
1147 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001148 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001149 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001150 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001151 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001152 break;
1153 }
jeffhaoeae26912013-01-28 16:29:54 -08001154 case 0x16: {
1155 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
1156 ArmRegister Rd(instr, 8);
1157 ArmRegister Rn(instr, 16);
1158 uint32_t msb = instr & 0x1F;
1159 uint32_t imm2 = (instr >> 6) & 0x3;
1160 uint32_t imm3 = (instr >> 12) & 0x7;
1161 uint32_t lsb = (imm3 << 2) | imm2;
1162 uint32_t width = msb - lsb + 1;
1163 if (Rn.r != 0xF) {
1164 opcode << "bfi";
1165 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1166 } else {
1167 opcode << "bfc";
1168 args << Rd << ", #" << lsb << ", #" << width;
1169 }
1170 break;
1171 }
Ian Rogers40627db2012-03-04 17:31:09 -08001172 default:
1173 break;
1174 }
1175 } else {
1176 // Branches and miscellaneous control
1177 // |111|11|1000000|0000|1|111|1100|00000000|
1178 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1179 // |---|--|-------|----|-|---|----|--------|
1180 // |332|22|2222222|1111|1|111|1100|00000000|
1181 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1182 // |---|--|-------|----|-|---|----|--------|
1183 // |111|10| op2 | |1|op3|op4 | |
1184
1185 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001186 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001187 switch (op3) {
1188 case 0:
1189 if ((op2 & 0x38) != 0x38) {
1190 // Conditional branch
1191 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1192 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1193 // |---|--|-|----|------|-|-|--|-|--|-----------|
1194 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1195 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1196 // |---|--|-|----|------|-|-|--|-|--|-----------|
1197 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1198 uint32_t S = (instr >> 26) & 1;
1199 uint32_t J2 = (instr >> 11) & 1;
1200 uint32_t J1 = (instr >> 13) & 1;
1201 uint32_t imm6 = (instr >> 16) & 0x3F;
1202 uint32_t imm11 = instr & 0x7FF;
1203 uint32_t cond = (instr >> 22) & 0xF;
1204 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1205 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001206 opcode << "b";
1207 DumpCond(opcode, cond);
1208 opcode << ".w";
1209 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001210 } else if (op2 == 0x3B) {
1211 // Miscellaneous control instructions
1212 uint32_t op5 = (instr >> 4) & 0xF;
1213 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001214 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1215 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1216 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001217 }
Ian Rogers40627db2012-03-04 17:31:09 -08001218 }
1219 break;
1220 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001221 if ((op2 & 0x38) == 0x38) {
1222 if (op2 == 0x7F) {
1223 opcode << "udf";
1224 }
1225 break;
1226 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001227 FALLTHROUGH_INTENDED; // Else deliberate fall-through to B.
Ian Rogersd0876a92013-02-08 11:30:38 -08001228 case 1: case 3: {
1229 // B
1230 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1231 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1232 // |---|--|-|----|------|--|--|-|--|-----------|
1233 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1234 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1235 // |---|--|-|----|------|--|--|-|--|-----------|
1236 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1237 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1238 uint32_t S = (instr >> 26) & 1;
1239 uint32_t cond = (instr >> 22) & 0xF;
1240 uint32_t J2 = (instr >> 11) & 1;
1241 uint32_t form = (instr >> 12) & 1;
1242 uint32_t J1 = (instr >> 13) & 1;
1243 uint32_t imm10 = (instr >> 16) & 0x3FF;
1244 uint32_t imm6 = (instr >> 16) & 0x3F;
1245 uint32_t imm11 = instr & 0x7FF;
1246 opcode << "b";
1247 int32_t imm32;
1248 if (form == 0) {
1249 DumpCond(opcode, cond);
1250 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1251 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1252 } else {
1253 uint32_t I1 = ~(J1 ^ S);
1254 uint32_t I2 = ~(J2 ^ S);
1255 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1256 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1257 }
1258 opcode << ".w";
1259 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001260 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001261 }
Ian Rogers40627db2012-03-04 17:31:09 -08001262 case 4: case 6: case 5: case 7: {
1263 // BL, BLX (immediate)
1264 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1265 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1266 // |---|--|-|----------|--|--|-|--|-----------|
1267 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1268 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1269 // |---|--|-|----------|--|--|-|--|-----------|
Dave Allisond6ed6422014-04-09 23:36:15 +00001270 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
Ian Rogers40627db2012-03-04 17:31:09 -08001271 uint32_t S = (instr >> 26) & 1;
1272 uint32_t J2 = (instr >> 11) & 1;
Dave Allisond6ed6422014-04-09 23:36:15 +00001273 uint32_t L = (instr >> 12) & 1;
Ian Rogers40627db2012-03-04 17:31:09 -08001274 uint32_t J1 = (instr >> 13) & 1;
1275 uint32_t imm10 = (instr >> 16) & 0x3FF;
1276 uint32_t imm11 = instr & 0x7FF;
Dave Allisond6ed6422014-04-09 23:36:15 +00001277 if (L == 0) {
1278 opcode << "bx";
Dave Allisonf9487c02014-04-08 23:08:12 +00001279 } else {
Dave Allisond6ed6422014-04-09 23:36:15 +00001280 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001281 }
1282 uint32_t I1 = ~(J1 ^ S);
1283 uint32_t I2 = ~(J2 ^ S);
1284 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1285 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001286 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001287 break;
1288 }
1289 }
1290 }
1291 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001292 case 3:
1293 switch (op2) {
Vladimir Marko55d7c182015-01-05 15:17:01 +00001294 case 0x07: case 0x0F: case 0x17: case 0x1F: { // Explicitly UNDEFINED, A6.3.
1295 opcode << "UNDEFINED";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001296 break;
1297 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001298 case 0x06: case 0x0E: { // "Store single data item" undefined opcodes, A6.3.10.
1299 opcode << "UNDEFINED [store]";
1300 break;
1301 }
1302 case 0x15: case 0x1D: { // "Load word" undefined opcodes, A6.3.7.
1303 opcode << "UNDEFINED [load]";
1304 break;
1305 }
1306 case 0x10: case 0x12: case 0x14: case 0x16: case 0x18: case 0x1A: case 0x1C: case 0x1E: {
1307 opcode << "UNKNOWN " << op2 << " [SIMD]";
1308 break;
1309 }
1310 case 0x01: case 0x00: case 0x09: case 0x08: // {LD,ST}RB{,T}
1311 case 0x03: case 0x02: case 0x0B: case 0x0A: // {LD,ST}RH{,T}
1312 case 0x05: case 0x04: case 0x0D: case 0x0C: // {LD,ST}R{,T}
1313 case 0x11: case 0x19: // LDRSB{,T} (no signed store)
1314 case 0x13: case 0x1B: { // LDRSH{,T} (no signed store)
1315 // Load:
1316 // (Store is the same except that l==0 and always s==0 below.)
1317 // 00s.whl (sign, word, half, load)
1318 // LDR{S}B imm12: 11111|00s1001| Rn | Rt |imm12 (0x09)
1319 // LDR{S}B imm8: 11111|00s0001| Rn | Rt |1PUW|imm8 (0x01)
1320 // LDR{S}BT imm8: 11111|00s0001| Rn | Rt |1110|imm8 (0x01)
1321 // LDR{S}B lit: 11111|00sU001|1111| Rt |imm12 (0x01/0x09)
1322 // LDR{S}B reg: 11111|00s0001| Rn | Rt |000000|imm2| Rm (0x01)
1323 // LDR{S}H imm12: 11111|00s1011| Rn | Rt |imm12 (0x0B)
1324 // LDR{S}H imm8: 11111|00s0011| Rn | Rt |1PUW|imm8 (0x03)
1325 // LDR{S}HT imm8: 11111|00s0011| Rn | Rt |1110|imm8 (0x03)
1326 // LDR{S}H lit: 11111|00sU011|1111| Rt |imm12 (0x03/0x0B)
1327 // LDR{S}H reg: 11111|00s0011| Rn | Rt |000000|imm2| Rm (0x03)
1328 // LDR imm12: 11111|0001101| Rn | Rt |imm12 (0x0D)
1329 // LDR imm8: 11111|0000101| Rn | Rt |1PUW|imm8 (0x05)
1330 // LDRT imm8: 11111|0000101| Rn | Rt |1110|imm8 (0x05)
1331 // LDR lit: 11111|000U101|1111| Rt |imm12 (0x05/0x0D)
1332 // LDR reg: 11111|0000101| Rn | Rt |000000|imm2| Rm (0x05)
1333 //
1334 // If Rt == 15, instead of load we have preload:
1335 // PLD{W} imm12: 11111|00010W1| Rn |1111|imm12 (0x09/0x0B)
1336 // PLD{W} imm8: 11111|00000W1| Rn |1111|1100|imm8 (0x01/0x03); -imm8
1337 // PLD lit: 11111|000U001|1111|1111|imm12 (0x01/0x09)
1338 // PLD{W} reg: 11111|00000W1| Rn |1111|000000|imm2| Rm (0x01/0x03)
1339 // PLI imm12: 11111|0011001| Rn |1111|imm12 (0x19)
1340 // PLI imm8: 11111|0010001| Rn |1111|1100|imm8 (0x11); -imm8
1341 // PLI lit: 11111|001U001|1111|1111|imm12 (0x01/0x09)
1342 // PLI reg: 11111|0010001| Rn |1111|000000|imm2| Rm (0x01/0x03)
1343
1344 bool is_load = HasBitSet(instr, 20);
1345 bool is_half = HasBitSet(instr, 21); // W for PLD/PLDW.
1346 bool is_word = HasBitSet(instr, 22);
1347 bool is_signed = HasBitSet(instr, 24);
jeffhaoeae26912013-01-28 16:29:54 -08001348 ArmRegister Rn(instr, 16);
1349 ArmRegister Rt(instr, 12);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001350 uint32_t imm12 = instr & 0xFFF;
1351 uint32_t U = (instr >> 23) & 1; // U for imm12
1352 uint32_t imm8 = instr & 0xFF;
1353 uint32_t op4 = (instr >> 8) & 0xF; // 1PUW for imm8
1354 if (Rt.r == PC && is_load && !is_word) {
1355 // PLD, PLDW, PLI
1356 const char* pld_pli = (is_signed ? "pli" : "pld");
1357 const char* w = (is_half ? "w" : "");
1358 if (is_signed && !is_half) {
1359 opcode << "UNDEFINED [PLI+W]";
1360 } else if (Rn.r == PC || U != 0u) {
1361 opcode << pld_pli << w;
1362 args << "[" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1363 if (Rn.r == PC && is_half) {
1364 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001365 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001366 } else if ((instr & 0xFC0) == 0) {
1367 opcode << pld_pli << w;
1368 RmLslImm2 Rm(instr);
1369 args << "[" << Rn << ", " << Rm << "]";
1370 } else if (op4 == 0xC) {
1371 opcode << pld_pli << w;
1372 args << "[" << Rn << ", #-" << imm8 << "]";
1373 } else {
1374 opcode << "UNDEFINED [~" << pld_pli << "]";
jeffhaoeae26912013-01-28 16:29:54 -08001375 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001376 break;
1377 }
1378 const char* ldr_str = is_load ? "ldr" : "str";
1379 const char* sign = is_signed ? "s" : "";
1380 const char* type = is_word ? "" : is_half ? "h" : "b";
1381 bool unpred = (Rt.r == SP && !is_word) || (Rt.r == PC && !is_load);
1382 if (Rn.r == PC && !is_load) {
1383 opcode << "UNDEFINED [STR-lit]";
1384 unpred = false;
1385 } else if (Rn.r == PC || U != 0u) {
1386 // Load/store with imm12 (load literal if Rn.r == PC; there's no store literal).
1387 opcode << ldr_str << sign << type << ".w";
1388 args << Rt << ", [" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1389 if (Rn.r == TR && is_load) {
1390 args << " ; ";
1391 Thread::DumpThreadOffset<4>(args, imm12);
1392 } else if (Rn.r == PC) {
1393 T2LitType lit_type[] = {
1394 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1395 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1396 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1397 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1398 };
1399 DCHECK_LT(op2 >> 1, arraysize(lit_type));
1400 DCHECK_NE(lit_type[op2 >> 1], kT2LitInvalid);
1401 DumpThumb2Literal(args, instr_ptr, U, imm12, lit_type[op2 >> 1]);
1402 }
1403 } else if ((instr & 0xFC0) == 0) {
1404 opcode << ldr_str << sign << type << ".w";
1405 RmLslImm2 Rm(instr);
1406 args << Rt << ", [" << Rn << ", " << Rm << "]";
1407 unpred = unpred || (Rm.rm.r == SP) || (Rm.rm.r == PC);
1408 } else if (is_word && Rn.r == SP && imm8 == 4 && op4 == (is_load ? 0xB : 0xD)) {
1409 opcode << (is_load ? "pop" : "push") << ".w";
1410 args << Rn;
1411 unpred = unpred || (Rn.r == SP);
1412 } else if ((op4 & 5) == 0) {
1413 opcode << "UNDEFINED [P = W = 0 for " << ldr_str << "]";
1414 unpred = false;
1415 } else {
1416 uint32_t P = (instr >> 10) & 1;
1417 U = (instr >> 9) & 1;
1418 uint32_t W = (instr >> 8) & 1;
1419 bool pre_index = (P != 0 && W == 1);
1420 bool post_index = (P == 0 && W == 1);
1421 const char* t = (P != 0 && U != 0 && W == 0) ? "t" : ""; // Unprivileged load/store?
1422 opcode << ldr_str << sign << type << t << ".w";
1423 args << Rt << ", [" << Rn << (post_index ? "]" : "") << ", #" << (U != 0 ? "" : "-")
1424 << imm8 << (post_index ? "" : "]") << (pre_index ? "!" : "");
1425 unpred = (W != 0 && Rn.r == Rt.r);
1426 }
1427 if (unpred) {
1428 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001429 }
1430 break;
1431 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001432 case 0x29: { // 0101001
1433 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1434 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1435 // |---|--|-------|----|----|----|--|---|----|
1436 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1437 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1438 // |---|--|-------|----|----|----|--|---|----|
1439 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1440 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1441 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1442 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1443 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1444 if ((instr & 0xf0c0) == 0xf080) {
1445 uint32_t op3 = (instr >> 4) & 3;
1446 opcode << kThumbReverseOperations[op3];
1447 ArmRegister Rm(instr, 0);
1448 ArmRegister Rd(instr, 8);
1449 args << Rd << ", " << Rm;
1450 ArmRegister Rm2(instr, 16);
1451 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1452 args << " (UNPREDICTABLE)";
1453 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001454 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001455 break;
1456 }
Scott Wakeling611d3392015-07-10 11:42:06 +01001457 case 0x2B: { // 0101011
1458 // CLZ - 111 11 0101011 mmmm 1111 dddd 1000 mmmm
1459 if ((instr & 0xf0f0) == 0xf080) {
1460 opcode << "clz";
1461 ArmRegister Rm(instr, 0);
1462 ArmRegister Rd(instr, 8);
1463 args << Rd << ", " << Rm;
1464 ArmRegister Rm2(instr, 16);
1465 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1466 args << " (UNPREDICTABLE)";
1467 }
1468 }
1469 break;
1470 }
Dave Allison70202782013-10-22 17:52:19 -07001471 default: // more formats
1472 if ((op2 >> 4) == 2) { // 010xxxx
1473 // data processing (register)
Vladimir Markoc777e0d2014-04-03 17:59:02 +01001474 if ((instr & 0x0080f0f0) == 0x0000f000) {
1475 // LSL, LSR, ASR, ROR
1476 uint32_t shift_op = (instr >> 21) & 3;
1477 uint32_t S = (instr >> 20) & 1;
1478 ArmRegister Rd(instr, 8);
1479 ArmRegister Rn(instr, 16);
1480 ArmRegister Rm(instr, 0);
1481 opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
1482 args << Rd << ", " << Rn << ", " << Rm;
1483 }
Dave Allison70202782013-10-22 17:52:19 -07001484 } else if ((op2 >> 3) == 6) { // 0110xxx
1485 // Multiply, multiply accumulate, and absolute difference
1486 op1 = (instr >> 20) & 0x7;
Ningsheng Jiana262f772014-11-25 16:48:07 +08001487 op2 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -07001488 ArmRegister Ra(instr, 12);
1489 ArmRegister Rn(instr, 16);
1490 ArmRegister Rm(instr, 0);
1491 ArmRegister Rd(instr, 8);
1492 switch (op1) {
1493 case 0:
1494 if (op2 == 0) {
1495 if (Ra.r == 0xf) {
1496 opcode << "mul";
1497 args << Rd << ", " << Rn << ", " << Rm;
1498 } else {
1499 opcode << "mla";
1500 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1501 }
1502 } else {
1503 opcode << "mls";
1504 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1505 }
1506 break;
1507 case 1:
1508 case 2:
1509 case 3:
1510 case 4:
1511 case 5:
1512 case 6:
1513 break; // do these sometime
1514 }
1515 } else if ((op2 >> 3) == 7) { // 0111xxx
1516 // Long multiply, long multiply accumulate, and divide
1517 op1 = (instr >> 20) & 0x7;
1518 op2 = (instr >> 4) & 0xf;
1519 ArmRegister Rn(instr, 16);
1520 ArmRegister Rm(instr, 0);
1521 ArmRegister Rd(instr, 8);
1522 ArmRegister RdHi(instr, 8);
1523 ArmRegister RdLo(instr, 12);
1524 switch (op1) {
1525 case 0:
1526 opcode << "smull";
1527 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1528 break;
1529 case 1:
1530 opcode << "sdiv";
1531 args << Rd << ", " << Rn << ", " << Rm;
1532 break;
1533 case 2:
1534 opcode << "umull";
1535 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1536 break;
1537 case 3:
1538 opcode << "udiv";
1539 args << Rd << ", " << Rn << ", " << Rm;
1540 break;
1541 case 4:
1542 case 5:
1543 case 6:
1544 break; // TODO: when we generate these...
1545 }
1546 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001547 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001548 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001549 default:
1550 break;
1551 }
Ian Rogers9af89402012-09-07 11:29:35 -07001552
1553 // Apply any IT-block conditions to the opcode if necessary.
1554 if (!it_conditions_.empty()) {
1555 opcode << it_conditions_.back();
1556 it_conditions_.pop_back();
1557 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001558 if (opcode.str().size() == 0) {
1559 opcode << "UNKNOWN " << op2;
1560 }
Ian Rogers9af89402012-09-07 11:29:35 -07001561
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001562 os << FormatInstructionPointer(instr_ptr)
1563 << StringPrintf(": %08x\t%-7s ", instr, opcode.str().c_str())
1564 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001565 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001566} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001567
1568size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1569 uint16_t instr = ReadU16(instr_ptr);
1570 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1571 if (is_32bit) {
1572 return DumpThumb32(os, instr_ptr);
1573 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001574 std::ostringstream opcode;
1575 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001576 uint16_t opcode1 = instr >> 10;
1577 if (opcode1 < 0x10) {
1578 // shift (immediate), add, subtract, move, and compare
1579 uint16_t opcode2 = instr >> 9;
1580 switch (opcode2) {
1581 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1582 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001583 // Logical shift left - 00 000xx iii mmm ddd
1584 // Logical shift right - 00 001xx iii mmm ddd
1585 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001586 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001587 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001588 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001589 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001590 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001591 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001592 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001593 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001594 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001595 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001596 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001597 break;
1598 }
1599 case 0xC: case 0xD: case 0xE: case 0xF: {
1600 // Add register - 00 01100 mmm nnn ddd
1601 // Sub register - 00 01101 mmm nnn ddd
1602 // Add 3-bit immediate - 00 01110 iii nnn ddd
1603 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1604 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001605 ThumbRegister Rn(instr, 3);
1606 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001607 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001608 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001609 } else {
1610 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001611 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001612 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001613 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001614 }
1615 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001616 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001617 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001618 ArmRegister Rm(imm3_or_Rm);
1619 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001621 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001622 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001623 break;
1624 }
1625 case 0x10: case 0x11: case 0x12: case 0x13:
1626 case 0x14: case 0x15: case 0x16: case 0x17:
1627 case 0x18: case 0x19: case 0x1A: case 0x1B:
1628 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1629 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1630 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1631 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1632 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001633 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001634 uint16_t imm8 = instr & 0xFF;
1635 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001636 case 4: opcode << "movs"; break;
1637 case 5: opcode << "cmp"; break;
1638 case 6: opcode << "adds"; break;
1639 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001640 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001641 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001642 break;
1643 }
1644 default:
1645 break;
1646 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001647 } else if (opcode1 == 0x10) {
1648 // Data-processing
1649 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001650 ThumbRegister rm(instr, 3);
1651 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001652 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001653 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654 } else if (opcode1 == 0x11) {
1655 // Special data instructions and branch and exchange
1656 uint16_t opcode2 = (instr >> 6) & 0x0F;
1657 switch (opcode2) {
1658 case 0x0: case 0x1: case 0x2: case 0x3: {
1659 // Add low registers - 010001 0000 xxxxxx
1660 // Add high registers - 010001 0001/001x xxxxxx
1661 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001662 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001663 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001664 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001665 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001666 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667 break;
1668 }
1669 case 0x8: case 0x9: case 0xA: case 0xB: {
1670 // Move low registers - 010001 1000 xxxxxx
1671 // Move high registers - 010001 1001/101x xxxxxx
1672 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001673 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001674 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001675 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001676 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001677 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001678 break;
1679 }
1680 case 0x5: case 0x6: case 0x7: {
1681 // Compare high registers - 010001 0101/011x xxxxxx
1682 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001683 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001684 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001685 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001686 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001687 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001688 break;
1689 }
1690 case 0xC: case 0xD: case 0xE: case 0xF: {
1691 // Branch and exchange - 010001 110x xxxxxx
1692 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001693 ArmRegister rm(instr, 3);
1694 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1695 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001696 break;
1697 }
1698 default:
1699 break;
1700 }
jeffhaoeae26912013-01-28 16:29:54 -08001701 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1702 ThumbRegister Rt(instr, 8);
1703 uint16_t imm8 = instr & 0xFF;
1704 opcode << "ldr";
1705 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001706 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1707 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1708 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1709 // Load/store single data item
1710 uint16_t opA = (instr >> 12) & 0xF;
1711 if (opA == 0x5) {
1712 uint16_t opB = (instr >> 9) & 0x7;
1713 ThumbRegister Rm(instr, 6);
1714 ThumbRegister Rn(instr, 3);
1715 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001716 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001717 case 0: opcode << "str"; break;
1718 case 1: opcode << "strh"; break;
1719 case 2: opcode << "strb"; break;
1720 case 3: opcode << "ldrsb"; break;
1721 case 4: opcode << "ldr"; break;
1722 case 5: opcode << "ldrh"; break;
1723 case 6: opcode << "ldrb"; break;
1724 case 7: opcode << "ldrsh"; break;
1725 }
1726 args << Rt << ", [" << Rn << ", " << Rm << "]";
1727 } else if (opA == 9) {
1728 uint16_t opB = (instr >> 11) & 1;
1729 ThumbRegister Rt(instr, 8);
1730 uint16_t imm8 = instr & 0xFF;
1731 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001732 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001733 } else {
1734 uint16_t imm5 = (instr >> 6) & 0x1F;
1735 uint16_t opB = (instr >> 11) & 1;
1736 ThumbRegister Rn(instr, 3);
1737 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001738 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001739 case 6:
1740 imm5 <<= 2;
1741 opcode << (opB == 0 ? "str" : "ldr");
1742 break;
1743 case 7:
1744 imm5 <<= 0;
1745 opcode << (opB == 0 ? "strb" : "ldrb");
1746 break;
1747 case 8:
1748 imm5 <<= 1;
1749 opcode << (opB == 0 ? "strh" : "ldrh");
1750 break;
1751 }
1752 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1753 }
jeffhaoeae26912013-01-28 16:29:54 -08001754 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001755 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001756 uint32_t cond = (instr >> 8) & 0xF;
1757 opcode << "b";
1758 DumpCond(opcode, cond);
1759 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001760 } else if ((instr & 0xF800) == 0xA800) {
1761 // Generate SP-relative address
1762 ThumbRegister rd(instr, 8);
1763 int imm8 = instr & 0xFF;
1764 opcode << "add";
1765 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001766 } else if ((instr & 0xF000) == 0xB000) {
1767 // Miscellaneous 16-bit instructions
1768 uint16_t opcode2 = (instr >> 5) & 0x7F;
1769 switch (opcode2) {
1770 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1771 // Add immediate to SP - 1011 00000 ii iiiii
1772 // Subtract immediate from SP - 1011 00001 ii iiiii
1773 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001774 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001775 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001776 break;
1777 }
Ian Rogers087b2412012-03-21 01:30:32 -07001778 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001779 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001780 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1781 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001782 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001783 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1784 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1785 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001786 // CBNZ, CBZ
1787 uint16_t op = (instr >> 11) & 1;
1788 uint16_t i = (instr >> 9) & 1;
1789 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001790 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001791 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001792 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001793 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001794 DumpBranchTarget(args, instr_ptr + 4, imm32);
1795 break;
1796 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001797 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1798 case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F: {
1799 opcode << "push";
1800 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 6));
1801 break;
1802 }
1803 case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
1804 case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F: {
1805 opcode << "pop";
1806 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 7));
1807 break;
1808 }
1809 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: {
1810 opcode << "bkpt";
1811 args << "#" << (instr & 0xFF);
1812 break;
1813 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001814 case 0x50: case 0x51: // 101000x
1815 case 0x52: case 0x53: // 101001x
1816 case 0x56: case 0x57: { // 101011x
1817 uint16_t op = (instr >> 6) & 3;
1818 opcode << kThumbReverseOperations[op];
1819 ThumbRegister Rm(instr, 3);
1820 ThumbRegister Rd(instr, 0);
1821 args << Rd << ", " << Rm;
1822 break;
1823 }
Ian Rogers40627db2012-03-04 17:31:09 -08001824 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1825 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1826 // If-Then, and hints
1827 uint16_t opA = (instr >> 4) & 0xF;
1828 uint16_t opB = instr & 0xF;
1829 if (opB == 0) {
1830 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001831 case 0: opcode << "nop"; break;
1832 case 1: opcode << "yield"; break;
1833 case 2: opcode << "wfe"; break;
1834 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001835 default: break;
1836 }
1837 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001838 uint32_t first_cond = opA;
1839 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001840 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001841
1842 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1843 // and store up the actual condition codes we'll want to add to the next few opcodes.
1844 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001845 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001846 for (size_t i = 0; i < count; ++i) {
1847 bool positive_cond = ((first_cond & 1) != 0);
1848 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1849 if (positive_mask == positive_cond) {
1850 opcode << 't';
1851 it_conditions_[i] = kConditionCodeNames[first_cond];
1852 } else {
1853 opcode << 'e';
1854 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1855 }
1856 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001857 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001858
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001859 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1860 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001861 }
1862 break;
1863 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001864 default:
1865 break;
1866 }
1867 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1868 ((instr & 0xE000) == 0x8000)) {
1869 // Load/store single data item
1870 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001871 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001872 switch (opA) {
1873 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001874 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1875 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001876 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001877 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001878 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001879 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1880 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001881 break;
1882 }
1883 case 0x9: {
1884 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1885 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1886 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001887 ThumbRegister Rt(instr, 8);
1888 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1889 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001890 break;
1891 }
1892 default:
1893 break;
1894 }
Ian Rogers40627db2012-03-04 17:31:09 -08001895 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1896 uint16_t imm11 = instr & 0x7FFF;
1897 int32_t imm32 = imm11 << 1;
1898 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001899 opcode << "b";
1900 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001901 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001902
1903 // Apply any IT-block conditions to the opcode if necessary.
1904 if (!it_conditions_.empty()) {
1905 opcode << it_conditions_.back();
1906 it_conditions_.pop_back();
1907 }
1908
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001909 os << FormatInstructionPointer(instr_ptr)
1910 << StringPrintf(": %04x \t%-7s ", instr, opcode.str().c_str())
1911 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001912 }
1913 return 2;
1914}
1915
1916} // namespace arm
1917} // namespace art