aboutsummaryrefslogtreecommitdiffstats
path: root/example-vms/jitterlisp/jitterlisp.jitter
blob: df36fe0d2676e6d3160e0f89f035faa3f5e59e55 (plain)
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
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
## Jitter specification for the JitterLisp VM.

legal-notice
  Copyright (C) 2017, 2018, 2020, 2021  Luca Saiu
  Written by Luca Saiu

  This file is part of JitterLisp, distributed as an example along with
  GNU Jitter under the same license.

  JitterLisp is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  JitterLisp is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with JitterLisp.  If not, see <http://www.gnu.org/licenses/>.
end


## Global configuration.
#################################################################

vm
  set prefix "jitterlispvm"
end




## Stacks and register classes.
#################################################################

stack m
  long-name "mainstack"
  c-element-type "jitterlisp_object"
  element-no 10240
  # c-initial-value "JITTERLISP_UNINITIALIZED_MAINSTACK_VALUE"
  # guard-underflow guard-overflow
  no-guard-underflow no-guard-overflow
  tos-optimized
end

stack t
  long-name "returnstack"
  c-element-type "jitterlisp_object"
  element-no 10240
  # c-initial-value "JITTERLISP_UNINITIALIZED_RETURNSTACK_VALUE"
  # guard-underflow guard-overflow
  no-guard-underflow no-guard-overflow
  non-tos-optimized
end

register-class r
  c-type "jitterlisp_object"
#  c-initial-value "JITTERLISP_UNINITIALIZED_REGISTER_VALUE"
  fast-register-no 0#1
end




## Functions and globals to wrap.
#################################################################

wrapped-functions
  # Libc cunctions which may be implicitly called by compiled code, for
  # example when passing parameters.
# FIXME: no, GCC is not stupid and generates these directly in its
# intermediate representation.  The hack doesn't work here.

  # Temporary/debugging stuff.
  debug_pointer
#  printf
  jitterlisp_print

  # Calling interpreted closures from compiled code.
  jitterlisp_call_interpreted

  # These are needed in operations, as per jitterlisp-operations.h .  Only
  # the functions we call *directly* thru instructions need to be wrapped:
  # low-priority primitives, not worth optimizing, are all called via the
  # "primitive" VM instruction which invokes a function pointer given as an
  # instruction argument.
  # (None yet.)
  # [FIXME: I'll need some function related to eval, to call interpreted
  # code from VM code]

  # Some primitives, notably cons , allocate heap memory from VM instructions.
  jitterlisp_allocate

  # Erroring out from instructions.
  jitterlisp_fail_from_vm
  jitterlispvm_error_invalid_primitive_argument_type_unfriendly
  jitterlispvm_error_invalid_primitive_argument_type_friendly
end

wrapped-globals
  stack_printf_format_string
end

early-header-c
  code
#   include "jitterlisp.h"
  end
end

initialization-c
  code
  end
end

finalization-c
  code
  end
end

early-c
  code
#   include <stdio.h>
  end
end
late-c
  code
__attribute__ ((unused))
void
debug_pointer (jitter_int index, const void *pointer)
{
  return;
  printf ("[DEBUG: %lli %p]\n", (long long) index, pointer);
  fflush (stdout);
}

/* Open an argument-type-checking sequence, for the given number of arguments
   to be read from the main stack.  This is a helper macro for
   JITTERLISPVM_CHECK_TYPES_* . */
#define JITTERLISPVM_BEGIN_CHECK_TYPES_(_jitterlisp_vm_in_arity)  \
  JITTER_BEGIN_                                                   \
    int _jitterlispvm_arg_no = (_jitterlisp_vm_in_arity);         \
    int _jitterlispvm_arg_index __attribute__ ((unused)) = 0;     \
    int _jitterlispvm_arg_depth __attribute__ ((unused)) \
      = _jitterlispvm_arg_no - 1;

/* Check the next argument in the sequence, failing if its type doesn't match
   the given uppercase suffix.  Do nothing if compiling an unsafe JitterLisp. */
#ifdef JITTERLISP_UNSAFE
# define JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type,    \
                                       _jitterlisp_error_fast_label)  \
    { /* Do nothing. */ }
#else
# define JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type,     \
                                       _jitterlisp_error_fast_label)   \
    {                                                                  \
      /* Branch-fast to the given label if the next object type is     \
         not the required one. */                                      \
      JITTER_CONCATENATE_TWO (JITTERLISP_BRANCH_FAST_UNLESS_,          \
                              _jitterlisp_uppercase_type)              \
         (JITTER_AT_DEPTH_MAINSTACK(_jitterlispvm_arg_depth),          \
          _jitterlisp_error_fast_label);                               \
      _jitterlispvm_arg_depth --;                                      \
      _jitterlispvm_arg_index ++;                                      \
    }
#endif // #ifdef JITTERLISP_UNSAFE

/* Close an argument-type-checking sequence.  This is a helper macro for
   JITTERLISPVM_CHECK_TYPES_* . */
#define JITTERLISPVM_END_CHECK_TYPES_  \
  JITTER_END_

/* For in-arity N, check that the topmost N arguments have the given N types
   expressed as uppercase suffixes, respectively.  This functionality is
   provided for a few common values of N . */
#define JITTERLISPVM_CHECK_TYPES_1(_jitterlisp_uppercase_type_0,  \
                                   _jitterlisp_error_fast_label)  \
  JITTERLISPVM_BEGIN_CHECK_TYPES_(1)                              \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_0,   \
                                  _jitterlisp_error_fast_label);  \
  JITTERLISPVM_END_CHECK_TYPES_
#define JITTERLISPVM_CHECK_TYPES_2(_jitterlisp_uppercase_type_0,  \
                                   _jitterlisp_uppercase_type_1,  \
                                   _jitterlisp_error_fast_label)  \
  JITTERLISPVM_BEGIN_CHECK_TYPES_(2)                              \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_0,   \
                                  _jitterlisp_error_fast_label);  \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_1,   \
                                  _jitterlisp_error_fast_label);  \
  JITTERLISPVM_END_CHECK_TYPES_
#define JITTERLISPVM_CHECK_TYPES_3(_jitterlisp_uppercase_type_0,  \
                                   _jitterlisp_uppercase_type_1,  \
                                   _jitterlisp_uppercase_type_2,  \
                                   _jitterlisp_error_fast_label)  \
  JITTERLISPVM_BEGIN_CHECK_TYPES_(3)                              \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_0,   \
                                  _jitterlisp_error_fast_label);  \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_1,   \
                                  _jitterlisp_error_fast_label);  \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_2,   \
                                  _jitterlisp_error_fast_label);  \
  JITTERLISPVM_END_CHECK_TYPES_
#define JITTERLISPVM_CHECK_TYPES_4(_jitterlisp_uppercase_type_0,  \
                                   _jitterlisp_uppercase_type_1,  \
                                   _jitterlisp_uppercase_type_2,  \
                                   _jitterlisp_uppercase_type_3,  \
                                   _jitterlisp_error_fast_label)  \
  JITTERLISPVM_BEGIN_CHECK_TYPES_(4)                              \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_0,   \
                                  _jitterlisp_error_fast_label);  \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_1,   \
                                  _jitterlisp_error_fast_label);  \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_2,   \
                                  _jitterlisp_error_fast_label);  \
    JITTERLISPVM_CHECK_NEXT_TYPE_(_jitterlisp_uppercase_type_3,   \
                                  _jitterlisp_error_fast_label);  \
  JITTERLISPVM_END_CHECK_TYPES_

#if 0
# define JITTERLISPVM_ERROR_INVALID_PRIMITIVE_ARGUMENT_TYPE_          \
    jitterlispvm_error_invalid_primitive_argument_type_unfriendly ()
#else
# define JITTERLISPVM_ERROR_INVALID_PRIMITIVE_ARGUMENT_TYPE_     \
    jitterlispvm_error_invalid_primitive_argument_type_friendly  \
       (JITTER_SPECIALIZED_INSTRUCTION_OPCODE,                   \
        _jitterlispvm_arg_index,                                 \
        JITTER_AT_DEPTH_MAINSTACK(_jitterlispvm_arg_depth))
#endif


// Unfortunately the attribute is invisible across the wrapper.
// This is convenient for debugging, but I don't want to use it in production:
// passing parameters at every call makes the code much bigger, even if the
// call is not executed.
__attribute__ ((noreturn, unused))
static void
jitterlispvm_error_invalid_primitive_argument_type_friendly
   (int sins_opcode,
    int arg_index,
    jitterlisp_object o)
{
  /* Print information about the argument and its hex representation:
     don't dare yet to print it as a Lisp object, as the printer might
     crash if there is a bug and the object is invalid. */
#define SIZEOF_BUFFER 1000
  char buffer [SIZEOF_BUFFER];
  snprintf (buffer,
            SIZEOF_BUFFER,
            "%s: About the %i-th (0-based) primitive argument %p\n",
            jitterlispvm_specialized_instruction_names [sins_opcode],
            arg_index, (void *) o);
#undef SIZEOF_BUFFER
  jitterlisp_print_error_char_star (buffer);

  /* This shouldn't be needed on GNU, but flush the output so that we can be
     sure that the previous line is visible before we crash. */
  jitter_print_flush (jitterlisp_print_context);

  /* Okay, now we can print in in Lisp. */
  jitterlisp_print_error_char_star ("  (in Lisp ");
  jitterlisp_print_error (o);
  jitterlisp_print_error_char_star ("):\n");

  jitterlisp_error_cloned ("invalid primitive argument type");
}

__attribute__ ((noreturn, unused))
static void
jitterlispvm_error_invalid_primitive_argument_type_unfriendly (void)
{
  jitterlisp_error_cloned ("invalid primitive argument type");
}

static void
jitterlisp_fail_from_vm (void)
{
  jitterlisp_error_cloned ("unspecified error raised by VM code");
}

    static const char *stack_printf_format_string __attribute__ ((unused))
      = "%" JITTER_PRIi "\n";
  end
end




## Custom literal argument printer.
#################################################################

printer-c
  code
    /* Not really needed.  The printer is jitterlisp_print. */
  end
end




## Debugging code for instructions.
#################################################################

early-c
  code
  end
end

late-c
#initialization-c
  code
  end
end

instruction-beginning-c
  code
  end
end

instruction-end-c
  code
  end
end




## User-defined state fields (scratch).
#################################################################

early-header-c
  code
/* If enabled, use my nonworking heap allocation stub.  This is useful to me,
   for playing with the still non-existent Jitter garbage collector and reason
   about its API. */
//#define JITTER_GC_STUB
  end
end

late-header-c
  code
  end
end

late-c
  code
  end
end

state-struct-runtime-c
  code
#ifdef JITTER_GC_STUB
    /* A pointer to the next free byte in the nursery.  Untagged. */
    char *allocation_next;

    /* The nursery allocation limit, untagged -- which is to say, the maximum
       valid address in the nursery plus 1.
       Notice that it is correct, if slightly conservative, to compare even a
       tagged pointer against this in an expression like
          new_tagged_pointer < allocation_limit
       , since a tagged pointer is always greater than or equal to its untagged
       counterpart. */
    char *allocation_limit;
#endif // #ifdef JITTER_GC_STUB
  end
end

state-struct-backing-c
  code
    /* Every VM state which is currently in use is linked within the list
       jitterlisp_used_states; every VM state which is *not* currently in use is
       linked within the list jitterlisp_unused_states.  Since each state is at
       any time only in one of the two lists, we can reuse the same pointer
       fields.
       See the "State pool" section in jitterlisp-eval-vm.h .
       The code for linking and unlinking states within the list is in
       jitterlisp-eval-vm.c ; it is not part of state initialisation or
       finalisation. */
    struct jitter_list_links pool_links;
  end
end

state-initialization-c
  code
#ifdef JITTER_GC_STUB
  /* Initialize the next pointer and the limit pointer to refer to a fixed-size
     nursery.  There is no real GC yet, so when next hits limit there will be
     a failure; still, allocation should work up to that point. */
  size_t nursery_size = 1024 * 1024 * 10;
  char *nursery = jitter_xmalloc (nursery_size);
  jitter_state_runtime->allocation_next = nursery;
  jitter_state_runtime->allocation_limit = nursery + nursery_size;
#endif // #ifdef JITTER_GC_STUB

  /* Register the two stack backings as GC roots. */
  jitterlisp_push_stack_backing_as_gc_root
     (& jitter_state_backing->jitter_stack_mainstack_backing);
  jitterlisp_push_stack_backing_as_gc_root
     (& jitter_state_backing->jitter_stack_returnstack_backing);
  end
end

state-reset-c
  code
    /* If this explicit code block were not provided a state would be reset
       by first executing the user finalisation code, and then the user
       initialisation code.  That would first unregister some roots, and
       then re-register them.
       The best solution here consists in explicitly doing nothing. */
  end
end

state-finalization-c
  code
    /* This assumes that states are made and destroyed in LIFO order, which
       is true in JitterLisp but still crude.  I will define a better API
       when switching to the new Jitter GC (in Boehm's defense, it could
       easily be done with his GC as well). */
    jitterlisp_pop_gc_roots (2);
  end
end



## Instructions.
#################################################################

instruction nop ()
  code
  end
end

instruction dup ()
  code
    JITTER_DUP_MAINSTACK();
  end
end

instruction drop ()
  code
    JITTER_DROP_MAINSTACK();
  end
end

instruction nip ()
  code
    JITTER_NIP_MAINSTACK();
  end
end

instruction nip-drop ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end

instruction drop-nip ()
  code
    JITTER_DROP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
  end
end

# FIXME: make a one-argument version generalizing these.  These zero-argument
# versions are useful to generate from rewrite rules right now, before Jitter
# support is ready.
# Each of these VM instructions compiles to just one fast hardware instruction,
# decrementing a register by a constant without touching memory.
instruction nip-two ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
  end
end
instruction nip-three ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
  end
end
instruction nip-four ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
  end
end
instruction nip-five ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
  end
end
instruction nip-six ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
  end
end

# Each of these has the same cost of a single drop.
# FIXME: generalize into a unary instruction when Jitter rewrite rules become
# expressive enough to obtain these by rewriting.
instruction nip-two-drop ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end
instruction nip-three-drop ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end
instruction nip-four-drop ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end
instruction nip-five-drop ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end
instruction nip-six-drop ()
  code
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end

# FIXME: is this ever needed?
instruction push-unspecified ()
  code
    JITTER_PUSH_UNSPECIFIED_MAINSTACK();
  end
end

instruction push-register (?R)
  code
    jitterlisp_object k = JITTER_ARG0;
    JITTER_PUSH_MAINSTACK(k);
  end
end

instruction push-literal (?n jitterlisp_print)
  code
    jitterlisp_object k = JITTER_ARGN0;
    JITTER_PUSH_MAINSTACK(k);
  end
end

instruction push-global (?n jitterlisp_print, ?f)
  code
    jitterlisp_object tagged_symbol = JITTER_ARGN0;
    struct jitterlisp_symbol * symbol = JITTERLISP_SYMBOL_DECODE(tagged_symbol);

#ifndef JITTERLISP_UNSAFE
    JITTER_BRANCH_FAST_IF_EQUAL (symbol->global_value, JITTERLISP_UNDEFINED,
                                 JITTER_ARGF1);
#endif // #ifndef JITTERLISP_UNSAFE

//    JITTER_PUSH_MAINSTACK(symbol->global_value);
    JITTER_PUSH_UNSPECIFIED_MAINSTACK();
    JITTER_TOP_MAINSTACK() = symbol->global_value;
  end
end

# FIXME: use these, in rewrites.
instruction push-nil ()
  code
    JITTER_PUSH_MAINSTACK(JITTERLISP_EMPTY_LIST);
  end
end
instruction push-zero ()
  code
    JITTER_PUSH_MAINSTACK(JITTERLISP_FIXNUM_ENCODE(0));
  end
end
instruction push-one ()
  code
    JITTER_PUSH_MAINSTACK(JITTERLISP_FIXNUM_ENCODE(1));
  end
end
instruction push-false ()
  code
    JITTER_PUSH_MAINSTACK(JITTERLISP_FALSE);
  end
end
instruction push-nothing ()
  code
    JITTER_PUSH_MAINSTACK(JITTERLISP_NOTHING);
  end
end

instruction pop-to-register (!R)
  code
    JITTER_ARG0 = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end

instruction pop-to-global (?n jitterlisp_print, ?f)
  code
    jitterlisp_object tagged_symbol = JITTER_ARGN0;
    struct jitterlisp_symbol *symbol = JITTERLISP_SYMBOL_DECODE(tagged_symbol);
#ifndef JITTERLISP_UNSAFE
    JITTER_BRANCH_FAST_IF_NONZERO (symbol->global_constant, JITTER_ARGF1);
#endif // #ifndef JITTERLISP_UNSAFE
    symbol->global_value = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end

instruction pop-to-global-defined (?n jitterlisp_print, ?f)
  code
    jitterlisp_object tagged_symbol = JITTER_ARGN0;
    struct jitterlisp_symbol *symbol = JITTERLISP_SYMBOL_DECODE(tagged_symbol);
#ifndef JITTERLISP_UNSAFE
    JITTER_BRANCH_FAST_IF_NONZERO (symbol->global_constant, JITTER_ARGF1);
    JITTER_BRANCH_FAST_IF_EQUAL (symbol->global_value, JITTERLISP_UNDEFINED,
                                 JITTER_ARGF1);
#endif // #ifndef JITTERLISP_UNSAFE
    symbol->global_value = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
  end
end

# The first argument must not be zero.
# Rationale: when I switch to JITTER_AT_DEPTH_UNSAFE_MAINSTACK this will make the
# unspecialized case faster by avoiding a conditional.
instruction at-depth-to-register (?n 1 2 3 4 5 6 7 8 9 10, !R)
  code
    // FIXME: replace with JITTER_AT_DEPTH_UNSAFE_MAINSTACK after I implement
    // unsafe at-depth operations.
    JITTER_ARG1 = JITTER_AT_DEPTH_MAINSTACK(JITTER_ARGN0);
  end
end

instruction copy-to-register (!R)
  code
    JITTER_ARG0 = JITTER_TOP_MAINSTACK();
  end
end

instruction copy-from-register (?R)
  code
    JITTER_TOP_MAINSTACK() = JITTER_ARG0;
  end
end

instruction copy-from-literal (?n jitterlisp_print)
  code
    JITTER_TOP_MAINSTACK() = JITTER_ARGN0;
  end
end

instruction literal-to-register (?n jitterlisp_print, !R)
  code
    JITTER_ARG1 = JITTER_ARGN0;
  end
end

instruction register-to-register (?R, !R)
  code
    JITTER_ARG1 = JITTER_ARG0;
  end
end

# Do a check on the top element, fast-branching to the given error-handling
# routine in case of mismatch, and do nothing otherwise.
# Do *not* pop an operand from the stack.
# FIXME: factor into a common comment for checking instructions.
instruction check-closure (?f)
  code
#   ifndef JITTERLISP_UNSAFE
      jitterlisp_object top = JITTER_TOP_MAINSTACK();
      if (! JITTERLISP_IS_CLOSURE(top))
        JITTER_BRANCH_FAST(JITTER_ARGF0);
#   endif // #ifndef JITTERLISP_UNSAFE
  end
end

# Fast-branch to the given error label if the given argument, a tagged symbol,
# is not globally bound.
instruction check-global-defined (?n jitterlisp_print, ?f)
  code
#ifndef JITTERLISP_UNSAFE
    jitterlisp_object tagged_symbol = JITTER_ARGN0;
    struct jitterlisp_symbol * symbol = JITTERLISP_SYMBOL_DECODE(tagged_symbol);
    JITTER_BRANCH_FAST_IF_EQUAL (symbol->global_value, JITTERLISP_UNDEFINED,
                                 JITTER_ARGF1);
#endif // #ifndef JITTERLISP_UNSAFE
  end
end

# This versions is the one failing under no-threading.  I want to keep it for tests
# even if it's not the best implementation.
# # The fixnum argument is untagged.
instruction check-in-arity--alt (?n 0 1 2 3 4 5 6 7 8 9 10, ?f)
  code
    // FIXME: this loops forever with no-threading (not with the other dispatches
    // including minimal threading) when the callee is compiled.  A Jitter bug.
    /* Here we can assume that the top object is a closure, without checking: we either
       already performed a type check, or the compiler decided it wasn't necessary. */
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    struct jitterlisp_closure *c = JITTERLISP_CLOSURE_DECODE(top);

    /* FIXME: this is optimizable.  I should store the in-arity as a field, independently
       from the closure compiledness.  [Done] */
    if (JITTERLISP_IS_COMPILED_CLOSURE(top))
      {
        if (c->in_arity != JITTER_ARGN0)
          JITTER_BRANCH_FAST(JITTER_ARGF1);
      }
    else
      {
        /* The closure is interpreted.  Compute its in-arity.  We can assume that
           the formal list is well-formal, which is to say actually a proper list
           of distinct symbols. */
        struct jitterlisp_interpreted_closure *ic = & c->interpreted;
        jitterlisp_object rest = ic->formals;
        jitter_uint in_arity = 0;
        while (! JITTERLISP_IS_EMPTY_LIST(rest))
          {
            in_arity ++;
            rest = JITTERLISP_EXP_C_A_CDR(rest);
          }
        if (in_arity != JITTER_ARGN0)
          JITTER_BRANCH_FAST(JITTER_ARGF1);
      }
  end
end

instruction check-in-arity (?n 0 1 2 3 4 5 6 7 8 9 10, ?f)
  code
#   ifndef JITTERLISP_UNSAFE
      /* Here we can assume that the top object is a closure, without checking:
         we either already performed a type check, or the compiler decided it
         wasn't necessary. */
      jitterlisp_object top = JITTER_TOP_MAINSTACK();
      struct jitterlisp_closure *c = JITTERLISP_CLOSURE_DECODE(top);

      JITTER_BRANCH_FAST_IF_NOTEQUAL(c->in_arity, JITTER_ARGN0, JITTER_ARGF1);
#   endif // #ifndef JITTERLISP_UNSAFE
  end
end


instruction branch (?f)
  code
    JITTER_BRANCH_FAST(JITTER_ARGF0);
  end
end

instruction branch-if-false (?f)
  code
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
    JITTER_BRANCH_FAST_IF_EQUAL(top, JITTERLISP_FALSE, JITTER_ARGF0);
  end
end

instruction branch-if-true (?f)
  code
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
    JITTER_BRANCH_FAST_IF_NOTEQUAL(top, JITTERLISP_FALSE, JITTER_ARGF0);
  end
end

instruction branch-if-null (?f)
  code
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
    JITTER_BRANCH_FAST_IF_EQUAL(top, JITTERLISP_EMPTY_LIST, JITTER_ARGF0);
  end
end

instruction branch-if-not-null (?f)
  code
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
    JITTER_BRANCH_FAST_IF_NOTEQUAL(top, JITTERLISP_EMPTY_LIST, JITTER_ARGF0);
  end
end

instruction branch-if-positive (?f, ?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF1);
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
    JITTER_BRANCH_FAST_IF_POSITIVE(top, JITTER_ARGF0);
  end
end

instruction branch-if-non-positive (?f, ?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF1);
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
    JITTER_BRANCH_FAST_IF_NONPOSITIVE(top, JITTER_ARGF0);
  end
end

instruction branch-if-register-positive (?R, ?f, ?f)
  code
#if ! defined (JITTERLISP_UNSAFE)
    JITTERLISP_BRANCH_FAST_UNLESS_FIXNUM (JITTER_ARG0, JITTER_ARGF2);
#endif
    JITTER_BRANCH_FAST_IF_POSITIVE(JITTER_ARG0, JITTER_ARGF1);
  end
end

instruction branch-if-register-non-positive (?R, ?f, ?f)
  code
#if ! defined (JITTERLISP_UNSAFE)
    JITTERLISP_BRANCH_FAST_UNLESS_FIXNUM (JITTER_ARG0, JITTER_ARGF2);
#endif
    JITTER_BRANCH_FAST_IF_NONPOSITIVE(JITTER_ARG0, JITTER_ARGF1);
  end
end

instruction branch-if-register-null (?R, ?f)
  code
    JITTER_BRANCH_FAST_IF_EQUAL(JITTER_ARG0,
                                JITTERLISP_EMPTY_LIST,
                                JITTER_ARGF1);
  end
end

instruction branch-if-register-not-null (?R, ?f)
  code
    JITTER_BRANCH_FAST_IF_NOTEQUAL(JITTER_ARG0,
                                   JITTERLISP_EMPTY_LIST,
                                   JITTER_ARGF1);
  end
end

instruction one-minus-register (?R, !R, ?f)
  code
#if ! defined (JITTERLISP_UNSAFE)
    JITTERLISP_BRANCH_FAST_UNLESS_FIXNUM (JITTER_ARG0, JITTER_ARGF2);
#endif
    JITTERLISP_1MINUS_OR_OVERFLOW_(JITTER_ARG1,
                                   JITTER_ARG0,
                                   JITTER_ARGF2);
  end
end

instruction one-plus-register (?R, !R, ?f)
  code
#if ! defined (JITTERLISP_UNSAFE)
    JITTERLISP_BRANCH_FAST_UNLESS_FIXNUM (JITTER_ARG0, JITTER_ARGF2);
#endif
    JITTERLISP_1PLUS_OR_OVERFLOW_(JITTER_ARG1,
                                  JITTER_ARG0,
                                  JITTER_ARGF2);
  end
end

instruction cdr-register (?R, !R, ?f)
  code
#if ! defined (JITTERLISP_UNSAFE)
    JITTERLISP_BRANCH_FAST_UNLESS_CONS (JITTER_ARG0, JITTER_ARGF2);
#endif
    JITTERLISP_CDR_(JITTER_ARG1,
                    JITTER_ARG0);
  end
end

instruction canonicalize-boolean ()
  code
    JITTERLISP_BOOLEAN_CANONICALIZE_(JITTER_TOP_MAINSTACK(),
                                     JITTER_TOP_MAINSTACK());
  end
end

# The first argument is a primitive function pointer.
# The second is its in-arity.
# instruction primitive (?n, ?n 0 1 2 3 4, ?l)
#  non-relocatable
instruction primitive (?n, ?n 0 1 2 3 4, ?f)
  code
    /* The error-handling label is not actually used in this case: the
       primitive function, written in C, has its own handling.  It's
       harmless to have it anyway, as it makes the C code generator
       more uniform. */
    jitterlisp_primitive_function p
      = (jitterlisp_primitive_function) JITTER_ARGP0;
    const jitter_uint in_arity = JITTER_ARGN1;
    JITTER_PUSH_UNSPECIFIED_MAINSTACK();
    //asm volatile ("nop");
    jitterlisp_object *first_arg
      // FIXME: add a stack operator to compute this address.
      = (& JITTER_UNDER_TOP_MAINSTACK()) - in_arity + 1;
    jitterlisp_object res = JITTER_CALL_C (p, first_arg);

    jitter_uint i;
    // Remove as many elements as the primitive in-arity, but not more:
    // the top unspecified value we pushed will be replaced with the result.
    for (i = 0; i < in_arity; i ++)
      JITTER_NIP_MAINSTACK();
    //asm ("unop" : "+g" (jitter_state_runtime));
    JITTER_TOP_MAINSTACK() = res;
  end
end

instruction primitive-car (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(CONS, JITTER_ARGF0);
    JITTERLISP_CAR_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-cdr (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(CONS, JITTER_ARGF0);
    JITTERLISP_CDR_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction heap-allocate (?n 4 8 12 16 24 32 36 48 52 64)
  code
#ifdef JITTER_GC_STUB
    /* FIXME: I might want to merge this, along with the body of other
       instructions, into a macro.  This may still be factored in a different
       way, but the idea looks correct and promising. */
    /* Bump the allocation pointer, unconditionally.  Another instruction
       will check if it went out of the nursery. */
    const size_t allocation_size = JITTER_ARGN0;
    JITTERLISPVM_STATE_RUNTIME_FIELD (allocation_next) += allocation_size;
#else
    /* Nothing to do.  With the ordinary non-stub heap all the actual
       memory allocation happens in primitive-cons-special. */
#endif //#ifdef JITTER_GC_STUB
  end
end

instruction gc-if-needed (?f)
  code
#ifdef JITTER_GC_STUB
    /* FIXME: I might want to merge this, along with the body of other
       instructions, into a macro.  This may still be factored in a different
       way, but the idea looks correct and promising. */
    /* Branch to the slow path of the allocation, which includes a minor GC,
       in case the allocation pointer is now out of the nursery.  Do nothing
       otherwise.
       Rationale: separating branches from instructions having effect on the VM
       state prevents defective instructions; even if I have reason to believe
       that this particular state effect (unconditionally incrementing a runtime
       field) is not problematic with respect to defects, I will need to seriously
       stress the system after defect replacement is really implemented. */
    JITTER_BRANCH_FAST_IF_NOTLESS_UNSIGNED
       (JITTERLISPVM_STATE_RUNTIME_FIELD (allocation_next),
        JITTERLISPVM_STATE_RUNTIME_FIELD (allocation_limit),
        JITTER_ARGF0);
#else
    /* Nothing to do.  With the ordinary non-stub heap all the actual
       memory allocation happens in primitive-cons-special. */
#endif //#ifdef JITTER_GC_STUB
  end
end

# This doesn't nip, in the hope that the separate nip instruction generated
# right after this will be rewritten.
instruction primitive-cons-special ()
  code
#ifdef JITTER_GC_STUB
    /* This is a preliminary version of the allocation fast path, using a
       still non-existing Jitter garbage collector.
       Of course most of this should be factored into a macro. */
    const size_t allocation_size
      = JITTER_BYTES_PER_WORD * 2;
    const size_t header_size = 0;
    jitterlisp_object tagged_cons
      = JITTERLISP_CONS_ENCODE
          (JITTERLISPVM_STATE_RUNTIME_FIELD (allocation_next)
           - allocation_size + header_size);
    JITTERLISP_CONS_DECODE (tagged_cons)->car = JITTER_UNDER_TOP_MAINSTACK();
    JITTERLISP_CONS_DECODE (tagged_cons)->cdr = JITTER_TOP_MAINSTACK();
    JITTER_TOP_MAINSTACK() = tagged_cons;
#else // ! JITTER_GC_STUB
    JITTERLISP_CONS_(JITTER_TOP_MAINSTACK(),
                     JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
#endif //#ifdef JITTER_GC_STUB
  end
end

# The two VM instructions for the set-car! and set-cdr! operations do not
# consume any operand from the stack, and so are followed by other VM
# instructions to adjust the stack and set the TOS in compiled code.  Such
# separate instructions can often be rewritten away.
instruction primitive-set-carb-special (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(CONS, ANYTHING, JITTER_ARGF0);
    jitterlisp_object useless __attribute__ ((unused));
    JITTERLISP_SET_CARB_(useless,
                         JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    /* Leave the two operands on the stack. */
  end
end
instruction primitive-set-cdrb-special (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(CONS, ANYTHING, JITTER_ARGF0);
    jitterlisp_object useless __attribute__ ((unused));
    JITTERLISP_SET_CDRB_(useless,
                         JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    /* Leave the two operands on the stack. */
  end
end

instruction primitive-box ()
  code
    JITTERLISP_BOX_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-box-get (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(BOX, JITTER_ARGF0);
    JITTERLISP_BOX_GET_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

# See the comment above for set-car! and set-cdr! .  The same performance
# considerations hold for box-set! .
instruction primitive-box-setb-special (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(BOX, ANYTHING, JITTER_ARGF0);
    jitterlisp_object useless __attribute__ ((unused));
    JITTERLISP_BOX_SETB_(useless,
                         JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    /* Leave the two operands on the stack. */
  end
end

instruction primitive-eqp ()
  code
    JITTERLISP_EQP_(JITTER_TOP_MAINSTACK(),
                    JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-not-eqp ()
  code
    JITTERLISP_NOT_EQP_(JITTER_TOP_MAINSTACK(),
                        JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-fixnum-eqp (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_EQP_(JITTER_TOP_MAINSTACK(),
                    JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-fixnum-not-eqp (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_NOT_EQP_(JITTER_TOP_MAINSTACK(),
                        JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-zerop (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_ZEROP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-non-zerop (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_NON_ZEROP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-positivep (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_POSITIVEP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-non-positivep (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_NON_POSITIVEP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-negativep (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_NEGATIVEP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-non-negativep (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_NON_NEGATIVEP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-lessp (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_LESSP_(JITTER_TOP_MAINSTACK(),
                      JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-greaterp (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_GREATERP_(JITTER_TOP_MAINSTACK(),
                         JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-not-lessp (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_NOTLESSP_(JITTER_TOP_MAINSTACK(),
                         JITTER_UNDER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-not-greaterp (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_NOTGREATERP_(JITTER_TOP_MAINSTACK(),
                            JITTER_UNDER_TOP_MAINSTACK(),
                            JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-one-plus (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_1PLUS_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                  JITTER_TOP_MAINSTACK(),
                                  JITTER_ARGF0);
  end
end

instruction primitive-one-minus (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_1MINUS_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                   JITTER_TOP_MAINSTACK(),
                                   JITTER_ARGF0);
  end
end

instruction primitive-two-times (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_2TIMES_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                   JITTER_TOP_MAINSTACK(),
                                   JITTER_ARGF0);
  end
end

instruction primitive-two-divided (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_2DIVIDED_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-two-quotient (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_2QUOTIENT_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-two-remainder (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_2REMAINDER_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-negate (?f)
  code
    JITTERLISPVM_CHECK_TYPES_1(FIXNUM, JITTER_ARGF0);
    JITTERLISP_NEGATE_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                   JITTER_TOP_MAINSTACK(),
                                   JITTER_ARGF0);
  end
end

instruction primitive-primordial-plus (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_PLUS_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                 JITTER_UNDER_TOP_MAINSTACK(),
                                 JITTER_TOP_MAINSTACK(),
                                 JITTER_ARGF0);
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-primordial-minus (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_MINUS_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                  JITTER_UNDER_TOP_MAINSTACK(),
                                  JITTER_TOP_MAINSTACK(),
                                  JITTER_ARGF0);
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-primordial-times (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_TIMES_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                  JITTER_UNDER_TOP_MAINSTACK(),
                                  JITTER_TOP_MAINSTACK(),
                                  JITTER_ARGF0);
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-primordial-divided (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_DIVIDED_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                    JITTER_UNDER_TOP_MAINSTACK(),
                                    JITTER_TOP_MAINSTACK(),
                                    JITTER_ARGF0);
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-quotient (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_QUOTIENT_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                     JITTER_UNDER_TOP_MAINSTACK(),
                                     JITTER_TOP_MAINSTACK(),
                                     JITTER_ARGF0);
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-remainder (?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF0);
    JITTERLISP_REMAINDER_OR_OVERFLOW_(JITTER_TOP_MAINSTACK(),
                                      JITTER_UNDER_TOP_MAINSTACK(),
                                      JITTER_TOP_MAINSTACK(),
                                      JITTER_ARGF0);
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-primordial-divided-unsafe (?f)
  code
    /* The second argument has already been validated if we are using this. */
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, ANYTHING, JITTER_ARGF0);
    JITTERLISP_DIVIDED_UNSAFE_(JITTER_TOP_MAINSTACK(),
                               JITTER_UNDER_TOP_MAINSTACK(),
                               JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-quotient-unsafe (?f)
  code
    /* The second argument has already been validated if we are using this. */
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, ANYTHING, JITTER_ARGF0);
    JITTERLISP_QUOTIENT_UNSAFE_(JITTER_TOP_MAINSTACK(),
                               JITTER_UNDER_TOP_MAINSTACK(),
                               JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-remainder-unsafe (?f)
  code
    /* The second argument has already been validated if we are using this. */
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, ANYTHING, JITTER_ARGF0);
    JITTERLISP_REMAINDER_UNSAFE_(JITTER_TOP_MAINSTACK(),
                                 JITTER_UNDER_TOP_MAINSTACK(),
                                 JITTER_TOP_MAINSTACK());
    JITTER_NIP_MAINSTACK();
  end
end

instruction primitive-nullp ()
  code
    JITTERLISP_NULLP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-non-nullp ()
  code
    JITTERLISP_NON_NULLP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-nothingp ()
  code
    JITTERLISP_NOTHINGP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-fixnump ()
  code
    JITTERLISP_FIXNUMP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-characterp ()
  code
    JITTERLISP_CHARACTERP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-uniquep ()
  code
    JITTERLISP_UNIQUEP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-consp ()
  code
    JITTERLISP_CONSP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-non-consp ()
  code
    JITTERLISP_NON_CONSP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-symbolp ()
  code
    JITTERLISP_SYMBOLP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-non-symbolp ()
  code
    JITTERLISP_NON_SYMBOLP_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-not ()
  code
    JITTERLISP_NOT_(JITTER_TOP_MAINSTACK(), JITTER_TOP_MAINSTACK());
  end
end

instruction primitive-boolean-canonicalize ()
  code
    JITTERLISP_BOOLEAN_CANONICALIZE_(JITTER_TOP_MAINSTACK(),
                                     JITTER_TOP_MAINSTACK());
  end
end

# Take a compiled closure (no type checking), its arguments already evaluated,
# and the number of arguments (untagged) on the top of stack.  Replace all the
# stack operands with the call result.
# Rationale: this instruction has been designed to be the first and only
# instruction executed in the driver VM routine, called from the AST interpreter
# in C.
# The same driver routine can be generated once at initialization, and then
# reused to launch procedures as needed: this is why the instruction takes
# the closure in-arity from the stack instead of from an instruction argument.
instruction call-from-c ()
  caller
  code
    /* Get a number (untagged) from the stack top, so that I know at which
       depth to find the closure.  The arity check must have been
       performed already on the interpreter side. */
    const jitter_uint in_arity_plus_one = JITTER_TOP_MAINSTACK();

    /* What follows is similar to the call instruction in the case of a compiled
       callee.  Shall I factor the common code with a macro? */

    /* Decode the closure. */
    jitterlisp_object callee
      = JITTER_AT_NONZERO_DEPTH_MAINSTACK(in_arity_plus_one);
    struct jitterlisp_closure *c = JITTERLISP_CLOSURE_DECODE(callee);
    struct jitterlisp_compiled_closure *cc = & c->compiled;

    /* Drop the number of arguments. */
    JITTER_DROP_MAINSTACK();

    /* Make place for the return address in the return stack.  The
       actual value will be written by the callee. */
    JITTER_PUSH_UNSPECIFIED_RETURNSTACK();

    /* Branch-and-link.  This follows the ordinary calling convention,
       with the main stack containing the closure and its actuals, known
       to be in the correct number.  The next VM instruction will be a
       prolog saving the link; then execution will fall thru into the
       compiled closure body. */
    JITTER_BRANCH_AND_LINK(cc->first_program_point);
  end
end

# This is the generic code working for both compiled and interpreted closures.
# FIXME: factor with the other call instructions.
instruction call (?n 0 1 2 3 4 5 6 7 8 9 10)
  caller
  code
    const jitter_uint in_arity = JITTER_ARGN0;
    jitterlisp_object callee = JITTER_AT_DEPTH_MAINSTACK(in_arity);
    struct jitterlisp_closure *c = JITTERLISP_CLOSURE_DECODE(callee);

    if (c->kind == jitterlisp_closure_type_compiled)
      {
        /* Make place for the return address in the return stack.  The
           actual value will be written by the callee. */
        JITTER_PUSH_UNSPECIFIED_RETURNSTACK();

        /* Branch-and-link to the native code, whose first instruction
           will be a prolog. */
        struct jitterlisp_compiled_closure *cc = & c->compiled;
        JITTER_BRANCH_AND_LINK(cc->first_program_point);
      }
    else
      {
        const struct jitterlisp_interpreted_closure *ic = & c->interpreted;

        /* Push an element on the main stack.  This way we can ensure that
           every actual is accessible thru memory in a contiguous array,
           even if the main stack is TOS-optimized.  Compute the address
           (on the main stack backing) where the actuals begin.

           Notice that the interpreted function we are calling is allowed
           to call into compiled code in its turn.  This is not a problem,
           as we are not moving stack pointers down until the call ends:
           the unused part of the main and return stacks is available to
           the callee. */
        JITTER_PUSH_UNSPECIFIED_MAINSTACK();
        jitterlisp_object *actuals
          // FIXME: add a stack operator to compute this address.
          = (& JITTER_UNDER_TOP_MAINSTACK()) - in_arity + 1;

        /* Call the interpreter. */
        jitterlisp_object interpretation_result
          = jitterlisp_call_interpreted (ic, actuals, in_arity);

        /* Remove as many elements as the in-arity plus one (the callee) from
           the stack, without affecting the top.  Unless JITTER_ARGN0 is
           residual this whole loops compiles to one instruction when the main
           stack is TOS-optimized, and doesn't touch memory. */
        int i;
        for (i = 0; i < in_arity + 1; i ++)
          JITTER_NIP_MAINSTACK();

        /* Now the stack is one element higher than it was before the call,
           containing the unspecified element at the top.  Replace it with
           the result.  Again this doesn't touch memory. */
        JITTER_TOP_MAINSTACK() = interpretation_result;
      }
  end
end

# FIXME: factor with the other call instructions.
instruction call-compiled (?n 0 1 2 3 4 5 6 7 8 9 10)
  caller
  code
    const jitter_uint in_arity = JITTER_ARGN0;
    jitterlisp_object callee = JITTER_AT_DEPTH_MAINSTACK(in_arity);
    struct jitterlisp_closure *c = JITTERLISP_CLOSURE_DECODE(callee);

    /* Here we can assume that * c is a compiled closure without checking. */
    struct jitterlisp_compiled_closure *cc = & c->compiled;

    /* Make place for the return address in the return stack.  The
       actual value will be written by the callee. */
    JITTER_PUSH_UNSPECIFIED_RETURNSTACK();
//    JITTER_TOP_RETURNSTACK() = 0x4242aaaa; // FIXME: just a test.  Remove.

    /* Branch-and-link to the native code, whose first instruction
       will be a prolog. */
    JITTER_BRANCH_AND_LINK(cc->first_program_point);
  end
end

# FIXME: factor with the other call instructions.
instruction tail-call (?n 0 1 2 3 4 5 6 7 8 9 10)
  caller
  returning
  code
    const jitter_uint in_arity = JITTER_ARGN0;
    jitterlisp_object callee = JITTER_AT_DEPTH_MAINSTACK(in_arity);
    struct jitterlisp_closure *c = JITTERLISP_CLOSURE_DECODE(callee);

    if (c->kind == jitterlisp_closure_type_compiled)
      {
        /* Extract the current return address from the return stack, to be
           reused.  Differently from non-tail calls, don't push anything on
           the return stack: the callee will overwrite the top with its
           current value, which is what we want. */
        jitterlispvm_program_point current_link
          = (jitterlispvm_program_point) JITTER_TOP_RETURNSTACK();

        /* Branch-and-relink to the native code, whose first instruction
           will be a prolog. */
        struct jitterlisp_compiled_closure *cc = & c->compiled;
        JITTER_BRANCH_AND_LINK_WITH(cc->first_program_point, current_link);
      }
    else
      {
        /* Unfortunately I cannot really tail-call from compiled code to
           interpreted code.  Instead I will call the interpreter as a C
           function and then return. */
        const struct jitterlisp_interpreted_closure *ic = & c->interpreted;

        /* Push an element on the main stack.  This way we can ensure that
           every actual is accessible thru memory in a contiguous array,
           even if the main stack is TOS-optimized.  Compute the address
           (on the main stack backing) where the actuals begin.

           Notice that the interpreted function we are calling is allowed
           to call into compiled code in its turn.  This is not a problem,
           as we are not moving stack pointers down until the call ends:
           the unused part of the main and return stacks is available to
           the callee. */
        JITTER_PUSH_UNSPECIFIED_MAINSTACK();
        jitterlisp_object *actuals
          // FIXME: add a stack operator to compute this address.
          = (& JITTER_UNDER_TOP_MAINSTACK()) - in_arity + 1;

        /* Call the interpreter. */
        jitterlisp_object interpretation_result
          = jitterlisp_call_interpreted (ic, actuals, in_arity);

        /* Remove as many elements as the in-arity plus one (the callee) from
           the stack, without affecting the top.  Unless JITTER_ARGN0 is
           residual this whole loops compiles to one instruction when the main
           stack is TOS-optimized, and doesn't touch memory. */
        int i;
        for (i = 0; i < in_arity + 1; i ++)
          JITTER_NIP_MAINSTACK();

        /* Now the stack is one element higher than it was before the call,
           containing the unspecified element at the top.  Replace it with
           the result.  Again this doesn't touch memory. */
        JITTER_TOP_MAINSTACK() = interpretation_result;

        /* Return to our original caller. */
        jitter_uint return_address = JITTER_TOP_RETURNSTACK();
        JITTER_DROP_RETURNSTACK();
        JITTER_RETURN(return_address);
      }
  end
end

# FIXME: factor with the other call instructions.
instruction tail-call-compiled (?n 0 1 2 3 4 5 6 7 8 9 10)
  caller
  code
    const jitter_uint in_arity = JITTER_ARGN0;
    jitterlisp_object callee = JITTER_AT_DEPTH_MAINSTACK(in_arity);
    struct jitterlisp_closure *c = JITTERLISP_CLOSURE_DECODE(callee);

    /* Here we can assume that * c is a compiled closure without checking. */
    struct jitterlisp_compiled_closure *cc = & c->compiled;

    /* Take the current return address from the return stack: I want to keep
       returning there.  Differently from the case of non-tail calls I don't
       push anything on the return stack: the callee prolog will overwrite
       the current top with a copy of itself, which is fine. */
    jitterlispvm_program_point current_link
      = (jitterlispvm_program_point) JITTER_TOP_RETURNSTACK();

    /* Branch-and-relink to the native code, whose first instruction
       will be a prolog. */
    JITTER_BRANCH_AND_LINK_WITH(cc->first_program_point, current_link);
  end
end

instruction procedure-prolog ()
  callee
  code
    /* Fill the return stack slot with the return address.  The return
       stack has already been pushed (with an unspecified value on the
       top) by the caller. */
    JITTER_TOP_RETURNSTACK() = (jitterlisp_object) JITTER_LINK;
  end
end

instruction return ()
  returning
  code
    jitter_uint return_address = JITTER_TOP_RETURNSTACK();
    JITTER_DROP_RETURNSTACK();
    JITTER_RETURN(return_address);
  end
end

instruction save-register (?R)
  code
    JITTER_PUSH_RETURNSTACK(JITTER_ARG0);
  end
end

instruction restore-register (!R)
  code
    JITTER_ARG0 = JITTER_TOP_RETURNSTACK();
    JITTER_DROP_RETURNSTACK();
  end
end

instruction fail ()
  non-relocatable
  cold
  code
    jitterlisp_fail_from_vm ();
  end
end




## Optimization rewrites.
#################################################################

rule pop-to-register-push-register rewrite
  pop-to-register $a; push-register $b
into
  copy-to-register $a; copy-from-register $b
end
rule pop-to-register-push-literal rewrite
  pop-to-register $a; push-literal $b
into
  copy-to-register $a; copy-from-literal $b
end

rule push-literal-pop-to-register rewrite
  push-literal $a; pop-to-register $b
into
  literal-to-register $a, $b
end

rule drop-push-register rewrite
  drop; push-register $a
into
  copy-from-register $a
end
rule drop-push-literal rewrite
  drop; push-literal $a
into
  copy-from-literal $a
end
rule drop-push-unspecified rewrite
  drop; push-unspecified
into
  # Nothing.
end

rule push-register-pop-to-register rewrite
  push-register $a; pop-to-register $b
into
  register-to-register $a, $b
end

rule copy-from-register-pop-to-register rewrite
  copy-from-register $a; pop-to-register $b
into
  register-to-register $a, $b; drop
end

rule copy-from-literal-pop-to-register rewrite
  copy-from-literal $a; pop-to-register $b
into
  literal-to-register $a, $b; drop
end

rule push-register-push-register rewrite
  push-register $a; push-register $a
into
  push-register $a; dup
end

rule copy-from-register-push-register rewrite
  copy-from-register $a; push-register $a
into
  copy-from-register $a; dup
end

rule copy-to-register-and-push-the-same-register rewrite
  copy-to-register $a; push-register $a
into
  copy-to-register $a; dup
end

rule copy-to-and-from-the-same-register rewrite
  copy-to-register $a; copy-from-register $a
into
  copy-to-register $a
end

rule push-the-same-literal-twice rewrite
  push-literal $a; push-literal $a
into
  push-literal $a; dup
end

rule copy-from-literal-and-push-the-same-literal rewrite
  copy-from-literal $a; push-literal $a
into
  copy-from-literal $a; dup
end

# Remove the first instruction in a two-instruction sequence made of copy-from
# instructions.
rule copy-from-literal-then-from-another-literal rewrite
  copy-from-literal $a; copy-from-literal $b
into
  copy-from-literal $b
end
rule copy-from-literal-then-from-a-register rewrite
  copy-from-literal $a; copy-from-register $b
into
  copy-from-register $b
end
rule copy-from-a-register-then-from-literal rewrite
  copy-from-register $a; copy-from-literal $b
into
  copy-from-literal $b
end
rule copy-from-a-register-then-from-another-register rewrite
  copy-from-register $a; copy-from-register $b
into
  copy-from-register $b
end

# A copy-from instruction is useless when immediately followed by a drop.
rule useless-copy-from-literal-elimination rewrite
  copy-from-literal $a; drop
into
  drop
end
rule useless-copy-from-register-elimination rewrite
  copy-from-register $a; drop
into
  drop
end

rule push-register-drop rewrite
  push-register $a; drop
into
  # Nothing.
end
rule push-literal-drop rewrite
  push-literal $a; drop
into
  # Nothing.
end

rule pop-to-register-copy-from-register rewrite
  pop-to-register $a; copy-from-register $a
into
  copy-to-register $a; nip
end
rule pop-to-register-drop rewrite
  pop-to-register $a; drop
into
  nip; pop-to-register $a
end

rule pop-to-register-nip rewrite
  pop-to-register $a; nip
into
  copy-to-register $a; drop-nip
end

rule drop-nip rewrite
  drop; nip
into
  drop-nip
end

rule nip-drop rewrite
  nip; drop
into
  nip-drop
end

rule drop-drop rewrite
  drop; drop
into
  nip-drop
end

rule nip-drop-drop rewrite
  nip-drop; drop
into
  nip-two-drop
end
rule nip-two-drop-drop rewrite
  nip-two-drop; drop
into
  nip-three-drop
end
rule nip-three-drop-drop rewrite
  nip-three-drop; drop
into
  nip-four-drop
end
rule nip-four-drop-drop rewrite
  nip-four-drop; drop
into
  nip-five-drop
end
rule nip-five-drop-drop rewrite
  nip-five-drop; drop
into
  nip-six-drop
end

# Combine consecutive nip instructions into single instructions.  This
# implementation will become nicer and more general when Jitter rewrite rules
# become more expressive.
rule nip-nip rewrite
  nip; nip
into
  nip-two
end
rule nip-two-nip rewrite
  nip-two; nip
into
  nip-three
end
rule nip-three-nip rewrite
  nip-three; nip
into
  nip-four
end
rule nip-four-nip rewrite
  nip-four; nip
into
  nip-five
end
rule nip-five-nip rewrite
  nip-five; nip
into
  nip-six
end

# Move nip instructions before copy-from instructions.  This will make it
# easier to rewrite them into a single nip-multiple instruction.
rule nip-before-copy-from-register rewrite
  copy-from-register $a; nip
into
  nip; copy-from-register $a
end
rule nip-before-copy-from-literal rewrite
  copy-from-literal $a; nip
into
  nip; copy-from-literal $a
end

# Having copy-to-register at the end might make it possible to combine
# with some other instruction; nip is not easy to combine, except with
# other nips..
rule copy-to-register-nip rewrite
  copy-to-register $a; nip
into
  nip; copy-to-register $a
end

rule pop-to-register-return rewrite
  pop-to-register $a; return
into
  drop; return
end

rule copy-to-register-return rewrite
  copy-to-register $a; return
into
  return
end

# The AST rewriter takes care of removing most "not; branch-if-*" sequences,
# including every use of "not" as a condition and every use of
# "boolean-canonicalize" as a guard or condition; however
# "not; branch-if-true" sequnces remain as while guards, where the branch is
# always on a true condiion (I compile while loops as do..while, with the
# conditional branch at the end).  It's easier to optimize this pattern here
# than to add a special case to the compiler.
rule not-branch-if-true rewrite
  primitive-not; branch-if-true $b
into
  branch-if-false $b
end

rule nullp-branch-if-true rewrite
  primitive-nullp; branch-if-true $a
into
  branch-if-null $a
end
rule nullp-branch-if-false rewrite
  primitive-nullp; branch-if-false $a
into
  branch-if-not-null $a
end

rule non-nullp-branch-if-true rewrite
  primitive-non-nullp; branch-if-true $a
into
  branch-if-not-null $a
end
rule non-nullp-branch-if-false rewrite
  primitive-non-nullp; branch-if-false $a
into
  branch-if-null $a
end

rule positivep-branch-if-true rewrite
  primitive-positivep $a; branch-if-true $b
into
  branch-if-positive $b, $a
end
rule positivep-branch-if-false rewrite
  primitive-positivep $a; branch-if-false $b
into
  branch-if-non-positive $b, $a
end

rule push-register-branch-if-positive rewrite
  push-register $a; branch-if-positive $b, $c
into
  branch-if-register-positive $a, $b, $c
end
rule push-register-branch-if-non-positive rewrite
  push-register $a; branch-if-non-positive $b, $c
into
  branch-if-register-non-positive $a, $b, $c
end

rule push-register-branch-if-null rewrite
  push-register $a; branch-if-null $b
into
  branch-if-register-null $a, $b
end
rule push-register-branch-if-non-null rewrite
  push-register $a; branch-if-not-null $b
into
  branch-if-register-not-null $a, $b
end

# These rules optimise the common pattern in which a register
# value's successor, predecessor or cdr is written into another
# (or the same) register.
rule push-register-one-minus-pop-to-register rewrite
  push-register $a; primitive-one-minus $b; pop-to-register $c
into
  one-minus-register $a, $c, $b
end
rule push-register-one-plus-pop-to-register rewrite
  push-register $a; primitive-one-plus $b; pop-to-register $c
into
  one-plus-register $a, $c, $b
end
rule push-register-cdr-pop-to-register rewrite
  push-register $a; primitive-cdr $b; pop-to-register $c
into
  cdr-register $a, $c, $b
end

# # Some save/restore pairs are easy to eliminate.
# # FIXME: no, this never fires because of the implicit label after the call,
# # which makes the sequence non-rewritable.  Do I really want this behavior?
# # I suspect not.  [FIXME: the behavior is no longer there.]
# rule rewrite
#   save-register $a; call $b; restore-register $a; primitive-primordial-plus $c; return
# into
#   call $b; primitive-primordial-plus $c; return
# end

# Some useless restore/save pairs come from compiling nested non-tail procedure calls.
# Notice that this fires multiple times with multiple nested pairs such as
#   restore-register %r1; restore-register %r0; save-register %r0; save-register %r1
# , until every pair has been eliminated.
rule restore-register-then-save-the-same-register rewrite
  restore-register $a; save-register $a
into
  # Nothing.
end


# # FIXME: this is a temporary kludge, to be used before I actually implement
# # tail calls.
# rule remove-tail-call--kludge rewrite
#   tail-call $a
# into
#   call $a; return
# end
# rule remove-tail-call-compiled--kludge rewrite
#   tail-call-compiled $a
# into
#   call-compiled $a; return
# end




## Scratch.
#################################################################

instruction branch-if-register-non-zero (?R, ?f, ?f)
  code
#if ! defined (JITTERLISP_UNSAFE)
    JITTERLISP_BRANCH_FAST_UNLESS_FIXNUM (JITTER_ARG0, JITTER_ARGF2);
#endif
    JITTER_BRANCH_FAST_IF_NOTEQUAL(JITTER_ARG0, JITTERLISP_FIXNUM_ENCODE(0),
                                   JITTER_ARGF1);
  end
end

rule scratch rewrite
  push-register $a; primitive-non-zerop $b; branch-if-true $c
into
  branch-if-register-non-zero $a, $c, $b
end

instruction branch-if-not-less (?f, ?f)
  code
    JITTERLISPVM_CHECK_TYPES_2(FIXNUM, FIXNUM, JITTER_ARGF1);
    jitterlisp_object undertop = JITTER_UNDER_TOP_MAINSTACK();
    jitterlisp_object top = JITTER_TOP_MAINSTACK();
    JITTER_NIP_MAINSTACK();
    JITTER_DROP_MAINSTACK();
    JITTER_BRANCH_FAST_IF_NOTLESS_SIGNED(undertop, top, JITTER_ARGF0);
  end
end
# FIXME: write more rules like this one.
# rule rewrite
#   primitive-lessp $a; branch-if-false $b
# into
#   branch-if-not-less $b, $a
# end

rule nip-drop-push-literal rewrite
  nip-drop; push-literal $a
into
  nip; copy-from-literal $a
end
rule nip-drop-push-register rewrite
  nip-drop; push-register $a
into
  nip; copy-from-register $a
end
rule nip-two-drop-push-literal rewrite
  nip-two-drop; push-literal $a
into
  nip-two; copy-from-literal $a
end
rule nip-two-drop-push-register rewrite
  nip-two-drop; push-register $a
into
  nip-two; copy-from-register $a
end
rule nip-three-drop-push-literal rewrite
  nip-three-drop; push-literal $a
into
  nip-three; copy-from-literal $a
end
rule nip-three-drop-push-register rewrite
  nip-three-drop; push-register $a
into
  nip-three; copy-from-register $a
end
rule nip-four-drop-push-literal  rewrite
  nip-four-drop; push-literal $a
into
  nip-four; copy-from-literal $a
end
rule nip-four-drop-push-register rewrite
  nip-four-drop; push-register $a
into
  nip-four; copy-from-register $a
end
rule nip-five-drop-push-literal rewrite
  nip-five-drop; push-literal $a
into
  nip-five; copy-from-literal $a
end
rule nip-five-drop-push-register rewrite
  nip-five-drop; push-register $a
into
  nip-five; copy-from-register $a
end
rule nip-six-drop-push-literal rewrite
  nip-six-drop; push-literal $a
into
  nip-six; copy-from-literal $a
end
rule nip-six-drop-push-register rewrite
  nip-six-drop; push-register $a
into
  nip-six; copy-from-register $a
end

# For these rule to fire the cons primitive use must be in a non-tail position,
# used as a non-last operand of a primitive or procedure.
# Testcases: alist-copy zip-reversed-iterative zip-reversed-tail-recursive-helper
#            zip-non-tail-recursive unzip-non-tail-recursive .
instruction nip-push-literal (?n)
  code
    JITTER_UNDER_TOP_MAINSTACK() = JITTER_TOP_MAINSTACK();
    JITTER_TOP_MAINSTACK() = JITTER_ARGN0;
  end
end
instruction nip-push-register (?R)
  code
    JITTER_UNDER_TOP_MAINSTACK() = JITTER_TOP_MAINSTACK();
    JITTER_TOP_MAINSTACK() = JITTER_ARG0;
  end
end
rule rewrite
  nip; push-literal $a
into
  nip-push-literal $a
end
rule rewrite
  nip; push-register $a
into
  nip-push-register $a
end

# Test case: (lambda () (while #t (cons 1 2)))
rule useless-cons-elimination rewrite
  heap-allocate $a; gc-if-needed $f; primitive-cons-special; nip-drop
into
  # Don't cons at all and drop the two cons operands as well, which will
  # hopefully rewrite further.  I don't generate just one nip-drop instruction
  # because the first rewritten drop might already rewrite along with its
  # previous instruction; and so may the second.
  drop; drop
end
Generated by cgit on ageinghacker.net.
I am Luca Saiu. If you have reason to request commit access to one of these repositories please contact me. You may also send me patches by email.