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>
6 # Usage: awk -f gen-insn-attr-x86.awk x86-opcode-map.txt > inat-tables.c
8 # Awk implementation sanity check
9 function check_awk_implement() {
10 if (sprintf("%x", 0) != "0")
11 return "Your awk has a printf-format problem."
16 function clear_vars() {
28 # Implementation error checking
29 awkchecked = check_awk_implement()
30 if (awkchecked != "") {
31 print "Error: " awkchecked > "/dev/stderr"
32 print "Please try to use gawk." > "/dev/stderr"
36 # Setup generating tables
37 print "/* x86 opcode map generated from x86-opcode-map.txt */"
38 print "/* Do not change this code. */\n"
46 opnd_expr = "^[A-Za-z/]"
49 group_expr = "^Grp[0-9A-Za-z]+"
51 imm_expr = "^[IJAOL][a-z]"
52 imm_flag["Ib"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
53 imm_flag["Jb"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
54 imm_flag["Iw"] = "INAT_MAKE_IMM(INAT_IMM_WORD)"
55 imm_flag["Id"] = "INAT_MAKE_IMM(INAT_IMM_DWORD)"
56 imm_flag["Iq"] = "INAT_MAKE_IMM(INAT_IMM_QWORD)"
57 imm_flag["Ap"] = "INAT_MAKE_IMM(INAT_IMM_PTR)"
58 imm_flag["Iz"] = "INAT_MAKE_IMM(INAT_IMM_VWORD32)"
59 imm_flag["Jz"] = "INAT_MAKE_IMM(INAT_IMM_VWORD32)"
60 imm_flag["Iv"] = "INAT_MAKE_IMM(INAT_IMM_VWORD)"
61 imm_flag["Ob"] = "INAT_MOFFSET"
62 imm_flag["Ov"] = "INAT_MOFFSET"
63 imm_flag["Lx"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
65 modrm_expr = "^([CDEGMNPQRSUVW/][a-z]+|NTA|T[012])"
66 force64_expr = "\\([df]64\\)"
67 rex_expr = "^REX(\\.[XRWB]+)*"
68 fpu_expr = "^ESC" # TODO
70 lprefix1_expr = "\\((66|!F3)\\)"
71 lprefix2_expr = "\\(F3\\)"
72 lprefix3_expr = "\\((F2|!F3|66&F2)\\)"
73 lprefix_expr = "\\((66|F2|F3)\\)"
76 # All opcodes starting with lower-case 'v', 'k' or with (v1) superscript
78 vexok_opcode_expr = "^[vk].*"
79 vexok_expr = "\\(v1\\)"
80 # All opcodes with (v) superscript supports *only* VEX prefix
81 vexonly_expr = "\\(v\\)"
82 # All opcodes with (ev) superscript supports *only* EVEX prefix
83 evexonly_expr = "\\(ev\\)"
85 prefix_expr = "\\(Prefix\\)"
86 prefix_num["Operand-Size"] = "INAT_PFX_OPNDSZ"
87 prefix_num["REPNE"] = "INAT_PFX_REPNE"
88 prefix_num["REP/REPE"] = "INAT_PFX_REPE"
89 prefix_num["XACQUIRE"] = "INAT_PFX_REPNE"
90 prefix_num["XRELEASE"] = "INAT_PFX_REPE"
91 prefix_num["LOCK"] = "INAT_PFX_LOCK"
92 prefix_num["SEG=CS"] = "INAT_PFX_CS"
93 prefix_num["SEG=DS"] = "INAT_PFX_DS"
94 prefix_num["SEG=ES"] = "INAT_PFX_ES"
95 prefix_num["SEG=FS"] = "INAT_PFX_FS"
96 prefix_num["SEG=GS"] = "INAT_PFX_GS"
97 prefix_num["SEG=SS"] = "INAT_PFX_SS"
98 prefix_num["Address-Size"] = "INAT_PFX_ADDRSZ"
99 prefix_num["VEX+1byte"] = "INAT_PFX_VEX2"
100 prefix_num["VEX+2byte"] = "INAT_PFX_VEX3"
101 prefix_num["EVEX"] = "INAT_PFX_EVEX"
106 function semantic_error(msg) {
107 print "Semantic error at " NR ": " msg > "/dev/stderr"
111 function debug(msg) {
115 function array_size(arr, i,c) {
125 semantic_error("Hit Table: before EndTable:.");
130 # escape opcode table
132 for (i = 2; i <= NF; i++)
135 tname = sprintf("inat_escape_table_%d", eid)
141 # AVX/escape opcode table
145 if (tname == "") # AVX only opcode table
146 tname = sprintf("inat_avx_table_%d", $2)
148 if (aid == -1 && eid == -1) # primary opcode table
149 tname = "inat_primary_table"
155 semantic_error("No group: " $2 )
157 tname = "inat_group_table_" gid
160 function print_table(tbl,name,fmt,n)
162 print "const insn_attr_t " name " = {"
163 for (i = 0; i < n; i++) {
166 print " [" id "] = " tbl[id] ","
174 if (array_size(table) != 0) {
175 print_table(table, tname "[INAT_GROUP_TABLE_SIZE]",
177 gtable[gid,0] = tname
179 if (array_size(lptable1) != 0) {
180 print_table(lptable1, tname "_1[INAT_GROUP_TABLE_SIZE]",
182 gtable[gid,1] = tname "_1"
184 if (array_size(lptable2) != 0) {
185 print_table(lptable2, tname "_2[INAT_GROUP_TABLE_SIZE]",
187 gtable[gid,2] = tname "_2"
189 if (array_size(lptable3) != 0) {
190 print_table(lptable3, tname "_3[INAT_GROUP_TABLE_SIZE]",
192 gtable[gid,3] = tname "_3"
195 # print primary/escaped tables
196 if (array_size(table) != 0) {
197 print_table(table, tname "[INAT_OPCODE_TABLE_SIZE]",
199 etable[eid,0] = tname
201 atable[aid,0] = tname
203 if (array_size(lptable1) != 0) {
204 print_table(lptable1,tname "_1[INAT_OPCODE_TABLE_SIZE]",
206 etable[eid,1] = tname "_1"
208 atable[aid,1] = tname "_1"
210 if (array_size(lptable2) != 0) {
211 print_table(lptable2,tname "_2[INAT_OPCODE_TABLE_SIZE]",
213 etable[eid,2] = tname "_2"
215 atable[aid,2] = tname "_2"
217 if (array_size(lptable3) != 0) {
218 print_table(lptable3,tname "_3[INAT_OPCODE_TABLE_SIZE]",
220 etable[eid,3] = tname "_3"
222 atable[aid,3] = tname "_3"
229 function add_flags(old,new) {
238 # convert operands to flags.
239 function convert_operands(count,opnd, i,j,imm,mod)
243 for (j = 1; j <= count; j++) {
245 if (match(i, imm_expr) == 1) {
247 semantic_error("Unknown imm opnd: " i)
250 semantic_error("Second IMM error")
251 imm = add_flags(imm, "INAT_SCNDIMM")
254 } else if (match(i, modrm_expr))
257 return add_flags(imm, mod)
264 idx = "0x" substr($1, 1, index($1,":") - 1)
266 semantic_error("Redefine " idx " in " tname)
268 # check if escaped opcode
269 if ("escape" == $2) {
271 semantic_error("No escaped name")
273 for (i = 4; i <= NF; i++)
276 semantic_error("Redefine escape (" ref ")")
279 table[idx] = "INAT_MAKE_ESCAPE(" escape[ref] ")"
293 if (match($i, opnd_expr)) {
295 count = split($(i++), opnds, ",")
296 flags = convert_operands(count, opnds)
298 if (match($i, ext_expr))
300 if (match($i, sep_expr))
303 semantic_error($i " is not a separator")
305 # check if group opcode
306 if (match(opcode, group_expr)) {
307 if (!(opcode in group)) {
311 flags = add_flags(flags, "INAT_MAKE_GROUP(" group[opcode] ")")
313 # check force(or default) 64bit
314 if (match(ext, force64_expr))
315 flags = add_flags(flags, "INAT_FORCE64")
318 if (match(opcode, rex_expr))
319 flags = add_flags(flags, "INAT_MAKE_PREFIX(INAT_PFX_REX)")
321 # check coprocessor escape : TODO
322 if (match(opcode, fpu_expr))
323 flags = add_flags(flags, "INAT_MODRM")
326 if (match(ext, evexonly_expr))
327 flags = add_flags(flags, "INAT_VEXOK | INAT_EVEXONLY")
328 else if (match(ext, vexonly_expr))
329 flags = add_flags(flags, "INAT_VEXOK | INAT_VEXONLY")
330 else if (match(ext, vexok_expr) || match(opcode, vexok_opcode_expr))
331 flags = add_flags(flags, "INAT_VEXOK")
334 if (match(ext, prefix_expr)) {
335 if (!prefix_num[opcode])
336 semantic_error("Unknown prefix: " opcode)
337 flags = add_flags(flags, "INAT_MAKE_PREFIX(" prefix_num[opcode] ")")
339 if (length(flags) == 0)
341 # check if last prefix
342 if (match(ext, lprefix1_expr)) {
343 lptable1[idx] = add_flags(lptable1[idx],flags)
344 variant = "INAT_VARIANT"
346 if (match(ext, lprefix2_expr)) {
347 lptable2[idx] = add_flags(lptable2[idx],flags)
348 variant = "INAT_VARIANT"
350 if (match(ext, lprefix3_expr)) {
351 lptable3[idx] = add_flags(lptable3[idx],flags)
352 variant = "INAT_VARIANT"
354 if (!match(ext, lprefix_expr)){
355 table[idx] = add_flags(table[idx],flags)
359 table[idx] = add_flags(table[idx],variant)
363 if (awkchecked != "")
366 print "#ifndef __BOOT_COMPRESSED\n"
368 # print escape opcode map's array
369 print "/* Escape opcode map array */"
370 print "const insn_attr_t * const inat_escape_tables[INAT_ESC_MAX + 1]" \
371 "[INAT_LSTPFX_MAX + 1] = {"
372 for (i = 0; i < geid; i++)
373 for (j = 0; j < max_lprefix; j++)
375 print " ["i"]["j"] = "etable[i,j]","
377 # print group opcode map's array
378 print "/* Group opcode map array */"
379 print "const insn_attr_t * const inat_group_tables[INAT_GRP_MAX + 1]"\
380 "[INAT_LSTPFX_MAX + 1] = {"
381 for (i = 0; i < ggid; i++)
382 for (j = 0; j < max_lprefix; j++)
384 print " ["i"]["j"] = "gtable[i,j]","
386 # print AVX opcode map's array
387 print "/* AVX opcode map array */"
388 print "const insn_attr_t * const inat_avx_tables[X86_VEX_M_MAX + 1]"\
389 "[INAT_LSTPFX_MAX + 1] = {"
390 for (i = 0; i < gaid; i++)
391 for (j = 0; j < max_lprefix; j++)
393 print " ["i"]["j"] = "atable[i,j]","
396 print "#else /* !__BOOT_COMPRESSED */\n"
398 print "/* Escape opcode map array */"
399 print "static const insn_attr_t *inat_escape_tables[INAT_ESC_MAX + 1]" \
400 "[INAT_LSTPFX_MAX + 1];"
403 print "/* Group opcode map array */"
404 print "static const insn_attr_t *inat_group_tables[INAT_GRP_MAX + 1]"\
405 "[INAT_LSTPFX_MAX + 1];"
408 print "/* AVX opcode map array */"
409 print "static const insn_attr_t *inat_avx_tables[X86_VEX_M_MAX + 1]"\
410 "[INAT_LSTPFX_MAX + 1];"
413 print "static void inat_init_tables(void)"
416 # print escape opcode map's array
417 print "\t/* Print Escape opcode map array */"
418 for (i = 0; i < geid; i++)
419 for (j = 0; j < max_lprefix; j++)
421 print "\tinat_escape_tables["i"]["j"] = "etable[i,j]";"
424 # print group opcode map's array
425 print "\t/* Print Group opcode map array */"
426 for (i = 0; i < ggid; i++)
427 for (j = 0; j < max_lprefix; j++)
429 print "\tinat_group_tables["i"]["j"] = "gtable[i,j]";"
431 # print AVX opcode map's array
432 print "\t/* Print AVX opcode map array */"
433 for (i = 0; i < gaid; i++)
434 for (j = 0; j < max_lprefix; j++)
436 print "\tinat_avx_tables["i"]["j"] = "atable[i,j]";"