X86RecognizableInstr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //===- X86RecognizableInstr.h - Disassembler instruction spec ----*- 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 the interface of a single recognizable instruction.
  11. // Documentation for the disassembler emitter in general can be found in
  12. // X86DisassemblerEmitter.h.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
  16. #define LLVM_UTILS_TABLEGEN_X86RECOGNIZABLEINSTR_H
  17. #include "CodeGenTarget.h"
  18. #include "X86DisassemblerTables.h"
  19. #include "llvm/Support/DataTypes.h"
  20. #include "llvm/TableGen/Record.h"
  21. namespace llvm {
  22. #define X86_INSTR_MRM_MAPPING \
  23. MAP(C0, 64) \
  24. MAP(C1, 65) \
  25. MAP(C2, 66) \
  26. MAP(C3, 67) \
  27. MAP(C4, 68) \
  28. MAP(C5, 69) \
  29. MAP(C6, 70) \
  30. MAP(C7, 71) \
  31. MAP(C8, 72) \
  32. MAP(C9, 73) \
  33. MAP(CA, 74) \
  34. MAP(CB, 75) \
  35. MAP(CC, 76) \
  36. MAP(CD, 77) \
  37. MAP(CE, 78) \
  38. MAP(CF, 79) \
  39. MAP(D0, 80) \
  40. MAP(D1, 81) \
  41. MAP(D2, 82) \
  42. MAP(D3, 83) \
  43. MAP(D4, 84) \
  44. MAP(D5, 85) \
  45. MAP(D6, 86) \
  46. MAP(D7, 87) \
  47. MAP(D8, 88) \
  48. MAP(D9, 89) \
  49. MAP(DA, 90) \
  50. MAP(DB, 91) \
  51. MAP(DC, 92) \
  52. MAP(DD, 93) \
  53. MAP(DE, 94) \
  54. MAP(DF, 95) \
  55. MAP(E0, 96) \
  56. MAP(E1, 97) \
  57. MAP(E2, 98) \
  58. MAP(E3, 99) \
  59. MAP(E4, 100) \
  60. MAP(E5, 101) \
  61. MAP(E6, 102) \
  62. MAP(E7, 103) \
  63. MAP(E8, 104) \
  64. MAP(E9, 105) \
  65. MAP(EA, 106) \
  66. MAP(EB, 107) \
  67. MAP(EC, 108) \
  68. MAP(ED, 109) \
  69. MAP(EE, 110) \
  70. MAP(EF, 111) \
  71. MAP(F0, 112) \
  72. MAP(F1, 113) \
  73. MAP(F2, 114) \
  74. MAP(F3, 115) \
  75. MAP(F4, 116) \
  76. MAP(F5, 117) \
  77. MAP(F6, 118) \
  78. MAP(F7, 119) \
  79. MAP(F8, 120) \
  80. MAP(F9, 121) \
  81. MAP(FA, 122) \
  82. MAP(FB, 123) \
  83. MAP(FC, 124) \
  84. MAP(FD, 125) \
  85. MAP(FE, 126) \
  86. MAP(FF, 127)
  87. // A clone of X86 since we can't depend on something that is generated.
  88. namespace X86Local {
  89. enum {
  90. Pseudo = 0,
  91. RawFrm = 1,
  92. AddRegFrm = 2,
  93. RawFrmMemOffs = 3,
  94. RawFrmSrc = 4,
  95. RawFrmDst = 5,
  96. RawFrmDstSrc = 6,
  97. RawFrmImm8 = 7,
  98. RawFrmImm16 = 8,
  99. AddCCFrm = 9,
  100. PrefixByte = 10,
  101. MRMr0 = 21,
  102. MRMSrcMemFSIB = 22,
  103. MRMDestMemFSIB = 23,
  104. MRMDestMem = 24,
  105. MRMSrcMem = 25,
  106. MRMSrcMem4VOp3 = 26,
  107. MRMSrcMemOp4 = 27,
  108. MRMSrcMemCC = 28,
  109. MRMXmCC = 30, MRMXm = 31,
  110. MRM0m = 32, MRM1m = 33, MRM2m = 34, MRM3m = 35,
  111. MRM4m = 36, MRM5m = 37, MRM6m = 38, MRM7m = 39,
  112. MRMDestReg = 40,
  113. MRMSrcReg = 41,
  114. MRMSrcReg4VOp3 = 42,
  115. MRMSrcRegOp4 = 43,
  116. MRMSrcRegCC = 44,
  117. MRMXrCC = 46, MRMXr = 47,
  118. MRM0r = 48, MRM1r = 49, MRM2r = 50, MRM3r = 51,
  119. MRM4r = 52, MRM5r = 53, MRM6r = 54, MRM7r = 55,
  120. MRM0X = 56, MRM1X = 57, MRM2X = 58, MRM3X = 59,
  121. MRM4X = 60, MRM5X = 61, MRM6X = 62, MRM7X = 63,
  122. #define MAP(from, to) MRM_##from = to,
  123. X86_INSTR_MRM_MAPPING
  124. #undef MAP
  125. };
  126. enum {
  127. OB = 0, TB = 1, T8 = 2, TA = 3, XOP8 = 4, XOP9 = 5, XOPA = 6, ThreeDNow = 7
  128. };
  129. enum {
  130. PD = 1, XS = 2, XD = 3, PS = 4
  131. };
  132. enum {
  133. VEX = 1, XOP = 2, EVEX = 3
  134. };
  135. enum {
  136. OpSize16 = 1, OpSize32 = 2
  137. };
  138. enum {
  139. AdSize16 = 1, AdSize32 = 2, AdSize64 = 3
  140. };
  141. }
  142. namespace X86Disassembler {
  143. /// RecognizableInstr - Encapsulates all information required to decode a single
  144. /// instruction, as extracted from the LLVM instruction tables. Has methods
  145. /// to interpret the information available in the LLVM tables, and to emit the
  146. /// instruction into DisassemblerTables.
  147. class RecognizableInstr {
  148. private:
  149. /// The opcode of the instruction, as used in an MCInst
  150. InstrUID UID;
  151. /// The record from the .td files corresponding to this instruction
  152. const Record* Rec;
  153. /// The OpPrefix field from the record
  154. uint8_t OpPrefix;
  155. /// The OpMap field from the record
  156. uint8_t OpMap;
  157. /// The opcode field from the record; this is the opcode used in the Intel
  158. /// encoding and therefore distinct from the UID
  159. uint8_t Opcode;
  160. /// The form field from the record
  161. uint8_t Form;
  162. // The encoding field from the record
  163. uint8_t Encoding;
  164. /// The OpSize field from the record
  165. uint8_t OpSize;
  166. /// The AdSize field from the record
  167. uint8_t AdSize;
  168. /// The hasREX_WPrefix field from the record
  169. bool HasREX_WPrefix;
  170. /// The hasVEX_4V field from the record
  171. bool HasVEX_4V;
  172. /// The HasVEX_WPrefix field from the record
  173. bool HasVEX_W;
  174. /// The IgnoresVEX_W field from the record
  175. bool IgnoresVEX_W;
  176. /// Inferred from the operands; indicates whether the L bit in the VEX prefix is set
  177. bool HasVEX_LPrefix;
  178. /// The ignoreVEX_L field from the record
  179. bool IgnoresVEX_L;
  180. /// The hasEVEX_L2Prefix field from the record
  181. bool HasEVEX_L2Prefix;
  182. /// The hasEVEX_K field from the record
  183. bool HasEVEX_K;
  184. /// The hasEVEX_KZ field from the record
  185. bool HasEVEX_KZ;
  186. /// The hasEVEX_B field from the record
  187. bool HasEVEX_B;
  188. /// Indicates that the instruction uses the L and L' fields for RC.
  189. bool EncodeRC;
  190. /// The isCodeGenOnly field from the record
  191. bool IsCodeGenOnly;
  192. /// The ForceDisassemble field from the record
  193. bool ForceDisassemble;
  194. // The CD8_Scale field from the record
  195. uint8_t CD8_Scale;
  196. // Whether the instruction has the predicate "In64BitMode"
  197. bool Is64Bit;
  198. // Whether the instruction has the predicate "In32BitMode"
  199. bool Is32Bit;
  200. /// The instruction name as listed in the tables
  201. std::string Name;
  202. /// Indicates whether the instruction should be emitted into the decode
  203. /// tables; regardless, it will be emitted into the instruction info table
  204. bool ShouldBeEmitted;
  205. /// The operands of the instruction, as listed in the CodeGenInstruction.
  206. /// They are not one-to-one with operands listed in the MCInst; for example,
  207. /// memory operands expand to 5 operands in the MCInst
  208. const std::vector<CGIOperandList::OperandInfo>* Operands;
  209. /// The description of the instruction that is emitted into the instruction
  210. /// info table
  211. InstructionSpecifier* Spec;
  212. /// insnContext - Returns the primary context in which the instruction is
  213. /// valid.
  214. ///
  215. /// @return - The context in which the instruction is valid.
  216. InstructionContext insnContext() const;
  217. /// typeFromString - Translates an operand type from the string provided in
  218. /// the LLVM tables to an OperandType for use in the operand specifier.
  219. ///
  220. /// @param s - The string, as extracted by calling Rec->getName()
  221. /// on a CodeGenInstruction::OperandInfo.
  222. /// @param hasREX_WPrefix - Indicates whether the instruction has a REX.W
  223. /// prefix. If it does, 32-bit register operands stay
  224. /// 32-bit regardless of the operand size.
  225. /// @param OpSize Indicates the operand size of the instruction.
  226. /// If register size does not match OpSize, then
  227. /// register sizes keep their size.
  228. /// @return - The operand's type.
  229. static OperandType typeFromString(const std::string& s,
  230. bool hasREX_WPrefix, uint8_t OpSize);
  231. /// immediateEncodingFromString - Translates an immediate encoding from the
  232. /// string provided in the LLVM tables to an OperandEncoding for use in
  233. /// the operand specifier.
  234. ///
  235. /// @param s - See typeFromString().
  236. /// @param OpSize - Indicates whether this is an OpSize16 instruction.
  237. /// If it is not, then 16-bit immediate operands stay 16-bit.
  238. /// @return - The operand's encoding.
  239. static OperandEncoding immediateEncodingFromString(const std::string &s,
  240. uint8_t OpSize);
  241. /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
  242. /// handles operands that are in the REG field of the ModR/M byte.
  243. static OperandEncoding rmRegisterEncodingFromString(const std::string &s,
  244. uint8_t OpSize);
  245. /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
  246. /// handles operands that are in the REG field of the ModR/M byte.
  247. static OperandEncoding roRegisterEncodingFromString(const std::string &s,
  248. uint8_t OpSize);
  249. static OperandEncoding memoryEncodingFromString(const std::string &s,
  250. uint8_t OpSize);
  251. static OperandEncoding relocationEncodingFromString(const std::string &s,
  252. uint8_t OpSize);
  253. static OperandEncoding opcodeModifierEncodingFromString(const std::string &s,
  254. uint8_t OpSize);
  255. static OperandEncoding vvvvRegisterEncodingFromString(const std::string &s,
  256. uint8_t OpSize);
  257. static OperandEncoding writemaskRegisterEncodingFromString(const std::string &s,
  258. uint8_t OpSize);
  259. /// Adjust the encoding type for an operand based on the instruction.
  260. void adjustOperandEncoding(OperandEncoding &encoding);
  261. /// handleOperand - Converts a single operand from the LLVM table format to
  262. /// the emitted table format, handling any duplicate operands it encounters
  263. /// and then one non-duplicate.
  264. ///
  265. /// @param optional - Determines whether to assert that the
  266. /// operand exists.
  267. /// @param operandIndex - The index into the generated operand table.
  268. /// Incremented by this function one or more
  269. /// times to reflect possible duplicate
  270. /// operands).
  271. /// @param physicalOperandIndex - The index of the current operand into the
  272. /// set of non-duplicate ('physical') operands.
  273. /// Incremented by this function once.
  274. /// @param numPhysicalOperands - The number of non-duplicate operands in the
  275. /// instructions.
  276. /// @param operandMapping - The operand mapping, which has an entry for
  277. /// each operand that indicates whether it is a
  278. /// duplicate, and of what.
  279. void handleOperand(bool optional,
  280. unsigned &operandIndex,
  281. unsigned &physicalOperandIndex,
  282. unsigned numPhysicalOperands,
  283. const unsigned *operandMapping,
  284. OperandEncoding (*encodingFromString)
  285. (const std::string&,
  286. uint8_t OpSize));
  287. /// shouldBeEmitted - Returns the shouldBeEmitted field. Although filter()
  288. /// filters out many instructions, at various points in decoding we
  289. /// determine that the instruction should not actually be decodable. In
  290. /// particular, MMX MOV instructions aren't emitted, but they're only
  291. /// identified during operand parsing.
  292. ///
  293. /// @return - true if at this point we believe the instruction should be
  294. /// emitted; false if not. This will return false if filter() returns false
  295. /// once emitInstructionSpecifier() has been called.
  296. bool shouldBeEmitted() const {
  297. return ShouldBeEmitted;
  298. }
  299. /// emitInstructionSpecifier - Loads the instruction specifier for the current
  300. /// instruction into a DisassemblerTables.
  301. ///
  302. void emitInstructionSpecifier();
  303. /// emitDecodePath - Populates the proper fields in the decode tables
  304. /// corresponding to the decode paths for this instruction.
  305. ///
  306. /// \param tables The DisassemblerTables to populate with the decode
  307. /// decode information for the current instruction.
  308. void emitDecodePath(DisassemblerTables &tables) const;
  309. /// Constructor - Initializes a RecognizableInstr with the appropriate fields
  310. /// from a CodeGenInstruction.
  311. ///
  312. /// \param tables The DisassemblerTables that the specifier will be added to.
  313. /// \param insn The CodeGenInstruction to extract information from.
  314. /// \param uid The unique ID of the current instruction.
  315. RecognizableInstr(DisassemblerTables &tables,
  316. const CodeGenInstruction &insn,
  317. InstrUID uid);
  318. public:
  319. /// processInstr - Accepts a CodeGenInstruction and loads decode information
  320. /// for it into a DisassemblerTables if appropriate.
  321. ///
  322. /// \param tables The DiassemblerTables to be populated with decode
  323. /// information.
  324. /// \param insn The CodeGenInstruction to be used as a source for this
  325. /// information.
  326. /// \param uid The unique ID of the instruction.
  327. static void processInstr(DisassemblerTables &tables,
  328. const CodeGenInstruction &insn,
  329. InstrUID uid);
  330. };
  331. } // namespace X86Disassembler
  332. } // namespace llvm
  333. #endif