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/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/System/Signals.h"
31 static cl::opt
<std::string
>
32 InputFilename(cl::Positional
, cl::desc("<input bitcode>"), cl::init("-"));
34 static cl::opt
<std::string
>
35 OutputFilename("o", cl::desc("Override output filename"),
36 cl::value_desc("filename"));
39 Force("f", cl::desc("Enable binary output on terminals"));
42 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden
);
44 int main(int argc
, char **argv
) {
45 // Print a stack trace if we signal out.
46 sys::PrintStackTraceOnErrorSignal();
47 PrettyStackTraceProgram
X(argc
, argv
);
49 LLVMContext
&Context
= getGlobalContext();
50 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
53 cl::ParseCommandLineOptions(argc
, argv
, "llvm .bc -> .ll disassembler\n");
55 std::string ErrorMessage
;
56 std::auto_ptr
<Module
> M
;
58 if (MemoryBuffer
*Buffer
59 = MemoryBuffer::getFileOrSTDIN(InputFilename
, &ErrorMessage
)) {
60 M
.reset(ParseBitcodeFile(Buffer
, Context
, &ErrorMessage
));
65 errs() << argv
[0] << ": ";
66 if (ErrorMessage
.size())
67 errs() << ErrorMessage
<< "\n";
69 errs() << "bitcode didn't read correctly.\n";
73 // Just use stdout. We won't actually print anything on it.
77 if (OutputFilename
.empty()) { // Unspecified output, infer it.
78 if (InputFilename
== "-") {
81 const std::string
&IFN
= InputFilename
;
82 int Len
= IFN
.length();
83 // If the source ends in .bc, strip it off.
84 if (IFN
[Len
-3] == '.' && IFN
[Len
-2] == 'b' && IFN
[Len
-1] == 'c')
85 OutputFilename
= std::string(IFN
.begin(), IFN
.end()-3)+".ll";
87 OutputFilename
= IFN
+".ll";
91 // Make sure that the Out file gets unlinked from the disk if we get a
93 if (OutputFilename
!= "-")
94 sys::RemoveFileOnSignal(sys::Path(OutputFilename
));
96 std::string ErrorInfo
;
97 std::auto_ptr
<raw_fd_ostream
>
98 Out(new raw_fd_ostream(OutputFilename
.c_str(), ErrorInfo
,
99 raw_fd_ostream::F_Binary
));
100 if (!ErrorInfo
.empty()) {
101 errs() << ErrorInfo
<< '\n';
105 // All that llvm-dis does is write the assembly to a file.