blob: db6c9f8904665fbdc989db86a6187d96c734df11 [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 Quynh96a056d2013-12-02 18:37:46 +08006INCL_DIR = '../include/'
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': '',
danghvu8054c9e2013-12-01 13:24:11 -060044 }
45}
46
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080047# markup for comments to be added to autogen files
48MARKUP = '//>'
49
danghvu8054c9e2013-12-01 13:24:11 -060050def gen(templ):
51 global include, INCL_DIR
52 for target in include:
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080053 prefix = templ[target]
54 outfile = open(templ['out_file'] %(prefix), 'w')
55 outfile.write(templ['header'] % (prefix))
danghvu8054c9e2013-12-01 13:24:11 -060056
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080057 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060058
59 count = 0
60 for line in lines:
61 line = line.strip()
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080062
63 if line.startswith(MARKUP): # markup for comments
Nguyen Anh Quynh7957ed12013-12-15 00:32:20 +080064 outfile.write("\n%s%s%s\n" %(templ['comment_open'], \
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080065 line.replace(MARKUP, ''), templ['comment_close']))
66 continue
67
danghvu8054c9e2013-12-01 13:24:11 -060068 if line == '' or line.startswith('//'):
69 continue
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080070
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080071 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060072 continue
73
74 tmp = line.strip().split(',')
75 for t in tmp:
76 t = t.strip()
77 if not t or t.startswith('//'): continue
78 f = re.split('\s+', t)
79
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080080 if f[0].startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060081 if len(f) > 1 and f[1] not in '//=':
fenuks110ab1d2014-04-11 11:00:33 +020082 print("Error: Unable to convert %s" % f)
danghvu8054c9e2013-12-01 13:24:11 -060083 continue
84 elif len(f) > 1 and f[1] == '=':
danghvu5611de02014-01-05 03:35:43 +070085 rhs = ''.join(f[2:])
danghvu8054c9e2013-12-01 13:24:11 -060086 else:
87 rhs = str(count)
88 count += 1
89
danghvub09c1222013-12-04 00:30:45 -060090 try:
danghvub4b6fea2013-12-04 00:19:48 -060091 count = int(rhs) + 1
92 if (count == 1):
93 outfile.write("\n")
danghvub09c1222013-12-04 00:30:45 -060094 except ValueError:
95 pass
danghvu8054c9e2013-12-01 13:24:11 -060096
97 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
98
99 outfile.write(templ['footer'])
100 outfile.close()
101
102def main():
danghvucfb01202013-12-01 13:46:49 -0600103 try:
104 gen(template[sys.argv[1]])
105 except:
106 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -0600107
108if __name__ == "__main__":
109 if len(sys.argv) < 2:
fenuks110ab1d2014-04-11 11:00:33 +0200110 print("Usage:", sys.argv[0], " <bindings: java|python>")
danghvu8054c9e2013-12-01 13:24:11 -0600111 sys.exit(1)
112 main()