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/Streams.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/System/Signals.h"
36 static cl::opt<std::string>
37 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
39 static cl::opt<std::string>
40 OutputFilename("o", cl::desc("Override output filename"),
41 cl::value_desc("filename"));
44 Force("f", cl::desc("Overwrite output files"));
47 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
49 int main(int argc, char **argv) {
50 // Print a stack trace if we signal out.
51 sys::PrintStackTraceOnErrorSignal();
52 PrettyStackTraceProgram X(argc, argv);
55 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
57 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
59 std::ostream *Out = &std::cout; // Default to printing to stdout.
60 std::string ErrorMessage;
62 std::auto_ptr<Module> M;
64 if (MemoryBuffer *Buffer
65 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
66 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
71 cerr << argv[0] << ": ";
72 if (ErrorMessage.size())
73 cerr << ErrorMessage << "\n";
75 cerr << "bitcode didn't read correctly.\n";
80 // Just use stdout. We won't actually print anything on it.
81 } else if (OutputFilename != "") { // Specified an output filename?
82 if (OutputFilename != "-") { // Not stdout?
83 if (!Force && std::ifstream(OutputFilename.c_str())) {
84 // If force is not specified, make sure not to overwrite a file!
85 cerr << argv[0] << ": error opening '" << OutputFilename
86 << "': file exists! Sending to standard output.\n";
88 Out = new std::ofstream(OutputFilename.c_str());
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 if (!Force && std::ifstream(OutputFilename.c_str())) {
105 // If force is not specified, make sure not to overwrite a file!
106 cerr << argv[0] << ": error opening '" << OutputFilename
107 << "': file exists! Sending to standard output.\n";
109 Out = new std::ofstream(OutputFilename.c_str());
111 // Make sure that the Out file gets unlinked from the disk if we get a
113 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
119 cerr << argv[0] << ": error opening " << OutputFilename
120 << ": sending to stdout instead!\n";
124 // All that llvm-dis does is write the assembly to a file.
127 raw_os_ostream L(*Out);
128 Passes.add(createPrintModulePass(&L));
129 Passes.run(*M.get());
132 if (Out != &std::cout) {
133 ((std::ofstream*)Out)->close();
137 } catch (const std::string& msg) {
138 cerr << argv[0] << ": " << msg << "\n";
140 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";