llvm-as.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by the LLVM research group and is distributed under
  6. // the University of Illinois Open Source 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 bytecode to stdout
  13. // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode
  14. // to the x.bc file.
  15. //
  16. //===------------------------------------------------------------------------===
  17. #include "llvm/Module.h"
  18. #include "llvm/Assembly/Parser.h"
  19. #include "llvm/Bytecode/Writer.h"
  20. #include "llvm/Analysis/Verifier.h"
  21. #include "Support/CommandLine.h"
  22. #include "llvm/System/Signals.h"
  23. #include <fstream>
  24. #include <iostream>
  25. #include <memory>
  26. using namespace llvm;
  27. static cl::opt<std::string>
  28. InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
  29. static cl::opt<std::string>
  30. OutputFilename("o", cl::desc("Override output filename"),
  31. cl::value_desc("filename"));
  32. static cl::opt<bool>
  33. Force("f", cl::desc("Overwrite output files"));
  34. static cl::opt<bool>
  35. DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
  36. static cl::opt<bool>
  37. DisableVerify("disable-verify", cl::Hidden,
  38. cl::desc("Do not run verifier on input LLVM (dangerous!"));
  39. int main(int argc, char **argv) {
  40. cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
  41. PrintStackTraceOnErrorSignal();
  42. std::ostream *Out = 0;
  43. try {
  44. // Parse the file now...
  45. std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
  46. if (M.get() == 0) {
  47. std::cerr << argv[0] << ": assembly didn't read correctly.\n";
  48. return 1;
  49. }
  50. if (!DisableVerify && verifyModule(*M.get())) {
  51. std::cerr << argv[0]
  52. << ": assembly parsed, but does not verify as correct!\n";
  53. return 1;
  54. }
  55. if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
  56. if (OutputFilename != "") { // Specified an output filename?
  57. if (OutputFilename != "-") { // Not stdout?
  58. if (!Force && std::ifstream(OutputFilename.c_str())) {
  59. // If force is not specified, make sure not to overwrite a file!
  60. std::cerr << argv[0] << ": error opening '" << OutputFilename
  61. << "': file exists!\n"
  62. << "Use -f command line argument to force output\n";
  63. return 1;
  64. }
  65. Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
  66. std::ios_base::trunc | std::ios_base::binary);
  67. } else { // Specified stdout
  68. Out = &std::cout;
  69. }
  70. } else {
  71. if (InputFilename == "-") {
  72. OutputFilename = "-";
  73. Out = &std::cout;
  74. } else {
  75. std::string IFN = InputFilename;
  76. int Len = IFN.length();
  77. if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
  78. // Source ends in .ll
  79. OutputFilename = std::string(IFN.begin(), IFN.end()-3);
  80. } else {
  81. OutputFilename = IFN; // Append a .bc to it
  82. }
  83. OutputFilename += ".bc";
  84. if (!Force && std::ifstream(OutputFilename.c_str())) {
  85. // If force is not specified, make sure not to overwrite a file!
  86. std::cerr << argv[0] << ": error opening '" << OutputFilename
  87. << "': file exists!\n"
  88. << "Use -f command line argument to force output\n";
  89. return 1;
  90. }
  91. Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out |
  92. std::ios_base::trunc | std::ios_base::binary);
  93. // Make sure that the Out file gets unlinked from the disk if we get a
  94. // SIGINT
  95. RemoveFileOnSignal(OutputFilename);
  96. }
  97. }
  98. if (!Out->good()) {
  99. std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
  100. return 1;
  101. }
  102. WriteBytecodeToFile(M.get(), *Out);
  103. } catch (const ParseException &E) {
  104. std::cerr << argv[0] << ": " << E.getMessage() << "\n";
  105. return 1;
  106. }
  107. if (Out != &std::cout) delete Out;
  108. return 0;
  109. }