1 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/TableGen/Main.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/SMLoc.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/ToolOutputFile.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/TableGen/Error.h"
31 #include "llvm/TableGen/Record.h"
32 #include "llvm/TableGen/TGTimer.h"
33 #include "llvm/TableGen/TableGenBackend.h"
36 #include <system_error>
40 static cl::opt
<std::string
>
41 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
44 static cl::opt
<std::string
>
46 cl::desc("Dependency filename"),
47 cl::value_desc("filename"),
50 static cl::opt
<std::string
>
51 InputFilename(cl::Positional
, cl::desc("<input file>"), cl::init("-"));
53 static cl::list
<std::string
>
54 IncludeDirs("I", cl::desc("Directory of include files"),
55 cl::value_desc("directory"), cl::Prefix
);
57 static cl::list
<std::string
>
58 MacroNames("D", cl::desc("Name of the macro to be defined"),
59 cl::value_desc("macro name"), cl::Prefix
);
62 WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed"));
65 TimePhases("time-phases", cl::desc("Time phases of parser and backend"));
67 static cl::opt
<bool> NoWarnOnUnusedTemplateArgs(
68 "no-warn-on-unused-template-args",
69 cl::desc("Disable unused template argument warnings."));
71 static int reportError(const char *ProgName
, Twine Msg
) {
72 errs() << ProgName
<< ": " << Msg
;
77 /// Create a dependency file for `-d` option.
79 /// This functionality is really only for the benefit of the build system.
80 /// It is similar to GCC's `-M*` family of options.
81 static int createDependencyFile(const TGParser
&Parser
, const char *argv0
) {
82 if (OutputFilename
== "-")
83 return reportError(argv0
, "the option -d must be used together with -o\n");
86 ToolOutputFile
DepOut(DependFilename
, EC
, sys::fs::OF_Text
);
88 return reportError(argv0
, "error opening " + DependFilename
+ ":" +
90 DepOut
.os() << OutputFilename
<< ":";
91 for (const auto &Dep
: Parser
.getDependencies()) {
92 DepOut
.os() << ' ' << Dep
;
99 int llvm::TableGenMain(const char *argv0
,
100 std::function
<TableGenMainFn
> MainFn
) {
101 RecordKeeper Records
;
102 TGTimer
&Timer
= Records
.getTimer();
105 Timer
.startPhaseTiming();
107 // Parse the input file.
109 Timer
.startTimer("Parse, build records");
110 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
111 MemoryBuffer::getFileOrSTDIN(InputFilename
, /*IsText=*/true);
112 if (std::error_code EC
= FileOrErr
.getError())
113 return reportError(argv0
, "Could not open input file '" + InputFilename
+
114 "': " + EC
.message() + "\n");
116 Records
.saveInputFilename(InputFilename
);
118 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
119 SrcMgr
.AddNewSourceBuffer(std::move(*FileOrErr
), SMLoc());
121 // Record the location of the include directory so that the lexer can find
123 SrcMgr
.setIncludeDirs(IncludeDirs
);
125 TGParser
Parser(SrcMgr
, MacroNames
, Records
, NoWarnOnUnusedTemplateArgs
);
127 if (Parser
.ParseFile())
131 // Write output to memory.
132 Timer
.startBackendTimer("Backend overall");
133 std::string OutString
;
134 raw_string_ostream
Out(OutString
);
136 // ApplyCallback will return true if it did not apply any callback. In that
137 // case, attempt to apply the MainFn.
138 if (TableGen::Emitter::ApplyCallback(Records
, Out
))
139 status
= MainFn
? MainFn(Out
, Records
) : 1;
140 Timer
.stopBackendTimer();
144 // Always write the depfile, even if the main output hasn't changed.
145 // If it's missing, Ninja considers the output dirty. If this was below
146 // the early exit below and someone deleted the .inc.d file but not the .inc
147 // file, tablegen would never write the depfile.
148 if (!DependFilename
.empty()) {
149 if (int Ret
= createDependencyFile(Parser
, argv0
))
153 Timer
.startTimer("Write output");
154 bool WriteFile
= true;
155 if (WriteIfChanged
) {
156 // Only updates the real output file if there are any differences.
157 // This prevents recompilation of all the files depending on it if there
159 if (auto ExistingOrErr
=
160 MemoryBuffer::getFile(OutputFilename
, /*IsText=*/true))
161 if (std::move(ExistingOrErr
.get())->getBuffer() == OutString
)
166 ToolOutputFile
OutFile(OutputFilename
, EC
, sys::fs::OF_Text
);
168 return reportError(argv0
, "error opening " + OutputFilename
+ ": " +
169 EC
.message() + "\n");
170 OutFile
.os() << OutString
;
171 if (ErrorsPrinted
== 0)
176 Timer
.stopPhaseTiming();
178 if (ErrorsPrinted
> 0)
179 return reportError(argv0
, Twine(ErrorsPrinted
) + " errors.\n");