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/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/TableGenBackend.h" 33 #include <memory> 34 #include <string> 35 #include <system_error> 36 #include <utility> 37 #include <vector> 38 using namespace llvm; 39 40 static cl::opt<std::string> 41 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), 42 cl::init("-")); 43 44 static cl::opt<std::string> 45 DependFilename("d", 46 cl::desc("Dependency filename"), 47 cl::value_desc("filename"), 48 cl::init("")); 49 50 static cl::opt<std::string> 51 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); 52 53 static cl::list<std::string> 54 IncludeDirs("I", cl::desc("Directory of include files"), 55 cl::value_desc("directory"), cl::Prefix); 56 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); 60 61 static cl::opt<bool> 62 WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed")); 63 64 static cl::opt<bool> 65 TimePhases("time-phases", cl::desc("Time phases of parser and backend")); 66 67 static cl::opt<bool> NoWarnOnUnusedTemplateArgs( 68 "no-warn-on-unused-template-args", 69 cl::desc("Disable unused template argument warnings.")); 70 71 static int reportError(const char *ProgName, Twine Msg) { 72 errs() << ProgName << ": " << Msg; 73 errs().flush(); 74 return 1; 75 } 76 77 /// Create a dependency file for `-d` option. 78 /// 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"); 84 85 std::error_code EC; 86 ToolOutputFile DepOut(DependFilename, EC, sys::fs::OF_Text); 87 if (EC) 88 return reportError(argv0, "error opening " + DependFilename + ":" + 89 EC.message() + "\n"); 90 DepOut.os() << OutputFilename << ":"; 91 for (const auto &Dep : Parser.getDependencies()) { 92 DepOut.os() << ' ' << Dep; 93 } 94 DepOut.os() << "\n"; 95 DepOut.keep(); 96 return 0; 97 } 98 99 int llvm::TableGenMain(const char *argv0, 100 std::function<TableGenMainFn> MainFn) { 101 RecordKeeper Records; 102 103 if (TimePhases) 104 Records.startPhaseTiming(); 105 106 // Parse the input file. 107 108 Records.startTimer("Parse, build records"); 109 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 110 MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true); 111 if (std::error_code EC = FileOrErr.getError()) 112 return reportError(argv0, "Could not open input file '" + InputFilename + 113 "': " + EC.message() + "\n"); 114 115 Records.saveInputFilename(InputFilename); 116 117 // Tell SrcMgr about this buffer, which is what TGParser will pick up. 118 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc()); 119 120 // Record the location of the include directory so that the lexer can find 121 // it later. 122 SrcMgr.setIncludeDirs(IncludeDirs); 123 124 TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs); 125 126 if (Parser.ParseFile()) 127 return 1; 128 Records.stopTimer(); 129 130 // Write output to memory. 131 Records.startBackendTimer("Backend overall"); 132 std::string OutString; 133 raw_string_ostream Out(OutString); 134 unsigned status = 0; 135 TableGen::Emitter::FnT ActionFn = TableGen::Emitter::Action->getValue(); 136 if (ActionFn) 137 ActionFn(Records, Out); 138 else if (MainFn) 139 status = MainFn(Out, Records); 140 else 141 return 1; 142 Records.stopBackendTimer(); 143 if (status) 144 return 1; 145 146 // Always write the depfile, even if the main output hasn't changed. 147 // If it's missing, Ninja considers the output dirty. If this was below 148 // the early exit below and someone deleted the .inc.d file but not the .inc 149 // file, tablegen would never write the depfile. 150 if (!DependFilename.empty()) { 151 if (int Ret = createDependencyFile(Parser, argv0)) 152 return Ret; 153 } 154 155 Records.startTimer("Write output"); 156 bool WriteFile = true; 157 if (WriteIfChanged) { 158 // Only updates the real output file if there are any differences. 159 // This prevents recompilation of all the files depending on it if there 160 // aren't any. 161 if (auto ExistingOrErr = 162 MemoryBuffer::getFile(OutputFilename, /*IsText=*/true)) 163 if (std::move(ExistingOrErr.get())->getBuffer() == Out.str()) 164 WriteFile = false; 165 } 166 if (WriteFile) { 167 std::error_code EC; 168 ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_Text); 169 if (EC) 170 return reportError(argv0, "error opening " + OutputFilename + ": " + 171 EC.message() + "\n"); 172 OutFile.os() << Out.str(); 173 if (ErrorsPrinted == 0) 174 OutFile.keep(); 175 } 176 177 Records.stopTimer(); 178 Records.stopPhaseTiming(); 179 180 if (ErrorsPrinted > 0) 181 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n"); 182 return 0; 183 } 184