xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/X86DisassemblerTables.h (revision d30a1689f5b37e78ea189232a8b94a7011dc0dc8)
1 //===- X86DisassemblerTables.h - Disassembler tables ------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is part of the X86 Disassembler Emitter.
10 // It contains the interface of the disassembler tables.
11 // Documentation for the disassembler emitter in general can be found in
12 //  X86DisassemblerEmitter.h.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_UTILS_TABLEGEN_X86DISASSEMBLERTABLES_H
17 #define LLVM_UTILS_TABLEGEN_X86DISASSEMBLERTABLES_H
18 
19 #include "X86DisassemblerShared.h"
20 #include "llvm/Support/X86DisassemblerDecoderCommon.h"
21 #include <map>
22 #include <memory>
23 #include <vector>
24 
25 namespace llvm {
26 class raw_ostream;
27 
28 namespace X86Disassembler {
29 
30 class ModRMFilter;
31 
32 /// DisassemblerTables - Encapsulates all the decode tables being generated by
33 ///   the table emitter.  Contains functions to populate the tables as well as
34 ///   to emit them as hierarchical C structures suitable for consumption by the
35 ///   runtime.
36 class DisassemblerTables {
37 private:
38   /// The decoder tables.  There is one for each opcode type:
39   /// [0] one-byte opcodes
40   /// [1] two-byte opcodes of the form 0f __
41   /// [2] three-byte opcodes of the form 0f 38 __
42   /// [3] three-byte opcodes of the form 0f 3a __
43   /// [4] XOP8 map opcode
44   /// [5] XOP9 map opcode
45   /// [6] XOPA map opcode
46   /// [7] 3dnow map opcode
47   /// [8] fixed length MAP5 opcode
48   /// [9] fixed length MAP6 opcode
49   std::unique_ptr<ContextDecision> Tables[10];
50 
51   // Table of ModRM encodings.
52   typedef std::map<std::vector<unsigned>, unsigned> ModRMMapTy;
53   mutable ModRMMapTy ModRMTable;
54 
55   /// The instruction information table
56   std::vector<InstructionSpecifier> InstructionSpecifiers;
57 
58   /// True if there are primary decode conflicts in the instruction set
59   bool HasConflicts;
60 
61   /// emitModRMDecision - Emits a table of entries corresponding to a single
62   ///   ModR/M decision.  Compacts the ModR/M decision if possible.  ModR/M
63   ///   decisions are printed as:
64   ///
65   ///   { /* struct ModRMDecision */
66   ///     TYPE,
67   ///     modRMTablennnn
68   ///   }
69   ///
70   ///   where nnnn is a unique ID for the corresponding table of IDs.
71   ///   TYPE indicates whether the table has one entry that is the same
72   ///   regardless of ModR/M byte, two entries - one for bytes 0x00-0xbf and one
73   ///   for bytes 0xc0-0xff -, or 256 entries, one for each possible byte.
74   ///   nnnn is the number of a table for looking up these values.  The tables
75   ///   are written separately so that tables consisting entirely of zeros will
76   ///   not be duplicated.  (These all have the name modRMEmptyTable.)  A table
77   ///   is printed as:
78   ///
79   ///   InstrUID modRMTablennnn[k] = {
80   ///     nnnn, /* MNEMONIC */
81   ///     ...
82   ///     nnnn /* MNEMONIC */
83   ///   };
84   ///
85   /// @param o1       - The output stream to print the ID table to.
86   /// @param o2       - The output stream to print the decision structure to.
87   /// @param i1       - The indentation level to use with stream o1.
88   /// @param i2       - The indentation level to use with stream o2.
89   /// @param ModRMTableNum - next table number for adding to ModRMTable.
90   /// @param decision - The ModR/M decision to emit.  This decision has 256
91   ///                   entries - emitModRMDecision decides how to compact it.
92   void emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
93                          unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
94                          ModRMDecision &decision) const;
95 
96   /// emitOpcodeDecision - Emits an OpcodeDecision and all its subsidiary ModR/M
97   ///   decisions.  An OpcodeDecision is printed as:
98   ///
99   ///   { /* struct OpcodeDecision */
100   ///     /* 0x00 */
101   ///     { /* struct ModRMDecision */
102   ///       ...
103   ///     }
104   ///     ...
105   ///   }
106   ///
107   ///   where the ModRMDecision structure is printed as described in the
108   ///   documentation for emitModRMDecision().  emitOpcodeDecision() passes on a
109   ///   stream and indent level for the UID tables generated by
110   ///   emitModRMDecision(), but does not use them itself.
111   ///
112   /// @param o1       - The output stream to print the ID tables generated by
113   ///                   emitModRMDecision() to.
114   /// @param o2       - The output stream for the decision structure itself.
115   /// @param i1       - The indent level to use with stream o1.
116   /// @param i2       - The indent level to use with stream o2.
117   /// @param ModRMTableNum - next table number for adding to ModRMTable.
118   /// @param decision - The OpcodeDecision to emit along with its subsidiary
119   ///                    structures.
120   void emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
121                           unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
122                           OpcodeDecision &decision) const;
123 
124   /// emitContextDecision - Emits a ContextDecision and all its subsidiary
125   ///   Opcode and ModRMDecisions.  A ContextDecision is printed as:
126   ///
127   ///   struct ContextDecision NAME = {
128   ///     { /* OpcodeDecisions */
129   ///       /* IC */
130   ///       { /* struct OpcodeDecision */
131   ///         ...
132   ///       },
133   ///       ...
134   ///     }
135   ///   }
136   ///
137   ///   NAME is the name of the ContextDecision (typically one of the four names
138   ///   ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM from
139   ///   X86DisassemblerDecoderCommon.h).
140   ///   IC is one of the contexts in InstructionContext.  There is an opcode
141   ///   decision for each possible context.
142   ///   The OpcodeDecision structures are printed as described in the
143   ///   documentation for emitOpcodeDecision.
144   ///
145   /// @param o1       - The output stream to print the ID tables generated by
146   ///                   emitModRMDecision() to.
147   /// @param o2       - The output stream to print the decision structure to.
148   /// @param i1       - The indent level to use with stream o1.
149   /// @param i2       - The indent level to use with stream o2.
150   /// @param ModRMTableNum - next table number for adding to ModRMTable.
151   /// @param decision - The ContextDecision to emit along with its subsidiary
152   ///                   structures.
153   /// @param name     - The name for the ContextDecision.
154   void emitContextDecision(raw_ostream &o1, raw_ostream &o2,
155                            unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
156                            ContextDecision &decision, const char* name) const;
157 
158   /// emitInstructionInfo - Prints the instruction specifier table, which has
159   ///   one entry for each instruction, and contains name and operand
160   ///   information.  This table is printed as:
161   ///
162   ///   struct InstructionSpecifier CONTEXTS_SYM[k] = {
163   ///     {
164   ///       /* nnnn */
165   ///       "MNEMONIC",
166   ///       0xnn,
167   ///       {
168   ///         {
169   ///           ENCODING,
170   ///           TYPE
171   ///         },
172   ///         ...
173   ///       }
174   ///     },
175   ///   };
176   ///
177   ///   k is the total number of instructions.
178   ///   nnnn is the ID of the current instruction (0-based).  This table
179   ///   includes entries for non-instructions like PHINODE.
180   ///   0xnn is the lowest possible opcode for the current instruction, used for
181   ///   AddRegFrm instructions to compute the operand's value.
182   ///   ENCODING and TYPE describe the encoding and type for a single operand.
183   ///
184   /// @param o  - The output stream to which the instruction table should be
185   ///             written.
186   /// @param i  - The indent level for use with the stream.
187   void emitInstructionInfo(raw_ostream &o, unsigned &i) const;
188 
189   /// emitContextTable - Prints the table that is used to translate from an
190   ///   instruction attribute mask to an instruction context.  This table is
191   ///   printed as:
192   ///
193   ///   InstructionContext CONTEXTS_STR[256] = {
194   ///     IC, /* 0x00 */
195   ///     ...
196   ///   };
197   ///
198   ///   IC is the context corresponding to the mask 0x00, and there are 256
199   ///   possible masks.
200   ///
201   /// @param o  - The output stream to which the context table should be written.
202   /// @param i  - The indent level for use with the stream.
203   void emitContextTable(raw_ostream &o, uint32_t &i) const;
204 
205   /// emitContextDecisions - Prints all four ContextDecision structures using
206   ///   emitContextDecision().
207   ///
208   /// @param o1 - The output stream to print the ID tables generated by
209   ///             emitModRMDecision() to.
210   /// @param o2 - The output stream to print the decision structures to.
211   /// @param i1 - The indent level to use with stream o1.
212   /// @param i2 - The indent level to use with stream o2.
213   /// @param ModRMTableNum - next table number for adding to ModRMTable.
214   void emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
215                             unsigned &i1, unsigned &i2,
216                             unsigned &ModRMTableNum) const;
217 
218   /// setTableFields - Uses a ModRMFilter to set the appropriate entries in a
219   ///   ModRMDecision to refer to a particular instruction ID.
220   ///
221   /// @param decision - The ModRMDecision to populate.
222   /// @param filter   - The filter to use in deciding which entries to populate.
223   /// @param uid      - The unique ID to set matching entries to.
224   /// @param opcode   - The opcode of the instruction, for error reporting.
225   void setTableFields(ModRMDecision &decision,
226                       const ModRMFilter &filter,
227                       InstrUID uid,
228                       uint8_t opcode);
229 public:
230   /// Constructor - Allocates space for the class decisions and clears them.
231   DisassemblerTables();
232 
233   ~DisassemblerTables();
234 
235   /// emit - Emits the instruction table, context table, and class decisions.
236   ///
237   /// @param o  - The output stream to print the tables to.
238   void emit(raw_ostream &o) const;
239 
240   /// setTableFields - Uses the opcode type, instruction context, opcode, and a
241   ///   ModRMFilter as criteria to set a particular set of entries in the
242   ///   decode tables to point to a specific uid.
243   ///
244   /// @param type         - The opcode type (ONEBYTE, TWOBYTE, etc.)
245   /// @param insnContext  - The context to use (IC, IC_64BIT, etc.)
246   /// @param opcode       - The last byte of the opcode (not counting any escape
247   ///                       or extended opcodes).
248   /// @param filter       - The ModRMFilter that decides which ModR/M byte values
249   ///                       correspond to the desired instruction.
250   /// @param uid          - The unique ID of the instruction.
251   /// @param is32bit      - Instructon is only 32-bit
252   /// @param noPrefix     - Instruction record has no prefix.
253   /// @param ignoresVEX_L - Instruction ignores VEX.L
254   /// @param ignoresVEX_W - Instruction ignores VEX.W
255   /// @param AddrSize     - Instructions address size 16/32/64. 0 is unspecified
256   void setTableFields(OpcodeType type,
257                       InstructionContext insnContext,
258                       uint8_t opcode,
259                       const ModRMFilter &filter,
260                       InstrUID uid,
261                       bool is32bit,
262                       bool noPrefix,
263                       bool ignoresVEX_L,
264                       bool ignoresVEX_W,
265                       unsigned AddrSize);
266 
267   /// specForUID - Returns the instruction specifier for a given unique
268   ///   instruction ID.  Used when resolving collisions.
269   ///
270   /// @param uid  - The unique ID of the instruction.
271   /// @return     - A reference to the instruction specifier.
272   InstructionSpecifier& specForUID(InstrUID uid) {
273     if (uid >= InstructionSpecifiers.size())
274       InstructionSpecifiers.resize(uid + 1);
275 
276     return InstructionSpecifiers[uid];
277   }
278 
279   // hasConflicts - Reports whether there were primary decode conflicts
280   //   from any instructions added to the tables.
281   // @return  - true if there were; false otherwise.
282 
283   bool hasConflicts() {
284     return HasConflicts;
285   }
286 };
287 
288 } // namespace X86Disassembler
289 
290 } // namespace llvm
291 
292 #endif
293