blob: 1bc2790f093d6a1fafd94167f4c111f7258c6c86 [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"
32#include "SubtargetEmitter.h"
33#include "IntrinsicEmitter.h"
Mikhail Glushenkov41405722008-05-06 18:09:29 +000034#include "LLVMCConfigurationEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include <algorithm>
36#include <cstdio>
37#include <fstream>
38#include <ios>
39using namespace llvm;
40
41enum ActionType {
42 PrintRecords,
43 GenEmitter,
44 GenRegisterEnums, GenRegister, GenRegisterHeader,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000045 GenInstrEnums, GenInstrs, GenAsmWriter,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 GenCallingConv,
47 GenDAGISel,
48 GenSubtarget,
49 GenIntrinsic,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000050 GenLLVMCCConf,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 PrintEnums
52};
53
54namespace {
55 cl::opt<ActionType>
56 Action(cl::desc("Action to perform:"),
57 cl::values(clEnumValN(PrintRecords, "print-records",
58 "Print all records to stdout (default)"),
59 clEnumValN(GenEmitter, "gen-emitter",
60 "Generate machine code emitter"),
61 clEnumValN(GenRegisterEnums, "gen-register-enums",
62 "Generate enum values for registers"),
63 clEnumValN(GenRegister, "gen-register-desc",
64 "Generate a register info description"),
65 clEnumValN(GenRegisterHeader, "gen-register-desc-header",
66 "Generate a register info description header"),
67 clEnumValN(GenInstrEnums, "gen-instr-enums",
68 "Generate enum values for instructions"),
69 clEnumValN(GenInstrs, "gen-instr-desc",
70 "Generate instruction descriptions"),
71 clEnumValN(GenCallingConv, "gen-callingconv",
72 "Generate calling convention descriptions"),
73 clEnumValN(GenAsmWriter, "gen-asm-writer",
74 "Generate assembly writer"),
75 clEnumValN(GenDAGISel, "gen-dag-isel",
76 "Generate a DAG instruction selector"),
77 clEnumValN(GenSubtarget, "gen-subtarget",
78 "Generate subtarget enumerations"),
79 clEnumValN(GenIntrinsic, "gen-intrinsic",
80 "Generate intrinsic information"),
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000081 clEnumValN(GenLLVMCCConf, "gen-llvmcc",
82 "Generate LLVMCC configuration library"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 clEnumValN(PrintEnums, "print-enums",
84 "Print enum values for a class"),
85 clEnumValEnd));
86
87 cl::opt<std::string>
88 Class("class", cl::desc("Print Enum list for this class"),
89 cl::value_desc("class name"));
90
91 cl::opt<std::string>
92 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
93 cl::init("-"));
94
95 cl::opt<std::string>
96 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
97
98 cl::list<std::string>
99 IncludeDirs("I", cl::desc("Directory of include files"),
100 cl::value_desc("directory"), cl::Prefix);
101}
102
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103RecordKeeper llvm::Records;
104
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000105/// ParseFile - this function begins the parsing of the specified tablegen
106/// file.
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000107static bool ParseFile(const std::string &Filename,
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000108 const std::vector<std::string> &IncludeDirs) {
109 std::string ErrorStr;
Chris Lattnerfc003612008-04-01 18:04:03 +0000110 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000111 if (F == 0) {
112 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
113 return true;
114 }
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000115
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000116 TGParser Parser(F);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000117
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000118 // Record the location of the include directory so that the lexer can find
119 // it later.
120 Parser.setIncludeDirs(IncludeDirs);
Mikhail Glushenkov41405722008-05-06 18:09:29 +0000121
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000122 return Parser.ParseFile();
123}
124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125int main(int argc, char **argv) {
126 cl::ParseCommandLineOptions(argc, argv);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000127
128 // Parse the input file.
129 if (ParseFile(InputFilename, IncludeDirs))
130 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 std::ostream *Out = cout.stream();
133 if (OutputFilename != "-") {
134 Out = new std::ofstream(OutputFilename.c_str());
135
136 if (!Out->good()) {
137 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
138 return 1;
139 }
140
141 // Make sure the file gets removed if *gasp* tablegen crashes...
142 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
143 }
144
145 try {
146 switch (Action) {
147 case PrintRecords:
148 *Out << Records; // No argument, dump all contents
149 break;
150 case GenEmitter:
151 CodeEmitterGen(Records).run(*Out);
152 break;
153
154 case GenRegisterEnums:
155 RegisterInfoEmitter(Records).runEnums(*Out);
156 break;
157 case GenRegister:
158 RegisterInfoEmitter(Records).run(*Out);
159 break;
160 case GenRegisterHeader:
161 RegisterInfoEmitter(Records).runHeader(*Out);
162 break;
163
164 case GenInstrEnums:
Chris Lattner030e6e52008-01-06 00:49:05 +0000165 InstrEnumEmitter(Records).run(*Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 break;
167 case GenInstrs:
168 InstrInfoEmitter(Records).run(*Out);
169 break;
170 case GenCallingConv:
171 CallingConvEmitter(Records).run(*Out);
172 break;
173 case GenAsmWriter:
174 AsmWriterEmitter(Records).run(*Out);
175 break;
176
177 case GenDAGISel:
178 DAGISelEmitter(Records).run(*Out);
179 break;
180 case GenSubtarget:
181 SubtargetEmitter(Records).run(*Out);
182 break;
183 case GenIntrinsic:
184 IntrinsicEmitter(Records).run(*Out);
185 break;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000186 case GenLLVMCCConf:
187 LLVMCCConfigurationEmitter(Records).run(*Out);
188 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 case PrintEnums:
190 {
191 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
192 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
193 *Out << Recs[i]->getName() << ", ";
194 *Out << "\n";
195 break;
196 }
197 default:
198 assert(1 && "Invalid Action");
199 return 1;
200 }
201 } catch (const std::string &Error) {
202 cerr << argv[0] << ": " << Error << "\n";
203 if (Out != cout.stream()) {
204 delete Out; // Close the file
205 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
206 }
207 return 1;
208 } catch (...) {
209 cerr << argv[0] << ": Unknown unexpected exception occurred.\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 2;
215 }
216
217 if (Out != cout.stream()) {
218 delete Out; // Close the file
219 }
220 return 0;
221}