i386-gen.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /* Copyright (C) 2007-2015 Free Software Foundation, Inc.
  2. This file is part of the GNU opcodes library.
  3. This library is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. It is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  10. License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  14. MA 02110-1301, USA. */
  15. #include "sysdep.h"
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include "getopt.h"
  19. #include "libiberty.h"
  20. #include "hashtab.h"
  21. #include "safe-ctype.h"
  22. #include "i386-opc.h"
  23. #include <libintl.h>
  24. #define _(String) gettext (String)
  25. static const char *program_name = NULL;
  26. static int debug = 0;
  27. typedef struct initializer
  28. {
  29. const char *name;
  30. const char *init;
  31. } initializer;
  32. static initializer cpu_flag_init[] =
  33. {
  34. { "CPU_UNKNOWN_FLAGS",
  35. "~(CpuL1OM|CpuK1OM)" },
  36. { "CPU_GENERIC32_FLAGS",
  37. "Cpu186|Cpu286|Cpu386" },
  38. { "CPU_GENERIC64_FLAGS",
  39. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuLM" },
  40. { "CPU_NONE_FLAGS",
  41. "0" },
  42. { "CPU_I186_FLAGS",
  43. "Cpu186" },
  44. { "CPU_I286_FLAGS",
  45. "Cpu186|Cpu286" },
  46. { "CPU_I386_FLAGS",
  47. "Cpu186|Cpu286|Cpu386" },
  48. { "CPU_I486_FLAGS",
  49. "Cpu186|Cpu286|Cpu386|Cpu486" },
  50. { "CPU_I586_FLAGS",
  51. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu387" },
  52. { "CPU_I686_FLAGS",
  53. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687" },
  54. { "CPU_PENTIUMPRO_FLAGS",
  55. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop" },
  56. { "CPU_P2_FLAGS",
  57. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX" },
  58. { "CPU_P3_FLAGS",
  59. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE" },
  60. { "CPU_P4_FLAGS",
  61. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2" },
  62. { "CPU_NOCONA_FLAGS",
  63. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuLM|CpuCX16" },
  64. { "CPU_CORE_FLAGS",
  65. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuCX16" },
  66. { "CPU_CORE2_FLAGS",
  67. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuLM|CpuCX16" },
  68. { "CPU_COREI7_FLAGS",
  69. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuClflush|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuRdtscp|CpuLM|CpuCX16" },
  70. { "CPU_K6_FLAGS",
  71. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX" },
  72. { "CPU_K6_2_FLAGS",
  73. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuSYSCALL|Cpu387|CpuMMX|Cpu3dnow" },
  74. { "CPU_ATHLON_FLAGS",
  75. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA" },
  76. { "CPU_K8_FLAGS",
  77. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuLM" },
  78. { "CPU_AMDFAM10_FLAGS",
  79. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM" },
  80. { "CPU_BDVER1_FLAGS",
  81. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA4|CpuXOP|CpuLWP|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuXsave|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW" },
  82. { "CPU_BDVER2_FLAGS",
  83. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuXsave|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW" },
  84. { "CPU_BDVER3_FLAGS",
  85. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW|CpuXsave|CpuXsaveopt|CpuFSGSBase" },
  86. { "CPU_BDVER4_FLAGS",
  87. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuFMA4|CpuXOP|CpuLWP|CpuBMI|CpuTBM|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW|CpuXsave|CpuXsaveopt|CpuFSGSBase|CpuAVX2|CpuMovbe|CpuBMI2|CpuRdRnd|CpuMWAITX" },
  88. { "CPU_ZNVER1_FLAGS",
  89. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuFISTTP|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a|CpuABM|CpuLM|CpuFMA|CpuBMI|CpuF16C|CpuCX16|CpuClflush|CpuSSSE3|CpuSVME|CpuSSE4_1|CpuSSE4_2|CpuAES|CpuAVX|CpuPCLMUL|CpuLZCNT|CpuPRFCHW|CpuXsave|CpuXsaveopt|CpuFSGSBase|CpuAVX2|CpuMovbe|CpuBMI2|CpuRdRnd|CpuADX|CpuRdSeed|CpuSMAP|CpuSHA|CpuXSAVEC|CpuXSAVES|CpuClflushOpt|CpuCLZERO|CpuMWAITX" },
  90. { "CPU_BTVER1_FLAGS",
  91. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4a|CpuABM|CpuLM|CpuPRFCHW|CpuCX16|CpuClflush|CpuFISTTP|CpuSVME|CpuLZCNT" },
  92. { "CPU_BTVER2_FLAGS",
  93. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuSYSCALL|CpuRdtscp|Cpu387|Cpu687|CpuNop|CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4a|CpuSSE4_1|CpuSSE4_2|CpuABM|CpuLM|CpuBMI|CpuF16C|CpuAES|CpuPCLMUL|CpuAVX|CpuMovbe|CpuXsave|CpuXsaveopt|CpuPRFCHW|CpuCX16|CpuClflush|CpuFISTTP|CpuSVME|CpuLZCNT" },
  94. { "CPU_8087_FLAGS",
  95. "Cpu8087" },
  96. { "CPU_287_FLAGS",
  97. "Cpu287" },
  98. { "CPU_387_FLAGS",
  99. "Cpu387" },
  100. { "CPU_ANY87_FLAGS",
  101. "Cpu8087|Cpu287|Cpu387|Cpu687|CpuFISTTP" },
  102. { "CPU_CLFLUSH_FLAGS",
  103. "CpuClflush" },
  104. { "CPU_NOP_FLAGS",
  105. "CpuNop" },
  106. { "CPU_SYSCALL_FLAGS",
  107. "CpuSYSCALL" },
  108. { "CPU_MMX_FLAGS",
  109. "CpuMMX" },
  110. { "CPU_SSE_FLAGS",
  111. "CpuMMX|CpuSSE" },
  112. { "CPU_SSE2_FLAGS",
  113. "CpuMMX|CpuSSE|CpuSSE2" },
  114. { "CPU_SSE3_FLAGS",
  115. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3" },
  116. { "CPU_SSSE3_FLAGS",
  117. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3" },
  118. { "CPU_SSE4_1_FLAGS",
  119. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1" },
  120. { "CPU_SSE4_2_FLAGS",
  121. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2" },
  122. { "CPU_ANY_SSE_FLAGS",
  123. "CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuSSE4a|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD|CpuAVX512ER|CpuAVX512PF" },
  124. { "CPU_VMX_FLAGS",
  125. "CpuVMX" },
  126. { "CPU_SMX_FLAGS",
  127. "CpuSMX" },
  128. { "CPU_XSAVE_FLAGS",
  129. "CpuXsave" },
  130. { "CPU_XSAVEOPT_FLAGS",
  131. "CpuXsaveopt" },
  132. { "CPU_AES_FLAGS",
  133. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAES" },
  134. { "CPU_PCLMUL_FLAGS",
  135. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuPCLMUL" },
  136. { "CPU_FMA_FLAGS",
  137. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA" },
  138. { "CPU_FMA4_FLAGS",
  139. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuFMA4" },
  140. { "CPU_XOP_FLAGS",
  141. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuSSE4a|CpuABM|CpuAVX|CpuFMA4|CpuXOP" },
  142. { "CPU_LWP_FLAGS",
  143. "CpuLWP" },
  144. { "CPU_BMI_FLAGS",
  145. "CpuBMI" },
  146. { "CPU_TBM_FLAGS",
  147. "CpuTBM" },
  148. { "CPU_MOVBE_FLAGS",
  149. "CpuMovbe" },
  150. { "CPU_CX16_FLAGS",
  151. "CpuCX16" },
  152. { "CPU_RDTSCP_FLAGS",
  153. "CpuRdtscp" },
  154. { "CPU_EPT_FLAGS",
  155. "CpuEPT" },
  156. { "CPU_FSGSBASE_FLAGS",
  157. "CpuFSGSBase" },
  158. { "CPU_RDRND_FLAGS",
  159. "CpuRdRnd" },
  160. { "CPU_F16C_FLAGS",
  161. "CpuF16C" },
  162. { "CPU_BMI2_FLAGS",
  163. "CpuBMI2" },
  164. { "CPU_LZCNT_FLAGS",
  165. "CpuLZCNT" },
  166. { "CPU_HLE_FLAGS",
  167. "CpuHLE" },
  168. { "CPU_RTM_FLAGS",
  169. "CpuRTM" },
  170. { "CPU_INVPCID_FLAGS",
  171. "CpuINVPCID" },
  172. { "CPU_VMFUNC_FLAGS",
  173. "CpuVMFUNC" },
  174. { "CPU_3DNOW_FLAGS",
  175. "CpuMMX|Cpu3dnow" },
  176. { "CPU_3DNOWA_FLAGS",
  177. "CpuMMX|Cpu3dnow|Cpu3dnowA" },
  178. { "CPU_PADLOCK_FLAGS",
  179. "CpuPadLock" },
  180. { "CPU_SVME_FLAGS",
  181. "CpuSVME" },
  182. { "CPU_SSE4A_FLAGS",
  183. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSE4a" },
  184. { "CPU_ABM_FLAGS",
  185. "CpuABM" },
  186. { "CPU_AVX_FLAGS",
  187. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX" },
  188. { "CPU_AVX2_FLAGS",
  189. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2" },
  190. { "CPU_AVX512F_FLAGS",
  191. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F" },
  192. { "CPU_AVX512CD_FLAGS",
  193. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD" },
  194. { "CPU_AVX512ER_FLAGS",
  195. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512ER" },
  196. { "CPU_AVX512PF_FLAGS",
  197. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512PF" },
  198. { "CPU_ANY_AVX_FLAGS",
  199. "CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512CD|CpuAVX512ER|CpuAVX512PF" },
  200. { "CPU_L1OM_FLAGS",
  201. "unknown" },
  202. { "CPU_K1OM_FLAGS",
  203. "unknown" },
  204. { "CPU_IAMCU_FLAGS",
  205. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586" },
  206. { "CPU_IAMCU_COMPAT_FLAGS",
  207. "Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuNo64|CpuNop" },
  208. { "CPU_ADX_FLAGS",
  209. "CpuADX" },
  210. { "CPU_RDSEED_FLAGS",
  211. "CpuRdSeed" },
  212. { "CPU_PRFCHW_FLAGS",
  213. "CpuPRFCHW" },
  214. { "CPU_SMAP_FLAGS",
  215. "CpuSMAP" },
  216. { "CPU_MPX_FLAGS",
  217. "CpuMPX" },
  218. { "CPU_SHA_FLAGS",
  219. "CpuSHA" },
  220. { "CPU_CLFLUSHOPT_FLAGS",
  221. "CpuClflushOpt" },
  222. { "CPU_XSAVES_FLAGS",
  223. "CpuXSAVES" },
  224. { "CPU_XSAVEC_FLAGS",
  225. "CpuXSAVEC" },
  226. { "CPU_PREFETCHWT1_FLAGS",
  227. "CpuPREFETCHWT1" },
  228. { "CPU_SE1_FLAGS",
  229. "CpuSE1" },
  230. { "CPU_AVX512DQ_FLAGS",
  231. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512DQ" },
  232. { "CPU_AVX512BW_FLAGS",
  233. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512BW" },
  234. { "CPU_AVX512VL_FLAGS",
  235. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512VL" },
  236. { "CPU_CLWB_FLAGS",
  237. "CpuCLWB" },
  238. { "CPU_PCOMMIT_FLAGS",
  239. "CpuPCOMMIT" },
  240. { "CPU_AVX512IFMA_FLAGS",
  241. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512IFMA" },
  242. { "CPU_AVX512VBMI_FLAGS",
  243. "CpuMMX|CpuSSE|CpuSSE2|CpuSSE3|CpuSSSE3|CpuSSE4_1|CpuSSE4_2|CpuAVX|CpuAVX2|CpuAVX512F|CpuAVX512VBMI" },
  244. { "CPU_CLZERO_FLAGS",
  245. "CpuCLZERO" },
  246. { "CPU_MWAITX_FLAGS",
  247. "CpuMWAITX" },
  248. };
  249. static initializer operand_type_init[] =
  250. {
  251. { "OPERAND_TYPE_NONE",
  252. "0" },
  253. { "OPERAND_TYPE_REG8",
  254. "Reg8" },
  255. { "OPERAND_TYPE_REG16",
  256. "Reg16" },
  257. { "OPERAND_TYPE_REG32",
  258. "Reg32" },
  259. { "OPERAND_TYPE_REG64",
  260. "Reg64" },
  261. { "OPERAND_TYPE_IMM1",
  262. "Imm1" },
  263. { "OPERAND_TYPE_IMM8",
  264. "Imm8" },
  265. { "OPERAND_TYPE_IMM8S",
  266. "Imm8S" },
  267. { "OPERAND_TYPE_IMM16",
  268. "Imm16" },
  269. { "OPERAND_TYPE_IMM32",
  270. "Imm32" },
  271. { "OPERAND_TYPE_IMM32S",
  272. "Imm32S" },
  273. { "OPERAND_TYPE_IMM64",
  274. "Imm64" },
  275. { "OPERAND_TYPE_BASEINDEX",
  276. "BaseIndex" },
  277. { "OPERAND_TYPE_DISP8",
  278. "Disp8" },
  279. { "OPERAND_TYPE_DISP16",
  280. "Disp16" },
  281. { "OPERAND_TYPE_DISP32",
  282. "Disp32" },
  283. { "OPERAND_TYPE_DISP32S",
  284. "Disp32S" },
  285. { "OPERAND_TYPE_DISP64",
  286. "Disp64" },
  287. { "OPERAND_TYPE_INOUTPORTREG",
  288. "InOutPortReg" },
  289. { "OPERAND_TYPE_SHIFTCOUNT",
  290. "ShiftCount" },
  291. { "OPERAND_TYPE_CONTROL",
  292. "Control" },
  293. { "OPERAND_TYPE_TEST",
  294. "Test" },
  295. { "OPERAND_TYPE_DEBUG",
  296. "FloatReg" },
  297. { "OPERAND_TYPE_FLOATREG",
  298. "FloatReg" },
  299. { "OPERAND_TYPE_FLOATACC",
  300. "FloatAcc" },
  301. { "OPERAND_TYPE_SREG2",
  302. "SReg2" },
  303. { "OPERAND_TYPE_SREG3",
  304. "SReg3" },
  305. { "OPERAND_TYPE_ACC",
  306. "Acc" },
  307. { "OPERAND_TYPE_JUMPABSOLUTE",
  308. "JumpAbsolute" },
  309. { "OPERAND_TYPE_REGMMX",
  310. "RegMMX" },
  311. { "OPERAND_TYPE_REGXMM",
  312. "RegXMM" },
  313. { "OPERAND_TYPE_REGYMM",
  314. "RegYMM" },
  315. { "OPERAND_TYPE_REGZMM",
  316. "RegZMM" },
  317. { "OPERAND_TYPE_REGMASK",
  318. "RegMask" },
  319. { "OPERAND_TYPE_ESSEG",
  320. "EsSeg" },
  321. { "OPERAND_TYPE_ACC32",
  322. "Reg32|Acc|Dword" },
  323. { "OPERAND_TYPE_ACC64",
  324. "Reg64|Acc|Qword" },
  325. { "OPERAND_TYPE_INOUTPORTREG",
  326. "InOutPortReg" },
  327. { "OPERAND_TYPE_REG16_INOUTPORTREG",
  328. "Reg16|InOutPortReg" },
  329. { "OPERAND_TYPE_DISP16_32",
  330. "Disp16|Disp32" },
  331. { "OPERAND_TYPE_ANYDISP",
  332. "Disp8|Disp16|Disp32|Disp32S|Disp64" },
  333. { "OPERAND_TYPE_IMM16_32",
  334. "Imm16|Imm32" },
  335. { "OPERAND_TYPE_IMM16_32S",
  336. "Imm16|Imm32S" },
  337. { "OPERAND_TYPE_IMM16_32_32S",
  338. "Imm16|Imm32|Imm32S" },
  339. { "OPERAND_TYPE_IMM32_64",
  340. "Imm32|Imm64" },
  341. { "OPERAND_TYPE_IMM32_32S_DISP32",
  342. "Imm32|Imm32S|Disp32" },
  343. { "OPERAND_TYPE_IMM64_DISP64",
  344. "Imm64|Disp64" },
  345. { "OPERAND_TYPE_IMM32_32S_64_DISP32",
  346. "Imm32|Imm32S|Imm64|Disp32" },
  347. { "OPERAND_TYPE_IMM32_32S_64_DISP32_64",
  348. "Imm32|Imm32S|Imm64|Disp32|Disp64" },
  349. { "OPERAND_TYPE_VEC_IMM4",
  350. "Vec_Imm4" },
  351. { "OPERAND_TYPE_REGBND",
  352. "RegBND" },
  353. { "OPERAND_TYPE_VEC_DISP8",
  354. "Vec_Disp8" },
  355. };
  356. typedef struct bitfield
  357. {
  358. int position;
  359. int value;
  360. const char *name;
  361. } bitfield;
  362. #define BITFIELD(n) { n, 0, #n }
  363. static bitfield cpu_flags[] =
  364. {
  365. BITFIELD (Cpu186),
  366. BITFIELD (Cpu286),
  367. BITFIELD (Cpu386),
  368. BITFIELD (Cpu486),
  369. BITFIELD (Cpu586),
  370. BITFIELD (Cpu686),
  371. BITFIELD (CpuClflush),
  372. BITFIELD (CpuNop),
  373. BITFIELD (CpuSYSCALL),
  374. BITFIELD (Cpu8087),
  375. BITFIELD (Cpu287),
  376. BITFIELD (Cpu387),
  377. BITFIELD (Cpu687),
  378. BITFIELD (CpuFISTTP),
  379. BITFIELD (CpuMMX),
  380. BITFIELD (CpuSSE),
  381. BITFIELD (CpuSSE2),
  382. BITFIELD (CpuSSE3),
  383. BITFIELD (CpuSSSE3),
  384. BITFIELD (CpuSSE4_1),
  385. BITFIELD (CpuSSE4_2),
  386. BITFIELD (CpuAVX),
  387. BITFIELD (CpuAVX2),
  388. BITFIELD (CpuAVX512F),
  389. BITFIELD (CpuAVX512CD),
  390. BITFIELD (CpuAVX512ER),
  391. BITFIELD (CpuAVX512PF),
  392. BITFIELD (CpuAVX512VL),
  393. BITFIELD (CpuAVX512DQ),
  394. BITFIELD (CpuAVX512BW),
  395. BITFIELD (CpuL1OM),
  396. BITFIELD (CpuK1OM),
  397. BITFIELD (CpuIAMCU),
  398. BITFIELD (CpuSSE4a),
  399. BITFIELD (Cpu3dnow),
  400. BITFIELD (Cpu3dnowA),
  401. BITFIELD (CpuPadLock),
  402. BITFIELD (CpuSVME),
  403. BITFIELD (CpuVMX),
  404. BITFIELD (CpuSMX),
  405. BITFIELD (CpuABM),
  406. BITFIELD (CpuXsave),
  407. BITFIELD (CpuXsaveopt),
  408. BITFIELD (CpuAES),
  409. BITFIELD (CpuPCLMUL),
  410. BITFIELD (CpuFMA),
  411. BITFIELD (CpuFMA4),
  412. BITFIELD (CpuXOP),
  413. BITFIELD (CpuLWP),
  414. BITFIELD (CpuBMI),
  415. BITFIELD (CpuTBM),
  416. BITFIELD (CpuLM),
  417. BITFIELD (CpuMovbe),
  418. BITFIELD (CpuCX16),
  419. BITFIELD (CpuEPT),
  420. BITFIELD (CpuRdtscp),
  421. BITFIELD (CpuFSGSBase),
  422. BITFIELD (CpuRdRnd),
  423. BITFIELD (CpuF16C),
  424. BITFIELD (CpuBMI2),
  425. BITFIELD (CpuLZCNT),
  426. BITFIELD (CpuHLE),
  427. BITFIELD (CpuRTM),
  428. BITFIELD (CpuINVPCID),
  429. BITFIELD (CpuVMFUNC),
  430. BITFIELD (CpuRDSEED),
  431. BITFIELD (CpuADX),
  432. BITFIELD (CpuPRFCHW),
  433. BITFIELD (CpuSMAP),
  434. BITFIELD (CpuSHA),
  435. BITFIELD (CpuVREX),
  436. BITFIELD (CpuClflushOpt),
  437. BITFIELD (CpuXSAVES),
  438. BITFIELD (CpuXSAVEC),
  439. BITFIELD (CpuPREFETCHWT1),
  440. BITFIELD (CpuSE1),
  441. BITFIELD (CpuCLWB),
  442. BITFIELD (CpuPCOMMIT),
  443. BITFIELD (Cpu64),
  444. BITFIELD (CpuNo64),
  445. BITFIELD (CpuMPX),
  446. BITFIELD (CpuAVX512IFMA),
  447. BITFIELD (CpuAVX512VBMI),
  448. BITFIELD (CpuMWAITX),
  449. BITFIELD (CpuCLZERO),
  450. BITFIELD (CpuAMD64),
  451. BITFIELD (CpuIntel64),
  452. #ifdef CpuUnused
  453. BITFIELD (CpuUnused),
  454. #endif
  455. };
  456. static bitfield opcode_modifiers[] =
  457. {
  458. BITFIELD (D),
  459. BITFIELD (W),
  460. BITFIELD (S),
  461. BITFIELD (Modrm),
  462. BITFIELD (ShortForm),
  463. BITFIELD (Jump),
  464. BITFIELD (JumpDword),
  465. BITFIELD (JumpByte),
  466. BITFIELD (JumpInterSegment),
  467. BITFIELD (FloatMF),
  468. BITFIELD (FloatR),
  469. BITFIELD (FloatD),
  470. BITFIELD (Size16),
  471. BITFIELD (Size32),
  472. BITFIELD (Size64),
  473. BITFIELD (CheckRegSize),
  474. BITFIELD (IgnoreSize),
  475. BITFIELD (DefaultSize),
  476. BITFIELD (No_bSuf),
  477. BITFIELD (No_wSuf),
  478. BITFIELD (No_lSuf),
  479. BITFIELD (No_sSuf),
  480. BITFIELD (No_qSuf),
  481. BITFIELD (No_ldSuf),
  482. BITFIELD (FWait),
  483. BITFIELD (IsString),
  484. BITFIELD (BNDPrefixOk),
  485. BITFIELD (IsLockable),
  486. BITFIELD (RegKludge),
  487. BITFIELD (FirstXmm0),
  488. BITFIELD (Implicit1stXmm0),
  489. BITFIELD (RepPrefixOk),
  490. BITFIELD (HLEPrefixOk),
  491. BITFIELD (ToDword),
  492. BITFIELD (ToQword),
  493. BITFIELD (AddrPrefixOp0),
  494. BITFIELD (IsPrefix),
  495. BITFIELD (ImmExt),
  496. BITFIELD (NoRex64),
  497. BITFIELD (Rex64),
  498. BITFIELD (Ugh),
  499. BITFIELD (Vex),
  500. BITFIELD (VexVVVV),
  501. BITFIELD (VexW),
  502. BITFIELD (VexOpcode),
  503. BITFIELD (VexSources),
  504. BITFIELD (VexImmExt),
  505. BITFIELD (VecSIB),
  506. BITFIELD (SSE2AVX),
  507. BITFIELD (NoAVX),
  508. BITFIELD (EVex),
  509. BITFIELD (Masking),
  510. BITFIELD (VecESize),
  511. BITFIELD (Broadcast),
  512. BITFIELD (StaticRounding),
  513. BITFIELD (SAE),
  514. BITFIELD (Disp8MemShift),
  515. BITFIELD (NoDefMask),
  516. BITFIELD (OldGcc),
  517. BITFIELD (ATTMnemonic),
  518. BITFIELD (ATTSyntax),
  519. BITFIELD (IntelSyntax),
  520. };
  521. static bitfield operand_types[] =
  522. {
  523. BITFIELD (Reg8),
  524. BITFIELD (Reg16),
  525. BITFIELD (Reg32),
  526. BITFIELD (Reg64),
  527. BITFIELD (FloatReg),
  528. BITFIELD (RegMMX),
  529. BITFIELD (RegXMM),
  530. BITFIELD (RegYMM),
  531. BITFIELD (RegZMM),
  532. BITFIELD (RegMask),
  533. BITFIELD (Imm1),
  534. BITFIELD (Imm8),
  535. BITFIELD (Imm8S),
  536. BITFIELD (Imm16),
  537. BITFIELD (Imm32),
  538. BITFIELD (Imm32S),
  539. BITFIELD (Imm64),
  540. BITFIELD (BaseIndex),
  541. BITFIELD (Disp8),
  542. BITFIELD (Disp16),
  543. BITFIELD (Disp32),
  544. BITFIELD (Disp32S),
  545. BITFIELD (Disp64),
  546. BITFIELD (InOutPortReg),
  547. BITFIELD (ShiftCount),
  548. BITFIELD (Control),
  549. BITFIELD (Debug),
  550. BITFIELD (Test),
  551. BITFIELD (SReg2),
  552. BITFIELD (SReg3),
  553. BITFIELD (Acc),
  554. BITFIELD (FloatAcc),
  555. BITFIELD (JumpAbsolute),
  556. BITFIELD (EsSeg),
  557. BITFIELD (RegMem),
  558. BITFIELD (Mem),
  559. BITFIELD (Byte),
  560. BITFIELD (Word),
  561. BITFIELD (Dword),
  562. BITFIELD (Fword),
  563. BITFIELD (Qword),
  564. BITFIELD (Tbyte),
  565. BITFIELD (Xmmword),
  566. BITFIELD (Ymmword),
  567. BITFIELD (Zmmword),
  568. BITFIELD (Unspecified),
  569. BITFIELD (Anysize),
  570. BITFIELD (Vec_Imm4),
  571. BITFIELD (RegBND),
  572. BITFIELD (Vec_Disp8),
  573. #ifdef OTUnused
  574. BITFIELD (OTUnused),
  575. #endif
  576. };
  577. static const char *filename;
  578. static int
  579. compare (const void *x, const void *y)
  580. {
  581. const bitfield *xp = (const bitfield *) x;
  582. const bitfield *yp = (const bitfield *) y;
  583. return xp->position - yp->position;
  584. }
  585. static void
  586. fail (const char *message, ...)
  587. {
  588. va_list args;
  589. va_start (args, message);
  590. fprintf (stderr, _("%s: Error: "), program_name);
  591. vfprintf (stderr, message, args);
  592. va_end (args);
  593. xexit (1);
  594. }
  595. static void
  596. process_copyright (FILE *fp)
  597. {
  598. fprintf (fp, "/* This file is automatically generated by i386-gen. Do not edit! */\n\
  599. /* Copyright (C) 2007-2015 Free Software Foundation, Inc.\n\
  600. \n\
  601. This file is part of the GNU opcodes library.\n\
  602. \n\
  603. This library is free software; you can redistribute it and/or modify\n\
  604. it under the terms of the GNU General Public License as published by\n\
  605. the Free Software Foundation; either version 3, or (at your option)\n\
  606. any later version.\n\
  607. \n\
  608. It is distributed in the hope that it will be useful, but WITHOUT\n\
  609. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n\
  610. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\n\
  611. License for more details.\n\
  612. \n\
  613. You should have received a copy of the GNU General Public License\n\
  614. along with this program; if not, write to the Free Software\n\
  615. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\n\
  616. MA 02110-1301, USA. */\n");
  617. }
  618. /* Remove leading white spaces. */
  619. static char *
  620. remove_leading_whitespaces (char *str)
  621. {
  622. while (ISSPACE (*str))
  623. str++;
  624. return str;
  625. }
  626. /* Remove trailing white spaces. */
  627. static void
  628. remove_trailing_whitespaces (char *str)
  629. {
  630. size_t last = strlen (str);
  631. if (last == 0)
  632. return;
  633. do
  634. {
  635. last--;
  636. if (ISSPACE (str [last]))
  637. str[last] = '\0';
  638. else
  639. break;
  640. }
  641. while (last != 0);
  642. }
  643. /* Find next field separated by SEP and terminate it. Return a
  644. pointer to the one after it. */
  645. static char *
  646. next_field (char *str, char sep, char **next, char *last)
  647. {
  648. char *p;
  649. p = remove_leading_whitespaces (str);
  650. for (str = p; *str != sep && *str != '\0'; str++);
  651. *str = '\0';
  652. remove_trailing_whitespaces (p);
  653. *next = str + 1;
  654. if (p >= last)
  655. abort ();
  656. return p;
  657. }
  658. static void
  659. set_bitfield (const char *f, bitfield *array, int value,
  660. unsigned int size, int lineno)
  661. {
  662. unsigned int i;
  663. if (strcmp (f, "CpuFP") == 0)
  664. {
  665. set_bitfield("Cpu387", array, value, size, lineno);
  666. set_bitfield("Cpu287", array, value, size, lineno);
  667. f = "Cpu8087";
  668. }
  669. else if (strcmp (f, "Mmword") == 0)
  670. f= "Qword";
  671. else if (strcmp (f, "Oword") == 0)
  672. f= "Xmmword";
  673. for (i = 0; i < size; i++)
  674. if (strcasecmp (array[i].name, f) == 0)
  675. {
  676. array[i].value = value;
  677. return;
  678. }
  679. if (value)
  680. {
  681. const char *v = strchr (f, '=');
  682. if (v)
  683. {
  684. size_t n = v - f;
  685. char *end;
  686. for (i = 0; i < size; i++)
  687. if (strncasecmp (array[i].name, f, n) == 0)
  688. {
  689. value = strtol (v + 1, &end, 0);
  690. if (*end == '\0')
  691. {
  692. array[i].value = value;
  693. return;
  694. }
  695. break;
  696. }
  697. }
  698. }
  699. if (lineno != -1)
  700. fail (_("%s: %d: Unknown bitfield: %s\n"), filename, lineno, f);
  701. else
  702. fail (_("Unknown bitfield: %s\n"), f);
  703. }
  704. static void
  705. output_cpu_flags (FILE *table, bitfield *flags, unsigned int size,
  706. int macro, const char *comma, const char *indent)
  707. {
  708. unsigned int i;
  709. fprintf (table, "%s{ { ", indent);
  710. for (i = 0; i < size - 1; i++)
  711. {
  712. if (((i + 1) % 20) != 0)
  713. fprintf (table, "%d, ", flags[i].value);
  714. else
  715. fprintf (table, "%d,", flags[i].value);
  716. if (((i + 1) % 20) == 0)
  717. {
  718. /* We need \\ for macro. */
  719. if (macro)
  720. fprintf (table, " \\\n %s", indent);
  721. else
  722. fprintf (table, "\n %s", indent);
  723. }
  724. }
  725. fprintf (table, "%d } }%s\n", flags[i].value, comma);
  726. }
  727. static void
  728. process_i386_cpu_flag (FILE *table, char *flag, int macro,
  729. const char *comma, const char *indent,
  730. int lineno)
  731. {
  732. char *str, *next, *last;
  733. unsigned int i;
  734. bitfield flags [ARRAY_SIZE (cpu_flags)];
  735. /* Copy the default cpu flags. */
  736. memcpy (flags, cpu_flags, sizeof (cpu_flags));
  737. if (strcasecmp (flag, "unknown") == 0)
  738. {
  739. /* We turn on everything except for cpu64 in case of
  740. CPU_UNKNOWN_FLAGS. */
  741. for (i = 0; i < ARRAY_SIZE (flags); i++)
  742. if (flags[i].position != Cpu64)
  743. flags[i].value = 1;
  744. }
  745. else if (flag[0] == '~')
  746. {
  747. last = flag + strlen (flag);
  748. if (flag[1] == '(')
  749. {
  750. last -= 1;
  751. next = flag + 2;
  752. if (*last != ')')
  753. fail (_("%s: %d: Missing `)' in bitfield: %s\n"), filename,
  754. lineno, flag);
  755. *last = '\0';
  756. }
  757. else
  758. next = flag + 1;
  759. /* First we turn on everything except for cpu64. */
  760. for (i = 0; i < ARRAY_SIZE (flags); i++)
  761. if (flags[i].position != Cpu64)
  762. flags[i].value = 1;
  763. /* Turn off selective bits. */
  764. for (; next && next < last; )
  765. {
  766. str = next_field (next, '|', &next, last);
  767. if (str)
  768. set_bitfield (str, flags, 0, ARRAY_SIZE (flags), lineno);
  769. }
  770. }
  771. else if (strcmp (flag, "0"))
  772. {
  773. /* Turn on selective bits. */
  774. last = flag + strlen (flag);
  775. for (next = flag; next && next < last; )
  776. {
  777. str = next_field (next, '|', &next, last);
  778. if (str)
  779. set_bitfield (str, flags, 1, ARRAY_SIZE (flags), lineno);
  780. }
  781. }
  782. output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro,
  783. comma, indent);
  784. }
  785. static void
  786. output_opcode_modifier (FILE *table, bitfield *modifier, unsigned int size)
  787. {
  788. unsigned int i;
  789. fprintf (table, " { ");
  790. for (i = 0; i < size - 1; i++)
  791. {
  792. if (((i + 1) % 20) != 0)
  793. fprintf (table, "%d, ", modifier[i].value);
  794. else
  795. fprintf (table, "%d,", modifier[i].value);
  796. if (((i + 1) % 20) == 0)
  797. fprintf (table, "\n ");
  798. }
  799. fprintf (table, "%d },\n", modifier[i].value);
  800. }
  801. static void
  802. process_i386_opcode_modifier (FILE *table, char *mod, int lineno)
  803. {
  804. char *str, *next, *last;
  805. bitfield modifiers [ARRAY_SIZE (opcode_modifiers)];
  806. /* Copy the default opcode modifier. */
  807. memcpy (modifiers, opcode_modifiers, sizeof (modifiers));
  808. if (strcmp (mod, "0"))
  809. {
  810. last = mod + strlen (mod);
  811. for (next = mod; next && next < last; )
  812. {
  813. str = next_field (next, '|', &next, last);
  814. if (str)
  815. set_bitfield (str, modifiers, 1, ARRAY_SIZE (modifiers),
  816. lineno);
  817. }
  818. }
  819. output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers));
  820. }
  821. static void
  822. output_operand_type (FILE *table, bitfield *types, unsigned int size,
  823. int macro, const char *indent)
  824. {
  825. unsigned int i;
  826. fprintf (table, "{ { ");
  827. for (i = 0; i < size - 1; i++)
  828. {
  829. if (((i + 1) % 20) != 0)
  830. fprintf (table, "%d, ", types[i].value);
  831. else
  832. fprintf (table, "%d,", types[i].value);
  833. if (((i + 1) % 20) == 0)
  834. {
  835. /* We need \\ for macro. */
  836. if (macro)
  837. fprintf (table, " \\\n%s", indent);
  838. else
  839. fprintf (table, "\n%s", indent);
  840. }
  841. }
  842. fprintf (table, "%d } }", types[i].value);
  843. }
  844. static void
  845. process_i386_operand_type (FILE *table, char *op, int macro,
  846. const char *indent, int lineno)
  847. {
  848. char *str, *next, *last;
  849. bitfield types [ARRAY_SIZE (operand_types)];
  850. /* Copy the default operand type. */
  851. memcpy (types, operand_types, sizeof (types));
  852. if (strcmp (op, "0"))
  853. {
  854. last = op + strlen (op);
  855. for (next = op; next && next < last; )
  856. {
  857. str = next_field (next, '|', &next, last);
  858. if (str)
  859. set_bitfield (str, types, 1, ARRAY_SIZE (types), lineno);
  860. }
  861. }
  862. output_operand_type (table, types, ARRAY_SIZE (types), macro,
  863. indent);
  864. }
  865. static void
  866. output_i386_opcode (FILE *table, const char *name, char *str,
  867. char *last, int lineno)
  868. {
  869. unsigned int i;
  870. char *operands, *base_opcode, *extension_opcode, *opcode_length;
  871. char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
  872. /* Find number of operands. */
  873. operands = next_field (str, ',', &str, last);
  874. /* Find base_opcode. */
  875. base_opcode = next_field (str, ',', &str, last);
  876. /* Find extension_opcode. */
  877. extension_opcode = next_field (str, ',', &str, last);
  878. /* Find opcode_length. */
  879. opcode_length = next_field (str, ',', &str, last);
  880. /* Find cpu_flags. */
  881. cpu_flags = next_field (str, ',', &str, last);
  882. /* Find opcode_modifier. */
  883. opcode_modifier = next_field (str, ',', &str, last);
  884. /* Remove the first {. */
  885. str = remove_leading_whitespaces (str);
  886. if (*str != '{')
  887. abort ();
  888. str = remove_leading_whitespaces (str + 1);
  889. i = strlen (str);
  890. /* There are at least "X}". */
  891. if (i < 2)
  892. abort ();
  893. /* Remove trailing white spaces and }. */
  894. do
  895. {
  896. i--;
  897. if (ISSPACE (str[i]) || str[i] == '}')
  898. str[i] = '\0';
  899. else
  900. break;
  901. }
  902. while (i != 0);
  903. last = str + i;
  904. /* Find operand_types. */
  905. for (i = 0; i < ARRAY_SIZE (operand_types); i++)
  906. {
  907. if (str >= last)
  908. {
  909. operand_types [i] = NULL;
  910. break;
  911. }
  912. operand_types [i] = next_field (str, ',', &str, last);
  913. if (*operand_types[i] == '0')
  914. {
  915. if (i != 0)
  916. operand_types[i] = NULL;
  917. break;
  918. }
  919. }
  920. fprintf (table, " { \"%s\", %s, %s, %s, %s,\n",
  921. name, operands, base_opcode, extension_opcode,
  922. opcode_length);
  923. process_i386_cpu_flag (table, cpu_flags, 0, ",", " ", lineno);
  924. process_i386_opcode_modifier (table, opcode_modifier, lineno);
  925. fprintf (table, " { ");
  926. for (i = 0; i < ARRAY_SIZE (operand_types); i++)
  927. {
  928. if (operand_types[i] == NULL || *operand_types[i] == '0')
  929. {
  930. if (i == 0)
  931. process_i386_operand_type (table, "0", 0, "\t ", lineno);
  932. break;
  933. }
  934. if (i != 0)
  935. fprintf (table, ",\n ");
  936. process_i386_operand_type (table, operand_types[i], 0,
  937. "\t ", lineno);
  938. }
  939. fprintf (table, " } },\n");
  940. }
  941. struct opcode_hash_entry
  942. {
  943. struct opcode_hash_entry *next;
  944. char *name;
  945. char *opcode;
  946. int lineno;
  947. };
  948. /* Calculate the hash value of an opcode hash entry P. */
  949. static hashval_t
  950. opcode_hash_hash (const void *p)
  951. {
  952. struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
  953. return htab_hash_string (entry->name);
  954. }
  955. /* Compare a string Q against an opcode hash entry P. */
  956. static int
  957. opcode_hash_eq (const void *p, const void *q)
  958. {
  959. struct opcode_hash_entry *entry = (struct opcode_hash_entry *) p;
  960. const char *name = (const char *) q;
  961. return strcmp (name, entry->name) == 0;
  962. }
  963. static void
  964. process_i386_opcodes (FILE *table)
  965. {
  966. FILE *fp;
  967. char buf[2048];
  968. unsigned int i, j;
  969. char *str, *p, *last, *name;
  970. struct opcode_hash_entry **hash_slot, **entry, *next;
  971. htab_t opcode_hash_table;
  972. struct opcode_hash_entry **opcode_array;
  973. unsigned int opcode_array_size = 1024;
  974. int lineno = 0;
  975. filename = "i386-opc.tbl";
  976. fp = fopen (filename, "r");
  977. if (fp == NULL)
  978. fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
  979. xstrerror (errno));
  980. i = 0;
  981. opcode_array = (struct opcode_hash_entry **)
  982. xmalloc (sizeof (*opcode_array) * opcode_array_size);
  983. opcode_hash_table = htab_create_alloc (16, opcode_hash_hash,
  984. opcode_hash_eq, NULL,
  985. xcalloc, free);
  986. fprintf (table, "\n/* i386 opcode table. */\n\n");
  987. fprintf (table, "const insn_template i386_optab[] =\n{\n");
  988. /* Put everything on opcode array. */
  989. while (!feof (fp))
  990. {
  991. if (fgets (buf, sizeof (buf), fp) == NULL)
  992. break;
  993. lineno++;
  994. p = remove_leading_whitespaces (buf);
  995. /* Skip comments. */
  996. str = strstr (p, "//");
  997. if (str != NULL)
  998. str[0] = '\0';
  999. /* Remove trailing white spaces. */
  1000. remove_trailing_whitespaces (p);
  1001. switch (p[0])
  1002. {
  1003. case '#':
  1004. /* Ignore comments. */
  1005. case '\0':
  1006. continue;
  1007. break;
  1008. default:
  1009. break;
  1010. }
  1011. last = p + strlen (p);
  1012. /* Find name. */
  1013. name = next_field (p, ',', &str, last);
  1014. /* Get the slot in hash table. */
  1015. hash_slot = (struct opcode_hash_entry **)
  1016. htab_find_slot_with_hash (opcode_hash_table, name,
  1017. htab_hash_string (name),
  1018. INSERT);
  1019. if (*hash_slot == NULL)
  1020. {
  1021. /* It is the new one. Put it on opcode array. */
  1022. if (i >= opcode_array_size)
  1023. {
  1024. /* Grow the opcode array when needed. */
  1025. opcode_array_size += 1024;
  1026. opcode_array = (struct opcode_hash_entry **)
  1027. xrealloc (opcode_array,
  1028. sizeof (*opcode_array) * opcode_array_size);
  1029. }
  1030. opcode_array[i] = (struct opcode_hash_entry *)
  1031. xmalloc (sizeof (struct opcode_hash_entry));
  1032. opcode_array[i]->next = NULL;
  1033. opcode_array[i]->name = xstrdup (name);
  1034. opcode_array[i]->opcode = xstrdup (str);
  1035. opcode_array[i]->lineno = lineno;
  1036. *hash_slot = opcode_array[i];
  1037. i++;
  1038. }
  1039. else
  1040. {
  1041. /* Append it to the existing one. */
  1042. entry = hash_slot;
  1043. while ((*entry) != NULL)
  1044. entry = &(*entry)->next;
  1045. *entry = (struct opcode_hash_entry *)
  1046. xmalloc (sizeof (struct opcode_hash_entry));
  1047. (*entry)->next = NULL;
  1048. (*entry)->name = (*hash_slot)->name;
  1049. (*entry)->opcode = xstrdup (str);
  1050. (*entry)->lineno = lineno;
  1051. }
  1052. }
  1053. /* Process opcode array. */
  1054. for (j = 0; j < i; j++)
  1055. {
  1056. for (next = opcode_array[j]; next; next = next->next)
  1057. {
  1058. name = next->name;
  1059. str = next->opcode;
  1060. lineno = next->lineno;
  1061. last = str + strlen (str);
  1062. output_i386_opcode (table, name, str, last, lineno);
  1063. }
  1064. }
  1065. fclose (fp);
  1066. fprintf (table, " { NULL, 0, 0, 0, 0,\n");
  1067. process_i386_cpu_flag (table, "0", 0, ",", " ", -1);
  1068. process_i386_opcode_modifier (table, "0", -1);
  1069. fprintf (table, " { ");
  1070. process_i386_operand_type (table, "0", 0, "\t ", -1);
  1071. fprintf (table, " } }\n");
  1072. fprintf (table, "};\n");
  1073. }
  1074. static void
  1075. process_i386_registers (FILE *table)
  1076. {
  1077. FILE *fp;
  1078. char buf[2048];
  1079. char *str, *p, *last;
  1080. char *reg_name, *reg_type, *reg_flags, *reg_num;
  1081. char *dw2_32_num, *dw2_64_num;
  1082. int lineno = 0;
  1083. filename = "i386-reg.tbl";
  1084. fp = fopen (filename, "r");
  1085. if (fp == NULL)
  1086. fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
  1087. xstrerror (errno));
  1088. fprintf (table, "\n/* i386 register table. */\n\n");
  1089. fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
  1090. while (!feof (fp))
  1091. {
  1092. if (fgets (buf, sizeof (buf), fp) == NULL)
  1093. break;
  1094. lineno++;
  1095. p = remove_leading_whitespaces (buf);
  1096. /* Skip comments. */
  1097. str = strstr (p, "//");
  1098. if (str != NULL)
  1099. str[0] = '\0';
  1100. /* Remove trailing white spaces. */
  1101. remove_trailing_whitespaces (p);
  1102. switch (p[0])
  1103. {
  1104. case '#':
  1105. fprintf (table, "%s\n", p);
  1106. case '\0':
  1107. continue;
  1108. break;
  1109. default:
  1110. break;
  1111. }
  1112. last = p + strlen (p);
  1113. /* Find reg_name. */
  1114. reg_name = next_field (p, ',', &str, last);
  1115. /* Find reg_type. */
  1116. reg_type = next_field (str, ',', &str, last);
  1117. /* Find reg_flags. */
  1118. reg_flags = next_field (str, ',', &str, last);
  1119. /* Find reg_num. */
  1120. reg_num = next_field (str, ',', &str, last);
  1121. fprintf (table, " { \"%s\",\n ", reg_name);
  1122. process_i386_operand_type (table, reg_type, 0, "\t", lineno);
  1123. /* Find 32-bit Dwarf2 register number. */
  1124. dw2_32_num = next_field (str, ',', &str, last);
  1125. /* Find 64-bit Dwarf2 register number. */
  1126. dw2_64_num = next_field (str, ',', &str, last);
  1127. fprintf (table, ",\n %s, %s, { %s, %s } },\n",
  1128. reg_flags, reg_num, dw2_32_num, dw2_64_num);
  1129. }
  1130. fclose (fp);
  1131. fprintf (table, "};\n");
  1132. fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
  1133. }
  1134. static void
  1135. process_i386_initializers (void)
  1136. {
  1137. unsigned int i;
  1138. FILE *fp = fopen ("i386-init.h", "w");
  1139. char *init;
  1140. if (fp == NULL)
  1141. fail (_("can't create i386-init.h, errno = %s\n"),
  1142. xstrerror (errno));
  1143. process_copyright (fp);
  1144. for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++)
  1145. {
  1146. fprintf (fp, "\n#define %s \\\n", cpu_flag_init[i].name);
  1147. init = xstrdup (cpu_flag_init[i].init);
  1148. process_i386_cpu_flag (fp, init, 1, "", " ", -1);
  1149. free (init);
  1150. }
  1151. for (i = 0; i < ARRAY_SIZE (operand_type_init); i++)
  1152. {
  1153. fprintf (fp, "\n\n#define %s \\\n ", operand_type_init[i].name);
  1154. init = xstrdup (operand_type_init[i].init);
  1155. process_i386_operand_type (fp, init, 1, " ", -1);
  1156. free (init);
  1157. }
  1158. fprintf (fp, "\n");
  1159. fclose (fp);
  1160. }
  1161. /* Program options. */
  1162. #define OPTION_SRCDIR 200
  1163. struct option long_options[] =
  1164. {
  1165. {"srcdir", required_argument, NULL, OPTION_SRCDIR},
  1166. {"debug", no_argument, NULL, 'd'},
  1167. {"version", no_argument, NULL, 'V'},
  1168. {"help", no_argument, NULL, 'h'},
  1169. {0, no_argument, NULL, 0}
  1170. };
  1171. static void
  1172. print_version (void)
  1173. {
  1174. printf ("%s: version 1.0\n", program_name);
  1175. xexit (0);
  1176. }
  1177. static void
  1178. usage (FILE * stream, int status)
  1179. {
  1180. fprintf (stream, "Usage: %s [-V | --version] [-d | --debug] [--srcdir=dirname] [--help]\n",
  1181. program_name);
  1182. xexit (status);
  1183. }
  1184. int
  1185. main (int argc, char **argv)
  1186. {
  1187. extern int chdir (char *);
  1188. char *srcdir = NULL;
  1189. int c;
  1190. FILE *table;
  1191. program_name = *argv;
  1192. xmalloc_set_program_name (program_name);
  1193. while ((c = getopt_long (argc, argv, "vVdh", long_options, 0)) != EOF)
  1194. switch (c)
  1195. {
  1196. case OPTION_SRCDIR:
  1197. srcdir = optarg;
  1198. break;
  1199. case 'V':
  1200. case 'v':
  1201. print_version ();
  1202. break;
  1203. case 'd':
  1204. debug = 1;
  1205. break;
  1206. case 'h':
  1207. case '?':
  1208. usage (stderr, 0);
  1209. default:
  1210. case 0:
  1211. break;
  1212. }
  1213. if (optind != argc)
  1214. usage (stdout, 1);
  1215. if (srcdir != NULL)
  1216. if (chdir (srcdir) != 0)
  1217. fail (_("unable to change directory to \"%s\", errno = %s\n"),
  1218. srcdir, xstrerror (errno));
  1219. /* Check the unused bitfield in i386_cpu_flags. */
  1220. #ifndef CpuUnused
  1221. c = CpuNumOfBits - CpuMax - 1;
  1222. if (c)
  1223. fail (_("%d unused bits in i386_cpu_flags.\n"), c);
  1224. #endif
  1225. /* Check the unused bitfield in i386_operand_type. */
  1226. #ifndef OTUnused
  1227. c = OTNumOfBits - OTMax - 1;
  1228. if (c)
  1229. fail (_("%d unused bits in i386_operand_type.\n"), c);
  1230. #endif
  1231. qsort (cpu_flags, ARRAY_SIZE (cpu_flags), sizeof (cpu_flags [0]),
  1232. compare);
  1233. qsort (opcode_modifiers, ARRAY_SIZE (opcode_modifiers),
  1234. sizeof (opcode_modifiers [0]), compare);
  1235. qsort (operand_types, ARRAY_SIZE (operand_types),
  1236. sizeof (operand_types [0]), compare);
  1237. table = fopen ("i386-tbl.h", "w");
  1238. if (table == NULL)
  1239. fail (_("can't create i386-tbl.h, errno = %s\n"),
  1240. xstrerror (errno));
  1241. process_copyright (table);
  1242. process_i386_opcodes (table);
  1243. process_i386_registers (table);
  1244. process_i386_initializers ();
  1245. fclose (table);
  1246. exit (0);
  1247. }