-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenforth.c
2325 lines (2048 loc) · 65.5 KB
/
enforth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2008-2014, Michael Alyn Miller <malyn@strangeGizmo.com>.
* All rights reserved.
* vi:ts=4:sts=4:et:sw=4:sr:et:tw=72:fo=tcrq
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of Michael Alyn Miller nor the names of the
* contributors to this software may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* -------------------------------------
* Includes.
*/
/* ANSI C includes. */
#include <stdlib.h>
#if ENABLE_TRACING
#include <stdio.h>
#endif
#include <stddef.h>
#include <string.h>
/* AVR includes. */
#ifdef __AVR__
#include <avr/pgmspace.h>
#else
#define PROGMEM
#define pgm_read_byte(p) (*(uint8_t*)(p))
#define pgm_read_word(p) (*(int *)(p))
#endif
/* Enforth includes. */
#include "enforth.h"
/* -------------------------------------
* Enforth tokens.
*/
typedef enum EnforthToken
{
#include "enforth_tokens.h"
/* Tokens 0x70-0x7f are reserved for jump labels to the "CFA"
* primitives. The token names themselves do not need to be defined
* because they are never referenced (we're just reserving space in
* the token list and Address Interpreter jump table, in other
* words), but we do list them here in order to make it easier to
* turn raw tokens into enum values in the debugger. */
DOCOLON = 0x70,
DOCOLONROM,
DOCONSTANT,
DOCREATE,
DOVARIABLE,
/* Unused */
/* Unused */
/* Unused */
DOFFI0 = 0x78,
DOFFI1,
DOFFI2,
DOFFI3,
DOFFI4,
DOFFI5,
DOFFI6,
DOFFI7,
} EnforthToken;
/* -------------------------------------
* Enforth constants
*/
/* Some compilers have issues with these things being constants (static
* const int), which is why they are #define'd instead. */
#define kNFAtoCFA (1 /* PSF+namelen */ + 2 /* LFA */)
#define kNFAtoPFA (1 /* PSF+namelen */ + 2 /* LFA */ + 2 /* CFA */)
#define kTaskUserVariableSize 8
#define kTaskReturnStackSize 32
#define kTaskDataStackSize 24
#define kTaskStartToReturnTop ((kEnforthCellSize * kTaskUserVariableSize) + (kEnforthCellSize * (kTaskReturnStackSize - 1)))
#define kTaskStartToDataTop ((kEnforthCellSize * kTaskUserVariableSize) + (kEnforthCellSize * kTaskReturnStackSize) + (kEnforthCellSize * (kTaskDataStackSize - 1)))
/* -------------------------------------
* Enforth globals.
*/
#if ENABLE_TRACING
static int gTraceLevel = 0;
#endif
/* -------------------------------------
* Enforth definitions.
*/
/* This must be stored in near memory on large AVRs since it is accessed
* using instruction space words, all of which require that their target
* addresses be able to fit in a cell (which is 16 bits on the AVR). */
static const int8_t definitions[] PROGMEM = {
#include "enforth_definitions.h"
};
/* -------------------------------------
* Public functions.
*/
void enforth_init(
EnforthVM * const vm,
uint8_t * const dictionary, int dictionary_size,
const EnforthFFIDef * const last_ffi,
int (*keyq)(void), char (*key)(void), void (*emit)(char),
int (*load)(uint8_t*, int), int (*save)(uint8_t*, int))
{
vm->last_ffi = last_ffi;
vm->keyq = keyq;
vm->key = key;
vm->emit = emit;
vm->load = load;
vm->save = save;
vm->dictionary.ram = dictionary;
vm->dictionary_size.u = dictionary_size;
enforth_reset(vm);
}
void enforth_reset(EnforthVM * const vm)
{
/* Initialize the dictionary, which contains the DP, LATEST, and
* LASTTASK cells, and the default task. The user-accessible
* portion of the dictionary starts after that block of data. DP is
* reset to point after that block, LATEST points at ROMDEF_LAST,
* and LASTTASK points to the default task. */
/* TODO The DP and LASTTASK cells should be dictionary-relative so
* that the dictionary can be loaded into RAM at a different
* location across SAVE/LOADs. */
((EnforthCell*)vm->dictionary.ram)[0].ram
= vm->dictionary.ram
+ kEnforthCellSize /* DP */
+ kEnforthCellSize /* LATEST */
+ kEnforthCellSize /* LASTTASK */
+ (kEnforthCellSize * 64); /* Task Control Block */
((EnforthCell*)vm->dictionary.ram)[1].u
= ROMDEF_LAST;
vm->cur_task.ram
= vm->dictionary.ram
+ (kEnforthCellSize * 3);
((EnforthCell*)vm->dictionary.ram)[2].ram
= vm->cur_task.ram;
/* Reset the globals. */
vm->hld = NULL;
vm->state = 0;
/* Reset the task. */
/* TODO These should be dictionary-relative so that they can
* relocate with the dictionary. */
((EnforthCell*)vm->cur_task.ram)[0].u = 0; /* User: PREVTASK */
((EnforthCell*)vm->cur_task.ram)[1].u = 0; /* User: SAVEDSP */
((EnforthCell*)vm->cur_task.ram)[2].u = 10; /* User: BASE */
/* TODO This entire block below isn't really necessary since people
* aren't allowed to call enforth_resume on their own. Instead,
* they should always call enforth_evaluate or enforth_go. But if
* they call enforth_evaluate then enforth_evaluate needs to have an
* IP and RSP to pop from the stack and so enforth_reset needs to
* prepare the stack.
*
* What we really need is some sort of vm->status enum that tells us
* if the VM is halted and can be resumed (and therefore has IP and
* RSP stack items) or has never been run and therefore
* enforth_evaluate doesn't need to clean anything up. */
/* Clear both stacks. */
EnforthCell * sp = (EnforthCell*)(vm->cur_task.ram + kTaskStartToDataTop - kEnforthCellSize);
EnforthCell * rsp = (EnforthCell*)(vm->cur_task.ram + kTaskStartToReturnTop - kEnforthCellSize);
/* Set the IP to the beginning of COLD. */
#ifdef __AVR__
uint8_t* ip = (void *)(0x8000 | (unsigned int)((uint8_t*)definitions + (ROMDEF_COLD&0x3FFF) + kNFAtoPFA));
#else
uint8_t* ip = (uint8_t*)definitions + (ROMDEF_COLD&0x3FFF) + kNFAtoPFA;
#endif
/* Push RSP and IP to the stack. */
(--sp)->ram = (uint8_t*)rsp;
(--sp)->ram = ip;
/* Save the stack pointer. */
/* TODO This should be dictionary-relative so that it can relocate
* with the dictionary. */
((EnforthCell*)vm->cur_task.ram)[1].ram = (uint8_t*)sp;
}
void enforth_evaluate(EnforthVM * const vm, const char * const text)
{
/* Clear the return stack. */
EnforthCell * rsp = (EnforthCell*)(vm->cur_task.ram + kTaskStartToReturnTop - kEnforthCellSize);
/* Push the address of HALT onto the return stack so that we exit
* the interpreter after EVALUATE is done. */
#ifdef __AVR__
(--rsp)->ram = (void *)(0x8000 | (unsigned int)((uint8_t*)definitions + (ROMDEF_HALT&0x3FFF) + kNFAtoPFA));
#else
(--rsp)->ram = (uint8_t*)definitions + (ROMDEF_HALT&0x3FFF) + kNFAtoPFA;
#endif
/* Set the IP to the beginning of EVALUATE. */
#ifdef __AVR__
uint8_t* ip = (void *)(0x8000 | (unsigned int)((uint8_t*)definitions + (ROMDEF_EVALUATE&0x3FFF) + kNFAtoPFA));
#else
uint8_t* ip = (uint8_t*)definitions + (ROMDEF_EVALUATE&0x3FFF) + kNFAtoPFA;
#endif
/* Restore the stack pointer. */
EnforthCell * sp = (EnforthCell*)((EnforthCell*)vm->cur_task.ram)[1].ram;
/* Pop the previous IP and RSP; we're about to replace them. */
++sp; /* IP */
++sp; /* RSP */
/* Push the text and text length onto the stack. */
(--sp)->ram = (uint8_t*)text;
(--sp)->u = strlen(text);
/* Push the new RSP and IP to the stack. */
(--sp)->ram = (uint8_t*)rsp;
(--sp)->ram = ip;
/* Update the saved the stack pointer now that we have modified the
* stack. */
/* TODO This should be dictionary-relative so that it can relocate
* with the dictionary. */
((EnforthCell*)vm->cur_task.ram)[1].ram = (uint8_t*)sp;
/* Resume the interpreter. */
enforth_resume(vm);
}
void enforth_resume(EnforthVM * const vm)
{
register uint8_t *ip;
register uint16_t xt;
register EnforthCell tos;
register EnforthCell *restDataStack; /* Points at the second item on the stack. */
register uint8_t *w;
register EnforthCell *returnTop;
#ifdef __AVR__
register int8_t inProgramSpace;
#endif
#if ENABLE_STACK_CHECKING
/* Check for available stack space and abort with a message if this
* operation would run out of space. */
/* TODO The overflow logic seems slightly too aggressive -- it
* probably needs a "- 1" in there given that we store TOS in a
* register. */
#define CHECK_STACK(numArgs, numResults) \
{ \
if (((EnforthCell*)(vm->cur_task.ram + kTaskStartToDataTop) - restDataStack) < numArgs) { \
goto STACK_UNDERFLOW; \
} else if ((((EnforthCell*)(vm->cur_task.ram + kTaskStartToDataTop) - restDataStack) - numArgs) + numResults > 32) { \
goto STACK_OVERFLOW; \
} \
}
#else
#define CHECK_STACK(numArgs, numResults)
#endif
static const void * const primitive_table[128] PROGMEM = {
#include "enforth_jumptable.h"
/* $70 - $77 */
&&DOCOLON,
&&DOCOLONROM,
&&DOCONSTANT,
&&DOCREATE,
&&DOVARIABLE,
0, /* Unused */
0, /* Unused */
0, /* Unused */
/* $78 - $7F */
&&DOFFI0,
&&DOFFI1,
&&DOFFI2,
&&DOFFI3,
&&DOFFI4,
&&DOFFI5,
&&DOFFI6,
&&DOFFI7,
};
UNPAUSE:
/* Restore the stack pointer. */
restDataStack = (EnforthCell*)((EnforthCell*)vm->cur_task.ram)[1].ram;
/* Pop IP and RSP from the stack. */
ip = (restDataStack++)->ram;
#ifdef __AVR__
if (((unsigned int)ip & 0x8000) != 0)
{
/* This IP address points at a ROM definition; strip off that
* flag as part of popping the address. */
ip = (uint8_t*)((unsigned int)ip & 0x7FFF);
inProgramSpace = -1;
}
else
{
inProgramSpace = 0;
}
#endif
returnTop = (EnforthCell *)(restDataStack++)->ram;
/* Pop TOS into our register. */
tos = *restDataStack++;
/* The inner interpreter. */
for (;;)
{
/* Get the next instruction, which could be one or two bytes
* depending on if this is a Code Primitive (one byte) or a
* Definition (two bytes). The W ("Word") pointer needs to be
* set if this is a Definition. */
uint8_t token;
#ifdef __AVR__
if (inProgramSpace)
{
token = pgm_read_byte(ip++);
}
else
#endif
{
token = *ip++;
}
/* Is this actually a token (< 128)? If so, dispatch the
* token. */
if (token < 128)
{
w = NULL;
goto DISPATCH_TOKEN;
}
else
{
/* Not a token, which means that this is a two-byte XT that
* points at the NFA of the word to be called. */
xt = token << 8;
#ifdef __AVR__
if (inProgramSpace)
{
xt |= pgm_read_byte(ip++);
}
else
#endif
{
xt |= *ip++;
}
DISPATCH_XT:
/* This is a two-byte XT that points at another word. We
* need to find the word that is being targeted by this XT
* and set W to that word's PFA. We then need to dispatch
* to the word's Code Field. The Code Field will almost
* always contain a DO* token, *unless the word was modified
* by a defining word.* In that case, the Code Field will
* contain a full, two-byte XT and that XT will point to the
* runtime behavior of the defining word (the code after
* DOES> in the defining word).
*
* Note that ROM Definitions never use DOES> and so we can
* assume that the Code Field in a ROM Definition will
* always contain a token. */
if ((xt & 0xC000) == 0xC000) /* ROM Definition: 0xCxxx */
{
/* Advance past the empty first byte in the Code Field,
* then read the token in the second byte. */
/* TODO We could probably eliminate the first byte in
* ROM Definitions. */
w = (uint8_t*)((uint8_t*)definitions + (xt & 0x3FFF) + kNFAtoCFA + 1);
#ifdef __AVR__
token = pgm_read_byte(w++);
#else
token = *w++;
#endif
}
else /* User Definition: 0x8xxx */
{
/* Load the Code Field. */
w = (uint8_t*)(vm->dictionary.ram + (xt & 0x3FFF) + kNFAtoCFA);
xt = *w++ << 8;
xt |= *w++;
/* We're done if this is a token. */
if (xt < 0x80)
{
token = xt;
goto DISPATCH_TOKEN;
}
else
{
/* Not a token, which means that this word was
* defined by DOES>; jump to DODOES to perform the
* runtime behavior of the defining word. */
goto DODOES;
}
}
}
/* W now points at the PFA; fall through to dispatch the CFA
* token. */
DISPATCH_TOKEN:
#if ENABLE_TRACING == 2
{
int i;
for (i = 0; i < gTraceLevel; i++)
{
printf(".");
}
printf(" [%02x] ", token);
/* FIXME Needs to get the XT of the token and then use that
* to retrieve the name. */
#if false
if (token < 0x70) /* No names for DO* tokens */
{
const char * curDef = kDefinitionNames;
for (i = 0; i < token; i++)
{
curDef += 1 + (((unsigned int)*curDef) & 0x1f);
}
for (i = 1; i <= (((unsigned int)*curDef) & 0x1f); i++)
{
printf("%c", *(curDef + i));
}
}
#endif
for (i = 0; i < &vm->data_stack[32] - restDataStack - 1; i++)
{
printf(" %x", vm->data_stack[30 - i].u);
}
if ((&vm->data_stack[32] - restDataStack) > 0)
{
printf(" %x", tos.u);
}
printf("\n");
}
#endif
goto *(void *)pgm_read_word(&primitive_table[token]);
#if ENABLE_STACK_CHECKING
STACK_OVERFLOW:
{
if (vm->emit != NULL)
{
vm->emit('\n');
vm->emit('!');
vm->emit('O');
vm->emit('V');
vm->emit('\n');
}
goto ABORT;
}
continue;
STACK_UNDERFLOW:
{
if (vm->emit != NULL)
{
vm->emit('\n');
vm->emit('!');
vm->emit('U');
vm->emit('N');
vm->emit('\n');
}
goto ABORT;
}
continue;
#endif
/* =============================================================
* KERNEL PRIMITIVES
*/
DOCOLON:
{
/* IP points to the next word in the PFA and that is the
* location to which we should return once this new word has
* executed. */
#ifdef __AVR__
if (inProgramSpace)
{
/* FIXME Can this ever happen? How would a ROM word
* know about a RAM word? */
/* Set the high bit on the return address so that we
* know that this address is in ROM. */
ip = (uint8_t*)((unsigned int)ip | 0x8000);
/* We are no longer in program space since, by design,
* DOCOLON is only ever used for user-defined words in
* RAM. */
inProgramSpace = 0;
}
#endif
(--returnTop)->ram = ip;
/* Now set the IP to the PFA of the word that is being
* called and continue execution inside of that word. */
ip = w;
}
continue;
DOCOLONROM:
{
#if ENABLE_TRACING
gTraceLevel++;
int i;
for (i = 0; i < gTraceLevel; i++)
{
printf(">");
}
printf(" [%02x] ", token);
/* W points at the PFA; back up to the NFA. */
const char * curDef = (const char *)w - (1 + 2 + 1);
/* Is this a hidden definition (the name is length zero)?
* If so, output the XT instead of the name. */
if ((*curDef & 0x1f) == 0)
{
printf("<pfa=0x%04X>", 0xC000 | (uint16_t)((uint8_t*)curDef - (uint8_t*)definitions));
}
else
{
/* Normal definition; walk backwards and output the
* name. */
for (i = 1; i <= (((unsigned int)*curDef) & 0x1f); --i)
{
printf("%c", *(curDef + i) & 0x7f);
}
}
/* Print the data stack and top-of-stack. */
for (i = 0; i < &vm->data_stack[32] - restDataStack - 1; i++)
{
printf(" %x", vm->data_stack[30 - i].u);
}
if ((&vm->data_stack[32] - restDataStack) > 0)
{
printf(" %x", tos.u);
}
printf("\n");
fflush(stdout);
#endif
/* IP points to the next word in the PFA and that is the
* location to which we should return once this new word has
* executed. */
#ifdef __AVR__
if (inProgramSpace)
{
/* Set the high bit on the return address so that we
* know that this address is in ROM. */
ip = (uint8_t*)((unsigned int)ip | 0x8000);
}
#endif
(--returnTop)->ram = ip;
/* Now set the IP to the PFA of the word that is being
* called and continue execution inside of that word. */
ip = w;
#ifdef __AVR__
/* We are obviously now in program space since, by design,
* DOCOLONROM is only ever used for ROM definitions. */
inProgramSpace = -1;
#endif
}
continue;
DOCONSTANT:
{
/* W points at the PFA of this word; push the address in
* that location onto the stack. */
*--restDataStack = tos;
tos = *(EnforthCell*)w;
}
continue;
DODOES:
{
/* We're currently sitting in a word that is calling a word
* defined with DOES>. IP has been advanced beyond that
* call and is still in the calling word. XT contains the
* Code Field of the defined word (and thus points to the
* DOES> part of the defining word's thread). We need to
* push IP to the stack, push the PFA of the defined word to
* the stack, and then continue execution at XT (the
* defining word's runtime behavior). */
#ifdef __AVR__
if (inProgramSpace)
{
/* FIXME Can this ever happen? How would a ROM word
* know about a RAM word? */
/* Set the high bit on the return address so that we
* know that this address is in ROM. */
ip = (uint8_t*)((unsigned int)ip | 0x8000);
/* We are no longer in program space since, by design,
* DODOES is only ever used for user-defined words in
* RAM. */
inProgramSpace = 0;
}
#endif
(--returnTop)->ram = ip;
/* W points at the PFA of the defined word; push that to the
* stack per the runtime behavior of DOES>. */
*--restDataStack = tos;
tos.ram = w;
/* Point IP at the DOES> portion of the defining word (which
* is the target of the defined word's Code Field and is
* thus in XT). */
ip = (uint8_t*)(vm->dictionary.ram + (xt & 0x3FFF));
}
continue;
DOCREATE:
DOVARIABLE:
{
/* W points at the PFA of this word; push that location onto
* the stack. */
*--restDataStack = tos;
tos.ram = w;
}
continue;
DOFFI0:
{
CHECK_STACK(0, 1);
/* W contains a pointer to the PFA of the FFI definition;
* get the FFI definition pointer and then use that to get
* the FFI function pointer. Get the value of the is_void
* flag as well.*/
ZeroArgFFI fn = (ZeroArgFFI)pgm_read_word(&(*(EnforthFFIDef**)w)->fn);
uint8_t is_void = pgm_read_byte(&(*(EnforthFFIDef**)w)->is_void);
if (is_void == 0)
{
*--restDataStack = tos;
tos = (*fn)();
}
else
{
(*fn)();
}
}
continue;
DOFFI1:
{
CHECK_STACK(1, 1);
/* W contains a pointer to the PFA of the FFI definition;
* get the FFI definition pointer and then use that to get
* the FFI function pointer. Get the value of the is_void
* flag as well. */
OneArgFFI fn = (OneArgFFI)pgm_read_word(&(*(EnforthFFIDef**)w)->fn);
uint8_t is_void = pgm_read_byte(&(*(EnforthFFIDef**)w)->is_void);
tos = (*fn)(tos);
if (is_void == 1)
{
tos = *restDataStack++;
}
}
continue;
DOFFI2:
{
CHECK_STACK(2, 1);
TwoArgFFI fn = (TwoArgFFI)pgm_read_word(&(*(EnforthFFIDef**)w)->fn);
uint8_t is_void = pgm_read_byte(&(*(EnforthFFIDef**)w)->is_void);
EnforthCell arg2 = tos;
EnforthCell arg1 = *restDataStack++;
tos = (*fn)(arg1, arg2);
if (is_void == 1)
{
tos = *restDataStack++;
}
}
continue;
DOFFI3:
{
CHECK_STACK(3, 1);
ThreeArgFFI fn = (ThreeArgFFI)pgm_read_word(&(*(EnforthFFIDef**)w)->fn);
uint8_t is_void = pgm_read_byte(&(*(EnforthFFIDef**)w)->is_void);
EnforthCell arg3 = tos;
EnforthCell arg2 = *restDataStack++;
EnforthCell arg1 = *restDataStack++;
tos = (*fn)(arg1, arg2, arg3);
if (is_void == 1)
{
tos = *restDataStack++;
}
}
continue;
DOFFI4:
CHECK_STACK(4, 1);
continue;
DOFFI5:
CHECK_STACK(5, 1);
continue;
DOFFI6:
CHECK_STACK(6, 1);
continue;
DOFFI7:
CHECK_STACK(7, 1);
continue;
/* -------------------------------------------------------------
***{:token :ibranch
*** :flags #{:headerless}}
*/
/* TODO Add docs re: Note that (branch) and (0branch) offsets
* are 8-bit relative offsets. UNLIKE word addresses, the IP
* points at the offset in BRANCH/ZBRANCH. These offsets can be
* positive or negative because branches can go both forwards
* and backwards. */
IBRANCH:
#ifdef __AVR__
{
CHECK_STACK(0, 0);
ip += (int8_t)pgm_read_byte(ip);
}
continue;
#else
/* Fall through, since the other architectures use shared
* instruction and data space. */
#endif
/* -------------------------------------------------------------
***{:token :branch
*** :flags #{:headerless}}
*/
/* TODO Add docs re: Note that (branch) and (0branch) offsets
* are 8-bit relative offsets. UNLIKE word addresses, the IP
* points at the offset in BRANCH/ZBRANCH. These offsets can be
* positive or negative because branches can go both forwards
* and backwards. */
BRANCH:
{
CHECK_STACK(0, 0);
/* Relative, because these are entirely within a single word
* and so we want it to be relocatable without us having to
* do anything. Note that the offset cannot be larger than
* +/- 127 bytes! */
ip += *(int8_t*)ip;
}
continue;
/* -------------------------------------------------------------
***{:token :icharlit
*** :args [[] [:char]]
*** :flags #{:headerless}}
*/
ICHARLIT:
#ifdef __AVR__
{
CHECK_STACK(0, 1);
*--restDataStack = tos;
tos.i = pgm_read_byte(ip);
ip++;
}
continue;
#else
/* Fall through, since the other architectures use shared
* instruction and data space. */
#endif
/* -------------------------------------------------------------
***{:token :charlit
*** :flags #{:headerless}}
*/
CHARLIT:
{
CHECK_STACK(0, 1);
*--restDataStack = tos;
tos.i = *ip++;
}
continue;
/* -------------------------------------------------------------
***{:token :exit}
*/
EXIT:
{
#if ENABLE_TRACING
int i;
for (i = 0; i < gTraceLevel; i++)
{
printf("<");
}
printf(" (");
for (i = 0; i < &vm->data_stack[32] - restDataStack - 1; i++)
{
printf("%x ", vm->data_stack[30 - i].u);
}
if ((&vm->data_stack[32] - restDataStack) > 0)
{
printf("%x", tos.u);
}
printf(")");
printf(" (R:");
for (i = 0; i < &vm->return_stack[32] - returnTop; i++)
{
printf(" %x", vm->return_stack[31 - i].u);
}
printf(")");
printf("\n");
gTraceLevel--;
#endif
ip = (uint8_t *)((returnTop++)->ram);
#ifdef __AVR__
if (((unsigned int)ip & 0x8000) != 0)
{
/* This return address points at a ROM definition; strip
* off that flag as part of popping the address. */
ip = (uint8_t*)((unsigned int)ip & 0x7FFF);
inProgramSpace = -1;
}
else
{
inProgramSpace = 0;
}
#endif
}
continue;
/* -------------------------------------------------------------
***{:token :lit
*** :name "(LIT)"
*** :args [[] [:x]]
*** :flags #{:headerless}}
*/
/* Cannot be used in ROM definitions! */
LIT:
{
CHECK_STACK(0, 1);
*--restDataStack = tos;
tos = *(EnforthCell*)ip;
ip += kEnforthCellSize;
}
continue;
/* -------------------------------------------------------------
* PAUSE [Enforth] ( -- )
*
* Suspend the current task and resume execution of the next
* task in the task list. PAUSE will return to the caller when
* all of the tasks in the task list have had a chance to
* execute (and called PAUSE in order to relinquish execution to
* their next task).
*
***{:token :pause}
*/
PAUSE:
{
/* Push TOS onto the stack. */
*--restDataStack = tos;
/* Push RSP and IP to the stack. */
/* TODO Both of these need to be relative addresses. */
(--restDataStack)->ram = (uint8_t*)returnTop;
#ifdef __AVR__
if (inProgramSpace)
{
/* Set the high bit on the current IP so that we know
* that this address is in ROM. */
ip = (uint8_t*)((unsigned int)ip | 0x8000);
}
#endif
(--restDataStack)->ram = ip;
/* Save the stack pointer. */
((EnforthCell*)vm->cur_task.ram)[1].ram = (uint8_t*)restDataStack;
/* Make the previous task the current task. */
vm->cur_task.ram = ((EnforthCell*)vm->cur_task.ram)[0].ram;
/* Did we hit the beginning of the list? If so, wrap around
* to the last task. */
if (vm->cur_task.ram == 0)
{
vm->cur_task.ram = ((EnforthCell*)vm->dictionary.ram)[2].ram;
}
/* Unpause the interpreter. */
goto UNPAUSE;
}
/* -------------------------------------------------------------
* (HALT) [Enforth] ( i*x -- i*x ) ( R: j*x -- j*x )
*
* Stops and exits the VM. State is preserved on the stack,
* allowing the VM to be resumed by enforth_resume.
*
***{:token :phalt
*** :name "(HALT)"
*** :args [[] []]
*** :flags #{:headerless}}
*/
PHALT:
{
/* Push TOS onto the stack. */
*--restDataStack = tos;
/* Push RSP and IP to the stack. */
/* TODO Both of these need to be relative addresses. */
(--restDataStack)->ram = (uint8_t*)returnTop;
(--restDataStack)->ram = ip;