blob: 8909547c5834b0ffdd6201c103369e3bcdb0c92b [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 Quynh1055a2e2014-03-10 14:37:08 +08008include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.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',
danghvu5611de02014-01-05 03:35:43 +070021 'ppc.h': 'Ppc',
Nguyen Anh Quynh1055a2e2014-03-10 14:37:08 +080022 'sparc.h': 'Sparc',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080023 'comment_open': '\t//',
24 'comment_close': '',
danghvucfb01202013-12-01 13:46:49 -060025 },
26 'python': {
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080027 'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
danghvucfb01202013-12-01 13:46:49 -060028 'footer': "",
29 'line_format': '%s = %s\n',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080030 'out_file': './python/capstone/%s_const.py',
31 # prefixes for constant filenames of all archs - case sensitive
32 'arm.h': 'arm',
33 'arm64.h': 'arm64',
34 'mips.h': 'mips',
35 'x86.h': 'x86',
danghvu5611de02014-01-05 03:35:43 +070036 'ppc.h': 'ppc',
Nguyen Anh Quynh1055a2e2014-03-10 14:37:08 +080037 'sparc.h': 'sparc',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080038 'comment_open': '#',
39 'comment_close': '',
danghvu8054c9e2013-12-01 13:24:11 -060040 }
41}
42
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080043# markup for comments to be added to autogen files
44MARKUP = '//>'
45
danghvu8054c9e2013-12-01 13:24:11 -060046def gen(templ):
47 global include, INCL_DIR
48 for target in include:
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080049 prefix = templ[target]
50 outfile = open(templ['out_file'] %(prefix), 'w')
51 outfile.write(templ['header'] % (prefix))
danghvu8054c9e2013-12-01 13:24:11 -060052
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080053 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060054
55 count = 0
56 for line in lines:
57 line = line.strip()
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080058
59 if line.startswith(MARKUP): # markup for comments
Nguyen Anh Quynh7957ed12013-12-15 00:32:20 +080060 outfile.write("\n%s%s%s\n" %(templ['comment_open'], \
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080061 line.replace(MARKUP, ''), templ['comment_close']))
62 continue
63
danghvu8054c9e2013-12-01 13:24:11 -060064 if line == '' or line.startswith('//'):
65 continue
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080066
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080067 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060068 continue
69
70 tmp = line.strip().split(',')
71 for t in tmp:
72 t = t.strip()
73 if not t or t.startswith('//'): continue
74 f = re.split('\s+', t)
75
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080076 if f[0].startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060077 if len(f) > 1 and f[1] not in '//=':
78 print "Error: Unable to convert %s" % f
79 continue
80 elif len(f) > 1 and f[1] == '=':
danghvu5611de02014-01-05 03:35:43 +070081 rhs = ''.join(f[2:])
danghvu8054c9e2013-12-01 13:24:11 -060082 else:
83 rhs = str(count)
84 count += 1
85
danghvub09c1222013-12-04 00:30:45 -060086 try:
danghvub4b6fea2013-12-04 00:19:48 -060087 count = int(rhs) + 1
88 if (count == 1):
89 outfile.write("\n")
danghvub09c1222013-12-04 00:30:45 -060090 except ValueError:
91 pass
danghvu8054c9e2013-12-01 13:24:11 -060092
93 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
94
95 outfile.write(templ['footer'])
96 outfile.close()
97
98def main():
danghvucfb01202013-12-01 13:46:49 -060099 try:
100 gen(template[sys.argv[1]])
101 except:
102 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -0600103
104if __name__ == "__main__":
105 if len(sys.argv) < 2:
106 print "Usage:", sys.argv[0], " <bindings: java|python>"
107 sys.exit(1)
108 main()