Skip to content

Commit

Permalink
Add getter and setter for label names
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Nov 3, 2024
1 parent aa852f2 commit 7eef2b5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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

0 comments on commit 7eef2b5

Please sign in to comment.