Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | |
| 3 | import argparse |
| 4 | import itertools |
| 5 | import os |
| 6 | import re |
| 7 | import subprocess |
| 8 | import sys |
| 9 | |
| 10 | from utils import shellcmd |
| 11 | |
| 12 | def main(): |
| 13 | """Run the llvm2ice compiler on an llvm file. |
| 14 | |
| 15 | Takes an llvm input file, freezes it into a pexe file, converts |
| 16 | it to a Subzero program, and finally compiles it. |
| 17 | """ |
| 18 | argparser = argparse.ArgumentParser( |
| 19 | description=' ' + main.__doc__, |
| 20 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 21 | argparser.add_argument('--input', '-i', required=True, |
| 22 | help='LLVM source file to compile') |
| 23 | argparser.add_argument('--insts', required=False, |
| 24 | action='store_true', |
| 25 | help='Stop after translating to ' + |
| 26 | 'Subzero instructions') |
| 27 | argparser.add_argument('--no-local-syms', required=False, |
| 28 | action='store_true', |
| 29 | help="Don't keep local symbols in the pexe file") |
| 30 | argparser.add_argument('--llvm', required=False, |
| 31 | action='store_true', |
| 32 | help='Parse pexe into llvm IR first, then ' + |
| 33 | 'convert to Subzero') |
| 34 | argparser.add_argument('--llvm-source', required=False, |
| 35 | action='store_true', |
| 36 | help='Parse source directly into llvm IR ' + |
| 37 | '(without generating a pexe), then ' + |
| 38 | 'convert to Subzero') |
| 39 | argparser.add_argument( |
| 40 | '--llvm2ice', required=False, default='./llvm2ice', metavar='LLVM2ICE', |
| 41 | help="Subzero translator 'llvm2ice'") |
| 42 | argparser.add_argument('--llvm-bin-path', required=False, |
| 43 | default=None, metavar='LLVM_BIN_PATH', |
| 44 | help='Path to LLVM executables ' + |
| 45 | '(for building PEXE files)') |
| 46 | argparser.add_argument('--echo-cmd', required=False, |
| 47 | action='store_true', |
| 48 | help='Trace command that generates ICE instructions') |
| 49 | argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, |
| 50 | help='Remaining arguments are passed to llvm2ice') |
| 51 | |
| 52 | args = argparser.parse_args() |
| 53 | llvm_bin_path = args.llvm_bin_path |
| 54 | llfile = args.input |
| 55 | |
| 56 | if args.llvm and args.llvm_source: |
| 57 | raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") |
| 58 | |
| 59 | if args.llvm_source and args.no_local_syms: |
| 60 | raise RuntimeError("Can't specify both '--llvm-source' and " + |
| 61 | "'--no-local-syms'") |
| 62 | |
| 63 | cmd = [] |
| 64 | if not args.llvm_source: |
| 65 | cmd = [os.path.join(llvm_bin_path, 'llvm-as'), llfile, '-o', '-', '|', |
| 66 | os.path.join(llvm_bin_path, 'pnacl-freeze')] |
| 67 | if not args.no_local_syms: |
| 68 | cmd += ['--allow-local-symbol-tables'] |
| 69 | cmd += ['|'] |
| 70 | cmd += [args.llvm2ice] |
| 71 | if args.insts: |
| 72 | cmd += ['-verbose', 'inst', '-notranslate'] |
| 73 | if not args.llvm_source: |
| 74 | cmd += ['--bitcode-format=pnacl'] |
| 75 | if not args.no_local_syms: |
| 76 | cmd += ['--allow-local-symbol-tables'] |
| 77 | if not (args.llvm or args.llvm_source): |
| 78 | cmd += ['--build-on-read'] |
| 79 | if args.args: |
| 80 | cmd += args.args |
| 81 | if args.llvm_source: |
| 82 | cmd += [llfile] |
| 83 | |
| 84 | stdout_result = shellcmd(cmd, echo=args.echo_cmd) |
| 85 | if not args.echo_cmd: |
| 86 | sys.stdout.write(stdout_result) |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | main() |