From 7eef2b5b46a554838be124dd8f8e87b348d22ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:32:56 +0200 Subject: [PATCH] Add getter and setter for label names --- zasm/include/zasm/program/program.hpp | 14 ++++++++ zasm/src/zasm/src/program/program.cpp | 48 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/zasm/include/zasm/program/program.hpp b/zasm/include/zasm/program/program.hpp index e9c4931..6710437 100644 --- a/zasm/include/zasm/program/program.hpp +++ b/zasm/include/zasm/program/program.hpp @@ -263,6 +263,20 @@ namespace zasm /// Node for which the label is bound to or null if the label is invalid or not bound Node* getNodeForLabel(const Label& label); + /// + /// Returns the current assigned label name, returns nullptr if the label is invalid or has no name. + /// + /// Label + /// Label name or nullptr + const char* getLabelName(const Label& label) const noexcept; + + /// + /// Assigns a name to a label. + /// + /// Label + /// The new name, pasing nullptr will clear the name, the string will be copied + void setLabelName(const Label& label, const char* name); + public: /// /// Creates a new section that can be used to segment code and data. diff --git a/zasm/src/zasm/src/program/program.cpp b/zasm/src/zasm/src/program/program.cpp index 031cec9..ddcb651 100644 --- a/zasm/src/zasm/src/program/program.cpp +++ b/zasm/src/zasm/src/program/program.cpp @@ -110,6 +110,54 @@ namespace zasm return entry.node; } + const char* Program::getLabelName(const Label& label) const noexcept + { + if (!label.isValid()) + { + return nullptr; + } + + const auto entryIdx = static_cast(label.getId()); + if (entryIdx >= _state->labels.size()) + { + return nullptr; + } + + auto& entry = _state->labels[entryIdx]; + if (entry.nameId != StringPool::Id::Invalid) + { + return _state->symbolNames.get(entry.nameId); + } + + return nullptr; + } + + void Program::setLabelName(const Label& label, const char* name) + { + if (!label.isValid()) + { + return; + } + + const auto entryIdx = static_cast(label.getId()); + if (entryIdx >= _state->labels.size()) + { + return; + } + + auto& entry = _state->labels[entryIdx]; + if (entry.nameId != StringPool::Id::Invalid) + { + _state->symbolNames.release(entry.nameId); + entry.nameId = StringPool::Id::Invalid; + } + + if (name != nullptr) + { + entry.nameId = _state->symbolNames.aquire(name); + } + } + Node* Program::getNodeForSection(const Section& section) { if (!section.isValid())