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,
43
45 LAST = VASM + 1,
46 };
47 [[nodiscard]] static zstring_view toString(Type type);
48 [[nodiscard]] static std::optional<Type> parseType(std::string_view str);
49 [[nodiscard]] auto& getSymbols() { return symbols; }
50
51 std::optional<uint8_t> slot;
52 std::string filename;
53 std::vector<Symbol> symbols;
55};
56
58{
59 virtual void notifySymbolsChanged() = 0;
60};
61
63{
64public:
65 explicit SymbolManager(CommandController& commandController);
66
67 void setObserver(SymbolObserver* observer_) {
68 assert(!observer || !observer_);
69 observer = observer_;
70 }
71
72 // Load or reload a file.
73 // * Throws on failure to read from file. In that case the existing file is left unchanged.
74 // * Parse errors in the file are ignored.
75 // * When the file contains no symbols and when 'allowEmpty=false' the
76 // existing file is not replaced and this method returns 'false'.
77 // Otherwise it return 'true'.
78 enum class LoadEmpty { ALLOWED, NOT_ALLOWED };
79 bool reloadFile(const std::string& filename, LoadEmpty loadEmpty, SymbolFile::Type type, std::optional<uint8_t> slot = {});
80
81 void removeFile(std::string_view filename);
82 void removeAllFiles();
83
84 [[nodiscard]] const auto& getFiles() const { return files; }
85 [[nodiscard]] SymbolFile* findFile(std::string_view filename);
86 [[nodiscard]] std::span<Symbol const * const> lookupValue(uint16_t value);
87 [[nodiscard]] std::optional<uint16_t> parseSymbolOrValue(std::string_view s) const;
88
89 [[nodiscard]] static std::string getFileFilters();
90 [[nodiscard]] static SymbolFile::Type getTypeForFilter(std::string_view filter);
91
92//private:
93 // These should not be called directly, except by the unittest
94 [[nodiscard]] static std::optional<unsigned> isHexDigit(char c);
95 [[nodiscard]] static std::optional<uint16_t> is4DigitHex(std::string_view s);
96 template <typename T>
97 [[nodiscard]] static std::optional<T> parseValue(std::string_view str);
98 [[nodiscard]] static std::optional<Symbol> checkLabel(std::string_view label, uint32_t value);
99 [[nodiscard]] static std::optional<Symbol> checkLabelAndValue(std::string_view label, std::string_view value);
100 [[nodiscard]] static std::optional<Symbol> checkLabelSegmentAndValue(std::string_view label, std::string_view value);
101 [[nodiscard]] static SymbolFile::Type detectType(std::string_view filename, std::string_view buffer);
102 [[nodiscard]] static SymbolFile loadLines(
103 std::string_view filename, std::string_view buffer, SymbolFile::Type type,
104 function_ref<std::optional<Symbol>(std::span<std::string_view>)> lineParser);
105 [[nodiscard]] static SymbolFile loadGeneric(std::string_view filename, std::string_view buffer);
106 [[nodiscard]] static SymbolFile loadNoICE(std::string_view filename, std::string_view buffer);
107 [[nodiscard]] static SymbolFile loadHTC(std::string_view filename, std::string_view buffer);
108 [[nodiscard]] static SymbolFile loadVASM(std::string_view filename, std::string_view buffer);
109 [[nodiscard]] static SymbolFile loadASMSX(std::string_view filename, std::string_view buffer);
110 [[nodiscard]] static SymbolFile loadLinkMap(std::string_view filename, std::string_view buffer);
111 [[nodiscard]] static SymbolFile loadSymbolFile(const std::string& filename, SymbolFile::Type type, std::optional<uint8_t> slot = {});
112
113private:
114 void refresh();
115
116private:
117 CommandController& commandController;
118 SymbolObserver* observer = nullptr; // only one for now, could become a vector later
119 std::vector<SymbolFile> files;
120 hash_map<uint16_t, std::vector<const Symbol*>> lookupValueCache; // calculated from 'files'
121};
122
123
124} // namespace openmsx
125
126#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)
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