1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 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 13 // to the x.ll file. 14 // Options: 15 // --help - Output information about command line switches 16 // 17 //===----------------------------------------------------------------------===// 18 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> 38 using namespace llvm; 39 40 static cl::opt<std::string> 41 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); 42 43 static cl::opt<std::string> 44 OutputFilename("o", cl::desc("Override output filename"), 45 cl::value_desc("filename")); 46 47 static cl::opt<bool> 48 Force("f", cl::desc("Enable binary output on terminals")); 49 50 static cl::opt<bool> 51 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); 52 53 static cl::opt<bool> 54 ShowAnnotations("show-annotations", 55 cl::desc("Add informational comments to the .ll file")); 56 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); 61 62 static cl::opt<bool> 63 MaterializeMetadata("materialize-metadata", 64 cl::desc("Load module without materializing metadata, " 65 "then materialize only the metadata")); 66 67 namespace { 68 69 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) { 70 OS << DL.getLine() << ":" << DL.getCol(); 71 if (DILocation *IDL = DL.getInlinedAt()) { 72 OS << "@"; 73 printDebugLoc(IDL, OS); 74 } 75 } 76 class CommentWriter : public AssemblyAnnotationWriter { 77 public: 78 void emitFunctionAnnot(const Function *F, 79 formatted_raw_ostream &OS) override { 80 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses 81 OS << '\n'; 82 } 83 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { 84 bool Padded = false; 85 if (!V.getType()->isVoidTy()) { 86 OS.PadToColumn(50); 87 Padded = true; 88 // Output # uses and type 89 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; 90 } 91 if (const Instruction *I = dyn_cast<Instruction>(&V)) { 92 if (const DebugLoc &DL = I->getDebugLoc()) { 93 if (!Padded) { 94 OS.PadToColumn(50); 95 Padded = true; 96 OS << ";"; 97 } 98 OS << " [debug line = "; 99 printDebugLoc(DL,OS); 100 OS << "]"; 101 } 102 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { 103 if (!Padded) { 104 OS.PadToColumn(50); 105 OS << ";"; 106 } 107 OS << " [debug variable = " << DDI->getVariable()->getName() << "]"; 108 } 109 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { 110 if (!Padded) { 111 OS.PadToColumn(50); 112 OS << ";"; 113 } 114 OS << " [debug variable = " << DVI->getVariable()->getName() << "]"; 115 } 116 } 117 } 118 }; 119 120 } // end anon namespace 121 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; 130 } 131 132 DiagnosticPrinterRawOStream DP(OS); 133 DI.print(DP); 134 OS << '\n'; 135 136 if (DI.getSeverity() == DS_Error) 137 exit(1); 138 } 139 140 static ExitOnError ExitOnErr; 141 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()); 150 else 151 ExitOnErr(M->materializeAll()); 152 return M; 153 } 154 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); 159 160 ExitOnErr.setBanner(std::string(argv[0]) + ": error: "); 161 162 LLVMContext Context; 163 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 164 165 Context.setDiagnosticHandler(diagnosticHandler, argv[0]); 166 167 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); 168 169 std::unique_ptr<Module> M = openInputFile(Context); 170 171 // Just use stdout. We won't actually print anything on it. 172 if (DontPrint) 173 OutputFilename = "-"; 174 175 if (OutputFilename.empty()) { // Unspecified output, infer it. 176 if (InputFilename == "-") { 177 OutputFilename = "-"; 178 } else { 179 StringRef IFN = InputFilename; 180 OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str(); 181 OutputFilename += ".ll"; 182 } 183 } 184 185 std::error_code EC; 186 std::unique_ptr<tool_output_file> Out( 187 new tool_output_file(OutputFilename, EC, sys::fs::F_None)); 188 if (EC) { 189 errs() << EC.message() << '\n'; 190 return 1; 191 } 192 193 std::unique_ptr<AssemblyAnnotationWriter> Annotator; 194 if (ShowAnnotations) 195 Annotator.reset(new CommentWriter()); 196 197 // All that llvm-dis does is write the assembly to a file. 198 if (!DontPrint) 199 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder); 200 201 // Declare success. 202 Out->keep(); 203 204 return 0; 205 } 206