1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting a description of the target
11 // instruction set for the code generator.
13 //===----------------------------------------------------------------------===//
15 #include "InstrInfoEmitter.h"
16 #include "CodeGenTarget.h"
20 // runEnums - Print out enum values for all of the instructions.
21 void InstrInfoEmitter::runEnums(std::ostream &OS) {
22 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
24 if (Insts.size() == 0)
25 throw std::string("No 'Instruction' subclasses defined!");
27 std::string Namespace = Insts[0]->getValueAsString("Namespace");
29 EmitSourceFileHeader("Target Instruction Enum Values", OS);
31 if (!Namespace.empty())
32 OS << "namespace " << Namespace << " {\n";
37 // We must emit the PHI opcode first...
38 Record *InstrInfo = Target.getInstructionSet();
39 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
41 OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
43 // Print out the rest of the instructions now...
44 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
46 OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
49 if (!Namespace.empty())
51 EmitSourceFileTail(OS);
54 void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name,
55 std::ostream &OS) const {
56 OS << "static const unsigned " << Name << "[] = { ";
57 for (unsigned j = 0, e = LI->getSize(); j != e; ++j)
58 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j)))
59 OS << getQualifiedName(DI->getDef()) << ", ";
61 throw "Illegal value in '" + Name + "' list!";
66 // run - Emit the main instruction description records for the target...
67 void InstrInfoEmitter::run(std::ostream &OS) {
68 EmitSourceFileHeader("Target Instruction Descriptors", OS);
70 const std::string &TargetName = Target.getName();
71 Record *InstrInfo = Target.getInstructionSet();
72 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
74 std::vector<Record*> Instructions =
75 Records.getAllDerivedDefinitions("Instruction");
77 // Emit empty implicit uses and defs lists
78 OS << "static const unsigned EmptyImpUses[] = { 0 };\n"
79 << "static const unsigned EmptyImpDefs[] = { 0 };\n";
81 // Emit all of the instruction's implicit uses and defs...
82 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
83 Record *Inst = Instructions[i];
84 ListInit *LI = Inst->getValueAsListInit("Uses");
85 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS);
86 LI = Inst->getValueAsListInit("Defs");
87 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS);
90 OS << "\nstatic const TargetInstrDescriptor " << TargetName
92 emitRecord(PHI, 0, InstrInfo, OS);
94 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
95 if (Instructions[i] != PHI)
96 emitRecord(Instructions[i], i+1, InstrInfo, OS);
98 EmitSourceFileTail(OS);
101 void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
103 OS << " { \"" << R->getValueAsString("Name")
104 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
106 // Emit all of the target indepedent flags...
107 if (R->getValueAsBit("isReturn")) OS << "|M_RET_FLAG";
108 if (R->getValueAsBit("isBranch")) OS << "|M_BRANCH_FLAG";
109 if (R->getValueAsBit("isBarrier")) OS << "|M_BARRIER_FLAG";
110 if (R->getValueAsBit("isCall" )) OS << "|M_CALL_FLAG";
111 if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG";
112 if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG";
115 // Emit all of the target-specific flags...
116 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
117 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
118 if (LI->getSize() != Shift->getSize())
119 throw "Lengths of " + InstrInfo->getName() +
120 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
122 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
123 emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)),
124 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
128 // Emit the implicit uses and defs lists...
129 LI = R->getValueAsListInit("Uses");
131 OS << "EmptyImpUses, ";
133 OS << R->getName() << "ImpUses, ";
135 LI = R->getValueAsListInit("Defs");
137 OS << "EmptyImpDefs ";
139 OS << R->getName() << "ImpDefs ";
141 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
144 void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
145 IntInit *ShiftInt, std::ostream &OS) {
146 if (Val == 0 || ShiftInt == 0)
147 throw std::string("Illegal value or shift amount in TargetInfo*!");
148 RecordVal *RV = R->getValue(Val->getValue());
149 int Shift = ShiftInt->getValue();
151 if (RV == 0 || RV->getValue() == 0)
152 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
154 Init *Value = RV->getValue();
155 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
156 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
158 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
159 // Convert the Bits to an integer to print...
160 Init *I = BI->convertInitializerTo(new IntRecTy());
162 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
164 OS << "|(" << II->getValue() << "<<" << Shift << ")";
168 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
169 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
173 std::cerr << "Unhandled initializer: " << *Val << "\n";
174 throw "In record '" + R->getName() + "' for TSFlag emission.";