blob: 5211146480821694b3f0e01f29acd94d86f13407 [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 Hughes0f3c5532012-03-30 14:51:51 -070021#include "logging.h"
22#include "stringprintf.h"
23
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080024namespace art {
25namespace arm {
26
27DisassemblerArm::DisassemblerArm() {
28}
29
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080030void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
31 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
32 for (const uint8_t* cur = begin; cur < end; cur += 4) {
33 DumpArm(os, cur);
34 }
35 } else {
36 // remove thumb specifier bits
37 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
38 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
39 for (const uint8_t* cur = begin; cur < end;) {
40 cur += DumpThumb16(os, cur);
41 }
42 }
43}
44
Elliott Hughes77405792012-03-15 15:22:12 -070045static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070046 "eq", // 0000 - equal
47 "ne", // 0001 - not-equal
48 "cs", // 0010 - carry-set, greater than, equal or unordered
49 "cc", // 0011 - carry-clear, less than
50 "mi", // 0100 - minus, negative
51 "pl", // 0101 - plus, positive or zero
52 "vs", // 0110 - overflow
53 "vc", // 0111 - no overflow
54 "hi", // 1000 - unsigned higher
55 "ls", // 1001 - unsigned lower or same
56 "ge", // 1010 - signed greater than or equal
57 "lt", // 1011 - signed less than
58 "gt", // 1100 - signed greater than
59 "le", // 1101 - signed less than or equal
60 "", // 1110 - always
61 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080062};
63
64void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
65 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070066 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080067 } else {
68 os << "Unexpected condition: " << cond;
69 }
70}
71
Ian Rogers40627db2012-03-04 17:31:09 -080072void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
73 os << imm32 << " (" << reinterpret_cast<const void*>(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080074}
75
76static uint32_t ReadU16(const uint8_t* ptr) {
77 return ptr[0] | (ptr[1] << 8);
78}
79
80static uint32_t ReadU32(const uint8_t* ptr) {
81 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
82}
83
Elliott Hughes77405792012-03-15 15:22:12 -070084static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070085 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
86 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -070087};
88
Ian Rogersad03ef52012-03-18 19:34:47 -070089static const char* kThumbDataProcessingOperations[] = {
90 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
91 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
92};
93
Elliott Hughes77405792012-03-15 15:22:12 -070094struct ArmRegister {
95 ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -070096 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -070097 uint32_t r;
98};
99std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
100 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700101 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700102 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700103 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700104 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700105 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700106 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700107 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700108 }
109 return os;
110}
111
Elliott Hughes630e77d2012-03-22 19:20:56 -0700112struct ThumbRegister : ArmRegister {
113 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700114};
115
116struct Rm {
117 Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
118 uint32_t shift;
119 ArmRegister rm;
120};
121std::ostream& operator<<(std::ostream& os, const Rm& r) {
122 os << r.rm;
123 if (r.shift != 0) {
124 os << "-shift-" << r.shift; // TODO
125 }
126 return os;
127}
128
129struct Imm12 {
130 Imm12(uint32_t instruction) : rotate((instruction >> 8) & 0xf), imm(instruction & 0xff) {}
131 uint32_t rotate;
132 uint32_t imm;
133};
134std::ostream& operator<<(std::ostream& os, const Imm12& rhs) {
135 uint32_t imm = (rhs.imm >> (2 * rhs.rotate)) | (rhs.imm << (32 - (2 * rhs.rotate)));
136 os << "#" << imm;
137 return os;
138}
139
140struct RegisterList {
141 RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
142 uint32_t register_list;
143};
144std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
145 if (rhs.register_list == 0) {
146 os << "<no register list?>";
147 return os;
148 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700149 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700150 bool first = true;
151 for (size_t i = 0; i < 16; i++) {
152 if ((rhs.register_list & (1 << i)) != 0) {
153 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700154 first = false;
155 } else {
156 os << ", ";
157 }
158 os << ArmRegister(i);
159 }
160 }
161 os << "}";
162 return os;
163}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800164
165void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700166 uint32_t instruction = ReadU32(instr_ptr);
167 uint32_t cond = (instruction >> 28) & 0xf;
168 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700169 std::ostringstream opcode;
170 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700171 switch (op1) {
172 case 0:
173 case 1: // Data processing instructions.
174 {
175 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughescbf0b612012-03-15 16:23:47 -0700176 opcode << (((instruction >> 5) & 1) ? "blx" : "bx");
177 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700178 break;
179 }
180 bool i = (instruction & (1 << 25)) != 0;
181 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700182 opcode << kDataProcessingOperations[(instruction >> 21) & 0xf]
183 << kConditionCodeNames[cond]
184 << (s ? "s" : "");
Elliott Hughes630e77d2012-03-22 19:20:56 -0700185 args << ArmRegister(instruction, 12) << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700186 if (i) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700187 args << ArmRegister(instruction, 16) << ", " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700188 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700189 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700190 }
191 }
192 break;
193 case 2: // Load/store word and unsigned byte.
194 {
195 bool p = (instruction & (1 << 24)) != 0;
196 bool b = (instruction & (1 << 22)) != 0;
197 bool w = (instruction & (1 << 21)) != 0;
198 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700199 opcode << (l ? "ldr" : "str") << (b ? "b" : "") << kConditionCodeNames[cond];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700200 args << ArmRegister(instruction, 12) << ", ";
201 ArmRegister rn(instruction, 16);
202 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700203 UNIMPLEMENTED(FATAL) << "literals";
204 } else {
205 bool wback = !p || w;
206 if (p && !wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700207 args << "[" << rn << ", " << Imm12(instruction) << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700208 } else if (p && wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700209 args << "[" << rn << ", " << Imm12(instruction) << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700210 } else if (!p && wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700211 args << "[" << rn << "], " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700212 } else {
213 LOG(FATAL) << p << " " << w;
214 }
215 }
216 }
217 break;
218 case 4: // Load/store multiple.
219 {
220 bool p = (instruction & (1 << 24)) != 0;
221 bool u = (instruction & (1 << 23)) != 0;
222 bool w = (instruction & (1 << 21)) != 0;
223 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700224 opcode << (l ? "ldm" : "stm")
225 << (u ? 'i' : 'd')
226 << (p ? 'b' : 'a')
227 << kConditionCodeNames[cond];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700228 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700229 }
230 break;
231 default:
Elliott Hughescbf0b612012-03-15 16:23:47 -0700232 opcode << "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700233 break;
234 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700235 // TODO: a more complete ARM disassembler could generate wider opcodes.
236 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 -0800237}
238
239size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
240 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
241 // |111|1 1|1000000|0000|1111110000000000|
242 // |5 3|2 1|0987654|3 0|5 0 5 0|
243 // |---|---|-------|----|----------------|
244 // |332|2 2|2222222|1111|1111110000000000|
245 // |1 9|8 7|6543210|9 6|5 0 5 0|
246 // |---|---|-------|----|----------------|
247 // |111|op1| op2 | | |
248 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700249 if (op1 == 0) {
250 return DumpThumb16(os, instr_ptr);
251 }
252
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800253 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700254 std::ostringstream opcode;
255 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800256 switch (op1) {
257 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800258 break;
259 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700260 if ((op2 & 0x64) == 0) { // 00x x0xx
261 // |111|11|10|00|0|00|0000|1111110000000000|
262 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
263 // |---|--|--|--|-|--|----|----------------|
264 // |332|22|22|22|2|22|1111|1111110000000000|
265 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
266 // |---|--|--|--|-|--|----|----------------|
267 // |111|01|00|op|0|WL| Rn | |
268 // |111|01| op2 | | |
269 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
270 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
271 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
272 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
273 uint32_t op = (instr >> 23) & 3;
274 uint32_t W = (instr >> 21) & 1;
275 uint32_t L = (instr >> 20) & 1;
276 ArmRegister Rn(instr, 16);
277 if (op == 1 || op == 2) {
278 if (op == 1) {
279 if (L == 0) {
280 opcode << "stm";
281 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800282 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700283 if (Rn.r != 13) {
284 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700285 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700286 } else {
287 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800288 }
289 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700290 } else {
291 if (L == 0) {
292 if (Rn.r != 13) {
293 opcode << "stmdb";
294 args << Rn << (W == 0 ? "" : "!") << ", ";
295 } else {
296 opcode << "push";
297 }
298 } else {
299 opcode << "ldmdb";
300 args << Rn << (W == 0 ? "" : "!") << ", ";
301 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800302 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700303 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800304 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700305 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
306 // Data-processing (shifted register)
307 // |111|1110|0000|0|0000|1111|1100|0000|0000|
308 // |5 3|2109|8765|4|3 0|5 |10 8|7 5 |3 0|
309 // |---|----|----|-|----|----|----|----|----|
310 // |332|2222|2222|2|1111|1111|1100|0000|0000|
311 // |1 9|8765|4321|0|9 6|5 |10 8|7 5 |3 0|
312 // |---|----|----|-|----|----|----|----|----|
313 // |111|0101| op3|S| Rn | | Rd | | Rm |
314 uint32_t op3 = (instr >> 21) & 0xF;
315 uint32_t S = (instr >> 20) & 1;
316 uint32_t Rn = (instr >> 16) & 0xF;
317 ArmRegister Rd(instr, 8);
318 ArmRegister Rm(instr, 0);
319 switch (op3) {
320 case 0x0:
321 if (Rn != 0xF) {
322 opcode << "and";
323 } else {
324 opcode << "tst";
325 S = 0; // don't print 's'
326 }
327 break;
328 case 0x1: opcode << "bic"; break;
329 case 0x2:
330 if (Rn != 0xF) {
331 opcode << "orr";
332 } else {
333 opcode << "mov";
334 }
335 break;
336 case 0x3:
337 if (Rn != 0xF) {
338 opcode << "orn";
339 } else {
340 opcode << "mvn";
341 }
342 break;
343 case 0x4:
344 if (Rn != 0xF) {
345 opcode << "eor";
346 } else {
347 opcode << "teq";
348 S = 0; // don't print 's'
349 }
350 break;
351 case 0x6: opcode << "pkh"; break;
352 case 0x8:
353 if (Rn != 0xF) {
354 opcode << "add";
355 } else {
356 opcode << "cmn";
357 S = 0; // don't print 's'
358 }
359 break;
360 case 0xA: opcode << "adc"; break;
361 case 0xB: opcode << "sbc"; break;
362 }
Ian Rogers087b2412012-03-21 01:30:32 -0700363
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700364 if (S == 1) {
365 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700366 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700367 opcode << ".w";
368 args << Rd << ", " << Rm;
369 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
370 // Co-processor instructions
371 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
372 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
373 // |---|-|--|------|----|----|----|---|---|----|
374 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
375 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
376 // |---|-|--|------|----|----|----|---|---|----|
377 // |111| |11| op3 | Rn | |copr| |op4| |
378 uint32_t op3 = (instr >> 20) & 0x3F;
379 uint32_t coproc = (instr >> 8) & 0xF;
380 uint32_t op4 = (instr >> 4) & 0x1;
381 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
382 if ((coproc & 0xE) == 0xA) {
383 // VFP data-processing instructions
384 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
385 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
386 // |---|-|----|----|----|----|---|-|----|-|-|----|
387 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
388 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
389 // |---|-|----|----|----|----|---|-|----|-|-|----|
390 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
391 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
392 uint32_t opc1 = (instr >> 20) & 0xF;
393 uint32_t opc2 = (instr >> 16) & 0xF;
394 //uint32_t opc3 = (instr >> 6) & 0x3;
395 if ((opc1 & 0xB) == 0xB) { // 1x11
396 // Other VFP data-processing instructions.
397 switch (opc2) {
398 case 0x4: case 0x5: { // Vector compare
399 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
400 uint32_t D = (instr >> 22) & 0x1;
401 uint32_t Vd = (instr >> 12) & 0xF;
402 uint32_t sz = (instr >> 8) & 1;
403 uint32_t E = (instr >> 7) & 1;
404 uint32_t M = (instr >> 5) & 1;
405 uint32_t Vm = instr & 0xF;
406 bool dp_operation = sz == 1;
407 opcode << (E == 0 ? "vcmp" : "vcmpe");
408 opcode << (dp_operation ? ".f64" : ".f32");
409 if (dp_operation) {
410 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
411 } else {
412 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
413 }
414 break;
415 }
416 }
417 }
418 }
419 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800420 }
421 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800422 case 2:
423 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
424 // Data-processing (modified immediate)
425 // |111|11|10|0000|0|0000|1|111|1100|00000000|
426 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
427 // |---|--|--|----|-|----|-|---|----|--------|
428 // |332|22|22|2222|2|1111|1|111|1100|00000000|
429 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
430 // |---|--|--|----|-|----|-|---|----|--------|
431 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
432 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800433 uint32_t i = (instr >> 26) & 1;
434 uint32_t op3 = (instr >> 21) & 0xF;
435 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700436 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800437 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700438 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800439 uint32_t imm8 = instr & 0xFF;
440 int32_t imm32 = (i << 12) | (imm3 << 8) | imm8;
441 switch (op3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700442 case 0x0: opcode << "and"; break;
443 case 0x1: opcode << "bic"; break;
444 case 0x2: opcode << "orr"; break;
445 case 0x3: opcode << "orn"; break;
446 case 0x4: opcode << "eor"; break;
447 case 0x8: opcode << "add"; break;
448 case 0xA: opcode << "adc"; break;
449 case 0xB: opcode << "sbc"; break;
450 case 0xD: opcode << "sub"; break;
451 case 0xE: opcode << "rsb"; break;
452 default: opcode << "UNKNOWN DPMI-" << op3; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800453 }
454 if (S == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700455 opcode << "s";
Ian Rogers40627db2012-03-04 17:31:09 -0800456 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700457 args << Rd << ", " << Rn << ", ThumbExpand(" << imm32 << ")";
Ian Rogers40627db2012-03-04 17:31:09 -0800458 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
459 // Data-processing (plain binary immediate)
460 // |111|11|10|00000|0000|1|111110000000000|
461 // |5 3|21|09|87654|3 0|5|4 0 5 0|
462 // |---|--|--|-----|----|-|---------------|
463 // |332|22|22|22222|1111|1|111110000000000|
464 // |1 9|87|65|43210|9 6|5|4 0 5 0|
465 // |---|--|--|-----|----|-|---------------|
466 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
467 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800468 switch (op3) {
469 case 0x04: {
470 // MOVW Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700471 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800472 uint32_t i = (instr >> 26) & 1;
473 uint32_t imm3 = (instr >> 12) & 0x7;
474 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700475 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800476 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700477 opcode << "movw";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700478 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800479 break;
480 }
481 case 0x0A: {
482 // SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700483 ArmRegister Rd(instr, 8);
484 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800485 uint32_t i = (instr >> 26) & 1;
486 uint32_t imm3 = (instr >> 12) & 0x7;
487 uint32_t imm8 = instr & 0xFF;
488 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700489 opcode << "sub.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700490 args << Rd << ", " << Rn << ", #" << imm12;
Ian Rogers40627db2012-03-04 17:31:09 -0800491 break;
492 }
493 default:
494 break;
495 }
496 } else {
497 // Branches and miscellaneous control
498 // |111|11|1000000|0000|1|111|1100|00000000|
499 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
500 // |---|--|-------|----|-|---|----|--------|
501 // |332|22|2222222|1111|1|111|1100|00000000|
502 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
503 // |---|--|-------|----|-|---|----|--------|
504 // |111|10| op2 | |1|op3|op4 | |
505
506 uint32_t op3 = (instr >> 12) & 7;
507 //uint32_t op4 = (instr >> 8) & 0xF;
508 switch (op3) {
509 case 0:
510 if ((op2 & 0x38) != 0x38) {
511 // Conditional branch
512 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
513 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
514 // |---|--|-|----|------|-|-|--|-|--|-----------|
515 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
516 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
517 // |---|--|-|----|------|-|-|--|-|--|-----------|
518 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
519 uint32_t S = (instr >> 26) & 1;
520 uint32_t J2 = (instr >> 11) & 1;
521 uint32_t J1 = (instr >> 13) & 1;
522 uint32_t imm6 = (instr >> 16) & 0x3F;
523 uint32_t imm11 = instr & 0x7FF;
524 uint32_t cond = (instr >> 22) & 0xF;
525 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
526 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700527 opcode << "b";
528 DumpCond(opcode, cond);
529 opcode << ".w";
530 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800531 }
532 break;
533 case 2:
534 case 1: case 3:
535 break;
536 case 4: case 6: case 5: case 7: {
537 // BL, BLX (immediate)
538 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
539 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
540 // |---|--|-|----------|--|--|-|--|-----------|
541 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
542 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
543 // |---|--|-|----------|--|--|-|--|-----------|
544 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
545 uint32_t S = (instr >> 26) & 1;
546 uint32_t J2 = (instr >> 11) & 1;
547 uint32_t L = (instr >> 12) & 1;
548 uint32_t J1 = (instr >> 13) & 1;
549 uint32_t imm10 = (instr >> 16) & 0x3FF;
550 uint32_t imm11 = instr & 0x7FF;
551 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700552 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800553 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700554 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800555 }
556 uint32_t I1 = ~(J1 ^ S);
557 uint32_t I2 = ~(J2 ^ S);
558 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
559 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700560 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800561 break;
562 }
563 }
564 }
565 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800566 case 3:
567 switch (op2) {
568 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
569 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
570 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800571 // |111|11|100|000|0|0000|1111|110000|000000|
572 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
573 // |---|--|---|---|-|----|----|------|------|
574 // |332|22|222|222|2|1111|1111|110000|000000|
575 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
576 // |---|--|---|---|-|----|----|------|------|
577 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800578 uint32_t op3 = (instr >> 21) & 7;
579 //uint32_t op4 = (instr >> 6) & 0x3F;
580 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700581 case 0x0: case 0x4: {
582 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
583 // 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 -0700584 ArmRegister Rn(instr, 16);
585 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700586 opcode << "strb";
587 if ((instr & 0x800) != 0) {
588 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700589 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700590 } else {
591 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700592 ArmRegister Rm(instr, 0);
593 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700594 if (imm2 != 0) {
595 args << ", " << "lsl #" << imm2;
596 }
597 args << "]";
598 }
599 break;
600 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800601 case 0x2: case 0x6: {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800602 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
603 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700604 ArmRegister Rn(instr, 16);
605 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800606 if (op3 == 2) {
607 uint32_t P = (instr >> 10) & 1;
608 uint32_t U = (instr >> 9) & 1;
609 uint32_t W = (instr >> 8) & 1;
610 uint32_t imm8 = instr & 0xFF;
611 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
Elliott Hughes630e77d2012-03-22 19:20:56 -0700612 if (Rn.r == 13 && P == 1 && U == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700613 opcode << "push";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700614 args << Rt;
615 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700616 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800617 } else {
618 if (P == 1 && U == 1 && W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700619 opcode << "strt";
Ian Rogers40627db2012-03-04 17:31:09 -0800620 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700621 opcode << "str";
Ian Rogers40627db2012-03-04 17:31:09 -0800622 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700623 args << Rt << ", [" << Rn;
Ian Rogers40627db2012-03-04 17:31:09 -0800624 if (P == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700625 args << "], #" << imm32;
Ian Rogers40627db2012-03-04 17:31:09 -0800626 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700627 args << ", #" << imm32 << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800628 if (W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700629 args << "!";
Ian Rogers40627db2012-03-04 17:31:09 -0800630 }
631 }
Ian Rogers40627db2012-03-04 17:31:09 -0800632 }
633 } else if (op3 == 6) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800634 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700635 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700636 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800637 }
Ian Rogers40627db2012-03-04 17:31:09 -0800638 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800639 }
640 }
641
642 break;
643 }
644 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
645 // Load word
646 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
647 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
648 // |---|--|--|---|--|-|----|----|------|------|
649 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
650 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
651 // |---|--|--|---|--|-|----|----|------|------|
652 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
653 // |111|11| op2 | | | imm12 |
654 uint32_t op3 = (instr >> 23) & 3;
655 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700656 ArmRegister Rn(instr, 16);
657 ArmRegister Rt(instr, 12);
658 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
660 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
661 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700662 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700663 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800664 } else if (op4 == 0) {
665 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
666 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700667 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700668 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700669 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800670 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700671 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800672 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700673 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800674 } else {
675 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
676 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700677 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700678 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800679 }
680 break;
681 }
682 }
683 default:
684 break;
685 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700686 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 -0800687 return 4;
688}
689
690size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
691 uint16_t instr = ReadU16(instr_ptr);
692 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
693 if (is_32bit) {
694 return DumpThumb32(os, instr_ptr);
695 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700696 std::ostringstream opcode;
697 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800698 uint16_t opcode1 = instr >> 10;
699 if (opcode1 < 0x10) {
700 // shift (immediate), add, subtract, move, and compare
701 uint16_t opcode2 = instr >> 9;
702 switch (opcode2) {
703 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
704 case 0x8: case 0x9: case 0xA: case 0xB: {
705 // Logical shift left - 00 000xx xxxxxxxxx
706 // Logical shift right - 00 001xx xxxxxxxxx
707 // Arithmetic shift right - 00 010xx xxxxxxxxx
708 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700709 ThumbRegister rm(instr, 3);
710 ThumbRegister Rd(instr, 7);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800711 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700712 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800713 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700714 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800715 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700716 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800717 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700718 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800719 break;
720 }
721 case 0xC: case 0xD: case 0xE: case 0xF: {
722 // Add register - 00 01100 mmm nnn ddd
723 // Sub register - 00 01101 mmm nnn ddd
724 // Add 3-bit immediate - 00 01110 iii nnn ddd
725 // Sub 3-bit immediate - 00 01111 iii nnn ddd
726 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700727 ThumbRegister Rn(instr, 3);
728 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800729 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700730 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800731 } else {
732 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700733 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800734 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700735 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800736 }
737 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700738 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800739 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700740 ArmRegister Rm(imm3_or_Rm);
741 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800742 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700743 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800744 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800745 break;
746 }
747 case 0x10: case 0x11: case 0x12: case 0x13:
748 case 0x14: case 0x15: case 0x16: case 0x17:
749 case 0x18: case 0x19: case 0x1A: case 0x1B:
750 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
751 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
752 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
753 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
754 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700755 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800756 uint16_t imm8 = instr & 0xFF;
757 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700758 case 4: opcode << "movs"; break;
759 case 5: opcode << "cmp"; break;
760 case 6: opcode << "adds"; break;
761 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800762 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700763 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800764 break;
765 }
766 default:
767 break;
768 }
Ian Rogersad03ef52012-03-18 19:34:47 -0700769 } else if (opcode1 == 0x10) {
770 // Data-processing
771 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700772 ThumbRegister rm(instr, 3);
773 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -0700774 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700775 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800776 } else if (opcode1 == 0x11) {
777 // Special data instructions and branch and exchange
778 uint16_t opcode2 = (instr >> 6) & 0x0F;
779 switch (opcode2) {
780 case 0x0: case 0x1: case 0x2: case 0x3: {
781 // Add low registers - 010001 0000 xxxxxx
782 // Add high registers - 010001 0001/001x xxxxxx
783 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700784 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800785 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700786 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700787 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700788 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800789 break;
790 }
791 case 0x8: case 0x9: case 0xA: case 0xB: {
792 // Move low registers - 010001 1000 xxxxxx
793 // Move high registers - 010001 1001/101x xxxxxx
794 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700795 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800796 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700797 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700798 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700799 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800800 break;
801 }
802 case 0x5: case 0x6: case 0x7: {
803 // Compare high registers - 010001 0101/011x xxxxxx
804 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700805 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800806 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700807 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700808 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700809 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800810 break;
811 }
812 case 0xC: case 0xD: case 0xE: case 0xF: {
813 // Branch and exchange - 010001 110x xxxxxx
814 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -0700815 ArmRegister rm(instr, 3);
816 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
817 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800818 break;
819 }
820 default:
821 break;
822 }
823 } else if ((instr & 0xF000) == 0xB000) {
824 // Miscellaneous 16-bit instructions
825 uint16_t opcode2 = (instr >> 5) & 0x7F;
826 switch (opcode2) {
827 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
828 // Add immediate to SP - 1011 00000 ii iiiii
829 // Subtract immediate from SP - 1011 00001 ii iiiii
830 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700831 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700832 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800833 break;
834 }
Ian Rogers087b2412012-03-21 01:30:32 -0700835 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
836 case 0x0C: case 0x0D: case 0x0E: case 0x0F: {
837 // CBNZ, CBZ
838 uint16_t op = (instr >> 11) & 1;
839 uint16_t i = (instr >> 9) & 1;
840 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700841 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -0700842 opcode << (op != 0 ? "cbnz" : "cbz");
843 uint32_t imm32 = (i << 7) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -0700844 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -0700845 DumpBranchTarget(args, instr_ptr + 4, imm32);
846 break;
847 }
Ian Rogers40627db2012-03-04 17:31:09 -0800848 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
849 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
850 // If-Then, and hints
851 uint16_t opA = (instr >> 4) & 0xF;
852 uint16_t opB = instr & 0xF;
853 if (opB == 0) {
854 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700855 case 0: opcode << "nop"; break;
856 case 1: opcode << "yield"; break;
857 case 2: opcode << "wfe"; break;
858 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800859 default: break;
860 }
861 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700862 opcode << "it";
863 args << reinterpret_cast<void*>(opB) << " ";
864 DumpCond(args, opA);
Ian Rogers40627db2012-03-04 17:31:09 -0800865 }
866 break;
867 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800868 default:
869 break;
870 }
871 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
872 ((instr & 0xE000) == 0x8000)) {
873 // Load/store single data item
874 uint16_t opA = instr >> 12;
875 //uint16_t opB = (instr >> 9) & 7;
876 switch (opA) {
877 case 0x6: {
878 // STR Rt, Rn, #imm - 01100 iiiii nnn ttt
879 // LDR Rt, Rn, #imm - 01101 iiiii nnn ttt
880 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700881 ThumbRegister Rn(instr, 3);
882 ThumbRegister Rt(instr, 7);
883 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
884 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885 break;
886 }
887 case 0x9: {
888 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
889 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
890 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700891 ThumbRegister Rt(instr, 8);
892 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
893 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800894 break;
895 }
896 default:
897 break;
898 }
Ian Rogers40627db2012-03-04 17:31:09 -0800899 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
900 uint16_t imm11 = instr & 0x7FFF;
901 int32_t imm32 = imm11 << 1;
902 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700903 opcode << "b";
904 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800905 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700906 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 -0800907 }
908 return 2;
909}
910
911} // namespace arm
912} // namespace art