1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility may be invoked in the following manner:
11 // 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
15 // --help - Output information about command line switches
17 //===----------------------------------------------------------------------===//
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/Bitcode/BitcodeReader.h"
21 #include "llvm/IR/AssemblyAnnotationWriter.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Error.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/ManagedStatic.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/PrettyStackTrace.h"
35 #include "llvm/Support/Signals.h"
36 #include "llvm/Support/ToolOutputFile.h"
37 #include <system_error>
40 static cl::opt<std::string>
41 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
43 static cl::opt<std::string>
44 OutputFilename("o", cl::desc("Override output filename"),
45 cl::value_desc("filename"));
48 Force("f", cl::desc("Enable binary output on terminals"));
51 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
54 ShowAnnotations("show-annotations",
55 cl::desc("Add informational comments to the .ll file"));
57 static cl::opt<bool> PreserveAssemblyUseListOrder(
58 "preserve-ll-uselistorder",
59 cl::desc("Preserve use-list order when writing LLVM assembly."),
60 cl::init(false), cl::Hidden);
63 MaterializeMetadata("materialize-metadata",
64 cl::desc("Load module without materializing metadata, "
65 "then materialize only the metadata"));
69 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
70 OS << DL.getLine() << ":" << DL.getCol();
71 if (DILocation *IDL = DL.getInlinedAt()) {
73 printDebugLoc(IDL, OS);
76 class CommentWriter : public AssemblyAnnotationWriter {
78 void emitFunctionAnnot(const Function *F,
79 formatted_raw_ostream &OS) override {
80 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
83 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
85 if (!V.getType()->isVoidTy()) {
88 // Output # uses and type
89 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
91 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
92 if (const DebugLoc &DL = I->getDebugLoc()) {
98 OS << " [debug line = ";
102 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
107 OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
109 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
114 OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
120 } // end anon namespace
122 static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
123 raw_ostream &OS = errs();
124 OS << (char *)Context << ": ";
125 switch (DI.getSeverity()) {
126 case DS_Error: OS << "error: "; break;
127 case DS_Warning: OS << "warning: "; break;
128 case DS_Remark: OS << "remark: "; break;
129 case DS_Note: OS << "note: "; break;
132 DiagnosticPrinterRawOStream DP(OS);
136 if (DI.getSeverity() == DS_Error)
140 static ExitOnError ExitOnErr;
142 static std::unique_ptr<Module> openInputFile(LLVMContext &Context) {
143 std::unique_ptr<MemoryBuffer> MB =
144 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
145 std::unique_ptr<Module> M =
146 ExitOnErr(getOwningLazyBitcodeModule(std::move(MB), Context,
147 /*ShouldLazyLoadMetadata=*/true));
148 if (MaterializeMetadata)
149 ExitOnErr(M->materializeMetadata());
151 ExitOnErr(M->materializeAll());
155 int main(int argc, char **argv) {
156 // Print a stack trace if we signal out.
157 sys::PrintStackTraceOnErrorSignal(argv[0]);
158 PrettyStackTraceProgram X(argc, argv);
160 ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
163 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
165 Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
167 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
169 std::unique_ptr<Module> M = openInputFile(Context);
171 // Just use stdout. We won't actually print anything on it.
173 OutputFilename = "-";
175 if (OutputFilename.empty()) { // Unspecified output, infer it.
176 if (InputFilename == "-") {
177 OutputFilename = "-";
179 StringRef IFN = InputFilename;
180 OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
181 OutputFilename += ".ll";
186 std::unique_ptr<tool_output_file> Out(
187 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
189 errs() << EC.message() << '\n';
193 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
195 Annotator.reset(new CommentWriter());
197 // All that llvm-dis does is write the assembly to a file.
199 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);