| OLD | NEW |
| 1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python2 |
| 2 | 2 |
| 3 import argparse | 3 import argparse |
| 4 import itertools | 4 import itertools |
| 5 import os | 5 import os |
| 6 import re | 6 import re |
| 7 import subprocess | 7 import subprocess |
| 8 import sys | 8 import sys |
| 9 import tempfile | 9 import tempfile |
| 10 | 10 |
| 11 from utils import shellcmd | 11 from utils import shellcmd |
| 12 | 12 |
| 13 def main(): | 13 def main(): |
| 14 """Run the llvm2ice compiler on an llvm file. | 14 """Run the pnacl-sz compiler on an llvm file. |
| 15 | 15 |
| 16 Takes an llvm input file, freezes it into a pexe file, converts | 16 Takes an llvm input file, freezes it into a pexe file, converts |
| 17 it to a Subzero program, and finally compiles it. | 17 it to a Subzero program, and finally compiles it. |
| 18 """ | 18 """ |
| 19 argparser = argparse.ArgumentParser( | 19 argparser = argparse.ArgumentParser( |
| 20 description=' ' + main.__doc__, | 20 description=' ' + main.__doc__, |
| 21 formatter_class=argparse.ArgumentDefaultsHelpFormatter) | 21 formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 22 argparser.add_argument('--input', '-i', required=True, | 22 argparser.add_argument('--input', '-i', required=True, |
| 23 help='LLVM source file to compile') | 23 help='LLVM source file to compile') |
| 24 argparser.add_argument('--insts', required=False, | 24 argparser.add_argument('--insts', required=False, |
| 25 action='store_true', | 25 action='store_true', |
| 26 help='Stop after translating to ' + | 26 help='Stop after translating to ' + |
| 27 'Subzero instructions') | 27 'Subzero instructions') |
| 28 argparser.add_argument('--no-local-syms', required=False, | 28 argparser.add_argument('--no-local-syms', required=False, |
| 29 action='store_true', | 29 action='store_true', |
| 30 help="Don't keep local symbols in the pexe file") | 30 help="Don't keep local symbols in the pexe file") |
| 31 argparser.add_argument('--llvm', required=False, | 31 argparser.add_argument('--llvm', required=False, |
| 32 action='store_true', | 32 action='store_true', |
| 33 help='Parse pexe into llvm IR first, then ' + | 33 help='Parse pexe into llvm IR first, then ' + |
| 34 'convert to Subzero') | 34 'convert to Subzero') |
| 35 argparser.add_argument('--llvm-source', required=False, | 35 argparser.add_argument('--llvm-source', required=False, |
| 36 action='store_true', | 36 action='store_true', |
| 37 help='Parse source directly into llvm IR ' + | 37 help='Parse source directly into llvm IR ' + |
| 38 '(without generating a pexe), then ' + | 38 '(without generating a pexe), then ' + |
| 39 'convert to Subzero') | 39 'convert to Subzero') |
| 40 argparser.add_argument( | 40 argparser.add_argument( |
| 41 '--llvm2ice', required=False, default='./llvm2ice', metavar='LLVM2ICE', | 41 '--pnacl-sz', required=False, default='./pnacl-sz', metavar='PNACL-SZ', |
| 42 help="Subzero translator 'llvm2ice'") | 42 help="Subzero translator 'pnacl-sz'") |
| 43 argparser.add_argument('--llvm-bin-path', required=False, | 43 argparser.add_argument('--llvm-bin-path', required=False, |
| 44 default=None, metavar='LLVM_BIN_PATH', | 44 default=None, metavar='LLVM_BIN_PATH', |
| 45 help='Path to LLVM executables ' + | 45 help='Path to LLVM executables ' + |
| 46 '(for building PEXE files)') | 46 '(for building PEXE files)') |
| 47 argparser.add_argument('--binutils-bin-path', required=False, | 47 argparser.add_argument('--binutils-bin-path', required=False, |
| 48 default=None, metavar='BINUTILS_BIN_PATH', | 48 default=None, metavar='BINUTILS_BIN_PATH', |
| 49 help='Path to Binutils executables') | 49 help='Path to Binutils executables') |
| 50 argparser.add_argument('--assemble', required=False, | 50 argparser.add_argument('--assemble', required=False, |
| 51 action='store_true', | 51 action='store_true', |
| 52 help='Assemble the output') | 52 help='Assemble the output') |
| 53 argparser.add_argument('--disassemble', required=False, | 53 argparser.add_argument('--disassemble', required=False, |
| 54 action='store_true', | 54 action='store_true', |
| 55 help='Disassemble the assembled output') | 55 help='Disassemble the assembled output') |
| 56 argparser.add_argument('--dis-flags', required=False, | 56 argparser.add_argument('--dis-flags', required=False, |
| 57 action='append', default=[], | 57 action='append', default=[], |
| 58 help='Add a disassembler flag') | 58 help='Add a disassembler flag') |
| 59 argparser.add_argument('--filetype', default='iasm', dest='filetype', | 59 argparser.add_argument('--filetype', default='iasm', dest='filetype', |
| 60 choices=['obj', 'asm', 'iasm'], | 60 choices=['obj', 'asm', 'iasm'], |
| 61 help='Output file type. Default %(default)s.') | 61 help='Output file type. Default %(default)s.') |
| 62 argparser.add_argument('--echo-cmd', required=False, | 62 argparser.add_argument('--echo-cmd', required=False, |
| 63 action='store_true', | 63 action='store_true', |
| 64 help='Trace command that generates ICE instructions') | 64 help='Trace command that generates ICE instructions') |
| 65 argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, | 65 argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, |
| 66 default=[], | 66 default=[], |
| 67 help='Remaining arguments are passed to llvm2ice') | 67 help='Remaining arguments are passed to pnacl-sz') |
| 68 | 68 |
| 69 args = argparser.parse_args() | 69 args = argparser.parse_args() |
| 70 llvm_bin_path = args.llvm_bin_path | 70 llvm_bin_path = args.llvm_bin_path |
| 71 binutils_bin_path = args.binutils_bin_path | 71 binutils_bin_path = args.binutils_bin_path |
| 72 llfile = args.input | 72 llfile = args.input |
| 73 | 73 |
| 74 if args.llvm and args.llvm_source: | 74 if args.llvm and args.llvm_source: |
| 75 raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") | 75 raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") |
| 76 | 76 |
| 77 if args.llvm_source and args.no_local_syms: | 77 if args.llvm_source and args.no_local_syms: |
| 78 raise RuntimeError("Can't specify both '--llvm-source' and " + | 78 raise RuntimeError("Can't specify both '--llvm-source' and " + |
| 79 "'--no-local-syms'") | 79 "'--no-local-syms'") |
| 80 | 80 |
| 81 cmd = [] | 81 cmd = [] |
| 82 if not args.llvm_source: | 82 if not args.llvm_source: |
| 83 cmd = [os.path.join(llvm_bin_path, 'llvm-as'), llfile, '-o', '-', '|', | 83 cmd = [os.path.join(llvm_bin_path, 'llvm-as'), llfile, '-o', '-', '|', |
| 84 os.path.join(llvm_bin_path, 'pnacl-freeze')] | 84 os.path.join(llvm_bin_path, 'pnacl-freeze')] |
| 85 if not args.no_local_syms: | 85 if not args.no_local_syms: |
| 86 cmd += ['--allow-local-symbol-tables'] | 86 cmd += ['--allow-local-symbol-tables'] |
| 87 cmd += ['|'] | 87 cmd += ['|'] |
| 88 cmd += [args.llvm2ice] | 88 cmd += [args.pnacl_sz] |
| 89 if args.insts: | 89 if args.insts: |
| 90 # If the tests are based on '-verbose inst' output, force | 90 # If the tests are based on '-verbose inst' output, force |
| 91 # single-threaded translation because dump output does not get | 91 # single-threaded translation because dump output does not get |
| 92 # reassembled into order. | 92 # reassembled into order. |
| 93 cmd += ['-verbose', 'inst', '-notranslate', '-threads=0'] | 93 cmd += ['-verbose', 'inst', '-notranslate', '-threads=0'] |
| 94 if not args.llvm_source: | 94 if not args.llvm_source: |
| 95 cmd += ['--bitcode-format=pnacl'] | 95 cmd += ['--bitcode-format=pnacl'] |
| 96 if not args.no_local_syms: | 96 if not args.no_local_syms: |
| 97 cmd += ['--allow-local-symbol-tables'] | 97 cmd += ['--allow-local-symbol-tables'] |
| 98 if args.llvm or args.llvm_source: | 98 if args.llvm or args.llvm_source: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 123 ['-w', '-d', '-r', '-Mintel', asm_temp.name]) | 123 ['-w', '-d', '-r', '-Mintel', asm_temp.name]) |
| 124 | 124 |
| 125 stdout_result = shellcmd(cmd, echo=args.echo_cmd) | 125 stdout_result = shellcmd(cmd, echo=args.echo_cmd) |
| 126 if not args.echo_cmd: | 126 if not args.echo_cmd: |
| 127 sys.stdout.write(stdout_result) | 127 sys.stdout.write(stdout_result) |
| 128 if asm_temp: | 128 if asm_temp: |
| 129 os.remove(asm_temp.name) | 129 os.remove(asm_temp.name) |
| 130 | 130 |
| 131 if __name__ == '__main__': | 131 if __name__ == '__main__': |
| 132 main() | 132 main() |
| OLD | NEW |