forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.c
More file actions
5811 lines (4937 loc) · 174 KB
/
eval.c
File metadata and controls
5811 lines (4937 loc) · 174 KB
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
/*
Racket
Copyright (c) 2004-2014 PLT Design Inc.
Copyright (c) 1995-2001 Matthew Flatt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
libscheme
Copyright (c) 1994 Brent Benson
All rights reserved.
*/
/* This file contains the main interpreter eval-apply loop,
scheme_do_eval(), C and Scheme stack management routines,
and other bridges between evaluation and compilation.
Evaluation:
The bytecode interpreter uses the C stack for continuations, and a
separate Scheme stack for activation-frame variables and collecting
application arguments. Closures are (nearly) flat, so mutable
variables are boxed. A third stack is used for continuation marks,
only as needed.
Tail calls are, for the most part, gotos within scheme_do_eval(). A
C function called by the main evaluation loop can perform a
trampoling tail call via scheme_tail_apply(). The trampoline must
return to its caller without allocating any memory, because an
allocation optimization in the tail-call code assumes no GCs will
occur between the time that a tail call is issued and the time when
it's handled.
Multiple values are returned as a special SCHEME_MULTIPLE_VALUES
token that indicates actual values are stored in the current
thread's record.
The `apply' half of the `eval--apply' loop branches on all possible
application types. Some functions can be JIT-generated native code,
so `apply' is the bridge from interpreted code to JITted
code. Primitive functions (including cons) are implemented by C
functions outside the loop. Continuation applications are handled
directly in scheme_do_eval(). That leaves calls to non-JITted
closures, which are also performed within scheme_do_eval() (so that
most tail calls avoid the trampoline), which is analogous to a
primitive.
The `eval' half of the loop handles all core syntactic forms, such
as application and `letrec's.
When collecting the arguments for an application, scheme_do_eval()
avoids recursive C calls to evaluate arguments by recognizing
easily-evaluated expressions, such as constrants and variable
lookups. This can be viewed as a kind of half-way A-normalization.
Bytecodes are not linear. They're actually trees of expression
nodes.
Top-level variables (global or module) are referenced through the
Scheme stack, so that the variables can be "re-linked" each time a
module is instantiated. Syntax constants are similarly accessed
through the Scheme stack. The global variables and syntax objects
are sometimes called the "prefix", and scheme_push_prefix()
initializes the prefix portion of the stack. This prefix is
captured in a continuation that refers to global or module-level
variables (which is why the closure is not entirely flat). Special
GC support allows a prefix to be pruned to just the globals that
are used by live closures.
Bytecode compilation:
Compilation works in four passes.
The first pass, called "compile", performs most of the work and
tracks variable usage (including whether a variable is mutated or
not). See "compile.c" along with "compenv.c".
The second pass, called "optimize", performs constant propagation,
constant folding, and function inlining; this pass mutates records
produced by the first pass. See "optimize.c".
The third pass, called "resolve", finishes compilation by computing
variable offsets and indirections (often mutating the records
produced by the first pass). It is also responsible for closure
conversion (i.e., converting closure content to arguments) and
lifting (of procedures that close over nothing or only globals).
Beware that the resulting bytecode object is a graph, not a tree,
due to sharing (potentially cyclic) of closures that are "empty"
but actually refer to other "empty" closures. See "resolve.c".
The fourth pass, "sfs", performs another liveness analysis on stack
slots and inserts operations to clear stack slots as necessary to
make execution safe for space. In particular, dead slots need to be
cleared before a non-tail call into arbitrary Racket code. This pass
can mutate the result of the "resolve" pass. See "sfs.c".
Bytecode marshaling and validation:
See "marshal.c" for functions that [un]marshal bytecode form
to/from S-expressions (roughly), which can then be printed using a
"fast-load" format.
The bytecode validator is applied to unmarshaled bytecode to check
that the bytecode is well formed and won't cause any segfaults in
the interpreter or in JITted form. See "validate.c".
Just-in-time compilation:
If the JIT is enabled, then `eval' processes (perhaps unmarshaled
and validated) bytecode one more time: `lambda' and `case-lambda'
forms are converted to native-code generators, instead of bytecode
variants. The code is not actually JITted until it is called; this
preparation step merely sets up a JIT hook for each function. The
preparation pass is a shallow, function (i.e., it doesn't mutate
the original bytecode) pass; the body of a fuction is preparred for
JITting lazily. See "jitprep.c".
*/
#include "schpriv.h"
#include "schrunst.h"
#include "schexpobs.h"
#ifdef MZ_USE_FUTURES
# include "future.h"
#endif
#ifdef USE_STACKAVAIL
#include <malloc.h>
#endif
#ifdef UNIX_FIND_STACK_BOUNDS
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif
#ifdef PTHREAD_STACKSEG_FIND_STACK_BOUNDS
# include <sys/signal.h>
# include <pthread.h>
# include <pthread_np.h>
#endif
#ifdef WINDOWS_FIND_STACK_BOUNDS
#include <windows.h>
#endif
#ifdef BEOS_FIND_STACK_BOUNDS
# include <be/kernel/OS.h>
#endif
#ifdef OSKIT_FIXED_STACK_BOUNDS
# include <oskit/machine/base_stack.h>
#endif
#include "schmach.h"
#ifdef MACOS_STACK_LIMIT
#include <Memory.h>
#endif
#ifdef MZ_USE_FUTURES
# include "future.h"
#endif
#ifdef MZ_USE_JIT
# define INIT_JIT_ON 1
#else
# define INIT_JIT_ON 0
#endif
#ifdef __clang__
# ifdef MZ_PRECISE_GC
# pragma clang diagnostic ignored "-Wself-assign"
# endif
#endif
/* globals */
SHARED_OK int scheme_startup_use_jit = INIT_JIT_ON;
void scheme_set_startup_use_jit(int v) { scheme_startup_use_jit = v; }
SHARED_OK static int valdiate_compile_result = 0;
/* THREAD LOCAL SHARED */
THREAD_LOCAL_DECL(volatile int scheme_fuel_counter);
#ifdef USE_STACK_BOUNDARY_VAR
THREAD_LOCAL_DECL(uintptr_t scheme_stack_boundary);
THREAD_LOCAL_DECL(uintptr_t volatile scheme_jit_stack_boundary);
#endif
THREAD_LOCAL_DECL(int scheme_continuation_application_count);
THREAD_LOCAL_DECL(static int generate_lifts_count);
THREAD_LOCAL_DECL(int scheme_overflow_count);
THREAD_LOCAL_DECL(Scheme_Prefix *scheme_prefix_finalize);
int scheme_get_overflow_count() { return scheme_overflow_count; }
/* read-only globals */
READ_ONLY Scheme_Object *scheme_eval_waiting;
READ_ONLY Scheme_Object *scheme_multiple_values;
/* symbols */
ROSYM static Scheme_Object *app_symbol;
ROSYM static Scheme_Object *datum_symbol;
ROSYM static Scheme_Object *top_symbol;
ROSYM static Scheme_Object *top_level_symbol;
ROSYM static Scheme_Object *define_values_symbol;
ROSYM static Scheme_Object *letrec_values_symbol;
ROSYM static Scheme_Object *lambda_symbol;
ROSYM static Scheme_Object *unknown_symbol;
ROSYM static Scheme_Object *void_link_symbol;
ROSYM static Scheme_Object *quote_symbol;
ROSYM static Scheme_Object *letrec_syntaxes_symbol;
ROSYM static Scheme_Object *begin_symbol;
ROSYM static Scheme_Object *let_values_symbol;
ROSYM static Scheme_Object *internal_define_symbol;
ROSYM static Scheme_Object *module_symbol;
ROSYM static Scheme_Object *module_begin_symbol;
ROSYM static Scheme_Object *expression_symbol;
ROSYM Scheme_Object *scheme_stack_dump_key;
READ_ONLY static Scheme_Object *zero_rands_ptr; /* &zero_rands_ptr is dummy rands pointer */
/* locals */
static Scheme_Object *eval(int argc, Scheme_Object *argv[]);
static Scheme_Object *compile(int argc, Scheme_Object *argv[]);
static Scheme_Object *compiled_p(int argc, Scheme_Object *argv[]);
static Scheme_Object *expand(int argc, Scheme_Object **argv);
static Scheme_Object *local_expand(int argc, Scheme_Object **argv);
static Scheme_Object *local_expand_expr(int argc, Scheme_Object **argv);
static Scheme_Object *local_expand_catch_lifts(int argc, Scheme_Object **argv);
static Scheme_Object *local_transformer_expand(int argc, Scheme_Object **argv);
static Scheme_Object *local_transformer_expand_catch_lifts(int argc, Scheme_Object **argv);
static Scheme_Object *local_eval(int argc, Scheme_Object **argv);
static Scheme_Object *expand_once(int argc, Scheme_Object **argv);
static Scheme_Object *expand_to_top_form(int argc, Scheme_Object **argv);
static Scheme_Object *enable_break(int, Scheme_Object *[]);
static Scheme_Object *current_eval(int argc, Scheme_Object *[]);
static Scheme_Object *current_compile(int argc, Scheme_Object *[]);
static Scheme_Object *eval_stx(int argc, Scheme_Object *argv[]);
static Scheme_Object *compile_stx(int argc, Scheme_Object *argv[]);
static Scheme_Object *expand_stx(int argc, Scheme_Object **argv);
static Scheme_Object *expand_stx_once(int argc, Scheme_Object **argv);
static Scheme_Object *expand_stx_to_top_form(int argc, Scheme_Object **argv);
static Scheme_Object *top_introduce_stx(int argc, Scheme_Object **argv);
static Scheme_Object *allow_set_undefined(int argc, Scheme_Object **argv);
static Scheme_Object *compile_module_constants(int argc, Scheme_Object **argv);
static Scheme_Object *use_jit(int argc, Scheme_Object **argv);
static Scheme_Object *disallow_inline(int argc, Scheme_Object **argv);
static Scheme_Object *_eval_compiled_multi_with_prompt(Scheme_Object *obj, Scheme_Env *env);
void scheme_escape_to_continuation(Scheme_Object *obj, int num_rands, Scheme_Object **rands, Scheme_Object *alt_full);
#ifdef MZ_PRECISE_GC
static void mark_pruned_prefixes(struct NewGC *gc);
#endif
#define cons(x,y) scheme_make_pair(x,y)
typedef void (*DW_PrePost_Proc)(void *);
#ifdef MZ_PRECISE_GC
static void register_traversers(void);
#endif
#define icons scheme_make_pair
/*========================================================================*/
/* initialization */
/*========================================================================*/
void
scheme_init_eval (Scheme_Env *env)
{
#ifdef MZ_PRECISE_GC
register_traversers();
#endif
#ifdef MZ_EVAL_WAITING_CONSTANT
scheme_eval_waiting = MZ_EVAL_WAITING_CONSTANT;
#else
REGISTER_SO(scheme_eval_waiting);
scheme_eval_waiting = scheme_alloc_eternal_object();
scheme_eval_waiting->type = scheme_eval_waiting_type;
#endif
#ifdef MZ_EVAL_WAITING_CONSTANT
scheme_multiple_values = MZ_MULTIPLE_VALUES_CONSTANT;
#else
REGISTER_SO(scheme_multiple_values);
scheme_multiple_values = scheme_alloc_eternal_object();
scheme_multiple_values->type = scheme_multiple_values_type;
#endif
REGISTER_SO(define_values_symbol);
REGISTER_SO(letrec_values_symbol);
REGISTER_SO(lambda_symbol);
REGISTER_SO(unknown_symbol);
REGISTER_SO(void_link_symbol);
REGISTER_SO(quote_symbol);
REGISTER_SO(letrec_syntaxes_symbol);
REGISTER_SO(begin_symbol);
REGISTER_SO(let_values_symbol);
define_values_symbol = scheme_intern_symbol("define-values");
letrec_values_symbol = scheme_intern_symbol("letrec-values");
let_values_symbol = scheme_intern_symbol("let-values");
lambda_symbol = scheme_intern_symbol("lambda");
unknown_symbol = scheme_intern_symbol("unknown");
void_link_symbol = scheme_intern_symbol("-v");
quote_symbol = scheme_intern_symbol("quote");
letrec_syntaxes_symbol = scheme_intern_symbol("letrec-syntaxes+values");
begin_symbol = scheme_intern_symbol("begin");
REGISTER_SO(module_symbol);
REGISTER_SO(module_begin_symbol);
REGISTER_SO(internal_define_symbol);
REGISTER_SO(expression_symbol);
REGISTER_SO(top_level_symbol);
module_symbol = scheme_intern_symbol("module");
module_begin_symbol = scheme_intern_symbol("module-begin");
internal_define_symbol = scheme_intern_symbol("internal-define");
expression_symbol = scheme_intern_symbol("expression");
top_level_symbol = scheme_intern_symbol("top-level");
REGISTER_SO(app_symbol);
REGISTER_SO(datum_symbol);
REGISTER_SO(top_symbol);
app_symbol = scheme_intern_symbol("#%app");
datum_symbol = scheme_intern_symbol("#%datum");
top_symbol = scheme_intern_symbol("#%top");
REGISTER_SO(scheme_stack_dump_key);
scheme_stack_dump_key = scheme_make_symbol("stk"); /* uninterned! */
GLOBAL_PRIM_W_ARITY2("eval", eval, 1, 2, 0, -1, env);
GLOBAL_PRIM_W_ARITY2("eval-syntax", eval_stx, 1, 2, 0, -1, env);
GLOBAL_PRIM_W_ARITY("compile", compile, 1, 1, env);
GLOBAL_PRIM_W_ARITY("compile-syntax", compile_stx, 1, 1, env);
GLOBAL_PRIM_W_ARITY("compiled-expression?", compiled_p, 1, 1, env);
GLOBAL_PRIM_W_ARITY("expand", expand, 1, 1, env);
GLOBAL_PRIM_W_ARITY("expand-syntax", expand_stx, 1, 1, env);
GLOBAL_PRIM_W_ARITY("local-expand", local_expand, 3, 4, env);
GLOBAL_PRIM_W_ARITY2("syntax-local-expand-expression", local_expand_expr, 1, 1, 2, 2, env);
GLOBAL_PRIM_W_ARITY("syntax-local-bind-syntaxes", local_eval, 3, 3, env);
GLOBAL_PRIM_W_ARITY("local-expand/capture-lifts", local_expand_catch_lifts, 3, 5, env);
GLOBAL_PRIM_W_ARITY("local-transformer-expand", local_transformer_expand, 3, 4, env);
GLOBAL_PRIM_W_ARITY("local-transformer-expand/capture-lifts", local_transformer_expand_catch_lifts, 3, 5, env);
GLOBAL_PRIM_W_ARITY("expand-once", expand_once, 1, 1, env);
GLOBAL_PRIM_W_ARITY("expand-syntax-once", expand_stx_once, 1, 1, env);
GLOBAL_PRIM_W_ARITY("expand-to-top-form", expand_to_top_form, 1, 1, env);
GLOBAL_PRIM_W_ARITY("expand-syntax-to-top-form", expand_stx_to_top_form, 1, 1, env);
GLOBAL_PRIM_W_ARITY("namespace-syntax-introduce", top_introduce_stx, 1, 1, env);
GLOBAL_PRIM_W_ARITY("break-enabled", enable_break, 0, 1, env);
GLOBAL_PARAMETER("current-eval", current_eval, MZCONFIG_EVAL_HANDLER, env);
GLOBAL_PARAMETER("current-compile", current_compile, MZCONFIG_COMPILE_HANDLER, env);
GLOBAL_PARAMETER("compile-allow-set!-undefined", allow_set_undefined, MZCONFIG_ALLOW_SET_UNDEFINED, env);
GLOBAL_PARAMETER("compile-enforce-module-constants", compile_module_constants, MZCONFIG_COMPILE_MODULE_CONSTS, env);
GLOBAL_PARAMETER("eval-jit-enabled", use_jit, MZCONFIG_USE_JIT, env);
GLOBAL_PARAMETER("compile-context-preservation-enabled", disallow_inline, MZCONFIG_DISALLOW_INLINE, env);
if (getenv("PLT_VALIDATE_COMPILE")) {
/* Enables validation of bytecode as it is generated,
to double-check that the compiler is producing
valid bytecode as it should. */
valdiate_compile_result = 1;
}
}
void scheme_init_eval_places()
{
#ifdef MZ_PRECISE_GC
scheme_prefix_finalize = (Scheme_Prefix *)0x1; /* 0x1 acts as a sentenel */
GC_set_post_propagate_hook(mark_pruned_prefixes);
#endif
#ifdef DEBUG_CHECK_STACK_FRAME_SIZE
(void)scheme_do_eval(SCHEME_TAIL_CALL_WAITING, 0, NULL, 0);
#endif
}
XFORM_NONGCING static void ignore_result(Scheme_Object *v)
{
if (SAME_OBJ(v, SCHEME_MULTIPLE_VALUES)) {
scheme_current_thread->ku.multiple.array = NULL;
}
}
void scheme_ignore_result(Scheme_Object *v)
{
ignore_result(v);
}
/*========================================================================*/
/* C stack and Scheme stack handling */
/*========================================================================*/
Scheme_Object *
scheme_handle_stack_overflow(Scheme_Object *(*k)(void))
{
/* "Stack overflow" means running out of C-stack space. The other
end of this handler (i.e., the target for the longjmp) is
scheme_top_level_do in fun.c */
Scheme_Thread *p = scheme_current_thread;
Scheme_Overflow *overflow;
Scheme_Overflow_Jmp *jmp;
scheme_about_to_move_C_stack();
p->overflow_k = k;
scheme_overflow_count++;
overflow = MALLOC_ONE_RT(Scheme_Overflow);
#ifdef MZTAG_REQUIRED
overflow->type = scheme_rt_overflow;
#endif
/* push old overflow */
overflow->prev = scheme_current_thread->overflow;
p->overflow = overflow;
overflow->stack_start = p->stack_start;
jmp = MALLOC_ONE_RT(Scheme_Overflow_Jmp);
#ifdef MZTAG_REQUIRED
jmp->type = scheme_rt_overflow_jmp;
#endif
overflow->jmp = jmp;
scheme_init_jmpup_buf(&overflow->jmp->cont);
scheme_zero_unneeded_rands(scheme_current_thread); /* for GC */
if (scheme_setjmpup(&overflow->jmp->cont, overflow->jmp, p->stack_start)) {
p = scheme_current_thread;
overflow = p->overflow;
p->overflow = overflow->prev;
p->error_buf = overflow->jmp->savebuf;
if (p->meta_prompt) {
/* When unwinding a stack overflow, we need to fix up
the meta prompt to have the restored stack base.
(When overflow happens with a meta prompt in place,
no fixup is needed, because the overflow is detected
at the point where the meta-prompt's base would be used.) */
Scheme_Prompt *meta_prompt;
meta_prompt = MALLOC_ONE_TAGGED(Scheme_Prompt);
memcpy(meta_prompt, p->meta_prompt, sizeof(Scheme_Prompt));
meta_prompt->stack_boundary = p->stack_start;
p->meta_prompt = meta_prompt;
}
if (!overflow->jmp->captured) /* reset if not captured in a continuation */
scheme_reset_jmpup_buf(&overflow->jmp->cont);
if (!scheme_overflow_reply) {
/* No reply value means we should continue some escape. */
if (p->cjs.jumping_to_continuation
&& p->cjs.is_escape) {
/* Jump directly to prompt: */
Scheme_Prompt *prompt = (Scheme_Prompt *)p->cjs.jumping_to_continuation;
scheme_longjmp(*prompt->prompt_buf, 1);
} else if (p->cjs.jumping_to_continuation
&& SCHEME_CONTP(p->cjs.jumping_to_continuation)) {
Scheme_Cont *c = (Scheme_Cont *)p->cjs.jumping_to_continuation;
p->cjs.jumping_to_continuation = NULL;
scheme_longjmpup(&c->buf_ptr->buf);
} else {
/* Continue normal escape: */
scheme_longjmp(scheme_error_buf, 1);
}
} else {
Scheme_Object *reply = scheme_overflow_reply;
scheme_overflow_reply = NULL;
return reply;
}
} else {
p->stack_start = scheme_overflow_stack_start;
scheme_longjmpup(&scheme_overflow_jmp->cont);
}
return NULL; /* never gets here */
}
#ifdef LINUX_FIND_STACK_BASE
static uintptr_t adjust_stack_base(uintptr_t bnd) {
if (bnd == scheme_get_primordial_thread_stack_base()) {
/* The address `base' might be far from the actual stack base
if Exec Shield is enabled (in some versions)? Use
"/proc/self/maps" to get exactly the stack base. */
FILE *f;
char *buf;
f = fopen("/proc/self/maps", "r");
if (f) {
buf = malloc(256);
while (fgets(buf, 256, f)) {
int len;
len = strlen(buf);
if ((len > 8) && !strcmp("[stack]\n", buf + len - 8)) {
uintptr_t p = 0;
int i;
/* find separator: */
for (i = 0; buf[i]; i++) {
if (buf[i] == '-') {
i++;
break;
}
}
/* parse number after separator: */
for (; buf[i]; i++) {
if ((buf[i] >= '0') && (buf[i] <= '9')) {
p = (p << 4) | (buf[i] - '0');
} else if ((buf[i] >= 'a') && (buf[i] <= 'f')) {
p = (p << 4) | (buf[i] - 'a' + 10);
} else if ((buf[i] >= 'A') && (buf[i] <= 'F')) {
p = (p << 4) | (buf[i] - 'A' + 10);
} else
break;
}
/* printf("%p vs. %p: %d\n", (void*)bnd, (void*)p, p - bnd); */
return p;
}
}
free(buf);
fclose(f);
}
}
return bnd;
}
#endif
#ifdef WINDOWS_FIND_STACK_BOUNDS
intptr_t find_exe_stack_size()
{
intptr_t sz = WINDOWS_DEFAULT_STACK_SIZE;
wchar_t *fn;
DWORD len = 1024;
/* Try to read the executable to find out the initial
stack size. */
fn = (wchar_t *)malloc(sizeof(wchar_t) * len);
if (GetModuleFileNameW(NULL, fn, len) < len) {
HANDLE fd;
fd = CreateFileW(fn,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (fd != INVALID_HANDLE_VALUE) {
int pos;
short kind;
DWORD got;
/* Skip DOS stub */
if (SetFilePointer(fd, 0x3C, NULL, FILE_BEGIN)
!= INVALID_SET_FILE_POINTER) {
if (ReadFile(fd, &pos, sizeof(int), &got, NULL)
&& (got == sizeof(int))) {
/* Read offset to header */
if (SetFilePointer(fd, pos + 20 + 4, NULL, FILE_BEGIN)
!= INVALID_SET_FILE_POINTER) {
/* Check magic number */
if (ReadFile(fd, &kind, sizeof(short), &got, NULL)
&& (got == sizeof(short))) {
/* Two possible magic numbers: PE32 or PE32+: */
if ((kind == 0x10b) || (kind == 0x20b)) {
/* Skip to PE32[+] header's stack reservation value: */
if (SetFilePointer(fd, pos + 20 + 4 + 72, NULL, FILE_BEGIN)
!= INVALID_SET_FILE_POINTER) {
mzlonglong lsz;
if (kind == 0x10b) {
/* PE32: 32-bit stack size: */
int ssz;
if (ReadFile(fd, &ssz, sizeof(int), &got, NULL)
&& (got == sizeof(int))) {
sz = ssz;
}
} else {
/* PE32+: 64-bit stack size: */
mzlonglong lsz;
if (ReadFile(fd, &lsz, sizeof(mzlonglong), &got, NULL)
&& (got == sizeof(mzlonglong))) {
sz = lsz;
}
}
}
}
}
}
}
}
CloseHandle(fd);
}
}
free(fn);
return sz;
}
#endif
void scheme_init_stack_check()
/* Finds the C stack limit --- platform-specific. */
{
int *v, stack_grows_up;
uintptr_t deeper;
deeper = scheme_get_deeper_address();
stack_grows_up = (deeper > (uintptr_t)&v);
#ifdef STACK_GROWS_UP
if (!stack_grows_up) {
if (scheme_console_printf)
scheme_console_printf("Stack grows DOWN, not UP.\n");
else
printf("Stack grows DOWN, not UP.\n");
exit(1);
}
#endif
#ifdef STACK_GROWS_DOWN
if (stack_grows_up) {
if (scheme_console_printf)
scheme_console_printf("Stack grows UP, not DOWN.\n");
else
printf("Stack grows UP, not DOWN.\n");
exit(1);
}
#endif
#ifdef USE_STACK_BOUNDARY_VAR
if (!scheme_stack_boundary) {
# ifdef ASSUME_FIXED_STACK_SIZE
scheme_stack_boundary = scheme_get_current_os_thread_stack_base();
if (stack_grows_up)
scheme_stack_boundary += (FIXED_STACK_SIZE - STACK_SAFETY_MARGIN);
else
scheme_stack_boundary += (STACK_SAFETY_MARGIN - FIXED_STACK_SIZE);
# endif
# ifdef WINDOWS_FIND_STACK_BOUNDS
scheme_stack_boundary = scheme_get_current_os_thread_stack_base();
{
intptr_t sz;
sz = find_exe_stack_size();
scheme_stack_boundary += (STACK_SAFETY_MARGIN - sz);
}
# endif
# ifdef MACOS_FIND_STACK_BOUNDS
scheme_stack_boundary = (uintptr_t)&v + STACK_SAFETY_MARGIN - StackSpace();
# endif
# ifdef PALMOS_FIND_STACK_BOUNDS
{
Ptr s, e;
SysGetStackInfo(Ptr &s, &e);
scheme_stack_boundary = (uintptr_t)e + STACK_SAFETY_MARGIN;
}
# endif
# ifdef BEOS_FIND_STACK_BOUNDS
{
thread_info info;
get_thread_info(find_thread(NULL), &info);
scheme_stack_boundary = (uintptr_t)info.stack_base + STACK_SAFETY_MARGIN;
}
# endif
# ifdef OSKIT_FIXED_STACK_BOUNDS
scheme_stack_boundary = (uintptr_t)base_stack_start + STACK_SAFETY_MARGIN;
# endif
# ifdef UNIX_FIND_STACK_BOUNDS
{
struct rlimit rl;
uintptr_t bnd, lim;
bnd = (uintptr_t)scheme_get_current_os_thread_stack_base();
getrlimit(RLIMIT_STACK, &rl);
# ifdef LINUX_FIND_STACK_BASE
bnd = adjust_stack_base(bnd);
# endif
lim = (uintptr_t)rl.rlim_cur;
# ifdef UNIX_STACK_MAXIMUM
if (lim > UNIX_STACK_MAXIMUM)
lim = UNIX_STACK_MAXIMUM;
# endif
if (stack_grows_up)
bnd += (lim - STACK_SAFETY_MARGIN);
else
bnd += (STACK_SAFETY_MARGIN - lim);
scheme_stack_boundary = bnd;
}
# endif
# ifdef PTHREAD_STACKSEG_FIND_STACK_BOUNDS
{
stack_t stack;
pthread_stackseg_np(pthread_self(), &stack);
scheme_stack_boundary = (uintptr_t)((char *)stack.ss_sp - (stack.ss_size - STACK_SAFETY_MARGIN));
}
# endif
}
#endif
#ifdef USE_STACK_BOUNDARY_VAR
scheme_jit_stack_boundary = scheme_stack_boundary;
#endif
}
int scheme_check_runstack(intptr_t size)
/* Checks whether the Scheme stack has `size' room left */
{
return ((MZ_RUNSTACK - MZ_RUNSTACK_START) >= (size + SCHEME_TAIL_COPY_THRESHOLD));
}
void *scheme_enlarge_runstack(intptr_t size, void *(*k)())
/* Adds a Scheme stack segment, of at least `size' bytes */
{
Scheme_Thread *p = scheme_current_thread;
Scheme_Saved_Stack *saved;
void *v;
int cont_count;
volatile int escape;
mz_jmp_buf newbuf, * volatile savebuf;
saved = MALLOC_ONE_RT(Scheme_Saved_Stack);
#ifdef MZTAG_REQUIRED
saved->type = scheme_rt_saved_stack;
#endif
saved->prev = p->runstack_saved;
saved->runstack_start = MZ_RUNSTACK_START;
saved->runstack_offset = (MZ_RUNSTACK - MZ_RUNSTACK_START);
saved->runstack_size = p->runstack_size;
size += SCHEME_TAIL_COPY_THRESHOLD;
if (size) {
/* If we keep growing the stack, then probably it
needs to be much larger, so at least double the
stack size, to a point: */
intptr_t min_size;
min_size = 2 * (p->runstack_size);
if (min_size > 128000)
min_size = 128000;
if (size < min_size)
size = min_size;
} else {
/* This is for a prompt. Re-use the current size,
up to a point: */
size = p->runstack_size;
if (size > 1000)
size = 1000;
}
if (p->spare_runstack && (size <= p->spare_runstack_size)) {
size = p->spare_runstack_size;
MZ_RUNSTACK_START = p->spare_runstack;
p->spare_runstack = NULL;
} else {
MZ_RUNSTACK_START = scheme_alloc_runstack(size);
}
p->runstack_size = size;
MZ_RUNSTACK = MZ_RUNSTACK_START + size;
p->runstack_saved = saved;
cont_count = scheme_cont_capture_count;
savebuf = p->error_buf;
p->error_buf = &newbuf;
if (scheme_setjmp(newbuf)) {
v = NULL;
escape = 1;
p = scheme_current_thread; /* might have changed! */
} else {
v = k();
escape = 0;
p = scheme_current_thread; /* might have changed! */
if (cont_count == scheme_cont_capture_count) {
if (!p->spare_runstack || (p->runstack_size > p->spare_runstack_size)) {
p->spare_runstack = MZ_RUNSTACK_START;
p->spare_runstack_size = p->runstack_size;
}
}
}
p->error_buf = savebuf;
saved = p->runstack_saved;
p->runstack_saved = saved->prev;
MZ_RUNSTACK_START = saved->runstack_start;
MZ_RUNSTACK = MZ_RUNSTACK_START + saved->runstack_offset;
p->runstack_size = saved->runstack_size;
if (escape) {
scheme_longjmp(*p->error_buf, 1);
}
return v;
}
/*========================================================================*/
/* linking variables */
/*========================================================================*/
static Scheme_Object *link_module_variable(Scheme_Object *modidx,
Scheme_Object *varname,
int check_access, Scheme_Object *insp,
int pos, int mod_phase,
Scheme_Env *env,
Scheme_Object **exprs, int which,
int flags, Scheme_Object *shape)
{
Scheme_Object *modname;
Scheme_Env *menv;
Scheme_Bucket *bkt;
int self = 0;
/* If it's a name id, resolve the name. */
modname = scheme_module_resolve(modidx, 1);
if (env->module && SAME_OBJ(env->module->modname, modname)
&& (env->mod_phase == mod_phase)) {
self = 1;
menv = env;
} else {
menv = scheme_module_access(modname, env, mod_phase);
if (!menv) {
scheme_wrong_syntax("link", NULL, varname,
"namespace mismatch;\n"
" reference to a module that is not available\n"
" reference phase: %d\n"
" referenced module: %D\n"
" referenced phase level: %d\n"
" reference in module: %D",
env->phase,
modname,
mod_phase,
env->module ? env->module->modsrc : scheme_false);
return NULL;
}
if (check_access && !SAME_OBJ(menv, env)) {
varname = scheme_check_accessible_in_module(menv, insp, NULL, varname, NULL, NULL,
insp, NULL, pos, 0, NULL, NULL, env, NULL,
NULL);
}
}
if (exprs) {
if (self) {
exprs[which] = varname;
} else {
if (flags & SCHEME_MODVAR_CONST) {
Scheme_Object *v;
v = scheme_make_vector((mod_phase != 0) ? 4 : 3, modname);
SCHEME_VEC_ELS(v)[1] = varname;
SCHEME_VEC_ELS(v)[2] = (shape ? shape : scheme_false);
if (mod_phase != 0)
SCHEME_VEC_ELS(v)[3] = scheme_make_integer(mod_phase);
} else {
Scheme_Object *v = modname;
if (mod_phase != 0)
v = scheme_make_pair(v, scheme_make_integer(mod_phase));
v = scheme_make_pair(varname, v);
exprs[which] = v;
}
}
}
bkt = scheme_global_bucket(varname, menv);
if (!self) {
const char *bad_reason = NULL;
if (!bkt->val) {
bad_reason = "is uninitialized";
} else if (flags) {
if (flags & SCHEME_MODVAR_CONST) {
if (!(((Scheme_Bucket_With_Flags *)bkt)->flags & GLOB_IS_CONSISTENT))
bad_reason = "is not a procedure or structure-type constant across all instantiations";
else if (shape && SCHEME_TRUEP(shape)) {
if (!scheme_get_or_check_procedure_shape(bkt->val, shape))
bad_reason = "has the wrong procedure or structure-type shape";
}
} else {
if (!(((Scheme_Bucket_With_Flags *)bkt)->flags & GLOB_IS_IMMUTATED))
bad_reason = "not constant";
}
}
if (bad_reason) {
scheme_wrong_syntax("link", NULL, varname,
"bad variable linkage;\n"
" reference to a variable that %s\n"
" reference phase level: %d\n"
" variable module: %D\n"
" variable phase: %d\n"
" reference in module: %D",
bad_reason,
env->phase,
modname,
mod_phase,
env->module ? env->module->modsrc : scheme_false);
}
if (!(((Scheme_Bucket_With_Flags *)bkt)->flags & (GLOB_IS_IMMUTATED | GLOB_IS_LINKED)))
((Scheme_Bucket_With_Flags *)bkt)->flags |= GLOB_IS_LINKED;
}
return (Scheme_Object *)bkt;
}
static Scheme_Object *link_toplevel(Scheme_Object **exprs, int which, Scheme_Env *env,
Scheme_Object *src_modidx,
Scheme_Object *dest_modidx,
Scheme_Object *insp)
{
Scheme_Object *expr = exprs[which];
if (SCHEME_FALSEP(expr)) {
/* See scheme_make_environment_dummy */
Scheme_Bucket *b;
b = scheme_global_bucket(scheme_stack_dump_key, env);
if (!(((Scheme_Bucket_With_Flags *)b)->flags & GLOB_STRONG_HOME_LINK)) {
((Scheme_Bucket_With_Flags *)b)->flags |= GLOB_STRONG_HOME_LINK;
((Scheme_Bucket_With_Home *)b)->home_link = (Scheme_Object *)env;
}
return (Scheme_Object *)b;
} else if (SCHEME_PAIRP(expr) || SCHEME_SYMBOLP(expr) || SCHEME_VECTORP(expr)) {
/* Simplified module reference (as installed by link_module_variable) */
Scheme_Object *modname, *varname, *shape;
int mod_phase = 0, flags = 0;
if (SCHEME_SYMBOLP(expr)) {
if (!env->module) {
/* compiled as a module variable, but instantiated in a non-module
namespace; grab a bucket */
return (Scheme_Object *)scheme_global_bucket(expr, env);
} else {
varname = expr;
modname = env->module->modname;
mod_phase = env->mod_phase;
}
shape = NULL;
} else if (SCHEME_PAIRP(expr)) {
varname = SCHEME_CAR(expr);
modname = SCHEME_CDR(expr);
if (SCHEME_PAIRP(modname)) {
mod_phase = SCHEME_INT_VAL(SCHEME_CDR(modname));
modname = SCHEME_CAR(modname);
}
shape = NULL;
} else {
modname = SCHEME_VEC_ELS(expr)[0];
varname = SCHEME_VEC_ELS(expr)[1];
flags = SCHEME_MODVAR_CONST;
shape = SCHEME_VEC_ELS(expr)[2];
if (SCHEME_VEC_SIZE(expr) > 3)
mod_phase = SCHEME_INT_VAL(SCHEME_VEC_ELS(expr)[3]);
}
return link_module_variable(modname,
varname,
0, NULL,
-1, mod_phase,
env,
NULL, 0,
flags, shape);
} else if (SAME_TYPE(SCHEME_TYPE(expr), scheme_variable_type)) {
Scheme_Bucket *b = (Scheme_Bucket *)expr;
Scheme_Env *home;
home = scheme_get_bucket_home(b);
if (!env || !home || !home->module)
return (Scheme_Object *)b;
else
return link_module_variable(home->module->modname,
(Scheme_Object *)b->key,
1, home->access_insp,
-1, home->mod_phase,
env,
exprs, which,
0, NULL);
} else {
Module_Variable *mv = (Module_Variable *)expr;
if ((!insp || SCHEME_FALSEP(insp)) && !mv->insp)
insp = scheme_get_param(scheme_current_config(), MZCONFIG_CODE_INSPECTOR);