1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This utility may be invoked in the following manner:
10 // 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
14 // --help - Output information about command line switches
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Bitcode/BitcodeReader.h"
19 #include "llvm/IR/AssemblyAnnotationWriter.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/FormattedStream.h"
31 #include "llvm/Support/InitLLVM.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/ToolOutputFile.h"
34 #include "llvm/Support/WithColor.h"
35 #include <system_error>
38 static cl::opt<std::string>
39 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
41 static cl::opt<std::string>
42 OutputFilename("o", cl::desc("Override output filename"),
43 cl::value_desc("filename"));
46 Force("f", cl::desc("Enable binary output on terminals"));
49 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
52 SetImporting("set-importing",
53 cl::desc("Set lazy loading to pretend to import a module"),
57 ShowAnnotations("show-annotations",
58 cl::desc("Add informational comments to the .ll file"));
60 static 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);
66 MaterializeMetadata("materialize-metadata",
67 cl::desc("Load module without materializing metadata, "
68 "then materialize only the metadata"));
72 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
73 OS << DL.getLine() << ":" << DL.getCol();
74 if (DILocation *IDL = DL.getInlinedAt()) {
76 printDebugLoc(IDL, OS);
79 class CommentWriter : public AssemblyAnnotationWriter {
81 void emitFunctionAnnot(const Function *F,
82 formatted_raw_ostream &OS) override {
83 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
86 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
88 if (!V.getType()->isVoidTy()) {
91 // Output # uses and type
92 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
94 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
95 if (const DebugLoc &DL = I->getDebugLoc()) {
101 OS << " [debug line = ";
102 printDebugLoc(DL,OS);
105 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
110 OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
112 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
117 OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
123 struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
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()) {
130 case DS_Error: WithColor::error(OS); break;
131 case DS_Warning: WithColor::warning(OS); break;
132 case DS_Remark: OS << "remark: "; break;
133 case DS_Note: WithColor::note(OS); break;
136 DiagnosticPrinterRawOStream DP(OS);
140 if (DI.getSeverity() == DS_Error)
145 } // end anon namespace
147 static ExitOnError ExitOnErr;
149 int main(int argc, char **argv) {
150 InitLLVM X(argc, argv);
152 ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
155 Context.setDiagnosticHandler(
156 llvm::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
157 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
159 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());
166 ExitOnErr(M->materializeAll());
168 BitcodeLTOInfo LTOInfo = ExitOnErr(getBitcodeLTOInfo(*MB));
169 std::unique_ptr<ModuleSummaryIndex> Index;
170 if (LTOInfo.HasSummary)
171 Index = ExitOnErr(getModuleSummaryIndex(*MB));
173 // Just use stdout. We won't actually print anything on it.
175 OutputFilename = "-";
177 if (OutputFilename.empty()) { // Unspecified output, infer it.
178 if (InputFilename == "-") {
179 OutputFilename = "-";
181 StringRef IFN = InputFilename;
182 OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
183 OutputFilename += ".ll";
188 std::unique_ptr<ToolOutputFile> Out(
189 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
191 errs() << EC.message() << '\n';
195 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
197 Annotator.reset(new CommentWriter());
199 // All that llvm-dis does is write the assembly to a file.
201 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
203 Index->print(Out->os());