forth-riscv

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

commit 8a5d7259aa0ecfc12fc023f049ac99202078d6de
parent 17d2b9223162bf54a64e828bdd2ef3585161a4f0
Author: Christian Ermann <christianermann@gmail.com>
Date:   Thu, 27 Mar 2025 20:42:21 -0700

Simplify 'bootstrap' and its dependencies

Diffstat:
Msrc/forth.s | 180++++++++++++++++++++++++++++++++++++++-----------------------------------------
Msrc/riscv32-virt.ld | 1-
2 files changed, 86 insertions(+), 95 deletions(-)

diff --git a/src/forth.s b/src/forth.s @@ -263,7 +263,7 @@ defvar "mp", mp, 0x532E0998 .int __meta_start defvar "latest", latest, 0x41704246 - .int name_read_line + .int name_constant defvar "base", base, 0x3DDC94D8 .int 10 @@ -388,11 +388,19 @@ _refill_fail: _refill_dispatch_table: .int accept_tib -defword "source", source, 0x1BCF29D8 +defword "source@", source_fetch, 0x5F1E7A48 jal tick_source jal two_fetch exit +defword "source!", source_store, 0xC01F12FB + # ( addr len -- ) + jal tick_source + jal two_store + la x, _source_offset + sw zero, 0*cell(x) + exit + skip_escape: addi x, x, 1 addi w, w, -1 @@ -910,7 +918,7 @@ _interpret_start: jal match_prefix bnez w, _interpret_prefix _interpret_find_word: - jal drop + pop w jal find beqz w, _interpret_retry _interpret_word_found: @@ -976,35 +984,6 @@ defword "(interpret-execute)", interpret_execute, 0x2477F685 _inner_interpret_done: exit -defword "bootstrap", bootstrap, 0x15C160D9 - push w - li w, 1 - jal evaluate - exit - -defword "run-tests", run_tests, 0xC7C52582 - push w - li w, 2 - jal evaluate - exit - -defword "evaluate", evaluate, 0xACE4360A - jal source_id - jal store -_evaluate_loop: - jal refill - jal q_branch - .int _evaluate_done - jal interpret - jal branch - .int _evaluate_loop -_evaluate_done: - jal lit - .int 0 - jal source_id - jal store - exit - defcode "branch", branch, 0xB6873945 load_cell ra, 0(ra) next @@ -1031,8 +1010,68 @@ defword "quit", quit, 0x47878736 jal okay jal two_fetch jal type - jal branch - .int quit + j quit + +defcode "read-line", read_line, 0xAF1308A2 + # ( src dst n -- len flag ) + lw y, 1*cell(psp) # src + lw x, 0*cell(psp) # dst + addi psp, psp, 1*cell + blez w, _read_line_eof +_read_line_loop: + lb t0, 0(y) + beqz t0, _read_line_eof + sb t0, 0(x) + addi y, y, 1 + addi x, x, 1 + addi w, w, -1 + addi t0, t0, -0x0A + beqz t0, _read_line_nl + bgtz w, _read_line_loop +_read_line_eof: + lw x, 0*cell(psp) + sub x, y, x + sw x, 0*cell(psp) + mv w, zero + next +_read_line_nl: + lw x, 0*cell(psp) + sub x, y, x + sw x, 0*cell(psp) + li w, -1 + next + +defword "tib-source!" tib_source_store, 0x93EE3A0D + # ( len -- ) + jal tib + jal fetch_second + jal swap + jal source_store + exit + +defword "bootstrap", bootstrap, 0x15C160D9 + push w + la w, __file_start +_bootstrap_loop: + jal dup + jal tib + jal two_fetch + jal read_line + jal q_branch + .int _bootstrap_done + jal dup + addi w, w, -1 + jal tib_source_store + jal plus + jal to_ret + jal interpret + jal from_ret + j _bootstrap_loop +_bootstrap_done: + jal tib_source_store + jal drop + jal interpret + exit defcode "abort", abort, 0xA52BCAF9 jal start @@ -1075,6 +1114,16 @@ defcode "2@", two_fetch, 0x3DF21B8F push y next +defcode "@1", fetch_first, 0x9FD8152A + # ( addr -- first-value ) + lw w, 0*cell(w) + next + +defcode "@2", fetch_second, 0x9ED81397 + # ( addr -- second-value ) + lw w, 1*cell(w) + next + defcode "+!", add_store, 0x08DC01D1 pop x load_cell y, 0(w) @@ -1694,63 +1743,6 @@ defword "constant", constant, 0x0691EA25, flags=flag_immediate jal l_bracket exit -defword "read-line", read_line, 0xAF1308A2 # ( addr len id -- ? ) - # index into file table - la x, _file_table - slli w, w, 4 - add x, x, w - push_ret x - # load file information - load_cell y, 1 * cell(x) # addr - load_cell w, 2 * cell(x) # len - load_cell x, 3 * cell(x) # offset - # apply offset - add y, y, x - sub w, w, x - # len = min(buffer, file) - jal min - - # w=maxlen, x=dst, y=src - load_cell x, 0(psp) -1: # loop - lb t0, 0(y) - sb t0, 0(x) - addi t0, t0, -0x0A - beqz t0, 3f - addi y, y, 1 - addi x, x, 1 - addi w, w, -1 - bgez w, 1b -2: # eof or too long - lw y, 0(psp) # dst start - sub x, x, y # dst len - mv w, zero # flag - sw x, 0(psp) - j 4f -3: # newline - lw y, 0(psp) # dst start - sub x, x, y # dst len - li w, -1 # flag - sw x, 0(psp) -4: - pop_ret y - load_cell t0, 3 * cell(y) - addi x, x, 1 - add t0, t0, x - store_cell t0, 3 * cell(y) - exit - -.section ".data" -_file_table: - .int 0, 0, 0, 0 - .int 0x6B09C3CE # bootstrap.fs - .int __file_start # addr - .int __file_end - __file_start # len - .int 0 # offset - .int 0x98CD7A4F - .int __tests_start - .int __tests_end - __tests_start - .int 0 .section ".text" program: @@ -1770,14 +1762,14 @@ _okay_addr: .ascii "ok\n" _okay_len = (. - _okay_addr) +__file_end: + .int 0 + .section ".data" .balign cell _tib_addr: .space 255 _tib_len = (. - _tib_addr) -_fib: - .int 255 - .space 255 .section ".text.boot" .globl start diff --git a/src/riscv32-virt.ld b/src/riscv32-virt.ld @@ -21,7 +21,6 @@ SECTIONS { . = ORIGIN(RAM); .text : { *(.text.boot); *(.text); *(.text.*) } >RAM .text : { __file_start = .; "src/bootstrap.fs" ; __file_end = .; } >RAM - .text : { __tests_start = .; "src/tests.fs" ; __tests_end = .; } >RAM .rodata : { *(.rodata) } >RAM . = ALIGN(4); __here_start = .;