17 #ifdef CAPSTONE_HAS_MIPS
22 #include "capstone/platform.h"
26 #include "../../utils.h"
28 #include "../../MCRegisterInfo.h"
29 #include "../../SStream.h"
31 #include "../../MathExtras.h"
36 #include "../../MCFixedLenDisassembler.h"
37 #include "../../MCInst.h"
39 #include "../../MCRegisterInfo.h"
40 #include "../../MCDisassembler.h"
278 #define GET_SUBTARGETINFO_ENUM
287 Bits &= ~Mips_FeatureMips1;
290 Bits &= ~Mips_FeatureMicroMips;
304 Bits &= ~Mips_FeatureMips16;
305 Bits &= ~Mips_FeatureFP64Bit;
306 Bits &= ~Mips_FeatureMips64r2;
307 Bits &= ~Mips_FeatureMips32r6;
308 Bits &= ~Mips_FeatureMips64r6;
310 Bits &= ~Mips_FeatureMips16;
311 Bits &= ~Mips_FeatureMips64r6;
312 Bits &= ~Mips_FeatureMips32r6;
314 Bits |= Mips_FeatureMips32r6;
315 Bits &= ~Mips_FeatureMips16;
316 Bits &= ~Mips_FeatureFP64Bit;
317 Bits &= ~Mips_FeatureMips64r6;
318 Bits &= ~Mips_FeatureMips64r2;
322 Bits |= Mips_FeatureMicroMips;
323 Bits &= ~Mips_FeatureMips4_32r2;
324 Bits &= ~Mips_FeatureMips2;
332 #define GET_REGINFO_ENUM
335 #define GET_REGINFO_MC_DESC
338 #define GET_INSTRINFO_ENUM
359 MipsMCRegisterClasses, 62,
363 MipsSubRegIdxLists, 12,
369 static void readInstruction16(
unsigned char *
code,
uint32_t *insn,
381 static void readInstruction32(
unsigned char *
code,
uint32_t *insn,
bool isBigEndian,
bool isMicroMips)
398 *insn = (
code[2] << 0) | (
code[3] << 8) | (
code[0] << 16) |
401 *insn = (
code[0] << 0) | (
code[1] << 8) | (
code[2] << 16) |
424 readInstruction16((
unsigned char*)
code, &Insn, isBigEndian);
437 readInstruction32((
unsigned char*)
code, &Insn, isBigEndian,
true);
453 readInstruction32((
unsigned char*)
code, &Insn, isBigEndian,
false);
518 static unsigned getReg(
const MCRegisterInfo *MRI,
unsigned RC,
unsigned RegNo)
532 DecodeFN RegDecoder =
NULL;
534 if ((
tmp & 0x18) == 0x00) {
536 RegDecoder = DecodeMSA128BRegisterClass;
537 }
else if ((
tmp & 0x1c) == 0x10) {
539 RegDecoder = DecodeMSA128HRegisterClass;
540 }
else if ((
tmp & 0x1e) == 0x18) {
542 RegDecoder = DecodeMSA128WRegisterClass;
543 }
else if ((
tmp & 0x1f) == 0x1c) {
545 RegDecoder = DecodeMSA128DRegisterClass;
549 if (NSize == 0 || RegDecoder ==
NULL)
553 tmp = fieldFromInstruction(insn, 6, 5);
562 tmp = fieldFromInstruction(insn, 16, NSize);
566 tmp = fieldFromInstruction(insn, 11, 5);
589 uint32_t Rs = fieldFromInstruction(insn, 21, 5);
590 uint32_t Rt = fieldFromInstruction(insn, 16, 5);
597 }
else if (Rs != 0 && Rs < Rt) {
625 uint32_t Rs = fieldFromInstruction(insn, 21, 5);
626 uint32_t Rt = fieldFromInstruction(insn, 16, 5);
633 }
else if (Rs != 0 && Rs < Rt) {
662 uint32_t Rs = fieldFromInstruction(insn, 21, 5);
663 uint32_t Rt = fieldFromInstruction(insn, 16, 5);
704 uint32_t Rs = fieldFromInstruction(insn, 21, 5);
705 uint32_t Rt = fieldFromInstruction(insn, 16, 5);
742 uint32_t Rs = fieldFromInstruction(insn, 21, 5);
743 uint32_t Rt = fieldFromInstruction(insn, 16, 5);
751 }
else if (Rs == 0) {
754 }
else if (Rs == Rt) {
788 uint32_t Rs = fieldFromInstruction(insn, 21, 5);
789 uint32_t Rt = fieldFromInstruction(insn, 16, 5);
828 Reg = getReg(Decoder, Mips_GPR64RegClassID, RegNo);
841 Reg = getReg(Decoder, Mips_GPRMM16RegClassID, RegNo);
854 Reg = getReg(Decoder, Mips_GPRMM16ZeroRegClassID, RegNo);
867 Reg = getReg(Decoder, Mips_GPRMM16MovePRegClassID, RegNo);
880 Reg = getReg(Decoder, Mips_GPR32RegClassID, RegNo);
890 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
892 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
898 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
909 Reg = getReg(Decoder, Mips_FGR64RegClassID, RegNo);
922 Reg = getReg(Decoder, Mips_FGR32RegClassID, RegNo);
935 Reg = getReg(Decoder, Mips_CCRRegClassID, RegNo);
948 Reg = getReg(Decoder, Mips_FCCRegClassID, RegNo);
961 Reg = getReg(Decoder, Mips_CCRegClassID, RegNo);
974 Reg = getReg(Decoder, Mips_FGRCCRegClassID, RegNo);
983 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
984 unsigned Base = fieldFromInstruction(Insn, 21, 5);
987 Reg = getReg(Decoder, Mips_GPR32RegClassID,
Reg);
988 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
990 if (opcode == Mips_SC || opcode == Mips_SCD) {
1005 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1006 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1008 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1021 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1022 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1024 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1036 int Offset = fieldFromInstruction(Insn, 7, 9);
1037 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1038 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1040 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1053 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1055 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1067 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1068 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1070 Reg = getReg(Decoder, Mips_MSA128BRegClassID,
Reg);
1071 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1113 unsigned Offset = Insn & 0xf;
1114 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1115 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1121 if (DecodeGPRMM16RegisterClass(Inst,
Reg, Address, Decoder)
1128 if (DecodeGPRMM16ZeroRegisterClass(Inst,
Reg, Address, Decoder)
1134 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1164 unsigned Offset = Insn & 0x1F;
1165 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1167 Reg = getReg(Decoder, Mips_GPR32RegClassID,
Reg);
1179 unsigned Offset = Insn & 0x7F;
1180 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1182 Reg = getReg(Decoder, Mips_GPR32RegClassID,
Reg);
1209 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1210 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1212 Reg = getReg(Decoder, Mips_GPR32RegClassID,
Reg);
1213 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1218 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1243 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1244 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1246 Reg = getReg(Decoder, Mips_GPR32RegClassID,
Reg);
1247 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1260 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1261 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1263 Reg = getReg(Decoder, Mips_FGR64RegClassID,
Reg);
1264 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1277 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1278 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1280 Reg = getReg(Decoder, Mips_COP2RegClassID,
Reg);
1281 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1294 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1295 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1297 Reg = getReg(Decoder, Mips_COP3RegClassID,
Reg);
1298 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1311 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1312 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1314 Reg = getReg(Decoder, Mips_COP2RegClassID,
Reg);
1315 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1328 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1329 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1331 Rt = getReg(Decoder, Mips_GPR32RegClassID, Rt);
1332 Base = getReg(Decoder, Mips_GPR32RegClassID, Base);
1363 if (RegNo > 30 || RegNo % 2)
1366 Reg = getReg(Decoder, Mips_AFGR64RegClassID, RegNo /2);
1380 Reg = getReg(Decoder, Mips_ACC64DSPRegClassID, RegNo);
1393 Reg = getReg(Decoder, Mips_HI32DSPRegClassID, RegNo);
1407 Reg = getReg(Decoder, Mips_LO32DSPRegClassID, RegNo);
1421 Reg = getReg(Decoder, Mips_MSA128BRegClassID, RegNo);
1435 Reg = getReg(Decoder, Mips_MSA128HRegClassID, RegNo);
1449 Reg = getReg(Decoder, Mips_MSA128WRegClassID, RegNo);
1463 Reg = getReg(Decoder, Mips_MSA128DRegClassID, RegNo);
1477 Reg = getReg(Decoder, Mips_MSACtrlRegClassID, RegNo);
1491 Reg = getReg(Decoder, Mips_COP2RegClassID, RegNo);
1509 uint64_t TargetAddress = (fieldFromInstruction(Insn, 0, 26) << 2) | ((Address + 4) & ~0x0FFFFFFF);
1562 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1573 else if (Value == 0x7)
1630 int Size = (
int) Insn -
Pos + 1;
1639 int Size = (
int)Insn + 1;
1668 case 0: DecodedValue = 256;
break;
1669 case 1: DecodedValue = 257;
break;
1670 case 510: DecodedValue = -258;
break;
1671 case 511: DecodedValue = -257;
break;
1684 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1706 unsigned Regs[] = {Mips_S0, Mips_S1, Mips_S2, Mips_S3, Mips_S4, Mips_S5,
1711 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1716 RegNum = RegLst & 0xf;
1729 unsigned Regs[] = {Mips_S0, Mips_S1, Mips_S2, Mips_S3};
1730 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1731 unsigned RegNum = RegLst & 0x3;
1734 for (
i = 0;
i <= RegNum;
i++)
1745 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
unsigned MCInst_getOpcode(const MCInst *inst)
MCOperand * MCInst_getOperand(MCInst *inst, unsigned i)
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
int64_t MCOperand_getImm(MCOperand *op)
void MCInst_setOpcode(MCInst *inst, unsigned Op)
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
const MCRegisterClass * MCRegisterInfo_getRegClass(const MCRegisterInfo *RI, unsigned i)
void MCRegisterInfo_InitMCRegisterInfo(MCRegisterInfo *RI, const MCRegisterDesc *D, unsigned NR, unsigned RA, unsigned PC, const MCRegisterClass *C, unsigned NC, uint16_t(*RURoots)[2], unsigned NRU, const MCPhysReg *DL, const char *Strings, const uint16_t *SubIndices, unsigned NumIndices, const uint16_t *RET)
bool Mips_getInstruction(csh handle, const uint8_t *code, size_t code_len, MCInst *instr, uint16_t *size, uint64_t address, void *info)
void Mips_init(MCRegisterInfo *MRI)
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader, const void *readerArg, uint64_t startLoc, DisassemblerMode mode)
static mcore_handle handle
RzBinInfo * info(RzBinFile *bf)
@ CS_MODE_64
64-bit mode (X86, PPC)
@ CS_MODE_MIPS64
Mips64 ISA (Mips)
@ CS_MODE_32
32-bit mode (X86)
@ CS_MODE_MICRO
MicroMips mode (MIPS)
@ CS_MODE_MIPS3
Mips III ISA.
@ CS_MODE_MIPS32R6
Mips32r6 ISA.
@ CS_MODE_16
16-bit mode (X86)
@ CS_MODE_MIPS2
Mips II ISA.
#define MODE_IS_BIG_ENDIAN(mode)
#define offsetof(type, member)
return memset(p, 0, total)
static const char struct stat static buf struct stat static buf static vhangup int status
static void struct sockaddr socklen_t static fromlen static backlog static fork char char char static envp int struct rusage static rusage struct utsname static buf struct sembuf unsigned
_W64 unsigned int uintptr_t