1 //===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===//
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 code to translate the data produced by the decoder into
14 // The X86 disassembler is a table-driven disassembler for the 16-, 32-, and
15 // 64-bit X86 instruction sets. The main decode sequence for an assembly
16 // instruction in this disassembler is:
18 // 1. Read the prefix bytes and determine the attributes of the instruction.
19 // These attributes, recorded in enum attributeBits
20 // (X86DisassemblerDecoderCommon.h), form a bitmask. The table CONTEXTS_SYM
21 // provides a mapping from bitmasks to contexts, which are represented by
22 // enum InstructionContext (ibid.).
24 // 2. Read the opcode, and determine what kind of opcode it is. The
25 // disassembler distinguishes four kinds of opcodes, which are enumerated in
26 // OpcodeType (X86DisassemblerDecoderCommon.h): one-byte (0xnn), two-byte
27 // (0x0f 0xnn), three-byte-38 (0x0f 0x38 0xnn), or three-byte-3a
28 // (0x0f 0x3a 0xnn). Mandatory prefixes are treated as part of the context.
30 // 3. Depending on the opcode type, look in one of four ClassDecision structures
31 // (X86DisassemblerDecoderCommon.h). Use the opcode class to determine which
32 // OpcodeDecision (ibid.) to look the opcode in. Look up the opcode, to get
33 // a ModRMDecision (ibid.).
35 // 4. Some instructions, such as escape opcodes or extended opcodes, or even
36 // instructions that have ModRM*Reg / ModRM*Mem forms in LLVM, need the
37 // ModR/M byte to complete decode. The ModRMDecision's type is an entry from
38 // ModRMDecisionType (X86DisassemblerDecoderCommon.h) that indicates if the
39 // ModR/M byte is required and how to interpret it.
41 // 5. After resolving the ModRMDecision, the disassembler has a unique ID
42 // of type InstrUID (X86DisassemblerDecoderCommon.h). Looking this ID up in
43 // INSTRUCTIONS_SYM yields the name of the instruction and the encodings and
44 // meanings of its operands.
46 // 6. For each operand, its encoding is an entry from OperandEncoding
47 // (X86DisassemblerDecoderCommon.h) and its type is an entry from
48 // OperandType (ibid.). The encoding indicates how to read it from the
49 // instruction; the type indicates how to interpret the value once it has
50 // been read. For example, a register operand could be stored in the R/M
51 // field of the ModR/M byte, the REG field of the ModR/M byte, or added to
52 // the main opcode. This is orthogonal from its meaning (an GPR or an XMM
53 // register, for instance). Given this information, the operands can be
54 // extracted and interpreted.
56 // 7. As the last step, the disassembler translates the instruction information
57 // and operands into a format understandable by the client - in this case, an
58 // MCInst for use by the MC infrastructure.
60 // The disassembler is broken broadly into two parts: the table emitter that
61 // emits the instruction decode tables discussed above during compilation, and
62 // the disassembler itself. The table emitter is documented in more detail in
63 // utils/TableGen/X86DisassemblerEmitter.h.
65 // X86Disassembler.cpp contains the code responsible for step 7, and for
66 // invoking the decoder to execute steps 1-6.
67 // X86DisassemblerDecoderCommon.h contains the definitions needed by both the
68 // table emitter and the disassembler.
69 // X86DisassemblerDecoder.h contains the public interface of the decoder,
70 // factored out into C for possible use by other projects.
71 // X86DisassemblerDecoder.c contains the source code of the decoder, which is
72 // responsible for steps 1-6.
74 //===----------------------------------------------------------------------===//
76 #include "MCTargetDesc/X86BaseInfo.h"
77 #include "MCTargetDesc/X86MCTargetDesc.h"
78 #include "TargetInfo/X86TargetInfo.h"
79 #include "X86DisassemblerDecoder.h"
80 #include "llvm/MC/MCContext.h"
81 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
82 #include "llvm/MC/MCExpr.h"
83 #include "llvm/MC/MCInst.h"
84 #include "llvm/MC/MCInstrInfo.h"
85 #include "llvm/MC/MCSubtargetInfo.h"
86 #include "llvm/Support/Debug.h"
87 #include "llvm/Support/Format.h"
88 #include "llvm/Support/TargetRegistry.h"
89 #include "llvm/Support/raw_ostream.h"
92 using namespace llvm::X86Disassembler
;
94 #define DEBUG_TYPE "x86-disassembler"
96 #define debug(s) LLVM_DEBUG(dbgs() << __LINE__ << ": " << s);
98 // Specifies whether a ModR/M byte is needed and (if so) which
99 // instruction each possible value of the ModR/M byte corresponds to. Once
100 // this information is known, we have narrowed down to a single instruction.
101 struct ModRMDecision
{
103 uint16_t instructionIDs
;
106 // Specifies which set of ModR/M->instruction tables to look at
107 // given a particular opcode.
108 struct OpcodeDecision
{
109 ModRMDecision modRMDecisions
[256];
112 // Specifies which opcode->instruction tables to look at given
113 // a particular context (set of attributes). Since there are many possible
114 // contexts, the decoder first uses CONTEXTS_SYM to determine which context
115 // applies given a specific set of attributes. Hence there are only IC_max
116 // entries in this table, rather than 2^(ATTR_max).
117 struct ContextDecision
{
118 OpcodeDecision opcodeDecisions
[IC_max
];
121 #include "X86GenDisassemblerTables.inc"
123 static InstrUID
decode(OpcodeType type
, InstructionContext insnContext
,
124 uint8_t opcode
, uint8_t modRM
) {
125 const struct ModRMDecision
*dec
;
129 dec
= &ONEBYTE_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
132 dec
= &TWOBYTE_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
135 dec
= &THREEBYTE38_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
138 dec
= &THREEBYTE3A_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
141 dec
= &XOP8_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
144 dec
= &XOP9_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
147 dec
= &XOPA_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
151 &THREEDNOW_MAP_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
154 dec
= &MAP5_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
157 dec
= &MAP6_SYM
.opcodeDecisions
[insnContext
].modRMDecisions
[opcode
];
161 switch (dec
->modrm_type
) {
163 llvm_unreachable("Corrupt table! Unknown modrm_type");
166 return modRMTable
[dec
->instructionIDs
];
168 if (modFromModRM(modRM
) == 0x3)
169 return modRMTable
[dec
->instructionIDs
+ 1];
170 return modRMTable
[dec
->instructionIDs
];
172 if (modFromModRM(modRM
) == 0x3)
173 return modRMTable
[dec
->instructionIDs
+ ((modRM
& 0x38) >> 3) + 8];
174 return modRMTable
[dec
->instructionIDs
+ ((modRM
& 0x38) >> 3)];
175 case MODRM_SPLITMISC
:
176 if (modFromModRM(modRM
) == 0x3)
177 return modRMTable
[dec
->instructionIDs
+ (modRM
& 0x3f) + 8];
178 return modRMTable
[dec
->instructionIDs
+ ((modRM
& 0x38) >> 3)];
180 return modRMTable
[dec
->instructionIDs
+ modRM
];
184 static bool peek(struct InternalInstruction
*insn
, uint8_t &byte
) {
185 uint64_t offset
= insn
->readerCursor
- insn
->startLocation
;
186 if (offset
>= insn
->bytes
.size())
188 byte
= insn
->bytes
[offset
];
192 template <typename T
> static bool consume(InternalInstruction
*insn
, T
&ptr
) {
193 auto r
= insn
->bytes
;
194 uint64_t offset
= insn
->readerCursor
- insn
->startLocation
;
195 if (offset
+ sizeof(T
) > r
.size())
198 for (unsigned i
= 0; i
< sizeof(T
); ++i
)
199 ret
|= (uint64_t)r
[offset
+ i
] << (i
* 8);
201 insn
->readerCursor
+= sizeof(T
);
205 static bool isREX(struct InternalInstruction
*insn
, uint8_t prefix
) {
206 return insn
->mode
== MODE_64BIT
&& prefix
>= 0x40 && prefix
<= 0x4f;
209 // Consumes all of an instruction's prefix bytes, and marks the
210 // instruction as having them. Also sets the instruction's default operand,
211 // address, and other relevant data sizes to report operands correctly.
213 // insn must not be empty.
214 static int readPrefixes(struct InternalInstruction
*insn
) {
215 bool isPrefix
= true;
219 LLVM_DEBUG(dbgs() << "readPrefixes()");
222 // If we fail reading prefixes, just stop here and let the opcode reader
224 if (consume(insn
, byte
))
227 // If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
228 // break and let it be disassembled as a normal "instruction".
229 if (insn
->readerCursor
- 1 == insn
->startLocation
&& byte
== 0xf0) // LOCK
232 if ((byte
== 0xf2 || byte
== 0xf3) && !peek(insn
, nextByte
)) {
233 // If the byte is 0xf2 or 0xf3, and any of the following conditions are
235 // - it is followed by a LOCK (0xf0) prefix
236 // - it is followed by an xchg instruction
237 // then it should be disassembled as a xacquire/xrelease not repne/rep.
238 if (((nextByte
== 0xf0) ||
239 ((nextByte
& 0xfe) == 0x86 || (nextByte
& 0xf8) == 0x90))) {
240 insn
->xAcquireRelease
= true;
241 if (!(byte
== 0xf3 && nextByte
== 0x90)) // PAUSE instruction support
244 // Also if the byte is 0xf3, and the following condition is met:
245 // - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
246 // "mov mem, imm" (opcode 0xc6/0xc7) instructions.
247 // then it should be disassembled as an xrelease not rep.
248 if (byte
== 0xf3 && (nextByte
== 0x88 || nextByte
== 0x89 ||
249 nextByte
== 0xc6 || nextByte
== 0xc7)) {
250 insn
->xAcquireRelease
= true;
253 if (isREX(insn
, nextByte
)) {
255 // Go to REX prefix after the current one
256 if (consume(insn
, nnextByte
))
258 // We should be able to read next byte after REX prefix
259 if (peek(insn
, nnextByte
))
261 --insn
->readerCursor
;
267 insn
->hasLockPrefix
= true;
269 case 0xf2: // REPNE/REPNZ
270 case 0xf3: { // REP or REPE/REPZ
272 if (peek(insn
, nextByte
))
275 // 1. There could be several 0x66
276 // 2. if (nextByte == 0x66) and nextNextByte != 0x0f then
277 // it's not mandatory prefix
278 // 3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
279 // 0x0f exactly after it to be mandatory prefix
280 if (isREX(insn
, nextByte
) || nextByte
== 0x0f || nextByte
== 0x66)
281 // The last of 0xf2 /0xf3 is mandatory prefix
282 insn
->mandatoryPrefix
= byte
;
283 insn
->repeatPrefix
= byte
;
286 case 0x2e: // CS segment override -OR- Branch not taken
287 insn
->segmentOverride
= SEG_OVERRIDE_CS
;
289 case 0x36: // SS segment override -OR- Branch taken
290 insn
->segmentOverride
= SEG_OVERRIDE_SS
;
292 case 0x3e: // DS segment override
293 insn
->segmentOverride
= SEG_OVERRIDE_DS
;
295 case 0x26: // ES segment override
296 insn
->segmentOverride
= SEG_OVERRIDE_ES
;
298 case 0x64: // FS segment override
299 insn
->segmentOverride
= SEG_OVERRIDE_FS
;
301 case 0x65: // GS segment override
302 insn
->segmentOverride
= SEG_OVERRIDE_GS
;
304 case 0x66: { // Operand-size override {
306 insn
->hasOpSize
= true;
307 if (peek(insn
, nextByte
))
309 // 0x66 can't overwrite existing mandatory prefix and should be ignored
310 if (!insn
->mandatoryPrefix
&& (nextByte
== 0x0f || isREX(insn
, nextByte
)))
311 insn
->mandatoryPrefix
= byte
;
314 case 0x67: // Address-size override
315 insn
->hasAdSize
= true;
317 default: // Not a prefix byte
323 LLVM_DEBUG(dbgs() << format("Found prefix 0x%hhx", byte
));
326 insn
->vectorExtensionType
= TYPE_NO_VEX_XOP
;
329 uint8_t byte1
, byte2
;
330 if (consume(insn
, byte1
)) {
331 LLVM_DEBUG(dbgs() << "Couldn't read second byte of EVEX prefix");
335 if (peek(insn
, byte2
)) {
336 LLVM_DEBUG(dbgs() << "Couldn't read third byte of EVEX prefix");
340 if ((insn
->mode
== MODE_64BIT
|| (byte1
& 0xc0) == 0xc0) &&
341 ((~byte1
& 0x8) == 0x8) && ((byte2
& 0x4) == 0x4)) {
342 insn
->vectorExtensionType
= TYPE_EVEX
;
344 --insn
->readerCursor
; // unconsume byte1
345 --insn
->readerCursor
; // unconsume byte
348 if (insn
->vectorExtensionType
== TYPE_EVEX
) {
349 insn
->vectorExtensionPrefix
[0] = byte
;
350 insn
->vectorExtensionPrefix
[1] = byte1
;
351 if (consume(insn
, insn
->vectorExtensionPrefix
[2])) {
352 LLVM_DEBUG(dbgs() << "Couldn't read third byte of EVEX prefix");
355 if (consume(insn
, insn
->vectorExtensionPrefix
[3])) {
356 LLVM_DEBUG(dbgs() << "Couldn't read fourth byte of EVEX prefix");
360 // We simulate the REX prefix for simplicity's sake
361 if (insn
->mode
== MODE_64BIT
) {
362 insn
->rexPrefix
= 0x40 |
363 (wFromEVEX3of4(insn
->vectorExtensionPrefix
[2]) << 3) |
364 (rFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 2) |
365 (xFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 1) |
366 (bFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 0);
371 "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
372 insn
->vectorExtensionPrefix
[0], insn
->vectorExtensionPrefix
[1],
373 insn
->vectorExtensionPrefix
[2], insn
->vectorExtensionPrefix
[3]));
375 } else if (byte
== 0xc4) {
377 if (peek(insn
, byte1
)) {
378 LLVM_DEBUG(dbgs() << "Couldn't read second byte of VEX");
382 if (insn
->mode
== MODE_64BIT
|| (byte1
& 0xc0) == 0xc0)
383 insn
->vectorExtensionType
= TYPE_VEX_3B
;
385 --insn
->readerCursor
;
387 if (insn
->vectorExtensionType
== TYPE_VEX_3B
) {
388 insn
->vectorExtensionPrefix
[0] = byte
;
389 consume(insn
, insn
->vectorExtensionPrefix
[1]);
390 consume(insn
, insn
->vectorExtensionPrefix
[2]);
392 // We simulate the REX prefix for simplicity's sake
394 if (insn
->mode
== MODE_64BIT
)
395 insn
->rexPrefix
= 0x40 |
396 (wFromVEX3of3(insn
->vectorExtensionPrefix
[2]) << 3) |
397 (rFromVEX2of3(insn
->vectorExtensionPrefix
[1]) << 2) |
398 (xFromVEX2of3(insn
->vectorExtensionPrefix
[1]) << 1) |
399 (bFromVEX2of3(insn
->vectorExtensionPrefix
[1]) << 0);
401 LLVM_DEBUG(dbgs() << format("Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
402 insn
->vectorExtensionPrefix
[0],
403 insn
->vectorExtensionPrefix
[1],
404 insn
->vectorExtensionPrefix
[2]));
406 } else if (byte
== 0xc5) {
408 if (peek(insn
, byte1
)) {
409 LLVM_DEBUG(dbgs() << "Couldn't read second byte of VEX");
413 if (insn
->mode
== MODE_64BIT
|| (byte1
& 0xc0) == 0xc0)
414 insn
->vectorExtensionType
= TYPE_VEX_2B
;
416 --insn
->readerCursor
;
418 if (insn
->vectorExtensionType
== TYPE_VEX_2B
) {
419 insn
->vectorExtensionPrefix
[0] = byte
;
420 consume(insn
, insn
->vectorExtensionPrefix
[1]);
422 if (insn
->mode
== MODE_64BIT
)
424 0x40 | (rFromVEX2of2(insn
->vectorExtensionPrefix
[1]) << 2);
426 switch (ppFromVEX2of2(insn
->vectorExtensionPrefix
[1])) {
430 insn
->hasOpSize
= true;
434 LLVM_DEBUG(dbgs() << format("Found VEX prefix 0x%hhx 0x%hhx",
435 insn
->vectorExtensionPrefix
[0],
436 insn
->vectorExtensionPrefix
[1]));
438 } else if (byte
== 0x8f) {
440 if (peek(insn
, byte1
)) {
441 LLVM_DEBUG(dbgs() << "Couldn't read second byte of XOP");
445 if ((byte1
& 0x38) != 0x0) // 0 in these 3 bits is a POP instruction.
446 insn
->vectorExtensionType
= TYPE_XOP
;
448 --insn
->readerCursor
;
450 if (insn
->vectorExtensionType
== TYPE_XOP
) {
451 insn
->vectorExtensionPrefix
[0] = byte
;
452 consume(insn
, insn
->vectorExtensionPrefix
[1]);
453 consume(insn
, insn
->vectorExtensionPrefix
[2]);
455 // We simulate the REX prefix for simplicity's sake
457 if (insn
->mode
== MODE_64BIT
)
458 insn
->rexPrefix
= 0x40 |
459 (wFromXOP3of3(insn
->vectorExtensionPrefix
[2]) << 3) |
460 (rFromXOP2of3(insn
->vectorExtensionPrefix
[1]) << 2) |
461 (xFromXOP2of3(insn
->vectorExtensionPrefix
[1]) << 1) |
462 (bFromXOP2of3(insn
->vectorExtensionPrefix
[1]) << 0);
464 switch (ppFromXOP3of3(insn
->vectorExtensionPrefix
[2])) {
468 insn
->hasOpSize
= true;
472 LLVM_DEBUG(dbgs() << format("Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
473 insn
->vectorExtensionPrefix
[0],
474 insn
->vectorExtensionPrefix
[1],
475 insn
->vectorExtensionPrefix
[2]));
477 } else if (isREX(insn
, byte
)) {
478 if (peek(insn
, nextByte
))
480 insn
->rexPrefix
= byte
;
481 LLVM_DEBUG(dbgs() << format("Found REX prefix 0x%hhx", byte
));
483 --insn
->readerCursor
;
485 if (insn
->mode
== MODE_16BIT
) {
486 insn
->registerSize
= (insn
->hasOpSize
? 4 : 2);
487 insn
->addressSize
= (insn
->hasAdSize
? 4 : 2);
488 insn
->displacementSize
= (insn
->hasAdSize
? 4 : 2);
489 insn
->immediateSize
= (insn
->hasOpSize
? 4 : 2);
490 } else if (insn
->mode
== MODE_32BIT
) {
491 insn
->registerSize
= (insn
->hasOpSize
? 2 : 4);
492 insn
->addressSize
= (insn
->hasAdSize
? 2 : 4);
493 insn
->displacementSize
= (insn
->hasAdSize
? 2 : 4);
494 insn
->immediateSize
= (insn
->hasOpSize
? 2 : 4);
495 } else if (insn
->mode
== MODE_64BIT
) {
496 if (insn
->rexPrefix
&& wFromREX(insn
->rexPrefix
)) {
497 insn
->registerSize
= 8;
498 insn
->addressSize
= (insn
->hasAdSize
? 4 : 8);
499 insn
->displacementSize
= 4;
500 insn
->immediateSize
= 4;
501 insn
->hasOpSize
= false;
503 insn
->registerSize
= (insn
->hasOpSize
? 2 : 4);
504 insn
->addressSize
= (insn
->hasAdSize
? 4 : 8);
505 insn
->displacementSize
= (insn
->hasOpSize
? 2 : 4);
506 insn
->immediateSize
= (insn
->hasOpSize
? 2 : 4);
513 // Consumes the SIB byte to determine addressing information.
514 static int readSIB(struct InternalInstruction
*insn
) {
515 SIBBase sibBaseBase
= SIB_BASE_NONE
;
518 LLVM_DEBUG(dbgs() << "readSIB()");
519 switch (insn
->addressSize
) {
522 llvm_unreachable("SIB-based addressing doesn't work in 16-bit mode");
524 insn
->sibIndexBase
= SIB_INDEX_EAX
;
525 sibBaseBase
= SIB_BASE_EAX
;
528 insn
->sibIndexBase
= SIB_INDEX_RAX
;
529 sibBaseBase
= SIB_BASE_RAX
;
533 if (consume(insn
, insn
->sib
))
536 index
= indexFromSIB(insn
->sib
) | (xFromREX(insn
->rexPrefix
) << 3);
539 insn
->sibIndex
= SIB_INDEX_NONE
;
541 insn
->sibIndex
= (SIBIndex
)(insn
->sibIndexBase
+ index
);
544 insn
->sibScale
= 1 << scaleFromSIB(insn
->sib
);
546 base
= baseFromSIB(insn
->sib
) | (bFromREX(insn
->rexPrefix
) << 3);
551 switch (modFromModRM(insn
->modRM
)) {
553 insn
->eaDisplacement
= EA_DISP_32
;
554 insn
->sibBase
= SIB_BASE_NONE
;
557 insn
->eaDisplacement
= EA_DISP_8
;
558 insn
->sibBase
= (SIBBase
)(sibBaseBase
+ base
);
561 insn
->eaDisplacement
= EA_DISP_32
;
562 insn
->sibBase
= (SIBBase
)(sibBaseBase
+ base
);
565 llvm_unreachable("Cannot have Mod = 0b11 and a SIB byte");
569 insn
->sibBase
= (SIBBase
)(sibBaseBase
+ base
);
576 static int readDisplacement(struct InternalInstruction
*insn
) {
580 LLVM_DEBUG(dbgs() << "readDisplacement()");
582 insn
->displacementOffset
= insn
->readerCursor
- insn
->startLocation
;
583 switch (insn
->eaDisplacement
) {
587 if (consume(insn
, d8
))
589 insn
->displacement
= d8
;
592 if (consume(insn
, d16
))
594 insn
->displacement
= d16
;
597 if (consume(insn
, d32
))
599 insn
->displacement
= d32
;
606 // Consumes all addressing information (ModR/M byte, SIB byte, and displacement.
607 static int readModRM(struct InternalInstruction
*insn
) {
608 uint8_t mod
, rm
, reg
, evexrm
;
609 LLVM_DEBUG(dbgs() << "readModRM()");
611 if (insn
->consumedModRM
)
614 if (consume(insn
, insn
->modRM
))
616 insn
->consumedModRM
= true;
618 mod
= modFromModRM(insn
->modRM
);
619 rm
= rmFromModRM(insn
->modRM
);
620 reg
= regFromModRM(insn
->modRM
);
622 // This goes by insn->registerSize to pick the correct register, which messes
623 // up if we're using (say) XMM or 8-bit register operands. That gets fixed in
625 switch (insn
->registerSize
) {
627 insn
->regBase
= MODRM_REG_AX
;
628 insn
->eaRegBase
= EA_REG_AX
;
631 insn
->regBase
= MODRM_REG_EAX
;
632 insn
->eaRegBase
= EA_REG_EAX
;
635 insn
->regBase
= MODRM_REG_RAX
;
636 insn
->eaRegBase
= EA_REG_RAX
;
640 reg
|= rFromREX(insn
->rexPrefix
) << 3;
641 rm
|= bFromREX(insn
->rexPrefix
) << 3;
644 if (insn
->vectorExtensionType
== TYPE_EVEX
&& insn
->mode
== MODE_64BIT
) {
645 reg
|= r2FromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 4;
646 evexrm
= xFromEVEX2of4(insn
->vectorExtensionPrefix
[1]) << 4;
649 insn
->reg
= (Reg
)(insn
->regBase
+ reg
);
651 switch (insn
->addressSize
) {
653 EABase eaBaseBase
= EA_BASE_BX_SI
;
658 insn
->eaBase
= EA_BASE_NONE
;
659 insn
->eaDisplacement
= EA_DISP_16
;
660 if (readDisplacement(insn
))
663 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
664 insn
->eaDisplacement
= EA_DISP_NONE
;
668 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
669 insn
->eaDisplacement
= EA_DISP_8
;
670 insn
->displacementSize
= 1;
671 if (readDisplacement(insn
))
675 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
676 insn
->eaDisplacement
= EA_DISP_16
;
677 if (readDisplacement(insn
))
681 insn
->eaBase
= (EABase
)(insn
->eaRegBase
+ rm
);
682 if (readDisplacement(insn
))
690 EABase eaBaseBase
= (insn
->addressSize
== 4 ? EA_BASE_EAX
: EA_BASE_RAX
);
694 insn
->eaDisplacement
= EA_DISP_NONE
; // readSIB may override this
695 // In determining whether RIP-relative mode is used (rm=5),
696 // or whether a SIB byte is present (rm=4),
697 // the extension bits (REX.b and EVEX.x) are ignored.
699 case 0x4: // SIB byte is present
700 insn
->eaBase
= (insn
->addressSize
== 4 ? EA_BASE_sib
: EA_BASE_sib64
);
701 if (readSIB(insn
) || readDisplacement(insn
))
704 case 0x5: // RIP-relative
705 insn
->eaBase
= EA_BASE_NONE
;
706 insn
->eaDisplacement
= EA_DISP_32
;
707 if (readDisplacement(insn
))
711 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
716 insn
->displacementSize
= 1;
719 insn
->eaDisplacement
= (mod
== 0x1 ? EA_DISP_8
: EA_DISP_32
);
721 case 0x4: // SIB byte is present
722 insn
->eaBase
= EA_BASE_sib
;
723 if (readSIB(insn
) || readDisplacement(insn
))
727 insn
->eaBase
= (EABase
)(eaBaseBase
+ rm
);
728 if (readDisplacement(insn
))
734 insn
->eaDisplacement
= EA_DISP_NONE
;
735 insn
->eaBase
= (EABase
)(insn
->eaRegBase
+ rm
+ evexrm
);
740 } // switch (insn->addressSize)
745 #define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
746 static uint16_t name(struct InternalInstruction *insn, OperandType type, \
747 uint8_t index, uint8_t *valid) { \
751 debug("Unhandled register type"); \
755 return base + index; \
760 if (insn->rexPrefix && index >= 4 && index <= 7) { \
761 return prefix##_SPL + (index - 4); \
763 return prefix##_AL + index; \
769 return prefix##_AX + index; \
774 return prefix##_EAX + index; \
779 return prefix##_RAX + index; \
781 return prefix##_ZMM0 + index; \
783 return prefix##_YMM0 + index; \
785 return prefix##_XMM0 + index; \
789 return prefix##_TMM0 + index; \
794 return prefix##_K0 + index; \
798 return prefix##_K0_K1 + (index / 2); \
800 return prefix##_MM0 + (index & 0x7); \
801 case TYPE_SEGMENTREG: \
802 if ((index & 7) > 5) \
804 return prefix##_ES + (index & 7); \
805 case TYPE_DEBUGREG: \
806 return prefix##_DR0 + index; \
807 case TYPE_CONTROLREG: \
808 return prefix##_CR0 + index; \
812 return prefix##_BND0 + index; \
814 return prefix##_XMM0 + index; \
816 return prefix##_YMM0 + index; \
818 return prefix##_ZMM0 + index; \
822 // Consult an operand type to determine the meaning of the reg or R/M field. If
823 // the operand is an XMM operand, for example, an operand would be XMM0 instead
824 // of AX, which readModRM() would otherwise misinterpret it as.
826 // @param insn - The instruction containing the operand.
827 // @param type - The operand type.
828 // @param index - The existing value of the field as reported by readModRM().
829 // @param valid - The address of a uint8_t. The target is set to 1 if the
830 // field is valid for the register class; 0 if not.
831 // @return - The proper value.
832 GENERIC_FIXUP_FUNC(fixupRegValue
, insn
->regBase
, MODRM_REG
, 0x1f)
833 GENERIC_FIXUP_FUNC(fixupRMValue
, insn
->eaRegBase
, EA_REG
, 0xf)
835 // Consult an operand specifier to determine which of the fixup*Value functions
836 // to use in correcting readModRM()'ss interpretation.
838 // @param insn - See fixup*Value().
839 // @param op - The operand specifier.
840 // @return - 0 if fixup was successful; -1 if the register returned was
841 // invalid for its class.
842 static int fixupReg(struct InternalInstruction
*insn
,
843 const struct OperandSpecifier
*op
) {
845 LLVM_DEBUG(dbgs() << "fixupReg()");
847 switch ((OperandEncoding
)op
->encoding
) {
849 debug("Expected a REG or R/M encoding in fixupReg");
853 (Reg
)fixupRegValue(insn
, (OperandType
)op
->type
, insn
->vvvv
, &valid
);
858 insn
->reg
= (Reg
)fixupRegValue(insn
, (OperandType
)op
->type
,
859 insn
->reg
- insn
->regBase
, &valid
);
865 if (insn
->eaBase
>= insn
->eaRegBase
) {
866 insn
->eaBase
= (EABase
)fixupRMValue(
867 insn
, (OperandType
)op
->type
, insn
->eaBase
- insn
->eaRegBase
, &valid
);
877 // Read the opcode (except the ModR/M byte in the case of extended or escape
879 static bool readOpcode(struct InternalInstruction
*insn
) {
881 LLVM_DEBUG(dbgs() << "readOpcode()");
883 insn
->opcodeType
= ONEBYTE
;
884 if (insn
->vectorExtensionType
== TYPE_EVEX
) {
885 switch (mmmFromEVEX2of4(insn
->vectorExtensionPrefix
[1])) {
888 dbgs() << format("Unhandled mmm field for instruction (0x%hhx)",
889 mmmFromEVEX2of4(insn
->vectorExtensionPrefix
[1])));
892 insn
->opcodeType
= TWOBYTE
;
893 return consume(insn
, insn
->opcode
);
895 insn
->opcodeType
= THREEBYTE_38
;
896 return consume(insn
, insn
->opcode
);
898 insn
->opcodeType
= THREEBYTE_3A
;
899 return consume(insn
, insn
->opcode
);
901 insn
->opcodeType
= MAP5
;
902 return consume(insn
, insn
->opcode
);
904 insn
->opcodeType
= MAP6
;
905 return consume(insn
, insn
->opcode
);
907 } else if (insn
->vectorExtensionType
== TYPE_VEX_3B
) {
908 switch (mmmmmFromVEX2of3(insn
->vectorExtensionPrefix
[1])) {
911 dbgs() << format("Unhandled m-mmmm field for instruction (0x%hhx)",
912 mmmmmFromVEX2of3(insn
->vectorExtensionPrefix
[1])));
915 insn
->opcodeType
= TWOBYTE
;
916 return consume(insn
, insn
->opcode
);
918 insn
->opcodeType
= THREEBYTE_38
;
919 return consume(insn
, insn
->opcode
);
921 insn
->opcodeType
= THREEBYTE_3A
;
922 return consume(insn
, insn
->opcode
);
924 insn
->opcodeType
= MAP5
;
925 return consume(insn
, insn
->opcode
);
927 insn
->opcodeType
= MAP6
;
928 return consume(insn
, insn
->opcode
);
930 } else if (insn
->vectorExtensionType
== TYPE_VEX_2B
) {
931 insn
->opcodeType
= TWOBYTE
;
932 return consume(insn
, insn
->opcode
);
933 } else if (insn
->vectorExtensionType
== TYPE_XOP
) {
934 switch (mmmmmFromXOP2of3(insn
->vectorExtensionPrefix
[1])) {
937 dbgs() << format("Unhandled m-mmmm field for instruction (0x%hhx)",
938 mmmmmFromVEX2of3(insn
->vectorExtensionPrefix
[1])));
940 case XOP_MAP_SELECT_8
:
941 insn
->opcodeType
= XOP8_MAP
;
942 return consume(insn
, insn
->opcode
);
943 case XOP_MAP_SELECT_9
:
944 insn
->opcodeType
= XOP9_MAP
;
945 return consume(insn
, insn
->opcode
);
946 case XOP_MAP_SELECT_A
:
947 insn
->opcodeType
= XOPA_MAP
;
948 return consume(insn
, insn
->opcode
);
952 if (consume(insn
, current
))
955 if (current
== 0x0f) {
957 dbgs() << format("Found a two-byte escape prefix (0x%hhx)", current
));
958 if (consume(insn
, current
))
961 if (current
== 0x38) {
962 LLVM_DEBUG(dbgs() << format("Found a three-byte escape prefix (0x%hhx)",
964 if (consume(insn
, current
))
967 insn
->opcodeType
= THREEBYTE_38
;
968 } else if (current
== 0x3a) {
969 LLVM_DEBUG(dbgs() << format("Found a three-byte escape prefix (0x%hhx)",
971 if (consume(insn
, current
))
974 insn
->opcodeType
= THREEBYTE_3A
;
975 } else if (current
== 0x0f) {
977 dbgs() << format("Found a 3dnow escape prefix (0x%hhx)", current
));
979 // Consume operands before the opcode to comply with the 3DNow encoding
983 if (consume(insn
, current
))
986 insn
->opcodeType
= THREEDNOW_MAP
;
988 LLVM_DEBUG(dbgs() << "Didn't find a three-byte escape prefix");
989 insn
->opcodeType
= TWOBYTE
;
991 } else if (insn
->mandatoryPrefix
)
992 // The opcode with mandatory prefix must start with opcode escape.
993 // If not it's legacy repeat prefix
994 insn
->mandatoryPrefix
= 0;
996 // At this point we have consumed the full opcode.
997 // Anything we consume from here on must be unconsumed.
998 insn
->opcode
= current
;
1003 // Determine whether equiv is the 16-bit equivalent of orig (32-bit or 64-bit).
1004 static bool is16BitEquivalent(const char *orig
, const char *equiv
) {
1005 for (int i
= 0;; i
++) {
1006 if (orig
[i
] == '\0' && equiv
[i
] == '\0')
1008 if (orig
[i
] == '\0' || equiv
[i
] == '\0')
1010 if (orig
[i
] != equiv
[i
]) {
1011 if ((orig
[i
] == 'Q' || orig
[i
] == 'L') && equiv
[i
] == 'W')
1013 if ((orig
[i
] == '6' || orig
[i
] == '3') && equiv
[i
] == '1')
1015 if ((orig
[i
] == '4' || orig
[i
] == '2') && equiv
[i
] == '6')
1022 // Determine whether this instruction is a 64-bit instruction.
1023 static bool is64Bit(const char *name
) {
1024 for (int i
= 0;; ++i
) {
1025 if (name
[i
] == '\0')
1027 if (name
[i
] == '6' && name
[i
+ 1] == '4')
1032 // Determine the ID of an instruction, consuming the ModR/M byte as appropriate
1033 // for extended and escape opcodes, and using a supplied attribute mask.
1034 static int getInstructionIDWithAttrMask(uint16_t *instructionID
,
1035 struct InternalInstruction
*insn
,
1036 uint16_t attrMask
) {
1037 auto insnCtx
= InstructionContext(x86DisassemblerContexts
[attrMask
]);
1038 const ContextDecision
*decision
;
1039 switch (insn
->opcodeType
) {
1041 decision
= &ONEBYTE_SYM
;
1044 decision
= &TWOBYTE_SYM
;
1047 decision
= &THREEBYTE38_SYM
;
1050 decision
= &THREEBYTE3A_SYM
;
1053 decision
= &XOP8_MAP_SYM
;
1056 decision
= &XOP9_MAP_SYM
;
1059 decision
= &XOPA_MAP_SYM
;
1062 decision
= &THREEDNOW_MAP_SYM
;
1065 decision
= &MAP5_SYM
;
1068 decision
= &MAP6_SYM
;
1072 if (decision
->opcodeDecisions
[insnCtx
]
1073 .modRMDecisions
[insn
->opcode
]
1074 .modrm_type
!= MODRM_ONEENTRY
) {
1075 if (readModRM(insn
))
1078 decode(insn
->opcodeType
, insnCtx
, insn
->opcode
, insn
->modRM
);
1080 *instructionID
= decode(insn
->opcodeType
, insnCtx
, insn
->opcode
, 0);
1086 // Determine the ID of an instruction, consuming the ModR/M byte as appropriate
1087 // for extended and escape opcodes. Determines the attributes and context for
1088 // the instruction before doing so.
1089 static int getInstructionID(struct InternalInstruction
*insn
,
1090 const MCInstrInfo
*mii
) {
1092 uint16_t instructionID
;
1094 LLVM_DEBUG(dbgs() << "getID()");
1096 attrMask
= ATTR_NONE
;
1098 if (insn
->mode
== MODE_64BIT
)
1099 attrMask
|= ATTR_64BIT
;
1101 if (insn
->vectorExtensionType
!= TYPE_NO_VEX_XOP
) {
1102 attrMask
|= (insn
->vectorExtensionType
== TYPE_EVEX
) ? ATTR_EVEX
: ATTR_VEX
;
1104 if (insn
->vectorExtensionType
== TYPE_EVEX
) {
1105 switch (ppFromEVEX3of4(insn
->vectorExtensionPrefix
[2])) {
1107 attrMask
|= ATTR_OPSIZE
;
1110 attrMask
|= ATTR_XS
;
1113 attrMask
|= ATTR_XD
;
1117 if (zFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
1118 attrMask
|= ATTR_EVEXKZ
;
1119 if (bFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
1120 attrMask
|= ATTR_EVEXB
;
1121 if (aaaFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
1122 attrMask
|= ATTR_EVEXK
;
1123 if (lFromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
1124 attrMask
|= ATTR_VEXL
;
1125 if (l2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
1126 attrMask
|= ATTR_EVEXL2
;
1127 } else if (insn
->vectorExtensionType
== TYPE_VEX_3B
) {
1128 switch (ppFromVEX3of3(insn
->vectorExtensionPrefix
[2])) {
1130 attrMask
|= ATTR_OPSIZE
;
1133 attrMask
|= ATTR_XS
;
1136 attrMask
|= ATTR_XD
;
1140 if (lFromVEX3of3(insn
->vectorExtensionPrefix
[2]))
1141 attrMask
|= ATTR_VEXL
;
1142 } else if (insn
->vectorExtensionType
== TYPE_VEX_2B
) {
1143 switch (ppFromVEX2of2(insn
->vectorExtensionPrefix
[1])) {
1145 attrMask
|= ATTR_OPSIZE
;
1146 if (insn
->hasAdSize
)
1147 attrMask
|= ATTR_ADSIZE
;
1150 attrMask
|= ATTR_XS
;
1153 attrMask
|= ATTR_XD
;
1157 if (lFromVEX2of2(insn
->vectorExtensionPrefix
[1]))
1158 attrMask
|= ATTR_VEXL
;
1159 } else if (insn
->vectorExtensionType
== TYPE_XOP
) {
1160 switch (ppFromXOP3of3(insn
->vectorExtensionPrefix
[2])) {
1162 attrMask
|= ATTR_OPSIZE
;
1165 attrMask
|= ATTR_XS
;
1168 attrMask
|= ATTR_XD
;
1172 if (lFromXOP3of3(insn
->vectorExtensionPrefix
[2]))
1173 attrMask
|= ATTR_VEXL
;
1177 } else if (!insn
->mandatoryPrefix
) {
1178 // If we don't have mandatory prefix we should use legacy prefixes here
1179 if (insn
->hasOpSize
&& (insn
->mode
!= MODE_16BIT
))
1180 attrMask
|= ATTR_OPSIZE
;
1181 if (insn
->hasAdSize
)
1182 attrMask
|= ATTR_ADSIZE
;
1183 if (insn
->opcodeType
== ONEBYTE
) {
1184 if (insn
->repeatPrefix
== 0xf3 && (insn
->opcode
== 0x90))
1185 // Special support for PAUSE
1186 attrMask
|= ATTR_XS
;
1188 if (insn
->repeatPrefix
== 0xf2)
1189 attrMask
|= ATTR_XD
;
1190 else if (insn
->repeatPrefix
== 0xf3)
1191 attrMask
|= ATTR_XS
;
1194 switch (insn
->mandatoryPrefix
) {
1196 attrMask
|= ATTR_XD
;
1199 attrMask
|= ATTR_XS
;
1202 if (insn
->mode
!= MODE_16BIT
)
1203 attrMask
|= ATTR_OPSIZE
;
1204 if (insn
->hasAdSize
)
1205 attrMask
|= ATTR_ADSIZE
;
1208 attrMask
|= ATTR_ADSIZE
;
1213 if (insn
->rexPrefix
& 0x08) {
1214 attrMask
|= ATTR_REXW
;
1215 attrMask
&= ~ATTR_ADSIZE
;
1218 if (insn
->mode
== MODE_16BIT
) {
1219 // JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1220 // of the AdSize prefix is inverted w.r.t. 32-bit mode.
1221 if (insn
->opcodeType
== ONEBYTE
&& insn
->opcode
== 0xE3)
1222 attrMask
^= ATTR_ADSIZE
;
1223 // If we're in 16-bit mode and this is one of the relative jumps and opsize
1224 // prefix isn't present, we need to force the opsize attribute since the
1225 // prefix is inverted relative to 32-bit mode.
1226 if (!insn
->hasOpSize
&& insn
->opcodeType
== ONEBYTE
&&
1227 (insn
->opcode
== 0xE8 || insn
->opcode
== 0xE9))
1228 attrMask
|= ATTR_OPSIZE
;
1230 if (!insn
->hasOpSize
&& insn
->opcodeType
== TWOBYTE
&&
1231 insn
->opcode
>= 0x80 && insn
->opcode
<= 0x8F)
1232 attrMask
|= ATTR_OPSIZE
;
1236 if (getInstructionIDWithAttrMask(&instructionID
, insn
, attrMask
))
1239 // The following clauses compensate for limitations of the tables.
1241 if (insn
->mode
!= MODE_64BIT
&&
1242 insn
->vectorExtensionType
!= TYPE_NO_VEX_XOP
) {
1243 // The tables can't distinquish between cases where the W-bit is used to
1244 // select register size and cases where its a required part of the opcode.
1245 if ((insn
->vectorExtensionType
== TYPE_EVEX
&&
1246 wFromEVEX3of4(insn
->vectorExtensionPrefix
[2])) ||
1247 (insn
->vectorExtensionType
== TYPE_VEX_3B
&&
1248 wFromVEX3of3(insn
->vectorExtensionPrefix
[2])) ||
1249 (insn
->vectorExtensionType
== TYPE_XOP
&&
1250 wFromXOP3of3(insn
->vectorExtensionPrefix
[2]))) {
1252 uint16_t instructionIDWithREXW
;
1253 if (getInstructionIDWithAttrMask(&instructionIDWithREXW
, insn
,
1254 attrMask
| ATTR_REXW
)) {
1255 insn
->instructionID
= instructionID
;
1256 insn
->spec
= &INSTRUCTIONS_SYM
[instructionID
];
1260 auto SpecName
= mii
->getName(instructionIDWithREXW
);
1261 // If not a 64-bit instruction. Switch the opcode.
1262 if (!is64Bit(SpecName
.data())) {
1263 insn
->instructionID
= instructionIDWithREXW
;
1264 insn
->spec
= &INSTRUCTIONS_SYM
[instructionIDWithREXW
];
1270 // Absolute moves, umonitor, and movdir64b need special handling.
1271 // -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1273 // -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1275 if ((insn
->opcodeType
== ONEBYTE
&& ((insn
->opcode
& 0xFC) == 0xA0)) ||
1276 (insn
->opcodeType
== TWOBYTE
&& (insn
->opcode
== 0xAE)) ||
1277 (insn
->opcodeType
== THREEBYTE_38
&& insn
->opcode
== 0xF8)) {
1278 // Make sure we observed the prefixes in any position.
1279 if (insn
->hasAdSize
)
1280 attrMask
|= ATTR_ADSIZE
;
1281 if (insn
->hasOpSize
)
1282 attrMask
|= ATTR_OPSIZE
;
1284 // In 16-bit, invert the attributes.
1285 if (insn
->mode
== MODE_16BIT
) {
1286 attrMask
^= ATTR_ADSIZE
;
1288 // The OpSize attribute is only valid with the absolute moves.
1289 if (insn
->opcodeType
== ONEBYTE
&& ((insn
->opcode
& 0xFC) == 0xA0))
1290 attrMask
^= ATTR_OPSIZE
;
1293 if (getInstructionIDWithAttrMask(&instructionID
, insn
, attrMask
))
1296 insn
->instructionID
= instructionID
;
1297 insn
->spec
= &INSTRUCTIONS_SYM
[instructionID
];
1301 if ((insn
->mode
== MODE_16BIT
|| insn
->hasOpSize
) &&
1302 !(attrMask
& ATTR_OPSIZE
)) {
1303 // The instruction tables make no distinction between instructions that
1304 // allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1305 // particular spot (i.e., many MMX operations). In general we're
1306 // conservative, but in the specific case where OpSize is present but not in
1307 // the right place we check if there's a 16-bit operation.
1308 const struct InstructionSpecifier
*spec
;
1309 uint16_t instructionIDWithOpsize
;
1310 llvm::StringRef specName
, specWithOpSizeName
;
1312 spec
= &INSTRUCTIONS_SYM
[instructionID
];
1314 if (getInstructionIDWithAttrMask(&instructionIDWithOpsize
, insn
,
1315 attrMask
| ATTR_OPSIZE
)) {
1316 // ModRM required with OpSize but not present. Give up and return the
1317 // version without OpSize set.
1318 insn
->instructionID
= instructionID
;
1323 specName
= mii
->getName(instructionID
);
1324 specWithOpSizeName
= mii
->getName(instructionIDWithOpsize
);
1326 if (is16BitEquivalent(specName
.data(), specWithOpSizeName
.data()) &&
1327 (insn
->mode
== MODE_16BIT
) ^ insn
->hasOpSize
) {
1328 insn
->instructionID
= instructionIDWithOpsize
;
1329 insn
->spec
= &INSTRUCTIONS_SYM
[instructionIDWithOpsize
];
1331 insn
->instructionID
= instructionID
;
1337 if (insn
->opcodeType
== ONEBYTE
&& insn
->opcode
== 0x90 &&
1338 insn
->rexPrefix
& 0x01) {
1339 // NOOP shouldn't decode as NOOP if REX.b is set. Instead it should decode
1340 // as XCHG %r8, %eax.
1341 const struct InstructionSpecifier
*spec
;
1342 uint16_t instructionIDWithNewOpcode
;
1343 const struct InstructionSpecifier
*specWithNewOpcode
;
1345 spec
= &INSTRUCTIONS_SYM
[instructionID
];
1347 // Borrow opcode from one of the other XCHGar opcodes
1348 insn
->opcode
= 0x91;
1350 if (getInstructionIDWithAttrMask(&instructionIDWithNewOpcode
, insn
,
1352 insn
->opcode
= 0x90;
1354 insn
->instructionID
= instructionID
;
1359 specWithNewOpcode
= &INSTRUCTIONS_SYM
[instructionIDWithNewOpcode
];
1362 insn
->opcode
= 0x90;
1364 insn
->instructionID
= instructionIDWithNewOpcode
;
1365 insn
->spec
= specWithNewOpcode
;
1370 insn
->instructionID
= instructionID
;
1371 insn
->spec
= &INSTRUCTIONS_SYM
[insn
->instructionID
];
1376 // Read an operand from the opcode field of an instruction and interprets it
1377 // appropriately given the operand width. Handles AddRegFrm instructions.
1379 // @param insn - the instruction whose opcode field is to be read.
1380 // @param size - The width (in bytes) of the register being specified.
1381 // 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1383 // @return - 0 on success; nonzero otherwise.
1384 static int readOpcodeRegister(struct InternalInstruction
*insn
, uint8_t size
) {
1385 LLVM_DEBUG(dbgs() << "readOpcodeRegister()");
1388 size
= insn
->registerSize
;
1392 insn
->opcodeRegister
= (Reg
)(
1393 MODRM_REG_AL
+ ((bFromREX(insn
->rexPrefix
) << 3) | (insn
->opcode
& 7)));
1394 if (insn
->rexPrefix
&& insn
->opcodeRegister
>= MODRM_REG_AL
+ 0x4 &&
1395 insn
->opcodeRegister
< MODRM_REG_AL
+ 0x8) {
1396 insn
->opcodeRegister
=
1397 (Reg
)(MODRM_REG_SPL
+ (insn
->opcodeRegister
- MODRM_REG_AL
- 4));
1402 insn
->opcodeRegister
= (Reg
)(
1403 MODRM_REG_AX
+ ((bFromREX(insn
->rexPrefix
) << 3) | (insn
->opcode
& 7)));
1406 insn
->opcodeRegister
=
1407 (Reg
)(MODRM_REG_EAX
+
1408 ((bFromREX(insn
->rexPrefix
) << 3) | (insn
->opcode
& 7)));
1411 insn
->opcodeRegister
=
1412 (Reg
)(MODRM_REG_RAX
+
1413 ((bFromREX(insn
->rexPrefix
) << 3) | (insn
->opcode
& 7)));
1420 // Consume an immediate operand from an instruction, given the desired operand
1423 // @param insn - The instruction whose operand is to be read.
1424 // @param size - The width (in bytes) of the operand.
1425 // @return - 0 if the immediate was successfully consumed; nonzero
1427 static int readImmediate(struct InternalInstruction
*insn
, uint8_t size
) {
1433 LLVM_DEBUG(dbgs() << "readImmediate()");
1435 assert(insn
->numImmediatesConsumed
< 2 && "Already consumed two immediates");
1437 insn
->immediateSize
= size
;
1438 insn
->immediateOffset
= insn
->readerCursor
- insn
->startLocation
;
1442 if (consume(insn
, imm8
))
1444 insn
->immediates
[insn
->numImmediatesConsumed
] = imm8
;
1447 if (consume(insn
, imm16
))
1449 insn
->immediates
[insn
->numImmediatesConsumed
] = imm16
;
1452 if (consume(insn
, imm32
))
1454 insn
->immediates
[insn
->numImmediatesConsumed
] = imm32
;
1457 if (consume(insn
, imm64
))
1459 insn
->immediates
[insn
->numImmediatesConsumed
] = imm64
;
1462 llvm_unreachable("invalid size");
1465 insn
->numImmediatesConsumed
++;
1470 // Consume vvvv from an instruction if it has a VEX prefix.
1471 static int readVVVV(struct InternalInstruction
*insn
) {
1472 LLVM_DEBUG(dbgs() << "readVVVV()");
1475 if (insn
->vectorExtensionType
== TYPE_EVEX
)
1476 vvvv
= (v2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]) << 4 |
1477 vvvvFromEVEX3of4(insn
->vectorExtensionPrefix
[2]));
1478 else if (insn
->vectorExtensionType
== TYPE_VEX_3B
)
1479 vvvv
= vvvvFromVEX3of3(insn
->vectorExtensionPrefix
[2]);
1480 else if (insn
->vectorExtensionType
== TYPE_VEX_2B
)
1481 vvvv
= vvvvFromVEX2of2(insn
->vectorExtensionPrefix
[1]);
1482 else if (insn
->vectorExtensionType
== TYPE_XOP
)
1483 vvvv
= vvvvFromXOP3of3(insn
->vectorExtensionPrefix
[2]);
1487 if (insn
->mode
!= MODE_64BIT
)
1488 vvvv
&= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
1490 insn
->vvvv
= static_cast<Reg
>(vvvv
);
1494 // Read an mask register from the opcode field of an instruction.
1496 // @param insn - The instruction whose opcode field is to be read.
1497 // @return - 0 on success; nonzero otherwise.
1498 static int readMaskRegister(struct InternalInstruction
*insn
) {
1499 LLVM_DEBUG(dbgs() << "readMaskRegister()");
1501 if (insn
->vectorExtensionType
!= TYPE_EVEX
)
1505 static_cast<Reg
>(aaaFromEVEX4of4(insn
->vectorExtensionPrefix
[3]));
1509 // Consults the specifier for an instruction and consumes all
1510 // operands for that instruction, interpreting them as it goes.
1511 static int readOperands(struct InternalInstruction
*insn
) {
1512 int hasVVVV
, needVVVV
;
1515 LLVM_DEBUG(dbgs() << "readOperands()");
1517 // If non-zero vvvv specified, make sure one of the operands uses it.
1518 hasVVVV
= !readVVVV(insn
);
1519 needVVVV
= hasVVVV
&& (insn
->vvvv
!= 0);
1521 for (const auto &Op
: x86OperandSets
[insn
->spec
->operands
]) {
1522 switch (Op
.encoding
) {
1528 // VSIB can use the V2 bit so check only the other bits.
1530 needVVVV
= hasVVVV
& ((insn
->vvvv
& 0xf) != 0);
1531 if (readModRM(insn
))
1534 // Reject if SIB wasn't used.
1535 if (insn
->eaBase
!= EA_BASE_sib
&& insn
->eaBase
!= EA_BASE_sib64
)
1538 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1539 if (insn
->sibIndex
== SIB_INDEX_NONE
)
1540 insn
->sibIndex
= (SIBIndex
)(insn
->sibIndexBase
+ 4);
1542 // If EVEX.v2 is set this is one of the 16-31 registers.
1543 if (insn
->vectorExtensionType
== TYPE_EVEX
&& insn
->mode
== MODE_64BIT
&&
1544 v2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]))
1545 insn
->sibIndex
= (SIBIndex
)(insn
->sibIndex
+ 16);
1547 // Adjust the index register to the correct size.
1548 switch ((OperandType
)Op
.type
) {
1550 debug("Unhandled VSIB index type");
1554 (SIBIndex
)(SIB_INDEX_XMM0
+ (insn
->sibIndex
- insn
->sibIndexBase
));
1558 (SIBIndex
)(SIB_INDEX_YMM0
+ (insn
->sibIndex
- insn
->sibIndexBase
));
1562 (SIBIndex
)(SIB_INDEX_ZMM0
+ (insn
->sibIndex
- insn
->sibIndexBase
));
1566 // Apply the AVX512 compressed displacement scaling factor.
1567 if (Op
.encoding
!= ENCODING_REG
&& insn
->eaDisplacement
== EA_DISP_8
)
1568 insn
->displacement
*= 1 << (Op
.encoding
- ENCODING_VSIB
);
1571 // Reject if SIB wasn't used.
1572 if (insn
->eaBase
!= EA_BASE_sib
&& insn
->eaBase
!= EA_BASE_sib64
)
1574 if (readModRM(insn
))
1576 if (fixupReg(insn
, &Op
))
1581 if (readModRM(insn
))
1583 if (fixupReg(insn
, &Op
))
1585 // Apply the AVX512 compressed displacement scaling factor.
1586 if (Op
.encoding
!= ENCODING_REG
&& insn
->eaDisplacement
== EA_DISP_8
)
1587 insn
->displacement
*= 1 << (Op
.encoding
- ENCODING_RM
);
1591 // Saw a register immediate so don't read again and instead split the
1592 // previous immediate. FIXME: This is a hack.
1593 insn
->immediates
[insn
->numImmediatesConsumed
] =
1594 insn
->immediates
[insn
->numImmediatesConsumed
- 1] & 0xf;
1595 ++insn
->numImmediatesConsumed
;
1598 if (readImmediate(insn
, 1))
1600 if (Op
.type
== TYPE_XMM
|| Op
.type
== TYPE_YMM
)
1604 if (readImmediate(insn
, 2))
1608 if (readImmediate(insn
, 4))
1612 if (readImmediate(insn
, 8))
1616 if (readImmediate(insn
, insn
->immediateSize
))
1620 if (readImmediate(insn
, insn
->addressSize
))
1624 insn
->RC
= (l2FromEVEX4of4(insn
->vectorExtensionPrefix
[3]) << 1) |
1625 lFromEVEX4of4(insn
->vectorExtensionPrefix
[3]);
1628 if (readOpcodeRegister(insn
, 1))
1632 if (readOpcodeRegister(insn
, 2))
1636 if (readOpcodeRegister(insn
, 4))
1640 if (readOpcodeRegister(insn
, 8))
1644 if (readOpcodeRegister(insn
, 0))
1648 insn
->immediates
[1] = insn
->opcode
& 0xf;
1653 needVVVV
= 0; // Mark that we have found a VVVV operand.
1656 if (insn
->mode
!= MODE_64BIT
)
1657 insn
->vvvv
= static_cast<Reg
>(insn
->vvvv
& 0x7);
1658 if (fixupReg(insn
, &Op
))
1661 case ENCODING_WRITEMASK
:
1662 if (readMaskRegister(insn
))
1668 LLVM_DEBUG(dbgs() << "Encountered an operand with an unknown encoding.");
1673 // If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail
1682 // Fill-ins to make the compiler happy. These constants are never actually
1683 // assigned; they are just filler to make an automatically-generated switch
1698 static bool translateInstruction(MCInst
&target
,
1699 InternalInstruction
&source
,
1700 const MCDisassembler
*Dis
);
1704 /// Generic disassembler for all X86 platforms. All each platform class should
1705 /// have to do is subclass the constructor, and provide a different
1706 /// disassemblerMode value.
1707 class X86GenericDisassembler
: public MCDisassembler
{
1708 std::unique_ptr
<const MCInstrInfo
> MII
;
1710 X86GenericDisassembler(const MCSubtargetInfo
&STI
, MCContext
&Ctx
,
1711 std::unique_ptr
<const MCInstrInfo
> MII
);
1713 DecodeStatus
getInstruction(MCInst
&instr
, uint64_t &size
,
1714 ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
1715 raw_ostream
&cStream
) const override
;
1718 DisassemblerMode fMode
;
1723 X86GenericDisassembler::X86GenericDisassembler(
1724 const MCSubtargetInfo
&STI
,
1726 std::unique_ptr
<const MCInstrInfo
> MII
)
1727 : MCDisassembler(STI
, Ctx
), MII(std::move(MII
)) {
1728 const FeatureBitset
&FB
= STI
.getFeatureBits();
1729 if (FB
[X86::Mode16Bit
]) {
1732 } else if (FB
[X86::Mode32Bit
]) {
1735 } else if (FB
[X86::Mode64Bit
]) {
1740 llvm_unreachable("Invalid CPU mode");
1743 MCDisassembler::DecodeStatus
X86GenericDisassembler::getInstruction(
1744 MCInst
&Instr
, uint64_t &Size
, ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
1745 raw_ostream
&CStream
) const {
1746 CommentStream
= &CStream
;
1748 InternalInstruction Insn
;
1749 memset(&Insn
, 0, sizeof(InternalInstruction
));
1751 Insn
.startLocation
= Address
;
1752 Insn
.readerCursor
= Address
;
1755 if (Bytes
.empty() || readPrefixes(&Insn
) || readOpcode(&Insn
) ||
1756 getInstructionID(&Insn
, MII
.get()) || Insn
.instructionID
== 0 ||
1757 readOperands(&Insn
)) {
1758 Size
= Insn
.readerCursor
- Address
;
1762 Insn
.operands
= x86OperandSets
[Insn
.spec
->operands
];
1763 Insn
.length
= Insn
.readerCursor
- Insn
.startLocation
;
1766 LLVM_DEBUG(dbgs() << "Instruction exceeds 15-byte limit");
1768 bool Ret
= translateInstruction(Instr
, Insn
, this);
1770 unsigned Flags
= X86::IP_NO_PREFIX
;
1772 Flags
|= X86::IP_HAS_AD_SIZE
;
1773 if (!Insn
.mandatoryPrefix
) {
1775 Flags
|= X86::IP_HAS_OP_SIZE
;
1776 if (Insn
.repeatPrefix
== 0xf2)
1777 Flags
|= X86::IP_HAS_REPEAT_NE
;
1778 else if (Insn
.repeatPrefix
== 0xf3 &&
1779 // It should not be 'pause' f3 90
1780 Insn
.opcode
!= 0x90)
1781 Flags
|= X86::IP_HAS_REPEAT
;
1782 if (Insn
.hasLockPrefix
)
1783 Flags
|= X86::IP_HAS_LOCK
;
1785 Instr
.setFlags(Flags
);
1787 return (!Ret
) ? Success
: Fail
;
1791 // Private code that translates from struct InternalInstructions to MCInsts.
1794 /// translateRegister - Translates an internal register to the appropriate LLVM
1795 /// register, and appends it as an operand to an MCInst.
1797 /// @param mcInst - The MCInst to append to.
1798 /// @param reg - The Reg to append.
1799 static void translateRegister(MCInst
&mcInst
, Reg reg
) {
1800 #define ENTRY(x) X86::x,
1801 static constexpr MCPhysReg llvmRegnums
[] = {ALL_REGS
};
1804 MCPhysReg llvmRegnum
= llvmRegnums
[reg
];
1805 mcInst
.addOperand(MCOperand::createReg(llvmRegnum
));
1808 /// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
1809 /// immediate Value in the MCInst.
1811 /// @param Value - The immediate Value, has had any PC adjustment made by
1813 /// @param isBranch - If the instruction is a branch instruction
1814 /// @param Address - The starting address of the instruction
1815 /// @param Offset - The byte offset to this immediate in the instruction
1816 /// @param Width - The byte width of this immediate in the instruction
1818 /// If the getOpInfo() function was set when setupForSymbolicDisassembly() was
1819 /// called then that function is called to get any symbolic information for the
1820 /// immediate in the instruction using the Address, Offset and Width. If that
1821 /// returns non-zero then the symbolic information it returns is used to create
1822 /// an MCExpr and that is added as an operand to the MCInst. If getOpInfo()
1823 /// returns zero and isBranch is true then a symbol look up for immediate Value
1824 /// is done and if a symbol is found an MCExpr is created with that, else
1825 /// an MCExpr with the immediate Value is created. This function returns true
1826 /// if it adds an operand to the MCInst and false otherwise.
1827 static bool tryAddingSymbolicOperand(int64_t Value
, bool isBranch
,
1828 uint64_t Address
, uint64_t Offset
,
1829 uint64_t Width
, MCInst
&MI
,
1830 const MCDisassembler
*Dis
) {
1831 return Dis
->tryAddingSymbolicOperand(MI
, Value
, Address
, isBranch
,
1835 /// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
1836 /// referenced by a load instruction with the base register that is the rip.
1837 /// These can often be addresses in a literal pool. The Address of the
1838 /// instruction and its immediate Value are used to determine the address
1839 /// being referenced in the literal pool entry. The SymbolLookUp call back will
1840 /// return a pointer to a literal 'C' string if the referenced address is an
1841 /// address into a section with 'C' string literals.
1842 static void tryAddingPcLoadReferenceComment(uint64_t Address
, uint64_t Value
,
1843 const void *Decoder
) {
1844 const MCDisassembler
*Dis
= static_cast<const MCDisassembler
*>(Decoder
);
1845 Dis
->tryAddingPcLoadReferenceComment(Value
, Address
);
1848 static const uint8_t segmentRegnums
[SEG_OVERRIDE_max
] = {
1849 0, // SEG_OVERRIDE_NONE
1858 /// translateSrcIndex - Appends a source index operand to an MCInst.
1860 /// @param mcInst - The MCInst to append to.
1861 /// @param insn - The internal instruction.
1862 static bool translateSrcIndex(MCInst
&mcInst
, InternalInstruction
&insn
) {
1865 if (insn
.mode
== MODE_64BIT
)
1866 baseRegNo
= insn
.hasAdSize
? X86::ESI
: X86::RSI
;
1867 else if (insn
.mode
== MODE_32BIT
)
1868 baseRegNo
= insn
.hasAdSize
? X86::SI
: X86::ESI
;
1870 assert(insn
.mode
== MODE_16BIT
);
1871 baseRegNo
= insn
.hasAdSize
? X86::ESI
: X86::SI
;
1873 MCOperand baseReg
= MCOperand::createReg(baseRegNo
);
1874 mcInst
.addOperand(baseReg
);
1876 MCOperand segmentReg
;
1877 segmentReg
= MCOperand::createReg(segmentRegnums
[insn
.segmentOverride
]);
1878 mcInst
.addOperand(segmentReg
);
1882 /// translateDstIndex - Appends a destination index operand to an MCInst.
1884 /// @param mcInst - The MCInst to append to.
1885 /// @param insn - The internal instruction.
1887 static bool translateDstIndex(MCInst
&mcInst
, InternalInstruction
&insn
) {
1890 if (insn
.mode
== MODE_64BIT
)
1891 baseRegNo
= insn
.hasAdSize
? X86::EDI
: X86::RDI
;
1892 else if (insn
.mode
== MODE_32BIT
)
1893 baseRegNo
= insn
.hasAdSize
? X86::DI
: X86::EDI
;
1895 assert(insn
.mode
== MODE_16BIT
);
1896 baseRegNo
= insn
.hasAdSize
? X86::EDI
: X86::DI
;
1898 MCOperand baseReg
= MCOperand::createReg(baseRegNo
);
1899 mcInst
.addOperand(baseReg
);
1903 /// translateImmediate - Appends an immediate operand to an MCInst.
1905 /// @param mcInst - The MCInst to append to.
1906 /// @param immediate - The immediate value to append.
1907 /// @param operand - The operand, as stored in the descriptor table.
1908 /// @param insn - The internal instruction.
1909 static void translateImmediate(MCInst
&mcInst
, uint64_t immediate
,
1910 const OperandSpecifier
&operand
,
1911 InternalInstruction
&insn
,
1912 const MCDisassembler
*Dis
) {
1913 // Sign-extend the immediate if necessary.
1915 OperandType type
= (OperandType
)operand
.type
;
1917 bool isBranch
= false;
1919 if (type
== TYPE_REL
) {
1921 pcrel
= insn
.startLocation
+
1922 insn
.immediateOffset
+ insn
.immediateSize
;
1923 switch (operand
.encoding
) {
1927 switch (insn
.displacementSize
) {
1931 if(immediate
& 0x80)
1932 immediate
|= ~(0xffull
);
1935 if(immediate
& 0x8000)
1936 immediate
|= ~(0xffffull
);
1939 if(immediate
& 0x80000000)
1940 immediate
|= ~(0xffffffffull
);
1947 if(immediate
& 0x80)
1948 immediate
|= ~(0xffull
);
1951 if(immediate
& 0x8000)
1952 immediate
|= ~(0xffffull
);
1955 if(immediate
& 0x80000000)
1956 immediate
|= ~(0xffffffffull
);
1960 // By default sign-extend all X86 immediates based on their encoding.
1961 else if (type
== TYPE_IMM
) {
1962 switch (operand
.encoding
) {
1966 if(immediate
& 0x80)
1967 immediate
|= ~(0xffull
);
1970 if(immediate
& 0x8000)
1971 immediate
|= ~(0xffffull
);
1974 if(immediate
& 0x80000000)
1975 immediate
|= ~(0xffffffffull
);
1984 mcInst
.addOperand(MCOperand::createReg(X86::XMM0
+ (immediate
>> 4)));
1987 mcInst
.addOperand(MCOperand::createReg(X86::YMM0
+ (immediate
>> 4)));
1990 mcInst
.addOperand(MCOperand::createReg(X86::ZMM0
+ (immediate
>> 4)));
1993 // operand is 64 bits wide. Do nothing.
1997 if(!tryAddingSymbolicOperand(immediate
+ pcrel
, isBranch
, insn
.startLocation
,
1998 insn
.immediateOffset
, insn
.immediateSize
,
2000 mcInst
.addOperand(MCOperand::createImm(immediate
));
2002 if (type
== TYPE_MOFFS
) {
2003 MCOperand segmentReg
;
2004 segmentReg
= MCOperand::createReg(segmentRegnums
[insn
.segmentOverride
]);
2005 mcInst
.addOperand(segmentReg
);
2009 /// translateRMRegister - Translates a register stored in the R/M field of the
2010 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
2011 /// @param mcInst - The MCInst to append to.
2012 /// @param insn - The internal instruction to extract the R/M field
2014 /// @return - 0 on success; -1 otherwise
2015 static bool translateRMRegister(MCInst
&mcInst
,
2016 InternalInstruction
&insn
) {
2017 if (insn
.eaBase
== EA_BASE_sib
|| insn
.eaBase
== EA_BASE_sib64
) {
2018 debug("A R/M register operand may not have a SIB byte");
2022 switch (insn
.eaBase
) {
2024 debug("Unexpected EA base register");
2027 debug("EA_BASE_NONE for ModR/M base");
2029 #define ENTRY(x) case EA_BASE_##x:
2032 debug("A R/M register operand may not have a base; "
2033 "the operand must be a register.");
2037 mcInst.addOperand(MCOperand::createReg(X86::x)); break;
2045 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
2046 /// fields of an internal instruction (and possibly its SIB byte) to a memory
2047 /// operand in LLVM's format, and appends it to an MCInst.
2049 /// @param mcInst - The MCInst to append to.
2050 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
2052 /// @param ForceSIB - The instruction must use SIB.
2053 /// @return - 0 on success; nonzero otherwise
2054 static bool translateRMMemory(MCInst
&mcInst
, InternalInstruction
&insn
,
2055 const MCDisassembler
*Dis
,
2056 bool ForceSIB
= false) {
2057 // Addresses in an MCInst are represented as five operands:
2058 // 1. basereg (register) The R/M base, or (if there is a SIB) the
2060 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
2062 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
2063 // the index (which is multiplied by the
2065 // 4. displacement (immediate) 0, or the displacement if there is one
2066 // 5. segmentreg (register) x86_registerNONE for now, but could be set
2067 // if we have segment overrides
2070 MCOperand scaleAmount
;
2072 MCOperand displacement
;
2073 MCOperand segmentReg
;
2076 if (insn
.eaBase
== EA_BASE_sib
|| insn
.eaBase
== EA_BASE_sib64
) {
2077 if (insn
.sibBase
!= SIB_BASE_NONE
) {
2078 switch (insn
.sibBase
) {
2080 debug("Unexpected sibBase");
2083 case SIB_BASE_##x: \
2084 baseReg = MCOperand::createReg(X86::x); break;
2089 baseReg
= MCOperand::createReg(X86::NoRegister
);
2092 if (insn
.sibIndex
!= SIB_INDEX_NONE
) {
2093 switch (insn
.sibIndex
) {
2095 debug("Unexpected sibIndex");
2098 case SIB_INDEX_##x: \
2099 indexReg = MCOperand::createReg(X86::x); break;
2108 // Use EIZ/RIZ for a few ambiguous cases where the SIB byte is present,
2109 // but no index is used and modrm alone should have been enough.
2110 // -No base register in 32-bit mode. In 64-bit mode this is used to
2111 // avoid rip-relative addressing.
2112 // -Any base register used other than ESP/RSP/R12D/R12. Using these as a
2113 // base always requires a SIB byte.
2114 // -A scale other than 1 is used.
2116 (insn
.sibScale
!= 1 ||
2117 (insn
.sibBase
== SIB_BASE_NONE
&& insn
.mode
!= MODE_64BIT
) ||
2118 (insn
.sibBase
!= SIB_BASE_NONE
&&
2119 insn
.sibBase
!= SIB_BASE_ESP
&& insn
.sibBase
!= SIB_BASE_RSP
&&
2120 insn
.sibBase
!= SIB_BASE_R12D
&& insn
.sibBase
!= SIB_BASE_R12
))) {
2121 indexReg
= MCOperand::createReg(insn
.addressSize
== 4 ? X86::EIZ
:
2124 indexReg
= MCOperand::createReg(X86::NoRegister
);
2127 scaleAmount
= MCOperand::createImm(insn
.sibScale
);
2129 switch (insn
.eaBase
) {
2131 if (insn
.eaDisplacement
== EA_DISP_NONE
) {
2132 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
2135 if (insn
.mode
== MODE_64BIT
){
2136 pcrel
= insn
.startLocation
+
2137 insn
.displacementOffset
+ insn
.displacementSize
;
2138 tryAddingPcLoadReferenceComment(insn
.startLocation
+
2139 insn
.displacementOffset
,
2140 insn
.displacement
+ pcrel
, Dis
);
2142 baseReg
= MCOperand::createReg(insn
.addressSize
== 4 ? X86::EIP
:
2146 baseReg
= MCOperand::createReg(X86::NoRegister
);
2148 indexReg
= MCOperand::createReg(X86::NoRegister
);
2151 baseReg
= MCOperand::createReg(X86::BX
);
2152 indexReg
= MCOperand::createReg(X86::SI
);
2155 baseReg
= MCOperand::createReg(X86::BX
);
2156 indexReg
= MCOperand::createReg(X86::DI
);
2159 baseReg
= MCOperand::createReg(X86::BP
);
2160 indexReg
= MCOperand::createReg(X86::SI
);
2163 baseReg
= MCOperand::createReg(X86::BP
);
2164 indexReg
= MCOperand::createReg(X86::DI
);
2167 indexReg
= MCOperand::createReg(X86::NoRegister
);
2168 switch (insn
.eaBase
) {
2170 debug("Unexpected eaBase");
2172 // Here, we will use the fill-ins defined above. However,
2173 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
2174 // sib and sib64 were handled in the top-level if, so they're only
2175 // placeholders to keep the compiler happy.
2178 baseReg = MCOperand::createReg(X86::x); break;
2181 #define ENTRY(x) case EA_REG_##x:
2184 debug("A R/M memory operand may not be a register; "
2185 "the base field must be a base.");
2190 scaleAmount
= MCOperand::createImm(1);
2193 displacement
= MCOperand::createImm(insn
.displacement
);
2195 segmentReg
= MCOperand::createReg(segmentRegnums
[insn
.segmentOverride
]);
2197 mcInst
.addOperand(baseReg
);
2198 mcInst
.addOperand(scaleAmount
);
2199 mcInst
.addOperand(indexReg
);
2200 if(!tryAddingSymbolicOperand(insn
.displacement
+ pcrel
, false,
2201 insn
.startLocation
, insn
.displacementOffset
,
2202 insn
.displacementSize
, mcInst
, Dis
))
2203 mcInst
.addOperand(displacement
);
2204 mcInst
.addOperand(segmentReg
);
2208 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
2209 /// byte of an instruction to LLVM form, and appends it to an MCInst.
2211 /// @param mcInst - The MCInst to append to.
2212 /// @param operand - The operand, as stored in the descriptor table.
2213 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
2215 /// @return - 0 on success; nonzero otherwise
2216 static bool translateRM(MCInst
&mcInst
, const OperandSpecifier
&operand
,
2217 InternalInstruction
&insn
, const MCDisassembler
*Dis
) {
2218 switch (operand
.type
) {
2220 debug("Unexpected type for a R/M operand");
2235 case TYPE_CONTROLREG
:
2237 return translateRMRegister(mcInst
, insn
);
2242 return translateRMMemory(mcInst
, insn
, Dis
);
2244 return translateRMMemory(mcInst
, insn
, Dis
, true);
2248 /// translateFPRegister - Translates a stack position on the FPU stack to its
2249 /// LLVM form, and appends it to an MCInst.
2251 /// @param mcInst - The MCInst to append to.
2252 /// @param stackPos - The stack position to translate.
2253 static void translateFPRegister(MCInst
&mcInst
,
2255 mcInst
.addOperand(MCOperand::createReg(X86::ST0
+ stackPos
));
2258 /// translateMaskRegister - Translates a 3-bit mask register number to
2259 /// LLVM form, and appends it to an MCInst.
2261 /// @param mcInst - The MCInst to append to.
2262 /// @param maskRegNum - Number of mask register from 0 to 7.
2263 /// @return - false on success; true otherwise.
2264 static bool translateMaskRegister(MCInst
&mcInst
,
2265 uint8_t maskRegNum
) {
2266 if (maskRegNum
>= 8) {
2267 debug("Invalid mask register number");
2271 mcInst
.addOperand(MCOperand::createReg(X86::K0
+ maskRegNum
));
2275 /// translateOperand - Translates an operand stored in an internal instruction
2276 /// to LLVM's format and appends it to an MCInst.
2278 /// @param mcInst - The MCInst to append to.
2279 /// @param operand - The operand, as stored in the descriptor table.
2280 /// @param insn - The internal instruction.
2281 /// @return - false on success; true otherwise.
2282 static bool translateOperand(MCInst
&mcInst
, const OperandSpecifier
&operand
,
2283 InternalInstruction
&insn
,
2284 const MCDisassembler
*Dis
) {
2285 switch (operand
.encoding
) {
2287 debug("Unhandled operand encoding during translation");
2290 translateRegister(mcInst
, insn
.reg
);
2292 case ENCODING_WRITEMASK
:
2293 return translateMaskRegister(mcInst
, insn
.writemask
);
2297 return translateRM(mcInst
, operand
, insn
, Dis
);
2304 translateImmediate(mcInst
,
2305 insn
.immediates
[insn
.numImmediatesTranslated
++],
2311 mcInst
.addOperand(MCOperand::createImm(insn
.RC
));
2314 return translateSrcIndex(mcInst
, insn
);
2316 return translateDstIndex(mcInst
, insn
);
2322 translateRegister(mcInst
, insn
.opcodeRegister
);
2325 mcInst
.addOperand(MCOperand::createImm(insn
.immediates
[1]));
2328 translateFPRegister(mcInst
, insn
.modRM
& 7);
2331 translateRegister(mcInst
, insn
.vvvv
);
2334 return translateOperand(mcInst
, insn
.operands
[operand
.type
- TYPE_DUP0
],
2339 /// translateInstruction - Translates an internal instruction and all its
2340 /// operands to an MCInst.
2342 /// @param mcInst - The MCInst to populate with the instruction's data.
2343 /// @param insn - The internal instruction.
2344 /// @return - false on success; true otherwise.
2345 static bool translateInstruction(MCInst
&mcInst
,
2346 InternalInstruction
&insn
,
2347 const MCDisassembler
*Dis
) {
2349 debug("Instruction has no specification");
2354 mcInst
.setOpcode(insn
.instructionID
);
2355 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
2356 // prefix bytes should be disassembled as xrelease and xacquire then set the
2357 // opcode to those instead of the rep and repne opcodes.
2358 if (insn
.xAcquireRelease
) {
2359 if(mcInst
.getOpcode() == X86::REP_PREFIX
)
2360 mcInst
.setOpcode(X86::XRELEASE_PREFIX
);
2361 else if(mcInst
.getOpcode() == X86::REPNE_PREFIX
)
2362 mcInst
.setOpcode(X86::XACQUIRE_PREFIX
);
2365 insn
.numImmediatesTranslated
= 0;
2367 for (const auto &Op
: insn
.operands
) {
2368 if (Op
.encoding
!= ENCODING_NONE
) {
2369 if (translateOperand(mcInst
, Op
, insn
, Dis
)) {
2378 static MCDisassembler
*createX86Disassembler(const Target
&T
,
2379 const MCSubtargetInfo
&STI
,
2381 std::unique_ptr
<const MCInstrInfo
> MII(T
.createMCInstrInfo());
2382 return new X86GenericDisassembler(STI
, Ctx
, std::move(MII
));
2385 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeX86Disassembler() {
2386 // Register the disassembler.
2387 TargetRegistry::RegisterMCDisassembler(getTheX86_32Target(),
2388 createX86Disassembler
);
2389 TargetRegistry::RegisterMCDisassembler(getTheX86_64Target(),
2390 createX86Disassembler
);