2013-12-02 09:44:48 +00:00
|
|
|
# Capstone Disassembler Engine
|
|
|
|
# By Dang Hoang Vu, 2013
|
2014-04-11 09:00:33 +00:00
|
|
|
from __future__ import print_function
|
2013-12-01 19:24:11 +00:00
|
|
|
import sys, re
|
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
INCL_DIR = '../include/'
|
2013-12-01 19:24:11 +00:00
|
|
|
|
2014-05-26 15:47:04 +00:00
|
|
|
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h' ]
|
2013-12-01 19:24:11 +00:00
|
|
|
|
|
|
|
template = {
|
|
|
|
'java': {
|
2013-12-02 10:37:46 +00:00
|
|
|
'header': "// For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %s_const {\n",
|
2013-12-01 19:24:11 +00:00
|
|
|
'footer': "}",
|
|
|
|
'line_format': '\tpublic static final int %s = %s;\n',
|
2013-12-02 10:37:46 +00:00
|
|
|
'out_file': './java/capstone/%s_const.java',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'Arm',
|
|
|
|
'arm64.h': 'Arm64',
|
|
|
|
'mips.h': 'Mips',
|
|
|
|
'x86.h': 'X86',
|
2014-01-04 20:35:43 +00:00
|
|
|
'ppc.h': 'Ppc',
|
2014-03-10 06:37:08 +00:00
|
|
|
'sparc.h': 'Sparc',
|
2014-03-23 03:17:24 +00:00
|
|
|
'systemz.h': 'Sysz',
|
2014-05-26 15:47:04 +00:00
|
|
|
'xcore.h': 'Xcore',
|
2013-12-04 15:56:24 +00:00
|
|
|
'comment_open': '\t//',
|
|
|
|
'comment_close': '',
|
2013-12-01 19:46:49 +00:00
|
|
|
},
|
|
|
|
'python': {
|
2013-12-02 10:37:46 +00:00
|
|
|
'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
|
2013-12-01 19:46:49 +00:00
|
|
|
'footer': "",
|
|
|
|
'line_format': '%s = %s\n',
|
2013-12-02 10:37:46 +00:00
|
|
|
'out_file': './python/capstone/%s_const.py',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'arm',
|
|
|
|
'arm64.h': 'arm64',
|
|
|
|
'mips.h': 'mips',
|
|
|
|
'x86.h': 'x86',
|
2014-01-04 20:35:43 +00:00
|
|
|
'ppc.h': 'ppc',
|
2014-03-10 06:37:08 +00:00
|
|
|
'sparc.h': 'sparc',
|
2014-03-23 03:17:24 +00:00
|
|
|
'systemz.h': 'sysz',
|
2014-05-26 15:47:04 +00:00
|
|
|
'xcore.h': 'xcore',
|
2013-12-04 15:56:24 +00:00
|
|
|
'comment_open': '#',
|
|
|
|
'comment_close': '',
|
2014-09-21 15:23:38 +00:00
|
|
|
},
|
|
|
|
'ocaml': {
|
|
|
|
'header': "(* For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.ml] *)\n",
|
|
|
|
'footer': "",
|
|
|
|
'line_format': 'let _%s = %s;;\n',
|
|
|
|
'out_file': './ocaml/%s_const.ml',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'arm',
|
|
|
|
'arm64.h': 'arm64',
|
|
|
|
'mips.h': 'mips',
|
|
|
|
'x86.h': 'x86',
|
|
|
|
'ppc.h': 'ppc',
|
|
|
|
'sparc.h': 'sparc',
|
|
|
|
'systemz.h': 'sysz',
|
|
|
|
'xcore.h': 'xcore',
|
|
|
|
'comment_open': '(*',
|
|
|
|
'comment_close': ' *)',
|
|
|
|
},
|
2013-12-01 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 15:56:24 +00:00
|
|
|
# markup for comments to be added to autogen files
|
|
|
|
MARKUP = '//>'
|
|
|
|
|
2014-09-21 15:32:50 +00:00
|
|
|
def gen(lang):
|
2013-12-01 19:24:11 +00:00
|
|
|
global include, INCL_DIR
|
2015-10-03 04:59:51 +00:00
|
|
|
print('Generating bindings for', lang)
|
2014-09-21 16:07:58 +00:00
|
|
|
templ = template[lang]
|
2013-12-01 19:24:11 +00:00
|
|
|
for target in include:
|
2013-12-02 10:37:46 +00:00
|
|
|
prefix = templ[target]
|
2015-10-03 04:59:51 +00:00
|
|
|
outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines
|
|
|
|
outfile.write((templ['header'] % (prefix)).encode("utf-8"))
|
2013-12-01 19:24:11 +00:00
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
lines = open(INCL_DIR + target).readlines()
|
2013-12-01 19:24:11 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
for line in lines:
|
|
|
|
line = line.strip()
|
2013-12-04 15:56:24 +00:00
|
|
|
|
|
|
|
if line.startswith(MARKUP): # markup for comments
|
2015-10-03 04:59:51 +00:00
|
|
|
outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \
|
|
|
|
line.replace(MARKUP, ''), \
|
|
|
|
templ['comment_close']) ).encode("utf-8"))
|
2013-12-04 15:56:24 +00:00
|
|
|
continue
|
|
|
|
|
2013-12-01 19:24:11 +00:00
|
|
|
if line == '' or line.startswith('//'):
|
|
|
|
continue
|
2013-12-04 15:56:24 +00:00
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
if not line.startswith(prefix.upper()):
|
2013-12-01 19:24:11 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
tmp = line.strip().split(',')
|
|
|
|
for t in tmp:
|
|
|
|
t = t.strip()
|
|
|
|
if not t or t.startswith('//'): continue
|
|
|
|
f = re.split('\s+', t)
|
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
if f[0].startswith(prefix.upper()):
|
2013-12-01 19:24:11 +00:00
|
|
|
if len(f) > 1 and f[1] not in '//=':
|
2014-04-11 09:00:33 +00:00
|
|
|
print("Error: Unable to convert %s" % f)
|
2013-12-01 19:24:11 +00:00
|
|
|
continue
|
|
|
|
elif len(f) > 1 and f[1] == '=':
|
2014-01-04 20:35:43 +00:00
|
|
|
rhs = ''.join(f[2:])
|
2013-12-01 19:24:11 +00:00
|
|
|
else:
|
|
|
|
rhs = str(count)
|
|
|
|
count += 1
|
|
|
|
|
2013-12-04 06:30:45 +00:00
|
|
|
try:
|
2013-12-04 06:19:48 +00:00
|
|
|
count = int(rhs) + 1
|
|
|
|
if (count == 1):
|
2015-10-03 04:59:51 +00:00
|
|
|
outfile.write(("\n").encode("utf-8"))
|
2013-12-04 06:30:45 +00:00
|
|
|
except ValueError:
|
2014-09-21 15:32:50 +00:00
|
|
|
if lang == 'ocaml':
|
2014-09-21 16:07:58 +00:00
|
|
|
# ocaml uses lsl for '<<', lor for '|'
|
2014-09-21 15:32:50 +00:00
|
|
|
rhs = rhs.replace('<<', ' lsl ')
|
|
|
|
rhs = rhs.replace('|', ' lor ')
|
2014-09-21 16:07:58 +00:00
|
|
|
# ocaml variable has _ as prefix
|
2014-09-21 15:32:50 +00:00
|
|
|
if rhs[0].isalpha():
|
|
|
|
rhs = '_' + rhs
|
2013-12-01 19:24:11 +00:00
|
|
|
|
2015-10-03 04:59:51 +00:00
|
|
|
outfile.write((templ['line_format'] %(f[0].strip(), rhs)).encode("utf-8"))
|
2013-12-01 19:24:11 +00:00
|
|
|
|
2015-10-03 04:59:51 +00:00
|
|
|
outfile.write((templ['footer']).encode("utf-8"))
|
2013-12-01 19:24:11 +00:00
|
|
|
outfile.close()
|
|
|
|
|
|
|
|
def main():
|
2013-12-01 19:46:49 +00:00
|
|
|
try:
|
2014-09-21 15:32:50 +00:00
|
|
|
gen(sys.argv[1])
|
2013-12-01 19:46:49 +00:00
|
|
|
except:
|
|
|
|
raise RuntimeError("Unsupported binding %s" % sys.argv[1])
|
2013-12-01 19:24:11 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) < 2:
|
2014-09-21 15:23:38 +00:00
|
|
|
print("Usage:", sys.argv[0], " <bindings: java|python|ocaml>")
|
2013-12-01 19:24:11 +00:00
|
|
|
sys.exit(1)
|
|
|
|
main()
|