blob: 91da6f7ff8a6a322d0679f56f555fda19adbdf9a [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) {
136 os << "-shift-" << r.shift; // TODO
137 }
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:
188 case 1: // Data processing instructions.
189 {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700190 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
191 opcode = "bkpt";
192 uint32_t imm12 = (instruction >> 8) & 0xfff;
193 uint32_t imm4 = (instruction & 0xf);
194 args << '#' << ((imm12 << 4) | imm4);
195 break;
196 }
Elliott Hughes77405792012-03-15 15:22:12 -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];
206 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
207 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;
222 case 2: // Load/store word and unsigned byte.
223 {
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;
252 case 4: // Load/store multiple.
253 {
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;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700262 case 5: // Branch/branch with link.
263 {
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;
267 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
281size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
282 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
283 // |111|1 1|1000000|0000|1111110000000000|
284 // |5 3|2 1|0987654|3 0|5 0 5 0|
285 // |---|---|-------|----|----------------|
286 // |332|2 2|2222222|1111|1111110000000000|
287 // |1 9|8 7|6543210|9 6|5 0 5 0|
288 // |---|---|-------|----|----------------|
289 // |111|op1| op2 | | |
290 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700291 if (op1 == 0) {
292 return DumpThumb16(os, instr_ptr);
293 }
294
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800295 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700296 std::ostringstream opcode;
297 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800298 switch (op1) {
299 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800300 break;
301 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700302 if ((op2 & 0x64) == 0) { // 00x x0xx
303 // |111|11|10|00|0|00|0000|1111110000000000|
304 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
305 // |---|--|--|--|-|--|----|----------------|
306 // |332|22|22|22|2|22|1111|1111110000000000|
307 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
308 // |---|--|--|--|-|--|----|----------------|
309 // |111|01|00|op|0|WL| Rn | |
310 // |111|01| op2 | | |
311 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
312 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
313 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
314 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
315 uint32_t op = (instr >> 23) & 3;
316 uint32_t W = (instr >> 21) & 1;
317 uint32_t L = (instr >> 20) & 1;
318 ArmRegister Rn(instr, 16);
319 if (op == 1 || op == 2) {
320 if (op == 1) {
321 if (L == 0) {
322 opcode << "stm";
323 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800324 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700325 if (Rn.r != 13) {
326 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700327 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700328 } else {
329 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800330 }
331 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700332 } else {
333 if (L == 0) {
334 if (Rn.r != 13) {
335 opcode << "stmdb";
336 args << Rn << (W == 0 ? "" : "!") << ", ";
337 } else {
338 opcode << "push";
339 }
340 } else {
341 opcode << "ldmdb";
342 args << Rn << (W == 0 ? "" : "!") << ", ";
343 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800344 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700345 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800346 }
Ian Rogers9af89402012-09-07 11:29:35 -0700347 } else if ((op2 & 0x64) == 4) { // 00x x1xx
348 uint32_t op3 = (instr >> 23) & 3;
349 uint32_t op4 = (instr >> 20) & 3;
350 //uint32_t op5 = (instr >> 4) & 0xF;
351 ArmRegister Rn(instr, 16);
352 ArmRegister Rt(instr, 12);
353 uint32_t imm8 = instr & 0xFF;
354 if (op3 == 0 && op4 == 0) { // STREX
355 ArmRegister Rd(instr, 8);
356 opcode << "strex";
357 args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
358 } else if (op3 == 0 && op4 == 1) { // LDREX
359 opcode << "ldrex";
360 args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
361 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700362 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
363 // Data-processing (shifted register)
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100364 // |111|1110|0000|0|0000|1111|1100|00|00|0000|
365 // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
366 // |---|----|----|-|----|----|----|--|--|----|
367 // |332|2222|2222|2|1111|1111|1100|00|00|0000|
368 // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
369 // |---|----|----|-|----|----|----|--|--|----|
370 // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700371 uint32_t op3 = (instr >> 21) & 0xF;
372 uint32_t S = (instr >> 20) & 1;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100373 uint32_t imm3 = ((instr >> 12) & 0x7);
374 uint32_t imm2 = ((instr >> 6) & 0x3);
375 uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F;
376 uint32_t shift_type = ((instr >> 4) & 0x2);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700377 ArmRegister Rd(instr, 8);
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100378 ArmRegister Rn(instr, 16);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700379 ArmRegister Rm(instr, 0);
380 switch (op3) {
381 case 0x0:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100382 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700383 opcode << "and";
384 } else {
385 opcode << "tst";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100386 DCHECK_EQ(S, 1U);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700387 S = 0; // don't print 's'
388 }
389 break;
390 case 0x1: opcode << "bic"; break;
391 case 0x2:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100392 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700393 opcode << "orr";
394 } else {
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100395 // TODO: use canonical form if there is a shift (lsl, ...).
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700396 opcode << "mov";
397 }
398 break;
399 case 0x3:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100400 if (Rn.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700401 opcode << "orn";
402 } else {
403 opcode << "mvn";
404 }
405 break;
406 case 0x4:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100407 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700408 opcode << "eor";
409 } else {
410 opcode << "teq";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100411 DCHECK_EQ(S, 1U);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700412 S = 0; // don't print 's'
413 }
414 break;
415 case 0x6: opcode << "pkh"; break;
416 case 0x8:
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100417 if (Rd.r != 0xF) {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700418 opcode << "add";
419 } else {
420 opcode << "cmn";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100421 DCHECK_EQ(S, 1U);
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700422 S = 0; // don't print 's'
423 }
424 break;
425 case 0xA: opcode << "adc"; break;
426 case 0xB: opcode << "sbc"; break;
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100427 case 0xD:
428 if (Rd.r != 0xF) {
429 opcode << "sub";
430 } else {
431 opcode << "cmp";
432 DCHECK_EQ(S, 1U);
433 S = 0; // don't print 's'
434 }
435 break;
436 case 0xE: opcode << "rsb"; break;
437 default: opcode << "UNKNOWN DPSR-" << op3; break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700438 }
Ian Rogers087b2412012-03-21 01:30:32 -0700439
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700440 if (S == 1) {
441 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700442 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700443 opcode << ".w";
Sebastien Hertzd9e63c02013-02-22 15:19:55 +0100444
445 if (Rd.r != 0xF) {
446 args << Rd << ", ";
447 }
448 if (Rn.r != 0xF) {
449 args << Rn << ", ";
450 }
451 args << Rm;
452
453 // Shift operand.
454 bool noShift = (imm5 == 0 && shift_type != 0x3);
455 if (!noShift) {
456 args << ", ";
457 switch (shift_type) {
458 case 0x0: args << "lsl"; break;
459 case 0x1: args << "lsr"; break;
460 case 0x2: args << "asr"; break;
461 case 0x3:
462 if (imm5 == 0) {
463 args << "rrx";
464 } else {
465 args << "ror";
466 }
467 break;
468 }
469 if (shift_type != 0x3 /* rrx */) {
470 args << StringPrintf(" #%d", imm5);
471 }
472 }
473
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700474 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
475 // Co-processor instructions
476 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
477 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
478 // |---|-|--|------|----|----|----|---|---|----|
479 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
480 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
481 // |---|-|--|------|----|----|----|---|---|----|
482 // |111| |11| op3 | Rn | |copr| |op4| |
483 uint32_t op3 = (instr >> 20) & 0x3F;
484 uint32_t coproc = (instr >> 8) & 0xF;
485 uint32_t op4 = (instr >> 4) & 0x1;
Ian Rogers9af89402012-09-07 11:29:35 -0700486 if ((op3 == 2 || op3 == 2 || op3 == 6 || op3 == 7) || // 00x1x
487 (op3 >= 8 && op3 <= 15) || (op3 >= 16 && op3 <= 31)) { // 001xxx, 01xxxx
488 // Extension register load/store instructions
489 // |111|1|110|00000|0000|1111|110|000000000|
490 // |5 3|2|109|87654|3 0|54 2|10 |87 54 0|
491 // |---|-|---|-----|----|----|---|---------|
492 // |332|2|222|22222|1111|1111|110|000000000|
493 // |1 9|8|765|43210|9 6|54 2|10 |87 54 0|
494 // |---|-|---|-----|----|----|---|---------|
495 // |111|T|110| op3 | Rn | |101| |
496 // 111 0 110 01001 0011 0000 101 000000011 - ec930a03
497 if (op3 == 9 || op3 == 0xD) { // VLDM
498 // 1110 110 PUDW1 nnnn dddd 101S iiii iiii
499 uint32_t P = (instr >> 24) & 1;
500 uint32_t U = (instr >> 23) & 1;
501 uint32_t D = (instr >> 22) & 1;
502 uint32_t W = (instr >> 21) & 1;
503 uint32_t S = (instr >> 8) & 1;
504 ArmRegister Rn(instr, 16);
505 uint32_t Vd = (instr >> 12) & 0xF;
506 uint32_t imm8 = instr & 0xFF;
507 uint32_t d = (S == 0 ? ((Vd << 1) | D) : (Vd | (D << 4)));
508 if (P == 0 && U == 0 && W == 0) {
509 // TODO: 64bit transfers between ARM core and extension registers.
510 } else if (P == 0 && U == 1 && Rn.r == 13) { // VPOP
511 opcode << "vpop" << (S == 0 ? ".f64" : ".f32");
512 args << d << " .. " << (d + imm8);
513 } else if (P == 1 && W == 0) { // VLDR
514 opcode << "vldr" << (S == 0 ? ".f64" : ".f32");
515 args << d << ", [" << Rn << ", #" << imm8 << "]";
516 } else { // VLDM
517 opcode << "vldm" << (S == 0 ? ".f64" : ".f32");
518 args << Rn << ", " << d << " .. " << (d + imm8);
519 }
520 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700521 } else if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700522 if ((coproc & 0xE) == 0xA) {
523 // VFP data-processing instructions
524 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
525 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
526 // |---|-|----|----|----|----|---|-|----|-|-|----|
527 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
528 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
529 // |---|-|----|----|----|----|---|-|----|-|-|----|
530 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
531 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
532 uint32_t opc1 = (instr >> 20) & 0xF;
533 uint32_t opc2 = (instr >> 16) & 0xF;
Ian Rogers0183dd72012-09-17 23:06:51 -0700534 uint32_t opc3 = (instr >> 6) & 0x3;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700535 if ((opc1 & 0xB) == 0xB) { // 1x11
536 // Other VFP data-processing instructions.
Ian Rogers0183dd72012-09-17 23:06:51 -0700537 uint32_t D = (instr >> 22) & 0x1;
538 uint32_t Vd = (instr >> 12) & 0xF;
539 uint32_t sz = (instr >> 8) & 1;
540 uint32_t M = (instr >> 5) & 1;
541 uint32_t Vm = instr & 0xF;
542 bool dp_operation = sz == 1;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700543 switch (opc2) {
Ian Rogers0183dd72012-09-17 23:06:51 -0700544 case 0x1: // Vneg/Vsqrt
545 // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm
546 opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (dp_operation ? ".f64" : ".f32");
547 if (dp_operation) {
548 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
549 } else {
550 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
551 }
552 break;
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700553 case 0x4: case 0x5: { // Vector compare
554 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
Ian Rogers0183dd72012-09-17 23:06:51 -0700555 opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (dp_operation ? ".f64" : ".f32");
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700556 if (dp_operation) {
557 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
558 } else {
559 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
560 }
561 break;
562 }
563 }
564 }
565 }
Ian Rogers0183dd72012-09-17 23:06:51 -0700566 } else if ((op3 & 0x30) == 0x30) { // 11 xxxx
567 // Advanced SIMD
568 if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt
569 // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm
570 // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8
571 uint32_t D = (instr >> 22) & 1;
572 uint32_t Vd = (instr >> 12) & 0xF;
573 uint32_t sz = (instr >> 8) & 1;
574 uint32_t M = (instr >> 5) & 1;
575 uint32_t Vm = instr & 0xF;
576 bool dp_operation = sz == 1;
577 opcode << "vsqrt" << (dp_operation ? ".f64" : ".f32");
578 if (dp_operation) {
579 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
580 } else {
581 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
582 }
583 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700584 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800585 }
586 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800587 case 2:
588 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
589 // Data-processing (modified immediate)
590 // |111|11|10|0000|0|0000|1|111|1100|00000000|
591 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
592 // |---|--|--|----|-|----|-|---|----|--------|
593 // |332|22|22|2222|2|1111|1|111|1100|00000000|
594 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
595 // |---|--|--|----|-|----|-|---|----|--------|
596 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
597 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800598 uint32_t i = (instr >> 26) & 1;
599 uint32_t op3 = (instr >> 21) & 0xF;
600 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700601 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800602 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700603 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800604 uint32_t imm8 = instr & 0xFF;
Jeff Hao7cb0f9c2013-02-04 16:15:27 -0800605 int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
606 if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
607 if (op3 == 0x2) {
608 opcode << "mov";
609 if (S == 1) {
610 opcode << "s";
611 }
612 opcode << ".w";
613 } else {
614 opcode << "mvn";
615 if (S == 1) {
616 opcode << "s";
617 }
618 }
619 args << Rd << ", ThumbExpand(" << imm32 << ")";
620 } else if (Rd.r == 0xF && S == 1 &&
621 (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
622 if (op3 == 0x0) {
623 opcode << "tst";
624 } else if (op3 == 0x4) {
625 opcode << "teq";
626 } else if (op3 == 0x8) {
627 opcode << "cmw";
628 } else {
629 opcode << "cmp.w";
630 }
631 args << Rn << ", ThumbExpand(" << imm32 << ")";
632 } else {
633 switch (op3) {
634 case 0x0: opcode << "and"; break;
635 case 0x1: opcode << "bic"; break;
636 case 0x2: opcode << "orr"; break;
637 case 0x3: opcode << "orn"; break;
638 case 0x4: opcode << "eor"; break;
639 case 0x8: opcode << "add"; break;
640 case 0xA: opcode << "adc"; break;
641 case 0xB: opcode << "sbc"; break;
642 case 0xD: opcode << "sub"; break;
643 case 0xE: opcode << "rsb"; break;
644 default: opcode << "UNKNOWN DPMI-" << op3; break;
645 }
646 if (S == 1) {
647 opcode << "s";
648 }
649 args << Rd << ", " << Rn << ", ThumbExpand(" << imm32 << ")";
Ian Rogers40627db2012-03-04 17:31:09 -0800650 }
Ian Rogers40627db2012-03-04 17:31:09 -0800651 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
652 // Data-processing (plain binary immediate)
653 // |111|11|10|00000|0000|1|111110000000000|
654 // |5 3|21|09|87654|3 0|5|4 0 5 0|
655 // |---|--|--|-----|----|-|---------------|
656 // |332|22|22|22222|1111|1|111110000000000|
657 // |1 9|87|65|43210|9 6|5|4 0 5 0|
658 // |---|--|--|-----|----|-|---------------|
659 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
660 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800661 switch (op3) {
Ian Rogers55019132013-02-08 01:05:23 -0800662 case 0x00: case 0x0A: {
663 // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Ian Rogers66a3fca2012-04-09 19:51:34 -0700664 ArmRegister Rd(instr, 8);
665 ArmRegister Rn(instr, 16);
666 uint32_t i = (instr >> 26) & 1;
667 uint32_t imm3 = (instr >> 12) & 0x7;
668 uint32_t imm8 = instr & 0xFF;
669 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
670 if (Rn.r != 0xF) {
Ian Rogers55019132013-02-08 01:05:23 -0800671 opcode << (op3 == 0 ? "addw" : "subw");
Ian Rogers66a3fca2012-04-09 19:51:34 -0700672 args << Rd << ", " << Rn << ", #" << imm12;
673 } else {
674 opcode << "adr";
675 args << Rd << ", ";
Ian Rogers55019132013-02-08 01:05:23 -0800676 DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
Ian Rogers66a3fca2012-04-09 19:51:34 -0700677 }
678 break;
679 }
Ian Rogers55019132013-02-08 01:05:23 -0800680 case 0x04: case 0x0C: {
681 // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700682 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800683 uint32_t i = (instr >> 26) & 1;
684 uint32_t imm3 = (instr >> 12) & 0x7;
685 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700686 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800687 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Ian Rogers55019132013-02-08 01:05:23 -0800688 opcode << (op3 == 0x04 ? "movw" : "movt");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700689 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800690 break;
691 }
jeffhaoeae26912013-01-28 16:29:54 -0800692 case 0x16: {
693 // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
694 ArmRegister Rd(instr, 8);
695 ArmRegister Rn(instr, 16);
696 uint32_t msb = instr & 0x1F;
697 uint32_t imm2 = (instr >> 6) & 0x3;
698 uint32_t imm3 = (instr >> 12) & 0x7;
699 uint32_t lsb = (imm3 << 2) | imm2;
700 uint32_t width = msb - lsb + 1;
701 if (Rn.r != 0xF) {
702 opcode << "bfi";
703 args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
704 } else {
705 opcode << "bfc";
706 args << Rd << ", #" << lsb << ", #" << width;
707 }
708 break;
709 }
Ian Rogers40627db2012-03-04 17:31:09 -0800710 default:
711 break;
712 }
713 } else {
714 // Branches and miscellaneous control
715 // |111|11|1000000|0000|1|111|1100|00000000|
716 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
717 // |---|--|-------|----|-|---|----|--------|
718 // |332|22|2222222|1111|1|111|1100|00000000|
719 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
720 // |---|--|-------|----|-|---|----|--------|
721 // |111|10| op2 | |1|op3|op4 | |
722
723 uint32_t op3 = (instr >> 12) & 7;
724 //uint32_t op4 = (instr >> 8) & 0xF;
725 switch (op3) {
726 case 0:
727 if ((op2 & 0x38) != 0x38) {
728 // Conditional branch
729 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
730 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
731 // |---|--|-|----|------|-|-|--|-|--|-----------|
732 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
733 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
734 // |---|--|-|----|------|-|-|--|-|--|-----------|
735 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
736 uint32_t S = (instr >> 26) & 1;
737 uint32_t J2 = (instr >> 11) & 1;
738 uint32_t J1 = (instr >> 13) & 1;
739 uint32_t imm6 = (instr >> 16) & 0x3F;
740 uint32_t imm11 = instr & 0x7FF;
741 uint32_t cond = (instr >> 22) & 0xF;
742 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
743 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700744 opcode << "b";
745 DumpCond(opcode, cond);
746 opcode << ".w";
747 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers9af89402012-09-07 11:29:35 -0700748 } else if (op2 == 0x3B) {
749 // Miscellaneous control instructions
750 uint32_t op5 = (instr >> 4) & 0xF;
751 switch (op5) {
752 case 4: opcode << "dsb"; break;
753 case 5: opcode << "dmb"; break;
754 case 6: opcode << "isb"; break;
755 }
Ian Rogers40627db2012-03-04 17:31:09 -0800756 }
757 break;
758 case 2:
Ian Rogersd0876a92013-02-08 11:30:38 -0800759 if ((op2 & 0x38) == 0x38) {
760 if (op2 == 0x7F) {
761 opcode << "udf";
762 }
763 break;
764 }
765 // Else deliberate fall-through to B.
766 case 1: case 3: {
767 // B
768 // |111|11|1|0000|000000|11|1 |1|1 |10000000000|
769 // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
770 // |---|--|-|----|------|--|--|-|--|-----------|
771 // |332|22|2|2222|221111|11|1 |1|1 |10000000000|
772 // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
773 // |---|--|-|----|------|--|--|-|--|-----------|
774 // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
775 // |111|10|S| imm10 |10|J1|1|J2| imm11 |
776 uint32_t S = (instr >> 26) & 1;
777 uint32_t cond = (instr >> 22) & 0xF;
778 uint32_t J2 = (instr >> 11) & 1;
779 uint32_t form = (instr >> 12) & 1;
780 uint32_t J1 = (instr >> 13) & 1;
781 uint32_t imm10 = (instr >> 16) & 0x3FF;
782 uint32_t imm6 = (instr >> 16) & 0x3F;
783 uint32_t imm11 = instr & 0x7FF;
784 opcode << "b";
785 int32_t imm32;
786 if (form == 0) {
787 DumpCond(opcode, cond);
788 imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
789 imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
790 } else {
791 uint32_t I1 = ~(J1 ^ S);
792 uint32_t I2 = ~(J2 ^ S);
793 imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
794 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
795 }
796 opcode << ".w";
797 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800798 break;
Ian Rogersd0876a92013-02-08 11:30:38 -0800799 }
Ian Rogers40627db2012-03-04 17:31:09 -0800800 case 4: case 6: case 5: case 7: {
801 // BL, BLX (immediate)
802 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
803 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
804 // |---|--|-|----------|--|--|-|--|-----------|
805 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
806 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
807 // |---|--|-|----------|--|--|-|--|-----------|
808 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
809 uint32_t S = (instr >> 26) & 1;
810 uint32_t J2 = (instr >> 11) & 1;
811 uint32_t L = (instr >> 12) & 1;
812 uint32_t J1 = (instr >> 13) & 1;
813 uint32_t imm10 = (instr >> 16) & 0x3FF;
814 uint32_t imm11 = instr & 0x7FF;
815 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700816 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800817 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700818 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800819 }
820 uint32_t I1 = ~(J1 ^ S);
821 uint32_t I2 = ~(J2 ^ S);
822 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
823 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700824 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800825 break;
826 }
827 }
828 }
829 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800830 case 3:
831 switch (op2) {
832 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
833 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
834 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800835 // |111|11|100|000|0|0000|1111|110000|000000|
836 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
837 // |---|--|---|---|-|----|----|------|------|
838 // |332|22|222|222|2|1111|1111|110000|000000|
839 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
840 // |---|--|---|---|-|----|----|------|------|
841 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800842 uint32_t op3 = (instr >> 21) & 7;
843 //uint32_t op4 = (instr >> 6) & 0x3F;
844 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700845 case 0x0: case 0x4: {
846 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
847 // 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 -0700848 ArmRegister Rn(instr, 16);
849 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700850 opcode << "strb";
851 if ((instr & 0x800) != 0) {
852 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700853 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700854 } else {
855 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700856 ArmRegister Rm(instr, 0);
857 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700858 if (imm2 != 0) {
859 args << ", " << "lsl #" << imm2;
860 }
861 args << "]";
862 }
863 break;
864 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800865 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700866 ArmRegister Rn(instr, 16);
867 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800868 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700869 if ((instr & 0x800) != 0) {
870 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
871 uint32_t P = (instr >> 10) & 1;
872 uint32_t U = (instr >> 9) & 1;
873 uint32_t W = (instr >> 8) & 1;
874 uint32_t imm8 = instr & 0xFF;
875 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
876 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
877 opcode << "push";
878 args << Rt;
879 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
880 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800881 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700882 if (P == 1 && U == 1 && W == 0) {
883 opcode << "strt";
884 } else {
885 opcode << "str";
886 }
887 args << Rt << ", [" << Rn;
888 if (P == 0 && W == 1) {
889 args << "], #" << imm32;
890 } else {
891 args << ", #" << imm32 << "]";
892 if (W == 1) {
893 args << "!";
894 }
Ian Rogers40627db2012-03-04 17:31:09 -0800895 }
896 }
Ian Rogers66a3fca2012-04-09 19:51:34 -0700897 } else {
898 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
899 ArmRegister Rn(instr, 16);
900 ArmRegister Rt(instr, 12);
901 ArmRegister Rm(instr, 0);
902 uint32_t imm2 = (instr >> 4) & 3;
903 opcode << "str.w";
904 args << Rt << ", [" << Rn << ", " << Rm;
905 if (imm2 != 0) {
906 args << ", lsl #" << imm2;
907 }
908 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800909 }
910 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700911 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800912 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700913 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700914 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800915 }
Ian Rogers40627db2012-03-04 17:31:09 -0800916 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800917 }
918 }
919
920 break;
921 }
jeffhaoeae26912013-01-28 16:29:54 -0800922 case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011
923 // Load halfword
924 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
925 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
926 // |---|--|--|---|--|-|----|----|------|------|
927 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
928 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
929 // |---|--|--|---|--|-|----|----|------|------|
930 // |111|11|00|op3|01|1| Rn | Rt | op4 | |
931 // |111|11| op2 | | | imm12 |
932 uint32_t op3 = (instr >> 23) & 3;
933 ArmRegister Rn(instr, 16);
934 ArmRegister Rt(instr, 12);
935 if (Rt.r != 15) {
936 if (op3 == 1) {
937 // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii
938 uint32_t imm12 = instr & 0xFFF;
939 opcode << "ldrh.w";
940 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
941 if (Rn.r == 9) {
942 args << " ; ";
943 Thread::DumpThreadOffset(args, imm12, 4);
944 } else if (Rn.r == 15) {
945 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
946 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
947 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
948 }
949 } else if (op3 == 3) {
950 // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii
951 uint32_t imm12 = instr & 0xFFF;
952 opcode << "ldrsh.w";
953 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
954 if (Rn.r == 9) {
955 args << " ; ";
956 Thread::DumpThreadOffset(args, imm12, 4);
957 } else if (Rn.r == 15) {
958 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
959 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
960 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
961 }
962 }
963 }
964 break;
965 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800966 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
967 // Load word
968 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
969 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
970 // |---|--|--|---|--|-|----|----|------|------|
971 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
972 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
973 // |---|--|--|---|--|-|----|----|------|------|
974 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
975 // |111|11| op2 | | | imm12 |
976 uint32_t op3 = (instr >> 23) & 3;
977 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700978 ArmRegister Rn(instr, 16);
979 ArmRegister Rt(instr, 12);
980 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800981 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
982 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
983 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700984 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700985 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700986 if (Rn.r == 9) {
987 args << " ; ";
988 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -0700989 } 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));
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700993 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800994 } else if (op4 == 0) {
995 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
996 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700997 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700998 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700999 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001000 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001001 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001002 }
Elliott Hughescbf0b612012-03-15 16:23:47 -07001003 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001004 } else {
1005 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
1006 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001007 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001008 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001009 }
1010 break;
1011 }
1012 }
1013 default:
1014 break;
1015 }
Ian Rogers9af89402012-09-07 11:29:35 -07001016
1017 // Apply any IT-block conditions to the opcode if necessary.
1018 if (!it_conditions_.empty()) {
1019 opcode << it_conditions_.back();
1020 it_conditions_.pop_back();
1021 }
1022
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001023 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001024 return 4;
1025}
1026
1027size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
1028 uint16_t instr = ReadU16(instr_ptr);
1029 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
1030 if (is_32bit) {
1031 return DumpThumb32(os, instr_ptr);
1032 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001033 std::ostringstream opcode;
1034 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001035 uint16_t opcode1 = instr >> 10;
1036 if (opcode1 < 0x10) {
1037 // shift (immediate), add, subtract, move, and compare
1038 uint16_t opcode2 = instr >> 9;
1039 switch (opcode2) {
1040 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
1041 case 0x8: case 0x9: case 0xA: case 0xB: {
Sebastien Hertze78500c2013-02-19 14:29:52 +01001042 // Logical shift left - 00 000xx iii mmm ddd
1043 // Logical shift right - 00 001xx iii mmm ddd
1044 // Arithmetic shift right - 00 010xx iii mmm ddd
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001045 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001046 ThumbRegister rm(instr, 3);
Sebastien Hertze78500c2013-02-19 14:29:52 +01001047 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001048 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001049 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001050 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001051 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001052 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001053 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001054 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001055 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001056 break;
1057 }
1058 case 0xC: case 0xD: case 0xE: case 0xF: {
1059 // Add register - 00 01100 mmm nnn ddd
1060 // Sub register - 00 01101 mmm nnn ddd
1061 // Add 3-bit immediate - 00 01110 iii nnn ddd
1062 // Sub 3-bit immediate - 00 01111 iii nnn ddd
1063 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001064 ThumbRegister Rn(instr, 3);
1065 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001066 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001067 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001068 } else {
1069 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001070 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001071 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001072 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001073 }
1074 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001075 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001076 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -07001077 ArmRegister Rm(imm3_or_Rm);
1078 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001079 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001080 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001081 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001082 break;
1083 }
1084 case 0x10: case 0x11: case 0x12: case 0x13:
1085 case 0x14: case 0x15: case 0x16: case 0x17:
1086 case 0x18: case 0x19: case 0x1A: case 0x1B:
1087 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
1088 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
1089 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
1090 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
1091 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -07001092 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001093 uint16_t imm8 = instr & 0xFF;
1094 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001095 case 4: opcode << "movs"; break;
1096 case 5: opcode << "cmp"; break;
1097 case 6: opcode << "adds"; break;
1098 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001099 }
Elliott Hughes630e77d2012-03-22 19:20:56 -07001100 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001101 break;
1102 }
1103 default:
1104 break;
1105 }
Ian Rogersad03ef52012-03-18 19:34:47 -07001106 } else if (opcode1 == 0x10) {
1107 // Data-processing
1108 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001109 ThumbRegister rm(instr, 3);
1110 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -07001111 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -07001112 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001113 } else if (opcode1 == 0x11) {
1114 // Special data instructions and branch and exchange
1115 uint16_t opcode2 = (instr >> 6) & 0x0F;
1116 switch (opcode2) {
1117 case 0x0: case 0x1: case 0x2: case 0x3: {
1118 // Add low registers - 010001 0000 xxxxxx
1119 // Add high registers - 010001 0001/001x xxxxxx
1120 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001121 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001122 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001123 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001124 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001125 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001126 break;
1127 }
1128 case 0x8: case 0x9: case 0xA: case 0xB: {
1129 // Move low registers - 010001 1000 xxxxxx
1130 // Move high registers - 010001 1001/101x xxxxxx
1131 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001132 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001133 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001134 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001135 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001136 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001137 break;
1138 }
1139 case 0x5: case 0x6: case 0x7: {
1140 // Compare high registers - 010001 0101/011x xxxxxx
1141 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001142 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001143 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001144 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -07001145 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -07001146 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001147 break;
1148 }
1149 case 0xC: case 0xD: case 0xE: case 0xF: {
1150 // Branch and exchange - 010001 110x xxxxxx
1151 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -07001152 ArmRegister rm(instr, 3);
1153 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
1154 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001155 break;
1156 }
1157 default:
1158 break;
1159 }
jeffhaoeae26912013-01-28 16:29:54 -08001160 } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x
1161 ThumbRegister Rt(instr, 8);
1162 uint16_t imm8 = instr & 0xFF;
1163 opcode << "ldr";
1164 args << Rt << ", [pc, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001165 } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx
1166 (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx
1167 (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx
1168 // Load/store single data item
1169 uint16_t opA = (instr >> 12) & 0xF;
1170 if (opA == 0x5) {
1171 uint16_t opB = (instr >> 9) & 0x7;
1172 ThumbRegister Rm(instr, 6);
1173 ThumbRegister Rn(instr, 3);
1174 ThumbRegister Rt(instr, 0);
1175 switch(opB) {
1176 case 0: opcode << "str"; break;
1177 case 1: opcode << "strh"; break;
1178 case 2: opcode << "strb"; break;
1179 case 3: opcode << "ldrsb"; break;
1180 case 4: opcode << "ldr"; break;
1181 case 5: opcode << "ldrh"; break;
1182 case 6: opcode << "ldrb"; break;
1183 case 7: opcode << "ldrsh"; break;
1184 }
1185 args << Rt << ", [" << Rn << ", " << Rm << "]";
1186 } else if (opA == 9) {
1187 uint16_t opB = (instr >> 11) & 1;
1188 ThumbRegister Rt(instr, 8);
1189 uint16_t imm8 = instr & 0xFF;
1190 opcode << (opB == 0 ? "str" : "ldr");
Ian Rogers137e88f2012-10-08 17:46:47 -07001191 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogersd83bc362012-09-07 17:43:13 -07001192 } else {
1193 uint16_t imm5 = (instr >> 6) & 0x1F;
1194 uint16_t opB = (instr >> 11) & 1;
1195 ThumbRegister Rn(instr, 3);
1196 ThumbRegister Rt(instr, 0);
1197 switch(opA) {
1198 case 6:
1199 imm5 <<= 2;
1200 opcode << (opB == 0 ? "str" : "ldr");
1201 break;
1202 case 7:
1203 imm5 <<= 0;
1204 opcode << (opB == 0 ? "strb" : "ldrb");
1205 break;
1206 case 8:
1207 imm5 <<= 1;
1208 opcode << (opB == 0 ? "strh" : "ldrh");
1209 break;
1210 }
1211 args << Rt << ", [" << Rn << ", #" << imm5 << "]";
1212 }
jeffhaoeae26912013-01-28 16:29:54 -08001213 } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx
1214 uint32_t imm8 = instr & 0xFF;
1215 uint32_t cond = (instr >> 8) & 0xF;
1216 opcode << "b";
1217 DumpCond(opcode, cond);
1218 DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1));
Ian Rogers9af89402012-09-07 11:29:35 -07001219 } else if ((instr & 0xF800) == 0xA800) {
1220 // Generate SP-relative address
1221 ThumbRegister rd(instr, 8);
1222 int imm8 = instr & 0xFF;
1223 opcode << "add";
1224 args << rd << ", sp, #" << (imm8 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001225 } else if ((instr & 0xF000) == 0xB000) {
1226 // Miscellaneous 16-bit instructions
1227 uint16_t opcode2 = (instr >> 5) & 0x7F;
1228 switch (opcode2) {
1229 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
1230 // Add immediate to SP - 1011 00000 ii iiiii
1231 // Subtract immediate from SP - 1011 00001 ii iiiii
1232 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001233 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -07001234 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001235 break;
1236 }
Ian Rogers087b2412012-03-21 01:30:32 -07001237 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
Ian Rogersebbc5772012-04-11 17:00:08 -07001238 case 0x0C: case 0x0D: case 0x0E: case 0x0F:
Ian Rogers55019132013-02-08 01:05:23 -08001239 case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx
1240 case 0x1C: case 0x1D: case 0x1E: case 0x1F:
Ian Rogersebbc5772012-04-11 17:00:08 -07001241 case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx
Ian Rogers55019132013-02-08 01:05:23 -08001242 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1243 case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx
1244 case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
Ian Rogers087b2412012-03-21 01:30:32 -07001245 // CBNZ, CBZ
1246 uint16_t op = (instr >> 11) & 1;
1247 uint16_t i = (instr >> 9) & 1;
1248 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001249 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -07001250 opcode << (op != 0 ? "cbnz" : "cbz");
1251 uint32_t imm32 = (i << 7) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001252 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -07001253 DumpBranchTarget(args, instr_ptr + 4, imm32);
1254 break;
1255 }
Ian Rogers40627db2012-03-04 17:31:09 -08001256 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
1257 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
1258 // If-Then, and hints
1259 uint16_t opA = (instr >> 4) & 0xF;
1260 uint16_t opB = instr & 0xF;
1261 if (opB == 0) {
1262 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -07001263 case 0: opcode << "nop"; break;
1264 case 1: opcode << "yield"; break;
1265 case 2: opcode << "wfe"; break;
1266 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -08001267 default: break;
1268 }
1269 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -07001270 uint32_t first_cond = opA;
1271 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -07001272 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -07001273
1274 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
1275 // and store up the actual condition codes we'll want to add to the next few opcodes.
1276 size_t count = 3 - CTZ(mask);
1277 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
1278 for (size_t i = 0; i < count; ++i) {
1279 bool positive_cond = ((first_cond & 1) != 0);
1280 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
1281 if (positive_mask == positive_cond) {
1282 opcode << 't';
1283 it_conditions_[i] = kConditionCodeNames[first_cond];
1284 } else {
1285 opcode << 'e';
1286 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
1287 }
1288 }
1289 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
1290
1291 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
1292 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -08001293 }
1294 break;
1295 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001296 default:
1297 break;
1298 }
1299 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
1300 ((instr & 0xE000) == 0x8000)) {
1301 // Load/store single data item
1302 uint16_t opA = instr >> 12;
1303 //uint16_t opB = (instr >> 9) & 7;
1304 switch (opA) {
1305 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001306 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
1307 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001308 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001309 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001310 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -07001311 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1312 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001313 break;
1314 }
1315 case 0x9: {
1316 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
1317 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
1318 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -07001319 ThumbRegister Rt(instr, 8);
1320 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
1321 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001322 break;
1323 }
1324 default:
1325 break;
1326 }
Ian Rogers40627db2012-03-04 17:31:09 -08001327 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
1328 uint16_t imm11 = instr & 0x7FFF;
1329 int32_t imm32 = imm11 << 1;
1330 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -07001331 opcode << "b";
1332 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001333 }
Elliott Hughes105afd22012-04-10 15:04:25 -07001334
1335 // Apply any IT-block conditions to the opcode if necessary.
1336 if (!it_conditions_.empty()) {
1337 opcode << it_conditions_.back();
1338 it_conditions_.pop_back();
1339 }
1340
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001341 os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001342 }
1343 return 2;
1344}
1345
1346} // namespace arm
1347} // namespace art