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/TargetRegistry.h"
88 #include "llvm/Support/raw_ostream.h"
91 using namespace llvm::X86Disassembler
;
93 #define DEBUG_TYPE "x86-disassembler"
95 void llvm::X86Disassembler::Debug(const char *file
, unsigned line
,
97 dbgs() << file
<< ":" << line
<< ": " << s
;
100 StringRef
llvm::X86Disassembler::GetInstrName(unsigned Opcode
,
102 const MCInstrInfo
*MII
= static_cast<const MCInstrInfo
*>(mii
);
103 return MII
->getName(Opcode
);
106 #define debug(s) LLVM_DEBUG(Debug(__FILE__, __LINE__, s));
110 // Fill-ins to make the compiler happy. These constants are never actually
111 // assigned; they are just filler to make an automatically-generated switch
126 static bool translateInstruction(MCInst
&target
,
127 InternalInstruction
&source
,
128 const MCDisassembler
*Dis
);
132 /// Generic disassembler for all X86 platforms. All each platform class should
133 /// have to do is subclass the constructor, and provide a different
134 /// disassemblerMode value.
135 class X86GenericDisassembler
: public MCDisassembler
{
136 std::unique_ptr
<const MCInstrInfo
> MII
;
138 X86GenericDisassembler(const MCSubtargetInfo
&STI
, MCContext
&Ctx
,
139 std::unique_ptr
<const MCInstrInfo
> MII
);
141 DecodeStatus
getInstruction(MCInst
&instr
, uint64_t &size
,
142 ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
143 raw_ostream
&vStream
,
144 raw_ostream
&cStream
) const override
;
147 DisassemblerMode fMode
;
152 X86GenericDisassembler::X86GenericDisassembler(
153 const MCSubtargetInfo
&STI
,
155 std::unique_ptr
<const MCInstrInfo
> MII
)
156 : MCDisassembler(STI
, Ctx
), MII(std::move(MII
)) {
157 const FeatureBitset
&FB
= STI
.getFeatureBits();
158 if (FB
[X86::Mode16Bit
]) {
161 } else if (FB
[X86::Mode32Bit
]) {
164 } else if (FB
[X86::Mode64Bit
]) {
169 llvm_unreachable("Invalid CPU mode");
174 ArrayRef
<uint8_t> Bytes
;
176 Region(ArrayRef
<uint8_t> Bytes
, uint64_t Base
) : Bytes(Bytes
), Base(Base
) {}
178 } // end anonymous namespace
180 /// A callback function that wraps the readByte method from Region.
182 /// @param Arg - The generic callback parameter. In this case, this should
183 /// be a pointer to a Region.
184 /// @param Byte - A pointer to the byte to be read.
185 /// @param Address - The address to be read.
186 static int regionReader(const void *Arg
, uint8_t *Byte
, uint64_t Address
) {
187 auto *R
= static_cast<const Region
*>(Arg
);
188 ArrayRef
<uint8_t> Bytes
= R
->Bytes
;
189 unsigned Index
= Address
- R
->Base
;
190 if (Bytes
.size() <= Index
)
192 *Byte
= Bytes
[Index
];
196 /// logger - a callback function that wraps the operator<< method from
199 /// @param arg - The generic callback parameter. This should be a pointe
200 /// to a raw_ostream.
201 /// @param log - A string to be logged. logger() adds a newline.
202 static void logger(void* arg
, const char* log
) {
206 raw_ostream
&vStream
= *(static_cast<raw_ostream
*>(arg
));
207 vStream
<< log
<< "\n";
211 // Public interface for the disassembler
214 MCDisassembler::DecodeStatus
X86GenericDisassembler::getInstruction(
215 MCInst
&Instr
, uint64_t &Size
, ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
216 raw_ostream
&VStream
, raw_ostream
&CStream
) const {
217 CommentStream
= &CStream
;
219 InternalInstruction InternalInstr
;
221 dlog_t LoggerFn
= logger
;
222 if (&VStream
== &nulls())
223 LoggerFn
= nullptr; // Disable logging completely if it's going to nulls().
225 Region
R(Bytes
, Address
);
227 int Ret
= decodeInstruction(&InternalInstr
, regionReader
, (const void *)&R
,
228 LoggerFn
, (void *)&VStream
,
229 (const void *)MII
.get(), Address
, fMode
);
232 Size
= InternalInstr
.readerCursor
- Address
;
235 Size
= InternalInstr
.length
;
236 bool Ret
= translateInstruction(Instr
, InternalInstr
, this);
238 unsigned Flags
= X86::IP_NO_PREFIX
;
239 if (InternalInstr
.hasAdSize
)
240 Flags
|= X86::IP_HAS_AD_SIZE
;
241 if (!InternalInstr
.mandatoryPrefix
) {
242 if (InternalInstr
.hasOpSize
)
243 Flags
|= X86::IP_HAS_OP_SIZE
;
244 if (InternalInstr
.repeatPrefix
== 0xf2)
245 Flags
|= X86::IP_HAS_REPEAT_NE
;
246 else if (InternalInstr
.repeatPrefix
== 0xf3 &&
247 // It should not be 'pause' f3 90
248 InternalInstr
.opcode
!= 0x90)
249 Flags
|= X86::IP_HAS_REPEAT
;
250 if (InternalInstr
.hasLockPrefix
)
251 Flags
|= X86::IP_HAS_LOCK
;
253 Instr
.setFlags(Flags
);
255 return (!Ret
) ? Success
: Fail
;
260 // Private code that translates from struct InternalInstructions to MCInsts.
263 /// translateRegister - Translates an internal register to the appropriate LLVM
264 /// register, and appends it as an operand to an MCInst.
266 /// @param mcInst - The MCInst to append to.
267 /// @param reg - The Reg to append.
268 static void translateRegister(MCInst
&mcInst
, Reg reg
) {
269 #define ENTRY(x) X86::x,
270 static constexpr MCPhysReg llvmRegnums
[] = {ALL_REGS
};
273 MCPhysReg llvmRegnum
= llvmRegnums
[reg
];
274 mcInst
.addOperand(MCOperand::createReg(llvmRegnum
));
277 /// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
278 /// immediate Value in the MCInst.
280 /// @param Value - The immediate Value, has had any PC adjustment made by
282 /// @param isBranch - If the instruction is a branch instruction
283 /// @param Address - The starting address of the instruction
284 /// @param Offset - The byte offset to this immediate in the instruction
285 /// @param Width - The byte width of this immediate in the instruction
287 /// If the getOpInfo() function was set when setupForSymbolicDisassembly() was
288 /// called then that function is called to get any symbolic information for the
289 /// immediate in the instruction using the Address, Offset and Width. If that
290 /// returns non-zero then the symbolic information it returns is used to create
291 /// an MCExpr and that is added as an operand to the MCInst. If getOpInfo()
292 /// returns zero and isBranch is true then a symbol look up for immediate Value
293 /// is done and if a symbol is found an MCExpr is created with that, else
294 /// an MCExpr with the immediate Value is created. This function returns true
295 /// if it adds an operand to the MCInst and false otherwise.
296 static bool tryAddingSymbolicOperand(int64_t Value
, bool isBranch
,
297 uint64_t Address
, uint64_t Offset
,
298 uint64_t Width
, MCInst
&MI
,
299 const MCDisassembler
*Dis
) {
300 return Dis
->tryAddingSymbolicOperand(MI
, Value
, Address
, isBranch
,
304 /// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
305 /// referenced by a load instruction with the base register that is the rip.
306 /// These can often be addresses in a literal pool. The Address of the
307 /// instruction and its immediate Value are used to determine the address
308 /// being referenced in the literal pool entry. The SymbolLookUp call back will
309 /// return a pointer to a literal 'C' string if the referenced address is an
310 /// address into a section with 'C' string literals.
311 static void tryAddingPcLoadReferenceComment(uint64_t Address
, uint64_t Value
,
312 const void *Decoder
) {
313 const MCDisassembler
*Dis
= static_cast<const MCDisassembler
*>(Decoder
);
314 Dis
->tryAddingPcLoadReferenceComment(Value
, Address
);
317 static const uint8_t segmentRegnums
[SEG_OVERRIDE_max
] = {
318 0, // SEG_OVERRIDE_NONE
327 /// translateSrcIndex - Appends a source index operand to an MCInst.
329 /// @param mcInst - The MCInst to append to.
330 /// @param insn - The internal instruction.
331 static bool translateSrcIndex(MCInst
&mcInst
, InternalInstruction
&insn
) {
334 if (insn
.mode
== MODE_64BIT
)
335 baseRegNo
= insn
.hasAdSize
? X86::ESI
: X86::RSI
;
336 else if (insn
.mode
== MODE_32BIT
)
337 baseRegNo
= insn
.hasAdSize
? X86::SI
: X86::ESI
;
339 assert(insn
.mode
== MODE_16BIT
);
340 baseRegNo
= insn
.hasAdSize
? X86::ESI
: X86::SI
;
342 MCOperand baseReg
= MCOperand::createReg(baseRegNo
);
343 mcInst
.addOperand(baseReg
);
345 MCOperand segmentReg
;
346 segmentReg
= MCOperand::createReg(segmentRegnums
[insn
.segmentOverride
]);
347 mcInst
.addOperand(segmentReg
);
351 /// translateDstIndex - Appends a destination index operand to an MCInst.
353 /// @param mcInst - The MCInst to append to.
354 /// @param insn - The internal instruction.
356 static bool translateDstIndex(MCInst
&mcInst
, InternalInstruction
&insn
) {
359 if (insn
.mode
== MODE_64BIT
)
360 baseRegNo
= insn
.hasAdSize
? X86::EDI
: X86::RDI
;
361 else if (insn
.mode
== MODE_32BIT
)
362 baseRegNo
= insn
.hasAdSize
? X86::DI
: X86::EDI
;
364 assert(insn
.mode
== MODE_16BIT
);
365 baseRegNo
= insn
.hasAdSize
? X86::EDI
: X86::DI
;
367 MCOperand baseReg
= MCOperand::createReg(baseRegNo
);
368 mcInst
.addOperand(baseReg
);
372 /// translateImmediate - Appends an immediate operand to an MCInst.
374 /// @param mcInst - The MCInst to append to.
375 /// @param immediate - The immediate value to append.
376 /// @param operand - The operand, as stored in the descriptor table.
377 /// @param insn - The internal instruction.
378 static void translateImmediate(MCInst
&mcInst
, uint64_t immediate
,
379 const OperandSpecifier
&operand
,
380 InternalInstruction
&insn
,
381 const MCDisassembler
*Dis
) {
382 // Sign-extend the immediate if necessary.
384 OperandType type
= (OperandType
)operand
.type
;
386 bool isBranch
= false;
388 if (type
== TYPE_REL
) {
390 pcrel
= insn
.startLocation
+
391 insn
.immediateOffset
+ insn
.immediateSize
;
392 switch (operand
.encoding
) {
396 switch (insn
.displacementSize
) {
401 immediate
|= ~(0xffull
);
404 if(immediate
& 0x8000)
405 immediate
|= ~(0xffffull
);
408 if(immediate
& 0x80000000)
409 immediate
|= ~(0xffffffffull
);
417 immediate
|= ~(0xffull
);
420 if(immediate
& 0x8000)
421 immediate
|= ~(0xffffull
);
424 if(immediate
& 0x80000000)
425 immediate
|= ~(0xffffffffull
);
429 // By default sign-extend all X86 immediates based on their encoding.
430 else if (type
== TYPE_IMM
) {
431 switch (operand
.encoding
) {
436 immediate
|= ~(0xffull
);
439 if(immediate
& 0x8000)
440 immediate
|= ~(0xffffull
);
443 if(immediate
& 0x80000000)
444 immediate
|= ~(0xffffffffull
);
453 mcInst
.addOperand(MCOperand::createReg(X86::XMM0
+ (immediate
>> 4)));
456 mcInst
.addOperand(MCOperand::createReg(X86::YMM0
+ (immediate
>> 4)));
459 mcInst
.addOperand(MCOperand::createReg(X86::ZMM0
+ (immediate
>> 4)));
462 // operand is 64 bits wide. Do nothing.
466 if(!tryAddingSymbolicOperand(immediate
+ pcrel
, isBranch
, insn
.startLocation
,
467 insn
.immediateOffset
, insn
.immediateSize
,
469 mcInst
.addOperand(MCOperand::createImm(immediate
));
471 if (type
== TYPE_MOFFS
) {
472 MCOperand segmentReg
;
473 segmentReg
= MCOperand::createReg(segmentRegnums
[insn
.segmentOverride
]);
474 mcInst
.addOperand(segmentReg
);
478 /// translateRMRegister - Translates a register stored in the R/M field of the
479 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
480 /// @param mcInst - The MCInst to append to.
481 /// @param insn - The internal instruction to extract the R/M field
483 /// @return - 0 on success; -1 otherwise
484 static bool translateRMRegister(MCInst
&mcInst
,
485 InternalInstruction
&insn
) {
486 if (insn
.eaBase
== EA_BASE_sib
|| insn
.eaBase
== EA_BASE_sib64
) {
487 debug("A R/M register operand may not have a SIB byte");
491 switch (insn
.eaBase
) {
493 debug("Unexpected EA base register");
496 debug("EA_BASE_NONE for ModR/M base");
498 #define ENTRY(x) case EA_BASE_##x:
501 debug("A R/M register operand may not have a base; "
502 "the operand must be a register.");
506 mcInst.addOperand(MCOperand::createReg(X86::x)); break;
514 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
515 /// fields of an internal instruction (and possibly its SIB byte) to a memory
516 /// operand in LLVM's format, and appends it to an MCInst.
518 /// @param mcInst - The MCInst to append to.
519 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
521 /// @return - 0 on success; nonzero otherwise
522 static bool translateRMMemory(MCInst
&mcInst
, InternalInstruction
&insn
,
523 const MCDisassembler
*Dis
) {
524 // Addresses in an MCInst are represented as five operands:
525 // 1. basereg (register) The R/M base, or (if there is a SIB) the
527 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
529 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
530 // the index (which is multiplied by the
532 // 4. displacement (immediate) 0, or the displacement if there is one
533 // 5. segmentreg (register) x86_registerNONE for now, but could be set
534 // if we have segment overrides
537 MCOperand scaleAmount
;
539 MCOperand displacement
;
540 MCOperand segmentReg
;
543 if (insn
.eaBase
== EA_BASE_sib
|| insn
.eaBase
== EA_BASE_sib64
) {
544 if (insn
.sibBase
!= SIB_BASE_NONE
) {
545 switch (insn
.sibBase
) {
547 debug("Unexpected sibBase");
551 baseReg = MCOperand::createReg(X86::x); break;
556 baseReg
= MCOperand::createReg(X86::NoRegister
);
559 if (insn
.sibIndex
!= SIB_INDEX_NONE
) {
560 switch (insn
.sibIndex
) {
562 debug("Unexpected sibIndex");
565 case SIB_INDEX_##x: \
566 indexReg = MCOperand::createReg(X86::x); break;
575 // Use EIZ/RIZ for a few ambiguous cases where the SIB byte is present,
576 // but no index is used and modrm alone should have been enough.
577 // -No base register in 32-bit mode. In 64-bit mode this is used to
578 // avoid rip-relative addressing.
579 // -Any base register used other than ESP/RSP/R12D/R12. Using these as a
580 // base always requires a SIB byte.
581 // -A scale other than 1 is used.
582 if (insn
.sibScale
!= 1 ||
583 (insn
.sibBase
== SIB_BASE_NONE
&& insn
.mode
!= MODE_64BIT
) ||
584 (insn
.sibBase
!= SIB_BASE_NONE
&&
585 insn
.sibBase
!= SIB_BASE_ESP
&& insn
.sibBase
!= SIB_BASE_RSP
&&
586 insn
.sibBase
!= SIB_BASE_R12D
&& insn
.sibBase
!= SIB_BASE_R12
)) {
587 indexReg
= MCOperand::createReg(insn
.addressSize
== 4 ? X86::EIZ
:
590 indexReg
= MCOperand::createReg(X86::NoRegister
);
593 scaleAmount
= MCOperand::createImm(insn
.sibScale
);
595 switch (insn
.eaBase
) {
597 if (insn
.eaDisplacement
== EA_DISP_NONE
) {
598 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
601 if (insn
.mode
== MODE_64BIT
){
602 pcrel
= insn
.startLocation
+
603 insn
.displacementOffset
+ insn
.displacementSize
;
604 tryAddingPcLoadReferenceComment(insn
.startLocation
+
605 insn
.displacementOffset
,
606 insn
.displacement
+ pcrel
, Dis
);
608 baseReg
= MCOperand::createReg(insn
.addressSize
== 4 ? X86::EIP
:
612 baseReg
= MCOperand::createReg(X86::NoRegister
);
614 indexReg
= MCOperand::createReg(X86::NoRegister
);
617 baseReg
= MCOperand::createReg(X86::BX
);
618 indexReg
= MCOperand::createReg(X86::SI
);
621 baseReg
= MCOperand::createReg(X86::BX
);
622 indexReg
= MCOperand::createReg(X86::DI
);
625 baseReg
= MCOperand::createReg(X86::BP
);
626 indexReg
= MCOperand::createReg(X86::SI
);
629 baseReg
= MCOperand::createReg(X86::BP
);
630 indexReg
= MCOperand::createReg(X86::DI
);
633 indexReg
= MCOperand::createReg(X86::NoRegister
);
634 switch (insn
.eaBase
) {
636 debug("Unexpected eaBase");
638 // Here, we will use the fill-ins defined above. However,
639 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
640 // sib and sib64 were handled in the top-level if, so they're only
641 // placeholders to keep the compiler happy.
644 baseReg = MCOperand::createReg(X86::x); break;
647 #define ENTRY(x) case EA_REG_##x:
650 debug("A R/M memory operand may not be a register; "
651 "the base field must be a base.");
656 scaleAmount
= MCOperand::createImm(1);
659 displacement
= MCOperand::createImm(insn
.displacement
);
661 segmentReg
= MCOperand::createReg(segmentRegnums
[insn
.segmentOverride
]);
663 mcInst
.addOperand(baseReg
);
664 mcInst
.addOperand(scaleAmount
);
665 mcInst
.addOperand(indexReg
);
666 if(!tryAddingSymbolicOperand(insn
.displacement
+ pcrel
, false,
667 insn
.startLocation
, insn
.displacementOffset
,
668 insn
.displacementSize
, mcInst
, Dis
))
669 mcInst
.addOperand(displacement
);
670 mcInst
.addOperand(segmentReg
);
674 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
675 /// byte of an instruction to LLVM form, and appends it to an MCInst.
677 /// @param mcInst - The MCInst to append to.
678 /// @param operand - The operand, as stored in the descriptor table.
679 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
681 /// @return - 0 on success; nonzero otherwise
682 static bool translateRM(MCInst
&mcInst
, const OperandSpecifier
&operand
,
683 InternalInstruction
&insn
, const MCDisassembler
*Dis
) {
684 switch (operand
.type
) {
686 debug("Unexpected type for a R/M operand");
700 case TYPE_CONTROLREG
:
702 return translateRMRegister(mcInst
, insn
);
707 return translateRMMemory(mcInst
, insn
, Dis
);
711 /// translateFPRegister - Translates a stack position on the FPU stack to its
712 /// LLVM form, and appends it to an MCInst.
714 /// @param mcInst - The MCInst to append to.
715 /// @param stackPos - The stack position to translate.
716 static void translateFPRegister(MCInst
&mcInst
,
718 mcInst
.addOperand(MCOperand::createReg(X86::ST0
+ stackPos
));
721 /// translateMaskRegister - Translates a 3-bit mask register number to
722 /// LLVM form, and appends it to an MCInst.
724 /// @param mcInst - The MCInst to append to.
725 /// @param maskRegNum - Number of mask register from 0 to 7.
726 /// @return - false on success; true otherwise.
727 static bool translateMaskRegister(MCInst
&mcInst
,
728 uint8_t maskRegNum
) {
729 if (maskRegNum
>= 8) {
730 debug("Invalid mask register number");
734 mcInst
.addOperand(MCOperand::createReg(X86::K0
+ maskRegNum
));
738 /// translateOperand - Translates an operand stored in an internal instruction
739 /// to LLVM's format and appends it to an MCInst.
741 /// @param mcInst - The MCInst to append to.
742 /// @param operand - The operand, as stored in the descriptor table.
743 /// @param insn - The internal instruction.
744 /// @return - false on success; true otherwise.
745 static bool translateOperand(MCInst
&mcInst
, const OperandSpecifier
&operand
,
746 InternalInstruction
&insn
,
747 const MCDisassembler
*Dis
) {
748 switch (operand
.encoding
) {
750 debug("Unhandled operand encoding during translation");
753 translateRegister(mcInst
, insn
.reg
);
755 case ENCODING_WRITEMASK
:
756 return translateMaskRegister(mcInst
, insn
.writemask
);
759 return translateRM(mcInst
, operand
, insn
, Dis
);
766 translateImmediate(mcInst
,
767 insn
.immediates
[insn
.numImmediatesTranslated
++],
773 mcInst
.addOperand(MCOperand::createImm(insn
.RC
));
776 return translateSrcIndex(mcInst
, insn
);
778 return translateDstIndex(mcInst
, insn
);
784 translateRegister(mcInst
, insn
.opcodeRegister
);
787 mcInst
.addOperand(MCOperand::createImm(insn
.immediates
[1]));
790 translateFPRegister(mcInst
, insn
.modRM
& 7);
793 translateRegister(mcInst
, insn
.vvvv
);
796 return translateOperand(mcInst
, insn
.operands
[operand
.type
- TYPE_DUP0
],
801 /// translateInstruction - Translates an internal instruction and all its
802 /// operands to an MCInst.
804 /// @param mcInst - The MCInst to populate with the instruction's data.
805 /// @param insn - The internal instruction.
806 /// @return - false on success; true otherwise.
807 static bool translateInstruction(MCInst
&mcInst
,
808 InternalInstruction
&insn
,
809 const MCDisassembler
*Dis
) {
811 debug("Instruction has no specification");
816 mcInst
.setOpcode(insn
.instructionID
);
817 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
818 // prefix bytes should be disassembled as xrelease and xacquire then set the
819 // opcode to those instead of the rep and repne opcodes.
820 if (insn
.xAcquireRelease
) {
821 if(mcInst
.getOpcode() == X86::REP_PREFIX
)
822 mcInst
.setOpcode(X86::XRELEASE_PREFIX
);
823 else if(mcInst
.getOpcode() == X86::REPNE_PREFIX
)
824 mcInst
.setOpcode(X86::XACQUIRE_PREFIX
);
827 insn
.numImmediatesTranslated
= 0;
829 for (const auto &Op
: insn
.operands
) {
830 if (Op
.encoding
!= ENCODING_NONE
) {
831 if (translateOperand(mcInst
, Op
, insn
, Dis
)) {
840 static MCDisassembler
*createX86Disassembler(const Target
&T
,
841 const MCSubtargetInfo
&STI
,
843 std::unique_ptr
<const MCInstrInfo
> MII(T
.createMCInstrInfo());
844 return new X86GenericDisassembler(STI
, Ctx
, std::move(MII
));
847 extern "C" void LLVMInitializeX86Disassembler() {
848 // Register the disassembler.
849 TargetRegistry::RegisterMCDisassembler(getTheX86_32Target(),
850 createX86Disassembler
);
851 TargetRegistry::RegisterMCDisassembler(getTheX86_64Target(),
852 createX86Disassembler
);