blob: ee4953f8ed2075dc55dfe5527a1680bb7c52371f [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 Hughes28fa76d2012-04-09 17:31:46 -070028#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070029
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080030namespace art {
31namespace arm {
32
Ian Rogersb23a7722012-10-09 16:54:26 -070033size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
34 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
35 DumpArm(os, begin);
36 return 4;
37 } else {
38 // remove thumb specifier bits
39 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
40 return DumpThumb16(os, begin);
41 }
42}
43
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080044void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
45 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
46 for (const uint8_t* cur = begin; cur < end; cur += 4) {
47 DumpArm(os, cur);
48 }
49 } else {
50 // remove thumb specifier bits
51 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
52 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
53 for (const uint8_t* cur = begin; cur < end;) {
54 cur += DumpThumb16(os, cur);
55 }
56 }
57}
58
Elliott Hughes77405792012-03-15 15:22:12 -070059static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070060 "eq", // 0000 - equal
61 "ne", // 0001 - not-equal
62 "cs", // 0010 - carry-set, greater than, equal or unordered
63 "cc", // 0011 - carry-clear, less than
64 "mi", // 0100 - minus, negative
65 "pl", // 0101 - plus, positive or zero
66 "vs", // 0110 - overflow
67 "vc", // 0111 - no overflow
68 "hi", // 1000 - unsigned higher
69 "ls", // 1001 - unsigned lower or same
70 "ge", // 1010 - signed greater than or equal
71 "lt", // 1011 - signed less than
72 "gt", // 1100 - signed greater than
73 "le", // 1101 - signed less than or equal
74 "", // 1110 - always
75 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080076};
77
78void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
79 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070080 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080081 } else {
82 os << "Unexpected condition: " << cond;
83 }
84}
85
Ian Rogersb122a4b2013-11-19 18:00:50 -080086void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
87 switch (domain) {
Andreas Gampec8ccf682014-09-29 20:07:43 -070088 case 15U /* 0b1111 */: os << "sy"; break;
89 case 14U /* 0b1110 */: os << "st"; break;
90 case 11U /* 0b1011 */: os << "ish"; break;
91 case 10U /* 0b1010 */: os << "ishst"; break;
92 case 7U /* 0b0111 */: os << "nsh"; break;
93 case 6U /* 0b0110 */: os << "nshst"; break;
94 case 3U /* 0b0011 */: os << "osh"; break;
95 case 2U /* 0b0010 */: os << "oshst"; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -080096 }
97}
98
Ian Rogers40627db2012-03-04 17:31:09 -080099void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700100 os << StringPrintf("%+d (", imm32) << FormatInstructionPointer(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800101}
102
103static uint32_t ReadU16(const uint8_t* ptr) {
104 return ptr[0] | (ptr[1] << 8);
105}
106
107static uint32_t ReadU32(const uint8_t* ptr) {
108 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
109}
110
Elliott Hughes77405792012-03-15 15:22:12 -0700111static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700112 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
113 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700114};
115
Ian Rogersad03ef52012-03-18 19:34:47 -0700116static const char* kThumbDataProcessingOperations[] = {
117 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
118 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
119};
120
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100121static const char* const kThumb2ShiftOperations[] = {
122 "lsl", "lsr", "asr", "ror"
123};
124
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100125static const char* kThumbReverseOperations[] = {
126 "rev", "rev16", "rbit", "revsh"
127};
128
Elliott Hughes77405792012-03-15 15:22:12 -0700129struct ArmRegister {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800130 explicit ArmRegister(uint32_t r_in) : r(r_in) { CHECK_LE(r_in, 15U); }
131 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) {
132 CHECK_LE(r, 15U);
133 }
Elliott Hughes77405792012-03-15 15:22:12 -0700134 uint32_t r;
135};
136std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
137 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700138 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700139 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700140 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700141 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700142 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700143 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700144 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700145 }
146 return os;
147}
148
Elliott Hughes630e77d2012-03-22 19:20:56 -0700149struct ThumbRegister : ArmRegister {
150 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700151};
152
Vladimir Marko55d7c182015-01-05 15:17:01 +0000153struct RmLslImm2 {
154 explicit RmLslImm2(uint32_t instr) : imm2((instr >> 4) & 0x3), rm(instr & 0xf) {}
155 uint32_t imm2;
Elliott Hughes77405792012-03-15 15:22:12 -0700156 ArmRegister rm;
157};
Vladimir Marko55d7c182015-01-05 15:17:01 +0000158std::ostream& operator<<(std::ostream& os, const RmLslImm2& r) {
Elliott Hughes77405792012-03-15 15:22:12 -0700159 os << r.rm;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000160 if (r.imm2 != 0) {
161 os << ", lsl #" << r.imm2;
Elliott Hughes77405792012-03-15 15:22:12 -0700162 }
163 return os;
164}
165
Elliott Hughes1ca98492012-04-12 17:21:02 -0700166struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700167 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700168 uint32_t rotate = ((instruction >> 8) & 0xf);
169 uint32_t imm = (instruction & 0xff);
170 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
171 }
172 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700173};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700174std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700175 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700176 return os;
177}
178
179struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700180 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700181 uint32_t register_list;
182};
183std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
184 if (rhs.register_list == 0) {
185 os << "<no register list?>";
186 return os;
187 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700188 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700189 bool first = true;
190 for (size_t i = 0; i < 16; i++) {
191 if ((rhs.register_list & (1 << i)) != 0) {
192 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700193 first = false;
194 } else {
195 os << ", ";
196 }
197 os << ArmRegister(i);
198 }
199 }
200 os << "}";
201 return os;
202}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800203
Vladimir Markodd577a32013-11-07 19:25:24 +0000204struct FpRegister {
Roland Levillain3887c462015-08-12 18:15:42 +0100205 FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
Vladimir Markodd577a32013-11-07 19:25:24 +0000206 size = (instr >> 8) & 1;
207 uint32_t Vn = (instr >> at_bit) & 0xF;
208 uint32_t N = (instr >> extra_at_bit) & 1;
209 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
210 }
Roland Levillain3887c462015-08-12 18:15:42 +0100211 FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit, uint32_t forced_size) {
Zheng Xue19649a2014-02-27 13:30:55 +0000212 size = forced_size;
213 uint32_t Vn = (instr >> at_bit) & 0xF;
214 uint32_t N = (instr >> extra_at_bit) & 1;
215 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
216 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000217 FpRegister(const FpRegister& other, uint32_t offset)
218 : size(other.size), r(other.r + offset) {}
219
220 uint32_t size; // 0 = f32, 1 = f64
221 uint32_t r;
222};
223std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
224 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
225}
226
227struct FpRegisterRange {
228 explicit FpRegisterRange(uint32_t instr)
229 : first(instr, 12, 22), imm8(instr & 0xFF) {}
230 FpRegister first;
231 uint32_t imm8;
232};
233std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
234 os << "{" << rhs.first;
235 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
236 if (count > 1) {
237 os << "-" << FpRegister(rhs.first, count - 1);
238 }
239 if (rhs.imm8 == 0) {
240 os << " (EMPTY)";
241 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
242 os << rhs.first << " (HALF)";
243 }
244 os << "}";
245 return os;
246}
247
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800248void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700249 uint32_t instruction = ReadU32(instr_ptr);
250 uint32_t cond = (instruction >> 28) & 0xf;
251 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700252 std::string opcode;
253 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700254 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700255 switch (op1) {
256 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700257 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700258 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700259 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700260 opcode = "bkpt";
261 uint32_t imm12 = (instruction >> 8) & 0xfff;
262 uint32_t imm4 = (instruction & 0xf);
263 args << '#' << ((imm12 << 4) | imm4);
264 break;
265 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700266 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700267 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700268 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700269 break;
270 }
271 bool i = (instruction & (1 << 25)) != 0;
272 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700273 uint32_t op = (instruction >> 21) & 0xf;
274 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700275 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700276 bool is_mov = op == 13U /* 0b1101 */ || op == 15U /* 0b1111 */;
Dave Allison20dfc792014-06-16 20:44:29 -0700277 if (is_mov) {
278 // Show only Rd and Rm.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700279 if (s) {
Dave Allison20dfc792014-06-16 20:44:29 -0700280 suffixes += 's';
281 }
282 args << ArmRegister(instruction, 12) << ", ";
283 if (i) {
284 args << ShiftedImmediate(instruction);
285 } else {
286 // TODO: Shifted register.
287 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
288 }
Elliott Hughes77405792012-03-15 15:22:12 -0700289 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700290 if (implicit_s) {
291 // Rd is unused (and not shown), and we don't show the 's' suffix either.
292 } else {
293 if (s) {
294 suffixes += 's';
295 }
296 args << ArmRegister(instruction, 12) << ", ";
297 }
298 if (i) {
299 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
300 } else {
301 // TODO: Shifted register.
302 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
303 }
Elliott Hughes77405792012-03-15 15:22:12 -0700304 }
305 }
306 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700307 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700308 {
309 bool p = (instruction & (1 << 24)) != 0;
310 bool b = (instruction & (1 << 22)) != 0;
311 bool w = (instruction & (1 << 21)) != 0;
312 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700313 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700314 args << ArmRegister(instruction, 12) << ", ";
315 ArmRegister rn(instruction, 16);
316 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700317 UNIMPLEMENTED(FATAL) << "literals";
318 } else {
319 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700320 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700321 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700322 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700323 } else 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 {
328 LOG(FATAL) << p << " " << w;
329 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700330 if (rn.r == 9) {
331 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -0700332 Thread::DumpThreadOffset<4>(args, offset);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700333 }
Elliott Hughes77405792012-03-15 15:22:12 -0700334 }
335 }
336 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700337 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700338 {
339 bool p = (instruction & (1 << 24)) != 0;
340 bool u = (instruction & (1 << 23)) != 0;
341 bool w = (instruction & (1 << 21)) != 0;
342 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700343 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700344 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700345 }
346 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700347 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700348 {
349 bool bl = (instruction & (1 << 24)) != 0;
350 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700351 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700352 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700353 DumpBranchTarget(args, instr_ptr + 8, imm32);
354 }
355 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700356 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700357 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700358 break;
359 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700360 opcode += kConditionCodeNames[cond];
361 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700362 // TODO: a more complete ARM disassembler could generate wider opcodes.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363 os << FormatInstructionPointer(instr_ptr)
364 << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
365 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800366}
367
Ian Rogersa9650dd2013-10-04 08:23:32 -0700368int32_t ThumbExpand(int32_t imm12) {
369 if ((imm12 & 0xC00) == 0) {
370 switch ((imm12 >> 8) & 3) {
371 case 0:
372 return imm12 & 0xFF;
373 case 1:
374 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
375 case 2:
376 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
377 default: // 3
378 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
379 (imm12 & 0xFF);
380 }
381 } else {
382 uint32_t val = 0x80 | (imm12 & 0x7F);
383 int32_t rotate = (imm12 >> 7) & 0x1F;
384 return (val >> rotate) | (val << (32 - rotate));
385 }
386}
387
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100388uint32_t VFPExpand32(uint32_t imm8) {
389 CHECK_EQ(imm8 & 0xffu, imm8);
390 uint32_t bit_a = (imm8 >> 7) & 1;
391 uint32_t bit_b = (imm8 >> 6) & 1;
392 uint32_t slice = imm8 & 0x3f;
393 return (bit_a << 31) | ((1 << 30) - (bit_b << 25)) | (slice << 19);
394}
395
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800396static uint64_t VFPExpand64(uint32_t imm8) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100397 CHECK_EQ(imm8 & 0xffu, imm8);
398 uint64_t bit_a = (imm8 >> 7) & 1;
399 uint64_t bit_b = (imm8 >> 6) & 1;
400 uint64_t slice = imm8 & 0x3f;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000401 return (bit_a << 63) | ((UINT64_C(1) << 62) - (bit_b << 54)) | (slice << 48);
402}
403
404enum T2LitType {
405 kT2LitInvalid,
406 kT2LitUByte,
407 kT2LitSByte,
408 kT2LitUHalf,
409 kT2LitSHalf,
410 kT2LitUWord,
411 kT2LitSWord,
412 kT2LitHexWord,
413 kT2LitULong,
414 kT2LitSLong,
415 kT2LitHexLong,
416};
417std::ostream& operator<<(std::ostream& os, T2LitType type) {
418 return os << static_cast<int>(type);
419}
420
Aart Bika6e95b32016-05-11 10:30:47 -0700421void DumpThumb2Literal(std::ostream& args,
422 const uint8_t* instr_ptr,
423 const uintptr_t lo_adr,
424 const uintptr_t hi_adr,
425 uint32_t U,
426 uint32_t imm32,
Vladimir Marko55d7c182015-01-05 15:17:01 +0000427 T2LitType type) {
428 // Literal offsets (imm32) are not required to be aligned so we may need unaligned access.
429 typedef const int16_t unaligned_int16_t __attribute__ ((aligned (1)));
430 typedef const uint16_t unaligned_uint16_t __attribute__ ((aligned (1)));
431 typedef const int32_t unaligned_int32_t __attribute__ ((aligned (1)));
432 typedef const uint32_t unaligned_uint32_t __attribute__ ((aligned (1)));
433 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (1)));
434 typedef const uint64_t unaligned_uint64_t __attribute__ ((aligned (1)));
435
Aart Bika6e95b32016-05-11 10:30:47 -0700436 // Get address of literal. Bail if not within expected buffer range to
437 // avoid trying to fetch invalid literals (we can encounter this when
438 // interpreting raw data as instructions).
Vladimir Marko55d7c182015-01-05 15:17:01 +0000439 uintptr_t pc = RoundDown(reinterpret_cast<intptr_t>(instr_ptr) + 4, 4);
440 uintptr_t lit_adr = U ? pc + imm32 : pc - imm32;
Aart Bika6e95b32016-05-11 10:30:47 -0700441 if (lit_adr < lo_adr || lit_adr >= hi_adr) {
442 args << " ; (?)";
443 return;
444 }
445
Vladimir Marko55d7c182015-01-05 15:17:01 +0000446 args << " ; ";
447 switch (type) {
448 case kT2LitUByte:
449 args << *reinterpret_cast<const uint8_t*>(lit_adr);
450 break;
451 case kT2LitSByte:
452 args << *reinterpret_cast<const int8_t*>(lit_adr);
453 break;
454 case kT2LitUHalf:
455 args << *reinterpret_cast<const unaligned_uint16_t*>(lit_adr);
456 break;
457 case kT2LitSHalf:
458 args << *reinterpret_cast<const unaligned_int16_t*>(lit_adr);
459 break;
460 case kT2LitUWord:
461 args << *reinterpret_cast<const unaligned_uint32_t*>(lit_adr);
462 break;
463 case kT2LitSWord:
464 args << *reinterpret_cast<const unaligned_int32_t*>(lit_adr);
465 break;
466 case kT2LitHexWord:
467 args << StringPrintf("0x%08x", *reinterpret_cast<const unaligned_uint32_t*>(lit_adr));
468 break;
469 case kT2LitULong:
470 args << *reinterpret_cast<const unaligned_uint64_t*>(lit_adr);
471 break;
472 case kT2LitSLong:
473 args << *reinterpret_cast<const unaligned_int64_t*>(lit_adr);
474 break;
475 case kT2LitHexLong:
476 args << StringPrintf("0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
477 break;
478 default:
479 LOG(FATAL) << "Invalid type: " << type;
480 break;
481 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100482}
483
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
485 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
486 // |111|1 1|1000000|0000|1111110000000000|
487 // |5 3|2 1|0987654|3 0|5 0 5 0|
488 // |---|---|-------|----|----------------|
489 // |332|2 2|2222222|1111|1111110000000000|
490 // |1 9|8 7|6543210|9 6|5 0 5 0|
491 // |---|---|-------|----|----------------|
492 // |111|op1| op2 | | |
493 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700494 if (op1 == 0) {
495 return DumpThumb16(os, instr_ptr);
496 }
497
Aart Bika6e95b32016-05-11 10:30:47 -0700498 // Set valid address range of backing buffer.
499 const uintptr_t lo_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->base_address_);
500 const uintptr_t hi_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->end_address_);
501
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700503 std::ostringstream opcode;
504 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 switch (op1) {
506 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800507 break;
508 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700509 if ((op2 & 0x64) == 0) { // 00x x0xx
510 // |111|11|10|00|0|00|0000|1111110000000000|
511 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
512 // |---|--|--|--|-|--|----|----------------|
513 // |332|22|22|22|2|22|1111|1111110000000000|
514 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
515 // |---|--|--|--|-|--|----|----------------|
516 // |111|01|00|op|0|WL| Rn | |
517 // |111|01| op2 | | |
518 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
519 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
520 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
521 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
522 uint32_t op = (instr >> 23) & 3;
523 uint32_t W = (instr >> 21) & 1;
524 uint32_t L = (instr >> 20) & 1;
525 ArmRegister Rn(instr, 16);
526 if (op == 1 || op == 2) {
527 if (op == 1) {
528 if (L == 0) {
529 opcode << "stm";
530 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800531 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700532 if (Rn.r != 13) {
533 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700534 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700535 } else {
536 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 }
538 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700539 } else {
540 if (L == 0) {
541 if (Rn.r != 13) {
542 opcode << "stmdb";
543 args << Rn << (W == 0 ? "" : "!") << ", ";
544 } else {
545 opcode << "push";
546 }
547 } else {
548 opcode << "ldmdb";
549 args << Rn << (W == 0 ? "" : "!") << ", ";
550 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700552 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800553 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700554 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700555 uint32_t op3 = (instr >> 23) & 3;
556 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700557 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700558 ArmRegister Rn(instr, 16);
559 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700560 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700561 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700562 if ((op3 & 2) == 2) { // 1x
563 int W = (instr >> 21) & 1;
564 int U = (instr >> 23) & 1;
565 int P = (instr >> 24) & 1;
566
567 if ((op4 & 1) == 1) {
568 opcode << "ldrd";
569 } else {
570 opcode << "strd";
571 }
572 args << Rt << "," << Rd << ", [" << Rn;
573 const char *sign = U ? "+" : "-";
574 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000575 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700576 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000577 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700578 if (W == 1) {
579 args << "!";
580 }
581 }
582 } else { // 0x
583 switch (op4) {
584 case 0:
585 if (op3 == 0) { // op3 is 00, op4 is 00
586 opcode << "strex";
587 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000588 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
589 Rd.r == Rn.r || Rd.r == Rt.r) {
590 args << " (UNPREDICTABLE)";
591 }
Dave Allison70202782013-10-22 17:52:19 -0700592 } else { // op3 is 01, op4 is 00
593 // this is one of strexb, strexh or strexd
594 int op5 = (instr >> 4) & 0xf;
595 switch (op5) {
596 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700597 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000598 opcode << ((op5 == 4) ? "strexb" : "strexh");
599 Rd = ArmRegister(instr, 0);
600 args << Rd << ", " << Rt << ", [" << Rn << "]";
601 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
602 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
603 args << " (UNPREDICTABLE)";
604 }
Dave Allison70202782013-10-22 17:52:19 -0700605 break;
606 case 7:
607 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000608 ArmRegister Rt2 = Rd;
609 Rd = ArmRegister(instr, 0);
610 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
611 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
612 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
613 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
614 args << " (UNPREDICTABLE)";
615 }
Dave Allison70202782013-10-22 17:52:19 -0700616 break;
617 }
618 }
619 break;
620 case 1:
621 if (op3 == 0) { // op3 is 00, op4 is 01
622 opcode << "ldrex";
623 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000624 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
625 args << " (UNPREDICTABLE)";
626 }
Dave Allison70202782013-10-22 17:52:19 -0700627 } else { // op3 is 01, op4 is 01
628 // this is one of strexb, strexh or strexd
629 int op5 = (instr >> 4) & 0xf;
630 switch (op5) {
631 case 0:
632 opcode << "tbb";
633 break;
634 case 1:
635 opcode << "tbh";
636 break;
637 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700638 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000639 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
640 args << Rt << ", [" << Rn << "]";
641 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
642 args << " (UNPREDICTABLE)";
643 }
Dave Allison70202782013-10-22 17:52:19 -0700644 break;
645 case 7:
646 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000647 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
648 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
649 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
650 args << " (UNPREDICTABLE)";
651 }
Dave Allison70202782013-10-22 17:52:19 -0700652 break;
653 }
654 }
655 break;
656 case 2: // op3 is 0x, op4 is 10
657 case 3: // op3 is 0x, op4 is 11
658 if (op4 == 2) {
659 opcode << "strd";
660 } else {
661 opcode << "ldrd";
662 }
663 int W = (instr >> 21) & 1;
664 int U = (instr >> 23) & 1;
665 int P = (instr >> 24) & 1;
666
667 args << Rt << "," << Rd << ", [" << Rn;
668 const char *sign = U ? "+" : "-";
669 if (P == 0 && W == 1) {
670 args << "], #" << sign << imm8;
671 } else {
672 args << ", #" << sign << imm8 << "]";
673 if (W == 1) {
674 args << "!";
675 }
676 }
677 break;
678 }
679 }
680
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700681 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
682 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100683 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
684 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
685 // |---|----|----|-|----|----|----|--|--|----|
686 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
687 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
688 // |---|----|----|-|----|----|----|--|--|----|
689 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700690 uint32_t op3 = (instr >> 21) & 0xF;
691 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100692 uint32_t imm3 = ((instr >> 12) & 0x7);
693 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700694 uint32_t imm5 = ((imm3 << 2) | imm2);
695 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700696 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100697 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700698 ArmRegister Rm(instr, 0);
699 switch (op3) {
700 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100701 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700702 opcode << "and";
703 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700704 if (S != 1U) {
705 opcode << "UNKNOWN TST-" << S;
706 break;
707 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700708 opcode << "tst";
709 S = 0; // don't print 's'
710 }
711 break;
712 case 0x1: opcode << "bic"; break;
713 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100714 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700715 opcode << "orr";
716 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100717 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700718 opcode << "mov";
719 }
720 break;
721 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100722 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700723 opcode << "orn";
724 } else {
725 opcode << "mvn";
726 }
727 break;
728 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100729 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700730 opcode << "eor";
731 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700732 if (S != 1U) {
733 opcode << "UNKNOWN TEQ-" << S;
734 break;
735 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700736 opcode << "teq";
737 S = 0; // don't print 's'
738 }
739 break;
740 case 0x6: opcode << "pkh"; break;
741 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100742 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700743 opcode << "add";
744 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700745 if (S != 1U) {
746 opcode << "UNKNOWN CMN-" << S;
747 break;
748 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700749 opcode << "cmn";
750 S = 0; // don't print 's'
751 }
752 break;
753 case 0xA: opcode << "adc"; break;
754 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100755 case 0xD:
756 if (Rd.r != 0xF) {
757 opcode << "sub";
758 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700759 if (S != 1U) {
760 opcode << "UNKNOWN CMP-" << S;
761 break;
762 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100763 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100764 S = 0; // don't print 's'
765 }
766 break;
767 case 0xE: opcode << "rsb"; break;
768 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700769 }
Ian Rogers087b2412012-03-21 01:30:32 -0700770
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700771 if (S == 1) {
772 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700773 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700774 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100775
776 if (Rd.r != 0xF) {
777 args << Rd << ", ";
778 }
779 if (Rn.r != 0xF) {
780 args << Rn << ", ";
781 }
782 args << Rm;
783
784 // Shift operand.
Vladimir Markob7d10aa2016-07-11 15:52:00 +0100785 bool noShift = (imm5 == 0 && shift_type == 0x0);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100786 if (!noShift) {
787 args << ", ";
Vladimir Markob7d10aa2016-07-11 15:52:00 +0100788 if (shift_type == 0x3u && imm5 == 0u) {
789 args << "rrx";
790 } else {
791 args << kThumb2ShiftOperations[shift_type] << " #" << ((0 != imm5) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100792 }
793 }
794
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700795 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
796 // Co-processor instructions
797 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
798 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
799 // |---|-|--|------|----|----|----|---|---|----|
800 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
801 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
802 // |---|-|--|------|----|----|----|---|---|----|
803 // |111| |11| op3 | Rn | |copr| |op4| |
804 uint32_t op3 = (instr >> 20) & 0x3F;
805 uint32_t coproc = (instr >> 8) & 0xF;
806 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700807
Ian Rogersef6a7762013-12-19 17:58:05 -0800808 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000809 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
810 // Extension register load/store instructions
811 // |1111|110|00000|0000|1111|110|0|00000000|
812 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
813 // |----|---|-----|----|----|---|-|--------|
814 // |3322|222|22222|1111|1111|110|0|00000000|
815 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
816 // |----|---|-----|----|----|---|-|--------|
817 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700818 uint32_t P = (instr >> 24) & 1;
819 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700820 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000821 if (P == U && W == 1) {
822 opcode << "UNDEFINED";
823 } else {
824 uint32_t L = (instr >> 20) & 1;
825 uint32_t S = (instr >> 8) & 1;
826 ArmRegister Rn(instr, 16);
827 if (P == 1 && W == 0) { // VLDR
828 FpRegister d(instr, 12, 22);
829 uint32_t imm8 = instr & 0xFF;
830 opcode << (L == 1 ? "vldr" : "vstr");
831 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
832 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800833 if (Rn.r == 15 && U == 1) {
Aart Bika6e95b32016-05-11 10:30:47 -0700834 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm8 << 2, kT2LitHexLong);
Ian Rogersef6a7762013-12-19 17:58:05 -0800835 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000836 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
837 opcode << (L == 1 ? "vpop" : "vpush");
838 args << FpRegisterRange(instr);
839 } else { // VLDM
840 opcode << (L == 1 ? "vldm" : "vstm");
841 args << Rn << ((W == 1) ? "!" : "") << ", "
842 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700843 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000844 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700845 }
Dave Allison70202782013-10-22 17:52:19 -0700846 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000847 if ((instr & 0xD0) == 0x10) {
848 // 64bit transfers between ARM core and extension registers.
849 uint32_t L = (instr >> 20) & 1;
850 uint32_t S = (instr >> 8) & 1;
851 ArmRegister Rt2(instr, 16);
852 ArmRegister Rt(instr, 12);
853 FpRegister m(instr, 0, 5);
854 opcode << "vmov" << (S ? ".f64" : ".f32");
855 if (L == 1) {
856 args << Rt << ", " << Rt2 << ", ";
857 }
858 if (S) {
859 args << m;
860 } else {
861 args << m << ", " << FpRegister(m, 1);
862 }
863 if (L == 0) {
864 args << ", " << Rt << ", " << Rt2;
865 }
866 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
867 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
868 args << " (UNPREDICTABLE)";
869 }
870 }
Dave Allison70202782013-10-22 17:52:19 -0700871 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
872 // fp data processing
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100873 // VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
874 // |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
875 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
876 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
877 // |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
878 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
879 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
880 // |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
881 // |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
882 // |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
883 // |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
884 // |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
885 // |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
886 // |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
887 // |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
888 // |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
889 // |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
890 uint32_t S = (instr >> 8) & 1;
891 uint32_t Q = (instr >> 6) & 1;
892 FpRegister d(instr, 12, 22);
893 FpRegister n(instr, 16, 7);
894 FpRegister m(instr, 0, 5);
Zheng Xue19649a2014-02-27 13:30:55 +0000895 if ((op3 & 0xB) == 0) { // 100x00
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100896 opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
Zheng Xue19649a2014-02-27 13:30:55 +0000897 args << d << ", " << n << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100898 } else if ((op3 & 0xB) == 0x2) { // 100x10
899 opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
900 args << d << ", " << n << ", " << m;
901 } else if ((op3 & 0xB) == 0x3) { // 100x11
902 opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
903 args << d << ", " << n << ", " << m;
904 } else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
905 opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
906 args << d << ", " << n << ", " << m;
907 } else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
908 uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
909 opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
910 args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
911 : StringPrintf("0x%08x", VFPExpand32(imm8)));
912 if ((instr & 0xa0) != 0) {
913 args << " (UNPREDICTABLE)";
914 }
915 } else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
916 // VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
917 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
918 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
919 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
920 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
921 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
922 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
923 // |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
924 // |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
925 // |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
926 // |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
927 // |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
928 // |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
929 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
930 uint32_t op5 = (instr >> 16) & 0xF;
931 uint32_t op = (instr >> 7) & 1;
932 // Register types in VCVT instructions rely on the combination of op5 and S.
933 FpRegister Dd(instr, 12, 22, 1);
934 FpRegister Sd(instr, 12, 22, 0);
935 FpRegister Dm(instr, 0, 5, 1);
936 FpRegister Sm(instr, 0, 5, 0);
937 if (op5 == 0) {
938 opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
939 args << d << ", " << m;
940 } else if (op5 == 1) {
941 opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
942 args << d << ", " << m;
943 } else if (op5 == 4) {
Vladimir Marko2887aa22016-08-01 17:41:45 +0100944 opcode << "vcmp" << ((op != 0) ? "e" : "") << (S != 0 ? ".f64" : ".f32");
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100945 args << d << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100946 } else if (op5 == 5) {
Vladimir Marko2887aa22016-08-01 17:41:45 +0100947 opcode << "vcmp" << ((op != 0) ? "e" : "") << (S != 0 ? ".f64" : ".f32");
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100948 args << d << ", #0.0";
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100949 if ((instr & 0x2f) != 0) {
950 args << " (UNPREDICTABLE)";
951 }
952 } else if (op5 == 0xD) {
953 if (S == 1) {
954 // vcvt{r}.s32.f64
955 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
956 args << Sd << ", " << Dm;
957 } else {
958 // vcvt{r}.s32.f32
959 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
960 args << Sd << ", " << Sm;
961 }
962 } else if (op5 == 0xC) {
963 if (S == 1) {
964 // vcvt{r}.u32.f64
965 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
966 args << Sd << ", " << Dm;
967 } else {
968 // vcvt{r}.u32.f32
969 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
970 args << Sd << ", " << Sm;
971 }
972 } else if (op5 == 0x8) {
973 if (S == 1) {
974 // vcvt.f64.<Tm>
975 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
976 args << Dd << ", " << Sm;
977 } else {
978 // vcvt.f32.<Tm>
979 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
980 args << Sd << ", " << Sm;
981 }
982 } else if (op5 == 0x7) {
983 if (op == 1) {
Zheng Xue19649a2014-02-27 13:30:55 +0000984 if (S == 1) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100985 // vcvt.f64.f32
986 opcode << "vcvt.f64.f32";
Zheng Xue19649a2014-02-27 13:30:55 +0000987 args << Dd << ", " << Sm;
988 } else {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100989 // vcvt.f32.f64
990 opcode << "vcvt.f32.f64";
991 args << Sd << ", " << Dm;
Zheng Xue19649a2014-02-27 13:30:55 +0000992 }
993 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100994 } else if ((op5 & 0xa) == 0xa) {
995 opcode << "vcvt";
996 args << "[undecoded: floating <-> fixed]";
Zheng Xue19649a2014-02-27 13:30:55 +0000997 }
998 }
Dave Allison70202782013-10-22 17:52:19 -0700999 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +00001000 if (coproc == 10 && (op3 & 0xE) == 0) {
1001 // VMOV (between ARM core register and single-precision register)
1002 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
1003 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
1004 // |----|----|---|- |----|----|----|-|--|-|----|
1005 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
1006 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
1007 // |----|----|---|- |----|----|----|-|--|-|----|
1008 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
1009 uint32_t op = op3 & 1;
1010 ArmRegister Rt(instr, 12);
1011 FpRegister n(instr, 16, 7);
1012 opcode << "vmov.f32";
1013 if (op) {
1014 args << Rt << ", " << n;
1015 } else {
1016 args << n << ", " << Rt;
1017 }
1018 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
1019 args << " (UNPREDICTABLE)";
1020 }
1021 } else if (coproc == 10 && op3 == 0x2F) {
1022 // VMRS
1023 // |1111|11000000|0000|1111|1100|000|0|0000|
1024 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
1025 // |----|--------|----|----|----|---|-|----|
1026 // |3322|22222222|1111|1111|1100|000|0|0000|
1027 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
1028 // |----|--------|----|----|----|---|-|----|
1029 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
1030 uint32_t spec_reg = (instr >> 16) & 0xF;
1031 ArmRegister Rt(instr, 12);
1032 opcode << "vmrs";
1033 if (spec_reg == 1) {
1034 if (Rt.r == 15) {
1035 args << "APSR_nzcv, FPSCR";
1036 } else if (Rt.r == 13) {
1037 args << Rt << ", FPSCR (UNPREDICTABLE)";
1038 } else {
1039 args << Rt << ", FPSCR";
1040 }
1041 } else {
1042 args << "(PRIVILEGED)";
1043 }
1044 } else if (coproc == 11 && (op3 & 0x9) != 8) {
1045 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
1046 }
Ian Rogers9af89402012-09-07 11:29:35 -07001047 }
Dave Allison70202782013-10-22 17:52:19 -07001048 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001049 }
1050 break;
Ian Rogers40627db2012-03-04 17:31:09 -08001051 case 2:
1052 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
1053 // Data-processing (modified immediate)
1054 // |111|11|10|0000|0|0000|1|111|1100|00000000|
1055 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
1056 // |---|--|--|----|-|----|-|---|----|--------|
1057 // |332|22|22|2222|2|1111|1|111|1100|00000000|
1058 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
1059 // |---|--|--|----|-|----|-|---|----|--------|
1060 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
1061 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -08001062 uint32_t i = (instr >> 26) & 1;
1063 uint32_t op3 = (instr >> 21) & 0xF;
1064 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001065 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -08001066 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001067 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001068 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001069 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
1070 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
1071 if (op3 == 0x2) {
1072 opcode << "mov";
1073 if (S == 1) {
1074 opcode << "s";
1075 }
1076 opcode << ".w";
1077 } else {
1078 opcode << "mvn";
1079 if (S == 1) {
1080 opcode << "s";
1081 }
1082 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001083 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001084 } else if (Rd.r == 0xF && S == 1 &&
1085 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
1086 if (op3 == 0x0) {
1087 opcode << "tst";
1088 } else if (op3 == 0x4) {
1089 opcode << "teq";
1090 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +00001091 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001092 } else {
1093 opcode << "cmp.w";
1094 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001095 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001096 } else {
1097 switch (op3) {
1098 case 0x0: opcode << "and"; break;
1099 case 0x1: opcode << "bic"; break;
1100 case 0x2: opcode << "orr"; break;
1101 case 0x3: opcode << "orn"; break;
1102 case 0x4: opcode << "eor"; break;
1103 case 0x8: opcode << "add"; break;
1104 case 0xA: opcode << "adc"; break;
1105 case 0xB: opcode << "sbc"; break;
1106 case 0xD: opcode << "sub"; break;
1107 case 0xE: opcode << "rsb"; break;
1108 default: opcode << "UNKNOWN DPMI-" << op3; break;
1109 }
1110 if (S == 1) {
1111 opcode << "s";
1112 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001113 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001114 }
Ian Rogers40627db2012-03-04 17:31:09 -08001115 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1116 // Data-processing (plain binary immediate)
1117 // |111|11|10|00000|0000|1|111110000000000|
1118 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1119 // |---|--|--|-----|----|-|---------------|
1120 // |332|22|22|22222|1111|1|111110000000000|
1121 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1122 // |---|--|--|-----|----|-|---------------|
1123 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1124 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001125 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001126 case 0x00: case 0x0A: {
1127 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001128 ArmRegister Rd(instr, 8);
1129 ArmRegister Rn(instr, 16);
1130 uint32_t i = (instr >> 26) & 1;
1131 uint32_t imm3 = (instr >> 12) & 0x7;
1132 uint32_t imm8 = instr & 0xFF;
1133 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1134 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001135 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001136 args << Rd << ", " << Rn << ", #" << imm12;
1137 } else {
1138 opcode << "adr";
1139 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001140 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001141 }
1142 break;
1143 }
Ian Rogers55019132013-02-08 01:05:23 -08001144 case 0x04: case 0x0C: {
1145 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001146 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001147 uint32_t i = (instr >> 26) & 1;
1148 uint32_t imm3 = (instr >> 12) & 0x7;
1149 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001150 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001151 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001152 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001153 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001154 break;
1155 }
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001156 case 0x16: case 0x14: case 0x1C: {
jeffhaoeae26912013-01-28 16:29:54 -08001157 // 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 +00001158 // SBFX Rd, Rn, #lsb, #width - 111 10 0 11 010 0 nnnn 0 iii dddd ii 0 iiiii
1159 // UBFX Rd, Rn, #lsb, #width - 111 10 0 11 110 0 nnnn 0 iii dddd ii 0 iiiii
jeffhaoeae26912013-01-28 16:29:54 -08001160 ArmRegister Rd(instr, 8);
1161 ArmRegister Rn(instr, 16);
1162 uint32_t msb = instr & 0x1F;
1163 uint32_t imm2 = (instr >> 6) & 0x3;
1164 uint32_t imm3 = (instr >> 12) & 0x7;
1165 uint32_t lsb = (imm3 << 2) | imm2;
1166 uint32_t width = msb - lsb + 1;
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001167 if (op3 == 0x16) {
1168 if (Rn.r != 0xF) {
1169 opcode << "bfi";
1170 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1171 } else {
1172 opcode << "bfc";
1173 args << Rd << ", #" << lsb << ", #" << width;
1174 }
jeffhaoeae26912013-01-28 16:29:54 -08001175 } else {
Vladimir Marko8cdbc2a2016-02-10 12:52:59 +00001176 opcode << ((op3 & 0x8) != 0u ? "ubfx" : "sbfx");
1177 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1178 if (Rd.r == 13 || Rd.r == 15 || Rn.r == 13 || Rn.r == 15 ||
1179 (instr & 0x04000020) != 0u) {
1180 args << " (UNPREDICTABLE)";
1181 }
jeffhaoeae26912013-01-28 16:29:54 -08001182 }
1183 break;
1184 }
Ian Rogers40627db2012-03-04 17:31:09 -08001185 default:
1186 break;
1187 }
1188 } else {
1189 // Branches and miscellaneous control
1190 // |111|11|1000000|0000|1|111|1100|00000000|
1191 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1192 // |---|--|-------|----|-|---|----|--------|
1193 // |332|22|2222222|1111|1|111|1100|00000000|
1194 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1195 // |---|--|-------|----|-|---|----|--------|
1196 // |111|10| op2 | |1|op3|op4 | |
1197
1198 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001199 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001200 switch (op3) {
1201 case 0:
1202 if ((op2 & 0x38) != 0x38) {
1203 // Conditional branch
1204 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1205 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1206 // |---|--|-|----|------|-|-|--|-|--|-----------|
1207 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1208 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1209 // |---|--|-|----|------|-|-|--|-|--|-----------|
1210 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1211 uint32_t S = (instr >> 26) & 1;
1212 uint32_t J2 = (instr >> 11) & 1;
1213 uint32_t J1 = (instr >> 13) & 1;
1214 uint32_t imm6 = (instr >> 16) & 0x3F;
1215 uint32_t imm11 = instr & 0x7FF;
1216 uint32_t cond = (instr >> 22) & 0xF;
1217 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1218 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001219 opcode << "b";
1220 DumpCond(opcode, cond);
1221 opcode << ".w";
1222 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001223 } else if (op2 == 0x3B) {
1224 // Miscellaneous control instructions
1225 uint32_t op5 = (instr >> 4) & 0xF;
1226 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001227 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1228 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1229 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001230 }
Ian Rogers40627db2012-03-04 17:31:09 -08001231 }
1232 break;
1233 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001234 if ((op2 & 0x38) == 0x38) {
1235 if (op2 == 0x7F) {
1236 opcode << "udf";
1237 }
1238 break;
1239 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001240 FALLTHROUGH_INTENDED; // Else deliberate fall-through to B.
Ian Rogersd0876a92013-02-08 11:30:38 -08001241 case 1: case 3: {
1242 // B
1243 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1244 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1245 // |---|--|-|----|------|--|--|-|--|-----------|
1246 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1247 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1248 // |---|--|-|----|------|--|--|-|--|-----------|
1249 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1250 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1251 uint32_t S = (instr >> 26) & 1;
1252 uint32_t cond = (instr >> 22) & 0xF;
1253 uint32_t J2 = (instr >> 11) & 1;
1254 uint32_t form = (instr >> 12) & 1;
1255 uint32_t J1 = (instr >> 13) & 1;
1256 uint32_t imm10 = (instr >> 16) & 0x3FF;
1257 uint32_t imm6 = (instr >> 16) & 0x3F;
1258 uint32_t imm11 = instr & 0x7FF;
1259 opcode << "b";
1260 int32_t imm32;
1261 if (form == 0) {
1262 DumpCond(opcode, cond);
1263 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1264 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1265 } else {
Vladimir Marko369da222016-04-21 14:52:03 +01001266 uint32_t I1 = (J1 ^ S) ^ 1;
1267 uint32_t I2 = (J2 ^ S) ^ 1;
Ian Rogersd0876a92013-02-08 11:30:38 -08001268 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
Vladimir Marko369da222016-04-21 14:52:03 +01001269 imm32 = (imm32 << 7) >> 7; // sign extend 25 bit immediate.
Ian Rogersd0876a92013-02-08 11:30:38 -08001270 }
1271 opcode << ".w";
1272 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001273 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001274 }
Ian Rogers40627db2012-03-04 17:31:09 -08001275 case 4: case 6: case 5: case 7: {
1276 // BL, BLX (immediate)
1277 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1278 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1279 // |---|--|-|----------|--|--|-|--|-----------|
1280 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1281 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1282 // |---|--|-|----------|--|--|-|--|-----------|
Dave Allisond6ed6422014-04-09 23:36:15 +00001283 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
Ian Rogers40627db2012-03-04 17:31:09 -08001284 uint32_t S = (instr >> 26) & 1;
1285 uint32_t J2 = (instr >> 11) & 1;
Dave Allisond6ed6422014-04-09 23:36:15 +00001286 uint32_t L = (instr >> 12) & 1;
Ian Rogers40627db2012-03-04 17:31:09 -08001287 uint32_t J1 = (instr >> 13) & 1;
1288 uint32_t imm10 = (instr >> 16) & 0x3FF;
1289 uint32_t imm11 = instr & 0x7FF;
Dave Allisond6ed6422014-04-09 23:36:15 +00001290 if (L == 0) {
1291 opcode << "bx";
Dave Allisonf9487c02014-04-08 23:08:12 +00001292 } else {
Dave Allisond6ed6422014-04-09 23:36:15 +00001293 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001294 }
1295 uint32_t I1 = ~(J1 ^ S);
1296 uint32_t I2 = ~(J2 ^ S);
1297 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1298 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001299 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001300 break;
1301 }
1302 }
1303 }
1304 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001305 case 3:
1306 switch (op2) {
Vladimir Marko55d7c182015-01-05 15:17:01 +00001307 case 0x07: case 0x0F: case 0x17: case 0x1F: { // Explicitly UNDEFINED, A6.3.
1308 opcode << "UNDEFINED";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001309 break;
1310 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001311 case 0x06: case 0x0E: { // "Store single data item" undefined opcodes, A6.3.10.
1312 opcode << "UNDEFINED [store]";
1313 break;
1314 }
1315 case 0x15: case 0x1D: { // "Load word" undefined opcodes, A6.3.7.
1316 opcode << "UNDEFINED [load]";
1317 break;
1318 }
1319 case 0x10: case 0x12: case 0x14: case 0x16: case 0x18: case 0x1A: case 0x1C: case 0x1E: {
1320 opcode << "UNKNOWN " << op2 << " [SIMD]";
1321 break;
1322 }
1323 case 0x01: case 0x00: case 0x09: case 0x08: // {LD,ST}RB{,T}
1324 case 0x03: case 0x02: case 0x0B: case 0x0A: // {LD,ST}RH{,T}
1325 case 0x05: case 0x04: case 0x0D: case 0x0C: // {LD,ST}R{,T}
1326 case 0x11: case 0x19: // LDRSB{,T} (no signed store)
1327 case 0x13: case 0x1B: { // LDRSH{,T} (no signed store)
1328 // Load:
1329 // (Store is the same except that l==0 and always s==0 below.)
1330 // 00s.whl (sign, word, half, load)
1331 // LDR{S}B imm12: 11111|00s1001| Rn | Rt |imm12 (0x09)
1332 // LDR{S}B imm8: 11111|00s0001| Rn | Rt |1PUW|imm8 (0x01)
1333 // LDR{S}BT imm8: 11111|00s0001| Rn | Rt |1110|imm8 (0x01)
1334 // LDR{S}B lit: 11111|00sU001|1111| Rt |imm12 (0x01/0x09)
1335 // LDR{S}B reg: 11111|00s0001| Rn | Rt |000000|imm2| Rm (0x01)
1336 // LDR{S}H imm12: 11111|00s1011| Rn | Rt |imm12 (0x0B)
1337 // LDR{S}H imm8: 11111|00s0011| Rn | Rt |1PUW|imm8 (0x03)
1338 // LDR{S}HT imm8: 11111|00s0011| Rn | Rt |1110|imm8 (0x03)
1339 // LDR{S}H lit: 11111|00sU011|1111| Rt |imm12 (0x03/0x0B)
1340 // LDR{S}H reg: 11111|00s0011| Rn | Rt |000000|imm2| Rm (0x03)
1341 // LDR imm12: 11111|0001101| Rn | Rt |imm12 (0x0D)
1342 // LDR imm8: 11111|0000101| Rn | Rt |1PUW|imm8 (0x05)
1343 // LDRT imm8: 11111|0000101| Rn | Rt |1110|imm8 (0x05)
1344 // LDR lit: 11111|000U101|1111| Rt |imm12 (0x05/0x0D)
1345 // LDR reg: 11111|0000101| Rn | Rt |000000|imm2| Rm (0x05)
1346 //
1347 // If Rt == 15, instead of load we have preload:
1348 // PLD{W} imm12: 11111|00010W1| Rn |1111|imm12 (0x09/0x0B)
1349 // PLD{W} imm8: 11111|00000W1| Rn |1111|1100|imm8 (0x01/0x03); -imm8
1350 // PLD lit: 11111|000U001|1111|1111|imm12 (0x01/0x09)
1351 // PLD{W} reg: 11111|00000W1| Rn |1111|000000|imm2| Rm (0x01/0x03)
1352 // PLI imm12: 11111|0011001| Rn |1111|imm12 (0x19)
1353 // PLI imm8: 11111|0010001| Rn |1111|1100|imm8 (0x11); -imm8
1354 // PLI lit: 11111|001U001|1111|1111|imm12 (0x01/0x09)
1355 // PLI reg: 11111|0010001| Rn |1111|000000|imm2| Rm (0x01/0x03)
1356
1357 bool is_load = HasBitSet(instr, 20);
1358 bool is_half = HasBitSet(instr, 21); // W for PLD/PLDW.
1359 bool is_word = HasBitSet(instr, 22);
1360 bool is_signed = HasBitSet(instr, 24);
jeffhaoeae26912013-01-28 16:29:54 -08001361 ArmRegister Rn(instr, 16);
1362 ArmRegister Rt(instr, 12);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001363 uint32_t imm12 = instr & 0xFFF;
1364 uint32_t U = (instr >> 23) & 1; // U for imm12
1365 uint32_t imm8 = instr & 0xFF;
1366 uint32_t op4 = (instr >> 8) & 0xF; // 1PUW for imm8
1367 if (Rt.r == PC && is_load && !is_word) {
1368 // PLD, PLDW, PLI
1369 const char* pld_pli = (is_signed ? "pli" : "pld");
1370 const char* w = (is_half ? "w" : "");
1371 if (is_signed && !is_half) {
1372 opcode << "UNDEFINED [PLI+W]";
1373 } else if (Rn.r == PC || U != 0u) {
1374 opcode << pld_pli << w;
1375 args << "[" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1376 if (Rn.r == PC && is_half) {
1377 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001378 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001379 } else if ((instr & 0xFC0) == 0) {
1380 opcode << pld_pli << w;
1381 RmLslImm2 Rm(instr);
1382 args << "[" << Rn << ", " << Rm << "]";
1383 } else if (op4 == 0xC) {
1384 opcode << pld_pli << w;
1385 args << "[" << Rn << ", #-" << imm8 << "]";
1386 } else {
1387 opcode << "UNDEFINED [~" << pld_pli << "]";
jeffhaoeae26912013-01-28 16:29:54 -08001388 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001389 break;
1390 }
1391 const char* ldr_str = is_load ? "ldr" : "str";
1392 const char* sign = is_signed ? "s" : "";
1393 const char* type = is_word ? "" : is_half ? "h" : "b";
1394 bool unpred = (Rt.r == SP && !is_word) || (Rt.r == PC && !is_load);
1395 if (Rn.r == PC && !is_load) {
1396 opcode << "UNDEFINED [STR-lit]";
1397 unpred = false;
1398 } else if (Rn.r == PC || U != 0u) {
1399 // Load/store with imm12 (load literal if Rn.r == PC; there's no store literal).
1400 opcode << ldr_str << sign << type << ".w";
1401 args << Rt << ", [" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
1402 if (Rn.r == TR && is_load) {
1403 args << " ; ";
1404 Thread::DumpThreadOffset<4>(args, imm12);
1405 } else if (Rn.r == PC) {
1406 T2LitType lit_type[] = {
1407 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1408 kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
1409 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1410 kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
1411 };
1412 DCHECK_LT(op2 >> 1, arraysize(lit_type));
1413 DCHECK_NE(lit_type[op2 >> 1], kT2LitInvalid);
Aart Bika6e95b32016-05-11 10:30:47 -07001414 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm12, lit_type[op2 >> 1]);
Vladimir Marko55d7c182015-01-05 15:17:01 +00001415 }
1416 } else if ((instr & 0xFC0) == 0) {
1417 opcode << ldr_str << sign << type << ".w";
1418 RmLslImm2 Rm(instr);
1419 args << Rt << ", [" << Rn << ", " << Rm << "]";
1420 unpred = unpred || (Rm.rm.r == SP) || (Rm.rm.r == PC);
1421 } else if (is_word && Rn.r == SP && imm8 == 4 && op4 == (is_load ? 0xB : 0xD)) {
1422 opcode << (is_load ? "pop" : "push") << ".w";
1423 args << Rn;
1424 unpred = unpred || (Rn.r == SP);
1425 } else if ((op4 & 5) == 0) {
1426 opcode << "UNDEFINED [P = W = 0 for " << ldr_str << "]";
1427 unpred = false;
1428 } else {
1429 uint32_t P = (instr >> 10) & 1;
1430 U = (instr >> 9) & 1;
1431 uint32_t W = (instr >> 8) & 1;
1432 bool pre_index = (P != 0 && W == 1);
1433 bool post_index = (P == 0 && W == 1);
1434 const char* t = (P != 0 && U != 0 && W == 0) ? "t" : ""; // Unprivileged load/store?
1435 opcode << ldr_str << sign << type << t << ".w";
1436 args << Rt << ", [" << Rn << (post_index ? "]" : "") << ", #" << (U != 0 ? "" : "-")
1437 << imm8 << (post_index ? "" : "]") << (pre_index ? "!" : "");
1438 unpred = (W != 0 && Rn.r == Rt.r);
1439 }
1440 if (unpred) {
1441 args << " (UNPREDICTABLE)";
jeffhaoeae26912013-01-28 16:29:54 -08001442 }
1443 break;
1444 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001445 case 0x29: { // 0101001
1446 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1447 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1448 // |---|--|-------|----|----|----|--|---|----|
1449 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1450 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1451 // |---|--|-------|----|----|----|--|---|----|
1452 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1453 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1454 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1455 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1456 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1457 if ((instr & 0xf0c0) == 0xf080) {
1458 uint32_t op3 = (instr >> 4) & 3;
1459 opcode << kThumbReverseOperations[op3];
1460 ArmRegister Rm(instr, 0);
1461 ArmRegister Rd(instr, 8);
1462 args << Rd << ", " << Rm;
1463 ArmRegister Rm2(instr, 16);
1464 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1465 args << " (UNPREDICTABLE)";
1466 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001467 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001468 break;
1469 }
Scott Wakeling611d3392015-07-10 11:42:06 +01001470 case 0x2B: { // 0101011
1471 // CLZ - 111 11 0101011 mmmm 1111 dddd 1000 mmmm
1472 if ((instr & 0xf0f0) == 0xf080) {
1473 opcode << "clz";
1474 ArmRegister Rm(instr, 0);
1475 ArmRegister Rd(instr, 8);
1476 args << Rd << ", " << Rm;
1477 ArmRegister Rm2(instr, 16);
1478 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1479 args << " (UNPREDICTABLE)";
1480 }
1481 }
1482 break;
1483 }
xueliang.zhong3988a8e2016-06-13 14:42:27 +01001484 case 0x7B: case 0x7F: {
1485 FpRegister d(instr, 12, 22);
1486 FpRegister m(instr, 0, 5);
1487 uint32_t sz = (instr >> 18) & 0x3; // Decode size bits.
1488 uint32_t size = (sz == 0) ? 8 : sz << 4;
1489 uint32_t opc2 = (instr >> 7) & 0xF;
1490 uint32_t Q = (instr >> 6) & 1;
1491 if (Q == 0 && opc2 == 0xA && size == 8) { // 1010, VCNT
1492 opcode << "vcnt." << size;
1493 args << d << ", " << m;
1494 } else if (Q == 0 && (opc2 == 0x4 || opc2 == 0x5) && size <= 32) { // 010x, VPADDL
1495 bool op = HasBitSet(instr, 7);
1496 opcode << "vpaddl." << (op ? "u" : "s") << size;
1497 args << d << ", " << m;
1498 } else {
1499 opcode << "UNKNOWN " << op2;
1500 }
1501 break;
1502 }
Vladimir Markob7d10aa2016-07-11 15:52:00 +01001503 default: // more formats
1504 if ((op2 >> 4) == 2) { // 010xxxx
1505 // data processing (register)
1506 if ((instr & 0x0080f0f0) == 0x0000f000) {
1507 // LSL, LSR, ASR, ROR
1508 uint32_t shift_op = (instr >> 21) & 3;
1509 uint32_t S = (instr >> 20) & 1;
1510 ArmRegister Rd(instr, 8);
1511 ArmRegister Rn(instr, 16);
1512 ArmRegister Rm(instr, 0);
1513 opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
1514 args << Rd << ", " << Rn << ", " << Rm;
1515 }
1516 } else if ((op2 >> 3) == 6) { // 0110xxx
1517 // Multiply, multiply accumulate, and absolute difference
1518 op1 = (instr >> 20) & 0x7;
1519 op2 = (instr >> 4) & 0x1;
1520 ArmRegister Ra(instr, 12);
Vladimir Markoc777e0d2014-04-03 17:59:02 +01001521 ArmRegister Rn(instr, 16);
1522 ArmRegister Rm(instr, 0);
Vladimir Markob7d10aa2016-07-11 15:52:00 +01001523 ArmRegister Rd(instr, 8);
1524 switch (op1) {
1525 case 0:
1526 if (op2 == 0) {
1527 if (Ra.r == 0xf) {
1528 opcode << "mul";
1529 args << Rd << ", " << Rn << ", " << Rm;
1530 } else {
1531 opcode << "mla";
1532 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1533 }
Dave Allison70202782013-10-22 17:52:19 -07001534 } else {
Vladimir Markob7d10aa2016-07-11 15:52:00 +01001535 opcode << "mls";
Dave Allison70202782013-10-22 17:52:19 -07001536 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1537 }
Vladimir Markob7d10aa2016-07-11 15:52:00 +01001538 break;
1539 case 1:
1540 case 2:
1541 case 3:
1542 case 4:
1543 case 5:
1544 case 6:
1545 break; // do these sometime
Dave Allison70202782013-10-22 17:52:19 -07001546 }
Vladimir Markob7d10aa2016-07-11 15:52:00 +01001547 } else if ((op2 >> 3) == 7) { // 0111xxx
1548 // Long multiply, long multiply accumulate, and divide
1549 op1 = (instr >> 20) & 0x7;
1550 op2 = (instr >> 4) & 0xf;
1551 ArmRegister Rn(instr, 16);
1552 ArmRegister Rm(instr, 0);
1553 ArmRegister Rd(instr, 8);
1554 ArmRegister RdHi(instr, 8);
1555 ArmRegister RdLo(instr, 12);
1556 switch (op1) {
1557 case 0:
1558 opcode << "smull";
1559 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1560 break;
1561 case 1:
1562 opcode << "sdiv";
1563 args << Rd << ", " << Rn << ", " << Rm;
1564 break;
1565 case 2:
1566 opcode << "umull";
1567 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1568 break;
1569 case 3:
1570 opcode << "udiv";
1571 args << Rd << ", " << Rn << ", " << Rm;
1572 break;
1573 case 4:
1574 case 5:
1575 case 6:
1576 break; // TODO: when we generate these...
1577 }
Dave Allison70202782013-10-22 17:52:19 -07001578 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001579 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001580 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001581 default:
1582 break;
1583 }
Ian Rogers9af89402012-09-07 11:29:35 -07001584
1585 // Apply any IT-block conditions to the opcode if necessary.
1586 if (!it_conditions_.empty()) {
1587 opcode << it_conditions_.back();
1588 it_conditions_.pop_back();
1589 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001590 if (opcode.str().size() == 0) {
1591 opcode << "UNKNOWN " << op2;
1592 }
Ian Rogers9af89402012-09-07 11:29:35 -07001593
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001594 os << FormatInstructionPointer(instr_ptr)
1595 << StringPrintf(": %08x\t%-7s ", instr, opcode.str().c_str())
1596 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001597 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001598} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001599
1600size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1601 uint16_t instr = ReadU16(instr_ptr);
1602 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1603 if (is_32bit) {
1604 return DumpThumb32(os, instr_ptr);
1605 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001606 std::ostringstream opcode;
1607 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001608 uint16_t opcode1 = instr >> 10;
1609 if (opcode1 < 0x10) {
1610 // shift (immediate), add, subtract, move, and compare
1611 uint16_t opcode2 = instr >> 9;
1612 switch (opcode2) {
1613 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1614 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001615 // Logical shift left - 00 000xx iii mmm ddd
1616 // Logical shift right - 00 001xx iii mmm ddd
1617 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001618 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001619 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001620 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001621 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001622 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001623 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001624 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001625 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001626 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001627 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001628 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001629 break;
1630 }
1631 case 0xC: case 0xD: case 0xE: case 0xF: {
1632 // Add register - 00 01100 mmm nnn ddd
1633 // Sub register - 00 01101 mmm nnn ddd
1634 // Add 3-bit immediate - 00 01110 iii nnn ddd
1635 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1636 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001637 ThumbRegister Rn(instr, 3);
1638 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001639 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001640 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001641 } else {
1642 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001643 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001644 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001645 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001646 }
1647 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001648 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001649 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001650 ArmRegister Rm(imm3_or_Rm);
1651 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001652 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001653 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001655 break;
1656 }
1657 case 0x10: case 0x11: case 0x12: case 0x13:
1658 case 0x14: case 0x15: case 0x16: case 0x17:
1659 case 0x18: case 0x19: case 0x1A: case 0x1B:
1660 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1661 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1662 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1663 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1664 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001665 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001666 uint16_t imm8 = instr & 0xFF;
1667 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001668 case 4: opcode << "movs"; break;
1669 case 5: opcode << "cmp"; break;
1670 case 6: opcode << "adds"; break;
1671 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001672 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001673 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001674 break;
1675 }
1676 default:
1677 break;
1678 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001679 } else if (opcode1 == 0x10) {
1680 // Data-processing
1681 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001682 ThumbRegister rm(instr, 3);
1683 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001684 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001685 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001686 } else if (opcode1 == 0x11) {
1687 // Special data instructions and branch and exchange
1688 uint16_t opcode2 = (instr >> 6) & 0x0F;
1689 switch (opcode2) {
1690 case 0x0: case 0x1: case 0x2: case 0x3: {
1691 // Add low registers - 010001 0000 xxxxxx
1692 // Add high registers - 010001 0001/001x xxxxxx
1693 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001694 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001695 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001696 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001697 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001698 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001699 break;
1700 }
1701 case 0x8: case 0x9: case 0xA: case 0xB: {
1702 // Move low registers - 010001 1000 xxxxxx
1703 // Move high registers - 010001 1001/101x xxxxxx
1704 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001705 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001706 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001707 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001708 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001709 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001710 break;
1711 }
1712 case 0x5: case 0x6: case 0x7: {
1713 // Compare high registers - 010001 0101/011x xxxxxx
1714 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001715 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001716 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001717 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001718 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001719 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001720 break;
1721 }
1722 case 0xC: case 0xD: case 0xE: case 0xF: {
1723 // Branch and exchange - 010001 110x xxxxxx
1724 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001725 ArmRegister rm(instr, 3);
1726 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1727 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001728 break;
1729 }
1730 default:
1731 break;
1732 }
jeffhaoeae26912013-01-28 16:29:54 -08001733 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
Aart Bika6e95b32016-05-11 10:30:47 -07001734 const uintptr_t lo_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->base_address_);
1735 const uintptr_t hi_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->end_address_);
jeffhaoeae26912013-01-28 16:29:54 -08001736 ThumbRegister Rt(instr, 8);
1737 uint16_t imm8 = instr & 0xFF;
1738 opcode << "ldr";
1739 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Aart Bika6e95b32016-05-11 10:30:47 -07001740 DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, /*U*/ 1u, imm8 << 2, kT2LitHexWord);
Ian Rogersd83bc362012-09-07 17:43:13 -07001741 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1742 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1743 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1744 // Load/store single data item
1745 uint16_t opA = (instr >> 12) & 0xF;
1746 if (opA == 0x5) {
1747 uint16_t opB = (instr >> 9) & 0x7;
1748 ThumbRegister Rm(instr, 6);
1749 ThumbRegister Rn(instr, 3);
1750 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001751 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001752 case 0: opcode << "str"; break;
1753 case 1: opcode << "strh"; break;
1754 case 2: opcode << "strb"; break;
1755 case 3: opcode << "ldrsb"; break;
1756 case 4: opcode << "ldr"; break;
1757 case 5: opcode << "ldrh"; break;
1758 case 6: opcode << "ldrb"; break;
1759 case 7: opcode << "ldrsh"; break;
1760 }
1761 args << Rt << ", [" << Rn << ", " << Rm << "]";
1762 } else if (opA == 9) {
1763 uint16_t opB = (instr >> 11) & 1;
1764 ThumbRegister Rt(instr, 8);
1765 uint16_t imm8 = instr & 0xFF;
1766 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001767 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001768 } else {
1769 uint16_t imm5 = (instr >> 6) & 0x1F;
1770 uint16_t opB = (instr >> 11) & 1;
1771 ThumbRegister Rn(instr, 3);
1772 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001773 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001774 case 6:
1775 imm5 <<= 2;
1776 opcode << (opB == 0 ? "str" : "ldr");
1777 break;
1778 case 7:
1779 imm5 <<= 0;
1780 opcode << (opB == 0 ? "strb" : "ldrb");
1781 break;
1782 case 8:
1783 imm5 <<= 1;
1784 opcode << (opB == 0 ? "strh" : "ldrh");
1785 break;
1786 }
1787 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1788 }
jeffhaoeae26912013-01-28 16:29:54 -08001789 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001790 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001791 uint32_t cond = (instr >> 8) & 0xF;
1792 opcode << "b";
1793 DumpCond(opcode, cond);
1794 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001795 } else if ((instr & 0xF800) == 0xA800) {
1796 // Generate SP-relative address
1797 ThumbRegister rd(instr, 8);
1798 int imm8 = instr & 0xFF;
1799 opcode << "add";
1800 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001801 } else if ((instr & 0xF000) == 0xB000) {
1802 // Miscellaneous 16-bit instructions
1803 uint16_t opcode2 = (instr >> 5) & 0x7F;
1804 switch (opcode2) {
1805 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1806 // Add immediate to SP - 1011 00000 ii iiiii
1807 // Subtract immediate from SP - 1011 00001 ii iiiii
1808 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001809 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001810 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001811 break;
1812 }
Ian Rogers087b2412012-03-21 01:30:32 -07001813 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001814 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001815 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1816 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001817 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001818 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1819 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1820 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001821 // CBNZ, CBZ
1822 uint16_t op = (instr >> 11) & 1;
1823 uint16_t i = (instr >> 9) & 1;
1824 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001825 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001826 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001827 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001828 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001829 DumpBranchTarget(args, instr_ptr + 4, imm32);
1830 break;
1831 }
Vladimir Marko55d7c182015-01-05 15:17:01 +00001832 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1833 case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F: {
1834 opcode << "push";
1835 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 6));
1836 break;
1837 }
1838 case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
1839 case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F: {
1840 opcode << "pop";
1841 args << RegisterList((instr & 0xFF) | ((instr & 0x100) << 7));
1842 break;
1843 }
1844 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: {
1845 opcode << "bkpt";
1846 args << "#" << (instr & 0xFF);
1847 break;
1848 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001849 case 0x50: case 0x51: // 101000x
1850 case 0x52: case 0x53: // 101001x
1851 case 0x56: case 0x57: { // 101011x
1852 uint16_t op = (instr >> 6) & 3;
1853 opcode << kThumbReverseOperations[op];
1854 ThumbRegister Rm(instr, 3);
1855 ThumbRegister Rd(instr, 0);
1856 args << Rd << ", " << Rm;
1857 break;
1858 }
Ian Rogers40627db2012-03-04 17:31:09 -08001859 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1860 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1861 // If-Then, and hints
1862 uint16_t opA = (instr >> 4) & 0xF;
1863 uint16_t opB = instr & 0xF;
1864 if (opB == 0) {
1865 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001866 case 0: opcode << "nop"; break;
1867 case 1: opcode << "yield"; break;
1868 case 2: opcode << "wfe"; break;
1869 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001870 default: break;
1871 }
1872 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001873 uint32_t first_cond = opA;
1874 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001875 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001876
1877 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1878 // and store up the actual condition codes we'll want to add to the next few opcodes.
1879 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001880 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001881 for (size_t i = 0; i < count; ++i) {
1882 bool positive_cond = ((first_cond & 1) != 0);
1883 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1884 if (positive_mask == positive_cond) {
1885 opcode << 't';
1886 it_conditions_[i] = kConditionCodeNames[first_cond];
1887 } else {
1888 opcode << 'e';
1889 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1890 }
1891 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001892 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001893
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001894 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1895 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001896 }
1897 break;
1898 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001899 default:
1900 break;
1901 }
1902 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1903 ((instr & 0xE000) == 0x8000)) {
1904 // Load/store single data item
1905 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001906 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001907 switch (opA) {
1908 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001909 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1910 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001911 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001912 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001913 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001914 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1915 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001916 break;
1917 }
1918 case 0x9: {
1919 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1920 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1921 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001922 ThumbRegister Rt(instr, 8);
1923 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1924 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001925 break;
1926 }
1927 default:
1928 break;
1929 }
Ian Rogers40627db2012-03-04 17:31:09 -08001930 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1931 uint16_t imm11 = instr & 0x7FFF;
1932 int32_t imm32 = imm11 << 1;
1933 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001934 opcode << "b";
1935 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001936 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001937
1938 // Apply any IT-block conditions to the opcode if necessary.
1939 if (!it_conditions_.empty()) {
1940 opcode << it_conditions_.back();
1941 it_conditions_.pop_back();
1942 }
1943
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001944 os << FormatInstructionPointer(instr_ptr)
1945 << StringPrintf(": %04x \t%-7s ", instr, opcode.str().c_str())
1946 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001947 }
1948 return 2;
1949}
1950
1951} // namespace arm
1952} // namespace art