Make current Program managed by shared pointer
MODIFY
src/edittab.cpp
src/edittab.cpp
@@ -86,26 +86,27 @@ void EditTab::loadFile(const LoadFileParams& fileParams) {
}
bool success = true;
- Program loadedProgram;
+ auto loadedProgram = std::make_shared<Program>();
switch (fileParams.type) {
case SourceType::C:
case SourceType::Assembly:
- success &= loadSourceFile(loadedProgram, file);
+ success &= loadSourceFile(*loadedProgram, file);
break;
case SourceType::FlatBinary:
- success &= loadFlatBinaryFile(loadedProgram, file, fileParams.binaryEntryPoint, fileParams.binaryLoadAt);
+ success &= loadFlatBinaryFile(*loadedProgram, file, fileParams.binaryEntryPoint, fileParams.binaryLoadAt);
break;
case SourceType::InternalELF:
- success &= loadElfFile(loadedProgram, file);
+ success &= loadElfFile(*loadedProgram, file);
break;
case SourceType::ExternalELF:
// Since there is no related source code for an externally compiled ELF, the editor is disabled
disableEditor();
- success &= loadElfFile(loadedProgram, file);
+ success &= loadElfFile(*loadedProgram, file);
break;
}
if (success) {
+ // Move the shared pointer to be the current active program
m_activeProgram = loadedProgram;
emitProgramChanged();
} else {
@@ -166,7 +167,7 @@ void EditTab::sourceTypeChanged() {
}
void EditTab::emitProgramChanged() {
- emit programChanged(&m_activeProgram);
+ emit programChanged(m_activeProgram);
updateProgramViewer();
}
@@ -186,7 +187,7 @@ void EditTab::assemble() {
if (m_ui->codeEditor->syntaxAccepted()) {
m_assembler->assemble(*m_ui->codeEditor->document());
if (!m_assembler->hasError()) {
- m_activeProgram = m_assembler->getProgram();
+ m_activeProgram = std::make_shared<Program>(m_assembler->getProgram());
emitProgramChanged();
} else {
QMessageBox err;
@@ -231,13 +232,13 @@ void EditTab::setSourceText(const QString& text) {
void EditTab::enableAssemblyInput() {
// Clear currently loaded binary/ELF program
- m_activeProgram = Program();
+ m_activeProgram.reset();
m_ui->programViewer->clear();
enableEditor();
}
void EditTab::updateProgramViewer() {
- m_ui->programViewer->updateProgram(m_activeProgram, !m_ui->disassembledViewButton->isChecked());
+ m_ui->programViewer->updateProgram(*m_activeProgram, !m_ui->disassembledViewButton->isChecked());
}
void EditTab::enableEditor() {

MODIFY
src/edittab.h
src/edittab.h
@@ -43,7 +43,7 @@ public:
void loadExternalFile(const LoadFileParams& params);
signals:
- void programChanged(const Program* program);
+ void programChanged(std::shared_ptr<Program> program);
void editorStateChanged(bool enabled);
public slots:
@@ -86,7 +86,7 @@ private:
Ui::EditTab* m_ui = nullptr;
std::unique_ptr<Assembler> m_assembler;
- Program m_activeProgram;
+ std::shared_ptr<Program> m_activeProgram;
SourceType m_currentSourceType;

MODIFY
src/gotocombobox.cpp
src/gotocombobox.cpp
@@ -51,8 +51,8 @@ void GoToComboBox::signalFilter(int index) {
void GoToSectionComboBox::addTargets() {
addItem("Address...", QVariant::fromValue<GoToUserData>({GoToFunction::Address, 0}));
- if (ProcessorHandler::get()->getProgram()) {
- for (const auto& section : ProcessorHandler::get()->getProgram()->sections) {
+ if (auto prog_spt = ProcessorHandler::get()->getProgram().lock()) {
+ for (const auto& section : prog_spt->sections) {
addItem(section.name, QVariant::fromValue<GoToUserData>({GoToFunction::Custom, 0}));
}
}
@@ -60,7 +60,11 @@ void GoToSectionComboBox::addTargets() {
uint32_t GoToSectionComboBox::addrForIndex(int i) {
const QString& sectionName = itemText(i);
- return ProcessorHandler::get()->getProgram()->getSection(sectionName)->address;
+ if (auto prog_spt = ProcessorHandler::get()->getProgram().lock()) {
+ return prog_spt->getSection(sectionName)->address;
+ } else {
+ return -1;
+ }
}
void GoToRegisterComboBox::addTargets() {

MODIFY
src/instructionmodel.h
src/instructionmodel.h
@@ -14,15 +14,17 @@ class Parser;
class Pipeline;
static inline uint32_t indexToAddress(const QModelIndex& index) {
- if (ProcessorHandler::get()->getProgram()) {
- return (index.row() * 4) + ProcessorHandler::get()->getProgram()->getSection(TEXT_SECTION_NAME)->address;
+ if (auto prog_spt = ProcessorHandler::get()->getProgram().lock()) {
+ return (index.row() * 4) + prog_spt->getSection(TEXT_SECTION_NAME)->address;
}
return 0;
}
static inline int addressToIndex(uint32_t addr) {
- if (ProcessorHandler::get()->getProgram() && ProcessorHandler::get()->getProgram()->getSection(TEXT_SECTION_NAME) != nullptr) {
- return (addr - ProcessorHandler::get()->getProgram()->getSection(TEXT_SECTION_NAME)->address) / 4;
+ if (auto prog_spt = ProcessorHandler::get()->getProgram().lock()) {
+ if (prog_spt->getSection(TEXT_SECTION_NAME) != nullptr) {
+ return (addr - prog_spt->getSection(TEXT_SECTION_NAME)->address) / 4;
+ }
}
return 0;
}

MODIFY
src/processorhandler.cpp
src/processorhandler.cpp
@@ -21,7 +21,7 @@ ProcessorHandler::ProcessorHandler() {
m_currentProcessor->setReverseStackSize(RipesSettings::value(RIPES_SETTING_REWINDSTACKSIZE).toUInt());
}
-void ProcessorHandler::loadProgram(const Program* p) {
+void ProcessorHandler::loadProgram(std::shared_ptr<Program> p) {
// Stop any currently executing simulation
stop();

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; }
- const Program* getProgram() const { return m_program; }
+ std::weak_ptr<Program> getProgram() const { return m_program; }
const ISAInfoBase* currentISA() const { return m_currentProcessor->implementsISA(); }
/**
@@ -155,7 +155,7 @@ signals:
void runFinished();
public slots:
- void loadProgram(const Program* p);
+ void loadProgram(std::shared_ptr<Program> p);
private slots:
void handleSysCall();
@@ -173,7 +173,7 @@ private:
vsrtl::VSRTLWidget* m_vsrtlWidget = nullptr;
std::set<uint32_t> m_breakpoints;
- const Program* m_program = nullptr;
+ std::shared_ptr<Program> m_program;
QFutureWatcher<void> m_runWatcher;
bool m_stopRunningFlag = false;

MODIFY
src/stagetablemodel.cpp
src/stagetablemodel.cpp
namespace Ripes {
static inline uint32_t indexToAddress(unsigned index) {
- if (ProcessorHandler::get()->getProgram()) {
- return (index * ProcessorHandler::get()->currentISA()->bytes()) +
- ProcessorHandler::get()->getProgram()->getSection(TEXT_SECTION_NAME)->address;
+ if (auto spt = ProcessorHandler::get()->getProgram().lock()) {
+ return (index * ProcessorHandler::get()->currentISA()->bytes()) + spt->getSection(TEXT_SECTION_NAME)->address;
}
return 0;
}

MODIFY
test/tst_riscv.cpp
test/tst_riscv.cpp
@@ -77,7 +77,7 @@ private:
void handleSysCall();
bool m_stop = false;
- Program m_program;
+ std::shared_ptr<Program> m_program;
QString m_err;
private slots:
@@ -122,9 +122,9 @@ void tst_RISCV::loadBinaryToSimulator(const QString& binFile) {
}
QByteArray programByteArray = testFile.readAll();
- m_program = Program();
- m_program.sections.push_back({TEXT_SECTION_NAME, 0, programByteArray});
- ProcessorHandler::get()->loadProgram(&m_program);
+ m_program = std::make_shared<Program>();
+ m_program->sections.push_back({TEXT_SECTION_NAME, 0, programByteArray});
+ ProcessorHandler::get()->loadProgram(m_program);
}
void tst_RISCV::handleSysCall() {

Make current Program managed by shared pointer