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/ReaderWriter.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/DataStream.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);
64 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
65 OS << DL.getLine() << ":" << DL.getCol();
66 if (DILocation *IDL = DL.getInlinedAt()) {
68 printDebugLoc(IDL, OS);
71 class CommentWriter : public AssemblyAnnotationWriter {
73 void emitFunctionAnnot(const Function *F,
74 formatted_raw_ostream &OS) override {
75 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
78 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
80 if (!V.getType()->isVoidTy()) {
83 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type
85 if (const Instruction *I = dyn_cast<Instruction>(&V)) {
86 if (const DebugLoc &DL = I->getDebugLoc()) {
92 OS << " [debug line = ";
96 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
101 OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
103 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
108 OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
114 } // end anon namespace
116 static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
117 raw_ostream &OS = errs();
118 OS << (char *)Context << ": ";
119 switch (DI.getSeverity()) {
120 case DS_Error: OS << "error: "; break;
121 case DS_Warning: OS << "warning: "; break;
122 case DS_Remark: OS << "remark: "; break;
123 case DS_Note: OS << "note: "; break;
126 DiagnosticPrinterRawOStream DP(OS);
130 if (DI.getSeverity() == DS_Error)
134 int main(int argc, char **argv) {
135 // Print a stack trace if we signal out.
136 sys::PrintStackTraceOnErrorSignal();
137 PrettyStackTraceProgram X(argc, argv);
139 LLVMContext &Context = getGlobalContext();
140 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
142 Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
144 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
146 std::string ErrorMessage;
147 std::unique_ptr<Module> M;
149 // Use the bitcode streaming interface
150 DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
152 std::string DisplayFilename;
153 if (InputFilename == "-")
154 DisplayFilename = "<stdin>";
156 DisplayFilename = InputFilename;
157 ErrorOr<std::unique_ptr<Module>> MOrErr =
158 getStreamedBitcodeModule(DisplayFilename, Streamer, Context);
159 M = std::move(*MOrErr);
160 M->materializeAllPermanently();
163 // Just use stdout. We won't actually print anything on it.
165 OutputFilename = "-";
167 if (OutputFilename.empty()) { // Unspecified output, infer it.
168 if (InputFilename == "-") {
169 OutputFilename = "-";
171 const std::string &IFN = InputFilename;
172 int Len = IFN.length();
173 // If the source ends in .bc, strip it off.
174 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
175 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
177 OutputFilename = IFN+".ll";
182 std::unique_ptr<tool_output_file> Out(
183 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
185 errs() << EC.message() << '\n';
189 std::unique_ptr<AssemblyAnnotationWriter> Annotator;
191 Annotator.reset(new CommentWriter());
193 // All that llvm-dis does is write the assembly to a file.
195 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);