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 "CodeGenWrappers.h"
19 #include "Support/StringExtras.h"
24 // runEnums - Print out enum values for all of the registers.
25 void RegisterInfoEmitter::runEnums(std::ostream &OS) {
26 std::vector<Record*> Registers = Records.getAllDerivedDefinitions("Register");
28 if (Registers.size() == 0)
29 throw std::string("No 'Register' subclasses defined!");
31 std::string Namespace = Registers[0]->getValueAsString("Namespace");
33 EmitSourceFileHeader("Target Register Enum Values", OS);
35 if (!Namespace.empty())
36 OS << "namespace " << Namespace << " {\n";
37 OS << " enum {\n NoRegister,\n";
39 for (unsigned i = 0, e = Registers.size(); i != e; ++i)
40 OS << " " << Registers[i]->getName() << ", \t// " << i+1 << "\n";
43 if (!Namespace.empty())
45 EmitSourceFileTail(OS);
48 void RegisterInfoEmitter::runHeader(std::ostream &OS) {
49 EmitSourceFileHeader("Register Information Header Fragment", OS);
50 const std::string &TargetName = CodeGenTarget().getName();
51 std::string ClassName = TargetName + "GenRegisterInfo";
53 OS << "#include \"llvm/Target/MRegisterInfo.h\"\n\n";
55 OS << "struct " << ClassName << " : public MRegisterInfo {\n"
57 << "(int CallFrameSetupOpcode = -1, int CallFrameDestroyOpcode = -1);\n"
58 << " const unsigned* getCalleeSaveRegs() const;\n"
61 std::vector<Record*> RegisterClasses =
62 Records.getAllDerivedDefinitions("RegisterClass");
64 OS << "namespace " << TargetName << " { // Register classes\n";
65 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
66 if (RegisterClasses[i]->getValueAsBit("isDummyClass"))
67 continue; // Ignore dummies
69 const std::string &Name = RegisterClasses[i]->getName();
70 if (Name.size() < 9 || Name[9] != '.') // Ignore anonymous classes
71 OS << " extern TargetRegisterClass *" << Name << "RegisterClass;\n";
73 OS << "} // end of namespace " << TargetName << "\n\n";
74 EmitSourceFileTail(OS);
77 // RegisterInfoEmitter::run - Main register file description emitter.
79 void RegisterInfoEmitter::run(std::ostream &OS) {
80 EmitSourceFileHeader("Register Information Source Fragment", OS);
82 // Start out by emitting each of the register classes... to do this, we build
83 // a set of registers which belong to a register class, this is to ensure that
84 // each register is only in a single register class.
86 std::vector<Record*> RegisterClasses =
87 Records.getAllDerivedDefinitions("RegisterClass");
89 std::vector<Record*> Registers = Records.getAllDerivedDefinitions("Register");
91 std::set<Record*> RegistersFound;
92 std::vector<std::string> RegClassNames;
94 // Loop over all of the register classes... emitting each one.
95 OS << "namespace { // Register classes...\n";
97 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
98 Record *RC = RegisterClasses[rc];
99 if (RC->getValueAsBit("isDummyClass")) continue; // Ignore dummies
101 std::string Name = RC->getName();
102 if (Name.size() > 9 && Name[9] == '.') {
103 static unsigned AnonCounter = 0;
104 Name = "AnonRegClass_"+utostr(AnonCounter++);
107 RegClassNames.push_back(Name);
109 // Emit the register list now...
110 OS << " // " << Name << " Register Class...\n const unsigned " << Name
112 ListInit *RegList = RC->getValueAsListInit("MemberList");
113 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
114 DefInit *RegDef = dynamic_cast<DefInit*>(RegList->getElement(i));
115 if (!RegDef) throw "Register class member is not a record!";
116 Record *Reg = RegDef->getDef();
117 if (!Reg->isSubClassOf("Register"))
118 throw "Register Class member '" + Reg->getName() +
119 " does not derive from the Register class!";
120 if (RegistersFound.count(Reg))
121 throw "Register '" + Reg->getName() +
122 "' included in multiple register classes!";
123 RegistersFound.insert(Reg);
124 OS << getQualifiedName(Reg) << ", ";
128 OS << " struct " << Name << "Class : public TargetRegisterClass {\n"
129 << " " << Name << "Class() : TargetRegisterClass("
130 << RC->getValueAsInt("Size")/8 << ", " << RC->getValueAsInt("Alignment")
131 << ", " << Name << ", " << Name << " + " << RegList->getSize()
134 if (CodeInit *CI = dynamic_cast<CodeInit*>(RC->getValueInit("Methods")))
135 OS << CI->getValue();
137 throw "Expected 'code' fragment for 'Methods' value in register class '"+
138 RC->getName() + "'!";
140 OS << " } " << Name << "Instance;\n\n";
143 OS << " const TargetRegisterClass* const RegisterClasses[] = {\n";
144 for (unsigned i = 0, e = RegClassNames.size(); i != e; ++i)
145 OS << " &" << RegClassNames[i] << "Instance,\n";
148 // Emit register class aliases...
149 std::vector<Record*> RegisterAliasesRecs =
150 Records.getAllDerivedDefinitions("RegisterAliases");
151 std::map<Record*, std::set<Record*> > RegisterAliases;
153 for (unsigned i = 0, e = RegisterAliasesRecs.size(); i != e; ++i) {
154 Record *AS = RegisterAliasesRecs[i];
155 Record *R = AS->getValueAsDef("Reg");
156 ListInit *LI = AS->getValueAsListInit("Aliases");
158 // Add information that R aliases all of the elements in the list... and
159 // that everything in the list aliases R.
160 for (unsigned j = 0, e = LI->getSize(); j != e; ++j) {
161 DefInit *Reg = dynamic_cast<DefInit*>(LI->getElement(j));
162 if (!Reg) throw "ERROR: Alias list element is not a def!";
163 if (RegisterAliases[R].count(Reg->getDef()))
164 std::cerr << "Warning: register alias between " << getQualifiedName(R)
165 << " and " << getQualifiedName(Reg->getDef())
166 << " specified multiple times!\n";
167 RegisterAliases[R].insert(Reg->getDef());
169 if (RegisterAliases[Reg->getDef()].count(R))
170 std::cerr << "Warning: register alias between " << getQualifiedName(R)
171 << " and " << getQualifiedName(Reg->getDef())
172 << " specified multiple times!\n";
173 RegisterAliases[Reg->getDef()].insert(R);
177 if (!RegisterAliases.empty())
178 OS << "\n\n // Register Alias Sets...\n";
180 // Emit the empty alias list
181 OS << " const unsigned Empty_AliasSet[] = { 0 };\n";
182 // Loop over all of the registers which have aliases, emitting the alias list
184 for (std::map<Record*, std::set<Record*> >::iterator
185 I = RegisterAliases.begin(), E = RegisterAliases.end(); I != E; ++I) {
186 OS << " const unsigned " << I->first->getName() << "_AliasSet[] = { ";
187 for (std::set<Record*>::iterator ASI = I->second.begin(),
188 E = I->second.end(); ASI != E; ++ASI)
189 OS << getQualifiedName(*ASI) << ", ";
193 OS << "\n const MRegisterDesc RegisterDescriptors[] = { // Descriptors\n";
194 OS << " { \"NOREG\",\t0,\t\t0,\t0 },\n";
195 // Now that register alias sets have been emitted, emit the register
197 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
198 Record *Reg = Registers[i];
200 if (!Reg->getValueAsString("Name").empty())
201 OS << Reg->getValueAsString("Name");
203 OS << Reg->getName();
205 if (RegisterAliases.count(Reg))
206 OS << Reg->getName() << "_AliasSet,\t";
208 OS << "Empty_AliasSet,\t";
211 OS << " };\n"; // End of register descriptors...
212 OS << "}\n\n"; // End of anonymous namespace...
214 CodeGenTarget Target;
216 OS << "namespace " << Target.getName() << " { // Register classes\n";
217 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
218 if (RegisterClasses[i]->getValueAsBit("isDummyClass"))
219 continue; // Ignore dummies
221 const std::string &Name = RegisterClasses[i]->getName();
222 if (Name.size() < 9 || Name[9] != '.') // Ignore anonymous classes
223 OS << " TargetRegisterClass *" << Name << "RegisterClass = &"
224 << Name << "Instance;\n";
226 OS << "} // end of namespace " << Target.getName() << "\n\n";
230 std::string ClassName = Target.getName() + "GenRegisterInfo";
232 // Emit the constructor of the class...
233 OS << ClassName << "::" << ClassName
234 << "(int CallFrameSetupOpcode, int CallFrameDestroyOpcode)\n"
235 << " : MRegisterInfo(RegisterDescriptors, " << Registers.size()+1
236 << ", RegisterClasses, RegisterClasses+" << RegClassNames.size() << ",\n "
237 << " CallFrameSetupOpcode, CallFrameDestroyOpcode) {}\n\n";
239 // Emit the getCalleeSaveRegs method...
240 OS << "const unsigned* " << ClassName << "::getCalleeSaveRegs() const {\n"
241 << " static const unsigned CalleeSaveRegs[] = {\n ";
243 const std::vector<Record*> &CSR = Target.getCalleeSavedRegisters();
244 for (unsigned i = 0, e = CSR.size(); i != e; ++i)
245 OS << getQualifiedName(CSR[i]) << ", ";
246 OS << " 0\n };\n return CalleeSaveRegs;\n}\n\n";
247 EmitSourceFileTail(OS);
250 } // End llvm namespace