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 # Redirecting the output file needs to be done through the script because | |
Jim Stichnoth
2016/01/13 00:36:44
This comment should probably be moved closer to th
sehr
2016/01/13 18:15:35
Done.
| |
159 # forceasm may introduce a new temporary file between pnacl-sz and llvm-mc. | |
160 for i, j in enumerate(args.args): | |
Jim Stichnoth
2016/01/13 00:36:44
You're not using "i", right? I think the typical
sehr
2016/01/13 18:15:35
Done.
| |
161 if re.search('^-?-o(=.+)?', j): | |
Jim Stichnoth
2016/01/13 00:36:44
I think I would just use a single big regex here,
sehr
2016/01/13 18:15:35
Done.
| |
162 scriptname = os.path.basename(sys.argv[0]) | |
Jim Stichnoth
2016/01/13 00:36:44
Hoist this out of the loop, and remove duplication
sehr
2016/01/13 18:15:35
Done.
| |
163 print 'Output file should be set using:' | |
164 print ' %s ... --output <outfile> ... --args' % scriptname | |
165 print 'rather than:' | |
166 print ' %s ... --args -o <outfile>' % scriptname | |
167 exit(1) | |
168 elif re.search('^--filetype(=.+)?', j): | |
169 scriptname = os.path.basename(sys.argv[0]) | |
170 print 'Output file type should be set using:' | |
171 print ' %s ... --filetype=<type> ... --args' % scriptname | |
172 print 'rather than:' | |
173 print ' %s ... --args --filetype=<type>' % scriptname | |
174 exit(1) | |
175 elif re.search('^--target(=.+)?', j): | |
Jim Stichnoth
2016/01/13 00:36:44
Add a similar check for --sandbox
sehr
2016/01/13 18:15:35
Done.
| |
176 scriptname = os.path.basename(sys.argv[0]) | |
177 print 'Target architecture should be set using:' | |
178 print ' %s ... --target=<target> ... --args' % scriptname | |
179 print 'rather than:' | |
180 print ' %s ... --args --target=<target>' % scriptname | |
181 exit(1) | |
182 asm_temp = None | |
183 output_file_name = None | |
184 keep_output_file = False | |
185 if args.output: | |
186 output_file_name = args.output | |
187 keep_output_file = True | |
144 cmd += args.args | 188 cmd += args.args |
145 if args.llvm_source: | 189 if args.llvm_source: |
146 cmd += [llfile] | 190 cmd += [llfile] |
147 asm_temp = None | |
148 if args.assemble or args.disassemble: | 191 if args.assemble or args.disassemble: |
149 # On windows we may need to close the file first before it can be | 192 if not output_file_name: |
150 # re-opened by the other tools, so don't do delete-on-close, | 193 # On windows we may need to close the file first before it can be |
151 # and instead manually delete. | 194 # re-opened by the other tools, so don't do delete-on-close, |
152 asm_temp = tempfile.NamedTemporaryFile(delete=False) | 195 # and instead manually delete. |
153 asm_temp.close() | 196 asm_temp = tempfile.NamedTemporaryFile(delete=False) |
197 asm_temp.close() | |
198 output_file_name = asm_temp.name | |
154 if args.assemble and args.filetype != 'obj': | 199 if args.assemble and args.filetype != 'obj': |
155 cmd += (['|', os.path.join(pnacl_bin_path, 'llvm-mc')] + | 200 cmd += (['|', os.path.join(pnacl_bin_path, 'llvm-mc')] + |
156 TargetAssemblerFlags(args.target, args.sandbox) + | 201 TargetAssemblerFlags(args.target, args.sandbox) + |
157 ['-filetype=obj', '-o', asm_temp.name]) | 202 ['-filetype=obj', '-o', output_file_name]) |
158 elif asm_temp: | 203 elif output_file_name: |
159 cmd += ['-o', asm_temp.name] | 204 cmd += ['-o', output_file_name] |
160 if args.disassemble: | 205 if args.disassemble: |
161 # Show wide instruction encodings, diassemble, and show relocs. | 206 # Show wide instruction encodings, diassemble, and show relocs. |
162 cmd += (['&&', os.path.join(pnacl_bin_path, 'le32-nacl-objdump')] + | 207 cmd += (['&&', os.path.join(pnacl_bin_path, 'le32-nacl-objdump')] + |
163 args.dis_flags + | 208 args.dis_flags + |
164 ['-w', '-d', '-r'] + TargetDisassemblerFlags(args.target) + | 209 ['-w', '-d', '-r'] + TargetDisassemblerFlags(args.target) + |
165 [asm_temp.name]) | 210 [output_file_name]) |
166 | 211 |
167 stdout_result = shellcmd(cmd, echo=args.echo_cmd) | 212 stdout_result = shellcmd(cmd, echo=args.echo_cmd) |
168 if not args.echo_cmd: | 213 if not args.echo_cmd: |
169 sys.stdout.write(stdout_result) | 214 sys.stdout.write(stdout_result) |
170 if asm_temp: | 215 if asm_temp and not keep_output_file: |
171 os.remove(asm_temp.name) | 216 os.remove(output_file_name) |
172 | 217 |
173 if __name__ == '__main__': | 218 if __name__ == '__main__': |
174 main() | 219 main() |
OLD | NEW |