blob: 5d609468a380b21779975b60753dccf61b1b6163 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswell09344dc2003-10-20 17:47:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00008//
9// This utility may be invoked in the following manner:
Gabor Greife16561c2007-07-05 17:07:56 +000010// llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout
11// llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
Misha Brukmanf12549d2003-08-28 21:34:13 +000012// to the x.ll file.
Chris Lattnerf284ac52001-06-13 19:55:41 +000013// Options:
14// --help - Output information about command line switches
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//
Chris Lattner6d56c6b2001-10-13 07:06:57 +000016//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000017
Teresa Johnsonad176792016-11-11 05:34:58 +000018#include "llvm/Bitcode/BitcodeReader.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000019#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000020#include "llvm/IR/DebugInfo.h"
Rafael Espindolad0b23be2015-01-10 00:07:30 +000021#include "llvm/IR/DiagnosticInfo.h"
22#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/IntrinsicInst.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000024#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Module.h"
26#include "llvm/IR/Type.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
Peter Collingbourne21521892016-06-21 23:42:48 +000028#include "llvm/Support/Error.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000029#include "llvm/Support/FileSystem.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000030#include "llvm/Support/FormattedStream.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000031#include "llvm/Support/InitLLVM.h"
Chris Lattner6694f602007-04-29 07:54:31 +000032#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000033#include "llvm/Support/ToolOutputFile.h"
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +000034#include "llvm/Support/WithColor.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000035#include <system_error>
Brian Gaeke960707c2003-11-11 22:41:34 +000036using namespace llvm;
37
Chris Lattner64a67272002-07-25 16:31:09 +000038static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000039InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000040
Chris Lattner64a67272002-07-25 16:31:09 +000041static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000042OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000043 cl::value_desc("filename"));
44
45static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000046Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000047
Chris Lattner216835d2007-02-07 04:39:35 +000048static cl::opt<bool>
49DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
50
Chris Lattner7f2f0932010-09-02 23:21:44 +000051static cl::opt<bool>
Charles Saternos55d93e72017-08-15 22:23:44 +000052 SetImporting("set-importing",
53 cl::desc("Set lazy loading to pretend to import a module"),
54 cl::Hidden);
55
56static cl::opt<bool>
57 ShowAnnotations("show-annotations",
58 cl::desc("Add informational comments to the .ll file"));
Chris Lattner7f2f0932010-09-02 23:21:44 +000059
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +000060static cl::opt<bool> PreserveAssemblyUseListOrder(
61 "preserve-ll-uselistorder",
62 cl::desc("Preserve use-list order when writing LLVM assembly."),
63 cl::init(false), cl::Hidden);
64
Peter Collingbourne21521892016-06-21 23:42:48 +000065static cl::opt<bool>
66 MaterializeMetadata("materialize-metadata",
67 cl::desc("Load module without materializing metadata, "
68 "then materialize only the metadata"));
69
Chris Lattner7f2f0932010-09-02 23:21:44 +000070namespace {
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +000071
Devang Patel982efb52011-03-11 18:07:33 +000072static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
73 OS << DL.getLine() << ":" << DL.getCol();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000074 if (DILocation *IDL = DL.getInlinedAt()) {
Duncan P. N. Exon Smithb8afdd62015-03-30 20:04:06 +000075 OS << "@";
76 printDebugLoc(IDL, OS);
Devang Patel982efb52011-03-11 18:07:33 +000077 }
78}
Chris Lattner7f2f0932010-09-02 23:21:44 +000079class CommentWriter : public AssemblyAnnotationWriter {
80public:
81 void emitFunctionAnnot(const Function *F,
Craig Toppere56917c2014-03-08 08:27:28 +000082 formatted_raw_ostream &OS) override {
Chris Lattner7f2f0932010-09-02 23:21:44 +000083 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
84 OS << '\n';
85 }
Craig Toppere56917c2014-03-08 08:27:28 +000086 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
Devang Patel982efb52011-03-11 18:07:33 +000087 bool Padded = false;
88 if (!V.getType()->isVoidTy()) {
89 OS.PadToColumn(50);
90 Padded = true;
Alexey Samsonovd9235082015-05-11 21:20:20 +000091 // Output # uses and type
92 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
Devang Patel982efb52011-03-11 18:07:33 +000093 }
94 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
Duncan P. N. Exon Smithb8afdd62015-03-30 20:04:06 +000095 if (const DebugLoc &DL = I->getDebugLoc()) {
Devang Patel982efb52011-03-11 18:07:33 +000096 if (!Padded) {
97 OS.PadToColumn(50);
98 Padded = true;
99 OS << ";";
100 }
101 OS << " [debug line = ";
102 printDebugLoc(DL,OS);
103 OS << "]";
104 }
105 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
Devang Patel982efb52011-03-11 18:07:33 +0000106 if (!Padded) {
107 OS.PadToColumn(50);
Devang Patel982efb52011-03-11 18:07:33 +0000108 OS << ";";
109 }
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000110 OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
Devang Patel982efb52011-03-11 18:07:33 +0000111 }
112 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
Devang Patel982efb52011-03-11 18:07:33 +0000113 if (!Padded) {
114 OS.PadToColumn(50);
Devang Patel982efb52011-03-11 18:07:33 +0000115 OS << ";";
116 }
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000117 OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
Devang Patel982efb52011-03-11 18:07:33 +0000118 }
119 }
Chris Lattner7f2f0932010-09-02 23:21:44 +0000120 }
121};
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000122
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000123struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
124 char *Prefix;
125 LLVMDisDiagnosticHandler(char *PrefixPtr) : Prefix(PrefixPtr) {}
126 bool handleDiagnostics(const DiagnosticInfo &DI) override {
127 raw_ostream &OS = errs();
128 OS << Prefix << ": ";
129 switch (DI.getSeverity()) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000130 case DS_Error: WithColor::error(OS); break;
131 case DS_Warning: WithColor::warning(OS); break;
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000132 case DS_Remark: OS << "remark: "; break;
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000133 case DS_Note: WithColor::note(OS); break;
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000134 }
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000135
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000136 DiagnosticPrinterRawOStream DP(OS);
137 DI.print(DP);
138 OS << '\n';
139
140 if (DI.getSeverity() == DS_Error)
141 exit(1);
142 return true;
Vivek Pandyadf8598d2017-09-15 19:53:54 +0000143 }
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000144};
145} // end anon namespace
Vivek Pandyadf8598d2017-09-15 19:53:54 +0000146
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000147static ExitOnError ExitOnErr;
148
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000150 InitLLVM X(argc, argv);
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000151
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000152 ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
153
Mehdi Amini03b42e42016-04-14 21:59:01 +0000154 LLVMContext Context;
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000155 Context.setDiagnosticHandler(
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000156 std::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000157 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattner12439ff2004-02-19 20:32:12 +0000158
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000159 std::unique_ptr<MemoryBuffer> MB =
160 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000161
Matthew Voss141bb5f2019-11-12 16:29:37 -0800162 BitcodeFileContents IF = ExitOnErr(llvm::getBitcodeFileContents(*MB));
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000163
Matthew Voss141bb5f2019-11-12 16:29:37 -0800164 const size_t N = IF.Mods.size();
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000165
Matthew Voss141bb5f2019-11-12 16:29:37 -0800166 if (OutputFilename == "-" && N > 1)
167 errs() << "only single module bitcode files can be written to stdout\n";
168
169 for (size_t i = 0; i < N; ++i) {
170 BitcodeModule MB = IF.Mods[i];
171 std::unique_ptr<Module> M = ExitOnErr(MB.getLazyModule(Context, MaterializeMetadata,
172 SetImporting));
173 if (MaterializeMetadata)
174 ExitOnErr(M->materializeMetadata());
175 else
176 ExitOnErr(M->materializeAll());
177
178 BitcodeLTOInfo LTOInfo = ExitOnErr(MB.getLTOInfo());
179 std::unique_ptr<ModuleSummaryIndex> Index;
180 if (LTOInfo.HasSummary)
181 Index = ExitOnErr(MB.getSummary());
182
183 std::string FinalFilename(OutputFilename);
184
185 // Just use stdout. We won't actually print anything on it.
186 if (DontPrint)
187 FinalFilename = "-";
188
Matthew Vosse69a7362019-11-14 12:33:52 -0800189 if (FinalFilename.empty()) { // Unspecified output, infer it.
Matthew Voss141bb5f2019-11-12 16:29:37 -0800190 if (InputFilename == "-") {
191 FinalFilename = "-";
192 } else {
193 StringRef IFN = InputFilename;
194 FinalFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
195 if (N > 1)
196 FinalFilename += std::string(".") + std::to_string(i);
197 FinalFilename += ".ll";
198 }
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000199 } else {
Matthew Voss141bb5f2019-11-12 16:29:37 -0800200 if (N > 1)
201 FinalFilename += std::string(".") + std::to_string(i);
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000202 }
Matthew Voss141bb5f2019-11-12 16:29:37 -0800203
204 std::error_code EC;
205 std::unique_ptr<ToolOutputFile> Out(
206 new ToolOutputFile(FinalFilename, EC, sys::fs::OF_Text));
207 if (EC) {
208 errs() << EC.message() << '\n';
209 return 1;
210 }
211
212 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
213 if (ShowAnnotations)
214 Annotator.reset(new CommentWriter());
215
216 // All that llvm-dis does is write the assembly to a file.
217 if (!DontPrint) {
218 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
219 if (Index)
220 Index->print(Out->os());
221 }
222
223 // Declare success.
224 Out->keep();
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000225 }
Dan Gohmane5929232009-09-11 20:46:33 +0000226
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000227 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228}