| OLD | NEW |
| (Empty) |
| 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 import tempfile | |
| 10 | |
| 11 from utils import shellcmd | |
| 12 | |
| 13 def main(): | |
| 14 """Run the llvm2ice compiler on an llvm file. | |
| 15 | |
| 16 Takes an llvm input file, freezes it into a pexe file, converts | |
| 17 it to a Subzero program, and finally compiles it. | |
| 18 """ | |
| 19 argparser = argparse.ArgumentParser( | |
| 20 description=' ' + main.__doc__, | |
| 21 formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
| 22 argparser.add_argument('--input', '-i', required=True, | |
| 23 help='LLVM source file to compile') | |
| 24 argparser.add_argument('--insts', required=False, | |
| 25 action='store_true', | |
| 26 help='Stop after translating to ' + | |
| 27 'Subzero instructions') | |
| 28 argparser.add_argument('--no-local-syms', required=False, | |
| 29 action='store_true', | |
| 30 help="Don't keep local symbols in the pexe file") | |
| 31 argparser.add_argument('--llvm', required=False, | |
| 32 action='store_true', | |
| 33 help='Parse pexe into llvm IR first, then ' + | |
| 34 'convert to Subzero') | |
| 35 argparser.add_argument('--llvm-source', required=False, | |
| 36 action='store_true', | |
| 37 help='Parse source directly into llvm IR ' + | |
| 38 '(without generating a pexe), then ' + | |
| 39 'convert to Subzero') | |
| 40 argparser.add_argument( | |
| 41 '--llvm2ice', required=False, default='./llvm2ice', metavar='LLVM2ICE', | |
| 42 help="Subzero translator 'llvm2ice'") | |
| 43 argparser.add_argument('--llvm-bin-path', required=False, | |
| 44 default=None, metavar='LLVM_BIN_PATH', | |
| 45 help='Path to LLVM executables ' + | |
| 46 '(for building PEXE files)') | |
| 47 argparser.add_argument('--binutils-bin-path', required=False, | |
| 48 default=None, metavar='BINUTILS_BIN_PATH', | |
| 49 help='Path to Binutils executables') | |
| 50 argparser.add_argument('--assemble', required=False, | |
| 51 action='store_true', | |
| 52 help='Assemble the output') | |
| 53 argparser.add_argument('--disassemble', required=False, | |
| 54 action='store_true', | |
| 55 help='Disassemble the assembled output') | |
| 56 argparser.add_argument('--dis-flags', required=False, | |
| 57 action='append', default=[], | |
| 58 help='Add a disassembler flag') | |
| 59 argparser.add_argument('--filetype', default='iasm', dest='filetype', | |
| 60 choices=['obj', 'asm', 'iasm'], | |
| 61 help='Output file type. Default %(default)s.') | |
| 62 argparser.add_argument('--echo-cmd', required=False, | |
| 63 action='store_true', | |
| 64 help='Trace command that generates ICE instructions') | |
| 65 argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, | |
| 66 default=[], | |
| 67 help='Remaining arguments are passed to llvm2ice') | |
| 68 | |
| 69 args = argparser.parse_args() | |
| 70 llvm_bin_path = args.llvm_bin_path | |
| 71 binutils_bin_path = args.binutils_bin_path | |
| 72 llfile = args.input | |
| 73 | |
| 74 if args.llvm and args.llvm_source: | |
| 75 raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") | |
| 76 | |
| 77 if args.llvm_source and args.no_local_syms: | |
| 78 raise RuntimeError("Can't specify both '--llvm-source' and " + | |
| 79 "'--no-local-syms'") | |
| 80 | |
| 81 cmd = [] | |
| 82 if not args.llvm_source: | |
| 83 cmd = [os.path.join(llvm_bin_path, 'llvm-as'), llfile, '-o', '-', '|', | |
| 84 os.path.join(llvm_bin_path, 'pnacl-freeze')] | |
| 85 if not args.no_local_syms: | |
| 86 cmd += ['--allow-local-symbol-tables'] | |
| 87 cmd += ['|'] | |
| 88 cmd += [args.llvm2ice] | |
| 89 if args.insts: | |
| 90 # If the tests are based on '-verbose inst' output, force | |
| 91 # single-threaded translation because dump output does not get | |
| 92 # reassembled into order. | |
| 93 cmd += ['-verbose', 'inst', '-notranslate', '-threads=0'] | |
| 94 if not args.llvm_source: | |
| 95 cmd += ['--bitcode-format=pnacl'] | |
| 96 if not args.no_local_syms: | |
| 97 cmd += ['--allow-local-symbol-tables'] | |
| 98 if args.llvm or args.llvm_source: | |
| 99 cmd += ['--build-on-read=0'] | |
| 100 else: | |
| 101 cmd += ['--build-on-read=1'] | |
| 102 cmd += ['--filetype=' + args.filetype] | |
| 103 cmd += args.args | |
| 104 if args.llvm_source: | |
| 105 cmd += [llfile] | |
| 106 asm_temp = None | |
| 107 if args.assemble or args.disassemble: | |
| 108 # On windows we may need to close the file first before it can be | |
| 109 # re-opened by the other tools, so don't do delete-on-close, | |
| 110 # and instead manually delete. | |
| 111 asm_temp = tempfile.NamedTemporaryFile(delete=False) | |
| 112 asm_temp.close() | |
| 113 if args.assemble and args.filetype != 'obj': | |
| 114 cmd += ['|', os.path.join(llvm_bin_path, 'llvm-mc'), | |
| 115 '-triple=i686-none-nacl', | |
| 116 '-filetype=obj', '-o', asm_temp.name] | |
| 117 elif asm_temp: | |
| 118 cmd += ['-o', asm_temp.name] | |
| 119 if args.disassemble: | |
| 120 # Show wide instruction encodings, diassemble, and show relocs. | |
| 121 cmd += (['&&', os.path.join(binutils_bin_path, 'objdump')] + | |
| 122 args.dis_flags + | |
| 123 ['-w', '-d', '-r', '-Mintel', asm_temp.name]) | |
| 124 | |
| 125 stdout_result = shellcmd(cmd, echo=args.echo_cmd) | |
| 126 if not args.echo_cmd: | |
| 127 sys.stdout.write(stdout_result) | |
| 128 if asm_temp: | |
| 129 os.remove(asm_temp.name) | |
| 130 | |
| 131 if __name__ == '__main__': | |
| 132 main() | |
| OLD | NEW |