19 #ifdef CAPSTONE_HAS_X86
22 #if defined(CAPSTONE_HAS_OSXKERNEL)
23 #include <libkern/libkern.h>
28 #include "../../cs_priv.h"
29 #include "../../utils.h"
36 struct ModRMDecision {
43 struct OpcodeDecision {
44 struct ModRMDecision modRMDecisions[256];
52 struct ContextDecision {
53 struct OpcodeDecision opcodeDecisions[
IC_max];
56 #ifdef CAPSTONE_X86_REDUCE
63 #define GET_INSTRINFO_MC_DESC
64 #ifdef CAPSTONE_X86_REDUCE
98 const struct OpcodeDecision *decision =
NULL;
106 indextable = index_x86DisassemblerOneByteOpcodes;
110 indextable = index_x86DisassemblerTwoByteOpcodes;
114 indextable = index_x86DisassemblerThreeByte38Opcodes;
118 indextable = index_x86DisassemblerThreeByte3AOpcodes;
120 #ifndef CAPSTONE_X86_REDUCE
123 indextable = index_x86DisassemblerXOP8Opcodes;
127 indextable = index_x86DisassemblerXOP9Opcodes;
131 indextable = index_x86DisassemblerXOPAOpcodes;
139 index = indextable[insnContext];
141 return decision[index - 1].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
161 const struct ModRMDecision *dec =
NULL;
168 indextable = index_x86DisassemblerOneByteOpcodes;
169 index = indextable[insnContext];
171 dec = &
ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
173 dec = &emptyTable.modRMDecisions[opcode];
176 indextable = index_x86DisassemblerTwoByteOpcodes;
177 index = indextable[insnContext];
179 dec = &
TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
181 dec = &emptyTable.modRMDecisions[opcode];
184 indextable = index_x86DisassemblerThreeByte38Opcodes;
185 index = indextable[insnContext];
189 dec = &emptyTable.modRMDecisions[opcode];
192 indextable = index_x86DisassemblerThreeByte3AOpcodes;
193 index = indextable[insnContext];
197 dec = &emptyTable.modRMDecisions[opcode];
199 #ifndef CAPSTONE_X86_REDUCE
201 indextable = index_x86DisassemblerXOP8Opcodes;
202 index = indextable[insnContext];
206 dec = &emptyTable.modRMDecisions[opcode];
209 indextable = index_x86DisassemblerXOP9Opcodes;
210 index = indextable[insnContext];
214 dec = &emptyTable.modRMDecisions[opcode];
217 indextable = index_x86DisassemblerXOPAOpcodes;
218 index = indextable[insnContext];
222 dec = &emptyTable.modRMDecisions[opcode];
225 indextable = index_x86DisassemblerT3DNOWOpcodes;
226 index = indextable[insnContext];
230 dec = &emptyTable.modRMDecisions[opcode];
235 switch (dec->modrm_type) {
240 return modRMTable[dec->instructionIDs];
243 return modRMTable[dec->instructionIDs+1];
244 return modRMTable[dec->instructionIDs];
247 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
248 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
249 case MODRM_SPLITMISC:
251 return modRMTable[dec->instructionIDs+(modRM & 0x3f)+8];
252 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
254 return modRMTable[dec->instructionIDs+modRM];
308 #define CONSUME_FUNC(name, type) \
309 static int name(struct InternalInstruction *insn, type *ptr) { \
312 for (offset = 0; offset < sizeof(type); ++offset) { \
314 int ret = insn->reader(insn->readerArg, \
316 insn->readerCursor + offset); \
319 combined = combined | (type)((uint64_t)byte << (offset * 8)); \
322 insn->readerCursor += sizeof(type); \
336 CONSUME_FUNC(consumeInt8,
int8_t)
337 CONSUME_FUNC(consumeInt16,
int16_t)
338 CONSUME_FUNC(consumeInt32,
int32_t)
339 CONSUME_FUNC(consumeUInt16,
uint16_t)
340 CONSUME_FUNC(consumeUInt32,
uint32_t)
341 CONSUME_FUNC(consumeUInt64,
uint64_t)
478 bool isPrefix =
true;
482 bool hasAdSize =
false;
483 bool hasOpSize =
false;
490 if (consumeByte(insn, &
byte))
493 if ((
byte & 0xf0) == 0x40) {
495 if (lookAtByte(insn, &
byte))
497 if ((
byte & 0xf0) == 0x40) {
499 if (consumeByte(insn, &
byte))
531 if (consumeByte(insn, &
byte))
535 && (
byte == 0xf2 ||
byte == 0xf3)) {
537 if (lookAtByte(insn, &nextByte))
547 if (((nextByte == 0xf0) ||
548 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90)))
558 (nextByte == 0x88 || nextByte == 0x89 ||
559 nextByte == 0xc6 || nextByte == 0xc7))
563 if (consumeByte(insn, &nextByte))
565 if (lookAtByte(insn, &nextByte))
579 setPrefixPresent(insn,
byte, prefixLocation);
592 setPrefixPresent(insn,
byte, prefixLocation);
605 setPrefixPresent(insn,
byte, prefixLocation);
618 setPrefixPresent(insn,
byte, prefixLocation);
631 setPrefixPresent(insn,
byte, prefixLocation);
644 setPrefixPresent(insn,
byte, prefixLocation);
657 setPrefixPresent(insn,
byte, prefixLocation);
662 setPrefixPresent(insn,
byte, prefixLocation);
667 setPrefixPresent(insn,
byte, prefixLocation);
685 if (consumeByte(insn, &byte1)) {
691 ((~byte1 & 0xc) == 0xc)) {
692 if (lookAtByte(insn, &byte2)) {
697 if ((byte2 & 0x4) == 0x4) {
743 }
else if (
byte == 0xc4) {
746 if (lookAtByte(insn, &byte1)) {
783 }
else if (
byte == 0xc5) {
786 if (lookAtByte(insn, &byte1)) {
815 }
else if (
byte == 0x8f) {
818 if (lookAtByte(insn, &byte1)) {
823 if ((byte1 & 0x38) != 0x0) {
857 if ((
byte & 0xf0) == 0x40) {
861 if (lookAtByte(insn, &opcodeByte))
863 if ((opcodeByte & 0xf0) == 0x40) {
865 if (consumeByte(insn, &
byte))
889 insn->
immSize = (hasOpSize ? 4 : 2);
895 insn->
immSize = (hasOpSize ? 2 : 4);
908 insn->
immSize = (hasOpSize ? 2 : 4);
914 insn->
immSize = (hasOpSize ? 4 : 8);
948 return consumeByte(insn, &insn->
opcode);
951 return consumeByte(insn, &insn->
opcode);
954 return consumeByte(insn, &insn->
opcode);
965 return consumeByte(insn, &insn->
opcode);
970 return consumeByte(insn, &insn->
opcode);
975 return consumeByte(insn, &insn->
opcode);
980 return consumeByte(insn, &insn->
opcode);
990 return consumeByte(insn, &insn->
opcode);
994 return consumeByte(insn, &insn->
opcode);
998 return consumeByte(insn, &insn->
opcode);
1002 if (consumeByte(insn, ¤t))
1008 if (current == 0x0f) {
1013 if (consumeByte(insn, ¤t))
1016 if (current == 0x38) {
1021 if (consumeByte(insn, ¤t))
1025 }
else if (current == 0x3a) {
1030 if (consumeByte(insn, ¤t))
1035 #ifndef CAPSTONE_X86_REDUCE
1048 if (readModRM(insn))
1051 if (consumeByte(insn, ¤t))
1071 #define GET_INSTRINFO_ENUM
1072 #ifndef CAPSTONE_X86_REDUCE
1090 static int getIDWithAttrMask(
uint16_t *instructionID,
1094 bool hasModRMExtension;
1098 #ifndef CAPSTONE_X86_REDUCE
1101 *instructionID = X86_FEMMS;
1107 instructionClass = IC_OF;
1109 instructionClass = contextForAttrs(attrMask);
1111 hasModRMExtension = modRMRequired(insn->
opcodeType,
1115 if (hasModRMExtension) {
1116 if (readModRM(insn))
1140 static bool is16BitEquivalent(
unsigned orig,
unsigned equiv)
1145 if ((
idx = x86_16_bit_eq_lookup[orig]) != 0) {
1146 for (
i =
idx - 1;
i <
ARR_SIZE(x86_16_bit_eq_tbl) && x86_16_bit_eq_tbl[
i].first == orig;
i++) {
1147 if (x86_16_bit_eq_tbl[
i].second == equiv)
1162 return is_64bit_insn[
id];
1180 attrMask = ATTR_NONE;
1183 attrMask |= ATTR_64BIT;
1191 attrMask |= ATTR_OPSIZE;
1194 attrMask |= ATTR_XS;
1197 attrMask |= ATTR_XD;
1202 attrMask |= ATTR_EVEXKZ;
1204 attrMask |= ATTR_EVEXB;
1206 attrMask |= ATTR_EVEXK;
1208 attrMask |= ATTR_EVEXL;
1210 attrMask |= ATTR_EVEXL2;
1214 attrMask |= ATTR_OPSIZE;
1217 attrMask |= ATTR_XS;
1220 attrMask |= ATTR_XD;
1225 attrMask |= ATTR_VEXL;
1229 attrMask |= ATTR_OPSIZE;
1232 attrMask |= ATTR_XS;
1235 attrMask |= ATTR_XD;
1240 attrMask |= ATTR_VEXL;
1244 attrMask |= ATTR_OPSIZE;
1247 attrMask |= ATTR_XS;
1250 attrMask |= ATTR_XD;
1255 attrMask |= ATTR_VEXL;
1261 attrMask |= ATTR_OPSIZE;
1263 attrMask |= ATTR_ADSIZE;
1265 attrMask |= ATTR_XS;
1267 attrMask |= ATTR_XD;
1272 attrMask |= ATTR_REXW;
1280 attrMask ^= ATTR_ADSIZE;
1282 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1299 if (getIDWithAttrMask(&instructionIDWithREXW,
1300 insn, attrMask | ATTR_REXW)) {
1302 insn->
spec = specifierForUID(instructionID);
1308 if (!is64Bit(instructionIDWithREXW)) {
1310 insn->
spec = specifierForUID(instructionIDWithREXW);
1327 attrMask |= ATTR_ADSIZE;
1329 attrMask |= ATTR_OPSIZE;
1333 attrMask ^= ATTR_ADSIZE | ATTR_OPSIZE;
1335 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1339 insn->
spec = specifierForUID(instructionID);
1345 !(attrMask & ATTR_OPSIZE)) {
1357 spec = specifierForUID(instructionID);
1359 if (getIDWithAttrMask(&instructionIDWithOpsize,
1360 insn, attrMask | ATTR_OPSIZE)) {
1371 if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1374 insn->
spec = specifierForUID(instructionIDWithOpsize);
1390 uint16_t instructionIDWithNewOpcode;
1393 spec = specifierForUID(instructionID);
1398 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1408 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1414 insn->
spec = specWithNewOpcode;
1450 sibIndexBase = SIB_INDEX_EAX;
1451 sibBaseBase = SIB_BASE_EAX;
1454 sibIndexBase = SIB_INDEX_RAX;
1455 sibBaseBase = SIB_BASE_RAX;
1459 if (consumeByte(insn, &insn->
sib))
1472 if (insn->
sibIndex == SIB_INDEX_sib ||
1550 if (consumeInt8(insn, &
d8))
1555 if (consumeInt16(insn, &
d16))
1560 if (consumeInt32(insn, &
d32))
1588 if (consumeByte(insn, &insn->
modRM))
1600 insn->
modRM |= 0xC0;
1617 insn->
regBase = MODRM_REG_EAX;
1621 insn->
regBase = MODRM_REG_RAX;
1644 if (readDisplacement(insn))
1655 if (readDisplacement(insn))
1661 if (readDisplacement(insn))
1667 if (readDisplacement(insn))
1684 EA_BASE_sib : EA_BASE_sib64);
1685 if (readSIB(insn) || readDisplacement(insn))
1692 if (readDisplacement(insn))
1710 insn->
eaBase = EA_BASE_sib;
1711 if (readSIB(insn) || readDisplacement(insn))
1716 if (readDisplacement(insn))
1732 #define GENERIC_FIXUP_FUNC(name, base, prefix) \
1733 static uint8_t name(struct InternalInstruction *insn, \
1743 return base + index; \
1745 if (insn->rexPrefix && \
1746 index >= 4 && index <= 7) { \
1747 return prefix##_SPL + (index - 4); \
1749 return prefix##_AL + index; \
1752 return prefix##_AX + index; \
1754 return prefix##_EAX + index; \
1756 return prefix##_RAX + index; \
1758 return prefix##_ZMM0 + index; \
1760 return prefix##_YMM0 + index; \
1765 return prefix##_XMM0 + index; \
1771 return prefix##_K0 + index; \
1773 return prefix##_MM0 + (index & 0x7); \
1774 case TYPE_SEGMENTREG: \
1777 return prefix##_ES + index; \
1778 case TYPE_DEBUGREG: \
1779 return prefix##_DR0 + index; \
1780 case TYPE_CONTROLREG: \
1781 return prefix##_CR0 + index; \
1799 GENERIC_FIXUP_FUNC(fixupRegValue, insn->
regBase, MODRM_REG)
1800 GENERIC_FIXUP_FUNC(fixupRMValue, insn->
eaRegBase, EA_REG)
1823 insn->
vvvv = (
Reg)fixupRegValue(insn,
1831 insn->
reg = (
Reg)fixupRegValue(insn,
1936 if (consumeByte(insn, &imm8))
1941 if (consumeUInt16(insn, &imm16))
1946 if (consumeUInt32(insn, &imm32))
1951 if (consumeUInt64(insn, &imm64))
2023 int hasVVVV, needVVVV;
2029 hasVVVV = !readVVVV(insn);
2030 needVVVV = hasVVVV && (insn->
vvvv != 0);
2034 switch (x86OperandSets[insn->
spec->
operands][index].encoding) {
2041 if (readModRM(insn))
2043 if (fixupReg(insn, &x86OperandSets[insn->
spec->
operands][index]))
2066 if (readImmediate(insn, 1))
2068 if (x86OperandSets[insn->
spec->
operands][index].type == TYPE_XMM128 ||
2069 x86OperandSets[insn->
spec->
operands][index].type == TYPE_XMM256)
2073 if (readImmediate(insn, 2))
2077 if (readImmediate(insn, 4))
2081 if (readImmediate(insn, 8))
2102 if (readOpcodeRegister(insn, 1))
2106 if (readOpcodeRegister(insn, 2))
2110 if (readOpcodeRegister(insn, 4))
2114 if (readOpcodeRegister(insn, 8))
2118 if (readOpcodeRegister(insn, 0))
2127 if (fixupReg(insn, &x86OperandSets[insn->
spec->
operands][index]))
2130 case ENCODING_WRITEMASK:
2131 if (readMaskRegister(insn))
2143 if (needVVVV)
return -1;
2248 case X86_CMPXCHG16B:
2249 case X86_CMPXCHG16rm:
2250 case X86_CMPXCHG32rm:
2251 case X86_CMPXCHG64rm:
2252 case X86_CMPXCHG8rm:
2280 case X86_OR32mrLocked:
2389 const void *readerArg,
2399 if (readPrefixes(insn) ||
2403 checkPrefix(insn) ||
#define rFromEVEX2of4(evex)
#define lFromEVEX4of4(evex)
#define l2FromEVEX4of4(evex)
#define rFromVEX2of3(vex)
#define zFromEVEX4of4(evex)
#define bFromXOP2of3(xop)
#define xFromVEX2of3(vex)
#define mmmmmFromVEX2of3(vex)
#define rmFromModRM(modRM)
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader, const void *readerArg, uint64_t startLoc, DisassemblerMode mode)
#define bFromEVEX4of4(evex)
#define rFromVEX2of2(vex)
#define ppFromEVEX3of4(evex)
#define v2FromEVEX4of4(evex)
#define modFromModRM(modRM)
#define rFromXOP2of3(xop)
#define lFromXOP3of3(xop)
#define lFromVEX2of2(vex)
#define scaleFromSIB(sib)
#define regFromModRM(modRM)
#define vvvvFromVEX2of2(vex)
#define ppFromXOP3of3(xop)
#define vvvvFromVEX3of3(vex)
#define r2FromEVEX2of4(evex)
#define mmFromEVEX2of4(evex)
#define xFromXOP2of3(xop)
#define wFromEVEX3of4(evex)
#define bFromVEX2of3(vex)
#define wFromVEX3of3(vex)
int(* byteReader_t)(const struct reader_info *arg, uint8_t *byte, uint64_t address)
#define mmmmmFromXOP2of3(xop)
#define aaaFromEVEX4of4(evex)
#define lFromVEX3of3(vex)
#define ppFromVEX3of3(vex)
#define bFromEVEX2of4(evex)
#define xFromEVEX2of4(evex)
#define vvvvFromXOP3of3(vex)
#define ppFromVEX2of2(vex)
#define indexFromSIB(sib)
#define wFromXOP3of3(xop)
#define vvvvFromEVEX3of4(evex)
int(* decode)(const ut8 *, ebc_command_t *cmd)
unsigned short prefix[65536]
EADisplacement eaDisplacement
const struct InstructionSpecifier * spec
uint8_t numImmediatesConsumed
uint64_t necessaryPrefixLocation
uint8_t displacementOffset
VectorExtensionType vectorExtensionType
uint8_t vectorExtensionPrefix[4]
const struct OperandSpecifier * operands
bool consumedDisplacement
SegmentOverride segmentOverride