Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use constexpr instead of #define #1598

Merged
merged 3 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion include/asm/fstack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct FileStackNode {
std::string const &dump(uint32_t curLineNo) const;
};

#define DEFAULT_MAX_DEPTH 64
extern size_t maxRecursionDepth;

struct MacroArgs;
Expand Down
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
2 changes: 1 addition & 1 deletion src/asm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int main(int argc, char *argv[]) {
opt_P(0);
opt_Q(16);
sym_SetExportAll(false);
uint32_t maxDepth = DEFAULT_MAX_DEPTH;
uint32_t maxDepth = 64;
char const *dependFileName = nullptr;
std::unordered_map<std::string, std::vector<StateFeature>> stateFileSpecs;
std::string newTarget;
Expand Down
2 changes: 1 addition & 1 deletion src/asm/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void out_WriteObject() {
// Also write symbols that weren't written above
sym_ForEach(registerUnregisteredSymbol);

fprintf(file, RGBDS_OBJECT_VERSION_STRING);
fputs(RGBDS_OBJECT_VERSION_STRING, file);
putLong(RGBDS_OBJECT_REV, file);

putLong(objectSymbols.size(), file);
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/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
int matchedElems;

if (fscanf(file, RGBDS_OBJECT_VERSION_STRING "%n", &matchedElems) == 1
&& matchedElems != strlen(RGBDS_OBJECT_VERSION_STRING))
&& matchedElems != QUOTEDSTRLEN(RGBDS_OBJECT_VERSION_STRING))
errx("%s: Not a RGBDS object file", fileName);

verbosePrint("Reading object file %s\n", fileName);
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
Loading