forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize.c
More file actions
7006 lines (6039 loc) · 217 KB
/
optimize.c
File metadata and controls
7006 lines (6039 loc) · 217 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 implements bytecode optimzation.
See "eval.c" for an overview of compilation passes. */
#include "schpriv.h"
#include "schrunst.h"
#include "schmach.h"
#define cons(a,b) scheme_make_pair(a,b)
/* Controls for inlining algorithm: */
#define OPT_ESTIMATE_FUTURE_SIZES 1
#define OPT_DISCOURAGE_EARLY_INLINE 1
#define OPT_LIMIT_FUNCTION_RESIZE 0
#define OPT_BRANCH_ADDS_NO_SIZE 1
#define OPT_DELAY_GROUP_PROPAGATE 0
#define MAX_PROC_INLINE_SIZE 256
#define CROSS_MODULE_INLINE_SIZE 8
#define SCHEME_PRIM_IS_UNSAFE_NONMUTATING (SCHEME_PRIM_IS_UNSAFE_FUNCTIONAL | SCHEME_PRIM_IS_UNSAFE_OMITABLE)
struct Optimize_Info
{
MZTAG_IF_REQUIRED
short flags;
struct Optimize_Info *next;
int original_frame, new_frame;
Scheme_Object *consts;
Comp_Prefix *cp;
/* Propagated up and down the chain: */
int size, vclock, psize;
short inline_fuel;
char letrec_not_twice, enforce_const, use_psize, has_nonleaf;
Scheme_Hash_Table *top_level_consts;
/* Set by expression optimization: */
int single_result, preserves_marks; /* negative means "tentative", due to fixpoint in progress */
char **stat_dists; /* (pos, depth) => used? */
int *sd_depths;
int used_toplevel;
char *use;
int transitive_use_pos; /* set to pos + 1 when optimizing a letrec-bound procedure */
mzshort **transitive_use;
int *transitive_use_len;
Scheme_Object *context; /* for logging */
Scheme_Logger *logger;
Scheme_Hash_Tree *types; /* maps position (from this frame) to predicate */
};
#define OPT_IS_MUTATED 0x1
#define OPT_LOCAL_TYPE_ARG_SHIFT 2
#define OPT_LOCAL_TYPE_VAL_SHIFT (OPT_LOCAL_TYPE_ARG_SHIFT + SCHEME_MAX_LOCAL_TYPE_BITS)
static char *get_closure_local_type_map(Scheme_Closure_Data *data, int arg_n, int *ok);
static void set_closure_local_type_map(Scheme_Closure_Data *data, char *local_type_map);
static void merge_closure_local_type_map(Scheme_Closure_Data *data1, Scheme_Closure_Data *data2);
static int closure_body_size(Scheme_Closure_Data *data, int check_assign,
Optimize_Info *info, int *is_leaf);
static int closure_has_top_level(Scheme_Closure_Data *data);
static int closure_argument_flags(Scheme_Closure_Data *data, int i);
static int wants_local_type_arguments(Scheme_Object *rator, int argpos);
static int optimize_info_is_ready(Optimize_Info *info, int pos);
static void optimize_propagate(Optimize_Info *info, int pos, Scheme_Object *value, int single_use);
static Scheme_Object *optimize_info_lookup(Optimize_Info *info, int pos, int *closure_offset, int *single_use,
int once_used_ok, int context, int *potential_size, int *_mutated);
static Scheme_Object *optimize_info_mutated_lookup(Optimize_Info *info, int pos, int *is_mutated);
static void optimize_info_used_top(Optimize_Info *info);
static Scheme_Object *optimize_get_predicate(int pos, Optimize_Info *info);
static void add_type(Optimize_Info *info, int pos, Scheme_Object *pred);
static void optimize_mutated(Optimize_Info *info, int pos);
static void optimize_produces_local_type(Optimize_Info *info, int pos, int ct);
static Scheme_Object *optimize_reverse(Optimize_Info *info, int pos, int unless_mutated, int disrupt_single_use);
static int optimize_is_used(Optimize_Info *info, int pos);
static int optimize_any_uses(Optimize_Info *info, int start_pos, int end_pos);
static int optimize_is_mutated(Optimize_Info *info, int pos);
static int optimize_is_local_type_arg(Optimize_Info *info, int pos, int depth);
static int optimize_is_local_type_valued(Optimize_Info *info, int pos);
static int env_uses_toplevel(Optimize_Info *frame);
static void env_make_closure_map(Optimize_Info *frame, mzshort *size, mzshort **map);
static Optimize_Info *optimize_info_add_frame(Optimize_Info *info, int orig, int current, int flags);
static int optimize_info_get_shift(Optimize_Info *info, int pos);
static void optimize_info_done(Optimize_Info *info, Optimize_Info *parent);
static Scheme_Object *estimate_closure_size(Scheme_Object *e);
static Scheme_Object *no_potential_size(Scheme_Object *value);
static Scheme_Object *optimize_clone(int dup_ok, Scheme_Object *obj, Optimize_Info *info, int delta, int closure_depth);
static Scheme_Object *optimize_shift(Scheme_Object *obj, int delta, int after_depth);
#define IS_COMPILED_PROC(vals_expr) (SAME_TYPE(SCHEME_TYPE(vals_expr), scheme_compiled_unclosed_procedure_type) \
|| SAME_TYPE(SCHEME_TYPE(vals_expr), scheme_case_lambda_sequence_type))
static int compiled_proc_body_size(Scheme_Object *o, int less_args);
typedef struct Scheme_Once_Used {
Scheme_Object so;
Scheme_Object *expr;
int pos;
int vclock;
int used;
int delta;
int cross_lambda;
Optimize_Info *info;
struct Scheme_Once_Used *next;
} Scheme_Once_Used;
static Scheme_Once_Used *make_once_used(Scheme_Object *val, int pos, int vclock, Scheme_Once_Used *prev);
#ifdef MZ_PRECISE_GC
static void register_traversers(void);
#endif
void scheme_init_optimize()
{
#ifdef MZ_PRECISE_GC
register_traversers();
#endif
}
/*========================================================================*/
/* utils */
/*========================================================================*/
int scheme_is_functional_primitive(Scheme_Object *rator, int num_args, int expected_vals)
/* return 2 => results are a constant when arguments are constants */
{
if (SCHEME_PRIMP(rator)
&& (SCHEME_PRIM_PROC_OPT_FLAGS(rator) & (SCHEME_PRIM_IS_OMITABLE | SCHEME_PRIM_IS_UNSAFE_NONMUTATING))
&& (num_args >= ((Scheme_Primitive_Proc *)rator)->mina)
&& (num_args <= ((Scheme_Primitive_Proc *)rator)->mu.maxa)
&& ((expected_vals < 0)
|| ((expected_vals == 1) && !(SCHEME_PRIM_PROC_FLAGS(rator) & SCHEME_PRIM_IS_MULTI_RESULT))
|| (SAME_OBJ(scheme_values_func, rator)
&& (expected_vals == num_args)))) {
if (SAME_OBJ(scheme_values_func, rator))
return 2;
return 1;
} else
return 0;
}
static Scheme_Object *get_struct_proc_shape(Scheme_Object *rator, Optimize_Info *info)
{
Scheme_Object *c;
if (info
&& (info->top_level_consts || info->cp->inline_variants)
&& SAME_TYPE(SCHEME_TYPE(rator), scheme_compiled_toplevel_type)) {
int pos;
pos = SCHEME_TOPLEVEL_POS(rator);
c = NULL;
if (info->top_level_consts)
c = scheme_hash_get(info->top_level_consts, scheme_make_integer(pos));
if (!c && info->cp->inline_variants)
c = scheme_hash_get(info->cp->inline_variants, scheme_make_integer(pos));
if (c && SAME_TYPE(SCHEME_TYPE(c), scheme_struct_proc_shape_type)) {
return c;
}
}
return NULL;
}
int scheme_is_struct_functional(Scheme_Object *rator, int num_args, Optimize_Info *info, int vals)
{
Scheme_Object *c;
if ((vals == 1) || (vals == -1)) {
c = get_struct_proc_shape(rator, info);
if (c) {
int mode = (SCHEME_PROC_SHAPE_MODE(c) & STRUCT_PROC_SHAPE_MASK);
int field_count = (SCHEME_PROC_SHAPE_MODE(c) >> STRUCT_PROC_SHAPE_SHIFT);
if (((num_args == 1) && (mode == STRUCT_PROC_SHAPE_PRED))
|| ((num_args == field_count) && (mode == STRUCT_PROC_SHAPE_CONSTR))) {
return 1;
}
}
}
return 0;
}
static void note_match(int actual, int expected, Optimize_Info *warn_info)
{
if (!warn_info || (expected == -1))
return;
if (actual != expected) {
scheme_log(warn_info->logger,
SCHEME_LOG_WARNING,
0,
"warning%s: %d values produced when %d expected",
scheme_optimize_context_to_string(warn_info->context),
actual, expected);
}
}
int scheme_omittable_expr(Scheme_Object *o, int vals, int fuel, int resolved,
Optimize_Info *opt_info, Optimize_Info *warn_info, int deeper_than, int no_id)
/* Checks whether the bytecode `o' returns `vals' values with no
side-effects and without pushing and using continuation marks.
-1 for vals means that any return count is ok.
Also used with fully resolved expression by `module' to check
for "functional" bodies.
If warn_info is supplied, complain when a mismatch is detected.
If no_id is 1, then an identifier doesn't count as omittable,
unless the identifier is a consistent top-level; currently, this
is used to imply the absece of a continuation-mark impersonator. */
{
Scheme_Type vtype;
/* FIXME: can overflow the stack */
try_again:
vtype = SCHEME_TYPE(o);
if ((vtype > _scheme_compiled_values_types_)
|| ((vtype == scheme_local_type)
&& !no_id
&& !(SCHEME_GET_LOCAL_FLAGS(o) == SCHEME_LOCAL_CLEAR_ON_READ)
&& (SCHEME_LOCAL_POS(o) > deeper_than))
|| ((vtype == scheme_local_unbox_type)
&& !no_id
&& !(SCHEME_GET_LOCAL_FLAGS(o) == SCHEME_LOCAL_CLEAR_ON_READ)
&& (SCHEME_LOCAL_POS(o) > deeper_than))
|| (vtype == scheme_unclosed_procedure_type)
|| (vtype == scheme_compiled_unclosed_procedure_type)
|| (vtype == scheme_inline_variant_type)
|| (vtype == scheme_case_lambda_sequence_type)
|| (vtype == scheme_quote_syntax_type)
|| (vtype == scheme_varref_form_type)
|| (vtype == scheme_compiled_quote_syntax_type)) {
note_match(1, vals, warn_info);
return ((vals == 1) || (vals < 0));
}
if (vtype == scheme_toplevel_type) {
note_match(1, vals, warn_info);
if (!no_id && resolved && ((vals == 1) || (vals < 0))) {
if (SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK)
return 1;
else
return 0;
}
}
if (vtype == scheme_compiled_toplevel_type) {
note_match(1, vals, warn_info);
if ((vals == 1) || (vals < 0)) {
if (!no_id && (SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_READY)
return 1;
else if ((SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_CONST)
return 1;
else
return 0;
}
}
if (vtype == scheme_case_lambda_sequence_type) {
note_match(1, vals, warn_info);
return 1;
}
if (vtype == scheme_compiled_quote_syntax_type) {
note_match(1, vals, warn_info);
return ((vals == 1) || (vals < 0));
}
if (vtype == scheme_branch_type) {
Scheme_Branch_Rec *b;
b = (Scheme_Branch_Rec *)o;
return (scheme_omittable_expr(b->test, 1, fuel - 1, resolved, opt_info, warn_info, deeper_than, 0)
&& scheme_omittable_expr(b->tbranch, vals, fuel - 1, resolved, opt_info, warn_info, deeper_than, no_id)
&& scheme_omittable_expr(b->fbranch, vals, fuel - 1, resolved, opt_info, warn_info, deeper_than, no_id));
}
#if 0
/* We can't do this because a set! to a lexical is turned into
a let_value_type! */
if (vtype == scheme_let_value_type) {
Scheme_Let_Value *lv = (Scheme_Let_Value *)o;
return (scheme_omittable_expr(lv->value, lv->count, fuel - 1, resolved, opt_info, warn_info, deeper_than, no_id)
&& scheme_omittable_expr(lv->body, vals, fuel - 1, resolved, opt_info, warn_info, deeper_than, no_id));
}
#endif
if (vtype == scheme_let_one_type) {
Scheme_Let_One *lo = (Scheme_Let_One *)o;
return (scheme_omittable_expr(lo->value, 1, fuel - 1, resolved, opt_info, warn_info, deeper_than + 1, 0)
&& scheme_omittable_expr(lo->body, vals, fuel - 1, resolved, opt_info, warn_info, deeper_than + 1, no_id));
}
if (vtype == scheme_let_void_type) {
Scheme_Let_Void *lv = (Scheme_Let_Void *)o;
/* recognize (letrec ([x <omittable>]) ...): */
if (SAME_TYPE(SCHEME_TYPE(lv->body), scheme_let_value_type)) {
Scheme_Let_Value *lv2 = (Scheme_Let_Value *)lv->body;
if ((lv2->count == 1)
&& (lv2->position == 0)
&& scheme_omittable_expr(lv2->value, 1, fuel - 1, resolved, opt_info, warn_info,
deeper_than + 1 + lv->count,
0)) {
o = lv2->body;
deeper_than += 1;
} else
o = lv->body;
} else
o = lv->body;
deeper_than += lv->count;
goto try_again;
}
if (vtype == scheme_compiled_let_void_type) {
/* recognize another (let ([x <omittable>]) ...) pattern: */
Scheme_Let_Header *lh = (Scheme_Let_Header *)o;
if ((lh->count == 1) && (lh->num_clauses == 1)) {
if (SAME_TYPE(SCHEME_TYPE(lh->body), scheme_compiled_let_value_type)) {
Scheme_Compiled_Let_Value *lv = (Scheme_Compiled_Let_Value *)lh->body;
if (scheme_omittable_expr(lv->value, 1, fuel - 1, resolved, opt_info, warn_info, deeper_than + 1, 0)) {
o = lv->body;
deeper_than++;
goto try_again;
}
}
}
}
if (vtype == scheme_letrec_type) {
o = ((Scheme_Letrec *)o)->body;
goto try_again;
}
if (vtype == scheme_application_type) {
Scheme_App_Rec *app = (Scheme_App_Rec *)o;
if ((app->num_args >= 4) && (app->num_args <= 11)
&& SAME_OBJ(scheme_make_struct_type_proc, app->args[0])) {
note_match(5, vals, warn_info);
}
if (scheme_is_functional_primitive(app->args[0], app->num_args, vals)
|| scheme_is_struct_functional(app->args[0], app->num_args, opt_info, vals)) {
int i;
for (i = app->num_args; i--; ) {
if (!scheme_omittable_expr(app->args[i + 1], 1, fuel - 1, resolved, opt_info, warn_info,
deeper_than + (resolved ? app->num_args : 0), 0))
return 0;
}
return 1;
} else if (SCHEME_PRIMP(app->args[0])) {
if (!(SCHEME_PRIM_PROC_FLAGS(app->args[0]) & SCHEME_PRIM_IS_MULTI_RESULT)) {
note_match(1, vals, warn_info);
} else if (SAME_OBJ(scheme_values_func, app->args[0])) {
note_match(app->num_args, vals, warn_info);
}
}
return 0;
}
if (vtype == scheme_application2_type) {
Scheme_App2_Rec *app = (Scheme_App2_Rec *)o;
if (scheme_is_functional_primitive(app->rator, 1, vals)
|| scheme_is_struct_functional(app->rator, 1, opt_info, vals)) {
if (scheme_omittable_expr(app->rand, 1, fuel - 1, resolved, opt_info, warn_info,
deeper_than + (resolved ? 1 : 0), 0))
return 1;
} else if (SCHEME_PRIMP(app->rator)) {
if (!(SCHEME_PRIM_PROC_FLAGS(app->rator) & SCHEME_PRIM_IS_MULTI_RESULT)
|| SAME_OBJ(scheme_values_func, app->rator)) {
note_match(1, vals, warn_info);
}
}
return 0;
}
if (vtype == scheme_application3_type) {
Scheme_App3_Rec *app = (Scheme_App3_Rec *)o;
if (scheme_is_functional_primitive(app->rator, 2, vals)
|| scheme_is_struct_functional(app->rator, 2, opt_info, vals)) {
if (scheme_omittable_expr(app->rand1, 1, fuel - 1, resolved, opt_info, warn_info,
deeper_than + (resolved ? 2 : 0), 0)
&& scheme_omittable_expr(app->rand2, 1, fuel - 1, resolved, opt_info, warn_info,
deeper_than + (resolved ? 2 : 0), 0))
return 1;
} else if (SCHEME_PRIMP(app->rator)) {
if (!(SCHEME_PRIM_PROC_FLAGS(app->rator) & SCHEME_PRIM_IS_MULTI_RESULT)) {
note_match(1, vals, warn_info);
} else if (SAME_OBJ(scheme_values_func, app->rator)) {
note_match(2, vals, warn_info);
}
}
return 0;
}
/* check for struct-type declaration: */
{
Scheme_Object *auto_e;
int auto_e_depth;
auto_e = scheme_is_simple_make_struct_type(o, vals, resolved, 0, &auto_e_depth,
NULL,
(opt_info ? opt_info->top_level_consts : NULL),
NULL, NULL, 0, NULL, NULL,
5);
if (auto_e) {
if (scheme_omittable_expr(auto_e, 1, fuel - 1, resolved, opt_info, warn_info,
deeper_than + auto_e_depth, 0))
return 1;
}
}
return 0;
}
static int is_inspector_call(Scheme_Object *a)
{
if (SAME_TYPE(SCHEME_TYPE(a), scheme_application_type)) {
Scheme_App_Rec *app = (Scheme_App_Rec *)a;
if (!app->num_args
&& (SAME_OBJ(app->args[0], scheme_current_inspector_proc)
|| SAME_OBJ(app->args[0], scheme_make_inspector_proc)))
return 1;
}
return 0;
}
static int is_proc_spec_proc(Scheme_Object *p)
{
Scheme_Type vtype;
if (SCHEME_PROCP(p)) {
p = scheme_get_or_check_arity(p, -1);
if (SCHEME_INTP(p)) {
return (SCHEME_INT_VAL(p) >= 1);
} else if (SCHEME_STRUCTP(p)
&& scheme_is_struct_instance(scheme_arity_at_least, p)) {
p = ((Scheme_Structure *)p)->slots[0];
if (SCHEME_INTP(p))
return (SCHEME_INT_VAL(p) >= 1);
}
return 0;
}
vtype = SCHEME_TYPE(p);
if (vtype == scheme_unclosed_procedure_type) {
if (((Scheme_Closure_Data *)p)->num_params >= 1)
return 1;
}
return 0;
}
static int is_local_ref(Scheme_Object *e, int p, int r)
{
return (SAME_TYPE(SCHEME_TYPE(e), scheme_local_type)
&& (SCHEME_LOCAL_POS(e) >= p)
&& (SCHEME_LOCAL_POS(e) < (p + r)));
}
static int is_int_list(Scheme_Object *o, int up_to)
{
if (SCHEME_PAIRP(o)) {
char *s, quick[8];
Scheme_Object *e;
if (up_to <= 8)
s = quick;
else
s = (char *)scheme_malloc_atomic(up_to);
memset(s, 0, up_to);
while (SCHEME_PAIRP(o)) {
e = SCHEME_CAR(o);
o = SCHEME_CDR(o);
if (!SCHEME_INTP(e)
|| (SCHEME_INT_VAL(e) < 0)
|| (SCHEME_INT_VAL(e) > up_to)
|| s[SCHEME_INT_VAL(e)])
return 0;
s[SCHEME_INT_VAL(e)] = 1;
}
}
return SCHEME_NULLP(o);
}
static int ok_proc_creator_args(Scheme_Object *rator, Scheme_Object *rand1, Scheme_Object *rand2, Scheme_Object *rand3,
int delta2, int field_count)
{
if ((SAME_OBJ(rator, scheme_make_struct_field_accessor_proc)
&& is_local_ref(rand1, delta2+3, 1))
|| (SAME_OBJ(rator, scheme_make_struct_field_mutator_proc)
&& is_local_ref(rand1, delta2+4, 1))) {
if (SCHEME_INTP(rand2)
&& (SCHEME_INT_VAL(rand2) >= 0)
&& (SCHEME_INT_VAL(rand2) < field_count)
&& (!rand3 || SCHEME_SYMBOLP(rand3))) {
return 1;
}
}
return 0;
}
static int is_values_with_accessors_and_mutators(Scheme_Object *e, int vals, int resolved,
Simple_Stuct_Type_Info *_stinfo)
{
if (SAME_TYPE(SCHEME_TYPE(e), scheme_application_type)) {
Scheme_App_Rec *app = (Scheme_App_Rec *)e;
int delta = (resolved ? app->num_args : 0);
if (SAME_OBJ(app->args[0], scheme_values_func)
&& (app->num_args == vals)
&& (app->num_args >= 3)
&& is_local_ref(app->args[1], delta, 1)
&& is_local_ref(app->args[2], delta+1, 1)
&& is_local_ref(app->args[3], delta+2, 1)) {
int i, num_gets = 0, num_sets = 0, normal_ops = 1;
for (i = app->num_args; i > 3; i--) {
if (is_local_ref(app->args[i], delta, 5)) {
normal_ops = 0;
} else if (SAME_TYPE(SCHEME_TYPE(app->args[i]), scheme_application_type)
&& _stinfo->normal_ops && !_stinfo->indexed_ops) {
Scheme_App_Rec *app3 = (Scheme_App_Rec *)app->args[i];
int delta2 = delta + (resolved ? app3->num_args : 0);
if (app3->num_args == 3) {
if (!ok_proc_creator_args(app3->args[0], app3->args[1], app3->args[2], app3->args[3],
delta2, _stinfo->field_count))
break;
if (SAME_OBJ(app3->args[0], scheme_make_struct_field_mutator_proc)) {
if (num_gets) normal_ops = 0;
num_sets++;
} else
num_gets++;
} else
break;
} else if (SAME_TYPE(SCHEME_TYPE(app->args[i]), scheme_application3_type)
&& _stinfo->normal_ops && !_stinfo->indexed_ops) {
Scheme_App3_Rec *app3 = (Scheme_App3_Rec *)app->args[i];
int delta2 = delta + (resolved ? 2 : 0);
if (!ok_proc_creator_args(app3->rator, app3->rand1, app3->rand2, NULL,
delta2, _stinfo->field_count))
break;
if (SAME_OBJ(app3->rator, scheme_make_struct_field_mutator_proc)) {
if (num_gets) normal_ops = 0;
num_sets++;
} else
num_gets++;
} else
break;
}
if (i <= 3) {
_stinfo->normal_ops = normal_ops;
_stinfo->indexed_ops = 1;
_stinfo->num_gets = num_gets;
_stinfo->num_sets = num_sets;
return 1;
}
}
}
return 0;
}
static Scheme_Object *skip_clears(Scheme_Object *body)
{
if (SAME_TYPE(SCHEME_TYPE(body), scheme_sequence_type)) {
Scheme_Sequence *seq = (Scheme_Sequence *)body;
int i;
for (i = seq->count - 1; i--; ) {
if (!SAME_TYPE(SCHEME_TYPE(seq->array[i]), scheme_local_type))
break;
}
if (i < 0)
return seq->array[seq->count-1];
}
return body;
}
static int is_constant_super(Scheme_Object *arg,
Scheme_Hash_Table *top_level_consts,
Scheme_Hash_Table *top_level_table,
Scheme_Object **runstack, int rs_delta,
Scheme_Object **symbols, Scheme_Hash_Table *symbol_table)
{
int pos;
Scheme_Object *v;
if (SAME_TYPE(SCHEME_TYPE(arg), scheme_compiled_toplevel_type)) {
pos = SCHEME_TOPLEVEL_POS(arg);
if (top_level_consts) {
/* This is optimize mode */
v = scheme_hash_get(top_level_consts, scheme_make_integer(pos));
if (v && SAME_TYPE(SCHEME_TYPE(v), scheme_struct_proc_shape_type)) {
int mode = (SCHEME_PROC_SHAPE_MODE(v) & STRUCT_PROC_SHAPE_MASK);
int field_count = (SCHEME_PROC_SHAPE_MODE(v) >> STRUCT_PROC_SHAPE_SHIFT);
if (mode == STRUCT_PROC_SHAPE_STRUCT)
return field_count + 1;
}
}
} else if (SAME_TYPE(SCHEME_TYPE(arg), scheme_toplevel_type)) {
pos = SCHEME_TOPLEVEL_POS(arg);
if (runstack) {
/* This is eval mode; conceptually, this code belongs in
define_execute_with_dynamic_state() */
Scheme_Bucket *b;
Scheme_Prefix *toplevels;
toplevels = (Scheme_Prefix *)runstack[SCHEME_TOPLEVEL_DEPTH(arg) - rs_delta];
b = (Scheme_Bucket *)toplevels->a[pos];
if (b->val) {
if (SCHEME_STRUCT_TYPEP(b->val)
&& (((Scheme_Bucket_With_Flags *)b)->flags & GLOB_IS_CONSISTENT)) {
Scheme_Struct_Type *st = (Scheme_Struct_Type *)b->val;
if (st->num_slots == st->num_islots)
return st->num_slots + 1;
}
}
}
if (symbols) {
/* This is module-export mode; conceptually, this code belongs in
setup_accessible_table() */
Scheme_Object *name;
name = symbols[pos];
if (SCHEME_SYMBOLP(name)) {
v = scheme_hash_get(symbol_table, name);
if (v && SCHEME_VECTORP(v) && (SCHEME_VEC_SIZE(v) == 3)) {
v = SCHEME_VEC_ELS(v)[1];
if (v && SCHEME_INTP(v)) {
int mode = (SCHEME_INT_VAL(v) & STRUCT_PROC_SHAPE_MASK);
int field_count = (SCHEME_INT_VAL(v) >> STRUCT_PROC_SHAPE_SHIFT);
if (mode == STRUCT_PROC_SHAPE_STRUCT)
return field_count + 1;
}
}
} else if (SAME_TYPE(SCHEME_TYPE(name), scheme_module_variable_type)) {
intptr_t k;
if (scheme_decode_struct_shape(((Module_Variable *)name)->shape, &k)) {
if ((k & STRUCT_PROC_SHAPE_MASK) == STRUCT_PROC_SHAPE_STRUCT)
return (k >> STRUCT_PROC_SHAPE_SHIFT) + 1;
}
}
}
if (top_level_table) {
/* This is validate mode; conceptually, this code belongs in
define_values_validate() */
v = scheme_hash_get(top_level_table, scheme_make_integer(pos));
if (v) {
int k = SCHEME_INT_VAL(v);
if ((k & STRUCT_PROC_SHAPE_MASK) == STRUCT_PROC_SHAPE_STRUCT)
return (k >> STRUCT_PROC_SHAPE_SHIFT) + 1;
}
}
}
return 0;
}
Scheme_Object *scheme_is_simple_make_struct_type(Scheme_Object *e, int vals, int resolved,
int check_auto,
GC_CAN_IGNORE int *_auto_e_depth,
Simple_Stuct_Type_Info *_stinfo,
Scheme_Hash_Table *top_level_consts,
Scheme_Hash_Table *top_level_table,
Scheme_Object **runstack, int rs_delta,
Scheme_Object **symbols, Scheme_Hash_Table *symbol_table,
int fuel)
/* Checks whether it's a `make-struct-type' call that certainly succeeds
(i.e., no exception) --- pending a check of the auto-value argument if !check_auto.
The result is the auto-value argument or scheme_true if it's simple, NULL if not.
The first result is a struct type, the second a constructor, and the thrd a predicate;
the rest are an unspecified mixture of selectors and mutators. */
{
if (!fuel) return NULL;
if (SAME_TYPE(SCHEME_TYPE(e), scheme_application_type)) {
if ((vals == 5) || (vals < 0)) {
Scheme_App_Rec *app = (Scheme_App_Rec *)e;
if ((app->num_args >= 4) && (app->num_args <= 11)
&& SAME_OBJ(scheme_make_struct_type_proc, app->args[0])) {
int super_count_plus_one;
if (!SCHEME_FALSEP(app->args[2]))
super_count_plus_one = is_constant_super(app->args[2],
top_level_consts, top_level_table, runstack,
rs_delta + app->num_args,
symbols, symbol_table);
else
super_count_plus_one = 0;
if (SCHEME_SYMBOLP(app->args[1])
&& (SCHEME_FALSEP(app->args[2]) /* super */
|| super_count_plus_one)
&& SCHEME_INTP(app->args[3])
&& (SCHEME_INT_VAL(app->args[3]) >= 0)
&& SCHEME_INTP(app->args[4])
&& (SCHEME_INT_VAL(app->args[4]) >= 0)
&& ((app->num_args < 5)
/* auto-field value: */
|| !check_auto
|| scheme_omittable_expr(app->args[5], 1, 3, resolved, NULL, NULL, -1, 0))
&& ((app->num_args < 6)
/* no properties: */
|| SCHEME_NULLP(app->args[6]))
&& ((app->num_args < 7)
/* inspector: */
|| SCHEME_FALSEP(app->args[7])
|| (SCHEME_SYMBOLP(app->args[7])
&& !strcmp("prefab", SCHEME_SYM_VAL(app->args[7]))
&& !SCHEME_SYM_WEIRDP(app->args[7]))
|| is_inspector_call(app->args[7]))
&& ((app->num_args < 8)
/* propcedure property: */
|| SCHEME_FALSEP(app->args[8])
|| is_proc_spec_proc(app->args[8]))
&& ((app->num_args < 9)
/* immutables: */
|| is_int_list(app->args[9],
SCHEME_INT_VAL(app->args[3])))
&& ((app->num_args < 10)
/* guard: */
|| SCHEME_FALSEP(app->args[10]))
&& ((app->num_args < 11)
/* constructor name: */
|| SCHEME_FALSEP(app->args[11])
|| SCHEME_SYMBOLP(app->args[11]))) {
if (_auto_e_depth)
*_auto_e_depth = (resolved ? app->num_args : 0);
if (_stinfo) {
int super_count = (super_count_plus_one
? (super_count_plus_one - 1)
: 0);
_stinfo->init_field_count = SCHEME_INT_VAL(app->args[3]) + super_count;
_stinfo->field_count = (SCHEME_INT_VAL(app->args[3])
+ SCHEME_INT_VAL(app->args[4])
+ super_count);
_stinfo->uses_super = (super_count_plus_one ? 1 : 0);
_stinfo->normal_ops = 1;
_stinfo->indexed_ops = 0;
_stinfo->num_gets = 1;
_stinfo->num_sets = 1;
}
return ((app->num_args < 5) ? scheme_true : app->args[5]);
}
}
}
}
if (SAME_TYPE(SCHEME_TYPE(e), scheme_compiled_let_void_type)) {
/* check for (let-values ([(: mk ? ref- set-!) (make-struct-type ...)]) (values ...))
as generated by the expansion of `struct' */
Scheme_Let_Header *lh = (Scheme_Let_Header *)e;
if ((lh->count == 5) && (lh->num_clauses == 1)) {
if (SAME_TYPE(SCHEME_TYPE(lh->body), scheme_compiled_let_value_type)) {
Scheme_Compiled_Let_Value *lv = (Scheme_Compiled_Let_Value *)lh->body;
if (SAME_TYPE(SCHEME_TYPE(lv->value), scheme_application_type)) {
Scheme_Object *auto_e;
Simple_Stuct_Type_Info stinfo;
int lh_delta = ((SCHEME_LET_FLAGS(lh) & (SCHEME_LET_RECURSIVE | SCHEME_LET_STAR))
? lh->count
: 0);
if (!_stinfo) _stinfo = &stinfo;
auto_e = scheme_is_simple_make_struct_type(lv->value, 5, resolved, check_auto,
_auto_e_depth, _stinfo,
top_level_consts, top_level_table,
runstack, rs_delta + lh_delta,
symbols, symbol_table,
fuel-1);
if (auto_e) {
/* We have (let-values ([... (make-struct-type)]) ....), so make sure body
just uses `make-struct-field-{accessor,mutator}'. */
if (is_values_with_accessors_and_mutators(lv->body, vals, resolved, _stinfo)) {
if (_auto_e_depth && lh_delta)
*_auto_e_depth += lh_delta;
return auto_e;
}
}
}
}
}
}
if (SAME_TYPE(SCHEME_TYPE(e), scheme_let_void_type)) {
/* same thing, but in resolved form */
Scheme_Let_Void *lvd = (Scheme_Let_Void *)e;
if (lvd->count == 5) {
if (SAME_TYPE(SCHEME_TYPE(lvd->body), scheme_let_value_type)) {
Scheme_Let_Value *lv = (Scheme_Let_Value *)lvd->body;
if ((lv->position == 0) && (lv->count == 5)) {
Scheme_Object *e2;
e2 = skip_clears(lv->value);
if (SAME_TYPE(SCHEME_TYPE(e2), scheme_application_type)) {
Scheme_Object *auto_e;
Simple_Stuct_Type_Info stinfo;
if (!_stinfo) _stinfo = &stinfo;
auto_e = scheme_is_simple_make_struct_type(e2, 5, resolved, check_auto,
_auto_e_depth, _stinfo,
top_level_consts, top_level_table,
runstack, rs_delta + lvd->count,
symbols, symbol_table,
fuel-1);
if (auto_e) {
/* We have (let-values ([... (make-struct-type)]) ....), so make sure body
just uses `make-struct-field-{accessor,mutator}'. */
e2 = skip_clears(lv->body);
if (is_values_with_accessors_and_mutators(e2, vals, resolved, _stinfo)) {
if (_auto_e_depth) *_auto_e_depth += lvd->count;
return auto_e;
}
}
}
}
}
}
}
return NULL;
}
intptr_t scheme_get_struct_proc_shape(int k, Simple_Stuct_Type_Info *stinfo)
{
switch (k) {
case 0:
if (stinfo->field_count == stinfo->init_field_count)
return STRUCT_PROC_SHAPE_STRUCT | (stinfo->field_count << STRUCT_PROC_SHAPE_SHIFT);
else
return STRUCT_PROC_SHAPE_OTHER;
break;
case 1:
return STRUCT_PROC_SHAPE_CONSTR | (stinfo->init_field_count << STRUCT_PROC_SHAPE_SHIFT);
break;
case 2:
return STRUCT_PROC_SHAPE_PRED;
break;
default:
if (stinfo && stinfo->normal_ops && stinfo->indexed_ops) {
if (k - 3 < stinfo->num_gets)
return STRUCT_PROC_SHAPE_GETTER | (stinfo->field_count << STRUCT_PROC_SHAPE_SHIFT);
else
return STRUCT_PROC_SHAPE_SETTER | (stinfo->field_count << STRUCT_PROC_SHAPE_SHIFT);
}
}
return STRUCT_PROC_SHAPE_OTHER;
}
Scheme_Object *scheme_make_struct_proc_shape(intptr_t k)
{
Scheme_Object *ps;
ps = scheme_malloc_small_atomic_tagged(sizeof(Scheme_Small_Object));
ps->type = scheme_struct_proc_shape_type;
SCHEME_PROC_SHAPE_MODE(ps) = k;
return ps;
}
static int single_valued_noncm_expression(Scheme_Object *expr, int fuel)
/* Non-omittable but single-valued expresions that are not sensitive
to being in tail position. */
{
Scheme_Object *rator = NULL;
switch (SCHEME_TYPE(expr)) {
case scheme_compiled_toplevel_type:
return 1;
case scheme_application_type:
rator = ((Scheme_App_Rec *)expr)->args[0];
break;
case scheme_application2_type:
rator = ((Scheme_App2_Rec *)expr)->rator;
break;
case scheme_application3_type:
rator = ((Scheme_App2_Rec *)expr)->rator;
break;
case scheme_compiled_let_void_type:
{
Scheme_Let_Header *lh = (Scheme_Let_Header *)expr;
Scheme_Compiled_Let_Value *clv;
if ((lh->count == 1) && (lh->num_clauses == 1) && (fuel > 0)) {
clv = (Scheme_Compiled_Let_Value *)lh->body;
return single_valued_noncm_expression(clv->body, fuel - 1);
}
}
break;
}
if (rator && SCHEME_PRIMP(rator)) {
int opt;
opt = ((Scheme_Prim_Proc_Header *)rator)->flags & SCHEME_PRIM_OPT_MASK;
if (opt >= SCHEME_PRIM_OPT_NONCM)
return 1;
}
return 0;
}
static int is_movable_prim(Scheme_Object *rator, int n, int cross_lambda)
/* A -1 return means that the arguments must be movable without
changing space complexity. */
{
if (rator && SCHEME_PRIMP(rator)) {
if (SCHEME_PRIM_PROC_OPT_FLAGS(rator) & SCHEME_PRIM_IS_UNSAFE_FUNCTIONAL) {
/* Although it's semantically ok to return -1 even when cross_lambda,
doing so risks duplicating a computation if the relevant `lambda'
is later inlined. */
if (cross_lambda) return 0;
return -1;
}
}
if (SAME_OBJ(scheme_void_proc, rator))
return -1;
if (!cross_lambda
/* Note that none of these have space-safety issues, since they
return values that contain all arguments: */
&& (SAME_OBJ(scheme_list_proc, rator)
|| (SAME_OBJ(scheme_cons_proc, rator) && (n == 2))
|| (SAME_OBJ(scheme_mcons_proc, rator) && (n == 2))
|| (SAME_OBJ(scheme_unsafe_cons_list_proc, rator) && (n == 2))
|| SAME_OBJ(scheme_list_star_proc, rator)
|| SAME_OBJ(scheme_vector_proc, rator)
|| SAME_OBJ(scheme_vector_immutable_proc, rator)
|| (SAME_OBJ(scheme_box_proc, rator) && (n == 1))
|| (SAME_OBJ(scheme_box_immutable_proc, rator) && (n == 1))))
return 1;
return 0;
}
static int movable_expression(Scheme_Object *expr, Optimize_Info *info, int delta, int cross_lambda,
int check_space, int fuel)
/* An expression that can't necessarily be constant-folded,
but can be delayed because it has no side-effects (or is unsafe);
also not sensitive to being in tail position */
{
int can_move;
if (fuel < 0) return 0;
switch (SCHEME_TYPE(expr)) {
case scheme_toplevel_type:
return ((SCHEME_TOPLEVEL_FLAGS(expr) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_FIXED);
case scheme_compiled_quote_syntax_type:
return 1;
case scheme_local_type:
{
/* Ok if not mutable */
int pos = SCHEME_LOCAL_POS(expr);
if (pos + delta < 0)
return 1;
else if (!optimize_is_mutated(info, pos + delta)) {
if (check_space) {
if (optimize_is_local_type_valued(info, pos + delta))
return 1;
/* the value of the identifier might be something that would
retain significant memory, so we can't delay evaluation */
return 0;
}
return 1;
}
}
break;