mirror of
https://github.com/supleed2/svd2cpp.git
synced 2024-12-22 13:45:50 +00:00
Fixed builders, fixed passing arguments to ClassBuilder
This commit is contained in:
parent
04a24f9bbf
commit
0605e58c05
|
@ -1,39 +1,47 @@
|
|||
#include <Builders.hpp>
|
||||
#include <iostream>
|
||||
|
||||
void ZeroPointerBuilder::build(std::stringstream& ss){
|
||||
|
||||
void ZeroPointerBuilder::build(std::stringstream& ss) const{
|
||||
ss << "template <unsigned int zero = 0>\n"
|
||||
"constexpr unsigned int *zeroVal = reinterpret_cast<unsigned int *>(zero);\n";
|
||||
}
|
||||
|
||||
void PeripheralBuilder::build(std::stringstream& ss){
|
||||
ss << "namespace " << peripheral.name.c_str() << "{\n";
|
||||
void PeripheralBuilder::build(std::stringstream& ss) const{
|
||||
ss << "namespace " << peripheral.name << "{\n";
|
||||
for(auto& registe : peripheral.registers){
|
||||
RegisterBuilder(registe).build(ss);
|
||||
RegisterBuilder(registe, peripheral.baseAddress).build(ss);
|
||||
}
|
||||
ss << "}\n";
|
||||
}
|
||||
|
||||
void RegisterBuilder::build(std::stringstream& ss){
|
||||
//TODO: fix INVALID: some derived names are not copied
|
||||
void RegisterBuilder::build(std::stringstream& ss) const{
|
||||
ss << " "
|
||||
<< "namespace " << (registe.name.length() > 100 ? "INVALID" : registe.name) << "{\n";
|
||||
<< "namespace " << registe.name << "{\n";
|
||||
for(auto& field : registe.fields){
|
||||
FieldBuilder(field).build(ss);
|
||||
FieldBuilder(field, getRegisterAddress()).build(ss);
|
||||
}
|
||||
ss << " " << "}\n";
|
||||
}
|
||||
|
||||
void FieldBuilder::build(std::stringstream& ss){
|
||||
ss << " " << "struct " << (field.name.length() > 100 ? "INVALID" : field.name) << "{\n";
|
||||
unsigned int RegisterBuilder::getRegisterAddress() const{
|
||||
return baseAddress + registe.addressOffset;
|
||||
}
|
||||
|
||||
void FieldBuilder::build(std::stringstream& ss) const{
|
||||
ss << " " << "struct " << field.name << "{\n";
|
||||
|
||||
//TODO: fix INVALID: some derived items are not copied
|
||||
ss << " " << " " << "constexpr static inline unsigned int bitOffset(){"
|
||||
<< "return " << field.bitOffset << "}\n";
|
||||
<< "return " << field.bitOffset << ";}\n";
|
||||
|
||||
ss << " " << " " << "constexpr static inline unsigned int bitMask(){"
|
||||
<< "return " << field.bitWidth << "}\n";
|
||||
<< "return " << field.bitWidth << ";}\n";
|
||||
|
||||
ss << " " << " " << "constexpr static inline unsigned int address(){"
|
||||
<< "return " << "TODO" << "}\n";
|
||||
ss << " }\n";
|
||||
ss << " " << " " << "constexpr static inline unsigned int* address(){"
|
||||
<< "return zeroVal<> + " << getAddress() << ";}\n";
|
||||
ss << " };\n";
|
||||
}
|
||||
|
||||
std::string FieldBuilder::getAddress() const{
|
||||
return std::to_string(registerAddress);
|
||||
}
|
|
@ -7,30 +7,35 @@
|
|||
#include <sstream>
|
||||
|
||||
struct ZeroPointerBuilder : public IBuilder{
|
||||
void build(std::stringstream& ss) final;
|
||||
void build(std::stringstream& ss) const final;
|
||||
};
|
||||
|
||||
struct PeripheralBuilder : public IBuilder{
|
||||
PeripheralBuilder(const Peripheral &peripheral_) : peripheral(peripheral_) {}
|
||||
void build(std::stringstream& ss) final;
|
||||
void build(std::stringstream& ss) const final;
|
||||
|
||||
private:
|
||||
const Peripheral& peripheral;
|
||||
};
|
||||
|
||||
struct RegisterBuilder : public IBuilder{
|
||||
RegisterBuilder(const Register& register_) : registe(register_){}
|
||||
void build(std::stringstream& ss) final;
|
||||
|
||||
RegisterBuilder(const Register& register_, const unsigned int baseAddress_)
|
||||
: registe(register_), baseAddress(baseAddress_) {}
|
||||
void build(std::stringstream& ss) const final;
|
||||
unsigned int getRegisterAddress() const;
|
||||
private:
|
||||
const Register& registe;
|
||||
const unsigned int baseAddress;
|
||||
};
|
||||
|
||||
struct FieldBuilder : public IBuilder{
|
||||
FieldBuilder(const Field& field_) : field(field_){}
|
||||
void build(std::stringstream& ss) final;
|
||||
FieldBuilder(const Field& field_, const unsigned int registerAddress_)
|
||||
: field(field_), registerAddress(registerAddress_) {}
|
||||
void build(std::stringstream& ss) const final;
|
||||
std::string getAddress() const;
|
||||
|
||||
private:
|
||||
const Field& field;
|
||||
const unsigned int registerAddress;
|
||||
};
|
||||
#endif
|
||||
|
|
|
@ -2,13 +2,12 @@
|
|||
#include <Builders.hpp>
|
||||
|
||||
|
||||
ClassBuilder::ClassBuilder(const cxxopts::ParseResult& results,
|
||||
const DeviceInfo& deviceInfo,
|
||||
const std::vector<Peripheral>& peripherals) :
|
||||
results(results),
|
||||
deviceInfo(deviceInfo),
|
||||
peripherals(peripherals){
|
||||
}
|
||||
ClassBuilder::ClassBuilder(const cxxopts::ParseResult& results_,
|
||||
const DeviceInfo& deviceInfo_,
|
||||
const std::vector<Peripheral>& peripherals_) :
|
||||
results(results_),
|
||||
deviceInfo(deviceInfo_),
|
||||
peripherals(peripherals_){}
|
||||
|
||||
void ClassBuilder::setupBuilders(){
|
||||
builders.push_back(std::make_unique<ZeroPointerBuilder>());
|
||||
|
|
|
@ -12,9 +12,9 @@
|
|||
|
||||
struct ClassBuilder
|
||||
{
|
||||
ClassBuilder(const cxxopts::ParseResult& results,
|
||||
const DeviceInfo& deviceInfo,
|
||||
const std::vector<Peripheral>& peripherals);
|
||||
ClassBuilder(const cxxopts::ParseResult& results_,
|
||||
const DeviceInfo& deviceInfo_,
|
||||
const std::vector<Peripheral>& peripherals_);
|
||||
void setupBuilders();
|
||||
void build();
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
|
||||
struct IBuilder{
|
||||
virtual void build(std::stringstream&) = 0;
|
||||
virtual void build(std::stringstream&) const = 0;
|
||||
virtual ~IBuilder() = default;
|
||||
};
|
||||
|
||||
|
|
|
@ -11,17 +11,13 @@ enum class EAccess{
|
|||
Read_Write
|
||||
};
|
||||
|
||||
struct IDisplay{
|
||||
virtual void display() = 0;
|
||||
};
|
||||
|
||||
struct Field : public IDisplay{
|
||||
struct Field{
|
||||
std::string name;
|
||||
std::string description;
|
||||
unsigned int bitOffset;
|
||||
unsigned int bitWidth;
|
||||
EAccess fieldAccess;
|
||||
void display() final{
|
||||
void display() const{
|
||||
std::cout << "\t\tname: " << name << std::endl
|
||||
<< "\t\tdescription: " << description << std::endl
|
||||
<< "\t\tbitOffset: " << bitOffset << std::endl
|
||||
|
@ -31,7 +27,7 @@ struct Field : public IDisplay{
|
|||
}
|
||||
};
|
||||
|
||||
struct Register : public IDisplay{
|
||||
struct Register{
|
||||
std::string name;
|
||||
std::string description;
|
||||
unsigned int addressOffset;
|
||||
|
@ -40,7 +36,7 @@ struct Register : public IDisplay{
|
|||
unsigned int resetValue;
|
||||
std::vector<Field> fields;
|
||||
|
||||
void display() final{
|
||||
void display() const{
|
||||
std::cout << "\tname: " << name << std::endl
|
||||
<< "\tdescription: " << description << std::endl
|
||||
<< "\taddressOffset: " << addressOffset << std::endl
|
||||
|
@ -55,24 +51,24 @@ struct Register : public IDisplay{
|
|||
}
|
||||
};
|
||||
|
||||
struct AddressBlock : public IDisplay{
|
||||
struct AddressBlock{
|
||||
unsigned int offset;
|
||||
unsigned int size;
|
||||
void display() final{
|
||||
void display() const{
|
||||
std::cout << std::endl
|
||||
<< "\toffset: " << offset << std::endl
|
||||
<< "\tsize: " << size << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
struct Peripheral : public IDisplay{
|
||||
struct Peripheral{
|
||||
std::string name;
|
||||
std::string description;
|
||||
std::string groupName;
|
||||
unsigned int baseAddress;
|
||||
AddressBlock addressBlock;
|
||||
std::vector<Register> registers;
|
||||
void display() final{
|
||||
void display() const{
|
||||
std::cout << std::endl
|
||||
<< "name: " << name << std::endl
|
||||
<< "description: " << description << std::endl
|
||||
|
|
|
@ -41,7 +41,7 @@ void XmlParser::parseXml(){
|
|||
|
||||
void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, std::string &field) const{
|
||||
tinyxml2::XMLElement* deviceEntry = deviceRoot->FirstChildElement(name);
|
||||
field = deviceEntry != nullptr ? deviceEntry->GetText() : noValue;
|
||||
field = deviceEntry != nullptr ? std::string(deviceEntry->GetText()) : noValue;
|
||||
}
|
||||
|
||||
void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, unsigned int &field) const{
|
||||
|
@ -79,8 +79,8 @@ Peripheral XmlParser::parsePeripheral(tinyxml2::XMLElement* peripheralRoot) cons
|
|||
//Find the base peripheral and copy it to the new one
|
||||
auto crit = [&](auto &periph) { return periph.name == derivedFrom; };
|
||||
auto resultIt = std::find_if(peripherals.begin(), peripherals.end(), crit);
|
||||
if(resultIt < peripherals.end()){
|
||||
peripheral = *resultIt;
|
||||
if(resultIt <= peripherals.end()){
|
||||
peripheral = Peripheral(*resultIt);
|
||||
}
|
||||
else{
|
||||
std::cout << "Couldn't find peripheral " << derivedFrom << std::endl;
|
||||
|
|
|
@ -13,10 +13,10 @@ struct XmlParser{
|
|||
XmlParser(const std::string& inputFile);
|
||||
std::optional<std::string> isError() const;
|
||||
void parseXml();
|
||||
inline const DeviceInfo getDeviceInfo() const{
|
||||
inline const DeviceInfo& getDeviceInfo() const{
|
||||
return deviceInfo;
|
||||
}
|
||||
inline const std::vector<Peripheral> getPeripherals() const{
|
||||
inline const std::vector<Peripheral>& getPeripherals() const{
|
||||
return peripherals;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ int main(int argc, char** argv){
|
|||
}
|
||||
inputFile = result["input"].as<std::string>();
|
||||
outputFile = result["output"].as<std::string>();
|
||||
std::cout << "Input: " << inputFile << "\tOutput: " << outputFile << std::endl;
|
||||
// std::cout << "Input: " << inputFile << "\tOutput: " << outputFile << std::endl;
|
||||
}
|
||||
catch(cxxopts::OptionException& ex){
|
||||
std::cout << ex.what() << std::endl;
|
||||
|
|
Loading…
Reference in a new issue