blob: 6450ea6ac74b9ffe0743791bfd1bd2a6110f0379 [file] [log] [blame]
Chris Lattner8f71f042003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Gabor Greife16561c2007-07-05 17:07:56 +000011// 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 Brukmanf12549d2003-08-28 21:34:13 +000013// to the x.ll file.
Chris Lattnerf284ac52001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattner2f7c9632001-06-06 20:29:01 +000016//
Chris Lattner6d56c6b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000018
Owen Anderson6773d382009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/Module.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000021#include "llvm/Type.h"
Devang Patel982efb52011-03-11 18:07:33 +000022#include "llvm/IntrinsicInst.h"
Chris Lattnere2244462007-04-22 06:31:02 +000023#include "llvm/Bitcode/ReaderWriter.h"
Devang Patel982efb52011-03-11 18:07:33 +000024#include "llvm/Analysis/DebugInfo.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000025#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Derek Schuff8b2dcad2012-02-06 22:30:29 +000027#include "llvm/Support/DataStream.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000028#include "llvm/Support/FormattedStream.h"
Chris Lattner76d46322006-12-06 01:18:01 +000029#include "llvm/Support/ManagedStatic.h"
Chris Lattner6694f602007-04-29 07:54:31 +000030#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000031#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000032#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000033#include "llvm/Support/Signals.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000034#include "llvm/Support/system_error.h"
Brian Gaeke960707c2003-11-11 22:41:34 +000035using namespace llvm;
36
Chris Lattner64a67272002-07-25 16:31:09 +000037static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000038InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000039
Chris Lattner64a67272002-07-25 16:31:09 +000040static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000041OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000042 cl::value_desc("filename"));
43
44static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000045Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000046
Chris Lattner216835d2007-02-07 04:39:35 +000047static cl::opt<bool>
48DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
49
Chris Lattner7f2f0932010-09-02 23:21:44 +000050static cl::opt<bool>
51ShowAnnotations("show-annotations",
52 cl::desc("Add informational comments to the .ll file"));
53
54namespace {
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +000055
Devang Patel982efb52011-03-11 18:07:33 +000056static 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 Lattner7f2f0932010-09-02 23:21:44 +000066class CommentWriter : public AssemblyAnnotationWriter {
67public:
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 Patel982efb52011-03-11 18:07:33 +000074 bool Padded = false;
75 if (!V.getType()->isVoidTy()) {
76 OS.PadToColumn(50);
77 Padded = true;
Chris Lattner0f214eb2011-06-18 21:18:23 +000078 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type
Devang Patel982efb52011-03-11 18:07:33 +000079 }
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);
96 Padded = true;
97 OS << ";";
98 }
99 OS << " [debug variable = " << Var.getName() << "]";
100 }
101 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
102 DIVariable Var(DVI->getVariable());
103 if (!Padded) {
104 OS.PadToColumn(50);
105 Padded = true;
106 OS << ";";
107 }
108 OS << " [debug variable = " << Var.getName() << "]";
109 }
110 }
Chris Lattner7f2f0932010-09-02 23:21:44 +0000111 }
112};
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000113
Chris Lattner7f2f0932010-09-02 23:21:44 +0000114} // end anon namespace
115
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000117 // Print a stack trace if we signal out.
118 sys::PrintStackTraceOnErrorSignal();
119 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000120
Owen Anderson19251ec2009-07-15 22:16:10 +0000121 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000122 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000123
124
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000125 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattner12439ff2004-02-19 20:32:12 +0000126
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000127 std::string ErrorMessage;
128 std::auto_ptr<Module> M;
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000129
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000130 // Use the bitcode streaming interface
131 DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
132 if (streamer) {
133 std::string DisplayFilename;
134 if (InputFilename == "-")
135 DisplayFilename = "<stdin>";
Michael J. Spencera646f392010-12-16 22:37:52 +0000136 else
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000137 DisplayFilename = InputFilename;
138 M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context,
139 &ErrorMessage));
140 if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) {
141 M.reset();
142 }
Michael J. Spencera646f392010-12-16 22:37:52 +0000143 }
Chris Lattner76d46322006-12-06 01:18:01 +0000144
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000145 if (M.get() == 0) {
146 errs() << argv[0] << ": ";
147 if (ErrorMessage.size())
148 errs() << ErrorMessage << "\n";
149 else
150 errs() << "bitcode didn't read correctly.\n";
151 return 1;
152 }
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000153
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000154 // Just use stdout. We won't actually print anything on it.
155 if (DontPrint)
156 OutputFilename = "-";
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000157
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000158 if (OutputFilename.empty()) { // Unspecified output, infer it.
159 if (InputFilename == "-") {
160 OutputFilename = "-";
161 } else {
162 const std::string &IFN = InputFilename;
163 int Len = IFN.length();
164 // If the source ends in .bc, strip it off.
165 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
166 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
167 else
168 OutputFilename = IFN+".ll";
169 }
170 }
Dan Gohmane5929232009-09-11 20:46:33 +0000171
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000172 std::string ErrorInfo;
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000173 OwningPtr<tool_output_file>
Dan Gohman268b0f42010-08-20 01:07:01 +0000174 Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
175 raw_fd_ostream::F_Binary));
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000176 if (!ErrorInfo.empty()) {
177 errs() << ErrorInfo << '\n';
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000178 return 1;
179 }
180
Chris Lattner7f2f0932010-09-02 23:21:44 +0000181 OwningPtr<AssemblyAnnotationWriter> Annotator;
182 if (ShowAnnotations)
183 Annotator.reset(new CommentWriter());
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000184
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000185 // All that llvm-dis does is write the assembly to a file.
Dan Gohman972c9c52009-09-15 15:33:42 +0000186 if (!DontPrint)
Chris Lattner7f2f0932010-09-02 23:21:44 +0000187 M->print(Out->os(), Annotator.get());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000188
Dan Gohman268b0f42010-08-20 01:07:01 +0000189 // Declare success.
190 Out->keep();
191
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000192 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193}