1 //===- X86RecognizableInstr.h - Disassembler instruction spec ----*- C++ -*-===//
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 Emitter.
10 // It contains the interface of a single recognizable instruction.
11 // Documentation for the disassembler emitter in general can be found in
12 // X86DisassemblerEmitter.h.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
17 #define LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
19 #include "CodeGenTarget.h"
20 #include "X86DisassemblerTables.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/TableGen/Record.h"
26 #define X86_INSTR_MRM_MAPPING \
92 // A clone of X86 since we can't depend on something that is generated.
110 MRMXmCC
= 38, MRMXm
= 39,
111 MRM0m
= 40, MRM1m
= 41, MRM2m
= 42, MRM3m
= 43,
112 MRM4m
= 44, MRM5m
= 45, MRM6m
= 46, MRM7m
= 47,
118 MRMXrCC
= 54, MRMXr
= 55,
119 MRM0r
= 56, MRM1r
= 57, MRM2r
= 58, MRM3r
= 59,
120 MRM4r
= 60, MRM5r
= 61, MRM6r
= 62, MRM7r
= 63,
121 #define MAP(from, to) MRM_##from = to,
122 X86_INSTR_MRM_MAPPING
127 OB
= 0, TB
= 1, T8
= 2, TA
= 3, XOP8
= 4, XOP9
= 5, XOPA
= 6, ThreeDNow
= 7
131 PD
= 1, XS
= 2, XD
= 3, PS
= 4
135 VEX
= 1, XOP
= 2, EVEX
= 3
139 OpSize16
= 1, OpSize32
= 2
143 AdSize16
= 1, AdSize32
= 2, AdSize64
= 3
147 namespace X86Disassembler
{
149 /// RecognizableInstr - Encapsulates all information required to decode a single
150 /// instruction, as extracted from the LLVM instruction tables. Has methods
151 /// to interpret the information available in the LLVM tables, and to emit the
152 /// instruction into DisassemblerTables.
153 class RecognizableInstr
{
155 /// The opcode of the instruction, as used in an MCInst
157 /// The record from the .td files corresponding to this instruction
159 /// The OpPrefix field from the record
161 /// The OpMap field from the record
163 /// The opcode field from the record; this is the opcode used in the Intel
164 /// encoding and therefore distinct from the UID
166 /// The form field from the record
168 // The encoding field from the record
170 /// The OpSize field from the record
172 /// The AdSize field from the record
174 /// The hasREX_WPrefix field from the record
176 /// The hasVEX_4V field from the record
178 /// The HasVEX_WPrefix field from the record
180 /// The IgnoresVEX_W field from the record
182 /// Inferred from the operands; indicates whether the L bit in the VEX prefix is set
184 /// The ignoreVEX_L field from the record
186 /// The hasEVEX_L2Prefix field from the record
187 bool HasEVEX_L2Prefix
;
188 /// The hasEVEX_K field from the record
190 /// The hasEVEX_KZ field from the record
192 /// The hasEVEX_B field from the record
194 /// Indicates that the instruction uses the L and L' fields for RC.
196 /// The isCodeGenOnly field from the record
198 /// The ForceDisassemble field from the record
199 bool ForceDisassemble
;
200 // The CD8_Scale field from the record
202 // Whether the instruction has the predicate "In64BitMode"
204 // Whether the instruction has the predicate "In32BitMode"
207 /// The instruction name as listed in the tables
210 /// Indicates whether the instruction should be emitted into the decode
211 /// tables; regardless, it will be emitted into the instruction info table
212 bool ShouldBeEmitted
;
214 /// The operands of the instruction, as listed in the CodeGenInstruction.
215 /// They are not one-to-one with operands listed in the MCInst; for example,
216 /// memory operands expand to 5 operands in the MCInst
217 const std::vector
<CGIOperandList::OperandInfo
>* Operands
;
219 /// The description of the instruction that is emitted into the instruction
221 InstructionSpecifier
* Spec
;
223 /// insnContext - Returns the primary context in which the instruction is
226 /// @return - The context in which the instruction is valid.
227 InstructionContext
insnContext() const;
229 /// typeFromString - Translates an operand type from the string provided in
230 /// the LLVM tables to an OperandType for use in the operand specifier.
232 /// @param s - The string, as extracted by calling Rec->getName()
233 /// on a CodeGenInstruction::OperandInfo.
234 /// @param hasREX_WPrefix - Indicates whether the instruction has a REX.W
235 /// prefix. If it does, 32-bit register operands stay
236 /// 32-bit regardless of the operand size.
237 /// @param OpSize Indicates the operand size of the instruction.
238 /// If register size does not match OpSize, then
239 /// register sizes keep their size.
240 /// @return - The operand's type.
241 static OperandType
typeFromString(const std::string
& s
,
242 bool hasREX_WPrefix
, uint8_t OpSize
);
244 /// immediateEncodingFromString - Translates an immediate encoding from the
245 /// string provided in the LLVM tables to an OperandEncoding for use in
246 /// the operand specifier.
248 /// @param s - See typeFromString().
249 /// @param OpSize - Indicates whether this is an OpSize16 instruction.
250 /// If it is not, then 16-bit immediate operands stay 16-bit.
251 /// @return - The operand's encoding.
252 static OperandEncoding
immediateEncodingFromString(const std::string
&s
,
255 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
256 /// handles operands that are in the REG field of the ModR/M byte.
257 static OperandEncoding
rmRegisterEncodingFromString(const std::string
&s
,
260 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
261 /// handles operands that are in the REG field of the ModR/M byte.
262 static OperandEncoding
roRegisterEncodingFromString(const std::string
&s
,
264 static OperandEncoding
memoryEncodingFromString(const std::string
&s
,
266 static OperandEncoding
relocationEncodingFromString(const std::string
&s
,
268 static OperandEncoding
opcodeModifierEncodingFromString(const std::string
&s
,
270 static OperandEncoding
vvvvRegisterEncodingFromString(const std::string
&s
,
272 static OperandEncoding
writemaskRegisterEncodingFromString(const std::string
&s
,
275 /// Adjust the encoding type for an operand based on the instruction.
276 void adjustOperandEncoding(OperandEncoding
&encoding
);
278 /// handleOperand - Converts a single operand from the LLVM table format to
279 /// the emitted table format, handling any duplicate operands it encounters
280 /// and then one non-duplicate.
282 /// @param optional - Determines whether to assert that the
284 /// @param operandIndex - The index into the generated operand table.
285 /// Incremented by this function one or more
286 /// times to reflect possible duplicate
288 /// @param physicalOperandIndex - The index of the current operand into the
289 /// set of non-duplicate ('physical') operands.
290 /// Incremented by this function once.
291 /// @param numPhysicalOperands - The number of non-duplicate operands in the
293 /// @param operandMapping - The operand mapping, which has an entry for
294 /// each operand that indicates whether it is a
295 /// duplicate, and of what.
296 void handleOperand(bool optional
,
297 unsigned &operandIndex
,
298 unsigned &physicalOperandIndex
,
299 unsigned numPhysicalOperands
,
300 const unsigned *operandMapping
,
301 OperandEncoding (*encodingFromString
)
305 /// shouldBeEmitted - Returns the shouldBeEmitted field. Although filter()
306 /// filters out many instructions, at various points in decoding we
307 /// determine that the instruction should not actually be decodable. In
308 /// particular, MMX MOV instructions aren't emitted, but they're only
309 /// identified during operand parsing.
311 /// @return - true if at this point we believe the instruction should be
312 /// emitted; false if not. This will return false if filter() returns false
313 /// once emitInstructionSpecifier() has been called.
314 bool shouldBeEmitted() const {
315 return ShouldBeEmitted
;
318 /// emitInstructionSpecifier - Loads the instruction specifier for the current
319 /// instruction into a DisassemblerTables.
321 void emitInstructionSpecifier();
323 /// emitDecodePath - Populates the proper fields in the decode tables
324 /// corresponding to the decode paths for this instruction.
326 /// \param tables The DisassemblerTables to populate with the decode
327 /// decode information for the current instruction.
328 void emitDecodePath(DisassemblerTables
&tables
) const;
330 /// Constructor - Initializes a RecognizableInstr with the appropriate fields
331 /// from a CodeGenInstruction.
333 /// \param tables The DisassemblerTables that the specifier will be added to.
334 /// \param insn The CodeGenInstruction to extract information from.
335 /// \param uid The unique ID of the current instruction.
336 RecognizableInstr(DisassemblerTables
&tables
,
337 const CodeGenInstruction
&insn
,
340 /// processInstr - Accepts a CodeGenInstruction and loads decode information
341 /// for it into a DisassemblerTables if appropriate.
343 /// \param tables The DiassemblerTables to be populated with decode
345 /// \param insn The CodeGenInstruction to be used as a source for this
347 /// \param uid The unique ID of the instruction.
348 static void processInstr(DisassemblerTables
&tables
,
349 const CodeGenInstruction
&insn
,
353 } // namespace X86Disassembler