diff --git a/ueforth/common/boot.fs b/ueforth/common/boot.fs index 240a91c..e73c1e6 100644 --- a/ueforth/common/boot.fs +++ b/ueforth/common/boot.fs @@ -52,25 +52,29 @@ : 2@ ( a -- lo hi ) dup @ swap cell+ @ ; : 2! ( lo hi a -- ) dup >r cell+ ! r> ! ; -( System Variables ) -: 'tib ( -- a ) 'sys 0 cells + ; -: #tib ( -- a ) 'sys 1 cells + ; -: >in ( -- a ) 'sys 2 cells + ; -: state ( -- a ) 'sys 3 cells + ; -: base ( -- a ) 'sys 4 cells + ; -: 'heap ( -- a ) 'sys 5 cells + ; -: current ( -- a ) 'sys 6 cells + ; -: 'context ( -- a ) 'sys 7 cells + ; : context 'context @ cell+ ; -: 'notfound ( -- a ) 'sys 8 cells + ; - ( Dictionary ) -: here ( -- a ) 'heap @ ; -: allot ( n -- ) 'heap +! ; +: here ( -- a ) 'sys @ ; +: allot ( n -- ) 'sys +! ; : aligned ( a -- a ) cell 1 - dup >r + r> invert and ; : align here aligned here - allot ; : , ( n -- ) here ! cell allot ; : c, ( ch -- ) here c! 1 allot ; +( Constants and Variables ) +: constant ( n "name" -- ) create , does> @ ; +: variable ( "name" -- ) create 0 , ; + +( System Variables ) +: sys: ( a -- a' "name" ) dup constant cell+ ; +'sys sys: 'heap sys: current sys: 'context sys: 'notfound + sys: 'heap-start sys: 'heap-size sys: 'stack-cells + sys: 'boot sys: 'boot-size + sys: 'tib sys: #tib sys: >in + sys: state sys: base + sys: 'argc sys: 'argv sys: 'runner +: context ( -- a ) 'context @ cell+ ; +: remaining ( -- n ) 'heap-start @ 'heap-size @ + 'heap @ - ; + ( Compilation State ) : [ 0 state ! ; immediate : ] -1 state ! ; immediate @@ -112,10 +116,6 @@ : immediate? ( xt -- f ) >flags @ 1 and 0= 0= ; : postpone ' dup immediate? if , else aliteral ['] , , then ; immediate -( Constants and Variables ) -: constant ( n "name" -- ) create , does> @ ; -: variable ( "name" -- ) create 0 , ; - ( Stack Convience ) sp@ constant sp0 rp@ constant rp0 @@ -256,3 +256,6 @@ create input-buffer input-limit allot : quit begin ['] evaluate-buffer catch if 0 state ! sp0 sp! fp0 fp! rp0 rp! ." ERROR" cr then prompt refill drop again ; +: raw-ok ." v{{VERSION}} - rev {{REVISION}}" cr + remaining . ." bytes heap free" cr + prompt refill drop quit ; diff --git a/ueforth/common/core.h b/ueforth/common/core.h index bfe0c4d..81e9449 100644 --- a/ueforth/common/core.h +++ b/ueforth/common/core.h @@ -28,12 +28,18 @@ #endif static struct { + cell_t *heap, **current, ***context, notfound; + cell_t *heap_start; + cell_t heap_size, stack_cells; + const char *boot; + cell_t boot_size; const char *tib; cell_t ntib, tin, state, base; - cell_t *heap, **current, ***context, notfound; int argc; char **argv; cell_t *(*runner)(cell_t *rp); // pointer to forth_run + + // Layout not used by Forth. cell_t *rp; // spot to park main thread cell_t DOLIT_XT, DOFLIT_XT, DOEXIT_XT, YIELD_XT; } g_sys; @@ -191,10 +197,16 @@ static cell_t *forth_run(cell_t *initrp); static void forth_init(int argc, char *argv[], void *heap, const char *src, cell_t src_len) { - g_sys.heap = ((cell_t *) heap) + 4; // Leave a little room. - float *fp = (float *) (g_sys.heap + 1); g_sys.heap += STACK_SIZE; - cell_t *rp = g_sys.heap + 1; g_sys.heap += STACK_SIZE; - cell_t *sp = g_sys.heap + 1; g_sys.heap += STACK_SIZE; + g_sys.heap_start = (cell_t *) heap; + g_sys.heap_size = HEAP_SIZE; + g_sys.stack_cells = STACK_CELLS; + g_sys.boot = src; + g_sys.boot_size = src_len; + + g_sys.heap = g_sys.heap_start + 4; // Leave a little room. + float *fp = (float *) (g_sys.heap + 1); g_sys.heap += STACK_CELLS; + cell_t *rp = g_sys.heap + 1; g_sys.heap += STACK_CELLS; + cell_t *sp = g_sys.heap + 1; g_sys.heap += STACK_CELLS; // FORTH vocabulary *g_sys.heap++ = 0; cell_t *forth = g_sys.heap; diff --git a/ueforth/common/opcodes.h b/ueforth/common/opcodes.h index bf215d7..cb5e5b5 100644 --- a/ueforth/common/opcodes.h +++ b/ueforth/common/opcodes.h @@ -103,5 +103,4 @@ typedef int64_t dcell_t; sp = evaluate1(sp, &tfp); \ fp = tfp; w = *sp--; DROP; if (w) JMPW) \ Y(EXIT, ip = (cell_t *) *rp--) \ - X(";", SEMICOLON, UNSMUDGE(); COMMA(g_sys.DOEXIT_XT); g_sys.state = 0) \ - + X(";", SEMICOLON, UNSMUDGE(); COMMA(g_sys.DOEXIT_XT); g_sys.state = 0) diff --git a/ueforth/common/vocabulary.fs b/ueforth/common/vocabulary.fs index 67373c5..ec0c209 100644 --- a/ueforth/common/vocabulary.fs +++ b/ueforth/common/vocabulary.fs @@ -48,12 +48,14 @@ transfer{ 'context 'notfound notfound immediate? input-buffer ?echo ?arrow. arrow evaluate1 evaluate-buffer - 'sys 'heap aliteral + 'sys 'heap aliteral 'heap-start 'heap-size + 'stack-cells 'boot 'boot-size + 'argc 'argv 'runner leaving( )leaving leaving leaving, (do) (?do) (+loop) parse-quote digit $@ raw.s tib-setup input-limit - [SKIP] [SKIP]' + [SKIP] [SKIP]' raw-ok }transfer forth definitions diff --git a/ueforth/esp32/platform.fs b/ueforth/esp32/platform.fs index 047af44..0516217 100644 --- a/ueforth/esp32/platform.fs +++ b/ueforth/esp32/platform.fs @@ -63,4 +63,4 @@ led OUTPUT pinMode high led pin ( Setup entry ) -: ok ." ESP32forth v{{VERSION}} - rev {{REVISION}}" cr prompt refill drop quit ; +internals : ok ." ESP32forth" raw-ok ; forth diff --git a/ueforth/esp32/template.ino b/ueforth/esp32/template.ino index 65818a8..bfbb185 100644 --- a/ueforth/esp32/template.ino +++ b/ueforth/esp32/template.ino @@ -85,7 +85,7 @@ #include #define HEAP_SIZE (100 * 1024) -#define STACK_SIZE 512 +#define STACK_CELLS 512 #define INTERRUPT_STACK_CELLS 64 // Optional hook to pull in words for userwords.h diff --git a/ueforth/posix/posix.fs b/ueforth/posix/posix.fs index 5f08721..8ed7049 100644 --- a/ueforth/posix/posix.fs +++ b/ueforth/posix/posix.fs @@ -189,4 +189,4 @@ O_RDWR constant r/w forth ( Setup entry ) -: ok ." uEforth v{{VERSION}} - rev {{REVISION}}" cr prompt refill drop quit ; +internals : ok ." uEforth" raw-ok ; forth diff --git a/ueforth/posix/posix_desktop.fs b/ueforth/posix/posix_desktop.fs index 9b8ac6b..4f76cb6 100644 --- a/ueforth/posix/posix_desktop.fs +++ b/ueforth/posix/posix_desktop.fs @@ -13,9 +13,6 @@ \ limitations under the License. ( Arguments ) -internals definitions -: 'argc ( -- a ) 'sys 9 cells + ; -: 'argv ( -- a ) 'sys 10 cells + ; forth definitions internals : argc ( -- n ) 'argc @ ; : argv ( n -- a n ) cells 'argv @ + @ z>s ; diff --git a/ueforth/posix/posix_main.c b/ueforth/posix/posix_main.c index acab5ec..1112356 100644 --- a/ueforth/posix/posix_main.c +++ b/ueforth/posix/posix_main.c @@ -21,7 +21,7 @@ #include "common/calls.h" #define HEAP_SIZE (10 * 1024 * 1024) -#define STACK_SIZE (64 * 1024) +#define STACK_CELLS (8 * 1024) #define PLATFORM_OPCODE_LIST \ Y(DLSYM, tos = (cell_t) dlsym(a1, a0); --sp) \ diff --git a/ueforth/posix/xlib_test.fs b/ueforth/posix/xlib_test.fs index 86f6368..ab74229 100644 --- a/ueforth/posix/xlib_test.fs +++ b/ueforth/posix/xlib_test.fs @@ -47,7 +47,8 @@ create event xevent-size allot display gc white XSetBackground drop display window gc 0 0 width @ 2/ height @ 2/ XFillRectangle drop ; -: de event xevent-size +: handle-event + event xevent-size event c@ . event c@ Expose = if draw @@ -67,5 +68,5 @@ create event xevent-size allot then event c@ MapNotify = if ." MapNotify" then cr ; -: 1e display event XNextEvent drop de ; -: gg begin draw 1e again ; +: do-event display event XNextEvent drop handle-event ; +: gg begin draw do-event again ; diff --git a/ueforth/site/common.html b/ueforth/site/common.html index d74bdc3..ad7c5d6 100644 --- a/ueforth/site/common.html +++ b/ueforth/site/common.html @@ -42,6 +42,7 @@ R| ( string| -- a n ) Creates a temporary counted string ending with | DUMP ( a n -- ) Dump a memory region SEE ( "name" -- ) Attempt to decompile a word VARIABLE ECHO -- Determines if commands are echoed +REMAINING ( -- n ) Bytes remaining in Forth heap.
Vocabularies
diff --git a/ueforth/web/web.template.js b/ueforth/web/web.template.js index 6bd3c6b..129b728 100644 --- a/ueforth/web/web.template.js +++ b/ueforth/web/web.template.js @@ -17,7 +17,7 @@ (function() { const HEAP_SIZE = (1024 * 1024); -const STACK_SIZE = 4096; +const STACK_CELLS = 4096; const boot = ` {{boot}} @@ -133,9 +133,9 @@ function Init() { InitDictionary(); i32[g_sp>>2] = i32[g_heap>>2] + 1; - i32[g_heap>>2] += STACK_SIZE; + i32[g_heap>>2] += STACK_CELLS; i32[g_rp>>2] = i32[g_heap>>2] + 1; - i32[g_heap>>2] += STACK_SIZE; + i32[g_heap>>2] += STACK_CELLS; i32[((i32[g_current]>>2) - 4)>>2] = 1; // Make ; IMMMEDIATE // Do not need DOLIT_XT, DOEXIT_XT, YIELD_XT (do by convention) i32[g_notfound>>2] = Find('DROP'); diff --git a/ueforth/windows/windows.fs b/ueforth/windows/windows.fs index 4822525..0f3e987 100644 --- a/ueforth/windows/windows.fs +++ b/ueforth/windows/windows.fs @@ -160,5 +160,5 @@ r/o w/o or constant r/w forth ( Setup entry ) -: ok ." uEforth v{{VERSION}} - rev {{REVISION}}" cr prompt refill drop quit ; +internals : ok ." uEforth" raw-ok ; forth ' forth ( leave on stack for fini.fs ) diff --git a/ueforth/windows/windows_main.c b/ueforth/windows/windows_main.c index 27bfba1..3865672 100644 --- a/ueforth/windows/windows_main.c +++ b/ueforth/windows/windows_main.c @@ -33,7 +33,7 @@ #include "common/calls.h" #define HEAP_SIZE (10 * 1024 * 1024) -#define STACK_SIZE (64 * 1024) +#define STACK_CELLS (8 * 1024) #define PLATFORM_OPCODE_LIST \ Y(GETPROCADDRESS, \