Pass program as weakptr
MODIFY
src/edittab.cpp
src/edittab.cpp
@@ -240,7 +240,7 @@ void EditTab::enableAssemblyInput() {
}
void EditTab::updateProgramViewer() {
- m_ui->programViewer->updateProgram(*m_activeProgram, !m_ui->disassembledViewButton->isChecked());
+ m_ui->programViewer->updateProgram(!m_ui->disassembledViewButton->isChecked());
}
void EditTab::enableEditor() {

MODIFY
src/parser.cpp
src/parser.cpp
@@ -24,7 +24,7 @@ Parser::Parser() {
Parser::~Parser() {}
-QString Parser::disassemble(const Program& program, AddrOffsetMap& addrOffsetMap) const {
+QString Parser::disassemble(std::weak_ptr<const Program> program, AddrOffsetMap& addrOffsetMap) const {
return stringifyProgram(program, 4,
[this, &program](const std::vector<char>& buffer, uint32_t index) {
// Hardcoded for RV32 for now
@@ -37,7 +37,7 @@ QString Parser::disassemble(const Program& program, AddrOffsetMap& addrOffsetMap
addrOffsetMap);
}
-QString Parser::binarize(const Program& program, AddrOffsetMap& addrOffsetMap) const {
+QString Parser::binarize(std::weak_ptr<const Program> program, AddrOffsetMap& addrOffsetMap) const {
return stringifyProgram(
program, 4,
[](const std::vector<char>& buffer, uint32_t) {
@@ -56,10 +56,11 @@ void incrementAddressOffsetMap(const QString& text, AddrOffsetMap& map, int& off
}
} // namespace
-QString Parser::stringifyProgram(const Program& program, unsigned stride,
+QString Parser::stringifyProgram(std::weak_ptr<const Program> program, unsigned stride,
std::function<QString(const std::vector<char>&, uint32_t index)> stringifier,
AddrOffsetMap& addrOffsetMap) const {
- const auto* textSection = program.getSection(TEXT_SECTION_NAME);
+ if (auto sp = program.lock()) {
+ const auto* textSection = sp->getSection(TEXT_SECTION_NAME);
if (!textSection)
return QString();
@@ -75,13 +76,13 @@ QString Parser::stringifyProgram(const Program& program, unsigned stride,
dataStream.readRawData(buffer.data(), stride);
// Function label
- if (program.symbols.count(addr)) {
- // We are adding non-instruction lines to the output string. Record the line number as well as the sum of
- // invalid lines up to the given point.
+ if (sp->symbols.count(addr)) {
+ // We are adding non-instruction lines to the output string. Record the line number as well as the sum
+ // of invalid lines up to the given point.
incrementAddressOffsetMap(out, addrOffsetMap, infoOffsets);
out += "\n";
incrementAddressOffsetMap(out, addrOffsetMap, infoOffsets);
- out += QString::number(addr, 16).rightJustified(8, '0') + " <" + program.symbols.at(addr) + ">:\n";
+ out += QString::number(addr, 16).rightJustified(8, '0') + " <" + sp->symbols.at(addr) + ">:\n";
}
// Instruction address
@@ -99,6 +100,8 @@ QString Parser::stringifyProgram(const Program& program, unsigned stride,
}
return out;
}
+ return QString();
+}
decode_functor Parser::generateWordParser(std::vector<int> bitFields) {
// Generates functors that can decode a binary number based on the input
@@ -133,18 +136,19 @@ decode_functor Parser::generateWordParser(std::vector<int> bitFields) {
return wordParser;
}
-QString Parser::disassemble(const Program& program, uint32_t instr, uint32_t address) const {
+QString Parser::disassemble(std::weak_ptr<const Program> program, uint32_t instr, uint32_t address) const {
+ if (auto sp = program.lock()) {
switch (instr & 0x7f) {
case instrType::LUI:
return generateLuiString(instr);
case instrType::AUIPC:
return generateAuipcString(instr);
case instrType::JAL:
- return generateJalString(instr, address, program);
+ return generateJalString(instr, address, *sp);
case instrType::JALR:
return generateJalrString(instr);
case instrType::BRANCH:
- return generateBranchString(instr, address, program);
+ return generateBranchString(instr, address, *sp);
case instrType::LOAD:
return generateLoadString(instr);
case instrType::STORE:
@@ -159,6 +163,7 @@ QString Parser::disassemble(const Program& program, uint32_t instr, uint32_t add
return QString("Invalid instruction");
}
}
+}
QString Parser::generateEcallString(uint32_t) const {
return QString("ecall");
}

MODIFY
src/parser.h
src/parser.h
#include <fstream>
#include <functional>
+#include <memory>
#include <unordered_map>
#include <vector>
@@ -28,7 +29,7 @@ public:
return &parser;
}
- QString disassemble(const Program& program, uint32_t instr, uint32_t address) const;
+ QString disassemble(std::weak_ptr<const Program> program, uint32_t instr, uint32_t address) const;
// Const interfaces to intstruction decode lamdas
std::vector<uint32_t> decodeUInstr(uint32_t instr) const { return m_decodeUInstr(instr); }
@@ -38,11 +39,11 @@ public:
std::vector<uint32_t> decodeRInstr(uint32_t instr) const { return m_decodeRInstr(instr); }
std::vector<uint32_t> decodeBInstr(uint32_t instr) const { return m_decodeBInstr(instr); }
- QString disassemble(const Program& program, AddrOffsetMap& addrOffsetMap) const;
- QString binarize(const Program& program, AddrOffsetMap& addrOffsetMap) const;
+ QString disassemble(std::weak_ptr<const Program> program, AddrOffsetMap& addrOffsetMap) const;
+ QString binarize(std::weak_ptr<const Program> program, AddrOffsetMap& addrOffsetMap) const;
private:
- QString stringifyProgram(const Program& program, unsigned stride,
+ QString stringifyProgram(std::weak_ptr<const Program> program, unsigned stride,
std::function<QString(const std::vector<char>& buffer, uint32_t index)> stringifier,
AddrOffsetMap& addrOffsetMap) const;

MODIFY
src/processorhandler.cpp
src/processorhandler.cpp
@@ -178,7 +178,7 @@ unsigned long ProcessorHandler::getTextStart() const {
QString ProcessorHandler::parseInstrAt(const uint32_t addr) const {
if (m_program) {
- return Parser::getParser()->disassemble(*m_program, m_currentProcessor->getMemory().readMem(addr), addr);
+ return Parser::getParser()->disassemble(m_program, m_currentProcessor->getMemory().readMem(addr), addr);
} else {
return QString();
}

MODIFY
src/processorhandler.h
src/processorhandler.h
@@ -28,7 +28,7 @@ public:
vsrtl::core::RipesProcessor* getProcessorNonConst() { return m_currentProcessor.get(); }
const vsrtl::core::RipesProcessor* getProcessor() { return m_currentProcessor.get(); }
const ProcessorID& getID() const { return m_currentID; }
- std::weak_ptr<Program> getProgram() const { return m_program; }
+ std::weak_ptr<const Program> getProgram() const { return m_program; }
const ISAInfoBase* currentISA() const { return m_currentProcessor->implementsISA(); }
/**

MODIFY
src/programviewer.cpp
src/programviewer.cpp
@@ -44,13 +44,15 @@ void ProgramViewer::resizeEvent(QResizeEvent* e) {
updateHighlightedAddresses();
}
-void ProgramViewer::updateProgram(const Program& program, bool binary) {
+void ProgramViewer::updateProgram(bool binary) {
m_labelAddrOffsetMap.clear();
- const QString text = binary ? Parser::getParser()->binarize(program, m_labelAddrOffsetMap)
- : Parser::getParser()->disassemble(program, m_labelAddrOffsetMap);
+ const QString text =
+ binary ? Parser::getParser()->binarize(ProcessorHandler::get()->getProgram(), m_labelAddrOffsetMap)
+ : Parser::getParser()->disassemble(ProcessorHandler::get()->getProgram(), m_labelAddrOffsetMap);
- // A memory occurs within QPlainTextEdit::clear if extra selections has been set. This is most possibly a bug, but
- // seems to be fixed if we manually clear the selections before we clear (and add new text) to the text edit.
+ // A memory occurs within QPlainTextEdit::clear if extra selections has been set. This is most possibly a bug,
+ // but seems to be fixed if we manually clear the selections before we clear (and add new text) to the text
+ // edit.
setExtraSelections({});
setPlainText(text);

MODIFY
src/programviewer.h
src/programviewer.h
@@ -32,10 +32,10 @@ public:
///
/// \brief updateProgram
- /// Refreshes the programViewer view with @p program, using the parser to either disassemble or (if @p binary is
+ /// Refreshes the programViewer view, using the parser to either disassemble or (if @p binary is
/// true) show the raw binary version of the loaded program.
///
- void updateProgram(const Program& program, bool binary = false);
+ void updateProgram(bool binary = false);
public slots:
void updateHighlightedAddresses();

Pass program as weakptr