blob: 55fd52f080ea1b99330eb6ee58a36a9aa5e338d1 [file] [log] [blame]
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "disassembler_arm.h"
18
Ian Rogersef7d42f2014-01-06 12:55:46 -080019#include <inttypes.h>
20
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080021#include <iostream>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080024#include "base/stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070025#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070026
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080027namespace art {
28namespace arm {
29
Ian Rogersb23a7722012-10-09 16:54:26 -070030size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
31 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
32 DumpArm(os, begin);
33 return 4;
34 } else {
35 // remove thumb specifier bits
36 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
37 return DumpThumb16(os, begin);
38 }
39}
40
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080041void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
42 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
43 for (const uint8_t* cur = begin; cur < end; cur += 4) {
44 DumpArm(os, cur);
45 }
46 } else {
47 // remove thumb specifier bits
48 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
49 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
50 for (const uint8_t* cur = begin; cur < end;) {
51 cur += DumpThumb16(os, cur);
52 }
53 }
54}
55
Elliott Hughes77405792012-03-15 15:22:12 -070056static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070057 "eq", // 0000 - equal
58 "ne", // 0001 - not-equal
59 "cs", // 0010 - carry-set, greater than, equal or unordered
60 "cc", // 0011 - carry-clear, less than
61 "mi", // 0100 - minus, negative
62 "pl", // 0101 - plus, positive or zero
63 "vs", // 0110 - overflow
64 "vc", // 0111 - no overflow
65 "hi", // 1000 - unsigned higher
66 "ls", // 1001 - unsigned lower or same
67 "ge", // 1010 - signed greater than or equal
68 "lt", // 1011 - signed less than
69 "gt", // 1100 - signed greater than
70 "le", // 1101 - signed less than or equal
71 "", // 1110 - always
72 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080073};
74
75void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
76 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070077 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080078 } else {
79 os << "Unexpected condition: " << cond;
80 }
81}
82
Ian Rogersb122a4b2013-11-19 18:00:50 -080083void DisassemblerArm::DumpMemoryDomain(std::ostream& os, uint32_t domain) {
84 switch (domain) {
85 case 0b1111: os << "sy"; break;
86 case 0b1110: os << "st"; break;
87 case 0b1011: os << "ish"; break;
88 case 0b1010: os << "ishst"; break;
89 case 0b0111: os << "nsh"; break;
90 case 0b0110: os << "nshst"; break;
91 case 0b0011: os << "osh"; break;
92 case 0b0010: os << "oshst"; break;
93 }
94}
95
Ian Rogers40627db2012-03-04 17:31:09 -080096void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Elliott Hughes1ca98492012-04-12 17:21:02 -070097 os << StringPrintf("%+d (%p)", imm32, instr_ptr + imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080098}
99
100static uint32_t ReadU16(const uint8_t* ptr) {
101 return ptr[0] | (ptr[1] << 8);
102}
103
104static uint32_t ReadU32(const uint8_t* ptr) {
105 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
106}
107
Elliott Hughes77405792012-03-15 15:22:12 -0700108static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700109 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
110 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -0700111};
112
Ian Rogersad03ef52012-03-18 19:34:47 -0700113static const char* kThumbDataProcessingOperations[] = {
114 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
115 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
116};
117
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100118static const char* kThumbReverseOperations[] = {
119 "rev", "rev16", "rbit", "revsh"
120};
121
Elliott Hughes77405792012-03-15 15:22:12 -0700122struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -0700123 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700124 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -0700125 uint32_t r;
126};
127std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
128 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700129 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700130 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700131 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700132 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700133 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700134 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700135 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700136 }
137 return os;
138}
139
Elliott Hughes630e77d2012-03-22 19:20:56 -0700140struct ThumbRegister : ArmRegister {
141 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700142};
143
144struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700145 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700146 uint32_t shift;
147 ArmRegister rm;
148};
149std::ostream& operator<<(std::ostream& os, const Rm& r) {
150 os << r.rm;
151 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700152 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700153 }
154 return os;
155}
156
Elliott Hughes1ca98492012-04-12 17:21:02 -0700157struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700158 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700159 uint32_t rotate = ((instruction >> 8) & 0xf);
160 uint32_t imm = (instruction & 0xff);
161 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
162 }
163 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700164};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700165std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700166 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700167 return os;
168}
169
170struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700171 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700172 uint32_t register_list;
173};
174std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
175 if (rhs.register_list == 0) {
176 os << "<no register list?>";
177 return os;
178 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700179 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700180 bool first = true;
181 for (size_t i = 0; i < 16; i++) {
182 if ((rhs.register_list & (1 << i)) != 0) {
183 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700184 first = false;
185 } else {
186 os << ", ";
187 }
188 os << ArmRegister(i);
189 }
190 }
191 os << "}";
192 return os;
193}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800194
Vladimir Markodd577a32013-11-07 19:25:24 +0000195struct FpRegister {
196 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
197 size = (instr >> 8) & 1;
198 uint32_t Vn = (instr >> at_bit) & 0xF;
199 uint32_t N = (instr >> extra_at_bit) & 1;
200 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
201 }
Zheng Xue19649a2014-02-27 13:30:55 +0000202 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit,
203 uint32_t forced_size) {
204 size = forced_size;
205 uint32_t Vn = (instr >> at_bit) & 0xF;
206 uint32_t N = (instr >> extra_at_bit) & 1;
207 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
208 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000209 FpRegister(const FpRegister& other, uint32_t offset)
210 : size(other.size), r(other.r + offset) {}
211
212 uint32_t size; // 0 = f32, 1 = f64
213 uint32_t r;
214};
215std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
216 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
217}
218
219struct FpRegisterRange {
220 explicit FpRegisterRange(uint32_t instr)
221 : first(instr, 12, 22), imm8(instr & 0xFF) {}
222 FpRegister first;
223 uint32_t imm8;
224};
225std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
226 os << "{" << rhs.first;
227 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
228 if (count > 1) {
229 os << "-" << FpRegister(rhs.first, count - 1);
230 }
231 if (rhs.imm8 == 0) {
232 os << " (EMPTY)";
233 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
234 os << rhs.first << " (HALF)";
235 }
236 os << "}";
237 return os;
238}
239
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800240void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700241 uint32_t instruction = ReadU32(instr_ptr);
242 uint32_t cond = (instruction >> 28) & 0xf;
243 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700244 std::string opcode;
245 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700246 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700247 switch (op1) {
248 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700249 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700250 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700251 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700252 opcode = "bkpt";
253 uint32_t imm12 = (instruction >> 8) & 0xfff;
254 uint32_t imm4 = (instruction & 0xf);
255 args << '#' << ((imm12 << 4) | imm4);
256 break;
257 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700258 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700259 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700260 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700261 break;
262 }
263 bool i = (instruction & (1 << 25)) != 0;
264 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700265 uint32_t op = (instruction >> 21) & 0xf;
266 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700267 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700268 if (implicit_s) {
269 // Rd is unused (and not shown), and we don't show the 's' suffix either.
270 } else {
271 if (s) {
272 suffixes += 's';
273 }
274 args << ArmRegister(instruction, 12) << ", ";
275 }
Elliott Hughes77405792012-03-15 15:22:12 -0700276 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700277 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700278 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700279 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700280 }
281 }
282 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700283 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700284 {
285 bool p = (instruction & (1 << 24)) != 0;
286 bool b = (instruction & (1 << 22)) != 0;
287 bool w = (instruction & (1 << 21)) != 0;
288 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700289 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700290 args << ArmRegister(instruction, 12) << ", ";
291 ArmRegister rn(instruction, 16);
292 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700293 UNIMPLEMENTED(FATAL) << "literals";
294 } else {
295 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700296 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700297 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700298 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700299 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700300 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700301 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700302 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700303 } else {
304 LOG(FATAL) << p << " " << w;
305 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700306 if (rn.r == 9) {
307 args << " ; ";
Elliott Hughes1ca98492012-04-12 17:21:02 -0700308 Thread::DumpThreadOffset(args, offset, 4);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700309 }
Elliott Hughes77405792012-03-15 15:22:12 -0700310 }
311 }
312 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700313 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700314 {
315 bool p = (instruction & (1 << 24)) != 0;
316 bool u = (instruction & (1 << 23)) != 0;
317 bool w = (instruction & (1 << 21)) != 0;
318 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700319 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700320 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700321 }
322 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700323 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700324 {
325 bool bl = (instruction & (1 << 24)) != 0;
326 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700327 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700328 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700329 DumpBranchTarget(args, instr_ptr + 8, imm32);
330 }
331 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700332 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700333 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700334 break;
335 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700336 opcode += kConditionCodeNames[cond];
337 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700338 // TODO: a more complete ARM disassembler could generate wider opcodes.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800339 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800340}
341
Ian Rogersa9650dd2013-10-04 08:23:32 -0700342int32_t ThumbExpand(int32_t imm12) {
343 if ((imm12 & 0xC00) == 0) {
344 switch ((imm12 >> 8) & 3) {
345 case 0:
346 return imm12 & 0xFF;
347 case 1:
348 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
349 case 2:
350 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
351 default: // 3
352 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
353 (imm12 & 0xFF);
354 }
355 } else {
356 uint32_t val = 0x80 | (imm12 & 0x7F);
357 int32_t rotate = (imm12 >> 7) & 0x1F;
358 return (val >> rotate) | (val << (32 - rotate));
359 }
360}
361
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800362size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
363 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
364 // |111|1 1|1000000|0000|1111110000000000|
365 // |5 3|2 1|0987654|3 0|5 0 5 0|
366 // |---|---|-------|----|----------------|
367 // |332|2 2|2222222|1111|1111110000000000|
368 // |1 9|8 7|6543210|9 6|5 0 5 0|
369 // |---|---|-------|----|----------------|
370 // |111|op1| op2 | | |
371 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700372 if (op1 == 0) {
373 return DumpThumb16(os, instr_ptr);
374 }
375
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800376 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700377 std::ostringstream opcode;
378 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800379 switch (op1) {
380 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800381 break;
382 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700383 if ((op2 & 0x64) == 0) { // 00x x0xx
384 // |111|11|10|00|0|00|0000|1111110000000000|
385 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
386 // |---|--|--|--|-|--|----|----------------|
387 // |332|22|22|22|2|22|1111|1111110000000000|
388 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
389 // |---|--|--|--|-|--|----|----------------|
390 // |111|01|00|op|0|WL| Rn | |
391 // |111|01| op2 | | |
392 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
393 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
394 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
395 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
396 uint32_t op = (instr >> 23) & 3;
397 uint32_t W = (instr >> 21) & 1;
398 uint32_t L = (instr >> 20) & 1;
399 ArmRegister Rn(instr, 16);
400 if (op == 1 || op == 2) {
401 if (op == 1) {
402 if (L == 0) {
403 opcode << "stm";
404 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800405 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700406 if (Rn.r != 13) {
407 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700408 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700409 } else {
410 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800411 }
412 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700413 } else {
414 if (L == 0) {
415 if (Rn.r != 13) {
416 opcode << "stmdb";
417 args << Rn << (W == 0 ? "" : "!") << ", ";
418 } else {
419 opcode << "push";
420 }
421 } else {
422 opcode << "ldmdb";
423 args << Rn << (W == 0 ? "" : "!") << ", ";
424 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800425 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700426 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800427 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700428 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700429 uint32_t op3 = (instr >> 23) & 3;
430 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700431 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700432 ArmRegister Rn(instr, 16);
433 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700434 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700435 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700436 if ((op3 & 2) == 2) { // 1x
437 int W = (instr >> 21) & 1;
438 int U = (instr >> 23) & 1;
439 int P = (instr >> 24) & 1;
440
441 if ((op4 & 1) == 1) {
442 opcode << "ldrd";
443 } else {
444 opcode << "strd";
445 }
446 args << Rt << "," << Rd << ", [" << Rn;
447 const char *sign = U ? "+" : "-";
448 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000449 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700450 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000451 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700452 if (W == 1) {
453 args << "!";
454 }
455 }
456 } else { // 0x
457 switch (op4) {
458 case 0:
459 if (op3 == 0) { // op3 is 00, op4 is 00
460 opcode << "strex";
461 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000462 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
463 Rd.r == Rn.r || Rd.r == Rt.r) {
464 args << " (UNPREDICTABLE)";
465 }
Dave Allison70202782013-10-22 17:52:19 -0700466 } else { // op3 is 01, op4 is 00
467 // this is one of strexb, strexh or strexd
468 int op5 = (instr >> 4) & 0xf;
469 switch (op5) {
470 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700471 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000472 opcode << ((op5 == 4) ? "strexb" : "strexh");
473 Rd = ArmRegister(instr, 0);
474 args << Rd << ", " << Rt << ", [" << Rn << "]";
475 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
476 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
477 args << " (UNPREDICTABLE)";
478 }
Dave Allison70202782013-10-22 17:52:19 -0700479 break;
480 case 7:
481 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000482 ArmRegister Rt2 = Rd;
483 Rd = ArmRegister(instr, 0);
484 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
485 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
486 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
487 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
488 args << " (UNPREDICTABLE)";
489 }
Dave Allison70202782013-10-22 17:52:19 -0700490 break;
491 }
492 }
493 break;
494 case 1:
495 if (op3 == 0) { // op3 is 00, op4 is 01
496 opcode << "ldrex";
497 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000498 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
499 args << " (UNPREDICTABLE)";
500 }
Dave Allison70202782013-10-22 17:52:19 -0700501 } else { // op3 is 01, op4 is 01
502 // this is one of strexb, strexh or strexd
503 int op5 = (instr >> 4) & 0xf;
504 switch (op5) {
505 case 0:
506 opcode << "tbb";
507 break;
508 case 1:
509 opcode << "tbh";
510 break;
511 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700512 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000513 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
514 args << Rt << ", [" << Rn << "]";
515 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
516 args << " (UNPREDICTABLE)";
517 }
Dave Allison70202782013-10-22 17:52:19 -0700518 break;
519 case 7:
520 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000521 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
522 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
523 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
524 args << " (UNPREDICTABLE)";
525 }
Dave Allison70202782013-10-22 17:52:19 -0700526 break;
527 }
528 }
529 break;
530 case 2: // op3 is 0x, op4 is 10
531 case 3: // op3 is 0x, op4 is 11
532 if (op4 == 2) {
533 opcode << "strd";
534 } else {
535 opcode << "ldrd";
536 }
537 int W = (instr >> 21) & 1;
538 int U = (instr >> 23) & 1;
539 int P = (instr >> 24) & 1;
540
541 args << Rt << "," << Rd << ", [" << Rn;
542 const char *sign = U ? "+" : "-";
543 if (P == 0 && W == 1) {
544 args << "], #" << sign << imm8;
545 } else {
546 args << ", #" << sign << imm8 << "]";
547 if (W == 1) {
548 args << "!";
549 }
550 }
551 break;
552 }
553 }
554
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700555 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
556 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100557 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
558 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
559 // |---|----|----|-|----|----|----|--|--|----|
560 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
561 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
562 // |---|----|----|-|----|----|----|--|--|----|
563 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700564 uint32_t op3 = (instr >> 21) & 0xF;
565 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100566 uint32_t imm3 = ((instr >> 12) & 0x7);
567 uint32_t imm2 = ((instr >> 6) & 0x3);
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700568 uint32_t imm5 = ((imm3 << 2) | imm2);
569 uint32_t shift_type = ((instr >> 4) & 0x3);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700570 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100571 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700572 ArmRegister Rm(instr, 0);
573 switch (op3) {
574 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100575 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700576 opcode << "and";
577 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700578 if (S != 1U) {
579 opcode << "UNKNOWN TST-" << S;
580 break;
581 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700582 opcode << "tst";
583 S = 0; // don't print 's'
584 }
585 break;
586 case 0x1: opcode << "bic"; break;
587 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100588 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700589 opcode << "orr";
590 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100591 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700592 opcode << "mov";
593 }
594 break;
595 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100596 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700597 opcode << "orn";
598 } else {
599 opcode << "mvn";
600 }
601 break;
602 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100603 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700604 opcode << "eor";
605 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700606 if (S != 1U) {
607 opcode << "UNKNOWN TEQ-" << S;
608 break;
609 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700610 opcode << "teq";
611 S = 0; // don't print 's'
612 }
613 break;
614 case 0x6: opcode << "pkh"; break;
615 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100616 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700617 opcode << "add";
618 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700619 if (S != 1U) {
620 opcode << "UNKNOWN CMN-" << S;
621 break;
622 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700623 opcode << "cmn";
624 S = 0; // don't print 's'
625 }
626 break;
627 case 0xA: opcode << "adc"; break;
628 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100629 case 0xD:
630 if (Rd.r != 0xF) {
631 opcode << "sub";
632 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700633 if (S != 1U) {
634 opcode << "UNKNOWN CMP-" << S;
635 break;
636 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100637 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100638 S = 0; // don't print 's'
639 }
640 break;
641 case 0xE: opcode << "rsb"; break;
642 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700643 }
Ian Rogers087b2412012-03-21 01:30:32 -0700644
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700645 if (S == 1) {
646 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700647 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700648 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100649
650 if (Rd.r != 0xF) {
651 args << Rd << ", ";
652 }
653 if (Rn.r != 0xF) {
654 args << Rn << ", ";
655 }
656 args << Rm;
657
658 // Shift operand.
659 bool noShift = (imm5 == 0 && shift_type != 0x3);
660 if (!noShift) {
661 args << ", ";
662 switch (shift_type) {
663 case 0x0: args << "lsl"; break;
664 case 0x1: args << "lsr"; break;
665 case 0x2: args << "asr"; break;
666 case 0x3:
667 if (imm5 == 0) {
668 args << "rrx";
669 } else {
670 args << "ror";
671 }
672 break;
673 }
674 if (shift_type != 0x3 /* rrx */) {
Dmitriy Ivanov7d180cb2014-03-25 10:31:04 -0700675 args << StringPrintf(" #%d", (0 != imm5 || 0 == shift_type) ? imm5 : 32);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100676 }
677 }
678
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700679 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
680 // Co-processor instructions
681 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
682 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
683 // |---|-|--|------|----|----|----|---|---|----|
684 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
685 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
686 // |---|-|--|------|----|----|----|---|---|----|
687 // |111| |11| op3 | Rn | |copr| |op4| |
688 uint32_t op3 = (instr >> 20) & 0x3F;
689 uint32_t coproc = (instr >> 8) & 0xF;
690 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700691
Ian Rogersef6a7762013-12-19 17:58:05 -0800692 if (coproc == 0xA || coproc == 0xB) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000693 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
694 // Extension register load/store instructions
695 // |1111|110|00000|0000|1111|110|0|00000000|
696 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
697 // |----|---|-----|----|----|---|-|--------|
698 // |3322|222|22222|1111|1111|110|0|00000000|
699 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
700 // |----|---|-----|----|----|---|-|--------|
701 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700702 uint32_t P = (instr >> 24) & 1;
703 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700704 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000705 if (P == U && W == 1) {
706 opcode << "UNDEFINED";
707 } else {
708 uint32_t L = (instr >> 20) & 1;
709 uint32_t S = (instr >> 8) & 1;
710 ArmRegister Rn(instr, 16);
711 if (P == 1 && W == 0) { // VLDR
712 FpRegister d(instr, 12, 22);
713 uint32_t imm8 = instr & 0xFF;
714 opcode << (L == 1 ? "vldr" : "vstr");
715 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
716 << (imm8 << 2) << "]";
Ian Rogersef6a7762013-12-19 17:58:05 -0800717 if (Rn.r == 15 && U == 1) {
718 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
719 lit_adr = RoundDown(lit_adr, 4) + 4 + (imm8 << 2);
Brian Carlstromc2687ef2014-03-13 15:12:11 -0700720 typedef const int64_t unaligned_int64_t __attribute__ ((aligned (2)));
721 args << StringPrintf(" ; 0x%" PRIx64, *reinterpret_cast<unaligned_int64_t*>(lit_adr));
Ian Rogersef6a7762013-12-19 17:58:05 -0800722 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000723 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
724 opcode << (L == 1 ? "vpop" : "vpush");
725 args << FpRegisterRange(instr);
726 } else { // VLDM
727 opcode << (L == 1 ? "vldm" : "vstm");
728 args << Rn << ((W == 1) ? "!" : "") << ", "
729 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700730 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000731 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700732 }
Dave Allison70202782013-10-22 17:52:19 -0700733 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000734 if ((instr & 0xD0) == 0x10) {
735 // 64bit transfers between ARM core and extension registers.
736 uint32_t L = (instr >> 20) & 1;
737 uint32_t S = (instr >> 8) & 1;
738 ArmRegister Rt2(instr, 16);
739 ArmRegister Rt(instr, 12);
740 FpRegister m(instr, 0, 5);
741 opcode << "vmov" << (S ? ".f64" : ".f32");
742 if (L == 1) {
743 args << Rt << ", " << Rt2 << ", ";
744 }
745 if (S) {
746 args << m;
747 } else {
748 args << m << ", " << FpRegister(m, 1);
749 }
750 if (L == 0) {
751 args << ", " << Rt << ", " << Rt2;
752 }
753 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
754 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
755 args << " (UNPREDICTABLE)";
756 }
757 }
Dave Allison70202782013-10-22 17:52:19 -0700758 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
759 // fp data processing
Zheng Xue19649a2014-02-27 13:30:55 +0000760 if ((op3 & 0xB) == 0) { // 100x00
761 // VMLA, VMLS
762 // |1111|1100|0|0|00|0000|1111|110|0|0|0 |0|0|0000|
763 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6 |5|4|3 0|
764 // |----|----|-|-|--|----|----|---|-|-|- |-|-|----|
765 // |3322|2222|2|2|22|1111|1111|110|0|0|0 |0|0|0000|
766 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6 |5|4|3 0|
767 // |----|----|-|-|--|----|----|---|-|-|- |-|-|----|
768 // |1110|1110|0|D|00| Vn | Vd |101|S|N|op|M|0| Vm |
769 uint32_t op = (instr >> 6) & 1;
770 FpRegister d(instr, 12, 22);
771 FpRegister n(instr, 16, 7);
772 FpRegister m(instr, 0, 5);
773 opcode << (op == 0 ? "vmla" : "vmls");
774 args << d << ", " << n << ", " << m;
775 } else if ((op3 & 0xB) == 0xB) { // 101x11
776 uint32_t Q = (instr >> 6) & 1;
777 if (Q == 1) {
778 // VCVT (floating-point conversion)
779 // |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
780 // |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
781 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
782 // |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
783 // |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
784 // |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
785 // |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm |
786 uint32_t op5 = (instr >> 16) & 0xF;
787 uint32_t S = (instr >> 8) & 1;
788 uint32_t op = (instr >> 7) & 1;
789 // Register types in these instructions relies on the combination of op5 and S.
790 FpRegister Dd(instr, 12, 22, 1);
791 FpRegister Sd(instr, 12, 22, 0);
792 FpRegister Dm(instr, 0, 5, 1);
793 FpRegister Sm(instr, 0, 5, 0);
794 if (op5 == 0xD) {
795 if (S == 1) {
796 // vcvt{r}.s32.f64
797 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
798 args << Sd << ", " << Dm;
799 } else {
800 // vcvt{r}.s32.f32
801 opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
802 args << Sd << ", " << Sm;
803 }
804 } else if (op5 == 0xC) {
805 if (S == 1) {
806 // vcvt{r}.u32.f64
807 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
808 args << Sd << ", " << Dm;
809 } else {
810 // vcvt{r}.u32.f32
811 opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
812 args << Sd << ", " << Sm;
813 }
814 } else if (op5 == 0x8) {
815 if (S == 1) {
816 // vcvt.f64.<Tm>
817 opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
818 args << Dd << ", " << Sm;
819 } else {
820 // vcvt.f32.<Tm>
821 opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
822 args << Sd << ", " << Sm;
823 }
824 } else if (op5 == 0x7) {
825 if (op == 1) {
826 if (S == 1) {
827 // vcvt.f64.f32
828 opcode << "vcvt.f64.f32";
829 args << Dd << ", " << Sm;
830 } else {
831 // vcvt.f32.f64
832 opcode << "vcvt.f32.f64";
833 args << Sd << ", " << Dm;
834 }
835 }
836 }
837 }
838 }
Dave Allison70202782013-10-22 17:52:19 -0700839 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000840 if (coproc == 10 && (op3 & 0xE) == 0) {
841 // VMOV (between ARM core register and single-precision register)
842 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
843 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
844 // |----|----|---|- |----|----|----|-|--|-|----|
845 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
846 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
847 // |----|----|---|- |----|----|----|-|--|-|----|
848 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
849 uint32_t op = op3 & 1;
850 ArmRegister Rt(instr, 12);
851 FpRegister n(instr, 16, 7);
852 opcode << "vmov.f32";
853 if (op) {
854 args << Rt << ", " << n;
855 } else {
856 args << n << ", " << Rt;
857 }
858 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
859 args << " (UNPREDICTABLE)";
860 }
861 } else if (coproc == 10 && op3 == 0x2F) {
862 // VMRS
863 // |1111|11000000|0000|1111|1100|000|0|0000|
864 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
865 // |----|--------|----|----|----|---|-|----|
866 // |3322|22222222|1111|1111|1100|000|0|0000|
867 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
868 // |----|--------|----|----|----|---|-|----|
869 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
870 uint32_t spec_reg = (instr >> 16) & 0xF;
871 ArmRegister Rt(instr, 12);
872 opcode << "vmrs";
873 if (spec_reg == 1) {
874 if (Rt.r == 15) {
875 args << "APSR_nzcv, FPSCR";
876 } else if (Rt.r == 13) {
877 args << Rt << ", FPSCR (UNPREDICTABLE)";
878 } else {
879 args << Rt << ", FPSCR";
880 }
881 } else {
882 args << "(PRIVILEGED)";
883 }
884 } else if (coproc == 11 && (op3 & 0x9) != 8) {
885 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
886 }
Ian Rogers9af89402012-09-07 11:29:35 -0700887 }
Dave Allison70202782013-10-22 17:52:19 -0700888 }
889
890 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700891 if ((coproc & 0xE) == 0xA) {
892 // VFP data-processing instructions
893 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
894 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
895 // |---|-|----|----|----|----|---|-|----|-|-|----|
896 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
897 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
898 // |---|-|----|----|----|----|---|-|----|-|-|----|
899 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
900 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
901 uint32_t opc1 = (instr >> 20) & 0xF;
902 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700903 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700904 if ((opc1 & 0xB) == 0xB) { // 1x11
905 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700906 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000907 FpRegister d(instr, 12, 22);
908 FpRegister m(instr, 0, 5);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700909 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700910 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700911 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000912 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (sz == 1 ? ".f64" : ".f32");
913 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700914 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700915 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700916 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000917 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (sz == 1 ? ".f64" : ".f32");
918 args << d << ", " << m;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700919 break;
920 }
921 }
922 }
923 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700924 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
925 // Advanced SIMD
926 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
927 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
928 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
Ian Rogers0183dd72012-09-17 23:06:51 -0700929 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000930 FpRegister d(instr, 12, 22);
931 FpRegister m(instr, 0, 5);
932 opcode << "vsqrt" << (sz == 1 ? ".f64" : ".f32");
933 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700934 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700935 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800936 }
937 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800938 case 2:
939 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
940 // Data-processing (modified immediate)
941 // |111|11|10|0000|0|0000|1|111|1100|00000000|
942 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
943 // |---|--|--|----|-|----|-|---|----|--------|
944 // |332|22|22|2222|2|1111|1|111|1100|00000000|
945 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
946 // |---|--|--|----|-|----|-|---|----|--------|
947 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
948 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800949 uint32_t i = (instr >> 26) & 1;
950 uint32_t op3 = (instr >> 21) & 0xF;
951 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700952 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800953 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700954 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800955 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800956 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
957 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
958 if (op3 == 0x2) {
959 opcode << "mov";
960 if (S == 1) {
961 opcode << "s";
962 }
963 opcode << ".w";
964 } else {
965 opcode << "mvn";
966 if (S == 1) {
967 opcode << "s";
968 }
969 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700970 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800971 } else if (Rd.r == 0xF && S == 1 &&
972 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
973 if (op3 == 0x0) {
974 opcode << "tst";
975 } else if (op3 == 0x4) {
976 opcode << "teq";
977 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +0000978 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800979 } else {
980 opcode << "cmp.w";
981 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700982 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800983 } else {
984 switch (op3) {
985 case 0x0: opcode << "and"; break;
986 case 0x1: opcode << "bic"; break;
987 case 0x2: opcode << "orr"; break;
988 case 0x3: opcode << "orn"; break;
989 case 0x4: opcode << "eor"; break;
990 case 0x8: opcode << "add"; break;
991 case 0xA: opcode << "adc"; break;
992 case 0xB: opcode << "sbc"; break;
993 case 0xD: opcode << "sub"; break;
994 case 0xE: opcode << "rsb"; break;
995 default: opcode << "UNKNOWN DPMI-" << op3; break;
996 }
997 if (S == 1) {
998 opcode << "s";
999 }
Ian Rogersa9650dd2013-10-04 08:23:32 -07001000 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001001 }
Ian Rogers40627db2012-03-04 17:31:09 -08001002 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
1003 // Data-processing (plain binary immediate)
1004 // |111|11|10|00000|0000|1|111110000000000|
1005 // |5 3|21|09|87654|3 0|5|4 0 5 0|
1006 // |---|--|--|-----|----|-|---------------|
1007 // |332|22|22|22222|1111|1|111110000000000|
1008 // |1 9|87|65|43210|9 6|5|4 0 5 0|
1009 // |---|--|--|-----|----|-|---------------|
1010 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
1011 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -08001012 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -08001013 case 0x00: case 0x0A: {
1014 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -07001015 ArmRegister Rd(instr, 8);
1016 ArmRegister Rn(instr, 16);
1017 uint32_t i = (instr >> 26) & 1;
1018 uint32_t imm3 = (instr >> 12) & 0x7;
1019 uint32_t imm8 = instr & 0xFF;
1020 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
1021 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -08001022 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -07001023 args << Rd << ", " << Rn << ", #" << imm12;
1024 } else {
1025 opcode << "adr";
1026 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -08001027 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -07001028 }
1029 break;
1030 }
Ian Rogers55019132013-02-08 01:05:23 -08001031 case 0x04: case 0x0C: {
1032 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001033 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -08001034 uint32_t i = (instr >> 26) & 1;
1035 uint32_t imm3 = (instr >> 12) & 0x7;
1036 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001037 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001038 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -08001039 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -07001040 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -08001041 break;
1042 }
jeffhaoeae26912013-01-28 16:29:54 -08001043 case 0x16: {
1044 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
1045 ArmRegister Rd(instr, 8);
1046 ArmRegister Rn(instr, 16);
1047 uint32_t msb = instr & 0x1F;
1048 uint32_t imm2 = (instr >> 6) & 0x3;
1049 uint32_t imm3 = (instr >> 12) & 0x7;
1050 uint32_t lsb = (imm3 << 2) | imm2;
1051 uint32_t width = msb - lsb + 1;
1052 if (Rn.r != 0xF) {
1053 opcode << "bfi";
1054 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
1055 } else {
1056 opcode << "bfc";
1057 args << Rd << ", #" << lsb << ", #" << width;
1058 }
1059 break;
1060 }
Ian Rogers40627db2012-03-04 17:31:09 -08001061 default:
1062 break;
1063 }
1064 } else {
1065 // Branches and miscellaneous control
1066 // |111|11|1000000|0000|1|111|1100|00000000|
1067 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
1068 // |---|--|-------|----|-|---|----|--------|
1069 // |332|22|2222222|1111|1|111|1100|00000000|
1070 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
1071 // |---|--|-------|----|-|---|----|--------|
1072 // |111|10| op2 | |1|op3|op4 | |
1073
1074 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001075 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -08001076 switch (op3) {
1077 case 0:
1078 if ((op2 & 0x38) != 0x38) {
1079 // Conditional branch
1080 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
1081 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
1082 // |---|--|-|----|------|-|-|--|-|--|-----------|
1083 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
1084 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
1085 // |---|--|-|----|------|-|-|--|-|--|-----------|
1086 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
1087 uint32_t S = (instr >> 26) & 1;
1088 uint32_t J2 = (instr >> 11) & 1;
1089 uint32_t J1 = (instr >> 13) & 1;
1090 uint32_t imm6 = (instr >> 16) & 0x3F;
1091 uint32_t imm11 = instr & 0x7FF;
1092 uint32_t cond = (instr >> 22) & 0xF;
1093 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1094 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001095 opcode << "b";
1096 DumpCond(opcode, cond);
1097 opcode << ".w";
1098 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -07001099 } else if (op2 == 0x3B) {
1100 // Miscellaneous control instructions
1101 uint32_t op5 = (instr >> 4) & 0xF;
1102 switch (op5) {
Ian Rogersb122a4b2013-11-19 18:00:50 -08001103 case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
1104 case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
1105 case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
Ian Rogers9af89402012-09-07 11:29:35 -07001106 }
Ian Rogers40627db2012-03-04 17:31:09 -08001107 }
1108 break;
1109 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001110 if ((op2 & 0x38) == 0x38) {
1111 if (op2 == 0x7F) {
1112 opcode << "udf";
1113 }
1114 break;
1115 }
1116 // Else deliberate fall-through to B.
1117 case 1: case 3: {
1118 // B
1119 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1120 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1121 // |---|--|-|----|------|--|--|-|--|-----------|
1122 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1123 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1124 // |---|--|-|----|------|--|--|-|--|-----------|
1125 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1126 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1127 uint32_t S = (instr >> 26) & 1;
1128 uint32_t cond = (instr >> 22) & 0xF;
1129 uint32_t J2 = (instr >> 11) & 1;
1130 uint32_t form = (instr >> 12) & 1;
1131 uint32_t J1 = (instr >> 13) & 1;
1132 uint32_t imm10 = (instr >> 16) & 0x3FF;
1133 uint32_t imm6 = (instr >> 16) & 0x3F;
1134 uint32_t imm11 = instr & 0x7FF;
1135 opcode << "b";
1136 int32_t imm32;
1137 if (form == 0) {
1138 DumpCond(opcode, cond);
1139 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1140 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1141 } else {
1142 uint32_t I1 = ~(J1 ^ S);
1143 uint32_t I2 = ~(J2 ^ S);
1144 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1145 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1146 }
1147 opcode << ".w";
1148 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001149 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001150 }
Ian Rogers40627db2012-03-04 17:31:09 -08001151 case 4: case 6: case 5: case 7: {
1152 // BL, BLX (immediate)
1153 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1154 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1155 // |---|--|-|----------|--|--|-|--|-----------|
1156 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1157 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1158 // |---|--|-|----------|--|--|-|--|-----------|
1159 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
1160 uint32_t S = (instr >> 26) & 1;
1161 uint32_t J2 = (instr >> 11) & 1;
1162 uint32_t L = (instr >> 12) & 1;
1163 uint32_t J1 = (instr >> 13) & 1;
1164 uint32_t imm10 = (instr >> 16) & 0x3FF;
1165 uint32_t imm11 = instr & 0x7FF;
1166 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001167 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -08001168 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001169 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001170 }
1171 uint32_t I1 = ~(J1 ^ S);
1172 uint32_t I2 = ~(J2 ^ S);
1173 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1174 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001175 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001176 break;
1177 }
1178 }
1179 }
1180 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001181 case 3:
1182 switch (op2) {
1183 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
1184 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
1185 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001186 // |111|11|100|000|0|0000|1111|110000|000000|
1187 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1188 // |---|--|---|---|-|----|----|------|------|
1189 // |332|22|222|222|2|1111|1111|110000|000000|
1190 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1191 // |---|--|---|---|-|----|----|------|------|
1192 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001193 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001194 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001195 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001196 case 0x0: case 0x4: {
1197 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1198 // 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 -07001199 ArmRegister Rn(instr, 16);
1200 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -07001201 opcode << "strb";
1202 if ((instr & 0x800) != 0) {
1203 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001204 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001205 } else {
1206 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001207 ArmRegister Rm(instr, 0);
1208 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001209 if (imm2 != 0) {
1210 args << ", " << "lsl #" << imm2;
1211 }
1212 args << "]";
1213 }
1214 break;
1215 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001216 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001217 ArmRegister Rn(instr, 16);
1218 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001219 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001220 if ((instr & 0x800) != 0) {
1221 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1222 uint32_t P = (instr >> 10) & 1;
1223 uint32_t U = (instr >> 9) & 1;
1224 uint32_t W = (instr >> 8) & 1;
1225 uint32_t imm8 = instr & 0xFF;
1226 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1227 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1228 opcode << "push";
1229 args << Rt;
1230 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1231 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001232 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001233 if (P == 1 && U == 1 && W == 0) {
1234 opcode << "strt";
1235 } else {
1236 opcode << "str";
1237 }
1238 args << Rt << ", [" << Rn;
1239 if (P == 0 && W == 1) {
1240 args << "], #" << imm32;
1241 } else {
1242 args << ", #" << imm32 << "]";
1243 if (W == 1) {
1244 args << "!";
1245 }
Ian Rogers40627db2012-03-04 17:31:09 -08001246 }
1247 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001248 } else {
1249 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1250 ArmRegister Rn(instr, 16);
1251 ArmRegister Rt(instr, 12);
1252 ArmRegister Rm(instr, 0);
1253 uint32_t imm2 = (instr >> 4) & 3;
1254 opcode << "str.w";
1255 args << Rt << ", [" << Rn << ", " << Rm;
1256 if (imm2 != 0) {
1257 args << ", lsl #" << imm2;
1258 }
1259 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001260 }
1261 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001262 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001263 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001264 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001265 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001266 }
Ian Rogers40627db2012-03-04 17:31:09 -08001267 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001268 }
1269 }
1270
1271 break;
1272 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001273 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -08001274 // Load halfword
1275 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1276 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1277 // |---|--|--|---|--|-|----|----|------|------|
1278 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1279 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1280 // |---|--|--|---|--|-|----|----|------|------|
1281 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1282 // |111|11| op2 | | | imm12 |
1283 uint32_t op3 = (instr >> 23) & 3;
1284 ArmRegister Rn(instr, 16);
1285 ArmRegister Rt(instr, 12);
1286 if (Rt.r != 15) {
1287 if (op3 == 1) {
1288 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1289 uint32_t imm12 = instr & 0xFFF;
1290 opcode << "ldrh.w";
1291 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1292 if (Rn.r == 9) {
1293 args << " ; ";
1294 Thread::DumpThreadOffset(args, imm12, 4);
1295 } else if (Rn.r == 15) {
1296 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1297 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1298 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1299 }
1300 } else if (op3 == 3) {
1301 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
1302 uint32_t imm12 = instr & 0xFFF;
1303 opcode << "ldrsh.w";
1304 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1305 if (Rn.r == 9) {
1306 args << " ; ";
1307 Thread::DumpThreadOffset(args, imm12, 4);
1308 } else if (Rn.r == 15) {
1309 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1310 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1311 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1312 }
1313 }
1314 }
1315 break;
1316 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001317 case 0x29: { // 0101001
1318 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1319 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1320 // |---|--|-------|----|----|----|--|---|----|
1321 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1322 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1323 // |---|--|-------|----|----|----|--|---|----|
1324 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1325 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1326 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1327 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1328 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1329 if ((instr & 0xf0c0) == 0xf080) {
1330 uint32_t op3 = (instr >> 4) & 3;
1331 opcode << kThumbReverseOperations[op3];
1332 ArmRegister Rm(instr, 0);
1333 ArmRegister Rd(instr, 8);
1334 args << Rd << ", " << Rm;
1335 ArmRegister Rm2(instr, 16);
1336 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1337 args << " (UNPREDICTABLE)";
1338 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001339 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001340 break;
1341 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001342 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001343 // Load word
1344 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1345 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1346 // |---|--|--|---|--|-|----|----|------|------|
1347 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1348 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1349 // |---|--|--|---|--|-|----|----|------|------|
1350 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1351 // |111|11| op2 | | | imm12 |
1352 uint32_t op3 = (instr >> 23) & 3;
1353 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001354 ArmRegister Rn(instr, 16);
1355 ArmRegister Rt(instr, 12);
1356 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001357 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1358 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1359 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001360 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001361 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001362 if (Rn.r == 9) {
1363 args << " ; ";
1364 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001365 } else if (Rn.r == 15) {
1366 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1367 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1368 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001369 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001370 } else if (op4 == 0) {
1371 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1372 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001373 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001374 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001375 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001376 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001377 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001378 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001379 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001380 } else {
1381 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1382 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001383 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001384 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001385 }
1386 break;
1387 }
Dave Allison70202782013-10-22 17:52:19 -07001388 default: // more formats
1389 if ((op2 >> 4) == 2) { // 010xxxx
1390 // data processing (register)
1391 } else if ((op2 >> 3) == 6) { // 0110xxx
1392 // Multiply, multiply accumulate, and absolute difference
1393 op1 = (instr >> 20) & 0x7;
1394 op2 = (instr >> 4) & 0x2;
1395 ArmRegister Ra(instr, 12);
1396 ArmRegister Rn(instr, 16);
1397 ArmRegister Rm(instr, 0);
1398 ArmRegister Rd(instr, 8);
1399 switch (op1) {
1400 case 0:
1401 if (op2 == 0) {
1402 if (Ra.r == 0xf) {
1403 opcode << "mul";
1404 args << Rd << ", " << Rn << ", " << Rm;
1405 } else {
1406 opcode << "mla";
1407 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1408 }
1409 } else {
1410 opcode << "mls";
1411 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1412 }
1413 break;
1414 case 1:
1415 case 2:
1416 case 3:
1417 case 4:
1418 case 5:
1419 case 6:
1420 break; // do these sometime
1421 }
1422 } else if ((op2 >> 3) == 7) { // 0111xxx
1423 // Long multiply, long multiply accumulate, and divide
1424 op1 = (instr >> 20) & 0x7;
1425 op2 = (instr >> 4) & 0xf;
1426 ArmRegister Rn(instr, 16);
1427 ArmRegister Rm(instr, 0);
1428 ArmRegister Rd(instr, 8);
1429 ArmRegister RdHi(instr, 8);
1430 ArmRegister RdLo(instr, 12);
1431 switch (op1) {
1432 case 0:
1433 opcode << "smull";
1434 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1435 break;
1436 case 1:
1437 opcode << "sdiv";
1438 args << Rd << ", " << Rn << ", " << Rm;
1439 break;
1440 case 2:
1441 opcode << "umull";
1442 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1443 break;
1444 case 3:
1445 opcode << "udiv";
1446 args << Rd << ", " << Rn << ", " << Rm;
1447 break;
1448 case 4:
1449 case 5:
1450 case 6:
1451 break; // TODO: when we generate these...
1452 }
1453 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001454 }
1455 default:
1456 break;
1457 }
Ian Rogers9af89402012-09-07 11:29:35 -07001458
1459 // Apply any IT-block conditions to the opcode if necessary.
1460 if (!it_conditions_.empty()) {
1461 opcode << it_conditions_.back();
1462 it_conditions_.pop_back();
1463 }
1464
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001465 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001466 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001467} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001468
1469size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1470 uint16_t instr = ReadU16(instr_ptr);
1471 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1472 if (is_32bit) {
1473 return DumpThumb32(os, instr_ptr);
1474 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001475 std::ostringstream opcode;
1476 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001477 uint16_t opcode1 = instr >> 10;
1478 if (opcode1 < 0x10) {
1479 // shift (immediate), add, subtract, move, and compare
1480 uint16_t opcode2 = instr >> 9;
1481 switch (opcode2) {
1482 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1483 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001484 // Logical shift left - 00 000xx iii mmm ddd
1485 // Logical shift right - 00 001xx iii mmm ddd
1486 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001487 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001488 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001489 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001490 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001491 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001492 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001493 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001494 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001495 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001496 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001497 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001498 break;
1499 }
1500 case 0xC: case 0xD: case 0xE: case 0xF: {
1501 // Add register - 00 01100 mmm nnn ddd
1502 // Sub register - 00 01101 mmm nnn ddd
1503 // Add 3-bit immediate - 00 01110 iii nnn ddd
1504 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1505 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001506 ThumbRegister Rn(instr, 3);
1507 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001508 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001509 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001510 } else {
1511 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001512 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001513 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001514 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001515 }
1516 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001517 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001518 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001519 ArmRegister Rm(imm3_or_Rm);
1520 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001521 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001522 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001523 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001524 break;
1525 }
1526 case 0x10: case 0x11: case 0x12: case 0x13:
1527 case 0x14: case 0x15: case 0x16: case 0x17:
1528 case 0x18: case 0x19: case 0x1A: case 0x1B:
1529 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1530 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1531 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1532 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1533 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001534 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001535 uint16_t imm8 = instr & 0xFF;
1536 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001537 case 4: opcode << "movs"; break;
1538 case 5: opcode << "cmp"; break;
1539 case 6: opcode << "adds"; break;
1540 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001541 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001542 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001543 break;
1544 }
1545 default:
1546 break;
1547 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001548 } else if (opcode1 == 0x10) {
1549 // Data-processing
1550 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001551 ThumbRegister rm(instr, 3);
1552 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001553 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001554 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001555 } else if (opcode1 == 0x11) {
1556 // Special data instructions and branch and exchange
1557 uint16_t opcode2 = (instr >> 6) & 0x0F;
1558 switch (opcode2) {
1559 case 0x0: case 0x1: case 0x2: case 0x3: {
1560 // Add low registers - 010001 0000 xxxxxx
1561 // Add high registers - 010001 0001/001x xxxxxx
1562 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001563 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001564 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001565 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001566 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001567 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001568 break;
1569 }
1570 case 0x8: case 0x9: case 0xA: case 0xB: {
1571 // Move low registers - 010001 1000 xxxxxx
1572 // Move high registers - 010001 1001/101x xxxxxx
1573 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001574 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001575 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001576 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001577 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001578 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001579 break;
1580 }
1581 case 0x5: case 0x6: case 0x7: {
1582 // Compare high registers - 010001 0101/011x xxxxxx
1583 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001584 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001585 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001586 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001587 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001588 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001589 break;
1590 }
1591 case 0xC: case 0xD: case 0xE: case 0xF: {
1592 // Branch and exchange - 010001 110x xxxxxx
1593 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001594 ArmRegister rm(instr, 3);
1595 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1596 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001597 break;
1598 }
1599 default:
1600 break;
1601 }
jeffhaoeae26912013-01-28 16:29:54 -08001602 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1603 ThumbRegister Rt(instr, 8);
1604 uint16_t imm8 = instr & 0xFF;
1605 opcode << "ldr";
1606 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001607 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1608 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1609 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1610 // Load/store single data item
1611 uint16_t opA = (instr >> 12) & 0xF;
1612 if (opA == 0x5) {
1613 uint16_t opB = (instr >> 9) & 0x7;
1614 ThumbRegister Rm(instr, 6);
1615 ThumbRegister Rn(instr, 3);
1616 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001617 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001618 case 0: opcode << "str"; break;
1619 case 1: opcode << "strh"; break;
1620 case 2: opcode << "strb"; break;
1621 case 3: opcode << "ldrsb"; break;
1622 case 4: opcode << "ldr"; break;
1623 case 5: opcode << "ldrh"; break;
1624 case 6: opcode << "ldrb"; break;
1625 case 7: opcode << "ldrsh"; break;
1626 }
1627 args << Rt << ", [" << Rn << ", " << Rm << "]";
1628 } else if (opA == 9) {
1629 uint16_t opB = (instr >> 11) & 1;
1630 ThumbRegister Rt(instr, 8);
1631 uint16_t imm8 = instr & 0xFF;
1632 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001633 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001634 } else {
1635 uint16_t imm5 = (instr >> 6) & 0x1F;
1636 uint16_t opB = (instr >> 11) & 1;
1637 ThumbRegister Rn(instr, 3);
1638 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001639 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001640 case 6:
1641 imm5 <<= 2;
1642 opcode << (opB == 0 ? "str" : "ldr");
1643 break;
1644 case 7:
1645 imm5 <<= 0;
1646 opcode << (opB == 0 ? "strb" : "ldrb");
1647 break;
1648 case 8:
1649 imm5 <<= 1;
1650 opcode << (opB == 0 ? "strh" : "ldrh");
1651 break;
1652 }
1653 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1654 }
jeffhaoeae26912013-01-28 16:29:54 -08001655 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001656 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001657 uint32_t cond = (instr >> 8) & 0xF;
1658 opcode << "b";
1659 DumpCond(opcode, cond);
1660 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001661 } else if ((instr & 0xF800) == 0xA800) {
1662 // Generate SP-relative address
1663 ThumbRegister rd(instr, 8);
1664 int imm8 = instr & 0xFF;
1665 opcode << "add";
1666 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667 } else if ((instr & 0xF000) == 0xB000) {
1668 // Miscellaneous 16-bit instructions
1669 uint16_t opcode2 = (instr >> 5) & 0x7F;
1670 switch (opcode2) {
1671 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1672 // Add immediate to SP - 1011 00000 ii iiiii
1673 // Subtract immediate from SP - 1011 00001 ii iiiii
1674 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001675 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001676 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001677 break;
1678 }
Ian Rogers087b2412012-03-21 01:30:32 -07001679 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001680 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001681 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1682 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001683 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001684 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1685 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1686 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001687 // CBNZ, CBZ
1688 uint16_t op = (instr >> 11) & 1;
1689 uint16_t i = (instr >> 9) & 1;
1690 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001691 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001692 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001693 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001694 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001695 DumpBranchTarget(args, instr_ptr + 4, imm32);
1696 break;
1697 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001698 case 0x50: case 0x51: // 101000x
1699 case 0x52: case 0x53: // 101001x
1700 case 0x56: case 0x57: { // 101011x
1701 uint16_t op = (instr >> 6) & 3;
1702 opcode << kThumbReverseOperations[op];
1703 ThumbRegister Rm(instr, 3);
1704 ThumbRegister Rd(instr, 0);
1705 args << Rd << ", " << Rm;
1706 break;
1707 }
Ian Rogers40627db2012-03-04 17:31:09 -08001708 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1709 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1710 // If-Then, and hints
1711 uint16_t opA = (instr >> 4) & 0xF;
1712 uint16_t opB = instr & 0xF;
1713 if (opB == 0) {
1714 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001715 case 0: opcode << "nop"; break;
1716 case 1: opcode << "yield"; break;
1717 case 2: opcode << "wfe"; break;
1718 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001719 default: break;
1720 }
1721 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001722 uint32_t first_cond = opA;
1723 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001724 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001725
1726 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1727 // and store up the actual condition codes we'll want to add to the next few opcodes.
1728 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001729 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001730 for (size_t i = 0; i < count; ++i) {
1731 bool positive_cond = ((first_cond & 1) != 0);
1732 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1733 if (positive_mask == positive_cond) {
1734 opcode << 't';
1735 it_conditions_[i] = kConditionCodeNames[first_cond];
1736 } else {
1737 opcode << 'e';
1738 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1739 }
1740 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001741 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001742
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001743 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1744 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001745 }
1746 break;
1747 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001748 default:
1749 break;
1750 }
1751 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1752 ((instr & 0xE000) == 0x8000)) {
1753 // Load/store single data item
1754 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001755 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001756 switch (opA) {
1757 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001758 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1759 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001760 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001761 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001762 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001763 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1764 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001765 break;
1766 }
1767 case 0x9: {
1768 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1769 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1770 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001771 ThumbRegister Rt(instr, 8);
1772 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1773 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001774 break;
1775 }
1776 default:
1777 break;
1778 }
Ian Rogers40627db2012-03-04 17:31:09 -08001779 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1780 uint16_t imm11 = instr & 0x7FFF;
1781 int32_t imm32 = imm11 << 1;
1782 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001783 opcode << "b";
1784 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001785 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001786
1787 // Apply any IT-block conditions to the opcode if necessary.
1788 if (!it_conditions_.empty()) {
1789 opcode << it_conditions_.back();
1790 it_conditions_.pop_back();
1791 }
1792
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001793 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001794 }
1795 return 2;
1796}
1797
1798} // namespace arm
1799} // namespace art