aboutsummaryrefslogtreecommitdiffstats
path: root/capstone/suite/synctools/instrinfo.py
blob: 17ac4ee4dc95dedad992802528ec6440ef5b9dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
# convert LLVM GenInstrInfo.inc for Capstone disassembler.
# by Nguyen Anh Quynh, 2019

import sys

if len(sys.argv) == 1:
    print("Syntax: %s <GenInstrInfo.inc> <AsmMatcher.info>" %sys.argv[0])
    sys.exit(1)


# lib/Target/X86/X86GenAsmMatcher.inc
# static const MatchEntry MatchTable1[] = {
#  { 0 /* aaa */, X86::AAA, Convert_NoOperands, Feature_Not64BitMode, {  }, },

# return (arch, mnem)
def extract_insn(line):
    tmp = line.split(',')
    insn_raw = tmp[1].strip()
    insn_mnem = tmp[0].split(' ')[3]
    # X86 mov.s
    if '.' in insn_mnem:
        tmp = insn_mnem.split('.')
        insn_mnem = tmp[0]
    tmp = insn_raw.split('::')
    arch = tmp[0]
    # AArch64 -> ARM64
    if arch.upper() == 'AArch64':
        arch = 'ARM64'
    return (arch, insn_mnem)

# get (arch, first insn) from MatchTable
def get_first_insn(filename):
    f = open(filename)
    lines = f.readlines()
    f.close()
    count = 0
    for line in lines:
        line = line.strip()
    
        if len(line) == 0:
            continue
    
        # Intel syntax in Table1
        if 'MatchEntry MatchTable1[] = {' in line:
            count += 1
            #print(line.strip())
            continue
    
        if count == 1:
            arch, mnem = extract_insn(line)
            return (arch, mnem)

    return (None, None)


arch, first_insn = get_first_insn(sys.argv[2])
first_insn = first_insn.upper()
arch = arch.upper()
#print(arch, first_insn)

print("""
/* Capstone Disassembly Engine, http://www.capstone-engine.org */
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */

/*===- TableGen'erated file -------------------------------------*- C++ -*-===*\
|*                                                                            *|
|* Target Instruction Enum Values and Descriptors                             *|
|*                                                                            *|
|* Automatically generated file, do not edit!                                 *|
|*                                                                            *|
\*===----------------------------------------------------------------------===*/

#ifdef GET_INSTRINFO_ENUM
#undef GET_INSTRINFO_ENUM
""")

enum_count = 0
meet_insn = False

f = open(sys.argv[1])
lines = f.readlines()
f.close()

# 1st enum is register enum
for line in lines:
    line = line.rstrip()

    if len(line.strip()) == 0:
        continue

    if line.strip() == 'enum {':
        enum_count += 1
        print(line.strip())
        continue

    line = line.strip()
    if enum_count == 1:
        if line == '};':
            # done with first enum
            break
        else:
            insn = None
            if meet_insn:
                # enum items
                insn = line
            elif line.startswith(first_insn):
                insn = line
                meet_insn = True
            if insn:
                print("\t%s_%s" %(arch, line))

print('};\n')

print("#endif // GET_INSTRINFO_ENUM")