22c5d50415544d6f446a1e4136d1fde952c9e341
[oota-llvm.git] / utils / TableGen / TableGen.cpp
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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "Record.h"
19 #include "TGParser.h"
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"
38 #include <algorithm>
39 #include <cstdio>
40 #include <fstream>
41 #include <ios>
42 using namespace llvm;
43
44 enum ActionType {
45   PrintRecords,
46   GenEmitter,
47   GenRegisterEnums, GenRegister, GenRegisterHeader,
48   GenInstrEnums, GenInstrs, GenAsmWriter,
49   GenCallingConv,
50   GenClangDiagsDefs,
51   GenDAGISel,
52   GenFastISel,
53   GenSubtarget,
54   GenIntrinsic,
55   GenTgtIntrinsic,
56   GenLLVMCConf,
57   PrintEnums
58 };
59
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(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"),
97                     clEnumValEnd));
98
99   cl::opt<std::string>
100   Class("class", cl::desc("Print Enum list for this class"),
101         cl::value_desc("class name"));
102
103   cl::opt<std::string>
104   OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
105                  cl::init("-"));
106
107   cl::opt<std::string>
108   InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
109
110   cl::list<std::string>
111   IncludeDirs("I", cl::desc("Directory of include files"),
112               cl::value_desc("directory"), cl::Prefix);
113   
114   cl::opt<std::string>
115   ClangComponent("clang-component",
116                  cl::desc("Only use warnings from specified component"),
117                  cl::value_desc("component"), cl::Hidden);
118 }
119
120
121 // FIXME: Eliminate globals from tblgen.
122 RecordKeeper llvm::Records;
123
124 static TGSourceMgr SrcMgr;
125
126 void llvm::PrintError(TGLoc ErrorLoc, const std::string &Msg) {
127   SrcMgr.PrintError(ErrorLoc, Msg);
128 }
129
130
131
132 /// ParseFile - this function begins the parsing of the specified tablegen
133 /// file.
134 static bool ParseFile(const std::string &Filename,
135                       const std::vector<std::string> &IncludeDirs,
136                       TGSourceMgr &SrcMgr) {
137   std::string ErrorStr;
138   MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
139   if (F == 0) {
140     cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
141     return true;
142   }
143   
144   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
145   SrcMgr.AddNewSourceBuffer(F, TGLoc());
146   
147   TGParser Parser(SrcMgr);
148
149   // Record the location of the include directory so that the lexer can find
150   // it later.
151   Parser.setIncludeDirs(IncludeDirs);
152
153   return Parser.ParseFile();
154 }
155
156 int main(int argc, char **argv) {
157   sys::PrintStackTraceOnErrorSignal();
158   PrettyStackTraceProgram X(argc, argv);
159   cl::ParseCommandLineOptions(argc, argv);
160
161   
162   // Parse the input file.
163   if (ParseFile(InputFilename, IncludeDirs, SrcMgr))
164     return 1;
165
166   std::ostream *Out = cout.stream();
167   if (OutputFilename != "-") {
168     Out = new std::ofstream(OutputFilename.c_str());
169
170     if (!Out->good()) {
171       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
172       return 1;
173     }
174
175     // Make sure the file gets removed if *gasp* tablegen crashes...
176     sys::RemoveFileOnSignal(sys::Path(OutputFilename));
177   }
178
179   try {
180     switch (Action) {
181     case PrintRecords:
182       *Out << Records;           // No argument, dump all contents
183       break;
184     case GenEmitter:
185       CodeEmitterGen(Records).run(*Out);
186       break;
187
188     case GenRegisterEnums:
189       RegisterInfoEmitter(Records).runEnums(*Out);
190       break;
191     case GenRegister:
192       RegisterInfoEmitter(Records).run(*Out);
193       break;
194     case GenRegisterHeader:
195       RegisterInfoEmitter(Records).runHeader(*Out);
196       break;
197     case GenInstrEnums:
198       InstrEnumEmitter(Records).run(*Out);
199       break;
200     case GenInstrs:
201       InstrInfoEmitter(Records).run(*Out);
202       break;
203     case GenCallingConv:
204       CallingConvEmitter(Records).run(*Out);
205       break;
206     case GenAsmWriter:
207       AsmWriterEmitter(Records).run(*Out);
208       break;
209     case GenClangDiagsDefs:
210       ClangDiagsDefsEmitter(Records, ClangComponent).run(*Out);
211       break;
212     case GenDAGISel:
213       DAGISelEmitter(Records).run(*Out);
214       break;
215     case GenFastISel:
216       FastISelEmitter(Records).run(*Out);
217       break;
218     case GenSubtarget:
219       SubtargetEmitter(Records).run(*Out);
220       break;
221     case GenIntrinsic:
222       IntrinsicEmitter(Records).run(*Out);
223       break;
224     case GenTgtIntrinsic:
225       IntrinsicEmitter(Records, true).run(*Out);
226       break;
227     case GenLLVMCConf:
228       LLVMCConfigurationEmitter(Records).run(*Out);
229       break;
230     case PrintEnums:
231     {
232       std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
233       for (unsigned i = 0, e = Recs.size(); i != e; ++i)
234         *Out << Recs[i]->getName() << ", ";
235       *Out << "\n";
236       break;
237     }
238     default:
239       assert(1 && "Invalid Action");
240       return 1;
241     }
242     
243     if (Out != cout.stream()) 
244       delete Out;                               // Close the file
245     return 0;
246     
247   } catch (const TGError &Error) {
248     cerr << argv[0] << ": error:\n";
249     PrintError(Error.getLoc(), Error.getMessage());
250     
251   } catch (const std::string &Error) {
252     cerr << argv[0] << ": " << Error << "\n";
253   } catch (const char *Error) {
254     cerr << argv[0] << ": " << Error << "\n";
255   } catch (...) {
256     cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
257   }
258   
259   if (Out != cout.stream()) {
260     delete Out;                             // Close the file
261     std::remove(OutputFilename.c_str());    // Remove the file, it's broken
262   }
263   return 1;
264 }