221 lines · cpp
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-exception6//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 the11// description in some predefined format. In practice, this is used by the LLVM12// code generators to automate generation of a code generator through a13// 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("-"));45 46static cl::opt<std::string>47DependFilename("d",48 cl::desc("Dependency filename"),49 cl::value_desc("filename"),50 cl::init(""));51 52static cl::opt<std::string>53InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));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(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(argv0, "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(argv0, "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 108static int WriteOutput(const TGParser &Parser, const char *argv0,109 StringRef Filename, 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 there113 // 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(argv0, "error opening " + Filename + ": " +122 EC.message() + "\n");123 OutFile.os() << Content;124 if (ErrorsPrinted == 0)125 OutFile.keep();126 127 return 0;128}129 130int 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("Parse, build records");140 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =141 MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true);142 if (std::error_code EC = FileOrErr.getError())143 return reportError(argv0, "Could not open input file '" + InputFilename +144 "': " + EC.message() + "\n");145 146 Records.saveInputFilename(InputFilename);147 148 // Tell SrcMgr about this buffer, which is what TGParser will pick up.149 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());150 151 // Record the location of the include directory so that the lexer can find152 // 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 parsing163 // (e.g., assert failures).164 if (ErrorsPrinted > 0)165 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");166 167 // Write output to memory.168 Timer.startBackendTimer("Backend overall");169 TableGenOutputFiles OutFiles;170 unsigned status = 0;171 // ApplyCallback will return true if it did not apply any callback. In that172 // case, attempt to apply the MainFn.173 StringRef FilenamePrefix(sys::path::stem(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 below182 // the early exit below and someone deleted the .inc.d file but not the .inc183 // 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("Write output");190 if (int Ret = WriteOutput(Parser, argv0, OutputFilename, 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(Filename, "");197 Filename.append(Suffix);198 }199 if (int Ret = WriteOutput(Parser, argv0, Filename, Content))200 return Ret;201 }202 203 Timer.stopTimer();204 Timer.stopPhaseTiming();205 206 if (ErrorsPrinted > 0)207 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");208 return 0;209}210 211int llvm::TableGenMain(const char *argv0, TableGenMainFn MainFn) {212 return TableGenMain(argv0, [&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 = {S, {}};218 return Res;219 });220}221