merged dict with heap

This commit is contained in:
2021-11-15 23:07:46 +01:00
parent 964aec76c8
commit c2cba4c057
6 changed files with 134 additions and 108 deletions
+5 -11
View File
@@ -7,6 +7,8 @@
#ifndef FORTH_FH_MEM_H
#define FORTH_FH_MEM_H
void fh_align(struct fh_thread_s *fh);
void fh_setbase(struct fh_thread_s *fh, uint32_t base);
enum fh_error fh_fetch(struct fh_thread_s *fh, uint32_t addr, uint32_t *dst);
@@ -26,16 +28,8 @@ void fh_heap_write(struct fh_thread_s *fh, uint32_t addr, const void *src, uint3
enum fh_error fh_heap_put(struct fh_thread_s *fh, const void *src, uint32_t len);
void fh_heap_copy(struct fh_thread_s *fh, uint32_t addr, uint32_t srcaddr, uint32_t len);
static inline char *fh_str_at(struct fh_thread_s *fh, uint32_t addr) {
return (char *) &fh->heap[addr];
}
static inline struct fh_instruction_s *fh_instr_at(struct fh_thread_s *fh, uint32_t addr) {
return (void *) &fh->heap[addr];
}
static inline struct fh_word_s *fh_word_at(struct fh_thread_s *fh, uint32_t addr) {
return (struct fh_word_s *) &fh->heap[addr];
}
char *fh_str_at(struct fh_thread_s *fh, uint32_t addr);
struct fh_instruction_s *fh_instr_at(struct fh_thread_s *fh, uint32_t addr);
struct fh_word_s *fh_word_at(struct fh_thread_s *fh, uint32_t addr);
#endif //FORTH_FH_MEM_H
+18 -15
View File
@@ -94,11 +94,17 @@ enum fh_substate {
FH_SUBSTATE_MAX,
};
/** Indicates that this is a built-in instruction and not a word call */
#define WORDFLAG_BUILTIN 0x01
/** Indicates that this instruction should always be treated as interpreted */
#define WORDFLAG_IMMEDIATE 0x02
/** Word struct as they are stored in the dictionary */
struct fh_word_s {
uint32_t index;
/** Linked list pointer to previous word */
uint32_t previous;
/** Word name */
char name[MAX_NAME_LEN];
char name[MAX_NAME_LEN]; // XXX this wastes RAM!
/**
* Handler function.
* Builtin functions use pre-defined native handlers.
@@ -106,17 +112,14 @@ struct fh_word_s {
* bytecode at 'start' address of the compile-memory area.
*/
word_exec_t handler;
/** Indicates that this is a built-in instruction and not a word call */
bool builtin;
/** Indicates that this instruction should always be treated as interpreted */
bool immediate;
uint32_t flags;
/** Start address in case of user words, or param for builtins */
union {
uint32_t start;
uint32_t param;
};
uint32_t param;
};
#define MAGICADDR_DICTFIRST 0xFFFFFFFFULL
#define DICTWORD_SIZE sizeof(struct fh_word_s)
/**
* Forth runtime instance - state variables and memory areas.
*
@@ -124,6 +127,8 @@ struct fh_word_s {
* to a shared pointer if multi-threading and synchronization is added.
*/
struct fh_thread_s {
// TODO stacks could live on heap too and share space with some other structures
/** Data stack */
uint32_t data_stack[DATA_STACK_DEPTH];
size_t data_stack_top;
@@ -140,10 +145,8 @@ struct fh_thread_s {
/** Pointer into the compile buffer for execution */
uint32_t execptr;
/** Word dict */
struct fh_word_s dict[DICT_SIZE];
uint32_t dict_top;
/** Address of the last dict word */
uint32_t dict_last;
/** Forth state */
enum fh_state state;
@@ -193,6 +196,6 @@ enum fh_error fh_handle_ascii_word(
size_t wordlen
);
enum fh_error fh_handle_word(struct fh_thread_s *fh, const struct fh_word_s *w);
enum fh_error fh_handle_word(struct fh_thread_s *fh, uint32_t addr);
#endif //FORTH_FH_RUNTIME_H