Replace some #define with constexpr

This commit is contained in:
Rangi42
2025-01-03 21:35:17 -05:00
committed by Rangi
parent a5f12f66bb
commit 4e2464a69d
8 changed files with 38 additions and 40 deletions

View File

@@ -15,7 +15,7 @@
// 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
// buffering performance when it doesn't/can't (e.g. when piping a file into RGBASM). // 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 // 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"); 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 // This caps the size of buffer reads, and according to POSIX, passing more than SSIZE_MAX is UB

View File

@@ -19,7 +19,7 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include "helpers.hpp" // assume, QUOTEDSTRLEN #include "helpers.hpp"
#include "util.hpp" #include "util.hpp"
#include "asm/fixpoint.hpp" #include "asm/fixpoint.hpp"
@@ -500,27 +500,27 @@ BufferedContent::~BufferedContent() {
} }
void BufferedContent::advance() { void BufferedContent::advance() {
assume(offset < LEXER_BUF_SIZE); assume(offset < ARRAY_SIZE(buf));
offset++; offset++;
if (offset == LEXER_BUF_SIZE) if (offset == ARRAY_SIZE(buf))
offset = 0; // Wrap around if necessary offset = 0; // Wrap around if necessary
assume(size > 0); assume(size > 0);
size--; size--;
} }
void BufferedContent::refill() { 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 // 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 the range to fill passes over the buffer wrapping point, we need two reads
if (startIndex + target > LEXER_BUF_SIZE) { if (startIndex + target > ARRAY_SIZE(buf)) {
size_t nbExpectedChars = LEXER_BUF_SIZE - startIndex; size_t nbExpectedChars = ARRAY_SIZE(buf) - startIndex;
size_t nbReadChars = readMore(startIndex, nbExpectedChars); size_t nbReadChars = readMore(startIndex, nbExpectedChars);
startIndex += nbReadChars; startIndex += nbReadChars;
if (startIndex == LEXER_BUF_SIZE) if (startIndex == ARRAY_SIZE(buf))
startIndex = 0; startIndex = 0;
// If the read was incomplete, don't perform a second read // 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) { size_t BufferedContent::readMore(size_t startIndex, size_t nbChars) {
// This buffer overflow made me lose WEEKS of my life. Never again. // 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); ssize_t nbReadChars = read(fd, &buf[startIndex], nbChars);
if (nbReadChars == -1) if (nbReadChars == -1)
@@ -720,7 +720,7 @@ int LexerState::peekChar() {
auto &cbuf = content.get<BufferedContent>(); auto &cbuf = content.get<BufferedContent>();
if (cbuf.size == 0) if (cbuf.size == 0)
cbuf.refill(); cbuf.refill();
assume(cbuf.offset < LEXER_BUF_SIZE); assume(cbuf.offset < ARRAY_SIZE(cbuf.buf));
if (cbuf.size > 0) if (cbuf.size > 0)
return static_cast<uint8_t>(cbuf.buf[cbuf.offset]); return static_cast<uint8_t>(cbuf.buf[cbuf.offset]);
} }
@@ -748,11 +748,11 @@ int LexerState::peekCharAhead() {
return static_cast<uint8_t>(view.span.ptr[view.offset + distance]); return static_cast<uint8_t>(view.span.ptr[view.offset + distance]);
} else { } else {
auto &cbuf = content.get<BufferedContent>(); auto &cbuf = content.get<BufferedContent>();
assume(distance < LEXER_BUF_SIZE); assume(distance < ARRAY_SIZE(cbuf.buf));
if (cbuf.size <= distance) if (cbuf.size <= distance)
cbuf.refill(); cbuf.refill();
if (cbuf.size > distance) if (cbuf.size > distance)
return static_cast<uint8_t>(cbuf.buf[(cbuf.offset + distance) % LEXER_BUF_SIZE]); return static_cast<uint8_t>(cbuf.buf[(cbuf.offset + distance) % ARRAY_SIZE(cbuf.buf)]);
} }
// If there aren't enough chars, give up // If there aren't enough chars, give up

View File

@@ -17,9 +17,10 @@
#include "platform.hpp" #include "platform.hpp"
#include "version.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 // Short options
static char const *optstring = "Ccf:i:jk:L:l:m:n:Op:r:st:Vv"; 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" // Read "additional features"
uint8_t features = 0; uint8_t features = 0;
#define RAM (1 << 7) static constexpr uint8_t RAM = 1 << 7;
#define BATTERY (1 << 6) static constexpr uint8_t BATTERY = 1 << 6;
#define TIMER (1 << 5) static constexpr uint8_t TIMER = 1 << 5;
#define RUMBLE (1 << 4) static constexpr uint8_t RUMBLE = 1 << 4;
#define SENSOR (1 << 3) static constexpr uint8_t SENSOR = 1 << 3;
#define MULTIRUMBLE (1 << 2) static constexpr uint8_t MULTIRUMBLE = 1 << 2;
for (;;) { for (;;) {
// Trim off trailing whitespace // Trim off trailing whitespace
@@ -740,12 +741,12 @@ static uint8_t const nintendoLogo[] = {
}; };
static uint8_t fixSpec = 0; static uint8_t fixSpec = 0;
#define FIX_LOGO (1 << 7) static constexpr uint8_t FIX_LOGO = 1 << 7;
#define TRASH_LOGO (1 << 6) static constexpr uint8_t TRASH_LOGO = 1 << 6;
#define FIX_HEADER_SUM (1 << 5) static constexpr uint8_t FIX_HEADER_SUM = 1 << 5;
#define TRASH_HEADER_SUM (1 << 4) static constexpr uint8_t TRASH_HEADER_SUM = 1 << 4;
#define FIX_GLOBAL_SUM (1 << 3) static constexpr uint8_t FIX_GLOBAL_SUM = 1 << 3;
#define TRASH_GLOBAL_SUM (1 << 2) static constexpr uint8_t TRASH_GLOBAL_SUM = 1 << 2;
static enum { DMG, BOTH, CGB } model = DMG; // If DMG, byte is left alone static enum { DMG, BOTH, CGB } model = DMG; // If DMG, byte is left alone
static char const *gameID = nullptr; static char const *gameID = nullptr;
@@ -896,11 +897,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
if (gameID) if (gameID)
overwriteBytes( overwriteBytes(
rom0, rom0, 0x13F, reinterpret_cast<uint8_t const *>(gameID), gameIDLen, "manufacturer code"
0x13F,
reinterpret_cast<uint8_t const *>(gameID),
gameIDLen,
"manufacturer code"
); );
if (model != DMG) if (model != DMG)

View File

@@ -328,9 +328,9 @@ static void placeSection(Section &section) {
); );
} }
#define BANK_CONSTRAINED (1 << 2) static constexpr uint8_t BANK_CONSTRAINED = 1 << 2;
#define ORG_CONSTRAINED (1 << 1) static constexpr uint8_t ORG_CONSTRAINED = 1 << 1;
#define ALIGN_CONSTRAINED (1 << 0) static constexpr uint8_t ALIGN_CONSTRAINED = 1 << 0;
static std::deque<Section *> unassignedSections[1 << 3]; static std::deque<Section *> unassignedSections[1 << 3];
/* /*

View File

@@ -20,7 +20,7 @@
#include "link/section.hpp" #include "link/section.hpp"
#include "link/symbol.hpp" #include "link/symbol.hpp"
#define BANK_SIZE 0x4000 static constexpr size_t BANK_SIZE = 0x4000;
FILE *outputFile; FILE *outputFile;
FILE *overlayFile; FILE *overlayFile;

View File

@@ -201,7 +201,8 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
fatal(&where, lineNo, "Unknown endianness type '%c'", line[0]); 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) if (line[1] != '0' + ADDR_SIZE)
fatal(&where, lineNo, "Unknown or unsupported address size '%c'", line[1]); fatal(&where, lineNo, "Unknown or unsupported address size '%c'", line[1]);

View File

@@ -1,5 +1,5 @@
SECTION "test", ROM0[0] SECTION "test", ROM0[0]
ldh [ $ff00 + c ], a 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
ldh [ $ff00 + c ], a ldh [ $ff00 + c ], a

View File

@@ -217,8 +217,8 @@ static void write_image(
} }
static void generate_random_image(char const *filename) { static void generate_random_image(char const *filename) {
#define MIN_TILES_PER_SIDE 3 static constexpr uint8_t MIN_TILES_PER_SIDE = 3;
#define MAX_TILES ((MIN_TILES_PER_SIDE + 7) * (MIN_TILES_PER_SIDE + 7)) static constexpr uint8_t MAX_TILES = (MIN_TILES_PER_SIDE + 7) * (MIN_TILES_PER_SIDE + 7);
Attributes attributes[MAX_TILES]; Attributes attributes[MAX_TILES];
unsigned char tileData[MAX_TILES][8][8]; unsigned char tileData[MAX_TILES][8][8];
uint8_t width = getRandomBits(3) + MIN_TILES_PER_SIDE, uint8_t width = getRandomBits(3) + MIN_TILES_PER_SIDE,