Nguyen Anh Quynh | ac6d1da | 2013-12-02 17:44:48 +0800 | [diff] [blame] | 1 | # Capstone Disassembler Engine |
| 2 | # By Dang Hoang Vu, 2013 |
| 3 | |
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 | 1055a2e | 2014-03-10 14:37:08 +0800 | [diff] [blame] | 8 | include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.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 | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 23 | 'comment_open': '\t//', |
| 24 | 'comment_close': '', |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 25 | }, |
| 26 | 'python': { |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 27 | '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] | 28 | 'footer': "", |
| 29 | 'line_format': '%s = %s\n', |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 30 | 'out_file': './python/capstone/%s_const.py', |
| 31 | # prefixes for constant filenames of all archs - case sensitive |
| 32 | 'arm.h': 'arm', |
| 33 | 'arm64.h': 'arm64', |
| 34 | 'mips.h': 'mips', |
| 35 | 'x86.h': 'x86', |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 36 | 'ppc.h': 'ppc', |
Nguyen Anh Quynh | 1055a2e | 2014-03-10 14:37:08 +0800 | [diff] [blame] | 37 | 'sparc.h': 'sparc', |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 38 | 'comment_open': '#', |
| 39 | 'comment_close': '', |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 43 | # markup for comments to be added to autogen files |
| 44 | MARKUP = '//>' |
| 45 | |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 46 | def gen(templ): |
| 47 | global include, INCL_DIR |
| 48 | for target in include: |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 49 | prefix = templ[target] |
| 50 | outfile = open(templ['out_file'] %(prefix), 'w') |
| 51 | outfile.write(templ['header'] % (prefix)) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 52 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 53 | lines = open(INCL_DIR + target).readlines() |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 54 | |
| 55 | count = 0 |
| 56 | for line in lines: |
| 57 | line = line.strip() |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 58 | |
| 59 | if line.startswith(MARKUP): # markup for comments |
Nguyen Anh Quynh | 7957ed1 | 2013-12-15 00:32:20 +0800 | [diff] [blame] | 60 | outfile.write("\n%s%s%s\n" %(templ['comment_open'], \ |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 61 | line.replace(MARKUP, ''), templ['comment_close'])) |
| 62 | continue |
| 63 | |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 64 | if line == '' or line.startswith('//'): |
| 65 | continue |
Nguyen Anh Quynh | a2f825f | 2013-12-04 23:56:24 +0800 | [diff] [blame] | 66 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 67 | if not line.startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 68 | continue |
| 69 | |
| 70 | tmp = line.strip().split(',') |
| 71 | for t in tmp: |
| 72 | t = t.strip() |
| 73 | if not t or t.startswith('//'): continue |
| 74 | f = re.split('\s+', t) |
| 75 | |
Nguyen Anh Quynh | 96a056d | 2013-12-02 18:37:46 +0800 | [diff] [blame] | 76 | if f[0].startswith(prefix.upper()): |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 77 | if len(f) > 1 and f[1] not in '//=': |
| 78 | print "Error: Unable to convert %s" % f |
| 79 | continue |
| 80 | elif len(f) > 1 and f[1] == '=': |
danghvu | 5611de0 | 2014-01-05 03:35:43 +0700 | [diff] [blame] | 81 | rhs = ''.join(f[2:]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 82 | else: |
| 83 | rhs = str(count) |
| 84 | count += 1 |
| 85 | |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 86 | try: |
danghvu | b4b6fea | 2013-12-04 00:19:48 -0600 | [diff] [blame] | 87 | count = int(rhs) + 1 |
| 88 | if (count == 1): |
| 89 | outfile.write("\n") |
danghvu | b09c122 | 2013-12-04 00:30:45 -0600 | [diff] [blame] | 90 | except ValueError: |
| 91 | pass |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 92 | |
| 93 | outfile.write(templ['line_format'] %(f[0].strip(), rhs)) |
| 94 | |
| 95 | outfile.write(templ['footer']) |
| 96 | outfile.close() |
| 97 | |
| 98 | def main(): |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame] | 99 | try: |
| 100 | gen(template[sys.argv[1]]) |
| 101 | except: |
| 102 | raise RuntimeError("Unsupported binding %s" % sys.argv[1]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 103 | |
| 104 | if __name__ == "__main__": |
| 105 | if len(sys.argv) < 2: |
| 106 | print "Usage:", sys.argv[0], " <bindings: java|python>" |
| 107 | sys.exit(1) |
| 108 | main() |