blob: c3e288deaefe752d3f6169e8d6c5dfe830a78119 [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
Andreas Gampebda1d602016-08-29 17:43:45 -070024#include "android-base/logging.h"
25#include "android-base/stringprintf.h"
26
Vladimir Marko55d7c182015-01-05 15:17:01 +000027#include "arch/arm/registers_arm.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070028#include "base/bit_utils.h"
Andreas Gampebda1d602016-08-29 17:43:45 -070029
30using android::base::StringPrintf;
Elliott Hughes0f3c5532012-03-30 14:51:51 -070031
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080032namespace art {
33namespace arm {
34
Ian Rogersb23a7722012-10-09 16:54:26 -070035size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
36 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
37 DumpArm(os, begin);
38 return 4;
39 } else {
40 // remove thumb specifier bits
41 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
42 return DumpThumb16(os, begin);
43 }
44}
45
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080046void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
47 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
48 for (const uint8_t* cur = begin; cur < end; cur += 4) {
49 DumpArm(os, cur);
50 }
51 } else {
52 // remove thumb specifier bits
53 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
54 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
55 for (const uint8_t* cur = begin; cur < end;) {
56 cur += DumpThumb16(os, cur);
57 }
58 }
59}
60
Elliott Hughes77405792012-03-15 15:22:12 -070061static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070062 "eq", // 0000 - equal
63 "ne", // 0001 - not-equal
64 "cs", // 0010 - carry-set, greater than, equal or unordered
65 "cc", // 0011 - carry-clear, less than
66 "mi", // 0100 - minus, negative
67 "pl", // 0101 - plus, positive or zero
68 "vs", // 0110 - overflow
69 "vc", // 0111 - no overflow
70 "hi", // 1000 - unsigned higher
71 "ls", // 1001 - unsigned lower or same
72 "ge", // 1010 - signed greater than or equal
73 "lt", // 1011 - signed less than
74 "gt", // 1100 - signed greater than
75 "le", // 1101 - signed less than or equal
76 "", // 1110 - always
77 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080078};
79
80void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
81 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070082 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080083 } else {
84 os << "Unexpected condition: " << cond;
85 }
86}
87
Ian Rogersb122a4b2013-11-19 18:00:50 -080088void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
89 switch (domain) {
Andreas Gampec8ccf682014-09-29 20:07:43 -070090 case 15U /* 0b1111 */: os << "sy"; break;
91 case 14U /* 0b1110 */: os << "st"; break;
92 case 11U /* 0b1011 */: os << "ish"; break;
93 case 10U /* 0b1010 */: os << "ishst"; break;
94 case 7U /* 0b0111 */: os << "nsh"; break;
95 case 6U /* 0b0110 */: os << "nshst"; break;
96 case 3U /* 0b0011 */: os << "osh"; break;
97 case 2U /* 0b0010 */: os << "oshst"; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -080098 }
99}
100
Ian Rogers40627db2012-03-04 17:31:09 -0800101void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700102 os << StringPrintf("%+d (", imm32) << FormatInstructionPointer(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800103}
104
105static uint32_t ReadU16(const uint8_t* ptr) {
106 return ptr[0] | (ptr[1] << 8);
107}
108
109static uint32_t ReadU32(const uint8_t* ptr) {
110 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
111}
112
Elliott Hughes77405792012-03-15 15:22:12 -0700113static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700114 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
115 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700116};
117
Ian Rogersad03ef52012-03-18 19:34:47 -0700118static const char* kThumbDataProcessingOperations[] = {
119 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
120 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
121};
122
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100123static const char* const kThumb2ShiftOperations[] = {
124 "lsl", "lsr", "asr", "ror"
125};
126
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100127static const char* kThumbReverseOperations[] = {
128 "rev", "rev16", "rbit", "revsh"
129};
130
Elliott Hughes77405792012-03-15 15:22:12 -0700131struct ArmRegister {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800132 explicit ArmRegister(uint32_t r_in) : r(r_in) { CHECK_LE(r_in, 15U); }
133 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) {
134 CHECK_LE(r, 15U);
135 }
Elliott Hughes77405792012-03-15 15:22:12 -0700136 uint32_t r;
137};
138std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
139 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700140 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700141 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700142 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700143 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700144 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700145 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700146 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700147 }
148 return os;
149}
150
Elliott Hughes630e77d2012-03-22 19:20:56 -0700151struct ThumbRegister : ArmRegister {
152 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700153};
154
Vladimir Marko55d7c182015-01-05 15:17:01 +0000155struct RmLslImm2 {
156 explicit RmLslImm2(uint32_t instr) : imm2((instr >> 4) & 0x3), rm(instr & 0xf) {}
157 uint32_t imm2;
Elliott Hughes77405792012-03-15 15:22:12 -0700158 ArmRegister rm;
159};
Vladimir Marko55d7c182015-01-05 15:17:01 +0000160std::ostream& operator<<(std::ostream& os, const RmLslImm2& r) {
Elliott Hughes77405792012-03-15 15:22:12 -0700161 os << r.rm;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000162 if (r.imm2 != 0) {
163 os << ", lsl #" << r.imm2;
Elliott Hughes77405792012-03-15 15:22:12 -0700164 }
165 return os;
166}
167
Elliott Hughes1ca98492012-04-12 17:21:02 -0700168struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700169 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700170 uint32_t rotate = ((instruction >> 8) & 0xf);
171 uint32_t imm = (instruction & 0xff);
172 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
173 }
174 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700175};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700176std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700177 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700178 return os;
179}
180
181struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700182 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700183 uint32_t register_list;
184};
185std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
186 if (rhs.register_list == 0) {
187 os << "<no register list?>";
188 return os;
189 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700190 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700191 bool first = true;
192 for (size_t i = 0; i < 16; i++) {
193 if ((rhs.register_list & (1 << i)) != 0) {
194 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700195 first = false;
196 } else {
197 os << ", ";
198 }
199 os << ArmRegister(i);
200 }
201 }
202 os << "}";
203 return os;
204}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800205
Vladimir Markodd577a32013-11-07 19:25:24 +0000206struct FpRegister {
Roland Levillain3887c462015-08-12 18:15:42 +0100207 FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
Vladimir Markodd577a32013-11-07 19:25:24 +0000208 size = (instr >> 8) & 1;
209 uint32_t Vn = (instr >> at_bit) & 0xF;
210 uint32_t N = (instr >> extra_at_bit) & 1;
211 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
212 }
Roland Levillain3887c462015-08-12 18:15:42 +0100213 FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit, uint32_t forced_size) {
Zheng Xue19649a2014-02-27 13:30:55 +0000214 size = forced_size;
215 uint32_t Vn = (instr >> at_bit) & 0xF;
216 uint32_t N = (instr >> extra_at_bit) & 1;
217 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
218 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000219 FpRegister(const FpRegister& other, uint32_t offset)
220 : size(other.size), r(other.r + offset) {}
221
222 uint32_t size; // 0 = f32, 1 = f64
223 uint32_t r;
224};
225std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
226 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
227}
228
229struct FpRegisterRange {
230 explicit FpRegisterRange(uint32_t instr)
231 : first(instr, 12, 22), imm8(instr & 0xFF) {}
232 FpRegister first;
233 uint32_t imm8;
234};
235std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
236 os << "{" << rhs.first;
237 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
238 if (count > 1) {
239 os << "-" << FpRegister(rhs.first, count - 1);
240 }
241 if (rhs.imm8 == 0) {
242 os << " (EMPTY)";
243 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
244 os << rhs.first << " (HALF)";
245 }
246 os << "}";
247 return os;
248}
249
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800250void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700251 uint32_t instruction = ReadU32(instr_ptr);
252 uint32_t cond = (instruction >> 28) & 0xf;
253 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700254 std::string opcode;
255 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700256 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700257 switch (op1) {
258 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700259 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700260 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700261 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700262 opcode = "bkpt";
263 uint32_t imm12 = (instruction >> 8) & 0xfff;
264 uint32_t imm4 = (instruction & 0xf);
265 args << '#' << ((imm12 << 4) | imm4);
266 break;
267 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700268 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700269 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700270 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700271 break;
272 }
273 bool i = (instruction & (1 << 25)) != 0;
274 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700275 uint32_t op = (instruction >> 21) & 0xf;
276 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700277 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700278 bool is_mov = op == 13U /* 0b1101 */ || op == 15U /* 0b1111 */;
Dave Allison20dfc792014-06-16 20:44:29 -0700279 if (is_mov) {
280 // Show only Rd and Rm.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700281 if (s) {
Dave Allison20dfc792014-06-16 20:44:29 -0700282 suffixes += 's';
283 }
284 args << ArmRegister(instruction, 12) << ", ";
285 if (i) {
286 args << ShiftedImmediate(instruction);
287 } else {
288 // TODO: Shifted register.
289 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
290 }
Elliott Hughes77405792012-03-15 15:22:12 -0700291 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700292 if (implicit_s) {
293 // Rd is unused (and not shown), and we don't show the 's' suffix either.
294 } else {
295 if (s) {
296 suffixes += 's';
297 }
298 args << ArmRegister(instruction, 12) << ", ";
299 }
300 if (i) {
301 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
302 } else {
303 // TODO: Shifted register.
304 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
305 }
Elliott Hughes77405792012-03-15 15:22:12 -0700306 }
307 }
308 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700309 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700310 {
311 bool p = (instruction & (1 << 24)) != 0;
312 bool b = (instruction & (1 << 22)) != 0;
313 bool w = (instruction & (1 << 21)) != 0;
314 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700315 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700316 args << ArmRegister(instruction, 12) << ", ";
317 ArmRegister rn(instruction, 16);
318 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700319 UNIMPLEMENTED(FATAL) << "literals";
320 } else {
321 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700322 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700323 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700324 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700325 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700326 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700327 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700328 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700329 } else {
330 LOG(FATAL) << p << " " << w;
331 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700332 if (rn.r == 9) {
333 args << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -0700334 GetDisassemblerOptions()->thread_offset_name_function_(args, offset);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700335 }
Elliott Hughes77405792012-03-15 15:22:12 -0700336 }
337 }
338 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700339 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700340 {
341 bool p = (instruction & (1 << 24)) != 0;
342 bool u = (instruction & (1 << 23)) != 0;
343 bool w = (instruction & (1 << 21)) != 0;
344 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700345 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700346 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700347 }
348 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700349 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700350 {
351 bool bl = (instruction & (1 << 24)) != 0;
352 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700353 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700354 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700355 DumpBranchTarget(args, instr_ptr + 8, imm32);
356 }
357 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700358 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700359 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700360 break;
361 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700362 opcode += kConditionCodeNames[cond];
363 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700364 // TODO: a more complete ARM disassembler could generate wider opcodes.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 os << FormatInstructionPointer(instr_ptr)
366 << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
367 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800368}
369
Ian Rogersa9650dd2013-10-04 08:23:32 -0700370int32_t ThumbExpand(int32_t imm12) {
371 if ((imm12 & 0xC00) == 0) {
372 switch ((imm12 >> 8) & 3) {
373 case 0:
374 return imm12 & 0xFF;
375 case 1:
376 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
377 case 2:
378 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
379 default: // 3
380 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
381 (imm12 & 0xFF);
382 }
383 } else {
384 uint32_t val = 0x80 | (imm12 & 0x7F);
385 int32_t rotate = (imm12 >> 7) & 0x1F;
386 return (val >> rotate) | (val << (32 - rotate));
387 }
388}
389
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100390uint32_t VFPExpand32(uint32_t imm8) {
391 CHECK_EQ(imm8 & 0xffu, imm8);
392 uint32_t bit_a = (imm8 >> 7) & 1;
393 uint32_t bit_b = (imm8 >> 6) & 1;
394 uint32_t slice = imm8 & 0x3f;
395 return (bit_a << 31) | ((1 << 30) - (bit_b << 25)) | (slice << 19);
396}
397
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800398static uint64_t VFPExpand64(uint32_t imm8) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100399 CHECK_EQ(imm8 & 0xffu, imm8);
400 uint64_t bit_a = (imm8 >> 7) & 1;
401 uint64_t bit_b = (imm8 >> 6) & 1;
402 uint64_t slice = imm8 & 0x3f;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000403 return (bit_a << 63) | ((UINT64_C(1) << 62) - (bit_b << 54)) | (slice << 48);
404}
405
406enum T2LitType {
407 kT2LitInvalid,
408 kT2LitUByte,
409 kT2LitSByte,
410 kT2LitUHalf,
411 kT2LitSHalf,
412 kT2LitUWord,
413 kT2LitSWord,
414 kT2LitHexWord,
415 kT2LitULong,
416 kT2LitSLong,
417 kT2LitHexLong,
418};
419std::ostream& operator<<(std::ostream& os, T2LitType type) {
420 return os << static_cast<int>(type);
421}
422
Aart Bikd3059e72016-05-11 10:30:47 -0700423void DumpThumb2Literal(std::ostream& args,
424 const uint8_t* instr_ptr,
425 const uintptr_t lo_adr,
426 const uintptr_t hi_adr,
427 uint32_t U,
428 uint32_t imm32,
Vladimir Marko55d7c182015-01-05 15:17:01 +0000429 T2LitType type) {
430 // Literal offsets (imm32) are not required to be aligned so we may need unaligned access.
431 typedef const int16_t unaligned_int16_t __attribute__ ((aligned (1)));
432 typedef const uint16_t unaligned_uint16_t __attribute__ ((aligned (1)));
433 typedef const int32_t unaligned_int32_t __attribute__ ((aligned (1)));
434 typedef const uint32_t unaligned_uint32_t __attribute__ ((aligned (1)));
435 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (1)));
436 typedef const uint64_t unaligned_uint64_t __attribute__ ((aligned (1)));
437
Aart Bikd3059e72016-05-11 10:30:47 -0700438 // Get address of literal. Bail if not within expected buffer range to
439 // avoid trying to fetch invalid literals (we can encounter this when
440 // interpreting raw data as instructions).
Vladimir Marko55d7c182015-01-05 15:17:01 +0000441 uintptr_t pc = RoundDown(reinterpret_cast<intptr_t>(instr_ptr) + 4, 4);
442 uintptr_t lit_adr = U ? pc + imm32 : pc - imm32;
Aart Bikd3059e72016-05-11 10:30:47 -0700443 if (lit_adr < lo_adr || lit_adr >= hi_adr) {
444 args << " ; (?)";
445 return;
446 }
447
Vladimir Marko55d7c182015-01-05 15:17:01 +0000448 args << " ; ";
449 switch (type) {
450 case kT2LitUByte:
451 args << *reinterpret_cast<const uint8_t*>(lit_adr);
452 break;
453 case kT2LitSByte:
454 args << *reinterpret_cast<const int8_t*>(lit_adr);
455 break;
456 case kT2LitUHalf:
457 args << *reinterpret_cast<const unaligned_uint16_t*>(lit_adr);
458 break;
459 case kT2LitSHalf:
460 args << *reinterpret_cast<const unaligned_int16_t*>(lit_adr);
461 break;
462 case kT2LitUWord:
463 args << *reinterpret_cast<const unaligned_uint32_t*>(lit_adr);
464 break;
465 case kT2LitSWord:
466 args << *reinterpret_cast<const unaligned_int32_t*>(lit_adr);
467 break;
468 case kT2LitHexWord:
469 args << StringPrintf("0x%08x", *reinterpret_cast<const unaligned_uint32_t*>(lit_adr));
470 break;
471 case kT2LitULong:
472 args << *reinterpret_cast<const unaligned_uint64_t*>(lit_adr);
473 break;
474 case kT2LitSLong:
475 args << *reinterpret_cast<const unaligned_int64_t*>(lit_adr);
476 break;
477 case kT2LitHexLong:
478 args << StringPrintf("0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
479 break;
480 default:
481 LOG(FATAL) << "Invalid type: " << type;
482 break;
483 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100484}
485
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800486size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
487 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
488 // |111|1 1|1000000|0000|1111110000000000|
489 // |5 3|2 1|0987654|3 0|5 0 5 0|
490 // |---|---|-------|----|----------------|
491 // |332|2 2|2222222|1111|1111110000000000|
492 // |1 9|8 7|6543210|9 6|5 0 5 0|
493 // |---|---|-------|----|----------------|
494 // |111|op1| op2 | | |
495 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700496 if (op1 == 0) {
497 return DumpThumb16(os, instr_ptr);
498 }
499
Aart Bikd3059e72016-05-11 10:30:47 -0700500 // Set valid address range of backing buffer.
501 const uintptr_t lo_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->base_address_);
502 const uintptr_t hi_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->end_address_);
503
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800504 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700505 std::ostringstream opcode;
506 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800507 switch (op1) {
508 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 break;
510 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700511 if ((op2 & 0x64) == 0) { // 00x x0xx
512 // |111|11|10|00|0|00|0000|1111110000000000|
513 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
514 // |---|--|--|--|-|--|----|----------------|
515 // |332|22|22|22|2|22|1111|1111110000000000|
516 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
517 // |---|--|--|--|-|--|----|----------------|
518 // |111|01|00|op|0|WL| Rn | |
519 // |111|01| op2 | | |
520 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
521 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
522 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
523 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
524 uint32_t op = (instr >> 23) & 3;
525 uint32_t W = (instr >> 21) & 1;
526 uint32_t L = (instr >> 20) & 1;
527 ArmRegister Rn(instr, 16);
528 if (op == 1 || op == 2) {
529 if (op == 1) {
530 if (L == 0) {
531 opcode << "stm";
532 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700534 if (Rn.r != 13) {
535 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700536 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700537 } else {
538 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 }
540 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700541 } else {
542 if (L == 0) {
543 if (Rn.r != 13) {
544 opcode << "stmdb";
545 args << Rn << (W == 0 ? "" : "!") << ", ";
546 } else {
547 opcode << "push";
548 }
549 } else {
550 opcode << "ldmdb";
551 args << Rn << (W == 0 ? "" : "!") << ", ";
552 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800553 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700554 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800555 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700556 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700557 uint32_t op3 = (instr >> 23) & 3;
558 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700559 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700560 ArmRegister Rn(instr, 16);
561 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700562 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700563 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700564 if ((op3 & 2) == 2) { // 1x
565 int W = (instr >> 21) & 1;
566 int U = (instr >> 23) & 1;
567 int P = (instr >> 24) & 1;
568
569 if ((op4 & 1) == 1) {
570 opcode << "ldrd";
571 } else {
572 opcode << "strd";
573 }
574 args << Rt << "," << Rd << ", [" << Rn;
575 const char *sign = U ? "+" : "-";
576 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000577 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700578 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000579 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700580 if (W == 1) {
581 args << "!";
582 }
583 }
584 } else { // 0x
585 switch (op4) {
586 case 0:
587 if (op3 == 0) { // op3 is 00, op4 is 00
588 opcode << "strex";
589 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000590 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
591 Rd.r == Rn.r || Rd.r == Rt.r) {
592 args << " (UNPREDICTABLE)";
593 }
Dave Allison70202782013-10-22 17:52:19 -0700594 } else { // op3 is 01, op4 is 00
595 // this is one of strexb, strexh or strexd
596 int op5 = (instr >> 4) & 0xf;
597 switch (op5) {
598 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700599 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000600 opcode << ((op5 == 4) ? "strexb" : "strexh");
601 Rd = ArmRegister(instr, 0);
602 args << Rd << ", " << Rt << ", [" << Rn << "]";
603 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
604 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
605 args << " (UNPREDICTABLE)";
606 }
Dave Allison70202782013-10-22 17:52:19 -0700607 break;
608 case 7:
609 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000610 ArmRegister Rt2 = Rd;
611 Rd = ArmRegister(instr, 0);
612 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
613 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
614 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
615 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
616 args << " (UNPREDICTABLE)";
617 }
Dave Allison70202782013-10-22 17:52:19 -0700618 break;
619 }
620 }
621 break;
622 case 1:
623 if (op3 == 0) { // op3 is 00, op4 is 01
624 opcode << "ldrex";
625 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000626 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
627 args << " (UNPREDICTABLE)";
628 }
Dave Allison70202782013-10-22 17:52:19 -0700629 } else { // op3 is 01, op4 is 01
630 // this is one of strexb, strexh or strexd
631 int op5 = (instr >> 4) & 0xf;
632 switch (op5) {
633 case 0:
634 opcode << "tbb";
635 break;
636 case 1:
637 opcode << "tbh";
638 break;
639 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700640 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000641 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
642 args << Rt << ", [" << Rn << "]";
643 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
644 args << " (UNPREDICTABLE)";
645 }
Dave Allison70202782013-10-22 17:52:19 -0700646 break;
647 case 7:
648 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000649 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
650 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
651 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
652 args << " (UNPREDICTABLE)";
653 }
Dave Allison70202782013-10-22 17:52:19 -0700654 break;
655 }
656 }
657 break;
658 case 2: // op3 is 0x, op4 is 10
659 case 3: // op3 is 0x, op4 is 11
660 if (op4 == 2) {
661 opcode << "strd";
662 } else {
663 opcode << "ldrd";
664 }
665 int W = (instr >> 21) & 1;
666 int U = (instr >> 23) & 1;
667 int P = (instr >> 24) & 1;
668
669 args << Rt << "," << Rd << ", [" << Rn;
670 const char *sign = U ? "+" : "-";
671 if (P == 0 && W == 1) {
672 args << "], #" << sign << imm8;
673 } else {
674 args << ", #" << sign << imm8 << "]";
675 if (W == 1) {
676 args << "!";
677 }
678 }
679 break;
680 }
681 }
682
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700683 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
684 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100685 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
686 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
687 // |---|----|----|-|----|----|----|--|--|----|
688 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
689 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
690 // |---|----|----|-|----|----|----|--|--|----|
691 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700692 uint32_t op3 = (instr >> 21) & 0xF;
693 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100694 uint32_t imm3 = ((instr >> 12) & 0x7);
695 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700696 uint32_t imm5 = ((imm3 << 2) | imm2);
697 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700698 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100699 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700700 ArmRegister Rm(instr, 0);
701 switch (op3) {
702 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100703 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700704 opcode << "and";
705 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700706 if (S != 1U) {
707 opcode << "UNKNOWN TST-" << S;
708 break;
709 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700710 opcode << "tst";
711 S = 0; // don't print 's'
712 }
713 break;
714 case 0x1: opcode << "bic"; break;
715 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100716 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700717 opcode << "orr";
718 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100719 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700720 opcode << "mov";
721 }
722 break;
723 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100724 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700725 opcode << "orn";
726 } else {
727 opcode << "mvn";
728 }
729 break;
730 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100731 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700732 opcode << "eor";
733 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700734 if (S != 1U) {
735 opcode << "UNKNOWN TEQ-" << S;
736 break;
737 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700738 opcode << "teq";
739 S = 0; // don't print 's'
740 }
741 break;
742 case 0x6: opcode << "pkh"; break;
743 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100744 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700745 opcode << "add";
746 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700747 if (S != 1U) {
748 opcode << "UNKNOWN CMN-" << S;
749 break;
750 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700751 opcode << "cmn";
752 S = 0; // don't print 's'
753 }
754 break;
755 case 0xA: opcode << "adc"; break;
756 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100757 case 0xD:
758 if (Rd.r != 0xF) {
759 opcode << "sub";
760 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700761 if (S != 1U) {
762 opcode << "UNKNOWN CMP-" << S;
763 break;
764 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100765 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100766 S = 0; // don't print 's'
767 }
768 break;
769 case 0xE: opcode << "rsb"; break;
770 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700771 }
Ian Rogers087b2412012-03-21 01:30:32 -0700772
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700773 if (S == 1) {
774 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700775 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700776 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100777
778 if (Rd.r != 0xF) {
779 args << Rd << ", ";
780 }
781 if (Rn.r != 0xF) {
782 args << Rn << ", ";
783 }
784 args << Rm;
785
786 // Shift operand.
Vladimir Marko194bcfe2016-07-11 15:52:00 +0100787 bool noShift = (imm5 == 0 && shift_type == 0x0);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100788 if (!noShift) {
789 args << ", ";
Vladimir Marko194bcfe2016-07-11 15:52:00 +0100790 if (shift_type == 0x3u && imm5 == 0u) {
791 args << "rrx";
792 } else {
793 args << kThumb2ShiftOperations[shift_type] << " #" << ((0 != imm5) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100794 }
795 }
796
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700797 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
798 // Co-processor instructions
799 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
800 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
801 // |---|-|--|------|----|----|----|---|---|----|
802 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
803 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
804 // |---|-|--|------|----|----|----|---|---|----|
805 // |111| |11| op3 | Rn | |copr| |op4| |
806 uint32_t op3 = (instr >> 20) & 0x3F;
807 uint32_t coproc = (instr >> 8) & 0xF;
808 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700809
Ian Rogersef6a7762013-12-19 17:58:05 -0800810 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000811 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
812 // Extension register load/store instructions
813 // |1111|110|00000|0000|1111|110|0|00000000|
814 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
815 // |----|---|-----|----|----|---|-|--------|
816 // |3322|222|22222|1111|1111|110|0|00000000|
817 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
818 // |----|---|-----|----|----|---|-|--------|
819 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700820 uint32_t P = (instr >> 24) & 1;
821 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700822 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000823 if (P == U && W == 1) {
824 opcode << "UNDEFINED";
825 } else {
826 uint32_t L = (instr >> 20) & 1;
827 uint32_t S = (instr >> 8) & 1;
828 ArmRegister Rn(instr, 16);
829 if (P == 1 && W == 0) { // VLDR
830 FpRegister d(instr, 12, 22);
831 uint32_t imm8 = instr & 0xFF;
832 opcode << (L == 1 ? "vldr" : "vstr");
833 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
834 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800835 if (Rn.r == 15 && U == 1) {
Aart Bikd3059e72016-05-11 10:30:47 -0700836 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm8 << 2, kT2LitHexLong);
Ian Rogersef6a7762013-12-19 17:58:05 -0800837 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000838 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
839 opcode << (L == 1 ? "vpop" : "vpush");
840 args << FpRegisterRange(instr);
841 } else { // VLDM
842 opcode << (L == 1 ? "vldm" : "vstm");
843 args << Rn << ((W == 1) ? "!" : "") << ", "
844 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700845 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000846 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700847 }
Dave Allison70202782013-10-22 17:52:19 -0700848 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000849 if ((instr & 0xD0) == 0x10) {
850 // 64bit transfers between ARM core and extension registers.
851 uint32_t L = (instr >> 20) & 1;
852 uint32_t S = (instr >> 8) & 1;
853 ArmRegister Rt2(instr, 16);
854 ArmRegister Rt(instr, 12);
855 FpRegister m(instr, 0, 5);
856 opcode << "vmov" << (S ? ".f64" : ".f32");
857 if (L == 1) {
858 args << Rt << ", " << Rt2 << ", ";
859 }
860 if (S) {
861 args << m;
862 } else {
863 args << m << ", " << FpRegister(m, 1);
864 }
865 if (L == 0) {
866 args << ", " << Rt << ", " << Rt2;
867 }
868 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
869 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
870 args << " (UNPREDICTABLE)";
871 }
872 }
Dave Allison70202782013-10-22 17:52:19 -0700873 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
874 // fp data processing
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100875 // VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
876 // |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
877 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
878 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
879 // |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
880 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
881 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
882 // |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
883 // |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
884 // |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
885 // |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
886 // |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
887 // |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
888 // |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
889 // |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
890 // |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
891 // |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
892 uint32_t S = (instr >> 8) & 1;
893 uint32_t Q = (instr >> 6) & 1;
894 FpRegister d(instr, 12, 22);
895 FpRegister n(instr, 16, 7);
896 FpRegister m(instr, 0, 5);
Zheng Xue19649a2014-02-27 13:30:55 +0000897 if ((op3 & 0xB) == 0) { // 100x00
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100898 opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
Zheng Xue19649a2014-02-27 13:30:55 +0000899 args << d << ", " << n << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100900 } else if ((op3 & 0xB) == 0x2) { // 100x10
901 opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
902 args << d << ", " << n << ", " << m;
903 } else if ((op3 & 0xB) == 0x3) { // 100x11
904 opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
905 args << d << ", " << n << ", " << m;
906 } else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
907 opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
908 args << d << ", " << n << ", " << m;
909 } else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
910 uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
911 opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
912 args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
913 : StringPrintf("0x%08x", VFPExpand32(imm8)));
914 if ((instr & 0xa0) != 0) {
915 args << " (UNPREDICTABLE)";
916 }
917 } else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
918 // VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
919 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
920 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
921 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
922 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
923 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
924 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
925 // |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
926 // |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
927 // |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
928 // |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
929 // |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
930 // |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
931 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
932 uint32_t op5 = (instr >> 16) & 0xF;
933 uint32_t op = (instr >> 7) & 1;
934 // Register types in VCVT instructions rely on the combination of op5 and S.
935 FpRegister Dd(instr, 12, 22, 1);
936 FpRegister Sd(instr, 12, 22, 0);
937 FpRegister Dm(instr, 0, 5, 1);
938 FpRegister Sm(instr, 0, 5, 0);
939 if (op5 == 0) {
940 opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
941 args << d << ", " << m;
942 } else if (op5 == 1) {
943 opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
944 args << d << ", " << m;
945 } else if (op5 == 4) {
Vladimir Marko37dd80d2016-08-01 17:41:45 +0100946 opcode << "vcmp" << ((op != 0) ? "e" : "") << (S != 0 ? ".f64" : ".f32");
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100947 args << d << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100948 } else if (op5 == 5) {
Vladimir Marko37dd80d2016-08-01 17:41:45 +0100949 opcode << "vcmp" << ((op != 0) ? "e" : "") << (S != 0 ? ".f64" : ".f32");
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100950 args << d << ", #0.0";
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100951 if ((instr & 0x2f) != 0) {
952 args << " (UNPREDICTABLE)";
953 }
954 } else if (op5 == 0xD) {
955 if (S == 1) {
956 // vcvt{r}.s32.f64
957 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
958 args << Sd << ", " << Dm;
959 } else {
960 // vcvt{r}.s32.f32
961 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
962 args << Sd << ", " << Sm;
963 }
964 } else if (op5 == 0xC) {
965 if (S == 1) {
966 // vcvt{r}.u32.f64
967 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
968 args << Sd << ", " << Dm;
969 } else {
970 // vcvt{r}.u32.f32
971 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
972 args << Sd << ", " << Sm;
973 }
974 } else if (op5 == 0x8) {
975 if (S == 1) {
976 // vcvt.f64.<Tm>
977 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
978 args << Dd << ", " << Sm;
979 } else {
980 // vcvt.f32.<Tm>
981 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
982 args << Sd << ", " << Sm;
983 }
984 } else if (op5 == 0x7) {
985 if (op == 1) {
Zheng Xue19649a2014-02-27 13:30:55 +0000986 if (S == 1) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100987 // vcvt.f64.f32
988 opcode << "vcvt.f64.f32";
Zheng Xue19649a2014-02-27 13:30:55 +0000989 args << Dd << ", " << Sm;
990 } else {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100991 // vcvt.f32.f64
992 opcode << "vcvt.f32.f64";
993 args << Sd << ", " << Dm;
Zheng Xue19649a2014-02-27 13:30:55 +0000994 }
995 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100996 } else if ((op5 & 0xa) == 0xa) {
997 opcode << "vcvt";
998 args << "[undecoded: floating <-> fixed]";
Zheng Xue19649a2014-02-27 13:30:55 +0000999 }
1000 }
Dave Allison70202782013-10-22 17:52:19 -07001001 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +00001002 if (coproc == 10 && (op3 & 0xE) == 0) {
1003 // VMOV (between ARM core register and single-precision register)
1004 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
1005 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
1006 // |----|----|---|- |----|----|----|-|--|-|----|
1007 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
1008 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
1009 // |----|----|---|- |----|----|----|-|--|-|----|
1010 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
1011 uint32_t op = op3 & 1;
1012 ArmRegister Rt(instr, 12);
1013 FpRegister n(instr, 16, 7);
1014 opcode << "vmov.f32";
1015 if (op) {
1016 args << Rt << ", " << n;
1017 } else {
1018 args << n << ", " << Rt;
1019 }
1020 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
1021 args << " (UNPREDICTABLE)";
1022 }
1023 } else if (coproc == 10 && op3 == 0x2F) {
1024 // VMRS
1025 // |1111|11000000|0000|1111|1100|000|0|0000|
1026 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
1027 // |----|--------|----|----|----|---|-|----|
1028 // |3322|22222222|1111|1111|1100|000|0|0000|
1029 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
1030 // |----|--------|----|----|----|---|-|----|
1031 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
1032 uint32_t spec_reg = (instr >> 16) & 0xF;
1033 ArmRegister Rt(instr, 12);
1034 opcode << "vmrs";
1035 if (spec_reg == 1) {
1036 if (Rt.r == 15) {
1037 args << "APSR_nzcv, FPSCR";
1038 } else if (Rt.r == 13) {
1039 args << Rt << ", FPSCR (UNPREDICTABLE)";
1040 } else {
1041 args << Rt << ", FPSCR";
1042 }
1043 } else {
1044 args << "(PRIVILEGED)";
1045 }
1046 } else if (coproc == 11 && (op3 & 0x9) != 8) {
1047 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
1048 }
Ian Rogers9af89402012-09-07 11:29:35 -07001049 }
Dave Allison70202782013-10-22 17:52:19 -07001050 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001051 }
1052 break;
Ian Rogers40627db2012-03-04 17:31:09 -08001053 case 2:
1054 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
1055 // Data-processing (modified immediate)
1056 // |111|11|10|0000|0|0000|1|111|1100|00000000|
1057 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
1058 // |---|--|--|----|-|----|-|---|----|--------|
1059 // |332|22|22|2222|2|1111|1|111|1100|00000000|
1060 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
1061 // |---|--|--|----|-|----|-|---|----|--------|
1062 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
1063 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -08001064 uint32_t i = (instr >> 26) & 1;
1065 uint32_t op3 = (instr >> 21) & 0xF;
1066 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001067 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -08001068 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001069 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001070 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001071 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
1072 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
1073 if (op3 == 0x2) {
1074 opcode << "mov";
1075 if (S == 1) {
1076 opcode << "s";
1077 }
1078 opcode << ".w";
1079 } else {
1080 opcode << "mvn";
1081 if (S == 1) {
1082 opcode << "s";
1083 }
1084 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001085 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001086 } else if (Rd.r == 0xF && S == 1 &&
1087 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
1088 if (op3 == 0x0) {
1089 opcode << "tst";
1090 } else if (op3 == 0x4) {
1091 opcode << "teq";
1092 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +00001093 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001094 } else {
1095 opcode << "cmp.w";
1096 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001097 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001098 } else {
1099 switch (op3) {
1100 case 0x0: opcode << "and"; break;
1101 case 0x1: opcode << "bic"; break;
1102 case 0x2: opcode << "orr"; break;
1103 case 0x3: opcode << "orn"; break;
1104 case 0x4: opcode << "eor"; break;
1105 case 0x8: opcode << "add"; break;
1106 case 0xA: opcode << "adc"; break;
1107 case 0xB: opcode << "sbc"; break;
1108 case 0xD: opcode << "sub"; break;
1109 case 0xE: opcode << "rsb"; break;
1110 default: opcode << "UNKNOWN DPMI-" << op3; break;
1111 }
1112 if (S == 1) {
1113 opcode << "s";
1114 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001115 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001116 }
Ian Rogers40627db2012-03-04 17:31:09 -08001117 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1118 // Data-processing (plain binary immediate)
1119 // |111|11|10|00000|0000|1|111110000000000|
1120 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1121 // |---|--|--|-----|----|-|---------------|
1122 // |332|22|22|22222|1111|1|111110000000000|
1123 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1124 // |---|--|--|-----|----|-|---------------|
1125 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1126 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001127 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001128 case 0x00: case 0x0A: {
1129 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001130 ArmRegister Rd(instr, 8);
1131 ArmRegister Rn(instr, 16);
1132 uint32_t i = (instr >> 26) & 1;
1133 uint32_t imm3 = (instr >> 12) & 0x7;
1134 uint32_t imm8 = instr & 0xFF;
1135 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1136 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001137 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001138 args << Rd << ", " << Rn << ", #" << imm12;
1139 } else {
1140 opcode << "adr";
1141 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001142 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001143 }
1144 break;
1145 }
Ian Rogers55019132013-02-08 01:05:23 -08001146 case 0x04: case 0x0C: {
1147 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001148 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001149 uint32_t i = (instr >> 26) & 1;
1150 uint32_t imm3 = (instr >> 12) & 0x7;
1151 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001152 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001153 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001154 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001155 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001156 break;
1157 }
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001158 case 0x16: case 0x14: case 0x1C: {
jeffhaoeae26912013-01-28 16:29:54 -08001159 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001160 // SBFX Rd, Rn, #lsb, #width - 111 10 0 11 010 0 nnnn 0 iii dddd ii 0 iiiii
1161 // UBFX Rd, Rn, #lsb, #width - 111 10 0 11 110 0 nnnn 0 iii dddd ii 0 iiiii
jeffhaoeae26912013-01-28 16:29:54 -08001162 ArmRegister Rd(instr, 8);
1163 ArmRegister Rn(instr, 16);
1164 uint32_t msb = instr & 0x1F;
1165 uint32_t imm2 = (instr >> 6) & 0x3;
1166 uint32_t imm3 = (instr >> 12) & 0x7;
1167 uint32_t lsb = (imm3 << 2) | imm2;
1168 uint32_t width = msb - lsb + 1;
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001169 if (op3 == 0x16) {
1170 if (Rn.r != 0xF) {
1171 opcode << "bfi";
1172 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1173 } else {
1174 opcode << "bfc";
1175 args << Rd << ", #" << lsb << ", #" << width;
1176 }
jeffhaoeae26912013-01-28 16:29:54 -08001177 } else {
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001178 opcode << ((op3 & 0x8) != 0u ? "ubfx" : "sbfx");
1179 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1180 if (Rd.r == 13 || Rd.r == 15 || Rn.r == 13 || Rn.r == 15 ||
1181 (instr & 0x04000020) != 0u) {
1182 args << " (UNPREDICTABLE)";
1183 }
jeffhaoeae26912013-01-28 16:29:54 -08001184 }
1185 break;
1186 }
Ian Rogers40627db2012-03-04 17:31:09 -08001187 default:
1188 break;
1189 }
1190 } else {
1191 // Branches and miscellaneous control
1192 // |111|11|1000000|0000|1|111|1100|00000000|
1193 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1194 // |---|--|-------|----|-|---|----|--------|
1195 // |332|22|2222222|1111|1|111|1100|00000000|
1196 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1197 // |---|--|-------|----|-|---|----|--------|
1198 // |111|10| op2 | |1|op3|op4 | |
1199
1200 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001201 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001202 switch (op3) {
1203 case 0:
1204 if ((op2 & 0x38) != 0x38) {
1205 // Conditional branch
1206 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1207 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1208 // |---|--|-|----|------|-|-|--|-|--|-----------|
1209 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1210 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1211 // |---|--|-|----|------|-|-|--|-|--|-----------|
1212 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1213 uint32_t S = (instr >> 26) & 1;
1214 uint32_t J2 = (instr >> 11) & 1;
1215 uint32_t J1 = (instr >> 13) & 1;
1216 uint32_t imm6 = (instr >> 16) & 0x3F;
1217 uint32_t imm11 = instr & 0x7FF;
1218 uint32_t cond = (instr >> 22) & 0xF;
1219 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1220 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001221 opcode << "b";
1222 DumpCond(opcode, cond);
1223 opcode << ".w";
1224 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001225 } else if (op2 == 0x3B) {
1226 // Miscellaneous control instructions
1227 uint32_t op5 = (instr >> 4) & 0xF;
1228 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001229 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1230 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1231 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001232 }
Ian Rogers40627db2012-03-04 17:31:09 -08001233 }
1234 break;
1235 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001236 if ((op2 & 0x38) == 0x38) {
1237 if (op2 == 0x7F) {
1238 opcode << "udf";
1239 }
1240 break;
1241 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001242 FALLTHROUGH_INTENDED; // Else deliberate fall-through to B.
Ian Rogersd0876a92013-02-08 11:30:38 -08001243 case 1: case 3: {
1244 // B
1245 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1246 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1247 // |---|--|-|----|------|--|--|-|--|-----------|
1248 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1249 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1250 // |---|--|-|----|------|--|--|-|--|-----------|
1251 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1252 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1253 uint32_t S = (instr >> 26) & 1;
1254 uint32_t cond = (instr >> 22) & 0xF;
1255 uint32_t J2 = (instr >> 11) & 1;
1256 uint32_t form = (instr >> 12) & 1;
1257 uint32_t J1 = (instr >> 13) & 1;
1258 uint32_t imm10 = (instr >> 16) & 0x3FF;
1259 uint32_t imm6 = (instr >> 16) & 0x3F;
1260 uint32_t imm11 = instr & 0x7FF;
1261 opcode << "b";
1262 int32_t imm32;
1263 if (form == 0) {
1264 DumpCond(opcode, cond);
1265 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1266 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1267 } else {
Vladimir Markocb55b292016-04-21 14:52:03 +01001268 uint32_t I1 = (J1 ^ S) ^ 1;
1269 uint32_t I2 = (J2 ^ S) ^ 1;
Ian Rogersd0876a92013-02-08 11:30:38 -08001270 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
Vladimir Markocb55b292016-04-21 14:52:03 +01001271 imm32 = (imm32 << 7) >> 7; // sign extend 25 bit immediate.
Ian Rogersd0876a92013-02-08 11:30:38 -08001272 }
1273 opcode << ".w";
1274 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001275 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001276 }
Ian Rogers40627db2012-03-04 17:31:09 -08001277 case 4: case 6: case 5: case 7: {
1278 // BL, BLX (immediate)
1279 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1280 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1281 // |---|--|-|----------|--|--|-|--|-----------|
1282 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1283 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1284 // |---|--|-|----------|--|--|-|--|-----------|
Dave Allisond6ed6422014-04-09 23:36:15 +00001285 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
Ian Rogers40627db2012-03-04 17:31:09 -08001286 uint32_t S = (instr >> 26) & 1;
1287 uint32_t J2 = (instr >> 11) & 1;
Dave Allisond6ed6422014-04-09 23:36:15 +00001288 uint32_t L = (instr >> 12) & 1;
Ian Rogers40627db2012-03-04 17:31:09 -08001289 uint32_t J1 = (instr >> 13) & 1;
1290 uint32_t imm10 = (instr >> 16) & 0x3FF;
1291 uint32_t imm11 = instr & 0x7FF;
Dave Allisond6ed6422014-04-09 23:36:15 +00001292 if (L == 0) {
1293 opcode << "bx";
Dave Allisonf9487c02014-04-08 23:08:12 +00001294 } else {
Dave Allisond6ed6422014-04-09 23:36:15 +00001295 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001296 }
1297 uint32_t I1 = ~(J1 ^ S);
1298 uint32_t I2 = ~(J2 ^ S);
1299 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1300 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001301 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001302 break;
1303 }
1304 }
1305 }
1306 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001307 case 3:
1308 switch (op2) {
Vladimir Marko55d7c182015-01-05 15:17:01 +00001309 case 0x07: case 0x0F: case 0x17: case 0x1F: { // Explicitly UNDEFINED, A6.3.
1310 opcode << "UNDEFINED";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001311 break;
1312 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001313 case 0x06: case 0x0E: { // "Store single data item" undefined opcodes, A6.3.10.
1314 opcode << "UNDEFINED [store]";
1315 break;
1316 }
1317 case 0x15: case 0x1D: { // "Load word" undefined opcodes, A6.3.7.
1318 opcode << "UNDEFINED [load]";
1319 break;
1320 }
1321 case 0x10: case 0x12: case 0x14: case 0x16: case 0x18: case 0x1A: case 0x1C: case 0x1E: {
1322 opcode << "UNKNOWN " << op2 << " [SIMD]";
1323 break;
1324 }
1325 case 0x01: case 0x00: case 0x09: case 0x08: // {LD,ST}RB{,T}
1326 case 0x03: case 0x02: case 0x0B: case 0x0A: // {LD,ST}RH{,T}
1327 case 0x05: case 0x04: case 0x0D: case 0x0C: // {LD,ST}R{,T}
1328 case 0x11: case 0x19: // LDRSB{,T} (no signed store)
1329 case 0x13: case 0x1B: { // LDRSH{,T} (no signed store)
1330 // Load:
1331 // (Store is the same except that l==0 and always s==0 below.)
1332 // 00s.whl (sign, word, half, load)
1333 // LDR{S}B imm12: 11111|00s1001| Rn | Rt |imm12 (0x09)
1334 // LDR{S}B imm8: 11111|00s0001| Rn | Rt |1PUW|imm8 (0x01)
1335 // LDR{S}BT imm8: 11111|00s0001| Rn | Rt |1110|imm8 (0x01)
1336 // LDR{S}B lit: 11111|00sU001|1111| Rt |imm12 (0x01/0x09)
1337 // LDR{S}B reg: 11111|00s0001| Rn | Rt |000000|imm2| Rm (0x01)
1338 // LDR{S}H imm12: 11111|00s1011| Rn | Rt |imm12 (0x0B)
1339 // LDR{S}H imm8: 11111|00s0011| Rn | Rt |1PUW|imm8 (0x03)
1340 // LDR{S}HT imm8: 11111|00s0011| Rn | Rt |1110|imm8 (0x03)
1341 // LDR{S}H lit: 11111|00sU011|1111| Rt |imm12 (0x03/0x0B)
1342 // LDR{S}H reg: 11111|00s0011| Rn | Rt |000000|imm2| Rm (0x03)
1343 // LDR imm12: 11111|0001101| Rn | Rt |imm12 (0x0D)
1344 // LDR imm8: 11111|0000101| Rn | Rt |1PUW|imm8 (0x05)
1345 // LDRT imm8: 11111|0000101| Rn | Rt |1110|imm8 (0x05)
1346 // LDR lit: 11111|000U101|1111| Rt |imm12 (0x05/0x0D)
1347 // LDR reg: 11111|0000101| Rn | Rt |000000|imm2| Rm (0x05)
1348 //
1349 // If Rt == 15, instead of load we have preload:
1350 // PLD{W} imm12: 11111|00010W1| Rn |1111|imm12 (0x09/0x0B)
1351 // PLD{W} imm8: 11111|00000W1| Rn |1111|1100|imm8 (0x01/0x03); -imm8
1352 // PLD lit: 11111|000U001|1111|1111|imm12 (0x01/0x09)
1353 // PLD{W} reg: 11111|00000W1| Rn |1111|000000|imm2| Rm (0x01/0x03)
1354 // PLI imm12: 11111|0011001| Rn |1111|imm12 (0x19)
1355 // PLI imm8: 11111|0010001| Rn |1111|1100|imm8 (0x11); -imm8
1356 // PLI lit: 11111|001U001|1111|1111|imm12 (0x01/0x09)
1357 // PLI reg: 11111|0010001| Rn |1111|000000|imm2| Rm (0x01/0x03)
1358
1359 bool is_load = HasBitSet(instr, 20);
1360 bool is_half = HasBitSet(instr, 21); // W for PLD/PLDW.
1361 bool is_word = HasBitSet(instr, 22);
1362 bool is_signed = HasBitSet(instr, 24);
jeffhaoeae26912013-01-28 16:29:54 -08001363 ArmRegister Rn(instr, 16);
1364 ArmRegister Rt(instr, 12);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001365 uint32_t imm12 = instr & 0xFFF;
1366 uint32_t U = (instr >> 23) & 1; // U for imm12
1367 uint32_t imm8 = instr & 0xFF;
1368 uint32_t op4 = (instr >> 8) & 0xF; // 1PUW for imm8
1369 if (Rt.r == PC && is_load && !is_word) {
1370 // PLD, PLDW, PLI
1371 const char* pld_pli = (is_signed ? "pli" : "pld");
1372 const char* w = (is_half ? "w" : "");
1373 if (is_signed && !is_half) {
1374 opcode << "UNDEFINED [PLI+W]";
1375 } else if (Rn.r == PC || U != 0u) {
1376 opcode << pld_pli << w;
1377 args << "[" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1378 if (Rn.r == PC && is_half) {
1379 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001380 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001381 } else if ((instr & 0xFC0) == 0) {
1382 opcode << pld_pli << w;
1383 RmLslImm2 Rm(instr);
1384 args << "[" << Rn << ", " << Rm << "]";
1385 } else if (op4 == 0xC) {
1386 opcode << pld_pli << w;
1387 args << "[" << Rn << ", #-" << imm8 << "]";
1388 } else {
1389 opcode << "UNDEFINED [~" << pld_pli << "]";
jeffhaoeae26912013-01-28 16:29:54 -08001390 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001391 break;
1392 }
1393 const char* ldr_str = is_load ? "ldr" : "str";
1394 const char* sign = is_signed ? "s" : "";
1395 const char* type = is_word ? "" : is_half ? "h" : "b";
1396 bool unpred = (Rt.r == SP && !is_word) || (Rt.r == PC && !is_load);
1397 if (Rn.r == PC && !is_load) {
1398 opcode << "UNDEFINED [STR-lit]";
1399 unpred = false;
1400 } else if (Rn.r == PC || U != 0u) {
1401 // Load/store with imm12 (load literal if Rn.r == PC; there's no store literal).
1402 opcode << ldr_str << sign << type << ".w";
1403 args << Rt << ", [" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1404 if (Rn.r == TR && is_load) {
1405 args << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -07001406 GetDisassemblerOptions()->thread_offset_name_function_(args, imm12);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001407 } else if (Rn.r == PC) {
1408 T2LitType lit_type[] = {
1409 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1410 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1411 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1412 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1413 };
1414 DCHECK_LT(op2 >> 1, arraysize(lit_type));
1415 DCHECK_NE(lit_type[op2 >> 1], kT2LitInvalid);
Aart Bikd3059e72016-05-11 10:30:47 -07001416 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm12, lit_type[op2 >> 1]);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001417 }
1418 } else if ((instr & 0xFC0) == 0) {
1419 opcode << ldr_str << sign << type << ".w";
1420 RmLslImm2 Rm(instr);
1421 args << Rt << ", [" << Rn << ", " << Rm << "]";
1422 unpred = unpred || (Rm.rm.r == SP) || (Rm.rm.r == PC);
1423 } else if (is_word && Rn.r == SP && imm8 == 4 && op4 == (is_load ? 0xB : 0xD)) {
1424 opcode << (is_load ? "pop" : "push") << ".w";
1425 args << Rn;
1426 unpred = unpred || (Rn.r == SP);
1427 } else if ((op4 & 5) == 0) {
1428 opcode << "UNDEFINED [P = W = 0 for " << ldr_str << "]";
1429 unpred = false;
1430 } else {
1431 uint32_t P = (instr >> 10) & 1;
1432 U = (instr >> 9) & 1;
1433 uint32_t W = (instr >> 8) & 1;
1434 bool pre_index = (P != 0 && W == 1);
1435 bool post_index = (P == 0 && W == 1);
1436 const char* t = (P != 0 && U != 0 && W == 0) ? "t" : ""; // Unprivileged load/store?
1437 opcode << ldr_str << sign << type << t << ".w";
1438 args << Rt << ", [" << Rn << (post_index ? "]" : "") << ", #" << (U != 0 ? "" : "-")
1439 << imm8 << (post_index ? "" : "]") << (pre_index ? "!" : "");
1440 unpred = (W != 0 && Rn.r == Rt.r);
1441 }
1442 if (unpred) {
1443 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001444 }
1445 break;
1446 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001447 case 0x29: { // 0101001
1448 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1449 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1450 // |---|--|-------|----|----|----|--|---|----|
1451 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1452 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1453 // |---|--|-------|----|----|----|--|---|----|
1454 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1455 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1456 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1457 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1458 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1459 if ((instr & 0xf0c0) == 0xf080) {
1460 uint32_t op3 = (instr >> 4) & 3;
1461 opcode << kThumbReverseOperations[op3];
1462 ArmRegister Rm(instr, 0);
1463 ArmRegister Rd(instr, 8);
1464 args << Rd << ", " << Rm;
1465 ArmRegister Rm2(instr, 16);
1466 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1467 args << " (UNPREDICTABLE)";
1468 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001469 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001470 break;
1471 }
Scott Wakeling611d3392015-07-10 11:42:06 +01001472 case 0x2B: { // 0101011
1473 // CLZ - 111 11 0101011 mmmm 1111 dddd 1000 mmmm
1474 if ((instr & 0xf0f0) == 0xf080) {
1475 opcode << "clz";
1476 ArmRegister Rm(instr, 0);
1477 ArmRegister Rd(instr, 8);
1478 args << Rd << ", " << Rm;
1479 ArmRegister Rm2(instr, 16);
1480 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1481 args << " (UNPREDICTABLE)";
1482 }
1483 }
1484 break;
1485 }
xueliang.zhonge652c122016-06-13 14:42:27 +01001486 case 0x7B: case 0x7F: {
1487 FpRegister d(instr, 12, 22);
1488 FpRegister m(instr, 0, 5);
1489 uint32_t sz = (instr >> 18) & 0x3; // Decode size bits.
1490 uint32_t size = (sz == 0) ? 8 : sz << 4;
1491 uint32_t opc2 = (instr >> 7) & 0xF;
1492 uint32_t Q = (instr >> 6) & 1;
1493 if (Q == 0 && opc2 == 0xA && size == 8) { // 1010, VCNT
1494 opcode << "vcnt." << size;
1495 args << d << ", " << m;
1496 } else if (Q == 0 && (opc2 == 0x4 || opc2 == 0x5) && size <= 32) { // 010x, VPADDL
1497 bool op = HasBitSet(instr, 7);
1498 opcode << "vpaddl." << (op ? "u" : "s") << size;
1499 args << d << ", " << m;
1500 } else {
1501 opcode << "UNKNOWN " << op2;
1502 }
1503 break;
1504 }
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001505 default: // more formats
1506 if ((op2 >> 4) == 2) { // 010xxxx
1507 // data processing (register)
1508 if ((instr & 0x0080f0f0) == 0x0000f000) {
1509 // LSL, LSR, ASR, ROR
1510 uint32_t shift_op = (instr >> 21) & 3;
1511 uint32_t S = (instr >> 20) & 1;
1512 ArmRegister Rd(instr, 8);
1513 ArmRegister Rn(instr, 16);
1514 ArmRegister Rm(instr, 0);
1515 opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
1516 args << Rd << ", " << Rn << ", " << Rm;
1517 }
1518 } else if ((op2 >> 3) == 6) { // 0110xxx
1519 // Multiply, multiply accumulate, and absolute difference
1520 op1 = (instr >> 20) & 0x7;
1521 op2 = (instr >> 4) & 0x1;
1522 ArmRegister Ra(instr, 12);
Vladimir Markoc777e0d2014-04-03 17:59:02 +01001523 ArmRegister Rn(instr, 16);
1524 ArmRegister Rm(instr, 0);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001525 ArmRegister Rd(instr, 8);
1526 switch (op1) {
1527 case 0:
1528 if (op2 == 0) {
1529 if (Ra.r == 0xf) {
1530 opcode << "mul";
1531 args << Rd << ", " << Rn << ", " << Rm;
1532 } else {
1533 opcode << "mla";
1534 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1535 }
Dave Allison70202782013-10-22 17:52:19 -07001536 } else {
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001537 opcode << "mls";
Dave Allison70202782013-10-22 17:52:19 -07001538 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1539 }
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001540 break;
1541 case 1:
1542 case 2:
1543 case 3:
1544 case 4:
1545 case 5:
1546 case 6:
1547 break; // do these sometime
Dave Allison70202782013-10-22 17:52:19 -07001548 }
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001549 } else if ((op2 >> 3) == 7) { // 0111xxx
1550 // Long multiply, long multiply accumulate, and divide
1551 op1 = (instr >> 20) & 0x7;
1552 op2 = (instr >> 4) & 0xf;
1553 ArmRegister Rn(instr, 16);
1554 ArmRegister Rm(instr, 0);
1555 ArmRegister Rd(instr, 8);
1556 ArmRegister RdHi(instr, 8);
1557 ArmRegister RdLo(instr, 12);
1558 switch (op1) {
1559 case 0:
1560 opcode << "smull";
1561 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1562 break;
1563 case 1:
1564 opcode << "sdiv";
1565 args << Rd << ", " << Rn << ", " << Rm;
1566 break;
1567 case 2:
1568 opcode << "umull";
1569 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1570 break;
1571 case 3:
1572 opcode << "udiv";
1573 args << Rd << ", " << Rn << ", " << Rm;
1574 break;
1575 case 4:
1576 case 5:
1577 case 6:
1578 break; // TODO: when we generate these...
1579 }
Dave Allison70202782013-10-22 17:52:19 -07001580 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001581 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001582 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001583 default:
1584 break;
1585 }
Ian Rogers9af89402012-09-07 11:29:35 -07001586
1587 // Apply any IT-block conditions to the opcode if necessary.
1588 if (!it_conditions_.empty()) {
1589 opcode << it_conditions_.back();
1590 it_conditions_.pop_back();
1591 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001592 if (opcode.str().size() == 0) {
1593 opcode << "UNKNOWN " << op2;
1594 }
Ian Rogers9af89402012-09-07 11:29:35 -07001595
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001596 os << FormatInstructionPointer(instr_ptr)
1597 << StringPrintf(": %08x\t%-7s ", instr, opcode.str().c_str())
1598 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001599 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001600} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001601
1602size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1603 uint16_t instr = ReadU16(instr_ptr);
1604 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1605 if (is_32bit) {
1606 return DumpThumb32(os, instr_ptr);
1607 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001608 std::ostringstream opcode;
1609 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001610 uint16_t opcode1 = instr >> 10;
1611 if (opcode1 < 0x10) {
1612 // shift (immediate), add, subtract, move, and compare
1613 uint16_t opcode2 = instr >> 9;
1614 switch (opcode2) {
1615 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1616 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001617 // Logical shift left - 00 000xx iii mmm ddd
1618 // Logical shift right - 00 001xx iii mmm ddd
1619 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001621 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001622 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001623 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001624 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001625 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001626 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001627 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001628 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001629 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001630 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001631 break;
1632 }
1633 case 0xC: case 0xD: case 0xE: case 0xF: {
1634 // Add register - 00 01100 mmm nnn ddd
1635 // Sub register - 00 01101 mmm nnn ddd
1636 // Add 3-bit immediate - 00 01110 iii nnn ddd
1637 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1638 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001639 ThumbRegister Rn(instr, 3);
1640 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001641 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001642 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001643 } else {
1644 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001645 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001646 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001647 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 }
1649 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001650 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001651 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001652 ArmRegister Rm(imm3_or_Rm);
1653 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001655 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001656 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001657 break;
1658 }
1659 case 0x10: case 0x11: case 0x12: case 0x13:
1660 case 0x14: case 0x15: case 0x16: case 0x17:
1661 case 0x18: case 0x19: case 0x1A: case 0x1B:
1662 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1663 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1664 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1665 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1666 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001667 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001668 uint16_t imm8 = instr & 0xFF;
1669 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001670 case 4: opcode << "movs"; break;
1671 case 5: opcode << "cmp"; break;
1672 case 6: opcode << "adds"; break;
1673 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001674 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001675 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001676 break;
1677 }
1678 default:
1679 break;
1680 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001681 } else if (opcode1 == 0x10) {
1682 // Data-processing
1683 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001684 ThumbRegister rm(instr, 3);
1685 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001686 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001687 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001688 } else if (opcode1 == 0x11) {
1689 // Special data instructions and branch and exchange
1690 uint16_t opcode2 = (instr >> 6) & 0x0F;
1691 switch (opcode2) {
1692 case 0x0: case 0x1: case 0x2: case 0x3: {
1693 // Add low registers - 010001 0000 xxxxxx
1694 // Add high registers - 010001 0001/001x xxxxxx
1695 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001696 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001697 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001698 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001699 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001700 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001701 break;
1702 }
1703 case 0x8: case 0x9: case 0xA: case 0xB: {
1704 // Move low registers - 010001 1000 xxxxxx
1705 // Move high registers - 010001 1001/101x xxxxxx
1706 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001707 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001708 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001709 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001710 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001711 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001712 break;
1713 }
1714 case 0x5: case 0x6: case 0x7: {
1715 // Compare high registers - 010001 0101/011x xxxxxx
1716 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001717 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001718 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001719 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001720 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001721 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001722 break;
1723 }
1724 case 0xC: case 0xD: case 0xE: case 0xF: {
1725 // Branch and exchange - 010001 110x xxxxxx
1726 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001727 ArmRegister rm(instr, 3);
1728 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1729 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001730 break;
1731 }
1732 default:
1733 break;
1734 }
jeffhaoeae26912013-01-28 16:29:54 -08001735 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
Aart Bikd3059e72016-05-11 10:30:47 -07001736 const uintptr_t lo_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->base_address_);
1737 const uintptr_t hi_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->end_address_);
jeffhaoeae26912013-01-28 16:29:54 -08001738 ThumbRegister Rt(instr, 8);
1739 uint16_t imm8 = instr & 0xFF;
1740 opcode << "ldr";
1741 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Aart Bikd3059e72016-05-11 10:30:47 -07001742 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, /*U*/ 1u, imm8 << 2, kT2LitHexWord);
Ian Rogersd83bc362012-09-07 17:43:13 -07001743 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1744 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1745 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1746 // Load/store single data item
1747 uint16_t opA = (instr >> 12) & 0xF;
1748 if (opA == 0x5) {
1749 uint16_t opB = (instr >> 9) & 0x7;
1750 ThumbRegister Rm(instr, 6);
1751 ThumbRegister Rn(instr, 3);
1752 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001753 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001754 case 0: opcode << "str"; break;
1755 case 1: opcode << "strh"; break;
1756 case 2: opcode << "strb"; break;
1757 case 3: opcode << "ldrsb"; break;
1758 case 4: opcode << "ldr"; break;
1759 case 5: opcode << "ldrh"; break;
1760 case 6: opcode << "ldrb"; break;
1761 case 7: opcode << "ldrsh"; break;
1762 }
1763 args << Rt << ", [" << Rn << ", " << Rm << "]";
1764 } else if (opA == 9) {
1765 uint16_t opB = (instr >> 11) & 1;
1766 ThumbRegister Rt(instr, 8);
1767 uint16_t imm8 = instr & 0xFF;
1768 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001769 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001770 } else {
1771 uint16_t imm5 = (instr >> 6) & 0x1F;
1772 uint16_t opB = (instr >> 11) & 1;
1773 ThumbRegister Rn(instr, 3);
1774 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001775 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001776 case 6:
1777 imm5 <<= 2;
1778 opcode << (opB == 0 ? "str" : "ldr");
1779 break;
1780 case 7:
1781 imm5 <<= 0;
1782 opcode << (opB == 0 ? "strb" : "ldrb");
1783 break;
1784 case 8:
1785 imm5 <<= 1;
1786 opcode << (opB == 0 ? "strh" : "ldrh");
1787 break;
1788 }
1789 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1790 }
jeffhaoeae26912013-01-28 16:29:54 -08001791 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001792 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001793 uint32_t cond = (instr >> 8) & 0xF;
1794 opcode << "b";
1795 DumpCond(opcode, cond);
1796 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001797 } else if ((instr & 0xF800) == 0xA800) {
1798 // Generate SP-relative address
1799 ThumbRegister rd(instr, 8);
1800 int imm8 = instr & 0xFF;
1801 opcode << "add";
1802 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001803 } else if ((instr & 0xF000) == 0xB000) {
1804 // Miscellaneous 16-bit instructions
1805 uint16_t opcode2 = (instr >> 5) & 0x7F;
1806 switch (opcode2) {
1807 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1808 // Add immediate to SP - 1011 00000 ii iiiii
1809 // Subtract immediate from SP - 1011 00001 ii iiiii
1810 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001811 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001812 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001813 break;
1814 }
Ian Rogers087b2412012-03-21 01:30:32 -07001815 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001816 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001817 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1818 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001819 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001820 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1821 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1822 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001823 // CBNZ, CBZ
1824 uint16_t op = (instr >> 11) & 1;
1825 uint16_t i = (instr >> 9) & 1;
1826 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001827 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001828 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001829 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001830 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001831 DumpBranchTarget(args, instr_ptr + 4, imm32);
1832 break;
1833 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001834 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1835 case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F: {
1836 opcode << "push";
1837 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 6));
1838 break;
1839 }
1840 case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
1841 case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F: {
1842 opcode << "pop";
1843 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 7));
1844 break;
1845 }
1846 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: {
1847 opcode << "bkpt";
1848 args << "#" << (instr & 0xFF);
1849 break;
1850 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001851 case 0x50: case 0x51: // 101000x
1852 case 0x52: case 0x53: // 101001x
1853 case 0x56: case 0x57: { // 101011x
1854 uint16_t op = (instr >> 6) & 3;
1855 opcode << kThumbReverseOperations[op];
1856 ThumbRegister Rm(instr, 3);
1857 ThumbRegister Rd(instr, 0);
1858 args << Rd << ", " << Rm;
1859 break;
1860 }
Ian Rogers40627db2012-03-04 17:31:09 -08001861 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1862 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1863 // If-Then, and hints
1864 uint16_t opA = (instr >> 4) & 0xF;
1865 uint16_t opB = instr & 0xF;
1866 if (opB == 0) {
1867 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001868 case 0: opcode << "nop"; break;
1869 case 1: opcode << "yield"; break;
1870 case 2: opcode << "wfe"; break;
1871 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001872 default: break;
1873 }
1874 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001875 uint32_t first_cond = opA;
1876 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001877 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001878
1879 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1880 // and store up the actual condition codes we'll want to add to the next few opcodes.
1881 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001882 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001883 for (size_t i = 0; i < count; ++i) {
1884 bool positive_cond = ((first_cond & 1) != 0);
1885 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1886 if (positive_mask == positive_cond) {
1887 opcode << 't';
1888 it_conditions_[i] = kConditionCodeNames[first_cond];
1889 } else {
1890 opcode << 'e';
1891 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1892 }
1893 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001894 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001895
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001896 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1897 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001898 }
1899 break;
1900 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001901 default:
1902 break;
1903 }
1904 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1905 ((instr & 0xE000) == 0x8000)) {
1906 // Load/store single data item
1907 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001908 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001909 switch (opA) {
1910 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001911 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1912 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001913 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001914 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001915 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001916 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1917 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001918 break;
1919 }
1920 case 0x9: {
1921 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1922 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1923 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001924 ThumbRegister Rt(instr, 8);
1925 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1926 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001927 break;
1928 }
1929 default:
1930 break;
1931 }
Ian Rogers40627db2012-03-04 17:31:09 -08001932 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1933 uint16_t imm11 = instr & 0x7FFF;
1934 int32_t imm32 = imm11 << 1;
1935 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001936 opcode << "b";
1937 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001938 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001939
1940 // Apply any IT-block conditions to the opcode if necessary.
1941 if (!it_conditions_.empty()) {
1942 opcode << it_conditions_.back();
1943 it_conditions_.pop_back();
1944 }
1945
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001946 os << FormatInstructionPointer(instr_ptr)
1947 << StringPrintf(": %04x \t%-7s ", instr, opcode.str().c_str())
1948 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001949 }
1950 return 2;
1951}
1952
1953} // namespace arm
1954} // namespace art