blob: e2622161a5666c6487de9b6299c0a18746806411 [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
183void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700184 uint32_t instruction = ReadU32(instr_ptr);
185 uint32_t cond = (instruction >> 28) & 0xf;
186 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700187 std::string opcode;
188 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700189 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700190 switch (op1) {
191 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700192 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700193 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700194 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700195 opcode = "bkpt";
196 uint32_t imm12 = (instruction >> 8) & 0xfff;
197 uint32_t imm4 = (instruction & 0xf);
198 args << '#' << ((imm12 << 4) | imm4);
199 break;
200 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700201 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700202 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700203 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700204 break;
205 }
206 bool i = (instruction & (1 << 25)) != 0;
207 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700208 uint32_t op = (instruction >> 21) & 0xf;
209 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700210 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700211 if (implicit_s) {
212 // Rd is unused (and not shown), and we don't show the 's' suffix either.
213 } else {
214 if (s) {
215 suffixes += 's';
216 }
217 args << ArmRegister(instruction, 12) << ", ";
218 }
Elliott Hughes77405792012-03-15 15:22:12 -0700219 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700220 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700221 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700222 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700223 }
224 }
225 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700226 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700227 {
228 bool p = (instruction & (1 << 24)) != 0;
229 bool b = (instruction & (1 << 22)) != 0;
230 bool w = (instruction & (1 << 21)) != 0;
231 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700232 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700233 args << ArmRegister(instruction, 12) << ", ";
234 ArmRegister rn(instruction, 16);
235 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700236 UNIMPLEMENTED(FATAL) << "literals";
237 } else {
238 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700239 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700240 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700241 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700242 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700243 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700244 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700245 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700246 } else {
247 LOG(FATAL) << p << " " << w;
248 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700249 if (rn.r == 9) {
250 args << " ; ";
Elliott Hughes1ca98492012-04-12 17:21:02 -0700251 Thread::DumpThreadOffset(args, offset, 4);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700252 }
Elliott Hughes77405792012-03-15 15:22:12 -0700253 }
254 }
255 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700256 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700257 {
258 bool p = (instruction & (1 << 24)) != 0;
259 bool u = (instruction & (1 << 23)) != 0;
260 bool w = (instruction & (1 << 21)) != 0;
261 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700262 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700263 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700264 }
265 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700266 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700267 {
268 bool bl = (instruction & (1 << 24)) != 0;
269 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700270 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700271 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700272 DumpBranchTarget(args, instr_ptr + 8, imm32);
273 }
274 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700275 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700276 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700277 break;
278 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700279 opcode += kConditionCodeNames[cond];
280 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700281 // TODO: a more complete ARM disassembler could generate wider opcodes.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800282 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800283}
284
Ian Rogersa9650dd2013-10-04 08:23:32 -0700285int32_t ThumbExpand(int32_t imm12) {
286 if ((imm12 & 0xC00) == 0) {
287 switch ((imm12 >> 8) & 3) {
288 case 0:
289 return imm12 & 0xFF;
290 case 1:
291 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
292 case 2:
293 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
294 default: // 3
295 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
296 (imm12 & 0xFF);
297 }
298 } else {
299 uint32_t val = 0x80 | (imm12 & 0x7F);
300 int32_t rotate = (imm12 >> 7) & 0x1F;
301 return (val >> rotate) | (val << (32 - rotate));
302 }
303}
304
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800305size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
306 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
307 // |111|1 1|1000000|0000|1111110000000000|
308 // |5 3|2 1|0987654|3 0|5 0 5 0|
309 // |---|---|-------|----|----------------|
310 // |332|2 2|2222222|1111|1111110000000000|
311 // |1 9|8 7|6543210|9 6|5 0 5 0|
312 // |---|---|-------|----|----------------|
313 // |111|op1| op2 | | |
314 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700315 if (op1 == 0) {
316 return DumpThumb16(os, instr_ptr);
317 }
318
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800319 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700320 std::ostringstream opcode;
321 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800322 switch (op1) {
323 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800324 break;
325 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700326 if ((op2 & 0x64) == 0) { // 00x x0xx
327 // |111|11|10|00|0|00|0000|1111110000000000|
328 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
329 // |---|--|--|--|-|--|----|----------------|
330 // |332|22|22|22|2|22|1111|1111110000000000|
331 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
332 // |---|--|--|--|-|--|----|----------------|
333 // |111|01|00|op|0|WL| Rn | |
334 // |111|01| op2 | | |
335 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
336 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
337 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
338 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
339 uint32_t op = (instr >> 23) & 3;
340 uint32_t W = (instr >> 21) & 1;
341 uint32_t L = (instr >> 20) & 1;
342 ArmRegister Rn(instr, 16);
343 if (op == 1 || op == 2) {
344 if (op == 1) {
345 if (L == 0) {
346 opcode << "stm";
347 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800348 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700349 if (Rn.r != 13) {
350 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700351 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700352 } else {
353 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800354 }
355 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700356 } else {
357 if (L == 0) {
358 if (Rn.r != 13) {
359 opcode << "stmdb";
360 args << Rn << (W == 0 ? "" : "!") << ", ";
361 } else {
362 opcode << "push";
363 }
364 } else {
365 opcode << "ldmdb";
366 args << Rn << (W == 0 ? "" : "!") << ", ";
367 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800368 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700369 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800370 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700371 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700372 uint32_t op3 = (instr >> 23) & 3;
373 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700374 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700375 ArmRegister Rn(instr, 16);
376 ArmRegister Rt(instr, 12);
377 uint32_t imm8 = instr & 0xFF;
378 if (op3 == 0 && op4 == 0) { // STREX
379 ArmRegister Rd(instr, 8);
380 opcode << "strex";
381 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
382 } else if (op3 == 0 && op4 == 1) { // LDREX
383 opcode << "ldrex";
384 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
385 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700386 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
387 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100388 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
389 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
390 // |---|----|----|-|----|----|----|--|--|----|
391 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
392 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
393 // |---|----|----|-|----|----|----|--|--|----|
394 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700395 uint32_t op3 = (instr >> 21) & 0xF;
396 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100397 uint32_t imm3 = ((instr >> 12) & 0x7);
398 uint32_t imm2 = ((instr >> 6) & 0x3);
399 uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F;
400 uint32_t shift_type = ((instr >> 4) & 0x2);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700401 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100402 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700403 ArmRegister Rm(instr, 0);
404 switch (op3) {
405 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100406 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700407 opcode << "and";
408 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700409 if (S != 1U) {
410 opcode << "UNKNOWN TST-" << S;
411 break;
412 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700413 opcode << "tst";
414 S = 0; // don't print 's'
415 }
416 break;
417 case 0x1: opcode << "bic"; break;
418 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100419 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700420 opcode << "orr";
421 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100422 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700423 opcode << "mov";
424 }
425 break;
426 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100427 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700428 opcode << "orn";
429 } else {
430 opcode << "mvn";
431 }
432 break;
433 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100434 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700435 opcode << "eor";
436 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700437 if (S != 1U) {
438 opcode << "UNKNOWN TEQ-" << S;
439 break;
440 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700441 opcode << "teq";
442 S = 0; // don't print 's'
443 }
444 break;
445 case 0x6: opcode << "pkh"; break;
446 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100447 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700448 opcode << "add";
449 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700450 if (S != 1U) {
451 opcode << "UNKNOWN CMN-" << S;
452 break;
453 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700454 opcode << "cmn";
455 S = 0; // don't print 's'
456 }
457 break;
458 case 0xA: opcode << "adc"; break;
459 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100460 case 0xD:
461 if (Rd.r != 0xF) {
462 opcode << "sub";
463 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700464 if (S != 1U) {
465 opcode << "UNKNOWN CMP-" << S;
466 break;
467 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100468 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100469 S = 0; // don't print 's'
470 }
471 break;
472 case 0xE: opcode << "rsb"; break;
473 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700474 }
Ian Rogers087b2412012-03-21 01:30:32 -0700475
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700476 if (S == 1) {
477 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700478 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700479 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100480
481 if (Rd.r != 0xF) {
482 args << Rd << ", ";
483 }
484 if (Rn.r != 0xF) {
485 args << Rn << ", ";
486 }
487 args << Rm;
488
489 // Shift operand.
490 bool noShift = (imm5 == 0 && shift_type != 0x3);
491 if (!noShift) {
492 args << ", ";
493 switch (shift_type) {
494 case 0x0: args << "lsl"; break;
495 case 0x1: args << "lsr"; break;
496 case 0x2: args << "asr"; break;
497 case 0x3:
498 if (imm5 == 0) {
499 args << "rrx";
500 } else {
501 args << "ror";
502 }
503 break;
504 }
505 if (shift_type != 0x3 /* rrx */) {
506 args << StringPrintf(" #%d", imm5);
507 }
508 }
509
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700510 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
511 // Co-processor instructions
512 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
513 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
514 // |---|-|--|------|----|----|----|---|---|----|
515 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
516 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
517 // |---|-|--|------|----|----|----|---|---|----|
518 // |111| |11| op3 | Rn | |copr| |op4| |
519 uint32_t op3 = (instr >> 20) & 0x3F;
520 uint32_t coproc = (instr >> 8) & 0xF;
521 uint32_t op4 = (instr >> 4) & 0x1;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700522 if ((op3 == 2 || op3 == 2 || op3 == 6 || op3 == 7) || // 00x1x
523 (op3 >= 8 && op3 <= 15) || (op3 >= 16 && op3 <= 31)) { // 001xxx, 01xxxx
Ian Rogers9af89402012-09-07 11:29:35 -0700524 // Extension register load/store instructions
525 // |111|1|110|00000|0000|1111|110|000000000|
526 // |5 3|2|109|87654|3 0|54 2|10 |87 54 0|
527 // |---|-|---|-----|----|----|---|---------|
528 // |332|2|222|22222|1111|1111|110|000000000|
529 // |1 9|8|765|43210|9 6|54 2|10 |87 54 0|
530 // |---|-|---|-----|----|----|---|---------|
531 // |111|T|110| op3 | Rn | |101| |
532 // 111 0 110 01001 0011 0000 101 000000011 - ec930a03
533 if (op3 == 9 || op3 == 0xD) { // VLDM
534 // 1110 110 PUDW1 nnnn dddd 101S iiii iiii
535 uint32_t P = (instr >> 24) & 1;
536 uint32_t U = (instr >> 23) & 1;
537 uint32_t D = (instr >> 22) & 1;
538 uint32_t W = (instr >> 21) & 1;
539 uint32_t S = (instr >> 8) & 1;
540 ArmRegister Rn(instr, 16);
541 uint32_t Vd = (instr >> 12) & 0xF;
542 uint32_t imm8 = instr & 0xFF;
543 uint32_t d = (S == 0 ? ((Vd << 1) | D) : (Vd | (D << 4)));
544 if (P == 0 && U == 0 && W == 0) {
545 // TODO: 64bit transfers between ARM core and extension registers.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700546 } else if (P == 0 && U == 1 && Rn.r == 13) { // VPOP
Ian Rogers9af89402012-09-07 11:29:35 -0700547 opcode << "vpop" << (S == 0 ? ".f64" : ".f32");
548 args << d << " .. " << (d + imm8);
549 } else if (P == 1 && W == 0) { // VLDR
550 opcode << "vldr" << (S == 0 ? ".f64" : ".f32");
551 args << d << ", [" << Rn << ", #" << imm8 << "]";
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700552 } else { // VLDM
Ian Rogers9af89402012-09-07 11:29:35 -0700553 opcode << "vldm" << (S == 0 ? ".f64" : ".f32");
554 args << Rn << ", " << d << " .. " << (d + imm8);
555 }
556 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700557 } else if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700558 if ((coproc & 0xE) == 0xA) {
559 // VFP data-processing instructions
560 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
561 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
562 // |---|-|----|----|----|----|---|-|----|-|-|----|
563 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
564 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
565 // |---|-|----|----|----|----|---|-|----|-|-|----|
566 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
567 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
568 uint32_t opc1 = (instr >> 20) & 0xF;
569 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700570 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700571 if ((opc1 & 0xB) == 0xB) { // 1x11
572 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700573 uint32_t D = (instr >> 22) & 0x1;
574 uint32_t Vd = (instr >> 12) & 0xF;
575 uint32_t sz = (instr >> 8) & 1;
576 uint32_t M = (instr >> 5) & 1;
577 uint32_t Vm = instr & 0xF;
578 bool dp_operation = sz == 1;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700579 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700580 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700581 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
582 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (dp_operation ? ".f64" : ".f32");
583 if (dp_operation) {
584 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
585 } else {
586 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
587 }
588 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700589 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700590 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Ian Rogers0183dd72012-09-17 23:06:51 -0700591 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (dp_operation ? ".f64" : ".f32");
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700592 if (dp_operation) {
593 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
594 } else {
595 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
596 }
597 break;
598 }
599 }
600 }
601 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700602 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
603 // Advanced SIMD
604 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
605 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
606 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
607 uint32_t D = (instr >> 22) & 1;
608 uint32_t Vd = (instr >> 12) & 0xF;
609 uint32_t sz = (instr >> 8) & 1;
610 uint32_t M = (instr >> 5) & 1;
611 uint32_t Vm = instr & 0xF;
612 bool dp_operation = sz == 1;
613 opcode << "vsqrt" << (dp_operation ? ".f64" : ".f32");
614 if (dp_operation) {
615 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
616 } else {
617 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
618 }
619 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700620 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800621 }
622 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800623 case 2:
624 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
625 // Data-processing (modified immediate)
626 // |111|11|10|0000|0|0000|1|111|1100|00000000|
627 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
628 // |---|--|--|----|-|----|-|---|----|--------|
629 // |332|22|22|2222|2|1111|1|111|1100|00000000|
630 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
631 // |---|--|--|----|-|----|-|---|----|--------|
632 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
633 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800634 uint32_t i = (instr >> 26) & 1;
635 uint32_t op3 = (instr >> 21) & 0xF;
636 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700637 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800638 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700639 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800640 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800641 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
642 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
643 if (op3 == 0x2) {
644 opcode << "mov";
645 if (S == 1) {
646 opcode << "s";
647 }
648 opcode << ".w";
649 } else {
650 opcode << "mvn";
651 if (S == 1) {
652 opcode << "s";
653 }
654 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700655 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800656 } else if (Rd.r == 0xF && S == 1 &&
657 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
658 if (op3 == 0x0) {
659 opcode << "tst";
660 } else if (op3 == 0x4) {
661 opcode << "teq";
662 } else if (op3 == 0x8) {
663 opcode << "cmw";
664 } else {
665 opcode << "cmp.w";
666 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700667 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800668 } else {
669 switch (op3) {
670 case 0x0: opcode << "and"; break;
671 case 0x1: opcode << "bic"; break;
672 case 0x2: opcode << "orr"; break;
673 case 0x3: opcode << "orn"; break;
674 case 0x4: opcode << "eor"; break;
675 case 0x8: opcode << "add"; break;
676 case 0xA: opcode << "adc"; break;
677 case 0xB: opcode << "sbc"; break;
678 case 0xD: opcode << "sub"; break;
679 case 0xE: opcode << "rsb"; break;
680 default: opcode << "UNKNOWN DPMI-" << op3; break;
681 }
682 if (S == 1) {
683 opcode << "s";
684 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700685 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800686 }
Ian Rogers40627db2012-03-04 17:31:09 -0800687 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
688 // Data-processing (plain binary immediate)
689 // |111|11|10|00000|0000|1|111110000000000|
690 // |5 3|21|09|87654|3 0|5|4 0 5 0|
691 // |---|--|--|-----|----|-|---------------|
692 // |332|22|22|22222|1111|1|111110000000000|
693 // |1 9|87|65|43210|9 6|5|4 0 5 0|
694 // |---|--|--|-----|----|-|---------------|
695 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
696 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800697 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -0800698 case 0x00: case 0x0A: {
699 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -0700700 ArmRegister Rd(instr, 8);
701 ArmRegister Rn(instr, 16);
702 uint32_t i = (instr >> 26) & 1;
703 uint32_t imm3 = (instr >> 12) & 0x7;
704 uint32_t imm8 = instr & 0xFF;
705 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
706 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -0800707 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -0700708 args << Rd << ", " << Rn << ", #" << imm12;
709 } else {
710 opcode << "adr";
711 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -0800712 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -0700713 }
714 break;
715 }
Ian Rogers55019132013-02-08 01:05:23 -0800716 case 0x04: case 0x0C: {
717 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700718 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800719 uint32_t i = (instr >> 26) & 1;
720 uint32_t imm3 = (instr >> 12) & 0x7;
721 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700722 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800723 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -0800724 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700725 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800726 break;
727 }
jeffhaoeae26912013-01-28 16:29:54 -0800728 case 0x16: {
729 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
730 ArmRegister Rd(instr, 8);
731 ArmRegister Rn(instr, 16);
732 uint32_t msb = instr & 0x1F;
733 uint32_t imm2 = (instr >> 6) & 0x3;
734 uint32_t imm3 = (instr >> 12) & 0x7;
735 uint32_t lsb = (imm3 << 2) | imm2;
736 uint32_t width = msb - lsb + 1;
737 if (Rn.r != 0xF) {
738 opcode << "bfi";
739 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
740 } else {
741 opcode << "bfc";
742 args << Rd << ", #" << lsb << ", #" << width;
743 }
744 break;
745 }
Ian Rogers40627db2012-03-04 17:31:09 -0800746 default:
747 break;
748 }
749 } else {
750 // Branches and miscellaneous control
751 // |111|11|1000000|0000|1|111|1100|00000000|
752 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
753 // |---|--|-------|----|-|---|----|--------|
754 // |332|22|2222222|1111|1|111|1100|00000000|
755 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
756 // |---|--|-------|----|-|---|----|--------|
757 // |111|10| op2 | |1|op3|op4 | |
758
759 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700760 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800761 switch (op3) {
762 case 0:
763 if ((op2 & 0x38) != 0x38) {
764 // Conditional branch
765 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
766 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
767 // |---|--|-|----|------|-|-|--|-|--|-----------|
768 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
769 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
770 // |---|--|-|----|------|-|-|--|-|--|-----------|
771 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
772 uint32_t S = (instr >> 26) & 1;
773 uint32_t J2 = (instr >> 11) & 1;
774 uint32_t J1 = (instr >> 13) & 1;
775 uint32_t imm6 = (instr >> 16) & 0x3F;
776 uint32_t imm11 = instr & 0x7FF;
777 uint32_t cond = (instr >> 22) & 0xF;
778 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
779 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700780 opcode << "b";
781 DumpCond(opcode, cond);
782 opcode << ".w";
783 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -0700784 } else if (op2 == 0x3B) {
785 // Miscellaneous control instructions
786 uint32_t op5 = (instr >> 4) & 0xF;
787 switch (op5) {
788 case 4: opcode << "dsb"; break;
789 case 5: opcode << "dmb"; break;
790 case 6: opcode << "isb"; break;
791 }
Ian Rogers40627db2012-03-04 17:31:09 -0800792 }
793 break;
794 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -0800795 if ((op2 & 0x38) == 0x38) {
796 if (op2 == 0x7F) {
797 opcode << "udf";
798 }
799 break;
800 }
801 // Else deliberate fall-through to B.
802 case 1: case 3: {
803 // B
804 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
805 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
806 // |---|--|-|----|------|--|--|-|--|-----------|
807 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
808 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
809 // |---|--|-|----|------|--|--|-|--|-----------|
810 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
811 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
812 uint32_t S = (instr >> 26) & 1;
813 uint32_t cond = (instr >> 22) & 0xF;
814 uint32_t J2 = (instr >> 11) & 1;
815 uint32_t form = (instr >> 12) & 1;
816 uint32_t J1 = (instr >> 13) & 1;
817 uint32_t imm10 = (instr >> 16) & 0x3FF;
818 uint32_t imm6 = (instr >> 16) & 0x3F;
819 uint32_t imm11 = instr & 0x7FF;
820 opcode << "b";
821 int32_t imm32;
822 if (form == 0) {
823 DumpCond(opcode, cond);
824 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
825 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
826 } else {
827 uint32_t I1 = ~(J1 ^ S);
828 uint32_t I2 = ~(J2 ^ S);
829 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
830 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
831 }
832 opcode << ".w";
833 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800834 break;
Ian Rogersd0876a92013-02-08 11:30:38 -0800835 }
Ian Rogers40627db2012-03-04 17:31:09 -0800836 case 4: case 6: case 5: case 7: {
837 // BL, BLX (immediate)
838 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
839 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
840 // |---|--|-|----------|--|--|-|--|-----------|
841 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
842 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
843 // |---|--|-|----------|--|--|-|--|-----------|
844 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
845 uint32_t S = (instr >> 26) & 1;
846 uint32_t J2 = (instr >> 11) & 1;
847 uint32_t L = (instr >> 12) & 1;
848 uint32_t J1 = (instr >> 13) & 1;
849 uint32_t imm10 = (instr >> 16) & 0x3FF;
850 uint32_t imm11 = instr & 0x7FF;
851 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700852 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800853 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700854 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800855 }
856 uint32_t I1 = ~(J1 ^ S);
857 uint32_t I2 = ~(J2 ^ S);
858 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
859 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700860 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800861 break;
862 }
863 }
864 }
865 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800866 case 3:
867 switch (op2) {
868 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
869 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
870 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800871 // |111|11|100|000|0|0000|1111|110000|000000|
872 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
873 // |---|--|---|---|-|----|----|------|------|
874 // |332|22|222|222|2|1111|1111|110000|000000|
875 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
876 // |---|--|---|---|-|----|----|------|------|
877 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800878 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700879 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800880 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700881 case 0x0: case 0x4: {
882 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
883 // 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 -0700884 ArmRegister Rn(instr, 16);
885 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700886 opcode << "strb";
887 if ((instr & 0x800) != 0) {
888 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700889 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700890 } else {
891 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700892 ArmRegister Rm(instr, 0);
893 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700894 if (imm2 != 0) {
895 args << ", " << "lsl #" << imm2;
896 }
897 args << "]";
898 }
899 break;
900 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800901 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700902 ArmRegister Rn(instr, 16);
903 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800904 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700905 if ((instr & 0x800) != 0) {
906 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
907 uint32_t P = (instr >> 10) & 1;
908 uint32_t U = (instr >> 9) & 1;
909 uint32_t W = (instr >> 8) & 1;
910 uint32_t imm8 = instr & 0xFF;
911 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
912 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
913 opcode << "push";
914 args << Rt;
915 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
916 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800917 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700918 if (P == 1 && U == 1 && W == 0) {
919 opcode << "strt";
920 } else {
921 opcode << "str";
922 }
923 args << Rt << ", [" << Rn;
924 if (P == 0 && W == 1) {
925 args << "], #" << imm32;
926 } else {
927 args << ", #" << imm32 << "]";
928 if (W == 1) {
929 args << "!";
930 }
Ian Rogers40627db2012-03-04 17:31:09 -0800931 }
932 }
Ian Rogers66a3fca2012-04-09 19:51:34 -0700933 } else {
934 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
935 ArmRegister Rn(instr, 16);
936 ArmRegister Rt(instr, 12);
937 ArmRegister Rm(instr, 0);
938 uint32_t imm2 = (instr >> 4) & 3;
939 opcode << "str.w";
940 args << Rt << ", [" << Rn << ", " << Rm;
941 if (imm2 != 0) {
942 args << ", lsl #" << imm2;
943 }
944 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800945 }
946 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700947 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800948 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700949 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700950 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800951 }
Ian Rogers40627db2012-03-04 17:31:09 -0800952 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800953 }
954 }
955
956 break;
957 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700958 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -0800959 // Load halfword
960 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
961 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
962 // |---|--|--|---|--|-|----|----|------|------|
963 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
964 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
965 // |---|--|--|---|--|-|----|----|------|------|
966 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
967 // |111|11| op2 | | | imm12 |
968 uint32_t op3 = (instr >> 23) & 3;
969 ArmRegister Rn(instr, 16);
970 ArmRegister Rt(instr, 12);
971 if (Rt.r != 15) {
972 if (op3 == 1) {
973 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
974 uint32_t imm12 = instr & 0xFFF;
975 opcode << "ldrh.w";
976 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
977 if (Rn.r == 9) {
978 args << " ; ";
979 Thread::DumpThreadOffset(args, imm12, 4);
980 } else if (Rn.r == 15) {
981 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
982 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
983 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
984 }
985 } else if (op3 == 3) {
986 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
987 uint32_t imm12 = instr & 0xFFF;
988 opcode << "ldrsh.w";
989 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
990 if (Rn.r == 9) {
991 args << " ; ";
992 Thread::DumpThreadOffset(args, imm12, 4);
993 } else if (Rn.r == 15) {
994 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
995 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
996 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
997 }
998 }
999 }
1000 break;
1001 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001002 case 0x29: { // 0101001
1003 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1004 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1005 // |---|--|-------|----|----|----|--|---|----|
1006 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1007 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1008 // |---|--|-------|----|----|----|--|---|----|
1009 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1010 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1011 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1012 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1013 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1014 if ((instr & 0xf0c0) == 0xf080) {
1015 uint32_t op3 = (instr >> 4) & 3;
1016 opcode << kThumbReverseOperations[op3];
1017 ArmRegister Rm(instr, 0);
1018 ArmRegister Rd(instr, 8);
1019 args << Rd << ", " << Rm;
1020 ArmRegister Rm2(instr, 16);
1021 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1022 args << " (UNPREDICTABLE)";
1023 }
1024 } // else unknown instruction
1025 break;
1026 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001027 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001028 // Load word
1029 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1030 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1031 // |---|--|--|---|--|-|----|----|------|------|
1032 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1033 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1034 // |---|--|--|---|--|-|----|----|------|------|
1035 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1036 // |111|11| op2 | | | imm12 |
1037 uint32_t op3 = (instr >> 23) & 3;
1038 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001039 ArmRegister Rn(instr, 16);
1040 ArmRegister Rt(instr, 12);
1041 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001042 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1043 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1044 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001045 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001046 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001047 if (Rn.r == 9) {
1048 args << " ; ";
1049 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001050 } else if (Rn.r == 15) {
1051 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1052 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1053 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001054 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001055 } else if (op4 == 0) {
1056 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1057 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001058 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001059 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001060 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001061 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001062 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001063 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001064 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001065 } else {
1066 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1067 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001068 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001069 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001070 }
1071 break;
1072 }
1073 }
1074 default:
1075 break;
1076 }
Ian Rogers9af89402012-09-07 11:29:35 -07001077
1078 // Apply any IT-block conditions to the opcode if necessary.
1079 if (!it_conditions_.empty()) {
1080 opcode << it_conditions_.back();
1081 it_conditions_.pop_back();
1082 }
1083
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001084 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001085 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001086} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001087
1088size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1089 uint16_t instr = ReadU16(instr_ptr);
1090 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1091 if (is_32bit) {
1092 return DumpThumb32(os, instr_ptr);
1093 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001094 std::ostringstream opcode;
1095 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001096 uint16_t opcode1 = instr >> 10;
1097 if (opcode1 < 0x10) {
1098 // shift (immediate), add, subtract, move, and compare
1099 uint16_t opcode2 = instr >> 9;
1100 switch (opcode2) {
1101 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1102 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001103 // Logical shift left - 00 000xx iii mmm ddd
1104 // Logical shift right - 00 001xx iii mmm ddd
1105 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001106 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001107 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001108 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001109 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001110 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001111 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001112 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001113 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001114 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001115 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001116 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001117 break;
1118 }
1119 case 0xC: case 0xD: case 0xE: case 0xF: {
1120 // Add register - 00 01100 mmm nnn ddd
1121 // Sub register - 00 01101 mmm nnn ddd
1122 // Add 3-bit immediate - 00 01110 iii nnn ddd
1123 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1124 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001125 ThumbRegister Rn(instr, 3);
1126 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001127 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001128 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001129 } else {
1130 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001131 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001132 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001133 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001134 }
1135 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001136 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001137 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001138 ArmRegister Rm(imm3_or_Rm);
1139 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001140 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001141 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001142 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001143 break;
1144 }
1145 case 0x10: case 0x11: case 0x12: case 0x13:
1146 case 0x14: case 0x15: case 0x16: case 0x17:
1147 case 0x18: case 0x19: case 0x1A: case 0x1B:
1148 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1149 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1150 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1151 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1152 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001153 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001154 uint16_t imm8 = instr & 0xFF;
1155 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001156 case 4: opcode << "movs"; break;
1157 case 5: opcode << "cmp"; break;
1158 case 6: opcode << "adds"; break;
1159 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001160 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001161 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001162 break;
1163 }
1164 default:
1165 break;
1166 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001167 } else if (opcode1 == 0x10) {
1168 // Data-processing
1169 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001170 ThumbRegister rm(instr, 3);
1171 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001172 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001173 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001174 } else if (opcode1 == 0x11) {
1175 // Special data instructions and branch and exchange
1176 uint16_t opcode2 = (instr >> 6) & 0x0F;
1177 switch (opcode2) {
1178 case 0x0: case 0x1: case 0x2: case 0x3: {
1179 // Add low registers - 010001 0000 xxxxxx
1180 // Add high registers - 010001 0001/001x xxxxxx
1181 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001182 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001183 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001184 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001185 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001186 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001187 break;
1188 }
1189 case 0x8: case 0x9: case 0xA: case 0xB: {
1190 // Move low registers - 010001 1000 xxxxxx
1191 // Move high registers - 010001 1001/101x xxxxxx
1192 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001193 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001194 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001195 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001196 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001197 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001198 break;
1199 }
1200 case 0x5: case 0x6: case 0x7: {
1201 // Compare high registers - 010001 0101/011x xxxxxx
1202 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001203 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001204 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001205 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001206 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001207 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001208 break;
1209 }
1210 case 0xC: case 0xD: case 0xE: case 0xF: {
1211 // Branch and exchange - 010001 110x xxxxxx
1212 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001213 ArmRegister rm(instr, 3);
1214 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1215 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001216 break;
1217 }
1218 default:
1219 break;
1220 }
jeffhaoeae26912013-01-28 16:29:54 -08001221 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1222 ThumbRegister Rt(instr, 8);
1223 uint16_t imm8 = instr & 0xFF;
1224 opcode << "ldr";
1225 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001226 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1227 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1228 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1229 // Load/store single data item
1230 uint16_t opA = (instr >> 12) & 0xF;
1231 if (opA == 0x5) {
1232 uint16_t opB = (instr >> 9) & 0x7;
1233 ThumbRegister Rm(instr, 6);
1234 ThumbRegister Rn(instr, 3);
1235 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001236 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001237 case 0: opcode << "str"; break;
1238 case 1: opcode << "strh"; break;
1239 case 2: opcode << "strb"; break;
1240 case 3: opcode << "ldrsb"; break;
1241 case 4: opcode << "ldr"; break;
1242 case 5: opcode << "ldrh"; break;
1243 case 6: opcode << "ldrb"; break;
1244 case 7: opcode << "ldrsh"; break;
1245 }
1246 args << Rt << ", [" << Rn << ", " << Rm << "]";
1247 } else if (opA == 9) {
1248 uint16_t opB = (instr >> 11) & 1;
1249 ThumbRegister Rt(instr, 8);
1250 uint16_t imm8 = instr & 0xFF;
1251 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001252 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001253 } else {
1254 uint16_t imm5 = (instr >> 6) & 0x1F;
1255 uint16_t opB = (instr >> 11) & 1;
1256 ThumbRegister Rn(instr, 3);
1257 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001258 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001259 case 6:
1260 imm5 <<= 2;
1261 opcode << (opB == 0 ? "str" : "ldr");
1262 break;
1263 case 7:
1264 imm5 <<= 0;
1265 opcode << (opB == 0 ? "strb" : "ldrb");
1266 break;
1267 case 8:
1268 imm5 <<= 1;
1269 opcode << (opB == 0 ? "strh" : "ldrh");
1270 break;
1271 }
1272 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1273 }
jeffhaoeae26912013-01-28 16:29:54 -08001274 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001275 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001276 uint32_t cond = (instr >> 8) & 0xF;
1277 opcode << "b";
1278 DumpCond(opcode, cond);
1279 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001280 } else if ((instr & 0xF800) == 0xA800) {
1281 // Generate SP-relative address
1282 ThumbRegister rd(instr, 8);
1283 int imm8 = instr & 0xFF;
1284 opcode << "add";
1285 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001286 } else if ((instr & 0xF000) == 0xB000) {
1287 // Miscellaneous 16-bit instructions
1288 uint16_t opcode2 = (instr >> 5) & 0x7F;
1289 switch (opcode2) {
1290 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1291 // Add immediate to SP - 1011 00000 ii iiiii
1292 // Subtract immediate from SP - 1011 00001 ii iiiii
1293 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001294 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001295 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001296 break;
1297 }
Ian Rogers087b2412012-03-21 01:30:32 -07001298 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001299 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001300 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1301 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001302 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001303 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1304 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1305 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001306 // CBNZ, CBZ
1307 uint16_t op = (instr >> 11) & 1;
1308 uint16_t i = (instr >> 9) & 1;
1309 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001310 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001311 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001312 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001313 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001314 DumpBranchTarget(args, instr_ptr + 4, imm32);
1315 break;
1316 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001317 case 0x50: case 0x51: // 101000x
1318 case 0x52: case 0x53: // 101001x
1319 case 0x56: case 0x57: { // 101011x
1320 uint16_t op = (instr >> 6) & 3;
1321 opcode << kThumbReverseOperations[op];
1322 ThumbRegister Rm(instr, 3);
1323 ThumbRegister Rd(instr, 0);
1324 args << Rd << ", " << Rm;
1325 break;
1326 }
Ian Rogers40627db2012-03-04 17:31:09 -08001327 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1328 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1329 // If-Then, and hints
1330 uint16_t opA = (instr >> 4) & 0xF;
1331 uint16_t opB = instr & 0xF;
1332 if (opB == 0) {
1333 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001334 case 0: opcode << "nop"; break;
1335 case 1: opcode << "yield"; break;
1336 case 2: opcode << "wfe"; break;
1337 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001338 default: break;
1339 }
1340 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001341 uint32_t first_cond = opA;
1342 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001343 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001344
1345 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1346 // and store up the actual condition codes we'll want to add to the next few opcodes.
1347 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001348 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001349 for (size_t i = 0; i < count; ++i) {
1350 bool positive_cond = ((first_cond & 1) != 0);
1351 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1352 if (positive_mask == positive_cond) {
1353 opcode << 't';
1354 it_conditions_[i] = kConditionCodeNames[first_cond];
1355 } else {
1356 opcode << 'e';
1357 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1358 }
1359 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001360 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001361
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001362 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1363 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001364 }
1365 break;
1366 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001367 default:
1368 break;
1369 }
1370 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1371 ((instr & 0xE000) == 0x8000)) {
1372 // Load/store single data item
1373 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001374 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001375 switch (opA) {
1376 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001377 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1378 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001379 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001380 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001381 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001382 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1383 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001384 break;
1385 }
1386 case 0x9: {
1387 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1388 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1389 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001390 ThumbRegister Rt(instr, 8);
1391 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1392 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001393 break;
1394 }
1395 default:
1396 break;
1397 }
Ian Rogers40627db2012-03-04 17:31:09 -08001398 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1399 uint16_t imm11 = instr & 0x7FFF;
1400 int32_t imm32 = imm11 << 1;
1401 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001402 opcode << "b";
1403 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001404 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001405
1406 // Apply any IT-block conditions to the opcode if necessary.
1407 if (!it_conditions_.empty()) {
1408 opcode << it_conditions_.back();
1409 it_conditions_.pop_back();
1410 }
1411
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001412 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001413 }
1414 return 2;
1415}
1416
1417} // namespace arm
1418} // namespace art