blob: 23847adf6e467eaac891fabb1691fc2f0a8fae8c [file] [log] [blame]
danghvu8054c9e2013-12-01 13:24:11 -06001import sys, re
2
3INCL_DIR = '../include'
4
5include = [
6 ('/arm.h', 'ARM_'),
7 ('/arm64.h', 'ARM64_'),
8 ('/x86.h', 'X86_'),
9 ('/mips.h', 'MIPS_'),
10]
11
12template = {
13 'java': {
danghvucfb01202013-12-01 13:46:49 -060014 'header': "// AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %sconst {\n",
danghvu8054c9e2013-12-01 13:24:11 -060015 'footer': "}",
16 'line_format': '\tpublic static final int %s = %s;\n',
danghvucfb01202013-12-01 13:46:49 -060017 'out_file': './java/capstone/%sconst.java',
18 },
19 'python': {
20 'header': "# AUTO-GENERATED FILE, DO NOT EDIT [%s]\n",
21 'footer': "",
22 'line_format': '%s = %s\n',
23 'out_file': './python/capstone/%sconst.py',
danghvu8054c9e2013-12-01 13:24:11 -060024 }
25}
26
27def gen(templ):
28 global include, INCL_DIR
29 for target in include:
30 prefix = target[1];
31 outfile = open(templ['out_file'] %(prefix.capitalize()), 'w')
32 outfile.write(templ['header'] % (prefix.capitalize()))
33
34 lines = open(INCL_DIR + target[0]).readlines()
35
36 count = 0
37 for line in lines:
38 line = line.strip()
39 if line == '' or line.startswith('//'):
40 continue
41 if not line.startswith(prefix):
42 continue
43
44 tmp = line.strip().split(',')
45 for t in tmp:
46 t = t.strip()
47 if not t or t.startswith('//'): continue
48 f = re.split('\s+', t)
49
50 if f[0].startswith(prefix):
51 if len(f) > 1 and f[1] not in '//=':
52 print "Error: Unable to convert %s" % f
53 continue
54 elif len(f) > 1 and f[1] == '=':
55 rhs = f[2]
56 else:
57 rhs = str(count)
58 count += 1
59
60 if rhs == '0':
61 outfile.write("\n")
62 count = 1
63
64 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
65
66 outfile.write(templ['footer'])
67 outfile.close()
68
69def main():
danghvucfb01202013-12-01 13:46:49 -060070 try:
71 gen(template[sys.argv[1]])
72 except:
73 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -060074
75if __name__ == "__main__":
76 if len(sys.argv) < 2:
77 print "Usage:", sys.argv[0], " <bindings: java|python>"
78 sys.exit(1)
79 main()