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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
|
(use-modules (ice-9 format)
(ice-9 popen)
(ice-9 rdelim)
(srfi srfi-1)
(srfi srfi-11))
;;;
;;; Syntax predicates and accessors
;;;
(define (immediate-rep x)
(cond
((integer? x)
(ash x fixnum-shift))
((char? x)
(logior (ash (char->integer x) char-shift) char-tag))
((boolean? x)
(logior (ash (if x 1 0) boolean-shift) boolean-tag))
((null? x)
empty-list)))
(define (immediate? x)
(or (integer? x)
(char? x)
(boolean? x)
(null? x)))
(define (variable? x)
(symbol? x))
(define (op-eq? lst op)
(and (pair? lst)
(eq? (first lst) op)))
(define (if? x)
(op-eq? x 'if))
(define (cond? x)
(op-eq? x 'cond))
(define (case? x)
(op-eq? x 'case))
(define (when? x)
(op-eq? x 'when))
(define (unless? x)
(op-eq? x 'unless))
(define (or? x)
(op-eq? x 'or))
(define (and? x)
(op-eq? x 'and))
(define (define? x)
(op-eq? x 'define))
(define (let? x)
(op-eq? x 'let))
(define (let*? x)
(op-eq? x 'let*))
(define (letrec? x)
(op-eq? x 'letrec))
(define (letrec*? x)
(op-eq? x 'letrec*))
(define (primcall? x)
(and (pair? x)
(memq (first x)
'(add1 sub1
integer->char char->integer
zero? null? integer? char? boolean?
+ - *
= < <= > >=
eq? char=?
cons car cdr
make-vector vector vector-length vector-ref vector-set!
make-string string string-length string-ref string-set!
constant-init constant-ref))))
(define (tagged-primcall? x)
(op-eq? x 'primcall))
(define (labels? x)
(op-eq? x 'labels))
(define (code? x)
(op-eq? x 'code))
(define (datum? x)
(op-eq? x 'datum))
(define (closure? x)
(op-eq? x 'closure))
(define (funcall? x)
(op-eq? x 'funcall))
(define (tail-call? x)
(op-eq? x 'tail-call))
(define (lambda? x)
(op-eq? x 'lambda))
(define (quote? x)
(op-eq? x 'quote))
(define (set? x)
(op-eq? x 'set!))
(define (primcall-op x)
(first x))
(define (primcall-operands x)
(cdr x))
(define (tagged-primcall-op x)
(second x))
(define (tagged-primcall-operands x)
(cddr x))
(define (tagged-primcall-operand1 x)
(third x))
(define (tagged-primcall-operand2 x)
(fourth x))
(define (tagged-primcall-operand3 x)
(fifth x))
(define (lookup name env)
(or (assq-ref env name)
(error "unbound variable:" name env)))
(define (extend-env name si-or-label env)
(cons (cons name si-or-label) env))
(define (let-bindings x)
(second x))
(define (let-body x)
(cddr x))
(define (label-bindings x)
(second x))
(define (label-body x)
(cddr x))
(define (lhs b)
(first b))
(define (rhs b)
(second b))
(define (test x)
(second x))
(define (consequent x)
(third x))
(define (alternate x)
(fourth x))
(define (closure-lvar x)
(second x))
(define (closure-vars x)
(cddr x))
(define (lambda-args x)
(second x))
(define (lambda-body x)
(cddr x))
(define (funcall-proc x)
(second x))
(define (funcall-arguments x)
(drop x 2))
(define (tail-call-proc x)
(second x))
(define (tail-call-arguments x)
(drop x 2))
(define (code-vars x)
(second x))
(define (code-free-vars x)
(third x))
(define (code-body x)
(fourth x))
(define (quote-data x)
(second x))
(define unique-counter (make-parameter 0))
(define (unique-number)
(let ((n (unique-counter)))
(unique-counter (+ n 1))
n))
(define (unique-lvar)
(string->symbol
(format #f "f~a" (unique-number))))
(define (unique-variable)
(string->symbol
(format #f "t~a" (unique-number))))
;;;
;;; Native code compilation
;;;
;; Assuming a 64 bit intel machine here.
(define wordsize 8)
(define fixnum-mask 3)
(define fixnum-shift 2)
(define fixnum-tag 0)
(define char-mask 255)
(define char-shift 8)
(define char-tag 15)
(define boolean-mask 127)
(define boolean-shift 7)
(define boolean-tag 31)
(define empty-list 47) ;; #b00101111
;; 3 bit tags for heap allocated values.
(define pair-tag 1)
(define vector-tag 2)
(define string-tag 3)
(define closure-tag 6)
(define (unique-label)
(format #f "L~a" (unique-number)))
(define (register name)
(format #f "%~a" name))
(define (immediate value)
(format #f "$~a" value))
(define rax (register "rax")) ; default register for storing values
(define rbx (register "rbx")) ; extra storage
(define rdi (register "rdi")) ; closure pointers
(define rsp (register "rsp")) ; heap
(define rsi (register "rsi")) ; stack
(define rip (register "rip")) ; instruction pointer
(define al (register "al"))
(define (offset n register)
(format #f "~a(~a)" n register))
(define (register-offset base-register index-register)
(format #f "(~a, ~a)" base-register index-register))
(define (emit template-string . args)
(display " ")
(apply format #t template-string args)
(newline))
(define (emit-mov src dest)
(emit "mov ~a, ~a" src dest))
(define (emit-movb src dest)
(emit "movb ~a, ~a" src dest))
(define (emit-and mask dest)
(emit "and ~a, ~a" mask dest))
(define (emit-andq mask dest)
(emit "andq ~a, ~a" mask dest))
(define (emit-or addend dest)
(emit "or ~a, ~a" addend dest))
(define (emit-add addend dest)
(emit "add ~a, ~a" addend dest))
(define (emit-sub subtrahend dest)
(emit "sub ~a, ~a" subtrahend dest))
(define (emit-imul multiplicand dest)
(emit "imul ~a, ~a" multiplicand dest))
(define (emit-sal n dest)
(emit "sal ~a, ~a" n dest))
(define (emit-shr n dest)
(emit "shr ~a, ~a" n dest))
(define (emit-cmp a b)
(emit "cmp ~a, ~a" a b))
(define (emit-setl dest)
(emit "setl ~a" dest))
(define (emit-setle dest)
(emit "setle ~a" dest))
(define (emit-setg dest)
(emit "setg ~a" dest))
(define (emit-setge dest)
(emit "setge ~a" dest))
(define (emit-sete dest)
(emit "sete ~a" dest))
(define (emit-je label)
(emit "je ~a" label))
(define (emit-jmp label)
(emit "jmp ~a" label))
(define (emit-call label)
(emit "call ~a" label))
(define (emit-lea src dest)
(emit "lea ~a, ~a" src dest))
(define (emit-ret)
(emit "ret"))
(define (emit-label label)
(format #t "~a:\n" label))
(define (emit-tag-check x mask tag si env)
(emit-expr x si env)
(emit-and (immediate mask) rax)
(emit-cmp (immediate tag) rax)
(emit-mov (immediate 0) rax)
(emit-sete al)
(emit-sal (immediate boolean-shift) rax)
(emit-or (immediate boolean-tag) rax))
(define (emit-comparison x y instruction si env)
(emit-expr y si env)
(emit-mov rax (offset si rsp))
(emit-expr x (- si wordsize) env)
(emit-cmp (offset si rsp) rax)
(emit-mov (immediate 0) rax)
(instruction al)
(emit-sal (immediate boolean-shift) rax)
(emit-or (immediate boolean-tag) rax))
(define (emit-primitive-call x si env)
(case (tagged-primcall-op x)
((add1)
(emit-expr (tagged-primcall-operand1 x) si env)
(emit-add (immediate (immediate-rep 1)) rax))
((sub1)
(emit-expr (tagged-primcall-operand1 x) si env)
(emit-sub (immediate (immediate-rep 1)) rax))
((integer->char)
(emit-expr (tagged-primcall-operand1 x) si env)
(emit-sal (immediate (- char-shift fixnum-shift)) rax)
(emit-or (immediate char-tag) rax))
((char->integer)
(emit-expr (tagged-primcall-operand1 x) si env)
(emit-shr (immediate (- char-shift fixnum-shift)) rax))
((zero?)
(emit-expr (tagged-primcall-operand1 x) si env)
;; Since the tag of fixnums is 0, we can skip an 'andl'
;; instruction that would apply the mask to the immediate
;; value.
(emit-cmp (immediate 0) rax)
(emit-mov (immediate 0) rax)
(emit-sete al)
(emit-sal (immediate boolean-shift) rax)
(emit-or (immediate boolean-tag) rax))
((null?)
(emit-expr (tagged-primcall-operand1 x) si env)
(emit-cmp (immediate empty-list) rax)
(emit-mov (immediate 0) rax)
(emit-sete al)
(emit-sal (immediate boolean-shift) rax)
(emit-or (immediate boolean-tag) rax))
((integer?)
(emit-tag-check (tagged-primcall-operand1 x) fixnum-mask fixnum-tag si env))
((char?)
(emit-tag-check (tagged-primcall-operand1 x) char-mask char-tag si env))
((boolean?)
(emit-tag-check (tagged-primcall-operand1 x) boolean-mask boolean-tag si env))
((+)
(emit-expr (tagged-primcall-operand2 x) si env)
(emit-mov rax (offset si rsp))
(emit-expr (tagged-primcall-operand1 x) (- si wordsize) env)
(emit-add (offset si rsp) rax))
((-)
(emit-expr (tagged-primcall-operand2 x) si env)
(emit-mov rax (offset si rsp))
(emit-expr (tagged-primcall-operand1 x) (- si wordsize) env)
(emit-sub (offset si rsp) rax))
((*)
(emit-expr (tagged-primcall-operand2 x) si env)
(emit-mov rax (offset si rsp))
(emit-expr (tagged-primcall-operand1 x) (- si wordsize) env)
(emit-imul (offset si rsp) rax)
;; When two fixnums (which have 2 tag bits) are multiplied, the
;; relevant bits for the result are now 4 bytes to the left, so
;; we have to shift back 2 bytes.
(emit-shr (immediate fixnum-shift) rax))
((=)
(emit-comparison (tagged-primcall-operand1 x)
(tagged-primcall-operand2 x)
emit-sete si env))
((<)
(emit-comparison (tagged-primcall-operand1 x)
(tagged-primcall-operand2 x)
emit-setl si env))
((<=)
(emit-comparison (tagged-primcall-operand1 x)
(tagged-primcall-operand2 x)
emit-setle si env))
((>)
(emit-comparison (tagged-primcall-operand1 x)
(tagged-primcall-operand2 x)
emit-setg si env))
((>=)
(emit-comparison (tagged-primcall-operand1 x)
(tagged-primcall-operand2 x)
emit-setge si env))
((eq?)
(emit-comparison (tagged-primcall-operand1 x)
(tagged-primcall-operand2 x)
emit-sete si env))
((char=?)
(emit-comparison (tagged-primcall-operand1 x)
(tagged-primcall-operand2 x)
emit-sete si env))
((cons)
(emit-expr (tagged-primcall-operand2 x) si env) ; eval cdr
(emit-mov rax (offset si rsp)) ; save car to stack
(emit-expr (tagged-primcall-operand1 x) (- si wordsize) env) ; eval car
(emit-mov rax (offset 0 rsi)) ; move car onto heap
(emit-mov (offset si rsp) rax) ; copy cdr from the stack
(emit-mov rax (offset wordsize rsi)) ; move cdr onto heap
(emit-mov rsi rax) ; heap pointer is the value returned
(emit-or (immediate pair-tag) rax) ; set tag
(emit-add (immediate (* wordsize 2)) rsi)) ; bump heap pointer
((car)
(emit-expr (tagged-primcall-operand1 x) si env)
;; We have to untag the pair to get the pointer to the 'car'.
;; The pair tag is 1 so simply subtracting 1 gets us the pointer.
(emit-mov (offset -1 rax) rax))
((cdr)
(emit-expr (tagged-primcall-operand1 x) si env)
;; Again, the value is the pointer incremented by 1, so to get to
;; the cdr we need to jump ahead one word minus 1 byte.
(emit-mov (offset (- wordsize 1) rax) rax))
((make-vector)
(emit-expr (tagged-primcall-operand1 x) si env)
;; Wouldn't it be better to save the length untagged so that
;; vector-ref and vector-set! don't have untag it and
;; vector-length just needs to retag it?
(emit-mov rax (offset 0 rsi)) ; save length onto heap
(emit-mov rax rbx) ; save length in another register
(emit-mov rsi rax) ; copy heap pointer
(emit-or (immediate vector-tag) rax) ; set tag
;; Align to next two-word object boundary. I had to add an
;; additional shift instruction compared to what the paper did to
;; accommodate the difference in word size (the paper uses 4 byte
;; words, I'm using 8 byte words) which makes me wonder: For a 64
;; bit compiler, should the fixnum tag be 3 bits instead of 2?
(emit-sal (immediate 1) rbx)
(emit-add (immediate (* 2 wordsize)) rbx)
(emit-and (immediate (- (* 2 wordsize))) rbx)
(emit-add rbx rsi)) ; bump heap pointer by length of vector
((vector)
(let ((items (tagged-primcall-operands x)))
;; Eval all vector items and save them to stack locations.
;; It's important that we eval all items first, and not copy to
;; the heap as we go, because any sub-expression that also does
;; heap allocation will corrupt the heap space we think we have
;; all to ourselves here.
(let loop ((items items)
(si si))
(unless (null? items)
(emit-expr (car items) si env) ; eval item
(emit-mov rax (offset si rsp)) ; save to stack
(loop (cdr items) (- si wordsize))))
;; Save length onto heap (tagged as immediate int)
(emit-mov (immediate (ash (length items) 2)) rax)
(emit-mov rax (offset 0 rsi))
;; Copy items from the stack to the vector.
(let loop ((items items)
(si si)
(heap-offset wordsize))
(unless (null? items)
(emit-mov (offset si rsp) rax) ; copy from stack
(emit-mov rax (offset heap-offset rsi)) ; save to heap
(loop (cdr items) (- si wordsize) (+ heap-offset wordsize))))
(emit-mov rsi rax) ; copy heap pointer
(emit-or (immediate vector-tag) rax) ; set tag
;; Align heap pointer to next available 2 word boundary.
(emit-add (immediate (logand (* (+ (length items) 2) wordsize)
(- (* 2 wordsize))))
rsi)))
((vector-length)
(emit-expr (tagged-primcall-operand1 x) si env) ; get vector pointer
(emit-sub (immediate vector-tag) rax) ; untag vector
(emit-mov (offset 0 rax) rax)) ; the first word contains the length
((vector-ref)
(emit-expr (tagged-primcall-operand2 x) si env) ; get index arg
(emit-shr (immediate fixnum-shift) rax) ; untag it
(emit-add (immediate 1) rax) ; first word is the length so skip over it
(emit-imul (immediate wordsize) rax) ; scale by word size
(emit-mov rax rbx) ; save index to another register
(emit-expr (tagged-primcall-operand1 x) si env) ; get vector pointer
(emit-sub (immediate vector-tag) rax) ; untag vector
(emit-mov (register-offset rbx rax) rax)) ; get element
((vector-set!)
(emit-expr (tagged-primcall-operand1 x) si env) ; get vector pointer
(emit-sub (immediate vector-tag) rax) ; untag vector
(emit-mov rax rbx) ; save vector
(emit-expr (tagged-primcall-operand2 x) si env) ; get index
(emit-shr (immediate fixnum-shift) rax) ; untag it
(emit-add (immediate 1) rax) ; first word is the length so skip over it
(emit-imul (immediate wordsize) rax) ; scale by word size
(emit-add rax rbx) ; advance pointer to element being set
(emit-expr (tagged-primcall-operand3 x) si env) ; get value
(emit-mov rax (offset 0 rbx)))
((make-string)
(emit-expr (tagged-primcall-operand1 x) si env)
(emit-mov rax (offset 0 rsi)) ; save length onto heap
(emit-mov rax rbx) ; save length in another register
(emit-mov rsi rax) ; write to heap
(emit-or (immediate string-tag) rax) ; set tag
(emit-shr (immediate fixnum-shift) rbx) ; untag length
;; Align to next two-word object boundary, keeping in mind that
;; we need one additional word to store the length. Since we're
;; only storing ASCII characters in this simple compiler, we only
;; need 1 byte per character.
(emit-add (immediate (- (* 3 wordsize) 1)) rbx)
(emit-and (immediate (- (* 2 wordsize))) rbx)
(emit-add rbx rsi)) ; bump heap pointer by length of string
((string)
(let ((chars (tagged-primcall-operands x)))
;; Save length onto heap (tagged as immediate int)
(emit-mov (immediate (ash (length chars) 2)) rax)
(emit-mov rax (offset 0 rsi))
;; Add chars to string, one byte per item since we're only
;; covering the ASCII character set.
(let loop ((chars chars)
(heap-offset 1))
(unless (null? chars)
(emit-expr (first chars) si env) ; eval arg (should be char)
(emit-shr (immediate char-shift) rax) ; untag char
(emit-andq (immediate -255) (offset heap-offset rsi)) ; clear LSB
(emit-or rax (offset heap-offset rsi)) ; set char
(loop (cdr chars) (+ heap-offset 1))))
(emit-mov rsi rax) ; copy heap pointer
(emit-or (immediate string-tag) rax) ; set tag
;; Align heap pointer to next available 2 word boundary.
(emit-add (immediate (logand (+ (length chars) (- (* 3 wordsize) 1))
(- (* 2 wordsize))))
rsi)))
((string-length)
(emit-expr (tagged-primcall-operand1 x) si env) ; get string pointer
(emit-sub (immediate string-tag) rax) ; untag string
(emit-mov (offset 0 rax) rax)) ; the first word contains the length
((string-ref)
(emit-expr (tagged-primcall-operand2 x) si env) ; get index arg
(emit-shr (immediate fixnum-shift) rax) ; untag it
;; The first word of a string contains the length, however we
;; don't want to advance the pointer by a word because each
;; character is only a single byte. Instead, we advance the
;; pointer by a single byte so that the character we want to
;; access is in the least significant bit (LSB) section of the
;; word. From there, its a simple matter of masking out
;; everything but the LSB to isolate the character.
(emit-add (immediate 1) rax)
(emit-mov rax rbx) ; save index to another register
(emit-expr (tagged-primcall-operand1 x) si env) ; get string pointer
(emit-sub (immediate string-tag) rax) ; untag string
(emit-mov (register-offset rbx rax) rax) ; get char into LSB position
(emit-and (immediate 255) rax) ; clear out everything but the LSB
(emit-sal (immediate char-shift) rax) ; tag char
(emit-or (immediate char-tag) rax))
((string-set!)
(emit-expr (tagged-primcall-operand1 x) si env) ; get string pointer
(emit-sub (immediate string-tag) rax) ; untag string
(emit-mov rax rbx) ; save string pointer
(emit-expr (tagged-primcall-operand2 x) si env) ; get index arg
(emit-shr (immediate fixnum-shift) rax) ; untag it
(emit-add (immediate 1) rax)
(emit-add rax rbx) ; get char into LSB position
(emit-expr (tagged-primcall-operand3 x) si env) ; get value
(emit-shr (immediate char-shift) rax) ; untag char
(emit-andq (immediate -255) (offset 0 rbx)) ; clear LSB
(emit-or rax (offset 0 rbx))) ; copy char
((constant-init)
(let ((label (lookup (tagged-primcall-operand1 x) env)))
(emit-expr (tagged-primcall-operand2 x) si env)
(emit-lea (offset label rip) rbx)
(emit-mov rax (offset 0 rbx))))
((constant-ref)
(let ((label (lookup (tagged-primcall-operand1 x) env)))
(emit-lea (offset label rip) rax)
(emit-mov (offset 0 rax) rax)))
(else
(error "unknown primcall op" (primcall-op x)))))
(define (emit-let bindings body si env)
(let loop ((b* bindings) (new-env env) (si si))
(if (null? b*)
(for-each (lambda (x)
(emit-expr x si new-env))
body)
(let ((b (first b*)))
(emit-expr (rhs b) si env)
(emit-mov rax (offset si rsp))
(loop (cdr b*)
(extend-env (lhs b) si new-env)
(- si wordsize))))))
(define (emit-if test conseq altern si env)
(let ((L0 (unique-label))
(L1 (unique-label)))
(emit-expr test si env)
(emit-cmp (immediate (immediate-rep #f)) rax)
(emit-je L0)
(emit-expr conseq si env)
(emit-jmp L1)
(emit-label L0)
(emit-expr altern si env)
(emit-label L1)))
(define (emit-datum label)
(display ".data\n")
(emit-label label))
(define (emit-code label vars free-vars body env)
(display ".text\n")
(emit-label label)
;; Extend environment to include all procedure variables
(let loop ((vars vars)
(si (- wordsize))
(env env))
(if (null? vars)
;; Extend environment to include all free variables and
;; emit code to initialize their value from the current
;; closure pointer.
(let free-loop ((free-vars free-vars)
(ci wordsize)
(si si)
(env env))
(if (null? free-vars)
;; All variable setup is complete, we can finally
;; emit the body of the procedure.
(begin
(emit-expr body si env)
(emit-ret))
(begin
(emit-mov (offset ci rdi) rax) ; get value from closure
(emit-mov rax (offset si rsp)) ; push it to stack
(free-loop (cdr free-vars)
(+ ci wordsize)
(- si wordsize)
(extend-env (first free-vars) si env)))))
(loop (cdr vars)
(- si wordsize)
(extend-env (first vars) si env)))))
(define (emit-funcall proc args si env)
(emit-mov rdi (offset si rsp)) ; save current closure pointer
(let loop ((args args)
;; Skip a stack space to use as the return point.
(si* (- si (* wordsize 2))))
(if (null? args)
(let ((stack-start si))
(emit-expr proc si env) ; eval closure
(emit-sub (immediate closure-tag) rax) ; untag it to get pointer
(emit-mov rax rdi) ; store pointer in destination register
(unless (zero? stack-start)
(emit-add (immediate stack-start) rsp)) ; move stack pointer
(emit-call (string-append "*" (offset 0 rdi)))
(unless (zero? stack-start)
(emit-sub (immediate stack-start) rsp)) ; restore stack pointer
(emit-mov (offset (- si wordsize) rsp) rdi)) ; restore closure pointer
(begin ; eval argument
(emit-expr (first args) si* env)
(emit-mov rax (offset si* rsp))
(loop (cdr args)
(- si* wordsize))))))
(define (emit-tail-call proc args si env)
(let loop ((args* args)
(si* si))
(if (null? args*)
(let ((stack-start si))
(emit-expr proc si env) ; eval closure
(emit-sub (immediate closure-tag) rax) ; untag it to get pointer
(emit-mov rax rdi) ; store pointer in destination register
;; Copy all of the args from their current stack locations
;; at the top of the stack to the bottom of the stack.
;; Function calls are expecting to find the values of their
;; arguments starting from the bottom of the stack, so we
;; need to set things up as if we incremented the stack
;; pointer and made a 'call', but really we are doing a
;; 'jmp'. I feel like we're playing a trick on the
;; function. A very neat trick. :)
(let copy-loop ((args* args)
(si* si))
(unless (null? args*)
(emit-mov (offset si* rsp) rax) ; copy from top of stack...
(emit-mov rax (offset (- si* si wordsize) rsp)) ; ...to the bottom
(copy-loop (cdr args*) (- si* wordsize))))
(emit-jmp (string-append "*" (offset 0 rdi)))
(emit-mov (offset (- si wordsize) rsp) rdi)) ; restore closure pointer
(begin ; eval argument
(emit-expr (first args*) si* env)
(emit-mov rax (offset si* rsp))
(loop (cdr args*)
(- si* wordsize))))))
(define (emit-closure lvar vars si env)
(let ((label (lookup lvar env)))
(emit-lea (offset label rip) rax) ; first word of closure points to label
(emit-mov rax (offset 0 rsi)) ; first element of closure is label pointer
(let loop ((vars vars)
(i wordsize))
(if (null? vars)
(begin
(emit-mov rsi rax) ; capture heap pointer
(emit-or (immediate closure-tag) rax) ; set tag
;; Align heap pointer to nearest 2 word boundary for next
;; heap allocation.
(emit-add (immediate (logand (+ i wordsize)
(- (* 2 wordsize))))
rsi))
(let ((var-offset (lookup (first vars) env)))
(emit-mov (offset var-offset rsp) rax) ; copy value of free variable
(emit-mov rax (offset i rsi)) ; save it to closure
(loop (cdr vars)
(+ i wordsize)))))))
(define (emit-expr x si env)
(cond
((immediate? x)
(emit-mov (immediate (immediate-rep x)) rax))
((variable? x)
(emit-mov (offset (lookup x env) rsp) rax))
((if? x)
(emit-if (test x) (consequent x) (alternate x) si env))
((let? x)
(emit-let (let-bindings x) (let-body x) si env))
((tagged-primcall? x)
(emit-primitive-call x si env))
((labels? x)
(emit-labels (label-bindings x) (label-body x) si env))
((closure? x)
(emit-closure (closure-lvar x) (closure-vars x) si env))
((funcall? x)
(emit-funcall (funcall-proc x) (funcall-arguments x) si env))
((tail-call? x)
(emit-tail-call (tail-call-proc x) (tail-call-arguments x) si env))
(else
(error "unknown expression in native code generation" x))))
(define (emit-labels lvars body-exps si env)
(let* ((lvars* (map (lambda (lvar)
(cons (unique-label) lvar))
lvars))
(env* (fold (lambda (lvar env)
(let ((label (first lvar))
(name (second lvar)))
(extend-env name label env)))
env lvars*)))
(for-each (lambda (lvar)
(let ((label (first lvar))
(x (third lvar)))
(cond
((datum? x)
(emit-datum label))
((code? x)
(emit-code label (code-vars x) (code-free-vars x)
(code-body x) env*))
(else
(error "unknown label expression" x)))))
lvars*)
(display ".text\n")
(emit-label "scheme_entry")
(for-each (lambda (body-exp)
(emit-expr body-exp si env*))
body-exps)
(emit-ret)))
;;;
;;; Source to source program transformations
;;;
;; Find all free variables in an expression.
(define (free-variables x)
(define (add-variables vars more-vars)
(fold (lambda (new-var prev)
(if (memq new-var vars)
prev
(cons new-var prev)))
vars more-vars))
(let loop ((vars (second x))
(x (third x)))
(cond
((immediate? x) '())
((quote? x) '())
((variable? x)
(if (memq x vars)
'()
(list x)))
((if? x)
(delete-duplicates
(append (loop vars (test x))
(loop vars (consequent x))
(loop vars (alternate x)))))
((let? x)
(append-map (lambda (y)
(loop (add-variables vars (map lhs (let-bindings x))) y))
(let-body x)))
((tagged-primcall? x)
(append-map (lambda (operand)
(loop vars operand))
(tagged-primcall-operands x)))
((lambda? x)
(append-map (lambda (y)
(loop (add-variables vars (lambda-args x)) y))
(lambda-body x)))
((funcall? x)
(append-map (lambda (operand)
(loop vars operand))
(cdr x)))
((pair? x)
(append-map (lambda (arg)
(loop vars arg))
x)))))
;; Perform free variable analysis and transform 'lambda' forms into
;; closure/funcall forms and generate top-level labels for all
;; procedures.
(define (annotate-free-variables x)
(cond
((immediate? x) x)
((quote? x) x)
((variable? x) x)
((if? x)
`(if ,(annotate-free-variables (test x))
,(annotate-free-variables (consequent x))
,(annotate-free-variables (alternate x))))
((let? x)
`(let ,(map (lambda (binding)
(list (lhs binding)
(annotate-free-variables (rhs binding))))
(let-bindings x))
,@(map annotate-free-variables (let-body x))))
((tagged-primcall? x)
`(primcall ,(tagged-primcall-op x)
,@(map annotate-free-variables (tagged-primcall-operands x))))
((funcall? x)
`(funcall ,@(map annotate-free-variables (cdr x))))
((lambda? x)
`(lambda ,(lambda-args x)
,(free-variables x)
,@(map annotate-free-variables (lambda-body x))))
(else
(error "unknown form in free variable annotation" x))))
;; Determine if var is mutated (via set!) within an expression.
(define (mutable? var x)
(cond
((immediate? x) #f)
((quote? x) #f)
((variable? x) #f)
((if? x)
(or (mutable? var (test x))
(mutable? var (consequent x))
(mutable? var (alternate x))))
((let? x)
;; First, check if any of the expressions for the variable
;; bindings mutate the variable.
(or (any (lambda (binding)
(mutable? var (rhs binding)))
(let-bindings x))
;; Now check if the body mutates the variable, but not if the
;; let shadows the variable with a new meaning.
(let ((shadowed? (any (lambda (binding)
(eq? (lhs binding) var))
(let-bindings x))))
(if shadowed?
#f
(any (lambda (y)
(mutable? var y))
(let-body x))))))
((tagged-primcall? x)
(any (lambda (arg)
(mutable? var arg))
(tagged-primcall-operands x)))
((lambda? x)
(let ((shadowed? (any (lambda (arg)
(eq? arg var))
(lambda-args x))))
(if shadowed?
#f
(any (lambda (y)
(mutable? var y))
(lambda-body x)))))
((set? x)
(eq? (second x) var))
((pair? x)
(any (lambda (y) (mutable? var y)) x))))
(define* (box-mutable-variables x #:optional (mutable-vars '()))
(cond
((immediate? x) x)
((quote? x) x)
((variable? x)
;; Mutable variable references must be unboxed.
(if (memq x mutable-vars)
`(primcall vector-ref ,x 0)
x))
((if? x)
`(if ,(box-mutable-variables (test x) mutable-vars)
,(box-mutable-variables (consequent x) mutable-vars)
,(box-mutable-variables (alternate x) mutable-vars)))
((let? x)
(let* ((bindings (let-bindings x))
(mutable-bindings
;; Find all mutable bindings.
(filter-map (lambda (binding)
(and (any (lambda (y)
(mutable? (lhs binding) y))
(let-body x))
(lhs binding)))
bindings)))
`(let ,(map (lambda (binding)
(let ((var (lhs binding)))
(list var
;; Use a 1 element vector to box mutable
;; variables.
(if (memq var mutable-bindings)
`(primcall vector
,(box-mutable-variables (rhs binding)
mutable-vars))
(box-mutable-variables (rhs binding)
mutable-vars)))))
bindings)
,@(let ((mutable-vars*
(fold (lambda (var memo)
;; If the variable is mutable and not
;; already in the list of mutable vars
;; (meaning that this new mutable var is
;; shadowing another mutable var) then add
;; it to the list. If the variable isn't
;; mutable, then remove it from the list of
;; mutable vars, if present.
(if (memq var mutable-bindings)
(if (not (memq var mutable-vars))
(cons var memo)
memo)
(delq var memo)))
mutable-vars
mutable-bindings)))
(map (lambda (y)
(box-mutable-variables y mutable-vars*))
(let-body x))))))
((lambda? x)
(let* ((args (lambda-args x))
;; Mutable arguments get new names.
(args* (filter-map (lambda (arg)
(if (mutable? arg (third x))
(cons arg (unique-variable))
(cons arg arg)))
args))
;; Remove shadowed bindings. If a mutable var is shadowed
;; by another mutable var, it will be added back to the
;; list later.
(mutable-vars* (fold (lambda (arg memo)
(delq arg memo))
mutable-vars
args)))
`(lambda ,(map cdr args*)
;; Similar to let, box the values of the renamed args into 1
;; element vectors bound to the original names.
(let ,(filter-map (lambda (arg)
(and (not (eq? (car arg) (cdr arg)))
(list (car arg)
`(primcall vector ,(cdr arg)))))
args*)
,@(let ((mutable-vars*
(fold (lambda (arg memo)
(if (eq? (car arg) (cdr arg))
memo
(cons (car arg) memo)))
mutable-vars*
args*)))
(map (lambda (y)
(box-mutable-variables y mutable-vars*))
(lambda-body x)))))))
((tagged-primcall? x)
`(primcall ,(tagged-primcall-op x)
,@(map (lambda (arg)
(box-mutable-variables arg mutable-vars))
(tagged-primcall-operands x))))
((funcall? x)
`(funcall ,@(map (lambda (y)
(box-mutable-variables y mutable-vars))
(cdr x))))
((set? x)
;; Calls to set! are simply transformed to calls to vector-set! to
;; modify the item inside the box.
`(primcall vector-set! ,(second x) 0
,(box-mutable-variables (third x) mutable-vars)))
(else
(error "unknown form in mutable variable boxing pass" x))))
;; Transforms a quote form into code.
(define (expand-quote x)
(cond
((immediate? x) x)
((string? x)
`(primcall string ,@(string->list x)))
((vector? x)
`(primcall vector ,@(map expand-quote (vector->list x))))
((pair? x)
`(primcall cons ,(expand-quote (car x))
,(expand-quote (cdr x))))
((null? x)
'())
;; The compiler doesn't support symbols yet...
(else
(error "unknown quoted expression" x))))
;; Extracts certain forms to global program labels. Lambdas are
;; converted to closures and given a label. Quoted expressions are
;; initialized at the start of program execution and given a label.
(define (convert-closures-and-constants x)
(define (iter x)
(cond
((immediate? x)
(values x '() '()))
;; String literals and vectors are self-quoting.
((or (string? x) (vector? x))
(iter `(quote x)))
;; Quoted forms are complex constant values that must be
;; evaluated exactly once and referred to by a global label.
((quote? x)
(let ((quoted (expand-quote (quote-data x))))
;; Skip label generation if the quoted form expands to an
;; immediate value.
(if (immediate? quoted)
(values quoted '() '())
(let ((const (unique-variable)))
(values `(primcall constant-ref ,const)
(list (list const '(datum)))
(list `(primcall constant-init
,const
,(expand-quote
(quote-data x)))))))))
((variable? x)
(values x '() '()))
((if? x)
(let-values (((test* labels0 init0) (iter (test x)))
((consequent* labels1 init1) (iter (consequent x)))
((alternate* labels2 init2) (iter (alternate x))))
(values `(if ,test* ,consequent* ,alternate*)
(append labels0 labels1 labels2)
(append init0 init1 init2))))
((let? x)
(let ((bindings*
(map (lambda (binding)
(let-values (((rhs* labels initializers)
(iter (rhs binding))))
(list (list (lhs binding) rhs*)
labels
initializers)))
(let-bindings x)))
(body*
(map (lambda (y)
(let-values (((y* labels initializers) (iter y)))
(list y* labels initializers)))
(let-body x))))
(values `(let ,(map first bindings*)
,@(map first body*))
(append (concatenate (map second bindings*))
(concatenate (map second body*)))
(append (concatenate (map third bindings*))
(concatenate (map third body*))))))
((tagged-primcall? x)
(let ((operands (map (lambda (operand)
(let-values (((operand* labels initializers)
(iter operand)))
(list operand* labels initializers)))
(tagged-primcall-operands x))))
(values `(primcall ,(tagged-primcall-op x) ,@(map first operands))
(concatenate (map second operands))
(concatenate (map third operands)))))
;; Convert procedure calls to 'funcall' form that refers to a
;; closure.
((funcall? x)
(let ((args (map (lambda (arg)
(let-values (((arg* labels initializers)
(iter arg)))
(list arg* labels initializers)))
(cdr x))))
(values `(funcall ,@(map first args))
(concatenate (map second args))
(concatenate (map third args)))))
;; Perform closure conversion.
((lambda? x)
(let-values (((body* labels initializers)
(iter (fourth x))))
(let ((name (unique-lvar)))
(values `(closure ,name ,@(third x))
(cons (list name
`(code ,(second x) ,(third x) ,body*))
labels)
initializers))))))
(let-values (((new-x labels initializers) (iter x)))
`(labels ,labels ,@(append initializers (list new-x)))))
(define (mark-tail-calls x)
(define (maybe-mark x)
(if (funcall? x)
`(tail-call ,@(cdr x))
(mark-tail-calls x)))
(cond
((immediate? x) x)
((variable? x) x)
((closure? x) x)
((quote? x) x)
((if? x)
`(if ,(mark-tail-calls (test x))
,(maybe-mark (consequent x))
,(maybe-mark (alternate x))))
((let? x)
`(let ,(map (lambda (binding)
(list (lhs binding)
(mark-tail-calls (rhs binding))))
(let-bindings x))
,@(map maybe-mark (let-body x))))
((tagged-primcall? x)
`(primcall ,(tagged-primcall-op x)
,@(map mark-tail-calls (tagged-primcall-operands x))))
((funcall? x)
`(funcall ,@(map mark-tail-calls (cdr x))))
((code? x)
`(code ,(code-vars x) ,(code-free-vars x) ,(mark-tail-calls (fourth x))))
((datum? x)
'(datum))
((labels? x)
`(labels ,(map (lambda (binding)
(list (lhs binding)
(mark-tail-calls (rhs binding))))
(label-bindings x))
,@(map mark-tail-calls (drop x 2))))))
;; Transform expressions like:
;; (define (foo x) x)
;; Into:
;; (define foo (lambda (x) x))
(define (expand-definition x)
(if (pair? (second x)) ;
`(define ,(car (second x))
(lambda ,(cdr (second x))
,@(cddr x)))
x))
;; Tranform a series of expressions with 'define' forms into a 'let'
;; expression that binds all the defined variables.
(define (hoist-definitions exprs)
(let loop ((orig-exprs exprs)
(defs '())
(new-exprs '()))
(cond
((and (null? orig-exprs) (null? defs))
exprs)
((null? orig-exprs)
`((let ,(reverse defs)
,@(reverse new-exprs))))
((define? (car orig-exprs))
(let ((x (expand-definition (car orig-exprs))))
(loop (cdr orig-exprs)
(cons (list (second x) (third x)) defs)
new-exprs)))
(else
(loop (cdr orig-exprs)
defs
(cons (car orig-exprs) new-exprs))))))
;; Expand a fixed set of macros (let*, letrec, letrec*, cond, case,
;; when, unless, or, and, and define) in a hygienic manner and
;; explicitly tag primitive calls and function calls.
(define (macro-expand x)
(let loop ((x x)
(vars '()))
(cond
;; Immediates and quoted values are left as-is.
((immediate? x) x)
((quote? x) x)
;; Variable references are replaced with their unique,
;; alpha-converted name. This prevents lexical variable names
;; from clashing with primitive call names.
((symbol? x)
(or (assq-ref vars x)
(error "unbound variable in macro expansion" x)))
((if? x)
`(if ,(loop (test x) vars)
,(loop (consequent x) vars)
,(loop (alternate x) vars)))
((set? x)
`(set! ,(loop (second x) vars) ,(loop (third x) vars)))
;; Convert extended forms to primitive forms. For example, a
;; let* expression is rewritten as a nested series of let
;; expression.
((let*? x)
(loop (car
(let expand-let* ((bindings (let-bindings x)))
(if (null? bindings)
(let-body x)
(let ((binding (car bindings)))
`((let (,binding)
,@(expand-let* (cdr bindings))))))))
vars))
;; Simple implementation of letrec: Bind variables to empty
;; values. Bind temporary variables to the real values. Mutate
;; original variables to have the values of the temporaries.
((letrec? x)
;; TODO: Use special unspecified value instead of #f.
(loop (let ((temps (map (lambda (binding)
(cons (lhs binding) (unique-variable)))
(let-bindings x))))
`(let ,(map (lambda (binding)
(list (lhs binding) #f))
(let-bindings x))
(let ,(map (lambda (binding temp)
(list (cdr temp) (rhs binding)))
(let-bindings x)
temps)
,@(map (lambda (binding)
(let ((var (lhs binding)))
`(set! ,var ,(assq-ref temps var))))
(let-bindings x))
,@(let-body x))))
vars))
;; letrec* is simpler than letrec because binding happens
;; sequentially so we don't need to use temporaries.
((letrec*? x)
;; TODO: Use special unspecified value instead of #f.
(loop `(let ,(map (lambda (binding)
(list (lhs binding) #f))
(let-bindings x))
,@(map (lambda (binding)
`(set! ,(lhs binding) ,(rhs binding)))
(let-bindings x))
,@(let-body x))
vars))
;; TODO: Handle multiple expressions in clause.
;; TODO: Handle => syntax.
((cond? x) ; 'cond' is just nested 'if's
(loop (let expand-cond ((clauses (cdr x)))
(if (null? clauses)
#f
(let ((clause (car clauses)))
(cond
((and (eq? (first clause) 'else)
(null? (cdr clauses)))
(second clause))
((eq? (first clause) 'else)
(error "else must be the last clause of cond" x))
(else
`(if ,(first clause)
,(second clause)
,(expand-cond (cdr clauses))))))))
vars))
;; 'when' and 'unless' are, you guessed it, special forms of
;; 'if'!
;;
;; TODO: Handle multiple expressions
((when? x)
(loop `(if ,(second x) ,(third x) #f) vars))
((unless? x)
(loop `(if (eq? ,(second x) #f) ,(third x) #f) vars))
;; 'or' and 'and' are also defined in terms of 'if'
((or? x)
(loop (let or-loop ((exprs (cdr x)))
(if (null? exprs)
#f
`(let ((v ,(car exprs)))
(if v v ,(or-loop (cdr exprs))))))
vars))
((and? x)
(loop (let and-loop ((exprs (cdr x)))
(cond
((null? exprs) #t)
((null? (cdr exprs))
`(let ((v ,(car exprs)))
(if v v #f)))
(else
`(let ((v ,(car exprs)))
(if v ,(and-loop (cdr exprs)) #f)))))
vars))
;; TODO: Handle => syntax
;; TODO: Handle symbols once implemented
;; TODO: Handle multiple expressions in a clause
((case? x)
(loop `(let ((key ,(second x)))
,(let expand-cond ((clauses (cddr x)))
(if (null? clauses)
#f
(let ((clause (car clauses)))
(cond
((and (eq? (first clause) 'else)
(null? (cdr clauses)))
(second clause))
((eq? (first clause) 'else)
(error "else must be the last clause of case" x))
(else
`(if (or ,@(map (lambda (datum)
`(= key ,datum))
(first clause)))
,(second clause)
,(expand-cond (cdr clauses)))))))))
vars))
;; Convert lexical variables to unique identifiers through
;; alpha-conversion for 'let' and 'lambda' forms.
((let? x)
(let ((new-vars (append (map (lambda (binding)
(cons (lhs binding) (unique-variable)))
(let-bindings x))
vars)))
`(let ,(map (lambda (binding)
(list (assq-ref new-vars (lhs binding))
(loop (rhs binding) vars)))
(let-bindings x))
,@(map (lambda (y) (loop y new-vars))
(hoist-definitions (let-body x))))))
((lambda? x)
(let ((new-vars (append (map (lambda (arg)
(cons arg (unique-variable)))
(lambda-args x))
vars)))
`(lambda ,(map (lambda (arg)
(assq-ref new-vars arg))
(lambda-args x))
,@(map (lambda (y)
(loop y new-vars))
(hoist-definitions (lambda-body x))))))
;; Function calls. The 'or' expression first tests for a valid
;; variable reference in the operator position. Failing that, it
;; for a sub-expression in the operator position. We need to do
;; this kind of analysis because primitive calls are currently
;; *not* part of the lexical environment, so if we tried to
;; lookup the variable '+', for example, it would fail.
((and (pair? x)
(or (and (symbol? (car x)) (assq-ref vars (car x)))
(pair? (car x))))
`(funcall ,@(map (lambda (y) (loop y vars)) x)))
;; If the function call operator is not a variable reference or a
;; more complex expression, then it might be a primitive call.
((primcall? x)
`(primcall ,(primcall-op x)
,@(map (lambda (y) (loop y vars))
(primcall-operands x))))
(else ; oh no
(error "unknown form" x)))))
;; Apply all compiler passes to transform the input program into a
;; form that it is suitable for compilation to native assembly code.
(define (transform x)
(parameterize ((unique-counter 0))
(mark-tail-calls
(convert-closures-and-constants
(annotate-free-variables
(box-mutable-variables
(macro-expand x)))))))
;;;
;;; Compiler frontend
;;;
(define (compile-program x)
(let ((x* (transform x)))
(parameterize ((unique-counter 0))
(emit-labels (second x*) (drop x* 2) (- wordsize) '()))))
(define (compile-and-run x)
(with-output-to-file "scheme_entry.s"
(lambda ()
(display ".p2align 4
.globl scheme_entry
.type scheme_entry, @function
")
(compile-program x)))
(unless (zero? (system* "gcc" "-c" "scheme_entry.s"))
(error "failed to compile scheme_entry.s"))
(unless (zero? (system* "gcc" "-c" "test.c"))
(error "failed to compile test.c"))
(unless (zero? (system* "gcc" "-o" "test" "scheme_entry.o" "test.o"))
(error "failed to link program"))
(let* ((pipe (open-pipe* OPEN_READ "./test"))
(output (read-line pipe)))
(close pipe)
output))
;;;
;;; Tests
;;;
(define (test-case x expected-output)
(let ((result (compile-and-run x)))
(if (and (not (eof-object? result))
(string=? result expected-output))
#t
(begin
(display "expected: ")
(display expected-output)
(display ", got: ")
(display result)
(newline)
#f))))
(begin
(test-case 1 "1")
(test-case #\b "b")
(test-case #t "#t")
(test-case #f "#f")
(test-case '(add1 3) "4")
(test-case '(sub1 3) "2")
(test-case '(integer->char 98) "b")
(test-case '(char->integer #\b) "98")
(test-case '(zero? 1) "#f")
(test-case '(zero? 0) "#t")
(test-case '(null? 1) "#f")
(test-case '(null? #\b) "#f")
(test-case '(null? #t) "#f")
(test-case '(null? #f) "#f")
(test-case '(null? ()) "#t")
(test-case '(integer? #\b) "#f")
(test-case '(integer? #f) "#f")
(test-case '(integer? 1) "#t")
(test-case '(char? 1) "#f")
(test-case '(char? #f) "#f")
(test-case '(char? #\b) "#t")
(test-case '(boolean? 1) "#f")
(test-case '(boolean? #\b) "#f")
(test-case '(boolean? #f) "#t")
(test-case '(boolean? #t) "#t")
(test-case '(+ 1 2) "3")
(test-case '(- 3 1) "2")
(test-case '(* 2 3) "6")
(test-case '(= 1 2) "#f")
(test-case '(= 1 1) "#t")
(test-case '(< 2 1) "#f")
(test-case '(< 1 2) "#t")
(test-case '(<= 2 1) "#f")
(test-case '(<= 1 2) "#t")
(test-case '(<= 2 2) "#t")
(test-case '(> 1 2) "#f")
(test-case '(> 2 1) "#t")
(test-case '(>= 1 2) "#f")
(test-case '(>= 2 1) "#t")
(test-case '(>= 2 2) "#t")
(test-case '(char=? #\a #\b) "#f")
(test-case '(char=? #\b #\b) "#t")
(test-case '(let ((x 1)) x) "1")
(test-case '(let ((x 1) (y 2)) (+ x y)) "3")
(test-case '(if #t 1 2) "1")
(test-case '(if #f 1 2) "2")
(test-case '(car (cons 10 20)) "10")
(test-case '(cdr (cons 10 20)) "20")
(test-case '(car (cdr (cons 1 (cons 2 '())))) "2")
(test-case '(vector-length (make-vector 3)) "3")
(test-case '(vector-ref (vector 1 2 3) 1) "2")
(test-case '(string-length (make-string 5)) "5")
(test-case '(string-ref (string #\a #\b #\c) 1) "b")
;; procedure with no free variables and multiple args
(test-case '(let ((perimeter (lambda (length width)
(+ (* length 2)
(* width 2)))))
(perimeter 4 3))
"14")
;; closures!
(test-case '(let ((x 5))
(let ((f (lambda (y) (+ x y))))
(f 4)))
"9")
;; recursive tail calls
(test-case '(let ((f (lambda (x f)
(if (= x 5)
789
(f (add1 x) f)))))
(f 0 f))
"789")
;; complex constants
(test-case '(let ((f (lambda () (quote (1 . "H")))))
(eq? (f) (f)))
"#t")
;; mutable variables
(test-case '(let ((f (lambda (c)
(cons (lambda (v) (set! c v))
(lambda () c)))))
(let ((p (f 0)))
((car p) 12)
((cdr p))))
"12")
;; overriding primcalls
(test-case '(let ((+ (lambda (a b) (* a b))))
(+ 3 3))
"9")
;; let*
(test-case '(let* ((x 1)
(y (+ x 2)))
(+ x y))
"4")
;; letrec
(test-case '(letrec ((f (lambda (x) (+ (g x) 1)))
(g (lambda (x) (+ x 2))))
(f 1))
"4")
;; letrec*
(test-case '(letrec* ((f (lambda (x) (+ (g x) 1)))
(g (lambda (x) (+ x 2))))
(f 1))
"4")
;; when
(test-case '(let ((x 0))
(when (= x 0)
123))
"123")
(test-case '(let ((x 1))
(when (= x 0)
123))
"#f")
;; unless
(test-case '(let ((x 1))
(unless (= x 0)
123))
"123")
(test-case '(let ((x 1))
(unless (= x 1)
123))
"#f")
;; cond
(test-case '(let ((x 3))
(cond
((= x 0) 10)
((= x 1) 11)
((= x 2) 12)
(else 13)))
"13")
;; or
(test-case '(or) "#f")
(test-case '(or #f 666) "666")
(test-case '(or #f #f) "#f")
;; and
(test-case '(and) "#t")
(test-case '(and 1 2 3) "3")
(test-case '(and 1 2 #f) "#f")
;; case
(test-case '(case 666
((111 222 333)
444)
((555 666)
777)
(else 888))
"777")
;; internal define
(test-case '(let ()
(define x 1)
(define y 2)
(define (f a b)
(+ a b))
(f x y))
"3"))
|