add an option for timing bc file reading.
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This utility may be invoked in the following manner:
11 //  llvm-dis [options]      - Read LLVM bytecode from stdin, write asm to stdout
12 //  llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
13 //                            to the x.ll file.
14 //  Options:
15 //      --help   - Output information about command line switches
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bytecode/Reader.h"
22 #include "llvm/Assembly/PrintModulePass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Streams.h"
26 #include "llvm/System/Signals.h"
27 #include <iostream>
28 #include <fstream>
29 #include <memory>
30 using namespace llvm;
31
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
34
35 static cl::opt<std::string>
36 OutputFilename("o", cl::desc("Override output filename"),
37                cl::value_desc("filename"));
38
39 static cl::opt<bool>
40 Force("f", cl::desc("Overwrite output files"));
41
42 static cl::opt<bool>
43 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
44
45 int main(int argc, char **argv) {
46   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
47   try {
48     cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
49     sys::PrintStackTraceOnErrorSignal();
50
51     std::ostream *Out = &std::cout;  // Default to printing to stdout.
52     std::string ErrorMessage;
53
54     std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
55     if (M.get() == 0) {
56       cerr << argv[0] << ": ";
57       if (ErrorMessage.size())
58         cerr << ErrorMessage << "\n";
59       else
60         cerr << "bytecode didn't read correctly.\n";
61       return 1;
62     }
63
64     if (DontPrint) {
65       // Just use stdout.  We won't actually print anything on it.
66     } else if (OutputFilename != "") {   // Specified an output filename?
67       if (OutputFilename != "-") { // Not stdout?
68         if (!Force && std::ifstream(OutputFilename.c_str())) {
69           // If force is not specified, make sure not to overwrite a file!
70           cerr << argv[0] << ": error opening '" << OutputFilename
71                << "': file exists! Sending to standard output.\n";
72         } else {
73           Out = new std::ofstream(OutputFilename.c_str());
74         }
75       }
76     } else {
77       if (InputFilename == "-") {
78         OutputFilename = "-";
79       } else {
80         std::string IFN = InputFilename;
81         int Len = IFN.length();
82         if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
83           // Source ends in .bc
84           OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
85         } else {
86           OutputFilename = IFN+".ll";
87         }
88
89         if (!Force && std::ifstream(OutputFilename.c_str())) {
90           // If force is not specified, make sure not to overwrite a file!
91           cerr << argv[0] << ": error opening '" << OutputFilename
92                << "': file exists! Sending to standard output.\n";
93         } else {
94           Out = new std::ofstream(OutputFilename.c_str());
95
96           // Make sure that the Out file gets unlinked from the disk if we get a
97           // SIGINT
98           sys::RemoveFileOnSignal(sys::Path(OutputFilename));
99         }
100       }
101     }
102
103     if (!Out->good()) {
104       cerr << argv[0] << ": error opening " << OutputFilename
105            << ": sending to stdout instead!\n";
106       Out = &std::cout;
107     }
108
109     // All that llvm-dis does is write the assembly to a file.
110     if (!DontPrint) {
111       PassManager Passes;
112       OStream L(*Out);
113       Passes.add(new PrintModulePass(&L));
114       Passes.run(*M.get());
115     }
116
117     if (Out != &std::cout) {
118       ((std::ofstream*)Out)->close();
119       delete Out;
120     }
121     return 0;
122   } catch (const std::string& msg) {
123     cerr << argv[0] << ": " << msg << "\n";
124   } catch (...) {
125     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
126   }
127
128   return 1;
129 }
130