From 2828ec06d50df4d4425bf9385aaf726ddf2bfba1 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Fri, 3 Jan 2025 21:35:17 -0500 Subject: [PATCH] Replace some `#define` with `constexpr` --- include/asm/lexer.hpp | 2 +- src/asm/lexer.cpp | 24 ++++++++++++------------ src/fix/main.cpp | 35 ++++++++++++++++------------------- src/link/assign.cpp | 6 +++--- src/link/output.cpp | 2 +- src/link/sdas_obj.cpp | 3 ++- test/asm/ff00+c.asm | 2 +- test/gfx/randtilegen.cpp | 4 ++-- 8 files changed, 38 insertions(+), 40 deletions(-) diff --git a/include/asm/lexer.hpp b/include/asm/lexer.hpp index a282bbe92..9ca6cfa5e 100644 --- a/include/asm/lexer.hpp +++ b/include/asm/lexer.hpp @@ -15,7 +15,7 @@ // This value is a compromise between `LexerState` allocation performance when `mmap` works, and // buffering performance when it doesn't/can't (e.g. when piping a file into RGBASM). -#define LEXER_BUF_SIZE 64 +static constexpr size_t LEXER_BUF_SIZE = 64; // The buffer needs to be large enough for the maximum `lexerState->peek()` lookahead distance static_assert(LEXER_BUF_SIZE > 1, "Lexer buffer size is too small"); // This caps the size of buffer reads, and according to POSIX, passing more than SSIZE_MAX is UB diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 5290eaa34..aa8e12878 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -19,7 +19,7 @@ #include #endif -#include "helpers.hpp" // assume, QUOTEDSTRLEN +#include "helpers.hpp" #include "util.hpp" #include "asm/fixpoint.hpp" @@ -500,27 +500,27 @@ BufferedContent::~BufferedContent() { } void BufferedContent::advance() { - assume(offset < LEXER_BUF_SIZE); + assume(offset < ARRAY_SIZE(buf)); offset++; - if (offset == LEXER_BUF_SIZE) + if (offset == ARRAY_SIZE(buf)) offset = 0; // Wrap around if necessary assume(size > 0); size--; } void BufferedContent::refill() { - size_t target = LEXER_BUF_SIZE - size; // Aim: making the buf full + size_t target = ARRAY_SIZE(buf) - size; // Aim: making the buf full // Compute the index we'll start writing to - size_t startIndex = (offset + size) % LEXER_BUF_SIZE; + size_t startIndex = (offset + size) % ARRAY_SIZE(buf); // If the range to fill passes over the buffer wrapping point, we need two reads - if (startIndex + target > LEXER_BUF_SIZE) { - size_t nbExpectedChars = LEXER_BUF_SIZE - startIndex; + if (startIndex + target > ARRAY_SIZE(buf)) { + size_t nbExpectedChars = ARRAY_SIZE(buf) - startIndex; size_t nbReadChars = readMore(startIndex, nbExpectedChars); startIndex += nbReadChars; - if (startIndex == LEXER_BUF_SIZE) + if (startIndex == ARRAY_SIZE(buf)) startIndex = 0; // If the read was incomplete, don't perform a second read @@ -534,7 +534,7 @@ void BufferedContent::refill() { size_t BufferedContent::readMore(size_t startIndex, size_t nbChars) { // This buffer overflow made me lose WEEKS of my life. Never again. - assume(startIndex + nbChars <= LEXER_BUF_SIZE); + assume(startIndex + nbChars <= ARRAY_SIZE(buf)); ssize_t nbReadChars = read(fd, &buf[startIndex], nbChars); if (nbReadChars == -1) @@ -720,7 +720,7 @@ int LexerState::peekChar() { auto &cbuf = content.get(); if (cbuf.size == 0) cbuf.refill(); - assume(cbuf.offset < LEXER_BUF_SIZE); + assume(cbuf.offset < ARRAY_SIZE(cbuf.buf)); if (cbuf.size > 0) return static_cast(cbuf.buf[cbuf.offset]); } @@ -748,11 +748,11 @@ int LexerState::peekCharAhead() { return static_cast(view.span.ptr[view.offset + distance]); } else { auto &cbuf = content.get(); - assume(distance < LEXER_BUF_SIZE); + assume(distance < ARRAY_SIZE(cbuf.buf)); if (cbuf.size <= distance) cbuf.refill(); if (cbuf.size > distance) - return static_cast(cbuf.buf[(cbuf.offset + distance) % LEXER_BUF_SIZE]); + return static_cast(cbuf.buf[(cbuf.offset + distance) % ARRAY_SIZE(cbuf.buf)]); } // If there aren't enough chars, give up diff --git a/src/fix/main.cpp b/src/fix/main.cpp index 06a3a88a2..3ceaf8a44 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -17,9 +17,10 @@ #include "platform.hpp" #include "version.hpp" -#define UNSPECIFIED 0x200 // Should not be in byte range +static constexpr uint16_t UNSPECIFIED = 0x200; +static_assert(UNSPECIFIED > 0xFF, "UNSPECIFIED should not be in byte range!"); -#define BANK_SIZE 0x4000 +static constexpr off_t BANK_SIZE = 0x4000; // Short options static char const *optstring = "Ccf:i:jk:L:l:m:n:Op:r:st:Vv"; @@ -383,12 +384,12 @@ static MbcType parseMBC(char const *name) { // Read "additional features" uint8_t features = 0; -#define RAM (1 << 7) -#define BATTERY (1 << 6) -#define TIMER (1 << 5) -#define RUMBLE (1 << 4) -#define SENSOR (1 << 3) -#define MULTIRUMBLE (1 << 2) + static constexpr uint8_t RAM = 1 << 7; + static constexpr uint8_t BATTERY = 1 << 6; + static constexpr uint8_t TIMER = 1 << 5; + static constexpr uint8_t RUMBLE = 1 << 4; + static constexpr uint8_t SENSOR = 1 << 3; + static constexpr uint8_t MULTIRUMBLE = 1 << 2; for (;;) { // Trim off trailing whitespace @@ -740,12 +741,12 @@ static uint8_t const nintendoLogo[] = { }; static uint8_t fixSpec = 0; -#define FIX_LOGO (1 << 7) -#define TRASH_LOGO (1 << 6) -#define FIX_HEADER_SUM (1 << 5) -#define TRASH_HEADER_SUM (1 << 4) -#define FIX_GLOBAL_SUM (1 << 3) -#define TRASH_GLOBAL_SUM (1 << 2) +static constexpr uint8_t FIX_LOGO = 1 << 7; +static constexpr uint8_t TRASH_LOGO = 1 << 6; +static constexpr uint8_t FIX_HEADER_SUM = 1 << 5; +static constexpr uint8_t TRASH_HEADER_SUM = 1 << 4; +static constexpr uint8_t FIX_GLOBAL_SUM = 1 << 3; +static constexpr uint8_t TRASH_GLOBAL_SUM = 1 << 2; static enum { DMG, BOTH, CGB } model = DMG; // If DMG, byte is left alone static char const *gameID = nullptr; @@ -896,11 +897,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize) if (gameID) overwriteBytes( - rom0, - 0x13F, - reinterpret_cast(gameID), - gameIDLen, - "manufacturer code" + rom0, 0x13F, reinterpret_cast(gameID), gameIDLen, "manufacturer code" ); if (model != DMG) diff --git a/src/link/assign.cpp b/src/link/assign.cpp index 0cfc5a8d9..35810c24f 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -328,9 +328,9 @@ static void placeSection(Section §ion) { ); } -#define BANK_CONSTRAINED (1 << 2) -#define ORG_CONSTRAINED (1 << 1) -#define ALIGN_CONSTRAINED (1 << 0) +static constexpr uint8_t BANK_CONSTRAINED = 1 << 2; +static constexpr uint8_t ORG_CONSTRAINED = 1 << 1; +static constexpr uint8_t ALIGN_CONSTRAINED = 1 << 0; static std::deque
unassignedSections[1 << 3]; /* diff --git a/src/link/output.cpp b/src/link/output.cpp index f1bfe94ea..45bb93e25 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -20,7 +20,7 @@ #include "link/section.hpp" #include "link/symbol.hpp" -#define BANK_SIZE 0x4000 +static constexpr size_t BANK_SIZE = 0x4000; FILE *outputFile; FILE *overlayFile; diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index 5a03ff965..b8f6c15fd 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -201,7 +201,8 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector fatal(&where, lineNo, "Unknown endianness type '%c'", line[0]); } -#define ADDR_SIZE 3 + static constexpr uint8_t ADDR_SIZE = 3; + if (line[1] != '0' + ADDR_SIZE) fatal(&where, lineNo, "Unknown or unsupported address size '%c'", line[1]); diff --git a/test/asm/ff00+c.asm b/test/asm/ff00+c.asm index f03f3fc23..4ef5c574d 100644 --- a/test/asm/ff00+c.asm +++ b/test/asm/ff00+c.asm @@ -1,5 +1,5 @@ SECTION "test", ROM0[0] ldh [ $ff00 + c ], a - ; 257 spaces exceeds both LEXER_BUF_SIZE (42) and uint8_t limit (255) + ; 257 spaces exceeds both LEXER_BUF_SIZE (64) and uint8_t limit (255) ldh [ $ff00 + c ], a ldh [ $ff00 + c ], a diff --git a/test/gfx/randtilegen.cpp b/test/gfx/randtilegen.cpp index e4e06a0ff..089de8241 100644 --- a/test/gfx/randtilegen.cpp +++ b/test/gfx/randtilegen.cpp @@ -217,8 +217,8 @@ static void write_image( } static void generate_random_image(char const *filename) { -#define MIN_TILES_PER_SIDE 3 -#define MAX_TILES ((MIN_TILES_PER_SIDE + 7) * (MIN_TILES_PER_SIDE + 7)) + static constexpr uint8_t MIN_TILES_PER_SIDE = 3; + static constexpr uint8_t MAX_TILES = (MIN_TILES_PER_SIDE + 7) * (MIN_TILES_PER_SIDE + 7); Attributes attributes[MAX_TILES]; unsigned char tileData[MAX_TILES][8][8]; uint8_t width = getRandomBits(3) + MIN_TILES_PER_SIDE,