in disassembler/disassembler_arm.cc [484:1595]
size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) {
uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2);
// |111|1 1|1000000|0000|1111110000000000|
// |5 3|2 1|0987654|3 0|5 0 5 0|
// |---|---|-------|----|----------------|
// |332|2 2|2222222|1111|1111110000000000|
// |1 9|8 7|6543210|9 6|5 0 5 0|
// |---|---|-------|----|----------------|
// |111|op1| op2 | | |
uint32_t op1 = (instr >> 27) & 3;
if (op1 == 0) {
return DumpThumb16(os, instr_ptr);
}
// Set valid address range of backing buffer.
const uintptr_t lo_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->base_address_);
const uintptr_t hi_adr = reinterpret_cast<intptr_t>(GetDisassemblerOptions()->end_address_);
uint32_t op2 = (instr >> 20) & 0x7F;
std::ostringstream opcode;
std::ostringstream args;
switch (op1) {
case 0:
break;
case 1:
if ((op2 & 0x64) == 0) { // 00x x0xx
// |111|11|10|00|0|00|0000|1111110000000000|
// |5 3|21|09|87|6|54|3 0|5 0 5 0|
// |---|--|--|--|-|--|----|----------------|
// |332|22|22|22|2|22|1111|1111110000000000|
// |1 9|87|65|43|2|10|9 6|5 0 5 0|
// |---|--|--|--|-|--|----|----------------|
// |111|01|00|op|0|WL| Rn | |
// |111|01| op2 | | |
// STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr
// LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr
// PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr
// POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr
uint32_t op = (instr >> 23) & 3;
uint32_t W = (instr >> 21) & 1;
uint32_t L = (instr >> 20) & 1;
ArmRegister Rn(instr, 16);
if (op == 1 || op == 2) {
if (op == 1) {
if (L == 0) {
opcode << "stm";
args << Rn << (W == 0 ? "" : "!") << ", ";
} else {
if (Rn.r != 13) {
opcode << "ldm";
args << Rn << (W == 0 ? "" : "!") << ", ";
} else {
opcode << "pop";
}
}
} else {
if (L == 0) {
if (Rn.r != 13) {
opcode << "stmdb";
args << Rn << (W == 0 ? "" : "!") << ", ";
} else {
opcode << "push";
}
} else {
opcode << "ldmdb";
args << Rn << (W == 0 ? "" : "!") << ", ";
}
}
args << RegisterList(instr);
}
} else if ((op2 & 0x64) == 4) { // 00x x1xx
uint32_t op3 = (instr >> 23) & 3;
uint32_t op4 = (instr >> 20) & 3;
// uint32_t op5 = (instr >> 4) & 0xF;
ArmRegister Rn(instr, 16);
ArmRegister Rt(instr, 12);
ArmRegister Rd(instr, 8);
uint32_t imm8 = instr & 0xFF;
if ((op3 & 2) == 2) { // 1x
int W = (instr >> 21) & 1;
int U = (instr >> 23) & 1;
int P = (instr >> 24) & 1;
if ((op4 & 1) == 1) {
opcode << "ldrd";
} else {
opcode << "strd";
}
args << Rt << "," << Rd << ", [" << Rn;
const char *sign = U ? "+" : "-";
if (P == 0 && W == 1) {
args << "], #" << sign << (imm8 << 2);
} else {
args << ", #" << sign << (imm8 << 2) << "]";
if (W == 1) {
args << "!";
}
}
} else { // 0x
switch (op4) {
case 0:
if (op3 == 0) { // op3 is 00, op4 is 00
opcode << "strex";
args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
Rd.r == Rn.r || Rd.r == Rt.r) {
args << " (UNPREDICTABLE)";
}
} else { // op3 is 01, op4 is 00
// this is one of strexb, strexh or strexd
int op5 = (instr >> 4) & 0xf;
switch (op5) {
case 4:
case 5:
opcode << ((op5 == 4) ? "strexb" : "strexh");
Rd = ArmRegister(instr, 0);
args << Rd << ", " << Rt << ", [" << Rn << "]";
if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 || Rn.r == 15 ||
Rd.r == Rn.r || Rd.r == Rt.r || (instr & 0xf00) != 0xf00) {
args << " (UNPREDICTABLE)";
}
break;
case 7:
opcode << "strexd";
ArmRegister Rt2 = Rd;
Rd = ArmRegister(instr, 0);
args << Rd << ", " << Rt << ", " << Rt2 << ", [" << Rn << "]";
if (Rd.r == 13 || Rd.r == 15 || Rt.r == 13 || Rt.r == 15 ||
Rt2.r == 13 || Rt2.r == 15 || Rn.r == 15 ||
Rd.r == Rn.r || Rd.r == Rt.r || Rd.r == Rt2.r) {
args << " (UNPREDICTABLE)";
}
break;
}
}
break;
case 1:
if (op3 == 0) { // op3 is 00, op4 is 01
opcode << "ldrex";
args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]";
if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf00) != 0xf00) {
args << " (UNPREDICTABLE)";
}
} else { // op3 is 01, op4 is 01
// this is one of strexb, strexh or strexd
int op5 = (instr >> 4) & 0xf;
switch (op5) {
case 0:
opcode << "tbb";
break;
case 1:
opcode << "tbh";
break;
case 4:
case 5:
opcode << ((op5 == 4) ? "ldrexb" : "ldrexh");
args << Rt << ", [" << Rn << "]";
if (Rt.r == 13 || Rt.r == 15 || Rn.r == 15 || (instr & 0xf0f) != 0xf0f) {
args << " (UNPREDICTABLE)";
}
break;
case 7:
opcode << "ldrexd";
args << Rt << ", " << Rd /* Rt2 */ << ", [" << Rn << "]";
if (Rt.r == 13 || Rt.r == 15 || Rd.r == 13 /* Rt2 */ || Rd.r == 15 /* Rt2 */ ||
Rn.r == 15 || (instr & 0x00f) != 0x00f) {
args << " (UNPREDICTABLE)";
}
break;
}
}
break;
case 2: // op3 is 0x, op4 is 10
case 3: // op3 is 0x, op4 is 11
if (op4 == 2) {
opcode << "strd";
} else {
opcode << "ldrd";
}
int W = (instr >> 21) & 1;
int U = (instr >> 23) & 1;
int P = (instr >> 24) & 1;
args << Rt << "," << Rd << ", [" << Rn;
const char *sign = U ? "+" : "-";
if (P == 0 && W == 1) {
args << "], #" << sign << imm8;
} else {
args << ", #" << sign << imm8 << "]";
if (W == 1) {
args << "!";
}
}
break;
}
}
} else if ((op2 & 0x60) == 0x20) { // 01x xxxx
// Data-processing (shifted register)
// |111|1110|0000|0|0000|1111|1100|00|00|0000|
// |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0|
// |---|----|----|-|----|----|----|--|--|----|
// |332|2222|2222|2|1111|1111|1100|00|00|0000|
// |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0|
// |---|----|----|-|----|----|----|--|--|----|
// |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm |
uint32_t op3 = (instr >> 21) & 0xF;
uint32_t S = (instr >> 20) & 1;
uint32_t imm3 = ((instr >> 12) & 0x7);
uint32_t imm2 = ((instr >> 6) & 0x3);
uint32_t imm5 = ((imm3 << 2) | imm2);
uint32_t shift_type = ((instr >> 4) & 0x3);
ArmRegister Rd(instr, 8);
ArmRegister Rn(instr, 16);
ArmRegister Rm(instr, 0);
switch (op3) {
case 0x0:
if (Rd.r != 0xF) {
opcode << "and";
} else {
if (S != 1U) {
opcode << "UNKNOWN TST-" << S;
break;
}
opcode << "tst";
S = 0; // don't print 's'
}
break;
case 0x1: opcode << "bic"; break;
case 0x2:
if (Rn.r != 0xF) {
opcode << "orr";
} else {
// TODO: use canonical form if there is a shift (lsl, ...).
opcode << "mov";
}
break;
case 0x3:
if (Rn.r != 0xF) {
opcode << "orn";
} else {
opcode << "mvn";
}
break;
case 0x4:
if (Rd.r != 0xF) {
opcode << "eor";
} else {
if (S != 1U) {
opcode << "UNKNOWN TEQ-" << S;
break;
}
opcode << "teq";
S = 0; // don't print 's'
}
break;
case 0x6: opcode << "pkh"; break;
case 0x8:
if (Rd.r != 0xF) {
opcode << "add";
} else {
if (S != 1U) {
opcode << "UNKNOWN CMN-" << S;
break;
}
opcode << "cmn";
S = 0; // don't print 's'
}
break;
case 0xA: opcode << "adc"; break;
case 0xB: opcode << "sbc"; break;
case 0xD:
if (Rd.r != 0xF) {
opcode << "sub";
} else {
if (S != 1U) {
opcode << "UNKNOWN CMP-" << S;
break;
}
opcode << "cmp";
S = 0; // don't print 's'
}
break;
case 0xE: opcode << "rsb"; break;
default: opcode << "UNKNOWN DPSR-" << op3; break;
}
if (S == 1) {
opcode << "s";
}
opcode << ".w";
if (Rd.r != 0xF) {
args << Rd << ", ";
}
if (Rn.r != 0xF) {
args << Rn << ", ";
}
args << Rm;
// Shift operand.
bool noShift = (imm5 == 0 && shift_type != 0x3);
if (!noShift) {
args << ", ";
switch (shift_type) {
case 0x0: args << "lsl"; break;
case 0x1: args << "lsr"; break;
case 0x2: args << "asr"; break;
case 0x3:
if (imm5 == 0) {
args << "rrx";
} else {
args << "ror #" << imm5;
}
break;
}
if (shift_type != 0x3 /* rrx */) {
args << StringPrintf(" #%d", (0 != imm5 || 0 == shift_type) ? imm5 : 32);
}
}
} else if ((op2 & 0x40) == 0x40) { // 1xx xxxx
// Co-processor instructions
// |111|1|11|000000|0000|1111|1100|000|0 |0000|
// |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0|
// |---|-|--|------|----|----|----|---|---|----|
// |332|2|22|222222|1111|1111|1100|000|0 |0000|
// |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0|
// |---|-|--|------|----|----|----|---|---|----|
// |111| |11| op3 | Rn | |copr| |op4| |
uint32_t op3 = (instr >> 20) & 0x3F;
uint32_t coproc = (instr >> 8) & 0xF;
uint32_t op4 = (instr >> 4) & 0x1;
if (coproc == 0xA || coproc == 0xB) { // 101x
if (op3 < 0x20 && (op3 & ~5) != 0) { // 0xxxxx and not 000x0x
// Extension register load/store instructions
// |1111|110|00000|0000|1111|110|0|00000000|
// |5 2|1 9|87654|3 0|5 2|1 9|8|7 0|
// |----|---|-----|----|----|---|-|--------|
// |3322|222|22222|1111|1111|110|0|00000000|
// |1 8|7 5|4 0|9 6|5 2|1 9|8|7 0|
// |----|---|-----|----|----|---|-|--------|
// |1110|110|PUDWL| Rn | Vd |101|S| imm8 |
uint32_t P = (instr >> 24) & 1;
uint32_t U = (instr >> 23) & 1;
uint32_t W = (instr >> 21) & 1;
if (P == U && W == 1) {
opcode << "UNDEFINED";
} else {
uint32_t L = (instr >> 20) & 1;
uint32_t S = (instr >> 8) & 1;
ArmRegister Rn(instr, 16);
if (P == 1 && W == 0) { // VLDR
FpRegister d(instr, 12, 22);
uint32_t imm8 = instr & 0xFF;
opcode << (L == 1 ? "vldr" : "vstr");
args << d << ", [" << Rn << ", #" << ((U == 1) ? "" : "-")
<< (imm8 << 2) << "]";
if (Rn.r == 15 && U == 1) {
DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm8 << 2, kT2LitHexLong);
}
} else if (Rn.r == 13 && W == 1 && U == L) { // VPUSH/VPOP
opcode << (L == 1 ? "vpop" : "vpush");
args << FpRegisterRange(instr);
} else { // VLDM
opcode << (L == 1 ? "vldm" : "vstm");
args << Rn << ((W == 1) ? "!" : "") << ", "
<< FpRegisterRange(instr);
}
opcode << (S == 1 ? ".f64" : ".f32");
}
} else if ((op3 >> 1) == 2) { // 00010x
if ((instr & 0xD0) == 0x10) {
// 64bit transfers between ARM core and extension registers.
uint32_t L = (instr >> 20) & 1;
uint32_t S = (instr >> 8) & 1;
ArmRegister Rt2(instr, 16);
ArmRegister Rt(instr, 12);
FpRegister m(instr, 0, 5);
opcode << "vmov" << (S ? ".f64" : ".f32");
if (L == 1) {
args << Rt << ", " << Rt2 << ", ";
}
if (S) {
args << m;
} else {
args << m << ", " << FpRegister(m, 1);
}
if (L == 0) {
args << ", " << Rt << ", " << Rt2;
}
if (Rt.r == 15 || Rt.r == 13 || Rt2.r == 15 || Rt2.r == 13 ||
(S == 0 && m.r == 31) || (L == 1 && Rt.r == Rt2.r)) {
args << " (UNPREDICTABLE)";
}
}
} else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0
// fp data processing
// VMLA, VMLS, VMUL, VNMUL, VADD, VSUB, VDIV, VMOV, ...
// |1111|1100|0|0|00|0000|1111|110|0|0|0|0|0|0000|
// |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7|6|5|4|3 0|
// |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
// |3322|2222|2|2|22|1111|1111|110|0|0|0|0|0|0000|
// |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7|6|5|4|3 0|
// |----|----|-|-|--|----|----|---|-|-|-|-|-|----|
// |1110|1110| op3 | Vn | Vd |101|S|N|Q|M|0| Vm |
// |1110|1110|0|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VMLA
// |1110|1110|0|D|00| Vn | Vd |101|S|N|1|M|0| Vm | VMLS
// |1110|1110|0|D|10| Vn | Vd |101|S|N|0|M|0| Vm | VMUL
// |1110|1110|0|D|10| Vn | Vd |101|S|N|1|M|0| Vm | VNMUL
// |1110|1110|0|D|11| Vn | Vd |101|S|N|0|M|0| Vm | VADD
// |1110|1110|0|D|11| Vn | Vd |101|S|N|1|M|0| Vm | VSUB
// |1110|1110|1|D|00| Vn | Vd |101|S|N|0|M|0| Vm | VDIV
// |1110|1110|1|D|11| iH | Vd |101|S|0|0|0|0| iL | VMOV (imm)
// |1110|1110|1|D|11|op5 | Vd |101|S|.|1|M|0| Vm | ... (see below)
uint32_t S = (instr >> 8) & 1;
uint32_t Q = (instr >> 6) & 1;
FpRegister d(instr, 12, 22);
FpRegister n(instr, 16, 7);
FpRegister m(instr, 0, 5);
if ((op3 & 0xB) == 0) { // 100x00
opcode << (Q == 0 ? "vmla" : "vmls") << (S != 0 ? ".f64" : ".f32");
args << d << ", " << n << ", " << m;
} else if ((op3 & 0xB) == 0x2) { // 100x10
opcode << (Q == 0 ? "vmul" : "vnmul") << (S != 0 ? ".f64" : ".f32");
args << d << ", " << n << ", " << m;
} else if ((op3 & 0xB) == 0x3) { // 100x11
opcode << (Q == 0 ? "vadd" : "vsub") << (S != 0 ? ".f64" : ".f32");
args << d << ", " << n << ", " << m;
} else if ((op3 & 0xB) == 0x8 && Q == 0) { // 101x00, Q == 0
opcode << "vdiv" << (S != 0 ? ".f64" : ".f32");
args << d << ", " << n << ", " << m;
} else if ((op3 & 0xB) == 0xB && Q == 0) { // 101x11, Q == 0
uint32_t imm8 = ((instr & 0xf0000u) >> 12) | (instr & 0xfu);
opcode << "vmov" << (S != 0 ? ".f64" : ".f32");
args << d << ", " << (S != 0 ? StringPrintf("0x%016" PRIx64, VFPExpand64(imm8))
: StringPrintf("0x%08x", VFPExpand32(imm8)));
if ((instr & 0xa0) != 0) {
args << " (UNPREDICTABLE)";
}
} else if ((op3 & 0xB) == 0xB && Q == 1) { // 101x11, Q == 1
// VNEG, VSQRT, VCMP, VCMPE, VCVT (floating-point conversion)
// |1111|1100|0|0|00|0000|1111|110|0|0 |0|0|0|0000|
// |5 2|1 8|7|6|54|3 0|5 2|1 9|8|7 |6|5|4|3 0|
// |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
// |3322|2222|2|2|22|1111|1111|110|0|0 |0|0|0|0000|
// |1 8|7 4|3|2|10|9 6|5 2|1 9|8|7 |6|5|4|3 0|
// |----|----|-|-|--|----|----|---|-|- |-|-|-|----|
// |1110|1110|1|D|11|0000| Vd |101|S|0 |1|M|0| Vm | VMOV (reg)
// |1110|1110|1|D|11|0000| Vd |101|S|1 |1|M|0| Vm | VABS
// |1110|1110|1|D|11|0001| Vd |101|S|0 |1|M|0| Vm | VNEG
// |1110|1110|1|D|11|0001| Vd |101|S|1 |1|M|0| Vm | VSQRT
// |1110|1110|1|D|11|0100| Vd |101|S|op|1|M|0| Vm | VCMP
// |1110|1110|1|D|11|0101| Vd |101|S|op|1|0|0|0000| VCMPE
// |1110|1110|1|D|11|op5 | Vd |101|S|op|1|M|0| Vm | VCVT
uint32_t op5 = (instr >> 16) & 0xF;
uint32_t op = (instr >> 7) & 1;
// Register types in VCVT instructions rely on the combination of op5 and S.
FpRegister Dd(instr, 12, 22, 1);
FpRegister Sd(instr, 12, 22, 0);
FpRegister Dm(instr, 0, 5, 1);
FpRegister Sm(instr, 0, 5, 0);
if (op5 == 0) {
opcode << (op == 0 ? "vmov" : "vabs") << (S != 0 ? ".f64" : ".f32");
args << d << ", " << m;
} else if (op5 == 1) {
opcode << (op != 0 ? "vsqrt" : "vneg") << (S != 0 ? ".f64" : ".f32");
args << d << ", " << m;
} else if (op5 == 4) {
opcode << "vcmp" << (S != 0 ? ".f64" : ".f32");
args << d << ", " << m;
if (op != 0) {
args << " (quiet nan)";
}
} else if (op5 == 5) {
opcode << "vcmpe" << (S != 0 ? ".f64" : ".f32");
args << d << ", #0.0";
if (op != 0) {
args << " (quiet nan)";
}
if ((instr & 0x2f) != 0) {
args << " (UNPREDICTABLE)";
}
} else if (op5 == 0xD) {
if (S == 1) {
// vcvt{r}.s32.f64
opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f64";
args << Sd << ", " << Dm;
} else {
// vcvt{r}.s32.f32
opcode << "vcvt" << (op == 0 ? "r" : "") << ".s32.f32";
args << Sd << ", " << Sm;
}
} else if (op5 == 0xC) {
if (S == 1) {
// vcvt{r}.u32.f64
opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f64";
args << Sd << ", " << Dm;
} else {
// vcvt{r}.u32.f32
opcode << "vcvt" << (op == 0 ? "r" : "") << ".u32.f32";
args << Sd << ", " << Sm;
}
} else if (op5 == 0x8) {
if (S == 1) {
// vcvt.f64.<Tm>
opcode << "vcvt.f64." << (op == 0 ? "u" : "s") << "32";
args << Dd << ", " << Sm;
} else {
// vcvt.f32.<Tm>
opcode << "vcvt.f32." << (op == 0 ? "u" : "s") << "32";
args << Sd << ", " << Sm;
}
} else if (op5 == 0x7) {
if (op == 1) {
if (S == 1) {
// vcvt.f64.f32
opcode << "vcvt.f64.f32";
args << Dd << ", " << Sm;
} else {
// vcvt.f32.f64
opcode << "vcvt.f32.f64";
args << Sd << ", " << Dm;
}
}
} else if ((op5 & 0xa) == 0xa) {
opcode << "vcvt";
args << "[undecoded: floating <-> fixed]";
}
}
} else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1
if (coproc == 10 && (op3 & 0xE) == 0) {
// VMOV (between ARM core register and single-precision register)
// |1111|1100|000|0 |0000|1111|1100|0|00|0|0000|
// |5 |1 8|7 5|4 |3 0|5 2|1 8|7|65|4|3 0|
// |----|----|---|- |----|----|----|-|--|-|----|
// |3322|2222|222|2 |1111|1111|1100|0|00|0|0000|
// |1 8|7 4|3 1|0 |9 6|5 2|1 8|7|65|4|3 0|
// |----|----|---|- |----|----|----|-|--|-|----|
// |1110|1110|000|op| Vn | Rt |1010|N|00|1|0000|
uint32_t op = op3 & 1;
ArmRegister Rt(instr, 12);
FpRegister n(instr, 16, 7);
opcode << "vmov.f32";
if (op) {
args << Rt << ", " << n;
} else {
args << n << ", " << Rt;
}
if (Rt.r == 13 || Rt.r == 15 || (instr & 0x6F) != 0) {
args << " (UNPREDICTABLE)";
}
} else if (coproc == 10 && op3 == 0x2F) {
// VMRS
// |1111|11000000|0000|1111|1100|000|0|0000|
// |5 |1 4|3 0|5 2|1 8|7 5|4|3 0|
// |----|--------|----|----|----|---|-|----|
// |3322|22222222|1111|1111|1100|000|0|0000|
// |1 8|7 0|9 6|5 2|1 8|7 5|4|3 0|
// |----|--------|----|----|----|---|-|----|
// |1110|11101111|reg | Rt |1010|000|1|0000| - last 7 0s are (0)
uint32_t spec_reg = (instr >> 16) & 0xF;
ArmRegister Rt(instr, 12);
opcode << "vmrs";
if (spec_reg == 1) {
if (Rt.r == 15) {
args << "APSR_nzcv, FPSCR";
} else if (Rt.r == 13) {
args << Rt << ", FPSCR (UNPREDICTABLE)";
} else {
args << Rt << ", FPSCR";
}
} else {
args << "(PRIVILEGED)";
}
} else if (coproc == 11 && (op3 & 0x9) != 8) {
// VMOV (ARM core register to scalar or vice versa; 8/16/32-bit)
}
}
}
}
break;
case 2:
if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) {
// Data-processing (modified immediate)
// |111|11|10|0000|0|0000|1|111|1100|00000000|
// |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0|
// |---|--|--|----|-|----|-|---|----|--------|
// |332|22|22|2222|2|1111|1|111|1100|00000000|
// |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0|
// |---|--|--|----|-|----|-|---|----|--------|
// |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii|
// 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx
uint32_t i = (instr >> 26) & 1;
uint32_t op3 = (instr >> 21) & 0xF;
uint32_t S = (instr >> 20) & 1;
ArmRegister Rn(instr, 16);
uint32_t imm3 = (instr >> 12) & 7;
ArmRegister Rd(instr, 8);
uint32_t imm8 = instr & 0xFF;
int32_t imm32 = (i << 11) | (imm3 << 8) | imm8;
if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) {
if (op3 == 0x2) {
opcode << "mov";
if (S == 1) {
opcode << "s";
}
opcode << ".w";
} else {
opcode << "mvn";
if (S == 1) {
opcode << "s";
}
}
args << Rd << ", #" << ThumbExpand(imm32);
} else if (Rd.r == 0xF && S == 1 &&
(op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) {
if (op3 == 0x0) {
opcode << "tst";
} else if (op3 == 0x4) {
opcode << "teq";
} else if (op3 == 0x8) {
opcode << "cmn.w";
} else {
opcode << "cmp.w";
}
args << Rn << ", #" << ThumbExpand(imm32);
} else {
switch (op3) {
case 0x0: opcode << "and"; break;
case 0x1: opcode << "bic"; break;
case 0x2: opcode << "orr"; break;
case 0x3: opcode << "orn"; break;
case 0x4: opcode << "eor"; break;
case 0x8: opcode << "add"; break;
case 0xA: opcode << "adc"; break;
case 0xB: opcode << "sbc"; break;
case 0xD: opcode << "sub"; break;
case 0xE: opcode << "rsb"; break;
default: opcode << "UNKNOWN DPMI-" << op3; break;
}
if (S == 1) {
opcode << "s";
}
args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32);
}
} else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) {
// Data-processing (plain binary immediate)
// |111|11|10|00000|0000|1|111110000000000|
// |5 3|21|09|87654|3 0|5|4 0 5 0|
// |---|--|--|-----|----|-|---------------|
// |332|22|22|22222|1111|1|111110000000000|
// |1 9|87|65|43210|9 6|5|4 0 5 0|
// |---|--|--|-----|----|-|---------------|
// |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx|
uint32_t op3 = (instr >> 20) & 0x1F;
switch (op3) {
case 0x00: case 0x0A: {
// ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii
ArmRegister Rd(instr, 8);
ArmRegister Rn(instr, 16);
uint32_t i = (instr >> 26) & 1;
uint32_t imm3 = (instr >> 12) & 0x7;
uint32_t imm8 = instr & 0xFF;
uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8;
if (Rn.r != 0xF) {
opcode << (op3 == 0 ? "addw" : "subw");
args << Rd << ", " << Rn << ", #" << imm12;
} else {
opcode << "adr";
args << Rd << ", ";
DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12);
}
break;
}
case 0x04: case 0x0C: {
// MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii
ArmRegister Rd(instr, 8);
uint32_t i = (instr >> 26) & 1;
uint32_t imm3 = (instr >> 12) & 0x7;
uint32_t imm8 = instr & 0xFF;
uint32_t Rn = (instr >> 16) & 0xF;
uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8;
opcode << (op3 == 0x04 ? "movw" : "movt");
args << Rd << ", #" << imm16;
break;
}
case 0x16: case 0x14: case 0x1C: {
// BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii
// SBFX Rd, Rn, #lsb, #width - 111 10 0 11 010 0 nnnn 0 iii dddd ii 0 iiiii
// UBFX Rd, Rn, #lsb, #width - 111 10 0 11 110 0 nnnn 0 iii dddd ii 0 iiiii
ArmRegister Rd(instr, 8);
ArmRegister Rn(instr, 16);
uint32_t msb = instr & 0x1F;
uint32_t imm2 = (instr >> 6) & 0x3;
uint32_t imm3 = (instr >> 12) & 0x7;
uint32_t lsb = (imm3 << 2) | imm2;
uint32_t width = msb - lsb + 1;
if (op3 == 0x16) {
if (Rn.r != 0xF) {
opcode << "bfi";
args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
} else {
opcode << "bfc";
args << Rd << ", #" << lsb << ", #" << width;
}
} else {
opcode << ((op3 & 0x8) != 0u ? "ubfx" : "sbfx");
args << Rd << ", " << Rn << ", #" << lsb << ", #" << width;
if (Rd.r == 13 || Rd.r == 15 || Rn.r == 13 || Rn.r == 15 ||
(instr & 0x04000020) != 0u) {
args << " (UNPREDICTABLE)";
}
}
break;
}
default:
break;
}
} else {
// Branches and miscellaneous control
// |111|11|1000000|0000|1|111|1100|00000000|
// |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0|
// |---|--|-------|----|-|---|----|--------|
// |332|22|2222222|1111|1|111|1100|00000000|
// |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0|
// |---|--|-------|----|-|---|----|--------|
// |111|10| op2 | |1|op3|op4 | |
uint32_t op3 = (instr >> 12) & 7;
// uint32_t op4 = (instr >> 8) & 0xF;
switch (op3) {
case 0:
if ((op2 & 0x38) != 0x38) {
// Conditional branch
// |111|11|1|0000|000000|1|1|1 |1|1 |10000000000|
// |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0|
// |---|--|-|----|------|-|-|--|-|--|-----------|
// |332|22|2|2222|221111|1|1|1 |1|1 |10000000000|
// |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0|
// |---|--|-|----|------|-|-|--|-|--|-----------|
// |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 |
uint32_t S = (instr >> 26) & 1;
uint32_t J2 = (instr >> 11) & 1;
uint32_t J1 = (instr >> 13) & 1;
uint32_t imm6 = (instr >> 16) & 0x3F;
uint32_t imm11 = instr & 0x7FF;
uint32_t cond = (instr >> 22) & 0xF;
int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate
opcode << "b";
DumpCond(opcode, cond);
opcode << ".w";
DumpBranchTarget(args, instr_ptr + 4, imm32);
} else if (op2 == 0x3B) {
// Miscellaneous control instructions
uint32_t op5 = (instr >> 4) & 0xF;
switch (op5) {
case 4: opcode << "dsb"; DumpMemoryDomain(args, instr & 0xF); break;
case 5: opcode << "dmb"; DumpMemoryDomain(args, instr & 0xF); break;
case 6: opcode << "isb"; DumpMemoryDomain(args, instr & 0xF); break;
}
}
break;
case 2:
if ((op2 & 0x38) == 0x38) {
if (op2 == 0x7F) {
opcode << "udf";
}
break;
}
FALLTHROUGH_INTENDED; // Else deliberate fall-through to B.
case 1: case 3: {
// B
// |111|11|1|0000|000000|11|1 |1|1 |10000000000|
// |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0|
// |---|--|-|----|------|--|--|-|--|-----------|
// |332|22|2|2222|221111|11|1 |1|1 |10000000000|
// |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0|
// |---|--|-|----|------|--|--|-|--|-----------|
// |111|10|S|cond| imm6 |10|J1|0|J2| imm11 |
// |111|10|S| imm10 |10|J1|1|J2| imm11 |
uint32_t S = (instr >> 26) & 1;
uint32_t cond = (instr >> 22) & 0xF;
uint32_t J2 = (instr >> 11) & 1;
uint32_t form = (instr >> 12) & 1;
uint32_t J1 = (instr >> 13) & 1;
uint32_t imm10 = (instr >> 16) & 0x3FF;
uint32_t imm6 = (instr >> 16) & 0x3F;
uint32_t imm11 = instr & 0x7FF;
opcode << "b";
int32_t imm32;
if (form == 0) {
DumpCond(opcode, cond);
imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate.
} else {
uint32_t I1 = (J1 ^ S) ^ 1;
uint32_t I2 = (J2 ^ S) ^ 1;
imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
imm32 = (imm32 << 7) >> 7; // sign extend 25 bit immediate.
}
opcode << ".w";
DumpBranchTarget(args, instr_ptr + 4, imm32);
break;
}
case 4: case 6: case 5: case 7: {
// BL, BLX (immediate)
// |111|11|1|0000000000|11|1 |1|1 |10000000000|
// |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0|
// |---|--|-|----------|--|--|-|--|-----------|
// |332|22|2|2222221111|11|1 |1|1 |10000000000|
// |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0|
// |---|--|-|----------|--|--|-|--|-----------|
// |111|10|S| imm10 |11|J1|L|J2| imm11 |
uint32_t S = (instr >> 26) & 1;
uint32_t J2 = (instr >> 11) & 1;
uint32_t L = (instr >> 12) & 1;
uint32_t J1 = (instr >> 13) & 1;
uint32_t imm10 = (instr >> 16) & 0x3FF;
uint32_t imm11 = instr & 0x7FF;
if (L == 0) {
opcode << "bx";
} else {
opcode << "blx";
}
uint32_t I1 = ~(J1 ^ S);
uint32_t I2 = ~(J2 ^ S);
int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate.
DumpBranchTarget(args, instr_ptr + 4, imm32);
break;
}
}
}
break;
case 3:
switch (op2) {
case 0x07: case 0x0F: case 0x17: case 0x1F: { // Explicitly UNDEFINED, A6.3.
opcode << "UNDEFINED";
break;
}
case 0x06: case 0x0E: { // "Store single data item" undefined opcodes, A6.3.10.
opcode << "UNDEFINED [store]";
break;
}
case 0x15: case 0x1D: { // "Load word" undefined opcodes, A6.3.7.
opcode << "UNDEFINED [load]";
break;
}
case 0x10: case 0x12: case 0x14: case 0x16: case 0x18: case 0x1A: case 0x1C: case 0x1E: {
opcode << "UNKNOWN " << op2 << " [SIMD]";
break;
}
case 0x01: case 0x00: case 0x09: case 0x08: // {LD,ST}RB{,T}
case 0x03: case 0x02: case 0x0B: case 0x0A: // {LD,ST}RH{,T}
case 0x05: case 0x04: case 0x0D: case 0x0C: // {LD,ST}R{,T}
case 0x11: case 0x19: // LDRSB{,T} (no signed store)
case 0x13: case 0x1B: { // LDRSH{,T} (no signed store)
// Load:
// (Store is the same except that l==0 and always s==0 below.)
// 00s.whl (sign, word, half, load)
// LDR{S}B imm12: 11111|00s1001| Rn | Rt |imm12 (0x09)
// LDR{S}B imm8: 11111|00s0001| Rn | Rt |1PUW|imm8 (0x01)
// LDR{S}BT imm8: 11111|00s0001| Rn | Rt |1110|imm8 (0x01)
// LDR{S}B lit: 11111|00sU001|1111| Rt |imm12 (0x01/0x09)
// LDR{S}B reg: 11111|00s0001| Rn | Rt |000000|imm2| Rm (0x01)
// LDR{S}H imm12: 11111|00s1011| Rn | Rt |imm12 (0x0B)
// LDR{S}H imm8: 11111|00s0011| Rn | Rt |1PUW|imm8 (0x03)
// LDR{S}HT imm8: 11111|00s0011| Rn | Rt |1110|imm8 (0x03)
// LDR{S}H lit: 11111|00sU011|1111| Rt |imm12 (0x03/0x0B)
// LDR{S}H reg: 11111|00s0011| Rn | Rt |000000|imm2| Rm (0x03)
// LDR imm12: 11111|0001101| Rn | Rt |imm12 (0x0D)
// LDR imm8: 11111|0000101| Rn | Rt |1PUW|imm8 (0x05)
// LDRT imm8: 11111|0000101| Rn | Rt |1110|imm8 (0x05)
// LDR lit: 11111|000U101|1111| Rt |imm12 (0x05/0x0D)
// LDR reg: 11111|0000101| Rn | Rt |000000|imm2| Rm (0x05)
//
// If Rt == 15, instead of load we have preload:
// PLD{W} imm12: 11111|00010W1| Rn |1111|imm12 (0x09/0x0B)
// PLD{W} imm8: 11111|00000W1| Rn |1111|1100|imm8 (0x01/0x03); -imm8
// PLD lit: 11111|000U001|1111|1111|imm12 (0x01/0x09)
// PLD{W} reg: 11111|00000W1| Rn |1111|000000|imm2| Rm (0x01/0x03)
// PLI imm12: 11111|0011001| Rn |1111|imm12 (0x19)
// PLI imm8: 11111|0010001| Rn |1111|1100|imm8 (0x11); -imm8
// PLI lit: 11111|001U001|1111|1111|imm12 (0x01/0x09)
// PLI reg: 11111|0010001| Rn |1111|000000|imm2| Rm (0x01/0x03)
bool is_load = HasBitSet(instr, 20);
bool is_half = HasBitSet(instr, 21); // W for PLD/PLDW.
bool is_word = HasBitSet(instr, 22);
bool is_signed = HasBitSet(instr, 24);
ArmRegister Rn(instr, 16);
ArmRegister Rt(instr, 12);
uint32_t imm12 = instr & 0xFFF;
uint32_t U = (instr >> 23) & 1; // U for imm12
uint32_t imm8 = instr & 0xFF;
uint32_t op4 = (instr >> 8) & 0xF; // 1PUW for imm8
if (Rt.r == PC && is_load && !is_word) {
// PLD, PLDW, PLI
const char* pld_pli = (is_signed ? "pli" : "pld");
const char* w = (is_half ? "w" : "");
if (is_signed && !is_half) {
opcode << "UNDEFINED [PLI+W]";
} else if (Rn.r == PC || U != 0u) {
opcode << pld_pli << w;
args << "[" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
if (Rn.r == PC && is_half) {
args << " (UNPREDICTABLE)";
}
} else if ((instr & 0xFC0) == 0) {
opcode << pld_pli << w;
RmLslImm2 Rm(instr);
args << "[" << Rn << ", " << Rm << "]";
} else if (op4 == 0xC) {
opcode << pld_pli << w;
args << "[" << Rn << ", #-" << imm8 << "]";
} else {
opcode << "UNDEFINED [~" << pld_pli << "]";
}
break;
}
const char* ldr_str = is_load ? "ldr" : "str";
const char* sign = is_signed ? "s" : "";
const char* type = is_word ? "" : is_half ? "h" : "b";
bool unpred = (Rt.r == SP && !is_word) || (Rt.r == PC && !is_load);
if (Rn.r == PC && !is_load) {
opcode << "UNDEFINED [STR-lit]";
unpred = false;
} else if (Rn.r == PC || U != 0u) {
// Load/store with imm12 (load literal if Rn.r == PC; there's no store literal).
opcode << ldr_str << sign << type << ".w";
args << Rt << ", [" << Rn << ", #" << (U != 0u ? "" : "-") << imm12 << "]";
if (Rn.r == TR && is_load) {
args << " ; ";
Thread::DumpThreadOffset<4>(args, imm12);
} else if (Rn.r == PC) {
T2LitType lit_type[] = {
kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
kT2LitUByte, kT2LitUHalf, kT2LitHexWord, kT2LitInvalid,
kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
kT2LitSByte, kT2LitSHalf, kT2LitInvalid, kT2LitInvalid,
};
DCHECK_LT(op2 >> 1, arraysize(lit_type));
DCHECK_NE(lit_type[op2 >> 1], kT2LitInvalid);
DumpThumb2Literal(args, instr_ptr, lo_adr, hi_adr, U, imm12, lit_type[op2 >> 1]);
}
} else if ((instr & 0xFC0) == 0) {
opcode << ldr_str << sign << type << ".w";
RmLslImm2 Rm(instr);
args << Rt << ", [" << Rn << ", " << Rm << "]";
unpred = unpred || (Rm.rm.r == SP) || (Rm.rm.r == PC);
} else if (is_word && Rn.r == SP && imm8 == 4 && op4 == (is_load ? 0xB : 0xD)) {
opcode << (is_load ? "pop" : "push") << ".w";
args << Rn;
unpred = unpred || (Rn.r == SP);
} else if ((op4 & 5) == 0) {
opcode << "UNDEFINED [P = W = 0 for " << ldr_str << "]";
unpred = false;
} else {
uint32_t P = (instr >> 10) & 1;
U = (instr >> 9) & 1;
uint32_t W = (instr >> 8) & 1;
bool pre_index = (P != 0 && W == 1);
bool post_index = (P == 0 && W == 1);
const char* t = (P != 0 && U != 0 && W == 0) ? "t" : ""; // Unprivileged load/store?
opcode << ldr_str << sign << type << t << ".w";
args << Rt << ", [" << Rn << (post_index ? "]" : "") << ", #" << (U != 0 ? "" : "-")
<< imm8 << (post_index ? "" : "]") << (pre_index ? "!" : "");
unpred = (W != 0 && Rn.r == Rt.r);
}
if (unpred) {
args << " (UNPREDICTABLE)";
}
break;
}
case 0x29: { // 0101001
// |111|11|1000000|0000|1111|1100|00|0 0|0000|
// |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0|
// |---|--|-------|----|----|----|--|---|----|
// |332|22|2222222|1111|1111|1100|00|0 0|0000|
// |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0|
// |---|--|-------|----|----|----|--|---|----|
// |111|11|0101001| Rm |1111| Rd |11|op3| Rm |
// REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm
// REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm
// RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm
// REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm
if ((instr & 0xf0c0) == 0xf080) {
uint32_t op3 = (instr >> 4) & 3;
opcode << kThumbReverseOperations[op3];
ArmRegister Rm(instr, 0);
ArmRegister Rd(instr, 8);
args << Rd << ", " << Rm;
ArmRegister Rm2(instr, 16);
if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
args << " (UNPREDICTABLE)";
}
} // else unknown instruction
break;
}
case 0x2B: { // 0101011
// CLZ - 111 11 0101011 mmmm 1111 dddd 1000 mmmm
if ((instr & 0xf0f0) == 0xf080) {
opcode << "clz";
ArmRegister Rm(instr, 0);
ArmRegister Rd(instr, 8);
args << Rd << ", " << Rm;
ArmRegister Rm2(instr, 16);
if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) {
args << " (UNPREDICTABLE)";
}
}
break;
}
default: // more formats
if ((op2 >> 4) == 2) { // 010xxxx
// data processing (register)
if ((instr & 0x0080f0f0) == 0x0000f000) {
// LSL, LSR, ASR, ROR
uint32_t shift_op = (instr >> 21) & 3;
uint32_t S = (instr >> 20) & 1;
ArmRegister Rd(instr, 8);
ArmRegister Rn(instr, 16);
ArmRegister Rm(instr, 0);
opcode << kThumb2ShiftOperations[shift_op] << (S != 0 ? "s" : "");
args << Rd << ", " << Rn << ", " << Rm;
}
} else if ((op2 >> 3) == 6) { // 0110xxx
// Multiply, multiply accumulate, and absolute difference
op1 = (instr >> 20) & 0x7;
op2 = (instr >> 4) & 0x1;
ArmRegister Ra(instr, 12);
ArmRegister Rn(instr, 16);
ArmRegister Rm(instr, 0);
ArmRegister Rd(instr, 8);
switch (op1) {
case 0:
if (op2 == 0) {
if (Ra.r == 0xf) {
opcode << "mul";
args << Rd << ", " << Rn << ", " << Rm;
} else {
opcode << "mla";
args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
}
} else {
opcode << "mls";
args << Rd << ", " << Rn << ", " << Rm << ", " << Ra;
}
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
break; // do these sometime
}
} else if ((op2 >> 3) == 7) { // 0111xxx
// Long multiply, long multiply accumulate, and divide
op1 = (instr >> 20) & 0x7;
op2 = (instr >> 4) & 0xf;
ArmRegister Rn(instr, 16);
ArmRegister Rm(instr, 0);
ArmRegister Rd(instr, 8);
ArmRegister RdHi(instr, 8);
ArmRegister RdLo(instr, 12);
switch (op1) {
case 0:
opcode << "smull";
args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
break;
case 1:
opcode << "sdiv";
args << Rd << ", " << Rn << ", " << Rm;
break;
case 2:
opcode << "umull";
args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm;
break;
case 3:
opcode << "udiv";
args << Rd << ", " << Rn << ", " << Rm;
break;
case 4:
case 5:
case 6:
break; // TODO: when we generate these...
}
}
}
break;
default:
break;
}
// Apply any IT-block conditions to the opcode if necessary.
if (!it_conditions_.empty()) {
opcode << it_conditions_.back();
it_conditions_.pop_back();
}
if (opcode.str().size() == 0) {
opcode << "UNKNOWN " << op2;
}
os << FormatInstructionPointer(instr_ptr)
<< StringPrintf(": %08x\t%-7s ", instr, opcode.str().c_str())
<< args.str() << '\n';
return 4;
} // NOLINT(readability/fn_size)