12 #include "../../MCInst.h"
13 #include "../../LEB128.h"
16 #define FieldFromInstruction(fname, InsnType) \
17 static InsnType fname(InsnType insn, unsigned startBit, unsigned numBits) \
20 if (numBits == sizeof(InsnType)*8) \
21 fieldMask = (InsnType)(-1LL); \
23 fieldMask = (((InsnType)1 << numBits) - 1) << startBit; \
24 return (insn & fieldMask) >> startBit; \
27 static const uint8_t DecoderTable16[] = {
358 static const uint8_t DecoderTable32[] = {
602 static bool checkDecoderPredicate(
unsigned Idx,
uint64_t Bits)
607 #define DecodeToMCInst(fname,fieldname, InsnType) \
608 static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \
609 uint64_t Address, const void *Decoder) \
617 tmp = fieldname(insn, 0, 4); \
618 if (DecodeGRRegsRegisterClass(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
621 if (Decode2RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
624 if (Decode2RUSInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
627 if (DecodeR2RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
630 if (Decode3RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
633 if (Decode2RImmInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
636 if (Decode2RSrcDstInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
639 if (DecodeRUSSrcDstBitpInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
642 if (DecodeRUSInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
645 tmp = fieldname(insn, 6, 4); \
646 if (DecodeRRegsRegisterClass(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
647 tmp = fieldname(insn, 0, 6); \
648 MCOperand_CreateImm0(MI, tmp); \
651 tmp = fieldname(insn, 0, 6); \
652 MCOperand_CreateImm0(MI, tmp); \
655 tmp = fieldname(insn, 6, 4); \
656 if (DecodeGRRegsRegisterClass(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
657 tmp = fieldname(insn, 0, 6); \
658 MCOperand_CreateImm0(MI, tmp); \
661 tmp = fieldname(insn, 0, 6); \
662 if (DecodeNegImmOperand(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
665 tmp = fieldname(insn, 6, 4); \
666 if (DecodeGRRegsRegisterClass(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
667 tmp = fieldname(insn, 0, 6); \
668 if (DecodeNegImmOperand(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
671 if (DecodeRUSBitpInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
674 if (Decode2RUSBitpInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
677 if (Decode3RImmInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
680 tmp = fieldname(insn, 0, 10); \
681 MCOperand_CreateImm0(MI, tmp); \
684 tmp = fieldname(insn, 0, 10); \
685 if (DecodeNegImmOperand(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
688 if (DecodeL2RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
691 if (DecodeL3RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
694 if (DecodeL4RSrcDstInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
697 if (DecodeL4RSrcDstSrcDstInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
700 if (DecodeL5RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
703 if (DecodeL6RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
706 if (DecodeLR2RInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
709 tmp = fieldname(insn, 22, 4); \
710 if (DecodeRRegsRegisterClass(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
712 tmp |= (fieldname(insn, 0, 10) << 6); \
713 tmp |= (fieldname(insn, 16, 6) << 0); \
714 MCOperand_CreateImm0(MI, tmp); \
718 tmp |= (fieldname(insn, 0, 10) << 6); \
719 tmp |= (fieldname(insn, 16, 6) << 0); \
720 MCOperand_CreateImm0(MI, tmp); \
723 tmp = fieldname(insn, 22, 4); \
724 if (DecodeGRRegsRegisterClass(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
726 tmp |= (fieldname(insn, 0, 10) << 6); \
727 tmp |= (fieldname(insn, 16, 6) << 0); \
728 MCOperand_CreateImm0(MI, tmp); \
732 tmp |= (fieldname(insn, 0, 10) << 6); \
733 tmp |= (fieldname(insn, 16, 6) << 0); \
734 if (DecodeNegImmOperand(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
737 tmp = fieldname(insn, 22, 4); \
738 if (DecodeGRRegsRegisterClass(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
740 tmp |= (fieldname(insn, 0, 10) << 6); \
741 tmp |= (fieldname(insn, 16, 6) << 0); \
742 if (DecodeNegImmOperand(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
745 if (DecodeL2RUSBitpInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
748 if (DecodeL2RUSInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
751 if (DecodeL3RSrcDstInstruction(MI, insn, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
755 tmp |= (fieldname(insn, 0, 10) << 10); \
756 tmp |= (fieldname(insn, 16, 10) << 0); \
757 MCOperand_CreateImm0(MI, tmp); \
761 tmp |= (fieldname(insn, 0, 10) << 10); \
762 tmp |= (fieldname(insn, 16, 10) << 0); \
763 if (DecodeNegImmOperand(MI, tmp, Address, Decoder) == MCDisassembler_Fail) return MCDisassembler_Fail; \
768 #define DecodeInstruction(fname, fieldname, decoder, InsnType) \
769 static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \
770 InsnType insn, uint64_t Address, const MCRegisterInfo *MRI, int feature) \
772 uint64_t Bits = getFeatureBits(feature); \
773 const uint8_t *Ptr = DecodeTable; \
774 uint32_t CurFieldValue = 0, ExpectedValue; \
775 DecodeStatus S = MCDisassembler_Success; \
776 unsigned Start, Len, NumToSkip, PIdx, Opc, DecodeIdx; \
777 InsnType Val, FieldValue, PositiveMask, NegativeMask; \
782 return MCDisassembler_Fail; \
783 case MCD_OPC_ExtractField: { \
787 CurFieldValue = (uint32_t)fieldname(insn, Start, Len); \
790 case MCD_OPC_FilterValue: { \
791 Val = (InsnType)decodeULEB128(++Ptr, &Len); \
793 NumToSkip = *Ptr++; \
794 NumToSkip |= (*Ptr++) << 8; \
795 if (Val != CurFieldValue) \
799 case MCD_OPC_CheckField: { \
802 FieldValue = fieldname(insn, Start, Len); \
803 ExpectedValue = (uint32_t)decodeULEB128(++Ptr, &Len); \
805 NumToSkip = *Ptr++; \
806 NumToSkip |= (*Ptr++) << 8; \
807 if (ExpectedValue != FieldValue) \
811 case MCD_OPC_CheckPredicate: { \
812 PIdx = (uint32_t)decodeULEB128(++Ptr, &Len); \
814 NumToSkip = *Ptr++; \
815 NumToSkip |= (*Ptr++) << 8; \
816 Pred = checkDecoderPredicate(PIdx, Bits); \
822 case MCD_OPC_Decode: { \
823 Opc = (unsigned)decodeULEB128(++Ptr, &Len); \
825 DecodeIdx = (unsigned)decodeULEB128(Ptr, &Len); \
827 MCInst_setOpcode(MI, Opc); \
828 return decoder(S, DecodeIdx, insn, MI, Address, MRI); \
830 case MCD_OPC_SoftFail: { \
831 PositiveMask = (InsnType)decodeULEB128(++Ptr, &Len); \
833 NegativeMask = (InsnType)decodeULEB128(Ptr, &Len); \
835 Fail = (insn & PositiveMask) || (~insn & NegativeMask); \
837 S = MCDisassembler_SoftFail; \
840 case MCD_OPC_Fail: { \
841 return MCDisassembler_Fail; \
848 FieldFromInstruction(fieldFromInstruction_2,
uint16_t)
849 DecodeToMCInst(decodeToMCInst_2, fieldFromInstruction_2,
uint16_t)
850 DecodeInstruction(decodeInstruction_2, fieldFromInstruction_2, decodeToMCInst_2,
uint16_t)
851 FieldFromInstruction(fieldFromInstruction_4,
uint32_t)
852 DecodeToMCInst(decodeToMCInst_4, fieldFromInstruction_4,
uint32_t)
853 DecodeInstruction(decodeInstruction_4, fieldFromInstruction_4, decodeToMCInst_4,
uint32_t)