Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 1 | //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===// |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame^] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This tablegen backend is responsible for emitting a description of the target |
| 11 | // instruction set for the code generator. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "InstrInfoEmitter.h" |
Chris Lattner | 7884b75 | 2003-08-07 05:39:09 +0000 | [diff] [blame] | 16 | #include "CodeGenWrappers.h" |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 17 | #include "Record.h" |
| 18 | |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 19 | // runEnums - Print out enum values for all of the instructions. |
| 20 | void InstrInfoEmitter::runEnums(std::ostream &OS) { |
| 21 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
| 22 | |
| 23 | if (Insts.size() == 0) |
| 24 | throw std::string("No 'Instruction' subclasses defined!"); |
| 25 | |
| 26 | std::string Namespace = Insts[0]->getValueAsString("Namespace"); |
| 27 | |
Chris Lattner | bc01723 | 2003-08-06 04:32:07 +0000 | [diff] [blame] | 28 | EmitSourceFileHeader("Target Instruction Enum Values", OS); |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 29 | |
| 30 | if (!Namespace.empty()) |
| 31 | OS << "namespace " << Namespace << " {\n"; |
| 32 | OS << " enum {\n"; |
| 33 | |
Chris Lattner | 7884b75 | 2003-08-07 05:39:09 +0000 | [diff] [blame] | 34 | CodeGenTarget Target; |
| 35 | |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 36 | // We must emit the PHI opcode first... |
Chris Lattner | 7884b75 | 2003-08-07 05:39:09 +0000 | [diff] [blame] | 37 | Record *InstrInfo = Target.getInstructionSet(); |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 38 | Record *PHI = InstrInfo->getValueAsDef("PHIInst"); |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 39 | |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 40 | OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n"; |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 41 | |
| 42 | // Print out the rest of the instructions now... |
| 43 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 44 | if (Insts[i] != PHI) |
| 45 | OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n"; |
Chris Lattner | 33ccf7e | 2003-08-03 17:24:10 +0000 | [diff] [blame] | 46 | |
| 47 | OS << " };\n"; |
| 48 | if (!Namespace.empty()) |
| 49 | OS << "}\n"; |
| 50 | } |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 51 | |
Chris Lattner | bc01723 | 2003-08-06 04:32:07 +0000 | [diff] [blame] | 52 | void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name, |
| 53 | std::ostream &OS) const { |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 54 | OS << "static const unsigned " << Name << "[] = { "; |
| 55 | for (unsigned j = 0, e = LI->getSize(); j != e; ++j) |
| 56 | if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j))) |
| 57 | OS << getQualifiedName(DI->getDef()) << ", "; |
| 58 | else |
| 59 | throw "Illegal value in '" + Name + "' list!"; |
| 60 | OS << "0 };\n"; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | // run - Emit the main instruction description records for the target... |
| 65 | void InstrInfoEmitter::run(std::ostream &OS) { |
Chris Lattner | bc01723 | 2003-08-06 04:32:07 +0000 | [diff] [blame] | 66 | EmitSourceFileHeader("Target Instruction Descriptors", OS); |
Chris Lattner | 7884b75 | 2003-08-07 05:39:09 +0000 | [diff] [blame] | 67 | CodeGenTarget Target; |
| 68 | const std::string &TargetName = Target.getName(); |
| 69 | Record *InstrInfo = Target.getInstructionSet(); |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 70 | Record *PHI = InstrInfo->getValueAsDef("PHIInst"); |
| 71 | |
| 72 | std::vector<Record*> Instructions = |
| 73 | Records.getAllDerivedDefinitions("Instruction"); |
| 74 | |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 75 | // Emit empty implicit uses and defs lists |
| 76 | OS << "static const unsigned EmptyImpUses[] = { 0 };\n" |
| 77 | << "static const unsigned EmptyImpDefs[] = { 0 };\n"; |
| 78 | |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 79 | // Emit all of the instruction's implicit uses and defs... |
| 80 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { |
| 81 | Record *Inst = Instructions[i]; |
| 82 | ListInit *LI = Inst->getValueAsListInit("Uses"); |
| 83 | if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS); |
| 84 | LI = Inst->getValueAsListInit("Defs"); |
| 85 | if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS); |
| 86 | } |
| 87 | |
| 88 | OS << "\nstatic const TargetInstrDescriptor " << TargetName |
| 89 | << "Insts[] = {\n"; |
| 90 | emitRecord(PHI, 0, InstrInfo, OS); |
| 91 | |
| 92 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) |
| 93 | if (Instructions[i] != PHI) |
| 94 | emitRecord(Instructions[i], i+1, InstrInfo, OS); |
| 95 | OS << "};\n"; |
| 96 | } |
| 97 | |
| 98 | void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo, |
| 99 | std::ostream &OS) { |
| 100 | OS << " { \"" << R->getValueAsString("Name") |
| 101 | << "\",\t-1, -1, 0, false, 0, 0, 0, 0"; |
| 102 | |
| 103 | // Emit all of the target indepedent flags... |
| 104 | if (R->getValueAsBit("isReturn")) OS << "|M_RET_FLAG"; |
| 105 | if (R->getValueAsBit("isBranch")) OS << "|M_BRANCH_FLAG"; |
| 106 | if (R->getValueAsBit("isCall" )) OS << "|M_CALL_FLAG"; |
| 107 | if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG"; |
| 108 | if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG"; |
| 109 | OS << ", 0"; |
| 110 | |
| 111 | // Emit all of the target-specific flags... |
| 112 | ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields"); |
| 113 | ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts"); |
| 114 | if (LI->getSize() != Shift->getSize()) |
| 115 | throw "Lengths of " + InstrInfo->getName() + |
| 116 | ":(TargetInfoFields, TargetInfoPositions) must be equal!"; |
| 117 | |
| 118 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) |
| 119 | emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)), |
| 120 | dynamic_cast<IntInit*>(Shift->getElement(i)), OS); |
| 121 | |
| 122 | OS << ", "; |
| 123 | |
| 124 | // Emit the implicit uses and defs lists... |
| 125 | LI = R->getValueAsListInit("Uses"); |
| 126 | if (!LI->getSize()) |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 127 | OS << "EmptyImpUses, "; |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 128 | else |
| 129 | OS << R->getName() << "ImpUses, "; |
| 130 | |
| 131 | LI = R->getValueAsListInit("Defs"); |
| 132 | if (!LI->getSize()) |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 133 | OS << "EmptyImpDefs "; |
Chris Lattner | a3ae614 | 2003-08-03 21:57:51 +0000 | [diff] [blame] | 134 | else |
| 135 | OS << R->getName() << "ImpDefs "; |
| 136 | |
| 137 | OS << " }, // Inst #" << Num << " = " << R->getName() << "\n"; |
| 138 | } |
| 139 | |
| 140 | void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val, |
| 141 | IntInit *ShiftInt, std::ostream &OS) { |
| 142 | if (Val == 0 || ShiftInt == 0) |
| 143 | throw std::string("Illegal value or shift amount in TargetInfo*!"); |
| 144 | RecordVal *RV = R->getValue(Val->getValue()); |
| 145 | int Shift = ShiftInt->getValue(); |
| 146 | |
| 147 | if (RV == 0 || RV->getValue() == 0) |
| 148 | throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!"; |
| 149 | |
| 150 | Init *Value = RV->getValue(); |
| 151 | if (BitInit *BI = dynamic_cast<BitInit*>(Value)) { |
| 152 | if (BI->getValue()) OS << "|(1<<" << Shift << ")"; |
| 153 | return; |
| 154 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) { |
| 155 | // Convert the Bits to an integer to print... |
| 156 | Init *I = BI->convertInitializerTo(new IntRecTy()); |
| 157 | if (I) |
| 158 | if (IntInit *II = dynamic_cast<IntInit*>(I)) { |
| 159 | if (II->getValue()) |
| 160 | OS << "|(" << II->getValue() << "<<" << Shift << ")"; |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) { |
| 165 | if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")"; |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | std::cerr << "Unhandled initializer: " << *Val << "\n"; |
| 170 | throw "In record '" + R->getName() + "' for TSFlag emission."; |
| 171 | } |