X86DisassemblerTables.cpp 43 KB

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