Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- TableGen.cpp - Top-Level TableGen implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | fd6c2f0 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 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 Lattner | 6b7d76a | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 19 | #include "TGParser.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/Streams.h" |
| 22 | #include "llvm/System/Signals.h" |
| 23 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | 6b7d76a | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include "CallingConvEmitter.h" |
| 26 | #include "CodeEmitterGen.h" |
| 27 | #include "RegisterInfoEmitter.h" |
| 28 | #include "InstrInfoEmitter.h" |
Chris Lattner | 030e6e5 | 2008-01-06 00:49:05 +0000 | [diff] [blame] | 29 | #include "InstrEnumEmitter.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | #include "AsmWriterEmitter.h" |
| 31 | #include "DAGISelEmitter.h" |
| 32 | #include "SubtargetEmitter.h" |
| 33 | #include "IntrinsicEmitter.h" |
Anton Korobeynikov | e9ffb5b | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 34 | #include "LLVMCCConfigurationEmitter.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | #include <cstdio> |
| 37 | #include <fstream> |
| 38 | #include <ios> |
| 39 | using namespace llvm; |
| 40 | |
| 41 | enum ActionType { |
| 42 | PrintRecords, |
| 43 | GenEmitter, |
| 44 | GenRegisterEnums, GenRegister, GenRegisterHeader, |
Anton Korobeynikov | e9ffb5b | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 45 | GenInstrEnums, GenInstrs, GenAsmWriter, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | GenCallingConv, |
| 47 | GenDAGISel, |
| 48 | GenSubtarget, |
| 49 | GenIntrinsic, |
Anton Korobeynikov | e9ffb5b | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 50 | GenLLVMCCConf, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | PrintEnums |
| 52 | }; |
| 53 | |
| 54 | namespace { |
| 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 Korobeynikov | e9ffb5b | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 81 | clEnumValN(GenLLVMCCConf, "gen-llvmcc", |
| 82 | "Generate LLVMCC configuration library"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | 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 Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 103 | RecordKeeper llvm::Records; |
| 104 | |
Chris Lattner | 6b7d76a | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 105 | /// ParseFile - this function begins the parsing of the specified tablegen |
| 106 | /// file. |
| 107 | static bool ParseFile(const std::string &Filename, |
| 108 | const std::vector<std::string> &IncludeDirs) { |
| 109 | std::string ErrorStr; |
Chris Lattner | fc00361 | 2008-04-01 18:04:03 +0000 | [diff] [blame^] | 110 | MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr); |
Chris Lattner | 6b7d76a | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 111 | if (F == 0) { |
| 112 | cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n"; |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | TGParser Parser(F); |
| 117 | |
| 118 | // Record the location of the include directory so that the lexer can find |
| 119 | // it later. |
| 120 | Parser.setIncludeDirs(IncludeDirs); |
| 121 | |
| 122 | return Parser.ParseFile(); |
| 123 | } |
| 124 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 125 | int main(int argc, char **argv) { |
| 126 | cl::ParseCommandLineOptions(argc, argv); |
Chris Lattner | 6b7d76a | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 127 | |
| 128 | // Parse the input file. |
| 129 | if (ParseFile(InputFilename, IncludeDirs)) |
| 130 | return 1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 131 | |
| 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 Lattner | 030e6e5 | 2008-01-06 00:49:05 +0000 | [diff] [blame] | 165 | InstrEnumEmitter(Records).run(*Out); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 166 | 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 Korobeynikov | e9ffb5b | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 186 | case GenLLVMCCConf: |
| 187 | LLVMCCConfigurationEmitter(Records).run(*Out); |
| 188 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 189 | 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 | } |