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
57 #include "X86GenDisassemblerTables_reduce.inc"
59 #include "X86GenDisassemblerTables.inc"
63 #define GET_INSTRINFO_MC_DESC
64 #ifdef CAPSTONE_X86_REDUCE
65 #include "X86GenInstrInfo_reduce.inc"
67 #include "X86GenInstrInfo.inc"
98 const struct OpcodeDecision *decision = NULL;
99 const uint8_t *indextable = 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;
162 const uint8_t *indextable = NULL;
168 indextable = index_x86DisassemblerOneByteOpcodes;
169 index = indextable[insnContext];
173 dec = &emptyTable.modRMDecisions[
opcode];
176 indextable = index_x86DisassemblerTwoByteOpcodes;
177 index = indextable[insnContext];
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) == 0
x86 || (nextByte & 0xf8) == 0x90)))
557 (nextByte == 0x88 || nextByte == 0x89 ||
558 nextByte == 0xc6 || nextByte == 0xc7))
562 if (consumeByte(insn, &nextByte))
564 if (lookAtByte(insn, &nextByte))
578 setPrefixPresent(insn,
byte, prefixLocation);
591 setPrefixPresent(insn,
byte, prefixLocation);
604 setPrefixPresent(insn,
byte, prefixLocation);
617 setPrefixPresent(insn,
byte, prefixLocation);
630 setPrefixPresent(insn,
byte, prefixLocation);
643 setPrefixPresent(insn,
byte, prefixLocation);
656 setPrefixPresent(insn,
byte, prefixLocation);
661 setPrefixPresent(insn,
byte, prefixLocation);
666 setPrefixPresent(insn,
byte, prefixLocation);
684 if (consumeByte(insn, &byte1)) {
690 ((~byte1 & 0xc) == 0xc)) {
691 if (lookAtByte(insn, &byte2)) {
696 if ((byte2 & 0x4) == 0x4) {
742 }
else if (
byte == 0xc4) {
745 if (lookAtByte(insn, &byte1)) {
782 }
else if (
byte == 0xc5) {
785 if (lookAtByte(insn, &byte1)) {
814 }
else if (
byte == 0x8f) {
817 if (lookAtByte(insn, &byte1)) {
822 if ((byte1 & 0x38) != 0x0) {
856 if ((
byte & 0xf0) == 0x40) {
860 if (lookAtByte(insn, &opcodeByte))
862 if ((opcodeByte & 0xf0) == 0x40) {
864 if (consumeByte(insn, &
byte))
888 insn->
immSize = (hasOpSize ? 4 : 2);
894 insn->
immSize = (hasOpSize ? 2 : 4);
907 insn->
immSize = (hasOpSize ? 2 : 4);
913 insn->
immSize = (hasOpSize ? 4 : 8);
947 return consumeByte(insn, &insn->
opcode);
950 return consumeByte(insn, &insn->
opcode);
953 return consumeByte(insn, &insn->
opcode);
964 return consumeByte(insn, &insn->
opcode);
969 return consumeByte(insn, &insn->
opcode);
974 return consumeByte(insn, &insn->
opcode);
979 return consumeByte(insn, &insn->
opcode);
989 return consumeByte(insn, &insn->
opcode);
993 return consumeByte(insn, &insn->
opcode);
997 return consumeByte(insn, &insn->
opcode);
1001 if (consumeByte(insn, ¤t))
1007 if (current == 0x0f) {
1012 if (consumeByte(insn, ¤t))
1015 if (current == 0x38) {
1020 if (consumeByte(insn, ¤t))
1024 }
else if (current == 0x3a) {
1029 if (consumeByte(insn, ¤t))
1034 #ifndef CAPSTONE_X86_REDUCE
1047 if (readModRM(insn))
1050 if (consumeByte(insn, ¤t))
1070 #define GET_INSTRINFO_ENUM
1071 #ifndef CAPSTONE_X86_REDUCE
1072 #include "X86GenInstrInfo.inc"
1074 #include "X86GenInstrInfo_reduce.inc"
1089 static int getIDWithAttrMask(
uint16_t *instructionID,
1093 bool hasModRMExtension;
1097 #ifndef CAPSTONE_X86_REDUCE
1100 *instructionID = X86_FEMMS;
1106 instructionClass = IC_OF;
1108 instructionClass = contextForAttrs(attrMask);
1110 hasModRMExtension = modRMRequired(insn->
opcodeType,
1114 if (hasModRMExtension) {
1115 if (readModRM(insn))
1139 static bool is16BitEquivalent(
unsigned orig,
unsigned equiv)
1144 if ((
idx = x86_16_bit_eq_lookup[orig]) != 0) {
1145 for (
i =
idx - 1;
i <
ARR_SIZE(x86_16_bit_eq_tbl) && x86_16_bit_eq_tbl[
i].first == orig;
i++) {
1146 if (x86_16_bit_eq_tbl[
i].
second == equiv)
1161 return is_64bit_insn[
id];
1179 attrMask = ATTR_NONE;
1182 attrMask |= ATTR_64BIT;
1190 attrMask |= ATTR_OPSIZE;
1193 attrMask |= ATTR_XS;
1196 attrMask |= ATTR_XD;
1201 attrMask |= ATTR_EVEXKZ;
1203 attrMask |= ATTR_EVEXB;
1205 attrMask |= ATTR_EVEXK;
1207 attrMask |= ATTR_EVEXL;
1209 attrMask |= ATTR_EVEXL2;
1213 attrMask |= ATTR_OPSIZE;
1216 attrMask |= ATTR_XS;
1219 attrMask |= ATTR_XD;
1224 attrMask |= ATTR_VEXL;
1228 attrMask |= ATTR_OPSIZE;
1231 attrMask |= ATTR_XS;
1234 attrMask |= ATTR_XD;
1239 attrMask |= ATTR_VEXL;
1243 attrMask |= ATTR_OPSIZE;
1246 attrMask |= ATTR_XS;
1249 attrMask |= ATTR_XD;
1254 attrMask |= ATTR_VEXL;
1260 attrMask |= ATTR_OPSIZE;
1262 attrMask |= ATTR_ADSIZE;
1264 attrMask |= ATTR_XS;
1266 attrMask |= ATTR_XD;
1271 attrMask |= ATTR_REXW;
1279 attrMask ^= ATTR_ADSIZE;
1281 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1298 if (getIDWithAttrMask(&instructionIDWithREXW,
1299 insn, attrMask | ATTR_REXW)) {
1301 insn->
spec = specifierForUID(instructionID);
1307 if (!is64Bit(instructionIDWithREXW)) {
1309 insn->
spec = specifierForUID(instructionIDWithREXW);
1326 attrMask |= ATTR_ADSIZE;
1328 attrMask |= ATTR_OPSIZE;
1332 attrMask ^= ATTR_ADSIZE | ATTR_OPSIZE;
1334 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1338 insn->
spec = specifierForUID(instructionID);
1344 !(attrMask & ATTR_OPSIZE)) {
1356 spec = specifierForUID(instructionID);
1358 if (getIDWithAttrMask(&instructionIDWithOpsize,
1359 insn, attrMask | ATTR_OPSIZE)) {
1370 if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1373 insn->
spec = specifierForUID(instructionIDWithOpsize);
1389 uint16_t instructionIDWithNewOpcode;
1392 spec = specifierForUID(instructionID);
1397 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1407 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1413 insn->
spec = specWithNewOpcode;
1449 sibIndexBase = SIB_INDEX_EAX;
1450 sibBaseBase = SIB_BASE_EAX;
1453 sibIndexBase = SIB_INDEX_RAX;
1454 sibBaseBase = SIB_BASE_RAX;
1458 if (consumeByte(insn, &insn->
sib))
1471 if (insn->
sibIndex == SIB_INDEX_sib ||
1549 if (consumeInt8(insn, &d8))
1554 if (consumeInt16(insn, &d16))
1559 if (consumeInt32(insn, &d32))
1587 if (consumeByte(insn, &insn->
modRM))
1599 insn->
modRM |= 0xC0;
1616 insn->
regBase = MODRM_REG_EAX;
1620 insn->
regBase = MODRM_REG_RAX;
1643 if (readDisplacement(insn))
1654 if (readDisplacement(insn))
1660 if (readDisplacement(insn))
1666 if (readDisplacement(insn))
1683 EA_BASE_sib : EA_BASE_sib64);
1684 if (readSIB(insn) || readDisplacement(insn))
1691 if (readDisplacement(insn))
1709 insn->
eaBase = EA_BASE_sib;
1710 if (readSIB(insn) || readDisplacement(insn))
1715 if (readDisplacement(insn))
1731 #define GENERIC_FIXUP_FUNC(name, base, prefix) \
1732 static uint8_t name(struct InternalInstruction *insn, \
1742 return base + index; \
1744 if (insn->rexPrefix && \
1745 index >= 4 && index <= 7) { \
1746 return prefix##_SPL + (index - 4); \
1748 return prefix##_AL + index; \
1751 return prefix##_AX + index; \
1753 return prefix##_EAX + index; \
1755 return prefix##_RAX + index; \
1757 return prefix##_ZMM0 + index; \
1759 return prefix##_YMM0 + index; \
1764 return prefix##_XMM0 + index; \
1770 return prefix##_K0 + index; \
1772 return prefix##_MM0 + (index & 0x7); \
1773 case TYPE_SEGMENTREG: \
1776 return prefix##_ES + index; \
1777 case TYPE_DEBUGREG: \
1778 return prefix##_DR0 + index; \
1779 case TYPE_CONTROLREG: \
1780 return prefix##_CR0 + index; \
1798 GENERIC_FIXUP_FUNC(fixupRegValue, insn->
regBase, MODRM_REG)
1799 GENERIC_FIXUP_FUNC(fixupRMValue, insn->
eaRegBase, EA_REG)
1822 insn->
vvvv = (
Reg)fixupRegValue(insn,
1830 insn->
reg = (
Reg)fixupRegValue(insn,
1935 if (consumeByte(insn, &imm8))
1940 if (consumeUInt16(insn, &imm16))
1945 if (consumeUInt32(insn, &imm32))
1950 if (consumeUInt64(insn, &imm64))
2022 int hasVVVV, needVVVV;
2028 hasVVVV = !readVVVV(insn);
2029 needVVVV = hasVVVV && (insn->
vvvv != 0);
2040 if (readModRM(insn))
2065 if (readImmediate(insn, 1))
2072 if (readImmediate(insn, 2))
2076 if (readImmediate(insn, 4))
2080 if (readImmediate(insn, 8))
2101 if (readOpcodeRegister(insn, 1))
2105 if (readOpcodeRegister(insn, 2))
2109 if (readOpcodeRegister(insn, 4))
2113 if (readOpcodeRegister(insn, 8))
2117 if (readOpcodeRegister(insn, 0))
2129 case ENCODING_WRITEMASK:
2130 if (readMaskRegister(insn))
2142 if (needVVVV)
return -1;
2234 case X86_CMPXCHG16B:
2235 case X86_CMPXCHG16rm:
2236 case X86_CMPXCHG32rm:
2237 case X86_CMPXCHG64rm:
2238 case X86_CMPXCHG8rm:
2266 case X86_OR32mrLocked:
2363 const void *readerArg,
2373 if (readPrefixes(insn) ||
2377 checkPrefix(insn) ||
2399 printf(
"\n>>> x86OperandSets = %lu\n",
sizeof(x86OperandSets));
2400 printf(
">>> x86DisassemblerInstrSpecifiers = %lu\n",
sizeof(x86DisassemblerInstrSpecifiers));
2401 printf(
">>> x86DisassemblerContexts = %lu\n",
sizeof(x86DisassemblerContexts));
2402 printf(
">>> modRMTable = %lu\n",
sizeof(modRMTable));
2403 printf(
">>> x86DisassemblerOneByteOpcodes = %lu\n",
sizeof(x86DisassemblerOneByteOpcodes));
2404 printf(
">>> x86DisassemblerTwoByteOpcodes = %lu\n",
sizeof(x86DisassemblerTwoByteOpcodes));
2405 printf(
">>> x86DisassemblerThreeByte38Opcodes = %lu\n",
sizeof(x86DisassemblerThreeByte38Opcodes));
2406 printf(
">>> x86DisassemblerThreeByte3AOpcodes = %lu\n",
sizeof(x86DisassemblerThreeByte3AOpcodes));
2407 printf(
">>> x86DisassemblerThreeByteA6Opcodes = %lu\n",
sizeof(x86DisassemblerThreeByteA6Opcodes));
2408 printf(
">>> x86DisassemblerThreeByteA7Opcodes= %lu\n",
sizeof(x86DisassemblerThreeByteA7Opcodes));
2409 printf(
">>> x86DisassemblerXOP8Opcodes = %lu\n",
sizeof(x86DisassemblerXOP8Opcodes));
2410 printf(
">>> x86DisassemblerXOP9Opcodes = %lu\n",
sizeof(x86DisassemblerXOP9Opcodes));
2411 printf(
">>> x86DisassemblerXOPAOpcodes = %lu\n\n",
sizeof(x86DisassemblerXOPAOpcodes));