1 //===- RegisterInfoEmitter.cpp - Generate a Register File Desc. -*- C++ -*-===//
3 // This tablegen backend is responsible for emitting a description of a target
4 // register file for a code generator. It uses instances of the Register,
5 // RegisterAliases, and RegisterClass classes to gather this information.
7 //===----------------------------------------------------------------------===//
9 #include "RegisterInfoEmitter.h"
11 #include "Support/StringExtras.h"
14 static void EmitSourceHeader(const std::string &Desc, std::ostream &o) {
15 o << "//===- TableGen'erated file -------------------------------------*-"
16 " C++ -*-===//\n//\n// " << Desc << "\n//\n// Automatically generate"
17 "d file, do not edit!\n//\n//===------------------------------------"
18 "----------------------------------===//\n\n";
21 // runEnums - Print out enum values for all of the registers.
22 void RegisterInfoEmitter::runEnums(std::ostream &OS) {
23 std::vector<Record*> Registers = Records.getAllDerivedDefinitions("Register");
25 if (Registers.size() == 0)
26 throw std::string("No 'Register' subclasses defined!");
28 std::string Namespace = Registers[0]->getValueAsString("Namespace");
30 EmitSourceHeader("Target Register Enum Values", OS);
32 if (!Namespace.empty())
33 OS << "namespace " << Namespace << " {\n";
34 OS << " enum {\n NoRegister,\n";
36 for (unsigned i = 0, e = Registers.size(); i != e; ++i)
37 OS << " " << Registers[i]->getName() << ", \t// " << i+1 << "\n";
40 if (!Namespace.empty())
44 static Record *getTarget(RecordKeeper &RC) {
45 std::vector<Record*> Targets = RC.getAllDerivedDefinitions("Target");
47 if (Targets.size() != 1)
48 throw std::string("ERROR: Multiple subclasses of Target defined!");
52 static std::string getQualifiedName(Record *R) {
53 std::string Namespace = R->getValueAsString("Namespace");
54 if (Namespace.empty()) return R->getName();
55 return Namespace + "::" + R->getName();
58 void RegisterInfoEmitter::runHeader(std::ostream &OS) {
59 EmitSourceHeader("Register Information Header Fragment", OS);
61 std::string ClassName = getTarget(Records)->getName() + "GenRegisterInfo";
63 OS << "#include \"llvm/Target/MRegisterInfo.h\"\n\n";
65 OS << "struct " << ClassName << " : public MRegisterInfo {\n"
67 << "(int CallFrameSetupOpcode = -1, int CallFrameDestroyOpcode = -1);\n"
68 << " const unsigned* getCalleeSaveRegs() const;\n"
72 // RegisterInfoEmitter::run - Main register file description emitter.
74 void RegisterInfoEmitter::run(std::ostream &OS) {
75 EmitSourceHeader("Register Information Source Fragment", OS);
77 // Start out by emitting each of the register classes... to do this, we build
78 // a set of registers which belong to a register class, this is to ensure that
79 // each register is only in a single register class.
81 std::vector<Record*> RegisterClasses =
82 Records.getAllDerivedDefinitions("RegisterClass");
84 std::vector<Record*> Registers = Records.getAllDerivedDefinitions("Register");
85 Record *RegisterClass = Records.getClass("Register");
87 std::set<Record*> RegistersFound;
88 std::vector<std::string> RegClassNames;
90 // Loop over all of the register classes... emitting each one.
91 OS << "namespace { // Register classes...\n";
93 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
94 Record *RC = RegisterClasses[rc];
95 std::string Name = RC->getName();
96 if (Name.size() > 9 && Name[9] == '.') {
97 static unsigned AnonCounter = 0;
98 Name = "AnonRegClass_"+utostr(AnonCounter++);
101 RegClassNames.push_back(Name);
103 // Emit the register list now...
104 OS << " // " << Name << " Register Class...\n const unsigned " << Name
106 ListInit *RegList = RC->getValueAsListInit("MemberList");
107 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
108 DefInit *RegDef = dynamic_cast<DefInit*>(RegList->getElement(i));
109 if (!RegDef) throw "Register class member is not a record!";
110 Record *Reg = RegDef->getDef();
111 if (!Reg->isSubClassOf(RegisterClass))
112 throw "Register Class member '" + Reg->getName() +
113 " does not derive from the Register class!";
114 if (RegistersFound.count(Reg))
115 throw "Register '" + Reg->getName() +
116 "' included in multiple register classes!";
117 RegistersFound.insert(Reg);
118 OS << getQualifiedName(Reg) << ", ";
122 OS << " struct " << Name << "Class : public TargetRegisterClass {\n"
123 << " " << Name << "Class() : TargetRegisterClass("
124 << RC->getValueAsInt("Size")/8 << ", " << RC->getValueAsInt("Alignment")
125 << ", " << Name << ", " << Name << " + " << RegList->getSize()
128 if (CodeInit *CI = dynamic_cast<CodeInit*>(RC->getValueInit("Methods")))
129 OS << CI->getValue();
131 throw "Expected 'code' fragment for 'Methods' value in register class '"+
132 RC->getName() + "'!";
134 OS << " } " << Name << "Instance;\n\n";
137 OS << " const TargetRegisterClass* const RegisterClasses[] = {\n";
138 for (unsigned i = 0, e = RegClassNames.size(); i != e; ++i)
139 OS << " &" << RegClassNames[i] << "Instance,\n";
142 // Emit register class aliases...
143 std::vector<Record*> RegisterAliasesRecs =
144 Records.getAllDerivedDefinitions("RegisterAliases");
145 std::map<Record*, std::set<Record*> > RegisterAliases;
147 for (unsigned i = 0, e = RegisterAliasesRecs.size(); i != e; ++i) {
148 Record *AS = RegisterAliasesRecs[i];
149 Record *R = AS->getValueAsDef("Reg");
150 ListInit *LI = AS->getValueAsListInit("Aliases");
152 // Add information that R aliases all of the elements in the list... and
153 // that everything in the list aliases R.
154 for (unsigned j = 0, e = LI->getSize(); j != e; ++j) {
155 DefInit *Reg = dynamic_cast<DefInit*>(LI->getElement(j));
156 if (!Reg) throw "ERROR: Alias list element is not a def!";
157 if (RegisterAliases[R].count(Reg->getDef()))
158 std::cerr << "Warning: register alias between " << getQualifiedName(R)
159 << " and " << getQualifiedName(Reg->getDef())
160 << " specified multiple times!\n";
161 RegisterAliases[R].insert(Reg->getDef());
163 if (RegisterAliases[Reg->getDef()].count(R))
164 std::cerr << "Warning: register alias between " << getQualifiedName(R)
165 << " and " << getQualifiedName(Reg->getDef())
166 << " specified multiple times!\n";
167 RegisterAliases[Reg->getDef()].insert(R);
171 if (!RegisterAliases.empty())
172 OS << "\n\n // Register Alias Sets...\n";
174 // Loop over all of the registers which have aliases, emitting the alias list
176 for (std::map<Record*, std::set<Record*> >::iterator
177 I = RegisterAliases.begin(), E = RegisterAliases.end(); I != E; ++I) {
178 OS << " const unsigned " << I->first->getName() << "_AliasSet[] = { ";
179 for (std::set<Record*>::iterator ASI = I->second.begin(),
180 E = I->second.end(); ASI != E; ++ASI)
181 OS << getQualifiedName(*ASI) << ", ";
185 OS << "\n const MRegisterDesc RegisterDescriptors[] = { // Descriptors\n";
186 OS << " { \"NOREG\",\t0,\t\t0,\t0 },\n";
187 // Now that register alias sets have been emitted, emit the register
189 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
190 Record *Reg = Registers[i];
192 if (!Reg->getValueAsString("Name").empty())
193 OS << Reg->getValueAsString("Name");
195 OS << Reg->getName();
197 if (RegisterAliases.count(Reg))
198 OS << Reg->getName() << "_AliasSet,\t";
203 OS << " };\n"; // End of register descriptors...
204 OS << "}\n\n"; // End of anonymous namespace...
206 Record *Target = getTarget(Records);
207 std::string ClassName = Target->getName() + "GenRegisterInfo";
209 // Emit the constructor of the class...
210 OS << ClassName << "::" << ClassName
211 << "(int CallFrameSetupOpcode, int CallFrameDestroyOpcode)\n"
212 << " : MRegisterInfo(RegisterDescriptors, " << Registers.size()+1
213 << ", RegisterClasses, RegisterClasses+" << RegClassNames.size() << ",\n "
214 << " CallFrameSetupOpcode, CallFrameDestroyOpcode) {}\n\n";
216 // Emit the getCalleeSaveRegs method...
217 OS << "const unsigned* " << ClassName << "::getCalleeSaveRegs() const {\n"
218 << " static const unsigned CalleeSaveRegs[] = {\n ";
220 ListInit *LI = Target->getValueAsListInit("CalleeSavedRegisters");
221 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
222 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(i)))
223 OS << getQualifiedName(DI->getDef()) << ", ";
225 throw "Expected register definition in CalleeSavedRegisters list!";
226 OS << " 0\n };\n return CalleeSaveRegs;\n}\n\n";