Skip to content

Commit

Permalink
Use a constexpr instead of #define LEXER_BUF_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Jan 4, 2025
1 parent 4b4d393 commit b716289
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions include/asm/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`

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 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

0 comments on commit b716289

Please # to comment.