blob: d6e84609f430aa961d5fd1d35260b3f6a4ce4acc [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "CallingConvEmitter.h"
26#include "CodeEmitterGen.h"
27#include "RegisterInfoEmitter.h"
28#include "InstrInfoEmitter.h"
Chris Lattner030e6e52008-01-06 00:49:05 +000029#include "InstrEnumEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "AsmWriterEmitter.h"
31#include "DAGISelEmitter.h"
Dan Gohmanb75dead2008-08-13 20:19:35 +000032#include "FastISelEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "SubtargetEmitter.h"
34#include "IntrinsicEmitter.h"
Mikhail Glushenkov41405722008-05-06 18:09:29 +000035#include "LLVMCConfigurationEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include <algorithm>
37#include <cstdio>
38#include <fstream>
39#include <ios>
40using namespace llvm;
41
42enum ActionType {
43 PrintRecords,
44 GenEmitter,
45 GenRegisterEnums, GenRegister, GenRegisterHeader,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000046 GenInstrEnums, GenInstrs, GenAsmWriter,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 GenCallingConv,
48 GenDAGISel,
Dan Gohmanb75dead2008-08-13 20:19:35 +000049 GenFastISel,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 GenSubtarget,
51 GenIntrinsic,
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000052 GenLLVMCConf,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 PrintEnums
54};
55
56namespace {
57 cl::opt<ActionType>
58 Action(cl::desc("Action to perform:"),
59 cl::values(clEnumValN(PrintRecords, "print-records",
60 "Print all records to stdout (default)"),
61 clEnumValN(GenEmitter, "gen-emitter",
62 "Generate machine code emitter"),
63 clEnumValN(GenRegisterEnums, "gen-register-enums",
64 "Generate enum values for registers"),
65 clEnumValN(GenRegister, "gen-register-desc",
66 "Generate a register info description"),
67 clEnumValN(GenRegisterHeader, "gen-register-desc-header",
68 "Generate a register info description header"),
69 clEnumValN(GenInstrEnums, "gen-instr-enums",
70 "Generate enum values for instructions"),
71 clEnumValN(GenInstrs, "gen-instr-desc",
72 "Generate instruction descriptions"),
73 clEnumValN(GenCallingConv, "gen-callingconv",
74 "Generate calling convention descriptions"),
75 clEnumValN(GenAsmWriter, "gen-asm-writer",
76 "Generate assembly writer"),
77 clEnumValN(GenDAGISel, "gen-dag-isel",
78 "Generate a DAG instruction selector"),
Dan Gohmanb75dead2008-08-13 20:19:35 +000079 clEnumValN(GenFastISel, "gen-fast-isel",
80 "Generate a \"fast\" instruction selector"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 clEnumValN(GenSubtarget, "gen-subtarget",
82 "Generate subtarget enumerations"),
83 clEnumValN(GenIntrinsic, "gen-intrinsic",
84 "Generate intrinsic information"),
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000085 clEnumValN(GenLLVMCConf, "gen-llvmc",
86 "Generate LLVMC configuration library"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 clEnumValN(PrintEnums, "print-enums",
88 "Print enum values for a class"),
89 clEnumValEnd));
90
91 cl::opt<std::string>
92 Class("class", cl::desc("Print Enum list for this class"),
93 cl::value_desc("class name"));
94
95 cl::opt<std::string>
96 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
97 cl::init("-"));
98
99 cl::opt<std::string>
100 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
101
102 cl::list<std::string>
103 IncludeDirs("I", cl::desc("Directory of include files"),
104 cl::value_desc("directory"), cl::Prefix);
105}
106
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107RecordKeeper llvm::Records;
108
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000109/// ParseFile - this function begins the parsing of the specified tablegen
110/// file.
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000111static bool ParseFile(const std::string &Filename,
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000112 const std::vector<std::string> &IncludeDirs) {
113 std::string ErrorStr;
Chris Lattnerfc003612008-04-01 18:04:03 +0000114 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000115 if (F == 0) {
116 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
117 return true;
118 }
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000119
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000120 TGParser Parser(F);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000121
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000122 // Record the location of the include directory so that the lexer can find
123 // it later.
124 Parser.setIncludeDirs(IncludeDirs);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000125
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000126 return Parser.ParseFile();
127}
128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129int main(int argc, char **argv) {
130 cl::ParseCommandLineOptions(argc, argv);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000131
132 // Parse the input file.
133 if (ParseFile(InputFilename, IncludeDirs))
134 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135
136 std::ostream *Out = cout.stream();
137 if (OutputFilename != "-") {
138 Out = new std::ofstream(OutputFilename.c_str());
139
140 if (!Out->good()) {
141 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
142 return 1;
143 }
144
145 // Make sure the file gets removed if *gasp* tablegen crashes...
146 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
147 }
148
149 try {
150 switch (Action) {
151 case PrintRecords:
152 *Out << Records; // No argument, dump all contents
153 break;
154 case GenEmitter:
155 CodeEmitterGen(Records).run(*Out);
156 break;
157
158 case GenRegisterEnums:
159 RegisterInfoEmitter(Records).runEnums(*Out);
160 break;
161 case GenRegister:
162 RegisterInfoEmitter(Records).run(*Out);
163 break;
164 case GenRegisterHeader:
165 RegisterInfoEmitter(Records).runHeader(*Out);
166 break;
167
168 case GenInstrEnums:
Chris Lattner030e6e52008-01-06 00:49:05 +0000169 InstrEnumEmitter(Records).run(*Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
171 case GenInstrs:
172 InstrInfoEmitter(Records).run(*Out);
173 break;
174 case GenCallingConv:
175 CallingConvEmitter(Records).run(*Out);
176 break;
177 case GenAsmWriter:
178 AsmWriterEmitter(Records).run(*Out);
179 break;
180
181 case GenDAGISel:
182 DAGISelEmitter(Records).run(*Out);
183 break;
Dan Gohmanb75dead2008-08-13 20:19:35 +0000184 case GenFastISel:
185 FastISelEmitter(Records).run(*Out);
186 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 case GenSubtarget:
188 SubtargetEmitter(Records).run(*Out);
189 break;
190 case GenIntrinsic:
191 IntrinsicEmitter(Records).run(*Out);
192 break;
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +0000193 case GenLLVMCConf:
194 LLVMCConfigurationEmitter(Records).run(*Out);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000195 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 case PrintEnums:
197 {
198 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
199 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
200 *Out << Recs[i]->getName() << ", ";
201 *Out << "\n";
202 break;
203 }
204 default:
205 assert(1 && "Invalid Action");
206 return 1;
207 }
208 } catch (const std::string &Error) {
209 cerr << argv[0] << ": " << Error << "\n";
210 if (Out != cout.stream()) {
211 delete Out; // Close the file
212 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
213 }
214 return 1;
215 } catch (...) {
216 cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
217 if (Out != cout.stream()) {
218 delete Out; // Close the file
219 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
220 }
221 return 2;
222 }
223
224 if (Out != cout.stream()) {
225 delete Out; // Close the file
226 }
227 return 0;
228}