Debian Codegraph @

suite/synctools/systemregister.py

raw symbols SCIP
1 #!/usr/bin/python
2 # convert LLVM GenSystemRegister.inc for Capstone disassembler.
3 # by Nguyen Anh Quynh, 2019
4
5 import sys
6
7 if len(sys.argv) == 1:
8 print("Syntax: %s <GenSystemRegister.inc>" %sys.argv[0])
9 sys.exit(1)
10
11 f = open(sys.argv[1])
12 lines = f.readlines()
13 f.close()
14
15 #arch = sys.argv[2].upper()
16
17 print("""
18 /* Capstone Disassembly Engine, http://www.capstone-engine.org */
19 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
20
21 /*===- TableGen'erated file -------------------------------------*- C++ -*-===*\
22 |* *|
23 |* GenSystemRegister Source Fragment *|
24 |* *|
25 |* Automatically generated file, do not edit! *|
26 |* *|
27 \*===----------------------------------------------------------------------===*/
28
29 """)
30
31 # extract BankedRegValues enum
32 count = 0
33 for line in lines:
34 line = line.rstrip()
35
36 if len(line.strip()) == 0:
37 continue
38
39 if line.strip() == 'enum BankedRegValues {':
40 count += 1
41 print(line.strip())
42 continue
43
44 line = line.strip()
45 if count == 1:
46 if line == '};':
47 # done with first enum
48 break
49 else:
50 # skip pseudo instructions
51 print("\t%s" %(line))
52
53 print('};\n')
54
55 # extract MClassSysRegsList
56 count = 0
57 for line in lines:
58 line = line.rstrip()
59
60 if len(line.strip()) == 0:
61 continue
62
63 if 'MClassSysRegsList[]' in line:
64 count += 1
65 print('static const MClassSysReg MClassSysRegsList[] = {')
66 continue
67
68 if count == 1:
69 if line.strip() == '};':
70 # done with first enum
71 break
72 else:
73 # enum items
74 # { "apsr_g", 0x400, 0x0, 0x400, {ARM::FeatureDSP} }, // 0
75 line2 = line.replace('::', '_')
76 sysreg = line2[line2.index('"') + 1 : line2.index('",')]
77 tmp = line2.split(',')
78 print("%s, ARM_SYSREG_%s%s" %(line2[:line2.index('",') + 1], sysreg.upper(), line2[line2.index('",') + 1 :]))
79
80 print('};\n')
81
82 # extract BankedRegsList
83 count = 0
84 for line in lines:
85 line = line.rstrip()
86
87 if len(line.strip()) == 0:
88 continue
89
90 if 'BankedRegsList[]' in line:
91 count += 1
92 print('static const BankedReg BankedRegsList[] = {')
93 continue
94
95 if count == 1:
96 if line.strip() == '};':
97 # done with first enum
98 break
99 else:
100 # enum items
101 line2 = line.replace('::', '_')
102 sysreg = line2[line2.index('"') + 1 : line2.index('",')]
103 tmp = line2.split(',')
104 print("%s, ARM_SYSREG_%s%s" %(line2[:line2.index('",') + 1], sysreg.upper(), line2[line2.index('",') + 1 :]))
105
106 print('};\n')
107
108 # lookupMClassSysRegByM2M3Encoding8
109 count = 0
110 for line in lines:
111 line = line.rstrip()
112
113 if len(line.strip()) == 0:
114 continue
115
116 if 'lookupMClassSysRegByM2M3Encoding8' in line and '{' in line:
117 count += 1
118 print('const MClassSysReg *lookupMClassSysRegByM2M3Encoding8(uint16_t encoding)\n{')
119 print(' unsigned int i;')
120 continue
121
122 if count == 1 and 'IndexType Index[] = {' in line:
123 count += 1
124
125 if count == 2:
126 if line.strip() == '};':
127 # done with array, or this function?
128 print(line)
129 break
130 else:
131 # enum items
132 print(line)
133
134 print("""
135 i = binsearch_IndexTypeEncoding(Index, ARR_SIZE(Index), encoding);
136 if (i == -1)
137 return NULL;
138 else
139 return &MClassSysRegsList[Index[i].index];
140 }
141 """)
142
143
144 # lookupMClassSysRegByM1Encoding12
145 count = 0
146 for line in lines:
147 line = line.rstrip()
148
149 if len(line.strip()) == 0:
150 continue
151
152 if 'lookupMClassSysRegByM1Encoding12' in line and '{' in line:
153 count += 1
154 print('const MClassSysReg *lookupMClassSysRegByM1Encoding12(uint16_t encoding)\n{')
155 print(' unsigned int i;')
156 continue
157
158 if count == 1 and 'IndexType Index[] = {' in line:
159 count += 1
160
161 if count == 2:
162 if line.strip() == '};':
163 # done with array, or this function?
164 print(line)
165 break
166 else:
167 # enum items
168 print(line)
169
170 print("""
171 i = binsearch_IndexTypeEncoding(Index, ARR_SIZE(Index), encoding);
172 if (i == -1)
173 return NULL;
174 else
175 return &MClassSysRegsList[Index[i].index];
176 }
177 """)
178
179 # lookupBankedRegByEncoding
180 count = 0
181 for line in lines:
182 line = line.rstrip()
183
184 if len(line.strip()) == 0:
185 continue
186
187 if 'lookupBankedRegByEncoding' in line and '{' in line:
188 count += 1
189 print('const BankedReg *lookupBankedRegByEncoding(uint8_t encoding)\n{')
190 print(' unsigned int i;')
191 continue
192
193 if count == 1 and 'IndexType Index[] = {' in line:
194 count += 1
195
196 if count == 2:
197 if line.strip() == '};':
198 # done with array, or this function?
199 print(line)
200 break
201 else:
202 # enum items
203 print(line)
204
205 print("""
206 i = binsearch_IndexTypeEncoding(Index, ARR_SIZE(Index), encoding);
207 if (i == -1)
208 return NULL;
209 else
210 return &BankedRegsList[Index[i].index];
211 }
212 """)
213