blob: 936fb07728bd3b70adbc5ccf921b1d58ea5491fc [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) << "]";
443 } else { // op3 is 01, op4 is 00
444 // this is one of strexb, strexh or strexd
445 int op5 = (instr >> 4) & 0xf;
446 switch (op5) {
447 case 4:
448 opcode << "strexb";
449 break;
450 case 5:
451 opcode << "strexh";
452 break;
453 case 7:
454 opcode << "strexd";
455 break;
456 }
457 }
458 break;
459 case 1:
460 if (op3 == 0) { // op3 is 00, op4 is 01
461 opcode << "ldrex";
462 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
463 } else { // op3 is 01, op4 is 01
464 // this is one of strexb, strexh or strexd
465 int op5 = (instr >> 4) & 0xf;
466 switch (op5) {
467 case 0:
468 opcode << "tbb";
469 break;
470 case 1:
471 opcode << "tbh";
472 break;
473 case 4:
474 opcode << "ldrexb";
475 break;
476 case 5:
477 opcode << "ldrexh";
478 break;
479 case 7:
480 opcode << "ldrexd";
481 break;
482 }
483 }
484 break;
485 case 2: // op3 is 0x, op4 is 10
486 case 3: // op3 is 0x, op4 is 11
487 if (op4 == 2) {
488 opcode << "strd";
489 } else {
490 opcode << "ldrd";
491 }
492 int W = (instr >> 21) & 1;
493 int U = (instr >> 23) & 1;
494 int P = (instr >> 24) & 1;
495
496 args << Rt << "," << Rd << ", [" << Rn;
497 const char *sign = U ? "+" : "-";
498 if (P == 0 && W == 1) {
499 args << "], #" << sign << imm8;
500 } else {
501 args << ", #" << sign << imm8 << "]";
502 if (W == 1) {
503 args << "!";
504 }
505 }
506 break;
507 }
508 }
509
510
Ian Rogers9af89402012-09-07 11:29:35 -0700511 if (op3 == 0 && op4 == 0) { // STREX
512 ArmRegister Rd(instr, 8);
513 opcode << "strex";
514 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
515 } else if (op3 == 0 && op4 == 1) { // LDREX
516 opcode << "ldrex";
517 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
518 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700519 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
520 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100521 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
522 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
523 // |---|----|----|-|----|----|----|--|--|----|
524 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
525 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
526 // |---|----|----|-|----|----|----|--|--|----|
527 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700528 uint32_t op3 = (instr >> 21) & 0xF;
529 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100530 uint32_t imm3 = ((instr >> 12) & 0x7);
531 uint32_t imm2 = ((instr >> 6) & 0x3);
532 uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F;
533 uint32_t shift_type = ((instr >> 4) & 0x2);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700534 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100535 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700536 ArmRegister Rm(instr, 0);
537 switch (op3) {
538 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100539 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700540 opcode << "and";
541 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700542 if (S != 1U) {
543 opcode << "UNKNOWN TST-" << S;
544 break;
545 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700546 opcode << "tst";
547 S = 0; // don't print 's'
548 }
549 break;
550 case 0x1: opcode << "bic"; break;
551 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100552 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700553 opcode << "orr";
554 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100555 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700556 opcode << "mov";
557 }
558 break;
559 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100560 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700561 opcode << "orn";
562 } else {
563 opcode << "mvn";
564 }
565 break;
566 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100567 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700568 opcode << "eor";
569 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700570 if (S != 1U) {
571 opcode << "UNKNOWN TEQ-" << S;
572 break;
573 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700574 opcode << "teq";
575 S = 0; // don't print 's'
576 }
577 break;
578 case 0x6: opcode << "pkh"; break;
579 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100580 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700581 opcode << "add";
582 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700583 if (S != 1U) {
584 opcode << "UNKNOWN CMN-" << S;
585 break;
586 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700587 opcode << "cmn";
588 S = 0; // don't print 's'
589 }
590 break;
591 case 0xA: opcode << "adc"; break;
592 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100593 case 0xD:
594 if (Rd.r != 0xF) {
595 opcode << "sub";
596 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700597 if (S != 1U) {
598 opcode << "UNKNOWN CMP-" << S;
599 break;
600 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100601 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100602 S = 0; // don't print 's'
603 }
604 break;
605 case 0xE: opcode << "rsb"; break;
606 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700607 }
Ian Rogers087b2412012-03-21 01:30:32 -0700608
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700609 if (S == 1) {
610 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700611 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700612 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100613
614 if (Rd.r != 0xF) {
615 args << Rd << ", ";
616 }
617 if (Rn.r != 0xF) {
618 args << Rn << ", ";
619 }
620 args << Rm;
621
622 // Shift operand.
623 bool noShift = (imm5 == 0 && shift_type != 0x3);
624 if (!noShift) {
625 args << ", ";
626 switch (shift_type) {
627 case 0x0: args << "lsl"; break;
628 case 0x1: args << "lsr"; break;
629 case 0x2: args << "asr"; break;
630 case 0x3:
631 if (imm5 == 0) {
632 args << "rrx";
633 } else {
634 args << "ror";
635 }
636 break;
637 }
638 if (shift_type != 0x3 /* rrx */) {
639 args << StringPrintf(" #%d", imm5);
640 }
641 }
642
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700643 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
644 // Co-processor instructions
645 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
646 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
647 // |---|-|--|------|----|----|----|---|---|----|
648 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
649 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
650 // |---|-|--|------|----|----|----|---|---|----|
651 // |111| |11| op3 | Rn | |copr| |op4| |
652 uint32_t op3 = (instr >> 20) & 0x3F;
653 uint32_t coproc = (instr >> 8) & 0xF;
654 uint32_t op4 = (instr >> 4) & 0x1;
Dave Allison70202782013-10-22 17:52:19 -0700655
656 if (coproc == 10 || coproc == 11) { // 101x
Vladimir Markodd577a32013-11-07 19:25:24 +0000657 if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
658 // Extension register load/store instructions
659 // |1111|110|00000|0000|1111|110|0|00000000|
660 // |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
661 // |----|---|-----|----|----|---|-|--------|
662 // |3322|222|22222|1111|1111|110|0|00000000|
663 // |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
664 // |----|---|-----|----|----|---|-|--------|
665 // |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
Ian Rogers9af89402012-09-07 11:29:35 -0700666 uint32_t P = (instr >> 24) & 1;
667 uint32_t U = (instr >> 23) & 1;
Ian Rogers9af89402012-09-07 11:29:35 -0700668 uint32_t W = (instr >> 21) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000669 if (P == U && W == 1) {
670 opcode << "UNDEFINED";
671 } else {
672 uint32_t L = (instr >> 20) & 1;
673 uint32_t S = (instr >> 8) & 1;
674 ArmRegister Rn(instr, 16);
675 if (P == 1 && W == 0) { // VLDR
676 FpRegister d(instr, 12, 22);
677 uint32_t imm8 = instr & 0xFF;
678 opcode << (L == 1 ? "vldr" : "vstr");
679 args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
680 << (imm8 << 2) << "]";
681 } else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
682 opcode << (L == 1 ? "vpop" : "vpush");
683 args << FpRegisterRange(instr);
684 } else { // VLDM
685 opcode << (L == 1 ? "vldm" : "vstm");
686 args << Rn << ((W == 1) ? "!" : "") << ", "
687 << FpRegisterRange(instr);
Dave Allison70202782013-10-22 17:52:19 -0700688 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000689 opcode << (S == 1 ? ".f64" : ".f32");
Ian Rogers9af89402012-09-07 11:29:35 -0700690 }
Dave Allison70202782013-10-22 17:52:19 -0700691 } else if ((op3 >> 1) == 2) { // 00010x
Vladimir Markodd577a32013-11-07 19:25:24 +0000692 if ((instr & 0xD0) == 0x10) {
693 // 64bit transfers between ARM core and extension registers.
694 uint32_t L = (instr >> 20) & 1;
695 uint32_t S = (instr >> 8) & 1;
696 ArmRegister Rt2(instr, 16);
697 ArmRegister Rt(instr, 12);
698 FpRegister m(instr, 0, 5);
699 opcode << "vmov" << (S ? ".f64" : ".f32");
700 if (L == 1) {
701 args << Rt << ", " << Rt2 << ", ";
702 }
703 if (S) {
704 args << m;
705 } else {
706 args << m << ", " << FpRegister(m, 1);
707 }
708 if (L == 0) {
709 args << ", " << Rt << ", " << Rt2;
710 }
711 if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
712 (S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
713 args << " (UNPREDICTABLE)";
714 }
715 }
Dave Allison70202782013-10-22 17:52:19 -0700716 } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
717 // fp data processing
718 } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
Vladimir Markodd577a32013-11-07 19:25:24 +0000719 if (coproc == 10 && (op3 & 0xE) == 0) {
720 // VMOV (between ARM core register and single-precision register)
721 // |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
722 // |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
723 // |----|----|---|- |----|----|----|-|--|-|----|
724 // |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
725 // |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
726 // |----|----|---|- |----|----|----|-|--|-|----|
727 // |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
728 uint32_t op = op3 & 1;
729 ArmRegister Rt(instr, 12);
730 FpRegister n(instr, 16, 7);
731 opcode << "vmov.f32";
732 if (op) {
733 args << Rt << ", " << n;
734 } else {
735 args << n << ", " << Rt;
736 }
737 if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
738 args << " (UNPREDICTABLE)";
739 }
740 } else if (coproc == 10 && op3 == 0x2F) {
741 // VMRS
742 // |1111|11000000|0000|1111|1100|000|0|0000|
743 // |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
744 // |----|--------|----|----|----|---|-|----|
745 // |3322|22222222|1111|1111|1100|000|0|0000|
746 // |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
747 // |----|--------|----|----|----|---|-|----|
748 // |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
749 uint32_t spec_reg = (instr >> 16) & 0xF;
750 ArmRegister Rt(instr, 12);
751 opcode << "vmrs";
752 if (spec_reg == 1) {
753 if (Rt.r == 15) {
754 args << "APSR_nzcv, FPSCR";
755 } else if (Rt.r == 13) {
756 args << Rt << ", FPSCR (UNPREDICTABLE)";
757 } else {
758 args << Rt << ", FPSCR";
759 }
760 } else {
761 args << "(PRIVILEGED)";
762 }
763 } else if (coproc == 11 && (op3 & 0x9) != 8) {
764 // VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
765 }
Ian Rogers9af89402012-09-07 11:29:35 -0700766 }
Dave Allison70202782013-10-22 17:52:19 -0700767 }
768
769 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700770 if ((coproc & 0xE) == 0xA) {
771 // VFP data-processing instructions
772 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
773 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
774 // |---|-|----|----|----|----|---|-|----|-|-|----|
775 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
776 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
777 // |---|-|----|----|----|----|---|-|----|-|-|----|
778 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
779 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
780 uint32_t opc1 = (instr >> 20) & 0xF;
781 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700782 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700783 if ((opc1 & 0xB) == 0xB) { // 1x11
784 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700785 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000786 FpRegister d(instr, 12, 22);
787 FpRegister m(instr, 0, 5);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700788 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700789 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700790 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000791 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (sz == 1 ? ".f64" : ".f32");
792 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700793 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700794 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700795 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Vladimir Markodd577a32013-11-07 19:25:24 +0000796 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (sz == 1 ? ".f64" : ".f32");
797 args << d << ", " << m;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700798 break;
799 }
800 }
801 }
802 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700803 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
804 // Advanced SIMD
805 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
806 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
807 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
Ian Rogers0183dd72012-09-17 23:06:51 -0700808 uint32_t sz = (instr >> 8) & 1;
Vladimir Markodd577a32013-11-07 19:25:24 +0000809 FpRegister d(instr, 12, 22);
810 FpRegister m(instr, 0, 5);
811 opcode << "vsqrt" << (sz == 1 ? ".f64" : ".f32");
812 args << d << ", " << m;
Ian Rogers0183dd72012-09-17 23:06:51 -0700813 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700814 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800815 }
816 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800817 case 2:
818 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
819 // Data-processing (modified immediate)
820 // |111|11|10|0000|0|0000|1|111|1100|00000000|
821 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
822 // |---|--|--|----|-|----|-|---|----|--------|
823 // |332|22|22|2222|2|1111|1|111|1100|00000000|
824 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
825 // |---|--|--|----|-|----|-|---|----|--------|
826 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
827 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800828 uint32_t i = (instr >> 26) & 1;
829 uint32_t op3 = (instr >> 21) & 0xF;
830 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700831 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800832 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700833 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800834 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800835 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
836 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
837 if (op3 == 0x2) {
838 opcode << "mov";
839 if (S == 1) {
840 opcode << "s";
841 }
842 opcode << ".w";
843 } else {
844 opcode << "mvn";
845 if (S == 1) {
846 opcode << "s";
847 }
848 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700849 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800850 } else if (Rd.r == 0xF && S == 1 &&
851 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
852 if (op3 == 0x0) {
853 opcode << "tst";
854 } else if (op3 == 0x4) {
855 opcode << "teq";
856 } else if (op3 == 0x8) {
Vladimir Marko22479842013-11-19 17:04:50 +0000857 opcode << "cmn.w";
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800858 } else {
859 opcode << "cmp.w";
860 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700861 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800862 } else {
863 switch (op3) {
864 case 0x0: opcode << "and"; break;
865 case 0x1: opcode << "bic"; break;
866 case 0x2: opcode << "orr"; break;
867 case 0x3: opcode << "orn"; break;
868 case 0x4: opcode << "eor"; break;
869 case 0x8: opcode << "add"; break;
870 case 0xA: opcode << "adc"; break;
871 case 0xB: opcode << "sbc"; break;
872 case 0xD: opcode << "sub"; break;
873 case 0xE: opcode << "rsb"; break;
874 default: opcode << "UNKNOWN DPMI-" << op3; break;
875 }
876 if (S == 1) {
877 opcode << "s";
878 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700879 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800880 }
Ian Rogers40627db2012-03-04 17:31:09 -0800881 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
882 // Data-processing (plain binary immediate)
883 // |111|11|10|00000|0000|1|111110000000000|
884 // |5 3|21|09|87654|3 0|5|4 0 5 0|
885 // |---|--|--|-----|----|-|---------------|
886 // |332|22|22|22222|1111|1|111110000000000|
887 // |1 9|87|65|43210|9 6|5|4 0 5 0|
888 // |---|--|--|-----|----|-|---------------|
889 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
890 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800891 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -0800892 case 0x00: case 0x0A: {
893 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -0700894 ArmRegister Rd(instr, 8);
895 ArmRegister Rn(instr, 16);
896 uint32_t i = (instr >> 26) & 1;
897 uint32_t imm3 = (instr >> 12) & 0x7;
898 uint32_t imm8 = instr & 0xFF;
899 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
900 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -0800901 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -0700902 args << Rd << ", " << Rn << ", #" << imm12;
903 } else {
904 opcode << "adr";
905 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -0800906 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -0700907 }
908 break;
909 }
Ian Rogers55019132013-02-08 01:05:23 -0800910 case 0x04: case 0x0C: {
911 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700912 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800913 uint32_t i = (instr >> 26) & 1;
914 uint32_t imm3 = (instr >> 12) & 0x7;
915 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700916 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800917 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -0800918 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700919 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800920 break;
921 }
jeffhaoeae26912013-01-28 16:29:54 -0800922 case 0x16: {
923 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
924 ArmRegister Rd(instr, 8);
925 ArmRegister Rn(instr, 16);
926 uint32_t msb = instr & 0x1F;
927 uint32_t imm2 = (instr >> 6) & 0x3;
928 uint32_t imm3 = (instr >> 12) & 0x7;
929 uint32_t lsb = (imm3 << 2) | imm2;
930 uint32_t width = msb - lsb + 1;
931 if (Rn.r != 0xF) {
932 opcode << "bfi";
933 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
934 } else {
935 opcode << "bfc";
936 args << Rd << ", #" << lsb << ", #" << width;
937 }
938 break;
939 }
Ian Rogers40627db2012-03-04 17:31:09 -0800940 default:
941 break;
942 }
943 } else {
944 // Branches and miscellaneous control
945 // |111|11|1000000|0000|1|111|1100|00000000|
946 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
947 // |---|--|-------|----|-|---|----|--------|
948 // |332|22|2222222|1111|1|111|1100|00000000|
949 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
950 // |---|--|-------|----|-|---|----|--------|
951 // |111|10| op2 | |1|op3|op4 | |
952
953 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700954 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800955 switch (op3) {
956 case 0:
957 if ((op2 & 0x38) != 0x38) {
958 // Conditional branch
959 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
960 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
961 // |---|--|-|----|------|-|-|--|-|--|-----------|
962 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
963 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
964 // |---|--|-|----|------|-|-|--|-|--|-----------|
965 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
966 uint32_t S = (instr >> 26) & 1;
967 uint32_t J2 = (instr >> 11) & 1;
968 uint32_t J1 = (instr >> 13) & 1;
969 uint32_t imm6 = (instr >> 16) & 0x3F;
970 uint32_t imm11 = instr & 0x7FF;
971 uint32_t cond = (instr >> 22) & 0xF;
972 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
973 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700974 opcode << "b";
975 DumpCond(opcode, cond);
976 opcode << ".w";
977 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -0700978 } else if (op2 == 0x3B) {
979 // Miscellaneous control instructions
980 uint32_t op5 = (instr >> 4) & 0xF;
981 switch (op5) {
982 case 4: opcode << "dsb"; break;
983 case 5: opcode << "dmb"; break;
984 case 6: opcode << "isb"; break;
985 }
Ian Rogers40627db2012-03-04 17:31:09 -0800986 }
987 break;
988 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -0800989 if ((op2 & 0x38) == 0x38) {
990 if (op2 == 0x7F) {
991 opcode << "udf";
992 }
993 break;
994 }
995 // Else deliberate fall-through to B.
996 case 1: case 3: {
997 // B
998 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
999 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
1000 // |---|--|-|----|------|--|--|-|--|-----------|
1001 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
1002 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
1003 // |---|--|-|----|------|--|--|-|--|-----------|
1004 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
1005 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
1006 uint32_t S = (instr >> 26) & 1;
1007 uint32_t cond = (instr >> 22) & 0xF;
1008 uint32_t J2 = (instr >> 11) & 1;
1009 uint32_t form = (instr >> 12) & 1;
1010 uint32_t J1 = (instr >> 13) & 1;
1011 uint32_t imm10 = (instr >> 16) & 0x3FF;
1012 uint32_t imm6 = (instr >> 16) & 0x3F;
1013 uint32_t imm11 = instr & 0x7FF;
1014 opcode << "b";
1015 int32_t imm32;
1016 if (form == 0) {
1017 DumpCond(opcode, cond);
1018 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
1019 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
1020 } else {
1021 uint32_t I1 = ~(J1 ^ S);
1022 uint32_t I2 = ~(J2 ^ S);
1023 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1024 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
1025 }
1026 opcode << ".w";
1027 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001028 break;
Ian Rogersd0876a92013-02-08 11:30:38 -08001029 }
Ian Rogers40627db2012-03-04 17:31:09 -08001030 case 4: case 6: case 5: case 7: {
1031 // BL, BLX (immediate)
1032 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
1033 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
1034 // |---|--|-|----------|--|--|-|--|-----------|
1035 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
1036 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
1037 // |---|--|-|----------|--|--|-|--|-----------|
1038 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
1039 uint32_t S = (instr >> 26) & 1;
1040 uint32_t J2 = (instr >> 11) & 1;
1041 uint32_t L = (instr >> 12) & 1;
1042 uint32_t J1 = (instr >> 13) & 1;
1043 uint32_t imm10 = (instr >> 16) & 0x3FF;
1044 uint32_t imm11 = instr & 0x7FF;
1045 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001046 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -08001047 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001048 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -08001049 }
1050 uint32_t I1 = ~(J1 ^ S);
1051 uint32_t I2 = ~(J2 ^ S);
1052 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
1053 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -07001054 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -08001055 break;
1056 }
1057 }
1058 }
1059 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001060 case 3:
1061 switch (op2) {
1062 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
1063 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
1064 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -08001065 // |111|11|100|000|0|0000|1111|110000|000000|
1066 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
1067 // |---|--|---|---|-|----|----|------|------|
1068 // |332|22|222|222|2|1111|1111|110000|000000|
1069 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
1070 // |---|--|---|---|-|----|----|------|------|
1071 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001072 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001073 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001074 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -07001075 case 0x0: case 0x4: {
1076 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
1077 // 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 -07001078 ArmRegister Rn(instr, 16);
1079 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -07001080 opcode << "strb";
1081 if ((instr & 0x800) != 0) {
1082 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001083 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -07001084 } else {
1085 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001086 ArmRegister Rm(instr, 0);
1087 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -07001088 if (imm2 != 0) {
1089 args << ", " << "lsl #" << imm2;
1090 }
1091 args << "]";
1092 }
1093 break;
1094 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001095 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001096 ArmRegister Rn(instr, 16);
1097 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -08001098 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001099 if ((instr & 0x800) != 0) {
1100 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
1101 uint32_t P = (instr >> 10) & 1;
1102 uint32_t U = (instr >> 9) & 1;
1103 uint32_t W = (instr >> 8) & 1;
1104 uint32_t imm8 = instr & 0xFF;
1105 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
1106 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
1107 opcode << "push";
1108 args << Rt;
1109 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
1110 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -08001111 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001112 if (P == 1 && U == 1 && W == 0) {
1113 opcode << "strt";
1114 } else {
1115 opcode << "str";
1116 }
1117 args << Rt << ", [" << Rn;
1118 if (P == 0 && W == 1) {
1119 args << "], #" << imm32;
1120 } else {
1121 args << ", #" << imm32 << "]";
1122 if (W == 1) {
1123 args << "!";
1124 }
Ian Rogers40627db2012-03-04 17:31:09 -08001125 }
1126 }
Ian Rogers66a3fca2012-04-09 19:51:34 -07001127 } else {
1128 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
1129 ArmRegister Rn(instr, 16);
1130 ArmRegister Rt(instr, 12);
1131 ArmRegister Rm(instr, 0);
1132 uint32_t imm2 = (instr >> 4) & 3;
1133 opcode << "str.w";
1134 args << Rt << ", [" << Rn << ", " << Rm;
1135 if (imm2 != 0) {
1136 args << ", lsl #" << imm2;
1137 }
1138 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -08001139 }
1140 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -07001141 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001142 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001143 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001144 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001145 }
Ian Rogers40627db2012-03-04 17:31:09 -08001146 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001147 }
1148 }
1149
1150 break;
1151 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001152 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -08001153 // Load halfword
1154 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1155 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1156 // |---|--|--|---|--|-|----|----|------|------|
1157 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1158 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1159 // |---|--|--|---|--|-|----|----|------|------|
1160 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
1161 // |111|11| op2 | | | imm12 |
1162 uint32_t op3 = (instr >> 23) & 3;
1163 ArmRegister Rn(instr, 16);
1164 ArmRegister Rt(instr, 12);
1165 if (Rt.r != 15) {
1166 if (op3 == 1) {
1167 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
1168 uint32_t imm12 = instr & 0xFFF;
1169 opcode << "ldrh.w";
1170 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1171 if (Rn.r == 9) {
1172 args << " ; ";
1173 Thread::DumpThreadOffset(args, imm12, 4);
1174 } else if (Rn.r == 15) {
1175 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1176 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1177 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1178 }
1179 } else if (op3 == 3) {
1180 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
1181 uint32_t imm12 = instr & 0xFFF;
1182 opcode << "ldrsh.w";
1183 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
1184 if (Rn.r == 9) {
1185 args << " ; ";
1186 Thread::DumpThreadOffset(args, imm12, 4);
1187 } else if (Rn.r == 15) {
1188 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1189 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1190 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
1191 }
1192 }
1193 }
1194 break;
1195 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001196 case 0x29: { // 0101001
1197 // |111|11|1000000|0000|1111|1100|00|0 0|0000|
1198 // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
1199 // |---|--|-------|----|----|----|--|---|----|
1200 // |332|22|2222222|1111|1111|1100|00|0 0|0000|
1201 // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
1202 // |---|--|-------|----|----|----|--|---|----|
1203 // |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
1204 // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
1205 // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
1206 // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
1207 // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
1208 if ((instr & 0xf0c0) == 0xf080) {
1209 uint32_t op3 = (instr >> 4) & 3;
1210 opcode << kThumbReverseOperations[op3];
1211 ArmRegister Rm(instr, 0);
1212 ArmRegister Rd(instr, 8);
1213 args << Rd << ", " << Rm;
1214 ArmRegister Rm2(instr, 16);
1215 if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
1216 args << " (UNPREDICTABLE)";
1217 }
Vladimir Marko1f6754d2013-10-28 20:27:17 +00001218 } // else unknown instruction
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001219 break;
1220 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001221 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001222 // Load word
1223 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1224 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1225 // |---|--|--|---|--|-|----|----|------|------|
1226 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1227 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1228 // |---|--|--|---|--|-|----|----|------|------|
1229 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1230 // |111|11| op2 | | | imm12 |
1231 uint32_t op3 = (instr >> 23) & 3;
1232 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001233 ArmRegister Rn(instr, 16);
1234 ArmRegister Rt(instr, 12);
1235 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001236 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1237 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1238 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001239 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001240 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001241 if (Rn.r == 9) {
1242 args << " ; ";
1243 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001244 } else if (Rn.r == 15) {
1245 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1246 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1247 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001248 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001249 } else if (op4 == 0) {
1250 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1251 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001252 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001253 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001254 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001255 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001256 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001257 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001258 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001259 } else {
1260 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1261 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001262 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001263 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001264 }
1265 break;
1266 }
Dave Allison70202782013-10-22 17:52:19 -07001267 default: // more formats
1268 if ((op2 >> 4) == 2) { // 010xxxx
1269 // data processing (register)
1270 } else if ((op2 >> 3) == 6) { // 0110xxx
1271 // Multiply, multiply accumulate, and absolute difference
1272 op1 = (instr >> 20) & 0x7;
1273 op2 = (instr >> 4) & 0x2;
1274 ArmRegister Ra(instr, 12);
1275 ArmRegister Rn(instr, 16);
1276 ArmRegister Rm(instr, 0);
1277 ArmRegister Rd(instr, 8);
1278 switch (op1) {
1279 case 0:
1280 if (op2 == 0) {
1281 if (Ra.r == 0xf) {
1282 opcode << "mul";
1283 args << Rd << ", " << Rn << ", " << Rm;
1284 } else {
1285 opcode << "mla";
1286 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1287 }
1288 } else {
1289 opcode << "mls";
1290 args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
1291 }
1292 break;
1293 case 1:
1294 case 2:
1295 case 3:
1296 case 4:
1297 case 5:
1298 case 6:
1299 break; // do these sometime
1300 }
1301 } else if ((op2 >> 3) == 7) { // 0111xxx
1302 // Long multiply, long multiply accumulate, and divide
1303 op1 = (instr >> 20) & 0x7;
1304 op2 = (instr >> 4) & 0xf;
1305 ArmRegister Rn(instr, 16);
1306 ArmRegister Rm(instr, 0);
1307 ArmRegister Rd(instr, 8);
1308 ArmRegister RdHi(instr, 8);
1309 ArmRegister RdLo(instr, 12);
1310 switch (op1) {
1311 case 0:
1312 opcode << "smull";
1313 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1314 break;
1315 case 1:
1316 opcode << "sdiv";
1317 args << Rd << ", " << Rn << ", " << Rm;
1318 break;
1319 case 2:
1320 opcode << "umull";
1321 args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
1322 break;
1323 case 3:
1324 opcode << "udiv";
1325 args << Rd << ", " << Rn << ", " << Rm;
1326 break;
1327 case 4:
1328 case 5:
1329 case 6:
1330 break; // TODO: when we generate these...
1331 }
1332 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001333 }
1334 default:
1335 break;
1336 }
Ian Rogers9af89402012-09-07 11:29:35 -07001337
1338 // Apply any IT-block conditions to the opcode if necessary.
1339 if (!it_conditions_.empty()) {
1340 opcode << it_conditions_.back();
1341 it_conditions_.pop_back();
1342 }
1343
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001344 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001346} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001347
1348size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1349 uint16_t instr = ReadU16(instr_ptr);
1350 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1351 if (is_32bit) {
1352 return DumpThumb32(os, instr_ptr);
1353 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001354 std::ostringstream opcode;
1355 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001356 uint16_t opcode1 = instr >> 10;
1357 if (opcode1 < 0x10) {
1358 // shift (immediate), add, subtract, move, and compare
1359 uint16_t opcode2 = instr >> 9;
1360 switch (opcode2) {
1361 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1362 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001363 // Logical shift left - 00 000xx iii mmm ddd
1364 // Logical shift right - 00 001xx iii mmm ddd
1365 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001366 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001367 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001368 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001369 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001370 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001371 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001372 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001373 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001374 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001375 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001376 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001377 break;
1378 }
1379 case 0xC: case 0xD: case 0xE: case 0xF: {
1380 // Add register - 00 01100 mmm nnn ddd
1381 // Sub register - 00 01101 mmm nnn ddd
1382 // Add 3-bit immediate - 00 01110 iii nnn ddd
1383 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1384 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001385 ThumbRegister Rn(instr, 3);
1386 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001387 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001388 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001389 } else {
1390 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001391 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001392 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001393 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001394 }
1395 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001396 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001397 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001398 ArmRegister Rm(imm3_or_Rm);
1399 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001400 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001401 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001402 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001403 break;
1404 }
1405 case 0x10: case 0x11: case 0x12: case 0x13:
1406 case 0x14: case 0x15: case 0x16: case 0x17:
1407 case 0x18: case 0x19: case 0x1A: case 0x1B:
1408 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1409 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1410 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1411 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1412 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001413 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001414 uint16_t imm8 = instr & 0xFF;
1415 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001416 case 4: opcode << "movs"; break;
1417 case 5: opcode << "cmp"; break;
1418 case 6: opcode << "adds"; break;
1419 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001420 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001421 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001422 break;
1423 }
1424 default:
1425 break;
1426 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001427 } else if (opcode1 == 0x10) {
1428 // Data-processing
1429 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001430 ThumbRegister rm(instr, 3);
1431 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001432 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001433 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001434 } else if (opcode1 == 0x11) {
1435 // Special data instructions and branch and exchange
1436 uint16_t opcode2 = (instr >> 6) & 0x0F;
1437 switch (opcode2) {
1438 case 0x0: case 0x1: case 0x2: case 0x3: {
1439 // Add low registers - 010001 0000 xxxxxx
1440 // Add high registers - 010001 0001/001x xxxxxx
1441 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001442 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001443 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001444 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001445 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001446 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001447 break;
1448 }
1449 case 0x8: case 0x9: case 0xA: case 0xB: {
1450 // Move low registers - 010001 1000 xxxxxx
1451 // Move high registers - 010001 1001/101x xxxxxx
1452 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001453 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001454 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001455 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001456 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001457 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001458 break;
1459 }
1460 case 0x5: case 0x6: case 0x7: {
1461 // Compare high registers - 010001 0101/011x xxxxxx
1462 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001463 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001464 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001465 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001466 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001467 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001468 break;
1469 }
1470 case 0xC: case 0xD: case 0xE: case 0xF: {
1471 // Branch and exchange - 010001 110x xxxxxx
1472 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001473 ArmRegister rm(instr, 3);
1474 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1475 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001476 break;
1477 }
1478 default:
1479 break;
1480 }
jeffhaoeae26912013-01-28 16:29:54 -08001481 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1482 ThumbRegister Rt(instr, 8);
1483 uint16_t imm8 = instr & 0xFF;
1484 opcode << "ldr";
1485 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001486 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1487 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1488 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1489 // Load/store single data item
1490 uint16_t opA = (instr >> 12) & 0xF;
1491 if (opA == 0x5) {
1492 uint16_t opB = (instr >> 9) & 0x7;
1493 ThumbRegister Rm(instr, 6);
1494 ThumbRegister Rn(instr, 3);
1495 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001496 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001497 case 0: opcode << "str"; break;
1498 case 1: opcode << "strh"; break;
1499 case 2: opcode << "strb"; break;
1500 case 3: opcode << "ldrsb"; break;
1501 case 4: opcode << "ldr"; break;
1502 case 5: opcode << "ldrh"; break;
1503 case 6: opcode << "ldrb"; break;
1504 case 7: opcode << "ldrsh"; break;
1505 }
1506 args << Rt << ", [" << Rn << ", " << Rm << "]";
1507 } else if (opA == 9) {
1508 uint16_t opB = (instr >> 11) & 1;
1509 ThumbRegister Rt(instr, 8);
1510 uint16_t imm8 = instr & 0xFF;
1511 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001512 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001513 } else {
1514 uint16_t imm5 = (instr >> 6) & 0x1F;
1515 uint16_t opB = (instr >> 11) & 1;
1516 ThumbRegister Rn(instr, 3);
1517 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001518 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001519 case 6:
1520 imm5 <<= 2;
1521 opcode << (opB == 0 ? "str" : "ldr");
1522 break;
1523 case 7:
1524 imm5 <<= 0;
1525 opcode << (opB == 0 ? "strb" : "ldrb");
1526 break;
1527 case 8:
1528 imm5 <<= 1;
1529 opcode << (opB == 0 ? "strh" : "ldrh");
1530 break;
1531 }
1532 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1533 }
jeffhaoeae26912013-01-28 16:29:54 -08001534 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001535 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001536 uint32_t cond = (instr >> 8) & 0xF;
1537 opcode << "b";
1538 DumpCond(opcode, cond);
1539 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001540 } else if ((instr & 0xF800) == 0xA800) {
1541 // Generate SP-relative address
1542 ThumbRegister rd(instr, 8);
1543 int imm8 = instr & 0xFF;
1544 opcode << "add";
1545 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001546 } else if ((instr & 0xF000) == 0xB000) {
1547 // Miscellaneous 16-bit instructions
1548 uint16_t opcode2 = (instr >> 5) & 0x7F;
1549 switch (opcode2) {
1550 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1551 // Add immediate to SP - 1011 00000 ii iiiii
1552 // Subtract immediate from SP - 1011 00001 ii iiiii
1553 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001554 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001555 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001556 break;
1557 }
Ian Rogers087b2412012-03-21 01:30:32 -07001558 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001559 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001560 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1561 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001562 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001563 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1564 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1565 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001566 // CBNZ, CBZ
1567 uint16_t op = (instr >> 11) & 1;
1568 uint16_t i = (instr >> 9) & 1;
1569 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001570 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001571 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001572 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001573 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001574 DumpBranchTarget(args, instr_ptr + 4, imm32);
1575 break;
1576 }
Vladimir Markoa8b4caf2013-10-24 15:08:57 +01001577 case 0x50: case 0x51: // 101000x
1578 case 0x52: case 0x53: // 101001x
1579 case 0x56: case 0x57: { // 101011x
1580 uint16_t op = (instr >> 6) & 3;
1581 opcode << kThumbReverseOperations[op];
1582 ThumbRegister Rm(instr, 3);
1583 ThumbRegister Rd(instr, 0);
1584 args << Rd << ", " << Rm;
1585 break;
1586 }
Ian Rogers40627db2012-03-04 17:31:09 -08001587 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1588 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1589 // If-Then, and hints
1590 uint16_t opA = (instr >> 4) & 0xF;
1591 uint16_t opB = instr & 0xF;
1592 if (opB == 0) {
1593 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001594 case 0: opcode << "nop"; break;
1595 case 1: opcode << "yield"; break;
1596 case 2: opcode << "wfe"; break;
1597 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001598 default: break;
1599 }
1600 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001601 uint32_t first_cond = opA;
1602 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001603 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001604
1605 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1606 // and store up the actual condition codes we'll want to add to the next few opcodes.
1607 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001608 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001609 for (size_t i = 0; i < count; ++i) {
1610 bool positive_cond = ((first_cond & 1) != 0);
1611 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1612 if (positive_mask == positive_cond) {
1613 opcode << 't';
1614 it_conditions_[i] = kConditionCodeNames[first_cond];
1615 } else {
1616 opcode << 'e';
1617 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1618 }
1619 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001620 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001621
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001622 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1623 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001624 }
1625 break;
1626 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001627 default:
1628 break;
1629 }
1630 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1631 ((instr & 0xE000) == 0x8000)) {
1632 // Load/store single data item
1633 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001634 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001635 switch (opA) {
1636 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001637 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1638 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001639 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001640 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001641 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001642 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1643 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001644 break;
1645 }
1646 case 0x9: {
1647 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1648 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1649 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001650 ThumbRegister Rt(instr, 8);
1651 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1652 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001653 break;
1654 }
1655 default:
1656 break;
1657 }
Ian Rogers40627db2012-03-04 17:31:09 -08001658 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1659 uint16_t imm11 = instr & 0x7FFF;
1660 int32_t imm32 = imm11 << 1;
1661 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001662 opcode << "b";
1663 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001665
1666 // Apply any IT-block conditions to the opcode if necessary.
1667 if (!it_conditions_.empty()) {
1668 opcode << it_conditions_.back();
1669 it_conditions_.pop_back();
1670 }
1671
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001673 }
1674 return 2;
1675}
1676
1677} // namespace arm
1678} // namespace art