Fix part 1 of pr4682. PICADD is a 16-bit instruction even in thumb2 mode.
[llvm/avr.git] / utils / TableGen / TableGen.cpp
blob315bb657a07b079072a149e56e2195df041897d3
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(), /*Binary=*/false,
175 /*Force=*/true, Error);
177 if (!Error.empty()) {
178 errs() << argv[0] << ": error opening " << OutputFilename
179 << ":" << Error << "\n";
180 return 1;
183 // Make sure the file gets removed if *gasp* tablegen crashes...
184 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
187 try {
188 switch (Action) {
189 case PrintRecords:
190 *Out << Records; // No argument, dump all contents
191 break;
192 case GenEmitter:
193 CodeEmitterGen(Records).run(*Out);
194 break;
196 case GenRegisterEnums:
197 RegisterInfoEmitter(Records).runEnums(*Out);
198 break;
199 case GenRegister:
200 RegisterInfoEmitter(Records).run(*Out);
201 break;
202 case GenRegisterHeader:
203 RegisterInfoEmitter(Records).runHeader(*Out);
204 break;
205 case GenInstrEnums:
206 InstrEnumEmitter(Records).run(*Out);
207 break;
208 case GenInstrs:
209 InstrInfoEmitter(Records).run(*Out);
210 break;
211 case GenCallingConv:
212 CallingConvEmitter(Records).run(*Out);
213 break;
214 case GenAsmWriter:
215 AsmWriterEmitter(Records).run(*Out);
216 break;
217 case GenAsmMatcher:
218 AsmMatcherEmitter(Records).run(*Out);
219 break;
220 case GenClangDiagsDefs:
221 ClangDiagsDefsEmitter(Records, ClangComponent).run(*Out);
222 break;
223 case GenClangDiagGroups:
224 ClangDiagGroupsEmitter(Records).run(*Out);
225 break;
226 case GenDAGISel:
227 DAGISelEmitter(Records).run(*Out);
228 break;
229 case GenFastISel:
230 FastISelEmitter(Records).run(*Out);
231 break;
232 case GenSubtarget:
233 SubtargetEmitter(Records).run(*Out);
234 break;
235 case GenIntrinsic:
236 IntrinsicEmitter(Records).run(*Out);
237 break;
238 case GenTgtIntrinsic:
239 IntrinsicEmitter(Records, true).run(*Out);
240 break;
241 case GenLLVMCConf:
242 LLVMCConfigurationEmitter(Records).run(*Out);
243 break;
244 case PrintEnums:
246 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
247 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
248 *Out << Recs[i]->getName() << ", ";
249 *Out << "\n";
250 break;
252 default:
253 assert(1 && "Invalid Action");
254 return 1;
257 if (Out != &outs())
258 delete Out; // Close the file
259 return 0;
261 } catch (const TGError &Error) {
262 errs() << argv[0] << ": error:\n";
263 PrintError(Error.getLoc(), Error.getMessage());
265 } catch (const std::string &Error) {
266 errs() << argv[0] << ": " << Error << "\n";
267 } catch (const char *Error) {
268 errs() << argv[0] << ": " << Error << "\n";
269 } catch (...) {
270 errs() << argv[0] << ": Unknown unexpected exception occurred.\n";
273 if (Out != &outs()) {
274 delete Out; // Close the file
275 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
277 return 1;