blob: 7cde7b06dc7b8e1f5e2cb9b5b309bf4864d30c23 [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 Rogers3a5c1ce2012-02-29 10:06:46 -080071void DisassemblerArm::DumpReg(std::ostream& os, uint32_t reg) {
72 switch (reg) {
Elliott Hughescbf0b612012-03-15 16:23:47 -070073 case 13: os << "sp"; break;
74 case 14: os << "lr"; break;
75 case 15: os << "pc"; break;
76 default: os << "r" << reg; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080077 }
78}
79
Ian Rogers40627db2012-03-04 17:31:09 -080080void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) {
81 os << imm32 << " (" << reinterpret_cast<const void*>(instr_ptr + imm32) << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080082}
83
84static uint32_t ReadU16(const uint8_t* ptr) {
85 return ptr[0] | (ptr[1] << 8);
86}
87
88static uint32_t ReadU32(const uint8_t* ptr) {
89 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
90}
91
Elliott Hughes77405792012-03-15 15:22:12 -070092static const char* kDataProcessingOperations[] = {
Elliott Hughescbf0b612012-03-15 16:23:47 -070093 "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc",
94 "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn",
Elliott Hughes77405792012-03-15 15:22:12 -070095};
96
Ian Rogersad03ef52012-03-18 19:34:47 -070097static const char* kThumbDataProcessingOperations[] = {
98 "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror",
99 "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn",
100};
101
Elliott Hughes77405792012-03-15 15:22:12 -0700102struct ArmRegister {
103 ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
104 uint32_t r;
105};
106std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
107 if (r.r == 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700108 os << "sp";
Elliott Hughes77405792012-03-15 15:22:12 -0700109 } else if (r.r == 14) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700110 os << "lr";
Elliott Hughes77405792012-03-15 15:22:12 -0700111 } else if (r.r == 15) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700112 os << "pc";
Elliott Hughes77405792012-03-15 15:22:12 -0700113 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700114 os << "r" << r.r;
Elliott Hughes77405792012-03-15 15:22:12 -0700115 }
116 return os;
117}
118
119struct Rd : ArmRegister {
120 Rd(uint32_t instruction) : ArmRegister((instruction >> 12) & 0xf) {}
121};
122typedef Rd Rt;
123struct Rn : ArmRegister {
124 Rn(uint32_t instruction) : ArmRegister((instruction >> 16) & 0xf) {}
125};
126
127struct Rm {
128 Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
129 uint32_t shift;
130 ArmRegister rm;
131};
132std::ostream& operator<<(std::ostream& os, const Rm& r) {
133 os << r.rm;
134 if (r.shift != 0) {
135 os << "-shift-" << r.shift; // TODO
136 }
137 return os;
138}
139
140struct Imm12 {
141 Imm12(uint32_t instruction) : rotate((instruction >> 8) & 0xf), imm(instruction & 0xff) {}
142 uint32_t rotate;
143 uint32_t imm;
144};
145std::ostream& operator<<(std::ostream& os, const Imm12& rhs) {
146 uint32_t imm = (rhs.imm >> (2 * rhs.rotate)) | (rhs.imm << (32 - (2 * rhs.rotate)));
147 os << "#" << imm;
148 return os;
149}
150
151struct RegisterList {
152 RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
153 uint32_t register_list;
154};
155std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
156 if (rhs.register_list == 0) {
157 os << "<no register list?>";
158 return os;
159 }
160 bool first = true;
161 for (size_t i = 0; i < 16; i++) {
162 if ((rhs.register_list & (1 << i)) != 0) {
163 if (first) {
164 os << "{";
165 first = false;
166 } else {
167 os << ", ";
168 }
169 os << ArmRegister(i);
170 }
171 }
172 os << "}";
173 return os;
174}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800175
176void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700177 uint32_t instruction = ReadU32(instr_ptr);
178 uint32_t cond = (instruction >> 28) & 0xf;
179 uint32_t op1 = (instruction >> 25) & 0x7;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700180 std::ostringstream opcode;
181 std::ostringstream args;
Elliott Hughes77405792012-03-15 15:22:12 -0700182 switch (op1) {
183 case 0:
184 case 1: // Data processing instructions.
185 {
186 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
Elliott Hughescbf0b612012-03-15 16:23:47 -0700187 opcode << (((instruction >> 5) & 1) ? "blx" : "bx");
188 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 Hughescbf0b612012-03-15 16:23:47 -0700193 opcode << kDataProcessingOperations[(instruction >> 21) & 0xf]
194 << kConditionCodeNames[cond]
195 << (s ? "s" : "");
196 args << Rd(instruction) << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700197 if (i) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700198 args << Rn(instruction) << ", " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700199 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700200 args << Rm(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700201 }
202 }
203 break;
204 case 2: // Load/store word and unsigned byte.
205 {
206 bool p = (instruction & (1 << 24)) != 0;
207 bool b = (instruction & (1 << 22)) != 0;
208 bool w = (instruction & (1 << 21)) != 0;
209 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700210 opcode << (l ? "ldr" : "str") << (b ? "b" : "") << kConditionCodeNames[cond];
211 args << Rt(instruction) << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700212 if (Rn(instruction).r == 0xf) {
213 UNIMPLEMENTED(FATAL) << "literals";
214 } else {
215 bool wback = !p || w;
216 if (p && !wback) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700217 args << "[" << Rn(instruction) << ", " << Imm12(instruction) << "]";
Elliott Hughes77405792012-03-15 15:22:12 -0700218 } else if (p && wback) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700219 args << "[" << Rn(instruction) << ", " << Imm12(instruction) << "]!";
Elliott Hughes77405792012-03-15 15:22:12 -0700220 } else if (!p && wback) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700221 args << "[" << Rn(instruction) << "], " << Imm12(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700222 } else {
223 LOG(FATAL) << p << " " << w;
224 }
225 }
226 }
227 break;
228 case 4: // Load/store multiple.
229 {
230 bool p = (instruction & (1 << 24)) != 0;
231 bool u = (instruction & (1 << 23)) != 0;
232 bool w = (instruction & (1 << 21)) != 0;
233 bool l = (instruction & (1 << 20)) != 0;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700234 opcode << (l ? "ldm" : "stm")
235 << (u ? 'i' : 'd')
236 << (p ? 'b' : 'a')
237 << kConditionCodeNames[cond];
238 args << Rn(instruction) << (w ? "!" : "") << ", " << RegisterList(instruction);
Elliott Hughes77405792012-03-15 15:22:12 -0700239 }
240 break;
241 default:
Elliott Hughescbf0b612012-03-15 16:23:47 -0700242 opcode << "???";
Elliott Hughes77405792012-03-15 15:22:12 -0700243 break;
244 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700245 // TODO: a more complete ARM disassembler could generate wider opcodes.
246 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 -0800247}
248
249size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
250 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
251 // |111|1 1|1000000|0000|1111110000000000|
252 // |5 3|2 1|0987654|3 0|5 0 5 0|
253 // |---|---|-------|----|----------------|
254 // |332|2 2|2222222|1111|1111110000000000|
255 // |1 9|8 7|6543210|9 6|5 0 5 0|
256 // |---|---|-------|----|----------------|
257 // |111|op1| op2 | | |
258 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700259 if (op1 == 0) {
260 return DumpThumb16(os, instr_ptr);
261 }
262
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800263 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700264 std::ostringstream opcode;
265 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800266 switch (op1) {
267 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800268 break;
269 case 1:
270 switch (op2) {
271 case 0x00: case 0x01: case 0x02: case 0x03: case 0x08: case 0x09: case 0x0A: case 0x0B:
272 case 0x10: case 0x11: case 0x12: case 0x13: case 0x18: case 0x19: case 0x1A: case 0x1B: {
273 // |111|11|10|00|0|00|0000|1111110000000000|
274 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
275 // |---|--|--|--|-|--|----|----------------|
276 // |332|22|22|22|2|22|1111|1111110000000000|
277 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
278 // |---|--|--|--|-|--|----|----------------|
279 // |111|01|00|op|0|WL| Rn | |
280 // |111|01| op2 | | |
281 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
282 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
283 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
284 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
285 uint32_t op = (instr >> 23) & 3;
286 uint32_t W = (instr >> 21) & 1;
287 uint32_t L = (instr >> 20) & 1;
288 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800289 if (op == 1 || op == 2) {
290 if (op == 1) {
291 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700292 opcode << "stm";
293 DumpReg(args, Rn);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800294 if (W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700295 args << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800296 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700297 args << "!, ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800298 }
299 } else {
300 if (Rn != 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700301 opcode << "ldm";
302 DumpReg(args, Rn);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800303 if (W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700304 args << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800305 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700306 args << "!, ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800307 }
308 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700309 opcode << "pop";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800310 }
311 }
312 } else {
313 if (L == 0) {
314 if (Rn != 13) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700315 opcode << "stmdb";
316 DumpReg(args, Rn);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800317 if (W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700318 args << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800319 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700320 args << "!, ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800321 }
322 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700323 opcode << "push";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800324 }
325 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700326 opcode << "ldmdb";
327 DumpReg(args, Rn);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800328 if (W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700329 args << ", ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800330 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700331 args << "!, ";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800332 }
333 }
334 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700335 args << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800336 }
337 break;
338 }
Ian Rogers087b2412012-03-21 01:30:32 -0700339 case 0x20: case 0x21: case 0x22: case 0x23: // 01xxxxx
340 case 0x24: case 0x25: case 0x26: case 0x27:
341 case 0x28: case 0x29: case 0x2A: case 0x2B:
342 case 0x2C: case 0x2D: case 0x2E: case 0x2F:
343 case 0x30: case 0x31: case 0x32: case 0x33:
344 case 0x34: case 0x35: case 0x36: case 0x37:
345 case 0x38: case 0x39: case 0x3A: case 0x3B:
346 case 0x3C: case 0x3D: case 0x3E: case 0x3F: {
347 // Data-processing (shifted register)
348 // |111|1110|0000|0|0000|1111|1100|0000|0000|
349 // |5 3|2109|8765|4|3 0|5 |10 8|7 5 |3 0|
350 // |---|----|----|-|----|----|----|----|----|
351 // |332|2222|2222|2|1111|1111|1100|0000|0000|
352 // |1 9|8765|4321|0|9 6|5 |10 8|7 5 |3 0|
353 // |---|----|----|-|----|----|----|----|----|
354 // |111|0101| op3|S| Rn | | Rd | | Rm |
355 uint32_t op3 = (instr >> 21) & 0xF;
356 uint32_t S = (instr >> 20) & 1;
357 uint32_t Rn = (instr >> 16) & 0xF;
358 uint32_t Rd = (instr >> 8) & 0xF;
359 uint32_t Rm = instr & 0xF;
360 switch (op3) {
361 case 0x0:
362 if (Rn != 0xF) {
363 opcode << "and";
364 } else {
365 opcode << "tst";
366 S = 0; // don't print 's'
367 }
368 break;
369 case 0x1: opcode << "bic"; break;
370 case 0x2:
371 if (Rn != 0xF) {
372 opcode << "orr";
373 } else {
374 opcode << "mov";
375 }
376 break;
377 case 0x3:
378 if (Rn != 0xF) {
379 opcode << "orn";
380 } else {
381 opcode << "mvn";
382 }
383 break;
384 case 0x4:
385 if (Rn != 0xF) {
386 opcode << "eor";
387 } else {
388 opcode << "teq";
389 S = 0; // don't print 's'
390 }
391 break;
392 case 0x6: opcode << "pkh"; break;
393 case 0x8:
394 if (Rn != 0xF) {
395 opcode << "add";
396 } else {
397 opcode << "cmn";
398 S = 0; // don't print 's'
399 }
400 break;
401 case 0xA: opcode << "adc"; break;
402 case 0xB: opcode << "sbc"; break;
403 }
404
405 if (S == 1) {
406 opcode << "s";
407 }
408 opcode << ".w";
409 DumpReg(args, Rd);
410 args << ", ";
411 DumpReg(args, Rm);
412 break;
413 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800414 default:
415 break;
416 }
417 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800418 case 2:
419 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
420 // Data-processing (modified immediate)
421 // |111|11|10|0000|0|0000|1|111|1100|00000000|
422 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
423 // |---|--|--|----|-|----|-|---|----|--------|
424 // |332|22|22|2222|2|1111|1|111|1100|00000000|
425 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
426 // |---|--|--|----|-|----|-|---|----|--------|
427 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
428 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
429 // 111 10 00 0110 0 0000 1 000 0000 10101101 - f0c080ad
430 uint32_t i = (instr >> 26) & 1;
431 uint32_t op3 = (instr >> 21) & 0xF;
432 uint32_t S = (instr >> 20) & 1;
433 uint32_t Rn = (instr >> 16) & 0xF;
434 uint32_t imm3 = (instr >> 12) & 7;
435 uint32_t Rd = (instr >> 8) & 0xF;
436 uint32_t imm8 = instr & 0xFF;
437 int32_t imm32 = (i << 12) | (imm3 << 8) | imm8;
438 switch (op3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700439 case 0x0: opcode << "and"; break;
440 case 0x1: opcode << "bic"; break;
441 case 0x2: opcode << "orr"; break;
442 case 0x3: opcode << "orn"; break;
443 case 0x4: opcode << "eor"; break;
444 case 0x8: opcode << "add"; break;
445 case 0xA: opcode << "adc"; break;
446 case 0xB: opcode << "sbc"; break;
447 case 0xD: opcode << "sub"; break;
448 case 0xE: opcode << "rsb"; break;
449 default: opcode << "UNKNOWN DPMI-" << op3; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800450 }
451 if (S == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700452 opcode << "s";
Ian Rogers40627db2012-03-04 17:31:09 -0800453 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700454 DumpReg(args, Rd);
455 args << ", ";
456 DumpReg(args, Rn);
457 args << ", 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;
468 uint32_t Rn = (instr >> 16) & 0xF;
469 switch (op3) {
470 case 0x04: {
471 // MOVW Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
472 uint32_t Rd = (instr >> 8) & 0xF;
473 uint32_t i = (instr >> 26) & 1;
474 uint32_t imm3 = (instr >> 12) & 0x7;
475 uint32_t imm8 = instr & 0xFF;
476 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700477 opcode << "movw";
478 DumpReg(args, Rd);
479 args << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800480 break;
481 }
482 case 0x0A: {
483 // SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
484 uint32_t Rd = (instr >> 8) & 0xF;
485 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";
490 DumpReg(args, Rd);
491 args << ", ";
492 DumpReg(args, Rn);
493 args << ", #" << imm12;
Ian Rogers40627db2012-03-04 17:31:09 -0800494 break;
495 }
496 default:
497 break;
498 }
499 } else {
500 // Branches and miscellaneous control
501 // |111|11|1000000|0000|1|111|1100|00000000|
502 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
503 // |---|--|-------|----|-|---|----|--------|
504 // |332|22|2222222|1111|1|111|1100|00000000|
505 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
506 // |---|--|-------|----|-|---|----|--------|
507 // |111|10| op2 | |1|op3|op4 | |
508
509 uint32_t op3 = (instr >> 12) & 7;
510 //uint32_t op4 = (instr >> 8) & 0xF;
511 switch (op3) {
512 case 0:
513 if ((op2 & 0x38) != 0x38) {
514 // Conditional branch
515 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
516 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
517 // |---|--|-|----|------|-|-|--|-|--|-----------|
518 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
519 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
520 // |---|--|-|----|------|-|-|--|-|--|-----------|
521 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
522 uint32_t S = (instr >> 26) & 1;
523 uint32_t J2 = (instr >> 11) & 1;
524 uint32_t J1 = (instr >> 13) & 1;
525 uint32_t imm6 = (instr >> 16) & 0x3F;
526 uint32_t imm11 = instr & 0x7FF;
527 uint32_t cond = (instr >> 22) & 0xF;
528 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
529 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700530 opcode << "b";
531 DumpCond(opcode, cond);
532 opcode << ".w";
533 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800534 }
535 break;
536 case 2:
537 case 1: case 3:
538 break;
539 case 4: case 6: case 5: case 7: {
540 // BL, BLX (immediate)
541 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
542 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
543 // |---|--|-|----------|--|--|-|--|-----------|
544 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
545 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
546 // |---|--|-|----------|--|--|-|--|-----------|
547 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
548 uint32_t S = (instr >> 26) & 1;
549 uint32_t J2 = (instr >> 11) & 1;
550 uint32_t L = (instr >> 12) & 1;
551 uint32_t J1 = (instr >> 13) & 1;
552 uint32_t imm10 = (instr >> 16) & 0x3FF;
553 uint32_t imm11 = instr & 0x7FF;
554 if (L == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700555 opcode << "bx";
Ian Rogers40627db2012-03-04 17:31:09 -0800556 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700557 opcode << "blx";
Ian Rogers40627db2012-03-04 17:31:09 -0800558 }
559 uint32_t I1 = ~(J1 ^ S);
560 uint32_t I2 = ~(J2 ^ S);
561 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
562 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
Elliott Hughescbf0b612012-03-15 16:23:47 -0700563 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800564 break;
565 }
566 }
567 }
568 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800569 case 3:
570 switch (op2) {
571 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
572 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
573 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800574 // |111|11|100|000|0|0000|1111|110000|000000|
575 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
576 // |---|--|---|---|-|----|----|------|------|
577 // |332|22|222|222|2|1111|1111|110000|000000|
578 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
579 // |---|--|---|---|-|----|----|------|------|
580 // |111|11|000|op3|0| | | op4 | |
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800581 uint32_t op3 = (instr >> 21) & 7;
582 //uint32_t op4 = (instr >> 6) & 0x3F;
583 switch (op3) {
Ian Rogers087b2412012-03-21 01:30:32 -0700584 case 0x0: case 0x4: {
585 // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii
586 // STRB Rt,[Rn,Rm,lsl #imm2] - 111 11 00 0 0 00 0 nnnn tttt 0 00000 ii mmmm
587 uint32_t Rn = (instr >> 16) & 0xF;
588 uint32_t Rt = (instr >> 12) & 0xF;
589 opcode << "strb";
590 if ((instr & 0x800) != 0) {
591 uint32_t imm8 = instr & 0xFF;
592 DumpReg(args, Rt);
593 args << ", [";
594 DumpReg(args, Rn);
595 args << ",#" << imm8 << "]";
596 } else {
597 uint32_t imm2 = (instr >> 4) & 3;
598 uint32_t Rm = instr & 0xF;
599 DumpReg(args, Rt);
600 args << ", [";
601 DumpReg(args, Rn);
602 args << ", ";
603 DumpReg(args, Rm);
604 if (imm2 != 0) {
605 args << ", " << "lsl #" << imm2;
606 }
607 args << "]";
608 }
609 break;
610 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800611 case 0x2: case 0x6: {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800612 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
613 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
614 uint32_t Rn = (instr >> 16) & 0xF;
615 uint32_t Rt = (instr >> 12) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800616 if (op3 == 2) {
617 uint32_t P = (instr >> 10) & 1;
618 uint32_t U = (instr >> 9) & 1;
619 uint32_t W = (instr >> 8) & 1;
620 uint32_t imm8 = instr & 0xFF;
621 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
622 if (Rn == 13 && P == 1 && U == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700623 opcode << "push";
624 DumpReg(args, Rt);
Ian Rogers40627db2012-03-04 17:31:09 -0800625 } else if (Rn == 15 || (P == 0 && W == 0)) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700626 opcode << "UNDEFINED";
Ian Rogers40627db2012-03-04 17:31:09 -0800627 } else {
628 if (P == 1 && U == 1 && W == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700629 opcode << "strt";
Ian Rogers40627db2012-03-04 17:31:09 -0800630 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700631 opcode << "str";
Ian Rogers40627db2012-03-04 17:31:09 -0800632 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700633 DumpReg(args, Rt);
634 args << ", [";
635 DumpReg(args, Rn);
Ian Rogers40627db2012-03-04 17:31:09 -0800636 if (P == 0 && W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700637 args << "], #" << imm32;
Ian Rogers40627db2012-03-04 17:31:09 -0800638 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700639 args << ", #" << imm32 << "]";
Ian Rogers40627db2012-03-04 17:31:09 -0800640 if (W == 1) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700641 args << "!";
Ian Rogers40627db2012-03-04 17:31:09 -0800642 }
643 }
Ian Rogers40627db2012-03-04 17:31:09 -0800644 }
645 } else if (op3 == 6) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800646 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700647 opcode << "str.w";
648 DumpReg(args, Rt);
649 args << ", [";
650 DumpReg(args, Rn);
651 args << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800652 }
Ian Rogers40627db2012-03-04 17:31:09 -0800653 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800654 }
655 }
656
657 break;
658 }
659 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
660 // Load word
661 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
662 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
663 // |---|--|--|---|--|-|----|----|------|------|
664 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
665 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
666 // |---|--|--|---|--|-|----|----|------|------|
667 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
668 // |111|11| op2 | | | imm12 |
669 uint32_t op3 = (instr >> 23) & 3;
670 uint32_t op4 = (instr >> 6) & 0x3F;
671 uint32_t Rn = (instr >> 16) & 0xF;
672 uint32_t Rt = (instr >> 12) & 0xF;
673 if (op3 == 1 || Rn == 15) {
674 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
675 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
676 uint32_t imm12 = instr & 0xFFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700677 opcode << "ldr.w";
678 DumpReg(args, Rt);
679 args << ", [";
680 DumpReg(args, Rn);
681 args << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800682 } else if (op4 == 0) {
683 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
684 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes77405792012-03-15 15:22:12 -0700685 uint32_t rm = instr & 0xF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700686 opcode << "ldr.w";
687 DumpReg(args, Rt);
688 args << ", [";
689 DumpReg(args, Rn);
690 args << ", ";
691 DumpReg(args, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800692 if (imm2 != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700693 args << ", lsl #" << imm2;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800694 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700695 args << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800696 } else {
697 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
698 uint32_t imm8 = instr & 0xFF;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700699 opcode << "ldrt";
700 DumpReg(args, Rt);
701 args << ", [";
702 DumpReg(args, Rn);
703 args << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800704 }
705 break;
706 }
707 }
708 default:
709 break;
710 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700711 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 -0800712 return 4;
713}
714
715size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
716 uint16_t instr = ReadU16(instr_ptr);
717 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
718 if (is_32bit) {
719 return DumpThumb32(os, instr_ptr);
720 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700721 std::ostringstream opcode;
722 std::ostringstream args;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800723 uint16_t opcode1 = instr >> 10;
724 if (opcode1 < 0x10) {
725 // shift (immediate), add, subtract, move, and compare
726 uint16_t opcode2 = instr >> 9;
727 switch (opcode2) {
728 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
729 case 0x8: case 0x9: case 0xA: case 0xB: {
730 // Logical shift left - 00 000xx xxxxxxxxx
731 // Logical shift right - 00 001xx xxxxxxxxx
732 // Arithmetic shift right - 00 010xx xxxxxxxxx
733 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes77405792012-03-15 15:22:12 -0700734 uint16_t rm = (instr >> 3) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800735 uint16_t Rd = instr & 7;
736 if (opcode2 <= 3) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700737 opcode << "lsls";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800738 } else if (opcode2 <= 7) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700739 opcode << "lsrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800740 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700741 opcode << "asrs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800742 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700743 DumpReg(args, Rd);
744 args << ", ";
745 DumpReg(args, rm);
746 args << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800747 break;
748 }
749 case 0xC: case 0xD: case 0xE: case 0xF: {
750 // Add register - 00 01100 mmm nnn ddd
751 // Sub register - 00 01101 mmm nnn ddd
752 // Add 3-bit immediate - 00 01110 iii nnn ddd
753 // Sub 3-bit immediate - 00 01111 iii nnn ddd
754 uint16_t imm3_or_Rm = (instr >> 6) & 7;
755 uint16_t Rn = (instr >> 3) & 7;
756 uint16_t Rd = instr & 7;
757 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700758 opcode << "mov";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800759 } else {
760 if ((opcode2 & 1) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700761 opcode << "adds";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800762 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700763 opcode << "subs";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800764 }
765 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700766 DumpReg(args, Rd);
767 args << ", ";
768 DumpReg(args, Rn);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800769 if ((opcode2 & 2) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700770 args << ", ";
771 DumpReg(args, imm3_or_Rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800772 } else if (imm3_or_Rm != 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700773 args << ", #" << imm3_or_Rm;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800774 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800775 break;
776 }
777 case 0x10: case 0x11: case 0x12: case 0x13:
778 case 0x14: case 0x15: case 0x16: case 0x17:
779 case 0x18: case 0x19: case 0x1A: case 0x1B:
780 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
781 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
782 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
783 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
784 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
785 uint16_t Rn = (instr >> 8) & 7;
786 uint16_t imm8 = instr & 0xFF;
787 switch (opcode2 >> 2) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700788 case 4: opcode << "movs"; break;
789 case 5: opcode << "cmp"; break;
790 case 6: opcode << "adds"; break;
791 case 7: opcode << "subs"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800792 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700793 DumpReg(args, Rn);
794 args << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800795 break;
796 }
797 default:
798 break;
799 }
Ian Rogersad03ef52012-03-18 19:34:47 -0700800 } else if (opcode1 == 0x10) {
801 // Data-processing
802 uint16_t opcode2 = (instr >> 6) & 0xF;
803 uint16_t rm = (instr >> 3) & 0x7;
804 uint16_t rdn = instr & 7;
805 opcode << kThumbDataProcessingOperations[opcode2];
806 DumpReg(args, rdn);
807 args << ", ";
808 DumpReg(args, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800809 } else if (opcode1 == 0x11) {
810 // Special data instructions and branch and exchange
811 uint16_t opcode2 = (instr >> 6) & 0x0F;
812 switch (opcode2) {
813 case 0x0: case 0x1: case 0x2: case 0x3: {
814 // Add low registers - 010001 0000 xxxxxx
815 // Add high registers - 010001 0001/001x xxxxxx
816 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes77405792012-03-15 15:22:12 -0700817 uint16_t rm = (instr >> 3) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800818 uint16_t Rdn = instr & 7;
819 uint16_t DN_Rdn = (DN << 3) | Rdn;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700820 opcode << "add";
821 DumpReg(args, DN_Rdn);
822 args << ", ";
823 DumpReg(args, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800824 break;
825 }
826 case 0x8: case 0x9: case 0xA: case 0xB: {
827 // Move low registers - 010001 1000 xxxxxx
828 // Move high registers - 010001 1001/101x xxxxxx
829 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes77405792012-03-15 15:22:12 -0700830 uint16_t rm = (instr >> 3) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800831 uint16_t Rdn = instr & 7;
832 uint16_t DN_Rdn = (DN << 3) | Rdn;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700833 opcode << "mov";
834 DumpReg(args, DN_Rdn);
835 args << ", ";
836 DumpReg(args, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800837 break;
838 }
839 case 0x5: case 0x6: case 0x7: {
840 // Compare high registers - 010001 0101/011x xxxxxx
841 uint16_t N = (instr >> 7) & 1;
Elliott Hughes77405792012-03-15 15:22:12 -0700842 uint16_t rm = (instr >> 3) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800843 uint16_t Rn = instr & 7;
844 uint16_t N_Rn = (N << 3) | Rn;
Elliott Hughescbf0b612012-03-15 16:23:47 -0700845 opcode << "cmp";
846 DumpReg(args, N_Rn);
847 args << ", ";
848 DumpReg(args, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800849 break;
850 }
851 case 0xC: case 0xD: case 0xE: case 0xF: {
852 // Branch and exchange - 010001 110x xxxxxx
853 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes77405792012-03-15 15:22:12 -0700854 uint16_t rm = instr >> 3 & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800855 if ((opcode2 & 0x2) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700856 opcode << "bx";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800857 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700858 opcode << "blx";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800859 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700860 DumpReg(args, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800861 break;
862 }
863 default:
864 break;
865 }
866 } else if ((instr & 0xF000) == 0xB000) {
867 // Miscellaneous 16-bit instructions
868 uint16_t opcode2 = (instr >> 5) & 0x7F;
869 switch (opcode2) {
870 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
871 // Add immediate to SP - 1011 00000 ii iiiii
872 // Subtract immediate from SP - 1011 00001 ii iiiii
873 int imm7 = instr & 0x7F;
874 if ((opcode2 & 4) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700875 opcode << "add";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800876 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700877 opcode << "sub";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800878 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700879 args << "sp, sp, #" << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800880 break;
881 }
Ian Rogers087b2412012-03-21 01:30:32 -0700882 case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx
883 case 0x0C: case 0x0D: case 0x0E: case 0x0F: {
884 // CBNZ, CBZ
885 uint16_t op = (instr >> 11) & 1;
886 uint16_t i = (instr >> 9) & 1;
887 uint16_t imm5 = (instr >> 3) & 0x1F;
888 uint16_t Rn = instr & 7;
889 opcode << (op != 0 ? "cbnz" : "cbz");
890 uint32_t imm32 = (i << 7) | (imm5 << 1);
891 DumpReg(args, Rn);
892 args << ", ";
893 DumpBranchTarget(args, instr_ptr + 4, imm32);
894 break;
895 }
Ian Rogers40627db2012-03-04 17:31:09 -0800896 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
897 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
898 // If-Then, and hints
899 uint16_t opA = (instr >> 4) & 0xF;
900 uint16_t opB = instr & 0xF;
901 if (opB == 0) {
902 switch (opA) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700903 case 0: opcode << "nop"; break;
904 case 1: opcode << "yield"; break;
905 case 2: opcode << "wfe"; break;
906 case 3: opcode << "sev"; break;
Ian Rogers40627db2012-03-04 17:31:09 -0800907 default: break;
908 }
909 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700910 opcode << "it";
911 args << reinterpret_cast<void*>(opB) << " ";
912 DumpCond(args, opA);
Ian Rogers40627db2012-03-04 17:31:09 -0800913 }
914 break;
915 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800916 default:
917 break;
918 }
919 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
920 ((instr & 0xE000) == 0x8000)) {
921 // Load/store single data item
922 uint16_t opA = instr >> 12;
923 //uint16_t opB = (instr >> 9) & 7;
924 switch (opA) {
925 case 0x6: {
926 // STR Rt, Rn, #imm - 01100 iiiii nnn ttt
927 // LDR Rt, Rn, #imm - 01101 iiiii nnn ttt
928 uint16_t imm5 = (instr >> 6) & 0x1F;
929 uint16_t Rn = (instr >> 3) & 7;
930 uint16_t Rt = instr & 7;
931 if ((instr & 0x800) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700932 opcode << "str";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800933 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700934 opcode << "ldr";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800935 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700936 DumpReg(args, Rt);
937 args << ", [";
938 DumpReg(args, Rn);
939 args << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800940 break;
941 }
942 case 0x9: {
943 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
944 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
945 uint16_t imm8 = instr & 0xFF;
946 uint16_t Rt = (instr >> 8) & 7;
947 if ((instr & 0x800) == 0) {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700948 opcode << "str";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800949 } else {
Elliott Hughescbf0b612012-03-15 16:23:47 -0700950 opcode << "ldr";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800951 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700952 DumpReg(args, Rt);
953 args << ", [sp, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800954 break;
955 }
956 default:
957 break;
958 }
Ian Rogers40627db2012-03-04 17:31:09 -0800959 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
960 uint16_t imm11 = instr & 0x7FFF;
961 int32_t imm32 = imm11 << 1;
962 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
Elliott Hughescbf0b612012-03-15 16:23:47 -0700963 opcode << "b";
964 DumpBranchTarget(args, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800965 }
Elliott Hughescbf0b612012-03-15 16:23:47 -0700966 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 -0800967 }
968 return 2;
969}
970
971} // namespace arm
972} // namespace art