make all llvm tools call llvm_shutdown when they exit, static'ify some stuff.
[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 int main(int argc, char **argv) {
43   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
44   try {
45     cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
46     sys::PrintStackTraceOnErrorSignal();
47
48     std::ostream *Out = &std::cout;  // Default to printing to stdout.
49     std::string ErrorMessage;
50
51     std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
52     if (M.get() == 0) {
53       llvm_cerr << argv[0] << ": ";
54       if (ErrorMessage.size())
55         llvm_cerr << ErrorMessage << "\n";
56       else
57         llvm_cerr << "bytecode didn't read correctly.\n";
58       return 1;
59     }
60
61     if (OutputFilename != "") {   // Specified an output filename?
62       if (OutputFilename != "-") { // Not stdout?
63         if (!Force && std::ifstream(OutputFilename.c_str())) {
64           // If force is not specified, make sure not to overwrite a file!
65           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
66                     << "': file exists! Sending to standard output.\n";
67         } else {
68           Out = new std::ofstream(OutputFilename.c_str());
69         }
70       }
71     } else {
72       if (InputFilename == "-") {
73         OutputFilename = "-";
74       } else {
75         std::string IFN = InputFilename;
76         int Len = IFN.length();
77         if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
78           // Source ends in .bc
79           OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
80         } else {
81           OutputFilename = IFN+".ll";
82         }
83
84         if (!Force && std::ifstream(OutputFilename.c_str())) {
85           // If force is not specified, make sure not to overwrite a file!
86           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
87                     << "': file exists! Sending to standard output.\n";
88         } else {
89           Out = new std::ofstream(OutputFilename.c_str());
90
91           // Make sure that the Out file gets unlinked from the disk if we get a
92           // SIGINT
93           sys::RemoveFileOnSignal(sys::Path(OutputFilename));
94         }
95       }
96     }
97
98     if (!Out->good()) {
99       llvm_cerr << argv[0] << ": error opening " << OutputFilename
100                 << ": sending to stdout instead!\n";
101       Out = &std::cout;
102     }
103
104     // All that llvm-dis does is write the assembly to a file.
105     PassManager Passes;
106     llvm_ostream L(*Out);
107     Passes.add(new PrintModulePass(&L));
108     Passes.run(*M.get());
109
110     if (Out != &std::cout) {
111       ((std::ofstream*)Out)->close();
112       delete Out;
113     }
114     return 0;
115   } catch (const std::string& msg) {
116     llvm_cerr << argv[0] << ": " << msg << "\n";
117   } catch (...) {
118     llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
119   }
120
121   return 1;
122 }
123