forked from bazurbat/chicken-scheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.c
9498 lines (7279 loc) · 229 KB
/
runtime.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
/* runtime.c - Runtime code for compiler generated executables
;
; Copyright (c) 2008-2014, The Chicken Team
; Copyright (c) 2000-2007, Felix L. Winkelmann
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
; disclaimer.
; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
; disclaimer in the documentation and/or other materials provided with the distribution.
; Neither the name of the author nor the names of its contributors may be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
*/
#include "chicken.h"
#include <assert.h>
#include <errno.h>
#include <float.h>
#include <signal.h>
#include <sys/stat.h>
#ifdef HAVE_SYSEXITS_H
# include <sysexits.h>
#endif
#ifdef __ANDROID__
# include <android/log.h>
#endif
#if !defined(PIC)
# define NO_DLOAD2
#endif
#ifndef NO_DLOAD2
# ifdef HAVE_DLFCN_H
# include <dlfcn.h>
# endif
# ifdef HAVE_DL_H
# include <dl.h>
# endif
#endif
#ifndef EX_SOFTWARE
# define EX_SOFTWARE 70
#endif
#ifndef EOVERFLOW
# define EOVERFLOW 0
#endif
/* TODO: Include sys/select.h? Windows doesn't seem to have it... */
#ifdef HAVE_POSIX_POLL
# include <poll.h>
#endif
#if !defined(C_NONUNIX)
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/wait.h>
#else
#ifdef ECOS
#include <cyg/kernel/kapi.h>
static C_TLS int timezone;
#define NSIG 32
#endif
#endif
#ifndef RTLD_GLOBAL
# define RTLD_GLOBAL 0
#endif
#ifndef RTLD_NOW
# define RTLD_NOW 0
#endif
#ifndef RTLD_LOCAL
# define RTLD_LOCAL 0
#endif
#ifndef RTLD_LAZY
# define RTLD_LAZY 0
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
/* Include winsock2 to get select() for check_fd_ready() */
# include <winsock2.h>
# include <windows.h>
#endif
#ifdef HAVE_CONFIG_H
# ifdef PACKAGE
# undef PACKAGE
# endif
# ifdef VERSION
# undef VERSION
# endif
# include <chicken-config.h>
# ifndef HAVE_ALLOCA
# error this package requires "alloca()"
# endif
#endif
#ifdef C_HACKED_APPLY
# if defined(C_MACOSX) || defined(__MINGW32__) || defined(__CYGWIN__)
extern void C_do_apply_hack(void *proc, C_word *args, int count) C_noret;
# else
extern void _C_do_apply_hack(void *proc, C_word *args, int count) C_noret;
# define C_do_apply_hack _C_do_apply_hack
# endif
#endif
#if defined(C_NO_HACKED_APPLY) && defined(C_HACKED_APPLY)
# undef C_HACKED_APPLY
#endif
/* Parameters: */
#define RELAX_MULTIVAL_CHECK
#ifdef C_SIXTY_FOUR
# define DEFAULT_STACK_SIZE (1024 * 1024)
#else
# define DEFAULT_STACK_SIZE (256 * 1024)
#endif
#define DEFAULT_SYMBOL_TABLE_SIZE 2999
#define DEFAULT_HEAP_SIZE DEFAULT_STACK_SIZE
#define MINIMAL_HEAP_SIZE DEFAULT_STACK_SIZE
#define DEFAULT_MAXIMAL_HEAP_SIZE 0x7ffffff0
#define DEFAULT_HEAP_GROWTH 200
#define DEFAULT_HEAP_SHRINKAGE 50
#define DEFAULT_HEAP_SHRINKAGE_USED 25
#define DEFAULT_FORWARDING_TABLE_SIZE 32
#define DEFAULT_LOCATIVE_TABLE_SIZE 32
#define DEFAULT_COLLECTIBLES_SIZE 1024
#define DEFAULT_TRACE_BUFFER_SIZE 16
#define MIN_TRACE_BUFFER_SIZE 3
#define MAX_HASH_PREFIX 64
#define WEAK_TABLE_SIZE 997
#define WEAK_HASH_ITERATIONS 4
#define WEAK_HASH_DISPLACEMENT 7
#define WEAK_COUNTER_MASK 3
#define WEAK_COUNTER_MAX 2
#define TEMPORARY_STACK_SIZE 2048
#define STRING_BUFFER_SIZE 4096
#define DEFAULT_MUTATION_STACK_SIZE 1024
#define FILE_INFO_SIZE 7
#define MAX_PENDING_INTERRUPTS 100
#ifdef C_DOUBLE_IS_32_BITS
# define FLONUM_PRINT_PRECISION 7
#else
# define FLONUM_PRINT_PRECISION 15
#endif
#define WORDS_PER_FLONUM C_SIZEOF_FLONUM
#define INITIAL_TIMER_INTERRUPT_PERIOD 10000
#define HDUMP_TABLE_SIZE 1001
/* only for relevant for Windows: */
#define MAXIMAL_NUMBER_OF_COMMAND_LINE_ARGUMENTS 256
/* Constants: */
#ifdef C_SIXTY_FOUR
# define ALIGNMENT_HOLE_MARKER ((C_word)0xfffffffffffffffeL)
# define FORWARDING_BIT_SHIFT 63
# define UWORD_FORMAT_STRING "0x%016lx"
# define UWORD_COUNT_FORMAT_STRING "%u"
#else
# define ALIGNMENT_HOLE_MARKER ((C_word)0xfffffffe)
# define FORWARDING_BIT_SHIFT 31
# define UWORD_FORMAT_STRING "0x%08x"
# define UWORD_COUNT_FORMAT_STRING "%u"
#endif
#ifdef C_LLP
# define LONG_FORMAT_STRING "%lldf"
#else
# define LONG_FORMAT_STRING "%ld"
#endif
#define GC_MINOR 0
#define GC_MAJOR 1
#define GC_REALLOC 2
/* Macros: */
#define nmax(x, y) ((x) > (y) ? (x) : (y))
#define nmin(x, y) ((x) < (y) ? (x) : (y))
#define percentage(n, p) ((C_long)(((double)(n) * (double)p) / 100))
#define is_fptr(x) (((x) & C_GC_FORWARDING_BIT) != 0)
#define ptr_to_fptr(x) ((((x) >> FORWARDING_BIT_SHIFT) & 1) | C_GC_FORWARDING_BIT | ((x) & ~1))
#define fptr_to_ptr(x) (((x) << FORWARDING_BIT_SHIFT) | ((x) & ~(C_GC_FORWARDING_BIT | 1)))
#define C_check_flonum(x, w) if(C_immediatep(x) || C_block_header(x) != C_FLONUM_TAG) \
barf(C_BAD_ARGUMENT_TYPE_NO_FLONUM_ERROR, w, x);
#define C_check_real(x, w, v) if(((x) & C_FIXNUM_BIT) != 0) v = C_unfix(x); \
else if(C_immediatep(x) || C_block_header(x) != C_FLONUM_TAG) \
barf(C_BAD_ARGUMENT_TYPE_NO_NUMBER_ERROR, w, x); \
else v = C_flonum_magnitude(x);
/* these could be shorter in unsafe mode: */
#define C_check_int(x, f, n, w) if(((x) & C_FIXNUM_BIT) != 0) n = C_unfix(x); \
else if(C_immediatep(x) || C_block_header(x) != C_FLONUM_TAG) \
barf(C_BAD_ARGUMENT_TYPE_NO_NUMBER_ERROR, w, x); \
else { double _m; \
f = C_flonum_magnitude(x); \
if(modf(f, &_m) != 0.0 || f < C_WORD_MIN || f > C_WORD_MAX) \
barf(C_BAD_ARGUMENT_TYPE_NO_INTEGER_ERROR, w, x); \
else n = (C_word)f; \
}
#ifdef BITWISE_UINT_ONLY
#define C_check_uint(x, f, n, w) if(((x) & C_FIXNUM_BIT) != 0) n = C_unfix(x); \
else if(C_immediatep(x) || C_block_header(x) != C_FLONUM_TAG) \
barf(C_BAD_ARGUMENT_TYPE_NO_NUMBER_ERROR, w, x); \
else { double _m; \
f = C_flonum_magnitude(x); \
if(modf(f, &_m) != 0.0 || f < 0 || f > C_UWORD_MAX) \
barf(C_BAD_ARGUMENT_TYPE_NO_UINTEGER_ERROR, w, x); \
else n = (C_uword)f; \
}
#else
#define C_check_uint(x, f, n, w) if(((x) & C_FIXNUM_BIT) != 0) n = C_unfix(x); \
else if(C_immediatep(x) || C_block_header(x) != C_FLONUM_TAG) \
barf(C_BAD_ARGUMENT_TYPE_NO_NUMBER_ERROR, w, x); \
else { double _m; \
f = C_flonum_magnitude(x); \
if(modf(f, &_m) != 0.0 || f > C_UWORD_MAX) \
barf(C_BAD_ARGUMENT_TYPE_NO_UINTEGER_ERROR, w, x); \
else n = (C_uword)f; \
}
#endif
#ifdef C_SIXTY_FOUR
#define C_limit_fixnum(n) ((n) & C_MOST_POSITIVE_FIXNUM)
#else
#define C_limit_fixnum(n) (n)
#endif
#define C_pte(name) pt[ i ].id = #name; pt[ i++ ].ptr = (void *)name;
#ifndef SIGBUS
# define SIGBUS 0
#endif
/* Type definitions: */
typedef void (*TOPLEVEL)(C_word c, C_word self, C_word k) C_noret;
typedef void (C_fcall *TRAMPOLINE)(void *proc) C_regparm C_noret;
typedef struct lf_list_struct
{
C_word *lf;
int count;
struct lf_list_struct *next, *prev;
C_PTABLE_ENTRY *ptable;
void *module_handle;
char *module_name;
} LF_LIST;
typedef struct weak_table_entry_struct
{
C_word item, /* item weakly held (symbol) */
container; /* object holding reference to symbol, lowest 3 bits are */
} WEAK_TABLE_ENTRY; /* also used as a counter, saturated at 2 or more */
typedef struct finalizer_node_struct
{
struct finalizer_node_struct
*next,
*previous;
C_word
item,
finalizer;
} FINALIZER_NODE;
typedef struct trace_info_struct
{
C_char *raw;
C_word cooked1, cooked2, thread;
} TRACE_INFO;
typedef struct hdump_bucket_struct
{
C_word key;
int count, total;
struct hdump_bucket_struct *next;
} HDUMP_BUCKET;
/* Variables: */
C_TLS C_word
*C_temporary_stack,
*C_temporary_stack_bottom,
*C_temporary_stack_limit,
*C_stack_limit;
C_TLS C_long
C_timer_interrupt_counter,
C_initial_timer_interrupt_period;
C_TLS C_byte
*C_fromspace_top,
*C_fromspace_limit;
#ifdef HAVE_SIGSETJMP
C_TLS sigjmp_buf C_restart;
#else
C_TLS jmp_buf C_restart;
#endif
C_TLS void *C_restart_address;
C_TLS int C_entry_point_status;
C_TLS int (*C_gc_mutation_hook)(C_word *slot, C_word val);
C_TLS void (*C_gc_trace_hook)(C_word *var, int mode);
C_TLS void (*C_panic_hook)(C_char *msg) = NULL;
C_TLS void (*C_pre_gc_hook)(int mode) = NULL;
C_TLS void (*C_post_gc_hook)(int mode, C_long ms) = NULL;
C_TLS void (C_fcall *C_restart_trampoline)(void *proc) C_regparm C_noret;
C_TLS int
C_gui_mode = 0,
C_abort_on_thread_exceptions,
C_enable_repl,
C_interrupts_enabled,
C_disable_overflow_check,
#ifdef C_COLLECT_ALL_SYMBOLS
C_enable_gcweak = 1,
#else
C_enable_gcweak = 0,
#endif
C_heap_size_is_fixed,
C_trace_buffer_size = DEFAULT_TRACE_BUFFER_SIZE,
C_max_pending_finalizers = C_DEFAULT_MAX_PENDING_FINALIZERS,
C_main_argc;
C_TLS C_uword
C_heap_growth,
C_heap_shrinkage;
C_TLS C_uword C_maximal_heap_size;
C_TLS time_t C_startup_time_seconds;
C_TLS char
**C_main_argv,
*C_dlerror;
static C_TLS TRACE_INFO
*trace_buffer,
*trace_buffer_limit,
*trace_buffer_top;
static C_TLS C_byte
*heapspace1,
*heapspace2,
*fromspace_start,
*tospace_start,
*tospace_top,
*tospace_limit,
*new_tospace_start,
*new_tospace_top,
*new_tospace_limit,
*heap_scan_top;
static C_TLS size_t
heapspace1_size,
heapspace2_size,
heap_size;
static C_TLS C_char
buffer[ STRING_BUFFER_SIZE ],
*private_repository = NULL,
*current_module_name,
*save_string;
static C_TLS C_SYMBOL_TABLE
*symbol_table,
*symbol_table_list;
static C_TLS C_word
**collectibles,
**collectibles_top,
**collectibles_limit,
*saved_stack_limit,
**mutation_stack_bottom,
**mutation_stack_limit,
**mutation_stack_top,
*stack_bottom,
*locative_table,
error_location,
interrupt_hook_symbol,
current_thread_symbol,
error_hook_symbol,
pending_finalizers_symbol,
callback_continuation_stack_symbol,
*forwarding_table;
static C_TLS int
trace_buffer_full,
forwarding_table_size,
return_to_host,
page_size,
show_trace,
fake_tty_flag,
debug_mode,
dump_heap_on_exit,
gc_bell,
gc_report_flag = 0,
gc_mode,
gc_count_1,
gc_count_1_total,
gc_count_2,
weak_table_randomization,
stack_size_changed,
dlopen_flags,
heap_size_changed,
chicken_is_running,
chicken_ran_once,
pass_serious_signals = 1,
callback_continuation_level;
static volatile C_TLS int serious_signal_occurred = 0;
static C_TLS unsigned int
mutation_count,
stack_size;
static C_TLS int chicken_is_initialized;
#ifdef HAVE_SIGSETJMP
static C_TLS sigjmp_buf gc_restart;
#else
static C_TLS jmp_buf gc_restart;
#endif
static C_TLS double
timer_start_ms,
gc_ms,
timer_accumulated_gc_ms,
interrupt_time,
last_interrupt_latency;
static C_TLS LF_LIST *lf_list;
static C_TLS int signal_mapping_table[ NSIG ];
static C_TLS int
locative_table_size,
locative_table_count,
live_finalizer_count,
allocated_finalizer_count,
pending_finalizer_count,
callback_returned_flag;
static C_TLS WEAK_TABLE_ENTRY *weak_item_table;
static C_TLS C_GC_ROOT *gc_root_list = NULL;
static C_TLS FINALIZER_NODE
*finalizer_list,
*finalizer_free_list,
**pending_finalizer_indices;
static C_TLS void *current_module_handle;
static C_TLS int flonum_print_precision = FLONUM_PRINT_PRECISION;
static C_TLS HDUMP_BUCKET **hdump_table;
static C_TLS int
pending_interrupts[ MAX_PENDING_INTERRUPTS ],
pending_interrupts_count,
handling_interrupts;
/* Prototypes: */
static void parse_argv(C_char *cmds);
static void initialize_symbol_table(void);
static void global_signal_handler(int signum);
static C_word arg_val(C_char *arg);
static void barf(int code, char *loc, ...) C_noret;
static void panic(C_char *msg) C_noret;
static void usual_panic(C_char *msg) C_noret;
static void horror(C_char *msg) C_noret;
static void C_fcall initial_trampoline(void *proc) C_regparm C_noret;
static C_ccall void termination_continuation(C_word c, C_word self, C_word result) C_noret;
static void C_fcall mark_system_globals(void) C_regparm;
static void C_fcall really_mark(C_word *x) C_regparm;
static WEAK_TABLE_ENTRY *C_fcall lookup_weak_table_entry(C_word item, C_word container) C_regparm;
static C_ccall void values_continuation(C_word c, C_word closure, C_word dummy, ...) C_noret;
static C_word add_symbol(C_word **ptr, C_word key, C_word string, C_SYMBOL_TABLE *stable);
static C_regparm int C_fcall C_in_new_heapp(C_word x);
static C_word C_fcall hash_string(int len, C_char *str, C_word m, C_word r, int ci) C_regparm;
static C_word C_fcall lookup(C_word key, int len, C_char *str, C_SYMBOL_TABLE *stable) C_regparm;
static double compute_symbol_table_load(double *avg_bucket_len, int *total);
static C_word C_fcall convert_string_to_number(C_char *str, int radix, C_word *fix, double *flo) C_regparm;
static C_word C_fcall maybe_inexact_to_exact(C_word n) C_regparm;
static void C_fcall remark_system_globals(void) C_regparm;
static void C_fcall really_remark(C_word *x) C_regparm;
static C_word C_fcall intern0(C_char *name) C_regparm;
static void C_fcall update_locative_table(int mode) C_regparm;
static LF_LIST *find_module_handle(C_char *name);
static C_ccall void call_cc_wrapper(C_word c, C_word closure, C_word k, C_word result) C_noret;
static C_ccall void call_cc_values_wrapper(C_word c, C_word closure, C_word k, ...) C_noret;
static void gc_2(void *dummy) C_noret;
static void allocate_vector_2(void *dummy) C_noret;
static void get_argv_2(void *dummy) C_noret; /* OBSOLETE */
static void get_argument_2(void *dummy) C_noret; /* OBSOLETE */
static void make_structure_2(void *dummy) C_noret;
static void generic_trampoline(void *dummy) C_noret;
static void get_environment_variable_2(void *dummy) C_noret; /* OBSOLETE */
static void handle_interrupt(void *trampoline, void *proc) C_noret;
static void callback_trampoline(void *dummy) C_noret;
static C_ccall void callback_return_continuation(C_word c, C_word self, C_word r) C_noret;
static void become_2(void *dummy) C_noret;
static void copy_closure_2(void *dummy) C_noret;
static void dump_heap_state_2(void *dummy) C_noret;
static void C_fcall sigsegv_trampoline(void *) C_regparm;
static void C_fcall sigill_trampoline(void *) C_regparm;
static void C_fcall sigfpe_trampoline(void *) C_regparm;
static void C_fcall sigbus_trampoline(void *) C_regparm;
static C_PTABLE_ENTRY *create_initial_ptable();
#if !defined(NO_DLOAD2) && (defined(HAVE_DLFCN_H) || defined(HAVE_DL_H) || (defined(HAVE_LOADLIBRARY) && defined(HAVE_GETPROCADDRESS)))
static void dload_2(void *dummy) C_noret;
#endif
static void
C_dbg(C_char *prefix, C_char *fstr, ...)
{
va_list va;
va_start(va, fstr);
#ifdef __ANDROID__
__android_log_vprint(ANDROID_LOG_DEBUG, prefix, fstr, va);
#else
C_fflush(C_stdout);
C_fprintf(C_stderr, "[%s] ", prefix);
C_vfprintf(C_stderr, fstr, va);
C_fflush(C_stderr);
#endif
va_end(va);
}
/* Startup code: */
int CHICKEN_main(int argc, char *argv[], void *toplevel)
{
C_word h, s, n;
if(C_gui_mode) {
#ifdef _WIN32
parse_argv(GetCommandLine());
argc = C_main_argc;
argv = C_main_argv;
#else
/* ??? */
#endif
}
pass_serious_signals = 0;
CHICKEN_parse_command_line(argc, argv, &h, &s, &n);
if(!CHICKEN_initialize(h, s, n, toplevel))
panic(C_text("cannot initialize - out of memory"));
CHICKEN_run(NULL);
return 0;
}
/* Custom argv parser for Windoze: */
void parse_argv(C_char *cmds)
{
C_char *ptr = cmds,
*bptr0, *bptr, *aptr;
int n = 0;
C_main_argv = (C_char **)malloc(MAXIMAL_NUMBER_OF_COMMAND_LINE_ARGUMENTS * sizeof(C_char *));
if(C_main_argv == NULL)
panic(C_text("cannot allocate argument-list buffer"));
C_main_argc = 0;
for(;;) {
while(isspace((int)(*ptr))) ++ptr;
if(*ptr == '\0') break;
for(bptr0 = bptr = buffer; !isspace((int)(*ptr)) && *ptr != '\0'; *(bptr++) = *(ptr++))
++n;
*bptr = '\0';
aptr = (C_char*) malloc(sizeof(C_char) * (n + 1));
if (!aptr)
panic(C_text("cannot allocate argument buffer"));
C_strlcpy(aptr, bptr0, sizeof(C_char) * (n + 1));
C_main_argv[ C_main_argc++ ] = aptr;
}
}
/* Initialize runtime system: */
int CHICKEN_initialize(int heap, int stack, int symbols, void *toplevel)
{
int i;
#ifdef HAVE_SIGACTION
struct sigaction sa;
#endif
/*FIXME Should have C_tzset in chicken.h? */
#ifdef C_NONUNIX
C_startup_time_seconds = (time_t)0;
# if defined(__MINGW32__)
/* Make sure _tzname, _timezone, and _daylight are set */
_tzset();
# endif
#else
struct timeval tv;
C_gettimeofday(&tv, NULL);
C_startup_time_seconds = tv.tv_sec;
/* Make sure tzname, timezone, and daylight are set */
tzset();
#endif
if(chicken_is_initialized) return 1;
else chicken_is_initialized = 1;
#ifdef __ANDROID__
debug_mode = 2;
#endif
if(debug_mode)
C_dbg(C_text("debug"), C_text("application startup...\n"));
C_panic_hook = usual_panic;
symbol_table_list = NULL;
symbol_table = C_new_symbol_table(".", symbols ? symbols : DEFAULT_SYMBOL_TABLE_SIZE);
if(symbol_table == NULL)
return 0;
page_size = 0;
stack_size = stack ? stack : DEFAULT_STACK_SIZE;
C_set_or_change_heap_size(heap ? heap : DEFAULT_HEAP_SIZE, 0);
/* Allocate temporary stack: */
if((C_temporary_stack_limit = (C_word *)C_malloc(TEMPORARY_STACK_SIZE * sizeof(C_word))) == NULL)
return 0;
C_temporary_stack_bottom = C_temporary_stack_limit + TEMPORARY_STACK_SIZE;
C_temporary_stack = C_temporary_stack_bottom;
/* Allocate mutation stack: */
mutation_stack_bottom = (C_word **)C_malloc(DEFAULT_MUTATION_STACK_SIZE * sizeof(C_word *));
if(mutation_stack_bottom == NULL) return 0;
mutation_stack_top = mutation_stack_bottom;
mutation_stack_limit = mutation_stack_bottom + DEFAULT_MUTATION_STACK_SIZE;
C_gc_mutation_hook = NULL;
C_gc_trace_hook = NULL;
/* Allocate weak item table: */
if(C_enable_gcweak) {
weak_item_table = (WEAK_TABLE_ENTRY *)C_calloc(WEAK_TABLE_SIZE, sizeof(WEAK_TABLE_ENTRY));
if(weak_item_table == NULL)
return 0;
}
/* Initialize finalizer lists: */
finalizer_list = NULL;
finalizer_free_list = NULL;
pending_finalizer_indices =
(FINALIZER_NODE **)C_malloc(C_max_pending_finalizers * sizeof(FINALIZER_NODE *));
if(pending_finalizer_indices == NULL) return 0;
/* Initialize forwarding table: */
forwarding_table =
(C_word *)C_malloc((DEFAULT_FORWARDING_TABLE_SIZE + 1) * 2 * sizeof(C_word));
if(forwarding_table == NULL) return 0;
*forwarding_table = 0;
forwarding_table_size = DEFAULT_FORWARDING_TABLE_SIZE;
/* Initialize locative table: */
locative_table = (C_word *)C_malloc(DEFAULT_LOCATIVE_TABLE_SIZE * sizeof(C_word));
if(locative_table == NULL) return 0;
locative_table_size = DEFAULT_LOCATIVE_TABLE_SIZE;
locative_table_count = 0;
/* Setup collectibles: */
collectibles = (C_word **)C_malloc(sizeof(C_word *) * DEFAULT_COLLECTIBLES_SIZE);
if(collectibles == NULL) return 0;
collectibles_top = collectibles;
collectibles_limit = collectibles + DEFAULT_COLLECTIBLES_SIZE;
gc_root_list = NULL;
/* Initialize global variables: */
if(C_heap_growth <= 0) C_heap_growth = DEFAULT_HEAP_GROWTH;
if(C_heap_shrinkage <= 0) C_heap_shrinkage = DEFAULT_HEAP_SHRINKAGE;
if(C_maximal_heap_size <= 0) C_maximal_heap_size = DEFAULT_MAXIMAL_HEAP_SIZE;
#if !defined(NO_DLOAD2) && defined(HAVE_DLFCN_H)
dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
#else
dlopen_flags = 0;
#endif
/* setup signal handlers */
if(!pass_serious_signals) {
#ifdef HAVE_SIGACTION
sa.sa_flags = 0;
sigfillset(&sa.sa_mask); /* See note in C_establish_signal_handler() */
sa.sa_handler = global_signal_handler;
C_sigaction(SIGBUS, &sa, NULL);
C_sigaction(SIGFPE, &sa, NULL);
C_sigaction(SIGILL, &sa, NULL);
C_sigaction(SIGSEGV, &sa, NULL);
#else
C_signal(SIGBUS, global_signal_handler);
C_signal(SIGILL, global_signal_handler);
C_signal(SIGFPE, global_signal_handler);
C_signal(SIGSEGV, global_signal_handler);
#endif
}
mutation_count = gc_count_1 = gc_count_1_total = gc_count_2 = 0;
lf_list = NULL;
C_register_lf2(NULL, 0, create_initial_ptable());
C_restart_address = toplevel;
C_restart_trampoline = initial_trampoline;
trace_buffer = NULL;
C_clear_trace_buffer();
chicken_is_running = chicken_ran_once = 0;
pending_interrupts_count = 0;
handling_interrupts = 0;
last_interrupt_latency = 0;
C_interrupts_enabled = 1;
C_initial_timer_interrupt_period = INITIAL_TIMER_INTERRUPT_PERIOD;
C_timer_interrupt_counter = INITIAL_TIMER_INTERRUPT_PERIOD;
memset(signal_mapping_table, 0, sizeof(int) * NSIG);
initialize_symbol_table();
C_dlerror = "cannot load compiled code dynamically - this is a statically linked executable";
error_location = C_SCHEME_FALSE;
C_pre_gc_hook = NULL;
C_post_gc_hook = NULL;
live_finalizer_count = 0;
allocated_finalizer_count = 0;
current_module_name = NULL;
current_module_handle = NULL;
callback_continuation_level = 0;
gc_ms = 0;
(void)C_randomize(C_fix(time(NULL)));
return 1;
}
static C_PTABLE_ENTRY *create_initial_ptable()
{
/* IMPORTANT: hardcoded table size - this must match the number of C_pte calls! */
C_PTABLE_ENTRY *pt = (C_PTABLE_ENTRY *)C_malloc(sizeof(C_PTABLE_ENTRY) * 58);
int i = 0;
if(pt == NULL)
panic(C_text("out of memory - cannot create initial ptable"));
C_pte(termination_continuation);
C_pte(callback_return_continuation);
C_pte(values_continuation);
C_pte(call_cc_values_wrapper);
C_pte(call_cc_wrapper);
C_pte(C_gc);
C_pte(C_allocate_vector);
C_pte(C_make_structure);
C_pte(C_ensure_heap_reserve);
C_pte(C_return_to_host);
C_pte(C_get_symbol_table_info);
C_pte(C_get_memory_info);
C_pte(C_decode_seconds);
C_pte(C_get_environment_variable); /* OBSOLETE */
C_pte(C_stop_timer);
C_pte(C_dload);
C_pte(C_set_dlopen_flags);
C_pte(C_become);
C_pte(C_apply_values);
C_pte(C_times);
C_pte(C_minus);
C_pte(C_plus);
C_pte(C_divide);
C_pte(C_nequalp);
C_pte(C_greaterp);
/* IMPORTANT: have you read the comments at the start and the end of this function? */
C_pte(C_lessp);
C_pte(C_greater_or_equal_p);
C_pte(C_less_or_equal_p);
C_pte(C_quotient);
C_pte(C_flonum_fraction);
C_pte(C_flonum_rat);
C_pte(C_expt);
C_pte(C_number_to_string);
C_pte(C_make_symbol);
C_pte(C_string_to_symbol);
C_pte(C_apply);
C_pte(C_call_cc);
C_pte(C_values);
C_pte(C_call_with_values);
C_pte(C_continuation_graft);
C_pte(C_open_file_port);
C_pte(C_software_type);
C_pte(C_machine_type);
C_pte(C_machine_byte_order);
C_pte(C_software_version);
C_pte(C_build_platform);
C_pte(C_make_pointer);
C_pte(C_make_tagged_pointer);
C_pte(C_peek_signed_integer);
C_pte(C_peek_unsigned_integer);
C_pte(C_context_switch);
C_pte(C_register_finalizer);
C_pte(C_locative_ref);
C_pte(C_copy_closure);
C_pte(C_dump_heap_state);
C_pte(C_filter_heap_objects);
C_pte(C_get_argument); /* OBSOLETE */
/* IMPORTANT: did you remember the hardcoded pte table size? */
pt[ i ].id = NULL;
return pt;
}
void *CHICKEN_new_gc_root_2(int finalizable)
{
C_GC_ROOT *r = (C_GC_ROOT *)C_malloc(sizeof(C_GC_ROOT));
if(r == NULL)
panic(C_text("out of memory - cannot allocate GC root"));
r->value = C_SCHEME_UNDEFINED;
r->next = gc_root_list;
r->prev = NULL;
r->finalizable = finalizable;
if(gc_root_list != NULL) gc_root_list->prev = r;
gc_root_list = r;
return (void *)r;
}
void *CHICKEN_new_gc_root()
{
return CHICKEN_new_gc_root_2(0);
}
void *CHICKEN_new_finalizable_gc_root()
{
return CHICKEN_new_gc_root_2(1);
}
void CHICKEN_delete_gc_root(void *root)
{
C_GC_ROOT *r = (C_GC_ROOT *)root;
if(r->prev == NULL) gc_root_list = r->next;
else r->prev->next = r->next;
if(r->next != NULL) r->next->prev = r->prev;
C_free(root);
}
void *CHICKEN_global_lookup(char *name)
{
int
len = C_strlen(name),
key = hash_string(len, name, symbol_table->size, symbol_table->rand, 0);
C_word s;
void *root = CHICKEN_new_gc_root();
if(C_truep(s = lookup(key, len, name, symbol_table))) {
if(C_block_item(s, 0) != C_SCHEME_UNBOUND) {
CHICKEN_gc_root_set(root, s);
return root;
}
}
return NULL;
}
int CHICKEN_is_running()
{
return chicken_is_running;
}
void CHICKEN_interrupt()
{
C_timer_interrupt_counter = 0;
}
C_regparm C_SYMBOL_TABLE *C_new_symbol_table(char *name, unsigned int size)
{
C_SYMBOL_TABLE *stp;
int i;
if((stp = C_find_symbol_table(name)) != NULL) return stp;
if((stp = (C_SYMBOL_TABLE *)C_malloc(sizeof(C_SYMBOL_TABLE))) == NULL)
return NULL;
stp->name = name;
stp->size = size;
stp->next = symbol_table_list;
stp->rand = rand();
if((stp->table = (C_word *)C_malloc(size * sizeof(C_word))) == NULL)
return NULL;
for(i = 0; i < stp->size; stp->table[ i++ ] = C_SCHEME_END_OF_LIST);
symbol_table_list = stp;
return stp;
}
C_regparm void C_delete_symbol_table(C_SYMBOL_TABLE *st)
{
C_SYMBOL_TABLE *stp, *prev = NULL;
for(stp = symbol_table_list; stp != NULL; stp = stp->next)
if(stp == st) {
if(prev != NULL) prev->next = stp->next;
else symbol_table_list = stp->next;
return;
}
}
C_regparm void C_set_symbol_table(C_SYMBOL_TABLE *st)
{
symbol_table = st;
}
C_regparm C_SYMBOL_TABLE *C_find_symbol_table(char *name)
{
C_SYMBOL_TABLE *stp;
for(stp = symbol_table_list; stp != NULL; stp = stp->next)
if(!C_strcmp(name, stp->name)) return stp;
return NULL;
}
C_regparm C_word C_find_symbol(C_word str, C_SYMBOL_TABLE *stable)
{
char *sptr = C_c_string(str);
int
len = C_header_size(str),
key = hash_string(len, sptr, stable->size, stable->rand, 0);
C_word s;
if(C_truep(s = lookup(key, len, sptr, stable))) return s;