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 |
Jan Voung | a2703ae | 2015-02-19 11:27:44 -0800 | [diff] [blame] | 9 | import tempfile |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 10 | |
Sean Klein | 1ece70e | 2016-03-09 15:26:41 -0800 | [diff] [blame] | 11 | from utils import FindBaseNaCl, GetObjdumpCmd, shellcmd |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 12 | |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 13 | |
John Porto | 52b5157 | 2015-12-05 14:16:25 -0800 | [diff] [blame] | 14 | def TargetAssemblerFlags(target, sandboxed): |
Reed Kotler | d00d48d | 2015-07-08 09:49:07 -0700 | [diff] [blame] | 15 | # TODO(reed kotler). Need to find out exactly we need to |
| 16 | # add here for Mips32. |
John Porto | 52b5157 | 2015-12-05 14:16:25 -0800 | [diff] [blame] | 17 | flags = { 'x8632': ['-triple=%s' % ('i686-nacl' if sandboxed else 'i686')], |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 18 | 'x8664': ['-triple=%s' % ( |
| 19 | 'x86_64-nacl' if sandboxed else 'x86_64')], |
John Porto | 52b5157 | 2015-12-05 14:16:25 -0800 | [diff] [blame] | 20 | 'arm32': ['-triple=%s' % ( |
| 21 | 'armv7a-nacl' if sandboxed else 'armv7a'), |
| 22 | '-mcpu=cortex-a9', '-mattr=+neon'], |
Reed Kotler | e105c74 | 2015-07-10 14:52:59 -0700 | [diff] [blame] | 23 | 'mips32': ['-triple=mipsel' ] } |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 24 | return flags[target] |
| 25 | |
| 26 | |
| 27 | def TargetDisassemblerFlags(target): |
| 28 | flags = { 'x8632': ['-Mintel'], |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 29 | 'x8664': ['-Mintel'], |
Reed Kotler | d00d48d | 2015-07-08 09:49:07 -0700 | [diff] [blame] | 30 | 'arm32': [], |
| 31 | 'mips32':[] } |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 32 | return flags[target] |
| 33 | |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 34 | def main(): |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 35 | """Run the pnacl-sz compiler on an llvm file. |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 36 | |
| 37 | Takes an llvm input file, freezes it into a pexe file, converts |
| 38 | it to a Subzero program, and finally compiles it. |
| 39 | """ |
| 40 | argparser = argparse.ArgumentParser( |
| 41 | description=' ' + main.__doc__, |
| 42 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 43 | argparser.add_argument('--input', '-i', required=True, |
| 44 | help='LLVM source file to compile') |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 45 | argparser.add_argument('--output', '-o', required=False, |
| 46 | help='Output file to write') |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 47 | argparser.add_argument('--insts', required=False, |
| 48 | action='store_true', |
| 49 | help='Stop after translating to ' + |
| 50 | 'Subzero instructions') |
| 51 | argparser.add_argument('--no-local-syms', required=False, |
| 52 | action='store_true', |
| 53 | help="Don't keep local symbols in the pexe file") |
| 54 | argparser.add_argument('--llvm', required=False, |
| 55 | action='store_true', |
| 56 | help='Parse pexe into llvm IR first, then ' + |
| 57 | 'convert to Subzero') |
| 58 | argparser.add_argument('--llvm-source', required=False, |
| 59 | action='store_true', |
| 60 | help='Parse source directly into llvm IR ' + |
| 61 | '(without generating a pexe), then ' + |
| 62 | 'convert to Subzero') |
| 63 | argparser.add_argument( |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 64 | '--pnacl-sz', required=False, default='./pnacl-sz', metavar='PNACL-SZ', |
| 65 | help="Subzero translator 'pnacl-sz'") |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 66 | argparser.add_argument('--pnacl-bin-path', required=False, |
Jim Stichnoth | 79fe490 | 2015-12-10 11:08:49 -0800 | [diff] [blame] | 67 | default=( |
| 68 | '{root}/toolchain/linux_x86/pnacl_newlib_raw/bin' |
| 69 | ).format(root=FindBaseNaCl()), |
| 70 | metavar='PNACL_BIN_PATH', |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 71 | help='Path to LLVM & Binutils executables ' + |
| 72 | '(e.g. for building PEXE files)') |
Jan Voung | a2703ae | 2015-02-19 11:27:44 -0800 | [diff] [blame] | 73 | argparser.add_argument('--assemble', required=False, |
| 74 | action='store_true', |
| 75 | help='Assemble the output') |
| 76 | argparser.add_argument('--disassemble', required=False, |
| 77 | action='store_true', |
| 78 | help='Disassemble the assembled output') |
| 79 | argparser.add_argument('--dis-flags', required=False, |
| 80 | action='append', default=[], |
| 81 | help='Add a disassembler flag') |
Jan Voung | b5447a0 | 2015-02-24 16:57:17 -0800 | [diff] [blame] | 82 | argparser.add_argument('--filetype', default='iasm', dest='filetype', |
| 83 | choices=['obj', 'asm', 'iasm'], |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 84 | help='Output file type. Default %(default)s') |
| 85 | argparser.add_argument('--forceasm', required=False, action='store_true', |
| 86 | help='Force --filetype=asm') |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 87 | argparser.add_argument('--target', default='x8632', dest='target', |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 88 | choices=['x8632','x8664','arm32','mips32'], |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 89 | help='Target architecture. Default %(default)s') |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 90 | argparser.add_argument('--echo-cmd', required=False, |
| 91 | action='store_true', |
| 92 | help='Trace command that generates ICE instructions') |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 93 | argparser.add_argument('--tbc', required=False, action='store_true', |
| 94 | help='Input is textual bitcode (not .ll)') |
| 95 | argparser.add_argument('--expect-fail', required=False, action='store_true', |
| 96 | help='Negate success of run by using LLVM not') |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 97 | argparser.add_argument('--allow-pnacl-reader-error-recovery', |
| 98 | action='store_true', |
| 99 | help='Continue parsing after first error') |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 100 | argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 101 | default=[], |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 102 | help='Remaining arguments are passed to pnacl-sz') |
John Porto | 52b5157 | 2015-12-05 14:16:25 -0800 | [diff] [blame] | 103 | argparser.add_argument('--sandbox', required=False, action='store_true', |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 104 | help='Sandboxes the generated code') |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 105 | |
| 106 | args = argparser.parse_args() |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 107 | pnacl_bin_path = args.pnacl_bin_path |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 108 | llfile = args.input |
| 109 | |
| 110 | if args.llvm and args.llvm_source: |
| 111 | raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") |
| 112 | |
| 113 | if args.llvm_source and args.no_local_syms: |
| 114 | raise RuntimeError("Can't specify both '--llvm-source' and " + |
| 115 | "'--no-local-syms'") |
| 116 | |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 117 | if args.llvm_source and args.tbc: |
| 118 | raise RuntimeError("Can't specify both '--tbc' and '--llvm-source'") |
| 119 | |
| 120 | if args.llvm and args.tbc: |
| 121 | raise RuntimeError("Can't specify both '--tbc' and '--llvm'") |
| 122 | |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 123 | if args.forceasm: |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 124 | if args.expect_fail: |
| 125 | args.forceasm = False |
| 126 | elif args.filetype == 'asm': |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 127 | pass |
| 128 | elif args.filetype == 'iasm': |
| 129 | # TODO(sehr) implement forceasm for iasm. |
| 130 | pass |
| 131 | elif args.filetype == 'obj': |
| 132 | args.filetype = 'asm' |
| 133 | args.assemble = True |
| 134 | |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 135 | cmd = [] |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 136 | if args.tbc: |
| 137 | cmd = [os.path.join(pnacl_bin_path, 'pnacl-bcfuzz'), llfile, |
| 138 | '-bitcode-as-text', '-output', '-', '|'] |
| 139 | elif not args.llvm_source: |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 140 | cmd = [os.path.join(pnacl_bin_path, 'llvm-as'), llfile, '-o', '-', '|', |
| 141 | os.path.join(pnacl_bin_path, 'pnacl-freeze')] |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 142 | if not args.no_local_syms: |
| 143 | cmd += ['--allow-local-symbol-tables'] |
| 144 | cmd += ['|'] |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 145 | if args.expect_fail: |
| 146 | cmd += [os.path.join(pnacl_bin_path, 'not')] |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 147 | cmd += [args.pnacl_sz] |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 148 | cmd += ['--target', args.target] |
John Porto | 52b5157 | 2015-12-05 14:16:25 -0800 | [diff] [blame] | 149 | if args.sandbox: |
| 150 | cmd += ['-sandbox'] |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 151 | if args.insts: |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 152 | # If the tests are based on '-verbose inst' output, force |
| 153 | # single-threaded translation because dump output does not get |
| 154 | # reassembled into order. |
Jim Stichnoth | b6dcf3c | 2016-02-29 09:34:01 -0800 | [diff] [blame] | 155 | cmd += ['-verbose', 'inst,global_init', '-notranslate', '-threads=0'] |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 156 | elif args.allow_pnacl_reader_error_recovery: |
| 157 | cmd += ['-allow-pnacl-reader-error-recovery', '-threads=0'] |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 158 | if not args.llvm_source: |
| 159 | cmd += ['--bitcode-format=pnacl'] |
| 160 | if not args.no_local_syms: |
| 161 | cmd += ['--allow-local-symbol-tables'] |
Karl Schimpf | ab06df3 | 2014-10-29 14:58:25 -0700 | [diff] [blame] | 162 | if args.llvm or args.llvm_source: |
| 163 | cmd += ['--build-on-read=0'] |
| 164 | else: |
| 165 | cmd += ['--build-on-read=1'] |
Jan Voung | b5447a0 | 2015-02-24 16:57:17 -0800 | [diff] [blame] | 166 | cmd += ['--filetype=' + args.filetype] |
Jim Stichnoth | 54cf1a2 | 2016-08-08 14:15:00 -0700 | [diff] [blame] | 167 | cmd += ['--emit-revision=0'] |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 168 | script_name = os.path.basename(sys.argv[0]) |
| 169 | for _, arg in enumerate(args.args): |
| 170 | # Redirecting the output file needs to be done through the script |
| 171 | # because forceasm may introduce a new temporary file between pnacl-sz |
| 172 | # and llvm-mc. Similar issues could occur when setting filetype, target, |
| 173 | # or sandbox through --args. Filter and report an error. |
| 174 | if re.search('^-?-(o|output|filetype|target|sandbox)(=.+)?$', arg): |
| 175 | preferred_option = '--output' if re.search('^-?-o(=.+)?$', arg) else arg |
| 176 | print 'Option should be set using:' |
| 177 | print ' %s ... %s ... --args' % (script_name, preferred_option) |
| 178 | print 'rather than:' |
| 179 | print ' %s ... --args %s ...' % (script_name, arg) |
| 180 | exit(1) |
| 181 | asm_temp = None |
| 182 | output_file_name = None |
| 183 | keep_output_file = False |
| 184 | if args.output: |
| 185 | output_file_name = args.output |
| 186 | keep_output_file = True |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 187 | cmd += args.args |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 188 | if args.llvm_source: |
| 189 | cmd += [llfile] |
Jan Voung | a2703ae | 2015-02-19 11:27:44 -0800 | [diff] [blame] | 190 | if args.assemble or args.disassemble: |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 191 | if not output_file_name: |
| 192 | # On windows we may need to close the file first before it can be |
| 193 | # re-opened by the other tools, so don't do delete-on-close, |
| 194 | # and instead manually delete. |
| 195 | asm_temp = tempfile.NamedTemporaryFile(delete=False) |
| 196 | asm_temp.close() |
| 197 | output_file_name = asm_temp.name |
Jan Voung | b5447a0 | 2015-02-24 16:57:17 -0800 | [diff] [blame] | 198 | if args.assemble and args.filetype != 'obj': |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 199 | cmd += (['|', os.path.join(pnacl_bin_path, 'llvm-mc')] + |
John Porto | 52b5157 | 2015-12-05 14:16:25 -0800 | [diff] [blame] | 200 | TargetAssemblerFlags(args.target, args.sandbox) + |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 201 | ['-filetype=obj', '-o', output_file_name]) |
| 202 | elif output_file_name: |
| 203 | cmd += ['-o', output_file_name] |
Jan Voung | a2703ae | 2015-02-19 11:27:44 -0800 | [diff] [blame] | 204 | if args.disassemble: |
Srdjan Obucina | 8fbddc6 | 2016-09-15 17:26:40 -0700 | [diff] [blame^] | 205 | # Show wide instruction encodings, diassemble, show relocs and |
| 206 | # dissasemble zeros. |
Sean Klein | 8a4675c | 2016-03-09 10:29:54 -0800 | [diff] [blame] | 207 | cmd += (['&&', os.path.join(pnacl_bin_path, GetObjdumpCmd())] + |
Jan Voung | a2703ae | 2015-02-19 11:27:44 -0800 | [diff] [blame] | 208 | args.dis_flags + |
Srdjan Obucina | 8fbddc6 | 2016-09-15 17:26:40 -0700 | [diff] [blame^] | 209 | ['-w', '-d', '-r', '-z'] + TargetDisassemblerFlags(args.target) + |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 210 | [output_file_name]) |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 211 | |
| 212 | stdout_result = shellcmd(cmd, echo=args.echo_cmd) |
| 213 | if not args.echo_cmd: |
| 214 | sys.stdout.write(stdout_result) |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 215 | if asm_temp and not keep_output_file: |
| 216 | os.remove(output_file_name) |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 217 | |
| 218 | if __name__ == '__main__': |
| 219 | main() |