diff --git a/include/link/object.hpp b/include/link/object.hpp index 67067b08..7163174f 100644 --- a/include/link/object.hpp +++ b/include/link/object.hpp @@ -10,11 +10,6 @@ */ void obj_ReadFile(char const *fileName, unsigned int i); -/* - * Evaluate all assertions - */ -void obj_CheckAssertions(); - /* * Sets up object file reading * @param nbFiles The number of object files that will be read diff --git a/include/link/patch.hpp b/include/link/patch.hpp index cf76fbb9..7e2a68c4 100644 --- a/include/link/patch.hpp +++ b/include/link/patch.hpp @@ -3,26 +3,11 @@ #ifndef RGBDS_LINK_PATCH_H #define RGBDS_LINK_PATCH_H -#include -#include -#include - -#include "linkdefs.hpp" - -#include "link/section.hpp" - -struct Assertion { - Patch patch; // Also used for its `.type` - std::string message; - // This would be redundant with `.section->fileSymbols`, but `section` is sometimes `nullptr`! - std::vector *fileSymbols; -}; - /* * Checks all assertions * @return true if assertion failed */ -void patch_CheckAssertions(std::deque &assertions); +void patch_CheckAssertions(); /* * Applies all SECTIONs' patches to them diff --git a/include/link/section.hpp b/include/link/section.hpp index 64bae79e..191f4493 100644 --- a/include/link/section.hpp +++ b/include/link/section.hpp @@ -5,6 +5,7 @@ // GUIDELINE: external code MUST NOT BE AWARE of the data structure used! +#include #include #include #include @@ -13,6 +14,7 @@ #include "linkdefs.hpp" #include "link/main.hpp" +#include "link/patch.hpp" struct FileStackNode; struct Section; @@ -53,6 +55,15 @@ struct Section { std::unique_ptr
nextu; // The next "component" of this unionized sect }; +struct Assertion { + Patch patch; // Also used for its `.type` + std::string message; + // This would be redundant with `.section->fileSymbols`, but `section` is sometimes `nullptr`! + std::vector *fileSymbols; +}; + +extern std::deque assertions; + /* * Execute a callback for each section currently registered. * This is to avoid exposing the data structure in which sections are stored. diff --git a/src/link/main.cpp b/src/link/main.cpp index 8d7366f9..d0cb440c 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -457,7 +457,7 @@ int main(int argc, char *argv[]) { if (nbErrors != 0) reportErrors(); assign_AssignSections(); - obj_CheckAssertions(); + patch_CheckAssertions(); // and finally output the result. patch_ApplyPatches(); diff --git a/src/link/object.cpp b/src/link/object.cpp index 2d878ecb..cf3c84cb 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -28,7 +28,6 @@ static std::deque> symbolLists; static std::vector> nodes; -static std::deque assertions; // Helper functions for reading object files @@ -632,10 +631,6 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) { } } -void obj_CheckAssertions() { - patch_CheckAssertions(assertions); -} - void obj_Setup(unsigned int nbFiles) { nodes.resize(nbFiles); } diff --git a/src/link/patch.cpp b/src/link/patch.cpp index 111802de..a9919817 100644 --- a/src/link/patch.cpp +++ b/src/link/patch.cpp @@ -3,20 +3,29 @@ #include "link/patch.hpp" #include +#include #include #include +#include #include #include #include +#include #include "error.hpp" #include "helpers.hpp" #include "opmath.hpp" #include "platform.hpp" +#include "linkdefs.hpp" + +#include "link/main.hpp" #include "link/object.hpp" +#include "link/section.hpp" #include "link/symbol.hpp" +std::deque assertions; + struct RPNStackEntry { int32_t value; bool errorFlag; // Whether the value is a placeholder inserted for error recovery @@ -88,10 +97,6 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil // So, if there are two `popRPN` in the same expression, make // sure the operation is commutative. switch (command) { - Symbol const *symbol; - char const *name; - Section const *sect; - case RPN_ADD: value = popRPN(patch) + popRPN(patch); break; @@ -207,9 +212,8 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil value = 0; for (uint8_t shift = 0; shift < 32; shift += 8) value |= getRPNByte(expression, size, patch) << shift; - symbol = getSymbol(fileSymbols, value); - if (!symbol) { + if (Symbol const *symbol = getSymbol(fileSymbols, value); !symbol) { error( patch.src, patch.lineNo, @@ -232,16 +236,14 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil } break; - case RPN_BANK_SECT: + case RPN_BANK_SECT: { // `expression` is not guaranteed to be '\0'-terminated. If it is not, // `getRPNByte` will have a fatal internal error. - name = (char const *)expression; + char const *name = (char const *)expression; while (getRPNByte(expression, size, patch)) ; - sect = sect_GetSection(name); - - if (!sect) { + if (Section const *sect = sect_GetSection(name); !sect) { error( patch.src, patch.lineNo, @@ -254,6 +256,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil value = sect->bank; } break; + } case RPN_BANK_SELF: if (!patch.pcSection) { @@ -265,15 +268,13 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil } break; - case RPN_SIZEOF_SECT: + case RPN_SIZEOF_SECT: { // This has assumptions commented in the `RPN_BANK_SECT` case above. - name = (char const *)expression; + char const *name = (char const *)expression; while (getRPNByte(expression, size, patch)) ; - sect = sect_GetSection(name); - - if (!sect) { + if (Section const *sect = sect_GetSection(name); !sect) { error( patch.src, patch.lineNo, @@ -286,17 +287,15 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil value = sect->size; } break; + } - case RPN_STARTOF_SECT: + case RPN_STARTOF_SECT: { // This has assumptions commented in the `RPN_BANK_SECT` case above. - name = (char const *)expression; + char const *name = (char const *)expression; while (getRPNByte(expression, size, patch)) ; - sect = sect_GetSection(name); - assert(sect->offset == 0); - - if (!sect) { + if (Section const *sect = sect_GetSection(name); !sect) { error( patch.src, patch.lineNo, @@ -306,9 +305,11 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil isError = true; value = 1; } else { + assert(sect->offset == 0); value = sect->org; } break; + } case RPN_SIZEOF_SECTTYPE: value = getRPNByte(expression, size, patch); @@ -373,9 +374,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil value = patch.pcOffset + patch.pcSection->org; } } else { - symbol = getSymbol(fileSymbols, value); - - if (!symbol) { + if (Symbol const *symbol = getSymbol(fileSymbols, value); !symbol) { error( patch.src, patch.lineNo, @@ -403,7 +402,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector const &fil return popRPN(patch); } -void patch_CheckAssertions(std::deque &assertions) { +void patch_CheckAssertions() { verbosePrint("Checking assertions...\n"); for (Assertion &assert : assertions) { diff --git a/src/link/section.cpp b/src/link/section.cpp index 0173bd11..7869d8b9 100644 --- a/src/link/section.cpp +++ b/src/link/section.cpp @@ -3,7 +3,6 @@ #include "link/section.hpp" #include -#include #include #include #include