1 //===- RegisterInfoEmitter.cpp - Generate a Register File Desc. -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting a description of a target
11 // register file for a code generator. It uses instances of the Register,
12 // RegisterAliases, and RegisterClass classes to gather this information.
14 //===----------------------------------------------------------------------===//
16 #include "RegisterInfoEmitter.h"
17 #include "CodeGenTarget.h"
18 #include "CodeGenRegisters.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Support/Format.h"
27 // runEnums - Print out enum values for all of the registers.
29 RegisterInfoEmitter::runEnums(raw_ostream &OS,
30 CodeGenTarget &Target, CodeGenRegBank &Bank) {
31 const std::vector<CodeGenRegister*> &Registers = Bank.getRegisters();
33 std::string Namespace = Registers[0]->TheDef->getValueAsString("Namespace");
35 EmitSourceFileHeader("Target Register Enum Values", OS);
37 OS << "\n#ifdef GET_REGINFO_ENUM\n";
38 OS << "#undef GET_REGINFO_ENUM\n";
40 OS << "namespace llvm {\n\n";
42 if (!Namespace.empty())
43 OS << "namespace " << Namespace << " {\n";
44 OS << "enum {\n NoRegister,\n";
46 for (unsigned i = 0, e = Registers.size(); i != e; ++i)
47 OS << " " << Registers[i]->getName() << " = " <<
48 Registers[i]->EnumValue << ",\n";
49 assert(Registers.size() == Registers[Registers.size()-1]->EnumValue &&
50 "Register enum value mismatch!");
51 OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
53 if (!Namespace.empty())
56 const std::vector<CodeGenRegisterClass> &RegisterClasses =
57 Target.getRegisterClasses();
58 if (!RegisterClasses.empty()) {
59 OS << "\n// Register classes\n";
60 OS << "namespace " << RegisterClasses[0].Namespace << " {\n";
62 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
64 OS << " " << RegisterClasses[i].getName() << "RegClassID";
71 OS << "} // End llvm namespace \n";
72 OS << "#endif // GET_REGINFO_ENUM\n\n";
76 // runMCDesc - Print out MC register descriptions.
79 RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
80 CodeGenRegBank &RegBank) {
81 EmitSourceFileHeader("MC Register Information", OS);
83 OS << "\n#ifdef GET_REGINFO_MC_DESC\n";
84 OS << "#undef GET_REGINFO_MC_DESC\n";
86 std::map<const CodeGenRegister*, CodeGenRegister::Set> Overlaps;
87 RegBank.computeOverlaps(Overlaps);
89 OS << "namespace llvm {\n\n";
91 const std::string &TargetName = Target.getName();
92 std::string ClassName = TargetName + "GenMCRegisterInfo";
93 OS << "struct " << ClassName << " : public MCRegisterInfo {\n"
94 << " explicit " << ClassName << "(const MCRegisterDesc *D);\n";
97 OS << "\nnamespace {\n";
99 const std::vector<CodeGenRegister*> &Regs = RegBank.getRegisters();
101 // Emit an overlap list for all registers.
102 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
103 const CodeGenRegister *Reg = Regs[i];
104 const CodeGenRegister::Set &O = Overlaps[Reg];
105 // Move Reg to the front so TRI::getAliasSet can share the list.
106 OS << " const unsigned " << Reg->getName() << "_Overlaps[] = { "
107 << getQualifiedName(Reg->TheDef) << ", ";
108 for (CodeGenRegister::Set::const_iterator I = O.begin(), E = O.end();
111 OS << getQualifiedName((*I)->TheDef) << ", ";
115 // Emit the empty sub-registers list
116 OS << " const unsigned Empty_SubRegsSet[] = { 0 };\n";
117 // Loop over all of the registers which have sub-registers, emitting the
118 // sub-registers list to memory.
119 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
120 const CodeGenRegister &Reg = *Regs[i];
121 if (Reg.getSubRegs().empty())
123 // getSubRegs() orders by SubRegIndex. We want a topological order.
124 SetVector<CodeGenRegister*> SR;
125 Reg.addSubRegsPreOrder(SR);
126 OS << " const unsigned " << Reg.getName() << "_SubRegsSet[] = { ";
127 for (unsigned j = 0, je = SR.size(); j != je; ++j)
128 OS << getQualifiedName(SR[j]->TheDef) << ", ";
132 // Emit the empty super-registers list
133 OS << " const unsigned Empty_SuperRegsSet[] = { 0 };\n";
134 // Loop over all of the registers which have super-registers, emitting the
135 // super-registers list to memory.
136 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
137 const CodeGenRegister &Reg = *Regs[i];
138 const CodeGenRegister::SuperRegList &SR = Reg.getSuperRegs();
141 OS << " const unsigned " << Reg.getName() << "_SuperRegsSet[] = { ";
142 for (unsigned j = 0, je = SR.size(); j != je; ++j)
143 OS << getQualifiedName(SR[j]->TheDef) << ", ";
147 OS << "\n const MCRegisterDesc " << TargetName
148 << "RegDesc[] = { // Descriptors\n";
149 OS << " { \"NOREG\",\t0,\t0,\t0 },\n";
151 // Now that register alias and sub-registers sets have been emitted, emit the
152 // register descriptors now.
153 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
154 const CodeGenRegister &Reg = *Regs[i];
156 OS << Reg.getName() << "\",\t" << Reg.getName() << "_Overlaps,\t";
157 if (!Reg.getSubRegs().empty())
158 OS << Reg.getName() << "_SubRegsSet,\t";
160 OS << "Empty_SubRegsSet,\t";
161 if (!Reg.getSuperRegs().empty())
162 OS << Reg.getName() << "_SuperRegsSet";
164 OS << "Empty_SuperRegsSet";
167 OS << " };\n"; // End of register descriptors...
169 OS << "}\n\n"; // End of anonymous namespace...
171 // MCRegisterInfo initialization routine.
172 OS << "static inline void Init" << TargetName
173 << "MCRegisterInfo(MCRegisterInfo *RI) {\n";
174 OS << " RI->InitMCRegisterInfo(" << TargetName << "RegDesc, "
175 << Regs.size()+1 << ");\n}\n\n";
177 OS << "} // End llvm namespace \n";
178 OS << "#endif // GET_REGINFO_MC_DESC\n\n";
182 RegisterInfoEmitter::runTargetHeader(raw_ostream &OS, CodeGenTarget &Target,
183 CodeGenRegBank &RegBank) {
184 EmitSourceFileHeader("Register Information Header Fragment", OS);
186 OS << "\n#ifdef GET_REGINFO_HEADER\n";
187 OS << "#undef GET_REGINFO_HEADER\n";
189 const std::string &TargetName = Target.getName();
190 std::string ClassName = TargetName + "GenRegisterInfo";
192 OS << "#include \"llvm/Target/TargetRegisterInfo.h\"\n";
193 OS << "#include <string>\n\n";
195 OS << "namespace llvm {\n\n";
197 OS << "struct " << ClassName << " : public TargetRegisterInfo {\n"
198 << " explicit " << ClassName
199 << "(const MCRegisterDesc *D, const TargetRegisterInfoDesc *ID, "
200 << "int CallFrameSetupOpcode = -1, int CallFrameDestroyOpcode = -1);\n"
201 << " virtual int getDwarfRegNumFull(unsigned RegNum, "
202 << "unsigned Flavour) const;\n"
203 << " virtual int getLLVMRegNumFull(unsigned DwarfRegNum, "
204 << "unsigned Flavour) const;\n"
205 << " virtual int getDwarfRegNum(unsigned RegNum, bool isEH) const = 0;\n"
206 << " virtual bool needsStackRealignment(const MachineFunction &) const\n"
207 << " { return false; }\n"
208 << " unsigned getSubReg(unsigned RegNo, unsigned Index) const;\n"
209 << " unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const;\n"
210 << " unsigned composeSubRegIndices(unsigned, unsigned) const;\n"
213 const std::vector<Record*> &SubRegIndices = RegBank.getSubRegIndices();
214 if (!SubRegIndices.empty()) {
215 OS << "\n// Subregister indices\n";
216 std::string Namespace = SubRegIndices[0]->getValueAsString("Namespace");
217 if (!Namespace.empty())
218 OS << "namespace " << Namespace << " {\n";
219 OS << "enum {\n NoSubRegister,\n";
220 for (unsigned i = 0, e = RegBank.getNumNamedIndices(); i != e; ++i)
221 OS << " " << SubRegIndices[i]->getName() << ",\t// " << i+1 << "\n";
222 OS << " NUM_TARGET_NAMED_SUBREGS = " << SubRegIndices.size()+1 << "\n";
224 if (!Namespace.empty())
228 const std::vector<CodeGenRegisterClass> &RegisterClasses =
229 Target.getRegisterClasses();
231 if (!RegisterClasses.empty()) {
232 OS << "namespace " << RegisterClasses[0].Namespace
233 << " { // Register classes\n";
235 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
236 const CodeGenRegisterClass &RC = RegisterClasses[i];
237 const std::string &Name = RC.getName();
239 // Output the register class definition.
240 OS << " struct " << Name << "Class : public TargetRegisterClass {\n"
241 << " " << Name << "Class();\n";
242 if (!RC.AltOrderSelect.empty())
243 OS << " ArrayRef<unsigned> "
244 "getRawAllocationOrder(const MachineFunction&) const;\n";
247 // Output the extern for the instance.
248 OS << " extern " << Name << "Class\t" << Name << "RegClass;\n";
249 // Output the extern for the pointer to the instance (should remove).
250 OS << " static TargetRegisterClass * const "<< Name <<"RegisterClass = &"
251 << Name << "RegClass;\n";
253 OS << "} // end of namespace " << TargetName << "\n\n";
255 OS << "} // End llvm namespace \n";
256 OS << "#endif // GET_REGINFO_HEADER\n\n";
260 // runTargetDesc - Output the target register and register file descriptions.
263 RegisterInfoEmitter::runTargetDesc(raw_ostream &OS, CodeGenTarget &Target,
264 CodeGenRegBank &RegBank){
265 EmitSourceFileHeader("Target Register and Register Classes Information", OS);
267 OS << "\n#ifdef GET_REGINFO_TARGET_DESC\n";
268 OS << "#undef GET_REGINFO_TARGET_DESC\n";
270 OS << "namespace llvm {\n\n";
272 // Start out by emitting each of the register classes.
273 const std::vector<CodeGenRegisterClass> &RegisterClasses =
274 Target.getRegisterClasses();
276 // Collect all registers belonging to any allocatable class.
277 std::set<Record*> AllocatableRegs;
279 // Loop over all of the register classes... emitting each one.
280 OS << "namespace { // Register classes...\n";
282 // Emit the register enum value arrays for each RegisterClass
283 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
284 const CodeGenRegisterClass &RC = RegisterClasses[rc];
285 ArrayRef<Record*> Order = RC.getOrder();
287 // Collect allocatable registers.
289 AllocatableRegs.insert(Order.begin(), Order.end());
291 // Give the register class a legal C name if it's anonymous.
292 std::string Name = RC.getName();
294 // Emit the register list now.
295 OS << " // " << Name << " Register Class...\n"
296 << " static const unsigned " << Name
298 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
299 Record *Reg = Order[i];
300 OS << getQualifiedName(Reg) << ", ";
305 // Emit the ValueType arrays for each RegisterClass
306 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
307 const CodeGenRegisterClass &RC = RegisterClasses[rc];
309 // Give the register class a legal C name if it's anonymous.
310 std::string Name = RC.getName() + "VTs";
312 // Emit the register list now.
314 << " Register Class Value Types...\n"
315 << " static const EVT " << Name
317 for (unsigned i = 0, e = RC.VTs.size(); i != e; ++i)
318 OS << getEnumName(RC.VTs[i]) << ", ";
319 OS << "MVT::Other\n };\n\n";
321 OS << "} // end anonymous namespace\n\n";
323 // Now that all of the structs have been emitted, emit the instances.
324 if (!RegisterClasses.empty()) {
325 OS << "namespace " << RegisterClasses[0].Namespace
326 << " { // Register class instances\n";
327 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i)
328 OS << " " << RegisterClasses[i].getName() << "Class\t"
329 << RegisterClasses[i].getName() << "RegClass;\n";
331 std::map<unsigned, std::set<unsigned> > SuperClassMap;
332 std::map<unsigned, std::set<unsigned> > SuperRegClassMap;
335 unsigned NumSubRegIndices = RegBank.getSubRegIndices().size();
337 if (NumSubRegIndices) {
338 // Emit the sub-register classes for each RegisterClass
339 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
340 const CodeGenRegisterClass &RC = RegisterClasses[rc];
341 std::vector<Record*> SRC(NumSubRegIndices);
342 for (DenseMap<Record*,Record*>::const_iterator
343 i = RC.SubRegClasses.begin(),
344 e = RC.SubRegClasses.end(); i != e; ++i) {
346 unsigned idx = RegBank.getSubRegIndexNo(i->first);
347 SRC.at(idx-1) = i->second;
349 // Find the register class number of i->second for SuperRegClassMap.
350 for (unsigned rc2 = 0, e2 = RegisterClasses.size(); rc2 != e2; ++rc2) {
351 const CodeGenRegisterClass &RC2 = RegisterClasses[rc2];
352 if (RC2.TheDef == i->second) {
353 SuperRegClassMap[rc2].insert(rc);
359 // Give the register class a legal C name if it's anonymous.
360 std::string Name = RC.TheDef->getName();
363 << " Sub-register Classes...\n"
364 << " static const TargetRegisterClass* const "
365 << Name << "SubRegClasses[] = {\n ";
367 for (unsigned idx = 0; idx != NumSubRegIndices; ++idx) {
371 OS << "&" << getQualifiedName(SRC[idx]) << "RegClass";
378 // Emit the super-register classes for each RegisterClass
379 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
380 const CodeGenRegisterClass &RC = RegisterClasses[rc];
382 // Give the register class a legal C name if it's anonymous.
383 std::string Name = RC.TheDef->getName();
386 << " Super-register Classes...\n"
387 << " static const TargetRegisterClass* const "
388 << Name << "SuperRegClasses[] = {\n ";
391 std::map<unsigned, std::set<unsigned> >::iterator I =
392 SuperRegClassMap.find(rc);
393 if (I != SuperRegClassMap.end()) {
394 for (std::set<unsigned>::iterator II = I->second.begin(),
395 EE = I->second.end(); II != EE; ++II) {
396 const CodeGenRegisterClass &RC2 = RegisterClasses[*II];
399 OS << "&" << getQualifiedName(RC2.TheDef) << "RegClass";
404 OS << (!Empty ? ", " : "") << "NULL";
408 // No subregindices in this target
409 OS << " static const TargetRegisterClass* const "
410 << "NullRegClasses[] = { NULL };\n\n";
413 // Emit the sub-classes array for each RegisterClass
414 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
415 const CodeGenRegisterClass &RC = RegisterClasses[rc];
417 // Give the register class a legal C name if it's anonymous.
418 std::string Name = RC.TheDef->getName();
421 << " Register Class sub-classes...\n"
422 << " static const TargetRegisterClass* const "
423 << Name << "Subclasses[] = {\n ";
426 for (unsigned rc2 = 0, e2 = RegisterClasses.size(); rc2 != e2; ++rc2) {
427 const CodeGenRegisterClass &RC2 = RegisterClasses[rc2];
429 // Sub-classes are used to determine if a virtual register can be used
430 // as an instruction operand, or if it must be copied first.
431 if (rc == rc2 || !RC.hasSubClass(&RC2)) continue;
433 if (!Empty) OS << ", ";
434 OS << "&" << getQualifiedName(RC2.TheDef) << "RegClass";
437 std::map<unsigned, std::set<unsigned> >::iterator SCMI =
438 SuperClassMap.find(rc2);
439 if (SCMI == SuperClassMap.end()) {
440 SuperClassMap.insert(std::make_pair(rc2, std::set<unsigned>()));
441 SCMI = SuperClassMap.find(rc2);
443 SCMI->second.insert(rc);
446 OS << (!Empty ? ", " : "") << "NULL";
450 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
451 const CodeGenRegisterClass &RC = RegisterClasses[rc];
453 // Give the register class a legal C name if it's anonymous.
454 std::string Name = RC.TheDef->getName();
457 << " Register Class super-classes...\n"
458 << " static const TargetRegisterClass* const "
459 << Name << "Superclasses[] = {\n ";
462 std::map<unsigned, std::set<unsigned> >::iterator I =
463 SuperClassMap.find(rc);
464 if (I != SuperClassMap.end()) {
465 for (std::set<unsigned>::iterator II = I->second.begin(),
466 EE = I->second.end(); II != EE; ++II) {
467 const CodeGenRegisterClass &RC2 = RegisterClasses[*II];
468 if (!Empty) OS << ", ";
469 OS << "&" << getQualifiedName(RC2.TheDef) << "RegClass";
474 OS << (!Empty ? ", " : "") << "NULL";
479 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
480 const CodeGenRegisterClass &RC = RegisterClasses[i];
481 OS << RC.getName() << "Class::" << RC.getName()
482 << "Class() : TargetRegisterClass("
483 << RC.getName() + "RegClassID" << ", "
484 << '\"' << RC.getName() << "\", "
485 << RC.getName() + "VTs" << ", "
486 << RC.getName() + "Subclasses" << ", "
487 << RC.getName() + "Superclasses" << ", "
488 << (NumSubRegIndices ? RC.getName() + "Sub" : std::string("Null"))
490 << (NumSubRegIndices ? RC.getName() + "Super" : std::string("Null"))
492 << RC.SpillSize/8 << ", "
493 << RC.SpillAlignment/8 << ", "
494 << RC.CopyCost << ", "
495 << RC.Allocatable << ", "
496 << RC.getName() << ", " << RC.getName() << " + "
497 << RC.getOrder().size()
499 if (!RC.AltOrderSelect.empty()) {
500 OS << "\nstatic inline unsigned " << RC.getName()
501 << "AltOrderSelect(const MachineFunction &MF) {"
502 << RC.AltOrderSelect << "}\n\nArrayRef<unsigned> "
503 << RC.getName() << "Class::"
504 << "getRawAllocationOrder(const MachineFunction &MF) const {\n";
505 for (unsigned oi = 1 , oe = RC.getNumOrders(); oi != oe; ++oi) {
506 ArrayRef<Record*> Elems = RC.getOrder(oi);
507 OS << " static const unsigned AltOrder" << oi << "[] = {";
508 for (unsigned elem = 0; elem != Elems.size(); ++elem)
509 OS << (elem ? ", " : " ") << getQualifiedName(Elems[elem]);
512 OS << " static const ArrayRef<unsigned> Order[] = {\n"
513 << " ArrayRef<unsigned>(" << RC.getName();
514 for (unsigned oi = 1, oe = RC.getNumOrders(); oi != oe; ++oi)
515 OS << "),\n ArrayRef<unsigned>(AltOrder" << oi;
516 OS << ")\n };\n const unsigned Select = " << RC.getName()
517 << "AltOrderSelect(MF);\n assert(Select < " << RC.getNumOrders()
518 << ");\n return Order[Select];\n}\n";
525 OS << "\nnamespace {\n";
526 OS << " const TargetRegisterClass* const RegisterClasses[] = {\n";
527 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i)
528 OS << " &" << getQualifiedName(RegisterClasses[i].TheDef)
532 // Emit extra information about registers.
533 OS << "\n static const TargetRegisterInfoDesc "
534 << Target.getName() << "RegInfoDesc[] = "
535 << "{ // Extra Descriptors\n";
536 OS << " { 0, 0 },\n";
538 const std::vector<CodeGenRegister*> &Regs = RegBank.getRegisters();
539 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
540 const CodeGenRegister &Reg = *Regs[i];
542 OS << Reg.CostPerUse << ", "
543 << int(AllocatableRegs.count(Reg.TheDef)) << " },\n";
545 OS << " };\n"; // End of register descriptors...
548 // Calculate the mapping of subregister+index pairs to physical registers.
549 // This will also create further anonymous indexes.
550 unsigned NamedIndices = RegBank.getNumNamedIndices();
552 // Emit SubRegIndex names, skipping 0
553 const std::vector<Record*> &SubRegIndices = RegBank.getSubRegIndices();
554 OS << "\n const char *const SubRegIndexTable[] = { \"";
555 for (unsigned i = 0, e = SubRegIndices.size(); i != e; ++i) {
556 OS << SubRegIndices[i]->getName();
562 // Emit names of the anonymus subreg indexes.
563 if (SubRegIndices.size() > NamedIndices) {
565 for (unsigned i = NamedIndices, e = SubRegIndices.size(); i != e; ++i) {
566 OS << "\n " << SubRegIndices[i]->getName() << " = " << i+1;
572 OS << "}\n\n"; // End of anonymous namespace...
574 std::string ClassName = Target.getName() + "GenRegisterInfo";
576 // Emit the subregister + index mapping function based on the information
578 OS << "unsigned " << ClassName
579 << "::getSubReg(unsigned RegNo, unsigned Index) const {\n"
580 << " switch (RegNo) {\n"
581 << " default:\n return 0;\n";
582 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
583 const CodeGenRegister::SubRegMap &SRM = Regs[i]->getSubRegs();
586 OS << " case " << getQualifiedName(Regs[i]->TheDef) << ":\n";
587 OS << " switch (Index) {\n";
588 OS << " default: return 0;\n";
589 for (CodeGenRegister::SubRegMap::const_iterator ii = SRM.begin(),
590 ie = SRM.end(); ii != ie; ++ii)
591 OS << " case " << getQualifiedName(ii->first)
592 << ": return " << getQualifiedName(ii->second->TheDef) << ";\n";
593 OS << " };\n" << " break;\n";
596 OS << " return 0;\n";
599 OS << "unsigned " << ClassName
600 << "::getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const {\n"
601 << " switch (RegNo) {\n"
602 << " default:\n return 0;\n";
603 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
604 const CodeGenRegister::SubRegMap &SRM = Regs[i]->getSubRegs();
607 OS << " case " << getQualifiedName(Regs[i]->TheDef) << ":\n";
608 for (CodeGenRegister::SubRegMap::const_iterator ii = SRM.begin(),
609 ie = SRM.end(); ii != ie; ++ii)
610 OS << " if (SubRegNo == " << getQualifiedName(ii->second->TheDef)
611 << ") return " << getQualifiedName(ii->first) << ";\n";
612 OS << " return 0;\n";
615 OS << " return 0;\n";
618 // Emit composeSubRegIndices
619 OS << "unsigned " << ClassName
620 << "::composeSubRegIndices(unsigned IdxA, unsigned IdxB) const {\n"
621 << " switch (IdxA) {\n"
622 << " default:\n return IdxB;\n";
623 for (unsigned i = 0, e = SubRegIndices.size(); i != e; ++i) {
625 for (unsigned j = 0; j != e; ++j) {
626 if (Record *Comp = RegBank.getCompositeSubRegIndex(SubRegIndices[i],
629 OS << " case " << getQualifiedName(SubRegIndices[i])
630 << ": switch(IdxB) {\n default: return IdxB;\n";
633 OS << " case " << getQualifiedName(SubRegIndices[j])
634 << ": return " << getQualifiedName(Comp) << ";\n";
642 // Emit the constructor of the class...
643 OS << ClassName << "::" << ClassName
644 << "(const MCRegisterDesc *D, const TargetRegisterInfoDesc *ID, "
645 << "int CallFrameSetupOpcode, int CallFrameDestroyOpcode)\n"
646 << " : TargetRegisterInfo(ID"
647 << ", RegisterClasses, RegisterClasses+" << RegisterClasses.size() <<",\n"
648 << " SubRegIndexTable,\n"
649 << " CallFrameSetupOpcode, CallFrameDestroyOpcode) {\n"
650 << " InitMCRegisterInfo(D, " << Regs.size()+1 << ");\n"
653 // Collect all information about dwarf register numbers
654 typedef std::map<Record*, std::vector<int64_t>, LessRecord> DwarfRegNumsMapTy;
655 DwarfRegNumsMapTy DwarfRegNums;
657 // First, just pull all provided information to the map
658 unsigned maxLength = 0;
659 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
660 Record *Reg = Regs[i]->TheDef;
661 std::vector<int64_t> RegNums = Reg->getValueAsListOfInts("DwarfNumbers");
662 maxLength = std::max((size_t)maxLength, RegNums.size());
663 if (DwarfRegNums.count(Reg))
664 errs() << "Warning: DWARF numbers for register " << getQualifiedName(Reg)
665 << "specified multiple times\n";
666 DwarfRegNums[Reg] = RegNums;
669 // Now we know maximal length of number list. Append -1's, where needed
670 for (DwarfRegNumsMapTy::iterator
671 I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I)
672 for (unsigned i = I->second.size(), e = maxLength; i != e; ++i)
673 I->second.push_back(-1);
675 // Emit reverse information about the dwarf register numbers.
676 OS << "int " << ClassName << "::getLLVMRegNumFull(unsigned DwarfRegNum, "
677 << "unsigned Flavour) const {\n"
678 << " switch (Flavour) {\n"
680 << " assert(0 && \"Unknown DWARF flavour\");\n"
683 for (unsigned i = 0, e = maxLength; i != e; ++i) {
684 OS << " case " << i << ":\n"
685 << " switch (DwarfRegNum) {\n"
687 << " assert(0 && \"Invalid DwarfRegNum\");\n"
690 for (DwarfRegNumsMapTy::iterator
691 I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I) {
692 int DwarfRegNo = I->second[i];
694 OS << " case " << DwarfRegNo << ":\n"
695 << " return " << getQualifiedName(I->first) << ";\n";
702 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
703 Record *Reg = Regs[i]->TheDef;
704 const RecordVal *V = Reg->getValue("DwarfAlias");
705 if (!V || !V->getValue())
708 DefInit *DI = dynamic_cast<DefInit*>(V->getValue());
709 Record *Alias = DI->getDef();
710 DwarfRegNums[Reg] = DwarfRegNums[Alias];
713 // Emit information about the dwarf register numbers.
714 OS << "int " << ClassName << "::getDwarfRegNumFull(unsigned RegNum, "
715 << "unsigned Flavour) const {\n"
716 << " switch (Flavour) {\n"
718 << " assert(0 && \"Unknown DWARF flavour\");\n"
721 for (unsigned i = 0, e = maxLength; i != e; ++i) {
722 OS << " case " << i << ":\n"
723 << " switch (RegNum) {\n"
725 << " assert(0 && \"Invalid RegNum\");\n"
728 // Sort by name to get a stable order.
731 for (DwarfRegNumsMapTy::iterator
732 I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I) {
733 int RegNo = I->second[i];
734 OS << " case " << getQualifiedName(I->first) << ":\n"
735 << " return " << RegNo << ";\n";
742 OS << "} // End llvm namespace \n";
743 OS << "#endif // GET_REGINFO_TARGET_DESC\n\n";
746 void RegisterInfoEmitter::run(raw_ostream &OS) {
747 CodeGenTarget Target(Records);
748 CodeGenRegBank &RegBank = Target.getRegBank();
749 RegBank.computeDerivedInfo();
751 runEnums(OS, Target, RegBank);
752 runMCDesc(OS, Target, RegBank);
753 runTargetHeader(OS, Target, RegBank);
754 runTargetDesc(OS, Target, RegBank);