From e7631a62857b5664797d9d49be115f4ae624cc43 Mon Sep 17 00:00:00 2001 From: Bhavy Airi Date: Thu, 3 Aug 2023 18:01:19 +0530 Subject: [PATCH] refactor: move ProcessorHandler::getCurrentProgramSize to Program Signed-off-by: Bhavy Airi --- src/assembler/program.cpp | 11 ++++++++++- src/assembler/program.h | 15 +++++++++++++++ src/pipelinediagrammodel.cpp | 2 +- src/processorhandler.cpp | 10 ---------- src/processorhandler.h | 7 ------- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/assembler/program.cpp b/src/assembler/program.cpp index 82f9190d8..e7e4334f7 100644 --- a/src/assembler/program.cpp +++ b/src/assembler/program.cpp @@ -77,7 +77,7 @@ const DisassembledProgram &Program::getDisassembled() const { const VInt textSectionBaseAddr = textSection->address; unsigned line = 0; for (AInt addr = 0; - addr < static_cast(ProcessorHandler::getCurrentProgramSize());) { + addr < static_cast(Program::getCurrentProgramSize());) { const VInt disassembleAddr = addr + textSectionBaseAddr; auto disRes = assembler->disassemble(memory.readMem(disassembleAddr, instrBytes), @@ -98,6 +98,15 @@ const DisassembledProgram &Program::getDisassembled() const { return disassembled; } +int Program::_getCurrentProgramSize() const { + const auto *textSection = getSection(TEXT_SECTION_NAME); + + if (textSection) + return textSection->data.length(); + + return 0; +} + QString Program::calculateHash(const QByteArray &data) { return QCryptographicHash::hash(data, QCryptographicHash::Sha1); } diff --git a/src/assembler/program.h b/src/assembler/program.h index e2848f752..c3dcba696 100644 --- a/src/assembler/program.h +++ b/src/assembler/program.h @@ -121,6 +121,12 @@ class DisassembledProgram { */ class Program { public: + /// Returns a pointer to this class singleton. + static Program *get() { + static auto *handler = new Program; + return handler; + } + // A source mapping is a mapping from {instruction address : source code // lines} using SourceMapping = std::map>; @@ -148,9 +154,18 @@ class Program { /// Calculates a hash used for source identification. static QString calculateHash(const QByteArray &data); + /** + * @brief getCurrentProgramSize + * @return size (in bytes) of the currently loaded .text segment + */ + static int getCurrentProgramSize() { return get()->_getCurrentProgramSize(); } + private: /// A caching of the disassembled version of this program. mutable DisassembledProgram disassembled; + + /// Returns the current program size. + int _getCurrentProgramSize() const; }; } // namespace Ripes diff --git a/src/pipelinediagrammodel.cpp b/src/pipelinediagrammodel.cpp index 44a5c9190..fbd3e9b84 100644 --- a/src/pipelinediagrammodel.cpp +++ b/src/pipelinediagrammodel.cpp @@ -38,7 +38,7 @@ QVariant PipelineDiagramModel::headerData(int section, } int PipelineDiagramModel::rowCount(const QModelIndex &) const { - return ProcessorHandler::getCurrentProgramSize() / + return Program::getCurrentProgramSize() / ProcessorHandler::currentISA()->instrBytes(); } diff --git a/src/processorhandler.cpp b/src/processorhandler.cpp index 0caa9e19c..1b54eb307 100644 --- a/src/processorhandler.cpp +++ b/src/processorhandler.cpp @@ -361,16 +361,6 @@ void ProcessorHandler::_selectProcessor(const ProcessorID &id, RipesSettings::getObserver(RIPES_GLOBALSIGNAL_REQRESET)->trigger(); } -int ProcessorHandler::_getCurrentProgramSize() const { - if (m_program) { - const auto *textSection = m_program->getSection(TEXT_SECTION_NAME); - if (textSection) - return textSection->data.length(); - } - - return 0; -} - AInt ProcessorHandler::_getTextStart() const { if (m_program) { const auto *textSection = m_program->getSection(TEXT_SECTION_NAME); diff --git a/src/processorhandler.h b/src/processorhandler.h index d5f628877..781403a6f 100644 --- a/src/processorhandler.h +++ b/src/processorhandler.h @@ -100,12 +100,6 @@ class ProcessorHandler : public QObject { return get()->_isExecutableAddress(address); } - /** - * @brief getCurrentProgramSize - * @return size (in bytes) of the currently loaded .text segment - */ - static int getCurrentProgramSize() { return get()->_getCurrentProgramSize(); } - /** * @brief getEntryPoint * @return address of the entry point of the currently loaded program @@ -290,7 +284,6 @@ private slots: const ProcessorID &id, const QStringList &extensions = {}, const RegisterInitialization &setup = RegisterInitialization()); bool _isExecutableAddress(AInt address) const; - int _getCurrentProgramSize() const; AInt _getTextStart() const; QString _disassembleInstr(const AInt address) const; vsrtl::core::AddressSpaceMM &_getMemory();