2
0

X86DisassemblerTables.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. //===- X86DisassemblerTables.cpp - Disassembler tables ----------*- 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 implementation of the disassembler tables.
  12. // Documentation for the disassembler emitter in general can be found in
  13. // X86DisasemblerEmitter.h.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "X86DisassemblerTables.h"
  17. #include "X86DisassemblerShared.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/Support/Format.h"
  21. #include <map>
  22. using namespace llvm;
  23. using namespace X86Disassembler;
  24. /// stringForContext - Returns a string containing the name of a particular
  25. /// InstructionContext, usually for diagnostic purposes.
  26. ///
  27. /// @param insnContext - The instruction class to transform to a string.
  28. /// @return - A statically-allocated string constant that contains the
  29. /// name of the instruction class.
  30. static inline const char* stringForContext(InstructionContext insnContext) {
  31. switch (insnContext) {
  32. default:
  33. llvm_unreachable("Unhandled instruction class");
  34. #define ENUM_ENTRY(n, r, d) case n: return #n; break;
  35. #define ENUM_ENTRY_K_B(n, r, d) ENUM_ENTRY(n, r, d) ENUM_ENTRY(n##_K_B, r, d)\
  36. ENUM_ENTRY(n##_KZ, r, d) ENUM_ENTRY(n##_K, r, d) ENUM_ENTRY(n##_B, r, d)\
  37. ENUM_ENTRY(n##_KZ_B, r, d)
  38. INSTRUCTION_CONTEXTS
  39. #undef ENUM_ENTRY
  40. #undef ENUM_ENTRY_K_B
  41. }
  42. }
  43. /// stringForOperandType - Like stringForContext, but for OperandTypes.
  44. static inline const char* stringForOperandType(OperandType type) {
  45. switch (type) {
  46. default:
  47. llvm_unreachable("Unhandled type");
  48. #define ENUM_ENTRY(i, d) case i: return #i;
  49. TYPES
  50. #undef ENUM_ENTRY
  51. }
  52. }
  53. /// stringForOperandEncoding - like stringForContext, but for
  54. /// OperandEncodings.
  55. static inline const char* stringForOperandEncoding(OperandEncoding encoding) {
  56. switch (encoding) {
  57. default:
  58. llvm_unreachable("Unhandled encoding");
  59. #define ENUM_ENTRY(i, d) case i: return #i;
  60. ENCODINGS
  61. #undef ENUM_ENTRY
  62. }
  63. }
  64. /// inheritsFrom - Indicates whether all instructions in one class also belong
  65. /// to another class.
  66. ///
  67. /// @param child - The class that may be the subset
  68. /// @param parent - The class that may be the superset
  69. /// @return - True if child is a subset of parent, false otherwise.
  70. static inline bool inheritsFrom(InstructionContext child,
  71. InstructionContext parent,
  72. bool VEX_LIG = false, bool AdSize64 = false) {
  73. if (child == parent)
  74. return true;
  75. switch (parent) {
  76. case IC:
  77. return(inheritsFrom(child, IC_64BIT, AdSize64) ||
  78. inheritsFrom(child, IC_OPSIZE) ||
  79. inheritsFrom(child, IC_ADSIZE) ||
  80. inheritsFrom(child, IC_XD) ||
  81. inheritsFrom(child, IC_XS));
  82. case IC_64BIT:
  83. return(inheritsFrom(child, IC_64BIT_REXW) ||
  84. inheritsFrom(child, IC_64BIT_OPSIZE) ||
  85. (!AdSize64 && inheritsFrom(child, IC_64BIT_ADSIZE)) ||
  86. inheritsFrom(child, IC_64BIT_XD) ||
  87. inheritsFrom(child, IC_64BIT_XS));
  88. case IC_OPSIZE:
  89. return inheritsFrom(child, IC_64BIT_OPSIZE) ||
  90. inheritsFrom(child, IC_OPSIZE_ADSIZE);
  91. case IC_ADSIZE:
  92. return inheritsFrom(child, IC_OPSIZE_ADSIZE);
  93. case IC_OPSIZE_ADSIZE:
  94. return false;
  95. case IC_64BIT_ADSIZE:
  96. return inheritsFrom(child, IC_64BIT_OPSIZE_ADSIZE);
  97. case IC_64BIT_OPSIZE_ADSIZE:
  98. return false;
  99. case IC_XD:
  100. return inheritsFrom(child, IC_64BIT_XD);
  101. case IC_XS:
  102. return inheritsFrom(child, IC_64BIT_XS);
  103. case IC_XD_OPSIZE:
  104. return inheritsFrom(child, IC_64BIT_XD_OPSIZE);
  105. case IC_XS_OPSIZE:
  106. return inheritsFrom(child, IC_64BIT_XS_OPSIZE);
  107. case IC_64BIT_REXW:
  108. return(inheritsFrom(child, IC_64BIT_REXW_XS) ||
  109. inheritsFrom(child, IC_64BIT_REXW_XD) ||
  110. inheritsFrom(child, IC_64BIT_REXW_OPSIZE) ||
  111. (!AdSize64 && inheritsFrom(child, IC_64BIT_REXW_ADSIZE)));
  112. case IC_64BIT_OPSIZE:
  113. return inheritsFrom(child, IC_64BIT_REXW_OPSIZE) ||
  114. (!AdSize64 && inheritsFrom(child, IC_64BIT_OPSIZE_ADSIZE)) ||
  115. (!AdSize64 && inheritsFrom(child, IC_64BIT_REXW_ADSIZE));
  116. case IC_64BIT_XD:
  117. return(inheritsFrom(child, IC_64BIT_REXW_XD));
  118. case IC_64BIT_XS:
  119. return(inheritsFrom(child, IC_64BIT_REXW_XS));
  120. case IC_64BIT_XD_OPSIZE:
  121. case IC_64BIT_XS_OPSIZE:
  122. return false;
  123. case IC_64BIT_REXW_XD:
  124. case IC_64BIT_REXW_XS:
  125. case IC_64BIT_REXW_OPSIZE:
  126. case IC_64BIT_REXW_ADSIZE:
  127. return false;
  128. case IC_VEX:
  129. return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W)) ||
  130. inheritsFrom(child, IC_VEX_W) ||
  131. (VEX_LIG && inheritsFrom(child, IC_VEX_L));
  132. case IC_VEX_XS:
  133. return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XS)) ||
  134. inheritsFrom(child, IC_VEX_W_XS) ||
  135. (VEX_LIG && inheritsFrom(child, IC_VEX_L_XS));
  136. case IC_VEX_XD:
  137. return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XD)) ||
  138. inheritsFrom(child, IC_VEX_W_XD) ||
  139. (VEX_LIG && inheritsFrom(child, IC_VEX_L_XD));
  140. case IC_VEX_OPSIZE:
  141. return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W_OPSIZE)) ||
  142. inheritsFrom(child, IC_VEX_W_OPSIZE) ||
  143. (VEX_LIG && inheritsFrom(child, IC_VEX_L_OPSIZE));
  144. case IC_VEX_W:
  145. return VEX_LIG && inheritsFrom(child, IC_VEX_L_W);
  146. case IC_VEX_W_XS:
  147. return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XS);
  148. case IC_VEX_W_XD:
  149. return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XD);
  150. case IC_VEX_W_OPSIZE:
  151. return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_OPSIZE);
  152. case IC_VEX_L:
  153. return inheritsFrom(child, IC_VEX_L_W);
  154. case IC_VEX_L_XS:
  155. return inheritsFrom(child, IC_VEX_L_W_XS);
  156. case IC_VEX_L_XD:
  157. return inheritsFrom(child, IC_VEX_L_W_XD);
  158. case IC_VEX_L_OPSIZE:
  159. return inheritsFrom(child, IC_VEX_L_W_OPSIZE);
  160. case IC_VEX_L_W:
  161. case IC_VEX_L_W_XS:
  162. case IC_VEX_L_W_XD:
  163. case IC_VEX_L_W_OPSIZE:
  164. return false;
  165. case IC_EVEX:
  166. return inheritsFrom(child, IC_EVEX_W) ||
  167. inheritsFrom(child, IC_EVEX_L_W);
  168. case IC_EVEX_XS:
  169. return inheritsFrom(child, IC_EVEX_W_XS) ||
  170. inheritsFrom(child, IC_EVEX_L_W_XS);
  171. case IC_EVEX_XD:
  172. return inheritsFrom(child, IC_EVEX_W_XD) ||
  173. inheritsFrom(child, IC_EVEX_L_W_XD);
  174. case IC_EVEX_OPSIZE:
  175. return inheritsFrom(child, IC_EVEX_W_OPSIZE) ||
  176. inheritsFrom(child, IC_EVEX_L_W_OPSIZE);
  177. case IC_EVEX_B:
  178. return false;
  179. case IC_EVEX_W:
  180. case IC_EVEX_W_XS:
  181. case IC_EVEX_W_XD:
  182. case IC_EVEX_W_OPSIZE:
  183. return false;
  184. case IC_EVEX_L:
  185. case IC_EVEX_L_K_B:
  186. case IC_EVEX_L_KZ_B:
  187. case IC_EVEX_L_B:
  188. case IC_EVEX_L_XS:
  189. case IC_EVEX_L_XD:
  190. case IC_EVEX_L_OPSIZE:
  191. return false;
  192. case IC_EVEX_L_W:
  193. case IC_EVEX_L_W_XS:
  194. case IC_EVEX_L_W_XD:
  195. case IC_EVEX_L_W_OPSIZE:
  196. return false;
  197. case IC_EVEX_L2:
  198. case IC_EVEX_L2_XS:
  199. case IC_EVEX_L2_XD:
  200. case IC_EVEX_L2_OPSIZE:
  201. return false;
  202. case IC_EVEX_L2_W:
  203. case IC_EVEX_L2_W_XS:
  204. case IC_EVEX_L2_W_XD:
  205. case IC_EVEX_L2_W_OPSIZE:
  206. return false;
  207. case IC_EVEX_K:
  208. return inheritsFrom(child, IC_EVEX_W_K) ||
  209. inheritsFrom(child, IC_EVEX_L_W_K);
  210. case IC_EVEX_XS_K:
  211. case IC_EVEX_XS_K_B:
  212. case IC_EVEX_XS_KZ_B:
  213. return inheritsFrom(child, IC_EVEX_W_XS_K) ||
  214. inheritsFrom(child, IC_EVEX_L_W_XS_K);
  215. case IC_EVEX_XD_K:
  216. case IC_EVEX_XD_K_B:
  217. case IC_EVEX_XD_KZ_B:
  218. return inheritsFrom(child, IC_EVEX_W_XD_K) ||
  219. inheritsFrom(child, IC_EVEX_L_W_XD_K);
  220. case IC_EVEX_XS_B:
  221. case IC_EVEX_XD_B:
  222. case IC_EVEX_K_B:
  223. case IC_EVEX_KZ:
  224. return false;
  225. case IC_EVEX_XS_KZ:
  226. return inheritsFrom(child, IC_EVEX_W_XS_KZ) ||
  227. inheritsFrom(child, IC_EVEX_L_W_XS_KZ);
  228. case IC_EVEX_XD_KZ:
  229. return inheritsFrom(child, IC_EVEX_W_XD_KZ) ||
  230. inheritsFrom(child, IC_EVEX_L_W_XD_KZ);
  231. case IC_EVEX_KZ_B:
  232. case IC_EVEX_OPSIZE_K:
  233. case IC_EVEX_OPSIZE_B:
  234. case IC_EVEX_OPSIZE_K_B:
  235. case IC_EVEX_OPSIZE_KZ:
  236. case IC_EVEX_OPSIZE_KZ_B:
  237. return false;
  238. case IC_EVEX_W_K:
  239. case IC_EVEX_W_B:
  240. case IC_EVEX_W_K_B:
  241. case IC_EVEX_W_KZ_B:
  242. case IC_EVEX_W_XS_K:
  243. case IC_EVEX_W_XD_K:
  244. case IC_EVEX_W_OPSIZE_K:
  245. case IC_EVEX_W_OPSIZE_B:
  246. case IC_EVEX_W_OPSIZE_K_B:
  247. return false;
  248. case IC_EVEX_L_K:
  249. case IC_EVEX_L_XS_K:
  250. case IC_EVEX_L_XD_K:
  251. case IC_EVEX_L_XD_B:
  252. case IC_EVEX_L_XD_K_B:
  253. case IC_EVEX_L_OPSIZE_K:
  254. case IC_EVEX_L_OPSIZE_B:
  255. case IC_EVEX_L_OPSIZE_K_B:
  256. return false;
  257. case IC_EVEX_W_KZ:
  258. case IC_EVEX_W_XS_KZ:
  259. case IC_EVEX_W_XD_KZ:
  260. case IC_EVEX_W_XS_B:
  261. case IC_EVEX_W_XD_B:
  262. case IC_EVEX_W_XS_K_B:
  263. case IC_EVEX_W_XD_K_B:
  264. case IC_EVEX_W_XS_KZ_B:
  265. case IC_EVEX_W_XD_KZ_B:
  266. case IC_EVEX_W_OPSIZE_KZ:
  267. case IC_EVEX_W_OPSIZE_KZ_B:
  268. return false;
  269. case IC_EVEX_L_KZ:
  270. case IC_EVEX_L_XS_KZ:
  271. case IC_EVEX_L_XS_B:
  272. case IC_EVEX_L_XS_K_B:
  273. case IC_EVEX_L_XS_KZ_B:
  274. case IC_EVEX_L_XD_KZ:
  275. case IC_EVEX_L_XD_KZ_B:
  276. case IC_EVEX_L_OPSIZE_KZ:
  277. case IC_EVEX_L_OPSIZE_KZ_B:
  278. return false;
  279. case IC_EVEX_L_W_K:
  280. case IC_EVEX_L_W_B:
  281. case IC_EVEX_L_W_K_B:
  282. case IC_EVEX_L_W_XS_K:
  283. case IC_EVEX_L_W_XS_B:
  284. case IC_EVEX_L_W_XS_K_B:
  285. case IC_EVEX_L_W_XS_KZ:
  286. case IC_EVEX_L_W_XS_KZ_B:
  287. case IC_EVEX_L_W_OPSIZE_K:
  288. case IC_EVEX_L_W_OPSIZE_B:
  289. case IC_EVEX_L_W_OPSIZE_K_B:
  290. case IC_EVEX_L_W_KZ:
  291. case IC_EVEX_L_W_KZ_B:
  292. case IC_EVEX_L_W_XD_K:
  293. case IC_EVEX_L_W_XD_B:
  294. case IC_EVEX_L_W_XD_K_B:
  295. case IC_EVEX_L_W_XD_KZ:
  296. case IC_EVEX_L_W_XD_KZ_B:
  297. case IC_EVEX_L_W_OPSIZE_KZ:
  298. case IC_EVEX_L_W_OPSIZE_KZ_B:
  299. return false;
  300. case IC_EVEX_L2_K:
  301. case IC_EVEX_L2_B:
  302. case IC_EVEX_L2_K_B:
  303. case IC_EVEX_L2_KZ_B:
  304. case IC_EVEX_L2_XS_K:
  305. case IC_EVEX_L2_XS_K_B:
  306. case IC_EVEX_L2_XS_B:
  307. case IC_EVEX_L2_XD_B:
  308. case IC_EVEX_L2_XD_K:
  309. case IC_EVEX_L2_XD_K_B:
  310. case IC_EVEX_L2_OPSIZE_K:
  311. case IC_EVEX_L2_OPSIZE_B:
  312. case IC_EVEX_L2_OPSIZE_K_B:
  313. case IC_EVEX_L2_KZ:
  314. case IC_EVEX_L2_XS_KZ:
  315. case IC_EVEX_L2_XS_KZ_B:
  316. case IC_EVEX_L2_XD_KZ:
  317. case IC_EVEX_L2_XD_KZ_B:
  318. case IC_EVEX_L2_OPSIZE_KZ:
  319. case IC_EVEX_L2_OPSIZE_KZ_B:
  320. return false;
  321. case IC_EVEX_L2_W_K:
  322. case IC_EVEX_L2_W_B:
  323. case IC_EVEX_L2_W_K_B:
  324. case IC_EVEX_L2_W_KZ_B:
  325. case IC_EVEX_L2_W_XS_K:
  326. case IC_EVEX_L2_W_XS_B:
  327. case IC_EVEX_L2_W_XS_K_B:
  328. case IC_EVEX_L2_W_XD_K:
  329. case IC_EVEX_L2_W_XD_B:
  330. case IC_EVEX_L2_W_OPSIZE_K:
  331. case IC_EVEX_L2_W_OPSIZE_B:
  332. case IC_EVEX_L2_W_OPSIZE_K_B:
  333. case IC_EVEX_L2_W_KZ:
  334. case IC_EVEX_L2_W_XS_KZ:
  335. case IC_EVEX_L2_W_XS_KZ_B:
  336. case IC_EVEX_L2_W_XD_KZ:
  337. case IC_EVEX_L2_W_XD_K_B:
  338. case IC_EVEX_L2_W_XD_KZ_B:
  339. case IC_EVEX_L2_W_OPSIZE_KZ:
  340. case IC_EVEX_L2_W_OPSIZE_KZ_B:
  341. return false;
  342. default:
  343. errs() << "Unknown instruction class: " <<
  344. stringForContext((InstructionContext)parent) << "\n";
  345. llvm_unreachable("Unknown instruction class");
  346. }
  347. }
  348. /// outranks - Indicates whether, if an instruction has two different applicable
  349. /// classes, which class should be preferred when performing decode. This
  350. /// imposes a total ordering (ties are resolved toward "lower")
  351. ///
  352. /// @param upper - The class that may be preferable
  353. /// @param lower - The class that may be less preferable
  354. /// @return - True if upper is to be preferred, false otherwise.
  355. static inline bool outranks(InstructionContext upper,
  356. InstructionContext lower) {
  357. assert(upper < IC_max);
  358. assert(lower < IC_max);
  359. #define ENUM_ENTRY(n, r, d) r,
  360. #define ENUM_ENTRY_K_B(n, r, d) ENUM_ENTRY(n, r, d) \
  361. ENUM_ENTRY(n##_K_B, r, d) ENUM_ENTRY(n##_KZ_B, r, d) \
  362. ENUM_ENTRY(n##_KZ, r, d) ENUM_ENTRY(n##_K, r, d) ENUM_ENTRY(n##_B, r, d)
  363. static int ranks[IC_max] = {
  364. INSTRUCTION_CONTEXTS
  365. };
  366. #undef ENUM_ENTRY
  367. #undef ENUM_ENTRY_K_B
  368. return (ranks[upper] > ranks[lower]);
  369. }
  370. /// getDecisionType - Determines whether a ModRM decision with 255 entries can
  371. /// be compacted by eliminating redundant information.
  372. ///
  373. /// @param decision - The decision to be compacted.
  374. /// @return - The compactest available representation for the decision.
  375. static ModRMDecisionType getDecisionType(ModRMDecision &decision) {
  376. bool satisfiesOneEntry = true;
  377. bool satisfiesSplitRM = true;
  378. bool satisfiesSplitReg = true;
  379. bool satisfiesSplitMisc = true;
  380. for (unsigned index = 0; index < 256; ++index) {
  381. if (decision.instructionIDs[index] != decision.instructionIDs[0])
  382. satisfiesOneEntry = false;
  383. if (((index & 0xc0) == 0xc0) &&
  384. (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
  385. satisfiesSplitRM = false;
  386. if (((index & 0xc0) != 0xc0) &&
  387. (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
  388. satisfiesSplitRM = false;
  389. if (((index & 0xc0) == 0xc0) &&
  390. (decision.instructionIDs[index] != decision.instructionIDs[index&0xf8]))
  391. satisfiesSplitReg = false;
  392. if (((index & 0xc0) != 0xc0) &&
  393. (decision.instructionIDs[index] != decision.instructionIDs[index&0x38]))
  394. satisfiesSplitMisc = false;
  395. }
  396. if (satisfiesOneEntry)
  397. return MODRM_ONEENTRY;
  398. if (satisfiesSplitRM)
  399. return MODRM_SPLITRM;
  400. if (satisfiesSplitReg && satisfiesSplitMisc)
  401. return MODRM_SPLITREG;
  402. if (satisfiesSplitMisc)
  403. return MODRM_SPLITMISC;
  404. return MODRM_FULL;
  405. }
  406. /// stringForDecisionType - Returns a statically-allocated string corresponding
  407. /// to a particular decision type.
  408. ///
  409. /// @param dt - The decision type.
  410. /// @return - A pointer to the statically-allocated string (e.g.,
  411. /// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
  412. static const char* stringForDecisionType(ModRMDecisionType dt) {
  413. #define ENUM_ENTRY(n) case n: return #n;
  414. switch (dt) {
  415. default:
  416. llvm_unreachable("Unknown decision type");
  417. MODRMTYPES
  418. };
  419. #undef ENUM_ENTRY
  420. }
  421. DisassemblerTables::DisassemblerTables() {
  422. unsigned i;
  423. for (i = 0; i < array_lengthof(Tables); i++) {
  424. Tables[i] = new ContextDecision;
  425. memset(Tables[i], 0, sizeof(ContextDecision));
  426. }
  427. HasConflicts = false;
  428. }
  429. DisassemblerTables::~DisassemblerTables() {
  430. unsigned i;
  431. for (i = 0; i < array_lengthof(Tables); i++)
  432. delete Tables[i];
  433. }
  434. void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
  435. unsigned &i1, unsigned &i2,
  436. unsigned &ModRMTableNum,
  437. ModRMDecision &decision) const {
  438. static uint32_t sTableNumber = 0;
  439. static uint32_t sEntryNumber = 1;
  440. ModRMDecisionType dt = getDecisionType(decision);
  441. if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
  442. {
  443. o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
  444. i2++;
  445. o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
  446. o2.indent(i2) << 0 << " /* EmptyTable */\n";
  447. i2--;
  448. o2.indent(i2) << "}";
  449. return;
  450. }
  451. std::vector<unsigned> ModRMDecision;
  452. switch (dt) {
  453. default:
  454. llvm_unreachable("Unknown decision type");
  455. case MODRM_ONEENTRY:
  456. ModRMDecision.push_back(decision.instructionIDs[0]);
  457. break;
  458. case MODRM_SPLITRM:
  459. ModRMDecision.push_back(decision.instructionIDs[0x00]);
  460. ModRMDecision.push_back(decision.instructionIDs[0xc0]);
  461. break;
  462. case MODRM_SPLITREG:
  463. for (unsigned index = 0; index < 64; index += 8)
  464. ModRMDecision.push_back(decision.instructionIDs[index]);
  465. for (unsigned index = 0xc0; index < 256; index += 8)
  466. ModRMDecision.push_back(decision.instructionIDs[index]);
  467. break;
  468. case MODRM_SPLITMISC:
  469. for (unsigned index = 0; index < 64; index += 8)
  470. ModRMDecision.push_back(decision.instructionIDs[index]);
  471. for (unsigned index = 0xc0; index < 256; ++index)
  472. ModRMDecision.push_back(decision.instructionIDs[index]);
  473. break;
  474. case MODRM_FULL:
  475. for (unsigned index = 0; index < 256; ++index)
  476. ModRMDecision.push_back(decision.instructionIDs[index]);
  477. break;
  478. }
  479. unsigned &EntryNumber = ModRMTable[ModRMDecision];
  480. if (EntryNumber == 0) {
  481. EntryNumber = ModRMTableNum;
  482. ModRMTableNum += ModRMDecision.size();
  483. o1 << "/* Table" << EntryNumber << " */\n";
  484. i1++;
  485. for (std::vector<unsigned>::const_iterator I = ModRMDecision.begin(),
  486. E = ModRMDecision.end(); I != E; ++I) {
  487. o1.indent(i1 * 2) << format("0x%hx", *I) << ", /* "
  488. << InstructionSpecifiers[*I].name << " */\n";
  489. }
  490. i1--;
  491. }
  492. o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
  493. i2++;
  494. o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
  495. o2.indent(i2) << EntryNumber << " /* Table" << EntryNumber << " */\n";
  496. i2--;
  497. o2.indent(i2) << "}";
  498. switch (dt) {
  499. default:
  500. llvm_unreachable("Unknown decision type");
  501. case MODRM_ONEENTRY:
  502. sEntryNumber += 1;
  503. break;
  504. case MODRM_SPLITRM:
  505. sEntryNumber += 2;
  506. break;
  507. case MODRM_SPLITREG:
  508. sEntryNumber += 16;
  509. break;
  510. case MODRM_SPLITMISC:
  511. sEntryNumber += 8 + 64;
  512. break;
  513. case MODRM_FULL:
  514. sEntryNumber += 256;
  515. break;
  516. }
  517. // We assume that the index can fit into uint16_t.
  518. assert(sEntryNumber < 65536U &&
  519. "Index into ModRMDecision is too large for uint16_t!");
  520. ++sTableNumber;
  521. }
  522. void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
  523. unsigned &i1, unsigned &i2,
  524. unsigned &ModRMTableNum,
  525. OpcodeDecision &decision) const {
  526. o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
  527. i2++;
  528. o2.indent(i2) << "{" << "\n";
  529. i2++;
  530. for (unsigned index = 0; index < 256; ++index) {
  531. o2.indent(i2);
  532. o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
  533. emitModRMDecision(o1, o2, i1, i2, ModRMTableNum,
  534. decision.modRMDecisions[index]);
  535. if (index < 255)
  536. o2 << ",";
  537. o2 << "\n";
  538. }
  539. i2--;
  540. o2.indent(i2) << "}" << "\n";
  541. i2--;
  542. o2.indent(i2) << "}" << "\n";
  543. }
  544. void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2,
  545. unsigned &i1, unsigned &i2,
  546. unsigned &ModRMTableNum,
  547. ContextDecision &decision,
  548. const char* name) const {
  549. o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
  550. i2++;
  551. o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
  552. i2++;
  553. for (unsigned index = 0; index < IC_max; ++index) {
  554. o2.indent(i2) << "/* ";
  555. o2 << stringForContext((InstructionContext)index);
  556. o2 << " */";
  557. o2 << "\n";
  558. emitOpcodeDecision(o1, o2, i1, i2, ModRMTableNum,
  559. decision.opcodeDecisions[index]);
  560. if (index + 1 < IC_max)
  561. o2 << ", ";
  562. }
  563. i2--;
  564. o2.indent(i2) << "}" << "\n";
  565. i2--;
  566. o2.indent(i2) << "};" << "\n";
  567. }
  568. void DisassemblerTables::emitInstructionInfo(raw_ostream &o,
  569. unsigned &i) const {
  570. unsigned NumInstructions = InstructionSpecifiers.size();
  571. o << "static const struct OperandSpecifier x86OperandSets[]["
  572. << X86_MAX_OPERANDS << "] = {\n";
  573. typedef SmallVector<std::pair<OperandEncoding, OperandType>,
  574. X86_MAX_OPERANDS> OperandListTy;
  575. std::map<OperandListTy, unsigned> OperandSets;
  576. unsigned OperandSetNum = 0;
  577. for (unsigned Index = 0; Index < NumInstructions; ++Index) {
  578. OperandListTy OperandList;
  579. for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
  580. ++OperandIndex) {
  581. OperandEncoding Encoding = (OperandEncoding)InstructionSpecifiers[Index]
  582. .operands[OperandIndex].encoding;
  583. OperandType Type = (OperandType)InstructionSpecifiers[Index]
  584. .operands[OperandIndex].type;
  585. OperandList.push_back(std::make_pair(Encoding, Type));
  586. }
  587. unsigned &N = OperandSets[OperandList];
  588. if (N != 0) continue;
  589. N = ++OperandSetNum;
  590. o << " { /* " << (OperandSetNum - 1) << " */\n";
  591. for (unsigned i = 0, e = OperandList.size(); i != e; ++i) {
  592. const char *Encoding = stringForOperandEncoding(OperandList[i].first);
  593. const char *Type = stringForOperandType(OperandList[i].second);
  594. o << " { " << Encoding << ", " << Type << " },\n";
  595. }
  596. o << " },\n";
  597. }
  598. o << "};" << "\n\n";
  599. o.indent(i * 2) << "static const struct InstructionSpecifier ";
  600. o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
  601. i++;
  602. for (unsigned index = 0; index < NumInstructions; ++index) {
  603. o.indent(i * 2) << "{ /* " << index << " */\n";
  604. i++;
  605. OperandListTy OperandList;
  606. for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
  607. ++OperandIndex) {
  608. OperandEncoding Encoding = (OperandEncoding)InstructionSpecifiers[index]
  609. .operands[OperandIndex].encoding;
  610. OperandType Type = (OperandType)InstructionSpecifiers[index]
  611. .operands[OperandIndex].type;
  612. OperandList.push_back(std::make_pair(Encoding, Type));
  613. }
  614. o.indent(i * 2) << (OperandSets[OperandList] - 1) << ",\n";
  615. o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */\n";
  616. i--;
  617. o.indent(i * 2) << "},\n";
  618. }
  619. i--;
  620. o.indent(i * 2) << "};" << "\n";
  621. }
  622. void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const {
  623. const unsigned int tableSize = 16384;
  624. o.indent(i * 2) << "static const uint8_t " CONTEXTS_STR
  625. "[" << tableSize << "] = {\n";
  626. i++;
  627. for (unsigned index = 0; index < tableSize; ++index) {
  628. o.indent(i * 2);
  629. if (index & ATTR_EVEX) {
  630. o << "IC_EVEX";
  631. if (index & ATTR_EVEXL2)
  632. o << "_L2";
  633. else if (index & ATTR_EVEXL)
  634. o << "_L";
  635. if (index & ATTR_REXW)
  636. o << "_W";
  637. if (index & ATTR_OPSIZE)
  638. o << "_OPSIZE";
  639. else if (index & ATTR_XD)
  640. o << "_XD";
  641. else if (index & ATTR_XS)
  642. o << "_XS";
  643. if (index & ATTR_EVEXKZ)
  644. o << "_KZ";
  645. else if (index & ATTR_EVEXK)
  646. o << "_K";
  647. if (index & ATTR_EVEXB)
  648. o << "_B";
  649. }
  650. else if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
  651. o << "IC_VEX_L_W_OPSIZE";
  652. else if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_XD))
  653. o << "IC_VEX_L_W_XD";
  654. else if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_XS))
  655. o << "IC_VEX_L_W_XS";
  656. else if ((index & ATTR_VEXL) && (index & ATTR_REXW))
  657. o << "IC_VEX_L_W";
  658. else if ((index & ATTR_VEXL) && (index & ATTR_OPSIZE))
  659. o << "IC_VEX_L_OPSIZE";
  660. else if ((index & ATTR_VEXL) && (index & ATTR_XD))
  661. o << "IC_VEX_L_XD";
  662. else if ((index & ATTR_VEXL) && (index & ATTR_XS))
  663. o << "IC_VEX_L_XS";
  664. else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
  665. o << "IC_VEX_W_OPSIZE";
  666. else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XD))
  667. o << "IC_VEX_W_XD";
  668. else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
  669. o << "IC_VEX_W_XS";
  670. else if (index & ATTR_VEXL)
  671. o << "IC_VEX_L";
  672. else if ((index & ATTR_VEX) && (index & ATTR_REXW))
  673. o << "IC_VEX_W";
  674. else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
  675. o << "IC_VEX_OPSIZE";
  676. else if ((index & ATTR_VEX) && (index & ATTR_XD))
  677. o << "IC_VEX_XD";
  678. else if ((index & ATTR_VEX) && (index & ATTR_XS))
  679. o << "IC_VEX_XS";
  680. else if (index & ATTR_VEX)
  681. o << "IC_VEX";
  682. else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
  683. o << "IC_64BIT_REXW_XS";
  684. else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
  685. o << "IC_64BIT_REXW_XD";
  686. else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
  687. (index & ATTR_OPSIZE))
  688. o << "IC_64BIT_REXW_OPSIZE";
  689. else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
  690. (index & ATTR_ADSIZE))
  691. o << "IC_64BIT_REXW_ADSIZE";
  692. else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
  693. o << "IC_64BIT_XD_OPSIZE";
  694. else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE))
  695. o << "IC_64BIT_XS_OPSIZE";
  696. else if ((index & ATTR_64BIT) && (index & ATTR_XS))
  697. o << "IC_64BIT_XS";
  698. else if ((index & ATTR_64BIT) && (index & ATTR_XD))
  699. o << "IC_64BIT_XD";
  700. else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE) &&
  701. (index & ATTR_ADSIZE))
  702. o << "IC_64BIT_OPSIZE_ADSIZE";
  703. else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
  704. o << "IC_64BIT_OPSIZE";
  705. else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE))
  706. o << "IC_64BIT_ADSIZE";
  707. else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
  708. o << "IC_64BIT_REXW";
  709. else if ((index & ATTR_64BIT))
  710. o << "IC_64BIT";
  711. else if ((index & ATTR_XS) && (index & ATTR_OPSIZE))
  712. o << "IC_XS_OPSIZE";
  713. else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
  714. o << "IC_XD_OPSIZE";
  715. else if (index & ATTR_XS)
  716. o << "IC_XS";
  717. else if (index & ATTR_XD)
  718. o << "IC_XD";
  719. else if ((index & ATTR_OPSIZE) && (index & ATTR_ADSIZE))
  720. o << "IC_OPSIZE_ADSIZE";
  721. else if (index & ATTR_OPSIZE)
  722. o << "IC_OPSIZE";
  723. else if (index & ATTR_ADSIZE)
  724. o << "IC_ADSIZE";
  725. else
  726. o << "IC";
  727. if (index < tableSize - 1)
  728. o << ",";
  729. else
  730. o << " ";
  731. o << " /* " << index << " */";
  732. o << "\n";
  733. }
  734. i--;
  735. o.indent(i * 2) << "};" << "\n";
  736. }
  737. void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
  738. unsigned &i1, unsigned &i2,
  739. unsigned &ModRMTableNum) const {
  740. emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[0], ONEBYTE_STR);
  741. emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[1], TWOBYTE_STR);
  742. emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[2], THREEBYTE38_STR);
  743. emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[3], THREEBYTE3A_STR);
  744. emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[4], XOP8_MAP_STR);
  745. emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[5], XOP9_MAP_STR);
  746. emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[6], XOPA_MAP_STR);
  747. }
  748. void DisassemblerTables::emit(raw_ostream &o) const {
  749. unsigned i1 = 0;
  750. unsigned i2 = 0;
  751. std::string s1;
  752. std::string s2;
  753. raw_string_ostream o1(s1);
  754. raw_string_ostream o2(s2);
  755. emitInstructionInfo(o, i2);
  756. o << "\n";
  757. emitContextTable(o, i2);
  758. o << "\n";
  759. unsigned ModRMTableNum = 0;
  760. o << "static const InstrUID modRMTable[] = {\n";
  761. i1++;
  762. std::vector<unsigned> EmptyTable(1, 0);
  763. ModRMTable[EmptyTable] = ModRMTableNum;
  764. ModRMTableNum += EmptyTable.size();
  765. o1 << "/* EmptyTable */\n";
  766. o1.indent(i1 * 2) << "0x0,\n";
  767. i1--;
  768. emitContextDecisions(o1, o2, i1, i2, ModRMTableNum);
  769. o << o1.str();
  770. o << " 0x0\n";
  771. o << "};\n";
  772. o << "\n";
  773. o << o2.str();
  774. o << "\n";
  775. o << "\n";
  776. }
  777. void DisassemblerTables::setTableFields(ModRMDecision &decision,
  778. const ModRMFilter &filter,
  779. InstrUID uid,
  780. uint8_t opcode) {
  781. for (unsigned index = 0; index < 256; ++index) {
  782. if (filter.accepts(index)) {
  783. if (decision.instructionIDs[index] == uid)
  784. continue;
  785. if (decision.instructionIDs[index] != 0) {
  786. InstructionSpecifier &newInfo =
  787. InstructionSpecifiers[uid];
  788. InstructionSpecifier &previousInfo =
  789. InstructionSpecifiers[decision.instructionIDs[index]];
  790. if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
  791. newInfo.name == "XCHG32ar" ||
  792. newInfo.name == "XCHG32ar64" ||
  793. newInfo.name == "XCHG64ar"))
  794. continue; // special case for XCHG*ar and NOOP
  795. if (outranks(previousInfo.insnContext, newInfo.insnContext))
  796. continue;
  797. if (previousInfo.insnContext == newInfo.insnContext) {
  798. errs() << "Error: Primary decode conflict: ";
  799. errs() << newInfo.name << " would overwrite " << previousInfo.name;
  800. errs() << "\n";
  801. errs() << "ModRM " << index << "\n";
  802. errs() << "Opcode " << (uint16_t)opcode << "\n";
  803. errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
  804. HasConflicts = true;
  805. }
  806. }
  807. decision.instructionIDs[index] = uid;
  808. }
  809. }
  810. }
  811. void DisassemblerTables::setTableFields(OpcodeType type,
  812. InstructionContext insnContext,
  813. uint8_t opcode,
  814. const ModRMFilter &filter,
  815. InstrUID uid,
  816. bool is32bit,
  817. bool ignoresVEX_L,
  818. unsigned addressSize) {
  819. ContextDecision &decision = *Tables[type];
  820. for (unsigned index = 0; index < IC_max; ++index) {
  821. if ((is32bit || addressSize == 16) &&
  822. inheritsFrom((InstructionContext)index, IC_64BIT))
  823. continue;
  824. bool adSize64 = addressSize == 64;
  825. if (inheritsFrom((InstructionContext)index,
  826. InstructionSpecifiers[uid].insnContext, ignoresVEX_L,
  827. adSize64))
  828. setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
  829. filter,
  830. uid,
  831. opcode);
  832. }
  833. }