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