blob: 0b97622a80ce93af0e6a1b3466ee83182be33a53 [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': '',
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]
danghvu8054c9e2013-12-01 13:24:11 -060070 for target in include:
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080071 prefix = templ[target]
72 outfile = open(templ['out_file'] %(prefix), 'w')
73 outfile.write(templ['header'] % (prefix))
danghvu8054c9e2013-12-01 13:24:11 -060074
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080075 lines = open(INCL_DIR + target).readlines()
danghvu8054c9e2013-12-01 13:24:11 -060076
77 count = 0
78 for line in lines:
79 line = line.strip()
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080080
81 if line.startswith(MARKUP): # markup for comments
Nguyen Anh Quynh7957ed12013-12-15 00:32:20 +080082 outfile.write("\n%s%s%s\n" %(templ['comment_open'], \
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080083 line.replace(MARKUP, ''), templ['comment_close']))
84 continue
85
danghvu8054c9e2013-12-01 13:24:11 -060086 if line == '' or line.startswith('//'):
87 continue
Nguyen Anh Quynha2f825f2013-12-04 23:56:24 +080088
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080089 if not line.startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060090 continue
91
92 tmp = line.strip().split(',')
93 for t in tmp:
94 t = t.strip()
95 if not t or t.startswith('//'): continue
96 f = re.split('\s+', t)
97
Nguyen Anh Quynh96a056d2013-12-02 18:37:46 +080098 if f[0].startswith(prefix.upper()):
danghvu8054c9e2013-12-01 13:24:11 -060099 if len(f) > 1 and f[1] not in '//=':
fenuks110ab1d2014-04-11 11:00:33 +0200100 print("Error: Unable to convert %s" % f)
danghvu8054c9e2013-12-01 13:24:11 -0600101 continue
102 elif len(f) > 1 and f[1] == '=':
danghvu5611de02014-01-05 03:35:43 +0700103 rhs = ''.join(f[2:])
danghvu8054c9e2013-12-01 13:24:11 -0600104 else:
105 rhs = str(count)
106 count += 1
107
danghvub09c1222013-12-04 00:30:45 -0600108 try:
danghvub4b6fea2013-12-04 00:19:48 -0600109 count = int(rhs) + 1
110 if (count == 1):
111 outfile.write("\n")
danghvub09c1222013-12-04 00:30:45 -0600112 except ValueError:
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800113 if lang == 'ocaml':
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +0800114 # ocaml uses lsl for '<<', lor for '|'
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800115 rhs = rhs.replace('<<', ' lsl ')
116 rhs = rhs.replace('|', ' lor ')
Nguyen Anh Quynhe483c6e2014-09-22 00:07:58 +0800117 # ocaml variable has _ as prefix
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800118 if rhs[0].isalpha():
119 rhs = '_' + rhs
danghvu8054c9e2013-12-01 13:24:11 -0600120
121 outfile.write(templ['line_format'] %(f[0].strip(), rhs))
122
123 outfile.write(templ['footer'])
124 outfile.close()
125
126def main():
danghvucfb01202013-12-01 13:46:49 -0600127 try:
Nguyen Anh Quynha22d3002014-09-21 23:32:50 +0800128 gen(sys.argv[1])
danghvucfb01202013-12-01 13:46:49 -0600129 except:
130 raise RuntimeError("Unsupported binding %s" % sys.argv[1])
danghvu8054c9e2013-12-01 13:24:11 -0600131
132if __name__ == "__main__":
133 if len(sys.argv) < 2:
Nguyen Anh Quynh586be762014-09-21 23:23:38 +0800134 print("Usage:", sys.argv[0], " <bindings: java|python|ocaml>")
danghvu8054c9e2013-12-01 13:24:11 -0600135 sys.exit(1)
136 main()