hotpy_2 / Python / trace_recorder_register.c

   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
/* Trace Recorder */
/* This is the final stage of the pipeline.
 * It transforms the incoming stack-base code into register based code */

#include <stddef.h> /* For offsetof */
#include "Python.h"
#include "opcode.h"
#include "optimiser.h"
#include "trace_opcode.h"
#include "register_macros.h"

#ifdef Py_DEBUG
static char *instruction_formats[256];

int consistent_format(int op, char *fmt)
{
    assert(op >= 0 && op < 256);
    if (instruction_formats[op] == NULL) {
        instruction_formats[op] = fmt;
    }
    else {
        if (strcmp(instruction_formats[op], fmt)) {
            fprintf(stderr, "Mismatched formats for %d (%s): '%s' and '%s'",
                    op, _HotPy_Instruction_Names[op],
                    instruction_formats[op], fmt);
            return 0;
        }
    }
    return 1;
}

static int length_via_format_matches(int op, int expected)
{
    int bytes = 0;
    char *fmt =  instruction_formats[op];
    if (fmt == NULL)
        return 1;
    for(; *fmt; fmt++)
        bytes += *fmt < 'a' ? 2 : 1;
    return (1 + (bytes >> 2)) == expected;
}

#endif

typedef struct _trace_recorder {
    HotPyOptimiser opt_base;
    Py_ssize_t next_inst;
    Py_ssize_t allocated_insts;
    uint32_t *insts;
    Py_ssize_t loop_header;
    PyObject *exits;
    PyObject *name_ids;
    PyObject *name_list;
    PyObject *constant_ids;
    PyObject *constant_list;
    HotPyTraceObject *trace;
    int stack[64];
    int used_regs[HOTPY_STACK_REGISTERS];
    int stack_items;
    FILE *log_file;
    PyDictKeysObject *keys[256];
    int keys_count;
} HotPyTraceRecorder;

#define FAILED(opt) do { HotPyTrace_Invalidate((PyObject *)opt->trace); PyErr_Clear(); return -1; } while(0)

static void
resize_code(HotPyTraceRecorder *opt) {
    uint32_t *new_insts;
    assert(opt->next_inst == opt->allocated_insts);
    if (opt->trace == NULL)
        return;
    new_insts = PyMem_Malloc(opt->allocated_insts*2*sizeof(uint32_t));
    if (new_insts == NULL) {
        opt->next_inst = 0;
        Py_CLEAR(opt->trace);
        return;
    }
    memcpy(new_insts, opt->insts, opt->allocated_insts*sizeof(uint32_t));
    PyMem_Free(opt->insts);
    opt->insts = new_insts;
    opt->allocated_insts *= 2;
}

static void
overwrite_word_1111(uint32_t *ptr, int b0, int b1, int b2, int b3)
{
    uint32_t instruction;
    assert(b0 >= 0 && b0 < 256);
    assert(b1 >= 0 && b1 < 256);
    assert(b2 >= 0 && b2 < 256);
    assert(b3 >= 0 && b3 < 256);
    instruction = ((uint32_t)b0) | (((uint32_t)b1) << 8) |
                  (((uint32_t)b2) << 16) | (((uint32_t)b3) << 24);
    *ptr = instruction;
}

static void
write_word_1111(HotPyOptimiser *x, int b0, int b1, int b2, int b3)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    if (opt->next_inst >= opt->allocated_insts)
        resize_code(opt);
    overwrite_word_1111(&opt->insts[opt->next_inst], b0, b1, b2, b3);
    opt->next_inst++;
}

static void
write_word_112(HotPyOptimiser *x, int b0, int b1, int s2)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    uint32_t instruction;
    assert(b0 >= 0 && b0 < 256);
    assert(b1 >= 0 && b1 < 256);
    assert(s2 >= 0 && s2 < (1 << 16));
    if (opt->next_inst >= opt->allocated_insts)
        resize_code(opt);
    instruction = ((uint32_t)b0) | (((uint32_t)b1) << 8) |
                  (((uint32_t)s2) << 16);
    opt->insts[opt->next_inst++] = instruction;
}

static void
write_word_22(HotPyOptimiser *x, int s0, int s1)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    uint32_t instruction;
    assert(s0 >= 0 && s0 < (1 << 16));
    assert(s1 >= 0 && s1 < (1 << 16));
    if (opt->next_inst >= opt->allocated_insts)
        resize_code(opt);
    instruction = ((uint32_t)s0) | (((uint32_t)s1) << 16);
    opt->insts[opt->next_inst++] = instruction;
}

static void flush_stack(HotPyOptimiser *x)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int i;
    for (i = 0; i < opt->stack_items; i++) {
        int r = opt->stack[i];
        write_word_1111(x, PUSH_REGISTER_TO_STACK, r, 0, 0);
        assert(r >= 0 && r < HOTPY_STACK_REGISTERS);
        opt->used_regs[r] = 0;
    }
    opt->stack_items = 0;
}

static void
write_move(HotPyOptimiser *x, int from, int to)
{
    if (from != to)
        write_word_1111(x, MOVE, from, to, 0);
}

#ifdef Py_DEBUG
static int
consistent_stack(HotPyOptimiser *x)
{
    int i;
    int used = 0;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    for (i = 0; i < opt->stack_items; i++) {
        if (opt->used_regs[i] == 0)
            return 0;
    }
    for (i = 0; i < HOTPY_STACK_REGISTERS; i++)
        used += opt->used_regs[i];
    return used == opt->stack_items;
}

#define CONSISTENT(x) assert(consistent_stack(x))
#else
#define CONSISTENT(x) ((void)0)
#endif

static int choose_register(HotPyOptimiser *x)
{
    int i, chosen;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    for (i = 0; i < HOTPY_STACK_REGISTERS; i++) {
        if (opt->used_regs[i] == 0) {
            opt->used_regs[i] = 1;
            return i;
        }
    }
    assert(opt->stack_items > 0);
    chosen = opt->stack[0];
    write_word_1111(x, PUSH_REGISTER_TO_STACK, chosen, 0, 0);
    assert(opt->stack_items > 0);
    opt->stack_items--;
    for (i = 0; i < opt->stack_items; i++)
        opt->stack[i] = opt->stack[i+1];
    return chosen;
}

static int pop_as_register(HotPyOptimiser *x)
{
    int reg;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    if (opt->stack_items) {
        reg = opt->stack[--opt->stack_items];
        return reg;
    }
    reg = choose_register(x);
    write_word_1111(x, POP_STACK_TO_REGISTER, reg, 0, 0);
    return reg;
}

static void free_register(HotPyOptimiser *x, int reg)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    assert(reg >= 0 && reg < HOTPY_STACK_REGISTERS);
    opt->used_regs[reg] = 0;
}

static int push_as_register(HotPyOptimiser *x)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int reg = choose_register(x);
    assert(reg < HOTPY_STACK_REGISTERS);
    opt->stack[opt->stack_items++] = reg;
    return reg;
}

static int peek_as_register(HotPyOptimiser *x)
{
    int reg;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    if (opt->stack_items)
        return opt->stack[opt->stack_items-1];
    reg = choose_register(x);
    write_word_1111(x, POP_STACK_TO_REGISTER, reg, 0, 0);
    opt->stack[0] = reg;
    opt->stack_items = 1;
    return reg;
}

#include "register_write_functions.h"

/* This exists to eliminate compiler warnings about unused stuff */
static void
unused_functions(void)
{
    (void)write_o;
    (void)write_roo;
    (void)write_rroo;
    (void)defuses_for_roo;
    (void)defuses_for_rroo;
    (void)relabel_uses_roo;
    (void)relabel_uses_rroo;
    (void)relabel_defs_roo;
    (void)relabel_defs_rroo;
    (void)get_exit_context_roo;
    (void)get_exit_context_rroo;
}

static void
print_register_code(uint32_t *code, int count, FILE* out)
{
    unused_functions();
    fprintf(out, "--------------\n");
    uint32_t *limit = code + count;
    while (code < limit) {
        int op = *code & 255;
        switch (op) {
        case LEAVE_FRAME: case POP_CALLER_BLOCK: case CLEAR_FRAME: case NOP:
        case POP_BLOCK: case POP_EXCEPT: case SETUP_EXCEPT_HANDLER:
        case STOP_ITERATION_OR_RERAISE: case RERAISE:
        case SAVE_EXC_STATE: case SWAP_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
            fprintf(out, "%s\n", _HotPy_Instruction_Names[op]);
            code++;
            break;
        case DELETE_FAST: case DELETE_DEREF:
            print_K(out, &code);
            break;
        case SETUP_BLOCK:
            print_kkK(out, &code);
            break;
        case MAKE_FRAME: case NEW_FRAME: case NEW_CLOSURE: case KW_DICT_MERGE:
            print_rrro(out, &code);
            break;
        case GET_GLOBAL_AND_BUILTINS:
            print_oo(out, &code);
            break;
        case UNARY_NOT: case AS_TUPLE: case MAKE_GENERATOR: case GEN_STARTED:
        case HAS_DICT: case GET_TYPE: case OVERRIDES_GENERIC_GETATTRIBUTE:
        case OVERRIDES_GENERIC_SETATTR: case ALLOCATE: case SIZE: case MOVE:
            print_ro(out, &code);
            break;
        case STORE_MAP: case DESCRIPTOR_SET: case SET_IN_OBJECT_DICT:
            print_rrr(out, &code);
            break;
        case ENTER_FRAME: case PUSH_REGISTER_TO_STACK: case STORE_LOCALS:
        case GEN_ENTER: case GEN_EXIT:
            print_r(out, &code);
            break;
        case GEN_CLEAR:
            print_p(out, &code);
            break;
        case CALL_CFUNC_1: case CALL_CMETH_0:
        case CALL_WRAPPER_NEXT: case CALL_WRAPPER_UNARY:
            print_roKE(out, &code);
            break;
        case VALUE_FROM_OBJECT_DICT_OR_EXIT:
            print_rroE(out, &code);
            break;
        case ONE_ARG: case TRACE_DESCRIPTOR_GET:
            print_rroE(out, &code);
            break;
        case TRACE_DESCRIPTOR_SET:
            print_rrrE(out, &code);
            break;
        case CALL_CFUNC_0:
            print_oKE(out, &code);
            break;
        case CALL_CFUNC_2: case CALL_CMETH_1:
            print_rroKE(out, &code);
            break;
        case CALL_CMETH_2:
            print_rrrokE(out, &code);
            break;
        case CALL_WRAPPER_BINARY:
            print_rroKE(out, &code);
            break;
        case CALL_OBJECT:
            print_rrroE(out, &code);
            break;
        case CALL_WRAPPER_TRINARY_PROC:
            print_rrrKE(out, &code);
            break;
        case CALL_WRAPPER_BINARY_NULL_PROC:
            print_rrKE(out, &code);
            break;
        case NO_ARGS:
            print_rrE(out, &code);
            break;
        case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
            print_rrE(out, &code);
            break;
        case ONE_PLUS_ARGS: case TWO_ARGS:
            print_rrooE(out, &code);
            break;
        case THREE_ARGS:
            print_rroooE(out, &code);
            break;
        case EXIT_IF_FALSE: case EXIT_IF_TRUE: case EXIT_IF_NOT_BOOL_OR_INT:
            print_rE(out, &code);
            break;
        case ENSURE_TYPE: case ENSURE_VALUE: case POLY_EXIT:
        case POLY_TYPE_EXIT: case  GEN_CHECK_OR_EXIT:
            print_rKE(out, &code);
            break;
        case ENSURE_SLOT:
            print_rKKE(out, &code);
            break;
        case EXIT:
            print_E(out, &code);
            break;
        case BINARY_OP:
            print_rrok(out, &code);
            break;
        case BINARY_OP_KV: case BINARY_OP_VK:
            print_rokk(out, &code);
            break;
        case SET_LASTI:
            print_K(out, &code);
            break;
        case CONST_TO_REGISTER: case PICK: case LOAD_FAST: case BUILD_SET:
        case LOAD_DEREF: case BUILD_TUPLE: case BUILD_LIST: case BUILD_MAP:
        case LOAD_CLOSURE:
            print_oK(out, &code);
            break;
        case STORE_FAST: case STORE_DEREF: case UNPACK_SEQUENCE:
            print_rK(out, &code);
            break;
        case LIST_APPEND: case SET_ADD:
            print_rr(out, &code);
            break;
        case TUPLE_SET_ITEM: case LIST_SET_ITEM:
        case STORE_SLOT: case MATERIALISE_FRAME:
            print_rrk(out, &code);
            break;
        case LOAD_FROM_GLOBAL_AND_BUILTINS:
            print_rroK(out, &code);
            break;
        case HAS_SPECIAL:
            print_roK(out, &code);
            break;
        case BUILD_SLICE: case OVERRIDES:
            print_rrro(out, &code);
            break;
        case DESCRIPTOR_GET: case GET_CLASS_ATTR: case HAS_CLASS_ATTR:
        case SUB_TYPE: case NEW_FUNCTION:
            print_rro(out, &code);
            break;
        case VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST:
            print_rokkkE(out, &code);
            break;
        case POP_STACK_TO_REGISTER:
            print_o(out, &code);
            break;
        case LOAD_SLOT:
            print_rok(out, &code);
            break;
        case LOAD_SLOT_EX: case LOAD_MEMBER:
            print_rokE(out, &code);
            break;
        case STORE_TO_GLOBALS:
            print_rrK(out, &code);
            break;
        case RETURN_VALUE:
            print_r(out, &code);
            break;
        case SET_IN_OBJECT_DICT_CONST:
            print_rrkkk(out, &code);
            break;
        case TWO_MOVE:
            print_rroo(out, &code);
            break;
        case COPY_MOVE:
            print_roo(out, &code);
            break;
        case FAST_LOAD_GLOBAL:
            print_oKKK(out, &code);
            break;
        default:
            fprintf(stderr, "Unexpected opcode: %s\n", _HotPy_Instruction_Names[op]);
            exit(-1);
        }
    }
    fflush(out);
}

/* Steals a reference to the context */
static Py_ssize_t
new_exit(HotPyTraceRecorder *opt, HotPyContext *context)
{
    Py_ssize_t index;
    PyObject *exit;
    exit = HotPyExitObject_New(context);
    Py_DECREF(context);
    if (exit == NULL)
        FAILED(opt);
    index = PyList_Size(opt->exits);
    if (PyList_Append(opt->exits, exit))
        FAILED(opt);
    return index;
}

static Py_ssize_t
index_for_object(HotPyTraceRecorder *opt, PyObject *ids,
                 PyObject *list, PyObject *value)
{
    PyObject *index_obj;
    Py_ssize_t index;
    PyObject *id = PyLong_FromVoidPtr(value);
    if (id == NULL)
        FAILED(opt);
    index_obj = PyDict_GetItem(ids, id);
    if (index_obj == NULL) {
        index = PyList_Size(list);
        index_obj = PyLong_FromSize_t(index);
        if (index_obj == NULL)
            FAILED(opt);
        if (PyDict_SetItem(ids, id, index_obj))
            FAILED(opt);
        if (PyList_Append(list, value))
            FAILED(opt);
    } else
        index = PyLong_AsSsize_t(index_obj);
    Py_DECREF(id);
    assert(index >= 0);
    return index;
}

static Py_ssize_t
index_for_constant(HotPyTraceRecorder *opt, PyObject *value)
{
    return index_for_object(opt, opt->constant_ids, opt->constant_list, value);
}

static Py_ssize_t
index_for_name(HotPyOptimiser *x, PyObject *value)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    return index_for_object(opt, opt->name_ids, opt->name_list, value);
}

static int
index_for_keys(HotPyTraceRecorder *opt, PyDictKeysObject *keys)
{
    int i;
    for (i = 0; i < opt->keys_count; i++) {
        if (opt->keys[i] == keys)
            return i;
    }
    if (opt->keys_count == 256)
        return -1;
    opt->keys[opt->keys_count] = keys;
    PyDictKeysObject_IncRef(keys);
    return opt->keys_count++;
}

static void
store_register(HotPyOptimiser *x, int arg)
{
    int from = pop_as_register(x);
    free_register(x, from);
    write_move(x, from, arg);
    CONSISTENT(x);
}

static void
push_constant(HotPyOptimiser *x, PyObject *value)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    Py_ssize_t index;
    assert(value);
    index = index_for_constant(opt, value);
    if (index < 0)
        return;
    write_oK(x, CONST_TO_REGISTER, index);
    CONSISTENT(x);
}

static void
pick(HotPyOptimiser *x, int depth)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int reg = push_as_register(x);
    if (opt->stack_items > depth) {
        int from = opt->stack[opt->stack_items-depth-1];
        write_move(x, from, reg);
    }
    else {
        write_word_112(x, PICK, reg, depth - opt->stack_items);
    }
    CONSISTENT(x);
}

static HotPyContext *
current_context(HotPyOptimiser *x)
{
    return HotPyContext_New();
}

static HotPyContext *
require_type(HotPyOptimiser *x, PyTypeObject* type)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int e, k;
    HotPyContext *context = HotPyContext_New();
    k = index_for_constant(opt, (PyObject *)type);
    e = new_exit(opt, context);
    if (e < 0)
        return NULL;
    write_rKE(x, ENSURE_TYPE, k, e);
    CONSISTENT(x);
    return context;
}

static HotPyContext *
require_value(HotPyOptimiser *x, PyObject* value)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int e, k;
    HotPyContext *context = HotPyContext_New();
    k = index_for_constant(opt, value);
    e = new_exit(opt, context);
    if (e < 0)
        return NULL;
    write_rKE(x, ENSURE_VALUE, k, e);
    CONSISTENT(x);
    return context;
}

static HotPyContext *
require_lasti(HotPyOptimiser *x, int lasti)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int e;
    HotPyContext *context = HotPyContext_New();
    e = new_exit(opt, context);
    if (e < 0)
        return NULL;
    /* lasti could be -1 which is treated as 2**16-1 */
    write_rKE(x, GEN_CHECK_OR_EXIT, (unsigned short)lasti, e);
    CONSISTENT(x);
    return context;
}

static HotPyContext *
require_boolean_or_int(HotPyOptimiser *x)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    Py_ssize_t index = new_exit(opt, context);
    if (index < 0)
        return NULL;
    write_rE(x, EXIT_IF_NOT_BOOL_OR_INT, index);
    CONSISTENT(x);
    return context;
}

static HotPyContext *
call_cfunction(HotPyOptimiser *x, PyObject *f, int args)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    int e = new_exit(opt, context);
    int k = index_for_constant(opt, f);
    if (e < 0 || k < 0)
        return NULL;
    switch(args) {
    case 0:
        write_oKE(x, CALL_CFUNC_0, k, e);
        break;
    case 1:
        write_roKE(x, CALL_CFUNC_1, k, e);
        break;
    case 2:
        write_rroKE(x, CALL_CFUNC_2, k, e);
        break;
    default:
        assert("Wrong args" && 0);
        abort();
    }
    CONSISTENT(x);
    return context;
}

static HotPyContext *
call_cmethod(HotPyOptimiser *x, PyObject *m, int args)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    int e = new_exit(opt, context);
    int k = index_for_constant(opt, m);
    if (e < 0 || k < 0)
        return NULL;
    switch(args) {
    case 0:
        write_roKE(x, CALL_CMETH_0, k, e);
        break;
    case 1:
        write_rroKE(x, CALL_CMETH_1, k, e);
        break;
    case 2:
        write_rrrokE(x, CALL_CMETH_2, k, e);
        break;
    default:
        assert("Wrong args" && 0);
        abort();
    }
    CONSISTENT(x);
    return context;
}

static HotPyContext *
call_wrapper(HotPyOptimiser *x, int opcode, PyObject *w)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    int e = new_exit(opt, context);
    int k = index_for_constant(opt, w);
    if (e < 0 || k < 0)
        return NULL;
    switch(opcode) {
    case CALL_WRAPPER_UNARY:
    case CALL_WRAPPER_NEXT:
        write_roKE(x, opcode, k, e);
        break;
    case CALL_WRAPPER_BINARY:
        write_rroKE(x, opcode, k, e);
        break;
    case CALL_WRAPPER_BINARY_NULL_PROC:
        write_rrKE(x, opcode, k, e);
        break;
    case CALL_WRAPPER_TRINARY_PROC:
        write_rrrKE(x, opcode, k, e);
        break;
    }
    CONSISTENT(x);
    return context;
}

static HotPyContext *
opcode_with_exit(HotPyOptimiser *x, int op)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    Py_ssize_t index = new_exit(opt, context);
    if (index < 0)
        return NULL;
    switch (op) {
        break;
    case VALUE_FROM_OBJECT_DICT_OR_EXIT: case CALL_CFUNC_1: case CALL_CMETH_0:
        write_rroE(x, op, index);
        break;
    case ONE_ARG: case TRACE_DESCRIPTOR_GET:
        write_rroE(x, op, index);
        break;
    case TRACE_DESCRIPTOR_SET:
        write_rrrE(x, op, index);
        break;
    case NO_ARGS:
        write_rrE(x, op, index);
        break;
    case ONE_PLUS_ARGS: case TWO_ARGS:
        write_rrooE(x, op, index);
        break;
    case THREE_ARGS:
        write_rroooE(x, op, index);
        break;
    case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
        write_rrE(x, op, index);
        break;
    case EXIT:
        write_E(x, op, index);
        break;
    case POP_EXIT_IF_FALSE:
        write_rE(x, EXIT_IF_FALSE, index);
        break;
    case POP_EXIT_IF_TRUE:
        write_rE(x, EXIT_IF_TRUE, index);
        break;
    default:
        fprintf(stderr, "Trace Recorder (register) not implemented: %s\n", _HotPy_Instruction_Names[op]);
        exit(-1);
    }
    CONSISTENT(x);
    return context;
}


static void clear(HotPyOptimiser *x)
{
    int i;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    opt->next_inst = 0;
    opt->allocated_insts = 0;
    if (opt->insts)
        PyMem_Free(opt->insts);
    opt->insts = NULL;
    opt->stack_items = 0;
    Py_CLEAR(opt->exits);
    Py_CLEAR(opt->constant_ids);
    Py_CLEAR(opt->constant_list);
    Py_CLEAR(opt->trace);
    while (opt->keys_count)
        PyDictKeysObject_DecRef(opt->keys[--opt->keys_count]);
    for (i = 0; i < HOTPY_STACK_REGISTERS; i++)
        opt->used_regs[i] = 0;
}

static void
fail(HotPyOptimiser *x, int op, char *msg)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    /* Blacklist the trace */
    HotPyTrace_Invalidate((PyObject *)opt->trace);
    opt->trace->execution_count = opt->trace->execution_count/2 - 1000;
    clear(x);
}

static int coalesce_registers(uint32_t *, int, PyObject *);

static int compact_code(uint32_t *code, int count)
{
    uint32_t *instr = code;
    uint32_t *out = code;
    uint32_t *end;
    while (instr < code + count) {
        int op = instr[0] & 255;
        switch(op) {
        case NOP:
            instr++;
            break;
        case MOVE:
        {
            uint32_t from = (instr[0] >> 8) & 255;
            uint32_t to = (instr[0] >> 16) & 255;
            if (from == to) {
                instr++;
                break;
            }
            out[0] = instr[0];
            out++;
            instr++;
            break;
        }
        case LEAVE_FRAME: case POP_CALLER_BLOCK: case CLEAR_FRAME:
        case POP_BLOCK: case POP_EXCEPT: case SETUP_EXCEPT_HANDLER:
        case STOP_ITERATION_OR_RERAISE: case RERAISE:
        case SAVE_EXC_STATE: case SWAP_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
        case DELETE_FAST: case DELETE_DEREF:
        case GET_GLOBAL_AND_BUILTINS:
        case UNARY_NOT: case AS_TUPLE: case MAKE_GENERATOR: case GEN_STARTED:
        case HAS_DICT: case GET_TYPE: case OVERRIDES_GENERIC_GETATTRIBUTE:
        case OVERRIDES_GENERIC_SETATTR: case ALLOCATE: case SIZE:
        case STORE_MAP: case DESCRIPTOR_SET: case SET_IN_OBJECT_DICT:
        case ENTER_FRAME: case PUSH_REGISTER_TO_STACK: case STORE_LOCALS:
        case GEN_ENTER: case GEN_EXIT: case EXIT: case SET_LASTI:
        case CONST_TO_REGISTER: case PICK: case LOAD_FAST: case BUILD_SET:
        case LOAD_DEREF: case BUILD_TUPLE: case BUILD_LIST: case BUILD_MAP:
        case LOAD_CLOSURE: case STORE_FAST: case STORE_DEREF:
        case UNPACK_SEQUENCE: case LIST_APPEND: case SET_ADD:
        case TUPLE_SET_ITEM: case LIST_SET_ITEM: case STORE_SLOT:
        case DESCRIPTOR_GET: case GET_CLASS_ATTR: case HAS_CLASS_ATTR:
        case SUB_TYPE: case NEW_FUNCTION: case POP_STACK_TO_REGISTER:
        case LOAD_SLOT: case RETURN_VALUE: case GEN_CLEAR:
        case EXIT_IF_FALSE: case EXIT_IF_TRUE: case EXIT_IF_NOT_BOOL_OR_INT:
        case MATERIALISE_FRAME:
            assert(length_via_format_matches(op, 1));
            out[0] = instr[0];
            out++;
            instr++;
            break;
        case ONE_ARG: case CALL_CFUNC_0: case SETUP_BLOCK:
        case CALL_CFUNC_2: case CALL_CMETH_1: case CALL_CMETH_2:
        case CALL_WRAPPER_BINARY: case CALL_OBJECT: case TRACE_DESCRIPTOR_SET:
        case CALL_WRAPPER_TRINARY_PROC: case TRACE_DESCRIPTOR_GET:
        case CALL_WRAPPER_BINARY_NULL_PROC: case NO_ARGS:
        case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
        case ONE_PLUS_ARGS: case TWO_ARGS: case THREE_ARGS:
        case MAKE_FRAME: case NEW_FRAME: case NEW_CLOSURE: case KW_DICT_MERGE:
        case CALL_WRAPPER_NEXT: case CALL_WRAPPER_UNARY:
        case CALL_CFUNC_1: case CALL_CMETH_0:
        case VALUE_FROM_OBJECT_DICT_OR_EXIT:
        case ENSURE_TYPE: case ENSURE_VALUE: case POLY_EXIT:
        case POLY_TYPE_EXIT: case  GEN_CHECK_OR_EXIT:
        case ENSURE_SLOT: case BINARY_OP: case LOAD_MEMBER:
        case BINARY_OP_KV: case BINARY_OP_VK:
        case LOAD_FROM_GLOBAL_AND_BUILTINS:
        case HAS_SPECIAL: case BUILD_SLICE: case OVERRIDES:
        case VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST:
        case STORE_TO_GLOBALS: case SET_IN_OBJECT_DICT_CONST:
        case FAST_LOAD_GLOBAL: case LOAD_SLOT_EX:
            assert(length_via_format_matches(op, 2));
            out[0] = instr[0];
            out[1] = instr[1];
            out += 2;
            instr += 2;
            break;
        default:
            fprintf(stderr, "Unexpected opcode: %s\n", _HotPy_Instruction_Names[op]);
            abort();
        }
    }
    end = out;
    instr = code;
    out = code;
    while (instr < end) {
        int op = instr[0] & 255;
        switch(op) {
        case MOVE:
        {
            uint32_t from = (instr[0] >> 8) & 255;
            uint32_t to = (instr[0] >> 16) & 255;
            assert (from != to);
            if ((instr[1] & 255) == MOVE) {
                uint32_t from1 = (instr[1] >> 8) & 255;
                uint32_t to1 = (instr[1] >> 16) & 255;
                assert (from1 != to1);
                if (to == from1 || to == to1) {
                    out[0] = COPY_MOVE | (from << 8) |
                    (to << 16) | (to1 << 24);
                    instr += 2;
                    out++;
                    break;
                }
                else {
                    out[0] = TWO_MOVE | (from << 8) |
                               (from1 << 16) | (to << 24);
                    out[1] = to1;
                    instr += 2;
                    out += 2;
                    break;
                }
            }
            out[0] = instr[0];
            instr++;
            out++;
            break;
        }
        case LEAVE_FRAME: case POP_CALLER_BLOCK: case CLEAR_FRAME:
        case POP_BLOCK: case POP_EXCEPT: case SETUP_EXCEPT_HANDLER:
        case STOP_ITERATION_OR_RERAISE: case RERAISE:
        case SAVE_EXC_STATE: case SWAP_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
        case DELETE_FAST: case DELETE_DEREF:
        case GET_GLOBAL_AND_BUILTINS:
        case UNARY_NOT: case AS_TUPLE: case MAKE_GENERATOR: case GEN_STARTED:
        case HAS_DICT: case GET_TYPE: case OVERRIDES_GENERIC_GETATTRIBUTE:
        case OVERRIDES_GENERIC_SETATTR: case ALLOCATE: case SIZE:
        case STORE_MAP: case DESCRIPTOR_SET: case SET_IN_OBJECT_DICT:
        case ENTER_FRAME: case PUSH_REGISTER_TO_STACK: case STORE_LOCALS:
        case GEN_ENTER: case GEN_EXIT: case EXIT: case SET_LASTI:
        case CONST_TO_REGISTER: case PICK: case LOAD_FAST: case BUILD_SET:
        case LOAD_DEREF: case BUILD_TUPLE: case BUILD_LIST: case BUILD_MAP:
        case LOAD_CLOSURE: case STORE_FAST: case STORE_DEREF:
        case UNPACK_SEQUENCE: case LIST_APPEND: case SET_ADD:
        case TUPLE_SET_ITEM: case LIST_SET_ITEM: case STORE_SLOT:
        case DESCRIPTOR_GET: case GET_CLASS_ATTR: case HAS_CLASS_ATTR:
        case SUB_TYPE: case NEW_FUNCTION: case POP_STACK_TO_REGISTER:
        case LOAD_SLOT: case RETURN_VALUE: case GEN_CLEAR:
        case EXIT_IF_FALSE: case EXIT_IF_TRUE: case EXIT_IF_NOT_BOOL_OR_INT:
        case MATERIALISE_FRAME:
            assert(length_via_format_matches(op, 1));
            out[0] = instr[0];
            out++;
            instr++;
            break;
        case ONE_ARG: case CALL_CFUNC_0: case SETUP_BLOCK:
        case CALL_CFUNC_2: case CALL_CMETH_1: case CALL_CMETH_2:
        case CALL_WRAPPER_BINARY: case CALL_OBJECT: case TRACE_DESCRIPTOR_SET:
        case CALL_WRAPPER_TRINARY_PROC: case TRACE_DESCRIPTOR_GET:
        case CALL_WRAPPER_BINARY_NULL_PROC: case NO_ARGS:
        case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
        case ONE_PLUS_ARGS: case TWO_ARGS: case THREE_ARGS:
        case MAKE_FRAME: case NEW_FRAME: case NEW_CLOSURE: case KW_DICT_MERGE:
        case CALL_WRAPPER_NEXT: case CALL_WRAPPER_UNARY:
        case CALL_CFUNC_1: case CALL_CMETH_0:
        case VALUE_FROM_OBJECT_DICT_OR_EXIT:
        case ENSURE_TYPE: case ENSURE_VALUE: case POLY_EXIT:
        case POLY_TYPE_EXIT: case  GEN_CHECK_OR_EXIT:
        case ENSURE_SLOT: case BINARY_OP: case LOAD_MEMBER:
        case BINARY_OP_KV: case BINARY_OP_VK:
        case LOAD_FROM_GLOBAL_AND_BUILTINS:
        case HAS_SPECIAL: case BUILD_SLICE: case OVERRIDES:
        case VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST:
        case STORE_TO_GLOBALS: case SET_IN_OBJECT_DICT_CONST:
        case FAST_LOAD_GLOBAL: case LOAD_SLOT_EX:
            assert(length_via_format_matches(op, 2));
            out[0] = instr[0];
            out[1] = instr[1];
            out += 2;
            instr += 2;
            break;
        default:
            fprintf(stderr, "Unexpected opcode: %s\n", _HotPy_Instruction_Names[op]);
            abort();
        }
    }
    return out - code;
}




static void
finish(HotPyOptimiser *x)
{
    static FILE *coalesced_out = NULL;
    PyObject *consts;
    PyObject *exits;
    PyObject *bytes;
    PyObject *names;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    PyDictKeysObject **keys;
    if (opt->trace) {
        if (_HotPy_Options.log_trace) {
            if (opt->log_file == NULL)
                opt->log_file = fopen("/tmp/hotpy_regtrace", "w+");
            if (opt->log_file)
                print_register_code(opt->insts, opt->next_inst, opt->log_file);
        }
        exits = PyList_AsTuple(opt->exits);
        if (_HotPy_Options.coalesce) {
            coalesce_registers(opt->insts, opt->next_inst, exits);
            if (_HotPy_Options.compact)
                opt->next_inst = compact_code(opt->insts, opt->next_inst);
            if (_HotPy_Options.log_trace) {
                if (coalesced_out == NULL)
                    coalesced_out = fopen("/tmp/hotpy_coalesced", "w+");
                if (coalesced_out)
                    print_register_code(opt->insts, opt->next_inst, coalesced_out);
            }
        }
        else if (_HotPy_Options.compact)
            opt->next_inst = compact_code(opt->insts, opt->next_inst);
        bytes = PyBytes_FromStringAndSize((char *)opt->insts, opt->next_inst * sizeof(int32_t));
        consts = PyList_AsTuple(opt->constant_list);
        names = PyList_AsTuple(opt->name_list);
        keys = PyMem_Malloc(sizeof(PyDictKeysObject *) * opt->keys_count);
        if (bytes && consts && exits && opt->trace->status == TRACE_NOT_READY && keys) {
            HotPyTraceObject_Init(opt->trace, bytes, exits, consts, names);
            opt->trace->trace_keys = keys;
            memcpy(keys, opt->keys, sizeof(PyDictKeysObject *) * opt->keys_count);
            opt->keys_count = 0;
        }
        else {
            HotPyTrace_Invalidate((PyObject *)opt->trace);
            Py_XDECREF(bytes);
            Py_XDECREF(consts);
            Py_XDECREF(exits);
            PyMem_FREE(keys);
        }
    }
    CONSISTENT(x);
    clear(x);
}

static int
start(HotPyOptimiser *x, HotPyTraceObject *trace,
      PyFrameObject *f, HotPyContext *ctx, char *reason)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    opt->next_inst = 0;
    opt->loop_header = 0;
    opt->insts = PyMem_Malloc(128*sizeof(int32_t));
    opt->allocated_insts = 128;
    opt->exits = PyList_New(0);
    opt->constant_ids = PyDict_New();
    opt->constant_list = PyList_New(0);
    opt->name_ids = PyDict_New();
    opt->name_list = PyList_New(0);
    if (opt->insts == NULL || opt->exits == NULL ||
        opt->constant_ids == NULL || opt->constant_list == NULL ||
        opt->name_ids == NULL || opt->name_list == NULL) {
        clear(x);
        return -1;
    }
    Py_INCREF(trace);
    opt->trace = trace;
    /* Mark as not-ready at start, so that any guards that are triggered
       during tracing (which is possible) are able to invalidate the trace */
    trace->status = TRACE_NOT_READY;
    assert(opt->keys_count == 0);
    CONSISTENT(x);
    return 0;
}

static void
opcode(HotPyOptimiser *x, int op)
{
    int r0, r1, r2, r3;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    switch(op) {
    case POP_TOP:
        free_register(x, pop_as_register(x));
        return;
    case ROT_TWO:
        if (opt->stack_items >= 2) {
            r0 = opt->stack[opt->stack_items-2];
            r1 = opt->stack[opt->stack_items-1];
            opt->stack[opt->stack_items-2] = r1;
            opt->stack[opt->stack_items-1] = r0;
        }
        else {
            r1 = pop_as_register(x);
            r0 = pop_as_register(x);
            r3 = push_as_register(x);
            write_move(x, r1, r3);
            r3 = push_as_register(x);
            write_move(x, r0, r3);
            free_register(x, r0);
            free_register(x, r1);
        }
        return;
    case ROT_THREE:
        if (opt->stack_items >= 3) {
            r0 = opt->stack[opt->stack_items-3];
            r1 = opt->stack[opt->stack_items-2];
            r2 = opt->stack[opt->stack_items-1];
            opt->stack[opt->stack_items-3] = r2;
            opt->stack[opt->stack_items-2] = r0;
            opt->stack[opt->stack_items-1] = r1;
        }
        else {
            r2 = pop_as_register(x);
            r1 = pop_as_register(x);
            r0 = pop_as_register(x);
            r3 = push_as_register(x);
            write_move(x, r2, r3);
            r3 = push_as_register(x);
            write_move(x, r0, r3);
            r3 = push_as_register(x);
            write_move(x, r1, r3);
            free_register(x, r0);
            free_register(x, r1);
            free_register(x, r2);
        }
        return;
    case DUP_TOP:
        r0 = peek_as_register(x);
        r1 = push_as_register(x);
        write_move(x, r0, r1);
        return;
    case DUP_TOP_TWO:
        r1 = pop_as_register(x);
        r0 = peek_as_register(x);
        r3 = push_as_register(x);
        write_move(x, r1, r3);
        r2 = push_as_register(x);
        write_move(x, r0, r2);
        r3 = push_as_register(x);
        write_move(x, r1, r3);
        free_register(x, r1);
        return;
    case VALUE_NOP: case NOP:
        return;
    case GET_CLASS_ATTR: case HAS_CLASS_ATTR: case NEW_FUNCTION:
    case DESCRIPTOR_GET: case SUB_TYPE:
        write_rro(x, op);
        return;
    case HAS_DICT: case GET_TYPE: case GEN_STARTED: case AS_TUPLE:
    case OVERRIDES_GENERIC_GETATTRIBUTE: case UNARY_NOT:
    case MAKE_GENERATOR: case SIZE: case ALLOCATE:
    case OVERRIDES_GENERIC_SETATTR:
        write_ro(x, op);
        return;
    case POP_BLOCK: case POP_CALLER_BLOCK: case STOP_ITERATION_OR_RERAISE:
    case SWAP_EXC_STATE: case SAVE_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
    case CLEAR_FRAME: case POP_EXCEPT:
        write_word_112(x, op, 0, 0);
        return;
    case SET_IN_OBJECT_DICT: case DESCRIPTOR_SET:
        write_rrr(x, op);
        return;
    case STORE_MAP:
        /* STORE_MAP would have rrp format, but that is not allowed. */
        r0 = peek_as_register(x);
        write_rrr(x, op);
        r1 = push_as_register(x);
        write_move(x, r0, r1);
        return;
    case GEN_EXIT: case RETURN_VALUE:
        write_r(x, op);
        return;
    case GEN_CLEAR:
        write_p(x, op);
        return;
    case NEW_CLOSURE: case OVERRIDES: case KW_DICT_MERGE:
        write_rrro(x, op);
        return;
    default:
        fprintf(stderr, "Trace Recorder (register) not implemented: %s\n", _HotPy_Instruction_Names[op]);
        exit(-1);
    }
    CONSISTENT(x);
}

#define TEMP_REG 0

static void
opcode_with_arg(HotPyOptimiser *x, int op, int oparg)
{
    int i, reg;
    switch(op) {
    case LIST_APPEND: case SET_ADD:
        assert(oparg == 0);
        write_rr(x, op);
        CONSISTENT(x);
        return;
    case DELETE_DEREF:
        write_rK(x, op, oparg);
        CONSISTENT(x);
        return;
    case LOAD_DEREF:
        write_oK(x, op, oparg);
        CONSISTENT(x);
        return;
    case STORE_DEREF:
        write_rK(x, op, oparg);
        CONSISTENT(x);
        return;
    case BUILD_TUPLE:
        reg = choose_register(x);
        write_word_112(x, op, reg, oparg);
        for (i = oparg-1; i >= 0; i--) {
            int item_reg = pop_as_register(x);
            free_register(x, item_reg);
            /* TUPLE_SET_ITEM has rrk format */
            write_word_1111(x, TUPLE_SET_ITEM, reg, item_reg, i);
        }
        free_register(x, reg);
        write_move(x, reg, push_as_register(x));
        CONSISTENT(x);
        return;
    case BUILD_LIST:
        reg = choose_register(x);
        write_word_112(x, op, reg, oparg);
        for (i = oparg-1; i >= 0; i--) {
            int item_reg = pop_as_register(x);
            free_register(x, item_reg);
            /* LIST_SET_ITEM has rrk format */
            write_word_1111(x, LIST_SET_ITEM, reg, item_reg, i);
        }
        free_register(x, reg);
        write_move(x, reg, push_as_register(x));
        CONSISTENT(x);
        return;
    case BUILD_SET: case BUILD_MAP: case LOAD_CLOSURE:
        write_oK(x, op, oparg);
        CONSISTENT(x);
        return;
    case BUILD_SLICE:
        if (oparg == 2)
            push_constant(x, Py_None);
        write_rrro(x, op);
        CONSISTENT(x);
        return;
    case UNPACK_SEQUENCE:
        reg = pop_as_register(x);
        flush_stack(x);
        free_register(x, reg);
        write_word_112(x, op, reg, oparg);
        CONSISTENT(x);
        return;
    case MATERIALISE_FRAME: case STORE_SLOT:
        write_rrk(x, op, oparg);
        CONSISTENT(x);
        return;
    default:
        fprintf(stderr, "Trace Recorder (register) not implemented: %s\n", _HotPy_Instruction_Names[op]);
        exit(-1);
    }
}

static void
binary_op(HotPyOptimiser *x, int func, PyObject *res)
{
    write_rrok(x, BINARY_OP, func);
    CONSISTENT(x);
}

static void
binary_op_vk(HotPyOptimiser *x, int func, PyObject *k, PyObject *res)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int index = index_for_constant(opt, k);
    write_rokk(x, BINARY_OP_VK, func, index);
    CONSISTENT(x);
}

static void
binary_op_kv(HotPyOptimiser *x, int func, PyObject *k, PyObject *res)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int index = index_for_constant(opt, k);
    write_rokk(x, BINARY_OP_KV, func, index);
    CONSISTENT(x);
}

static void
set_lasti(HotPyOptimiser *x, int lasti)
{
    write_K(x, SET_LASTI, lasti);
    CONSISTENT(x);
}

static void
load_fast(HotPyOptimiser *x, int arg)
{
    write_oK(x, LOAD_FAST, arg);
    CONSISTENT(x);
}

static void
store_fast(HotPyOptimiser *x, int arg)
{
    write_rK(x, STORE_FAST, arg);
    CONSISTENT(x);
}

static void
delete_fast(HotPyOptimiser *x, int arg)
{
    write_K(x, DELETE_FAST, arg);
    CONSISTENT(x);
}

static void
load_clear_register(HotPyOptimiser *x, int arg)
{
    int r = push_as_register(x);
    write_move(x, arg, r);
    CONSISTENT(x);
}

static void
load_register(HotPyOptimiser *x, int arg)
{
    int r = push_as_register(x);
    write_move(x, arg, r);
    CONSISTENT(x);
}

static void
tuple_concat(HotPyOptimiser *x)
{
    static int tuple_concat_function = 0;
    if (tuple_concat_function == 0) {
        int index;
        _Py_AddBinaryFastFunction(&_Py_operator_nb_add, &PyTuple_Type,
                  &PyTuple_Type, PyTuple_Type.tp_as_sequence->sq_concat);
        index = BINARY_FUNCTION_INDEX(&PyTuple_Type, &PyTuple_Type);
        tuple_concat_function =_Py_operator_nb_add.functions[index];
        assert(tuple_concat_function && "must be non-zero");
    }
    binary_op(x, tuple_concat_function, NULL);
    CONSISTENT(x);
}

static void
has_special(HotPyOptimiser *x, PyObject *name)
{
    int index = index_for_name(x, name);
    write_roK(x, HAS_SPECIAL, index);
    CONSISTENT(x);
}

static void
fast_frame(HotPyOptimiser *x, int arg, PyCodeObject *code)
{
    int i;
    int r0, r1, r2, frame_reg;
    int offset0 = offsetof(PyFrameObject, f_localsplus[0])/sizeof(PyObject*);
    r2 = pop_as_register(x);
    r1 = pop_as_register(x);
    r0 = pop_as_register(x);
    free_register(x, r1);
    free_register(x, r2);
    write_word_1111(x, NEW_FRAME, r0, r1, r2);
    write_word_112(x, r0, 0, 0);
    for (i = arg-1; i >= 0; i--) {
        int reg = pop_as_register(x);
        free_register(x, reg);
        write_word_1111(x, STORE_SLOT, r0, reg, offset0 + i);
    }
    free_register(x, r0);
    frame_reg = push_as_register(x);
    write_move(x, r0, frame_reg);
    CONSISTENT(x);
}

static HotPyContext *
make_frame(HotPyOptimiser *x, PyObject *func)
{
    write_rrro(x, MAKE_FRAME);
    CONSISTENT(x);
    return NULL;
}

static void
make_frame_const(HotPyOptimiser *x, PyObject *func)
{
    write_rrro(x, MAKE_FRAME);
    CONSISTENT(x);
}

static void
gen_enter(HotPyOptimiser *x, PyCodeObject *code)
{
    int r = pop_as_register(x);
    flush_stack(x);
    free_register(x, r);
    write_word_112(x, GEN_ENTER, r, 0);
    CONSISTENT(x);
}

static void
enter_frame(HotPyOptimiser *x, PyCodeObject *code)
{
    int r = pop_as_register(x);
    flush_stack(x);
    free_register(x, r);
    write_word_112(x, ENTER_FRAME, r, 0);
    CONSISTENT(x);
}

static void
leave_frame(HotPyOptimiser *x)
{
    flush_stack(x);
    write_word_112(x, LEAVE_FRAME, 0, 0);
    CONSISTENT(x);
}

static void unrolling(HotPyOptimiser *x)
{
    /* To do... */
}

static HotPyContext *
prepare_call1(HotPyOptimiser *x)
{
    return NULL;
}

static HotPyContext *
prepare_call2(HotPyOptimiser *x)
{
    return NULL;
}

static HotPyContext *
call_object(HotPyOptimiser *x)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    int e = new_exit(opt, context);
    if (e < 0)
        return NULL;
    write_rrroE(x, CALL_OBJECT, e);
    CONSISTENT(x);
    return context;
}

static void
bind_callable(HotPyOptimiser *x, PyTypeObject *bound_type)
{
    write_rro(x, DESCRIPTOR_GET);
    CONSISTENT(x);
}

static void
comment(HotPyOptimiser *x, const char *text)
{
}

static HotPyContext *
load_member(HotPyOptimiser *x, PyMemberDescrObject *desc)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    PyMemberDef *def = desc->d_member;
    int e, slot = def->offset/sizeof(PyObject*);
    HotPyContext *context = NULL;
    assert(slot < (1 << 8));
    assert(def->offset % sizeof(PyObject*) == 0);
    if (def->type == T_OBJECT) {
        write_rok(x, LOAD_SLOT, slot);
    }
    else if (def->type == T_OBJECT_EX) {
        context = HotPyContext_New();
        e = new_exit(opt, context);
        if (e < 0)
            return NULL;
        write_rokE(x, LOAD_SLOT_EX, slot, e);
    }
    else {
        /* Don't support int/float slots efficiently yet */
        int index = index_for_constant(opt, (PyObject *)desc);
        context = HotPyContext_New();
        e = new_exit(opt, context);
        if (e < 0)
            return NULL;
        write_rokE(x, LOAD_MEMBER, index, e);
    }
    CONSISTENT(x);
    return context;
}

static HotPyContext *
store_member(HotPyOptimiser *x, PyMemberDef *def)
{
    int slot = def->offset/sizeof(PyObject*);
    assert(slot < (1 << 8));
    assert(def->offset % sizeof(PyObject*) == 0);
    assert((def->flags & READONLY) == 0);
    if (def->type == T_OBJECT || def->type == T_OBJECT_EX) {
        write_rrk(x, STORE_SLOT, slot);
    }
    else {
        /* Index for store-member is 5 bit type code + 11 bit offset
         * Type code is divided into 2 bit size code & 2 bit
        */
        assert(def->offset < (1 << 11));
        opcode_with_arg(x, STORE_MEMBER, ((def->type & 31) << 11) | def->offset);
    }
    CONSISTENT(x);
    return NULL;
}

static PyMemberDef func_code_def = {
    "__code__", T_OBJECT, offsetof(PyFunctionObject, func_code), READONLY
};

static PyMemberDef gen_code_def = {
    "gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY
};

static HotPyContext *
require_code(HotPyOptimiser *x, PyObject* obj)
{
    /* This should be rare if the specialiser is turned on */
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context;
    int index, offset, k;
    context = HotPyContext_New();
    index = new_exit(opt, context);
    if (index < 0)
        return NULL;
    if (PyFunction_Check(obj)) {
        offset = func_code_def.offset;
        k = index_for_constant(opt, PyFunction_GET_CODE(obj));
    }
    else {
        assert(PyGen_Check(obj));
        offset = gen_code_def.offset;
        k = index_for_constant(opt, ((PyGenObject *)obj)->gi_code);
    }
    write_rKKE(x, ENSURE_SLOT, offset, k, index);
    CONSISTENT(x);
    return context;
}

static void
setup_block(HotPyOptimiser *x, int op, int handler, int level)
{
    if (op == SETUP_EXCEPT_HANDLER) {
        flush_stack(x);
        write_word_112(x, SETUP_EXCEPT_HANDLER, 0, 0);
    }
    else if (op == SETUP_FOR_ITER)
        write_kkK(x, SETUP_BLOCK, FOR_ITER, level, handler);
    else
        write_kkK(x, SETUP_BLOCK, op, level, handler);
    CONSISTENT(x);
}

static void
get_globals(HotPyOptimiser *x)
{
    write_oo(x, GET_GLOBAL_AND_BUILTINS);
    CONSISTENT(x);
}

static void
load_from_globals(HotPyOptimiser *x, PyObject *name)
{
    int k = index_for_name(x, name);
    write_rroK(x, LOAD_FROM_GLOBAL_AND_BUILTINS, k);
}

static void
fast_load_from_globals(HotPyOptimiser *x, PyObject *globals, PyObject *name)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int k0 = index_for_constant(opt, globals);
    int k1 = index_for_name(x, name);
    write_oKKK(x, FAST_LOAD_GLOBAL, k0, k1, 0);
}

static void
store_to_globals(HotPyOptimiser *x, PyObject *name)
{
    int k = index_for_name(x, name);
    write_rrK(x, STORE_TO_GLOBALS, k);
    CONSISTENT(x);
}

static HotPyContext*
from_object_dict_fast(HotPyOptimiser *x, int dict_offset, PyDictKeysObject *keys, PyObject *key)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context;
    int k, index, exit;
    context = HotPyContext_New();
    k = index_for_keys(opt, keys);
    index = _PyDictKeysObject_IndexOf(keys, key);
    exit = new_exit(opt, context);
    if (exit < 0)
        return NULL;
    write_rokkkE(x, VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST, dict_offset, k, index, exit);
    CONSISTENT(x);
    return context;
}

static void
set_in_object_dict_fast(HotPyOptimiser *x, int dict_offset,
                        PyDictKeysObject *keys, PyObject *key)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    int k, index;
    k = index_for_keys(opt, keys);
    index = _PyDictKeysObject_IndexOf(keys, key);
    write_rrkkk(x, SET_IN_OBJECT_DICT_CONST, dict_offset, k, index);
    CONSISTENT(x);
}

HotPyOptimiser *HotPyTraceRecorderRegister_New(void) {
    int i;
    HotPyTraceRecorder *x = PyMem_Malloc(sizeof(HotPyTraceRecorder));
    x->opt_base.opt_name = "trace recorder (register)";
    x->opt_base.next = NULL;
    x->opt_base.push_constant = push_constant;
    x->opt_base.require_type = require_type;
    x->opt_base.require_lasti = require_lasti;
    x->opt_base.require_value = require_value;
    x->opt_base.require_code = require_code;
    x->opt_base.require_boolean_or_int = require_boolean_or_int;
    x->opt_base.opcode_with_exit = opcode_with_exit;
    x->opt_base.clear = clear;
    x->opt_base.start = start;
    x->opt_base.setup_block = setup_block;
    x->opt_base.finish = finish;
    x->opt_base.fail = fail;
    x->opt_base.comment = comment;
    x->opt_base.set_lasti = set_lasti;
    x->opt_base.unrolling = unrolling;
    x->opt_base.load_fast = load_fast;
    x->opt_base.store_fast = store_fast;
    x->opt_base.delete_fast = delete_fast;
    x->opt_base.get_globals = get_globals;
    x->opt_base.load_from_globals = load_from_globals;
    x->opt_base.fast_load_from_globals = fast_load_from_globals;
    x->opt_base.store_to_globals = store_to_globals;
    x->opt_base.has_special = has_special;
    x->opt_base.load_register = load_register;
    x->opt_base.load_clear_register = load_clear_register;
    x->opt_base.store_register = store_register;
    x->opt_base.set_lasti = set_lasti;
    x->opt_base.fast_frame = fast_frame;
    x->opt_base.make_frame = make_frame;
    x->opt_base.make_frame_const = make_frame_const;
    x->opt_base.enter_frame = enter_frame;
    x->opt_base.gen_enter = gen_enter;
    x->opt_base.leave_gen_frame = leave_frame;
    x->opt_base.leave_frame = leave_frame;
    x->opt_base.binary_op = binary_op;
    x->opt_base.binary_op_kv = binary_op_kv;
    x->opt_base.binary_op_vk = binary_op_vk;
    x->opt_base.tuple_concat = tuple_concat;
    x->opt_base.pick = pick;
    x->opt_base.bind_callable = bind_callable;
    x->opt_base.load_member = load_member;
    x->opt_base.store_member = store_member;
    x->opt_base.opcode = opcode;
    x->opt_base.opcode_with_arg = opcode_with_arg;
    x->opt_base.current_context = current_context;
    x->opt_base.call_object = call_object;
    x->opt_base.call_cfunction = call_cfunction;
    x->opt_base.call_cmethod = call_cmethod;
    x->opt_base.call_wrapper = call_wrapper;
    x->opt_base.prepare_call1 = prepare_call1;
    x->opt_base.prepare_call2 = prepare_call2;
    x->opt_base.from_object_dict_fast = from_object_dict_fast;
    x->opt_base.set_in_object_dict_fast = set_in_object_dict_fast;
    x->next_inst = 0;
    x->insts = NULL;
    x->allocated_insts = 0;
    x->exits = NULL;
    x->name_ids = NULL;
    x->name_list = NULL;
    x->constant_ids = NULL;
    x->constant_list = NULL;
    x->trace = NULL;
    x->stack_items = 0;
    for (i = 0; i < HOTPY_STACK_REGISTERS; i++)
        x->used_regs[i] = 0;
    x->log_file = NULL;
    x->keys_count = 0;
    return (HotPyOptimiser *)x;
}

/********* Register coalescing code ***********/

#define BITS_PER_LONG (8*sizeof(long))
#define VECTOR_WORDS (256/BITS_PER_LONG)

typedef struct _liverange {
    int def;
    int last_use;
    short fixed_reg;
    unsigned char reg;
    long interferes_with_fixed[VECTOR_WORDS];
} LiveRange;

typedef struct _relabelrange {
    unsigned int where;
    unsigned char what;
    unsigned char as;
} RelabelEvent;

static void
print_relabel(RelabelEvent *r, FILE *out)
{
    fprintf(out, "Relabel register %d as %d, from instruction %d.\n",
        r->what, r->as, r->where);
}

static void
print_relabels(RelabelEvent *relabels, int count)
{
    int i;
    for (i = 0; i < count; i++) {
        print_relabel(relabels+i, stdout);
    }
    printf("\n");
    fflush(stdout);
}

typedef enum _liveness_event_kind {
    EVENT_DEF,
    EVENT_LAST_USE,
    EVENT_MOVE_SRC,
} Kind;

typedef struct _liveness_event {
    unsigned short inst;
    Kind kind;
    unsigned char reg;
    unsigned char fixed;
} LivenessEvent;

static void
print_liveness_event(LivenessEvent *e, FILE *out)
{
    static char *kinds[] = { "Definition of", "Last use of", "Move source" };
    fprintf(out, "%s (%s) reg %d at %d\n", kinds[e->kind],
        e->fixed ? "fixed": "free", e->reg, e->inst);
}

typedef struct _move {
    int inst;
} Move;

static int
liveness_compare(const void *p0, const void *p1)
{
    const LivenessEvent *e0 = p0;
    const LivenessEvent *e1 = p1;
    if (e0->inst == e1->inst) {
        /* Uses come before defns & moves (moves are defns) */
        return e1->kind - e0->kind;
    }
    if (e0->inst < e1->inst)
        return -1;
    else
        return 1;
}

static uint32_t *
get_defs_and_uses_for(uint32_t *instr, int *defs, int *uses)
{
    int op = instr[0] & 255;
    switch (op) {
    case LEAVE_FRAME: case POP_CALLER_BLOCK: case CLEAR_FRAME: case NOP:
    case POP_BLOCK: case POP_EXCEPT: case SETUP_EXCEPT_HANDLER:
    case STOP_ITERATION_OR_RERAISE: case RERAISE:
    case SAVE_EXC_STATE: case SWAP_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
        defs[0] = -1;
        uses[0] = -1;
        return instr + 1;
    case DELETE_FAST: case DELETE_DEREF:
        return defuses_for_K(instr, defs, uses);
    case SETUP_BLOCK:
        return defuses_for_kkK(instr, defs, uses);
    case MAKE_FRAME: case NEW_FRAME: case NEW_CLOSURE: case KW_DICT_MERGE:
        return defuses_for_rrro(instr, defs, uses);
    case GET_GLOBAL_AND_BUILTINS:
        return defuses_for_oo(instr, defs, uses);
    case UNARY_NOT: case AS_TUPLE: case MAKE_GENERATOR: case GEN_STARTED:
    case HAS_DICT: case GET_TYPE: case OVERRIDES_GENERIC_GETATTRIBUTE:
    case OVERRIDES_GENERIC_SETATTR: case ALLOCATE: case SIZE: case MOVE:
        return defuses_for_ro(instr, defs, uses);
    case STORE_MAP: case DESCRIPTOR_SET: case SET_IN_OBJECT_DICT:
        return defuses_for_rrr(instr, defs, uses);
    case ENTER_FRAME: case PUSH_REGISTER_TO_STACK: case STORE_LOCALS:
    case GEN_ENTER: case GEN_EXIT:
        return defuses_for_r(instr, defs, uses);
    case GEN_CLEAR:
        return defuses_for_p(instr, defs, uses);
    case CALL_WRAPPER_NEXT: case CALL_WRAPPER_UNARY:
    case CALL_CFUNC_1: case CALL_CMETH_0:
        return defuses_for_roKE(instr, defs, uses);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT:
        return defuses_for_rroE(instr, defs, uses);
    case ONE_ARG: case TRACE_DESCRIPTOR_GET:
        return defuses_for_rroE(instr, defs, uses);
    case TRACE_DESCRIPTOR_SET:
        return defuses_for_rrrE(instr, defs, uses);
    case CALL_CFUNC_0:
        return defuses_for_oKE(instr, defs, uses);
    case CALL_CFUNC_2: case CALL_CMETH_1: case CALL_WRAPPER_BINARY:
        return defuses_for_rroKE(instr, defs, uses);
    case CALL_CMETH_2:
        return defuses_for_rrrokE(instr, defs, uses);
    case CALL_OBJECT:
        return defuses_for_rrroE(instr, defs, uses);
    case CALL_WRAPPER_TRINARY_PROC:
        return defuses_for_rrrKE(instr, defs, uses);
    case CALL_WRAPPER_BINARY_NULL_PROC:
        return defuses_for_rrKE(instr, defs, uses);
    case NO_ARGS:
        return defuses_for_rrE(instr, defs, uses);
    case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
        return defuses_for_rrE(instr, defs, uses);
    case ONE_PLUS_ARGS: case TWO_ARGS:
        return defuses_for_rrooE(instr, defs, uses);
    case THREE_ARGS:
        return defuses_for_rroooE(instr, defs, uses);
    case EXIT_IF_FALSE: case EXIT_IF_TRUE: case EXIT_IF_NOT_BOOL_OR_INT:
        return defuses_for_rE(instr, defs, uses);
    case ENSURE_TYPE: case ENSURE_VALUE: case POLY_EXIT:
    case POLY_TYPE_EXIT: case  GEN_CHECK_OR_EXIT:
        return defuses_for_rKE(instr, defs, uses);
    case ENSURE_SLOT:
        return defuses_for_rKKE(instr, defs, uses);
    case EXIT:
        return defuses_for_E(instr, defs, uses);
    case BINARY_OP:
        return defuses_for_rrok(instr, defs, uses);
    case BINARY_OP_KV: case BINARY_OP_VK:
         return defuses_for_rokk(instr, defs, uses);
    case SET_LASTI:
        return defuses_for_K(instr, defs, uses);
    case CONST_TO_REGISTER: case PICK: case LOAD_FAST: case BUILD_SET:
    case LOAD_DEREF: case BUILD_TUPLE: case BUILD_LIST: case BUILD_MAP:
    case LOAD_CLOSURE:
        return defuses_for_oK(instr, defs, uses);
    case STORE_FAST: case STORE_DEREF: case UNPACK_SEQUENCE:
        return defuses_for_rK(instr, defs, uses);
    case LIST_APPEND: case SET_ADD:
        return defuses_for_rr(instr, defs, uses);
    case TUPLE_SET_ITEM: case LIST_SET_ITEM:
    case STORE_SLOT: case MATERIALISE_FRAME:
        return defuses_for_rrk(instr, defs, uses);
    case LOAD_FROM_GLOBAL_AND_BUILTINS:
        return defuses_for_rroK(instr, defs, uses);
    case HAS_SPECIAL:
        return defuses_for_roK(instr, defs, uses);
    case BUILD_SLICE: case OVERRIDES:
        return defuses_for_rrro(instr, defs, uses);
    case DESCRIPTOR_GET: case GET_CLASS_ATTR: case HAS_CLASS_ATTR:
    case SUB_TYPE: case NEW_FUNCTION:
        return defuses_for_rro(instr, defs, uses);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST:
        return defuses_for_rokkkE(instr, defs, uses);
    case POP_STACK_TO_REGISTER:
        return defuses_for_o(instr, defs, uses);
    case LOAD_SLOT:
        return defuses_for_rok(instr, defs, uses);
    case LOAD_SLOT_EX: case LOAD_MEMBER:
        return defuses_for_rokE(instr, defs, uses);
    case STORE_TO_GLOBALS:
        return defuses_for_rrK(instr, defs, uses);
    case RETURN_VALUE:
        return defuses_for_r(instr, defs, uses);
    case SET_IN_OBJECT_DICT_CONST:
        return defuses_for_rrkkk(instr, defs, uses);
    case FAST_LOAD_GLOBAL:
        return defuses_for_oKKK(instr, defs, uses);
    default:
        fprintf(stderr, "Unexpected opcode: %s\n", _HotPy_Instruction_Names[op]);
        abort();
    }
}

#define COALESCE_DEBUG 0

extern void _HotPyContext_LiveRegisters(HotPyContext *, char *);

#define MAX_DEF_USES 5

static HotPyContext *get_exit_context(uint32_t *, PyObject *);

static LivenessEvent *
liveness_events(uint32_t *code, int count, int *event_count_ptr, PyObject *exits)
{
    HotPyContext *exit_context;
    int i;
    int allocated = 64;
    int event_count = 0;
    char live_on_exit[256];
    LivenessEvent *events = PyMem_Malloc(sizeof(LivenessEvent) * allocated);
    uint32_t *instr = code;
    uint32_t *limit_ptr = code + count;
    int last_use[256];
    if (events == NULL)
        return NULL;
    for (i = 0; i < 256; i++) {
        live_on_exit[i] = 0;
        last_use[i] = -1;
    }
    while (instr < limit_ptr) {
        int defs[4];
        int uses[5];
        int *def, *use;
        uint32_t *following_instr;
        if (event_count >= allocated - MAX_DEF_USES) {
            LivenessEvent *new_events =
                PyMem_Malloc(sizeof(LivenessEvent) * 2 * allocated);
            if (new_events == NULL) {
                PyMem_Free(events);
                return NULL;
            }
            memcpy(new_events, events, allocated * sizeof(LivenessEvent));
            allocated *= 2;
            PyMem_Free(events);
            events = new_events;
        }
        if ((*instr & 255) == MOVE) {
            LivenessEvent *e = &events[event_count++];
            e->kind = EVENT_MOVE_SRC;
            e->inst = instr - code;
            e->reg = (*instr >> 8)& 255;
            e->fixed = 0;
        }
        following_instr = get_defs_and_uses_for(instr, defs, uses);
        for (use = &uses[0]; use[0] >= 0; use++) {
            last_use[*use] = instr - code;
        }
        for (def = &defs[0]; def[0] >= 0; def++) {
            LivenessEvent *e;
            if (last_use[*def] >= 0) {
                e = &events[event_count++];
                e->inst = last_use[*def];
                e->reg = *def;
                e->kind = EVENT_LAST_USE;
                e->fixed = live_on_exit[*def];
                last_use[*def] = -1;
                live_on_exit[*def] = 0;
            }
            e = &events[event_count++];
            e->kind = EVENT_DEF;
            e->inst = instr - code;
            e->reg = *def;
            e->fixed = 0;
            assert(last_use[*def] == -1);
            assert(live_on_exit[*def] == 0);
        }
        exit_context = get_exit_context(instr, exits);
        if (exit_context) {
            char exit_live[256];
            memset(exit_live, 0, 256);
            _HotPyContext_LiveRegisters(exit_context, exit_live);
            if (COALESCE_DEBUG) {
                printf("Live on exit: ");
                for (i = 0; i < 256; i++) {
                    if (exit_live[i])
                        printf("%d ", i);
                }
                printf("\n");
            }
            /* Make last_use for *all* live registers */
            for (i = 0; i < 256; i++) {
                if (exit_live[i]) {
                    last_use[i] = instr - code;
                    live_on_exit[i] = 1;
                }
            }
        }
        instr = following_instr;
    }
    for (i = 0; i < 256; i++) {
        if (last_use[i] >= 0) {
            LivenessEvent *e;
            if (event_count >= allocated - 1) {
                LivenessEvent *new_events =
                    PyMem_Malloc(sizeof(LivenessEvent) * 2 * allocated);
                if (new_events == NULL) {
                    PyMem_Free(events);
                    return NULL;
                }
                memcpy(new_events, events, allocated * sizeof(LivenessEvent));
                allocated *= 2;
                PyMem_Free(events);
                events = new_events;
            }
            e = &events[event_count++];
            e->inst = last_use[i];
            e->reg = i;
            e->kind = EVENT_LAST_USE;
            e->fixed = live_on_exit[i];
        }
    }
    /* XXX This could be made faster by keeping uses separate from
     * EVENT_MOVEs/EVENT_DEFNs which are pre-sorted; sort uses then merge. */
    qsort(events, event_count, sizeof(LivenessEvent), liveness_compare);
    /* Run backwards through events propogating "fixedness" from LAST_USE
    *  to MOVE_SRC and DEFN */
    for (i = 0; i < 256; i++) {
        live_on_exit[i] = 0;
    }
    for (i = event_count-1; i >= 0; i--) {
        LivenessEvent *e = events + i;
        if (live_on_exit[e->reg])
            e->fixed = 1;
        if (e->kind == EVENT_DEF)
            live_on_exit[e->reg] = 0;
        else if (e->kind == EVENT_LAST_USE)
            live_on_exit[e->reg] = e->fixed;
    }
    *event_count_ptr = event_count;
    return events;
}

static void init_liverange(LiveRange *range, int last_use, unsigned char reg,
                           int fixed_reg)
{
    int i;
    range->def = -1;
    range->last_use = last_use;
    range->reg = reg;
    range->fixed_reg = fixed_reg;
    for (i = 0; i < VECTOR_WORDS; i++)
        range->interferes_with_fixed[i] = 0;
}

static int
range_compare(const void *p0, const void *p1)
{
    const LiveRange *l0 = p0;
    const LiveRange *l1 = p1;
    if (l0->def == l1->def)
        return 0;
    else if (l0->def < l1->def)
        return -1;
    else
        return 1;
}

static int
relabel_compare(const void *p0, const void *p1)
{
    const RelabelEvent *r0 = p0;
    const RelabelEvent *r1 = p1;
    if (r0->where == r1->where)
        return 0;
    else if (r0->where < r1->where)
        return -1;
    else
        return 1;
}

static void
init_relabel(RelabelEvent *r, int what, int as, int where)
{
    r->what = what;
    r->as = as;
    r->where = where;
}

static int forward_pass(Move *moves, int mcount, LivenessEvent *events,
                        int ecount, RelabelEvent *relabels)
{
    int i, rcount = 0;
    LivenessEvent *last_event = events-1;
    for (i = 0; i < mcount; i++) {
        Move *m = moves + i;
        LivenessEvent *e = last_event + 1;
        LivenessEvent *src, *dest, *end;
        while (e->inst < m->inst) e++;
        src = e++;
        assert(src->kind == EVENT_MOVE_SRC);
        if (e->kind == EVENT_LAST_USE)
            e++;
        dest = last_event = e++;
        assert(dest->kind == EVENT_DEF);
        if (src->fixed && !dest->fixed) {
            int success = 0;
            int live = 0;
            /* Attempt to coalesce */
            for (; e < events + ecount; e++) {
                if (e->kind == EVENT_DEF && e->reg == src->reg)
                    break; /* Failure */
                if (e->kind == EVENT_LAST_USE && e->reg == dest->reg) {
                    success = 1;
                    live = 1;
                    break;
                }
                if (e->kind == EVENT_DEF && e->reg == dest->reg) {
                    success = 1;
                    break;
                }
            }
            if (e == events + ecount) {
                /* Dead store */
                success = 1;
            }
            if (success) {
                if (live == 0) {
                    init_relabel(relabels + rcount++, dest->reg, src->reg, dest->inst);
                    init_relabel(relabels + rcount++, dest->reg, dest->reg, dest->inst+1);
                    dest->reg = src->reg;
                    assert(dest->fixed == 0);
                    dest->fixed = 1;
                }
                else {
                    int dest_reg = dest->reg;
                    init_relabel(relabels + rcount++, dest_reg, src->reg, dest->inst);
                    init_relabel(relabels + rcount++, dest_reg, dest_reg, e->inst);
                    end = e;
                    for (e = dest; e <= end; e++) {
                        if (e->reg == dest_reg) {
                            e->reg = src->reg;
                            assert(e->fixed == 0);
                            e->fixed = 1;
                        }
                    }
                }
            }
        }
    }
    return rcount;
}

static int backward_pass(Move *moves, int mcount, LivenessEvent *events,
                        int ecount, RelabelEvent *relabels)
{
    int i, rcount = 0;
    LivenessEvent *last_event = events + ecount;
    for (i = mcount - 1; i >= 0; i--) {
        Move *m = moves + i;
        LivenessEvent *e = last_event - 1;
        LivenessEvent *src, *dest, *end;
        while (e->inst > m->inst) e--;
        dest = e--;
        assert(dest->kind == EVENT_DEF);
        if (e->kind == EVENT_LAST_USE) {
            src = e--;
            assert(e->kind == EVENT_MOVE_SRC);
            if (!src->fixed) {
                int success = 0;
                /* Attempt to coalesce */
                for (; e >= events; e--) {
                    if (e->kind == EVENT_LAST_USE && e->reg == dest->reg)
                        break; /* Failure */
                    if (e->kind == EVENT_DEF) {
                        if (e->reg == src->reg) {
                            success = 1;
                            break;
                        }
                        if (e->reg == dest->reg) {
                            /* Dead store -- Implicit LAST_USE */
                            break; /* Failure */
                        }
                    }
                }
                if (success) {
                    int src_reg = src->reg;
                    init_relabel(relabels + rcount++, src_reg, dest->reg, e->inst);
                    init_relabel(relabels + rcount++, src_reg, src_reg, src->inst);
                    end = src;
                    for (; e <= end; e++) {
                        if (e->reg == src_reg) {
                            e->reg = dest->reg;
                            assert(e->fixed == 0);
                            e->fixed = 1;
                        }
                    }
                }
            }
        }
    }
    return rcount;
}

static int
doesnt_interfere(LiveRange *range, int fixed_reg)
{
    int word_index = fixed_reg / BITS_PER_LONG;
    int bit_index = fixed_reg & (BITS_PER_LONG-1);
    return (range->interferes_with_fixed[word_index] & (1 << bit_index)) == 0;
}

/* Iterate over liveness events to generate liveranges */
LiveRange *liveranges(LivenessEvent *events, int count,
                      int *range_count_ptr, uint32_t *code)
{
    int i, j;
    LiveRange *current_ranges[256];
    int range_count = 0;
    /* Number of ranges < number of events, so no need to resize */
    LiveRange *ranges = PyMem_Malloc(sizeof(LiveRange) * count);
    long live_fixed_vector[VECTOR_WORDS];
    for (i = 0; i < VECTOR_WORDS; i++) {
        live_fixed_vector[i] = 0;
    }
    for (i = 0; i < 256; i++) {
        current_ranges[i] = NULL;
    }
    for (i = count -1; i >= 0; i--) {
        LiveRange *r;
        LivenessEvent *e = &events[i];
        int word_index;
        int bit_index;
        switch(e->kind) {
        case EVENT_DEF:
            word_index = e->reg / BITS_PER_LONG;
            bit_index = e->reg & (BITS_PER_LONG-1);
            live_fixed_vector[word_index] &= ~(1 << bit_index);
            if (current_ranges[e->reg]) {
                r = current_ranges[e->reg];
                r->def = e->inst;
                current_ranges[e->reg] = NULL;
                if (r->fixed_reg >= 0) {
                    /* This is a fixed range; all interferes with all other ranges */
                    for (j = 0; j < 256; j++) {
                        if (current_ranges[j]) {
                            current_ranges[j]->interferes_with_fixed[word_index]
                                |= (1 << bit_index);
                        }
                    }
                }
                else {
                    /* Not a fixed range; interferes with all live fixed */
                    /* If defined by a move do not interfere with the source
                        of the move */
                    int no_interference = 0;
                    int src = -1;
                    if ((code[e->inst] & 255) == MOVE) {
                        src = (code[e->inst] >> 8) & 255;
                        assert(((code[e->inst] >> 16) & 255) == e->reg);
                        no_interference = doesnt_interfere(r, src);
                    }
                    for (j = 0; j < VECTOR_WORDS; j++)
                        r->interferes_with_fixed[j] |= live_fixed_vector[j];
                    if (no_interference) {
                        assert(src >= 0);
                        r->interferes_with_fixed[src/BITS_PER_LONG] &=
                            ~(1 << (src & (BITS_PER_LONG-1)));
                    }
                }
            }
            else {
                /* Dead store - Create a very short liverange */
                /* Don't interfere with anything */
                init_liverange(&ranges[range_count], e->inst, e->reg,
                               -1);
                ranges[range_count++].def = e->inst;
            }
            break;
        case EVENT_LAST_USE:
            current_ranges[e->reg] = &ranges[range_count];
            if (e->fixed) {
                word_index = e->reg / BITS_PER_LONG;
                bit_index = e->reg & (BITS_PER_LONG-1);
                live_fixed_vector[word_index] |= (1 << bit_index);
                init_liverange(&ranges[range_count++], e->inst, e->reg,
                           e->reg);
            }
            else {
                init_liverange(&ranges[range_count++], e->inst, e->reg,
                           -1);
            }
            break;
        case EVENT_MOVE_SRC:
            break;
        }
    }
    for (i = 0; i < 256; i++) {
        if (current_ranges[i]) {
            assert(current_ranges[i]->def == -1);
            /* From deferred state, do not relabel register. */
            current_ranges[i]->fixed_reg = current_ranges[i]->reg;
        }
    }

    *range_count_ptr = range_count;
    qsort(ranges, range_count, sizeof(LiveRange), range_compare);
    return ranges;
}

static int coalesce2(RelabelEvent *relabels, LivenessEvent *events,
                     int ecount, uint32_t *code, int count)
{
    int i, rcount;
    Move *moves = PyMem_Malloc(sizeof(Move) * ecount);
    int mcount = 0;
    /* Scan over code looking for moves. */
    for (i = 0; i < count; i++) {
        if ((code[i] & 255) == MOVE) {
            Move *m = &moves[mcount++];
            m->inst = i;
        }
    }
    rcount = forward_pass(moves, mcount, events, ecount, relabels);
    rcount += backward_pass(moves, mcount, events, ecount, relabels + rcount);
    //rcount += forward_pass(moves, mcount, events, ecount, relabels + rcount);
    //rcount += backward_pass(moves, mcount, events, ecount, relabels + rcount);
    qsort(relabels, rcount, sizeof(RelabelEvent), relabel_compare);
    return rcount;
}

static uint32_t *
relabel_uses(uint32_t *instr, unsigned char *relabel_uses_table)
{
    int op = instr[0] & 255;
    switch (op) {
    case LEAVE_FRAME: case POP_CALLER_BLOCK: case CLEAR_FRAME: case NOP:
    case POP_BLOCK: case POP_EXCEPT: case SETUP_EXCEPT_HANDLER:
    case STOP_ITERATION_OR_RERAISE: case RERAISE:
    case SAVE_EXC_STATE: case SWAP_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
        return instr + 1;
    case DELETE_FAST: case DELETE_DEREF:
        return relabel_uses_K(instr, relabel_uses_table);
    case SETUP_BLOCK:
        return relabel_uses_kkK(instr, relabel_uses_table);
    case MAKE_FRAME: case NEW_FRAME: case NEW_CLOSURE: case KW_DICT_MERGE:
        return relabel_uses_rrro(instr, relabel_uses_table);
    case GET_GLOBAL_AND_BUILTINS:
        return relabel_uses_oo(instr, relabel_uses_table);
    case UNARY_NOT: case AS_TUPLE: case MAKE_GENERATOR: case GEN_STARTED:
    case HAS_DICT: case GET_TYPE: case OVERRIDES_GENERIC_GETATTRIBUTE:
    case OVERRIDES_GENERIC_SETATTR: case ALLOCATE: case SIZE: case MOVE:
        return relabel_uses_ro(instr, relabel_uses_table);
    case STORE_MAP: case DESCRIPTOR_SET: case SET_IN_OBJECT_DICT:
        return relabel_uses_rrr(instr, relabel_uses_table);
    case ENTER_FRAME: case PUSH_REGISTER_TO_STACK: case STORE_LOCALS:
    case GEN_ENTER: case GEN_EXIT:
        return relabel_uses_r(instr, relabel_uses_table);
    case GEN_CLEAR:
        return relabel_uses_p(instr, relabel_uses_table);
    case CALL_WRAPPER_NEXT: case CALL_WRAPPER_UNARY:
    case CALL_CFUNC_1: case CALL_CMETH_0:
        return relabel_uses_roKE(instr, relabel_uses_table);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT:
        return relabel_uses_rroE(instr, relabel_uses_table);
    case ONE_ARG: case TRACE_DESCRIPTOR_GET:
        return relabel_uses_rroE(instr, relabel_uses_table);
     case TRACE_DESCRIPTOR_SET:
        return relabel_uses_rrrE(instr, relabel_uses_table);
    case CALL_CFUNC_0:
        return relabel_uses_oKE(instr, relabel_uses_table);
    case CALL_CFUNC_2: case CALL_CMETH_1:
        return relabel_uses_rroKE(instr, relabel_uses_table);
    case CALL_CMETH_2:
        return relabel_uses_rrrokE(instr, relabel_uses_table);
    case CALL_WRAPPER_BINARY:
        return relabel_uses_rroKE(instr, relabel_uses_table);
    case CALL_OBJECT:
        return relabel_uses_rrroE(instr, relabel_uses_table);
    case CALL_WRAPPER_TRINARY_PROC:
        return relabel_uses_rrrKE(instr, relabel_uses_table);
    case CALL_WRAPPER_BINARY_NULL_PROC:
        return relabel_uses_rrKE(instr, relabel_uses_table);
    case NO_ARGS:
        return relabel_uses_rrE(instr, relabel_uses_table);
    case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
        return relabel_uses_rrE(instr, relabel_uses_table);
    case ONE_PLUS_ARGS: case TWO_ARGS:
        return relabel_uses_rrooE(instr, relabel_uses_table);
    case THREE_ARGS:
        return relabel_uses_rroooE(instr, relabel_uses_table);
    case EXIT_IF_FALSE: case EXIT_IF_TRUE: case EXIT_IF_NOT_BOOL_OR_INT:
        return relabel_uses_rE(instr, relabel_uses_table);
    case ENSURE_TYPE: case ENSURE_VALUE: case POLY_EXIT:
    case POLY_TYPE_EXIT: case  GEN_CHECK_OR_EXIT:
        return relabel_uses_rKE(instr, relabel_uses_table);
    case ENSURE_SLOT:
        return relabel_uses_rKKE(instr, relabel_uses_table);
    case EXIT:
        return relabel_uses_E(instr, relabel_uses_table);
    case BINARY_OP:
        return relabel_uses_rrok(instr, relabel_uses_table);
    case BINARY_OP_KV: case BINARY_OP_VK:
        return relabel_uses_rokk(instr, relabel_uses_table);
    case SET_LASTI:
        return relabel_uses_K(instr, relabel_uses_table);
    case CONST_TO_REGISTER: case PICK: case LOAD_FAST: case BUILD_SET:
    case LOAD_DEREF: case BUILD_TUPLE: case BUILD_LIST: case BUILD_MAP:
    case LOAD_CLOSURE:
        return relabel_uses_oK(instr, relabel_uses_table);
    case STORE_FAST: case STORE_DEREF: case UNPACK_SEQUENCE:
        return relabel_uses_rK(instr, relabel_uses_table);
    case LIST_APPEND: case SET_ADD:
        return relabel_uses_rr(instr, relabel_uses_table);
    case TUPLE_SET_ITEM: case LIST_SET_ITEM:
    case STORE_SLOT: case MATERIALISE_FRAME:
        return relabel_uses_rrk(instr, relabel_uses_table);
    case LOAD_FROM_GLOBAL_AND_BUILTINS:
        return relabel_uses_rroK(instr, relabel_uses_table);
    case HAS_SPECIAL:
        return relabel_uses_roK(instr, relabel_uses_table);
    case BUILD_SLICE: case OVERRIDES:
        return relabel_uses_rrro(instr, relabel_uses_table);
    case DESCRIPTOR_GET: case GET_CLASS_ATTR: case HAS_CLASS_ATTR:
    case SUB_TYPE: case NEW_FUNCTION:
        return relabel_uses_rro(instr, relabel_uses_table);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST:
        return relabel_uses_rokkkE(instr, relabel_uses_table);
    case POP_STACK_TO_REGISTER:
        return relabel_uses_o(instr, relabel_uses_table);
    case LOAD_SLOT:
        return relabel_uses_rok(instr, relabel_uses_table);
    case LOAD_SLOT_EX: case LOAD_MEMBER:
        return relabel_uses_rokE(instr, relabel_uses_table);
    case STORE_TO_GLOBALS:
        return relabel_uses_rrK(instr, relabel_uses_table);
    case RETURN_VALUE:
        return relabel_uses_r(instr, relabel_uses_table);
    case SET_IN_OBJECT_DICT_CONST:
        return relabel_uses_rrkkk(instr, relabel_uses_table);
    case FAST_LOAD_GLOBAL:
        return relabel_uses_oKKK(instr, relabel_uses_table);
    default:
        fprintf(stderr, "Unexpected opcode: %s\n", _HotPy_Instruction_Names[op]);
        abort();
    }
}

static uint32_t *
relabel_defs(uint32_t *instr, unsigned char *relabel_defs_table)
{
    int op = instr[0] & 255;
    switch (op) {
    case LEAVE_FRAME: case POP_CALLER_BLOCK: case CLEAR_FRAME: case NOP:
    case POP_BLOCK: case POP_EXCEPT: case SETUP_EXCEPT_HANDLER:
    case STOP_ITERATION_OR_RERAISE: case RERAISE:
    case SAVE_EXC_STATE: case SWAP_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
        return instr + 1;
    case DELETE_FAST: case DELETE_DEREF:
        return relabel_defs_K(instr, relabel_defs_table);
    case SETUP_BLOCK:
        return relabel_defs_kkK(instr, relabel_defs_table);
    case MAKE_FRAME: case NEW_FRAME: case NEW_CLOSURE: case KW_DICT_MERGE:
        return relabel_defs_rrro(instr, relabel_defs_table);
    case GET_GLOBAL_AND_BUILTINS:
        return relabel_defs_oo(instr, relabel_defs_table);
    case UNARY_NOT: case AS_TUPLE: case MAKE_GENERATOR: case GEN_STARTED:
    case HAS_DICT: case GET_TYPE: case OVERRIDES_GENERIC_GETATTRIBUTE:
    case OVERRIDES_GENERIC_SETATTR: case ALLOCATE: case SIZE: case MOVE:
        return relabel_defs_ro(instr, relabel_defs_table);
    case STORE_MAP: case DESCRIPTOR_SET: case SET_IN_OBJECT_DICT:
        return relabel_defs_rrr(instr, relabel_defs_table);
    case ENTER_FRAME: case PUSH_REGISTER_TO_STACK: case STORE_LOCALS:
    case GEN_ENTER: case GEN_EXIT:
        return relabel_defs_r(instr, relabel_defs_table);
    case GEN_CLEAR:
        return relabel_defs_p(instr, relabel_defs_table);
    case CALL_WRAPPER_NEXT: case CALL_WRAPPER_UNARY:
    case CALL_CFUNC_1: case CALL_CMETH_0:
        return relabel_defs_roKE(instr, relabel_defs_table);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT:
        return relabel_defs_rroE(instr, relabel_defs_table);
    case ONE_ARG: case TRACE_DESCRIPTOR_GET:
        return relabel_defs_rroE(instr, relabel_defs_table);
    case TRACE_DESCRIPTOR_SET:
        return relabel_defs_rrrE(instr, relabel_defs_table);
    case CALL_CFUNC_0:
        return relabel_defs_oKE(instr, relabel_defs_table);
    case CALL_CFUNC_2: case CALL_CMETH_1:
        return relabel_defs_rroKE(instr, relabel_defs_table);
    case CALL_CMETH_2:
        return relabel_defs_rrrokE(instr, relabel_defs_table);
    case CALL_WRAPPER_BINARY:
        return relabel_defs_rroKE(instr, relabel_defs_table);
    case CALL_OBJECT:
        return relabel_defs_rrroE(instr, relabel_defs_table);
    case CALL_WRAPPER_TRINARY_PROC:
        return relabel_defs_rrrKE(instr, relabel_defs_table);
    case CALL_WRAPPER_BINARY_NULL_PROC:
        return relabel_defs_rrKE(instr, relabel_defs_table);
    case NO_ARGS:
        return relabel_defs_rrE(instr, relabel_defs_table);
    case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
        return relabel_defs_rrE(instr, relabel_defs_table);
    case ONE_PLUS_ARGS: case TWO_ARGS:
        return relabel_defs_rrooE(instr, relabel_defs_table);
    case THREE_ARGS:
        return relabel_defs_rroooE(instr, relabel_defs_table);
    case EXIT_IF_FALSE: case EXIT_IF_TRUE: case EXIT_IF_NOT_BOOL_OR_INT:
        return relabel_defs_rE(instr, relabel_defs_table);
    case ENSURE_TYPE: case ENSURE_VALUE: case POLY_EXIT:
    case POLY_TYPE_EXIT: case  GEN_CHECK_OR_EXIT:
        return relabel_defs_rKE(instr, relabel_defs_table);
    case ENSURE_SLOT:
        return relabel_defs_rKKE(instr, relabel_defs_table);
    case EXIT:
        return relabel_defs_E(instr, relabel_defs_table);
    case BINARY_OP:
        return relabel_defs_rrok(instr, relabel_defs_table);
    case BINARY_OP_KV: case BINARY_OP_VK:
        return relabel_defs_rokk(instr, relabel_defs_table);
    case SET_LASTI:
        return relabel_defs_K(instr, relabel_defs_table);
    case CONST_TO_REGISTER: case PICK: case LOAD_FAST: case BUILD_SET:
    case LOAD_DEREF: case BUILD_TUPLE: case BUILD_LIST: case BUILD_MAP:
    case LOAD_CLOSURE:
        return relabel_defs_oK(instr, relabel_defs_table);
    case STORE_FAST: case STORE_DEREF: case UNPACK_SEQUENCE:
        return relabel_defs_rK(instr, relabel_defs_table);
    case LIST_APPEND: case SET_ADD:
        return relabel_defs_rr(instr, relabel_defs_table);
    case TUPLE_SET_ITEM: case LIST_SET_ITEM:
    case STORE_SLOT: case MATERIALISE_FRAME:
        return relabel_defs_rrk(instr, relabel_defs_table);
    case LOAD_FROM_GLOBAL_AND_BUILTINS:
        return relabel_defs_rroK(instr, relabel_defs_table);
    case HAS_SPECIAL:
        return relabel_defs_roK(instr, relabel_defs_table);
    case BUILD_SLICE: case OVERRIDES:
        return relabel_defs_rrro(instr, relabel_defs_table);
    case DESCRIPTOR_GET: case GET_CLASS_ATTR: case HAS_CLASS_ATTR:
    case SUB_TYPE: case NEW_FUNCTION:
        return relabel_defs_rro(instr, relabel_defs_table);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST:
        return relabel_defs_rokkkE(instr, relabel_defs_table);
    case POP_STACK_TO_REGISTER:
        return relabel_defs_o(instr, relabel_defs_table);
    case LOAD_SLOT:
        return relabel_defs_rok(instr, relabel_defs_table);
    case LOAD_SLOT_EX: case LOAD_MEMBER:
        return relabel_defs_rokE(instr, relabel_defs_table);
    case STORE_TO_GLOBALS:
        return relabel_defs_rrK(instr, relabel_defs_table);
    case RETURN_VALUE:
        return relabel_defs_r(instr, relabel_defs_table);
    case SET_IN_OBJECT_DICT_CONST:
        return relabel_defs_rrkkk(instr, relabel_defs_table);
    case FAST_LOAD_GLOBAL:
        return relabel_defs_oKKK(instr, relabel_defs_table);
    default:
        fprintf(stderr, "Unexpected opcode: %s\n", _HotPy_Instruction_Names[op]);
        abort();
    }
}

static HotPyContext *
get_exit_context(uint32_t *instr, PyObject *exits)
{
    int op = instr[0] & 255;
    switch (op) {
    case LEAVE_FRAME: case POP_CALLER_BLOCK: case CLEAR_FRAME: case NOP:
    case POP_BLOCK: case POP_EXCEPT: case SETUP_EXCEPT_HANDLER:
    case STOP_ITERATION_OR_RERAISE: case RERAISE:
    case SAVE_EXC_STATE: case SWAP_EXC_STATE: case RESTORE_CLEAR_EXC_STATE:
        return NULL;
    case DELETE_FAST: case DELETE_DEREF:
        return get_exit_context_K(instr, exits);
    case SETUP_BLOCK:
        return get_exit_context_kkK(instr, exits);
    case MAKE_FRAME: case NEW_FRAME: case NEW_CLOSURE: case KW_DICT_MERGE:
        return get_exit_context_rrro(instr, exits);
    case GET_GLOBAL_AND_BUILTINS:
        return get_exit_context_oo(instr, exits);
    case UNARY_NOT: case AS_TUPLE: case MAKE_GENERATOR: case GEN_STARTED:
    case HAS_DICT: case GET_TYPE: case OVERRIDES_GENERIC_GETATTRIBUTE:
    case OVERRIDES_GENERIC_SETATTR: case ALLOCATE: case SIZE: case MOVE:
        return get_exit_context_ro(instr, exits);
    case STORE_MAP: case DESCRIPTOR_SET: case SET_IN_OBJECT_DICT:
        return get_exit_context_rrr(instr, exits);
    case ENTER_FRAME: case PUSH_REGISTER_TO_STACK: case STORE_LOCALS:
    case GEN_ENTER: case GEN_EXIT:
        return get_exit_context_r(instr, exits);
    case GEN_CLEAR:
        return get_exit_context_p(instr, exits);
    case CALL_WRAPPER_NEXT: case CALL_WRAPPER_UNARY:
    case CALL_CFUNC_1: case CALL_CMETH_0:
        return get_exit_context_roKE(instr, exits);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT:
        return get_exit_context_rroE(instr, exits);
    case ONE_ARG: case TRACE_DESCRIPTOR_GET:
        return get_exit_context_rroE(instr, exits);
    case TRACE_DESCRIPTOR_SET:
        return get_exit_context_rrrE(instr, exits);
    case CALL_CFUNC_0:
        return get_exit_context_oKE(instr, exits);
    case CALL_CFUNC_2: case CALL_CMETH_1:
        return get_exit_context_rroKE(instr, exits);
    case CALL_CMETH_2:
        return get_exit_context_rrrokE(instr, exits);
    case CALL_WRAPPER_BINARY:
        return get_exit_context_rroKE(instr, exits);
    case CALL_OBJECT:
        return get_exit_context_rrroE(instr, exits);
    case CALL_WRAPPER_TRINARY_PROC:
        return get_exit_context_rrrKE(instr, exits);
    case CALL_WRAPPER_BINARY_NULL_PROC:
        return get_exit_context_rrKE(instr, exits);
    case NO_ARGS:
        return get_exit_context_rrE(instr, exits);
    case EXIT_IF_VALUE_IN_OBJECT_DICT: case NO_KEYWORDS:
        return get_exit_context_rrE(instr, exits);
    case ONE_PLUS_ARGS: case TWO_ARGS:
        return get_exit_context_rrooE(instr, exits);
    case THREE_ARGS:
        return get_exit_context_rroooE(instr, exits);
    case EXIT_IF_FALSE: case EXIT_IF_TRUE: case EXIT_IF_NOT_BOOL_OR_INT:
        return get_exit_context_rE(instr, exits);
    case ENSURE_TYPE: case ENSURE_VALUE: case POLY_EXIT:
    case POLY_TYPE_EXIT: case  GEN_CHECK_OR_EXIT:
        return get_exit_context_rKE(instr, exits);
    case ENSURE_SLOT:
        return get_exit_context_rKKE(instr, exits);
    case EXIT:
        return get_exit_context_E(instr, exits);
    case BINARY_OP:
        return get_exit_context_rrok(instr, exits);
    case BINARY_OP_KV: case BINARY_OP_VK:
        return get_exit_context_rokk(instr, exits);
    case SET_LASTI:
        return get_exit_context_K(instr, exits);
    case CONST_TO_REGISTER: case PICK: case LOAD_FAST: case BUILD_SET:
    case LOAD_DEREF: case BUILD_TUPLE: case BUILD_LIST: case BUILD_MAP:
    case LOAD_CLOSURE:
        return get_exit_context_oK(instr, exits);
    case STORE_FAST: case STORE_DEREF: case UNPACK_SEQUENCE:
        return get_exit_context_rK(instr, exits);
    case LIST_APPEND: case SET_ADD:
        return get_exit_context_rr(instr, exits);
    case TUPLE_SET_ITEM: case LIST_SET_ITEM:
    case STORE_SLOT: case MATERIALISE_FRAME:
        return get_exit_context_rrk(instr, exits);
    case LOAD_FROM_GLOBAL_AND_BUILTINS:
        return get_exit_context_rroK(instr, exits);
    case HAS_SPECIAL:
        return get_exit_context_roK(instr, exits);
    case BUILD_SLICE: case OVERRIDES:
        return get_exit_context_rrro(instr, exits);
    case DESCRIPTOR_GET: case GET_CLASS_ATTR: case HAS_CLASS_ATTR:
    case SUB_TYPE: case NEW_FUNCTION:
        return get_exit_context_rro(instr, exits);
    case VALUE_FROM_OBJECT_DICT_OR_EXIT_CONST:
        return get_exit_context_rokkkE(instr, exits);
    case POP_STACK_TO_REGISTER:
        return get_exit_context_o(instr, exits);
    case LOAD_SLOT:
        return get_exit_context_rok(instr, exits);
    case LOAD_SLOT_EX: case LOAD_MEMBER:
        return get_exit_context_rokE(instr, exits);
    case STORE_TO_GLOBALS:
        return get_exit_context_rrK(instr, exits);
    case RETURN_VALUE:
        return get_exit_context_r(instr, exits);
    case SET_IN_OBJECT_DICT_CONST:
        return get_exit_context_rrkkk(instr, exits);
    case FAST_LOAD_GLOBAL:
        return get_exit_context_oKKK(instr, exits);
    default:
        fprintf(stderr, "Unexpected opcode: %s\n", _HotPy_Instruction_Names[op]);
        abort();
    }
}

static int relabel2(uint32_t *code, int count, RelabelEvent *ranges, int rcount)
{
    int i;
    unsigned char relabel_table[256];
    uint32_t *instr = code;
    for (i = 0; i < 256; i++)
        relabel_table[i] = i;
    for (i = 0; i < rcount;) {
        int next_relabel = ranges[i].where;
        while (instr < code + next_relabel) {
            relabel_uses(instr, relabel_table);
            instr = relabel_defs(instr, relabel_table);
        }
        assert(instr == code + next_relabel);
        relabel_uses(instr, relabel_table);
        while (i < rcount && ranges[i].where == next_relabel) {
            relabel_table[ranges[i].what] = ranges[i].as;
            i++;
        }
        instr = relabel_defs(instr, relabel_table);
    }
    while (instr < code + count) {
        relabel_uses(instr, relabel_table);
        instr = relabel_defs(instr, relabel_table);
    }
    return 0;
}

static void
print_liveness_events(LivenessEvent *events, int count)
{
    int i;
    for (i = 0; i < count; i++)
        print_liveness_event(events+i, stdout);
    printf("\n");
}

static int
coalesce_registers(uint32_t *code, int count, PyObject *exits)
{
    int result = -1;
    LivenessEvent *events;
    int event_count;
    int rcount;
    RelabelEvent *relabels = NULL;
    events = liveness_events(code, count, &event_count, exits);
    if (events == NULL)
        goto fail;
    if (COALESCE_DEBUG)
        print_liveness_events(events, event_count);
    relabels = PyMem_Malloc(sizeof(RelabelEvent) * event_count);
    if (relabels == NULL)
        goto fail;
    rcount = coalesce2(relabels, events, event_count, code, count);
    if (rcount < 0)
        goto fail;
    if (COALESCE_DEBUG) {
        printf("After coalescing:\n");
        print_relabels(relabels, rcount);
    }
    /* dump_relabelling(ranges, range_count); */
    if (relabel2(code, count, relabels, rcount))
        goto fail;
    result = 0;
fail:
    if (events)
        PyMem_Free(events);
    PyMem_Free(relabels);
    return result;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.