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 "Common/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.
111 MRMDestMem4VOp3CC
= 20,
153 #define MAP(from, to) MRM_##from = to,
154 X86_INSTR_MRM_MAPPING
173 enum { PD
= 1, XS
= 2, XD
= 3, PS
= 4 };
174 enum { VEX
= 1, XOP
= 2, EVEX
= 3 };
175 enum { OpSize16
= 1, OpSize32
= 2 };
176 enum { AdSize16
= 1, AdSize32
= 2, AdSize64
= 3 };
177 enum { ExplicitREX2
= 1, ExplicitEVEX
= 3 };
178 } // namespace X86Local
180 namespace X86Disassembler
{
181 class DisassemblerTables
;
182 /// Extract common fields of a single X86 instruction from a CodeGenInstruction
183 struct RecognizableInstrBase
{
184 /// The OpPrefix field from the record
186 /// The OpMap field from the record
188 /// The opcode field from the record; this is the opcode used in the Intel
189 /// encoding and therefore distinct from the UID
191 /// The form field from the record
193 // The encoding field from the record
195 /// The OpSize field from the record
197 /// The AdSize field from the record
199 /// The hasREX_W field from the record
201 /// The hasVEX_4V field from the record
203 /// The IgnoresW field from the record
205 /// The hasVEX_L field from the record
207 /// The ignoreVEX_L field from the record
209 /// The hasEVEX_L2Prefix field from the record
211 /// The hasEVEX_K field from the record
213 /// The hasEVEX_KZ field from the record
215 /// The hasEVEX_B field from the record
217 /// The hasEVEX_U field from the record
219 /// The hasEVEX_NF field from the record
221 /// The hasTwoConditionalOps field from the record
222 bool HasTwoConditionalOps
;
223 /// Indicates that the instruction uses the L and L' fields for RC.
225 /// The isCodeGenOnly field from the record
227 /// The isAsmParserOnly field from the record
228 bool IsAsmParserOnly
;
229 /// The ForceDisassemble field from the record
230 bool ForceDisassemble
;
231 // The CD8_Scale field from the record
233 /// If explicitOpPrefix field from the record equals ExplicitREX2
234 bool ExplicitREX2Prefix
;
235 /// \param insn The CodeGenInstruction to extract information from.
236 RecognizableInstrBase(const CodeGenInstruction
&insn
);
237 /// \returns true if this instruction should be emitted
238 bool shouldBeEmitted() const;
241 /// RecognizableInstr - Encapsulates all information required to decode a single
242 /// instruction, as extracted from the LLVM instruction tables. Has methods
243 /// to interpret the information available in the LLVM tables, and to emit the
244 /// instruction into DisassemblerTables.
245 class RecognizableInstr
: public RecognizableInstrBase
{
247 /// The record from the .td files corresponding to this instruction
249 /// The instruction name as listed in the tables
251 // Whether the instruction has the predicate "In32BitMode"
253 // Whether the instruction has the predicate "In64BitMode"
255 /// The operands of the instruction, as listed in the CodeGenInstruction.
256 /// They are not one-to-one with operands listed in the MCInst; for example,
257 /// memory operands expand to 5 operands in the MCInst
258 const std::vector
<CGIOperandList::OperandInfo
> *Operands
;
260 /// The opcode of the instruction, as used in an MCInst
262 /// The description of the instruction that is emitted into the instruction
264 InstructionSpecifier
*Spec
;
266 /// insnContext - Returns the primary context in which the instruction is
269 /// @return - The context in which the instruction is valid.
270 InstructionContext
insnContext() const;
272 /// typeFromString - Translates an operand type from the string provided in
273 /// the LLVM tables to an OperandType for use in the operand specifier.
275 /// @param s - The string, as extracted by calling Rec->getName()
276 /// on a CodeGenInstruction::OperandInfo.
277 /// @param hasREX_W - Indicates whether the instruction has a REX.W
278 /// prefix. If it does, 32-bit register operands stay
279 /// 32-bit regardless of the operand size.
280 /// @param OpSize Indicates the operand size of the instruction.
281 /// If register size does not match OpSize, then
282 /// register sizes keep their size.
283 /// @return - The operand's type.
284 static OperandType
typeFromString(const std::string
&s
, bool hasREX_W
,
287 /// immediateEncodingFromString - Translates an immediate encoding from the
288 /// string provided in the LLVM tables to an OperandEncoding for use in
289 /// the operand specifier.
291 /// @param s - See typeFromString().
292 /// @param OpSize - Indicates whether this is an OpSize16 instruction.
293 /// If it is not, then 16-bit immediate operands stay 16-bit.
294 /// @return - The operand's encoding.
295 static OperandEncoding
immediateEncodingFromString(const std::string
&s
,
298 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
299 /// handles operands that are in the REG field of the ModR/M byte.
300 static OperandEncoding
rmRegisterEncodingFromString(const std::string
&s
,
303 /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
304 /// handles operands that are in the REG field of the ModR/M byte.
305 static OperandEncoding
roRegisterEncodingFromString(const std::string
&s
,
307 static OperandEncoding
memoryEncodingFromString(const std::string
&s
,
309 static OperandEncoding
relocationEncodingFromString(const std::string
&s
,
311 static OperandEncoding
opcodeModifierEncodingFromString(const std::string
&s
,
313 static OperandEncoding
vvvvRegisterEncodingFromString(const std::string
&s
,
315 static OperandEncoding
316 writemaskRegisterEncodingFromString(const std::string
&s
, uint8_t OpSize
);
318 /// Adjust the encoding type for an operand based on the instruction.
319 void adjustOperandEncoding(OperandEncoding
&encoding
);
321 /// handleOperand - Converts a single operand from the LLVM table format to
322 /// the emitted table format, handling any duplicate operands it encounters
323 /// and then one non-duplicate.
325 /// @param optional - Determines whether to assert that the
327 /// @param operandIndex - The index into the generated operand table.
328 /// Incremented by this function one or more
329 /// times to reflect possible duplicate
331 /// @param physicalOperandIndex - The index of the current operand into the
332 /// set of non-duplicate ('physical') operands.
333 /// Incremented by this function once.
334 /// @param numPhysicalOperands - The number of non-duplicate operands in the
336 /// @param operandMapping - The operand mapping, which has an entry for
337 /// each operand that indicates whether it is a
338 /// duplicate, and of what.
339 void handleOperand(bool optional
, unsigned &operandIndex
,
340 unsigned &physicalOperandIndex
,
341 unsigned numPhysicalOperands
,
342 const unsigned *operandMapping
,
343 OperandEncoding (*encodingFromString
)(const std::string
&,
346 /// emitInstructionSpecifier - Loads the instruction specifier for the current
347 /// instruction into a DisassemblerTables.
349 void emitInstructionSpecifier();
351 /// emitDecodePath - Populates the proper fields in the decode tables
352 /// corresponding to the decode paths for this instruction.
354 /// \param tables The DisassemblerTables to populate with the decode
355 /// decode information for the current instruction.
356 void emitDecodePath(DisassemblerTables
&tables
) const;
359 /// Constructor - Initializes a RecognizableInstr with the appropriate fields
360 /// from a CodeGenInstruction.
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.
365 RecognizableInstr(DisassemblerTables
&tables
, const CodeGenInstruction
&insn
,
367 /// processInstr - Accepts a CodeGenInstruction and loads decode information
368 /// for it into a DisassemblerTables if appropriate.
370 /// \param tables The DiassemblerTables to be populated with decode
372 /// \param insn The CodeGenInstruction to be used as a source for this
374 /// \param uid The unique ID of the instruction.
375 static void processInstr(DisassemblerTables
&tables
,
376 const CodeGenInstruction
&insn
, InstrUID uid
);
379 std::string
getMnemonic(const CodeGenInstruction
*I
, unsigned Variant
);
380 bool isRegisterOperand(const Record
*Rec
);
381 bool isMemoryOperand(const Record
*Rec
);
382 bool isImmediateOperand(const Record
*Rec
);
383 unsigned getRegOperandSize(const Record
*RegRec
);
384 unsigned getMemOperandSize(const Record
*MemRec
);
385 } // namespace X86Disassembler