forth-riscv

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

commit 9bf77f0cd30845c8d018242ee6ca2dbe3627ba09
parent d03eb65da9ad16272c07b03e1577f535f26c5109
Author: Christian Ermann <christianermann@gmail.com>
Date:   Sat, 26 Oct 2024 11:55:01 -0700

Add 'interpret', 'branch', and 'quit'

Diffstat:
Mforth.s | 46++++++++++++++++++++++++++++++++++++----------
1 file changed, 36 insertions(+), 10 deletions(-)

diff --git a/forth.s b/forth.s @@ -9,7 +9,9 @@ #define psp sp #define rsp s2 -#define cell 4 +#define cell 4 +#define dcell 8 + #define flag_offset 4 #define name_len_offset 5 #define name_offset 6 @@ -45,7 +47,7 @@ .endm .macro push_ret reg - store_cell \reg -cell(rsp) + store_cell \reg, -cell(rsp) addi rsp, rsp, -cell .endm @@ -72,13 +74,12 @@ name_\label: code_\label: # 8. This is where our assembly code will go. .endm -.macro defword name, name_length, flags, label +.macro defword name, name_length, flags, label, last .section ".rodata" .balign cell .globl name_\label name_\label: - .int link - .equ link, name_\label + .int name_\last .byte \flags .byte \name_length .ascii "\name" @@ -88,6 +89,11 @@ name_\label: .int docol .endm +docol: + push_ret ip + addi ip, w, cell + NEXT + .equ name_null, 0 defcode "exit", 4, 0, exit, null @@ -191,7 +197,7 @@ to_cfa_impl: add t0, t0, t1 add w, w, t0 addi w, w, 3 - andi w, w, 0xFFFFFFF4 + andi w, w, 0xFFFFFFFC ret defcode "execute", 7, 0, execute, to_cfa @@ -200,24 +206,44 @@ defcode "execute", 7, 0, execute, to_cfa jr x # 'next' should be called by the executed word +defcode "interpret", 9, 0, interpret, execute + jal word_impl + jal find_impl + beqz w, _not_found + jal to_cfa_impl + load_cell x, 0(w) + jr x +_not_found: + next + +defcode "branch", 6, 0, branch, interpret + load_cell t0, 0(ip) + add ip, ip, t0 + next + +defword "quit", 4, 0, quit, branch + .int interpret + .int branch + .int -dcell + .section ".rodata" program: .int type - .int word - .int find - .int to_cfa - .int execute + .int quit version_string: .ascii "soup forth rv32\n" version_string_len = (. - version_string) word_buffer: .space 255 +return_stack: .space 512 +return_stack_top: latest: .int name_find .section ".text.boot" start: la psp, __stacktop + la rsp, return_stack_top la ip, program push_addr version_string