blob: df58e01429f1943d4f83f0bec801fa5028920214 [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',
danghvucfb01202013-12-01 13:46:49 -060021 },
22 'python': {
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080023 'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
danghvucfb01202013-12-01 13:46:49 -060024 'footer': "",
25 'line_format': '%s = %s\n',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080026 'out_file': './python/capstone/%s_const.py',
27 # prefixes for constant filenames of all archs - case sensitive
28 'arm.h': 'arm',
29 'arm64.h': 'arm64',
30 'mips.h': 'mips',
31 'x86.h': 'x86',
danghvu8054c9e2013-12-01 13:24:11 -060032 }
33}
34
35def gen(templ):
36 global include, INCL_DIR
37 for target in include:
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080038 prefix = templ[target]
39 outfile = open(templ['out_file'] %(prefix), 'w')
40 outfile.write(templ['header'] % (prefix))
danghvu8054c9e2013-12-01 13:24:11 -060041
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080042 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060043
44 count = 0
45 for line in lines:
46 line = line.strip()
47 if line == '' or line.startswith('//'):
48 continue
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080049 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060050 continue
51
52 tmp = line.strip().split(',')
53 for t in tmp:
54 t = t.strip()
55 if not t or t.startswith('//'): continue
56 f = re.split('\s+', t)
57
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080058 if f[0].startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060059 if len(f) > 1 and f[1] not in '//=':
60 print "Error: Unable to convert %s" % f
61 continue
62 elif len(f) > 1 and f[1] == '=':
63 rhs = f[2]
64 else:
65 rhs = str(count)
66 count += 1
67
68 if rhs == '0':
69 outfile.write("\n")
70 count = 1
71
72 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
73
74 outfile.write(templ['footer'])
75 outfile.close()
76
77def main():
danghvucfb01202013-12-01 13:46:49 -060078 try:
79 gen(template[sys.argv[1]])
80 except:
81 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -060082
83if __name__ == "__main__":
84 if len(sys.argv) < 2:
85 print "Usage:", sys.argv[0], " <bindings: java|python>"
86 sys.exit(1)
87 main()