diff --git a/Makefile b/Makefile index 71dce00..cef251f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ -CFLAGS := -O3 -ggdb -g3 -Wall -Wextra -I. +CFLAGS := -O3 -ggdb -g3 -Wall -Wextra -I. \ + -DFOCI_DATA_STACK_SIZE=12 \ + -DFOCI_RETURN_STACK_SIZE=12 all: x86-64 diff --git a/foci.c b/foci.c index 77b4b67..2488c9c 100644 --- a/foci.c +++ b/foci.c @@ -18,20 +18,17 @@ #define END_OF(arr) ((arr) + (sizeof((arr)) / sizeof(*(arr)))) -static intptr_t dstack[8]; -static intptr_t **rstack[8]; -static intptr_t dict[100] = { - 0, 10, 0 -}; -#define state dict[0] -#define BASE dict[1] -#define LATEST dict[2] -#define BEGIN dict[3] -static intptr_t *here = &BEGIN; +static intptr_t STATE; +static intptr_t BASE; +static intptr_t LATEST; + +static intptr_t dstack[FOCI_DATA_STACK_SIZE]; +static intptr_t **rstack[FOCI_RETURN_STACK_SIZE]; +static intptr_t *dictmem; +static intptr_t *here; static char *in; - -static intptr_t * saved_sp; -static intptr_t *** saved_rp; +static intptr_t *saved_sp; +static intptr_t ***saved_rp; NAKED void enter(void) { *--rp = ++pp; pp = (intptr_t **)*pp; goto *(*pp); } NAKED void push(void) { *--sp = (intptr_t)*++pp; NEXT; } @@ -118,23 +115,23 @@ N(xor, "xor", &w_or) { sp[1] ^= sp[0]; ++sp; NEXT; } N(eq, "=", &w_xor) { sp[1] = sp[0] == sp[1]; ++sp; NEXT; } N(lt, "<", &w_eq) { sp[1] = sp[0] > sp[1]; ++sp; NEXT; } N(compname, "_c", &w_lt) ; -C(intr, "[", &w_compname) { state = 0; NEXT; } -N(comp, "]", &w_intr) { state = -1; NEXT; } +C(intr, "[", &w_compname) { STATE = 0; NEXT; } +N(comp, "]", &w_intr) { STATE = -1; NEXT; } N(comma, ",", &w_comp) { *here++ = *sp++; NEXT; } -W(cell, "cell", &w_comma) LIT(sizeof(*sp)), END -W(cellp, "cell+", &w_cell) FTH(cell), add, END -W(cells, "cells", &w_cellp) FTH(cell), mul, END -W(dict, "_d", &w_cells) LIT(dict), END -W(base, "base", &w_dict) LIT(&BASE), END -W(here, "here", &w_base) LIT(&here), peek, END -W(allot, "allot", &w_here) LIT(&here), FTH(addto), END -W(latest, "latest", &w_allot) LIT(&LATEST), peek, END -W(negate, "negate", &w_latest) LIT(-1), mul, END -W(invert, "invert", &w_negate) LIT(-1), xor, END -W(dec, "1-", &w_invert) LIT(1), sub, END -W(inc, "1+", &w_dec) LIT(1), add, END -W(aligned, "aligned", &w_inc) LIT(sizeof(*sp) - 1), add, LIT(~(sizeof(*sp) - 1)), - and, END +W(cell, "cell", &w_comma) LIT(sizeof(*sp)), END +W(cellp, "cell+", &w_cell) FTH(cell), add, END +W(cells, "cells", &w_cellp) FTH(cell), mul, END +N(dict, "_d", &w_cells) { *--sp = (intptr_t)dictmem; NEXT; } +W(base, "base", &w_dict) LIT(&BASE), END +W(here, "here", &w_base) LIT(&here), peek, END +W(allot, "allot", &w_here) LIT(&here), FTH(addto), END +W(latest, "latest", &w_allot) LIT(&LATEST), peek, END +W(negate, "negate", &w_latest) LIT(-1), mul, END +W(invert, "invert", &w_negate) LIT(-1), xor, END +W(dec, "1-", &w_invert) LIT(1), sub, END +W(inc, "1+", &w_dec) LIT(1), add, END +W(aligned, "aligned", &w_inc) LIT(sizeof(*sp) - 1), add, LIT(~(sizeof(*sp) - 1)), + and, END W(align, "align", &w_aligned) FTH(here), FTH(aligned), LIT(&here), poke, END W(colon, ":", &w_align) FTH(here), LIT(0), comma, FTH(latest), comma, compname, FTH(allot), FTH(align), dup, @@ -166,7 +163,7 @@ N(popr2, "2r>", &w_pushr2) { *--sp = (intptr_t)rp[1]; *--sp = (intptr_t)rp[0]; rp += 2; NEXT; } #define LATEST_INIT &w_popr2 -void enter_forth(const void * const ptr) +void enter_forth(const void *ptr) { STASH; sp = saved_sp; @@ -244,7 +241,7 @@ static void parse_word(const char *start, const char *end) const char *nend; intptr_t n = parse_number(start, &nend, BASE); if (nend == end) { - if (state) { + if (STATE) { *here++ = (intptr_t)push; *here++ = n; } else { @@ -256,7 +253,7 @@ static void parse_word(const char *start, const char *end) foci_putchar(*err++); } } else { - if (state && !(l->attr & ATTR_IMMEDIATE)) { + if (STATE && !(l->attr & ATTR_IMMEDIATE)) { if (!(l->attr & ATTR_NATIVE)) { *here++ = (intptr_t)enter; *here++ = (intptr_t)l->body; @@ -289,12 +286,16 @@ void interpret(void) } } -void init(void) +void init(intptr_t *dictmemm) { + dictmem = dictmemm; + STATE = 0; + BASE = 10; + LATEST = (intptr_t)LATEST_INIT; + here = dictmem; + in = 0; saved_sp = END_OF(dstack); saved_rp = END_OF(rstack); - - LATEST = (intptr_t)LATEST_INIT; } int depth(void) @@ -304,7 +305,7 @@ int depth(void) int compiling(void) { - return state; + return STATE; } void define(struct word_t *w) diff --git a/foci.h b/foci.h index 9c21c27..b666b96 100644 --- a/foci.h +++ b/foci.h @@ -90,7 +90,7 @@ register intptr_t tmp asm("r7"); extern void foci_putchar(int); extern int foci_getchar(void); -void init(void); +void init(intptr_t *dictmem); int depth(void); int compiling(void); void interpret(void); diff --git a/msp430/main.c b/msp430/main.c index fd0489f..9d9cc83 100644 --- a/msp430/main.c +++ b/msp430/main.c @@ -2,6 +2,8 @@ #include "foci.h" +static intptr_t dict[100]; + int main() { WDTCTL = WDTPW | WDTHOLD; @@ -18,7 +20,7 @@ int main() UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; - init(); + init(dict); foci_putchar('o'); foci_putchar('k'); diff --git a/x86/main.c b/x86/main.c index e6230de..1415cc2 100644 --- a/x86/main.c +++ b/x86/main.c @@ -18,19 +18,11 @@ #include -void foci_putchar(int ch) -{ - putchar(ch); -} - -int foci_getchar(void) -{ - return getchar(); -} +static intptr_t dict[8192]; int main() { - init(); + init(dict); for (;;) { interpret(); @@ -40,3 +32,13 @@ int main() return 0; } +void foci_putchar(int ch) +{ + putchar(ch); +} + +int foci_getchar(void) +{ + return getchar(); +} +