blob: aa7a39b957d7ec3942df902a01c7e09de6fedbef [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:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700259 if ((op2 & 0x64) == 0) { // 00x x0xx
260 // |111|11|10|00|0|00|0000|1111110000000000|
261 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
262 // |---|--|--|--|-|--|----|----------------|
263 // |332|22|22|22|2|22|1111|1111110000000000|
264 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
265 // |---|--|--|--|-|--|----|----------------|
266 // |111|01|00|op|0|WL| Rn | |
267 // |111|01| op2 | | |
268 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
269 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
270 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
271 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
272 uint32_t op = (instr >> 23) & 3;
273 uint32_t W = (instr >> 21) & 1;
274 uint32_t L = (instr >> 20) & 1;
275 ArmRegister Rn(instr, 16);
276 if (op == 1 || op == 2) {
277 if (op == 1) {
278 if (L == 0) {
279 opcode << "stm";
280 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800281 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700282 if (Rn.r != 13) {
283 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700284 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700285 } else {
286 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800287 }
288 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700289 } else {
290 if (L == 0) {
291 if (Rn.r != 13) {
292 opcode << "stmdb";
293 args << Rn << (W == 0 ? "" : "!") << ", ";
294 } else {
295 opcode << "push";
296 }
297 } else {
298 opcode << "ldmdb";
299 args << Rn << (W == 0 ? "" : "!") << ", ";
300 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800301 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700302 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800303 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700304 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
305 // Data-processing (shifted register)
306 // |111|1110|0000|0|0000|1111|1100|0000|0000|
307 // |5 3|2109|8765|4|3 0|5 |10 8|7 5 |3 0|
308 // |---|----|----|-|----|----|----|----|----|
309 // |332|2222|2222|2|1111|1111|1100|0000|0000|
310 // |1 9|8765|4321|0|9 6|5 |10 8|7 5 |3 0|
311 // |---|----|----|-|----|----|----|----|----|
312 // |111|0101| op3|S| Rn | | Rd | | Rm |
313 uint32_t op3 = (instr >> 21) & 0xF;
314 uint32_t S = (instr >> 20) & 1;
315 uint32_t Rn = (instr >> 16) & 0xF;
316 ArmRegister Rd(instr, 8);
317 ArmRegister Rm(instr, 0);
318 switch (op3) {
319 case 0x0:
320 if (Rn != 0xF) {
321 opcode << "and";
322 } else {
323 opcode << "tst";
324 S = 0; // don't print 's'
325 }
326 break;
327 case 0x1: opcode << "bic"; break;
328 case 0x2:
329 if (Rn != 0xF) {
330 opcode << "orr";
331 } else {
332 opcode << "mov";
333 }
334 break;
335 case 0x3:
336 if (Rn != 0xF) {
337 opcode << "orn";
338 } else {
339 opcode << "mvn";
340 }
341 break;
342 case 0x4:
343 if (Rn != 0xF) {
344 opcode << "eor";
345 } else {
346 opcode << "teq";
347 S = 0; // don't print 's'
348 }
349 break;
350 case 0x6: opcode << "pkh"; break;
351 case 0x8:
352 if (Rn != 0xF) {
353 opcode << "add";
354 } else {
355 opcode << "cmn";
356 S = 0; // don't print 's'
357 }
358 break;
359 case 0xA: opcode << "adc"; break;
360 case 0xB: opcode << "sbc"; break;
361 }
Ian Rogers087b2412012-03-21 01:30:32 -0700362
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700363 if (S == 1) {
364 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700365 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700366 opcode << ".w";
367 args << Rd << ", " << Rm;
368 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
369 // Co-processor instructions
370 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
371 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
372 // |---|-|--|------|----|----|----|---|---|----|
373 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
374 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
375 // |---|-|--|------|----|----|----|---|---|----|
376 // |111| |11| op3 | Rn | |copr| |op4| |
377 uint32_t op3 = (instr >> 20) & 0x3F;
378 uint32_t coproc = (instr >> 8) & 0xF;
379 uint32_t op4 = (instr >> 4) & 0x1;
380 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
381 if ((coproc & 0xE) == 0xA) {
382 // VFP data-processing instructions
383 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
384 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
385 // |---|-|----|----|----|----|---|-|----|-|-|----|
386 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
387 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
388 // |---|-|----|----|----|----|---|-|----|-|-|----|
389 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
390 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
391 uint32_t opc1 = (instr >> 20) & 0xF;
392 uint32_t opc2 = (instr >> 16) & 0xF;
393 //uint32_t opc3 = (instr >> 6) & 0x3;
394 if ((opc1 & 0xB) == 0xB) { // 1x11
395 // Other VFP data-processing instructions.
396 switch (opc2) {
397 case 0x4: case 0x5: { // Vector compare
398 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
399 uint32_t D = (instr >> 22) & 0x1;
400 uint32_t Vd = (instr >> 12) & 0xF;
401 uint32_t sz = (instr >> 8) & 1;
402 uint32_t E = (instr >> 7) & 1;
403 uint32_t M = (instr >> 5) & 1;
404 uint32_t Vm = instr & 0xF;
405 bool dp_operation = sz == 1;
406 opcode << (E == 0 ? "vcmp" : "vcmpe");
407 opcode << (dp_operation ? ".f64" : ".f32");
408 if (dp_operation) {
409 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
410 } else {
411 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
412 }
413 break;
414 }
415 }
416 }
417 }
418 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800419 }
420 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800421 case 2:
422 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
423 // Data-processing (modified immediate)
424 // |111|11|10|0000|0|0000|1|111|1100|00000000|
425 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
426 // |---|--|--|----|-|----|-|---|----|--------|
427 // |332|22|22|2222|2|1111|1|111|1100|00000000|
428 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
429 // |---|--|--|----|-|----|-|---|----|--------|
430 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
431 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800432 uint32_t i = (instr >> 26) & 1;
433 uint32_t op3 = (instr >> 21) & 0xF;
434 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700435 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800436 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700437 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800438 uint32_t imm8 = instr & 0xFF;
439 int32_t imm32 = (i << 12) | (imm3 << 8) | imm8;
440 switch (op3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700441 case 0x0: opcode << "and"; break;
442 case 0x1: opcode << "bic"; break;
443 case 0x2: opcode << "orr"; break;
444 case 0x3: opcode << "orn"; break;
445 case 0x4: opcode << "eor"; break;
446 case 0x8: opcode << "add"; break;
447 case 0xA: opcode << "adc"; break;
448 case 0xB: opcode << "sbc"; break;
449 case 0xD: opcode << "sub"; break;
450 case 0xE: opcode << "rsb"; break;
451 default: opcode << "UNKNOWN DPMI-" << op3; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800452 }
453 if (S == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700454 opcode << "s";
Ian Rogers40627db2012-03-04 17:31:09 -0800455 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700456 args << Rd << ", " << Rn << ", ThumbExpand(" << imm32 << ")";
Ian Rogers40627db2012-03-04 17:31:09 -0800457 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
458 // Data-processing (plain binary immediate)
459 // |111|11|10|00000|0000|1|111110000000000|
460 // |5 3|21|09|87654|3 0|5|4 0 5 0|
461 // |---|--|--|-----|----|-|---------------|
462 // |332|22|22|22222|1111|1|111110000000000|
463 // |1 9|87|65|43210|9 6|5|4 0 5 0|
464 // |---|--|--|-----|----|-|---------------|
465 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
466 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800467 switch (op3) {
468 case 0x04: {
469 // MOVW Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700470 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800471 uint32_t i = (instr >> 26) & 1;
472 uint32_t imm3 = (instr >> 12) & 0x7;
473 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700474 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800475 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700476 opcode << "movw";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700477 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800478 break;
479 }
480 case 0x0A: {
481 // SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700482 ArmRegister Rd(instr, 8);
483 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800484 uint32_t i = (instr >> 26) & 1;
485 uint32_t imm3 = (instr >> 12) & 0x7;
486 uint32_t imm8 = instr & 0xFF;
487 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700488 opcode << "sub.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700489 args << Rd << ", " << Rn << ", #" << imm12;
Ian Rogers40627db2012-03-04 17:31:09 -0800490 break;
491 }
492 default:
493 break;
494 }
495 } else {
496 // Branches and miscellaneous control
497 // |111|11|1000000|0000|1|111|1100|00000000|
498 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
499 // |---|--|-------|----|-|---|----|--------|
500 // |332|22|2222222|1111|1|111|1100|00000000|
501 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
502 // |---|--|-------|----|-|---|----|--------|
503 // |111|10| op2 | |1|op3|op4 | |
504
505 uint32_t op3 = (instr >> 12) & 7;
506 //uint32_t op4 = (instr >> 8) & 0xF;
507 switch (op3) {
508 case 0:
509 if ((op2 & 0x38) != 0x38) {
510 // Conditional branch
511 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
512 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
513 // |---|--|-|----|------|-|-|--|-|--|-----------|
514 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
515 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
516 // |---|--|-|----|------|-|-|--|-|--|-----------|
517 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
518 uint32_t S = (instr >> 26) & 1;
519 uint32_t J2 = (instr >> 11) & 1;
520 uint32_t J1 = (instr >> 13) & 1;
521 uint32_t imm6 = (instr >> 16) & 0x3F;
522 uint32_t imm11 = instr & 0x7FF;
523 uint32_t cond = (instr >> 22) & 0xF;
524 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
525 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700526 opcode << "b";
527 DumpCond(opcode, cond);
528 opcode << ".w";
529 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800530 }
531 break;
532 case 2:
533 case 1: case 3:
534 break;
535 case 4: case 6: case 5: case 7: {
536 // BL, BLX (immediate)
537 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
538 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
539 // |---|--|-|----------|--|--|-|--|-----------|
540 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
541 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
542 // |---|--|-|----------|--|--|-|--|-----------|
543 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
544 uint32_t S = (instr >> 26) & 1;
545 uint32_t J2 = (instr >> 11) & 1;
546 uint32_t L = (instr >> 12) & 1;
547 uint32_t J1 = (instr >> 13) & 1;
548 uint32_t imm10 = (instr >> 16) & 0x3FF;
549 uint32_t imm11 = instr & 0x7FF;
550 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700551 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800552 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700553 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800554 }
555 uint32_t I1 = ~(J1 ^ S);
556 uint32_t I2 = ~(J2 ^ S);
557 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
558 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700559 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800560 break;
561 }
562 }
563 }
564 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800565 case 3:
566 switch (op2) {
567 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
568 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
569 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800570 // |111|11|100|000|0|0000|1111|110000|000000|
571 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
572 // |---|--|---|---|-|----|----|------|------|
573 // |332|22|222|222|2|1111|1111|110000|000000|
574 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
575 // |---|--|---|---|-|----|----|------|------|
576 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 uint32_t op3 = (instr >> 21) & 7;
578 //uint32_t op4 = (instr >> 6) & 0x3F;
579 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700580 case 0x0: case 0x4: {
581 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
582 // 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 -0700583 ArmRegister Rn(instr, 16);
584 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700585 opcode << "strb";
586 if ((instr & 0x800) != 0) {
587 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700588 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700589 } else {
590 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700591 ArmRegister Rm(instr, 0);
592 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700593 if (imm2 != 0) {
594 args << ", " << "lsl #" << imm2;
595 }
596 args << "]";
597 }
598 break;
599 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800600 case 0x2: case 0x6: {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800601 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
602 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700603 ArmRegister Rn(instr, 16);
604 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800605 if (op3 == 2) {
606 uint32_t P = (instr >> 10) & 1;
607 uint32_t U = (instr >> 9) & 1;
608 uint32_t W = (instr >> 8) & 1;
609 uint32_t imm8 = instr & 0xFF;
610 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
Elliott Hughes630e77d2012-03-22 19:20:56 -0700611 if (Rn.r == 13 && P == 1 && U == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700612 opcode << "push";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700613 args << Rt;
614 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700615 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800616 } else {
617 if (P == 1 && U == 1 && W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700618 opcode << "strt";
Ian Rogers40627db2012-03-04 17:31:09 -0800619 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700620 opcode << "str";
Ian Rogers40627db2012-03-04 17:31:09 -0800621 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700622 args << Rt << ", [" << Rn;
Ian Rogers40627db2012-03-04 17:31:09 -0800623 if (P == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700624 args << "], #" << imm32;
Ian Rogers40627db2012-03-04 17:31:09 -0800625 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700626 args << ", #" << imm32 << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800627 if (W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700628 args << "!";
Ian Rogers40627db2012-03-04 17:31:09 -0800629 }
630 }
Ian Rogers40627db2012-03-04 17:31:09 -0800631 }
632 } else if (op3 == 6) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800633 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700634 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700635 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800636 }
Ian Rogers40627db2012-03-04 17:31:09 -0800637 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800638 }
639 }
640
641 break;
642 }
643 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
644 // Load word
645 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
646 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
647 // |---|--|--|---|--|-|----|----|------|------|
648 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
649 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
650 // |---|--|--|---|--|-|----|----|------|------|
651 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
652 // |111|11| op2 | | | imm12 |
653 uint32_t op3 = (instr >> 23) & 3;
654 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700655 ArmRegister Rn(instr, 16);
656 ArmRegister Rt(instr, 12);
657 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800658 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
659 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
660 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700661 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700662 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800663 } else if (op4 == 0) {
664 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
665 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700666 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700667 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700668 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800669 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700670 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800671 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700672 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800673 } else {
674 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
675 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700676 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700677 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800678 }
679 break;
680 }
681 }
682 default:
683 break;
684 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700685 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 -0800686 return 4;
687}
688
689size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
690 uint16_t instr = ReadU16(instr_ptr);
691 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
692 if (is_32bit) {
693 return DumpThumb32(os, instr_ptr);
694 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700695 std::ostringstream opcode;
696 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800697 uint16_t opcode1 = instr >> 10;
698 if (opcode1 < 0x10) {
699 // shift (immediate), add, subtract, move, and compare
700 uint16_t opcode2 = instr >> 9;
701 switch (opcode2) {
702 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
703 case 0x8: case 0x9: case 0xA: case 0xB: {
704 // Logical shift left - 00 000xx xxxxxxxxx
705 // Logical shift right - 00 001xx xxxxxxxxx
706 // Arithmetic shift right - 00 010xx xxxxxxxxx
707 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700708 ThumbRegister rm(instr, 3);
709 ThumbRegister Rd(instr, 7);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800710 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700711 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800712 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700713 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800714 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700715 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800716 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700717 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800718 break;
719 }
720 case 0xC: case 0xD: case 0xE: case 0xF: {
721 // Add register - 00 01100 mmm nnn ddd
722 // Sub register - 00 01101 mmm nnn ddd
723 // Add 3-bit immediate - 00 01110 iii nnn ddd
724 // Sub 3-bit immediate - 00 01111 iii nnn ddd
725 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700726 ThumbRegister Rn(instr, 3);
727 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800728 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700729 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800730 } else {
731 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700732 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800733 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700734 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800735 }
736 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700737 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800738 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700739 ArmRegister Rm(imm3_or_Rm);
740 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800741 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700742 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800743 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800744 break;
745 }
746 case 0x10: case 0x11: case 0x12: case 0x13:
747 case 0x14: case 0x15: case 0x16: case 0x17:
748 case 0x18: case 0x19: case 0x1A: case 0x1B:
749 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
750 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
751 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
752 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
753 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700754 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800755 uint16_t imm8 = instr & 0xFF;
756 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700757 case 4: opcode << "movs"; break;
758 case 5: opcode << "cmp"; break;
759 case 6: opcode << "adds"; break;
760 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800761 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700762 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800763 break;
764 }
765 default:
766 break;
767 }
Ian Rogersad03ef52012-03-18 19:34:47 -0700768 } else if (opcode1 == 0x10) {
769 // Data-processing
770 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700771 ThumbRegister rm(instr, 3);
772 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -0700773 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700774 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800775 } else if (opcode1 == 0x11) {
776 // Special data instructions and branch and exchange
777 uint16_t opcode2 = (instr >> 6) & 0x0F;
778 switch (opcode2) {
779 case 0x0: case 0x1: case 0x2: case 0x3: {
780 // Add low registers - 010001 0000 xxxxxx
781 // Add high registers - 010001 0001/001x xxxxxx
782 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700783 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800784 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700785 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700786 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700787 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800788 break;
789 }
790 case 0x8: case 0x9: case 0xA: case 0xB: {
791 // Move low registers - 010001 1000 xxxxxx
792 // Move high registers - 010001 1001/101x xxxxxx
793 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700794 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800795 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700796 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700797 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700798 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800799 break;
800 }
801 case 0x5: case 0x6: case 0x7: {
802 // Compare high registers - 010001 0101/011x xxxxxx
803 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700804 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800805 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700806 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700807 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700808 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800809 break;
810 }
811 case 0xC: case 0xD: case 0xE: case 0xF: {
812 // Branch and exchange - 010001 110x xxxxxx
813 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -0700814 ArmRegister rm(instr, 3);
815 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
816 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800817 break;
818 }
819 default:
820 break;
821 }
822 } else if ((instr & 0xF000) == 0xB000) {
823 // Miscellaneous 16-bit instructions
824 uint16_t opcode2 = (instr >> 5) & 0x7F;
825 switch (opcode2) {
826 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
827 // Add immediate to SP - 1011 00000 ii iiiii
828 // Subtract immediate from SP - 1011 00001 ii iiiii
829 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700830 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700831 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800832 break;
833 }
Ian Rogers087b2412012-03-21 01:30:32 -0700834 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
835 case 0x0C: case 0x0D: case 0x0E: case 0x0F: {
836 // CBNZ, CBZ
837 uint16_t op = (instr >> 11) & 1;
838 uint16_t i = (instr >> 9) & 1;
839 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700840 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -0700841 opcode << (op != 0 ? "cbnz" : "cbz");
842 uint32_t imm32 = (i << 7) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -0700843 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -0700844 DumpBranchTarget(args, instr_ptr + 4, imm32);
845 break;
846 }
Ian Rogers40627db2012-03-04 17:31:09 -0800847 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
848 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
849 // If-Then, and hints
850 uint16_t opA = (instr >> 4) & 0xF;
851 uint16_t opB = instr & 0xF;
852 if (opB == 0) {
853 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700854 case 0: opcode << "nop"; break;
855 case 1: opcode << "yield"; break;
856 case 2: opcode << "wfe"; break;
857 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800858 default: break;
859 }
860 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700861 opcode << "it";
862 args << reinterpret_cast<void*>(opB) << " ";
863 DumpCond(args, opA);
Ian Rogers40627db2012-03-04 17:31:09 -0800864 }
865 break;
866 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800867 default:
868 break;
869 }
870 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
871 ((instr & 0xE000) == 0x8000)) {
872 // Load/store single data item
873 uint16_t opA = instr >> 12;
874 //uint16_t opB = (instr >> 9) & 7;
875 switch (opA) {
876 case 0x6: {
877 // STR Rt, Rn, #imm - 01100 iiiii nnn ttt
878 // LDR Rt, Rn, #imm - 01101 iiiii nnn ttt
879 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700880 ThumbRegister Rn(instr, 3);
881 ThumbRegister Rt(instr, 7);
882 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
883 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800884 break;
885 }
886 case 0x9: {
887 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
888 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
889 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700890 ThumbRegister Rt(instr, 8);
891 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
892 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800893 break;
894 }
895 default:
896 break;
897 }
Ian Rogers40627db2012-03-04 17:31:09 -0800898 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
899 uint16_t imm11 = instr & 0x7FFF;
900 int32_t imm32 = imm11 << 1;
901 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700902 opcode << "b";
903 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800904 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700905 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 -0800906 }
907 return 2;
908}
909
910} // namespace arm
911} // namespace art