gen-insn-attr-x86.awk 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #!/bin/awk -f
  2. # SPDX-License-Identifier: GPL-2.0
  3. # gen-insn-attr-x86.awk: Instruction attribute table generator
  4. # Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. #
  6. # Usage: awk -f gen-insn-attr-x86.awk x86-opcode-map.txt > inat-tables.c
  7. # Awk implementation sanity check
  8. function check_awk_implement() {
  9. if (sprintf("%x", 0) != "0")
  10. return "Your awk has a printf-format problem."
  11. return ""
  12. }
  13. # Clear working vars
  14. function clear_vars() {
  15. delete table
  16. delete lptable2
  17. delete lptable1
  18. delete lptable3
  19. eid = -1 # escape id
  20. gid = -1 # group id
  21. aid = -1 # AVX id
  22. tname = ""
  23. }
  24. BEGIN {
  25. # Implementation error checking
  26. awkchecked = check_awk_implement()
  27. if (awkchecked != "") {
  28. print "Error: " awkchecked > "/dev/stderr"
  29. print "Please try to use gawk." > "/dev/stderr"
  30. exit 1
  31. }
  32. # Setup generating tables
  33. print "/* x86 opcode map generated from x86-opcode-map.txt */"
  34. print "/* Do not change this code. */\n"
  35. ggid = 1
  36. geid = 1
  37. gaid = 0
  38. delete etable
  39. delete gtable
  40. delete atable
  41. opnd_expr = "^[A-Za-z/]"
  42. ext_expr = "^\\("
  43. sep_expr = "^\\|$"
  44. group_expr = "^Grp[0-9A-Za-z]+"
  45. imm_expr = "^[IJAOL][a-z]"
  46. imm_flag["Ib"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
  47. imm_flag["Jb"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
  48. imm_flag["Iw"] = "INAT_MAKE_IMM(INAT_IMM_WORD)"
  49. imm_flag["Id"] = "INAT_MAKE_IMM(INAT_IMM_DWORD)"
  50. imm_flag["Iq"] = "INAT_MAKE_IMM(INAT_IMM_QWORD)"
  51. imm_flag["Ap"] = "INAT_MAKE_IMM(INAT_IMM_PTR)"
  52. imm_flag["Iz"] = "INAT_MAKE_IMM(INAT_IMM_VWORD32)"
  53. imm_flag["Jz"] = "INAT_MAKE_IMM(INAT_IMM_VWORD32)"
  54. imm_flag["Iv"] = "INAT_MAKE_IMM(INAT_IMM_VWORD)"
  55. imm_flag["Ob"] = "INAT_MOFFSET"
  56. imm_flag["Ov"] = "INAT_MOFFSET"
  57. imm_flag["Lx"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
  58. modrm_expr = "^([CDEGMNPQRSUVW/][a-z]+|NTA|T[012])"
  59. force64_expr = "\\([df]64\\)"
  60. rex_expr = "^REX(\\.[XRWB]+)*"
  61. fpu_expr = "^ESC" # TODO
  62. lprefix1_expr = "\\((66|!F3)\\)"
  63. lprefix2_expr = "\\(F3\\)"
  64. lprefix3_expr = "\\((F2|!F3|66&F2)\\)"
  65. lprefix_expr = "\\((66|F2|F3)\\)"
  66. max_lprefix = 4
  67. # All opcodes starting with lower-case 'v', 'k' or with (v1) superscript
  68. # accepts VEX prefix
  69. vexok_opcode_expr = "^[vk].*"
  70. vexok_expr = "\\(v1\\)"
  71. # All opcodes with (v) superscript supports *only* VEX prefix
  72. vexonly_expr = "\\(v\\)"
  73. # All opcodes with (ev) superscript supports *only* EVEX prefix
  74. evexonly_expr = "\\(ev\\)"
  75. prefix_expr = "\\(Prefix\\)"
  76. prefix_num["Operand-Size"] = "INAT_PFX_OPNDSZ"
  77. prefix_num["REPNE"] = "INAT_PFX_REPNE"
  78. prefix_num["REP/REPE"] = "INAT_PFX_REPE"
  79. prefix_num["XACQUIRE"] = "INAT_PFX_REPNE"
  80. prefix_num["XRELEASE"] = "INAT_PFX_REPE"
  81. prefix_num["LOCK"] = "INAT_PFX_LOCK"
  82. prefix_num["SEG=CS"] = "INAT_PFX_CS"
  83. prefix_num["SEG=DS"] = "INAT_PFX_DS"
  84. prefix_num["SEG=ES"] = "INAT_PFX_ES"
  85. prefix_num["SEG=FS"] = "INAT_PFX_FS"
  86. prefix_num["SEG=GS"] = "INAT_PFX_GS"
  87. prefix_num["SEG=SS"] = "INAT_PFX_SS"
  88. prefix_num["Address-Size"] = "INAT_PFX_ADDRSZ"
  89. prefix_num["VEX+1byte"] = "INAT_PFX_VEX2"
  90. prefix_num["VEX+2byte"] = "INAT_PFX_VEX3"
  91. prefix_num["EVEX"] = "INAT_PFX_EVEX"
  92. clear_vars()
  93. }
  94. function semantic_error(msg) {
  95. print "Semantic error at " NR ": " msg > "/dev/stderr"
  96. exit 1
  97. }
  98. function debug(msg) {
  99. print "DEBUG: " msg
  100. }
  101. function array_size(arr, i,c) {
  102. c = 0
  103. for (i in arr)
  104. c++
  105. return c
  106. }
  107. /^Table:/ {
  108. print "/* " $0 " */"
  109. if (tname != "")
  110. semantic_error("Hit Table: before EndTable:.");
  111. }
  112. /^Referrer:/ {
  113. if (NF != 1) {
  114. # escape opcode table
  115. ref = ""
  116. for (i = 2; i <= NF; i++)
  117. ref = ref $i
  118. eid = escape[ref]
  119. tname = sprintf("inat_escape_table_%d", eid)
  120. }
  121. }
  122. /^AVXcode:/ {
  123. if (NF != 1) {
  124. # AVX/escape opcode table
  125. aid = $2
  126. if (gaid <= aid)
  127. gaid = aid + 1
  128. if (tname == "") # AVX only opcode table
  129. tname = sprintf("inat_avx_table_%d", $2)
  130. }
  131. if (aid == -1 && eid == -1) # primary opcode table
  132. tname = "inat_primary_table"
  133. }
  134. /^GrpTable:/ {
  135. print "/* " $0 " */"
  136. if (!($2 in group))
  137. semantic_error("No group: " $2 )
  138. gid = group[$2]
  139. tname = "inat_group_table_" gid
  140. }
  141. function print_table(tbl,name,fmt,n)
  142. {
  143. print "const insn_attr_t " name " = {"
  144. for (i = 0; i < n; i++) {
  145. id = sprintf(fmt, i)
  146. if (tbl[id])
  147. print " [" id "] = " tbl[id] ","
  148. }
  149. print "};"
  150. }
  151. /^EndTable/ {
  152. if (gid != -1) {
  153. # print group tables
  154. if (array_size(table) != 0) {
  155. print_table(table, tname "[INAT_GROUP_TABLE_SIZE]",
  156. "0x%x", 8)
  157. gtable[gid,0] = tname
  158. }
  159. if (array_size(lptable1) != 0) {
  160. print_table(lptable1, tname "_1[INAT_GROUP_TABLE_SIZE]",
  161. "0x%x", 8)
  162. gtable[gid,1] = tname "_1"
  163. }
  164. if (array_size(lptable2) != 0) {
  165. print_table(lptable2, tname "_2[INAT_GROUP_TABLE_SIZE]",
  166. "0x%x", 8)
  167. gtable[gid,2] = tname "_2"
  168. }
  169. if (array_size(lptable3) != 0) {
  170. print_table(lptable3, tname "_3[INAT_GROUP_TABLE_SIZE]",
  171. "0x%x", 8)
  172. gtable[gid,3] = tname "_3"
  173. }
  174. } else {
  175. # print primary/escaped tables
  176. if (array_size(table) != 0) {
  177. print_table(table, tname "[INAT_OPCODE_TABLE_SIZE]",
  178. "0x%02x", 256)
  179. etable[eid,0] = tname
  180. if (aid >= 0)
  181. atable[aid,0] = tname
  182. }
  183. if (array_size(lptable1) != 0) {
  184. print_table(lptable1,tname "_1[INAT_OPCODE_TABLE_SIZE]",
  185. "0x%02x", 256)
  186. etable[eid,1] = tname "_1"
  187. if (aid >= 0)
  188. atable[aid,1] = tname "_1"
  189. }
  190. if (array_size(lptable2) != 0) {
  191. print_table(lptable2,tname "_2[INAT_OPCODE_TABLE_SIZE]",
  192. "0x%02x", 256)
  193. etable[eid,2] = tname "_2"
  194. if (aid >= 0)
  195. atable[aid,2] = tname "_2"
  196. }
  197. if (array_size(lptable3) != 0) {
  198. print_table(lptable3,tname "_3[INAT_OPCODE_TABLE_SIZE]",
  199. "0x%02x", 256)
  200. etable[eid,3] = tname "_3"
  201. if (aid >= 0)
  202. atable[aid,3] = tname "_3"
  203. }
  204. }
  205. print ""
  206. clear_vars()
  207. }
  208. function add_flags(old,new) {
  209. if (old && new)
  210. return old " | " new
  211. else if (old)
  212. return old
  213. else
  214. return new
  215. }
  216. # convert operands to flags.
  217. function convert_operands(count,opnd, i,j,imm,mod)
  218. {
  219. imm = null
  220. mod = null
  221. for (j = 1; j <= count; j++) {
  222. i = opnd[j]
  223. if (match(i, imm_expr) == 1) {
  224. if (!imm_flag[i])
  225. semantic_error("Unknown imm opnd: " i)
  226. if (imm) {
  227. if (i != "Ib")
  228. semantic_error("Second IMM error")
  229. imm = add_flags(imm, "INAT_SCNDIMM")
  230. } else
  231. imm = imm_flag[i]
  232. } else if (match(i, modrm_expr))
  233. mod = "INAT_MODRM"
  234. }
  235. return add_flags(imm, mod)
  236. }
  237. /^[0-9a-f]+:/ {
  238. if (NR == 1)
  239. next
  240. # get index
  241. idx = "0x" substr($1, 1, index($1,":") - 1)
  242. if (idx in table)
  243. semantic_error("Redefine " idx " in " tname)
  244. # check if escaped opcode
  245. if ("escape" == $2) {
  246. if ($3 != "#")
  247. semantic_error("No escaped name")
  248. ref = ""
  249. for (i = 4; i <= NF; i++)
  250. ref = ref $i
  251. if (ref in escape)
  252. semantic_error("Redefine escape (" ref ")")
  253. escape[ref] = geid
  254. geid++
  255. table[idx] = "INAT_MAKE_ESCAPE(" escape[ref] ")"
  256. next
  257. }
  258. variant = null
  259. # converts
  260. i = 2
  261. while (i <= NF) {
  262. opcode = $(i++)
  263. delete opnds
  264. ext = null
  265. flags = null
  266. opnd = null
  267. # parse one opcode
  268. if (match($i, opnd_expr)) {
  269. opnd = $i
  270. count = split($(i++), opnds, ",")
  271. flags = convert_operands(count, opnds)
  272. }
  273. if (match($i, ext_expr))
  274. ext = $(i++)
  275. if (match($i, sep_expr))
  276. i++
  277. else if (i < NF)
  278. semantic_error($i " is not a separator")
  279. # check if group opcode
  280. if (match(opcode, group_expr)) {
  281. if (!(opcode in group)) {
  282. group[opcode] = ggid
  283. ggid++
  284. }
  285. flags = add_flags(flags, "INAT_MAKE_GROUP(" group[opcode] ")")
  286. }
  287. # check force(or default) 64bit
  288. if (match(ext, force64_expr))
  289. flags = add_flags(flags, "INAT_FORCE64")
  290. # check REX prefix
  291. if (match(opcode, rex_expr))
  292. flags = add_flags(flags, "INAT_MAKE_PREFIX(INAT_PFX_REX)")
  293. # check coprocessor escape : TODO
  294. if (match(opcode, fpu_expr))
  295. flags = add_flags(flags, "INAT_MODRM")
  296. # check VEX codes
  297. if (match(ext, evexonly_expr))
  298. flags = add_flags(flags, "INAT_VEXOK | INAT_EVEXONLY")
  299. else if (match(ext, vexonly_expr))
  300. flags = add_flags(flags, "INAT_VEXOK | INAT_VEXONLY")
  301. else if (match(ext, vexok_expr) || match(opcode, vexok_opcode_expr))
  302. flags = add_flags(flags, "INAT_VEXOK")
  303. # check prefixes
  304. if (match(ext, prefix_expr)) {
  305. if (!prefix_num[opcode])
  306. semantic_error("Unknown prefix: " opcode)
  307. flags = add_flags(flags, "INAT_MAKE_PREFIX(" prefix_num[opcode] ")")
  308. }
  309. if (length(flags) == 0)
  310. continue
  311. # check if last prefix
  312. if (match(ext, lprefix1_expr)) {
  313. lptable1[idx] = add_flags(lptable1[idx],flags)
  314. variant = "INAT_VARIANT"
  315. }
  316. if (match(ext, lprefix2_expr)) {
  317. lptable2[idx] = add_flags(lptable2[idx],flags)
  318. variant = "INAT_VARIANT"
  319. }
  320. if (match(ext, lprefix3_expr)) {
  321. lptable3[idx] = add_flags(lptable3[idx],flags)
  322. variant = "INAT_VARIANT"
  323. }
  324. if (!match(ext, lprefix_expr)){
  325. table[idx] = add_flags(table[idx],flags)
  326. }
  327. }
  328. if (variant)
  329. table[idx] = add_flags(table[idx],variant)
  330. }
  331. END {
  332. if (awkchecked != "")
  333. exit 1
  334. # print escape opcode map's array
  335. print "/* Escape opcode map array */"
  336. print "const insn_attr_t * const inat_escape_tables[INAT_ESC_MAX + 1]" \
  337. "[INAT_LSTPFX_MAX + 1] = {"
  338. for (i = 0; i < geid; i++)
  339. for (j = 0; j < max_lprefix; j++)
  340. if (etable[i,j])
  341. print " ["i"]["j"] = "etable[i,j]","
  342. print "};\n"
  343. # print group opcode map's array
  344. print "/* Group opcode map array */"
  345. print "const insn_attr_t * const inat_group_tables[INAT_GRP_MAX + 1]"\
  346. "[INAT_LSTPFX_MAX + 1] = {"
  347. for (i = 0; i < ggid; i++)
  348. for (j = 0; j < max_lprefix; j++)
  349. if (gtable[i,j])
  350. print " ["i"]["j"] = "gtable[i,j]","
  351. print "};\n"
  352. # print AVX opcode map's array
  353. print "/* AVX opcode map array */"
  354. print "const insn_attr_t * const inat_avx_tables[X86_VEX_M_MAX + 1]"\
  355. "[INAT_LSTPFX_MAX + 1] = {"
  356. for (i = 0; i < gaid; i++)
  357. for (j = 0; j < max_lprefix; j++)
  358. if (atable[i,j])
  359. print " ["i"]["j"] = "atable[i,j]","
  360. print "};"
  361. }