Debian Codegraph @

suite/synctools/instrinfo-arch.py

raw symbols SCIP
1 #!/usr/bin/python
2 # convert LLVM GenInstrInfo.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 <GenInstrInfo.inc> <arch>" %sys.argv[0])
9 sys.exit(1)
10
11
12 # lib/Target/X86/X86GenAsmMatcher.inc
13 # static const MatchEntry MatchTable1[] = {
14 # { 0 /* aaa */, X86::AAA, Convert_NoOperands, Feature_Not64BitMode, { }, },
15
16 # return (arch, mnem)
17 def extract_insn(line):
18 tmp = line.split(',')
19 insn_raw = tmp[1].strip()
20 insn_mnem = tmp[0].split(' ')[3]
21 # X86 mov.s
22 if '.' in insn_mnem:
23 tmp = insn_mnem.split('.')
24 insn_mnem = tmp[0]
25 tmp = insn_raw.split('::')
26 arch = tmp[0]
27 # AArch64 -> ARM64
28 if arch.upper() == 'AArch64':
29 arch = 'ARM64'
30 return (arch, insn_mnem)
31
32 # get (arch, first insn) from MatchTable
33 def get_first_insn(filename):
34 f = open(filename)
35 lines = f.readlines()
36 f.close()
37 count = 0
38 for line in lines:
39 line = line.strip()
40
41 if len(line) == 0:
42 continue
43
44 # Intel syntax in Table1
45 if 'MatchEntry MatchTable1[] = {' in line:
46 count += 1
47 #print(line.strip())
48 continue
49
50 if count == 1:
51 arch, mnem = extract_insn(line)
52 return (arch, mnem)
53
54 return (None, None)
55
56
57 #arch, first_insn = get_first_insn(sys.argv[2])
58 #first_insn = first_insn.upper()
59 #print(arch, first_insn)
60
61 arch = sys.argv[2].upper()
62
63 if arch.upper() == 'AARCH64':
64 arch = 'AArch64'
65 elif arch.upper() == 'ARM64':
66 arch = 'AArch64'
67
68 print("""
69 /* Capstone Disassembly Engine, http://www.capstone-engine.org */
70 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
71
72 /*===- TableGen'erated file -------------------------------------*- C++ -*-===*\
73 |* *|
74 |* Target Instruction Enum Values and Descriptors *|
75 |* *|
76 |* Automatically generated file, do not edit! *|
77 |* *|
78 \*===----------------------------------------------------------------------===*/
79
80 #ifdef GET_INSTRINFO_ENUM
81 #undef GET_INSTRINFO_ENUM
82 """)
83
84 enum_count = 0
85
86 f = open(sys.argv[1])
87 lines = f.readlines()
88 f.close()
89
90 # 1st enum is register enum
91 for line in lines:
92 line = line.rstrip()
93
94 if len(line.strip()) == 0:
95 continue
96
97 if line.strip() == 'enum {':
98 enum_count += 1
99 print(line.strip())
100 continue
101
102 line = line.strip()
103 if enum_count == 1:
104 if line == '};':
105 # done with first enum
106 break
107 else:
108 # skip pseudo instructions
109 if '__' in line or 'setjmp' in line or 'longjmp' in line or 'Pseudo' in line:
110 pass
111 else:
112 print("\t%s_%s" %(arch, line))
113
114 print('};\n')
115
116 print("#endif // GET_INSTRINFO_ENUM")
117
118 if arch == 'ARM64':
119 sys.exit(0)
120
121 print("")
122 print("#ifdef GET_INSTRINFO_MC_DESC")
123 print("#undef GET_INSTRINFO_MC_DESC")
124 print("")
125 print("#define nullptr 0")
126 print("")
127
128 in_insts = False
129
130 for line in lines:
131 if line.strip() == '':
132 continue
133
134 line = line.rstrip()
135
136 if 'static const MCOperandInfo ' in line:
137 line2 = line.replace('::', '_')
138 print(line2)
139
140 elif 'Insts[] = {' in line:
141 # extern const MCInstrDesc ARMInsts[] = {
142 line2 = line.replace('extern const ', 'static const ')
143 print("")
144 print(line2)
145 in_insts = True
146
147 elif in_insts:
148 if line == '};':
149 print(line)
150 break
151 # { 0, 1, 1, 0, 0, 0|(1ULL<<MCID::Pseudo)|(1ULL<<MCID::Variadic), 0x0ULL, nullptr, nullptr, OperandInfo2, -1 ,nullptr }, // Inst #0 = PHI
152 # take 2nd & 10th entries
153 tmp = line.split(',')
154 print(" { %s, %s }," %(tmp[1].strip(), tmp[9].strip()))
155
156
157 print("")
158 print("#endif // GET_INSTRINFO_MC_DESC")
159
160 #static const MCInstrDesc ARMInsts[] = {
161 #static MCOperandInfo OperandInfo2[] = { { -1, 0, MCOI_OPERAND_IMMEDIATE, 0 }, };