diff --git a/include/asm/lexer.hpp b/include/asm/lexer.hpp index a282bbe92..535c15a43 100644 --- a/include/asm/lexer.hpp +++ b/include/asm/lexer.hpp @@ -15,11 +15,11 @@ // 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"); +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 -static_assert(LEXER_BUF_SIZE <= SSIZE_MAX, "Lexer buffer size is too large"); +static_assert(lexer_buf_size <= SSIZE_MAX, "Lexer buffer size is too large"); enum LexerMode { LEXER_NORMAL, @@ -58,7 +58,7 @@ struct ViewedContent { struct BufferedContent { int fd; // File from which to read chars - char buf[LEXER_BUF_SIZE] = {}; // Circular buffer of chars + char buf[lexer_buf_size] = {}; // Circular buffer of chars size_t offset = 0; // Cursor into `buf` size_t size = 0; // Number of "fresh" chars in `buf` 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/test/asm/ff00+c.asm b/test/asm/ff00+c.asm index f03f3fc23..a32f764cb 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