blob: 90d84d5a725a949c9feab32c14ea9620718fea44 [file] [log] [blame]
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "disassembler_arm.h"
18
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringprintf.h"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070023#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080025namespace art {
26namespace arm {
27
28DisassemblerArm::DisassemblerArm() {
29}
30
Ian Rogersb23a7722012-10-09 16:54:26 -070031size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
32 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
33 DumpArm(os, begin);
34 return 4;
35 } else {
36 // remove thumb specifier bits
37 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
38 return DumpThumb16(os, begin);
39 }
40}
41
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080042void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
43 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
44 for (const uint8_t* cur = begin; cur < end; cur += 4) {
45 DumpArm(os, cur);
46 }
47 } else {
48 // remove thumb specifier bits
49 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
50 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
51 for (const uint8_t* cur = begin; cur < end;) {
52 cur += DumpThumb16(os, cur);
53 }
54 }
55}
56
Elliott Hughes77405792012-03-15 15:22:12 -070057static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070058 "eq", // 0000 - equal
59 "ne", // 0001 - not-equal
60 "cs", // 0010 - carry-set, greater than, equal or unordered
61 "cc", // 0011 - carry-clear, less than
62 "mi", // 0100 - minus, negative
63 "pl", // 0101 - plus, positive or zero
64 "vs", // 0110 - overflow
65 "vc", // 0111 - no overflow
66 "hi", // 1000 - unsigned higher
67 "ls", // 1001 - unsigned lower or same
68 "ge", // 1010 - signed greater than or equal
69 "lt", // 1011 - signed less than
70 "gt", // 1100 - signed greater than
71 "le", // 1101 - signed less than or equal
72 "", // 1110 - always
73 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080074};
75
76void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
77 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070078 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080079 } else {
80 os << "Unexpected condition: " << cond;
81 }
82}
83
Ian Rogers40627db2012-03-04 17:31:09 -080084void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
Elliott Hughes1ca98492012-04-12 17:21:02 -070085 os << StringPrintf("%+d (%p)", imm32, instr_ptr + imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080086}
87
88static uint32_t ReadU16(const uint8_t* ptr) {
89 return ptr[0] | (ptr[1] << 8);
90}
91
92static uint32_t ReadU32(const uint8_t* ptr) {
93 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
94}
95
Elliott Hughes77405792012-03-15 15:22:12 -070096static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070097 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
98 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -070099};
100
Ian Rogersad03ef52012-03-18 19:34:47 -0700101static const char* kThumbDataProcessingOperations[] = {
102 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
103 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
104};
105
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100106static const char* kThumbReverseOperations[] = {
107 "rev", "rev16", "rbit", "revsh"
108};
109
Elliott Hughes77405792012-03-15 15:22:12 -0700110struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -0700111 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700112 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -0700113 uint32_t r;
114};
115std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
116 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700117 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700118 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700119 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700120 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700121 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700122 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700123 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700124 }
125 return os;
126}
127
Elliott Hughes630e77d2012-03-22 19:20:56 -0700128struct ThumbRegister : ArmRegister {
129 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700130};
131
132struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700133 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700134 uint32_t shift;
135 ArmRegister rm;
136};
137std::ostream& operator<<(std::ostream& os, const Rm& r) {
138 os << r.rm;
139 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700140 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700141 }
142 return os;
143}
144
Elliott Hughes1ca98492012-04-12 17:21:02 -0700145struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700146 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700147 uint32_t rotate = ((instruction >> 8) & 0xf);
148 uint32_t imm = (instruction & 0xff);
149 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
150 }
151 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700152};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700153std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700154 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700155 return os;
156}
157
158struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700159 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700160 uint32_t register_list;
161};
162std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
163 if (rhs.register_list == 0) {
164 os << "<no register list?>";
165 return os;
166 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700167 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700168 bool first = true;
169 for (size_t i = 0; i < 16; i++) {
170 if ((rhs.register_list & (1 << i)) != 0) {
171 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700172 first = false;
173 } else {
174 os << ", ";
175 }
176 os << ArmRegister(i);
177 }
178 }
179 os << "}";
180 return os;
181}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800182
Vladimir Markodd577a32013-11-07 19:25:24 +0000183struct FpRegister {
184 explicit FpRegister(uint32_t instr, uint16_t at_bit, uint16_t extra_at_bit) {
185 size = (instr >> 8) & 1;
186 uint32_t Vn = (instr >> at_bit) & 0xF;
187 uint32_t N = (instr >> extra_at_bit) & 1;
188 r = (size != 0 ? ((N << 4) | Vn) : ((Vn << 1) | N));
189 }
190 FpRegister(const FpRegister& other, uint32_t offset)
191 : size(other.size), r(other.r + offset) {}
192
193 uint32_t size; // 0 = f32, 1 = f64
194 uint32_t r;
195};
196std::ostream& operator<<(std::ostream& os, const FpRegister& rhs) {
197 return os << ((rhs.size != 0) ? "d" : "s") << rhs.r;
198}
199
200struct FpRegisterRange {
201 explicit FpRegisterRange(uint32_t instr)
202 : first(instr, 12, 22), imm8(instr & 0xFF) {}
203 FpRegister first;
204 uint32_t imm8;
205};
206std::ostream& operator<<(std::ostream& os, const FpRegisterRange& rhs) {
207 os << "{" << rhs.first;
208 int count = (rhs.first.size != 0 ? ((rhs.imm8 + 1u) >> 1) : rhs.imm8);
209 if (count > 1) {
210 os << "-" << FpRegister(rhs.first, count - 1);
211 }
212 if (rhs.imm8 == 0) {
213 os << " (EMPTY)";
214 } else if (rhs.first.size != 0 && (rhs.imm8 & 1) != 0) {
215 os << rhs.first << " (HALF)";
216 }
217 os << "}";
218 return os;
219}
220
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800221void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700222 uint32_t instruction = ReadU32(instr_ptr);
223 uint32_t cond = (instruction >> 28) & 0xf;
224 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700225 std::string opcode;
226 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700227 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700228 switch (op1) {
229 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700230 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700231 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700232 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700233 opcode = "bkpt";
234 uint32_t imm12 = (instruction >> 8) & 0xfff;
235 uint32_t imm4 = (instruction & 0xf);
236 args << '#' << ((imm12 << 4) | imm4);
237 break;
238 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700239 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700240 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700241 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700242 break;
243 }
244 bool i = (instruction & (1 << 25)) != 0;
245 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700246 uint32_t op = (instruction >> 21) & 0xf;
247 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700248 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700249 if (implicit_s) {
250 // Rd is unused (and not shown), and we don't show the 's' suffix either.
251 } else {
252 if (s) {
253 suffixes += 's';
254 }
255 args << ArmRegister(instruction, 12) << ", ";
256 }
Elliott Hughes77405792012-03-15 15:22:12 -0700257 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700258 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700259 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700260 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700261 }
262 }
263 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700264 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700265 {
266 bool p = (instruction & (1 << 24)) != 0;
267 bool b = (instruction & (1 << 22)) != 0;
268 bool w = (instruction & (1 << 21)) != 0;
269 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700270 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700271 args << ArmRegister(instruction, 12) << ", ";
272 ArmRegister rn(instruction, 16);
273 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700274 UNIMPLEMENTED(FATAL) << "literals";
275 } else {
276 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700277 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700278 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700279 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700280 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700281 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700282 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700283 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700284 } else {
285 LOG(FATAL) << p << " " << w;
286 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700287 if (rn.r == 9) {
288 args << " ; ";
Elliott Hughes1ca98492012-04-12 17:21:02 -0700289 Thread::DumpThreadOffset(args, offset, 4);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700290 }
Elliott Hughes77405792012-03-15 15:22:12 -0700291 }
292 }
293 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700294 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700295 {
296 bool p = (instruction & (1 << 24)) != 0;
297 bool u = (instruction & (1 << 23)) != 0;
298 bool w = (instruction & (1 << 21)) != 0;
299 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700300 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700301 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700302 }
303 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700304 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700305 {
306 bool bl = (instruction & (1 << 24)) != 0;
307 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700308 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700309 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700310 DumpBranchTarget(args, instr_ptr + 8, imm32);
311 }
312 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700313 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700314 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700315 break;
316 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700317 opcode += kConditionCodeNames[cond];
318 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700319 // TODO: a more complete ARM disassembler could generate wider opcodes.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800320 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800321}
322
Ian Rogersa9650dd2013-10-04 08:23:32 -0700323int32_t ThumbExpand(int32_t imm12) {
324 if ((imm12 & 0xC00) == 0) {
325 switch ((imm12 >> 8) & 3) {
326 case 0:
327 return imm12 & 0xFF;
328 case 1:
329 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
330 case 2:
331 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
332 default: // 3
333 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
334 (imm12 & 0xFF);
335 }
336 } else {
337 uint32_t val = 0x80 | (imm12 & 0x7F);
338 int32_t rotate = (imm12 >> 7) & 0x1F;
339 return (val >> rotate) | (val << (32 - rotate));
340 }
341}
342
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800343size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
344 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
345 // |111|1 1|1000000|0000|1111110000000000|
346 // |5 3|2 1|0987654|3 0|5 0 5 0|
347 // |---|---|-------|----|----------------|
348 // |332|2 2|2222222|1111|1111110000000000|
349 // |1 9|8 7|6543210|9 6|5 0 5 0|
350 // |---|---|-------|----|----------------|
351 // |111|op1| op2 | | |
352 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700353 if (op1 == 0) {
354 return DumpThumb16(os, instr_ptr);
355 }
356
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800357 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700358 std::ostringstream opcode;
359 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800360 switch (op1) {
361 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800362 break;
363 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700364 if ((op2 & 0x64) == 0) { // 00x x0xx
365 // |111|11|10|00|0|00|0000|1111110000000000|
366 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
367 // |---|--|--|--|-|--|----|----------------|
368 // |332|22|22|22|2|22|1111|1111110000000000|
369 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
370 // |---|--|--|--|-|--|----|----------------|
371 // |111|01|00|op|0|WL| Rn | |
372 // |111|01| op2 | | |
373 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
374 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
375 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
376 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
377 uint32_t op = (instr >> 23) & 3;
378 uint32_t W = (instr >> 21) & 1;
379 uint32_t L = (instr >> 20) & 1;
380 ArmRegister Rn(instr, 16);
381 if (op == 1 || op == 2) {
382 if (op == 1) {
383 if (L == 0) {
384 opcode << "stm";
385 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800386 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700387 if (Rn.r != 13) {
388 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700389 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700390 } else {
391 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800392 }
393 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700394 } else {
395 if (L == 0) {
396 if (Rn.r != 13) {
397 opcode << "stmdb";
398 args << Rn << (W == 0 ? "" : "!") << ", ";
399 } else {
400 opcode << "push";
401 }
402 } else {
403 opcode << "ldmdb";
404 args << Rn << (W == 0 ? "" : "!") << ", ";
405 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800406 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700407 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700409 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700410 uint32_t op3 = (instr >> 23) & 3;
411 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700412 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700413 ArmRegister Rn(instr, 16);
414 ArmRegister Rt(instr, 12);
Dave Allison70202782013-10-22 17:52:19 -0700415 ArmRegister Rd(instr, 8);
Ian Rogers9af89402012-09-07 11:29:35 -0700416 uint32_t imm8 = instr & 0xFF;
Dave Allison70202782013-10-22 17:52:19 -0700417 if ((op3 & 2) == 2) { // 1x
418 int W = (instr >> 21) & 1;
419 int U = (instr >> 23) & 1;
420 int P = (instr >> 24) & 1;
421
422 if ((op4 & 1) == 1) {
423 opcode << "ldrd";
424 } else {
425 opcode << "strd";
426 }
427 args << Rt << "," << Rd << ", [" << Rn;
428 const char *sign = U ? "+" : "-";
429 if (P == 0 && W == 1) {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000430 args << "], #" << sign << (imm8 << 2);
Dave Allison70202782013-10-22 17:52:19 -0700431 } else {
Vladimir Markoad435eb2013-11-15 15:21:25 +0000432 args << ", #" << sign << (imm8 << 2) << "]";
Dave Allison70202782013-10-22 17:52:19 -0700433 if (W == 1) {
434 args << "!";
435 }
436 }
437 } else { // 0x
438 switch (op4) {
439 case 0:
440 if (op3 == 0) { // op3 is 00, op4 is 00
441 opcode << "strex";
442 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000443 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
444 Rd.r == Rn.r || Rd.r == Rt.r) {
445 args << " (UNPREDICTABLE)";
446 }
Dave Allison70202782013-10-22 17:52:19 -0700447 } else { // op3 is 01, op4 is 00
448 // this is one of strexb, strexh or strexd
449 int op5 = (instr >> 4) & 0xf;
450 switch (op5) {
451 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700452 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000453 opcode << ((op5 == 4) ? "strexb" : "strexh");
454 Rd = ArmRegister(instr, 0);
455 args << Rd << ", " << Rt << ", [" << Rn << "]";
456 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
457 Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
458 args << " (UNPREDICTABLE)";
459 }
Dave Allison70202782013-10-22 17:52:19 -0700460 break;
461 case 7:
462 opcode << "strexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000463 ArmRegister Rt2 = Rd;
464 Rd = ArmRegister(instr, 0);
465 args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
466 if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
467 Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
468 Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
469 args << " (UNPREDICTABLE)";
470 }
Dave Allison70202782013-10-22 17:52:19 -0700471 break;
472 }
473 }
474 break;
475 case 1:
476 if (op3 == 0) { // op3 is 00, op4 is 01
477 opcode << "ldrex";
478 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000479 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
480 args << " (UNPREDICTABLE)";
481 }
Dave Allison70202782013-10-22 17:52:19 -0700482 } else { // op3 is 01, op4 is 01
483 // this is one of strexb, strexh or strexd
484 int op5 = (instr >> 4) & 0xf;
485 switch (op5) {
486 case 0:
487 opcode << "tbb";
488 break;
489 case 1:
490 opcode << "tbh";
491 break;
492 case 4:
Dave Allison70202782013-10-22 17:52:19 -0700493 case 5:
Vladimir Marko3e5af822013-11-21 15:01:20 +0000494 opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
495 args << Rt << ", [" << Rn << "]";
496 if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
497 args << " (UNPREDICTABLE)";
498 }
Dave Allison70202782013-10-22 17:52:19 -0700499 break;
500 case 7:
501 opcode << "ldrexd";
Vladimir Marko3e5af822013-11-21 15:01:20 +0000502 args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
503 if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
504 Rn.r == 15 || (instr & 0x00f) != 0x00f) {
505 args << " (UNPREDICTABLE)";
506 }
Dave Allison70202782013-10-22 17:52:19 -0700507 break;
508 }
509 }
510 break;
511 case 2: // op3 is 0x, op4 is 10
512 case 3: // op3 is 0x, op4 is 11
513 if (op4 == 2) {
514 opcode << "strd";
515 } else {
516 opcode << "ldrd";
517 }
518 int W = (instr >> 21) & 1;
519 int U = (instr >> 23) & 1;
520 int P = (instr >> 24) & 1;
521
522 args << Rt << "," << Rd << ", [" << Rn;
523 const char *sign = U ? "+" : "-";
524 if (P == 0 && W == 1) {
525 args << "], #" << sign << imm8;
526 } else {
527 args << ", #" << sign << imm8 << "]";
528 if (W == 1) {
529 args << "!";
530 }
531 }
532 break;
533 }
534 }
535
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700536 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
537 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100538 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
539 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
540 // |---|----|----|-|----|----|----|--|--|----|
541 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
542 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
543 // |---|----|----|-|----|----|----|--|--|----|
544 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700545 uint32_t op3 = (instr >> 21) & 0xF;
546 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100547 uint32_t imm3 = ((instr >> 12) & 0x7);
548 uint32_t imm2 = ((instr >> 6) & 0x3);
549 uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F;
550 uint32_t shift_type = ((instr >> 4) & 0x2);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700551 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100552 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700553 ArmRegister Rm(instr, 0);
554 switch (op3) {
555 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100556 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700557 opcode << "and";
558 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700559 if (S != 1U) {
560 opcode << "UNKNOWN TST-" << S;
561 break;
562 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700563 opcode << "tst";
564 S = 0; // don't print 's'
565 }
566 break;
567 case 0x1: opcode << "bic"; break;
568 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100569 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700570 opcode << "orr";
571 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100572 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700573 opcode << "mov";
574 }
575 break;
576 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100577 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700578 opcode << "orn";
579 } else {
580 opcode << "mvn";
581 }
582 break;
583 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100584 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700585 opcode << "eor";
586 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700587 if (S != 1U) {
588 opcode << "UNKNOWN TEQ-" << S;
589 break;
590 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700591 opcode << "teq";
592 S = 0; // don't print 's'
593 }
594 break;
595 case 0x6: opcode << "pkh"; break;
596 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100597 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700598 opcode << "add";
599 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700600 if (S != 1U) {
601 opcode << "UNKNOWN CMN-" << S;
602 break;
603 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700604 opcode << "cmn";
605 S = 0; // don't print 's'
606 }
607 break;
608 case 0xA: opcode << "adc"; break;
609 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100610 case 0xD:
611 if (Rd.r != 0xF) {
612 opcode << "sub";
613 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700614 if (S != 1U) {
615 opcode << "UNKNOWN CMP-" << S;
616 break;
617 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100618 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100619 S = 0; // don't print 's'
620 }
621 break;
622 case 0xE: opcode << "rsb"; break;
623 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700624 }
Ian Rogers087b2412012-03-21 01:30:32 -0700625
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700626 if (S == 1) {
627 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700628 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700629 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100630
631 if (Rd.r != 0xF) {
632 args << Rd << ", ";
633 }
634 if (Rn.r != 0xF) {
635 args << Rn << ", ";
636 }
637 args << Rm;
638
639 // Shift operand.
640 bool noShift = (imm5 == 0 && shift_type != 0x3);
641 if (!noShift) {
642 args << ", ";
643 switch (shift_type) {
644 case 0x0: args << "lsl"; break;
645 case 0x1: args << "lsr"; break;
646 case 0x2: args << "asr"; break;
647 case 0x3:
648 if (imm5 == 0) {
649 args << "rrx";
650 } else {
651 args << "ror";
652 }
653 break;
654 }
655 if (shift_type != 0x3 /* rrx */) {
656 args << StringPrintf(" #%d", imm5);
657 }
658 }
659
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700660 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
661 // Co-processor instructions
662 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
663 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
664 // |---|-|--|------|----|----|----|---|---|----|
665 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
666 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
667 // |---|-|--|------|----|----|----|---|---|----|
668 // |111| |11| op3 | Rn | |copr| |op4| |
669 uint32_t op3 = (instr >> 20) & 0x3F;
670 uint32_t coproc = (instr >> 8) & 0xF;
671 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700672
673 if (coproc == 10 || coproc == 11) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000674 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
675 // Extension register load/store instructions
676 // |1111|110|00000|0000|1111|110|0|00000000|
677 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
678 // |----|---|-----|----|----|---|-|--------|
679 // |3322|222|22222|1111|1111|110|0|00000000|
680 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
681 // |----|---|-----|----|----|---|-|--------|
682 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700683 uint32_t P = (instr >> 24) & 1;
684 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700685 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000686 if (P == U && W == 1) {
687 opcode << "UNDEFINED";
688 } else {
689 uint32_t L = (instr >> 20) & 1;
690 uint32_t S = (instr >> 8) & 1;
691 ArmRegister Rn(instr, 16);
692 if (P == 1 && W == 0) { // VLDR
693 FpRegister d(instr, 12, 22);
694 uint32_t imm8 = instr & 0xFF;
695 opcode << (L == 1 ? "vldr" : "vstr");
696 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
697 << (imm8 << 2) << "]";
698 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
699 opcode << (L == 1 ? "vpop" : "vpush");
700 args << FpRegisterRange(instr);
701 } else { // VLDM
702 opcode << (L == 1 ? "vldm" : "vstm");
703 args << Rn << ((W == 1) ? "!" : "") << ", "
704 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700705 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000706 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700707 }
Dave Allison70202782013-10-22 17:52:19 -0700708 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000709 if ((instr & 0xD0) == 0x10) {
710 // 64bit transfers between ARM core and extension registers.
711 uint32_t L = (instr >> 20) & 1;
712 uint32_t S = (instr >> 8) & 1;
713 ArmRegister Rt2(instr, 16);
714 ArmRegister Rt(instr, 12);
715 FpRegister m(instr, 0, 5);
716 opcode << "vmov" << (S ? ".f64" : ".f32");
717 if (L == 1) {
718 args << Rt << ", " << Rt2 << ", ";
719 }
720 if (S) {
721 args << m;
722 } else {
723 args << m << ", " << FpRegister(m, 1);
724 }
725 if (L == 0) {
726 args << ", " << Rt << ", " << Rt2;
727 }
728 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
729 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
730 args << " (UNPREDICTABLE)";
731 }
732 }
Dave Allison70202782013-10-22 17:52:19 -0700733 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
734 // fp data processing
735 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000736 if (coproc == 10 && (op3 & 0xE) == 0) {
737 // VMOV (between ARM core register and single-precision register)
738 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
739 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
740 // |----|----|---|- |----|----|----|-|--|-|----|
741 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
742 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
743 // |----|----|---|- |----|----|----|-|--|-|----|
744 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
745 uint32_t op = op3 & 1;
746 ArmRegister Rt(instr, 12);
747 FpRegister n(instr, 16, 7);
748 opcode << "vmov.f32";
749 if (op) {
750 args << Rt << ", " << n;
751 } else {
752 args << n << ", " << Rt;
753 }
754 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
755 args << " (UNPREDICTABLE)";
756 }
757 } else if (coproc == 10 && op3 == 0x2F) {
758 // VMRS
759 // |1111|11000000|0000|1111|1100|000|0|0000|
760 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
761 // |----|--------|----|----|----|---|-|----|
762 // |3322|22222222|1111|1111|1100|000|0|0000|
763 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
764 // |----|--------|----|----|----|---|-|----|
765 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
766 uint32_t spec_reg = (instr >> 16) & 0xF;
767 ArmRegister Rt(instr, 12);
768 opcode << "vmrs";
769 if (spec_reg == 1) {
770 if (Rt.r == 15) {
771 args << "APSR_nzcv, FPSCR";
772 } else if (Rt.r == 13) {
773 args << Rt << ", FPSCR (UNPREDICTABLE)";
774 } else {
775 args << Rt << ", FPSCR";
776 }
777 } else {
778 args << "(PRIVILEGED)";
779 }
780 } else if (coproc == 11 && (op3 & 0x9) != 8) {
781 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
782 }
Ian Rogers9af89402012-09-07 11:29:35 -0700783 }
Dave Allison70202782013-10-22 17:52:19 -0700784 }
785
786 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700787 if ((coproc & 0xE) == 0xA) {
788 // VFP data-processing instructions
789 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
790 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
791 // |---|-|----|----|----|----|---|-|----|-|-|----|
792 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
793 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
794 // |---|-|----|----|----|----|---|-|----|-|-|----|
795 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
796 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
797 uint32_t opc1 = (instr >> 20) & 0xF;
798 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700799 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700800 if ((opc1 & 0xB) == 0xB) { // 1x11
801 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700802 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000803 FpRegister d(instr, 12, 22);
804 FpRegister m(instr, 0, 5);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700805 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700806 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700807 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000808 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (sz == 1 ? ".f64" : ".f32");
809 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700810 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700811 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700812 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000813 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (sz == 1 ? ".f64" : ".f32");
814 args << d << ", " << m;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700815 break;
816 }
817 }
818 }
819 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700820 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
821 // Advanced SIMD
822 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
823 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
824 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
Ian Rogers0183dd72012-09-17 23:06:51 -0700825 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000826 FpRegister d(instr, 12, 22);
827 FpRegister m(instr, 0, 5);
828 opcode << "vsqrt" << (sz == 1 ? ".f64" : ".f32");
829 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700830 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700831 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800832 }
833 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800834 case 2:
835 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
836 // Data-processing (modified immediate)
837 // |111|11|10|0000|0|0000|1|111|1100|00000000|
838 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
839 // |---|--|--|----|-|----|-|---|----|--------|
840 // |332|22|22|2222|2|1111|1|111|1100|00000000|
841 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
842 // |---|--|--|----|-|----|-|---|----|--------|
843 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
844 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800845 uint32_t i = (instr >> 26) & 1;
846 uint32_t op3 = (instr >> 21) & 0xF;
847 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700848 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800849 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700850 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800851 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800852 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
853 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
854 if (op3 == 0x2) {
855 opcode << "mov";
856 if (S == 1) {
857 opcode << "s";
858 }
859 opcode << ".w";
860 } else {
861 opcode << "mvn";
862 if (S == 1) {
863 opcode << "s";
864 }
865 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700866 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800867 } else if (Rd.r == 0xF && S == 1 &&
868 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
869 if (op3 == 0x0) {
870 opcode << "tst";
871 } else if (op3 == 0x4) {
872 opcode << "teq";
873 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +0000874 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800875 } else {
876 opcode << "cmp.w";
877 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700878 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800879 } else {
880 switch (op3) {
881 case 0x0: opcode << "and"; break;
882 case 0x1: opcode << "bic"; break;
883 case 0x2: opcode << "orr"; break;
884 case 0x3: opcode << "orn"; break;
885 case 0x4: opcode << "eor"; break;
886 case 0x8: opcode << "add"; break;
887 case 0xA: opcode << "adc"; break;
888 case 0xB: opcode << "sbc"; break;
889 case 0xD: opcode << "sub"; break;
890 case 0xE: opcode << "rsb"; break;
891 default: opcode << "UNKNOWN DPMI-" << op3; break;
892 }
893 if (S == 1) {
894 opcode << "s";
895 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700896 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800897 }
Ian Rogers40627db2012-03-04 17:31:09 -0800898 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
899 // Data-processing (plain binary immediate)
900 // |111|11|10|00000|0000|1|111110000000000|
901 // |5 3|21|09|87654|3 0|5|4 0 5 0|
902 // |---|--|--|-----|----|-|---------------|
903 // |332|22|22|22222|1111|1|111110000000000|
904 // |1 9|87|65|43210|9 6|5|4 0 5 0|
905 // |---|--|--|-----|----|-|---------------|
906 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
907 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800908 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -0800909 case 0x00: case 0x0A: {
910 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -0700911 ArmRegister Rd(instr, 8);
912 ArmRegister Rn(instr, 16);
913 uint32_t i = (instr >> 26) & 1;
914 uint32_t imm3 = (instr >> 12) & 0x7;
915 uint32_t imm8 = instr & 0xFF;
916 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
917 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -0800918 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -0700919 args << Rd << ", " << Rn << ", #" << imm12;
920 } else {
921 opcode << "adr";
922 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -0800923 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -0700924 }
925 break;
926 }
Ian Rogers55019132013-02-08 01:05:23 -0800927 case 0x04: case 0x0C: {
928 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700929 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800930 uint32_t i = (instr >> 26) & 1;
931 uint32_t imm3 = (instr >> 12) & 0x7;
932 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700933 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800934 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -0800935 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700936 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800937 break;
938 }
jeffhaoeae26912013-01-28 16:29:54 -0800939 case 0x16: {
940 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
941 ArmRegister Rd(instr, 8);
942 ArmRegister Rn(instr, 16);
943 uint32_t msb = instr & 0x1F;
944 uint32_t imm2 = (instr >> 6) & 0x3;
945 uint32_t imm3 = (instr >> 12) & 0x7;
946 uint32_t lsb = (imm3 << 2) | imm2;
947 uint32_t width = msb - lsb + 1;
948 if (Rn.r != 0xF) {
949 opcode << "bfi";
950 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
951 } else {
952 opcode << "bfc";
953 args << Rd << ", #" << lsb << ", #" << width;
954 }
955 break;
956 }
Ian Rogers40627db2012-03-04 17:31:09 -0800957 default:
958 break;
959 }
960 } else {
961 // Branches and miscellaneous control
962 // |111|11|1000000|0000|1|111|1100|00000000|
963 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
964 // |---|--|-------|----|-|---|----|--------|
965 // |332|22|2222222|1111|1|111|1100|00000000|
966 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
967 // |---|--|-------|----|-|---|----|--------|
968 // |111|10| op2 | |1|op3|op4 | |
969
970 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700971 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800972 switch (op3) {
973 case 0:
974 if ((op2 & 0x38) != 0x38) {
975 // Conditional branch
976 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
977 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
978 // |---|--|-|----|------|-|-|--|-|--|-----------|
979 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
980 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
981 // |---|--|-|----|------|-|-|--|-|--|-----------|
982 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
983 uint32_t S = (instr >> 26) & 1;
984 uint32_t J2 = (instr >> 11) & 1;
985 uint32_t J1 = (instr >> 13) & 1;
986 uint32_t imm6 = (instr >> 16) & 0x3F;
987 uint32_t imm11 = instr & 0x7FF;
988 uint32_t cond = (instr >> 22) & 0xF;
989 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
990 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700991 opcode << "b";
992 DumpCond(opcode, cond);
993 opcode << ".w";
994 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -0700995 } else if (op2 == 0x3B) {
996 // Miscellaneous control instructions
997 uint32_t op5 = (instr >> 4) & 0xF;
998 switch (op5) {
999 case 4: opcode << "dsb"; break;
1000 case 5: opcode << "dmb"; break;
1001 case 6: opcode << "isb"; break;
1002 }
Ian Rogers40627db2012-03-04 17:31:09 -08001003 }
1004 break;
1005 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -08001006 if ((op2 & 0x38) == 0x38) {
1007 if (op2 == 0x7F) {
1008 opcode << "udf";
1009 }
1010 break;
1011 }
1012 // Else deliberate fall-through to B.
1013 case 1: case 3: {
1014 // B
1015 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
1016 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1017 // |---|--|-|----|------|--|--|-|--|-----------|
1018 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1019 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1020 // |---|--|-|----|------|--|--|-|--|-----------|
1021 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1022 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1023 uint32_t S = (instr >> 26) & 1;
1024 uint32_t cond = (instr >> 22) & 0xF;
1025 uint32_t J2 = (instr >> 11) & 1;
1026 uint32_t form = (instr >> 12) & 1;
1027 uint32_t J1 = (instr >> 13) & 1;
1028 uint32_t imm10 = (instr >> 16) & 0x3FF;
1029 uint32_t imm6 = (instr >> 16) & 0x3F;
1030 uint32_t imm11 = instr & 0x7FF;
1031 opcode << "b";
1032 int32_t imm32;
1033 if (form == 0) {
1034 DumpCond(opcode, cond);
1035 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1036 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1037 } else {
1038 uint32_t I1 = ~(J1 ^ S);
1039 uint32_t I2 = ~(J2 ^ S);
1040 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1041 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1042 }
1043 opcode << ".w";
1044 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001045 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001046 }
Ian Rogers40627db2012-03-04 17:31:09 -08001047 case 4: case 6: case 5: case 7: {
1048 // BL, BLX (immediate)
1049 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1050 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1051 // |---|--|-|----------|--|--|-|--|-----------|
1052 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1053 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1054 // |---|--|-|----------|--|--|-|--|-----------|
1055 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
1056 uint32_t S = (instr >> 26) & 1;
1057 uint32_t J2 = (instr >> 11) & 1;
1058 uint32_t L = (instr >> 12) & 1;
1059 uint32_t J1 = (instr >> 13) & 1;
1060 uint32_t imm10 = (instr >> 16) & 0x3FF;
1061 uint32_t imm11 = instr & 0x7FF;
1062 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001063 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -08001064 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001065 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001066 }
1067 uint32_t I1 = ~(J1 ^ S);
1068 uint32_t I2 = ~(J2 ^ S);
1069 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1070 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001071 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001072 break;
1073 }
1074 }
1075 }
1076 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001077 case 3:
1078 switch (op2) {
1079 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
1080 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
1081 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001082 // |111|11|100|000|0|0000|1111|110000|000000|
1083 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1084 // |---|--|---|---|-|----|----|------|------|
1085 // |332|22|222|222|2|1111|1111|110000|000000|
1086 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1087 // |---|--|---|---|-|----|----|------|------|
1088 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001089 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001090 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001091 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001092 case 0x0: case 0x4: {
1093 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1094 // 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 -07001095 ArmRegister Rn(instr, 16);
1096 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -07001097 opcode << "strb";
1098 if ((instr & 0x800) != 0) {
1099 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001100 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001101 } else {
1102 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001103 ArmRegister Rm(instr, 0);
1104 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001105 if (imm2 != 0) {
1106 args << ", " << "lsl #" << imm2;
1107 }
1108 args << "]";
1109 }
1110 break;
1111 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001112 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001113 ArmRegister Rn(instr, 16);
1114 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001115 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001116 if ((instr & 0x800) != 0) {
1117 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1118 uint32_t P = (instr >> 10) & 1;
1119 uint32_t U = (instr >> 9) & 1;
1120 uint32_t W = (instr >> 8) & 1;
1121 uint32_t imm8 = instr & 0xFF;
1122 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1123 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1124 opcode << "push";
1125 args << Rt;
1126 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1127 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001128 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001129 if (P == 1 && U == 1 && W == 0) {
1130 opcode << "strt";
1131 } else {
1132 opcode << "str";
1133 }
1134 args << Rt << ", [" << Rn;
1135 if (P == 0 && W == 1) {
1136 args << "], #" << imm32;
1137 } else {
1138 args << ", #" << imm32 << "]";
1139 if (W == 1) {
1140 args << "!";
1141 }
Ian Rogers40627db2012-03-04 17:31:09 -08001142 }
1143 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001144 } else {
1145 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1146 ArmRegister Rn(instr, 16);
1147 ArmRegister Rt(instr, 12);
1148 ArmRegister Rm(instr, 0);
1149 uint32_t imm2 = (instr >> 4) & 3;
1150 opcode << "str.w";
1151 args << Rt << ", [" << Rn << ", " << Rm;
1152 if (imm2 != 0) {
1153 args << ", lsl #" << imm2;
1154 }
1155 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001156 }
1157 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001158 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001159 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001160 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001161 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001162 }
Ian Rogers40627db2012-03-04 17:31:09 -08001163 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001164 }
1165 }
1166
1167 break;
1168 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001169 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -08001170 // Load halfword
1171 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1172 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1173 // |---|--|--|---|--|-|----|----|------|------|
1174 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1175 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1176 // |---|--|--|---|--|-|----|----|------|------|
1177 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1178 // |111|11| op2 | | | imm12 |
1179 uint32_t op3 = (instr >> 23) & 3;
1180 ArmRegister Rn(instr, 16);
1181 ArmRegister Rt(instr, 12);
1182 if (Rt.r != 15) {
1183 if (op3 == 1) {
1184 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1185 uint32_t imm12 = instr & 0xFFF;
1186 opcode << "ldrh.w";
1187 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1188 if (Rn.r == 9) {
1189 args << " ; ";
1190 Thread::DumpThreadOffset(args, imm12, 4);
1191 } else if (Rn.r == 15) {
1192 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1193 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1194 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1195 }
1196 } else if (op3 == 3) {
1197 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
1198 uint32_t imm12 = instr & 0xFFF;
1199 opcode << "ldrsh.w";
1200 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1201 if (Rn.r == 9) {
1202 args << " ; ";
1203 Thread::DumpThreadOffset(args, imm12, 4);
1204 } else if (Rn.r == 15) {
1205 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1206 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1207 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1208 }
1209 }
1210 }
1211 break;
1212 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001213 case 0x29: { // 0101001
1214 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1215 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1216 // |---|--|-------|----|----|----|--|---|----|
1217 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1218 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1219 // |---|--|-------|----|----|----|--|---|----|
1220 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1221 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1222 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1223 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1224 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1225 if ((instr & 0xf0c0) == 0xf080) {
1226 uint32_t op3 = (instr >> 4) & 3;
1227 opcode << kThumbReverseOperations[op3];
1228 ArmRegister Rm(instr, 0);
1229 ArmRegister Rd(instr, 8);
1230 args << Rd << ", " << Rm;
1231 ArmRegister Rm2(instr, 16);
1232 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1233 args << " (UNPREDICTABLE)";
1234 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001235 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001236 break;
1237 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001238 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001239 // Load word
1240 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1241 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1242 // |---|--|--|---|--|-|----|----|------|------|
1243 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1244 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1245 // |---|--|--|---|--|-|----|----|------|------|
1246 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1247 // |111|11| op2 | | | imm12 |
1248 uint32_t op3 = (instr >> 23) & 3;
1249 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001250 ArmRegister Rn(instr, 16);
1251 ArmRegister Rt(instr, 12);
1252 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001253 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1254 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1255 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001256 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001257 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001258 if (Rn.r == 9) {
1259 args << " ; ";
1260 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001261 } else if (Rn.r == 15) {
1262 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1263 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1264 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001265 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001266 } else if (op4 == 0) {
1267 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1268 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001269 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001270 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001271 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001272 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001273 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001274 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001275 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001276 } else {
1277 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1278 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001279 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001280 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001281 }
1282 break;
1283 }
Dave Allison70202782013-10-22 17:52:19 -07001284 default: // more formats
1285 if ((op2 >> 4) == 2) { // 010xxxx
1286 // data processing (register)
1287 } else if ((op2 >> 3) == 6) { // 0110xxx
1288 // Multiply, multiply accumulate, and absolute difference
1289 op1 = (instr >> 20) & 0x7;
1290 op2 = (instr >> 4) & 0x2;
1291 ArmRegister Ra(instr, 12);
1292 ArmRegister Rn(instr, 16);
1293 ArmRegister Rm(instr, 0);
1294 ArmRegister Rd(instr, 8);
1295 switch (op1) {
1296 case 0:
1297 if (op2 == 0) {
1298 if (Ra.r == 0xf) {
1299 opcode << "mul";
1300 args << Rd << ", " << Rn << ", " << Rm;
1301 } else {
1302 opcode << "mla";
1303 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1304 }
1305 } else {
1306 opcode << "mls";
1307 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1308 }
1309 break;
1310 case 1:
1311 case 2:
1312 case 3:
1313 case 4:
1314 case 5:
1315 case 6:
1316 break; // do these sometime
1317 }
1318 } else if ((op2 >> 3) == 7) { // 0111xxx
1319 // Long multiply, long multiply accumulate, and divide
1320 op1 = (instr >> 20) & 0x7;
1321 op2 = (instr >> 4) & 0xf;
1322 ArmRegister Rn(instr, 16);
1323 ArmRegister Rm(instr, 0);
1324 ArmRegister Rd(instr, 8);
1325 ArmRegister RdHi(instr, 8);
1326 ArmRegister RdLo(instr, 12);
1327 switch (op1) {
1328 case 0:
1329 opcode << "smull";
1330 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1331 break;
1332 case 1:
1333 opcode << "sdiv";
1334 args << Rd << ", " << Rn << ", " << Rm;
1335 break;
1336 case 2:
1337 opcode << "umull";
1338 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1339 break;
1340 case 3:
1341 opcode << "udiv";
1342 args << Rd << ", " << Rn << ", " << Rm;
1343 break;
1344 case 4:
1345 case 5:
1346 case 6:
1347 break; // TODO: when we generate these...
1348 }
1349 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001350 }
1351 default:
1352 break;
1353 }
Ian Rogers9af89402012-09-07 11:29:35 -07001354
1355 // Apply any IT-block conditions to the opcode if necessary.
1356 if (!it_conditions_.empty()) {
1357 opcode << it_conditions_.back();
1358 it_conditions_.pop_back();
1359 }
1360
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001361 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001362 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001363} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001364
1365size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1366 uint16_t instr = ReadU16(instr_ptr);
1367 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1368 if (is_32bit) {
1369 return DumpThumb32(os, instr_ptr);
1370 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001371 std::ostringstream opcode;
1372 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001373 uint16_t opcode1 = instr >> 10;
1374 if (opcode1 < 0x10) {
1375 // shift (immediate), add, subtract, move, and compare
1376 uint16_t opcode2 = instr >> 9;
1377 switch (opcode2) {
1378 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1379 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001380 // Logical shift left - 00 000xx iii mmm ddd
1381 // Logical shift right - 00 001xx iii mmm ddd
1382 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001383 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001384 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001385 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001386 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001387 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001388 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001389 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001390 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001391 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001392 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001393 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001394 break;
1395 }
1396 case 0xC: case 0xD: case 0xE: case 0xF: {
1397 // Add register - 00 01100 mmm nnn ddd
1398 // Sub register - 00 01101 mmm nnn ddd
1399 // Add 3-bit immediate - 00 01110 iii nnn ddd
1400 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1401 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001402 ThumbRegister Rn(instr, 3);
1403 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001404 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001405 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001406 } else {
1407 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001408 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001409 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001410 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001411 }
1412 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001413 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001414 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001415 ArmRegister Rm(imm3_or_Rm);
1416 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001417 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001418 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001419 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001420 break;
1421 }
1422 case 0x10: case 0x11: case 0x12: case 0x13:
1423 case 0x14: case 0x15: case 0x16: case 0x17:
1424 case 0x18: case 0x19: case 0x1A: case 0x1B:
1425 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1426 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1427 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1428 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1429 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001430 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001431 uint16_t imm8 = instr & 0xFF;
1432 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001433 case 4: opcode << "movs"; break;
1434 case 5: opcode << "cmp"; break;
1435 case 6: opcode << "adds"; break;
1436 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001437 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001438 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001439 break;
1440 }
1441 default:
1442 break;
1443 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001444 } else if (opcode1 == 0x10) {
1445 // Data-processing
1446 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001447 ThumbRegister rm(instr, 3);
1448 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001449 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001450 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001451 } else if (opcode1 == 0x11) {
1452 // Special data instructions and branch and exchange
1453 uint16_t opcode2 = (instr >> 6) & 0x0F;
1454 switch (opcode2) {
1455 case 0x0: case 0x1: case 0x2: case 0x3: {
1456 // Add low registers - 010001 0000 xxxxxx
1457 // Add high registers - 010001 0001/001x xxxxxx
1458 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001459 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001460 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001461 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001462 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001463 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001464 break;
1465 }
1466 case 0x8: case 0x9: case 0xA: case 0xB: {
1467 // Move low registers - 010001 1000 xxxxxx
1468 // Move high registers - 010001 1001/101x xxxxxx
1469 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001470 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001471 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001472 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001473 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001474 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001475 break;
1476 }
1477 case 0x5: case 0x6: case 0x7: {
1478 // Compare high registers - 010001 0101/011x xxxxxx
1479 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001480 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001481 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001482 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001483 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001484 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001485 break;
1486 }
1487 case 0xC: case 0xD: case 0xE: case 0xF: {
1488 // Branch and exchange - 010001 110x xxxxxx
1489 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001490 ArmRegister rm(instr, 3);
1491 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1492 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001493 break;
1494 }
1495 default:
1496 break;
1497 }
jeffhaoeae26912013-01-28 16:29:54 -08001498 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1499 ThumbRegister Rt(instr, 8);
1500 uint16_t imm8 = instr & 0xFF;
1501 opcode << "ldr";
1502 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001503 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1504 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1505 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1506 // Load/store single data item
1507 uint16_t opA = (instr >> 12) & 0xF;
1508 if (opA == 0x5) {
1509 uint16_t opB = (instr >> 9) & 0x7;
1510 ThumbRegister Rm(instr, 6);
1511 ThumbRegister Rn(instr, 3);
1512 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001513 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001514 case 0: opcode << "str"; break;
1515 case 1: opcode << "strh"; break;
1516 case 2: opcode << "strb"; break;
1517 case 3: opcode << "ldrsb"; break;
1518 case 4: opcode << "ldr"; break;
1519 case 5: opcode << "ldrh"; break;
1520 case 6: opcode << "ldrb"; break;
1521 case 7: opcode << "ldrsh"; break;
1522 }
1523 args << Rt << ", [" << Rn << ", " << Rm << "]";
1524 } else if (opA == 9) {
1525 uint16_t opB = (instr >> 11) & 1;
1526 ThumbRegister Rt(instr, 8);
1527 uint16_t imm8 = instr & 0xFF;
1528 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001529 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001530 } else {
1531 uint16_t imm5 = (instr >> 6) & 0x1F;
1532 uint16_t opB = (instr >> 11) & 1;
1533 ThumbRegister Rn(instr, 3);
1534 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001535 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001536 case 6:
1537 imm5 <<= 2;
1538 opcode << (opB == 0 ? "str" : "ldr");
1539 break;
1540 case 7:
1541 imm5 <<= 0;
1542 opcode << (opB == 0 ? "strb" : "ldrb");
1543 break;
1544 case 8:
1545 imm5 <<= 1;
1546 opcode << (opB == 0 ? "strh" : "ldrh");
1547 break;
1548 }
1549 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1550 }
jeffhaoeae26912013-01-28 16:29:54 -08001551 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001552 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001553 uint32_t cond = (instr >> 8) & 0xF;
1554 opcode << "b";
1555 DumpCond(opcode, cond);
1556 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001557 } else if ((instr & 0xF800) == 0xA800) {
1558 // Generate SP-relative address
1559 ThumbRegister rd(instr, 8);
1560 int imm8 = instr & 0xFF;
1561 opcode << "add";
1562 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001563 } else if ((instr & 0xF000) == 0xB000) {
1564 // Miscellaneous 16-bit instructions
1565 uint16_t opcode2 = (instr >> 5) & 0x7F;
1566 switch (opcode2) {
1567 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1568 // Add immediate to SP - 1011 00000 ii iiiii
1569 // Subtract immediate from SP - 1011 00001 ii iiiii
1570 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001571 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001572 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001573 break;
1574 }
Ian Rogers087b2412012-03-21 01:30:32 -07001575 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001576 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001577 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1578 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001579 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001580 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1581 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1582 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001583 // CBNZ, CBZ
1584 uint16_t op = (instr >> 11) & 1;
1585 uint16_t i = (instr >> 9) & 1;
1586 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001587 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001588 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001589 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001590 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001591 DumpBranchTarget(args, instr_ptr + 4, imm32);
1592 break;
1593 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001594 case 0x50: case 0x51: // 101000x
1595 case 0x52: case 0x53: // 101001x
1596 case 0x56: case 0x57: { // 101011x
1597 uint16_t op = (instr >> 6) & 3;
1598 opcode << kThumbReverseOperations[op];
1599 ThumbRegister Rm(instr, 3);
1600 ThumbRegister Rd(instr, 0);
1601 args << Rd << ", " << Rm;
1602 break;
1603 }
Ian Rogers40627db2012-03-04 17:31:09 -08001604 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1605 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1606 // If-Then, and hints
1607 uint16_t opA = (instr >> 4) & 0xF;
1608 uint16_t opB = instr & 0xF;
1609 if (opB == 0) {
1610 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001611 case 0: opcode << "nop"; break;
1612 case 1: opcode << "yield"; break;
1613 case 2: opcode << "wfe"; break;
1614 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001615 default: break;
1616 }
1617 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001618 uint32_t first_cond = opA;
1619 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001620 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001621
1622 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1623 // and store up the actual condition codes we'll want to add to the next few opcodes.
1624 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001625 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001626 for (size_t i = 0; i < count; ++i) {
1627 bool positive_cond = ((first_cond & 1) != 0);
1628 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1629 if (positive_mask == positive_cond) {
1630 opcode << 't';
1631 it_conditions_[i] = kConditionCodeNames[first_cond];
1632 } else {
1633 opcode << 'e';
1634 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1635 }
1636 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001637 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001638
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001639 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1640 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001641 }
1642 break;
1643 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001644 default:
1645 break;
1646 }
1647 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1648 ((instr & 0xE000) == 0x8000)) {
1649 // Load/store single data item
1650 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001651 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001652 switch (opA) {
1653 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001654 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1655 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001656 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001657 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001658 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001659 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1660 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001661 break;
1662 }
1663 case 0x9: {
1664 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1665 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1666 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001667 ThumbRegister Rt(instr, 8);
1668 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1669 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001670 break;
1671 }
1672 default:
1673 break;
1674 }
Ian Rogers40627db2012-03-04 17:31:09 -08001675 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1676 uint16_t imm11 = instr & 0x7FFF;
1677 int32_t imm32 = imm11 << 1;
1678 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001679 opcode << "b";
1680 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001681 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001682
1683 // Apply any IT-block conditions to the opcode if necessary.
1684 if (!it_conditions_.empty()) {
1685 opcode << it_conditions_.back();
1686 it_conditions_.pop_back();
1687 }
1688
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001689 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001690 }
1691 return 2;
1692}
1693
1694} // namespace arm
1695} // namespace art