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

DSP/LabelMap: C++17 transitional changes/cleanup #8162

Merged
merged 6 commits into from Jun 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Source/Core/Core/DSP/DSPAssembler.cpp
Expand Up @@ -200,9 +200,9 @@ s32 DSPAssembler::ParseValue(const char* str)
else // Everything else is a label.
{
// Lookup label
u16 value;
if (m_labels.GetLabelValue(ptr, &value))
return value;
if (const std::optional<u16> value = m_labels.GetLabelValue(ptr))
return *value;

if (m_cur_pass == 2)
ShowError(AssemblerError::UnknownLabel, str);
}
Expand Down
57 changes: 34 additions & 23 deletions Source/Core/Core/DSP/LabelMap.cpp
Expand Up @@ -4,16 +4,28 @@

#include "Core/DSP/LabelMap.h"

#include <algorithm>
#include <string>
#include <vector>

#include "Core/DSP/DSPTables.h"

namespace DSP
{
LabelMap::LabelMap()
struct LabelMap::Label
{
}
Label(std::string lbl, s32 address, LabelType ltype)
: name(std::move(lbl)), addr(address), type(ltype)
{
}
std::string name;
s32 addr;
LabelType type;
};

LabelMap::LabelMap() = default;

LabelMap::~LabelMap() = default;

void LabelMap::RegisterDefaults()
{
Expand All @@ -30,48 +42,47 @@ void LabelMap::RegisterDefaults()
}
}

void LabelMap::RegisterLabel(const std::string& label, u16 lval, LabelType type)
void LabelMap::RegisterLabel(std::string label, u16 lval, LabelType type)
{
u16 old_value;
if (GetLabelValue(label, &old_value) && old_value != lval)
const std::optional<u16> old_value = GetLabelValue(label);
if (old_value && old_value != lval)
{
printf("WARNING: Redefined label %s to %04x - old value %04x\n", label.c_str(), lval,
old_value);
*old_value);
DeleteLabel(label);
}
labels.emplace_back(label, lval, type);
labels.emplace_back(std::move(label), lval, type);
}

void LabelMap::DeleteLabel(const std::string& label)
void LabelMap::DeleteLabel(std::string_view label)
{
for (std::vector<label_t>::iterator iter = labels.begin(); iter != labels.end(); ++iter)
{
if (!label.compare(iter->name))
{
labels.erase(iter);
return;
}
}
const auto iter = std::find_if(labels.cbegin(), labels.cend(),
[&label](const auto& entry) { return entry.name == label; });

if (iter == labels.cend())
return;

labels.erase(iter);
}

bool LabelMap::GetLabelValue(const std::string& name, u16* value, LabelType type) const
std::optional<u16> LabelMap::GetLabelValue(const std::string& name, LabelType type) const
{
for (auto& label : labels)
for (const auto& label : labels)
{
if (!name.compare(label.name))
if (name == label.name)
{
if (type & label.type)
if ((type & label.type) != 0)
{
*value = label.addr;
return true;
return label.addr;
}
else
{
printf("WARNING: Wrong label type requested. %s\n", name.c_str());
}
}
}
return false;

return std::nullopt;
}

void LabelMap::Clear()
Expand Down
27 changes: 11 additions & 16 deletions Source/Core/Core/DSP/LabelMap.h
Expand Up @@ -4,7 +4,9 @@

#pragma once

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "Common/CommonTypes.h"
Expand All @@ -21,25 +23,18 @@ enum LabelType

class LabelMap
{
struct label_t
{
label_t(const std::string& lbl, s32 address, LabelType ltype)
: name(lbl), addr(address), type(ltype)
{
}
std::string name;
s32 addr;
LabelType type;
};
std::vector<label_t> labels;

public:
LabelMap();
~LabelMap() {}
~LabelMap();

void RegisterDefaults();
void RegisterLabel(const std::string& label, u16 lval, LabelType type = LABEL_VALUE);
void DeleteLabel(const std::string& label);
bool GetLabelValue(const std::string& label, u16* value, LabelType type = LABEL_ANY) const;
void RegisterLabel(std::string label, u16 lval, LabelType type = LABEL_VALUE);
void DeleteLabel(std::string_view label);
std::optional<u16> GetLabelValue(const std::string& label, LabelType type = LABEL_ANY) const;
void Clear();

private:
struct Label;
std::vector<Label> labels;
};
} // namespace DSP