1 //===- RegisterInfoEmitter.cpp - Generate a Register File Desc. -*- C++ -*-===//
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 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/Streams.h"
26 // runEnums - Print out enum values for all of the registers.
27 void RegisterInfoEmitter::runEnums(std::ostream &OS) {
29 const std::vector<CodeGenRegister> &Registers = Target.getRegisters();
31 std::string Namespace = Registers[0].TheDef->getValueAsString("Namespace");
33 EmitSourceFileHeader("Target Register Enum Values", OS);
34 OS << "namespace llvm {\n\n";
36 if (!Namespace.empty())
37 OS << "namespace " << Namespace << " {\n";
38 OS << " enum {\n NoRegister,\n";
40 for (unsigned i = 0, e = Registers.size(); i != e; ++i)
41 OS << " " << Registers[i].getName() << ", \t// " << i+1 << "\n";
42 OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
44 if (!Namespace.empty())
46 OS << "} // End llvm namespace \n";
49 void RegisterInfoEmitter::runHeader(std::ostream &OS) {
50 EmitSourceFileHeader("Register Information Header Fragment", OS);
52 const std::string &TargetName = Target.getName();
53 std::string ClassName = TargetName + "GenRegisterInfo";
55 OS << "#include \"llvm/Target/MRegisterInfo.h\"\n";
56 OS << "#include <string>\n\n";
58 OS << "namespace llvm {\n\n";
60 OS << "struct " << ClassName << " : public MRegisterInfo {\n"
62 << "(int CallFrameSetupOpcode = -1, int CallFrameDestroyOpcode = -1);\n"
63 << " int getDwarfRegNum(unsigned RegNum) const;\n"
64 << " unsigned getSubReg(unsigned RegNo, unsigned Index) const;\n"
67 const std::vector<CodeGenRegisterClass> &RegisterClasses =
68 Target.getRegisterClasses();
70 if (!RegisterClasses.empty()) {
71 OS << "namespace " << RegisterClasses[0].Namespace
72 << " { // Register classes\n";
75 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
77 OS << " " << RegisterClasses[i].getName() << "RegClassID";
82 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
83 const std::string &Name = RegisterClasses[i].getName();
85 // Output the register class definition.
86 OS << " struct " << Name << "Class : public TargetRegisterClass {\n"
87 << " " << Name << "Class();\n"
88 << RegisterClasses[i].MethodProtos << " };\n";
90 // Output the extern for the instance.
91 OS << " extern " << Name << "Class\t" << Name << "RegClass;\n";
92 // Output the extern for the pointer to the instance (should remove).
93 OS << " static TargetRegisterClass * const "<< Name <<"RegisterClass = &"
94 << Name << "RegClass;\n";
96 OS << "} // end of namespace " << TargetName << "\n\n";
98 OS << "} // End llvm namespace \n";
101 bool isSubRegisterClass(const CodeGenRegisterClass &RC,
102 std::set<Record*> &RegSet) {
103 for (unsigned i = 0, e = RC.Elements.size(); i != e; ++i) {
104 Record *Reg = RC.Elements[i];
105 if (!RegSet.count(Reg))
111 static void addSuperReg(Record *R, Record *S,
112 std::map<Record*, std::set<Record*> > &SubRegs,
113 std::map<Record*, std::set<Record*> > &SuperRegs,
114 std::map<Record*, std::set<Record*> > &Aliases,
115 RegisterInfoEmitter &RIE) {
117 cerr << "Error: recursive sub-register relationship between"
118 << " register " << RIE.getQualifiedName(R)
119 << " and its sub-registers?\n";
122 if (!SuperRegs[R].insert(S).second)
124 SubRegs[S].insert(R);
125 Aliases[R].insert(S);
126 Aliases[S].insert(R);
127 if (SuperRegs.count(S))
128 for (std::set<Record*>::iterator I = SuperRegs[S].begin(),
129 E = SuperRegs[S].end(); I != E; ++I)
130 addSuperReg(R, *I, SubRegs, SuperRegs, Aliases, RIE);
133 static void addSubSuperReg(Record *R, Record *S,
134 std::map<Record*, std::set<Record*> > &SubRegs,
135 std::map<Record*, std::set<Record*> > &SuperRegs,
136 std::map<Record*, std::set<Record*> > &Aliases,
137 RegisterInfoEmitter &RIE) {
139 cerr << "Error: recursive sub-register relationship between"
140 << " register " << RIE.getQualifiedName(R)
141 << " and its sub-registers?\n";
145 if (!SubRegs[R].insert(S).second)
147 addSuperReg(S, R, SubRegs, SuperRegs, Aliases, RIE);
148 Aliases[R].insert(S);
149 Aliases[S].insert(R);
150 if (SubRegs.count(S))
151 for (std::set<Record*>::iterator I = SubRegs[S].begin(),
152 E = SubRegs[S].end(); I != E; ++I)
153 addSubSuperReg(R, *I, SubRegs, SuperRegs, Aliases, RIE);
156 // RegisterInfoEmitter::run - Main register file description emitter.
158 void RegisterInfoEmitter::run(std::ostream &OS) {
159 CodeGenTarget Target;
160 EmitSourceFileHeader("Register Information Source Fragment", OS);
162 OS << "namespace llvm {\n\n";
164 // Start out by emitting each of the register classes... to do this, we build
165 // a set of registers which belong to a register class, this is to ensure that
166 // each register is only in a single register class.
168 const std::vector<CodeGenRegisterClass> &RegisterClasses =
169 Target.getRegisterClasses();
171 // Loop over all of the register classes... emitting each one.
172 OS << "namespace { // Register classes...\n";
174 // RegClassesBelongedTo - Keep track of which register classes each reg
176 std::multimap<Record*, const CodeGenRegisterClass*> RegClassesBelongedTo;
178 // Emit the register enum value arrays for each RegisterClass
179 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
180 const CodeGenRegisterClass &RC = RegisterClasses[rc];
182 // Give the register class a legal C name if it's anonymous.
183 std::string Name = RC.TheDef->getName();
185 // Emit the register list now.
186 OS << " // " << Name << " Register Class...\n"
187 << " static const unsigned " << Name
189 for (unsigned i = 0, e = RC.Elements.size(); i != e; ++i) {
190 Record *Reg = RC.Elements[i];
191 OS << getQualifiedName(Reg) << ", ";
193 // Keep track of which regclasses this register is in.
194 RegClassesBelongedTo.insert(std::make_pair(Reg, &RC));
199 // Emit the ValueType arrays for each RegisterClass
200 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
201 const CodeGenRegisterClass &RC = RegisterClasses[rc];
203 // Give the register class a legal C name if it's anonymous.
204 std::string Name = RC.TheDef->getName() + "VTs";
206 // Emit the register list now.
208 << " Register Class Value Types...\n"
209 << " static const MVT::ValueType " << Name
211 for (unsigned i = 0, e = RC.VTs.size(); i != e; ++i)
212 OS << getName(RC.VTs[i]) << ", ";
213 OS << "MVT::Other\n };\n\n";
215 OS << "} // end anonymous namespace\n\n";
217 // Now that all of the structs have been emitted, emit the instances.
218 if (!RegisterClasses.empty()) {
219 OS << "namespace " << RegisterClasses[0].Namespace
220 << " { // Register class instances\n";
221 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i)
222 OS << " " << RegisterClasses[i].getName() << "Class\t"
223 << RegisterClasses[i].getName() << "RegClass;\n";
225 std::map<unsigned, std::set<unsigned> > SuperClassMap;
229 // Emit the sub-register classes for each RegisterClass
230 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
231 const CodeGenRegisterClass &RC = RegisterClasses[rc];
233 // Give the register class a legal C name if it's anonymous.
234 std::string Name = RC.TheDef->getName();
237 << " Sub-register Classess...\n"
238 << " static const TargetRegisterClass* const "
239 << Name << "SubRegClasses [] = {\n ";
243 for (unsigned subrc = 0, e2 = RC.SubRegClasses.size();
244 subrc != e2; ++subrc) {
245 unsigned rc2 = 0, e2 = RegisterClasses.size();
246 for (; rc2 != e2; ++rc2) {
247 const CodeGenRegisterClass &RC2 = RegisterClasses[rc2];
248 if (RC.SubRegClasses[subrc]->getName() == RC2.getName()) {
249 if (!Empty) OS << ", ";
250 OS << "&" << getQualifiedName(RC2.TheDef) << "RegClass";
256 throw "Register Class member '" +
257 RC.SubRegClasses[subrc]->getName() +
258 "' is not a valid RegisterClass!";
261 OS << (!Empty ? ", " : "") << "NULL";
265 // Emit the sub-classes array for each RegisterClass
266 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
267 const CodeGenRegisterClass &RC = RegisterClasses[rc];
269 // Give the register class a legal C name if it's anonymous.
270 std::string Name = RC.TheDef->getName();
272 std::set<Record*> RegSet;
273 for (unsigned i = 0, e = RC.Elements.size(); i != e; ++i) {
274 Record *Reg = RC.Elements[i];
279 << " Register Class sub-classes...\n"
280 << " static const TargetRegisterClass* const "
281 << Name << "Subclasses [] = {\n ";
284 for (unsigned rc2 = 0, e2 = RegisterClasses.size(); rc2 != e2; ++rc2) {
285 const CodeGenRegisterClass &RC2 = RegisterClasses[rc2];
286 if (rc == rc2 || RC2.Elements.size() > RC.Elements.size() ||
287 RC.SpillSize != RC2.SpillSize || !isSubRegisterClass(RC2, RegSet))
290 if (!Empty) OS << ", ";
291 OS << "&" << getQualifiedName(RC2.TheDef) << "RegClass";
294 std::map<unsigned, std::set<unsigned> >::iterator SCMI =
295 SuperClassMap.find(rc2);
296 if (SCMI == SuperClassMap.end()) {
297 SuperClassMap.insert(std::make_pair(rc2, std::set<unsigned>()));
298 SCMI = SuperClassMap.find(rc2);
300 SCMI->second.insert(rc);
303 OS << (!Empty ? ", " : "") << "NULL";
307 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
308 const CodeGenRegisterClass &RC = RegisterClasses[rc];
310 // Give the register class a legal C name if it's anonymous.
311 std::string Name = RC.TheDef->getName();
314 << " Register Class super-classes...\n"
315 << " static const TargetRegisterClass* const "
316 << Name << "Superclasses [] = {\n ";
319 std::map<unsigned, std::set<unsigned> >::iterator I =
320 SuperClassMap.find(rc);
321 if (I != SuperClassMap.end()) {
322 for (std::set<unsigned>::iterator II = I->second.begin(),
323 EE = I->second.end(); II != EE; ++II) {
324 const CodeGenRegisterClass &RC2 = RegisterClasses[*II];
325 if (!Empty) OS << ", ";
326 OS << "&" << getQualifiedName(RC2.TheDef) << "RegClass";
331 OS << (!Empty ? ", " : "") << "NULL";
336 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
337 const CodeGenRegisterClass &RC = RegisterClasses[i];
338 OS << RC.MethodBodies << "\n";
339 OS << RC.getName() << "Class::" << RC.getName()
340 << "Class() : TargetRegisterClass("
341 << RC.getName() + "RegClassID" << ", "
342 << RC.getName() + "VTs" << ", "
343 << RC.getName() + "Subclasses" << ", "
344 << RC.getName() + "Superclasses" << ", "
345 << RC.getName() + "SubRegClasses" << ", "
346 << RC.SpillSize/8 << ", "
347 << RC.SpillAlignment/8 << ", " << RC.getName() << ", "
348 << RC.getName() << " + " << RC.Elements.size() << ") {}\n";
354 OS << "\nnamespace {\n";
355 OS << " const TargetRegisterClass* const RegisterClasses[] = {\n";
356 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i)
357 OS << " &" << getQualifiedName(RegisterClasses[i].TheDef)
361 // Emit register sub-registers / super-registers, aliases...
362 std::map<Record*, std::set<Record*> > RegisterSubRegs;
363 std::map<Record*, std::set<Record*> > RegisterSuperRegs;
364 std::map<Record*, std::set<Record*> > RegisterAliases;
365 std::map<Record*, std::vector<std::pair<int, Record*> > > SubRegVectors;
366 const std::vector<CodeGenRegister> &Regs = Target.getRegisters();
368 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
369 Record *R = Regs[i].TheDef;
370 std::vector<Record*> LI = Regs[i].TheDef->getValueAsListOfDefs("Aliases");
371 // Add information that R aliases all of the elements in the list... and
372 // that everything in the list aliases R.
373 for (unsigned j = 0, e = LI.size(); j != e; ++j) {
375 if (RegisterAliases[R].count(Reg))
376 cerr << "Warning: register alias between " << getQualifiedName(R)
377 << " and " << getQualifiedName(Reg)
378 << " specified multiple times!\n";
379 RegisterAliases[R].insert(Reg);
381 if (RegisterAliases[Reg].count(R))
382 cerr << "Warning: register alias between " << getQualifiedName(R)
383 << " and " << getQualifiedName(Reg)
384 << " specified multiple times!\n";
385 RegisterAliases[Reg].insert(R);
389 // Process sub-register sets.
390 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
391 Record *R = Regs[i].TheDef;
392 std::vector<Record*> LI = Regs[i].TheDef->getValueAsListOfDefs("SubRegs");
393 // Process sub-register set and add aliases information.
394 for (unsigned j = 0, e = LI.size(); j != e; ++j) {
395 Record *SubReg = LI[j];
396 if (RegisterSubRegs[R].count(SubReg))
397 cerr << "Warning: register " << getQualifiedName(SubReg)
398 << " specified as a sub-register of " << getQualifiedName(R)
399 << " multiple times!\n";
400 addSubSuperReg(R, SubReg, RegisterSubRegs, RegisterSuperRegs,
401 RegisterAliases, *this);
405 if (!RegisterAliases.empty())
406 OS << "\n\n // Register Alias Sets...\n";
408 // Emit the empty alias list
409 OS << " const unsigned Empty_AliasSet[] = { 0 };\n";
410 // Loop over all of the registers which have aliases, emitting the alias list
412 for (std::map<Record*, std::set<Record*> >::iterator
413 I = RegisterAliases.begin(), E = RegisterAliases.end(); I != E; ++I) {
414 OS << " const unsigned " << I->first->getName() << "_AliasSet[] = { ";
415 for (std::set<Record*>::iterator ASI = I->second.begin(),
416 E = I->second.end(); ASI != E; ++ASI)
417 OS << getQualifiedName(*ASI) << ", ";
421 if (!RegisterSubRegs.empty())
422 OS << "\n\n // Register Sub-registers Sets...\n";
424 // Emit the empty sub-registers list
425 OS << " const unsigned Empty_SubRegsSet[] = { 0 };\n";
426 // Loop over all of the registers which have sub-registers, emitting the
427 // sub-registers list to memory.
428 for (std::map<Record*, std::set<Record*> >::iterator
429 I = RegisterSubRegs.begin(), E = RegisterSubRegs.end(); I != E; ++I) {
430 OS << " const unsigned " << I->first->getName() << "_SubRegsSet[] = { ";
431 for (std::set<Record*>::iterator ASI = I->second.begin(),
432 E = I->second.end(); ASI != E; ++ASI)
433 OS << getQualifiedName(*ASI) << ", ";
437 if (!RegisterSuperRegs.empty())
438 OS << "\n\n // Register Super-registers Sets...\n";
440 // Emit the empty super-registers list
441 OS << " const unsigned Empty_SuperRegsSet[] = { 0 };\n";
442 // Loop over all of the registers which have super-registers, emitting the
443 // super-registers list to memory.
444 for (std::map<Record*, std::set<Record*> >::iterator
445 I = RegisterSuperRegs.begin(), E = RegisterSuperRegs.end(); I != E; ++I) {
446 OS << " const unsigned " << I->first->getName() << "_SuperRegsSet[] = { ";
447 for (std::set<Record*>::iterator ASI = I->second.begin(),
448 E = I->second.end(); ASI != E; ++ASI)
449 OS << getQualifiedName(*ASI) << ", ";
453 OS<<"\n const TargetRegisterDesc RegisterDescriptors[] = { // Descriptors\n";
454 OS << " { \"NOREG\",\t0,\t0,\t0 },\n";
456 // Now that register alias and sub-registers sets have been emitted, emit the
457 // register descriptors now.
458 const std::vector<CodeGenRegister> &Registers = Target.getRegisters();
459 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
460 const CodeGenRegister &Reg = Registers[i];
462 if (!Reg.TheDef->getValueAsString("Name").empty())
463 OS << Reg.TheDef->getValueAsString("Name");
467 if (RegisterAliases.count(Reg.TheDef))
468 OS << Reg.getName() << "_AliasSet,\t";
470 OS << "Empty_AliasSet,\t";
471 if (RegisterSubRegs.count(Reg.TheDef))
472 OS << Reg.getName() << "_SubRegsSet,\t";
474 OS << "Empty_SubRegsSet,\t";
475 if (RegisterSuperRegs.count(Reg.TheDef))
476 OS << Reg.getName() << "_SuperRegsSet },\n";
478 OS << "Empty_SuperRegsSet },\n";
480 OS << " };\n"; // End of register descriptors...
481 OS << "}\n\n"; // End of anonymous namespace...
483 std::string ClassName = Target.getName() + "GenRegisterInfo";
485 // Calculate the mapping of subregister+index pairs to physical registers.
486 std::vector<Record*> SubRegs = Records.getAllDerivedDefinitions("SubRegSet");
487 for (unsigned i = 0, e = SubRegs.size(); i != e; ++i) {
488 int subRegIndex = SubRegs[i]->getValueAsInt("index");
489 std::vector<Record*> From = SubRegs[i]->getValueAsListOfDefs("From");
490 std::vector<Record*> To = SubRegs[i]->getValueAsListOfDefs("To");
492 if (From.size() != To.size()) {
493 cerr << "Error: register list and sub-register list not of equal length"
494 << " in SubRegSet\n";
498 // For each entry in from/to vectors, insert the to register at index
499 for (unsigned ii = 0, ee = From.size(); ii != ee; ++ii)
500 SubRegVectors[From[ii]].push_back(std::make_pair(subRegIndex, To[ii]));
503 // Emit the subregister + index mapping function based on the information
505 OS << "unsigned " << ClassName
506 << "::getSubReg(unsigned RegNo, unsigned Index) const {\n"
507 << " switch (RegNo) {\n"
508 << " default: abort(); break;\n";
509 for (std::map<Record*, std::vector<std::pair<int, Record*> > >::iterator
510 I = SubRegVectors.begin(), E = SubRegVectors.end(); I != E; ++I) {
511 OS << " case " << getQualifiedName(I->first) << ":\n";
512 OS << " switch (Index) {\n";
513 OS << " default: abort(); break;\n";
514 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
515 OS << " case " << (I->second)[i].first << ": return "
516 << getQualifiedName((I->second)[i].second) << ";\n";
517 OS << " }; break;\n";
520 OS << " return 0;\n";
523 // Emit the constructor of the class...
524 OS << ClassName << "::" << ClassName
525 << "(int CallFrameSetupOpcode, int CallFrameDestroyOpcode)\n"
526 << " : MRegisterInfo(RegisterDescriptors, " << Registers.size()+1
527 << ", RegisterClasses, RegisterClasses+" << RegisterClasses.size() <<",\n "
528 << " CallFrameSetupOpcode, CallFrameDestroyOpcode) {}\n\n";
530 // Emit information about the dwarf register numbers.
531 OS << "int " << ClassName << "::getDwarfRegNum(unsigned RegNum) const {\n";
532 OS << " static const int DwarfRegNums[] = { -1, // NoRegister";
533 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
534 if (!(i % 16)) OS << "\n ";
535 const CodeGenRegister &Reg = Registers[i];
536 int DwarfRegNum = Reg.TheDef->getValueAsInt("DwarfNumber");
538 if ((i + 1) != e) OS << ", ";
541 OS << " assert(RegNum < (sizeof(DwarfRegNums)/sizeof(int)) &&\n";
542 OS << " \"RegNum exceeds number of registers\");\n";
543 OS << " return DwarfRegNums[RegNum];\n";
546 OS << "} // End llvm namespace \n";