ff111a3dabc58ceef7a98888e41a277d5468a419
[oota-llvm.git] / 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 X86RECOGNIZABLEINSTR_H
18 #define X86RECOGNIZABLEINSTR_H
19
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"
25
26 namespace llvm {
27
28 namespace X86Disassembler {
29
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 {
35 private:
36   /// The opcode of the instruction, as used in an MCInst
37   InstrUID UID;
38   /// The record from the .td files corresponding to this instruction
39   const Record* Rec;
40   /// The prefix field from the record
41   uint8_t Prefix;
42   /// The opcode field from the record; this is the opcode used in the Intel
43   /// encoding and therefore distinct from the UID
44   uint8_t Opcode;
45   /// The form field from the record
46   uint8_t Form;
47   /// The segment override field from the record
48   uint8_t SegOvr;
49   /// The hasOpSizePrefix field from the record
50   bool HasOpSizePrefix;
51   /// The hasAdSizePrefix field from the record
52   bool HasAdSizePrefix;
53   /// The hasREX_WPrefix field from the record
54   bool HasREX_WPrefix;
55   /// The hasVEXPrefix field from the record
56   bool HasVEXPrefix;
57   /// The hasVEX_4VPrefix field from the record
58   bool HasVEX_4VPrefix;
59   /// The hasVEX_4VOp3Prefix field from the record
60   bool HasVEX_4VOp3Prefix;
61   /// The hasVEX_WPrefix field from the record
62   bool HasVEX_WPrefix;
63   /// Inferred from the operands; indicates whether the L bit in the VEX prefix is set
64   bool HasVEX_LPrefix;
65   /// The hasMemOp4Prefix field from the record
66   bool HasMemOp4Prefix;
67   /// The ignoreVEX_L field from the record
68   bool IgnoresVEX_L;
69   /// The hasEVEXPrefix field from the record
70   bool HasEVEXPrefix;
71   /// The hasEVEX_L2Prefix field from the record
72   bool HasEVEX_L2Prefix;
73   /// The hasEVEX_K field from the record
74   bool HasEVEX_K;
75   /// The hasEVEX_KZ field from the record
76   bool HasEVEX_KZ;
77   /// The hasEVEX_B field from the record
78   bool HasEVEX_B;
79   /// The hasLockPrefix field from the record
80   bool HasLockPrefix;
81   /// The isCodeGenOnly field from the record
82   bool IsCodeGenOnly;
83   /// The ForceDisassemble field from the record
84   bool ForceDisassemble;
85   // Whether the instruction has the predicate "In64BitMode"
86   bool Is64Bit;
87   // Whether the instruction has the predicate "In32BitMode"
88   bool Is32Bit;
89
90   /// The instruction name as listed in the tables
91   std::string Name;
92   /// The AT&T AsmString for the instruction
93   std::string AsmString;
94   
95   /// Indicates whether the instruction is SSE
96   bool IsSSE;
97   /// Indicates whether the instruction should be emitted into the decode
98   /// tables; regardless, it will be emitted into the instruction info table
99   bool ShouldBeEmitted;
100   
101   /// The operands of the instruction, as listed in the CodeGenInstruction.
102   /// They are not one-to-one with operands listed in the MCInst; for example,
103   /// memory operands expand to 5 operands in the MCInst
104   const std::vector<CGIOperandList::OperandInfo>* Operands;
105   
106   /// The description of the instruction that is emitted into the instruction
107   /// info table
108   InstructionSpecifier* Spec;
109
110   /// insnContext - Returns the primary context in which the instruction is
111   ///   valid.
112   ///
113   /// @return - The context in which the instruction is valid.
114   InstructionContext insnContext() const;
115   
116   enum filter_ret {
117     FILTER_STRONG,    // instruction has no place in the instruction tables
118     FILTER_WEAK,      // instruction may conflict, and should be eliminated if
119                       // it does
120     FILTER_NORMAL     // instruction should have high priority and generate an
121                       // error if it conflcits with any other FILTER_NORMAL
122                       // instruction
123   };
124       
125   /// filter - Determines whether the instruction should be decodable.  Some 
126   ///   instructions are pure intrinsics and use unencodable operands; many
127   ///   synthetic instructions are duplicates of other instructions; other
128   ///   instructions only differ in the logical way in which they are used, and
129   ///   have the same decoding.  Because these would cause decode conflicts,
130   ///   they must be filtered out.
131   ///
132   /// @return - The degree of filtering to be applied (see filter_ret).
133   filter_ret filter() const;
134
135   /// hasFROperands - Returns true if any operand is a FR operand.
136   bool hasFROperands() const;
137
138   /// typeFromString - Translates an operand type from the string provided in
139   ///   the LLVM tables to an OperandType for use in the operand specifier.
140   ///
141   /// @param s              - The string, as extracted by calling Rec->getName()
142   ///                         on a CodeGenInstruction::OperandInfo.
143   /// @param isSSE          - Indicates whether the instruction is an SSE 
144   ///                         instruction.  For SSE instructions, immediates are 
145   ///                         fixed-size rather than being affected by the
146   ///                         mandatory OpSize prefix.
147   /// @param hasREX_WPrefix - Indicates whether the instruction has a REX.W
148   ///                         prefix.  If it does, 32-bit register operands stay
149   ///                         32-bit regardless of the operand size.
150   /// @param hasOpSizePrefix  Indicates whether the instruction has an OpSize
151   ///                         prefix.  If it does not, then 16-bit register
152   ///                         operands stay 16-bit.
153   /// @return               - The operand's type.
154   static OperandType typeFromString(const std::string& s, 
155                                     bool isSSE,
156                                     bool hasREX_WPrefix,
157                                     bool hasOpSizePrefix);
158   
159   /// immediateEncodingFromString - Translates an immediate encoding from the
160   ///   string provided in the LLVM tables to an OperandEncoding for use in
161   ///   the operand specifier.
162   ///
163   /// @param s                - See typeFromString().
164   /// @param hasOpSizePrefix  - Indicates whether the instruction has an OpSize
165   ///                           prefix.  If it does not, then 16-bit immediate
166   ///                           operands stay 16-bit.
167   /// @return                 - The operand's encoding.
168   static OperandEncoding immediateEncodingFromString(const std::string &s,
169                                                      bool hasOpSizePrefix);
170   
171   /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
172   ///   handles operands that are in the REG field of the ModR/M byte.
173   static OperandEncoding rmRegisterEncodingFromString(const std::string &s,
174                                                       bool hasOpSizePrefix);
175   
176   /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
177   ///   handles operands that are in the REG field of the ModR/M byte.
178   static OperandEncoding roRegisterEncodingFromString(const std::string &s,
179                                                       bool hasOpSizePrefix);
180   static OperandEncoding memoryEncodingFromString(const std::string &s,
181                                                   bool hasOpSizePrefix);
182   static OperandEncoding relocationEncodingFromString(const std::string &s,
183                                                       bool hasOpSizePrefix);
184   static OperandEncoding opcodeModifierEncodingFromString(const std::string &s,
185                                                           bool hasOpSizePrefix);
186   static OperandEncoding vvvvRegisterEncodingFromString(const std::string &s,
187                                                         bool HasOpSizePrefix);
188   static OperandEncoding writemaskRegisterEncodingFromString(const std::string &s,
189                                                              bool HasOpSizePrefix);
190   
191   /// handleOperand - Converts a single operand from the LLVM table format to
192   ///   the emitted table format, handling any duplicate operands it encounters
193   ///   and then one non-duplicate.
194   ///
195   /// @param optional             - Determines whether to assert that the
196   ///                               operand exists.
197   /// @param operandIndex         - The index into the generated operand table.
198   ///                               Incremented by this function one or more
199   ///                               times to reflect possible duplicate 
200   ///                               operands).
201   /// @param physicalOperandIndex - The index of the current operand into the
202   ///                               set of non-duplicate ('physical') operands.
203   ///                               Incremented by this function once.
204   /// @param numPhysicalOperands  - The number of non-duplicate operands in the
205   ///                               instructions.
206   /// @param operandMapping       - The operand mapping, which has an entry for
207   ///                               each operand that indicates whether it is a
208   ///                               duplicate, and of what.
209   void handleOperand(bool optional,
210                      unsigned &operandIndex,
211                      unsigned &physicalOperandIndex,
212                      unsigned &numPhysicalOperands,
213                      const unsigned *operandMapping,
214                      OperandEncoding (*encodingFromString)
215                        (const std::string&,
216                         bool hasOpSizePrefix));
217   
218   /// shouldBeEmitted - Returns the shouldBeEmitted field.  Although filter()
219   ///   filters out many instructions, at various points in decoding we
220   ///   determine that the instruction should not actually be decodable.  In
221   ///   particular, MMX MOV instructions aren't emitted, but they're only
222   ///   identified during operand parsing.
223   ///
224   /// @return - true if at this point we believe the instruction should be
225   ///   emitted; false if not.  This will return false if filter() returns false
226   ///   once emitInstructionSpecifier() has been called.
227   bool shouldBeEmitted() const {
228     return ShouldBeEmitted;
229   }
230   
231   /// emitInstructionSpecifier - Loads the instruction specifier for the current
232   ///   instruction into a DisassemblerTables.
233   ///
234   void emitInstructionSpecifier();
235   
236   /// emitDecodePath - Populates the proper fields in the decode tables
237   ///   corresponding to the decode paths for this instruction.
238   ///
239   /// \param tables The DisassemblerTables to populate with the decode
240   ///               decode information for the current instruction.
241   void emitDecodePath(DisassemblerTables &tables) const;
242
243   /// Constructor - Initializes a RecognizableInstr with the appropriate fields
244   ///   from a CodeGenInstruction.
245   ///
246   /// \param tables The DisassemblerTables that the specifier will be added to.
247   /// \param insn   The CodeGenInstruction to extract information from.
248   /// \param uid    The unique ID of the current instruction.
249   RecognizableInstr(DisassemblerTables &tables,
250                     const CodeGenInstruction &insn,
251                     InstrUID uid);
252 public:
253   /// processInstr - Accepts a CodeGenInstruction and loads decode information
254   ///   for it into a DisassemblerTables if appropriate.
255   ///
256   /// \param tables The DiassemblerTables to be populated with decode
257   ///               information.
258   /// \param insn   The CodeGenInstruction to be used as a source for this
259   ///               information.
260   /// \param uid    The unique ID of the instruction.
261   static void processInstr(DisassemblerTables &tables,
262                            const CodeGenInstruction &insn,
263                            InstrUID uid);
264 };
265   
266 } // namespace X86Disassembler
267
268 } // namespace llvm
269
270 #endif