blob: 4c00edc2c12eaf201e3c38e92e836a8c975bbd52 [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 Quynh1c8405d2014-03-23 11:17:24 +08008include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.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 Quynha2f825f2013-12-04 23:56:24 +080024 'comment_open': '\t//',
25 'comment_close': '',
danghvucfb01202013-12-01 13:46:49 -060026 },
27 'python': {
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080028 'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
danghvucfb01202013-12-01 13:46:49 -060029 'footer': "",
30 'line_format': '%s = %s\n',
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080031 'out_file': './python/capstone/%s_const.py',
32 # prefixes for constant filenames of all archs - case sensitive
33 'arm.h': 'arm',
34 'arm64.h': 'arm64',
35 'mips.h': 'mips',
36 'x86.h': 'x86',
danghvu5611de02014-01-05 03:35:43 +070037 'ppc.h': 'ppc',
Nguyen Anh Quynh1055a2e2014-03-10 14:37:08 +080038 'sparc.h': 'sparc',
Nguyen Anh Quynh1c8405d2014-03-23 11:17:24 +080039 'systemz.h': 'sysz',
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080040 'comment_open': '#',
41 'comment_close': '',
danghvu8054c9e2013-12-01 13:24:11 -060042 }
43}
44
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080045# markup for comments to be added to autogen files
46MARKUP = '//>'
47
danghvu8054c9e2013-12-01 13:24:11 -060048def gen(templ):
49 global include, INCL_DIR
50 for target in include:
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080051 prefix = templ[target]
52 outfile = open(templ['out_file'] %(prefix), 'w')
53 outfile.write(templ['header'] % (prefix))
danghvu8054c9e2013-12-01 13:24:11 -060054
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080055 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060056
57 count = 0
58 for line in lines:
59 line = line.strip()
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080060
61 if line.startswith(MARKUP): # markup for comments
Nguyen Anh Quynh7957ed12013-12-15 00:32:20 +080062 outfile.write("\n%s%s%s\n" %(templ['comment_open'], \
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080063 line.replace(MARKUP, ''), templ['comment_close']))
64 continue
65
danghvu8054c9e2013-12-01 13:24:11 -060066 if line == '' or line.startswith('//'):
67 continue
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080068
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080069 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060070 continue
71
72 tmp = line.strip().split(',')
73 for t in tmp:
74 t = t.strip()
75 if not t or t.startswith('//'): continue
76 f = re.split('\s+', t)
77
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080078 if f[0].startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060079 if len(f) > 1 and f[1] not in '//=':
80 print "Error: Unable to convert %s" % f
81 continue
82 elif len(f) > 1 and f[1] == '=':
danghvu5611de02014-01-05 03:35:43 +070083 rhs = ''.join(f[2:])
danghvu8054c9e2013-12-01 13:24:11 -060084 else:
85 rhs = str(count)
86 count += 1
87
danghvub09c1222013-12-04 00:30:45 -060088 try:
danghvub4b6fea2013-12-04 00:19:48 -060089 count = int(rhs) + 1
90 if (count == 1):
91 outfile.write("\n")
danghvub09c1222013-12-04 00:30:45 -060092 except ValueError:
93 pass
danghvu8054c9e2013-12-01 13:24:11 -060094
95 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
96
97 outfile.write(templ['footer'])
98 outfile.close()
99
100def main():
danghvucfb01202013-12-01 13:46:49 -0600101 try:
102 gen(template[sys.argv[1]])
103 except:
104 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -0600105
106if __name__ == "__main__":
107 if len(sys.argv) < 2:
108 print "Usage:", sys.argv[0], " <bindings: java|python>"
109 sys.exit(1)
110 main()