Skip to content

Commit ee60f32

Browse files
committed
Add getter and setter for label names
1 parent aa852f2 commit ee60f32

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

zasm/include/zasm/program/program.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ namespace zasm
263263
/// <returns>Node for which the label is bound to or null if the label is invalid or not bound</returns>
264264
Node* getNodeForLabel(const Label& label);
265265

266+
/// <summary>
267+
/// Returns the current assigned label name, returns nullptr if the label is invalid or has no name.
268+
/// </summary>
269+
/// <param name="label">Label</param>
270+
/// <returns>Label name or nullptr</returns>
271+
const char* getLabelName(const Label& label) const noexcept;
272+
273+
/// <summary>
274+
/// Assigns a name to a label.
275+
/// </summary>
276+
/// <param name="label">Label</param>
277+
/// <param name="name">The new name, pasing nullptr will clear the name</param>
278+
void setLabelName(const Label& label, const char* name);
279+
266280
public:
267281
/// <summary>
268282
/// Creates a new section that can be used to segment code and data.

zasm/src/zasm/src/program/program.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,54 @@ namespace zasm
110110
return entry.node;
111111
}
112112

113+
const char* Program::getLabelName(const Label& label) const noexcept
114+
{
115+
if (!label.isValid())
116+
{
117+
return nullptr;
118+
}
119+
120+
const auto entryIdx = static_cast<std::size_t>(label.getId());
121+
if (entryIdx >= _state->labels.size())
122+
{
123+
return nullptr;
124+
}
125+
126+
auto& entry = _state->labels[entryIdx];
127+
if (entry.nameId != StringPool::Id::Invalid)
128+
{
129+
return _state->symbolNames.get(entry.nameId);
130+
}
131+
132+
return nullptr;
133+
}
134+
135+
void Program::setLabelName(const Label& label, const char* name)
136+
{
137+
if (!label.isValid())
138+
{
139+
return;
140+
}
141+
142+
const auto entryIdx = static_cast<std::size_t>(label.getId());
143+
if (entryIdx >= _state->labels.size())
144+
{
145+
return;
146+
}
147+
148+
auto& entry = _state->labels[entryIdx];
149+
if (entry.nameId != StringPool::Id::Invalid)
150+
{
151+
_state->symbolNames.release(entry.nameId);
152+
entry.nameId = StringPool::Id::Invalid;
153+
}
154+
155+
if (name != nullptr)
156+
{
157+
entry.nameId = _state->symbolNames.aquire(name);
158+
}
159+
}
160+
113161
Node* Program::getNodeForSection(const Section& section)
114162
{
115163
if (!section.isValid())

0 commit comments

Comments
 (0)