1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 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 12 // to the x.ll file. 13 // Options: 14 // --help - Output information about command line switches 15 // 16 //===----------------------------------------------------------------------===// 17 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> 36 using namespace llvm; 37 38 static cl::opt<std::string> 39 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); 40 41 static cl::opt<std::string> 42 OutputFilename("o", cl::desc("Override output filename"), 43 cl::value_desc("filename")); 44 45 static cl::opt<bool> 46 Force("f", cl::desc("Enable binary output on terminals")); 47 48 static cl::opt<bool> 49 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); 50 51 static cl::opt<bool> 52 SetImporting("set-importing", 53 cl::desc("Set lazy loading to pretend to import a module"), 54 cl::Hidden); 55 56 static cl::opt<bool> 57 ShowAnnotations("show-annotations", 58 cl::desc("Add informational comments to the .ll file")); 59 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); 64 65 static cl::opt<bool> 66 MaterializeMetadata("materialize-metadata", 67 cl::desc("Load module without materializing metadata, " 68 "then materialize only the metadata")); 69 70 namespace { 71 72 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) { 73 OS << DL.getLine() << ":" << DL.getCol(); 74 if (DILocation *IDL = DL.getInlinedAt()) { 75 OS << "@"; 76 printDebugLoc(IDL, OS); 77 } 78 } 79 class CommentWriter : public AssemblyAnnotationWriter { 80 public: 81 void emitFunctionAnnot(const Function *F, 82 formatted_raw_ostream &OS) override { 83 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses 84 OS << '\n'; 85 } 86 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { 87 bool Padded = false; 88 if (!V.getType()->isVoidTy()) { 89 OS.PadToColumn(50); 90 Padded = true; 91 // Output # uses and type 92 OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; 93 } 94 if (const Instruction *I = dyn_cast<Instruction>(&V)) { 95 if (const DebugLoc &DL = I->getDebugLoc()) { 96 if (!Padded) { 97 OS.PadToColumn(50); 98 Padded = true; 99 OS << ";"; 100 } 101 OS << " [debug line = "; 102 printDebugLoc(DL,OS); 103 OS << "]"; 104 } 105 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { 106 if (!Padded) { 107 OS.PadToColumn(50); 108 OS << ";"; 109 } 110 OS << " [debug variable = " << DDI->getVariable()->getName() << "]"; 111 } 112 else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { 113 if (!Padded) { 114 OS.PadToColumn(50); 115 OS << ";"; 116 } 117 OS << " [debug variable = " << DVI->getVariable()->getName() << "]"; 118 } 119 } 120 } 121 }; 122 123 struct LLVMDisDiagnosticHandler : public DiagnosticHandler { 124 char *Prefix; 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; 134 } 135 136 DiagnosticPrinterRawOStream DP(OS); 137 DI.print(DP); 138 OS << '\n'; 139 140 if (DI.getSeverity() == DS_Error) 141 exit(1); 142 return true; 143 } 144 }; 145 } // end anon namespace 146 147 static ExitOnError ExitOnErr; 148 149 int main(int argc, char **argv) { 150 InitLLVM X(argc, argv); 151 152 ExitOnErr.setBanner(std::string(argv[0]) + ": error: "); 153 154 LLVMContext Context; 155 Context.setDiagnosticHandler( 156 llvm::make_unique<LLVMDisDiagnosticHandler>(argv[0])); 157 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); 158 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()); 165 else 166 ExitOnErr(M->materializeAll()); 167 168 BitcodeLTOInfo LTOInfo = ExitOnErr(getBitcodeLTOInfo(*MB)); 169 std::unique_ptr<ModuleSummaryIndex> Index; 170 if (LTOInfo.HasSummary) 171 Index = ExitOnErr(getModuleSummaryIndex(*MB)); 172 173 // Just use stdout. We won't actually print anything on it. 174 if (DontPrint) 175 OutputFilename = "-"; 176 177 if (OutputFilename.empty()) { // Unspecified output, infer it. 178 if (InputFilename == "-") { 179 OutputFilename = "-"; 180 } else { 181 StringRef IFN = InputFilename; 182 OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str(); 183 OutputFilename += ".ll"; 184 } 185 } 186 187 std::error_code EC; 188 std::unique_ptr<ToolOutputFile> Out( 189 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); 190 if (EC) { 191 errs() << EC.message() << '\n'; 192 return 1; 193 } 194 195 std::unique_ptr<AssemblyAnnotationWriter> Annotator; 196 if (ShowAnnotations) 197 Annotator.reset(new CommentWriter()); 198 199 // All that llvm-dis does is write the assembly to a file. 200 if (!DontPrint) { 201 M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder); 202 if (Index) 203 Index->print(Out->os()); 204 } 205 206 // Declare success. 207 Out->keep(); 208 209 return 0; 210 } 211