Chris Lattner | 1dd27b1 | 2003-10-20 17:58:43 +0000 | [diff] [blame] | 1 | //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This utility may be invoked in the following manner: |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 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 |
Misha Brukman | ad0bf0f | 2003-08-28 21:34:13 +0000 | [diff] [blame] | 13 | // to the x.ll file. |
Chris Lattner | 9bff2e9 | 2001-06-13 19:55:41 +0000 | [diff] [blame] | 14 | // Options: |
| 15 | // --help - Output information about command line switches |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | // |
Chris Lattner | 1d7b50b | 2001-10-13 07:06:57 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 19 | #include "llvm/LLVMContext.h" |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/AssemblyAnnotationWriter.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 21 | #include "llvm/Bitcode/ReaderWriter.h" |
| 22 | #include "llvm/DebugInfo.h" |
| 23 | #include "llvm/IntrinsicInst.h" |
| 24 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/DataStream.h" |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 30 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Signals.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 32 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 33 | #include "llvm/Support/system_error.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 34 | #include "llvm/Type.h" |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 37 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 38 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 40 | static cl::opt<std::string> |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 41 | OutputFilename("o", cl::desc("Override output filename"), |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 42 | cl::value_desc("filename")); |
| 43 | |
| 44 | static cl::opt<bool> |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 45 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 4d544cd | 2007-02-07 04:39:35 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
| 48 | DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); |
| 49 | |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 50 | static cl::opt<bool> |
| 51 | ShowAnnotations("show-annotations", |
| 52 | cl::desc("Add informational comments to the .ll file")); |
| 53 | |
| 54 | namespace { |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 55 | |
Devang Patel | 027f099 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 56 | static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) { |
| 57 | OS << DL.getLine() << ":" << DL.getCol(); |
| 58 | if (MDNode *N = DL.getInlinedAt(getGlobalContext())) { |
| 59 | DebugLoc IDL = DebugLoc::getFromDILocation(N); |
| 60 | if (!IDL.isUnknown()) { |
| 61 | OS << "@"; |
| 62 | printDebugLoc(IDL,OS); |
| 63 | } |
| 64 | } |
| 65 | } |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 66 | class CommentWriter : public AssemblyAnnotationWriter { |
| 67 | public: |
| 68 | void emitFunctionAnnot(const Function *F, |
| 69 | formatted_raw_ostream &OS) { |
| 70 | OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses |
| 71 | OS << '\n'; |
| 72 | } |
| 73 | void printInfoComment(const Value &V, formatted_raw_ostream &OS) { |
Devang Patel | 027f099 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 74 | bool Padded = false; |
| 75 | if (!V.getType()->isVoidTy()) { |
| 76 | OS.PadToColumn(50); |
| 77 | Padded = true; |
Chris Lattner | 0cd0d88 | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 78 | OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type |
Devang Patel | 027f099 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 79 | } |
| 80 | if (const Instruction *I = dyn_cast<Instruction>(&V)) { |
| 81 | const DebugLoc &DL = I->getDebugLoc(); |
| 82 | if (!DL.isUnknown()) { |
| 83 | if (!Padded) { |
| 84 | OS.PadToColumn(50); |
| 85 | Padded = true; |
| 86 | OS << ";"; |
| 87 | } |
| 88 | OS << " [debug line = "; |
| 89 | printDebugLoc(DL,OS); |
| 90 | OS << "]"; |
| 91 | } |
| 92 | if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { |
| 93 | DIVariable Var(DDI->getVariable()); |
| 94 | if (!Padded) { |
| 95 | OS.PadToColumn(50); |
Devang Patel | 027f099 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 96 | OS << ";"; |
| 97 | } |
| 98 | OS << " [debug variable = " << Var.getName() << "]"; |
| 99 | } |
| 100 | else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { |
| 101 | DIVariable Var(DVI->getVariable()); |
| 102 | if (!Padded) { |
| 103 | OS.PadToColumn(50); |
Devang Patel | 027f099 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 104 | OS << ";"; |
| 105 | } |
| 106 | OS << " [debug variable = " << Var.getName() << "]"; |
| 107 | } |
| 108 | } |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 109 | } |
| 110 | }; |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 112 | } // end anon namespace |
| 113 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 114 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 115 | // Print a stack trace if we signal out. |
| 116 | sys::PrintStackTraceOnErrorSignal(); |
| 117 | PrettyStackTraceProgram X(argc, argv); |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 118 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 119 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 120 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 121 | |
| 122 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 123 | cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); |
Chris Lattner | f73b4ca | 2004-02-19 20:32:12 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 125 | std::string ErrorMessage; |
| 126 | std::auto_ptr<Module> M; |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 127 | |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 128 | // Use the bitcode streaming interface |
| 129 | DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage); |
| 130 | if (streamer) { |
| 131 | std::string DisplayFilename; |
| 132 | if (InputFilename == "-") |
| 133 | DisplayFilename = "<stdin>"; |
Michael J. Spencer | c448aa6 | 2010-12-16 22:37:52 +0000 | [diff] [blame] | 134 | else |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 135 | DisplayFilename = InputFilename; |
| 136 | M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context, |
| 137 | &ErrorMessage)); |
| 138 | if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) { |
| 139 | M.reset(); |
| 140 | } |
Michael J. Spencer | c448aa6 | 2010-12-16 22:37:52 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 143 | if (M.get() == 0) { |
| 144 | errs() << argv[0] << ": "; |
| 145 | if (ErrorMessage.size()) |
| 146 | errs() << ErrorMessage << "\n"; |
| 147 | else |
| 148 | errs() << "bitcode didn't read correctly.\n"; |
| 149 | return 1; |
| 150 | } |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 152 | // Just use stdout. We won't actually print anything on it. |
| 153 | if (DontPrint) |
| 154 | OutputFilename = "-"; |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 156 | if (OutputFilename.empty()) { // Unspecified output, infer it. |
| 157 | if (InputFilename == "-") { |
| 158 | OutputFilename = "-"; |
| 159 | } else { |
| 160 | const std::string &IFN = InputFilename; |
| 161 | int Len = IFN.length(); |
| 162 | // If the source ends in .bc, strip it off. |
| 163 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') |
| 164 | OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll"; |
| 165 | else |
| 166 | OutputFilename = IFN+".ll"; |
| 167 | } |
| 168 | } |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 170 | std::string ErrorInfo; |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 171 | OwningPtr<tool_output_file> |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 172 | Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo, |
| 173 | raw_fd_ostream::F_Binary)); |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 174 | if (!ErrorInfo.empty()) { |
| 175 | errs() << ErrorInfo << '\n'; |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 179 | OwningPtr<AssemblyAnnotationWriter> Annotator; |
| 180 | if (ShowAnnotations) |
| 181 | Annotator.reset(new CommentWriter()); |
Michael J. Spencer | 7aa4844 | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 183 | // All that llvm-dis does is write the assembly to a file. |
Dan Gohman | 04fc8c8 | 2009-09-15 15:33:42 +0000 | [diff] [blame] | 184 | if (!DontPrint) |
Chris Lattner | 6cd71f0 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 185 | M->print(Out->os(), Annotator.get()); |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 186 | |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 187 | // Declare success. |
| 188 | Out->keep(); |
| 189 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 190 | return 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 191 | } |