Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/49780 )
Change subject: cpu: Add an iterator type to RegClass.
......................................................................
cpu: Add an iterator type to RegClass.
This will enable it to be used in range based for loops, to iterate over
all the RegIds which are part of a RegClass. This cleans up that sort of
loop a bit, and also makes it less necessary to construct a RegId
directly.
Change-Id: Ia3c2aa5cf4d842465bd0948d174f51a1b92e5e3f
---
M src/cpu/reg_class.hh
1 file changed, 71 insertions(+), 0 deletions(-)
diff --git a/src/cpu/reg_class.hh b/src/cpu/reg_class.hh
index 732b10f..be076f6 100644
--- a/src/cpu/reg_class.hh
+++ b/src/cpu/reg_class.hh
@@ -43,6 +43,7 @@
#include <any>
#include <cstddef>
+#include <iterator>
#include <limits>
#include <string>
@@ -78,6 +79,8 @@
virtual std::string valString(const void *val, size_t size) const;
};
+class RegClassIterator;
+
class RegClass
{
private:
@@ -120,6 +123,11 @@
{
return _ops->valString(val, size());
}
+
+ using iterator = RegClassIterator;
+
+ inline iterator begin() const;
+ inline iterator end() const;
};
/** Register ID: describe an architectural register with its class and
index.
@@ -136,6 +144,7 @@
int numPinnedWrites;
friend struct std::hash<RegId>;
+ friend class RegClassIterator;
public:
constexpr RegId() : RegId(InvalidRegClass, 0) {}
@@ -210,6 +219,68 @@
}
};
+class RegClassIterator
+{
+ private:
+ const RegClass ®Class;
+ RegId id;
+
+ RegClassIterator(const RegClass ®_class, RegIndex idx=0) :
+ regClass(reg_class), id(reg_class.type(), idx)
+ {}
+
+ friend class RegClass;
+
+ public:
+ using iterator_category = std::forward_iterator_tag;
+ using difference_type = std::size_t;
+ using value_type = const RegId;
+ using pointer = value_type *;
+ using reference = value_type &;
+
+ reference operator*() const { return id; }
+ pointer operator->() { return &id; }
+
+ RegClassIterator &
+ operator++()
+ {
+ id.regIdx++;
+ return *this;
+ }
+
+ RegClassIterator
+ operator++(int)
+ {
+ auto tmp = *this;
+ ++(*this);
+ return tmp;
+ }
+
+ bool
+ operator==(const RegClassIterator &other) const
+ {
+ return id == other.id;
+ }
+
+ bool
+ operator!=(const RegClassIterator &other) const
+ {
+ return id != other.id;
+ }
+};
+
+RegClassIterator
+RegClass::begin() const
+{
+ return RegClassIterator(*this, 0);
+}
+
+RegClassIterator
+RegClass::end() const
+{
+ return RegClassIterator(*this, size());
+}
+
template <typename ValueType>
class TypedRegClassOps : public RegClassOps
{
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/49780
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ia3c2aa5cf4d842465bd0948d174f51a1b92e5e3f
Gerrit-Change-Number: 49780
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabe.bl...@gmail.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s