llvm-dis.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/Bitcode/BitcodeReader.h"
  20. #include "llvm/IR/AssemblyAnnotationWriter.h"
  21. #include "llvm/IR/DebugInfo.h"
  22. #include "llvm/IR/DiagnosticInfo.h"
  23. #include "llvm/IR/DiagnosticPrinter.h"
  24. #include "llvm/IR/IntrinsicInst.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/ManagedStatic.h"
  32. #include "llvm/Support/MemoryBuffer.h"
  33. #include "llvm/Support/PrettyStackTrace.h"
  34. #include "llvm/Support/Signals.h"
  35. #include "llvm/Support/ToolOutputFile.h"
  36. #include <system_error>
  37. using namespace llvm;
  38. static cl::opt<std::string>
  39. InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
  40. static cl::opt<std::string>
  41. OutputFilename("o", cl::desc("Override output filename"),
  42. cl::value_desc("filename"));
  43. static cl::opt<bool>
  44. Force("f", cl::desc("Enable binary output on terminals"));
  45. static cl::opt<bool>
  46. DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
  47. static cl::opt<bool>
  48. SetImporting("set-importing",
  49. cl::desc("Set lazy loading to pretend to import a module"),
  50. cl::Hidden);
  51. static cl::opt<bool>
  52. ShowAnnotations("show-annotations",
  53. cl::desc("Add informational comments to the .ll file"));
  54. static cl::opt<bool> PreserveAssemblyUseListOrder(
  55. "preserve-ll-uselistorder",
  56. cl::desc("Preserve use-list order when writing LLVM assembly."),
  57. cl::init(false), cl::Hidden);
  58. static cl::opt<bool>
  59. MaterializeMetadata("materialize-metadata",
  60. cl::desc("Load module without materializing metadata, "
  61. "then materialize only the metadata"));
  62. namespace {
  63. static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
  64. OS << DL.getLine() << ":" << DL.getCol();
  65. if (DILocation *IDL = DL.getInlinedAt()) {
  66. OS << "@";
  67. printDebugLoc(IDL, OS);
  68. }
  69. }
  70. class CommentWriter : public AssemblyAnnotationWriter {
  71. public:
  72. void emitFunctionAnnot(const Function *F,
  73. formatted_raw_ostream &OS) override {
  74. OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
  75. OS << '\n';
  76. }
  77. void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
  78. bool Padded = false;
  79. if (!V.getType()->isVoidTy()) {
  80. OS.PadToColumn(50);
  81. Padded = true;
  82. // Output # uses and type
  83. OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
  84. }
  85. if (const Instruction *I = dyn_cast<Instruction>(&V)) {
  86. if (const DebugLoc &DL = I->getDebugLoc()) {
  87. if (!Padded) {
  88. OS.PadToColumn(50);
  89. Padded = true;
  90. OS << ";";
  91. }
  92. OS << " [debug line = ";
  93. printDebugLoc(DL,OS);
  94. OS << "]";
  95. }
  96. if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
  97. if (!Padded) {
  98. OS.PadToColumn(50);
  99. OS << ";";
  100. }
  101. OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
  102. }
  103. else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
  104. if (!Padded) {
  105. OS.PadToColumn(50);
  106. OS << ";";
  107. }
  108. OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
  109. }
  110. }
  111. }
  112. };
  113. struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
  114. char *Prefix;
  115. LLVMDisDiagnosticHandler(char *PrefixPtr) : Prefix(PrefixPtr) {}
  116. bool handleDiagnostics(const DiagnosticInfo &DI) override {
  117. raw_ostream &OS = errs();
  118. OS << Prefix << ": ";
  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;
  124. }
  125. DiagnosticPrinterRawOStream DP(OS);
  126. DI.print(DP);
  127. OS << '\n';
  128. if (DI.getSeverity() == DS_Error)
  129. exit(1);
  130. return true;
  131. }
  132. };
  133. } // end anon namespace
  134. static ExitOnError ExitOnErr;
  135. static std::unique_ptr<Module> openInputFile(LLVMContext &Context) {
  136. std::unique_ptr<MemoryBuffer> MB =
  137. ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
  138. std::unique_ptr<Module> M = ExitOnErr(getOwningLazyBitcodeModule(
  139. std::move(MB), Context,
  140. /*ShouldLazyLoadMetadata=*/true, SetImporting));
  141. if (MaterializeMetadata)
  142. ExitOnErr(M->materializeMetadata());
  143. else
  144. ExitOnErr(M->materializeAll());
  145. return M;
  146. }
  147. int main(int argc, char **argv) {
  148. // Print a stack trace if we signal out.
  149. sys::PrintStackTraceOnErrorSignal(argv[0]);
  150. PrettyStackTraceProgram X(argc, argv);
  151. ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
  152. LLVMContext Context;
  153. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  154. Context.setDiagnosticHandler(
  155. llvm::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
  156. cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
  157. std::unique_ptr<Module> M = openInputFile(Context);
  158. // Just use stdout. We won't actually print anything on it.
  159. if (DontPrint)
  160. OutputFilename = "-";
  161. if (OutputFilename.empty()) { // Unspecified output, infer it.
  162. if (InputFilename == "-") {
  163. OutputFilename = "-";
  164. } else {
  165. StringRef IFN = InputFilename;
  166. OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
  167. OutputFilename += ".ll";
  168. }
  169. }
  170. std::error_code EC;
  171. std::unique_ptr<ToolOutputFile> Out(
  172. new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
  173. if (EC) {
  174. errs() << EC.message() << '\n';
  175. return 1;
  176. }
  177. std::unique_ptr<AssemblyAnnotationWriter> Annotator;
  178. if (ShowAnnotations)
  179. Annotator.reset(new CommentWriter());
  180. // All that llvm-dis does is write the assembly to a file.
  181. if (!DontPrint)
  182. M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
  183. // Declare success.
  184. Out->keep();
  185. return 0;
  186. }