blob: a47b6adcc97431f433d211c9720c8e5c838a1e0f [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"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070025#include "base/bit_utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080027#include "base/stringprintf.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 << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -0700331 GetDisassemblerOptions()->thread_offset_name_function_(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
Aart Bikd3059e72016-05-11 10:30:47 -0700420void DumpThumb2Literal(std::ostream& args,
421 const uint8_t* instr_ptr,
422 const uintptr_t lo_adr,
423 const uintptr_t hi_adr,
424 uint32_t U,
425 uint32_t imm32,
Vladimir Marko55d7c182015-01-05 15:17:01 +0000426 T2LitType type) {
427 // Literal offsets (imm32) are not required to be aligned so we may need unaligned access.
428 typedef const int16_t unaligned_int16_t __attribute__ ((aligned (1)));
429 typedef const uint16_t unaligned_uint16_t __attribute__ ((aligned (1)));
430 typedef const int32_t unaligned_int32_t __attribute__ ((aligned (1)));
431 typedef const uint32_t unaligned_uint32_t __attribute__ ((aligned (1)));
432 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (1)));
433 typedef const uint64_t unaligned_uint64_t __attribute__ ((aligned (1)));
434
Aart Bikd3059e72016-05-11 10:30:47 -0700435 // Get address of literal. Bail if not within expected buffer range to
436 // avoid trying to fetch invalid literals (we can encounter this when
437 // interpreting raw data as instructions).
Vladimir Marko55d7c182015-01-05 15:17:01 +0000438 uintptr_t pc = RoundDown(reinterpret_cast<intptr_t>(instr_ptr) + 4, 4);
439 uintptr_t lit_adr = U ? pc + imm32 : pc - imm32;
Aart Bikd3059e72016-05-11 10:30:47 -0700440 if (lit_adr < lo_adr || lit_adr >= hi_adr) {
441 args << " ; (?)";
442 return;
443 }
444
Vladimir Marko55d7c182015-01-05 15:17:01 +0000445 args << " ; ";
446 switch (type) {
447 case kT2LitUByte:
448 args << *reinterpret_cast<const uint8_t*>(lit_adr);
449 break;
450 case kT2LitSByte:
451 args << *reinterpret_cast<const int8_t*>(lit_adr);
452 break;
453 case kT2LitUHalf:
454 args << *reinterpret_cast<const unaligned_uint16_t*>(lit_adr);
455 break;
456 case kT2LitSHalf:
457 args << *reinterpret_cast<const unaligned_int16_t*>(lit_adr);
458 break;
459 case kT2LitUWord:
460 args << *reinterpret_cast<const unaligned_uint32_t*>(lit_adr);
461 break;
462 case kT2LitSWord:
463 args << *reinterpret_cast<const unaligned_int32_t*>(lit_adr);
464 break;
465 case kT2LitHexWord:
466 args << StringPrintf("0x%08x", *reinterpret_cast<const unaligned_uint32_t*>(lit_adr));
467 break;
468 case kT2LitULong:
469 args << *reinterpret_cast<const unaligned_uint64_t*>(lit_adr);
470 break;
471 case kT2LitSLong:
472 args << *reinterpret_cast<const unaligned_int64_t*>(lit_adr);
473 break;
474 case kT2LitHexLong:
475 args << StringPrintf("0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
476 break;
477 default:
478 LOG(FATAL) << "Invalid type: " << type;
479 break;
480 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100481}
482
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
484 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
485 // |111|1 1|1000000|0000|1111110000000000|
486 // |5 3|2 1|0987654|3 0|5 0 5 0|
487 // |---|---|-------|----|----------------|
488 // |332|2 2|2222222|1111|1111110000000000|
489 // |1 9|8 7|6543210|9 6|5 0 5 0|
490 // |---|---|-------|----|----------------|
491 // |111|op1| op2 | | |
492 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700493 if (op1 == 0) {
494 return DumpThumb16(os, instr_ptr);
495 }
496
Aart Bikd3059e72016-05-11 10:30:47 -0700497 // Set valid address range of backing buffer.
498 const uintptr_t lo_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->base_address_);
499 const uintptr_t hi_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->end_address_);
500
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800501 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700502 std::ostringstream opcode;
503 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800504 switch (op1) {
505 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 break;
507 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700508 if ((op2 & 0x64) == 0) { // 00x x0xx
509 // |111|11|10|00|0|00|0000|1111110000000000|
510 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
511 // |---|--|--|--|-|--|----|----------------|
512 // |332|22|22|22|2|22|1111|1111110000000000|
513 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
514 // |---|--|--|--|-|--|----|----------------|
515 // |111|01|00|op|0|WL| Rn | |
516 // |111|01| op2 | | |
517 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
518 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
519 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
520 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
521 uint32_t op = (instr >> 23) & 3;
522 uint32_t W = (instr >> 21) & 1;
523 uint32_t L = (instr >> 20) & 1;
524 ArmRegister Rn(instr, 16);
525 if (op == 1 || op == 2) {
526 if (op == 1) {
527 if (L == 0) {
528 opcode << "stm";
529 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700531 if (Rn.r != 13) {
532 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700533 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700534 } else {
535 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800536 }
537 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700538 } else {
539 if (L == 0) {
540 if (Rn.r != 13) {
541 opcode << "stmdb";
542 args << Rn << (W == 0 ? "" : "!") << ", ";
543 } else {
544 opcode << "push";
545 }
546 } else {
547 opcode << "ldmdb";
548 args << Rn << (W == 0 ? "" : "!") << ", ";
549 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700551 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700553 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700554 uint32_t op3 = (instr >> 23) & 3;
555 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700556 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700557 ArmRegister Rn(instr, 16);
558 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700559 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700560 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700561 if ((op3 & 2) == 2) { // 1x
562 int W = (instr >> 21) & 1;
563 int U = (instr >> 23) & 1;
564 int P = (instr >> 24) & 1;
565
566 if ((op4 & 1) == 1) {
567 opcode << "ldrd";
568 } else {
569 opcode << "strd";
570 }
571 args << Rt << "," << Rd << ", [" << Rn;
572 const char *sign = U ? "+" : "-";
573 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000574 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700575 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000576 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700577 if (W == 1) {
578 args << "!";
579 }
580 }
581 } else { // 0x
582 switch (op4) {
583 case 0:
584 if (op3 == 0) { // op3 is 00, op4 is 00
585 opcode << "strex";
586 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000587 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
588 Rd.r == Rn.r || Rd.r == Rt.r) {
589 args << " (UNPREDICTABLE)";
590 }
Dave Allison70202782013-10-22 17:52:19 -0700591 } else { // op3 is 01, op4 is 00
592 // this is one of strexb, strexh or strexd
593 int op5 = (instr >> 4) & 0xf;
594 switch (op5) {
595 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700596 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000597 opcode << ((op5 == 4) ? "strexb" : "strexh");
598 Rd = ArmRegister(instr, 0);
599 args << Rd << ", " << Rt << ", [" << Rn << "]";
600 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
601 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
602 args << " (UNPREDICTABLE)";
603 }
Dave Allison70202782013-10-22 17:52:19 -0700604 break;
605 case 7:
606 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000607 ArmRegister Rt2 = Rd;
608 Rd = ArmRegister(instr, 0);
609 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
610 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
611 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
612 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
613 args << " (UNPREDICTABLE)";
614 }
Dave Allison70202782013-10-22 17:52:19 -0700615 break;
616 }
617 }
618 break;
619 case 1:
620 if (op3 == 0) { // op3 is 00, op4 is 01
621 opcode << "ldrex";
622 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000623 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
624 args << " (UNPREDICTABLE)";
625 }
Dave Allison70202782013-10-22 17:52:19 -0700626 } else { // op3 is 01, op4 is 01
627 // this is one of strexb, strexh or strexd
628 int op5 = (instr >> 4) & 0xf;
629 switch (op5) {
630 case 0:
631 opcode << "tbb";
632 break;
633 case 1:
634 opcode << "tbh";
635 break;
636 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700637 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000638 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
639 args << Rt << ", [" << Rn << "]";
640 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
641 args << " (UNPREDICTABLE)";
642 }
Dave Allison70202782013-10-22 17:52:19 -0700643 break;
644 case 7:
645 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000646 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
647 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
648 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
649 args << " (UNPREDICTABLE)";
650 }
Dave Allison70202782013-10-22 17:52:19 -0700651 break;
652 }
653 }
654 break;
655 case 2: // op3 is 0x, op4 is 10
656 case 3: // op3 is 0x, op4 is 11
657 if (op4 == 2) {
658 opcode << "strd";
659 } else {
660 opcode << "ldrd";
661 }
662 int W = (instr >> 21) & 1;
663 int U = (instr >> 23) & 1;
664 int P = (instr >> 24) & 1;
665
666 args << Rt << "," << Rd << ", [" << Rn;
667 const char *sign = U ? "+" : "-";
668 if (P == 0 && W == 1) {
669 args << "], #" << sign << imm8;
670 } else {
671 args << ", #" << sign << imm8 << "]";
672 if (W == 1) {
673 args << "!";
674 }
675 }
676 break;
677 }
678 }
679
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700680 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
681 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100682 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
683 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
684 // |---|----|----|-|----|----|----|--|--|----|
685 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
686 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
687 // |---|----|----|-|----|----|----|--|--|----|
688 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700689 uint32_t op3 = (instr >> 21) & 0xF;
690 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100691 uint32_t imm3 = ((instr >> 12) & 0x7);
692 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700693 uint32_t imm5 = ((imm3 << 2) | imm2);
694 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700695 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100696 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700697 ArmRegister Rm(instr, 0);
698 switch (op3) {
699 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100700 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700701 opcode << "and";
702 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700703 if (S != 1U) {
704 opcode << "UNKNOWN TST-" << S;
705 break;
706 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700707 opcode << "tst";
708 S = 0; // don't print 's'
709 }
710 break;
711 case 0x1: opcode << "bic"; break;
712 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100713 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700714 opcode << "orr";
715 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100716 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700717 opcode << "mov";
718 }
719 break;
720 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100721 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700722 opcode << "orn";
723 } else {
724 opcode << "mvn";
725 }
726 break;
727 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100728 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700729 opcode << "eor";
730 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700731 if (S != 1U) {
732 opcode << "UNKNOWN TEQ-" << S;
733 break;
734 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700735 opcode << "teq";
736 S = 0; // don't print 's'
737 }
738 break;
739 case 0x6: opcode << "pkh"; break;
740 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100741 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700742 opcode << "add";
743 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700744 if (S != 1U) {
745 opcode << "UNKNOWN CMN-" << S;
746 break;
747 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700748 opcode << "cmn";
749 S = 0; // don't print 's'
750 }
751 break;
752 case 0xA: opcode << "adc"; break;
753 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100754 case 0xD:
755 if (Rd.r != 0xF) {
756 opcode << "sub";
757 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700758 if (S != 1U) {
759 opcode << "UNKNOWN CMP-" << S;
760 break;
761 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100762 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100763 S = 0; // don't print 's'
764 }
765 break;
766 case 0xE: opcode << "rsb"; break;
767 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700768 }
Ian Rogers087b2412012-03-21 01:30:32 -0700769
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700770 if (S == 1) {
771 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700772 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700773 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100774
775 if (Rd.r != 0xF) {
776 args << Rd << ", ";
777 }
778 if (Rn.r != 0xF) {
779 args << Rn << ", ";
780 }
781 args << Rm;
782
783 // Shift operand.
Vladimir Marko194bcfe2016-07-11 15:52:00 +0100784 bool noShift = (imm5 == 0 && shift_type == 0x0);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100785 if (!noShift) {
786 args << ", ";
Vladimir Marko194bcfe2016-07-11 15:52:00 +0100787 if (shift_type == 0x3u && imm5 == 0u) {
788 args << "rrx";
789 } else {
790 args << kThumb2ShiftOperations[shift_type] << " #" << ((0 != imm5) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100791 }
792 }
793
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700794 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
795 // Co-processor instructions
796 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
797 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
798 // |---|-|--|------|----|----|----|---|---|----|
799 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
800 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
801 // |---|-|--|------|----|----|----|---|---|----|
802 // |111| |11| op3 | Rn | |copr| |op4| |
803 uint32_t op3 = (instr >> 20) & 0x3F;
804 uint32_t coproc = (instr >> 8) & 0xF;
805 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700806
Ian Rogersef6a7762013-12-19 17:58:05 -0800807 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000808 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
809 // Extension register load/store instructions
810 // |1111|110|00000|0000|1111|110|0|00000000|
811 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
812 // |----|---|-----|----|----|---|-|--------|
813 // |3322|222|22222|1111|1111|110|0|00000000|
814 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
815 // |----|---|-----|----|----|---|-|--------|
816 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700817 uint32_t P = (instr >> 24) & 1;
818 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700819 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000820 if (P == U && W == 1) {
821 opcode << "UNDEFINED";
822 } else {
823 uint32_t L = (instr >> 20) & 1;
824 uint32_t S = (instr >> 8) & 1;
825 ArmRegister Rn(instr, 16);
826 if (P == 1 && W == 0) { // VLDR
827 FpRegister d(instr, 12, 22);
828 uint32_t imm8 = instr & 0xFF;
829 opcode << (L == 1 ? "vldr" : "vstr");
830 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
831 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800832 if (Rn.r == 15 && U == 1) {
Aart Bikd3059e72016-05-11 10:30:47 -0700833 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm8 << 2, kT2LitHexLong);
Ian Rogersef6a7762013-12-19 17:58:05 -0800834 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000835 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
836 opcode << (L == 1 ? "vpop" : "vpush");
837 args << FpRegisterRange(instr);
838 } else { // VLDM
839 opcode << (L == 1 ? "vldm" : "vstm");
840 args << Rn << ((W == 1) ? "!" : "") << ", "
841 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700842 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000843 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700844 }
Dave Allison70202782013-10-22 17:52:19 -0700845 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000846 if ((instr & 0xD0) == 0x10) {
847 // 64bit transfers between ARM core and extension registers.
848 uint32_t L = (instr >> 20) & 1;
849 uint32_t S = (instr >> 8) & 1;
850 ArmRegister Rt2(instr, 16);
851 ArmRegister Rt(instr, 12);
852 FpRegister m(instr, 0, 5);
853 opcode << "vmov" << (S ? ".f64" : ".f32");
854 if (L == 1) {
855 args << Rt << ", " << Rt2 << ", ";
856 }
857 if (S) {
858 args << m;
859 } else {
860 args << m << ", " << FpRegister(m, 1);
861 }
862 if (L == 0) {
863 args << ", " << Rt << ", " << Rt2;
864 }
865 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
866 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
867 args << " (UNPREDICTABLE)";
868 }
869 }
Dave Allison70202782013-10-22 17:52:19 -0700870 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
871 // fp data processing
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100872 // VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
873 // |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
874 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
875 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
876 // |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
877 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
878 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
879 // |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
880 // |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
881 // |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
882 // |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
883 // |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
884 // |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
885 // |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
886 // |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
887 // |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
888 // |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
889 uint32_t S = (instr >> 8) & 1;
890 uint32_t Q = (instr >> 6) & 1;
891 FpRegister d(instr, 12, 22);
892 FpRegister n(instr, 16, 7);
893 FpRegister m(instr, 0, 5);
Zheng Xue19649a2014-02-27 13:30:55 +0000894 if ((op3 & 0xB) == 0) { // 100x00
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100895 opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
Zheng Xue19649a2014-02-27 13:30:55 +0000896 args << d << ", " << n << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100897 } else if ((op3 & 0xB) == 0x2) { // 100x10
898 opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
899 args << d << ", " << n << ", " << m;
900 } else if ((op3 & 0xB) == 0x3) { // 100x11
901 opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
902 args << d << ", " << n << ", " << m;
903 } else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
904 opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
905 args << d << ", " << n << ", " << m;
906 } else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
907 uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
908 opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
909 args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
910 : StringPrintf("0x%08x", VFPExpand32(imm8)));
911 if ((instr & 0xa0) != 0) {
912 args << " (UNPREDICTABLE)";
913 }
914 } else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
915 // VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
916 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
917 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
918 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
919 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
920 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
921 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
922 // |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
923 // |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
924 // |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
925 // |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
926 // |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
927 // |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
928 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
929 uint32_t op5 = (instr >> 16) & 0xF;
930 uint32_t op = (instr >> 7) & 1;
931 // Register types in VCVT instructions rely on the combination of op5 and S.
932 FpRegister Dd(instr, 12, 22, 1);
933 FpRegister Sd(instr, 12, 22, 0);
934 FpRegister Dm(instr, 0, 5, 1);
935 FpRegister Sm(instr, 0, 5, 0);
936 if (op5 == 0) {
937 opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
938 args << d << ", " << m;
939 } else if (op5 == 1) {
940 opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
941 args << d << ", " << m;
942 } else if (op5 == 4) {
Vladimir Marko37dd80d2016-08-01 17:41:45 +0100943 opcode << "vcmp" << ((op != 0) ? "e" : "") << (S != 0 ? ".f64" : ".f32");
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100944 args << d << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100945 } else if (op5 == 5) {
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 << ", #0.0";
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100948 if ((instr & 0x2f) != 0) {
949 args << " (UNPREDICTABLE)";
950 }
951 } else if (op5 == 0xD) {
952 if (S == 1) {
953 // vcvt{r}.s32.f64
954 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
955 args << Sd << ", " << Dm;
956 } else {
957 // vcvt{r}.s32.f32
958 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
959 args << Sd << ", " << Sm;
960 }
961 } else if (op5 == 0xC) {
962 if (S == 1) {
963 // vcvt{r}.u32.f64
964 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
965 args << Sd << ", " << Dm;
966 } else {
967 // vcvt{r}.u32.f32
968 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
969 args << Sd << ", " << Sm;
970 }
971 } else if (op5 == 0x8) {
972 if (S == 1) {
973 // vcvt.f64.<Tm>
974 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
975 args << Dd << ", " << Sm;
976 } else {
977 // vcvt.f32.<Tm>
978 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
979 args << Sd << ", " << Sm;
980 }
981 } else if (op5 == 0x7) {
982 if (op == 1) {
Zheng Xue19649a2014-02-27 13:30:55 +0000983 if (S == 1) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100984 // vcvt.f64.f32
985 opcode << "vcvt.f64.f32";
Zheng Xue19649a2014-02-27 13:30:55 +0000986 args << Dd << ", " << Sm;
987 } else {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100988 // vcvt.f32.f64
989 opcode << "vcvt.f32.f64";
990 args << Sd << ", " << Dm;
Zheng Xue19649a2014-02-27 13:30:55 +0000991 }
992 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100993 } else if ((op5 & 0xa) == 0xa) {
994 opcode << "vcvt";
995 args << "[undecoded: floating <-> fixed]";
Zheng Xue19649a2014-02-27 13:30:55 +0000996 }
997 }
Dave Allison70202782013-10-22 17:52:19 -0700998 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000999 if (coproc == 10 && (op3 & 0xE) == 0) {
1000 // VMOV (between ARM core register and single-precision register)
1001 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
1002 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
1003 // |----|----|---|- |----|----|----|-|--|-|----|
1004 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
1005 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
1006 // |----|----|---|- |----|----|----|-|--|-|----|
1007 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
1008 uint32_t op = op3 & 1;
1009 ArmRegister Rt(instr, 12);
1010 FpRegister n(instr, 16, 7);
1011 opcode << "vmov.f32";
1012 if (op) {
1013 args << Rt << ", " << n;
1014 } else {
1015 args << n << ", " << Rt;
1016 }
1017 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
1018 args << " (UNPREDICTABLE)";
1019 }
1020 } else if (coproc == 10 && op3 == 0x2F) {
1021 // VMRS
1022 // |1111|11000000|0000|1111|1100|000|0|0000|
1023 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
1024 // |----|--------|----|----|----|---|-|----|
1025 // |3322|22222222|1111|1111|1100|000|0|0000|
1026 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
1027 // |----|--------|----|----|----|---|-|----|
1028 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
1029 uint32_t spec_reg = (instr >> 16) & 0xF;
1030 ArmRegister Rt(instr, 12);
1031 opcode << "vmrs";
1032 if (spec_reg == 1) {
1033 if (Rt.r == 15) {
1034 args << "APSR_nzcv, FPSCR";
1035 } else if (Rt.r == 13) {
1036 args << Rt << ", FPSCR (UNPREDICTABLE)";
1037 } else {
1038 args << Rt << ", FPSCR";
1039 }
1040 } else {
1041 args << "(PRIVILEGED)";
1042 }
1043 } else if (coproc == 11 && (op3 & 0x9) != 8) {
1044 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
1045 }
Ian Rogers9af89402012-09-07 11:29:35 -07001046 }
Dave Allison70202782013-10-22 17:52:19 -07001047 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001048 }
1049 break;
Ian Rogers40627db2012-03-04 17:31:09 -08001050 case 2:
1051 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
1052 // Data-processing (modified immediate)
1053 // |111|11|10|0000|0|0000|1|111|1100|00000000|
1054 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
1055 // |---|--|--|----|-|----|-|---|----|--------|
1056 // |332|22|22|2222|2|1111|1|111|1100|00000000|
1057 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
1058 // |---|--|--|----|-|----|-|---|----|--------|
1059 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
1060 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -08001061 uint32_t i = (instr >> 26) & 1;
1062 uint32_t op3 = (instr >> 21) & 0xF;
1063 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001064 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -08001065 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001066 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001067 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001068 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
1069 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
1070 if (op3 == 0x2) {
1071 opcode << "mov";
1072 if (S == 1) {
1073 opcode << "s";
1074 }
1075 opcode << ".w";
1076 } else {
1077 opcode << "mvn";
1078 if (S == 1) {
1079 opcode << "s";
1080 }
1081 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001082 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001083 } else if (Rd.r == 0xF && S == 1 &&
1084 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
1085 if (op3 == 0x0) {
1086 opcode << "tst";
1087 } else if (op3 == 0x4) {
1088 opcode << "teq";
1089 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +00001090 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001091 } else {
1092 opcode << "cmp.w";
1093 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001094 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001095 } else {
1096 switch (op3) {
1097 case 0x0: opcode << "and"; break;
1098 case 0x1: opcode << "bic"; break;
1099 case 0x2: opcode << "orr"; break;
1100 case 0x3: opcode << "orn"; break;
1101 case 0x4: opcode << "eor"; break;
1102 case 0x8: opcode << "add"; break;
1103 case 0xA: opcode << "adc"; break;
1104 case 0xB: opcode << "sbc"; break;
1105 case 0xD: opcode << "sub"; break;
1106 case 0xE: opcode << "rsb"; break;
1107 default: opcode << "UNKNOWN DPMI-" << op3; break;
1108 }
1109 if (S == 1) {
1110 opcode << "s";
1111 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001112 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001113 }
Ian Rogers40627db2012-03-04 17:31:09 -08001114 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1115 // Data-processing (plain binary immediate)
1116 // |111|11|10|00000|0000|1|111110000000000|
1117 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1118 // |---|--|--|-----|----|-|---------------|
1119 // |332|22|22|22222|1111|1|111110000000000|
1120 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1121 // |---|--|--|-----|----|-|---------------|
1122 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1123 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001124 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001125 case 0x00: case 0x0A: {
1126 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001127 ArmRegister Rd(instr, 8);
1128 ArmRegister Rn(instr, 16);
1129 uint32_t i = (instr >> 26) & 1;
1130 uint32_t imm3 = (instr >> 12) & 0x7;
1131 uint32_t imm8 = instr & 0xFF;
1132 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1133 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001134 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001135 args << Rd << ", " << Rn << ", #" << imm12;
1136 } else {
1137 opcode << "adr";
1138 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001139 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001140 }
1141 break;
1142 }
Ian Rogers55019132013-02-08 01:05:23 -08001143 case 0x04: case 0x0C: {
1144 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001145 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001146 uint32_t i = (instr >> 26) & 1;
1147 uint32_t imm3 = (instr >> 12) & 0x7;
1148 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001149 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001150 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001151 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001152 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001153 break;
1154 }
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001155 case 0x16: case 0x14: case 0x1C: {
jeffhaoeae26912013-01-28 16:29:54 -08001156 // 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 +00001157 // SBFX Rd, Rn, #lsb, #width - 111 10 0 11 010 0 nnnn 0 iii dddd ii 0 iiiii
1158 // UBFX Rd, Rn, #lsb, #width - 111 10 0 11 110 0 nnnn 0 iii dddd ii 0 iiiii
jeffhaoeae26912013-01-28 16:29:54 -08001159 ArmRegister Rd(instr, 8);
1160 ArmRegister Rn(instr, 16);
1161 uint32_t msb = instr & 0x1F;
1162 uint32_t imm2 = (instr >> 6) & 0x3;
1163 uint32_t imm3 = (instr >> 12) & 0x7;
1164 uint32_t lsb = (imm3 << 2) | imm2;
1165 uint32_t width = msb - lsb + 1;
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001166 if (op3 == 0x16) {
1167 if (Rn.r != 0xF) {
1168 opcode << "bfi";
1169 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1170 } else {
1171 opcode << "bfc";
1172 args << Rd << ", #" << lsb << ", #" << width;
1173 }
jeffhaoeae26912013-01-28 16:29:54 -08001174 } else {
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001175 opcode << ((op3 & 0x8) != 0u ? "ubfx" : "sbfx");
1176 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1177 if (Rd.r == 13 || Rd.r == 15 || Rn.r == 13 || Rn.r == 15 ||
1178 (instr & 0x04000020) != 0u) {
1179 args << " (UNPREDICTABLE)";
1180 }
jeffhaoeae26912013-01-28 16:29:54 -08001181 }
1182 break;
1183 }
Ian Rogers40627db2012-03-04 17:31:09 -08001184 default:
1185 break;
1186 }
1187 } else {
1188 // Branches and miscellaneous control
1189 // |111|11|1000000|0000|1|111|1100|00000000|
1190 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1191 // |---|--|-------|----|-|---|----|--------|
1192 // |332|22|2222222|1111|1|111|1100|00000000|
1193 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1194 // |---|--|-------|----|-|---|----|--------|
1195 // |111|10| op2 | |1|op3|op4 | |
1196
1197 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001198 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001199 switch (op3) {
1200 case 0:
1201 if ((op2 & 0x38) != 0x38) {
1202 // Conditional branch
1203 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1204 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1205 // |---|--|-|----|------|-|-|--|-|--|-----------|
1206 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1207 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1208 // |---|--|-|----|------|-|-|--|-|--|-----------|
1209 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1210 uint32_t S = (instr >> 26) & 1;
1211 uint32_t J2 = (instr >> 11) & 1;
1212 uint32_t J1 = (instr >> 13) & 1;
1213 uint32_t imm6 = (instr >> 16) & 0x3F;
1214 uint32_t imm11 = instr & 0x7FF;
1215 uint32_t cond = (instr >> 22) & 0xF;
1216 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1217 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001218 opcode << "b";
1219 DumpCond(opcode, cond);
1220 opcode << ".w";
1221 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001222 } else if (op2 == 0x3B) {
1223 // Miscellaneous control instructions
1224 uint32_t op5 = (instr >> 4) & 0xF;
1225 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001226 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1227 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1228 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001229 }
Ian Rogers40627db2012-03-04 17:31:09 -08001230 }
1231 break;
1232 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001233 if ((op2 & 0x38) == 0x38) {
1234 if (op2 == 0x7F) {
1235 opcode << "udf";
1236 }
1237 break;
1238 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001239 FALLTHROUGH_INTENDED; // Else deliberate fall-through to B.
Ian Rogersd0876a92013-02-08 11:30:38 -08001240 case 1: case 3: {
1241 // B
1242 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1243 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1244 // |---|--|-|----|------|--|--|-|--|-----------|
1245 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1246 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1247 // |---|--|-|----|------|--|--|-|--|-----------|
1248 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1249 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1250 uint32_t S = (instr >> 26) & 1;
1251 uint32_t cond = (instr >> 22) & 0xF;
1252 uint32_t J2 = (instr >> 11) & 1;
1253 uint32_t form = (instr >> 12) & 1;
1254 uint32_t J1 = (instr >> 13) & 1;
1255 uint32_t imm10 = (instr >> 16) & 0x3FF;
1256 uint32_t imm6 = (instr >> 16) & 0x3F;
1257 uint32_t imm11 = instr & 0x7FF;
1258 opcode << "b";
1259 int32_t imm32;
1260 if (form == 0) {
1261 DumpCond(opcode, cond);
1262 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1263 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1264 } else {
Vladimir Markocb55b292016-04-21 14:52:03 +01001265 uint32_t I1 = (J1 ^ S) ^ 1;
1266 uint32_t I2 = (J2 ^ S) ^ 1;
Ian Rogersd0876a92013-02-08 11:30:38 -08001267 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
Vladimir Markocb55b292016-04-21 14:52:03 +01001268 imm32 = (imm32 << 7) >> 7; // sign extend 25 bit immediate.
Ian Rogersd0876a92013-02-08 11:30:38 -08001269 }
1270 opcode << ".w";
1271 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001272 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001273 }
Ian Rogers40627db2012-03-04 17:31:09 -08001274 case 4: case 6: case 5: case 7: {
1275 // BL, BLX (immediate)
1276 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1277 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1278 // |---|--|-|----------|--|--|-|--|-----------|
1279 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1280 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1281 // |---|--|-|----------|--|--|-|--|-----------|
Dave Allisond6ed6422014-04-09 23:36:15 +00001282 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
Ian Rogers40627db2012-03-04 17:31:09 -08001283 uint32_t S = (instr >> 26) & 1;
1284 uint32_t J2 = (instr >> 11) & 1;
Dave Allisond6ed6422014-04-09 23:36:15 +00001285 uint32_t L = (instr >> 12) & 1;
Ian Rogers40627db2012-03-04 17:31:09 -08001286 uint32_t J1 = (instr >> 13) & 1;
1287 uint32_t imm10 = (instr >> 16) & 0x3FF;
1288 uint32_t imm11 = instr & 0x7FF;
Dave Allisond6ed6422014-04-09 23:36:15 +00001289 if (L == 0) {
1290 opcode << "bx";
Dave Allisonf9487c02014-04-08 23:08:12 +00001291 } else {
Dave Allisond6ed6422014-04-09 23:36:15 +00001292 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001293 }
1294 uint32_t I1 = ~(J1 ^ S);
1295 uint32_t I2 = ~(J2 ^ S);
1296 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1297 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001298 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001299 break;
1300 }
1301 }
1302 }
1303 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001304 case 3:
1305 switch (op2) {
Vladimir Marko55d7c182015-01-05 15:17:01 +00001306 case 0x07: case 0x0F: case 0x17: case 0x1F: { // Explicitly UNDEFINED, A6.3.
1307 opcode << "UNDEFINED";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001308 break;
1309 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001310 case 0x06: case 0x0E: { // "Store single data item" undefined opcodes, A6.3.10.
1311 opcode << "UNDEFINED [store]";
1312 break;
1313 }
1314 case 0x15: case 0x1D: { // "Load word" undefined opcodes, A6.3.7.
1315 opcode << "UNDEFINED [load]";
1316 break;
1317 }
1318 case 0x10: case 0x12: case 0x14: case 0x16: case 0x18: case 0x1A: case 0x1C: case 0x1E: {
1319 opcode << "UNKNOWN " << op2 << " [SIMD]";
1320 break;
1321 }
1322 case 0x01: case 0x00: case 0x09: case 0x08: // {LD,ST}RB{,T}
1323 case 0x03: case 0x02: case 0x0B: case 0x0A: // {LD,ST}RH{,T}
1324 case 0x05: case 0x04: case 0x0D: case 0x0C: // {LD,ST}R{,T}
1325 case 0x11: case 0x19: // LDRSB{,T} (no signed store)
1326 case 0x13: case 0x1B: { // LDRSH{,T} (no signed store)
1327 // Load:
1328 // (Store is the same except that l==0 and always s==0 below.)
1329 // 00s.whl (sign, word, half, load)
1330 // LDR{S}B imm12: 11111|00s1001| Rn | Rt |imm12 (0x09)
1331 // LDR{S}B imm8: 11111|00s0001| Rn | Rt |1PUW|imm8 (0x01)
1332 // LDR{S}BT imm8: 11111|00s0001| Rn | Rt |1110|imm8 (0x01)
1333 // LDR{S}B lit: 11111|00sU001|1111| Rt |imm12 (0x01/0x09)
1334 // LDR{S}B reg: 11111|00s0001| Rn | Rt |000000|imm2| Rm (0x01)
1335 // LDR{S}H imm12: 11111|00s1011| Rn | Rt |imm12 (0x0B)
1336 // LDR{S}H imm8: 11111|00s0011| Rn | Rt |1PUW|imm8 (0x03)
1337 // LDR{S}HT imm8: 11111|00s0011| Rn | Rt |1110|imm8 (0x03)
1338 // LDR{S}H lit: 11111|00sU011|1111| Rt |imm12 (0x03/0x0B)
1339 // LDR{S}H reg: 11111|00s0011| Rn | Rt |000000|imm2| Rm (0x03)
1340 // LDR imm12: 11111|0001101| Rn | Rt |imm12 (0x0D)
1341 // LDR imm8: 11111|0000101| Rn | Rt |1PUW|imm8 (0x05)
1342 // LDRT imm8: 11111|0000101| Rn | Rt |1110|imm8 (0x05)
1343 // LDR lit: 11111|000U101|1111| Rt |imm12 (0x05/0x0D)
1344 // LDR reg: 11111|0000101| Rn | Rt |000000|imm2| Rm (0x05)
1345 //
1346 // If Rt == 15, instead of load we have preload:
1347 // PLD{W} imm12: 11111|00010W1| Rn |1111|imm12 (0x09/0x0B)
1348 // PLD{W} imm8: 11111|00000W1| Rn |1111|1100|imm8 (0x01/0x03); -imm8
1349 // PLD lit: 11111|000U001|1111|1111|imm12 (0x01/0x09)
1350 // PLD{W} reg: 11111|00000W1| Rn |1111|000000|imm2| Rm (0x01/0x03)
1351 // PLI imm12: 11111|0011001| Rn |1111|imm12 (0x19)
1352 // PLI imm8: 11111|0010001| Rn |1111|1100|imm8 (0x11); -imm8
1353 // PLI lit: 11111|001U001|1111|1111|imm12 (0x01/0x09)
1354 // PLI reg: 11111|0010001| Rn |1111|000000|imm2| Rm (0x01/0x03)
1355
1356 bool is_load = HasBitSet(instr, 20);
1357 bool is_half = HasBitSet(instr, 21); // W for PLD/PLDW.
1358 bool is_word = HasBitSet(instr, 22);
1359 bool is_signed = HasBitSet(instr, 24);
jeffhaoeae26912013-01-28 16:29:54 -08001360 ArmRegister Rn(instr, 16);
1361 ArmRegister Rt(instr, 12);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001362 uint32_t imm12 = instr & 0xFFF;
1363 uint32_t U = (instr >> 23) & 1; // U for imm12
1364 uint32_t imm8 = instr & 0xFF;
1365 uint32_t op4 = (instr >> 8) & 0xF; // 1PUW for imm8
1366 if (Rt.r == PC && is_load && !is_word) {
1367 // PLD, PLDW, PLI
1368 const char* pld_pli = (is_signed ? "pli" : "pld");
1369 const char* w = (is_half ? "w" : "");
1370 if (is_signed && !is_half) {
1371 opcode << "UNDEFINED [PLI+W]";
1372 } else if (Rn.r == PC || U != 0u) {
1373 opcode << pld_pli << w;
1374 args << "[" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1375 if (Rn.r == PC && is_half) {
1376 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001377 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001378 } else if ((instr & 0xFC0) == 0) {
1379 opcode << pld_pli << w;
1380 RmLslImm2 Rm(instr);
1381 args << "[" << Rn << ", " << Rm << "]";
1382 } else if (op4 == 0xC) {
1383 opcode << pld_pli << w;
1384 args << "[" << Rn << ", #-" << imm8 << "]";
1385 } else {
1386 opcode << "UNDEFINED [~" << pld_pli << "]";
jeffhaoeae26912013-01-28 16:29:54 -08001387 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001388 break;
1389 }
1390 const char* ldr_str = is_load ? "ldr" : "str";
1391 const char* sign = is_signed ? "s" : "";
1392 const char* type = is_word ? "" : is_half ? "h" : "b";
1393 bool unpred = (Rt.r == SP && !is_word) || (Rt.r == PC && !is_load);
1394 if (Rn.r == PC && !is_load) {
1395 opcode << "UNDEFINED [STR-lit]";
1396 unpred = false;
1397 } else if (Rn.r == PC || U != 0u) {
1398 // Load/store with imm12 (load literal if Rn.r == PC; there's no store literal).
1399 opcode << ldr_str << sign << type << ".w";
1400 args << Rt << ", [" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1401 if (Rn.r == TR && is_load) {
1402 args << " ; ";
Andreas Gampe372f3a32016-08-19 10:49:06 -07001403 GetDisassemblerOptions()->thread_offset_name_function_(args, imm12);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001404 } else if (Rn.r == PC) {
1405 T2LitType lit_type[] = {
1406 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1407 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1408 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1409 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1410 };
1411 DCHECK_LT(op2 >> 1, arraysize(lit_type));
1412 DCHECK_NE(lit_type[op2 >> 1], kT2LitInvalid);
Aart Bikd3059e72016-05-11 10:30:47 -07001413 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm12, lit_type[op2 >> 1]);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001414 }
1415 } else if ((instr & 0xFC0) == 0) {
1416 opcode << ldr_str << sign << type << ".w";
1417 RmLslImm2 Rm(instr);
1418 args << Rt << ", [" << Rn << ", " << Rm << "]";
1419 unpred = unpred || (Rm.rm.r == SP) || (Rm.rm.r == PC);
1420 } else if (is_word && Rn.r == SP && imm8 == 4 && op4 == (is_load ? 0xB : 0xD)) {
1421 opcode << (is_load ? "pop" : "push") << ".w";
1422 args << Rn;
1423 unpred = unpred || (Rn.r == SP);
1424 } else if ((op4 & 5) == 0) {
1425 opcode << "UNDEFINED [P = W = 0 for " << ldr_str << "]";
1426 unpred = false;
1427 } else {
1428 uint32_t P = (instr >> 10) & 1;
1429 U = (instr >> 9) & 1;
1430 uint32_t W = (instr >> 8) & 1;
1431 bool pre_index = (P != 0 && W == 1);
1432 bool post_index = (P == 0 && W == 1);
1433 const char* t = (P != 0 && U != 0 && W == 0) ? "t" : ""; // Unprivileged load/store?
1434 opcode << ldr_str << sign << type << t << ".w";
1435 args << Rt << ", [" << Rn << (post_index ? "]" : "") << ", #" << (U != 0 ? "" : "-")
1436 << imm8 << (post_index ? "" : "]") << (pre_index ? "!" : "");
1437 unpred = (W != 0 && Rn.r == Rt.r);
1438 }
1439 if (unpred) {
1440 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001441 }
1442 break;
1443 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001444 case 0x29: { // 0101001
1445 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1446 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1447 // |---|--|-------|----|----|----|--|---|----|
1448 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1449 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1450 // |---|--|-------|----|----|----|--|---|----|
1451 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1452 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1453 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1454 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1455 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1456 if ((instr & 0xf0c0) == 0xf080) {
1457 uint32_t op3 = (instr >> 4) & 3;
1458 opcode << kThumbReverseOperations[op3];
1459 ArmRegister Rm(instr, 0);
1460 ArmRegister Rd(instr, 8);
1461 args << Rd << ", " << Rm;
1462 ArmRegister Rm2(instr, 16);
1463 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1464 args << " (UNPREDICTABLE)";
1465 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001466 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001467 break;
1468 }
Scott Wakeling611d3392015-07-10 11:42:06 +01001469 case 0x2B: { // 0101011
1470 // CLZ - 111 11 0101011 mmmm 1111 dddd 1000 mmmm
1471 if ((instr & 0xf0f0) == 0xf080) {
1472 opcode << "clz";
1473 ArmRegister Rm(instr, 0);
1474 ArmRegister Rd(instr, 8);
1475 args << Rd << ", " << Rm;
1476 ArmRegister Rm2(instr, 16);
1477 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1478 args << " (UNPREDICTABLE)";
1479 }
1480 }
1481 break;
1482 }
xueliang.zhonge652c122016-06-13 14:42:27 +01001483 case 0x7B: case 0x7F: {
1484 FpRegister d(instr, 12, 22);
1485 FpRegister m(instr, 0, 5);
1486 uint32_t sz = (instr >> 18) & 0x3; // Decode size bits.
1487 uint32_t size = (sz == 0) ? 8 : sz << 4;
1488 uint32_t opc2 = (instr >> 7) & 0xF;
1489 uint32_t Q = (instr >> 6) & 1;
1490 if (Q == 0 && opc2 == 0xA && size == 8) { // 1010, VCNT
1491 opcode << "vcnt." << size;
1492 args << d << ", " << m;
1493 } else if (Q == 0 && (opc2 == 0x4 || opc2 == 0x5) && size <= 32) { // 010x, VPADDL
1494 bool op = HasBitSet(instr, 7);
1495 opcode << "vpaddl." << (op ? "u" : "s") << size;
1496 args << d << ", " << m;
1497 } else {
1498 opcode << "UNKNOWN " << op2;
1499 }
1500 break;
1501 }
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001502 default: // more formats
1503 if ((op2 >> 4) == 2) { // 010xxxx
1504 // data processing (register)
1505 if ((instr & 0x0080f0f0) == 0x0000f000) {
1506 // LSL, LSR, ASR, ROR
1507 uint32_t shift_op = (instr >> 21) & 3;
1508 uint32_t S = (instr >> 20) & 1;
1509 ArmRegister Rd(instr, 8);
1510 ArmRegister Rn(instr, 16);
1511 ArmRegister Rm(instr, 0);
1512 opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
1513 args << Rd << ", " << Rn << ", " << Rm;
1514 }
1515 } else if ((op2 >> 3) == 6) { // 0110xxx
1516 // Multiply, multiply accumulate, and absolute difference
1517 op1 = (instr >> 20) & 0x7;
1518 op2 = (instr >> 4) & 0x1;
1519 ArmRegister Ra(instr, 12);
Vladimir Markoc777e0d2014-04-03 17:59:02 +01001520 ArmRegister Rn(instr, 16);
1521 ArmRegister Rm(instr, 0);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001522 ArmRegister Rd(instr, 8);
1523 switch (op1) {
1524 case 0:
1525 if (op2 == 0) {
1526 if (Ra.r == 0xf) {
1527 opcode << "mul";
1528 args << Rd << ", " << Rn << ", " << Rm;
1529 } else {
1530 opcode << "mla";
1531 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1532 }
Dave Allison70202782013-10-22 17:52:19 -07001533 } else {
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001534 opcode << "mls";
Dave Allison70202782013-10-22 17:52:19 -07001535 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1536 }
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001537 break;
1538 case 1:
1539 case 2:
1540 case 3:
1541 case 4:
1542 case 5:
1543 case 6:
1544 break; // do these sometime
Dave Allison70202782013-10-22 17:52:19 -07001545 }
Vladimir Marko194bcfe2016-07-11 15:52:00 +01001546 } else if ((op2 >> 3) == 7) { // 0111xxx
1547 // Long multiply, long multiply accumulate, and divide
1548 op1 = (instr >> 20) & 0x7;
1549 op2 = (instr >> 4) & 0xf;
1550 ArmRegister Rn(instr, 16);
1551 ArmRegister Rm(instr, 0);
1552 ArmRegister Rd(instr, 8);
1553 ArmRegister RdHi(instr, 8);
1554 ArmRegister RdLo(instr, 12);
1555 switch (op1) {
1556 case 0:
1557 opcode << "smull";
1558 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1559 break;
1560 case 1:
1561 opcode << "sdiv";
1562 args << Rd << ", " << Rn << ", " << Rm;
1563 break;
1564 case 2:
1565 opcode << "umull";
1566 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1567 break;
1568 case 3:
1569 opcode << "udiv";
1570 args << Rd << ", " << Rn << ", " << Rm;
1571 break;
1572 case 4:
1573 case 5:
1574 case 6:
1575 break; // TODO: when we generate these...
1576 }
Dave Allison70202782013-10-22 17:52:19 -07001577 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001578 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001579 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001580 default:
1581 break;
1582 }
Ian Rogers9af89402012-09-07 11:29:35 -07001583
1584 // Apply any IT-block conditions to the opcode if necessary.
1585 if (!it_conditions_.empty()) {
1586 opcode << it_conditions_.back();
1587 it_conditions_.pop_back();
1588 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001589 if (opcode.str().size() == 0) {
1590 opcode << "UNKNOWN " << op2;
1591 }
Ian Rogers9af89402012-09-07 11:29:35 -07001592
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001593 os << FormatInstructionPointer(instr_ptr)
1594 << StringPrintf(": %08x\t%-7s ", instr, opcode.str().c_str())
1595 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001596 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001597} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001598
1599size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1600 uint16_t instr = ReadU16(instr_ptr);
1601 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1602 if (is_32bit) {
1603 return DumpThumb32(os, instr_ptr);
1604 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001605 std::ostringstream opcode;
1606 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001607 uint16_t opcode1 = instr >> 10;
1608 if (opcode1 < 0x10) {
1609 // shift (immediate), add, subtract, move, and compare
1610 uint16_t opcode2 = instr >> 9;
1611 switch (opcode2) {
1612 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1613 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001614 // Logical shift left - 00 000xx iii mmm ddd
1615 // Logical shift right - 00 001xx iii mmm ddd
1616 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001617 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001618 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001619 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001621 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001622 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001623 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001624 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001625 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001626 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001627 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001628 break;
1629 }
1630 case 0xC: case 0xD: case 0xE: case 0xF: {
1631 // Add register - 00 01100 mmm nnn ddd
1632 // Sub register - 00 01101 mmm nnn ddd
1633 // Add 3-bit immediate - 00 01110 iii nnn ddd
1634 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1635 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001636 ThumbRegister Rn(instr, 3);
1637 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001638 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001639 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001640 } else {
1641 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001642 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001643 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001644 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001645 }
1646 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001647 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001649 ArmRegister Rm(imm3_or_Rm);
1650 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001651 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001652 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001653 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654 break;
1655 }
1656 case 0x10: case 0x11: case 0x12: case 0x13:
1657 case 0x14: case 0x15: case 0x16: case 0x17:
1658 case 0x18: case 0x19: case 0x1A: case 0x1B:
1659 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1660 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1661 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1662 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1663 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001664 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001665 uint16_t imm8 = instr & 0xFF;
1666 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001667 case 4: opcode << "movs"; break;
1668 case 5: opcode << "cmp"; break;
1669 case 6: opcode << "adds"; break;
1670 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001671 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001672 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001673 break;
1674 }
1675 default:
1676 break;
1677 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001678 } else if (opcode1 == 0x10) {
1679 // Data-processing
1680 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001681 ThumbRegister rm(instr, 3);
1682 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001683 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001684 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001685 } else if (opcode1 == 0x11) {
1686 // Special data instructions and branch and exchange
1687 uint16_t opcode2 = (instr >> 6) & 0x0F;
1688 switch (opcode2) {
1689 case 0x0: case 0x1: case 0x2: case 0x3: {
1690 // Add low registers - 010001 0000 xxxxxx
1691 // Add high registers - 010001 0001/001x xxxxxx
1692 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001693 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001694 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001695 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001696 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001697 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001698 break;
1699 }
1700 case 0x8: case 0x9: case 0xA: case 0xB: {
1701 // Move low registers - 010001 1000 xxxxxx
1702 // Move high registers - 010001 1001/101x xxxxxx
1703 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001704 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001705 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001706 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001707 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001708 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001709 break;
1710 }
1711 case 0x5: case 0x6: case 0x7: {
1712 // Compare high registers - 010001 0101/011x xxxxxx
1713 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001714 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001715 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001716 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001717 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001718 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001719 break;
1720 }
1721 case 0xC: case 0xD: case 0xE: case 0xF: {
1722 // Branch and exchange - 010001 110x xxxxxx
1723 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001724 ArmRegister rm(instr, 3);
1725 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1726 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001727 break;
1728 }
1729 default:
1730 break;
1731 }
jeffhaoeae26912013-01-28 16:29:54 -08001732 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
Aart Bikd3059e72016-05-11 10:30:47 -07001733 const uintptr_t lo_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->base_address_);
1734 const uintptr_t hi_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->end_address_);
jeffhaoeae26912013-01-28 16:29:54 -08001735 ThumbRegister Rt(instr, 8);
1736 uint16_t imm8 = instr & 0xFF;
1737 opcode << "ldr";
1738 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Aart Bikd3059e72016-05-11 10:30:47 -07001739 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, /*U*/ 1u, imm8 << 2, kT2LitHexWord);
Ian Rogersd83bc362012-09-07 17:43:13 -07001740 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1741 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1742 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1743 // Load/store single data item
1744 uint16_t opA = (instr >> 12) & 0xF;
1745 if (opA == 0x5) {
1746 uint16_t opB = (instr >> 9) & 0x7;
1747 ThumbRegister Rm(instr, 6);
1748 ThumbRegister Rn(instr, 3);
1749 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001750 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001751 case 0: opcode << "str"; break;
1752 case 1: opcode << "strh"; break;
1753 case 2: opcode << "strb"; break;
1754 case 3: opcode << "ldrsb"; break;
1755 case 4: opcode << "ldr"; break;
1756 case 5: opcode << "ldrh"; break;
1757 case 6: opcode << "ldrb"; break;
1758 case 7: opcode << "ldrsh"; break;
1759 }
1760 args << Rt << ", [" << Rn << ", " << Rm << "]";
1761 } else if (opA == 9) {
1762 uint16_t opB = (instr >> 11) & 1;
1763 ThumbRegister Rt(instr, 8);
1764 uint16_t imm8 = instr & 0xFF;
1765 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001766 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001767 } else {
1768 uint16_t imm5 = (instr >> 6) & 0x1F;
1769 uint16_t opB = (instr >> 11) & 1;
1770 ThumbRegister Rn(instr, 3);
1771 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001772 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001773 case 6:
1774 imm5 <<= 2;
1775 opcode << (opB == 0 ? "str" : "ldr");
1776 break;
1777 case 7:
1778 imm5 <<= 0;
1779 opcode << (opB == 0 ? "strb" : "ldrb");
1780 break;
1781 case 8:
1782 imm5 <<= 1;
1783 opcode << (opB == 0 ? "strh" : "ldrh");
1784 break;
1785 }
1786 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1787 }
jeffhaoeae26912013-01-28 16:29:54 -08001788 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001789 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001790 uint32_t cond = (instr >> 8) & 0xF;
1791 opcode << "b";
1792 DumpCond(opcode, cond);
1793 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001794 } else if ((instr & 0xF800) == 0xA800) {
1795 // Generate SP-relative address
1796 ThumbRegister rd(instr, 8);
1797 int imm8 = instr & 0xFF;
1798 opcode << "add";
1799 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001800 } else if ((instr & 0xF000) == 0xB000) {
1801 // Miscellaneous 16-bit instructions
1802 uint16_t opcode2 = (instr >> 5) & 0x7F;
1803 switch (opcode2) {
1804 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1805 // Add immediate to SP - 1011 00000 ii iiiii
1806 // Subtract immediate from SP - 1011 00001 ii iiiii
1807 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001808 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001809 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001810 break;
1811 }
Ian Rogers087b2412012-03-21 01:30:32 -07001812 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001813 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001814 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1815 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001816 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001817 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1818 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1819 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001820 // CBNZ, CBZ
1821 uint16_t op = (instr >> 11) & 1;
1822 uint16_t i = (instr >> 9) & 1;
1823 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001824 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001825 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001826 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001827 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001828 DumpBranchTarget(args, instr_ptr + 4, imm32);
1829 break;
1830 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001831 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1832 case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F: {
1833 opcode << "push";
1834 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 6));
1835 break;
1836 }
1837 case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
1838 case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F: {
1839 opcode << "pop";
1840 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 7));
1841 break;
1842 }
1843 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: {
1844 opcode << "bkpt";
1845 args << "#" << (instr & 0xFF);
1846 break;
1847 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001848 case 0x50: case 0x51: // 101000x
1849 case 0x52: case 0x53: // 101001x
1850 case 0x56: case 0x57: { // 101011x
1851 uint16_t op = (instr >> 6) & 3;
1852 opcode << kThumbReverseOperations[op];
1853 ThumbRegister Rm(instr, 3);
1854 ThumbRegister Rd(instr, 0);
1855 args << Rd << ", " << Rm;
1856 break;
1857 }
Ian Rogers40627db2012-03-04 17:31:09 -08001858 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1859 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1860 // If-Then, and hints
1861 uint16_t opA = (instr >> 4) & 0xF;
1862 uint16_t opB = instr & 0xF;
1863 if (opB == 0) {
1864 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001865 case 0: opcode << "nop"; break;
1866 case 1: opcode << "yield"; break;
1867 case 2: opcode << "wfe"; break;
1868 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001869 default: break;
1870 }
1871 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001872 uint32_t first_cond = opA;
1873 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001874 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001875
1876 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1877 // and store up the actual condition codes we'll want to add to the next few opcodes.
1878 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001879 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001880 for (size_t i = 0; i < count; ++i) {
1881 bool positive_cond = ((first_cond & 1) != 0);
1882 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1883 if (positive_mask == positive_cond) {
1884 opcode << 't';
1885 it_conditions_[i] = kConditionCodeNames[first_cond];
1886 } else {
1887 opcode << 'e';
1888 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1889 }
1890 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001891 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001892
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001893 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1894 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001895 }
1896 break;
1897 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001898 default:
1899 break;
1900 }
1901 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1902 ((instr & 0xE000) == 0x8000)) {
1903 // Load/store single data item
1904 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001905 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001906 switch (opA) {
1907 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001908 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1909 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001910 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001911 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001912 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001913 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1914 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001915 break;
1916 }
1917 case 0x9: {
1918 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1919 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1920 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001921 ThumbRegister Rt(instr, 8);
1922 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1923 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001924 break;
1925 }
1926 default:
1927 break;
1928 }
Ian Rogers40627db2012-03-04 17:31:09 -08001929 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1930 uint16_t imm11 = instr & 0x7FFF;
1931 int32_t imm32 = imm11 << 1;
1932 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001933 opcode << "b";
1934 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001935 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001936
1937 // Apply any IT-block conditions to the opcode if necessary.
1938 if (!it_conditions_.empty()) {
1939 opcode << it_conditions_.back();
1940 it_conditions_.pop_back();
1941 }
1942
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001943 os << FormatInstructionPointer(instr_ptr)
1944 << StringPrintf(": %04x \t%-7s ", instr, opcode.str().c_str())
1945 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001946 }
1947 return 2;
1948}
1949
1950} // namespace arm
1951} // namespace art