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
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
|
/* Execution of byte code produced by bytecomp.el.
Copyright (C) 2018 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs 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.
GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#ifdef HAVE_LIBJIT
#include "lisp.h"
#include "buffer.h"
#include "bytecode.h"
#include "window.h"
#ifndef HAVE_OPEN_MEMSTREAM
#include "sysstdio.h"
#endif
#include <stdio.h>
#include <jit/jit.h>
#include <jit/jit-dump.h>
#ifdef WINDOWSNT
# include <windows.h>
# include "w32common.h"
# include "w32.h"
/* Define a variable that will be loaded from a DLL. */
#define DEF_DLL_VAR(type, var) static type *pv_##var
/* Load a variable from the DLL. */
#define LOAD_DLL_VAR(lib, var) \
do \
{ \
pv_##var = (void *) GetProcAddress (lib, #var); \
if (!pv_##var) \
return false; \
} \
while (false)
DEF_DLL_FN (jit_value_t, jit_value_create_nint_constant,
(jit_function_t, jit_type_t, jit_nint));
# if EMACS_INT_MAX > LONG_MAX
DEF_DLL_FN (jit_value_t, jit_value_create_long_constant,
(jit_function_t, jit_type_t, jit_long));
# endif
DEF_DLL_FN (void, jit_insn_store, (jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_and,
(jit_function_t, jit_value_t, jit_value_t));
# if !USE_LSB_TAG
DEF_DLL_FN (jit_value_t, jit_insn_ushr,
(jit_function_t, jit_value_t, jit_value_t));
# endif
DEF_DLL_FN (jit_value_t, jit_insn_sub,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_sshr,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_eq,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (int, jit_insn_branch_if_not,
(jit_function_t, jit_value_t, jit_label_t *));
DEF_DLL_FN (int, jit_insn_branch_if,
(jit_function_t, jit_value_t, jit_label_t *));
DEF_DLL_FN (int, jit_insn_branch, (jit_function_t, jit_label_t *));
DEF_DLL_FN (void, jit_insn_label, (jit_function_t, jit_label_t *));
DEF_DLL_FN (jit_value_t, jit_insn_call_native,
(jit_function_t, const char *, void *, jit_type_t, jit_value_t *,
unsigned, int));
DEF_DLL_FN (jit_value_t, jit_insn_shl,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_add,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_load_relative,
(jit_function_t, jit_value_t, jit_nint, jit_type_t));
DEF_DLL_FN (jit_value_t, jit_insn_neg, (jit_function_t, jit_value_t));
DEF_DLL_FN (int, jit_insn_store_relative,
(jit_function_t, jit_value_t, jit_nint, jit_value_t));
DEF_DLL_FN (jit_function_t, jit_function_create, (jit_context_t, jit_type_t));
DEF_DLL_FN (int, jit_function_set_meta,
(jit_function_t, int, void *, jit_meta_free_func, int));
DEF_DLL_FN (jit_value_t, jit_value_create, (jit_function_t, jit_type_t));
DEF_DLL_FN (jit_value_t, jit_value_get_param, (jit_function_t, unsigned));
DEF_DLL_FN (jit_value_t, jit_insn_gt,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_lt,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_le,
(jit_function_t, jit_value_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_load_elem_address,
(jit_function_t, jit_value_t, jit_value_t, jit_type_t));
DEF_DLL_FN (int, jit_insn_return, (jit_function_t, jit_value_t));
DEF_DLL_FN (jit_value_t, jit_insn_add_relative,
(jit_function_t, jit_value_t, jit_nint));
DEF_DLL_FN (jit_label_t, jit_function_reserve_label, (jit_function_t));
DEF_DLL_FN (int, jit_insn_jump_table,
(jit_function_t, jit_value_t, jit_label_t *, unsigned));
DEF_DLL_FN (jit_value_t, jit_insn_alloca, (jit_function_t, jit_value_t));
DEF_DLL_FN (int, jit_insn_move_blocks_to_start,
(jit_function_t, jit_label_t, jit_label_t));
DEF_DLL_FN (int, jit_function_compile, (jit_function_t));
DEF_DLL_FN (void, jit_function_abandon, (jit_function_t));
DEF_DLL_FN (void *, jit_function_to_closure, (jit_function_t));
DEF_DLL_FN (void, jit_context_build_start, (jit_context_t));
DEF_DLL_FN (void, jit_context_build_end, (jit_context_t));
DEF_DLL_FN (jit_function_t, jit_function_from_closure, (jit_context_t, void *));
DEF_DLL_FN (void, jit_dump_function, (FILE *, jit_function_t, const char *));
DEF_DLL_FN (void, jit_init, (void));
DEF_DLL_FN (jit_context_t, jit_context_create, (void));
DEF_DLL_FN (jit_type_t, jit_type_create_signature,
(jit_abi_t, jit_type_t, jit_type_t *, unsigned, int));
DEF_DLL_VAR (jit_type_t, jit_type_void_ptr);
DEF_DLL_VAR (jit_type_t, jit_type_uint);
DEF_DLL_VAR (jit_type_t, jit_type_nint);
DEF_DLL_VAR (jit_type_t, jit_type_sys_int);
DEF_DLL_VAR (jit_type_t, jit_type_int);
DEF_DLL_VAR (jit_type_t, jit_type_void);
# if EMACS_INT_MAX > LONG_MAX
DEF_DLL_VAR (jit_type_t, jit_type_sys_longlong);
DEF_DLL_VAR (jit_type_t, jit_type_long);
# endif
DEF_DLL_VAR (jit_type_t, jit_type_ulong);
static bool
init_libjit_functions (void)
{
HMODULE library = w32_delayed_load (Qlibjit);
if (!library)
return false;
LOAD_DLL_FN (library, jit_value_create_nint_constant);
# if EMACS_INT_MAX > LONG_MAX
LOAD_DLL_FN (library, jit_value_create_long_constant);
# endif
LOAD_DLL_FN (library, jit_insn_store);
LOAD_DLL_FN (library, jit_insn_and);
# if !USE_LSB_TAG
LOAD_DLL_FN (library, jit_insn_ushr);
# endif
LOAD_DLL_FN (library, jit_insn_sub);
LOAD_DLL_FN (library, jit_insn_sshr);
LOAD_DLL_FN (library, jit_insn_eq);
LOAD_DLL_FN (library, jit_insn_branch_if_not);
LOAD_DLL_FN (library, jit_insn_branch_if);
LOAD_DLL_FN (library, jit_insn_branch);
LOAD_DLL_FN (library, jit_insn_label);
LOAD_DLL_FN (library, jit_insn_call_native);
LOAD_DLL_FN (library, jit_insn_shl);
LOAD_DLL_FN (library, jit_insn_add);
LOAD_DLL_FN (library, jit_insn_load_relative);
LOAD_DLL_FN (library, jit_insn_neg);
LOAD_DLL_FN (library, jit_insn_store_relative);
LOAD_DLL_FN (library, jit_function_create);
LOAD_DLL_FN (library, jit_function_set_meta);
LOAD_DLL_FN (library, jit_value_create);
LOAD_DLL_FN (library, jit_value_get_param);
LOAD_DLL_FN (library, jit_insn_gt);
LOAD_DLL_FN (library, jit_insn_lt);
LOAD_DLL_FN (library, jit_insn_le);
LOAD_DLL_FN (library, jit_insn_load_elem_address);
LOAD_DLL_FN (library, jit_insn_return);
LOAD_DLL_FN (library, jit_insn_add_relative);
LOAD_DLL_FN (library, jit_function_reserve_label);
LOAD_DLL_FN (library, jit_insn_jump_table);
LOAD_DLL_FN (library, jit_insn_alloca);
LOAD_DLL_FN (library, jit_insn_move_blocks_to_start);
LOAD_DLL_FN (library, jit_function_compile);
LOAD_DLL_FN (library, jit_function_abandon);
LOAD_DLL_FN (library, jit_function_to_closure);
LOAD_DLL_FN (library, jit_context_build_start);
LOAD_DLL_FN (library, jit_context_build_end);
LOAD_DLL_FN (library, jit_function_from_closure);
LOAD_DLL_FN (library, jit_dump_function);
LOAD_DLL_FN (library, jit_init);
LOAD_DLL_FN (library, jit_context_create);
LOAD_DLL_FN (library, jit_type_create_signature);
LOAD_DLL_VAR (library, jit_type_void_ptr);
LOAD_DLL_VAR (library, jit_type_uint);
LOAD_DLL_VAR (library, jit_type_nint);
LOAD_DLL_VAR (library, jit_type_sys_int);
LOAD_DLL_VAR (library, jit_type_int);
LOAD_DLL_VAR (library, jit_type_void);
# if EMACS_INT_MAX > LONG_MAX
LOAD_DLL_VAR (library, jit_type_sys_longlong);
LOAD_DLL_VAR (library, jit_type_long);
# endif
LOAD_DLL_VAR (library, jit_type_ulong);
return true;
}
# undef jit_value_create_nint_constant
# if EMACS_INT_MAX > LONG_MAX
# undef jit_value_create_long_constant
# endif
# undef jit_insn_store
# undef jit_insn_and
# if !USE_LSB_TAG
# undef jit_insn_ushr
# endif
# undef jit_insn_sub
# undef jit_insn_sshr
# undef jit_insn_eq
# undef jit_insn_branch_if_not
# undef jit_insn_branch_if
# undef jit_insn_branch
# undef jit_insn_label
# undef jit_insn_call_native
# undef jit_insn_shl
# undef jit_insn_add
# undef jit_insn_load_relative
# undef jit_insn_neg
# undef jit_insn_store_relative
# undef jit_function_create
# undef jit_function_set_meta
# undef jit_value_create
# undef jit_value_get_param
# undef jit_insn_gt
# undef jit_insn_lt
# undef jit_insn_le
# undef jit_insn_load_elem_address
# undef jit_insn_return
# undef jit_insn_add_relative
# undef jit_function_reserve_label
# undef jit_insn_jump_table
# undef jit_insn_alloca
# undef jit_insn_move_blocks_to_start
# undef jit_function_compile
# undef jit_function_abandon
# undef jit_function_to_closure
# undef jit_context_build_start
# undef jit_context_build_end
# undef jit_function_from_closure
# undef jit_dump_function
# undef jit_init
# undef jit_context_create
# undef jit_type_create_signature
# define jit_value_create_nint_constant fn_jit_value_create_nint_constant
# if EMACS_INT_MAX > LONG_MAX
# define jit_value_create_long_constant fn_jit_value_create_long_constant
# endif
# define jit_insn_store fn_jit_insn_store
# define jit_insn_and fn_jit_insn_and
# if !USE_LSB_TAG
# define jit_insn_ushr fn_jit_insn_ushr
# endif
# define jit_insn_sub fn_jit_insn_sub
# define jit_insn_sshr fn_jit_insn_sshr
# define jit_insn_eq fn_jit_insn_eq
# define jit_insn_branch_if_not fn_jit_insn_branch_if_not
# define jit_insn_branch_if fn_jit_insn_branch_if
# define jit_insn_branch fn_jit_insn_branch
# define jit_insn_label fn_jit_insn_label
# define jit_insn_call_native fn_jit_insn_call_native
# define jit_insn_shl fn_jit_insn_shl
# define jit_insn_add fn_jit_insn_add
# define jit_insn_load_relative fn_jit_insn_load_relative
# define jit_insn_neg fn_jit_insn_neg
# define jit_insn_store_relative fn_jit_insn_store_relative
# define jit_function_create fn_jit_function_create
# define jit_function_set_meta fn_jit_function_set_meta
# define jit_value_create fn_jit_value_create
# define jit_value_get_param fn_jit_value_get_param
# define jit_insn_gt fn_jit_insn_gt
# define jit_insn_lt fn_jit_insn_lt
# define jit_insn_le fn_jit_insn_le
# define jit_insn_load_elem_address fn_jit_insn_load_elem_address
# define jit_insn_return fn_jit_insn_return
# define jit_insn_add_relative fn_jit_insn_add_relative
# define jit_function_reserve_label fn_jit_function_reserve_label
# define jit_insn_jump_table fn_jit_insn_jump_table
# define jit_insn_alloca fn_jit_insn_alloca
# define jit_insn_move_blocks_to_start fn_jit_insn_move_blocks_to_start
# define jit_function_compile fn_jit_function_compile
# define jit_function_abandon fn_jit_function_abandon
# define jit_function_to_closure fn_jit_function_to_closure
# define jit_context_build_start fn_jit_context_build_start
# define jit_context_build_end fn_jit_context_build_end
# define jit_function_from_closure fn_jit_function_from_closure
# define jit_dump_function fn_jit_dump_function
# define jit_init fn_jit_init
# define jit_context_create fn_jit_context_create
# define jit_type_create_signature fn_jit_type_create_signature
# undef jit_type_void_ptr
# undef jit_type_uint
# undef jit_type_nint
# undef jit_type_sys_int
# undef jit_type_int
# undef jit_type_void
# if EMACS_INT_MAX > LONG_MAX
# undef jit_type_sys_longlong
# undef jit_type_long
# endif
# undef jit_type_ulong
# define jit_type_void_ptr *pv_jit_type_void_ptr
# define jit_type_uint *pv_jit_type_uint
# define jit_type_nint *pv_jit_type_nint
# define jit_type_sys_int *pv_jit_type_sys_int
# define jit_type_int *pv_jit_type_int
# define jit_type_void *pv_jit_type_void
# if EMACS_INT_MAX > LONG_MAX
# define jit_type_sys_longlong *pv_jit_type_sys_longlong
# define jit_type_long *pv_jit_type_long
# endif
# define jit_type_ulong *pv_jit_type_ulong
#endif /* WINDOWSNT */
static bool emacs_jit_initialized;
jit_context_t emacs_jit_context;
static jit_type_t nullary_signature;
static jit_type_t unary_signature;
static jit_type_t binary_signature;
static jit_type_t ternary_signature;
static jit_type_t unbind_n_signature;
static jit_type_t temp_output_buffer_show_signature;
static jit_type_t arithcompare_signature;
static jit_type_t callN_signature;
static jit_type_t compiled_signature;
static jit_type_t wrong_number_of_arguments_signature;
static jit_type_t set_internal_signature;
static jit_type_t specbind_signature;
static jit_type_t record_unwind_protect_excursion_signature;
static jit_type_t record_unwind_protect_signature;
static jit_type_t void_void_signature;
static jit_type_t lisp_void_signature;
static jit_type_t push_handler_signature;
static jit_type_t setjmp_signature;
static jit_type_t internal_catch_signature;
static jit_type_t subr_signature[SUBR_MAX_ARGS];
static jit_type_t ptrdiff_t_type, lisp_object_type;
/* Make a pointer constant. */
#if EMACS_INT_MAX == INT_MAX
#define CONSTANT(FUNC, VAL) \
jit_value_create_nint_constant (FUNC, jit_type_nint, XLI (VAL))
#else
#define CONSTANT(FUNC, VAL) \
jit_value_create_long_constant (FUNC, jit_type_long, (jit_long) XLI (VAL))
#endif
/* Fetch the next byte from the bytecode stream. */
#define FETCH (bytestr_data[pc++])
/* Fetch two bytes from the bytecode stream and make a 16-bit number
out of them. */
#define FETCH2 (op = FETCH, op + (FETCH << 8))
#define PUSH(VALUE) \
jit_insn_store (func, stack[++stack_pointer], VALUE)
#define POP stack[stack_pointer--]
/* Discard n values from the execution stack. */
#define DISCARD(n) (stack_pointer -= (n))
/* Get the value which is at the top of the execution stack, but don't
pop it. */
#define TOP stack[stack_pointer]
/* Compile code that extracts the type from VAL. */
static jit_value_t
get_type (jit_function_t func, jit_value_t val)
{
#if USE_LSB_TAG
jit_value_t mask = jit_value_create_nint_constant (func, jit_type_void_ptr,
~VALMASK);
return jit_insn_and (func, val, mask);
#else /* USE_LSB_TAG */
jit_value_t shift = jit_value_create_nint_constant (func, jit_type_void_ptr,
VALBITS);
return jit_insn_ushr (func, val, shift);
#endif /* not USE_LSB_TAG */
}
static jit_value_t
untag (jit_function_t func, jit_value_t val, EMACS_UINT utype)
{
jit_value_t tem;
utype = utype << (USE_LSB_TAG ? 0 : VALBITS);
#if EMACS_INT_MAX <= LONG_MAX
tem = jit_value_create_nint_constant (func, jit_type_void_ptr, utype);
#else
tem = jit_value_create_long_constant (func, jit_type_ulong, (jit_ulong)utype);
#endif
return jit_insn_sub (func, val, tem);
}
static jit_value_t
to_int (jit_function_t func, jit_value_t val)
{
jit_value_t shift = jit_value_create_nint_constant (func, jit_type_uint,
INTTYPEBITS);
#if !USE_LSB_TAG
val = jit_insn_shl (func, val, shift);
#endif
return jit_insn_sshr (func, val, shift);
}
static jit_value_t
eq_nil (jit_function_t func, jit_value_t val)
{
jit_value_t nilval = CONSTANT (func, Qnil);
return jit_insn_eq (func, val, nilval);
}
static jit_value_t
compare_type (jit_function_t func, jit_value_t val, int to_be_checked)
{
jit_value_t real_type = get_type (func, val);
jit_value_t type_val
= jit_value_create_nint_constant (func, jit_type_void_ptr, to_be_checked);
return jit_insn_eq (func, real_type, type_val);
}
/* If the next instruction in the stream is a conditional branch,
return true and compile jumps based on COMPARE. Otherwise, return
false. */
static bool
peek_condition (jit_function_t func,
unsigned char *bytestr_data, ptrdiff_t pc,
jit_value_t compare, jit_value_t dest,
jit_label_t *labels)
{
int op = FETCH;
switch (op)
{
case Bgotoifnil:
op = FETCH2;
jit_insn_branch_if_not (func, compare, &labels[op]);
break;
case Bgotoifnonnil:
op = FETCH2;
jit_insn_branch_if (func, compare, &labels[op]);
break;
case Bgotoifnilelsepop:
op = FETCH2;
jit_insn_store (func, dest, CONSTANT (func, Qnil));
jit_insn_branch_if_not (func, compare, &labels[op]);
break;
case Bgotoifnonnilelsepop:
op = FETCH2;
jit_insn_store (func, dest, CONSTANT (func, Qt));
jit_insn_branch_if (func, compare, &labels[op]);
break;
case BRgotoifnil:
op = FETCH - 128;
op += pc;
jit_insn_branch_if_not (func, compare, &labels[op]);
break;
case BRgotoifnonnil:
op = FETCH - 128;
op += pc;
jit_insn_branch_if (func, compare, &labels[op]);
break;
case BRgotoifnilelsepop:
op = FETCH - 128;
op += pc;
jit_insn_store (func, dest, CONSTANT (func, Qnil));
jit_insn_branch_if_not (func, compare, &labels[op]);
break;
case BRgotoifnonnilelsepop:
op = FETCH - 128;
op += pc;
jit_insn_store (func, dest, CONSTANT (func, Qt));
jit_insn_branch_if (func, compare, &labels[op]);
break;
default:
return false;
}
/* This is necessary to bypass the (probably dead) code that will be
emitted for the branch in the main JIT loop. */
jit_insn_branch (func, &labels[pc]);
return true;
}
static void
emit_qnil_or_qt (jit_function_t func,
unsigned char *bytestr_data, ptrdiff_t pc,
jit_value_t compare, jit_value_t dest,
jit_label_t *labels)
{
jit_value_t tem;
/* Optimize the case where we see bytecode like:
Beq
Bgotoifnil [...]
Here, we don't actually need to load and store the `nil' or `t'
-- we can just branch directly based on the condition we just
computed. */
if (!peek_condition (func, bytestr_data, pc, compare, dest, labels))
{
/* Actually must emit a load of Qt or Qnil. */
jit_label_t nil_label = jit_label_undefined;
jit_insn_branch_if_not (func, compare, &nil_label);
tem = CONSTANT (func, Qt);
jit_insn_store (func, dest, tem);
jit_insn_branch (func, &labels[pc]);
jit_insn_label (func, &nil_label);
tem = CONSTANT (func, Qnil);
jit_insn_store (func, dest, tem);
}
}
static jit_value_t
compile_nullary (jit_function_t func, const char *name,
Lisp_Object (*callee) (void))
{
return jit_insn_call_native (func, name, (void *) callee,
nullary_signature, NULL, 0,
JIT_CALL_NOTHROW);
}
static void
compile_unary (jit_function_t func, const char *name,
Lisp_Object (*callee) (Lisp_Object),
jit_value_t arg_and_dest)
{
jit_value_t result = jit_insn_call_native (func, name, (void *) callee,
unary_signature, &arg_and_dest, 1,
JIT_CALL_NOTHROW);
jit_insn_store (func, arg_and_dest, result);
}
static void
compile_binary (jit_function_t func, const char *name,
Lisp_Object (*callee) (Lisp_Object, Lisp_Object),
jit_value_t arg_and_dest, jit_value_t arg2)
{
jit_value_t args[2] = { arg_and_dest, arg2 };
jit_value_t result = jit_insn_call_native (func, name, (void *) callee,
binary_signature, args, 2,
JIT_CALL_NOTHROW);
jit_insn_store (func, arg_and_dest, result);
}
static void
compile_ternary (jit_function_t func, const char *name,
Lisp_Object (*callee) (Lisp_Object, Lisp_Object, Lisp_Object),
jit_value_t arg_and_dest, jit_value_t arg2, jit_value_t arg3)
{
jit_value_t args[3] = { arg_and_dest, arg2, arg3 };
jit_value_t result = jit_insn_call_native (func, name, (void *) callee,
ternary_signature, args, 3,
JIT_CALL_NOTHROW);
jit_insn_store (func, arg_and_dest, result);
}
static void
compile_arithcompare (jit_function_t func, const char *name,
jit_value_t arg_and_dest, jit_value_t arg2, int arg3)
{
jit_value_t tem
= jit_value_create_nint_constant (func, jit_type_sys_int, arg3);
jit_value_t args[3] = { arg_and_dest, arg2, tem };
jit_value_t result = jit_insn_call_native (func, name, (void *) arithcompare,
arithcompare_signature, args, 3,
JIT_CALL_NOTHROW);
jit_insn_store (func, arg_and_dest, result);
}
static jit_value_t
compile_make_natnum (jit_function_t func, jit_value_t untagged_int)
{
#if USE_LSB_TAG
jit_value_t nbits
= jit_value_create_nint_constant (func, jit_type_int, INTTYPEBITS);
jit_value_t val = jit_insn_shl (func, untagged_int, nbits);
jit_value_t tag
= jit_value_create_nint_constant (func, jit_type_void_ptr, Lisp_Int0);
return jit_insn_add (func, val, tag);
#else /* USE_LSB_TAG */
# if EMACS_INT_MAX <= LONG_MAX
jit_value_t tag
= jit_value_create_nint_constant (func, jit_type_void_ptr,
((EMACS_INT) Lisp_Int0) << VALBITS);
# else
jit_value_t tag
= jit_value_create_long_constant (func, jit_type_ulong,
((EMACS_INT) Lisp_Int0) << VALBITS);
# endif
return jit_insn_add (func, untagged_int, tag);
#endif /* not USE_LSB_TAG */
}
static jit_value_t
compile_make_number (jit_function_t func, jit_value_t untagged_int)
{
#if USE_LSB_TAG
jit_value_t nbits
= jit_value_create_nint_constant (func, jit_type_int, INTTYPEBITS);
jit_value_t val = jit_insn_shl (func, untagged_int, nbits);
jit_value_t tag
= jit_value_create_nint_constant (func, jit_type_void_ptr, Lisp_Int0);
return jit_insn_add (func, val, tag);
#else /* USE_LSB_TAG */
# if EMACS_INT_MAX <= LONG_MAX
jit_value_t mask
= jit_value_create_nint_constant (func, jit_type_void_ptr, INTMASK);
jit_value_t tag
= jit_value_create_nint_constant (func, jit_type_void_ptr,
((EMACS_INT) Lisp_Int0) << VALBITS);
# else
jit_value_t mask
= jit_value_create_long_constant (func, jit_type_ulong, INTMASK);
jit_value_t tag
= jit_value_create_long_constant (func, jit_type_ulong,
((EMACS_INT) Lisp_Int0) << VALBITS);
# endif
jit_value_t val = jit_insn_and (func, untagged_int, mask);
return jit_insn_add (func, val, tag);
#endif /* not USE_LSB_TAG */
}
static jit_value_t
compile_current_thread (jit_function_t func)
{
jit_value_t thread_ptr =
jit_value_create_nint_constant (func, jit_type_void_ptr,
(jit_nint) ¤t_thread);
return jit_insn_load_relative (func, thread_ptr, 0, jit_type_void_ptr);
}
static jit_value_t
compile_current_buffer (jit_function_t func)
{
jit_value_t current_thread = compile_current_thread (func);
return jit_insn_load_relative (func, current_thread,
offsetof (struct thread_state,
m_current_buffer),
jit_type_void_ptr);
}
static jit_value_t
compile_buffer_int (jit_function_t func, off_t offset)
{
jit_value_t current_buffer_val = compile_current_buffer (func);
jit_value_t value
= jit_insn_load_relative (func, current_buffer_val, offset,
ptrdiff_t_type);
return compile_make_natnum (func, value);
}
static jit_value_t
compare_buffer_ints (jit_function_t func, off_t off1, off_t off2)
{
jit_value_t current_buffer_val = compile_current_buffer (func);
jit_value_t value1
= jit_insn_load_relative (func, current_buffer_val, off1,
ptrdiff_t_type);
jit_value_t value2
= jit_insn_load_relative (func, current_buffer_val, off2,
ptrdiff_t_type);
return jit_insn_eq (func, value1, value2);
}
static void
car_or_cdr (jit_function_t func, jit_value_t val, off_t offset,
jit_label_t *next_insn, bool safe,
bool *called_wtype, jit_label_t *wtype_label,
jit_value_t wtype_arg)
{
jit_value_t tem;
jit_label_t not_a_cons = jit_label_undefined;
jit_value_t is_cons = compare_type (func, val, Lisp_Cons);
jit_insn_branch_if_not (func, is_cons, ¬_a_cons);
/* Is a cons. */
tem = untag (func, val, Lisp_Cons);
tem = jit_insn_load_relative (func, tem, offset,
lisp_object_type);
jit_insn_store (func, val, tem);
jit_insn_branch (func, next_insn);
jit_insn_label (func, ¬_a_cons);
if (safe)
{
/* Not a cons, so just use nil. */
jit_value_t nilval = CONSTANT (func, Qnil);
jit_insn_store (func, val, nilval);
}
else
{
/* Check if it is nil. */
tem = eq_nil (func, val);
/* If it is nil, VAL is already correct and we can carry on. */
jit_insn_branch_if (func, tem, next_insn);
/* Wrong type. */
jit_insn_store (func, wtype_arg, val);
jit_insn_branch (func, wtype_label);
*called_wtype = true;
}
}
static void
compile_wrong_type_argument (jit_function_t func, jit_label_t *label,
jit_value_t wtype_arg)
{
jit_value_t args[2];
args[0] = CONSTANT (func, Qlistp);
args[1] = wtype_arg;
jit_insn_label (func, label);
jit_insn_call_native (func, "wrong_type_argument",
(void *) wrong_type_argument,
specbind_signature, args, 2,
JIT_CALL_NORETURN);
}
static jit_value_t
compare_integerp (jit_function_t func, jit_value_t val, jit_value_t *type_out)
{
jit_value_t type = get_type (func, val);
if (type_out != NULL)
*type_out = type;
jit_value_t c1
= jit_value_create_nint_constant (func, jit_type_void_ptr,
Lisp_Int0 | ~Lisp_Int1);
jit_value_t c2
= jit_value_create_nint_constant (func, jit_type_void_ptr,
Lisp_Int0);
jit_value_t tem = jit_insn_and (func, type, c1);
return jit_insn_eq (func, tem, c2);
}
static Lisp_Object
negate (Lisp_Object arg)
{
return Fminus (1, &arg);
}
enum math_op
{
SUB1,
ADD1,
NEGATE
};
static void
unary_intmath (jit_function_t func, jit_value_t val, enum math_op op,
jit_label_t *next_insn)
{
jit_label_t not_an_int = jit_label_undefined;
jit_value_t compare = compare_integerp (func, val, NULL);
jit_insn_branch_if_not (func, compare, ¬_an_int);
/* Got an integer. */
jit_value_t result = to_int (func, val);
jit_value_t tem;
switch (op)
{
case SUB1:
/* Don't allow (1- most-negative-fixnum). */
#if EMACS_INT_MAX <= LONG_MAX
tem = jit_value_create_nint_constant (func, jit_type_sys_int,
MOST_NEGATIVE_FIXNUM);
#else
tem = jit_value_create_long_constant (func, jit_type_sys_longlong,
MOST_NEGATIVE_FIXNUM);
#endif
compare = jit_insn_eq (func, result, tem);
jit_insn_branch_if (func, compare, ¬_an_int);
tem = jit_value_create_nint_constant (func, jit_type_sys_int, 1);
result = jit_insn_sub (func, result, tem);
break;
case ADD1:
/* Don't allow (1+ most-positive-fixnum). */
#if EMACS_INT_MAX <= LONG_MAX
tem = jit_value_create_nint_constant (func, jit_type_sys_int,
MOST_POSITIVE_FIXNUM);
#else
tem = jit_value_create_long_constant (func, jit_type_sys_longlong,
MOST_POSITIVE_FIXNUM);
#endif
compare = jit_insn_eq (func, result, tem);
jit_insn_branch_if (func, compare, ¬_an_int);
tem = jit_value_create_nint_constant (func, jit_type_sys_int, 1);
result = jit_insn_add (func, result, tem);
break;
case NEGATE:
/* Don't allow (- most-negative-fixnum). */
#if EMACS_INT_MAX <= LONG_MAX
tem = jit_value_create_nint_constant (func, jit_type_sys_int,
MOST_NEGATIVE_FIXNUM);
#else
tem = jit_value_create_long_constant (func, jit_type_sys_longlong,
MOST_NEGATIVE_FIXNUM);
#endif
compare = jit_insn_eq (func, result, tem);
jit_insn_branch_if (func, compare, ¬_an_int);
result = jit_insn_neg (func, result);
break;
default:
emacs_abort ();
}
result = compile_make_number (func, result);
jit_insn_store (func, val, result);
jit_insn_branch (func, next_insn);
jit_insn_label (func, ¬_an_int);
const char *name;
void *callee;
switch (op)
{
case SUB1:
name = "Fsub1";
callee = (void *) Fsub1;
break;
case ADD1:
name = "Fadd1";
callee = (void *) Fadd1;
break;
case NEGATE:
name = "negate";
callee = (void *) negate;
break;
default:
emacs_abort ();
}
tem = jit_insn_call_native (func, name, (void *) callee,
unary_signature, &val, 1,
JIT_CALL_NOTHROW);
jit_insn_store (func, val, tem);
}
static jit_value_t
compile_callN (jit_function_t func, const char *name,
Lisp_Object (*callee) (ptrdiff_t, Lisp_Object *),
int howmany, jit_value_t scratch, jit_value_t *stack)
{
jit_value_t args[2];
args[0] = jit_value_create_nint_constant (func, ptrdiff_t_type, howmany);
args[1] = scratch;
int i;
for (i = 0; i < howmany; ++i)
jit_insn_store_relative (func, scratch, i * sizeof (Lisp_Object),
stack[1 + i]);
return jit_insn_call_native (func, name, (void *) callee,
callN_signature, args, 2, JIT_CALL_NOTHROW);
}
static jit_value_t
compile_next_handlerlist (jit_function_t func)
{
jit_value_t current_thread = compile_current_thread (func);
jit_value_t hlist_ptr
= jit_insn_load_relative (func, current_thread,
offsetof (struct thread_state,
m_handlerlist),
jit_type_void_ptr);
jit_value_t next
= jit_insn_load_relative (func, hlist_ptr,
offsetof (struct handler, next),
jit_type_void_ptr);
jit_insn_store_relative (func, current_thread,
offsetof (struct thread_state,
m_handlerlist),
next);
return hlist_ptr;
}
#define COMPILE_CALLN(FUNC, N) \
do { \
jit_value_t result; \
DISCARD (N); \
result = compile_callN (func, # FUNC, FUNC, \
N, scratch, &stack[stack_pointer]); \
if (N > scratch_slots_needed) \
scratch_slots_needed = N; \
PUSH (result); \
} while (0)
static void
bcall0 (Lisp_Object f)
{
if (FUNCTIONP (f))
Ffuncall (1, &f);
}
static Lisp_Object
native_save_window_excursion (Lisp_Object v1)
{
ptrdiff_t count1 = SPECPDL_INDEX ();
record_unwind_protect (restore_window_configuration,
Fcurrent_window_configuration (Qnil));
v1 = Fprogn (v1);
unbind_to (count1, v1);
return v1;
}
static Lisp_Object
native_temp_output_buffer_setup (Lisp_Object x)
{
CHECK_STRING (x);
temp_output_buffer_setup (SSDATA (x));
return Vstandard_output;
}
static Lisp_Object
unbind_n (int val)
{
return unbind_to (SPECPDL_INDEX () - val, Qnil);
}
static void
wrong_number_of_arguments (int mandatory, int nonrest, int nargs)
{
Fsignal (Qwrong_number_of_arguments,
list2 (Fcons (make_fixnum (mandatory), make_fixnum (nonrest)),
make_fixnum (nargs)));
}
struct pc_list
{
/* PC at which to (re-)start compilation. */
int pc;
/* Saved stack pointer. */
int stack_pointer;
struct pc_list *next;
};
#define PUSH_PC(insn) \
do { \
struct pc_list *new = xmalloc (sizeof (struct pc_list)); \
new->pc = insn; \
new->stack_pointer = stack_pointer; \
new->next = pc_list; \
pc_list = new; \
} while (0)
#define COMPILE_BUFFER_INT(FIELD) \
compile_buffer_int (func, offsetof (struct buffer, FIELD))
#define COMPARE_BUFFER_INTS(FIELD1, FIELD2) \
compare_buffer_ints (func, \
offsetof (struct buffer, FIELD1), \
offsetof (struct buffer, FIELD2))
static bool
find_hash_min_max_pc (struct Lisp_Hash_Table *htab,
EMACS_INT *min_pc, EMACS_INT *max_pc)
{
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (htab); ++i)
if (!NILP (HASH_HASH (htab, i)))
{
Lisp_Object pc = HASH_VALUE (htab, i);
if (!FIXNUMP (pc))
return false;
EMACS_INT pcval = XFIXNUM (pc);
if (pcval < *min_pc)
*min_pc = pcval;
if (pcval > *max_pc)
*max_pc = pcval;
}
++*max_pc;
return true;
}
static struct subr_function *
compile (ptrdiff_t bytestr_length, unsigned char *bytestr_data,
EMACS_INT stack_depth, Lisp_Object *vectorp,
ptrdiff_t vector_size, Lisp_Object args_template)
{
int type;
struct pc_list *pc_list = NULL;
/* Note that any error before this is attached to the function must
free this object. */
struct subr_function *result = xmalloc (sizeof (struct subr_function));
result->min_args = 0;
result->max_args = MANY;
jit_type_t function_signature = compiled_signature;
bool parse_args = true;
if (FIXNUMP (args_template))
{
ptrdiff_t at = XFIXNUM (args_template);
bool rest = (at & 128) != 0;
int mandatory = at & 127;
ptrdiff_t nonrest = at >> 8;
/* Always set this correctly so that funcall_subr will do some
checking for us. */
result->min_args = mandatory;
if (!rest && nonrest < SUBR_MAX_ARGS)
{
result->max_args = nonrest;
function_signature = subr_signature[nonrest];
parse_args = false;
}
}
jit_function_t func = jit_function_create (emacs_jit_context,
function_signature);
ptrdiff_t pc = 0;
jit_value_t *stack = (jit_value_t *) xmalloc (stack_depth
* sizeof (jit_value_t));
int stack_pointer = -1;
jit_label_t *labels = (jit_label_t *) xmalloc (bytestr_length
* sizeof (jit_label_t));
/* Temporary array used only for switches. */
jit_label_t *sw_labels = (jit_label_t *) xmalloc (bytestr_length
* sizeof (jit_label_t));
int *stack_depths = (int *) xmalloc (bytestr_length * sizeof (int));
jit_value_t n_args, arg_vec;
/* On failure this will also free RESULT. */
jit_function_set_meta (func, 0, result, xfree, 0);
for (int i = 0; i < bytestr_length; ++i)
{
labels[i] = jit_label_undefined;
sw_labels[i] = jit_label_undefined;
stack_depths[i] = -1;
}
for (int i = 0; i < stack_depth; ++i)
stack[i] = jit_value_create (func, lisp_object_type);
/* This is a placeholder; once we know how much space we'll need, we
will allocate it and move it into place at the start of the
function. */
jit_value_t scratch = jit_value_create (func, lisp_object_type);
int scratch_slots_needed = 0;
/* State needed if we need to emit a call to
wrong_type_argument. */
bool called_wtype = false;
jit_label_t wtype_label = jit_label_undefined;
jit_value_t wtype_arg = jit_value_create (func, lisp_object_type);
jit_label_t argfail = jit_label_undefined;
bool need_argfail = false;
jit_value_t mandatory_val, nonrest_val;
if (!parse_args)
{
/* We can emit function that doesn't need to manually decipher
its arguments. */
ptrdiff_t at = XFIXNUM (args_template);
ptrdiff_t nonrest = at >> 8;
for (ptrdiff_t i = 0; i < nonrest; ++i)
PUSH (jit_value_get_param (func, i));
}
else
{
/* Prologue. */
n_args = jit_value_get_param (func, 0);
arg_vec = jit_value_get_param (func, 1);
if (FIXNUMP (args_template))
{
ptrdiff_t at = XFIXNUM (args_template);
bool rest = (at & 128) != 0;
int mandatory = at & 127;
ptrdiff_t nonrest = at >> 8;
mandatory_val
= jit_value_create_nint_constant (func, ptrdiff_t_type, mandatory);
nonrest_val
= jit_value_create_nint_constant (func, ptrdiff_t_type, nonrest);
/* If there are no rest arguments and we have more than the
maximum, error. Note that funcall_subr ensures that, no
matter what, we'll never see fewer than the minimum
number of arguments. */
if (!rest)
{
jit_value_t compare = jit_insn_gt (func, n_args, nonrest_val);
jit_insn_branch_if (func, compare, &argfail);
need_argfail = true;
}
/* Load mandatory arguments. */
for (ptrdiff_t i = 0; i < mandatory; ++i)
{
jit_value_t loaded
= jit_insn_load_relative (func, arg_vec, i * sizeof (Lisp_Object),
lisp_object_type);
jit_insn_store (func, stack[i], loaded);
}
/* &optional arguments are a bit weirder since we can't refer to
the appropriate stack slot by index at runtime. */
if (nonrest > mandatory)
{
jit_value_t qnil = CONSTANT (func, Qnil);
jit_label_t *opt_labels
= (jit_label_t *) xmalloc ((nonrest - mandatory)
* sizeof (jit_label_t));
jit_label_t opts_done = jit_label_undefined;
for (ptrdiff_t i = mandatory; i < nonrest; ++i)
{
opt_labels[i - mandatory] = jit_label_undefined;
jit_value_t this_arg
= jit_value_create_nint_constant (func, jit_type_sys_int, i);
jit_value_t cmp = jit_insn_le (func, n_args, this_arg);
/* If this argument wasn't found, then neither are the
subsequent ones; so branch into the correct spot in a
series of loads of Qnil. */
jit_insn_branch_if (func, cmp, &opt_labels[i - mandatory]);
jit_value_t loaded
= jit_insn_load_relative (func, arg_vec,
i * sizeof (Lisp_Object),
lisp_object_type);
jit_insn_store (func, stack[i], loaded);
}
jit_insn_branch (func, &opts_done);
for (ptrdiff_t i = mandatory; i < nonrest; ++i)
{
jit_insn_label (func, &opt_labels[i - mandatory]);
jit_insn_store (func, stack[i], qnil);
}
jit_insn_label (func, &opts_done);
xfree (opt_labels);
}
stack_pointer = nonrest - 1;
/* Now handle rest arguments, if any. */
if (rest)
{
jit_label_t no_rest = jit_label_undefined;
jit_value_t cmp = jit_insn_lt (func, nonrest_val, n_args);
jit_insn_branch_if_not (func, cmp, &no_rest);
jit_value_t vec_addr
= jit_insn_load_elem_address (func, arg_vec, nonrest_val,
lisp_object_type);
jit_value_t new_args
= jit_insn_sub (func, n_args, nonrest_val);
jit_value_t args[2] = { new_args, vec_addr };
jit_value_t listval
= jit_insn_call_native (func, "list", (void *) Flist,
callN_signature,
args, 2, JIT_CALL_NOTHROW);
PUSH (listval);
jit_insn_branch (func, &labels[0]);
/* Since we emitted a branch. */
--stack_pointer;
jit_insn_label (func, &no_rest);
jit_value_t qnil = CONSTANT (func, Qnil);
PUSH (qnil);
}
/* Fall through to the main body. */
}
}
for (;;)
{
if (pc == bytestr_length)
{
/* Falling off the end would be bad. */
goto fail;
}
else if (pc != -1 && (stack_depths[pc] != -1))
{
/* We've already compiled this code, and we're expecting to
fall through. So, emit a goto and then resume work at
some other PC. */
jit_insn_branch (func, &labels[pc]);
pc = -1;
}
/* If we don't have a PC currently, pop a new one from the list
and work there. */
while (pc == -1 && pc_list != NULL)
{
struct pc_list *next;
pc = pc_list->pc;
stack_pointer = pc_list->stack_pointer;
next = pc_list->next;
xfree (pc_list);
pc_list = next;
if (stack_depths[pc] == -1)
{
/* Work on this one. */
stack_depths[pc] = stack_pointer + 1;
break;
}
else if (stack_depths[pc] == stack_pointer + 1)
{
/* Already compiled this. */
pc = -1;
}
else
{
/* Oops - failure. */
goto fail;
}
}
if (pc == -1 && pc_list == NULL)
{
/* No more blocks to examine. */
break;
}
jit_insn_label (func, &labels[pc]);
int op = FETCH;
switch (op)
{
case Bvarref7:
op = FETCH2;
goto varref;
case Bvarref:
case Bvarref1:
case Bvarref2:
case Bvarref3:
case Bvarref4:
case Bvarref5:
op -= Bvarref;
goto varref;
case Bvarref6:
op = FETCH;
varref:
{
jit_value_t sym, result;
sym = CONSTANT (func, vectorp[op]);
result = jit_insn_call_native (func, "symbol-value",
(void *) Fsymbol_value,
unary_signature, &sym, 1,
JIT_CALL_NOTHROW);
PUSH (result);
break;
}
case Bcar:
car_or_cdr (func, TOP, offsetof (struct Lisp_Cons, u.s.car),
&labels[pc], false,
&called_wtype, &wtype_label, wtype_arg);
break;
case Beq:
{
jit_value_t v1 = POP;
jit_value_t compare = jit_insn_eq (func, v1, TOP);
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
case Bmemq:
{
jit_value_t v1 = POP;
compile_binary (func, "memq", Fmemq, TOP, v1);
break;
}
case Bcdr:
car_or_cdr (func, TOP, offsetof (struct Lisp_Cons, u.s.u.cdr),
&labels[pc], false,
&called_wtype, &wtype_label, wtype_arg);
break;
case Bvarset:
case Bvarset1:
case Bvarset2:
case Bvarset3:
case Bvarset4:
case Bvarset5:
op -= Bvarset;
goto varset;
case Bvarset7:
op = FETCH2;
goto varset;
case Bvarset6:
op = FETCH;
varset:
{
jit_value_t args[4];
args[0] = CONSTANT (func, vectorp[op]);
args[1] = POP;
args[2] = CONSTANT (func, Qnil);
args[3] = jit_value_create_nint_constant (func, jit_type_sys_int,
SET_INTERNAL_SET);
jit_insn_call_native (func, "set_internal", (void *) set_internal,
set_internal_signature, args, 4,
JIT_CALL_NOTHROW);
}
break;
case Bdup:
{
jit_value_t v1 = TOP;
PUSH (v1);
break;
}
/* ------------------ */
case Bvarbind6:
op = FETCH;
goto varbind;
case Bvarbind7:
op = FETCH2;
goto varbind;
case Bvarbind:
case Bvarbind1:
case Bvarbind2:
case Bvarbind3:
case Bvarbind4:
case Bvarbind5:
op -= Bvarbind;
varbind:
{
jit_value_t vals[2];
vals[0] = CONSTANT (func, vectorp[op]);
vals[1] = POP;
jit_insn_call_native (func, "specbind", (void *) specbind,
specbind_signature, vals, 2,
JIT_CALL_NOTHROW);
break;
}
case Bcall6:
op = FETCH;
goto docall;
case Bcall7:
op = FETCH2;
goto docall;
case Bcall:
case Bcall1:
case Bcall2:
case Bcall3:
case Bcall4:
case Bcall5:
op -= Bcall;
docall:
{
COMPILE_CALLN (Ffuncall, op + 1);
break;
}
case Bunbind6:
op = FETCH;
goto dounbind;
case Bunbind7:
op = FETCH2;
goto dounbind;
case Bunbind:
case Bunbind1:
case Bunbind2:
case Bunbind3:
case Bunbind4:
case Bunbind5:
op -= Bunbind;
dounbind:
{
jit_value_t val = jit_value_create_nint_constant (func,
jit_type_sys_int,
op);
jit_insn_call_native (func, "unbind_n", (void *) unbind_n,
unbind_n_signature, &val, 1,
JIT_CALL_NOTHROW);
}
break;
case Bgoto:
op = FETCH2;
/* This looks funny but it circumvents the code above that
handles the case where fall-through actually requires a
branch. */
PUSH_PC (op);
pc = -1;
jit_insn_branch (func, &labels[op]);
break;
case Bgotoifnil:
{
jit_value_t v1 = POP;
jit_value_t compare = eq_nil (func, v1);
op = FETCH2;
PUSH_PC (op);
jit_insn_branch_if (func, compare, &labels[op]);
break;
}
case Bgotoifnonnil:
{
jit_value_t val = POP;
jit_value_t compare = eq_nil (func, val);
op = FETCH2;
PUSH_PC (op);
jit_insn_branch_if_not (func, compare, &labels[op]);
break;
}
case Bgotoifnilelsepop:
{
jit_value_t v1 = TOP;
jit_value_t compare = eq_nil (func, v1);
op = FETCH2;
PUSH_PC (op);
jit_insn_branch_if (func, compare, &labels[op]);
DISCARD (1);
break;
}
break;
case Bgotoifnonnilelsepop:
{
jit_value_t v1 = TOP;
jit_value_t compare = eq_nil (func, v1);
op = FETCH2;
PUSH_PC (op);
jit_insn_branch_if_not (func, compare, &labels[op]);
DISCARD (1);
break;
}
break;
case BRgoto:
{
op = FETCH - 128;
op += pc;
/* This looks funny but it circumvents the code above that
handles the case where fall-through actually requires a
branch. */
PUSH_PC (op);
pc = -1;
jit_insn_branch (func, &labels[op]);
break;
}
case BRgotoifnil:
{
jit_value_t v1 = POP;
jit_value_t compare = eq_nil (func, v1);
op = FETCH - 128;
op += pc;
PUSH_PC (op);
jit_insn_branch_if (func, compare, &labels[op]);
break;
}
case BRgotoifnonnil:
{
jit_value_t v1 = POP;
jit_value_t compare = eq_nil (func, v1);
op = FETCH - 128;
op += pc;
PUSH_PC (op);
jit_insn_branch_if_not (func, compare, &labels[op]);
break;
}
case BRgotoifnilelsepop:
{
jit_value_t v1 = TOP;
jit_value_t compare = eq_nil (func, v1);
op = FETCH - 128;
op += pc;
PUSH_PC (op);
jit_insn_branch_if (func, compare, &labels[op]);
DISCARD (1);
break;
}
case BRgotoifnonnilelsepop:
{
jit_value_t v1 = TOP;
jit_value_t compare = eq_nil (func, v1);
op = FETCH - 128;
op += pc;
PUSH_PC (op);
jit_insn_branch_if_not (func, compare, &labels[op]);
DISCARD (1);
break;
}
case Breturn:
jit_insn_return (func, TOP);
pc = -1;
break;
case Bdiscard:
DISCARD (1);
break;
case Bsave_excursion:
jit_insn_call_native (func, "record_unwind_protect_excursion",
(void *) record_unwind_protect_excursion,
record_unwind_protect_excursion_signature,
NULL, 0, JIT_CALL_NOTHROW);
break;
case Bsave_current_buffer: /* Obsolete since ??. */
case Bsave_current_buffer_1:
jit_insn_call_native (func, "record_unwind_current_buffer",
(void *) record_unwind_current_buffer,
void_void_signature,
NULL, 0, JIT_CALL_NOTHROW);
break;
case Bsave_window_excursion: /* Obsolete since 24.1. */
{
compile_unary (func, "save-window-excursion",
native_save_window_excursion, TOP);
break;
}
case Bsave_restriction:
{
jit_value_t vals[2];
vals[0] = jit_value_create_nint_constant (func, jit_type_void_ptr,
(jit_nint) save_restriction_restore);
vals[1] = jit_insn_call_native (func, "save_restriction_save",
(void *) save_restriction_save,
lisp_void_signature,
NULL, 0, JIT_CALL_NOTHROW);
jit_insn_call_native (func, "record_unwind_protect",
(void *) record_unwind_protect,
record_unwind_protect_signature,
vals, 2, JIT_CALL_NOTHROW);
break;
}
case Bcatch: /* Obsolete since 24.4. */
{
jit_value_t args[3];
args[1] = jit_value_create_nint_constant (func, jit_type_void_ptr,
(jit_nint) eval_sub);
args[2] = POP;
args[0] = POP;
jit_value_t result = jit_insn_call_native (func, "internal_catch",
internal_catch,
internal_catch_signature,
args, 3,
JIT_CALL_NOTHROW);
PUSH (result);
break;
}
case Bpushcatch: /* New in 24.4. */
type = CATCHER;
goto pushhandler;
case Bpushconditioncase: /* New in 24.4. */
type = CONDITION_CASE;
pushhandler:
{
jit_value_t args[2];
jit_value_t handler, cond;
int handler_pc = FETCH2;
args[0] = POP;
args[1] = jit_value_create_nint_constant (func, jit_type_sys_int,
type);
handler = jit_insn_call_native (func, "push_handler", push_handler,
push_handler_signature,
args, 2, JIT_CALL_NOTHROW);
jit_value_t jmp
= jit_insn_add_relative (func, handler,
offsetof (struct handler, jmp));
/* FIXME probably should be using the same as the rest of
emacs. */
#ifdef HAVE__SETJMP
cond = jit_insn_call_native (func, "sys_setjmp", _setjmp,
setjmp_signature,
&jmp, 1, JIT_CALL_NOTHROW);
#else
cond = jit_insn_call_native (func, "sys_setjmp", setjmp,
setjmp_signature,
&jmp, 1, JIT_CALL_NOTHROW);
#endif
PUSH_PC (pc);
jit_insn_branch_if_not (func, cond, &labels[pc]);
/* Something threw to here. */
jit_value_t hlist = compile_next_handlerlist (func);
jit_value_t val
= jit_insn_load_relative (func, hlist,
offsetof (struct handler, val),
lisp_object_type);
PUSH (val);
PUSH_PC (handler_pc);
jit_insn_branch (func, &labels[handler_pc]);
pc = -1;
break;
}
case Bpophandler: /* New in 24.4. */
{
compile_next_handlerlist (func);
break;
}
case Bunwind_protect: /* FIXME: avoid closure for lexbind. */
{
jit_value_t args[2];
args[0] = jit_value_create_nint_constant (func, jit_type_void_ptr,
(jit_nint) bcall0);
args[1] = POP;
jit_insn_call_native (func, "record_unwind_protect",
(void *) record_unwind_protect,
record_unwind_protect_signature,
args, 2, JIT_CALL_NOTHROW);
break;
}
case Bcondition_case: /* Obsolete since 24.4. */
{
jit_value_t handlers = POP, body = POP;
compile_ternary (func, "condition-case",
internal_lisp_condition_case, TOP,
body, handlers);
break;
}
case Btemp_output_buffer_setup: /* Obsolete since 24.1. */
{
compile_unary (func, "temp-output-buffer-setup",
native_temp_output_buffer_setup, TOP);
break;
}
case Btemp_output_buffer_show: /* Obsolete since 24.1. */
{
jit_value_t v1 = POP;
jit_value_t v2 = TOP;
jit_value_t tem;
jit_insn_call_native (func, "temp_output_buffer_show",
(void *) temp_output_buffer_show,
temp_output_buffer_show_signature,
&v2, 1, JIT_CALL_NOTHROW);
jit_insn_store (func, TOP, v1);
tem = jit_value_create_nint_constant (func, jit_type_sys_int, 1);
jit_insn_call_native (func, "unbind_n", (void *) unbind_n,
unbind_n_signature, &tem, 1,
JIT_CALL_NOTHROW);
break;
}
case Bnth:
{
jit_value_t v2 = POP;
compile_binary (func, "nth", Fnth, TOP, v2);
break;
}
case Bsymbolp:
{
jit_value_t compare = compare_type (func, TOP, Lisp_Symbol);
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
case Bconsp:
{
jit_value_t compare = compare_type (func, TOP, Lisp_Cons);
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
case Bstringp:
{
jit_value_t compare = compare_type (func, TOP, Lisp_String);
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
case Blistp:
{
jit_value_t tem, nilval;
jit_label_t not_a_cons = jit_label_undefined;
jit_label_t not_nil = jit_label_undefined;
jit_value_t is_cons = compare_type (func, TOP, Lisp_Cons);
jit_insn_branch_if_not (func, is_cons, ¬_a_cons);
/* Is a cons. */
tem = CONSTANT (func, Qt);
jit_insn_store (func, TOP, tem);
jit_insn_branch (func, &labels[pc]);
jit_insn_label (func, ¬_a_cons);
nilval = CONSTANT (func, Qnil);
tem = jit_insn_eq (func, TOP, nilval);
jit_insn_branch_if_not (func, tem, ¬_nil);
/* Is nil. */
tem = CONSTANT (func, Qt);
jit_insn_store (func, TOP, tem);
jit_insn_branch (func, &labels[pc]);
jit_insn_label (func, ¬_nil);
jit_insn_store (func, TOP, nilval);
}
break;
case Bnot:
{
jit_value_t compare = eq_nil (func, TOP);
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
case Bcons:
{
jit_value_t v1 = POP;
compile_binary (func, "cons", Fcons, TOP, v1);
break;
}
case BlistN:
op = FETCH;
goto make_list;
case Blist1:
case Blist2:
case Blist3:
case Blist4:
op = op + 1 - Blist1;
make_list:
{
jit_value_t args[2];
int i;
args[1] = CONSTANT (func, Qnil);
for (i = 0; i < op; ++i)
{
args[0] = POP;
args[1] = jit_insn_call_native (func, "cons", (void *) Fcons,
binary_signature, args, 2,
JIT_CALL_NOTHROW);
}
PUSH (args[1]);
break;
}
case Blength:
compile_unary (func, "length", Flength, TOP);
break;
case Baref:
{
jit_value_t v1 = POP;
compile_binary (func, "aref", Faref, TOP, v1);
break;
}
case Baset:
{
jit_value_t v2 = POP, v1 = POP;
compile_ternary (func, "aset", Faset, TOP, v1, v2);
break;
}
case Bsymbol_value:
compile_unary (func, "symbol-value", Fsymbol_value, TOP);
break;
case Bsymbol_function:
compile_unary (func, "symbol-function", Fsymbol_function, TOP);
break;
case Bset:
{
jit_value_t v1 = POP;
compile_binary (func, "set", Fset, TOP, v1);
break;
}
case Bfset:
{
jit_value_t v1 = POP;
compile_binary (func, "fset", Ffset, TOP, v1);
break;
}
case Bget:
{
jit_value_t v1 = POP;
compile_binary (func, "get", Fget, TOP, v1);
break;
}
case Bsubstring:
{
jit_value_t v2 = POP, v1 = POP;
compile_ternary (func, "substring", Fsubstring, TOP, v1, v2);
break;
}
case Bconcat2:
{
COMPILE_CALLN (Fconcat, 2);
break;
}
case Bconcat3:
{
COMPILE_CALLN (Fconcat, 3);
break;
}
case Bconcat4:
{
COMPILE_CALLN (Fconcat, 4);
break;
}
case BconcatN:
{
op = FETCH;
COMPILE_CALLN (Fconcat, op);
break;
}
case Bsub1:
unary_intmath (func, TOP, SUB1, &labels[pc]);
break;
case Badd1:
unary_intmath (func, TOP, ADD1, &labels[pc]);
break;
case Beqlsign:
{
jit_value_t v1 = POP;
compile_arithcompare (func, "=", TOP, v1, ARITH_EQUAL);
break;
}
case Bgtr:
{
jit_value_t v1 = POP;
compile_arithcompare (func, ">", TOP, v1, ARITH_GRTR);
break;
}
case Blss:
{
jit_value_t v1 = POP;
compile_arithcompare (func, "<", TOP, v1, ARITH_LESS);
break;
}
case Bleq:
{
jit_value_t v1 = POP;
compile_arithcompare (func, "<=", TOP, v1, ARITH_LESS_OR_EQUAL);
break;
}
case Bgeq:
{
jit_value_t v1 = POP;
compile_arithcompare (func, ">=", TOP, v1, ARITH_GRTR_OR_EQUAL);
break;
}
case Bdiff:
{
COMPILE_CALLN (Fminus, 2);
break;
}
case Bnegate:
unary_intmath (func, TOP, NEGATE, &labels[pc]);
break;
case Bplus:
{
COMPILE_CALLN (Fplus, 2);
break;
}
case Bmax:
{
COMPILE_CALLN (Fmax, 2);
break;
}
case Bmin:
{
COMPILE_CALLN (Fmin, 2);
break;
}
case Bmult:
{
COMPILE_CALLN (Ftimes, 2);
break;
}
case Bquo:
{
COMPILE_CALLN (Fquo, 2);
break;
}
case Brem:
{
jit_value_t v1 = POP;
compile_binary (func, "rem", Frem, TOP, v1);
break;
}
case Bpoint:
PUSH (COMPILE_BUFFER_INT (pt));
break;
case Bgoto_char:
compile_unary (func, "goto-char", Fgoto_char, TOP);
break;
case Binsert:
{
COMPILE_CALLN (Finsert, 1);
break;
}
case BinsertN:
{
op = FETCH;
COMPILE_CALLN (Finsert, op);
break;
}
case Bpoint_max:
PUSH (COMPILE_BUFFER_INT (zv));
break;
case Bpoint_min:
PUSH (COMPILE_BUFFER_INT (begv));
break;
case Bchar_after:
compile_unary (func, "char-after", Fchar_after, TOP);
break;
case Bfollowing_char:
PUSH (compile_nullary (func, "following-char", Ffollowing_char));
break;
case Bpreceding_char:
PUSH (compile_nullary (func, "previous-char", Fprevious_char));
break;
case Bcurrent_column:
PUSH (compile_nullary (func, "current-column", Fcurrent_column));
break;
case Bindent_to:
{
jit_value_t tem = CONSTANT (func, Qnil);
compile_binary (func, "indent-to", Findent_to, TOP, tem);
break;
}
case Beolp:
PUSH (compile_nullary (func, "eolp", Feolp));
break;
case Beobp:
{
jit_value_t compare = COMPARE_BUFFER_INTS (pt, zv);
++stack_pointer;
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
case Bbolp:
PUSH (compile_nullary (func, "bolp", Fbolp));
break;
case Bbobp:
{
jit_value_t compare = COMPARE_BUFFER_INTS (pt, begv);
++stack_pointer;
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
case Bcurrent_buffer:
PUSH (compile_nullary (func, "current-buffer", Fcurrent_buffer));
break;
case Bset_buffer:
compile_unary (func, "set-buffer", Fset_buffer, TOP);
break;
case Binteractive_p: /* Obsolete since 24.1. */
{
jit_value_t arg = CONSTANT (func, Qinteractive_p);
jit_value_t result = jit_insn_call_native (func, "interactive-p",
(void *) call0,
unary_signature, &arg, 1,
JIT_CALL_NOTHROW);
PUSH (result);
break;
}
case Bforward_char:
compile_unary (func, "forward-char", Fforward_char, TOP);
break;
case Bforward_word:
compile_unary (func, "forward-word", Fforward_word, TOP);
break;
case Bskip_chars_forward:
{
jit_value_t v1 = POP;
compile_binary (func, "skip-chars-forward", Fskip_chars_forward,
TOP, v1);
break;
}
case Bskip_chars_backward:
{
jit_value_t v1 = POP;
compile_binary (func, "skip-chars-backward", Fskip_chars_backward,
TOP, v1);
break;
}
case Bforward_line:
compile_unary (func, "forward-line", Fforward_line, TOP);
break;
case Bchar_syntax:
compile_unary (func, "char-syntax", Fchar_syntax, TOP);
break;
case Bbuffer_substring:
{
jit_value_t v1 = POP;
compile_binary (func, "buffer-substring", Fbuffer_substring,
TOP, v1);
break;
}
case Bdelete_region:
{
jit_value_t v1 = POP;
compile_binary (func, "delete-region", Fdelete_region, TOP, v1);
break;
}
case Bnarrow_to_region:
{
jit_value_t v1 = POP;
compile_binary (func, "narrow-to-region", Fnarrow_to_region,
TOP, v1);
break;
}
case Bwiden:
PUSH (compile_nullary (func, "widen", Fwiden));
break;
case Bend_of_line:
compile_unary (func, "end-of-line", Fend_of_line, TOP);
break;
case Bset_marker:
{
jit_value_t v2 = POP, v1 = POP;
compile_ternary (func, "set-marker", Fset_marker, TOP, v1, v2);
break;
}
case Bmatch_beginning:
compile_unary (func, "match-beginning", Fmatch_beginning, TOP);
break;
case Bmatch_end:
compile_unary (func, "match-end", Fmatch_end, TOP);
break;
case Bupcase:
compile_unary (func, "upcase", Fupcase, TOP);
break;
case Bdowncase:
compile_unary (func, "downcase", Fdowncase, TOP);
break;
case Bstringeqlsign:
{
jit_value_t v1 = POP;
compile_binary (func, "string=", Fstring_equal, TOP, v1);
break;
}
case Bstringlss:
{
jit_value_t v1 = POP;
compile_binary (func, "string<", Fstring_lessp, TOP, v1);
break;
}
case Bequal:
{
jit_value_t v1 = POP;
compile_binary (func, "equal", Fequal, TOP, v1);
break;
}
case Bnthcdr:
{
jit_value_t v1 = POP;
compile_binary (func, "nthcdr", Fnthcdr, TOP, v1);
break;
}
case Belt:
{
jit_value_t v1 = POP;
compile_binary (func, "elt", Felt, TOP, v1);
break;
}
case Bmember:
{
jit_value_t v1 = POP;
compile_binary (func, "member", Fmember, TOP, v1);
break;
}
case Bassq:
{
jit_value_t v1 = POP;
compile_binary (func, "assq", Fassq, TOP, v1);
break;
}
case Bnreverse:
compile_unary (func, "nreverse", Fnreverse, TOP);
break;
case Bsetcar:
{
jit_value_t v1 = POP;
compile_binary (func, "setcar", Fsetcar, TOP, v1);
break;
}
case Bsetcdr:
{
jit_value_t v1 = POP;
compile_binary (func, "setcdr", Fsetcdr, TOP, v1);
break;
}
case Bcar_safe:
car_or_cdr (func, TOP, offsetof (struct Lisp_Cons, u.s.car),
&labels[pc], true,
&called_wtype, &wtype_label, wtype_arg);
break;
case Bcdr_safe:
car_or_cdr (func, TOP, offsetof (struct Lisp_Cons, u.s.u.cdr),
&labels[pc], true,
&called_wtype, &wtype_label, wtype_arg);
break;
case Bnconc:
{
COMPILE_CALLN (Fnconc, 2);
break;
}
case Bnumberp:
{
jit_label_t push_t = jit_label_undefined;
jit_label_t push_nil = jit_label_undefined;
jit_value_t val = POP;
jit_value_t type;
jit_value_t compare = compare_integerp (func, val, &type);
jit_insn_branch_if (func, compare, &push_t);
jit_value_t type_val
= jit_value_create_nint_constant (func, jit_type_void_ptr,
Lisp_Float);
compare = jit_insn_eq (func, type, type_val);
jit_insn_branch_if (func, compare, &push_t);
jit_insn_label (func, &push_nil);
PUSH (CONSTANT (func, Qnil));
jit_insn_branch (func, &labels[pc]);
--stack_pointer;
jit_insn_label (func, &push_t);
PUSH (CONSTANT (func, Qt));
break;
}
case Bintegerp:
{
jit_value_t compare = compare_integerp (func, TOP, NULL);
emit_qnil_or_qt (func, bytestr_data, pc, compare, TOP, labels);
break;
}
/* Handy byte-codes for lexical binding. */
case Bstack_ref1:
case Bstack_ref2:
case Bstack_ref3:
case Bstack_ref4:
case Bstack_ref5:
{
jit_value_t v1 = stack[stack_pointer - (op - Bstack_ref)];
PUSH (v1);
break;
}
case Bstack_ref6:
{
jit_value_t v1 = stack[stack_pointer - FETCH];
PUSH (v1);
break;
}
case Bstack_ref7:
{
jit_value_t v1 = stack[stack_pointer - FETCH2];
PUSH (v1);
break;
}
case Bstack_set:
/* stack-set-0 = discard; stack-set-1 = discard-1-preserve-tos. */
{
jit_value_t tos = POP;
op = FETCH;
if (op > 0)
jit_insn_store (func, stack[stack_pointer + 1 - op], tos);
break;
}
case Bstack_set2:
{
jit_value_t tos = POP;
op = FETCH2;
if (op > 0)
jit_insn_store (func, stack[stack_pointer + 1 - op], tos);
break;
}
case BdiscardN:
op = FETCH;
if (op & 0x80)
{
op &= 0x7F;
jit_insn_store (func, stack[stack_pointer - op], TOP);
}
DISCARD (op);
break;
case Bswitch:
/* The cases of Bswitch that we handle (which in theory is
all of them) are done in Bconstant, below. This is done
due to a design issue with Bswitch -- it should have
taken a constant pool index inline, but instead looks for
a constant on the stack. */
goto fail;
case Bconstant2:
op = FETCH2;
goto do_constant;
default:
case Bconstant:
{
if (op < Bconstant || op > Bconstant + vector_size)
goto fail;
op -= Bconstant;
do_constant:
/* See the Bswitch case for commentary. */
if (pc >= bytestr_length || bytestr_data[pc] != Bswitch)
{
jit_value_t c = CONSTANT (func, vectorp[op]);
PUSH (c);
break;
}
/* We're compiling Bswitch instead. */
++pc;
Lisp_Object htab = vectorp[op];
struct Lisp_Hash_Table *h = XHASH_TABLE (vectorp[op]);
/* Minimum and maximum PC values for the table. */
EMACS_INT min_pc = bytestr_length, max_pc = 0;
if (!find_hash_min_max_pc (h, &min_pc, &max_pc))
goto fail;
jit_value_t args[3];
args[0] = POP;
args[1] = CONSTANT (func, htab);
args[2] = CONSTANT (func, Qnil);
jit_value_t value
= jit_insn_call_native (func, "Fgethash", (void *) Fgethash,
ternary_signature, args, 3,
JIT_CALL_NOTHROW);
jit_value_t compare = jit_insn_eq (func, value, args[2]);
jit_label_t default_case = jit_function_reserve_label (func);
jit_insn_branch_if (func, compare, &default_case);
/* Note that we know the type because we check it when
walking the hash table, and the hash table is
(effectively) immutable. */
value = to_int (func, value);
jit_value_t min_pc_val
= jit_value_create_nint_constant (func, jit_type_sys_int,
min_pc);
value = jit_insn_sub (func, value, min_pc_val);
/* Initialize switch labels. */
for (int i = 0; i < max_pc - min_pc; ++i)
sw_labels[i] = default_case;
/* Fill in the switch table. */
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
if (!NILP (HASH_HASH (h, i)))
{
Lisp_Object pc = HASH_VALUE (h, i);
/* This was already checked by
find_hash_min_max_pc. */
eassert (FIXNUMP (pc));
EMACS_INT pcval = XFIXNUM (pc);
/* Make sure that the label we'll need is defined. */
if (labels[pcval] == jit_label_undefined)
labels[pcval] = jit_function_reserve_label (func);
sw_labels[pcval - min_pc] = labels[pcval];
PUSH_PC (pcval);
}
}
jit_insn_jump_table (func, value, sw_labels, max_pc - min_pc);
jit_insn_label (func, &default_case);
break;
}
}
}
if (scratch_slots_needed > 0)
{
jit_label_t init_start = jit_label_undefined;
jit_label_t init_end = jit_label_undefined;
jit_insn_label (func, &init_start);
jit_value_t scratch_size
= jit_value_create_nint_constant (func, jit_type_sys_int,
(scratch_slots_needed
* sizeof (Lisp_Object)));
jit_value_t allocated = jit_insn_alloca (func, scratch_size);
jit_insn_store (func, scratch, allocated);
jit_insn_label (func, &init_end);
jit_insn_move_blocks_to_start (func, init_start, init_end);
}
if (need_argfail)
{
jit_value_t args[3];
jit_insn_label (func, &argfail);
args[0] = mandatory_val;
args[1] = nonrest_val;
args[2] = n_args;
jit_insn_call_native (func, "wrong-number-of-arguments",
(void *) wrong_number_of_arguments,
wrong_number_of_arguments_signature,
args, 3, JIT_CALL_NORETURN);
}
if (called_wtype)
compile_wrong_type_argument (func, &wtype_label, wtype_arg);
if (!jit_function_compile (func))
{
/* Boo! */
fail:
jit_function_abandon (func);
result = NULL;
/* Be sure to clean up. */
while (pc_list != NULL)
{
struct pc_list *next = pc_list->next;
xfree (pc_list);
pc_list = next;
}
}
else
{
/* FIXME bogus cast. */
result->function.a0
= (Lisp_Object (*) (void)) jit_function_to_closure (func);
}
xfree (stack);
xfree (labels);
xfree (sw_labels);
xfree (stack_depths);
eassert (pc_list == NULL);
return result;
}
void
emacs_jit_compile (Lisp_Object func)
{
if (!emacs_jit_initialized || jit_disable)
return;
Lisp_Object bytestr = AREF (func, COMPILED_BYTECODE);
CHECK_STRING (bytestr);
if (STRING_MULTIBYTE (bytestr))
/* BYTESTR must have been produced by Emacs 20.2 or the earlier
because they produced a raw 8-bit string for byte-code and now
such a byte-code string is loaded as multibyte while raw 8-bit
characters converted to multibyte form. Thus, now we must
convert them back to the originally intended unibyte form. */
bytestr = Fstring_as_unibyte (bytestr);
ptrdiff_t bytestr_length = SBYTES (bytestr);
Lisp_Object vector = AREF (func, COMPILED_CONSTANTS);
CHECK_VECTOR (vector);
Lisp_Object *vectorp = XVECTOR (vector)->contents;
Lisp_Object maxdepth = AREF (func, COMPILED_STACK_DEPTH);
CHECK_FIXNAT (maxdepth);
jit_context_build_start (emacs_jit_context);
struct subr_function *subr = compile (bytestr_length, SDATA (bytestr),
XFIXNAT (maxdepth) + 1,
vectorp, ASIZE (vector),
AREF (func, COMPILED_ARGLIST));
XVECTOR (func)->contents[COMPILED_JIT_CODE] = XPL (subr);
jit_context_build_end (emacs_jit_context);
}
DEFUN ("jit-compile", Fjit_compile, Sjit_compile,
1, 1, 0,
doc: /* JIT compile a function. */)
(Lisp_Object func)
{
struct Lisp_Vector *vec;
if (!COMPILEDP (func))
error ("Not a byte-compiled function");
vec = XVECTOR (func);
if (XLP (vec->contents[COMPILED_JIT_CODE]) == NULL)
emacs_jit_compile (func);
return Qnil;
}
DEFUN ("jit-disassemble-to-string", Fjit_disassemble_to_string,
Sjit_disassemble_to_string, 1, 1, 0,
doc: /* Disassemble a JIT-compiled function and return a string with the disassembly. */)
(Lisp_Object func)
{
char *buffer = NULL;
size_t size = 0;
FILE *stream;
Lisp_Object str;
struct Lisp_Vector *vec;
jit_function_t cfunc;
struct subr_function *sfunc;
if (!COMPILEDP (func))
error ("Not a byte-compiled function");
vec = XVECTOR (func);
sfunc = (struct subr_function *) XLP (vec->contents[COMPILED_JIT_CODE]);
if (sfunc == NULL)
error ("Not JIT-compiled");
cfunc = jit_function_from_closure (emacs_jit_context, sfunc->function.a0);
#ifdef HAVE_OPEN_MEMSTREAM
stream = open_memstream (&buffer, &size);
jit_dump_function (stream, cfunc, "Function");
fclose (stream);
str = make_string (buffer, size);
xfree (buffer);
return str;
#else
static const char fname[] = "emacs-jit-disassemble.txt";
stream = emacs_fopen (fname, "w");
jit_dump_function (stream, cfunc, "Function");
fclose (stream);
struct stat st;
int fd;
if (stat (fname, &st) == 0
&& (fd = emacs_open (fname, O_RDONLY | O_TEXT, S_IRUSR)) >= 0)
{
buffer = xmalloc (st.st_size + 1);
ptrdiff_t nread = emacs_read (fd, buffer, st.st_size);
if (nread > 0)
{
size = nread;
buffer[size] = '\0';
str = make_string (buffer, size);
emacs_close (fd);
unlink (fname);
}
else
str = empty_unibyte_string;
xfree (buffer);
return str;
}
else
error ("jit-disassemble file could not be found");
#endif
}
void
syms_of_jit (void)
{
defsubr (&Sjit_compile);
defsubr (&Sjit_disassemble_to_string);
DEFSYM (Qinteractive_p, "interactive-p");
DEFVAR_BOOL ("jit-disable", jit_disable,
doc: /* Non-nil means disable JIT compilation even if supported. */);
jit_disable = 0;
}
void
init_jit (void)
{
#define LEN SUBR_MAX_ARGS
jit_type_t params[LEN];
int i;
#ifdef WINDOWSNT
if (!init_libjit_functions ())
return;
Vlibrary_cache = Fcons (Fcons (Qlibjit, Qt), Vlibrary_cache);
#endif
#if EMACS_INT_MAX <= LONG_MAX
/* 32-bit builds without wide ints, 64-bit builds on Posix hosts. */
lisp_object_type = jit_type_void_ptr;
#else
/* 64-bit builds on MS-Windows, 32-bit builds with wide ints. */
lisp_object_type = jit_type_sys_longlong;
#endif
jit_init ();
emacs_jit_context = jit_context_create ();
if (sizeof (ptrdiff_t) == 8)
ptrdiff_t_type = jit_type_ulong;
else
{
eassert (sizeof (ptrdiff_t) == 4);
ptrdiff_t_type = jit_type_uint;
}
for (i = 0; i < LEN; ++i)
params[i] = lisp_object_type;
for (i = 0; i < SUBR_MAX_ARGS; ++i)
subr_signature[i] = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type,
params, i, 1);
nullary_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type, params, 0,
1);
unary_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type, params, 1,
1);
binary_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type, params, 2,
1);
ternary_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type, params, 3,
1);
specbind_signature = jit_type_create_signature (jit_abi_cdecl,
jit_type_void, params, 2, 1);
record_unwind_protect_excursion_signature
= jit_type_create_signature (jit_abi_cdecl, jit_type_void, NULL, 0, 1);
lisp_void_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type, NULL, 0, 1);
void_void_signature = jit_type_create_signature (jit_abi_cdecl,
jit_type_void, NULL, 0, 1);
temp_output_buffer_show_signature
= jit_type_create_signature (jit_abi_cdecl, jit_type_void, params, 1, 1);
params[0] = jit_type_void_ptr;
record_unwind_protect_signature
= jit_type_create_signature (jit_abi_cdecl, jit_type_void, params, 2, 1);
params[0] = lisp_object_type;
params[2] = jit_type_sys_int;
arithcompare_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type,
params, 3, 1);
params[0] = jit_type_sys_int;
unbind_n_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type, params, 1,
1);
params[0] = ptrdiff_t_type;
params[1] = jit_type_void_ptr;
callN_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type, params, 2,
1);
compiled_signature = callN_signature;
params[0] = jit_type_sys_int;
params[1] = jit_type_sys_int;
params[2] = jit_type_sys_int;
wrong_number_of_arguments_signature
= jit_type_create_signature (jit_abi_cdecl, jit_type_void, params, 3, 1);
params[0] = lisp_object_type;
params[1] = lisp_object_type;
params[2] = lisp_object_type;
params[3] = jit_type_sys_int;
set_internal_signature
= jit_type_create_signature (jit_abi_cdecl, jit_type_void, params, 4, 1);
setjmp_signature = jit_type_create_signature (jit_abi_cdecl,
jit_type_sys_int,
params, 1, 1);
params[1] = jit_type_sys_int;
push_handler_signature = jit_type_create_signature (jit_abi_cdecl,
jit_type_void_ptr,
params, 2, 1);
params[1] = jit_type_void_ptr;
internal_catch_signature = jit_type_create_signature (jit_abi_cdecl,
lisp_object_type,
params, 3, 1);
emacs_jit_initialized = true;
}
#endif /* HAVE_LIBJIT */
|