-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrutinizer.scm
3013 lines (2821 loc) · 91 KB
/
scrutinizer.scm
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
;;;; scrutinizer.scm - The CHICKEN Scheme compiler (local flow analysis)
;
; Copyright (c) 2009-2021, The CHICKEN Team
; 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.
(declare
(unit scrutinizer)
(uses data-structures expand extras pathname port support internal))
(module chicken.compiler.scrutinizer
(scrutinize load-type-database emit-types-file
validate-type check-and-validate-type install-specializations
;; Exported for use in the tests:
match-types refine-types type<=?)
(import scheme
chicken.base
chicken.compiler.support
chicken.fixnum
chicken.format
chicken.internal
chicken.io
chicken.keyword
chicken.pathname
chicken.platform
chicken.plist
chicken.port
chicken.pretty-print
chicken.string
chicken.syntax)
(include "tweaks")
(include "mini-srfi-1.scm")
(define d-depth 0)
(define scrutiny-debug #t)
(define *complain?* #f)
(define (d fstr . args)
(when (and scrutiny-debug (##sys#debug-mode?))
(printf "[debug|~a] ~a~?~%" d-depth (make-string d-depth #\space) fstr args)) )
(define dd d)
(define ddd d)
(define-syntax d (syntax-rules () ((_ . _) (void))))
(define-syntax dd (syntax-rules () ((_ . _) (void))))
(define-syntax ddd (syntax-rules () ((_ . _) (void))))
;;; Walk node tree, keeping type and binding information
;
; result specifiers:
;
; SPEC = * | (TYPE1 ...)
; TYPE = (or TYPE1 ...)
; | (not TYPE)
; | (struct NAME)
; | (procedure [NAME] (TYPE1 ... [#!optional TYPE1 ...] [#!rest [TYPE | values]]) . RESULTS)
; | VALUE
; | BASIC
; | COMPLEX
; | (forall (TVAR1 ...) TYPE)
; | (refine (SYMBOL ...) VALUE)
; | deprecated
; | (deprecated NAME)
; VALUE = string | symbol | keyword | char | number |
; boolean | true | false |
; null | eof | blob | pointer | port | locative | fixnum |
; float | bignum | ratnum | cplxnum | integer | pointer-vector
; BASIC = * | list | pair | procedure | vector | undefined | noreturn | values
; COMPLEX = (pair TYPE TYPE)
; | (vector-of TYPE)
; | (list-of TYPE)
; | (vector TYPE1 ...)
; | (list TYPE1 ...)
; RESULTS = *
; | (TYPE1 ...)
; TVAR = (VAR TYPE) | VAR
;
; global symbol properties:
;
; ##compiler#type -> TYPESPEC
; ##compiler#type-source -> 'db | 'local | 'inference
; ##compiler#predicate -> TYPESPEC
; ##compiler#specializations -> (SPECIALIZATION ...)
; ##compiler#local-specializations -> (SPECIALIZATION ...)
; ##compiler#enforce -> BOOL
; ##compiler#special-result-type -> PROCEDURE
; ##compiler#escape -> #f | 'yes | 'no
; ##compiler#type-abbreviation -> TYPESPEC
;; ##compiler#tv-root -> STRING
;
; specialization specifiers:
;
; SPECIALIZATION = ((TYPE ... [#!rest TYPE]) [RESULTS] TEMPLATE)
; TEMPLATE = #(INDEX)
; | #(INDEX ...)
; | #(SYMBOL)
; | INTEGER | SYMBOL | STRING
; | (quote CONSTANT)
; | (TEMPLATE . TEMPLATE)
;
; As an alternative to the "#!rest" and "#!optional" keywords, "&rest" or "&optional"
; may be used.
(define-constant +fragment-max-length+ 6)
(define-constant +fragment-max-depth+ 4)
(define-constant +maximal-union-type-length+ 20)
(define-constant +maximal-complex-object-constructor-result-type-length+ 256)
(define-constant value-types
'(string symbol keyword char null boolean true false blob eof
fixnum float number integer bignum ratnum cplxnum
pointer-vector port pointer locative))
(define-constant basic-types
'(* list pair procedure vector undefined deprecated noreturn values))
(define-constant struct-types
'(u8vector s8vector u16vector s16vector u32vector s32vector u64vector
s64vector f32vector f64vector thread queue environment time
continuation lock mmap condition hash-table tcp-listener))
(define-constant type-expansions
'((pair . (pair * *))
(list . (list-of *))
(vector . (vector-of *))
(boolean . (or true false))
(integer . (or fixnum bignum))
(number . (or fixnum float bignum ratnum cplxnum))
(procedure . (procedure (#!rest *) . *))))
(define-inline (struct-type? t)
(and (pair? t) (eq? (car t) 'struct)))
(define-inline (value-type? t)
(or (struct-type? t) (memq t value-types)))
(define specialization-statistics '())
(define trail '())
(define (walked-result n)
(first (node-parameters n))) ; assumes ##core#the/result node
(define (type-always-immediate? t)
(cond ((pair? t)
(case (car t)
((or) (every type-always-immediate? (cdr t)))
((forall) (type-always-immediate? (third t)))
(else #f)))
((memq t '(eof null fixnum char boolean undefined)) #t)
(else #f)))
(define (scrutinize node db complain specialize strict block-compilation)
(d "################################## SCRUTINIZE ##################################")
(define (report loc msg . args)
(when *complain?*
(warning
(conc (location-name loc)
(sprintf "~?" msg args)))))
(set! *complain?* complain)
(let ((blist '()) ; (((VAR . FLOW) TYPE) ...)
(aliased '())
(noreturn #f)
(dropped-branches 0)
(assigned-immediates 0)
(errors #f)
(safe-calls 0))
(define (constant-result lit)
(cond ((string? lit) 'string)
((keyword? lit) 'keyword)
((symbol? lit) 'symbol)
;; Do not assume fixnum width matches target platforms!
((or (big-fixnum? lit) (small-bignum? lit)) 'integer)
((fixnum? lit) 'fixnum)
((bignum? lit) 'bignum)
((flonum? lit) 'float) ; Why not "flonum", for consistency?
((ratnum? lit) 'ratnum)
((cplxnum? lit) 'cplxnum)
((boolean? lit)
(if lit 'true 'false))
((null? lit) 'null)
((list? lit)
`(list ,@(map constant-result lit)))
((pair? lit)
(simplify-type
`(pair ,(constant-result (car lit)) ,(constant-result (cdr lit)))))
((eof-object? lit) 'eof)
((vector? lit)
(simplify-type
`(vector ,@(map constant-result (vector->list lit)))))
((and (not (##sys#immediate? lit)) (##sys#generic-structure? lit))
`(struct ,(##sys#slot lit 0)))
((char? lit) 'char)
(else '*)))
(define (global-result id loc node)
(cond ((variable-mark id '##compiler#type) =>
(lambda (a)
(cond
((eq? a 'deprecated)
(r-deprecated-identifier loc node id)
'(*))
((and (pair? a) (eq? (car a) 'deprecated))
(r-deprecated-identifier loc node id (cadr a))
'(*))
(else (list a)))))
(else '(*))))
(define (blist-type id flow)
(cond ((find (lambda (b)
(and (eq? id (caar b))
(memq (cdar b) flow)) )
blist)
=> cdr)
(else #f)))
(define (variable-result id e loc node flow)
(cond ((blist-type id flow) => list)
((and (not strict)
(db-get db id 'assigned)
(not (variable-mark id '##compiler#type-source)))
'(*))
((assq id e) =>
(lambda (a)
(cond ((eq? 'undefined (cdr a))
#;(report
loc
"access to variable `~a' which has an undefined value"
(real-name id db))
'(*))
(else (list (cdr a))))))
(else (global-result id loc node))))
(define (always-true1 t)
(cond ((pair? t)
(case (car t)
((or) (every always-true1 (cdr t)))
((not) (not (always-true1 (second t))))
((forall) (always-true1 (third t)))
(else #t)))
((memq t '(* boolean false undefined noreturn)) #f)
(else #t)))
(define (always-true if-node test-node t loc)
(and-let* ((_ (always-true1 t)))
(r-cond-test-always-true loc if-node test-node t)
#t))
(define (always-false if-node test-node t loc)
(and-let* ((_ (eq? t 'false)))
(r-cond-test-always-false loc if-node test-node)
#t))
(define (single tv r-value-count-mismatch)
(if (eq? '* tv)
'*
(let ((n (length tv)))
(cond ((= 1 n) (car tv))
((zero? n)
(r-value-count-mismatch tv)
'undefined)
(else
(r-value-count-mismatch tv)
(first tv))))))
(define add-loc cons)
(define (get-specializations name)
(let* ((a (variable-mark name '##compiler#local-specializations))
(b (variable-mark name '##compiler#specializations))
(c (append (or a '()) (or b '()))))
(and (pair? c) c)))
(define (call-result node args loc typeenv)
(let* ((actualtypes (map walked-result args))
(ptype (car actualtypes))
(pptype? (procedure-type? ptype))
(nargs (length (cdr args)))
(xptype `(procedure ,(make-list nargs '*) *))
(typeenv (append-map type-typeenv actualtypes))
(op #f))
(d " call: ~a, te: ~a" actualtypes typeenv)
(cond ((and (not pptype?) (not (match-types xptype ptype typeenv)))
(r-invalid-called-procedure-type
loc node (resolve xptype typeenv) (car args) (resolve ptype typeenv))
(values '* #f))
(else
(let-values (((atypes values-rest ok alen)
(procedure-argument-types ptype nargs typeenv)))
(unless ok
(r-proc-call-argument-count-mismatch loc node alen nargs ptype))
(do ((actualtypes (cdr actualtypes) (cdr actualtypes))
(anodes (cdr args) (cdr anodes))
(atypes atypes (cdr atypes))
(i 1 (add1 i)))
((or (null? actualtypes) (null? atypes)))
(unless (match-types
(car atypes)
(car actualtypes)
typeenv)
(r-proc-call-argument-type-mismatch
loc node i
(car anodes)
(resolve (car atypes) typeenv)
(resolve (car actualtypes) typeenv)
ptype)))
(when (noreturn-procedure-type? ptype)
(set! noreturn #t))
(let ((r (procedure-result-types ptype values-rest (cdr actualtypes) typeenv)))
(let* ((pn (procedure-name ptype))
(trail0 trail))
(when pn
(cond ((and (fx= 1 nargs)
(variable-mark pn '##compiler#predicate)) =>
(lambda (pt)
(cond ((match-argument-types (list pt) (cdr actualtypes) typeenv)
(r-pred-call-always-true
loc node pt (cadr actualtypes))
(when specialize
(specialize-node!
node (cdr args)
`(let ((#(tmp) #(1))) '#t))
(set! r '(true))
(set! op (list pn pt))))
((begin
(trail-restore trail0 typeenv)
(match-argument-types (list `(not ,pt)) (cdr actualtypes) typeenv))
(r-pred-call-always-false
loc node pt (cadr actualtypes))
(when specialize
(specialize-node!
node (cdr args)
`(let ((#(tmp) #(1))) '#f))
(set! r '(false))
(set! op (list pt `(not ,pt)))))
(else (trail-restore trail0 typeenv)))))
((maybe-constant-fold-call node (node-subexpressions node)
(lambda (ok res _constant?)
(and ok (cons res ok))))
=> (lambda (res.ok)
;; Actual type doesn't matter; the node gets walked again
(set! r '*)
(mutate-node! node (list 'quote (car res.ok)))))
((and specialize (get-specializations pn)) =>
(lambda (specs)
(let loop ((specs specs))
(and (pair? specs)
(let* ((spec (car specs))
(stype (first spec))
(tenv2 (append
(append-map type-typeenv stype)
typeenv)))
(cond ((match-argument-types stype (cdr actualtypes) tenv2)
(set! op (cons pn (car spec)))
(set! typeenv tenv2)
(let* ((r2 (and (pair? (cddr spec))
(second spec)))
(rewrite (if r2
(third spec)
(second spec))))
(specialize-node! node (cdr args) rewrite)
(when r2 (set! r r2))))
(else
(trail-restore trail0 tenv2)
(loop (cdr specs))))))))))
(when op
(d " specialized: `~s' for ~a" (car op) (cdr op))
(cond ((assoc op specialization-statistics) =>
(lambda (a) (set-cdr! a (add1 (cdr a)))))
(else
(set! specialization-statistics
(cons (cons op 1)
specialization-statistics))))))
(when (and specialize (not op) (procedure-type? ptype)
(eq? '##core#call (node-class node)))
(set-car! (node-parameters node) #t)
(set! safe-calls (add1 safe-calls))))
(let ((r (if (eq? '* r) r (map (cut resolve <> typeenv) r))))
(d " result-types: ~a" r)
(values r op))))))))
(define tag
(let ((n 0))
(lambda ()
(set! n (add1 n))
n)))
(define (add-to-blist var flow type)
(let loop ((var var))
(set! blist (alist-update! (cons var flow) type blist equal?))
(let ((a (assq var aliased)))
(when a
(d " applying to alias: ~a -> ~a" (cdr a) type)
(loop (cdr a))))))
(define (initial-argument-types dest vars argc)
(if (and dest strict
(variable-mark dest '##compiler#type-source))
(let* ((ptype (variable-mark dest '##compiler#type))
(typeenv (type-typeenv ptype)))
(if (procedure-type? ptype)
(map (cut resolve <> typeenv)
(nth-value 0 (procedure-argument-types ptype argc '() #t)))
(make-list argc '*)))
(make-list argc '*)))
(define (walk n e loc dest flow ctags) ; returns result specifier
(let ((subs (node-subexpressions n))
(params (node-parameters n))
(class (node-class n)) )
(dd "walk: ~a ~s (loc: ~a, dest: ~a, flow: ~a)"
class params loc dest flow)
#;(dd "walk: ~a ~s (loc: ~a, dest: ~a, flow: ~a, blist: ~a, e: ~a)"
class params loc dest flow blist e)
(set! d-depth (add1 d-depth))
(let ((results
(case class
((##core#the/result) (list (first params))) ; already walked
((quote) (list (constant-result (first params))))
((##core#undefined) '(*))
((##core#proc) '(procedure))
((##core#variable) (variable-result (first params) e loc n flow))
((##core#inline_ref)
(list (foreign-type->scrutiny-type (second params) 'result)))
((##core#inline_loc_ref)
(list (foreign-type->scrutiny-type (first params) 'result)))
((if)
(let ((tags (cons (tag) (tag)))
(tst (first subs))
(nor-1 noreturn))
(set! noreturn #f)
(let* ((rt (single (walk tst e loc #f flow tags)
(cut r-conditional-value-count-invalid loc n tst <>)))
(c (second subs))
(a (third subs))
(nor0 noreturn))
(cond
((and (always-true n tst rt loc) specialize)
(set! dropped-branches (add1 dropped-branches))
(mutate-node! n `(let ((,(gensym) ,tst)) ,c))
(walk n e loc dest flow ctags))
((and (always-false n tst rt loc) specialize)
(set! dropped-branches (add1 dropped-branches))
(mutate-node! n `(let ((,(gensym) ,tst)) ,a))
(walk n e loc dest flow ctags))
(else
(let* ((r1 (walk c e loc dest (cons (car tags) flow) #f))
(nor1 noreturn))
(set! noreturn #f)
(let* ((r2 (walk a e loc dest (cons (cdr tags) flow) #f))
(nor2 noreturn))
(set! noreturn (or nor-1 nor0 (and nor1 nor2)))
;; when only one branch is noreturn, add blist entries for
;; all in other branch:
(when (or (and nor1 (not nor2))
(and nor2 (not nor1)))
(let ((yestag (if nor1 (cdr tags) (car tags))))
(for-each
(lambda (ble)
(when (eq? (cdar ble) yestag)
(d "adding blist entry ~a for single returning conditional branch"
ble)
(add-to-blist (caar ble) (car flow) (cdr ble))))
blist)))
(cond ((and (not (eq? '* r1)) (not (eq? '* r2)))
;;(dd " branches: ~s:~s / ~s:~s" nor1 r1 nor2 r2)
(cond ((and (not nor1) (not nor2)
(not (= (length r1) (length r2))))
(r-cond-branch-value-count-mismatch loc n c a r1 r2)
'*)
(nor1 r2)
(nor2 r1)
(else
(dd "merge branch results: ~s + ~s" r1 r2)
(map (lambda (t1 t2)
(simplify-type `(or ,t1 ,t2)))
r1 r2))))
(else '*)))))))))
((let)
;; before CPS-conversion, `let'-nodes may have multiple bindings
(let loop ((vars params) (body subs) (e2 '()))
(if (null? vars)
(walk (car body) (append e2 e) loc dest flow ctags)
(let* ((var (car vars))
(val (car body))
(t (single (walk val e loc var flow #f)
(cut r-let-value-count-invalid loc var n val <>))))
(when (and (eq? (node-class val) '##core#variable)
(not (db-get db var 'assigned)))
(let ((var2 (first (node-parameters val))))
(unless (db-get db var2 'assigned) ;XXX too conservative?
(set! aliased (alist-cons var var2 aliased)))))
(loop (cdr vars) (cdr body) (alist-cons (car vars) t e2))))))
((##core#lambda lambda)
(##sys#decompose-lambda-list
(first params)
(lambda (vars argc rest)
(let* ((namelst (if dest (list dest) '()))
(inits (initial-argument-types dest vars argc))
(args (append inits (if rest '(#!rest) '())))
(e2 (append (map (lambda (v i) (cons v i))
(if rest (butlast vars) vars)
inits)
e)))
(when dest
(d "~a: initial-argument types: ~a" dest inits))
(fluid-let ((blist '())
(noreturn #f)
(aliased '()))
(let* ((initial-tag (tag))
(r (walk (first subs)
(if rest (alist-cons rest 'list e2) e2)
(add-loc dest loc)
#f (list initial-tag) #f)))
#;(when (and specialize
dest
(variable-mark dest '##compiler#type-source)
(not unsafe))
(debugging 'x "checks argument-types" dest) ;XXX
;; [1] this is subtle: we don't want argtype-checks to be
;; generated for toplevel defs other than user-declared ones.
;; But since the ##compiler#type-source mark is set AFTER
;; the lambda has been walked (see below, [2]), nothing is added.
(generate-type-checks! n dest vars inits))
(list
(append
'(procedure)
namelst
(list
(let loop ((argc argc) (vars vars) (args args))
(cond ((zero? argc) args)
((and (not (db-get db (car vars) 'assigned))
(assoc (cons (car vars) initial-tag) blist))
=>
(lambda (a)
(cons
(cond ((eq? (cdr a) '*) '*)
(else
(d "adjusting procedure argument type for `~a' to: ~a"
(car vars) (cdr a))
(cdr a) ))
(loop (sub1 argc) (cdr vars) (cdr args)))))
(else
(cons
(car args)
(loop (sub1 argc) (cdr vars) (cdr args)))))))
r))))))))
((set! ##core#set!)
(let* ((var (first params))
(type (variable-mark var '##compiler#type))
(rt (single (walk (first subs) e loc var flow #f)
(cut r-assignment-value-count-invalid
loc var n (first subs) <>)))
(typeenv (append
(if type (type-typeenv type) '())
(type-typeenv rt)))
(b (assq var e)) )
(when (and type (not b)
(not (or (eq? type 'deprecated)
(and (pair? type)
(eq? (car type) 'deprecated))))
(not (match-types type rt typeenv)))
(when strict (set! errors #t))
(r-toplevel-var-assignment-type-mismatch loc n rt var type (first subs)))
(when (and (not type) ;XXX global declaration could allow this
(not b)
(not (eq? '* rt))
(not (db-get db var 'unknown)))
(and-let* ((val (or (db-get db var 'value)
(db-get db var 'local-value))))
(when (and (eq? val (first subs))
(or (not (variable-visible? var block-compilation))
(not (eq? (variable-mark var '##compiler#inline)
'no))))
(let ((rtlst (list (cons #f (tree-copy rt)))))
(smash-component-types! rtlst "global")
(let ((rt (cdar rtlst)))
(debugging '|I| (sprintf "(: ~s ~s)" var rt))
;; [2] sets property, but lambda has already been walked,
;; so no type-checks are generated (see also [1], above)
;; note that implicit declarations are not enforcing
(mark-variable var '##compiler#type-source 'inference)
(mark-variable var '##compiler#type rt))))))
(when b
(cond ((eq? 'undefined (cdr b)) (set-cdr! b rt))
#;(strict
(let ((ot (or (blist-type var flow) (cdr b))))
;;XXX compiler-syntax for "map" will introduce
;; assignments that trigger this warning, so this
;; is currently disabled
(unless (compatible-types? ot rt)
(report
loc
"variable `~a' of type `~a' was modified to a value of type `~a'"
var ot rt)))))
(let ((t (if (or strict (not (db-get db var 'captured)))
rt
'*))
(fl (car flow)))
;; For each outer flow F, change the var's
;; type to (or t <old-type@F>). Add a new
;; entry for current flow if it's missing.
;;
;; Motivating example:
;;
;; (let* ((x 1)
;; (y x)) ; y x : fixnum @ flow f_1
;; (if foo
;; (set! y 'a)) ; y : symbol @ flow f_2
;; y) ; (1) @ flow f_1
;;
;; At point (1) the type of y can be inferred
;; to be (or fixnum symbol). The type of x
;; should stay unchanged, however.
(let loop ((bl blist) (fl-found? #f))
(cond ((null? bl)
(unless fl-found?
(dd "set! ~a in ~a (new) --> ~a" var fl t)
(set! blist (alist-cons (cons var fl) t blist))))
((eq? var (ble-id (car bl)))
(let* ((ble (car bl))
(old-type (ble-type ble))
(t2 (simplify-type `(or ,t ,old-type))))
(dd "set! ~a in ~a, or old ~a with ~a --> ~a"
var tag old-type t t2)
(ble-type-set! ble t2)
(loop (cdr bl) (or fl-found? (eq? fl (ble-tag ble))))))
(else (loop (cdr bl) fl-found?))))))
(when (type-always-immediate? rt)
(d " assignment to var ~a in ~a is always immediate" var loc)
(set! assigned-immediates (add1 assigned-immediates))
(set-cdr! params '(#t)))
'(undefined)))
((##core#primitive) '*)
((##core#call)
(let* ((f (fragment n))
(len (length subs))
(args (map (lambda (n2 i)
(make-node
'##core#the/result
(list
(single
(walk n2 e loc #f flow #f)
(cut r-proc-call-argument-value-count loc n i n2 <>)))
(list n2)))
subs
(iota len)))
(fn (walked-result (car args)))
(pn (procedure-name fn))
(typeenv (type-typeenv
`(or ,@(map walked-result args)))) ; hack
(enforces
(and pn (variable-mark pn '##compiler#enforce)))
(pt (and pn (variable-mark pn '##compiler#predicate))))
(let-values (((r specialized?)
(call-result n args loc typeenv)))
(define (smash)
(when (and (not strict)
(or (not pn)
(and
(not (variable-mark pn '##compiler#pure))
(not (variable-mark pn '##compiler#clean)))))
(smash-component-types! e "env")
(smash-component-types! blist "blist")))
(cond (specialized?
(walk n e loc dest flow ctags)
(smash)
;; keep type, as the specialization may contain icky stuff
;; like "##core#inline", etc.
(if (eq? '* r)
r
(map (cut resolve <> typeenv) r)))
((eq? 'quote (node-class n)) ; Call got constant folded
(walk n e loc dest flow ctags))
(else
(for-each
(lambda (arg argr)
(when (eq? '##core#variable (node-class arg))
(let* ((var (first (node-parameters arg)))
(a (or (blist-type var flow) (alist-ref var e)))
(argr (resolve argr typeenv))
(oparg? (eq? arg (first subs)))
(pred (and pt
ctags
(not (db-get db var 'assigned))
(not oparg?))))
(cond (pred
;;XXX is this needed? "typeenv" is the te of "args",
;; not of "pt":
(let ((pt (resolve pt typeenv)))
(d " predicate `~a' indicates `~a' is ~a in flow ~a"
pn var pt (car ctags))
(add-to-blist
var (car ctags)
(if (not a) pt (refine-types a pt)))
;; if the variable type is an "or"-type, we can
;; can remove all elements that match the predicate
;; type
(when a
;;XXX hack, again:
(let ((at (refine-types a `(not ,pt))))
(when at
(d " predicate `~a' indicates `~a' is ~a in flow ~a"
pn var at (cdr ctags))
(add-to-blist var (cdr ctags) at))))))
(a
(when enforces
(let ((ar (if (db-get db var 'assigned)
'* ; XXX necessary?
(refine-types a argr))))
(d " assuming: ~a -> ~a (flow: ~a)"
var ar (car flow))
(add-to-blist var (car flow) ar)
(when ctags
(add-to-blist var (car ctags) ar)
(add-to-blist var (cdr ctags) ar)))))
((and oparg?
(variable-mark
var
'##compiler#special-result-type))
=> (lambda (srt)
(dd " hardcoded special result-type: ~a" var)
(set! r (srt n args loc r))))))))
subs
(cons
fn
(nth-value
0
(procedure-argument-types fn (sub1 len) typeenv))))
(smash)
(if (eq? '* r)
r
(map (cut resolve <> typeenv) r)))))))
((##core#the)
(let ((t (first params))
(rt (walk (first subs) e loc dest flow ctags)))
(cond ((eq? rt '*))
((null? rt) (r-zero-values-for-the loc (first subs) t))
(else
(when (> (length rt) 1)
(r-too-many-values-for-the loc (first subs) t rt))
(when (and (second params)
(not (compatible-types? t (first rt))))
(when strict (set! errors #t))
(r-type-mismatch-in-the loc (first subs) (first rt) t))))
(list t)))
((##core#typecase)
(let* ((ts (walk (first subs) e loc #f flow ctags))
(trail0 trail)
(typeenv0 (type-typeenv (car ts))))
;; first exp is always a variable so ts must be of length 1
(let loop ((types (cdr params)) (subs (cdr subs)))
(if (null? types)
(fail-compiler-typecase loc n (car ts) (cdr params))
(let ((typeenv (append (type-typeenv (car types)) typeenv0)))
(if (match-types (car types) (car ts) typeenv #t)
(begin ; drops exp
(mutate-node! n (car subs))
(walk n e loc dest flow ctags))
(begin
(trail-restore trail0 typeenv)
(loop (cdr types) (cdr subs)))))))))
((##core#switch ##core#cond)
(bomb "scrutinize: unexpected node class" class))
(else
(for-each (lambda (n) (walk n e loc #f flow #f)) subs)
'*))))
(set! d-depth (sub1 d-depth))
(dd "walked ~a -> ~a flow: ~a" class results flow)
results)))
(let ((rn (walk (first (node-subexpressions node)) '() '() #f (list (tag)) #f)))
(when (pair? specialization-statistics)
(with-debugging-output
'(o e)
(lambda ()
(print "specializations:")
(for-each
(lambda (ss)
(printf " ~a ~s~%" (cdr ss) (car ss)))
specialization-statistics))))
(when (positive? safe-calls)
(debugging '(o e) "safe calls" safe-calls))
(when (positive? dropped-branches)
(debugging '(o e) "dropped branches" dropped-branches))
(when (positive? assigned-immediates)
(debugging '(o e) "assignments to immediate values" assigned-immediates))
(d "############################### SCRUTINIZE FINISH ##############################")
(when errors
(quit-compiling "some variable types do not satisfy strictness"))
rn)))
;;; replace pair/vector types with components to variants with undetermined
;; component types (used for env or blist); also convert "list[-of]" types
;; into "pair", since mutation may take place
(define (smash-component-types! lst where)
;; assumes list of the form "((_ . T1) ...)"
(do ((lst lst (cdr lst)))
((null? lst))
(let loop ((t (cdar lst))
(change! (cute set-cdr! (car lst) <>)))
(when (pair? t)
(case (car t)
((vector-of)
(dd " smashing `~s' in ~a" (caar lst) where)
(change! 'vector)
(car t))
((vector)
(dd " smashing `~s' in ~a" (caar lst) where)
;; (vector x y z) => (vector * * *)
(change! (cons 'vector (map (constantly '*) (cdr t))))
(car t))
((list-of list)
(dd " smashing `~s' in ~a" (caar lst) where)
(change! '(or pair null))
(car t))
((pair)
(dd " smashing `~s' in ~a" (caar lst) where)
(change! (car t))
(car t))
((forall)
(loop (third t)
(cute set-car! (cddr t) <>))))))))
;;; blist (binding list) helpers
;;
;; - Entries (ble) in blist have type ((symbol . fixnum) . type)
(define ble-id caar) ; variable name : symbol
(define ble-tag cdar) ; block tag : fixnum
(define ble-type cdr) ; variable type : valid type sexp
(define ble-type-set! set-cdr!)
;;; Type-matching
;
; - "all" means: all elements in `or'-types in second argument must match
(define (match-types t1 t2 #!optional (typeenv (type-typeenv `(or ,t1 ,t2))) all)
(define (match-args args1 args2)
(d "match args: ~s <-> ~s" args1 args2)
(let loop ((args1 args1) (args2 args2) (opt1 #f) (opt2 #f))
(cond ((null? args1)
(or opt2
(null? args2)
(optargs? (car args2))))
((null? args2)
(or opt1
(optargs? (car args1))))
((eq? '#!optional (car args1))
(loop (cdr args1) args2 #t opt2))
((eq? '#!optional (car args2))
(loop args1 (cdr args2) opt1 #t))
((eq? '#!rest (car args1))
(match-rest (rest-type (cdr args1)) args2 opt2))
((eq? '#!rest (car args2))
(match-rest (rest-type (cdr args2)) args1 opt1))
((match1 (car args1) (car args2))
(loop (cdr args1) (cdr args2) opt1 opt2))
(else #f))))
(define (match-rest rtype args opt) ;XXX currently ignores `opt'
(let-values (((head tail) (span (lambda (x) (not (eq? '#!rest x))) args)))
(and (every
(lambda (t)
(or (eq? '#!optional t)
(match1 rtype t)))
head)
(match1 rtype (if (pair? tail) (rest-type (cdr tail)) '*)))))
(define (optargs? a)
(memq a '(#!rest #!optional)))
(define (match-results results1 results2)
(cond ((eq? '* results1))
((eq? '* results2) (not all))
((null? results1) (null? results2))
((null? results2) #f)
((and (memq (car results1) '(undefined noreturn))
(memq (car results2) '(undefined noreturn))))
((match1 (car results1) (car results2))
(match-results (cdr results1) (cdr results2)))
(else #f)))
(define (rawmatch1 t1 t2)
(fluid-let ((all #f))
(match1 t1 t2)))
(define (every-match1 lst1 lst2)
(let loop ((lst1 lst1) (lst2 lst2))
(cond ((null? lst1))
((match1 (car lst1) (car lst2)) (loop (cdr lst1) (cdr lst2)))
(else #f))))
(define (match1 t1 t2)
;; note: the order of determining the type is important
(dd " match1: ~s <-> ~s" t1 t2)
(cond ((eq? t1 t2))
;;XXX do we have to handle circularities?
((and (symbol? t1) (assq t1 typeenv)) =>
(lambda (e)
(cond ((second e)
(and (match1 (second e) t2)
(or (not (third e)) ; constraint
(rawmatch1 (third e) t2))))
;; special case for two unbound typevars
((and (symbol? t2) (assq t2 typeenv)) =>
(lambda (e2)
;;XXX probably not fully right, consider:
;; (forall (a b) ((a a b) ->)) + (forall (c d) ((c d d) ->))
;; or is this not a problem? I don't know right now...
(or (not (second e2))
(and (match1 t1 (second e2))
(or (not (third e2)) ; constraint
(rawmatch1 t1 (third e2)))))))
((or (not (third e))
(rawmatch1 (third e) t2))
(dd " unify ~a = ~a" t1 t2)
(set! trail (cons t1 trail))
(set-car! (cdr e) t2)
#t)
(else #f))))
((and (symbol? t2) (assq t2 typeenv)) =>
(lambda (e)
(cond ((second e)
(and (match1 t1 (second e))
(or (not (third e)) ; constraint
(rawmatch1 t1 (third e)))))
((or (not (third e))
(rawmatch1 t1 (third e)))
(dd " unify ~a = ~a" t2 t1)
(set! trail (cons t2 trail))
(set-car! (cdr e) t1)
#t)
(else #f))))
((eq? t1 '*))
((eq? t2 '*) (not all))
((eq? t1 'undefined) #f)
((eq? t2 'undefined) #f)
((eq? t1 'noreturn))
((eq? t2 'noreturn))
((maybe-expand-type t1) => (cut match1 <> t2))
((maybe-expand-type t2) => (cut match1 t1 <>))
((and (pair? t1) (eq? 'not (car t1)))
(fluid-let ((all (not all)))
(let* ((trail0 trail)
(m (match1 (cadr t1) t2)))
(trail-restore trail0 typeenv)
(not m))))
((and (pair? t2) (eq? 'not (car t2)))
(and (not all)
(fluid-let ((all #t))
(let* ((trail0 trail)
(m (match1 (cadr t2) t1)))
(trail-restore trail0 typeenv)
(not m)))))
;; this is subtle: "or" types for t2 are less restrictive,
;; so we handle them before "or" types for t1
((and (pair? t2) (eq? 'or (car t2)))
(over-all-instantiations
(cdr t2)
typeenv
all
(lambda (t) (match1 t1 t))))
;; s.a.
((and (pair? t1) (eq? 'or (car t1)))
(over-all-instantiations