Nguyen Anh Quynh | ac6d1da | 2013-12-02 17:44:48 +0800 | [diff] [blame] | 1 | # Capstone Disassembler Engine |
| 2 | # By Dang Hoang Vu, 2013 |
fenuks | 110ab1d | 2014-04-11 11:00:33 +0200 | [diff] [blame] | 3 | from __future__ import print_function |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 4 | import sys, re |
| 5 | |
Nguyen Anh Quynh | 72d3c4f | 2015-02-28 08:42:40 +0800 | [diff] [blame] | 6 | INCL_DIR = '../include/capstone/' |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 7 | |
Sebastian Macke | 8663d75 | 2018-12-04 21:47:03 +0100 | [diff] [blame^] | 8 | include = [ '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' ] |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 9 | |
| 10 | template = { |
| 11 | 'java': { |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 12 | 'header': "// For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %s_const {\n", |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 13 | 'footer': "}", |
| 14 | 'line_format': '\tpublic static final int %s = %s;\n', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 15 | '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 Quynh | 57bf77a | 2015-10-03 11:32:24 +0800 | [diff] [blame] | 19 | 'm68k.h': 'M68k', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 20 | 'mips.h': 'Mips', |
| 21 | 'x86.h': 'X86', |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 22 | 'ppc.h': 'Ppc', |
Nguyen Anh Quynh | 1055a2e | 2014-03-10 14:37:08 +0800 | [diff] [blame] | 23 | 'sparc.h': 'Sparc', |
Nguyen Anh Quynh | 1c8405d | 2014-03-23 11:17:24 +0800 | [diff] [blame] | 24 | 'systemz.h': 'Sysz', |
Nguyen Anh Quynh | 553bb48 | 2014-05-26 23:47:04 +0800 | [diff] [blame] | 25 | 'xcore.h': 'Xcore', |
Fotis Loukos | 0a159f1 | 2016-05-03 16:37:52 +0300 | [diff] [blame] | 26 | 'tms320c64x.h': 'TMS320C64x', |
Wolfgang Schwotzer | 22b4d0e | 2017-10-21 15:44:36 +0200 | [diff] [blame] | 27 | 'm680x.h': 'M680x', |
Nguyen Anh Quynh | ed1246d | 2018-03-31 17:29:22 +0800 | [diff] [blame] | 28 | 'evm.h': 'Evm', |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 29 | 'comment_open': '\t//', |
| 30 | 'comment_close': '', |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 31 | }, |
| 32 | 'python': { |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 33 | 'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n", |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 34 | 'footer': "", |
| 35 | 'line_format': '%s = %s\n', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 36 | '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 Collin | 2ee675c | 2015-08-03 18:45:08 +0200 | [diff] [blame] | 40 | 'm68k.h': 'm68k', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 41 | 'mips.h': 'mips', |
| 42 | 'x86.h': 'x86', |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 43 | 'ppc.h': 'ppc', |
Nguyen Anh Quynh | 1055a2e | 2014-03-10 14:37:08 +0800 | [diff] [blame] | 44 | 'sparc.h': 'sparc', |
Nguyen Anh Quynh | 1c8405d | 2014-03-23 11:17:24 +0800 | [diff] [blame] | 45 | 'systemz.h': 'sysz', |
Nguyen Anh Quynh | 553bb48 | 2014-05-26 23:47:04 +0800 | [diff] [blame] | 46 | 'xcore.h': 'xcore', |
Fotis Loukos | 0a159f1 | 2016-05-03 16:37:52 +0300 | [diff] [blame] | 47 | 'tms320c64x.h': 'tms320c64x', |
Wolfgang Schwotzer | 22b4d0e | 2017-10-21 15:44:36 +0200 | [diff] [blame] | 48 | 'm680x.h': 'm680x', |
Nguyen Anh Quynh | ed1246d | 2018-03-31 17:29:22 +0800 | [diff] [blame] | 49 | 'evm.h': 'evm', |
Sebastian Macke | 8663d75 | 2018-12-04 21:47:03 +0100 | [diff] [blame^] | 50 | 'mos65xx.h': 'mos65xx', |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 51 | 'comment_open': '#', |
| 52 | 'comment_close': '', |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 53 | }, |
| 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 Quynh | 57bf77a | 2015-10-03 11:32:24 +0800 | [diff] [blame] | 63 | 'm68k.h': 'm68k', |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 64 | 'x86.h': 'x86', |
| 65 | 'ppc.h': 'ppc', |
| 66 | 'sparc.h': 'sparc', |
| 67 | 'systemz.h': 'sysz', |
| 68 | 'xcore.h': 'xcore', |
Fotis Loukos | 0a159f1 | 2016-05-03 16:37:52 +0300 | [diff] [blame] | 69 | 'tms320c64x.h': 'tms320c64x', |
Wolfgang Schwotzer | 22b4d0e | 2017-10-21 15:44:36 +0200 | [diff] [blame] | 70 | 'm680x.h': 'm680x', |
Nguyen Anh Quynh | ed1246d | 2018-03-31 17:29:22 +0800 | [diff] [blame] | 71 | 'evm.h': 'evm', |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 72 | 'comment_open': '(*', |
| 73 | 'comment_close': ' *)', |
| 74 | }, |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 75 | } |
| 76 | |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 77 | # markup for comments to be added to autogen files |
| 78 | MARKUP = '//>' |
| 79 | |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 80 | def gen(lang): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 81 | global include, INCL_DIR |
Nicolas PLANEL | 8b25b0a | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 82 | print('Generating bindings for', lang) |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 83 | templ = template[lang] |
learn_more | 8c7b320 | 2015-08-02 20:09:41 +0200 | [diff] [blame] | 84 | print('Generating bindings for', lang) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 85 | for target in include: |
Sebastian Macke | 8663d75 | 2018-12-04 21:47:03 +0100 | [diff] [blame^] | 86 | if target not in templ: |
| 87 | print("Warning: No binding found for %s" % target) |
| 88 | continue |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 89 | prefix = templ[target] |
Nicolas PLANEL | 8b25b0a | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 90 | outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines |
| 91 | outfile.write((templ['header'] % (prefix)).encode("utf-8")) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 92 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 93 | lines = open(INCL_DIR + target).readlines() |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 94 | |
| 95 | count = 0 |
| 96 | for line in lines: |
| 97 | line = line.strip() |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 98 | |
| 99 | if line.startswith(MARKUP): # markup for comments |
Nicolas PLANEL | 8b25b0a | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 100 | outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \ |
| 101 | line.replace(MARKUP, ''), \ |
| 102 | templ['comment_close']) ).encode("utf-8")) |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 103 | continue |
| 104 | |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 105 | if line == '' or line.startswith('//'): |
| 106 | continue |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 107 | |
learn_more | f6ded66 | 2015-08-02 20:12:51 +0200 | [diff] [blame] | 108 | 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 Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 118 | if not line.startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 119 | 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 Quynh | 10647ae | 2015-03-25 17:35:59 +0800 | [diff] [blame] | 125 | # hacky: remove type cast (uint64_t) |
| 126 | t = t.replace('(uint64_t)', '') |
learn_more | f6ded66 | 2015-08-02 20:12:51 +0200 | [diff] [blame] | 127 | t = re.sub(r'\((\d+)ULL << (\d+)\)', r'\1 << \2', t) # (1ULL<<1) to 1 << 1 |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 128 | f = re.split('\s+', t) |
| 129 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 130 | if f[0].startswith(prefix.upper()): |
Nguyen Anh Quynh | 2b4aec9 | 2018-10-01 20:29:39 +0800 | [diff] [blame] | 131 | if len(f) > 1 and f[1] not in ('//', '///<', '='): |
fenuks | 110ab1d | 2014-04-11 11:00:33 +0200 | [diff] [blame] | 132 | print("Error: Unable to convert %s" % f) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 133 | continue |
| 134 | elif len(f) > 1 and f[1] == '=': |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 135 | rhs = ''.join(f[2:]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 136 | else: |
| 137 | rhs = str(count) |
| 138 | count += 1 |
| 139 | |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 140 | try: |
danghvu | b4b6fea | 2013-12-04 00:19:48 -0600 | [diff] [blame] | 141 | count = int(rhs) + 1 |
| 142 | if (count == 1): |
Nicolas PLANEL | 8b25b0a | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 143 | outfile.write(("\n").encode("utf-8")) |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 144 | except ValueError: |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 145 | if lang == 'ocaml': |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 146 | # ocaml uses lsl for '<<', lor for '|' |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 147 | rhs = rhs.replace('<<', ' lsl ') |
| 148 | rhs = rhs.replace('|', ' lor ') |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 149 | # ocaml variable has _ as prefix |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 150 | if rhs[0].isalpha(): |
| 151 | rhs = '_' + rhs |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 152 | |
Nicolas PLANEL | 8b25b0a | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 153 | outfile.write((templ['line_format'] %(f[0].strip(), rhs)).encode("utf-8")) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 154 | |
Nicolas PLANEL | 8b25b0a | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 155 | outfile.write((templ['footer']).encode("utf-8")) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 156 | outfile.close() |
| 157 | |
| 158 | def main(): |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 159 | try: |
learn_more | 8c7b320 | 2015-08-02 20:09:41 +0200 | [diff] [blame] | 160 | if sys.argv[1] == 'all': |
| 161 | for key in template.keys(): |
| 162 | gen(key) |
| 163 | else: |
| 164 | gen(sys.argv[1]) |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 165 | except: |
| 166 | raise RuntimeError("Unsupported binding %s" % sys.argv[1]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 167 | |
| 168 | if __name__ == "__main__": |
| 169 | if len(sys.argv) < 2: |
learn_more | 8c7b320 | 2015-08-02 20:09:41 +0200 | [diff] [blame] | 170 | print("Usage:", sys.argv[0], " <bindings: java|python|ocaml|all>") |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 171 | sys.exit(1) |
| 172 | main() |