diff --git a/utils/TableGen/CodeGenRegisters.h b/utils/TableGen/CodeGenRegisters.h index ba89190325d..bbec4ea6706 100644 --- a/utils/TableGen/CodeGenRegisters.h +++ b/utils/TableGen/CodeGenRegisters.h @@ -16,6 +16,7 @@ #define CODEGEN_REGISTERS_H #include +#include namespace llvm { class Record; @@ -31,7 +32,14 @@ namespace llvm { struct CodeGenRegisterClass { + Record *TheDef; + std::vector Elements; + unsigned SpillSize; + unsigned SpillAlignment; + const std::string &getName() const; + + CodeGenRegisterClass(Record *R); }; } diff --git a/utils/TableGen/CodeGenTarget.cpp b/utils/TableGen/CodeGenTarget.cpp index c495519e58d..e0d585c6187 100644 --- a/utils/TableGen/CodeGenTarget.cpp +++ b/utils/TableGen/CodeGenTarget.cpp @@ -121,6 +121,38 @@ const std::string &CodeGenRegister::getName() const { return TheDef->getName(); } +void CodeGenTarget::ReadRegisterClasses() const { + std::vector RegClasses = + Records.getAllDerivedDefinitions("RegisterClass"); + if (RegClasses.empty()) + throw std::string("No 'RegisterClass' subclasses defined!"); + + RegisterClasses.reserve(RegClasses.size()); + RegisterClasses.assign(RegClasses.begin(), RegClasses.end()); +} + +CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) { + SpillSize = R->getValueAsInt("Size"); + SpillAlignment = R->getValueAsInt("Alignment"); + + ListInit *RegList = R->getValueAsListInit("MemberList"); + for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) { + DefInit *RegDef = dynamic_cast(RegList->getElement(i)); + if (!RegDef) throw "Register class member is not a record!"; + Record *Reg = RegDef->getDef(); + + if (!Reg->isSubClassOf("Register")) + throw "Register Class member '" + Reg->getName() + + "' does not derive from the Register class!"; + Elements.push_back(Reg); + } +} + +const std::string &CodeGenRegisterClass::getName() const { + return TheDef->getName(); +} + + void CodeGenTarget::ReadInstructions() const { std::vector Insts = Records.getAllDerivedDefinitions("Instruction"); diff --git a/utils/TableGen/CodeGenTarget.h b/utils/TableGen/CodeGenTarget.h index 2d65b7be8ea..9b338d88c9f 100644 --- a/utils/TableGen/CodeGenTarget.h +++ b/utils/TableGen/CodeGenTarget.h @@ -46,8 +46,10 @@ class CodeGenTarget { mutable std::map Instructions; mutable std::vector Registers; - void ReadInstructions() const; + mutable std::vector RegisterClasses; void ReadRegisters() const; + void ReadRegisterClasses() const; + void ReadInstructions() const; public: CodeGenTarget(); @@ -73,6 +75,12 @@ public: return Registers; } + const std::vector getRegisterClasses() { + if (RegisterClasses.empty()) ReadRegisterClasses(); + return RegisterClasses; + } + + /// getInstructions - Return all of the instructions defined for this target. /// const std::map &getInstructions() const { diff --git a/utils/TableGen/RegisterInfoEmitter.cpp b/utils/TableGen/RegisterInfoEmitter.cpp index 4ed1a0fe8c5..6763af3f65b 100644 --- a/utils/TableGen/RegisterInfoEmitter.cpp +++ b/utils/TableGen/RegisterInfoEmitter.cpp @@ -85,8 +85,8 @@ void RegisterInfoEmitter::run(std::ostream &OS) { // a set of registers which belong to a register class, this is to ensure that // each register is only in a single register class. // - std::vector RegisterClasses = - Records.getAllDerivedDefinitions("RegisterClass"); + const std::vector &RegisterClasses = + Target.getRegisterClasses(); std::set RegistersFound; std::vector RegClassNames; @@ -95,9 +95,9 @@ void RegisterInfoEmitter::run(std::ostream &OS) { OS << "namespace { // Register classes...\n"; for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) { - Record *RC = RegisterClasses[rc]; + const CodeGenRegisterClass &RC = RegisterClasses[rc]; - std::string Name = RC->getName(); + std::string Name = RC.getName(); if (Name.size() > 9 && Name[9] == '.') { static unsigned AnonCounter = 0; Name = "AnonRegClass_"+utostr(AnonCounter++); @@ -108,14 +108,8 @@ void RegisterInfoEmitter::run(std::ostream &OS) { // Emit the register list now... OS << " // " << Name << " Register Class...\n const unsigned " << Name << "[] = {\n "; - ListInit *RegList = RC->getValueAsListInit("MemberList"); - for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) { - DefInit *RegDef = dynamic_cast(RegList->getElement(i)); - if (!RegDef) throw "Register class member is not a record!"; - Record *Reg = RegDef->getDef(); - if (!Reg->isSubClassOf("Register")) - throw "Register Class member '" + Reg->getName() + - " does not derive from the Register class!"; + for (unsigned i = 0, e = RC.Elements.size(); i != e; ++i) { + Record *Reg = RC.Elements[i]; if (RegistersFound.count(Reg)) throw "Register '" + Reg->getName() + "' included in multiple register classes!"; @@ -126,15 +120,15 @@ void RegisterInfoEmitter::run(std::ostream &OS) { OS << " struct " << Name << "Class : public TargetRegisterClass {\n" << " " << Name << "Class() : TargetRegisterClass(" - << RC->getValueAsInt("Size")/8 << ", " << RC->getValueAsInt("Alignment") - << ", " << Name << ", " << Name << " + " << RegList->getSize() - << ") {}\n"; + << RC.SpillSize/8 << ", " << RC.SpillAlignment << ", " << Name << ", " + << Name << " + " << RC.Elements.size() << ") {}\n"; - if (CodeInit *CI = dynamic_cast(RC->getValueInit("Methods"))) + if (CodeInit *CI = + dynamic_cast(RC.TheDef->getValueInit("Methods"))) OS << CI->getValue(); else throw "Expected 'code' fragment for 'Methods' value in register class '"+ - RC->getName() + "'!"; + RC.getName() + "'!"; OS << " } " << Name << "Instance;\n\n"; } @@ -215,7 +209,7 @@ void RegisterInfoEmitter::run(std::ostream &OS) { OS << "namespace " << Target.getName() << " { // Register classes\n"; for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) { - const std::string &Name = RegisterClasses[i]->getName(); + const std::string &Name = RegisterClasses[i].getName(); if (Name.size() < 9 || Name[9] != '.') // Ignore anonymous classes OS << " TargetRegisterClass *" << Name << "RegisterClass = &" << Name << "Instance;\n";