1 //===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // TableGen is a tool which can be used to build up a description of something,
11 // then invoke one or more "tablegen backends" to emit information about the
12 // description in some predefined format. In practice, this is used by the LLVM
13 // code generators to automate generation of a code generator through a
14 // high-level description of the target.
16 //===----------------------------------------------------------------------===//
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Streams.h"
22 #include "llvm/System/Signals.h"
23 #include "llvm/Support/FileUtilities.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "CallingConvEmitter.h"
27 #include "CodeEmitterGen.h"
28 #include "RegisterInfoEmitter.h"
29 #include "InstrInfoEmitter.h"
30 #include "InstrEnumEmitter.h"
31 #include "AsmWriterEmitter.h"
32 #include "DAGISelEmitter.h"
33 #include "FastISelEmitter.h"
34 #include "SubtargetEmitter.h"
35 #include "IntrinsicEmitter.h"
36 #include "LLVMCConfigurationEmitter.h"
37 #include "ClangDiagnosticsEmitter.h"
47 GenRegisterEnums, GenRegister, GenRegisterHeader,
48 GenInstrEnums, GenInstrs, GenAsmWriter,
62 Action(cl::desc("Action to perform:"),
63 cl::values(clEnumValN(PrintRecords, "print-records",
64 "Print all records to stdout (default)"),
65 clEnumValN(GenEmitter, "gen-emitter",
66 "Generate machine code emitter"),
67 clEnumValN(GenRegisterEnums, "gen-register-enums",
68 "Generate enum values for registers"),
69 clEnumValN(GenRegister, "gen-register-desc",
70 "Generate a register info description"),
71 clEnumValN(GenRegisterHeader, "gen-register-desc-header",
72 "Generate a register info description header"),
73 clEnumValN(GenInstrEnums, "gen-instr-enums",
74 "Generate enum values for instructions"),
75 clEnumValN(GenInstrs, "gen-instr-desc",
76 "Generate instruction descriptions"),
77 clEnumValN(GenCallingConv, "gen-callingconv",
78 "Generate calling convention descriptions"),
79 clEnumValN(GenAsmWriter, "gen-asm-writer",
80 "Generate assembly writer"),
81 clEnumValN(GenDAGISel, "gen-dag-isel",
82 "Generate a DAG instruction selector"),
83 clEnumValN(GenFastISel, "gen-fast-isel",
84 "Generate a \"fast\" instruction selector"),
85 clEnumValN(GenSubtarget, "gen-subtarget",
86 "Generate subtarget enumerations"),
87 clEnumValN(GenIntrinsic, "gen-intrinsic",
88 "Generate intrinsic information"),
89 clEnumValN(GenTgtIntrinsic, "gen-tgt-intrinsic",
90 "Generate target intrinsic information"),
91 clEnumValN(GenClangDiagsDefs, "gen-clang-diags-defs",
92 "Generate Clang diagnostics definitions"),
93 clEnumValN(GenLLVMCConf, "gen-llvmc",
94 "Generate LLVMC configuration library"),
95 clEnumValN(PrintEnums, "print-enums",
96 "Print enum values for a class"),
100 Class("class", cl::desc("Print Enum list for this class"),
101 cl::value_desc("class name"));
104 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
108 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
110 cl::list<std::string>
111 IncludeDirs("I", cl::desc("Directory of include files"),
112 cl::value_desc("directory"), cl::Prefix);
115 ClangComponent("clang-component",
116 cl::desc("Only use warnings from specified component"),
117 cl::value_desc("component"), cl::Hidden);
121 // FIXME: Eliminate globals from tblgen.
122 RecordKeeper llvm::Records;
124 static TGSourceMgr SrcMgr;
126 void llvm::PrintError(TGLoc ErrorLoc, const std::string &Msg) {
127 SrcMgr.PrintError(ErrorLoc, Msg);
132 /// ParseFile - this function begins the parsing of the specified tablegen
134 static bool ParseFile(const std::string &Filename,
135 const std::vector<std::string> &IncludeDirs,
136 TGSourceMgr &SrcMgr) {
137 std::string ErrorStr;
138 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
140 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
144 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
145 SrcMgr.AddNewSourceBuffer(F, TGLoc());
147 TGParser Parser(SrcMgr);
149 // Record the location of the include directory so that the lexer can find
151 Parser.setIncludeDirs(IncludeDirs);
153 return Parser.ParseFile();
156 int main(int argc, char **argv) {
157 sys::PrintStackTraceOnErrorSignal();
158 PrettyStackTraceProgram X(argc, argv);
159 cl::ParseCommandLineOptions(argc, argv);
162 // Parse the input file.
163 if (ParseFile(InputFilename, IncludeDirs, SrcMgr))
166 std::ostream *Out = cout.stream();
167 if (OutputFilename != "-") {
168 Out = new std::ofstream(OutputFilename.c_str());
171 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
175 // Make sure the file gets removed if *gasp* tablegen crashes...
176 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
182 *Out << Records; // No argument, dump all contents
185 CodeEmitterGen(Records).run(*Out);
188 case GenRegisterEnums:
189 RegisterInfoEmitter(Records).runEnums(*Out);
192 RegisterInfoEmitter(Records).run(*Out);
194 case GenRegisterHeader:
195 RegisterInfoEmitter(Records).runHeader(*Out);
198 InstrEnumEmitter(Records).run(*Out);
201 InstrInfoEmitter(Records).run(*Out);
204 CallingConvEmitter(Records).run(*Out);
207 AsmWriterEmitter(Records).run(*Out);
209 case GenClangDiagsDefs:
210 ClangDiagsDefsEmitter(Records, ClangComponent).run(*Out);
213 DAGISelEmitter(Records).run(*Out);
216 FastISelEmitter(Records).run(*Out);
219 SubtargetEmitter(Records).run(*Out);
222 IntrinsicEmitter(Records).run(*Out);
224 case GenTgtIntrinsic:
225 IntrinsicEmitter(Records, true).run(*Out);
228 LLVMCConfigurationEmitter(Records).run(*Out);
232 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
233 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
234 *Out << Recs[i]->getName() << ", ";
239 assert(1 && "Invalid Action");
243 if (Out != cout.stream())
244 delete Out; // Close the file
247 } catch (const TGError &Error) {
248 cerr << argv[0] << ": error:\n";
249 PrintError(Error.getLoc(), Error.getMessage());
251 } catch (const std::string &Error) {
252 cerr << argv[0] << ": " << Error << "\n";
253 } catch (const char *Error) {
254 cerr << argv[0] << ": " << Error << "\n";
256 cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
259 if (Out != cout.stream()) {
260 delete Out; // Close the file
261 std::remove(OutputFilename.c_str()); // Remove the file, it's broken