blob: 9020a527866c35590fe8186f3e2607de803cb1c5 [file] [log] [blame]
Chris Lattner1dd27b12003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Gabor Greifa99be512007-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 Brukmanad0bf0f2003-08-28 21:34:13 +000013// to the x.ll file.
Chris Lattner9bff2e92001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattner00950542001-06-06 20:29:01 +000016//
Chris Lattner1d7b50b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000018
Owen Anderson8b477ed2009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Module.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000021#include "llvm/Type.h"
Devang Patel027f0992011-03-11 18:07:33 +000022#include "llvm/IntrinsicInst.h"
Chris Lattnerb3ca3622007-04-22 06:31:02 +000023#include "llvm/Bitcode/ReaderWriter.h"
Devang Patel027f0992011-03-11 18:07:33 +000024#include "llvm/Analysis/DebugInfo.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000025#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000027#include "llvm/Support/FormattedStream.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000028#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000029#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000030#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000031#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000032#include "llvm/Support/Signals.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000033#include "llvm/Support/system_error.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000034using namespace llvm;
35
Chris Lattnerc7a09852002-07-25 16:31:09 +000036static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000037InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000038
Chris Lattnerc7a09852002-07-25 16:31:09 +000039static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000040OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000041 cl::value_desc("filename"));
42
43static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000044Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000045
Chris Lattner4d544cd2007-02-07 04:39:35 +000046static cl::opt<bool>
47DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
48
Chris Lattner6cd71f02010-09-02 23:21:44 +000049static cl::opt<bool>
50ShowAnnotations("show-annotations",
51 cl::desc("Add informational comments to the .ll file"));
52
53namespace {
Michael J. Spencer7aa48442010-12-16 16:23:30 +000054
Devang Patel027f0992011-03-11 18:07:33 +000055static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
56 OS << DL.getLine() << ":" << DL.getCol();
57 if (MDNode *N = DL.getInlinedAt(getGlobalContext())) {
58 DebugLoc IDL = DebugLoc::getFromDILocation(N);
59 if (!IDL.isUnknown()) {
60 OS << "@";
61 printDebugLoc(IDL,OS);
62 }
63 }
64}
Chris Lattner6cd71f02010-09-02 23:21:44 +000065class CommentWriter : public AssemblyAnnotationWriter {
66public:
67 void emitFunctionAnnot(const Function *F,
68 formatted_raw_ostream &OS) {
69 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
70 OS << '\n';
71 }
72 void printInfoComment(const Value &V, formatted_raw_ostream &OS) {
Devang Patel027f0992011-03-11 18:07:33 +000073 bool Padded = false;
74 if (!V.getType()->isVoidTy()) {
75 OS.PadToColumn(50);
76 Padded = true;
Chris Lattner0cd0d882011-06-18 21:18:23 +000077 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type
Devang Patel027f0992011-03-11 18:07:33 +000078 }
79 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
80 const DebugLoc &DL = I->getDebugLoc();
81 if (!DL.isUnknown()) {
82 if (!Padded) {
83 OS.PadToColumn(50);
84 Padded = true;
85 OS << ";";
86 }
87 OS << " [debug line = ";
88 printDebugLoc(DL,OS);
89 OS << "]";
90 }
91 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
92 DIVariable Var(DDI->getVariable());
93 if (!Padded) {
94 OS.PadToColumn(50);
95 Padded = true;
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);
104 Padded = true;
105 OS << ";";
106 }
107 OS << " [debug variable = " << Var.getName() << "]";
108 }
109 }
Chris Lattner6cd71f02010-09-02 23:21:44 +0000110 }
111};
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000112
Chris Lattner6cd71f02010-09-02 23:21:44 +0000113} // end anon namespace
114
Chris Lattner00950542001-06-06 20:29:01 +0000115int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000116 // Print a stack trace if we signal out.
117 sys::PrintStackTraceOnErrorSignal();
118 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000119
Owen Anderson0d7c6952009-07-15 22:16:10 +0000120 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000121 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000122
123
Chris Lattner17e9edc2009-08-23 02:51:22 +0000124 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000125
Chris Lattner17e9edc2009-08-23 02:51:22 +0000126 std::string ErrorMessage;
127 std::auto_ptr<Module> M;
Michael J. Spencer333fb042010-12-09 17:36:48 +0000128
Michael J. Spencerc448aa62010-12-16 22:37:52 +0000129 {
130 OwningPtr<MemoryBuffer> BufferPtr;
131 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
132 ErrorMessage = ec.message();
133 else
134 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
135 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000136
Chris Lattner17e9edc2009-08-23 02:51:22 +0000137 if (M.get() == 0) {
138 errs() << argv[0] << ": ";
139 if (ErrorMessage.size())
140 errs() << ErrorMessage << "\n";
141 else
142 errs() << "bitcode didn't read correctly.\n";
143 return 1;
144 }
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000145
Chris Lattner17e9edc2009-08-23 02:51:22 +0000146 // Just use stdout. We won't actually print anything on it.
147 if (DontPrint)
148 OutputFilename = "-";
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000149
Chris Lattner17e9edc2009-08-23 02:51:22 +0000150 if (OutputFilename.empty()) { // Unspecified output, infer it.
151 if (InputFilename == "-") {
152 OutputFilename = "-";
153 } else {
154 const std::string &IFN = InputFilename;
155 int Len = IFN.length();
156 // If the source ends in .bc, strip it off.
157 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
158 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
159 else
160 OutputFilename = IFN+".ll";
161 }
162 }
Dan Gohmanec080462009-09-11 20:46:33 +0000163
Chris Lattner17e9edc2009-08-23 02:51:22 +0000164 std::string ErrorInfo;
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000165 OwningPtr<tool_output_file>
Dan Gohmand5826a32010-08-20 01:07:01 +0000166 Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
167 raw_fd_ostream::F_Binary));
Chris Lattner17e9edc2009-08-23 02:51:22 +0000168 if (!ErrorInfo.empty()) {
169 errs() << ErrorInfo << '\n';
Chris Lattner17e9edc2009-08-23 02:51:22 +0000170 return 1;
171 }
172
Chris Lattner6cd71f02010-09-02 23:21:44 +0000173 OwningPtr<AssemblyAnnotationWriter> Annotator;
174 if (ShowAnnotations)
175 Annotator.reset(new CommentWriter());
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000176
Chris Lattner17e9edc2009-08-23 02:51:22 +0000177 // All that llvm-dis does is write the assembly to a file.
Dan Gohman04fc8c82009-09-15 15:33:42 +0000178 if (!DontPrint)
Chris Lattner6cd71f02010-09-02 23:21:44 +0000179 M->print(Out->os(), Annotator.get());
Chris Lattner17e9edc2009-08-23 02:51:22 +0000180
Dan Gohmand5826a32010-08-20 01:07:01 +0000181 // Declare success.
182 Out->keep();
183
Chris Lattner17e9edc2009-08-23 02:51:22 +0000184 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000185}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000186