blob: 05e7576085af2c6a17861ffea9ec0e21426478b5 [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>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080024#include "base/stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070025#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070026
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080027namespace art {
28namespace arm {
29
Ian Rogersb23a7722012-10-09 16:54:26 -070030size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
31 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
32 DumpArm(os, begin);
33 return 4;
34 } else {
35 // remove thumb specifier bits
36 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
37 return DumpThumb16(os, begin);
38 }
39}
40
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080041void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
42 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
43 for (const uint8_t* cur = begin; cur < end; cur += 4) {
44 DumpArm(os, cur);
45 }
46 } else {
47 // remove thumb specifier bits
48 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
49 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
50 for (const uint8_t* cur = begin; cur < end;) {
51 cur += DumpThumb16(os, cur);
52 }
53 }
54}
55
Elliott Hughes77405792012-03-15 15:22:12 -070056static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070057 "eq", // 0000 - equal
58 "ne", // 0001 - not-equal
59 "cs", // 0010 - carry-set, greater than, equal or unordered
60 "cc", // 0011 - carry-clear, less than
61 "mi", // 0100 - minus, negative
62 "pl", // 0101 - plus, positive or zero
63 "vs", // 0110 - overflow
64 "vc", // 0111 - no overflow
65 "hi", // 1000 - unsigned higher
66 "ls", // 1001 - unsigned lower or same
67 "ge", // 1010 - signed greater than or equal
68 "lt", // 1011 - signed less than
69 "gt", // 1100 - signed greater than
70 "le", // 1101 - signed less than or equal
71 "", // 1110 - always
72 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080073};
74
75void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
76 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070077 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080078 } else {
79 os << "Unexpected condition: " << cond;
80 }
81}
82
Ian Rogersb122a4b2013-11-19 18:00:50 -080083void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
84 switch (domain) {
85 case 0b1111: os << "sy"; break;
86 case 0b1110: os << "st"; break;
87 case 0b1011: os << "ish"; break;
88 case 0b1010: os << "ishst"; break;
89 case 0b0111: os << "nsh"; break;
90 case 0b0110: os << "nshst"; break;
91 case 0b0011: os << "osh"; break;
92 case 0b0010: os << "oshst"; break;
93 }
94}
95
Ian Rogers40627db2012-03-04 17:31:09 -080096void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Elliott Hughes1ca98492012-04-12 17:21:02 -070097 os << StringPrintf("%+d (%p)", imm32, instr_ptr + imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080098}
99
100static uint32_t ReadU16(const uint8_t* ptr) {
101 return ptr[0] | (ptr[1] << 8);
102}
103
104static uint32_t ReadU32(const uint8_t* ptr) {
105 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
106}
107
Elliott Hughes77405792012-03-15 15:22:12 -0700108static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700109 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
110 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700111};
112
Ian Rogersad03ef52012-03-18 19:34:47 -0700113static const char* kThumbDataProcessingOperations[] = {
114 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
115 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
116};
117
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100118static const char* const kThumb2ShiftOperations[] = {
119 "lsl", "lsr", "asr", "ror"
120};
121
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100122static const char* kThumbReverseOperations[] = {
123 "rev", "rev16", "rbit", "revsh"
124};
125
Elliott Hughes77405792012-03-15 15:22:12 -0700126struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -0700127 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700128 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -0700129 uint32_t r;
130};
131std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
132 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700133 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700134 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700135 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700136 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700137 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700138 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700139 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700140 }
141 return os;
142}
143
Elliott Hughes630e77d2012-03-22 19:20:56 -0700144struct ThumbRegister : ArmRegister {
145 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700146};
147
148struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700149 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700150 uint32_t shift;
151 ArmRegister rm;
152};
153std::ostream& operator<<(std::ostream& os, const Rm& r) {
154 os << r.rm;
155 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700156 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700157 }
158 return os;
159}
160
Elliott Hughes1ca98492012-04-12 17:21:02 -0700161struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700162 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700163 uint32_t rotate = ((instruction >> 8) & 0xf);
164 uint32_t imm = (instruction & 0xff);
165 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
166 }
167 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700168};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700169std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700170 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700171 return os;
172}
173
174struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700175 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700176 uint32_t register_list;
177};
178std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
179 if (rhs.register_list == 0) {
180 os << "<no register list?>";
181 return os;
182 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700183 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700184 bool first = true;
185 for (size_t i = 0; i < 16; i++) {
186 if ((rhs.register_list & (1 << i)) != 0) {
187 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700188 first = false;
189 } else {
190 os << ", ";
191 }
192 os << ArmRegister(i);
193 }
194 }
195 os << "}";
196 return os;
197}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800198
Vladimir Markodd577a32013-11-07 19:25:24 +0000199struct FpRegister {
200 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
201 size = (instr >> 8) & 1;
202 uint32_t Vn = (instr >> at_bit) & 0xF;
203 uint32_t N = (instr >> extra_at_bit) & 1;
204 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
205 }
Zheng Xue19649a2014-02-27 13:30:55 +0000206 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit,
207 uint32_t forced_size) {
208 size = forced_size;
209 uint32_t Vn = (instr >> at_bit) & 0xF;
210 uint32_t N = (instr >> extra_at_bit) & 1;
211 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
212 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000213 FpRegister(const FpRegister& other, uint32_t offset)
214 : size(other.size), r(other.r + offset) {}
215
216 uint32_t size; // 0 = f32, 1 = f64
217 uint32_t r;
218};
219std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
220 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
221}
222
223struct FpRegisterRange {
224 explicit FpRegisterRange(uint32_t instr)
225 : first(instr, 12, 22), imm8(instr & 0xFF) {}
226 FpRegister first;
227 uint32_t imm8;
228};
229std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
230 os << "{" << rhs.first;
231 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
232 if (count > 1) {
233 os << "-" << FpRegister(rhs.first, count - 1);
234 }
235 if (rhs.imm8 == 0) {
236 os << " (EMPTY)";
237 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
238 os << rhs.first << " (HALF)";
239 }
240 os << "}";
241 return os;
242}
243
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800244void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700245 uint32_t instruction = ReadU32(instr_ptr);
246 uint32_t cond = (instruction >> 28) & 0xf;
247 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700248 std::string opcode;
249 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700250 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700251 switch (op1) {
252 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700253 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700254 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700255 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700256 opcode = "bkpt";
257 uint32_t imm12 = (instruction >> 8) & 0xfff;
258 uint32_t imm4 = (instruction & 0xf);
259 args << '#' << ((imm12 << 4) | imm4);
260 break;
261 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700262 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700263 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700264 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700265 break;
266 }
267 bool i = (instruction & (1 << 25)) != 0;
268 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700269 uint32_t op = (instruction >> 21) & 0xf;
270 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700271 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700272 if (implicit_s) {
273 // Rd is unused (and not shown), and we don't show the 's' suffix either.
274 } else {
275 if (s) {
276 suffixes += 's';
277 }
278 args << ArmRegister(instruction, 12) << ", ";
279 }
Elliott Hughes77405792012-03-15 15:22:12 -0700280 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700281 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700282 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700283 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700284 }
285 }
286 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700287 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700288 {
289 bool p = (instruction & (1 << 24)) != 0;
290 bool b = (instruction & (1 << 22)) != 0;
291 bool w = (instruction & (1 << 21)) != 0;
292 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700293 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700294 args << ArmRegister(instruction, 12) << ", ";
295 ArmRegister rn(instruction, 16);
296 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700297 UNIMPLEMENTED(FATAL) << "literals";
298 } else {
299 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700300 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700301 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700302 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700303 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700304 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700305 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700306 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700307 } else {
308 LOG(FATAL) << p << " " << w;
309 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700310 if (rn.r == 9) {
311 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -0700312 Thread::DumpThreadOffset<4>(args, offset);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700313 }
Elliott Hughes77405792012-03-15 15:22:12 -0700314 }
315 }
316 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700317 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700318 {
319 bool p = (instruction & (1 << 24)) != 0;
320 bool u = (instruction & (1 << 23)) != 0;
321 bool w = (instruction & (1 << 21)) != 0;
322 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700323 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700324 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700325 }
326 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700327 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700328 {
329 bool bl = (instruction & (1 << 24)) != 0;
330 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700331 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700332 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700333 DumpBranchTarget(args, instr_ptr + 8, imm32);
334 }
335 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700336 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700337 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700338 break;
339 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700340 opcode += kConditionCodeNames[cond];
341 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700342 // TODO: a more complete ARM disassembler could generate wider opcodes.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800343 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800344}
345
Ian Rogersa9650dd2013-10-04 08:23:32 -0700346int32_t ThumbExpand(int32_t imm12) {
347 if ((imm12 & 0xC00) == 0) {
348 switch ((imm12 >> 8) & 3) {
349 case 0:
350 return imm12 & 0xFF;
351 case 1:
352 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
353 case 2:
354 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
355 default: // 3
356 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
357 (imm12 & 0xFF);
358 }
359 } else {
360 uint32_t val = 0x80 | (imm12 & 0x7F);
361 int32_t rotate = (imm12 >> 7) & 0x1F;
362 return (val >> rotate) | (val << (32 - rotate));
363 }
364}
365
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100366uint32_t VFPExpand32(uint32_t imm8) {
367 CHECK_EQ(imm8 & 0xffu, imm8);
368 uint32_t bit_a = (imm8 >> 7) & 1;
369 uint32_t bit_b = (imm8 >> 6) & 1;
370 uint32_t slice = imm8 & 0x3f;
371 return (bit_a << 31) | ((1 << 30) - (bit_b << 25)) | (slice << 19);
372}
373
374uint64_t VFPExpand64(uint32_t imm8) {
375 CHECK_EQ(imm8 & 0xffu, imm8);
376 uint64_t bit_a = (imm8 >> 7) & 1;
377 uint64_t bit_b = (imm8 >> 6) & 1;
378 uint64_t slice = imm8 & 0x3f;
379 return (bit_a << 31) | ((UINT64_C(1) << 62) - (bit_b << 54)) | (slice << 48);
380}
381
382uint64_t AdvSIMDExpand(uint32_t op, uint32_t cmode, uint32_t imm8) {
383 CHECK_EQ(op & 1, op);
384 CHECK_EQ(cmode & 0xf, cmode);
385 CHECK_EQ(imm8 & 0xff, imm8);
386 int32_t cmode321 = cmode >> 1;
387 if (imm8 == 0 && cmode321 != 0 && cmode321 != 4 && cmode321 != 7) {
388 return INT64_C(0x00000000deadbeef); // UNPREDICTABLE
389 }
390 uint64_t imm = imm8;
391 switch (cmode321) {
392 case 3: imm <<= 8; // Fall through.
393 case 2: imm <<= 8; // Fall through.
394 case 1: imm <<= 8; // Fall through.
395 case 0: return static_cast<int64_t>((imm << 32) | imm);
396 case 5: imm <<= 8; // Fall through.
397 case 4: return static_cast<int64_t>((imm << 48) | (imm << 32) | (imm << 16) | imm);
398 case 6:
399 imm = ((imm + 1u) << ((cmode & 1) != 0 ? 16 : 8)) - 1u; // Add 8 or 16 ones.
400 return static_cast<int64_t>((imm << 32) | imm);
401 default:
402 CHECK_EQ(cmode321, 7);
403 if ((cmode & 1) == 0 && op == 0) {
404 imm = (imm << 8) | imm;
405 return static_cast<int64_t>((imm << 48) | (imm << 32) | (imm << 16) | imm);
406 } else if ((cmode & 1) == 0 && op != 0) {
407 for (int i = 1; i != 8; ++i) {
408 imm |= ((imm >> i) & UINT64_C(1)) << (i * 8);
409 }
410 imm = imm & ~UINT64_C(0xfe);
411 return static_cast<int64_t>((imm << 8) - imm);
412 } else if ((cmode & 1) != 0 && op == 0) {
413 imm = static_cast<uint32_t>(VFPExpand32(imm8));
414 return static_cast<int64_t>((imm << 32) | imm);
415 } else {
416 return INT64_C(0xdeadbeef00000000); // UNDEFINED
417 }
418 }
419}
420
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800421size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
422 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
423 // |111|1 1|1000000|0000|1111110000000000|
424 // |5 3|2 1|0987654|3 0|5 0 5 0|
425 // |---|---|-------|----|----------------|
426 // |332|2 2|2222222|1111|1111110000000000|
427 // |1 9|8 7|6543210|9 6|5 0 5 0|
428 // |---|---|-------|----|----------------|
429 // |111|op1| op2 | | |
430 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700431 if (op1 == 0) {
432 return DumpThumb16(os, instr_ptr);
433 }
434
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800435 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700436 std::ostringstream opcode;
437 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800438 switch (op1) {
439 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800440 break;
441 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700442 if ((op2 & 0x64) == 0) { // 00x x0xx
443 // |111|11|10|00|0|00|0000|1111110000000000|
444 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
445 // |---|--|--|--|-|--|----|----------------|
446 // |332|22|22|22|2|22|1111|1111110000000000|
447 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
448 // |---|--|--|--|-|--|----|----------------|
449 // |111|01|00|op|0|WL| Rn | |
450 // |111|01| op2 | | |
451 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
452 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
453 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
454 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
455 uint32_t op = (instr >> 23) & 3;
456 uint32_t W = (instr >> 21) & 1;
457 uint32_t L = (instr >> 20) & 1;
458 ArmRegister Rn(instr, 16);
459 if (op == 1 || op == 2) {
460 if (op == 1) {
461 if (L == 0) {
462 opcode << "stm";
463 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800464 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700465 if (Rn.r != 13) {
466 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700467 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700468 } else {
469 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800470 }
471 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700472 } else {
473 if (L == 0) {
474 if (Rn.r != 13) {
475 opcode << "stmdb";
476 args << Rn << (W == 0 ? "" : "!") << ", ";
477 } else {
478 opcode << "push";
479 }
480 } else {
481 opcode << "ldmdb";
482 args << Rn << (W == 0 ? "" : "!") << ", ";
483 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700485 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800486 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700487 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700488 uint32_t op3 = (instr >> 23) & 3;
489 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700490 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700491 ArmRegister Rn(instr, 16);
492 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700493 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700494 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700495 if ((op3 & 2) == 2) { // 1x
496 int W = (instr >> 21) & 1;
497 int U = (instr >> 23) & 1;
498 int P = (instr >> 24) & 1;
499
500 if ((op4 & 1) == 1) {
501 opcode << "ldrd";
502 } else {
503 opcode << "strd";
504 }
505 args << Rt << "," << Rd << ", [" << Rn;
506 const char *sign = U ? "+" : "-";
507 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000508 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700509 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000510 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700511 if (W == 1) {
512 args << "!";
513 }
514 }
515 } else { // 0x
516 switch (op4) {
517 case 0:
518 if (op3 == 0) { // op3 is 00, op4 is 00
519 opcode << "strex";
520 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000521 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
522 Rd.r == Rn.r || Rd.r == Rt.r) {
523 args << " (UNPREDICTABLE)";
524 }
Dave Allison70202782013-10-22 17:52:19 -0700525 } else { // op3 is 01, op4 is 00
526 // this is one of strexb, strexh or strexd
527 int op5 = (instr >> 4) & 0xf;
528 switch (op5) {
529 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700530 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000531 opcode << ((op5 == 4) ? "strexb" : "strexh");
532 Rd = ArmRegister(instr, 0);
533 args << Rd << ", " << Rt << ", [" << Rn << "]";
534 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
535 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
536 args << " (UNPREDICTABLE)";
537 }
Dave Allison70202782013-10-22 17:52:19 -0700538 break;
539 case 7:
540 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000541 ArmRegister Rt2 = Rd;
542 Rd = ArmRegister(instr, 0);
543 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
544 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
545 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
546 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
547 args << " (UNPREDICTABLE)";
548 }
Dave Allison70202782013-10-22 17:52:19 -0700549 break;
550 }
551 }
552 break;
553 case 1:
554 if (op3 == 0) { // op3 is 00, op4 is 01
555 opcode << "ldrex";
556 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000557 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
558 args << " (UNPREDICTABLE)";
559 }
Dave Allison70202782013-10-22 17:52:19 -0700560 } else { // op3 is 01, op4 is 01
561 // this is one of strexb, strexh or strexd
562 int op5 = (instr >> 4) & 0xf;
563 switch (op5) {
564 case 0:
565 opcode << "tbb";
566 break;
567 case 1:
568 opcode << "tbh";
569 break;
570 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700571 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000572 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
573 args << Rt << ", [" << Rn << "]";
574 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
575 args << " (UNPREDICTABLE)";
576 }
Dave Allison70202782013-10-22 17:52:19 -0700577 break;
578 case 7:
579 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000580 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
581 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
582 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
583 args << " (UNPREDICTABLE)";
584 }
Dave Allison70202782013-10-22 17:52:19 -0700585 break;
586 }
587 }
588 break;
589 case 2: // op3 is 0x, op4 is 10
590 case 3: // op3 is 0x, op4 is 11
591 if (op4 == 2) {
592 opcode << "strd";
593 } else {
594 opcode << "ldrd";
595 }
596 int W = (instr >> 21) & 1;
597 int U = (instr >> 23) & 1;
598 int P = (instr >> 24) & 1;
599
600 args << Rt << "," << Rd << ", [" << Rn;
601 const char *sign = U ? "+" : "-";
602 if (P == 0 && W == 1) {
603 args << "], #" << sign << imm8;
604 } else {
605 args << ", #" << sign << imm8 << "]";
606 if (W == 1) {
607 args << "!";
608 }
609 }
610 break;
611 }
612 }
613
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700614 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
615 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100616 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
617 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
618 // |---|----|----|-|----|----|----|--|--|----|
619 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
620 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
621 // |---|----|----|-|----|----|----|--|--|----|
622 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700623 uint32_t op3 = (instr >> 21) & 0xF;
624 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100625 uint32_t imm3 = ((instr >> 12) & 0x7);
626 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700627 uint32_t imm5 = ((imm3 << 2) | imm2);
628 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700629 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100630 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700631 ArmRegister Rm(instr, 0);
632 switch (op3) {
633 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100634 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700635 opcode << "and";
636 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700637 if (S != 1U) {
638 opcode << "UNKNOWN TST-" << S;
639 break;
640 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700641 opcode << "tst";
642 S = 0; // don't print 's'
643 }
644 break;
645 case 0x1: opcode << "bic"; break;
646 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100647 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700648 opcode << "orr";
649 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100650 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700651 opcode << "mov";
652 }
653 break;
654 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100655 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700656 opcode << "orn";
657 } else {
658 opcode << "mvn";
659 }
660 break;
661 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100662 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700663 opcode << "eor";
664 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700665 if (S != 1U) {
666 opcode << "UNKNOWN TEQ-" << S;
667 break;
668 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700669 opcode << "teq";
670 S = 0; // don't print 's'
671 }
672 break;
673 case 0x6: opcode << "pkh"; break;
674 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100675 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700676 opcode << "add";
677 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700678 if (S != 1U) {
679 opcode << "UNKNOWN CMN-" << S;
680 break;
681 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700682 opcode << "cmn";
683 S = 0; // don't print 's'
684 }
685 break;
686 case 0xA: opcode << "adc"; break;
687 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100688 case 0xD:
689 if (Rd.r != 0xF) {
690 opcode << "sub";
691 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700692 if (S != 1U) {
693 opcode << "UNKNOWN CMP-" << S;
694 break;
695 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100696 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100697 S = 0; // don't print 's'
698 }
699 break;
700 case 0xE: opcode << "rsb"; break;
701 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700702 }
Ian Rogers087b2412012-03-21 01:30:32 -0700703
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700704 if (S == 1) {
705 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700706 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700707 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100708
709 if (Rd.r != 0xF) {
710 args << Rd << ", ";
711 }
712 if (Rn.r != 0xF) {
713 args << Rn << ", ";
714 }
715 args << Rm;
716
717 // Shift operand.
718 bool noShift = (imm5 == 0 && shift_type != 0x3);
719 if (!noShift) {
720 args << ", ";
721 switch (shift_type) {
722 case 0x0: args << "lsl"; break;
723 case 0x1: args << "lsr"; break;
724 case 0x2: args << "asr"; break;
725 case 0x3:
726 if (imm5 == 0) {
727 args << "rrx";
728 } else {
729 args << "ror";
730 }
731 break;
732 }
733 if (shift_type != 0x3 /* rrx */) {
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700734 args << StringPrintf(" #%d", (0 != imm5 || 0 == shift_type) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100735 }
736 }
737
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700738 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
739 // Co-processor instructions
740 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
741 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
742 // |---|-|--|------|----|----|----|---|---|----|
743 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
744 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
745 // |---|-|--|------|----|----|----|---|---|----|
746 // |111| |11| op3 | Rn | |copr| |op4| |
747 uint32_t op3 = (instr >> 20) & 0x3F;
748 uint32_t coproc = (instr >> 8) & 0xF;
749 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700750
Ian Rogersef6a7762013-12-19 17:58:05 -0800751 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000752 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
753 // Extension register load/store instructions
754 // |1111|110|00000|0000|1111|110|0|00000000|
755 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
756 // |----|---|-----|----|----|---|-|--------|
757 // |3322|222|22222|1111|1111|110|0|00000000|
758 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
759 // |----|---|-----|----|----|---|-|--------|
760 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700761 uint32_t P = (instr >> 24) & 1;
762 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700763 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000764 if (P == U && W == 1) {
765 opcode << "UNDEFINED";
766 } else {
767 uint32_t L = (instr >> 20) & 1;
768 uint32_t S = (instr >> 8) & 1;
769 ArmRegister Rn(instr, 16);
770 if (P == 1 && W == 0) { // VLDR
771 FpRegister d(instr, 12, 22);
772 uint32_t imm8 = instr & 0xFF;
773 opcode << (L == 1 ? "vldr" : "vstr");
774 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
775 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800776 if (Rn.r == 15 && U == 1) {
777 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
778 lit_adr = RoundDown(lit_adr, 4) + 4 + (imm8 << 2);
Brian Carlstromc2687ef2014-03-13 15:12:11 -0700779 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (2)));
780 args << StringPrintf(" ; 0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
Ian Rogersef6a7762013-12-19 17:58:05 -0800781 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000782 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
783 opcode << (L == 1 ? "vpop" : "vpush");
784 args << FpRegisterRange(instr);
785 } else { // VLDM
786 opcode << (L == 1 ? "vldm" : "vstm");
787 args << Rn << ((W == 1) ? "!" : "") << ", "
788 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700789 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000790 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700791 }
Dave Allison70202782013-10-22 17:52:19 -0700792 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000793 if ((instr & 0xD0) == 0x10) {
794 // 64bit transfers between ARM core and extension registers.
795 uint32_t L = (instr >> 20) & 1;
796 uint32_t S = (instr >> 8) & 1;
797 ArmRegister Rt2(instr, 16);
798 ArmRegister Rt(instr, 12);
799 FpRegister m(instr, 0, 5);
800 opcode << "vmov" << (S ? ".f64" : ".f32");
801 if (L == 1) {
802 args << Rt << ", " << Rt2 << ", ";
803 }
804 if (S) {
805 args << m;
806 } else {
807 args << m << ", " << FpRegister(m, 1);
808 }
809 if (L == 0) {
810 args << ", " << Rt << ", " << Rt2;
811 }
812 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
813 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
814 args << " (UNPREDICTABLE)";
815 }
816 }
Dave Allison70202782013-10-22 17:52:19 -0700817 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
818 // fp data processing
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100819 // VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
820 // |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
821 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
822 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
823 // |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
824 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
825 // |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
826 // |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
827 // |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
828 // |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
829 // |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
830 // |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
831 // |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
832 // |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
833 // |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
834 // |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
835 // |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
836 uint32_t S = (instr >> 8) & 1;
837 uint32_t Q = (instr >> 6) & 1;
838 FpRegister d(instr, 12, 22);
839 FpRegister n(instr, 16, 7);
840 FpRegister m(instr, 0, 5);
Zheng Xue19649a2014-02-27 13:30:55 +0000841 if ((op3 & 0xB) == 0) { // 100x00
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100842 opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
Zheng Xue19649a2014-02-27 13:30:55 +0000843 args << d << ", " << n << ", " << m;
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100844 } else if ((op3 & 0xB) == 0x2) { // 100x10
845 opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
846 args << d << ", " << n << ", " << m;
847 } else if ((op3 & 0xB) == 0x3) { // 100x11
848 opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
849 args << d << ", " << n << ", " << m;
850 } else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
851 opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
852 args << d << ", " << n << ", " << m;
853 } else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
854 uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
855 opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
856 args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
857 : StringPrintf("0x%08x", VFPExpand32(imm8)));
858 if ((instr & 0xa0) != 0) {
859 args << " (UNPREDICTABLE)";
860 }
861 } else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
862 // VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
863 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
864 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
865 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
866 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
867 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
868 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
869 // |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
870 // |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
871 // |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
872 // |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
873 // |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
874 // |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
875 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
876 uint32_t op5 = (instr >> 16) & 0xF;
877 uint32_t op = (instr >> 7) & 1;
878 // Register types in VCVT instructions rely on the combination of op5 and S.
879 FpRegister Dd(instr, 12, 22, 1);
880 FpRegister Sd(instr, 12, 22, 0);
881 FpRegister Dm(instr, 0, 5, 1);
882 FpRegister Sm(instr, 0, 5, 0);
883 if (op5 == 0) {
884 opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
885 args << d << ", " << m;
886 } else if (op5 == 1) {
887 opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
888 args << d << ", " << m;
889 } else if (op5 == 4) {
890 opcode << "vcmp" << (S != 0 ? ".f64" : ".f32");
891 args << d << ", " << m;
892 if (op != 0) {
893 args << " (quiet nan)";
894 }
895 } else if (op5 == 5) {
896 opcode << "vcmpe" << (S != 0 ? ".f64" : ".f32");
897 args << d << ", #0.0";
898 if (op != 0) {
899 args << " (quiet nan)";
900 }
901 if ((instr & 0x2f) != 0) {
902 args << " (UNPREDICTABLE)";
903 }
904 } else if (op5 == 0xD) {
905 if (S == 1) {
906 // vcvt{r}.s32.f64
907 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
908 args << Sd << ", " << Dm;
909 } else {
910 // vcvt{r}.s32.f32
911 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
912 args << Sd << ", " << Sm;
913 }
914 } else if (op5 == 0xC) {
915 if (S == 1) {
916 // vcvt{r}.u32.f64
917 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
918 args << Sd << ", " << Dm;
919 } else {
920 // vcvt{r}.u32.f32
921 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
922 args << Sd << ", " << Sm;
923 }
924 } else if (op5 == 0x8) {
925 if (S == 1) {
926 // vcvt.f64.<Tm>
927 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
928 args << Dd << ", " << Sm;
929 } else {
930 // vcvt.f32.<Tm>
931 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
932 args << Sd << ", " << Sm;
933 }
934 } else if (op5 == 0x7) {
935 if (op == 1) {
Zheng Xue19649a2014-02-27 13:30:55 +0000936 if (S == 1) {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100937 // vcvt.f64.f32
938 opcode << "vcvt.f64.f32";
Zheng Xue19649a2014-02-27 13:30:55 +0000939 args << Dd << ", " << Sm;
940 } else {
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100941 // vcvt.f32.f64
942 opcode << "vcvt.f32.f64";
943 args << Sd << ", " << Dm;
Zheng Xue19649a2014-02-27 13:30:55 +0000944 }
945 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100946 } else if ((op5 & 0xa) == 0xa) {
947 opcode << "vcvt";
948 args << "[undecoded: floating <-> fixed]";
Zheng Xue19649a2014-02-27 13:30:55 +0000949 }
950 }
Dave Allison70202782013-10-22 17:52:19 -0700951 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000952 if (coproc == 10 && (op3 & 0xE) == 0) {
953 // VMOV (between ARM core register and single-precision register)
954 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
955 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
956 // |----|----|---|- |----|----|----|-|--|-|----|
957 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
958 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
959 // |----|----|---|- |----|----|----|-|--|-|----|
960 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
961 uint32_t op = op3 & 1;
962 ArmRegister Rt(instr, 12);
963 FpRegister n(instr, 16, 7);
964 opcode << "vmov.f32";
965 if (op) {
966 args << Rt << ", " << n;
967 } else {
968 args << n << ", " << Rt;
969 }
970 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
971 args << " (UNPREDICTABLE)";
972 }
973 } else if (coproc == 10 && op3 == 0x2F) {
974 // VMRS
975 // |1111|11000000|0000|1111|1100|000|0|0000|
976 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
977 // |----|--------|----|----|----|---|-|----|
978 // |3322|22222222|1111|1111|1100|000|0|0000|
979 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
980 // |----|--------|----|----|----|---|-|----|
981 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
982 uint32_t spec_reg = (instr >> 16) & 0xF;
983 ArmRegister Rt(instr, 12);
984 opcode << "vmrs";
985 if (spec_reg == 1) {
986 if (Rt.r == 15) {
987 args << "APSR_nzcv, FPSCR";
988 } else if (Rt.r == 13) {
989 args << Rt << ", FPSCR (UNPREDICTABLE)";
990 } else {
991 args << Rt << ", FPSCR";
992 }
993 } else {
994 args << "(PRIVILEGED)";
995 }
996 } else if (coproc == 11 && (op3 & 0x9) != 8) {
997 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
998 }
Ian Rogers9af89402012-09-07 11:29:35 -0700999 }
Dave Allison70202782013-10-22 17:52:19 -07001000 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001001 }
1002 break;
Ian Rogers40627db2012-03-04 17:31:09 -08001003 case 2:
1004 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
1005 // Data-processing (modified immediate)
1006 // |111|11|10|0000|0|0000|1|111|1100|00000000|
1007 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
1008 // |---|--|--|----|-|----|-|---|----|--------|
1009 // |332|22|22|2222|2|1111|1|111|1100|00000000|
1010 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
1011 // |---|--|--|----|-|----|-|---|----|--------|
1012 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
1013 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -08001014 uint32_t i = (instr >> 26) & 1;
1015 uint32_t op3 = (instr >> 21) & 0xF;
1016 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001017 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -08001018 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001019 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001020 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001021 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
1022 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
1023 if (op3 == 0x2) {
1024 opcode << "mov";
1025 if (S == 1) {
1026 opcode << "s";
1027 }
1028 opcode << ".w";
1029 } else {
1030 opcode << "mvn";
1031 if (S == 1) {
1032 opcode << "s";
1033 }
1034 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001035 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001036 } else if (Rd.r == 0xF && S == 1 &&
1037 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
1038 if (op3 == 0x0) {
1039 opcode << "tst";
1040 } else if (op3 == 0x4) {
1041 opcode << "teq";
1042 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +00001043 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001044 } else {
1045 opcode << "cmp.w";
1046 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001047 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -08001048 } else {
1049 switch (op3) {
1050 case 0x0: opcode << "and"; break;
1051 case 0x1: opcode << "bic"; break;
1052 case 0x2: opcode << "orr"; break;
1053 case 0x3: opcode << "orn"; break;
1054 case 0x4: opcode << "eor"; break;
1055 case 0x8: opcode << "add"; break;
1056 case 0xA: opcode << "adc"; break;
1057 case 0xB: opcode << "sbc"; break;
1058 case 0xD: opcode << "sub"; break;
1059 case 0xE: opcode << "rsb"; break;
1060 default: opcode << "UNKNOWN DPMI-" << op3; break;
1061 }
1062 if (S == 1) {
1063 opcode << "s";
1064 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001065 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001066 }
Ian Rogers40627db2012-03-04 17:31:09 -08001067 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1068 // Data-processing (plain binary immediate)
1069 // |111|11|10|00000|0000|1|111110000000000|
1070 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1071 // |---|--|--|-----|----|-|---------------|
1072 // |332|22|22|22222|1111|1|111110000000000|
1073 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1074 // |---|--|--|-----|----|-|---------------|
1075 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1076 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001077 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001078 case 0x00: case 0x0A: {
1079 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001080 ArmRegister Rd(instr, 8);
1081 ArmRegister Rn(instr, 16);
1082 uint32_t i = (instr >> 26) & 1;
1083 uint32_t imm3 = (instr >> 12) & 0x7;
1084 uint32_t imm8 = instr & 0xFF;
1085 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1086 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001087 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001088 args << Rd << ", " << Rn << ", #" << imm12;
1089 } else {
1090 opcode << "adr";
1091 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001092 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001093 }
1094 break;
1095 }
Ian Rogers55019132013-02-08 01:05:23 -08001096 case 0x04: case 0x0C: {
1097 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001098 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001099 uint32_t i = (instr >> 26) & 1;
1100 uint32_t imm3 = (instr >> 12) & 0x7;
1101 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001102 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001103 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001104 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001105 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001106 break;
1107 }
jeffhaoeae26912013-01-28 16:29:54 -08001108 case 0x16: {
1109 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
1110 ArmRegister Rd(instr, 8);
1111 ArmRegister Rn(instr, 16);
1112 uint32_t msb = instr & 0x1F;
1113 uint32_t imm2 = (instr >> 6) & 0x3;
1114 uint32_t imm3 = (instr >> 12) & 0x7;
1115 uint32_t lsb = (imm3 << 2) | imm2;
1116 uint32_t width = msb - lsb + 1;
1117 if (Rn.r != 0xF) {
1118 opcode << "bfi";
1119 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1120 } else {
1121 opcode << "bfc";
1122 args << Rd << ", #" << lsb << ", #" << width;
1123 }
1124 break;
1125 }
Ian Rogers40627db2012-03-04 17:31:09 -08001126 default:
1127 break;
1128 }
1129 } else {
1130 // Branches and miscellaneous control
1131 // |111|11|1000000|0000|1|111|1100|00000000|
1132 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1133 // |---|--|-------|----|-|---|----|--------|
1134 // |332|22|2222222|1111|1|111|1100|00000000|
1135 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1136 // |---|--|-------|----|-|---|----|--------|
1137 // |111|10| op2 | |1|op3|op4 | |
1138
1139 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001140 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001141 switch (op3) {
1142 case 0:
1143 if ((op2 & 0x38) != 0x38) {
1144 // Conditional branch
1145 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1146 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1147 // |---|--|-|----|------|-|-|--|-|--|-----------|
1148 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1149 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1150 // |---|--|-|----|------|-|-|--|-|--|-----------|
1151 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1152 uint32_t S = (instr >> 26) & 1;
1153 uint32_t J2 = (instr >> 11) & 1;
1154 uint32_t J1 = (instr >> 13) & 1;
1155 uint32_t imm6 = (instr >> 16) & 0x3F;
1156 uint32_t imm11 = instr & 0x7FF;
1157 uint32_t cond = (instr >> 22) & 0xF;
1158 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1159 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001160 opcode << "b";
1161 DumpCond(opcode, cond);
1162 opcode << ".w";
1163 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001164 } else if (op2 == 0x3B) {
1165 // Miscellaneous control instructions
1166 uint32_t op5 = (instr >> 4) & 0xF;
1167 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001168 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1169 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1170 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001171 }
Ian Rogers40627db2012-03-04 17:31:09 -08001172 }
1173 break;
1174 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001175 if ((op2 & 0x38) == 0x38) {
1176 if (op2 == 0x7F) {
1177 opcode << "udf";
1178 }
1179 break;
1180 }
1181 // Else deliberate fall-through to B.
1182 case 1: case 3: {
1183 // B
1184 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1185 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1186 // |---|--|-|----|------|--|--|-|--|-----------|
1187 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1188 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1189 // |---|--|-|----|------|--|--|-|--|-----------|
1190 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1191 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1192 uint32_t S = (instr >> 26) & 1;
1193 uint32_t cond = (instr >> 22) & 0xF;
1194 uint32_t J2 = (instr >> 11) & 1;
1195 uint32_t form = (instr >> 12) & 1;
1196 uint32_t J1 = (instr >> 13) & 1;
1197 uint32_t imm10 = (instr >> 16) & 0x3FF;
1198 uint32_t imm6 = (instr >> 16) & 0x3F;
1199 uint32_t imm11 = instr & 0x7FF;
1200 opcode << "b";
1201 int32_t imm32;
1202 if (form == 0) {
1203 DumpCond(opcode, cond);
1204 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1205 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1206 } else {
1207 uint32_t I1 = ~(J1 ^ S);
1208 uint32_t I2 = ~(J2 ^ S);
1209 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1210 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1211 }
1212 opcode << ".w";
1213 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001214 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001215 }
Ian Rogers40627db2012-03-04 17:31:09 -08001216 case 4: case 6: case 5: case 7: {
1217 // BL, BLX (immediate)
1218 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1219 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1220 // |---|--|-|----------|--|--|-|--|-----------|
1221 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1222 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1223 // |---|--|-|----------|--|--|-|--|-----------|
Dave Allisonf9487c02014-04-08 23:08:12 +00001224 // |111|10|S| imm10 |11|J1|X|J2| imm11 |
Ian Rogers40627db2012-03-04 17:31:09 -08001225 uint32_t S = (instr >> 26) & 1;
1226 uint32_t J2 = (instr >> 11) & 1;
Dave Allisonf9487c02014-04-08 23:08:12 +00001227 uint32_t X = (instr >> 12) & 1;
Ian Rogers40627db2012-03-04 17:31:09 -08001228 uint32_t J1 = (instr >> 13) & 1;
1229 uint32_t imm10 = (instr >> 16) & 0x3FF;
1230 uint32_t imm11 = instr & 0x7FF;
Dave Allisonf9487c02014-04-08 23:08:12 +00001231 if (X == 0) {
1232 // This bit is unnamed in spec, but if zero, the instruction
1233 // is BLX.
Dave Allison081f73e2014-04-07 18:58:07 +00001234 opcode << "blx";
Dave Allisonf9487c02014-04-08 23:08:12 +00001235 } else {
1236 opcode << "bl";
Ian Rogers40627db2012-03-04 17:31:09 -08001237 }
1238 uint32_t I1 = ~(J1 ^ S);
1239 uint32_t I2 = ~(J2 ^ S);
1240 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1241 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001242 DumpBranchTarget(args, instr_ptr + 4, imm32);
Dave Allisonf9487c02014-04-08 23:08:12 +00001243 if (X == 1) {
1244 // For a BL instruction we look and see if it's an entrypoint
1245 // trampoline.
1246 const uint16_t* target = reinterpret_cast<const uint16_t*>(instr_ptr + 4 + imm32);
1247 if (target == nullptr) {
1248 // Defensive, will probably never happen.
1249 break;
1250 }
1251 const uint32_t targetinst = static_cast<uint32_t>(*target << 16 | *(target + 1));
1252
1253 // Is the target instruction an entrypoint trampoline:
1254 // ldr pc,[r9,#foo]
1255 if ((targetinst & 0xfffff000) == (0xf8d0f000 | (9 << 16))) {
1256 uint16_t offset = targetinst & 0xfff;
1257 args << " ; ";
1258 Thread::DumpThreadOffset<4>(args, offset);
1259 }
1260 }
Ian Rogers40627db2012-03-04 17:31:09 -08001261 break;
1262 }
1263 }
1264 }
1265 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001266 case 3:
1267 switch (op2) {
1268 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
1269 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
1270 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001271 // |111|11|100|000|0|0000|1111|110000|000000|
1272 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1273 // |---|--|---|---|-|----|----|------|------|
1274 // |332|22|222|222|2|1111|1111|110000|000000|
1275 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1276 // |---|--|---|---|-|----|----|------|------|
1277 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001278 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001279 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001280 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001281 case 0x0: case 0x4: {
1282 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1283 // STRB 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);
Ian Rogers087b2412012-03-21 01:30:32 -07001286 opcode << "strb";
1287 if ((instr & 0x800) != 0) {
1288 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001289 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001290 } else {
1291 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001292 ArmRegister Rm(instr, 0);
1293 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001294 if (imm2 != 0) {
1295 args << ", " << "lsl #" << imm2;
1296 }
1297 args << "]";
1298 }
1299 break;
1300 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001301 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001302 ArmRegister Rn(instr, 16);
1303 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001304 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001305 if ((instr & 0x800) != 0) {
1306 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1307 uint32_t P = (instr >> 10) & 1;
1308 uint32_t U = (instr >> 9) & 1;
1309 uint32_t W = (instr >> 8) & 1;
1310 uint32_t imm8 = instr & 0xFF;
1311 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1312 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1313 opcode << "push";
1314 args << Rt;
1315 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1316 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001317 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001318 if (P == 1 && U == 1 && W == 0) {
1319 opcode << "strt";
1320 } else {
1321 opcode << "str";
1322 }
1323 args << Rt << ", [" << Rn;
1324 if (P == 0 && W == 1) {
1325 args << "], #" << imm32;
1326 } else {
1327 args << ", #" << imm32 << "]";
1328 if (W == 1) {
1329 args << "!";
1330 }
Ian Rogers40627db2012-03-04 17:31:09 -08001331 }
1332 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001333 } else {
1334 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1335 ArmRegister Rn(instr, 16);
1336 ArmRegister Rt(instr, 12);
1337 ArmRegister Rm(instr, 0);
1338 uint32_t imm2 = (instr >> 4) & 3;
1339 opcode << "str.w";
1340 args << Rt << ", [" << Rn << ", " << Rm;
1341 if (imm2 != 0) {
1342 args << ", lsl #" << imm2;
1343 }
1344 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001345 }
1346 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001347 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001348 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001349 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001350 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001351 }
Ian Rogers40627db2012-03-04 17:31:09 -08001352 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001353 }
1354 }
1355
1356 break;
1357 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001358 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -08001359 // Load halfword
1360 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1361 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1362 // |---|--|--|---|--|-|----|----|------|------|
1363 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1364 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1365 // |---|--|--|---|--|-|----|----|------|------|
1366 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1367 // |111|11| op2 | | | imm12 |
1368 uint32_t op3 = (instr >> 23) & 3;
1369 ArmRegister Rn(instr, 16);
1370 ArmRegister Rt(instr, 12);
1371 if (Rt.r != 15) {
1372 if (op3 == 1) {
1373 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1374 uint32_t imm12 = instr & 0xFFF;
1375 opcode << "ldrh.w";
1376 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1377 if (Rn.r == 9) {
1378 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001379 Thread::DumpThreadOffset<4>(args, imm12);
jeffhaoeae26912013-01-28 16:29:54 -08001380 } else if (Rn.r == 15) {
1381 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1382 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1383 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1384 }
1385 } else if (op3 == 3) {
1386 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
1387 uint32_t imm12 = instr & 0xFFF;
1388 opcode << "ldrsh.w";
1389 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1390 if (Rn.r == 9) {
1391 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001392 Thread::DumpThreadOffset<4>(args, imm12);
jeffhaoeae26912013-01-28 16:29:54 -08001393 } else if (Rn.r == 15) {
1394 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1395 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1396 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1397 }
1398 }
1399 }
1400 break;
1401 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001402 case 0x29: { // 0101001
1403 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1404 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1405 // |---|--|-------|----|----|----|--|---|----|
1406 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1407 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1408 // |---|--|-------|----|----|----|--|---|----|
1409 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1410 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1411 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1412 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1413 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1414 if ((instr & 0xf0c0) == 0xf080) {
1415 uint32_t op3 = (instr >> 4) & 3;
1416 opcode << kThumbReverseOperations[op3];
1417 ArmRegister Rm(instr, 0);
1418 ArmRegister Rd(instr, 8);
1419 args << Rd << ", " << Rm;
1420 ArmRegister Rm2(instr, 16);
1421 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1422 args << " (UNPREDICTABLE)";
1423 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001424 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001425 break;
1426 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001427 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001428 // Load word
1429 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1430 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1431 // |---|--|--|---|--|-|----|----|------|------|
1432 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1433 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1434 // |---|--|--|---|--|-|----|----|------|------|
1435 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1436 // |111|11| op2 | | | imm12 |
1437 uint32_t op3 = (instr >> 23) & 3;
1438 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001439 ArmRegister Rn(instr, 16);
1440 ArmRegister Rt(instr, 12);
1441 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001442 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1443 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1444 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001445 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001446 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001447 if (Rn.r == 9) {
1448 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001449 Thread::DumpThreadOffset<4>(args, imm12);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001450 } else if (Rn.r == 15) {
1451 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1452 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1453 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001454 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001455 } else if (op4 == 0) {
1456 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1457 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001458 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001459 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001460 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001461 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001462 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001463 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001464 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001465 } else {
1466 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1467 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001468 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001469 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001470 }
1471 break;
1472 }
Dave Allison70202782013-10-22 17:52:19 -07001473 default: // more formats
1474 if ((op2 >> 4) == 2) { // 010xxxx
1475 // data processing (register)
Vladimir Markoc777e0d2014-04-03 17:59:02 +01001476 if ((instr & 0x0080f0f0) == 0x0000f000) {
1477 // LSL, LSR, ASR, ROR
1478 uint32_t shift_op = (instr >> 21) & 3;
1479 uint32_t S = (instr >> 20) & 1;
1480 ArmRegister Rd(instr, 8);
1481 ArmRegister Rn(instr, 16);
1482 ArmRegister Rm(instr, 0);
1483 opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
1484 args << Rd << ", " << Rn << ", " << Rm;
1485 }
Dave Allison70202782013-10-22 17:52:19 -07001486 } else if ((op2 >> 3) == 6) { // 0110xxx
1487 // Multiply, multiply accumulate, and absolute difference
1488 op1 = (instr >> 20) & 0x7;
1489 op2 = (instr >> 4) & 0x2;
1490 ArmRegister Ra(instr, 12);
1491 ArmRegister Rn(instr, 16);
1492 ArmRegister Rm(instr, 0);
1493 ArmRegister Rd(instr, 8);
1494 switch (op1) {
1495 case 0:
1496 if (op2 == 0) {
1497 if (Ra.r == 0xf) {
1498 opcode << "mul";
1499 args << Rd << ", " << Rn << ", " << Rm;
1500 } else {
1501 opcode << "mla";
1502 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1503 }
1504 } else {
1505 opcode << "mls";
1506 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1507 }
1508 break;
1509 case 1:
1510 case 2:
1511 case 3:
1512 case 4:
1513 case 5:
1514 case 6:
1515 break; // do these sometime
1516 }
1517 } else if ((op2 >> 3) == 7) { // 0111xxx
1518 // Long multiply, long multiply accumulate, and divide
1519 op1 = (instr >> 20) & 0x7;
1520 op2 = (instr >> 4) & 0xf;
1521 ArmRegister Rn(instr, 16);
1522 ArmRegister Rm(instr, 0);
1523 ArmRegister Rd(instr, 8);
1524 ArmRegister RdHi(instr, 8);
1525 ArmRegister RdLo(instr, 12);
1526 switch (op1) {
1527 case 0:
1528 opcode << "smull";
1529 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1530 break;
1531 case 1:
1532 opcode << "sdiv";
1533 args << Rd << ", " << Rn << ", " << Rm;
1534 break;
1535 case 2:
1536 opcode << "umull";
1537 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1538 break;
1539 case 3:
1540 opcode << "udiv";
1541 args << Rd << ", " << Rn << ", " << Rm;
1542 break;
1543 case 4:
1544 case 5:
1545 case 6:
1546 break; // TODO: when we generate these...
1547 }
1548 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001549 }
1550 default:
1551 break;
1552 }
Ian Rogers9af89402012-09-07 11:29:35 -07001553
1554 // Apply any IT-block conditions to the opcode if necessary.
1555 if (!it_conditions_.empty()) {
1556 opcode << it_conditions_.back();
1557 it_conditions_.pop_back();
1558 }
1559
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001560 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001561 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001562} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001563
1564size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1565 uint16_t instr = ReadU16(instr_ptr);
1566 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1567 if (is_32bit) {
1568 return DumpThumb32(os, instr_ptr);
1569 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001570 std::ostringstream opcode;
1571 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001572 uint16_t opcode1 = instr >> 10;
1573 if (opcode1 < 0x10) {
1574 // shift (immediate), add, subtract, move, and compare
1575 uint16_t opcode2 = instr >> 9;
1576 switch (opcode2) {
1577 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1578 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001579 // Logical shift left - 00 000xx iii mmm ddd
1580 // Logical shift right - 00 001xx iii mmm ddd
1581 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001582 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001583 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001584 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001585 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001586 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001587 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001588 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001589 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001590 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001591 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001592 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001593 break;
1594 }
1595 case 0xC: case 0xD: case 0xE: case 0xF: {
1596 // Add register - 00 01100 mmm nnn ddd
1597 // Sub register - 00 01101 mmm nnn ddd
1598 // Add 3-bit immediate - 00 01110 iii nnn ddd
1599 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1600 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001601 ThumbRegister Rn(instr, 3);
1602 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001603 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001604 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001605 } else {
1606 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001607 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001608 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001609 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001610 }
1611 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001612 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001613 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001614 ArmRegister Rm(imm3_or_Rm);
1615 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001616 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001617 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001618 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001619 break;
1620 }
1621 case 0x10: case 0x11: case 0x12: case 0x13:
1622 case 0x14: case 0x15: case 0x16: case 0x17:
1623 case 0x18: case 0x19: case 0x1A: case 0x1B:
1624 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1625 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1626 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1627 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1628 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001629 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001630 uint16_t imm8 = instr & 0xFF;
1631 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001632 case 4: opcode << "movs"; break;
1633 case 5: opcode << "cmp"; break;
1634 case 6: opcode << "adds"; break;
1635 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001636 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001637 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001638 break;
1639 }
1640 default:
1641 break;
1642 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001643 } else if (opcode1 == 0x10) {
1644 // Data-processing
1645 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001646 ThumbRegister rm(instr, 3);
1647 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001648 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001649 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001650 } else if (opcode1 == 0x11) {
1651 // Special data instructions and branch and exchange
1652 uint16_t opcode2 = (instr >> 6) & 0x0F;
1653 switch (opcode2) {
1654 case 0x0: case 0x1: case 0x2: case 0x3: {
1655 // Add low registers - 010001 0000 xxxxxx
1656 // Add high registers - 010001 0001/001x xxxxxx
1657 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001658 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001659 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001660 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001661 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001662 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001663 break;
1664 }
1665 case 0x8: case 0x9: case 0xA: case 0xB: {
1666 // Move low registers - 010001 1000 xxxxxx
1667 // Move high registers - 010001 1001/101x xxxxxx
1668 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001669 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001670 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001671 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001672 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001673 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001674 break;
1675 }
1676 case 0x5: case 0x6: case 0x7: {
1677 // Compare high registers - 010001 0101/011x xxxxxx
1678 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001679 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001680 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001681 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001682 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001683 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001684 break;
1685 }
1686 case 0xC: case 0xD: case 0xE: case 0xF: {
1687 // Branch and exchange - 010001 110x xxxxxx
1688 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001689 ArmRegister rm(instr, 3);
1690 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1691 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001692 break;
1693 }
1694 default:
1695 break;
1696 }
jeffhaoeae26912013-01-28 16:29:54 -08001697 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1698 ThumbRegister Rt(instr, 8);
1699 uint16_t imm8 = instr & 0xFF;
1700 opcode << "ldr";
1701 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001702 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1703 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1704 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1705 // Load/store single data item
1706 uint16_t opA = (instr >> 12) & 0xF;
1707 if (opA == 0x5) {
1708 uint16_t opB = (instr >> 9) & 0x7;
1709 ThumbRegister Rm(instr, 6);
1710 ThumbRegister Rn(instr, 3);
1711 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001712 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001713 case 0: opcode << "str"; break;
1714 case 1: opcode << "strh"; break;
1715 case 2: opcode << "strb"; break;
1716 case 3: opcode << "ldrsb"; break;
1717 case 4: opcode << "ldr"; break;
1718 case 5: opcode << "ldrh"; break;
1719 case 6: opcode << "ldrb"; break;
1720 case 7: opcode << "ldrsh"; break;
1721 }
1722 args << Rt << ", [" << Rn << ", " << Rm << "]";
1723 } else if (opA == 9) {
1724 uint16_t opB = (instr >> 11) & 1;
1725 ThumbRegister Rt(instr, 8);
1726 uint16_t imm8 = instr & 0xFF;
1727 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001728 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001729 } else {
1730 uint16_t imm5 = (instr >> 6) & 0x1F;
1731 uint16_t opB = (instr >> 11) & 1;
1732 ThumbRegister Rn(instr, 3);
1733 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001734 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001735 case 6:
1736 imm5 <<= 2;
1737 opcode << (opB == 0 ? "str" : "ldr");
1738 break;
1739 case 7:
1740 imm5 <<= 0;
1741 opcode << (opB == 0 ? "strb" : "ldrb");
1742 break;
1743 case 8:
1744 imm5 <<= 1;
1745 opcode << (opB == 0 ? "strh" : "ldrh");
1746 break;
1747 }
1748 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1749 }
jeffhaoeae26912013-01-28 16:29:54 -08001750 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001751 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001752 uint32_t cond = (instr >> 8) & 0xF;
1753 opcode << "b";
1754 DumpCond(opcode, cond);
1755 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001756 } else if ((instr & 0xF800) == 0xA800) {
1757 // Generate SP-relative address
1758 ThumbRegister rd(instr, 8);
1759 int imm8 = instr & 0xFF;
1760 opcode << "add";
1761 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001762 } else if ((instr & 0xF000) == 0xB000) {
1763 // Miscellaneous 16-bit instructions
1764 uint16_t opcode2 = (instr >> 5) & 0x7F;
1765 switch (opcode2) {
1766 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1767 // Add immediate to SP - 1011 00000 ii iiiii
1768 // Subtract immediate from SP - 1011 00001 ii iiiii
1769 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001770 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001771 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001772 break;
1773 }
Ian Rogers087b2412012-03-21 01:30:32 -07001774 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001775 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001776 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1777 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001778 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001779 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1780 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1781 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001782 // CBNZ, CBZ
1783 uint16_t op = (instr >> 11) & 1;
1784 uint16_t i = (instr >> 9) & 1;
1785 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001786 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001787 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001788 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001789 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001790 DumpBranchTarget(args, instr_ptr + 4, imm32);
1791 break;
1792 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001793 case 0x50: case 0x51: // 101000x
1794 case 0x52: case 0x53: // 101001x
1795 case 0x56: case 0x57: { // 101011x
1796 uint16_t op = (instr >> 6) & 3;
1797 opcode << kThumbReverseOperations[op];
1798 ThumbRegister Rm(instr, 3);
1799 ThumbRegister Rd(instr, 0);
1800 args << Rd << ", " << Rm;
1801 break;
1802 }
Ian Rogers40627db2012-03-04 17:31:09 -08001803 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1804 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1805 // If-Then, and hints
1806 uint16_t opA = (instr >> 4) & 0xF;
1807 uint16_t opB = instr & 0xF;
1808 if (opB == 0) {
1809 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001810 case 0: opcode << "nop"; break;
1811 case 1: opcode << "yield"; break;
1812 case 2: opcode << "wfe"; break;
1813 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001814 default: break;
1815 }
1816 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001817 uint32_t first_cond = opA;
1818 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001819 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001820
1821 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1822 // and store up the actual condition codes we'll want to add to the next few opcodes.
1823 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001824 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001825 for (size_t i = 0; i < count; ++i) {
1826 bool positive_cond = ((first_cond & 1) != 0);
1827 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1828 if (positive_mask == positive_cond) {
1829 opcode << 't';
1830 it_conditions_[i] = kConditionCodeNames[first_cond];
1831 } else {
1832 opcode << 'e';
1833 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1834 }
1835 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001836 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001837
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001838 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1839 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001840 }
1841 break;
1842 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001843 default:
1844 break;
1845 }
1846 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1847 ((instr & 0xE000) == 0x8000)) {
1848 // Load/store single data item
1849 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001850 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001851 switch (opA) {
1852 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001853 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1854 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001855 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001856 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001857 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001858 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1859 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001860 break;
1861 }
1862 case 0x9: {
1863 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1864 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1865 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001866 ThumbRegister Rt(instr, 8);
1867 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1868 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001869 break;
1870 }
1871 default:
1872 break;
1873 }
Ian Rogers40627db2012-03-04 17:31:09 -08001874 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1875 uint16_t imm11 = instr & 0x7FFF;
1876 int32_t imm32 = imm11 << 1;
1877 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001878 opcode << "b";
1879 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001880 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001881
1882 // Apply any IT-block conditions to the opcode if necessary.
1883 if (!it_conditions_.empty()) {
1884 opcode << it_conditions_.back();
1885 it_conditions_.pop_back();
1886 }
1887
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001888 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001889 }
1890 return 2;
1891}
1892
1893} // namespace arm
1894} // namespace art