blob: 9caae9c48b2de750175ed76d975068d95638b820 [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 Rogers3a5c1ce2012-02-29 10:06:46 -080021#include <iostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070022#include <sstream>
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070026#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070027
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080028namespace art {
29namespace arm {
30
Ian Rogersb23a7722012-10-09 16:54:26 -070031size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
32 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
33 DumpArm(os, begin);
34 return 4;
35 } else {
36 // remove thumb specifier bits
37 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
38 return DumpThumb16(os, begin);
39 }
40}
41
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080042void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
43 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
44 for (const uint8_t* cur = begin; cur < end; cur += 4) {
45 DumpArm(os, cur);
46 }
47 } else {
48 // remove thumb specifier bits
49 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
50 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
51 for (const uint8_t* cur = begin; cur < end;) {
52 cur += DumpThumb16(os, cur);
53 }
54 }
55}
56
Elliott Hughes77405792012-03-15 15:22:12 -070057static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070058 "eq", // 0000 - equal
59 "ne", // 0001 - not-equal
60 "cs", // 0010 - carry-set, greater than, equal or unordered
61 "cc", // 0011 - carry-clear, less than
62 "mi", // 0100 - minus, negative
63 "pl", // 0101 - plus, positive or zero
64 "vs", // 0110 - overflow
65 "vc", // 0111 - no overflow
66 "hi", // 1000 - unsigned higher
67 "ls", // 1001 - unsigned lower or same
68 "ge", // 1010 - signed greater than or equal
69 "lt", // 1011 - signed less than
70 "gt", // 1100 - signed greater than
71 "le", // 1101 - signed less than or equal
72 "", // 1110 - always
73 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080074};
75
76void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
77 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070078 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080079 } else {
80 os << "Unexpected condition: " << cond;
81 }
82}
83
Ian Rogersb122a4b2013-11-19 18:00:50 -080084void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
85 switch (domain) {
Andreas Gampec8ccf682014-09-29 20:07:43 -070086 case 15U /* 0b1111 */: os << "sy"; break;
87 case 14U /* 0b1110 */: os << "st"; break;
88 case 11U /* 0b1011 */: os << "ish"; break;
89 case 10U /* 0b1010 */: os << "ishst"; break;
90 case 7U /* 0b0111 */: os << "nsh"; break;
91 case 6U /* 0b0110 */: os << "nshst"; break;
92 case 3U /* 0b0011 */: os << "osh"; break;
93 case 2U /* 0b0010 */: os << "oshst"; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -080094 }
95}
96
Ian Rogers40627db2012-03-04 17:31:09 -080097void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070098 os << StringPrintf("%+d (", imm32) << FormatInstructionPointer(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080099}
100
101static uint32_t ReadU16(const uint8_t* ptr) {
102 return ptr[0] | (ptr[1] << 8);
103}
104
105static uint32_t ReadU32(const uint8_t* ptr) {
106 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
107}
108
Elliott Hughes77405792012-03-15 15:22:12 -0700109static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700110 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
111 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700112};
113
Ian Rogersad03ef52012-03-18 19:34:47 -0700114static const char* kThumbDataProcessingOperations[] = {
115 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
116 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
117};
118
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100119static const char* const kThumb2ShiftOperations[] = {
120 "lsl", "lsr", "asr", "ror"
121};
122
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100123static const char* kThumbReverseOperations[] = {
124 "rev", "rev16", "rbit", "revsh"
125};
126
Elliott Hughes77405792012-03-15 15:22:12 -0700127struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -0700128 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700129 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -0700130 uint32_t r;
131};
132std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
133 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700134 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700135 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700136 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700137 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700138 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700139 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700140 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700141 }
142 return os;
143}
144
Elliott Hughes630e77d2012-03-22 19:20:56 -0700145struct ThumbRegister : ArmRegister {
146 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700147};
148
149struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700150 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700151 uint32_t shift;
152 ArmRegister rm;
153};
154std::ostream& operator<<(std::ostream& os, const Rm& r) {
155 os << r.rm;
156 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700157 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700158 }
159 return os;
160}
161
Elliott Hughes1ca98492012-04-12 17:21:02 -0700162struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700163 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700164 uint32_t rotate = ((instruction >> 8) & 0xf);
165 uint32_t imm = (instruction & 0xff);
166 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
167 }
168 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700169};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700170std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700171 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700172 return os;
173}
174
175struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700176 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700177 uint32_t register_list;
178};
179std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
180 if (rhs.register_list == 0) {
181 os << "<no register list?>";
182 return os;
183 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700184 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700185 bool first = true;
186 for (size_t i = 0; i < 16; i++) {
187 if ((rhs.register_list & (1 << i)) != 0) {
188 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700189 first = false;
190 } else {
191 os << ", ";
192 }
193 os << ArmRegister(i);
194 }
195 }
196 os << "}";
197 return os;
198}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800199
Vladimir Markodd577a32013-11-07 19:25:24 +0000200struct FpRegister {
201 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
202 size = (instr >> 8) & 1;
203 uint32_t Vn = (instr >> at_bit) & 0xF;
204 uint32_t N = (instr >> extra_at_bit) & 1;
205 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
206 }
Zheng Xue19649a2014-02-27 13:30:55 +0000207 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit,
208 uint32_t forced_size) {
209 size = forced_size;
210 uint32_t Vn = (instr >> at_bit) & 0xF;
211 uint32_t N = (instr >> extra_at_bit) & 1;
212 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
213 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000214 FpRegister(const FpRegister& other, uint32_t offset)
215 : size(other.size), r(other.r + offset) {}
216
217 uint32_t size; // 0 = f32, 1 = f64
218 uint32_t r;
219};
220std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
221 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
222}
223
224struct FpRegisterRange {
225 explicit FpRegisterRange(uint32_t instr)
226 : first(instr, 12, 22), imm8(instr & 0xFF) {}
227 FpRegister first;
228 uint32_t imm8;
229};
230std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
231 os << "{" << rhs.first;
232 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
233 if (count > 1) {
234 os << "-" << FpRegister(rhs.first, count - 1);
235 }
236 if (rhs.imm8 == 0) {
237 os << " (EMPTY)";
238 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
239 os << rhs.first << " (HALF)";
240 }
241 os << "}";
242 return os;
243}
244
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800245void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700246 uint32_t instruction = ReadU32(instr_ptr);
247 uint32_t cond = (instruction >> 28) & 0xf;
248 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700249 std::string opcode;
250 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700251 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700252 switch (op1) {
253 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700254 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700255 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700256 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700257 opcode = "bkpt";
258 uint32_t imm12 = (instruction >> 8) & 0xfff;
259 uint32_t imm4 = (instruction & 0xf);
260 args << '#' << ((imm12 << 4) | imm4);
261 break;
262 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700263 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700264 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700265 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700266 break;
267 }
268 bool i = (instruction & (1 << 25)) != 0;
269 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700270 uint32_t op = (instruction >> 21) & 0xf;
271 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700272 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700273 bool is_mov = op == 13U /* 0b1101 */ || op == 15U /* 0b1111 */;
Dave Allison20dfc792014-06-16 20:44:29 -0700274 if (is_mov) {
275 // Show only Rd and Rm.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700276 if (s) {
Dave Allison20dfc792014-06-16 20:44:29 -0700277 suffixes += 's';
278 }
279 args << ArmRegister(instruction, 12) << ", ";
280 if (i) {
281 args << ShiftedImmediate(instruction);
282 } else {
283 // TODO: Shifted register.
284 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
285 }
Elliott Hughes77405792012-03-15 15:22:12 -0700286 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700287 if (implicit_s) {
288 // Rd is unused (and not shown), and we don't show the 's' suffix either.
289 } else {
290 if (s) {
291 suffixes += 's';
292 }
293 args << ArmRegister(instruction, 12) << ", ";
294 }
295 if (i) {
296 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
297 } else {
298 // TODO: Shifted register.
299 args << ArmRegister(instruction, 16) << ", " << ArmRegister(instruction, 0);
300 }
Elliott Hughes77405792012-03-15 15:22:12 -0700301 }
302 }
303 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700304 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700305 {
306 bool p = (instruction & (1 << 24)) != 0;
307 bool b = (instruction & (1 << 22)) != 0;
308 bool w = (instruction & (1 << 21)) != 0;
309 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700310 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700311 args << ArmRegister(instruction, 12) << ", ";
312 ArmRegister rn(instruction, 16);
313 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700314 UNIMPLEMENTED(FATAL) << "literals";
315 } else {
316 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700317 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700318 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700319 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700320 } else 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 {
325 LOG(FATAL) << p << " " << w;
326 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700327 if (rn.r == 9) {
328 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -0700329 Thread::DumpThreadOffset<4>(args, offset);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700330 }
Elliott Hughes77405792012-03-15 15:22:12 -0700331 }
332 }
333 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700334 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700335 {
336 bool p = (instruction & (1 << 24)) != 0;
337 bool u = (instruction & (1 << 23)) != 0;
338 bool w = (instruction & (1 << 21)) != 0;
339 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700340 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700341 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700342 }
343 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700344 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700345 {
346 bool bl = (instruction & (1 << 24)) != 0;
347 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700348 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700349 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700350 DumpBranchTarget(args, instr_ptr + 8, imm32);
351 }
352 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700353 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700354 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700355 break;
356 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700357 opcode += kConditionCodeNames[cond];
358 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700359 // TODO: a more complete ARM disassembler could generate wider opcodes.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360 os << FormatInstructionPointer(instr_ptr)
361 << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
362 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800363}
364
Ian Rogersa9650dd2013-10-04 08:23:32 -0700365int32_t ThumbExpand(int32_t imm12) {
366 if ((imm12 & 0xC00) == 0) {
367 switch ((imm12 >> 8) & 3) {
368 case 0:
369 return imm12 & 0xFF;
370 case 1:
371 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
372 case 2:
373 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
374 default: // 3
375 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
376 (imm12 & 0xFF);
377 }
378 } else {
379 uint32_t val = 0x80 | (imm12 & 0x7F);
380 int32_t rotate = (imm12 >> 7) & 0x1F;
381 return (val >> rotate) | (val << (32 - rotate));
382 }
383}
384
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100385uint32_t VFPExpand32(uint32_t imm8) {
386 CHECK_EQ(imm8 & 0xffu, imm8);
387 uint32_t bit_a = (imm8 >> 7) & 1;
388 uint32_t bit_b = (imm8 >> 6) & 1;
389 uint32_t slice = imm8 & 0x3f;
390 return (bit_a << 31) | ((1 << 30) - (bit_b << 25)) | (slice << 19);
391}
392
393uint64_t VFPExpand64(uint32_t imm8) {
394 CHECK_EQ(imm8 & 0xffu, imm8);
395 uint64_t bit_a = (imm8 >> 7) & 1;
396 uint64_t bit_b = (imm8 >> 6) & 1;
397 uint64_t slice = imm8 & 0x3f;
398 return (bit_a << 31) | ((UINT64_C(1) << 62) - (bit_b << 54)) | (slice << 48);
399}
400
401uint64_t AdvSIMDExpand(uint32_t op, uint32_t cmode, uint32_t imm8) {
402 CHECK_EQ(op & 1, op);
403 CHECK_EQ(cmode & 0xf, cmode);
404 CHECK_EQ(imm8 & 0xff, imm8);
405 int32_t cmode321 = cmode >> 1;
406 if (imm8 == 0 && cmode321 != 0 && cmode321 != 4 && cmode321 != 7) {
407 return INT64_C(0x00000000deadbeef); // UNPREDICTABLE
408 }
409 uint64_t imm = imm8;
410 switch (cmode321) {
Ian Rogersfc787ec2014-10-09 21:56:44 -0700411 case 3: imm <<= 8; FALLTHROUGH_INTENDED;
412 case 2: imm <<= 8; FALLTHROUGH_INTENDED;
413 case 1: imm <<= 8; FALLTHROUGH_INTENDED;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100414 case 0: return static_cast<int64_t>((imm << 32) | imm);
Ian Rogersfc787ec2014-10-09 21:56:44 -0700415 case 5: imm <<= 8; FALLTHROUGH_INTENDED;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100416 case 4: return static_cast<int64_t>((imm << 48) | (imm << 32) | (imm << 16) | imm);
417 case 6:
418 imm = ((imm + 1u) << ((cmode & 1) != 0 ? 16 : 8)) - 1u; // Add 8 or 16 ones.
419 return static_cast<int64_t>((imm << 32) | imm);
420 default:
421 CHECK_EQ(cmode321, 7);
422 if ((cmode & 1) == 0 && op == 0) {
423 imm = (imm << 8) | imm;
424 return static_cast<int64_t>((imm << 48) | (imm << 32) | (imm << 16) | imm);
425 } else if ((cmode & 1) == 0 && op != 0) {
426 for (int i = 1; i != 8; ++i) {
427 imm |= ((imm >> i) & UINT64_C(1)) << (i * 8);
428 }
429 imm = imm & ~UINT64_C(0xfe);
430 return static_cast<int64_t>((imm << 8) - imm);
431 } else if ((cmode & 1) != 0 && op == 0) {
432 imm = static_cast<uint32_t>(VFPExpand32(imm8));
433 return static_cast<int64_t>((imm << 32) | imm);
434 } else {
435 return INT64_C(0xdeadbeef00000000); // UNDEFINED
436 }
437 }
438}
439
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800440size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
441 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
442 // |111|1 1|1000000|0000|1111110000000000|
443 // |5 3|2 1|0987654|3 0|5 0 5 0|
444 // |---|---|-------|----|----------------|
445 // |332|2 2|2222222|1111|1111110000000000|
446 // |1 9|8 7|6543210|9 6|5 0 5 0|
447 // |---|---|-------|----|----------------|
448 // |111|op1| op2 | | |
449 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700450 if (op1 == 0) {
451 return DumpThumb16(os, instr_ptr);
452 }
453
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800454 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700455 std::ostringstream opcode;
456 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800457 switch (op1) {
458 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800459 break;
460 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700461 if ((op2 & 0x64) == 0) { // 00x x0xx
462 // |111|11|10|00|0|00|0000|1111110000000000|
463 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
464 // |---|--|--|--|-|--|----|----------------|
465 // |332|22|22|22|2|22|1111|1111110000000000|
466 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
467 // |---|--|--|--|-|--|----|----------------|
468 // |111|01|00|op|0|WL| Rn | |
469 // |111|01| op2 | | |
470 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
471 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
472 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
473 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
474 uint32_t op = (instr >> 23) & 3;
475 uint32_t W = (instr >> 21) & 1;
476 uint32_t L = (instr >> 20) & 1;
477 ArmRegister Rn(instr, 16);
478 if (op == 1 || op == 2) {
479 if (op == 1) {
480 if (L == 0) {
481 opcode << "stm";
482 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700484 if (Rn.r != 13) {
485 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700486 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700487 } else {
488 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800489 }
490 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700491 } else {
492 if (L == 0) {
493 if (Rn.r != 13) {
494 opcode << "stmdb";
495 args << Rn << (W == 0 ? "" : "!") << ", ";
496 } else {
497 opcode << "push";
498 }
499 } else {
500 opcode << "ldmdb";
501 args << Rn << (W == 0 ? "" : "!") << ", ";
502 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700504 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700506 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700507 uint32_t op3 = (instr >> 23) & 3;
508 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700509 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700510 ArmRegister Rn(instr, 16);
511 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700512 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700513 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700514 if ((op3 & 2) == 2) { // 1x
515 int W = (instr >> 21) & 1;
516 int U = (instr >> 23) & 1;
517 int P = (instr >> 24) & 1;
518
519 if ((op4 & 1) == 1) {
520 opcode << "ldrd";
521 } else {
522 opcode << "strd";
523 }
524 args << Rt << "," << Rd << ", [" << Rn;
525 const char *sign = U ? "+" : "-";
526 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000527 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700528 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000529 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700530 if (W == 1) {
531 args << "!";
532 }
533 }
534 } else { // 0x
535 switch (op4) {
536 case 0:
537 if (op3 == 0) { // op3 is 00, op4 is 00
538 opcode << "strex";
539 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000540 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
541 Rd.r == Rn.r || Rd.r == Rt.r) {
542 args << " (UNPREDICTABLE)";
543 }
Dave Allison70202782013-10-22 17:52:19 -0700544 } else { // op3 is 01, op4 is 00
545 // this is one of strexb, strexh or strexd
546 int op5 = (instr >> 4) & 0xf;
547 switch (op5) {
548 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700549 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000550 opcode << ((op5 == 4) ? "strexb" : "strexh");
551 Rd = ArmRegister(instr, 0);
552 args << Rd << ", " << Rt << ", [" << Rn << "]";
553 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
554 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
555 args << " (UNPREDICTABLE)";
556 }
Dave Allison70202782013-10-22 17:52:19 -0700557 break;
558 case 7:
559 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000560 ArmRegister Rt2 = Rd;
561 Rd = ArmRegister(instr, 0);
562 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
563 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
564 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
565 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
566 args << " (UNPREDICTABLE)";
567 }
Dave Allison70202782013-10-22 17:52:19 -0700568 break;
569 }
570 }
571 break;
572 case 1:
573 if (op3 == 0) { // op3 is 00, op4 is 01
574 opcode << "ldrex";
575 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000576 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
577 args << " (UNPREDICTABLE)";
578 }
Dave Allison70202782013-10-22 17:52:19 -0700579 } else { // op3 is 01, op4 is 01
580 // this is one of strexb, strexh or strexd
581 int op5 = (instr >> 4) & 0xf;
582 switch (op5) {
583 case 0:
584 opcode << "tbb";
585 break;
586 case 1:
587 opcode << "tbh";
588 break;
589 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700590 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000591 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
592 args << Rt << ", [" << Rn << "]";
593 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
594 args << " (UNPREDICTABLE)";
595 }
Dave Allison70202782013-10-22 17:52:19 -0700596 break;
597 case 7:
598 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000599 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
600 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
601 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
602 args << " (UNPREDICTABLE)";
603 }
Dave Allison70202782013-10-22 17:52:19 -0700604 break;
605 }
606 }
607 break;
608 case 2: // op3 is 0x, op4 is 10
609 case 3: // op3 is 0x, op4 is 11
610 if (op4 == 2) {
611 opcode << "strd";
612 } else {
613 opcode << "ldrd";
614 }
615 int W = (instr >> 21) & 1;
616 int U = (instr >> 23) & 1;
617 int P = (instr >> 24) & 1;
618
619 args << Rt << "," << Rd << ", [" << Rn;
620 const char *sign = U ? "+" : "-";
621 if (P == 0 && W == 1) {
622 args << "], #" << sign << imm8;
623 } else {
624 args << ", #" << sign << imm8 << "]";
625 if (W == 1) {
626 args << "!";
627 }
628 }
629 break;
630 }
631 }
632
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700633 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
634 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100635 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
636 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
637 // |---|----|----|-|----|----|----|--|--|----|
638 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
639 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
640 // |---|----|----|-|----|----|----|--|--|----|
641 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700642 uint32_t op3 = (instr >> 21) & 0xF;
643 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100644 uint32_t imm3 = ((instr >> 12) & 0x7);
645 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700646 uint32_t imm5 = ((imm3 << 2) | imm2);
647 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700648 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100649 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700650 ArmRegister Rm(instr, 0);
651 switch (op3) {
652 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100653 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700654 opcode << "and";
655 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700656 if (S != 1U) {
657 opcode << "UNKNOWN TST-" << S;
658 break;
659 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700660 opcode << "tst";
661 S = 0; // don't print 's'
662 }
663 break;
664 case 0x1: opcode << "bic"; break;
665 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100666 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700667 opcode << "orr";
668 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100669 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700670 opcode << "mov";
671 }
672 break;
673 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100674 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700675 opcode << "orn";
676 } else {
677 opcode << "mvn";
678 }
679 break;
680 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100681 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700682 opcode << "eor";
683 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700684 if (S != 1U) {
685 opcode << "UNKNOWN TEQ-" << S;
686 break;
687 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700688 opcode << "teq";
689 S = 0; // don't print 's'
690 }
691 break;
692 case 0x6: opcode << "pkh"; break;
693 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100694 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700695 opcode << "add";
696 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700697 if (S != 1U) {
698 opcode << "UNKNOWN CMN-" << S;
699 break;
700 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700701 opcode << "cmn";
702 S = 0; // don't print 's'
703 }
704 break;
705 case 0xA: opcode << "adc"; break;
706 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100707 case 0xD:
708 if (Rd.r != 0xF) {
709 opcode << "sub";
710 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700711 if (S != 1U) {
712 opcode << "UNKNOWN CMP-" << S;
713 break;
714 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100715 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100716 S = 0; // don't print 's'
717 }
718 break;
719 case 0xE: opcode << "rsb"; break;
720 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700721 }
Ian Rogers087b2412012-03-21 01:30:32 -0700722
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700723 if (S == 1) {
724 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700725 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700726 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100727
728 if (Rd.r != 0xF) {
729 args << Rd << ", ";
730 }
731 if (Rn.r != 0xF) {
732 args << Rn << ", ";
733 }
734 args << Rm;
735
736 // Shift operand.
737 bool noShift = (imm5 == 0 && shift_type != 0x3);
738 if (!noShift) {
739 args << ", ";
740 switch (shift_type) {
741 case 0x0: args << "lsl"; break;
742 case 0x1: args << "lsr"; break;
743 case 0x2: args << "asr"; break;
744 case 0x3:
745 if (imm5 == 0) {
746 args << "rrx";
747 } else {
748 args << "ror";
749 }
750 break;
751 }
752 if (shift_type != 0x3 /* rrx */) {
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700753 args << StringPrintf(" #%d", (0 != imm5 || 0 == shift_type) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100754 }
755 }
756
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700757 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
758 // Co-processor instructions
759 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
760 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
761 // |---|-|--|------|----|----|----|---|---|----|
762 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
763 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
764 // |---|-|--|------|----|----|----|---|---|----|
765 // |111| |11| op3 | Rn | |copr| |op4| |
766 uint32_t op3 = (instr >> 20) & 0x3F;
767 uint32_t coproc = (instr >> 8) & 0xF;
768 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700769
Ian Rogersef6a7762013-12-19 17:58:05 -0800770 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000771 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
772 // Extension register load/store instructions
773 // |1111|110|00000|0000|1111|110|0|00000000|
774 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
775 // |----|---|-----|----|----|---|-|--------|
776 // |3322|222|22222|1111|1111|110|0|00000000|
777 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
778 // |----|---|-----|----|----|---|-|--------|
779 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700780 uint32_t P = (instr >> 24) & 1;
781 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700782 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000783 if (P == U && W == 1) {
784 opcode << "UNDEFINED";
785 } else {
786 uint32_t L = (instr >> 20) & 1;
787 uint32_t S = (instr >> 8) & 1;
788 ArmRegister Rn(instr, 16);
789 if (P == 1 && W == 0) { // VLDR
790 FpRegister d(instr, 12, 22);
791 uint32_t imm8 = instr & 0xFF;
792 opcode << (L == 1 ? "vldr" : "vstr");
793 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
794 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800795 if (Rn.r == 15 && U == 1) {
796 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
797 lit_adr = RoundDown(lit_adr, 4) + 4 + (imm8 << 2);
Brian Carlstromc2687ef2014-03-13 15:12:11 -0700798 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (2)));
799 args << StringPrintf(" ; 0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
Ian Rogersef6a7762013-12-19 17:58:05 -0800800 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000801 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
802 opcode << (L == 1 ? "vpop" : "vpush");
803 args << FpRegisterRange(instr);
804 } else { // VLDM
805 opcode << (L == 1 ? "vldm" : "vstm");
806 args << Rn << ((W == 1) ? "!" : "") << ", "
807 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700808 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000809 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700810 }
Dave Allison70202782013-10-22 17:52:19 -0700811 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000812 if ((instr & 0xD0) == 0x10) {
813 // 64bit transfers between ARM core and extension registers.
814 uint32_t L = (instr >> 20) & 1;
815 uint32_t S = (instr >> 8) & 1;
816 ArmRegister Rt2(instr, 16);
817 ArmRegister Rt(instr, 12);
818 FpRegister m(instr, 0, 5);
819 opcode << "vmov" << (S ? ".f64" : ".f32");
820 if (L == 1) {
821 args << Rt << ", " << Rt2 << ", ";
822 }
823 if (S) {
824 args << m;
825 } else {
826 args << m << ", " << FpRegister(m, 1);
827 }
828 if (L == 0) {
829 args << ", " << Rt << ", " << Rt2;
830 }
831 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
832 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
833 args << " (UNPREDICTABLE)";
834 }
835 }
Dave Allison70202782013-10-22 17:52:19 -0700836 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
837 // fp data processing
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100838 // VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
839 // |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
840 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
841 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
842 // |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
843 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
844 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
845 // |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
846 // |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
847 // |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
848 // |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
849 // |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
850 // |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
851 // |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
852 // |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
853 // |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
854 // |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
855 uint32_t S = (instr >> 8) & 1;
856 uint32_t Q = (instr >> 6) & 1;
857 FpRegister d(instr, 12, 22);
858 FpRegister n(instr, 16, 7);
859 FpRegister m(instr, 0, 5);
Zheng Xue19649a2014-02-27 13:30:55 +0000860 if ((op3 & 0xB) == 0) { // 100x00
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100861 opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
Zheng Xue19649a2014-02-27 13:30:55 +0000862 args << d << ", " << n << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100863 } else if ((op3 & 0xB) == 0x2) { // 100x10
864 opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
865 args << d << ", " << n << ", " << m;
866 } else if ((op3 & 0xB) == 0x3) { // 100x11
867 opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
868 args << d << ", " << n << ", " << m;
869 } else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
870 opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
871 args << d << ", " << n << ", " << m;
872 } else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
873 uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
874 opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
875 args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
876 : StringPrintf("0x%08x", VFPExpand32(imm8)));
877 if ((instr & 0xa0) != 0) {
878 args << " (UNPREDICTABLE)";
879 }
880 } else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
881 // VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
882 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
883 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
884 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
885 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
886 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
887 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
888 // |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
889 // |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
890 // |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
891 // |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
892 // |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
893 // |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
894 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
895 uint32_t op5 = (instr >> 16) & 0xF;
896 uint32_t op = (instr >> 7) & 1;
897 // Register types in VCVT instructions rely on the combination of op5 and S.
898 FpRegister Dd(instr, 12, 22, 1);
899 FpRegister Sd(instr, 12, 22, 0);
900 FpRegister Dm(instr, 0, 5, 1);
901 FpRegister Sm(instr, 0, 5, 0);
902 if (op5 == 0) {
903 opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
904 args << d << ", " << m;
905 } else if (op5 == 1) {
906 opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
907 args << d << ", " << m;
908 } else if (op5 == 4) {
909 opcode << "vcmp" << (S != 0 ? ".f64" : ".f32");
910 args << d << ", " << m;
911 if (op != 0) {
912 args << " (quiet nan)";
913 }
914 } else if (op5 == 5) {
915 opcode << "vcmpe" << (S != 0 ? ".f64" : ".f32");
916 args << d << ", #0.0";
917 if (op != 0) {
918 args << " (quiet nan)";
919 }
920 if ((instr & 0x2f) != 0) {
921 args << " (UNPREDICTABLE)";
922 }
923 } else if (op5 == 0xD) {
924 if (S == 1) {
925 // vcvt{r}.s32.f64
926 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
927 args << Sd << ", " << Dm;
928 } else {
929 // vcvt{r}.s32.f32
930 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
931 args << Sd << ", " << Sm;
932 }
933 } else if (op5 == 0xC) {
934 if (S == 1) {
935 // vcvt{r}.u32.f64
936 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
937 args << Sd << ", " << Dm;
938 } else {
939 // vcvt{r}.u32.f32
940 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
941 args << Sd << ", " << Sm;
942 }
943 } else if (op5 == 0x8) {
944 if (S == 1) {
945 // vcvt.f64.<Tm>
946 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
947 args << Dd << ", " << Sm;
948 } else {
949 // vcvt.f32.<Tm>
950 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
951 args << Sd << ", " << Sm;
952 }
953 } else if (op5 == 0x7) {
954 if (op == 1) {
Zheng Xue19649a2014-02-27 13:30:55 +0000955 if (S == 1) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100956 // vcvt.f64.f32
957 opcode << "vcvt.f64.f32";
Zheng Xue19649a2014-02-27 13:30:55 +0000958 args << Dd << ", " << Sm;
959 } else {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100960 // vcvt.f32.f64
961 opcode << "vcvt.f32.f64";
962 args << Sd << ", " << Dm;
Zheng Xue19649a2014-02-27 13:30:55 +0000963 }
964 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100965 } else if ((op5 & 0xa) == 0xa) {
966 opcode << "vcvt";
967 args << "[undecoded: floating <-> fixed]";
Zheng Xue19649a2014-02-27 13:30:55 +0000968 }
969 }
Dave Allison70202782013-10-22 17:52:19 -0700970 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000971 if (coproc == 10 && (op3 & 0xE) == 0) {
972 // VMOV (between ARM core register and single-precision register)
973 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
974 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
975 // |----|----|---|- |----|----|----|-|--|-|----|
976 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
977 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
978 // |----|----|---|- |----|----|----|-|--|-|----|
979 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
980 uint32_t op = op3 & 1;
981 ArmRegister Rt(instr, 12);
982 FpRegister n(instr, 16, 7);
983 opcode << "vmov.f32";
984 if (op) {
985 args << Rt << ", " << n;
986 } else {
987 args << n << ", " << Rt;
988 }
989 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
990 args << " (UNPREDICTABLE)";
991 }
992 } else if (coproc == 10 && op3 == 0x2F) {
993 // VMRS
994 // |1111|11000000|0000|1111|1100|000|0|0000|
995 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
996 // |----|--------|----|----|----|---|-|----|
997 // |3322|22222222|1111|1111|1100|000|0|0000|
998 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
999 // |----|--------|----|----|----|---|-|----|
1000 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
1001 uint32_t spec_reg = (instr >> 16) & 0xF;
1002 ArmRegister Rt(instr, 12);
1003 opcode << "vmrs";
1004 if (spec_reg == 1) {
1005 if (Rt.r == 15) {
1006 args << "APSR_nzcv, FPSCR";
1007 } else if (Rt.r == 13) {
1008 args << Rt << ", FPSCR (UNPREDICTABLE)";
1009 } else {
1010 args << Rt << ", FPSCR";
1011 }
1012 } else {
1013 args << "(PRIVILEGED)";
1014 }
1015 } else if (coproc == 11 && (op3 & 0x9) != 8) {
1016 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
1017 }
Ian Rogers9af89402012-09-07 11:29:35 -07001018 }
Dave Allison70202782013-10-22 17:52:19 -07001019 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001020 }
1021 break;
Ian Rogers40627db2012-03-04 17:31:09 -08001022 case 2:
1023 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
1024 // Data-processing (modified immediate)
1025 // |111|11|10|0000|0|0000|1|111|1100|00000000|
1026 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
1027 // |---|--|--|----|-|----|-|---|----|--------|
1028 // |332|22|22|2222|2|1111|1|111|1100|00000000|
1029 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
1030 // |---|--|--|----|-|----|-|---|----|--------|
1031 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
1032 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -08001033 uint32_t i = (instr >> 26) & 1;
1034 uint32_t op3 = (instr >> 21) & 0xF;
1035 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001036 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -08001037 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001038 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001039 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001040 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
1041 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
1042 if (op3 == 0x2) {
1043 opcode << "mov";
1044 if (S == 1) {
1045 opcode << "s";
1046 }
1047 opcode << ".w";
1048 } else {
1049 opcode << "mvn";
1050 if (S == 1) {
1051 opcode << "s";
1052 }
1053 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001054 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001055 } else if (Rd.r == 0xF && S == 1 &&
1056 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
1057 if (op3 == 0x0) {
1058 opcode << "tst";
1059 } else if (op3 == 0x4) {
1060 opcode << "teq";
1061 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +00001062 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001063 } else {
1064 opcode << "cmp.w";
1065 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001066 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001067 } else {
1068 switch (op3) {
1069 case 0x0: opcode << "and"; break;
1070 case 0x1: opcode << "bic"; break;
1071 case 0x2: opcode << "orr"; break;
1072 case 0x3: opcode << "orn"; break;
1073 case 0x4: opcode << "eor"; break;
1074 case 0x8: opcode << "add"; break;
1075 case 0xA: opcode << "adc"; break;
1076 case 0xB: opcode << "sbc"; break;
1077 case 0xD: opcode << "sub"; break;
1078 case 0xE: opcode << "rsb"; break;
1079 default: opcode << "UNKNOWN DPMI-" << op3; break;
1080 }
1081 if (S == 1) {
1082 opcode << "s";
1083 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001084 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001085 }
Ian Rogers40627db2012-03-04 17:31:09 -08001086 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1087 // Data-processing (plain binary immediate)
1088 // |111|11|10|00000|0000|1|111110000000000|
1089 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1090 // |---|--|--|-----|----|-|---------------|
1091 // |332|22|22|22222|1111|1|111110000000000|
1092 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1093 // |---|--|--|-----|----|-|---------------|
1094 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1095 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001096 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001097 case 0x00: case 0x0A: {
1098 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001099 ArmRegister Rd(instr, 8);
1100 ArmRegister Rn(instr, 16);
1101 uint32_t i = (instr >> 26) & 1;
1102 uint32_t imm3 = (instr >> 12) & 0x7;
1103 uint32_t imm8 = instr & 0xFF;
1104 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1105 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001106 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001107 args << Rd << ", " << Rn << ", #" << imm12;
1108 } else {
1109 opcode << "adr";
1110 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001111 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001112 }
1113 break;
1114 }
Ian Rogers55019132013-02-08 01:05:23 -08001115 case 0x04: case 0x0C: {
1116 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001117 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001118 uint32_t i = (instr >> 26) & 1;
1119 uint32_t imm3 = (instr >> 12) & 0x7;
1120 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001121 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001122 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001123 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001124 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001125 break;
1126 }
jeffhaoeae26912013-01-28 16:29:54 -08001127 case 0x16: {
1128 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
1129 ArmRegister Rd(instr, 8);
1130 ArmRegister Rn(instr, 16);
1131 uint32_t msb = instr & 0x1F;
1132 uint32_t imm2 = (instr >> 6) & 0x3;
1133 uint32_t imm3 = (instr >> 12) & 0x7;
1134 uint32_t lsb = (imm3 << 2) | imm2;
1135 uint32_t width = msb - lsb + 1;
1136 if (Rn.r != 0xF) {
1137 opcode << "bfi";
1138 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1139 } else {
1140 opcode << "bfc";
1141 args << Rd << ", #" << lsb << ", #" << width;
1142 }
1143 break;
1144 }
Ian Rogers40627db2012-03-04 17:31:09 -08001145 default:
1146 break;
1147 }
1148 } else {
1149 // Branches and miscellaneous control
1150 // |111|11|1000000|0000|1|111|1100|00000000|
1151 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1152 // |---|--|-------|----|-|---|----|--------|
1153 // |332|22|2222222|1111|1|111|1100|00000000|
1154 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1155 // |---|--|-------|----|-|---|----|--------|
1156 // |111|10| op2 | |1|op3|op4 | |
1157
1158 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001159 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001160 switch (op3) {
1161 case 0:
1162 if ((op2 & 0x38) != 0x38) {
1163 // Conditional branch
1164 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1165 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1166 // |---|--|-|----|------|-|-|--|-|--|-----------|
1167 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1168 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1169 // |---|--|-|----|------|-|-|--|-|--|-----------|
1170 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1171 uint32_t S = (instr >> 26) & 1;
1172 uint32_t J2 = (instr >> 11) & 1;
1173 uint32_t J1 = (instr >> 13) & 1;
1174 uint32_t imm6 = (instr >> 16) & 0x3F;
1175 uint32_t imm11 = instr & 0x7FF;
1176 uint32_t cond = (instr >> 22) & 0xF;
1177 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1178 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001179 opcode << "b";
1180 DumpCond(opcode, cond);
1181 opcode << ".w";
1182 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001183 } else if (op2 == 0x3B) {
1184 // Miscellaneous control instructions
1185 uint32_t op5 = (instr >> 4) & 0xF;
1186 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001187 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1188 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1189 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001190 }
Ian Rogers40627db2012-03-04 17:31:09 -08001191 }
1192 break;
1193 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001194 if ((op2 & 0x38) == 0x38) {
1195 if (op2 == 0x7F) {
1196 opcode << "udf";
1197 }
1198 break;
1199 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001200 FALLTHROUGH_INTENDED; // Else deliberate fall-through to B.
Ian Rogersd0876a92013-02-08 11:30:38 -08001201 case 1: case 3: {
1202 // B
1203 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1204 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1205 // |---|--|-|----|------|--|--|-|--|-----------|
1206 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1207 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1208 // |---|--|-|----|------|--|--|-|--|-----------|
1209 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1210 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1211 uint32_t S = (instr >> 26) & 1;
1212 uint32_t cond = (instr >> 22) & 0xF;
1213 uint32_t J2 = (instr >> 11) & 1;
1214 uint32_t form = (instr >> 12) & 1;
1215 uint32_t J1 = (instr >> 13) & 1;
1216 uint32_t imm10 = (instr >> 16) & 0x3FF;
1217 uint32_t imm6 = (instr >> 16) & 0x3F;
1218 uint32_t imm11 = instr & 0x7FF;
1219 opcode << "b";
1220 int32_t imm32;
1221 if (form == 0) {
1222 DumpCond(opcode, cond);
1223 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1224 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1225 } else {
1226 uint32_t I1 = ~(J1 ^ S);
1227 uint32_t I2 = ~(J2 ^ S);
1228 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1229 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1230 }
1231 opcode << ".w";
1232 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001233 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001234 }
Ian Rogers40627db2012-03-04 17:31:09 -08001235 case 4: case 6: case 5: case 7: {
1236 // BL, BLX (immediate)
1237 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1238 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1239 // |---|--|-|----------|--|--|-|--|-----------|
1240 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1241 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1242 // |---|--|-|----------|--|--|-|--|-----------|
Dave Allisond6ed6422014-04-09 23:36:15 +00001243 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
Ian Rogers40627db2012-03-04 17:31:09 -08001244 uint32_t S = (instr >> 26) & 1;
1245 uint32_t J2 = (instr >> 11) & 1;
Dave Allisond6ed6422014-04-09 23:36:15 +00001246 uint32_t L = (instr >> 12) & 1;
Ian Rogers40627db2012-03-04 17:31:09 -08001247 uint32_t J1 = (instr >> 13) & 1;
1248 uint32_t imm10 = (instr >> 16) & 0x3FF;
1249 uint32_t imm11 = instr & 0x7FF;
Dave Allisond6ed6422014-04-09 23:36:15 +00001250 if (L == 0) {
1251 opcode << "bx";
Dave Allisonf9487c02014-04-08 23:08:12 +00001252 } else {
Dave Allisond6ed6422014-04-09 23:36:15 +00001253 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001254 }
1255 uint32_t I1 = ~(J1 ^ S);
1256 uint32_t I2 = ~(J2 ^ S);
1257 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1258 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001259 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001260 break;
1261 }
1262 }
1263 }
1264 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001265 case 3:
1266 switch (op2) {
1267 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001268 case 0x08: case 0x09: case 0x0A: case 0x0C: case 0x0E: {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001269 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001270 // |111|11|100|000|0|0000|1111|110000|000000|
1271 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1272 // |---|--|---|---|-|----|----|------|------|
1273 // |332|22|222|222|2|1111|1111|110000|000000|
1274 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1275 // |---|--|---|---|-|----|----|------|------|
1276 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001277 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001278 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001279 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001280 case 0x0: case 0x4: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001281 // {ST,LD}RB Rt,[Rn,#+/-imm12] - 111 11 00 0 1 00 0 nnnn tttt 1 PUWii ii iiii
1282 // {ST,LD}RB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1283 // {ST,LD}RB Rt,[Rn,Rm,lsl #imm2] - 111 11 00 0 0 00 0 nnnn tttt 0 00000 ii mmmm
Elliott Hughes630e77d2012-03-22 19:20:56 -07001284 ArmRegister Rn(instr, 16);
1285 ArmRegister Rt(instr, 12);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001286 opcode << (HasBitSet(instr, 20) ? "ldrb" : "strb");
1287 if (HasBitSet(instr, 23)) {
1288 uint32_t imm12 = instr & 0xFFF;
1289 args << Rt << ", [" << Rn << ",#" << imm12 << "]";
1290 } else if ((instr & 0x800) != 0) {
1291 uint32_t imm8 = instr & 0xFF;
1292 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
1293 } else {
1294 uint32_t imm2 = (instr >> 4) & 3;
1295 ArmRegister Rm(instr, 0);
1296 args << Rt << ", [" << Rn << ", " << Rm;
1297 if (imm2 != 0) {
1298 args << ", " << "lsl #" << imm2;
1299 }
1300 args << "]";
1301 }
1302 break;
1303 }
1304 case 0x1: case 0x5: {
1305 // STRH Rt,[Rn,#+/-imm12] - 111 11 00 0 1 01 0 nnnn tttt 1 PUWii ii iiii
1306 // STRH Rt,[Rn,#+/-imm8] - 111 11 00 0 0 01 0 nnnn tttt 1 PUWii ii iiii
1307 // STRH Rt,[Rn,Rm,lsl #imm2] - 111 11 00 0 0 01 0 nnnn tttt 0 00000 ii mmmm
1308 ArmRegister Rn(instr, 16);
1309 ArmRegister Rt(instr, 12);
1310 opcode << "strh";
1311 if (HasBitSet(instr, 23)) {
1312 uint32_t imm12 = instr & 0xFFF;
1313 args << Rt << ", [" << Rn << ",#" << imm12 << "]";
1314 } else if ((instr & 0x800) != 0) {
Ian Rogers087b2412012-03-21 01:30:32 -07001315 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001316 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001317 } else {
1318 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001319 ArmRegister Rm(instr, 0);
1320 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001321 if (imm2 != 0) {
1322 args << ", " << "lsl #" << imm2;
1323 }
1324 args << "]";
1325 }
1326 break;
1327 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001328 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001329 ArmRegister Rn(instr, 16);
1330 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001331 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001332 if ((instr & 0x800) != 0) {
1333 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1334 uint32_t P = (instr >> 10) & 1;
1335 uint32_t U = (instr >> 9) & 1;
1336 uint32_t W = (instr >> 8) & 1;
1337 uint32_t imm8 = instr & 0xFF;
1338 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1339 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1340 opcode << "push";
Dave Allison20dfc792014-06-16 20:44:29 -07001341 args << "{" << Rt << "}";
Ian Rogers66a3fca2012-04-09 19:51:34 -07001342 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1343 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001344 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001345 if (P == 1 && U == 1 && W == 0) {
1346 opcode << "strt";
1347 } else {
1348 opcode << "str";
1349 }
1350 args << Rt << ", [" << Rn;
1351 if (P == 0 && W == 1) {
1352 args << "], #" << imm32;
1353 } else {
1354 args << ", #" << imm32 << "]";
1355 if (W == 1) {
1356 args << "!";
1357 }
Ian Rogers40627db2012-03-04 17:31:09 -08001358 }
1359 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001360 } else {
1361 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1362 ArmRegister Rn(instr, 16);
1363 ArmRegister Rt(instr, 12);
1364 ArmRegister Rm(instr, 0);
1365 uint32_t imm2 = (instr >> 4) & 3;
1366 opcode << "str.w";
1367 args << Rt << ", [" << Rn << ", " << Rm;
1368 if (imm2 != 0) {
1369 args << ", lsl #" << imm2;
1370 }
1371 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001372 }
1373 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001374 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001375 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001376 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001377 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001378 }
Ian Rogers40627db2012-03-04 17:31:09 -08001379 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001380 }
1381 }
1382
1383 break;
1384 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001385 case 0x03: case 0x0B: case 0x11: case 0x13: case 0x19: case 0x1B: { // 00xx011
1386 // Load byte/halfword
jeffhaoeae26912013-01-28 16:29:54 -08001387 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1388 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1389 // |---|--|--|---|--|-|----|----|------|------|
1390 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1391 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1392 // |---|--|--|---|--|-|----|----|------|------|
1393 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1394 // |111|11| op2 | | | imm12 |
1395 uint32_t op3 = (instr >> 23) & 3;
1396 ArmRegister Rn(instr, 16);
1397 ArmRegister Rt(instr, 12);
1398 if (Rt.r != 15) {
1399 if (op3 == 1) {
1400 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1401 uint32_t imm12 = instr & 0xFFF;
1402 opcode << "ldrh.w";
1403 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1404 if (Rn.r == 9) {
1405 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001406 Thread::DumpThreadOffset<4>(args, imm12);
jeffhaoeae26912013-01-28 16:29:54 -08001407 } else if (Rn.r == 15) {
1408 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1409 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
Ian Rogersff093b32014-04-30 19:04:27 -07001410 args << StringPrintf(" ; 0x%08x", *reinterpret_cast<int32_t*>(lit_adr));
jeffhaoeae26912013-01-28 16:29:54 -08001411 }
1412 } else if (op3 == 3) {
1413 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001414 // LDRSB.W Rt, [Rn, #imm12] - 111 11 00 11 001 nnnn tttt iiiiiiiiiiii
jeffhaoeae26912013-01-28 16:29:54 -08001415 uint32_t imm12 = instr & 0xFFF;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001416 opcode << (HasBitSet(instr, 20) ? "ldrsb.w" : "ldrsh.w");
jeffhaoeae26912013-01-28 16:29:54 -08001417 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1418 if (Rn.r == 9) {
1419 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001420 Thread::DumpThreadOffset<4>(args, imm12);
jeffhaoeae26912013-01-28 16:29:54 -08001421 } else if (Rn.r == 15) {
1422 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1423 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
Ian Rogersff093b32014-04-30 19:04:27 -07001424 args << StringPrintf(" ; 0x%08x", *reinterpret_cast<int32_t*>(lit_adr));
jeffhaoeae26912013-01-28 16:29:54 -08001425 }
1426 }
1427 }
1428 break;
1429 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001430 case 0x29: { // 0101001
1431 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1432 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1433 // |---|--|-------|----|----|----|--|---|----|
1434 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1435 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1436 // |---|--|-------|----|----|----|--|---|----|
1437 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1438 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1439 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1440 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1441 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1442 if ((instr & 0xf0c0) == 0xf080) {
1443 uint32_t op3 = (instr >> 4) & 3;
1444 opcode << kThumbReverseOperations[op3];
1445 ArmRegister Rm(instr, 0);
1446 ArmRegister Rd(instr, 8);
1447 args << Rd << ", " << Rm;
1448 ArmRegister Rm2(instr, 16);
1449 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1450 args << " (UNPREDICTABLE)";
1451 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001452 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001453 break;
1454 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001455 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001456 // Load word
1457 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1458 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1459 // |---|--|--|---|--|-|----|----|------|------|
1460 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1461 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1462 // |---|--|--|---|--|-|----|----|------|------|
1463 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1464 // |111|11| op2 | | | imm12 |
1465 uint32_t op3 = (instr >> 23) & 3;
1466 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001467 ArmRegister Rn(instr, 16);
1468 ArmRegister Rt(instr, 12);
1469 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001470 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1471 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1472 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001473 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001474 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001475 if (Rn.r == 9) {
1476 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001477 Thread::DumpThreadOffset<4>(args, imm12);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001478 } else if (Rn.r == 15) {
1479 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1480 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
Ian Rogersff093b32014-04-30 19:04:27 -07001481 args << StringPrintf(" ; 0x%08x", *reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001482 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001483 } else if (op4 == 0) {
1484 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1485 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001486 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001487 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001488 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001489 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001490 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001491 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001492 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001493 } else {
Dave Allison20dfc792014-06-16 20:44:29 -07001494 bool p = (instr & (1 << 10)) != 0;
1495 bool w = (instr & (1 << 8)) != 0;
1496 bool u = (instr & (1 << 9)) != 0;
1497 if (p && u && !w) {
1498 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1499 uint32_t imm8 = instr & 0xFF;
1500 opcode << "ldrt";
1501 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
1502 } else if (Rn.r == 13 && !p && u && w && (instr & 0xff) == 4) {
1503 // POP
1504 opcode << "pop";
1505 args << "{" << Rt << "}";
1506 } else {
1507 bool wback = !p || w;
1508 uint32_t offset = (instr & 0xff);
1509 opcode << "ldr.w";
1510 args << Rt << ",";
1511 if (p && !wback) {
1512 args << "[" << Rn << ", #" << offset << "]";
1513 } else if (p && wback) {
1514 args << "[" << Rn << ", #" << offset << "]!";
1515 } else if (!p && wback) {
1516 args << "[" << Rn << "], #" << offset;
1517 } else {
1518 LOG(FATAL) << p << " " << w;
1519 }
1520 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001521 }
1522 break;
1523 }
Dave Allison70202782013-10-22 17:52:19 -07001524 default: // more formats
1525 if ((op2 >> 4) == 2) { // 010xxxx
1526 // data processing (register)
Vladimir Markoc777e0d2014-04-03 17:59:02 +01001527 if ((instr & 0x0080f0f0) == 0x0000f000) {
1528 // LSL, LSR, ASR, ROR
1529 uint32_t shift_op = (instr >> 21) & 3;
1530 uint32_t S = (instr >> 20) & 1;
1531 ArmRegister Rd(instr, 8);
1532 ArmRegister Rn(instr, 16);
1533 ArmRegister Rm(instr, 0);
1534 opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
1535 args << Rd << ", " << Rn << ", " << Rm;
1536 }
Dave Allison70202782013-10-22 17:52:19 -07001537 } else if ((op2 >> 3) == 6) { // 0110xxx
1538 // Multiply, multiply accumulate, and absolute difference
1539 op1 = (instr >> 20) & 0x7;
1540 op2 = (instr >> 4) & 0x2;
1541 ArmRegister Ra(instr, 12);
1542 ArmRegister Rn(instr, 16);
1543 ArmRegister Rm(instr, 0);
1544 ArmRegister Rd(instr, 8);
1545 switch (op1) {
1546 case 0:
1547 if (op2 == 0) {
1548 if (Ra.r == 0xf) {
1549 opcode << "mul";
1550 args << Rd << ", " << Rn << ", " << Rm;
1551 } else {
1552 opcode << "mla";
1553 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1554 }
1555 } else {
1556 opcode << "mls";
1557 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1558 }
1559 break;
1560 case 1:
1561 case 2:
1562 case 3:
1563 case 4:
1564 case 5:
1565 case 6:
1566 break; // do these sometime
1567 }
1568 } else if ((op2 >> 3) == 7) { // 0111xxx
1569 // Long multiply, long multiply accumulate, and divide
1570 op1 = (instr >> 20) & 0x7;
1571 op2 = (instr >> 4) & 0xf;
1572 ArmRegister Rn(instr, 16);
1573 ArmRegister Rm(instr, 0);
1574 ArmRegister Rd(instr, 8);
1575 ArmRegister RdHi(instr, 8);
1576 ArmRegister RdLo(instr, 12);
1577 switch (op1) {
1578 case 0:
1579 opcode << "smull";
1580 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1581 break;
1582 case 1:
1583 opcode << "sdiv";
1584 args << Rd << ", " << Rn << ", " << Rm;
1585 break;
1586 case 2:
1587 opcode << "umull";
1588 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1589 break;
1590 case 3:
1591 opcode << "udiv";
1592 args << Rd << ", " << Rn << ", " << Rm;
1593 break;
1594 case 4:
1595 case 5:
1596 case 6:
1597 break; // TODO: when we generate these...
1598 }
1599 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001600 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07001601 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001602 default:
1603 break;
1604 }
Ian Rogers9af89402012-09-07 11:29:35 -07001605
1606 // Apply any IT-block conditions to the opcode if necessary.
1607 if (!it_conditions_.empty()) {
1608 opcode << it_conditions_.back();
1609 it_conditions_.pop_back();
1610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001611 if (opcode.str().size() == 0) {
1612 opcode << "UNKNOWN " << op2;
1613 }
Ian Rogers9af89402012-09-07 11:29:35 -07001614
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001615 os << FormatInstructionPointer(instr_ptr)
1616 << StringPrintf(": %08x\t%-7s ", instr, opcode.str().c_str())
1617 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001618 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001619} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620
1621size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1622 uint16_t instr = ReadU16(instr_ptr);
1623 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1624 if (is_32bit) {
1625 return DumpThumb32(os, instr_ptr);
1626 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001627 std::ostringstream opcode;
1628 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001629 uint16_t opcode1 = instr >> 10;
1630 if (opcode1 < 0x10) {
1631 // shift (immediate), add, subtract, move, and compare
1632 uint16_t opcode2 = instr >> 9;
1633 switch (opcode2) {
1634 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1635 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001636 // Logical shift left - 00 000xx iii mmm ddd
1637 // Logical shift right - 00 001xx iii mmm ddd
1638 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001639 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001640 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001641 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001642 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001643 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001644 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001645 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001646 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001647 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001649 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001650 break;
1651 }
1652 case 0xC: case 0xD: case 0xE: case 0xF: {
1653 // Add register - 00 01100 mmm nnn ddd
1654 // Sub register - 00 01101 mmm nnn ddd
1655 // Add 3-bit immediate - 00 01110 iii nnn ddd
1656 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1657 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001658 ThumbRegister Rn(instr, 3);
1659 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001660 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001661 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001662 } else {
1663 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001664 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001665 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001666 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667 }
1668 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001669 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001670 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001671 ArmRegister Rm(imm3_or_Rm);
1672 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001673 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001674 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001675 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001676 break;
1677 }
1678 case 0x10: case 0x11: case 0x12: case 0x13:
1679 case 0x14: case 0x15: case 0x16: case 0x17:
1680 case 0x18: case 0x19: case 0x1A: case 0x1B:
1681 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1682 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1683 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1684 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1685 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001686 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001687 uint16_t imm8 = instr & 0xFF;
1688 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001689 case 4: opcode << "movs"; break;
1690 case 5: opcode << "cmp"; break;
1691 case 6: opcode << "adds"; break;
1692 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001693 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001694 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001695 break;
1696 }
1697 default:
1698 break;
1699 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001700 } else if (opcode1 == 0x10) {
1701 // Data-processing
1702 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001703 ThumbRegister rm(instr, 3);
1704 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001705 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001706 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001707 } else if (opcode1 == 0x11) {
1708 // Special data instructions and branch and exchange
1709 uint16_t opcode2 = (instr >> 6) & 0x0F;
1710 switch (opcode2) {
1711 case 0x0: case 0x1: case 0x2: case 0x3: {
1712 // Add low registers - 010001 0000 xxxxxx
1713 // Add high registers - 010001 0001/001x xxxxxx
1714 uint16_t DN = (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 Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001717 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001718 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001719 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001720 break;
1721 }
1722 case 0x8: case 0x9: case 0xA: case 0xB: {
1723 // Move low registers - 010001 1000 xxxxxx
1724 // Move high registers - 010001 1001/101x xxxxxx
1725 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001726 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001727 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001728 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001729 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001730 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001731 break;
1732 }
1733 case 0x5: case 0x6: case 0x7: {
1734 // Compare high registers - 010001 0101/011x xxxxxx
1735 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001736 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001737 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001738 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001739 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001740 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001741 break;
1742 }
1743 case 0xC: case 0xD: case 0xE: case 0xF: {
1744 // Branch and exchange - 010001 110x xxxxxx
1745 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001746 ArmRegister rm(instr, 3);
1747 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1748 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001749 break;
1750 }
1751 default:
1752 break;
1753 }
jeffhaoeae26912013-01-28 16:29:54 -08001754 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1755 ThumbRegister Rt(instr, 8);
1756 uint16_t imm8 = instr & 0xFF;
1757 opcode << "ldr";
1758 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001759 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1760 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1761 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1762 // Load/store single data item
1763 uint16_t opA = (instr >> 12) & 0xF;
1764 if (opA == 0x5) {
1765 uint16_t opB = (instr >> 9) & 0x7;
1766 ThumbRegister Rm(instr, 6);
1767 ThumbRegister Rn(instr, 3);
1768 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001769 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001770 case 0: opcode << "str"; break;
1771 case 1: opcode << "strh"; break;
1772 case 2: opcode << "strb"; break;
1773 case 3: opcode << "ldrsb"; break;
1774 case 4: opcode << "ldr"; break;
1775 case 5: opcode << "ldrh"; break;
1776 case 6: opcode << "ldrb"; break;
1777 case 7: opcode << "ldrsh"; break;
1778 }
1779 args << Rt << ", [" << Rn << ", " << Rm << "]";
1780 } else if (opA == 9) {
1781 uint16_t opB = (instr >> 11) & 1;
1782 ThumbRegister Rt(instr, 8);
1783 uint16_t imm8 = instr & 0xFF;
1784 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001785 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001786 } else {
1787 uint16_t imm5 = (instr >> 6) & 0x1F;
1788 uint16_t opB = (instr >> 11) & 1;
1789 ThumbRegister Rn(instr, 3);
1790 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001791 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001792 case 6:
1793 imm5 <<= 2;
1794 opcode << (opB == 0 ? "str" : "ldr");
1795 break;
1796 case 7:
1797 imm5 <<= 0;
1798 opcode << (opB == 0 ? "strb" : "ldrb");
1799 break;
1800 case 8:
1801 imm5 <<= 1;
1802 opcode << (opB == 0 ? "strh" : "ldrh");
1803 break;
1804 }
1805 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1806 }
jeffhaoeae26912013-01-28 16:29:54 -08001807 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001808 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001809 uint32_t cond = (instr >> 8) & 0xF;
1810 opcode << "b";
1811 DumpCond(opcode, cond);
1812 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001813 } else if ((instr & 0xF800) == 0xA800) {
1814 // Generate SP-relative address
1815 ThumbRegister rd(instr, 8);
1816 int imm8 = instr & 0xFF;
1817 opcode << "add";
1818 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001819 } else if ((instr & 0xF000) == 0xB000) {
1820 // Miscellaneous 16-bit instructions
1821 uint16_t opcode2 = (instr >> 5) & 0x7F;
1822 switch (opcode2) {
1823 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1824 // Add immediate to SP - 1011 00000 ii iiiii
1825 // Subtract immediate from SP - 1011 00001 ii iiiii
1826 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001827 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001828 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001829 break;
1830 }
Ian Rogers087b2412012-03-21 01:30:32 -07001831 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001832 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001833 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1834 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001835 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001836 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1837 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1838 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001839 // CBNZ, CBZ
1840 uint16_t op = (instr >> 11) & 1;
1841 uint16_t i = (instr >> 9) & 1;
1842 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001843 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001844 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001845 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001846 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001847 DumpBranchTarget(args, instr_ptr + 4, imm32);
1848 break;
1849 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001850 case 0x50: case 0x51: // 101000x
1851 case 0x52: case 0x53: // 101001x
1852 case 0x56: case 0x57: { // 101011x
1853 uint16_t op = (instr >> 6) & 3;
1854 opcode << kThumbReverseOperations[op];
1855 ThumbRegister Rm(instr, 3);
1856 ThumbRegister Rd(instr, 0);
1857 args << Rd << ", " << Rm;
1858 break;
1859 }
Ian Rogers40627db2012-03-04 17:31:09 -08001860 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1861 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1862 // If-Then, and hints
1863 uint16_t opA = (instr >> 4) & 0xF;
1864 uint16_t opB = instr & 0xF;
1865 if (opB == 0) {
1866 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001867 case 0: opcode << "nop"; break;
1868 case 1: opcode << "yield"; break;
1869 case 2: opcode << "wfe"; break;
1870 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001871 default: break;
1872 }
1873 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001874 uint32_t first_cond = opA;
1875 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001876 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001877
1878 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1879 // and store up the actual condition codes we'll want to add to the next few opcodes.
1880 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001881 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001882 for (size_t i = 0; i < count; ++i) {
1883 bool positive_cond = ((first_cond & 1) != 0);
1884 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1885 if (positive_mask == positive_cond) {
1886 opcode << 't';
1887 it_conditions_[i] = kConditionCodeNames[first_cond];
1888 } else {
1889 opcode << 'e';
1890 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1891 }
1892 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001893 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001894
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001895 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1896 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001897 }
1898 break;
1899 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001900 default:
1901 break;
1902 }
1903 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1904 ((instr & 0xE000) == 0x8000)) {
1905 // Load/store single data item
1906 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001907 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001908 switch (opA) {
1909 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001910 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1911 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001912 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001913 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001914 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001915 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1916 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001917 break;
1918 }
1919 case 0x9: {
1920 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1921 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1922 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001923 ThumbRegister Rt(instr, 8);
1924 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1925 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001926 break;
1927 }
1928 default:
1929 break;
1930 }
Ian Rogers40627db2012-03-04 17:31:09 -08001931 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1932 uint16_t imm11 = instr & 0x7FFF;
1933 int32_t imm32 = imm11 << 1;
1934 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001935 opcode << "b";
1936 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001937 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001938
1939 // Apply any IT-block conditions to the opcode if necessary.
1940 if (!it_conditions_.empty()) {
1941 opcode << it_conditions_.back();
1942 it_conditions_.pop_back();
1943 }
1944
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001945 os << FormatInstructionPointer(instr_ptr)
1946 << StringPrintf(": %04x \t%-7s ", instr, opcode.str().c_str())
1947 << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001948 }
1949 return 2;
1950}
1951
1952} // namespace arm
1953} // namespace art