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 |
(...skipping 22 matching lines...) Expand all Loading... | |
33 """Run the pnacl-sz compiler on an llvm file. | 33 """Run the pnacl-sz compiler on an llvm file. |
34 | 34 |
35 Takes an llvm input file, freezes it into a pexe file, converts | 35 Takes an llvm input file, freezes it into a pexe file, converts |
36 it to a Subzero program, and finally compiles it. | 36 it to a Subzero program, and finally compiles it. |
37 """ | 37 """ |
38 argparser = argparse.ArgumentParser( | 38 argparser = argparse.ArgumentParser( |
39 description=' ' + main.__doc__, | 39 description=' ' + main.__doc__, |
40 formatter_class=argparse.ArgumentDefaultsHelpFormatter) | 40 formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
41 argparser.add_argument('--input', '-i', required=True, | 41 argparser.add_argument('--input', '-i', required=True, |
42 help='LLVM source file to compile') | 42 help='LLVM source file to compile') |
43 argparser.add_argument('--output', '-o', required=False, | |
44 help='Output file to write') | |
43 argparser.add_argument('--insts', required=False, | 45 argparser.add_argument('--insts', required=False, |
44 action='store_true', | 46 action='store_true', |
45 help='Stop after translating to ' + | 47 help='Stop after translating to ' + |
46 'Subzero instructions') | 48 'Subzero instructions') |
47 argparser.add_argument('--no-local-syms', required=False, | 49 argparser.add_argument('--no-local-syms', required=False, |
48 action='store_true', | 50 action='store_true', |
49 help="Don't keep local symbols in the pexe file") | 51 help="Don't keep local symbols in the pexe file") |
50 argparser.add_argument('--llvm', required=False, | 52 argparser.add_argument('--llvm', required=False, |
51 action='store_true', | 53 action='store_true', |
52 help='Parse pexe into llvm IR first, then ' + | 54 help='Parse pexe into llvm IR first, then ' + |
(...skipping 17 matching lines...) Expand all Loading... | |
70 action='store_true', | 72 action='store_true', |
71 help='Assemble the output') | 73 help='Assemble the output') |
72 argparser.add_argument('--disassemble', required=False, | 74 argparser.add_argument('--disassemble', required=False, |
73 action='store_true', | 75 action='store_true', |
74 help='Disassemble the assembled output') | 76 help='Disassemble the assembled output') |
75 argparser.add_argument('--dis-flags', required=False, | 77 argparser.add_argument('--dis-flags', required=False, |
76 action='append', default=[], | 78 action='append', default=[], |
77 help='Add a disassembler flag') | 79 help='Add a disassembler flag') |
78 argparser.add_argument('--filetype', default='iasm', dest='filetype', | 80 argparser.add_argument('--filetype', default='iasm', dest='filetype', |
79 choices=['obj', 'asm', 'iasm'], | 81 choices=['obj', 'asm', 'iasm'], |
80 help='Output file type. Default %(default)s.') | 82 help='Output file type. Default %(default)s') |
83 argparser.add_argument('--forceasm', required=False, action='store_true', | |
84 help='Force --filetype=asm') | |
81 argparser.add_argument('--target', default='x8632', dest='target', | 85 argparser.add_argument('--target', default='x8632', dest='target', |
82 choices=['x8632','arm32','mips32'], | 86 choices=['x8632','arm32','mips32'], |
83 help='Target architecture. Default %(default)s.') | 87 help='Target architecture. Default %(default)s') |
84 argparser.add_argument('--echo-cmd', required=False, | 88 argparser.add_argument('--echo-cmd', required=False, |
85 action='store_true', | 89 action='store_true', |
86 help='Trace command that generates ICE instructions') | 90 help='Trace command that generates ICE instructions') |
87 argparser.add_argument('--tbc', required=False, action='store_true', | 91 argparser.add_argument('--tbc', required=False, action='store_true', |
88 help='Input is textual bitcode (not .ll)') | 92 help='Input is textual bitcode (not .ll)') |
89 argparser.add_argument('--expect-fail', required=False, action='store_true', | 93 argparser.add_argument('--expect-fail', required=False, action='store_true', |
90 help='Negate success of run by using LLVM not') | 94 help='Negate success of run by using LLVM not') |
91 argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, | 95 argparser.add_argument('--args', '-a', nargs=argparse.REMAINDER, |
92 default=[], | 96 default=[], |
93 help='Remaining arguments are passed to pnacl-sz') | 97 help='Remaining arguments are passed to pnacl-sz') |
94 argparser.add_argument('--sandbox', required=False, action='store_true', | 98 argparser.add_argument('--sandbox', required=False, action='store_true', |
95 help='Sandboxes the generated code.') | 99 help='Sandboxes the generated code') |
96 | 100 |
97 args = argparser.parse_args() | 101 args = argparser.parse_args() |
98 pnacl_bin_path = args.pnacl_bin_path | 102 pnacl_bin_path = args.pnacl_bin_path |
99 llfile = args.input | 103 llfile = args.input |
100 | 104 |
101 if args.llvm and args.llvm_source: | 105 if args.llvm and args.llvm_source: |
102 raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") | 106 raise RuntimeError("Can't specify both '--llvm' and '--llvm-source'") |
103 | 107 |
104 if args.llvm_source and args.no_local_syms: | 108 if args.llvm_source and args.no_local_syms: |
105 raise RuntimeError("Can't specify both '--llvm-source' and " + | 109 raise RuntimeError("Can't specify both '--llvm-source' and " + |
106 "'--no-local-syms'") | 110 "'--no-local-syms'") |
107 | 111 |
108 if args.llvm_source and args.tbc: | 112 if args.llvm_source and args.tbc: |
109 raise RuntimeError("Can't specify both '--tbc' and '--llvm-source'") | 113 raise RuntimeError("Can't specify both '--tbc' and '--llvm-source'") |
110 | 114 |
111 if args.llvm and args.tbc: | 115 if args.llvm and args.tbc: |
112 raise RuntimeError("Can't specify both '--tbc' and '--llvm'") | 116 raise RuntimeError("Can't specify both '--tbc' and '--llvm'") |
113 | 117 |
118 if args.forceasm: | |
119 if args.filetype == 'asm': | |
120 pass | |
121 elif args.filetype == 'iasm': | |
122 # TODO(sehr) implement forceasm for iasm. | |
123 pass | |
124 elif args.filetype == 'obj': | |
125 args.filetype = 'asm' | |
126 args.assemble = True | |
127 | |
114 cmd = [] | 128 cmd = [] |
115 if args.tbc: | 129 if args.tbc: |
116 cmd = [os.path.join(pnacl_bin_path, 'pnacl-bcfuzz'), llfile, | 130 cmd = [os.path.join(pnacl_bin_path, 'pnacl-bcfuzz'), llfile, |
117 '-bitcode-as-text', '-output', '-', '|'] | 131 '-bitcode-as-text', '-output', '-', '|'] |
118 elif not args.llvm_source: | 132 elif not args.llvm_source: |
119 cmd = [os.path.join(pnacl_bin_path, 'llvm-as'), llfile, '-o', '-', '|', | 133 cmd = [os.path.join(pnacl_bin_path, 'llvm-as'), llfile, '-o', '-', '|', |
120 os.path.join(pnacl_bin_path, 'pnacl-freeze')] | 134 os.path.join(pnacl_bin_path, 'pnacl-freeze')] |
121 if not args.no_local_syms: | 135 if not args.no_local_syms: |
122 cmd += ['--allow-local-symbol-tables'] | 136 cmd += ['--allow-local-symbol-tables'] |
123 cmd += ['|'] | 137 cmd += ['|'] |
(...skipping 10 matching lines...) Expand all Loading... | |
134 cmd += ['-verbose', 'inst', '-notranslate', '-threads=0'] | 148 cmd += ['-verbose', 'inst', '-notranslate', '-threads=0'] |
135 if not args.llvm_source: | 149 if not args.llvm_source: |
136 cmd += ['--bitcode-format=pnacl'] | 150 cmd += ['--bitcode-format=pnacl'] |
137 if not args.no_local_syms: | 151 if not args.no_local_syms: |
138 cmd += ['--allow-local-symbol-tables'] | 152 cmd += ['--allow-local-symbol-tables'] |
139 if args.llvm or args.llvm_source: | 153 if args.llvm or args.llvm_source: |
140 cmd += ['--build-on-read=0'] | 154 cmd += ['--build-on-read=0'] |
141 else: | 155 else: |
142 cmd += ['--build-on-read=1'] | 156 cmd += ['--build-on-read=1'] |
143 cmd += ['--filetype=' + args.filetype] | 157 cmd += ['--filetype=' + args.filetype] |
158 script_name = os.path.basename(sys.argv[0]) | |
159 for _, j in enumerate(args.args): | |
Jim Stichnoth
2016/01/13 22:00:44
'j' looks kind of odd as a variable name here. Ho
sehr
2016/01/13 22:19:39
Done.
| |
160 # Redirecting the output file needs to be done through the script | |
161 # because forceasm may introduce a new temporary file between pnacl-sz | |
162 # and llvm-mc. Similar issues could occur when setting filetype, target, | |
163 # or sandbox through --args. Filter and report an error. | |
164 if re.search('^-?-(o|filetype|target|sandbox)(=.+)?$', j): | |
Jim Stichnoth
2016/01/13 22:00:44
add 'output' as well because pnacl-sz -o and --out
sehr
2016/01/13 22:19:39
Done.
| |
165 if re.search('^-?-o', j): | |
166 preferred_option = '--output' | |
167 else: | |
168 preferred_option = j | |
Jim Stichnoth
2016/01/13 22:00:44
preferred_option = '--output' if re.search('^-?-o'
sehr
2016/01/13 22:19:39
Done. Thanks, I should have thought of that...
| |
169 print 'Option should be set using:' | |
170 print ' %s ... %s ... --args' % (script_name, preferred_option) | |
171 print 'rather than:' | |
172 print ' %s ... --args %s ...' % (script_name, j) | |
173 exit(1) | |
174 asm_temp = None | |
175 output_file_name = None | |
176 keep_output_file = False | |
177 if args.output: | |
178 output_file_name = args.output | |
179 keep_output_file = True | |
144 cmd += args.args | 180 cmd += args.args |
145 if args.llvm_source: | 181 if args.llvm_source: |
146 cmd += [llfile] | 182 cmd += [llfile] |
147 asm_temp = None | |
148 if args.assemble or args.disassemble: | 183 if args.assemble or args.disassemble: |
149 # On windows we may need to close the file first before it can be | 184 if not output_file_name: |
150 # re-opened by the other tools, so don't do delete-on-close, | 185 # On windows we may need to close the file first before it can be |
151 # and instead manually delete. | 186 # re-opened by the other tools, so don't do delete-on-close, |
152 asm_temp = tempfile.NamedTemporaryFile(delete=False) | 187 # and instead manually delete. |
153 asm_temp.close() | 188 asm_temp = tempfile.NamedTemporaryFile(delete=False) |
189 asm_temp.close() | |
190 output_file_name = asm_temp.name | |
154 if args.assemble and args.filetype != 'obj': | 191 if args.assemble and args.filetype != 'obj': |
155 cmd += (['|', os.path.join(pnacl_bin_path, 'llvm-mc')] + | 192 cmd += (['|', os.path.join(pnacl_bin_path, 'llvm-mc')] + |
156 TargetAssemblerFlags(args.target, args.sandbox) + | 193 TargetAssemblerFlags(args.target, args.sandbox) + |
157 ['-filetype=obj', '-o', asm_temp.name]) | 194 ['-filetype=obj', '-o', output_file_name]) |
158 elif asm_temp: | 195 elif output_file_name: |
159 cmd += ['-o', asm_temp.name] | 196 cmd += ['-o', output_file_name] |
160 if args.disassemble: | 197 if args.disassemble: |
161 # Show wide instruction encodings, diassemble, and show relocs. | 198 # Show wide instruction encodings, diassemble, and show relocs. |
162 cmd += (['&&', os.path.join(pnacl_bin_path, 'le32-nacl-objdump')] + | 199 cmd += (['&&', os.path.join(pnacl_bin_path, 'le32-nacl-objdump')] + |
163 args.dis_flags + | 200 args.dis_flags + |
164 ['-w', '-d', '-r'] + TargetDisassemblerFlags(args.target) + | 201 ['-w', '-d', '-r'] + TargetDisassemblerFlags(args.target) + |
165 [asm_temp.name]) | 202 [output_file_name]) |
166 | 203 |
167 stdout_result = shellcmd(cmd, echo=args.echo_cmd) | 204 stdout_result = shellcmd(cmd, echo=args.echo_cmd) |
168 if not args.echo_cmd: | 205 if not args.echo_cmd: |
169 sys.stdout.write(stdout_result) | 206 sys.stdout.write(stdout_result) |
170 if asm_temp: | 207 if asm_temp and not keep_output_file: |
171 os.remove(asm_temp.name) | 208 os.remove(output_file_name) |
172 | 209 |
173 if __name__ == '__main__': | 210 if __name__ == '__main__': |
174 main() | 211 main() |
OLD | NEW |