Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/X86RecognizableInstr.h
213766 views
//===- X86RecognizableInstr.h - Disassembler instruction spec ---*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is part of the X86 Disassembler Emitter.9// It contains the interface of a single recognizable instruction.10// Documentation for the disassembler emitter in general can be found in11// X86DisassemblerEmitter.h.12//13//===----------------------------------------------------------------------===//1415#ifndef LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H16#define LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H1718#include "Common/CodeGenInstruction.h"19#include "llvm/Support/X86DisassemblerDecoderCommon.h"20#include <cstdint>21#include <string>22#include <vector>2324struct InstructionSpecifier;2526namespace llvm {27class Record;28#define X86_INSTR_MRM_MAPPING \29MAP(C0, 64) \30MAP(C1, 65) \31MAP(C2, 66) \32MAP(C3, 67) \33MAP(C4, 68) \34MAP(C5, 69) \35MAP(C6, 70) \36MAP(C7, 71) \37MAP(C8, 72) \38MAP(C9, 73) \39MAP(CA, 74) \40MAP(CB, 75) \41MAP(CC, 76) \42MAP(CD, 77) \43MAP(CE, 78) \44MAP(CF, 79) \45MAP(D0, 80) \46MAP(D1, 81) \47MAP(D2, 82) \48MAP(D3, 83) \49MAP(D4, 84) \50MAP(D5, 85) \51MAP(D6, 86) \52MAP(D7, 87) \53MAP(D8, 88) \54MAP(D9, 89) \55MAP(DA, 90) \56MAP(DB, 91) \57MAP(DC, 92) \58MAP(DD, 93) \59MAP(DE, 94) \60MAP(DF, 95) \61MAP(E0, 96) \62MAP(E1, 97) \63MAP(E2, 98) \64MAP(E3, 99) \65MAP(E4, 100) \66MAP(E5, 101) \67MAP(E6, 102) \68MAP(E7, 103) \69MAP(E8, 104) \70MAP(E9, 105) \71MAP(EA, 106) \72MAP(EB, 107) \73MAP(EC, 108) \74MAP(ED, 109) \75MAP(EE, 110) \76MAP(EF, 111) \77MAP(F0, 112) \78MAP(F1, 113) \79MAP(F2, 114) \80MAP(F3, 115) \81MAP(F4, 116) \82MAP(F5, 117) \83MAP(F6, 118) \84MAP(F7, 119) \85MAP(F8, 120) \86MAP(F9, 121) \87MAP(FA, 122) \88MAP(FB, 123) \89MAP(FC, 124) \90MAP(FD, 125) \91MAP(FE, 126) \92MAP(FF, 127)9394// A clone of X86 since we can't depend on something that is generated.95namespace X86Local {96enum {97Pseudo = 0,98RawFrm = 1,99AddRegFrm = 2,100RawFrmMemOffs = 3,101RawFrmSrc = 4,102RawFrmDst = 5,103RawFrmDstSrc = 6,104RawFrmImm8 = 7,105RawFrmImm16 = 8,106AddCCFrm = 9,107PrefixByte = 10,108MRMDestRegCC = 18,109MRMDestMemCC = 19,110MRMDestMem4VOp3CC = 20,111MRMr0 = 21,112MRMSrcMemFSIB = 22,113MRMDestMemFSIB = 23,114MRMDestMem = 24,115MRMSrcMem = 25,116MRMSrcMem4VOp3 = 26,117MRMSrcMemOp4 = 27,118MRMSrcMemCC = 28,119MRMXmCC = 30,120MRMXm = 31,121MRM0m = 32,122MRM1m = 33,123MRM2m = 34,124MRM3m = 35,125MRM4m = 36,126MRM5m = 37,127MRM6m = 38,128MRM7m = 39,129MRMDestReg = 40,130MRMSrcReg = 41,131MRMSrcReg4VOp3 = 42,132MRMSrcRegOp4 = 43,133MRMSrcRegCC = 44,134MRMXrCC = 46,135MRMXr = 47,136MRM0r = 48,137MRM1r = 49,138MRM2r = 50,139MRM3r = 51,140MRM4r = 52,141MRM5r = 53,142MRM6r = 54,143MRM7r = 55,144MRM0X = 56,145MRM1X = 57,146MRM2X = 58,147MRM3X = 59,148MRM4X = 60,149MRM5X = 61,150MRM6X = 62,151MRM7X = 63,152#define MAP(from, to) MRM_##from = to,153X86_INSTR_MRM_MAPPING154#undef MAP155};156157enum {158OB = 0,159TB = 1,160T8 = 2,161TA = 3,162XOP8 = 4,163XOP9 = 5,164XOPA = 6,165ThreeDNow = 7,166T_MAP4 = 8,167T_MAP5 = 9,168T_MAP6 = 10,169T_MAP7 = 11170};171172enum { PD = 1, XS = 2, XD = 3, PS = 4 };173enum { VEX = 1, XOP = 2, EVEX = 3 };174enum { OpSize16 = 1, OpSize32 = 2 };175enum { AdSize16 = 1, AdSize32 = 2, AdSize64 = 3 };176enum { ExplicitREX2 = 1, ExplicitEVEX = 3 };177} // namespace X86Local178179namespace X86Disassembler {180class DisassemblerTables;181/// Extract common fields of a single X86 instruction from a CodeGenInstruction182struct RecognizableInstrBase {183/// The OpPrefix field from the record184uint8_t OpPrefix;185/// The OpMap field from the record186uint8_t OpMap;187/// The opcode field from the record; this is the opcode used in the Intel188/// encoding and therefore distinct from the UID189uint8_t Opcode;190/// The form field from the record191uint8_t Form;192// The encoding field from the record193uint8_t Encoding;194/// The OpSize field from the record195uint8_t OpSize;196/// The AdSize field from the record197uint8_t AdSize;198/// The hasREX_W field from the record199bool HasREX_W;200/// The hasVEX_4V field from the record201bool HasVEX_4V;202/// The IgnoresW field from the record203bool IgnoresW;204/// The hasVEX_L field from the record205bool HasVEX_L;206/// The ignoreVEX_L field from the record207bool IgnoresVEX_L;208/// The hasEVEX_L2Prefix field from the record209bool HasEVEX_L2;210/// The hasEVEX_K field from the record211bool HasEVEX_K;212/// The hasEVEX_KZ field from the record213bool HasEVEX_KZ;214/// The hasEVEX_B field from the record215bool HasEVEX_B;216/// The hasEVEX_U field from the record217bool HasEVEX_U;218/// The hasEVEX_NF field from the record219bool HasEVEX_NF;220/// The hasTwoConditionalOps field from the record221bool HasTwoConditionalOps;222/// Indicates that the instruction uses the L and L' fields for RC.223bool EncodeRC;224/// The isCodeGenOnly field from the record225bool IsCodeGenOnly;226/// The isAsmParserOnly field from the record227bool IsAsmParserOnly;228/// The ForceDisassemble field from the record229bool ForceDisassemble;230// The CD8_Scale field from the record231uint8_t CD8_Scale;232/// If explicitOpPrefix field from the record equals ExplicitREX2233bool ExplicitREX2Prefix;234/// \param insn The CodeGenInstruction to extract information from.235RecognizableInstrBase(const CodeGenInstruction &insn);236/// \returns true if this instruction should be emitted237bool shouldBeEmitted() const;238};239240/// RecognizableInstr - Encapsulates all information required to decode a single241/// instruction, as extracted from the LLVM instruction tables. Has methods242/// to interpret the information available in the LLVM tables, and to emit the243/// instruction into DisassemblerTables.244class RecognizableInstr : public RecognizableInstrBase {245private:246/// The record from the .td files corresponding to this instruction247const Record *Rec;248/// The instruction name as listed in the tables249std::string Name;250// Whether the instruction has the predicate "In32BitMode"251bool Is32Bit;252// Whether the instruction has the predicate "In64BitMode"253bool Is64Bit;254/// The operands of the instruction, as listed in the CodeGenInstruction.255/// They are not one-to-one with operands listed in the MCInst; for example,256/// memory operands expand to 5 operands in the MCInst257const std::vector<CGIOperandList::OperandInfo> *Operands;258259/// The opcode of the instruction, as used in an MCInst260InstrUID UID;261/// The description of the instruction that is emitted into the instruction262/// info table263InstructionSpecifier *Spec;264265/// insnContext - Returns the primary context in which the instruction is266/// valid.267///268/// @return - The context in which the instruction is valid.269InstructionContext insnContext() const;270271/// typeFromString - Translates an operand type from the string provided in272/// the LLVM tables to an OperandType for use in the operand specifier.273///274/// @param s - The string, as extracted by calling Rec->getName()275/// on a CodeGenInstruction::OperandInfo.276/// @param hasREX_W - Indicates whether the instruction has a REX.W277/// prefix. If it does, 32-bit register operands stay278/// 32-bit regardless of the operand size.279/// @param OpSize Indicates the operand size of the instruction.280/// If register size does not match OpSize, then281/// register sizes keep their size.282/// @return - The operand's type.283static OperandType typeFromString(StringRef Str, bool hasREX_W,284uint8_t OpSize);285286/// immediateEncodingFromString - Translates an immediate encoding from the287/// string provided in the LLVM tables to an OperandEncoding for use in288/// the operand specifier.289///290/// @param s - See typeFromString().291/// @param OpSize - Indicates whether this is an OpSize16 instruction.292/// If it is not, then 16-bit immediate operands stay 16-bit.293/// @return - The operand's encoding.294static OperandEncoding immediateEncodingFromString(StringRef Str,295uint8_t OpSize);296297/// rmRegisterEncodingFromString - Like immediateEncodingFromString, but298/// handles operands that are in the REG field of the ModR/M byte.299static OperandEncoding rmRegisterEncodingFromString(StringRef Str,300uint8_t OpSize);301302/// rmRegisterEncodingFromString - Like immediateEncodingFromString, but303/// handles operands that are in the REG field of the ModR/M byte.304static OperandEncoding roRegisterEncodingFromString(StringRef Str,305uint8_t OpSize);306static OperandEncoding memoryEncodingFromString(StringRef Str,307uint8_t OpSize);308static OperandEncoding relocationEncodingFromString(StringRef Str,309uint8_t OpSize);310static OperandEncoding opcodeModifierEncodingFromString(StringRef Str,311uint8_t OpSize);312static OperandEncoding vvvvRegisterEncodingFromString(StringRef Str,313uint8_t OpSize);314static OperandEncoding writemaskRegisterEncodingFromString(StringRef Str,315uint8_t OpSize);316317/// Adjust the encoding type for an operand based on the instruction.318void adjustOperandEncoding(OperandEncoding &encoding);319320/// handleOperand - Converts a single operand from the LLVM table format to321/// the emitted table format, handling any duplicate operands it encounters322/// and then one non-duplicate.323///324/// @param optional - Determines whether to assert that the325/// operand exists.326/// @param operandIndex - The index into the generated operand table.327/// Incremented by this function one or more328/// times to reflect possible duplicate329/// operands).330/// @param physicalOperandIndex - The index of the current operand into the331/// set of non-duplicate ('physical') operands.332/// Incremented by this function once.333/// @param numPhysicalOperands - The number of non-duplicate operands in the334/// instructions.335/// @param operandMapping - The operand mapping, which has an entry for336/// each operand that indicates whether it is a337/// duplicate, and of what.338using EncodingFn =339llvm::function_ref<OperandEncoding(StringRef s, uint8_t OpSize)>;340void handleOperand(bool optional, unsigned &operandIndex,341unsigned &physicalOperandIndex,342unsigned numPhysicalOperands,343const unsigned *operandMapping,344EncodingFn encodingFromString);345346/// emitInstructionSpecifier - Loads the instruction specifier for the current347/// instruction into a DisassemblerTables.348///349void emitInstructionSpecifier();350351/// emitDecodePath - Populates the proper fields in the decode tables352/// corresponding to the decode paths for this instruction.353///354/// \param tables The DisassemblerTables to populate with the decode355/// decode information for the current instruction.356void emitDecodePath(DisassemblerTables &tables) const;357358public:359/// Constructor - Initializes a RecognizableInstr with the appropriate fields360/// from a CodeGenInstruction.361///362/// \param tables The DisassemblerTables that the specifier will be added to.363/// \param insn The CodeGenInstruction to extract information from.364/// \param uid The unique ID of the current instruction.365RecognizableInstr(DisassemblerTables &tables, const CodeGenInstruction &insn,366InstrUID uid);367/// processInstr - Accepts a CodeGenInstruction and loads decode information368/// for it into a DisassemblerTables if appropriate.369///370/// \param tables The DiassemblerTables to be populated with decode371/// information.372/// \param insn The CodeGenInstruction to be used as a source for this373/// information.374/// \param uid The unique ID of the instruction.375static void processInstr(DisassemblerTables &tables,376const CodeGenInstruction &insn, InstrUID uid);377};378379std::string getMnemonic(const CodeGenInstruction *I, unsigned Variant);380bool isRegisterOperand(const Record *Rec);381bool isMemoryOperand(const Record *Rec);382bool isImmediateOperand(const Record *Rec);383unsigned getRegOperandSize(const Record *RegRec);384unsigned getMemOperandSize(const Record *MemRec);385} // namespace X86Disassembler386} // namespace llvm387#endif388389390