diff options
author | Alexis Lockwood | 2021-07-03 20:13:59 -0400 |
---|---|---|
committer | Alexis Lockwood | 2021-07-03 20:13:59 -0400 |
commit | a1d4108a1e8e0f1d44ea30fe3bc6c6ba9f189979 (patch) | |
tree | 588b3f39d0292564e78b64781789608fc2c10242 | |
parent | 5d3df0725b85abbb526c8635ce5fb8fd4c9404df (diff) |
Clean up the fetch interface
This removes a relatively expensive compare in a hot spot, and some
dodgy integer arith
-rw-r--r-- | src/ls_goto.c | 2 | ||||
-rw-r--r-- | src/ls_internal.c | 8 | ||||
-rw-r--r-- | src/ls_internal.h | 9 | ||||
-rw-r--r-- | src/ls_kw_impl_PRINT.c | 2 | ||||
-rw-r--r-- | src/ls_lex.c | 14 |
5 files changed, 16 insertions, 19 deletions
diff --git a/src/ls_goto.c b/src/ls_goto.c index 1bc74f9..f941620 100644 --- a/src/ls_goto.c +++ b/src/ls_goto.c @@ -161,7 +161,7 @@ static bool _rewind_line(ls_t * self) for (;;) { - unsigned char ch = ls_fetch(self, pc); + unsigned char ch = ls_fetch_at(self, pc); if (ch == '\n') { diff --git a/src/ls_internal.c b/src/ls_internal.c index 78bab75..659ec91 100644 --- a/src/ls_internal.c +++ b/src/ls_internal.c @@ -116,14 +116,12 @@ void ls_throw_err(ls_t * self, ls_error_t e) } } -ls_uchar ls_fetch_rel(ls_t * self, int offset) +ls_uchar ls_fetch(ls_t * self) { - if (self->_pc + offset > UINT16_MAX || self->_pc + offset < 0) - ls_throw_err(self, LS_INTERNAL_ERROR); - return ls_fetch(self, (ls_addr_t)(self->_pc + offset)); + return ls_fetch_at(self, self->_pc); } -ls_uchar ls_fetch(ls_t * self, ls_addr_t pc) +ls_uchar ls_fetch_at(ls_t * self, ls_addr_t pc) { int rc = self->fetcher(self->fetcher_arg, pc); if (rc == -LS_NO_MORE_PROGRAM) diff --git a/src/ls_internal.h b/src/ls_internal.h index 524f5b4..6a66de0 100644 --- a/src/ls_internal.h +++ b/src/ls_internal.h @@ -122,14 +122,13 @@ ls_kw_t ls_convert_kw(char const * word); /// is LS_OK, no throw occurs. void ls_throw_err(ls_t * self, ls_error_t e); -/// Fetch a character, relative to the current one. Throws if the fetcher -/// returns an error other than LS_NO_PROGRAM. If there is no character, -/// returns nul. -ls_uchar ls_fetch_rel(ls_t * self, int offset); +/// Fetch the current character. Throws if the fetcher returns an error other +/// than LS_NO_PROGRAM. If there is no character, returns nul. +ls_uchar ls_fetch(ls_t * self); /// Fetch a character at a specific location. If there is no character, returns /// nul. -ls_uchar ls_fetch(ls_t * self, ls_addr_t pc); +ls_uchar ls_fetch_at(ls_t * self, ls_addr_t pc); /// Allocate from the pool, throwing LS_OUT_OF_MEMORY if not possible. Always /// returns a valid pointer (or doesn't return) diff --git a/src/ls_kw_impl_PRINT.c b/src/ls_kw_impl_PRINT.c index a854bb3..1294219 100644 --- a/src/ls_kw_impl_PRINT.c +++ b/src/ls_kw_impl_PRINT.c @@ -44,7 +44,7 @@ void ls_kw_fun_PRINT(ls_t * self) i <= self->_token.string[1]; i++) { - unsigned char c = ls_fetch(self, i); + unsigned char c = ls_fetch_at(self, i); fprintf(file, "%c", c); } break; diff --git a/src/ls_lex.c b/src/ls_lex.c index 95c9045..b9370e3 100644 --- a/src/ls_lex.c +++ b/src/ls_lex.c @@ -58,7 +58,7 @@ ls_token_t ls_lex(ls_t * self) bool skip_rem = false; do { - ch[0] = ls_fetch_rel(self, 0); + ch[0] = ls_fetch(self); ch_kind = _ident_ch_kind(ch[0]); self->_pc++; @@ -68,7 +68,7 @@ ls_token_t ls_lex(ls_t * self) skip_rem = false; } while (ch_kind == CH_KIND_SPACE || skip_rem); - ch[1] = ls_fetch_rel(self, 0); + ch[1] = ls_fetch(self); switch (ch_kind) { @@ -126,7 +126,7 @@ ls_token_t ls_lex(ls_t * self) void ls_consume_to_eol(ls_t * self) { for (;;) { - ls_uchar uch = ls_fetch_rel(self, 0); + ls_uchar uch = ls_fetch(self); if (uch == 0 || uch == '\n') return; @@ -284,7 +284,7 @@ static ls_token_t _lex_num(ls_t * self, ls_uchar ch[2]) nch = 1; self->_pc = (ls_addr_t)(self->_pc + nch - 1); - ch[0] = ls_fetch_rel(self, 0); + ch[0] = ls_fetch(self); self->_pc++; } @@ -317,7 +317,7 @@ static ls_token_t _lex_num(ls_t * self, ls_uchar ch[2]) break; } - ch[0] = ls_fetch_rel(self, 0); + ch[0] = ls_fetch(self); self->_pc++; if (digit >= radix) @@ -340,7 +340,7 @@ static ls_token_t _lex_word(ls_t * self, ls_uchar ch[2]) if (i < sizeof(self->_token.word) - 1 && ch[0] <= CHAR_MAX) self->_token.word[i] = (char) ch[0]; - ch[0] = ls_fetch_rel(self, 0); + ch[0] = ls_fetch(self); ch_kind_t ch_kind = _ident_ch_kind(ch[0]); self->_pc++; @@ -408,7 +408,7 @@ static ls_token_t _lex_str(ls_t * self, ls_uchar ch[2]) self->_token.string[0] = self->_pc; for (;;) { - ch[0] = ls_fetch_rel(self, 0); + ch[0] = ls_fetch(self); if (ch[0] == '"') { self->_token.string[1] = (ls_addr_t)(self->_pc - 1); |