X86RecognizableInstr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. MRMDestMem4VOp3CC = 20,
  103. MRMr0 = 21,
  104. MRMSrcMemFSIB = 22,
  105. MRMDestMemFSIB = 23,
  106. MRMDestMem = 24,
  107. MRMSrcMem = 25,
  108. MRMSrcMem4VOp3 = 26,
  109. MRMSrcMemOp4 = 27,
  110. MRMSrcMemCC = 28,
  111. MRMXmCC = 30, MRMXm = 31,
  112. MRM0m = 32, MRM1m = 33, MRM2m = 34, MRM3m = 35,
  113. MRM4m = 36, MRM5m = 37, MRM6m = 38, MRM7m = 39,
  114. MRMDestReg = 40,
  115. MRMSrcReg = 41,
  116. MRMSrcReg4VOp3 = 42,
  117. MRMSrcRegOp4 = 43,
  118. MRMSrcRegCC = 44,
  119. MRMXrCC = 46, MRMXr = 47,
  120. MRM0r = 48, MRM1r = 49, MRM2r = 50, MRM3r = 51,
  121. MRM4r = 52, MRM5r = 53, MRM6r = 54, MRM7r = 55,
  122. MRM0X = 56, MRM1X = 57, MRM2X = 58, MRM3X = 59,
  123. MRM4X = 60, MRM5X = 61, MRM6X = 62, MRM7X = 63,
  124. #define MAP(from, to) MRM_##from = to,
  125. X86_INSTR_MRM_MAPPING
  126. #undef MAP
  127. };
  128. enum {
  129. OB = 0, TB = 1, T8 = 2, TA = 3, XOP8 = 4, XOP9 = 5, XOPA = 6, ThreeDNow = 7,
  130. T_MAP5 = 8, T_MAP6 = 9
  131. };
  132. enum {
  133. PD = 1, XS = 2, XD = 3, PS = 4
  134. };
  135. enum {
  136. VEX = 1, XOP = 2, EVEX = 3
  137. };
  138. enum {
  139. OpSize16 = 1, OpSize32 = 2
  140. };
  141. enum {
  142. AdSize16 = 1, AdSize32 = 2, AdSize64 = 3
  143. };
  144. }
  145. namespace X86Disassembler {
  146. class DisassemblerTables;
  147. /// Extract common fields of a single X86 instruction from a CodeGenInstruction
  148. struct RecognizableInstrBase {
  149. /// The OpPrefix field from the record
  150. uint8_t OpPrefix;
  151. /// The OpMap field from the record
  152. uint8_t OpMap;
  153. /// The opcode field from the record; this is the opcode used in the Intel
  154. /// encoding and therefore distinct from the UID
  155. uint8_t Opcode;
  156. /// The form field from the record
  157. uint8_t Form;
  158. // The encoding field from the record
  159. uint8_t Encoding;
  160. /// The OpSize field from the record
  161. uint8_t OpSize;
  162. /// The AdSize field from the record
  163. uint8_t AdSize;
  164. /// The hasREX_W field from the record
  165. bool HasREX_W;
  166. /// The hasVEX_4V field from the record
  167. bool HasVEX_4V;
  168. /// The HasVEX_WPrefix field from the record
  169. bool HasVEX_W;
  170. /// The IgnoresVEX_W field from the record
  171. bool IgnoresVEX_W;
  172. /// The hasVEX_L field from the record
  173. bool HasVEX_L;
  174. /// The ignoreVEX_L field from the record
  175. bool IgnoresVEX_L;
  176. /// The hasEVEX_L2Prefix field from the record
  177. bool HasEVEX_L2;
  178. /// The hasEVEX_K field from the record
  179. bool HasEVEX_K;
  180. /// The hasEVEX_KZ field from the record
  181. bool HasEVEX_KZ;
  182. /// The hasEVEX_B field from the record
  183. bool HasEVEX_B;
  184. /// Indicates that the instruction uses the L and L' fields for RC.
  185. bool EncodeRC;
  186. /// The isCodeGenOnly field from the record
  187. bool IsCodeGenOnly;
  188. /// The isAsmParserOnly field from the record
  189. bool IsAsmParserOnly;
  190. /// The ForceDisassemble field from the record
  191. bool ForceDisassemble;
  192. // The CD8_Scale field from the record
  193. uint8_t CD8_Scale;
  194. /// \param insn The CodeGenInstruction to extract information from.
  195. RecognizableInstrBase(const CodeGenInstruction &insn);
  196. /// \returns true if this instruction should be emitted
  197. bool shouldBeEmitted() const;
  198. };
  199. /// RecognizableInstr - Encapsulates all information required to decode a single
  200. /// instruction, as extracted from the LLVM instruction tables. Has methods
  201. /// to interpret the information available in the LLVM tables, and to emit the
  202. /// instruction into DisassemblerTables.
  203. class RecognizableInstr : public RecognizableInstrBase {
  204. private:
  205. /// The record from the .td files corresponding to this instruction
  206. const Record* Rec;
  207. /// The instruction name as listed in the tables
  208. std::string Name;
  209. // Whether the instruction has the predicate "In32BitMode"
  210. bool Is32Bit;
  211. // Whether the instruction has the predicate "In64BitMode"
  212. bool Is64Bit;
  213. /// The operands of the instruction, as listed in the CodeGenInstruction.
  214. /// They are not one-to-one with operands listed in the MCInst; for example,
  215. /// memory operands expand to 5 operands in the MCInst
  216. const std::vector<CGIOperandList::OperandInfo>* Operands;
  217. /// The opcode of the instruction, as used in an MCInst
  218. InstrUID UID;
  219. /// The description of the instruction that is emitted into the instruction
  220. /// info table
  221. InstructionSpecifier* Spec;
  222. /// insnContext - Returns the primary context in which the instruction is
  223. /// valid.
  224. ///
  225. /// @return - The context in which the instruction is valid.
  226. InstructionContext insnContext() const;
  227. /// typeFromString - Translates an operand type from the string provided in
  228. /// the LLVM tables to an OperandType for use in the operand specifier.
  229. ///
  230. /// @param s - The string, as extracted by calling Rec->getName()
  231. /// on a CodeGenInstruction::OperandInfo.
  232. /// @param hasREX_W - Indicates whether the instruction has a REX.W
  233. /// prefix. If it does, 32-bit register operands stay
  234. /// 32-bit regardless of the operand size.
  235. /// @param OpSize Indicates the operand size of the instruction.
  236. /// If register size does not match OpSize, then
  237. /// register sizes keep their size.
  238. /// @return - The operand's type.
  239. static OperandType typeFromString(const std::string& s,
  240. bool hasREX_W, uint8_t OpSize);
  241. /// immediateEncodingFromString - Translates an immediate encoding from the
  242. /// string provided in the LLVM tables to an OperandEncoding for use in
  243. /// the operand specifier.
  244. ///
  245. /// @param s - See typeFromString().
  246. /// @param OpSize - Indicates whether this is an OpSize16 instruction.
  247. /// If it is not, then 16-bit immediate operands stay 16-bit.
  248. /// @return - The operand's encoding.
  249. static OperandEncoding immediateEncodingFromString(const std::string &s,
  250. uint8_t OpSize);
  251. /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
  252. /// handles operands that are in the REG field of the ModR/M byte.
  253. static OperandEncoding rmRegisterEncodingFromString(const std::string &s,
  254. uint8_t OpSize);
  255. /// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
  256. /// handles operands that are in the REG field of the ModR/M byte.
  257. static OperandEncoding roRegisterEncodingFromString(const std::string &s,
  258. uint8_t OpSize);
  259. static OperandEncoding memoryEncodingFromString(const std::string &s,
  260. uint8_t OpSize);
  261. static OperandEncoding relocationEncodingFromString(const std::string &s,
  262. uint8_t OpSize);
  263. static OperandEncoding opcodeModifierEncodingFromString(const std::string &s,
  264. uint8_t OpSize);
  265. static OperandEncoding vvvvRegisterEncodingFromString(const std::string &s,
  266. uint8_t OpSize);
  267. static OperandEncoding writemaskRegisterEncodingFromString(const std::string &s,
  268. uint8_t OpSize);
  269. /// Adjust the encoding type for an operand based on the instruction.
  270. void adjustOperandEncoding(OperandEncoding &encoding);
  271. /// handleOperand - Converts a single operand from the LLVM table format to
  272. /// the emitted table format, handling any duplicate operands it encounters
  273. /// and then one non-duplicate.
  274. ///
  275. /// @param optional - Determines whether to assert that the
  276. /// operand exists.
  277. /// @param operandIndex - The index into the generated operand table.
  278. /// Incremented by this function one or more
  279. /// times to reflect possible duplicate
  280. /// operands).
  281. /// @param physicalOperandIndex - The index of the current operand into the
  282. /// set of non-duplicate ('physical') operands.
  283. /// Incremented by this function once.
  284. /// @param numPhysicalOperands - The number of non-duplicate operands in the
  285. /// instructions.
  286. /// @param operandMapping - The operand mapping, which has an entry for
  287. /// each operand that indicates whether it is a
  288. /// duplicate, and of what.
  289. void handleOperand(bool optional,
  290. unsigned &operandIndex,
  291. unsigned &physicalOperandIndex,
  292. unsigned numPhysicalOperands,
  293. const unsigned *operandMapping,
  294. OperandEncoding (*encodingFromString)
  295. (const std::string&,
  296. uint8_t OpSize));
  297. /// emitInstructionSpecifier - Loads the instruction specifier for the current
  298. /// instruction into a DisassemblerTables.
  299. ///
  300. void emitInstructionSpecifier();
  301. /// emitDecodePath - Populates the proper fields in the decode tables
  302. /// corresponding to the decode paths for this instruction.
  303. ///
  304. /// \param tables The DisassemblerTables to populate with the decode
  305. /// decode information for the current instruction.
  306. void emitDecodePath(DisassemblerTables &tables) const;
  307. public:
  308. /// Constructor - Initializes a RecognizableInstr with the appropriate fields
  309. /// from a CodeGenInstruction.
  310. ///
  311. /// \param tables The DisassemblerTables that the specifier will be added to.
  312. /// \param insn The CodeGenInstruction to extract information from.
  313. /// \param uid The unique ID of the current instruction.
  314. RecognizableInstr(DisassemblerTables &tables,
  315. const CodeGenInstruction &insn,
  316. InstrUID uid);
  317. /// processInstr - Accepts a CodeGenInstruction and loads decode information
  318. /// for it into a DisassemblerTables if appropriate.
  319. ///
  320. /// \param tables The DiassemblerTables to be populated with decode
  321. /// information.
  322. /// \param insn The CodeGenInstruction to be used as a source for this
  323. /// information.
  324. /// \param uid The unique ID of the instruction.
  325. static void processInstr(DisassemblerTables &tables,
  326. const CodeGenInstruction &insn,
  327. InstrUID uid);
  328. };
  329. std::string getMnemonic(const CodeGenInstruction *I, unsigned Variant);
  330. bool isRegisterOperand(const Record *Rec);
  331. bool isMemoryOperand(const Record *Rec);
  332. bool isImmediateOperand(const Record *Rec);
  333. unsigned getRegOperandSize(const Record *RegRec);
  334. unsigned getMemOperandSize(const Record *MemRec);
  335. } // namespace X86Disassembler
  336. } // namespace llvm
  337. #endif