llvm-rs-as.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/IR/Verifier.h"
  19. #include "llvm/AsmParser/Parser.h"
  20. #include "llvm/Bitcode/ReaderWriter.h"
  21. #include "llvm/IR/Module.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 "slang_bitcode_gen.h"
  31. #include "slang_version.h"
  32. #include "StripUnkAttr/strip_unknown_attributes.h"
  33. #include <memory>
  34. using namespace llvm;
  35. static cl::opt<std::string>
  36. InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
  37. static cl::opt<std::string>
  38. OutputFilename("o", cl::desc("Override output filename"),
  39. cl::value_desc("filename"));
  40. static cl::opt<bool>
  41. Force("f", cl::desc("Enable binary output on terminals"));
  42. static cl::opt<bool>
  43. DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
  44. static cl::opt<uint32_t>
  45. TargetAPI("target-api", cl::desc("Specify RenderScript target API version "
  46. "(0 = development API) (default is 0)"),
  47. cl::init(0));
  48. static cl::opt<bool>
  49. DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
  50. static cl::opt<bool>
  51. DisableVerify("disable-verify", cl::Hidden,
  52. cl::desc("Do not run verifier on input LLVM (dangerous!)"));
  53. static void stripUnknownAttributes(llvm::Module *M) {
  54. for (llvm::Function &F : *M)
  55. slang::stripUnknownAttributes(F);
  56. }
  57. static void WriteOutputFile(const Module *M, uint32_t ModuleTargetAPI) {
  58. // Infer the output filename if needed.
  59. if (OutputFilename.empty()) {
  60. if (InputFilename == "-") {
  61. OutputFilename = "-";
  62. } else {
  63. std::string IFN = InputFilename;
  64. int Len = IFN.length();
  65. if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
  66. // Source ends in .ll
  67. OutputFilename = std::string(IFN.begin(), IFN.end()-3);
  68. } else {
  69. OutputFilename = IFN; // Append a .bc to it
  70. }
  71. OutputFilename += ".bc";
  72. }
  73. }
  74. std::error_code EC;
  75. std::unique_ptr<tool_output_file> Out
  76. (new tool_output_file(OutputFilename.c_str(), EC, llvm::sys::fs::F_None));
  77. if (EC) {
  78. // TODO(srhines): This isn't actually very specific and needs cleanup.
  79. errs() << EC.message() << '\n';
  80. exit(1);
  81. }
  82. if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
  83. slang::writeBitcode(Out->os(), *M,
  84. /* TargetAPI = */ ModuleTargetAPI,
  85. /* OptimizationLevel = */ 3,
  86. /* GenerateDebugInfo = */ false);
  87. if (!Out->os().has_error()) {
  88. // Declare success.
  89. Out->keep();
  90. }
  91. }
  92. }
  93. int main(int argc, char **argv) {
  94. // Print a stack trace if we signal out.
  95. sys::PrintStackTraceOnErrorSignal(argv[0]);
  96. PrettyStackTraceProgram X(argc, argv);
  97. LLVMContext Context;
  98. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  99. cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
  100. // Check target API.
  101. uint32_t ActualTargetAPI = (TargetAPI == 0) ? RS_DEVELOPMENT_API : TargetAPI;
  102. if (ActualTargetAPI != RS_DEVELOPMENT_API &&
  103. (ActualTargetAPI < SLANG_MINIMUM_TARGET_API ||
  104. ActualTargetAPI > SLANG_MAXIMUM_TARGET_API)) {
  105. errs() << "target API level '" << ActualTargetAPI << "' is out of range "
  106. << "('" << SLANG_MINIMUM_TARGET_API << "' - '"
  107. << SLANG_MAXIMUM_TARGET_API << "')\n";
  108. return 1;
  109. }
  110. // Parse the file now...
  111. SMDiagnostic Err;
  112. std::unique_ptr<Module> M(parseAssemblyFile(InputFilename, Err, Context));
  113. if (M.get() == 0) {
  114. Err.print(argv[0], errs());
  115. return 1;
  116. }
  117. if (!DisableVerify) {
  118. std::string Err;
  119. raw_string_ostream stream(Err);
  120. if (verifyModule(*M.get(), &stream)) {
  121. errs() << argv[0]
  122. << ": assembly parsed, but does not verify as correct!\n";
  123. errs() << Err;
  124. return 1;
  125. }
  126. }
  127. stripUnknownAttributes(M.get());
  128. if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
  129. if (!DisableOutput)
  130. WriteOutputFile(M.get(), ActualTargetAPI);
  131. return 0;
  132. }