| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
if len(sys.argv) == 1: |
| 9 |
print("Syntax: %s <GenAsmMatcher.inc> <GenInstrInfo.inc> MappingInsn.inc" %sys.argv[0]) |
| 10 |
sys.exit(1) |
| 11 |
|
| 12 |
f = open(sys.argv[3]) |
| 13 |
mapping = f.readlines() |
| 14 |
f.close() |
| 15 |
|
| 16 |
print("""/* Capstone Disassembly Engine, http://www.capstone-engine.org */ |
| 17 |
/* This is auto-gen data for Capstone disassembly engine (www.capstone-engine.org) */ |
| 18 |
/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */ |
| 19 |
""") |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
def extract_insn(line): |
| 28 |
tmp = line.split(',') |
| 29 |
insn_raw = tmp[1].strip() |
| 30 |
insn_mnem = tmp[0].split(' ')[3] |
| 31 |
|
| 32 |
if '.' in insn_mnem: |
| 33 |
tmp = insn_mnem.split('.') |
| 34 |
insn_mnem = tmp[0] |
| 35 |
tmp = insn_raw.split('::') |
| 36 |
arch = tmp[0] |
| 37 |
|
| 38 |
if arch.upper() == 'AARCH64': |
| 39 |
arch = 'ARM64' |
| 40 |
return (arch, insn_mnem, tmp[1]) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
def extract_matcher(filename): |
| 47 |
f = open(filename) |
| 48 |
lines = f.readlines() |
| 49 |
f.close() |
| 50 |
|
| 51 |
match_count = 0 |
| 52 |
count = 0 |
| 53 |
|
| 54 |
insn_id_list = {} |
| 55 |
arch = None |
| 56 |
first_insn = None |
| 57 |
|
| 58 |
|
| 59 |
for line in lines: |
| 60 |
line = line.rstrip() |
| 61 |
|
| 62 |
if len(line.strip()) == 0: |
| 63 |
continue |
| 64 |
|
| 65 |
if 'MatchEntry MatchTable1[] = {' in line.strip(): |
| 66 |
match_count += 1 |
| 67 |
|
| 68 |
continue |
| 69 |
|
| 70 |
line = line.strip() |
| 71 |
if match_count == 1: |
| 72 |
count += 1 |
| 73 |
if line == '};': |
| 74 |
|
| 75 |
break |
| 76 |
else: |
| 77 |
_arch, mnem, insn_id = extract_insn(line) |
| 78 |
if count == 1: |
| 79 |
arch, first_insn = _arch, insn_id |
| 80 |
|
| 81 |
if not insn_id in insn_id_list: |
| 82 |
|
| 83 |
insn_id_list[insn_id] = mnem |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
return arch, first_insn, insn_id_list |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
arch, first_insn, insn_id_list = extract_matcher(sys.argv[1]) |
| 92 |
arch = arch.upper() |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
insn_list = [] |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
def print_entry(arch, insn_id, mnem, mapping, mnem_can_be_wrong): |
| 106 |
if not mnem_can_be_wrong: |
| 107 |
insn = "%s_INS_%s" %(arch.upper(), mnem.upper()) |
| 108 |
if insn in insn_list: |
| 109 |
return |
| 110 |
print('\t"%s", // %s' %(mnem.lower(), insn)) |
| 111 |
insn_list.append(insn) |
| 112 |
return |
| 113 |
|
| 114 |
insn = "%s_%s" %(arch.upper(), insn_id) |
| 115 |
|
| 116 |
|
| 117 |
for i in range(len(mapping)): |
| 118 |
tmp = mapping[i].split(',') |
| 119 |
if tmp[0].strip() == insn: |
| 120 |
insn = tmp[1].strip() |
| 121 |
if insn in insn_list: |
| 122 |
return |
| 123 |
mnem = insn[len("%s_INS_" %(arch)):] |
| 124 |
|
| 125 |
print('\t"%s", // %s' %(mnem.lower(), insn)) |
| 126 |
insn_list.append(insn) |
| 127 |
return |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
enum_count = 0 |
| 132 |
meet_insn = False |
| 133 |
|
| 134 |
f = open(sys.argv[2]) |
| 135 |
lines = f.readlines() |
| 136 |
f.close() |
| 137 |
|
| 138 |
|
| 139 |
count = 0 |
| 140 |
last_mnem = None |
| 141 |
|
| 142 |
|
| 143 |
for line in lines: |
| 144 |
line = line.rstrip() |
| 145 |
|
| 146 |
if len(line.strip()) == 0: |
| 147 |
continue |
| 148 |
|
| 149 |
if line.strip() == 'enum {': |
| 150 |
enum_count += 1 |
| 151 |
|
| 152 |
continue |
| 153 |
|
| 154 |
line = line.strip() |
| 155 |
if enum_count == 1: |
| 156 |
if 'INSTRUCTION_LIST_END' in line: |
| 157 |
break |
| 158 |
else: |
| 159 |
insn = None |
| 160 |
if meet_insn: |
| 161 |
|
| 162 |
insn = line.split('=')[0].strip() |
| 163 |
if 'CALLSTACK' in insn or 'TAILJUMP' in insn: |
| 164 |
|
| 165 |
insn = None |
| 166 |
elif line.startswith(first_insn): |
| 167 |
insn = line.split('=')[0].strip() |
| 168 |
meet_insn = True |
| 169 |
|
| 170 |
if insn: |
| 171 |
count += 1 |
| 172 |
if insn == 'BSWAP16r_BAD': |
| 173 |
last_mnem = 'BSWAP' |
| 174 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 175 |
elif insn == 'CMOVNP_Fp32': |
| 176 |
last_mnem = 'FCMOVNP' |
| 177 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 178 |
elif insn == 'CMOVP_Fp3': |
| 179 |
last_mnem = 'FCMOVP' |
| 180 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 181 |
elif insn == 'CMPSDrm_Int': |
| 182 |
last_mnem = 'CMPSD' |
| 183 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 184 |
elif insn == 'MOVSX16rm16': |
| 185 |
last_mnem = 'MOVSX' |
| 186 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 187 |
elif insn == 'MOVZX16rm16': |
| 188 |
last_mnem = 'MOVZX' |
| 189 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 190 |
elif insn == 'ST_Fp32m': |
| 191 |
last_mnem = 'FST' |
| 192 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 193 |
elif insn == 'CMOVNP_Fp64': |
| 194 |
last_mnem = 'FCMOVNU' |
| 195 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 196 |
elif insn == 'CMPSDrr_Int': |
| 197 |
last_mnem = 'CMPSD' |
| 198 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 199 |
elif insn == 'CMPSSrm_Int': |
| 200 |
last_mnem = 'CMPSS' |
| 201 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 202 |
elif insn == 'VCMPSDrm_Int': |
| 203 |
last_mnem = 'VCMPSD' |
| 204 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 205 |
elif insn == 'VCMPSSrm_Int': |
| 206 |
last_mnem = 'VCMPSS' |
| 207 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 208 |
elif insn == 'VPCMOVYrrr_REV': |
| 209 |
last_mnem = 'VPCMOV' |
| 210 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 211 |
elif insn == 'VRNDSCALESDZm': |
| 212 |
last_mnem = 'VRNDSCALESD' |
| 213 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 214 |
elif insn == 'VRNDSCALESSZm': |
| 215 |
last_mnem = 'VRNDSCALESS' |
| 216 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 217 |
elif insn == 'VMAXCPDZ128rm': |
| 218 |
last_mnem = 'VMAXPD' |
| 219 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 220 |
elif insn == 'VMAXCPSZ128rm': |
| 221 |
last_mnem = 'VMAXPS' |
| 222 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 223 |
elif insn == 'VMAXCSDZrm': |
| 224 |
last_mnem = 'VMAXSD' |
| 225 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 226 |
elif insn == 'VMAXCSSZrm': |
| 227 |
last_mnem = 'VMAXSS' |
| 228 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 229 |
elif insn == 'VMINCPDZ128rm': |
| 230 |
last_mnem = 'VMINPD' |
| 231 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 232 |
elif insn == 'VMINCPSZ128rm': |
| 233 |
last_mnem = 'VMINPS' |
| 234 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 235 |
elif insn == 'VMINCSDZrm': |
| 236 |
last_mnem = 'VMINSD' |
| 237 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 238 |
elif insn == 'VMINCSSZrm': |
| 239 |
last_mnem = 'VMINSS' |
| 240 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 241 |
elif insn == 'VMOV64toPQIZrm': |
| 242 |
last_mnem = 'VMOVQ' |
| 243 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 244 |
elif insn == 'VPERMIL2PDYrr_REV': |
| 245 |
last_mnem = 'VPERMILPD' |
| 246 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 247 |
elif insn == 'VPERMIL2PSYrr_REV': |
| 248 |
last_mnem = 'VPERMILPS' |
| 249 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 250 |
elif insn == 'VCVTSD2SI64Zrm_Int': |
| 251 |
last_mnem = 'VCVTSD2SI' |
| 252 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 253 |
elif insn == 'VCVTSD2SSrm_Int': |
| 254 |
last_mnem = 'VCVTSD2SS' |
| 255 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 256 |
elif insn == 'VCVTSS2SI64Zrm_Int': |
| 257 |
last_mnem = 'VCVTSS2SI' |
| 258 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 259 |
elif insn == 'VCVTTSD2SI64Zrm_Int': |
| 260 |
last_mnem = 'VCVTTSD2SI' |
| 261 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 262 |
elif insn == 'VCVTTSS2SI64Zrm_Int': |
| 263 |
last_mnem = 'VCVTTSS2SI' |
| 264 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 265 |
|
| 266 |
elif insn.startswith('VFMSUBADD'): |
| 267 |
if insn[len('VFMSUBADD')].isdigit(): |
| 268 |
last_mnem = insn[:len('VFMSUBADD123xy')] |
| 269 |
else: |
| 270 |
last_mnem = insn[:len('VFMSUBADDSS')] |
| 271 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 272 |
|
| 273 |
elif insn.startswith('VFMADDSUB'): |
| 274 |
if insn[len('VFMADDSUB')].isdigit(): |
| 275 |
last_mnem = insn[:len('VFMADDSUB123xy')] |
| 276 |
else: |
| 277 |
last_mnem = insn[:len('VFMADDSUBSS')] |
| 278 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 279 |
|
| 280 |
elif insn.startswith('VFMADD'): |
| 281 |
if insn[len('VFMADD')].isdigit(): |
| 282 |
last_mnem = insn[:len('VFMADD123PD')] |
| 283 |
else: |
| 284 |
last_mnem = insn[:len('VFMADDPD')] |
| 285 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 286 |
|
| 287 |
elif insn.startswith('VFMSUB'): |
| 288 |
if insn[len('VFMSUB')].isdigit(): |
| 289 |
last_mnem = insn[:len('VFMSUB123PD')] |
| 290 |
else: |
| 291 |
last_mnem = insn[:len('VFMSUBPD')] |
| 292 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 293 |
|
| 294 |
elif insn.startswith('VFNMADD'): |
| 295 |
if insn[len('VFNMADD')].isdigit(): |
| 296 |
last_mnem = insn[:len('VFNMADD123xy')] |
| 297 |
else: |
| 298 |
last_mnem = insn[:len('VFNMADDSS')] |
| 299 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 300 |
|
| 301 |
elif insn.startswith('VFNMSUB'): |
| 302 |
if insn[len('VFNMSUB')].isdigit(): |
| 303 |
last_mnem = insn[:len('VFNMSUB123xy')] |
| 304 |
else: |
| 305 |
last_mnem = insn[:len('VFNMSUBSS')] |
| 306 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 307 |
|
| 308 |
elif insn in insn_id_list: |
| 309 |
|
| 310 |
last_mnem = insn_id_list[insn].upper() |
| 311 |
print_entry(arch.upper(), insn, last_mnem, mapping, False) |
| 312 |
else: |
| 313 |
|
| 314 |
print_entry(arch.upper(), insn, last_mnem, mapping, True) |