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 "CallingConvEmitter.h"
21 #include "CodeEmitterGen.h"
22 #include "RegisterInfoEmitter.h"
23 #include "InstrInfoEmitter.h"
24 #include "InstrEnumEmitter.h"
25 #include "AsmWriterEmitter.h"
26 #include "AsmMatcherEmitter.h"
27 #include "DAGISelEmitter.h"
28 #include "FastISelEmitter.h"
29 #include "SubtargetEmitter.h"
30 #include "IntrinsicEmitter.h"
31 #include "LLVMCConfigurationEmitter.h"
32 #include "ClangDiagnosticsEmitter.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/FileUtilities.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/PrettyStackTrace.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/System/Signals.h"
46 GenRegisterEnums
, GenRegister
, GenRegisterHeader
,
47 GenInstrEnums
, GenInstrs
, GenAsmWriter
, GenAsmMatcher
,
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(GenAsmMatcher
, "gen-asm-matcher",
82 "Generate assembly instruction matcher"),
83 clEnumValN(GenDAGISel
, "gen-dag-isel",
84 "Generate a DAG instruction selector"),
85 clEnumValN(GenFastISel
, "gen-fast-isel",
86 "Generate a \"fast\" instruction selector"),
87 clEnumValN(GenSubtarget
, "gen-subtarget",
88 "Generate subtarget enumerations"),
89 clEnumValN(GenIntrinsic
, "gen-intrinsic",
90 "Generate intrinsic information"),
91 clEnumValN(GenTgtIntrinsic
, "gen-tgt-intrinsic",
92 "Generate target intrinsic information"),
93 clEnumValN(GenClangDiagsDefs
, "gen-clang-diags-defs",
94 "Generate Clang diagnostics definitions"),
95 clEnumValN(GenClangDiagGroups
, "gen-clang-diag-groups",
96 "Generate Clang diagnostic groups"),
97 clEnumValN(GenLLVMCConf
, "gen-llvmc",
98 "Generate LLVMC configuration library"),
99 clEnumValN(PrintEnums
, "print-enums",
100 "Print enum values for a class"),
104 Class("class", cl::desc("Print Enum list for this class"),
105 cl::value_desc("class name"));
108 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
112 InputFilename(cl::Positional
, cl::desc("<input file>"), cl::init("-"));
114 cl::list
<std::string
>
115 IncludeDirs("I", cl::desc("Directory of include files"),
116 cl::value_desc("directory"), cl::Prefix
);
119 ClangComponent("clang-component",
120 cl::desc("Only use warnings from specified component"),
121 cl::value_desc("component"), cl::Hidden
);
125 // FIXME: Eliminate globals from tblgen.
126 RecordKeeper
llvm::Records
;
128 static SourceMgr SrcMgr
;
130 void llvm::PrintError(SMLoc ErrorLoc
, const std::string
&Msg
) {
131 SrcMgr
.PrintMessage(ErrorLoc
, Msg
, "error");
136 /// ParseFile - this function begins the parsing of the specified tablegen
138 static bool ParseFile(const std::string
&Filename
,
139 const std::vector
<std::string
> &IncludeDirs
,
141 std::string ErrorStr
;
142 MemoryBuffer
*F
= MemoryBuffer::getFileOrSTDIN(Filename
.c_str(), &ErrorStr
);
144 errs() << "Could not open input file '" << Filename
<< "': "
149 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
150 SrcMgr
.AddNewSourceBuffer(F
, SMLoc());
152 // Record the location of the include directory so that the lexer can find
154 SrcMgr
.setIncludeDirs(IncludeDirs
);
156 TGParser
Parser(SrcMgr
);
158 return Parser
.ParseFile();
161 int main(int argc
, char **argv
) {
162 sys::PrintStackTraceOnErrorSignal();
163 PrettyStackTraceProgram
X(argc
, argv
);
164 cl::ParseCommandLineOptions(argc
, argv
);
167 // Parse the input file.
168 if (ParseFile(InputFilename
, IncludeDirs
, SrcMgr
))
171 raw_ostream
*Out
= &outs();
172 if (OutputFilename
!= "-") {
174 Out
= new raw_fd_ostream(OutputFilename
.c_str(), Error
);
176 if (!Error
.empty()) {
177 errs() << argv
[0] << ": error opening " << OutputFilename
178 << ":" << Error
<< "\n";
182 // Make sure the file gets removed if *gasp* tablegen crashes...
183 sys::RemoveFileOnSignal(sys::Path(OutputFilename
));
189 *Out
<< Records
; // No argument, dump all contents
192 CodeEmitterGen(Records
).run(*Out
);
195 case GenRegisterEnums
:
196 RegisterInfoEmitter(Records
).runEnums(*Out
);
199 RegisterInfoEmitter(Records
).run(*Out
);
201 case GenRegisterHeader
:
202 RegisterInfoEmitter(Records
).runHeader(*Out
);
205 InstrEnumEmitter(Records
).run(*Out
);
208 InstrInfoEmitter(Records
).run(*Out
);
211 CallingConvEmitter(Records
).run(*Out
);
214 AsmWriterEmitter(Records
).run(*Out
);
217 AsmMatcherEmitter(Records
).run(*Out
);
219 case GenClangDiagsDefs
:
220 ClangDiagsDefsEmitter(Records
, ClangComponent
).run(*Out
);
222 case GenClangDiagGroups
:
223 ClangDiagGroupsEmitter(Records
).run(*Out
);
226 DAGISelEmitter(Records
).run(*Out
);
229 FastISelEmitter(Records
).run(*Out
);
232 SubtargetEmitter(Records
).run(*Out
);
235 IntrinsicEmitter(Records
).run(*Out
);
237 case GenTgtIntrinsic
:
238 IntrinsicEmitter(Records
, true).run(*Out
);
241 LLVMCConfigurationEmitter(Records
).run(*Out
);
245 std::vector
<Record
*> Recs
= Records
.getAllDerivedDefinitions(Class
);
246 for (unsigned i
= 0, e
= Recs
.size(); i
!= e
; ++i
)
247 *Out
<< Recs
[i
]->getName() << ", ";
252 assert(1 && "Invalid Action");
257 delete Out
; // Close the file
260 } catch (const TGError
&Error
) {
261 errs() << argv
[0] << ": error:\n";
262 PrintError(Error
.getLoc(), Error
.getMessage());
264 } catch (const std::string
&Error
) {
265 errs() << argv
[0] << ": " << Error
<< "\n";
266 } catch (const char *Error
) {
267 errs() << argv
[0] << ": " << Error
<< "\n";
269 errs() << argv
[0] << ": Unknown unexpected exception occurred.\n";
272 if (Out
!= &outs()) {
273 delete Out
; // Close the file
274 std::remove(OutputFilename
.c_str()); // Remove the file, it's broken