blob: 8cbba22069bdd6fc88cef93d5893769e1ad29060 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Streams.h"
22#include "llvm/System/Signals.h"
23#include "llvm/Support/FileUtilities.h"
Chris Lattner6b7d76a2007-11-22 20:49:04 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "CallingConvEmitter.h"
27#include "CodeEmitterGen.h"
28#include "RegisterInfoEmitter.h"
29#include "InstrInfoEmitter.h"
Chris Lattner030e6e52008-01-06 00:49:05 +000030#include "InstrEnumEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "AsmWriterEmitter.h"
32#include "DAGISelEmitter.h"
Dan Gohmanb75dead2008-08-13 20:19:35 +000033#include "FastISelEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "SubtargetEmitter.h"
35#include "IntrinsicEmitter.h"
Mikhail Glushenkov41405722008-05-06 18:09:29 +000036#include "LLVMCConfigurationEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include <algorithm>
38#include <cstdio>
39#include <fstream>
40#include <ios>
41using namespace llvm;
42
43enum ActionType {
44 PrintRecords,
45 GenEmitter,
46 GenRegisterEnums, GenRegister, GenRegisterHeader,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000047 GenInstrEnums, GenInstrs, GenAsmWriter,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 GenCallingConv,
49 GenDAGISel,
Dan Gohmanb75dead2008-08-13 20:19:35 +000050 GenFastISel,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 GenSubtarget,
52 GenIntrinsic,
Dale Johannesenfe5921a2009-02-05 01:49:45 +000053 GenTgtIntrinsic,
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000054 GenLLVMCConf,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 PrintEnums
56};
57
58namespace {
59 cl::opt<ActionType>
60 Action(cl::desc("Action to perform:"),
61 cl::values(clEnumValN(PrintRecords, "print-records",
62 "Print all records to stdout (default)"),
63 clEnumValN(GenEmitter, "gen-emitter",
64 "Generate machine code emitter"),
65 clEnumValN(GenRegisterEnums, "gen-register-enums",
66 "Generate enum values for registers"),
67 clEnumValN(GenRegister, "gen-register-desc",
68 "Generate a register info description"),
69 clEnumValN(GenRegisterHeader, "gen-register-desc-header",
70 "Generate a register info description header"),
71 clEnumValN(GenInstrEnums, "gen-instr-enums",
72 "Generate enum values for instructions"),
73 clEnumValN(GenInstrs, "gen-instr-desc",
74 "Generate instruction descriptions"),
75 clEnumValN(GenCallingConv, "gen-callingconv",
76 "Generate calling convention descriptions"),
77 clEnumValN(GenAsmWriter, "gen-asm-writer",
78 "Generate assembly writer"),
79 clEnumValN(GenDAGISel, "gen-dag-isel",
80 "Generate a DAG instruction selector"),
Dan Gohmanb75dead2008-08-13 20:19:35 +000081 clEnumValN(GenFastISel, "gen-fast-isel",
82 "Generate a \"fast\" instruction selector"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 clEnumValN(GenSubtarget, "gen-subtarget",
84 "Generate subtarget enumerations"),
85 clEnumValN(GenIntrinsic, "gen-intrinsic",
86 "Generate intrinsic information"),
Dale Johannesenfe5921a2009-02-05 01:49:45 +000087 clEnumValN(GenTgtIntrinsic, "gen-tgt-intrinsic",
88 "Generate target intrinsic information"),
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000089 clEnumValN(GenLLVMCConf, "gen-llvmc",
90 "Generate LLVMC configuration library"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 clEnumValN(PrintEnums, "print-enums",
92 "Print enum values for a class"),
93 clEnumValEnd));
94
95 cl::opt<std::string>
96 Class("class", cl::desc("Print Enum list for this class"),
97 cl::value_desc("class name"));
98
99 cl::opt<std::string>
100 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
101 cl::init("-"));
102
103 cl::opt<std::string>
104 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
105
106 cl::list<std::string>
107 IncludeDirs("I", cl::desc("Directory of include files"),
108 cl::value_desc("directory"), cl::Prefix);
109}
110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111RecordKeeper llvm::Records;
112
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000113/// ParseFile - this function begins the parsing of the specified tablegen
114/// file.
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000115static bool ParseFile(const std::string &Filename,
Chris Lattnerff99dd32009-03-13 07:05:43 +0000116 const std::vector<std::string> &IncludeDirs,
117 TGSourceMgr &SrcMgr) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000118 std::string ErrorStr;
Chris Lattnerfc003612008-04-01 18:04:03 +0000119 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000120 if (F == 0) {
121 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
122 return true;
123 }
Chris Lattnerff99dd32009-03-13 07:05:43 +0000124
125 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
Chris Lattnere7205cd2009-03-13 16:01:53 +0000126 SrcMgr.AddNewSourceBuffer(F, TGLoc());
Chris Lattnerff99dd32009-03-13 07:05:43 +0000127
128 TGParser Parser(SrcMgr);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000129
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000130 // Record the location of the include directory so that the lexer can find
131 // it later.
132 Parser.setIncludeDirs(IncludeDirs);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000133
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000134 return Parser.ParseFile();
135}
136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000138 sys::PrintStackTraceOnErrorSignal();
139 PrettyStackTraceProgram X(argc, argv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 cl::ParseCommandLineOptions(argc, argv);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000141
Chris Lattnerff99dd32009-03-13 07:05:43 +0000142 TGSourceMgr SrcMgr;
143
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000144 // Parse the input file.
Chris Lattnerff99dd32009-03-13 07:05:43 +0000145 if (ParseFile(InputFilename, IncludeDirs, SrcMgr))
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000146 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
148 std::ostream *Out = cout.stream();
149 if (OutputFilename != "-") {
150 Out = new std::ofstream(OutputFilename.c_str());
151
152 if (!Out->good()) {
153 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
154 return 1;
155 }
156
157 // Make sure the file gets removed if *gasp* tablegen crashes...
158 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
159 }
160
161 try {
162 switch (Action) {
163 case PrintRecords:
164 *Out << Records; // No argument, dump all contents
165 break;
166 case GenEmitter:
167 CodeEmitterGen(Records).run(*Out);
168 break;
169
170 case GenRegisterEnums:
171 RegisterInfoEmitter(Records).runEnums(*Out);
172 break;
173 case GenRegister:
174 RegisterInfoEmitter(Records).run(*Out);
175 break;
176 case GenRegisterHeader:
177 RegisterInfoEmitter(Records).runHeader(*Out);
178 break;
179
180 case GenInstrEnums:
Chris Lattner030e6e52008-01-06 00:49:05 +0000181 InstrEnumEmitter(Records).run(*Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 break;
183 case GenInstrs:
184 InstrInfoEmitter(Records).run(*Out);
185 break;
186 case GenCallingConv:
187 CallingConvEmitter(Records).run(*Out);
188 break;
189 case GenAsmWriter:
190 AsmWriterEmitter(Records).run(*Out);
191 break;
192
193 case GenDAGISel:
194 DAGISelEmitter(Records).run(*Out);
195 break;
Dan Gohmanb75dead2008-08-13 20:19:35 +0000196 case GenFastISel:
197 FastISelEmitter(Records).run(*Out);
198 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 case GenSubtarget:
200 SubtargetEmitter(Records).run(*Out);
201 break;
202 case GenIntrinsic:
203 IntrinsicEmitter(Records).run(*Out);
204 break;
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000205 case GenTgtIntrinsic:
206 IntrinsicEmitter(Records, true).run(*Out);
207 break;
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +0000208 case GenLLVMCConf:
209 LLVMCConfigurationEmitter(Records).run(*Out);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000210 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 case PrintEnums:
212 {
213 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
214 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
215 *Out << Recs[i]->getName() << ", ";
216 *Out << "\n";
217 break;
218 }
219 default:
220 assert(1 && "Invalid Action");
221 return 1;
222 }
223 } catch (const std::string &Error) {
224 cerr << argv[0] << ": " << Error << "\n";
225 if (Out != cout.stream()) {
226 delete Out; // Close the file
227 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
228 }
229 return 1;
Dan Gohman5679ab92008-11-07 21:01:13 +0000230 } catch (const char *Error) {
231 cerr << argv[0] << ": " << Error << "\n";
232 if (Out != cout.stream()) {
233 delete Out; // Close the file
234 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
235 }
236 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 } catch (...) {
238 cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
239 if (Out != cout.stream()) {
240 delete Out; // Close the file
241 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
242 }
243 return 2;
244 }
245
246 if (Out != cout.stream()) {
247 delete Out; // Close the file
248 }
249 return 0;
250}