-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheme-ms.c
3926 lines (3424 loc) · 117 KB
/
scheme-ms.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
/* scheme.c -- SCHEME INTERPRETER EXAMPLE FOR THE MEMORY POOL SYSTEM
*
* Copyright (c) 2001-2020 Ravenbrook Limited. See end of file for license.
*
* TO DO
* - unbounded integers, other number types.
* - named let.
* - quasiquote: vectors; nested; dotted.
* - Lots of library.
* - \#foo unsatisfactory in read and print
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// mmtk and debugging specific includes
#include <time.h>
#include <math.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include "mmtk.h"
void** global_stack_top = NULL;
void* global_mutator = NULL; // Global var for single mutator thread
void** mmtk_mutator_stack_top() {
printf("%s:%d:%s Someone is calling this function\n", __FILE__, __LINE__, __FUNCTION__ );
return global_stack_top;
}
void mmtk_stop_all_mutators(void* mutatorThread) {
printf("%s:%d:%s Someone is calling this function with %p\n", __FILE__, __LINE__, __FUNCTION__, mutatorThread );
// Do nothing
};
void mmtk_resume_mutators(void* mutatorThread) {
printf("%s:%d:%s Someone is calling this function with %p\n", __FILE__, __LINE__, __FUNCTION__, mutatorThread );
};
static void
mmtk_get_mutators(void (*visit_mutator)(void *mutator, void *data), void *data)
{
visit_mutator(global_mutator, data);
}
//UPCALLS
/*
static void
mmtk_scan_vm_specific_roots( void* workerThread, void* factory )
{
printf("%s:%d:%s Entered\n", __FILE__, __LINE__, __FUNCTION__ );
// here we will scan isymtab and sptab and call mmtk_is_mmtk_object(ptr)
// and when it returns true we will pass the pointer to factory
for (int i=0; i<7; ++i){
void *varp = isymtab[i].varp;
printf("thing: %p\n", varp);
}
void* first_in_isymtab = isymtab[0];
printf("first: %p\n",first_in_isymtab);
}
*/
static void
mmtk_scan_roots_in_mutator_thread( void* workerThread, void* mutator, void* factory )
{
printf("%s:%d:%s Entered\n", __FILE__, __LINE__, __FUNCTION__ );
void* stack_bottom_obj = 0;
void** stack_bottom = &stack_bottom_obj;
// Scan from stack_bottom to global_stack_top
// Call mmtk_is_interior_pointer on each pointer
// If it is an interior_pointer then pass the ObjectReference to factory
}
static void mmtk_scan_object( void* workerThread, void* objectReference, void* slot_visitor ) {
// Duplicate what is in scheme.c obj_scan
}
typedef struct {
void** (*mutator_stack_top)(void);
void (*stop_all_mutators)(void* mutatorThread);
void (*resume_mutators)(void* mutatorThread);
void (*get_mutators)(void (*visit_mutator)(void *mutator, void *data), void *data);
void (*scan_vm_specific_roots)(void* workerThread, void* factory);
// void (*scan_roots_in_mutator_thread)(void* workerThread, void* mutator, void* factory);
// void (*scan_object)(void* workerThread, void* objectReference, void* slot_visitor);
int (*num_entries_in_sptab)();
int (*num_entries_in_isymtab)();
void* (*first_in_sptab)();
void* (*first_in_isymtab)();
} RtUpcalls;
RtUpcalls global_rt_upcalls = {
mmtk_mutator_stack_top,
mmtk_stop_all_mutators,
mmtk_resume_mutators,
mmtk_get_mutators,
mmtk_scan_vm_specific_roots,
// mmtk_scan_roots_in_mutator_thread,
// mmtk_scan_object,
mmtk_num_entries_in_sptab,
mmtk_num_entries_in_isymtab,
mmtk_first_in_sptab,
mmtk_first_in_isymtab,
};
/* LANGUAGE EXTENSION */
#define unless(c) if(!(c))
#define LENGTH(array) (sizeof(array) / sizeof(array[0]))
/* CONFIGURATION PARAMETERS */
#define SYMMAX ((size_t)255) /* max length of a symbol */
#define MSGMAX ((size_t)255) /* max length of error message */
#define STRMAX ((size_t)255) /* max length of a string */
void* my_malloc(size_t size) {
size_t true_size = size;
if ((size & 7) != 0) {
// Align up the allocation to word size
true_size = (size+8)&(~7);
}
void* addr = mmtk_alloc( global_mutator, true_size, 8, 0, 0 );
if (mmtk_is_mmtk_object(addr)) {
printf("Before mmtk_post_alloc mmtk_is_mmtk_object returned %d when it should return 0\n", mmtk_is_mmtk_object(addr));
}
//printf("%s:%d:%s mmtk allocate %lu bytes -> %p\n", __FILE__, __LINE__, __FUNCTION__, true_size, addr );
// convert addr to object_reference
mmtk_post_alloc( global_mutator, addr, true_size, 0 );
if (!mmtk_is_mmtk_object(addr)) {
printf("Before mmtk_post_alloc mmtk_is_mmtk_object returned %d when it should return 1\n", mmtk_is_mmtk_object(addr));
}
return addr;
}
void my_free(void* addr) {
// Do nothing
}
/* DATA TYPES */
/* obj_t -- scheme object type
*
* obj_t is a pointer to a union, obj_u, which has members for
* each scheme representation.
*
* The obj_u also has a "type" member. Each representation
* structure also has a "type" field first. ANSI C guarantees
* that these type fields correspond [section?].
*
* Objects are allocated by allocating one of the representation
* structures and casting the pointer to it to type obj_t. This
* allows objects of different sizes to be represented by the
* same type.
*
* To access an object, check its type by reading TYPE(obj), then
* access the fields of the representation, e.g.
* if(TYPE(obj) == TYPE_PAIR) fiddle_with(CAR(obj));
*/
typedef union obj_u *obj_t;
typedef obj_t (*entry_t)(obj_t env, obj_t op_env, obj_t operator, obj_t rands);
typedef int type_t;
enum {
TYPE_PAIR,
TYPE_INTEGER,
TYPE_SYMBOL,
TYPE_SPECIAL,
TYPE_OPERATOR,
TYPE_STRING,
TYPE_PORT,
TYPE_PROMISE,
TYPE_CHARACTER,
TYPE_VECTOR,
TYPE_TABLE,
TYPE_BUCKETS
};
typedef struct type_s {
type_t type;
} type_s;
typedef struct pair_s {
type_t type; /* TYPE_PAIR */
obj_t car, cdr; /* first and second projections */
} pair_s;
typedef struct symbol_s {
type_t type; /* TYPE_SYMBOL */
size_t length; /* length of symbol string (excl. NUL) */
char string[1]; /* symbol string, NUL terminated */
} symbol_s;
typedef struct integer_s {
type_t type; /* TYPE_INTEGER */
long integer; /* the integer */
} integer_s;
typedef struct special_s {
type_t type; /* TYPE_SPECIAL */
char *name; /* printed representation, NUL terminated */
} special_s;
typedef struct operator_s {
type_t type; /* TYPE_OPERATOR */
char *name; /* printed name, NUL terminated */
entry_t entry; /* entry point -- see eval() */
obj_t arguments, body; /* function arguments and code */
obj_t env, op_env; /* closure environments */
} operator_s;
typedef struct string_s {
type_t type; /* TYPE_STRING */
size_t length; /* number of chars in string */
char string[1]; /* string, NUL terminated */
} string_s;
typedef struct port_s {
type_t type; /* TYPE_PORT */
obj_t name; /* name of stream */
FILE *stream;
} port_s;
typedef struct character_s {
type_t type; /* TYPE_CHARACTER */
char c; /* the character */
} character_s;
typedef struct vector_s {
type_t type; /* TYPE_VECTOR */
size_t length; /* number of elements */
obj_t vector[1]; /* vector elements */
} vector_s;
typedef unsigned long (*hash_t)(obj_t obj);
typedef int (*cmp_t)(obj_t obj1, obj_t obj2);
typedef struct table_s {
type_t type; /* TYPE_TABLE */
hash_t hash; /* hash function */
cmp_t cmp; /* comparison function */
obj_t buckets; /* hash buckets */
} table_s;
typedef struct buckets_s {
type_t type; /* TYPE_BUCKETS */
size_t length; /* number of buckets */
size_t used; /* number of buckets in use */
size_t deleted; /* number of deleted buckets */
struct bucket_s {
obj_t key, value;
} bucket[1]; /* hash buckets */
} buckets_s;
typedef union obj_u {
type_s type; /* one of TYPE_* */
pair_s pair;
symbol_s symbol;
integer_s integer;
special_s special;
operator_s operator;
string_s string;
port_s port;
character_s character;
vector_s vector;
table_s table;
buckets_s buckets;
} obj_s;
/* structure macros */
#define TYPE(obj) ((obj)->type.type)
#define CAR(obj) ((obj)->pair.car)
#define CDR(obj) ((obj)->pair.cdr)
#define CAAR(obj) CAR(CAR(obj))
#define CADR(obj) CAR(CDR(obj))
#define CDAR(obj) CDR(CAR(obj))
#define CDDR(obj) CDR(CDR(obj))
#define CADDR(obj) CAR(CDDR(obj))
#define CDDDR(obj) CDR(CDDR(obj))
#define CDDAR(obj) CDR(CDAR(obj))
#define CADAR(obj) CAR(CDAR(obj))
/* GLOBAL DATA */
/* total -- total allocated bytes */
static size_t total;
/* symtab -- symbol table
*
* The symbol table is a hash-table containing objects of TYPE_SYMBOL.
* When a string is "interned" it is looked up in the table, and added
* only if it is not there. This guarantees that all symbols which
* are equal are actually the same object.
*/
static obj_t *symtab;
static size_t symtab_size;
/* special objects
*
* These global variables are initialized to point to objects of
* TYPE_SPECIAL by main. They are used as markers for various
* special purposes.
*/
static obj_t obj_empty; /* (), the empty list */
static obj_t obj_eof; /* end of file */
static obj_t obj_error; /* error indicator */
static obj_t obj_true; /* #t, boolean true */
static obj_t obj_false; /* #f, boolean false */
static obj_t obj_undefined; /* undefined result indicator */
static obj_t obj_tail; /* tail recursion indicator */
static obj_t obj_deleted; /* deleted key in hashtable */
/* predefined symbols
*
* These global variables are initialized to point to interned
* objects of TYPE_SYMBOL. They have special meaning in the
* Scheme language, and are used by the evaluator to parse code.
*/
static obj_t obj_quote; /* "quote" symbol */
static obj_t obj_quasiquote; /* "quasiquote" symbol */
static obj_t obj_lambda; /* "lambda" symbol */
static obj_t obj_begin; /* "begin" symbol */
static obj_t obj_else; /* "else" symbol */
static obj_t obj_unquote; /* "unquote" symbol */
static obj_t obj_unquote_splic; /* "unquote-splicing" symbol */
/* error handler
*
* The error_handler variable is initialized to point at a
* jmp_buf to which the "error" function longjmps if there is
* any kind of error during evaluation. It can be set up by
* any enclosing function that wants to catch errors. There
* is a default error handler in main, in the read-eval-print
* loop. The error function also writes an error message
* into "error_message" before longjmping, and this can be
* displayed to the user when catching the error.
*
* [An error code should also be passed so that the error can
* be decoded by enclosing code.]
*/
static jmp_buf *error_handler = NULL;
static char error_message[MSGMAX+1];
/* SUPPORT FUNCTIONS */
/* handle_SIGUSR1 set a global variable when SIGUSR1 is detected
*/
int global_sigusr1 = 0;
void handle_SIGUSR1(int sig) {
global_sigusr1 = 1;
}
void setup_sigusr1() {
signal( SIGUSR1, handle_SIGUSR1 );
}
void wait_for_user_signal(const char* message) {
printf("%s:%d:%s\n"
"Paused for SIGUSR1 pid is %d\n"
"%s\n",
__FILE__, __LINE__, __FUNCTION__, getpid(), message);
if (getenv("CLASP_EXIT_ON_WAIT_FOR_USER_SIGNAL")) {
exit(1);
}
double dsec = 0.1;
double seconds = floor(dsec);
double frac_seconds = dsec - seconds;
double nanoseconds = (frac_seconds * 1000000000.0);
struct timespec ts;
while (!global_sigusr1) {
ts.tv_sec = seconds;
ts.tv_nsec = nanoseconds;
int code = nanosleep(&ts, &ts);
if (code < 0) {
if (errno == EINTR)
continue;
printf("%s:%d:%s nanosleep return error: %d\n", __FILE__, __LINE__, __FUNCTION__, errno);
abort();
}
}
printf("%s:%d:%s Received SIGUSR1\n", __FILE__, __LINE__, __FUNCTION__);
global_sigusr1 = 0;
}
/* error -- throw an error condition
*
* The "error" function takes a printf-style format string
* and arguments, writes the message into error_message and
* longjmps to *error_handler. There must be a setjmp at
* the other end to catch the condition and display the
* message.
*/
static void error(char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(error_message, sizeof error_message, format, args);
va_end(args);
if (error_handler) {
longjmp(*error_handler, 1);
} else {
fflush(stdout);
fprintf(stderr, "Fatal error during initialization: %s\n",
error_message);
abort();
}
}
/* make_* -- object constructors
*
* Each object type has a function here which allocates an
* instance of that type.
*/
static obj_t make_bool(int condition)
{
return condition ? obj_true : obj_false;
}
static obj_t make_pair(obj_t car, obj_t cdr)
{
obj_t obj = (obj_t)my_malloc(sizeof(pair_s));
if(obj == NULL) error("out of memory");
total += sizeof(pair_s);
obj->pair.type = TYPE_PAIR;
CAR(obj) = car;
CDR(obj) = cdr;
return obj;
}
static obj_t make_integer(long integer)
{
obj_t obj = (obj_t)my_malloc(sizeof(integer_s));
if(obj == NULL) error("out of memory");
total += sizeof(integer_s);
obj->integer.type = TYPE_INTEGER;
obj->integer.integer = integer;
return obj;
}
static obj_t make_symbol(size_t length, char string[])
{
size_t size = offsetof(symbol_s, string) + length+1;
obj_t obj = (obj_t)my_malloc(size);
if(obj == NULL) error("out of memory");
total += size;
obj->symbol.type = TYPE_SYMBOL;
obj->symbol.length = length;
memcpy(obj->symbol.string, string, length+1);
return obj;
}
static obj_t make_string(size_t length, char string[])
{
size_t size = offsetof(string_s, string) + length+1;
obj_t obj = (obj_t)my_malloc(size);
if(obj == NULL) error("out of memory");
total += size;
obj->string.type = TYPE_STRING;
obj->string.length = length;
if (string) memcpy(obj->string.string, string, length+1);
else memset(obj->string.string, 0, length+1);
return obj;
}
static obj_t make_special(char *string)
{
obj_t obj = (obj_t)my_malloc(sizeof(special_s));
if(obj == NULL) error("out of memory");
total += sizeof(special_s);
obj->special.type = TYPE_SPECIAL;
obj->special.name = string;
return obj;
}
static obj_t make_operator(char *name,
entry_t entry, obj_t arguments,
obj_t body, obj_t env, obj_t op_env)
{
obj_t obj = (obj_t)my_malloc(sizeof(operator_s));
if(obj == NULL) error("out of memory");
total += sizeof(operator_s);
obj->operator.type = TYPE_OPERATOR;
obj->operator.name = name;
obj->operator.entry = entry;
obj->operator.arguments = arguments;
obj->operator.body = body;
obj->operator.env = env;
obj->operator.op_env = op_env;
return obj;
}
static obj_t make_port(obj_t name, FILE *stream)
{
obj_t obj = (obj_t)my_malloc(sizeof(port_s));
if(obj == NULL) error("out of memory");
total += sizeof(port_s);
obj->port.type = TYPE_PORT;
obj->port.name = name;
obj->port.stream = stream;
return obj;
}
static obj_t make_character(char c)
{
obj_t obj = (obj_t)my_malloc(sizeof(character_s));
if(obj == NULL) error("out of memory");
total += sizeof(character_s);
obj->character.type = TYPE_CHARACTER;
obj->character.c = c;
return obj;
}
static obj_t make_vector(size_t length, obj_t fill)
{
size_t size = offsetof(vector_s, vector) + length * sizeof(obj_t);
size_t i;
obj_t obj = (obj_t)my_malloc(size);
if(obj == NULL) error("out of memory");
total += size;
obj->vector.type = TYPE_VECTOR;
obj->vector.length = length;
for(i = 0; i < length; ++i)
obj->vector.vector[i] = fill;
return obj;
}
static obj_t make_buckets(size_t length)
{
size_t i, size = offsetof(buckets_s, bucket) + length * 2 * sizeof(obj_t);
obj_t obj = (obj_t)my_malloc(size);
if(obj == NULL) error("out of memory");
total += size;
obj->buckets.type = TYPE_BUCKETS;
obj->buckets.length = length;
obj->buckets.used = 0;
obj->buckets.deleted = 0;
for(i = 0; i < length; ++i) {
obj->buckets.bucket[i].key = NULL;
obj->buckets.bucket[i].value = NULL;
}
return obj;
}
static obj_t make_table(size_t length, hash_t hashf, cmp_t cmpf)
{
size_t l, size = sizeof(table_s);
obj_t obj = (obj_t)my_malloc(size);
if(obj == NULL) error("out of memory");
total += size;
obj->table.type = TYPE_TABLE;
obj->table.hash = hashf;
obj->table.cmp = cmpf;
/* round up to next power of 2 */
for(l = 1; l < length; l *= 2);
obj->table.buckets = make_buckets(l);
return obj;
}
/* getnbc -- get next non-blank char from stream */
static int getnbc(FILE *stream)
{
int c;
do {
c = getc(stream);
if(c == ';') {
do
c = getc(stream);
while(c != EOF && c != '\n');
}
} while(isspace(c));
return c;
}
/* isealpha -- test for "extended alphabetic" char
*
* Scheme symbols may contain any "extended alphabetic"
* character (see section 2.1 of R4RS). This function
* returns non-zero if a character is in the set of
* extended characters.
*/
static int isealpha(int c)
{
return strchr("+-.*/<=>!?:$%_&~^", c) != NULL;
}
/* hash -- hash a string to an unsigned long
*
* This hash function was derived (with permission) from
* Paul Haahr's hash in the most excellent rc 1.4.
*/
static unsigned long hash(const char *s, size_t length) {
char c;
unsigned long h=0;
size_t i = 0;
switch(length % 4) {
do {
c=s[i++]; h+=(c<<17)^(c<<11)^(c<<5)^(c>>1);
case 3:
c=s[i++]; h^=(c<<14)+(c<<7)+(c<<4)+c;
case 2:
c=s[i++]; h^=(~c<<11)|((c<<3)^(c>>1));
case 1:
c=s[i++]; h-=(c<<16)|(c<<9)|(c<<2)|(c&3);
case 0:
;
} while(i < length);
}
return h;
}
/* find -- find entry for symbol in symbol table
*
* Look for a symbol matching the string in the symbol table.
* If the symbol was found, returns the address of the symbol
* table entry which points to the symbol. Otherwise it
* either returns the address of a NULL entry into which the
* new symbol should be inserted, or NULL if the symbol table
* is full.
*/
static obj_t *find(char *string) {
unsigned long i, h, probe;
h = hash(string, strlen(string));
probe = (h >> 8) | 1;
h &= (symtab_size-1);
i = h;
do {
if(symtab[i] == NULL ||
strcmp(string, symtab[i]->symbol.string) == 0)
return &symtab[i];
i = (i+probe) & (symtab_size-1);
} while(i != h);
return NULL;
}
/* rehash -- double size of symbol table */
static void rehash(void) {
obj_t *old_symtab = symtab;
unsigned old_symtab_size = symtab_size;
unsigned i;
symtab_size *= 2;
symtab = my_malloc(sizeof(obj_t) * symtab_size);
if(symtab == NULL) error("out of memory");
/* Initialize the new table to NULL so that "find" will work. */
for(i = 0; i < symtab_size; ++i)
symtab[i] = NULL;
for(i = 0; i < old_symtab_size; ++i)
if(old_symtab[i] != NULL) {
obj_t *where = find(old_symtab[i]->symbol.string);
assert(where != NULL); /* new table shouldn't be full */
assert(*where == NULL); /* shouldn't be in new table */
*where = old_symtab[i];
}
my_free(old_symtab);
}
/* union-find string in symbol table, rehashing if necessary */
static obj_t intern(char *string) {
obj_t *where;
where = find(string);
if(where == NULL) {
rehash();
where = find(string);
assert(where != NULL); /* shouldn't be full after rehash */
}
if(*where == NULL) /* symbol not found in table */
*where = make_symbol(strlen(string), string);
return *where;
}
/* Hash table implementation */
static unsigned long eq_hash(obj_t obj)
{
union {char s[sizeof(obj_t)]; obj_t addr;} u = {""};
u.addr = obj;
return hash(u.s, sizeof(obj_t));
}
static int eqp(obj_t obj1, obj_t obj2)
{
return obj1 == obj2;
}
static unsigned long eqv_hash(obj_t obj)
{
switch(TYPE(obj)) {
case TYPE_INTEGER:
return obj->integer.integer;
case TYPE_CHARACTER:
return obj->character.c;
default:
return eq_hash(obj);
}
}
static int eqvp(obj_t obj1, obj_t obj2)
{
if (obj1 == obj2)
return 1;
if (TYPE(obj1) != TYPE(obj2))
return 0;
switch(TYPE(obj1)) {
case TYPE_INTEGER:
return obj1->integer.integer == obj2->integer.integer;
case TYPE_CHARACTER:
return obj1->character.c == obj2->character.c;
default:
return 0;
}
}
static unsigned long string_hash(obj_t obj)
{
unless(TYPE(obj) == TYPE_STRING)
error("string-hash: argument must be a string");
return hash(obj->string.string, obj->string.length);
}
static int string_equalp(obj_t obj1, obj_t obj2)
{
return obj1 == obj2 ||
(TYPE(obj1) == TYPE_STRING &&
TYPE(obj2) == TYPE_STRING &&
obj1->string.length == obj2->string.length &&
0 == strcmp(obj1->string.string, obj2->string.string));
}
static struct bucket_s *buckets_find(obj_t tbl, obj_t buckets, obj_t key)
{
unsigned long i, h, probe;
struct bucket_s *result = NULL;
assert(TYPE(tbl) == TYPE_TABLE);
assert(TYPE(buckets) == TYPE_BUCKETS);
h = tbl->table.hash(key);
probe = (h >> 8) | 1;
h &= (buckets->buckets.length-1);
i = h;
do {
struct bucket_s *b = &buckets->buckets.bucket[i];
if(b->key == NULL || tbl->table.cmp(b->key, key))
return b;
if(result == NULL && b->key == obj_deleted)
result = b;
i = (i+probe) & (buckets->buckets.length-1);
} while(i != h);
return result;
}
static size_t table_size(obj_t tbl)
{
size_t used, deleted;
assert(TYPE(tbl) == TYPE_TABLE);
used = tbl->table.buckets->buckets.used;
deleted = tbl->table.buckets->buckets.deleted;
assert(used >= deleted);
return used - deleted;
}
static void table_rehash(obj_t tbl)
{
size_t i, old_length, new_length;
obj_t new_buckets;
assert(TYPE(tbl) == TYPE_TABLE);
old_length = tbl->table.buckets->buckets.length;
new_length = old_length * 2;
new_buckets = make_buckets(new_length);
for (i = 0; i < old_length; ++i) {
struct bucket_s *old_b = &tbl->table.buckets->buckets.bucket[i];
if (old_b->key != NULL && old_b->key != obj_deleted) {
struct bucket_s *b = buckets_find(tbl, new_buckets, old_b->key);
assert(b != NULL); /* new table shouldn't be full */
assert(b->key == NULL); /* shouldn't be in new table */
*b = *old_b;
++ new_buckets->buckets.used;
}
}
assert(new_buckets->buckets.used == table_size(tbl));
tbl->table.buckets = new_buckets;
}
static obj_t table_ref(obj_t tbl, obj_t key)
{
struct bucket_s *b;
assert(TYPE(tbl) == TYPE_TABLE);
b = buckets_find(tbl, tbl->table.buckets, key);
if (b && b->key != NULL && b->key != obj_deleted)
return b->value;
return NULL;
}
static int table_full(obj_t tbl)
{
assert(TYPE(tbl) == TYPE_TABLE);
return tbl->table.buckets->buckets.used >= tbl->table.buckets->buckets.length / 2;
}
static void table_set(obj_t tbl, obj_t key, obj_t value)
{
struct bucket_s *b;
assert(TYPE(tbl) == TYPE_TABLE);
if (table_full(tbl) || (b = buckets_find(tbl, tbl->table.buckets, key)) == NULL) {
table_rehash(tbl);
b = buckets_find(tbl, tbl->table.buckets, key);
assert(b != NULL); /* shouldn't be full after rehash */
}
if (b->key == NULL) {
b->key = key;
++ tbl->table.buckets->buckets.used;
} else if (b->key == obj_deleted) {
b->key = key;
assert(tbl->table.buckets->buckets.deleted > 0);
-- tbl->table.buckets->buckets.deleted;
}
b->value = value;
}
static void table_delete(obj_t tbl, obj_t key)
{
struct bucket_s *b;
assert(TYPE(tbl) == TYPE_TABLE);
b = buckets_find(tbl, tbl->table.buckets, key);
if (b != NULL && b->key != NULL) {
b->key = obj_deleted;
++ tbl->table.buckets->buckets.deleted;
}
}
static void print(obj_t obj, unsigned depth, FILE *stream)
{
switch(TYPE(obj)) {
case TYPE_INTEGER: {
fprintf(stream, "%ld", obj->integer.integer);
} break;
case TYPE_SYMBOL: {
fputs(obj->symbol.string, stream);
} break;
case TYPE_SPECIAL: {
fputs(obj->special.name, stream);
} break;
case TYPE_PORT: {
assert(TYPE(obj->port.name) == TYPE_STRING);
fprintf(stream, "#[port \"%s\"]",
obj->port.name->string.string);
} break;
case TYPE_STRING: {
size_t i;
putc('"', stream);
for(i = 0; i < obj->string.length; ++i) {
char c = obj->string.string[i];
switch(c) {
case '\\': fputs("\\\\", stream); break;
case '"': fputs("\\\"", stream); break;
default: putc(c, stream); break;
}
}
putc('"', stream);
} break;
case TYPE_PROMISE: {
assert(CAR(obj) == obj_true || CAR(obj) == obj_false);
fprintf(stream, "#[%sevaluated promise ",
CAR(obj) == obj_false ? "un" : "");
print(CDR(obj), depth - 1, stream);
putc(']', stream);
} break;
case TYPE_PAIR: {
if(TYPE(CAR(obj)) == TYPE_SYMBOL &&
TYPE(CDR(obj)) == TYPE_PAIR &&
CDDR(obj) == obj_empty) {
if(CAR(obj) == obj_quote) {
putc('\'', stream);
if(depth == 0)
fputs("...", stream);
else
print(CADR(obj), depth - 1, stream);
break;
}
if(CAR(obj) == obj_quasiquote) {
putc('`', stream);
if(depth == 0)
fputs("...", stream);
else
print(CADR(obj), depth - 1, stream);
break;
}
if(CAR(obj) == obj_unquote) {
putc(',', stream);
if(depth == 0)
fputs("...", stream);
else
print(CADR(obj), depth - 1, stream);
break;
}
if(CAR(obj) == obj_unquote_splic) {
fputs(",@", stream);
if(depth == 0)
fputs("...", stream);
else
print(CADR(obj), depth - 1, stream);
break;
}
}
putc('(', stream);
if(depth == 0)
fputs("...", stream);
else {
for(;;) {
print(CAR(obj), depth - 1, stream);
obj = CDR(obj);
if(TYPE(obj) != TYPE_PAIR) break;
putc(' ', stream);
}
if(obj != obj_empty) {
fputs(" . ", stream);
print(obj, depth - 1, stream);
}
}
putc(')', stream);
} break;
case TYPE_VECTOR: {
fputs("#(", stream);
if(depth == 0)
fputs("...", stream);