forked from toddjasonblackmon/wren
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwren.c
executable file
·1485 lines (1308 loc) · 41 KB
/
wren.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
/** @file wren.h
*
* @brief Wren interpreter
*
* @par
* @copyright Copyright (c) 2007 Darius Bacon <[email protected]>
* @copyright Copyright (c) 2018 Doug Currie, Londonderry, NH, USA
* @note See LICENSE file for licensing terms.
*/
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "wren.h" /* Configuration & API */
/* More Configuration */
enum {
/* Capacity in bytes. */
store_capacity = 4096,
/* True iff voluminous tracing is wanted. */
loud = 0,
};
/* Accessors for unaligned storage in dictionary and code spaces */
#if WREN_UNALIGNED_ACCESS_OK
static inline wIndex fetch_wX (const uint8_t *p)
{
return *(wIndex *)p;
}
static inline int16_t fetch_2i (const uint8_t *p)
{
return *(int16_t *)p;
}
static inline wValue fetch_wV (const uint8_t *p)
{
return *(wValue *)p;
}
static inline apply_t fetch_wP (const uint8_t *p)
{
return *(apply_t *)p;
}
static inline void write_2i (uint8_t *p, const int16_t v)
{
*(int16_t *)p = v;
}
static inline void write_wX (uint8_t *p, const wIndex v)
{
*(wIndex *)p = v;
}
static inline void write_wV (uint8_t *p, const wValue v)
{
*(wValue *)p = v;
}
static inline void write_wP (uint8_t *p, const apply_t v)
{
*(apply_t *)p = v;
}
#else
// Unaligned access is not supported by hardware.
// It looks a little silly using memcpy() here, but it has a big advantage:
// with sufficient optimization, e.g., -Os, the compiler will expand it inline
// using a simple (and hopfully optimal) instruction sequence. Together with
// the union, this code uses no Undefined Behavior as long as the data are
// written and read with the matching pair write_XX and fetch_XX.
static inline wIndex fetch_wX (const uint8_t *p)
{
union { wIndex v; uint8_t b[sizeof(wIndex)]; } u;
memcpy(u.b, p, sizeof(wIndex));
return u.v;
}
static inline int16_t fetch_2i (const uint8_t *p)
{
union { int16_t v; uint8_t b[sizeof(int16_t)]; } u;
memcpy(u.b, p, sizeof(int16_t));
return u.v;
}
static inline wValue fetch_wV (const uint8_t *p)
{
union { wValue v; uint8_t b[sizeof(wValue)]; } u;
memcpy(u.b, p, sizeof(wValue));
return u.v;
}
static inline apply_t fetch_wP (const uint8_t *p)
{
union { apply_t v; uint8_t b[sizeof(apply_t)]; } u;
memcpy(u.b, p, sizeof(apply_t));
return u.v;
}
static inline void write_wX (uint8_t *p, const wIndex v)
{
union { wIndex v; uint8_t b[sizeof(wIndex)]; } u;
u.v = v;
memcpy(p, u.b, sizeof(wIndex));
}
static inline void write_2i (uint8_t *p, const int16_t v)
{
union { int16_t v; uint8_t b[sizeof(int16_t)]; } u;
u.v = v;
memcpy(p, u.b, sizeof(int16_t));
}
static inline void write_wV (uint8_t *p, const wValue v)
{
union { wValue v; uint8_t b[sizeof(wValue)]; } u;
u.v = v;
memcpy(p, u.b, sizeof(wValue));
}
static inline void write_wP (uint8_t *p, const apply_t v)
{
union { apply_t v; uint8_t b[sizeof(apply_t)]; } u;
u.v = v;
memcpy(p, u.b, sizeof(apply_t));
}
#endif
/* Error state */
static const char *complaint = NULL;
static void complain (const char *msg)
{
if (!complaint)
complaint = msg;
}
/* Main data store in RAM
Most of the memory we use is doled out of one block.
From the top, growing downwards, is a dictionary: a stack of
header/name pairs. The header distinguishes the kind of name and
what address it denotes, along with the length of the name.
From the bottom, growing upwards, are the bindings of the names:
the code, for procedures, or the data cell, for globals. (Locals
denote an offset in a transient stack frame. We'd have interleaved
the dictionary headers with the values, like in Forth, except the
entries for locals would get in the way while we're compiling the
body of a procedure; moving all of the headers out of the way was
the simplest solution.)
At runtime, the stack grows down from the bottom of the dictionary
(but wValue-aligned).
*/
static uint8_t the_store[store_capacity];
#define store_end (the_store + store_capacity)
typedef enum { a_primitive, a_procedure, a_global, a_local, a_cfunction } NameKind;
typedef struct Header Header;
struct Header {
wIndex binding; // or for primintives, uint8_t arity; uint8_t opcode
uint8_t kind_lnm1; // (kind << 4) | (name_length - 1)
uint8_t name[0];
} __attribute__((packed)); /* XXX gcc dependency */
#define PRIM_HEADER(opcode, arity, name_length) \
(uint8_t )(arity), (uint8_t )(opcode), \
(uint8_t )(a_primitive << 4) | (((name_length) - 1u) & 0xfu)
static inline NameKind get_header_kind (const uint8_t *p_header)
{
return (NameKind )((((Header *)p_header)->kind_lnm1) >> 4);
}
static inline uint8_t get_header_name_length (const uint8_t *p_header)
{
return ((((Header *)p_header)->kind_lnm1) & 0xfu) + 1u;
}
static inline wIndex get_header_binding (const uint8_t *p_header)
{
return fetch_wX(p_header);
}
static inline uint8_t get_header_prim_arity (const uint8_t *p_header)
{
return p_header[0];
}
static inline uint8_t get_header_prim_opcode (const uint8_t *p_header)
{
return p_header[1];
}
static inline void set_header_kind_lnm1 (uint8_t *p_header, NameKind kind, int name_length)
{
uint8_t k = (uint8_t )kind;
uint8_t z = (uint8_t )(name_length - 1);
((Header *)p_header)->kind_lnm1 = (k << 4) | (z & 0xfu);
}
static inline void set_header_binding (uint8_t *p_header, const wIndex binding)
{
write_wX(p_header, binding);
}
/* We make code_idx and dict_idx accessible as a global variable to Wren code, located in
* the first two wValue cells of the_store. (See "cp" and "dp" setup in wren_initialize().)
*/
#define code_idx (((wUvalu *)the_store)[0])
#define dict_idx (((wUvalu *)the_store)[1])
#define code_ptr (&the_store[code_idx])
#define dict_ptr (&the_store[dict_idx])
static int available (unsigned amount)
{
if (code_idx + amount <= dict_idx)
return 1;
complain("Store exhausted");
return 0;
}
static const uint8_t *next_header (const uint8_t *header)
{
const Header *h = (const Header *) header;
return h->name + get_header_name_length(header);
}
static Header *bind (const char *name, unsigned length, NameKind kind, unsigned binding)
{
assert(name);
assert((length - 1u) < (1u << 4));
assert(kind <= a_cfunction);
assert(binding <= UINT16_MAX);
if (available(sizeof(Header) + length))
{
dict_idx -= sizeof(Header) + length;
{
Header *h = (Header *)dict_ptr;
set_header_kind_lnm1((uint8_t *)h, kind, (uint8_t )length);
set_header_binding((uint8_t *)h, (wIndex )binding);
memcpy(h->name, name, length);
return h;
}
}
return NULL;
}
static const Header *lookup (const uint8_t *dict, const uint8_t *end,
const char *name, unsigned length)
{
for (; dict < end; dict = next_header(dict))
{
const Header *h = (const Header *)dict;
if (get_header_name_length(dict) == length && 0 == memcmp(h->name, name, length))
return h;
}
return NULL;
}
static inline uint8_t get_proc_arity (wIndex binding)
{
// Procedures are compiled with the first byte holding the procedure's arity
return the_store[binding];
}
#ifndef NDEBUG
#if 0
static void dump (const uint8_t *dict, const uint8_t *end)
{
for (; dict < end; dict = next_header(dict))
{
const Header *h = (const Header *)dict;
const uint8_t nlen = get_header_name_length(dict);
const NameKind nknd = get_header_kind(dict);
printf(" %*.*s\t%x %x %x\n",
nlen, nlen, h->name, nknd, h->binding,
(nknd == a_procedure || nknd == a_cfunction)
? get_proc_arity(h->binding)
: nknd == a_primitive ? get_header_prim_arity(dict) : 0u);
}
}
#endif
#endif
/* The virtual machine */
typedef uint8_t Instruc;
enum {
/* 0 */ HALT, PUSH, POP, PUSH_STRING, GLOBAL_FETCH, GLOBAL_STORE, LOCAL_FETCH,
/* 7 */ TCALL, CALL, RETURN, BRANCH, JUMP,
/* 12 */ ADD, SUB, MUL, DIV, MOD, UMUL, UDIV, UMOD, NEGATE,
/* 21 */ EQ, LT, ULT, AND, OR, XOR, SLA, SRA, SRL,
/* 30 */ GETC, PUTC, REFB, REFV, SETV,
/* 35 */ LOCAL_FETCH_0, LOCAL_FETCH_1, PUSHW, PUSHB,
/* 39 */ CCALL, REFX, SETX, SETB,
};
#ifndef NDEBUG
static const char *opcode_names[] = {
"HALT", "PUSH", "POP", "PUSH_STRING", "GLOBAL_FETCH", "GLOBAL_STORE", "LOCAL_FETCH",
"TCALL", "CALL", "RETURN", "BRANCH", "JUMP",
"ADD", "SUB", "MUL", "DIV", "MOD", "UMUL", "UDIV", "UMOD", "NEGATE",
"EQ", "LT", "ULT", "AND", "OR", "XOR", "SLA", "SRA", "SRL",
"GETC", "PUTC", "REFB", "REFV", "SETV",
"LOCAL_FETCH_0", "LOCAL_FETCH_1", "PUSHW", "PUSHB",
"CCALL", "REFX", "SETX", "SETB",
};
#endif
static const uint8_t primitive_dictionary[] =
{
PRIM_HEADER(UMUL, 2, 4), 'u', 'm', 'u', 'l',
PRIM_HEADER(UDIV, 2, 4), 'u', 'd', 'i', 'v',
PRIM_HEADER(UMOD, 2, 4), 'u', 'm', 'o', 'd',
PRIM_HEADER(ULT, 2, 3), 'u', 'l', 't',
PRIM_HEADER(SLA, 2, 3), 's', 'l', 'a',
PRIM_HEADER(SRA, 2, 3), 's', 'r', 'a',
PRIM_HEADER(SRL, 2, 3), 's', 'r', 'l',
PRIM_HEADER(GETC, 0, 4), 'g', 'e', 't', 'c',
PRIM_HEADER(PUTC, 1, 4), 'p', 'u', 't', 'c',
PRIM_HEADER(REFV, 1, 4), 'r', 'e', 'f', 'v',
PRIM_HEADER(SETV, 2, 4), 's', 'e', 't', 'v',
PRIM_HEADER(REFX, 1, 4), 'r', 'e', 'f', 'x',
PRIM_HEADER(SETX, 2, 4), 's', 'e', 't', 'x',
PRIM_HEADER(SETB, 2, 4), 's', 'e', 't', 'b',
};
#ifndef NDEBUG
#if 0
static void dump_dictionary (void)
{
printf("dictionary:\n");
dump(dict_ptr, store_end);
dump(primitive_dictionary,
primitive_dictionary + sizeof primitive_dictionary);
}
#endif
#endif
/* Call to C functions; see bind_c_function and CCALL prim */
static long ccall(apply_t fn, wValue *args, unsigned arity)
{
switch (arity)
{
#define A1 args[0]
#define A2 args[1],A1
#define A3 args[2],A2
#define A4 args[3],A3
#define A5 args[4],A4
#define A6 args[5],A5
#define A7 args[6],A6
case 0: return (*fn)();
case 1: return (*fn)(A1);
case 2: return (*fn)(A2);
case 3: return (*fn)(A3);
case 4: return (*fn)(A4);
case 5: return (*fn)(A5);
case 6: return (*fn)(A6);
case 7: return (*fn)(A7);
default: return 0;
}
}
#undef A1
#undef A2
#undef A3
#undef A4
#undef A5
#undef A6
#undef A7
/* Run VM code starting at 'pc', with the stack allocated the space between
'end' and dict_ptr. Return the result on top of the stack. */
static wValue run (Instruc *pc, const Instruc *end)
{
/* Stack pointer and base pointer
Initially just above the first free aligned wValue cell below
the dictionary. */
wValue *sp = (wValue *)(((uintptr_t )dict_ptr) & ~(sizeof(wValue) - 1));
wValue *bp = sp;
#define need(n) \
if (((uint8_t *)sp - ((n) * sizeof(wValue))) < end) goto stack_overflow; else
for (;;)
{
#ifndef NDEBUG
if (loud)
printf("RUN: %"PRVAL"\t%s\n", (wValue )(pc - the_store), opcode_names[*pc]);
#endif
switch (*pc++)
{
case HALT:
return sp[0];
break;
case PUSH:
need(1);
*--sp = fetch_wV(pc);
pc += sizeof(wValue);
break;
case PUSHW:
need(1);
*--sp = fetch_2i(pc);
pc += sizeof(int16_t);
break;
case PUSHB:
need(1);
*--sp = *(int8_t *)pc;
pc += sizeof(int8_t);
break;
case POP:
++sp;
break;
case PUSH_STRING:
need(1);
*--sp = (wValue)(pc - the_store);
/* N.B. this op is slower the longer the string is! */
pc += strlen((const char *)pc) + 1;
break;
case GLOBAL_FETCH:
need(1);
*--sp = fetch_wV(the_store + fetch_wX(pc));
pc += sizeof(wIndex);
break;
case GLOBAL_STORE:
write_wV(the_store + fetch_wX(pc), sp[0]);
pc += sizeof(wIndex);
break;
case LOCAL_FETCH_0:
need(1);
*--sp = bp[0];
break;
case LOCAL_FETCH_1:
need(1);
*--sp = bp[-1];
break;
case LOCAL_FETCH:
need(1);
*--sp = bp[-*pc++];
break;
/* A stack frame looks like this:
bp[0]: leftmost argument
(This is also where the return value will go.)
...
bp[-(n-1)]: rightmost argument (where n is the number of arguments)
bp[-n]: pair of old bp and return address (in two half-words)
...temporaries...
sp[0]: topmost temporary
The bp could be dispensed with, but it simplifies the compiler and VM
interpreter slightly, and ought to make basic debugging support
significantly simpler, and if we were going to make every stack slot be
32 bits wide then we don't even waste any extra space.
By the time we return, there's only one temporary in this frame:
the return value. Thus, &bp[-n] == &sp[1] at this time, and the
RETURN instruction doesn't need to know the value of n. CALL,
otoh, does. It looks like <CALL> <n> <addr-byte-1> <addr-byte-2>.
*/
case TCALL: /* Known tail call. */
{
wIndex binding = fetch_wX(pc);
uint8_t n = get_proc_arity(binding);
/* XXX portability: this assumes two wIndex fit in a wValue */
wValue frame_info = sp[n];
memmove((bp+1-n), sp, n * sizeof(wValue));
sp = bp - n;
sp[0] = frame_info;
pc = the_store + binding + 1u;
}
break;
case CALL:
{
/* Optimize tail calls.
Why doesn't the compiler emit a tail-call instruction instead
of us checking this at runtime? Because I don't see how it
could without some greater expense there: when we finish parsing
a function with lots of if-then-else branches, we may discover
only then that a bunch of calls we've compiled were in tail
position.
(Maybe that expense would be worth incurring, though, for the
sake of smaller compiled code.)
*/
const Instruc *cont = pc + sizeof(wIndex);
while (*cont == JUMP)
{
++cont;
cont += fetch_wX(cont);
}
if (*cont == RETURN)
{
/* This is a tail call. Replace opcode and re-run */
*--pc = TCALL;
}
else
{
wIndex binding = fetch_wX(pc);
uint8_t n = get_proc_arity(binding);
/* This is a non-tail call. Build a new frame. */
need(1);
--sp;
{
/* XXX portability: this assumes two wIndex fit in a wValue
** and they the alignment is natural for both values; seems ok */
wIndex *f = (wIndex *)sp;
f[0] = (uint8_t *)bp - the_store;
f[1] = cont - the_store;
bp = sp + n;
}
pc = the_store + binding + 1u;
}
}
break;
case CCALL:
{
wIndex binding = fetch_wX(pc);
uint8_t n = get_proc_arity(binding);
wValue result = ccall((apply_t )fetch_wP(the_store + binding + 1u), sp, n);
if (n == 0u)
{
need(1);
sp -= 1;
}
else
{
sp += n - 1;
}
sp[0] = result;
pc += sizeof(wIndex);
}
break;
case RETURN:
{
wValue result = sp[0];
wIndex *f = (wIndex *)(sp + 1);
sp = bp;
bp = (wValue *)(the_store + f[0]);
pc = the_store + f[1];
sp[0] = result;
}
break;
case BRANCH:
if (0 == *sp++)
pc += fetch_wX(pc);
else
pc += sizeof(wIndex);
break;
case JUMP:
pc += fetch_wX(pc);
break;
case ADD: sp[1] += sp[0]; ++sp; break;
case SUB: sp[1] -= sp[0]; ++sp; break;
case MUL: sp[1] *= sp[0]; ++sp; break;
case DIV: sp[1] /= sp[0]; ++sp; break;
case MOD: sp[1] %= sp[0]; ++sp; break;
case UMUL: sp[1] = (wUvalu )sp[1] * (wUvalu )sp[0]; ++sp; break;
case UDIV: sp[1] = (wUvalu )sp[1] / (wUvalu )sp[0]; ++sp; break;
case UMOD: sp[1] = (wUvalu )sp[1] % (wUvalu )sp[0]; ++sp; break;
case NEGATE: sp[0] = -sp[0]; break;
case EQ: sp[1] = sp[1] == sp[0]; ++sp; break;
case LT: sp[1] = sp[1] < sp[0]; ++sp; break;
case ULT: sp[1] = (wUvalu )sp[1] < (wUvalu )sp[0]; ++sp; break;
case AND: sp[1] &= sp[0]; ++sp; break;
case OR: sp[1] |= sp[0]; ++sp; break;
case XOR: sp[1] ^= sp[0]; ++sp; break;
case SLA: sp[1] <<= sp[0]; ++sp; break;
case SRA: sp[1] >>= sp[0]; ++sp; break;
case SRL: sp[1] = (wUvalu )sp[1] >> (wUvalu )sp[0]; ++sp; break;
case GETC:
need(1);
*--sp = getc(stdin);
break;
case PUTC:
putc(sp[0], stdout);
break;
case REFB:
{
wUvalu x = (wUvalu )sp[0]; // unsigned for comparison
if (x < (wUvalu )store_capacity)
{
sp[0] = the_store[x];
}
else
{
// Out of Bounds -- XXX ignore!?
sp[0] = 0;
}
break;
}
case REFV:
{
wUvalu x = (wUvalu )sp[0]; // unsigned for comparison
if (x <= (wUvalu )(store_capacity - sizeof(wValue)))
{
sp[0] = fetch_wV(&the_store[x]);
}
else
{
// Out of Bounds -- XXX ignore!?
sp[0] = 0;
}
break;
}
case SETV:
{
wUvalu x = (wUvalu )sp[1]; // unsigned for comparison
if (x <= (wUvalu )(store_capacity - sizeof(wValue)))
{
write_wV(&the_store[x], sp[0]);
}
else
{
// Out of Bounds -- XXX ignore!?
}
++sp; // e: just one value popped
break;
}
case REFX:
{
wUvalu x = (wUvalu )sp[0]; // unsigned for comparison
if (x <= (wUvalu )(store_capacity - sizeof(wIndex)))
{
sp[0] = (wValue )fetch_wX(&the_store[x]);
}
else
{
// Out of Bounds -- XXX ignore!?
sp[0] = 0;
}
break;
}
case SETX:
{
wUvalu x = (wUvalu )sp[1]; // unsigned for comparison
if (x <= (wUvalu )(store_capacity - sizeof(wIndex)))
{
write_wX(&the_store[x], (wIndex )sp[0]);
}
else
{
// Out of Bounds -- XXX ignore!?
}
++sp; // e: just one value popped
break;
}
case SETB:
{
wUvalu x = (wUvalu )sp[1]; // unsigned for comparison
if (x < (wUvalu )store_capacity)
{
the_store[x] = (uint8_t )sp[0];
}
else
{
// Out of Bounds -- XXX ignore!?
}
++sp; // e: just one value popped
break;
}
default: assert(0);
}
}
stack_overflow:
complain("Stack overflow");
return 0;
}
/* The 'assembler' */
static wIndex prev_instruc = 0u;
static void gen (Instruc opcode)
{
#ifndef NDEBUG
if (loud)
printf("ASM: %"PRVAL"\t%s\n", code_idx, opcode_names[opcode]);
#endif
if (available(1))
{
prev_instruc = code_idx;
the_store[code_idx++] = opcode;
}
}
static void gen_ubyte (uint8_t b)
{
if (loud)
printf("ASM: %"PRVAL"\tubyte %u\n", code_idx, b);
if (available(1))
the_store[code_idx++] = b;
}
static void gen_sbyte (int8_t b)
{
if (loud)
printf("ASM: %"PRVAL"\tsbyte %d\n", code_idx, b);
if (available(1))
((int8_t *)the_store)[code_idx++] = b;
}
static void gen_ushort (wIndex u)
{
if (loud)
printf("ASM: %"PRVAL"\tushort %u\n", code_idx, u);
if (available(sizeof u))
{
write_wX(code_ptr, u);
code_idx += sizeof u;
}
}
static void gen_sshort (int16_t u)
{
if (loud)
printf("ASM: %"PRVAL"\tsshort %d\n", code_idx, u);
if (available(sizeof u))
{
write_2i(code_ptr, u);
code_idx += sizeof u;
}
}
static void gen_value (wValue v)
{
if (loud)
printf("ASM: %"PRVAL"\tvalue %"PRVAL"\n", code_idx, v);
if (available(sizeof v))
{
write_wV(code_ptr, v);
code_idx += sizeof v;
}
}
static void gen_pointer (apply_t v)
{
if (loud)
printf("ASM: %"PRVAL"\tvalue %"PRPTR"\n", code_idx, v);
if (available(sizeof v))
{
write_wP(code_ptr, v);
code_idx += sizeof v;
}
}
static wIndex forward_ref (void)
{
wIndex ref = code_idx;
code_idx += sizeof(wIndex);
return ref;
}
static void resolve (wIndex ref)
{
if (loud)
printf("ASM: %"PRIDX"\tresolved: %"PRVAL"\n", ref, code_idx - ref);
write_wX(&the_store[ref], code_idx - ref);
}
static void block_prev (void)
{
prev_instruc = 0u; // The previous instruction isn't really known
}
/* Scanning */
enum { unread = EOF - 1 };
static int input_char = unread;
static int token;
static FILE *in_file;
static wValue token_value;
static char token_name[17]; // 16 + NUL
static int ch (void)
{
if (input_char == unread)
input_char = getc(in_file);
return input_char;
}
static void next_char (void)
{
if (input_char != EOF)
input_char = unread;
}
static void skip_line (void)
{
while (ch() != '\n' && ch() != EOF)
next_char();
}
static unsigned hex_char_value (char c)
{
return c <= '9' ? c - '0' : toupper(c) - ('A'-10);
}
static void next (void)
{
again:
if (isdigit(ch()))
{
token = PUSH;
token_value = 0;
do {
token_value = 10 * token_value + ch() - '0';
next_char();
if (ch() == 'x' && token_value == 0)
{
unsigned int digit_count = 0;
/* Oh, it's a hex literal, not decimal as we presumed. */
next_char();
for (; isxdigit(ch()); next_char()) {
token_value = 16 * token_value + hex_char_value(ch());
digit_count++;
}
if (digit_count == 0)
complain("Invalid Hex Number");
else if (digit_count > 2*sizeof(wValue)) { // allow all bits used for hex entry
complain("Numeric overflow");
}
break;
}
if (token_value < 0) // overflow
{
complain("Numeric overflow");
break;
}
} while (isdigit(ch()));
}
else if (isalpha(ch()) || ch() == '_')
{
char *n = token_name;
do {
if (token_name + sizeof token_name == n + 1)
{
complain("Identifier too long");
break;
}
*n++ = ch();
next_char();
} while (isalnum(ch()) || ch() == '_');
*n++ = '\0';
if (0 == strcmp(token_name, "then"))
token = 't';
else if (0 == strcmp(token_name, "forget"))
token = 'o';
else if (0 == strcmp(token_name, "let"))
token = 'l';
else if (0 == strcmp(token_name, "if"))
token = 'i';
else if (0 == strcmp(token_name, "fun"))
token = 'f';
else if (0 == strcmp(token_name, "else"))
token = 'e';
else
token = 'a';
}
else
switch (ch())
{
case '\'':
next_char();
{
/* We need to stick this string somewhere; after reaching
the parser, if successfully parsed, it would be compiled
into the instruction stream right after the next opcode.
So just put it there -- but don't yet update code_idx. */
uint8_t *s = code_ptr + 1;
for (; ch() != '\''; next_char())
{
if (ch() == EOF)
{
complain("Unterminated string");
token = EOF;
return;
}
if (!available(s + 2 - code_ptr))
{
token = '\n'; /* XXX need more for error recovery? */
return;
}
*s++ = ch();
}
next_char();
*s = '\0';
token = '\'';
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '<':
case '&':
case '|':
case '^':
case '(':
case ')':
case '=':
case ':':
case ';':
case '\n':
case EOF:
token = ch();
next_char();
break;
case ' ':
case '\t':
case '\r':
next_char();
goto again;
case '#':
skip_line();
goto again;
default:
complain("Lexical error");
token = '\n'; /* XXX need more for error recovery */
break;
}
}
/* Parsing and compiling */
static int expect (uint8_t expected, const char *plaint)
{
if (token == expected)
return 1;
complain(plaint);
return 0;
}
static void skip_newline (void)
{
while (!complaint && token == '\n')
next();
}
static void parse_expr (int precedence);
static void parse_arguments (unsigned arity)
{
unsigned i;
for (i = 0; i < arity; ++i)
parse_expr(20); /* 20 is higher than any operator precedence */
}