• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- X86ModRMFilters.h - Disassembler ModR/M filterss ---------*- 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 ModR/M filters that determine which values of the ModR/M byte
12 //  are valid for a partiuclar instruction.
13 // Documentation for the disassembler emitter in general can be found in
14 //  X86DisasemblerEmitter.h.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef X86MODRMFILTERS_H
19 #define X86MODRMFILTERS_H
20 
21 #include "llvm/Support/DataTypes.h"
22 
23 namespace llvm {
24 
25 namespace X86Disassembler {
26 
27 /// ModRMFilter - Abstract base class for clases that recognize patterns in
28 ///   ModR/M bytes.
29 class ModRMFilter {
30 public:
31   /// Destructor    - Override as necessary.
~ModRMFilter()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.
isDumb()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 public:
isDumb()53   bool isDumb() const {
54     return true;
55   }
56 
accepts(uint8_t modRM)57   bool accepts(uint8_t modRM) const {
58     return true;
59   }
60 };
61 
62 /// ModFilter - Filters based on the mod bits [bits 7-6] of the ModR/M byte.
63 ///   Some instructions are classified based on whether they are 11 or anything
64 ///   else.  This filter performs that classification.
65 class ModFilter : public ModRMFilter {
66 private:
67   bool R;
68 public:
69   /// Constructor
70   ///
71   /// @r            - True if the mod bits of the ModR/M byte must be 11; false
72   ///                 otherwise.  The name r derives from the fact that the mod
73   ///                 bits indicate whether the R/M bits [bits 2-0] signify a
74   ///                 register or a memory operand.
ModFilter(bool r)75   ModFilter(bool r) :
76     ModRMFilter(),
77     R(r) {
78   }
79 
accepts(uint8_t modRM)80   bool accepts(uint8_t modRM) const {
81     if (R == ((modRM & 0xc0) == 0xc0))
82       return true;
83     else
84       return false;
85   }
86 };
87 
88 /// EscapeFilter - Filters escape opcodes, which are classified in two ways.  If
89 ///   the ModR/M byte is between 0xc0 and 0xff, then there is one slot for each
90 ///   possible value.  Otherwise, there is one instruction for each value of the
91 ///   nnn field [bits 5-3], known elsewhere as the reg field.
92 class EscapeFilter : public ModRMFilter {
93 private:
94   bool C0_FF;
95   uint8_t NNN_or_ModRM;
96 public:
97   /// Constructor
98   ///
99   /// @c0_ff        - True if the ModR/M byte must fall between 0xc0 and 0xff;
100   ///                 false otherwise.
101   /// @nnn_or_modRM - If c0_ff is true, the required value of the entire ModR/M
102   ///                 byte.  If c0_ff is false, the required value of the nnn
103   ///                 field.
EscapeFilter(bool c0_ff,uint8_t nnn_or_modRM)104   EscapeFilter(bool c0_ff, uint8_t nnn_or_modRM) :
105     ModRMFilter(),
106     C0_FF(c0_ff),
107     NNN_or_ModRM(nnn_or_modRM) {
108   }
109 
accepts(uint8_t modRM)110   bool accepts(uint8_t modRM) const {
111     if ((C0_FF && modRM >= 0xc0 && (modRM == NNN_or_ModRM)) ||
112         (!C0_FF && modRM < 0xc0  && ((modRM & 0x38) >> 3) == NNN_or_ModRM))
113       return true;
114     else
115       return false;
116   }
117 };
118 
119 /// AddRegEscapeFilter - Some escape opcodes have one of the register operands
120 ///   added to the ModR/M byte, meaning that a range of eight ModR/M values
121 ///   maps to a single instruction.  Such instructions require the ModR/M byte
122 ///   to fall between 0xc0 and 0xff.
123 class AddRegEscapeFilter : public ModRMFilter {
124 private:
125   uint8_t ModRM;
126 public:
127   /// Constructor
128   ///
129   /// @modRM        - The value of the ModR/M byte when the register operand
130   ///                 refers to the first register in the register set.
AddRegEscapeFilter(uint8_t modRM)131   AddRegEscapeFilter(uint8_t modRM) : ModRM(modRM) {
132   }
133 
accepts(uint8_t modRM)134   bool accepts(uint8_t modRM) const {
135     if (modRM >= ModRM && modRM < ModRM + 8)
136       return true;
137     else
138       return false;
139   }
140 };
141 
142 /// ExtendedFilter - Extended opcodes are classified based on the value of the
143 ///   mod field [bits 7-6] and the value of the nnn field [bits 5-3].
144 class ExtendedFilter : public ModRMFilter {
145 private:
146   bool R;
147   uint8_t NNN;
148 public:
149   /// Constructor
150   ///
151   /// @r            - True if the mod field must be set to 11; false otherwise.
152   ///                 The name is explained at ModFilter.
153   /// @nnn          - The required value of the nnn field.
ExtendedFilter(bool r,uint8_t nnn)154   ExtendedFilter(bool r, uint8_t nnn) :
155     ModRMFilter(),
156     R(r),
157     NNN(nnn) {
158   }
159 
accepts(uint8_t modRM)160   bool accepts(uint8_t modRM) const {
161     if (((R  && ((modRM & 0xc0) == 0xc0)) ||
162         (!R && ((modRM & 0xc0) != 0xc0))) &&
163         (((modRM & 0x38) >> 3) == NNN))
164       return true;
165     else
166       return false;
167   }
168 };
169 
170 /// ExactFilter - The occasional extended opcode (such as VMCALL or MONITOR)
171 ///   requires the ModR/M byte to have a specific value.
172 class ExactFilter : public ModRMFilter
173 {
174 private:
175   uint8_t ModRM;
176 public:
177   /// Constructor
178   ///
179   /// @modRM        - The required value of the full ModR/M byte.
ExactFilter(uint8_t modRM)180   ExactFilter(uint8_t modRM) :
181     ModRMFilter(),
182     ModRM(modRM) {
183   }
184 
accepts(uint8_t modRM)185   bool accepts(uint8_t modRM) const {
186     if (ModRM == modRM)
187       return true;
188     else
189       return false;
190   }
191 };
192 
193 } // namespace X86Disassembler
194 
195 } // namespace llvm
196 
197 #endif
198