new words

This commit is contained in:
2021-11-13 20:10:05 +01:00
parent e63c2287af
commit bc6d7e5d25
12 changed files with 629 additions and 65 deletions
+2
View File
@@ -16,4 +16,6 @@
#define HEAP_SIZE (1024*1024)
#define MAXLINE 65535
#define CELL 4
#endif //FORTH_FH_CONFIG_H
+2
View File
@@ -23,6 +23,8 @@ enum fh_error {
FH_ERR_INVALID_STATE,
FH_ERR_INTERNAL,
FH_ERR_UNKNOWN_WORD,
FH_ERR_ILLEGAL_FETCH,
FH_ERR_DIV_BY_ZERO,
FH_ERR_MAX,
};
+2
View File
@@ -7,6 +7,8 @@
#ifndef FORTH_FH_GLOBALS_H
#define FORTH_FH_GLOBALS_H
#include <stdbool.h>
/** Forth runtime global state */
struct fh_global_s {
/** Verbose logging enabled */
+8
View File
@@ -7,6 +7,14 @@
#ifndef FORTH_FH_MEM_H
#define FORTH_FH_MEM_H
enum fh_error fh_fetch(struct fh_thread_s *fh, uint32_t addr, uint32_t *dst);
enum fh_error fh_fetch_char(struct fh_thread_s *fh, uint32_t addr, char *dst);
enum fh_error fh_store(struct fh_thread_s *fh, uint32_t addr, uint32_t val);
enum fh_error fh_store_char(struct fh_thread_s *fh, uint32_t addr, char val);
enum fh_error fh_heap_reserve(
struct fh_thread_s *fh,
size_t len,
+13 -7
View File
@@ -19,7 +19,7 @@ struct fh_instruction_s;
struct fh_thread_s;
/** Word handler typedef */
typedef enum fh_error (*word_exec_t)(struct fh_thread_s *fh);
typedef enum fh_error (*word_exec_t)(struct fh_thread_s *fh, const struct fh_word_s *w);
/** Bytecode instruction type marker */
enum fb_instruction_kind {
@@ -62,6 +62,7 @@ _Static_assert(sizeof(struct fh_instruction_s) % 4 == 0, "Instruction struct is
enum fh_state {
FH_STATE_INTERPRET = 0,
FH_STATE_COMPILE,
FH_STATE_QUIT,
FH_STATE_SHUTDOWN,
FH_STATE_MAX,
};
@@ -93,8 +94,11 @@ struct fh_word_s {
/** Indicates that this instruction should always be treated as interpreted,
* in practice this is only used for `;` */
bool immediate;
/** Start address in case of user words */
uint32_t start;
/** Start address in case of user words, or param for builtins */
union {
uint32_t start;
uint32_t param;
};
};
/**
@@ -139,9 +143,8 @@ struct fh_thread_s {
/** Forth sub-state */
enum fh_substate substate;
/** Word currently being executed - a pointer is placed here
* before calling the handler */
struct fh_word_s *exec_word;
/** The numeric base register */
uint32_t base;
};
enum fh_error fh_add_word(const struct fh_word_s *w, struct fh_thread_s *fh);
@@ -149,10 +152,13 @@ enum fh_error fh_add_word(const struct fh_word_s *w, struct fh_thread_s *fh);
void fh_setstate(struct fh_thread_s *fh, enum fh_state state, enum fh_substate substate);
void fh_setsubstate(struct fh_thread_s *fh, enum fh_substate substate);
enum fh_error w_user_word(struct fh_thread_s *fh);
enum fh_error w_user_word(struct fh_thread_s *fh, const struct fh_word_s *w);
/* if the return address is this, we should drop back to interactive mode */
// SFR and magic addresses are "negative"
#define MAGICADDR_INTERACTIVE 0xFFFFFFFFULL
#define MAGICADDR_BASE 0xFFFBA5EFULL
/** Get a value rounded up to multiple of word size */
#define WORDALIGNED(var) (((var) + 3) & ~3)