danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 1 | import sys, re |
| 2 | |
| 3 | INCL_DIR = '../include' |
| 4 | |
| 5 | include = [ |
| 6 | ('/arm.h', 'ARM_'), |
| 7 | ('/arm64.h', 'ARM64_'), |
| 8 | ('/x86.h', 'X86_'), |
| 9 | ('/mips.h', 'MIPS_'), |
| 10 | ] |
| 11 | |
| 12 | template = { |
| 13 | 'java': { |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame^] | 14 | 'header': "// AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %sconst {\n", |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 15 | 'footer': "}", |
| 16 | 'line_format': '\tpublic static final int %s = %s;\n', |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame^] | 17 | 'out_file': './java/capstone/%sconst.java', |
| 18 | }, |
| 19 | 'python': { |
| 20 | 'header': "# AUTO-GENERATED FILE, DO NOT EDIT [%s]\n", |
| 21 | 'footer': "", |
| 22 | 'line_format': '%s = %s\n', |
| 23 | 'out_file': './python/capstone/%sconst.py', |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
| 27 | def gen(templ): |
| 28 | global include, INCL_DIR |
| 29 | for target in include: |
| 30 | prefix = target[1]; |
| 31 | outfile = open(templ['out_file'] %(prefix.capitalize()), 'w') |
| 32 | outfile.write(templ['header'] % (prefix.capitalize())) |
| 33 | |
| 34 | lines = open(INCL_DIR + target[0]).readlines() |
| 35 | |
| 36 | count = 0 |
| 37 | for line in lines: |
| 38 | line = line.strip() |
| 39 | if line == '' or line.startswith('//'): |
| 40 | continue |
| 41 | if not line.startswith(prefix): |
| 42 | continue |
| 43 | |
| 44 | tmp = line.strip().split(',') |
| 45 | for t in tmp: |
| 46 | t = t.strip() |
| 47 | if not t or t.startswith('//'): continue |
| 48 | f = re.split('\s+', t) |
| 49 | |
| 50 | if f[0].startswith(prefix): |
| 51 | if len(f) > 1 and f[1] not in '//=': |
| 52 | print "Error: Unable to convert %s" % f |
| 53 | continue |
| 54 | elif len(f) > 1 and f[1] == '=': |
| 55 | rhs = f[2] |
| 56 | else: |
| 57 | rhs = str(count) |
| 58 | count += 1 |
| 59 | |
| 60 | if rhs == '0': |
| 61 | outfile.write("\n") |
| 62 | count = 1 |
| 63 | |
| 64 | outfile.write(templ['line_format'] %(f[0].strip(), rhs)) |
| 65 | |
| 66 | outfile.write(templ['footer']) |
| 67 | outfile.close() |
| 68 | |
| 69 | def main(): |
danghvu | cfb0120 | 2013-12-01 13:46:49 -0600 | [diff] [blame^] | 70 | try: |
| 71 | gen(template[sys.argv[1]]) |
| 72 | except: |
| 73 | raise RuntimeError("Unsupported binding %s" % sys.argv[1]) |
danghvu | 8054c9e | 2013-12-01 13:24:11 -0600 | [diff] [blame] | 74 | |
| 75 | if __name__ == "__main__": |
| 76 | if len(sys.argv) < 2: |
| 77 | print "Usage:", sys.argv[0], " <bindings: java|python>" |
| 78 | sys.exit(1) |
| 79 | main() |