mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Use std::get_if instead of std::visit (#1367)
`std::visit` is (arguably) cleaner code, but older versions of gcc and clang (not very old; the ones packaged with Ubuntu 22.04 LTS) compile them as tables of function pointers, instead of efficient jump tables.
This commit is contained in:
@@ -53,12 +53,8 @@ public:
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
std::streambuf &operator*() {
|
std::streambuf &operator*() {
|
||||||
return std::visit(
|
auto *file = std::get_if<std::filebuf>(&_file);
|
||||||
Visitor{
|
return file ? *file : *std::get<std::streambuf *>(_file);
|
||||||
[](std::filebuf &file) -> std::streambuf & { return file; },
|
|
||||||
[](std::streambuf *buf) -> std::streambuf & { return *buf; }},
|
|
||||||
_file
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
std::streambuf const &operator*() const {
|
std::streambuf const &operator*() const {
|
||||||
// The non-`const` version does not perform any modifications, so it's okay.
|
// The non-`const` version does not perform any modifications, so it's okay.
|
||||||
@@ -71,32 +67,22 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
File *close() {
|
File *close() {
|
||||||
return std::visit(
|
if (auto *file = std::get_if<std::filebuf>(&_file); file) {
|
||||||
Visitor{
|
// This is called by the destructor, and an explicit `close` shouldn't close twice.
|
||||||
[this](std::filebuf &file) {
|
|
||||||
// This is called by the destructor, and an explicit `close`
|
|
||||||
// shouldn't close twice.
|
|
||||||
_file.emplace<std::streambuf *>(nullptr);
|
_file.emplace<std::streambuf *>(nullptr);
|
||||||
return file.close() != nullptr;
|
if (file->close() != nullptr) {
|
||||||
},
|
return this;
|
||||||
[](std::streambuf *buf) { return buf != nullptr; },
|
}
|
||||||
},
|
} else if (std::get<std::streambuf *>(_file) != nullptr) {
|
||||||
_file
|
return this;
|
||||||
)
|
}
|
||||||
? this
|
return nullptr;
|
||||||
: nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char const *c_str(std::string const &path) const {
|
char const *c_str(std::string const &path) const {
|
||||||
return std::visit(
|
return std::holds_alternative<std::filebuf>(_file) ? path.c_str()
|
||||||
Visitor{
|
: std::get<std::streambuf *>(_file) == std::cin.rdbuf() ? "<stdin>"
|
||||||
[&path](std::filebuf const &) { return path.c_str(); },
|
: "<stdout>";
|
||||||
[](std::streambuf const *buf) {
|
|
||||||
return buf == std::cin.rdbuf() ? "<stdin>" : "<stdout>";
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_file
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -62,28 +62,27 @@ std::string const &FileStackNode::name() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||||
Visitor visitor{
|
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
||||||
[this](std::vector<uint32_t> const &iters) -> std::string const & {
|
assert(parent); // REPT nodes use their parent's name
|
||||||
assert(this->parent); // REPT nodes use their parent's name
|
std::string const &lastName = parent->dump(lineNo);
|
||||||
std::string const &lastName = this->parent->dump(this->lineNo);
|
fputs(" -> ", stderr);
|
||||||
fprintf(stderr, " -> %s", lastName.c_str());
|
fputs(lastName.c_str(), stderr);
|
||||||
for (uint32_t i = iters.size(); i--;)
|
std::vector<uint32_t> const &nodeIters = iters();
|
||||||
fprintf(stderr, "::REPT~%" PRIu32, iters[i]);
|
for (uint32_t i = nodeIters.size(); i--;) {
|
||||||
return lastName;
|
fprintf(stderr, "::REPT~%" PRIu32, nodeIters[i]);
|
||||||
},
|
|
||||||
[this](std::string const &name) -> std::string const & {
|
|
||||||
if (this->parent) {
|
|
||||||
this->parent->dump(this->lineNo);
|
|
||||||
fprintf(stderr, " -> %s", name.c_str());
|
|
||||||
} else {
|
|
||||||
fputs(name.c_str(), stderr);
|
|
||||||
}
|
}
|
||||||
return name;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
std::string const &topName = std::visit(visitor, data);
|
|
||||||
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
||||||
return topName;
|
return lastName;
|
||||||
|
} else {
|
||||||
|
if (parent) {
|
||||||
|
parent->dump(lineNo);
|
||||||
|
fputs(" -> ", stderr);
|
||||||
|
}
|
||||||
|
std::string const &nodeName = name();
|
||||||
|
fputs(nodeName.c_str(), stderr);
|
||||||
|
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
||||||
|
return nodeName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fstk_DumpCurrent() {
|
void fstk_DumpCurrent() {
|
||||||
|
|||||||
@@ -449,14 +449,11 @@ void lexer_OpenFileView(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lexer_RestartRept(uint32_t lineNo) {
|
void lexer_RestartRept(uint32_t lineNo) {
|
||||||
std::visit(
|
if (auto *mmap = std::get_if<MmappedLexerState>(&lexerState->content); mmap) {
|
||||||
Visitor{
|
mmap->offset = 0;
|
||||||
[](MmappedLexerState &mmap) { mmap.offset = 0; },
|
} else if (auto *view = std::get_if<ViewedLexerState>(&lexerState->content); view) {
|
||||||
[](ViewedLexerState &view) { view.offset = 0; },
|
view->offset = 0;
|
||||||
[](auto &) {},
|
}
|
||||||
},
|
|
||||||
lexerState->content
|
|
||||||
);
|
|
||||||
initState(*lexerState);
|
initState(*lexerState);
|
||||||
lexerState->lineNo = lineNo;
|
lexerState->lineNo = lineNo;
|
||||||
}
|
}
|
||||||
@@ -475,17 +472,12 @@ void lexer_CleanupState(LexerState &state) {
|
|||||||
// `lexerStateEOL`, but there's currently no situation in which this should happen.
|
// `lexerStateEOL`, but there's currently no situation in which this should happen.
|
||||||
assert(&state != lexerStateEOL);
|
assert(&state != lexerStateEOL);
|
||||||
|
|
||||||
std::visit(
|
if (auto *mmap = std::get_if<MmappedLexerState>(&state.content); mmap) {
|
||||||
Visitor{
|
if (!mmap->isReferenced)
|
||||||
[](MmappedLexerState &mmap) {
|
munmap(mmap->ptr, mmap->size);
|
||||||
if (!mmap.isReferenced)
|
} else if (auto *cbuf = std::get_if<BufferedLexerState>(&state.content); cbuf) {
|
||||||
munmap(mmap.ptr, mmap.size);
|
close(cbuf->fd);
|
||||||
},
|
}
|
||||||
[](BufferedLexerState &cbuf) { close(cbuf.fd); },
|
|
||||||
[](auto &) {},
|
|
||||||
},
|
|
||||||
state.content
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lexer_SetMode(LexerMode mode) {
|
void lexer_SetMode(LexerMode mode) {
|
||||||
@@ -654,19 +646,18 @@ static int peekInternal(uint8_t distance) {
|
|||||||
LEXER_BUF_SIZE
|
LEXER_BUF_SIZE
|
||||||
);
|
);
|
||||||
|
|
||||||
return std::visit(
|
if (auto *mmap = std::get_if<MmappedLexerState>(&lexerState->content); mmap) {
|
||||||
Visitor{
|
if (size_t idx = mmap->offset + distance; idx < mmap->size)
|
||||||
[&distance](MmappedLexerState &mmap) -> int {
|
return (uint8_t)mmap->ptr[idx];
|
||||||
if (size_t idx = mmap.offset + distance; idx < mmap.size)
|
|
||||||
return (uint8_t)mmap.ptr[idx];
|
|
||||||
return EOF;
|
return EOF;
|
||||||
},
|
} else if (auto *view = std::get_if<ViewedLexerState>(&lexerState->content); view) {
|
||||||
[&distance](ViewedLexerState &view) -> int {
|
if (size_t idx = view->offset + distance; idx < view->size)
|
||||||
if (size_t idx = view.offset + distance; idx < view.size)
|
return (uint8_t)view->ptr[idx];
|
||||||
return (uint8_t)view.ptr[idx];
|
|
||||||
return EOF;
|
return EOF;
|
||||||
},
|
} else {
|
||||||
[&distance](BufferedLexerState &cbuf) -> int {
|
assert(std::holds_alternative<BufferedLexerState>(lexerState->content));
|
||||||
|
auto &cbuf = std::get<BufferedLexerState>(lexerState->content);
|
||||||
|
|
||||||
if (cbuf.nbChars > distance)
|
if (cbuf.nbChars > distance)
|
||||||
return (uint8_t)cbuf.buf[(cbuf.index + distance) % LEXER_BUF_SIZE];
|
return (uint8_t)cbuf.buf[(cbuf.index + distance) % LEXER_BUF_SIZE];
|
||||||
|
|
||||||
@@ -700,11 +691,7 @@ static int peekInternal(uint8_t distance) {
|
|||||||
|
|
||||||
// If there aren't enough chars even after refilling, give up
|
// If there aren't enough chars even after refilling, give up
|
||||||
return EOF;
|
return EOF;
|
||||||
},
|
}
|
||||||
[](std::monostate) -> int { return EOF; },
|
|
||||||
},
|
|
||||||
lexerState->content
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// forward declarations for peek
|
// forward declarations for peek
|
||||||
@@ -782,22 +769,20 @@ restart:
|
|||||||
} else {
|
} else {
|
||||||
// Advance within the file contents
|
// Advance within the file contents
|
||||||
lexerState->colNo++;
|
lexerState->colNo++;
|
||||||
std::visit(
|
if (auto *mmap = std::get_if<MmappedLexerState>(&lexerState->content); mmap) {
|
||||||
Visitor{
|
mmap->offset++;
|
||||||
[](MmappedLexerState &mmap) { mmap.offset++; },
|
} else if (auto *view = std::get_if<ViewedLexerState>(&lexerState->content); view) {
|
||||||
[](ViewedLexerState &view) { view.offset++; },
|
view->offset++;
|
||||||
[](BufferedLexerState &cbuf) {
|
} else {
|
||||||
|
assert(std::holds_alternative<BufferedLexerState>(lexerState->content));
|
||||||
|
auto &cbuf = std::get<BufferedLexerState>(lexerState->content);
|
||||||
assert(cbuf.index < LEXER_BUF_SIZE);
|
assert(cbuf.index < LEXER_BUF_SIZE);
|
||||||
cbuf.index++;
|
cbuf.index++;
|
||||||
if (cbuf.index == LEXER_BUF_SIZE)
|
if (cbuf.index == LEXER_BUF_SIZE)
|
||||||
cbuf.index = 0; // Wrap around if necessary
|
cbuf.index = 0; // Wrap around if necessary
|
||||||
assert(cbuf.nbChars > 0);
|
assert(cbuf.nbChars > 0);
|
||||||
cbuf.nbChars--;
|
cbuf.nbChars--;
|
||||||
},
|
}
|
||||||
[](std::monostate) {},
|
|
||||||
},
|
|
||||||
lexerState->content
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2225,13 +2210,16 @@ yy::parser::symbol_type yylex() {
|
|||||||
lexerState->lastToken = token.type;
|
lexerState->lastToken = token.type;
|
||||||
lexerState->atLineStart = token.type == T_(NEWLINE) || token.type == T_(EOB);
|
lexerState->atLineStart = token.type == T_(NEWLINE) || token.type == T_(EOB);
|
||||||
|
|
||||||
return std::visit(
|
if (auto *numValue = std::get_if<uint32_t>(&token.value); numValue) {
|
||||||
Visitor{
|
return yy::parser::symbol_type(token.type, *numValue);
|
||||||
[&token](std::monostate) { return yy::parser::symbol_type(token.type); },
|
} else if (auto *stringValue = std::get_if<String>(&token.value); stringValue) {
|
||||||
[&token](auto &&value) { return yy::parser::symbol_type(token.type, value); },
|
return yy::parser::symbol_type(token.type, *stringValue);
|
||||||
},
|
} else if (auto *strValue = std::get_if<std::string>(&token.value); strValue) {
|
||||||
token.value
|
return yy::parser::symbol_type(token.type, *strValue);
|
||||||
);
|
} else {
|
||||||
|
assert(std::holds_alternative<std::monostate>(token.value));
|
||||||
|
return yy::parser::symbol_type(token.type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void startCapture(CaptureBody &capture) {
|
static void startCapture(CaptureBody &capture) {
|
||||||
@@ -2242,21 +2230,14 @@ static void startCapture(CaptureBody &capture) {
|
|||||||
lexerState->disableInterpolation = true;
|
lexerState->disableInterpolation = true;
|
||||||
|
|
||||||
capture.lineNo = lexer_GetLineNo();
|
capture.lineNo = lexer_GetLineNo();
|
||||||
capture.body = std::visit(
|
if (auto *mmap = std::get_if<MmappedLexerState>(&lexerState->content);
|
||||||
Visitor{
|
mmap && lexerState->expansions.empty()) {
|
||||||
[](MmappedLexerState &mmap) -> char const * {
|
capture.body = &mmap->ptr[mmap->offset];
|
||||||
return lexerState->expansions.empty() ? &mmap.ptr[mmap.offset] : nullptr;
|
} else if (auto *view = std::get_if<ViewedLexerState>(&lexerState->content);
|
||||||
},
|
view && lexerState->expansions.empty()) {
|
||||||
[](ViewedLexerState &view) -> char const * {
|
capture.body = &view->ptr[view->offset];
|
||||||
return lexerState->expansions.empty() ? &view.ptr[view.offset] : nullptr;
|
} else {
|
||||||
},
|
capture.body = nullptr; // Indicates to retrieve the capture buffer when done capturing
|
||||||
[](auto &) -> char const * { return nullptr; },
|
|
||||||
},
|
|
||||||
lexerState->content
|
|
||||||
);
|
|
||||||
|
|
||||||
if (capture.body == nullptr) {
|
|
||||||
// Indicates to retrieve the capture buffer when done capturing
|
|
||||||
assert(lexerState->captureBuf == nullptr);
|
assert(lexerState->captureBuf == nullptr);
|
||||||
lexerState->captureBuf = new (std::nothrow) std::vector<char>();
|
lexerState->captureBuf = new (std::nothrow) std::vector<char>();
|
||||||
if (!lexerState->captureBuf)
|
if (!lexerState->captureBuf)
|
||||||
@@ -2342,7 +2323,7 @@ bool lexer_CaptureMacroBody(CaptureBody &capture) {
|
|||||||
startCapture(capture);
|
startCapture(capture);
|
||||||
|
|
||||||
// If the file is `mmap`ed, we need not to unmap it to keep access to the macro
|
// If the file is `mmap`ed, we need not to unmap it to keep access to the macro
|
||||||
if (MmappedLexerState *mmap = std::get_if<MmappedLexerState>(&lexerState->content); mmap)
|
if (auto *mmap = std::get_if<MmappedLexerState>(&lexerState->content); mmap)
|
||||||
mmap->isReferenced = true;
|
mmap->isReferenced = true;
|
||||||
|
|
||||||
int c = EOF;
|
int c = EOF;
|
||||||
|
|||||||
@@ -2689,14 +2689,12 @@ static std::string strfmt(
|
|||||||
} else if (argIndex >= args.size()) {
|
} else if (argIndex >= args.size()) {
|
||||||
// Will warn after formatting is done.
|
// Will warn after formatting is done.
|
||||||
str += '%';
|
str += '%';
|
||||||
|
} else if (auto *n = std::get_if<uint32_t>(&args[argIndex]); n) {
|
||||||
|
str.append(fmt.formatNumber(*n));
|
||||||
} else {
|
} else {
|
||||||
str.append(std::visit(
|
assert(std::holds_alternative<std::string>(args[argIndex]));
|
||||||
Visitor{
|
auto &s = std::get<std::string>(args[argIndex]);
|
||||||
[&fmt](uint32_t n) { return fmt.formatNumber(n); },
|
str.append(fmt.formatString(s));
|
||||||
[&fmt](std::string const &s) { return fmt.formatString(s); },
|
|
||||||
},
|
|
||||||
args[argIndex]
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
argIndex++;
|
argIndex++;
|
||||||
|
|||||||
@@ -61,21 +61,20 @@ static int32_t CallbackPC() {
|
|||||||
|
|
||||||
int32_t Symbol::getValue() const {
|
int32_t Symbol::getValue() const {
|
||||||
assert(std::holds_alternative<int32_t>(data) || std::holds_alternative<int32_t (*)()>(data));
|
assert(std::holds_alternative<int32_t>(data) || std::holds_alternative<int32_t (*)()>(data));
|
||||||
if (int32_t const *value = std::get_if<int32_t>(&data); value) {
|
if (auto *value = std::get_if<int32_t>(&data); value) {
|
||||||
return type == SYM_LABEL ? *value + getSection()->org : *value;
|
return type == SYM_LABEL ? *value + getSection()->org : *value;
|
||||||
}
|
}
|
||||||
return getOutputValue();
|
return getOutputValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t Symbol::getOutputValue() const {
|
int32_t Symbol::getOutputValue() const {
|
||||||
return std::visit(
|
if (auto *value = std::get_if<int32_t>(&data); value) {
|
||||||
Visitor{
|
return *value;
|
||||||
[](int32_t value) -> int32_t { return value; },
|
} else if (auto *callback = std::get_if<int32_t (*)()>(&data); callback) {
|
||||||
[](int32_t (*callback)()) -> int32_t { return callback(); },
|
return (*callback)();
|
||||||
[](auto &) -> int32_t { return 0; },
|
} else {
|
||||||
},
|
return 0;
|
||||||
data
|
}
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view *Symbol::getMacro() const {
|
std::string_view *Symbol::getMacro() const {
|
||||||
|
|||||||
@@ -66,31 +66,25 @@ std::string const &FileStackNode::name() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||||
Visitor visitor{
|
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
||||||
[this](std::vector<uint32_t> const &iters) -> std::string const & {
|
assert(parent); // REPT nodes use their parent's name
|
||||||
assert(this->parent); // REPT nodes use their parent's name
|
std::string const &lastName = parent->dump(lineNo);
|
||||||
std::string const &lastName = this->parent->dump(this->lineNo);
|
fputs(" -> ", stderr);
|
||||||
fprintf(stderr, " -> %s", lastName.c_str());
|
fputs(lastName.c_str(), stderr);
|
||||||
for (uint32_t iter : iters)
|
for (uint32_t iter : iters())
|
||||||
fprintf(stderr, "::REPT~%" PRIu32, iter);
|
fprintf(stderr, "::REPT~%" PRIu32, iter);
|
||||||
return lastName;
|
|
||||||
},
|
|
||||||
[this](std::string const &name) -> std::string const & {
|
|
||||||
if (this->parent) {
|
|
||||||
this->parent->dump(this->lineNo);
|
|
||||||
fprintf(stderr, " -> %s", name.c_str());
|
|
||||||
} else {
|
|
||||||
fputs(name.c_str(), stderr);
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
},
|
|
||||||
[](std::monostate) -> std::string const & {
|
|
||||||
unreachable_(); // This should not be possible
|
|
||||||
},
|
|
||||||
};
|
|
||||||
std::string const &topName = std::visit(visitor, data);
|
|
||||||
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
||||||
return topName;
|
return lastName;
|
||||||
|
} else {
|
||||||
|
if (parent) {
|
||||||
|
parent->dump(lineNo);
|
||||||
|
fputs(" -> ", stderr);
|
||||||
|
}
|
||||||
|
std::string const &nodeName = name();
|
||||||
|
fputs(nodeName.c_str(), stderr);
|
||||||
|
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
||||||
|
return nodeName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void printDiag(
|
void printDiag(
|
||||||
|
|||||||
@@ -564,7 +564,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
|||||||
|
|
||||||
if (symbol.type == SYMTYPE_EXPORT)
|
if (symbol.type == SYMTYPE_EXPORT)
|
||||||
sym_AddSymbol(symbol);
|
sym_AddSymbol(symbol);
|
||||||
if (Label *label = std::get_if<Label>(&symbol.data); label)
|
if (auto *label = std::get_if<Label>(&symbol.data); label)
|
||||||
nbSymPerSect[label->sectionID]++;
|
nbSymPerSect[label->sectionID]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -603,7 +603,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
|||||||
|
|
||||||
// Give symbols' section pointers to their sections
|
// Give symbols' section pointers to their sections
|
||||||
for (uint32_t i = 0; i < nbSymbols; i++) {
|
for (uint32_t i = 0; i < nbSymbols; i++) {
|
||||||
if (Label *label = std::get_if<Label>(&fileSymbols[i].data); label) {
|
if (auto *label = std::get_if<Label>(&fileSymbols[i].data); label) {
|
||||||
Section *section = fileSections[label->sectionID].get();
|
Section *section = fileSections[label->sectionID].get();
|
||||||
|
|
||||||
label->section = section;
|
label->section = section;
|
||||||
@@ -620,7 +620,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
|||||||
// This has to run **after** all the `sect_AddSection()` calls,
|
// This has to run **after** all the `sect_AddSection()` calls,
|
||||||
// so that `sect_GetSection()` will work
|
// so that `sect_GetSection()` will work
|
||||||
for (uint32_t i = 0; i < nbSymbols; i++) {
|
for (uint32_t i = 0; i < nbSymbols; i++) {
|
||||||
if (Label *label = std::get_if<Label>(&fileSymbols[i].data); label) {
|
if (auto *label = std::get_if<Label>(&fileSymbols[i].data); label) {
|
||||||
if (Section *section = label->section; section->modifier != SECTION_NORMAL) {
|
if (Section *section = label->section; section->modifier != SECTION_NORMAL) {
|
||||||
if (section->modifier == SECTION_FRAGMENT)
|
if (section->modifier == SECTION_FRAGMENT)
|
||||||
// Add the fragment's offset to the symbol's
|
// Add the fragment's offset to the symbol's
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
|||||||
);
|
);
|
||||||
isError = true;
|
isError = true;
|
||||||
value = 1;
|
value = 1;
|
||||||
} else if (Label const *label = std::get_if<Label>(&symbol->data); label) {
|
} else if (auto *label = std::get_if<Label>(&symbol->data); label) {
|
||||||
value = label->section->bank;
|
value = label->section->bank;
|
||||||
} else {
|
} else {
|
||||||
error(
|
error(
|
||||||
@@ -383,16 +383,11 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
|||||||
fileSymbols[value].name.c_str()
|
fileSymbols[value].name.c_str()
|
||||||
);
|
);
|
||||||
isError = true;
|
isError = true;
|
||||||
|
} else if (auto *label = std::get_if<Label>(&symbol->data); label) {
|
||||||
|
value = label->section->org + label->offset;
|
||||||
} else {
|
} else {
|
||||||
value = std::visit(
|
assert(std::holds_alternative<int32_t>(symbol->data));
|
||||||
Visitor{
|
value = std::get<int32_t>(symbol->data);
|
||||||
[](int32_t val) -> int32_t { return val; },
|
|
||||||
[](Label label) -> int32_t {
|
|
||||||
return label.section->org + label.offset;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
symbol->data
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -392,16 +392,14 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
|||||||
if (other) {
|
if (other) {
|
||||||
// The same symbol can only be defined twice if neither
|
// The same symbol can only be defined twice if neither
|
||||||
// definition is in a floating section
|
// definition is in a floating section
|
||||||
auto visitor = Visitor{
|
auto checkSymbol = [](Symbol const &sym) -> std::tuple<Section *, int32_t> {
|
||||||
[](int32_t value) -> std::tuple<Section *, int32_t> {
|
if (auto *label = std::get_if<Label>(&sym.data); label)
|
||||||
return {nullptr, value};
|
return {label->section, label->offset};
|
||||||
},
|
assert(std::holds_alternative<int32_t>(sym.data));
|
||||||
[](Label label) -> std::tuple<Section *, int32_t> {
|
return {nullptr, std::get<int32_t>(sym.data)};
|
||||||
return {label.section, label.offset};
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
auto [symbolSection, symbolValue] = std::visit(visitor, symbol.data);
|
auto [symbolSection, symbolValue] = checkSymbol(symbol);
|
||||||
auto [otherSection, otherValue] = std::visit(visitor, other->data);
|
auto [otherSection, otherValue] = checkSymbol(*other);
|
||||||
|
|
||||||
if ((otherSection && !otherSection->isAddressFixed)
|
if ((otherSection && !otherSection->isAddressFixed)
|
||||||
|| (symbolSection && !symbolSection->isAddressFixed)) {
|
|| (symbolSection && !symbolSection->isAddressFixed)) {
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ Label const &Symbol::label() const {
|
|||||||
|
|
||||||
void sym_AddSymbol(Symbol &symbol) {
|
void sym_AddSymbol(Symbol &symbol) {
|
||||||
Symbol *other = sym_GetSymbol(symbol.name);
|
Symbol *other = sym_GetSymbol(symbol.name);
|
||||||
int32_t const *symValue = std::get_if<int32_t>(&symbol.data);
|
auto *symValue = std::get_if<int32_t>(&symbol.data);
|
||||||
int32_t const *otherValue = other ? std::get_if<int32_t>(&other->data) : nullptr;
|
auto *otherValue = other ? std::get_if<int32_t>(&other->data) : nullptr;
|
||||||
|
|
||||||
// Check if the symbol already exists with a different value
|
// Check if the symbol already exists with a different value
|
||||||
if (other && !(symValue && otherValue && *symValue == *otherValue)) {
|
if (other && !(symValue && otherValue && *symValue == *otherValue)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user