Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame^] | 1 | //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===// |
| 2 | // |
| 3 | // This tablegen backend is responsible for emitting a description of the target |
| 4 | // instruction set for the code generator. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "InstrInfoEmitter.h" |
| 9 | #include "Record.h" |
| 10 | |
| 11 | static void EmitSourceHeader(const std::string &Desc, std::ostream &o) { |
| 12 | o << "//===- TableGen'erated file -------------------------------------*-" |
| 13 | " C++ -*-===//\n//\n// " << Desc << "\n//\n// Automatically generate" |
| 14 | "d file, do not edit!\n//\n//===------------------------------------" |
| 15 | "----------------------------------===//\n\n"; |
| 16 | } |
| 17 | |
| 18 | static std::string getQualifiedName(Record *R) { |
| 19 | std::string Namespace = R->getValueAsString("Namespace"); |
| 20 | if (Namespace.empty()) return R->getName(); |
| 21 | return Namespace + "::" + R->getName(); |
| 22 | } |
| 23 | |
| 24 | static Record *getTarget(RecordKeeper &RC) { |
| 25 | std::vector<Record*> Targets = RC.getAllDerivedDefinitions("Target"); |
| 26 | |
| 27 | if (Targets.size() != 1) |
| 28 | throw std::string("ERROR: Multiple subclasses of Target defined!"); |
| 29 | return Targets[0]; |
| 30 | } |
| 31 | |
| 32 | // runEnums - Print out enum values for all of the instructions. |
| 33 | void InstrInfoEmitter::runEnums(std::ostream &OS) { |
| 34 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
| 35 | |
| 36 | if (Insts.size() == 0) |
| 37 | throw std::string("No 'Instruction' subclasses defined!"); |
| 38 | |
| 39 | std::string Namespace = Insts[0]->getValueAsString("Namespace"); |
| 40 | |
| 41 | EmitSourceHeader("Target Instruction Enum Values", OS); |
| 42 | |
| 43 | if (!Namespace.empty()) |
| 44 | OS << "namespace " << Namespace << " {\n"; |
| 45 | OS << " enum {\n"; |
| 46 | |
| 47 | // We must emit the PHI and NOOP opcodes first... |
| 48 | Record *Target = getTarget(Records); |
| 49 | Record *InstrInfo = Target->getValueAsDef("InstructionSet"); |
| 50 | |
| 51 | Record *PHI = InstrInfo->getValueAsDef("PHIInst"); |
| 52 | Record *NOOP = InstrInfo->getValueAsDef("NOOPInst"); |
| 53 | |
| 54 | OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n" |
| 55 | << " " << NOOP->getName() << ", \t// 1 (fixed for all targets)\n"; |
| 56 | |
| 57 | // Print out the rest of the instructions now... |
| 58 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) |
| 59 | if (Insts[i] != PHI && Insts[i] != NOOP) |
| 60 | OS << " " << Insts[i]->getName() << ", \t// " << i+2 << "\n"; |
| 61 | |
| 62 | OS << " };\n"; |
| 63 | if (!Namespace.empty()) |
| 64 | OS << "}\n"; |
| 65 | } |