1//===- Main.cpp - Top-Level TableGen implementation -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// TableGen is a tool which can be used to build up a description of something,
10// then invoke one or more "tablegen backends" to emit information about the
11// description in some predefined format. In practice, this is used by the LLVM
12// code generators to automate generation of a code generator through a
13// high-level description of the target.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/TableGen/Main.h"
18#include "TGLexer.h"
19#include "TGParser.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/ErrorOr.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/SMLoc.h"
28#include "llvm/Support/SourceMgr.h"
29#include "llvm/Support/ToolOutputFile.h"
30#include "llvm/Support/VirtualFileSystem.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/TableGen/Error.h"
33#include "llvm/TableGen/Record.h"
34#include "llvm/TableGen/TGTimer.h"
35#include "llvm/TableGen/TableGenBackend.h"
36#include <memory>
37#include <string>
38#include <system_error>
39#include <utility>
40using namespace llvm;
41
42static cl::opt<std::string>
43OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
44 cl::init(Val: "-"));
45
46static cl::opt<std::string>
47DependFilename("d",
48 cl::desc("Dependency filename"),
49 cl::value_desc("filename"),
50 cl::init(Val: ""));
51
52static cl::opt<std::string>
53InputFilename(cl::Positional, cl::desc("<input file>"), cl::init(Val: "-"));
54
55static cl::list<std::string>
56IncludeDirs("I", cl::desc("Directory of include files"),
57 cl::value_desc("directory"), cl::Prefix);
58
59static cl::list<std::string>
60MacroNames("D", cl::desc("Name of the macro to be defined"),
61 cl::value_desc("macro name"), cl::Prefix);
62
63static cl::opt<bool>
64WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed"));
65
66static cl::opt<bool>
67TimePhases("time-phases", cl::desc("Time phases of parser and backend"));
68
69cl::opt<bool> llvm::EmitLongStrLiterals(
70 "long-string-literals",
71 cl::desc("when emitting large string tables, prefer string literals over "
72 "comma-separated char literals. This can be a readability and "
73 "compile-time performance win, but upsets some compilers"),
74 cl::Hidden, cl::init(Val: true));
75
76static cl::opt<bool> NoWarnOnUnusedTemplateArgs(
77 "no-warn-on-unused-template-args",
78 cl::desc("Disable unused template argument warnings."));
79
80static int reportError(const char *ProgName, Twine Msg) {
81 errs() << ProgName << ": " << Msg;
82 errs().flush();
83 return 1;
84}
85
86/// Create a dependency file for `-d` option.
87///
88/// This functionality is really only for the benefit of the build system.
89/// It is similar to GCC's `-M*` family of options.
90static int createDependencyFile(const TGParser &Parser, const char *argv0) {
91 if (OutputFilename == "-")
92 return reportError(ProgName: argv0, Msg: "the option -d must be used together with -o\n");
93
94 std::error_code EC;
95 ToolOutputFile DepOut(DependFilename, EC, sys::fs::OF_Text);
96 if (EC)
97 return reportError(ProgName: argv0, Msg: "error opening " + DependFilename + ":" +
98 EC.message() + "\n");
99 DepOut.os() << OutputFilename << ":";
100
101 // Emit the primary input file as a dependency. This matches C compilers like
102 // Clang and GCC. Without it, a .td file with no `include` directives would
103 // produce a depfile listing zero dependencies. CMake's
104 // `cmake_transform_depfile` then collapses that to a 0-byte file, which Ninja
105 // treats as a missing depfile and re-runs the rule on every incremental
106 // build.
107 if (InputFilename != "-")
108 DepOut.os() << ' ' << InputFilename;
109
110 for (const auto &Dep : Parser.getDependencies()) {
111 DepOut.os() << ' ' << Dep;
112 }
113 DepOut.os() << "\n";
114 DepOut.keep();
115 return 0;
116}
117
118static int WriteOutput(const char *argv0, StringRef Filename,
119 StringRef Content) {
120 if (WriteIfChanged) {
121 // Only updates the real output file if there are any differences.
122 // This prevents recompilation of all the files depending on it if there
123 // aren't any.
124 if (auto ExistingOrErr = MemoryBuffer::getFile(Filename, /*IsText=*/true))
125 if (std::move(ExistingOrErr.get())->getBuffer() == Content)
126 return 0;
127 }
128 std::error_code EC;
129 ToolOutputFile OutFile(Filename, EC, sys::fs::OF_Text);
130 if (EC)
131 return reportError(ProgName: argv0, Msg: "error opening " + Filename + ": " +
132 EC.message() + "\n");
133 OutFile.os() << Content;
134 if (ErrorsPrinted == 0)
135 OutFile.keep();
136
137 return 0;
138}
139
140int llvm::TableGenMain(const char *argv0, MultiFileTableGenMainFn MainFn) {
141 RecordKeeper Records;
142 TGTimer &Timer = Records.getTimer();
143
144 if (TimePhases)
145 Timer.startPhaseTiming();
146
147 // Parse the input file.
148
149 Timer.startTimer(Name: "Parse, build records");
150 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
151 MemoryBuffer::getFileOrSTDIN(Filename: InputFilename, /*IsText=*/true);
152 if (std::error_code EC = FileOrErr.getError())
153 return reportError(ProgName: argv0, Msg: "Could not open input file '" + InputFilename +
154 "': " + EC.message() + "\n");
155
156 Records.saveInputFilename(Filename: InputFilename);
157
158 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
159 SrcMgr.AddNewSourceBuffer(F: std::move(*FileOrErr), IncludeLoc: SMLoc());
160
161 // Record the location of the include directory so that the lexer can find
162 // it later.
163 SrcMgr.setIncludeDirs(IncludeDirs);
164 SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
165
166 TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs);
167
168 if (Parser.ParseFile())
169 return 1;
170 Timer.stopTimer();
171
172 // Return early if any other errors were generated during parsing
173 // (e.g., assert failures).
174 if (ErrorsPrinted > 0)
175 return reportError(ProgName: argv0, Msg: Twine(ErrorsPrinted) + " errors.\n");
176
177 // Write output to memory.
178 Timer.startBackendTimer(Name: "Backend overall");
179 TableGenOutputFiles OutFiles;
180 unsigned status = 0;
181 // ApplyCallback will return true if it did not apply any callback. In that
182 // case, attempt to apply the MainFn.
183 StringRef FilenamePrefix(sys::path::stem(path: OutputFilename));
184 if (TableGen::Emitter::ApplyCallback(Records, OutFiles, FilenamePrefix))
185 status = MainFn ? MainFn(OutFiles, Records) : 1;
186 Timer.stopBackendTimer();
187 if (status)
188 return 1;
189
190 // Always write the depfile, even if the main output hasn't changed.
191 // If it's missing, Ninja considers the output dirty. If this was below
192 // the early exit below and someone deleted the .inc.d file but not the .inc
193 // file, tablegen would never write the depfile.
194 if (!DependFilename.empty()) {
195 if (int Ret = createDependencyFile(Parser, argv0))
196 return Ret;
197 }
198
199 Timer.startTimer(Name: "Write output");
200 if (int Ret = WriteOutput(argv0, Filename: OutputFilename, Content: OutFiles.MainFile))
201 return Ret;
202 for (auto [Suffix, Content] : OutFiles.AdditionalFiles) {
203 SmallString<128> Filename(OutputFilename);
204 // TODO: Format using the split-file convention when writing to stdout?
205 if (Filename != "-") {
206 sys::path::replace_extension(path&: Filename, extension: "");
207 Filename.append(RHS: Suffix);
208 }
209 if (int Ret = WriteOutput(argv0, Filename, Content))
210 return Ret;
211 }
212
213 Timer.stopTimer();
214 Timer.stopPhaseTiming();
215
216 if (ErrorsPrinted > 0)
217 return reportError(ProgName: argv0, Msg: Twine(ErrorsPrinted) + " errors.\n");
218 return 0;
219}
220
221int llvm::TableGenMain(const char *argv0, TableGenMainFn MainFn) {
222 return TableGenMain(argv0, MainFn: [&MainFn](TableGenOutputFiles &OutFiles,
223 const RecordKeeper &Records) {
224 std::string S;
225 raw_string_ostream OS(S);
226 int Res = MainFn(OS, Records);
227 OutFiles = {.MainFile: std::move(S), .AdditionalFiles: {}};
228 return Res;
229 });
230}
231