-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathzuo.c
7628 lines (6583 loc) · 221 KB
/
zuo.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
/* Like Zuo overall, the kernel implementation here is layered as much
as possible. There should be little need for forward function
declarations. */
#define ZUO_VERSION 1
#define ZUO_MINOR_VERSION 10
#if defined(_MSC_VER) || defined(__MINGW32__)
# define ZUO_WINDOWS
#else
# define ZUO_UNIX
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef ZUO_UNIX
# include <fcntl.h>
# include <unistd.h>
# include <errno.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <sys/stat.h>
# include <sys/time.h>
# include <time.h>
# include <dirent.h>
# include <signal.h>
# include <poll.h>
#endif
#ifdef ZUO_WINDOWS
# include <windows.h>
# include <direct.h>
# include <io.h>
# include <sys/stat.h>
# include <fcntl.h>
#endif
#if 0
# include <assert.h>
# define ASSERT(x) assert(x)
#else
# define ASSERT(x) do { } while (0)
#endif
/* `zuo_int_t` should be a 64-bit integer type, so we don't have to
worry about Y2038 or large file sizes. `zuo_int32_t` should be a
32-bit integer type, obviously. `zuo_intptr_t` is an integer the
same width as a pointer. */
#ifdef ZUO_UNIX
#include <stdint.h>
typedef int64_t zuo_int_t;
typedef uint64_t zuo_uint_t;
typedef int32_t zuo_int32_t;
typedef uint32_t zuo_uint32_t;
typedef intptr_t zuo_intptr_t;
typedef uintptr_t zuo_uintptr_t;
typedef int zuo_raw_handle_t;
#endif
#ifdef ZUO_WINDOWS
/* avoiding stdint to work with very old compilers */
typedef long long zuo_int_t;
typedef unsigned long long zuo_uint_t;
typedef int zuo_int32_t;
typedef unsigned int zuo_uint32_t;
# ifdef _WIN64
typedef long long zuo_intptr_t;
typedef unsigned long long zuo_uintptr_t;
# else
typedef long zuo_intptr_t;
typedef unsigned long zuo_uintptr_t;
# endif
typedef HANDLE zuo_raw_handle_t;
#endif
#define ZUO_HANDLE_ID(h) ((zuo_int_t)(h))
/* the "image.zuo" script looks for this line: */
#define EMBEDDED_IMAGE 0
#ifndef ZUO_LIB_PATH
# define ZUO_LIB_PATH "lib"
#endif
static const char *zuo_lib_path = ZUO_LIB_PATH;
#ifdef ZUO_EMBEDDED
# include "zuo.h"
#endif
/* configurable lower bound on how much space to use: */
#ifndef ZUO_MIN_HEAP_SIZE
# define ZUO_MIN_HEAP_SIZE (32*1024*1024)
#endif
#define ZUO_UNUSED(id) (void)id
/*======================================================================*/
/* run-time configuration */
/*======================================================================*/
static const char *zuo_file_logging = NULL;
static int zuo_logging = 0;
static int zuo_probe_each = 0;
static int zuo_probe_counter = 0;
static void zuo_configure(void) {
const char *s;
if ((s = getenv("ZUO_LIB_PATH"))) {
zuo_lib_path = s;
}
if (getenv("ZUO_LOG"))
zuo_logging = 1;
if ((s = getenv("ZUO_PROBE_EACH"))) {
while (isdigit(*s)) {
zuo_probe_each = (zuo_probe_each * 10) + (s[0] - '0');
s++;
}
}
}
/*======================================================================*/
/* object layouts */
/*======================================================================*/
typedef enum {
zuo_singleton_tag,
zuo_pair_tag,
zuo_integer_tag,
zuo_string_tag,
zuo_symbol_tag,
zuo_trie_node_tag,
zuo_variable_tag,
zuo_primitive_tag,
zuo_closure_tag,
zuo_handle_tag,
zuo_opaque_tag,
zuo_cont_tag,
zuo_forwarded_tag
} zuo_tag_t;
typedef struct zuo_t {
zuo_int32_t tag;
/* every subtype must have more to make it at least as
large as `zuo_forwarded_t` */
} zuo_t;
typedef struct {
zuo_t obj;
zuo_t *forward;
} zuo_forwarded_t;
typedef struct {
zuo_t obj;
zuo_int32_t index;
} zuo_fasl_forwarded_t;
typedef struct {
zuo_t obj;
zuo_int_t i;
} zuo_integer_t;
#define ZUO_INT_I(p) (((zuo_integer_t *)(p))->i)
#define ZUO_UINT_I(p) ((zuo_uint_t)(((zuo_integer_t *)(p))->i))
typedef struct {
zuo_t obj;
zuo_t *car;
zuo_t *cdr;
} zuo_pair_t;
#define ZUO_CAR(p) (((zuo_pair_t *)(p))->car)
#define ZUO_CDR(p) (((zuo_pair_t *)(p))->cdr)
#ifdef ZUO_SAFER_INTERP
# define _zuo_car(p) zuo_car(p)
# define _zuo_cdr(p) zuo_cdr(p)
#else
# define _zuo_car(p) ZUO_CAR(p)
# define _zuo_cdr(p) ZUO_CDR(p)
#endif
typedef struct {
zuo_t obj;
zuo_intptr_t len; /* must be at the same place as forwarding */
unsigned char s[1];
} zuo_string_t;
/* Since `len` overlaps with forwarding, we can tentatively get the "length" from any object */
#define ZUO_STRING_LEN(obj) (((zuo_string_t *)(obj))->len)
#define ZUO_STRING_ALLOC_SIZE(len) (sizeof(zuo_string_t) + (len))
#define ZUO_STRING_PTR(obj) ((char *)&((zuo_string_t *)(obj))->s)
typedef struct {
zuo_t obj;
zuo_int32_t id;
zuo_t *str;
} zuo_symbol_t;
#define ZUO_TRIE_BFACTOR_BITS 4
#define ZUO_TRIE_BFACTOR (1 << ZUO_TRIE_BFACTOR_BITS)
#define ZUO_TRIE_BFACTOR_MASK (ZUO_TRIE_BFACTOR -1)
typedef struct zuo_trie_node_t {
zuo_t obj;
zuo_int_t count;
zuo_t *key;
zuo_t *val;
struct zuo_t* next[ZUO_TRIE_BFACTOR];
} zuo_trie_node_t;
typedef struct {
zuo_t obj;
zuo_t *name;
zuo_t *val;
} zuo_variable_t;
typedef void (*zuo_proc_t)(void);
typedef zuo_t *(*zuo_dispatcher_proc_t)(zuo_proc_t proc, zuo_t *arguments);
typedef struct {
zuo_t obj;
zuo_dispatcher_proc_t dispatcher;
zuo_proc_t proc;
zuo_int32_t arity_mask;
zuo_t *name;
} zuo_primitive_t;
/* only try to count up to this high for arity checking: */
#define ZUO_MAX_PRIM_ARITY 10
typedef struct {
zuo_t obj;
zuo_t *lambda;
zuo_t *env;
} zuo_closure_t;
typedef enum {
zuo_handle_open_fd_in_status,
zuo_handle_open_fd_out_status,
zuo_handle_closed_status,
zuo_handle_process_running_status,
zuo_handle_process_done_status,
zuo_handle_cleanable_status,
} zuo_handle_status_t;
typedef struct zuo_handle_t {
zuo_t obj;
zuo_int_t id;
union {
struct {
zuo_handle_status_t status;
union {
zuo_raw_handle_t handle;
zuo_int_t result;
} u;
} h;
zuo_t *forward; /* make sure the object is big enough */
} u;
} zuo_handle_t;
#define ZUO_HANDLE_RAW(obj) (((zuo_handle_t *)(obj))->u.h.u.handle)
typedef struct {
zuo_t obj;
zuo_t *tag;
zuo_t *val;
} zuo_opaque_t;
typedef enum {
zuo_apply_cont,
zuo_begin_cont,
zuo_let_cont,
zuo_if_cont,
zuo_done_cont
} zuo_cont_tag_t;
typedef struct zuo_cont_t {
zuo_t obj;
zuo_cont_tag_t tag;
zuo_t *data;
zuo_t *env;
zuo_t *in_proc; /* string or #f */
zuo_t *next;
} zuo_cont_t;
/* GC roots: */
static struct {
/* Roots kept in an image dump: */
struct {
/* singleton values */
zuo_t *o_undefined; /* internal use only */
zuo_t *o_true;
zuo_t *o_false;
zuo_t *o_null;
zuo_t *o_eof;
zuo_t *o_void;
zuo_t *o_empty_hash;
/* sentinel for the interpreter */
zuo_t *o_done_k;
/* intrinsic functions that manipulate interpreter state */
zuo_t *o_apply;
zuo_t *o_call_cc;
zuo_t *o_call_prompt;
zuo_t *o_kernel_eval;
/* private primitives */
zuo_t *o_kernel_read_string;
zuo_t *o_module_to_hash_star;
zuo_t *o_get_read_and_eval;
zuo_t *o_register_module;
/* symbol table, root environment, and modules */
zuo_t *o_intern_table;
zuo_t *o_top_env;
zuo_t *o_modules;
/* symbols for kernel core forms */
zuo_t *o_quote_symbol;
zuo_t *o_lambda_symbol;
zuo_t *o_let_symbol;
zuo_t *o_begin_symbol;
zuo_t *o_if_symbol;
} image;
/* Roots that are not included in a dump: */
struct {
/* CEK-style interp--continue registers */
zuo_t *o_interp_e;
zuo_t *o_interp_v;
zuo_t *o_interp_env;
zuo_t *o_interp_k;
zuo_t *o_interp_in_proc; /* used for a stack trace on error */
zuo_t *o_interp_meta_k; /* list of (cons <cont> <tag>) */
/* for cycle detection */
zuo_t *o_pending_modules;
#ifdef ZUO_UNIX
/* process status table */
zuo_t *o_pid_table;
#endif
zuo_t *o_fd_table;
zuo_t *o_cleanable_table;
/* startup info */
zuo_t *o_library_path;
zuo_t *o_runtime_env;
/* data to save across a GC that's possibly triggered by interp */
zuo_t *o_stash;
} runtime;
} zuo_roots;
#define z zuo_roots.image
#define Z zuo_roots.runtime
static zuo_int32_t zuo_symbol_count = 0;
static zuo_int32_t zuo_handle_count = 0;
/*======================================================================*/
/* sanity checks */
/*======================================================================*/
static void zuo_panic(const char *s) {
fprintf(stderr, "%s\n", s);
exit(1);
}
static void zuo_check_sanity(void) {
if (sizeof(zuo_int32_t) != 4)
zuo_panic("wrong int32 size");
if (sizeof(zuo_int_t) != 8)
zuo_panic("wrong int size");
if ((void*)&(((zuo_string_t *)NULL)->len) != (void*)&(((zuo_forwarded_t *)NULL)->forward))
zuo_panic("string len field misplaced");
}
/*======================================================================*/
/* signal forward declarations */
/*======================================================================*/
static zuo_t *zuo_resume_signal(void);
static zuo_t *zuo_suspend_signal(void);
/*======================================================================*/
/* memory manager */
/*======================================================================*/
#define ALLOC_ALIGN(i) (((i) + (sizeof(zuo_intptr_t) - 1)) & ~(sizeof(zuo_intptr_t) -1))
static zuo_int_t heap_size = ZUO_MIN_HEAP_SIZE;
static void *to_space = NULL;
static zuo_int_t allocation_offset = 0;
static zuo_int_t total_allocation = 0;
static zuo_int_t gc_threshold = 0;
typedef struct old_space_t {
void *space;
zuo_int_t size;
struct old_space_t *next;
} old_space_t;
static old_space_t *old_spaces;
static old_space_t *free_spaces; /* to recycle without going through malloc */
static void *malloc_or_recycle(zuo_int_t min_size, zuo_int_t *size) {
/* distinguishing min versus preferred lets us recycle more often;
recycling can avoid triggering mmap/munmap system calls and
associated page-clearing work */
old_space_t *sp, *prev = NULL;
for (sp = free_spaces; sp; prev = sp, sp = sp->next) {
if (sp->size >= min_size) {
void *result = sp->space;
*size = sp->size;
if (prev)
prev->next = sp->next;
else
free_spaces = sp->next;
free(sp);
return result;
}
}
return malloc(*size);
}
static zuo_t *zuo_new(int tag, zuo_int_t size) {
zuo_t *obj;
ASSERT(size >= sizeof(zuo_forwarded_t));
size = ALLOC_ALIGN(size);
if (to_space == NULL) {
to_space = malloc_or_recycle(heap_size, &heap_size);
gc_threshold = heap_size;
}
if (allocation_offset + size > heap_size) {
old_space_t *new_old_spaces;
new_old_spaces = malloc(sizeof(old_space_t));
new_old_spaces->space = to_space;
new_old_spaces->size = heap_size;
new_old_spaces->next = old_spaces;
old_spaces = new_old_spaces;
if (heap_size < size)
heap_size = size * 2;
to_space = malloc_or_recycle(size * 2, &heap_size);
allocation_offset = 0;
}
obj = (zuo_t *)((char *)to_space + allocation_offset);
obj->tag = tag;
allocation_offset += size;
total_allocation += size;
return obj;
}
static zuo_int_t object_size(zuo_int32_t tag, zuo_int_t maybe_string_len) {
switch(tag) {
case zuo_singleton_tag:
return sizeof(zuo_forwarded_t);
case zuo_integer_tag:
return sizeof(zuo_integer_t);
case zuo_string_tag:
return ZUO_STRING_ALLOC_SIZE(maybe_string_len);
case zuo_pair_tag:
return sizeof(zuo_pair_t);
case zuo_symbol_tag:
return sizeof(zuo_symbol_t);
case zuo_trie_node_tag:
return sizeof(zuo_trie_node_t);
case zuo_variable_tag:
return sizeof(zuo_variable_t);
case zuo_primitive_tag:
return sizeof(zuo_primitive_t);
case zuo_closure_tag:
return sizeof(zuo_closure_t);
case zuo_handle_tag:
return sizeof(zuo_handle_t);
case zuo_opaque_tag:
return sizeof(zuo_opaque_t);
case zuo_cont_tag:
return sizeof(zuo_cont_t);
default:
case zuo_forwarded_tag:
return sizeof(zuo_forwarded_t);
}
}
static void zuo_update(zuo_t **addr_to_update) {
zuo_t *obj = *addr_to_update;
if (obj->tag != zuo_forwarded_tag) {
zuo_int_t size = ALLOC_ALIGN(object_size(obj->tag, ZUO_STRING_LEN(obj)));
zuo_t *new_obj = (zuo_t *)((char *)to_space + allocation_offset);
allocation_offset += size;
memcpy(new_obj, obj, size);
obj->tag = zuo_forwarded_tag;
((zuo_forwarded_t *)obj)->forward = new_obj;
}
*addr_to_update = ((zuo_forwarded_t *)obj)->forward;
}
static void zuo_trace(zuo_t *obj) {
switch(obj->tag) {
case zuo_singleton_tag:
case zuo_integer_tag:
case zuo_string_tag:
case zuo_handle_tag:
case zuo_forwarded_tag:
break;
case zuo_pair_tag:
zuo_update(&((zuo_pair_t *)obj)->car);
zuo_update(&((zuo_pair_t *)obj)->cdr);
break;
case zuo_symbol_tag:
zuo_update(&((zuo_symbol_t *)obj)->str);
break;
case zuo_trie_node_tag:
{
int i;
zuo_update(&((zuo_trie_node_t *)obj)->key);
zuo_update(&((zuo_trie_node_t *)obj)->val);
for (i = 0; i < ZUO_TRIE_BFACTOR; i++)
zuo_update(&((zuo_trie_node_t *)obj)->next[i]);
}
break;
case zuo_variable_tag:
zuo_update(&((zuo_variable_t *)obj)->name);
zuo_update(&((zuo_variable_t *)obj)->val);
break;
case zuo_primitive_tag:
zuo_update(&((zuo_primitive_t *)obj)->name);
break;
case zuo_closure_tag:
zuo_update(&((zuo_closure_t *)obj)->lambda);
zuo_update(&((zuo_closure_t *)obj)->env);
break;
case zuo_opaque_tag:
zuo_update(&((zuo_opaque_t *)obj)->tag);
zuo_update(&((zuo_opaque_t *)obj)->val);
break;
case zuo_cont_tag:
zuo_update(&((zuo_cont_t *)obj)->data);
zuo_update(&((zuo_cont_t *)obj)->env);
zuo_update(&((zuo_cont_t *)obj)->in_proc);
zuo_update(&((zuo_cont_t *)obj)->next);
break;
}
}
static void zuo_trace_objects(void) {
zuo_int_t trace_offset = 0;
while (trace_offset < allocation_offset) {
zuo_t *obj = (zuo_t *)((char *)to_space + trace_offset);
zuo_trace(obj);
trace_offset += ALLOC_ALIGN(object_size(obj->tag, ZUO_STRING_LEN(obj)));
}
}
static void zuo_finish_gc(void *old_space, zuo_int_t old_heap_size, old_space_t *old_old_spaces) {
old_space_t *sp;
sp = free_spaces;
while (sp != NULL) {
old_space_t *next_free_spaces = sp->next;
free(sp->space);
free(sp);
sp = next_free_spaces;
}
sp = malloc(sizeof(old_space_t));
sp->space = old_space;
sp->size = old_heap_size;
sp->next = old_old_spaces;
free_spaces = sp;
total_allocation = allocation_offset;
gc_threshold = total_allocation * 2;
if (gc_threshold < ZUO_MIN_HEAP_SIZE)
gc_threshold = ZUO_MIN_HEAP_SIZE;
}
static void zuo_collect(void) {
void *old_space = to_space;
old_space_t *old_old_spaces = old_spaces;
zuo_int_t old_heap_size = heap_size;
zuo_suspend_signal();
old_spaces = NULL;
heap_size = total_allocation * 2;
to_space = malloc_or_recycle(total_allocation, &heap_size);
allocation_offset = 0;
/* roots */
{
zuo_t **p = (zuo_t **)&zuo_roots;
int i, len;
len = sizeof(zuo_roots) / sizeof(zuo_t*);
for (i = 0; i < len; i++) {
zuo_update(p+i);
}
}
/* collect */
zuo_trace_objects();
/* cleanup */
zuo_finish_gc(old_space, old_heap_size, old_old_spaces);
zuo_resume_signal();
}
static void zuo_check_collect(void) {
if (total_allocation >= gc_threshold)
zuo_collect();
}
static void zuo_replace_heap(void *space, zuo_int_t size, zuo_int_t offset) {
allocation_offset = offset;
zuo_finish_gc(to_space, heap_size, old_spaces);
old_spaces = NULL;
heap_size = size;
to_space = space;
}
/*======================================================================*/
/* table of primitives used for fasl */
/*======================================================================*/
#define ZUO_MAX_PRIMITIVE_COUNT 128
static zuo_primitive_t zuo_registered_prims[ZUO_MAX_PRIMITIVE_COUNT];
static int zuo_registered_prim_count;
static void zuo_register_primitive(zuo_dispatcher_proc_t dispatcher, zuo_proc_t proc, zuo_int32_t arity_mask) {
if (zuo_registered_prim_count == ZUO_MAX_PRIMITIVE_COUNT)
zuo_panic("primitive table is too small");
zuo_registered_prims[zuo_registered_prim_count].dispatcher = dispatcher;
zuo_registered_prims[zuo_registered_prim_count].proc = proc;
zuo_registered_prims[zuo_registered_prim_count].arity_mask = arity_mask;
zuo_registered_prim_count++;
}
static zuo_int32_t zuo_primitive_to_id(zuo_primitive_t *obj) {
int i;
for (i = 0; i < zuo_registered_prim_count; i++)
if (obj->proc == zuo_registered_prims[i].proc)
return i;
zuo_panic("could not find primitive");
return 0;
}
static void zuo_id_to_primitive(zuo_int32_t i, zuo_primitive_t *obj) {
obj->dispatcher = zuo_registered_prims[i].dispatcher;
obj->proc = zuo_registered_prims[i].proc;
obj->arity_mask = zuo_registered_prims[i].arity_mask;
}
/*======================================================================*/
/* heap fasl */
/*======================================================================*/
typedef struct {
zuo_int32_t magic;
zuo_int32_t map_size; /* in int32s */
zuo_int32_t image_size; /* in int32s */
zuo_int32_t symbol_count;
} zuo_fasl_header_t;
static zuo_int32_t zuo_magic(void) {
/* gets magic specific to the current machine's endianness */
return *(zuo_int32_t *)"\0zuo";
}
typedef enum {
zuo_fasl_out,
zuo_fasl_in
} zuo_fasl_mode_t;
typedef struct {
zuo_fasl_mode_t mode;
} zuo_fasl_stream_t;
typedef struct {
zuo_fasl_stream_t stream;
void *heap, *shadow_heap;
zuo_int32_t heap_size;
zuo_t **objs;
zuo_int32_t *map; /* offset in image */
zuo_int32_t map_size, map_offset;
zuo_int32_t *image;
zuo_int32_t image_size, image_offset;
} zuo_fasl_stream_out_t;
typedef struct {
zuo_fasl_stream_t stream;
void *heap;
zuo_int32_t heap_size;
zuo_int32_t *map;
zuo_int32_t *image;
zuo_int32_t offset;
} zuo_fasl_stream_in_t;
static void zuo_ensure_image_room(zuo_fasl_stream_out_t *stream) {
if (stream->image_size == stream->image_offset) {
zuo_int32_t *new_image = malloc(stream->image_size * 2 * sizeof(zuo_int32_t));
memcpy(new_image, stream->image, (stream->image_offset) * sizeof(zuo_int32_t));
free(stream->image);
stream->image = new_image;
stream->image_size *= 2;
}
}
static void zuo_ensure_map_room(zuo_fasl_stream_out_t *stream) {
if (stream->map_size == stream->map_offset) {
zuo_t **new_objs = malloc(stream->map_size * 2 * sizeof(zuo_t*));
zuo_int32_t *new_map = malloc(stream->map_size * 2 * sizeof(zuo_int32_t));
memcpy(new_objs, stream->objs, (stream->map_offset) * sizeof(zuo_t*));
memcpy(new_map, stream->map, (stream->map_offset) * sizeof(zuo_int32_t));
free(stream->objs);
free(stream->map);
stream->objs = new_objs;
stream->map = new_map;
stream->map_size *= 2;
}
}
static void zuo_fasl_ref(zuo_t **_obj, zuo_fasl_stream_t *_stream) {
if (_stream->mode == zuo_fasl_in) {
zuo_fasl_stream_in_t *stream = (zuo_fasl_stream_in_t *)_stream;
zuo_int32_t delta = stream->map[stream->image[stream->offset++]];
*_obj = (zuo_t *)((char *)stream->heap + delta);
} else {
zuo_fasl_stream_out_t *stream = (zuo_fasl_stream_out_t *)_stream;
zuo_t *obj = *_obj;
zuo_intptr_t delta = ((char *)obj) - ((char *)stream->heap);
zuo_t *shadow_obj = (zuo_t *)(((char *)stream->shadow_heap) + delta);
ASSERT((delta >= 0) && (delta < stream->heap_size));
zuo_ensure_image_room(stream);
if (shadow_obj->tag == zuo_forwarded_tag) {
stream->image[stream->image_offset++] = ((zuo_fasl_forwarded_t *)shadow_obj)->index;
} else {
zuo_ensure_map_room(stream);
shadow_obj->tag = zuo_forwarded_tag;
((zuo_fasl_forwarded_t *)shadow_obj)->index = stream->map_offset;
stream->image[stream->image_offset++] = stream->map_offset;
stream->objs[stream->map_offset++] = *_obj;
}
}
}
static void zuo_fasl_int32(zuo_int32_t *_i, zuo_fasl_stream_t *_stream) {
if (_stream->mode == zuo_fasl_in) {
zuo_fasl_stream_in_t *stream = (zuo_fasl_stream_in_t *)_stream;
*_i = stream->image[stream->offset++];
} else {
zuo_fasl_stream_out_t *stream = (zuo_fasl_stream_out_t *)_stream;
zuo_ensure_image_room(stream);
stream->image[stream->image_offset++] = *_i;
}
}
#define BUILD_INT(lo, hi) (((zuo_int_t)(hi) << 32) | ((zuo_int_t)(lo) & 0xFFFFFFFF))
static void zuo_fasl_int(zuo_int_t *_i, zuo_fasl_stream_t *_stream) {
zuo_int32_t lo, hi;
lo = *_i & (zuo_int_t)0xFFFFFFFF;
hi = *_i >> 32;
zuo_fasl_int32(&lo, _stream);
zuo_fasl_int32(&hi, _stream);
*_i = BUILD_INT(lo, hi);
}
static void zuo_fasl_char(unsigned char *_c, zuo_fasl_stream_t *stream) {
zuo_int32_t i = *_c;
zuo_fasl_int32(&i, stream);
*_c = i;
}
static void zuo_fasl(zuo_t *obj, zuo_fasl_stream_t *stream) {
{
zuo_int32_t tag = obj->tag;
zuo_fasl_int32(&tag, stream);
obj->tag = tag;
}
switch(obj->tag) {
case zuo_singleton_tag:
break;
case zuo_integer_tag:
zuo_fasl_int(&((zuo_integer_t *)obj)->i, stream);
break;
case zuo_string_tag:
{
int i;
zuo_int_t len = ((zuo_string_t *)obj)->len;
/* restore assumes that a string starts with its length */
zuo_fasl_int(&len, stream);
((zuo_string_t *)obj)->len = len;
for (i = 0; i < ((zuo_string_t *)obj)->len; i++)
zuo_fasl_char(&((zuo_string_t *)obj)->s[i], stream);
}
break;
case zuo_pair_tag:
zuo_fasl_ref(&((zuo_pair_t *)obj)->car, stream);
zuo_fasl_ref(&((zuo_pair_t *)obj)->cdr, stream);
break;
case zuo_symbol_tag:
zuo_fasl_int32(&((zuo_symbol_t *)obj)->id, stream);
zuo_fasl_ref(&((zuo_symbol_t *)obj)->str, stream);
break;
case zuo_trie_node_tag:
{
int i;
zuo_int32_t mask;
if (stream->mode == zuo_fasl_in)
zuo_fasl_int32(&mask, stream);
else {
mask = 0;
for (i = 0; i < ZUO_TRIE_BFACTOR; i++)
if (((zuo_trie_node_t *)obj)->next[i] != z.o_undefined)
mask |= (1 << i);
zuo_fasl_int32(&mask, stream);
}
zuo_fasl_ref(&((zuo_trie_node_t *)obj)->key, stream);
zuo_fasl_ref(&((zuo_trie_node_t *)obj)->val, stream);
for (i = 0; i < ZUO_TRIE_BFACTOR; i++)
if (mask & (1 << i))
zuo_fasl_ref(&((zuo_trie_node_t *)obj)->next[i], stream);
else
((zuo_trie_node_t *)obj)->next[i] = z.o_undefined;
}
break;
case zuo_variable_tag:
{
zuo_fasl_ref(&((zuo_variable_t *)obj)->name, stream);
zuo_fasl_ref(&((zuo_variable_t *)obj)->val, stream);
break;
}
case zuo_primitive_tag:
{
zuo_int32_t primitive_id = 0;
if (stream->mode == zuo_fasl_out) {
primitive_id = zuo_primitive_to_id((zuo_primitive_t *)obj);
zuo_fasl_int32(&primitive_id, stream);
} else {
zuo_fasl_int32(&primitive_id, stream);
zuo_id_to_primitive(primitive_id, (zuo_primitive_t *)obj);
}
zuo_fasl_ref(&((zuo_primitive_t *)obj)->name, stream);
}
break;
case zuo_closure_tag:
zuo_fasl_ref(&((zuo_closure_t *)obj)->lambda, stream);
zuo_fasl_ref(&((zuo_closure_t *)obj)->env, stream);
break;
case zuo_opaque_tag:
zuo_fasl_ref(&((zuo_opaque_t *)obj)->tag, stream);
zuo_fasl_ref(&((zuo_opaque_t *)obj)->val, stream);
break;
case zuo_cont_tag:
zuo_fasl_ref(&((zuo_cont_t *)obj)->data, stream);
zuo_fasl_ref(&((zuo_cont_t *)obj)->env, stream);
zuo_fasl_ref(&((zuo_cont_t *)obj)->in_proc, stream);
zuo_fasl_ref(&((zuo_cont_t *)obj)->next, stream);
break;
case zuo_handle_tag:
zuo_panic("cannot dump heap with handles");
break;
case zuo_forwarded_tag:
ASSERT(0);
break;
}
}
static void zuo_fasl_roots(zuo_fasl_stream_t *stream) {
zuo_t **p = (zuo_t **)&zuo_roots.image;
int i, len = sizeof(zuo_roots.image) / sizeof(zuo_t*);
for (i = 0; i < len; i++)
zuo_fasl_ref(p+i, stream);
}
static char *zuo_fasl_dump(zuo_int_t *_len) {
zuo_fasl_stream_out_t stream;
zuo_int32_t total_size, header_size = sizeof(zuo_fasl_header_t) / sizeof(zuo_int32_t);
zuo_int32_t *dump;
zuo_int32_t map_done;
/* make sure everything is in contiguous memory: */
zuo_collect();
stream.stream.mode = zuo_fasl_out;
stream.heap = to_space;
stream.heap_size = allocation_offset;
stream.shadow_heap = malloc(allocation_offset);
memset(stream.shadow_heap, 0, allocation_offset);
stream.map_size = 1024;
stream.map_offset = 0;
stream.map = malloc(stream.map_size * sizeof(zuo_int32_t));
stream.objs = malloc(stream.map_size * sizeof(zuo_t*));
stream.image_size = 4096;
stream.image_offset = 0;
stream.image = malloc(stream.image_size * sizeof(zuo_int32_t));
zuo_fasl_roots(&stream.stream);
/* analogous to the collector's trace_objects loop: */
for (map_done = 0; map_done < stream.map_offset; map_done++) {
zuo_t *obj = stream.objs[map_done];
/* register location of this object in the image */
stream.map[map_done] = stream.image_offset;
zuo_fasl(obj, &stream.stream);
}
total_size = header_size + stream.map_offset + stream.image_offset;
dump = malloc(total_size * sizeof(zuo_int32_t));
((zuo_fasl_header_t *)dump)->magic = zuo_magic();
((zuo_fasl_header_t *)dump)->map_size = stream.map_offset;
((zuo_fasl_header_t *)dump)->image_size = stream.image_offset;
((zuo_fasl_header_t *)dump)->symbol_count = zuo_symbol_count;
memcpy(dump + header_size, stream.map, stream.map_offset * sizeof(zuo_int32_t));
memcpy(dump + header_size + stream.map_offset, stream.image, stream.image_offset * sizeof(zuo_int32_t));
free(stream.image);
free(stream.objs);
free(stream.map);
free(stream.shadow_heap);
*_len = total_size * sizeof(zuo_int32_t);
return (char *)dump;
}
#define SWAP_ENDIAN(n) \
((((n) & 0xFF) << 24) | (((n) & 0xFF00) << 8) | (((n) & 0xFF0000) >> 8) | (((n) >> 24) & 0xFF))
static void zuo_fasl_restore(char *dump_in, zuo_int_t len) {
zuo_fasl_stream_in_t stream;
zuo_int32_t *dump = (zuo_int32_t *)dump_in, i, map_len, alloc_factor = 2;
zuo_int32_t header_size = sizeof(zuo_fasl_header_t) / sizeof(zuo_int32_t);
zuo_int32_t magic = zuo_magic();
if (((zuo_fasl_header_t *)dump)->magic != magic) {
if (((zuo_fasl_header_t *)dump)->magic == SWAP_ENDIAN(magic)) {
/* adapt little-endian to big-endian, or vice versa */
for (i = 0; i < len / (zuo_int_t)sizeof(zuo_int32_t); i++)
dump[i] = SWAP_ENDIAN(dump[i]);
} else
zuo_panic("image does not start with zuo magic");
}
map_len = ((zuo_fasl_header_t *)dump)->map_size;
stream.stream.mode = zuo_fasl_in;
stream.map = dump + header_size;
stream.image = dump + header_size + map_len;
stream.heap_size = 0;
stream.offset = 0;
/* compute heap size and replace image offsets with heap offsets */
for (i = 0; i < map_len; i++) {
zuo_int32_t delta = stream.map[i];
zuo_int32_t tag = stream.image[delta];