+void OpcodeDecoder::Decode_00xxxxxx(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; vsp = vsp + %u\n", Opcode,
+ ((Opcode & 0x3f) << 2) + 4);
+}
+void OpcodeDecoder::Decode_01xxxxxx(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; vsp = vsp - %u\n", Opcode,
+ ((Opcode & 0x3f) << 2) + 4);
+}
+void OpcodeDecoder::Decode_1000iiii_iiiiiiii(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode0 = Opcodes[OI++ ^ 3];
+ uint8_t Opcode1 = Opcodes[OI++ ^ 3];
+
+ uint16_t GPRMask = (Opcode1 << 4) | ((Opcode0 & 0x0f) << 12);
+ SW.startLine()
+ << format("0x%02X 0x%02X ; %s",
+ Opcode0, Opcode1, GPRMask ? "pop " : "refuse to unwind");
+ if (GPRMask)
+ PrintGPR(GPRMask);
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_10011101(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; reserved (ARM MOVrr)\n", Opcode);
+}
+void OpcodeDecoder::Decode_10011111(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; reserved (WiMMX MOVrr)\n", Opcode);
+}
+void OpcodeDecoder::Decode_1001nnnn(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; vsp = r%u\n", Opcode, (Opcode & 0x0f));
+}
+void OpcodeDecoder::Decode_10100nnn(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; pop ", Opcode);
+ PrintGPR((((1 << ((Opcode & 0x7) + 1)) - 1) << 4));
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_10101nnn(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; pop ", Opcode);
+ PrintGPR((((1 << ((Opcode & 0x7) + 1)) - 1) << 4) | (1 << 14));
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_10110000(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; finish\n", Opcode);
+}
+void OpcodeDecoder::Decode_10110001_0000iiii(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode0 = Opcodes[OI++ ^ 3];
+ uint8_t Opcode1 = Opcodes[OI++ ^ 3];
+
+ SW.startLine()
+ << format("0x%02X 0x%02X ; %s", Opcode0, Opcode1,
+ ((Opcode1 & 0xf0) || Opcode1 == 0x00) ? "spare" : "pop ");
+ if (((Opcode1 & 0xf0) == 0x00) && Opcode1)
+ PrintGPR((Opcode1 & 0x0f));
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_10110010_uleb128(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ", Opcode);
+
+ SmallVector<uint8_t, 4> ULEB;
+ do { ULEB.push_back(Opcodes[OI ^ 3]); } while (Opcodes[OI++ ^ 3] & 0x80);
+
+ for (unsigned BI = 0, BE = ULEB.size(); BI != BE; ++BI)
+ OS << format("0x%02X ", ULEB[BI]);
+
+ uint64_t Value = 0;
+ for (unsigned BI = 0, BE = ULEB.size(); BI != BE; ++BI)
+ Value = Value | ((ULEB[BI] & 0x7f) << (7 * BI));
+
+ OS << format("; vsp = vsp + %" PRIu64 "\n", 0x204 + (Value << 2));
+}
+void OpcodeDecoder::Decode_10110011_sssscccc(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode0 = Opcodes[OI++ ^ 3];
+ uint8_t Opcode1 = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
+ uint8_t Start = ((Opcode1 & 0xf0) >> 4);
+ uint8_t Count = ((Opcode1 & 0x0f) >> 0);
+ PrintRegisters((((1 << (Count + 1)) - 1) << Start), "d");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_101101nn(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; spare\n", Opcode);
+}
+void OpcodeDecoder::Decode_10111nnn(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; pop ", Opcode);
+ PrintRegisters((((1 << ((Opcode & 0x07) + 1)) - 1) << 8), "d");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_11000110_sssscccc(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode0 = Opcodes[OI++ ^ 3];
+ uint8_t Opcode1 = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
+ uint8_t Start = ((Opcode1 & 0xf0) >> 4);
+ uint8_t Count = ((Opcode1 & 0x0f) >> 0);
+ PrintRegisters((((1 << (Count + 1)) - 1) << Start), "wR");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_11000111_0000iiii(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode0 = Opcodes[OI++ ^ 3];
+ uint8_t Opcode1 = Opcodes[OI++ ^ 3];
+ SW.startLine()
+ << format("0x%02X 0x%02X ; %s", Opcode0, Opcode1,
+ ((Opcode1 & 0xf0) || Opcode1 == 0x00) ? "spare" : "pop ");
+ if ((Opcode1 & 0xf0) == 0x00 && Opcode1)
+ PrintRegisters(Opcode1 & 0x0f, "wCGR");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_11001000_sssscccc(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode0 = Opcodes[OI++ ^ 3];
+ uint8_t Opcode1 = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
+ uint8_t Start = 16 + ((Opcode1 & 0xf0) >> 4);
+ uint8_t Count = ((Opcode1 & 0x0f) >> 0);
+ PrintRegisters((((1 << (Count + 1)) - 1) << Start), "d");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_11001001_sssscccc(const uint8_t *Opcodes,
+ unsigned &OI) {
+ uint8_t Opcode0 = Opcodes[OI++ ^ 3];
+ uint8_t Opcode1 = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
+ uint8_t Start = ((Opcode1 & 0xf0) >> 4);
+ uint8_t Count = ((Opcode1 & 0x0f) >> 0);
+ PrintRegisters((((1 << (Count + 1)) - 1) << Start), "d");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_11001yyy(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; spare\n", Opcode);
+}
+void OpcodeDecoder::Decode_11000nnn(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; pop ", Opcode);
+ PrintRegisters((((1 << ((Opcode & 0x07) + 1)) - 1) << 10), "wR");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_11010nnn(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; pop ", Opcode);
+ PrintRegisters((((1 << ((Opcode & 0x07) + 1)) - 1) << 8), "d");
+ OS << '\n';
+}
+void OpcodeDecoder::Decode_11xxxyyy(const uint8_t *Opcodes, unsigned &OI) {
+ uint8_t Opcode = Opcodes[OI++ ^ 3];
+ SW.startLine() << format("0x%02X ; spare\n", Opcode);
+}
+
+void OpcodeDecoder::PrintGPR(uint16_t GPRMask) {
+ static const char *GPRRegisterNames[16] = {
+ "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
+ "fp", "ip", "sp", "lr", "pc"
+ };
+
+ OS << '{';
+ bool Comma = false;
+ for (unsigned RI = 0, RE = 17; RI < RE; ++RI) {
+ if (GPRMask & (1 << RI)) {
+ if (Comma)
+ OS << ", ";
+ OS << GPRRegisterNames[RI];
+ Comma = true;
+ }
+ }
+ OS << '}';
+}
+
+void OpcodeDecoder::PrintRegisters(uint32_t VFPMask, StringRef Prefix) {
+ OS << '{';
+ bool Comma = false;
+ for (unsigned RI = 0, RE = 32; RI < RE; ++RI) {
+ if (VFPMask & (1 << RI)) {
+ if (Comma)
+ OS << ", ";
+ OS << Prefix << RI;
+ Comma = true;
+ }
+ }
+ OS << '}';
+}
+
+void OpcodeDecoder::Decode(const uint8_t *Opcodes, off_t Offset, size_t Length) {
+ for (unsigned OCI = Offset; OCI < Length + Offset; ) {
+ bool Decoded = false;
+ for (unsigned REI = 0, REE = array_lengthof(Ring);
+ REI != REE && !Decoded; ++REI) {
+ if ((Opcodes[OCI ^ 3] & Ring[REI].Mask) == Ring[REI].Value) {
+ (this->*Ring[REI].Routine)(Opcodes, OCI);
+ Decoded = true;
+ break;
+ }
+ }
+ if (!Decoded)
+ SW.startLine() << format("0x%02X ; reserved\n", Opcodes[OCI++ ^ 3]);
+ }
+}
+
+template <typename ET>
+class PrinterContext {