📄 opgen.py
字号:
# # split opcode definition into # 1. prefixes (pfx) # 2. opcode bytes (opc) # 3. operands # if len(parts) == 1: opc = parts[0].split() elif len(parts) == 2: opc = parts[0].split() opr = parts[1].split() for o in opc: if o in pfx_dict: pfx = parts[0].split() opc = parts[1].split() break elif len(parts) == 3: pfx = parts[0].split() opc = parts[1].split() opr = parts[2].split() else: print "error: invalid opcode definition of %s\n" % mnemonic sys.exit(-1) # Convert opcodes to upper case for i in range(len(opc)): opc[i] = opc[i].upper() # # check for special cases of instruction translation # and ignore them # if mnemonic == 'pause' or \ ( mnemonic == 'nop' and opc[0] == '90' ) or \ mnemonic == 'invalid' or \ mnemonic == 'db' : continue # # Convert prefix # for p in pfx: if not ( p in pfx_dict.keys() ): print "error: invalid prefix specification: %s \n" % pfx pfx_c.append( pfx_dict[p] ) if len(pfx) == 0: pfx_c.append( "P_none" ) pfx = "|".join( pfx_c ) # # Convert operands # opr_c = [ "O_NONE", "O_NONE", "O_NONE" ] for i in range(len(opr)): if not (opr[i] in operand_dict.keys()): print "error: invalid operand declaration: %s\n" % opr[i] opr_c[i] = "O_" + opr[i] opr = "%-8s %-8s %s" % (opr_c[0] + ",", opr_c[1] + ",", opr_c[2]) table_sse = '' table_name = 'itab__1byte' table_size = 256 table_index = '' for op in opc: if op[0:3] == 'SSE': table_sse = op elif op == '0F' and len(table_sse): table_name = "itab__pfx_%s__0f" % table_sse table_size = 256 table_sse = '' elif op == '0F': table_name = "itab__0f" table_size = 256 elif op == '38' and (table_name == "itab__0f" or table_name == "itab__pfx_SSE66__0f"): table_index = '38' tables[table_name][table_index] = { \ 'type' : 'grp_3byte', \ 'name' : "%s__38" % (table_name) \ } table_name = tables[table_name][table_index]['name'] table_size = 256 elif op[0:5] == '/X87=': tables[table_name][table_index] = { \ 'type' : 'grp_x87', \ 'name' : "%s__op_%s__x87" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] table_index = "%02X" % int(op[5:7], 16) table_size = 64 elif op[0:4] == '/RM=': tables[table_name][table_index] = { \ 'type' : 'grp_rm', \ 'name' : "%s__op_%s__rm" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] table_index = "%02X" % int(op[4:6]) table_size = 8 elif op[0:5] == '/MOD=': tables[table_name][table_index] = { \ 'type' : 'grp_mod', \ 'name' : "%s__op_%s__mod" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] if len(op) == 8: v = op[5:8] else: v = op[5:7] mod_dict = { '!11' : 0, '11' : 1 } table_index = "%02X" % int(mod_dict[v]) table_size = 2 elif op[0:2] == '/O': tables[table_name][table_index] = { \ 'type' : 'grp_osize', \ 'name' : "%s__op_%s__osize" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] table_index = "%02X" % int(mode_dict[op[2:4]]) table_size = 3 elif op[0:2] == '/A': tables[table_name][table_index] = { \ 'type' : 'grp_asize', \ 'name' : "%s__op_%s__asize" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] table_index = "%02X" % int(mode_dict[op[2:4]]) table_size = 3 elif op[0:2] == '/M': tables[table_name][table_index] = { \ 'type' : 'grp_mode', \ 'name' : "%s__op_%s__mode" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] table_index = "%02X" % int(mode_dict[op[2:4]]) table_size = 3 elif op[0:6] == '/3DNOW': table_name = "itab__3dnow" table_size = 256 elif op[0:1] == '/': tables[table_name][table_index] = { \ 'type' : 'grp_reg', \ 'name' : "%s__op_%s__reg" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] table_index = "%02X" % int(op[1:2]) table_size = 8 else: table_index = op mktab(table_name, table_size) if len(vendor): tables[table_name][table_index] = { \ 'type' : 'grp_vendor', \ 'name' : "%s__op_%s__vendor" % (table_name, table_index) \ } table_name = tables[table_name][table_index]['name'] table_index = vend_dict[vendor] table_size = 3 mktab(table_name, table_size) tables[table_name][table_index] = { \ 'type' : 'leaf', \ 'name' : mnemonic, \ 'pfx' : pfx, \ 'opr' : opr, \ 'flags' : flags \ } if len(vendor): tables[table_name][vend_dict['ANY']] = { \ 'type' : 'leaf', \ 'name' : mnemonic, \ 'pfx' : pfx, \ 'opr' : opr, \ 'flags' : flags \ }# ---------------------------------------------------------------------# Generate itab.h# ---------------------------------------------------------------------f = open("itab.h", "w")f.write('''/* itab.h -- auto generated by opgen.py, do not edit. */#ifndef UD_ITAB_H#define UD_ITAB_H''')## Generate enumeration of size constants#siz_constants.sort()f.write('''''')f.write("\nenum ud_itab_vendor_index {\n" )f.write(" ITAB__VENDOR_INDX__AMD,\n" )f.write(" ITAB__VENDOR_INDX__INTEL,\n" )f.write(" ITAB__VENDOR_INDX__ANY,\n" )f.write("};\n\n")f.write("\nenum ud_itab_mode_index {\n" )f.write(" ITAB__MODE_INDX__16,\n" )f.write(" ITAB__MODE_INDX__32,\n" )f.write(" ITAB__MODE_INDX__64\n" )f.write("};\n\n")f.write("\nenum ud_itab_mod_index {\n" )f.write(" ITAB__MOD_INDX__NOT_11,\n" )f.write(" ITAB__MOD_INDX__11\n" )f.write("};\n\n")## Generate enumeration of the tables#table_names = tables.keys()table_names.sort();f.write( "\nenum ud_itab_index {\n" )for name in table_names: f.write(" %s,\n" % name.upper() );f.write( "};\n\n" ) ## Generate mnemonics list#f.write("\nenum __attribute__((packed)) ud_mnemonic_code {\n")for m in mnm_list: f.write(" UD_I%s,\n" % m)for m in spl_mnm_types: f.write(" UD_I%s,\n" % m)f.write("};\n\n")## Generate operand definitions#f.write("\n/* itab entry operand definitions */\n\n");operands = operand_dict.keys()operands.sort()for o in operands: f.write("#define O_%-7s { %-12s %-8s }\n" % (o, operand_dict[o][0] + ",", operand_dict[o][1]));f.write("\n");## Generate struct defs#f.write( \'''extern const char* ud_mnemonics_str[];;extern struct ud_itab_entry* ud_itab_list[];''' )f.write("#endif\n")f.close()# ---------------------------------------------------------------------# Generate itab.c# ---------------------------------------------------------------------f = open("itab.c", "w")f.write('''/* itab.c -- auto generated by opgen.py, do not edit. */#include "types.h"#include "decode.h"#include "itab.h"''')## generate mnemonic list#f.write("const char * ud_mnemonics_str[] = {\n")for m in mnm_list: f.write(" \"%s\",\n" % m )f.write("};\n\n")## generate instruction tables#f.write("\n")for t in table_names: f.write("\nstatic struct ud_itab_entry " + t.lower() + "[%d] = {\n" % table_sizes[t]); for i in range(int(table_sizes[t])): index = "%02X" % i if index in tables[t]: f.write(centry(index, tables[t][index])) else: f.write(centry(index,{"type":"invalid"})) f.write("};\n");## write the instruction table list#f.write( "\n/* the order of this table matches enum ud_itab_index */")f.write( "\nstruct ud_itab_entry * ud_itab_list[] = {\n" )for name in table_names: f.write( " %s,\n" % name.lower() )f.write( "};\n" );f.close();# vim:expandtab# vim:sw=4# vim:ts=4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -