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/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/raw_ostream.h"
29 #include "llvm/System/Signals.h"
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
36 static cl::opt<std::string>
37 OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
41 Force("f", cl::desc("Overwrite output files"));
44 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
46 int main(int argc, char **argv) {
47 // Print a stack trace if we signal out.
48 sys::PrintStackTraceOnErrorSignal();
49 PrettyStackTraceProgram X(argc, argv);
51 LLVMContext &Context = getGlobalContext();
52 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
54 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
56 raw_ostream *Out = &outs(); // Default to printing to stdout.
57 std::string ErrorMessage;
59 std::auto_ptr<Module> M;
61 if (MemoryBuffer *Buffer
62 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
63 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
68 errs() << argv[0] << ": ";
69 if (ErrorMessage.size())
70 errs() << ErrorMessage << "\n";
72 errs() << "bitcode didn't read correctly.\n";
77 // Just use stdout. We won't actually print anything on it.
78 } else if (OutputFilename != "") { // Specified an output filename?
79 if (OutputFilename != "-") { // Not stdout?
80 std::string ErrorInfo;
81 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
83 if (!ErrorInfo.empty()) {
84 errs() << ErrorInfo << '\n';
86 errs() << "Use -f command line argument to force output\n";
92 if (InputFilename == "-") {
95 std::string IFN = InputFilename;
96 int Len = IFN.length();
97 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
99 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
101 OutputFilename = IFN+".ll";
104 std::string ErrorInfo;
105 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
107 if (!ErrorInfo.empty()) {
108 errs() << ErrorInfo << '\n';
110 errs() << "Use -f command line argument to force output\n";
115 // Make sure that the Out file gets unlinked from the disk if we get a
117 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
121 // All that llvm-dis does is write the assembly to a file.
124 Passes.add(createPrintModulePass(Out));
125 Passes.run(*M.get());
131 } catch (const std::string& msg) {
132 errs() << argv[0] << ": " << msg << "\n";
134 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";