blob: a51402e070039abcdd6b1e632a27279bbc86379a [file] [log] [blame]
Nguyen Anh Quynhac6d1da2013-12-02 17:44:48 +08001# Capstone Disassembler Engine
2# By Dang Hoang Vu, 2013
fenuks110ab1d2014-04-11 11:00:33 +02003from __future__ import print_function
danghvu8054c9e2013-12-01 13:24:11 -06004import sys, re
5
Nguyen Anh Quynh72d3c4f2015-02-28 08:42:40 +08006INCL_DIR = '../include/capstone/'
danghvu8054c9e2013-12-01 13:24:11 -06007
Nguyen Anh Quynh553bb482014-05-26 23:47:04 +08008include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.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 Quynh1c8405d2014-03-23 11:17:24 +080023 'systemz.h': 'Sysz',
Nguyen Anh Quynh553bb482014-05-26 23:47:04 +080024 'xcore.h': 'Xcore',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080025 'comment_open': '\t//',
26 'comment_close': '',
danghvucfb01202013-12-01 13:46:49 -060027 },
28 'python': {
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080029 'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
danghvucfb01202013-12-01 13:46:49 -060030 'footer': "",
31 'line_format': '%s = %s\n',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080032 'out_file': './python/capstone/%s_const.py',
33 # prefixes for constant filenames of all archs - case sensitive
34 'arm.h': 'arm',
35 'arm64.h': 'arm64',
36 'mips.h': 'mips',
37 'x86.h': 'x86',
danghvu5611de02014-01-05 03:35:43 +070038 'ppc.h': 'ppc',
Nguyen Anh Quynh1055a2e2014-03-10 14:37:08 +080039 'sparc.h': 'sparc',
Nguyen Anh Quynh1c8405d2014-03-23 11:17:24 +080040 'systemz.h': 'sysz',
Nguyen Anh Quynh553bb482014-05-26 23:47:04 +080041 'xcore.h': 'xcore',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080042 'comment_open': '#',
43 'comment_close': '',
Nguyen Anh Quynh586be762014-09-21 23:23:38 +080044 },
45 'ocaml': {
46 'header': "(* For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.ml] *)\n",
47 'footer': "",
48 'line_format': 'let _%s = %s;;\n',
49 'out_file': './ocaml/%s_const.ml',
50 # prefixes for constant filenames of all archs - case sensitive
51 'arm.h': 'arm',
52 'arm64.h': 'arm64',
53 'mips.h': 'mips',
54 'x86.h': 'x86',
55 'ppc.h': 'ppc',
56 'sparc.h': 'sparc',
57 'systemz.h': 'sysz',
58 'xcore.h': 'xcore',
59 'comment_open': '(*',
60 'comment_close': ' *)',
61 },
danghvu8054c9e2013-12-01 13:24:11 -060062}
63
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080064# markup for comments to be added to autogen files
65MARKUP = '//>'
66
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +080067def gen(lang):
danghvu8054c9e2013-12-01 13:24:11 -060068 global include, INCL_DIR
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +080069 templ = template[lang]
learn_more8c7b3202015-08-02 20:09:41 +020070 print('Generating bindings for', lang)
danghvu8054c9e2013-12-01 13:24:11 -060071 for target in include:
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080072 prefix = templ[target]
learn_more78de4fa2015-08-02 20:10:35 +020073 outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080074 outfile.write(templ['header'] % (prefix))
danghvu8054c9e2013-12-01 13:24:11 -060075
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080076 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060077
78 count = 0
79 for line in lines:
80 line = line.strip()
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080081
82 if line.startswith(MARKUP): # markup for comments
Nguyen Anh Quynh7957ed12013-12-15 00:32:20 +080083 outfile.write("\n%s%s%s\n" %(templ['comment_open'], \
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080084 line.replace(MARKUP, ''), templ['comment_close']))
85 continue
86
danghvu8054c9e2013-12-01 13:24:11 -060087 if line == '' or line.startswith('//'):
88 continue
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080089
learn_moref6ded662015-08-02 20:12:51 +020090 if line.startswith('#define '):
91 line = line[8:] #cut off define
92 xline = re.split('\s+', line, 1) #split to at most 2 express
93 if len(xline) != 2:
94 continue
95 if '(' in xline[0] or ')' in xline[0]: #does it look like a function
96 continue
97 xline.insert(1, '=') # insert an = so the expression below can parse it
98 line = ' '.join(xline)
99
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +0800100 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -0600101 continue
102
103 tmp = line.strip().split(',')
104 for t in tmp:
105 t = t.strip()
106 if not t or t.startswith('//'): continue
Nguyen Anh Quynh10647ae2015-03-25 17:35:59 +0800107 # hacky: remove type cast (uint64_t)
108 t = t.replace('(uint64_t)', '')
learn_moref6ded662015-08-02 20:12:51 +0200109 t = re.sub(r'\((\d+)ULL << (\d+)\)', r'\1 << \2', t) # (1ULL<<1) to 1 << 1
danghvu8054c9e2013-12-01 13:24:11 -0600110 f = re.split('\s+', t)
111
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +0800112 if f[0].startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -0600113 if len(f) > 1 and f[1] not in '//=':
fenuks110ab1d2014-04-11 11:00:33 +0200114 print("Error: Unable to convert %s" % f)
danghvu8054c9e2013-12-01 13:24:11 -0600115 continue
116 elif len(f) > 1 and f[1] == '=':
danghvu5611de02014-01-05 03:35:43 +0700117 rhs = ''.join(f[2:])
danghvu8054c9e2013-12-01 13:24:11 -0600118 else:
119 rhs = str(count)
120 count += 1
121
danghvub09c1222013-12-04 00:30:45 -0600122 try:
danghvub4b6fea2013-12-04 00:19:48 -0600123 count = int(rhs) + 1
124 if (count == 1):
125 outfile.write("\n")
danghvub09c1222013-12-04 00:30:45 -0600126 except ValueError:
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800127 if lang == 'ocaml':
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +0800128 # ocaml uses lsl for '<<', lor for '|'
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800129 rhs = rhs.replace('<<', ' lsl ')
130 rhs = rhs.replace('|', ' lor ')
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +0800131 # ocaml variable has _ as prefix
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800132 if rhs[0].isalpha():
133 rhs = '_' + rhs
danghvu8054c9e2013-12-01 13:24:11 -0600134
135 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
136
137 outfile.write(templ['footer'])
138 outfile.close()
139
140def main():
danghvucfb01202013-12-01 13:46:49 -0600141 try:
learn_more8c7b3202015-08-02 20:09:41 +0200142 if sys.argv[1] == 'all':
143 for key in template.keys():
144 gen(key)
145 else:
146 gen(sys.argv[1])
danghvucfb01202013-12-01 13:46:49 -0600147 except:
148 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -0600149
150if __name__ == "__main__":
151 if len(sys.argv) < 2:
learn_more8c7b3202015-08-02 20:09:41 +0200152 print("Usage:", sys.argv[0], " <bindings: java|python|ocaml|all>")
danghvu8054c9e2013-12-01 13:24:11 -0600153 sys.exit(1)
154 main()