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);
116 // FIXME: Eliminate globals from tblgen.
117 RecordKeeper llvm::Records;
119 static TGSourceMgr SrcMgr;
121 void llvm::PrintError(TGLoc ErrorLoc, const std::string &Msg) {
122 SrcMgr.PrintError(ErrorLoc, Msg);
127 /// ParseFile - this function begins the parsing of the specified tablegen
129 static bool ParseFile(const std::string &Filename,
130 const std::vector<std::string> &IncludeDirs,
131 TGSourceMgr &SrcMgr) {
132 std::string ErrorStr;
133 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
135 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
139 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
140 SrcMgr.AddNewSourceBuffer(F, TGLoc());
142 TGParser Parser(SrcMgr);
144 // Record the location of the include directory so that the lexer can find
146 Parser.setIncludeDirs(IncludeDirs);
148 return Parser.ParseFile();
151 int main(int argc, char **argv) {
152 sys::PrintStackTraceOnErrorSignal();
153 PrettyStackTraceProgram X(argc, argv);
154 cl::ParseCommandLineOptions(argc, argv);
157 // Parse the input file.
158 if (ParseFile(InputFilename, IncludeDirs, SrcMgr))
161 std::ostream *Out = cout.stream();
162 if (OutputFilename != "-") {
163 Out = new std::ofstream(OutputFilename.c_str());
166 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
170 // Make sure the file gets removed if *gasp* tablegen crashes...
171 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
177 *Out << Records; // No argument, dump all contents
180 CodeEmitterGen(Records).run(*Out);
183 case GenRegisterEnums:
184 RegisterInfoEmitter(Records).runEnums(*Out);
187 RegisterInfoEmitter(Records).run(*Out);
189 case GenRegisterHeader:
190 RegisterInfoEmitter(Records).runHeader(*Out);
193 InstrEnumEmitter(Records).run(*Out);
196 InstrInfoEmitter(Records).run(*Out);
199 CallingConvEmitter(Records).run(*Out);
202 AsmWriterEmitter(Records).run(*Out);
204 case GenClangDiagsDefs:
205 ClangDiagsDefsEmitter(Records).run(*Out);
208 DAGISelEmitter(Records).run(*Out);
211 FastISelEmitter(Records).run(*Out);
214 SubtargetEmitter(Records).run(*Out);
217 IntrinsicEmitter(Records).run(*Out);
219 case GenTgtIntrinsic:
220 IntrinsicEmitter(Records, true).run(*Out);
223 LLVMCConfigurationEmitter(Records).run(*Out);
227 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
228 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
229 *Out << Recs[i]->getName() << ", ";
234 assert(1 && "Invalid Action");
238 if (Out != cout.stream())
239 delete Out; // Close the file
242 } catch (const TGError &Error) {
243 cerr << argv[0] << ": error:\n";
244 PrintError(Error.getLoc(), Error.getMessage());
246 } catch (const std::string &Error) {
247 cerr << argv[0] << ": " << Error << "\n";
248 } catch (const char *Error) {
249 cerr << argv[0] << ": " << Error << "\n";
251 cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
254 if (Out != cout.stream()) {
255 delete Out; // Close the file
256 std::remove(OutputFilename.c_str()); // Remove the file, it's broken