-
Notifications
You must be signed in to change notification settings - Fork 1
/
a15.scm
4266 lines (4178 loc) · 179 KB
/
a15.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
;; todo: * use SCCs in purify-letrec, and assimilate elsewhere;
;; * lift procedures and lift to global
;; how to trace globals (more) precisely?
;; You can use record referred globals mixed with code. You might take
;; a look at https://simonmar.github.io/posts/2018-06-22-New-SRTs.html
;; or inspect chez source code. Chez maintains a symbol table for code
;; objects for relocation, so that might be a different story.
;; * utilize more addressing modes (for vector)
;; * use disp- and index-operand earlier, and adjust (among others)
;; instruction selection accordingly
(load "match.scm")
(load "helpers.scm")
(load "driver.scm")
(load "fmts.pretty")
(define *standard* 'r6rs)
(define *cp-1-enabled* #t) ; better set to #f when testing trivial cases
(define *closure-optimization-enabled* #t)
(define *iterated-coalescing-enabled* #t)
(define *optimize-jumps-enabled* #t)
(define *max-inline-literal-size* 64)
(define *collection-enabled* #t)
(define *continuation-enabled* #t)
(define *optimize-allocation-enabled* #t)
;; (set! parameter-registers '(r8 r9 rdi rsi))
(set! allocation-pointer-register 'r11) ; leave rdx for division
(define stack-base-register 'r14)
(define end-of-allocation-register 'r13)
(define return-address-location 'fv0)
(define mask-symbol #b111)
(define tag-symbol #b100)
(define mask-char #b11111111)
(define tag-char #b11111110)
(define shift-char 8)
(define flag-true $true)
(define flag-false $false)
(define flag-nil $nil)
(define flag-trivial 0)
(define flag-pair 1)
(define flag-vector 2)
(define flag-vector-end 3)
(define flag-empty-vector 4)
(define take
(lambda (n lst)
(cond [(null? lst) '()]
[(= n 0) '()]
[else (cons (car lst) (take (- n 1) (cdr lst)))])))
(define drop
(lambda (n lst)
(cond [(null? lst) '()]
[(= n 0) lst]
[else (drop (- n 1) (cdr lst))])))
(define id (lambda (x) x))
(define binops
'(+ - * logand logor sra ash quotient remainder))
(define binop?
(lambda (x)
(memq x binops)))
(define commutative?
(lambda (x)
(memq x '(+ * logand logor))))
(define binop->assembly
(lambda (op)
(match op
[+ 'addq]
[- 'subq]
[* 'imulq]
[logand 'andq]
[logor 'orq]
[sra 'sarq]
[ash 'shlq])))
(define overflow?
(lambda (x)
(and (integer? x)
(exact? x)
(not (fixnum-range? x)))))
(define relops
'(= < <= > >=))
(define relop?
(lambda (x) (memq x relops)))
(define rel->assembly
(lambda (rel)
(match rel
[= 'je]
[< 'jl]
[<= 'jle]
[> 'jg]
[>= 'jge])))
(define not-rel->assembly
(lambda (rel)
(match rel
[= 'jne]
[< 'jge]
[<= 'jg]
[> 'jle]
[>= 'jl])))
(define flip-relop
(lambda (x)
(match x
[= '=]
[< '>]
[> '<]
[>= '<=]
[<= '>=])))
(define lambda?
(lambda (x)
(and (pair? x) (eq? (car x) 'lambda))))
(define trivial?
(lambda (v)
(or (uvar? v) (label? v) (integer? v))))
(define (make-nopless-begin x*)
(let ([x* (remove '(nop) x*)])
(if (null? x*)
'(nop)
(make-begin x*))))
(define make-let
(lambda (binding body)
(if (null? binding)
body
`(let ,binding ,body))))
(define make-letrec
(lambda (binding body)
(if (null? binding)
body
`(letrec ,binding ,body))))
(define make-let-assigned
(lambda (binding assign body)
(if (null? binding)
body
`(let ,binding (assigned ,assign ,body)))))
(define value-primitives
`(+ - * quotient remainder car cdr cons make-vector vector-length vector-ref void make-procedure procedure-ref procedure-code integer->char char->integer read-char ,@(if *continuation-enabled* '(call-with-current-continuation) '())))
(define side-effect-primitives
'(read-char ,@(if *continuation-enabled* '(call-with-current-continuation) '())))
(define predicate-primitives
'(<= < = >= > boolean? eq? fixnum? null? pair? vector? procedure? symbol? char=? char?))
(define effect-primitives
'(set-car! set-cdr! vector-set! procedure-set! inspect write display))
(define value-primitive?
(lambda (x) (memq x value-primitives)))
(define predicate-primitive?
(lambda (x) (memq x predicate-primitives)))
(define effect-primitive?
(lambda (x) (memq x effect-primitives)))
(define primitive?
(lambda (x) (or (value-primitive? x) (predicate-primitive? x) (effect-primitive? x))))
(define side-effect-primitive?
(lambda (x) (memq x side-effect-primitives)))
(define user-primitive
`([+ . 2]
[- . 2]
[* . 2]
[quotient . 2]
[remainder . 2]
[car . 1]
[cdr . 1]
[cons . 2]
[make-vector . 1]
[vector-length . 1]
[vector-ref . 2]
[void . 0]
[char->integer . 1]
[integer->char . 1]
[<= . 2]
[< . 2]
[= . 2]
[>= . 2]
[> . 2]
[char=? . 2]
[boolean? . 1]
[eq? . 2]
[fixnum? . 1]
[null? . 1]
[pair? . 1]
[procedure? . 1]
[vector? . 1]
[symbol? . 1]
[char? . 1]
[set-car! . 2]
[set-cdr! . 2]
[vector-set! . 3]
,@(if *continuation-enabled* '([call-with-current-continuation . 1]) '())
[inspect . 1]
[write . 1]
[display . 1]
[read-char . 0]))
(define user-primitive?
(lambda (x) (assq x user-primitive)))
(define user-primitive->arity
(lambda (x) (cdr (assq x user-primitive))))
(define aux-keywords
'(else unquote))
(define aux-keyword?
(lambda (x) (memq x aux-keywords)))
(define syntax-keywords
'(quote quasiquote if and or not cond begin set! lambda let letrec))
(define syntax-keyword?
(lambda (x) (memq x syntax-keywords)))
(define-who parse-scheme
(define check-bind-variable
(lambda (var* e)
(cond [(not (for-all symbol? var*))
(format-error who "invalid bound variable in ~s" e)]
[(not (set? var*))
(format-error who "duplicate bound variable in ~s" e)]
[else (void)])))
(define check-parameter
(lambda (var* e)
(cond [(not (for-all symbol? var*))
(format-error who "invalid parameter list in ~s" e)]
[(not (set? var*))
(format-error who "invalid parameter list in ~s" e)]
[else (void)])))
(define check-datum
(lambda (d)
(cond [(pair? d)
(check-datum (car d))
(check-datum (cdr d))]
[(vector? d)
(vector-for-each check-datum d)]
[(integer? d)
(unless (and (exact? d)
(fixnum-range? d))
(format-error who "~s is not a fixnum" d))]
[(boolean? d) (void)]
[(null? d) (void)]
[(symbol? d) (void)]
[(char? d) (void)]
[else (format-error who "invalid datum ~s" d)])))
(define convert-and
(lambda (rand*)
(cond [(null? rand*) '(quote #t)]
[(null? (cdr rand*)) (car rand*)]
[else `(if ,(car rand*)
,(convert-and (cdr rand*))
(quote #f))])))
(define convert-or
(lambda (rand*)
(cond [(null? rand*) '(quote #f)]
[(null? (cdr rand*)) (car rand*)]
[else (let ([tmp (unique-name 'tmp)])
`(let ([,tmp ,(car rand*)])
(if ,tmp ,tmp ,(convert-or (cdr rand*)))))])))
(define convert-cond
(lambda (clause* env)
(cond [(null? clause*) '(void)]
[else (let* ([cls (car clause*)]
[cond (car cls)]
[body* (map (Expr env) (cdr cls))])
(if (and (eq? cond 'else)
(not (assq 'else env)))
(if (eq? (cdr clause*) '())
(make-begin body*)
(format-error who "misplaced aux keyword else"))
`(if ,((Expr env) cond)
,(make-begin body*)
,(convert-cond (cdr clause*) env))))])))
(define convert-quasiquote
(lambda (qd env level)
(define unchanged
(lambda (qd expr)
(and (pair? expr)
(eq? (car expr) 'quote)
(eq? qd (cadr expr)))))
(cond [(and (pair? qd)
(eq? (car qd) 'unquote)
(not (assq 'unquote env))
(= level 0))
((Expr env) (cadr qd))]
[(pair? qd)
(let* ([a (car qd)]
[d (cdr qd)]
[level^ (cond [(eq? a 'quasiquote) (add1 level)]
[(eq? a 'unquote) (sub1 level)]
[else level])]
[a^ (convert-quasiquote a env level)]
[d^ (convert-quasiquote d env level^)])
(if (and (unchanged a a^) (unchanged d d^))
`(quote ,qd)
`(cons ,a^ ,d^)))]
[(vector? qd)
(let ([qd^ (vector-map (lambda (qd) (convert-quasiquote qd env level)) qd)]
[length (vector-length qd)])
(if (let loop ([i 0])
(if (= i length)
#t
(and (unchanged (vector-ref qd i) (vector-ref qd^ i))
(loop (add1 i)))))
`(quote ,qd)
(let* ([tmp (unique-name 'tmp)]
[fill
(let loop ([i 0])
(if (< i length)
(cons
`(vector-set! ,tmp (quote ,i) ,(vector-ref qd^ i))
(loop (add1 i)))
'()))])
`(let ([,tmp (make-vector (quote ,length))])
,(make-begin `(,@fill ,tmp))))))]
[else
(check-datum qd)
`(quote ,qd)])))
(define Var
(lambda (env)
(lambda (var)
(cond [(assq var env) => cdr]
[(user-primitive? var) var]
[(syntax-keyword? var) (format-error who "invalid syntax ~s" var)]
[(aux-keyword? var) (format-error who "misplaced aux keyword ~s" var)]
[else (format-error who "variable ~s is not bound" var)]))))
(define Expr
(lambda (env)
(lambda (x)
(match x
[#t '(quote #t)]
[#f '(quote #f)]
[,n (guard (integer? x) (exact? x) (fixnum-range? x)) `(quote ,n)]
[,ch (guard (char? ch)) `(quote ,ch)] ; ascii only
[call/cc (guard *continuation-enabled*) 'call-with-current-continuation]
[,var (guard (symbol? var)) ((Var env) var)]
[(,proc ,[(Expr env) -> arg*] ...) (guard (assq proc env))
`(,((Expr env) proc) ,arg* ...)]
[(call/cc ,rand* ...) (guard *continuation-enabled*)
((Expr env) `(call-with-current-continuation ,rand* ...))]
[(,prim ,[(Expr env) -> rand*] ...) (guard (user-primitive? prim))
(if (= (user-primitive->arity prim) (length rand*))
`(,prim ,rand* ...)
(format-error who "incorrect argument count in call ~s" x))]
[(quote ,datum)
(check-datum datum)
`(quote ,datum)]
[(quasiquote ,quasidatum) (convert-quasiquote quasidatum env 0)]
[(if ,[(Expr env) -> cond] ,[(Expr env) -> conseq])
`(if ,cond ,conseq (void))]
[(if ,[(Expr env) -> cond] ,[(Expr env) -> conseq] ,[(Expr env) -> alter])
`(if ,cond ,conseq ,alter)]
[(and ,[(Expr env) -> rand*] ...) (convert-and rand*)]
[(or ,[(Expr env) -> rand*] ...) (convert-or rand*)]
[(not ,[(Expr env) -> rand]) `(if ,rand (quote #f) (quote #t))]
[(cond ,clause* ...)
(if (<= (length clause*) 0)
(format-error who "invalid syntax ~s" x)
(convert-cond clause* env))]
[(begin ,[(Expr env) -> expr*] ... ,[(Expr env) -> expr])
(make-begin `(,expr* ... ,expr))]
[(set! ,var ,[(Expr env) -> expr])
(if (not (symbol? var))
(format-error who "invalid syntax ~s" x)
(if (not (assq var env))
(if (user-primitive? var)
(format-error who "attemp to assign immutable variable ~s" var)
(format-error who "variable ~s is not bound" var))
`(set! ,((Var env) var) ,expr)))]
[(lambda (,formal* ...) ,expr* ...)
(if (<= (length expr*) 0) (format-error who "invalid syntax ~s" x)) ; can improve
(check-parameter formal* x)
(let* ([uvar* (map unique-name formal*)]
[env^ (append (map cons formal* uvar*) env)]
[body* (map (Expr env^) expr*)])
`(lambda (,uvar* ...)
,(make-begin `(,body* ...))))]
[(let ([,var* ,[(Expr env) -> expr*]] ...) ,body* ...)
(if (<= (length body*) 0) (format-error who "invalid syntax ~s" x))
(check-bind-variable var* x)
(let* ([uvar* (map unique-name var*)]
[env^ (append (map cons var* uvar*) env)]
[body*^ (map (Expr env^) body*)])
`(let ([,uvar* ,expr*] ...)
,(make-begin `(,body*^ ...))))]
[(letrec ([,var* ,expr*] ...) ,body* ...)
(if (<= (length body*) 0) (format-error who "invalid syntax ~s" x))
(check-bind-variable var* x)
(let* ([uvar* (map unique-name var*)]
[env^ (append (map cons var* uvar*) env)]
[expr*^ (map (Expr env^) expr*)]
[body*^ (map (Expr env^) body*)])
`(letrec ([,uvar* ,expr*^] ...)
,(make-begin `(,body*^ ...))))]
[(,[(Expr env) -> proc] ,[(Expr env) -> arg*] ...) `(,proc ,arg* ...)]
[,x (format-error who "invalid expression ~s" x)]))))
(lambda (p) ((Expr '()) p)))
(define-who proceduralize-primitives
(define bindings)
(define Expr
(lambda (e)
(match e
[,prim (guard (user-primitive? prim))
(when (not (assq prim bindings))
(let ([new-p (unique-name prim)]
[new-fml* (let gen ([c (user-primitive->arity prim)])
(if (= c 0) '()
(cons (unique-name 'x) (gen (sub1 c)))))])
(set! bindings
(cons (cons prim `(,new-p (lambda ,new-fml* (,prim . ,new-fml*))))
bindings))))
(cadr (assq prim bindings))]
[(if ,[cond] ,[conseq] ,[alter]) `(if ,cond ,conseq ,alter)]
[(begin ,[expr*] ... ,[expr]) `(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[body*]] ...) ,[body]) `(let ([,uvar* ,body*] ...) ,body)]
[(letrec ([,uvar* ,[body*]] ...) ,[body]) `(letrec ([,uvar* ,body*] ...) ,body)]
[(set! ,uvar ,[expr]) `(set! ,uvar ,expr)]
[(lambda (,uvar* ...) ,[body]) `(lambda (,uvar* ...) ,body)]
[(,prim ,[rand*] ...) (guard (primitive? prim)) `(,prim ,rand* ...)]
[(quote ,imm) `(quote ,imm)]
[(,[proc] ,[arg*] ...) `(,proc ,arg* ...)]
[,x (guard (uvar? x)) x])))
(lambda (p)
(set! bindings '())
(let ([p^ (Expr p)])
(make-letrec (map cdr bindings) p^))))
(define-who convert-complex-datum
(define bindings)
(define fillings)
(define size
(lambda (d)
(cond [(pair? d) (add1 (+ (size (car d)) (size (cdr d))))]
[(vector? d) (add1 (apply + (vector->list (vector-map size d))))]
[else 1])))
(define do-conversion
(lambda (const)
(cond
[(and (pair? const))
`(cons
,(do-conversion (car const))
,(do-conversion (cdr const)))]
[(vector? const)
(let* ([tmp (unique-name 'tmp)]
[length (vector-length const)]
[fill (let loop ([i 0])
(cond [(< i length)
(cons
`(vector-set! ,tmp (quote ,i) ,(do-conversion (vector-ref const i)))
(loop (add1 i)))]
[else '()]))])
`(let ([,tmp (make-vector (quote ,length))])
,(make-begin `(,@fill ,tmp))))]
[else `(quote ,const)])))
(define remove-last
(lambda (l)
(cond [(null? (cdr l)) '()]
[else (cons (car l) (remove-last (cdr l)))])))
(define Expr
(lambda (e)
(match e
[(if ,[cond] ,[conseq] ,[alter]) `(if ,cond ,conseq ,alter)]
[(begin ,[expr*] ... ,[expr]) `(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[body*]] ...) ,[body]) `(let ([,uvar* ,body*] ...) ,body)]
[(letrec ([,uvar* ,[body*]] ...) ,[body]) `(letrec ([,uvar* ,body*] ...) ,body)]
[(set! ,uvar ,[expr]) `(set! ,uvar ,expr)]
[(lambda (,uvar* ...) ,[body]) `(lambda (,uvar* ...) ,body)]
[(,prim ,[rand*] ...) (guard (primitive? prim)) `(,prim ,rand* ...)]
[(quote ,imm)
(if (and (or (pair? imm) (vector? imm)))
(if (or (not *max-inline-literal-size*) (< (size imm) *max-inline-literal-size*))
(let ([imm^ (do-conversion imm)])
(cond [(and (pair? imm))
(let ([tmp (unique-name 'complex)])
(set! bindings (cons `(,tmp ,imm^) bindings))
tmp)]
[(vector? imm)
(set! bindings (cons (caadr imm^) bindings))
(set! fillings (append (remove-last (cdaddr imm^)) fillings))
(caaadr imm^)]))
(let ([tmp (unique-name 'complex)])
(set! bindings (cons (list tmp (list 'quote imm)) bindings))
tmp))
`(quote ,imm))]
[(,[proc] ,[arg*] ...) `(,proc ,arg* ...)]
[,x (guard (uvar? x)) x]
[,lab (guard (label? lab)) lab])))
(lambda (p)
(set! bindings '())
(set! fillings '())
(make-let bindings
(make-begin `(,@fillings ,(Expr p))))))
(define optimize-direct-call
(lambda (e)
(match e
[(if ,[cond] ,[conseq] ,[alter]) `(if ,cond ,conseq ,alter)]
[(begin ,[expr*] ... ,[expr]) `(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[body*]] ...) ,[body]) `(let ([,uvar* ,body*] ...) ,body)]
[(letrec ([,uvar* ,[body*]] ...) ,[body]) `(letrec ([,uvar* ,body*] ...) ,body)]
[(lambda (,uvar* ...) ,[body]) `(lambda (,uvar* ...) ,body)]
[(,prim ,[rand*] ...) (guard (primitive? prim)) `(,prim ,rand* ...)]
[(quote ,imm) `(quote ,imm)]
[((lambda (,uvar* ...) ,[body]) ,[arg*] ...)
(make-let `([,uvar* ,arg*] ...) body)]
[(,[proc] ,[arg*] ...) `(,proc ,arg* ...)]
[,x x])))
(define-who uncover-assigned
(define Expr
(lambda (e)
(match e
[(if ,[Expr -> cond a1] ,[Expr -> conseq a2] ,[Expr -> alter a3])
(values `(if ,cond ,conseq ,alter) (union a1 a2 a3))]
[(begin ,[Expr -> expr* a*] ... ,[Expr -> expr a])
(values `(begin ,expr* ... ,expr) (apply union a a*))]
[(let ([,uvar* ,[Expr -> body* a*]] ...) ,[Expr -> body a])
(values
`(let ([,uvar* ,body*] ...)
(assigned ,(intersection a uvar*) ,body))
(union (difference a uvar*) (apply union a*)))]
[(letrec ([,uvar* ,[Expr -> body* a*]] ...) ,[Expr -> body a])
(let ([a^ (apply union a a*)])
(values
`(letrec ([,uvar* ,body*] ...)
(assigned ,(intersection a^ uvar*) ,body))
(difference a^ uvar*)))]
[(set! ,uvar ,[Expr -> expr a])
(values `(set! ,uvar ,expr) (set-cons uvar a))]
[(lambda (,uvar* ...) ,[Expr -> body a])
(values
`(lambda (,uvar* ...)
(assigned ,(intersection a uvar*) ,body))
(difference a uvar*))]
[(,prim ,[Expr -> rand* a*] ...) (guard (primitive? prim))
(values `(,prim ,rand* ...) (apply union a*))]
[(quote ,imm) (values e '())]
[(,[Expr -> proc a] ,[Expr -> arg* a*] ...)
(values `(,proc ,arg* ...) (apply union a a*))]
[,x (guard (uvar? x)) (values x '())])))
(lambda (p)
(match p
[,[Expr -> e a] e])))
(define-who purify-letrec ; see ``fixing letrec''
(define simple?
(lambda (x* no-capture? no-effect?)
(lambda (e)
(match e
[(if ,cond ,conseq ,alter)
(and ((simple? x* no-capture? no-effect?) cond)
((simple? x* no-capture? no-effect?) conseq)
((simple? x* no-capture? no-effect?) alter))]
[(begin ,expr* ... ,expr)
(for-all (simple? x* no-capture? no-effect?) (cons expr expr*))]
[(let ([,uvar* ,expr*] ...) (assigned (,as* ...) ,expr))
(for-all (simple? x* no-capture? no-effect?) (cons expr expr*))]
[(letrec ([,uvar* ,expr*] ...) ,body) ((simple? x* no-capture? no-effect?) body)]
[(set! ,uvar ,expr)
(if no-effect? #f
((simple? x* no-capture? no-effect?) expr))]
[(lambda (,uvar* ...) (assigned (,as* ...) ,expr)) ((simple? x* #f #f) expr)]
[(call-with-current-continuation ,expr) (guard *continuation-enabled*)
(cond [no-effect? #f]
[(and no-capture? (eq? *standard* 'r5rs)) #f]
[else ((simple? x* no-capture? no-effect?) expr)])]
[(,prim ,rand* ...)
(guard (or (effect-primitive? prim)
(side-effect-primitive? prim)))
(if no-effect? #f (for-all (simple? x* no-capture? no-effect?) rand*))]
[(,prim ,rand* ...) (guard (primitive? prim))
(for-all (simple? x* no-capture? no-effect?) rand*)]
[(quote ,imm) #t]
[(,proc ,arg* ...)
(cond [no-effect? #f]
[(and no-capture? (eq? *standard* 'r5rs)) #f]
[else (for-all (simple? x* no-capture? no-effect?) (cons proc arg*))])]
[,x (guard (uvar? x)) (not (memq x x*))]))))
(define effect-free? (simple? '() #f #t))
(define partition
(lambda (x* b* as* body-lambda? no-effect?)
(let loop ([x* x*] [b* b*] [as* as*] [simple* '()] [lambda* '()] [complex* '()])
(if (null? b*)
(values as* simple* lambda* complex*)
(let* ([b (car b*)]
[x (car b)]
[e (cadr b)])
(cond
[(and (memq x as*) (not body-lambda?))
(loop x* (cdr b*) as* simple* lambda* (cons b complex*))]
[(and (not (memq x as*)) (lambda? e))
(loop x* (cdr b*) as* simple* (cons b lambda*) complex*)]
[(or (not (pair? e))
(and (not (eq? (car e) 'let))
(not (eq? (car e) 'letrec))
((simple? x* #t no-effect?) e)))
(loop x* (cdr b*) as* (cons b simple*) lambda* complex*)]
[else
(match e
[(letrec ([,x*^ ,e*^] ...) ,body^)
(loop (append x*^ x*) (cons (list x body^) (cdr b*))
as* simple* (append `([,x*^ ,e*^] ...) lambda*) complex*)]
[(let ([,x*^ ,e*^] ...) (assigned (,as*^ ...) ,body^))
(let-values ([(as*^^ simple*^ lambda*^ complex*^)
(partition '() `([,x*^ ,e*^] ...) as*^
(effect-free? body^) ; or lambda?
(not (effect-free? body^)))])
(if (null? complex*^)
(loop (append x*^ x*) (cons (list x body^) (cdr b*))
(append as*^^ as*^ as*)
(append simple*^ simple*)
(append lambda*^ lambda*)
complex*)
(loop x* (cdr b*) as* simple* lambda* (cons b complex*))))]
[,x (loop x* (cdr b*) as* simple* lambda* (cons b complex*))])]))))))
(lambda (e)
(match e
[(if ,[cond] ,[conseq] ,[alter]) `(if ,cond ,conseq ,alter)]
[(begin ,[expr*] ... ,[expr]) `(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[expr*]] ...) (assigned (,as* ...) ,[expr]))
`(let ([,uvar* ,expr*] ...) (assigned (,as* ...) ,expr))]
[(letrec ([,uvar* ,[expr*]] ...) (assigned (,as* ...) ,[body]))
(let-values ([(as* simple* lambda* complex*)
(partition uvar* `((,uvar* ,expr*) ...) as* (effect-free? body) #f)])
(let* ([c-x* (map car complex*)]
[c-e* (map cadr complex*)]
[tmp* (map unique-name c-x*)]
[innermost
(if (null? complex*) body
(make-begin
`((let ([,tmp* ,c-e*] ...)
(assigned ()
(begin (set! ,c-x* ,tmp*) ...)))
,body)))])
(make-let-assigned simple*
(intersection (map car simple*) as*)
(make-let-assigned `([,c-x* (void)] ...)
c-x*
(make-letrec lambda*
innermost)))))]
[(set! ,uvar ,[expr]) `(set! ,uvar ,expr)]
[(lambda (,uvar* ...) (assigned (,as* ...) ,[expr]))
`(lambda (,uvar* ...) (assigned (,as* ...) ,expr))]
[(,prim ,[rand*] ...) (guard (primitive? prim)) `(,prim ,rand* ...)]
[(quote ,imm) `(quote ,imm)]
[(,[proc] ,[arg*] ...) `(,proc ,arg* ...)]
[,x (guard (uvar? x)) x])))
(define-who optimize-constant
(define primitive->datum
(lambda (prim)
(match prim
[+ +]
[- -]
[* *]
[quotient quotient]
[remainder remainder]
[car car]
[cdr cdr]
[cons cons]
[vector-length vector-length]
[char->integer char->integer]
[integer->char integer->char]
[<= <=]
[< <]
[= =]
[>= >=]
[> >]
[char=? char=?]
[boolean? boolean?]
[eq? eq?]
[fixnum? fixnum?]
[null? null?]
[pair? pair?]
[procedure? procedure?]
[vector? vector?]
[symbol? symbol?]
[char? char?]
[,x #f])))
(define complex?
(lambda (im)
(not (or (null? im)
(symbol? im)
(char? im)
(integer? im)
(boolean? im)))))
(define merge-value
(lambda (v1 v2)
(cond [(null? v1) '()]
[(null? v2) '()]
[(eq? (car v1) (car v2)) v1]
[else '()])))
(define Expr
(lambda (env)
(lambda (e)
(match e
[(if ,[(Expr env) -> cd-e cd-v] ,[(Expr env) -> cq-e cq-v] ,[(Expr env) -> at-e at-v])
(cond [(null? cd-v) (values `(if ,cd-e ,cq-e ,at-e)
(merge-value cq-v at-v))]
[(car cd-v) (values cq-e cq-v)]
[else (values at-e at-v)])]
[(begin ,[(Expr env) -> e* v*] ... ,[(Expr env) -> e v])
(values (make-begin `(,e* ... ,e)) v)]
[(let ([,uvar* ,[(Expr env) -> e* v*]] ...)
(assigned (,a* ...) ,body))
(let ([env^ (append
(filter (lambda (x) (and (not (memq (car x) a*))
(not (null? (cdr x)))
(not (complex? (cadr x)))))
(map cons uvar* v*))
env)])
(let-values ([(e v) ((Expr env^) body)])
(values
`(let ([,uvar* ,e*] ...)
(assigned (,a* ...) ,e))
v)))]
[(letrec ([,uvar* ,[(Expr env) -> e* v*]] ...) ,[(Expr env) -> e v])
(values `(letrec ([,uvar* ,e*] ...) ,e) v)]
[(set! ,uvar ,[(Expr env) -> expr value])
(values `(set! ,uvar ,expr) (list (void)))]
[(lambda (,uvar* ...) (assigned (,a* ...) ,[(Expr env) -> body _]))
(values `(lambda (,uvar* ...) (assigned (,a* ...) ,body)) '())]
[(,prim ,[(Expr env) -> rand* v*] ...) (guard (primitive? prim))
(cond [(and (not (exists null? v*))
(primitive->datum prim)) =>
(lambda (d)
(let ([v (apply d (map car v*))])
(cond [(overflow? v) (values `(,prim ,rand* ...) '())]
[(complex? v) (values `(,prim ,rand* ...) (list v))]
[else (values `(quote ,v) (list v))])))]
[else (values `(,prim ,rand* ...) '())])]
[(quote ,imm) (values `(quote ,imm) (list imm))]
[(,[(Expr env) -> proc _] ,[(Expr env) -> arg* _*] ...)
(values `(,proc ,arg* ...) '())]
[,x (guard (uvar? x))
(cond [(assq x env) =>
(lambda (p)
(let ([v (cdr p)])
(values `(quote ,(car v)) v)))]
[else (values x '())])]))))
(lambda (p)
(if *cp-1-enabled*
(match p
[,[(Expr '()) -> e v] e])
p)))
(define-who optimize-useless
(define Expr
(lambda (ctx)
(lambda (expr)
(match expr
[(if ,[(Expr 'value) -> cond u1] ,[conseq u2] ,[alter u3])
(values `(if ,cond ,conseq ,alter) (union u1 u2 u3))]
[(begin ,[(Expr 'effect) -> e* u*] ... ,[e u])
(values
(make-begin `(,e* ... ,e))
(apply union u u*))]
[(let ,bd*
(assigned (,as* ...) ,[body u]))
(let-values ([(bd-used* bd-unused*)
(partition (lambda (bd) (memq (car bd) u)) bd*)])
(match (cons bd-used* bd-unused*)
[(([,x* ,[(Expr 'value) -> expr* u1*]] ...) .
([,y* ,[(Expr 'effect) -> effect* u2*]] ...))
(values
(make-begin
`(,@effect*
,(make-let-assigned `([,x* ,expr*] ...)
(intersection x* as*) body)))
(difference (union u (apply union u1*) (apply union u2*)) x*))]))]
[(letrec ([,x* ,[(Expr 'value) -> expr* u*]] ...) ,[body u])
(let ([bd* (map list x* expr* u*)])
(let-values ([(usbd* u^)
(let partition ([uncertain bd*] [useful '()] [used u])
(let loop ([bd* uncertain] [useful useful] [uncertain '()] [used used]
[same #t])
(if (null? bd*)
(if same
(values useful used)
(partition uncertain useful used))
(if (memq (caar bd*) used)
(loop (cdr bd*) (cons (car bd*) useful) uncertain
(union (caddar bd*) used) #f)
(loop (cdr bd*) useful (cons (car bd*) uncertain)
used same)))))])
(values
(make-letrec (map (lambda (bd) (list (car bd) (cadr bd))) usbd*)
body)
(difference u^ x*))))]
[(set! ,x ,[(Expr 'value) -> e u])
(values
`(set! ,x ,e)
(set-cons x u))]
[(lambda (,x* ...) (assigned (,as* ...) ,[(Expr 'value) -> body u]))
(if (eq? ctx 'value)
(values
`(lambda (,x* ...) (assigned (,as* ...) ,body))
(difference u x*))
(values '(void) '()))]
[(,prim ,[(Expr 'effect) -> rand* u*] ...)
(guard (primitive? prim)
(not (effect-primitive? prim))
(eq? ctx 'effect))
(values (if (null? rand*) '(void)
(make-begin rand*))
(apply union u*))]
[(,prim ,[(Expr 'value) -> rand* u*] ...) (guard (primitive? prim))
(values `(,prim ,rand* ...) (apply union u*))]
[(quote ,imm)
(if (eq? ctx 'value)
(values `(quote ,imm) '())
(values '(void) '()))]
[(,[(Expr 'value) -> proc u] ,[(Expr 'value) -> arg* u*] ...)
(values `(,proc ,arg* ...) (apply union u u*))]
[,x (guard (uvar? x))
(if (eq? ctx 'value)
(values x (list x))
(values '(void) '()))]))))
(lambda (prog)
(if *cp-1-enabled*
(match prog
[,[(Expr 'value) -> e u] e])
prog)))
(define-who convert-assignments
(define Expr
(lambda (assigned)
(lambda (e)
(match e
[(if ,[cond] ,[conseq] ,[alter]) `(if ,cond ,conseq ,alter)]
[(begin ,[expr*] ... ,[expr]) `(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[expr*]] ...) (assigned (,as* ...) ,expr))
(let* ([new-t* (map unique-name as*)]
[correspond (map cons as* new-t*)]
[subst (lambda (x) (cond [(assq x correspond) => cdr]
[else x]))]
[new-bind* (map subst uvar*)])
(make-let `([,new-bind* ,expr*] ...)
(make-let `([,as* (cons ,new-t* (void))] ...)
((Expr (append as* assigned)) expr))))]
[(letrec ([,uvar* ,[expr*]] ...) ,[expr]) `(letrec ([,uvar* ,expr*] ...) ,expr)]
[(set! ,uvar ,[expr])
(if (memq uvar assigned) `(set-car! ,uvar ,expr) `(set! ,uvar ,expr))]
[(lambda (,uvar* ...) (assigned (,as* ...) ,expr))
(let* ([new-t* (map unique-name as*)]
[correspond (map cons as* new-t*)]
[subst (lambda (x) (cond [(assq x correspond) => cdr]
[else x]))]
[new-bind* (map subst uvar*)])
`(lambda (,new-bind* ...)
,(make-let `([,as* (cons ,new-t* (void))] ...)
((Expr (append as* assigned)) expr))))]
[(,prim ,[rand*] ...) (guard (primitive? prim)) `(,prim ,rand* ...)]
[(quote ,imm) `(quote ,imm)]
[(,[proc] ,[arg*] ...) `(,proc ,arg* ...)]
[,x (guard (uvar? x)) (if (memq x assigned) `(car ,x) x)]))))
(lambda (p) ((Expr '()) p)))
(define-who remove-anonymous-lambda
(define let-rhs
(lambda (rhs)
(match rhs
[(lambda (,uvar* ...) ,[Expr -> body])
`(lambda (,uvar* ...) ,body)]
[,[Expr -> e] e])))
(define Expr
(lambda (e)
(match e
[(if ,[Expr -> cond] ,[Expr -> conseq] ,[Expr -> alter]) `(if ,cond ,conseq ,alter)]
[(begin ,[Expr -> expr*] ... ,[Expr -> expr]) `(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[let-rhs -> body*]] ...) ,[Expr -> body]) `(let ([,uvar* ,body*] ...) ,body)]
[(letrec ([,uvar* (lambda (,formal** ...) ,[Expr -> body*])] ...) ,[Expr -> body])
`(letrec ([,uvar* (lambda (,formal** ...) ,body*)] ...) ,body)]
[(lambda (,uvar* ...) ,[Expr -> body])
(let ([anon (unique-name 'anon)])
`(letrec ([,anon (lambda (,uvar* ...) ,body)])
,anon))]
[(,prim ,[Expr -> rand*] ...) (guard (primitive? prim)) `(,prim ,rand* ...)]
[(quote ,imm) `(quote ,imm)]
[(,[Expr -> proc] ,[Expr -> arg*] ...) `(,proc ,arg* ...)]
[,x x])))
Expr)
(define sanitize-binding-forms
(lambda (e)
(match e
[(if ,[cond] ,[conseq] ,[alter]) `(if ,cond ,conseq ,alter)]
[(begin ,[expr*] ... ,[expr]) `(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[body*]] ...) ,[body])
(let* ([bindings `([,uvar* ,body*] ...)])
(let-values ([(lambdas others)
(partition (lambda (x) (lambda? (cadr x))) bindings)])
(make-letrec lambdas
(make-let others body))))]
[(letrec ([,uvar* (lambda (,formal** ...) ,[body*])] ...) ,[body])
`(letrec ([,uvar* (lambda (,formal** ...) ,body*)] ...) ,body)]
[(lambda (,uvar* ...) ,[body]) `(lambda (,uvar* ...) ,body)]
[(,prim ,[rand*] ...) (guard (primitive? prim)) `(,prim ,rand* ...)]
[(quote ,imm) `(quote ,imm)]
[(,[proc] ,[arg*] ...) `(,proc ,arg* ...)]
[,x x])))
(define-who uncover-free
(define Expr
(lambda (e)
(match e
[(if ,[Expr -> cond f1] ,[Expr -> conseq f2] ,[Expr -> alter f3])
(values `(if ,cond ,conseq ,alter) (union f1 f2 f3))]
[(begin ,[Expr -> expr* f*] ... ,[Expr -> expr f])
(values `(begin ,expr* ... ,expr) (apply union f f*))]
[(let ([,uvar* ,[Expr -> body* f*]] ...) ,[Expr -> body f])
(values `(let ([,uvar* ,body*] ...) ,body) (union (difference f uvar*) (apply union f*)))]
[(letrec ([,uvar* (lambda (,formal** ...) ,[Expr -> body* f*])] ...) ,[Expr -> body f])
(let ([f*^ (map difference f* formal**)])
(values
`(letrec ([,uvar* (lambda (,formal** ...) (free ,f*^ ,body*))] ...) ,body)
(difference (apply union f f*^) uvar*)))]
[(,prim ,[Expr -> rand* f*] ...) (guard (primitive? prim))
(values `(,prim ,rand* ...) (apply union f*))]
[(quote ,imm) (values e '())]
[(,[Expr -> proc f] ,[Expr -> arg* f*] ...)
(values `(,proc ,arg* ...) (apply union f f*))]
[,x (guard (uvar? x)) (values x (list x))])))
(lambda (p)
(let-values ([(p^ fv) (Expr p)]) p^)))
(define-who convert-closures
(define Expr
(lambda (e)
(match e
[(if ,[Expr -> cond] ,[Expr -> conseq] ,[Expr -> alter])
`(if ,cond ,conseq ,alter)]
[(begin ,[Expr -> expr*] ... ,[Expr -> expr])
`(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[Expr -> body*]] ...) ,[Expr -> body])
`(let ([,uvar* ,body*] ...) ,body)]
[(letrec ([,uvar* (lambda (,formal** ...)
(free (,fv** ...) ,[Expr -> body*]))] ...)
,[Expr -> body])
(let ([cp* (map (lambda (x) (unique-name 'cp)) uvar*)]
[lab* (map unique-label uvar*)])
`(letrec ([,lab* (lambda (,cp* ,formal** ...)
(bind-free (,cp* ,fv** ...) ,body*))] ...)
(closures ([,uvar* ,lab* ,fv** ...] ...)
(well-known () ,body))))]
[(,prim ,[Expr -> rand*] ...) (guard (primitive? prim))
`(,prim ,rand* ...)]
[(quote ,imm) e]
[(,[Expr -> proc] ,[Expr -> arg*] ...)
(if (uvar? proc)
`(,proc ,proc ,arg* ...)
(let ([tmp (unique-name 'tmp)])
`(let ([,tmp ,proc])
(,tmp ,tmp ,arg* ...))))]
[,x (guard (uvar? x)) x])))
Expr)
(define-who optimize-known-call
(define Expr
(lambda (known)
(lambda (e)
(match e
[(if ,[(Expr known) -> cond] ,[(Expr known) -> conseq] ,[(Expr known) -> alter])
`(if ,cond ,conseq ,alter)]
[(begin ,[(Expr known) -> expr*] ... ,[(Expr known) -> expr])
`(begin ,expr* ... ,expr)]
[(let ([,uvar* ,[(Expr known) -> body*]] ...) ,[(Expr known) -> body])
`(let ([,uvar* ,body*] ...) ,body)]
[(letrec ([,lab* (lambda (,formal** ...)
(bind-free (,cp* ,fv** ...) ,body*))] ...)
(closures ([,uvar* ,data** ...] ...)
(well-known () ,body)))
(let* ([known^ (append uvar* known)]
[body*^ (map (Expr known^) body*)])
`(letrec ([,lab* (lambda (,formal** ...)
(bind-free (,cp* ,fv** ...) ,body*^))] ...)
(closures ([,uvar* ,data** ...] ...)
(well-known () ,((Expr known^) body)))))]
[(,prim ,[(Expr known) -> rand*] ...) (guard (primitive? prim))
`(,prim ,rand* ...)]
[(quote ,imm) e]
[(,proc ,[(Expr known) -> arg*] ...)
(if (memq proc known)
`(,(unique-label proc) ,arg* ...)
`(,proc ,arg* ...))]
[,x (guard (uvar? x)) x]))))
(lambda (p)
(if *closure-optimization-enabled*
((Expr '()) p)
p)))
(define-who uncover-well-known
(define Expr
(lambda (e)
(match e
[(if ,[cond u1] ,[conseq u2] ,[alter u3])