blob: 71f70c4e5b1c18ceb2a4242d9ec53729702eab74 [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 Rogers3a5c1ce2012-02-29 10:06:46 -080019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070023#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080025namespace art {
26namespace arm {
27
28DisassemblerArm::DisassemblerArm() {
29}
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) {
86 case 0b1111: os << "sy"; break;
87 case 0b1110: os << "st"; break;
88 case 0b1011: os << "ish"; break;
89 case 0b1010: os << "ishst"; break;
90 case 0b0111: os << "nsh"; break;
91 case 0b0110: os << "nshst"; break;
92 case 0b0011: os << "osh"; break;
93 case 0b0010: os << "oshst"; break;
94 }
95}
96
Ian Rogers40627db2012-03-04 17:31:09 -080097void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Elliott Hughes1ca98492012-04-12 17:21:02 -070098 os << StringPrintf("%+d (%p)", imm32, 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 Markoa8b4caf2013-10-24 15:08:57 +0100119static const char* kThumbReverseOperations[] = {
120 "rev", "rev16", "rbit", "revsh"
121};
122
Elliott Hughes77405792012-03-15 15:22:12 -0700123struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -0700124 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700125 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -0700126 uint32_t r;
127};
128std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
129 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700130 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700131 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700132 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700133 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700134 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700135 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700136 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700137 }
138 return os;
139}
140
Elliott Hughes630e77d2012-03-22 19:20:56 -0700141struct ThumbRegister : ArmRegister {
142 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700143};
144
145struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700146 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700147 uint32_t shift;
148 ArmRegister rm;
149};
150std::ostream& operator<<(std::ostream& os, const Rm& r) {
151 os << r.rm;
152 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700153 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700154 }
155 return os;
156}
157
Elliott Hughes1ca98492012-04-12 17:21:02 -0700158struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700159 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700160 uint32_t rotate = ((instruction >> 8) & 0xf);
161 uint32_t imm = (instruction & 0xff);
162 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
163 }
164 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700165};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700166std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700167 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700168 return os;
169}
170
171struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700172 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700173 uint32_t register_list;
174};
175std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
176 if (rhs.register_list == 0) {
177 os << "<no register list?>";
178 return os;
179 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700180 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700181 bool first = true;
182 for (size_t i = 0; i < 16; i++) {
183 if ((rhs.register_list & (1 << i)) != 0) {
184 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700185 first = false;
186 } else {
187 os << ", ";
188 }
189 os << ArmRegister(i);
190 }
191 }
192 os << "}";
193 return os;
194}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800195
Vladimir Markodd577a32013-11-07 19:25:24 +0000196struct FpRegister {
197 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
198 size = (instr >> 8) & 1;
199 uint32_t Vn = (instr >> at_bit) & 0xF;
200 uint32_t N = (instr >> extra_at_bit) & 1;
201 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
202 }
203 FpRegister(const FpRegister& other, uint32_t offset)
204 : size(other.size), r(other.r + offset) {}
205
206 uint32_t size; // 0 = f32, 1 = f64
207 uint32_t r;
208};
209std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
210 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
211}
212
213struct FpRegisterRange {
214 explicit FpRegisterRange(uint32_t instr)
215 : first(instr, 12, 22), imm8(instr & 0xFF) {}
216 FpRegister first;
217 uint32_t imm8;
218};
219std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
220 os << "{" << rhs.first;
221 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
222 if (count > 1) {
223 os << "-" << FpRegister(rhs.first, count - 1);
224 }
225 if (rhs.imm8 == 0) {
226 os << " (EMPTY)";
227 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
228 os << rhs.first << " (HALF)";
229 }
230 os << "}";
231 return os;
232}
233
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800234void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700235 uint32_t instruction = ReadU32(instr_ptr);
236 uint32_t cond = (instruction >> 28) & 0xf;
237 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700238 std::string opcode;
239 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700240 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700241 switch (op1) {
242 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700243 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700244 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700245 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700246 opcode = "bkpt";
247 uint32_t imm12 = (instruction >> 8) & 0xfff;
248 uint32_t imm4 = (instruction & 0xf);
249 args << '#' << ((imm12 << 4) | imm4);
250 break;
251 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700252 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700253 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700254 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700255 break;
256 }
257 bool i = (instruction & (1 << 25)) != 0;
258 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700259 uint32_t op = (instruction >> 21) & 0xf;
260 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700261 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700262 if (implicit_s) {
263 // Rd is unused (and not shown), and we don't show the 's' suffix either.
264 } else {
265 if (s) {
266 suffixes += 's';
267 }
268 args << ArmRegister(instruction, 12) << ", ";
269 }
Elliott Hughes77405792012-03-15 15:22:12 -0700270 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700271 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700272 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700273 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700274 }
275 }
276 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700277 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700278 {
279 bool p = (instruction & (1 << 24)) != 0;
280 bool b = (instruction & (1 << 22)) != 0;
281 bool w = (instruction & (1 << 21)) != 0;
282 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700283 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700284 args << ArmRegister(instruction, 12) << ", ";
285 ArmRegister rn(instruction, 16);
286 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700287 UNIMPLEMENTED(FATAL) << "literals";
288 } else {
289 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700290 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700291 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700292 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700293 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700294 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700295 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700296 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700297 } else {
298 LOG(FATAL) << p << " " << w;
299 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700300 if (rn.r == 9) {
301 args << " ; ";
Elliott Hughes1ca98492012-04-12 17:21:02 -0700302 Thread::DumpThreadOffset(args, offset, 4);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700303 }
Elliott Hughes77405792012-03-15 15:22:12 -0700304 }
305 }
306 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700307 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700308 {
309 bool p = (instruction & (1 << 24)) != 0;
310 bool u = (instruction & (1 << 23)) != 0;
311 bool w = (instruction & (1 << 21)) != 0;
312 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700313 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700314 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700315 }
316 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700317 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700318 {
319 bool bl = (instruction & (1 << 24)) != 0;
320 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700321 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700322 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700323 DumpBranchTarget(args, instr_ptr + 8, imm32);
324 }
325 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700326 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700327 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700328 break;
329 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700330 opcode += kConditionCodeNames[cond];
331 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700332 // TODO: a more complete ARM disassembler could generate wider opcodes.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800333 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800334}
335
Ian Rogersa9650dd2013-10-04 08:23:32 -0700336int32_t ThumbExpand(int32_t imm12) {
337 if ((imm12 & 0xC00) == 0) {
338 switch ((imm12 >> 8) & 3) {
339 case 0:
340 return imm12 & 0xFF;
341 case 1:
342 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
343 case 2:
344 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
345 default: // 3
346 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
347 (imm12 & 0xFF);
348 }
349 } else {
350 uint32_t val = 0x80 | (imm12 & 0x7F);
351 int32_t rotate = (imm12 >> 7) & 0x1F;
352 return (val >> rotate) | (val << (32 - rotate));
353 }
354}
355
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800356size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
357 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
358 // |111|1 1|1000000|0000|1111110000000000|
359 // |5 3|2 1|0987654|3 0|5 0 5 0|
360 // |---|---|-------|----|----------------|
361 // |332|2 2|2222222|1111|1111110000000000|
362 // |1 9|8 7|6543210|9 6|5 0 5 0|
363 // |---|---|-------|----|----------------|
364 // |111|op1| op2 | | |
365 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700366 if (op1 == 0) {
367 return DumpThumb16(os, instr_ptr);
368 }
369
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800370 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700371 std::ostringstream opcode;
372 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800373 switch (op1) {
374 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800375 break;
376 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700377 if ((op2 & 0x64) == 0) { // 00x x0xx
378 // |111|11|10|00|0|00|0000|1111110000000000|
379 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
380 // |---|--|--|--|-|--|----|----------------|
381 // |332|22|22|22|2|22|1111|1111110000000000|
382 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
383 // |---|--|--|--|-|--|----|----------------|
384 // |111|01|00|op|0|WL| Rn | |
385 // |111|01| op2 | | |
386 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
387 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
388 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
389 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
390 uint32_t op = (instr >> 23) & 3;
391 uint32_t W = (instr >> 21) & 1;
392 uint32_t L = (instr >> 20) & 1;
393 ArmRegister Rn(instr, 16);
394 if (op == 1 || op == 2) {
395 if (op == 1) {
396 if (L == 0) {
397 opcode << "stm";
398 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800399 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700400 if (Rn.r != 13) {
401 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700402 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700403 } else {
404 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800405 }
406 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700407 } else {
408 if (L == 0) {
409 if (Rn.r != 13) {
410 opcode << "stmdb";
411 args << Rn << (W == 0 ? "" : "!") << ", ";
412 } else {
413 opcode << "push";
414 }
415 } else {
416 opcode << "ldmdb";
417 args << Rn << (W == 0 ? "" : "!") << ", ";
418 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800419 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700420 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800421 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700422 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700423 uint32_t op3 = (instr >> 23) & 3;
424 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700425 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700426 ArmRegister Rn(instr, 16);
427 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700428 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700429 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700430 if ((op3 & 2) == 2) { // 1x
431 int W = (instr >> 21) & 1;
432 int U = (instr >> 23) & 1;
433 int P = (instr >> 24) & 1;
434
435 if ((op4 & 1) == 1) {
436 opcode << "ldrd";
437 } else {
438 opcode << "strd";
439 }
440 args << Rt << "," << Rd << ", [" << Rn;
441 const char *sign = U ? "+" : "-";
442 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000443 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700444 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000445 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700446 if (W == 1) {
447 args << "!";
448 }
449 }
450 } else { // 0x
451 switch (op4) {
452 case 0:
453 if (op3 == 0) { // op3 is 00, op4 is 00
454 opcode << "strex";
455 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000456 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
457 Rd.r == Rn.r || Rd.r == Rt.r) {
458 args << " (UNPREDICTABLE)";
459 }
Dave Allison70202782013-10-22 17:52:19 -0700460 } else { // op3 is 01, op4 is 00
461 // this is one of strexb, strexh or strexd
462 int op5 = (instr >> 4) & 0xf;
463 switch (op5) {
464 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700465 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000466 opcode << ((op5 == 4) ? "strexb" : "strexh");
467 Rd = ArmRegister(instr, 0);
468 args << Rd << ", " << Rt << ", [" << Rn << "]";
469 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
470 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
471 args << " (UNPREDICTABLE)";
472 }
Dave Allison70202782013-10-22 17:52:19 -0700473 break;
474 case 7:
475 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000476 ArmRegister Rt2 = Rd;
477 Rd = ArmRegister(instr, 0);
478 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
479 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
480 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
481 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
482 args << " (UNPREDICTABLE)";
483 }
Dave Allison70202782013-10-22 17:52:19 -0700484 break;
485 }
486 }
487 break;
488 case 1:
489 if (op3 == 0) { // op3 is 00, op4 is 01
490 opcode << "ldrex";
491 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000492 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
493 args << " (UNPREDICTABLE)";
494 }
Dave Allison70202782013-10-22 17:52:19 -0700495 } else { // op3 is 01, op4 is 01
496 // this is one of strexb, strexh or strexd
497 int op5 = (instr >> 4) & 0xf;
498 switch (op5) {
499 case 0:
500 opcode << "tbb";
501 break;
502 case 1:
503 opcode << "tbh";
504 break;
505 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700506 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000507 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
508 args << Rt << ", [" << Rn << "]";
509 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
510 args << " (UNPREDICTABLE)";
511 }
Dave Allison70202782013-10-22 17:52:19 -0700512 break;
513 case 7:
514 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000515 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
516 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
517 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
518 args << " (UNPREDICTABLE)";
519 }
Dave Allison70202782013-10-22 17:52:19 -0700520 break;
521 }
522 }
523 break;
524 case 2: // op3 is 0x, op4 is 10
525 case 3: // op3 is 0x, op4 is 11
526 if (op4 == 2) {
527 opcode << "strd";
528 } else {
529 opcode << "ldrd";
530 }
531 int W = (instr >> 21) & 1;
532 int U = (instr >> 23) & 1;
533 int P = (instr >> 24) & 1;
534
535 args << Rt << "," << Rd << ", [" << Rn;
536 const char *sign = U ? "+" : "-";
537 if (P == 0 && W == 1) {
538 args << "], #" << sign << imm8;
539 } else {
540 args << ", #" << sign << imm8 << "]";
541 if (W == 1) {
542 args << "!";
543 }
544 }
545 break;
546 }
547 }
548
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700549 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
550 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100551 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
552 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
553 // |---|----|----|-|----|----|----|--|--|----|
554 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
555 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
556 // |---|----|----|-|----|----|----|--|--|----|
557 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700558 uint32_t op3 = (instr >> 21) & 0xF;
559 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100560 uint32_t imm3 = ((instr >> 12) & 0x7);
561 uint32_t imm2 = ((instr >> 6) & 0x3);
562 uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F;
563 uint32_t shift_type = ((instr >> 4) & 0x2);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700564 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100565 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700566 ArmRegister Rm(instr, 0);
567 switch (op3) {
568 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100569 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700570 opcode << "and";
571 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700572 if (S != 1U) {
573 opcode << "UNKNOWN TST-" << S;
574 break;
575 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700576 opcode << "tst";
577 S = 0; // don't print 's'
578 }
579 break;
580 case 0x1: opcode << "bic"; break;
581 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100582 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700583 opcode << "orr";
584 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100585 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700586 opcode << "mov";
587 }
588 break;
589 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100590 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700591 opcode << "orn";
592 } else {
593 opcode << "mvn";
594 }
595 break;
596 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100597 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700598 opcode << "eor";
599 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700600 if (S != 1U) {
601 opcode << "UNKNOWN TEQ-" << S;
602 break;
603 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700604 opcode << "teq";
605 S = 0; // don't print 's'
606 }
607 break;
608 case 0x6: opcode << "pkh"; break;
609 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100610 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700611 opcode << "add";
612 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700613 if (S != 1U) {
614 opcode << "UNKNOWN CMN-" << S;
615 break;
616 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700617 opcode << "cmn";
618 S = 0; // don't print 's'
619 }
620 break;
621 case 0xA: opcode << "adc"; break;
622 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100623 case 0xD:
624 if (Rd.r != 0xF) {
625 opcode << "sub";
626 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700627 if (S != 1U) {
628 opcode << "UNKNOWN CMP-" << S;
629 break;
630 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100631 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100632 S = 0; // don't print 's'
633 }
634 break;
635 case 0xE: opcode << "rsb"; break;
636 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700637 }
Ian Rogers087b2412012-03-21 01:30:32 -0700638
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700639 if (S == 1) {
640 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700641 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700642 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100643
644 if (Rd.r != 0xF) {
645 args << Rd << ", ";
646 }
647 if (Rn.r != 0xF) {
648 args << Rn << ", ";
649 }
650 args << Rm;
651
652 // Shift operand.
653 bool noShift = (imm5 == 0 && shift_type != 0x3);
654 if (!noShift) {
655 args << ", ";
656 switch (shift_type) {
657 case 0x0: args << "lsl"; break;
658 case 0x1: args << "lsr"; break;
659 case 0x2: args << "asr"; break;
660 case 0x3:
661 if (imm5 == 0) {
662 args << "rrx";
663 } else {
664 args << "ror";
665 }
666 break;
667 }
668 if (shift_type != 0x3 /* rrx */) {
669 args << StringPrintf(" #%d", imm5);
670 }
671 }
672
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700673 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
674 // Co-processor instructions
675 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
676 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
677 // |---|-|--|------|----|----|----|---|---|----|
678 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
679 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
680 // |---|-|--|------|----|----|----|---|---|----|
681 // |111| |11| op3 | Rn | |copr| |op4| |
682 uint32_t op3 = (instr >> 20) & 0x3F;
683 uint32_t coproc = (instr >> 8) & 0xF;
684 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700685
686 if (coproc == 10 || coproc == 11) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000687 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
688 // Extension register load/store instructions
689 // |1111|110|00000|0000|1111|110|0|00000000|
690 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
691 // |----|---|-----|----|----|---|-|--------|
692 // |3322|222|22222|1111|1111|110|0|00000000|
693 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
694 // |----|---|-----|----|----|---|-|--------|
695 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700696 uint32_t P = (instr >> 24) & 1;
697 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700698 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000699 if (P == U && W == 1) {
700 opcode << "UNDEFINED";
701 } else {
702 uint32_t L = (instr >> 20) & 1;
703 uint32_t S = (instr >> 8) & 1;
704 ArmRegister Rn(instr, 16);
705 if (P == 1 && W == 0) { // VLDR
706 FpRegister d(instr, 12, 22);
707 uint32_t imm8 = instr & 0xFF;
708 opcode << (L == 1 ? "vldr" : "vstr");
709 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
710 << (imm8 << 2) << "]";
711 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
712 opcode << (L == 1 ? "vpop" : "vpush");
713 args << FpRegisterRange(instr);
714 } else { // VLDM
715 opcode << (L == 1 ? "vldm" : "vstm");
716 args << Rn << ((W == 1) ? "!" : "") << ", "
717 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700718 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000719 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700720 }
Dave Allison70202782013-10-22 17:52:19 -0700721 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000722 if ((instr & 0xD0) == 0x10) {
723 // 64bit transfers between ARM core and extension registers.
724 uint32_t L = (instr >> 20) & 1;
725 uint32_t S = (instr >> 8) & 1;
726 ArmRegister Rt2(instr, 16);
727 ArmRegister Rt(instr, 12);
728 FpRegister m(instr, 0, 5);
729 opcode << "vmov" << (S ? ".f64" : ".f32");
730 if (L == 1) {
731 args << Rt << ", " << Rt2 << ", ";
732 }
733 if (S) {
734 args << m;
735 } else {
736 args << m << ", " << FpRegister(m, 1);
737 }
738 if (L == 0) {
739 args << ", " << Rt << ", " << Rt2;
740 }
741 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
742 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
743 args << " (UNPREDICTABLE)";
744 }
745 }
Dave Allison70202782013-10-22 17:52:19 -0700746 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
747 // fp data processing
748 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000749 if (coproc == 10 && (op3 & 0xE) == 0) {
750 // VMOV (between ARM core register and single-precision register)
751 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
752 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
753 // |----|----|---|- |----|----|----|-|--|-|----|
754 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
755 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
756 // |----|----|---|- |----|----|----|-|--|-|----|
757 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
758 uint32_t op = op3 & 1;
759 ArmRegister Rt(instr, 12);
760 FpRegister n(instr, 16, 7);
761 opcode << "vmov.f32";
762 if (op) {
763 args << Rt << ", " << n;
764 } else {
765 args << n << ", " << Rt;
766 }
767 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
768 args << " (UNPREDICTABLE)";
769 }
770 } else if (coproc == 10 && op3 == 0x2F) {
771 // VMRS
772 // |1111|11000000|0000|1111|1100|000|0|0000|
773 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
774 // |----|--------|----|----|----|---|-|----|
775 // |3322|22222222|1111|1111|1100|000|0|0000|
776 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
777 // |----|--------|----|----|----|---|-|----|
778 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
779 uint32_t spec_reg = (instr >> 16) & 0xF;
780 ArmRegister Rt(instr, 12);
781 opcode << "vmrs";
782 if (spec_reg == 1) {
783 if (Rt.r == 15) {
784 args << "APSR_nzcv, FPSCR";
785 } else if (Rt.r == 13) {
786 args << Rt << ", FPSCR (UNPREDICTABLE)";
787 } else {
788 args << Rt << ", FPSCR";
789 }
790 } else {
791 args << "(PRIVILEGED)";
792 }
793 } else if (coproc == 11 && (op3 & 0x9) != 8) {
794 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
795 }
Ian Rogers9af89402012-09-07 11:29:35 -0700796 }
Dave Allison70202782013-10-22 17:52:19 -0700797 }
798
799 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700800 if ((coproc & 0xE) == 0xA) {
801 // VFP data-processing instructions
802 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
803 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
804 // |---|-|----|----|----|----|---|-|----|-|-|----|
805 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
806 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
807 // |---|-|----|----|----|----|---|-|----|-|-|----|
808 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
809 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
810 uint32_t opc1 = (instr >> 20) & 0xF;
811 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700812 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700813 if ((opc1 & 0xB) == 0xB) { // 1x11
814 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700815 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000816 FpRegister d(instr, 12, 22);
817 FpRegister m(instr, 0, 5);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700818 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700819 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700820 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000821 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (sz == 1 ? ".f64" : ".f32");
822 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700823 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700824 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700825 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000826 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (sz == 1 ? ".f64" : ".f32");
827 args << d << ", " << m;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700828 break;
829 }
830 }
831 }
832 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700833 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
834 // Advanced SIMD
835 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
836 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
837 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
Ian Rogers0183dd72012-09-17 23:06:51 -0700838 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000839 FpRegister d(instr, 12, 22);
840 FpRegister m(instr, 0, 5);
841 opcode << "vsqrt" << (sz == 1 ? ".f64" : ".f32");
842 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700843 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700844 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800845 }
846 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800847 case 2:
848 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
849 // Data-processing (modified immediate)
850 // |111|11|10|0000|0|0000|1|111|1100|00000000|
851 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
852 // |---|--|--|----|-|----|-|---|----|--------|
853 // |332|22|22|2222|2|1111|1|111|1100|00000000|
854 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
855 // |---|--|--|----|-|----|-|---|----|--------|
856 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
857 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800858 uint32_t i = (instr >> 26) & 1;
859 uint32_t op3 = (instr >> 21) & 0xF;
860 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700861 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800862 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700863 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800864 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800865 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
866 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
867 if (op3 == 0x2) {
868 opcode << "mov";
869 if (S == 1) {
870 opcode << "s";
871 }
872 opcode << ".w";
873 } else {
874 opcode << "mvn";
875 if (S == 1) {
876 opcode << "s";
877 }
878 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700879 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800880 } else if (Rd.r == 0xF && S == 1 &&
881 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
882 if (op3 == 0x0) {
883 opcode << "tst";
884 } else if (op3 == 0x4) {
885 opcode << "teq";
886 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +0000887 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800888 } else {
889 opcode << "cmp.w";
890 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700891 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800892 } else {
893 switch (op3) {
894 case 0x0: opcode << "and"; break;
895 case 0x1: opcode << "bic"; break;
896 case 0x2: opcode << "orr"; break;
897 case 0x3: opcode << "orn"; break;
898 case 0x4: opcode << "eor"; break;
899 case 0x8: opcode << "add"; break;
900 case 0xA: opcode << "adc"; break;
901 case 0xB: opcode << "sbc"; break;
902 case 0xD: opcode << "sub"; break;
903 case 0xE: opcode << "rsb"; break;
904 default: opcode << "UNKNOWN DPMI-" << op3; break;
905 }
906 if (S == 1) {
907 opcode << "s";
908 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700909 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800910 }
Ian Rogers40627db2012-03-04 17:31:09 -0800911 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
912 // Data-processing (plain binary immediate)
913 // |111|11|10|00000|0000|1|111110000000000|
914 // |5 3|21|09|87654|3 0|5|4 0 5 0|
915 // |---|--|--|-----|----|-|---------------|
916 // |332|22|22|22222|1111|1|111110000000000|
917 // |1 9|87|65|43210|9 6|5|4 0 5 0|
918 // |---|--|--|-----|----|-|---------------|
919 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
920 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800921 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -0800922 case 0x00: case 0x0A: {
923 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -0700924 ArmRegister Rd(instr, 8);
925 ArmRegister Rn(instr, 16);
926 uint32_t i = (instr >> 26) & 1;
927 uint32_t imm3 = (instr >> 12) & 0x7;
928 uint32_t imm8 = instr & 0xFF;
929 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
930 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -0800931 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -0700932 args << Rd << ", " << Rn << ", #" << imm12;
933 } else {
934 opcode << "adr";
935 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -0800936 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -0700937 }
938 break;
939 }
Ian Rogers55019132013-02-08 01:05:23 -0800940 case 0x04: case 0x0C: {
941 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700942 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800943 uint32_t i = (instr >> 26) & 1;
944 uint32_t imm3 = (instr >> 12) & 0x7;
945 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700946 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800947 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -0800948 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700949 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800950 break;
951 }
jeffhaoeae26912013-01-28 16:29:54 -0800952 case 0x16: {
953 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
954 ArmRegister Rd(instr, 8);
955 ArmRegister Rn(instr, 16);
956 uint32_t msb = instr & 0x1F;
957 uint32_t imm2 = (instr >> 6) & 0x3;
958 uint32_t imm3 = (instr >> 12) & 0x7;
959 uint32_t lsb = (imm3 << 2) | imm2;
960 uint32_t width = msb - lsb + 1;
961 if (Rn.r != 0xF) {
962 opcode << "bfi";
963 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
964 } else {
965 opcode << "bfc";
966 args << Rd << ", #" << lsb << ", #" << width;
967 }
968 break;
969 }
Ian Rogers40627db2012-03-04 17:31:09 -0800970 default:
971 break;
972 }
973 } else {
974 // Branches and miscellaneous control
975 // |111|11|1000000|0000|1|111|1100|00000000|
976 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
977 // |---|--|-------|----|-|---|----|--------|
978 // |332|22|2222222|1111|1|111|1100|00000000|
979 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
980 // |---|--|-------|----|-|---|----|--------|
981 // |111|10| op2 | |1|op3|op4 | |
982
983 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700984 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800985 switch (op3) {
986 case 0:
987 if ((op2 & 0x38) != 0x38) {
988 // Conditional branch
989 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
990 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
991 // |---|--|-|----|------|-|-|--|-|--|-----------|
992 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
993 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
994 // |---|--|-|----|------|-|-|--|-|--|-----------|
995 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
996 uint32_t S = (instr >> 26) & 1;
997 uint32_t J2 = (instr >> 11) & 1;
998 uint32_t J1 = (instr >> 13) & 1;
999 uint32_t imm6 = (instr >> 16) & 0x3F;
1000 uint32_t imm11 = instr & 0x7FF;
1001 uint32_t cond = (instr >> 22) & 0xF;
1002 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1003 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001004 opcode << "b";
1005 DumpCond(opcode, cond);
1006 opcode << ".w";
1007 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001008 } else if (op2 == 0x3B) {
1009 // Miscellaneous control instructions
1010 uint32_t op5 = (instr >> 4) & 0xF;
1011 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001012 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1013 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1014 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001015 }
Ian Rogers40627db2012-03-04 17:31:09 -08001016 }
1017 break;
1018 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001019 if ((op2 & 0x38) == 0x38) {
1020 if (op2 == 0x7F) {
1021 opcode << "udf";
1022 }
1023 break;
1024 }
1025 // Else deliberate fall-through to B.
1026 case 1: case 3: {
1027 // B
1028 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1029 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1030 // |---|--|-|----|------|--|--|-|--|-----------|
1031 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1032 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1033 // |---|--|-|----|------|--|--|-|--|-----------|
1034 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1035 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1036 uint32_t S = (instr >> 26) & 1;
1037 uint32_t cond = (instr >> 22) & 0xF;
1038 uint32_t J2 = (instr >> 11) & 1;
1039 uint32_t form = (instr >> 12) & 1;
1040 uint32_t J1 = (instr >> 13) & 1;
1041 uint32_t imm10 = (instr >> 16) & 0x3FF;
1042 uint32_t imm6 = (instr >> 16) & 0x3F;
1043 uint32_t imm11 = instr & 0x7FF;
1044 opcode << "b";
1045 int32_t imm32;
1046 if (form == 0) {
1047 DumpCond(opcode, cond);
1048 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1049 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1050 } else {
1051 uint32_t I1 = ~(J1 ^ S);
1052 uint32_t I2 = ~(J2 ^ S);
1053 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1054 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1055 }
1056 opcode << ".w";
1057 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001058 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001059 }
Ian Rogers40627db2012-03-04 17:31:09 -08001060 case 4: case 6: case 5: case 7: {
1061 // BL, BLX (immediate)
1062 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1063 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1064 // |---|--|-|----------|--|--|-|--|-----------|
1065 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1066 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1067 // |---|--|-|----------|--|--|-|--|-----------|
1068 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
1069 uint32_t S = (instr >> 26) & 1;
1070 uint32_t J2 = (instr >> 11) & 1;
1071 uint32_t L = (instr >> 12) & 1;
1072 uint32_t J1 = (instr >> 13) & 1;
1073 uint32_t imm10 = (instr >> 16) & 0x3FF;
1074 uint32_t imm11 = instr & 0x7FF;
1075 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001076 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -08001077 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001078 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001079 }
1080 uint32_t I1 = ~(J1 ^ S);
1081 uint32_t I2 = ~(J2 ^ S);
1082 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1083 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001084 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001085 break;
1086 }
1087 }
1088 }
1089 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001090 case 3:
1091 switch (op2) {
1092 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
1093 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
1094 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001095 // |111|11|100|000|0|0000|1111|110000|000000|
1096 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1097 // |---|--|---|---|-|----|----|------|------|
1098 // |332|22|222|222|2|1111|1111|110000|000000|
1099 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1100 // |---|--|---|---|-|----|----|------|------|
1101 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001102 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001103 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001104 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001105 case 0x0: case 0x4: {
1106 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1107 // 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 -07001108 ArmRegister Rn(instr, 16);
1109 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -07001110 opcode << "strb";
1111 if ((instr & 0x800) != 0) {
1112 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001113 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001114 } else {
1115 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001116 ArmRegister Rm(instr, 0);
1117 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001118 if (imm2 != 0) {
1119 args << ", " << "lsl #" << imm2;
1120 }
1121 args << "]";
1122 }
1123 break;
1124 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001125 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001126 ArmRegister Rn(instr, 16);
1127 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001128 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001129 if ((instr & 0x800) != 0) {
1130 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1131 uint32_t P = (instr >> 10) & 1;
1132 uint32_t U = (instr >> 9) & 1;
1133 uint32_t W = (instr >> 8) & 1;
1134 uint32_t imm8 = instr & 0xFF;
1135 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1136 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1137 opcode << "push";
1138 args << Rt;
1139 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1140 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001141 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001142 if (P == 1 && U == 1 && W == 0) {
1143 opcode << "strt";
1144 } else {
1145 opcode << "str";
1146 }
1147 args << Rt << ", [" << Rn;
1148 if (P == 0 && W == 1) {
1149 args << "], #" << imm32;
1150 } else {
1151 args << ", #" << imm32 << "]";
1152 if (W == 1) {
1153 args << "!";
1154 }
Ian Rogers40627db2012-03-04 17:31:09 -08001155 }
1156 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001157 } else {
1158 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1159 ArmRegister Rn(instr, 16);
1160 ArmRegister Rt(instr, 12);
1161 ArmRegister Rm(instr, 0);
1162 uint32_t imm2 = (instr >> 4) & 3;
1163 opcode << "str.w";
1164 args << Rt << ", [" << Rn << ", " << Rm;
1165 if (imm2 != 0) {
1166 args << ", lsl #" << imm2;
1167 }
1168 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001169 }
1170 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001171 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001172 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001173 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001174 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001175 }
Ian Rogers40627db2012-03-04 17:31:09 -08001176 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001177 }
1178 }
1179
1180 break;
1181 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001182 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -08001183 // Load halfword
1184 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1185 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1186 // |---|--|--|---|--|-|----|----|------|------|
1187 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1188 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1189 // |---|--|--|---|--|-|----|----|------|------|
1190 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1191 // |111|11| op2 | | | imm12 |
1192 uint32_t op3 = (instr >> 23) & 3;
1193 ArmRegister Rn(instr, 16);
1194 ArmRegister Rt(instr, 12);
1195 if (Rt.r != 15) {
1196 if (op3 == 1) {
1197 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1198 uint32_t imm12 = instr & 0xFFF;
1199 opcode << "ldrh.w";
1200 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1201 if (Rn.r == 9) {
1202 args << " ; ";
1203 Thread::DumpThreadOffset(args, imm12, 4);
1204 } else if (Rn.r == 15) {
1205 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1206 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1207 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1208 }
1209 } else if (op3 == 3) {
1210 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
1211 uint32_t imm12 = instr & 0xFFF;
1212 opcode << "ldrsh.w";
1213 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1214 if (Rn.r == 9) {
1215 args << " ; ";
1216 Thread::DumpThreadOffset(args, imm12, 4);
1217 } else if (Rn.r == 15) {
1218 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1219 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1220 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1221 }
1222 }
1223 }
1224 break;
1225 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001226 case 0x29: { // 0101001
1227 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1228 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1229 // |---|--|-------|----|----|----|--|---|----|
1230 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1231 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1232 // |---|--|-------|----|----|----|--|---|----|
1233 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1234 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1235 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1236 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1237 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1238 if ((instr & 0xf0c0) == 0xf080) {
1239 uint32_t op3 = (instr >> 4) & 3;
1240 opcode << kThumbReverseOperations[op3];
1241 ArmRegister Rm(instr, 0);
1242 ArmRegister Rd(instr, 8);
1243 args << Rd << ", " << Rm;
1244 ArmRegister Rm2(instr, 16);
1245 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1246 args << " (UNPREDICTABLE)";
1247 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001248 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001249 break;
1250 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001251 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001252 // Load word
1253 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1254 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1255 // |---|--|--|---|--|-|----|----|------|------|
1256 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1257 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1258 // |---|--|--|---|--|-|----|----|------|------|
1259 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1260 // |111|11| op2 | | | imm12 |
1261 uint32_t op3 = (instr >> 23) & 3;
1262 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001263 ArmRegister Rn(instr, 16);
1264 ArmRegister Rt(instr, 12);
1265 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001266 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1267 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1268 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001269 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001270 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001271 if (Rn.r == 9) {
1272 args << " ; ";
1273 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001274 } else if (Rn.r == 15) {
1275 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1276 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1277 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001278 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001279 } else if (op4 == 0) {
1280 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1281 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001282 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001283 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001284 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001285 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001286 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001287 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001288 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001289 } else {
1290 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1291 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001292 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001293 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001294 }
1295 break;
1296 }
Dave Allison70202782013-10-22 17:52:19 -07001297 default: // more formats
1298 if ((op2 >> 4) == 2) { // 010xxxx
1299 // data processing (register)
1300 } else if ((op2 >> 3) == 6) { // 0110xxx
1301 // Multiply, multiply accumulate, and absolute difference
1302 op1 = (instr >> 20) & 0x7;
1303 op2 = (instr >> 4) & 0x2;
1304 ArmRegister Ra(instr, 12);
1305 ArmRegister Rn(instr, 16);
1306 ArmRegister Rm(instr, 0);
1307 ArmRegister Rd(instr, 8);
1308 switch (op1) {
1309 case 0:
1310 if (op2 == 0) {
1311 if (Ra.r == 0xf) {
1312 opcode << "mul";
1313 args << Rd << ", " << Rn << ", " << Rm;
1314 } else {
1315 opcode << "mla";
1316 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1317 }
1318 } else {
1319 opcode << "mls";
1320 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1321 }
1322 break;
1323 case 1:
1324 case 2:
1325 case 3:
1326 case 4:
1327 case 5:
1328 case 6:
1329 break; // do these sometime
1330 }
1331 } else if ((op2 >> 3) == 7) { // 0111xxx
1332 // Long multiply, long multiply accumulate, and divide
1333 op1 = (instr >> 20) & 0x7;
1334 op2 = (instr >> 4) & 0xf;
1335 ArmRegister Rn(instr, 16);
1336 ArmRegister Rm(instr, 0);
1337 ArmRegister Rd(instr, 8);
1338 ArmRegister RdHi(instr, 8);
1339 ArmRegister RdLo(instr, 12);
1340 switch (op1) {
1341 case 0:
1342 opcode << "smull";
1343 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1344 break;
1345 case 1:
1346 opcode << "sdiv";
1347 args << Rd << ", " << Rn << ", " << Rm;
1348 break;
1349 case 2:
1350 opcode << "umull";
1351 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1352 break;
1353 case 3:
1354 opcode << "udiv";
1355 args << Rd << ", " << Rn << ", " << Rm;
1356 break;
1357 case 4:
1358 case 5:
1359 case 6:
1360 break; // TODO: when we generate these...
1361 }
1362 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001363 }
1364 default:
1365 break;
1366 }
Ian Rogers9af89402012-09-07 11:29:35 -07001367
1368 // Apply any IT-block conditions to the opcode if necessary.
1369 if (!it_conditions_.empty()) {
1370 opcode << it_conditions_.back();
1371 it_conditions_.pop_back();
1372 }
1373
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001374 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001375 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001376} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001377
1378size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1379 uint16_t instr = ReadU16(instr_ptr);
1380 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1381 if (is_32bit) {
1382 return DumpThumb32(os, instr_ptr);
1383 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001384 std::ostringstream opcode;
1385 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001386 uint16_t opcode1 = instr >> 10;
1387 if (opcode1 < 0x10) {
1388 // shift (immediate), add, subtract, move, and compare
1389 uint16_t opcode2 = instr >> 9;
1390 switch (opcode2) {
1391 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1392 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001393 // Logical shift left - 00 000xx iii mmm ddd
1394 // Logical shift right - 00 001xx iii mmm ddd
1395 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001396 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001397 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001398 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001399 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001400 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001401 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001402 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001403 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001404 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001405 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001406 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001407 break;
1408 }
1409 case 0xC: case 0xD: case 0xE: case 0xF: {
1410 // Add register - 00 01100 mmm nnn ddd
1411 // Sub register - 00 01101 mmm nnn ddd
1412 // Add 3-bit immediate - 00 01110 iii nnn ddd
1413 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1414 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001415 ThumbRegister Rn(instr, 3);
1416 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001417 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001418 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001419 } else {
1420 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001421 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001422 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001423 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001424 }
1425 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001426 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001427 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001428 ArmRegister Rm(imm3_or_Rm);
1429 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001430 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001431 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001432 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001433 break;
1434 }
1435 case 0x10: case 0x11: case 0x12: case 0x13:
1436 case 0x14: case 0x15: case 0x16: case 0x17:
1437 case 0x18: case 0x19: case 0x1A: case 0x1B:
1438 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1439 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1440 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1441 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1442 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001443 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001444 uint16_t imm8 = instr & 0xFF;
1445 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001446 case 4: opcode << "movs"; break;
1447 case 5: opcode << "cmp"; break;
1448 case 6: opcode << "adds"; break;
1449 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001450 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001451 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001452 break;
1453 }
1454 default:
1455 break;
1456 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001457 } else if (opcode1 == 0x10) {
1458 // Data-processing
1459 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001460 ThumbRegister rm(instr, 3);
1461 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001462 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001463 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001464 } else if (opcode1 == 0x11) {
1465 // Special data instructions and branch and exchange
1466 uint16_t opcode2 = (instr >> 6) & 0x0F;
1467 switch (opcode2) {
1468 case 0x0: case 0x1: case 0x2: case 0x3: {
1469 // Add low registers - 010001 0000 xxxxxx
1470 // Add high registers - 010001 0001/001x xxxxxx
1471 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001472 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001473 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001474 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001475 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001476 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001477 break;
1478 }
1479 case 0x8: case 0x9: case 0xA: case 0xB: {
1480 // Move low registers - 010001 1000 xxxxxx
1481 // Move high registers - 010001 1001/101x xxxxxx
1482 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001483 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001484 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001485 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001486 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001487 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001488 break;
1489 }
1490 case 0x5: case 0x6: case 0x7: {
1491 // Compare high registers - 010001 0101/011x xxxxxx
1492 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001493 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001494 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001495 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001496 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001497 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001498 break;
1499 }
1500 case 0xC: case 0xD: case 0xE: case 0xF: {
1501 // Branch and exchange - 010001 110x xxxxxx
1502 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001503 ArmRegister rm(instr, 3);
1504 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1505 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001506 break;
1507 }
1508 default:
1509 break;
1510 }
jeffhaoeae26912013-01-28 16:29:54 -08001511 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1512 ThumbRegister Rt(instr, 8);
1513 uint16_t imm8 = instr & 0xFF;
1514 opcode << "ldr";
1515 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001516 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1517 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1518 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1519 // Load/store single data item
1520 uint16_t opA = (instr >> 12) & 0xF;
1521 if (opA == 0x5) {
1522 uint16_t opB = (instr >> 9) & 0x7;
1523 ThumbRegister Rm(instr, 6);
1524 ThumbRegister Rn(instr, 3);
1525 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001526 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001527 case 0: opcode << "str"; break;
1528 case 1: opcode << "strh"; break;
1529 case 2: opcode << "strb"; break;
1530 case 3: opcode << "ldrsb"; break;
1531 case 4: opcode << "ldr"; break;
1532 case 5: opcode << "ldrh"; break;
1533 case 6: opcode << "ldrb"; break;
1534 case 7: opcode << "ldrsh"; break;
1535 }
1536 args << Rt << ", [" << Rn << ", " << Rm << "]";
1537 } else if (opA == 9) {
1538 uint16_t opB = (instr >> 11) & 1;
1539 ThumbRegister Rt(instr, 8);
1540 uint16_t imm8 = instr & 0xFF;
1541 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001542 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001543 } else {
1544 uint16_t imm5 = (instr >> 6) & 0x1F;
1545 uint16_t opB = (instr >> 11) & 1;
1546 ThumbRegister Rn(instr, 3);
1547 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001548 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001549 case 6:
1550 imm5 <<= 2;
1551 opcode << (opB == 0 ? "str" : "ldr");
1552 break;
1553 case 7:
1554 imm5 <<= 0;
1555 opcode << (opB == 0 ? "strb" : "ldrb");
1556 break;
1557 case 8:
1558 imm5 <<= 1;
1559 opcode << (opB == 0 ? "strh" : "ldrh");
1560 break;
1561 }
1562 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1563 }
jeffhaoeae26912013-01-28 16:29:54 -08001564 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001565 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001566 uint32_t cond = (instr >> 8) & 0xF;
1567 opcode << "b";
1568 DumpCond(opcode, cond);
1569 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001570 } else if ((instr & 0xF800) == 0xA800) {
1571 // Generate SP-relative address
1572 ThumbRegister rd(instr, 8);
1573 int imm8 = instr & 0xFF;
1574 opcode << "add";
1575 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001576 } else if ((instr & 0xF000) == 0xB000) {
1577 // Miscellaneous 16-bit instructions
1578 uint16_t opcode2 = (instr >> 5) & 0x7F;
1579 switch (opcode2) {
1580 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1581 // Add immediate to SP - 1011 00000 ii iiiii
1582 // Subtract immediate from SP - 1011 00001 ii iiiii
1583 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001584 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001585 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001586 break;
1587 }
Ian Rogers087b2412012-03-21 01:30:32 -07001588 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001589 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001590 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1591 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001592 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001593 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1594 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1595 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001596 // CBNZ, CBZ
1597 uint16_t op = (instr >> 11) & 1;
1598 uint16_t i = (instr >> 9) & 1;
1599 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001600 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001601 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001602 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001603 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001604 DumpBranchTarget(args, instr_ptr + 4, imm32);
1605 break;
1606 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001607 case 0x50: case 0x51: // 101000x
1608 case 0x52: case 0x53: // 101001x
1609 case 0x56: case 0x57: { // 101011x
1610 uint16_t op = (instr >> 6) & 3;
1611 opcode << kThumbReverseOperations[op];
1612 ThumbRegister Rm(instr, 3);
1613 ThumbRegister Rd(instr, 0);
1614 args << Rd << ", " << Rm;
1615 break;
1616 }
Ian Rogers40627db2012-03-04 17:31:09 -08001617 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1618 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1619 // If-Then, and hints
1620 uint16_t opA = (instr >> 4) & 0xF;
1621 uint16_t opB = instr & 0xF;
1622 if (opB == 0) {
1623 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001624 case 0: opcode << "nop"; break;
1625 case 1: opcode << "yield"; break;
1626 case 2: opcode << "wfe"; break;
1627 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001628 default: break;
1629 }
1630 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001631 uint32_t first_cond = opA;
1632 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001633 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001634
1635 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1636 // and store up the actual condition codes we'll want to add to the next few opcodes.
1637 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001638 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001639 for (size_t i = 0; i < count; ++i) {
1640 bool positive_cond = ((first_cond & 1) != 0);
1641 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1642 if (positive_mask == positive_cond) {
1643 opcode << 't';
1644 it_conditions_[i] = kConditionCodeNames[first_cond];
1645 } else {
1646 opcode << 'e';
1647 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1648 }
1649 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001650 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001651
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001652 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1653 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001654 }
1655 break;
1656 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001657 default:
1658 break;
1659 }
1660 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1661 ((instr & 0xE000) == 0x8000)) {
1662 // Load/store single data item
1663 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001664 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001665 switch (opA) {
1666 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001667 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1668 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001669 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001670 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001671 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001672 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1673 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001674 break;
1675 }
1676 case 0x9: {
1677 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1678 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1679 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001680 ThumbRegister Rt(instr, 8);
1681 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1682 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001683 break;
1684 }
1685 default:
1686 break;
1687 }
Ian Rogers40627db2012-03-04 17:31:09 -08001688 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1689 uint16_t imm11 = instr & 0x7FFF;
1690 int32_t imm32 = imm11 << 1;
1691 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001692 opcode << "b";
1693 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001694 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001695
1696 // Apply any IT-block conditions to the opcode if necessary.
1697 if (!it_conditions_.empty()) {
1698 opcode << it_conditions_.back();
1699 it_conditions_.pop_back();
1700 }
1701
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001703 }
1704 return 2;
1705}
1706
1707} // namespace arm
1708} // namespace art