Skip to content

Commit dd70bcd

Browse files
committed
Implement iterator interface for stages in ProcessorStructure #203
1 parent 2048c66 commit dd70bcd

File tree

6 files changed

+136
-113
lines changed

6 files changed

+136
-113
lines changed

src/editor/codeeditor.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -445,31 +445,27 @@ void CodeEditor::updateHighlighting() {
445445
const unsigned stages = proc->structure().numStages();
446446
auto colorGenerator = Colors::incrementalRedGenerator(stages);
447447

448-
for (auto laneIt : proc->structure()) {
449-
for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
450-
StageIndex sid = StageIndex{laneIt.first, stageIdx};
451-
const auto stageInfo = proc->stageInfo(sid);
452-
QColor stageColor = colorGenerator();
453-
if (stageInfo.stage_valid) {
454-
auto mappingIt = sourceMapping.find(stageInfo.pc);
455-
if (mappingIt == sourceMapping.end()) {
456-
// No source line registerred for this PC.
448+
for (auto sid : proc->structure().stageIt()) {
449+
const auto stageInfo = proc->stageInfo(sid);
450+
QColor stageColor = colorGenerator();
451+
if (stageInfo.stage_valid) {
452+
auto mappingIt = sourceMapping.find(stageInfo.pc);
453+
if (mappingIt == sourceMapping.end()) {
454+
// No source line registerred for this PC.
455+
continue;
456+
}
457+
458+
for (auto sourceLine : mappingIt->second) {
459+
// Find block
460+
QTextBlock block = document()->findBlockByLineNumber(sourceLine);
461+
if (!block.isValid())
457462
continue;
458-
}
459-
460-
for (auto sourceLine : mappingIt->second) {
461-
// Find block
462-
QTextBlock block = document()->findBlockByLineNumber(sourceLine);
463-
if (!block.isValid())
464-
continue;
465-
466-
// Record the stage name for the highlighted block for later painting
467-
QString stageString =
468-
ProcessorHandler::getProcessor()->stageName(sid);
469-
if (!stageInfo.namedState.isEmpty())
470-
stageString += " (" + stageInfo.namedState + ")";
471-
highlightBlock(block, stageColor, stageString);
472-
}
463+
464+
// Record the stage name for the highlighted block for later painting
465+
QString stageString = ProcessorHandler::getProcessor()->stageName(sid);
466+
if (!stageInfo.namedState.isEmpty())
467+
stageString += " (" + stageInfo.namedState + ")";
468+
highlightBlock(block, stageColor, stageString);
473469
}
474470
}
475471
}

src/instructionmodel.cpp

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ int InstructionModel::addressToRow(AInt addr) const {
2727

2828
InstructionModel::InstructionModel(QObject *parent)
2929
: QAbstractTableModel(parent) {
30-
for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
31-
for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
32-
StageIndex idx = {laneIt.first, stageIdx};
33-
m_stageNames[idx] = ProcessorHandler::getProcessor()->stageName(idx);
34-
m_stageInfos[idx] = ProcessorHandler::getProcessor()->stageInfo(idx);
35-
}
30+
for (auto idx : ProcessorHandler::getProcessor()->structure().stageIt()) {
31+
m_stageNames[idx] = ProcessorHandler::getProcessor()->stageName(idx);
32+
m_stageInfos[idx] = ProcessorHandler::getProcessor()->stageInfo(idx);
3633
}
3734
connect(ProcessorHandler::get(), &ProcessorHandler::procStateChangedNonRun,
3835
this, &InstructionModel::updateStageInfo);
@@ -67,34 +64,31 @@ int InstructionModel::rowCount(const QModelIndex &) const { return m_rowCount; }
6764

6865
void InstructionModel::updateStageInfo() {
6966
bool firstStageChanged = false;
70-
for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
71-
for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
72-
StageIndex idx = {laneIt.first, stageIdx};
73-
auto stageInfoIt = m_stageInfos.find(idx);
74-
if (stageInfoIt != m_stageInfos.end()) {
75-
auto &oldStageInfo = m_stageInfos.at(idx);
76-
if (idx == StageIndex(0, 0)) {
77-
if (oldStageInfo.pc !=
78-
ProcessorHandler::getProcessor()->stageInfo(idx).pc) {
79-
firstStageChanged = true;
80-
}
81-
}
82-
const auto stageInfo = ProcessorHandler::getProcessor()->stageInfo(idx);
83-
const AInt oldAddress = oldStageInfo.pc;
84-
if (oldStageInfo != stageInfo) {
85-
oldStageInfo = stageInfo;
86-
const int oldRow = addressToRow(oldAddress);
87-
const int newRow = addressToRow(stageInfo.pc);
88-
const QModelIndex oldIdx = index(oldRow, Stage);
89-
const QModelIndex newIdx = index(newRow, Stage);
90-
emit dataChanged(oldIdx, oldIdx, {Qt::DisplayRole});
91-
emit dataChanged(newIdx, newIdx, {Qt::DisplayRole});
92-
}
93-
if (firstStageChanged) {
94-
emit firstStageInstrChanged(addressToRow(m_stageInfos.at({0, 0}).pc));
95-
firstStageChanged = false;
67+
for (auto idx : ProcessorHandler::getProcessor()->structure().stageIt()) {
68+
auto stageInfoIt = m_stageInfos.find(idx);
69+
if (stageInfoIt != m_stageInfos.end()) {
70+
auto &oldStageInfo = m_stageInfos.at(idx);
71+
if (idx == StageIndex(0, 0)) {
72+
if (oldStageInfo.pc !=
73+
ProcessorHandler::getProcessor()->stageInfo(idx).pc) {
74+
firstStageChanged = true;
9675
}
9776
}
77+
const auto stageInfo = ProcessorHandler::getProcessor()->stageInfo(idx);
78+
const AInt oldAddress = oldStageInfo.pc;
79+
if (oldStageInfo != stageInfo) {
80+
oldStageInfo = stageInfo;
81+
const int oldRow = addressToRow(oldAddress);
82+
const int newRow = addressToRow(stageInfo.pc);
83+
const QModelIndex oldIdx = index(oldRow, Stage);
84+
const QModelIndex newIdx = index(newRow, Stage);
85+
emit dataChanged(oldIdx, oldIdx, {Qt::DisplayRole});
86+
emit dataChanged(newIdx, newIdx, {Qt::DisplayRole});
87+
}
88+
if (firstStageChanged) {
89+
emit firstStageInstrChanged(addressToRow(m_stageInfos.at({0, 0}).pc));
90+
firstStageChanged = false;
91+
}
9892
}
9993
}
10094
}

src/pipelinediagrammodel.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,9 @@ void PipelineDiagramModel::gatherStageInfo() {
7676
if (stageInfoForCycle == m_cycleStageInfos.end()) {
7777
return;
7878
}
79-
for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
80-
for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
81-
StageIndex idx = {laneIt.first, stageIdx};
82-
stageInfoForCycle->second[idx] =
83-
ProcessorHandler::getProcessor()->stageInfo(idx);
84-
}
85-
}
79+
for (auto idx : ProcessorHandler::getProcessor()->structure().stageIt())
80+
stageInfoForCycle->second[idx] =
81+
ProcessorHandler::getProcessor()->stageInfo(idx);
8682
}
8783

8884
QVariant PipelineDiagramModel::data(const QModelIndex &index, int role) const {

src/processors/interface/ripesprocessor.h

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,68 @@ struct MemoryAccess {
4444

4545
struct StageIndex : public std::pair<unsigned, unsigned> {
4646
using std::pair<unsigned, unsigned>::pair;
47-
unsigned lane() const {return this->first;}
48-
unsigned index() const {return this->second;}
47+
unsigned lane() const { return this->first; }
48+
unsigned index() const { return this->second; }
4949
};
5050

51-
52-
/// Structural description of the processor model. Currently this is a map of {# of lanes, # of stages}.
53-
/// @todo: add an iterator over all stages.
51+
/// Structural description of the processor model. Currently this is a map of {#
52+
/// of lanes, # of stages}.
5453
struct ProcessorStructure : public std::map<unsigned, unsigned> {
5554
using std::map<unsigned, unsigned>::map;
5655

56+
struct StageIterator {
57+
StageIterator(const ProcessorStructure &structure)
58+
: m_structure(structure) {}
59+
60+
struct Iterator {
61+
using iterator_category = std::forward_iterator_tag;
62+
using difference_type = std::ptrdiff_t;
63+
using value_type = StageIndex;
64+
using pointer = value_type *;
65+
using reference = value_type &;
66+
67+
Iterator(unsigned lane, unsigned index,
68+
const ProcessorStructure &structure)
69+
: m_lane(lane), m_index(index), m_structure(structure) {}
70+
StageIndex operator*() const { return {m_lane, m_index}; }
71+
StageIndex operator->() const { return {m_lane, m_index}; }
72+
Iterator &operator++() {
73+
++m_index;
74+
if (m_index == m_structure.at(m_lane)) {
75+
m_index = 0;
76+
++m_lane;
77+
}
78+
return *this;
79+
}
80+
Iterator operator++(int) {
81+
Iterator tmp = *this;
82+
++(*this);
83+
return tmp;
84+
}
85+
bool operator==(const Iterator &other) const {
86+
return m_lane == other.m_lane && m_index == other.m_index;
87+
}
88+
bool operator!=(const Iterator &other) const { return !(*this == other); }
89+
90+
unsigned m_lane = 0;
91+
unsigned m_index = 0;
92+
const ProcessorStructure &m_structure;
93+
};
94+
95+
Iterator begin() const { return Iterator(0, 0, m_structure); }
96+
Iterator end() const {
97+
return Iterator(m_structure.size(), 0, m_structure);
98+
}
99+
const ProcessorStructure &m_structure;
100+
};
101+
102+
// Returns an iterator over all stages.
103+
StageIterator stageIt() const { return {*this}; }
104+
57105
// Returns the total number of stages of this processor.
58106
unsigned numStages() const {
59107
unsigned count = 0;
60-
for(auto it : *this)
108+
for (auto it : *this)
61109
count += it.second;
62110
return count;
63111
}
@@ -129,9 +177,7 @@ class RipesProcessor {
129177
* @brief stageCount
130178
* @return number of stages for the processor
131179
*/
132-
virtual const ProcessorStructure& structure() const = 0;
133-
134-
180+
virtual const ProcessorStructure &structure() const = 0;
135181

136182
/**
137183
* @brief getPcForStage

src/processortab.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,12 @@ void ProcessorTab::loadLayout(const Layout &layout) {
186186

187187
// Adjust stage label positions
188188
const auto &parent = m_stageInstructionLabels.at({0, 0})->parentItem();
189-
for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
190-
for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
191-
StageIndex sid = {laneIt.first, stageIdx};
192-
auto &label = m_stageInstructionLabels.at(sid);
193-
QFontMetrics metrics(label->font());
194-
label->setPos(parent->boundingRect().width() *
195-
layout.stageLabelPositions.at(sid).x(),
196-
metrics.height() * layout.stageLabelPositions.at(sid).y());
197-
}
189+
for (auto sid : ProcessorHandler::getProcessor()->structure().stageIt()) {
190+
auto &label = m_stageInstructionLabels.at(sid);
191+
QFontMetrics metrics(label->font());
192+
label->setPos(parent->boundingRect().width() *
193+
layout.stageLabelPositions.at(sid).x(),
194+
metrics.height() * layout.stageLabelPositions.at(sid).y());
198195
}
199196
}
200197

@@ -473,31 +470,28 @@ void ProcessorTab::enableSimulatorControls() {
473470

474471
void ProcessorTab::updateInstructionLabels() {
475472
const auto &proc = ProcessorHandler::getProcessor();
476-
for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
477-
for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
478-
StageIndex sid = {laneIt.first, stageIdx};
479-
if (!m_stageInstructionLabels.count(sid))
480-
continue;
481-
const auto stageInfo = proc->stageInfo(sid);
482-
auto &instrLabel = m_stageInstructionLabels.at(sid);
483-
QString instrString;
484-
if (stageInfo.state != StageInfo::State::None) {
485-
/* clang-format off */
473+
for (auto sid : ProcessorHandler::getProcessor()->structure().stageIt()) {
474+
if (!m_stageInstructionLabels.count(sid))
475+
continue;
476+
const auto stageInfo = proc->stageInfo(sid);
477+
auto &instrLabel = m_stageInstructionLabels.at(sid);
478+
QString instrString;
479+
if (stageInfo.state != StageInfo::State::None) {
480+
/* clang-format off */
486481
switch (stageInfo.state) {
487482
case StageInfo::State::Flushed: instrString = "nop (flush)"; break;
488483
case StageInfo::State::Stalled: instrString = "nop (stall)"; break;
489484
case StageInfo::State::WayHazard: if(stageInfo.stage_valid) {instrString = "nop (way hazard)";} break;
490485
case StageInfo::State::Unused: instrString = "nop (unused)"; break;
491486
case StageInfo::State::None: Q_UNREACHABLE();
492487
}
493-
/* clang-format on */
494-
instrLabel->forceDefaultTextColor(Qt::red);
495-
} else if (stageInfo.stage_valid) {
496-
instrString = ProcessorHandler::disassembleInstr(stageInfo.pc);
497-
instrLabel->clearForcedDefaultTextColor();
498-
}
499-
instrLabel->setText(instrString);
488+
/* clang-format on */
489+
instrLabel->forceDefaultTextColor(Qt::red);
490+
} else if (stageInfo.stage_valid) {
491+
instrString = ProcessorHandler::disassembleInstr(stageInfo.pc);
492+
instrLabel->clearForcedDefaultTextColor();
500493
}
494+
instrLabel->setText(instrString);
501495
}
502496
}
503497

src/programviewer.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,18 @@ void ProgramViewer::updateHighlightedAddresses() {
105105
ProcessorHandler::getProcessor()->structure().numStages();
106106
auto colorGenerator = Colors::incrementalRedGenerator(stages);
107107

108-
for (auto laneIt : ProcessorHandler::getProcessor()->structure()) {
109-
for (unsigned stageIdx = 0; stageIdx < laneIt.second; stageIdx++) {
110-
StageIndex sid = {laneIt.first, stageIdx};
111-
const auto stageInfo = ProcessorHandler::getProcessor()->stageInfo(sid);
112-
if (stageInfo.stage_valid) {
113-
auto block = blockForAddress(stageInfo.pc);
114-
if (!block.isValid())
115-
continue;
116-
117-
// Record the stage name for the highlighted block for later painting
118-
QString stageString = ProcessorHandler::getProcessor()->stageName(sid);
119-
if (!stageInfo.namedState.isEmpty())
120-
stageString += " (" + stageInfo.namedState + ")";
121-
highlightBlock(block, colorGenerator(), stageString);
122-
}
108+
for (auto sid : ProcessorHandler::getProcessor()->structure().stageIt()) {
109+
const auto stageInfo = ProcessorHandler::getProcessor()->stageInfo(sid);
110+
if (stageInfo.stage_valid) {
111+
auto block = blockForAddress(stageInfo.pc);
112+
if (!block.isValid())
113+
continue;
114+
115+
// Record the stage name for the highlighted block for later painting
116+
QString stageString = ProcessorHandler::getProcessor()->stageName(sid);
117+
if (!stageInfo.namedState.isEmpty())
118+
stageString += " (" + stageInfo.namedState + ")";
119+
highlightBlock(block, colorGenerator(), stageString);
123120
}
124121
}
125122

0 commit comments

Comments
 (0)