X86RecognizableInstr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //===- X86RecognizableInstr.h - Disassembler instruction spec ----*- 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 the interface of a single recognizable instruction.
  12. // Documentation for the disassembler emitter in general can be found in
  13. // X86DisasemblerEmitter.h.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
  17. #define LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
  18. #include "CodeGenTarget.h"
  19. #include "X86DisassemblerTables.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/Support/DataTypes.h"
  22. #include "llvm/TableGen/Record.h"
  23. namespace llvm {
  24. namespace X86Disassembler {
  25. /// RecognizableInstr - Encapsulates all information required to decode a single
  26. /// instruction, as extracted from the LLVM instruction tables. Has methods
  27. /// to interpret the information available in the LLVM tables, and to emit the
  28. /// instruction into DisassemblerTables.
  29. class RecognizableInstr {
  30. private:
  31. /// The opcode of the instruction, as used in an MCInst
  32. InstrUID UID;
  33. /// The record from the .td files corresponding to this instruction
  34. const Record* Rec;
  35. /// The OpPrefix field from the record
  36. uint8_t OpPrefix;
  37. /// The OpMap field from the record
  38. uint8_t OpMap;
  39. /// The opcode field from the record; this is the opcode used in the Intel
  40. /// encoding and therefore distinct from the UID
  41. uint8_t Opcode;
  42. /// The form field from the record
  43. uint8_t Form;
  44. // The encoding field from the record
  45. uint8_t Encoding;
  46. /// The OpSize field from the record
  47. uint8_t OpSize;
  48. /// The AdSize field from the record
  49. uint8_t AdSize;
  50. /// The hasREX_WPrefix field from the record
  51. bool HasREX_WPrefix;
  52. /// The hasVEX_4V field from the record
  53. bool HasVEX_4V;
  54. /// The hasVEX_4VOp3 field from the record
  55. bool HasVEX_4VOp3;
  56. /// The hasVEX_WPrefix field from the record
  57. bool HasVEX_WPrefix;
  58. /// Inferred from the operands; indicates whether the L bit in the VEX prefix is set
  59. bool HasVEX_LPrefix;
  60. /// The hasMemOp4Prefix field from the record
  61. bool HasMemOp4Prefix;
  62. /// The ignoreVEX_L field from the record
  63. bool IgnoresVEX_L;
  64. /// The hasEVEX_L2Prefix field from the record
  65. bool HasEVEX_L2Prefix;
  66. /// The hasEVEX_K field from the record
  67. bool HasEVEX_K;
  68. /// The hasEVEX_KZ field from the record
  69. bool HasEVEX_KZ;
  70. /// The hasEVEX_B field from the record
  71. bool HasEVEX_B;
  72. /// The isCodeGenOnly field from the record
  73. bool IsCodeGenOnly;
  74. /// The ForceDisassemble field from the record
  75. bool ForceDisassemble;
  76. // The CD8_Scale field from the record
  77. uint8_t CD8_Scale;
  78. // Whether the instruction has the predicate "In64BitMode"
  79. bool Is64Bit;
  80. // Whether the instruction has the predicate "In32BitMode"
  81. bool Is32Bit;
  82. /// The instruction name as listed in the tables
  83. std::string Name;
  84. /// The AT&T AsmString for the instruction
  85. std::string AsmString;
  86. /// Indicates whether the instruction should be emitted into the decode
  87. /// tables; regardless, it will be emitted into the instruction info table
  88. bool ShouldBeEmitted;
  89. /// The operands of the instruction, as listed in the CodeGenInstruction.
  90. /// They are not one-to-one with operands listed in the MCInst; for example,
  91. /// memory operands expand to 5 operands in the MCInst
  92. const std::vector<CGIOperandList::OperandInfo>* Operands;
  93. /// The description of the instruction that is emitted into the instruction
  94. /// info table
  95. InstructionSpecifier* Spec;
  96. /// insnContext - Returns the primary context in which the instruction is
  97. /// valid.
  98. ///
  99. /// @return - The context in which the instruction is valid.
  100. InstructionContext insnContext() const;
  101. /// typeFromString - Translates an operand type from the string provided in
  102. /// the LLVM tables to an OperandType for use in the operand specifier.
  103. ///
  104. /// @param s - The string, as extracted by calling Rec->getName()
  105. /// on a CodeGenInstruction::OperandInfo.
  106. /// @param hasREX_WPrefix - Indicates whether the instruction has a REX.W
  107. /// prefix. If it does, 32-bit register operands stay
  108. /// 32-bit regardless of the operand size.
  109. /// @param OpSize Indicates the operand size of the instruction.
  110. /// If register size does not match OpSize, then
  111. /// register sizes keep their size.
  112. /// @return - The operand's type.
  113. static OperandType typeFromString(const std::string& s,
  114. bool hasREX_WPrefix, uint8_t OpSize);
  115. /// immediateEncodingFromString - Translates an immediate encoding from the
  116. /// string provided in the LLVM tables to an OperandEncoding for use in
  117. /// the operand specifier.
  118. ///
  119. /// @param s - See typeFromString().
  120. /// @param OpSize - Indicates whether this is an OpSize16 instruction.
  121. /// If it is not, then 16-bit immediate operands stay 16-bit.
  122. /// @return - The operand's encoding.
  123. static OperandEncoding immediateEncodingFromString(const std::string &s,
  124. uint8_t OpSize);
  125. /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
  126. /// handles operands that are in the REG field of the ModR/M byte.
  127. static OperandEncoding rmRegisterEncodingFromString(const std::string &s,
  128. uint8_t OpSize);
  129. /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
  130. /// handles operands that are in the REG field of the ModR/M byte.
  131. static OperandEncoding roRegisterEncodingFromString(const std::string &s,
  132. uint8_t OpSize);
  133. static OperandEncoding memoryEncodingFromString(const std::string &s,
  134. uint8_t OpSize);
  135. static OperandEncoding relocationEncodingFromString(const std::string &s,
  136. uint8_t OpSize);
  137. static OperandEncoding opcodeModifierEncodingFromString(const std::string &s,
  138. uint8_t OpSize);
  139. static OperandEncoding vvvvRegisterEncodingFromString(const std::string &s,
  140. uint8_t OpSize);
  141. static OperandEncoding writemaskRegisterEncodingFromString(const std::string &s,
  142. uint8_t OpSize);
  143. /// \brief Adjust the encoding type for an operand based on the instruction.
  144. void adjustOperandEncoding(OperandEncoding &encoding);
  145. /// handleOperand - Converts a single operand from the LLVM table format to
  146. /// the emitted table format, handling any duplicate operands it encounters
  147. /// and then one non-duplicate.
  148. ///
  149. /// @param optional - Determines whether to assert that the
  150. /// operand exists.
  151. /// @param operandIndex - The index into the generated operand table.
  152. /// Incremented by this function one or more
  153. /// times to reflect possible duplicate
  154. /// operands).
  155. /// @param physicalOperandIndex - The index of the current operand into the
  156. /// set of non-duplicate ('physical') operands.
  157. /// Incremented by this function once.
  158. /// @param numPhysicalOperands - The number of non-duplicate operands in the
  159. /// instructions.
  160. /// @param operandMapping - The operand mapping, which has an entry for
  161. /// each operand that indicates whether it is a
  162. /// duplicate, and of what.
  163. void handleOperand(bool optional,
  164. unsigned &operandIndex,
  165. unsigned &physicalOperandIndex,
  166. unsigned &numPhysicalOperands,
  167. const unsigned *operandMapping,
  168. OperandEncoding (*encodingFromString)
  169. (const std::string&,
  170. uint8_t OpSize));
  171. /// shouldBeEmitted - Returns the shouldBeEmitted field. Although filter()
  172. /// filters out many instructions, at various points in decoding we
  173. /// determine that the instruction should not actually be decodable. In
  174. /// particular, MMX MOV instructions aren't emitted, but they're only
  175. /// identified during operand parsing.
  176. ///
  177. /// @return - true if at this point we believe the instruction should be
  178. /// emitted; false if not. This will return false if filter() returns false
  179. /// once emitInstructionSpecifier() has been called.
  180. bool shouldBeEmitted() const {
  181. return ShouldBeEmitted;
  182. }
  183. /// emitInstructionSpecifier - Loads the instruction specifier for the current
  184. /// instruction into a DisassemblerTables.
  185. ///
  186. void emitInstructionSpecifier();
  187. /// emitDecodePath - Populates the proper fields in the decode tables
  188. /// corresponding to the decode paths for this instruction.
  189. ///
  190. /// \param tables The DisassemblerTables to populate with the decode
  191. /// decode information for the current instruction.
  192. void emitDecodePath(DisassemblerTables &tables) const;
  193. /// Constructor - Initializes a RecognizableInstr with the appropriate fields
  194. /// from a CodeGenInstruction.
  195. ///
  196. /// \param tables The DisassemblerTables that the specifier will be added to.
  197. /// \param insn The CodeGenInstruction to extract information from.
  198. /// \param uid The unique ID of the current instruction.
  199. RecognizableInstr(DisassemblerTables &tables,
  200. const CodeGenInstruction &insn,
  201. InstrUID uid);
  202. public:
  203. /// processInstr - Accepts a CodeGenInstruction and loads decode information
  204. /// for it into a DisassemblerTables if appropriate.
  205. ///
  206. /// \param tables The DiassemblerTables to be populated with decode
  207. /// information.
  208. /// \param insn The CodeGenInstruction to be used as a source for this
  209. /// information.
  210. /// \param uid The unique ID of the instruction.
  211. static void processInstr(DisassemblerTables &tables,
  212. const CodeGenInstruction &insn,
  213. InstrUID uid);
  214. };
  215. } // namespace X86Disassembler
  216. } // namespace llvm
  217. #endif