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 | |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 47 | // We must emit the PHI opcode first... |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 48 | Record *Target = getTarget(Records); |
| 49 | Record *InstrInfo = Target->getValueAsDef("InstructionSet"); |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 50 | Record *PHI = InstrInfo->getValueAsDef("PHIInst"); |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 51 | |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 52 | OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n"; |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 53 | |
| 54 | // Print out the rest of the instructions now... |
| 55 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 56 | if (Insts[i] != PHI) |
| 57 | OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n"; |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 58 | |
| 59 | OS << " };\n"; |
| 60 | if (!Namespace.empty()) |
| 61 | OS << "}\n"; |
| 62 | } |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 63 | |
| 64 | static void printDefList(ListInit *LI, const std::string &Name, |
| 65 | std::ostream &OS) { |
| 66 | OS << "static const unsigned " << Name << "[] = { "; |
| 67 | for (unsigned j = 0, e = LI->getSize(); j != e; ++j) |
| 68 | if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j))) |
| 69 | OS << getQualifiedName(DI->getDef()) << ", "; |
| 70 | else |
| 71 | throw "Illegal value in '" + Name + "' list!"; |
| 72 | OS << "0 };\n"; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | // run - Emit the main instruction description records for the target... |
| 77 | void InstrInfoEmitter::run(std::ostream &OS) { |
| 78 | EmitSourceHeader("Target Instruction Descriptors", OS); |
| 79 | Record *Target = getTarget(Records); |
| 80 | const std::string &TargetName = Target->getName(); |
| 81 | Record *InstrInfo = Target->getValueAsDef("InstructionSet"); |
| 82 | Record *PHI = InstrInfo->getValueAsDef("PHIInst"); |
| 83 | |
| 84 | std::vector<Record*> Instructions = |
| 85 | Records.getAllDerivedDefinitions("Instruction"); |
| 86 | |
| 87 | // Emit all of the instruction's implicit uses and defs... |
| 88 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { |
| 89 | Record *Inst = Instructions[i]; |
| 90 | ListInit *LI = Inst->getValueAsListInit("Uses"); |
| 91 | if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS); |
| 92 | LI = Inst->getValueAsListInit("Defs"); |
| 93 | if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS); |
| 94 | } |
| 95 | |
| 96 | OS << "\nstatic const TargetInstrDescriptor " << TargetName |
| 97 | << "Insts[] = {\n"; |
| 98 | emitRecord(PHI, 0, InstrInfo, OS); |
| 99 | |
| 100 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) |
| 101 | if (Instructions[i] != PHI) |
| 102 | emitRecord(Instructions[i], i+1, InstrInfo, OS); |
| 103 | OS << "};\n"; |
| 104 | } |
| 105 | |
| 106 | void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo, |
| 107 | std::ostream &OS) { |
| 108 | OS << " { \"" << R->getValueAsString("Name") |
| 109 | << "\",\t-1, -1, 0, false, 0, 0, 0, 0"; |
| 110 | |
| 111 | // Emit all of the target indepedent flags... |
| 112 | if (R->getValueAsBit("isReturn")) OS << "|M_RET_FLAG"; |
| 113 | if (R->getValueAsBit("isBranch")) OS << "|M_BRANCH_FLAG"; |
| 114 | if (R->getValueAsBit("isCall" )) OS << "|M_CALL_FLAG"; |
| 115 | if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG"; |
| 116 | if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG"; |
| 117 | OS << ", 0"; |
| 118 | |
| 119 | // Emit all of the target-specific flags... |
| 120 | ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields"); |
| 121 | ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts"); |
| 122 | if (LI->getSize() != Shift->getSize()) |
| 123 | throw "Lengths of " + InstrInfo->getName() + |
| 124 | ":(TargetInfoFields, TargetInfoPositions) must be equal!"; |
| 125 | |
| 126 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) |
| 127 | emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)), |
| 128 | dynamic_cast<IntInit*>(Shift->getElement(i)), OS); |
| 129 | |
| 130 | OS << ", "; |
| 131 | |
| 132 | // Emit the implicit uses and defs lists... |
| 133 | LI = R->getValueAsListInit("Uses"); |
| 134 | if (!LI->getSize()) |
| 135 | OS << "0, "; |
| 136 | else |
| 137 | OS << R->getName() << "ImpUses, "; |
| 138 | |
| 139 | LI = R->getValueAsListInit("Defs"); |
| 140 | if (!LI->getSize()) |
| 141 | OS << "0 "; |
| 142 | else |
| 143 | OS << R->getName() << "ImpDefs "; |
| 144 | |
| 145 | OS << " }, // Inst #" << Num << " = " << R->getName() << "\n"; |
| 146 | } |
| 147 | |
| 148 | void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val, |
| 149 | IntInit *ShiftInt, std::ostream &OS) { |
| 150 | if (Val == 0 || ShiftInt == 0) |
| 151 | throw std::string("Illegal value or shift amount in TargetInfo*!"); |
| 152 | RecordVal *RV = R->getValue(Val->getValue()); |
| 153 | int Shift = ShiftInt->getValue(); |
| 154 | |
| 155 | if (RV == 0 || RV->getValue() == 0) |
| 156 | throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!"; |
| 157 | |
| 158 | Init *Value = RV->getValue(); |
| 159 | if (BitInit *BI = dynamic_cast<BitInit*>(Value)) { |
| 160 | if (BI->getValue()) OS << "|(1<<" << Shift << ")"; |
| 161 | return; |
| 162 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) { |
| 163 | // Convert the Bits to an integer to print... |
| 164 | Init *I = BI->convertInitializerTo(new IntRecTy()); |
| 165 | if (I) |
| 166 | if (IntInit *II = dynamic_cast<IntInit*>(I)) { |
| 167 | if (II->getValue()) |
| 168 | OS << "|(" << II->getValue() << "<<" << Shift << ")"; |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) { |
| 173 | if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")"; |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | std::cerr << "Unhandled initializer: " << *Val << "\n"; |
| 178 | throw "In record '" + R->getName() + "' for TSFlag emission."; |
| 179 | } |