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 | 10 |
10 from utils import shellcmd | 11 from utils import shellcmd |
11 | 12 |
12 def main(): | 13 def main(): |
13 """Run the llvm2ice compiler on an llvm file. | 14 """Run the llvm2ice compiler on an llvm file. |
14 | 15 |
15 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 |
16 it to a Subzero program, and finally compiles it. | 17 it to a Subzero program, and finally compiles it. |
17 """ | 18 """ |
18 argparser = argparse.ArgumentParser( | 19 argparser = argparse.ArgumentParser( |
(...skipping 17 matching lines...) Expand all Loading... |
36 help='Parse source directly into llvm IR ' + | 37 help='Parse source directly into llvm IR ' + |
37 '(without generating a pexe), then ' + | 38 '(without generating a pexe), then ' + |
38 'convert to Subzero') | 39 'convert to Subzero') |
39 argparser.add_argument( | 40 argparser.add_argument( |
40 '--llvm2ice', required=False, default='./llvm2ice', metavar='LLVM2ICE', | 41 '--llvm2ice', required=False, default='./llvm2ice', metavar='LLVM2ICE', |
41 help="Subzero translator 'llvm2ice'") | 42 help="Subzero translator 'llvm2ice'") |
42 argparser.add_argument('--llvm-bin-path', required=False, | 43 argparser.add_argument('--llvm-bin-path', required=False, |
43 default=None, metavar='LLVM_BIN_PATH', | 44 default=None, metavar='LLVM_BIN_PATH', |
44 help='Path to LLVM executables ' + | 45 help='Path to LLVM executables ' + |
45 '(for building PEXE files)') | 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') |
46 argparser.add_argument('--echo-cmd', required=False, | 59 argparser.add_argument('--echo-cmd', required=False, |
47 action='store_true', | 60 action='store_true', |
48 help='Trace command that generates ICE instructions') | 61 help='Trace command that generates ICE instructions') |
49 argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, | 62 argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, |
50 default=[], | 63 default=[], |
51 help='Remaining arguments are passed to llvm2ice') | 64 help='Remaining arguments are passed to llvm2ice') |
52 | 65 |
53 args = argparser.parse_args() | 66 args = argparser.parse_args() |
54 llvm_bin_path = args.llvm_bin_path | 67 llvm_bin_path = args.llvm_bin_path |
| 68 binutils_bin_path = args.binutils_bin_path |
55 llfile = args.input | 69 llfile = args.input |
56 | 70 |
57 if args.llvm and args.llvm_source: | 71 if args.llvm and args.llvm_source: |
58 raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") | 72 raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") |
59 | 73 |
60 if args.llvm_source and args.no_local_syms: | 74 if args.llvm_source and args.no_local_syms: |
61 raise RuntimeError("Can't specify both '--llvm-source' and " + | 75 raise RuntimeError("Can't specify both '--llvm-source' and " + |
62 "'--no-local-syms'") | 76 "'--no-local-syms'") |
63 | 77 |
64 cmd = [] | 78 cmd = [] |
(...skipping 13 matching lines...) Expand all Loading... |
78 cmd += ['--bitcode-format=pnacl'] | 92 cmd += ['--bitcode-format=pnacl'] |
79 if not args.no_local_syms: | 93 if not args.no_local_syms: |
80 cmd += ['--allow-local-symbol-tables'] | 94 cmd += ['--allow-local-symbol-tables'] |
81 if args.llvm or args.llvm_source: | 95 if args.llvm or args.llvm_source: |
82 cmd += ['--build-on-read=0'] | 96 cmd += ['--build-on-read=0'] |
83 else: | 97 else: |
84 cmd += ['--build-on-read=1'] | 98 cmd += ['--build-on-read=1'] |
85 cmd += args.args | 99 cmd += args.args |
86 if args.llvm_source: | 100 if args.llvm_source: |
87 cmd += [llfile] | 101 cmd += [llfile] |
| 102 asm_temp = None |
| 103 if args.assemble or args.disassemble: |
| 104 # On windows we may need to close the file first before it can be |
| 105 # re-opened by the other tools, so don't do delete-on-close, |
| 106 # and instead manually delete. |
| 107 asm_temp = tempfile.NamedTemporaryFile(delete=False) |
| 108 asm_temp.close() |
| 109 if args.assemble: |
| 110 cmd += ['|', os.path.join(llvm_bin_path, 'llvm-mc'), |
| 111 '-triple=i686-none-nacl', |
| 112 '-filetype=obj', '-o', asm_temp.name] |
| 113 if args.disassemble: |
| 114 # Show wide instruction encodings, diassemble, and show relocs. |
| 115 cmd += (['&&', os.path.join(binutils_bin_path, 'objdump')] + |
| 116 args.dis_flags + |
| 117 ['-w', '-d', '-r', '-Mintel', asm_temp.name]) |
88 | 118 |
89 stdout_result = shellcmd(cmd, echo=args.echo_cmd) | 119 stdout_result = shellcmd(cmd, echo=args.echo_cmd) |
90 if not args.echo_cmd: | 120 if not args.echo_cmd: |
91 sys.stdout.write(stdout_result) | 121 sys.stdout.write(stdout_result) |
| 122 if asm_temp: |
| 123 os.remove(asm_temp.name) |
92 | 124 |
93 if __name__ == '__main__': | 125 if __name__ == '__main__': |
94 main() | 126 main() |
OLD | NEW |