gen-insn-attr-x86.awk 9.1 KB

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