1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 // --help - Output information about command line switches
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Assembly/PrintModulePass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Streams.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/System/Signals.h"
35 static cl::opt
<std::string
>
36 InputFilename(cl::Positional
, cl::desc("<input bitcode>"), cl::init("-"));
38 static cl::opt
<std::string
>
39 OutputFilename("o", cl::desc("Override output filename"),
40 cl::value_desc("filename"));
43 Force("f", cl::desc("Overwrite output files"));
46 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden
);
48 int main(int argc
, char **argv
) {
49 // Print a stack trace if we signal out.
50 sys::PrintStackTraceOnErrorSignal();
51 PrettyStackTraceProgram
X(argc
, argv
);
53 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
55 cl::ParseCommandLineOptions(argc
, argv
, "llvm .bc -> .ll disassembler\n");
57 std::ostream
*Out
= &std::cout
; // 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
, &ErrorMessage
));
69 cerr
<< argv
[0] << ": ";
70 if (ErrorMessage
.size())
71 cerr
<< ErrorMessage
<< "\n";
73 cerr
<< "bitcode didn't read correctly.\n";
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 if (!Force
&& std::ifstream(OutputFilename
.c_str())) {
82 // If force is not specified, make sure not to overwrite a file!
83 cerr
<< argv
[0] << ": error opening '" << OutputFilename
84 << "': file exists! Sending to standard output.\n";
86 Out
= new std::ofstream(OutputFilename
.c_str());
90 if (InputFilename
== "-") {
93 std::string IFN
= InputFilename
;
94 int Len
= IFN
.length();
95 if (IFN
[Len
-3] == '.' && IFN
[Len
-2] == 'b' && IFN
[Len
-1] == 'c') {
97 OutputFilename
= std::string(IFN
.begin(), IFN
.end()-3)+".ll";
99 OutputFilename
= IFN
+".ll";
102 if (!Force
&& std::ifstream(OutputFilename
.c_str())) {
103 // If force is not specified, make sure not to overwrite a file!
104 cerr
<< argv
[0] << ": error opening '" << OutputFilename
105 << "': file exists! Sending to standard output.\n";
107 Out
= new std::ofstream(OutputFilename
.c_str());
109 // Make sure that the Out file gets unlinked from the disk if we get a
111 sys::RemoveFileOnSignal(sys::Path(OutputFilename
));
117 cerr
<< argv
[0] << ": error opening " << OutputFilename
118 << ": sending to stdout instead!\n";
122 // All that llvm-dis does is write the assembly to a file.
125 raw_os_ostream
L(*Out
);
126 Passes
.add(createPrintModulePass(&L
));
127 Passes
.run(*M
.get());
130 if (Out
!= &std::cout
) {
131 ((std::ofstream
*)Out
)->close();
135 } catch (const std::string
& msg
) {
136 cerr
<< argv
[0] << ": " << msg
<< "\n";
138 cerr
<< argv
[0] << ": Unexpected unknown exception occurred.\n";