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 //===----------------------------------------------------------------------===//
18 #include "AsmMatcherEmitter.h"
19 #include "AsmWriterEmitter.h"
20 #include "CallingConvEmitter.h"
21 #include "ClangASTNodesEmitter.h"
22 #include "ClangAttrEmitter.h"
23 #include "ClangDiagnosticsEmitter.h"
24 #include "ClangSACheckersEmitter.h"
25 #include "CodeEmitterGen.h"
26 #include "DAGISelEmitter.h"
27 #include "DisassemblerEmitter.h"
28 #include "EDEmitter.h"
29 #include "FastISelEmitter.h"
30 #include "InstrEnumEmitter.h"
31 #include "InstrInfoEmitter.h"
32 #include "IntrinsicEmitter.h"
33 #include "LLVMCConfigurationEmitter.h"
34 #include "NeonEmitter.h"
35 #include "OptParserEmitter.h"
37 #include "RegisterInfoEmitter.h"
38 #include "ARMDecoderEmitter.h"
39 #include "SubtargetEmitter.h"
41 #include "llvm/ADT/OwningPtr.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/PrettyStackTrace.h"
45 #include "llvm/Support/ToolOutputFile.h"
46 #include "llvm/Support/Signals.h"
47 #include "llvm/Support/system_error.h"
55 GenRegisterEnums
, GenRegister
, GenRegisterHeader
,
56 GenInstrEnums
, GenInstrs
, GenAsmWriter
, GenAsmMatcher
,
65 GenClangAttrSpellingList
,
73 GenOptParserDefs
, GenOptParserImpl
,
87 Action(cl::desc("Action to perform:"),
88 cl::values(clEnumValN(PrintRecords
, "print-records",
89 "Print all records to stdout (default)"),
90 clEnumValN(GenEmitter
, "gen-emitter",
91 "Generate machine code emitter"),
92 clEnumValN(GenRegisterEnums
, "gen-register-enums",
93 "Generate enum values for registers"),
94 clEnumValN(GenRegister
, "gen-register-desc",
95 "Generate a register info description"),
96 clEnumValN(GenRegisterHeader
, "gen-register-desc-header",
97 "Generate a register info description header"),
98 clEnumValN(GenInstrEnums
, "gen-instr-enums",
99 "Generate enum values for instructions"),
100 clEnumValN(GenInstrs
, "gen-instr-desc",
101 "Generate instruction descriptions"),
102 clEnumValN(GenCallingConv
, "gen-callingconv",
103 "Generate calling convention descriptions"),
104 clEnumValN(GenAsmWriter
, "gen-asm-writer",
105 "Generate assembly writer"),
106 clEnumValN(GenARMDecoder
, "gen-arm-decoder",
107 "Generate decoders for ARM/Thumb"),
108 clEnumValN(GenDisassembler
, "gen-disassembler",
109 "Generate disassembler"),
110 clEnumValN(GenAsmMatcher
, "gen-asm-matcher",
111 "Generate assembly instruction matcher"),
112 clEnumValN(GenDAGISel
, "gen-dag-isel",
113 "Generate a DAG instruction selector"),
114 clEnumValN(GenFastISel
, "gen-fast-isel",
115 "Generate a \"fast\" instruction selector"),
116 clEnumValN(GenOptParserDefs
, "gen-opt-parser-defs",
117 "Generate option definitions"),
118 clEnumValN(GenOptParserImpl
, "gen-opt-parser-impl",
119 "Generate option parser implementation"),
120 clEnumValN(GenSubtarget
, "gen-subtarget",
121 "Generate subtarget enumerations"),
122 clEnumValN(GenIntrinsic
, "gen-intrinsic",
123 "Generate intrinsic information"),
124 clEnumValN(GenTgtIntrinsic
, "gen-tgt-intrinsic",
125 "Generate target intrinsic information"),
126 clEnumValN(GenClangAttrClasses
, "gen-clang-attr-classes",
127 "Generate clang attribute clases"),
128 clEnumValN(GenClangAttrImpl
, "gen-clang-attr-impl",
129 "Generate clang attribute implementations"),
130 clEnumValN(GenClangAttrList
, "gen-clang-attr-list",
131 "Generate a clang attribute list"),
132 clEnumValN(GenClangAttrPCHRead
, "gen-clang-attr-pch-read",
133 "Generate clang PCH attribute reader"),
134 clEnumValN(GenClangAttrPCHWrite
, "gen-clang-attr-pch-write",
135 "Generate clang PCH attribute writer"),
136 clEnumValN(GenClangAttrSpellingList
, "gen-clang-attr-spelling-list",
137 "Generate a clang attribute spelling list"),
138 clEnumValN(GenClangDiagsDefs
, "gen-clang-diags-defs",
139 "Generate Clang diagnostics definitions"),
140 clEnumValN(GenClangDiagGroups
, "gen-clang-diag-groups",
141 "Generate Clang diagnostic groups"),
142 clEnumValN(GenClangDeclNodes
, "gen-clang-decl-nodes",
143 "Generate Clang AST declaration nodes"),
144 clEnumValN(GenClangStmtNodes
, "gen-clang-stmt-nodes",
145 "Generate Clang AST statement nodes"),
146 clEnumValN(GenClangSACheckers
, "gen-clang-sa-checkers",
147 "Generate Clang Static Analyzer checkers"),
148 clEnumValN(GenLLVMCConf
, "gen-llvmc",
149 "Generate LLVMC configuration library"),
150 clEnumValN(GenEDInfo
, "gen-enhanced-disassembly-info",
151 "Generate enhanced disassembly info"),
152 clEnumValN(GenArmNeon
, "gen-arm-neon",
153 "Generate arm_neon.h for clang"),
154 clEnumValN(GenArmNeonSema
, "gen-arm-neon-sema",
155 "Generate ARM NEON sema support for clang"),
156 clEnumValN(GenArmNeonTest
, "gen-arm-neon-test",
157 "Generate ARM NEON tests for clang"),
158 clEnumValN(PrintEnums
, "print-enums",
159 "Print enum values for a class"),
163 Class("class", cl::desc("Print Enum list for this class"),
164 cl::value_desc("class name"));
167 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
171 InputFilename(cl::Positional
, cl::desc("<input file>"), cl::init("-"));
173 cl::list
<std::string
>
174 IncludeDirs("I", cl::desc("Directory of include files"),
175 cl::value_desc("directory"), cl::Prefix
);
178 ClangComponent("clang-component",
179 cl::desc("Only use warnings from specified component"),
180 cl::value_desc("component"), cl::Hidden
);
184 static SourceMgr SrcMgr
;
186 void llvm::PrintError(SMLoc ErrorLoc
, const Twine
&Msg
) {
187 SrcMgr
.PrintMessage(ErrorLoc
, Msg
, "error");
192 /// ParseFile - this function begins the parsing of the specified tablegen
194 static bool ParseFile(const std::string
&Filename
,
195 const std::vector
<std::string
> &IncludeDirs
,
197 RecordKeeper
&Records
) {
198 OwningPtr
<MemoryBuffer
> File
;
199 if (error_code ec
= MemoryBuffer::getFileOrSTDIN(Filename
.c_str(), File
)) {
200 errs() << "Could not open input file '" << Filename
<< "': "
201 << ec
.message() <<"\n";
204 MemoryBuffer
*F
= File
.take();
206 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
207 SrcMgr
.AddNewSourceBuffer(F
, SMLoc());
209 // Record the location of the include directory so that the lexer can find
211 SrcMgr
.setIncludeDirs(IncludeDirs
);
213 TGParser
Parser(SrcMgr
, Records
);
215 return Parser
.ParseFile();
218 int main(int argc
, char **argv
) {
219 RecordKeeper Records
;
221 sys::PrintStackTraceOnErrorSignal();
222 PrettyStackTraceProgram
X(argc
, argv
);
223 cl::ParseCommandLineOptions(argc
, argv
);
226 // Parse the input file.
227 if (ParseFile(InputFilename
, IncludeDirs
, SrcMgr
, Records
))
231 tool_output_file
Out(OutputFilename
.c_str(), Error
);
232 if (!Error
.empty()) {
233 errs() << argv
[0] << ": error opening " << OutputFilename
234 << ":" << Error
<< "\n";
241 Out
.os() << Records
; // No argument, dump all contents
244 CodeEmitterGen(Records
).run(Out
.os());
247 case GenRegisterEnums
:
248 RegisterInfoEmitter(Records
).runEnums(Out
.os());
251 RegisterInfoEmitter(Records
).run(Out
.os());
253 case GenRegisterHeader
:
254 RegisterInfoEmitter(Records
).runHeader(Out
.os());
257 InstrEnumEmitter(Records
).run(Out
.os());
260 InstrInfoEmitter(Records
).run(Out
.os());
263 CallingConvEmitter(Records
).run(Out
.os());
266 AsmWriterEmitter(Records
).run(Out
.os());
269 ARMDecoderEmitter(Records
).run(Out
.os());
272 AsmMatcherEmitter(Records
).run(Out
.os());
274 case GenClangAttrClasses
:
275 ClangAttrClassEmitter(Records
).run(Out
.os());
277 case GenClangAttrImpl
:
278 ClangAttrImplEmitter(Records
).run(Out
.os());
280 case GenClangAttrList
:
281 ClangAttrListEmitter(Records
).run(Out
.os());
283 case GenClangAttrPCHRead
:
284 ClangAttrPCHReadEmitter(Records
).run(Out
.os());
286 case GenClangAttrPCHWrite
:
287 ClangAttrPCHWriteEmitter(Records
).run(Out
.os());
289 case GenClangAttrSpellingList
:
290 ClangAttrSpellingListEmitter(Records
).run(Out
.os());
292 case GenClangDiagsDefs
:
293 ClangDiagsDefsEmitter(Records
, ClangComponent
).run(Out
.os());
295 case GenClangDiagGroups
:
296 ClangDiagGroupsEmitter(Records
).run(Out
.os());
298 case GenClangDeclNodes
:
299 ClangASTNodesEmitter(Records
, "Decl", "Decl").run(Out
.os());
300 ClangDeclContextEmitter(Records
).run(Out
.os());
302 case GenClangStmtNodes
:
303 ClangASTNodesEmitter(Records
, "Stmt", "").run(Out
.os());
305 case GenClangSACheckers
:
306 ClangSACheckersEmitter(Records
).run(Out
.os());
308 case GenDisassembler
:
309 DisassemblerEmitter(Records
).run(Out
.os());
311 case GenOptParserDefs
:
312 OptParserEmitter(Records
, true).run(Out
.os());
314 case GenOptParserImpl
:
315 OptParserEmitter(Records
, false).run(Out
.os());
318 DAGISelEmitter(Records
).run(Out
.os());
321 FastISelEmitter(Records
).run(Out
.os());
324 SubtargetEmitter(Records
).run(Out
.os());
327 IntrinsicEmitter(Records
).run(Out
.os());
329 case GenTgtIntrinsic
:
330 IntrinsicEmitter(Records
, true).run(Out
.os());
333 LLVMCConfigurationEmitter(Records
).run(Out
.os());
336 EDEmitter(Records
).run(Out
.os());
339 NeonEmitter(Records
).run(Out
.os());
342 NeonEmitter(Records
).runHeader(Out
.os());
345 NeonEmitter(Records
).runTests(Out
.os());
349 std::vector
<Record
*> Recs
= Records
.getAllDerivedDefinitions(Class
);
350 for (unsigned i
= 0, e
= Recs
.size(); i
!= e
; ++i
)
351 Out
.os() << Recs
[i
]->getName() << ", ";
356 assert(1 && "Invalid Action");
364 } catch (const TGError
&Error
) {
365 errs() << argv
[0] << ": error:\n";
366 PrintError(Error
.getLoc(), Error
.getMessage());
368 } catch (const std::string
&Error
) {
369 errs() << argv
[0] << ": " << Error
<< "\n";
370 } catch (const char *Error
) {
371 errs() << argv
[0] << ": " << Error
<< "\n";
373 errs() << argv
[0] << ": Unknown unexpected exception occurred.\n";