blob: 0ec9e986fd8eb744de005b668c602ce63fc5223e [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
Sebastian Macke8663d752018-12-04 21:47:03 +01008include = [ 'arm.h', 'arm64.h', 'm68k.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h', 'tms320c64x.h', 'm680x.h', 'evm.h', 'mos65xx.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',
Nguyen Anh Quynh57bf77a2015-10-03 11:32:24 +080019 'm68k.h': 'M68k',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080020 'mips.h': 'Mips',
21 'x86.h': 'X86',
danghvu5611de02014-01-05 03:35:43 +070022 'ppc.h': 'Ppc',
Nguyen Anh Quynh1055a2e2014-03-10 14:37:08 +080023 'sparc.h': 'Sparc',
Nguyen Anh Quynh1c8405d2014-03-23 11:17:24 +080024 'systemz.h': 'Sysz',
Nguyen Anh Quynh553bb482014-05-26 23:47:04 +080025 'xcore.h': 'Xcore',
Fotis Loukos0a159f12016-05-03 16:37:52 +030026 'tms320c64x.h': 'TMS320C64x',
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020027 'm680x.h': 'M680x',
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +080028 'evm.h': 'Evm',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080029 'comment_open': '\t//',
30 'comment_close': '',
danghvucfb01202013-12-01 13:46:49 -060031 },
32 'python': {
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080033 'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
danghvucfb01202013-12-01 13:46:49 -060034 'footer': "",
35 'line_format': '%s = %s\n',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080036 'out_file': './python/capstone/%s_const.py',
37 # prefixes for constant filenames of all archs - case sensitive
38 'arm.h': 'arm',
39 'arm64.h': 'arm64',
Daniel Collin2ee675c2015-08-03 18:45:08 +020040 'm68k.h': 'm68k',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080041 'mips.h': 'mips',
42 'x86.h': 'x86',
danghvu5611de02014-01-05 03:35:43 +070043 'ppc.h': 'ppc',
Nguyen Anh Quynh1055a2e2014-03-10 14:37:08 +080044 'sparc.h': 'sparc',
Nguyen Anh Quynh1c8405d2014-03-23 11:17:24 +080045 'systemz.h': 'sysz',
Nguyen Anh Quynh553bb482014-05-26 23:47:04 +080046 'xcore.h': 'xcore',
Fotis Loukos0a159f12016-05-03 16:37:52 +030047 'tms320c64x.h': 'tms320c64x',
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020048 'm680x.h': 'm680x',
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +080049 'evm.h': 'evm',
Sebastian Macke8663d752018-12-04 21:47:03 +010050 'mos65xx.h': 'mos65xx',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080051 'comment_open': '#',
52 'comment_close': '',
Nguyen Anh Quynh586be762014-09-21 23:23:38 +080053 },
54 'ocaml': {
55 'header': "(* For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.ml] *)\n",
56 'footer': "",
57 'line_format': 'let _%s = %s;;\n',
58 'out_file': './ocaml/%s_const.ml',
59 # prefixes for constant filenames of all archs - case sensitive
60 'arm.h': 'arm',
61 'arm64.h': 'arm64',
62 'mips.h': 'mips',
Nguyen Anh Quynh57bf77a2015-10-03 11:32:24 +080063 'm68k.h': 'm68k',
Nguyen Anh Quynh586be762014-09-21 23:23:38 +080064 'x86.h': 'x86',
65 'ppc.h': 'ppc',
66 'sparc.h': 'sparc',
67 'systemz.h': 'sysz',
68 'xcore.h': 'xcore',
Fotis Loukos0a159f12016-05-03 16:37:52 +030069 'tms320c64x.h': 'tms320c64x',
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020070 'm680x.h': 'm680x',
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +080071 'evm.h': 'evm',
Nguyen Anh Quynh586be762014-09-21 23:23:38 +080072 'comment_open': '(*',
73 'comment_close': ' *)',
74 },
danghvu8054c9e2013-12-01 13:24:11 -060075}
76
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080077# markup for comments to be added to autogen files
78MARKUP = '//>'
79
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +080080def gen(lang):
danghvu8054c9e2013-12-01 13:24:11 -060081 global include, INCL_DIR
Nicolas PLANEL8b25b0a2015-10-03 14:59:51 +100082 print('Generating bindings for', lang)
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +080083 templ = template[lang]
learn_more8c7b3202015-08-02 20:09:41 +020084 print('Generating bindings for', lang)
danghvu8054c9e2013-12-01 13:24:11 -060085 for target in include:
Sebastian Macke8663d752018-12-04 21:47:03 +010086 if target not in templ:
87 print("Warning: No binding found for %s" % target)
88 continue
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080089 prefix = templ[target]
Nicolas PLANEL8b25b0a2015-10-03 14:59:51 +100090 outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines
91 outfile.write((templ['header'] % (prefix)).encode("utf-8"))
danghvu8054c9e2013-12-01 13:24:11 -060092
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080093 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060094
95 count = 0
96 for line in lines:
97 line = line.strip()
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080098
99 if line.startswith(MARKUP): # markup for comments
Nicolas PLANEL8b25b0a2015-10-03 14:59:51 +1000100 outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \
101 line.replace(MARKUP, ''), \
102 templ['comment_close']) ).encode("utf-8"))
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +0800103 continue
104
danghvu8054c9e2013-12-01 13:24:11 -0600105 if line == '' or line.startswith('//'):
106 continue
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +0800107
learn_moref6ded662015-08-02 20:12:51 +0200108 if line.startswith('#define '):
109 line = line[8:] #cut off define
110 xline = re.split('\s+', line, 1) #split to at most 2 express
111 if len(xline) != 2:
112 continue
113 if '(' in xline[0] or ')' in xline[0]: #does it look like a function
114 continue
115 xline.insert(1, '=') # insert an = so the expression below can parse it
116 line = ' '.join(xline)
117
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +0800118 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -0600119 continue
120
121 tmp = line.strip().split(',')
122 for t in tmp:
123 t = t.strip()
124 if not t or t.startswith('//'): continue
Nguyen Anh Quynh10647ae2015-03-25 17:35:59 +0800125 # hacky: remove type cast (uint64_t)
126 t = t.replace('(uint64_t)', '')
learn_moref6ded662015-08-02 20:12:51 +0200127 t = re.sub(r'\((\d+)ULL << (\d+)\)', r'\1 << \2', t) # (1ULL<<1) to 1 << 1
danghvu8054c9e2013-12-01 13:24:11 -0600128 f = re.split('\s+', t)
129
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +0800130 if f[0].startswith(prefix.upper()):
Nguyen Anh Quynh2b4aec92018-10-01 20:29:39 +0800131 if len(f) > 1 and f[1] not in ('//', '///<', '='):
fenuks110ab1d2014-04-11 11:00:33 +0200132 print("Error: Unable to convert %s" % f)
danghvu8054c9e2013-12-01 13:24:11 -0600133 continue
134 elif len(f) > 1 and f[1] == '=':
danghvu5611de02014-01-05 03:35:43 +0700135 rhs = ''.join(f[2:])
danghvu8054c9e2013-12-01 13:24:11 -0600136 else:
137 rhs = str(count)
138 count += 1
139
danghvub09c1222013-12-04 00:30:45 -0600140 try:
danghvub4b6fea2013-12-04 00:19:48 -0600141 count = int(rhs) + 1
142 if (count == 1):
Nicolas PLANEL8b25b0a2015-10-03 14:59:51 +1000143 outfile.write(("\n").encode("utf-8"))
danghvub09c1222013-12-04 00:30:45 -0600144 except ValueError:
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800145 if lang == 'ocaml':
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +0800146 # ocaml uses lsl for '<<', lor for '|'
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800147 rhs = rhs.replace('<<', ' lsl ')
148 rhs = rhs.replace('|', ' lor ')
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +0800149 # ocaml variable has _ as prefix
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800150 if rhs[0].isalpha():
151 rhs = '_' + rhs
danghvu8054c9e2013-12-01 13:24:11 -0600152
Nicolas PLANEL8b25b0a2015-10-03 14:59:51 +1000153 outfile.write((templ['line_format'] %(f[0].strip(), rhs)).encode("utf-8"))
danghvu8054c9e2013-12-01 13:24:11 -0600154
Nicolas PLANEL8b25b0a2015-10-03 14:59:51 +1000155 outfile.write((templ['footer']).encode("utf-8"))
danghvu8054c9e2013-12-01 13:24:11 -0600156 outfile.close()
157
158def main():
danghvucfb01202013-12-01 13:46:49 -0600159 try:
learn_more8c7b3202015-08-02 20:09:41 +0200160 if sys.argv[1] == 'all':
161 for key in template.keys():
162 gen(key)
163 else:
164 gen(sys.argv[1])
danghvucfb01202013-12-01 13:46:49 -0600165 except:
166 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -0600167
168if __name__ == "__main__":
169 if len(sys.argv) < 2:
learn_more8c7b3202015-08-02 20:09:41 +0200170 print("Usage:", sys.argv[0], " <bindings: java|python|ocaml|all>")
danghvu8054c9e2013-12-01 13:24:11 -0600171 sys.exit(1)
172 main()