mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Use a custom generic tagged union Either instead of std::variant for efficiency (#1476)
* Implement custom generic tagged union `Either` This should be more efficient than `std::variant`, while still keeping runtime safety as it `assert`s when `get`ting values. * Use `Either` for RPN expressions * Use `Either` for file stack node data * Use `Either` for `File` buffer * Use `Either` for `STRFMT` args * Use `Either` for RGBLINK symbol values * Support an equivalent of `std::monostate` for `Either` * Use `Either` for lexer tokens * Use `Either` for symbol values * Use `Either` for lexer mmap/buffer state
This commit is contained in:
@@ -10,16 +10,16 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "either.hpp"
|
||||||
#include "linkdefs.hpp"
|
#include "linkdefs.hpp"
|
||||||
|
|
||||||
#include "asm/lexer.hpp"
|
#include "asm/lexer.hpp"
|
||||||
|
|
||||||
struct FileStackNode {
|
struct FileStackNode {
|
||||||
FileStackNodeType type;
|
FileStackNodeType type;
|
||||||
std::variant<
|
Either<
|
||||||
std::vector<uint32_t>, // NODE_REPT
|
std::vector<uint32_t>, // NODE_REPT
|
||||||
std::string // NODE_FILE, NODE_MACRO
|
std::string // NODE_FILE, NODE_MACRO
|
||||||
>
|
>
|
||||||
@@ -34,13 +34,13 @@ struct FileStackNode {
|
|||||||
uint32_t ID = -1;
|
uint32_t ID = -1;
|
||||||
|
|
||||||
// REPT iteration counts since last named node, in reverse depth order
|
// REPT iteration counts since last named node, in reverse depth order
|
||||||
std::vector<uint32_t> &iters();
|
std::vector<uint32_t> &iters() { return data.get<std::vector<uint32_t>>(); }
|
||||||
std::vector<uint32_t> const &iters() const;
|
std::vector<uint32_t> const &iters() const { return data.get<std::vector<uint32_t>>(); }
|
||||||
// File name for files, file::macro name for macros
|
// File name for files, file::macro name for macros
|
||||||
std::string &name();
|
std::string &name() { return data.get<std::string>(); }
|
||||||
std::string const &name() const;
|
std::string const &name() const { return data.get<std::string>(); }
|
||||||
|
|
||||||
FileStackNode(FileStackNodeType type_, std::variant<std::vector<uint32_t>, std::string> data_)
|
FileStackNode(FileStackNodeType type_, Either<std::vector<uint32_t>, std::string> data_)
|
||||||
: type(type_), data(data_){};
|
: type(type_), data(data_){};
|
||||||
|
|
||||||
std::string const &dump(uint32_t curLineNo) const;
|
std::string const &dump(uint32_t curLineNo) const;
|
||||||
|
|||||||
@@ -8,9 +8,9 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "either.hpp"
|
||||||
#include "platform.hpp" // SSIZE_MAX
|
#include "platform.hpp" // SSIZE_MAX
|
||||||
|
|
||||||
// This value is a compromise between `LexerState` allocation performance when `mmap` works, and
|
// This value is a compromise between `LexerState` allocation performance when `mmap` works, and
|
||||||
@@ -98,7 +98,7 @@ struct LexerState {
|
|||||||
bool expandStrings;
|
bool expandStrings;
|
||||||
std::deque<Expansion> expansions; // Front is the innermost current expansion
|
std::deque<Expansion> expansions; // Front is the innermost current expansion
|
||||||
|
|
||||||
std::variant<std::monostate, ViewedContent, BufferedContent> content;
|
Either<ViewedContent, BufferedContent> content;
|
||||||
|
|
||||||
~LexerState();
|
~LexerState();
|
||||||
|
|
||||||
|
|||||||
@@ -5,18 +5,19 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "either.hpp"
|
||||||
#include "linkdefs.hpp"
|
#include "linkdefs.hpp"
|
||||||
|
|
||||||
struct Symbol;
|
struct Symbol;
|
||||||
|
|
||||||
struct Expression {
|
struct Expression {
|
||||||
std::variant<
|
Either<
|
||||||
int32_t, // If the expression's value is known, it's here
|
int32_t, // If the expression's value is known, it's here
|
||||||
std::string // Why the expression is not known, if it isn't
|
std::string // Why the expression is not known, if it isn't
|
||||||
> data = 0;
|
>
|
||||||
|
data = 0;
|
||||||
bool isSymbol = false; // Whether the expression represents a symbol suitable for const diffing
|
bool isSymbol = false; // Whether the expression represents a symbol suitable for const diffing
|
||||||
std::vector<uint8_t> rpn{}; // Bytes serializing the RPN expression
|
std::vector<uint8_t> rpn{}; // Bytes serializing the RPN expression
|
||||||
uint32_t rpnPatchSize = 0; // Size the expression will take in the object file
|
uint32_t rpnPatchSize = 0; // Size the expression will take in the object file
|
||||||
@@ -30,8 +31,12 @@ struct Expression {
|
|||||||
|
|
||||||
Expression &operator=(Expression &&) = default;
|
Expression &operator=(Expression &&) = default;
|
||||||
|
|
||||||
bool isKnown() const { return std::holds_alternative<int32_t>(data); }
|
bool isKnown() const {
|
||||||
int32_t value() const;
|
return data.holds<int32_t>();
|
||||||
|
}
|
||||||
|
int32_t value() const {
|
||||||
|
return data.get<int32_t>();
|
||||||
|
}
|
||||||
|
|
||||||
int32_t getConstVal() const;
|
int32_t getConstVal() const;
|
||||||
Symbol const *symbolOf() const;
|
Symbol const *symbolOf() const;
|
||||||
|
|||||||
176
include/either.hpp
Normal file
176
include/either.hpp
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
/* SPDX-License-Identifier: MIT */
|
||||||
|
|
||||||
|
#ifndef RGBDS_EITHER_HPP
|
||||||
|
#define RGBDS_EITHER_HPP
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "helpers.hpp" // assume
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
union Either {
|
||||||
|
typedef T1 type1;
|
||||||
|
typedef T2 type2;
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<typename T, unsigned V>
|
||||||
|
struct Field {
|
||||||
|
constexpr static unsigned tag_value = V;
|
||||||
|
|
||||||
|
unsigned tag = tag_value;
|
||||||
|
T value;
|
||||||
|
|
||||||
|
Field() : value() {}
|
||||||
|
Field(T &value_) : value(value_) {}
|
||||||
|
Field(T const &value_) : value(value_) {}
|
||||||
|
Field(T &&value_) : value(std::move(value_)) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// The `_tag` unifies with the first `tag` member of each `struct`.
|
||||||
|
constexpr static unsigned nulltag = 0;
|
||||||
|
unsigned _tag = nulltag;
|
||||||
|
Field<T1, 1> _t1;
|
||||||
|
Field<T2, 2> _t2;
|
||||||
|
|
||||||
|
// Value accessors; the function parameters are dummies for overload resolution.
|
||||||
|
// Only used to implement `field()` below.
|
||||||
|
auto &pick(T1 *) { return _t1; }
|
||||||
|
auto const &pick(T1 *) const { return _t1; }
|
||||||
|
auto &pick(T2 *) { return _t2; }
|
||||||
|
auto const &pick(T2 *) const { return _t2; }
|
||||||
|
|
||||||
|
// Generic field accessors; for internal use only.
|
||||||
|
template<typename T>
|
||||||
|
auto &field() {
|
||||||
|
return pick((T *)nullptr);
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
auto const &field() const {
|
||||||
|
return pick((T *)nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Equivalent of `std::monostate` for `std::variant`s.
|
||||||
|
Either() : _tag() {}
|
||||||
|
// These constructors cannot be generic over the value type, because that would prevent
|
||||||
|
// constructible values from being inferred, e.g. a `const char *` string literal for an
|
||||||
|
// `std::string` field value.
|
||||||
|
Either(T1 &value) : _t1(value) {}
|
||||||
|
Either(T2 &value) : _t2(value) {}
|
||||||
|
Either(T1 const &value) : _t1(value) {}
|
||||||
|
Either(T2 const &value) : _t2(value) {}
|
||||||
|
Either(T1 &&value) : _t1(std::move(value)) {}
|
||||||
|
Either(T2 &&value) : _t2(std::move(value)) {}
|
||||||
|
|
||||||
|
// Destructor manually calls the appropriate value destructor.
|
||||||
|
~Either() {
|
||||||
|
if (_tag == _t1.tag_value) {
|
||||||
|
_t1.value.~T1();
|
||||||
|
} else if (_tag == _t2.tag_value) {
|
||||||
|
_t2.value.~T2();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy assignment operators for each possible value.
|
||||||
|
Either &operator=(T1 const &value) {
|
||||||
|
_t1.tag = _t1.tag_value;
|
||||||
|
new (&_t1.value) T1(value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Either &operator=(T2 const &value) {
|
||||||
|
_t2.tag = _t2.tag_value;
|
||||||
|
new (&_t2.value) T2(value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move assignment operators for each possible value.
|
||||||
|
Either &operator=(T1 &&value) {
|
||||||
|
_t1.tag = _t1.tag_value;
|
||||||
|
new (&_t1.value) T1(std::move(value));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Either &operator=(T2 &&value) {
|
||||||
|
_t2.tag = _t2.tag_value;
|
||||||
|
new (&_t2.value) T2(std::move(value));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy assignment operator from another `Either`.
|
||||||
|
Either &operator=(Either other) {
|
||||||
|
if (other._tag == other._t1.tag_value) {
|
||||||
|
*this = other._t1.value;
|
||||||
|
} else if (other._tag == other._t2.tag_value) {
|
||||||
|
*this = other._t2.value;
|
||||||
|
} else {
|
||||||
|
_tag = nulltag;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy constructor from another `Either`; implemented in terms of value assignment operators.
|
||||||
|
Either(Either const &other) {
|
||||||
|
if (other._tag == other._t1.tag_value) {
|
||||||
|
*this = other._t1.value;
|
||||||
|
} else if (other._tag == other._t2.tag_value) {
|
||||||
|
*this = other._t2.value;
|
||||||
|
} else {
|
||||||
|
_tag = nulltag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move constructor from another `Either`; implemented in terms of value assignment operators.
|
||||||
|
Either(Either &&other) {
|
||||||
|
if (other._tag == other._t1.tag_value) {
|
||||||
|
*this = std::move(other._t1.value);
|
||||||
|
} else if (other._tag == other._t2.tag_value) {
|
||||||
|
*this = std::move(other._t2.value);
|
||||||
|
} else {
|
||||||
|
_tag = nulltag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equivalent of `.emplace<T>()` for `std::variant`s.
|
||||||
|
template<typename T, typename... Args>
|
||||||
|
void emplace(Args &&...args) {
|
||||||
|
this->~Either();
|
||||||
|
if constexpr (std::is_same_v<T, T1>) {
|
||||||
|
_t1.tag = _t1.tag_value;
|
||||||
|
new (&_t1.value) T1(std::forward<Args>(args)...);
|
||||||
|
} else if constexpr (std::is_same_v<T, T2>) {
|
||||||
|
_t2.tag = _t2.tag_value;
|
||||||
|
new (&_t2.value) T2(std::forward<Args>(args)...);
|
||||||
|
} else {
|
||||||
|
_tag = nulltag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equivalent of `std::holds_alternative<std::monostate>()` for `std::variant`s.
|
||||||
|
bool empty() const { return _tag == nulltag; }
|
||||||
|
|
||||||
|
// Equivalent of `std::holds_alternative<T>()` for `std::variant`s.
|
||||||
|
template<typename T>
|
||||||
|
bool holds() const {
|
||||||
|
if constexpr (std::is_same_v<T, T1>) {
|
||||||
|
return _tag == _t1.tag_value;
|
||||||
|
} else if constexpr (std::is_same_v<T, T2>) {
|
||||||
|
return _tag == _t2.tag_value;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equivalent of `std::get<T>()` for `std::variant`s.
|
||||||
|
template<typename T>
|
||||||
|
auto &get() {
|
||||||
|
assume(holds<T>());
|
||||||
|
return field<T>().value;
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
auto const &get() const {
|
||||||
|
assume(holds<T>());
|
||||||
|
return field<T>().value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // RGBDS_EITHER_HPP
|
||||||
@@ -10,8 +10,8 @@
|
|||||||
#include <streambuf>
|
#include <streambuf>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
|
||||||
|
|
||||||
|
#include "either.hpp"
|
||||||
#include "helpers.hpp" // assume
|
#include "helpers.hpp" // assume
|
||||||
#include "platform.hpp"
|
#include "platform.hpp"
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
class File {
|
class File {
|
||||||
// Construct a `std::streambuf *` by default, since it's probably lighter than a `filebuf`.
|
// Construct a `std::streambuf *` by default, since it's probably lighter than a `filebuf`.
|
||||||
std::variant<std::streambuf *, std::filebuf> _file;
|
Either<std::streambuf *, std::filebuf> _file;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
File() {}
|
File() {}
|
||||||
@@ -31,7 +31,8 @@ public:
|
|||||||
*/
|
*/
|
||||||
File *open(std::string const &path, std::ios_base::openmode mode) {
|
File *open(std::string const &path, std::ios_base::openmode mode) {
|
||||||
if (path != "-") {
|
if (path != "-") {
|
||||||
return _file.emplace<std::filebuf>().open(path, mode) ? this : nullptr;
|
_file.emplace<std::filebuf>();
|
||||||
|
return _file.get<std::filebuf>().open(path, mode) ? this : nullptr;
|
||||||
} else if (mode & std::ios_base::in) {
|
} else if (mode & std::ios_base::in) {
|
||||||
assume(!(mode & std::ios_base::out));
|
assume(!(mode & std::ios_base::out));
|
||||||
_file.emplace<std::streambuf *>(std::cin.rdbuf());
|
_file.emplace<std::streambuf *>(std::cin.rdbuf());
|
||||||
@@ -49,8 +50,8 @@ public:
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
std::streambuf &operator*() {
|
std::streambuf &operator*() {
|
||||||
auto *file = std::get_if<std::filebuf>(&_file);
|
return _file.holds<std::filebuf>() ? _file.get<std::filebuf>()
|
||||||
return file ? *file : *std::get<std::streambuf *>(_file);
|
: *_file.get<std::streambuf *>();
|
||||||
}
|
}
|
||||||
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.
|
||||||
@@ -63,22 +64,23 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
File *close() {
|
File *close() {
|
||||||
if (auto *file = std::get_if<std::filebuf>(&_file); file) {
|
if (_file.holds<std::filebuf>()) {
|
||||||
// This is called by the destructor, and an explicit `close` shouldn't close twice.
|
// This is called by the destructor, and an explicit `close` shouldn't close twice.
|
||||||
|
std::filebuf fileBuf = std::move(_file.get<std::filebuf>());
|
||||||
_file.emplace<std::streambuf *>(nullptr);
|
_file.emplace<std::streambuf *>(nullptr);
|
||||||
if (file->close() != nullptr) {
|
if (fileBuf.close() != nullptr) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
} else if (std::get<std::streambuf *>(_file) != nullptr) {
|
} else if (_file.get<std::streambuf *>() != nullptr) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
char const *c_str(std::string const &path) const {
|
char const *c_str(std::string const &path) const {
|
||||||
return std::holds_alternative<std::filebuf>(_file) ? path.c_str()
|
return _file.holds<std::filebuf>() ? path.c_str()
|
||||||
: std::get<std::streambuf *>(_file) == std::cin.rdbuf() ? "<stdin>"
|
: _file.get<std::streambuf *>() == std::cin.rdbuf() ? "<stdin>"
|
||||||
: "<stdout>";
|
: "<stdout>";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "either.hpp"
|
||||||
#include "linkdefs.hpp"
|
#include "linkdefs.hpp"
|
||||||
|
|
||||||
// Variables related to CLI options
|
// Variables related to CLI options
|
||||||
@@ -38,8 +38,7 @@ extern bool disablePadding;
|
|||||||
|
|
||||||
struct FileStackNode {
|
struct FileStackNode {
|
||||||
FileStackNodeType type;
|
FileStackNodeType type;
|
||||||
std::variant<
|
Either<
|
||||||
std::monostate, // Default constructed; `.type` and `.data` must be set manually
|
|
||||||
std::vector<uint32_t>, // NODE_REPT
|
std::vector<uint32_t>, // NODE_REPT
|
||||||
std::string // NODE_FILE, NODE_MACRO
|
std::string // NODE_FILE, NODE_MACRO
|
||||||
>
|
>
|
||||||
@@ -50,11 +49,11 @@ struct FileStackNode {
|
|||||||
uint32_t lineNo;
|
uint32_t lineNo;
|
||||||
|
|
||||||
// REPT iteration counts since last named node, in reverse depth order
|
// REPT iteration counts since last named node, in reverse depth order
|
||||||
std::vector<uint32_t> &iters();
|
std::vector<uint32_t> &iters() { return data.get<std::vector<uint32_t>>(); }
|
||||||
std::vector<uint32_t> const &iters() const;
|
std::vector<uint32_t> const &iters() const { return data.get<std::vector<uint32_t>>(); }
|
||||||
// File name for files, file::macro name for macros
|
// File name for files, file::macro name for macros
|
||||||
std::string &name();
|
std::string &name() { return data.get<std::string>(); }
|
||||||
std::string const &name() const;
|
std::string const &name() const { return data.get<std::string>(); }
|
||||||
|
|
||||||
std::string const &dump(uint32_t curLineNo) const;
|
std::string const &dump(uint32_t curLineNo) const;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
|
||||||
|
|
||||||
|
#include "either.hpp"
|
||||||
#include "linkdefs.hpp"
|
#include "linkdefs.hpp"
|
||||||
|
|
||||||
struct FileStackNode;
|
struct FileStackNode;
|
||||||
@@ -28,14 +28,14 @@ struct Symbol {
|
|||||||
char const *objFileName;
|
char const *objFileName;
|
||||||
FileStackNode const *src;
|
FileStackNode const *src;
|
||||||
int32_t lineNo;
|
int32_t lineNo;
|
||||||
std::variant<
|
Either<
|
||||||
int32_t, // Constants just have a numeric value
|
int32_t, // Constants just have a numeric value
|
||||||
Label // Label values refer to an offset within a specific section
|
Label // Label values refer to an offset within a specific section
|
||||||
>
|
>
|
||||||
data;
|
data;
|
||||||
|
|
||||||
Label &label();
|
Label &label() { return data.get<Label>(); }
|
||||||
Label const &label() const;
|
Label const &label() const { return data.get<Label>(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
void sym_ForEach(void (*callback)(Symbol &));
|
void sym_ForEach(void (*callback)(Symbol &));
|
||||||
|
|||||||
@@ -48,28 +48,8 @@ static std::vector<std::string> includePaths = {""};
|
|||||||
|
|
||||||
static std::string preIncludeName;
|
static std::string preIncludeName;
|
||||||
|
|
||||||
std::vector<uint32_t> &FileStackNode::iters() {
|
|
||||||
assume(std::holds_alternative<std::vector<uint32_t>>(data));
|
|
||||||
return std::get<std::vector<uint32_t>>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<uint32_t> const &FileStackNode::iters() const {
|
|
||||||
assume(std::holds_alternative<std::vector<uint32_t>>(data));
|
|
||||||
return std::get<std::vector<uint32_t>>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string &FileStackNode::name() {
|
|
||||||
assume(std::holds_alternative<std::string>(data));
|
|
||||||
return std::get<std::string>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string const &FileStackNode::name() const {
|
|
||||||
assume(std::holds_alternative<std::string>(data));
|
|
||||||
return std::get<std::string>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||||
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
if (data.holds<std::vector<uint32_t>>()) {
|
||||||
assume(parent); // REPT nodes use their parent's name
|
assume(parent); // REPT nodes use their parent's name
|
||||||
std::string const &lastName = parent->dump(lineNo);
|
std::string const &lastName = parent->dump(lineNo);
|
||||||
fputs(" -> ", stderr);
|
fputs(" -> ", stderr);
|
||||||
|
|||||||
@@ -106,10 +106,10 @@ using namespace std::literals;
|
|||||||
|
|
||||||
struct Token {
|
struct Token {
|
||||||
int type;
|
int type;
|
||||||
std::variant<std::monostate, uint32_t, std::string> value;
|
Either<uint32_t, std::string> value;
|
||||||
|
|
||||||
Token() : type(T_(NUMBER)), value(std::monostate{}) {}
|
Token() : type(T_(NUMBER)), value() {}
|
||||||
Token(int type_) : type(type_), value(std::monostate{}) {}
|
Token(int type_) : type(type_), value() {}
|
||||||
Token(int type_, uint32_t value_) : type(type_), value(value_) {}
|
Token(int type_, uint32_t value_) : type(type_), value(value_) {}
|
||||||
Token(int type_, std::string const &value_) : type(type_), value(value_) {}
|
Token(int type_, std::string const &value_) : type(type_), value(value_) {}
|
||||||
Token(int type_, std::string &&value_) : type(type_), value(value_) {}
|
Token(int type_, std::string &&value_) : type(type_), value(value_) {}
|
||||||
@@ -464,8 +464,8 @@ void LexerState::setViewAsNextState(char const *name, ContentSpan const &span, u
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lexer_RestartRept(uint32_t lineNo) {
|
void lexer_RestartRept(uint32_t lineNo) {
|
||||||
if (auto *view = std::get_if<ViewedContent>(&lexerState->content); view) {
|
if (lexerState->content.holds<ViewedContent>()) {
|
||||||
view->offset = 0;
|
lexerState->content.get<ViewedContent>().offset = 0;
|
||||||
}
|
}
|
||||||
lexerState->clear(lineNo);
|
lexerState->clear(lineNo);
|
||||||
}
|
}
|
||||||
@@ -696,12 +696,12 @@ int LexerState::peekChar() {
|
|||||||
return (uint8_t)(*exp.contents)[exp.offset];
|
return (uint8_t)(*exp.contents)[exp.offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto *view = std::get_if<ViewedContent>(&content); view) {
|
if (content.holds<ViewedContent>()) {
|
||||||
if (view->offset < view->span.size)
|
auto &view = content.get<ViewedContent>();
|
||||||
return (uint8_t)view->span.ptr[view->offset];
|
if (view.offset < view.span.size)
|
||||||
|
return (uint8_t)view.span.ptr[view.offset];
|
||||||
} else {
|
} else {
|
||||||
assume(std::holds_alternative<BufferedContent>(content));
|
auto &cbuf = content.get<BufferedContent>();
|
||||||
auto &cbuf = std::get<BufferedContent>(content);
|
|
||||||
if (cbuf.size == 0)
|
if (cbuf.size == 0)
|
||||||
cbuf.refill();
|
cbuf.refill();
|
||||||
assume(cbuf.offset < LEXER_BUF_SIZE);
|
assume(cbuf.offset < LEXER_BUF_SIZE);
|
||||||
@@ -726,12 +726,12 @@ int LexerState::peekCharAhead() {
|
|||||||
distance -= exp.size() - exp.offset;
|
distance -= exp.size() - exp.offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto *view = std::get_if<ViewedContent>(&content); view) {
|
if (content.holds<ViewedContent>()) {
|
||||||
if (view->offset + distance < view->span.size)
|
auto &view = content.get<ViewedContent>();
|
||||||
return (uint8_t)view->span.ptr[view->offset + distance];
|
if (view.offset + distance < view.span.size)
|
||||||
|
return (uint8_t)view.span.ptr[view.offset + distance];
|
||||||
} else {
|
} else {
|
||||||
assume(std::holds_alternative<BufferedContent>(content));
|
auto &cbuf = content.get<BufferedContent>();
|
||||||
auto &cbuf = std::get<BufferedContent>(content);
|
|
||||||
assume(distance < LEXER_BUF_SIZE);
|
assume(distance < LEXER_BUF_SIZE);
|
||||||
if (cbuf.size <= distance)
|
if (cbuf.size <= distance)
|
||||||
cbuf.refill();
|
cbuf.refill();
|
||||||
@@ -815,12 +815,10 @@ restart:
|
|||||||
} else {
|
} else {
|
||||||
// Advance within the file contents
|
// Advance within the file contents
|
||||||
lexerState->colNo++;
|
lexerState->colNo++;
|
||||||
if (auto *view = std::get_if<ViewedContent>(&lexerState->content); view) {
|
if (lexerState->content.holds<ViewedContent>()) {
|
||||||
view->offset++;
|
lexerState->content.get<ViewedContent>().offset++;
|
||||||
} else {
|
} else {
|
||||||
assume(std::holds_alternative<BufferedContent>(lexerState->content));
|
lexerState->content.get<BufferedContent>().advance();
|
||||||
auto &cbuf = std::get<BufferedContent>(lexerState->content);
|
|
||||||
cbuf.advance();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1796,12 +1794,12 @@ static Token yylex_NORMAL() {
|
|||||||
return token;
|
return token;
|
||||||
|
|
||||||
// `token` is either an `ID` or a `LOCAL_ID`, and both have a `std::string` value.
|
// `token` is either an `ID` or a `LOCAL_ID`, and both have a `std::string` value.
|
||||||
assume(std::holds_alternative<std::string>(token.value));
|
assume(token.value.holds<std::string>());
|
||||||
|
|
||||||
// Local symbols cannot be string expansions
|
// Local symbols cannot be string expansions
|
||||||
if (token.type == T_(ID) && lexerState->expandStrings) {
|
if (token.type == T_(ID) && lexerState->expandStrings) {
|
||||||
// Attempt string expansion
|
// Attempt string expansion
|
||||||
Symbol const *sym = sym_FindExactSymbol(std::get<std::string>(token.value));
|
Symbol const *sym = sym_FindExactSymbol(token.value.get<std::string>());
|
||||||
|
|
||||||
if (sym && sym->type == SYM_EQUS) {
|
if (sym && sym->type == SYM_EQUS) {
|
||||||
std::shared_ptr<std::string> str = sym->getEqus();
|
std::shared_ptr<std::string> str = sym->getEqus();
|
||||||
@@ -2176,17 +2174,22 @@ yy::parser::symbol_type yylex() {
|
|||||||
Token token = lexerModeFuncs[lexerState->mode]();
|
Token token = lexerModeFuncs[lexerState->mode]();
|
||||||
|
|
||||||
// Captures end at their buffer's boundary no matter what
|
// Captures end at their buffer's boundary no matter what
|
||||||
if (token.type == T_(YYEOF) && !lexerState->capturing)
|
if (token.type == T_(YYEOF) && !lexerState->capturing) {
|
||||||
token = Token(T_(EOB));
|
// Doing `token = Token(T_(EOB));` here would be valid but redundant, because YYEOF and EOB
|
||||||
|
// both have the same empty value. Furthermore, g++ 11.4.0 was giving a false-positive
|
||||||
|
// '-Wmaybe-uninitialized' warning for `Token::value.Either<...>::_tag` that way.
|
||||||
|
// (This was on a developer's local machine; GitHub Actions CI's g++ was not warning.)
|
||||||
|
token.type = T_(EOB);
|
||||||
|
}
|
||||||
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);
|
||||||
|
|
||||||
if (auto *numValue = std::get_if<uint32_t>(&token.value); numValue) {
|
if (token.value.holds<uint32_t>()) {
|
||||||
return yy::parser::symbol_type(token.type, *numValue);
|
return yy::parser::symbol_type(token.type, token.value.get<uint32_t>());
|
||||||
} else if (auto *strValue = std::get_if<std::string>(&token.value); strValue) {
|
} else if (token.value.holds<std::string>()) {
|
||||||
return yy::parser::symbol_type(token.type, *strValue);
|
return yy::parser::symbol_type(token.type, token.value.get<std::string>());
|
||||||
} else {
|
} else {
|
||||||
assume(std::holds_alternative<std::monostate>(token.value));
|
assume(token.value.empty());
|
||||||
return yy::parser::symbol_type(token.type);
|
return yy::parser::symbol_type(token.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2202,10 +2205,10 @@ static Capture startCapture() {
|
|||||||
lexerState->captureSize = 0;
|
lexerState->captureSize = 0;
|
||||||
|
|
||||||
uint32_t lineNo = lexer_GetLineNo();
|
uint32_t lineNo = lexer_GetLineNo();
|
||||||
if (auto *view = std::get_if<ViewedContent>(&lexerState->content);
|
if (lexerState->content.holds<ViewedContent>() && lexerState->expansions.empty()) {
|
||||||
view && lexerState->expansions.empty()) {
|
auto &view = lexerState->content.get<ViewedContent>();
|
||||||
return {
|
return {
|
||||||
.lineNo = lineNo, .span = {.ptr = view->makeSharedContentPtr(), .size = 0}
|
.lineNo = lineNo, .span = {.ptr = view.makeSharedContentPtr(), .size = 0}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
assume(lexerState->captureBuf == nullptr);
|
assume(lexerState->captureBuf == nullptr);
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
%code requires {
|
%code requires {
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "asm/lexer.hpp"
|
#include "asm/lexer.hpp"
|
||||||
@@ -15,6 +14,7 @@
|
|||||||
#include "asm/rpn.hpp"
|
#include "asm/rpn.hpp"
|
||||||
#include "asm/section.hpp"
|
#include "asm/section.hpp"
|
||||||
|
|
||||||
|
#include "either.hpp"
|
||||||
#include "linkdefs.hpp"
|
#include "linkdefs.hpp"
|
||||||
|
|
||||||
struct AlignmentSpec {
|
struct AlignmentSpec {
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
struct StrFmtArgList {
|
struct StrFmtArgList {
|
||||||
std::string format;
|
std::string format;
|
||||||
std::vector<std::variant<uint32_t, std::string>> args;
|
std::vector<Either<uint32_t, std::string>> args;
|
||||||
|
|
||||||
StrFmtArgList() = default;
|
StrFmtArgList() = default;
|
||||||
StrFmtArgList(StrFmtArgList &&) = default;
|
StrFmtArgList(StrFmtArgList &&) = default;
|
||||||
@@ -81,8 +81,7 @@
|
|||||||
std::string_view str, std::string const &old, std::string const &rep
|
std::string_view str, std::string const &old, std::string const &rep
|
||||||
);
|
);
|
||||||
static std::string strfmt(
|
static std::string strfmt(
|
||||||
std::string const &spec,
|
std::string const &spec, std::vector<Either<uint32_t, std::string>> const &args
|
||||||
std::vector<std::variant<uint32_t, std::string>> const &args
|
|
||||||
);
|
);
|
||||||
static void compoundAssignment(std::string const &symName, RPNCommand op, int32_t constValue);
|
static void compoundAssignment(std::string const &symName, RPNCommand op, int32_t constValue);
|
||||||
static void failAssert(AssertionType type);
|
static void failAssert(AssertionType type);
|
||||||
@@ -2642,8 +2641,7 @@ static std::string strrpl(std::string_view str, std::string const &old, std::str
|
|||||||
}
|
}
|
||||||
|
|
||||||
static std::string strfmt(
|
static std::string strfmt(
|
||||||
std::string const &spec,
|
std::string const &spec, std::vector<Either<uint32_t, std::string>> const &args
|
||||||
std::vector<std::variant<uint32_t, std::string>> const &args
|
|
||||||
) {
|
) {
|
||||||
std::string str;
|
std::string str;
|
||||||
size_t argIndex = 0;
|
size_t argIndex = 0;
|
||||||
@@ -2684,12 +2682,10 @@ 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) {
|
} else if (args[argIndex].holds<uint32_t>()) {
|
||||||
fmt.appendNumber(str, *n);
|
fmt.appendNumber(str, args[argIndex].get<uint32_t>());
|
||||||
} else {
|
} else {
|
||||||
assume(std::holds_alternative<std::string>(args[argIndex]));
|
fmt.appendString(str, args[argIndex].get<std::string>());
|
||||||
auto &s = std::get<std::string>(args[argIndex]);
|
|
||||||
fmt.appendString(str, s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
argIndex++;
|
argIndex++;
|
||||||
|
|||||||
@@ -19,11 +19,6 @@
|
|||||||
|
|
||||||
using namespace std::literals;
|
using namespace std::literals;
|
||||||
|
|
||||||
int32_t Expression::value() const {
|
|
||||||
assume(std::holds_alternative<int32_t>(data));
|
|
||||||
return std::get<int32_t>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Expression::clear() {
|
void Expression::clear() {
|
||||||
data = 0;
|
data = 0;
|
||||||
isSymbol = false;
|
isSymbol = false;
|
||||||
@@ -44,7 +39,7 @@ uint8_t *Expression::reserveSpace(uint32_t size, uint32_t patchSize) {
|
|||||||
|
|
||||||
int32_t Expression::getConstVal() const {
|
int32_t Expression::getConstVal() const {
|
||||||
if (!isKnown()) {
|
if (!isKnown()) {
|
||||||
error("Expected constant expression: %s\n", std::get<std::string>(data).c_str());
|
error("Expected constant expression: %s\n", data.get<std::string>().c_str());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return value();
|
return value();
|
||||||
|
|||||||
@@ -46,28 +46,8 @@ FILE *linkerScript;
|
|||||||
|
|
||||||
static uint32_t nbErrors = 0;
|
static uint32_t nbErrors = 0;
|
||||||
|
|
||||||
std::vector<uint32_t> &FileStackNode::iters() {
|
|
||||||
assume(std::holds_alternative<std::vector<uint32_t>>(data));
|
|
||||||
return std::get<std::vector<uint32_t>>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<uint32_t> const &FileStackNode::iters() const {
|
|
||||||
assume(std::holds_alternative<std::vector<uint32_t>>(data));
|
|
||||||
return std::get<std::vector<uint32_t>>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string &FileStackNode::name() {
|
|
||||||
assume(std::holds_alternative<std::string>(data));
|
|
||||||
return std::get<std::string>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string const &FileStackNode::name() const {
|
|
||||||
assume(std::holds_alternative<std::string>(data));
|
|
||||||
return std::get<std::string>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||||
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
if (data.holds<std::vector<uint32_t>>()) {
|
||||||
assume(parent); // REPT nodes use their parent's name
|
assume(parent); // REPT nodes use their parent's name
|
||||||
std::string const &lastName = parent->dump(lineNo);
|
std::string const &lastName = parent->dump(lineNo);
|
||||||
fputs(" -> ", stderr);
|
fputs(" -> ", stderr);
|
||||||
|
|||||||
@@ -503,7 +503,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
|
|||||||
// object file. It's better than nothing.
|
// object file. It's better than nothing.
|
||||||
nodes[fileID].push_back({
|
nodes[fileID].push_back({
|
||||||
.type = NODE_FILE,
|
.type = NODE_FILE,
|
||||||
.data = fileName,
|
.data = Either<std::vector<uint32_t>, std::string>(fileName),
|
||||||
.parent = nullptr,
|
.parent = nullptr,
|
||||||
.lineNo = 0,
|
.lineNo = 0,
|
||||||
});
|
});
|
||||||
@@ -566,8 +566,8 @@ 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 (auto *label = std::get_if<Label>(&symbol.data); label)
|
if (symbol.data.holds<Label>())
|
||||||
nbSymPerSect[label->sectionID]++;
|
nbSymPerSect[symbol.data.get<Label>().sectionID]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This file's sections, stored in a table to link symbols to them
|
// This file's sections, stored in a table to link symbols to them
|
||||||
@@ -605,12 +605,11 @@ 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 (auto *label = std::get_if<Label>(&fileSymbols[i].data); label) {
|
if (fileSymbols[i].data.holds<Label>()) {
|
||||||
Section *section = fileSections[label->sectionID].get();
|
Label &label = fileSymbols[i].data.get<Label>();
|
||||||
|
label.section = fileSections[label.sectionID].get();
|
||||||
label->section = section;
|
|
||||||
// Give the section a pointer to the symbol as well
|
// Give the section a pointer to the symbol as well
|
||||||
linkSymToSect(fileSymbols[i], *section);
|
linkSymToSect(fileSymbols[i], *label.section);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -622,13 +621,15 @@ 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 (auto *label = std::get_if<Label>(&fileSymbols[i].data); label) {
|
if (fileSymbols[i].data.holds<Label>()) {
|
||||||
if (Section *section = label->section; section->modifier != SECTION_NORMAL) {
|
Label &label = fileSymbols[i].data.get<Label>();
|
||||||
if (section->modifier == SECTION_FRAGMENT)
|
if (Section *section = label.section; section->modifier != SECTION_NORMAL) {
|
||||||
|
if (section->modifier == SECTION_FRAGMENT) {
|
||||||
// Add the fragment's offset to the symbol's (`section->offset` is computed by `sect_AddSection`)
|
// Add the fragment's offset to the symbol's (`section->offset` is computed by `sect_AddSection`)
|
||||||
label->offset += section->offset;
|
label.offset += section->offset;
|
||||||
|
}
|
||||||
// Associate the symbol with the main section, not the "component" one
|
// Associate the symbol with the main section, not the "component" one
|
||||||
label->section = sect_GetSection(section->name);
|
label.section = sect_GetSection(section->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -564,16 +564,16 @@ static void writeSym() {
|
|||||||
constants.clear();
|
constants.clear();
|
||||||
sym_ForEach([](Symbol &sym) {
|
sym_ForEach([](Symbol &sym) {
|
||||||
// Symbols are already limited to the exported ones
|
// Symbols are already limited to the exported ones
|
||||||
if (std::holds_alternative<int32_t>(sym.data))
|
if (sym.data.holds<int32_t>())
|
||||||
constants.push_back(&sym);
|
constants.push_back(&sym);
|
||||||
});
|
});
|
||||||
// Numeric constants are ordered by value, then by name
|
// Numeric constants are ordered by value, then by name
|
||||||
std::sort(RANGE(constants), [](Symbol *sym1, Symbol *sym2) -> bool {
|
std::sort(RANGE(constants), [](Symbol *sym1, Symbol *sym2) -> bool {
|
||||||
int32_t val1 = std::get<int32_t>(sym1->data), val2 = std::get<int32_t>(sym2->data);
|
int32_t val1 = sym1->data.get<int32_t>(), val2 = sym2->data.get<int32_t>();
|
||||||
return val1 != val2 ? val1 < val2 : sym1->name < sym2->name;
|
return val1 != val2 ? val1 < val2 : sym1->name < sym2->name;
|
||||||
});
|
});
|
||||||
for (Symbol *sym : constants) {
|
for (Symbol *sym : constants) {
|
||||||
int32_t val = std::get<int32_t>(sym->data);
|
int32_t val = sym->data.get<int32_t>();
|
||||||
int width = val < 0x100 ? 2 : val < 0x10000 ? 4 : 8;
|
int width = val < 0x100 ? 2 : val < 0x10000 ? 4 : 8;
|
||||||
fprintf(symFile, "%0*" PRIx32 " %s\n", width, val, sym->name.c_str());
|
fprintf(symFile, "%0*" PRIx32 " %s\n", width, val, sym->name.c_str());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include <deque>
|
#include <deque>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <variant>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "helpers.hpp" // assume, clz, ctz
|
#include "helpers.hpp" // assume, clz, ctz
|
||||||
@@ -230,8 +229,8 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
|||||||
);
|
);
|
||||||
isError = true;
|
isError = true;
|
||||||
value = 1;
|
value = 1;
|
||||||
} else if (auto *label = std::get_if<Label>(&symbol->data); label) {
|
} else if (symbol->data.holds<Label>()) {
|
||||||
value = label->section->bank;
|
value = symbol->data.get<Label>().section->bank;
|
||||||
} else {
|
} else {
|
||||||
error(
|
error(
|
||||||
patch.src,
|
patch.src,
|
||||||
@@ -390,11 +389,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) {
|
} else if (symbol->data.holds<Label>()) {
|
||||||
value = label->section->org + label->offset;
|
Label const &label = symbol->data.get<Label>();
|
||||||
|
value = label.section->org + label.offset;
|
||||||
} else {
|
} else {
|
||||||
assume(std::holds_alternative<int32_t>(symbol->data));
|
value = symbol->data.get<int32_t>();
|
||||||
value = std::get<int32_t>(symbol->data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <variant>
|
|
||||||
|
|
||||||
#include "helpers.hpp" // assume
|
#include "helpers.hpp" // assume
|
||||||
#include "linkdefs.hpp"
|
#include "linkdefs.hpp"
|
||||||
@@ -395,10 +394,11 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
|||||||
// 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 checkSymbol = [](Symbol const &sym) -> std::tuple<Section *, int32_t> {
|
auto checkSymbol = [](Symbol const &sym) -> std::tuple<Section *, int32_t> {
|
||||||
if (auto *label = std::get_if<Label>(&sym.data); label)
|
if (sym.data.holds<Label>()) {
|
||||||
return {label->section, label->offset};
|
Label const &label = sym.data.get<Label>();
|
||||||
assume(std::holds_alternative<int32_t>(sym.data));
|
return {label.section, label.offset};
|
||||||
return {nullptr, std::get<int32_t>(sym.data)};
|
}
|
||||||
|
return {nullptr, sym.data.get<int32_t>()};
|
||||||
};
|
};
|
||||||
auto [symbolSection, symbolValue] = checkSymbol(symbol);
|
auto [symbolSection, symbolValue] = checkSymbol(symbol);
|
||||||
auto [otherSection, otherValue] = checkSymbol(*other);
|
auto [otherSection, otherValue] = checkSymbol(*other);
|
||||||
@@ -876,13 +876,15 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
|||||||
// 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 (Symbol &sym : fileSymbols) {
|
for (Symbol &sym : fileSymbols) {
|
||||||
if (auto *label = std::get_if<Label>(&sym.data); label) {
|
if (sym.data.holds<Label>()) {
|
||||||
if (Section *section = label->section; section->modifier != SECTION_NORMAL) {
|
Label &label = sym.data.get<Label>();
|
||||||
if (section->modifier == SECTION_FRAGMENT)
|
if (Section *section = label.section; section->modifier != SECTION_NORMAL) {
|
||||||
|
if (section->modifier == SECTION_FRAGMENT) {
|
||||||
// Add the fragment's offset to the symbol's (`section->offset` is computed by `sect_AddSection`)
|
// Add the fragment's offset to the symbol's (`section->offset` is computed by `sect_AddSection`)
|
||||||
label->offset += section->offset;
|
label.offset += section->offset;
|
||||||
|
}
|
||||||
// Associate the symbol with the main section, not the "component" one
|
// Associate the symbol with the main section, not the "component" one
|
||||||
label->section = sect_GetSection(section->name);
|
label.section = sect_GetSection(section->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,16 +12,6 @@
|
|||||||
|
|
||||||
std::unordered_map<std::string, Symbol *> symbols;
|
std::unordered_map<std::string, Symbol *> symbols;
|
||||||
|
|
||||||
Label &Symbol::label() {
|
|
||||||
assume(std::holds_alternative<Label>(data));
|
|
||||||
return std::get<Label>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
Label const &Symbol::label() const {
|
|
||||||
assume(std::holds_alternative<Label>(data));
|
|
||||||
return std::get<Label>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sym_ForEach(void (*callback)(Symbol &)) {
|
void sym_ForEach(void (*callback)(Symbol &)) {
|
||||||
for (auto &it : symbols)
|
for (auto &it : symbols)
|
||||||
callback(*it.second);
|
callback(*it.second);
|
||||||
@@ -29,8 +19,9 @@ void sym_ForEach(void (*callback)(Symbol &)) {
|
|||||||
|
|
||||||
void sym_AddSymbol(Symbol &symbol) {
|
void sym_AddSymbol(Symbol &symbol) {
|
||||||
Symbol *other = sym_GetSymbol(symbol.name);
|
Symbol *other = sym_GetSymbol(symbol.name);
|
||||||
auto *symValue = std::get_if<int32_t>(&symbol.data);
|
int32_t *symValue = symbol.data.holds<int32_t>() ? &symbol.data.get<int32_t>() : nullptr;
|
||||||
auto *otherValue = other ? std::get_if<int32_t>(&other->data) : nullptr;
|
int32_t *otherValue =
|
||||||
|
other && other->data.holds<int32_t>() ? &other->data.get<int32_t>() : 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