blob: 82dbaa5e3c60c285521dc0ec8f016492322a0e80 [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"
Teresa Johnsonad176792016-11-11 05:34:58 +000020#include "llvm/Bitcode/BitcodeReader.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"
Peter Collingbourne21521892016-06-21 23:42:48 +000029#include "llvm/Support/Error.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>
Charles Saternos55d93e72017-08-15 22:23:44 +000054 SetImporting("set-importing",
55 cl::desc("Set lazy loading to pretend to import a module"),
56 cl::Hidden);
57
58static cl::opt<bool>
59 ShowAnnotations("show-annotations",
60 cl::desc("Add informational comments to the .ll file"));
Chris Lattner7f2f0932010-09-02 23:21:44 +000061
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +000062static cl::opt<bool> PreserveAssemblyUseListOrder(
63 "preserve-ll-uselistorder",
64 cl::desc("Preserve use-list order when writing LLVM assembly."),
65 cl::init(false), cl::Hidden);
66
Peter Collingbourne21521892016-06-21 23:42:48 +000067static cl::opt<bool>
68 MaterializeMetadata("materialize-metadata",
69 cl::desc("Load module without materializing metadata, "
70 "then materialize only the metadata"));
71
Chris Lattner7f2f0932010-09-02 23:21:44 +000072namespace {
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +000073
Devang Patel982efb52011-03-11 18:07:33 +000074static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
75 OS << DL.getLine() << ":" << DL.getCol();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000076 if (DILocation *IDL = DL.getInlinedAt()) {
Duncan P. N. Exon Smithb8afdd62015-03-30 20:04:06 +000077 OS << "@";
78 printDebugLoc(IDL, OS);
Devang Patel982efb52011-03-11 18:07:33 +000079 }
80}
Chris Lattner7f2f0932010-09-02 23:21:44 +000081class CommentWriter : public AssemblyAnnotationWriter {
82public:
83 void emitFunctionAnnot(const Function *F,
Craig Toppere56917c2014-03-08 08:27:28 +000084 formatted_raw_ostream &OS) override {
Chris Lattner7f2f0932010-09-02 23:21:44 +000085 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
86 OS << '\n';
87 }
Craig Toppere56917c2014-03-08 08:27:28 +000088 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
Devang Patel982efb52011-03-11 18:07:33 +000089 bool Padded = false;
90 if (!V.getType()->isVoidTy()) {
91 OS.PadToColumn(50);
92 Padded = true;
Alexey Samsonovd9235082015-05-11 21:20:20 +000093 // Output # uses and type
94 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
Devang Patel982efb52011-03-11 18:07:33 +000095 }
96 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
Duncan P. N. Exon Smithb8afdd62015-03-30 20:04:06 +000097 if (const DebugLoc &DL = I->getDebugLoc()) {
Devang Patel982efb52011-03-11 18:07:33 +000098 if (!Padded) {
99 OS.PadToColumn(50);
100 Padded = true;
101 OS << ";";
102 }
103 OS << " [debug line = ";
104 printDebugLoc(DL,OS);
105 OS << "]";
106 }
107 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
Devang Patel982efb52011-03-11 18:07:33 +0000108 if (!Padded) {
109 OS.PadToColumn(50);
Devang Patel982efb52011-03-11 18:07:33 +0000110 OS << ";";
111 }
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000112 OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
Devang Patel982efb52011-03-11 18:07:33 +0000113 }
114 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
Devang Patel982efb52011-03-11 18:07:33 +0000115 if (!Padded) {
116 OS.PadToColumn(50);
Devang Patel982efb52011-03-11 18:07:33 +0000117 OS << ";";
118 }
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000119 OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
Devang Patel982efb52011-03-11 18:07:33 +0000120 }
121 }
Chris Lattner7f2f0932010-09-02 23:21:44 +0000122 }
123};
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000124
Chris Lattner7f2f0932010-09-02 23:21:44 +0000125} // end anon namespace
126
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000127static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000128 raw_ostream &OS = errs();
129 OS << (char *)Context << ": ";
Duncan P. N. Exon Smitha6b88952015-02-25 01:10:03 +0000130 switch (DI.getSeverity()) {
131 case DS_Error: OS << "error: "; break;
132 case DS_Warning: OS << "warning: "; break;
133 case DS_Remark: OS << "remark: "; break;
134 case DS_Note: OS << "note: "; break;
135 }
136
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000137 DiagnosticPrinterRawOStream DP(OS);
138 DI.print(DP);
139 OS << '\n';
Duncan P. N. Exon Smitha6b88952015-02-25 01:10:03 +0000140
141 if (DI.getSeverity() == DS_Error)
142 exit(1);
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000143}
144
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000145static ExitOnError ExitOnErr;
146
147static std::unique_ptr<Module> openInputFile(LLVMContext &Context) {
148 std::unique_ptr<MemoryBuffer> MB =
149 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
Charles Saternos55d93e72017-08-15 22:23:44 +0000150 std::unique_ptr<Module> M = ExitOnErr(getOwningLazyBitcodeModule(
151 std::move(MB), Context,
152 /*ShouldLazyLoadMetadata=*/true, SetImporting));
Peter Collingbourne028eb5a2016-11-02 00:08:19 +0000153 if (MaterializeMetadata)
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000154 ExitOnErr(M->materializeMetadata());
Peter Collingbourne028eb5a2016-11-02 00:08:19 +0000155 else
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000156 ExitOnErr(M->materializeAll());
157 return M;
Peter Collingbourne21521892016-06-21 23:42:48 +0000158}
159
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000161 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000162 sys::PrintStackTraceOnErrorSignal(argv[0]);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000163 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000164
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000165 ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
166
Mehdi Amini03b42e42016-04-14 21:59:01 +0000167 LLVMContext Context;
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000168 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000169
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000170 Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000171
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000172 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattner12439ff2004-02-19 20:32:12 +0000173
Peter Collingbournebda44982016-11-17 00:42:08 +0000174 std::unique_ptr<Module> M = openInputFile(Context);
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000175
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000176 // Just use stdout. We won't actually print anything on it.
177 if (DontPrint)
178 OutputFilename = "-";
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000179
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000180 if (OutputFilename.empty()) { // Unspecified output, infer it.
181 if (InputFilename == "-") {
182 OutputFilename = "-";
183 } else {
Alexey Samsonovd9235082015-05-11 21:20:20 +0000184 StringRef IFN = InputFilename;
185 OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
186 OutputFilename += ".ll";
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000187 }
188 }
Dan Gohmane5929232009-09-11 20:46:33 +0000189
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000190 std::error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000191 std::unique_ptr<tool_output_file> Out(
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000192 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
193 if (EC) {
194 errs() << EC.message() << '\n';
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000195 return 1;
196 }
197
Ahmed Charles56440fd2014-03-06 05:51:42 +0000198 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
Chris Lattner7f2f0932010-09-02 23:21:44 +0000199 if (ShowAnnotations)
200 Annotator.reset(new CommentWriter());
Michael J. Spencer7fda8fe2010-12-16 16:23:30 +0000201
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000202 // All that llvm-dis does is write the assembly to a file.
Dan Gohman972c9c52009-09-15 15:33:42 +0000203 if (!DontPrint)
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000204 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
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}