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