forth-riscv

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

commit ee25a1856fdff412fadab7ac8829631722e5ccd8
parent 38cc7345ce6b626acc2be223267ff7c997130c4f
Author: Christian Ermann <christianermann@gmail.com>
Date:   Wed,  6 Nov 2024 20:19:24 -0800

Add 'defconst' and 'defvar' macros

Diffstat:
Mforth.s | 80++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 41 insertions(+), 39 deletions(-)

diff --git a/forth.s b/forth.s @@ -123,6 +123,22 @@ code_\label: defhead "\name", \label, \hash, docol, flags=\flags .endm +.macro defvar name, label, hash, flags=0, initial=0 + defmeta "\name", \label + defhead "\name", \label, \hash, dovar, flags=\flags + .globl _\label +_\label: + .int \initial +.endm + +.macro defconst name, label, hash, flags=0, value=0 + defmeta "\name", \label + defhead "\name", \label, \hash, docon, flags=\flags + .globl _\label +_\label: + .int \value +.endm + docol: push_ret ip addi ip, w, cell @@ -143,6 +159,22 @@ defcode "exit", exit, 0xCDED1A85 pop_ret ip next +# ----------------------------------------------------------------------------- +# constants and variables +# ----------------------------------------------------------------------------- + +defconst "sp0", sp_top, 0x0C41B8B2, value=__stacktop +defconst "rp0", rp_top, 0x8AD91EDD, value=__stacktop_ret + +defvar "state", state, 0x783132F6, initial=state_immediate +defvar "dp", dp, 0x491CB0B9, initial=__here_start +defvar "latest", latest, 0x41704246, initial=name_read_line +defvar "base", base, 0x3DDC94D8, initial=10 + +# ----------------------------------------------------------------------------- +# input and output +# ----------------------------------------------------------------------------- + defcode "type", type, 0x5127F14D pop a1 # length pop a0 # address @@ -560,21 +592,9 @@ defcode "cell+", cell_plus, 0xB14A8CBA # dictionary management # ----------------------------------------------------------------------------- -defcode "dp", dp, 0x491CB0B9 - la w, _here - push w - next - -defcode "here", here, 0x213B65CB - la w, _here - load_cell x, 0(w) - push x - next - -defcode "latest", latest, 0x41704246 - la w, _latest - push w - next +defword "here", here, 0x213B65CB + .int dp, fetch + .int exit defcode "lit", lit, 0x404CD5B6 load_cell w, 0(ip) @@ -608,24 +628,17 @@ defword "c,", char_comma, 0xA32A0A5A .int lit, 1, allot # increment 'here' .int exit -defcode "align", align, 0x602C63DE - la w, _here - load_cell x, 0(w) - addi x, x, 3 - andi x, x, 0xFFFFFFFC - store_cell x, 0(w) - next +defword "align", align, 0x602C63DE + .int dp, dup, fetch + .int lit, 3, plus + .int lit, -4, and_ + .int swap, store + .int exit # ----------------------------------------------------------------------------- # stack manipulation # ----------------------------------------------------------------------------- -defcode "sp0", sp_top, 0x0C41B8B2 - # ( -- addr ) - la w, __stacktop - push w - next - defcode "sp@", sp_fetch, 0xFC419F82 # ( -- addr ) mv w, psp @@ -638,12 +651,6 @@ defcode "sp!", sp_store, 0x1D41D375 mv psp, w next -defcode "rp0", rp_top, 0x8AD91EDD - # ( -- addr ) - la w, __stacktop_ret - push w - next - defcode "rp@", rp_fetch, 0x7AD905AD # ( -- addr ) mv w, rsp @@ -992,13 +999,8 @@ _okay: word_buffer: .space 255 .balign cell -_latest: prev_link -_here: .int __here_start _meta: .int __meta_start -_state: .int state_immediate -_base: .int 10 - _bootstrap: .int __file_start _bootstrap_len: .int __file_len _bootstrap_offset: .int 0