blob: 4726c6326d04825e2398e73bebed547b4bde53c2 [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"
Elliott Hughes28fa76d2012-04-09 17:31:46 -070023#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080025namespace art {
26namespace arm {
27
28DisassemblerArm::DisassemblerArm() {
29}
30
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080031void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
32 if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) {
33 for (const uint8_t* cur = begin; cur < end; cur += 4) {
34 DumpArm(os, cur);
35 }
36 } else {
37 // remove thumb specifier bits
38 begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1);
39 end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1);
40 for (const uint8_t* cur = begin; cur < end;) {
41 cur += DumpThumb16(os, cur);
42 }
43 }
44}
45
Elliott Hughes77405792012-03-15 15:22:12 -070046static const char* kConditionCodeNames[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070047 "eq", // 0000 - equal
48 "ne", // 0001 - not-equal
49 "cs", // 0010 - carry-set, greater than, equal or unordered
50 "cc", // 0011 - carry-clear, less than
51 "mi", // 0100 - minus, negative
52 "pl", // 0101 - plus, positive or zero
53 "vs", // 0110 - overflow
54 "vc", // 0111 - no overflow
55 "hi", // 1000 - unsigned higher
56 "ls", // 1001 - unsigned lower or same
57 "ge", // 1010 - signed greater than or equal
58 "lt", // 1011 - signed less than
59 "gt", // 1100 - signed greater than
60 "le", // 1101 - signed less than or equal
61 "", // 1110 - always
62 "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating)
Ian Rogers40627db2012-03-04 17:31:09 -080063};
64
65void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) {
66 if (cond < 15) {
Elliott Hughes77405792012-03-15 15:22:12 -070067 os << kConditionCodeNames[cond];
Ian Rogers40627db2012-03-04 17:31:09 -080068 } else {
69 os << "Unexpected condition: " << cond;
70 }
71}
72
Ian Rogers40627db2012-03-04 17:31:09 -080073void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
74 os << imm32 << " (" << reinterpret_cast<const void*>(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080075}
76
77static uint32_t ReadU16(const uint8_t* ptr) {
78 return ptr[0] | (ptr[1] << 8);
79}
80
81static uint32_t ReadU32(const uint8_t* ptr) {
82 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
83}
84
Elliott Hughes77405792012-03-15 15:22:12 -070085static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070086 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
87 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -070088};
89
Ian Rogersad03ef52012-03-18 19:34:47 -070090static const char* kThumbDataProcessingOperations[] = {
91 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
92 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
93};
94
Elliott Hughes77405792012-03-15 15:22:12 -070095struct ArmRegister {
96 ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
Elliott Hughes630e77d2012-03-22 19:20:56 -070097 ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); }
Elliott Hughes77405792012-03-15 15:22:12 -070098 uint32_t r;
99};
100std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
101 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700102 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700103 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700104 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700105 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700106 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700107 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700108 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700109 }
110 return os;
111}
112
Elliott Hughes630e77d2012-03-22 19:20:56 -0700113struct ThumbRegister : ArmRegister {
114 ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700115};
116
117struct Rm {
118 Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
119 uint32_t shift;
120 ArmRegister rm;
121};
122std::ostream& operator<<(std::ostream& os, const Rm& r) {
123 os << r.rm;
124 if (r.shift != 0) {
125 os << "-shift-" << r.shift; // TODO
126 }
127 return os;
128}
129
130struct Imm12 {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700131 Imm12(uint32_t instruction) {
132 uint32_t rotate = ((instruction >> 8) & 0xf);
133 uint32_t imm = (instruction & 0xff);
134 value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate)));
135 }
136 uint32_t value;
Elliott Hughes77405792012-03-15 15:22:12 -0700137};
138std::ostream& operator<<(std::ostream& os, const Imm12& rhs) {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700139 os << "#" << rhs.value;
Elliott Hughes77405792012-03-15 15:22:12 -0700140 return os;
141}
142
143struct RegisterList {
144 RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
145 uint32_t register_list;
146};
147std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
148 if (rhs.register_list == 0) {
149 os << "<no register list?>";
150 return os;
151 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700152 os << "{";
Elliott Hughes77405792012-03-15 15:22:12 -0700153 bool first = true;
154 for (size_t i = 0; i < 16; i++) {
155 if ((rhs.register_list & (1 << i)) != 0) {
156 if (first) {
Elliott Hughes77405792012-03-15 15:22:12 -0700157 first = false;
158 } else {
159 os << ", ";
160 }
161 os << ArmRegister(i);
162 }
163 }
164 os << "}";
165 return os;
166}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800167
168void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700169 uint32_t instruction = ReadU32(instr_ptr);
170 uint32_t cond = (instruction >> 28) & 0xf;
171 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700172 std::string opcode;
173 std::string suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700174 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700175 switch (op1) {
176 case 0:
177 case 1: // Data processing instructions.
178 {
Elliott Hughes3d71d072012-04-10 18:28:35 -0700179 if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT
180 opcode = "bkpt";
181 uint32_t imm12 = (instruction >> 8) & 0xfff;
182 uint32_t imm4 = (instruction & 0xf);
183 args << '#' << ((imm12 << 4) | imm4);
184 break;
185 }
Elliott Hughes77405792012-03-15 15:22:12 -0700186 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughes3d71d072012-04-10 18:28:35 -0700187 opcode = (((instruction >> 5) & 1) ? "blx" : "bx");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700188 args << ArmRegister(instruction & 0xf);
Elliott Hughes77405792012-03-15 15:22:12 -0700189 break;
190 }
191 bool i = (instruction & (1 << 25)) != 0;
192 bool s = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700193 uint32_t op = (instruction >> 21) & 0xf;
194 opcode = kDataProcessingOperations[op];
195 bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN.
196 if (implicit_s) {
197 // Rd is unused (and not shown), and we don't show the 's' suffix either.
198 } else {
199 if (s) {
200 suffixes += 's';
201 }
202 args << ArmRegister(instruction, 12) << ", ";
203 }
Elliott Hughes77405792012-03-15 15:22:12 -0700204 if (i) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700205 args << ArmRegister(instruction, 16) << ", " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700206 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700207 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700208 }
209 }
210 break;
211 case 2: // Load/store word and unsigned byte.
212 {
213 bool p = (instruction & (1 << 24)) != 0;
214 bool b = (instruction & (1 << 22)) != 0;
215 bool w = (instruction & (1 << 21)) != 0;
216 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700217 opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : ""));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700218 args << ArmRegister(instruction, 12) << ", ";
219 ArmRegister rn(instruction, 16);
220 if (rn.r == 0xf) {
Elliott Hughes77405792012-03-15 15:22:12 -0700221 UNIMPLEMENTED(FATAL) << "literals";
222 } else {
223 bool wback = !p || w;
224 if (p && !wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700225 args << "[" << rn << ", " << Imm12(instruction) << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700226 } else if (p && wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700227 args << "[" << rn << ", " << Imm12(instruction) << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700228 } else if (!p && wback) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700229 args << "[" << rn << "], " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700230 } else {
231 LOG(FATAL) << p << " " << w;
232 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700233 if (rn.r == 9) {
234 args << " ; ";
235 Thread::DumpThreadOffset(args, Imm12(instruction).value, 4);
236 }
Elliott Hughes77405792012-03-15 15:22:12 -0700237 }
238 }
239 break;
240 case 4: // Load/store multiple.
241 {
242 bool p = (instruction & (1 << 24)) != 0;
243 bool u = (instruction & (1 << 23)) != 0;
244 bool w = (instruction & (1 << 21)) != 0;
245 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700246 opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a'));
Elliott Hughes630e77d2012-03-22 19:20:56 -0700247 args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700248 }
249 break;
Elliott Hughes3d71d072012-04-10 18:28:35 -0700250 case 5: // Branch/branch with link.
251 {
252 bool bl = (instruction & (1 << 24)) != 0;
253 opcode = (bl ? "bl" : "b");
Elliott Hughesd86261e2012-04-11 11:23:23 -0700254 int32_t imm26 = (instruction & 0xffffff) << 2;
255 int32_t imm32 = (imm26 << 6) >> 6; // Sign extend.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700256 DumpBranchTarget(args, instr_ptr + 8, imm32);
257 }
258 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700259 default:
Elliott Hughes3d71d072012-04-10 18:28:35 -0700260 opcode = "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700261 break;
262 }
Elliott Hughes3d71d072012-04-10 18:28:35 -0700263 opcode += kConditionCodeNames[cond];
264 opcode += suffixes;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700265 // TODO: a more complete ARM disassembler could generate wider opcodes.
Elliott Hughes3d71d072012-04-10 18:28:35 -0700266 os << StringPrintf("\t\t\t%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800267}
268
269size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
270 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
271 // |111|1 1|1000000|0000|1111110000000000|
272 // |5 3|2 1|0987654|3 0|5 0 5 0|
273 // |---|---|-------|----|----------------|
274 // |332|2 2|2222222|1111|1111110000000000|
275 // |1 9|8 7|6543210|9 6|5 0 5 0|
276 // |---|---|-------|----|----------------|
277 // |111|op1| op2 | | |
278 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700279 if (op1 == 0) {
280 return DumpThumb16(os, instr_ptr);
281 }
282
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800283 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700284 std::ostringstream opcode;
285 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800286 switch (op1) {
287 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800288 break;
289 case 1:
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700290 if ((op2 & 0x64) == 0) { // 00x x0xx
291 // |111|11|10|00|0|00|0000|1111110000000000|
292 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
293 // |---|--|--|--|-|--|----|----------------|
294 // |332|22|22|22|2|22|1111|1111110000000000|
295 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
296 // |---|--|--|--|-|--|----|----------------|
297 // |111|01|00|op|0|WL| Rn | |
298 // |111|01| op2 | | |
299 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
300 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
301 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
302 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
303 uint32_t op = (instr >> 23) & 3;
304 uint32_t W = (instr >> 21) & 1;
305 uint32_t L = (instr >> 20) & 1;
306 ArmRegister Rn(instr, 16);
307 if (op == 1 || op == 2) {
308 if (op == 1) {
309 if (L == 0) {
310 opcode << "stm";
311 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800312 } else {
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700313 if (Rn.r != 13) {
314 opcode << "ldm";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700315 args << Rn << (W == 0 ? "" : "!") << ", ";
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700316 } else {
317 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800318 }
319 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700320 } else {
321 if (L == 0) {
322 if (Rn.r != 13) {
323 opcode << "stmdb";
324 args << Rn << (W == 0 ? "" : "!") << ", ";
325 } else {
326 opcode << "push";
327 }
328 } else {
329 opcode << "ldmdb";
330 args << Rn << (W == 0 ? "" : "!") << ", ";
331 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800332 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700333 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800334 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700335 } else if ((op2 & 0x60) == 0x20) { // 01x xxxx
336 // Data-processing (shifted register)
337 // |111|1110|0000|0|0000|1111|1100|0000|0000|
338 // |5 3|2109|8765|4|3 0|5 |10 8|7 5 |3 0|
339 // |---|----|----|-|----|----|----|----|----|
340 // |332|2222|2222|2|1111|1111|1100|0000|0000|
341 // |1 9|8765|4321|0|9 6|5 |10 8|7 5 |3 0|
342 // |---|----|----|-|----|----|----|----|----|
343 // |111|0101| op3|S| Rn | | Rd | | Rm |
344 uint32_t op3 = (instr >> 21) & 0xF;
345 uint32_t S = (instr >> 20) & 1;
346 uint32_t Rn = (instr >> 16) & 0xF;
347 ArmRegister Rd(instr, 8);
348 ArmRegister Rm(instr, 0);
349 switch (op3) {
350 case 0x0:
351 if (Rn != 0xF) {
352 opcode << "and";
353 } else {
354 opcode << "tst";
355 S = 0; // don't print 's'
356 }
357 break;
358 case 0x1: opcode << "bic"; break;
359 case 0x2:
360 if (Rn != 0xF) {
361 opcode << "orr";
362 } else {
363 opcode << "mov";
364 }
365 break;
366 case 0x3:
367 if (Rn != 0xF) {
368 opcode << "orn";
369 } else {
370 opcode << "mvn";
371 }
372 break;
373 case 0x4:
374 if (Rn != 0xF) {
375 opcode << "eor";
376 } else {
377 opcode << "teq";
378 S = 0; // don't print 's'
379 }
380 break;
381 case 0x6: opcode << "pkh"; break;
382 case 0x8:
383 if (Rn != 0xF) {
384 opcode << "add";
385 } else {
386 opcode << "cmn";
387 S = 0; // don't print 's'
388 }
389 break;
390 case 0xA: opcode << "adc"; break;
391 case 0xB: opcode << "sbc"; break;
392 }
Ian Rogers087b2412012-03-21 01:30:32 -0700393
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700394 if (S == 1) {
395 opcode << "s";
Ian Rogers087b2412012-03-21 01:30:32 -0700396 }
Ian Rogersc7fe4e02012-03-29 21:36:21 -0700397 opcode << ".w";
398 args << Rd << ", " << Rm;
399 } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
400 // Co-processor instructions
401 // |111|1|11|000000|0000|1111|1100|000|0 |0000|
402 // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
403 // |---|-|--|------|----|----|----|---|---|----|
404 // |332|2|22|222222|1111|1111|1100|000|0 |0000|
405 // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
406 // |---|-|--|------|----|----|----|---|---|----|
407 // |111| |11| op3 | Rn | |copr| |op4| |
408 uint32_t op3 = (instr >> 20) & 0x3F;
409 uint32_t coproc = (instr >> 8) & 0xF;
410 uint32_t op4 = (instr >> 4) & 0x1;
411 if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0
412 if ((coproc & 0xE) == 0xA) {
413 // VFP data-processing instructions
414 // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000|
415 // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0|
416 // |---|-|----|----|----|----|---|-|----|-|-|----|
417 // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000|
418 // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0|
419 // |---|-|----|----|----|----|---|-|----|-|-|----|
420 // |111|T|1110|opc1|opc2| |101| |opc3| | | |
421 // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69
422 uint32_t opc1 = (instr >> 20) & 0xF;
423 uint32_t opc2 = (instr >> 16) & 0xF;
424 //uint32_t opc3 = (instr >> 6) & 0x3;
425 if ((opc1 & 0xB) == 0xB) { // 1x11
426 // Other VFP data-processing instructions.
427 switch (opc2) {
428 case 0x4: case 0x5: { // Vector compare
429 // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm
430 uint32_t D = (instr >> 22) & 0x1;
431 uint32_t Vd = (instr >> 12) & 0xF;
432 uint32_t sz = (instr >> 8) & 1;
433 uint32_t E = (instr >> 7) & 1;
434 uint32_t M = (instr >> 5) & 1;
435 uint32_t Vm = instr & 0xF;
436 bool dp_operation = sz == 1;
437 opcode << (E == 0 ? "vcmp" : "vcmpe");
438 opcode << (dp_operation ? ".f64" : ".f32");
439 if (dp_operation) {
440 args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm);
441 } else {
442 args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M);
443 }
444 break;
445 }
446 }
447 }
448 }
449 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800450 }
451 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800452 case 2:
453 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
454 // Data-processing (modified immediate)
455 // |111|11|10|0000|0|0000|1|111|1100|00000000|
456 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
457 // |---|--|--|----|-|----|-|---|----|--------|
458 // |332|22|22|2222|2|1111|1|111|1100|00000000|
459 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
460 // |---|--|--|----|-|----|-|---|----|--------|
461 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
462 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
Ian Rogers40627db2012-03-04 17:31:09 -0800463 uint32_t i = (instr >> 26) & 1;
464 uint32_t op3 = (instr >> 21) & 0xF;
465 uint32_t S = (instr >> 20) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700466 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800467 uint32_t imm3 = (instr >> 12) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700468 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800469 uint32_t imm8 = instr & 0xFF;
470 int32_t imm32 = (i << 12) | (imm3 << 8) | imm8;
471 switch (op3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700472 case 0x0: opcode << "and"; break;
473 case 0x1: opcode << "bic"; break;
474 case 0x2: opcode << "orr"; break;
475 case 0x3: opcode << "orn"; break;
476 case 0x4: opcode << "eor"; break;
477 case 0x8: opcode << "add"; break;
478 case 0xA: opcode << "adc"; break;
479 case 0xB: opcode << "sbc"; break;
480 case 0xD: opcode << "sub"; break;
481 case 0xE: opcode << "rsb"; break;
482 default: opcode << "UNKNOWN DPMI-" << op3; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800483 }
484 if (S == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700485 opcode << "s";
Ian Rogers40627db2012-03-04 17:31:09 -0800486 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700487 args << Rd << ", " << Rn << ", ThumbExpand(" << imm32 << ")";
Ian Rogers40627db2012-03-04 17:31:09 -0800488 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
489 // Data-processing (plain binary immediate)
490 // |111|11|10|00000|0000|1|111110000000000|
491 // |5 3|21|09|87654|3 0|5|4 0 5 0|
492 // |---|--|--|-----|----|-|---------------|
493 // |332|22|22|22222|1111|1|111110000000000|
494 // |1 9|87|65|43210|9 6|5|4 0 5 0|
495 // |---|--|--|-----|----|-|---------------|
496 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
497 uint32_t op3 = (instr >> 20) & 0x1F;
Ian Rogers40627db2012-03-04 17:31:09 -0800498 switch (op3) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700499 case 0x00: {
500 ArmRegister Rd(instr, 8);
501 ArmRegister Rn(instr, 16);
502 uint32_t i = (instr >> 26) & 1;
503 uint32_t imm3 = (instr >> 12) & 0x7;
504 uint32_t imm8 = instr & 0xFF;
505 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
506 if (Rn.r != 0xF) {
507 opcode << "addw";
508 args << Rd << ", " << Rn << ", #" << imm12;
509 } else {
510 opcode << "adr";
511 args << Rd << ", ";
512 DumpBranchTarget(args, instr_ptr + 4, imm12);
513 }
514 break;
515 }
Ian Rogers40627db2012-03-04 17:31:09 -0800516 case 0x04: {
517 // MOVW Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700518 ArmRegister Rd(instr, 8);
Ian Rogers40627db2012-03-04 17:31:09 -0800519 uint32_t i = (instr >> 26) & 1;
520 uint32_t imm3 = (instr >> 12) & 0x7;
521 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700522 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800523 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700524 opcode << "movw";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700525 args << Rd << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800526 break;
527 }
528 case 0x0A: {
529 // SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700530 ArmRegister Rd(instr, 8);
531 ArmRegister Rn(instr, 16);
Ian Rogers40627db2012-03-04 17:31:09 -0800532 uint32_t i = (instr >> 26) & 1;
533 uint32_t imm3 = (instr >> 12) & 0x7;
534 uint32_t imm8 = instr & 0xFF;
535 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700536 opcode << "sub.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700537 args << Rd << ", " << Rn << ", #" << imm12;
Ian Rogers40627db2012-03-04 17:31:09 -0800538 break;
539 }
540 default:
541 break;
542 }
543 } else {
544 // Branches and miscellaneous control
545 // |111|11|1000000|0000|1|111|1100|00000000|
546 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
547 // |---|--|-------|----|-|---|----|--------|
548 // |332|22|2222222|1111|1|111|1100|00000000|
549 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
550 // |---|--|-------|----|-|---|----|--------|
551 // |111|10| op2 | |1|op3|op4 | |
552
553 uint32_t op3 = (instr >> 12) & 7;
554 //uint32_t op4 = (instr >> 8) & 0xF;
555 switch (op3) {
556 case 0:
557 if ((op2 & 0x38) != 0x38) {
558 // Conditional branch
559 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
560 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
561 // |---|--|-|----|------|-|-|--|-|--|-----------|
562 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
563 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
564 // |---|--|-|----|------|-|-|--|-|--|-----------|
565 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
566 uint32_t S = (instr >> 26) & 1;
567 uint32_t J2 = (instr >> 11) & 1;
568 uint32_t J1 = (instr >> 13) & 1;
569 uint32_t imm6 = (instr >> 16) & 0x3F;
570 uint32_t imm11 = instr & 0x7FF;
571 uint32_t cond = (instr >> 22) & 0xF;
572 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
573 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700574 opcode << "b";
575 DumpCond(opcode, cond);
576 opcode << ".w";
577 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800578 }
579 break;
580 case 2:
581 case 1: case 3:
582 break;
583 case 4: case 6: case 5: case 7: {
584 // BL, BLX (immediate)
585 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
586 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
587 // |---|--|-|----------|--|--|-|--|-----------|
588 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
589 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
590 // |---|--|-|----------|--|--|-|--|-----------|
591 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
592 uint32_t S = (instr >> 26) & 1;
593 uint32_t J2 = (instr >> 11) & 1;
594 uint32_t L = (instr >> 12) & 1;
595 uint32_t J1 = (instr >> 13) & 1;
596 uint32_t imm10 = (instr >> 16) & 0x3FF;
597 uint32_t imm11 = instr & 0x7FF;
598 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700599 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800600 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700601 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800602 }
603 uint32_t I1 = ~(J1 ^ S);
604 uint32_t I2 = ~(J2 ^ S);
605 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
606 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700607 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800608 break;
609 }
610 }
611 }
612 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800613 case 3:
614 switch (op2) {
615 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
616 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
617 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800618 // |111|11|100|000|0|0000|1111|110000|000000|
619 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
620 // |---|--|---|---|-|----|----|------|------|
621 // |332|22|222|222|2|1111|1111|110000|000000|
622 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
623 // |---|--|---|---|-|----|----|------|------|
624 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800625 uint32_t op3 = (instr >> 21) & 7;
626 //uint32_t op4 = (instr >> 6) & 0x3F;
627 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700628 case 0x0: case 0x4: {
629 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
630 // 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 -0700631 ArmRegister Rn(instr, 16);
632 ArmRegister Rt(instr, 12);
Ian Rogers087b2412012-03-21 01:30:32 -0700633 opcode << "strb";
634 if ((instr & 0x800) != 0) {
635 uint32_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700636 args << Rt << ", [" << Rn << ",#" << imm8 << "]";
Ian Rogers087b2412012-03-21 01:30:32 -0700637 } else {
638 uint32_t imm2 = (instr >> 4) & 3;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700639 ArmRegister Rm(instr, 0);
640 args << Rt << ", [" << Rn << ", " << Rm;
Ian Rogers087b2412012-03-21 01:30:32 -0700641 if (imm2 != 0) {
642 args << ", " << "lsl #" << imm2;
643 }
644 args << "]";
645 }
646 break;
647 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800648 case 0x2: case 0x6: {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700649 ArmRegister Rn(instr, 16);
650 ArmRegister Rt(instr, 12);
Ian Rogers40627db2012-03-04 17:31:09 -0800651 if (op3 == 2) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700652 if ((instr & 0x800) != 0) {
653 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
654 uint32_t P = (instr >> 10) & 1;
655 uint32_t U = (instr >> 9) & 1;
656 uint32_t W = (instr >> 8) & 1;
657 uint32_t imm8 = instr & 0xFF;
658 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
659 if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) {
660 opcode << "push";
661 args << Rt;
662 } else if (Rn.r == 15 || (P == 0 && W == 0)) {
663 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800664 } else {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700665 if (P == 1 && U == 1 && W == 0) {
666 opcode << "strt";
667 } else {
668 opcode << "str";
669 }
670 args << Rt << ", [" << Rn;
671 if (P == 0 && W == 1) {
672 args << "], #" << imm32;
673 } else {
674 args << ", #" << imm32 << "]";
675 if (W == 1) {
676 args << "!";
677 }
Ian Rogers40627db2012-03-04 17:31:09 -0800678 }
679 }
Ian Rogers66a3fca2012-04-09 19:51:34 -0700680 } else {
681 // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm
682 ArmRegister Rn(instr, 16);
683 ArmRegister Rt(instr, 12);
684 ArmRegister Rm(instr, 0);
685 uint32_t imm2 = (instr >> 4) & 3;
686 opcode << "str.w";
687 args << Rt << ", [" << Rn << ", " << Rm;
688 if (imm2 != 0) {
689 args << ", lsl #" << imm2;
690 }
691 args << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800692 }
693 } else if (op3 == 6) {
Ian Rogers66a3fca2012-04-09 19:51:34 -0700694 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800695 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700696 opcode << "str.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700697 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800698 }
Ian Rogers40627db2012-03-04 17:31:09 -0800699 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800700 }
701 }
702
703 break;
704 }
705 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
706 // Load word
707 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
708 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
709 // |---|--|--|---|--|-|----|----|------|------|
710 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
711 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
712 // |---|--|--|---|--|-|----|----|------|------|
713 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
714 // |111|11| op2 | | | imm12 |
715 uint32_t op3 = (instr >> 23) & 3;
716 uint32_t op4 = (instr >> 6) & 0x3F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700717 ArmRegister Rn(instr, 16);
718 ArmRegister Rt(instr, 12);
719 if (op3 == 1 || Rn.r == 15) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800720 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
721 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
722 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700723 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700724 args << Rt << ", [" << Rn << ", #" << imm12 << "]";
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700725 if (Rn.r == 9) {
726 args << " ; ";
727 Thread::DumpThreadOffset(args, imm12, 4);
Ian Rogers5b9b1bc2012-04-09 22:51:43 -0700728 } else if (Rn.r == 15) {
729 intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr);
730 lit_adr = RoundDown(lit_adr, 4) + 4 + imm12;
731 args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr));
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700732 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800733 } else if (op4 == 0) {
734 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
735 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700736 ArmRegister rm(instr, 0);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700737 opcode << "ldr.w";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700738 args << Rt << ", [" << Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800739 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700740 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800741 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700742 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800743 } else {
744 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
745 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700746 opcode << "ldrt";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700747 args << Rt << ", [" << Rn << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800748 }
749 break;
750 }
751 }
752 default:
753 break;
754 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700755 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 -0800756 return 4;
757}
758
759size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
760 uint16_t instr = ReadU16(instr_ptr);
761 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
762 if (is_32bit) {
763 return DumpThumb32(os, instr_ptr);
764 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700765 std::ostringstream opcode;
766 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800767 uint16_t opcode1 = instr >> 10;
768 if (opcode1 < 0x10) {
769 // shift (immediate), add, subtract, move, and compare
770 uint16_t opcode2 = instr >> 9;
771 switch (opcode2) {
772 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
773 case 0x8: case 0x9: case 0xA: case 0xB: {
774 // Logical shift left - 00 000xx xxxxxxxxx
775 // Logical shift right - 00 001xx xxxxxxxxx
776 // Arithmetic shift right - 00 010xx xxxxxxxxx
777 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700778 ThumbRegister rm(instr, 3);
779 ThumbRegister Rd(instr, 7);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800780 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700781 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800782 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700783 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800784 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700785 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800786 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700787 args << Rd << ", " << rm << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800788 break;
789 }
790 case 0xC: case 0xD: case 0xE: case 0xF: {
791 // Add register - 00 01100 mmm nnn ddd
792 // Sub register - 00 01101 mmm nnn ddd
793 // Add 3-bit immediate - 00 01110 iii nnn ddd
794 // Sub 3-bit immediate - 00 01111 iii nnn ddd
795 uint16_t imm3_or_Rm = (instr >> 6) & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700796 ThumbRegister Rn(instr, 3);
797 ThumbRegister Rd(instr, 0);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800798 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700799 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800800 } else {
801 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700802 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800803 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700804 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800805 }
806 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700807 args << Rd << ", " << Rn;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800808 if ((opcode2 & 2) == 0) {
Elliott Hughes630e77d2012-03-22 19:20:56 -0700809 ArmRegister Rm(imm3_or_Rm);
810 args << ", " << Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800811 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700812 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800813 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800814 break;
815 }
816 case 0x10: case 0x11: case 0x12: case 0x13:
817 case 0x14: case 0x15: case 0x16: case 0x17:
818 case 0x18: case 0x19: case 0x1A: case 0x1B:
819 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
820 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
821 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
822 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
823 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
Elliott Hughes630e77d2012-03-22 19:20:56 -0700824 ThumbRegister Rn(instr, 8);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800825 uint16_t imm8 = instr & 0xFF;
826 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700827 case 4: opcode << "movs"; break;
828 case 5: opcode << "cmp"; break;
829 case 6: opcode << "adds"; break;
830 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800831 }
Elliott Hughes630e77d2012-03-22 19:20:56 -0700832 args << Rn << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800833 break;
834 }
835 default:
836 break;
837 }
Ian Rogersad03ef52012-03-18 19:34:47 -0700838 } else if (opcode1 == 0x10) {
839 // Data-processing
840 uint16_t opcode2 = (instr >> 6) & 0xF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700841 ThumbRegister rm(instr, 3);
842 ThumbRegister rdn(instr, 0);
Ian Rogersad03ef52012-03-18 19:34:47 -0700843 opcode << kThumbDataProcessingOperations[opcode2];
Elliott Hughes630e77d2012-03-22 19:20:56 -0700844 args << rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800845 } else if (opcode1 == 0x11) {
846 // Special data instructions and branch and exchange
847 uint16_t opcode2 = (instr >> 6) & 0x0F;
848 switch (opcode2) {
849 case 0x0: case 0x1: case 0x2: case 0x3: {
850 // Add low registers - 010001 0000 xxxxxx
851 // Add high registers - 010001 0001/001x xxxxxx
852 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700853 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800854 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700855 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700856 opcode << "add";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700857 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800858 break;
859 }
860 case 0x8: case 0x9: case 0xA: case 0xB: {
861 // Move low registers - 010001 1000 xxxxxx
862 // Move high registers - 010001 1001/101x xxxxxx
863 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700864 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800865 uint16_t Rdn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700866 ArmRegister DN_Rdn((DN << 3) | Rdn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700867 opcode << "mov";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700868 args << DN_Rdn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800869 break;
870 }
871 case 0x5: case 0x6: case 0x7: {
872 // Compare high registers - 010001 0101/011x xxxxxx
873 uint16_t N = (instr >> 7) & 1;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700874 ArmRegister rm(instr, 3);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800875 uint16_t Rn = instr & 7;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700876 ArmRegister N_Rn((N << 3) | Rn);
Elliott Hughescbf0b612012-03-15 16:23:47 -0700877 opcode << "cmp";
Elliott Hughes630e77d2012-03-22 19:20:56 -0700878 args << N_Rn << ", " << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800879 break;
880 }
881 case 0xC: case 0xD: case 0xE: case 0xF: {
882 // Branch and exchange - 010001 110x xxxxxx
883 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes630e77d2012-03-22 19:20:56 -0700884 ArmRegister rm(instr, 3);
885 opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx");
886 args << rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800887 break;
888 }
889 default:
890 break;
891 }
892 } else if ((instr & 0xF000) == 0xB000) {
893 // Miscellaneous 16-bit instructions
894 uint16_t opcode2 = (instr >> 5) & 0x7F;
895 switch (opcode2) {
896 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
897 // Add immediate to SP - 1011 00000 ii iiiii
898 // Subtract immediate from SP - 1011 00001 ii iiiii
899 int imm7 = instr & 0x7F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700900 opcode << ((opcode2 & 4) == 0 ? "add" : "sub");
Elliott Hughescbf0b612012-03-15 16:23:47 -0700901 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800902 break;
903 }
Ian Rogers087b2412012-03-21 01:30:32 -0700904 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
905 case 0x0C: case 0x0D: case 0x0E: case 0x0F: {
906 // CBNZ, CBZ
907 uint16_t op = (instr >> 11) & 1;
908 uint16_t i = (instr >> 9) & 1;
909 uint16_t imm5 = (instr >> 3) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700910 ThumbRegister Rn(instr, 0);
Ian Rogers087b2412012-03-21 01:30:32 -0700911 opcode << (op != 0 ? "cbnz" : "cbz");
912 uint32_t imm32 = (i << 7) | (imm5 << 1);
Elliott Hughes630e77d2012-03-22 19:20:56 -0700913 args << Rn << ", ";
Ian Rogers087b2412012-03-21 01:30:32 -0700914 DumpBranchTarget(args, instr_ptr + 4, imm32);
915 break;
916 }
Ian Rogers40627db2012-03-04 17:31:09 -0800917 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
918 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
919 // If-Then, and hints
920 uint16_t opA = (instr >> 4) & 0xF;
921 uint16_t opB = instr & 0xF;
922 if (opB == 0) {
923 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700924 case 0: opcode << "nop"; break;
925 case 1: opcode << "yield"; break;
926 case 2: opcode << "wfe"; break;
927 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800928 default: break;
929 }
930 } else {
Elliott Hughes105afd22012-04-10 15:04:25 -0700931 uint32_t first_cond = opA;
932 uint32_t mask = opB;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700933 opcode << "it";
Elliott Hughes105afd22012-04-10 15:04:25 -0700934
935 // Flesh out the base "it" opcode with the specific collection of 't's and 'e's,
936 // and store up the actual condition codes we'll want to add to the next few opcodes.
937 size_t count = 3 - CTZ(mask);
938 it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself.
939 for (size_t i = 0; i < count; ++i) {
940 bool positive_cond = ((first_cond & 1) != 0);
941 bool positive_mask = ((mask & (1 << (3 - i))) != 0);
942 if (positive_mask == positive_cond) {
943 opcode << 't';
944 it_conditions_[i] = kConditionCodeNames[first_cond];
945 } else {
946 opcode << 'e';
947 it_conditions_[i] = kConditionCodeNames[first_cond ^ 1];
948 }
949 }
950 it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'.
951
952 it_conditions_[count + 1] = ""; // No condition code for the IT itself...
953 DumpCond(args, first_cond); // ...because it's considered an argument.
Ian Rogers40627db2012-03-04 17:31:09 -0800954 }
955 break;
956 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800957 default:
958 break;
959 }
960 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
961 ((instr & 0xE000) == 0x8000)) {
962 // Load/store single data item
963 uint16_t opA = instr >> 12;
964 //uint16_t opB = (instr >> 9) & 7;
965 switch (opA) {
966 case 0x6: {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700967 // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt
968 // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800969 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700970 ThumbRegister Rn(instr, 3);
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700971 ThumbRegister Rt(instr, 0);
Elliott Hughes630e77d2012-03-22 19:20:56 -0700972 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
973 args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800974 break;
975 }
976 case 0x9: {
977 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
978 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
979 uint16_t imm8 = instr & 0xFF;
Elliott Hughes630e77d2012-03-22 19:20:56 -0700980 ThumbRegister Rt(instr, 8);
981 opcode << ((instr & 0x800) == 0 ? "str" : "ldr");
982 args << Rt << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800983 break;
984 }
985 default:
986 break;
987 }
Ian Rogers40627db2012-03-04 17:31:09 -0800988 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
989 uint16_t imm11 = instr & 0x7FFF;
990 int32_t imm32 = imm11 << 1;
991 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700992 opcode << "b";
993 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800994 }
Elliott Hughes105afd22012-04-10 15:04:25 -0700995
996 // Apply any IT-block conditions to the opcode if necessary.
997 if (!it_conditions_.empty()) {
998 opcode << it_conditions_.back();
999 it_conditions_.pop_back();
1000 }
1001
Elliott Hughescbf0b612012-03-15 16:23:47 -07001002 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 -08001003 }
1004 return 2;
1005}
1006
1007} // namespace arm
1008} // namespace art