blob: 8716a71bff5ae7ccdb22650232c623a0e095b9e8 [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
19#include "stringprintf.h"
20
21#include <iostream>
22
23namespace art {
24namespace arm {
25
26DisassemblerArm::DisassemblerArm() {
27}
28
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080029void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
30 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
31 for (const uint8_t* cur = begin; cur < end; cur += 4) {
32 DumpArm(os, cur);
33 }
34 } else {
35 // remove thumb specifier bits
36 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
37 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
38 for (const uint8_t* cur = begin; cur < end;) {
39 cur += DumpThumb16(os, cur);
40 }
41 }
42}
43
Elliott Hughes77405792012-03-15 15:22:12 -070044static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070045 "eq", // 0000 - equal
46 "ne", // 0001 - not-equal
47 "cs", // 0010 - carry-set, greater than, equal or unordered
48 "cc", // 0011 - carry-clear, less than
49 "mi", // 0100 - minus, negative
50 "pl", // 0101 - plus, positive or zero
51 "vs", // 0110 - overflow
52 "vc", // 0111 - no overflow
53 "hi", // 1000 - unsigned higher
54 "ls", // 1001 - unsigned lower or same
55 "ge", // 1010 - signed greater than or equal
56 "lt", // 1011 - signed less than
57 "gt", // 1100 - signed greater than
58 "le", // 1101 - signed less than or equal
59 "", // 1110 - always
60 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080061};
62
63void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
64 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070065 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080066 } else {
67 os << "Unexpected condition: " << cond;
68 }
69}
70
Ian Rogers40627db2012-03-04 17:31:09 -080071void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
72 os << imm32 << " (" << reinterpret_cast<const void*>(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080073}
74
75static uint32_t ReadU16(const uint8_t* ptr) {
76 return ptr[0] | (ptr[1] << 8);
77}
78
79static uint32_t ReadU32(const uint8_t* ptr) {
80 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
81}
82
Elliott Hughes77405792012-03-15 15:22:12 -070083static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070084 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
85 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -070086};
87
Ian Rogersad03ef52012-03-18 19:34:47 -070088static const char* kThumbDataProcessingOperations[] = {
89 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
90 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
91};
92
Elliott Hughes77405792012-03-15 15:22:12 -070093struct ArmRegister {
94 ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -070095 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -070096 uint32_t r;
97};
98std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
99 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700100 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700101 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700102 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700103 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700104 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700105 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700106 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700107 }
108 return os;
109}
110
Elliott Hughes630e77d2012-03-22 19:20:56 -0700111struct ThumbRegister : ArmRegister {
112 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700113};
114
115struct Rm {
116 Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
117 uint32_t shift;
118 ArmRegister rm;
119};
120std::ostream& operator<<(std::ostream& os, const Rm& r) {
121 os << r.rm;
122 if (r.shift != 0) {
123 os << "-shift-" << r.shift; // TODO
124 }
125 return os;
126}
127
128struct Imm12 {
129 Imm12(uint32_t instruction) : rotate((instruction >> 8) & 0xf), imm(instruction & 0xff) {}
130 uint32_t rotate;
131 uint32_t imm;
132};
133std::ostream& operator<<(std::ostream& os, const Imm12& rhs) {
134 uint32_t imm = (rhs.imm >> (2 * rhs.rotate)) | (rhs.imm << (32 - (2 * rhs.rotate)));
135 os << "#" << imm;
136 return os;
137}
138
139struct RegisterList {
140 RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
141 uint32_t register_list;
142};
143std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
144 if (rhs.register_list == 0) {
145 os << "<no register list?>";
146 return os;
147 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700148 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700149 bool first = true;
150 for (size_t i = 0; i < 16; i++) {
151 if ((rhs.register_list & (1 << i)) != 0) {
152 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700153 first = false;
154 } else {
155 os << ", ";
156 }
157 os << ArmRegister(i);
158 }
159 }
160 os << "}";
161 return os;
162}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800163
164void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700165 uint32_t instruction = ReadU32(instr_ptr);
166 uint32_t cond = (instruction >> 28) & 0xf;
167 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700168 std::ostringstream opcode;
169 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700170 switch (op1) {
171 case 0:
172 case 1: // Data processing instructions.
173 {
174 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughescbf0b612012-03-15 16:23:47 -0700175 opcode << (((instruction >> 5) & 1) ? "blx" : "bx");
176 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700177 break;
178 }
179 bool i = (instruction & (1 << 25)) != 0;
180 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700181 opcode << kDataProcessingOperations[(instruction >> 21) & 0xf]
182 << kConditionCodeNames[cond]
183 << (s ? "s" : "");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700184 args << ArmRegister(instruction, 12) << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700185 if (i) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700186 args << ArmRegister(instruction, 16) << ", " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700187 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700188 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700189 }
190 }
191 break;
192 case 2: // Load/store word and unsigned byte.
193 {
194 bool p = (instruction & (1 << 24)) != 0;
195 bool b = (instruction & (1 << 22)) != 0;
196 bool w = (instruction & (1 << 21)) != 0;
197 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700198 opcode << (l ? "ldr" : "str") << (b ? "b" : "") << kConditionCodeNames[cond];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700199 args << ArmRegister(instruction, 12) << ", ";
200 ArmRegister rn(instruction, 16);
201 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700202 UNIMPLEMENTED(FATAL) << "literals";
203 } else {
204 bool wback = !p || w;
205 if (p && !wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700206 args << "[" << rn << ", " << Imm12(instruction) << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700207 } else if (p && wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700208 args << "[" << rn << ", " << Imm12(instruction) << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700209 } else if (!p && wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700210 args << "[" << rn << "], " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700211 } else {
212 LOG(FATAL) << p << " " << w;
213 }
214 }
215 }
216 break;
217 case 4: // Load/store multiple.
218 {
219 bool p = (instruction & (1 << 24)) != 0;
220 bool u = (instruction & (1 << 23)) != 0;
221 bool w = (instruction & (1 << 21)) != 0;
222 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700223 opcode << (l ? "ldm" : "stm")
224 << (u ? 'i' : 'd')
225 << (p ? 'b' : 'a')
226 << kConditionCodeNames[cond];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700227 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700228 }
229 break;
230 default:
Elliott Hughescbf0b612012-03-15 16:23:47 -0700231 opcode << "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700232 break;
233 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700234 // TODO: a more complete ARM disassembler could generate wider opcodes.
235 os << StringPrintf("\t\t\t%p: %08x\t%-7s ", instr_ptr, instruction, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800236}
237
238size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
239 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
240 // |111|1 1|1000000|0000|1111110000000000|
241 // |5 3|2 1|0987654|3 0|5 0 5 0|
242 // |---|---|-------|----|----------------|
243 // |332|2 2|2222222|1111|1111110000000000|
244 // |1 9|8 7|6543210|9 6|5 0 5 0|
245 // |---|---|-------|----|----------------|
246 // |111|op1| op2 | | |
247 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700248 if (op1 == 0) {
249 return DumpThumb16(os, instr_ptr);
250 }
251
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800252 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700253 std::ostringstream opcode;
254 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800255 switch (op1) {
256 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800257 break;
258 case 1:
259 switch (op2) {
260 case 0x00: case 0x01: case 0x02: case 0x03: case 0x08: case 0x09: case 0x0A: case 0x0B:
261 case 0x10: case 0x11: case 0x12: case 0x13: case 0x18: case 0x19: case 0x1A: case 0x1B: {
262 // |111|11|10|00|0|00|0000|1111110000000000|
263 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
264 // |---|--|--|--|-|--|----|----------------|
265 // |332|22|22|22|2|22|1111|1111110000000000|
266 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
267 // |---|--|--|--|-|--|----|----------------|
268 // |111|01|00|op|0|WL| Rn | |
269 // |111|01| op2 | | |
270 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
271 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
272 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
273 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
274 uint32_t op = (instr >> 23) & 3;
275 uint32_t W = (instr >> 21) & 1;
276 uint32_t L = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700277 ArmRegister Rn(instr, 16);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800278 if (op == 1 || op == 2) {
279 if (op == 1) {
280 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700281 opcode << "stm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700282 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800283 } else {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700284 if (Rn.r != 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700285 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700286 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800287 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700288 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800289 }
290 }
291 } else {
292 if (L == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700293 if (Rn.r != 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700294 opcode << "stmdb";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700295 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800296 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700297 opcode << "push";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800298 }
299 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700300 opcode << "ldmdb";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700301 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800302 }
303 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700304 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800305 }
306 break;
307 }
Ian Rogers087b2412012-03-21 01:30:32 -0700308 case 0x20: case 0x21: case 0x22: case 0x23: // 01xxxxx
309 case 0x24: case 0x25: case 0x26: case 0x27:
310 case 0x28: case 0x29: case 0x2A: case 0x2B:
311 case 0x2C: case 0x2D: case 0x2E: case 0x2F:
312 case 0x30: case 0x31: case 0x32: case 0x33:
313 case 0x34: case 0x35: case 0x36: case 0x37:
314 case 0x38: case 0x39: case 0x3A: case 0x3B:
315 case 0x3C: case 0x3D: case 0x3E: case 0x3F: {
316 // Data-processing (shifted register)
317 // |111|1110|0000|0|0000|1111|1100|0000|0000|
318 // |5 3|2109|8765|4|3 0|5 |10 8|7 5 |3 0|
319 // |---|----|----|-|----|----|----|----|----|
320 // |332|2222|2222|2|1111|1111|1100|0000|0000|
321 // |1 9|8765|4321|0|9 6|5 |10 8|7 5 |3 0|
322 // |---|----|----|-|----|----|----|----|----|
323 // |111|0101| op3|S| Rn | | Rd | | Rm |
324 uint32_t op3 = (instr >> 21) & 0xF;
325 uint32_t S = (instr >> 20) & 1;
326 uint32_t Rn = (instr >> 16) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700327 ArmRegister Rd(instr, 8);
328 ArmRegister Rm(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -0700329 switch (op3) {
330 case 0x0:
331 if (Rn != 0xF) {
332 opcode << "and";
333 } else {
334 opcode << "tst";
335 S = 0; // don't print 's'
336 }
337 break;
338 case 0x1: opcode << "bic"; break;
339 case 0x2:
340 if (Rn != 0xF) {
341 opcode << "orr";
342 } else {
343 opcode << "mov";
344 }
345 break;
346 case 0x3:
347 if (Rn != 0xF) {
348 opcode << "orn";
349 } else {
350 opcode << "mvn";
351 }
352 break;
353 case 0x4:
354 if (Rn != 0xF) {
355 opcode << "eor";
356 } else {
357 opcode << "teq";
358 S = 0; // don't print 's'
359 }
360 break;
361 case 0x6: opcode << "pkh"; break;
362 case 0x8:
363 if (Rn != 0xF) {
364 opcode << "add";
365 } else {
366 opcode << "cmn";
367 S = 0; // don't print 's'
368 }
369 break;
370 case 0xA: opcode << "adc"; break;
371 case 0xB: opcode << "sbc"; break;
372 }
373
374 if (S == 1) {
375 opcode << "s";
376 }
377 opcode << ".w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700378 args << Rd << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700379 break;
380 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800381 default:
382 break;
383 }
384 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800385 case 2:
386 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
387 // Data-processing (modified immediate)
388 // |111|11|10|0000|0|0000|1|111|1100|00000000|
389 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
390 // |---|--|--|----|-|----|-|---|----|--------|
391 // |332|22|22|2222|2|1111|1|111|1100|00000000|
392 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
393 // |---|--|--|----|-|----|-|---|----|--------|
394 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
395 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
396 // 111 10 00 0110 0 0000 1 000 0000 10101101 - f0c080ad
397 uint32_t i = (instr >> 26) & 1;
398 uint32_t op3 = (instr >> 21) & 0xF;
399 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700400 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800401 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700402 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800403 uint32_t imm8 = instr & 0xFF;
404 int32_t imm32 = (i << 12) | (imm3 << 8) | imm8;
405 switch (op3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700406 case 0x0: opcode << "and"; break;
407 case 0x1: opcode << "bic"; break;
408 case 0x2: opcode << "orr"; break;
409 case 0x3: opcode << "orn"; break;
410 case 0x4: opcode << "eor"; break;
411 case 0x8: opcode << "add"; break;
412 case 0xA: opcode << "adc"; break;
413 case 0xB: opcode << "sbc"; break;
414 case 0xD: opcode << "sub"; break;
415 case 0xE: opcode << "rsb"; break;
416 default: opcode << "UNKNOWN DPMI-" << op3; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800417 }
418 if (S == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700419 opcode << "s";
Ian Rogers40627db2012-03-04 17:31:09 -0800420 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700421 args << Rd << ", " << Rn << ", ThumbExpand(" << imm32 << ")";
Ian Rogers40627db2012-03-04 17:31:09 -0800422 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
423 // Data-processing (plain binary immediate)
424 // |111|11|10|00000|0000|1|111110000000000|
425 // |5 3|21|09|87654|3 0|5|4 0 5 0|
426 // |---|--|--|-----|----|-|---------------|
427 // |332|22|22|22222|1111|1|111110000000000|
428 // |1 9|87|65|43210|9 6|5|4 0 5 0|
429 // |---|--|--|-----|----|-|---------------|
430 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
431 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800432 switch (op3) {
433 case 0x04: {
434 // MOVW Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700435 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800436 uint32_t i = (instr >> 26) & 1;
437 uint32_t imm3 = (instr >> 12) & 0x7;
438 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700439 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800440 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700441 opcode << "movw";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700442 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800443 break;
444 }
445 case 0x0A: {
446 // SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700447 ArmRegister Rd(instr, 8);
448 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800449 uint32_t i = (instr >> 26) & 1;
450 uint32_t imm3 = (instr >> 12) & 0x7;
451 uint32_t imm8 = instr & 0xFF;
452 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700453 opcode << "sub.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700454 args << Rd << ", " << Rn << ", #" << imm12;
Ian Rogers40627db2012-03-04 17:31:09 -0800455 break;
456 }
457 default:
458 break;
459 }
460 } else {
461 // Branches and miscellaneous control
462 // |111|11|1000000|0000|1|111|1100|00000000|
463 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
464 // |---|--|-------|----|-|---|----|--------|
465 // |332|22|2222222|1111|1|111|1100|00000000|
466 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
467 // |---|--|-------|----|-|---|----|--------|
468 // |111|10| op2 | |1|op3|op4 | |
469
470 uint32_t op3 = (instr >> 12) & 7;
471 //uint32_t op4 = (instr >> 8) & 0xF;
472 switch (op3) {
473 case 0:
474 if ((op2 & 0x38) != 0x38) {
475 // Conditional branch
476 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
477 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
478 // |---|--|-|----|------|-|-|--|-|--|-----------|
479 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
480 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
481 // |---|--|-|----|------|-|-|--|-|--|-----------|
482 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
483 uint32_t S = (instr >> 26) & 1;
484 uint32_t J2 = (instr >> 11) & 1;
485 uint32_t J1 = (instr >> 13) & 1;
486 uint32_t imm6 = (instr >> 16) & 0x3F;
487 uint32_t imm11 = instr & 0x7FF;
488 uint32_t cond = (instr >> 22) & 0xF;
489 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
490 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700491 opcode << "b";
492 DumpCond(opcode, cond);
493 opcode << ".w";
494 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800495 }
496 break;
497 case 2:
498 case 1: case 3:
499 break;
500 case 4: case 6: case 5: case 7: {
501 // BL, BLX (immediate)
502 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
503 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
504 // |---|--|-|----------|--|--|-|--|-----------|
505 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
506 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
507 // |---|--|-|----------|--|--|-|--|-----------|
508 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
509 uint32_t S = (instr >> 26) & 1;
510 uint32_t J2 = (instr >> 11) & 1;
511 uint32_t L = (instr >> 12) & 1;
512 uint32_t J1 = (instr >> 13) & 1;
513 uint32_t imm10 = (instr >> 16) & 0x3FF;
514 uint32_t imm11 = instr & 0x7FF;
515 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700516 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800517 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700518 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800519 }
520 uint32_t I1 = ~(J1 ^ S);
521 uint32_t I2 = ~(J2 ^ S);
522 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
523 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700524 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800525 break;
526 }
527 }
528 }
529 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 case 3:
531 switch (op2) {
532 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
533 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
534 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800535 // |111|11|100|000|0|0000|1111|110000|000000|
536 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
537 // |---|--|---|---|-|----|----|------|------|
538 // |332|22|222|222|2|1111|1111|110000|000000|
539 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
540 // |---|--|---|---|-|----|----|------|------|
541 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 uint32_t op3 = (instr >> 21) & 7;
543 //uint32_t op4 = (instr >> 6) & 0x3F;
544 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700545 case 0x0: case 0x4: {
546 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
547 // 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 -0700548 ArmRegister Rn(instr, 16);
549 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700550 opcode << "strb";
551 if ((instr & 0x800) != 0) {
552 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700553 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700554 } else {
555 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700556 ArmRegister Rm(instr, 0);
557 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700558 if (imm2 != 0) {
559 args << ", " << "lsl #" << imm2;
560 }
561 args << "]";
562 }
563 break;
564 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800565 case 0x2: case 0x6: {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800566 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
567 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700568 ArmRegister Rn(instr, 16);
569 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800570 if (op3 == 2) {
571 uint32_t P = (instr >> 10) & 1;
572 uint32_t U = (instr >> 9) & 1;
573 uint32_t W = (instr >> 8) & 1;
574 uint32_t imm8 = instr & 0xFF;
575 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
Elliott Hughes630e77d2012-03-22 19:20:56 -0700576 if (Rn.r == 13 && P == 1 && U == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700577 opcode << "push";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700578 args << Rt;
579 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700580 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800581 } else {
582 if (P == 1 && U == 1 && W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700583 opcode << "strt";
Ian Rogers40627db2012-03-04 17:31:09 -0800584 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700585 opcode << "str";
Ian Rogers40627db2012-03-04 17:31:09 -0800586 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700587 args << Rt << ", [" << Rn;
Ian Rogers40627db2012-03-04 17:31:09 -0800588 if (P == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700589 args << "], #" << imm32;
Ian Rogers40627db2012-03-04 17:31:09 -0800590 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700591 args << ", #" << imm32 << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800592 if (W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700593 args << "!";
Ian Rogers40627db2012-03-04 17:31:09 -0800594 }
595 }
Ian Rogers40627db2012-03-04 17:31:09 -0800596 }
597 } else if (op3 == 6) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700599 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700600 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800601 }
Ian Rogers40627db2012-03-04 17:31:09 -0800602 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800603 }
604 }
605
606 break;
607 }
608 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
609 // Load word
610 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
611 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
612 // |---|--|--|---|--|-|----|----|------|------|
613 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
614 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
615 // |---|--|--|---|--|-|----|----|------|------|
616 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
617 // |111|11| op2 | | | imm12 |
618 uint32_t op3 = (instr >> 23) & 3;
619 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700620 ArmRegister Rn(instr, 16);
621 ArmRegister Rt(instr, 12);
622 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800623 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
624 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
625 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700626 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700627 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800628 } else if (op4 == 0) {
629 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
630 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700631 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700632 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700633 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800634 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700635 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800636 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700637 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800638 } else {
639 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
640 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700641 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700642 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800643 }
644 break;
645 }
646 }
647 default:
648 break;
649 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700650 os << StringPrintf("\t\t\t%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800651 return 4;
652}
653
654size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
655 uint16_t instr = ReadU16(instr_ptr);
656 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
657 if (is_32bit) {
658 return DumpThumb32(os, instr_ptr);
659 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700660 std::ostringstream opcode;
661 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800662 uint16_t opcode1 = instr >> 10;
663 if (opcode1 < 0x10) {
664 // shift (immediate), add, subtract, move, and compare
665 uint16_t opcode2 = instr >> 9;
666 switch (opcode2) {
667 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
668 case 0x8: case 0x9: case 0xA: case 0xB: {
669 // Logical shift left - 00 000xx xxxxxxxxx
670 // Logical shift right - 00 001xx xxxxxxxxx
671 // Arithmetic shift right - 00 010xx xxxxxxxxx
672 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700673 ThumbRegister rm(instr, 3);
674 ThumbRegister Rd(instr, 7);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800675 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700676 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800677 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700678 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800679 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700680 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800681 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700682 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800683 break;
684 }
685 case 0xC: case 0xD: case 0xE: case 0xF: {
686 // Add register - 00 01100 mmm nnn ddd
687 // Sub register - 00 01101 mmm nnn ddd
688 // Add 3-bit immediate - 00 01110 iii nnn ddd
689 // Sub 3-bit immediate - 00 01111 iii nnn ddd
690 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700691 ThumbRegister Rn(instr, 3);
692 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800693 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700694 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800695 } else {
696 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700697 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800698 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700699 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800700 }
701 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700702 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800703 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700704 ArmRegister Rm(imm3_or_Rm);
705 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800706 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700707 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800708 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800709 break;
710 }
711 case 0x10: case 0x11: case 0x12: case 0x13:
712 case 0x14: case 0x15: case 0x16: case 0x17:
713 case 0x18: case 0x19: case 0x1A: case 0x1B:
714 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
715 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
716 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
717 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
718 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700719 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800720 uint16_t imm8 = instr & 0xFF;
721 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700722 case 4: opcode << "movs"; break;
723 case 5: opcode << "cmp"; break;
724 case 6: opcode << "adds"; break;
725 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800726 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700727 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800728 break;
729 }
730 default:
731 break;
732 }
Ian Rogersad03ef52012-03-18 19:34:47 -0700733 } else if (opcode1 == 0x10) {
734 // Data-processing
735 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700736 ThumbRegister rm(instr, 3);
737 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -0700738 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700739 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800740 } else if (opcode1 == 0x11) {
741 // Special data instructions and branch and exchange
742 uint16_t opcode2 = (instr >> 6) & 0x0F;
743 switch (opcode2) {
744 case 0x0: case 0x1: case 0x2: case 0x3: {
745 // Add low registers - 010001 0000 xxxxxx
746 // Add high registers - 010001 0001/001x xxxxxx
747 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700748 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800749 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700750 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700751 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700752 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800753 break;
754 }
755 case 0x8: case 0x9: case 0xA: case 0xB: {
756 // Move low registers - 010001 1000 xxxxxx
757 // Move high registers - 010001 1001/101x xxxxxx
758 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700759 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800760 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700761 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700762 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700763 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800764 break;
765 }
766 case 0x5: case 0x6: case 0x7: {
767 // Compare high registers - 010001 0101/011x xxxxxx
768 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700769 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800770 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700771 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700772 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700773 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800774 break;
775 }
776 case 0xC: case 0xD: case 0xE: case 0xF: {
777 // Branch and exchange - 010001 110x xxxxxx
778 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -0700779 ArmRegister rm(instr, 3);
780 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
781 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800782 break;
783 }
784 default:
785 break;
786 }
787 } else if ((instr & 0xF000) == 0xB000) {
788 // Miscellaneous 16-bit instructions
789 uint16_t opcode2 = (instr >> 5) & 0x7F;
790 switch (opcode2) {
791 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
792 // Add immediate to SP - 1011 00000 ii iiiii
793 // Subtract immediate from SP - 1011 00001 ii iiiii
794 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700795 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700796 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800797 break;
798 }
Ian Rogers087b2412012-03-21 01:30:32 -0700799 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
800 case 0x0C: case 0x0D: case 0x0E: case 0x0F: {
801 // CBNZ, CBZ
802 uint16_t op = (instr >> 11) & 1;
803 uint16_t i = (instr >> 9) & 1;
804 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700805 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -0700806 opcode << (op != 0 ? "cbnz" : "cbz");
807 uint32_t imm32 = (i << 7) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -0700808 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -0700809 DumpBranchTarget(args, instr_ptr + 4, imm32);
810 break;
811 }
Ian Rogers40627db2012-03-04 17:31:09 -0800812 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
813 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
814 // If-Then, and hints
815 uint16_t opA = (instr >> 4) & 0xF;
816 uint16_t opB = instr & 0xF;
817 if (opB == 0) {
818 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700819 case 0: opcode << "nop"; break;
820 case 1: opcode << "yield"; break;
821 case 2: opcode << "wfe"; break;
822 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800823 default: break;
824 }
825 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700826 opcode << "it";
827 args << reinterpret_cast<void*>(opB) << " ";
828 DumpCond(args, opA);
Ian Rogers40627db2012-03-04 17:31:09 -0800829 }
830 break;
831 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800832 default:
833 break;
834 }
835 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
836 ((instr & 0xE000) == 0x8000)) {
837 // Load/store single data item
838 uint16_t opA = instr >> 12;
839 //uint16_t opB = (instr >> 9) & 7;
840 switch (opA) {
841 case 0x6: {
842 // STR Rt, Rn, #imm - 01100 iiiii nnn ttt
843 // LDR Rt, Rn, #imm - 01101 iiiii nnn ttt
844 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700845 ThumbRegister Rn(instr, 3);
846 ThumbRegister Rt(instr, 7);
847 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
848 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800849 break;
850 }
851 case 0x9: {
852 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
853 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
854 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700855 ThumbRegister Rt(instr, 8);
856 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
857 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800858 break;
859 }
860 default:
861 break;
862 }
Ian Rogers40627db2012-03-04 17:31:09 -0800863 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
864 uint16_t imm11 = instr & 0x7FFF;
865 int32_t imm32 = imm11 << 1;
866 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700867 opcode << "b";
868 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800869 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700870 os << StringPrintf("\t\t\t%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800871 }
872 return 2;
873}
874
875} // namespace arm
876} // namespace art