blob: c35edbbcbf5bda0990334710782ae1cc3f5b254b [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
30DisassemblerArm::DisassemblerArm() {
31}
32
Ian Rogersb23a7722012-10-09 16:54:26 -070033size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
34 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
35 DumpArm(os, begin);
36 return 4;
37 } else {
38 // remove thumb specifier bits
39 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
40 return DumpThumb16(os, begin);
41 }
42}
43
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080044void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
45 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
46 for (const uint8_t* cur = begin; cur < end; cur += 4) {
47 DumpArm(os, cur);
48 }
49 } else {
50 // remove thumb specifier bits
51 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
52 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
53 for (const uint8_t* cur = begin; cur < end;) {
54 cur += DumpThumb16(os, cur);
55 }
56 }
57}
58
Elliott Hughes77405792012-03-15 15:22:12 -070059static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070060 "eq", // 0000 - equal
61 "ne", // 0001 - not-equal
62 "cs", // 0010 - carry-set, greater than, equal or unordered
63 "cc", // 0011 - carry-clear, less than
64 "mi", // 0100 - minus, negative
65 "pl", // 0101 - plus, positive or zero
66 "vs", // 0110 - overflow
67 "vc", // 0111 - no overflow
68 "hi", // 1000 - unsigned higher
69 "ls", // 1001 - unsigned lower or same
70 "ge", // 1010 - signed greater than or equal
71 "lt", // 1011 - signed less than
72 "gt", // 1100 - signed greater than
73 "le", // 1101 - signed less than or equal
74 "", // 1110 - always
75 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080076};
77
78void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
79 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070080 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080081 } else {
82 os << "Unexpected condition: " << cond;
83 }
84}
85
Ian Rogersb122a4b2013-11-19 18:00:50 -080086void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
87 switch (domain) {
88 case 0b1111: os << "sy"; break;
89 case 0b1110: os << "st"; break;
90 case 0b1011: os << "ish"; break;
91 case 0b1010: os << "ishst"; break;
92 case 0b0111: os << "nsh"; break;
93 case 0b0110: os << "nshst"; break;
94 case 0b0011: os << "osh"; break;
95 case 0b0010: os << "oshst"; break;
96 }
97}
98
Ian Rogers40627db2012-03-04 17:31:09 -080099void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700100 os << StringPrintf("%+d (%p)", imm32, instr_ptr + imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800101}
102
103static uint32_t ReadU16(const uint8_t* ptr) {
104 return ptr[0] | (ptr[1] << 8);
105}
106
107static uint32_t ReadU32(const uint8_t* ptr) {
108 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
109}
110
Elliott Hughes77405792012-03-15 15:22:12 -0700111static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700112 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
113 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700114};
115
Ian Rogersad03ef52012-03-18 19:34:47 -0700116static const char* kThumbDataProcessingOperations[] = {
117 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
118 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
119};
120
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100121static const char* kThumbReverseOperations[] = {
122 "rev", "rev16", "rbit", "revsh"
123};
124
Elliott Hughes77405792012-03-15 15:22:12 -0700125struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -0700126 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700127 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -0700128 uint32_t r;
129};
130std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
131 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700132 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700133 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700134 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700135 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700136 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700137 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700138 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700139 }
140 return os;
141}
142
Elliott Hughes630e77d2012-03-22 19:20:56 -0700143struct ThumbRegister : ArmRegister {
144 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700145};
146
147struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700148 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700149 uint32_t shift;
150 ArmRegister rm;
151};
152std::ostream& operator<<(std::ostream& os, const Rm& r) {
153 os << r.rm;
154 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700155 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700156 }
157 return os;
158}
159
Elliott Hughes1ca98492012-04-12 17:21:02 -0700160struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700161 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700162 uint32_t rotate = ((instruction >> 8) & 0xf);
163 uint32_t imm = (instruction & 0xff);
164 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
165 }
166 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700167};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700168std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700169 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700170 return os;
171}
172
173struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700174 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700175 uint32_t register_list;
176};
177std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
178 if (rhs.register_list == 0) {
179 os << "<no register list?>";
180 return os;
181 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700182 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700183 bool first = true;
184 for (size_t i = 0; i < 16; i++) {
185 if ((rhs.register_list & (1 << i)) != 0) {
186 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700187 first = false;
188 } else {
189 os << ", ";
190 }
191 os << ArmRegister(i);
192 }
193 }
194 os << "}";
195 return os;
196}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800197
Vladimir Markodd577a32013-11-07 19:25:24 +0000198struct FpRegister {
199 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
200 size = (instr >> 8) & 1;
201 uint32_t Vn = (instr >> at_bit) & 0xF;
202 uint32_t N = (instr >> extra_at_bit) & 1;
203 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
204 }
Zheng Xue19649a2014-02-27 13:30:55 +0000205 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit,
206 uint32_t forced_size) {
207 size = forced_size;
208 uint32_t Vn = (instr >> at_bit) & 0xF;
209 uint32_t N = (instr >> extra_at_bit) & 1;
210 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
211 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000212 FpRegister(const FpRegister& other, uint32_t offset)
213 : size(other.size), r(other.r + offset) {}
214
215 uint32_t size; // 0 = f32, 1 = f64
216 uint32_t r;
217};
218std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
219 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
220}
221
222struct FpRegisterRange {
223 explicit FpRegisterRange(uint32_t instr)
224 : first(instr, 12, 22), imm8(instr & 0xFF) {}
225 FpRegister first;
226 uint32_t imm8;
227};
228std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
229 os << "{" << rhs.first;
230 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
231 if (count > 1) {
232 os << "-" << FpRegister(rhs.first, count - 1);
233 }
234 if (rhs.imm8 == 0) {
235 os << " (EMPTY)";
236 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
237 os << rhs.first << " (HALF)";
238 }
239 os << "}";
240 return os;
241}
242
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800243void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700244 uint32_t instruction = ReadU32(instr_ptr);
245 uint32_t cond = (instruction >> 28) & 0xf;
246 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700247 std::string opcode;
248 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700249 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700250 switch (op1) {
251 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700252 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700253 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700254 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700255 opcode = "bkpt";
256 uint32_t imm12 = (instruction >> 8) & 0xfff;
257 uint32_t imm4 = (instruction & 0xf);
258 args << '#' << ((imm12 << 4) | imm4);
259 break;
260 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700261 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700262 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700263 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700264 break;
265 }
266 bool i = (instruction & (1 << 25)) != 0;
267 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700268 uint32_t op = (instruction >> 21) & 0xf;
269 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700270 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700271 if (implicit_s) {
272 // Rd is unused (and not shown), and we don't show the 's' suffix either.
273 } else {
274 if (s) {
275 suffixes += 's';
276 }
277 args << ArmRegister(instruction, 12) << ", ";
278 }
Elliott Hughes77405792012-03-15 15:22:12 -0700279 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700280 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700281 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700282 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700283 }
284 }
285 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700286 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700287 {
288 bool p = (instruction & (1 << 24)) != 0;
289 bool b = (instruction & (1 << 22)) != 0;
290 bool w = (instruction & (1 << 21)) != 0;
291 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700292 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700293 args << ArmRegister(instruction, 12) << ", ";
294 ArmRegister rn(instruction, 16);
295 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700296 UNIMPLEMENTED(FATAL) << "literals";
297 } else {
298 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700299 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700300 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700301 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700302 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700303 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700304 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700305 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700306 } else {
307 LOG(FATAL) << p << " " << w;
308 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700309 if (rn.r == 9) {
310 args << " ; ";
Elliott Hughes1ca98492012-04-12 17:21:02 -0700311 Thread::DumpThreadOffset(args, offset, 4);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700312 }
Elliott Hughes77405792012-03-15 15:22:12 -0700313 }
314 }
315 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700316 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700317 {
318 bool p = (instruction & (1 << 24)) != 0;
319 bool u = (instruction & (1 << 23)) != 0;
320 bool w = (instruction & (1 << 21)) != 0;
321 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700322 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700323 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700324 }
325 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700326 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700327 {
328 bool bl = (instruction & (1 << 24)) != 0;
329 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700330 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700331 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700332 DumpBranchTarget(args, instr_ptr + 8, imm32);
333 }
334 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700335 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700336 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700337 break;
338 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700339 opcode += kConditionCodeNames[cond];
340 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700341 // TODO: a more complete ARM disassembler could generate wider opcodes.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800342 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800343}
344
Ian Rogersa9650dd2013-10-04 08:23:32 -0700345int32_t ThumbExpand(int32_t imm12) {
346 if ((imm12 & 0xC00) == 0) {
347 switch ((imm12 >> 8) & 3) {
348 case 0:
349 return imm12 & 0xFF;
350 case 1:
351 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
352 case 2:
353 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
354 default: // 3
355 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
356 (imm12 & 0xFF);
357 }
358 } else {
359 uint32_t val = 0x80 | (imm12 & 0x7F);
360 int32_t rotate = (imm12 >> 7) & 0x1F;
361 return (val >> rotate) | (val << (32 - rotate));
362 }
363}
364
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800365size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
366 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
367 // |111|1 1|1000000|0000|1111110000000000|
368 // |5 3|2 1|0987654|3 0|5 0 5 0|
369 // |---|---|-------|----|----------------|
370 // |332|2 2|2222222|1111|1111110000000000|
371 // |1 9|8 7|6543210|9 6|5 0 5 0|
372 // |---|---|-------|----|----------------|
373 // |111|op1| op2 | | |
374 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700375 if (op1 == 0) {
376 return DumpThumb16(os, instr_ptr);
377 }
378
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800379 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700380 std::ostringstream opcode;
381 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800382 switch (op1) {
383 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800384 break;
385 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700386 if ((op2 & 0x64) == 0) { // 00x x0xx
387 // |111|11|10|00|0|00|0000|1111110000000000|
388 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
389 // |---|--|--|--|-|--|----|----------------|
390 // |332|22|22|22|2|22|1111|1111110000000000|
391 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
392 // |---|--|--|--|-|--|----|----------------|
393 // |111|01|00|op|0|WL| Rn | |
394 // |111|01| op2 | | |
395 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
396 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
397 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
398 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
399 uint32_t op = (instr >> 23) & 3;
400 uint32_t W = (instr >> 21) & 1;
401 uint32_t L = (instr >> 20) & 1;
402 ArmRegister Rn(instr, 16);
403 if (op == 1 || op == 2) {
404 if (op == 1) {
405 if (L == 0) {
406 opcode << "stm";
407 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700409 if (Rn.r != 13) {
410 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700411 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700412 } else {
413 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800414 }
415 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700416 } else {
417 if (L == 0) {
418 if (Rn.r != 13) {
419 opcode << "stmdb";
420 args << Rn << (W == 0 ? "" : "!") << ", ";
421 } else {
422 opcode << "push";
423 }
424 } else {
425 opcode << "ldmdb";
426 args << Rn << (W == 0 ? "" : "!") << ", ";
427 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800428 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700429 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800430 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700431 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700432 uint32_t op3 = (instr >> 23) & 3;
433 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700434 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700435 ArmRegister Rn(instr, 16);
436 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700437 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700438 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700439 if ((op3 & 2) == 2) { // 1x
440 int W = (instr >> 21) & 1;
441 int U = (instr >> 23) & 1;
442 int P = (instr >> 24) & 1;
443
444 if ((op4 & 1) == 1) {
445 opcode << "ldrd";
446 } else {
447 opcode << "strd";
448 }
449 args << Rt << "," << Rd << ", [" << Rn;
450 const char *sign = U ? "+" : "-";
451 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000452 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700453 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000454 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700455 if (W == 1) {
456 args << "!";
457 }
458 }
459 } else { // 0x
460 switch (op4) {
461 case 0:
462 if (op3 == 0) { // op3 is 00, op4 is 00
463 opcode << "strex";
464 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000465 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
466 Rd.r == Rn.r || Rd.r == Rt.r) {
467 args << " (UNPREDICTABLE)";
468 }
Dave Allison70202782013-10-22 17:52:19 -0700469 } else { // op3 is 01, op4 is 00
470 // this is one of strexb, strexh or strexd
471 int op5 = (instr >> 4) & 0xf;
472 switch (op5) {
473 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700474 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000475 opcode << ((op5 == 4) ? "strexb" : "strexh");
476 Rd = ArmRegister(instr, 0);
477 args << Rd << ", " << Rt << ", [" << Rn << "]";
478 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
479 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
480 args << " (UNPREDICTABLE)";
481 }
Dave Allison70202782013-10-22 17:52:19 -0700482 break;
483 case 7:
484 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000485 ArmRegister Rt2 = Rd;
486 Rd = ArmRegister(instr, 0);
487 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
488 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
489 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
490 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
491 args << " (UNPREDICTABLE)";
492 }
Dave Allison70202782013-10-22 17:52:19 -0700493 break;
494 }
495 }
496 break;
497 case 1:
498 if (op3 == 0) { // op3 is 00, op4 is 01
499 opcode << "ldrex";
500 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000501 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
502 args << " (UNPREDICTABLE)";
503 }
Dave Allison70202782013-10-22 17:52:19 -0700504 } else { // op3 is 01, op4 is 01
505 // this is one of strexb, strexh or strexd
506 int op5 = (instr >> 4) & 0xf;
507 switch (op5) {
508 case 0:
509 opcode << "tbb";
510 break;
511 case 1:
512 opcode << "tbh";
513 break;
514 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700515 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000516 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
517 args << Rt << ", [" << Rn << "]";
518 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
519 args << " (UNPREDICTABLE)";
520 }
Dave Allison70202782013-10-22 17:52:19 -0700521 break;
522 case 7:
523 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000524 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
525 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
526 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
527 args << " (UNPREDICTABLE)";
528 }
Dave Allison70202782013-10-22 17:52:19 -0700529 break;
530 }
531 }
532 break;
533 case 2: // op3 is 0x, op4 is 10
534 case 3: // op3 is 0x, op4 is 11
535 if (op4 == 2) {
536 opcode << "strd";
537 } else {
538 opcode << "ldrd";
539 }
540 int W = (instr >> 21) & 1;
541 int U = (instr >> 23) & 1;
542 int P = (instr >> 24) & 1;
543
544 args << Rt << "," << Rd << ", [" << Rn;
545 const char *sign = U ? "+" : "-";
546 if (P == 0 && W == 1) {
547 args << "], #" << sign << imm8;
548 } else {
549 args << ", #" << sign << imm8 << "]";
550 if (W == 1) {
551 args << "!";
552 }
553 }
554 break;
555 }
556 }
557
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700558 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
559 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100560 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
561 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
562 // |---|----|----|-|----|----|----|--|--|----|
563 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
564 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
565 // |---|----|----|-|----|----|----|--|--|----|
566 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700567 uint32_t op3 = (instr >> 21) & 0xF;
568 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100569 uint32_t imm3 = ((instr >> 12) & 0x7);
570 uint32_t imm2 = ((instr >> 6) & 0x3);
571 uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F;
572 uint32_t shift_type = ((instr >> 4) & 0x2);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700573 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100574 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700575 ArmRegister Rm(instr, 0);
576 switch (op3) {
577 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100578 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700579 opcode << "and";
580 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700581 if (S != 1U) {
582 opcode << "UNKNOWN TST-" << S;
583 break;
584 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700585 opcode << "tst";
586 S = 0; // don't print 's'
587 }
588 break;
589 case 0x1: opcode << "bic"; break;
590 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100591 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700592 opcode << "orr";
593 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100594 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700595 opcode << "mov";
596 }
597 break;
598 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100599 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700600 opcode << "orn";
601 } else {
602 opcode << "mvn";
603 }
604 break;
605 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100606 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700607 opcode << "eor";
608 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700609 if (S != 1U) {
610 opcode << "UNKNOWN TEQ-" << S;
611 break;
612 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700613 opcode << "teq";
614 S = 0; // don't print 's'
615 }
616 break;
617 case 0x6: opcode << "pkh"; break;
618 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100619 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700620 opcode << "add";
621 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700622 if (S != 1U) {
623 opcode << "UNKNOWN CMN-" << S;
624 break;
625 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700626 opcode << "cmn";
627 S = 0; // don't print 's'
628 }
629 break;
630 case 0xA: opcode << "adc"; break;
631 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100632 case 0xD:
633 if (Rd.r != 0xF) {
634 opcode << "sub";
635 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700636 if (S != 1U) {
637 opcode << "UNKNOWN CMP-" << S;
638 break;
639 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100640 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100641 S = 0; // don't print 's'
642 }
643 break;
644 case 0xE: opcode << "rsb"; break;
645 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700646 }
Ian Rogers087b2412012-03-21 01:30:32 -0700647
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700648 if (S == 1) {
649 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700650 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700651 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100652
653 if (Rd.r != 0xF) {
654 args << Rd << ", ";
655 }
656 if (Rn.r != 0xF) {
657 args << Rn << ", ";
658 }
659 args << Rm;
660
661 // Shift operand.
662 bool noShift = (imm5 == 0 && shift_type != 0x3);
663 if (!noShift) {
664 args << ", ";
665 switch (shift_type) {
666 case 0x0: args << "lsl"; break;
667 case 0x1: args << "lsr"; break;
668 case 0x2: args << "asr"; break;
669 case 0x3:
670 if (imm5 == 0) {
671 args << "rrx";
672 } else {
673 args << "ror";
674 }
675 break;
676 }
677 if (shift_type != 0x3 /* rrx */) {
678 args << StringPrintf(" #%d", imm5);
679 }
680 }
681
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700682 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
683 // Co-processor instructions
684 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
685 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
686 // |---|-|--|------|----|----|----|---|---|----|
687 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
688 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
689 // |---|-|--|------|----|----|----|---|---|----|
690 // |111| |11| op3 | Rn | |copr| |op4| |
691 uint32_t op3 = (instr >> 20) & 0x3F;
692 uint32_t coproc = (instr >> 8) & 0xF;
693 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700694
Ian Rogersef6a7762013-12-19 17:58:05 -0800695 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000696 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
697 // Extension register load/store instructions
698 // |1111|110|00000|0000|1111|110|0|00000000|
699 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
700 // |----|---|-----|----|----|---|-|--------|
701 // |3322|222|22222|1111|1111|110|0|00000000|
702 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
703 // |----|---|-----|----|----|---|-|--------|
704 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700705 uint32_t P = (instr >> 24) & 1;
706 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700707 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000708 if (P == U && W == 1) {
709 opcode << "UNDEFINED";
710 } else {
711 uint32_t L = (instr >> 20) & 1;
712 uint32_t S = (instr >> 8) & 1;
713 ArmRegister Rn(instr, 16);
714 if (P == 1 && W == 0) { // VLDR
715 FpRegister d(instr, 12, 22);
716 uint32_t imm8 = instr & 0xFF;
717 opcode << (L == 1 ? "vldr" : "vstr");
718 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
719 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800720 if (Rn.r == 15 && U == 1) {
721 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
722 lit_adr = RoundDown(lit_adr, 4) + 4 + (imm8 << 2);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800723 args << StringPrintf(" ; 0x%" PRIx64, *reinterpret_cast<int64_t*>(lit_adr));
Ian Rogersef6a7762013-12-19 17:58:05 -0800724 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000725 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
726 opcode << (L == 1 ? "vpop" : "vpush");
727 args << FpRegisterRange(instr);
728 } else { // VLDM
729 opcode << (L == 1 ? "vldm" : "vstm");
730 args << Rn << ((W == 1) ? "!" : "") << ", "
731 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700732 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000733 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700734 }
Dave Allison70202782013-10-22 17:52:19 -0700735 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000736 if ((instr & 0xD0) == 0x10) {
737 // 64bit transfers between ARM core and extension registers.
738 uint32_t L = (instr >> 20) & 1;
739 uint32_t S = (instr >> 8) & 1;
740 ArmRegister Rt2(instr, 16);
741 ArmRegister Rt(instr, 12);
742 FpRegister m(instr, 0, 5);
743 opcode << "vmov" << (S ? ".f64" : ".f32");
744 if (L == 1) {
745 args << Rt << ", " << Rt2 << ", ";
746 }
747 if (S) {
748 args << m;
749 } else {
750 args << m << ", " << FpRegister(m, 1);
751 }
752 if (L == 0) {
753 args << ", " << Rt << ", " << Rt2;
754 }
755 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
756 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
757 args << " (UNPREDICTABLE)";
758 }
759 }
Dave Allison70202782013-10-22 17:52:19 -0700760 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
761 // fp data processing
Zheng Xue19649a2014-02-27 13:30:55 +0000762 if ((op3 & 0xB) == 0) { // 100x00
763 // VMLA, VMLS
764 // |1111|1100|0|0|00|0000|1111|110|0|0|0 |0|0|0000|
765 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6 |5|4|3 0|
766 // |----|----|-|-|--|----|----|---|-|-|- |-|-|----|
767 // |3322|2222|2|2|22|1111|1111|110|0|0|0 |0|0|0000|
768 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6 |5|4|3 0|
769 // |----|----|-|-|--|----|----|---|-|-|- |-|-|----|
770 // |1110|1110|0|D|00| Vn | Vd |101|S|N|op|M|0| Vm |
771 uint32_t op = (instr >> 6) & 1;
772 FpRegister d(instr, 12, 22);
773 FpRegister n(instr, 16, 7);
774 FpRegister m(instr, 0, 5);
775 opcode << (op == 0 ? "vmla" : "vmls");
776 args << d << ", " << n << ", " << m;
777 } else if ((op3 & 0xB) == 0xB) { // 101x11
778 uint32_t Q = (instr >> 6) & 1;
779 if (Q == 1) {
780 // VCVT (floating-point conversion)
781 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
782 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
783 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
784 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
785 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
786 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
787 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm |
788 uint32_t op5 = (instr >> 16) & 0xF;
789 uint32_t S = (instr >> 8) & 1;
790 uint32_t op = (instr >> 7) & 1;
791 // Register types in these instructions relies on the combination of op5 and S.
792 FpRegister Dd(instr, 12, 22, 1);
793 FpRegister Sd(instr, 12, 22, 0);
794 FpRegister Dm(instr, 0, 5, 1);
795 FpRegister Sm(instr, 0, 5, 0);
796 if (op5 == 0xD) {
797 if (S == 1) {
798 // vcvt{r}.s32.f64
799 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
800 args << Sd << ", " << Dm;
801 } else {
802 // vcvt{r}.s32.f32
803 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
804 args << Sd << ", " << Sm;
805 }
806 } else if (op5 == 0xC) {
807 if (S == 1) {
808 // vcvt{r}.u32.f64
809 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
810 args << Sd << ", " << Dm;
811 } else {
812 // vcvt{r}.u32.f32
813 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
814 args << Sd << ", " << Sm;
815 }
816 } else if (op5 == 0x8) {
817 if (S == 1) {
818 // vcvt.f64.<Tm>
819 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
820 args << Dd << ", " << Sm;
821 } else {
822 // vcvt.f32.<Tm>
823 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
824 args << Sd << ", " << Sm;
825 }
826 } else if (op5 == 0x7) {
827 if (op == 1) {
828 if (S == 1) {
829 // vcvt.f64.f32
830 opcode << "vcvt.f64.f32";
831 args << Dd << ", " << Sm;
832 } else {
833 // vcvt.f32.f64
834 opcode << "vcvt.f32.f64";
835 args << Sd << ", " << Dm;
836 }
837 }
838 }
839 }
840 }
Dave Allison70202782013-10-22 17:52:19 -0700841 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000842 if (coproc == 10 && (op3 & 0xE) == 0) {
843 // VMOV (between ARM core register and single-precision register)
844 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
845 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
846 // |----|----|---|- |----|----|----|-|--|-|----|
847 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
848 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
849 // |----|----|---|- |----|----|----|-|--|-|----|
850 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
851 uint32_t op = op3 & 1;
852 ArmRegister Rt(instr, 12);
853 FpRegister n(instr, 16, 7);
854 opcode << "vmov.f32";
855 if (op) {
856 args << Rt << ", " << n;
857 } else {
858 args << n << ", " << Rt;
859 }
860 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
861 args << " (UNPREDICTABLE)";
862 }
863 } else if (coproc == 10 && op3 == 0x2F) {
864 // VMRS
865 // |1111|11000000|0000|1111|1100|000|0|0000|
866 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
867 // |----|--------|----|----|----|---|-|----|
868 // |3322|22222222|1111|1111|1100|000|0|0000|
869 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
870 // |----|--------|----|----|----|---|-|----|
871 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
872 uint32_t spec_reg = (instr >> 16) & 0xF;
873 ArmRegister Rt(instr, 12);
874 opcode << "vmrs";
875 if (spec_reg == 1) {
876 if (Rt.r == 15) {
877 args << "APSR_nzcv, FPSCR";
878 } else if (Rt.r == 13) {
879 args << Rt << ", FPSCR (UNPREDICTABLE)";
880 } else {
881 args << Rt << ", FPSCR";
882 }
883 } else {
884 args << "(PRIVILEGED)";
885 }
886 } else if (coproc == 11 && (op3 & 0x9) != 8) {
887 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
888 }
Ian Rogers9af89402012-09-07 11:29:35 -0700889 }
Dave Allison70202782013-10-22 17:52:19 -0700890 }
891
892 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700893 if ((coproc & 0xE) == 0xA) {
894 // VFP data-processing instructions
895 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
896 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
897 // |---|-|----|----|----|----|---|-|----|-|-|----|
898 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
899 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
900 // |---|-|----|----|----|----|---|-|----|-|-|----|
901 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
902 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
903 uint32_t opc1 = (instr >> 20) & 0xF;
904 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700905 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700906 if ((opc1 & 0xB) == 0xB) { // 1x11
907 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700908 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000909 FpRegister d(instr, 12, 22);
910 FpRegister m(instr, 0, 5);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700911 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700912 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700913 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000914 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (sz == 1 ? ".f64" : ".f32");
915 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700916 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700917 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700918 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000919 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (sz == 1 ? ".f64" : ".f32");
920 args << d << ", " << m;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700921 break;
922 }
923 }
924 }
925 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700926 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
927 // Advanced SIMD
928 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
929 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
930 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
Ian Rogers0183dd72012-09-17 23:06:51 -0700931 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000932 FpRegister d(instr, 12, 22);
933 FpRegister m(instr, 0, 5);
934 opcode << "vsqrt" << (sz == 1 ? ".f64" : ".f32");
935 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700936 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700937 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800938 }
939 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800940 case 2:
941 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
942 // Data-processing (modified immediate)
943 // |111|11|10|0000|0|0000|1|111|1100|00000000|
944 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
945 // |---|--|--|----|-|----|-|---|----|--------|
946 // |332|22|22|2222|2|1111|1|111|1100|00000000|
947 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
948 // |---|--|--|----|-|----|-|---|----|--------|
949 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
950 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800951 uint32_t i = (instr >> 26) & 1;
952 uint32_t op3 = (instr >> 21) & 0xF;
953 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700954 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800955 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700956 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800957 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800958 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
959 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
960 if (op3 == 0x2) {
961 opcode << "mov";
962 if (S == 1) {
963 opcode << "s";
964 }
965 opcode << ".w";
966 } else {
967 opcode << "mvn";
968 if (S == 1) {
969 opcode << "s";
970 }
971 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700972 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800973 } else if (Rd.r == 0xF && S == 1 &&
974 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
975 if (op3 == 0x0) {
976 opcode << "tst";
977 } else if (op3 == 0x4) {
978 opcode << "teq";
979 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +0000980 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800981 } else {
982 opcode << "cmp.w";
983 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700984 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800985 } else {
986 switch (op3) {
987 case 0x0: opcode << "and"; break;
988 case 0x1: opcode << "bic"; break;
989 case 0x2: opcode << "orr"; break;
990 case 0x3: opcode << "orn"; break;
991 case 0x4: opcode << "eor"; break;
992 case 0x8: opcode << "add"; break;
993 case 0xA: opcode << "adc"; break;
994 case 0xB: opcode << "sbc"; break;
995 case 0xD: opcode << "sub"; break;
996 case 0xE: opcode << "rsb"; break;
997 default: opcode << "UNKNOWN DPMI-" << op3; break;
998 }
999 if (S == 1) {
1000 opcode << "s";
1001 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001002 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001003 }
Ian Rogers40627db2012-03-04 17:31:09 -08001004 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1005 // Data-processing (plain binary immediate)
1006 // |111|11|10|00000|0000|1|111110000000000|
1007 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1008 // |---|--|--|-----|----|-|---------------|
1009 // |332|22|22|22222|1111|1|111110000000000|
1010 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1011 // |---|--|--|-----|----|-|---------------|
1012 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1013 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001014 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001015 case 0x00: case 0x0A: {
1016 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001017 ArmRegister Rd(instr, 8);
1018 ArmRegister Rn(instr, 16);
1019 uint32_t i = (instr >> 26) & 1;
1020 uint32_t imm3 = (instr >> 12) & 0x7;
1021 uint32_t imm8 = instr & 0xFF;
1022 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1023 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001024 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001025 args << Rd << ", " << Rn << ", #" << imm12;
1026 } else {
1027 opcode << "adr";
1028 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001029 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001030 }
1031 break;
1032 }
Ian Rogers55019132013-02-08 01:05:23 -08001033 case 0x04: case 0x0C: {
1034 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001035 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001036 uint32_t i = (instr >> 26) & 1;
1037 uint32_t imm3 = (instr >> 12) & 0x7;
1038 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001039 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001040 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001041 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001042 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001043 break;
1044 }
jeffhaoeae26912013-01-28 16:29:54 -08001045 case 0x16: {
1046 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
1047 ArmRegister Rd(instr, 8);
1048 ArmRegister Rn(instr, 16);
1049 uint32_t msb = instr & 0x1F;
1050 uint32_t imm2 = (instr >> 6) & 0x3;
1051 uint32_t imm3 = (instr >> 12) & 0x7;
1052 uint32_t lsb = (imm3 << 2) | imm2;
1053 uint32_t width = msb - lsb + 1;
1054 if (Rn.r != 0xF) {
1055 opcode << "bfi";
1056 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1057 } else {
1058 opcode << "bfc";
1059 args << Rd << ", #" << lsb << ", #" << width;
1060 }
1061 break;
1062 }
Ian Rogers40627db2012-03-04 17:31:09 -08001063 default:
1064 break;
1065 }
1066 } else {
1067 // Branches and miscellaneous control
1068 // |111|11|1000000|0000|1|111|1100|00000000|
1069 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1070 // |---|--|-------|----|-|---|----|--------|
1071 // |332|22|2222222|1111|1|111|1100|00000000|
1072 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1073 // |---|--|-------|----|-|---|----|--------|
1074 // |111|10| op2 | |1|op3|op4 | |
1075
1076 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001077 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001078 switch (op3) {
1079 case 0:
1080 if ((op2 & 0x38) != 0x38) {
1081 // Conditional branch
1082 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1083 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1084 // |---|--|-|----|------|-|-|--|-|--|-----------|
1085 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1086 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1087 // |---|--|-|----|------|-|-|--|-|--|-----------|
1088 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1089 uint32_t S = (instr >> 26) & 1;
1090 uint32_t J2 = (instr >> 11) & 1;
1091 uint32_t J1 = (instr >> 13) & 1;
1092 uint32_t imm6 = (instr >> 16) & 0x3F;
1093 uint32_t imm11 = instr & 0x7FF;
1094 uint32_t cond = (instr >> 22) & 0xF;
1095 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1096 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001097 opcode << "b";
1098 DumpCond(opcode, cond);
1099 opcode << ".w";
1100 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001101 } else if (op2 == 0x3B) {
1102 // Miscellaneous control instructions
1103 uint32_t op5 = (instr >> 4) & 0xF;
1104 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001105 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1106 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1107 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001108 }
Ian Rogers40627db2012-03-04 17:31:09 -08001109 }
1110 break;
1111 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001112 if ((op2 & 0x38) == 0x38) {
1113 if (op2 == 0x7F) {
1114 opcode << "udf";
1115 }
1116 break;
1117 }
1118 // Else deliberate fall-through to B.
1119 case 1: case 3: {
1120 // B
1121 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1122 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1123 // |---|--|-|----|------|--|--|-|--|-----------|
1124 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1125 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1126 // |---|--|-|----|------|--|--|-|--|-----------|
1127 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1128 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1129 uint32_t S = (instr >> 26) & 1;
1130 uint32_t cond = (instr >> 22) & 0xF;
1131 uint32_t J2 = (instr >> 11) & 1;
1132 uint32_t form = (instr >> 12) & 1;
1133 uint32_t J1 = (instr >> 13) & 1;
1134 uint32_t imm10 = (instr >> 16) & 0x3FF;
1135 uint32_t imm6 = (instr >> 16) & 0x3F;
1136 uint32_t imm11 = instr & 0x7FF;
1137 opcode << "b";
1138 int32_t imm32;
1139 if (form == 0) {
1140 DumpCond(opcode, cond);
1141 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1142 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1143 } else {
1144 uint32_t I1 = ~(J1 ^ S);
1145 uint32_t I2 = ~(J2 ^ S);
1146 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1147 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1148 }
1149 opcode << ".w";
1150 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001151 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001152 }
Ian Rogers40627db2012-03-04 17:31:09 -08001153 case 4: case 6: case 5: case 7: {
1154 // BL, BLX (immediate)
1155 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1156 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1157 // |---|--|-|----------|--|--|-|--|-----------|
1158 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1159 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1160 // |---|--|-|----------|--|--|-|--|-----------|
1161 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
1162 uint32_t S = (instr >> 26) & 1;
1163 uint32_t J2 = (instr >> 11) & 1;
1164 uint32_t L = (instr >> 12) & 1;
1165 uint32_t J1 = (instr >> 13) & 1;
1166 uint32_t imm10 = (instr >> 16) & 0x3FF;
1167 uint32_t imm11 = instr & 0x7FF;
1168 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001169 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -08001170 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001171 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001172 }
1173 uint32_t I1 = ~(J1 ^ S);
1174 uint32_t I2 = ~(J2 ^ S);
1175 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1176 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001177 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001178 break;
1179 }
1180 }
1181 }
1182 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001183 case 3:
1184 switch (op2) {
1185 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
1186 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
1187 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001188 // |111|11|100|000|0|0000|1111|110000|000000|
1189 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1190 // |---|--|---|---|-|----|----|------|------|
1191 // |332|22|222|222|2|1111|1111|110000|000000|
1192 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1193 // |---|--|---|---|-|----|----|------|------|
1194 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001195 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001196 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001197 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001198 case 0x0: case 0x4: {
1199 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1200 // 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 -07001201 ArmRegister Rn(instr, 16);
1202 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -07001203 opcode << "strb";
1204 if ((instr & 0x800) != 0) {
1205 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001206 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001207 } else {
1208 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001209 ArmRegister Rm(instr, 0);
1210 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001211 if (imm2 != 0) {
1212 args << ", " << "lsl #" << imm2;
1213 }
1214 args << "]";
1215 }
1216 break;
1217 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001218 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001219 ArmRegister Rn(instr, 16);
1220 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001221 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001222 if ((instr & 0x800) != 0) {
1223 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1224 uint32_t P = (instr >> 10) & 1;
1225 uint32_t U = (instr >> 9) & 1;
1226 uint32_t W = (instr >> 8) & 1;
1227 uint32_t imm8 = instr & 0xFF;
1228 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1229 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1230 opcode << "push";
1231 args << Rt;
1232 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1233 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001234 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001235 if (P == 1 && U == 1 && W == 0) {
1236 opcode << "strt";
1237 } else {
1238 opcode << "str";
1239 }
1240 args << Rt << ", [" << Rn;
1241 if (P == 0 && W == 1) {
1242 args << "], #" << imm32;
1243 } else {
1244 args << ", #" << imm32 << "]";
1245 if (W == 1) {
1246 args << "!";
1247 }
Ian Rogers40627db2012-03-04 17:31:09 -08001248 }
1249 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001250 } else {
1251 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1252 ArmRegister Rn(instr, 16);
1253 ArmRegister Rt(instr, 12);
1254 ArmRegister Rm(instr, 0);
1255 uint32_t imm2 = (instr >> 4) & 3;
1256 opcode << "str.w";
1257 args << Rt << ", [" << Rn << ", " << Rm;
1258 if (imm2 != 0) {
1259 args << ", lsl #" << imm2;
1260 }
1261 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001262 }
1263 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001264 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001265 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001266 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001267 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001268 }
Ian Rogers40627db2012-03-04 17:31:09 -08001269 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001270 }
1271 }
1272
1273 break;
1274 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001275 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -08001276 // Load halfword
1277 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1278 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1279 // |---|--|--|---|--|-|----|----|------|------|
1280 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1281 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1282 // |---|--|--|---|--|-|----|----|------|------|
1283 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1284 // |111|11| op2 | | | imm12 |
1285 uint32_t op3 = (instr >> 23) & 3;
1286 ArmRegister Rn(instr, 16);
1287 ArmRegister Rt(instr, 12);
1288 if (Rt.r != 15) {
1289 if (op3 == 1) {
1290 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1291 uint32_t imm12 = instr & 0xFFF;
1292 opcode << "ldrh.w";
1293 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1294 if (Rn.r == 9) {
1295 args << " ; ";
1296 Thread::DumpThreadOffset(args, imm12, 4);
1297 } else if (Rn.r == 15) {
1298 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1299 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1300 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1301 }
1302 } else if (op3 == 3) {
1303 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
1304 uint32_t imm12 = instr & 0xFFF;
1305 opcode << "ldrsh.w";
1306 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1307 if (Rn.r == 9) {
1308 args << " ; ";
1309 Thread::DumpThreadOffset(args, imm12, 4);
1310 } else if (Rn.r == 15) {
1311 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1312 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1313 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1314 }
1315 }
1316 }
1317 break;
1318 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001319 case 0x29: { // 0101001
1320 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1321 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1322 // |---|--|-------|----|----|----|--|---|----|
1323 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1324 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1325 // |---|--|-------|----|----|----|--|---|----|
1326 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1327 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1328 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1329 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1330 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1331 if ((instr & 0xf0c0) == 0xf080) {
1332 uint32_t op3 = (instr >> 4) & 3;
1333 opcode << kThumbReverseOperations[op3];
1334 ArmRegister Rm(instr, 0);
1335 ArmRegister Rd(instr, 8);
1336 args << Rd << ", " << Rm;
1337 ArmRegister Rm2(instr, 16);
1338 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1339 args << " (UNPREDICTABLE)";
1340 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001341 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001342 break;
1343 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001344 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345 // Load word
1346 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1347 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1348 // |---|--|--|---|--|-|----|----|------|------|
1349 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1350 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1351 // |---|--|--|---|--|-|----|----|------|------|
1352 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1353 // |111|11| op2 | | | imm12 |
1354 uint32_t op3 = (instr >> 23) & 3;
1355 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001356 ArmRegister Rn(instr, 16);
1357 ArmRegister Rt(instr, 12);
1358 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001359 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1360 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1361 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001362 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001363 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001364 if (Rn.r == 9) {
1365 args << " ; ";
1366 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001367 } else if (Rn.r == 15) {
1368 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1369 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1370 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001371 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001372 } else if (op4 == 0) {
1373 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1374 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001375 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001376 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001377 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001378 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001379 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001380 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001381 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001382 } else {
1383 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1384 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001385 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001386 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001387 }
1388 break;
1389 }
Dave Allison70202782013-10-22 17:52:19 -07001390 default: // more formats
1391 if ((op2 >> 4) == 2) { // 010xxxx
1392 // data processing (register)
1393 } else if ((op2 >> 3) == 6) { // 0110xxx
1394 // Multiply, multiply accumulate, and absolute difference
1395 op1 = (instr >> 20) & 0x7;
1396 op2 = (instr >> 4) & 0x2;
1397 ArmRegister Ra(instr, 12);
1398 ArmRegister Rn(instr, 16);
1399 ArmRegister Rm(instr, 0);
1400 ArmRegister Rd(instr, 8);
1401 switch (op1) {
1402 case 0:
1403 if (op2 == 0) {
1404 if (Ra.r == 0xf) {
1405 opcode << "mul";
1406 args << Rd << ", " << Rn << ", " << Rm;
1407 } else {
1408 opcode << "mla";
1409 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1410 }
1411 } else {
1412 opcode << "mls";
1413 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1414 }
1415 break;
1416 case 1:
1417 case 2:
1418 case 3:
1419 case 4:
1420 case 5:
1421 case 6:
1422 break; // do these sometime
1423 }
1424 } else if ((op2 >> 3) == 7) { // 0111xxx
1425 // Long multiply, long multiply accumulate, and divide
1426 op1 = (instr >> 20) & 0x7;
1427 op2 = (instr >> 4) & 0xf;
1428 ArmRegister Rn(instr, 16);
1429 ArmRegister Rm(instr, 0);
1430 ArmRegister Rd(instr, 8);
1431 ArmRegister RdHi(instr, 8);
1432 ArmRegister RdLo(instr, 12);
1433 switch (op1) {
1434 case 0:
1435 opcode << "smull";
1436 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1437 break;
1438 case 1:
1439 opcode << "sdiv";
1440 args << Rd << ", " << Rn << ", " << Rm;
1441 break;
1442 case 2:
1443 opcode << "umull";
1444 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1445 break;
1446 case 3:
1447 opcode << "udiv";
1448 args << Rd << ", " << Rn << ", " << Rm;
1449 break;
1450 case 4:
1451 case 5:
1452 case 6:
1453 break; // TODO: when we generate these...
1454 }
1455 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001456 }
1457 default:
1458 break;
1459 }
Ian Rogers9af89402012-09-07 11:29:35 -07001460
1461 // Apply any IT-block conditions to the opcode if necessary.
1462 if (!it_conditions_.empty()) {
1463 opcode << it_conditions_.back();
1464 it_conditions_.pop_back();
1465 }
1466
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001467 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001468 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001469} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001470
1471size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1472 uint16_t instr = ReadU16(instr_ptr);
1473 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1474 if (is_32bit) {
1475 return DumpThumb32(os, instr_ptr);
1476 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001477 std::ostringstream opcode;
1478 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001479 uint16_t opcode1 = instr >> 10;
1480 if (opcode1 < 0x10) {
1481 // shift (immediate), add, subtract, move, and compare
1482 uint16_t opcode2 = instr >> 9;
1483 switch (opcode2) {
1484 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1485 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001486 // Logical shift left - 00 000xx iii mmm ddd
1487 // Logical shift right - 00 001xx iii mmm ddd
1488 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001489 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001490 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001491 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001492 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001493 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001494 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001495 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001496 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001497 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001498 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001499 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001500 break;
1501 }
1502 case 0xC: case 0xD: case 0xE: case 0xF: {
1503 // Add register - 00 01100 mmm nnn ddd
1504 // Sub register - 00 01101 mmm nnn ddd
1505 // Add 3-bit immediate - 00 01110 iii nnn ddd
1506 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1507 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001508 ThumbRegister Rn(instr, 3);
1509 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001510 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001511 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001512 } else {
1513 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001514 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001515 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001516 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001517 }
1518 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001519 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001520 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001521 ArmRegister Rm(imm3_or_Rm);
1522 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001523 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001524 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001525 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001526 break;
1527 }
1528 case 0x10: case 0x11: case 0x12: case 0x13:
1529 case 0x14: case 0x15: case 0x16: case 0x17:
1530 case 0x18: case 0x19: case 0x1A: case 0x1B:
1531 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1532 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1533 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1534 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1535 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001536 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001537 uint16_t imm8 = instr & 0xFF;
1538 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001539 case 4: opcode << "movs"; break;
1540 case 5: opcode << "cmp"; break;
1541 case 6: opcode << "adds"; break;
1542 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001543 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001544 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001545 break;
1546 }
1547 default:
1548 break;
1549 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001550 } else if (opcode1 == 0x10) {
1551 // Data-processing
1552 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001553 ThumbRegister rm(instr, 3);
1554 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001555 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001556 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001557 } else if (opcode1 == 0x11) {
1558 // Special data instructions and branch and exchange
1559 uint16_t opcode2 = (instr >> 6) & 0x0F;
1560 switch (opcode2) {
1561 case 0x0: case 0x1: case 0x2: case 0x3: {
1562 // Add low registers - 010001 0000 xxxxxx
1563 // Add high registers - 010001 0001/001x xxxxxx
1564 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001565 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001566 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001567 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001568 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001569 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001570 break;
1571 }
1572 case 0x8: case 0x9: case 0xA: case 0xB: {
1573 // Move low registers - 010001 1000 xxxxxx
1574 // Move high registers - 010001 1001/101x xxxxxx
1575 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001576 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001577 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001578 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001579 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001580 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001581 break;
1582 }
1583 case 0x5: case 0x6: case 0x7: {
1584 // Compare high registers - 010001 0101/011x xxxxxx
1585 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001586 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001587 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001588 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001589 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001590 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001591 break;
1592 }
1593 case 0xC: case 0xD: case 0xE: case 0xF: {
1594 // Branch and exchange - 010001 110x xxxxxx
1595 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001596 ArmRegister rm(instr, 3);
1597 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1598 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001599 break;
1600 }
1601 default:
1602 break;
1603 }
jeffhaoeae26912013-01-28 16:29:54 -08001604 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1605 ThumbRegister Rt(instr, 8);
1606 uint16_t imm8 = instr & 0xFF;
1607 opcode << "ldr";
1608 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001609 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1610 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1611 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1612 // Load/store single data item
1613 uint16_t opA = (instr >> 12) & 0xF;
1614 if (opA == 0x5) {
1615 uint16_t opB = (instr >> 9) & 0x7;
1616 ThumbRegister Rm(instr, 6);
1617 ThumbRegister Rn(instr, 3);
1618 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001619 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001620 case 0: opcode << "str"; break;
1621 case 1: opcode << "strh"; break;
1622 case 2: opcode << "strb"; break;
1623 case 3: opcode << "ldrsb"; break;
1624 case 4: opcode << "ldr"; break;
1625 case 5: opcode << "ldrh"; break;
1626 case 6: opcode << "ldrb"; break;
1627 case 7: opcode << "ldrsh"; break;
1628 }
1629 args << Rt << ", [" << Rn << ", " << Rm << "]";
1630 } else if (opA == 9) {
1631 uint16_t opB = (instr >> 11) & 1;
1632 ThumbRegister Rt(instr, 8);
1633 uint16_t imm8 = instr & 0xFF;
1634 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001635 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001636 } else {
1637 uint16_t imm5 = (instr >> 6) & 0x1F;
1638 uint16_t opB = (instr >> 11) & 1;
1639 ThumbRegister Rn(instr, 3);
1640 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001641 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001642 case 6:
1643 imm5 <<= 2;
1644 opcode << (opB == 0 ? "str" : "ldr");
1645 break;
1646 case 7:
1647 imm5 <<= 0;
1648 opcode << (opB == 0 ? "strb" : "ldrb");
1649 break;
1650 case 8:
1651 imm5 <<= 1;
1652 opcode << (opB == 0 ? "strh" : "ldrh");
1653 break;
1654 }
1655 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1656 }
jeffhaoeae26912013-01-28 16:29:54 -08001657 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001658 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001659 uint32_t cond = (instr >> 8) & 0xF;
1660 opcode << "b";
1661 DumpCond(opcode, cond);
1662 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001663 } else if ((instr & 0xF800) == 0xA800) {
1664 // Generate SP-relative address
1665 ThumbRegister rd(instr, 8);
1666 int imm8 = instr & 0xFF;
1667 opcode << "add";
1668 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001669 } else if ((instr & 0xF000) == 0xB000) {
1670 // Miscellaneous 16-bit instructions
1671 uint16_t opcode2 = (instr >> 5) & 0x7F;
1672 switch (opcode2) {
1673 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1674 // Add immediate to SP - 1011 00000 ii iiiii
1675 // Subtract immediate from SP - 1011 00001 ii iiiii
1676 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001677 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001678 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001679 break;
1680 }
Ian Rogers087b2412012-03-21 01:30:32 -07001681 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001682 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001683 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1684 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001685 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001686 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1687 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1688 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001689 // CBNZ, CBZ
1690 uint16_t op = (instr >> 11) & 1;
1691 uint16_t i = (instr >> 9) & 1;
1692 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001693 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001694 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001695 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001696 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001697 DumpBranchTarget(args, instr_ptr + 4, imm32);
1698 break;
1699 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001700 case 0x50: case 0x51: // 101000x
1701 case 0x52: case 0x53: // 101001x
1702 case 0x56: case 0x57: { // 101011x
1703 uint16_t op = (instr >> 6) & 3;
1704 opcode << kThumbReverseOperations[op];
1705 ThumbRegister Rm(instr, 3);
1706 ThumbRegister Rd(instr, 0);
1707 args << Rd << ", " << Rm;
1708 break;
1709 }
Ian Rogers40627db2012-03-04 17:31:09 -08001710 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1711 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1712 // If-Then, and hints
1713 uint16_t opA = (instr >> 4) & 0xF;
1714 uint16_t opB = instr & 0xF;
1715 if (opB == 0) {
1716 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001717 case 0: opcode << "nop"; break;
1718 case 1: opcode << "yield"; break;
1719 case 2: opcode << "wfe"; break;
1720 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001721 default: break;
1722 }
1723 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001724 uint32_t first_cond = opA;
1725 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001726 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001727
1728 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1729 // and store up the actual condition codes we'll want to add to the next few opcodes.
1730 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001731 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001732 for (size_t i = 0; i < count; ++i) {
1733 bool positive_cond = ((first_cond & 1) != 0);
1734 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1735 if (positive_mask == positive_cond) {
1736 opcode << 't';
1737 it_conditions_[i] = kConditionCodeNames[first_cond];
1738 } else {
1739 opcode << 'e';
1740 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1741 }
1742 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001743 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001744
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001745 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1746 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001747 }
1748 break;
1749 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001750 default:
1751 break;
1752 }
1753 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1754 ((instr & 0xE000) == 0x8000)) {
1755 // Load/store single data item
1756 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001757 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001758 switch (opA) {
1759 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001760 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1761 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001762 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001763 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001764 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001765 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1766 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001767 break;
1768 }
1769 case 0x9: {
1770 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1771 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1772 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001773 ThumbRegister Rt(instr, 8);
1774 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1775 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001776 break;
1777 }
1778 default:
1779 break;
1780 }
Ian Rogers40627db2012-03-04 17:31:09 -08001781 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1782 uint16_t imm11 = instr & 0x7FFF;
1783 int32_t imm32 = imm11 << 1;
1784 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001785 opcode << "b";
1786 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001787 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001788
1789 // Apply any IT-block conditions to the opcode if necessary.
1790 if (!it_conditions_.empty()) {
1791 opcode << it_conditions_.back();
1792 it_conditions_.pop_back();
1793 }
1794
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001796 }
1797 return 2;
1798}
1799
1800} // namespace arm
1801} // namespace art