| @@ -40,24 +40,32 @@ coding standard · 39 unmodified lines |
| 40 | 40 | .. fn ; |
| 41 | 41 | |
| 42 | 42 | Heap memory map: uses 8 pages at the start, will increase by *2 when filled up |
| 43 | | −Variables |
| 44 | | − system variables |
| 43 | +H0: variables |
| 45 | 44 | heap start, heapstart, also in H0 |
| 46 | 45 | heap size, heapsize |
| 47 | 46 | forth stack pointer, forthsp |
| 48 | | − dictionary top, dtop |
| 47 | + dictionary pointer, Dp |
| 48 | + latest dictionary entry, Dtop |
| 49 | + need this as the system definitions and |
| 50 | + user definitions are not continuous |
| 49 | 51 | error string buffer 128 bytes |
| 50 | 52 | word buffer 512 bytes |
| 51 | | − tib, text input buffer 1024 bytes (until the next page?) |
| 52 | | −User dictionary upto 6 pages from the start |
| 53 | | −Return stack 1 page (4096 bytes, BY2PG, 512 entries) at FFSTART |
| 53 | +User dictionary upto pages from the start |
| 54 | 54 | | |
| 55 | 55 | | |
| 56 | 56 | v (grows downwards) |
| 57 | + ^ (grows upwards) |
| 58 | + | |
| 59 | + | |
| 57 | 60 | Parameter stack 1 page (BY2PG, 512 entries) at FFEND-4096 |
| 61 | +tib, text input buffer 1024 bytes (until the next page?) |
| 62 | + | |
| 63 | + | |
| 64 | + v (grows downwards) |
| 58 | 65 | ^ (grows upwards) |
| 59 | 66 | | |
| 60 | 67 | | |
| 68 | +Return stack 1 page (4096 bytes, BY2PG, 512 entries) at FFSTART |
| 61 | 69 | SSTACK_END = FORTHEND |
| 62 | 70 | */ |
| 63 | 71 | |
| @@ -71,36 +79,45 @@ SSTACK_END = FORTHEND · 7 unmodified lines |
| 71 | 79 | #define PSTACK_SIZE BY2PG |
| 72 | 80 | #define RSTACK_SIZE BY2PG |
| 73 | 81 | |
| 82 | +/* |
| 83 | + * user table at the start unlike in Starting Forth as it will be |
| 84 | + * easy to get to the variables with an offset |
| 85 | + */ |
| 74 | 86 | #define HEAPSTART (0ull) |
| 75 | | −#define HEAPEND (HEAPSTART+8) |
| 76 | | −#define FORTHSP (HEAPSTART+16) |
| 77 | | −#define DTOP (HEAPSTART+24) |
| 87 | +#define HEAPEND (HEAPSTART+(BY2WD*1)) |
| 88 | +#define FORTHSP (HEAPSTART+(BY2WD*2)) |
| 89 | +#define DP (HEAPSTART+(BY2WD*3)) /* cannot use H as it is nil in inferno |
| 90 | + next available cell for the dictionary */ |
| 91 | +#define DTOP (HEAPSTART+(BY2WD*4)) |
| 78 | 92 | /* variables used by the core words */ |
| 79 | | −#define TOIN (HEAPSTART+32) |
| 80 | | −#define TOLIMIT (HEAPSTART+40) |
| 81 | | −#define FINDADR (HEAPSTART+48) |
| 82 | | −#define BLK (HEAPSTART+56) |
| 83 | | −#define ARGS (HEAPSTART+64) |
| 84 | | −#define IOBUF (HEAPSTART+72) |
| 85 | | −#define SEARCHLEN (HEAPSTART+80) |
| 86 | | −#define BASE (HEAPSTART+88) |
| 87 | | −#define TONUM (HEAPSTART+96) |
| 88 | | −#define STATE (HEAPSTART+104) |
| 89 | | −#define ABORTVEC (HEAPSTART+112) |
| 90 | | −#define SOURCEBUF (HEAPSTART+120) |
| 91 | | −#define WORDBUF (HEAPSTART+128) |
| 92 | | −#define INFD (HEAPSTART+136) |
| 93 | | −#define OUTFD (HEAPSTART+144) |
| 94 | | −#define ERRFD (HEAPSTART+152) |
| 95 | | − |
| 96 | | −#define ERRSTR (HEAPSTART+160) |
| 97 | | −#define WORDB (HEAPSTART+288) /* word buffer */ |
| 98 | | −#define TIB (HEAPSTART+792) /* text input buffer */ |
| 99 | | −#define DICTIONARY (HEAPSTART+2048) |
| 93 | +#define TOIN (HEAPSTART+(BY2WD*5)) |
| 94 | +#define TOLIMIT (HEAPSTART+(BY2WD*6)) |
| 95 | +#define FINDADR (HEAPSTART+(BY2WD*7)) |
| 96 | +#define BLK (HEAPSTART+(BY2WD*8)) |
| 97 | +#define ARGS (HEAPSTART+(BY2WD*9)) |
| 98 | +#define IOBUF (HEAPSTART+(BY2WD*10)) |
| 99 | +#define SEARCHLEN (HEAPSTART+(BY2WD*11)) |
| 100 | +#define BASE (HEAPSTART+(BY2WD*12)) |
| 101 | +#define TONUM (HEAPSTART+(BY2WD*13)) |
| 102 | +#define STATE (HEAPSTART+(BY2WD*14)) |
| 103 | +#define ABORTVEC (HEAPSTART+(BY2WD*15)) |
| 104 | +#define SOURCEBUF (HEAPSTART+(BY2WD*16)) |
| 105 | +#define WORDBUF (HEAPSTART+(BY2WD*17)) |
| 106 | +#define INFD (HEAPSTART+(BY2WD*18)) |
| 107 | +#define OUTFD (HEAPSTART+(BY2WD*19)) |
| 108 | +#define ERRFD (HEAPSTART+(BY2WD*20)) |
| 109 | + |
| 110 | +#define ERRSTR (HEAPSTART+(BY2WD*32)) |
| 111 | +#define WORDB (HEAPSTART+(BY2WD*160)) /* word buffer */ |
| 112 | + |
| 113 | +#define DICTIONARY (HEAPSTART+2048) /* dictionary */ |
| 100 | 114 | #define DICTIONARY_END (HEAPSTART+(6*BY2PG)) |
| 101 | | −#define RSTACK (HEAPSTART+(6*BY2PG)) |
| 102 | | −#define PSTACK_END (RSTACK+(2*BY2PG)) |
| 103 | | −#define FORTHEND PSTACK_END |
| 115 | +#define PSTACK (HEAPSTART+(6*BY2PG)) |
| 116 | +#define PSTACK_END (HEAPSTART+(7*BY2PG)) |
| 117 | +#define TIB (HEAPSTART+(7*BY2PG)) /* text input buffer */ |
| 118 | +#define RSTACK (HEAPSTART+(8*BY2PG)) |
| 119 | +#define RSTACK_END (HEAPSTART+(9*BY2PG)) |
| 120 | +#define FORTHEND RSTACK_END |
| 104 | 121 | #define HEAPSIZE FORTHEND |
| 105 | 122 | |
| 106 | 123 | #define LAST $centry_c_boot(SB) /* last defined word, should generate this */ |
| @@ -123,10 +140,11 @@ TEXT forthmain(SB), 1, $-4 /* _main(SB), 1, $-4 without the libc */ · 16 unmodified lines |
| 123 | 140 | MOVQ RARG, H0 /* start of heap memory */ |
| 124 | 141 | |
| 125 | 142 | MOVQ H0, RSP |
| 126 | | − ADDQ $RSTACK, RSP /* return stack pointer, reset */ |
| 143 | + ADDQ $RSTACK_END, RSP /* return stack pointer, reset */ |
| 127 | 144 | |
| 128 | 145 | MOVQ H0, PSP |
| 129 | | − ADDQ $FORTHEND, PSP /* parameter stack pointer - stack setup, clear */ |
| 146 | + ADDQ $PSTACK_END, PSP /* parameter stack pointer - stack setup, clear */ |
| 147 | + MOVQ PSP, 16(H0) /* parameter stack pointer store, for forth to c */ |
| 130 | 148 | |
| 131 | 149 | MOVQ H0, TOP |
| 132 | 150 | ADDQ $HEAPSTART, TOP |
| @@ -134,8 +152,10 @@ TEXT forthmain(SB), 1, $-4 /* _main(SB), 1, $-4 without the libc */ · 1 unmodified line |
| 134 | 152 | ADDQ $(HEAPSIZE-1), TOP |
| 135 | 153 | MOVQ TOP, 8(H0) /* heap end */ |
| 136 | 154 | |
| 137 | | − MOVQ PSP, 16(H0) /* parameter stack pointer */ |
| 138 | | − MOVQ $centry_c_boot(SB), 24(H0) /* Last dictionary entry address */ |
| 155 | + MOVQ H0, TOP |
| 156 | + ADDQ $DICTIONARY, TOP |
| 157 | + MOVQ TOP, 24(H0) /* dictionary pointer */ |
| 158 | + MOVQ $centry_c_boot(SB), 24(H0) /* Latest dictionary entry address */ |
| 139 | 159 | |
| 140 | 160 | /* execute boot */ |
| 141 | 161 | MOVQ $centry_c_boot(SB), IP |
| @@ -162,33 +182,34 @@ Assume IP = 8 · 20 unmodified lines |
| 162 | 182 | MOVQ r, (PSP) |
| 163 | 183 | #define POP(r) MOVQ (PSP), r; \ |
| 164 | 184 | ADDQ $8, PSP |
| 185 | +#define RPUSH(r) SUBQ $8, RSP; \ |
| 186 | + MOVQ r, (RSP) |
| 187 | +#define RPOP(r) MOVQ (RSP), r; \ |
| 188 | + ADDQ $8, RSP |
| 165 | 189 | |
| 166 | 190 | NEXT |
| 167 | 191 | |
| 168 | 192 | TEXT reset(SB), 1, $-4 |
| 169 | 193 | MOVQ H0, RSP |
| 170 | | − ADDQ $RSTACK, RSP |
| 194 | + ADDQ $RSTACK_END, RSP |
| 171 | 195 | NEXT |
| 172 | 196 | |
| 173 | 197 | TEXT clear(SB), 1, $-4 |
| 174 | 198 | MOVQ H0, PSP |
| 175 | | − ADDQ $FFEND, PSP |
| 199 | + ADDQ $PSTACK_END, PSP |
| 176 | 200 | NEXT |
| 177 | 201 | |
| 178 | 202 | TEXT colon(SB), 1, $-4 |
| 179 | | − MOVQ IP,(RSP) |
| 180 | | − ADDQ $8, RSP |
| 203 | + RPUSH(IP) |
| 181 | 204 | LEAQ 8(W), IP |
| 182 | 205 | NEXT |
| 183 | 206 | |
| 184 | 207 | TEXT exitcolon(SB), 1, $-4 |
| 185 | | − SUBQ $8, RSP |
| 186 | | − MOVQ (RSP), IP |
| 208 | + RPOP(IP) |
| 187 | 209 | NEXT |
| 188 | 210 | |
| 189 | 211 | TEXT dodoes(SB), 1, $-4 /* ( -- a ) */ |
| 190 | | − MOVQ IP,(RSP) |
| 191 | | − ADDQ $8,RSP |
| 212 | + RPUSH(IP) |
| 192 | 213 | MOVQ 8(W),IP |
| 193 | 214 | PUSH(TOP) |
| 194 | 215 | LEAQ 16(W), TOP |
| @@ -306,25 +327,23 @@ doloop1: · 111 unmodified lines |
| 306 | 327 | NEXT |
| 307 | 328 | |
| 308 | 329 | TEXT doploop(SB), 1, $-4 /* ( n -- ) */ |
| 309 | | − ADDQ TOP, -16(RSP) |
| 330 | + ADDQ TOP, 16(RSP) |
| 310 | 331 | POP(TOP) |
| 311 | 332 | JMP doloop1 |
| 312 | 333 | |
| 313 | | −TEXT rfetch(SB), 1, $-4 /* ( -- n ) */ |
| 334 | +TEXT rfetch(SB), 1, $-4 /* ( -- n ) no change in RSP */ |
| 314 | 335 | PUSH(TOP) |
| 315 | | − MOVQ -8(RSP), TOP |
| 316 | | − NEXT |
| 317 | | − |
| 318 | | −TEXT rpush(SB), 1, $-4 /* ( n -- ) */ |
| 319 | | − MOVQ TOP,(RSP) |
| 320 | | − POP(TOP) |
| 321 | | − ADDQ $8,RSP |
| 336 | + MOVQ (RSP), TOP |
| 322 | 337 | NEXT |
| 323 | 338 | |
| 324 | 339 | TEXT rpop(SB), 1, $-4 /* ( -- n ) */ |
| 325 | 340 | PUSH(TOP) |
| 326 | | − SUBQ $8, RSP |
| 327 | | − MOVQ (RSP), TOP |
| 341 | + RPOP(TOP) |
| 342 | + NEXT |
| 343 | + |
| 344 | +TEXT rpush(SB), 1, $-4 /* ( n -- ) */ |
| 345 | + RPUSH(TOP) |
| 346 | + POP(TOP) |
| 328 | 347 | NEXT |
| 329 | 348 | |
| 330 | 349 | TEXT i(SB), 1, $-4 /* ( -- n ) */ |
| @@ -526,7 +545,7 @@ TEXT cas(SB), 1, $-4 /* ( a old new -- f ) */ · 195 unmodified lines |
| 526 | 545 | |
| 527 | 546 | TEXT s0(SB), 1, $-4 /* S0 needs a calculation to come up with the value */ |
| 528 | 547 | MOVQ H0, TOP |
| 529 | | − ADDQ $FORTHEND, TOP |
| 548 | + ADDQ $PSTACK_END, TOP |
| 530 | 549 | NEXT |
| 531 | 550 | |
| 532 | 551 | /* store the forth sp here when going to C */ |
| @@ -545,7 +564,8 @@ TEXT forthsp(SB), 1, $-4 · 12 unmodified lines |
| 545 | 564 | VARIABLE(Tib, $TIB) |
| 546 | 565 | VARIABLE(Wordb, $WORDB) |
| 547 | 566 | VARIABLE(Hzero, $HEAPSTART) |
| 548 | | −VARIABLE(Dp, $DTOP) |
| 567 | +VARIABLE(Dp, $DP) |
| 568 | +VARIABLE(Dtop, $DTOP) |
| 549 | 569 | VARIABLE(toIn, $TOIN) |
| 550 | 570 | VARIABLE(toLimit, $TOLIMIT) |
| 551 | 571 | VARIABLE(Findadr, $FINDADR) |