forth-riscv

My forth
git clone git://git.electrosoup.com/forth-riscv
Log | Files | Refs

commit 7c564c2bdfec3ad170e5fab00f810266b7324eb1
parent deeb1b70ee97718303cf23f56947f8c5a7acda12
Author: Christian Ermann <christianermann@gmail.com>
Date:   Tue,  3 Dec 2024 22:43:17 -0800

Refactor 'skip-while', 'skip-until', and 'parse'

Diffstat:
Msrc/forth.s | 125++++++++++++++++++++++++++++++++++---------------------------------------------
1 file changed, 54 insertions(+), 71 deletions(-)

diff --git a/src/forth.s b/src/forth.s @@ -392,87 +392,70 @@ defcode "fib", fib, 0xBCE49236 load_cell w, 0(w) next -defword "skip-while", skip_while, 0xBBFD4B86 +defcode "skip-while", skip_while, 0xBBFD4B86 # ( addr1 len1 -- addr2 len2 ) + lw x, 0*cell(psp) + blez w, _skip_while_done + la y, _delimiter + lb y, 0(y) _skip_while_loop: - jal dup - jal q_branch - .int _skip_while_done - jal over - jal char_fetch - jal delimiter - jal fetch - jal equal - jal q_branch - .int _skip_while_done - jal lit - .int 1 - jal apply_offset - jal branch - .int _skip_while_loop + lb t0, 0(x) + bne t0, y, _skip_while_done + addi x, x, 1 + addi w, w, -1 + bgtz w, _skip_while_loop _skip_while_done: - exit + sw x, 0*cell(psp) + next -defword "skip-until", skip_until, 0x661A5D67 - # ( addr1 len1 -- addr2 len2 ) +defcode "skip-until", skip_until, 0x661D5D67 + # (addr1 len1 -- addr2 len2 ) + lw x, 0*cell(psp) + blez w, _skip_until_done + la y, _delimiter + lb y, 0(y) _skip_until_loop: - jal dup - jal q_branch - .int _skip_until_done - jal over - jal char_fetch - jal delimiter - jal fetch - jal not_equal - jal q_branch - .int _skip_until_done - jal lit - .int 1 - jal apply_offset - jal branch - .int _skip_until_loop + lb t0, 0(x) + beq t0, y, _skip_until_done + addi x, x, 1 + addi w, w, -1 + bgtz w, _skip_until_loop _skip_until_done: - exit - -defword "apply-offset", apply_offset, 0x605143A5 - # ( addr1 len1 offset -- addr2 len2 ) - jal tuck - jal minus - jal to_ret - jal plus - jal from_ret - exit - -defword "parse-offset", parse_offset, 0x9E5C3F80 - # ( addr1 len1 -- offset ) - jal lit - .int 1 - jal min - jal plus - jal source - jal drop - jal minus - exit + sw x, 0*cell(psp) + next defword "parse", parse, 0x423B42EC - jal delimiter - jal store - jal source - jal source_offset - jal fetch - jal apply_offset + # ( delimiter -- addr len ) + # set delimiter + la x, _delimiter + sw w, 0(x) + # load source + la x, _tick_source + lw w, 0*cell(x) + lw x, 1*cell(x) + # apply offset + la y, _source_offset + lw y, 0*cell(y) + add x, x, y + sub w, w, y + # find start of token + push x jal skip_while - jal over - jal to_ret + lw x, 0*cell(psp) + push_ret x + # find end of token jal skip_until - jal two_dup - jal parse_offset - jal source_offset - jal store - jal drop - jal from_ret - jal tuck - jal minus + # compute offset + lw w, 0*cell(psp) + la x, _tick_source + lw x, 1*cell(x) + sub y, w, x + la x, _source_offset + sw y, 0(x) + # set outputs + pop_ret x + sw x, 0*cell(psp) + sub w, w, x exit defword "parse-word", parse_word, 0xB218226F