Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter and setter for label names #147

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions zasm/include/zasm/program/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ namespace zasm
/// <returns>Node for which the label is bound to or null if the label is invalid or not bound</returns>
Node* getNodeForLabel(const Label& label);

/// <summary>
/// Returns the current assigned label name, returns nullptr if the label is invalid or has no name.
/// </summary>
/// <param name="label">Label</param>
/// <returns>Label name or nullptr</returns>
const char* getLabelName(const Label& label) const noexcept;

/// <summary>
/// Assigns a name to a label.
/// </summary>
/// <param name="label">Label</param>
/// <param name="name">The new name, pasing nullptr will clear the name, the string will be copied</param>
void setLabelName(const Label& label, const char* name);

public:
/// <summary>
/// Creates a new section that can be used to segment code and data.
Expand Down
48 changes: 48 additions & 0 deletions zasm/src/zasm/src/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(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<std::size_t>(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())
Expand Down
Loading