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