1 //===- X86ModRMFilters.h - Disassembler ModR/M filterss ---------*- 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 ModR/M filters that determine which values of the ModR/M byte
11 //  are valid for a partiuclar instruction.
12 // Documentation for the disassembler emitter in general can be found in
13 //  X86DisassemblerEmitter.h.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_UTILS_TABLEGEN_X86MODRMFILTERS_H
18 #define LLVM_UTILS_TABLEGEN_X86MODRMFILTERS_H
19 
20 #include "llvm/Support/DataTypes.h"
21 
22 namespace llvm {
23 
24 namespace X86Disassembler {
25 
26 /// ModRMFilter - Abstract base class for clases that recognize patterns in
27 ///   ModR/M bytes.
28 class ModRMFilter {
29   virtual void anchor();
30 public:
31   /// Destructor    - Override as necessary.
32   virtual ~ModRMFilter() { }
33 
34   /// isDumb        - Indicates whether this filter returns the same value for
35   ///                 any value of the ModR/M byte.
36   ///
37   /// @result       - True if the filter returns the same value for any ModR/M
38   ///                 byte; false if not.
39   virtual bool isDumb() const { return false; }
40 
41   /// accepts       - Indicates whether the filter accepts a particular ModR/M
42   ///                 byte value.
43   ///
44   /// @result       - True if the filter accepts the ModR/M byte; false if not.
45   virtual bool accepts(uint8_t modRM) const = 0;
46 };
47 
48 /// DumbFilter - Accepts any ModR/M byte.  Used for instructions that do not
49 ///   require a ModR/M byte or instructions where the entire ModR/M byte is used
50 ///   for operands.
51 class DumbFilter : public ModRMFilter {
52   void anchor() override;
53 public:
54   bool isDumb() const override {
55     return true;
56   }
57 
58   bool accepts(uint8_t modRM) const override {
59     return true;
60   }
61 };
62 
63 /// ModFilter - Filters based on the mod bits [bits 7-6] of the ModR/M byte.
64 ///   Some instructions are classified based on whether they are 11 or anything
65 ///   else.  This filter performs that classification.
66 class ModFilter : public ModRMFilter {
67   void anchor() override;
68   bool R;
69 public:
70   /// Constructor
71   ///
72   /// \param r        True if the mod bits of the ModR/M byte must be 11; false
73   ///                 otherwise.  The name r derives from the fact that the mod
74   ///                 bits indicate whether the R/M bits [bits 2-0] signify a
75   ///                 register or a memory operand.
76   ModFilter(bool r) :
77     ModRMFilter(),
78     R(r) {
79   }
80 
81   bool accepts(uint8_t modRM) const override {
82     return (R == ((modRM & 0xc0) == 0xc0));
83   }
84 };
85 
86 /// ExtendedFilter - Extended opcodes are classified based on the value of the
87 ///   mod field [bits 7-6] and the value of the nnn field [bits 5-3].
88 class ExtendedFilter : public ModRMFilter {
89   void anchor() override;
90   bool R;
91   uint8_t NNN;
92 public:
93   /// Constructor
94   ///
95   /// \param r   True if the mod field must be set to 11; false otherwise.
96   ///            The name is explained at ModFilter.
97   /// \param nnn The required value of the nnn field.
98   ExtendedFilter(bool r, uint8_t nnn) :
99     ModRMFilter(),
100     R(r),
101     NNN(nnn) {
102   }
103 
104   bool accepts(uint8_t modRM) const override {
105     return (((R  && ((modRM & 0xc0) == 0xc0)) ||
106              (!R && ((modRM & 0xc0) != 0xc0))) &&
107             (((modRM & 0x38) >> 3) == NNN));
108   }
109 };
110 
111 /// ExtendedRMFilter - Extended opcodes are classified based on the value of the
112 ///   mod field [bits 7-6] and the value of the nnn field [bits 2-0].
113 class ExtendedRMFilter : public ModRMFilter {
114   void anchor() override;
115   bool R;
116   uint8_t NNN;
117 public:
118   /// Constructor
119   ///
120   /// \param r   True if the mod field must be set to 11; false otherwise.
121   ///            The name is explained at ModFilter.
122   /// \param nnn The required value of the nnn field.
123   ExtendedRMFilter(bool r, uint8_t nnn) :
124     ModRMFilter(),
125     R(r),
126     NNN(nnn) {
127   }
128 
129   bool accepts(uint8_t modRM) const override {
130     return ((R && ((modRM & 0xc0) == 0xc0)) &&
131             ((modRM & 0x7) == NNN));
132   }
133 };
134 /// ExactFilter - The occasional extended opcode (such as VMCALL or MONITOR)
135 ///   requires the ModR/M byte to have a specific value.
136 class ExactFilter : public ModRMFilter {
137   void anchor() override;
138   uint8_t ModRM;
139 public:
140   /// Constructor
141   ///
142   /// \param modRM The required value of the full ModR/M byte.
143   ExactFilter(uint8_t modRM) :
144     ModRMFilter(),
145     ModRM(modRM) {
146   }
147 
148   bool accepts(uint8_t modRM) const override {
149     return (ModRM == modRM);
150   }
151 };
152 
153 } // namespace X86Disassembler
154 
155 } // namespace llvm
156 
157 #endif
158