X86RecognizableInstr.h 14 KB

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