LLVM 24.0.0git
Main.cpp
Go to the documentation of this file.
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"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Twine.h"
27#include "llvm/Support/Path.h"
28#include "llvm/Support/SMLoc.h"
33#include "llvm/TableGen/Error.h"
37#include <memory>
38#include <set>
39#include <string>
40#include <system_error>
41#include <utility>
42using namespace llvm;
43
45OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
46 cl::init("-"));
47
50 cl::desc("Dependency filename"),
51 cl::value_desc("filename"),
52 cl::init(""));
53
56
58IncludeDirs("I", cl::desc("Directory of include files"),
59 cl::value_desc("directory"), cl::Prefix);
60
62MacroNames("D", cl::desc("Name of the macro to be defined"),
63 cl::value_desc("macro name"), cl::Prefix);
64
65static cl::opt<bool>
66WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed"));
67
68static cl::opt<bool>
69TimePhases("time-phases", cl::desc("Time phases of parser and backend"));
70
72 "long-string-literals",
73 cl::desc("when emitting large string tables, prefer string literals over "
74 "comma-separated char literals. This can be a readability and "
75 "compile-time performance win, but upsets some compilers"),
76 cl::Hidden, cl::init(true));
77
79 "no-warn-on-unused-template-args",
80 cl::desc("Disable unused template argument warnings."));
81
82static cl::opt<bool> Preprocess("E", cl::desc("Write preprocessed output"));
83
84static int reportError(const char *ProgName, Twine Msg) {
85 errs() << ProgName << ": " << Msg;
86 errs().flush();
87 return 1;
88}
89
90namespace {
91/// Encapsulate file, line and column numbers from SourceMgr.
92struct SMCoords {
93 unsigned Buf = 0;
94 unsigned Line = 0;
95 unsigned Col = 0;
96 SMCoords() = default;
97 SMCoords(const SourceMgr &Mgr, SMLoc Loc) {
98 Buf = Mgr.FindBufferContainingLoc(Loc);
99 // TODO: Find a faster way to do this. For example we don't need the column
100 // number for every token, only the first token on each output line.
101 std::tie(Line, Col) = Mgr.getLineAndColumn(Loc, Buf);
102 }
103};
104} // end anonymous namespace
105
106/// Create preprocessed output for `-E` option.
107static int preprocessInput(TGLexer &Lex, raw_ostream &OS) {
108 SMCoords Last;
109 while (true) {
110 Lex.Lex();
111 if (Lex.getCode() == tgtok::Eof || Lex.getCode() == tgtok::Error)
112 break;
113 SMCoords This(SrcMgr, Lex.getLoc());
114 if (This.Buf == Last.Buf && This.Line == Last.Line) {
115 // Add a single space between tokens on the same line. This is overkill in
116 // many cases but at least it will parse correctly.
117 OS << ' ';
118 } else {
119 // Always start a new line when including a new file or popping back out
120 // to the previous file. This is just a heuristic to make the output look
121 // reasonably pretty.
122 if (Last.Buf)
123 OS << '\n';
124 // Indent the first token on a line to its original indentation, to make
125 // the output look pretty.
126 OS.indent(This.Col - 1);
127 }
128
129 const char *Start = Lex.getLoc().getPointer();
130 const char *End = Lex.getLocRange().End.getPointer();
131 StringRef Text(Start, End - Start);
132 OS << Text;
133
134 Last = This;
135 // A code block is the only kind of token that can span multiple lines.
136 // Record the line it ends on, so that a following token on that line stays
137 // on the same output line. Count '\n' characters to match how SourceMgr
138 // counts lines.
139 if (Lex.getCode() == tgtok::CodeFragment)
140 Last.Line += Text.count('\n');
141 }
142 if (Last.Buf != 0)
143 OS << '\n';
144 return Lex.getCode() == tgtok::Error;
145}
146
147/// Escape a filename in the dependency file so that it is correctly
148/// interpreted by `make`. This is consistent with Clang, GCC, and lld.
150 std::string Res;
151 raw_string_ostream OS(Res);
152
153 // Only transform the path to native on Windows, where backslashes are valid
154 // path separators. On non-Windows platforms, we don't want backslashes in
155 // filenames to be incorrectly treated as path separators.
156#ifdef _WIN32
157 SmallString<256> NativePath;
158 sys::path::native(Filename, NativePath);
159#else
160 StringRef NativePath = Filename;
161#endif
162
163 for (unsigned I = 0, E = NativePath.size(); I != E; ++I) {
164 if (NativePath[I] == '#')
165 OS << '\\';
166 else if (NativePath[I] == ' ') {
167 OS << '\\';
168 unsigned J = I;
169 while (J > 0 && NativePath[--J] == '\\')
170 OS << '\\';
171 } else if (NativePath[I] == '$')
172 OS << '$';
173 OS << NativePath[I];
174 }
175
176 return Res;
177}
178
179/// Create a dependency file for `-d` option.
180///
181/// This functionality is really only for the benefit of the build system.
182/// It is similar to GCC's `-M*` family of options.
183static int createDependencyFile(const char *argv0) {
184 if (OutputFilename == "-")
185 return reportError(argv0, "the option -d must be used together with -o\n");
186
187 std::error_code EC;
189 if (EC)
190 return reportError(argv0, "error opening " + DependFilename + ":" +
191 EC.message() + "\n");
192 DepOut.os() << escapeDependencyFilename(OutputFilename) << ":";
193
194 // Emit the primary input file as a dependency. This matches C compilers like
195 // Clang and GCC. Without it, a .td file with no `include` directives would
196 // produce a depfile listing zero dependencies. CMake's
197 // `cmake_transform_depfile` then collapses that to a 0-byte file, which Ninja
198 // treats as a missing depfile and re-runs the rule on every incremental
199 // build.
200 if (InputFilename != "-")
201 DepOut.os() << ' ' << escapeDependencyFilename(InputFilename);
202
203 // Every other buffer in SrcMgr was added by an include directive, and its
204 // identifier is the path that the include resolved to. Buffer 1 is the main
205 // file. Use a set to sort the list and remove duplicates.
206 std::set<std::string> Dependencies;
207 for (unsigned I = 2, E = SrcMgr.getNumBuffers(); I <= E; ++I)
208 Dependencies.insert(SrcMgr.getMemoryBuffer(I)->getBufferIdentifier().str());
209 for (const std::string &Dep : Dependencies)
210 DepOut.os() << ' ' << escapeDependencyFilename(Dep);
211 DepOut.os() << "\n";
212 DepOut.keep();
213 return 0;
214}
215
216static int WriteOutput(const char *argv0, StringRef Filename,
217 StringRef Content) {
218 if (WriteIfChanged) {
219 // Only updates the real output file if there are any differences.
220 // This prevents recompilation of all the files depending on it if there
221 // aren't any.
222 if (auto ExistingOrErr = MemoryBuffer::getFile(Filename, /*IsText=*/true))
223 if (std::move(ExistingOrErr.get())->getBuffer() == Content)
224 return 0;
225 }
226 std::error_code EC;
227 // On z/OS, text files default to IBM-1047, but generated files are UTF-8.
228 // OF_ForceUTF8 has no effect on other platforms.
229 ToolOutputFile OutFile(Filename, EC,
231 if (EC)
232 return reportError(argv0, "error opening " + Filename + ": " +
233 EC.message() + "\n");
234 OutFile.os() << Content;
235 if (ErrorsPrinted == 0)
236 OutFile.keep();
237
238 return 0;
239}
240
241int llvm::TableGenMain(const char *argv0, MultiFileTableGenMainFn MainFn) {
244 if (std::error_code EC = FileOrErr.getError())
245 return reportError(argv0, "Could not open input file '" + InputFilename +
246 "': " + EC.message() + "\n");
247
248 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
249 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
250
251 // Record the location of the include directory so that the lexer can find
252 // it later.
253 SrcMgr.setIncludeDirs(IncludeDirs);
254 SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
255
256 if (Preprocess) {
258 std::string OutString;
259 raw_string_ostream Out(OutString);
260 if (int Ret = preprocessInput(Lex, Out))
261 return Ret;
262 // Return early if the lexer printed errors but still reached EOF (e.g., a
263 // NUL character in the source).
264 if (ErrorsPrinted > 0)
265 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
266 if (!DependFilename.empty()) {
267 if (int Ret = createDependencyFile(argv0))
268 return Ret;
269 }
270 return WriteOutput(argv0, OutputFilename, OutString);
271 }
272
273 RecordKeeper Records;
274 Records.saveInputFilename(InputFilename);
275
276 TGTimer &Timer = Records.getTimer();
277 if (TimePhases)
278 Timer.startPhaseTiming();
279
280 // Parse the input file.
281
282 Timer.startTimer("Parse, build records");
283
285
286 if (Parser.ParseFile())
287 return 1;
289
290 // Return early if any other errors were generated during parsing
291 // (e.g., assert failures).
292 if (ErrorsPrinted > 0)
293 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
294
295 // Write output to memory.
296 Timer.startBackendTimer("Backend overall");
297 TableGenOutputFiles OutFiles;
298 unsigned status = 0;
299 // ApplyCallback will return true if it did not apply any callback. In that
300 // case, attempt to apply the MainFn.
302 if (TableGen::Emitter::ApplyCallback(Records, OutFiles, FilenamePrefix))
303 status = MainFn ? MainFn(OutFiles, Records) : 1;
304 Timer.stopBackendTimer();
305 if (status)
306 return 1;
307
308 // Always write the depfile, even if the main output hasn't changed.
309 // If it's missing, Ninja considers the output dirty. If this was below
310 // the early exit below and someone deleted the .inc.d file but not the .inc
311 // file, tablegen would never write the depfile.
312 if (!DependFilename.empty()) {
313 if (int Ret = createDependencyFile(argv0))
314 return Ret;
315 }
316
317 Timer.startTimer("Write output");
318 if (int Ret = WriteOutput(argv0, OutputFilename, OutFiles.MainFile))
319 return Ret;
320 for (auto [Suffix, Content] : OutFiles.AdditionalFiles) {
322 // TODO: Format using the split-file convention when writing to stdout?
323 if (Filename != "-") {
325 Filename.append(Suffix);
326 }
327 if (int Ret = WriteOutput(argv0, Filename, Content))
328 return Ret;
329 }
330
332 Timer.stopPhaseTiming();
333
334 if (ErrorsPrinted > 0)
335 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
336 return 0;
337}
338
339int llvm::TableGenMain(const char *argv0, TableGenMainFn MainFn) {
340 return TableGenMain(argv0, [&MainFn](TableGenOutputFiles &OutFiles,
341 const RecordKeeper &Records) {
342 std::string S;
343 raw_string_ostream OS(S);
344 int Res = MainFn(OS, Records);
345 OutFiles = {std::move(S), {}};
346 return Res;
347 });
348}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Provides ErrorOr<T> smart pointer.
#define I(x, y, z)
Definition MD5.cpp:57
static cl::opt< bool > NoWarnOnUnusedTemplateArgs("no-warn-on-unused-template-args", cl::desc("Disable unused template argument warnings."))
static cl::list< std::string > IncludeDirs("I", cl::desc("Directory of include files"), cl::value_desc("directory"), cl::Prefix)
static std::string escapeDependencyFilename(StringRef Filename)
Escape a filename in the dependency file so that it is correctly interpreted by make.
Definition Main.cpp:149
static cl::opt< bool > WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed"))
static cl::opt< std::string > DependFilename("d", cl::desc("Dependency filename"), cl::value_desc("filename"), cl::init(""))
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
static cl::opt< bool > TimePhases("time-phases", cl::desc("Time phases of parser and backend"))
static int createDependencyFile(const char *argv0)
Create a dependency file for -d option.
Definition Main.cpp:183
static int preprocessInput(TGLexer &Lex, raw_ostream &OS)
Create preprocessed output for -E option.
Definition Main.cpp:107
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
static int WriteOutput(const char *argv0, StringRef Filename, StringRef Content)
Definition Main.cpp:216
static cl::list< std::string > MacroNames("D", cl::desc("Name of the macro to be defined"), cl::value_desc("macro name"), cl::Prefix)
static cl::opt< bool > Preprocess("E", cl::desc("Write preprocessed output"))
static int reportError(const char *ProgName, Twine Msg)
Definition Main.cpp:84
static constexpr StringLiteral Filename
const char * Msg
This file defines the SmallString class.
Defines the virtual file system interface vfs::FileSystem.
Represents either an error or a value T.
Definition ErrorOr.h:56
std::error_code getError() const
Definition ErrorOr.h:152
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Represents a location in source code.
Definition SMLoc.h:22
constexpr const char * getPointer() const
Definition SMLoc.h:33
SMLoc End
Definition SMLoc.h:49
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
LLVM_ABI std::pair< unsigned, unsigned > getLineAndColumn(SMLoc Loc, unsigned BufferID=0) const
Find the line and column number for the specified location in the specified file.
LLVM_ABI unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition SourceMgr.cpp:97
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TGLexer - TableGen Lexer class.
Definition TGLexer.h:193
SMRange getLocRange() const
Definition TGLexer.cpp:97
tgtok::TokKind Lex()
Definition TGLexer.h:212
tgtok::TokKind getCode() const
Definition TGLexer.h:214
SMLoc getLoc() const
Definition TGLexer.cpp:95
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
This class is used to track the amount of time spent between invocations of its startTimer()/stopTime...
Definition Timer.h:87
LLVM_ABI void stopTimer()
Stop the timer.
Definition Timer.cpp:159
LLVM_ABI void startTimer()
Start the timer running.
Definition Timer.cpp:150
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
void keep()
Indicate that the tool's job wrt this output file has been successful and the file should not be dele...
raw_fd_ostream & os()
Return the contained raw_fd_ostream.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an std::string.
LLVM_ABI bool ApplyCallback(const RecordKeeper &Records, TableGenOutputFiles &OutFiles, StringRef FilenamePrefix)
Apply callback for any command line option registered above.
initializer< Ty > init(const Ty &Val)
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:793
@ OF_ForceUTF8
The file should be tagged as UTF-8 rather than the default IBM-1047 encoding.
Definition FileSystem.h:832
LLVM_ABI StringRef stem(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get stem.
Definition Path.cpp:596
LLVM_ABI void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition Path.cpp:491
LLVM_ABI IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI unsigned ErrorsPrinted
Definition Error.cpp:25
function_ref< bool(TableGenOutputFiles &OutFiles, const RecordKeeper &Records)> MultiFileTableGenMainFn
Perform the action using Records, and store output in OutFiles.
Definition Main.h:37
function_ref< bool(raw_ostream &OS, const RecordKeeper &Records)> TableGenMainFn
Returns true on error, false otherwise.
Definition Main.h:32
LLVM_ABI cl::opt< bool > EmitLongStrLiterals
Controls emitting large character arrays as strings or character arrays.
LLVM_ABI SourceMgr SrcMgr
Definition Error.cpp:24
LLVM_ABI int TableGenMain(const char *argv0, TableGenMainFn MainFn=nullptr)
Definition Main.cpp:339
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
std::map< StringRef, std::string > AdditionalFiles
Definition Main.h:28
std::string MainFile
Definition Main.h:25