Skip to content

Commit

Permalink
Replace some #define with constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Jan 4, 2025
1 parent 5fb5a2a commit 2828ec0
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion include/asm/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions src/asm/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <unistd.h>
#endif

#include "helpers.hpp" // assume, QUOTEDSTRLEN
#include "helpers.hpp"
#include "util.hpp"

#include "asm/fixpoint.hpp"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -720,7 +720,7 @@ int LexerState::peekChar() {
auto &cbuf = content.get<BufferedContent>();
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<uint8_t>(cbuf.buf[cbuf.offset]);
}
Expand Down Expand Up @@ -748,11 +748,11 @@ int LexerState::peekCharAhead() {
return static_cast<uint8_t>(view.span.ptr[view.offset + distance]);
} else {
auto &cbuf = content.get<BufferedContent>();
assume(distance < LEXER_BUF_SIZE);
assume(distance < ARRAY_SIZE(cbuf.buf));
if (cbuf.size <= distance)
cbuf.refill();
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
Expand Down
35 changes: 16 additions & 19 deletions src/fix/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -896,11 +897,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize)

if (gameID)
overwriteBytes(
rom0,
0x13F,
reinterpret_cast<uint8_t const *>(gameID),
gameIDLen,
"manufacturer code"
rom0, 0x13F, reinterpret_cast<uint8_t const *>(gameID), gameIDLen, "manufacturer code"
);

if (model != DMG)
Expand Down
6 changes: 3 additions & 3 deletions src/link/assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ static void placeSection(Section &section) {
);
}

#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<Section *> unassignedSections[1 << 3];

/*
Expand Down
2 changes: 1 addition & 1 deletion src/link/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/link/sdas_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
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]);

Expand Down
2 changes: 1 addition & 1 deletion test/asm/ff00+c.asm
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/gfx/randtilegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2828ec0

Please sign in to comment.