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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/Streams.h" |
| 21 | #include "llvm/System/Signals.h" |
| 22 | #include "llvm/Support/FileUtilities.h" |
| 23 | #include "CallingConvEmitter.h" |
| 24 | #include "CodeEmitterGen.h" |
| 25 | #include "RegisterInfoEmitter.h" |
| 26 | #include "InstrInfoEmitter.h" |
| 27 | #include "AsmWriterEmitter.h" |
| 28 | #include "DAGISelEmitter.h" |
| 29 | #include "SubtargetEmitter.h" |
| 30 | #include "IntrinsicEmitter.h" |
| 31 | #include <algorithm> |
| 32 | #include <cstdio> |
| 33 | #include <fstream> |
| 34 | #include <ios> |
| 35 | using namespace llvm; |
| 36 | |
| 37 | enum ActionType { |
| 38 | PrintRecords, |
| 39 | GenEmitter, |
| 40 | GenRegisterEnums, GenRegister, GenRegisterHeader, |
| 41 | GenInstrEnums, GenInstrs, GenAsmWriter, |
| 42 | GenCallingConv, |
| 43 | GenDAGISel, |
| 44 | GenSubtarget, |
| 45 | GenIntrinsic, |
| 46 | PrintEnums |
| 47 | }; |
| 48 | |
| 49 | namespace { |
| 50 | cl::opt<ActionType> |
| 51 | Action(cl::desc("Action to perform:"), |
| 52 | cl::values(clEnumValN(PrintRecords, "print-records", |
| 53 | "Print all records to stdout (default)"), |
| 54 | clEnumValN(GenEmitter, "gen-emitter", |
| 55 | "Generate machine code emitter"), |
| 56 | clEnumValN(GenRegisterEnums, "gen-register-enums", |
| 57 | "Generate enum values for registers"), |
| 58 | clEnumValN(GenRegister, "gen-register-desc", |
| 59 | "Generate a register info description"), |
| 60 | clEnumValN(GenRegisterHeader, "gen-register-desc-header", |
| 61 | "Generate a register info description header"), |
| 62 | clEnumValN(GenInstrEnums, "gen-instr-enums", |
| 63 | "Generate enum values for instructions"), |
| 64 | clEnumValN(GenInstrs, "gen-instr-desc", |
| 65 | "Generate instruction descriptions"), |
| 66 | clEnumValN(GenCallingConv, "gen-callingconv", |
| 67 | "Generate calling convention descriptions"), |
| 68 | clEnumValN(GenAsmWriter, "gen-asm-writer", |
| 69 | "Generate assembly writer"), |
| 70 | clEnumValN(GenDAGISel, "gen-dag-isel", |
| 71 | "Generate a DAG instruction selector"), |
| 72 | clEnumValN(GenSubtarget, "gen-subtarget", |
| 73 | "Generate subtarget enumerations"), |
| 74 | clEnumValN(GenIntrinsic, "gen-intrinsic", |
| 75 | "Generate intrinsic information"), |
| 76 | clEnumValN(PrintEnums, "print-enums", |
| 77 | "Print enum values for a class"), |
| 78 | clEnumValEnd)); |
| 79 | |
| 80 | cl::opt<std::string> |
| 81 | Class("class", cl::desc("Print Enum list for this class"), |
| 82 | cl::value_desc("class name")); |
| 83 | |
| 84 | cl::opt<std::string> |
| 85 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 86 | cl::init("-")); |
| 87 | |
| 88 | cl::opt<std::string> |
| 89 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 90 | |
| 91 | cl::list<std::string> |
| 92 | IncludeDirs("I", cl::desc("Directory of include files"), |
| 93 | cl::value_desc("directory"), cl::Prefix); |
| 94 | } |
| 95 | |
| 96 | namespace llvm { |
| 97 | void ParseFile(const std::string &Filename, |
| 98 | const std::vector<std::string> &IncludeDirs); |
| 99 | } |
| 100 | |
| 101 | RecordKeeper llvm::Records; |
| 102 | |
| 103 | int main(int argc, char **argv) { |
| 104 | cl::ParseCommandLineOptions(argc, argv); |
| 105 | ParseFile(InputFilename, IncludeDirs); |
| 106 | |
| 107 | std::ostream *Out = cout.stream(); |
| 108 | if (OutputFilename != "-") { |
| 109 | Out = new std::ofstream(OutputFilename.c_str()); |
| 110 | |
| 111 | if (!Out->good()) { |
| 112 | cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | // Make sure the file gets removed if *gasp* tablegen crashes... |
| 117 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 118 | } |
| 119 | |
| 120 | try { |
| 121 | switch (Action) { |
| 122 | case PrintRecords: |
| 123 | *Out << Records; // No argument, dump all contents |
| 124 | break; |
| 125 | case GenEmitter: |
| 126 | CodeEmitterGen(Records).run(*Out); |
| 127 | break; |
| 128 | |
| 129 | case GenRegisterEnums: |
| 130 | RegisterInfoEmitter(Records).runEnums(*Out); |
| 131 | break; |
| 132 | case GenRegister: |
| 133 | RegisterInfoEmitter(Records).run(*Out); |
| 134 | break; |
| 135 | case GenRegisterHeader: |
| 136 | RegisterInfoEmitter(Records).runHeader(*Out); |
| 137 | break; |
| 138 | |
| 139 | case GenInstrEnums: |
| 140 | InstrInfoEmitter(Records).runEnums(*Out); |
| 141 | break; |
| 142 | case GenInstrs: |
| 143 | InstrInfoEmitter(Records).run(*Out); |
| 144 | break; |
| 145 | case GenCallingConv: |
| 146 | CallingConvEmitter(Records).run(*Out); |
| 147 | break; |
| 148 | case GenAsmWriter: |
| 149 | AsmWriterEmitter(Records).run(*Out); |
| 150 | break; |
| 151 | |
| 152 | case GenDAGISel: |
| 153 | DAGISelEmitter(Records).run(*Out); |
| 154 | break; |
| 155 | case GenSubtarget: |
| 156 | SubtargetEmitter(Records).run(*Out); |
| 157 | break; |
| 158 | case GenIntrinsic: |
| 159 | IntrinsicEmitter(Records).run(*Out); |
| 160 | break; |
| 161 | case PrintEnums: |
| 162 | { |
| 163 | std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class); |
| 164 | for (unsigned i = 0, e = Recs.size(); i != e; ++i) |
| 165 | *Out << Recs[i]->getName() << ", "; |
| 166 | *Out << "\n"; |
| 167 | break; |
| 168 | } |
| 169 | default: |
| 170 | assert(1 && "Invalid Action"); |
| 171 | return 1; |
| 172 | } |
| 173 | } catch (const std::string &Error) { |
| 174 | cerr << argv[0] << ": " << Error << "\n"; |
| 175 | if (Out != cout.stream()) { |
| 176 | delete Out; // Close the file |
| 177 | std::remove(OutputFilename.c_str()); // Remove the file, it's broken |
| 178 | } |
| 179 | return 1; |
| 180 | } catch (...) { |
| 181 | cerr << argv[0] << ": Unknown unexpected exception occurred.\n"; |
| 182 | if (Out != cout.stream()) { |
| 183 | delete Out; // Close the file |
| 184 | std::remove(OutputFilename.c_str()); // Remove the file, it's broken |
| 185 | } |
| 186 | return 2; |
| 187 | } |
| 188 | |
| 189 | if (Out != cout.stream()) { |
| 190 | delete Out; // Close the file |
| 191 | } |
| 192 | return 0; |
| 193 | } |