openMSX
SymbolManager.hh
Go to the documentation of this file.
1#ifndef SYMBOL_MANAGER_HH
2#define SYMBOL_MANAGER_HH
3
4#include "function_ref.hh"
5#include "hash_map.hh"
6#include "zstring_view.hh"
7
8#include <cassert>
9#include <cstdint>
10#include <optional>
11#include <span>
12#include <string>
13#include <string_view>
14#include <vector>
15
16namespace openmsx {
17
18class CommandController;
19
20struct Symbol
21{
22 Symbol(std::string n, uint16_t v, std::optional<uint8_t> s1, std::optional<uint16_t> s2)
23 : name(std::move(n)), value(v), slot(s1), segment(s2) {} // clang-15 workaround
24
25 std::string name;
26 uint16_t value;
27 std::optional<uint8_t> slot;
28 std::optional<uint16_t> segment;
29
30 auto operator<=>(const Symbol&) const = default;
31};
32
34{
35 enum class Type {
36 AUTO_DETECT = 0,
37 ASMSX,
38 GENERIC, // includes PASMO, SJASM, TNIASM0, TNIASM1
39 HTC,
40 LINKMAP,
41 NOICE,
42 VASM,
44
46 LAST = VASM + 1,
47 };
48 [[nodiscard]] static zstring_view toString(Type type);
49 [[nodiscard]] static std::optional<Type> parseType(std::string_view str);
50 [[nodiscard]] auto& getSymbols() { return symbols; }
51
52 std::optional<uint8_t> slot;
53 std::string filename;
54 std::vector<Symbol> symbols;
56};
57
59{
60 virtual void notifySymbolsChanged() = 0;
61};
62
64{
65public:
66 explicit SymbolManager(CommandController& commandController);
67
68 void setObserver(SymbolObserver* observer_) {
69 assert(!observer || !observer_);
70 observer = observer_;
71 }
72
73 // Load or reload a file.
74 // * Throws on failure to read from file. In that case the existing file is left unchanged.
75 // * Parse errors in the file are ignored.
76 // * When the file contains no symbols and when 'allowEmpty=false' the
77 // existing file is not replaced and this method returns 'false'.
78 // Otherwise it return 'true'.
79 enum class LoadEmpty { ALLOWED, NOT_ALLOWED };
80 bool reloadFile(const std::string& filename, LoadEmpty loadEmpty, SymbolFile::Type type, std::optional<uint8_t> slot = {});
81
82 void removeFile(std::string_view filename);
83 void removeAllFiles();
84
85 [[nodiscard]] const auto& getFiles() const { return files; }
86 [[nodiscard]] SymbolFile* findFile(std::string_view filename);
87 [[nodiscard]] std::span<Symbol const * const> lookupValue(uint16_t value);
88 [[nodiscard]] std::optional<uint16_t> parseSymbolOrValue(std::string_view s) const;
89
90 [[nodiscard]] static std::string getFileFilters();
91 [[nodiscard]] static SymbolFile::Type getTypeForFilter(std::string_view filter);
92
93//private:
94 // These should not be called directly, except by the unittest
95 [[nodiscard]] static std::optional<unsigned> isHexDigit(char c);
96 [[nodiscard]] static std::optional<uint16_t> is4DigitHex(std::string_view s);
97 template <typename T>
98 [[nodiscard]] static std::optional<T> parseValue(std::string_view str);
99 [[nodiscard]] static std::optional<Symbol> checkLabel(std::string_view label, uint32_t value);
100 [[nodiscard]] static std::optional<Symbol> checkLabelAndValue(std::string_view label, std::string_view value);
101 [[nodiscard]] static std::optional<Symbol> checkLabelSegmentAndValue(std::string_view label, std::string_view value);
102 [[nodiscard]] static SymbolFile::Type detectType(std::string_view filename, std::string_view buffer);
103 [[nodiscard]] static SymbolFile loadLines(
104 std::string_view filename, std::string_view buffer, SymbolFile::Type type,
105 function_ref<std::optional<Symbol>(std::span<std::string_view>)> lineParser);
106 [[nodiscard]] static SymbolFile loadGeneric(std::string_view filename, std::string_view buffer);
107 [[nodiscard]] static SymbolFile loadNoICE(std::string_view filename, std::string_view buffer);
108 [[nodiscard]] static SymbolFile loadHTC(std::string_view filename, std::string_view buffer);
109 [[nodiscard]] static SymbolFile loadVASM(std::string_view filename, std::string_view buffer);
110 [[nodiscard]] static SymbolFile loadNoGmb(std::string_view filename, std::string_view buffer);
111 [[nodiscard]] static SymbolFile loadASMSX(std::string_view filename, std::string_view buffer);
112 [[nodiscard]] static SymbolFile loadLinkMap(std::string_view filename, std::string_view buffer);
113 [[nodiscard]] static SymbolFile loadSymbolFile(const std::string& filename, SymbolFile::Type type, std::optional<uint8_t> slot = {});
114
115private:
116 void refresh();
117
118private:
119 CommandController& commandController;
120 SymbolObserver* observer = nullptr; // only one for now, could become a vector later
121 std::vector<SymbolFile> files;
122 hash_map<uint16_t, std::vector<const Symbol*>> lookupValueCache; // calculated from 'files'
123};
124
125
126} // namespace openmsx
127
128#endif
const auto & getFiles() const
static std::optional< unsigned > isHexDigit(char c)
static SymbolFile loadLines(std::string_view filename, std::string_view buffer, SymbolFile::Type type, function_ref< std::optional< Symbol >(std::span< std::string_view >)> lineParser)
static std::optional< Symbol > checkLabelSegmentAndValue(std::string_view label, std::string_view value)
static SymbolFile loadNoGmb(std::string_view filename, std::string_view buffer)
std::span< Symbol const *const > lookupValue(uint16_t value)
static SymbolFile::Type detectType(std::string_view filename, std::string_view buffer)
static SymbolFile loadNoICE(std::string_view filename, std::string_view buffer)
static SymbolFile loadHTC(std::string_view filename, std::string_view buffer)
static SymbolFile loadGeneric(std::string_view filename, std::string_view buffer)
static SymbolFile loadSymbolFile(const std::string &filename, SymbolFile::Type type, std::optional< uint8_t > slot={})
static SymbolFile loadVASM(std::string_view filename, std::string_view buffer)
static std::string getFileFilters()
static std::optional< uint16_t > is4DigitHex(std::string_view s)
void removeFile(std::string_view filename)
static std::optional< Symbol > checkLabel(std::string_view label, uint32_t value)
bool reloadFile(const std::string &filename, LoadEmpty loadEmpty, SymbolFile::Type type, std::optional< uint8_t > slot={})
static SymbolFile loadASMSX(std::string_view filename, std::string_view buffer)
std::optional< uint16_t > parseSymbolOrValue(std::string_view s) const
static SymbolFile loadLinkMap(std::string_view filename, std::string_view buffer)
static SymbolFile::Type getTypeForFilter(std::string_view filter)
SymbolFile * findFile(std::string_view filename)
static std::optional< T > parseValue(std::string_view str)
void setObserver(SymbolObserver *observer_)
static std::optional< Symbol > checkLabelAndValue(std::string_view label, std::string_view value)
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
This file implemented 3 utility functions:
Definition Autofire.cc:11
STL namespace.
static std::optional< Type > parseType(std::string_view str)
static zstring_view toString(Type type)
std::vector< Symbol > symbols
std::optional< uint8_t > slot
virtual void notifySymbolsChanged()=0
auto operator<=>(const Symbol &) const =default
std::optional< uint8_t > slot
std::string name
Symbol(std::string n, uint16_t v, std::optional< uint8_t > s1, std::optional< uint16_t > s2)
std::optional< uint16_t > segment