File: TRANS_TAB.ML

package info (click to toggle)
polyml 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 19,692 kB
  • ctags: 17,567
  • sloc: cpp: 37,221; sh: 9,591; asm: 4,120; ansic: 428; makefile: 203; ml: 191; awk: 91; sed: 10
file content (3942 lines) | stat: -rw-r--r-- 141,362 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
(*
	Copyright (c) 2000
		Cambridge University Technical Services Limited

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*)

(*
    Title:      Pseudo-stack Operations for Code Generator.
    Author:     Dave Matthews, Edinburgh University / Prolingua Ltd.
    Copyright   D.C.J. Matthews 1991
*)

(*
	Modified and improved.  David C.J. Matthews 2000-2001.
*)
(* This part of the code-generator deals with the pseudo-stack and the
  translation of addresses into stack offsets. *)

functor TRANS_TAB (

(*****************************************************************************)
(*                  CODECONS                                                 *)
(*****************************************************************************)
structure CODECONS :
sig
  type machineWord;
  type code;
  type reg;   (* Machine registers *)
  type addrs;
  type address;
  datatype storeWidth = STORE_WORD | STORE_BYTE
  
  val canShareRegs : bool; (* True if we have genuine 3-address instructions. *)
  
  val regNone:     reg;
  val regStackPtr: reg;
  val regClosure:  reg;
  
  val regs:    int;     (* No of registers. *)
  
  val regN:   int -> reg;
  val nReg:   reg -> int;
  
  val regEq:    reg * reg -> bool;
  val regNeq:   reg * reg -> bool;
  
  val regRepr: reg -> string;

  val genLoad:        int * reg * reg * code -> unit;
  val genStore:       reg * int * reg * storeWidth * reg * code -> unit;
  val isStoreI:       machineWord * storeWidth * bool -> bool;
  val genStoreI:      machineWord * int * reg * storeWidth * reg * code -> unit;
  val genLoadCoderef: code * reg * code -> unit;
  val genLoadPush:    int * reg * code -> unit;
  val genPush:        reg * code -> unit;
  val preferLoadPush: bool;
  val genStackOffset: reg * int * code -> unit;

  type instrs;
  
  val instrMove:    instrs;
  val instrPush:    instrs;

  val instrIsRI: instrs * machineWord -> bool; (* Is the immediate value ok? *)

  val genRR: instrs * reg * reg * reg * code -> unit;
  val genRI: instrs * reg * machineWord * reg * code -> unit;

  type tests;
  
  val isCompRI: tests * machineWord -> bool;
  
  val resetStack: int * code -> unit;  (* Set a pending reset *)
  val procName:   code -> string;      (* Name of the procedure. *)

  type labels; (* The source of a jump. *)

  val noJump: labels;
  
  val compareAndBranchRR: reg * reg * tests * code -> labels;
  val compareAndBranchRI: reg * machineWord * tests * code -> labels;

  val unconditionalBranch: code -> labels;

  val jumpback: addrs * bool * code -> unit; (* Backwards jump. *)

  val fixup: labels * code -> unit; (* Fix up a jump. *)

  type handlerLab;
  
  val loadHandlerAddress:  reg * code -> handlerLab;
  val fixupHandler: handlerLab * code -> unit;
  
  val ic : code -> addrs

  val codeAddress: code -> address option

  datatype callKinds =
		Recursive
	|	ConstantFun of machineWord * bool
	|	CodeFun of code
	|	FullCall
  
  val callFunction:       callKinds * code -> unit;
  val jumpToFunction:     callKinds * reg * code -> unit;

  val traceContext: code -> string;
end (* CODECONS *);

(*****************************************************************************)
(*                  DEBUG                                                    *)
(*****************************************************************************)
structure DEBUG :
sig
    (* Produce debugging output. *)
    val pstackTraceTag : bool Universal.tag
    val compilerOutputTag:      (string->unit) Universal.tag
    val getParameter :
       'a Universal.tag -> Universal.universal list -> 'a
end;

(*****************************************************************************)
(*                  MISC                                                     *)
(*****************************************************************************)
structure MISC :
sig
  exception InternalError of string;
  
  type 'a iter
   
   val Iter :
       { 
         continue: bool,
         value:    unit -> 'a,
         next:     unit -> 'a iter
       } -> 'a iter

  val for : 'a iter -> ('a -> 'b) -> unit;
  val revfoldIterator : ('a -> 'b -> 'b) -> 'b -> 'a iter -> 'b
end;

(*****************************************************************************)
(*                  ADDRESS                                                  *)
(*****************************************************************************)
structure ADDRESS :
sig
  type machineWord;
  type short = Word.word;
  type address;

  val isShort : 'a -> bool;
  val toShort : 'a  -> short;
  val toMachineWord:   'a     -> machineWord;
  val toAddress:   machineWord -> address;
  val loadWord:    address * short -> machineWord;
  val wordEq  : machineWord * machineWord -> bool;
  val wordSize: int
  val length:      address -> Word.word;
end;


(*****************************************************************************)
(*                  TRANSTAB sharing constraints                             *)
(*****************************************************************************)

sharing type
  ADDRESS.machineWord
= CODECONS.machineWord
) : 
   
(*****************************************************************************)
(*                  TRANSTAB export signature                                *)
(*****************************************************************************)
sig
  type machineWord;

  type ttab;
  type code;
  type reg;   (* Machine registers *)
  type tests;
  type instrs;
  type addrs;
  type savedState;
  type storeWidth;
  type regSet
  
  val ttabCreate: Universal.universal list -> ttab;
  
  (* Register allocation *)
  val getRegister:    ttab * code * reg -> unit;
  val getAnyRegister: ttab * code -> reg;
  val freeRegister:   ttab * reg -> unit;
  val addRegUse:      ttab * reg -> unit;
  val clearCache:     ttab -> unit;
  val removeRegistersFromCache: ttab * regSet -> unit;

  (* Stack handling *)
  type stackIndex;
  
  val noIndex: stackIndex;
  
  (* For debugging only - not used in "official" builds *)
  val printStack:   ttab -> string -> string -> unit
  
  (* Push entries *)
  val pushReg:      ttab * reg  -> stackIndex;
  val pushStack:    ttab * int  -> stackIndex;
  val pushConst:    ttab * machineWord -> stackIndex;
  val pushCodeRef:  ttab * code -> stackIndex;
  val pushNonLocal: ttab * ttab * stackIndex * (unit -> stackIndex) * code -> stackIndex;
  val pushAllBut:   ttab * code * ((stackIndex -> unit) -> unit) * regSet -> unit;
  val pushNonArguments: ttab * code * stackIndex list * regSet -> reg list;
  val pushAll:      ttab * code -> unit;
  val pushSpecificEntry: ttab * code * stackIndex -> unit;
  val incsp:        ttab -> stackIndex;
  val decsp:        ttab*int -> unit;
  val reserveStackSpace: ttab * code * int -> stackIndex;

  (* Code entries *)
  val loadEntry:         code * ttab * stackIndex * bool -> reg*stackIndex;
  val loadToSpecificReg: code * ttab * reg * stackIndex * bool -> stackIndex;
  val containsLocal:     ttab * reg  -> unit;
  val lockRegister:      ttab * reg -> unit;
  val unlockRegister:    ttab * reg -> unit;
  val loadIfArg:         code * ttab * stackIndex -> stackIndex
  val indirect:          int * stackIndex * code * ttab -> stackIndex;
  val moveToVec:         stackIndex * stackIndex * int * storeWidth * code * ttab -> unit;

  val removeStackEntry: ttab*stackIndex -> unit;

  val resetButReload:   code * ttab * int -> unit;
  val pushValueToStack: code * ttab * stackIndex * int -> stackIndex;
  val storeInStack:     code * ttab * stackIndex * int -> unit;
  val isProcB:          ttab * int -> bool;
  val realstackptr:     ttab -> int;
  val maxstack:         ttab -> int;
  val pstackForDec:     ttab * int -> stackIndex;
  val makeEntry:        ttab * code * stackIndex * int * int * bool -> unit;
  val incrUseCount:     ttab * stackIndex * int -> unit;
  
  type stackMark;
  
  val markStack: ttab -> stackMark;
  val unmarkStack: ttab * stackMark -> unit
  
  type labels;
  
  val noJump: labels;
  val isEmptyLabel: labels -> bool
  
  datatype mergeResult = NoMerge | MergeIndex of stackIndex;
 
  val unconditionalBranch: mergeResult * ttab * code -> labels;
  val jumpBack: addrs * ttab * code -> unit;

  val fixup: labels * ttab * code -> unit;
  val merge: labels * ttab * code * mergeResult * stackMark -> mergeResult;
  val mergeList: labels list * ttab * code * mergeResult * stackMark -> mergeResult;
  
  type handler;

  val pushAddress: ttab * code * int -> handler;
  val fixupH:      handler * int * ttab * code -> unit;

  val exiting: ttab -> unit;
  val haveExited: ttab -> bool

  datatype regHint = UseReg of reg | NoHint;
  val binaryOp: stackIndex * stackIndex * instrs * instrs * ttab * code * regHint -> stackIndex;
  val assignOp: stackIndex * stackIndex * stackIndex * storeWidth * ttab * code -> unit;

  val compareAndBranch: stackIndex * stackIndex * tests * tests * ttab * code -> labels;
  
  val saveState : ttab * code -> savedState
  val startCase : ttab * code * savedState -> addrs
  
  val chooseRegister : ttab -> reg

  val getRegisterSet: machineWord -> regSet
  val getRegisterSetForCode: code -> regSet
  val allRegisters : regSet
  val regSetUnion: regSet * regSet -> regSet
  val listToSet: reg list -> regSet

  val getFunctionRegSet: stackIndex * ttab -> regSet
  val addModifiedRegSet: ttab * regSet -> unit

  val getModifedRegSet: ttab -> reg list

  datatype argdest = ArgToRegister of reg | ArgToStack of int
  val getLoopDestinations: stackIndex list * ttab -> argdest list

  val callCode: stackIndex * bool * ttab * code -> unit
  val jumpToCode: stackIndex * bool * reg * ttab * code -> unit
end (* TRANSTAB export signature*) =

let

(*****************************************************************************)
(*                  ARRAY                                                    *)
(*****************************************************************************)
structure ARRAY :
sig
  type 'a array;

  val array  : int * '_a -> '_a array;
  val sub    : 'a array * int -> 'a;
  val update : 'a array * int * 'a -> unit;
end = Array;

(*****************************************************************************)
(*                  STRETCHARRAY                                             *)
(*****************************************************************************)
structure STRETCHARRAY :
sig
  type 'a stretchArray;

  val stretchArray  : int * '_a -> '_a stretchArray;
  val sub    : 'a stretchArray * int -> 'a;
  val update : '_a stretchArray * int * '_a -> unit;
end = StretchArray;

in

(*****************************************************************************)
(*                  TRANSTAB functor body                                    *)
(*****************************************************************************)
struct

  open CODECONS; infix 5 regEq regNeq;
  open DEBUG;
  open MISC;
  open ADDRESS;
  open ARRAY;

  datatype regHint = 
    UseReg of reg
  | NoHint;

  (* The set of registers modified by a function.  AllRegisters occurs
     so frequently that it's worth treating specially. *)
  abstype regSet = AllRegisters | SomeRegisters of reg list
  with
	  (* This is the set of all registers and is used as the default if
	     we are calling a non-constant function. *)
	  local
		  val listOfAllRegisters =
		  let
		  	 fun getRegs i l =
			 	if i < 0 then l else getRegs (i-1) (regN i :: l)
		  in
		  	 getRegs(regs-1) []
		  end
	
		  fun merge(a, []) = a
		   |  merge([], a) = a
		   |  merge(a as h::t, b as h'::t') =
		   		if h regEq h' then h :: merge(t, t')
				else if nReg h < nReg h'
				then h :: merge(t, b) else h' :: merge(a, t')
	  in
	  	  fun singleton r = SomeRegisters [r]
		  val allRegisters = AllRegisters
		  val noRegisters = SomeRegisters []

		  fun isAllRegs AllRegisters = true | isAllRegs _ = false

		  fun regSetUnion(AllRegisters, _) = AllRegisters
		    | regSetUnion(_, AllRegisters) = AllRegisters
			| regSetUnion(SomeRegisters a, SomeRegisters b) =
				let
					val merged = merge(a, b)
				in
					if List.length merged = regs
					then AllRegisters
					else SomeRegisters merged
				end
	
		  fun listToSet l =
		  	 let
			 	(* Sort and remove duplicates. *)
			 	val merged = List.foldl (fn(a,b) => merge([a], b)) [] l
			 in
			 	if List.length merged = regs
				then AllRegisters
				else SomeRegisters merged
			 end
	
		  fun setToList AllRegisters = listOfAllRegisters
		    | setToList (SomeRegisters r) = r
	
		  fun inverseSet AllRegisters = SomeRegisters []
		   |  inverseSet (SomeRegisters []) = AllRegisters
		   |  inverseSet (SomeRegisters r) =
		   		let
					fun diff (a as h::t, b as h'::t') =
						if h regEq h' then diff(t, t')
						else if nReg h < nReg h'
						then h :: diff(t, b)
						else diff(a, t')
					|	diff (a, _) = a
				in
					SomeRegisters(diff(listOfAllRegisters, r))
				end
	
		  fun inSet(r, AllRegisters) = true
		   |  inSet(r, SomeRegisters l) =
		   		let
					fun inset [] = false
					 |  inset (h::t) = h regEq r orelse inset t
				in
					inset l
				end
	  end
	end;
 

local
 
  val cFixup  = CODECONS.fixup;
  val cFixupH = CODECONS.fixupHandler;
  
  fun forLoop f i n = if i > n then () else (f i; forLoop f (i + 1) n);
  
  nonfix sub; (* was only 5 - gave subtle bugs (SPF 11/8/94 *)

  (* local definitions, for brevity *)
  type 'a stretchArray = 'a STRETCHARRAY.stretchArray;
  val stretchArray  = STRETCHARRAY.stretchArray;
  val stretchSub    = STRETCHARRAY.sub;
  val stretchUpdate = STRETCHARRAY.update;
  
(* ...
  type baseOffset = {base: reg, offset: int};
  
  fun base   ({base  ,...}:baseOffset) = base;
  fun offset ({offset,...}:baseOffset) = offset; 
  
  fun makeBaseOffset (base: reg) (offset: int) = {base = base, offset = offset};
... *)

  abstype stackIndex = StackIndex of int 
  with
   (* fixes off-by-one problem for array lookup *)
    fun getIndex (StackIndex n) = n;
    fun stackIndex n = StackIndex n;
    val first = stackIndex 0;
	val noIndex = stackIndex ~1; (* An invalid index. *)
  
    infix 4 indexEq indexNeq;
    
    fun (StackIndex a) indexEq  (StackIndex b) = a = b;
    fun (StackIndex a) indexNeq (StackIndex b) = a <> b;
  
    infix 5 indexGt indexGeq indexLt indexPlus indexMinus;
    
    fun (StackIndex a) indexGt  (StackIndex b) = a >  b;
    fun (StackIndex a) indexGeq (StackIndex b) = a >= b;
    fun (StackIndex a) indexLt  (StackIndex b) = a <  b;
    
    fun (StackIndex a) indexPlus  n = StackIndex (a + n);
    fun (StackIndex a) indexMinus n = StackIndex (a - n);
  end; 
  

  datatype stackUnion = 
    Register  of reg         (* In a register *)
  | Literal   of machineWord        (* A constant (was "int") *)
  | CodeRef   of code        (* Forward reference to code *)
  | Direct    of {base: reg, offset: int}  (* Register/Offset *)
  | Stack     of int         (* On the real stack. *)
  | Container of stackIndex list (* A group of stack entries. *)
                      
  fun isRegister (Register _) = true | isRegister _ = false;

  datatype stackEntry =
     NoStackEntry
   | StackEntry of 
      {
        ent:   stackUnion,
        cache: reg, (* regNone means not cached. *)
        uses:  int,
		destStack: int
			(* destStack is used to indicate where on the stack this
			   entry must be placed if it has to be spilled.  A value
			   of ~1 means that we don't care.  The reason for having
			   this is to ensure that if we split a flow of control
			   (e.g. the then- and else-parts of an "if") with a value
			   in a register and have to spill the register in one branch
			   then we spill it to the same location on the other branch.
			   This ensures that when we merge the flows of control we don't
			   have to mess around with the stack.  *)
      };
  
    fun makeStackEntry union reg i dest =
      StackEntry { ent = union, cache = reg, uses  = i, destStack = dest };

  val initTrans = 5; (* Initial size of tables. *)
  val initStack = 10;

  fun indDownto startInd finishInd =
  let
    fun prev n = Iter { continue = n indexGeq finishInd,
                        next     = fn () => prev (n indexMinus 1),
                        value    = fn () => n };
  in
    prev startInd
  end;

  (* The set of available registers. A register is free if its use-count
     is zero. A register has a reference for each occurence in an entry
     on the pseudo-stack or cache field (even if the use-count of the
     stack entry is zero). *)
  (* Added modification set to indicate if a register has been changed.
     We assume that if a register is allocated it has been modified.
	 DCJM 26/11/00. *)
     
  type rset = {vec: int array, nextr: int ref, modSet: regSet ref, freeRegs: int ref};

  fun Vec   ({vec  ,...}:rset) = vec;
  fun Nextr ({nextr,...}:rset) = nextr;
    
  val vecSize = regs;

  fun nextRegNo ({nextr,...}:rset) : int = !nextr;
  fun setNextRegNo ({nextr,...}:rset,n : int) : unit = nextr := n;
  
  (* Returns the first free register.  The start of the sequence is different
     each time to try to achieve a spread of usage. *)
  (* findFree seems to be biggest hot-spot in the whole compiler so we
     have to code it quite carefully. *)
  fun getAcceptableRegister ({vec,nextr,modSet,freeRegs}:rset, acceptable: regSet) =
  let
    val nextReg = ! nextr; 

	fun isAcceptable r = inSet(r, acceptable)		
    
    fun next n = if n = 0 then (vecSize - 1) else (n - 1);
	
	fun allocReg r =
	(* Mark the register as in use. *)
  	let
		val i = nReg r
	in
		update (vec, i, 1); (* Set the register use-count to 1 *)
		modSet := regSetUnion(singleton r, ! modSet); (* Mark as used *)
		(* Next time start looking at the register before this. *)
		nextr := next i;
		freeRegs := !freeRegs - 1;
		r
	end
    
    (* SPF 7/6/94 fixed off-by-one problem *)
	(* Pick a modified register if possible to keep the
	   set small. DCJM 26/11/00 *)
    fun findFree (i : int, firstChoice) : reg =
	let
		val useCount = sub (vec, i)
		and isModified = inSet(regN i, !modSet)
		and acceptable = isAcceptable(regN i)
	in
      if useCount = 0 andalso isModified andalso acceptable
	  then (* Free and it's been modified already - use it. *)
	  	 allocReg (regN i)
      else let (* Either it's in use or it hasn't been modified yet. *)
        val n = next i;
		val nextChoice =
			(* If we haven't found a register before and this is
			   free pick this one. *)
			if firstChoice regEq regNone andalso useCount = 0 andalso acceptable
			then regN i
			else firstChoice
      in
        if n = nextReg
		then (* None free or the only one we found was unmodified. *)
			if nextChoice regEq regNone then regNone (* Not found. *)
			else allocReg nextChoice
        else findFree(n, nextChoice)
      end
	end;

  in
  	 if !freeRegs = 0
	 then regNone
	 else findFree (nextReg, regNone)
  end;

  (* Get any register. *)
  fun getReg rset = getAcceptableRegister(rset, allRegisters)

  (* Print the allocated registers. *)
  fun printRegs printStream regs =
  let
    fun printReg i =
    let
      val useCount = sub (Vec regs, i)
    in
      if useCount > 0
      then
        (
          printStream " "; printStream (regRepr (regN i));
          printStream "="; printStream(Int.toString useCount);
          {}
        )
      else ()
    end (* printReg *);
  in
    forLoop printReg 0 (vecSize - 1)
  end (* printRegs *);


  fun getRset ({vec=v, modSet, freeRegs, ...}: rset) reg =
  let
    val r = nReg reg; (* SPF 7/6/94 - fixed off-by-one *)
    val useCount = sub (v, r)
  in
    if useCount = 0
	then
		(
		update (v, r, 1);
		modSet := regSetUnion(singleton reg, ! modSet);
		freeRegs := !freeRegs - 1
		)
    else raise InternalError ("getRset: can't get register #" ^ Int.toString r)
  end;

  fun free (regs as {freeRegs, ...}) reg =
  let
    val r = nReg reg; (* SPF 7/6/94 - fixed off-by-one *)
    val v = Vec regs;
    val useCount = sub (v, r)
  in
    if useCount = 0
    then raise InternalError ("free: register already free #" ^ Int.toString r)
    else
		(
		if useCount = 1
		then freeRegs := !freeRegs + 1
		else ();
		update (v, r, useCount - 1)
		)
  end;
  
  (* Increment use count. *)
  fun incr (regs as {freeRegs, ...}) reg =
  let
    val r = nReg reg; (* SPF 7/6/94 - fixed off-by-one *)
    val v = Vec regs;
    val useCount = sub (v, r)
  in
    if useCount = 0
	then freeRegs := !freeRegs - 1 else ();
    update (v, r, useCount + 1)
  end;

  (* SPF 7/6/94 - fixed off-by-one *)
  fun rsetMake () : rset =
     {vec = array (vecSize, 0),
	  nextr = ref (vecSize - 1),
	  modSet = ref noRegisters,
	  freeRegs = ref regs}

  fun usage regs reg =
  let
    val r = nReg reg; (* SPF 7/6/94 - fixed off-by-one *)
    val v = Vec regs;
    val useCount = sub (v, r)
  in
    useCount
  end;
  
 (* This table maps declaration numbers for a particular procedure or      *)
 (* inline procedure block into pseudo-stack offsets. The pseudo-stack     *)
 (* simulates the real stack and gives the real locations of objects       *)
 (* which may be in store, on the real stack or in registers. It maintains *)
 (* use-counts for values and allows the stack to be contracted and        *)
 (* registers to be re-used when they are no longer required.              *)
 datatype ttab = 
   Ttab of
     {
       decToPstack:  stackIndex stretchArray,
       isProc:       bool stretchArray,
       regset:       rset,
       pstack:       stackEntry stretchArray, (* Pseudo-stack *)
       pstackptr:    stackIndex ref,
       realstackptr: int ref,
       maxstack:     int ref,
       exited:       bool ref,
	   branched:	 bool ref,
       marker:       stackIndex ref,
       lowestDirect: stackIndex ref,
       pstackTrace:  bool,
       printStream:  string->unit
     };

  fun ttabCreate debugSwitches = 
    Ttab
      {
        decToPstack  = stretchArray (initTrans,noIndex),
        isProc       = stretchArray (initTrans,false),
        regset       = rsetMake(),
        pstack       = stretchArray (initStack,NoStackEntry),
        pstackptr    = ref first,
        realstackptr = ref 0,
        maxstack     = ref 1,
        exited       = ref false,
		branched	 = ref false,
        marker       = ref first,
        lowestDirect = ref first,
        pstackTrace  = DEBUG.getParameter DEBUG.pstackTraceTag debugSwitches,
        printStream    = DEBUG.getParameter DEBUG.compilerOutputTag debugSwitches
      };

  fun decToPstack  (Ttab {decToPstack ,...}) = decToPstack;
  fun isProc       (Ttab {isProc      ,...}) = isProc;
  fun regset       (Ttab {regset      ,...}) = regset;
  (*fun pstack       (Ttab {pstack      ,...}) = pstack;*)
  fun pstackptr    (Ttab {pstackptr   ,...}) = pstackptr;
  fun realstackptr (Ttab {realstackptr,...}) = realstackptr;
  fun maxstack     (Ttab {maxstack    ,...}) = maxstack;
  fun exited       (Ttab {exited      ,...}) = exited;
  fun branched	   (Ttab {branched    ,...}) = branched;
  fun marker       (Ttab {marker      ,...}) = marker;
  fun lowestDirect (Ttab {lowestDirect,...}) = lowestDirect;
  
  val pstackVal    = ! o pstackptr;
  val realstackVal = ! o realstackptr;
  val maxstackVal  = ! o maxstack;
  val exitedVal    = ! o exited;
  val branchedVal  = ! o branched;
  val markerVal    = ! o marker;

  fun pstackEntry (Ttab{pstack, ...}) (locn:stackIndex) : stackEntry = 
    stretchSub (pstack, getIndex locn);

  (* Returns the real entry from a chain of "copy" entries.
     Now that "copy" entries have been removed this just returns
	 the entry. *)
  fun pstackRealEntry (Ttab{pstack, ...}) (locn:stackIndex) =
      case (stretchSub (pstack, getIndex locn)) of
        NoStackEntry                    =>
           raise InternalError ("pstackRealEntry: not entry: " ^ Int.toString(getIndex locn))
           
      | StackEntry record => (locn, record)


   (* An iterator over the pseudo stack. Returns only entries which are not nil,
      but it will return entries whose use counts have gone to zero, but which
      still have useful values in their caches. *)
  fun overStack table downwards =
    if downwards
    then let
      (* The basis is an index to the stack *)
      fun next i =
      let
        val nxt = i - 1;
      in (* At the bottom ? *)
        if nxt < 0
        then nxt
        else 
          case (pstackEntry table (stackIndex nxt)) of
            NoStackEntry => 
              next nxt (* Empty entry - must continue *)
              
          | StackEntry _ =>
              nxt
      end;

      fun mkIter i =
        Iter
          {
             continue = i >= 0,
             value    = fn () => stackIndex i,
             next     = fn () => mkIter (next i)
          };
    in
      mkIter (next (getIndex (pstackVal table)))
    end

    else let (* upwards *)
      fun next i =
      let
        val nxt = i + 1;
      in 
        if nxt >= getIndex (pstackVal table)
          then nxt
        else 
          case (pstackEntry table (stackIndex nxt)) of
            NoStackEntry => 
              next nxt (* Empty entry - must continue *)
              
          | StackEntry _ =>
              nxt
      end;
  
      fun mkIter i = Iter { continue = i < getIndex (pstackVal table),
                            value    = fn () => stackIndex i,
                            next     = fn () => mkIter (next i) };
    in
      mkIter (next ~1)
    end (* overStack *);
   
   fun printStackUnion printStream stackun =
      case stackun of
        Register reg => 
          printStream(regRepr reg)
          
      | Literal w =>
          if isShort w
          then printStream(Int.toString (Word.toIntX (toShort w)))
          else printStream "?" (* ??? *)      
      
      | CodeRef si =>
          (
            printStream "(";
            printStream (procName si);
            printStream ")"
          )

     | Direct {base, offset} =>
         (
           printStream(regRepr base);
           printStream "@(";
           printStream(Int.toString offset);
           printStream ")"
         ) 

     | Stack i =>
        (
          printStream "base@(";
          printStream(Int.toString i);
          printStream ")"
        )

     | Container l =>
        (
          printStream "[";
          List.app (fn i => (printStream(Int.toString(getIndex i)); printStream " ")) l;
          printStream "]"
        )

  fun printEntry printStream NoStackEntry entry = ()
    | printEntry printStream (StackEntry {ent, uses, cache, destStack}) entry =
    (
      printStream(Int.toString(getIndex entry));
      printStream " ";
      
      printStream(Int.toString uses);
      printStream " ";
      
      printStackUnion printStream ent;
      
      if cache regNeq regNone
      then (printStream " in "; printStream(regRepr cache)) 
      else ();

	  if destStack = ~1 then ()
	  else (printStream " to base@("; printStream(Int.toString(destStack* ~wordSize)); printStream")");
        
      printStream "\n"
    );

  fun printStack (table as Ttab {printStream, ...}) why whereTo =
  ( 
    printStream ("\n" ^ why ^ " (" ^ whereTo ^ ")\n"); 
    printStream "psp=";
    printStream(Int.toString(getIndex (pstackVal table)));
    printStream " lim=";
    printStream(Int.toString(getIndex (markerVal table)));
    printStream " rsp="; 
    printStream (Int.toString(realstackVal table));
    printStream "\n";
    printStream "regs=";
    printRegs printStream (regset table);
    printStream "\n";
    for (overStack table true (* downwards *))
        (fn entry => printEntry printStream (pstackEntry table entry) entry)
  );
  
  (* Removes empty entries from the top of the stack. *)
  fun clearOff table =
  let
    val newIndex = (pstackVal table) indexMinus 1
  in
    if newIndex indexGeq (markerVal table)
     then
       case (pstackEntry table newIndex) of
         NoStackEntry => 
           (
             pstackptr table := newIndex;
             clearOff table 
           )
       | StackEntry _ => ()
     else ()
  end;  (* clearOff *)

  (* Removes an entry which is no longer required. If the entry is cached it *)
  (* may be retained unless it refers to the stack or another register when  *)
  (* it MUST be removed. *)
  fun removeEntry (table as Ttab{pstack, ...}) entry keepIfCache =
    case (pstackEntry table entry) of
      NoStackEntry => () 
       (* now handles this case, so we don't have to check 
          before calling "removeEntry" (SPF 19/11/94) *)
      
    | StackEntry {ent = stacken, cache, ...} =>
        (* If we are removing an entry from the real stack it must not be
           retained in the cache since we may push something else into that
           location. Actual parameters to procedures are not use-counted in
           the same way as locals so it is worth keeping them cached. *)
        let
          val cacheReg = 
            if cache regEq regNone
              then regNone
            else if not keepIfCache orelse 
                 (case stacken of
                    Register reg => true
                  | Stack i      => i < 0
                  | _            => false)
            
               then (* Clear cache. *)
                ( 
                  free (regset table) cache;
                  regNone
                )
            else (* Retain cache value. *) cache;
        in
          if cacheReg regEq regNone
          then (* If the cache is (now) empty we can remove the entry completely. *)
            ( 
             (* clobber the entry. *)
              stretchUpdate (pstack, getIndex entry, NoStackEntry);
              
              case stacken of
                Register reg       => free (regset table) reg
              | Direct {base, ...} => free (regset table) base
			  | Container l        => List.app (fn i => incrUseCount(table, i, ~1)) l
              | _                  => ()
              ;
              
                
              clearOff table
            )
         
          (* otherwise we just leave the entry there. *)
          else stretchUpdate (pstack, getIndex entry,
                       makeStackEntry stacken cacheReg 0 ~1)
        end (* end removeEntry *)

  (* Add the number of uses to the use count of an item on the stack. *)
  and incrUseCount (table as Ttab{pstack, ...}, entry, incr) : unit =
    case (pstackEntry table entry) of
      NoStackEntry => raise InternalError ("incrUseCount: no entry " ^ Int.toString(getIndex entry))
      
    | StackEntry {ent, cache, uses, destStack} =>
      let
        val newUses = uses + incr;
      in
        if newUses <=  0
        then removeEntry table entry true
        else 
          stretchUpdate (pstack, getIndex entry,
             makeStackEntry ent cache newUses destStack)
      end;

  (* True if this is the last reference to this entry. i.e. the use-count *)
  (* of this entry is 1. *)
  fun lastReference table entry =
    case (pstackEntry table entry) of
      NoStackEntry => raise InternalError "lastReference: no entry"
      
    | StackEntry {uses, ...} => uses = 1
    ;

  (* Push a value on the stack and return its location. *)
  fun pushPstack (table as Ttab{pstack, pstackTrace, ...}) entry name =
  let
    val stacktop = pstackVal table;
    val psp      = getIndex stacktop;
	val destStack =
		case entry of
			Stack addr => addr div (~wordSize)
		|	_ => ~1
  in
    stretchUpdate (pstack, psp, makeStackEntry entry regNone 1 destStack);
    pstackptr table := stacktop indexPlus 1;
    if pstackTrace then printStack table name "" else ();
    stacktop
  end;

in (* local definitions *)
  type stackIndex  = stackIndex;
  type ttab        = ttab;
  val ttabCreate   = ttabCreate;
  val noIndex      = noIndex;
  val incrUseCount = incrUseCount;
  val printStack   = printStack;

  (* Push a value onto the real stack. *)
  fun incsp table =
  let
    val stackaddr = realstackVal table * ~wordSize;
  in
    realstackptr table := realstackVal table + 1;
    
    if realstackVal table > maxstackVal table
    then maxstack table := realstackVal table
    else ();
    
    pushPstack table (Stack stackaddr) "incsp"
  end;

  (* The top of the pseudo-stack is held in a register *)
  fun pushReg (table, reg) : stackIndex = 
     pushPstack table (Register reg) "pushReg";

  (* The top of the pseudo-stack is a constant *)
  fun pushConst (table, v : machineWord) : stackIndex = 
    pushPstack table (Literal v) "pushConst";


  (* The top of the pseudo-stack is a forward reference to a procedure. *)
  fun pushCodeRef (table, rf : code) : stackIndex =
    pushPstack table (CodeRef rf) "pushCodeRef";

  fun addRegUse (table, reg) : unit = incr (regset table) reg;

  (* If we load a value into the last available register and then need to
     load another value (e.g. a second argument), it is possible for the
     first to be pushed onto the stack and the register to be re-used.  To
     avoid this we increment the use count on the first register before
     we attempt to load the second value.  This doesn't prevent the register
     being pushed onto the stack but it does prevent the register being
     reused. *)
   fun lockRegister (table as Ttab{pstackTrace, ...}, reg) = 
   let
     val U : unit = addRegUse (table, reg);
   in
     if pstackTrace then printStack table "lockRegister" "" else ()
   end;

   fun unlockRegister (table as Ttab{pstackTrace, ...}, reg) : unit =
   let
     val U : unit = free (regset table) reg;
   in
     if pstackTrace then printStack table "unlockRegister" "" else ()
   end;

  (* Puts a value in the real stack onto the pseudo-stack.
     Used for references to arguments that have not been
     passed in registers. *)
  fun pushStack (table, addr : int) : stackIndex =
  let (* Enter it only if it is not already there. *)
    fun search s =
      if s indexGeq pstackVal table
        then pushPstack table (Stack addr) "pushStack"
        
      else 
        case (pstackEntry table s) of
          StackEntry {ent = Stack index, ...} =>
            if index = addr
            then
              (
                incrUseCount (table, s, 1);
                s
              )
            else search (s indexPlus 1)
            
       | _ => search (s indexPlus 1)
  in
    search first
  end;

  (* Remove registers from the cache. *)
  fun removeFromCache (table as Ttab{pstack, ...}) regSet (continue: unit -> bool) =
  let
    fun ok r = inSet(r, regSet)

    fun clear (entry: stackIndex) (limit: stackIndex) =
      if entry indexLt limit
      then let
        val stackent = pstackEntry table entry;
      in
        case stackent of
          NoStackEntry => ()
          
        | StackEntry {ent = stacken, cache = cacheReg, uses, destStack} =>
          if cacheReg regEq regNone
          then ()
          else
            (* Remove the entry if the wanted register is the cache *)
            (* or the value being cached. *)
            if ok cacheReg orelse
              (case stacken of
                 Register reg      => ok reg
               | Direct {base,...} => ok base
               | _                 => false)
            then 
              (* If the use-count is positive, simply set the cache to *)
              (* empty, otherwise remove the entry. *)
              if uses = 0
              then removeEntry table entry false
              else
                ( 
                  free (regset table) cacheReg;
                  stretchUpdate (pstack, getIndex entry,
                        makeStackEntry stacken regNone uses destStack)
                )
            else ();
        
        if continue () then clear (entry indexPlus 1) limit else ()
      end
      else (); (* end clear *)
      
    val second = first indexPlus 1;
  in
   (* Do the constant pointer last.  If we are flushing the cache to free
      registers it's probably more use than the others. *)
   clear second (pstackVal table);
   if continue () then clear first second else ()
  end;

  (* Remove everything from the cache. *)
  fun clearCache table =
    removeFromCache table allRegisters (fn () => true);

  fun removeRegistersFromCache (table, regs) =
     removeFromCache table regs (fn () => true);

 
  (* The value on the stack is no longer required.
     This now just decrements the use count. *)
  fun removeStackEntry (table, index) =
  	incrUseCount(table, index, ~1)

  (* Reset the real stack stack pointer after a function call. *)
  fun decsp (table as Ttab{pstackTrace, ...}, args) = 
  ( realstackptr table := realstackVal table - args;
    if pstackTrace then printStack table "decsp" "" else () 
  );

  (* Returns the pstack offset for a particular declaration number. *)
  fun pstackForDec (table, locn) : stackIndex = 
    stretchSub (decToPstack table, locn);

  (* Called when a value has been newly created and so must be local. *)
  fun containsLocal (table, reg) : unit = ();

  (* Register to register move. *)
  fun moveRR rs rd cvec = genRR (instrMove, rs, regNone, rd, cvec);

  (* Frees registers by pushing values onto the stack or moving them to
     other registers.  `selectRegister' selects which registers are
     affected, `selectEntry' selects which entries are affected. Used
     either to clear all registers or just to free a particular one.
	 `loadIfPoss' is true if it is sufficient to move the entry to
	 another register. *)
  fun pushRegisters (table as Ttab{pstack, pstackTrace, ...}) cvec regSet selectEntry loadIfPoss =
  let
	
    fun pushEntries entry =
      if entry indexLt pstackVal table
      then let
        val stackent = pstackEntry table entry;
      in
        case stackent of
           StackEntry {uses, ent = Direct {base, offset}, cache = cacheReg,
		   			   destStack} =>
            (* Values which are cached but are otherwise not needed
               have a zero use-count. There is no need to push them.
               In fact, we have to explicitly clear the entry as
               otherwise getRegister could fail because the register
               could be locked by a zero-use pstack entry. SPF 26/9/95
             *)
			(* I don't understand this - we clear that register from the
			   cache - try uncommenting this.  DCJM 30/11/00 *)
			(* Was uncommenting this the cause of the bug in loadEntry
			   I fixed on 17/1/01?  If so the current code is better.
			   DCJM 25/1/01. *)
            if (* *) uses = 0 orelse (* *) not (selectEntry entry)
            then () 
            else let 
              (* Push reg onto the stack without changing the use count.*)
              fun saveDirectOnStack () =
			  	(
				  alignStack(table, cvec, destStack);

                  if cacheReg regNeq regNone
                  then genPush (cacheReg, cvec)
                  else genLoadPush (offset, base, cvec);
                  
                  (free (regset table) base) : unit;
                  
                  (* Overwrite this entry with a reference to the stack. *)
				  let
				  	 val stackAddr = realstackVal table
				  in
                     stretchUpdate (pstack, getIndex entry,
                        makeStackEntry (Stack(stackAddr * ~wordSize))
								cacheReg uses stackAddr)
				  end;
                            
                  realstackptr table := realstackVal table + 1;
                    
                  if realstackVal table > maxstackVal table
                  then maxstack table := realstackVal table
                  else ()
                );
              
              fun saveDirectInReg destReg =
                ( 
                  (free (regset table) base) : unit;
                  
                  if cacheReg regEq destReg
                    then () (* already cached in destination register *)
                  else if cacheReg regEq regNone
                    then genLoad (offset, base, destReg, cvec)
                    else ( (* Cached in a different register - move it there
							and free the cache. *)
						moveRR cacheReg destReg cvec;
						free (regset table) cacheReg
					);
                  
                  (* Clear out the cache and overwrite this entry with a
                      reference to the register. *)
                  stretchUpdate (pstack, getIndex entry,
                          makeStackEntry (Register destReg) regNone
						  	uses destStack)
                );
              
              fun discardDirect () =
                ( 
                  (free (regset table) base) : unit;
                  
                  if cacheReg regNeq regNone
                  then (free (regset table) cacheReg)
                  else ();

                  stretchUpdate (pstack, getIndex entry, NoStackEntry)
                );
            in
              if not (inSet(base, regSet))
                then ()
              else if uses = 0
                then (* discardDirect () *) removeEntry table entry false
              else if not loadIfPoss
			    (* Not allowed to move it to another register. *)
                then saveDirectOnStack ()
              else if cacheReg regNeq regNone andalso not (inSet(cacheReg, regSet))
			    (* It's cached in an acceptable register. *)
                then saveDirectInReg cacheReg
              else let
			    (* Is there an acceptable register free?  If so load it into
				   that. *)
                val destReg =
					getAcceptableRegister (regset table, inverseSet regSet);
              in
                if destReg regNeq regNone
                then saveDirectInReg destReg
                else saveDirectOnStack ()
              end
            end

        | StackEntry {uses, ent = Register reg, cache = cacheReg, destStack} =>
            if (* uses = 0 orelse *) not (selectEntry entry)
            then () 
            else let 
              (* Push reg onto the stack without changing the use count.*)
              fun saveRegOnStack () =
                ( 
				  alignStack(table, cvec, destStack);

                  genPush (reg, cvec) : unit;
                  
                  let
                    val newCache =
                      (* Have pushed a register - can treat the register as caching
                         the stack location we have pushed it into. *)
                      if cacheReg regEq regNone
                      then reg
                      else (free (regset table) reg; cacheReg);
					val stackAddr = realstackVal table
                  in
                    (* Overwrite this entry with a reference to the stack. *)
                    stretchUpdate (pstack, getIndex entry,
                            makeStackEntry (Stack(stackAddr * ~wordSize))
								newCache uses stackAddr);

                    realstackptr table := realstackVal table + 1;
                    
                    if realstackVal table > maxstackVal table
                    then maxstack table := realstackVal table
                    else ()
                  end
                );
              
              (* If we have any direct references using this register
                 we can adjust them to use the new register. This is
                 particularly important if we are moving values
                 out of this register because we want to load it with
                 something else. *)
              fun saveRegInReg destReg =
              let
                fun regChanged entry =
                  if entry indexLt pstackVal table
                  then
                    (
                      case (pstackEntry table entry) of
                        StackEntry {ent = Direct {base, offset},
										cache, uses, destStack} =>
                          if base regEq reg
                          then
                            ( (* Decrement the use count for the source reg 
                                 and increment it for the destination. *)
                              free (regset table) reg;
                              addRegUse (table, destReg);
                              stretchUpdate (pstack, getIndex entry, 
                                 makeStackEntry (Direct {base = destReg, offset = offset})
								 	cache uses destStack)
                            )
                          else ()
                        
                      | _ => ();
                      
                      regChanged (entry indexPlus 1)
                    )
                  else ();
              in
                free (regset table) reg : unit;
                
                if cacheReg regEq destReg
                then () (* already cached in destination register *)
                else moveRR reg destReg cvec;
                  
                regChanged entry; (* Start from this entry not from the bottom *)

                (* Clear out the cache and overwrite this entry with a
                   reference to the register. *)
                stretchUpdate (pstack, getIndex entry,
                     makeStackEntry (Register destReg) regNone uses destStack)

              end;
              
              fun discardReg () =
                ( 
                  (free (regset table) reg) : unit;
                  
                  if cacheReg regNeq regNone
                  then (free (regset table) cacheReg)
                  else ();

                  stretchUpdate (pstack, getIndex entry, NoStackEntry)
                );
            in
              if not (inSet(reg, regSet))
                then ()
              else if uses = 0
                then (* discardReg () *) removeEntry table entry false
              else if not loadIfPoss
                then saveRegOnStack ()
              else if cacheReg regNeq regNone andalso not (inSet(cacheReg, regSet))
                then saveRegInReg cacheReg
              else let
                val destReg =
					getAcceptableRegister (regset table, inverseSet regSet);
              in
                if destReg regNeq regNone
                then saveRegInReg destReg
                else saveRegOnStack ()
              end
            end (* let for saveReg etc. *)
            
        | _ => (); (* neither Direct nor Register *) 

        pushEntries (entry indexPlus 1)
      end (* let for stackent *)
      else () (* end pushEntries *);
  in
    pushEntries first;
	if pstackTrace then printStack table "pushRegisters" "" else ()
  end

  and pushAnyEntryAtCurrentSP(table, cvec): bool =
  (* Check that the next stack location is not the destination of an entry
     which has not yet been pushed and pushes it if it is. *)
  let
     val currentSp = realstackVal table
  	 fun selectEntry addr =
         case (pstackEntry table addr) of
           NoStackEntry => 
             raise InternalError "pushAnyEntryAtCurrentSP: no entry"
             
         | StackEntry {ent=Stack addr, ...} =>
		 	 (* Ok if already pushed.  Check that we don't have an entry
			    above the stack pointer. *)
		 	 if addr > currentSp
			 then raise InternalError "pushAnyEntryAtCurrentSP: entry above rsp"
			 else false

         | StackEntry {destStack, ...} =>
		 	 (* Consistency check to make sure that we haven't got an unpushed
			    entry below the current sp. *)
		 	 if destStack >= 0 andalso destStack < currentSp
			 then raise InternalError "pushAnyEntryAtCurrentSP: unpushed entry"
			 else destStack = currentSp (* Push it if we're there. *)
  in
  	 pushRegisters table cvec allRegisters (* Any register *) selectEntry false;
	 (* Return true if the stack pointer has changed. *)
	 realstackVal table <> currentSp
  end

  and alignStack (table, cvec, addr): unit =
	if addr < 0 (* Can use any offset. *)
	 (* There is a problem when we have gaps where we have
	    reserved addresses which are not consecutive.
		This can arise if we have something like:
		val a = ... val b = ...
		val c = if ...then ...(*X*)[push a]; [push b] a(last ref)
				else (if ...
					then (*Y*)[push b because we need its register}
					else (*Z*)[push a into the unused addr ???];
							  [push b to its explicit addr]...;
					a(last ref)
				)
		in ... b ... end.
		At X a and b are pushed and given explicit addresses but
		a is removed at the end of the branch.  At Y we've lost
		the explicit address for "a" so we have a gap.  What should
		we put in the gap?  We might be lucky and push a into it but
		what if we put something else in there?  All this is only a
		problem if, when we merge the states, we only try to push
		entries.  If we could store into the stack we'd be fine.
		We can store registers into the stack but not "direct"
		entries.
		
		For the moment, use the lowest value above the current sp
	    which is not currently reserved.  
	    DCJM 25/1/01.*)
	then
	 let
	 	fun minReserved s i =
		 	case pstackEntry table s of
				StackEntry {destStack, ...} => Int.max(destStack+1, i)
			|	_ => i
	  	val newAddr =
		 	revfoldIterator minReserved (realstackVal table)
				(indDownto ((pstackVal table) indexMinus 1) first)
	 in
	 	alignStack (table, cvec, newAddr)
	 end
	else (* We have an explicit offset *)
	 (
	 if addr < realstackVal table
	 then raise InternalError "pushRegisters: unpushed register"
	 else ();
	 if addr = realstackVal table
	 then () (* Got there. *)
	 else
	 	(
		(* If there is another entry for this address push it. *)
		if pushAnyEntryAtCurrentSP(table, cvec) then ()
		else (* Push any register simply to align the stack. *)
		 (
		 genPush (regN 0, cvec);
		 realstackptr table := realstackVal table + 1
		 );
		alignStack(table, cvec, addr) (* Keep going. *)
		)
	);


  (* Push all registers.  Now only used before a while-loop.  Also used
     when we have run out of registers.  That should be changed. *)
  fun pushAll (table, cvec) : unit =
    pushRegisters table cvec allRegisters (fn _ => true )(* all entries *) false;

  (* Push a specific entry.  This should really be incorporated into
     pushRegisters since at the moment it processes all the entries
	 and only selects the particular one.  *)
  fun pushSpecificEntry (table, cvec, entry) : unit =
    pushRegisters table cvec allRegisters (fn e => e indexEq entry ) false;

  (* Used particularly before procedure calls where we want to ensure
     that anything in a register is pushed onto the stack unless its
     last reference is in the call itself.  Also used before a handler. *)
  fun pushAllBut (table as Ttab{pstack, ...}, cvec, but, pushTheseRegs) : unit =
  let
    val useTab = stretchArray (initStack, 0);

	fun checkReg [] r = false
	  | checkReg (h::t) r = h regEq r orelse checkReg t r
  in
    but
     (fn addr => 
      let
        val (realAddr,_) = pstackRealEntry table addr
        val ind          = getIndex realAddr
      in
        stretchUpdate (useTab, ind,
           stretchSub (useTab, ind) + 1)
      end);
        
    pushRegisters table cvec
      (* registers that are modified *)
      pushTheseRegs
      (* entries with more uses than this *)
      (fn addr =>
         case (pstackEntry table addr) of
           NoStackEntry => 
             raise InternalError "pushAllBut: no entry"
             
         | StackEntry {uses, destStack, ent, cache} =>
		 	 if uses > stretchSub (useTab, getIndex addr)
			 then true
			 else
			 	(
				(* Set the destination stack for this entry to "undefined".
				   That's safe because we're going to remove this entry.
				   We do this because we may be about to push some arguments
				   or exception handlers and destStack may be in that area. *)
				if destStack <= 0 then ()
				else stretchUpdate (pstack, getIndex addr,
						makeStackEntry ent cache uses ~1);
				false
				)
       )
      false
  end;

  (* Ensures that all values which need to be preserved across a function
     call are pushed onto the stack or are in registers that will not
	 be modified. *)
  fun pushNonArguments (table as Ttab{pstack, ...}, cvec, args, pushTheseRegs) : reg list =
  let

	fun checkAddress [] addr = false
	  | checkAddress (h::t) addr = h indexEq addr orelse checkAddress t addr
	val onList = checkAddress args

	(* Get the list of registers which weren't pushed.  We need to lock
	   them so that they don't get pushed onto the stack while we are
	   pushing the arguments.  Actually I'm not sure this achieves what
	   we want. *)
	fun getRegisterList entry regs =
	  if entry indexLt pstackVal table
      then if onList entry (* Is it an argument? *)
	  then (* Ignore this. *) getRegisterList (entry indexPlus 1) regs
	  else let
        val stackent = pstackEntry table entry
		val nextRegs = 
	        case stackent of
	           StackEntry {uses, ent = Direct {base, ...}, ...} =>
			   	   if uses = 0 then regs
				   else (lockRegister(table, base); base::regs)
	        |  StackEntry {uses, ent = Register reg, ...} =>
			   	   if uses = 0 then regs
				   else (lockRegister(table, reg); reg::regs)
	        | _ => (* neither Direct nor Register *) regs
      in
	  	getRegisterList (entry indexPlus 1) nextRegs
	  end
	  else regs

  in
    pushRegisters table cvec
      (* registers that are modified *)
      pushTheseRegs
      (* Ignore entries corresponding to the arguments but only if they
	     have a use count of exactly one, *)
      (fn addr =>
         case (pstackEntry table addr) of
           NoStackEntry => 
             raise InternalError "pushNonArguments: no entry"
             
         | StackEntry {uses, destStack, ent, cache} =>
		 	 if uses > 1 orelse not (onList addr)
			 then true (* Must push it now if the register is modified. *)
			 else (* Don't need to save it because it's an argument. *)
			 	(
				(* Set the destination stack for this entry to "undefined".
				   That's safe because we're going to remove this entry.
				   We do this because we may be about to push some arguments
				   and destStack may be in that area.
				   There may not be the same need for this as in pushAllBut
				   but it shouldn't do any harm. *)
				if destStack <= 0 then ()
				else stretchUpdate (pstack, getIndex addr,
						makeStackEntry ent cache uses ~1);
				false
				)
       )
	  (* If all the registers must be pushed there's no point in trying to
	     move to another register. *)
      (not(isAllRegs pushTheseRegs));
	  getRegisterList first []
  end;


(* bugfixed makeEntry added 30/3/95 *)
  fun makeEntry (table as Ttab{pstackTrace, printStream, ...}, cvec : code, index, locn, use, isP) : unit =
  ( 
    stretchUpdate (isProc table, locn, isP);
    stretchUpdate (decToPstack table, locn, index);
    
   (* There is a bug which can happen if we make a reference to a copy entry
      which refers to a register.  If the value is used in a function call
      but also used after it, it is possible that pushAllBut will fail
      to save it because the use-count has been incremented on the copy
      entry but NOT on the register entry. This happens very rarely, so
      to avoid it, we explicitly push the register in that case. 
      
      Extra comment (25/9/95):
      This appears also to happen if we have a direct entry, 
      so we also save it in that case as well.
      
      What we really need is a complete rewrite of this module - 
      it uses the wrong data-structures. SPF 25/9/95.
	  
	  DCJM 26/11/99. Copy entries have been removed, so this is
	  no longer required.
    *)
    (* Add the number of uses (less one since the use-count will
       normally be one) to the use count. *)
    incrUseCount (table, index, use - 1);
	if pstackTrace
	then
	   (
	    printStream "makeEntry: index=";
		printStream(Int.toString(getIndex index));
		printStream " locn=";
		printStream(Int.toString locn);
		printStream " use=";
		printStream(Int.toString use);
		printStream "\n"
		)
	else ()
  );

  type savedState =
    {
      pStackPtr: stackIndex,
      realStackPtr: int,
      pStack: stackEntry stretchArray,
      nextRegNo : int,
	  context: string
    };
  
  fun pStackPtr    ({pStackPtr   ,...}:savedState) = pStackPtr;
  fun realStackPtr ({realStackPtr,...}:savedState) = realStackPtr;
  fun pStack       ({pStack      ,...}:savedState) = pStack;
 
  (* compare with the similar functions for ttabs!!! *)
  fun pStackEntry (table : savedState) (locn:stackIndex) : stackEntry = 
    stretchSub (pStack table, getIndex locn);
  
  fun printState printStream (save: savedState as {pStackPtr, realStackPtr, context, ... }) name =
  ( printStream name;
    printStream "\n";
	printStream "context="; printStream context;
    printStream " psp=";
    printStream(Int.toString(getIndex pStackPtr));
    printStream " rsp=";
    printStream(Int.toString realStackPtr);
    printStream "\n";
    for (indDownto pStackPtr first)
        (fn entry => printEntry printStream (pStackEntry save entry) entry)
  );

  (* Returns the real entry from a chain of "copy" entries. 
     Now that "copy" entries have been removed this just returns
	 the entry. *)
  fun pStackRealEntry (table:savedState) (locn:stackIndex) =
  let
    val pstack = pStack table
  in
      case (stretchSub (pstack, getIndex locn)) of
        NoStackEntry =>
          raise InternalError "pStackRealEntry: no entry"
          
      | StackEntry record => (locn,record)
  end;


  (* Sets the pseudo stack into a state to which it can be restored later.
     It is used when there are conditional branches to ensure that the state
     is the same if the branch falls through or is taken. *)
  fun saveState (table as Ttab{pstackTrace, printStream, ...}, cvec) : savedState =
  let
    val maxIndex  = getIndex (pstackVal table);
    val saveStack = stretchArray (maxIndex, NoStackEntry);
     
    fun copyState s =   (* copy the state. *)
    ( if s indexLt pstackVal table
      then
      ( stretchUpdate (saveStack, getIndex s, pstackEntry table s);
        copyState (s indexPlus 1)
      )
      else ()
    );
    
	val state =
	    { 
	      pStackPtr    = pstackVal table, 
	      realStackPtr = realstackVal table,
	      pStack       = saveStack,
	      nextRegNo    = nextRegNo (regset table),
		  context	   = traceContext cvec
	    }
  in
    copyState first;
	if pstackTrace then printState printStream state "saveState" else ();
  	state
  end;

  (* Tries first of all, then pushes all the registers and tries again. *)
  fun getAnyRegister (table as Ttab{pstackTrace, ...}, cvec) : reg =
  (let 
    (* First see if there is one free and grab that. *)
    val r = getReg (regset table);
    val r =
      if r regNeq regNone then r
      else let
	(* We seem to have run out.  First flush the cache, then if that
	   fails push all the registers. On the whole it seems to work better
	   to free a lot of registers rather then just freeing a few. We tend
	   to either have more than enough registers, or much too few. *) 
	fun untilSomethingFree () : bool =
	let
	  val rs = regset table;
	  val r = getReg rs;
	  val U : unit = 
	    if r regNeq regNone
	    then free rs r
	    else ()
	in
	  r regEq regNone
	end;
  
	val U : unit = removeFromCache table allRegisters untilSomethingFree;
	val r = getReg (regset table);
      in
	  if r regNeq regNone then r
	  else let
	    val U : unit = pushAll (table, cvec);
	    
	    (* Pushed values stay in the cache. *)
	    val U : unit = 
	      removeFromCache table allRegisters untilSomethingFree;
	    val r = getReg (regset table);
	  in (* If we still haven't found anything we are in big trouble. *)
	    if r regNeq regNone then r
	     else raise InternalError "No free registers"
	  end
      end
  in 
    if pstackTrace
    then printStack table "getAnyRegister" (traceContext cvec )
    else ();
    r
  end) handle SML90.Interrupt => raise SML90.Interrupt
  		 | exn => (printStack table "getAnyRegister" (traceContext cvec); raise exn);
		 (* getAnyRegister *)


  (* Resets the stack to the value given by removing any entries with
     non-zero use counts above it. This is fairly rare so does not have
     to be particularly efficient. Assumes that there are enough data
     registers to hold all the values. *)
  (* We use the stack for saving values, for function parameters and for
     handler entries.  Function parameters and handler entries have specific
	 formats with multiple words which must be contiguous.  If we have to
	 spill a register after, say, pushing one parameter and while computing
	 another, we must reload any spilled values and set the real stack pointer
	 correctly before continuing. *)
  fun resetButReload (cvec, table as Ttab{pstack, pstackTrace, ...}, stackOffset) : unit =
  let
    val oldSp = realstackVal table;
    
    (* Load any values above "stackOffset". *)
    fun loadEntries entry =
      if entry indexLt first
      then ()
      else let
        val stackent = pstackEntry table entry;
      in
        case stackent of
          StackEntry {ent = Stack addr, cache, uses, ...} =>
            if addr < ((stackOffset - 1) * ~wordSize) (* Above the limit on the stack. *)
            then let
               (* Load it without changing the use count. *)
              val reg = 
                if cache regNeq regNone
                then cache
                else let
                  val reg      = getAnyRegister (table, cvec);
                  val stackPtr = (realstackVal table - 1) * ~wordSize;
                in
                  genLoad (addr - stackPtr, regStackPtr, reg, cvec);
                  reg
                end;
            in (* Clear out the cache and overwrite this entry with a 
                  reference to the register. *)
              stretchUpdate (pstack, getIndex entry,
                 makeStackEntry (Register reg) regNone uses ~1)
            end
            else ()
            
          | _ => ();
      
        loadEntries (entry indexMinus 1)
      end (* loadEntries *);
  in
    loadEntries ((pstackVal table) indexMinus 1);
    
     (* If the real stack ptr has changed we must have pushed something,
        so our check has been useless. *)
     if  realstackVal table <> oldSp
     then raise InternalError "resetButReload: must have pushed something more"
     else ();

    (* Now reset the stack pointer. *)
    resetStack (realstackVal table - stackOffset, cvec);
    realstackptr table := stackOffset;
    
    if pstackTrace then printStack table "resetButReload" (traceContext cvec) else ()
  end;

  fun getRegister (table as Ttab{pstackTrace, ...}, cvec, reg) : unit =
  let (* Get it out of wherever it is being used. *)
    fun inUse ()  = usage (regset table) reg > 0
	val thisReg = singleton reg
  in
    pushRegisters table cvec thisReg (fn _ => true) (* any entry *) true;
    (* Clear it out of the cache (if it is there) *)
    removeFromCache table thisReg inUse;
    (* Should now be free. *)
    getRset (regset table) reg;
    
    if pstackTrace then printStack table "getRegister" (traceContext cvec) else ()
  end;

  fun freeRegister (table, reg) : unit = free (regset table) reg;

  fun chooseRegister (table : ttab) : reg =
  let
    fun chooseReg ({vec,nextr, ...}:rset) =
    let
      val nextReg = ! nextr;
      
      fun next n = if n = 0 then (vecSize - 1) else (n - 1);
      
      (* SPF 7/6/94 fixed off-by-one problem *)
      fun findFree (i : int) : reg =
	if sub (vec, i) = 0
	then
	  (
	    (* Next time start looking at the register before this. *)
	    nextr := next i; (* Do we really want to do this? *)
	    regN i
	  )
	else let
	  val n = next i;
	in
	  if n = nextReg then (* None free. *) regNone
	  else findFree n
	end;
    in
      findFree nextReg
    end;
  in
    chooseReg (regset table)
  end

  (* Return the set of modified registers for this function. *)
  fun getModifedRegSet (Ttab{regset={modSet=ref modSet, ...}, ...}) : reg list =
  let
  	  fun getRegs(i, rset) =
	  	 if i < 0 then rset
	  	 else if inSet(regN i, modSet) then getRegs(i-1, regN i :: rset)
		 else getRegs(i-1, rset)
  in
  	  getRegs(regs-1, [])
  end

  (* Add a set of registers to those modified by this function.
     This will be the set of registers modified by a function
	 called by this one. *)
  fun addModifiedRegSet (transtable: ttab, regs: regSet): unit =
  let
	  val Ttab{regset={modSet, ...}, ...} = transtable
  in
  	  modSet := regSetUnion(!modSet, regs)
  end

  (* Generates code for an entry on the pseudo-stack. *)
  (* Moves the entry (at locn) into destReg, decrementing the
     use-count for entry. Doesn't push anything new on the pstack. *)
  fun loadPstackEntry (table as Ttab{pstackTrace, ...}) locn (* Offset on the stack *) destReg cvec =
    let
      val (realLoc, {cache = cacheReg, ent, ...}) = pstackRealEntry table locn
    in
      if cacheReg regNeq regNone
      then
        (
         if cacheReg regNeq destReg
         then moveRR cacheReg destReg cvec
         else ()
        )
      else
        case ent of
          Register reg =>
            if reg regNeq destReg
            then moveRR reg destReg cvec
            else ()
          
        | Literal lit =>
            genRI (instrMove, regNone, lit, destReg, cvec)

        | CodeRef code =>
              genLoadCoderef (code, destReg, cvec)

        | Direct {base, offset} =>
            genLoad (offset, base, destReg, cvec)

        | Stack index =>
          let
            val stackPtr = (realstackVal table - 1) * ~wordSize;
          in
            genLoad (index - stackPtr, regStackPtr, destReg, cvec)
          end
    
        | Container l =>
		  (* The first entry in the container gives us the address. *)
		  	case pstackRealEntry table (hd l) of
				(_, {ent = Stack index, ...}) =>
		          let
		            val stackPtr = (realstackVal table - 1) * ~wordSize;
		          in
		            genStackOffset (destReg, index - stackPtr, cvec)
		          end
			|	_ => raise InternalError "loadPstackEntry: container entry is not on stack";
            

       (* Decrement use count and remove if done. *)
       incrUseCount (table, locn, ~1);

       if pstackTrace then printStack table "loadPstackEntry" (traceContext cvec) else ()
    end (* loadPstackEntry *) 

  (* Pushes a new pstack entry; loads value into register;
     decrements the use count of old pstack entry.
  *)
  and loadEntry (cvec, table as Ttab{pstack, ...}, entry, willTrample) : reg*stackIndex =
  let
    val (realLoc, {ent = stackEntry, cache = cacheReg, uses, destStack}) =
      pstackRealEntry table entry;
      
    (* If we find an entry in the cache or already in a register we can use
       it provided it will not be modified or this is its last use. Otherwise
       we must make a copy of it. *)
    val lastRef = lastReference table entry;
    
   fun useCacheRegister () : reg*stackIndex =
     let
       (* The value is being cached and we can safely use the register. *)
       (* Get the register, increment its use count and put it on the stack *)
       (* If we are going to trample on the register we must remove it
          from the cache. If this is the last real reference that will
          not matter, but if this is actually a reference to a parameter
          which could be loaded onto the stack again we have to be careful
          that the cache does not indicate a register which has been changed. *)
	   val U: unit = 
	       if willTrample
	       then stretchUpdate (pstack, getIndex realLoc,
	                  makeStackEntry stackEntry regNone uses destStack)
	       else addRegUse (table, cacheReg);
     
       val newEntry = pushReg (table, cacheReg)
	 in
     
       (* Must decrement the use-count of the entry we are loading as though
          we had actually loaded it. *)
       incrUseCount (table, entry, ~1);
       (cacheReg, newEntry)
     end
    
    fun useNewRegister () : reg*stackIndex =
    let
      (* It is loaded into a register. This is complicated because we want
         to put entries into the cache if we can. They must not be put into
         the cache until after they have been loaded otherwise the load
         instruction will simply copy the new cache value. It is possible
         that a value might be cached in a data register when it is needed
         in an address register or vice-versa. *)
      val resultReg = getAnyRegister (table, cvec);
    in 
      (* Get the entry again - getAnyRegister could have forced the
         entry onto the stack if it had run out of registers. *)
      case (pstackEntry table realLoc) of
        NoStackEntry => raise InternalError "loadEntry: entry deleted"

      | StackEntry {ent, uses, cache, destStack} =>
         let
           (* If the value is already cached, keep it in the old
              cache register, rather than the new one. This should
              help to minimise register-register moves when we have
              to merge branches. SPF 5/6/95 *)
		   val cacheIt =
		     not willTrample andalso cache regEq regNone andalso
	       case ent of
	          Direct   _    => true
	     (* | Literal   _   => true  *) (* constants are not cached *)
	       (* Cannot cache it if we are about to pop it. *)
	       | Stack index   => (0 <= index orelse not lastRef)
	       | _             => false
            (* If we are going to cache it we musn't let it be removed. *)
			val U: unit = 
	            if cacheIt
	            then incrUseCount (table, entry, 1) 
	            else ();
            
            val U: unit = loadPstackEntry table entry resultReg cvec;
            val newEntry = pushReg (table, resultReg);
      
         in
            if cacheIt
            then
              (
                (* First remove any register already in the cache.
                   This should now be a no-op. SPF 5/6/95 *)
                if cache regNeq regNone
                then freeRegister (table, cache)
                else ();
                
                (* put in the cache and restore use-count. *)
                stretchUpdate (pstack, getIndex realLoc,
                        makeStackEntry ent resultReg uses destStack);
                        
                addRegUse (table, resultReg);
                
                incrUseCount (table, entry, ~1)
              ) 
            else ();
            
            (resultReg, newEntry)
         end 
    end; (* useNewRegister *)
    
  in
    case stackEntry of
      Register reg =>
        if not willTrample
        then (reg, entry)

		else if lastRef
		then
			(
			(* We are going to trample on it but this is the last reference
			   so we can use it. It may, though, be caching a value so
			   we must remove it from the cache before we return it.
			   DCJM 17/1/01. *)
			removeRegistersFromCache(table, listToSet [reg]);
			(reg, entry)
			)
        
        else if cacheReg regNeq regNone (* Should not happen now. *)
          then useCacheRegister ()

        else useNewRegister () (* Must copy it. *)
        
    | _ =>
        if cacheReg regNeq regNone
          then useCacheRegister ()
        else useNewRegister ()  (* Not in a register. *)
  end;

  (* Load a value into a specific register.
     Used for loading argument regs etc.
     Pushes a new entry onto the pstack (why?). *)
  fun loadToSpecificReg (cvec, table, reg, entry, needExclusive) : stackIndex =
  let
    val (_, {ent = stackEntry, cache = cacheReg, ...})
        = pstackRealEntry table entry;
        
    val lastRef  = lastReference table entry;
  in
    if (case stackEntry of Register r => r regEq reg | _ => false)
    then (* It's already in the right register. *)
      if needExclusive andalso not lastRef
      (* The value is in the register but we are going to 
         change it - have to push the previous contents. *)
      then
        ( 
          getRegister (table, cvec, reg);
          let
            val newEntry : stackIndex = pushReg (table, reg);
          in
            incrUseCount (table, entry, ~1);
			newEntry
          end
        )
      
      (* Already there - use that entry. *)
      else entry

    else if cacheReg regEq reg
    then (* The register we want is caching the value - use it. *)
      ( 
        (* Get the register, increment its use count and put it on the stack *)
        if needExclusive andalso not lastRef
        then getRegister (table, cvec, reg)
        else addRegUse (table, reg);
      
        let
          val newEntry : stackIndex = pushReg (table, reg);
        in
           (* Must decrement the use-count of the entry we are loading
              as though we had actually loaded it. *)
          incrUseCount (table, entry, ~1);
		  newEntry
        end
      )

    else 
      ( (* Not in the register or must copy it. *)
      (* This entry may contain a reference to a storage location with
		 the register we want as the index register. If this is the only
		 reference to it then we can increment the use count and use the
		 register. This saves us from copying the value into a second
		 register in order to free this one, and then copying it back.
		 i.e. can generate lw rn,0(rn) *)
		 
		(* Remove it from the cache first - we have to check that its use
		   count is 1 (i.e. it is not being used as base register for
		   something else we want), but we don't care if it is only being
		   cached. It's not clear that we want to do this for non-direct
		   entries, but I would rather not change this code yet. 
		   SPF 25/5/95 *)
		removeFromCache
		    table
		    (singleton reg)
		    (fn () => usage (regset table) reg > 0);
		    
		(* We have now removed all "cache" uses of the register, but
		   there may still be several "direct" uses of it. For the
		   optimisation to work, there should only be one reference
		   left - to this entry, which must be a "direct". *)
		if lastRef andalso 
		      usage (regset table) reg = 1 andalso
		      (case stackEntry of
			 	Direct {base,...} => base regEq reg
		       | _ => false)
		then addRegUse (table, reg)
		else getRegister (table, cvec, reg); (* sets usage to 1 *)
		   
		loadPstackEntry table entry reg cvec;
	  	pushReg (table, reg)
      )
  end; (* loadToSpecificReg *)


  (* Checks if we are going to overwrite the stack, and loads the entry
     into a register. *)
  fun loadEntryBeforeOverwriting (cvec:code) (table as Ttab{pstack, ...}) (offset:int) =
    if 0 <= offset andalso offset < realstackVal table
    then let (* May have to reload something. *)
      fun findTheEntry (entry: stackIndex) =
        if entry indexLt first then () (* finish *)
        else let
          val stackent = pstackEntry table entry;
        in
          case stackent of
            StackEntry {ent = Stack addr, cache, uses, ...} =>
              if addr = offset * ~wordSize
              then let (* This is the entry. *)
                (* Load it without changing the use count. *)
                val reg =
                  if cache regNeq regNone
                  then cache
                  else let
                    val reg = getAnyRegister (table, cvec);
                    val off = (realstackVal table - 1) * wordSize;
                    val U : unit = 
                       genLoad  (addr + off, regStackPtr, reg, cvec);
                  in
                    reg
                  end;
                  
                  val newStackent =
				    (* Make a new entry with a NEW stack destination.
					   If we have to push it we have to use a new location.
					   I don't like this but it's safe because this only occurs
					   for a tail-recursive value or for a temporary value
					   in an exception handler. *)
                    makeStackEntry (Register reg) regNone uses ~1;
               in
                 (* Clear out the cache and overwrite this entry with a
                    reference to the register. *)
                 stretchUpdate (pstack, getIndex entry, newStackent)
               end
               else () (* not this entry *)
               
            | _ => ();
        
          findTheEntry (entry indexMinus 1)
        end (*  findTheEntry *) 
      in
        findTheEntry ((pstackVal table) indexMinus 1)
      end
      else (); (* end of loadEntryBeforeOverwriting *)
      
  (* Store a pseudo-stack entry at a given location on the real stack.  Used
    when making a tail-recursive call.  The problem is that the old entry
    in the real stack may be in use, so we may have to reload it first.
    We load all the values before storing any, so there is no danger of
    overwriting entries in the argument area, but we may have had to push
    some of the registers while doing the load, so those entries will have
    to be saved. *)
  fun storeInStack (cvec, table, entry, locn) : unit =
  let
    (* Move it to the stack, using a move-immediate if possible. *)
    fun inc x = (x := !x + 1);

    fun generalStoreInStack () = (* General case. *)
    let
      val (reg, regEntry) = loadEntry (cvec, table, entry, false)
	in
      (* Lock the register, otherwise it might be used to load an entry. *)
      lockRegister (table, reg);
      
      loadEntryBeforeOverwriting cvec table locn;
      
      (* N.B. loadEntry may push values onto the stack,
         so we cannot use isPush. *)
      if (realstackVal table) = locn
        then
			(
			genPush (reg, cvec);
            inc (realstackptr table)
       		)
        else let
          val loc = ((realstackVal table) - locn - 1) * wordSize
        in
          genStore (reg, loc, regStackPtr, STORE_WORD, regNone, cvec)
        end;

	  unlockRegister (table, reg);
      removeStackEntry(table, regEntry)
    end;

    val isPush = (realstackVal table = locn);

    val (_,{ent = valEnt, cache = cacheReg, ...}) = 
       pstackRealEntry table entry;
  in   
    (* Select the best instruction to use. The default is to load it
      into a register and store or push that. *)
    case valEnt of
      Literal lit =>
        if isPush andalso instrIsRI (instrPush, lit)
        then
		 ( (* Push-immediate. *)
          loadEntryBeforeOverwriting cvec table locn;
          genRI (instrPush, regNone, lit, regNone, cvec);
          incrUseCount (table, entry, ~1);
          inc (realstackptr table)
         )
       
        else if isStoreI(lit, STORE_WORD, false)
        then let (* Store immediate. *)
          val U : unit = loadEntryBeforeOverwriting cvec table locn;
             
          val locn = ((realstackVal table) - locn - 1) * wordSize
          val U : unit = genStoreI (lit, locn, regStackPtr, STORE_WORD, regNone, cvec);
        in
          (* Remove the entry for the literal. *)
          incrUseCount (table, entry, ~1)
        end

        else generalStoreInStack () 
  
    | Direct {base, offset} =>
        if preferLoadPush andalso isPush andalso cacheReg regEq regNone
        then let (* Push memory. *)
          val U : unit = loadEntryBeforeOverwriting cvec table locn;
          val U : unit = genLoadPush (offset, base, cvec);
        in
          incrUseCount (table, entry, ~1);
          inc (realstackptr table)
        end
          
        else generalStoreInStack ()


    | Stack index =>
        if preferLoadPush andalso isPush andalso cacheReg regEq regNone
        then let (* Push stack entry. *)
          val U : unit = loadEntryBeforeOverwriting cvec table locn;
          val locn     = index + (realstackVal table - 1) * wordSize;
          val U : unit = genLoadPush (locn, regStackPtr, cvec);
        in
          incrUseCount (table, entry, ~1);
          inc (realstackptr table)
        end
          
        else generalStoreInStack ()

    | _ => generalStoreInStack ()
    ;
        
    if realstackVal table > maxstackVal table
    then maxstack table := realstackVal table
    else ()
  end (* storeInStack *);

  (* Ensures that the top of the pseudo stack has been copied onto the
     real stack and is at the correct position. stackOffset contains the
     stack offset it should have. Primarily used to push arguments to
     procedures. *)
  fun pushValueToStack (cvec, table, entry, stackOffset) : stackIndex =
  let 
    val U : unit = storeInStack (cvec, table, entry, stackOffset - 1)
  
    val U : unit = 
      (* Remove any entries above the stack offset we need. *)
      if realstackVal table > stackOffset
      then resetButReload (cvec, table, stackOffset)
      else ();
  
    val stackAddr = (stackOffset - 1) * ~wordSize;
    val result : stackIndex =
      pushPstack table (Stack stackAddr) "pushValueToStack";
  in
    (* The stack pointer should now be the required value. *)
    if realstackVal table <> stackOffset
    then raise InternalError "pushValueToStack: Couldn't push to stack"
    else ();
	result
  end;

  fun reserveStackSpace(table: ttab, cvec: code, space: int): stackIndex =
  (* Reserve space on the stack for a tuple. *)
  let
    (* We must first make sure that the space we're going to allocate
	   hasn't been reserved for a register. *)
	val _ = alignStack(table, cvec, ~1)
	(* Initialise the store so that the garbage collector doesn't
	   accidentally pick up an invalid pointer. *)
	(* The stack grows downwards so we want the entries in reverse order.
	   The first entry must be lowest address. *)
	fun pushEntries 0 = []
	 |  pushEntries n =
		let
		    val pushRest = pushEntries (n-1)
			(* We could push a constant but on many architectures it's easier to
			   push a register.  It doesn't matter which provided it contains a
			   valid tagged value or pointer. *)
			val _ = genPush (regN 0, cvec);
		    val stackLocn = incsp table
		in
			stackLocn :: pushRest
		end;
	val entries = pushEntries space
  in
	pushPstack table (Container entries) "reserveStackSpace"
  end
	
  (* Generates an indirection on an item on the pseudo-stack. *)
  fun indirect (offSet, entry, cvec, table) : stackIndex =
  	  case pstackRealEntry table entry of
	  	 (_, {ent = Container l, ...}) =>
		 	(* If we are indirecting off a container we can simply load the entry. *)
			let
				val resIndex = List.nth(l, offSet div wordSize)
			in
				(* Increment its use count. *)
				incrUseCount (table, resIndex, 1);
				removeStackEntry(table, entry); (* Remove the container entry. *)
				resIndex
			end

	  | (_, {ent = Literal i, ...}) =>
	  	  (* We won't normally get this because it will have been optimised out.
		     The exception is when we have SetContainer with a tuple which is a constant. 
			 For safety we check that we have a valid address here although
			 unlike in findEntryInBlock we should never actually get an invalid one. *)
		  (* Actually, we can, in cases such as val (a,b) = raise ...  where we will
		     do an indirection on the dummyValue put on the pstack to represent the
			 non-existent result of the "raise".  In that case we put in a dummy result
			 of zero. *)
		    if isShort i andalso toShort i = 0w0
			then pushConst(table, toMachineWord 0)
			else if isShort i orelse ADDRESS.length (toAddress i) <= Word.fromInt(offSet div wordSize)
			then raise InternalError "indirect - invalid constant address"
			else
			   (
			   removeStackEntry(table, entry); (* Remove the container entry. *)
			   pushConst (table, loadWord (toAddress i, toShort (offSet div wordSize)))
			   )

	  |	 _ =>
	  let
	    val (topReg, topEntry) = loadEntry (cvec, table, entry, false);
	    val U = removeStackEntry(table, topEntry); (* Remove the entry for the register. *)
	    (* and push the indirection *)
	    (* Profiling shows that this search is where the compiler can spend most
	       of its time. To speed it up we keep a lower limit pointer which saves
	       us searching below the lowest direct entry. *)
	    (* See if it is already on the stack. *)
	    fun search s max foundD =
	      if s indexGeq max
	      then
	      ( 
	        (* Not there. *)
	        addRegUse (table, topReg);
	        (* If this is below the previous lower limit we need to reset it. *)
	        if !(lowestDirect table) indexGt (pstackVal table)
	        then lowestDirect table := pstackVal table
	        else ();
	        
	        pushPstack table (Direct {base = topReg, offset = offSet}) "indirect"
	      )
	      
	      else 
	        case (pstackEntry table s) of
	          StackEntry {ent = Direct {base, offset}, ...} =>
	            (
	              (* If we found no direct entries below here 
	                 then remember this as the first. *)
	              if not foundD then lowestDirect table := s else ();
	              
	              if base regEq topReg andalso offset = offSet
	              then (* Found it *)
	                ( 
	                  incrUseCount (table, s, 1);
	                  s
	                )
	              else search (s indexPlus 1) max true (* Found one *)
	            )
	     
	        | _ =>
	            search (s indexPlus 1) max foundD; (* end search *)
	  in
	    search (! (lowestDirect table)) (pstackVal table) false
	  end;

  (* Copies an item which is in another procedure, and therefore on a
     different table, onto the local stack. If the value is relative
     to the frame pointer we must generate a static-link entry and make
     the address relative to that. The only values should be either
     literals (i.e. the address of a static-link called procedure)
     or a stack value. *)
  fun pushNonLocal (fromTable, toTable, locn, makeSl, cvec) : stackIndex =
  let
    val (_,{ent = stacken, uses = use, ...}) = 
      pstackRealEntry fromTable locn;
  in
    if use <= 0 
    then raise InternalError "pushNonLocal: zero use count"
    else ();
    
    (* Don't decrement the use-count because there will only be one
       count for all the references from this closure. *)
    case stacken of
      Literal lit =>
        pushConst (toTable, lit)
        
    | CodeRef code =>
        pushCodeRef (toTable, code)
        
    | Stack index =>
      let
        (* Load static link entry (this points to the first
           word in the frame, not to the base of the frame). *)
        val sl = makeSl ();                          
      in (* Indirect to get the particular entry in the frame. *)
        indirect (index, sl, cvec, toTable)
      end    
       
    | _ => raise InternalError "pushNonLocal: not Literal, CodeRef or Stack"
  end;

  fun isProcB (table, locn) : bool = 
    stretchSub (isProc table, locn);

  (* Moves an expression into a newly created vector or into a container. *)
  fun moveToVec (vecEntry, valueEntry, addr, width, cvec, table) : unit =
  (* Vector index; Value to put in; Vector Offset, code; Translation table *)
    (* We have to load the vector address if we have just used the
       last free register. We could flush the registers and reuse topReg
       for valReg. Increment the use count on the register just to be sure. *)
    let
      val U : unit = incrUseCount (table, vecEntry, 1);
      val (topReg, topEntry)   = loadEntry (cvec, table, vecEntry, false);
      
     (* We have to be careful if we have just used the last free register to
        load the vector address. We could flush the registers and reuse topReg
        for valReg. Increment the use count on the register just to be sure. *)
      val U : unit  = lockRegister (table, topReg);
      
      fun storeViaRegister () =
      let
        val (valReg, regEntry) = loadEntry (cvec, table, valueEntry, false);
      in
        genStore (valReg, addr, topReg, width, regNone, cvec);
        removeStackEntry(table, regEntry)
      end;
      
     in
        case (pstackRealEntry table valueEntry) of 
          (* Can we simply move it? *)
          (_,{ent = Literal lit,...}) =>
            if isStoreI(lit, width, false)
            then
			(
				genStoreI (lit, addr, topReg, width, regNone, cvec);
                (* Remove the entry for the literal. *)
                incrUseCount (table, valueEntry, ~1)
            )
            else storeViaRegister ()
            
        | _ => storeViaRegister ()
        ;
      
       (* Release it. *)
	   unlockRegister (table,  topReg);
       removeStackEntry(table, topEntry)
     end;
     
  (* Loads a value into a register if it is in the argument area.  Used
     for tail-recursive calls. "storeInStack" checks for overwriting
     entries elsewhere on the stack, but because the argument area is not
     represented by entries on the pstack it won't work for them. *)
   fun loadIfArg (cvec, table, entry) : stackIndex =
   let
     val (_,valEntry) = pstackRealEntry table entry
   in
     case (pstackRealEntry table entry) of
       (_,{ent = Stack index, ...}) =>
         if index > 0
         then let
           val (_, newEntry) = loadEntry (cvec, table, entry, false)
         in
           newEntry
         end
         else entry
         
     | _ => entry  (* return the original. *)
   end;

  fun getRegisterSet (addr: machineWord): regSet =
  	(* The set of register modified by a function. *)
  let
  	 val doCall: int*machineWord -> Word.word
		= RunCall.run_call2 RuntimeCalls.POLY_SYS_process_env

	 val rSet = doCall(103, addr) (* Get the bit pattern from the function. *)
  	 (* It would be much better to use Word.word here rather than
		this Address type.  The only reason we still use it is just
		in case we ever need to bootstrap from an old database. *)
     val andb = Word.andb and << = Word.<<
	 infix andb <<

	 fun decodeBits (i: int) (l: reg list) =
	 	if i < 0 then l
		else if (rSet andb (0w1 << Word.fromInt i)) <> 0w0
		then decodeBits (i-1) (regN i :: l)
		else decodeBits (i-1) l

	 val registers = decodeBits (regs-1) []
  in
  	 listToSet registers
  end

  fun getRegisterSetForCode (cvec: code) : regSet =
  (* Get the register set for a forward reference which may or may not
     have already been compiled. *)
  	case codeAddress cvec of
		SOME addr => (* Now compiled - return the register set. *)
			getRegisterSet (toMachineWord addr)
	|   NONE =>
		 (* We haven't compiled this yet: assume worst case. *) allRegisters

  (* Get the register set for an entry on the stack which will be the entry
     point of a function.  If it's not a constant we have to assume it
	 modifies any of the registers. *)
  fun getFunctionRegSet(index: stackIndex, transtab: ttab) : regSet =
  let
    val (_,{ent = stacken, ...}) = pstackRealEntry transtab index;
  in    
    case stacken of
      Literal lit => getRegisterSet lit
    | CodeRef code => getRegisterSetForCode code
    | _ => allRegisters
  end;


  (* An optional result. i.e. if the code before the jump has returned a result
     this is the offset in the table of the result. *)
  datatype mergeResult = NoMerge | MergeIndex of stackIndex;

  (* A code label packaged up with a saved state. *)
  abstype labels =
     NoLabels
   | Labels of {result: mergeResult, lab: CODECONS.labels, state: savedState}
  with
    val noJump = NoLabels;
    fun isEmptyLabel NoLabels = true | isEmptyLabel _ = false;
    fun makeLabels res cLab sState = Labels {result=res, lab = cLab, state = sState};
  
    fun labs  (Labels {lab  ,...}) = lab   | labs  _ = raise Match;
    fun state (Labels {state,...}) = state | state _ = raise Match;
    fun result (Labels {result,...}) = result | result _ = raise Match;
  end;


  (* Set the state to the saved values. *) 
  fun setState (save : savedState, table as Ttab{pstack, pstackTrace, printStream, ...}, cvec, carry, mark, isMerge): mergeResult =
  let 
    val U : unit = 
      if pstackTrace then printState printStream save "setState" else ();
    
    (* This is logically unnecessary, but increases the likelihood
       that values moved out of resultReg (for example) are moved
       into the *same* register in the different branches. That in
       turn decreases the work we have to do when we merge the
       branches back again. SPF 5/6/95 *)
    val U : unit =
      setNextRegNo (regset table, #nextRegNo save);
    
    val topReg =
		case carry of
			NoMerge => regNone (* Unused *)
		  | MergeIndex savedTop =>
		  	(
		        case (pStackRealEntry save savedTop) of
		          (_,{ent = Register reg, ...}) =>
		            reg
		            
		        | (_,{cache = cacheReg, ...}) =>
		           if cacheReg regNeq regNone
		           then cacheReg
		           else raise InternalError "setState: not a register"
		    )
  in
    (* Clobber all entries above the "mark".
        This will remove the result register if there is one. *)
    (* TODO: I don't like this.  I think we should explicitly remove it.
	   DCJM 30/11/99. *)
    for (indDownto (pstackVal table indexMinus 1) mark)
      (fn s => removeEntry table s true);
      
    let
      (* Set up the saved state. Need to set the register set.
         Free the registers from the table. *)
      fun frees s =
        if s indexGeq pstackVal table
        then ()
        else let
          val stacken = pstackEntry table s;
        in
          case stacken of
            NoStackEntry => ()
          
         |  StackEntry {ent, cache, ...} =>
              (
                case ent of 
                  Register reg =>
                    freeRegister (table, reg)
                    
                | Direct {base, ...} =>
                    freeRegister (table, base)
                  
                | _ => ()
                ;
                
                if cache regNeq regNone
                then freeRegister (table, cache)
                else ()
              )
          ;
          
          frees (s indexPlus 1)
        end;
    in
      frees first 
    end;

    realstackptr table := realStackPtr save;
    
    let
      val oldPstackptr = pstackVal table;
      val U = pstackptr table := pStackPtr save;

      (* Go up the entries putting them onto the table from the saved
         state, then come back setting the use-counts where appropriate.
         We have to do it this way because of copy entries. *)
	  (* But we don't have copy entries any longer so this could
	     be improved. DCJM 30/11/99. *)
      fun putOnEntries s =
        if s indexGeq pStackPtr save
        then ()
        else let
          val saveEntry = pStackEntry save s;
          val (tabUseCount, tabDestStack) =
		  	(* Get the use-count and stack destination in the table. *)
            if s indexGeq oldPstackptr then (0, ~1)
            else 
              case (pstackEntry table s) of
                NoStackEntry           => (0, ~1)
              | StackEntry {uses, destStack, ...} =>
			  		(uses, if exitedVal table then ~1 else destStack)
              ;
        in
          (* Put the saved entry into the table. *)
          stretchUpdate (pstack, getIndex s, saveEntry);
          
          case saveEntry of
            NoStackEntry =>
              putOnEntries (s indexPlus 1)
          
          | StackEntry {ent, cache, destStack, uses, ...} =>
              (
			  	(* Compute the new register set. *)
                case ent of
                  Register reg => 
                    incr (regset table) reg
                    
                | Direct {base, ...} =>
                    incr (regset table) base
                    
                | _ => ()
                ;
                
                if cache regNeq regNone
                then incr (regset table) cache 
                else ();

			  (* Propagate the destination stack offset information.  If the
			     saved offset is non-negative it must agree with the current
				 information.  *)
			  if tabDestStack < 0 then ()
			  else if destStack >= 0
			  then (if destStack <> tabDestStack
			  		then raise InternalError "Different stack destinations" else ()
				   )
			  else stretchUpdate (pstack, getIndex s,
			  		makeStackEntry ent cache uses tabDestStack);
              
              putOnEntries (s indexPlus 1);
              
              (* Can now set the use counts. The use-counts may have changed
                 and entries may have been removed because the use-counts of
                 copy entries have been decremented. *)
			  (* This no longer applies now that copy entries have been
			     removed.  Continue to do it that way for the moment.
				 Note that with the change from use-counts to last-references
				 we no longer reduce the use count to the lower of the
				 saved and current values in the case where we are setting
				 the state at the start of a parallel flow of control (e.g.
				 at the start of the else-part of an if-then-else) but only
				 when this is being used to "merge" flows of control where
				 one flow has actually exited.  In that case the use counts should
				 normally agree but there may be cases where they don't, maybe
				 associated with statically-linked functions. *)
			  if isMerge
			  then
	              let
	                val currUseCount =
	                  if s indexGeq (pstackVal table) then 0
	                  else 
	                    case (pstackEntry table s) of
	                      NoStackEntry           => 0
	                    | StackEntry {uses, ...} => uses
	              in
	                if tabUseCount < currUseCount
	                then incrUseCount (table, s, tabUseCount - currUseCount)
	                else ()
	              end
			  else ()
            )
        end;
      val U: unit = putOnEntries first;

	  val result: mergeResult =
	  	  case carry of
		  	MergeIndex _ =>(* Put the result register onto the stack. *)
		      ( getRegister (table, cvec, topReg);
		        MergeIndex(pushReg (table, topReg))
		      )
	        | NoMerge => NoMerge
    in
      if pstackTrace then printStack table "setState" (traceContext cvec) else ();
	  result
    end
  end;

 (* Loads all "direct" entries into registers. This is done when saving the
    state before a branch to avoid a problem when the states are merged back.
    When the states are merged we do it by loading entries into registers,
    but we may not have enough registers to load all the direct entries, so
    we do it now, and push entries to the stack as necessary. *)
  (* I've removed the calls to this to help test the new code with
     explicit stack destinations.  DCJM 28/6/2000. *)
  fun loadDirectEntries (table as Ttab{pstack, ...}) cvec =
  let
    (* Load any values above "stackOffset". *)
    fun loadEntries entry max =
      if entry indexGeq max then ()
      else
        (
          case (pstackEntry table entry) of 
            StackEntry {ent = Direct {base, ...}, uses, cache, destStack} =>
              if uses <= 0  
              then ()
              
              else if cache regNeq regNone
              then let
                val newStackent = 
                  makeStackEntry (Register cache) regNone uses destStack;
                val U : unit =
                  stretchUpdate (pstack, getIndex entry, newStackent)
              in
                freeRegister (table, base)
              end
              
              else let
                val reg = getAnyRegister (table, cvec);
              in
                (* Getting a register could cause this entry to
                   be pushed onto the stack, so we have to check again. *)
                case (pstackEntry table entry) of 
                  StackEntry {ent = Direct {base, offset}, cache, uses, destStack} =>
                    let
                      val U : unit = genLoad (offset, base, reg, cvec)
                      val newStackent = 
                        makeStackEntry (Register reg) regNone uses destStack
                      val U : unit = 
                        stretchUpdate (pstack, getIndex entry, newStackent)
                    in
                      freeRegister (table, base)
                    end
                
                 | _ => (* Direct stackentry has already been pushed. *)
                    freeRegister (table, reg)
              end (* not cached *)
          | _ => () (* not direct *)
          ;
        
          loadEntries (entry indexPlus 1) max
      );  (* end loadEntries *)
  in            
    loadEntries (! (lowestDirect table)) (pstackVal table)
  end;

  fun unconditionalBranch (result, table, cvec) : labels =
    if branchedVal table then noJump
    else let
      (* val U : unit = loadDirectEntries table cvec *)
      val state = saveState (table, cvec);
    in
      branched table := true;
      makeLabels result (CODECONS.unconditionalBranch cvec) state
    end;

  fun jumpBack(start, table, cvec): unit =
    (
	jumpback(start, true, cvec);
	branched table := true
	)

  (* Record the stack limit when we diverge and then use it when we merge
     back again. *)
  type stackMark = { newMark: stackIndex, oldMark: stackIndex };
  
  fun newMark ({newMark,...}: stackMark) = newMark;
  fun oldMark ({oldMark,...}: stackMark) = oldMark;
  
  fun makeStackMark (newM: stackIndex) (oldM: stackIndex) = 
    { newMark = newM, oldMark = oldM };
  
  fun markStack table =
  let
    val oldMark = markerVal table;
    val newMark = pstackVal table;
  in
    marker table := newMark;
    makeStackMark newMark oldMark
  end;

  fun unmarkStack(table, mark) = marker table := oldMark mark;

  (* mergeState is used when two flows of control merge e.g. at the end of
     the else-part of an if-then-else when the state saved at the end of the
	 then-part has to be merged with the state resulting from the else-part.
	 This function first tries to do what it can to make the current state
	 match the saved state.  If it can't do it it may require a "reverse merge"
	 where we swap over the saved and current states.  Ideally we would simply
	 patch in extra code in the then-part but that's too complicated.  Instead
	 "fixup" does it by generating an unconditional branch, fixing up the original
	 branch and then calling mergeState to try and merge again.  This should only
	 require one reverse to converge.
	 I've virtually rewritten this function since it was the source of a number
	 of bugs, particularly some identified by Simon Finn.  The aim now is to
	 converge by having a (partial) ordering on the types of entries:
	    Stack > Register/Cached > Direct.
	 We never load a stack entry into a register.
	 DCJM 29/6/2000.
	 *)
  fun mergeState (save : savedState, savedResult: mergeResult,
  				  table as Ttab{pstack, pstackTrace, printStream, ...}, currentResult: mergeResult, cvec, mark) : bool*mergeResult =
  let
    val needOtherWay = ref false;
  in
    if pstackTrace
    then
    ( printStack table ("mergeState") (traceContext cvec);
      printState printStream save "saved state" 
    ) 
    else ();
    if (markerVal table) indexNeq (newMark mark)
    then raise InternalError "Marker"
    else ();
    
    (* Merge the tables together. The only complication is that if both
       sides are returning values they may be at different locations on
       the pseudo stack. We load the top
       of the current stack into the register that was used for the top
       of the saved state and then remove it. There is no need to remove
       the top of the saved state because those entries will correspond
       to zero-use count entries in the current stack. *)
    let
      val topReg =
        case (savedResult, currentResult) of
			(MergeIndex savedTop, MergeIndex currentTop) =>
        let
          val sTopReg = 
            case (pStackRealEntry save savedTop) of
              (_,{ent = Register reg, ...}) =>
                 reg
                 
            | (_,{cache = cacheReg, ...}) =>
                if cacheReg regNeq regNone
                then cacheReg
                else raise InternalError "Not a register";
            
            (* Load the value on the top of "table" into the same register
               (it ought to be there anyway). *)
           val regEntry =
		   	loadToSpecificReg (cvec, table, sTopReg, currentTop, true);
		in
           (* Because this register will be at a different offset in
              the table from in the saved state it is easier to remove
              the register and put it on later. *)
           removeStackEntry(table, regEntry);
           getRegister (table, cvec, sTopReg);
           sTopReg
         end
         | (NoMerge, NoMerge) => regNone (* Unused *)
		 | _ => (* They should agree on whether they will return a result or not. *)
		 	raise InternalError "mergeState - Mismatched result states"
     in
       (* Clobber all entries above the "mark". These are values which are
	      local to the block since the split and so are no longer required.
		  They should normally have been removed as soon as they were no
		  longer required.  *)
       for (indDownto (pstackVal table indexMinus 1) (newMark mark))
         (fn s => removeEntry table s true);
		 
	   (* First pass: get rid of entries which are no longer required.
	      Also propagate stack destination info.  That probably isn't
		  required because it should already have happened (the saved
		  state represents a previous state) but shouldn't be a problem. *)
       (* The entries on the stack will only be those that were there
         before we split the instruction streams we are now merging.
         All those pushed since then will be in different positions
         in the saved state and current state and so will be removed
         from the merged state. The common entries may differ if we
         have had to push some values that were in registers onto the
         real stack. *)
	   for (indDownto (pstackVal table indexMinus 1) first)
	   	 (fn s =>
		 	case (pstackEntry table s, pStackEntry save s) of
				(NoStackEntry, _) => () (* No entry in table. *)

			|	(StackEntry _, NoStackEntry) =>
                     (* table entry could be non-empty if it is a cache entry
                        or if we are doing a backwards merge. If we do a
                        backwards merge we can have entries in the table 
                        with non-zero use counts, but those can be removed. *)                 
                      removeEntry table s false

			|	(StackEntry {uses = tabUses, cache = tabCache, ent = tabEnt,
			  			     destStack = tabDest},
			     StackEntry {uses = saveUses, cache = saveCache, ent = saveEnt,
				  			 destStack = saveDest}) =>
					let
					  val mergedDest =
						if tabDest >= 0
						then
						  (if saveDest >= 0 andalso tabDest <> saveDest
						   then raise InternalError "merge: mismatched destination"
						   else ();
						   tabDest
						   )
						else saveDest
					in
                      if tabUses = 0 orelse saveUses = 0
                       (* The use-counts may be zero if we have retained an
                          entry because it is cached in a register. We remove
                          these entries unless it is the same value and cached 
                          in the same register *)
                      then
                        if tabCache regNeq saveCache
                        then removeEntry table s false
                        else 
                          case (tabEnt, saveEnt) of 
                            (Direct {base = tabBase,  offset = tabOffset},
                             Direct {base = saveBase, offset = saveOffset}) =>
                               if tabBase regEq saveBase andalso tabOffset = saveOffset
                               then ()
                               else removeEntry table s false
                               
                          | (Stack tabIndex, Stack saveIndex) =>
                                if tabIndex = saveIndex
                                then ()
                                else removeEntry table s false
                                
                          | _ => 
                                removeEntry table s false
            
                      else (* We need to retain this entry. *)
					     (
						 if tabDest <> mergedDest
						 then stretchUpdate (pstack, getIndex s,
                                makeStackEntry tabEnt tabCache tabUses mergedDest)
						 else ()
						 )
					end
		 );

	   (* Try to align the real stack pointer by popping unused values.
	      We MUST remove entries which have been pushed onto the stack
		  in the saved state but not in the current state since we'll
		  have to push them here.  We must not remove entries which
		  are currently in use.  One further complication is that we
		  may have exception handler(s) on the real stack so we can't
		  simply pop everything above the highest used stack position.
		  It would probably be better if we recorded handler locations
		  on the pstack - maybe change this. *)
       let
	  	 (* Find the highest stack value which is actually in use. *)
	     fun getInUse s i =
		 	case pstackEntry table s of
				StackEntry {ent = Stack addr, ...} =>
					(* The stack pointer must be one more
					   than the highest value in use. *)
					Int.max(addr div ~wordSize+1, i)
			|	_ => i

	  	 val stackInUse =
		 (* This is the highest used stack location, but we may have a
		    handler above it so we can't necessarily reset the stack
			to here. *)
		 	revfoldIterator getInUse 0
				(indDownto ((pstackVal table) indexMinus 1) first)

		 (* Examine the saved stack to see those entries which have
		    been pushed in the saved state but not in the current
			state. We need to reset the stack below this.  If
			there are no such entries we return the stack pointer
			from the saved state. *)
	     fun getMinStack s i =
		 	case (pstackEntry table s, pStackEntry save s) of
				(StackEntry {ent = Stack addr, ...}, _) => i
			|	(StackEntry _, StackEntry{ent = Stack addr, ...}) =>
					(* We have an entry which has been pushed in
					   the saved state but not in the current state.
					   We have to set sp below this. *)
					let
						val minStack = Int.min(addr div ~wordSize, i)
					in
						if minStack < stackInUse
						(* Check that we don't have entries we're going
						   to have to push below those we've already
						   pushed. DCJM 25/1/01. *)
						then raise InternalError "mergeState: unpushed entries"
						else minStack
					end
			|	_ => i;
	  	 val minStack =
		 	revfoldIterator getMinStack (realStackPtr save)
				(indDownto ((pstackVal table) indexMinus 1) first)

		 (* We can reset the stack to the maximum of the entries
		    currently in use and those which need to be pushed or
			the saved sp if there aren't any. *)
		 val maxStack = Int.max(stackInUse, minStack)
	   in
	  	if maxStack < realstackVal table
		then
		   (
		    resetStack (realstackVal table - maxStack, cvec);
			realstackptr table := maxStack
		   )
		else ()
       end;

	   (* Second pass: push any entry which was pushed in the saved state. *)
	   (* We have a choice here about what to do when we have a value
	     which is in a register on one branch and on the stack in the
		 other.  The original approach was to get both values back
		 into the register by reloading the register from the stack.
		 That worked well on the Sparc where there were plenty of
		 registers but less well on the i386.  The advantage is that
		 if we have a branch which is small and frequently taken
		 we don't incur any cost.
		 e.g. "val x = ...; val y = if ... then 1 else f();"
		 The register containing x has to be pushed before we call
		 f but not before 1.  If the then-branch is most frequently
		 taken we don't want to incur extra cost by pushing x on that
		 branch as well.
		 There are two disadvantages of trying to reload registers.
		 The first is that we may have to spill other registers as
		 we do it and end up thrashing around trying to get the
		 values into the correct registers.  The other is that if
		 we have to push the registers anyway we've incurred extra
		 cost.
		 The current approach is to move values to the stack.  *)
	   let
	   	  fun mustPush s =
		 	case (pstackEntry table s, pStackEntry save s) of
			 	(StackEntry {ent = Stack _, ...}, StackEntry {ent = Stack _, ...}) =>
					false (* both on stack *)
			|   (StackEntry _, StackEntry {ent = Stack _, ...}) =>
					true (* Saved value is on stack but current value isn't. *)
			|	_ => false
	   in
	   	  (* Pushing one entry may result in others being pushed if
		     they have a lower "destStack". *)
	      pushRegisters table cvec allRegisters mustPush false
	   end;
 
	   (* Third pass: Load any entry which is in a register in the saved state
	      and ensure that values in registers in the current state are moved
		  into the same register as before. *)
	   let
            (* Put the table entry in a specified register and
               make it a register entry. *)
	        fun loadToReg (s, prefReg, tabEnt, tabCache, tabUses, tabDest) =
	        let
	          val dReg =
	            if prefReg regEq regStackPtr
	            then (* No preference. *)
				let
	              val reg = getAnyRegister (table, cvec);
	            in
	              loadPstackEntry table s reg cvec;
	              reg
	            end
	            else 
	              (* Put it in the preferred register. If it's already there
				     we need to increment the use count because we will
					 decrement it in "removeEntry". *)
	              if (case tabEnt of Register reg => reg regEq prefReg | _ => false) orelse
	                 tabCache regEq prefReg  (* Already there. *)
	              then (addRegUse (table, prefReg); prefReg) (* Already there. *)
	              else
	                (
	                  getRegister (table, cvec, prefReg);
	                  loadPstackEntry table s prefReg cvec;
	                  prefReg
	                )
	        in 
	          (* loadPstackEntry will have decremented the use count and may
			     have completely removed the entry.  If it hasn't we need to
				 remove it before we replace it with the loaded register.
				 If we didn't call loadPstackEntry (because we already had
				 the value in the correct register) we have to call removeEntry
				 to decrement the register use count (we incremented it above)
				 and so restore it to the original value. *)
	          case (pstackEntry table s) of
	            NoStackEntry => ()
	          | StackEntry _ => removeEntry table s false
	          ;
	          stretchUpdate (pstack, getIndex s,
	                  makeStackEntry (Register dReg) regNone
					  	tabUses tabDest)
	        end (* loadToReg *)

	      fun loadEntries s =
		  	 case (pstackEntry table s, pStackEntry save s) of
			 	(StackEntry {ent = Stack _, ...}, _) =>
					(* If it's in the stack we don't try reloading it. *) ()

			 |	(StackEntry {ent = tabEnt, cache, uses, destStack, ...},
				 StackEntry {ent = Register savedReg, ...}) =>
					loadToReg(s, savedReg, tabEnt, cache, uses, destStack)

			 |	(StackEntry{ent = tabEnt as Direct{base = tabBase, offset = tabOffset},
			 			    cache = tabCache, uses, destStack, ...},
			 	 StackEntry{ent = Direct{base = saveBase, offset = saveOffset},
				 		    cache = savedCache, ...}) =>
				    (
				 	if tabOffset <> saveOffset
					then raise InternalError "merge: mismatched offsets"
					else ();
					(* If the base registers are different (which might
					   happen if the original reg was required) we need
					   to load this entry.  We will probably also need
					   to do a reverse merge and load the corresponding
					   entry in the saved state. *)
					if tabBase regNeq saveBase
					then
						let
							val prefReg =
								if savedCache regNeq regNone
								then savedCache
								else if tabCache regNeq regNone
                   				then tabCache
								else regStackPtr; (* No preference. *)
						in
						  loadToReg (s, prefReg, tabEnt, tabCache, uses, destStack)
						end
					else ()
					)
			 |  _ => ()
 
	   in
		  for (indDownto (pstackVal table indexMinus 1) first) loadEntries
	   end;

	   (* Final pass: Check to see if we need to do a "reverse merge" i.e.
	      operations that have to be done on the saved state before we
		  can finally merge.  Also flush mismatched items from the cache. *)
	   let
	      fun checkEntries s =
		  	 case (pstackEntry table s, pStackEntry save s) of
			 	(StackEntry {uses = tabUses, cache = tabCache, ent = tabEnt,
			  			     destStack = tabDest},
			     StackEntry {uses = saveUses, cache = saveCache, ent = saveEnt,
				  			 destStack = saveDest}) =>
					let
						fun flushCache () =
							if tabCache regNeq regNone andalso tabCache regNeq saveCache
							then
							   (
                               free (regset table) tabCache;
                               stretchUpdate (pstack, getIndex s,
                                  makeStackEntry tabEnt regNone tabUses tabDest)
							   )
							else ()
					in
					case tabEnt of
						Register tabReg =>
							(
							(* It's fine if the saved value was cached in that
							   register. *)
							if saveCache regEq tabReg then ()
							else case saveEnt of
								Register saveReg =>
									(* We should have moved these into the same
									   register.  It's possible it got moved again
									   as a result of loading something else. *)
									if tabReg regNeq saveReg
									then needOtherWay := true else ()
							|	Stack _ =>
									(* We should have pushed it in the second pass. *)
									raise InternalError "merge: unpushed entry"
							|	_ => (* Maybe a Direct entry which has to be
										loaded in a reverse merge. *)
									needOtherWay := true
							)
					|	Literal w =>
							(
							case saveEnt of
								Literal _ => flushCache()
							|	_ => raise InternalError "Literal mismatch"
							)
					|	CodeRef c =>
							(
							case saveEnt of
								CodeRef _ => flushCache()
							|	_ => raise InternalError "Coderef mismatch"
							)
					|	Direct {base = tabBase, ...} =>
							(
						    (* As with register entries these should have been
							   merged but might have diverged again. *)
							if saveCache regEq tabCache then ()
							else
								case saveEnt of
									Direct{base=saveBase, ...} =>
									   if tabBase regEq saveBase
									   then flushCache() (* Ok but must flush cache. *)
									   else needOtherWay := true
								|	_ =>
										raise InternalError "merge: mismatched Direct"
							)
					|	Stack tabIndex =>
							(
							case saveEnt of
								Stack saveIndex =>
									(
									(* Consistency check. *)
									if tabIndex = saveIndex then ()
									else raise InternalError "merge: mismatched stack entries";
									flushCache()
									)
								| _ => (* Need to push this in a reverse merge. *)
									needOtherWay := true
							)
					|	Container _ =>
							(
							case saveEnt of
								Container _ => ()
							|	_ => raise InternalError "merge: mismatched Container"
							)
					end
			  | _ => ()
	   in
		  for (indDownto (pstackVal table indexMinus 1) first) checkEntries
	   end;

	   (* Last of all, try to align the stack. If the current stack pointer
	      is greater than the saved value we must have live values on the
		  stack and have to do a reverse merge.  If the saved stack pointer
		  was greater than the current but otherwise everything is fine
		  we just push some dummy values rather than doing a reverse merge.
		  I may change this later. *)
	   if realStackPtr save < realstackVal table
	   then needOtherWay := true
	   else ();

	   if ! needOtherWay then ()
       else while realStackPtr save > realstackVal table
		do (* Have to push something in order to align the stack. *)
		if pushAnyEntryAtCurrentSP(table, cvec) then ()
		else (* Push a register just to align the stack.  It would
		        be better to push a register that wasn't currently
				saved but this will do for the moment. *)
			 (
			 genPush (regN 0, cvec);
			 realstackptr table := realstackVal table + 1
			 );

        (* Push any result. *)
		let
			val result =
				case currentResult of
					MergeIndex _ => MergeIndex(pushReg (table, topReg))
				  | NoMerge => NoMerge
        in
        if pstackTrace then printStack table "mergeState" (traceContext cvec) else ();
 
        (!needOtherWay, result)
		end
     end
  end
    handle SML90.Interrupt => raise SML90.Interrupt
	  | e =>
      (
        printStack table "mergeState" (traceContext cvec);
        printState printStream save "saved state";
        raise e
     );

  (* Fix up a label after an unconditional branch. *)
  fun fixup (lab, table, cvec) : unit =
    if not (branchedVal table) then raise InternalError "Not branched"
    else if isEmptyLabel lab then ()
    else
    ( 
      setState (state lab, table, cvec, NoMerge, pstackVal table, false);
      branched table := false;
	  exited table := false;
      cFixup (labs lab, cvec)
    );
    
  local
    (* Fix up a label.  If this follows an unconditional branch we replace the
       existing state with the saved state, otherwise we have to merge in. *)
    fun mergeLab (lab, table, cvec, currentResult: mergeResult, mark) : mergeResult =
      if isEmptyLabel lab then currentResult
      else
	if ! (branched table)
	then let
	  	val newResult = setState (state lab, table, cvec, result lab, newMark mark, true);
	  in
	    branched table := false;
		exited table := false;
		cFixup (labs lab, cvec);
		newResult
	  end
	else let
	  val (otherWay, mergeRes) =
	  	mergeState (state lab, result lab, table, currentResult, cvec, mark);
	in (* We can generate code before we fix up the label, but if we
	      want to add code to the other arm we have to put in an
	      unconditional branch and make the changes after it. *)
	  if otherWay
	  then let
	    (* Have to jump round to get the states the same. *)
	    val lab1 = unconditionalBranch (mergeRes, table, cvec);
	    val newResult = setState (state lab, table, cvec, result lab, newMark mark, true)
	  in
	    exited table := false;
		branched table := false;
	    cFixup (labs lab, cvec);
	    mergeLab (lab1, table, cvec, newResult, mark) (* Merge the other way. *)
	  end
	  else
	  	  (cFixup (labs lab, cvec); mergeRes)
	end;
  in
    (* Fix up a label.  If this follows an unconditional branch we replace the
       existing state with the saved state, otherwise we have to merge in. *)
    fun merge (lab, table, cvec, carry, mark) : mergeResult =
    let
       val res = mergeLab (lab, table, cvec, carry, mark);
    in (* Reset the marker even if we have not actually done any merging. *)
	  unmarkStack(table, mark);
	  res
    end;

    (* Fix up a list of labels, using the same stack mark *)
    fun mergeList (labs, table, cvec, carry, mark) : mergeResult =
    let
	  fun mergeLabs (l, carry) = mergeLab (l, table, cvec, carry, mark)
    
      val mergeRes = List.foldl mergeLabs carry labs
    in
	  unmarkStack(table, mark);
	  mergeRes
    end;
  end;
  
  type handler = { lab: handlerLab, oldps: stackIndex };
  
  (* Push the address of a handler. *)
  fun pushAddress (table, cvec, offset) : handler =
  let
    (* This is just after a mark. *)
	val reg = getAnyRegister(table, cvec)
    val oldps = pstackVal table

	(* Load the address of the handler into a register. *)
	val handlerLab = loadHandlerAddress(reg, cvec);
	val regEntry = pushReg(table, reg)

	(* Push it onto the stack at the specific offset. *)
	val pushedEntry = pushValueToStack (cvec, table, regEntry, offset);
  in
    (* Remove the pstack entry because we don't want it. *)
    removeStackEntry(table, pushedEntry);
    {lab = handlerLab, oldps = oldps}
  end;
  
  (* Fixup the address at the start of a handler. *)
  fun fixupH ({lab, oldps}, oldsp, table, cvec) : unit =
  ( clearCache table; (* Don't know the registers here. *)
    realstackptr table := oldsp;
    exited table := false;
	branched table := false;

   (* Remove any entries above the old pstack pointer. If the expression
      whose exceptions we are handling contained static-link functions
      there may be entries whose use-counts have not gone to zero. *)
   for (indDownto (pstackVal table indexMinus 1) oldps)
      (fn (s: stackIndex) => removeEntry table s false);

    cFixupH (lab, cvec)
  );

  (* Generate a binary operation. *)
  fun binaryOp (arg1, arg2, instr, revinstr, table as Ttab{pstackTrace, ...}, cvec, hint) : stackIndex =
  let (* If the operand is a short constant we don't need to put it in a register. *)
    val (_,{ent = firstEnt, ...})  = 
       pstackRealEntry table arg1;
       
    val (_,{ent = secondEnt,...}) =
       pstackRealEntry table arg2;
       
    fun genBinaryIR lit =   
    let
      val (initialReg2, initialReg2Loc) = loadEntry (cvec, table, arg2, false);
      
      (* Lock the register to make sure that
		 we don't accidentally reuse it for the result. *)
      val U : unit = lockRegister (table, initialReg2);

      (* Get a result register. It's a shame that if the value is
		 already in the desired result register, then we have to
		 choose a different register for the result. There are two
		 reasons for this:
	    (1) The i386 code generator can't share argument and result registers.
	    (2) Even for the other code-generators, we have a problem because
		we requested non-exclusive use of the argument, so we can't
		zap it with the instruction we're about to execute, because its
		use-count might be greater than 1. One day I'll come back and improve
		this code further.
	 
	 What we want to do is to reserve the result register first, then load the
	 argument. Unfortunately, this doesn't quite work - if the value is cached
	 in the result register, this code will move it out, which we don't want
	 (unless there's more than a single use required). I guess I'll need to
	 write another low-level register allocation function.
	 SPF 17/2/97
	 
	 Current attempt: load the argument first, but try to resuse this register
	 for the result if it's safe to do so. This is still not a perfect
	 approach, but it should generate better code than the previous version.
	 SPF 6/3/97
      *)

      val (arg2Reg, arg2RegLoc, resReg) : reg * stackIndex * reg = 
		case hint of
		  NoHint         => (initialReg2, initialReg2Loc, getAnyRegister (table, cvec))
		| UseReg prefReg => 
		    if prefReg regEq initialReg2
		    then if canShareRegs
		      then let (* We WANT to reuse the argument register for the result *)
		        val U : unit      = unlockRegister (table, initialReg2);
		        val (arg2Reg, arg2RegLoc) = loadEntry (cvec, table, initialReg2Loc, true);
	            val U : unit      = addRegUse (table, arg2Reg); (* For use as result reg. *)
	            val U : unit      = lockRegister (table, arg2Reg);
		      in
		        (arg2Reg, arg2RegLoc, arg2Reg)
		      end
		      else (initialReg2, initialReg2Loc, getAnyRegister (table, cvec))
		    else let
		      val U : unit = getRegister (table, cvec, prefReg);
		    in
		      (initialReg2, initialReg2Loc, prefReg)
		    end;
      
      (* Generate the code. Since we've reversed the operation *)
      (* we have to use the reverse instruction. *)
      val U : unit   =  genRI (revinstr, arg2Reg, lit, resReg, cvec);
      
      (* Push the result onto the stack. *)
      val rreg       = pushReg (table, resReg); 
      val U : unit   = unlockRegister (table, arg2Reg);
    in
      (* Remove the argument register. *)
      incrUseCount (table, arg2RegLoc, ~1);
      incrUseCount (table, arg1, ~1);
      rreg
    end;       
       
    fun genBinaryRI lit =   
    let
      val (initialReg1, initialReg1Loc) = loadEntry (cvec, table, arg1, false);
      val U : unit = lockRegister (table, initialReg1);

      val (arg1Reg, arg1RegLoc, resReg) : reg * stackIndex * reg = 
		case hint of
		  NoHint         => (initialReg1, initialReg1Loc, getAnyRegister (table, cvec))
		| UseReg prefReg => 
		    if prefReg regEq initialReg1
		    then if canShareRegs
		      then let (* We WANT to reuse the argument register for the result *)
		        val U : unit      = unlockRegister (table, initialReg1);
		        val (arg1Reg, arg1RegLoc) = loadEntry (cvec, table, initialReg1Loc, true);
	                val U : unit      = addRegUse (table, arg1Reg); (* For use as result reg. *)
	                val U : unit      = lockRegister (table, arg1Reg);
		      in
		        (arg1Reg, arg1RegLoc, arg1Reg)
		      end
		      else (initialReg1, initialReg1Loc, getAnyRegister (table, cvec))
		    else let
		      val U : unit = getRegister (table, cvec, prefReg);
		    in
		      (initialReg1, initialReg1Loc, prefReg)
		    end;
      
      val U : unit    = genRI (instr, arg1Reg, lit, resReg, cvec);
      val rreg        = pushReg (table, resReg);
      val U : unit    = unlockRegister (table, arg1Reg);
    in
      incrUseCount (table, arg1RegLoc, ~1);
      incrUseCount (table, arg2, ~1);
      rreg
    end;

    fun genBinaryRR () = 
    let
      val (initialReg1, initialReg1Loc) = loadEntry (cvec, table, arg1, false);
      val U : unit       = lockRegister (table, initialReg1);
      
      val (initialReg2, initialReg2Loc) = loadEntry (cvec, table, arg2, false);
      val U : unit       = lockRegister (table, initialReg2);
      
      (* We could improve this by considering what happens if arg1Reg and arg2Reg both share with prefReg,
         but that's getting too obscure to be worthe considering, and would require the
         construction of new low-level code, which I want to avoid.
         
         Note that even in the current code, initialReg1 = prefReg does NOT imply that arg1Reg = prefReg,
         because we might have needed to copy the value to another register if its use-count was
         more than 1.
         SPF 6/3/97
      *)
      val (arg1Reg, arg1RegLoc, arg2Reg, arg2RegLoc, resReg) : reg * stackIndex * reg * stackIndex * reg = 
		case hint of
		  NoHint         => (initialReg1, initialReg1Loc, initialReg2, initialReg2Loc, getAnyRegister (table, cvec))
		| UseReg prefReg => 
		    if prefReg regEq initialReg1
		    then if canShareRegs
		      then let (* We WANT to reuse the argument register for the result. *)
		        val U : unit      = unlockRegister (table, initialReg1);
		        val (arg1Reg, arg1RegLoc) = loadEntry (cvec, table, initialReg1Loc, true);
	                val U : unit      = addRegUse (table, arg1Reg); (* For use as result reg. *)
	                val U : unit      = lockRegister (table, arg1Reg);
		      in
		        (arg1Reg, arg1RegLoc, initialReg2, initialReg2Loc, arg1Reg)
		      end
		      else (initialReg1, initialReg1Loc, initialReg2, initialReg2Loc, getAnyRegister (table, cvec))
		      
		    else if prefReg regEq initialReg2
		    then if canShareRegs
		      then let (* We WANT to reuse the argument register for the result *)
		        val U : unit      = unlockRegister (table, initialReg2);
		        val (arg2Reg, arg2RegLoc) = loadEntry (cvec, table, initialReg2Loc, true);
	                val U : unit      = addRegUse (table, arg2Reg); (* For use as result reg. *)
	                val U : unit      = lockRegister (table, arg2Reg);
		      in
		        (initialReg1, initialReg1Loc, arg2Reg, arg2RegLoc, arg2Reg)
		      end
		      else (initialReg1, initialReg1Loc, initialReg2, initialReg2Loc, getAnyRegister (table, cvec))
		    
		    else let
		      val U : unit = getRegister (table, cvec, prefReg);
		    in
		      (initialReg1, initialReg1Loc, initialReg2, initialReg2Loc, prefReg)
		    end;

      val U          = genRR (instr, arg1Reg, arg2Reg, resReg, cvec);
      val rreg       = pushReg (table, resReg);
      val U : unit   = unlockRegister (table, arg1Reg);
      val U : unit   = unlockRegister (table, arg2Reg);
    in
      incrUseCount (table, arg2RegLoc, ~1); (* Remove the register entries. *)
      incrUseCount (table, arg1RegLoc, ~1);
      rreg
    end;
    
   val result : stackIndex =
     (* If it is a constant we have to check that it is in the range *)
     (* for the instruction we want to use. *)
     case (firstEnt,secondEnt) of
       (Literal lit1, Literal lit2) =>
          (* optimisation should have already dealt with this? *)
          if instrIsRI (revinstr, lit1)
             then genBinaryIR lit1
          else if instrIsRI (instr, lit2)
            then genBinaryRI lit2
          else genBinaryRR ()
          
     | (Literal lit1, _) =>
          if instrIsRI (revinstr, lit1)
             then genBinaryIR lit1
          else genBinaryRR ()
          
     | (_,Literal lit2) =>
          if instrIsRI (instr, lit2)
            then genBinaryRI lit2
          else genBinaryRR ()
          
     | (_,_) =>
          genBinaryRR ();

    val U : unit = if pstackTrace then printStack table "binaryOp" "" else ();
  in 
    result
  end;


  fun assignOp (addr: stackIndex, offset: stackIndex, value: stackIndex,
  				width:storeWidth, table: ttab, cvec: code) =
  let
    val (_,{ent = offsetEnt,...}) = pstackRealEntry table offset
    and (_,{ent = valueEnt,...}) = pstackRealEntry table value

    val (addrReg, addrEntry)   = loadEntry (cvec, table, addr, false)
	(* This register must be locked so that we don't reuse it for the
	   index or value. *)
    val U : unit  = lockRegister (table, addrReg);

	(* The values are indexes but if we use a constant offset it must be in bytes. *)
	val unitSize = case width of STORE_WORD => wordSize | STORE_BYTE => 1

    fun storeViaRegister offset =
      let
        val (valReg, regEntry) = loadEntry (cvec, table, value, false);
      in
        genStore (valReg, offset, addrReg, width, regNone, cvec);
        removeStackEntry(table, regEntry) (* Remove value entry. *)
      end;

	fun storeIndexedViaRegister () =
	  let
	  	val (indexReg, indexRegEntry) = loadEntry (cvec, table, offset, false);
		val U : unit  = lockRegister (table, indexReg);
        val (valReg, regEntry) = loadEntry (cvec, table, value, false);
	  in
        genStore (valReg, 0, addrReg, width, indexReg, cvec);
        removeStackEntry(table, regEntry); (* Remove value entry. *)
	    unlockRegister (table, indexReg); (* Release the index register. *)
        removeStackEntry(table, indexRegEntry)
	  end

  in
		case (offsetEnt, valueEnt) of
			(Literal litOffset, Literal litValue) =>
				(* Constant offset and constant value to be assigned. *)
			    let
					val offsetInt = Word.toInt (toShort litOffset) * unitSize
				in
					if isStoreI(litValue, width, false)
					then
						(
						genStoreI(litValue, offsetInt, addrReg, width, regNone, cvec);
		                (* Remove the entry for the value. *)
		                removeStackEntry(table, value)
						)
					else storeViaRegister offsetInt;
					(* Remove the index. *)
					removeStackEntry(table, offset)
				end

		|	(Literal litOffset, _) =>
				let
					val offsetInt = Word.toInt (toShort litOffset) * unitSize
				in
					(* Constant offset but general value to be assigned. *)
					storeViaRegister offsetInt;
					(* Remove the index. *)
					removeStackEntry(table, offset)
				end

		|   (_, Literal litValue) =>
				if isStoreI(litValue, STORE_WORD, true)
				then 
				(* Constant value but index value in register. *)
				let
				  	val (indexReg, indexRegEntry) =
						loadEntry (cvec, table, offset, false)
				in
					genStoreI(litValue, 0, addrReg, width, indexReg, cvec);
	                (* Remove the entry for the value. *)
	                removeStackEntry(table, value);
					(* Remove the index. *)
					removeStackEntry(table, indexRegEntry)
				end
				else storeIndexedViaRegister ()

		|	_ => storeIndexedViaRegister ()
		;

	   (* Release the base register. *)
	   unlockRegister (table, addrReg);
       removeStackEntry(table, addrEntry)
	end


  (* Generate a binary compare and jump operation. *)
  fun compareAndBranch (arg1, arg2, t, revt, table as Ttab{pstackTrace, ...}, cvec) : labels =
  let
    (* val U : unit                  = loadDirectEntries table cvec; *)
    val (_,{ent = firstEnt,...})  = pstackRealEntry table arg1;
    val (_,{ent = secondEnt,...}) = pstackRealEntry table arg2;

    fun genCompIR test lit =
    let
      val (arg2Reg, arg2RegLoc) = loadEntry (cvec, table, arg2, false);
    in
      (* Remove the register entry from the stack. *)
      incrUseCount (table, arg2RegLoc, ~1);
      incrUseCount (table, arg1, ~1);
      (* Generate the code. We have reversed it - Use the reversed test. *)
      compareAndBranchRI (arg2Reg, lit, test, cvec) 
    end;

    fun genCompRI test lit =
    let
      val (arg1Reg, arg1RegLoc) = loadEntry (cvec, table, arg1, false);
    in
      incrUseCount (table, arg1RegLoc, ~1);
      incrUseCount (table, arg2, ~1);
      compareAndBranchRI (arg1Reg, lit, test, cvec) 
    end;
      
    fun genCompRR test =
    let
      val (arg1Reg, arg1RegLoc) = loadEntry (cvec, table, arg1, false);
      val U : unit   = lockRegister (table, arg1Reg);
      val (arg2Reg, arg2RegLoc) = loadEntry (cvec, table, arg2, false);
      val U : unit   = unlockRegister (table, arg1Reg);
    in
      (* Remove the register entries. *)
      incrUseCount (table, arg1RegLoc, ~1);
      incrUseCount (table, arg2RegLoc, ~1);
      compareAndBranchRR (arg1Reg, arg2Reg, test, cvec)
    end;
      
    val lab = 
      case (firstEnt, secondEnt) of
        (Literal lit1, Literal lit2) =>
           if isCompRI (revt, lit1)
             then genCompIR revt lit1
           else if isCompRI (t, lit2)
             then genCompRI t lit2
           else genCompRR t
    
      | (Literal lit1, _) =>
           if isCompRI (revt, lit1)
             then genCompIR revt lit1
           else genCompRR t
    
      | (_, Literal lit2) =>
           if isCompRI (t, lit2)
             then genCompRI t lit2
           else genCompRR t
    
      | (_, _) =>
           genCompRR t
  in
    if pstackTrace then printStack table "compareAndBranch" "" else ();

    (* Package the label up with the state. *)
    makeLabels NoMerge lab (saveState (table, cvec))
  end 

  (* Tail recursive jump to a function. *)
  fun jumpToCode(codeAddr, isIndirect, returnReg, transtable, cvec) =
  let
  	 val (_, {ent, ...}) = pstackRealEntry transtable codeAddr 
  in
  	 case ent of
	 	Literal lit =>
			jumpToFunction(ConstantFun(lit, isIndirect), returnReg, cvec)

	 |	CodeRef code =>
	 		if isIndirect
			then raise InternalError "jumpToCode: indirect call to codeRef"
			else jumpToFunction(CodeFun code, returnReg, cvec)

	 |	Register reg => (* Should only be the closure register and only in
	 					   the indirect case. *)
			if isIndirect andalso reg regEq regClosure
			then jumpToFunction(FullCall, returnReg, cvec)
			else raise InternalError "jumpToCode: Not indirection through closure reg"

	 |  _ => (* Anything else shouldn't happen. *)
	 		raise InternalError "jumpToCode: Not a constant or register";

	incrUseCount (transtable, codeAddr, ~1)
  end;

  (* Call a function. *)
  fun callCode(codeAddr, isIndirect, transtable, cvec) =
  let
  	 val (_, {ent, ...}) = pstackRealEntry transtable codeAddr 
  in
  	 case ent of
	 	Literal lit => callFunction(ConstantFun(lit, isIndirect), cvec)

	 |	CodeRef code =>
	 		if isIndirect
			then raise InternalError "callCode: indirect call to codeRef"
			else callFunction(CodeFun code, cvec)

	 |	Register reg => (* Should only be the closure register and only in
	 					   the indirect case. *)
			if isIndirect andalso reg regEq regClosure
			then callFunction(FullCall, cvec)
			else raise InternalError "callCode: Not indirection through closure reg"

	 |  _ => (* Anything else shouldn't happen. *)
	 		raise InternalError "callCode: Not a constant or register";
	incrUseCount (transtable, codeAddr, ~1)
  end;

  fun startCase (table, cvec, state) : addrs =
    (
      setState (state, table, cvec, NoMerge, pstackVal table, false);
      exited table := false;
	  branched table := false;
      ic cvec
    );

  (* These are exported as read-only. *)
  val maxstack     = maxstackVal;
  val realstackptr = realstackVal;
  val haveExited = exitedVal

  (* This is called when we have either made a tail-recursive call,
     returned from a function or raised an exception. *)
  fun exiting table =
  (
    branched table := true;
    exited table := true
  )

val allRegisters = allRegisters


datatype argdest = ArgToRegister of reg | ArgToStack of int

(* Get the destination for the argument of a loop instruction.  This
   finds out where the argument was loaded at the start of the loop
   so that it can be put back there at the end. *)
fun getLoopDestinations(indices, transtable) =
	let
		fun getLoopDest entry =
			case pstackEntry transtable entry of
				NoStackEntry => raise InternalError "getLoopDest: no entry"
			|	StackEntry{ent = Stack index, ...} => ArgToStack index
			|	StackEntry{ent = Register reg, ...} => ArgToRegister reg
			|	_ => raise InternalError "getLoopDest: wrong entry type"
	in
		map getLoopDest indices
	end

end; (* local for stackUnion datatype declaration *)

end (* TRANSTAB *)

end; (* let for body of TRANSTAB *)