Reference to hidden symbols do not have to go through non-lazy pointer in non-pic...
[llvm/avr.git] / utils / TableGen / TableGen.cpp
blobc6d75020c72fce3b4191eeb01cc41432206174a3
1 //===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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 "Record.h"
19 #include "TGParser.h"
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"
39 #include <algorithm>
40 #include <cstdio>
41 using namespace llvm;
43 enum ActionType {
44 PrintRecords,
45 GenEmitter,
46 GenRegisterEnums, GenRegister, GenRegisterHeader,
47 GenInstrEnums, GenInstrs, GenAsmWriter, GenAsmMatcher,
48 GenCallingConv,
49 GenClangDiagsDefs,
50 GenClangDiagGroups,
51 GenDAGISel,
52 GenFastISel,
53 GenSubtarget,
54 GenIntrinsic,
55 GenTgtIntrinsic,
56 GenLLVMCConf,
57 PrintEnums
60 namespace {
61 cl::opt<ActionType>
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"),
101 clEnumValEnd));
103 cl::opt<std::string>
104 Class("class", cl::desc("Print Enum list for this class"),
105 cl::value_desc("class name"));
107 cl::opt<std::string>
108 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
109 cl::init("-"));
111 cl::opt<std::string>
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);
118 cl::opt<std::string>
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
137 /// file.
138 static bool ParseFile(const std::string &Filename,
139 const std::vector<std::string> &IncludeDirs,
140 SourceMgr &SrcMgr) {
141 std::string ErrorStr;
142 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
143 if (F == 0) {
144 errs() << "Could not open input file '" << Filename << "': "
145 << ErrorStr <<"\n";
146 return true;
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
153 // it later.
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))
169 return 1;
171 raw_ostream *Out = &outs();
172 if (OutputFilename != "-") {
173 std::string Error;
174 Out = new raw_fd_ostream(OutputFilename.c_str(), Error);
176 if (!Error.empty()) {
177 errs() << argv[0] << ": error opening " << OutputFilename
178 << ":" << Error << "\n";
179 return 1;
182 // Make sure the file gets removed if *gasp* tablegen crashes...
183 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
186 try {
187 switch (Action) {
188 case PrintRecords:
189 *Out << Records; // No argument, dump all contents
190 break;
191 case GenEmitter:
192 CodeEmitterGen(Records).run(*Out);
193 break;
195 case GenRegisterEnums:
196 RegisterInfoEmitter(Records).runEnums(*Out);
197 break;
198 case GenRegister:
199 RegisterInfoEmitter(Records).run(*Out);
200 break;
201 case GenRegisterHeader:
202 RegisterInfoEmitter(Records).runHeader(*Out);
203 break;
204 case GenInstrEnums:
205 InstrEnumEmitter(Records).run(*Out);
206 break;
207 case GenInstrs:
208 InstrInfoEmitter(Records).run(*Out);
209 break;
210 case GenCallingConv:
211 CallingConvEmitter(Records).run(*Out);
212 break;
213 case GenAsmWriter:
214 AsmWriterEmitter(Records).run(*Out);
215 break;
216 case GenAsmMatcher:
217 AsmMatcherEmitter(Records).run(*Out);
218 break;
219 case GenClangDiagsDefs:
220 ClangDiagsDefsEmitter(Records, ClangComponent).run(*Out);
221 break;
222 case GenClangDiagGroups:
223 ClangDiagGroupsEmitter(Records).run(*Out);
224 break;
225 case GenDAGISel:
226 DAGISelEmitter(Records).run(*Out);
227 break;
228 case GenFastISel:
229 FastISelEmitter(Records).run(*Out);
230 break;
231 case GenSubtarget:
232 SubtargetEmitter(Records).run(*Out);
233 break;
234 case GenIntrinsic:
235 IntrinsicEmitter(Records).run(*Out);
236 break;
237 case GenTgtIntrinsic:
238 IntrinsicEmitter(Records, true).run(*Out);
239 break;
240 case GenLLVMCConf:
241 LLVMCConfigurationEmitter(Records).run(*Out);
242 break;
243 case PrintEnums:
245 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
246 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
247 *Out << Recs[i]->getName() << ", ";
248 *Out << "\n";
249 break;
251 default:
252 assert(1 && "Invalid Action");
253 return 1;
256 if (Out != &outs())
257 delete Out; // Close the file
258 return 0;
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";
268 } catch (...) {
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
276 return 1;