From d79898fa201b334766d853fea9b0be1a4dedc7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 15 Nov 2021 23:25:56 +0100 Subject: [PATCH] remove some direct access to stack arrays --- include/fh_config.h | 4 +--- src/fh_builtins.c | 44 +++++++++++++++++++++++++------------------- src/fh_stack.c | 2 ++ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/include/fh_config.h b/include/fh_config.h index e451208..4c8bf3d 100644 --- a/include/fh_config.h +++ b/include/fh_config.h @@ -7,14 +7,12 @@ #ifndef FORTH_FH_CONFIG_H #define FORTH_FH_CONFIG_H -#define CONTROL_STACK_DEPTH 1024 #define DATA_STACK_DEPTH 1024 #define RETURN_STACK_DEPTH 1024 #define MAX_NAME_LEN 32 -#define DICT_SIZE 1024 #define HEAP_SIZE (1024*1024) #define MAXLINE 65535 -#define PAD_OFFSET 340 +#define PAD_OFFSET 340 // why? copied from somewhere #define WORDBUF_SIZE 128 #define INPUT_BUFFER_SIZE 256 diff --git a/src/fh_builtins.c b/src/fh_builtins.c index 673df8a..88ec39d 100644 --- a/src/fh_builtins.c +++ b/src/fh_builtins.c @@ -538,18 +538,17 @@ static enum fh_error w_two_swap(struct fh_thread_s *fh, const struct fh_word_s * { (void) w; enum fh_error rv; - if (fh->data_stack_top < 4) { - LOG("DS two-swap UNDERFLOW"); - return FH_ERR_DS_UNDERFLOW; - } - uint32_t n = fh->data_stack_top - 4; - uint32_t a = fh->data_stack[n]; - uint32_t b = fh->data_stack[n + 1]; - fh->data_stack[n] = fh->data_stack[n + 2]; - fh->data_stack[n + 1] = fh->data_stack[n + 3]; - fh->data_stack[n + 2] = a; - fh->data_stack[n + 3] = b; + uint32_t a, b, c, d; + TRY(ds_pop(fh, &a)); + TRY(ds_pop(fh, &b)); + TRY(ds_pop(fh, &c)); + TRY(ds_pop(fh, &d)); + + TRY(ds_push(fh, b)); + TRY(ds_push(fh, a)); + TRY(ds_push(fh, d)); + TRY(ds_push(fh, c)); return FH_OK; } @@ -719,14 +718,6 @@ static enum fh_error wp_putc(struct fh_thread_s *fh, const struct fh_word_s *w) return FH_OK; } -static enum fh_error w_space(struct fh_thread_s *fh, const struct fh_word_s *w) -{ - (void) w; - (void) fh; - FHPRINT(" "); - return FH_OK; -} - static enum fh_error w_debug_dump(struct fh_thread_s *fh, const struct fh_word_s *w) { (void) w; @@ -996,6 +987,20 @@ static enum fh_error w_see(struct fh_thread_s *fh, const struct fh_word_s *w) return FH_OK; } +static enum fh_error w_pad(struct fh_thread_s *fh, const struct fh_word_s *w) +{ + (void) w; + enum fh_error rv; + uint32_t addr = fh->here + PAD_OFFSET; + if (addr + 84 >= HEAP_SIZE) { + LOGE("Heap overflow, PAD is too small!"); + return FH_ERR_HEAP_FULL; + } + + TRY(ds_push(fh, addr)); + return FH_OK; +} + static enum fh_error wp_const(struct fh_thread_s *fh, const struct fh_word_s *w) { enum fh_error rv; @@ -1177,6 +1182,7 @@ enum fh_error register_builtin_words(struct fh_thread_s *fh) {"hex", wp_setbase, 0, 16}, {"base", wp_const, 0, MAGICADDR_BASE}, {"here", wp_const, 0, MAGICADDR_HERE}, + {"pad", w_pad, 0, 0}, {"false", wp_const, 0, 0}, {"true", wp_const, 0, 0xFFFFFFFF}, {"+", w_plus, 0, 0}, diff --git a/src/fh_stack.c b/src/fh_stack.c index 449b2ea..19ba599 100644 --- a/src/fh_stack.c +++ b/src/fh_stack.c @@ -4,6 +4,8 @@ #include "fh_stack.h" #include "fh_print.h" +// TODO stacks should grow down, not up! + enum fh_error ds_roll(struct fh_thread_s *fh, int n) { if (fh->data_stack_top <= n) {