1 //===-- X86DisassemblerDecoder.cpp - Disassembler decoder -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is part of the X86 Disassembler.
10 // It contains the implementation of the instruction decoder.
11 // Documentation for the disassembler can be found in X86Disassembler.h.
13 //===----------------------------------------------------------------------===//
15 #include "X86DisassemblerDecoder.h"
16 #include "llvm/ADT/StringRef.h"
18 #include <cstdarg> /* for va_*() */
19 #include <cstdio> /* for vsnprintf() */
20 #include <cstdlib> /* for exit() */
21 #include <cstring> /* for memset() */
23 using namespace llvm::X86Disassembler
;
25 /// Specifies whether a ModR/M byte is needed and (if so) which
26 /// instruction each possible value of the ModR/M byte corresponds to. Once
27 /// this information is known, we have narrowed down to a single instruction.
28 struct ModRMDecision
{
30 uint16_t instructionIDs
;
33 /// Specifies which set of ModR/M->instruction tables to look at
34 /// given a particular opcode.
35 struct OpcodeDecision
{
36 ModRMDecision modRMDecisions
[256];
39 /// Specifies which opcode->instruction tables to look at given
40 /// a particular context (set of attributes). Since there are many possible
41 /// contexts, the decoder first uses CONTEXTS_SYM to determine which context
42 /// applies given a specific set of attributes. Hence there are only IC_max
43 /// entries in this table, rather than 2^(ATTR_max).
44 struct ContextDecision
{
45 OpcodeDecision opcodeDecisions
[IC_max
];
48 #include "X86GenDisassemblerTables.inc"
51 #define debug(s) do { Debug(__FILE__, __LINE__, s); } while (0)
53 #define debug(s) do { } while (0)
57 * contextForAttrs - Client for the instruction context table. Takes a set of
58 * attributes and returns the appropriate decode context.
60 * @param attrMask - Attributes, from the enumeration attributeBits.
61 * @return - The InstructionContext to use when looking up an
62 * an instruction with these attributes.
64 static InstructionContext
contextForAttrs(uint16_t attrMask
) {
65 return static_cast<InstructionContext
>(CONTEXTS_SYM
[attrMask
]);
69 * modRMRequired - Reads the appropriate instruction table to determine whether
70 * the ModR/M byte is required to decode a particular instruction.
72 * @param type - The opcode type (i.e., how many bytes it has).
73 * @param insnContext - The context for the instruction, as returned by
75 * @param opcode - The last byte of the instruction's opcode, not counting
76 * ModR/M extensions and escapes.
77 * @return - true if the ModR/M byte is required, false otherwise.
79 static int modRMRequired(OpcodeType type
,
80 InstructionContext insnContext
,
82 const struct ContextDecision
* decision
= nullptr;
86 decision
= &ONEBYTE_SYM
;
89 decision
= &TWOBYTE_SYM
;
92 decision
= &THREEBYTE38_SYM
;
95 decision
= &THREEBYTE3A_SYM
;
98 decision
= &XOP8_MAP_SYM
;
101 decision
= &XOP9_MAP_SYM
;
104 decision
= &XOPA_MAP_SYM
;
107 decision
= &THREEDNOW_MAP_SYM
;
111 return decision
->opcodeDecisions
[insnContext
].modRMDecisions
[opcode
].
112 modrm_type
!= MODRM_ONEENTRY
;
116 * decode - Reads the appropriate instruction table to obtain the unique ID of
119 * @param type - See modRMRequired().
120 * @param insnContext - See modRMRequired().
121 * @param opcode - See modRMRequired().
122 * @param modRM - The ModR/M byte if required, or any value if not.
123 * @return - The UID of the instruction, or 0 on failure.
125 static InstrUID
decode(OpcodeType type
,
126 InstructionContext insnContext
,
129 const struct ModRMDecision
* dec
= nullptr;
133 dec
= &ONEBYTE_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
136 dec
= &TWOBYTE_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
139 dec
= &THREEBYTE38_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
142 dec
= &THREEBYTE3A_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
145 dec
= &XOP8_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
148 dec
= &XOP9_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
151 dec
= &XOPA_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
154 dec
= &THREEDNOW_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
158 switch (dec
->modrm_type
) {
160 debug("Corrupt table! Unknown modrm_type");
163 return modRMTable
[dec
->instructionIDs
];
165 if (modFromModRM(modRM
) == 0x3)
166 return modRMTable
[dec
->instructionIDs
+1];
167 return modRMTable
[dec
->instructionIDs
];
169 if (modFromModRM(modRM
) == 0x3)
170 return modRMTable
[dec
->instructionIDs
+((modRM
& 0x38) >> 3)+8];
171 return modRMTable
[dec
->instructionIDs
+((modRM
& 0x38) >> 3)];
172 case MODRM_SPLITMISC
:
173 if (modFromModRM(modRM
) == 0x3)
174 return modRMTable
[dec
->instructionIDs
+(modRM
& 0x3f)+8];
175 return modRMTable
[dec
->instructionIDs
+((modRM
& 0x38) >> 3)];
177 return modRMTable
[dec
->instructionIDs
+modRM
];
182 * specifierForUID - Given a UID, returns the name and operand specification for
185 * @param uid - The unique ID for the instruction. This should be returned by
186 * decode(); specifierForUID will not check bounds.
187 * @return - A pointer to the specification for that instruction.
189 static const struct InstructionSpecifier
*specifierForUID(InstrUID uid
) {
190 return &INSTRUCTIONS_SYM
[uid
];
194 * consumeByte - Uses the reader function provided by the user to consume one
195 * byte from the instruction's memory and advance the cursor.
197 * @param insn - The instruction with the reader function to use. The cursor
198 * for this instruction is advanced.
199 * @param byte - A pointer to a pre-allocated memory buffer to be populated
200 * with the data read.
201 * @return - 0 if the read was successful; nonzero otherwise.
203 static int consumeByte(struct InternalInstruction
* insn
, uint8_t* byte
) {
204 int ret
= insn
->reader(insn
->readerArg
, byte
, insn
->readerCursor
);
207 ++(insn
->readerCursor
);
213 * lookAtByte - Like consumeByte, but does not advance the cursor.
215 * @param insn - See consumeByte().
216 * @param byte - See consumeByte().
217 * @return - See consumeByte().
219 static int lookAtByte(struct InternalInstruction
* insn
, uint8_t* byte
) {
220 return insn
->reader(insn
->readerArg
, byte
, insn
->readerCursor
);
223 static void unconsumeByte(struct InternalInstruction
* insn
) {
224 insn
->readerCursor
--;
227 #define CONSUME_FUNC(name, type) \
228 static int name(struct InternalInstruction* insn, type* ptr) { \
231 for (offset = 0; offset < sizeof(type); ++offset) { \
233 int ret = insn->reader(insn->readerArg, \
235 insn->readerCursor + offset); \
238 combined = combined | ((uint64_t)byte << (offset * 8)); \
241 insn->readerCursor += sizeof(type); \
246 * consume* - Use the reader function provided by the user to consume data
247 * values of various sizes from the instruction's memory and advance the
248 * cursor appropriately. These readers perform endian conversion.
250 * @param insn - See consumeByte().
251 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
252 * be populated with the data read.
253 * @return - See consumeByte().
255 CONSUME_FUNC(consumeInt8
, int8_t)
256 CONSUME_FUNC(consumeInt16
, int16_t)
257 CONSUME_FUNC(consumeInt32
, int32_t)
258 CONSUME_FUNC(consumeUInt16
, uint16_t)
259 CONSUME_FUNC(consumeUInt32
, uint32_t)
260 CONSUME_FUNC(consumeUInt64
, uint64_t)
263 * dbgprintf - Uses the logging function provided by the user to log a single
264 * message, typically without a carriage-return.
266 * @param insn - The instruction containing the logging function.
267 * @param format - See printf().
268 * @param ... - See printf().
270 static void dbgprintf(struct InternalInstruction
* insn
,
279 va_start(ap
, format
);
280 (void)vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
283 insn
->dlog(insn
->dlogArg
, buffer
);
286 static bool isREX(struct InternalInstruction
*insn
, uint8_t prefix
) {
287 if (insn
->mode
== MODE_64BIT
)
288 return prefix
>= 0x40 && prefix
<= 0x4f;
293 * setPrefixPresent - Marks that a particular prefix is present as mandatory
295 * @param insn - The instruction to be marked as having the prefix.
296 * @param prefix - The prefix that is present.
298 static void setPrefixPresent(struct InternalInstruction
*insn
, uint8_t prefix
) {
302 insn
->hasLockPrefix
= true;
306 if (lookAtByte(insn
, &nextByte
))
309 // 1. There could be several 0x66
310 // 2. if (nextByte == 0x66) and nextNextByte != 0x0f then
311 // it's not mandatory prefix
312 // 3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
313 // 0x0f exactly after it to be mandatory prefix
314 if (isREX(insn
, nextByte
) || nextByte
== 0x0f || nextByte
== 0x66)
315 // The last of 0xf2 /0xf3 is mandatory prefix
316 insn
->mandatoryPrefix
= prefix
;
317 insn
->repeatPrefix
= prefix
;
320 if (lookAtByte(insn
, &nextByte
))
322 // 0x66 can't overwrite existing mandatory prefix and should be ignored
323 if (!insn
->mandatoryPrefix
&& (nextByte
== 0x0f || isREX(insn
, nextByte
)))
324 insn
->mandatoryPrefix
= prefix
;
330 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
331 * instruction as having them. Also sets the instruction's default operand,
332 * address, and other relevant data sizes to report operands correctly.
334 * @param insn - The instruction whose prefixes are to be read.
335 * @return - 0 if the instruction could be read until the end of the prefix
336 * bytes, and no prefixes conflicted; nonzero otherwise.
338 static int readPrefixes(struct InternalInstruction
* insn
) {
339 bool isPrefix
= true;
343 dbgprintf(insn
, "readPrefixes()");
346 /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
347 if (consumeByte(insn
, &byte
))
351 * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
352 * break and let it be disassembled as a normal "instruction".
354 if (insn
->readerCursor
- 1 == insn
->startLocation
&& byte
== 0xf0) // LOCK
357 if ((byte
== 0xf2 || byte
== 0xf3) && !lookAtByte(insn
, &nextByte
)) {
359 * If the byte is 0xf2 or 0xf3, and any of the following conditions are
361 * - it is followed by a LOCK (0xf0) prefix
362 * - it is followed by an xchg instruction
363 * then it should be disassembled as a xacquire/xrelease not repne/rep.
365 if (((nextByte
== 0xf0) ||
366 ((nextByte
& 0xfe) == 0x86 || (nextByte
& 0xf8) == 0x90))) {
367 insn
->xAcquireRelease
= true;
368 if (!(byte
== 0xf3 && nextByte
== 0x90)) // PAUSE instruction support
372 * Also if the byte is 0xf3, and the following condition is met:
373 * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
374 * "mov mem, imm" (opcode 0xc6/0xc7) instructions.
375 * then it should be disassembled as an xrelease not rep.
377 if (byte
== 0xf3 && (nextByte
== 0x88 || nextByte
== 0x89 ||
378 nextByte
== 0xc6 || nextByte
== 0xc7)) {
379 insn
->xAcquireRelease
= true;
382 if (isREX(insn
, nextByte
)) {
384 // Go to REX prefix after the current one
385 if (consumeByte(insn
, &nnextByte
))
387 // We should be able to read next byte after REX prefix
388 if (lookAtByte(insn
, &nnextByte
))
395 case 0xf0: /* LOCK */
396 case 0xf2: /* REPNE/REPNZ */
397 case 0xf3: /* REP or REPE/REPZ */
398 setPrefixPresent(insn
, byte
);
400 case 0x2e: /* CS segment override -OR- Branch not taken */
401 case 0x36: /* SS segment override -OR- Branch taken */
402 case 0x3e: /* DS segment override */
403 case 0x26: /* ES segment override */
404 case 0x64: /* FS segment override */
405 case 0x65: /* GS segment override */
408 insn
->segmentOverride
= SEG_OVERRIDE_CS
;
411 insn
->segmentOverride
= SEG_OVERRIDE_SS
;
414 insn
->segmentOverride
= SEG_OVERRIDE_DS
;
417 insn
->segmentOverride
= SEG_OVERRIDE_ES
;
420 insn
->segmentOverride
= SEG_OVERRIDE_FS
;
423 insn
->segmentOverride
= SEG_OVERRIDE_GS
;
426 debug("Unhandled override");
429 setPrefixPresent(insn
, byte
);
431 case 0x66: /* Operand-size override */
432 insn
->hasOpSize
= true;
433 setPrefixPresent(insn
, byte
);
435 case 0x67: /* Address-size override */
436 insn
->hasAdSize
= true;
437 setPrefixPresent(insn
, byte
);
439 default: /* Not a prefix byte */
445 dbgprintf(insn
, "Found prefix 0x%hhx", byte
);
448 insn
->vectorExtensionType
= TYPE_NO_VEX_XOP
;
451 uint8_t byte1
, byte2
;
453 if (consumeByte(insn
, &byte1
)) {
454 dbgprintf(insn
, "Couldn't read second byte of EVEX prefix");
458 if (lookAtByte(insn
, &byte2
)) {
459 dbgprintf(insn
, "Couldn't read third byte of EVEX prefix");
463 if ((insn
->mode
== MODE_64BIT
|| (byte1
& 0xc0) == 0xc0) &&
464 ((~byte1
& 0xc) == 0xc) && ((byte2
& 0x4) == 0x4)) {
465 insn
->vectorExtensionType
= TYPE_EVEX
;
467 unconsumeByte(insn
); /* unconsume byte1 */
468 unconsumeByte(insn
); /* unconsume byte */
471 if (insn
->vectorExtensionType
== TYPE_EVEX
) {
472 insn
->vectorExtensionPrefix
[0] = byte
;
473 insn
->vectorExtensionPrefix
[1] = byte1
;
474 if (consumeByte(insn
, &insn
->vectorExtensionPrefix
[2])) {
475 dbgprintf(insn
, "Couldn't read third byte of EVEX prefix");
478 if (consumeByte(insn
, &insn
->vectorExtensionPrefix
[3])) {
479 dbgprintf(insn
, "Couldn't read fourth byte of EVEX prefix");
483 /* We simulate the REX prefix for simplicity's sake */
484 if (insn
->mode
== MODE_64BIT
) {
485 insn
->rexPrefix
= 0x40
486 | (wFromEVEX3of4(insn
->vectorExtensionPrefix
[2]) << 3)
487 | (rFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 2)
488 | (xFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 1)
489 | (bFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 0);
492 dbgprintf(insn
, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
493 insn
->vectorExtensionPrefix
[0], insn
->vectorExtensionPrefix
[1],
494 insn
->vectorExtensionPrefix
[2], insn
->vectorExtensionPrefix
[3]);
496 } else if (byte
== 0xc4) {
499 if (lookAtByte(insn
, &byte1
)) {
500 dbgprintf(insn
, "Couldn't read second byte of VEX");
504 if (insn
->mode
== MODE_64BIT
|| (byte1
& 0xc0) == 0xc0)
505 insn
->vectorExtensionType
= TYPE_VEX_3B
;
509 if (insn
->vectorExtensionType
== TYPE_VEX_3B
) {
510 insn
->vectorExtensionPrefix
[0] = byte
;
511 consumeByte(insn
, &insn
->vectorExtensionPrefix
[1]);
512 consumeByte(insn
, &insn
->vectorExtensionPrefix
[2]);
514 /* We simulate the REX prefix for simplicity's sake */
516 if (insn
->mode
== MODE_64BIT
)
517 insn
->rexPrefix
= 0x40
518 | (wFromVEX3of3(insn
->vectorExtensionPrefix
[2]) << 3)
519 | (rFromVEX2of3(insn
->vectorExtensionPrefix
[1]) << 2)
520 | (xFromVEX2of3(insn
->vectorExtensionPrefix
[1]) << 1)
521 | (bFromVEX2of3(insn
->vectorExtensionPrefix
[1]) << 0);
523 dbgprintf(insn
, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
524 insn
->vectorExtensionPrefix
[0], insn
->vectorExtensionPrefix
[1],
525 insn
->vectorExtensionPrefix
[2]);
527 } else if (byte
== 0xc5) {
530 if (lookAtByte(insn
, &byte1
)) {
531 dbgprintf(insn
, "Couldn't read second byte of VEX");
535 if (insn
->mode
== MODE_64BIT
|| (byte1
& 0xc0) == 0xc0)
536 insn
->vectorExtensionType
= TYPE_VEX_2B
;
540 if (insn
->vectorExtensionType
== TYPE_VEX_2B
) {
541 insn
->vectorExtensionPrefix
[0] = byte
;
542 consumeByte(insn
, &insn
->vectorExtensionPrefix
[1]);
544 if (insn
->mode
== MODE_64BIT
)
545 insn
->rexPrefix
= 0x40
546 | (rFromVEX2of2(insn
->vectorExtensionPrefix
[1]) << 2);
548 switch (ppFromVEX2of2(insn
->vectorExtensionPrefix
[1])) {
552 insn
->hasOpSize
= true;
556 dbgprintf(insn
, "Found VEX prefix 0x%hhx 0x%hhx",
557 insn
->vectorExtensionPrefix
[0],
558 insn
->vectorExtensionPrefix
[1]);
560 } else if (byte
== 0x8f) {
563 if (lookAtByte(insn
, &byte1
)) {
564 dbgprintf(insn
, "Couldn't read second byte of XOP");
568 if ((byte1
& 0x38) != 0x0) /* 0 in these 3 bits is a POP instruction. */
569 insn
->vectorExtensionType
= TYPE_XOP
;
573 if (insn
->vectorExtensionType
== TYPE_XOP
) {
574 insn
->vectorExtensionPrefix
[0] = byte
;
575 consumeByte(insn
, &insn
->vectorExtensionPrefix
[1]);
576 consumeByte(insn
, &insn
->vectorExtensionPrefix
[2]);
578 /* We simulate the REX prefix for simplicity's sake */
580 if (insn
->mode
== MODE_64BIT
)
581 insn
->rexPrefix
= 0x40
582 | (wFromXOP3of3(insn
->vectorExtensionPrefix
[2]) << 3)
583 | (rFromXOP2of3(insn
->vectorExtensionPrefix
[1]) << 2)
584 | (xFromXOP2of3(insn
->vectorExtensionPrefix
[1]) << 1)
585 | (bFromXOP2of3(insn
->vectorExtensionPrefix
[1]) << 0);
587 switch (ppFromXOP3of3(insn
->vectorExtensionPrefix
[2])) {
591 insn
->hasOpSize
= true;
595 dbgprintf(insn
, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
596 insn
->vectorExtensionPrefix
[0], insn
->vectorExtensionPrefix
[1],
597 insn
->vectorExtensionPrefix
[2]);
599 } else if (isREX(insn
, byte
)) {
600 if (lookAtByte(insn
, &nextByte
))
602 insn
->rexPrefix
= byte
;
603 dbgprintf(insn
, "Found REX prefix 0x%hhx", byte
);
607 if (insn
->mode
== MODE_16BIT
) {
608 insn
->registerSize
= (insn
->hasOpSize
? 4 : 2);
609 insn
->addressSize
= (insn
->hasAdSize
? 4 : 2);
610 insn
->displacementSize
= (insn
->hasAdSize
? 4 : 2);
611 insn
->immediateSize
= (insn
->hasOpSize
? 4 : 2);
612 } else if (insn
->mode
== MODE_32BIT
) {
613 insn
->registerSize
= (insn
->hasOpSize
? 2 : 4);
614 insn
->addressSize
= (insn
->hasAdSize
? 2 : 4);
615 insn
->displacementSize
= (insn
->hasAdSize
? 2 : 4);
616 insn
->immediateSize
= (insn
->hasOpSize
? 2 : 4);
617 } else if (insn
->mode
== MODE_64BIT
) {
618 if (insn
->rexPrefix
&& wFromREX(insn
->rexPrefix
)) {
619 insn
->registerSize
= 8;
620 insn
->addressSize
= (insn
->hasAdSize
? 4 : 8);
621 insn
->displacementSize
= 4;
622 insn
->immediateSize
= 4;
624 insn
->registerSize
= (insn
->hasOpSize
? 2 : 4);
625 insn
->addressSize
= (insn
->hasAdSize
? 4 : 8);
626 insn
->displacementSize
= (insn
->hasOpSize
? 2 : 4);
627 insn
->immediateSize
= (insn
->hasOpSize
? 2 : 4);
634 static int readModRM(struct InternalInstruction
* insn
);
637 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
638 * extended or escape opcodes).
640 * @param insn - The instruction whose opcode is to be read.
641 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
643 static int readOpcode(struct InternalInstruction
* insn
) {
644 /* Determine the length of the primary opcode */
648 dbgprintf(insn
, "readOpcode()");
650 insn
->opcodeType
= ONEBYTE
;
652 if (insn
->vectorExtensionType
== TYPE_EVEX
) {
653 switch (mmFromEVEX2of4(insn
->vectorExtensionPrefix
[1])) {
655 dbgprintf(insn
, "Unhandled mm field for instruction (0x%hhx)",
656 mmFromEVEX2of4(insn
->vectorExtensionPrefix
[1]));
659 insn
->opcodeType
= TWOBYTE
;
660 return consumeByte(insn
, &insn
->opcode
);
662 insn
->opcodeType
= THREEBYTE_38
;
663 return consumeByte(insn
, &insn
->opcode
);
665 insn
->opcodeType
= THREEBYTE_3A
;
666 return consumeByte(insn
, &insn
->opcode
);
668 } else if (insn
->vectorExtensionType
== TYPE_VEX_3B
) {
669 switch (mmmmmFromVEX2of3(insn
->vectorExtensionPrefix
[1])) {
671 dbgprintf(insn
, "Unhandled m-mmmm field for instruction (0x%hhx)",
672 mmmmmFromVEX2of3(insn
->vectorExtensionPrefix
[1]));
675 insn
->opcodeType
= TWOBYTE
;
676 return consumeByte(insn
, &insn
->opcode
);
678 insn
->opcodeType
= THREEBYTE_38
;
679 return consumeByte(insn
, &insn
->opcode
);
681 insn
->opcodeType
= THREEBYTE_3A
;
682 return consumeByte(insn
, &insn
->opcode
);
684 } else if (insn
->vectorExtensionType
== TYPE_VEX_2B
) {
685 insn
->opcodeType
= TWOBYTE
;
686 return consumeByte(insn
, &insn
->opcode
);
687 } else if (insn
->vectorExtensionType
== TYPE_XOP
) {
688 switch (mmmmmFromXOP2of3(insn
->vectorExtensionPrefix
[1])) {
690 dbgprintf(insn
, "Unhandled m-mmmm field for instruction (0x%hhx)",
691 mmmmmFromVEX2of3(insn
->vectorExtensionPrefix
[1]));
693 case XOP_MAP_SELECT_8
:
694 insn
->opcodeType
= XOP8_MAP
;
695 return consumeByte(insn
, &insn
->opcode
);
696 case XOP_MAP_SELECT_9
:
697 insn
->opcodeType
= XOP9_MAP
;
698 return consumeByte(insn
, &insn
->opcode
);
699 case XOP_MAP_SELECT_A
:
700 insn
->opcodeType
= XOPA_MAP
;
701 return consumeByte(insn
, &insn
->opcode
);
705 if (consumeByte(insn
, ¤t
))
708 if (current
== 0x0f) {
709 dbgprintf(insn
, "Found a two-byte escape prefix (0x%hhx)", current
);
711 if (consumeByte(insn
, ¤t
))
714 if (current
== 0x38) {
715 dbgprintf(insn
, "Found a three-byte escape prefix (0x%hhx)", current
);
717 if (consumeByte(insn
, ¤t
))
720 insn
->opcodeType
= THREEBYTE_38
;
721 } else if (current
== 0x3a) {
722 dbgprintf(insn
, "Found a three-byte escape prefix (0x%hhx)", current
);
724 if (consumeByte(insn
, ¤t
))
727 insn
->opcodeType
= THREEBYTE_3A
;
728 } else if (current
== 0x0f) {
729 dbgprintf(insn
, "Found a 3dnow escape prefix (0x%hhx)", current
);
731 // Consume operands before the opcode to comply with the 3DNow encoding
735 if (consumeByte(insn
, ¤t
))
738 insn
->opcodeType
= THREEDNOW_MAP
;
740 dbgprintf(insn
, "Didn't find a three-byte escape prefix");
742 insn
->opcodeType
= TWOBYTE
;
744 } else if (insn
->mandatoryPrefix
)
745 // The opcode with mandatory prefix must start with opcode escape.
746 // If not it's legacy repeat prefix
747 insn
->mandatoryPrefix
= 0;
750 * At this point we have consumed the full opcode.
751 * Anything we consume from here on must be unconsumed.
754 insn
->opcode
= current
;
760 * getIDWithAttrMask - Determines the ID of an instruction, consuming
761 * the ModR/M byte as appropriate for extended and escape opcodes,
762 * and using a supplied attribute mask.
764 * @param instructionID - A pointer whose target is filled in with the ID of the
766 * @param insn - The instruction whose ID is to be determined.
767 * @param attrMask - The attribute mask to search.
768 * @return - 0 if the ModR/M could be read when needed or was not
769 * needed; nonzero otherwise.
771 static int getIDWithAttrMask(uint16_t* instructionID
,
772 struct InternalInstruction
* insn
,
774 bool hasModRMExtension
;
776 InstructionContext instructionClass
= contextForAttrs(attrMask
);
778 hasModRMExtension
= modRMRequired(insn
->opcodeType
,
782 if (hasModRMExtension
) {
786 *instructionID
= decode(insn
->opcodeType
,
791 *instructionID
= decode(insn
->opcodeType
,
801 * is16BitEquivalent - Determines whether two instruction names refer to
802 * equivalent instructions but one is 16-bit whereas the other is not.
804 * @param orig - The instruction that is not 16-bit
805 * @param equiv - The instruction that is 16-bit
807 static bool is16BitEquivalent(const char *orig
, const char *equiv
) {
811 if (orig
[i
] == '\0' && equiv
[i
] == '\0')
813 if (orig
[i
] == '\0' || equiv
[i
] == '\0')
815 if (orig
[i
] != equiv
[i
]) {
816 if ((orig
[i
] == 'Q' || orig
[i
] == 'L') && equiv
[i
] == 'W')
818 if ((orig
[i
] == '6' || orig
[i
] == '3') && equiv
[i
] == '1')
820 if ((orig
[i
] == '4' || orig
[i
] == '2') && equiv
[i
] == '6')
828 * is64Bit - Determines whether this instruction is a 64-bit instruction.
830 * @param name - The instruction that is not 16-bit
832 static bool is64Bit(const char *name
) {
838 if (name
[i
] == '6' && name
[i
+1] == '4')
844 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
845 * appropriate for extended and escape opcodes. Determines the attributes and
846 * context for the instruction before doing so.
848 * @param insn - The instruction whose ID is to be determined.
849 * @return - 0 if the ModR/M could be read when needed or was not needed;
852 static int getID(struct InternalInstruction
* insn
, const void *miiArg
) {
854 uint16_t instructionID
;
856 dbgprintf(insn
, "getID()");
858 attrMask
= ATTR_NONE
;
860 if (insn
->mode
== MODE_64BIT
)
861 attrMask
|= ATTR_64BIT
;
863 if (insn
->vectorExtensionType
!= TYPE_NO_VEX_XOP
) {
864 attrMask
|= (insn
->vectorExtensionType
== TYPE_EVEX
) ? ATTR_EVEX
: ATTR_VEX
;
866 if (insn
->vectorExtensionType
== TYPE_EVEX
) {
867 switch (ppFromEVEX3of4(insn
->vectorExtensionPrefix
[2])) {
869 attrMask
|= ATTR_OPSIZE
;
879 if (zFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
880 attrMask
|= ATTR_EVEXKZ
;
881 if (bFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
882 attrMask
|= ATTR_EVEXB
;
883 if (aaaFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
884 attrMask
|= ATTR_EVEXK
;
885 if (lFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
886 attrMask
|= ATTR_VEXL
;
887 if (l2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
888 attrMask
|= ATTR_EVEXL2
;
889 } else if (insn
->vectorExtensionType
== TYPE_VEX_3B
) {
890 switch (ppFromVEX3of3(insn
->vectorExtensionPrefix
[2])) {
892 attrMask
|= ATTR_OPSIZE
;
902 if (lFromVEX3of3(insn
->vectorExtensionPrefix
[2]))
903 attrMask
|= ATTR_VEXL
;
904 } else if (insn
->vectorExtensionType
== TYPE_VEX_2B
) {
905 switch (ppFromVEX2of2(insn
->vectorExtensionPrefix
[1])) {
907 attrMask
|= ATTR_OPSIZE
;
917 if (lFromVEX2of2(insn
->vectorExtensionPrefix
[1]))
918 attrMask
|= ATTR_VEXL
;
919 } else if (insn
->vectorExtensionType
== TYPE_XOP
) {
920 switch (ppFromXOP3of3(insn
->vectorExtensionPrefix
[2])) {
922 attrMask
|= ATTR_OPSIZE
;
932 if (lFromXOP3of3(insn
->vectorExtensionPrefix
[2]))
933 attrMask
|= ATTR_VEXL
;
937 } else if (!insn
->mandatoryPrefix
) {
938 // If we don't have mandatory prefix we should use legacy prefixes here
939 if (insn
->hasOpSize
&& (insn
->mode
!= MODE_16BIT
))
940 attrMask
|= ATTR_OPSIZE
;
942 attrMask
|= ATTR_ADSIZE
;
943 if (insn
->opcodeType
== ONEBYTE
) {
944 if (insn
->repeatPrefix
== 0xf3 && (insn
->opcode
== 0x90))
945 // Special support for PAUSE
948 if (insn
->repeatPrefix
== 0xf2)
950 else if (insn
->repeatPrefix
== 0xf3)
954 switch (insn
->mandatoryPrefix
) {
962 if (insn
->mode
!= MODE_16BIT
)
963 attrMask
|= ATTR_OPSIZE
;
966 attrMask
|= ATTR_ADSIZE
;
972 if (insn
->rexPrefix
& 0x08) {
973 attrMask
|= ATTR_REXW
;
974 attrMask
&= ~ATTR_ADSIZE
;
978 * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
979 * of the AdSize prefix is inverted w.r.t. 32-bit mode.
981 if (insn
->mode
== MODE_16BIT
&& insn
->opcodeType
== ONEBYTE
&&
982 insn
->opcode
== 0xE3)
983 attrMask
^= ATTR_ADSIZE
;
985 // If we're in 16-bit mode and this is one of the relative jumps and opsize
986 // prefix isn't present, we need to force the opsize attribute since the
987 // prefix is inverted relative to 32-bit mode.
988 if (insn
->mode
== MODE_16BIT
&& !insn
->hasOpSize
&&
989 insn
->opcodeType
== ONEBYTE
&&
990 (insn
->opcode
== 0xE8 || insn
->opcode
== 0xE9))
991 attrMask
|= ATTR_OPSIZE
;
993 if (insn
->mode
== MODE_16BIT
&& !insn
->hasOpSize
&&
994 insn
->opcodeType
== TWOBYTE
&&
995 insn
->opcode
>= 0x80 && insn
->opcode
<= 0x8F)
996 attrMask
|= ATTR_OPSIZE
;
998 if (getIDWithAttrMask(&instructionID
, insn
, attrMask
))
1001 /* The following clauses compensate for limitations of the tables. */
1003 if (insn
->mode
!= MODE_64BIT
&&
1004 insn
->vectorExtensionType
!= TYPE_NO_VEX_XOP
) {
1006 * The tables can't distinquish between cases where the W-bit is used to
1007 * select register size and cases where its a required part of the opcode.
1009 if ((insn
->vectorExtensionType
== TYPE_EVEX
&&
1010 wFromEVEX3of4(insn
->vectorExtensionPrefix
[2])) ||
1011 (insn
->vectorExtensionType
== TYPE_VEX_3B
&&
1012 wFromVEX3of3(insn
->vectorExtensionPrefix
[2])) ||
1013 (insn
->vectorExtensionType
== TYPE_XOP
&&
1014 wFromXOP3of3(insn
->vectorExtensionPrefix
[2]))) {
1016 uint16_t instructionIDWithREXW
;
1017 if (getIDWithAttrMask(&instructionIDWithREXW
,
1018 insn
, attrMask
| ATTR_REXW
)) {
1019 insn
->instructionID
= instructionID
;
1020 insn
->spec
= specifierForUID(instructionID
);
1024 auto SpecName
= GetInstrName(instructionIDWithREXW
, miiArg
);
1025 // If not a 64-bit instruction. Switch the opcode.
1026 if (!is64Bit(SpecName
.data())) {
1027 insn
->instructionID
= instructionIDWithREXW
;
1028 insn
->spec
= specifierForUID(instructionIDWithREXW
);
1035 * Absolute moves, umonitor, and movdir64b need special handling.
1036 * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1038 * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1041 if ((insn
->opcodeType
== ONEBYTE
&& ((insn
->opcode
& 0xFC) == 0xA0)) ||
1042 (insn
->opcodeType
== TWOBYTE
&& (insn
->opcode
== 0xAE)) ||
1043 (insn
->opcodeType
== THREEBYTE_38
&& insn
->opcode
== 0xF8)) {
1044 /* Make sure we observed the prefixes in any position. */
1045 if (insn
->hasAdSize
)
1046 attrMask
|= ATTR_ADSIZE
;
1047 if (insn
->hasOpSize
)
1048 attrMask
|= ATTR_OPSIZE
;
1050 /* In 16-bit, invert the attributes. */
1051 if (insn
->mode
== MODE_16BIT
) {
1052 attrMask
^= ATTR_ADSIZE
;
1054 /* The OpSize attribute is only valid with the absolute moves. */
1055 if (insn
->opcodeType
== ONEBYTE
&& ((insn
->opcode
& 0xFC) == 0xA0))
1056 attrMask
^= ATTR_OPSIZE
;
1059 if (getIDWithAttrMask(&instructionID
, insn
, attrMask
))
1062 insn
->instructionID
= instructionID
;
1063 insn
->spec
= specifierForUID(instructionID
);
1067 if ((insn
->mode
== MODE_16BIT
|| insn
->hasOpSize
) &&
1068 !(attrMask
& ATTR_OPSIZE
)) {
1070 * The instruction tables make no distinction between instructions that
1071 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1072 * particular spot (i.e., many MMX operations). In general we're
1073 * conservative, but in the specific case where OpSize is present but not
1074 * in the right place we check if there's a 16-bit operation.
1077 const struct InstructionSpecifier
*spec
;
1078 uint16_t instructionIDWithOpsize
;
1079 llvm::StringRef specName
, specWithOpSizeName
;
1081 spec
= specifierForUID(instructionID
);
1083 if (getIDWithAttrMask(&instructionIDWithOpsize
,
1085 attrMask
| ATTR_OPSIZE
)) {
1087 * ModRM required with OpSize but not present; give up and return version
1088 * without OpSize set
1091 insn
->instructionID
= instructionID
;
1096 specName
= GetInstrName(instructionID
, miiArg
);
1097 specWithOpSizeName
= GetInstrName(instructionIDWithOpsize
, miiArg
);
1099 if (is16BitEquivalent(specName
.data(), specWithOpSizeName
.data()) &&
1100 (insn
->mode
== MODE_16BIT
) ^ insn
->hasOpSize
) {
1101 insn
->instructionID
= instructionIDWithOpsize
;
1102 insn
->spec
= specifierForUID(instructionIDWithOpsize
);
1104 insn
->instructionID
= instructionID
;
1110 if (insn
->opcodeType
== ONEBYTE
&& insn
->opcode
== 0x90 &&
1111 insn
->rexPrefix
& 0x01) {
1113 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1114 * it should decode as XCHG %r8, %eax.
1117 const struct InstructionSpecifier
*spec
;
1118 uint16_t instructionIDWithNewOpcode
;
1119 const struct InstructionSpecifier
*specWithNewOpcode
;
1121 spec
= specifierForUID(instructionID
);
1123 /* Borrow opcode from one of the other XCHGar opcodes */
1124 insn
->opcode
= 0x91;
1126 if (getIDWithAttrMask(&instructionIDWithNewOpcode
,
1129 insn
->opcode
= 0x90;
1131 insn
->instructionID
= instructionID
;
1136 specWithNewOpcode
= specifierForUID(instructionIDWithNewOpcode
);
1139 insn
->opcode
= 0x90;
1141 insn
->instructionID
= instructionIDWithNewOpcode
;
1142 insn
->spec
= specWithNewOpcode
;
1147 insn
->instructionID
= instructionID
;
1148 insn
->spec
= specifierForUID(insn
->instructionID
);
1154 * readSIB - Consumes the SIB byte to determine addressing information for an
1157 * @param insn - The instruction whose SIB byte is to be read.
1158 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
1160 static int readSIB(struct InternalInstruction
* insn
) {
1161 SIBBase sibBaseBase
= SIB_BASE_NONE
;
1162 uint8_t index
, base
;
1164 dbgprintf(insn
, "readSIB()");
1166 if (insn
->consumedSIB
)
1169 insn
->consumedSIB
= true;
1171 switch (insn
->addressSize
) {
1173 dbgprintf(insn
, "SIB-based addressing doesn't work in 16-bit mode");
1176 insn
->sibIndexBase
= SIB_INDEX_EAX
;
1177 sibBaseBase
= SIB_BASE_EAX
;
1180 insn
->sibIndexBase
= SIB_INDEX_RAX
;
1181 sibBaseBase
= SIB_BASE_RAX
;
1185 if (consumeByte(insn
, &insn
->sib
))
1188 index
= indexFromSIB(insn
->sib
) | (xFromREX(insn
->rexPrefix
) << 3);
1191 insn
->sibIndex
= SIB_INDEX_NONE
;
1193 insn
->sibIndex
= (SIBIndex
)(insn
->sibIndexBase
+ index
);
1196 insn
->sibScale
= 1 << scaleFromSIB(insn
->sib
);
1198 base
= baseFromSIB(insn
->sib
) | (bFromREX(insn
->rexPrefix
) << 3);
1203 switch (modFromModRM(insn
->modRM
)) {
1205 insn
->eaDisplacement
= EA_DISP_32
;
1206 insn
->sibBase
= SIB_BASE_NONE
;
1209 insn
->eaDisplacement
= EA_DISP_8
;
1210 insn
->sibBase
= (SIBBase
)(sibBaseBase
+ base
);
1213 insn
->eaDisplacement
= EA_DISP_32
;
1214 insn
->sibBase
= (SIBBase
)(sibBaseBase
+ base
);
1217 debug("Cannot have Mod = 0b11 and a SIB byte");
1222 insn
->sibBase
= (SIBBase
)(sibBaseBase
+ base
);
1230 * readDisplacement - Consumes the displacement of an instruction.
1232 * @param insn - The instruction whose displacement is to be read.
1233 * @return - 0 if the displacement byte was successfully read; nonzero
1236 static int readDisplacement(struct InternalInstruction
* insn
) {
1241 dbgprintf(insn
, "readDisplacement()");
1243 if (insn
->consumedDisplacement
)
1246 insn
->consumedDisplacement
= true;
1247 insn
->displacementOffset
= insn
->readerCursor
- insn
->startLocation
;
1249 switch (insn
->eaDisplacement
) {
1251 insn
->consumedDisplacement
= false;
1254 if (consumeInt8(insn
, &d8
))
1256 insn
->displacement
= d8
;
1259 if (consumeInt16(insn
, &d16
))
1261 insn
->displacement
= d16
;
1264 if (consumeInt32(insn
, &d32
))
1266 insn
->displacement
= d32
;
1270 insn
->consumedDisplacement
= true;
1275 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1276 * displacement) for an instruction and interprets it.
1278 * @param insn - The instruction whose addressing information is to be read.
1279 * @return - 0 if the information was successfully read; nonzero otherwise.
1281 static int readModRM(struct InternalInstruction
* insn
) {
1282 uint8_t mod
, rm
, reg
, evexrm
;
1284 dbgprintf(insn
, "readModRM()");
1286 if (insn
->consumedModRM
)
1289 if (consumeByte(insn
, &insn
->modRM
))
1291 insn
->consumedModRM
= true;
1293 mod
= modFromModRM(insn
->modRM
);
1294 rm
= rmFromModRM(insn
->modRM
);
1295 reg
= regFromModRM(insn
->modRM
);
1298 * This goes by insn->registerSize to pick the correct register, which messes
1299 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1302 switch (insn
->registerSize
) {
1304 insn
->regBase
= MODRM_REG_AX
;
1305 insn
->eaRegBase
= EA_REG_AX
;
1308 insn
->regBase
= MODRM_REG_EAX
;
1309 insn
->eaRegBase
= EA_REG_EAX
;
1312 insn
->regBase
= MODRM_REG_RAX
;
1313 insn
->eaRegBase
= EA_REG_RAX
;
1317 reg
|= rFromREX(insn
->rexPrefix
) << 3;
1318 rm
|= bFromREX(insn
->rexPrefix
) << 3;
1321 if (insn
->vectorExtensionType
== TYPE_EVEX
&& insn
->mode
== MODE_64BIT
) {
1322 reg
|= r2FromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 4;
1323 evexrm
= xFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 4;
1326 insn
->reg
= (Reg
)(insn
->regBase
+ reg
);
1328 switch (insn
->addressSize
) {
1330 EABase eaBaseBase
= EA_BASE_BX_SI
;
1335 insn
->eaBase
= EA_BASE_NONE
;
1336 insn
->eaDisplacement
= EA_DISP_16
;
1337 if (readDisplacement(insn
))
1340 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
1341 insn
->eaDisplacement
= EA_DISP_NONE
;
1345 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
1346 insn
->eaDisplacement
= EA_DISP_8
;
1347 insn
->displacementSize
= 1;
1348 if (readDisplacement(insn
))
1352 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
1353 insn
->eaDisplacement
= EA_DISP_16
;
1354 if (readDisplacement(insn
))
1358 insn
->eaBase
= (EABase
)(insn
->eaRegBase
+ rm
);
1359 if (readDisplacement(insn
))
1367 EABase eaBaseBase
= (insn
->addressSize
== 4 ? EA_BASE_EAX
: EA_BASE_RAX
);
1371 insn
->eaDisplacement
= EA_DISP_NONE
; /* readSIB may override this */
1372 // In determining whether RIP-relative mode is used (rm=5),
1373 // or whether a SIB byte is present (rm=4),
1374 // the extension bits (REX.b and EVEX.x) are ignored.
1376 case 0x4: // SIB byte is present
1377 insn
->eaBase
= (insn
->addressSize
== 4 ?
1378 EA_BASE_sib
: EA_BASE_sib64
);
1379 if (readSIB(insn
) || readDisplacement(insn
))
1382 case 0x5: // RIP-relative
1383 insn
->eaBase
= EA_BASE_NONE
;
1384 insn
->eaDisplacement
= EA_DISP_32
;
1385 if (readDisplacement(insn
))
1389 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
1394 insn
->displacementSize
= 1;
1397 insn
->eaDisplacement
= (mod
== 0x1 ? EA_DISP_8
: EA_DISP_32
);
1399 case 0x4: // SIB byte is present
1400 insn
->eaBase
= EA_BASE_sib
;
1401 if (readSIB(insn
) || readDisplacement(insn
))
1405 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
1406 if (readDisplacement(insn
))
1412 insn
->eaDisplacement
= EA_DISP_NONE
;
1413 insn
->eaBase
= (EABase
)(insn
->eaRegBase
+ rm
+ evexrm
);
1418 } /* switch (insn->addressSize) */
1423 #define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
1424 static uint16_t name(struct InternalInstruction *insn, \
1431 debug("Unhandled register type"); \
1435 return base + index; \
1440 if (insn->rexPrefix && \
1441 index >= 4 && index <= 7) { \
1442 return prefix##_SPL + (index - 4); \
1444 return prefix##_AL + index; \
1450 return prefix##_AX + index; \
1455 return prefix##_EAX + index; \
1460 return prefix##_RAX + index; \
1462 return prefix##_ZMM0 + index; \
1464 return prefix##_YMM0 + index; \
1466 return prefix##_XMM0 + index; \
1471 return prefix##_K0 + index; \
1472 case TYPE_VK_PAIR: \
1475 return prefix##_K0_K1 + (index / 2); \
1477 return prefix##_MM0 + (index & 0x7); \
1478 case TYPE_SEGMENTREG: \
1479 if ((index & 7) > 5) \
1481 return prefix##_ES + (index & 7); \
1482 case TYPE_DEBUGREG: \
1483 return prefix##_DR0 + index; \
1484 case TYPE_CONTROLREG: \
1485 return prefix##_CR0 + index; \
1489 return prefix##_BND0 + index; \
1491 return prefix##_XMM0 + index; \
1493 return prefix##_YMM0 + index; \
1495 return prefix##_ZMM0 + index; \
1500 * fixup*Value - Consults an operand type to determine the meaning of the
1501 * reg or R/M field. If the operand is an XMM operand, for example, an
1502 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1503 * misinterpret it as.
1505 * @param insn - The instruction containing the operand.
1506 * @param type - The operand type.
1507 * @param index - The existing value of the field as reported by readModRM().
1508 * @param valid - The address of a uint8_t. The target is set to 1 if the
1509 * field is valid for the register class; 0 if not.
1510 * @return - The proper value.
1512 GENERIC_FIXUP_FUNC(fixupRegValue
, insn
->regBase
, MODRM_REG
, 0x1f)
1513 GENERIC_FIXUP_FUNC(fixupRMValue
, insn
->eaRegBase
, EA_REG
, 0xf)
1516 * fixupReg - Consults an operand specifier to determine which of the
1517 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1519 * @param insn - See fixup*Value().
1520 * @param op - The operand specifier.
1521 * @return - 0 if fixup was successful; -1 if the register returned was
1522 * invalid for its class.
1524 static int fixupReg(struct InternalInstruction
*insn
,
1525 const struct OperandSpecifier
*op
) {
1528 dbgprintf(insn
, "fixupReg()");
1530 switch ((OperandEncoding
)op
->encoding
) {
1532 debug("Expected a REG or R/M encoding in fixupReg");
1535 insn
->vvvv
= (Reg
)fixupRegValue(insn
,
1536 (OperandType
)op
->type
,
1543 insn
->reg
= (Reg
)fixupRegValue(insn
,
1544 (OperandType
)op
->type
,
1545 insn
->reg
- insn
->regBase
,
1551 if (insn
->eaBase
>= insn
->eaRegBase
) {
1552 insn
->eaBase
= (EABase
)fixupRMValue(insn
,
1553 (OperandType
)op
->type
,
1554 insn
->eaBase
- insn
->eaRegBase
,
1566 * readOpcodeRegister - Reads an operand from the opcode field of an
1567 * instruction and interprets it appropriately given the operand width.
1568 * Handles AddRegFrm instructions.
1570 * @param insn - the instruction whose opcode field is to be read.
1571 * @param size - The width (in bytes) of the register being specified.
1572 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1574 * @return - 0 on success; nonzero otherwise.
1576 static int readOpcodeRegister(struct InternalInstruction
* insn
, uint8_t size
) {
1577 dbgprintf(insn
, "readOpcodeRegister()");
1580 size
= insn
->registerSize
;
1584 insn
->opcodeRegister
= (Reg
)(MODRM_REG_AL
+ ((bFromREX(insn
->rexPrefix
) << 3)
1585 | (insn
->opcode
& 7)));
1586 if (insn
->rexPrefix
&&
1587 insn
->opcodeRegister
>= MODRM_REG_AL
+ 0x4 &&
1588 insn
->opcodeRegister
< MODRM_REG_AL
+ 0x8) {
1589 insn
->opcodeRegister
= (Reg
)(MODRM_REG_SPL
1590 + (insn
->opcodeRegister
- MODRM_REG_AL
- 4));
1595 insn
->opcodeRegister
= (Reg
)(MODRM_REG_AX
1596 + ((bFromREX(insn
->rexPrefix
) << 3)
1597 | (insn
->opcode
& 7)));
1600 insn
->opcodeRegister
= (Reg
)(MODRM_REG_EAX
1601 + ((bFromREX(insn
->rexPrefix
) << 3)
1602 | (insn
->opcode
& 7)));
1605 insn
->opcodeRegister
= (Reg
)(MODRM_REG_RAX
1606 + ((bFromREX(insn
->rexPrefix
) << 3)
1607 | (insn
->opcode
& 7)));
1615 * readImmediate - Consumes an immediate operand from an instruction, given the
1616 * desired operand size.
1618 * @param insn - The instruction whose operand is to be read.
1619 * @param size - The width (in bytes) of the operand.
1620 * @return - 0 if the immediate was successfully consumed; nonzero
1623 static int readImmediate(struct InternalInstruction
* insn
, uint8_t size
) {
1629 dbgprintf(insn
, "readImmediate()");
1631 if (insn
->numImmediatesConsumed
== 2) {
1632 debug("Already consumed two immediates");
1637 size
= insn
->immediateSize
;
1639 insn
->immediateSize
= size
;
1640 insn
->immediateOffset
= insn
->readerCursor
- insn
->startLocation
;
1644 if (consumeByte(insn
, &imm8
))
1646 insn
->immediates
[insn
->numImmediatesConsumed
] = imm8
;
1649 if (consumeUInt16(insn
, &imm16
))
1651 insn
->immediates
[insn
->numImmediatesConsumed
] = imm16
;
1654 if (consumeUInt32(insn
, &imm32
))
1656 insn
->immediates
[insn
->numImmediatesConsumed
] = imm32
;
1659 if (consumeUInt64(insn
, &imm64
))
1661 insn
->immediates
[insn
->numImmediatesConsumed
] = imm64
;
1665 insn
->numImmediatesConsumed
++;
1671 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
1673 * @param insn - The instruction whose operand is to be read.
1674 * @return - 0 if the vvvv was successfully consumed; nonzero
1677 static int readVVVV(struct InternalInstruction
* insn
) {
1678 dbgprintf(insn
, "readVVVV()");
1681 if (insn
->vectorExtensionType
== TYPE_EVEX
)
1682 vvvv
= (v2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]) << 4 |
1683 vvvvFromEVEX3of4(insn
->vectorExtensionPrefix
[2]));
1684 else if (insn
->vectorExtensionType
== TYPE_VEX_3B
)
1685 vvvv
= vvvvFromVEX3of3(insn
->vectorExtensionPrefix
[2]);
1686 else if (insn
->vectorExtensionType
== TYPE_VEX_2B
)
1687 vvvv
= vvvvFromVEX2of2(insn
->vectorExtensionPrefix
[1]);
1688 else if (insn
->vectorExtensionType
== TYPE_XOP
)
1689 vvvv
= vvvvFromXOP3of3(insn
->vectorExtensionPrefix
[2]);
1693 if (insn
->mode
!= MODE_64BIT
)
1694 vvvv
&= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
1696 insn
->vvvv
= static_cast<Reg
>(vvvv
);
1701 * readMaskRegister - Reads an mask register from the opcode field of an
1704 * @param insn - The instruction whose opcode field is to be read.
1705 * @return - 0 on success; nonzero otherwise.
1707 static int readMaskRegister(struct InternalInstruction
* insn
) {
1708 dbgprintf(insn
, "readMaskRegister()");
1710 if (insn
->vectorExtensionType
!= TYPE_EVEX
)
1714 static_cast<Reg
>(aaaFromEVEX4of4(insn
->vectorExtensionPrefix
[3]));
1719 * readOperands - Consults the specifier for an instruction and consumes all
1720 * operands for that instruction, interpreting them as it goes.
1722 * @param insn - The instruction whose operands are to be read and interpreted.
1723 * @return - 0 if all operands could be read; nonzero otherwise.
1725 static int readOperands(struct InternalInstruction
* insn
) {
1726 int hasVVVV
, needVVVV
;
1729 dbgprintf(insn
, "readOperands()");
1731 /* If non-zero vvvv specified, need to make sure one of the operands
1733 hasVVVV
= !readVVVV(insn
);
1734 needVVVV
= hasVVVV
&& (insn
->vvvv
!= 0);
1736 for (const auto &Op
: x86OperandSets
[insn
->spec
->operands
]) {
1737 switch (Op
.encoding
) {
1743 // VSIB can use the V2 bit so check only the other bits.
1745 needVVVV
= hasVVVV
& ((insn
->vvvv
& 0xf) != 0);
1746 if (readModRM(insn
))
1749 // Reject if SIB wasn't used.
1750 if (insn
->eaBase
!= EA_BASE_sib
&& insn
->eaBase
!= EA_BASE_sib64
)
1753 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1754 if (insn
->sibIndex
== SIB_INDEX_NONE
)
1755 insn
->sibIndex
= (SIBIndex
)(insn
->sibIndexBase
+ 4);
1757 // If EVEX.v2 is set this is one of the 16-31 registers.
1758 if (insn
->vectorExtensionType
== TYPE_EVEX
&& insn
->mode
== MODE_64BIT
&&
1759 v2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
1760 insn
->sibIndex
= (SIBIndex
)(insn
->sibIndex
+ 16);
1762 // Adjust the index register to the correct size.
1763 switch ((OperandType
)Op
.type
) {
1765 debug("Unhandled VSIB index type");
1768 insn
->sibIndex
= (SIBIndex
)(SIB_INDEX_XMM0
+
1769 (insn
->sibIndex
- insn
->sibIndexBase
));
1772 insn
->sibIndex
= (SIBIndex
)(SIB_INDEX_YMM0
+
1773 (insn
->sibIndex
- insn
->sibIndexBase
));
1776 insn
->sibIndex
= (SIBIndex
)(SIB_INDEX_ZMM0
+
1777 (insn
->sibIndex
- insn
->sibIndexBase
));
1781 // Apply the AVX512 compressed displacement scaling factor.
1782 if (Op
.encoding
!= ENCODING_REG
&& insn
->eaDisplacement
== EA_DISP_8
)
1783 insn
->displacement
*= 1 << (Op
.encoding
- ENCODING_VSIB
);
1787 if (readModRM(insn
))
1789 if (fixupReg(insn
, &Op
))
1791 // Apply the AVX512 compressed displacement scaling factor.
1792 if (Op
.encoding
!= ENCODING_REG
&& insn
->eaDisplacement
== EA_DISP_8
)
1793 insn
->displacement
*= 1 << (Op
.encoding
- ENCODING_RM
);
1797 /* Saw a register immediate so don't read again and instead split the
1798 previous immediate. FIXME: This is a hack. */
1799 insn
->immediates
[insn
->numImmediatesConsumed
] =
1800 insn
->immediates
[insn
->numImmediatesConsumed
- 1] & 0xf;
1801 ++insn
->numImmediatesConsumed
;
1804 if (readImmediate(insn
, 1))
1806 if (Op
.type
== TYPE_XMM
|| Op
.type
== TYPE_YMM
)
1810 if (readImmediate(insn
, 2))
1814 if (readImmediate(insn
, 4))
1818 if (readImmediate(insn
, 8))
1822 if (readImmediate(insn
, insn
->immediateSize
))
1826 if (readImmediate(insn
, insn
->addressSize
))
1830 insn
->RC
= (l2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]) << 1) |
1831 lFromEVEX4of4(insn
->vectorExtensionPrefix
[3]);
1834 if (readOpcodeRegister(insn
, 1))
1838 if (readOpcodeRegister(insn
, 2))
1842 if (readOpcodeRegister(insn
, 4))
1846 if (readOpcodeRegister(insn
, 8))
1850 if (readOpcodeRegister(insn
, 0))
1854 insn
->immediates
[1] = insn
->opcode
& 0xf;
1859 needVVVV
= 0; /* Mark that we have found a VVVV operand. */
1862 if (insn
->mode
!= MODE_64BIT
)
1863 insn
->vvvv
= static_cast<Reg
>(insn
->vvvv
& 0x7);
1864 if (fixupReg(insn
, &Op
))
1867 case ENCODING_WRITEMASK
:
1868 if (readMaskRegister(insn
))
1874 dbgprintf(insn
, "Encountered an operand with an unknown encoding.");
1879 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1880 if (needVVVV
) return -1;
1886 * decodeInstruction - Reads and interprets a full instruction provided by the
1889 * @param insn - A pointer to the instruction to be populated. Must be
1891 * @param reader - The function to be used to read the instruction's bytes.
1892 * @param readerArg - A generic argument to be passed to the reader to store
1893 * any internal state.
1894 * @param logger - If non-NULL, the function to be used to write log messages
1896 * @param loggerArg - A generic argument to be passed to the logger to store
1897 * any internal state.
1898 * @param startLoc - The address (in the reader's address space) of the first
1899 * byte in the instruction.
1900 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1901 * decode the instruction in.
1902 * @return - 0 if the instruction's memory could be read; nonzero if
1905 int llvm::X86Disassembler::decodeInstruction(
1906 struct InternalInstruction
*insn
, byteReader_t reader
,
1907 const void *readerArg
, dlog_t logger
, void *loggerArg
, const void *miiArg
,
1908 uint64_t startLoc
, DisassemblerMode mode
) {
1909 memset(insn
, 0, sizeof(struct InternalInstruction
));
1911 insn
->reader
= reader
;
1912 insn
->readerArg
= readerArg
;
1913 insn
->dlog
= logger
;
1914 insn
->dlogArg
= loggerArg
;
1915 insn
->startLocation
= startLoc
;
1916 insn
->readerCursor
= startLoc
;
1918 insn
->numImmediatesConsumed
= 0;
1920 if (readPrefixes(insn
) ||
1922 getID(insn
, miiArg
) ||
1923 insn
->instructionID
== 0 ||
1927 insn
->operands
= x86OperandSets
[insn
->spec
->operands
];
1929 insn
->length
= insn
->readerCursor
- insn
->startLocation
;
1931 dbgprintf(insn
, "Read from 0x%llx to 0x%llx: length %zu",
1932 startLoc
, insn
->readerCursor
, insn
->length
);
1934 if (insn
->length
> 15)
1935 dbgprintf(insn
, "Instruction exceeds 15-byte limit");