Merge branch 'master' into systemz
[llvm/systemz.git] / tools / llvm-dis / llvm-dis.cpp
blobe93d6d4e0992e187ae570ad0e6b2a66abc05151d
1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 bitcode from stdin, write asm to stdout
12 // llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
13 // to the x.ll file.
14 // Options:
15 // --help - Output information about command line switches
17 //===----------------------------------------------------------------------===//
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Bitcode/ReaderWriter.h"
23 #include "llvm/Assembly/PrintModulePass.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Streams.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/System/Signals.h"
31 #include <memory>
32 using namespace llvm;
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
41 static cl::opt<bool>
42 Force("f", cl::desc("Overwrite output files"));
44 static cl::opt<bool>
45 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
47 int main(int argc, char **argv) {
48 // Print a stack trace if we signal out.
49 sys::PrintStackTraceOnErrorSignal();
50 PrettyStackTraceProgram X(argc, argv);
52 LLVMContext &Context = getGlobalContext();
53 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
54 try {
55 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
57 raw_ostream *Out = &outs(); // Default to printing to stdout.
58 std::string ErrorMessage;
60 std::auto_ptr<Module> M;
62 if (MemoryBuffer *Buffer
63 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
64 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
65 delete Buffer;
68 if (M.get() == 0) {
69 cerr << argv[0] << ": ";
70 if (ErrorMessage.size())
71 cerr << ErrorMessage << "\n";
72 else
73 cerr << "bitcode didn't read correctly.\n";
74 return 1;
77 if (DontPrint) {
78 // Just use stdout. We won't actually print anything on it.
79 } else if (OutputFilename != "") { // Specified an output filename?
80 if (OutputFilename != "-") { // Not stdout?
81 std::string ErrorInfo;
82 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
83 Force, ErrorInfo);
84 if (!ErrorInfo.empty()) {
85 errs() << ErrorInfo << '\n';
86 if (!Force)
87 errs() << "Use -f command line argument to force output\n";
88 delete Out;
89 return 1;
92 } else {
93 if (InputFilename == "-") {
94 OutputFilename = "-";
95 } else {
96 std::string IFN = InputFilename;
97 int Len = IFN.length();
98 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
99 // Source ends in .bc
100 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
101 } else {
102 OutputFilename = IFN+".ll";
105 std::string ErrorInfo;
106 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
107 Force, ErrorInfo);
108 if (!ErrorInfo.empty()) {
109 errs() << ErrorInfo << '\n';
110 if (!Force)
111 errs() << "Use -f command line argument to force output\n";
112 delete Out;
113 return 1;
116 // Make sure that the Out file gets unlinked from the disk if we get a
117 // SIGINT
118 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
122 // All that llvm-dis does is write the assembly to a file.
123 if (!DontPrint) {
124 PassManager Passes;
125 Passes.add(createPrintModulePass(Out));
126 Passes.run(*M.get());
129 if (Out != &outs())
130 delete Out;
131 return 0;
132 } catch (const std::string& msg) {
133 cerr << argv[0] << ": " << msg << "\n";
134 } catch (...) {
135 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
138 return 1;