commit b0dcec26045df46a260b24c74680d3c0c0fcbee7
parent cb3cf75cb1b204679c9a679108866ab94624dbf8
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 20 Nov 2024 11:15:06 -0800
Fix and refactor file interpretation
Diffstat:
3 files changed, 60 insertions(+), 70 deletions(-)
diff --git a/src/forth.f b/src/bootstrap.fs
diff --git a/src/forth.s b/src/forth.s
@@ -263,10 +263,11 @@ accept_impl:
defword "refill", refill, 0x238BAA91
jal source_id
jal fetch
- jal q_branch
- .int _refill_tib
+ beqz w, _refill_tib
_refill_fib:
+ jal to_ret
jal fib
+ jal from_ret
jal read_line
jal q_branch
.int _refill_failed
@@ -276,6 +277,7 @@ _refill_fib:
jal branch
.int _refill_success
_refill_tib:
+ pop w
jal tib
jal accept
jal dup
@@ -565,9 +567,13 @@ _interpret_parse_area_empty:
pop w
exit
+defword "bootstrap", bootstrap, 0x15C160D9
+ push w
+ li w, 1
+ jal evaluate
+ exit
+
defword "evaluate", evaluate, 0xACE4360A
- jal lit
- .int 1
jal source_id
jal store
_evaluate_loop:
@@ -1200,70 +1206,58 @@ defword "(;does)", does_internal, 0xED4B7678
.int latest, fetch, lit, code_offset, plus, store
exit
-defcode "read-char", read_char, 0xF07E2044
- jal _read_char_impl
- push w
- next
-
-_read_char_impl:
- la w, _bootstrap
- load_cell w, 0(w)
- la x, _bootstrap_len
- load_cell x, 0(x)
- sub x, x, w
- la y, _bootstrap_offset
- load_cell y, 0(y)
- add w, w, y
- sub x, x, y
- beqz x, _read_char_eof
+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
+ # len = min(buffer, file)
+ jal min
- lb w, 0(w)
+ # 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
- la x, _bootstrap_offset
- store_cell y, 0(x)
- ret
-_read_char_eof:
- mv w, zero
- ret
-
-defword "buffer-open", buffer_open, 0x79AAD9CA
- # ( addr len -- addr end-addr cur-addr )
- .int over, plus, over
- exit
-
-defword "buffer-close", buffer_close, 0x42B7429E
- # ( open-buffer -- len )
- .int nip, swap, minus
- exit
-
-defword "buffer-emit", buffer_emit, 0xEA240555
- # ( open-buffer char -- open-buffer )
- .int over, store, lit, 1, plus
- exit
-
-defword "buffer-full?", buffer_full_q, 0x070A2E6C
- # ( open-buffer -- flag )
- .int two_dup, greater_than # TODO: >=
+ 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
-defword "read-line", read_line, 0xAF1308A2
- .int buffer_open
-_read_line_loop:
- .int read_char
- .int dup, lit, 0x0A, not_equal, q_branch, _read_line_newline
- .int dup, lit, 0x00, not_equal, q_branch, _read_line_eof
- .int buffer_emit
- .int buffer_full_q, q_branch, _read_line_buffer_full
- .int branch, _read_line_loop
-_read_line_newline:
- #.int drop, buffer_close, lit, -1, exit
-_read_line_eof:
- .int drop, buffer_close, dup, q_branch, _read_line_eof_fail
- #.int lit, -1, exit
-_read_line_eof_fail:
- #.int lit, 0, exit
-_read_line_buffer_full:
- #.int buffer_close, lit, 0, 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
.section ".text"
program:
@@ -1287,10 +1281,6 @@ word_buffer: .space 255
.balign cell
_meta: .int __meta_start
-_bootstrap: .int __file_start
-_bootstrap_len: .int __file_len
-_bootstrap_offset: .int 0
-
_tib: .space 255
_tib_len: .int 255
diff --git a/src/riscv32-virt.ld b/src/riscv32-virt.ld
@@ -1,6 +1,6 @@
TARGET(binary)
-INPUT("src/forth.f")
+INPUT("src/bootstrap.fs")
OUTPUT_FORMAT("elf32-littleriscv", "elf32-littleriscv", "elf32-littleriscv")
OUTPUT_ARCH(riscv)
@@ -17,7 +17,7 @@ SECTIONS {
. = ORIGIN(RAM);
.text : { *(.text.boot); *(.text); *(.text.*) } >RAM
- .text : { __file_start = .; "src/forth.f" ; __file_len = . - __file_start; } >RAM
+ .text : { __file_start = .; "src/bootstrap.fs" ; __file_end = .; } >RAM
.rodata : { *(.rodata) } >RAM
. = ALIGN(4);
__here_start = .;