184c287e3SPeter Collingbourne //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
284c287e3SPeter Collingbourne //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
684c287e3SPeter Collingbourne //
784c287e3SPeter Collingbourne //===----------------------------------------------------------------------===//
884c287e3SPeter Collingbourne //
984c287e3SPeter Collingbourne // TableGen is a tool which can be used to build up a description of something,
1084c287e3SPeter Collingbourne // then invoke one or more "tablegen backends" to emit information about the
1184c287e3SPeter Collingbourne // description in some predefined format. In practice, this is used by the LLVM
1284c287e3SPeter Collingbourne // code generators to automate generation of a code generator through a
1384c287e3SPeter Collingbourne // high-level description of the target.
1484c287e3SPeter Collingbourne //
1584c287e3SPeter Collingbourne //===----------------------------------------------------------------------===//
1684c287e3SPeter Collingbourne
171407b177SCraig Topper #include "llvm/TableGen/Main.h"
1884c287e3SPeter Collingbourne #include "TGParser.h"
1984c287e3SPeter Collingbourne #include "llvm/Support/CommandLine.h"
20d59664f4SBenjamin Kramer #include "llvm/Support/FileSystem.h"
2184c287e3SPeter Collingbourne #include "llvm/Support/MemoryBuffer.h"
2284c287e3SPeter Collingbourne #include "llvm/Support/ToolOutputFile.h"
2384c287e3SPeter Collingbourne #include "llvm/TableGen/Error.h"
2484c287e3SPeter Collingbourne #include "llvm/TableGen/Record.h"
2584c287e3SPeter Collingbourne #include <algorithm>
26a6e9c3e4SRafael Espindola #include <system_error>
2784c287e3SPeter Collingbourne using namespace llvm;
2884c287e3SPeter Collingbourne
29b7644fd5SCraig Topper static cl::opt<std::string>
3084c287e3SPeter Collingbourne OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
3184c287e3SPeter Collingbourne cl::init("-"));
3284c287e3SPeter Collingbourne
33b7644fd5SCraig Topper static cl::opt<std::string>
345c502811SMichael J. Spencer DependFilename("d",
355c502811SMichael J. Spencer cl::desc("Dependency filename"),
365c502811SMichael J. Spencer cl::value_desc("filename"),
3784c287e3SPeter Collingbourne cl::init(""));
3884c287e3SPeter Collingbourne
39b7644fd5SCraig Topper static cl::opt<std::string>
4084c287e3SPeter Collingbourne InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
4184c287e3SPeter Collingbourne
42b7644fd5SCraig Topper static cl::list<std::string>
4384c287e3SPeter Collingbourne IncludeDirs("I", cl::desc("Directory of include files"),
4484c287e3SPeter Collingbourne cl::value_desc("directory"), cl::Prefix);
4584c287e3SPeter Collingbourne
46f7d079e9SVyacheslav Zakharin static cl::list<std::string>
47f7d079e9SVyacheslav Zakharin MacroNames("D", cl::desc("Name of the macro to be defined"),
48f7d079e9SVyacheslav Zakharin cl::value_desc("macro name"), cl::Prefix);
49f7d079e9SVyacheslav Zakharin
50204623e0SNico Weber static cl::opt<bool>
51204623e0SNico Weber WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed"));
52204623e0SNico Weber
5354f9ee33SPaul C. Anagnostopoulos static cl::opt<bool>
5454f9ee33SPaul C. Anagnostopoulos TimePhases("time-phases", cl::desc("Time phases of parser and backend"));
5554f9ee33SPaul C. Anagnostopoulos
56*d968b173SCullen Rhodes static cl::opt<bool> NoWarnOnUnusedTemplateArgs(
57*d968b173SCullen Rhodes "no-warn-on-unused-template-args",
58*d968b173SCullen Rhodes cl::desc("Disable unused template argument warnings."));
59*d968b173SCullen Rhodes
reportError(const char * ProgName,Twine Msg)60ebd5350dSDavide Italiano static int reportError(const char *ProgName, Twine Msg) {
61ebd5350dSDavide Italiano errs() << ProgName << ": " << Msg;
62ebd5350dSDavide Italiano errs().flush();
63ebd5350dSDavide Italiano return 1;
64ebd5350dSDavide Italiano }
65ebd5350dSDavide Italiano
665f8f34e4SAdrian Prantl /// Create a dependency file for `-d` option.
670cd35365SSean Silva ///
680cd35365SSean Silva /// This functionality is really only for the benefit of the build system.
690cd35365SSean Silva /// It is similar to GCC's `-M*` family of options.
createDependencyFile(const TGParser & Parser,const char * argv0)702f7bf410SSean Silva static int createDependencyFile(const TGParser &Parser, const char *argv0) {
71ebd5350dSDavide Italiano if (OutputFilename == "-")
72ebd5350dSDavide Italiano return reportError(argv0, "the option -d must be used together with -o\n");
73ebd5350dSDavide Italiano
743fd1e993SRafael Espindola std::error_code EC;
754f750f6eSAbhina Sreeskantharajan ToolOutputFile DepOut(DependFilename, EC, sys::fs::OF_Text);
76ebd5350dSDavide Italiano if (EC)
77ebd5350dSDavide Italiano return reportError(argv0, "error opening " + DependFilename + ":" +
78ebd5350dSDavide Italiano EC.message() + "\n");
790cd35365SSean Silva DepOut.os() << OutputFilename << ":";
805fcc5abcSCraig Topper for (const auto &Dep : Parser.getDependencies()) {
81ee9b49eeSRiver Riddle DepOut.os() << ' ' << Dep;
820cd35365SSean Silva }
830cd35365SSean Silva DepOut.os() << "\n";
840cd35365SSean Silva DepOut.keep();
850cd35365SSean Silva return 0;
860cd35365SSean Silva }
870cd35365SSean Silva
TableGenMain(const char * argv0,TableGenMainFn * MainFn)8869dad324SAlex Brachet int llvm::TableGenMain(const char *argv0, TableGenMainFn *MainFn) {
8984c287e3SPeter Collingbourne RecordKeeper Records;
9084c287e3SPeter Collingbourne
9154f9ee33SPaul C. Anagnostopoulos if (TimePhases)
9254f9ee33SPaul C. Anagnostopoulos Records.startPhaseTiming();
9354f9ee33SPaul C. Anagnostopoulos
9484c287e3SPeter Collingbourne // Parse the input file.
9554f9ee33SPaul C. Anagnostopoulos
9654f9ee33SPaul C. Anagnostopoulos Records.startTimer("Parse, build records");
97adf21f2aSRafael Espindola ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
981bcf58b2SAbhina Sreeskantharajan MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true);
99ebd5350dSDavide Italiano if (std::error_code EC = FileOrErr.getError())
100ebd5350dSDavide Italiano return reportError(argv0, "Could not open input file '" + InputFilename +
101ebd5350dSDavide Italiano "': " + EC.message() + "\n");
10284c287e3SPeter Collingbourne
1030c1bb4f8SPaul C. Anagnostopoulos Records.saveInputFilename(InputFilename);
1040c1bb4f8SPaul C. Anagnostopoulos
10584c287e3SPeter Collingbourne // Tell SrcMgr about this buffer, which is what TGParser will pick up.
1061961f14cSDavid Blaikie SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
10784c287e3SPeter Collingbourne
10884c287e3SPeter Collingbourne // Record the location of the include directory so that the lexer can find
10984c287e3SPeter Collingbourne // it later.
11084c287e3SPeter Collingbourne SrcMgr.setIncludeDirs(IncludeDirs);
11184c287e3SPeter Collingbourne
112*d968b173SCullen Rhodes TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs);
11384c287e3SPeter Collingbourne
11484c287e3SPeter Collingbourne if (Parser.ParseFile())
11584c287e3SPeter Collingbourne return 1;
11654f9ee33SPaul C. Anagnostopoulos Records.stopTimer();
11784c287e3SPeter Collingbourne
118204623e0SNico Weber // Write output to memory.
11954f9ee33SPaul C. Anagnostopoulos Records.startBackendTimer("Backend overall");
120204623e0SNico Weber std::string OutString;
121204623e0SNico Weber raw_string_ostream Out(OutString);
12254f9ee33SPaul C. Anagnostopoulos unsigned status = MainFn(Out, Records);
12354f9ee33SPaul C. Anagnostopoulos Records.stopBackendTimer();
12454f9ee33SPaul C. Anagnostopoulos if (status)
125204623e0SNico Weber return 1;
126204623e0SNico Weber
127204623e0SNico Weber // Always write the depfile, even if the main output hasn't changed.
128204623e0SNico Weber // If it's missing, Ninja considers the output dirty. If this was below
129204623e0SNico Weber // the early exit below and someone deleted the .inc.d file but not the .inc
130204623e0SNico Weber // file, tablegen would never write the depfile.
131635debe8SJoerg Sonnenberger if (!DependFilename.empty()) {
1322f7bf410SSean Silva if (int Ret = createDependencyFile(Parser, argv0))
1330cd35365SSean Silva return Ret;
134635debe8SJoerg Sonnenberger }
135029cea90SChandler Carruth
13654f9ee33SPaul C. Anagnostopoulos Records.startTimer("Write output");
13754f9ee33SPaul C. Anagnostopoulos bool WriteFile = true;
138204623e0SNico Weber if (WriteIfChanged) {
139204623e0SNico Weber // Only updates the real output file if there are any differences.
140204623e0SNico Weber // This prevents recompilation of all the files depending on it if there
141204623e0SNico Weber // aren't any.
142c83cd8feSAbhina Sreeskantharajan if (auto ExistingOrErr =
1431bcf58b2SAbhina Sreeskantharajan MemoryBuffer::getFile(OutputFilename, /*IsText=*/true))
144204623e0SNico Weber if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
14554f9ee33SPaul C. Anagnostopoulos WriteFile = false;
146204623e0SNico Weber }
14754f9ee33SPaul C. Anagnostopoulos if (WriteFile) {
148204623e0SNico Weber std::error_code EC;
1494f750f6eSAbhina Sreeskantharajan ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_Text);
150204623e0SNico Weber if (EC)
151204623e0SNico Weber return reportError(argv0, "error opening " + OutputFilename + ": " +
152204623e0SNico Weber EC.message() + "\n");
153204623e0SNico Weber OutFile.os() << Out.str();
15454f9ee33SPaul C. Anagnostopoulos if (ErrorsPrinted == 0)
15554f9ee33SPaul C. Anagnostopoulos OutFile.keep();
15654f9ee33SPaul C. Anagnostopoulos }
15754f9ee33SPaul C. Anagnostopoulos
15854f9ee33SPaul C. Anagnostopoulos Records.stopTimer();
15954f9ee33SPaul C. Anagnostopoulos Records.stopPhaseTiming();
16084c287e3SPeter Collingbourne
161ebd5350dSDavide Italiano if (ErrorsPrinted > 0)
1623a13ed60SBenjamin Kramer return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
16384c287e3SPeter Collingbourne return 0;
16484c287e3SPeter Collingbourne }
165