blob: 2e9d6366a49eba5db1cb526afb4d834af9380bdf [file] [log] [blame]
Nguyen Anh Quynhac6d1da2013-12-02 17:44:48 +08001# Capstone Disassembler Engine
2# By Dang Hoang Vu, 2013
3
danghvu8054c9e2013-12-01 13:24:11 -06004import sys, re
5
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +08006INCL_DIR = '../include/'
danghvu8054c9e2013-12-01 13:24:11 -06007
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +08008include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h' ]
danghvu8054c9e2013-12-01 13:24:11 -06009
10template = {
11 'java': {
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080012 'header': "// For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %s_const {\n",
danghvu8054c9e2013-12-01 13:24:11 -060013 'footer': "}",
14 'line_format': '\tpublic static final int %s = %s;\n',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080015 'out_file': './java/capstone/%s_const.java',
16 # prefixes for constant filenames of all archs - case sensitive
17 'arm.h': 'Arm',
18 'arm64.h': 'Arm64',
19 'mips.h': 'Mips',
20 'x86.h': 'X86',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080021 'comment_open': '\t//',
22 'comment_close': '',
danghvucfb01202013-12-01 13:46:49 -060023 },
24 'python': {
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080025 'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
danghvucfb01202013-12-01 13:46:49 -060026 'footer': "",
27 'line_format': '%s = %s\n',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080028 'out_file': './python/capstone/%s_const.py',
29 # prefixes for constant filenames of all archs - case sensitive
30 'arm.h': 'arm',
31 'arm64.h': 'arm64',
32 'mips.h': 'mips',
33 'x86.h': 'x86',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080034 'comment_open': '#',
35 'comment_close': '',
danghvu8054c9e2013-12-01 13:24:11 -060036 }
37}
38
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080039# markup for comments to be added to autogen files
40MARKUP = '//>'
41
danghvu8054c9e2013-12-01 13:24:11 -060042def gen(templ):
43 global include, INCL_DIR
44 for target in include:
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080045 prefix = templ[target]
46 outfile = open(templ['out_file'] %(prefix), 'w')
47 outfile.write(templ['header'] % (prefix))
danghvu8054c9e2013-12-01 13:24:11 -060048
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080049 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060050
51 count = 0
52 for line in lines:
53 line = line.strip()
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080054
55 if line.startswith(MARKUP): # markup for comments
Nguyen Anh Quynhd912f912013-12-05 00:02:37 +080056 outfile.write("\n%s%s%s" %(templ['comment_open'], \
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080057 line.replace(MARKUP, ''), templ['comment_close']))
58 continue
59
danghvu8054c9e2013-12-01 13:24:11 -060060 if line == '' or line.startswith('//'):
61 continue
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080062
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080063 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060064 continue
65
66 tmp = line.strip().split(',')
67 for t in tmp:
68 t = t.strip()
69 if not t or t.startswith('//'): continue
70 f = re.split('\s+', t)
71
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080072 if f[0].startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060073 if len(f) > 1 and f[1] not in '//=':
74 print "Error: Unable to convert %s" % f
75 continue
76 elif len(f) > 1 and f[1] == '=':
77 rhs = f[2]
78 else:
79 rhs = str(count)
80 count += 1
81
danghvub09c1222013-12-04 00:30:45 -060082 try:
danghvub4b6fea2013-12-04 00:19:48 -060083 count = int(rhs) + 1
84 if (count == 1):
85 outfile.write("\n")
danghvub09c1222013-12-04 00:30:45 -060086 except ValueError:
87 pass
danghvu8054c9e2013-12-01 13:24:11 -060088
89 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
90
91 outfile.write(templ['footer'])
92 outfile.close()
93
94def main():
danghvucfb01202013-12-01 13:46:49 -060095 try:
96 gen(template[sys.argv[1]])
97 except:
98 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -060099
100if __name__ == "__main__":
101 if len(sys.argv) < 2:
102 print "Usage:", sys.argv[0], " <bindings: java|python>"
103 sys.exit(1)
104 main()