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