commit a89381df54a7bf1c25fd87cb298997ec806019a2
parent 4bfa38cbe0a168cc617bb1469da5b8d688a74016
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Tue, 21 Apr 2026 11:32:49 -0700
lisp.M1: fix BLTZ refactor regressions in _start and read/write_file_all
The branch-zero refactor in 4bfa38c dropped the `LI_R1 %0` that set up
r1 before `BLT_R0_R1` — switching that branch to `BLTZ_R0` (zero-compare)
meant r1 was no longer zeroed, but the subsequent `BEQ_R0_R1` in both
read_file_all and write_file_all still read r1. Since P1_SYSCALL on
aarch64/riscv64 restores r1 to its pre-syscall value (fd), the loop
was checking `n == fd` instead of `n == 0` and never terminating.
Switch both tails to `BEQZ_R0`.
The _start r6/r7 preservation block was placed after SYS_CLOSE, so
`MOV_R7_R0` captured close's return value (0) instead of bytes_read.
Move the capture + store before the close syscall.
Diffstat:
1 file changed, 12 insertions(+), 20 deletions(-)
diff --git a/src/lisp.M1 b/src/lisp.M1
@@ -125,15 +125,20 @@ DEFINE ZERO32 '0000000000000000000000000000000000000000000000000000000000000000'
&err_src_too_big
P1_BEQ_R0_R1
- ## Publish src_base/src_len so the reader can walk the source.
+ ## Publish src_base/src_len so the reader can walk the source,
+ ## and park (src_buf, bytes_read) in callee-saved r6/r7 so the
+ ## tuple survives primitive registration + prelude eval down to
+ ## the final eval_source CALL below. Capture r0 into r7 NOW —
+ ## SYS_CLOSE below will overwrite r0 with its own return value.
+ P1_MOV_R7_R0 ## r7 = bytes_read (captured pre-close)
+ P1_LI_R6
+ &src_buf ## r6 = &src_buf
P1_LI_R1
&src_len
- P1_ST_R0_R1_0
+ P1_ST_R7_R1_0
P1_LI_R1
&src_base
- P1_LI_R2
- &src_buf
- P1_ST_R2_R1_0
+ P1_ST_R6_R1_0
## close(fd). r4 still holds fd; SYSCALL clobbers r0 only.
P1_LI_R0
@@ -141,19 +146,6 @@ DEFINE ZERO32 '0000000000000000000000000000000000000000000000000000000000000000'
P1_MOV_R1_R4
P1_SYSCALL
- ## Preserve the script buffer tuple across primitive registration
- ## and prelude evaluation. r6/r7 are callee-saved, so every CALL
- ## below preserves them.
- P1_LI_R6
- &src_buf
- P1_MOV_R7_R0
- P1_LI_R1
- &src_len
- P1_ST_R7_R1_0
- P1_LI_R1
- &src_base
- P1_ST_R6_R1_0
-
## Align heap_next up to 8 (pair/closure tags need 8-aligned raw
## pointers — see step 2).
P1_LI_R4
@@ -330,7 +322,7 @@ DEFINE ZERO32 '0000000000000000000000000000000000000000000000000000000000000000'
P1_BLTZ_R0
P1_LI_BR
&read_file_all_done
- P1_BEQ_R0_R1
+ P1_BEQZ_R0
P1_ADD_R5_R5_R0 ## buf += n
P1_SUB_R6_R6_R0 ## cap -= n
@@ -371,7 +363,7 @@ DEFINE ZERO32 '0000000000000000000000000000000000000000000000000000000000000000'
P1_BLTZ_R0
P1_LI_BR
&write_file_all_err
- P1_BEQ_R0_R1
+ P1_BEQZ_R0
P1_ADD_R5_R5_R0
P1_SUB_R6_R6_R0