Main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
  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. // TableGen is a tool which can be used to build up a description of something,
  11. // then invoke one or more "tablegen backends" to emit information about the
  12. // description in some predefined format. In practice, this is used by the LLVM
  13. // code generators to automate generation of a code generator through a
  14. // high-level description of the target.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/TableGen/Main.h"
  18. #include "TGParser.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/FileSystem.h"
  22. #include "llvm/Support/MemoryBuffer.h"
  23. #include "llvm/Support/ToolOutputFile.h"
  24. #include "llvm/TableGen/Error.h"
  25. #include "llvm/TableGen/Record.h"
  26. #include <algorithm>
  27. #include <cstdio>
  28. #include <system_error>
  29. using namespace llvm;
  30. static cl::opt<std::string>
  31. OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
  32. cl::init("-"));
  33. static cl::opt<std::string>
  34. DependFilename("d",
  35. cl::desc("Dependency filename"),
  36. cl::value_desc("filename"),
  37. cl::init(""));
  38. static cl::opt<std::string>
  39. InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
  40. static cl::list<std::string>
  41. IncludeDirs("I", cl::desc("Directory of include files"),
  42. cl::value_desc("directory"), cl::Prefix);
  43. static int reportError(const char *ProgName, Twine Msg) {
  44. errs() << ProgName << ": " << Msg;
  45. errs().flush();
  46. return 1;
  47. }
  48. /// \brief Create a dependency file for `-d` option.
  49. ///
  50. /// This functionality is really only for the benefit of the build system.
  51. /// It is similar to GCC's `-M*` family of options.
  52. static int createDependencyFile(const TGParser &Parser, const char *argv0) {
  53. if (OutputFilename == "-")
  54. return reportError(argv0, "the option -d must be used together with -o\n");
  55. std::error_code EC;
  56. ToolOutputFile DepOut(DependFilename, EC, sys::fs::F_Text);
  57. if (EC)
  58. return reportError(argv0, "error opening " + DependFilename + ":" +
  59. EC.message() + "\n");
  60. DepOut.os() << OutputFilename << ":";
  61. for (const auto &Dep : Parser.getDependencies()) {
  62. DepOut.os() << ' ' << Dep.first;
  63. }
  64. DepOut.os() << "\n";
  65. DepOut.keep();
  66. return 0;
  67. }
  68. int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
  69. RecordKeeper Records;
  70. // Parse the input file.
  71. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  72. MemoryBuffer::getFileOrSTDIN(InputFilename);
  73. if (std::error_code EC = FileOrErr.getError())
  74. return reportError(argv0, "Could not open input file '" + InputFilename +
  75. "': " + EC.message() + "\n");
  76. // Tell SrcMgr about this buffer, which is what TGParser will pick up.
  77. SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
  78. // Record the location of the include directory so that the lexer can find
  79. // it later.
  80. SrcMgr.setIncludeDirs(IncludeDirs);
  81. TGParser Parser(SrcMgr, Records);
  82. if (Parser.ParseFile())
  83. return 1;
  84. std::error_code EC;
  85. ToolOutputFile Out(OutputFilename, EC, sys::fs::F_Text);
  86. if (EC)
  87. return reportError(argv0, "error opening " + OutputFilename + ":" +
  88. EC.message() + "\n");
  89. if (!DependFilename.empty()) {
  90. if (int Ret = createDependencyFile(Parser, argv0))
  91. return Ret;
  92. }
  93. if (MainFn(Out.os(), Records))
  94. return 1;
  95. if (ErrorsPrinted > 0)
  96. return reportError(argv0, utostr(ErrorsPrinted) + " errors.\n");
  97. // Declare success.
  98. Out.keep();
  99. return 0;
  100. }