16 for (
const auto& filename : register_defns_json_filenames) {
26 parse_(register_defns_json_filename);
33 return register_defns_.data();
37 void parse_(
const std::string& register_defns_json_filename)
40 std::ifstream ifs(register_defns_json_filename);
43 std::string json_str = ss.str();
46 rapidjson::Document document;
47 document.Parse(json_str.c_str());
49 for (
auto& item : document.GetArray()) {
50 if (item.HasMember(
"enabled") && !item[
"enabled"].GetBool()) {
56 cached_strings_.emplace_back(item[
"name"].GetString());
57 const char *name = cached_strings_.back().raw();
60 auto iter = group_idx_map_.find(group_num);
61 if (iter == group_idx_map_.end()) {
62 group_idx_map_[group_num] = 0;
66 cached_strings_.emplace_back(item[
"group_name"].GetString());
67 const char* group = cached_strings_.back().raw();
69 if (std::string(group).empty()) {
73 cached_strings_.emplace_back(item[
"desc"].GetString());
74 const char* desc = cached_strings_.back().raw();
78 std::vector<RegisterBase::Field::Definition> field_defns;
79 if (item.HasMember(
"fields")) {
80 for (
auto it = item[
"fields"].MemberBegin(); it != item[
"fields"].MemberEnd(); ++it) {
81 const char* field_name = it->name.GetString();
82 const rapidjson::Value& field_info = it->value;
83 cached_field_defns_.emplace_back(field_name, field_info);
84 field_defns.push_back(cached_field_defns_.back().getDefn());
88 static const std::vector<RegisterBase::bank_idx_type> bank_membership;
90 std::vector<std::string> alias_strings;
91 for (
auto& alias : item[
"aliases"].GetArray()) {
92 alias_strings.push_back(alias.GetString());
94 cached_aliases_.emplace_back(alias_strings);
95 const char** aliases = cached_aliases_.back().raw();
100 const unsigned char *initial_value =
nullptr;
101 if (item.HasMember(
"initial_value")) {
102 cached_initial_values_.emplace_back(item[
"initial_value"].GetString());
103 initial_value = cached_initial_values_.back().raw();
127 register_defns_.push_back(defn);
135 StringRef(
const std::string& str) : storage_(str) {}
136 const char* raw()
const {
return storage_.c_str(); }
138 std::string storage_;
145 AliasRef(
const std::vector<std::string>& aliases)
148 for (
const auto& str : storage_) {
149 pointers_.push_back(str.c_str());
154 return pointers_.data();
158 std::vector<std::string> storage_;
159 std::vector<const char*> pointers_;
163 class InitialValueRef
166 InitialValueRef(
const std::string& hex_str)
169 std::string hex = hex_str;
170 if (hex.substr(0, 2) ==
"0x") {
175 sparta_assert(hex.length() % 2 == 0,
"Hex string must have an even length");
178 hex_bytes_.resize(hex.length() / 2);
181 for (
size_t i = 0; i < hex.length(); i += 2) {
182 const auto byte_string = hex.substr(i, 2);
183 std::istringstream iss(byte_string);
185 iss >> std::hex >> byte;
186 hex_bytes_[i / 2] =
static_cast<char>(byte);
190 const unsigned char* raw()
const {
191 return hex_bytes_.data();
195 std::vector<unsigned char> hex_bytes_;
199 class FieldDefnConverter
202 FieldDefnConverter(
const std::string& field_name,
const rapidjson::Value& field_info)
203 : field_name_(field_name)
204 , desc_(field_info[
"desc"].GetString())
205 , field_defn_(field_name_.c_str(),
207 field_info[
"low_bit"].GetInt(),
208 field_info[
"high_bit"].GetInt(),
209 field_info[
"readonly"].GetBool())
219 std::string field_name_;
224 std::deque<StringRef> cached_strings_;
225 std::deque<AliasRef> cached_aliases_;
226 std::deque<InitialValueRef> cached_initial_values_;
227 std::deque<FieldDefnConverter> cached_field_defns_;
228 std::vector<RegisterBase::Definition> register_defns_;
232 std::map<RegisterBase::group_num_type, RegisterBase::group_idx_type> group_idx_map_;