16#ifndef SIMICS_MAPPABLE_CONF_OBJECT_H
17#define SIMICS_MAPPABLE_CONF_OBJECT_H
19#include <simics/base/log.h>
23#include <unordered_map>
24#include <unordered_set>
38template <
typename IFACE>
41 static size_t hash_str(
const std::string &name) {
42 return std::hash<std::string>{}(name);
46 void set_iface(
const std::string &name, IFACE *iface)
override {
48 throw std::invalid_argument {
49 "Cannot set with NULL interface"
51 }
else if (name.empty()) {
52 throw std::invalid_argument {
53 "Cannot set with empty name string"
56 name_to_iface_[
hash_str(name)] = iface;
59 IFACE *
get_iface(
const std::string &name)
const override {
61 if (name_to_iface_.find(hashed) == name_to_iface_.end()) {
64 return name_to_iface_.at(hashed);
67 name_to_iface_.erase(
hash_str(name));
71 std::unordered_map<size_t, IFACE *> name_to_iface_;
84 static size_t hash_str(
const std::string &name) {
85 return std::hash<std::string>{}(name);
91 throw std::invalid_argument {
92 "Cannot set with NULL interface"
94 }
else if (name.empty()) {
95 throw std::invalid_argument {
96 "Cannot set with empty name string"
99 name_to_iface_[
hash_str(name)] = iface;
100 all_registers_.insert(name);
107 name_to_iface_.erase(
hash_str(name));
108 all_registers_.erase(name);
112 if (name_to_iface_.find(name_hash) == name_to_iface_.end()) {
115 return name_to_iface_.at(name_hash);
121 std::unordered_map<size_t, RegisterInterface *> name_to_iface_;
137 template <
typename IFACE>
140 SIM_LOG_ERROR(
obj(), 0,
141 "Cannot set interface for %s when ConfObject"
142 " has been finalized", name.c_str());
146 auto current_iface = get_iface<IFACE>(name);
147 if (current_iface && current_iface != iface) {
148 if (is_bank_initialized(name)) {
149 SIM_LOG_INFO(4,
obj(), 0,
150 "Interface for %s ignored since bank"
151 " has been initialized", name.c_str());
158 std::get<MapNameToInterfaceObject<IFACE>>(
160 }
catch (std::exception &e) {
161 SIM_LOG_ERROR(
obj(), 0,
"%s", e.what());
169 template <
typename IFACE>
171 return std::get<MapNameToInterfaceObject<IFACE>>(
176 return std::get<MapNameToInterfaceObject<RegisterInterface>>(
181 template <
typename IFACE>
183 std::get<MapNameToInterfaceObject<IFACE>>(objects_).
erase_iface(name);
199 auto reg_name = name.substr(name.rfind(
'.') + 1);
200 auto num_dims = std::count(reg_name.cbegin(), reg_name.cend(),
'[');
204 std::vector<size_t> dims(num_dims);
207 auto base_name = name.substr(0, name.rfind(
'.') + 1) + \
208 reg_name.substr(0, reg_name.find(
'['));
209 for (
int dim_index = 0; dim_index < num_dims; ++dim_index) {
210 auto prefix = base_name;
211 for (
int prefix_index = 0; prefix_index < dim_index;
213 prefix.append(
"[0]");
216 for (
int postfix_index = dim_index + 1; postfix_index < num_dims;
218 postfix.append(
"[0]");
223 std::string check_name = prefix +
'[' + std::to_string(s) + \
225 if (all_regs.find(check_name) != all_regs.end()) {
237 initialized_banks_.insert(name);
242 return &allocated_bank_memories_[name_of_memory.data()];
246 bool is_bank_initialized(
const std::string &name) {
247 const auto &bank_name = name.substr(0, name.find(
'.'));
248 return initialized_banks_.find(bank_name) != initialized_banks_.end();
252 std::tuple<MapNameToInterfaceObject<BankInterface>,
253 MapNameToInterfaceObject<RegisterInterface>,
254 MapNameToInterfaceObject<FieldInterface>> objects_;
257 std::unordered_set<std::string> initialized_banks_;
272 std::unordered_map<std::string,
Represents Simics C type conf_object_t.
Definition: conf-object.h:37
A class inherited by a model class to support Simics configuration.
Definition: conf-object.h:114
virtual bool finalized()
Return if the finalize method has been called.
Definition: conf-object.h:137
ConfObjectRef obj() const
Return a ConfObjectRef represents this object.
Definition: conf-object.h:134
void erase_iface(const std::string &name) override
Definition: mappable-conf-object.h:106
static size_t hash_str(const std::string &name)
Definition: mappable-conf-object.h:84
void set_iface(const std::string &name, RegisterInterface *iface) override
Definition: mappable-conf-object.h:89
RegisterInterface * get_iface(size_t name_hash) const
Definition: mappable-conf-object.h:111
RegisterInterface * get_iface(const std::string &name) const override
Definition: mappable-conf-object.h:103
std::unordered_set< std::string > all_registers_
Definition: mappable-conf-object.h:118
Definition: mappable-conf-object.h:39
void set_iface(const std::string &name, IFACE *iface) override
Definition: mappable-conf-object.h:46
IFACE * get_iface(const std::string &name) const override
Definition: mappable-conf-object.h:59
void erase_iface(const std::string &name) override
Definition: mappable-conf-object.h:66
static size_t hash_str(const std::string &name)
Definition: mappable-conf-object.h:41
Definition: map-name-to-interface.h:24
Definition: mappable-conf-object.h:131
const std::unordered_set< std::string > & all_registers() const
Definition: mappable-conf-object.h:193
std::vector< size_t > register_dimensions(const std::string &name)
Definition: mappable-conf-object.h:198
void erase_iface(const std::string &name)
Definition: mappable-conf-object.h:182
RegisterInterface * get_iface(size_t name_hash) const
Definition: mappable-conf-object.h:175
bank_memory_t * get_bank_memory(std::string_view name_of_memory)
Definition: mappable-conf-object.h:241
virtual bool big_endian_bitorder()
Definition: mappable-conf-object.h:188
virtual ~MappableConfObject()
Definition: mappable-conf-object.h:134
void set_iface(const std::string &name, IFACE *iface)
Definition: mappable-conf-object.h:138
IFACE * get_iface(const std::string &name) const
Definition: mappable-conf-object.h:170
MappableConfObject(ConfObjectRef obj)
Definition: mappable-conf-object.h:133
void set_bank_as_initialized(const std::string &name)
Definition: mappable-conf-object.h:236
Definition: register-interface.h:36
Definition: attr-value.h:23
std::unordered_map< Offset, uint8_t > bank_memory_t
Definition: bank-type.h:48