blob: 8143a2a5a9343e6b5bd4740484d7c585d381da1c [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
Teresa Johnsona5479192016-11-11 05:34:58 +000019#include "llvm/Bitcode/BitcodeReader.h"
Chandler Carruthbc65a8d2014-01-07 12:34:26 +000020#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruthf4ec8bf2014-03-06 00:46:21 +000021#include "llvm/IR/DebugInfo.h"
Rafael Espindola68016e02015-01-10 00:07:30 +000022#include "llvm/IR/DiagnosticInfo.h"
23#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/IntrinsicInst.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000025#include "llvm/IR/LLVMContext.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
27#include "llvm/IR/Type.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/CommandLine.h"
Peter Collingbourne99e2e272016-06-21 23:42:48 +000029#include "llvm/Support/Error.h"
Benjamin Kramer7259f142014-04-29 23:26:49 +000030#include "llvm/Support/FileSystem.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000031#include "llvm/Support/FormattedStream.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000032#include "llvm/Support/InitLLVM.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000033#include "llvm/Support/MemoryBuffer.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000034#include "llvm/Support/ToolOutputFile.h"
Jonas Devlieghereae6875e2018-04-21 21:11:59 +000035#include "llvm/Support/WithColor.h"
Rafael Espindolad5132f92014-06-12 17:38:55 +000036#include <system_error>
Brian Gaeked0fde302003-11-11 22:41:34 +000037using namespace llvm;
38
Chris Lattnerc7a09852002-07-25 16:31:09 +000039static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000040InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Chris Lattnerc7a09852002-07-25 16:31:09 +000042static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000043OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000044 cl::value_desc("filename"));
45
46static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000047Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000048
Chris Lattner4d544cd2007-02-07 04:39:35 +000049static cl::opt<bool>
50DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
51
Chris Lattner6cd71f02010-09-02 23:21:44 +000052static cl::opt<bool>
Charles Saternos417caf52017-08-15 22:23:44 +000053 SetImporting("set-importing",
54 cl::desc("Set lazy loading to pretend to import a module"),
55 cl::Hidden);
56
57static cl::opt<bool>
58 ShowAnnotations("show-annotations",
59 cl::desc("Add informational comments to the .ll file"));
Chris Lattner6cd71f02010-09-02 23:21:44 +000060
Duncan P. N. Exon Smith8d61ee92015-04-15 03:14:06 +000061static cl::opt<bool> PreserveAssemblyUseListOrder(
62 "preserve-ll-uselistorder",
63 cl::desc("Preserve use-list order when writing LLVM assembly."),
64 cl::init(false), cl::Hidden);
65
Peter Collingbourne99e2e272016-06-21 23:42:48 +000066static cl::opt<bool>
67 MaterializeMetadata("materialize-metadata",
68 cl::desc("Load module without materializing metadata, "
69 "then materialize only the metadata"));
70
Chris Lattner6cd71f02010-09-02 23:21:44 +000071namespace {
Michael J. Spencer7aa48442010-12-16 16:23:30 +000072
Devang Patel027f0992011-03-11 18:07:33 +000073static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
74 OS << DL.getLine() << ":" << DL.getCol();
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000075 if (DILocation *IDL = DL.getInlinedAt()) {
Duncan P. N. Exon Smith68338f82015-03-30 20:04:06 +000076 OS << "@";
77 printDebugLoc(IDL, OS);
Devang Patel027f0992011-03-11 18:07:33 +000078 }
79}
Chris Lattner6cd71f02010-09-02 23:21:44 +000080class CommentWriter : public AssemblyAnnotationWriter {
81public:
82 void emitFunctionAnnot(const Function *F,
Craig Topperc83e68f2014-03-08 08:27:28 +000083 formatted_raw_ostream &OS) override {
Chris Lattner6cd71f02010-09-02 23:21:44 +000084 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
85 OS << '\n';
86 }
Craig Topperc83e68f2014-03-08 08:27:28 +000087 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
Devang Patel027f0992011-03-11 18:07:33 +000088 bool Padded = false;
89 if (!V.getType()->isVoidTy()) {
90 OS.PadToColumn(50);
91 Padded = true;
Alexey Samsonov4e5be5a2015-05-11 21:20:20 +000092 // Output # uses and type
93 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
Devang Patel027f0992011-03-11 18:07:33 +000094 }
95 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
Duncan P. N. Exon Smith68338f82015-03-30 20:04:06 +000096 if (const DebugLoc &DL = I->getDebugLoc()) {
Devang Patel027f0992011-03-11 18:07:33 +000097 if (!Padded) {
98 OS.PadToColumn(50);
99 Padded = true;
100 OS << ";";
101 }
102 OS << " [debug line = ";
103 printDebugLoc(DL,OS);
104 OS << "]";
105 }
106 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
Devang Patel027f0992011-03-11 18:07:33 +0000107 if (!Padded) {
108 OS.PadToColumn(50);
Devang Patel027f0992011-03-11 18:07:33 +0000109 OS << ";";
110 }
Duncan P. N. Exon Smith7f892712015-04-21 18:44:06 +0000111 OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
Devang Patel027f0992011-03-11 18:07:33 +0000112 }
113 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
Devang Patel027f0992011-03-11 18:07:33 +0000114 if (!Padded) {
115 OS.PadToColumn(50);
Devang Patel027f0992011-03-11 18:07:33 +0000116 OS << ";";
117 }
Duncan P. N. Exon Smith7f892712015-04-21 18:44:06 +0000118 OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
Devang Patel027f0992011-03-11 18:07:33 +0000119 }
120 }
Chris Lattner6cd71f02010-09-02 23:21:44 +0000121 }
122};
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000123
Vivek Pandya18b4c372017-09-15 20:10:09 +0000124struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
125 char *Prefix;
126 LLVMDisDiagnosticHandler(char *PrefixPtr) : Prefix(PrefixPtr) {}
127 bool handleDiagnostics(const DiagnosticInfo &DI) override {
128 raw_ostream &OS = errs();
129 OS << Prefix << ": ";
130 switch (DI.getSeverity()) {
Jonas Devlieghereae6875e2018-04-21 21:11:59 +0000131 case DS_Error: WithColor::error(OS); break;
132 case DS_Warning: WithColor::warning(OS); break;
Vivek Pandya18b4c372017-09-15 20:10:09 +0000133 case DS_Remark: OS << "remark: "; break;
Jonas Devlieghereae6875e2018-04-21 21:11:59 +0000134 case DS_Note: WithColor::note(OS); break;
Vivek Pandya18b4c372017-09-15 20:10:09 +0000135 }
Rafael Espindola68016e02015-01-10 00:07:30 +0000136
Vivek Pandya18b4c372017-09-15 20:10:09 +0000137 DiagnosticPrinterRawOStream DP(OS);
138 DI.print(DP);
139 OS << '\n';
140
141 if (DI.getSeverity() == DS_Error)
142 exit(1);
143 return true;
Vivek Pandya4abccff2017-09-15 19:53:54 +0000144 }
Vivek Pandya18b4c372017-09-15 20:10:09 +0000145};
146} // end anon namespace
Vivek Pandya4abccff2017-09-15 19:53:54 +0000147
Peter Collingbourne76c218e2016-11-09 17:49:19 +0000148static ExitOnError ExitOnErr;
149
Chris Lattner00950542001-06-06 20:29:01 +0000150int main(int argc, char **argv) {
Rui Ueyama0b9d56a2018-04-13 18:26:06 +0000151 InitLLVM X(argc, argv);
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000152
Peter Collingbourne76c218e2016-11-09 17:49:19 +0000153 ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
154
Mehdi Amini8be77072016-04-14 21:59:01 +0000155 LLVMContext Context;
Vivek Pandya18b4c372017-09-15 20:10:09 +0000156 Context.setDiagnosticHandler(
157 llvm::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
Chris Lattner17e9edc2009-08-23 02:51:22 +0000158 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000159
Teresa Johnsona9a21472018-05-26 02:34:13 +0000160 std::unique_ptr<MemoryBuffer> MB =
161 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
162 std::unique_ptr<Module> M = ExitOnErr(getLazyBitcodeModule(
163 *MB, Context, /*ShouldLazyLoadMetadata=*/true, SetImporting));
164 if (MaterializeMetadata)
165 ExitOnErr(M->materializeMetadata());
166 else
167 ExitOnErr(M->materializeAll());
168
169 BitcodeLTOInfo LTOInfo = ExitOnErr(getBitcodeLTOInfo(*MB));
170 std::unique_ptr<ModuleSummaryIndex> Index;
171 if (LTOInfo.HasSummary)
172 Index = ExitOnErr(getModuleSummaryIndex(*MB));
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000173
Chris Lattner17e9edc2009-08-23 02:51:22 +0000174 // Just use stdout. We won't actually print anything on it.
175 if (DontPrint)
176 OutputFilename = "-";
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000177
Chris Lattner17e9edc2009-08-23 02:51:22 +0000178 if (OutputFilename.empty()) { // Unspecified output, infer it.
179 if (InputFilename == "-") {
180 OutputFilename = "-";
181 } else {
Alexey Samsonov4e5be5a2015-05-11 21:20:20 +0000182 StringRef IFN = InputFilename;
183 OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
184 OutputFilename += ".ll";
Chris Lattner17e9edc2009-08-23 02:51:22 +0000185 }
186 }
Dan Gohmanec080462009-09-11 20:46:33 +0000187
Rafael Espindola8c968622014-08-25 18:16:47 +0000188 std::error_code EC;
Reid Kleckner97ca9642017-09-23 01:03:17 +0000189 std::unique_ptr<ToolOutputFile> Out(
190 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
Rafael Espindola8c968622014-08-25 18:16:47 +0000191 if (EC) {
192 errs() << EC.message() << '\n';
Chris Lattner17e9edc2009-08-23 02:51:22 +0000193 return 1;
194 }
195
Ahmed Charlesf4ccd112014-03-06 05:51:42 +0000196 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
Chris Lattner6cd71f02010-09-02 23:21:44 +0000197 if (ShowAnnotations)
198 Annotator.reset(new CommentWriter());
Michael J. Spencer7aa48442010-12-16 16:23:30 +0000199
Chris Lattner17e9edc2009-08-23 02:51:22 +0000200 // All that llvm-dis does is write the assembly to a file.
Teresa Johnsona9a21472018-05-26 02:34:13 +0000201 if (!DontPrint) {
Duncan P. N. Exon Smith8d61ee92015-04-15 03:14:06 +0000202 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
Teresa Johnsona9a21472018-05-26 02:34:13 +0000203 if (Index)
204 Index->print(Out->os());
205 }
Chris Lattner17e9edc2009-08-23 02:51:22 +0000206
Dan Gohmand5826a32010-08-20 01:07:01 +0000207 // Declare success.
208 Out->keep();
209
Chris Lattner17e9edc2009-08-23 02:51:22 +0000210 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000211}