llvm-dis.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/ReaderWriter.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/DataStream.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. ShowAnnotations("show-annotations",
  49. cl::desc("Add informational comments to the .ll file"));
  50. static cl::opt<bool> PreserveAssemblyUseListOrder(
  51. "preserve-ll-uselistorder",
  52. cl::desc("Preserve use-list order when writing LLVM assembly."),
  53. cl::init(false), cl::Hidden);
  54. namespace {
  55. static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
  56. OS << DL.getLine() << ":" << DL.getCol();
  57. if (DILocation *IDL = DL.getInlinedAt()) {
  58. OS << "@";
  59. printDebugLoc(IDL, OS);
  60. }
  61. }
  62. class CommentWriter : public AssemblyAnnotationWriter {
  63. public:
  64. void emitFunctionAnnot(const Function *F,
  65. formatted_raw_ostream &OS) override {
  66. OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
  67. OS << '\n';
  68. }
  69. void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
  70. bool Padded = false;
  71. if (!V.getType()->isVoidTy()) {
  72. OS.PadToColumn(50);
  73. Padded = true;
  74. // Output # uses and type
  75. OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
  76. }
  77. if (const Instruction *I = dyn_cast<Instruction>(&V)) {
  78. if (const DebugLoc &DL = I->getDebugLoc()) {
  79. if (!Padded) {
  80. OS.PadToColumn(50);
  81. Padded = true;
  82. OS << ";";
  83. }
  84. OS << " [debug line = ";
  85. printDebugLoc(DL,OS);
  86. OS << "]";
  87. }
  88. if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
  89. if (!Padded) {
  90. OS.PadToColumn(50);
  91. OS << ";";
  92. }
  93. OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
  94. }
  95. else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
  96. if (!Padded) {
  97. OS.PadToColumn(50);
  98. OS << ";";
  99. }
  100. OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
  101. }
  102. }
  103. }
  104. };
  105. } // end anon namespace
  106. static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
  107. raw_ostream &OS = errs();
  108. OS << (char *)Context << ": ";
  109. switch (DI.getSeverity()) {
  110. case DS_Error: OS << "error: "; break;
  111. case DS_Warning: OS << "warning: "; break;
  112. case DS_Remark: OS << "remark: "; break;
  113. case DS_Note: OS << "note: "; break;
  114. }
  115. DiagnosticPrinterRawOStream DP(OS);
  116. DI.print(DP);
  117. OS << '\n';
  118. if (DI.getSeverity() == DS_Error)
  119. exit(1);
  120. }
  121. // HLSL Change Starts
  122. #define NOMINMAX
  123. #include <windows.h>
  124. #include "llvm/Support/FileSystem.h"
  125. #include "llvm/Support/MSFileSystem.h"
  126. // HLSL Change Ends
  127. int __cdecl main(int argc, char **argv) { // HLSL Change - __cdecl
  128. // Print a stack trace if we signal out.
  129. // sys::PrintStackTraceOnErrorSignal(); // HLSL Change - disable this
  130. // PrettyStackTraceProgram X(argc, argv); // HLSL Change - disable this
  131. // HLSL Change Starts
  132. llvm::sys::fs::MSFileSystem* msfPtr;
  133. HRESULT hr;
  134. if (!SUCCEEDED(hr = CreateMSFileSystemForDisk(&msfPtr)))
  135. return 1;
  136. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
  137. llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  138. llvm::STDStreamCloser stdStreamCloser;
  139. // HLSL Change Ends
  140. LLVMContext &Context = getGlobalContext();
  141. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  142. Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
  143. cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
  144. std::string ErrorMessage;
  145. std::unique_ptr<Module> M;
  146. // Use the bitcode streaming interface
  147. std::unique_ptr<DataStreamer> Streamer =
  148. getDataFileStreamer(InputFilename, &ErrorMessage);
  149. if (Streamer) {
  150. std::string DisplayFilename;
  151. if (InputFilename == "-")
  152. DisplayFilename = "<stdin>";
  153. else
  154. DisplayFilename = InputFilename;
  155. ErrorOr<std::unique_ptr<Module>> MOrErr =
  156. getStreamedBitcodeModule(DisplayFilename, std::move(Streamer), Context);
  157. M = std::move(*MOrErr);
  158. M->materializeAllPermanently();
  159. } else {
  160. errs() << argv[0] << ": " << ErrorMessage << '\n';
  161. return 1;
  162. }
  163. // Just use stdout. We won't actually print anything on it.
  164. if (DontPrint)
  165. OutputFilename = "-";
  166. if (OutputFilename.empty()) { // Unspecified output, infer it.
  167. if (InputFilename == "-") {
  168. OutputFilename = "-";
  169. } else {
  170. StringRef IFN = InputFilename;
  171. OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
  172. OutputFilename += ".ll";
  173. }
  174. }
  175. std::error_code EC;
  176. std::unique_ptr<tool_output_file> Out(
  177. new tool_output_file(OutputFilename, EC, sys::fs::F_None));
  178. if (EC) {
  179. errs() << EC.message() << '\n';
  180. return 1;
  181. }
  182. std::unique_ptr<AssemblyAnnotationWriter> Annotator;
  183. if (ShowAnnotations)
  184. Annotator.reset(new CommentWriter());
  185. // All that llvm-dis does is write the assembly to a file.
  186. if (!DontPrint)
  187. M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
  188. // Declare success.
  189. Out->keep();
  190. return 0;
  191. }