llvm-as.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
  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-as --help - Output information about command line switches
  12. // llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout
  13. // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
  14. // to the x.bc file.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/IR/LLVMContext.h"
  18. #include "llvm/AsmParser/Parser.h"
  19. #include "llvm/Bitcode/ReaderWriter.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/Verifier.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/FileSystem.h"
  24. #include "llvm/Support/ManagedStatic.h"
  25. #include "llvm/Support/PrettyStackTrace.h"
  26. #include "llvm/Support/Signals.h"
  27. #include "llvm/Support/SourceMgr.h"
  28. #include "llvm/Support/SystemUtils.h"
  29. #include "llvm/Support/ToolOutputFile.h"
  30. #include <memory>
  31. using namespace llvm;
  32. static cl::opt<std::string>
  33. InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
  34. static cl::opt<std::string>
  35. OutputFilename("o", cl::desc("Override output filename"),
  36. cl::value_desc("filename"));
  37. static cl::opt<bool>
  38. Force("f", cl::desc("Enable binary output on terminals"));
  39. static cl::opt<bool>
  40. DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
  41. static cl::opt<bool>
  42. DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
  43. static cl::opt<bool>
  44. DisableVerify("disable-verify", cl::Hidden,
  45. cl::desc("Do not run verifier on input LLVM (dangerous!)"));
  46. static cl::opt<bool> PreserveBitcodeUseListOrder(
  47. "preserve-bc-uselistorder",
  48. cl::desc("Preserve use-list order when writing LLVM bitcode."),
  49. cl::init(true), cl::Hidden);
  50. static void WriteOutputFile(const Module *M) {
  51. // Infer the output filename if needed.
  52. if (OutputFilename.empty()) {
  53. if (InputFilename == "-") {
  54. OutputFilename = "-";
  55. } else {
  56. StringRef IFN = InputFilename;
  57. OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
  58. OutputFilename += ".bc";
  59. }
  60. }
  61. std::error_code EC;
  62. std::unique_ptr<tool_output_file> Out(
  63. new tool_output_file(OutputFilename, EC, sys::fs::F_None));
  64. if (EC) {
  65. errs() << EC.message() << '\n';
  66. exit(1);
  67. }
  68. if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
  69. WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder);
  70. // Declare success.
  71. Out->keep();
  72. }
  73. // HLSL Change Starts
  74. #define NOMINMAX
  75. #include <windows.h>
  76. #include "llvm/Support/FileSystem.h"
  77. #include "llvm/Support/MSFileSystem.h"
  78. // HLSL Change Ends
  79. // HLSL Change: changed calling convention to __cdecl
  80. int __cdecl main(int argc, char **argv) {
  81. // Print a stack trace if we signal out.
  82. // sys::PrintStackTraceOnErrorSignal(); // HLSL Change - disable this
  83. // PrettyStackTraceProgram X(argc, argv); // HLSL Change - disable this
  84. // HLSL Change Starts
  85. llvm::sys::fs::MSFileSystem* msfPtr;
  86. HRESULT hr;
  87. if (!SUCCEEDED(hr = CreateMSFileSystemForDisk(&msfPtr)))
  88. return 1;
  89. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
  90. llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  91. llvm::STDStreamCloser stdStreamCloser;
  92. // HLSL Change Ends
  93. LLVMContext &Context = getGlobalContext();
  94. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  95. cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
  96. // Parse the file now...
  97. SMDiagnostic Err;
  98. std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
  99. if (!M.get()) {
  100. Err.print(argv[0], errs());
  101. return 1;
  102. }
  103. if (!DisableVerify) {
  104. std::string ErrorStr;
  105. raw_string_ostream OS(ErrorStr);
  106. if (verifyModule(*M.get(), &OS)) {
  107. errs() << argv[0]
  108. << ": assembly parsed, but does not verify as correct!\n";
  109. errs() << OS.str();
  110. return 1;
  111. }
  112. }
  113. if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
  114. if (!DisableOutput)
  115. WriteOutputFile(M.get());
  116. return 0;
  117. }