blob: 147db12cf12a7938fd0bda13374b4fdd9832662f [file] [log] [blame]
Chris Lattner1d1adea2003-08-01 04:39:05 +00001//===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner1d1adea2003-08-01 04:39:05 +00009//
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
Chris Lattnere62c1182002-12-02 01:23:04 +000018#include "Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000019#include "TGParser.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000021#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000022#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/FileUtilities.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner50d45652007-02-27 22:08:27 +000026#include "CallingConvEmitter.h"
Misha Brukmanf00ce8b2003-05-24 00:17:12 +000027#include "CodeEmitterGen.h"
Chris Lattner1d1adea2003-08-01 04:39:05 +000028#include "RegisterInfoEmitter.h"
Chris Lattner169e66b2003-08-03 17:24:20 +000029#include "InstrInfoEmitter.h"
Chris Lattner7b117122008-01-06 00:49:05 +000030#include "InstrEnumEmitter.h"
Chris Lattner2e1f51b2004-08-01 05:59:33 +000031#include "AsmWriterEmitter.h"
Chris Lattner4a24c642005-09-03 01:14:03 +000032#include "DAGISelEmitter.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000033#include "FastISelEmitter.h"
Jim Laskeyf5fc2cb2005-10-21 19:05:19 +000034#include "SubtargetEmitter.h"
Chris Lattner9e493cf2006-03-03 02:32:46 +000035#include "IntrinsicEmitter.h"
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000036#include "LLVMCConfigurationEmitter.h"
Chris Lattnere62c1182002-12-02 01:23:04 +000037#include <algorithm>
Misha Brukmanc3fe45b2003-08-14 16:05:35 +000038#include <cstdio>
Chris Lattner9a886382003-06-03 05:04:42 +000039#include <fstream>
Duraid Madina6fb9a842005-12-26 05:08:55 +000040#include <ios>
Chris Lattner2082ebe2004-08-01 03:55:39 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Chris Lattnerbc520132003-06-03 04:56:29 +000043enum ActionType {
44 PrintRecords,
45 GenEmitter,
Chris Lattner54d156d2003-08-01 05:59:20 +000046 GenRegisterEnums, GenRegister, GenRegisterHeader,
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047 GenInstrEnums, GenInstrs, GenAsmWriter,
Chris Lattner50d45652007-02-27 22:08:27 +000048 GenCallingConv,
Chris Lattner4a24c642005-09-03 01:14:03 +000049 GenDAGISel,
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000050 GenFastISel,
Jim Laskeyf5fc2cb2005-10-21 19:05:19 +000051 GenSubtarget,
Chris Lattner9e493cf2006-03-03 02:32:46 +000052 GenIntrinsic,
Dale Johannesen49de9822009-02-05 01:49:45 +000053 GenTgtIntrinsic,
Mikhail Glushenkov895820d2008-05-06 18:12:03 +000054 GenLLVMCConf,
Chris Lattner3b239722006-03-03 02:34:28 +000055 PrintEnums
Chris Lattnerbc520132003-06-03 04:56:29 +000056};
57
58namespace {
59 cl::opt<ActionType>
60 Action(cl::desc("Action to perform:"),
61 cl::values(clEnumValN(PrintRecords, "print-records",
Chris Lattner85df2252003-06-03 05:07:28 +000062 "Print all records to stdout (default)"),
Chris Lattnerbc520132003-06-03 04:56:29 +000063 clEnumValN(GenEmitter, "gen-emitter",
64 "Generate machine code emitter"),
Chris Lattner54d156d2003-08-01 05:59:20 +000065 clEnumValN(GenRegisterEnums, "gen-register-enums",
66 "Generate enum values for registers"),
Chris Lattner1d1adea2003-08-01 04:39:05 +000067 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"),
Chris Lattner169e66b2003-08-03 17:24:20 +000071 clEnumValN(GenInstrEnums, "gen-instr-enums",
72 "Generate enum values for instructions"),
Chris Lattner15de32d2003-08-03 21:58:28 +000073 clEnumValN(GenInstrs, "gen-instr-desc",
74 "Generate instruction descriptions"),
Chris Lattner50d45652007-02-27 22:08:27 +000075 clEnumValN(GenCallingConv, "gen-callingconv",
76 "Generate calling convention descriptions"),
Chris Lattner2e1f51b2004-08-01 05:59:33 +000077 clEnumValN(GenAsmWriter, "gen-asm-writer",
78 "Generate assembly writer"),
Chris Lattner4a24c642005-09-03 01:14:03 +000079 clEnumValN(GenDAGISel, "gen-dag-isel",
80 "Generate a DAG instruction selector"),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000081 clEnumValN(GenFastISel, "gen-fast-isel",
82 "Generate a \"fast\" instruction selector"),
Jim Laskeyf5fc2cb2005-10-21 19:05:19 +000083 clEnumValN(GenSubtarget, "gen-subtarget",
84 "Generate subtarget enumerations"),
Chris Lattner9e493cf2006-03-03 02:32:46 +000085 clEnumValN(GenIntrinsic, "gen-intrinsic",
86 "Generate intrinsic information"),
Dale Johannesen49de9822009-02-05 01:49:45 +000087 clEnumValN(GenTgtIntrinsic, "gen-tgt-intrinsic",
88 "Generate target intrinsic information"),
Mikhail Glushenkov895820d2008-05-06 18:12:03 +000089 clEnumValN(GenLLVMCConf, "gen-llvmc",
90 "Generate LLVMC configuration library"),
Chris Lattnerbc520132003-06-03 04:56:29 +000091 clEnumValN(PrintEnums, "print-enums",
92 "Print enum values for a class"),
Chris Lattnerbd935332004-07-16 00:02:21 +000093 clEnumValEnd));
Chris Lattnerbc520132003-06-03 04:56:29 +000094
95 cl::opt<std::string>
Chris Lattner85df2252003-06-03 05:07:28 +000096 Class("class", cl::desc("Print Enum list for this class"),
97 cl::value_desc("class name"));
Chris Lattner9a886382003-06-03 05:04:42 +000098
Chris Lattner90523902003-07-30 19:48:02 +000099 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("-"));
John Criswell96b4bed2003-08-27 13:41:57 +0000105
Chris Lattnerd9f5d902006-03-03 01:47:14 +0000106 cl::list<std::string>
107 IncludeDirs("I", cl::desc("Directory of include files"),
Chris Lattnered542412006-03-03 01:53:40 +0000108 cl::value_desc("directory"), cl::Prefix);
Chris Lattnerbc520132003-06-03 04:56:29 +0000109}
110
Chris Lattner2082ebe2004-08-01 03:55:39 +0000111RecordKeeper llvm::Records;
Chris Lattnere62c1182002-12-02 01:23:04 +0000112
Chris Lattnerf4601652007-11-22 20:49:04 +0000113/// ParseFile - this function begins the parsing of the specified tablegen
114/// file.
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +0000115static bool ParseFile(const std::string &Filename,
Chris Lattnerf4601652007-11-22 20:49:04 +0000116 const std::vector<std::string> &IncludeDirs) {
117 std::string ErrorStr;
Chris Lattner038112a2008-04-01 18:04:03 +0000118 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattnerf4601652007-11-22 20:49:04 +0000119 if (F == 0) {
120 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
121 return true;
122 }
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +0000123
Chris Lattnerf4601652007-11-22 20:49:04 +0000124 TGParser Parser(F);
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +0000125
Chris Lattnerf4601652007-11-22 20:49:04 +0000126 // Record the location of the include directory so that the lexer can find
127 // it later.
128 Parser.setIncludeDirs(IncludeDirs);
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +0000129
Chris Lattnerf4601652007-11-22 20:49:04 +0000130 return Parser.ParseFile();
131}
132
Chris Lattnere62c1182002-12-02 01:23:04 +0000133int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000134 sys::PrintStackTraceOnErrorSignal();
135 PrettyStackTraceProgram X(argc, argv);
Chris Lattnere62c1182002-12-02 01:23:04 +0000136 cl::ParseCommandLineOptions(argc, argv);
Chris Lattnerf4601652007-11-22 20:49:04 +0000137
138 // Parse the input file.
139 if (ParseFile(InputFilename, IncludeDirs))
140 return 1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000141
Bill Wendlingf5da1332006-12-07 22:21:48 +0000142 std::ostream *Out = cout.stream();
Chris Lattner9a886382003-06-03 05:04:42 +0000143 if (OutputFilename != "-") {
Chris Lattner42df6d12004-07-13 06:11:46 +0000144 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner9a886382003-06-03 05:04:42 +0000145
146 if (!Out->good()) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000147 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner9a886382003-06-03 05:04:42 +0000148 return 1;
149 }
150
151 // Make sure the file gets removed if *gasp* tablegen crashes...
Reid Spencer227b6d02004-11-14 22:30:54 +0000152 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner9a886382003-06-03 05:04:42 +0000153 }
154
Chris Lattner1d1adea2003-08-01 04:39:05 +0000155 try {
156 switch (Action) {
Chris Lattneraccd8ab2003-08-01 04:47:20 +0000157 case PrintRecords:
158 *Out << Records; // No argument, dump all contents
159 break;
Chris Lattner1d1adea2003-08-01 04:39:05 +0000160 case GenEmitter:
161 CodeEmitterGen(Records).run(*Out);
162 break;
Chris Lattner169e66b2003-08-03 17:24:20 +0000163
Chris Lattner54d156d2003-08-01 05:59:20 +0000164 case GenRegisterEnums:
165 RegisterInfoEmitter(Records).runEnums(*Out);
166 break;
Chris Lattner1d1adea2003-08-01 04:39:05 +0000167 case GenRegister:
168 RegisterInfoEmitter(Records).run(*Out);
169 break;
170 case GenRegisterHeader:
171 RegisterInfoEmitter(Records).runHeader(*Out);
172 break;
Chris Lattner169e66b2003-08-03 17:24:20 +0000173
174 case GenInstrEnums:
Chris Lattner7b117122008-01-06 00:49:05 +0000175 InstrEnumEmitter(Records).run(*Out);
Chris Lattner169e66b2003-08-03 17:24:20 +0000176 break;
Chris Lattner15de32d2003-08-03 21:58:28 +0000177 case GenInstrs:
178 InstrInfoEmitter(Records).run(*Out);
179 break;
Chris Lattner50d45652007-02-27 22:08:27 +0000180 case GenCallingConv:
181 CallingConvEmitter(Records).run(*Out);
182 break;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000183 case GenAsmWriter:
184 AsmWriterEmitter(Records).run(*Out);
185 break;
186
Chris Lattner4a24c642005-09-03 01:14:03 +0000187 case GenDAGISel:
188 DAGISelEmitter(Records).run(*Out);
189 break;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000190 case GenFastISel:
191 FastISelEmitter(Records).run(*Out);
192 break;
Jim Laskeyf5fc2cb2005-10-21 19:05:19 +0000193 case GenSubtarget:
194 SubtargetEmitter(Records).run(*Out);
195 break;
Chris Lattner9e493cf2006-03-03 02:32:46 +0000196 case GenIntrinsic:
197 IntrinsicEmitter(Records).run(*Out);
198 break;
Dale Johannesen49de9822009-02-05 01:49:45 +0000199 case GenTgtIntrinsic:
200 IntrinsicEmitter(Records, true).run(*Out);
201 break;
Mikhail Glushenkov895820d2008-05-06 18:12:03 +0000202 case GenLLVMCConf:
203 LLVMCConfigurationEmitter(Records).run(*Out);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000204 break;
Chris Lattner1d1adea2003-08-01 04:39:05 +0000205 case PrintEnums:
Brian Gaeked0fde302003-11-11 22:41:34 +0000206 {
Chris Lattner1d1adea2003-08-01 04:39:05 +0000207 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
Chris Lattner1d1adea2003-08-01 04:39:05 +0000208 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
Chris Lattner7b9ee512004-02-06 03:19:17 +0000209 *Out << Recs[i]->getName() << ", ";
Chris Lattner1d1adea2003-08-01 04:39:05 +0000210 *Out << "\n";
211 break;
Chris Lattnere62c1182002-12-02 01:23:04 +0000212 }
Brian Gaeked0fde302003-11-11 22:41:34 +0000213 default:
214 assert(1 && "Invalid Action");
215 return 1;
216 }
Chris Lattner1d1adea2003-08-01 04:39:05 +0000217 } catch (const std::string &Error) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000218 cerr << argv[0] << ": " << Error << "\n";
219 if (Out != cout.stream()) {
Chris Lattnerf1e366a2003-08-01 19:21:43 +0000220 delete Out; // Close the file
221 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
222 }
Chris Lattner1d1adea2003-08-01 04:39:05 +0000223 return 1;
Dan Gohman212e6982008-11-07 21:01:13 +0000224 } catch (const char *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;
Reid Spencer23f7d512004-09-03 23:17:54 +0000231 } catch (...) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000232 cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
233 if (Out != cout.stream()) {
Reid Spencer23f7d512004-09-03 23:17:54 +0000234 delete Out; // Close the file
235 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
236 }
237 return 2;
Chris Lattnere62c1182002-12-02 01:23:04 +0000238 }
Chris Lattner9a886382003-06-03 05:04:42 +0000239
Bill Wendlingf5da1332006-12-07 22:21:48 +0000240 if (Out != cout.stream()) {
Chris Lattnere79c72d2003-08-01 20:35:01 +0000241 delete Out; // Close the file
Chris Lattnere79c72d2003-08-01 20:35:01 +0000242 }
Chris Lattner1d1adea2003-08-01 04:39:05 +0000243 return 0;
Chris Lattnere62c1182002-12-02 01:23:04 +0000244}