blob: 87eb34708a1debedd102b2e131b2de8d7b8deb0b [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
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/LLVMContext.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000020#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000021#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Type.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Derek Schuff2ea93872012-02-06 22:30:29 +000027#include "llvm/Support/DataStream.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000028#include "llvm/Support/FormattedStream.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000029#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000030#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000031#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000032#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000033#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000034#include "llvm/Support/system_error.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000035using namespace llvm;
36
Chris Lattnerc7a09852002-07-25 16:31:09 +000037static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000038InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000039
Chris Lattnerc7a09852002-07-25 16:31:09 +000040static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000041OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000042 cl::value_desc("filename"));
43
44static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000045Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000046
Chris Lattner4d544cd2007-02-07 04:39:35 +000047static cl::opt<bool>
48DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
49
Chris Lattner6cd71f02010-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. Spencer7aa48442010-12-16 16:23:30 +000055
Devang Patel027f0992011-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 Lattner6cd71f02010-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 Patel027f0992011-03-11 18:07:33 +000074 bool Padded = false;
75 if (!V.getType()->isVoidTy()) {
76 OS.PadToColumn(50);
77 Padded = true;
Chris Lattner0cd0d882011-06-18 21:18:23 +000078 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type
Devang Patel027f0992011-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);
Devang Patel027f0992011-03-11 18:07:33 +000096 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 Patel027f0992011-03-11 18:07:33 +0000104 OS << ";";
105 }
106 OS << " [debug variable = " << Var.getName() << "]";
107 }
108 }
Chris Lattner6cd71f02010-09-02 23:21:44 +0000109 }
110};
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000111
Chris Lattner6cd71f02010-09-02 23:21:44 +0000112} // end anon namespace
113
Chris Lattner00950542001-06-06 20:29:01 +0000114int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000115 // Print a stack trace if we signal out.
116 sys::PrintStackTraceOnErrorSignal();
117 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000118
Owen Anderson0d7c6952009-07-15 22:16:10 +0000119 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000120 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000121
122
Chris Lattner17e9edc2009-08-23 02:51:22 +0000123 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000124
Chris Lattner17e9edc2009-08-23 02:51:22 +0000125 std::string ErrorMessage;
Andy Gibbs200241e2013-04-12 10:56:28 +0000126 OwningPtr<Module> M;
Michael J. Spencer333fb042010-12-09 17:36:48 +0000127
Derek Schuff2ea93872012-02-06 22:30:29 +0000128 // 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. Spencerc448aa62010-12-16 22:37:52 +0000134 else
Derek Schuff2ea93872012-02-06 22:30:29 +0000135 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. Spencerc448aa62010-12-16 22:37:52 +0000141 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000142
Chris Lattner17e9edc2009-08-23 02:51:22 +0000143 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. Spencer7aa48442010-12-16 16:23:30 +0000151
Chris Lattner17e9edc2009-08-23 02:51:22 +0000152 // Just use stdout. We won't actually print anything on it.
153 if (DontPrint)
154 OutputFilename = "-";
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000155
Chris Lattner17e9edc2009-08-23 02:51:22 +0000156 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 Gohmanec080462009-09-11 20:46:33 +0000169
Chris Lattner17e9edc2009-08-23 02:51:22 +0000170 std::string ErrorInfo;
Rafael Espindolac1b49b52013-07-16 19:44:17 +0000171 OwningPtr<tool_output_file> Out(new tool_output_file(
172 OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary));
Chris Lattner17e9edc2009-08-23 02:51:22 +0000173 if (!ErrorInfo.empty()) {
174 errs() << ErrorInfo << '\n';
Chris Lattner17e9edc2009-08-23 02:51:22 +0000175 return 1;
176 }
177
Chris Lattner6cd71f02010-09-02 23:21:44 +0000178 OwningPtr<AssemblyAnnotationWriter> Annotator;
179 if (ShowAnnotations)
180 Annotator.reset(new CommentWriter());
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000181
Chris Lattner17e9edc2009-08-23 02:51:22 +0000182 // All that llvm-dis does is write the assembly to a file.
Dan Gohman04fc8c82009-09-15 15:33:42 +0000183 if (!DontPrint)
Chris Lattner6cd71f02010-09-02 23:21:44 +0000184 M->print(Out->os(), Annotator.get());
Chris Lattner17e9edc2009-08-23 02:51:22 +0000185
Dan Gohmand5826a32010-08-20 01:07:01 +0000186 // Declare success.
187 Out->keep();
188
Chris Lattner17e9edc2009-08-23 02:51:22 +0000189 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000190}