]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/utils/TableGen/X86RecognizableInstr.h
Merge from vendor branch importing dtc 1.4.3
[FreeBSD/FreeBSD.git] / contrib / llvm / utils / TableGen / X86RecognizableInstr.h
1 //===- X86RecognizableInstr.h - Disassembler instruction spec ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
18 #define LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
19
20 #include "CodeGenTarget.h"
21 #include "X86DisassemblerTables.h"
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/TableGen/Record.h"
24
25 namespace llvm {
26
27 namespace X86Disassembler {
28
29 /// RecognizableInstr - Encapsulates all information required to decode a single
30 ///   instruction, as extracted from the LLVM instruction tables.  Has methods
31 ///   to interpret the information available in the LLVM tables, and to emit the
32 ///   instruction into DisassemblerTables.
33 class RecognizableInstr {
34 private:
35   /// The opcode of the instruction, as used in an MCInst
36   InstrUID UID;
37   /// The record from the .td files corresponding to this instruction
38   const Record* Rec;
39   /// The OpPrefix field from the record
40   uint8_t OpPrefix;
41   /// The OpMap field from the record
42   uint8_t OpMap;
43   /// The opcode field from the record; this is the opcode used in the Intel
44   /// encoding and therefore distinct from the UID
45   uint8_t Opcode;
46   /// The form field from the record
47   uint8_t Form;
48   // The encoding field from the record
49   uint8_t Encoding;
50   /// The OpSize field from the record
51   uint8_t OpSize;
52   /// The AdSize field from the record
53   uint8_t AdSize;
54   /// The hasREX_WPrefix field from the record
55   bool HasREX_WPrefix;
56   /// The hasVEX_4V field from the record
57   bool HasVEX_4V;
58   /// The hasVEX_WPrefix field from the record
59   bool HasVEX_WPrefix;
60   /// Inferred from the operands; indicates whether the L bit in the VEX prefix is set
61   bool HasVEX_LPrefix;
62   /// The ignoreVEX_L field from the record
63   bool IgnoresVEX_L;
64   /// The hasEVEX_L2Prefix field from the record
65   bool HasEVEX_L2Prefix;
66   /// The hasEVEX_K field from the record
67   bool HasEVEX_K;
68   /// The hasEVEX_KZ field from the record
69   bool HasEVEX_KZ;
70   /// The hasEVEX_B field from the record
71   bool HasEVEX_B;
72   /// The isCodeGenOnly field from the record
73   bool IsCodeGenOnly;
74   /// The ForceDisassemble field from the record
75   bool ForceDisassemble;
76   // The CD8_Scale field from the record
77   uint8_t CD8_Scale;
78   // Whether the instruction has the predicate "In64BitMode"
79   bool Is64Bit;
80   // Whether the instruction has the predicate "In32BitMode"
81   bool Is32Bit;
82
83   /// The instruction name as listed in the tables
84   std::string Name;
85
86   /// Indicates whether the instruction should be emitted into the decode
87   /// tables; regardless, it will be emitted into the instruction info table
88   bool ShouldBeEmitted;
89   
90   /// The operands of the instruction, as listed in the CodeGenInstruction.
91   /// They are not one-to-one with operands listed in the MCInst; for example,
92   /// memory operands expand to 5 operands in the MCInst
93   const std::vector<CGIOperandList::OperandInfo>* Operands;
94   
95   /// The description of the instruction that is emitted into the instruction
96   /// info table
97   InstructionSpecifier* Spec;
98
99   /// insnContext - Returns the primary context in which the instruction is
100   ///   valid.
101   ///
102   /// @return - The context in which the instruction is valid.
103   InstructionContext insnContext() const;
104
105   /// typeFromString - Translates an operand type from the string provided in
106   ///   the LLVM tables to an OperandType for use in the operand specifier.
107   ///
108   /// @param s              - The string, as extracted by calling Rec->getName()
109   ///                         on a CodeGenInstruction::OperandInfo.
110   /// @param hasREX_WPrefix - Indicates whether the instruction has a REX.W
111   ///                         prefix.  If it does, 32-bit register operands stay
112   ///                         32-bit regardless of the operand size.
113   /// @param OpSize           Indicates the operand size of the instruction.
114   ///                         If register size does not match OpSize, then
115   ///                         register sizes keep their size.
116   /// @return               - The operand's type.
117   static OperandType typeFromString(const std::string& s,
118                                     bool hasREX_WPrefix, uint8_t OpSize);
119
120   /// immediateEncodingFromString - Translates an immediate encoding from the
121   ///   string provided in the LLVM tables to an OperandEncoding for use in
122   ///   the operand specifier.
123   ///
124   /// @param s       - See typeFromString().
125   /// @param OpSize  - Indicates whether this is an OpSize16 instruction.
126   ///                  If it is not, then 16-bit immediate operands stay 16-bit.
127   /// @return        - The operand's encoding.
128   static OperandEncoding immediateEncodingFromString(const std::string &s,
129                                                      uint8_t OpSize);
130
131   /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
132   ///   handles operands that are in the REG field of the ModR/M byte.
133   static OperandEncoding rmRegisterEncodingFromString(const std::string &s,
134                                                       uint8_t OpSize);
135
136   /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
137   ///   handles operands that are in the REG field of the ModR/M byte.
138   static OperandEncoding roRegisterEncodingFromString(const std::string &s,
139                                                       uint8_t OpSize);
140   static OperandEncoding memoryEncodingFromString(const std::string &s,
141                                                   uint8_t OpSize);
142   static OperandEncoding relocationEncodingFromString(const std::string &s,
143                                                       uint8_t OpSize);
144   static OperandEncoding opcodeModifierEncodingFromString(const std::string &s,
145                                                           uint8_t OpSize);
146   static OperandEncoding vvvvRegisterEncodingFromString(const std::string &s,
147                                                         uint8_t OpSize);
148   static OperandEncoding writemaskRegisterEncodingFromString(const std::string &s,
149                                                              uint8_t OpSize);
150
151   /// \brief Adjust the encoding type for an operand based on the instruction.
152   void adjustOperandEncoding(OperandEncoding &encoding);
153
154   /// handleOperand - Converts a single operand from the LLVM table format to
155   ///   the emitted table format, handling any duplicate operands it encounters
156   ///   and then one non-duplicate.
157   ///
158   /// @param optional             - Determines whether to assert that the
159   ///                               operand exists.
160   /// @param operandIndex         - The index into the generated operand table.
161   ///                               Incremented by this function one or more
162   ///                               times to reflect possible duplicate 
163   ///                               operands).
164   /// @param physicalOperandIndex - The index of the current operand into the
165   ///                               set of non-duplicate ('physical') operands.
166   ///                               Incremented by this function once.
167   /// @param numPhysicalOperands  - The number of non-duplicate operands in the
168   ///                               instructions.
169   /// @param operandMapping       - The operand mapping, which has an entry for
170   ///                               each operand that indicates whether it is a
171   ///                               duplicate, and of what.
172   void handleOperand(bool optional,
173                      unsigned &operandIndex,
174                      unsigned &physicalOperandIndex,
175                      unsigned numPhysicalOperands,
176                      const unsigned *operandMapping,
177                      OperandEncoding (*encodingFromString)
178                        (const std::string&,
179                         uint8_t OpSize));
180
181   /// shouldBeEmitted - Returns the shouldBeEmitted field.  Although filter()
182   ///   filters out many instructions, at various points in decoding we
183   ///   determine that the instruction should not actually be decodable.  In
184   ///   particular, MMX MOV instructions aren't emitted, but they're only
185   ///   identified during operand parsing.
186   ///
187   /// @return - true if at this point we believe the instruction should be
188   ///   emitted; false if not.  This will return false if filter() returns false
189   ///   once emitInstructionSpecifier() has been called.
190   bool shouldBeEmitted() const {
191     return ShouldBeEmitted;
192   }
193   
194   /// emitInstructionSpecifier - Loads the instruction specifier for the current
195   ///   instruction into a DisassemblerTables.
196   ///
197   void emitInstructionSpecifier();
198   
199   /// emitDecodePath - Populates the proper fields in the decode tables
200   ///   corresponding to the decode paths for this instruction.
201   ///
202   /// \param tables The DisassemblerTables to populate with the decode
203   ///               decode information for the current instruction.
204   void emitDecodePath(DisassemblerTables &tables) const;
205
206   /// Constructor - Initializes a RecognizableInstr with the appropriate fields
207   ///   from a CodeGenInstruction.
208   ///
209   /// \param tables The DisassemblerTables that the specifier will be added to.
210   /// \param insn   The CodeGenInstruction to extract information from.
211   /// \param uid    The unique ID of the current instruction.
212   RecognizableInstr(DisassemblerTables &tables,
213                     const CodeGenInstruction &insn,
214                     InstrUID uid);
215 public:
216   /// processInstr - Accepts a CodeGenInstruction and loads decode information
217   ///   for it into a DisassemblerTables if appropriate.
218   ///
219   /// \param tables The DiassemblerTables to be populated with decode
220   ///               information.
221   /// \param insn   The CodeGenInstruction to be used as a source for this
222   ///               information.
223   /// \param uid    The unique ID of the instruction.
224   static void processInstr(DisassemblerTables &tables,
225                            const CodeGenInstruction &insn,
226                            InstrUID uid);
227 };
228   
229 } // namespace X86Disassembler
230
231 } // namespace llvm
232
233 #endif