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 <cstdint> 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 31 public: 32 /// Destructor - Override as necessary. ~ModRMFilter()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. isDumb()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 void anchor() override; 54 55 public: isDumb()56 bool isDumb() const override { return true; } 57 accepts(uint8_t modRM)58 bool accepts(uint8_t modRM) const override { return true; } 59 }; 60 61 /// ModFilter - Filters based on the mod bits [bits 7-6] of the ModR/M byte. 62 /// Some instructions are classified based on whether they are 11 or anything 63 /// else. This filter performs that classification. 64 class ModFilter : public ModRMFilter { 65 void anchor() override; 66 bool R; 67 68 public: 69 /// Constructor 70 /// 71 /// \param 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) : R(r) {} 76 accepts(uint8_t modRM)77 bool accepts(uint8_t modRM) const override { 78 return (R == ((modRM & 0xc0) == 0xc0)); 79 } 80 }; 81 82 /// ExtendedFilter - Extended opcodes are classified based on the value of the 83 /// mod field [bits 7-6] and the value of the nnn field [bits 5-3]. 84 class ExtendedFilter : public ModRMFilter { 85 void anchor() override; 86 bool R; 87 uint8_t NNN; 88 89 public: 90 /// Constructor 91 /// 92 /// \param r True if the mod field must be set to 11; false otherwise. 93 /// The name is explained at ModFilter. 94 /// \param nnn The required value of the nnn field. ExtendedFilter(bool r,uint8_t nnn)95 ExtendedFilter(bool r, uint8_t nnn) : R(r), NNN(nnn) {} 96 accepts(uint8_t modRM)97 bool accepts(uint8_t modRM) const override { 98 return ( 99 ((R && ((modRM & 0xc0) == 0xc0)) || (!R && ((modRM & 0xc0) != 0xc0))) && 100 (((modRM & 0x38) >> 3) == NNN)); 101 } 102 }; 103 104 /// ExtendedRMFilter - Extended opcodes are classified based on the value of the 105 /// mod field [bits 7-6] and the value of the nnn field [bits 2-0]. 106 class ExtendedRMFilter : public ModRMFilter { 107 void anchor() override; 108 bool R; 109 uint8_t NNN; 110 111 public: 112 /// Constructor 113 /// 114 /// \param r True if the mod field must be set to 11; false otherwise. 115 /// The name is explained at ModFilter. 116 /// \param nnn The required value of the nnn field. ExtendedRMFilter(bool r,uint8_t nnn)117 ExtendedRMFilter(bool r, uint8_t nnn) : R(r), NNN(nnn) {} 118 accepts(uint8_t modRM)119 bool accepts(uint8_t modRM) const override { 120 return ((R && ((modRM & 0xc0) == 0xc0)) && ((modRM & 0x7) == NNN)); 121 } 122 }; 123 /// ExactFilter - The occasional extended opcode (such as VMCALL or MONITOR) 124 /// requires the ModR/M byte to have a specific value. 125 class ExactFilter : public ModRMFilter { 126 void anchor() override; 127 uint8_t ModRM; 128 129 public: 130 /// Constructor 131 /// 132 /// \param modRM The required value of the full ModR/M byte. ExactFilter(uint8_t modRM)133 ExactFilter(uint8_t modRM) : ModRM(modRM) {} 134 accepts(uint8_t modRM)135 bool accepts(uint8_t modRM) const override { return (ModRM == modRM); } 136 }; 137 138 } // namespace X86Disassembler 139 140 } // namespace llvm 141 142 #endif 143