1 //===- X86RecognizableInstr.cpp - Disassembler instruction spec --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is part of the X86 Disassembler Emitter.
11 // It contains the implementation of a single recognizable instruction.
12 // Documentation for the disassembler emitter in general can be found in
13 // X86DisasemblerEmitter.h.
15 //===----------------------------------------------------------------------===//
17 #include "X86DisassemblerShared.h"
18 #include "X86RecognizableInstr.h"
19 #include "X86ModRMFilters.h"
21 #include "llvm/Support/ErrorHandling.h"
41 // A clone of X86 since we can't depend on something that is generated.
51 MRM0r
= 16, MRM1r
= 17, MRM2r
= 18, MRM3r
= 19,
52 MRM4r
= 20, MRM5r
= 21, MRM6r
= 22, MRM7r
= 23,
53 MRM0m
= 24, MRM1m
= 25, MRM2m
= 26, MRM3m
= 27,
54 MRM4m
= 28, MRM5m
= 29, MRM6m
= 30, MRM7m
= 31,
56 #define MAP(from, to) MRM_##from = to,
67 D8
= 3, D9
= 4, DA
= 5, DB
= 6,
68 DC
= 7, DD
= 8, DE
= 9, DF
= 10,
75 // If rows are added to the opcode extension tables, then corresponding entries
76 // must be added here.
78 // If the row corresponds to a single byte (i.e., 8f), then add an entry for
79 // that byte to ONE_BYTE_EXTENSION_TABLES.
81 // If the row corresponds to two bytes where the first is 0f, add an entry for
82 // the second byte to TWO_BYTE_EXTENSION_TABLES.
84 // If the row corresponds to some other set of bytes, you will need to modify
85 // the code in RecognizableInstr::emitDecodePath() as well, and add new prefixes
86 // to the X86 TD files, except in two cases: if the first two bytes of such a
87 // new combination are 0f 38 or 0f 3a, you just have to add maps called
88 // THREE_BYTE_38_EXTENSION_TABLES and THREE_BYTE_3A_EXTENSION_TABLES and add a
89 // switch(Opcode) just below the case X86Local::T8: or case X86Local::TA: line
90 // in RecognizableInstr::emitDecodePath().
92 #define ONE_BYTE_EXTENSION_TABLES \
100 EXTENSION_TABLE(c6) \
101 EXTENSION_TABLE(c7) \
102 EXTENSION_TABLE(d0) \
103 EXTENSION_TABLE(d1) \
104 EXTENSION_TABLE(d2) \
105 EXTENSION_TABLE(d3) \
106 EXTENSION_TABLE(f6) \
107 EXTENSION_TABLE(f7) \
108 EXTENSION_TABLE(fe) \
111 #define TWO_BYTE_EXTENSION_TABLES \
112 EXTENSION_TABLE(00) \
113 EXTENSION_TABLE(01) \
114 EXTENSION_TABLE(18) \
115 EXTENSION_TABLE(71) \
116 EXTENSION_TABLE(72) \
117 EXTENSION_TABLE(73) \
118 EXTENSION_TABLE(ae) \
119 EXTENSION_TABLE(ba) \
122 using namespace X86Disassembler
;
124 /// needsModRMForDecode - Indicates whether a particular instruction requires a
125 /// ModR/M byte for the instruction to be properly decoded. For example, a
126 /// MRMDestReg instruction needs the Mod field in the ModR/M byte to be set to
129 /// @param form - The form of the instruction.
130 /// @return - true if the form implies that a ModR/M byte is required, false
132 static bool needsModRMForDecode(uint8_t form
) {
133 if (form
== X86Local::MRMDestReg
||
134 form
== X86Local::MRMDestMem
||
135 form
== X86Local::MRMSrcReg
||
136 form
== X86Local::MRMSrcMem
||
137 (form
>= X86Local::MRM0r
&& form
<= X86Local::MRM7r
) ||
138 (form
>= X86Local::MRM0m
&& form
<= X86Local::MRM7m
))
144 /// isRegFormat - Indicates whether a particular form requires the Mod field of
145 /// the ModR/M byte to be 0b11.
147 /// @param form - The form of the instruction.
148 /// @return - true if the form implies that Mod must be 0b11, false
150 static bool isRegFormat(uint8_t form
) {
151 if (form
== X86Local::MRMDestReg
||
152 form
== X86Local::MRMSrcReg
||
153 (form
>= X86Local::MRM0r
&& form
<= X86Local::MRM7r
))
159 /// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
160 /// Useful for switch statements and the like.
162 /// @param init - A reference to the BitsInit to be decoded.
163 /// @return - The field, with the first bit in the BitsInit as the lowest
165 static uint8_t byteFromBitsInit(BitsInit
&init
) {
166 int width
= init
.getNumBits();
168 assert(width
<= 8 && "Field is too large for uint8_t!");
175 for (index
= 0; index
< width
; index
++) {
176 if (static_cast<BitInit
*>(init
.getBit(index
))->getValue())
185 /// byteFromRec - Extract a value at most 8 bits in with from a Record given the
186 /// name of the field.
188 /// @param rec - The record from which to extract the value.
189 /// @param name - The name of the field in the record.
190 /// @return - The field, as translated by byteFromBitsInit().
191 static uint8_t byteFromRec(const Record
* rec
, const std::string
&name
) {
192 BitsInit
* bits
= rec
->getValueAsBitsInit(name
);
193 return byteFromBitsInit(*bits
);
196 RecognizableInstr::RecognizableInstr(DisassemblerTables
&tables
,
197 const CodeGenInstruction
&insn
,
202 Name
= Rec
->getName();
203 Spec
= &tables
.specForUID(UID
);
205 if (!Rec
->isSubClassOf("X86Inst")) {
206 ShouldBeEmitted
= false;
210 Prefix
= byteFromRec(Rec
, "Prefix");
211 Opcode
= byteFromRec(Rec
, "Opcode");
212 Form
= byteFromRec(Rec
, "FormBits");
213 SegOvr
= byteFromRec(Rec
, "SegOvrBits");
215 HasOpSizePrefix
= Rec
->getValueAsBit("hasOpSizePrefix");
216 HasREX_WPrefix
= Rec
->getValueAsBit("hasREX_WPrefix");
217 HasVEXPrefix
= Rec
->getValueAsBit("hasVEXPrefix");
218 HasVEX_4VPrefix
= Rec
->getValueAsBit("hasVEX_4VPrefix");
219 HasVEX_WPrefix
= Rec
->getValueAsBit("hasVEX_WPrefix");
220 HasLockPrefix
= Rec
->getValueAsBit("hasLockPrefix");
221 IsCodeGenOnly
= Rec
->getValueAsBit("isCodeGenOnly");
223 Name
= Rec
->getName();
224 AsmString
= Rec
->getValueAsString("AsmString");
226 Operands
= &insn
.Operands
.OperandList
;
228 IsSSE
= HasOpSizePrefix
&& (Name
.find("16") == Name
.npos
);
229 HasFROperands
= hasFROperands();
230 HasVEX_LPrefix
= has256BitOperands() || Rec
->getValueAsBit("hasVEX_L");
232 ShouldBeEmitted
= true;
235 void RecognizableInstr::processInstr(DisassemblerTables
&tables
,
236 const CodeGenInstruction
&insn
,
239 // Ignore "asm parser only" instructions.
240 if (insn
.TheDef
->getValueAsBit("isAsmParserOnly"))
243 RecognizableInstr
recogInstr(tables
, insn
, uid
);
245 recogInstr
.emitInstructionSpecifier(tables
);
247 if (recogInstr
.shouldBeEmitted())
248 recogInstr
.emitDecodePath(tables
);
251 InstructionContext
RecognizableInstr::insnContext() const {
252 InstructionContext insnContext
;
254 if (HasVEX_4VPrefix
|| HasVEXPrefix
) {
255 if (HasOpSizePrefix
&& HasVEX_LPrefix
)
256 insnContext
= IC_VEX_L_OPSIZE
;
257 else if (HasOpSizePrefix
&& HasVEX_WPrefix
)
258 insnContext
= IC_VEX_W_OPSIZE
;
259 else if (HasOpSizePrefix
)
260 insnContext
= IC_VEX_OPSIZE
;
261 else if (HasVEX_LPrefix
&& Prefix
== X86Local::XS
)
262 insnContext
= IC_VEX_L_XS
;
263 else if (HasVEX_LPrefix
&& Prefix
== X86Local::XD
)
264 insnContext
= IC_VEX_L_XD
;
265 else if (HasVEX_WPrefix
&& Prefix
== X86Local::XS
)
266 insnContext
= IC_VEX_W_XS
;
267 else if (HasVEX_WPrefix
&& Prefix
== X86Local::XD
)
268 insnContext
= IC_VEX_W_XD
;
269 else if (HasVEX_WPrefix
)
270 insnContext
= IC_VEX_W
;
271 else if (HasVEX_LPrefix
)
272 insnContext
= IC_VEX_L
;
273 else if (Prefix
== X86Local::XD
)
274 insnContext
= IC_VEX_XD
;
275 else if (Prefix
== X86Local::XS
)
276 insnContext
= IC_VEX_XS
;
278 insnContext
= IC_VEX
;
279 } else if (Name
.find("64") != Name
.npos
|| HasREX_WPrefix
) {
280 if (HasREX_WPrefix
&& HasOpSizePrefix
)
281 insnContext
= IC_64BIT_REXW_OPSIZE
;
282 else if (HasOpSizePrefix
)
283 insnContext
= IC_64BIT_OPSIZE
;
284 else if (HasREX_WPrefix
&& Prefix
== X86Local::XS
)
285 insnContext
= IC_64BIT_REXW_XS
;
286 else if (HasREX_WPrefix
&& Prefix
== X86Local::XD
)
287 insnContext
= IC_64BIT_REXW_XD
;
288 else if (Prefix
== X86Local::XD
)
289 insnContext
= IC_64BIT_XD
;
290 else if (Prefix
== X86Local::XS
)
291 insnContext
= IC_64BIT_XS
;
292 else if (HasREX_WPrefix
)
293 insnContext
= IC_64BIT_REXW
;
295 insnContext
= IC_64BIT
;
298 insnContext
= IC_OPSIZE
;
299 else if (Prefix
== X86Local::XD
)
301 else if (Prefix
== X86Local::XS
)
310 RecognizableInstr::filter_ret
RecognizableInstr::filter() const {
315 // Filter out intrinsics
317 if (!Rec
->isSubClassOf("X86Inst"))
318 return FILTER_STRONG
;
320 if (Form
== X86Local::Pseudo
||
322 return FILTER_STRONG
;
324 if (Form
== X86Local::MRMInitReg
)
325 return FILTER_STRONG
;
328 // TEMPORARY pending bug fixes
330 if (Name
.find("VMOVDQU") != Name
.npos
||
331 Name
.find("VMOVDQA") != Name
.npos
||
332 Name
.find("VROUND") != Name
.npos
)
333 return FILTER_STRONG
;
335 // Filter out artificial instructions
337 if (Name
.find("TAILJMP") != Name
.npos
||
338 Name
.find("_Int") != Name
.npos
||
339 Name
.find("_int") != Name
.npos
||
340 Name
.find("Int_") != Name
.npos
||
341 Name
.find("_NOREX") != Name
.npos
||
342 Name
.find("_TC") != Name
.npos
||
343 Name
.find("EH_RETURN") != Name
.npos
||
344 Name
.find("V_SET") != Name
.npos
||
345 Name
.find("LOCK_") != Name
.npos
||
346 Name
.find("WIN") != Name
.npos
||
347 Name
.find("_AVX") != Name
.npos
||
348 Name
.find("2SDL") != Name
.npos
)
349 return FILTER_STRONG
;
351 // Filter out instructions with segment override prefixes.
352 // They're too messy to handle now and we'll special case them if needed.
355 return FILTER_STRONG
;
357 // Filter out instructions that can't be printed.
359 if (AsmString
.size() == 0)
360 return FILTER_STRONG
;
362 // Filter out instructions with subreg operands.
364 if (AsmString
.find("subreg") != AsmString
.npos
)
365 return FILTER_STRONG
;
372 // Filter out instructions with a LOCK prefix;
373 // prefer forms that do not have the prefix
377 // Filter out alternate forms of AVX instructions
378 if (Name
.find("_alt") != Name
.npos
||
379 Name
.find("XrYr") != Name
.npos
||
380 Name
.find("r64r") != Name
.npos
||
381 Name
.find("_64mr") != Name
.npos
||
382 Name
.find("Xrr") != Name
.npos
||
383 Name
.find("rr64") != Name
.npos
)
386 if (Name
== "VMASKMOVDQU64" ||
387 Name
== "VEXTRACTPSrr64" ||
388 Name
== "VMOVQd64rr" ||
389 Name
== "VMOVQs64rr")
394 if (Name
.find("PCMPISTRI") != Name
.npos
&& Name
!= "PCMPISTRI")
396 if (Name
.find("PCMPESTRI") != Name
.npos
&& Name
!= "PCMPESTRI")
399 if (Name
.find("MOV") != Name
.npos
&& Name
.find("r0") != Name
.npos
)
401 if (Name
.find("MOVZ") != Name
.npos
&& Name
.find("MOVZX") == Name
.npos
)
403 if (Name
.find("Fs") != Name
.npos
)
405 if (Name
== "MOVLPDrr" ||
406 Name
== "MOVLPSrr" ||
412 Name
== "MOVSX16rm8" ||
413 Name
== "MOVSX16rr8" ||
414 Name
== "MOVZX16rm8" ||
415 Name
== "MOVZX16rr8" ||
416 Name
== "PUSH32i16" ||
417 Name
== "PUSH64i16" ||
418 Name
== "MOVPQI2QImr" ||
419 Name
== "VMOVPQI2QImr" ||
424 Name
== "MMX_MOVD64rrv164" ||
425 Name
== "CRC32m16" ||
426 Name
== "MOV64ri64i32" ||
430 if (HasFROperands
&& Name
.find("MOV") != Name
.npos
&&
431 ((Name
.find("2") != Name
.npos
&& Name
.find("32") == Name
.npos
) ||
432 (Name
.find("to") != Name
.npos
)))
435 return FILTER_NORMAL
;
438 bool RecognizableInstr::hasFROperands() const {
439 const std::vector
<CGIOperandList::OperandInfo
> &OperandList
= *Operands
;
440 unsigned numOperands
= OperandList
.size();
442 for (unsigned operandIndex
= 0; operandIndex
< numOperands
; ++operandIndex
) {
443 const std::string
&recName
= OperandList
[operandIndex
].Rec
->getName();
445 if (recName
.find("FR") != recName
.npos
)
451 bool RecognizableInstr::has256BitOperands() const {
452 const std::vector
<CGIOperandList::OperandInfo
> &OperandList
= *Operands
;
453 unsigned numOperands
= OperandList
.size();
455 for (unsigned operandIndex
= 0; operandIndex
< numOperands
; ++operandIndex
) {
456 const std::string
&recName
= OperandList
[operandIndex
].Rec
->getName();
458 if (!recName
.compare("VR256") || !recName
.compare("f256mem")) {
465 void RecognizableInstr::handleOperand(
467 unsigned &operandIndex
,
468 unsigned &physicalOperandIndex
,
469 unsigned &numPhysicalOperands
,
470 unsigned *operandMapping
,
471 OperandEncoding (*encodingFromString
)(const std::string
&, bool hasOpSizePrefix
)) {
473 if (physicalOperandIndex
>= numPhysicalOperands
)
476 assert(physicalOperandIndex
< numPhysicalOperands
);
479 while (operandMapping
[operandIndex
] != operandIndex
) {
480 Spec
->operands
[operandIndex
].encoding
= ENCODING_DUP
;
481 Spec
->operands
[operandIndex
].type
=
482 (OperandType
)(TYPE_DUP0
+ operandMapping
[operandIndex
]);
486 const std::string
&typeName
= (*Operands
)[operandIndex
].Rec
->getName();
488 Spec
->operands
[operandIndex
].encoding
= encodingFromString(typeName
,
490 Spec
->operands
[operandIndex
].type
= typeFromString(typeName
,
496 ++physicalOperandIndex
;
499 void RecognizableInstr::emitInstructionSpecifier(DisassemblerTables
&tables
) {
502 if (!Rec
->isSubClassOf("X86Inst"))
507 Spec
->filtered
= true;
510 ShouldBeEmitted
= false;
516 Spec
->insnContext
= insnContext();
518 const std::vector
<CGIOperandList::OperandInfo
> &OperandList
= *Operands
;
520 unsigned operandIndex
;
521 unsigned numOperands
= OperandList
.size();
522 unsigned numPhysicalOperands
= 0;
524 // operandMapping maps from operands in OperandList to their originals.
525 // If operandMapping[i] != i, then the entry is a duplicate.
526 unsigned operandMapping
[X86_MAX_OPERANDS
];
528 bool hasFROperands
= false;
530 assert(numOperands
< X86_MAX_OPERANDS
&& "X86_MAX_OPERANDS is not large enough");
532 for (operandIndex
= 0; operandIndex
< numOperands
; ++operandIndex
) {
533 if (OperandList
[operandIndex
].Constraints
.size()) {
534 const CGIOperandList::ConstraintInfo
&Constraint
=
535 OperandList
[operandIndex
].Constraints
[0];
536 if (Constraint
.isTied()) {
537 operandMapping
[operandIndex
] = Constraint
.getTiedOperand();
539 ++numPhysicalOperands
;
540 operandMapping
[operandIndex
] = operandIndex
;
543 ++numPhysicalOperands
;
544 operandMapping
[operandIndex
] = operandIndex
;
547 const std::string
&recName
= OperandList
[operandIndex
].Rec
->getName();
549 if (recName
.find("FR") != recName
.npos
)
550 hasFROperands
= true;
553 if (hasFROperands
&& Name
.find("MOV") != Name
.npos
&&
554 ((Name
.find("2") != Name
.npos
&& Name
.find("32") == Name
.npos
) ||
555 (Name
.find("to") != Name
.npos
)))
556 ShouldBeEmitted
= false;
558 if (!ShouldBeEmitted
)
561 #define HANDLE_OPERAND(class) \
562 handleOperand(false, \
564 physicalOperandIndex, \
565 numPhysicalOperands, \
567 class##EncodingFromString);
569 #define HANDLE_OPTIONAL(class) \
570 handleOperand(true, \
572 physicalOperandIndex, \
573 numPhysicalOperands, \
575 class##EncodingFromString);
577 // operandIndex should always be < numOperands
579 // physicalOperandIndex should always be < numPhysicalOperands
580 unsigned physicalOperandIndex
= 0;
583 case X86Local::RawFrm
:
584 // Operand 1 (optional) is an address or immediate.
585 // Operand 2 (optional) is an immediate.
586 assert(numPhysicalOperands
<= 2 &&
587 "Unexpected number of operands for RawFrm");
588 HANDLE_OPTIONAL(relocation
)
589 HANDLE_OPTIONAL(immediate
)
591 case X86Local::AddRegFrm
:
592 // Operand 1 is added to the opcode.
593 // Operand 2 (optional) is an address.
594 assert(numPhysicalOperands
>= 1 && numPhysicalOperands
<= 2 &&
595 "Unexpected number of operands for AddRegFrm");
596 HANDLE_OPERAND(opcodeModifier
)
597 HANDLE_OPTIONAL(relocation
)
599 case X86Local::MRMDestReg
:
600 // Operand 1 is a register operand in the R/M field.
601 // Operand 2 is a register operand in the Reg/Opcode field.
602 // Operand 3 (optional) is an immediate.
603 assert(numPhysicalOperands
>= 2 && numPhysicalOperands
<= 3 &&
604 "Unexpected number of operands for MRMDestRegFrm");
605 HANDLE_OPERAND(rmRegister
)
606 HANDLE_OPERAND(roRegister
)
607 HANDLE_OPTIONAL(immediate
)
609 case X86Local::MRMDestMem
:
610 // Operand 1 is a memory operand (possibly SIB-extended)
611 // Operand 2 is a register operand in the Reg/Opcode field.
612 // Operand 3 (optional) is an immediate.
613 assert(numPhysicalOperands
>= 2 && numPhysicalOperands
<= 3 &&
614 "Unexpected number of operands for MRMDestMemFrm");
615 HANDLE_OPERAND(memory
)
616 HANDLE_OPERAND(roRegister
)
617 HANDLE_OPTIONAL(immediate
)
619 case X86Local::MRMSrcReg
:
620 // Operand 1 is a register operand in the Reg/Opcode field.
621 // Operand 2 is a register operand in the R/M field.
622 // - In AVX, there is a register operand in the VEX.vvvv field here -
623 // Operand 3 (optional) is an immediate.
626 assert(numPhysicalOperands
>= 3 && numPhysicalOperands
<= 4 &&
627 "Unexpected number of operands for MRMSrcRegFrm with VEX_4V");
629 assert(numPhysicalOperands
>= 2 && numPhysicalOperands
<= 3 &&
630 "Unexpected number of operands for MRMSrcRegFrm");
632 HANDLE_OPERAND(roRegister
)
635 // FIXME: In AVX, the register below becomes the one encoded
636 // in ModRMVEX and the one above the one in the VEX.VVVV field
637 HANDLE_OPERAND(vvvvRegister
)
639 HANDLE_OPERAND(rmRegister
)
640 HANDLE_OPTIONAL(immediate
)
642 case X86Local::MRMSrcMem
:
643 // Operand 1 is a register operand in the Reg/Opcode field.
644 // Operand 2 is a memory operand (possibly SIB-extended)
645 // - In AVX, there is a register operand in the VEX.vvvv field here -
646 // Operand 3 (optional) is an immediate.
649 assert(numPhysicalOperands
>= 3 && numPhysicalOperands
<= 4 &&
650 "Unexpected number of operands for MRMSrcMemFrm with VEX_4V");
652 assert(numPhysicalOperands
>= 2 && numPhysicalOperands
<= 3 &&
653 "Unexpected number of operands for MRMSrcMemFrm");
655 HANDLE_OPERAND(roRegister
)
658 // FIXME: In AVX, the register below becomes the one encoded
659 // in ModRMVEX and the one above the one in the VEX.VVVV field
660 HANDLE_OPERAND(vvvvRegister
)
662 HANDLE_OPERAND(memory
)
663 HANDLE_OPTIONAL(immediate
)
665 case X86Local::MRM0r
:
666 case X86Local::MRM1r
:
667 case X86Local::MRM2r
:
668 case X86Local::MRM3r
:
669 case X86Local::MRM4r
:
670 case X86Local::MRM5r
:
671 case X86Local::MRM6r
:
672 case X86Local::MRM7r
:
673 // Operand 1 is a register operand in the R/M field.
674 // Operand 2 (optional) is an immediate or relocation.
676 assert(numPhysicalOperands
<= 3 &&
677 "Unexpected number of operands for MRMSrcMemFrm with VEX_4V");
679 assert(numPhysicalOperands
<= 2 &&
680 "Unexpected number of operands for MRMnRFrm");
682 HANDLE_OPERAND(vvvvRegister
);
683 HANDLE_OPTIONAL(rmRegister
)
684 HANDLE_OPTIONAL(relocation
)
686 case X86Local::MRM0m
:
687 case X86Local::MRM1m
:
688 case X86Local::MRM2m
:
689 case X86Local::MRM3m
:
690 case X86Local::MRM4m
:
691 case X86Local::MRM5m
:
692 case X86Local::MRM6m
:
693 case X86Local::MRM7m
:
694 // Operand 1 is a memory operand (possibly SIB-extended)
695 // Operand 2 (optional) is an immediate or relocation.
696 assert(numPhysicalOperands
>= 1 && numPhysicalOperands
<= 2 &&
697 "Unexpected number of operands for MRMnMFrm");
698 HANDLE_OPERAND(memory
)
699 HANDLE_OPTIONAL(relocation
)
701 case X86Local::RawFrmImm8
:
702 // operand 1 is a 16-bit immediate
703 // operand 2 is an 8-bit immediate
704 assert(numPhysicalOperands
== 2 &&
705 "Unexpected number of operands for X86Local::RawFrmImm8");
706 HANDLE_OPERAND(immediate
)
707 HANDLE_OPERAND(immediate
)
709 case X86Local::RawFrmImm16
:
710 // operand 1 is a 16-bit immediate
711 // operand 2 is a 16-bit immediate
712 HANDLE_OPERAND(immediate
)
713 HANDLE_OPERAND(immediate
)
715 case X86Local::MRMInitReg
:
720 #undef HANDLE_OPERAND
721 #undef HANDLE_OPTIONAL
724 void RecognizableInstr::emitDecodePath(DisassemblerTables
&tables
) const {
725 // Special cases where the LLVM tables are not complete
727 #define MAP(from, to) \
728 case X86Local::MRM_##from: \
729 filter = new ExactFilter(0x##from); \
732 OpcodeType opcodeType
= (OpcodeType
)-1;
734 ModRMFilter
* filter
= NULL
;
735 uint8_t opcodeToSet
= 0;
738 // Extended two-byte opcodes can start with f2 0f, f3 0f, or 0f
742 opcodeType
= TWOBYTE
;
746 if (needsModRMForDecode(Form
))
747 filter
= new ModFilter(isRegFormat(Form
));
749 filter
= new DumbFilter();
751 #define EXTENSION_TABLE(n) case 0x##n:
752 TWO_BYTE_EXTENSION_TABLES
753 #undef EXTENSION_TABLE
756 llvm_unreachable("Unhandled two-byte extended opcode");
757 case X86Local::MRM0r
:
758 case X86Local::MRM1r
:
759 case X86Local::MRM2r
:
760 case X86Local::MRM3r
:
761 case X86Local::MRM4r
:
762 case X86Local::MRM5r
:
763 case X86Local::MRM6r
:
764 case X86Local::MRM7r
:
765 filter
= new ExtendedFilter(true, Form
- X86Local::MRM0r
);
767 case X86Local::MRM0m
:
768 case X86Local::MRM1m
:
769 case X86Local::MRM2m
:
770 case X86Local::MRM3m
:
771 case X86Local::MRM4m
:
772 case X86Local::MRM5m
:
773 case X86Local::MRM6m
:
774 case X86Local::MRM7m
:
775 filter
= new ExtendedFilter(false, Form
- X86Local::MRM0m
);
781 opcodeToSet
= Opcode
;
784 opcodeType
= THREEBYTE_38
;
785 if (needsModRMForDecode(Form
))
786 filter
= new ModFilter(isRegFormat(Form
));
788 filter
= new DumbFilter();
789 opcodeToSet
= Opcode
;
792 opcodeType
= THREEBYTE_3A
;
793 if (needsModRMForDecode(Form
))
794 filter
= new ModFilter(isRegFormat(Form
));
796 filter
= new DumbFilter();
797 opcodeToSet
= Opcode
;
800 opcodeType
= THREEBYTE_A6
;
801 if (needsModRMForDecode(Form
))
802 filter
= new ModFilter(isRegFormat(Form
));
804 filter
= new DumbFilter();
805 opcodeToSet
= Opcode
;
808 opcodeType
= THREEBYTE_A7
;
809 if (needsModRMForDecode(Form
))
810 filter
= new ModFilter(isRegFormat(Form
));
812 filter
= new DumbFilter();
813 opcodeToSet
= Opcode
;
823 assert(Opcode
>= 0xc0 && "Unexpected opcode for an escape opcode");
824 opcodeType
= ONEBYTE
;
825 if (Form
== X86Local::AddRegFrm
) {
826 Spec
->modifierType
= MODIFIER_MODRM
;
827 Spec
->modifierBase
= Opcode
;
828 filter
= new AddRegEscapeFilter(Opcode
);
830 filter
= new EscapeFilter(true, Opcode
);
832 opcodeToSet
= 0xd8 + (Prefix
- X86Local::D8
);
835 opcodeType
= ONEBYTE
;
837 #define EXTENSION_TABLE(n) case 0x##n:
838 ONE_BYTE_EXTENSION_TABLES
839 #undef EXTENSION_TABLE
842 llvm_unreachable("Fell through the cracks of a single-byte "
844 case X86Local::MRM0r
:
845 case X86Local::MRM1r
:
846 case X86Local::MRM2r
:
847 case X86Local::MRM3r
:
848 case X86Local::MRM4r
:
849 case X86Local::MRM5r
:
850 case X86Local::MRM6r
:
851 case X86Local::MRM7r
:
852 filter
= new ExtendedFilter(true, Form
- X86Local::MRM0r
);
854 case X86Local::MRM0m
:
855 case X86Local::MRM1m
:
856 case X86Local::MRM2m
:
857 case X86Local::MRM3m
:
858 case X86Local::MRM4m
:
859 case X86Local::MRM5m
:
860 case X86Local::MRM6m
:
861 case X86Local::MRM7m
:
862 filter
= new ExtendedFilter(false, Form
- X86Local::MRM0m
);
875 filter
= new EscapeFilter(false, Form
- X86Local::MRM0m
);
878 if (needsModRMForDecode(Form
))
879 filter
= new ModFilter(isRegFormat(Form
));
881 filter
= new DumbFilter();
884 opcodeToSet
= Opcode
;
887 assert(opcodeType
!= (OpcodeType
)-1 &&
888 "Opcode type not set");
889 assert(filter
&& "Filter not set");
891 if (Form
== X86Local::AddRegFrm
) {
892 if(Spec
->modifierType
!= MODIFIER_MODRM
) {
893 assert(opcodeToSet
< 0xf9 &&
894 "Not enough room for all ADDREG_FRM operands");
896 uint8_t currentOpcode
;
898 for (currentOpcode
= opcodeToSet
;
899 currentOpcode
< opcodeToSet
+ 8;
901 tables
.setTableFields(opcodeType
,
907 Spec
->modifierType
= MODIFIER_OPCODE
;
908 Spec
->modifierBase
= opcodeToSet
;
910 // modifierBase was set where MODIFIER_MODRM was set
911 tables
.setTableFields(opcodeType
,
918 tables
.setTableFields(opcodeType
,
924 Spec
->modifierType
= MODIFIER_NONE
;
925 Spec
->modifierBase
= opcodeToSet
;
933 #define TYPE(str, type) if (s == str) return type;
934 OperandType
RecognizableInstr::typeFromString(const std::string
&s
,
937 bool hasOpSizePrefix
) {
939 // For SSE instructions, we ignore the OpSize prefix and force operand
941 TYPE("GR16", TYPE_R16
)
942 TYPE("GR32", TYPE_R32
)
943 TYPE("GR64", TYPE_R64
)
946 // For instructions with a REX_W prefix, a declared 32-bit register encoding
948 TYPE("GR32", TYPE_R32
)
950 if(!hasOpSizePrefix
) {
951 // For instructions without an OpSize prefix, a declared 16-bit register or
952 // immediate encoding is special.
953 TYPE("GR16", TYPE_R16
)
954 TYPE("i16imm", TYPE_IMM16
)
956 TYPE("i16mem", TYPE_Mv
)
957 TYPE("i16imm", TYPE_IMMv
)
958 TYPE("i16i8imm", TYPE_IMMv
)
959 TYPE("GR16", TYPE_Rv
)
960 TYPE("i32mem", TYPE_Mv
)
961 TYPE("i32imm", TYPE_IMMv
)
962 TYPE("i32i8imm", TYPE_IMM32
)
963 TYPE("GR32", TYPE_Rv
)
964 TYPE("i64mem", TYPE_Mv
)
965 TYPE("i64i32imm", TYPE_IMM64
)
966 TYPE("i64i8imm", TYPE_IMM64
)
967 TYPE("GR64", TYPE_R64
)
968 TYPE("i8mem", TYPE_M8
)
969 TYPE("i8imm", TYPE_IMM8
)
971 TYPE("VR128", TYPE_XMM128
)
972 TYPE("f128mem", TYPE_M128
)
973 TYPE("f256mem", TYPE_M256
)
974 TYPE("FR64", TYPE_XMM64
)
975 TYPE("f64mem", TYPE_M64FP
)
976 TYPE("sdmem", TYPE_M64FP
)
977 TYPE("FR32", TYPE_XMM32
)
978 TYPE("f32mem", TYPE_M32FP
)
979 TYPE("ssmem", TYPE_M32FP
)
981 TYPE("i128mem", TYPE_M128
)
982 TYPE("i256mem", TYPE_M256
)
983 TYPE("i64i32imm_pcrel", TYPE_REL64
)
984 TYPE("i16imm_pcrel", TYPE_REL16
)
985 TYPE("i32imm_pcrel", TYPE_REL32
)
986 TYPE("SSECC", TYPE_IMM3
)
987 TYPE("brtarget", TYPE_RELv
)
988 TYPE("uncondbrtarget", TYPE_RELv
)
989 TYPE("brtarget8", TYPE_REL8
)
990 TYPE("f80mem", TYPE_M80FP
)
991 TYPE("lea32mem", TYPE_LEA
)
992 TYPE("lea64_32mem", TYPE_LEA
)
993 TYPE("lea64mem", TYPE_LEA
)
994 TYPE("VR64", TYPE_MM64
)
995 TYPE("i64imm", TYPE_IMMv
)
996 TYPE("opaque32mem", TYPE_M1616
)
997 TYPE("opaque48mem", TYPE_M1632
)
998 TYPE("opaque80mem", TYPE_M1664
)
999 TYPE("opaque512mem", TYPE_M512
)
1000 TYPE("SEGMENT_REG", TYPE_SEGMENTREG
)
1001 TYPE("DEBUG_REG", TYPE_DEBUGREG
)
1002 TYPE("CONTROL_REG", TYPE_CONTROLREG
)
1003 TYPE("offset8", TYPE_MOFFS8
)
1004 TYPE("offset16", TYPE_MOFFS16
)
1005 TYPE("offset32", TYPE_MOFFS32
)
1006 TYPE("offset64", TYPE_MOFFS64
)
1007 TYPE("VR256", TYPE_XMM256
)
1008 errs() << "Unhandled type string " << s
<< "\n";
1009 llvm_unreachable("Unhandled type string");
1013 #define ENCODING(str, encoding) if (s == str) return encoding;
1014 OperandEncoding
RecognizableInstr::immediateEncodingFromString
1015 (const std::string
&s
,
1016 bool hasOpSizePrefix
) {
1017 if(!hasOpSizePrefix
) {
1018 // For instructions without an OpSize prefix, a declared 16-bit register or
1019 // immediate encoding is special.
1020 ENCODING("i16imm", ENCODING_IW
)
1022 ENCODING("i32i8imm", ENCODING_IB
)
1023 ENCODING("SSECC", ENCODING_IB
)
1024 ENCODING("i16imm", ENCODING_Iv
)
1025 ENCODING("i16i8imm", ENCODING_IB
)
1026 ENCODING("i32imm", ENCODING_Iv
)
1027 ENCODING("i64i32imm", ENCODING_ID
)
1028 ENCODING("i64i8imm", ENCODING_IB
)
1029 ENCODING("i8imm", ENCODING_IB
)
1030 // This is not a typo. Instructions like BLENDVPD put
1031 // register IDs in 8-bit immediates nowadays.
1032 ENCODING("VR256", ENCODING_IB
)
1033 ENCODING("VR128", ENCODING_IB
)
1034 errs() << "Unhandled immediate encoding " << s
<< "\n";
1035 llvm_unreachable("Unhandled immediate encoding");
1038 OperandEncoding
RecognizableInstr::rmRegisterEncodingFromString
1039 (const std::string
&s
,
1040 bool hasOpSizePrefix
) {
1041 ENCODING("GR16", ENCODING_RM
)
1042 ENCODING("GR32", ENCODING_RM
)
1043 ENCODING("GR64", ENCODING_RM
)
1044 ENCODING("GR8", ENCODING_RM
)
1045 ENCODING("VR128", ENCODING_RM
)
1046 ENCODING("FR64", ENCODING_RM
)
1047 ENCODING("FR32", ENCODING_RM
)
1048 ENCODING("VR64", ENCODING_RM
)
1049 ENCODING("VR256", ENCODING_RM
)
1050 errs() << "Unhandled R/M register encoding " << s
<< "\n";
1051 llvm_unreachable("Unhandled R/M register encoding");
1054 OperandEncoding
RecognizableInstr::roRegisterEncodingFromString
1055 (const std::string
&s
,
1056 bool hasOpSizePrefix
) {
1057 ENCODING("GR16", ENCODING_REG
)
1058 ENCODING("GR32", ENCODING_REG
)
1059 ENCODING("GR64", ENCODING_REG
)
1060 ENCODING("GR8", ENCODING_REG
)
1061 ENCODING("VR128", ENCODING_REG
)
1062 ENCODING("FR64", ENCODING_REG
)
1063 ENCODING("FR32", ENCODING_REG
)
1064 ENCODING("VR64", ENCODING_REG
)
1065 ENCODING("SEGMENT_REG", ENCODING_REG
)
1066 ENCODING("DEBUG_REG", ENCODING_REG
)
1067 ENCODING("CONTROL_REG", ENCODING_REG
)
1068 ENCODING("VR256", ENCODING_REG
)
1069 errs() << "Unhandled reg/opcode register encoding " << s
<< "\n";
1070 llvm_unreachable("Unhandled reg/opcode register encoding");
1073 OperandEncoding
RecognizableInstr::vvvvRegisterEncodingFromString
1074 (const std::string
&s
,
1075 bool hasOpSizePrefix
) {
1076 ENCODING("FR32", ENCODING_VVVV
)
1077 ENCODING("FR64", ENCODING_VVVV
)
1078 ENCODING("VR128", ENCODING_VVVV
)
1079 ENCODING("VR256", ENCODING_VVVV
)
1080 errs() << "Unhandled VEX.vvvv register encoding " << s
<< "\n";
1081 llvm_unreachable("Unhandled VEX.vvvv register encoding");
1084 OperandEncoding
RecognizableInstr::memoryEncodingFromString
1085 (const std::string
&s
,
1086 bool hasOpSizePrefix
) {
1087 ENCODING("i16mem", ENCODING_RM
)
1088 ENCODING("i32mem", ENCODING_RM
)
1089 ENCODING("i64mem", ENCODING_RM
)
1090 ENCODING("i8mem", ENCODING_RM
)
1091 ENCODING("ssmem", ENCODING_RM
)
1092 ENCODING("sdmem", ENCODING_RM
)
1093 ENCODING("f128mem", ENCODING_RM
)
1094 ENCODING("f256mem", ENCODING_RM
)
1095 ENCODING("f64mem", ENCODING_RM
)
1096 ENCODING("f32mem", ENCODING_RM
)
1097 ENCODING("i128mem", ENCODING_RM
)
1098 ENCODING("i256mem", ENCODING_RM
)
1099 ENCODING("f80mem", ENCODING_RM
)
1100 ENCODING("lea32mem", ENCODING_RM
)
1101 ENCODING("lea64_32mem", ENCODING_RM
)
1102 ENCODING("lea64mem", ENCODING_RM
)
1103 ENCODING("opaque32mem", ENCODING_RM
)
1104 ENCODING("opaque48mem", ENCODING_RM
)
1105 ENCODING("opaque80mem", ENCODING_RM
)
1106 ENCODING("opaque512mem", ENCODING_RM
)
1107 errs() << "Unhandled memory encoding " << s
<< "\n";
1108 llvm_unreachable("Unhandled memory encoding");
1111 OperandEncoding
RecognizableInstr::relocationEncodingFromString
1112 (const std::string
&s
,
1113 bool hasOpSizePrefix
) {
1114 if(!hasOpSizePrefix
) {
1115 // For instructions without an OpSize prefix, a declared 16-bit register or
1116 // immediate encoding is special.
1117 ENCODING("i16imm", ENCODING_IW
)
1119 ENCODING("i16imm", ENCODING_Iv
)
1120 ENCODING("i16i8imm", ENCODING_IB
)
1121 ENCODING("i32imm", ENCODING_Iv
)
1122 ENCODING("i32i8imm", ENCODING_IB
)
1123 ENCODING("i64i32imm", ENCODING_ID
)
1124 ENCODING("i64i8imm", ENCODING_IB
)
1125 ENCODING("i8imm", ENCODING_IB
)
1126 ENCODING("i64i32imm_pcrel", ENCODING_ID
)
1127 ENCODING("i16imm_pcrel", ENCODING_IW
)
1128 ENCODING("i32imm_pcrel", ENCODING_ID
)
1129 ENCODING("brtarget", ENCODING_Iv
)
1130 ENCODING("brtarget8", ENCODING_IB
)
1131 ENCODING("i64imm", ENCODING_IO
)
1132 ENCODING("offset8", ENCODING_Ia
)
1133 ENCODING("offset16", ENCODING_Ia
)
1134 ENCODING("offset32", ENCODING_Ia
)
1135 ENCODING("offset64", ENCODING_Ia
)
1136 errs() << "Unhandled relocation encoding " << s
<< "\n";
1137 llvm_unreachable("Unhandled relocation encoding");
1140 OperandEncoding
RecognizableInstr::opcodeModifierEncodingFromString
1141 (const std::string
&s
,
1142 bool hasOpSizePrefix
) {
1143 ENCODING("RST", ENCODING_I
)
1144 ENCODING("GR32", ENCODING_Rv
)
1145 ENCODING("GR64", ENCODING_RO
)
1146 ENCODING("GR16", ENCODING_Rv
)
1147 ENCODING("GR8", ENCODING_RB
)
1148 errs() << "Unhandled opcode modifier encoding " << s
<< "\n";
1149 llvm_unreachable("Unhandled opcode modifier encoding");