summaryrefslogtreecommitdiff
path: root/test/ruby/test_zjit.rb
blob: 805ecb98b20ff3065724d95abcf4d790b03da70e (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
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
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
# frozen_string_literal: true
#
# This set of tests can be run with:
# make test-all TESTS=test/ruby/test_zjit.rb

require 'test/unit'
require 'envutil'
require_relative '../lib/jit_support'
return unless JITSupport.zjit_supported?

class TestZJIT < Test::Unit::TestCase
  def test_enabled
    assert_runs 'false', <<~RUBY, zjit: false
      RubyVM::ZJIT.enabled?
    RUBY
    assert_runs 'true', <<~RUBY, zjit: true
      RubyVM::ZJIT.enabled?
    RUBY
  end

  def test_stats_enabled
    assert_runs 'false', <<~RUBY, stats: false
      RubyVM::ZJIT.stats_enabled?
    RUBY
    assert_runs 'true', <<~RUBY, stats: true
      RubyVM::ZJIT.stats_enabled?
    RUBY
  end

  def test_stats_quiet
    # Test that --zjit-stats-quiet collects stats but doesn't print them
    script = <<~RUBY
      def test = 42
      test
      test
      puts RubyVM::ZJIT.stats_enabled?
    RUBY

    stats_header = "***ZJIT: Printing ZJIT statistics on exit***"

    # With --zjit-stats, stats should be printed to stderr
    out, err, status = eval_with_jit(script, stats: true)
    assert_success(out, err, status)
    assert_includes(err, stats_header)
    assert_equal("true\n", out)

    # With --zjit-stats-quiet, stats should NOT be printed but still enabled
    out, err, status = eval_with_jit(script, stats: :quiet)
    assert_success(out, err, status)
    refute_includes(err, stats_header)
    assert_equal("true\n", out)

    # With --zjit-stats=<path>, stats should be printed to the path
    Tempfile.create("zjit-stats-") {|tmp|
      stats_file = tmp.path
      tmp.puts("Lorem ipsum dolor sit amet, consectetur adipiscing elit, ...")
      tmp.close

      out, err, status = eval_with_jit(script, stats: stats_file)
      assert_success(out, err, status)
      refute_includes(err, stats_header)
      assert_equal("true\n", out)
      assert_equal stats_header, File.open(stats_file) {|f| f.gets(chomp: true)}, "should be overwritten"
    }
  end

  def test_enable_through_env
    child_env = {'RUBY_YJIT_ENABLE' => nil, 'RUBY_ZJIT_ENABLE' => '1'}
    assert_in_out_err([child_env, '-v'], '') do |stdout, stderr|
      assert_includes(stdout.first, '+ZJIT')
      assert_equal([], stderr)
    end
  end

  def test_zjit_enable
    # --disable-all is important in case the build/environment has YJIT enabled by
    # default through e.g. -DYJIT_FORCE_ENABLE. Can't enable ZJIT when YJIT is on.
    assert_separately(["--disable-all"], <<~'RUBY')
      refute_predicate RubyVM::ZJIT, :enabled?
      refute_predicate RubyVM::ZJIT, :stats_enabled?
      refute_includes RUBY_DESCRIPTION, "+ZJIT"

      RubyVM::ZJIT.enable

      assert_predicate RubyVM::ZJIT, :enabled?
      refute_predicate RubyVM::ZJIT, :stats_enabled?
      assert_includes RUBY_DESCRIPTION, "+ZJIT"
    RUBY
  end

  def test_zjit_disable
    assert_separately(["--zjit", "--zjit-disable"], <<~'RUBY')
      refute_predicate RubyVM::ZJIT, :enabled?
      refute_includes RUBY_DESCRIPTION, "+ZJIT"

      RubyVM::ZJIT.enable

      assert_predicate RubyVM::ZJIT, :enabled?
      assert_includes RUBY_DESCRIPTION, "+ZJIT"
    RUBY
  end

  def test_zjit_enable_respects_existing_options
    assert_separately(['--zjit-disable', '--zjit-stats-quiet'], <<~RUBY)
      refute_predicate RubyVM::ZJIT, :enabled?
      assert_predicate RubyVM::ZJIT, :stats_enabled?

      RubyVM::ZJIT.enable

      assert_predicate RubyVM::ZJIT, :enabled?
      assert_predicate RubyVM::ZJIT, :stats_enabled?
    RUBY
  end

  def test_call_itself
    assert_compiles '42', <<~RUBY, call_threshold: 2
      def test = 42.itself
      test
      test
    RUBY
  end

  def test_nil
    assert_compiles 'nil', %q{
      def test = nil
      test
    }
  end

  def test_putobject
    assert_compiles '1', %q{
      def test = 1
      test
    }
  end

  def test_putstring
    assert_compiles '""', %q{
      def test = "#{""}"
      test
    }, insns: [:putstring]
  end

  def test_putchilldedstring
    assert_compiles '""', %q{
      def test = ""
      test
    }, insns: [:putchilledstring]
  end

  def test_leave_param
    assert_compiles '5', %q{
      def test(n) = n
      test(5)
    }
  end

  def test_getglobal_with_warning
    assert_compiles('"rescued"', %q{
      Warning[:deprecated] = true

      module Warning
        def warn(message)
          raise
        end
      end

      def test
        $=
      rescue
        "rescued"
      end

      $VERBOSE = true
      test
    }, insns: [:getglobal])
  end

  def test_setglobal
    assert_compiles '1', %q{
      def test
        $a = 1
        $a
      end

      test
    }, insns: [:setglobal]
  end

  def test_string_intern
    assert_compiles ':foo123', %q{
      def test
        :"foo#{123}"
      end

      test
    }, insns: [:intern]
  end

  def test_duphash
    assert_compiles '{a: 1}', %q{
      def test
        {a: 1}
      end

      test
    }, insns: [:duphash]
  end

  def test_pushtoarray
    assert_compiles '[1, 2, 3]', %q{
      def test
        [*[], 1, 2, 3]
      end
      test
    }, insns: [:pushtoarray]
  end

  def test_splatarray_new_array
    assert_compiles '[1, 2, 3]', %q{
      def test a
        [*a, 3]
      end
      test [1, 2]
    }, insns: [:splatarray]
  end

  def test_splatarray_existing_array
    assert_compiles '[1, 2, 3]', %q{
      def foo v
        [1, 2, v]
      end
      def test a
        foo(*a)
      end
      test [3]
    }, insns: [:splatarray]
  end

  def test_concattoarray
    assert_compiles '[1, 2, 3]', %q{
      def test(*a)
        [1, 2, *a]
      end
      test 3
    }, insns: [:concattoarray]
  end

  def test_definedivar
    assert_compiles '[nil, "instance-variable", nil]', %q{
      def test
        v0 = defined?(@a)
        @a = nil
        v1 = defined?(@a)
        remove_instance_variable :@a
        v2 = defined?(@a)
        [v0, v1, v2]
      end
      test
    }, insns: [:definedivar]
  end

  def test_setglobal_with_trace_var_exception
    assert_compiles '"rescued"', %q{
      def test
        $a = 1
      rescue
        "rescued"
      end

      trace_var(:$a) { raise }
      test
    }, insns: [:setglobal]
  end

  def test_toplevel_binding
    # Not using assert_compiles, which doesn't use the toplevel frame for `test_script`.
    out, err, status = eval_with_jit(%q{
      a = 1
      b = 2
      TOPLEVEL_BINDING.local_variable_set(:b, 3)
      c = 4
      print [a, b, c]
    })
    assert_success(out, err, status)
    assert_equal "[1, 3, 4]", out
  end

  def test_toplevel_local_after_eval
    # Not using assert_compiles, which doesn't use the toplevel frame for `test_script`.
    out, err, status = eval_with_jit(%q{
      a = 1
      b = 2
      eval('b = 3')
      c = 4
      print [a, b, c]
    })
    assert_success(out, err, status)
    assert_equal "[1, 3, 4]", out
  end

  def test_getlocal_after_eval
    assert_compiles '2', %q{
      def test
        a = 1
        eval('a = 2')
        a
      end
      test
    }
  end

  def test_getlocal_after_instance_eval
    assert_compiles '2', %q{
      def test
        a = 1
        instance_eval('a = 2')
        a
      end
      test
    }
  end

  def test_getlocal_after_module_eval
    assert_compiles '2', %q{
      def test
        a = 1
        Kernel.module_eval('a = 2')
        a
      end
      test
    }
  end

  def test_getlocal_after_class_eval
    assert_compiles '2', %q{
      def test
        a = 1
        Kernel.class_eval('a = 2')
        a
      end
      test
    }
  end

  def test_setlocal
    assert_compiles '3', %q{
      def test(n)
        m = n
        m
      end
      test(3)
    }
  end

  def test_return_nonparam_local
    # Use dead code (if false) to create a local without initialization instructions.
    assert_compiles 'nil', %q{
      def foo(a)
        if false
          x = nil
        end
        x
      end
      def test = foo(1)
      test
      test
    }, call_threshold: 2
  end

  def test_nonparam_local_nil_in_jit_call
    # Non-parameter locals must be initialized to nil in JIT-to-JIT calls.
    # Use dead code (if false) to create locals without initialization instructions.
    # Then eval a string that accesses the uninitialized locals.
    assert_compiles '["x", "x", "x", "x"]', %q{
      def f(a)
        a ||= 1
        if false; b = 1; end
        eval("-> { p 'x#{b}' }")
      end

      4.times.map { f(1).call }
    }, call_threshold: 2
  end

  def test_setlocal_on_eval
    assert_compiles '1', %q{
      @b = binding
      eval('a = 1', @b)
      eval('a', @b)
    }
  end

  def test_optional_arguments
    assert_compiles '[[1, 2, 3], [10, 20, 3], [100, 200, 300]]', %q{
      def test(a, b = 2, c = 3)
        [a, b, c]
      end
      [test(1), test(10, 20), test(100, 200, 300)]
    }
  end

  def test_optional_arguments_setlocal
    assert_compiles '[[2, 2], [1, nil]]', %q{
      def test(a = (b = 2))
        [a, b]
      end
      [test, test(1)]
    }
  end

  def test_optional_arguments_cyclic
    assert_compiles '[nil, 1]', %q{
      test = proc { |a=a| a }
      [test.call, test.call(1)]
    }
  end

  def test_optional_arguments_side_exit
    # This leads to FailedOptionalArguments, so not using assert_compiles
    assert_runs '[:foo, nil, 1]', %q{
      def test(a = (def foo = nil)) = a
      [test, (undef :foo), test(1)]
    }
  end

  def test_getblockparamproxy
    assert_compiles '1', %q{
      def test(&block)
        0.then(&block)
      end
      test { 1 }
    }, insns: [:getblockparamproxy]
  end

  def test_call_a_forwardable_method
    assert_runs '[]', %q{
      def test_root = forwardable
      def forwardable(...) = Array.[](...)
      test_root
      test_root
    }, call_threshold: 2
  end

  def test_setlocal_on_eval_with_spill
    assert_compiles '1', %q{
      @b = binding
      eval('a = 1; itself', @b)
      eval('a', @b)
    }
  end

  def test_nested_local_access
    assert_compiles '[1, 2, 3]', %q{
      1.times do |l2|
        1.times do |l1|
          define_method(:test) do
            l1 = 1
            l2 = 2
            l3 = 3
            [l1, l2, l3]
          end
        end
      end

      test
      test
      test
    }, call_threshold: 3, insns: [:getlocal, :setlocal, :getlocal_WC_0, :setlocal_WC_1]
  end

  def test_send_with_local_written_by_blockiseq
    assert_compiles '[1, 2]', %q{
      def test
        l1 = nil
        l2 = nil
        tap do |_|
          l1 = 1
          tap do |_|
            l2 = 2
          end
        end

        [l1, l2]
      end

      test
      test
    }, call_threshold: 2
  end

  def test_send_without_block
    assert_compiles '[1, 2, 3]', %q{
      def foo = 1
      def bar(a) = a - 1
      def baz(a, b) = a - b

      def test1 = foo
      def test2 = bar(3)
      def test3 = baz(4, 1)

      [test1, test2, test3]
    }
  end

  def test_send_with_six_args
    assert_compiles '[1, 2, 3, 4, 5, 6]', %q{
      def foo(a1, a2, a3, a4, a5, a6)
        [a1, a2, a3, a4, a5, a6]
      end

      def test
        foo(1, 2, 3, 4, 5, 6)
      end

      test # profile send
      test
    }, call_threshold: 2
  end

  def test_send_on_heap_object_in_spilled_arg
    # This leads to a register spill, so not using `assert_compiles`
    assert_runs 'Hash', %q{
      def entry(a1, a2, a3, a4, a5, a6, a7, a8, a9)
        a9.itself.class
      end

      entry(1, 2, 3, 4, 5, 6, 7, 8, {}) # profile
      entry(1, 2, 3, 4, 5, 6, 7, 8, {})
    }, call_threshold: 2
  end

  def test_send_exit_with_uninitialized_locals
    assert_runs 'nil', %q{
      def entry(init)
        function_stub_exit(init)
      end

      def function_stub_exit(init)
        uninitialized_local = 1 if init
        uninitialized_local
      end

      entry(true) # profile and set 1 to the local slot
      entry(false)
    }, call_threshold: 2, allowed_iseqs: 'entry@-e:2'
  end

  def test_send_optional_arguments
    assert_compiles '[[1, 2], [3, 4]]', %q{
      def test(a, b = 2) = [a, b]
      def entry = [test(1), test(3, 4)]
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_nil_block_arg
    assert_compiles 'false', %q{
      def test = block_given?
      def entry = test(&nil)
      test
    }
  end

  def test_send_symbol_block_arg
    assert_compiles '["1", "2"]', %q{
      def test = [1, 2].map(&:to_s)
      test
    }
  end

  def test_send_variadic_with_block
    assert_compiles '[[1, "a"], [2, "b"], [3, "c"]]', %q{
      A = [1, 2, 3]
      B = ["a", "b", "c"]

      def test
        result = []
        A.zip(B) { |x, y| result << [x, y] }
        result
      end

      test; test
    }, call_threshold: 2
  end

  def test_send_splat
    assert_runs '[1, 2]', %q{
      def test(a, b) = [a, b]
      def entry(arr) = test(*arr)
      entry([1, 2])
    }
  end

  def test_send_kwarg
    assert_runs '[1, 2]', %q{
      def test(a:, b:) = [a, b]
      def entry = test(b: 2, a: 1) # change order
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwarg_optional
    assert_compiles '[1, 2]', %q{
      def test(a: 1, b: 2) = [a, b]
      def entry = test
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwarg_optional_too_many
    assert_compiles '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]', %q{
      def test(a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10) = [a, b, c, d, e, f, g, h, i, j]
      def entry = test
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwarg_required_and_optional
    assert_compiles '[3, 2]', %q{
      def test(a:, b: 2) = [a, b]
      def entry = test(a: 3)
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwarg_to_hash
    assert_compiles '{a: 3}', %q{
      def test(hash) = hash
      def entry = test(a: 3)
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwarg_to_ccall
    assert_compiles '["a", "b", "c"]', %q{
      def test(s) = s.each_line(chomp: true).to_a
      def entry = test(%(a\nb\nc))
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwarg_and_block_to_ccall
    assert_compiles '["a", "b", "c"]', %q{
      def test(s)
        a = []
        s.each_line(chomp: true) { |l| a << l }
        a
      end
      def entry = test(%(a\nb\nc))
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwarg_with_too_many_args_to_c_call
    assert_compiles '"a b c d {kwargs: :e}"', %q{
      def test(a:, b:, c:, d:, e:) = sprintf("%s %s %s %s %s", a, b, c, d, kwargs: e)
      def entry = test(e: :e, d: :d, c: :c, a: :a, b: :b)
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwsplat
    assert_compiles '3', %q{
      def test(a:) = a
      def entry = test(**{a: 3})
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_kwrest
    assert_compiles '{a: 3}', %q{
      def test(**kwargs) = kwargs
      def entry = test(a: 3)
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_req_kwreq
    assert_compiles '[1, 3]', %q{
      def test(a, c:) = [a, c]
      def entry = test(1, c: 3)
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_req_opt_kwreq
    assert_compiles '[[1, 2, 3], [-1, -2, -3]]', %q{
      def test(a, b = 2, c:) = [a, b, c]
      def entry = [test(1, c: 3), test(-1, -2, c: -3)] # specify all, change kw order
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_req_opt_kwreq_kwopt
    assert_compiles '[[1, 2, 3, 4], [-1, -2, -3, -4]]', %q{
      def test(a, b = 2, c:, d: 4) = [a, b, c, d]
      def entry = [test(1, c: 3), test(-1, -2, d: -4, c: -3)] # specify all, change kw order
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_unexpected_keyword
    assert_compiles ':error', %q{
      def test(a: 1) = a*2
      def entry
        test(z: 2)
      rescue ArgumentError
        :error
      end

      entry
      entry
    }, call_threshold: 2
  end

  def test_send_all_arg_types
    assert_compiles '[:req, :opt, :post, :kwr, :kwo, true]', %q{
      def test(a, b = :opt, c, d:, e: :kwo) = [a, b, c, d, e, block_given?]
      def entry = test(:req, :post, d: :kwr) {}
      entry
      entry
    }, call_threshold: 2
  end

  def test_send_ccall_variadic_with_different_receiver_classes
    assert_compiles '[true, true]', %q{
      def test(obj) = obj.start_with?("a")
      [test("abc"), test(:abc)]
    }, call_threshold: 2
  end

  def test_forwardable_iseq
    assert_compiles '1', %q{
      def test(...) = 1
      test
    }
  end

  def test_sendforward
    assert_compiles '[1, 2]', %q{
      def callee(a, b) = [a, b]
      def test(...) = callee(...)
      test(1, 2)
    }, insns: [:sendforward]
  end

  def test_iseq_with_optional_arguments
    assert_compiles '[[1, 2], [3, 4]]', %q{
      def test(a, b = 2) = [a, b]
      [test(1), test(3, 4)]
    }
  end

  def test_invokesuper
    assert_compiles '[6, 60]', %q{
      class Foo
        def foo(a) = a + 1
        def bar(a) = a + 10
      end

      class Bar < Foo
        def foo(a) = super(a) + 2
        def bar(a) = super + 20
      end

      bar = Bar.new
      [bar.foo(3), bar.bar(30)]
    }
  end

  def test_invokesuper_with_local_written_by_blockiseq
    # Using `assert_runs` because we don't compile invokeblock yet
    assert_runs '3', %q{
      class Foo
        def test
          yield
        end
      end

      class Bar < Foo
        def test
          a = 1
          super do
            a += 2
          end
          a
        end
      end

      Bar.new.test
    }
  end

  def test_invokebuiltin
    # Not using assert_compiles due to register spill
    assert_runs '["."]', %q{
      def test = Dir.glob(".")
      test
    }
  end

  def test_invokebuiltin_delegate
    assert_compiles '[[], true]', %q{
      def test = [].clone(freeze: true)
      r = test
      [r, r.frozen?]
    }
  end

  def test_opt_plus_const
    assert_compiles '3', %q{
      def test = 1 + 2
      test # profile opt_plus
      test
    }, call_threshold: 2
  end

  def test_opt_plus_fixnum
    assert_compiles '3', %q{
      def test(a, b) = a + b
      test(0, 1) # profile opt_plus
      test(1, 2)
    }, call_threshold: 2
  end

  def test_opt_plus_chain
    assert_compiles '6', %q{
      def test(a, b, c) = a + b + c
      test(0, 1, 2) # profile opt_plus
      test(1, 2, 3)
    }, call_threshold: 2
  end

  def test_opt_plus_left_imm
    assert_compiles '3', %q{
      def test(a) = 1 + a
      test(1) # profile opt_plus
      test(2)
    }, call_threshold: 2
  end

  def test_opt_plus_type_guard_exit
    assert_compiles '[3, 3.0]', %q{
      def test(a) = 1 + a
      test(1) # profile opt_plus
      [test(2), test(2.0)]
    }, call_threshold: 2
  end

  def test_opt_plus_type_guard_exit_with_locals
    assert_compiles '[6, 6.0]', %q{
      def test(a)
        local = 3
        1 + a + local
      end
      test(1) # profile opt_plus
      [test(2), test(2.0)]
    }, call_threshold: 2
  end

  def test_opt_plus_type_guard_nested_exit
    assert_compiles '[4, 4.0]', %q{
      def side_exit(n) = 1 + n
      def jit_frame(n) = 1 + side_exit(n)
      def entry(n) = jit_frame(n)
      entry(2) # profile send
      [entry(2), entry(2.0)]
    }, call_threshold: 2
  end

  def test_opt_plus_type_guard_nested_exit_with_locals
    assert_compiles '[9, 9.0]', %q{
      def side_exit(n)
        local = 2
        1 + n + local
      end
      def jit_frame(n)
        local = 3
        1 + side_exit(n) + local
      end
      def entry(n) = jit_frame(n)
      entry(2) # profile send
      [entry(2), entry(2.0)]
    }, call_threshold: 2
  end

  # Test argument ordering
  def test_opt_minus
    assert_compiles '2', %q{
      def test(a, b) = a - b
      test(2, 1) # profile opt_minus
      test(6, 4)
    }, call_threshold: 2
  end

  def test_opt_mult
    assert_compiles '6', %q{
      def test(a, b) = a * b
      test(1, 2) # profile opt_mult
      test(2, 3)
    }, call_threshold: 2
  end

  def test_opt_mult_overflow
    assert_compiles '[6, -6, 9671406556917033397649408, -9671406556917033397649408, 21267647932558653966460912964485513216]', %q{
      def test(a, b)
        a * b
      end
      test(1, 1) # profile opt_mult

      r1 = test(2, 3)
      r2 = test(2, -3)
      r3 = test(2 << 40, 2 << 41)
      r4 = test(2 << 40, -2 << 41)
      r5 = test(1 << 62, 1 << 62)

      [r1, r2, r3, r4, r5]
    }, call_threshold: 2
  end

  def test_opt_eq
    assert_compiles '[true, false]', %q{
      def test(a, b) = a == b
      test(0, 2) # profile opt_eq
      [test(1, 1), test(0, 1)]
    }, insns: [:opt_eq], call_threshold: 2
  end

  def test_opt_eq_with_minus_one
    assert_compiles '[false, true]', %q{
      def test(a) = a == -1
      test(1) # profile opt_eq
      [test(0), test(-1)]
    }, insns: [:opt_eq], call_threshold: 2
  end

  def test_opt_neq_dynamic
    # TODO(max): Don't split this test; instead, run all tests with and without
    # profiling.
    assert_compiles '[false, true]', %q{
      def test(a, b) = a != b
      test(0, 2) # profile opt_neq
      [test(1, 1), test(0, 1)]
    }, insns: [:opt_neq], call_threshold: 1
  end

  def test_opt_neq_fixnum
    assert_compiles '[false, true]', %q{
      def test(a, b) = a != b
      test(0, 2) # profile opt_neq
      [test(1, 1), test(0, 1)]
    }, call_threshold: 2
  end

  def test_opt_lt
    assert_compiles '[true, false, false]', %q{
      def test(a, b) = a < b
      test(2, 3) # profile opt_lt
      [test(0, 1), test(0, 0), test(1, 0)]
    }, insns: [:opt_lt], call_threshold: 2
  end

  def test_opt_lt_with_literal_lhs
    assert_compiles '[false, false, true]', %q{
      def test(n) = 2 < n
      test(2) # profile opt_lt
      [test(1), test(2), test(3)]
    }, insns: [:opt_lt], call_threshold: 2
  end

  def test_opt_le
    assert_compiles '[true, true, false]', %q{
      def test(a, b) = a <= b
      test(2, 3) # profile opt_le
      [test(0, 1), test(0, 0), test(1, 0)]
    }, insns: [:opt_le], call_threshold: 2
  end

  def test_opt_gt
    assert_compiles '[false, false, true]', %q{
      def test(a, b) = a > b
      test(2, 3) # profile opt_gt
      [test(0, 1), test(0, 0), test(1, 0)]
    }, insns: [:opt_gt], call_threshold: 2
  end

  def test_opt_empty_p
    assert_compiles('[false, false, true]', <<~RUBY, insns: [:opt_empty_p])
      def test(x) = x.empty?
      return test([1]), test("1"), test({})
    RUBY
  end

  def test_opt_succ
    assert_compiles('[0, "B"]', <<~RUBY, insns: [:opt_succ])
      def test(obj) = obj.succ
      return test(-1), test("A")
    RUBY
  end

  def test_opt_and
    assert_compiles('[1, [3, 2, 1]]', <<~RUBY, insns: [:opt_and])
      def test(x, y) = x & y
      return test(0b1101, 3), test([3, 2, 1, 4], [8, 1, 2, 3])
    RUBY
  end

  def test_opt_or
    assert_compiles('[11, [3, 2, 1]]', <<~RUBY, insns: [:opt_or])
      def test(x, y) = x | y
      return test(0b1000, 3), test([3, 2, 1], [1, 2, 3])
    RUBY
  end

  def test_fixnum_and
    assert_compiles '[1, 2, 4]', %q{
      def test(a, b) = a & b
      [
        test(5, 3),
        test(0b011, 0b110),
        test(-0b011, 0b110)
      ]
    }, call_threshold: 2, insns: [:opt_and]
  end

  def test_fixnum_and_side_exit
    assert_compiles '[2, 2, false]', %q{
      def test(a, b) = a & b
      [
        test(2, 2),
        test(0b011, 0b110),
        test(true, false)
      ]
    }, call_threshold: 2, insns: [:opt_and]
  end

  def test_fixnum_or
    assert_compiles '[7, 3, -3]', %q{
      def test(a, b) = a | b
      [
        test(5, 3),
        test(1, 2),
        test(1, -4)
      ]
    }, call_threshold: 2, insns: [:opt_or]
  end

  def test_fixnum_or_side_exit
    assert_compiles '[3, 2, true]', %q{
      def test(a, b) = a | b
      [
        test(1, 2),
        test(2, 2),
        test(true, false)
      ]
    }, call_threshold: 2, insns: [:opt_or]
  end

  def test_fixnum_xor
    assert_compiles '[6, -8, 3]', %q{
      def test(a, b) = a ^ b
      [
        test(5, 3),
        test(-5, 3),
        test(1, 2)
      ]
    }, call_threshold: 2
  end

  def test_fixnum_xor_side_exit
    assert_compiles '[6, 6, true]', %q{
      def test(a, b) = a ^ b
      [
        test(5, 3),
        test(5, 3),
        test(true, false)
      ]
    }, call_threshold: 2
  end

  def test_fixnum_mul
    assert_compiles '12', %q{
      C = 3
      def test(n) = C * n
      test(4)
      test(4)
      test(4)
    }, call_threshold: 2, insns: [:opt_mult]
  end

  def test_fixnum_div
    assert_compiles '12', %q{
      C = 48
      def test(n) = C / n
      test(4)
      test(4)
    }, call_threshold: 2, insns: [:opt_div]
  end

  def test_fixnum_floor
    assert_compiles '0', %q{
      C = 3
      def test(n) = C / n
      test(4)
      test(4)
    }, call_threshold: 2, insns: [:opt_div]
  end

  def test_fixnum_div_zero
    assert_runs '"divided by 0"', %q{
      def test(n)
        n / 0
      rescue ZeroDivisionError => e
        e.message
      end

      test(0)
      test(0)
    }, call_threshold: 2, insns: [:opt_div]
  end

  def test_opt_not
    assert_compiles('[true, true, false]', <<~RUBY, insns: [:opt_not])
      def test(obj) = !obj
      return test(nil), test(false), test(0)
    RUBY
  end

  def test_opt_regexpmatch2
    assert_compiles('[1, nil]', <<~RUBY, insns: [:opt_regexpmatch2])
      def test(haystack) = /needle/ =~ haystack
      return test("kneedle"), test("")
    RUBY
  end

  def test_opt_ge
    assert_compiles '[false, true, true]', %q{
      def test(a, b) = a >= b
      test(2, 3) # profile opt_ge
      [test(0, 1), test(0, 0), test(1, 0)]
    }, insns: [:opt_ge], call_threshold: 2
  end

  def test_opt_new_does_not_push_frame
    assert_compiles 'nil', %q{
      class Foo
        attr_reader :backtrace

        def initialize
          @backtrace = caller
        end
      end
      def test = Foo.new

      foo = test
      foo.backtrace.find do |frame|
        frame.include?('Class#new')
      end
    }, insns: [:opt_new]
  end

  def test_opt_new_with_redefined
    assert_compiles '"foo"', %q{
      class Foo
        def self.new = "foo"

        def initialize = raise("unreachable")
      end
      def test = Foo.new

      test
    }, insns: [:opt_new]
  end

  def test_opt_new_invalidate_new
    assert_compiles '["Foo", "foo"]', %q{
      class Foo; end
      def test = Foo.new
      test; test
      result = [test.class.name]
      def Foo.new = "foo"
      result << test
      result
    }, insns: [:opt_new], call_threshold: 2
  end

  def test_opt_new_with_custom_allocator
    assert_compiles '"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"', %q{
      require "digest"
      def test = Digest::SHA256.new.hexdigest
      test; test
    }, insns: [:opt_new], call_threshold: 2
  end

  def test_opt_new_with_custom_allocator_raises
    assert_compiles '[42, 42]', %q{
      require "digest"
      class C < Digest::Base; end
      def test
        begin
          Digest::Base.new
        rescue NotImplementedError
          42
        end
      end
      [test, test]
    }, insns: [:opt_new], call_threshold: 2
  end

  def test_opt_newarray_send_include_p
    assert_compiles '[true, false]', %q{
      def test(x)
        [:y, 1, Object.new].include?(x)
      end
      [test(1), test("n")]
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_opt_newarray_send_include_p_redefined
    assert_compiles '[:true, :false]', %q{
      class Array
        alias_method :old_include?, :include?
        def include?(x)
          old_include?(x) ? :true : :false
        end
      end

      def test(x)
        [:y, 1, Object.new].include?(x)
      end
      [test(1), test("n")]
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_opt_duparray_send_include_p
    assert_compiles '[true, false]', %q{
      def test(x)
        [:y, 1].include?(x)
      end
      [test(1), test("n")]
    }, insns: [:opt_duparray_send], call_threshold: 1
  end

  def test_opt_duparray_send_include_p_redefined
    assert_compiles '[:true, :false]', %q{
      class Array
        alias_method :old_include?, :include?
        def include?(x)
          old_include?(x) ? :true : :false
        end
      end

      def test(x)
        [:y, 1].include?(x)
      end
      [test(1), test("n")]
    }, insns: [:opt_duparray_send], call_threshold: 1
  end

  def test_opt_newarray_send_pack_buffer
    assert_compiles '["ABC", "ABC", "ABC", "ABC"]', %q{
      def test(num, buffer)
        [num].pack('C', buffer:)
      end
      buf = ""
      [test(65, buf), test(66, buf), test(67, buf), buf]
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_opt_newarray_send_pack_buffer_redefined
    assert_compiles '["b", "A"]', %q{
      class Array
        alias_method :old_pack, :pack
        def pack(fmt, buffer: nil)
          old_pack(fmt, buffer: buffer)
          "b"
        end
      end

      def test(num, buffer)
        [num].pack('C', buffer:)
      end
      buf = ""
      [test(65, buf), buf]
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_opt_newarray_send_hash
    assert_compiles 'Integer', %q{
      def test(x)
        [1, 2, x].hash
      end
      test(20).class
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_opt_newarray_send_hash_redefined
    assert_compiles '42', %q{
      Array.class_eval { def hash = 42 }

      def test(x)
        [1, 2, x].hash
      end
      test(20)
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_opt_newarray_send_max
    assert_compiles '[20, 40]', %q{
      def test(a,b) = [a,b].max
      [test(10, 20), test(40, 30)]
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_opt_newarray_send_max_redefined
    assert_compiles '[60, 90]', %q{
      class Array
        alias_method :old_max, :max
        def max
          old_max * 2
        end
      end

      def test(a,b) = [a,b].max
      [test(15, 30), test(45, 35)]
    }, insns: [:opt_newarray_send], call_threshold: 1
  end

  def test_new_hash_empty
    assert_compiles '{}', %q{
      def test = {}
      test
    }, insns: [:newhash]
  end

  def test_new_hash_nonempty
    assert_compiles '{"key" => "value", 42 => 100}', %q{
      def test
        key = "key"
        value = "value"
        num = 42
        result = 100
        {key => value, num => result}
      end
      test
    }, insns: [:newhash]
  end

  def test_new_hash_single_key_value
    assert_compiles '{"key" => "value"}', %q{
      def test = {"key" => "value"}
      test
    }, insns: [:newhash]
  end

  def test_new_hash_with_computation
    assert_compiles '{"sum" => 5, "product" => 6}', %q{
      def test(a, b)
        {"sum" => a + b, "product" => a * b}
      end
      test(2, 3)
    }, insns: [:newhash]
  end

  def test_new_hash_with_user_defined_hash_method
    assert_runs 'true', %q{
      class CustomKey
        attr_reader :val

        def initialize(val)
          @val = val
        end

        def hash
          @val.hash
        end

        def eql?(other)
          other.is_a?(CustomKey) && @val == other.val
        end
      end

      def test
        key = CustomKey.new("key")
        hash = {key => "value"}
        hash[key] == "value"
      end
      test
    }
  end

  def test_new_hash_with_user_hash_method_exception
    assert_runs 'RuntimeError', %q{
      class BadKey
        def hash
          raise "Hash method failed!"
        end
      end

      def test
        key = BadKey.new
        {key => "value"}
      end

      begin
        test
      rescue => e
        e.class
      end
    }
  end

  def test_new_hash_with_user_eql_method_exception
    assert_runs 'RuntimeError', %q{
      class BadKey
        def hash
          42
        end

        def eql?(other)
          raise "Eql method failed!"
        end
      end

      def test
        key1 = BadKey.new
        key2 = BadKey.new
        {key1 => "value1", key2 => "value2"}
      end

      begin
        test
      rescue => e
        e.class
      end
    }
  end

  def test_opt_hash_freeze
    assert_compiles "[{}, 5]", %q{
      def test = {}.freeze
      result = [test]
      class Hash
        def freeze = 5
      end
      result << test
    }, insns: [:opt_hash_freeze], call_threshold: 1
  end

  def test_opt_hash_freeze_rewritten
    assert_compiles "5", %q{
      class Hash
        def freeze = 5
      end
      def test = {}.freeze
      test
    }, insns: [:opt_hash_freeze], call_threshold: 1
  end

  def test_opt_aset_hash
    assert_compiles '42', %q{
      def test(h, k, v)
        h[k] = v
      end
      h = {}
      test(h, :key, 42)
      test(h, :key, 42)
      h[:key]
    }, call_threshold: 2, insns: [:opt_aset]
  end

  def test_opt_aset_hash_returns_value
    assert_compiles '100', %q{
      def test(h, k, v)
        h[k] = v
      end
      test({}, :key, 100)
      test({}, :key, 100)
    }, call_threshold: 2
  end

  def test_opt_aset_hash_string_key
    assert_compiles '"bar"', %q{
      def test(h, k, v)
        h[k] = v
      end
      h = {}
      test(h, "foo", "bar")
      test(h, "foo", "bar")
      h["foo"]
    }, call_threshold: 2
  end

  def test_opt_aset_hash_subclass
    assert_compiles '42', %q{
      class MyHash < Hash; end
      def test(h, k, v)
        h[k] = v
      end
      h = MyHash.new
      test(h, :key, 42)
      test(h, :key, 42)
      h[:key]
    }, call_threshold: 2
  end

  def test_opt_aset_hash_too_few_args
    assert_compiles '"ArgumentError"', %q{
      def test(h)
        h.[]= 123
      rescue ArgumentError
        "ArgumentError"
      end
      test({})
      test({})
    }, call_threshold: 2
  end

  def test_opt_aset_hash_too_many_args
    assert_compiles '"ArgumentError"', %q{
      def test(h)
        h[:a, :b] = :c
      rescue ArgumentError
        "ArgumentError"
      end
      test({})
      test({})
    }, call_threshold: 2
  end

  def test_opt_ary_freeze
    assert_compiles "[[], 5]", %q{
      def test = [].freeze
      result = [test]
      class Array
        def freeze = 5
      end
      result << test
    }, insns: [:opt_ary_freeze], call_threshold: 1
  end

  def test_opt_ary_freeze_rewritten
    assert_compiles "5", %q{
      class Array
        def freeze = 5
      end
      def test = [].freeze
      test
    }, insns: [:opt_ary_freeze], call_threshold: 1
  end

  def test_opt_str_freeze
    assert_compiles "[\"\", 5]", %q{
      def test = ''.freeze
      result = [test]
      class String
        def freeze = 5
      end
      result << test
    }, insns: [:opt_str_freeze], call_threshold: 1
  end

  def test_opt_str_freeze_rewritten
    assert_compiles "5", %q{
      class String
        def freeze = 5
      end
      def test = ''.freeze
      test
    }, insns: [:opt_str_freeze], call_threshold: 1
  end

  def test_opt_str_uminus
    assert_compiles "[\"\", 5]", %q{
      def test = -''
      result = [test]
      class String
        def -@ = 5
      end
      result << test
    }, insns: [:opt_str_uminus], call_threshold: 1
  end

  def test_opt_str_uminus_rewritten
    assert_compiles "5", %q{
      class String
        def -@ = 5
      end
      def test = -''
      test
    }, insns: [:opt_str_uminus], call_threshold: 1
  end

  def test_new_array_empty
    assert_compiles '[]', %q{
      def test = []
      test
    }, insns: [:newarray]
  end

  def test_new_array_nonempty
    assert_compiles '[5]', %q{
      def a = 5
      def test = [a]
      test
    }
  end

  def test_new_array_order
    assert_compiles '[3, 2, 1]', %q{
      def a = 3
      def b = 2
      def c = 1
      def test = [a, b, c]
      test
    }
  end

  def test_array_dup
    assert_compiles '[1, 2, 3]', %q{
      def test = [1,2,3]
      test
    }
  end

  def test_array_fixnum_aref
    assert_compiles '3', %q{
      def test(x) = [1,2,3][x]
      test(2)
      test(2)
    }, call_threshold: 2, insns: [:opt_aref]
  end

  def test_empty_array_pop
    assert_compiles 'nil', %q{
      def test(arr) = arr.pop
      test([])
      test([])
    }, call_threshold: 2
  end

  def test_array_pop_no_arg
    assert_compiles '42', %q{
      def test(arr) = arr.pop
      test([32, 33, 42])
      test([32, 33, 42])
    }, call_threshold: 2
  end

  def test_array_pop_arg
    assert_compiles '[33, 42]', %q{
      def test(arr) = arr.pop(2)
      test([32, 33, 42])
      test([32, 33, 42])
    }, call_threshold: 2
  end

  def test_new_range_inclusive
    assert_compiles '1..5', %q{
      def test(a, b) = a..b
      test(1, 5)
    }
  end

  def test_new_range_exclusive
    assert_compiles '1...5', %q{
      def test(a, b) = a...b
      test(1, 5)
    }
  end

  def test_new_range_with_literal
    assert_compiles '3..10', %q{
      def test(n) = n..10
      test(3)
    }
  end

  def test_new_range_fixnum_both_literals_inclusive
    assert_compiles '1..2', %q{
      def test()
        a = 2
        (1..a)
      end
      test; test
    }, call_threshold: 2, insns: [:newrange]
  end

  def test_new_range_fixnum_both_literals_exclusive
    assert_compiles '1...2', %q{
      def test()
        a = 2
        (1...a)
      end
      test; test
    }, call_threshold: 2, insns: [:newrange]
  end

  def test_new_range_fixnum_low_literal_inclusive
    assert_compiles '1..3', %q{
      def test(a)
        (1..a)
      end
      test(2); test(3)
    }, call_threshold: 2, insns: [:newrange]
  end

  def test_new_range_fixnum_low_literal_exclusive
    assert_compiles '1...3', %q{
      def test(a)
        (1...a)
      end
      test(2); test(3)
    }, call_threshold: 2, insns: [:newrange]
  end

  def test_new_range_fixnum_high_literal_inclusive
    assert_compiles '3..10', %q{
      def test(a)
        (a..10)
      end
      test(2); test(3)
    }, call_threshold: 2, insns: [:newrange]
  end

  def test_new_range_fixnum_high_literal_exclusive
    assert_compiles '3...10', %q{
      def test(a)
        (a...10)
      end
      test(2); test(3)
    }, call_threshold: 2, insns: [:newrange]
  end

  def test_if
    assert_compiles '[0, nil]', %q{
      def test(n)
        if n < 5
          0
        end
      end
      [test(3), test(7)]
    }
  end

  def test_if_else
    assert_compiles '[0, 1]', %q{
      def test(n)
        if n < 5
          0
        else
          1
        end
      end
      [test(3), test(7)]
    }
  end

  def test_if_else_params
    assert_compiles '[1, 20]', %q{
      def test(n, a, b)
        if n < 5
          a
        else
          b
        end
      end
      [test(3, 1, 2), test(7, 10, 20)]
    }
  end

  def test_if_else_nested
    assert_compiles '[3, 8, 9, 14]', %q{
      def test(a, b, c, d, e)
        if 2 < a
          if a < 4
            b
          else
            c
          end
        else
          if a < 0
            d
          else
            e
          end
        end
      end
      [
        test(-1,  1,  2,  3,  4),
        test( 0,  5,  6,  7,  8),
        test( 3,  9, 10, 11, 12),
        test( 5, 13, 14, 15, 16),
      ]
    }
  end

  def test_if_else_chained
    assert_compiles '[12, 11, 21]', %q{
      def test(a)
        (if 2 < a then 1 else 2 end) + (if a < 4 then 10 else 20 end)
      end
      [test(0), test(3), test(5)]
    }
  end

  def test_if_elsif_else
    assert_compiles '[0, 2, 1]', %q{
      def test(n)
        if n < 5
          0
        elsif 8 < n
          1
        else
          2
        end
      end
      [test(3), test(7), test(9)]
    }
  end

  def test_ternary_operator
    assert_compiles '[1, 20]', %q{
      def test(n, a, b)
        n < 5 ? a : b
      end
      [test(3, 1, 2), test(7, 10, 20)]
    }
  end

  def test_ternary_operator_nested
    assert_compiles '[2, 21]', %q{
      def test(n, a, b)
        (n < 5 ? a : b) + 1
      end
      [test(3, 1, 2), test(7, 10, 20)]
    }
  end

  def test_while_loop
    assert_compiles '10', %q{
      def test(n)
        i = 0
        while i < n
          i = i + 1
        end
        i
      end
      test(10)
    }
  end

  def test_while_loop_chain
    assert_compiles '[135, 270]', %q{
      def test(n)
        i = 0
        while i < n
          i = i + 1
        end
        while i < n * 10
          i = i * 3
        end
        i
      end
      [test(5), test(10)]
    }
  end

  def test_while_loop_nested
    assert_compiles '[0, 4, 12]', %q{
      def test(n, m)
        i = 0
        while i < n
          j = 0
          while j < m
            j += 2
          end
          i += j
        end
        i
      end
      [test(0, 0), test(1, 3), test(10, 5)]
    }
  end

  def test_while_loop_if_else
    assert_compiles '[9, -1]', %q{
      def test(n)
        i = 0
        while i < n
          if n >= 10
            return -1
          else
            i = i + 1
          end
        end
        i
      end
      [test(9), test(10)]
    }
  end

  def test_if_while_loop
    assert_compiles '[9, 12]', %q{
      def test(n)
        i = 0
        if n < 10
          while i < n
            i += 1
          end
        else
          while i < n
            i += 3
          end
        end
        i
      end
      [test(9), test(10)]
    }
  end

  def test_live_reg_past_ccall
    assert_compiles '2', %q{
      def callee = 1
      def test = callee + callee
      test
    }
  end

  def test_method_call
    assert_compiles '12', %q{
      def callee(a, b)
        a - b
      end

      def test
        callee(4, 2) + 10
      end

      test # profile test
      test
    }, call_threshold: 2
  end

  def test_recursive_fact
    assert_compiles '[1, 6, 720]', %q{
      def fact(n)
        if n == 0
          return 1
        end
        return n * fact(n-1)
      end
      [fact(0), fact(3), fact(6)]
    }
  end

  def test_profiled_fact
    assert_compiles '[1, 6, 720]', %q{
      def fact(n)
        if n == 0
          return 1
        end
        return n * fact(n-1)
      end
      fact(1) # profile fact
      [fact(0), fact(3), fact(6)]
    }, call_threshold: 3, num_profiles: 2
  end

  def test_recursive_fib
    assert_compiles '[0, 2, 3]', %q{
      def fib(n)
        if n < 2
          return n
        end
        return fib(n-1) + fib(n-2)
      end
      [fib(0), fib(3), fib(4)]
    }
  end

  def test_profiled_fib
    assert_compiles '[0, 2, 3]', %q{
      def fib(n)
        if n < 2
          return n
        end
        return fib(n-1) + fib(n-2)
      end
      fib(3) # profile fib
      [fib(0), fib(3), fib(4)]
    }, call_threshold: 5, num_profiles: 3
  end

  def test_spilled_basic_block_args
    assert_compiles '55', %q{
      def test(n1, n2)
        n3 = 3
        n4 = 4
        n5 = 5
        n6 = 6
        n7 = 7
        n8 = 8
        n9 = 9
        n10 = 10
        if n1 < n2
          n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10
        end
      end
      test(1, 2)
    }
  end

  def test_spilled_method_args
    assert_runs '55', %q{
      def foo(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10)
        n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10
      end

      def test
        foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      end

      test
    }

    # TODO(Shopify/ruby#716): Support spills and change to assert_compiles
    assert_runs '1', %q{
      def a(n1,n2,n3,n4,n5,n6,n7,n8,n9) = n1+n9
      a(2,0,0,0,0,0,0,0,-1)
    }

    # TODO(Shopify/ruby#716): Support spills and change to assert_compiles
    assert_runs '0', %q{
      def a(n1,n2,n3,n4,n5,n6,n7,n8) = n8
      a(1,1,1,1,1,1,1,0)
    }

    # TODO(Shopify/ruby#716): Support spills and change to assert_compiles
    # self param with spilled param
    assert_runs '"main"', %q{
      def a(n1,n2,n3,n4,n5,n6,n7,n8) = self
      a(1,0,0,0,0,0,0,0).to_s
    }
  end

  def test_spilled_param_new_arary
    # TODO(Shopify/ruby#716): Support spills and change to assert_compiles
    assert_runs '[:ok]', %q{
      def a(n1,n2,n3,n4,n5,n6,n7,n8) = [n8]
      a(0,0,0,0,0,0,0, :ok)
    }
  end

  def test_forty_param_method
    # This used to a trigger a miscomp on A64 due
    # to a memory displacement larger than 9 bits.
    # Using assert_runs again due to register spill.
    # TODO: It should be fixed by register spill support.
    assert_runs '1', %Q{
      def foo(#{'_,' * 39} n40) = n40

      foo(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1)
    }
  end

  def test_putself
    assert_compiles '3', %q{
      class Integer
        def minus(a)
          self - a
        end
      end
      5.minus(2)
    }
  end

  def test_getinstancevariable
    assert_compiles 'nil', %q{
      def test() = @foo

      test()
    }
    assert_compiles '3', %q{
      @foo = 3
      def test() = @foo

      test()
    }
  end

  def test_getinstancevariable_miss
    assert_compiles '[1, 1, 4]', %q{
      class C
        def foo
          @foo
        end

        def foo_then_bar
          @foo = 1
          @bar = 2
        end

        def bar_then_foo
          @bar = 3
          @foo = 4
        end
      end

      o1 = C.new
      o1.foo_then_bar
      result = []
      result << o1.foo
      result << o1.foo
      o2 = C.new
      o2.bar_then_foo
      result << o2.foo
      result
    }
  end

  def test_setinstancevariable
    assert_compiles '1', %q{
      def test() = @foo = 1

      test()
      @foo
    }
  end

  def test_getclassvariable
    assert_compiles '42', %q{
      class Foo
        def self.test = @@x
      end

      Foo.class_variable_set(:@@x, 42)
      Foo.test()
    }
  end

  def test_getclassvariable_raises
    assert_compiles '"uninitialized class variable @@x in Foo"', %q{
      class Foo
        def self.test = @@x
      end

      begin
        Foo.test
      rescue NameError => e
        e.message
      end
    }
  end

  def test_setclassvariable
    assert_compiles '42', %q{
      class Foo
        def self.test = @@x = 42
      end

      Foo.test()
      Foo.class_variable_get(:@@x)
    }
  end

  def test_setclassvariable_raises
    assert_compiles '"can\'t modify frozen Class: Foo"', %q{
      class Foo
        def self.test = @@x = 42
        freeze
      end

      begin
        Foo.test
      rescue FrozenError => e
        e.message
      end
    }
  end

  def test_attr_reader
    assert_compiles '[4, 4]', %q{
      class C
        attr_reader :foo

        def initialize
          @foo = 4
        end
      end

      def test(c) = c.foo
      c = C.new
      [test(c), test(c)]
    }, call_threshold: 2, insns: [:opt_send_without_block]
  end

  def test_attr_accessor_getivar
    assert_compiles '[4, 4]', %q{
      class C
        attr_accessor :foo

        def initialize
          @foo = 4
        end
      end

      def test(c) = c.foo
      c = C.new
      [test(c), test(c)]
    }, call_threshold: 2, insns: [:opt_send_without_block]
  end

  def test_attr_accessor_setivar
    assert_compiles '[5, 5]', %q{
      class C
        attr_accessor :foo

        def initialize
          @foo = 4
        end
      end

      def test(c)
        c.foo = 5
        c.foo
      end

      c = C.new
      [test(c), test(c)]
    }, call_threshold: 2, insns: [:opt_send_without_block]
  end

  def test_attr_writer
    assert_compiles '[5, 5]', %q{
      class C
        attr_writer :foo

        def initialize
          @foo = 4
        end

        def get_foo = @foo
      end

      def test(c)
        c.foo = 5
        c.get_foo
      end
      c = C.new
      [test(c), test(c)]
    }, call_threshold: 2, insns: [:opt_send_without_block]
  end

  def test_uncached_getconstant_path
    assert_compiles RUBY_COPYRIGHT.dump, %q{
      def test = RUBY_COPYRIGHT
      test
    }, call_threshold: 1, insns: [:opt_getconstant_path]
  end

  def test_expandarray_no_splat
    assert_compiles '[3, 4]', %q{
      def test(o)
        a, b = o
        [a, b]
      end
      test [3, 4]
    }, call_threshold: 1, insns: [:expandarray]
  end

  def test_expandarray_splat
    assert_compiles '[3, [4]]', %q{
      def test(o)
        a, *b = o
        [a, b]
      end
      test [3, 4]
    }, call_threshold: 1, insns: [:expandarray]
  end

  def test_expandarray_splat_post
    assert_compiles '[3, [4], 5]', %q{
      def test(o)
        a, *b, c = o
        [a, b, c]
      end
      test [3, 4, 5]
    }, call_threshold: 1, insns: [:expandarray]
  end

  def test_getconstant_path_autoload
    # A constant-referencing expression can run arbitrary code through Kernel#autoload.
    Dir.mktmpdir('autoload') do |tmpdir|
      autoload_path = File.join(tmpdir, 'test_getconstant_path_autoload.rb')
      File.write(autoload_path, 'X = RUBY_COPYRIGHT')

      assert_compiles RUBY_COPYRIGHT.dump, %Q{
        Object.autoload(:X, #{File.realpath(autoload_path).inspect})
        def test = X
        test
      }, call_threshold: 1, insns: [:opt_getconstant_path]
    end
  end

  def test_constant_invalidation
    assert_compiles '123', <<~RUBY, call_threshold: 2, insns: [:opt_getconstant_path]
      class C; end
      def test = C
      test
      test

      C = 123
      test
    RUBY
  end

  def test_constant_path_invalidation
    assert_compiles '["Foo::C", "Foo::C", "Bar::C"]', <<~RUBY, call_threshold: 2, insns: [:opt_getconstant_path]
      module A
        module B; end
      end

      module Foo
        C = "Foo::C"
      end

      module Bar
        C = "Bar::C"
      end

      A::B = Foo

      def test = A::B::C

      result = []

      result << test
      result << test

      A::B = Bar

      result << test
      result
    RUBY
  end

  def test_single_ractor_mode_invalidation
    # Without invalidating the single-ractor mode, the test would crash
    assert_compiles '"errored but not crashed"', <<~RUBY, call_threshold: 2, insns: [:opt_getconstant_path]
      C = Object.new

      def test
        C
      rescue Ractor::IsolationError
        "errored but not crashed"
      end

      test
      test

      Ractor.new {
        test
      }.value
    RUBY
  end

  def test_dupn
    assert_compiles '[[1], [1, 1], :rhs, [nil, :rhs]]', <<~RUBY, insns: [:dupn]
      def test(array) = (array[1, 2] ||= :rhs)

      one = [1, 1]
      start_empty = []
      [test(one), one, test(start_empty), start_empty]
    RUBY
  end

  def test_send_backtrace
    backtrace = [
      "-e:2:in 'Object#jit_frame1'",
      "-e:3:in 'Object#entry'",
      "-e:5:in 'block in <main>'",
      "-e:6:in '<main>'",
    ]
    assert_compiles backtrace.inspect, %q{
      def jit_frame2 = caller     # 1
      def jit_frame1 = jit_frame2 # 2
      def entry = jit_frame1      # 3
      entry # profile send        # 4
      entry                       # 5
    }, call_threshold: 2
  end

  def test_bop_invalidation
    assert_compiles '100', %q{
      def test
        eval(<<~RUBY)
          class Integer
            def +(_) = 100
          end
        RUBY
        1 + 2
      end
      test
    }
  end

  def test_defined_with_defined_values
    assert_compiles '["constant", "method", "global-variable"]', %q{
      class Foo; end
      def bar; end
      $ruby = 1

      def test = return defined?(Foo), defined?(bar), defined?($ruby)

      test
    }, insns: [:defined]
  end

  def test_defined_with_undefined_values
    assert_compiles '[nil, nil, nil]', %q{
      def test = return defined?(Foo), defined?(bar), defined?($ruby)

      test
    }, insns: [:defined]
  end

  def test_defined_with_method_call
    assert_compiles '["method", nil]', %q{
      def test = return defined?("x".reverse(1)), defined?("x".reverse(1).reverse)

      test
    }, insns: [:defined]
  end

  def test_defined_method_raise
    assert_compiles '[nil, nil, nil]', %q{
      class C
        def assert_equal expected, actual
          if expected != actual
            raise "NO"
          end
        end

        def test_defined_method
          assert_equal(nil, defined?("x".reverse(1).reverse))
        end
      end

      c = C.new
      result = []
      result << c.test_defined_method
      result << c.test_defined_method
      result << c.test_defined_method
      result
    }
  end

  def test_defined_yield
    assert_compiles "nil", "defined?(yield)"
    assert_compiles '[nil, nil, "yield"]', %q{
      def test = defined?(yield)
      [test, test, test{}]
    }, call_threshold: 2, insns: [:defined]
  end

  def test_defined_yield_from_block
    # This will do some EP hopping to find the local EP,
    # so it's slightly different than doing it outside of a block.

    assert_compiles '[nil, nil, "yield"]', %q{
      def test
        yield_self { yield_self { defined?(yield) } }
      end

      [test, test, test{}]
    }, call_threshold: 2
  end

  def test_block_given_p
    assert_compiles "false", "block_given?"
    assert_compiles '[false, false, true]', %q{
      def test = block_given?
      [test, test, test{}]
    }, call_threshold: 2, insns: [:opt_send_without_block]
  end

  def test_block_given_p_from_block
    # This will do some EP hopping to find the local EP,
    # so it's slightly different than doing it outside of a block.

    assert_compiles '[false, false, true]', %q{
      def test
        yield_self { yield_self { block_given? } }
      end

      [test, test, test{}]
    }, call_threshold: 2
  end

  def test_invokeblock_without_block_after_jit_call
    assert_compiles '"no block given (yield)"', %q{
      def test(*arr, &b)
        arr.class
        yield
      end
      begin
        test
      rescue => e
        e.message
      end
    }
  end

  def test_putspecialobject_vm_core_and_cbase
    assert_compiles '10', %q{
      def test
        alias bar test
        10
      end

      test
      bar
    }, insns: [:putspecialobject]
  end

  def test_putspecialobject_const_base
    assert_compiles '1', %q{
      Foo = 1

      def test = Foo

      # First call: populates the constant cache
      test
      # Second call: triggers ZJIT compilation with warm cache
      # RubyVM::ZJIT.assert_compiles will panic if this fails to compile
      test
    }, call_threshold: 2
  end

  def test_branchnil
    assert_compiles '[2, nil]', %q{
      def test(x)
        x&.succ
      end
      [test(1), test(nil)]
    }, call_threshold: 1, insns: [:branchnil]
  end

  def test_nil_nil
    assert_compiles 'true', %q{
      def test = nil.nil?
      test
    }, insns: [:opt_nil_p]
  end

  def test_non_nil_nil
    assert_compiles 'false', %q{
      def test = 1.nil?
      test
    }, insns: [:opt_nil_p]
  end

  def test_getspecial_last_match
    assert_compiles '"hello"', %q{
      def test(str)
        str =~ /hello/
        $&
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_match_pre
    assert_compiles '"hello "', %q{
      def test(str)
        str =~ /world/
        $`
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_match_post
    assert_compiles '" world"', %q{
      def test(str)
        str =~ /hello/
        $'
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_match_last_group
    assert_compiles '"world"', %q{
      def test(str)
        str =~ /(hello) (world)/
        $+
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_numbered_match_1
    assert_compiles '"hello"', %q{
      def test(str)
        str =~ /(hello) (world)/
        $1
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_numbered_match_2
    assert_compiles '"world"', %q{
      def test(str)
        str =~ /(hello) (world)/
        $2
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_numbered_match_nonexistent
    assert_compiles 'nil', %q{
      def test(str)
        str =~ /(hello)/
        $2
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_no_match
    assert_compiles 'nil', %q{
      def test(str)
        str =~ /xyz/
        $&
      end
      test("hello world")
    }, insns: [:getspecial]
  end

  def test_getspecial_complex_pattern
    assert_compiles '"123"', %q{
      def test(str)
        str =~ /(\d+)/
        $1
      end
      test("abc123def")
    }, insns: [:getspecial]
  end

  def test_getspecial_multiple_groups
    assert_compiles '"456"', %q{
      def test(str)
        str =~ /(\d+)-(\d+)/
        $2
      end
      test("123-456")
    }, insns: [:getspecial]
  end

  # tool/ruby_vm/views/*.erb relies on the zjit instructions a) being contiguous and
  # b) being reliably ordered after all the other instructions.
  def test_instruction_order
    insn_names = RubyVM::INSTRUCTION_NAMES
    zjit, others = insn_names.map.with_index.partition { |name, _| name.start_with?('zjit_') }
    zjit_indexes = zjit.map(&:last)
    other_indexes = others.map(&:last)
    zjit_indexes.product(other_indexes).each do |zjit_index, other_index|
      assert zjit_index > other_index, "'#{insn_names[zjit_index]}' at #{zjit_index} "\
        "must be defined after '#{insn_names[other_index]}' at #{other_index}"
    end
  end

  def test_require_rubygems
    assert_runs 'true', %q{
      require 'rubygems'
    }, call_threshold: 2
  end

  def test_require_rubygems_with_auto_compact
    omit("GC.auto_compact= support is required for this test") unless GC.respond_to?(:auto_compact=)
    assert_runs 'true', %q{
      GC.auto_compact = true
      require 'rubygems'
    }, call_threshold: 2
  end

  def test_stats_availability
    assert_runs '[true, true]', %q{
      def test = 1
      test
      [
        RubyVM::ZJIT.stats[:zjit_insn_count] > 0,
        RubyVM::ZJIT.stats(:zjit_insn_count) > 0,
      ]
    }, stats: true
  end

  def test_stats_consistency
    assert_runs '[]', %q{
      def test = 1
      test # increment some counters

      RubyVM::ZJIT.stats.to_a.filter_map do |key, value|
        # The value may be incremented, but the class should stay the same
        other_value = RubyVM::ZJIT.stats(key)
        if value.class != other_value.class
          [key, value, other_value]
        end
      end
    }, stats: true
  end

  def test_reset_stats
    assert_runs 'true', %q{
      def test = 1
      100.times { test }

      # Get initial stats and verify they're non-zero
      initial_stats = RubyVM::ZJIT.stats

      # Reset the stats
      RubyVM::ZJIT.reset_stats!

      # Get stats after reset
      reset_stats = RubyVM::ZJIT.stats

      [
        # After reset, counters should be zero or at least much smaller
        # (some instructions might execute between reset and reading stats)
        :zjit_insn_count.then { |s| initial_stats[s] > 0 && reset_stats[s] < initial_stats[s] },
        :compiled_iseq_count.then { |s| initial_stats[s] > 0 && reset_stats[s] < initial_stats[s] }
      ].all?
    }, stats: true
  end

  def test_zjit_option_uses_array_each_in_ruby
    omit 'ZJIT wrongly compiles Array#each, so it is disabled for now'
    assert_runs '"<internal:array>"', %q{
      Array.instance_method(:each).source_location&.first
    }
  end

  def test_profile_under_nested_jit_call
    assert_compiles '[nil, nil, 3]', %q{
      def profile
        1 + 2
      end

      def jit_call(flag)
        if flag
          profile
        end
      end

      def entry(flag)
        jit_call(flag)
      end

      [entry(false), entry(false), entry(true)]
    }, call_threshold: 2
  end

  def test_bop_redefined
    assert_runs '[3, :+, 100]', %q{
      def test
        1 + 2
      end

      test # profile opt_plus
      [test, Integer.class_eval { def +(_) = 100 }, test]
    }, call_threshold: 2
  end

  def test_bop_redefined_with_adjacent_patch_points
    assert_runs '[15, :+, 100]', %q{
      def test
        1 + 2 + 3 + 4 + 5
      end

      test # profile opt_plus
      [test, Integer.class_eval { def +(_) = 100 }, test]
    }, call_threshold: 2
  end

  # ZJIT currently only generates a MethodRedefined patch point when the method
  # is called on the top-level self.
  def test_method_redefined_with_top_self
    assert_runs '["original", "redefined"]', %q{
      def foo
        "original"
      end

      def test = foo

      test; test

      result1 = test

      # Redefine the method
      def foo
        "redefined"
      end

      result2 = test

      [result1, result2]
    }, call_threshold: 2
  end

  def test_method_redefined_with_module
    assert_runs '["original", "redefined"]', %q{
      module Foo
        def self.foo = "original"
      end

      def test = Foo.foo
      test
      result1 = test

      def Foo.foo = "redefined"
      result2 = test

      [result1, result2]
    }, call_threshold: 2
  end

  def test_module_name_with_guard_passes
    assert_compiles '"Integer"', %q{
      def test(mod)
        mod.name
      end

      test(String)
      test(Integer)
    }, call_threshold: 2
  end

  def test_module_name_with_guard_side_exit
    # This test demonstrates that the guard side exit works correctly
    # In this case, when we call with a non-Class object, it should fall back to interpreter
    assert_compiles '["String", "Integer", "Bar"]', %q{
      class MyClass
        def name = "Bar"
      end

      def test(mod)
        mod.name
      end

      results = []
      results << test(String)
      results << test(Integer)
      results << test(MyClass.new)

      results
    }, call_threshold: 2
  end

  def test_objtostring_calls_to_s_on_non_strings
    assert_compiles '["foo", "foo"]', %q{
      results = []

      class Foo
        def to_s
          "foo"
        end
      end

      def test(str)
        "#{str}"
      end

      results << test(Foo.new)
      results << test(Foo.new)

      results
    }
  end

  def test_objtostring_rewrite_does_not_call_to_s_on_strings
    assert_compiles '["foo", "foo"]', %q{
      results = []

      class String
        def to_s
          "bad"
        end
      end

      def test(foo)
        "#{foo}"
      end

      results << test("foo")
      results << test("foo")

      results
    }
  end

  def test_objtostring_rewrite_does_not_call_to_s_on_string_subclasses
    assert_compiles '["foo", "foo"]', %q{
      results = []

      class StringSubclass < String
        def to_s
          "bad"
        end
      end

      foo = StringSubclass.new("foo")

      def test(str)
        "#{str}"
      end

      results << test(foo)
      results << test(foo)

      results
    }
  end

  def test_objtostring_profiled_string_fastpath
    assert_compiles '"foo"', %q{
      def test(str)
        "#{str}"
      end
      test('foo'); test('foo') # profile as string
    }, call_threshold: 2
  end

  def test_objtostring_profiled_string_subclass_fastpath
    assert_compiles '"foo"', %q{
      class MyString < String; end

      def test(str)
        "#{str}"
      end

      foo = MyString.new("foo")
      test(foo); test(foo) # still profiles as string
    }, call_threshold: 2
  end

  def test_objtostring_profiled_string_fastpath_exits_on_nonstring
    assert_compiles '"1"', %q{
      def test(str)
        "#{str}"
      end

      test('foo') # profile as string
      test(1)
    }, call_threshold: 2
  end

  def test_objtostring_profiled_nonstring_calls_to_s
    assert_compiles '"[1, 2, 3]"', %q{
      def test(str)
        "#{str}"
      end

      test([1,2,3]); # profile as nonstring
      test([1,2,3]);
    }, call_threshold: 2
  end

  def test_objtostring_profiled_nonstring_guard_exits_when_string
    assert_compiles '"foo"', %q{
      def test(str)
        "#{str}"
      end

      test([1,2,3]); # profiles as nonstring
      test('foo');
    }, call_threshold: 2
  end

  def test_string_bytesize_with_guard
    assert_compiles '5', %q{
      def test(str)
        str.bytesize
      end

      test('hello')
      test('world')
    }, call_threshold: 2
  end

  def test_string_bytesize_multibyte
    assert_compiles '4', %q{
      def test(s)
        s.bytesize
      end

      test("💎")
    }, call_threshold: 2
  end

  def test_nil_value_nil_opt_with_guard
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(nil)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_nil_value_nil_opt_with_guard_side_exit
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(nil)
      test(nil)
      test(1)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_true_nil_opt_with_guard
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(true)
      test(true)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_true_nil_opt_with_guard_side_exit
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(true)
      test(true)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_false_nil_opt_with_guard
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(false)
      test(false)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_false_nil_opt_with_guard_side_exit
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(false)
      test(false)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_integer_nil_opt_with_guard
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(1)
      test(2)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_integer_nil_opt_with_guard_side_exit
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(1)
      test(2)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_float_nil_opt_with_guard
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(1.0)
      test(2.0)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_float_nil_opt_with_guard_side_exit
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(1.0)
      test(2.0)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_symbol_nil_opt_with_guard
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(:foo)
      test(:bar)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_symbol_nil_opt_with_guard_side_exit
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(:foo)
      test(:bar)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_class_nil_opt_with_guard
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(String)
      test(Integer)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_class_nil_opt_with_guard_side_exit
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(String)
      test(Integer)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_module_nil_opt_with_guard
    assert_compiles 'false', %q{
      def test(val) = val.nil?

      test(Enumerable)
      test(Kernel)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_module_nil_opt_with_guard_side_exit
    assert_compiles 'true', %q{
      def test(val) = val.nil?

      test(Enumerable)
      test(Kernel)
      test(nil)
    }, call_threshold: 2, insns: [:opt_nil_p]
  end

  def test_basic_object_guard_works_with_immediate
    assert_compiles 'NilClass', %q{
      class Foo; end

      def test(val) = val.class

      test(Foo.new)
      test(Foo.new)
      test(nil)
    }, call_threshold: 2
  end

  def test_basic_object_guard_works_with_false
    assert_compiles 'FalseClass', %q{
      class Foo; end

      def test(val) = val.class

      test(Foo.new)
      test(Foo.new)
      test(false)
    }, call_threshold: 2
  end

  def test_string_concat
    assert_compiles '"123"', %q{
      def test = "#{1}#{2}#{3}"

      test
    }, insns: [:concatstrings]
  end

  def test_string_concat_empty
    assert_compiles '""', %q{
      def test = "#{}"

      test
    }, insns: [:concatstrings]
  end

  def test_regexp_interpolation
    assert_compiles '/123/', %q{
      def test = /#{1}#{2}#{3}/

      test
    }, insns: [:toregexp]
  end

  def test_new_range_non_leaf
    assert_compiles '(0/1)..1', %q{
      def jit_entry(v) = make_range_then_exit(v)

      def make_range_then_exit(v)
        range = (v..1)
        super rescue range # TODO(alan): replace super with side-exit intrinsic
      end

      jit_entry(0)    # profile
      jit_entry(0)    # compile
      jit_entry(0/1r) # run without stub
    }, call_threshold: 2
  end

  def test_raise_in_second_argument
    assert_compiles '{ok: true}', %q{
      def write(hash, key)
        hash[key] = raise rescue true
        hash
      end

      write({}, :ok)
    }
  end

  def test_ivar_attr_reader_optimization_with_multi_ractor_mode
    assert_compiles '42', %q{
      class Foo
        class << self
          attr_accessor :bar

          def get_bar
            bar
          rescue Ractor::IsolationError
            42
          end
        end
      end

      Foo.bar = [] # needs to be a ractor unshareable object

      def test
        Foo.get_bar
      end

      test
      test

      Ractor.new { test }.value
    }, call_threshold: 2
  end

  def test_ivar_get_with_multi_ractor_mode
    assert_compiles '42', %q{
      class Foo
        def self.set_bar
          @bar = [] # needs to be a ractor unshareable object
        end

        def self.bar
          @bar
        rescue Ractor::IsolationError
          42
        end
      end

      Foo.set_bar

      def test
        Foo.bar
      end

      test
      test

      Ractor.new { test }.value
    }, call_threshold: 2
  end

  def test_ivar_get_with_already_multi_ractor_mode
    assert_compiles '42', %q{
      class Foo
        def self.set_bar
          @bar = [] # needs to be a ractor unshareable object
        end

        def self.bar
          @bar
        rescue Ractor::IsolationError
          42
        end
      end

      Foo.set_bar
      r = Ractor.new {
        Ractor.receive
        Foo.bar
      }

      Foo.bar
      Foo.bar

      r << :go
      r.value
    }, call_threshold: 2
  end

  def test_ivar_set_with_multi_ractor_mode
    assert_compiles '42', %q{
      class Foo
        def self.bar
          _foo = 1
          _bar = 2
          begin
            @bar = _foo + _bar
          rescue Ractor::IsolationError
            42
          end
        end
      end

      def test
        Foo.bar
      end

      test
      test

      Ractor.new { test }.value
    }
  end

  def test_struct_set
    assert_compiles '[42, 42, :frozen_error]', %q{
      C = Struct.new(:foo).new(1)

      def test
        C.foo = Object.new
        42
      end

      r = [test, test]
      C.freeze
      r << begin
        test
      rescue FrozenError
        :frozen_error
      end
    }, call_threshold: 2
  end

  def test_global_tracepoint
    assert_compiles 'true', %q{
      def foo = 1

      foo
      foo

      called = false

      tp = TracePoint.new(:return) { |event|
        if event.method_id == :foo
          called = true
        end
      }
      tp.enable do
        foo
      end
      called
    }
  end

  def test_local_tracepoint
    assert_compiles 'true', %q{
      def foo = 1

      foo
      foo

      called = false

      tp = TracePoint.new(:return) { |_| called = true }
      tp.enable(target: method(:foo)) do
        foo
      end
      called
    }
  end

  def test_line_tracepoint_on_c_method
    assert_compiles '"[[:line, true]]"', %q{
      events = []
      events.instance_variable_set(
        :@tp,
        TracePoint.new(:line) { |tp| events << [tp.event, tp.lineno] if tp.path == __FILE__ }
      )
      def events.to_str
        @tp.enable; ''
      end

      # Stay in generated code while enabling tracing
      def events.compiled(obj)
        String(obj)
        @tp.disable; __LINE__
      end

      line = events.compiled(events)
      events[0][-1] = (events[0][-1] == line)

      events.to_s # can't dump events as it's a singleton object AND it has a TracePoint instance variable, which also can't be dumped
    }
  end

  def test_targeted_line_tracepoint_in_c_method_call
    assert_compiles '"[true]"', %q{
      events = []
      events.instance_variable_set(:@tp, TracePoint.new(:line) { |tp| events << tp.lineno })
      def events.to_str
        @tp.enable(target: method(:compiled))
        ''
      end

      # Stay in generated code while enabling tracing
      def events.compiled(obj)
        String(obj)
        __LINE__
      end

      line = events.compiled(events)
      events[0] = (events[0] == line)

      events.to_s # can't dump events as it's a singleton object AND it has a TracePoint instance variable, which also can't be dumped
    }
  end

  def test_opt_case_dispatch
    assert_compiles '[true, false]', %q{
      def test(x)
        case x
        when :foo
          true
        else
          false
        end
      end

      results = []
      results << test(:foo)
      results << test(1)
      results
    }, insns: [:opt_case_dispatch]
  end

  def test_stack_overflow
    assert_compiles 'nil', %q{
      def recurse(n)
        return if n == 0
        recurse(n-1)
        nil # no tail call
      end

      recurse(2)
      recurse(2)
      begin
        recurse(20_000)
      rescue SystemStackError
        # Not asserting an exception is raised here since main
        # thread stack size is environment-sensitive. Only
        # that we don't crash or infinite loop.
      end
    }, call_threshold: 2
  end

  def test_invokeblock
    assert_compiles '42', %q{
      def test
        yield
      end
      test { 42 }
    }, insns: [:invokeblock]
  end

  def test_invokeblock_with_args
    assert_compiles '3', %q{
      def test(x, y)
        yield x, y
      end
      test(1, 2) { |a, b| a + b }
    }, insns: [:invokeblock]
  end

  def test_invokeblock_no_block_given
    assert_compiles ':error', %q{
      def test
        yield rescue :error
      end
      test
    }, insns: [:invokeblock]
  end

  def test_invokeblock_multiple_yields
    assert_compiles "[1, 2, 3]", %q{
      results = []
      def test
        yield 1
        yield 2
        yield 3
      end
      test { |x| results << x }
      results
    }, insns: [:invokeblock]
  end

  def test_ccall_variadic_with_multiple_args
    assert_compiles "[1, 2, 3]", %q{
      def test
        a = []
        a.push(1, 2, 3)
        a
      end

      test
      test
    }, insns: [:opt_send_without_block]
  end

  def test_ccall_variadic_with_no_args
    assert_compiles "[1]", %q{
      def test
        a = [1]
        a.push
      end

      test
      test
    }, insns: [:opt_send_without_block]
  end

  def test_ccall_variadic_with_no_args_causing_argument_error
    assert_compiles ":error", %q{
      def test
        format
      rescue ArgumentError
        :error
      end

      test
      test
    }, insns: [:opt_send_without_block]
  end

  def test_allocating_in_hir_c_method_is
    assert_compiles ":k", %q{
      # Put opt_new in a frame JIT code sets up that doesn't set cfp->pc
      def a(f) = test(f)
      def test(f) = (f.new if f)
      # A parallel couple methods that will set PC at the same stack height
      def second = third
      def third = nil

      a(nil)
      a(nil)

      class Foo
        def self.new = :k
      end

      second

      a(Foo)
    }, call_threshold: 2, insns: [:opt_new]
  end

  def test_singleton_class_invalidation_annotated_ccall
    assert_compiles '[false, true]', %q{
      def define_singleton(obj, define)
        if define
          # Wrap in C method frame to avoid exiting JIT on defineclass
          [nil].reverse_each do
            class << obj
              def ==(_)
                true
              end
            end
          end
        end
        false
      end

      def test(define)
        obj = BasicObject.new
        # This == call gets compiled to a CCall
        obj == define_singleton(obj, define)
      end

      result = []
      result << test(false)  # Compiles BasicObject#==
      result << test(true)   # Should use singleton#== now
      result
    }, call_threshold: 2
  end

  def test_singleton_class_invalidation_optimized_variadic_ccall
    assert_compiles '[1, 1000]', %q{
      def define_singleton(arr, define)
        if define
          # Wrap in C method frame to avoid exiting JIT on defineclass
          [nil].reverse_each do
            class << arr
              def push(x)
                super(x * 1000)
              end
            end
          end
        end
        1
      end

      def test(define)
        arr = []
        val = define_singleton(arr, define)
        arr.push(val)  # This CCall should be invalidated if singleton was defined
        arr[0]
      end

      result = []
      result << test(false)  # Compiles Array#push as CCall
      result << test(true)   # Singleton defined, CCall should be invalidated
      result
    }, call_threshold: 2
  end

  def test_regression_cfp_sp_set_correctly_before_leaf_gc_call
    assert_compiles ':ok', %q{
      def check(l, r)
        return 1 unless l
        1 + check(*l) + check(*r)
      end

      def tree(depth)
        # This duparray is our leaf-gc target.
        return [nil, nil] unless depth > 0

        # Modify the local and pass it to the following calls.
        depth -= 1
        [tree(depth), tree(depth)]
      end

      def test
        GC.stress = true
        2.times do
          t = tree(11)
          check(*t)
        end
        :ok
      end

      test
    }, call_threshold: 14, num_profiles: 5
  end

  private

  # Assert that every method call in `test_script` can be compiled by ZJIT
  # at a given call_threshold
  def assert_compiles(expected, test_script, insns: [], **opts)
    assert_runs(expected, test_script, insns:, assert_compiles: true, **opts)
  end

  # Assert that `test_script` runs successfully with ZJIT enabled.
  # Unlike `assert_compiles`, `assert_runs(assert_compiles: false)`
  # allows ZJIT to skip compiling methods.
  def assert_runs(expected, test_script, insns: [], assert_compiles: false, **opts)
    pipe_fd = 3
    disasm_method = :test

    script = <<~RUBY
      ret_val = (_test_proc = -> { #{('RubyVM::ZJIT.assert_compiles; ' if assert_compiles)}#{test_script.lstrip} }).call
      result = {
        ret_val:,
        #{ unless insns.empty?
           "insns: RubyVM::InstructionSequence.of(method(#{disasm_method.inspect})).to_a"
        end}
      }
      IO.open(#{pipe_fd}).write(Marshal.dump(result))
    RUBY

    out, err, status, result = eval_with_jit(script, pipe_fd:, **opts)
    assert_success(out, err, status)

    result = Marshal.load(result)
    assert_equal(expected, result.fetch(:ret_val).inspect)

    unless insns.empty?
      iseq = result.fetch(:insns)
      assert_equal(
        "YARVInstructionSequence/SimpleDataFormat",
        iseq.first,
        "Failed to get ISEQ disassembly. " \
        "Make sure to put code directly under the '#{disasm_method}' method."
      )
      iseq_insns = iseq.last

      expected_insns = Set.new(insns)
      iseq_insns.each do
        next unless it.is_a?(Array)
        expected_insns.delete(it.first)
      end
      assert(expected_insns.empty?, -> { "Not present in ISeq: #{expected_insns.to_a}" })
    end
  end

  # Run a Ruby process with ZJIT options and a pipe for writing test results
  def eval_with_jit(
    script,
    call_threshold: 1,
    num_profiles: 1,
    zjit: true,
    stats: false,
    debug: true,
    allowed_iseqs: nil,
    timeout: 1000,
    pipe_fd: nil
  )
    args = ["--disable-gems"]
    if zjit
      args << "--zjit-call-threshold=#{call_threshold}"
      args << "--zjit-num-profiles=#{num_profiles}"
      case stats
      when true
        args << "--zjit-stats"
      when :quiet
        args << "--zjit-stats-quiet"
      else
        args << "--zjit-stats=#{stats}" if stats
      end
      args << "--zjit-debug" if debug
      if allowed_iseqs
        jitlist = Tempfile.new("jitlist")
        jitlist.write(allowed_iseqs)
        jitlist.close
        args << "--zjit-allowed-iseqs=#{jitlist.path}"
      end
    end
    args << "-e" << script_shell_encode(script)
    ios = {}
    if pipe_fd
      pipe_r, pipe_w = IO.pipe
      # Separate thread so we don't deadlock when
      # the child ruby blocks writing the output to pipe_fd
      pipe_out = nil
      pipe_reader = Thread.new do
        pipe_out = pipe_r.read
        pipe_r.close
      end
      ios[pipe_fd] = pipe_w
    end
    result = EnvUtil.invoke_ruby(args, '', true, true, rubybin: RbConfig.ruby, timeout: timeout, ios:)
    if pipe_fd
      pipe_w.close
      pipe_reader.join(timeout)
      result << pipe_out
    end
    result
  ensure
    pipe_reader&.kill
    pipe_reader&.join(timeout)
    pipe_r&.close
    pipe_w&.close
    jitlist&.unlink
  end

  def assert_success(out, err, status)
    message = "exited with status #{status.to_i}"
    message << "\nstdout:\n```\n#{out}```\n" unless out.empty?
    message << "\nstderr:\n```\n#{err}```\n" unless err.empty?
    assert status.success?, message
  end

  def script_shell_encode(s)
    # We can't pass utf-8-encoded characters directly in a shell arg. But we can use Ruby \u constants.
    s.chars.map { |c| c.ascii_only? ? c : "\\u%x" % c.codepoints[0] }.join
  end
end