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': '', |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 47 | # markup for comments to be added to autogen files |
| 48 | MARKUP = '//>' |
| 49 | |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 50 | def gen(templ): |
| 51 | global include, INCL_DIR |
| 52 | for target in include: |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 53 | prefix = templ[target] |
| 54 | outfile = open(templ['out_file'] %(prefix), 'w') |
| 55 | outfile.write(templ['header'] % (prefix)) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 56 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 57 | lines = open(INCL_DIR + target).readlines() |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 58 | |
| 59 | count = 0 |
| 60 | for line in lines: |
| 61 | line = line.strip() |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 62 | |
| 63 | if line.startswith(MARKUP): # markup for comments |
Nguyen Anh Quynh | 7957ed1 | 2013-12-15 00:32:20 +0800 | [diff] [blame] | 64 | outfile.write("\n%s%s%s\n" %(templ['comment_open'], \ |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 65 | line.replace(MARKUP, ''), templ['comment_close'])) |
| 66 | continue |
| 67 | |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 68 | if line == '' or line.startswith('//'): |
| 69 | continue |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 70 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 71 | if not line.startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 72 | continue |
| 73 | |
| 74 | tmp = line.strip().split(',') |
| 75 | for t in tmp: |
| 76 | t = t.strip() |
| 77 | if not t or t.startswith('//'): continue |
| 78 | f = re.split('\s+', t) |
| 79 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 80 | if f[0].startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 81 | if len(f) > 1 and f[1] not in '//=': |
fenuks | 110ab1d | 2014-04-11 11:00:33 +0200 | [diff] [blame] | 82 | print("Error: Unable to convert %s" % f) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 83 | continue |
| 84 | elif len(f) > 1 and f[1] == '=': |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 85 | rhs = ''.join(f[2:]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 86 | else: |
| 87 | rhs = str(count) |
| 88 | count += 1 |
| 89 | |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 90 | try: |
danghvu | b4b6fea | 2013-12-04 00:19:48 -0600 | [diff] [blame] | 91 | count = int(rhs) + 1 |
| 92 | if (count == 1): |
| 93 | outfile.write("\n") |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 94 | except ValueError: |
| 95 | pass |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 96 | |
| 97 | outfile.write(templ['line_format'] %(f[0].strip(), rhs)) |
| 98 | |
| 99 | outfile.write(templ['footer']) |
| 100 | outfile.close() |
| 101 | |
| 102 | def main(): |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 103 | try: |
| 104 | gen(template[sys.argv[1]]) |
| 105 | except: |
| 106 | raise RuntimeError("Unsupported binding %s" % sys.argv[1]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 107 | |
| 108 | if __name__ == "__main__": |
| 109 | if len(sys.argv) < 2: |
fenuks | 110ab1d | 2014-04-11 11:00:33 +0200 | [diff] [blame] | 110 | print("Usage:", sys.argv[0], " <bindings: java|python>") |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 111 | sys.exit(1) |
| 112 | main() |