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 "CodeGenInstruction.h"
20 #include "llvm/Support/X86DisassemblerDecoderCommon.h"
25 struct InstructionSpecifier
;
29 #define X86_INSTR_MRM_MAPPING \
95 // A clone of X86 since we can't depend on something that is generated.
109 MRMDestMem4VOp3CC
= 20,
151 #define MAP(from, to) MRM_##from = to,
152 X86_INSTR_MRM_MAPPING
171 enum { PD
= 1, XS
= 2, XD
= 3, PS
= 4 };
172 enum { VEX
= 1, XOP
= 2, EVEX
= 3 };
173 enum { OpSize16
= 1, OpSize32
= 2 };
174 enum { AdSize16
= 1, AdSize32
= 2, AdSize64
= 3 };
175 enum { ExplicitREX2
= 1 };
176 } // namespace X86Local
178 namespace X86Disassembler
{
179 class DisassemblerTables
;
180 /// Extract common fields of a single X86 instruction from a CodeGenInstruction
181 struct RecognizableInstrBase
{
182 /// The OpPrefix field from the record
184 /// The OpMap field from the record
186 /// The opcode field from the record; this is the opcode used in the Intel
187 /// encoding and therefore distinct from the UID
189 /// The form field from the record
191 // The encoding field from the record
193 /// The OpSize field from the record
195 /// The AdSize field from the record
197 /// The hasREX_W field from the record
199 /// The hasVEX_4V field from the record
201 /// The IgnoresW field from the record
203 /// The hasVEX_L field from the record
205 /// The ignoreVEX_L field from the record
207 /// The hasEVEX_L2Prefix field from the record
209 /// The hasEVEX_K field from the record
211 /// The hasEVEX_KZ field from the record
213 /// The hasEVEX_B field from the record
215 /// Indicates that the instruction uses the L and L' fields for RC.
217 /// The isCodeGenOnly field from the record
219 /// The isAsmParserOnly field from the record
220 bool IsAsmParserOnly
;
221 /// The ForceDisassemble field from the record
222 bool ForceDisassemble
;
223 // The CD8_Scale field from the record
225 /// If explicitOpPrefix field from the record equals ExplicitREX2
226 bool ExplicitREX2Prefix
;
227 /// \param insn The CodeGenInstruction to extract information from.
228 RecognizableInstrBase(const CodeGenInstruction
&insn
);
229 /// \returns true if this instruction should be emitted
230 bool shouldBeEmitted() const;
233 /// RecognizableInstr - Encapsulates all information required to decode a single
234 /// instruction, as extracted from the LLVM instruction tables. Has methods
235 /// to interpret the information available in the LLVM tables, and to emit the
236 /// instruction into DisassemblerTables.
237 class RecognizableInstr
: public RecognizableInstrBase
{
239 /// The record from the .td files corresponding to this instruction
241 /// The instruction name as listed in the tables
243 // Whether the instruction has the predicate "In32BitMode"
245 // Whether the instruction has the predicate "In64BitMode"
247 /// The operands of the instruction, as listed in the CodeGenInstruction.
248 /// They are not one-to-one with operands listed in the MCInst; for example,
249 /// memory operands expand to 5 operands in the MCInst
250 const std::vector
<CGIOperandList::OperandInfo
> *Operands
;
252 /// The opcode of the instruction, as used in an MCInst
254 /// The description of the instruction that is emitted into the instruction
256 InstructionSpecifier
*Spec
;
258 /// insnContext - Returns the primary context in which the instruction is
261 /// @return - The context in which the instruction is valid.
262 InstructionContext
insnContext() const;
264 /// typeFromString - Translates an operand type from the string provided in
265 /// the LLVM tables to an OperandType for use in the operand specifier.
267 /// @param s - The string, as extracted by calling Rec->getName()
268 /// on a CodeGenInstruction::OperandInfo.
269 /// @param hasREX_W - Indicates whether the instruction has a REX.W
270 /// prefix. If it does, 32-bit register operands stay
271 /// 32-bit regardless of the operand size.
272 /// @param OpSize Indicates the operand size of the instruction.
273 /// If register size does not match OpSize, then
274 /// register sizes keep their size.
275 /// @return - The operand's type.
276 static OperandType
typeFromString(const std::string
&s
, bool hasREX_W
,
279 /// immediateEncodingFromString - Translates an immediate encoding from the
280 /// string provided in the LLVM tables to an OperandEncoding for use in
281 /// the operand specifier.
283 /// @param s - See typeFromString().
284 /// @param OpSize - Indicates whether this is an OpSize16 instruction.
285 /// If it is not, then 16-bit immediate operands stay 16-bit.
286 /// @return - The operand's encoding.
287 static OperandEncoding
immediateEncodingFromString(const std::string
&s
,
290 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
291 /// handles operands that are in the REG field of the ModR/M byte.
292 static OperandEncoding
rmRegisterEncodingFromString(const std::string
&s
,
295 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
296 /// handles operands that are in the REG field of the ModR/M byte.
297 static OperandEncoding
roRegisterEncodingFromString(const std::string
&s
,
299 static OperandEncoding
memoryEncodingFromString(const std::string
&s
,
301 static OperandEncoding
relocationEncodingFromString(const std::string
&s
,
303 static OperandEncoding
opcodeModifierEncodingFromString(const std::string
&s
,
305 static OperandEncoding
vvvvRegisterEncodingFromString(const std::string
&s
,
307 static OperandEncoding
308 writemaskRegisterEncodingFromString(const std::string
&s
, uint8_t OpSize
);
310 /// Adjust the encoding type for an operand based on the instruction.
311 void adjustOperandEncoding(OperandEncoding
&encoding
);
313 /// handleOperand - Converts a single operand from the LLVM table format to
314 /// the emitted table format, handling any duplicate operands it encounters
315 /// and then one non-duplicate.
317 /// @param optional - Determines whether to assert that the
319 /// @param operandIndex - The index into the generated operand table.
320 /// Incremented by this function one or more
321 /// times to reflect possible duplicate
323 /// @param physicalOperandIndex - The index of the current operand into the
324 /// set of non-duplicate ('physical') operands.
325 /// Incremented by this function once.
326 /// @param numPhysicalOperands - The number of non-duplicate operands in the
328 /// @param operandMapping - The operand mapping, which has an entry for
329 /// each operand that indicates whether it is a
330 /// duplicate, and of what.
331 void handleOperand(bool optional
, unsigned &operandIndex
,
332 unsigned &physicalOperandIndex
,
333 unsigned numPhysicalOperands
,
334 const unsigned *operandMapping
,
335 OperandEncoding (*encodingFromString
)(const std::string
&,
338 /// emitInstructionSpecifier - Loads the instruction specifier for the current
339 /// instruction into a DisassemblerTables.
341 void emitInstructionSpecifier();
343 /// emitDecodePath - Populates the proper fields in the decode tables
344 /// corresponding to the decode paths for this instruction.
346 /// \param tables The DisassemblerTables to populate with the decode
347 /// decode information for the current instruction.
348 void emitDecodePath(DisassemblerTables
&tables
) const;
351 /// Constructor - Initializes a RecognizableInstr with the appropriate fields
352 /// from a CodeGenInstruction.
354 /// \param tables The DisassemblerTables that the specifier will be added to.
355 /// \param insn The CodeGenInstruction to extract information from.
356 /// \param uid The unique ID of the current instruction.
357 RecognizableInstr(DisassemblerTables
&tables
, const CodeGenInstruction
&insn
,
359 /// processInstr - Accepts a CodeGenInstruction and loads decode information
360 /// for it into a DisassemblerTables if appropriate.
362 /// \param tables The DiassemblerTables to be populated with decode
364 /// \param insn The CodeGenInstruction to be used as a source for this
366 /// \param uid The unique ID of the instruction.
367 static void processInstr(DisassemblerTables
&tables
,
368 const CodeGenInstruction
&insn
, InstrUID uid
);
371 std::string
getMnemonic(const CodeGenInstruction
*I
, unsigned Variant
);
372 bool isRegisterOperand(const Record
*Rec
);
373 bool isMemoryOperand(const Record
*Rec
);
374 bool isImmediateOperand(const Record
*Rec
);
375 unsigned getRegOperandSize(const Record
*RegRec
);
376 unsigned getMemOperandSize(const Record
*MemRec
);
377 } // namespace X86Disassembler