16#ifndef SIMICS_TYPE_HIERARCHICAL_OBJECT_NAME_H
17#define SIMICS_TYPE_HIERARCHICAL_OBJECT_NAME_H
47 :
std::string_view() {}
51 :
std::string_view(s, count) {
56 :
std::string_view(s) {
80 throw std::invalid_argument(
"Empty name is not allowed");
82 if (!isalpha(name.front())) {
83 throw std::invalid_argument(
84 std::string(
"Name (") + name.data() \
85 +
") does not begin with an alphabetic character");
89 for (
const auto &c : name.substr(0, name.find(
'['))) {
90 if (c !=
'_' && !isalnum(c)) {
91 throw std::invalid_argument(
92 std::string(
"Character (") + c \
93 +
") is not allowed to use in a name");
100 return substr(0, find(
'['));
105 auto pos = find(
'[');
128 throw std::invalid_argument(
"Invalid width 0");
137 for (
auto it = sizes_and_strides.rbegin();
138 it != sizes_and_strides.rend(); ++it) {
139 if (it->second != 0) {
142 if (it == sizes_and_strides.rbegin()) {
147 it->second = (it - 1)->first * (it - 1)->second;
151 std::map<std::string, size_t> names_to_offsets;
152 std::vector<size_t> indices(sizes_and_strides.size(), 0);
153 generateNamesToOffsets(sizes_and_strides, 0, &indices,
155 return names_to_offsets;
178 std::size_t content_pos = npos;
179 std::vector<std::pair<size_t, size_t>> dims_;
180 for (
size_t i = 0; i < s.size(); ++i) {
183 if (content_pos != npos) {
184 throw std::logic_error(
"Name has unbalanced brackets");
187 }
else if (c ==
']') {
188 if (content_pos == npos) {
189 throw std::logic_error(
"Name has unbalanced brackets");
191 if (content_pos == i) {
192 throw std::logic_error(
"Name has nothing in brackets");
194 const auto &[size, stride] = sizeAndStride(
195 s.substr(content_pos, i - content_pos));
197 throw std::logic_error(
"Dimension size is 0");
199 dims_.push_back({size, stride});
204 if (content_pos != npos) {
205 throw std::logic_error(
"Name has unbalanced brackets");
215 static constexpr bool isalpha(value_type c) {
216 return (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z');
219 static constexpr bool isalnum(value_type c) {
220 return isalpha(c) || (c >=
'0' && c <=
'9');
237 std::pair<size_t, size_t> sizeAndStride(std::string_view s)
const {
238 auto stride_pos = s.find(
" stride ");
243 if (stride_pos != npos) {
244 size = std::stoi(s.substr(0, stride_pos).data());
245 stride = std::stoi(s.substr(stride_pos + 8).data());
247 if (s.find_first_not_of(
"0123456789") != npos) {
248 throw std::invalid_argument(
"non-digit character");
250 size = std::stoi(s.data());
252 }
catch (
const std::exception &e) {
253 throw std::invalid_argument(
254 std::string(
"Array contents are malformed: ") + e.what());
257 return {size, stride};
284 void generateNamesToOffsets(
const std::vector<std::pair<size_t, size_t>>
287 std::vector<size_t> *indices,
288 std::map<std::string, size_t>
289 *names_to_offsets)
const {
290 if (current_dim == dims_info.size()) {
295 for (
size_t i = 0; i < indices->size(); i++) {
296 name +=
"[" + std::to_string((*indices)[i]) +
"]";
297 offset += (*indices)[i] * dims_info[i].second;
299 (*names_to_offsets)[name] = offset;
302 for (
size_t i = 0; i < dims_info[current_dim].first; i++) {
303 (*indices)[current_dim] = i;
304 generateNamesToOffsets(dims_info, current_dim + 1,
305 indices, names_to_offsets);
Represents name of a bank/register/field.
Definition: hierarchical-object-name.h:44
std::map< std::string, size_t > arrayNamesToOffsets(size_t width) const
Generates a mapping of array names to their corresponding memory offsets.
Definition: hierarchical-object-name.h:126
std::vector< std::pair< size_t, size_t > > arraySizesAndStrides() const
Parses a string representation of array dimensions to extract sizes and strides.
Definition: hierarchical-object-name.h:172
constexpr std::string_view array_str() const
Definition: hierarchical-object-name.h:104
static constexpr void validate_name(std::string_view name)
Validates the format of a name, ensuring it adheres to specific rules.
Definition: hierarchical-object-name.h:74
constexpr HierarchicalObjectName(const HierarchicalObjectName &other) noexcept=default
constexpr HierarchicalObjectName(const_pointer s)
Definition: hierarchical-object-name.h:54
constexpr HierarchicalObjectName(const_pointer s, size_type count)
Definition: hierarchical-object-name.h:50
constexpr HierarchicalObjectName() noexcept
Definition: hierarchical-object-name.h:46
constexpr std::string_view base_name() const
Definition: hierarchical-object-name.h:99
Definition: after-bank.h:33
Definition: common-types.h:66