llvm-as.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/UseListOrder.h"
  22. #include "llvm/IR/Verifier.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/ManagedStatic.h"
  26. #include "llvm/Support/PrettyStackTrace.h"
  27. #include "llvm/Support/Signals.h"
  28. #include "llvm/Support/SourceMgr.h"
  29. #include "llvm/Support/SystemUtils.h"
  30. #include "llvm/Support/ToolOutputFile.h"
  31. #include <memory>
  32. using namespace llvm;
  33. static cl::opt<std::string>
  34. InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
  35. static cl::opt<std::string>
  36. OutputFilename("o", cl::desc("Override output filename"),
  37. cl::value_desc("filename"));
  38. static cl::opt<bool>
  39. Force("f", cl::desc("Enable binary output on terminals"));
  40. static cl::opt<bool>
  41. DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
  42. static cl::opt<bool>
  43. DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
  44. static cl::opt<bool>
  45. DisableVerify("disable-verify", cl::Hidden,
  46. cl::desc("Do not run verifier on input LLVM (dangerous!)"));
  47. static void WriteOutputFile(const Module *M) {
  48. // Infer the output filename if needed.
  49. if (OutputFilename.empty()) {
  50. if (InputFilename == "-") {
  51. OutputFilename = "-";
  52. } else {
  53. std::string IFN = InputFilename;
  54. int Len = IFN.length();
  55. if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
  56. // Source ends in .ll
  57. OutputFilename = std::string(IFN.begin(), IFN.end()-3);
  58. } else {
  59. OutputFilename = IFN; // Append a .bc to it
  60. }
  61. OutputFilename += ".bc";
  62. }
  63. }
  64. std::error_code EC;
  65. std::unique_ptr<tool_output_file> Out(
  66. new tool_output_file(OutputFilename, EC, sys::fs::F_None));
  67. if (EC) {
  68. errs() << EC.message() << '\n';
  69. exit(1);
  70. }
  71. if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
  72. WriteBitcodeToFile(M, Out->os());
  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();
  79. PrettyStackTraceProgram X(argc, argv);
  80. LLVMContext &Context = getGlobalContext();
  81. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  82. // Turn on -preserve-bc-uselistorder by default, but let the command-line
  83. // override it.
  84. setPreserveBitcodeUseListOrder(true);
  85. cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
  86. // Parse the file now...
  87. SMDiagnostic Err;
  88. std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
  89. if (!M.get()) {
  90. Err.print(argv[0], errs());
  91. return 1;
  92. }
  93. if (!DisableVerify) {
  94. std::string ErrorStr;
  95. raw_string_ostream OS(ErrorStr);
  96. if (verifyModule(*M.get(), &OS)) {
  97. errs() << argv[0]
  98. << ": assembly parsed, but does not verify as correct!\n";
  99. errs() << OS.str();
  100. return 1;
  101. }
  102. }
  103. if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
  104. if (!DisableOutput)
  105. WriteOutputFile(M.get());
  106. return 0;
  107. }