blob: 782c1f32168370b8de15ff94d02d09c466eaae70 [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
Elliott Hughes77405792012-03-15 15:22:12 -0700106struct ArmRegister {
Elliott Hughes74847412012-06-20 18:10:21 -0700107 explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700108 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -0700109 uint32_t r;
110};
111std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
112 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700113 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700114 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700115 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700116 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700117 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700118 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700119 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700120 }
121 return os;
122}
123
Elliott Hughes630e77d2012-03-22 19:20:56 -0700124struct ThumbRegister : ArmRegister {
125 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700126};
127
128struct Rm {
Elliott Hughes74847412012-06-20 18:10:21 -0700129 explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700130 uint32_t shift;
131 ArmRegister rm;
132};
133std::ostream& operator<<(std::ostream& os, const Rm& r) {
134 os << r.rm;
135 if (r.shift != 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700136 os << "-shift-" << r.shift; // TODO
Elliott Hughes77405792012-03-15 15:22:12 -0700137 }
138 return os;
139}
140
Elliott Hughes1ca98492012-04-12 17:21:02 -0700141struct ShiftedImmediate {
Elliott Hughes74847412012-06-20 18:10:21 -0700142 explicit ShiftedImmediate(uint32_t instruction) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700143 uint32_t rotate = ((instruction >> 8) & 0xf);
144 uint32_t imm = (instruction & 0xff);
145 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
146 }
147 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700148};
Elliott Hughes1ca98492012-04-12 17:21:02 -0700149std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700150 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700151 return os;
152}
153
154struct RegisterList {
Elliott Hughes74847412012-06-20 18:10:21 -0700155 explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700156 uint32_t register_list;
157};
158std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
159 if (rhs.register_list == 0) {
160 os << "<no register list?>";
161 return os;
162 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700163 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700164 bool first = true;
165 for (size_t i = 0; i < 16; i++) {
166 if ((rhs.register_list & (1 << i)) != 0) {
167 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700168 first = false;
169 } else {
170 os << ", ";
171 }
172 os << ArmRegister(i);
173 }
174 }
175 os << "}";
176 return os;
177}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800178
179void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700180 uint32_t instruction = ReadU32(instr_ptr);
181 uint32_t cond = (instruction >> 28) & 0xf;
182 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700183 std::string opcode;
184 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700185 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700186 switch (op1) {
187 case 0:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700188 case 1: // Data processing instructions.
Elliott Hughes77405792012-03-15 15:22:12 -0700189 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700190 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
Elliott Hughes3d71d072012-04-10 18:28:35 -0700191 opcode = "bkpt";
192 uint32_t imm12 = (instruction >> 8) & 0xfff;
193 uint32_t imm4 = (instruction & 0xf);
194 args << '#' << ((imm12 << 4) | imm4);
195 break;
196 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700197 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700198 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700199 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700200 break;
201 }
202 bool i = (instruction & (1 << 25)) != 0;
203 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700204 uint32_t op = (instruction >> 21) & 0xf;
205 opcode = kDataProcessingOperations[op];
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700206 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700207 if (implicit_s) {
208 // Rd is unused (and not shown), and we don't show the 's' suffix either.
209 } else {
210 if (s) {
211 suffixes += 's';
212 }
213 args << ArmRegister(instruction, 12) << ", ";
214 }
Elliott Hughes77405792012-03-15 15:22:12 -0700215 if (i) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700216 args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700217 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700218 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700219 }
220 }
221 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700222 case 2: // Load/store word and unsigned byte.
Elliott Hughes77405792012-03-15 15:22:12 -0700223 {
224 bool p = (instruction & (1 << 24)) != 0;
225 bool b = (instruction & (1 << 22)) != 0;
226 bool w = (instruction & (1 << 21)) != 0;
227 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700228 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700229 args << ArmRegister(instruction, 12) << ", ";
230 ArmRegister rn(instruction, 16);
231 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700232 UNIMPLEMENTED(FATAL) << "literals";
233 } else {
234 bool wback = !p || w;
Elliott Hughes1ca98492012-04-12 17:21:02 -0700235 uint32_t offset = (instruction & 0xfff);
Elliott Hughes77405792012-03-15 15:22:12 -0700236 if (p && !wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700237 args << "[" << rn << ", #" << offset << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700238 } else if (p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700239 args << "[" << rn << ", #" << offset << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700240 } else if (!p && wback) {
Elliott Hughes1ca98492012-04-12 17:21:02 -0700241 args << "[" << rn << "], #" << offset;
Elliott Hughes77405792012-03-15 15:22:12 -0700242 } else {
243 LOG(FATAL) << p << " " << w;
244 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700245 if (rn.r == 9) {
246 args << " ; ";
Elliott Hughes1ca98492012-04-12 17:21:02 -0700247 Thread::DumpThreadOffset(args, offset, 4);
Elliott Hughes3d71d072012-04-10 18:28:35 -0700248 }
Elliott Hughes77405792012-03-15 15:22:12 -0700249 }
250 }
251 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700252 case 4: // Load/store multiple.
Elliott Hughes77405792012-03-15 15:22:12 -0700253 {
254 bool p = (instruction & (1 << 24)) != 0;
255 bool u = (instruction & (1 << 23)) != 0;
256 bool w = (instruction & (1 << 21)) != 0;
257 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700258 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700259 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700260 }
261 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700262 case 5: // Branch/branch with link.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700263 {
264 bool bl = (instruction & (1 << 24)) != 0;
265 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700266 int32_t imm26 = (instruction & 0xffffff) << 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700267 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700268 DumpBranchTarget(args, instr_ptr + 8, imm32);
269 }
270 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700271 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700272 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700273 break;
274 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700275 opcode += kConditionCodeNames[cond];
276 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700277 // TODO: a more complete ARM disassembler could generate wider opcodes.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800278 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800279}
280
Ian Rogersa9650dd2013-10-04 08:23:32 -0700281int32_t ThumbExpand(int32_t imm12) {
282 if ((imm12 & 0xC00) == 0) {
283 switch ((imm12 >> 8) & 3) {
284 case 0:
285 return imm12 & 0xFF;
286 case 1:
287 return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF);
288 case 2:
289 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8);
290 default: // 3
291 return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) |
292 (imm12 & 0xFF);
293 }
294 } else {
295 uint32_t val = 0x80 | (imm12 & 0x7F);
296 int32_t rotate = (imm12 >> 7) & 0x1F;
297 return (val >> rotate) | (val << (32 - rotate));
298 }
299}
300
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800301size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
302 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
303 // |111|1 1|1000000|0000|1111110000000000|
304 // |5 3|2 1|0987654|3 0|5 0 5 0|
305 // |---|---|-------|----|----------------|
306 // |332|2 2|2222222|1111|1111110000000000|
307 // |1 9|8 7|6543210|9 6|5 0 5 0|
308 // |---|---|-------|----|----------------|
309 // |111|op1| op2 | | |
310 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700311 if (op1 == 0) {
312 return DumpThumb16(os, instr_ptr);
313 }
314
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800315 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700316 std::ostringstream opcode;
317 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800318 switch (op1) {
319 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800320 break;
321 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700322 if ((op2 & 0x64) == 0) { // 00x x0xx
323 // |111|11|10|00|0|00|0000|1111110000000000|
324 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
325 // |---|--|--|--|-|--|----|----------------|
326 // |332|22|22|22|2|22|1111|1111110000000000|
327 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
328 // |---|--|--|--|-|--|----|----------------|
329 // |111|01|00|op|0|WL| Rn | |
330 // |111|01| op2 | | |
331 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
332 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
333 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
334 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
335 uint32_t op = (instr >> 23) & 3;
336 uint32_t W = (instr >> 21) & 1;
337 uint32_t L = (instr >> 20) & 1;
338 ArmRegister Rn(instr, 16);
339 if (op == 1 || op == 2) {
340 if (op == 1) {
341 if (L == 0) {
342 opcode << "stm";
343 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800344 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700345 if (Rn.r != 13) {
346 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700347 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700348 } else {
349 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800350 }
351 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700352 } else {
353 if (L == 0) {
354 if (Rn.r != 13) {
355 opcode << "stmdb";
356 args << Rn << (W == 0 ? "" : "!") << ", ";
357 } else {
358 opcode << "push";
359 }
360 } else {
361 opcode << "ldmdb";
362 args << Rn << (W == 0 ? "" : "!") << ", ";
363 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800364 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700365 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800366 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700367 } else if ((op2 & 0x64) == 4) { // 00x x1xx
Ian Rogers9af89402012-09-07 11:29:35 -0700368 uint32_t op3 = (instr >> 23) & 3;
369 uint32_t op4 = (instr >> 20) & 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700370 // uint32_t op5 = (instr >> 4) & 0xF;
Ian Rogers9af89402012-09-07 11:29:35 -0700371 ArmRegister Rn(instr, 16);
372 ArmRegister Rt(instr, 12);
373 uint32_t imm8 = instr & 0xFF;
374 if (op3 == 0 && op4 == 0) { // STREX
375 ArmRegister Rd(instr, 8);
376 opcode << "strex";
377 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
378 } else if (op3 == 0 && op4 == 1) { // LDREX
379 opcode << "ldrex";
380 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
381 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700382 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
383 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100384 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
385 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
386 // |---|----|----|-|----|----|----|--|--|----|
387 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
388 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
389 // |---|----|----|-|----|----|----|--|--|----|
390 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700391 uint32_t op3 = (instr >> 21) & 0xF;
392 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100393 uint32_t imm3 = ((instr >> 12) & 0x7);
394 uint32_t imm2 = ((instr >> 6) & 0x3);
395 uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F;
396 uint32_t shift_type = ((instr >> 4) & 0x2);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700397 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100398 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700399 ArmRegister Rm(instr, 0);
400 switch (op3) {
401 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100402 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700403 opcode << "and";
404 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700405 if (S != 1U) {
406 opcode << "UNKNOWN TST-" << S;
407 break;
408 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700409 opcode << "tst";
410 S = 0; // don't print 's'
411 }
412 break;
413 case 0x1: opcode << "bic"; break;
414 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100415 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700416 opcode << "orr";
417 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100418 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700419 opcode << "mov";
420 }
421 break;
422 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100423 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700424 opcode << "orn";
425 } else {
426 opcode << "mvn";
427 }
428 break;
429 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100430 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700431 opcode << "eor";
432 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700433 if (S != 1U) {
434 opcode << "UNKNOWN TEQ-" << S;
435 break;
436 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700437 opcode << "teq";
438 S = 0; // don't print 's'
439 }
440 break;
441 case 0x6: opcode << "pkh"; break;
442 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100443 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700444 opcode << "add";
445 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700446 if (S != 1U) {
447 opcode << "UNKNOWN CMN-" << S;
448 break;
449 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700450 opcode << "cmn";
451 S = 0; // don't print 's'
452 }
453 break;
454 case 0xA: opcode << "adc"; break;
455 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100456 case 0xD:
457 if (Rd.r != 0xF) {
458 opcode << "sub";
459 } else {
Brian Carlstrom4a999e22013-03-11 16:57:09 -0700460 if (S != 1U) {
461 opcode << "UNKNOWN CMP-" << S;
462 break;
463 }
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100464 opcode << "cmp";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100465 S = 0; // don't print 's'
466 }
467 break;
468 case 0xE: opcode << "rsb"; break;
469 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700470 }
Ian Rogers087b2412012-03-21 01:30:32 -0700471
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700472 if (S == 1) {
473 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700474 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700475 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100476
477 if (Rd.r != 0xF) {
478 args << Rd << ", ";
479 }
480 if (Rn.r != 0xF) {
481 args << Rn << ", ";
482 }
483 args << Rm;
484
485 // Shift operand.
486 bool noShift = (imm5 == 0 && shift_type != 0x3);
487 if (!noShift) {
488 args << ", ";
489 switch (shift_type) {
490 case 0x0: args << "lsl"; break;
491 case 0x1: args << "lsr"; break;
492 case 0x2: args << "asr"; break;
493 case 0x3:
494 if (imm5 == 0) {
495 args << "rrx";
496 } else {
497 args << "ror";
498 }
499 break;
500 }
501 if (shift_type != 0x3 /* rrx */) {
502 args << StringPrintf(" #%d", imm5);
503 }
504 }
505
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700506 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
507 // Co-processor instructions
508 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
509 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
510 // |---|-|--|------|----|----|----|---|---|----|
511 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
512 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
513 // |---|-|--|------|----|----|----|---|---|----|
514 // |111| |11| op3 | Rn | |copr| |op4| |
515 uint32_t op3 = (instr >> 20) & 0x3F;
516 uint32_t coproc = (instr >> 8) & 0xF;
517 uint32_t op4 = (instr >> 4) & 0x1;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700518 if ((op3 == 2 || op3 == 2 || op3 == 6 || op3 == 7) || // 00x1x
519 (op3 >= 8 && op3 <= 15) || (op3 >= 16 && op3 <= 31)) { // 001xxx, 01xxxx
Ian Rogers9af89402012-09-07 11:29:35 -0700520 // Extension register load/store instructions
521 // |111|1|110|00000|0000|1111|110|000000000|
522 // |5 3|2|109|87654|3 0|54 2|10 |87 54 0|
523 // |---|-|---|-----|----|----|---|---------|
524 // |332|2|222|22222|1111|1111|110|000000000|
525 // |1 9|8|765|43210|9 6|54 2|10 |87 54 0|
526 // |---|-|---|-----|----|----|---|---------|
527 // |111|T|110| op3 | Rn | |101| |
528 // 111 0 110 01001 0011 0000 101 000000011 - ec930a03
529 if (op3 == 9 || op3 == 0xD) { // VLDM
530 // 1110 110 PUDW1 nnnn dddd 101S iiii iiii
531 uint32_t P = (instr >> 24) & 1;
532 uint32_t U = (instr >> 23) & 1;
533 uint32_t D = (instr >> 22) & 1;
534 uint32_t W = (instr >> 21) & 1;
535 uint32_t S = (instr >> 8) & 1;
536 ArmRegister Rn(instr, 16);
537 uint32_t Vd = (instr >> 12) & 0xF;
538 uint32_t imm8 = instr & 0xFF;
539 uint32_t d = (S == 0 ? ((Vd << 1) | D) : (Vd | (D << 4)));
540 if (P == 0 && U == 0 && W == 0) {
541 // TODO: 64bit transfers between ARM core and extension registers.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700542 } else if (P == 0 && U == 1 && Rn.r == 13) { // VPOP
Ian Rogers9af89402012-09-07 11:29:35 -0700543 opcode << "vpop" << (S == 0 ? ".f64" : ".f32");
544 args << d << " .. " << (d + imm8);
545 } else if (P == 1 && W == 0) { // VLDR
546 opcode << "vldr" << (S == 0 ? ".f64" : ".f32");
547 args << d << ", [" << Rn << ", #" << imm8 << "]";
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700548 } else { // VLDM
Ian Rogers9af89402012-09-07 11:29:35 -0700549 opcode << "vldm" << (S == 0 ? ".f64" : ".f32");
550 args << Rn << ", " << d << " .. " << (d + imm8);
551 }
552 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700553 } else if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700554 if ((coproc & 0xE) == 0xA) {
555 // VFP data-processing instructions
556 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
557 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
558 // |---|-|----|----|----|----|---|-|----|-|-|----|
559 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
560 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
561 // |---|-|----|----|----|----|---|-|----|-|-|----|
562 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
563 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
564 uint32_t opc1 = (instr >> 20) & 0xF;
565 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700566 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700567 if ((opc1 & 0xB) == 0xB) { // 1x11
568 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700569 uint32_t D = (instr >> 22) & 0x1;
570 uint32_t Vd = (instr >> 12) & 0xF;
571 uint32_t sz = (instr >> 8) & 1;
572 uint32_t M = (instr >> 5) & 1;
573 uint32_t Vm = instr & 0xF;
574 bool dp_operation = sz == 1;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700575 switch (opc2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700576 case 0x1: // Vneg/Vsqrt
Ian Rogers0183dd72012-09-17 23:06:51 -0700577 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
578 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (dp_operation ? ".f64" : ".f32");
579 if (dp_operation) {
580 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
581 } else {
582 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
583 }
584 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700585 case 0x4: case 0x5: { // Vector compare
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700586 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Ian Rogers0183dd72012-09-17 23:06:51 -0700587 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (dp_operation ? ".f64" : ".f32");
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700588 if (dp_operation) {
589 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
590 } else {
591 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
592 }
593 break;
594 }
595 }
596 }
597 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700598 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
599 // Advanced SIMD
600 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
601 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
602 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
603 uint32_t D = (instr >> 22) & 1;
604 uint32_t Vd = (instr >> 12) & 0xF;
605 uint32_t sz = (instr >> 8) & 1;
606 uint32_t M = (instr >> 5) & 1;
607 uint32_t Vm = instr & 0xF;
608 bool dp_operation = sz == 1;
609 opcode << "vsqrt" << (dp_operation ? ".f64" : ".f32");
610 if (dp_operation) {
611 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
612 } else {
613 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
614 }
615 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700616 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800617 }
618 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800619 case 2:
620 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
621 // Data-processing (modified immediate)
622 // |111|11|10|0000|0|0000|1|111|1100|00000000|
623 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
624 // |---|--|--|----|-|----|-|---|----|--------|
625 // |332|22|22|2222|2|1111|1|111|1100|00000000|
626 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
627 // |---|--|--|----|-|----|-|---|----|--------|
628 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
629 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800630 uint32_t i = (instr >> 26) & 1;
631 uint32_t op3 = (instr >> 21) & 0xF;
632 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700633 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800634 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700635 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800636 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800637 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
638 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
639 if (op3 == 0x2) {
640 opcode << "mov";
641 if (S == 1) {
642 opcode << "s";
643 }
644 opcode << ".w";
645 } else {
646 opcode << "mvn";
647 if (S == 1) {
648 opcode << "s";
649 }
650 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700651 args << Rd << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800652 } else if (Rd.r == 0xF && S == 1 &&
653 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
654 if (op3 == 0x0) {
655 opcode << "tst";
656 } else if (op3 == 0x4) {
657 opcode << "teq";
658 } else if (op3 == 0x8) {
659 opcode << "cmw";
660 } else {
661 opcode << "cmp.w";
662 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700663 args << Rn << ", #" << ThumbExpand(imm32);
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800664 } else {
665 switch (op3) {
666 case 0x0: opcode << "and"; break;
667 case 0x1: opcode << "bic"; break;
668 case 0x2: opcode << "orr"; break;
669 case 0x3: opcode << "orn"; break;
670 case 0x4: opcode << "eor"; break;
671 case 0x8: opcode << "add"; break;
672 case 0xA: opcode << "adc"; break;
673 case 0xB: opcode << "sbc"; break;
674 case 0xD: opcode << "sub"; break;
675 case 0xE: opcode << "rsb"; break;
676 default: opcode << "UNKNOWN DPMI-" << op3; break;
677 }
678 if (S == 1) {
679 opcode << "s";
680 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700681 args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800682 }
Ian Rogers40627db2012-03-04 17:31:09 -0800683 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
684 // Data-processing (plain binary immediate)
685 // |111|11|10|00000|0000|1|111110000000000|
686 // |5 3|21|09|87654|3 0|5|4 0 5 0|
687 // |---|--|--|-----|----|-|---------------|
688 // |332|22|22|22222|1111|1|111110000000000|
689 // |1 9|87|65|43210|9 6|5|4 0 5 0|
690 // |---|--|--|-----|----|-|---------------|
691 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
692 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800693 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -0800694 case 0x00: case 0x0A: {
695 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -0700696 ArmRegister Rd(instr, 8);
697 ArmRegister Rn(instr, 16);
698 uint32_t i = (instr >> 26) & 1;
699 uint32_t imm3 = (instr >> 12) & 0x7;
700 uint32_t imm8 = instr & 0xFF;
701 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
702 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -0800703 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -0700704 args << Rd << ", " << Rn << ", #" << imm12;
705 } else {
706 opcode << "adr";
707 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -0800708 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -0700709 }
710 break;
711 }
Ian Rogers55019132013-02-08 01:05:23 -0800712 case 0x04: case 0x0C: {
713 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700714 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800715 uint32_t i = (instr >> 26) & 1;
716 uint32_t imm3 = (instr >> 12) & 0x7;
717 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700718 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800719 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -0800720 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700721 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800722 break;
723 }
jeffhaoeae26912013-01-28 16:29:54 -0800724 case 0x16: {
725 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
726 ArmRegister Rd(instr, 8);
727 ArmRegister Rn(instr, 16);
728 uint32_t msb = instr & 0x1F;
729 uint32_t imm2 = (instr >> 6) & 0x3;
730 uint32_t imm3 = (instr >> 12) & 0x7;
731 uint32_t lsb = (imm3 << 2) | imm2;
732 uint32_t width = msb - lsb + 1;
733 if (Rn.r != 0xF) {
734 opcode << "bfi";
735 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
736 } else {
737 opcode << "bfc";
738 args << Rd << ", #" << lsb << ", #" << width;
739 }
740 break;
741 }
Ian Rogers40627db2012-03-04 17:31:09 -0800742 default:
743 break;
744 }
745 } else {
746 // Branches and miscellaneous control
747 // |111|11|1000000|0000|1|111|1100|00000000|
748 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
749 // |---|--|-------|----|-|---|----|--------|
750 // |332|22|2222222|1111|1|111|1100|00000000|
751 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
752 // |---|--|-------|----|-|---|----|--------|
753 // |111|10| op2 | |1|op3|op4 | |
754
755 uint32_t op3 = (instr >> 12) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700756 // uint32_t op4 = (instr >> 8) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800757 switch (op3) {
758 case 0:
759 if ((op2 & 0x38) != 0x38) {
760 // Conditional branch
761 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
762 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
763 // |---|--|-|----|------|-|-|--|-|--|-----------|
764 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
765 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
766 // |---|--|-|----|------|-|-|--|-|--|-----------|
767 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
768 uint32_t S = (instr >> 26) & 1;
769 uint32_t J2 = (instr >> 11) & 1;
770 uint32_t J1 = (instr >> 13) & 1;
771 uint32_t imm6 = (instr >> 16) & 0x3F;
772 uint32_t imm11 = instr & 0x7FF;
773 uint32_t cond = (instr >> 22) & 0xF;
774 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
775 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700776 opcode << "b";
777 DumpCond(opcode, cond);
778 opcode << ".w";
779 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -0700780 } else if (op2 == 0x3B) {
781 // Miscellaneous control instructions
782 uint32_t op5 = (instr >> 4) & 0xF;
783 switch (op5) {
784 case 4: opcode << "dsb"; break;
785 case 5: opcode << "dmb"; break;
786 case 6: opcode << "isb"; break;
787 }
Ian Rogers40627db2012-03-04 17:31:09 -0800788 }
789 break;
790 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -0800791 if ((op2 & 0x38) == 0x38) {
792 if (op2 == 0x7F) {
793 opcode << "udf";
794 }
795 break;
796 }
797 // Else deliberate fall-through to B.
798 case 1: case 3: {
799 // B
800 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
801 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
802 // |---|--|-|----|------|--|--|-|--|-----------|
803 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
804 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
805 // |---|--|-|----|------|--|--|-|--|-----------|
806 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
807 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
808 uint32_t S = (instr >> 26) & 1;
809 uint32_t cond = (instr >> 22) & 0xF;
810 uint32_t J2 = (instr >> 11) & 1;
811 uint32_t form = (instr >> 12) & 1;
812 uint32_t J1 = (instr >> 13) & 1;
813 uint32_t imm10 = (instr >> 16) & 0x3FF;
814 uint32_t imm6 = (instr >> 16) & 0x3F;
815 uint32_t imm11 = instr & 0x7FF;
816 opcode << "b";
817 int32_t imm32;
818 if (form == 0) {
819 DumpCond(opcode, cond);
820 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
821 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
822 } else {
823 uint32_t I1 = ~(J1 ^ S);
824 uint32_t I2 = ~(J2 ^ S);
825 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
826 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
827 }
828 opcode << ".w";
829 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800830 break;
Ian Rogersd0876a92013-02-08 11:30:38 -0800831 }
Ian Rogers40627db2012-03-04 17:31:09 -0800832 case 4: case 6: case 5: case 7: {
833 // BL, BLX (immediate)
834 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
835 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
836 // |---|--|-|----------|--|--|-|--|-----------|
837 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
838 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
839 // |---|--|-|----------|--|--|-|--|-----------|
840 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
841 uint32_t S = (instr >> 26) & 1;
842 uint32_t J2 = (instr >> 11) & 1;
843 uint32_t L = (instr >> 12) & 1;
844 uint32_t J1 = (instr >> 13) & 1;
845 uint32_t imm10 = (instr >> 16) & 0x3FF;
846 uint32_t imm11 = instr & 0x7FF;
847 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700848 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800849 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700850 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800851 }
852 uint32_t I1 = ~(J1 ^ S);
853 uint32_t I2 = ~(J2 ^ S);
854 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
855 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700856 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800857 break;
858 }
859 }
860 }
861 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800862 case 3:
863 switch (op2) {
864 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
865 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
866 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800867 // |111|11|100|000|0|0000|1111|110000|000000|
868 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
869 // |---|--|---|---|-|----|----|------|------|
870 // |332|22|222|222|2|1111|1111|110000|000000|
871 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
872 // |---|--|---|---|-|----|----|------|------|
873 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800874 uint32_t op3 = (instr >> 21) & 7;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700875 // uint32_t op4 = (instr >> 6) & 0x3F;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800876 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700877 case 0x0: case 0x4: {
878 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
879 // 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 -0700880 ArmRegister Rn(instr, 16);
881 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700882 opcode << "strb";
883 if ((instr & 0x800) != 0) {
884 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700885 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700886 } else {
887 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700888 ArmRegister Rm(instr, 0);
889 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700890 if (imm2 != 0) {
891 args << ", " << "lsl #" << imm2;
892 }
893 args << "]";
894 }
895 break;
896 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800897 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700898 ArmRegister Rn(instr, 16);
899 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800900 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700901 if ((instr & 0x800) != 0) {
902 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
903 uint32_t P = (instr >> 10) & 1;
904 uint32_t U = (instr >> 9) & 1;
905 uint32_t W = (instr >> 8) & 1;
906 uint32_t imm8 = instr & 0xFF;
907 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
908 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
909 opcode << "push";
910 args << Rt;
911 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
912 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800913 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700914 if (P == 1 && U == 1 && W == 0) {
915 opcode << "strt";
916 } else {
917 opcode << "str";
918 }
919 args << Rt << ", [" << Rn;
920 if (P == 0 && W == 1) {
921 args << "], #" << imm32;
922 } else {
923 args << ", #" << imm32 << "]";
924 if (W == 1) {
925 args << "!";
926 }
Ian Rogers40627db2012-03-04 17:31:09 -0800927 }
928 }
Ian Rogers66a3fca2012-04-09 19:51:34 -0700929 } else {
930 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
931 ArmRegister Rn(instr, 16);
932 ArmRegister Rt(instr, 12);
933 ArmRegister Rm(instr, 0);
934 uint32_t imm2 = (instr >> 4) & 3;
935 opcode << "str.w";
936 args << Rt << ", [" << Rn << ", " << Rm;
937 if (imm2 != 0) {
938 args << ", lsl #" << imm2;
939 }
940 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800941 }
942 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700943 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800944 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700945 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700946 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800947 }
Ian Rogers40627db2012-03-04 17:31:09 -0800948 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800949 }
950 }
951
952 break;
953 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700954 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
jeffhaoeae26912013-01-28 16:29:54 -0800955 // Load halfword
956 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
957 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
958 // |---|--|--|---|--|-|----|----|------|------|
959 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
960 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
961 // |---|--|--|---|--|-|----|----|------|------|
962 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
963 // |111|11| op2 | | | imm12 |
964 uint32_t op3 = (instr >> 23) & 3;
965 ArmRegister Rn(instr, 16);
966 ArmRegister Rt(instr, 12);
967 if (Rt.r != 15) {
968 if (op3 == 1) {
969 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
970 uint32_t imm12 = instr & 0xFFF;
971 opcode << "ldrh.w";
972 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
973 if (Rn.r == 9) {
974 args << " ; ";
975 Thread::DumpThreadOffset(args, imm12, 4);
976 } else if (Rn.r == 15) {
977 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
978 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
979 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
980 }
981 } else if (op3 == 3) {
982 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
983 uint32_t imm12 = instr & 0xFFF;
984 opcode << "ldrsh.w";
985 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
986 if (Rn.r == 9) {
987 args << " ; ";
988 Thread::DumpThreadOffset(args, imm12, 4);
989 } else if (Rn.r == 15) {
990 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
991 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
992 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
993 }
994 }
995 }
996 break;
997 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700998 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800999 // Load word
1000 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
1001 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
1002 // |---|--|--|---|--|-|----|----|------|------|
1003 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
1004 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
1005 // |---|--|--|---|--|-|----|----|------|------|
1006 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
1007 // |111|11| op2 | | | imm12 |
1008 uint32_t op3 = (instr >> 23) & 3;
1009 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001010 ArmRegister Rn(instr, 16);
1011 ArmRegister Rt(instr, 12);
1012 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001013 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
1014 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
1015 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001016 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001017 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001018 if (Rn.r == 9) {
1019 args << " ; ";
1020 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -07001021 } else if (Rn.r == 15) {
1022 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
1023 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
1024 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001025 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001026 } else if (op4 == 0) {
1027 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
1028 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001029 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001030 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001031 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001032 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001033 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001034 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001035 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001036 } else {
1037 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1038 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001039 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001040 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001041 }
1042 break;
1043 }
1044 }
1045 default:
1046 break;
1047 }
Ian Rogers9af89402012-09-07 11:29:35 -07001048
1049 // Apply any IT-block conditions to the opcode if necessary.
1050 if (!it_conditions_.empty()) {
1051 opcode << it_conditions_.back();
1052 it_conditions_.pop_back();
1053 }
1054
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001055 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001056 return 4;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07001057} // NOLINT(readability/fn_size)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001058
1059size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1060 uint16_t instr = ReadU16(instr_ptr);
1061 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1062 if (is_32bit) {
1063 return DumpThumb32(os, instr_ptr);
1064 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001065 std::ostringstream opcode;
1066 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001067 uint16_t opcode1 = instr >> 10;
1068 if (opcode1 < 0x10) {
1069 // shift (immediate), add, subtract, move, and compare
1070 uint16_t opcode2 = instr >> 9;
1071 switch (opcode2) {
1072 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1073 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001074 // Logical shift left - 00 000xx iii mmm ddd
1075 // Logical shift right - 00 001xx iii mmm ddd
1076 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001077 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001078 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001079 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001080 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001081 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001082 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001083 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001084 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001085 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001086 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001087 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001088 break;
1089 }
1090 case 0xC: case 0xD: case 0xE: case 0xF: {
1091 // Add register - 00 01100 mmm nnn ddd
1092 // Sub register - 00 01101 mmm nnn ddd
1093 // Add 3-bit immediate - 00 01110 iii nnn ddd
1094 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1095 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001096 ThumbRegister Rn(instr, 3);
1097 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001098 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001099 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001100 } else {
1101 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001102 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001103 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001104 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001105 }
1106 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001107 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001108 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001109 ArmRegister Rm(imm3_or_Rm);
1110 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001111 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001112 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001113 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001114 break;
1115 }
1116 case 0x10: case 0x11: case 0x12: case 0x13:
1117 case 0x14: case 0x15: case 0x16: case 0x17:
1118 case 0x18: case 0x19: case 0x1A: case 0x1B:
1119 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1120 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1121 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1122 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1123 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001124 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001125 uint16_t imm8 = instr & 0xFF;
1126 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001127 case 4: opcode << "movs"; break;
1128 case 5: opcode << "cmp"; break;
1129 case 6: opcode << "adds"; break;
1130 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001131 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001132 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001133 break;
1134 }
1135 default:
1136 break;
1137 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001138 } else if (opcode1 == 0x10) {
1139 // Data-processing
1140 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001141 ThumbRegister rm(instr, 3);
1142 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001143 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001144 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001145 } else if (opcode1 == 0x11) {
1146 // Special data instructions and branch and exchange
1147 uint16_t opcode2 = (instr >> 6) & 0x0F;
1148 switch (opcode2) {
1149 case 0x0: case 0x1: case 0x2: case 0x3: {
1150 // Add low registers - 010001 0000 xxxxxx
1151 // Add high registers - 010001 0001/001x xxxxxx
1152 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001153 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001154 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001155 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001156 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001157 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001158 break;
1159 }
1160 case 0x8: case 0x9: case 0xA: case 0xB: {
1161 // Move low registers - 010001 1000 xxxxxx
1162 // Move high registers - 010001 1001/101x xxxxxx
1163 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001164 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001165 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001166 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001167 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001168 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001169 break;
1170 }
1171 case 0x5: case 0x6: case 0x7: {
1172 // Compare high registers - 010001 0101/011x xxxxxx
1173 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001174 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001175 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001176 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001177 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001178 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001179 break;
1180 }
1181 case 0xC: case 0xD: case 0xE: case 0xF: {
1182 // Branch and exchange - 010001 110x xxxxxx
1183 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001184 ArmRegister rm(instr, 3);
1185 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1186 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001187 break;
1188 }
1189 default:
1190 break;
1191 }
jeffhaoeae26912013-01-28 16:29:54 -08001192 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1193 ThumbRegister Rt(instr, 8);
1194 uint16_t imm8 = instr & 0xFF;
1195 opcode << "ldr";
1196 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001197 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1198 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1199 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1200 // Load/store single data item
1201 uint16_t opA = (instr >> 12) & 0xF;
1202 if (opA == 0x5) {
1203 uint16_t opB = (instr >> 9) & 0x7;
1204 ThumbRegister Rm(instr, 6);
1205 ThumbRegister Rn(instr, 3);
1206 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001207 switch (opB) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001208 case 0: opcode << "str"; break;
1209 case 1: opcode << "strh"; break;
1210 case 2: opcode << "strb"; break;
1211 case 3: opcode << "ldrsb"; break;
1212 case 4: opcode << "ldr"; break;
1213 case 5: opcode << "ldrh"; break;
1214 case 6: opcode << "ldrb"; break;
1215 case 7: opcode << "ldrsh"; break;
1216 }
1217 args << Rt << ", [" << Rn << ", " << Rm << "]";
1218 } else if (opA == 9) {
1219 uint16_t opB = (instr >> 11) & 1;
1220 ThumbRegister Rt(instr, 8);
1221 uint16_t imm8 = instr & 0xFF;
1222 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001223 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001224 } else {
1225 uint16_t imm5 = (instr >> 6) & 0x1F;
1226 uint16_t opB = (instr >> 11) & 1;
1227 ThumbRegister Rn(instr, 3);
1228 ThumbRegister Rt(instr, 0);
Brian Carlstromdf629502013-07-17 22:39:56 -07001229 switch (opA) {
Ian Rogersd83bc362012-09-07 17:43:13 -07001230 case 6:
1231 imm5 <<= 2;
1232 opcode << (opB == 0 ? "str" : "ldr");
1233 break;
1234 case 7:
1235 imm5 <<= 0;
1236 opcode << (opB == 0 ? "strb" : "ldrb");
1237 break;
1238 case 8:
1239 imm5 <<= 1;
1240 opcode << (opB == 0 ? "strh" : "ldrh");
1241 break;
1242 }
1243 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1244 }
jeffhaoeae26912013-01-28 16:29:54 -08001245 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
Ian Rogers7761cb62013-06-17 14:10:46 -07001246 int8_t imm8 = instr & 0xFF;
jeffhaoeae26912013-01-28 16:29:54 -08001247 uint32_t cond = (instr >> 8) & 0xF;
1248 opcode << "b";
1249 DumpCond(opcode, cond);
1250 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001251 } else if ((instr & 0xF800) == 0xA800) {
1252 // Generate SP-relative address
1253 ThumbRegister rd(instr, 8);
1254 int imm8 = instr & 0xFF;
1255 opcode << "add";
1256 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001257 } else if ((instr & 0xF000) == 0xB000) {
1258 // Miscellaneous 16-bit instructions
1259 uint16_t opcode2 = (instr >> 5) & 0x7F;
1260 switch (opcode2) {
1261 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1262 // Add immediate to SP - 1011 00000 ii iiiii
1263 // Subtract immediate from SP - 1011 00001 ii iiiii
1264 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001265 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001266 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001267 break;
1268 }
Ian Rogers087b2412012-03-21 01:30:32 -07001269 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001270 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001271 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1272 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001273 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001274 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1275 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1276 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001277 // CBNZ, CBZ
1278 uint16_t op = (instr >> 11) & 1;
1279 uint16_t i = (instr >> 9) & 1;
1280 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001281 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001282 opcode << (op != 0 ? "cbnz" : "cbz");
Ian Rogers828a07f2013-06-18 22:27:34 -07001283 uint32_t imm32 = (i << 6) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001284 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001285 DumpBranchTarget(args, instr_ptr + 4, imm32);
1286 break;
1287 }
Ian Rogers40627db2012-03-04 17:31:09 -08001288 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1289 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1290 // If-Then, and hints
1291 uint16_t opA = (instr >> 4) & 0xF;
1292 uint16_t opB = instr & 0xF;
1293 if (opB == 0) {
1294 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001295 case 0: opcode << "nop"; break;
1296 case 1: opcode << "yield"; break;
1297 case 2: opcode << "wfe"; break;
1298 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001299 default: break;
1300 }
1301 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001302 uint32_t first_cond = opA;
1303 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001304 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001305
1306 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1307 // and store up the actual condition codes we'll want to add to the next few opcodes.
1308 size_t count = 3 - CTZ(mask);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001309 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
Elliott Hughes105afd22012-04-10 15:04:25 -07001310 for (size_t i = 0; i < count; ++i) {
1311 bool positive_cond = ((first_cond & 1) != 0);
1312 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1313 if (positive_mask == positive_cond) {
1314 opcode << 't';
1315 it_conditions_[i] = kConditionCodeNames[first_cond];
1316 } else {
1317 opcode << 'e';
1318 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1319 }
1320 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001321 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
Elliott Hughes105afd22012-04-10 15:04:25 -07001322
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001323 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1324 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001325 }
1326 break;
1327 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001328 default:
1329 break;
1330 }
1331 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1332 ((instr & 0xE000) == 0x8000)) {
1333 // Load/store single data item
1334 uint16_t opA = instr >> 12;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001335 // uint16_t opB = (instr >> 9) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001336 switch (opA) {
1337 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001338 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1339 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001340 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001341 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001342 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001343 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1344 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345 break;
1346 }
1347 case 0x9: {
1348 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1349 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1350 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001351 ThumbRegister Rt(instr, 8);
1352 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1353 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001354 break;
1355 }
1356 default:
1357 break;
1358 }
Ian Rogers40627db2012-03-04 17:31:09 -08001359 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1360 uint16_t imm11 = instr & 0x7FFF;
1361 int32_t imm32 = imm11 << 1;
1362 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001363 opcode << "b";
1364 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001365 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001366
1367 // Apply any IT-block conditions to the opcode if necessary.
1368 if (!it_conditions_.empty()) {
1369 opcode << it_conditions_.back();
1370 it_conditions_.pop_back();
1371 }
1372
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001373 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001374 }
1375 return 2;
1376}
1377
1378} // namespace arm
1379} // namespace art