blob: c6a1f2e4ab5fa23c16a32f3641f1eab9da924913 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Chris Lattner6b7d76a2007-11-22 20:49:04 +000019#include "TGParser.h"
Chris Lattnerff99dd32009-03-13 07:05:43 +000020#include "TGSourceMgr.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Streams.h"
23#include "llvm/System/Signals.h"
24#include "llvm/Support/FileUtilities.h"
Chris Lattner6b7d76a2007-11-22 20:49:04 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "CallingConvEmitter.h"
28#include "CodeEmitterGen.h"
29#include "RegisterInfoEmitter.h"
30#include "InstrInfoEmitter.h"
Chris Lattner030e6e52008-01-06 00:49:05 +000031#include "InstrEnumEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "AsmWriterEmitter.h"
33#include "DAGISelEmitter.h"
Dan Gohmanb75dead2008-08-13 20:19:35 +000034#include "FastISelEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "SubtargetEmitter.h"
36#include "IntrinsicEmitter.h"
Mikhail Glushenkov41405722008-05-06 18:09:29 +000037#include "LLVMCConfigurationEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include <algorithm>
39#include <cstdio>
40#include <fstream>
41#include <ios>
42using namespace llvm;
43
44enum ActionType {
45 PrintRecords,
46 GenEmitter,
47 GenRegisterEnums, GenRegister, GenRegisterHeader,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000048 GenInstrEnums, GenInstrs, GenAsmWriter,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 GenCallingConv,
50 GenDAGISel,
Dan Gohmanb75dead2008-08-13 20:19:35 +000051 GenFastISel,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 GenSubtarget,
53 GenIntrinsic,
Dale Johannesenfe5921a2009-02-05 01:49:45 +000054 GenTgtIntrinsic,
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000055 GenLLVMCConf,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 PrintEnums
57};
58
59namespace {
60 cl::opt<ActionType>
61 Action(cl::desc("Action to perform:"),
62 cl::values(clEnumValN(PrintRecords, "print-records",
63 "Print all records to stdout (default)"),
64 clEnumValN(GenEmitter, "gen-emitter",
65 "Generate machine code emitter"),
66 clEnumValN(GenRegisterEnums, "gen-register-enums",
67 "Generate enum values for registers"),
68 clEnumValN(GenRegister, "gen-register-desc",
69 "Generate a register info description"),
70 clEnumValN(GenRegisterHeader, "gen-register-desc-header",
71 "Generate a register info description header"),
72 clEnumValN(GenInstrEnums, "gen-instr-enums",
73 "Generate enum values for instructions"),
74 clEnumValN(GenInstrs, "gen-instr-desc",
75 "Generate instruction descriptions"),
76 clEnumValN(GenCallingConv, "gen-callingconv",
77 "Generate calling convention descriptions"),
78 clEnumValN(GenAsmWriter, "gen-asm-writer",
79 "Generate assembly writer"),
80 clEnumValN(GenDAGISel, "gen-dag-isel",
81 "Generate a DAG instruction selector"),
Dan Gohmanb75dead2008-08-13 20:19:35 +000082 clEnumValN(GenFastISel, "gen-fast-isel",
83 "Generate a \"fast\" instruction selector"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 clEnumValN(GenSubtarget, "gen-subtarget",
85 "Generate subtarget enumerations"),
86 clEnumValN(GenIntrinsic, "gen-intrinsic",
87 "Generate intrinsic information"),
Dale Johannesenfe5921a2009-02-05 01:49:45 +000088 clEnumValN(GenTgtIntrinsic, "gen-tgt-intrinsic",
89 "Generate target intrinsic information"),
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000090 clEnumValN(GenLLVMCConf, "gen-llvmc",
91 "Generate LLVMC configuration library"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 clEnumValN(PrintEnums, "print-enums",
93 "Print enum values for a class"),
94 clEnumValEnd));
95
96 cl::opt<std::string>
97 Class("class", cl::desc("Print Enum list for this class"),
98 cl::value_desc("class name"));
99
100 cl::opt<std::string>
101 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
102 cl::init("-"));
103
104 cl::opt<std::string>
105 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
106
107 cl::list<std::string>
108 IncludeDirs("I", cl::desc("Directory of include files"),
109 cl::value_desc("directory"), cl::Prefix);
110}
111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112RecordKeeper llvm::Records;
113
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000114/// ParseFile - this function begins the parsing of the specified tablegen
115/// file.
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000116static bool ParseFile(const std::string &Filename,
Chris Lattnerff99dd32009-03-13 07:05:43 +0000117 const std::vector<std::string> &IncludeDirs,
118 TGSourceMgr &SrcMgr) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000119 std::string ErrorStr;
Chris Lattnerfc003612008-04-01 18:04:03 +0000120 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000121 if (F == 0) {
122 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
123 return true;
124 }
Chris Lattnerff99dd32009-03-13 07:05:43 +0000125
126 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
127 SrcMgr.AddNewSourceBuffer(F, TGLocTy());
128
129 TGParser Parser(SrcMgr);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000130
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000131 // Record the location of the include directory so that the lexer can find
132 // it later.
133 Parser.setIncludeDirs(IncludeDirs);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000134
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000135 return Parser.ParseFile();
136}
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000139 sys::PrintStackTraceOnErrorSignal();
140 PrettyStackTraceProgram X(argc, argv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 cl::ParseCommandLineOptions(argc, argv);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000142
Chris Lattnerff99dd32009-03-13 07:05:43 +0000143 TGSourceMgr SrcMgr;
144
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000145 // Parse the input file.
Chris Lattnerff99dd32009-03-13 07:05:43 +0000146 if (ParseFile(InputFilename, IncludeDirs, SrcMgr))
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000147 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148
149 std::ostream *Out = cout.stream();
150 if (OutputFilename != "-") {
151 Out = new std::ofstream(OutputFilename.c_str());
152
153 if (!Out->good()) {
154 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
155 return 1;
156 }
157
158 // Make sure the file gets removed if *gasp* tablegen crashes...
159 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
160 }
161
162 try {
163 switch (Action) {
164 case PrintRecords:
165 *Out << Records; // No argument, dump all contents
166 break;
167 case GenEmitter:
168 CodeEmitterGen(Records).run(*Out);
169 break;
170
171 case GenRegisterEnums:
172 RegisterInfoEmitter(Records).runEnums(*Out);
173 break;
174 case GenRegister:
175 RegisterInfoEmitter(Records).run(*Out);
176 break;
177 case GenRegisterHeader:
178 RegisterInfoEmitter(Records).runHeader(*Out);
179 break;
180
181 case GenInstrEnums:
Chris Lattner030e6e52008-01-06 00:49:05 +0000182 InstrEnumEmitter(Records).run(*Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 break;
184 case GenInstrs:
185 InstrInfoEmitter(Records).run(*Out);
186 break;
187 case GenCallingConv:
188 CallingConvEmitter(Records).run(*Out);
189 break;
190 case GenAsmWriter:
191 AsmWriterEmitter(Records).run(*Out);
192 break;
193
194 case GenDAGISel:
195 DAGISelEmitter(Records).run(*Out);
196 break;
Dan Gohmanb75dead2008-08-13 20:19:35 +0000197 case GenFastISel:
198 FastISelEmitter(Records).run(*Out);
199 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 case GenSubtarget:
201 SubtargetEmitter(Records).run(*Out);
202 break;
203 case GenIntrinsic:
204 IntrinsicEmitter(Records).run(*Out);
205 break;
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000206 case GenTgtIntrinsic:
207 IntrinsicEmitter(Records, true).run(*Out);
208 break;
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +0000209 case GenLLVMCConf:
210 LLVMCConfigurationEmitter(Records).run(*Out);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000211 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 case PrintEnums:
213 {
214 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
215 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
216 *Out << Recs[i]->getName() << ", ";
217 *Out << "\n";
218 break;
219 }
220 default:
221 assert(1 && "Invalid Action");
222 return 1;
223 }
224 } catch (const std::string &Error) {
225 cerr << argv[0] << ": " << Error << "\n";
226 if (Out != cout.stream()) {
227 delete Out; // Close the file
228 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
229 }
230 return 1;
Dan Gohman5679ab92008-11-07 21:01:13 +0000231 } catch (const char *Error) {
232 cerr << argv[0] << ": " << Error << "\n";
233 if (Out != cout.stream()) {
234 delete Out; // Close the file
235 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
236 }
237 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 } catch (...) {
239 cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
240 if (Out != cout.stream()) {
241 delete Out; // Close the file
242 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
243 }
244 return 2;
245 }
246
247 if (Out != cout.stream()) {
248 delete Out; // Close the file
249 }
250 return 0;
251}