-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathc.rkt
3574 lines (3462 loc) · 144 KB
/
c.rkt
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
#lang nanopass
;;; Copyright (c) 2013 Andrew W. Keep
;;; See the accompanying file Copyright for details
;;;
;;; A nanopass compiler developed to use as a demo during Clojure Conj 2013.
;;; The source language for the compiler is:
;;;
;;; Expr --> <Primitive>
;;; | <Var>
;;; | <Const>
;;; | (quote <Datum>)
;;; | (if <Expr> <Expr>)
;;; | (if <Expr> <Expr> <Expr>)
;;; | (or <Expr> ...)
;;; | (and <Expr> ...)
;;; | (not <Expr>)
;;; | (begin <Expr> ... <Expr>)
;;; | (lambda (<Var> ...) <Expr> ... <Expr>)
;;; | (let ([<Var> <Expr>] ...) <Expr> ... <Expr>)
;;; | (letrec ([<Var> <Expr>] ...) <Expr> ... <Expr>)
;;; | (set! <Var> <Expr>)
;;; | (<Expr> <Expr> ...)
;;;
;;; Primitive --> car | cdr | cons | pair? | null? | boolean? | make-vector
;;; | vector-ref | vector-set! | vector? | vector-length | box
;;; | unbox | set-box! | box? | + | - | * | / | = | < | <= | >
;;; | >= | eq?
;;; Var --> symbol
;;; Const --> #t | #f | '() | integer between -2^60 and 2^60 - 1
;;; Datum --> <Const> | (<Datum> . <Datum>) | #(<Datum> ...)
;;;
;;; or in nanopass parlance:
;;; (define-language Lsrc
;;; (terminals
;;; (symbol (x))
;;; (primitive (pr))
;;; (constant (c))
;;; (datum (d)))
;;; (Expr (e body)
;;; pr
;;; x
;;; c
;;; (quote d)
;;; (if e0 e1)
;;; (if e0 e1 e2)
;;; (or e* ...)
;;; (and e* ...)
;;; (not e)
;;; (begin e* ... e)
;;; (lambda (x* ...) body* ... body)
;;; (let ([x* e*] ...) body* ... body)
;;; (letrec ([x* e*] ...) body* ... body)
;;; (set! x e)
;;; (e e* ...)))
;;;
;;; The following exports are defined for this library:
;;;
;;; (my-tiny-compile <exp>)
;;; my-tiny-compile is the main interface the compiler, where <exp> is
;;; a quoted expression for the compiler to evaluate. This procedure will
;;; run the nanopass parts of the compiler, produce a C output file in t.c,
;;; compile it using gcc to a program t, run the program t, directing its
;;; output to t.out, and finally use the Scheme reader to read t.out and
;;; return the value to the host Scheme system. For example, if we wanted
;;; to run a program that calculates the factorial of 5, we could do the
;;; following:
;;; (my-tiny-compile '(letrec ([f (lambda (n)
;;; (if (= n 0)
;;; 1
;;; (* n (f (- n 1)))))])
;;; (f 10)))
;;;
;;; (trace-passes)
;;; (trace-passes <pass-spec>)
;;; trace-passes is a parameter used by my-tiny-compile to decide what
;;; passees should have their output printed. When it is called without
;;; any arguments, it returns the list of passes to be traced. When it
;;; is called with an argument, the argument should be one of the
;;; following:
;;; '<pass-name> - sets this pass to be traced
;;; '(<pass-name 0> <pass-name 1> ...) - set the list of passes to trace
;;; #t - traces all passes
;;; #f - turns off trace passing
;;;
;;; all-passes
;;; lists all passes in the compiler.
;;;
;;; (use-boehm?)
;;; (use-boehm? <boolean>)
;;; use-boehm? is a parameter that indicates if the generated C code should
;;; attempt to use the boehm garbage collector. This feature is, as of
;;; yet, untested.
;;;
;;; Internals that are exported to make them available for programmers
;;; experimenting with the compiler.
;;;
;;; TBD
;;;
;;;
(provide Lsrc unparse-Lsrc
L1 unparse-L1
L2 unparse-L2
L3 unparse-L3
L4 unparse-L4
L5 unparse-L5
L6 unparse-L6
L7 unparse-L7
L8 unparse-L8
L9 unparse-L9
L10 unparse-L10
L11 unparse-L11
L12 unparse-L12
L13 unparse-L13
L14 unparse-L14
L15 unparse-L15
L16 unparse-L16
L17 unparse-L17
L18 unparse-L18
L19 unparse-L19
; L20 unparse-L20
L21 unparse-L21
L22 unparse-L22
unique-var
user-alloc-value-prims
user-non-alloc-value-prims
user-pred-prims
user-effect-prims
user-prims
void+user-non-alloc-value-prims
void+user-prims
closure+user-alloc-value-prims
closure+void+user-non-alloc-value-prims
closure+user-effect-prims
internal+closure+user-effect-prims
closure+void+user-prims
primitive?
void+primitive?
closure+void+primitive?
effect-free-prim?
predicate-primitive?
effect-primitive?
value-primitive?
non-alloc-value-primitive?
effect+internal-primitive?
target-fixnum?
constant?
datum?
integer-64?
set-cons
union
difference
intersect
parse-and-rename
remove-one-armed-if
remove-and-or-not
make-begin-explicit
inverse-eta-raw-primitives
quote-constants
remove-complex-constants
identify-assigned-variables
purify-letrec
optimize-direct-call
find-let-bound-lambdas
remove-anonymous-lambda
convert-assignments
uncover-free
convert-closures
optimize-known-call
expose-closure-prims
lift-lambdas
remove-complex-opera*
recognize-context
expose-allocation-primitives
return-of-set!
flatten-set!
; push-if
specify-constant-representation
expand-primitives
generate-c
use-boehm?
my-tiny-compile
trace-passes
all-passes)
(require racket/fixnum
racket/pretty
racket/system
(for-syntax syntax/parse
syntax/stx
nanopass/base
racket/syntax))
;;; As of yet untested feature for using the boehm GC
;;; in the compiled output of our compiler.
(define use-boehm?
(let ([use? #f])
(case-lambda
[() use?]
[(u?) (set! use? u?)])))
;;; Representation of our data types.
;;; We use tagged pointers, because all of our pointers are 8-byte aligned,
;;; leaving te bottom 3 bits always being 0. Using these 3 bits for tags
;;; lets us store things like fixnums as pointers, and differentiate them
;;; from pointers like closures and vectors. It also saves us using a word
;;; for a tag when in our representation of vectors, closures, etc.
(define fixnum-tag #b000)
(define fixnum-mask #b111)
(define pair-tag #b001)
(define pair-mask #b111)
(define box-tag #b010)
(define box-mask #b111)
(define vector-tag #b011)
(define vector-mask #b111)
(define closure-tag #b100)
(define closure-mask #b111)
;;; NOTE: #b101 is used for constants
(define boolean-tag #b1101)
(define boolean-mask #b1111)
(define true-rep #b111101)
(define false-rep #b101101)
(define null-rep #b100101)
(define void-rep #b110101)
(define fixnum-shift 3)
(define word-size 8)
;;; Helper function for representing unique variables as symbols by adding a
;;; number to the variables (so if we start with f we get f.n where n might
;;; be 1, 2, 3, etc, and is unique).
(define unique-var
(let ()
(define count 0)
(lambda (name)
(let ([c count])
(set! count (+ count 1))
(string->symbol
(string-append (symbol->string name) "." (number->string c)))))))
;; strip the numberic bit back off the unique-var
(define base-var
(lambda (x)
(define s0
(lambda (rls)
(if (null? rls)
(error 'base-var "not a unique-var created variable ~a" x)
(let ([c (car rls)])
(cond
[(char-numeric? c) (s1 (cdr rls))]
[else (error 'base-var
"not a unique-var created variable ~a" x)])))))
(define s1
(lambda (rls)
(if (null? rls)
(error 'base-var "not a unique-var created variable ~a" x)
(let ([c (car rls)])
(cond
[(char-numeric? c) (s1 (cdr rls))]
[(char=? c #\.) (cdr rls)]
[else (error 'base-var
"not a unique-var created variable ~a" x)])))))
(string->symbol
(list->string
(reverse
(s0 (reverse (string->list (symbol->string x)))))))))
;;; Convenience procedure for building temporaries in the compiler.
(define make-tmp (lambda () (unique-var 't)))
;;; Helpers for the various sets of primitives we have over the course of the
;;; compiler
;;; All primitives:
;;;
;;; | | | Langauge | Language |
;;; primitive | arity | context | introduced | removed |
;;; --------------------+-------+---------+------------+----------+
;;; cons | 2 | value | Lsrc | L17 |
;;; make-vector | 1 | value | Lsrc | L17 |
;;; box | 1 | value | Lsrc | L17 |
;;; car | 1 | value | Lsrc | L22 |
;;; cdr | 1 | value | Lsrc | L22 |
;;; vector-ref | 2 | value | Lsrc | L22 |
;;; vector-length | 1 | value | Lsrc | L22 |
;;; unbox | 1 | value | Lsrc | L22 |
;;; + | 2 | value | Lsrc | L22 |
;;; - | 2 | value | Lsrc | L22 |
;;; * | 2 | value | Lsrc | L22 |
;;; / | 2 | value | Lsrc | L22 |
;;; pair? | 1 | pred | Lsrc | L22 |
;;; null? | 1 | pred | Lsrc | L22 |
;;; boolean? | 1 | pred | Lsrc | L22 |
;;; vector? | 1 | pred | Lsrc | L22 |
;;; box? | 1 | pred | Lsrc | L22 |
;;; = | 2 | pred | Lsrc | L22 |
;;; < | 2 | pred | Lsrc | L22 |
;;; <= | 2 | pred | Lsrc | L22 |
;;; > | 2 | pred | Lsrc | L22 |
;;; >= | 2 | pred | Lsrc | L22 |
;;; eq? | 2 | pred | Lsrc | L22 |
;;; vector-set! | 3 | effect | Lsrc | L22 |
;;; set-box! | 2 | effect | Lsrc | L22 |
;;; --------------------+-------+---------+------------+----------+
;;; void | 0 | value | L1 | L22 |
;;; --------------------+-------+---------+------------+----------+
;;; make-closure | 1 | value | L13 | L17 |
;;; closure-code | 2 | value | L13 | L22 |
;;; closure-ref | 2 | value | L13 | L22 |
;;; closure-code-set! | 2 | effect | L13 | L22 |
;;; closure-data-set! | 3 | effect | L13 | L22 |
;;; --------------------+-------+---------+------------+----------+
;;; $vector-length-set! | 2 | effect | L17 | L22 |
;;; $set-car! | 2 | effect | L17 | L22 |
;;; $set-cdr! | 2 | effect | L17 | L22 |
;;;
;;; This is a slightly cleaned up version, but this might still be better
;;; cleaned up by adding a define-primitives form, perhaps even one that can
;;; be used in the later parts of the compiler.
;;; user value primitives that perform allocation
(define user-alloc-value-prims
'((cons . 2) (make-vector . 1) (box . 1)))
;;; user value primitives that do not perform allocation
(define user-non-alloc-value-prims
'((car . 1) (cdr . 1) (vector-ref . 2) (vector-length . 1) (unbox . 1)
(+ . 2) (- . 2) (* . 2) (/ . 2)))
;;; user predicate primitives
;;; TODO: add procedure?
(define user-pred-prims
'((pair? . 1) (null? . 1) (boolean? . 1) (vector? . 1) (box? . 1) (= . 2)
(< . 2) (<= . 2) (> . 2) (>= . 2) (eq? . 2)))
;;; user effect primitives
(define user-effect-prims
'((vector-set! . 3) (set-box! . 2)))
;;; an association list with the user primitives
(define user-prims
(append user-alloc-value-prims user-non-alloc-value-prims user-pred-prims
user-effect-prims))
;;; void primitive + non-allocation user value primitives
(define void+user-non-alloc-value-prims
(cons '(void . 0) user-non-alloc-value-prims))
;;; an association list with void and all the user primitives
(define void+user-prims
(append user-alloc-value-prims void+user-non-alloc-value-prims
user-pred-prims user-effect-prims))
;;; all the allocation value primitives, including make-closure primitive
(define closure+user-alloc-value-prims
(cons '(make-closure . 1) user-alloc-value-prims))
;;; all the non-allocation value primitives, include the closure primitives
(define closure+void+user-non-alloc-value-prims
(list* '(closure-code . 2) '(closure-ref . 2)
void+user-non-alloc-value-prims))
;; all the user effect primitives with the closure primitives
(define closure+user-effect-prims
(list* '(closure-code-set! . 2) '(closure-data-set! . 3)
user-effect-prims))
;; all the user effect primitives, closure primitives, and internal primitives
(define internal+closure+user-effect-prims
(list* '($vector-length-set! . 2) '($set-car! . 2) '($set-cdr! . 2)
closure+user-effect-prims))
;; association list including all prims except the three final internal
;; primitives
(define closure+void+user-prims
(append closure+user-alloc-value-prims
closure+void+user-non-alloc-value-prims user-pred-prims
closure+user-effect-prims))
;;; various predicates for determining if a primitve is a valid prim.
(define primitive?
(lambda (x)
(assq x user-prims)))
(define void+primitive?
(lambda (x)
(assq x void+user-prims)))
(define closure+void+primitive?
(lambda (x)
(assq x closure+void+user-prims)))
(define effect-free-prim?
(lambda (x)
(assq x (append void+user-non-alloc-value-prims user-alloc-value-prims
user-pred-prims))))
(define predicate-primitive?
(lambda (x)
(assq x user-pred-prims)))
(define effect-primitive?
(lambda (x)
(assq x closure+user-effect-prims)))
(define value-primitive?
(lambda (x)
(assq x (append closure+user-alloc-value-prims
closure+void+user-non-alloc-value-prims))))
(define non-alloc-value-primitive?
(lambda (x)
(assq x closure+void+user-non-alloc-value-prims)))
(define effect+internal-primitive?
(lambda (x)
(assq x internal+closure+user-effect-prims)))
;;;;;;;;;;
;;; Helper functions for identifying terminals in the nanopass languages.
;;; determine if we have a 61-bit signed integer
(define target-fixnum?
(lambda (x)
(and (and (integer? x) (exact? x))
(<= (- (expt 2 60)) x (- (expt 2 60) 1)))))
;;; determine if we have a constant: #t, #f, '(), or 61-bit signed integer.
(define constant?
(lambda (x)
(or (target-fixnum? x) (boolean? x) (null? x))))
;;; determine if we have a valid datum (a constant, a pair of datum, or a
;;; vector of datum)
(define datum?
(lambda (x)
(or (constant? x)
(and (pair? x) (datum? (car x)) (datum? (cdr x)))
(and (vector? x)
(let loop ([i (vector-length x)])
(or (fx= i 0)
(let ([i (fx- i 1)])
(and (datum? (vector-ref x i))
(loop i)))))))))
;;; determine if we have a 64-bit signed integer (used later in the compiler
;;; to hold the ptr representation).
(define integer-64?
(lambda (x)
(and (and (integer? x) (exact? x))
(<= (- (expt 2 63)) x (- (expt 2 63) 1)))))
;;; Random helper available on most Scheme systems, but irritatingly not in
;;; the R6RS standard.
(define make-list
(case-lambda
[(n) (make-list n (void))]
[(n v) (let loop ([n n] [ls '()])
(if (zero? n)
ls
(loop (fx- n 1) (cons v ls))))]))
;;;;;;;;
;;; The standard (not very efficient) Scheme representation of sets as lists
;;; add an item to a set
(define set-cons
(lambda (x set)
(if (memq x set)
set
(cons x set))))
;;; construct the intersection of 0 to n sets
(define intersect
(lambda set*
(if (null? set*)
'()
(foldl (lambda (setb seta)
(let loop ([seta seta] [fset '()])
(if (null? seta)
fset
(let ([a (car seta)])
(if (memq a setb)
(loop (cdr seta) (cons a fset))
(loop (cdr seta) fset))))))
(car set*) (cdr set*)))))
;;; construct the union of 0 to n sets
(define union
(lambda set*
(if (null? set*)
'()
(foldl (lambda (setb seta)
(let loop ([setb setb] [seta seta])
(if (null? setb)
seta
(loop (cdr setb) (set-cons (car setb) seta)))))
(car set*) (cdr set*)))))
;;; construct the difference of 0 to n sets
(define difference
(lambda set*
(if (null? set*)
'()
(foldr (lambda (setb seta)
(let loop ([seta seta] [final '()])
(if (null? seta)
final
(let ([a (car seta)])
(if (memq a setb)
(loop (cdr seta) final)
(loop (cdr seta) (cons a final)))))))
(car set*) (cdr set*)))))
;;; Language definitions for Lsrc and L1 to L22
;;; Both the language extension and the fully specified language is included
;;; (though the fully specified language may be out of date). This can be
;;; regenerated by doing:
;;; > (import (c))
;;; > (import (nanopass))
;;; > (language->s-expression L10) => generates L10 definition
(define-language Lsrc
(terminals
(symbol (x))
(primitive (pr))
(constant (c))
(datum (d)))
(Expr (e body)
pr
x
c
(quote d)
(if e0 e1)
(if e0 e1 e2)
(or e* ...)
(and e* ...)
(not e)
(begin e* ... e)
(lambda (x* ...) body* ... body)
(let ([x* e*] ...) body* ... body)
(letrec ([x* e*] ...) body* ... body)
(set! x e)
(e e* ...)))
;;; Language 1: removes one-armed if and adds the void primitive
;
; (define-language L1
; (terminals (void+primitive (pr))
; (symbol (x))
; (constant (c))
; (datum (d)))
; (Expr (e body)
; pr
; x
; c
; (quote d)
; (if e0 e1 e2)
; (or e* ...)
; (and e* ...)
; (not e)
; (begin e* ... e)
; (lambda (x* ...) body* ... body)
; (let ([x* e*] ...) body* ... body)
; (letrec ([x* e*] ...) body* ... body)
; (set! x e)
; (e e* ...)))
;
(define-language L1
(extends Lsrc)
(terminals
(- (primitive (pr)))
(+ (void+primitive (pr))))
(Expr (e body)
(- (if e0 e1))))
;;; Language 2: removes or, and, and not forms
;
; (define-language L2
; (terminals (void+primitive (pr))
; (symbol (x))
; (constant (c))
; (datum (d)))
; (Expr (e body)
; pr
; x
; c
; (quote d)
; (if e0 e1 e2)
; (begin e* ... e)
; (lambda (x* ...) body* ... body)
; (let ([x* e*] ...) body* ... body)
; (letrec ([x* e*] ...) body* ... body)
; (set! x e)
; (e e* ...)))
;
(define-language L2
(extends L1)
(Expr (e body)
(- (or e* ...)
(and e* ...)
(not e))))
;;; Language 3: removes multiple expressions from the body of lambda, let,
;;; and letrec (to be replaced with a single begin expression that contains
;;; the expressions from the body).
;
; (define-language L3
; (terminals (void+primitive (pr))
; (symbol (x))
; (constant (c))
; (datum (d)))
; (Expr (e body)
; (letrec ([x* e*] ...) body)
; (let ([x* e*] ...) body)
; (lambda (x* ...) body)
; pr
; x
; c
; (quote d)
; (if e0 e1 e2)
; (begin e* ... e)
; (set! x e)
; (e e* ...)))
;
(define-language L3
(extends L2)
(Expr (e body)
(- (lambda (x* ...) body* ... body)
(let ([x* e*] ...) body* ... body)
(letrec ([x* e*] ...) body* ... body))
(+ (lambda (x* ...) body)
(let ([x* e*] ...) body)
(letrec ([x* e*] ...) body))))
;;; Language 4: removes raw primitives (to be replaced with a lambda and a
;;; primitive call).
;
; (define-language L4
; (terminals (void+primitive (pr))
; (symbol (x))
; (constant (c))
; (datum (d)))
; (Expr (e body)
; (primcall pr e* ...)
; (letrec ([x* e*] ...) body)
; (let ([x* e*] ...) body)
; (lambda (x* ...) body)
; x
; c
; (quote d)
; (if e0 e1 e2)
; (begin e* ... e)
; (set! x e)
; (e e* ...)))
;
(define-language L4
(extends L3)
(Expr (e body)
(- pr)
(+ (primcall pr e* ...) => (pr e* ...))))
;;; Language 5: removes raw constants (to be replaced with quoted constant).
;
; (define-language L5
; (terminals
; (void+primitive (pr))
; (symbol (x))
; (datum (d)))
; (Expr (e body)
; (primcall pr e* ...)
; (letrec ([x* e*] ...) body)
; (let ([x* e*] ...) body)
; (lambda (x* ...) body)
; x
; (quote d)
; (if e0 e1 e2)
; (begin e* ... e)
; (set! x e)
; (e e* ...)))
;
(define-language L5
(extends L4)
(terminals
(- (constant (c))))
(Expr (e body)
(- c)))
;;; Language 6: removes quoted datum (to be replaced with explicit calls to
;;; cons and make-vector+vector-set!).
;
; (define-language L6
; (terminals
; (constant (c))
; (void+primitive (pr))
; (symbol (x)))
; (Expr (e body)
; (quote c)
; (primcall pr e* ...)
; (letrec ([x* e*] ...) body)
; (let ([x* e*] ...) body)
; (lambda (x* ...) body)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (set! x e)
; (e e* ...)))
;
(define-language L6
(extends L5)
(terminals
(- (datum (d)))
(+ (constant (c))))
(Expr (e body)
(- (quote d))
(+ (quote c))))
;;; Language 7: adds a listing of assigned variables to the body of the
;;; binding forms: let, letrec, and lambda.
; (define-language L7
; (terminals
; (symbol (x a))
; (constant (c))
; (void+primitive (pr)))
; (Expr (e body)
; (letrec ([x* e*] ...) abody)
; (let ([x* e*] ...) abody)
; (lambda (x* ...) abody)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (set! x e)
; (e e* ...))
; (AssignedBody (abody)
; (assigned (a* ...) body)))
;
(define-language L7
(extends L6)
(terminals
(- (symbol (x)))
(+ (symbol (x a))))
(Expr (e body)
(- (lambda (x* ...) body)
(let ([x* e*] ...) body)
(letrec ([x* e*] ...) body))
(+ (lambda (x* ...) abody)
(let ([x* e*] ...) abody)
(letrec ([x* e*] ...) abody)))
(AssignedBody (abody)
(+ (assigned (a* ...) body))))
;;; Language 8: letrec binding is changed to only bind variables to lambdas.
;
; (define-language L8
; (terminals (symbol (x a))
; (constant (c))
; (void+primitive (pr)))
; (Expr (e body)
; (letrec ([x* le*] ...) body)
; le
; (let ([x* e*] ...) abody)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (set! x e)
; (e e* ...))
; (AssignedBody (abody)
; (assigned (a* ...) body))
; (LambdaExpr (le)
; (lambda (x* ...) abody)))
;
(define-language L8
(extends L7)
(Expr (e body)
(- (lambda (x* ...) abody)
(letrec ([x* e*] ...) abody))
(+ le
(letrec ([x* le*] ...) body)))
(LambdaExpr (le)
(+ (lambda (x* ...) abody))))
;;; Language 9: removes lambda expressions from expression context,
;;; effectively meaning we can only have lambdas bound in the right-hand-side
;;; of letrec expressions.
;
; (define-language L9
; (terminals
; (symbol (x a))
; (constant (c))
; (void+primitive (pr)))
; (Expr (e body)
; (letrec ([x* le*] ...) body)
; (let ([x* e*] ...) abody)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (set! x e)
; (e e* ...))
; (AssignedBody (abody)
; (assigned (a* ...) body))
; (LambdaExpr (le)
; (lambda (x* ...) abody)))
;
(define-language L9
(extends L8)
(Expr (e body)
(- le)))
;;; Language 10: removes set! and assigned bodies (to be replaced by set-box!
;;; primcall for set!, and unbox primcall for references of assigned variables).
;
; (define-language L10
; (terminals
; (symbol (x))
; (constant (c))
; (void+primitive (pr)))
; (Expr (e body)
; (let ([x* e*] ...) body)
; (letrec ([x* le*] ...) body)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (e e* ...))
; (LambdaExpr (le)
; (lambda (x* ...) body)))
;
(define-language L10
(extends L9)
(terminals
(- (symbol (x a)))
(+ (symbol (x))))
(Expr (e body)
(- (let ([x* e*] ...) abody)
(set! x e))
(+ (let ([x* e*] ...) body)))
(LambdaExpr (le)
(- (lambda (x* ...) abody))
(+ (lambda (x* ...) body)))
(AssignedBody (abody)
(- (assigned (a* ...) body))))
;;; Language 11: add a list of free variables to the body of lambda
;;; expressions (starting closure conversion code).
;
; (define-language L11
; (terminals
; (symbol (x f))
; (constant (c))
; (void+primitive (pr)))
; (Expr (e body)
; (let ([x* e*] ...) body)
; (letrec ([x* le*] ...) body)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (e e* ...))
; (LambdaExpr (le)
; (lambda (x* ...) fbody))
; (FreeBody (fbody)
; (free (f* ...) body)))
;
(define-language L11
(extends L10)
(terminals
(- (symbol (x)))
(+ (symbol (x f))))
(LambdaExpr (le)
(- (lambda (x* ...) body))
(+ (lambda (x* ...) fbody)))
(FreeBody (fbody)
(+ (free (f* ...) body))))
;;; Language L12: removes the letrec form and adds closure and labels forms
;;; to replace it. The closure form binds a variable to a label (code
;;; pointer) and its set of free variables, and the labels form binds labels
;;; (code pointer) to lambda expressions.
;
; (define-language L12
; (terminals
; (symbol (x f l))
; (constant (c))
; (void+primitive (pr)))
; (Expr (e body)
; (label l)
; (closures ((x* l* f** ...) ...) lbody)
; (let ([x* e*] ...) body)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (e e* ...))
; (LambdaExpr (le)
; (lambda (x* ...) fbody))
; (FreeBody (fbody)
; (free (f* ...) body))
; (LabelsBody (lbody)
; (labels ([l* le*] ...) body)))
;
(define-language L12
(extends L11)
(terminals
(- (symbol (x f)))
(+ (symbol (x f l))))
(Expr (e body)
(- (letrec ([x* le*] ...) body))
(+ (closures ([x* l* f** ...] ...) lbody)
(label l)))
(LabelsBody (lbody)
(+ (labels ([l* le*] ...) body))))
;;; Language 13: finishes closure conversion, removes the closures form,
;;; replacing it with primitive calls to deal with closure objects, and
;;; raises the labels from into the Expr non-terminal.
;
; (define-language L13
; (terminals
; (closure+void+primitive (pr))
; (symbol (x f l))
; (constant (c)))
; (Expr (e body)
; (labels ([l* le*] ...) body)
; (label l)
; (let ([x* e*] ...) body)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (e e* ...))
; (LambdaExpr (le)
; (lambda (x* ...) body)))
;
(define-language L13
(extends L12)
(terminals
(- (void+primitive (pr)))
(+ (closure+void+primitive (pr))))
(Expr (e body)
(- (closures ([x* l* f** ...] ...) lbody))
(+ (labels ([l* le*] ...) body)))
(LabelsBody (lbody)
(- (labels ([l* le*] ...) body)))
(LambdaExpr (le)
(- (lambda (x* ...) fbody))
(+ (lambda (x* ...) body)))
(FreeBody (fbody)
(- (free (f* ...) body))))
;;; Language 14: removes labels form from the Expr nonterminal and puts a
;;; single labels form at the top. Essentially this raises all of our
;;; closure converted functions to the top.
;
; (define-language L14
; (entry Program)
; (terminals
; (closure+void+primitive (pr))
; (symbol (x f l))
; (constant (c)))
; (Expr (e body)
; (label l)
; (let ([x* e*] ...) body)
; (quote c)
; (primcall pr e* ...)
; x
; (if e0 e1 e2)
; (begin e* ... e)
; (e e* ...))
; (LambdaExpr (le)
; (lambda (x* ...) body))
; (Program (p)
; (labels ([l* le*] ...) l)))
;
(define-language L14