blob: 1349ecc85a4cddbbabe886f7ef8f2fdb70e5dbbb [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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/LLVMContext.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000021#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000022#include "llvm/IR/DebugInfo.h"
Rafael Espindolad0b23be2015-01-10 00:07:30 +000023#include "llvm/IR/DiagnosticInfo.h"
24#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Module.h"
27#include "llvm/IR/Type.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/Support/CommandLine.h"
Derek Schuff8b2dcad2012-02-06 22:30:29 +000029#include "llvm/Support/DataStream.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000030#include "llvm/Support/FileSystem.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000031#include "llvm/Support/FormattedStream.h"
Chris Lattner76d46322006-12-06 01:18:01 +000032#include "llvm/Support/ManagedStatic.h"
Chris Lattner6694f602007-04-29 07:54:31 +000033#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000034#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000035#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000036#include "llvm/Support/ToolOutputFile.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000037#include <system_error>
Brian Gaeke960707c2003-11-11 22:41:34 +000038using namespace llvm;
39
Chris Lattner64a67272002-07-25 16:31:09 +000040static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000041InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000042
Chris Lattner64a67272002-07-25 16:31:09 +000043static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000044OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000045 cl::value_desc("filename"));
46
47static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000048Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000049
Chris Lattner216835d2007-02-07 04:39:35 +000050static cl::opt<bool>
51DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
52
Chris Lattner7f2f0932010-09-02 23:21:44 +000053static cl::opt<bool>
54ShowAnnotations("show-annotations",
55 cl::desc("Add informational comments to the .ll file"));
56
57namespace {
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +000058
Devang Patel982efb52011-03-11 18:07:33 +000059static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
60 OS << DL.getLine() << ":" << DL.getCol();
61 if (MDNode *N = DL.getInlinedAt(getGlobalContext())) {
62 DebugLoc IDL = DebugLoc::getFromDILocation(N);
63 if (!IDL.isUnknown()) {
64 OS << "@";
65 printDebugLoc(IDL,OS);
66 }
67 }
68}
Chris Lattner7f2f0932010-09-02 23:21:44 +000069class CommentWriter : public AssemblyAnnotationWriter {
70public:
71 void emitFunctionAnnot(const Function *F,
Craig Toppere56917c2014-03-08 08:27:28 +000072 formatted_raw_ostream &OS) override {
Chris Lattner7f2f0932010-09-02 23:21:44 +000073 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
74 OS << '\n';
75 }
Craig Toppere56917c2014-03-08 08:27:28 +000076 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
Devang Patel982efb52011-03-11 18:07:33 +000077 bool Padded = false;
78 if (!V.getType()->isVoidTy()) {
79 OS.PadToColumn(50);
80 Padded = true;
Chris Lattner0f214eb2011-06-18 21:18:23 +000081 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type
Devang Patel982efb52011-03-11 18:07:33 +000082 }
83 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
84 const DebugLoc &DL = I->getDebugLoc();
85 if (!DL.isUnknown()) {
86 if (!Padded) {
87 OS.PadToColumn(50);
88 Padded = true;
89 OS << ";";
90 }
91 OS << " [debug line = ";
92 printDebugLoc(DL,OS);
93 OS << "]";
94 }
95 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
96 DIVariable Var(DDI->getVariable());
97 if (!Padded) {
98 OS.PadToColumn(50);
Devang Patel982efb52011-03-11 18:07:33 +000099 OS << ";";
100 }
101 OS << " [debug variable = " << Var.getName() << "]";
102 }
103 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
104 DIVariable Var(DVI->getVariable());
105 if (!Padded) {
106 OS.PadToColumn(50);
Devang Patel982efb52011-03-11 18:07:33 +0000107 OS << ";";
108 }
109 OS << " [debug variable = " << Var.getName() << "]";
110 }
111 }
Chris Lattner7f2f0932010-09-02 23:21:44 +0000112 }
113};
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000114
Chris Lattner7f2f0932010-09-02 23:21:44 +0000115} // end anon namespace
116
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000117static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
118 assert(DI.getSeverity() == DS_Error && "Only expecting errors");
119
120 raw_ostream &OS = errs();
121 OS << (char *)Context << ": ";
122 DiagnosticPrinterRawOStream DP(OS);
123 DI.print(DP);
124 OS << '\n';
125 exit(1);
126}
127
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000129 // Print a stack trace if we signal out.
130 sys::PrintStackTraceOnErrorSignal();
131 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000132
Owen Anderson19251ec2009-07-15 22:16:10 +0000133 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000134 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000135
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000136 Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000137
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000138 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattner12439ff2004-02-19 20:32:12 +0000139
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000140 std::string ErrorMessage;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000141 std::unique_ptr<Module> M;
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000142
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000143 // Use the bitcode streaming interface
Rafael Espindola7d727b52014-12-18 05:08:43 +0000144 DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
145 if (Streamer) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000146 std::string DisplayFilename;
147 if (InputFilename == "-")
148 DisplayFilename = "<stdin>";
Michael J. Spencera646f392010-12-16 22:37:52 +0000149 else
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000150 DisplayFilename = InputFilename;
Rafael Espindola7d727b52014-12-18 05:08:43 +0000151 ErrorOr<std::unique_ptr<Module>> MOrErr =
152 getStreamedBitcodeModule(DisplayFilename, Streamer, Context);
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000153 M = std::move(*MOrErr);
154 M->materializeAllPermanently();
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000155 }
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000156
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000157 // Just use stdout. We won't actually print anything on it.
158 if (DontPrint)
159 OutputFilename = "-";
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000160
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000161 if (OutputFilename.empty()) { // Unspecified output, infer it.
162 if (InputFilename == "-") {
163 OutputFilename = "-";
164 } else {
165 const std::string &IFN = InputFilename;
166 int Len = IFN.length();
167 // If the source ends in .bc, strip it off.
168 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
169 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
170 else
171 OutputFilename = IFN+".ll";
172 }
173 }
Dan Gohmane5929232009-09-11 20:46:33 +0000174
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000175 std::error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000176 std::unique_ptr<tool_output_file> Out(
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000177 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
178 if (EC) {
179 errs() << EC.message() << '\n';
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000180 return 1;
181 }
182
Ahmed Charles56440fd2014-03-06 05:51:42 +0000183 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
Chris Lattner7f2f0932010-09-02 23:21:44 +0000184 if (ShowAnnotations)
185 Annotator.reset(new CommentWriter());
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000186
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000187 // All that llvm-dis does is write the assembly to a file.
Dan Gohman972c9c52009-09-15 15:33:42 +0000188 if (!DontPrint)
Chris Lattner7f2f0932010-09-02 23:21:44 +0000189 M->print(Out->os(), Annotator.get());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000190
Dan Gohman268b0f42010-08-20 01:07:01 +0000191 // Declare success.
192 Out->keep();
193
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000194 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195}