| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
if len(sys.argv) == 1: |
| 9 |
print("Syntax: %s <GenAsmMatcher.inc>" %sys.argv[0]) |
| 10 |
sys.exit(1) |
| 11 |
|
| 12 |
print("""/* Capstone Disassembly Engine, http://www.capstone-engine.org */ |
| 13 |
/* This is auto-gen data for Capstone disassembly engine (www.capstone-engine.org) */ |
| 14 |
/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */ |
| 15 |
""") |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
def extract_insn(line): |
| 24 |
tmp = line.split(',') |
| 25 |
insn_raw = tmp[1].strip() |
| 26 |
insn_mnem = tmp[0].split(' ')[3] |
| 27 |
|
| 28 |
if '.' in insn_mnem: |
| 29 |
tmp = insn_mnem.split('.') |
| 30 |
insn_mnem = tmp[0] |
| 31 |
tmp = insn_raw.split('::') |
| 32 |
arch = tmp[0] |
| 33 |
|
| 34 |
if arch.upper() == 'AARCH64': |
| 35 |
arch = 'ARM64' |
| 36 |
return (arch, insn_mnem, tmp[1]) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
def extract_matcher(filename): |
| 43 |
f = open(filename) |
| 44 |
lines = f.readlines() |
| 45 |
f.close() |
| 46 |
|
| 47 |
match_count = 0 |
| 48 |
mnem_list = [] |
| 49 |
insn_id_list = {} |
| 50 |
arch = None |
| 51 |
first_insn = None |
| 52 |
|
| 53 |
pattern = None |
| 54 |
|
| 55 |
for line in lines: |
| 56 |
if 'MatchEntry MatchTable0[] = {' in line.strip(): |
| 57 |
pattern = 'MatchEntry MatchTable0[] = {' |
| 58 |
elif 'MatchEntry MatchTable1[] = {' in line.strip(): |
| 59 |
pattern = 'MatchEntry MatchTable1[] = {' |
| 60 |
|
| 61 |
break |
| 62 |
|
| 63 |
|
| 64 |
for line in lines: |
| 65 |
line = line.rstrip() |
| 66 |
|
| 67 |
if len(line.strip()) == 0: |
| 68 |
continue |
| 69 |
|
| 70 |
if pattern in line.strip(): |
| 71 |
match_count += 1 |
| 72 |
|
| 73 |
continue |
| 74 |
|
| 75 |
line = line.strip() |
| 76 |
if match_count == 1: |
| 77 |
if line == '};': |
| 78 |
|
| 79 |
break |
| 80 |
else: |
| 81 |
_arch, mnem, insn_id = extract_insn(line) |
| 82 |
|
| 83 |
if not mnem.startswith('__'): |
| 84 |
|
| 85 |
if mnem.endswith('-') or mnem.endswith('+'): |
| 86 |
mnem = mnem[:-1] |
| 87 |
|
| 88 |
if not first_insn: |
| 89 |
arch, first_insn = _arch, insn_id |
| 90 |
|
| 91 |
if not insn_id in insn_id_list: |
| 92 |
|
| 93 |
insn_id_list[insn_id] = mnem |
| 94 |
|
| 95 |
if not mnem in mnem_list: |
| 96 |
print("%s_INS_%s," %(arch, mnem.upper())) |
| 97 |
mnem_list.append(mnem) |
| 98 |
|
| 99 |
|
| 100 |
return arch, first_insn, insn_id_list |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
arch, first_insn, insn_id_list = extract_matcher(sys.argv[1]) |