Implement iterator interface for stages in ProcessorStructure
MODIFY
src/editor/codeeditor.cpp
src/editor/codeeditor.cpp
@@ -445,9 +445,7 @@ void CodeEditor::updateHighlighting() {
const unsigned stages = proc->structure().numStages();
auto colorGenerator = Colors::incrementalRedGenerator(stages);
- for (auto laneIt : proc->structure()) {
- for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
- StageIndex sid = StageIndex{laneIt.first, stageIdx};
+ for (auto sid : proc->structure().stageIt()) {
const auto stageInfo = proc->stageInfo(sid);
QColor stageColor = colorGenerator();
if (stageInfo.stage_valid) {
@@ -464,8 +462,7 @@ void CodeEditor::updateHighlighting() {
continue;
// Record the stage name for the highlighted block for later painting
- QString stageString =
- ProcessorHandler::getProcessor()->stageName(sid);
+ QString stageString = ProcessorHandler::getProcessor()->stageName(sid);
if (!stageInfo.namedState.isEmpty())
stageString += " (" + stageInfo.namedState + ")";
highlightBlock(block, stageColor, stageString);
@@ -473,6 +470,5 @@ void CodeEditor::updateHighlighting() {
}
}
}
-}
} // namespace Ripes

MODIFY
src/instructionmodel.cpp
src/instructionmodel.cpp
@@ -27,13 +27,10 @@ int InstructionModel::addressToRow(AInt addr) const {
InstructionModel::InstructionModel(QObject *parent)
: QAbstractTableModel(parent) {
- for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
- for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
- StageIndex idx = {laneIt.first, stageIdx};
+ for (auto idx : ProcessorHandler::getProcessor()->structure().stageIt()) {
m_stageNames[idx] = ProcessorHandler::getProcessor()->stageName(idx);
m_stageInfos[idx] = ProcessorHandler::getProcessor()->stageInfo(idx);
}
- }
connect(ProcessorHandler::get(), &ProcessorHandler::procStateChangedNonRun,
this, &InstructionModel::updateStageInfo);
connect(ProcessorHandler::get(), &ProcessorHandler::processorReset, this,
@@ -67,9 +64,7 @@ int InstructionModel::rowCount(const QModelIndex &) const { return m_rowCount; }
void InstructionModel::updateStageInfo() {
bool firstStageChanged = false;
- for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
- for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
- StageIndex idx = {laneIt.first, stageIdx};
+ for (auto idx : ProcessorHandler::getProcessor()->structure().stageIt()) {
auto stageInfoIt = m_stageInfos.find(idx);
if (stageInfoIt != m_stageInfos.end()) {
auto &oldStageInfo = m_stageInfos.at(idx);
@@ -97,7 +92,6 @@ void InstructionModel::updateStageInfo() {
}
}
}
-}
bool InstructionModel::setData(const QModelIndex &index, const QVariant &value,
int role) {

MODIFY
src/pipelinediagrammodel.cpp
src/pipelinediagrammodel.cpp
@@ -76,14 +76,10 @@ void PipelineDiagramModel::gatherStageInfo() {
if (stageInfoForCycle == m_cycleStageInfos.end()) {
return;
}
- for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
- for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
- StageIndex idx = {laneIt.first, stageIdx};
+ for (auto idx : ProcessorHandler::getProcessor()->structure().stageIt())
stageInfoForCycle->second[idx] =
ProcessorHandler::getProcessor()->stageInfo(idx);
}
- }
-}
QVariant PipelineDiagramModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())

MODIFY
src/processors/interface/ripesprocessor.h
src/processors/interface/ripesprocessor.h
@@ -48,12 +48,60 @@ struct StageIndex : public std::pair<unsigned, unsigned> {
unsigned index() const { return this->second; }
};
-
-/// Structural description of the processor model. Currently this is a map of {# of lanes, # of stages}.
-/// @todo: add an iterator over all stages.
+/// Structural description of the processor model. Currently this is a map of {#
+/// of lanes, # of stages}.
struct ProcessorStructure : public std::map<unsigned, unsigned> {
using std::map<unsigned, unsigned>::map;
+ struct StageIterator {
+ StageIterator(const ProcessorStructure &structure)
+ : m_structure(structure) {}
+
+ struct Iterator {
+ using iterator_category = std::forward_iterator_tag;
+ using difference_type = std::ptrdiff_t;
+ using value_type = StageIndex;
+ using pointer = value_type *;
+ using reference = value_type &;
+
+ Iterator(unsigned lane, unsigned index,
+ const ProcessorStructure &structure)
+ : m_lane(lane), m_index(index), m_structure(structure) {}
+ StageIndex operator*() const { return {m_lane, m_index}; }
+ StageIndex operator->() const { return {m_lane, m_index}; }
+ Iterator &operator++() {
+ ++m_index;
+ if (m_index == m_structure.at(m_lane)) {
+ m_index = 0;
+ ++m_lane;
+ }
+ return *this;
+ }
+ Iterator operator++(int) {
+ Iterator tmp = *this;
+ ++(*this);
+ return tmp;
+ }
+ bool operator==(const Iterator &other) const {
+ return m_lane == other.m_lane && m_index == other.m_index;
+ }
+ bool operator!=(const Iterator &other) const { return !(*this == other); }
+
+ unsigned m_lane = 0;
+ unsigned m_index = 0;
+ const ProcessorStructure &m_structure;
+ };
+
+ Iterator begin() const { return Iterator(0, 0, m_structure); }
+ Iterator end() const {
+ return Iterator(m_structure.size(), 0, m_structure);
+ }
+ const ProcessorStructure &m_structure;
+ };
+
+ // Returns an iterator over all stages.
+ StageIterator stageIt() const { return {*this}; }
+
// Returns the total number of stages of this processor.
unsigned numStages() const {
unsigned count = 0;
@@ -131,8 +179,6 @@ public:
*/
virtual const ProcessorStructure &structure() const = 0;
-
-
/**
* @brief getPcForStage
* @param stageIndex

MODIFY
src/processortab.cpp
src/processortab.cpp
@@ -186,9 +186,7 @@ void ProcessorTab::loadLayout(const Layout &layout) {
// Adjust stage label positions
const auto &parent = m_stageInstructionLabels.at({0, 0})->parentItem();
- for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
- for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
- StageIndex sid = {laneIt.first, stageIdx};
+ for (auto sid : ProcessorHandler::getProcessor()->structure().stageIt()) {
auto &label = m_stageInstructionLabels.at(sid);
QFontMetrics metrics(label->font());
label->setPos(parent->boundingRect().width() *
@@ -196,7 +194,6 @@ void ProcessorTab::loadLayout(const Layout &layout) {
metrics.height() * layout.stageLabelPositions.at(sid).y());
}
}
-}
void ProcessorTab::setupSimulatorActions(QToolBar *controlToolbar) {
const QIcon processorIcon = QIcon(":/icons/cpu.svg");
@@ -473,9 +470,7 @@ void ProcessorTab::enableSimulatorControls() {
void ProcessorTab::updateInstructionLabels() {
const auto &proc = ProcessorHandler::getProcessor();
- for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
- for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
- StageIndex sid = {laneIt.first, stageIdx};
+ for (auto sid : ProcessorHandler::getProcessor()->structure().stageIt()) {
if (!m_stageInstructionLabels.count(sid))
continue;
const auto stageInfo = proc->stageInfo(sid);
@@ -499,7 +494,6 @@ void ProcessorTab::updateInstructionLabels() {
instrLabel->setText(instrString);
}
}
-}
void ProcessorTab::reset() {
m_autoClockAction->setChecked(false);

MODIFY
src/programviewer.cpp
src/programviewer.cpp
@@ -105,9 +105,7 @@ void ProgramViewer::updateHighlightedAddresses() {
ProcessorHandler::getProcessor()->structure().numStages();
auto colorGenerator = Colors::incrementalRedGenerator(stages);
- for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
- for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
- StageIndex sid = {laneIt.first, stageIdx};
+ for (auto sid : ProcessorHandler::getProcessor()->structure().stageIt()) {
const auto stageInfo = ProcessorHandler::getProcessor()->stageInfo(sid);
if (stageInfo.stage_valid) {
auto block = blockForAddress(stageInfo.pc);
@@ -121,7 +119,6 @@ void ProgramViewer::updateHighlightedAddresses() {
highlightBlock(block, colorGenerator(), stageString);
}
}
- }
if (m_following) {
updateCenterAddressFromProcessor();

Implement iterator interface for stages in ProcessorStructure #203