Home | History | Annotate | Download | only in TableGen
      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   virtual void anchor();
     31 public:
     32   /// Destructor    - Override as necessary.
     33   virtual ~ModRMFilter() { }
     34 
     35   /// isDumb        - Indicates whether this filter returns the same value for
     36   ///                 any value of the ModR/M byte.
     37   ///
     38   /// @result       - True if the filter returns the same value for any ModR/M
     39   ///                 byte; false if not.
     40   virtual bool isDumb() const { return false; }
     41 
     42   /// accepts       - Indicates whether the filter accepts a particular ModR/M
     43   ///                 byte value.
     44   ///
     45   /// @result       - True if the filter accepts the ModR/M byte; false if not.
     46   virtual bool accepts(uint8_t modRM) const = 0;
     47 };
     48 
     49 /// DumbFilter - Accepts any ModR/M byte.  Used for instructions that do not
     50 ///   require a ModR/M byte or instructions where the entire ModR/M byte is used
     51 ///   for operands.
     52 class DumbFilter : public ModRMFilter {
     53   virtual void anchor();
     54 public:
     55   bool isDumb() const {
     56     return true;
     57   }
     58 
     59   bool accepts(uint8_t modRM) const {
     60     return true;
     61   }
     62 };
     63 
     64 /// ModFilter - Filters based on the mod bits [bits 7-6] of the ModR/M byte.
     65 ///   Some instructions are classified based on whether they are 11 or anything
     66 ///   else.  This filter performs that classification.
     67 class ModFilter : public ModRMFilter {
     68   virtual void anchor();
     69   bool R;
     70 public:
     71   /// Constructor
     72   ///
     73   /// @r            - True if the mod bits of the ModR/M byte must be 11; false
     74   ///                 otherwise.  The name r derives from the fact that the mod
     75   ///                 bits indicate whether the R/M bits [bits 2-0] signify a
     76   ///                 register or a memory operand.
     77   ModFilter(bool r) :
     78     ModRMFilter(),
     79     R(r) {
     80   }
     81 
     82   bool accepts(uint8_t modRM) const {
     83     if (R == ((modRM & 0xc0) == 0xc0))
     84       return true;
     85     else
     86       return false;
     87   }
     88 };
     89 
     90 /// EscapeFilter - Filters escape opcodes, which are classified in two ways.  If
     91 ///   the ModR/M byte is between 0xc0 and 0xff, then there is one slot for each
     92 ///   possible value.  Otherwise, there is one instruction for each value of the
     93 ///   nnn field [bits 5-3], known elsewhere as the reg field.
     94 class EscapeFilter : public ModRMFilter {
     95   virtual void anchor();
     96   bool C0_FF;
     97   uint8_t NNN_or_ModRM;
     98 public:
     99   /// Constructor
    100   ///
    101   /// @c0_ff        - True if the ModR/M byte must fall between 0xc0 and 0xff;
    102   ///                 false otherwise.
    103   /// @nnn_or_modRM - If c0_ff is true, the required value of the entire ModR/M
    104   ///                 byte.  If c0_ff is false, the required value of the nnn
    105   ///                 field.
    106   EscapeFilter(bool c0_ff, uint8_t nnn_or_modRM) :
    107     ModRMFilter(),
    108     C0_FF(c0_ff),
    109     NNN_or_ModRM(nnn_or_modRM) {
    110   }
    111 
    112   bool accepts(uint8_t modRM) const {
    113     if ((C0_FF && modRM >= 0xc0 && (modRM == NNN_or_ModRM)) ||
    114         (!C0_FF && modRM < 0xc0  && ((modRM & 0x38) >> 3) == NNN_or_ModRM))
    115       return true;
    116     else
    117       return false;
    118   }
    119 };
    120 
    121 /// AddRegEscapeFilter - Some escape opcodes have one of the register operands
    122 ///   added to the ModR/M byte, meaning that a range of eight ModR/M values
    123 ///   maps to a single instruction.  Such instructions require the ModR/M byte
    124 ///   to fall between 0xc0 and 0xff.
    125 class AddRegEscapeFilter : public ModRMFilter {
    126   virtual void anchor();
    127   uint8_t ModRM;
    128 public:
    129   /// Constructor
    130   ///
    131   /// @modRM        - The value of the ModR/M byte when the register operand
    132   ///                 refers to the first register in the register set.
    133   AddRegEscapeFilter(uint8_t modRM) : ModRM(modRM) {
    134   }
    135 
    136   bool accepts(uint8_t modRM) const {
    137     if (modRM >= ModRM && modRM < ModRM + 8)
    138       return true;
    139     else
    140       return false;
    141   }
    142 };
    143 
    144 /// ExtendedFilter - Extended opcodes are classified based on the value of the
    145 ///   mod field [bits 7-6] and the value of the nnn field [bits 5-3].
    146 class ExtendedFilter : public ModRMFilter {
    147   virtual void anchor();
    148   bool R;
    149   uint8_t NNN;
    150 public:
    151   /// Constructor
    152   ///
    153   /// @r            - True if the mod field must be set to 11; false otherwise.
    154   ///                 The name is explained at ModFilter.
    155   /// @nnn          - The required value of the nnn field.
    156   ExtendedFilter(bool r, uint8_t nnn) :
    157     ModRMFilter(),
    158     R(r),
    159     NNN(nnn) {
    160   }
    161 
    162   bool accepts(uint8_t modRM) const {
    163     if (((R  && ((modRM & 0xc0) == 0xc0)) ||
    164         (!R && ((modRM & 0xc0) != 0xc0))) &&
    165         (((modRM & 0x38) >> 3) == NNN))
    166       return true;
    167     else
    168       return false;
    169   }
    170 };
    171 
    172 /// ExactFilter - The occasional extended opcode (such as VMCALL or MONITOR)
    173 ///   requires the ModR/M byte to have a specific value.
    174 class ExactFilter : public ModRMFilter {
    175   virtual void anchor();
    176   uint8_t ModRM;
    177 public:
    178   /// Constructor
    179   ///
    180   /// @modRM        - The required value of the full ModR/M byte.
    181   ExactFilter(uint8_t modRM) :
    182     ModRMFilter(),
    183     ModRM(modRM) {
    184   }
    185 
    186   bool accepts(uint8_t modRM) const {
    187     if (ModRM == modRM)
    188       return true;
    189     else
    190       return false;
    191   }
    192 };
    193 
    194 } // namespace X86Disassembler
    195 
    196 } // namespace llvm
    197 
    198 #endif
    199