blob: 29ddd145fa7d526d0c2fc4ef6de53474a0c7a1e9 [file] [log] [blame]
Karl Schimpfa667fb82014-05-19 14:56:51 -07001#!/usr/bin/env python2
2
3import argparse
4import itertools
5import os
6import re
7import subprocess
8import sys
9
Jan Voung1248a6d2014-06-06 08:47:54 -070010sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pydir'))
Karl Schimpfa667fb82014-05-19 14:56:51 -070011
12from utils import shellcmd
13
14if __name__ == '__main__':
15 desc = 'Run llvm2ice on llvm file to produce ICE instructions.'
16 argparser = argparse.ArgumentParser(
17 description=desc,
18 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
19 epilog='''
20 Runs in two modes, depending on whether the flag '--pnacl' is specified.
21
22 If flag '--pnacl' is omitted, it runs llvm2ice to (directly) generate
23 the corresponding ICE instructions.
24
25 If flag '--pnacl' is given, it first assembles and freezes the
26 llvm source file generating the corresponding PNaCl bitcode
27 file. The PNaCl bitcode file is then piped into llvm2ice to
28 generate the corresponding ICE instructions.
29 ''')
30 argparser.add_argument(
31 '--llvm2ice', required=False, default='./llvm2ice', metavar='LLVM2ICE',
32 help='Path to llvm2ice driver program')
33 argparser.add_argument('--llvm-bin-path', required=False,
34 default=None, metavar='LLVM_BIN_PATH',
35 help='Path to LLVM executables ' +
36 '(for building PNaCl files)')
37 argparser.add_argument('--pnacl', required=False,
38 action='store_true',
39 help='Convert llvm source to PNaCl bitcode ' +
40 'file first')
41 argparser.add_argument('--echo-cmd', required=False,
42 action='store_true',
43 help='Trace command that generates ICE instructions')
44 argparser.add_argument('llfile', nargs=1,
45 metavar='LLVM_FILE',
46 help='Llvm source file')
47
48 args = argparser.parse_args()
49 llvm_bin_path = args.llvm_bin_path
50 llfile = args.llfile[0]
51
52 cmd = []
53 if args.pnacl:
54 cmd = [os.path.join(llvm_bin_path, 'llvm-as'), llfile, '-o', '-', '|',
55 os.path.join(llvm_bin_path, 'pnacl-freeze'),
56 '--allow-local-symbol-tables', '|']
57 cmd += [args.llvm2ice, '-verbose', 'inst', '-notranslate']
58 if args.pnacl:
59 cmd += ['--allow-local-symbol-tables', '--bitcode-format=pnacl']
60 else:
61 cmd.append(llfile)
62
63 stdout_result = shellcmd(cmd, echo=args.echo_cmd)
64 if not args.echo_cmd:
65 sys.stdout.write(stdout_result)