blob: bbdc37e36e3caff0e98bed91f80113e05cce84a6 [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
29
30void 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[] = {
Ian Rogers40627db2012-03-04 17:31:09 -080046 "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
Elliott Hughes77405792012-03-15 15:22:12 -070060 "", // 1110 - always
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) {
Ian Rogers40627db2012-03-04 17:31:09 -080073 case 13: os << "SP"; break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080074 case 14: os << "LR"; break;
75 case 15: os << "PC"; break;
Elliott Hughes77405792012-03-15 15:22:12 -070076 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[] = {
93 "AND", "EOR", "SUB", "RSB", "ADD", "ADC", "SBC", "RSC",
94 "TST", "TEQ", "CMP", "CMN", "ORR", "MOV", "BIC", "MVN",
95};
96
97struct ArmRegister {
98 ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); }
99 uint32_t r;
100};
101std::ostream& operator<<(std::ostream& os, const ArmRegister& r) {
102 if (r.r == 13) {
103 os << "SP";
104 } else if (r.r == 14) {
105 os << "LR";
106 } else if (r.r == 15) {
107 os << "PC";
108 } else {
109 os << "R" << r.r;
110 }
111 return os;
112}
113
114struct Rd : ArmRegister {
115 Rd(uint32_t instruction) : ArmRegister((instruction >> 12) & 0xf) {}
116};
117typedef Rd Rt;
118struct Rn : ArmRegister {
119 Rn(uint32_t instruction) : ArmRegister((instruction >> 16) & 0xf) {}
120};
121
122struct Rm {
123 Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {}
124 uint32_t shift;
125 ArmRegister rm;
126};
127std::ostream& operator<<(std::ostream& os, const Rm& r) {
128 os << r.rm;
129 if (r.shift != 0) {
130 os << "-shift-" << r.shift; // TODO
131 }
132 return os;
133}
134
135struct Imm12 {
136 Imm12(uint32_t instruction) : rotate((instruction >> 8) & 0xf), imm(instruction & 0xff) {}
137 uint32_t rotate;
138 uint32_t imm;
139};
140std::ostream& operator<<(std::ostream& os, const Imm12& rhs) {
141 uint32_t imm = (rhs.imm >> (2 * rhs.rotate)) | (rhs.imm << (32 - (2 * rhs.rotate)));
142 os << "#" << imm;
143 return os;
144}
145
146struct RegisterList {
147 RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {}
148 uint32_t register_list;
149};
150std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) {
151 if (rhs.register_list == 0) {
152 os << "<no register list?>";
153 return os;
154 }
155 bool first = true;
156 for (size_t i = 0; i < 16; i++) {
157 if ((rhs.register_list & (1 << i)) != 0) {
158 if (first) {
159 os << "{";
160 first = false;
161 } else {
162 os << ", ";
163 }
164 os << ArmRegister(i);
165 }
166 }
167 os << "}";
168 return os;
169}
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800170
171void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes77405792012-03-15 15:22:12 -0700172 uint32_t instruction = ReadU32(instr_ptr);
173 uint32_t cond = (instruction >> 28) & 0xf;
174 uint32_t op1 = (instruction >> 25) & 0x7;
175 os << StringPrintf("\t\t\t%p: %08x: ", instr_ptr, instruction);
176 switch (op1) {
177 case 0:
178 case 1: // Data processing instructions.
179 {
180 if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register)
181 os << (((instruction >> 5) & 1) ? "BLX" : "BX") << " " << ArmRegister(instruction & 0xf);
182 break;
183 }
184 bool i = (instruction & (1 << 25)) != 0;
185 bool s = (instruction & (1 << 20)) != 0;
186 os << kDataProcessingOperations[(instruction >> 21) & 0xf]
187 << kConditionCodeNames[cond]
188 << (s ? "S" : "")
189 << " "
190 << Rd(instruction) << ", ";
191 if (i) {
192 os << Rn(instruction) << ", " << Imm12(instruction);
193 } else {
194 os << Rm(instruction);
195 }
196 }
197 break;
198 case 2: // Load/store word and unsigned byte.
199 {
200 bool p = (instruction & (1 << 24)) != 0;
201 bool b = (instruction & (1 << 22)) != 0;
202 bool w = (instruction & (1 << 21)) != 0;
203 bool l = (instruction & (1 << 20)) != 0;
204 os << (l ? "LDR" : "STR") << (b ? "B" : "") << kConditionCodeNames[cond] << " ";
205 os << Rt(instruction) << ", ";
206 if (Rn(instruction).r == 0xf) {
207 UNIMPLEMENTED(FATAL) << "literals";
208 } else {
209 bool wback = !p || w;
210 if (p && !wback) {
211 os << "[" << Rn(instruction) << ", " << Imm12(instruction) << "]";
212 } else if (p && wback) {
213 os << "[" << Rn(instruction) << ", " << Imm12(instruction) << "]!";
214 } else if (!p && wback) {
215 os << "[" << Rn(instruction) << "], " << Imm12(instruction);
216 } else {
217 LOG(FATAL) << p << " " << w;
218 }
219 }
220 }
221 break;
222 case 4: // Load/store multiple.
223 {
224 bool p = (instruction & (1 << 24)) != 0;
225 bool u = (instruction & (1 << 23)) != 0;
226 bool w = (instruction & (1 << 21)) != 0;
227 bool l = (instruction & (1 << 20)) != 0;
228 os << StringPrintf("%s%c%c%s ",
229 l ? "LDM" : "STM",
230 u ? 'I' : 'D',
231 p ? 'B' : 'A',
232 kConditionCodeNames[cond]);
233 os << Rn(instruction) << (w ? "!" : "") << ", " << RegisterList(instruction);
234 }
235 break;
236 default:
237 os << "???";
238 break;
239 }
240 os << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800241}
242
243size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
244 uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
245 // |111|1 1|1000000|0000|1111110000000000|
246 // |5 3|2 1|0987654|3 0|5 0 5 0|
247 // |---|---|-------|----|----------------|
248 // |332|2 2|2222222|1111|1111110000000000|
249 // |1 9|8 7|6543210|9 6|5 0 5 0|
250 // |---|---|-------|----|----------------|
251 // |111|op1| op2 | | |
252 uint32_t op1 = (instr >> 27) & 3;
Elliott Hughes77405792012-03-15 15:22:12 -0700253 if (op1 == 0) {
254 return DumpThumb16(os, instr_ptr);
255 }
256
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800257 uint32_t op2 = (instr >> 20) & 0x7F;
Elliott Hughes77405792012-03-15 15:22:12 -0700258 os << StringPrintf("\t\t\t%p: %08x: ", instr_ptr, instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800259 switch (op1) {
260 case 0:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800261 break;
262 case 1:
263 switch (op2) {
264 case 0x00: case 0x01: case 0x02: case 0x03: case 0x08: case 0x09: case 0x0A: case 0x0B:
265 case 0x10: case 0x11: case 0x12: case 0x13: case 0x18: case 0x19: case 0x1A: case 0x1B: {
266 // |111|11|10|00|0|00|0000|1111110000000000|
267 // |5 3|21|09|87|6|54|3 0|5 0 5 0|
268 // |---|--|--|--|-|--|----|----------------|
269 // |332|22|22|22|2|22|1111|1111110000000000|
270 // |1 9|87|65|43|2|10|9 6|5 0 5 0|
271 // |---|--|--|--|-|--|----|----------------|
272 // |111|01|00|op|0|WL| Rn | |
273 // |111|01| op2 | | |
274 // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
275 // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
276 // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
277 // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
278 uint32_t op = (instr >> 23) & 3;
279 uint32_t W = (instr >> 21) & 1;
280 uint32_t L = (instr >> 20) & 1;
281 uint32_t Rn = (instr >> 16) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800282 if (op == 1 || op == 2) {
283 if (op == 1) {
284 if (L == 0) {
285 os << "STM ";
286 DumpReg(os, Rn);
287 if (W == 0) {
288 os << ", ";
289 } else {
290 os << "!, ";
291 }
292 } else {
293 if (Rn != 13) {
294 os << "LDM ";
295 DumpReg(os, Rn);
296 if (W == 0) {
297 os << ", ";
298 } else {
299 os << "!, ";
300 }
301 } else {
302 os << "POP ";
303 }
304 }
305 } else {
306 if (L == 0) {
307 if (Rn != 13) {
308 os << "STMDB ";
309 DumpReg(os, Rn);
310 if (W == 0) {
311 os << ", ";
312 } else {
313 os << "!, ";
314 }
315 } else {
316 os << "PUSH ";
317 }
318 } else {
319 os << "LDMDB ";
320 DumpReg(os, Rn);
321 if (W == 0) {
322 os << ", ";
323 } else {
324 os << "!, ";
325 }
326 }
327 }
Elliott Hughes77405792012-03-15 15:22:12 -0700328 os << RegisterList(instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800329 }
330 break;
331 }
332 default:
333 break;
334 }
335 break;
Ian Rogers40627db2012-03-04 17:31:09 -0800336 case 2:
337 if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
338 // Data-processing (modified immediate)
339 // |111|11|10|0000|0|0000|1|111|1100|00000000|
340 // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
341 // |---|--|--|----|-|----|-|---|----|--------|
342 // |332|22|22|2222|2|1111|1|111|1100|00000000|
343 // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
344 // |---|--|--|----|-|----|-|---|----|--------|
345 // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
346 // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
347 // 111 10 00 0110 0 0000 1 000 0000 10101101 - f0c080ad
348 uint32_t i = (instr >> 26) & 1;
349 uint32_t op3 = (instr >> 21) & 0xF;
350 uint32_t S = (instr >> 20) & 1;
351 uint32_t Rn = (instr >> 16) & 0xF;
352 uint32_t imm3 = (instr >> 12) & 7;
353 uint32_t Rd = (instr >> 8) & 0xF;
354 uint32_t imm8 = instr & 0xFF;
355 int32_t imm32 = (i << 12) | (imm3 << 8) | imm8;
356 switch (op3) {
357 case 0x0: os << "AND"; break;
358 case 0x1: os << "BIC"; break;
359 case 0x2: os << "ORR"; break;
360 case 0x3: os << "ORN"; break;
361 case 0x4: os << "EOR"; break;
362 case 0x8: os << "ADD"; break;
363 case 0xA: os << "ADC"; break;
364 case 0xB: os << "SBC"; break;
365 case 0xD: os << "SUB"; break;
366 case 0xE: os << "RSB"; break;
367 default: os << "UNKNOWN DPMI-" << op3; break;
368 }
369 if (S == 1) {
370 os << "S ";
371 } else {
372 os << " ";
373 }
374 DumpReg(os, Rd);
375 os << ", ";
376 DumpReg(os, Rn);
Elliott Hughes77405792012-03-15 15:22:12 -0700377 os << ", ThumbExpand(" << imm32 << ")";
Ian Rogers40627db2012-03-04 17:31:09 -0800378 } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
379 // Data-processing (plain binary immediate)
380 // |111|11|10|00000|0000|1|111110000000000|
381 // |5 3|21|09|87654|3 0|5|4 0 5 0|
382 // |---|--|--|-----|----|-|---------------|
383 // |332|22|22|22222|1111|1|111110000000000|
384 // |1 9|87|65|43210|9 6|5|4 0 5 0|
385 // |---|--|--|-----|----|-|---------------|
386 // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
387 uint32_t op3 = (instr >> 20) & 0x1F;
388 uint32_t Rn = (instr >> 16) & 0xF;
389 switch (op3) {
390 case 0x04: {
391 // MOVW Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
392 uint32_t Rd = (instr >> 8) & 0xF;
393 uint32_t i = (instr >> 26) & 1;
394 uint32_t imm3 = (instr >> 12) & 0x7;
395 uint32_t imm8 = instr & 0xFF;
396 uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
397 os << "MOVW ";
398 DumpReg(os, Rd);
Elliott Hughes77405792012-03-15 15:22:12 -0700399 os << ", #" << imm16;
Ian Rogers40627db2012-03-04 17:31:09 -0800400 break;
401 }
402 case 0x0A: {
403 // SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
404 uint32_t Rd = (instr >> 8) & 0xF;
405 uint32_t i = (instr >> 26) & 1;
406 uint32_t imm3 = (instr >> 12) & 0x7;
407 uint32_t imm8 = instr & 0xFF;
408 uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
409 os << "SUB.W ";
410 DumpReg(os, Rd);
411 os << ", ";
412 DumpReg(os, Rn);
Elliott Hughes77405792012-03-15 15:22:12 -0700413 os << ", #" << imm12;
Ian Rogers40627db2012-03-04 17:31:09 -0800414 break;
415 }
416 default:
417 break;
418 }
419 } else {
420 // Branches and miscellaneous control
421 // |111|11|1000000|0000|1|111|1100|00000000|
422 // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
423 // |---|--|-------|----|-|---|----|--------|
424 // |332|22|2222222|1111|1|111|1100|00000000|
425 // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
426 // |---|--|-------|----|-|---|----|--------|
427 // |111|10| op2 | |1|op3|op4 | |
428
429 uint32_t op3 = (instr >> 12) & 7;
430 //uint32_t op4 = (instr >> 8) & 0xF;
431 switch (op3) {
432 case 0:
433 if ((op2 & 0x38) != 0x38) {
434 // Conditional branch
435 // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
436 // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
437 // |---|--|-|----|------|-|-|--|-|--|-----------|
438 // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
439 // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
440 // |---|--|-|----|------|-|-|--|-|--|-----------|
441 // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
442 uint32_t S = (instr >> 26) & 1;
443 uint32_t J2 = (instr >> 11) & 1;
444 uint32_t J1 = (instr >> 13) & 1;
445 uint32_t imm6 = (instr >> 16) & 0x3F;
446 uint32_t imm11 = instr & 0x7FF;
447 uint32_t cond = (instr >> 22) & 0xF;
448 int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
449 imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
450 os << "B";
451 DumpCond(os, cond);
452 os << ".W ";
453 DumpBranchTarget(os, instr_ptr + 4, imm32);
Ian Rogers40627db2012-03-04 17:31:09 -0800454 }
455 break;
456 case 2:
457 case 1: case 3:
458 break;
459 case 4: case 6: case 5: case 7: {
460 // BL, BLX (immediate)
461 // |111|11|1|0000000000|11|1 |1|1 |10000000000|
462 // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
463 // |---|--|-|----------|--|--|-|--|-----------|
464 // |332|22|2|2222221111|11|1 |1|1 |10000000000|
465 // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
466 // |---|--|-|----------|--|--|-|--|-----------|
467 // |111|10|S| imm10 |11|J1|L|J2| imm11 |
468 uint32_t S = (instr >> 26) & 1;
469 uint32_t J2 = (instr >> 11) & 1;
470 uint32_t L = (instr >> 12) & 1;
471 uint32_t J1 = (instr >> 13) & 1;
472 uint32_t imm10 = (instr >> 16) & 0x3FF;
473 uint32_t imm11 = instr & 0x7FF;
474 if (L == 0) {
475 os << "BX ";
476 } else {
477 os << "BLX ";
478 }
479 uint32_t I1 = ~(J1 ^ S);
480 uint32_t I2 = ~(J2 ^ S);
481 int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
482 imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
483 DumpBranchTarget(os, instr_ptr + 4, imm32);
484 break;
485 }
486 }
487 }
488 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800489 case 3:
490 switch (op2) {
491 case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0
492 case 0x08: case 0x0A: case 0x0C: case 0x0E: {
493 // Store single data item
Ian Rogers40627db2012-03-04 17:31:09 -0800494 // |111|11|100|000|0|0000|1111|110000|000000|
495 // |5 3|21|098|765|4|3 0|5 2|10 6|5 0|
496 // |---|--|---|---|-|----|----|------|------|
497 // |332|22|222|222|2|1111|1111|110000|000000|
498 // |1 9|87|654|321|0|9 6|5 2|10 6|5 0|
499 // |---|--|---|---|-|----|----|------|------|
500 // |111|11|000|op3|0| | | op4 | |
501
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 uint32_t op3 = (instr >> 21) & 7;
503 //uint32_t op4 = (instr >> 6) & 0x3F;
504 switch (op3) {
505 case 0x2: case 0x6: {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii
507 // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii
508 uint32_t Rn = (instr >> 16) & 0xF;
509 uint32_t Rt = (instr >> 12) & 0xF;
Ian Rogers40627db2012-03-04 17:31:09 -0800510 if (op3 == 2) {
511 uint32_t P = (instr >> 10) & 1;
512 uint32_t U = (instr >> 9) & 1;
513 uint32_t W = (instr >> 8) & 1;
514 uint32_t imm8 = instr & 0xFF;
515 int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8
516 if (Rn == 13 && P == 1 && U == 0 && W == 1) {
517 os << "PUSH ";
518 DumpReg(os, Rt);
Ian Rogers40627db2012-03-04 17:31:09 -0800519 } else if (Rn == 15 || (P == 0 && W == 0)) {
520 os << "UNDEFINED ";
521 } else {
522 if (P == 1 && U == 1 && W == 0) {
523 os << "STRT ";
524 } else {
525 os << "STR ";
526 }
527 DumpReg(os, Rt);
528 os << ", [";
529 DumpReg(os, Rn);
530 if (P == 0 && W == 1) {
531 os << "], #" << imm32;
532 } else {
533 os << ", #" << imm32 << "]";
534 if (W == 1) {
535 os << "!";
536 }
537 }
Ian Rogers40627db2012-03-04 17:31:09 -0800538 }
539 } else if (op3 == 6) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 uint32_t imm12 = instr & 0xFFF;
541 os << "STR.W ";
542 DumpReg(os, Rt);
543 os << ", [";
544 DumpReg(os, Rn);
Elliott Hughes77405792012-03-15 15:22:12 -0700545 os << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800546 }
Ian Rogers40627db2012-03-04 17:31:09 -0800547 break;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800548 }
549 }
550
551 break;
552 }
553 case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101
554 // Load word
555 // |111|11|10|0 0|00|0|0000|1111|110000|000000|
556 // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0|
557 // |---|--|--|---|--|-|----|----|------|------|
558 // |332|22|22|2 2|22|2|1111|1111|110000|000000|
559 // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0|
560 // |---|--|--|---|--|-|----|----|------|------|
561 // |111|11|00|op3|10|1| Rn | Rt | op4 | |
562 // |111|11| op2 | | | imm12 |
563 uint32_t op3 = (instr >> 23) & 3;
564 uint32_t op4 = (instr >> 6) & 0x3F;
565 uint32_t Rn = (instr >> 16) & 0xF;
566 uint32_t Rt = (instr >> 12) & 0xF;
567 if (op3 == 1 || Rn == 15) {
568 // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii
569 // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii
570 uint32_t imm12 = instr & 0xFFF;
571 os << "LDR.W ";
572 DumpReg(os, Rt);
573 os << ", [";
574 DumpReg(os, Rn);
Elliott Hughes77405792012-03-15 15:22:12 -0700575 os << ", #" << imm12 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800576 } else if (op4 == 0) {
577 // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm
578 uint32_t imm2 = (instr >> 4) & 0xF;
Elliott Hughes77405792012-03-15 15:22:12 -0700579 uint32_t rm = instr & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800580 os << "LDR.W ";
581 DumpReg(os, Rt);
582 os << ", [";
583 DumpReg(os, Rn);
584 os << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700585 DumpReg(os, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800586 if (imm2 != 0) {
587 os << ", LSL #" << imm2;
588 }
Elliott Hughes77405792012-03-15 15:22:12 -0700589 os << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800590 } else {
591 // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii
592 uint32_t imm8 = instr & 0xFF;
593 os << "LDRT ";
594 DumpReg(os, Rt);
595 os << ", [";
596 DumpReg(os, Rn);
Elliott Hughes77405792012-03-15 15:22:12 -0700597 os << ", #" << imm8 << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 }
599 break;
600 }
601 }
602 default:
603 break;
604 }
Elliott Hughes77405792012-03-15 15:22:12 -0700605 os << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800606 return 4;
607}
608
609size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) {
610 uint16_t instr = ReadU16(instr_ptr);
611 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
612 if (is_32bit) {
613 return DumpThumb32(os, instr_ptr);
614 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700615 os << StringPrintf("\t\t\t%p: %04x : ", instr_ptr, instr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800616 uint16_t opcode1 = instr >> 10;
617 if (opcode1 < 0x10) {
618 // shift (immediate), add, subtract, move, and compare
619 uint16_t opcode2 = instr >> 9;
620 switch (opcode2) {
621 case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
622 case 0x8: case 0x9: case 0xA: case 0xB: {
623 // Logical shift left - 00 000xx xxxxxxxxx
624 // Logical shift right - 00 001xx xxxxxxxxx
625 // Arithmetic shift right - 00 010xx xxxxxxxxx
626 uint16_t imm5 = (instr >> 6) & 0x1F;
Elliott Hughes77405792012-03-15 15:22:12 -0700627 uint16_t rm = (instr >> 3) & 7;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800628 uint16_t Rd = instr & 7;
629 if (opcode2 <= 3) {
630 os << "LSLS ";
631 } else if (opcode2 <= 7) {
632 os << "LSRS ";
633 } else {
634 os << "ASRS ";
635 }
636 DumpReg(os, Rd);
637 os << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700638 DumpReg(os, rm);
639 os << ", #" << imm5;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800640 break;
641 }
642 case 0xC: case 0xD: case 0xE: case 0xF: {
643 // Add register - 00 01100 mmm nnn ddd
644 // Sub register - 00 01101 mmm nnn ddd
645 // Add 3-bit immediate - 00 01110 iii nnn ddd
646 // Sub 3-bit immediate - 00 01111 iii nnn ddd
647 uint16_t imm3_or_Rm = (instr >> 6) & 7;
648 uint16_t Rn = (instr >> 3) & 7;
649 uint16_t Rd = instr & 7;
650 if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) {
651 os << "MOV ";
652 } else {
653 if ((opcode2 & 1) == 0) {
654 os << "ADDS ";
655 } else {
656 os << "SUBS ";
657 }
658 }
659 DumpReg(os, Rd);
660 os << ", ";
661 DumpReg(os, Rn);
662 if ((opcode2 & 2) == 0) {
663 os << ", ";
664 DumpReg(os, imm3_or_Rm);
665 } else if (imm3_or_Rm != 0) {
666 os << ", #" << imm3_or_Rm;
667 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800668 break;
669 }
670 case 0x10: case 0x11: case 0x12: case 0x13:
671 case 0x14: case 0x15: case 0x16: case 0x17:
672 case 0x18: case 0x19: case 0x1A: case 0x1B:
673 case 0x1C: case 0x1D: case 0x1E: case 0x1F: {
674 // MOVS Rd, #imm8 - 00100 ddd iiiiiiii
675 // CMP Rn, #imm8 - 00101 nnn iiiiiiii
676 // ADDS Rn, #imm8 - 00110 nnn iiiiiiii
677 // SUBS Rn, #imm8 - 00111 nnn iiiiiiii
678 uint16_t Rn = (instr >> 8) & 7;
679 uint16_t imm8 = instr & 0xFF;
680 switch (opcode2 >> 2) {
681 case 4: os << "MOVS "; break;
682 case 5: os << "CMP "; break;
683 case 6: os << "ADDS "; break;
684 case 7: os << "SUBS "; break;
685 }
686 DumpReg(os, Rn);
Elliott Hughes77405792012-03-15 15:22:12 -0700687 os << ", #" << imm8;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800688 break;
689 }
690 default:
691 break;
692 }
693 } else if (opcode1 == 0x11) {
694 // Special data instructions and branch and exchange
695 uint16_t opcode2 = (instr >> 6) & 0x0F;
696 switch (opcode2) {
697 case 0x0: case 0x1: case 0x2: case 0x3: {
698 // Add low registers - 010001 0000 xxxxxx
699 // Add high registers - 010001 0001/001x xxxxxx
700 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes77405792012-03-15 15:22:12 -0700701 uint16_t rm = (instr >> 3) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800702 uint16_t Rdn = instr & 7;
703 uint16_t DN_Rdn = (DN << 3) | Rdn;
704 os << "ADD ";
705 DumpReg(os, DN_Rdn);
706 os << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700707 DumpReg(os, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800708 break;
709 }
710 case 0x8: case 0x9: case 0xA: case 0xB: {
711 // Move low registers - 010001 1000 xxxxxx
712 // Move high registers - 010001 1001/101x xxxxxx
713 uint16_t DN = (instr >> 7) & 1;
Elliott Hughes77405792012-03-15 15:22:12 -0700714 uint16_t rm = (instr >> 3) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800715 uint16_t Rdn = instr & 7;
716 uint16_t DN_Rdn = (DN << 3) | Rdn;
717 os << "MOV ";
718 DumpReg(os, DN_Rdn);
719 os << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700720 DumpReg(os, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800721 break;
722 }
723 case 0x5: case 0x6: case 0x7: {
724 // Compare high registers - 010001 0101/011x xxxxxx
725 uint16_t N = (instr >> 7) & 1;
Elliott Hughes77405792012-03-15 15:22:12 -0700726 uint16_t rm = (instr >> 3) & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800727 uint16_t Rn = instr & 7;
728 uint16_t N_Rn = (N << 3) | Rn;
729 os << "CMP ";
730 DumpReg(os, N_Rn);
731 os << ", ";
Elliott Hughes77405792012-03-15 15:22:12 -0700732 DumpReg(os, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800733 break;
734 }
735 case 0xC: case 0xD: case 0xE: case 0xF: {
736 // Branch and exchange - 010001 110x xxxxxx
737 // Branch with link and exchange - 010001 111x xxxxxx
Elliott Hughes77405792012-03-15 15:22:12 -0700738 uint16_t rm = instr >> 3 & 0xF;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800739 if ((opcode2 & 0x2) == 0) {
740 os << "BX ";
741 } else {
742 os << "BLX ";
743 }
Elliott Hughes77405792012-03-15 15:22:12 -0700744 DumpReg(os, rm);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800745 break;
746 }
747 default:
748 break;
749 }
750 } else if ((instr & 0xF000) == 0xB000) {
751 // Miscellaneous 16-bit instructions
752 uint16_t opcode2 = (instr >> 5) & 0x7F;
753 switch (opcode2) {
754 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: {
755 // Add immediate to SP - 1011 00000 ii iiiii
756 // Subtract immediate from SP - 1011 00001 ii iiiii
757 int imm7 = instr & 0x7F;
758 if ((opcode2 & 4) == 0) {
759 os << "ADD SP, SP, #";
760 } else {
761 os << "SUB SP, SP, #";
762 }
Elliott Hughes77405792012-03-15 15:22:12 -0700763 os << (imm7 << 2);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800764 break;
765 }
Ian Rogers40627db2012-03-04 17:31:09 -0800766 case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx
767 case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
768 // If-Then, and hints
769 uint16_t opA = (instr >> 4) & 0xF;
770 uint16_t opB = instr & 0xF;
771 if (opB == 0) {
772 switch (opA) {
773 case 0: os << "NOP // "; break;
774 case 1: os << "YIELD // "; break;
775 case 2: os << "WFE // "; break;
776 case 3: os << "SEV // "; break;
777 default: break;
778 }
779 } else {
780 os << "IT " << reinterpret_cast<void*>(opB) << " ";
781 DumpCond(os, opA);
Ian Rogers40627db2012-03-04 17:31:09 -0800782 }
783 break;
784 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800785 default:
786 break;
787 }
788 } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) ||
789 ((instr & 0xE000) == 0x8000)) {
790 // Load/store single data item
791 uint16_t opA = instr >> 12;
792 //uint16_t opB = (instr >> 9) & 7;
793 switch (opA) {
794 case 0x6: {
795 // STR Rt, Rn, #imm - 01100 iiiii nnn ttt
796 // LDR Rt, Rn, #imm - 01101 iiiii nnn ttt
797 uint16_t imm5 = (instr >> 6) & 0x1F;
798 uint16_t Rn = (instr >> 3) & 7;
799 uint16_t Rt = instr & 7;
800 if ((instr & 0x800) == 0) {
801 os << "STR ";
802 } else {
803 os << "LDR ";
804 }
805 DumpReg(os, Rt);
806 os << ", [";
807 DumpReg(os, Rn);
Elliott Hughes77405792012-03-15 15:22:12 -0700808 os << ", #" << (imm5 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800809 break;
810 }
811 case 0x9: {
812 // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii
813 // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii
814 uint16_t imm8 = instr & 0xFF;
815 uint16_t Rt = (instr >> 8) & 7;
816 if ((instr & 0x800) == 0) {
817 os << "STR ";
818 } else {
819 os << "LDR ";
820 }
821 DumpReg(os, Rt);
Elliott Hughes77405792012-03-15 15:22:12 -0700822 os << ", [SP, #" << (imm8 << 2) << "]";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800823 break;
824 }
825 default:
826 break;
827 }
Ian Rogers40627db2012-03-04 17:31:09 -0800828 } else if (opcode1 == 0x38 || opcode1 == 0x39) {
829 uint16_t imm11 = instr & 0x7FFF;
830 int32_t imm32 = imm11 << 1;
831 imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate
832 os << "B ";
833 DumpBranchTarget(os, instr_ptr + 4, imm32);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800834 }
Elliott Hughes77405792012-03-15 15:22:12 -0700835 os << '\n';
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800836 }
837 return 2;
838}
839
840} // namespace arm
841} // namespace art