blob: 2a966a3512db006ab8924205afecfc1762779b05 [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);
Brian Carlstromc2687ef2014-03-13 15:12:11 -0700723 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (2)));
724 args << StringPrintf(" ; 0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
Ian Rogersef6a7762013-12-19 17:58:05 -0800725 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000726 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
727 opcode << (L == 1 ? "vpop" : "vpush");
728 args << FpRegisterRange(instr);
729 } else { // VLDM
730 opcode << (L == 1 ? "vldm" : "vstm");
731 args << Rn << ((W == 1) ? "!" : "") << ", "
732 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700733 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000734 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700735 }
Dave Allison70202782013-10-22 17:52:19 -0700736 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000737 if ((instr & 0xD0) == 0x10) {
738 // 64bit transfers between ARM core and extension registers.
739 uint32_t L = (instr >> 20) & 1;
740 uint32_t S = (instr >> 8) & 1;
741 ArmRegister Rt2(instr, 16);
742 ArmRegister Rt(instr, 12);
743 FpRegister m(instr, 0, 5);
744 opcode << "vmov" << (S ? ".f64" : ".f32");
745 if (L == 1) {
746 args << Rt << ", " << Rt2 << ", ";
747 }
748 if (S) {
749 args << m;
750 } else {
751 args << m << ", " << FpRegister(m, 1);
752 }
753 if (L == 0) {
754 args << ", " << Rt << ", " << Rt2;
755 }
756 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
757 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
758 args << " (UNPREDICTABLE)";
759 }
760 }
Dave Allison70202782013-10-22 17:52:19 -0700761 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
762 // fp data processing
Zheng Xue19649a2014-02-27 13:30:55 +0000763 if ((op3 & 0xB) == 0) { // 100x00
764 // VMLA, VMLS
765 // |1111|1100|0|0|00|0000|1111|110|0|0|0 |0|0|0000|
766 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6 |5|4|3 0|
767 // |----|----|-|-|--|----|----|---|-|-|- |-|-|----|
768 // |3322|2222|2|2|22|1111|1111|110|0|0|0 |0|0|0000|
769 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6 |5|4|3 0|
770 // |----|----|-|-|--|----|----|---|-|-|- |-|-|----|
771 // |1110|1110|0|D|00| Vn | Vd |101|S|N|op|M|0| Vm |
772 uint32_t op = (instr >> 6) & 1;
773 FpRegister d(instr, 12, 22);
774 FpRegister n(instr, 16, 7);
775 FpRegister m(instr, 0, 5);
776 opcode << (op == 0 ? "vmla" : "vmls");
777 args << d << ", " << n << ", " << m;
778 } else if ((op3 & 0xB) == 0xB) { // 101x11
779 uint32_t Q = (instr >> 6) & 1;
780 if (Q == 1) {
781 // VCVT (floating-point conversion)
782 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
783 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
784 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
785 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
786 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
787 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
788 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm |
789 uint32_t op5 = (instr >> 16) & 0xF;
790 uint32_t S = (instr >> 8) & 1;
791 uint32_t op = (instr >> 7) & 1;
792 // Register types in these instructions relies on the combination of op5 and S.
793 FpRegister Dd(instr, 12, 22, 1);
794 FpRegister Sd(instr, 12, 22, 0);
795 FpRegister Dm(instr, 0, 5, 1);
796 FpRegister Sm(instr, 0, 5, 0);
797 if (op5 == 0xD) {
798 if (S == 1) {
799 // vcvt{r}.s32.f64
800 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
801 args << Sd << ", " << Dm;
802 } else {
803 // vcvt{r}.s32.f32
804 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
805 args << Sd << ", " << Sm;
806 }
807 } else if (op5 == 0xC) {
808 if (S == 1) {
809 // vcvt{r}.u32.f64
810 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
811 args << Sd << ", " << Dm;
812 } else {
813 // vcvt{r}.u32.f32
814 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
815 args << Sd << ", " << Sm;
816 }
817 } else if (op5 == 0x8) {
818 if (S == 1) {
819 // vcvt.f64.<Tm>
820 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
821 args << Dd << ", " << Sm;
822 } else {
823 // vcvt.f32.<Tm>
824 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
825 args << Sd << ", " << Sm;
826 }
827 } else if (op5 == 0x7) {
828 if (op == 1) {
829 if (S == 1) {
830 // vcvt.f64.f32
831 opcode << "vcvt.f64.f32";
832 args << Dd << ", " << Sm;
833 } else {
834 // vcvt.f32.f64
835 opcode << "vcvt.f32.f64";
836 args << Sd << ", " << Dm;
837 }
838 }
839 }
840 }
841 }
Dave Allison70202782013-10-22 17:52:19 -0700842 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000843 if (coproc == 10 && (op3 & 0xE) == 0) {
844 // VMOV (between ARM core register and single-precision register)
845 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
846 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
847 // |----|----|---|- |----|----|----|-|--|-|----|
848 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
849 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
850 // |----|----|---|- |----|----|----|-|--|-|----|
851 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
852 uint32_t op = op3 & 1;
853 ArmRegister Rt(instr, 12);
854 FpRegister n(instr, 16, 7);
855 opcode << "vmov.f32";
856 if (op) {
857 args << Rt << ", " << n;
858 } else {
859 args << n << ", " << Rt;
860 }
861 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
862 args << " (UNPREDICTABLE)";
863 }
864 } else if (coproc == 10 && op3 == 0x2F) {
865 // VMRS
866 // |1111|11000000|0000|1111|1100|000|0|0000|
867 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
868 // |----|--------|----|----|----|---|-|----|
869 // |3322|22222222|1111|1111|1100|000|0|0000|
870 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
871 // |----|--------|----|----|----|---|-|----|
872 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
873 uint32_t spec_reg = (instr >> 16) & 0xF;
874 ArmRegister Rt(instr, 12);
875 opcode << "vmrs";
876 if (spec_reg == 1) {
877 if (Rt.r == 15) {
878 args << "APSR_nzcv, FPSCR";
879 } else if (Rt.r == 13) {
880 args << Rt << ", FPSCR (UNPREDICTABLE)";
881 } else {
882 args << Rt << ", FPSCR";
883 }
884 } else {
885 args << "(PRIVILEGED)";
886 }
887 } else if (coproc == 11 && (op3 & 0x9) != 8) {
888 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
889 }
Ian Rogers9af89402012-09-07 11:29:35 -0700890 }
Dave Allison70202782013-10-22 17:52:19 -0700891 }
892
893 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700894 if ((coproc & 0xE) == 0xA) {
895 // VFP data-processing instructions
896 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
897 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
898 // |---|-|----|----|----|----|---|-|----|-|-|----|
899 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
900 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
901 // |---|-|----|----|----|----|---|-|----|-|-|----|
902 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
903 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
904 uint32_t opc1 = (instr >> 20) & 0xF;
905 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700906 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700907 if ((opc1 & 0xB) == 0xB) { // 1x11
908 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700909 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000910 FpRegister d(instr, 12, 22);
911 FpRegister m(instr, 0, 5);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700912 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700913 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700914 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000915 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (sz == 1 ? ".f64" : ".f32");
916 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700917 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700918 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700919 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000920 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (sz == 1 ? ".f64" : ".f32");
921 args << d << ", " << m;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700922 break;
923 }
924 }
925 }
926 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700927 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
928 // Advanced SIMD
929 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
930 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
931 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
Ian Rogers0183dd72012-09-17 23:06:51 -0700932 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000933 FpRegister d(instr, 12, 22);
934 FpRegister m(instr, 0, 5);
935 opcode << "vsqrt" << (sz == 1 ? ".f64" : ".f32");
936 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700937 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700938 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800939 }
940 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800941 case 2:
942 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
943 // Data-processing (modified immediate)
944 // |111|11|10|0000|0|0000|1|111|1100|00000000|
945 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
946 // |---|--|--|----|-|----|-|---|----|--------|
947 // |332|22|22|2222|2|1111|1|111|1100|00000000|
948 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
949 // |---|--|--|----|-|----|-|---|----|--------|
950 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
951 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800952 uint32_t i = (instr >> 26) & 1;
953 uint32_t op3 = (instr >> 21) & 0xF;
954 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700955 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800956 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700957 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800958 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800959 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
960 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
961 if (op3 == 0x2) {
962 opcode << "mov";
963 if (S == 1) {
964 opcode << "s";
965 }
966 opcode << ".w";
967 } else {
968 opcode << "mvn";
969 if (S == 1) {
970 opcode << "s";
971 }
972 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700973 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800974 } else if (Rd.r == 0xF && S == 1 &&
975 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
976 if (op3 == 0x0) {
977 opcode << "tst";
978 } else if (op3 == 0x4) {
979 opcode << "teq";
980 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +0000981 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800982 } else {
983 opcode << "cmp.w";
984 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700985 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800986 } else {
987 switch (op3) {
988 case 0x0: opcode << "and"; break;
989 case 0x1: opcode << "bic"; break;
990 case 0x2: opcode << "orr"; break;
991 case 0x3: opcode << "orn"; break;
992 case 0x4: opcode << "eor"; break;
993 case 0x8: opcode << "add"; break;
994 case 0xA: opcode << "adc"; break;
995 case 0xB: opcode << "sbc"; break;
996 case 0xD: opcode << "sub"; break;
997 case 0xE: opcode << "rsb"; break;
998 default: opcode << "UNKNOWN DPMI-" << op3; break;
999 }
1000 if (S == 1) {
1001 opcode << "s";
1002 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001003 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001004 }
Ian Rogers40627db2012-03-04 17:31:09 -08001005 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1006 // Data-processing (plain binary immediate)
1007 // |111|11|10|00000|0000|1|111110000000000|
1008 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1009 // |---|--|--|-----|----|-|---------------|
1010 // |332|22|22|22222|1111|1|111110000000000|
1011 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1012 // |---|--|--|-----|----|-|---------------|
1013 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1014 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001015 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001016 case 0x00: case 0x0A: {
1017 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001018 ArmRegister Rd(instr, 8);
1019 ArmRegister Rn(instr, 16);
1020 uint32_t i = (instr >> 26) & 1;
1021 uint32_t imm3 = (instr >> 12) & 0x7;
1022 uint32_t imm8 = instr & 0xFF;
1023 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1024 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001025 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001026 args << Rd << ", " << Rn << ", #" << imm12;
1027 } else {
1028 opcode << "adr";
1029 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001030 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001031 }
1032 break;
1033 }
Ian Rogers55019132013-02-08 01:05:23 -08001034 case 0x04: case 0x0C: {
1035 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001036 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001037 uint32_t i = (instr >> 26) & 1;
1038 uint32_t imm3 = (instr >> 12) & 0x7;
1039 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001040 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001041 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001042 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001043 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001044 break;
1045 }
jeffhaoeae26912013-01-28 16:29:54 -08001046 case 0x16: {
1047 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
1048 ArmRegister Rd(instr, 8);
1049 ArmRegister Rn(instr, 16);
1050 uint32_t msb = instr & 0x1F;
1051 uint32_t imm2 = (instr >> 6) & 0x3;
1052 uint32_t imm3 = (instr >> 12) & 0x7;
1053 uint32_t lsb = (imm3 << 2) | imm2;
1054 uint32_t width = msb - lsb + 1;
1055 if (Rn.r != 0xF) {
1056 opcode << "bfi";
1057 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1058 } else {
1059 opcode << "bfc";
1060 args << Rd << ", #" << lsb << ", #" << width;
1061 }
1062 break;
1063 }
Ian Rogers40627db2012-03-04 17:31:09 -08001064 default:
1065 break;
1066 }
1067 } else {
1068 // Branches and miscellaneous control
1069 // |111|11|1000000|0000|1|111|1100|00000000|
1070 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1071 // |---|--|-------|----|-|---|----|--------|
1072 // |332|22|2222222|1111|1|111|1100|00000000|
1073 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1074 // |---|--|-------|----|-|---|----|--------|
1075 // |111|10| op2 | |1|op3|op4 | |
1076
1077 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001078 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001079 switch (op3) {
1080 case 0:
1081 if ((op2 & 0x38) != 0x38) {
1082 // Conditional branch
1083 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1084 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1085 // |---|--|-|----|------|-|-|--|-|--|-----------|
1086 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1087 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1088 // |---|--|-|----|------|-|-|--|-|--|-----------|
1089 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1090 uint32_t S = (instr >> 26) & 1;
1091 uint32_t J2 = (instr >> 11) & 1;
1092 uint32_t J1 = (instr >> 13) & 1;
1093 uint32_t imm6 = (instr >> 16) & 0x3F;
1094 uint32_t imm11 = instr & 0x7FF;
1095 uint32_t cond = (instr >> 22) & 0xF;
1096 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1097 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001098 opcode << "b";
1099 DumpCond(opcode, cond);
1100 opcode << ".w";
1101 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001102 } else if (op2 == 0x3B) {
1103 // Miscellaneous control instructions
1104 uint32_t op5 = (instr >> 4) & 0xF;
1105 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001106 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1107 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1108 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001109 }
Ian Rogers40627db2012-03-04 17:31:09 -08001110 }
1111 break;
1112 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001113 if ((op2 & 0x38) == 0x38) {
1114 if (op2 == 0x7F) {
1115 opcode << "udf";
1116 }
1117 break;
1118 }
1119 // Else deliberate fall-through to B.
1120 case 1: case 3: {
1121 // B
1122 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1123 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1124 // |---|--|-|----|------|--|--|-|--|-----------|
1125 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1126 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1127 // |---|--|-|----|------|--|--|-|--|-----------|
1128 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1129 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1130 uint32_t S = (instr >> 26) & 1;
1131 uint32_t cond = (instr >> 22) & 0xF;
1132 uint32_t J2 = (instr >> 11) & 1;
1133 uint32_t form = (instr >> 12) & 1;
1134 uint32_t J1 = (instr >> 13) & 1;
1135 uint32_t imm10 = (instr >> 16) & 0x3FF;
1136 uint32_t imm6 = (instr >> 16) & 0x3F;
1137 uint32_t imm11 = instr & 0x7FF;
1138 opcode << "b";
1139 int32_t imm32;
1140 if (form == 0) {
1141 DumpCond(opcode, cond);
1142 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1143 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1144 } else {
1145 uint32_t I1 = ~(J1 ^ S);
1146 uint32_t I2 = ~(J2 ^ S);
1147 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1148 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1149 }
1150 opcode << ".w";
1151 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001152 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001153 }
Ian Rogers40627db2012-03-04 17:31:09 -08001154 case 4: case 6: case 5: case 7: {
1155 // BL, BLX (immediate)
1156 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1157 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1158 // |---|--|-|----------|--|--|-|--|-----------|
1159 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1160 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1161 // |---|--|-|----------|--|--|-|--|-----------|
1162 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
1163 uint32_t S = (instr >> 26) & 1;
1164 uint32_t J2 = (instr >> 11) & 1;
1165 uint32_t L = (instr >> 12) & 1;
1166 uint32_t J1 = (instr >> 13) & 1;
1167 uint32_t imm10 = (instr >> 16) & 0x3FF;
1168 uint32_t imm11 = instr & 0x7FF;
1169 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001170 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -08001171 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001172 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001173 }
1174 uint32_t I1 = ~(J1 ^ S);
1175 uint32_t I2 = ~(J2 ^ S);
1176 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1177 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001178 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001179 break;
1180 }
1181 }
1182 }
1183 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001184 case 3:
1185 switch (op2) {
1186 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
1187 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
1188 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001189 // |111|11|100|000|0|0000|1111|110000|000000|
1190 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1191 // |---|--|---|---|-|----|----|------|------|
1192 // |332|22|222|222|2|1111|1111|110000|000000|
1193 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1194 // |---|--|---|---|-|----|----|------|------|
1195 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001196 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001197 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001198 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001199 case 0x0: case 0x4: {
1200 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1201 // 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 -07001202 ArmRegister Rn(instr, 16);
1203 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -07001204 opcode << "strb";
1205 if ((instr & 0x800) != 0) {
1206 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001207 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001208 } else {
1209 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001210 ArmRegister Rm(instr, 0);
1211 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001212 if (imm2 != 0) {
1213 args << ", " << "lsl #" << imm2;
1214 }
1215 args << "]";
1216 }
1217 break;
1218 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001219 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001220 ArmRegister Rn(instr, 16);
1221 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001222 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001223 if ((instr & 0x800) != 0) {
1224 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1225 uint32_t P = (instr >> 10) & 1;
1226 uint32_t U = (instr >> 9) & 1;
1227 uint32_t W = (instr >> 8) & 1;
1228 uint32_t imm8 = instr & 0xFF;
1229 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1230 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1231 opcode << "push";
1232 args << Rt;
1233 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1234 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001235 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001236 if (P == 1 && U == 1 && W == 0) {
1237 opcode << "strt";
1238 } else {
1239 opcode << "str";
1240 }
1241 args << Rt << ", [" << Rn;
1242 if (P == 0 && W == 1) {
1243 args << "], #" << imm32;
1244 } else {
1245 args << ", #" << imm32 << "]";
1246 if (W == 1) {
1247 args << "!";
1248 }
Ian Rogers40627db2012-03-04 17:31:09 -08001249 }
1250 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001251 } else {
1252 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1253 ArmRegister Rn(instr, 16);
1254 ArmRegister Rt(instr, 12);
1255 ArmRegister Rm(instr, 0);
1256 uint32_t imm2 = (instr >> 4) & 3;
1257 opcode << "str.w";
1258 args << Rt << ", [" << Rn << ", " << Rm;
1259 if (imm2 != 0) {
1260 args << ", lsl #" << imm2;
1261 }
1262 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001263 }
1264 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001265 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001266 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001267 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001268 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001269 }
Ian Rogers40627db2012-03-04 17:31:09 -08001270 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001271 }
1272 }
1273
1274 break;
1275 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001276 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -08001277 // Load halfword
1278 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1279 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1280 // |---|--|--|---|--|-|----|----|------|------|
1281 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1282 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1283 // |---|--|--|---|--|-|----|----|------|------|
1284 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1285 // |111|11| op2 | | | imm12 |
1286 uint32_t op3 = (instr >> 23) & 3;
1287 ArmRegister Rn(instr, 16);
1288 ArmRegister Rt(instr, 12);
1289 if (Rt.r != 15) {
1290 if (op3 == 1) {
1291 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1292 uint32_t imm12 = instr & 0xFFF;
1293 opcode << "ldrh.w";
1294 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1295 if (Rn.r == 9) {
1296 args << " ; ";
1297 Thread::DumpThreadOffset(args, imm12, 4);
1298 } else if (Rn.r == 15) {
1299 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1300 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1301 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1302 }
1303 } else if (op3 == 3) {
1304 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
1305 uint32_t imm12 = instr & 0xFFF;
1306 opcode << "ldrsh.w";
1307 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1308 if (Rn.r == 9) {
1309 args << " ; ";
1310 Thread::DumpThreadOffset(args, imm12, 4);
1311 } else if (Rn.r == 15) {
1312 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1313 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1314 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1315 }
1316 }
1317 }
1318 break;
1319 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001320 case 0x29: { // 0101001
1321 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1322 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1323 // |---|--|-------|----|----|----|--|---|----|
1324 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1325 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1326 // |---|--|-------|----|----|----|--|---|----|
1327 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1328 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1329 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1330 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1331 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1332 if ((instr & 0xf0c0) == 0xf080) {
1333 uint32_t op3 = (instr >> 4) & 3;
1334 opcode << kThumbReverseOperations[op3];
1335 ArmRegister Rm(instr, 0);
1336 ArmRegister Rd(instr, 8);
1337 args << Rd << ", " << Rm;
1338 ArmRegister Rm2(instr, 16);
1339 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1340 args << " (UNPREDICTABLE)";
1341 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001342 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001343 break;
1344 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001345 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001346 // Load word
1347 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1348 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1349 // |---|--|--|---|--|-|----|----|------|------|
1350 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1351 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1352 // |---|--|--|---|--|-|----|----|------|------|
1353 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1354 // |111|11| op2 | | | imm12 |
1355 uint32_t op3 = (instr >> 23) & 3;
1356 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001357 ArmRegister Rn(instr, 16);
1358 ArmRegister Rt(instr, 12);
1359 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001360 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1361 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1362 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001363 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001364 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001365 if (Rn.r == 9) {
1366 args << " ; ";
1367 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001368 } else if (Rn.r == 15) {
1369 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1370 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1371 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001372 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001373 } else if (op4 == 0) {
1374 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1375 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001376 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001377 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001378 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001379 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001380 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001381 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001382 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001383 } else {
1384 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1385 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001386 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001387 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001388 }
1389 break;
1390 }
Dave Allison70202782013-10-22 17:52:19 -07001391 default: // more formats
1392 if ((op2 >> 4) == 2) { // 010xxxx
1393 // data processing (register)
1394 } else if ((op2 >> 3) == 6) { // 0110xxx
1395 // Multiply, multiply accumulate, and absolute difference
1396 op1 = (instr >> 20) & 0x7;
1397 op2 = (instr >> 4) & 0x2;
1398 ArmRegister Ra(instr, 12);
1399 ArmRegister Rn(instr, 16);
1400 ArmRegister Rm(instr, 0);
1401 ArmRegister Rd(instr, 8);
1402 switch (op1) {
1403 case 0:
1404 if (op2 == 0) {
1405 if (Ra.r == 0xf) {
1406 opcode << "mul";
1407 args << Rd << ", " << Rn << ", " << Rm;
1408 } else {
1409 opcode << "mla";
1410 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1411 }
1412 } else {
1413 opcode << "mls";
1414 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1415 }
1416 break;
1417 case 1:
1418 case 2:
1419 case 3:
1420 case 4:
1421 case 5:
1422 case 6:
1423 break; // do these sometime
1424 }
1425 } else if ((op2 >> 3) == 7) { // 0111xxx
1426 // Long multiply, long multiply accumulate, and divide
1427 op1 = (instr >> 20) & 0x7;
1428 op2 = (instr >> 4) & 0xf;
1429 ArmRegister Rn(instr, 16);
1430 ArmRegister Rm(instr, 0);
1431 ArmRegister Rd(instr, 8);
1432 ArmRegister RdHi(instr, 8);
1433 ArmRegister RdLo(instr, 12);
1434 switch (op1) {
1435 case 0:
1436 opcode << "smull";
1437 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1438 break;
1439 case 1:
1440 opcode << "sdiv";
1441 args << Rd << ", " << Rn << ", " << Rm;
1442 break;
1443 case 2:
1444 opcode << "umull";
1445 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1446 break;
1447 case 3:
1448 opcode << "udiv";
1449 args << Rd << ", " << Rn << ", " << Rm;
1450 break;
1451 case 4:
1452 case 5:
1453 case 6:
1454 break; // TODO: when we generate these...
1455 }
1456 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001457 }
1458 default:
1459 break;
1460 }
Ian Rogers9af89402012-09-07 11:29:35 -07001461
1462 // Apply any IT-block conditions to the opcode if necessary.
1463 if (!it_conditions_.empty()) {
1464 opcode << it_conditions_.back();
1465 it_conditions_.pop_back();
1466 }
1467
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001468 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001469 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001470} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001471
1472size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1473 uint16_t instr = ReadU16(instr_ptr);
1474 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1475 if (is_32bit) {
1476 return DumpThumb32(os, instr_ptr);
1477 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001478 std::ostringstream opcode;
1479 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001480 uint16_t opcode1 = instr >> 10;
1481 if (opcode1 < 0x10) {
1482 // shift (immediate), add, subtract, move, and compare
1483 uint16_t opcode2 = instr >> 9;
1484 switch (opcode2) {
1485 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1486 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001487 // Logical shift left - 00 000xx iii mmm ddd
1488 // Logical shift right - 00 001xx iii mmm ddd
1489 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001490 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001491 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001492 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001493 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001494 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001495 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001496 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001497 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001498 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001499 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001500 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001501 break;
1502 }
1503 case 0xC: case 0xD: case 0xE: case 0xF: {
1504 // Add register - 00 01100 mmm nnn ddd
1505 // Sub register - 00 01101 mmm nnn ddd
1506 // Add 3-bit immediate - 00 01110 iii nnn ddd
1507 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1508 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001509 ThumbRegister Rn(instr, 3);
1510 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001511 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001512 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001513 } else {
1514 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001515 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001516 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001517 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001518 }
1519 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001520 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001521 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001522 ArmRegister Rm(imm3_or_Rm);
1523 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001524 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001525 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001526 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001527 break;
1528 }
1529 case 0x10: case 0x11: case 0x12: case 0x13:
1530 case 0x14: case 0x15: case 0x16: case 0x17:
1531 case 0x18: case 0x19: case 0x1A: case 0x1B:
1532 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1533 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1534 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1535 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1536 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001537 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001538 uint16_t imm8 = instr & 0xFF;
1539 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001540 case 4: opcode << "movs"; break;
1541 case 5: opcode << "cmp"; break;
1542 case 6: opcode << "adds"; break;
1543 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001544 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001545 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001546 break;
1547 }
1548 default:
1549 break;
1550 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001551 } else if (opcode1 == 0x10) {
1552 // Data-processing
1553 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001554 ThumbRegister rm(instr, 3);
1555 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001556 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001557 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001558 } else if (opcode1 == 0x11) {
1559 // Special data instructions and branch and exchange
1560 uint16_t opcode2 = (instr >> 6) & 0x0F;
1561 switch (opcode2) {
1562 case 0x0: case 0x1: case 0x2: case 0x3: {
1563 // Add low registers - 010001 0000 xxxxxx
1564 // Add high registers - 010001 0001/001x xxxxxx
1565 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001566 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001567 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001568 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001569 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001570 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001571 break;
1572 }
1573 case 0x8: case 0x9: case 0xA: case 0xB: {
1574 // Move low registers - 010001 1000 xxxxxx
1575 // Move high registers - 010001 1001/101x xxxxxx
1576 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001577 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001578 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001579 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001580 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001581 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001582 break;
1583 }
1584 case 0x5: case 0x6: case 0x7: {
1585 // Compare high registers - 010001 0101/011x xxxxxx
1586 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001587 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001588 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001589 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001590 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001591 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001592 break;
1593 }
1594 case 0xC: case 0xD: case 0xE: case 0xF: {
1595 // Branch and exchange - 010001 110x xxxxxx
1596 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001597 ArmRegister rm(instr, 3);
1598 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1599 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001600 break;
1601 }
1602 default:
1603 break;
1604 }
jeffhaoeae26912013-01-28 16:29:54 -08001605 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1606 ThumbRegister Rt(instr, 8);
1607 uint16_t imm8 = instr & 0xFF;
1608 opcode << "ldr";
1609 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001610 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1611 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1612 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1613 // Load/store single data item
1614 uint16_t opA = (instr >> 12) & 0xF;
1615 if (opA == 0x5) {
1616 uint16_t opB = (instr >> 9) & 0x7;
1617 ThumbRegister Rm(instr, 6);
1618 ThumbRegister Rn(instr, 3);
1619 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001620 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001621 case 0: opcode << "str"; break;
1622 case 1: opcode << "strh"; break;
1623 case 2: opcode << "strb"; break;
1624 case 3: opcode << "ldrsb"; break;
1625 case 4: opcode << "ldr"; break;
1626 case 5: opcode << "ldrh"; break;
1627 case 6: opcode << "ldrb"; break;
1628 case 7: opcode << "ldrsh"; break;
1629 }
1630 args << Rt << ", [" << Rn << ", " << Rm << "]";
1631 } else if (opA == 9) {
1632 uint16_t opB = (instr >> 11) & 1;
1633 ThumbRegister Rt(instr, 8);
1634 uint16_t imm8 = instr & 0xFF;
1635 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001636 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001637 } else {
1638 uint16_t imm5 = (instr >> 6) & 0x1F;
1639 uint16_t opB = (instr >> 11) & 1;
1640 ThumbRegister Rn(instr, 3);
1641 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001642 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001643 case 6:
1644 imm5 <<= 2;
1645 opcode << (opB == 0 ? "str" : "ldr");
1646 break;
1647 case 7:
1648 imm5 <<= 0;
1649 opcode << (opB == 0 ? "strb" : "ldrb");
1650 break;
1651 case 8:
1652 imm5 <<= 1;
1653 opcode << (opB == 0 ? "strh" : "ldrh");
1654 break;
1655 }
1656 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1657 }
jeffhaoeae26912013-01-28 16:29:54 -08001658 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001659 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001660 uint32_t cond = (instr >> 8) & 0xF;
1661 opcode << "b";
1662 DumpCond(opcode, cond);
1663 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001664 } else if ((instr & 0xF800) == 0xA800) {
1665 // Generate SP-relative address
1666 ThumbRegister rd(instr, 8);
1667 int imm8 = instr & 0xFF;
1668 opcode << "add";
1669 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001670 } else if ((instr & 0xF000) == 0xB000) {
1671 // Miscellaneous 16-bit instructions
1672 uint16_t opcode2 = (instr >> 5) & 0x7F;
1673 switch (opcode2) {
1674 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1675 // Add immediate to SP - 1011 00000 ii iiiii
1676 // Subtract immediate from SP - 1011 00001 ii iiiii
1677 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001678 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001679 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001680 break;
1681 }
Ian Rogers087b2412012-03-21 01:30:32 -07001682 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001683 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001684 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1685 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001686 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001687 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1688 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1689 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001690 // CBNZ, CBZ
1691 uint16_t op = (instr >> 11) & 1;
1692 uint16_t i = (instr >> 9) & 1;
1693 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001694 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001695 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001696 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001697 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001698 DumpBranchTarget(args, instr_ptr + 4, imm32);
1699 break;
1700 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001701 case 0x50: case 0x51: // 101000x
1702 case 0x52: case 0x53: // 101001x
1703 case 0x56: case 0x57: { // 101011x
1704 uint16_t op = (instr >> 6) & 3;
1705 opcode << kThumbReverseOperations[op];
1706 ThumbRegister Rm(instr, 3);
1707 ThumbRegister Rd(instr, 0);
1708 args << Rd << ", " << Rm;
1709 break;
1710 }
Ian Rogers40627db2012-03-04 17:31:09 -08001711 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1712 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1713 // If-Then, and hints
1714 uint16_t opA = (instr >> 4) & 0xF;
1715 uint16_t opB = instr & 0xF;
1716 if (opB == 0) {
1717 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001718 case 0: opcode << "nop"; break;
1719 case 1: opcode << "yield"; break;
1720 case 2: opcode << "wfe"; break;
1721 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001722 default: break;
1723 }
1724 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001725 uint32_t first_cond = opA;
1726 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001727 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001728
1729 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1730 // and store up the actual condition codes we'll want to add to the next few opcodes.
1731 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001732 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001733 for (size_t i = 0; i < count; ++i) {
1734 bool positive_cond = ((first_cond & 1) != 0);
1735 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1736 if (positive_mask == positive_cond) {
1737 opcode << 't';
1738 it_conditions_[i] = kConditionCodeNames[first_cond];
1739 } else {
1740 opcode << 'e';
1741 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1742 }
1743 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001744 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001745
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001746 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1747 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001748 }
1749 break;
1750 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001751 default:
1752 break;
1753 }
1754 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1755 ((instr & 0xE000) == 0x8000)) {
1756 // Load/store single data item
1757 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001758 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001759 switch (opA) {
1760 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001761 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1762 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001763 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001764 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001765 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001766 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1767 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001768 break;
1769 }
1770 case 0x9: {
1771 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1772 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1773 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001774 ThumbRegister Rt(instr, 8);
1775 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1776 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001777 break;
1778 }
1779 default:
1780 break;
1781 }
Ian Rogers40627db2012-03-04 17:31:09 -08001782 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1783 uint16_t imm11 = instr & 0x7FFF;
1784 int32_t imm32 = imm11 << 1;
1785 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001786 opcode << "b";
1787 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001788 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001789
1790 // Apply any IT-block conditions to the opcode if necessary.
1791 if (!it_conditions_.empty()) {
1792 opcode << it_conditions_.back();
1793 it_conditions_.pop_back();
1794 }
1795
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001797 }
1798 return 2;
1799}
1800
1801} // namespace arm
1802} // namespace art