blob: 3f337b874b166cb94f7ff64514625abfcb5bf9da [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(
156 llvm::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)));
161 std::unique_ptr<Module> M = ExitOnErr(getLazyBitcodeModule(
162 *MB, Context, /*ShouldLazyLoadMetadata=*/true, SetImporting));
163 if (MaterializeMetadata)
164 ExitOnErr(M->materializeMetadata());
165 else
166 ExitOnErr(M->materializeAll());
167
168 BitcodeLTOInfo LTOInfo = ExitOnErr(getBitcodeLTOInfo(*MB));
169 std::unique_ptr<ModuleSummaryIndex> Index;
170 if (LTOInfo.HasSummary)
171 Index = ExitOnErr(getModuleSummaryIndex(*MB));
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000172
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000173 // Just use stdout. We won't actually print anything on it.
174 if (DontPrint)
175 OutputFilename = "-";
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000176
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000177 if (OutputFilename.empty()) { // Unspecified output, infer it.
178 if (InputFilename == "-") {
179 OutputFilename = "-";
180 } else {
Alexey Samsonovd9235082015-05-11 21:20:20 +0000181 StringRef IFN = InputFilename;
182 OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
183 OutputFilename += ".ll";
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000184 }
185 }
Dan Gohmane5929232009-09-11 20:46:33 +0000186
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000187 std::error_code EC;
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000188 std::unique_ptr<ToolOutputFile> Out(
189 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000190 if (EC) {
191 errs() << EC.message() << '\n';
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000192 return 1;
193 }
194
Ahmed Charles56440fd2014-03-06 05:51:42 +0000195 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
Chris Lattner7f2f0932010-09-02 23:21:44 +0000196 if (ShowAnnotations)
197 Annotator.reset(new CommentWriter());
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000198
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000199 // All that llvm-dis does is write the assembly to a file.
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000200 if (!DontPrint) {
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000201 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000202 if (Index)
203 Index->print(Out->os());
204 }
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000205
Dan Gohman268b0f42010-08-20 01:07:01 +0000206 // Declare success.
207 Out->keep();
208
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000209 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210}