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 | |
Fotis Loukos | 0a159f1 | 2016-05-03 16:37:52 +0300 | [diff] [blame^] | 8 | include = [ 'arm.h', 'arm64.h', 'm68k.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h', 'tms320c64x.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', |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 27 | 'comment_open': '\t//', |
| 28 | 'comment_close': '', |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 29 | }, |
| 30 | 'python': { |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 31 | '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] | 32 | 'footer': "", |
| 33 | 'line_format': '%s = %s\n', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 34 | 'out_file': './python/capstone/%s_const.py', |
| 35 | # prefixes for constant filenames of all archs - case sensitive |
| 36 | 'arm.h': 'arm', |
| 37 | 'arm64.h': 'arm64', |
Daniel Collin | 2ee675c | 2015-08-03 18:45:08 +0200 | [diff] [blame] | 38 | 'm68k.h': 'm68k', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 39 | 'mips.h': 'mips', |
| 40 | 'x86.h': 'x86', |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 41 | 'ppc.h': 'ppc', |
Nguyen Anh Quynh | 1055a2e | 2014-03-10 14:37:08 +0800 | [diff] [blame] | 42 | 'sparc.h': 'sparc', |
Nguyen Anh Quynh | 1c8405d | 2014-03-23 11:17:24 +0800 | [diff] [blame] | 43 | 'systemz.h': 'sysz', |
Nguyen Anh Quynh | 553bb48 | 2014-05-26 23:47:04 +0800 | [diff] [blame] | 44 | 'xcore.h': 'xcore', |
Fotis Loukos | 0a159f1 | 2016-05-03 16:37:52 +0300 | [diff] [blame^] | 45 | 'tms320c64x.h': 'tms320c64x', |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 46 | 'comment_open': '#', |
| 47 | 'comment_close': '', |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 48 | }, |
| 49 | 'ocaml': { |
| 50 | 'header': "(* For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.ml] *)\n", |
| 51 | 'footer': "", |
| 52 | 'line_format': 'let _%s = %s;;\n', |
| 53 | 'out_file': './ocaml/%s_const.ml', |
| 54 | # prefixes for constant filenames of all archs - case sensitive |
| 55 | 'arm.h': 'arm', |
| 56 | 'arm64.h': 'arm64', |
| 57 | 'mips.h': 'mips', |
Nguyen Anh Quynh | 57bf77a | 2015-10-03 11:32:24 +0800 | [diff] [blame] | 58 | 'm68k.h': 'm68k', |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 59 | 'x86.h': 'x86', |
| 60 | 'ppc.h': 'ppc', |
| 61 | 'sparc.h': 'sparc', |
| 62 | 'systemz.h': 'sysz', |
| 63 | 'xcore.h': 'xcore', |
Fotis Loukos | 0a159f1 | 2016-05-03 16:37:52 +0300 | [diff] [blame^] | 64 | 'tms320c64x.h': 'tms320c64x', |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 65 | 'comment_open': '(*', |
| 66 | 'comment_close': ' *)', |
| 67 | }, |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 70 | # markup for comments to be added to autogen files |
| 71 | MARKUP = '//>' |
| 72 | |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 73 | def gen(lang): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 74 | global include, INCL_DIR |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 75 | templ = template[lang] |
learn_more | 8c7b320 | 2015-08-02 20:09:41 +0200 | [diff] [blame] | 76 | print('Generating bindings for', lang) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 77 | for target in include: |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 78 | prefix = templ[target] |
learn_more | 78de4fa | 2015-08-02 20:10:35 +0200 | [diff] [blame] | 79 | outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines |
Nicolas PLANEL | 101bc4e | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 80 | outfile.write((templ['header'] % (prefix)).encode("utf-8")) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 81 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 82 | lines = open(INCL_DIR + target).readlines() |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 83 | |
| 84 | count = 0 |
| 85 | for line in lines: |
| 86 | line = line.strip() |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 87 | |
| 88 | if line.startswith(MARKUP): # markup for comments |
Nicolas PLANEL | 101bc4e | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 89 | outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \ |
| 90 | line.replace(MARKUP, ''), \ |
| 91 | templ['comment_close']) ).encode("utf-8")) |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 92 | continue |
| 93 | |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 94 | if line == '' or line.startswith('//'): |
| 95 | continue |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 96 | |
learn_more | f6ded66 | 2015-08-02 20:12:51 +0200 | [diff] [blame] | 97 | if line.startswith('#define '): |
| 98 | line = line[8:] #cut off define |
| 99 | xline = re.split('\s+', line, 1) #split to at most 2 express |
| 100 | if len(xline) != 2: |
| 101 | continue |
| 102 | if '(' in xline[0] or ')' in xline[0]: #does it look like a function |
| 103 | continue |
| 104 | xline.insert(1, '=') # insert an = so the expression below can parse it |
| 105 | line = ' '.join(xline) |
| 106 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 107 | if not line.startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 108 | continue |
| 109 | |
| 110 | tmp = line.strip().split(',') |
| 111 | for t in tmp: |
| 112 | t = t.strip() |
| 113 | if not t or t.startswith('//'): continue |
Nguyen Anh Quynh | 10647ae | 2015-03-25 17:35:59 +0800 | [diff] [blame] | 114 | # hacky: remove type cast (uint64_t) |
| 115 | t = t.replace('(uint64_t)', '') |
learn_more | f6ded66 | 2015-08-02 20:12:51 +0200 | [diff] [blame] | 116 | 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] | 117 | f = re.split('\s+', t) |
| 118 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 119 | if f[0].startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 120 | if len(f) > 1 and f[1] not in '//=': |
fenuks | 110ab1d | 2014-04-11 11:00:33 +0200 | [diff] [blame] | 121 | print("Error: Unable to convert %s" % f) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 122 | continue |
| 123 | elif len(f) > 1 and f[1] == '=': |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 124 | rhs = ''.join(f[2:]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 125 | else: |
| 126 | rhs = str(count) |
| 127 | count += 1 |
| 128 | |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 129 | try: |
danghvu | b4b6fea | 2013-12-04 00:19:48 -0600 | [diff] [blame] | 130 | count = int(rhs) + 1 |
| 131 | if (count == 1): |
Nicolas PLANEL | 101bc4e | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 132 | outfile.write(("\n").encode("utf-8")) |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 133 | except ValueError: |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 134 | if lang == 'ocaml': |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 135 | # ocaml uses lsl for '<<', lor for '|' |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 136 | rhs = rhs.replace('<<', ' lsl ') |
| 137 | rhs = rhs.replace('|', ' lor ') |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 138 | # ocaml variable has _ as prefix |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 139 | if rhs[0].isalpha(): |
| 140 | rhs = '_' + rhs |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 141 | |
Nicolas PLANEL | 101bc4e | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 142 | outfile.write((templ['line_format'] %(f[0].strip(), rhs)).encode("utf-8")) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 143 | |
Nicolas PLANEL | 101bc4e | 2015-10-03 14:59:51 +1000 | [diff] [blame] | 144 | outfile.write((templ['footer']).encode("utf-8")) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 145 | outfile.close() |
| 146 | |
| 147 | def main(): |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 148 | try: |
learn_more | 8c7b320 | 2015-08-02 20:09:41 +0200 | [diff] [blame] | 149 | if sys.argv[1] == 'all': |
| 150 | for key in template.keys(): |
| 151 | gen(key) |
| 152 | else: |
| 153 | gen(sys.argv[1]) |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 154 | except: |
| 155 | raise RuntimeError("Unsupported binding %s" % sys.argv[1]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 156 | |
| 157 | if __name__ == "__main__": |
| 158 | if len(sys.argv) < 2: |
learn_more | 8c7b320 | 2015-08-02 20:09:41 +0200 | [diff] [blame] | 159 | print("Usage:", sys.argv[0], " <bindings: java|python|ocaml|all>") |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 160 | sys.exit(1) |
| 161 | main() |