llvm-as.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/AsmParser/Parser.h"
  18. #include "llvm/Bitcode/BitcodeWriter.h"
  19. #include "llvm/IR/LLVMContext.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> InputFilename(cl::Positional,
  33. cl::desc("<input .llvm file>"),
  34. cl::init("-"));
  35. static cl::opt<std::string> OutputFilename("o",
  36. cl::desc("Override output filename"),
  37. cl::value_desc("filename"));
  38. static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"));
  39. static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),
  40. cl::init(false));
  41. static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
  42. cl::init(false));
  43. static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
  44. cl::Hidden);
  45. static cl::opt<bool>
  46. DisableVerify("disable-verify", cl::Hidden,
  47. cl::desc("Do not run verifier on input LLVM (dangerous!)"));
  48. static cl::opt<bool> PreserveBitcodeUseListOrder(
  49. "preserve-bc-uselistorder",
  50. cl::desc("Preserve use-list order when writing LLVM bitcode."),
  51. cl::init(true), cl::Hidden);
  52. static void WriteOutputFile(const Module *M) {
  53. // Infer the output filename if needed.
  54. if (OutputFilename.empty()) {
  55. if (InputFilename == "-") {
  56. OutputFilename = "-";
  57. } else {
  58. StringRef IFN = InputFilename;
  59. OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
  60. OutputFilename += ".bc";
  61. }
  62. }
  63. std::error_code EC;
  64. std::unique_ptr<ToolOutputFile> Out(
  65. new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
  66. if (EC) {
  67. errs() << EC.message() << '\n';
  68. exit(1);
  69. }
  70. if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
  71. WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder, nullptr,
  72. EmitModuleHash);
  73. // Declare success.
  74. Out->keep();
  75. }
  76. int main(int argc, char **argv) {
  77. // Print a stack trace if we signal out.
  78. sys::PrintStackTraceOnErrorSignal(argv[0]);
  79. PrettyStackTraceProgram X(argc, argv);
  80. LLVMContext Context;
  81. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  82. cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
  83. // Parse the file now...
  84. SMDiagnostic Err;
  85. std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
  86. if (!M.get()) {
  87. Err.print(argv[0], errs());
  88. return 1;
  89. }
  90. if (!DisableVerify) {
  91. std::string ErrorStr;
  92. raw_string_ostream OS(ErrorStr);
  93. if (verifyModule(*M.get(), &OS)) {
  94. errs() << argv[0]
  95. << ": assembly parsed, but does not verify as correct!\n";
  96. errs() << OS.str();
  97. return 1;
  98. }
  99. }
  100. if (DumpAsm)
  101. errs() << "Here's the assembly:\n" << *M.get();
  102. if (!DisableOutput)
  103. WriteOutputFile(M.get());
  104. return 0;
  105. }