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