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 | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 6 | INCL_DIR = '../include/' |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 7 | |
Nguyen Anh Quynh | 553bb48 | 2014-05-26 23:47:04 +0800 | [diff] [blame] | 8 | include = [ 'arm.h', 'arm64.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', |
| 19 | 'mips.h': 'Mips', |
| 20 | 'x86.h': 'X86', |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 21 | 'ppc.h': 'Ppc', |
Nguyen Anh Quynh | 1055a2e | 2014-03-10 14:37:08 +0800 | [diff] [blame] | 22 | 'sparc.h': 'Sparc', |
Nguyen Anh Quynh | 1c8405d | 2014-03-23 11:17:24 +0800 | [diff] [blame] | 23 | 'systemz.h': 'Sysz', |
Nguyen Anh Quynh | 553bb48 | 2014-05-26 23:47:04 +0800 | [diff] [blame] | 24 | 'xcore.h': 'Xcore', |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 25 | 'comment_open': '\t//', |
| 26 | 'comment_close': '', |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 27 | }, |
| 28 | 'python': { |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 29 | '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] | 30 | 'footer': "", |
| 31 | 'line_format': '%s = %s\n', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 32 | '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', |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 38 | 'ppc.h': 'ppc', |
Nguyen Anh Quynh | 1055a2e | 2014-03-10 14:37:08 +0800 | [diff] [blame] | 39 | 'sparc.h': 'sparc', |
Nguyen Anh Quynh | 1c8405d | 2014-03-23 11:17:24 +0800 | [diff] [blame] | 40 | 'systemz.h': 'sysz', |
Nguyen Anh Quynh | 553bb48 | 2014-05-26 23:47:04 +0800 | [diff] [blame] | 41 | 'xcore.h': 'xcore', |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 42 | 'comment_open': '#', |
| 43 | 'comment_close': '', |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 44 | }, |
| 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 | }, |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 64 | # markup for comments to be added to autogen files |
| 65 | MARKUP = '//>' |
| 66 | |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 67 | def gen(lang): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 68 | global include, INCL_DIR |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 69 | templ = template[lang] |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 70 | for target in include: |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 71 | prefix = templ[target] |
| 72 | outfile = open(templ['out_file'] %(prefix), 'w') |
| 73 | outfile.write(templ['header'] % (prefix)) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 74 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 75 | lines = open(INCL_DIR + target).readlines() |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 76 | |
| 77 | count = 0 |
| 78 | for line in lines: |
| 79 | line = line.strip() |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 80 | |
| 81 | if line.startswith(MARKUP): # markup for comments |
Nguyen Anh Quynh | 7957ed1 | 2013-12-15 00:32:20 +0800 | [diff] [blame] | 82 | outfile.write("\n%s%s%s\n" %(templ['comment_open'], \ |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 83 | line.replace(MARKUP, ''), templ['comment_close'])) |
| 84 | continue |
| 85 | |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 86 | if line == '' or line.startswith('//'): |
| 87 | continue |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 88 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 89 | if not line.startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 90 | 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 Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 98 | if f[0].startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 99 | if len(f) > 1 and f[1] not in '//=': |
fenuks | 110ab1d | 2014-04-11 11:00:33 +0200 | [diff] [blame] | 100 | print("Error: Unable to convert %s" % f) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 101 | continue |
| 102 | elif len(f) > 1 and f[1] == '=': |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 103 | rhs = ''.join(f[2:]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 104 | else: |
| 105 | rhs = str(count) |
| 106 | count += 1 |
| 107 | |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 108 | try: |
danghvu | b4b6fea | 2013-12-04 00:19:48 -0600 | [diff] [blame] | 109 | count = int(rhs) + 1 |
| 110 | if (count == 1): |
| 111 | outfile.write("\n") |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 112 | except ValueError: |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 113 | if lang == 'ocaml': |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 114 | # ocaml uses lsl for '<<', lor for '|' |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 115 | rhs = rhs.replace('<<', ' lsl ') |
| 116 | rhs = rhs.replace('|', ' lor ') |
Nguyen Anh Quynh | e483c6e | 2014-09-22 00:07:58 +0800 | [diff] [blame] | 117 | # ocaml variable has _ as prefix |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 118 | if rhs[0].isalpha(): |
| 119 | rhs = '_' + rhs |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 120 | |
| 121 | outfile.write(templ['line_format'] %(f[0].strip(), rhs)) |
| 122 | |
| 123 | outfile.write(templ['footer']) |
| 124 | outfile.close() |
| 125 | |
| 126 | def main(): |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 127 | try: |
Nguyen Anh Quynh | a22d300 | 2014-09-21 23:32:50 +0800 | [diff] [blame] | 128 | gen(sys.argv[1]) |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 129 | except: |
| 130 | raise RuntimeError("Unsupported binding %s" % sys.argv[1]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 131 | |
| 132 | if __name__ == "__main__": |
| 133 | if len(sys.argv) < 2: |
Nguyen Anh Quynh | 586be76 | 2014-09-21 23:23:38 +0800 | [diff] [blame] | 134 | print("Usage:", sys.argv[0], " <bindings: java|python|ocaml>") |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 135 | sys.exit(1) |
| 136 | main() |