| 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> |
| 40 | using namespace llvm; |
| 41 | |
| 42 | static cl::opt<std::string> |
| 43 | OutputFilename("o" , cl::desc("Output filename" ), cl::value_desc("filename" ), |
| 44 | cl::init(Val: "-" )); |
| 45 | |
| 46 | static cl::opt<std::string> |
| 47 | DependFilename("d" , |
| 48 | cl::desc("Dependency filename" ), |
| 49 | cl::value_desc("filename" ), |
| 50 | cl::init(Val: "" )); |
| 51 | |
| 52 | static cl::opt<std::string> |
| 53 | InputFilename(cl::Positional, cl::desc("<input file>" ), cl::init(Val: "-" )); |
| 54 | |
| 55 | static cl::list<std::string> |
| 56 | IncludeDirs("I" , cl::desc("Directory of include files" ), |
| 57 | cl::value_desc("directory" ), cl::Prefix); |
| 58 | |
| 59 | static cl::list<std::string> |
| 60 | MacroNames("D" , cl::desc("Name of the macro to be defined" ), |
| 61 | cl::value_desc("macro name" ), cl::Prefix); |
| 62 | |
| 63 | static cl::opt<bool> |
| 64 | WriteIfChanged("write-if-changed" , cl::desc("Only write output if it changed" )); |
| 65 | |
| 66 | static cl::opt<bool> |
| 67 | TimePhases("time-phases" , cl::desc("Time phases of parser and backend" )); |
| 68 | |
| 69 | cl::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 | |
| 76 | static cl::opt<bool> NoWarnOnUnusedTemplateArgs( |
| 77 | "no-warn-on-unused-template-args" , |
| 78 | cl::desc("Disable unused template argument warnings." )); |
| 79 | |
| 80 | static 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. |
| 90 | static 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 | for (const auto &Dep : Parser.getDependencies()) { |
| 101 | DepOut.os() << ' ' << Dep; |
| 102 | } |
| 103 | DepOut.os() << "\n" ; |
| 104 | DepOut.keep(); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int WriteOutput(const char *argv0, StringRef Filename, |
| 109 | StringRef Content) { |
| 110 | if (WriteIfChanged) { |
| 111 | // Only updates the real output file if there are any differences. |
| 112 | // This prevents recompilation of all the files depending on it if there |
| 113 | // aren't any. |
| 114 | if (auto ExistingOrErr = MemoryBuffer::getFile(Filename, /*IsText=*/true)) |
| 115 | if (std::move(ExistingOrErr.get())->getBuffer() == Content) |
| 116 | return 0; |
| 117 | } |
| 118 | std::error_code EC; |
| 119 | ToolOutputFile OutFile(Filename, EC, sys::fs::OF_Text); |
| 120 | if (EC) |
| 121 | return reportError(ProgName: argv0, Msg: "error opening " + Filename + ": " + |
| 122 | EC.message() + "\n" ); |
| 123 | OutFile.os() << Content; |
| 124 | if (ErrorsPrinted == 0) |
| 125 | OutFile.keep(); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int llvm::TableGenMain(const char *argv0, MultiFileTableGenMainFn MainFn) { |
| 131 | RecordKeeper Records; |
| 132 | TGTimer &Timer = Records.getTimer(); |
| 133 | |
| 134 | if (TimePhases) |
| 135 | Timer.startPhaseTiming(); |
| 136 | |
| 137 | // Parse the input file. |
| 138 | |
| 139 | Timer.startTimer(Name: "Parse, build records" ); |
| 140 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 141 | MemoryBuffer::getFileOrSTDIN(Filename: InputFilename, /*IsText=*/true); |
| 142 | if (std::error_code EC = FileOrErr.getError()) |
| 143 | return reportError(ProgName: argv0, Msg: "Could not open input file '" + InputFilename + |
| 144 | "': " + EC.message() + "\n" ); |
| 145 | |
| 146 | Records.saveInputFilename(Filename: InputFilename); |
| 147 | |
| 148 | // Tell SrcMgr about this buffer, which is what TGParser will pick up. |
| 149 | SrcMgr.AddNewSourceBuffer(F: std::move(*FileOrErr), IncludeLoc: SMLoc()); |
| 150 | |
| 151 | // Record the location of the include directory so that the lexer can find |
| 152 | // it later. |
| 153 | SrcMgr.setIncludeDirs(IncludeDirs); |
| 154 | SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem()); |
| 155 | |
| 156 | TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs); |
| 157 | |
| 158 | if (Parser.ParseFile()) |
| 159 | return 1; |
| 160 | Timer.stopTimer(); |
| 161 | |
| 162 | // Return early if any other errors were generated during parsing |
| 163 | // (e.g., assert failures). |
| 164 | if (ErrorsPrinted > 0) |
| 165 | return reportError(ProgName: argv0, Msg: Twine(ErrorsPrinted) + " errors.\n" ); |
| 166 | |
| 167 | // Write output to memory. |
| 168 | Timer.startBackendTimer(Name: "Backend overall" ); |
| 169 | TableGenOutputFiles OutFiles; |
| 170 | unsigned status = 0; |
| 171 | // ApplyCallback will return true if it did not apply any callback. In that |
| 172 | // case, attempt to apply the MainFn. |
| 173 | StringRef FilenamePrefix(sys::path::stem(path: OutputFilename)); |
| 174 | if (TableGen::Emitter::ApplyCallback(Records, OutFiles, FilenamePrefix)) |
| 175 | status = MainFn ? MainFn(OutFiles, Records) : 1; |
| 176 | Timer.stopBackendTimer(); |
| 177 | if (status) |
| 178 | return 1; |
| 179 | |
| 180 | // Always write the depfile, even if the main output hasn't changed. |
| 181 | // If it's missing, Ninja considers the output dirty. If this was below |
| 182 | // the early exit below and someone deleted the .inc.d file but not the .inc |
| 183 | // file, tablegen would never write the depfile. |
| 184 | if (!DependFilename.empty()) { |
| 185 | if (int Ret = createDependencyFile(Parser, argv0)) |
| 186 | return Ret; |
| 187 | } |
| 188 | |
| 189 | Timer.startTimer(Name: "Write output" ); |
| 190 | if (int Ret = WriteOutput(argv0, Filename: OutputFilename, Content: OutFiles.MainFile)) |
| 191 | return Ret; |
| 192 | for (auto [Suffix, Content] : OutFiles.AdditionalFiles) { |
| 193 | SmallString<128> Filename(OutputFilename); |
| 194 | // TODO: Format using the split-file convention when writing to stdout? |
| 195 | if (Filename != "-" ) { |
| 196 | sys::path::replace_extension(path&: Filename, extension: "" ); |
| 197 | Filename.append(RHS: Suffix); |
| 198 | } |
| 199 | if (int Ret = WriteOutput(argv0, Filename, Content)) |
| 200 | return Ret; |
| 201 | } |
| 202 | |
| 203 | Timer.stopTimer(); |
| 204 | Timer.stopPhaseTiming(); |
| 205 | |
| 206 | if (ErrorsPrinted > 0) |
| 207 | return reportError(ProgName: argv0, Msg: Twine(ErrorsPrinted) + " errors.\n" ); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int llvm::TableGenMain(const char *argv0, TableGenMainFn MainFn) { |
| 212 | return TableGenMain(argv0, MainFn: [&MainFn](TableGenOutputFiles &OutFiles, |
| 213 | const RecordKeeper &Records) { |
| 214 | std::string S; |
| 215 | raw_string_ostream OS(S); |
| 216 | int Res = MainFn(OS, Records); |
| 217 | OutFiles = {.MainFile: S, .AdditionalFiles: {}}; |
| 218 | return Res; |
| 219 | }); |
| 220 | } |
| 221 | |