Add initial implementation of a TableGen backend for converting Clang-warnings
[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
115
116 // FIXME: Eliminate globals from tblgen.
117 RecordKeeper llvm::Records;
118
119 static TGSourceMgr SrcMgr;
120
121 void llvm::PrintError(TGLoc ErrorLoc, const std::string &Msg) {
122   SrcMgr.PrintError(ErrorLoc, Msg);
123 }
124
125
126
127 /// ParseFile - this function begins the parsing of the specified tablegen
128 /// file.
129 static bool ParseFile(const std::string &Filename,
130                       const std::vector<std::string> &IncludeDirs,
131                       TGSourceMgr &SrcMgr) {
132   std::string ErrorStr;
133   MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
134   if (F == 0) {
135     cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
136     return true;
137   }
138   
139   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
140   SrcMgr.AddNewSourceBuffer(F, TGLoc());
141   
142   TGParser Parser(SrcMgr);
143
144   // Record the location of the include directory so that the lexer can find
145   // it later.
146   Parser.setIncludeDirs(IncludeDirs);
147
148   return Parser.ParseFile();
149 }
150
151 int main(int argc, char **argv) {
152   sys::PrintStackTraceOnErrorSignal();
153   PrettyStackTraceProgram X(argc, argv);
154   cl::ParseCommandLineOptions(argc, argv);
155
156   
157   // Parse the input file.
158   if (ParseFile(InputFilename, IncludeDirs, SrcMgr))
159     return 1;
160
161   std::ostream *Out = cout.stream();
162   if (OutputFilename != "-") {
163     Out = new std::ofstream(OutputFilename.c_str());
164
165     if (!Out->good()) {
166       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
167       return 1;
168     }
169
170     // Make sure the file gets removed if *gasp* tablegen crashes...
171     sys::RemoveFileOnSignal(sys::Path(OutputFilename));
172   }
173
174   try {
175     switch (Action) {
176     case PrintRecords:
177       *Out << Records;           // No argument, dump all contents
178       break;
179     case GenEmitter:
180       CodeEmitterGen(Records).run(*Out);
181       break;
182
183     case GenRegisterEnums:
184       RegisterInfoEmitter(Records).runEnums(*Out);
185       break;
186     case GenRegister:
187       RegisterInfoEmitter(Records).run(*Out);
188       break;
189     case GenRegisterHeader:
190       RegisterInfoEmitter(Records).runHeader(*Out);
191       break;
192     case GenInstrEnums:
193       InstrEnumEmitter(Records).run(*Out);
194       break;
195     case GenInstrs:
196       InstrInfoEmitter(Records).run(*Out);
197       break;
198     case GenCallingConv:
199       CallingConvEmitter(Records).run(*Out);
200       break;
201     case GenAsmWriter:
202       AsmWriterEmitter(Records).run(*Out);
203       break;
204     case GenClangDiagsDefs:
205       ClangDiagsDefsEmitter(Records).run(*Out);
206       break;
207     case GenDAGISel:
208       DAGISelEmitter(Records).run(*Out);
209       break;
210     case GenFastISel:
211       FastISelEmitter(Records).run(*Out);
212       break;
213     case GenSubtarget:
214       SubtargetEmitter(Records).run(*Out);
215       break;
216     case GenIntrinsic:
217       IntrinsicEmitter(Records).run(*Out);
218       break;
219     case GenTgtIntrinsic:
220       IntrinsicEmitter(Records, true).run(*Out);
221       break;
222     case GenLLVMCConf:
223       LLVMCConfigurationEmitter(Records).run(*Out);
224       break;
225     case PrintEnums:
226     {
227       std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
228       for (unsigned i = 0, e = Recs.size(); i != e; ++i)
229         *Out << Recs[i]->getName() << ", ";
230       *Out << "\n";
231       break;
232     }
233     default:
234       assert(1 && "Invalid Action");
235       return 1;
236     }
237     
238     if (Out != cout.stream()) 
239       delete Out;                               // Close the file
240     return 0;
241     
242   } catch (const TGError &Error) {
243     cerr << argv[0] << ": error:\n";
244     PrintError(Error.getLoc(), Error.getMessage());
245     
246   } catch (const std::string &Error) {
247     cerr << argv[0] << ": " << Error << "\n";
248   } catch (const char *Error) {
249     cerr << argv[0] << ": " << Error << "\n";
250   } catch (...) {
251     cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
252   }
253   
254   if (Out != cout.stream()) {
255     delete Out;                             // Close the file
256     std::remove(OutputFilename.c_str());    // Remove the file, it's broken
257   }
258   return 1;
259 }