-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot-eval.c
executable file
·2317 lines (2089 loc) · 59.2 KB
/
boot-eval.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
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include <assert.h>
extern int isatty(int);
#define GC_APP_HEADER int type;
struct GC_StackRoot
{
void **root;
struct GC_StackRoot *next;
#if !defined(NDEBUG)
int live;
const char *name;
const char *file;
long line;
#endif
};
#if defined(NDEBUG)
# define GC_PROTECT(V) struct GC_StackRoot _sr_##V; _sr_##V.root= (void *)&V; GC_push_root(&_sr_##V)
# define GC_UNPROTECT(V) GC_pop_root(&_sr_##V)
#else
# define GC_PROTECT(V) struct GC_StackRoot _sr_##V; _sr_##V.root= (void *)&V; GC_push_root(&_sr_##V, #V, __FILE__, __LINE__)
# define GC_UNPROTECT(V) GC_pop_root(&_sr_##V, #V, __FILE__, __LINE__)
#endif
#define GC_INIT()
#define GC_init()
#if !defined(GC_API)
# define GC_API
#endif
GC_API void *GC_malloc(size_t nbytes);
GC_API void *GC_malloc_atomic(size_t nbytes);
GC_API void *GC_realloc(void *ptr, size_t lbs);
GC_API void GC_free(void *ptr);
GC_API size_t GC_size(void *ptr);
GC_API void GC_add_root(void *root);
GC_API void GC_delete_root(void *root);
GC_API void GC_mark(void *ptr);
GC_API void GC_mark_leaf(void *ptr);
GC_API void GC_sweep(void);
GC_API void GC_gcollect(void);
GC_API size_t GC_count_objects(void);
GC_API size_t GC_count_bytes(void);
GC_API double GC_count_fragments(void);
GC_API int GC_atomic(void *ptr);
#ifndef NDEBUG
GC_API void *GC_check(void *ptr);
GC_API void *GC_stamp(void *ptr, const char *file, long line, const char *func);
GC_API const char *GC_file(void *ptr);
GC_API long GC_line(void *ptr);
GC_API const char *GC_function(void *ptr);
#else
# define GC_check(PTR) (PTR)
# define GC_stamp(PTR, FILE, LINE, FUNC) (PTR)
# define GC_file(PTR) "?"
# define GC_line(PTR) 0
# define GC_function(PTR) "?"
#endif
typedef void (*GC_finaliser_t)(void *ptr, void *data);
GC_API void GC_register_finaliser(void *ptr, GC_finaliser_t finaliser, void *data);
extern struct GC_StackRoot *GC_stack_roots;
#if defined(NDEBUG)
GC_API inline void GC_push_root(struct GC_StackRoot *sr)
{
sr->next= GC_stack_roots;
GC_stack_roots= sr;
}
GC_API inline void GC_pop_root(struct GC_StackRoot *sr)
{
# if 0
GC_stack_roots= sr->next;
# else /* paranoid version for broken code warns of mismatched pops with a SEGV */
struct GC_StackRoot *nr= sr->next;
while (nr != GC_stack_roots) GC_stack_roots= GC_stack_roots->next;
# endif
}
#else
GC_API inline void GC_push_root(struct GC_StackRoot *sr, const char *name, const char *file, int line)
{
sr->next= GC_stack_roots;
sr->name= name;
sr->file= file;
sr->line= line;
sr->live= 1;
GC_stack_roots= sr;
}
static int GC_roots_include(struct GC_StackRoot *roots, struct GC_StackRoot *root)
{
while (roots) {
if (roots == root) return 1;
roots= roots->next;
}
return 0;
}
GC_API inline void GC_pop_root(struct GC_StackRoot *sr, const char *name, const char *file, int line)
{
struct GC_StackRoot *nr= sr->next;
struct GC_StackRoot *gr= GC_stack_roots;
if (!sr->live) { fprintf(stderr, "*** %s %d %s: STALE POP IN GC_pop_root\n", file, line, name); goto die; }
sr->live= 0;
if (GC_roots_include(nr, sr)) { fprintf(stderr, "*** %s %d %s: CYCLE IN GC_pop_root\n", file, line, name); goto die; }
int n= 0;
while (nr != gr) {
if (n++ > 10) { fprintf(stderr, "*** %s %d %s: LOOP IN GC_pop_root\n", file, line, name); goto die; }
gr= gr->next;
}
GC_stack_roots= gr;
return;
die:
fprintf(stderr, "* gc stack roots = %p %s %ld %s\n", gr, gr->file, gr->line, gr->name);
fprintf(stderr, "* popped root = %p %s %ld %s\n", sr, sr->file, sr->line, sr->name);
while (nr) {
fprintf(stderr, "* next root = %p %s %ld %s\n", nr, nr ? nr->file : 0, nr ? nr->line : 0, nr ? nr->name : 0);
nr= nr->next;
}
abort();
}
#endif
typedef void (*GC_pre_mark_function_t)(void);
extern GC_pre_mark_function_t GC_pre_mark_function;
typedef void (*GC_mark_function_t)(void *ptr);
extern GC_mark_function_t GC_mark_function;
typedef void (*GC_free_function_t)(void *ptr);
extern GC_free_function_t GC_free_function;
#define GC_ALIGN sizeof(long)
#define GC_MEMORY 0x7fffffff
#define GC_QUANTUM 50*1024
#if defined(DEBUGGC)
# define ALLOCS_PER_GC 1
#else
# define ALLOCS_PER_GC 32768
#endif
#define VERBOSE 0
#define BITS_PER_WORD (sizeof(long) * 8)
typedef struct _gcheader
{
unsigned long size : BITS_PER_WORD - 8 __attribute__((__packed__));
union {
unsigned int flags : 3;
struct {
unsigned int used : 1;
unsigned int atom : 1;
unsigned int mark : 1;
} __attribute__((__packed__));
} __attribute__((__packed__));
struct _gcheader *next;
struct _gcfinaliser *finalisers;
#ifndef NDEBUG
const char *file;
long line;
const char *func;
#endif
#if defined(GC_APP_HEADER)
GC_APP_HEADER
#endif
} gcheader;
static inline void *hdr2ptr(gcheader *hdr) { return (void *)(hdr + 1); }
static inline gcheader *ptr2hdr(void *ptr) { return (gcheader *)ptr - 1; }
#ifndef NDEBUG
GC_API void *GC_stamp(void *ptr, const char *file, long line, const char *func)
{
gcheader *hdr= ptr2hdr(ptr);
hdr->file= file;
hdr->line= line;
hdr->func= func;
return ptr;
}
GC_API const char *GC_file(void *ptr) { return ptr2hdr(ptr)->file; }
GC_API long GC_line(void *ptr) { return ptr2hdr(ptr)->line; }
GC_API const char *GC_function(void *ptr) { return ptr2hdr(ptr)->func; }
#endif
typedef struct _gcfinaliser
{
void *ptr;
GC_finaliser_t finaliser;
void *data;
struct _gcfinaliser *next;
} gcfinaliser;
static gcheader gcbase= { 0, { -1 }, &gcbase };
static gcheader *gcnext= &gcbase;
static size_t gcQuantum= GC_QUANTUM;
static int gcCount= ALLOCS_PER_GC;
static int gcAllocs= ALLOCS_PER_GC;
static size_t gcMemory= GC_MEMORY;
static gcfinaliser *finalisable= 0;
GC_API void *GC_malloc(size_t lbs)
{
gcheader *hdr, *org;
size_t split;
if ((!--gcAllocs) || (gcMemory < lbs)) {
//fprintf(stderr, "%i %lu %ld\t", gcAllocs, gcMemory, lbs);
# if VERBOSE >= 1
if (gcAllocs > 0) fprintf(stderr, "GC: heap full after %i allocations\n", gcCount - gcAllocs);
# endif
gcAllocs= gcCount;
GC_gcollect();
//fprintf(stderr, "GC %i %lu %ld\n", gcAllocs, gcMemory, lbs);
if (gcMemory < lbs) goto full;
}
org= hdr= gcnext;
lbs= (lbs + GC_ALIGN-1) & ~(GC_ALIGN-1);
#if VERBOSE > 1
fprintf(stderr, "malloc %i\n", (int)lbs);
#endif
again:
#if VERBOSE > 4
{
gcheader *h= gcnext;
do {
fprintf(stderr, " %2d %p -> %p = %i\n", h->flags, h, h->next, (int)h->size);
h= h->next;
} while (h != gcnext);
}
#endif
split= lbs + sizeof(gcheader) + GC_ALIGN;
do {
# if VERBOSE > 3
fprintf(stderr, "? %2d %p -> %p = %i\n", hdr->flags, hdr, hdr->next, (int)hdr->size);
# endif
if (!hdr->used) {
while ((!hdr->next->used) && (hdr2ptr(hdr) + hdr->size == hdr->next)) {
hdr->size += sizeof(gcheader) + hdr->next->size;
hdr->next= hdr->next->next;
}
if ((hdr->size >= split) || (hdr->size == lbs))
{
void *mem;
if (hdr->size >= split)
{
gcheader *ins= (gcheader *)(hdr2ptr(hdr) + lbs);
ins->flags= 0;
ins->next= hdr->next;
ins->size= hdr->size - lbs - sizeof(gcheader);
hdr->next= ins;
hdr->size= lbs;
}
hdr->used= 1;
hdr->finalisers= 0;
gcnext= hdr->next;
mem= hdr2ptr(hdr);
# if VERBOSE > 2
//if ((long)hdr == 0x800248) abort();
fprintf(stderr, "MALLOC %p -> %p + %i\n", mem, hdr, (int)GC_size(mem));
# endif
memset(mem, 0, hdr->size);
gcMemory -= hdr->size;
//if (mem == (void *)0x617190) { fprintf(stderr, "ALLOCATING %p\n", mem); bkpt(); }
return mem;
}
}
hdr= hdr->next;
} while (hdr != org);
{
size_t incr= gcQuantum;
size_t req= sizeof(gcheader) + lbs;
while (incr <= req) incr *= 2;
//fprintf(stderr, "extending by %ld => %ld @ %d\n", req, incr, (int)(gcCount - gcAllocs));
hdr= (gcheader *)malloc(incr);
//fprintf(stderr, "buffer at %x\n", (int)hdr);
if (hdr != (gcheader *)-1)
{
hdr->flags= 0;
hdr->next= gcbase.next;
gcbase.next= hdr;
hdr->size= incr - sizeof(gcheader);
#if VERBOSE
fprintf(stderr, "extend by %i at %p\n", (int)hdr->size, hdr);
#endif
goto again;
}
fprintf(stderr, "GC: sbrk failed\n");
}
full:
fprintf(stderr, "GC: out of memory\n");
abort();
return 0;
}
GC_API void *GC_malloc_atomic(size_t lbs)
{
void *mem= GC_malloc(lbs);
ptr2hdr(mem)->atom= 1;
return mem;
}
GC_API void *GC_realloc(void *ptr, size_t lbs)
{
gcheader *hdr= ptr2hdr(ptr);
void *mem;
if (lbs <= hdr->size) return ptr;
mem= GC_malloc(lbs);
memcpy(mem, ptr, hdr->size);
ptr2hdr(mem)->atom= hdr->atom;
GC_free(ptr);
return mem;
}
static gcheader *GC_freeHeader(gcheader *hdr)
{
#if VERBOSE > 2
fprintf(stderr, "FREE %p -> %p %s:%ld %s\n", hdr2ptr(hdr), hdr, hdr->file, hdr->line, hdr->func);
if (hdr->line == 0) {
fflush(stdout);
abort();
}
#endif
hdr->flags= 0;
gcMemory += hdr->size;
return hdr;
}
GC_API void GC_free(void *ptr)
{
gcnext= GC_freeHeader(ptr2hdr(ptr));
}
GC_API size_t GC_size(void *ptr)
{
return ptr2hdr(ptr)->size;
}
GC_API void GC_default_pre_mark_function(void) {}
GC_pre_mark_function_t GC_pre_mark_function= GC_default_pre_mark_function;
GC_API void GC_default_mark_function(void *ptr)
{
gcheader *hdr= ptr2hdr(ptr);
void **pos= ptr;
void **lim= hdr2ptr(hdr) + hdr->size - sizeof(void *);
while (pos <= lim)
{
void *field= *pos;
if (field && !((long)field & 1))
GC_mark(field);
++pos;
}
}
GC_mark_function_t GC_mark_function= GC_default_mark_function;
GC_API void GC_mark(void *ptr)
{
if ((long)ptr & 1) return;
gcheader *hdr= ptr2hdr(ptr);
#if VERBOSE > 3
fprintf(stderr, "mark? %p -> %p used %d atom %d mark %d\n", ptr, hdr, hdr->used, hdr->atom, hdr->mark);
#endif
if (!hdr->mark) {
hdr->mark= 1;
if (!hdr->atom)
GC_mark_function(ptr);
}
}
GC_API void GC_mark_leaf(void *ptr)
{
ptr2hdr(ptr)->mark= 1;
}
GC_free_function_t GC_free_function= 0;
GC_API void GC_sweep(void)
{
gcheader *hdr= gcbase.next;
do {
#if VERBOSE > 3
fprintf(stderr, "sweep? %p %d\n", hdr, hdr->flags);
#endif
if (hdr->flags)
{
if (hdr->mark)
hdr->mark= 0;
else {
if (hdr->finalisers) {
while (hdr->finalisers) {
gcfinaliser *gcf= hdr->finalisers;
hdr->finalisers= gcf->next;
gcf->next= finalisable;
finalisable= gcf;
}
}
else {
if (GC_free_function) GC_free_function(hdr2ptr(hdr));
hdr= GC_freeHeader(hdr);
}
}
}
hdr= hdr->next;
} while (hdr != &gcbase);
gcnext= gcbase.next;
while (finalisable)
{
gcfinaliser *gcf= finalisable;
gcf->finaliser(gcf->ptr, gcf->data);
finalisable= gcf->next;
free(gcf);
}
}
static void ***roots= 0;
static size_t numRoots= 0;
static size_t maxRoots= 0;
struct GC_StackRoot *GC_stack_roots= 0;
GC_API void GC_add_root(void *root)
{
if (numRoots == maxRoots)
roots= maxRoots
? realloc(roots, sizeof(roots[0]) * (maxRoots *= 2))
: malloc ( sizeof(roots[0]) * (maxRoots= 128));
roots[numRoots++]= (void **)root;
assert(root);
}
GC_API void GC_delete_root(void *root)
{
int i;
for (i= 0; i < numRoots; ++i)
if (roots[i] == (void **)root)
break;
if (i < numRoots)
{
memmove(roots + i, roots + i + 1, sizeof(roots[0]) * (numRoots - i));
--numRoots;
}
}
GC_API long GC_collections= 0;
GC_API void GC_gcollect(void)
{
int i;
struct GC_StackRoot *sr;
++GC_collections;
#if !defined(NDEBUG)
{
# undef static
static char *cursors= "-/|\\";
static int cursor= 0;
if (GC_collections % 100 == 0) {
if (0 == cursors[cursor]) cursor= 0;
fprintf(stderr, "%c\010", cursors[cursor]);
++cursor;
}
# if (NONSTATIC)
# define static
# endif
}
#endif
GC_pre_mark_function();
#if VERBOSE >= 1
fprintf(stderr, "*** GC: mark roots\n");
#endif
for (i= 0; i < numRoots; ++i)
if (*roots[i]) {
# if VERBOSE >= 2
fprintf(stderr, "*** GC: root %i *%p -> %p\n", i, roots[i], *roots[i]);
# endif
GC_mark(*roots[i]);
}
#if VERBOSE > 0
fprintf(stderr, "*** GC: mark stack\n");
#endif
for (sr= GC_stack_roots; sr; sr= sr->next) {
#if VERBOSE > 2 && defined(DEBUGGC)
fprintf(stderr, "*** GC: stack root %p %s %s:%ld\n", *sr->root, sr->name, sr->file, sr->line);
#endif
if (*(sr->root)) GC_mark(*(sr->root));
}
#if VERBOSE > 0
fprintf(stderr, "*** GC: sweep\n");
#endif
GC_sweep();
#if VERBOSE > 0
fprintf(stderr, "*** GC: done\n");
#endif
}
GC_API size_t GC_count_objects(void)
{
gcheader *hdr= gcbase.next;
size_t count= 0;
do {
if (hdr->used)
++count;
hdr= hdr->next;
} while (hdr != &gcbase);
return count;
}
GC_API size_t GC_count_bytes(void)
{
gcheader *hdr= gcbase.next;
size_t count= 0;
do {
if (hdr->used)
count += hdr->size;
hdr= hdr->next;
} while (hdr != &gcbase);
return count;
}
GC_API double GC_count_fragments(void)
{
gcheader *hdr= gcbase.next;
size_t used= 0;
size_t free= 0;
do {
if (hdr->used) {
++used;
//printf("%p\t%7d\n", hdr, (int)hdr->size);
}
else {
while ((!hdr->next->used) && (hdr2ptr(hdr) + hdr->size == hdr->next)) {
hdr->size += sizeof(gcheader) + hdr->next->size;
hdr->next= hdr->next->next;
}
++free;
//printf("%p\t\t%7d\n", hdr, (int)hdr->size);
}
hdr= hdr->next;
} while (hdr != &gcbase);
return (double)free / (double)used;
}
GC_API int GC_atomic(void *ptr)
{
return ptr2hdr(ptr)->atom;
}
#ifndef NDEBUG
GC_API void *GC_check(void *ptr)
{
gcheader *hdr= ptr2hdr(ptr);
if (!hdr->used) {
hdr->used= 1;
printf("accessible dead object %p %s:%ld %s\n", ptr, hdr->file, hdr->line, hdr->func);
}
return ptr;
}
#endif
GC_API void GC_register_finaliser(void *ptr, GC_finaliser_t finaliser, void *data)
{
gcheader *gch = ptr2hdr(ptr);
gcfinaliser *gcf = (struct _gcfinaliser *)malloc(sizeof(struct _gcfinaliser));
gcf->ptr = ptr;
gcf->finaliser = finaliser;
gcf->data = data;
gcf->next = gch->finalisers;
gch->finalisers = gcf;
}
struct buffer
{
char *buffer;
int size;
int position;
};
#define BUFFER_INITIALISER { 0, 0, 0 }
static void buffer_reset(struct buffer *b) { b->position= 0; }
static void buffer_append(struct buffer *b, int c)
{
if (b->position == b->size)
b->buffer= b->buffer
? realloc(b->buffer, b->size *= 2)
: malloc(b->size= 32);
b->buffer[b->position++]= c;
}
static void buffer_appendAll(struct buffer *b, const char *s)
{
while (*s) buffer_append(b, *s++);
}
static char *buffer_contents(struct buffer *b)
{
buffer_append(b, 0);
b->position--;
return b->buffer;
}
union Object;
typedef union Object *oop;
typedef oop (*imp_t)(oop args, oop env);
#define nil ((oop)0)
enum { Undefined, Long, String, Symbol, Pair, _Array, Array, Expr, Form, Fixed, Subr };
struct Long { long bits; };
struct String { oop size; char *bits; };
struct Symbol { char *bits; };
struct Pair { oop head, tail; };
struct Array { oop _array; };
struct Expr { oop defn, env; };
struct Form { oop function; };
struct Fixed { oop function; };
struct Subr { imp_t imp; char *name; };
union Object {
struct Long Long;
struct String String;
struct Symbol Symbol;
struct Pair Pair;
struct Array Array;
struct Expr Expr;
struct Form Form;
struct Fixed Fixed;
struct Subr Subr;
};
static void fatal(char *reason, ...);
#define setType(OBJ, TYPE) (ptr2hdr(OBJ)->type= (TYPE))
static inline int getType(oop obj) { return obj ? ptr2hdr(obj)->type : Undefined; }
#define is(TYPE, OBJ) ((OBJ) && (TYPE == getType(OBJ)))
#if defined(NDEBUG)
# define checkType(OBJ, TYPE) OBJ
#else
# define checkType(OBJ, TYPE) _checkType(OBJ, TYPE, #TYPE, __FILE__, __LINE__)
static inline oop _checkType(oop obj, int type, char *name, char *file, int line)
{
if (obj && !ptr2hdr(obj)->used) fatal("%s:%i: attempt to access dead object %s\n", file, line, name);
if (!is(type, obj)) fatal("%s:%i: typecheck failed for %s (%i != %i)\n", file, line, name, type, getType(obj));
return obj;
}
#endif
#define get(OBJ, TYPE, FIELD) (checkType(OBJ, TYPE)->TYPE.FIELD)
#define set(OBJ, TYPE, FIELD, VALUE) (checkType(OBJ, TYPE)->TYPE.FIELD= (VALUE))
#define getHead(OBJ) get(OBJ, Pair,head)
#define getTail(OBJ) get(OBJ, Pair,tail)
#define setHead(OBJ, VAL) set(OBJ, Pair,head, VAL)
#define setTail(OBJ, VAL) set(OBJ, Pair,tail, VAL)
#define getLong(X) get((X), Long,bits)
static oop car(oop obj) { return is(Pair, obj) ? getHead(obj) : nil; }
static oop cdr(oop obj) { return is(Pair, obj) ? getTail(obj) : nil; }
static oop caar(oop obj) { return car(car(obj)); }
static oop cadr(oop obj) { return car(cdr(obj)); }
static oop cddr(oop obj) { return cdr(cdr(obj)); }
//static oop caaar(oop obj) { return car(car(car(obj))); }
//static oop cadar(oop obj) { return car(cdr(car(obj))); }
//static oop caddr(oop obj) { return car(cdr(cdr(obj))); }
//static oop cadddr(oop obj) { return car(cdr(cdr(cdr(obj)))); }
#define newBits(TYPE) _newBits(TYPE, sizeof(struct TYPE))
#define newOops(TYPE) _newOops(TYPE, sizeof(struct TYPE))
static oop _newBits(int type, size_t size) { oop obj= GC_malloc_atomic(size); setType(obj, type); return obj; }
static oop _newOops(int type, size_t size) { oop obj= GC_malloc(size); setType(obj, type); return obj; }
static oop symbols= nil;
static oop s_set= nil, s_quote= nil, s_lambda= nil, s_let= nil, s_quasiquote= nil, s_unquote= nil, s_unquote_splicing= nil, s_t= nil, s_dot= nil;
static oop f_lambda= nil, f_let= nil, f_quote= nil, f_set= nil;
static oop globals= nil, expanders= nil, encoders= nil, evaluators= nil, applicators= nil;
static oop backtrace= nil;
static int opt_v= 0;
static oop newLong(long bits) { oop obj= newBits(Long); set(obj, Long,bits, bits); return obj; }
static oop _newString(size_t len)
{
char *gstr= GC_malloc_atomic(len + 1); GC_PROTECT(gstr); /* + 1 to ensure null terminator */
oop obj= newOops(String); GC_PROTECT(obj);
set(obj, String,size, newLong(len)); GC_UNPROTECT(obj);
set(obj, String,bits, gstr); GC_UNPROTECT(gstr);
return obj;
}
static oop newString(char *cstr)
{
size_t len= strlen(cstr);
oop obj= _newString(len);
memcpy(get(obj, String,bits), cstr, len);
return obj;
}
static int stringLength(oop string)
{
return getLong(get(string, String,size));
}
static oop newSymbol(char *cstr) { oop obj= newBits(Symbol); set(obj, Symbol,bits, strdup(cstr)); return obj; }
static oop newPair(oop head, oop tail) { oop obj= newOops(Pair); set(obj, Pair,head, head); set(obj, Pair,tail, tail); return obj; }
static oop newArray(int tally)
{
oop elts= _newOops(_Array, sizeof(oop) * tally); GC_PROTECT(elts);
oop obj= newOops( Array); GC_UNPROTECT(elts);
set(obj, Array,_array, elts);
return obj;
}
static int arrayLength(oop obj)
{
if (is(Array, obj))
return GC_size(get(obj, Array,_array)) / sizeof(oop);
return 0;
}
static oop oopAt(oop obj, int index)
{
if (obj) {
if (!GC_atomic(obj)) {
int size= GC_size(obj) / sizeof(oop);
if ((unsigned)index < (unsigned)size) return ((oop *)obj)[index];
}
}
return nil;
}
static oop oopAtPut(oop obj, int index, oop value)
{
if (!GC_atomic(obj)) {
int size= GC_size(obj) / sizeof(oop);
if ((unsigned)index < (unsigned)size) return ((oop *)obj)[index]= value;
}
return nil;
}
static oop arrayAt(oop array, int index)
{
if (is(Array, array)) {
oop elts= get(array, Array,_array);
int size= GC_size(elts) / sizeof(oop);
if ((unsigned)index < (unsigned)size)
return ((oop *)elts)[index];
}
return nil;
}
static oop arrayAtPut(oop array, int index, oop val)
{
if (is(Array, array)) {
oop elts= get(array, Array,_array);
int size= GC_size(elts) / sizeof(oop);
if ((unsigned)index >= (unsigned)size) {
oop oops= _newOops(_Array, sizeof(oop) * (index + 1));
memcpy((oop *)oops, (oop *)elts, size * sizeof(oop));
elts= set(array, Array,_array, oops);
}
return ((oop *)elts)[index]= val;
}
return nil;
}
static oop newExpr(oop defn, oop env) { oop obj= newOops(Expr); set(obj, Expr,defn, defn); set(obj, Expr,env, env); return obj; }
static oop newForm(oop function) { oop obj= newOops(Form); set(obj, Form,function, function); return obj; }
static oop newFixed(oop function) { oop obj= newOops(Fixed); set(obj, Fixed,function, function); return obj; }
static oop newSubr(imp_t imp, char *name)
{
oop obj= newBits(Subr);
set(obj, Subr,imp, imp);
set(obj, Subr,name, name);
return obj;
}
static oop newBool(int b) { return b ? s_t : nil; }
static oop intern(char *cstr)
{
oop list= nil;
for (list= symbols; is(Pair, list); list= getTail(list)) {
oop sym= getHead(list);
if (!strcmp(cstr, get(sym, Symbol,bits))) return sym;
}
oop sym= nil;
GC_PROTECT(sym);
sym= newSymbol(cstr);
symbols= newPair(sym, symbols);
GC_UNPROTECT(sym);
return sym;
}
#define CHAR_PRINT (1<<0)
#define CHAR_BLANK (1<<1)
#define CHAR_ALPHA (1<<2)
#define CHAR_DIGIT10 (1<<3)
#define CHAR_DIGIT16 (1<<4)
#define CHAR_LETTER (1<<5)
static char chartab[]= {
/* 00 nul */ 0,
/* 01 soh */ 0,
/* 02 stx */ 0,
/* 03 etx */ 0,
/* 04 eot */ 0,
/* 05 enq */ 0,
/* 06 ack */ 0,
/* 07 bel */ 0,
/* 08 bs */ 0,
/* 09 ht */ 0,
/* 0a nl */ CHAR_PRINT | CHAR_BLANK,
/* 0b vt */ 0,
/* 0c np */ CHAR_PRINT | CHAR_BLANK,
/* 0d cr */ CHAR_PRINT | CHAR_BLANK,
/* 0e so */ 0,
/* 0f si */ 0,
/* 10 dle */ 0,
/* 11 dc1 */ 0,
/* 12 dc2 */ 0,
/* 13 dc3 */ 0,
/* 14 dc4 */ 0,
/* 15 nak */ 0,
/* 16 syn */ 0,
/* 17 etb */ 0,
/* 18 can */ 0,
/* 19 em */ 0,
/* 1a sub */ 0,
/* 1b esc */ 0,
/* 1c fs */ 0,
/* 1d gs */ 0,
/* 1e rs */ 0,
/* 1f us */ 0,
/* 20 sp */ CHAR_PRINT | CHAR_BLANK,
/* 21 ! */ CHAR_PRINT | CHAR_LETTER,
/* 22 " */ CHAR_PRINT | CHAR_PRINT,
/* 23 # */ CHAR_PRINT | CHAR_LETTER,
/* 24 $ */ CHAR_PRINT | CHAR_LETTER,
/* 25 % */ CHAR_PRINT | CHAR_LETTER,
/* 26 & */ CHAR_PRINT | CHAR_LETTER,
/* 27 ' */ CHAR_PRINT,
/* 28 ( */ CHAR_PRINT,
/* 29 ) */ CHAR_PRINT,
/* 2a * */ CHAR_PRINT | CHAR_LETTER,
/* 2b + */ CHAR_PRINT | CHAR_LETTER,
/* 2c , */ CHAR_PRINT | CHAR_LETTER,
/* 2d - */ CHAR_PRINT | CHAR_LETTER,
/* 2e . */ CHAR_PRINT | CHAR_LETTER,
/* 2f / */ CHAR_PRINT | CHAR_LETTER,
/* 30 0 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 31 1 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 32 2 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 33 3 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 34 4 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 35 5 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 36 6 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 37 7 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 38 8 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 39 9 */ CHAR_PRINT | CHAR_DIGIT10 | CHAR_DIGIT16,
/* 3a : */ CHAR_PRINT | CHAR_LETTER,
/* 3b ; */ CHAR_PRINT,
/* 3c < */ CHAR_PRINT | CHAR_LETTER,
/* 3d = */ CHAR_PRINT | CHAR_LETTER,
/* 3e > */ CHAR_PRINT | CHAR_LETTER,
/* 3f ? */ CHAR_PRINT | CHAR_LETTER,
/* 40 @ */ CHAR_PRINT | CHAR_LETTER,
/* 41 A */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 42 B */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 43 C */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 44 D */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 45 E */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 46 F */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 47 G */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 48 H */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 49 I */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 4a J */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 4b K */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 4c L */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 4d M */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 4e N */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 4f O */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 50 P */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 51 Q */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 52 R */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 53 S */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 54 T */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 55 U */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 56 V */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 57 W */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 58 X */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 59 Y */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 5a Z */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 5b [ */ CHAR_PRINT,
/* 5c \ */ CHAR_PRINT | CHAR_LETTER,
/* 5d ] */ CHAR_PRINT,
/* 5e ^ */ CHAR_PRINT | CHAR_LETTER,
/* 5f _ */ CHAR_PRINT | CHAR_LETTER,
/* 60 ` */ CHAR_PRINT,
/* 61 a */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 62 b */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 63 c */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 64 d */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 65 e */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 66 f */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA | CHAR_DIGIT16,
/* 67 g */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 68 h */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 69 i */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 6a j */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 6b k */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 6c l */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 6d m */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 6e n */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 6f o */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 70 p */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 71 q */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 72 r */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 73 s */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 74 t */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 75 u */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 76 v */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 77 w */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 78 x */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 79 y */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 7a z */ CHAR_PRINT | CHAR_LETTER | CHAR_ALPHA,
/* 7b { */ CHAR_PRINT,
/* 7c | */ CHAR_PRINT | CHAR_LETTER,
/* 7d } */ CHAR_PRINT,
/* 7e ~ */ CHAR_PRINT | CHAR_LETTER,
/* 7f del */ 0,
};
static int isPrint(int c) { return 0 <= c && c <= 127 && (CHAR_PRINT & chartab[c]); }
static int isAlpha(int c) { return 0 <= c && c <= 127 && (CHAR_ALPHA & chartab[c]); }
static int isDigit10(int c) { return 0 <= c && c <= 127 && (CHAR_DIGIT10 & chartab[c]); }
static int isDigit16(int c) { return 0 <= c && c <= 127 && (CHAR_DIGIT16 & chartab[c]); }
static int isLetter(int c) { return 0 <= c && c <= 127 && (CHAR_LETTER & chartab[c]); }
static oop read(FILE *fp);
static oop readList(FILE *fp, int delim)
{
oop head= nil, tail= head, obj= nil;
GC_PROTECT(head);
GC_PROTECT(obj);
obj= read(fp);
if (obj == (oop)EOF) goto eof;
head= tail= newPair(obj, nil);
for (;;) {
obj= read(fp);
if (obj == (oop)EOF) goto eof;
if (obj == s_dot) {
obj= read(fp);
if (obj == (oop)EOF) fatal("missing item after .");
tail= set(tail, Pair,tail, obj);
obj= read(fp);
if (obj != (oop)EOF) fatal("extra item after .");
goto eof;
}
obj= newPair(obj, nil);