Skip to content

Commit 0e25a68

Browse files
committed
refactor with symbol finding functions in R3000Acpu
1 parent 6a2669d commit 0e25a68

File tree

4 files changed

+65
-63
lines changed

4 files changed

+65
-63
lines changed

src/core/r3000a.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "core/sio.h"
3535
#include "core/sio1.h"
3636
#include "core/spu.h"
37+
#include "supportpsx/memory.h"
3738
#include "fmt/format.h"
3839

3940
int PCSX::R3000Acpu::psxInit() {
@@ -502,3 +503,28 @@ void PCSX::R3000Acpu::processB0KernelCall(uint32_t call) {
502503
}
503504
}
504505
}
506+
507+
std::pair<const uint32_t, std::string>* PCSX::R3000Acpu::findContainingSymbol(uint32_t addr) {
508+
auto symBefore = m_symbols.upper_bound(addr);
509+
if (symBefore != m_symbols.begin()) { // verify there is actually a symbol before addr
510+
symBefore--;
511+
if (symBefore->first != addr) {
512+
PCSX::PSXAddress addrInfo(addr);
513+
PCSX::PSXAddress symbolInfo(symBefore->first);
514+
if (addrInfo.segment != symbolInfo.segment) {
515+
// if the symbol is different and not in the same memory region, it'd be wrong
516+
return nullptr;
517+
}
518+
}
519+
return &*symBefore;
520+
}
521+
return nullptr;
522+
}
523+
524+
std::string* PCSX::R3000Acpu::getSymbolAt(uint32_t addr) {
525+
auto symBefore = m_symbols.find(addr);
526+
if (symBefore != m_symbols.end()) {
527+
return &symBefore->second;
528+
}
529+
return nullptr;
530+
}

src/core/r3000a.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ class R3000Acpu {
290290

291291
std::map<uint32_t, std::string> m_symbols;
292292

293+
std::pair<const uint32_t, std::string>* findContainingSymbol(uint32_t addr);
294+
std::string* getSymbolAt(uint32_t addr);
295+
293296
static int psxInit();
294297
virtual bool isDynarec() = 0;
295298
void psxReset();

src/gui/widgets/assembly.cc

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ void PCSX::Widgets::Assembly::Target(uint32_t value) {
302302
if (m_displayArrowForJumps) m_arrows.push_back({m_currentAddr, value});
303303
std::snprintf(label, sizeof(label), "0x%8.8x##%8.8x", value, m_currentAddr);
304304
std::string longLabel = label;
305-
auto symbols = findSymbol(value);
306-
if (symbols.size() != 0) longLabel = *symbols.begin() + " ;" + label;
305+
std::string* symbol = g_emulator->m_cpu->getSymbolAt(value);
306+
if (symbol) longLabel = *symbol + " ;" + label;
307307
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
308308
if (ImGui::Button(longLabel.c_str())) {
309309
m_jumpToPC = value;
@@ -366,8 +366,8 @@ void PCSX::Widgets::Assembly::OfB(int16_t offset, uint8_t reg, int size) {
366366
uint32_t addr = m_registers->GPR.r[reg] + offset;
367367

368368
std::string longLabel;
369-
auto symbols = findSymbol(addr);
370-
if (symbols.size() != 0) longLabel = *symbols.begin() + " ; ";
369+
std::string* symbol = g_emulator->m_cpu->getSymbolAt(addr);
370+
if (symbol) longLabel = *symbol + " ; ";
371371

372372
const auto& io = ImGui::GetIO();
373373
unsigned targetEditorIndex = io.KeyShift ? 1 : (io.KeyCtrl ? 2 : 0);
@@ -405,17 +405,17 @@ void PCSX::Widgets::Assembly::BranchDest(uint32_t value) {
405405
sameLine();
406406
m_arrows.push_back({m_currentAddr, value});
407407
std::snprintf(label, sizeof(label), "0x%8.8x##%8.8x", value, m_currentAddr);
408-
auto symbols = findSymbol(value);
409-
if (symbols.size() == 0) {
408+
std::string* symbol = g_emulator->m_cpu->getSymbolAt(value);
409+
if (symbol) {
410+
std::string longLabel = *symbol + " ;" + label;
410411
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
411-
if (ImGui::Button(label)) {
412+
if (ImGui::Button(longLabel.c_str())) {
412413
m_jumpToPC = value;
413414
}
414415
ImGui::PopStyleVar();
415416
} else {
416-
std::string longLabel = *symbols.begin() + " ;" + label;
417417
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
418-
if (ImGui::Button(longLabel.c_str())) {
418+
if (ImGui::Button(label)) {
419419
m_jumpToPC = value;
420420
}
421421
ImGui::PopStyleVar();
@@ -427,8 +427,8 @@ void PCSX::Widgets::Assembly::Offset(uint32_t addr, int size) {
427427
char label[32];
428428
std::snprintf(label, sizeof(label), "0x%8.8x##%8.8x", addr, m_currentAddr);
429429
std::string longLabel = label;
430-
auto symbols = findSymbol(addr);
431-
if (symbols.size() != 0) longLabel = *symbols.begin() + " ;" + label;
430+
std::string* symbol = g_emulator->m_cpu->getSymbolAt(addr);
431+
if (symbol) longLabel = *symbol + " ;" + label;
432432

433433
const auto& io = ImGui::GetIO();
434434
unsigned targetEditorIndex = io.KeyShift ? 1 : (io.KeyCtrl ? 2 : 0);
@@ -680,20 +680,21 @@ settings, otherwise debugging features may not work.)");
680680
tcode >>= 8;
681681
b[3] = tcode & 0xff;
682682

683-
auto symbols = findSymbol(dispAddr);
684-
if (symbols.size() != 0) {
685-
ImGui::PushStyleColor(ImGuiCol_Text, s_labelColor);
686-
ImGui::Text("%s:", symbols.begin()->c_str());
687-
ImGui::PopStyleColor();
688-
} else {
689-
// if the first visible line is not a symbol, show the previous symbol
690-
float y = ImGui::GetCursorScreenPos().y;
691-
if (y + lineHeight >= topleft.y && y <= topleft.y + lineHeight) {
692-
previousSymbol = findPreviousSymbol(dispAddr);
693-
// if the second line is a symbol, push the previous symbol display up
694-
auto secondLineSymbol = findSymbol(dispAddr + 4);
695-
if (secondLineSymbol.size() != 0 && y < previousSymbolY) {
696-
previousSymbolY = y;
683+
std::pair<const uint32_t, std::string>* symbol = cpu->findContainingSymbol(dispAddr);
684+
if (symbol) {
685+
if (symbol->first == dispAddr) {
686+
ImGui::PushStyleColor(ImGuiCol_Text, s_labelColor);
687+
ImGui::Text("%s:", symbol->second.c_str());
688+
ImGui::PopStyleColor();
689+
} else {
690+
// if this is the first visible line and it's not a label itself, store the previous symbol
691+
float y = ImGui::GetCursorScreenPos().y;
692+
if (y + lineHeight >= topleft.y && y <= topleft.y + lineHeight) {
693+
previousSymbol = symbol->second;
694+
// if the second visible line is a symbol, push the previous symbol display up
695+
if (cpu->getSymbolAt(dispAddr + 4) && y < previousSymbolY) {
696+
previousSymbolY = y;
697+
}
697698
}
698699
}
699700
}
@@ -765,15 +766,15 @@ settings, otherwise debugging features may not work.)");
765766
}
766767
std::string contextMenuID = fmt::format("assembly address menu {}", dispAddr);
767768
if (ImGui::BeginPopupContextItem(contextMenuID.c_str())) {
768-
if (symbols.size() == 0) {
769+
if (symbol) {
770+
if (ImGui::MenuItem(_("Remove symbol"))) {
771+
cpu->m_symbols.erase(dispAddr);
772+
}
773+
} else {
769774
if (ImGui::MenuItem(_("Create symbol here"))) {
770775
openSymbolAdder = true;
771776
m_symbolAddress = dispAddr;
772777
}
773-
} else {
774-
if (ImGui::MenuItem(_("Remove symbol"))) {
775-
cpu->m_symbols.erase(dispAddr);
776-
}
777778
}
778779
if (ImGui::MenuItem(_("Copy Address"))) {
779780
char fmtAddr[10];
@@ -1100,7 +1101,7 @@ if not success then return msg else return nil end
11001101

11011102
if (m_showSymbols) {
11021103
if (ImGui::Begin(_("Symbols"), &m_showSymbols)) {
1103-
if (ImGui::Button(_("Refresh"))) rebuildSymbolsCaches();
1104+
if (ImGui::Button(_("Refresh"))) rebuildSymbolsCache();
11041105
ImGui::SameLine();
11051106
ImGui::InputText(_("Filter"), &m_symbolFilter);
11061107
ImGui::BeginChild("symbolsList");
@@ -1136,36 +1137,11 @@ if not success then return msg else return nil end
11361137
return changed;
11371138
}
11381139

1139-
std::list<std::string> PCSX::Widgets::Assembly::findSymbol(uint32_t addr) {
1140-
auto& cpu = g_emulator->m_cpu;
1141-
std::list<std::string> ret;
1142-
auto symbol = cpu->m_symbols.find(addr);
1143-
if (symbol != cpu->m_symbols.end()) ret.emplace_back(symbol->second);
1144-
1145-
if (!m_symbolsCachesValid) rebuildSymbolsCaches();
1146-
auto elfSymbol = m_elfSymbolsCache.find(addr);
1147-
if (elfSymbol != m_elfSymbolsCache.end()) ret.emplace_back(elfSymbol->second);
1148-
1149-
return ret;
1150-
}
1151-
1152-
std::optional<std::string> PCSX::Widgets::Assembly::findPreviousSymbol(uint32_t addr) {
1153-
auto& symbols = g_emulator->m_cpu->m_symbols;
1154-
auto symBeforeAddr = symbols.lower_bound(addr);
1155-
if (symBeforeAddr != symbols.begin()) { // verify there is actually a symbol before addr
1156-
symBeforeAddr--;
1157-
if (symBeforeAddr->first < addr && symBeforeAddr->first >= m_ramBase) {
1158-
return symBeforeAddr->second;
1159-
}
1160-
}
1161-
return {};
1162-
}
1163-
1164-
void PCSX::Widgets::Assembly::rebuildSymbolsCaches() {
1140+
void PCSX::Widgets::Assembly::rebuildSymbolsCache() {
11651141
auto& cpu = g_emulator->m_cpu;
11661142
m_symbolsCache.clear();
11671143
for (auto& symbol : cpu->m_symbols) {
11681144
m_symbolsCache.insert(std::pair(symbol.second, symbol.first));
11691145
}
1170-
m_symbolsCachesValid = true;
1146+
m_symbolsCacheValid = true;
11711147
}

src/gui/widgets/assembly.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,10 @@ class Assembly : private Disasm {
110110
uint32_t addr;
111111
};
112112

113-
std::list<std::string> findSymbol(uint32_t addr);
114-
std::optional<std::string> findPreviousSymbol(uint32_t addr);
115113
std::map<std::string, uint32_t> m_symbolsCache;
116-
std::map<uint32_t, std::string> m_elfSymbolsCache;
117-
bool m_symbolsCachesValid = false;
114+
bool m_symbolsCacheValid = false;
118115

119-
void rebuildSymbolsCaches();
116+
void rebuildSymbolsCache();
120117
void addMemoryEditorContext(uint32_t addr, int size);
121118
void addMemoryEditorSubMenu(uint32_t addr, int size);
122119

0 commit comments

Comments
 (0)