1 //===- X86RecognizableInstr.h - 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 interface of a single recognizable instruction.
12 // Documentation for the disassembler emitter in general can be found in
13 // X86DisasemblerEmitter.h.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
18 #define LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
20 #include "CodeGenTarget.h"
21 #include "X86DisassemblerTables.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/TableGen/Record.h"
28 namespace X86Disassembler {
30 /// RecognizableInstr - Encapsulates all information required to decode a single
31 /// instruction, as extracted from the LLVM instruction tables. Has methods
32 /// to interpret the information available in the LLVM tables, and to emit the
33 /// instruction into DisassemblerTables.
34 class RecognizableInstr {
36 /// The opcode of the instruction, as used in an MCInst
38 /// The record from the .td files corresponding to this instruction
40 /// The OpPrefix field from the record
42 /// The OpMap field from the record
44 /// The opcode field from the record; this is the opcode used in the Intel
45 /// encoding and therefore distinct from the UID
47 /// The form field from the record
49 // The encoding field from the record
51 /// The OpSize field from the record
53 /// The AdSize field from the record
55 /// The hasREX_WPrefix field from the record
57 /// The hasVEX_4V field from the record
59 /// The hasVEX_4VOp3 field from the record
61 /// The hasVEX_WPrefix field from the record
63 /// Inferred from the operands; indicates whether the L bit in the VEX prefix is set
65 /// The hasMemOp4Prefix field from the record
67 /// The ignoreVEX_L field from the record
69 /// The hasEVEX_L2Prefix field from the record
70 bool HasEVEX_L2Prefix;
71 /// The hasEVEX_K field from the record
73 /// The hasEVEX_KZ field from the record
75 /// The hasEVEX_B field from the record
77 /// The isCodeGenOnly field from the record
79 /// The ForceDisassemble field from the record
80 bool ForceDisassemble;
81 // The CD8_Scale field from the record
83 // Whether the instruction has the predicate "In64BitMode"
85 // Whether the instruction has the predicate "In32BitMode"
88 /// The instruction name as listed in the tables
90 /// The AT&T AsmString for the instruction
91 std::string AsmString;
93 /// Indicates whether the instruction should be emitted into the decode
94 /// tables; regardless, it will be emitted into the instruction info table
97 /// The operands of the instruction, as listed in the CodeGenInstruction.
98 /// They are not one-to-one with operands listed in the MCInst; for example,
99 /// memory operands expand to 5 operands in the MCInst
100 const std::vector<CGIOperandList::OperandInfo>* Operands;
102 /// The description of the instruction that is emitted into the instruction
104 InstructionSpecifier* Spec;
106 /// insnContext - Returns the primary context in which the instruction is
109 /// @return - The context in which the instruction is valid.
110 InstructionContext insnContext() const;
112 /// typeFromString - Translates an operand type from the string provided in
113 /// the LLVM tables to an OperandType for use in the operand specifier.
115 /// @param s - The string, as extracted by calling Rec->getName()
116 /// on a CodeGenInstruction::OperandInfo.
117 /// @param hasREX_WPrefix - Indicates whether the instruction has a REX.W
118 /// prefix. If it does, 32-bit register operands stay
119 /// 32-bit regardless of the operand size.
120 /// @param OpSize Indicates the operand size of the instruction.
121 /// If register size does not match OpSize, then
122 /// register sizes keep their size.
123 /// @return - The operand's type.
124 static OperandType typeFromString(const std::string& s,
125 bool hasREX_WPrefix, uint8_t OpSize);
127 /// immediateEncodingFromString - Translates an immediate encoding from the
128 /// string provided in the LLVM tables to an OperandEncoding for use in
129 /// the operand specifier.
131 /// @param s - See typeFromString().
132 /// @param OpSize - Indicates whether this is an OpSize16 instruction.
133 /// If it is not, then 16-bit immediate operands stay 16-bit.
134 /// @return - The operand's encoding.
135 static OperandEncoding immediateEncodingFromString(const std::string &s,
138 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
139 /// handles operands that are in the REG field of the ModR/M byte.
140 static OperandEncoding rmRegisterEncodingFromString(const std::string &s,
143 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
144 /// handles operands that are in the REG field of the ModR/M byte.
145 static OperandEncoding roRegisterEncodingFromString(const std::string &s,
147 static OperandEncoding memoryEncodingFromString(const std::string &s,
149 static OperandEncoding relocationEncodingFromString(const std::string &s,
151 static OperandEncoding opcodeModifierEncodingFromString(const std::string &s,
153 static OperandEncoding vvvvRegisterEncodingFromString(const std::string &s,
155 static OperandEncoding writemaskRegisterEncodingFromString(const std::string &s,
158 /// \brief Adjust the encoding type for an operand based on the instruction.
159 void adjustOperandEncoding(OperandEncoding &encoding);
161 /// handleOperand - Converts a single operand from the LLVM table format to
162 /// the emitted table format, handling any duplicate operands it encounters
163 /// and then one non-duplicate.
165 /// @param optional - Determines whether to assert that the
167 /// @param operandIndex - The index into the generated operand table.
168 /// Incremented by this function one or more
169 /// times to reflect possible duplicate
171 /// @param physicalOperandIndex - The index of the current operand into the
172 /// set of non-duplicate ('physical') operands.
173 /// Incremented by this function once.
174 /// @param numPhysicalOperands - The number of non-duplicate operands in the
176 /// @param operandMapping - The operand mapping, which has an entry for
177 /// each operand that indicates whether it is a
178 /// duplicate, and of what.
179 void handleOperand(bool optional,
180 unsigned &operandIndex,
181 unsigned &physicalOperandIndex,
182 unsigned &numPhysicalOperands,
183 const unsigned *operandMapping,
184 OperandEncoding (*encodingFromString)
188 /// shouldBeEmitted - Returns the shouldBeEmitted field. Although filter()
189 /// filters out many instructions, at various points in decoding we
190 /// determine that the instruction should not actually be decodable. In
191 /// particular, MMX MOV instructions aren't emitted, but they're only
192 /// identified during operand parsing.
194 /// @return - true if at this point we believe the instruction should be
195 /// emitted; false if not. This will return false if filter() returns false
196 /// once emitInstructionSpecifier() has been called.
197 bool shouldBeEmitted() const {
198 return ShouldBeEmitted;
201 /// emitInstructionSpecifier - Loads the instruction specifier for the current
202 /// instruction into a DisassemblerTables.
204 void emitInstructionSpecifier();
206 /// emitDecodePath - Populates the proper fields in the decode tables
207 /// corresponding to the decode paths for this instruction.
209 /// \param tables The DisassemblerTables to populate with the decode
210 /// decode information for the current instruction.
211 void emitDecodePath(DisassemblerTables &tables) const;
213 /// Constructor - Initializes a RecognizableInstr with the appropriate fields
214 /// from a CodeGenInstruction.
216 /// \param tables The DisassemblerTables that the specifier will be added to.
217 /// \param insn The CodeGenInstruction to extract information from.
218 /// \param uid The unique ID of the current instruction.
219 RecognizableInstr(DisassemblerTables &tables,
220 const CodeGenInstruction &insn,
223 /// processInstr - Accepts a CodeGenInstruction and loads decode information
224 /// for it into a DisassemblerTables if appropriate.
226 /// \param tables The DiassemblerTables to be populated with decode
228 /// \param insn The CodeGenInstruction to be used as a source for this
230 /// \param uid The unique ID of the instruction.
231 static void processInstr(DisassemblerTables &tables,
232 const CodeGenInstruction &insn,
236 } // namespace X86Disassembler