aboutsummaryrefslogtreecommitdiffstats
path: root/capstone/suite/synctools/systemregister.py
blob: ed3e94352f220d0886a1c00f494b5a53e87b7f80 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/python
# convert LLVM GenSystemRegister.inc for Capstone disassembler.
# by Nguyen Anh Quynh, 2019

import sys

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

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

#arch = sys.argv[2].upper()

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

/*===- TableGen'erated file -------------------------------------*- C++ -*-===*\
|*                                                                            *|
|* GenSystemRegister Source Fragment                                          *|
|*                                                                            *|
|* Automatically generated file, do not edit!                                 *|
|*                                                                            *|
\*===----------------------------------------------------------------------===*/

""")

# extract BankedRegValues enum
count = 0
for line in lines:
    line = line.rstrip()

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

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

    line = line.strip()
    if count == 1:
        if line == '};':
            # done with first enum
            break
        else:
            # skip pseudo instructions
            print("\t%s" %(line))

print('};\n')

# extract MClassSysRegsList
count = 0
for line in lines:
    line = line.rstrip()

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

    if 'MClassSysRegsList[]' in line:
        count += 1
        print('static const MClassSysReg MClassSysRegsList[] = {')
        continue

    if count == 1:
        if line.strip() == '};':
            # done with first enum
            break
        else:
            # enum items
            # { "apsr_g", 0x400, 0x0, 0x400,  {ARM::FeatureDSP}  }, // 0
            line2 = line.replace('::', '_')
            sysreg = line2[line2.index('"') + 1 : line2.index('",')]
            tmp = line2.split(',')
            print("%s, ARM_SYSREG_%s%s" %(line2[:line2.index('",') + 1], sysreg.upper(), line2[line2.index('",') + 1 :]))

print('};\n')

# extract BankedRegsList
count = 0
for line in lines:
    line = line.rstrip()

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

    if 'BankedRegsList[]' in line:
        count += 1
        print('static const BankedReg BankedRegsList[] = {')
        continue

    if count == 1:
        if line.strip() == '};':
            # done with first enum
            break
        else:
            # enum items
            line2 = line.replace('::', '_')
            sysreg = line2[line2.index('"') + 1 : line2.index('",')]
            tmp = line2.split(',')
            print("%s, ARM_SYSREG_%s%s" %(line2[:line2.index('",') + 1], sysreg.upper(), line2[line2.index('",') + 1 :]))

print('};\n')

# lookupMClassSysRegByM2M3Encoding8
count = 0
for line in lines:
    line = line.rstrip()

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

    if 'lookupMClassSysRegByM2M3Encoding8' in line and '{' in line:
        count += 1
        print('const MClassSysReg *lookupMClassSysRegByM2M3Encoding8(uint16_t encoding)\n{')
        print('  unsigned int i;')
        continue

    if count == 1 and 'IndexType Index[] = {' in line:
        count += 1

    if count == 2:
        if line.strip() == '};':
            # done with array, or this function?
            print(line)
            break
        else:
            # enum items
            print(line)

print("""
  i = binsearch_IndexTypeEncoding(Index, ARR_SIZE(Index), encoding);
  if (i == -1)
    return NULL;
  else
    return &MClassSysRegsList[Index[i].index];
}
""")


# lookupMClassSysRegByM1Encoding12
count = 0
for line in lines:
    line = line.rstrip()

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

    if 'lookupMClassSysRegByM1Encoding12' in line and '{' in line:
        count += 1
        print('const MClassSysReg *lookupMClassSysRegByM1Encoding12(uint16_t encoding)\n{')
        print('  unsigned int i;')
        continue

    if count == 1 and 'IndexType Index[] = {' in line:
        count += 1

    if count == 2:
        if line.strip() == '};':
            # done with array, or this function?
            print(line)
            break
        else:
            # enum items
            print(line)

print("""
  i = binsearch_IndexTypeEncoding(Index, ARR_SIZE(Index), encoding);
  if (i == -1)
    return NULL;
  else
    return &MClassSysRegsList[Index[i].index];
}
""")

# lookupBankedRegByEncoding
count = 0
for line in lines:
    line = line.rstrip()

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

    if 'lookupBankedRegByEncoding' in line and '{' in line:
        count += 1
        print('const BankedReg *lookupBankedRegByEncoding(uint8_t encoding)\n{')
        print('  unsigned int i;')
        continue

    if count == 1 and 'IndexType Index[] = {' in line:
        count += 1

    if count == 2:
        if line.strip() == '};':
            # done with array, or this function?
            print(line)
            break
        else:
            # enum items
            print(line)

print("""
  i = binsearch_IndexTypeEncoding(Index, ARR_SIZE(Index), encoding);
  if (i == -1)
    return NULL;
  else
    return &BankedRegsList[Index[i].index];
}
""")