Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5f5a573 | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Owen Anderson | 25209b4 | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 19 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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" |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 27 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 3b475e9 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | #include "llvm/System/Signals.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | #include <memory> |
| 31 | using namespace llvm; |
| 32 | |
| 33 | static cl::opt<std::string> |
| 34 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
| 35 | |
| 36 | static cl::opt<std::string> |
| 37 | OutputFilename("o", cl::desc("Override output filename"), |
| 38 | cl::value_desc("filename")); |
| 39 | |
| 40 | static cl::opt<bool> |
| 41 | Force("f", cl::desc("Overwrite output files")); |
| 42 | |
| 43 | static cl::opt<bool> |
| 44 | DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); |
| 45 | |
| 46 | int main(int argc, char **argv) { |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 47 | // Print a stack trace if we signal out. |
| 48 | sys::PrintStackTraceOnErrorSignal(); |
| 49 | PrettyStackTraceProgram X(argc, argv); |
| 50 | |
Owen Anderson | e84b8b3 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 51 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 52 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 53 | try { |
Dan Gohman | 6099df8 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 54 | cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 56 | raw_ostream *Out = &outs(); // Default to printing to stdout. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | std::string ErrorMessage; |
| 58 | |
| 59 | std::auto_ptr<Module> M; |
| 60 | |
| 61 | if (MemoryBuffer *Buffer |
| 62 | = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) { |
Owen Anderson | a148fdd | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 63 | M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | delete Buffer; |
| 65 | } |
| 66 | |
| 67 | if (M.get() == 0) { |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 68 | errs() << argv[0] << ": "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | if (ErrorMessage.size()) |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 70 | errs() << ErrorMessage << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | else |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 72 | errs() << "bitcode didn't read correctly.\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | if (DontPrint) { |
| 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? |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 80 | std::string ErrorInfo; |
| 81 | Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false, |
| 82 | Force, ErrorInfo); |
| 83 | if (!ErrorInfo.empty()) { |
| 84 | errs() << ErrorInfo << '\n'; |
| 85 | if (!Force) |
| 86 | errs() << "Use -f command line argument to force output\n"; |
| 87 | delete Out; |
| 88 | return 1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } else { |
| 92 | if (InputFilename == "-") { |
| 93 | OutputFilename = "-"; |
| 94 | } else { |
| 95 | std::string IFN = InputFilename; |
| 96 | int Len = IFN.length(); |
| 97 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { |
| 98 | // Source ends in .bc |
| 99 | OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll"; |
| 100 | } else { |
| 101 | OutputFilename = IFN+".ll"; |
| 102 | } |
| 103 | |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 104 | std::string ErrorInfo; |
| 105 | Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false, |
| 106 | Force, ErrorInfo); |
| 107 | if (!ErrorInfo.empty()) { |
| 108 | errs() << ErrorInfo << '\n'; |
| 109 | if (!Force) |
| 110 | errs() << "Use -f command line argument to force output\n"; |
| 111 | delete Out; |
| 112 | return 1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 114 | |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 115 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 116 | // SIGINT |
| 117 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 118 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // All that llvm-dis does is write the assembly to a file. |
| 122 | if (!DontPrint) { |
| 123 | PassManager Passes; |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 124 | Passes.add(createPrintModulePass(Out)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 125 | Passes.run(*M.get()); |
| 126 | } |
| 127 | |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 128 | if (Out != &outs()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | delete Out; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | return 0; |
| 131 | } catch (const std::string& msg) { |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 132 | errs() << argv[0] << ": " << msg << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 133 | } catch (...) { |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 134 | errs() << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return 1; |
| 138 | } |
| 139 | |