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
|
%---------------------------------------------------------------------------%
% Copyright (C) 1994-1999 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%---------------------------------------------------------------------------%
%
% File: code_info.m
%
% Main authors: conway, zs.
%
% This file defines the code_info type and various operations on it.
% The code_info structure is the 'state' of the code generator.
%
% This file is organized into nine submodules:
%
% - the code_info structure and its access predicates
% - simple wrappers around access predicates
% - handling branched control structures
% - handling failure continuations
% - handling liveness issues
% - saving and restoring heap pointers, trail tickets etc
% - interfacing to code_exprn
% - managing the info required by garbage collection and value numbering
% - managing stack slots
%
%---------------------------------------------------------------------------%
:- module code_info.
:- interface.
:- import_module hlds_module, hlds_pred, hlds_goal, llds, instmap, trace.
:- import_module continuation_info, prog_data, hlds_data, globals.
:- import_module bool, set, list, map, std_util, assoc_list.
:- implementation.
:- import_module code_util, code_exprn, llds_out, prog_out.
:- import_module arg_info, type_util, mode_util, options.
:- import_module term, varset.
:- import_module set, stack.
:- import_module string, require, char, bimap, tree, int.
%---------------------------------------------------------------------------%
% Submodule for the code_info type and its access predicates.
%
% This submodule has the following components:
%
% declarations for exported access predicates
% declarations for non-exported access predicates
% the definition of the type and the init predicate
% the definition of the get access predicates
% the definition of the set access predicates
%
% Please keep the order of mention of the various fields
% consistent in each of these five components.
:- interface.
:- type code_info.
% Create a new code_info structure. Also return the
% outermost resumption point, and info about the non-fixed
% stack slots used for tracing purposes.
:- pred code_info__init(bool, globals, pred_id, proc_id, proc_info,
follow_vars, module_info, int, resume_point_info,
trace_slot_info, code_info).
:- mode code_info__init(in, in, in, in, in, in, in, in, out, out, out) is det.
% Get the globals table.
:- pred code_info__get_globals(globals, code_info, code_info).
:- mode code_info__get_globals(out, in, out) is det.
% Get the HLDS of the entire module.
:- pred code_info__get_module_info(module_info, code_info, code_info).
:- mode code_info__get_module_info(out, in, out) is det.
% Get the id of the predicate we are generating code for.
:- pred code_info__get_pred_id(pred_id, code_info, code_info).
:- mode code_info__get_pred_id(out, in, out) is det.
% Get the id of the procedure we are generating code for.
:- pred code_info__get_proc_id(proc_id, code_info, code_info).
:- mode code_info__get_proc_id(out, in, out) is det.
% Get the HLDS of the procedure we are generating code for.
:- pred code_info__get_proc_info(proc_info, code_info, code_info).
:- mode code_info__get_proc_info(out, in, out) is det.
% Get the variables for the current procedure.
:- pred code_info__get_varset(prog_varset, code_info, code_info).
:- mode code_info__get_varset(out, in, out) is det.
:- pred code_info__get_maybe_trace_info(maybe(trace_info),
code_info, code_info).
:- mode code_info__get_maybe_trace_info(out, in, out) is det.
% Get the set of currently forward-live variables.
:- pred code_info__get_forward_live_vars(set(prog_var), code_info, code_info).
:- mode code_info__get_forward_live_vars(out, in, out) is det.
% Set the set of currently forward-live variables.
:- pred code_info__set_forward_live_vars(set(prog_var), code_info, code_info).
:- mode code_info__set_forward_live_vars(in, in, out) is det.
% Get the table mapping variables to the current
% instantiation states.
:- pred code_info__get_instmap(instmap, code_info, code_info).
:- mode code_info__get_instmap(out, in, out) is det.
% Set the table mapping variables to the current
% instantiation states.
:- pred code_info__set_instmap(instmap, code_info, code_info).
:- mode code_info__set_instmap(in, in, out) is det.
% The current value of the counter we use to give
% each "create" rval its unique cell number, for use
% in case the cell can be allocated statically.
:- pred code_info__get_cell_count(int, code_info, code_info).
:- mode code_info__get_cell_count(out, in, out) is det.
:- pred code_info__set_cell_count(int, code_info, code_info).
:- mode code_info__set_cell_count(in, in, out) is det.
% Get the flag that indicates whether succip is used or not.
:- pred code_info__get_succip_used(bool, code_info, code_info).
:- mode code_info__get_succip_used(out, in, out) is det.
% Get the label layout information created by tracing
% during code generation.
:- pred code_info__get_layout_info(map(label, internal_layout_info),
code_info, code_info).
:- mode code_info__get_layout_info(out, in, out) is det.
% Get the global static data structures that have
% been created during code generation and which do
% not have to be scanned by llds_common, since they
% have no common parts by construction.
:- pred code_info__get_non_common_static_data(list(comp_gen_c_data),
code_info, code_info).
:- mode code_info__get_non_common_static_data(out, in, out) is det.
:- pred code_info__get_max_reg_in_use_at_trace(int, code_info, code_info).
:- mode code_info__get_max_reg_in_use_at_trace(out, in, out) is det.
:- pred code_info__set_max_reg_in_use_at_trace(int, code_info, code_info).
:- mode code_info__set_max_reg_in_use_at_trace(in, in, out) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- pred code_info__get_var_slot_count(int, code_info, code_info).
:- mode code_info__get_var_slot_count(out, in, out) is det.
:- pred code_info__set_maybe_trace_info(maybe(trace_info),
code_info, code_info).
:- mode code_info__set_maybe_trace_info(in, in, out) is det.
:- pred code_info__get_zombies(set(prog_var), code_info, code_info).
:- mode code_info__get_zombies(out, in, out) is det.
:- pred code_info__set_zombies(set(prog_var), code_info, code_info).
:- mode code_info__set_zombies(in, in, out) is det.
:- pred code_info__get_exprn_info(exprn_info, code_info, code_info).
:- mode code_info__get_exprn_info(out, in, out) is det.
:- pred code_info__set_exprn_info(exprn_info, code_info, code_info).
:- mode code_info__set_exprn_info(in, in, out) is det.
:- pred code_info__get_temps_in_use(set(lval), code_info, code_info).
:- mode code_info__get_temps_in_use(out, in, out) is det.
:- pred code_info__set_temps_in_use(set(lval), code_info, code_info).
:- mode code_info__set_temps_in_use(in, in, out) is det.
:- pred code_info__get_fail_info(fail_info, code_info, code_info).
:- mode code_info__get_fail_info(out, in, out) is det.
:- pred code_info__set_fail_info(fail_info, code_info, code_info).
:- mode code_info__set_fail_info(in, in, out) is det.
:- pred code_info__get_label_count(int, code_info, code_info).
:- mode code_info__get_label_count(out, in, out) is det.
:- pred code_info__set_label_count(int, code_info, code_info).
:- mode code_info__set_label_count(in, in, out) is det.
:- pred code_info__set_succip_used(bool, code_info, code_info).
:- mode code_info__set_succip_used(in, in, out) is det.
:- pred code_info__set_layout_info(map(label, internal_layout_info),
code_info, code_info).
:- mode code_info__set_layout_info(in, in, out) is det.
:- pred code_info__get_max_temp_slot_count(int, code_info, code_info).
:- mode code_info__get_max_temp_slot_count(out, in, out) is det.
:- pred code_info__set_max_temp_slot_count(int, code_info, code_info).
:- mode code_info__set_max_temp_slot_count(in, in, out) is det.
:- pred code_info__get_temp_content_map(map(lval, slot_contents),
code_info, code_info).
:- mode code_info__get_temp_content_map(out, in, out) is det.
:- pred code_info__set_temp_content_map(map(lval, slot_contents),
code_info, code_info).
:- mode code_info__set_temp_content_map(in, in, out) is det.
:- pred code_info__set_non_common_static_data(list(comp_gen_c_data),
code_info, code_info).
:- mode code_info__set_non_common_static_data(in, in, out) is det.
%---------------------------------------------------------------------------%
% The code_info structure has three groups of fields.
%
% Some fields are static; they are set when the code_info structure
% is initialized, and never changed afterwards.
%
% Some fields record information about the state of the code generator
% at a particular location in the HLDS code of the current procedure.
% At the start of the branched control structure, the code generator
% remembers the values of these fields, and starts generating code
% for each branch from the same location-dependent state.
%
% Some fields record persistent information that does not depend
% on a code location. Updates to these fields must remain effective
% even when the code generator resets its location-dependent state.
:- type code_info --->
code_info(
% STATIC fields
globals, % For the code generation options.
module_info, % The module_info structure - you just
% never know when you might need it.
pred_id, % The id of the current predicate.
proc_id, % The id of the current procedure.
proc_info, % The proc_info for the this procedure.
prog_varset, % The variables in this procedure.
int, % The number of stack slots allocated.
% for storing variables.
% (Some extra stack slots are used
% for saving and restoring registers.)
maybe(trace_info),
% Information about which stack slots
% the call sequence number and depth
% are stored in, provided tracing is
% switched on.
% LOCATION DEPENDENT fields
set(prog_var), % Variables that are forward live
% after this goal.
instmap, % Current insts of the live variables.
set(prog_var), % Zombie variables; variables that are not
% forward live but which are protected by
% an enclosing resume point.
exprn_info, % A map storing the information about
% the status of each known variable.
% (Known vars = forward live vars + zombies)
set(lval), % The set of temporary locations currently in
% use. These lvals must be all be keys in the
% map of temporary locations ever used, which
% is one of the persistent fields below. Any
% keys in that map which are not in this set
% are free for reuse.
fail_info, % Information about how to manage failures.
% PERSISTENT fields
int, % Counter for the local labels used
% by this procedure.
int, % Counter for cells in this proc.
bool, % do we need to store succip?
map(label, internal_layout_info),
% Information on which values
% are live and where at which labels,
% for tracing. Accurate gc
int, % The maximum number of extra
% temporary stackslots that have been
% used during the procedure.
map(lval, slot_contents),
% The temporary locations that have ever been
% used on the stack, and what they contain.
% Once we have used a stack slot to store
% e.g. a ticket, we never reuse that slot
% to hold something else, e.g. a saved hp.
% This policy prevents us from making such
% conflicting choices in parallel branches,
% which would make it impossible to describe
% to gc what the slot contains after the end
% of the branched control structure.
list(comp_gen_c_data),
% Static data structures created for this
% procedure which do not need to be scanned
% by llds_common.
int % At each call to MR_trace, we compute the
% highest rN register number that contains
% a useful value. This slot contains the
% maximum of these highest values. Therefore
% at all calls to MR_trace in the procedure,
% we need only save the registers whose numbers
% are equal to or smaller than this field.
% This slot contains -1 if tracing is not
% enabled.
).
%---------------------------------------------------------------------------%
code_info__init(SaveSuccip, Globals, PredId, ProcId, ProcInfo, FollowVars,
ModuleInfo, CellCount, ResumePoint, TraceSlotInfo, CodeInfo) :-
proc_info_get_initial_instmap(ProcInfo, ModuleInfo, InstMap),
proc_info_liveness_info(ProcInfo, Liveness),
proc_info_headvars(ProcInfo, HeadVars),
proc_info_arg_info(ProcInfo, ArgInfos),
proc_info_interface_code_model(ProcInfo, CodeModel),
assoc_list__from_corresponding_lists(HeadVars, ArgInfos, Args),
arg_info__build_input_arg_list(Args, ArgList),
globals__get_options(Globals, Options),
proc_info_varset(ProcInfo, VarSet),
proc_info_stack_slots(ProcInfo, StackSlots),
code_exprn__init_state(ArgList, VarSet, StackSlots, FollowVars,
Options, ExprnInfo),
stack__init(ResumePoints),
globals__lookup_bool_option(Globals, allow_hijacks, AllowHijack),
(
AllowHijack = yes,
Hijack = allowed
;
AllowHijack = no,
Hijack = not_allowed
),
DummyFailInfo = fail_info(ResumePoints, resume_point_unknown,
may_be_different, not_inside_non_condition, Hijack),
map__init(TempContentMap),
set__init(TempsInUse),
set__init(Zombies),
map__init(LayoutMap),
code_info__max_var_slot(StackSlots, VarSlotMax),
trace__reserved_slots(ProcInfo, Globals, FixedSlots),
int__max(VarSlotMax, FixedSlots, SlotMax),
CodeInfo0 = code_info(
Globals,
ModuleInfo,
PredId,
ProcId,
ProcInfo,
VarSet,
SlotMax,
no,
Liveness,
InstMap,
Zombies,
ExprnInfo,
TempsInUse,
DummyFailInfo, % code_info__init_fail_info
% will override this dummy value
0,
CellCount,
SaveSuccip,
LayoutMap,
0,
TempContentMap,
[],
-1
),
code_info__init_maybe_trace_info(Globals, ModuleInfo, ProcInfo,
MaybeFailVars, TraceSlotInfo, CodeInfo0, CodeInfo1),
code_info__init_fail_info(CodeModel, MaybeFailVars, ResumePoint,
CodeInfo1, CodeInfo).
:- pred code_info__init_maybe_trace_info(globals, module_info, proc_info,
maybe(set(prog_var)), trace_slot_info, code_info, code_info).
:- mode code_info__init_maybe_trace_info(in, in, in, out, out, in, out) is det.
code_info__init_maybe_trace_info(Globals, ModuleInfo, ProcInfo,
MaybeFailVars, TraceSlotInfo) -->
{ globals__get_trace_level(Globals, TraceLevel) },
( { TraceLevel \= none } ->
trace__setup(Globals, TraceSlotInfo, TraceInfo),
code_info__set_maybe_trace_info(yes(TraceInfo)),
{ trace__fail_vars(ModuleInfo, ProcInfo, FailVars) },
{ MaybeFailVars = yes(FailVars) }
;
{ MaybeFailVars = no },
{ TraceSlotInfo = trace_slot_info(no, no, no) }
).
%---------------------------------------------------------------------------%
code_info__get_globals(SA, CI, CI) :-
CI = code_info(SA, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_module_info(SB, CI, CI) :-
CI = code_info(_, SB, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_pred_id(SC, CI, CI) :-
CI = code_info(_, _, SC, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_proc_id(SD, CI, CI) :-
CI = code_info(_, _, _, SD, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_proc_info(SE, CI, CI) :-
CI = code_info(_, _, _, _, SE, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_varset(SF, CI, CI) :-
CI = code_info(_, _, _, _, _, SF, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_var_slot_count(SG, CI, CI) :-
CI = code_info(_, _, _, _, _, _, SG, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_maybe_trace_info(SH, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, SH,
_, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_forward_live_vars(LA, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
LA, _, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_instmap(LB, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, LB, _, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_zombies(LC, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, LC, _, _, _, _, _, _, _, _, _, _, _).
code_info__get_exprn_info(LD, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, LD, _, _, _, _, _, _, _, _, _, _).
code_info__get_temps_in_use(LE, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, LE, _, _, _, _, _, _, _, _, _).
code_info__get_fail_info(LF, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, LF, _, _, _, _, _, _, _, _).
code_info__get_label_count(PA, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, PA, _, _, _, _, _, _, _).
code_info__get_cell_count(PB, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, PB, _, _, _, _, _, _).
code_info__get_succip_used(PC, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, PC, _, _, _, _, _).
code_info__get_layout_info(PD, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, PD, _, _, _, _).
code_info__get_max_temp_slot_count(PE, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, PE, _, _, _).
code_info__get_temp_content_map(PF, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, PF, _, _).
code_info__get_non_common_static_data(PG, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, PG, _).
code_info__get_max_reg_in_use_at_trace(PH, CI, CI) :-
CI = code_info(_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _, _, _, _, PH).
%---------------------------------------------------------------------------%
code_info__set_maybe_trace_info(SH, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, _,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_forward_live_vars(LA, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
_, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_instmap(LB, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, _, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_zombies(LC, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, _, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_exprn_info(LD, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, _, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_temps_in_use(LE, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, _, LF, PA, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_fail_info(LF, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, _, PA, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_label_count(PA, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, _, PB, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_cell_count(PB, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, _, PC, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_succip_used(PC, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, _, PD, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_layout_info(PD, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, _, PE, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_max_temp_slot_count(PE, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, _, PF, PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_temp_content_map(PF, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, _ , PG, PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_non_common_static_data(PG, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, _ , PH),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__set_max_reg_in_use_at_trace(PH, CI0, CI) :-
CI0 = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, _ ),
CI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule for simple wrappers around access predicates.
:- interface.
% Get the hlds mapping from variables to stack slots
:- pred code_info__get_stack_slots(stack_slots, code_info, code_info).
:- mode code_info__get_stack_slots(out, in, out) is det.
% Get the table that contains advice about where
% variables should be put.
:- pred code_info__get_follow_vars(follow_vars, code_info, code_info).
:- mode code_info__get_follow_vars(out, in, out) is det.
% Set the table that contains advice about where
% variables should be put.
:- pred code_info__set_follow_vars(follow_vars, code_info, code_info).
:- mode code_info__set_follow_vars(in, in, out) is det.
% code_info__pre_goal_update(GoalInfo, Atomic, OldCodeInfo, NewCodeInfo)
% updates OldCodeInfo to produce NewCodeInfo with the changes
% specified by GoalInfo.
:- pred code_info__pre_goal_update(hlds_goal_info, bool, code_info, code_info).
:- mode code_info__pre_goal_update(in, in, in, out) is det.
% code_info__post_goal_update(GoalInfo, OldCodeInfo, NewCodeInfo)
% updates OldCodeInfo to produce NewCodeInfo with the changes described
% by GoalInfo.
:- pred code_info__post_goal_update(hlds_goal_info, code_info, code_info).
:- mode code_info__post_goal_update(in, in, out) is det.
% Find out the type of the given variable.
:- pred code_info__variable_type(prog_var, type, code_info, code_info).
:- mode code_info__variable_type(in, out, in, out) is det.
:- pred code_info__lookup_type_defn(type, hlds_type_defn,
code_info, code_info).
:- mode code_info__lookup_type_defn(in, out, in, out) is det.
% Given a constructor id, and a variable (so that we can work out the
% type of the constructor), determine correct tag (representation)
% of that constructor.
:- pred code_info__cons_id_to_tag(prog_var, cons_id, cons_tag,
code_info, code_info).
:- mode code_info__cons_id_to_tag(in, in, out, in, out) is det.
% Get the code model of the current procedure.
:- pred code_info__get_proc_model(code_model, code_info, code_info).
:- mode code_info__get_proc_model(out, in, out) is det.
% Get the list of the head variables of the current procedure.
:- pred code_info__get_headvars(list(prog_var), code_info, code_info).
:- mode code_info__get_headvars(out, in, out) is det.
% Get the call argument information for the current procedure
:- pred code_info__get_arginfo(list(arg_info), code_info, code_info).
:- mode code_info__get_arginfo(out, in, out) is det.
% Get the call argument info for a given mode of a given predicate
:- pred code_info__get_pred_proc_arginfo(pred_id, proc_id, list(arg_info),
code_info, code_info).
:- mode code_info__get_pred_proc_arginfo(in, in, out, in, out) is det.
% Get the set of variables currently needed by the resume
% points of enclosing goals.
:- pred code_info__current_resume_point_vars(set(prog_var),
code_info, code_info).
:- mode code_info__current_resume_point_vars(out, in, out) is det.
:- pred code_info__variable_to_string(prog_var, string, code_info, code_info).
:- mode code_info__variable_to_string(in, out, in, out) is det.
% Create a code address which holds the address of the specified
% procedure.
% The fourth argument should be `no' if the the caller wants the
% returned address to be valid from everywhere in the program.
% If being valid from within the current procedure is enough,
% this argument should be `yes' wrapped around the value of the
% --procs-per-c-function option and the current procedure id.
% Using an address that is only valid from within the current
% procedure may make jumps more efficient.
%
% If the procs_per_c_function option tells us to put more than one
% procedure into each C function, but not all procedures in the module
% are in one function, then we would like to be able to use the
% fast form of reference to a procedure for references not only from
% within the same procedure but also from other procedures within
% the same C function. However, at the time of code generation,
% we do not yet know which procedures will be put into the same
% C functions, and so we cannot do this.
:- pred code_info__make_entry_label(module_info, pred_id, proc_id, bool,
code_addr, code_info, code_info).
:- mode code_info__make_entry_label(in, in, in, in, out, in, out) is det.
% Generate the next local label in sequence.
:- pred code_info__get_next_label(label, code_info, code_info).
:- mode code_info__get_next_label(out, in, out) is det.
% Generate the next cell number in sequence.
:- pred code_info__get_next_cell_number(int, code_info, code_info).
:- mode code_info__get_next_cell_number(out, in, out) is det.
% Note that the succip slot is used, and thus cannot be
% optimized away.
:- pred code_info__succip_is_used(code_info, code_info).
:- mode code_info__succip_is_used(in, out) is det.
:- pred code_info__add_trace_layout_for_label(label, term__context,
trace_port, goal_path, layout_label_info, code_info, code_info).
:- mode code_info__add_trace_layout_for_label(in, in, in,in, in, in, out)
is det.
:- pred code_info__add_non_common_static_data(comp_gen_c_data,
code_info, code_info).
:- mode code_info__add_non_common_static_data(in, in, out) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- pred code_info__add_resume_layout_for_label(label, layout_label_info,
code_info, code_info).
:- mode code_info__add_resume_layout_for_label(in, in, in, out) is det.
code_info__get_stack_slots(StackSlots, CI, CI) :-
code_info__get_exprn_info(ExprnInfo, CI, _),
code_exprn__get_stack_slots(StackSlots, ExprnInfo, _).
code_info__get_follow_vars(FollowVars, CI, CI) :-
code_info__get_exprn_info(ExprnInfo, CI, _),
code_exprn__get_follow_vars(FollowVars, ExprnInfo, _).
code_info__set_follow_vars(FollowVars, CI0, CI) :-
code_info__get_exprn_info(ExprnInfo0, CI0, _),
code_exprn__set_follow_vars(FollowVars, ExprnInfo0, ExprnInfo),
code_info__set_exprn_info(ExprnInfo, CI0, CI).
%-----------------------------------------------------------------------------%
% Update the code info structure to be consistent
% immediately prior to generating a goal.
code_info__pre_goal_update(GoalInfo, Atomic) -->
% The liveness pass puts resume_point annotations on some kinds
% of goals. The parts of the code generator that handle those kinds
% of goals should handle the resume point annotation as well;
% when they do, they remove the annotation. The following code
% is a sanity check to make sure that this has in fact been done.
{ goal_info_get_resume_point(GoalInfo, ResumePoint) },
(
{ ResumePoint = no_resume_point }
;
{ ResumePoint = resume_point(_, _) },
{ error("pre_goal_update with resume point") }
),
{ goal_info_get_follow_vars(GoalInfo, MaybeFollowVars) },
(
{ MaybeFollowVars = yes(FollowVars) },
code_info__set_follow_vars(FollowVars)
;
{ MaybeFollowVars = no }
),
% note: we must be careful to apply deaths before births
{ goal_info_get_pre_deaths(GoalInfo, PreDeaths) },
code_info__rem_forward_live_vars(PreDeaths),
code_info__make_vars_forward_dead(PreDeaths),
{ goal_info_get_pre_births(GoalInfo, PreBirths) },
code_info__add_forward_live_vars(PreBirths),
( { Atomic = yes } ->
{ goal_info_get_post_deaths(GoalInfo, PostDeaths) },
code_info__rem_forward_live_vars(PostDeaths)
;
[]
).
% Update the code info structure to be consistent
% immediately after generating a goal.
code_info__post_goal_update(GoalInfo) -->
% note: we must be careful to apply deaths before births
{ goal_info_get_post_deaths(GoalInfo, PostDeaths) },
code_info__rem_forward_live_vars(PostDeaths),
code_info__make_vars_forward_dead(PostDeaths),
{ goal_info_get_post_births(GoalInfo, PostBirths) },
code_info__add_forward_live_vars(PostBirths),
code_info__make_vars_forward_live(PostBirths),
{ goal_info_get_instmap_delta(GoalInfo, InstMapDelta) },
code_info__get_instmap(InstMap0),
{ instmap__apply_instmap_delta(InstMap0, InstMapDelta, InstMap) },
code_info__set_instmap(InstMap).
%---------------------------------------------------------------------------%
:- pred code_info__get_var_types(map(prog_var, type), code_info, code_info).
:- mode code_info__get_var_types(out, in, out) is det.
code_info__get_var_types(VarTypes) -->
code_info__get_proc_info(ProcInfo),
{ proc_info_vartypes(ProcInfo, VarTypes) }.
code_info__variable_type(Var, Type) -->
code_info__get_var_types(VarTypes),
{ map__lookup(VarTypes, Var, Type) }.
code_info__lookup_type_defn(Type, TypeDefn) -->
code_info__get_module_info(ModuleInfo),
{ type_to_type_id(Type, TypeIdPrime, _) ->
TypeId = TypeIdPrime
;
error("unknown type in code_aux__lookup_type_defn")
},
{ module_info_types(ModuleInfo, TypeTable) },
{ map__lookup(TypeTable, TypeId, TypeDefn) }.
code_info__cons_id_to_tag(Var, ConsId, ConsTag) -->
code_info__variable_type(Var, Type),
code_info__get_module_info(ModuleInfo),
{ code_util__cons_id_to_tag(ConsId, Type, ModuleInfo, ConsTag) }.
%---------------------------------------------------------------------------%
code_info__get_proc_model(CodeModel) -->
code_info__get_proc_info(ProcInfo),
{ proc_info_interface_code_model(ProcInfo, CodeModel) }.
code_info__get_headvars(HeadVars) -->
code_info__get_module_info(ModuleInfo),
code_info__get_pred_id(PredId),
code_info__get_proc_id(ProcId),
{ module_info_pred_proc_info(ModuleInfo, PredId, ProcId, _, ProcInfo) },
{ proc_info_headvars(ProcInfo, HeadVars) }.
code_info__get_arginfo(ArgInfo) -->
code_info__get_pred_id(PredId),
code_info__get_proc_id(ProcId),
code_info__get_pred_proc_arginfo(PredId, ProcId, ArgInfo).
code_info__get_pred_proc_arginfo(PredId, ProcId, ArgInfo) -->
code_info__get_module_info(ModuleInfo),
{ module_info_pred_proc_info(ModuleInfo, PredId, ProcId, _, ProcInfo) },
{ proc_info_arg_info(ProcInfo, ArgInfo) }.
code_info__current_resume_point_vars(ResumeVars) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(ResumePointStack, _, _, _, _) },
{ stack__top_det(ResumePointStack, ResumePointInfo) },
{ code_info__pick_first_resume_point(ResumePointInfo, ResumeMap, _) },
{ map__keys(ResumeMap, ResumeMapVarList) },
{ set__list_to_set(ResumeMapVarList, ResumeVars) }.
code_info__variable_to_string(Var, Name) -->
code_info__get_varset(Varset),
{ varset__lookup_name(Varset, Var, Name) }.
%---------------------------------------------------------------------------%
code_info__make_entry_label(ModuleInfo, PredId, ProcId, Immed0, PredAddress) -->
(
{ Immed0 = no },
{ Immed = no }
;
{ Immed0 = yes },
code_info__get_globals(Globals),
{ globals__lookup_int_option(Globals, procs_per_c_function,
ProcsPerFunc) },
code_info__get_pred_id(CurPredId),
code_info__get_proc_id(CurProcId),
{ Immed = yes(ProcsPerFunc - proc(CurPredId, CurProcId)) }
),
{ code_util__make_entry_label(ModuleInfo, PredId, ProcId, Immed,
PredAddress) }.
code_info__get_next_label(Label) -->
code_info__get_module_info(ModuleInfo),
code_info__get_pred_id(PredId),
code_info__get_proc_id(ProcId),
code_info__get_label_count(N0),
{ N is N0 + 1 },
code_info__set_label_count(N),
{ code_util__make_internal_label(ModuleInfo, PredId, ProcId, N,
Label) }.
code_info__get_next_cell_number(N) -->
code_info__get_cell_count(N0),
{ N is N0 + 1 },
code_info__set_cell_count(N).
code_info__succip_is_used -->
code_info__set_succip_used(yes).
code_info__add_trace_layout_for_label(Label, Context, Port, Path, Layout) -->
code_info__get_layout_info(Internals0),
{ Exec = yes(trace_port_layout_info(Context, Port, Path, Layout)) },
{ map__search(Internals0, Label, Internal0) ->
Internal0 = internal_layout_info(Exec0, Resume, Return),
( Exec0 = no ->
true
;
error("adding trace layout for already known label")
),
Internal = internal_layout_info(Exec, Resume, Return),
map__set(Internals0, Label, Internal, Internals)
;
Internal = internal_layout_info(Exec, no, no),
map__det_insert(Internals0, Label, Internal, Internals)
},
code_info__set_layout_info(Internals).
code_info__add_resume_layout_for_label(Label, LayoutInfo) -->
code_info__get_layout_info(Internals0),
{ Resume = yes(LayoutInfo) },
{ map__search(Internals0, Label, Internal0) ->
Internal0 = internal_layout_info(Exec, Resume0, Return),
( Resume0 = no ->
true
;
error("adding gc layout for already known label")
),
Internal = internal_layout_info(Exec, Resume, Return),
map__set(Internals0, Label, Internal, Internals)
;
Internal = internal_layout_info(no, Resume, no),
map__det_insert(Internals0, Label, Internal, Internals)
},
code_info__set_layout_info(Internals).
:- pred code_info__get_active_temps_data(assoc_list(lval, slot_contents),
code_info, code_info).
:- mode code_info__get_active_temps_data(out, in, out) is det.
code_info__get_active_temps_data(Temps) -->
code_info__get_temps_in_use(TempsInUse),
code_info__get_temp_content_map(TempContentMap),
{ map__select(TempContentMap, TempsInUse, TempsInUseContentMap) },
{ map__to_assoc_list(TempsInUseContentMap, Temps) }.
code_info__add_non_common_static_data(NonCommonData) -->
code_info__get_non_common_static_data(NonCommonDatas0),
code_info__set_non_common_static_data(
[NonCommonData | NonCommonDatas0]).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule for handling branched control structures.
:- interface.
:- type position_info.
:- type branch_end_info.
:- type branch_end == maybe(branch_end_info).
:- pred code_info__remember_position(position_info, code_info, code_info).
:- mode code_info__remember_position(out, in, out) is det.
:- pred code_info__reset_to_position(position_info, code_info, code_info).
:- mode code_info__reset_to_position(in, in, out) is det.
:- pred code_info__reset_resume_known(position_info, code_info, code_info).
:- mode code_info__reset_resume_known(in, in, out) is det.
:- pred code_info__generate_branch_end(store_map, branch_end, branch_end,
code_tree, code_info, code_info).
:- mode code_info__generate_branch_end(in, in, out, out, in, out) is det.
:- pred code_info__after_all_branches(store_map, branch_end,
code_info, code_info).
:- mode code_info__after_all_branches(in, in, in, out) is det.
:- pred code_info__save_hp_in_branch(code_tree, lval, position_info,
position_info).
:- mode code_info__save_hp_in_branch(out, out, in, out) is det.
:- implementation.
:- type position_info
---> position_info(
code_info % The code_info at a given position
% in the code of the procedure.
).
:- type branch_end_info
---> branch_end_info(
code_info % The code_info at the end of a branch.
).
code_info__remember_position(position_info(C), C, C).
code_info__reset_to_position(position_info(PosCI), CurCI, NextCI) :-
% The static fields in PosCI and CurCI should be identical.
PosCI = code_info(_, _, _, _, _, _, _, _,
LA, LB, LC, LD, LE, LF, _, _, _, _, _, _, _, _ ),
CurCI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
_, _, _, _, _, _, PA, PB, PC, PD, PE, PF, PG, PH),
NextCI = code_info(SA, SB, SC, SD, SE, SF, SG, SH,
LA, LB, LC, LD, LE, LF, PA, PB, PC, PD, PE, PF, PG, PH).
code_info__reset_resume_known(BranchStart) -->
{ BranchStart = position_info(BranchStartCI) },
{ code_info__get_fail_info(BranchStartFailInfo, BranchStartCI, _) },
{ BranchStartFailInfo = fail_info(_, BSResumeKnown, _, _, _) },
code_info__get_fail_info(CurFailInfo),
{ CurFailInfo = fail_info(CurFailStack, _,
CurCurfMaxfr, CurCondEnv, CurHijack) },
{ NewFailInfo = fail_info(CurFailStack, BSResumeKnown,
CurCurfMaxfr, CurCondEnv, CurHijack) },
code_info__set_fail_info(NewFailInfo).
code_info__generate_branch_end(StoreMap, MaybeEnd0, MaybeEnd, Code) -->
code_info__get_exprn_info(Exprn0),
{ map__to_assoc_list(StoreMap, VarLocs) },
{ code_exprn__place_vars(VarLocs, Code, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn),
=(EndCodeInfo1),
{
MaybeEnd0 = no,
EndCodeInfo = EndCodeInfo1
;
MaybeEnd0 = yes(branch_end_info(EndCodeInfo0)),
% Make sure the left context we leave the
% branched structure with is valid for all branches.
code_info__get_fail_info(FailInfo0, EndCodeInfo0, _),
code_info__get_fail_info(FailInfo1, EndCodeInfo1, _),
FailInfo0 = fail_info(_, ResumeKnown0, CurfrMaxfr0,
CondEnv0, Hijack0),
FailInfo1 = fail_info(R, ResumeKnown1, CurfrMaxfr1,
CondEnv1, Hijack1),
(
ResumeKnown0 = resume_point_known(Redoip0),
ResumeKnown1 = resume_point_known(Redoip1)
->
ResumeKnown = resume_point_known(Redoip0),
require(unify(Redoip0, Redoip1),
"redoip mismatch in generate_branch_end")
;
ResumeKnown = resume_point_unknown
),
(
CurfrMaxfr0 = must_be_equal,
CurfrMaxfr1 = must_be_equal
->
CurfrMaxfr = must_be_equal
;
CurfrMaxfr = may_be_different
),
(
Hijack0 = allowed,
Hijack1 = allowed
->
Hijack = allowed
;
Hijack = not_allowed
),
require(unify(CondEnv0, CondEnv1),
"some but not all branches inside a non condition"),
FailInfo = fail_info(R, ResumeKnown, CurfrMaxfr,
CondEnv0, Hijack),
code_info__set_fail_info(FailInfo, EndCodeInfo1, EndCodeInfoA),
% Make sure the "temps in use" set at the end of the
% branched control structure includes every slot
% in use at the end of any branch.
code_info__get_temps_in_use(TempsInUse0, EndCodeInfo0, _),
code_info__get_temps_in_use(TempsInUse1, EndCodeInfo1, _),
set__union(TempsInUse0, TempsInUse1, TempsInUse),
code_info__set_temps_in_use(TempsInUse, EndCodeInfoA,
EndCodeInfo)
},
{ MaybeEnd = yes(branch_end_info(EndCodeInfo)) }.
code_info__after_all_branches(StoreMap, MaybeEnd, CI0, CI) :-
(
MaybeEnd = yes(BranchEnd),
BranchEnd = branch_end_info(BranchEndCodeInfo),
code_info__reset_to_position(position_info(BranchEndCodeInfo),
CI0, CI1),
code_info__remake_with_store_map(StoreMap, CI1, CI)
;
MaybeEnd = no,
error("no branches in branched control structure")
).
% code_info__remake_with_store_map throws away the exprn_info data
% structure, forgetting the current locations of all variables,
% and rebuilds it from scratch based on the given store map.
% The new exprn_info will know about only the variables present
% in the store map, and will believe they are where the store map
% says they are.
:- pred code_info__remake_with_store_map(store_map, code_info, code_info).
:- mode code_info__remake_with_store_map(in, in, out) is det.
code_info__remake_with_store_map(StoreMap) -->
{ map__to_assoc_list(StoreMap, VarLvals) },
{ code_info__fixup_lvallist(VarLvals, VarRvals) },
code_info__get_exprn_info(Exprn0),
{ code_exprn__reinit_state(VarRvals, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
:- pred code_info__fixup_lvallist(assoc_list(prog_var, lval),
assoc_list(prog_var, rval)).
:- mode code_info__fixup_lvallist(in, out) is det.
code_info__fixup_lvallist([], []).
code_info__fixup_lvallist([V - L | Ls], [V - lval(L) | Rs]) :-
code_info__fixup_lvallist(Ls, Rs).
code_info__save_hp_in_branch(Code, Slot, Pos0, Pos) :-
Pos0 = position_info(CodeInfo0),
code_info__save_hp(Code, Slot, CodeInfo0, CodeInfo),
Pos = position_info(CodeInfo).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule for the handling of failure continuations.
% The principles underlying this submodule of code_info.m are
% documented in the file compiler/notes/failure.html, which also
% defines terms such as "quarter hijack"). Some parts of the submodule
% also require knowledge of compiler/notes/allocation.html.
:- interface.
:- type resume_map.
:- type resume_point_info.
% `prepare_for_disj_hijack' should be called before entering
% a disjunction. It saves the values of any nondet stack slots
% the disjunction may hijack, and if necessary, sets the redofr
% slot of the top frame to point to this frame. The code at the
% start of the individual disjuncts will override the redoip slot.
%
% `undo_disj_hijack' should be called before entering the last
% disjunct of a disjunction. It undoes the effects of
% `prepare_for_disj_hijack'.
:- type disj_hijack_info.
:- pred code_info__prepare_for_disj_hijack(code_model::in,
disj_hijack_info::out, code_tree::out,
code_info::in, code_info::out) is det.
:- pred code_info__undo_disj_hijack(disj_hijack_info::in,
code_tree::out, code_info::in, code_info::out) is det.
% `prepare_for_ite_hijack' should be called before entering
% an if-then-else. It saves the values of any nondet stack slots
% the if-then-else may hijack, and if necessary, sets the redofr
% slot of the top frame to point to this frame. Our caller
% will then override the redoip slot to point to the start of
% the else part before generating the code of the condition.
%
% `ite_enter_then', which should be called generating code for
% the condition, sets up the failure state of the code generator
% for generating the then-part, and returns the code sequences
% to be used at the starts of the then-part and the else-part
% to undo the effects of any hijacking.
:- type ite_hijack_info.
:- pred code_info__prepare_for_ite_hijack(code_model::in,
ite_hijack_info::out, code_tree::out,
code_info::in, code_info::out) is det.
:- pred code_info__ite_enter_then(ite_hijack_info::in,
code_tree::out, code_tree::out, code_info::in, code_info::out) is det.
% `enter_simple_neg' and `leave_simple_neg' should be called before
% and after generating the code for a negated unification, in
% situations where failure is a direct branch. We handle this case
% specially, because it occurs frequently and should not require
% a flushing of the expression cache, whereas the general way of
% handling negations does require a flush. These two predicates
% handle all aspects of the negation except for the unification
% itself.
:- type simple_neg_info.
:- pred code_info__enter_simple_neg(set(prog_var)::in, hlds_goal_info::in,
simple_neg_info::out, code_info::in, code_info::out) is det.
:- pred code_info__leave_simple_neg(hlds_goal_info::in, simple_neg_info::in,
code_info::in, code_info::out) is det.
% `prepare_for_det_commit' and `generate_det_commit' should be
% called before and after generating the code for the multi goal
% being cut across. If the goal succeeds, the commit will cut
% any choice points generated in the goal.
:- type det_commit_info.
:- pred code_info__prepare_for_det_commit(det_commit_info::out,
code_tree::out, code_info::in, code_info::out) is det.
:- pred code_info__generate_det_commit(det_commit_info::in,
code_tree::out, code_info::in, code_info::out) is det.
% `prepare_for_semi_commit' and `generate_semi_commit' should be
% called before and after generating the code for the nondet goal
% being cut across. If the goal succeeds, the commit will cut
% any choice points generated in the goal.
:- type semi_commit_info.
:- pred code_info__prepare_for_semi_commit(semi_commit_info::out,
code_tree::out, code_info::in, code_info::out) is det.
:- pred code_info__generate_semi_commit(semi_commit_info::in,
code_tree::out, code_info::in, code_info::out) is det.
% Put the given resume point into effect, by pushing it on to
% the resume point stack, and if necessary generating code to
% override the redoip of the top nondet stack frame.
:- pred code_info__effect_resume_point(resume_point_info::in, code_model::in,
code_tree::out, code_info::in, code_info::out) is det.
% Return the details of the resume point currently on top of the
% failure continuation stack.
:- pred code_info__top_resume_point(resume_point_info::out,
code_info::in, code_info::out) is det.
% Call this predicate to say "we have just left a disjunction;
% we don't know what address the following code will need to
% backtrack to".
:- pred code_info__set_resume_point_to_unknown(code_info::in, code_info::out)
is det.
% Call this predicate to say "we have just returned a model_non call;
% we don't know what address the following code will need to
% backtrack to, and there may now be nondet frames on top of ours
% that do not have their redofr slots pointing to our frame".
:- pred code_info__set_resume_point_and_frame_to_unknown(code_info::in,
code_info::out) is det.
% Generate code for executing a failure that is appropriate for the
% current failure environment.
:- pred code_info__generate_failure(code_tree::out,
code_info::in, code_info::out) is det.
% Generate code that checks if the given rval is false, and if yes,
% executes a failure that is appropriate for the current failure
% environment.
:- pred code_info__fail_if_rval_is_false(rval::in, code_tree::out,
code_info::in, code_info::out) is det.
% Checks whether the appropriate code for failure in the current
% failure environment is a direct branch.
:- pred code_info__failure_is_direct_branch(code_addr::out,
code_info::in, code_info::out) is semidet.
% Checks under what circumstances the current failure environment
% would allow a model_non call at this point to be turned into a
% tail call, provided of course that the return from the call is
% followed immediately by succeed().
:- pred code_info__may_use_nondet_tailcall(nondet_tail_call::out,
code_info::in, code_info::out) is det.
% Materialize the given variables into registers or stack slots.
:- pred code_info__produce_vars(set(prog_var)::in, resume_map::out,
code_tree::out, code_info::in, code_info::out) is det.
% Put the variables needed in enclosing failure continuations
% into their stack slots.
:- pred code_info__flush_resume_vars_to_stack(code_tree::out,
code_info::in, code_info::out) is det.
% Set up the resume_point_info structure.
:- pred code_info__make_resume_point(set(prog_var)::in, resume_locs::in,
resume_map::in, resume_point_info::out, code_info::in, code_info::out)
is det.
% Generate the code for a resume point.
:- pred code_info__generate_resume_point(resume_point_info::in,
code_tree::out, code_info::in, code_info::out) is det.
% List the variables that need to be preserved for the given
% resume point.
:- pred code_info__resume_point_vars(resume_point_info::in, list(prog_var)::out)
is det.
% See whether the given resume point includes a code address
% that presumes all the resume point variables to be in their
% stack slots. If yes, return that code address; otherwise,
% abort the compiler.
:- pred code_info__resume_point_stack_addr(resume_point_info::in,
code_addr::out) is det.
%---------------------------------------------------------------------------%
:- implementation.
% The part of the code generator state that says how to handle
% failures; also called the failure continuation stack.
:- type fail_info
---> fail_info(
stack(resume_point_info),
resume_point_known,
curfr_vs_maxfr,
condition_env,
hijack_allowed
).
% A resumption point has one or two labels associated with it.
% Backtracking can arrive at either label. The code following
% each label will assume that the variables needed at the resumption
% point are in the locations given by the resume_map associated with
% the given label and nowhere else. Any code that can cause
% backtracking to a label must make sure that those variables are
% in the positions expected by the label.
%
% The only time when a code_addr in a resume_point info is not a label
% is when the code_addr is do_fail, which indicates that the resumption
% point is not in (this invocation of) this procedure.
:- type resume_point_info
---> orig_only(resume_map, code_addr)
; stack_only(resume_map, code_addr)
; orig_and_stack(resume_map, code_addr, resume_map, code_addr)
; stack_and_orig(resume_map, code_addr, resume_map, code_addr).
% A resume map maps the variables that will be needed at a resumption
% point to the locations in which they will be.
:- type resume_map == map(prog_var, set(rval)).
:- type redoip_update ---> has_been_done
; wont_be_done.
:- type resume_point_known ---> resume_point_known(redoip_update)
; resume_point_unknown.
:- type curfr_vs_maxfr ---> must_be_equal
; may_be_different.
:- type condition_env ---> inside_non_condition
; not_inside_non_condition.
:- type hijack_allowed ---> allowed
; not_allowed.
%---------------------------------------------------------------------------%
:- type disj_hijack_info
---> disj_no_hijack
; disj_temp_frame
; disj_quarter_hijack
; disj_half_hijack(
lval % The stack slot in which we saved
% the value of the hijacked redoip.
)
; disj_full_hijack(
lval, % The stack slot in which we saved
% the value of the hijacked redoip.
lval % The stack slot in which we saved
% the value of the hijacked redofr.
).
code_info__prepare_for_disj_hijack(CodeModel, HijackInfo, Code) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(_, ResumeKnown, CurfrMaxfr, CondEnv, Allow) },
(
{ CodeModel \= model_non }
->
{ HijackInfo = disj_no_hijack },
{ Code = node([
comment("disj no hijack")
- ""
]) }
;
{ Allow = not_allowed ; CondEnv = inside_non_condition }
->
{ HijackInfo = disj_temp_frame },
code_info__create_temp_frame(do_fail,
"prepare for disjunction", Code)
;
{ CurfrMaxfr = must_be_equal },
{ ResumeKnown = resume_point_known(has_been_done) }
->
{ HijackInfo = disj_quarter_hijack },
{ Code = node([
comment("disj quarter hijack")
- ""
]) }
;
{ CurfrMaxfr = must_be_equal }
->
% Here ResumeKnown must be resume_point_unknown
% or resume_point_known(wont_be_done).
code_info__acquire_temp_slot(lval(redoip(lval(curfr))),
RedoipSlot),
{ HijackInfo = disj_half_hijack(RedoipSlot) },
{ Code = node([
assign(RedoipSlot, lval(redoip(lval(curfr))))
- "prepare for half disj hijack"
]) }
;
% Here CurfrMaxfr must be may_be_different.
code_info__acquire_temp_slot(lval(redoip(lval(maxfr))),
RedoipSlot),
code_info__acquire_temp_slot(lval(redofr(lval(maxfr))),
RedofrSlot),
{ HijackInfo = disj_full_hijack(RedoipSlot, RedofrSlot) },
{ Code = node([
assign(RedoipSlot, lval(redoip(lval(maxfr))))
- "prepare for full disj hijack",
assign(RedofrSlot, lval(redofr(lval(maxfr))))
- "prepare for full disj hijack",
assign(redofr(lval(maxfr)), lval(curfr))
- "prepare for full disj hijack"
]) }
).
code_info__undo_disj_hijack(HijackInfo, Code) -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints, ResumeKnown, CurfrMaxfr,
CondEnv, Allow) },
(
{ HijackInfo = disj_no_hijack },
{ Code = empty }
;
{ HijackInfo = disj_temp_frame },
{ Code = node([
assign(maxfr, lval(prevfr(lval(maxfr))))
- "restore maxfr for temp frame disj"
]) }
;
{ HijackInfo = disj_quarter_hijack },
{ require(unify(CurfrMaxfr, must_be_equal),
"maxfr may differ from curfr in disj_quarter_hijack") },
{ stack__top_det(ResumePoints, ResumePoint) },
{ code_info__pick_stack_resume_point(ResumePoint,
_, StackLabel) },
{ LabelConst = const(code_addr_const(StackLabel)) },
{ Code = node([
assign(redoip(lval(curfr)), LabelConst)
- "restore redoip for quarter disj hijack"
]) }
;
{ HijackInfo = disj_half_hijack(RedoipSlot) },
{ require(unify(ResumeKnown, resume_point_unknown),
"resume point known in disj_half_hijack") },
{ require(unify(CurfrMaxfr, must_be_equal),
"maxfr may differ from curfr in disj_half_hijack") },
{ Code = node([
assign(redoip(lval(curfr)), lval(RedoipSlot))
- "restore redoip for half disj hijack"
]) }
;
{ HijackInfo = disj_full_hijack(RedoipSlot, RedofrSlot) },
{ require(unify(CurfrMaxfr, may_be_different),
"maxfr same as curfr in disj_full_hijack") },
{ Code = node([
assign(redoip(lval(maxfr)), lval(RedoipSlot))
- "restore redoip for full disj hijack",
assign(redofr(lval(maxfr)), lval(RedofrSlot))
- "restore redofr for full disj hijack"
]) }
),
(
% HijackInfo \= disj_no_hijack if and only if
% the disjunction is model_non.
{ HijackInfo \= disj_no_hijack },
{ CondEnv = inside_non_condition }
->
{ FailInfo = fail_info(ResumePoints, resume_point_unknown,
CurfrMaxfr, CondEnv, Allow) },
code_info__set_fail_info(FailInfo)
;
[]
).
%---------------------------------------------------------------------------%
:- type ite_hijack_info
---> ite_info(
resume_point_known,
condition_env,
ite_hijack_type
).
:- type ite_hijack_type
---> ite_no_hijack
; ite_temp_frame(
lval % The stack slot in which we saved
% the value of maxfr.
)
; ite_quarter_hijack
; ite_half_hijack(
lval % The stack slot in which we saved
% the value of the hijacked redoip.
)
; ite_full_hijack(
lval, % The stack slot in which we saved
% the value of the hijacked redoip.
lval, % The stack slot in which we saved
% the value of the hijacked redofr.
lval % The stack slot in which we saved
% the value of maxfr.
).
code_info__prepare_for_ite_hijack(EffCodeModel, HijackInfo, Code) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(_, ResumeKnown, CurfrMaxfr, CondEnv, Allow) },
(
{ EffCodeModel \= model_non }
->
{ HijackType = ite_no_hijack },
{ Code = node([
comment("ite no hijack")
- ""
]) }
;
{ Allow = not_allowed ; CondEnv = inside_non_condition }
->
code_info__acquire_temp_slot(lval(maxfr), MaxfrSlot),
{ HijackType = ite_temp_frame(MaxfrSlot) },
code_info__create_temp_frame(do_fail, "prepare for ite",
TempFrameCode),
{ MaxfrCode = node([
assign(MaxfrSlot, lval(maxfr))
- "prepare for ite"
]) },
{ Code = tree(TempFrameCode, MaxfrCode) }
;
{ CurfrMaxfr = must_be_equal },
{ ResumeKnown = resume_point_known(_) }
->
{ HijackType = ite_quarter_hijack },
{ Code = node([
comment("ite quarter hijack")
- ""
]) }
;
{ CurfrMaxfr = must_be_equal }
->
% Here ResumeKnown must be resume_point_unknown.
code_info__acquire_temp_slot(lval(redoip(lval(curfr))),
RedoipSlot),
{ HijackType = ite_half_hijack(RedoipSlot) },
{ Code = node([
assign(RedoipSlot, lval(redoip(lval(curfr))))
- "prepare for half ite hijack"
]) }
;
% Here CurfrMaxfr must be may_be_different.
code_info__acquire_temp_slot(lval(redoip(lval(maxfr))),
RedoipSlot),
code_info__acquire_temp_slot(lval(redofr(lval(maxfr))),
RedofrSlot),
code_info__acquire_temp_slot(lval(maxfr),
MaxfrSlot),
{ HijackType = ite_full_hijack(RedoipSlot, RedofrSlot,
MaxfrSlot) },
{ Code = node([
assign(MaxfrSlot, lval(maxfr))
- "prepare for full ite hijack",
assign(RedoipSlot, lval(redoip(lval(maxfr))))
- "prepare for full ite hijack",
assign(RedofrSlot, lval(redofr(lval(maxfr))))
- "prepare for full ite hijack",
assign(redofr(lval(maxfr)), lval(curfr))
- "prepare for full ite hijack"
]) }
),
{ HijackInfo = ite_info(ResumeKnown, CondEnv, HijackType) },
( { EffCodeModel = model_non } ->
code_info__inside_non_condition
;
[]
).
code_info__ite_enter_then(HijackInfo, ThenCode, ElseCode) -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints0, ResumeKnown0, CurfrMaxfr,
_, Allow) },
{ stack__pop_det(ResumePoints0, _, ResumePoints) },
{ HijackInfo = ite_info(HijackResumeKnown, OldCondEnv, HijackType) },
{
HijackType = ite_no_hijack,
ThenCode = empty,
ElseCode = ThenCode
;
HijackType = ite_temp_frame(MaxfrSlot),
ThenCode = node([
% We can't remove the frame, it may not be on top.
assign(redoip(lval(MaxfrSlot)),
const(code_addr_const(do_fail)))
- "soft cut for temp frame ite"
]),
ElseCode = node([
assign(maxfr, lval(prevfr(lval(MaxfrSlot))))
- "restore maxfr for temp frame ite"
])
;
HijackType = ite_quarter_hijack,
stack__top_det(ResumePoints, ResumePoint),
(
code_info__maybe_pick_stack_resume_point(ResumePoint,
_, StackLabel)
->
LabelConst = const(code_addr_const(StackLabel)),
ThenCode = node([
assign(redoip(lval(curfr)), LabelConst) -
"restore redoip for quarter ite hijack"
])
;
% This can happen only if ResumePoint is unreachable
% from here.
ThenCode = empty
),
ElseCode = ThenCode
;
HijackType = ite_half_hijack(RedoipSlot),
ThenCode = node([
assign(redoip(lval(curfr)), lval(RedoipSlot))
- "restore redoip for half ite hijack"
]),
ElseCode = ThenCode
;
HijackType = ite_full_hijack(RedoipSlot, RedofrSlot,
MaxfrSlot),
ThenCode = node([
assign(redoip(lval(MaxfrSlot)), lval(RedoipSlot))
- "restore redoip for full ite hijack",
assign(redofr(lval(MaxfrSlot)), lval(RedofrSlot))
- "restore redofr for full ite hijack"
]),
ElseCode = node([
assign(redoip(lval(maxfr)), lval(RedoipSlot))
- "restore redoip for full ite hijack",
assign(redofr(lval(maxfr)), lval(RedofrSlot))
- "restore redofr for full ite hijack"
])
},
{ ResumeKnown0 = resume_point_unknown ->
ResumeKnown = resume_point_unknown
;
ResumeKnown = HijackResumeKnown
},
{ FailInfo = fail_info(ResumePoints, ResumeKnown, CurfrMaxfr,
OldCondEnv, Allow) },
code_info__set_fail_info(FailInfo).
%---------------------------------------------------------------------------%
:- type simple_neg_info == fail_info.
code_info__enter_simple_neg(ResumeVars, GoalInfo, FailInfo0) -->
code_info__get_fail_info(FailInfo0),
% The only reason why we push a resume point at all
% is to protect the variables in ResumeVars from becoming
% unknown; by including them in the domain of the resume point,
% we guarantee that they will become zombies instead of
% unknown if they die in the pre- or post-goal updates.
% Therefore the only part of ResumePoint that matters
% is the set of variables in the resume map; the other
% parts of ResumePoint (the locations, the code address)
% will not be referenced.
{ set__to_sorted_list(ResumeVars, ResumeVarList) },
{ map__init(ResumeMap0) },
{ code_info__make_fake_resume_map(ResumeVarList,
ResumeMap0, ResumeMap) },
{ ResumePoint = orig_only(ResumeMap, do_redo) },
code_info__effect_resume_point(ResumePoint, model_semi, Code),
{ require(unify(Code, empty), "nonempty code for simple neg") },
code_info__pre_goal_update(GoalInfo, yes).
code_info__leave_simple_neg(GoalInfo, FailInfo) -->
code_info__post_goal_update(GoalInfo),
code_info__set_fail_info(FailInfo).
:- pred code_info__make_fake_resume_map(list(prog_var)::in,
map(prog_var, set(rval))::in, map(prog_var, set(rval))::out) is det.
code_info__make_fake_resume_map([], ResumeMap, ResumeMap).
code_info__make_fake_resume_map([Var | Vars], ResumeMap0, ResumeMap) :-
% a visibly fake location
set__singleton_set(Locns, lval(reg(r, -1))),
map__det_insert(ResumeMap0, Var, Locns, ResumeMap1),
code_info__make_fake_resume_map(Vars, ResumeMap1, ResumeMap).
%---------------------------------------------------------------------------%
:- type det_commit_info
---> det_commit_info(
maybe(lval), % Location of saved maxfr.
maybe(pair(lval)) % Location of saved ticket
% counter and trail pointer.
).
code_info__prepare_for_det_commit(DetCommitInfo, Code) -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(_, _, CurfrMaxfr, _, _) },
(
{ CurfrMaxfr = may_be_different },
code_info__acquire_temp_slot(lval(maxfr), MaxfrSlot),
{ SaveMaxfrCode = node([
assign(MaxfrSlot, lval(maxfr))
- "save the value of maxfr"
]) },
{ MaybeMaxfrSlot = yes(MaxfrSlot) }
;
{ CurfrMaxfr = must_be_equal },
{ SaveMaxfrCode = empty },
{ MaybeMaxfrSlot = no }
),
code_info__maybe_save_trail_info(MaybeTrailSlots, SaveTrailCode),
{ DetCommitInfo = det_commit_info(MaybeMaxfrSlot, MaybeTrailSlots) },
{ Code = tree(SaveMaxfrCode, SaveTrailCode) }.
code_info__generate_det_commit(DetCommitInfo, Code) -->
{ DetCommitInfo = det_commit_info(MaybeMaxfrSlot, MaybeTrailSlots) },
(
{ MaybeMaxfrSlot = yes(MaxfrSlot) },
{ RestoreMaxfrCode = node([
assign(maxfr, lval(MaxfrSlot))
- "restore the value of maxfr - perform commit"
]) },
code_info__release_temp_slot(MaxfrSlot)
;
{ MaybeMaxfrSlot = no },
{ RestoreMaxfrCode = node([
assign(maxfr, lval(curfr))
- "restore the value of maxfr - perform commit"
]) }
),
code_info__maybe_restore_trail_info(MaybeTrailSlots,
CommitTrailCode, _),
{ Code = tree(RestoreMaxfrCode, CommitTrailCode) }.
%---------------------------------------------------------------------------%
:- type semi_commit_info
---> semi_commit_info(
fail_info, % Fail_info on entry.
resume_point_info,
commit_hijack_info,
maybe(pair(lval)) % Location of saved ticket
% counter and trail pointer.
).
:- type commit_hijack_info
---> commit_temp_frame(
lval, % The stack slot in which we saved
% the old value of maxfr.
bool % Do we bracket the goal with
% MR_commit_mark and MR_commit_cut?
)
; commit_quarter_hijack
; commit_half_hijack(
lval % The stack slot in which we saved
% the value of the hijacked redoip.
)
; commit_full_hijack(
lval, % The stack slot in which we saved
% the value of the hijacked redoip.
lval, % The stack slot in which we saved
% the value of the hijacked redofr.
lval % The stack slot in which we saved
% the value of maxfr.
).
code_info__prepare_for_semi_commit(SemiCommitInfo, Code) -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints0, ResumeKnown, CurfrMaxfr,
CondEnv, Allow) },
{ stack__top_det(ResumePoints0, TopResumePoint) },
code_info__clone_resume_point(TopResumePoint, NewResumePoint),
{ stack__push(ResumePoints0, NewResumePoint, ResumePoints) },
{ FailInfo = fail_info(ResumePoints, resume_point_known(has_been_done),
CurfrMaxfr, CondEnv, Allow) },
code_info__set_fail_info(FailInfo),
{ code_info__pick_stack_resume_point(NewResumePoint, _, StackLabel) },
{ StackLabelConst = const(code_addr_const(StackLabel)) },
(
{ Allow = not_allowed ; CondEnv = inside_non_condition }
->
code_info__acquire_temp_slot(lval(maxfr), MaxfrSlot),
{ MaxfrCode = node([
assign(MaxfrSlot, lval(maxfr))
- "prepare for temp frame commit"
]) },
code_info__create_temp_frame(StackLabel,
"prepare for temp frame commit", TempFrameCode),
code_info__get_globals(Globals),
{ globals__lookup_bool_option(Globals, use_minimal_model,
UseMinimalModel) },
{ HijackInfo = commit_temp_frame(MaxfrSlot, UseMinimalModel) },
{
UseMinimalModel = yes,
% If the code we are committing across starts but
% does not complete the evaluation of a tabled subgoal,
% the cut will remove the generator's choice point,
% so that the evaluation of the subgoal will never
% be completed. We handle such "dangling" generators
% by removing them from the subgoal trie of the
% tabled procedure. This requires knowing what
% tabled subgoals are started inside commits,
% which is why we wrap the goal being committed across
% inside MR_commit_{mark,cut}.
Components = [
pragma_c_raw_code(
"\tsave_transient_registers();\n"),
pragma_c_raw_code(
"\tMR_commit_mark();\n"),
pragma_c_raw_code(
"\trestore_transient_registers();\n")
],
MarkCode = node([
pragma_c([], Components, will_not_call_mercury,
no, no, no) - ""
])
;
UseMinimalModel = no,
MarkCode = empty
},
{ HijackCode = tree(MaxfrCode, tree(TempFrameCode, MarkCode)) }
;
{ ResumeKnown = resume_point_known(has_been_done) },
{ CurfrMaxfr = must_be_equal }
->
{ HijackInfo = commit_quarter_hijack },
{ HijackCode = node([
assign(redoip(lval(curfr)), StackLabelConst)
- "hijack the redofr slot"
]) }
;
{ CurfrMaxfr = must_be_equal }
->
% Here ResumeKnown must be resume_point_unknown or
% resume_point_known(wont_be_done).
code_info__acquire_temp_slot(lval(redoip(lval(curfr))),
RedoipSlot),
{ HijackInfo = commit_half_hijack(RedoipSlot) },
{ HijackCode = node([
assign(RedoipSlot, lval(redoip(lval(curfr))))
- "prepare for half commit hijack",
assign(redoip(lval(curfr)), StackLabelConst)
- "hijack the redofr slot"
]) }
;
% Here CurfrMaxfr must be may_be_different.
code_info__acquire_temp_slot(lval(redoip(lval(maxfr))),
RedoipSlot),
code_info__acquire_temp_slot(lval(redofr(lval(maxfr))),
RedofrSlot),
code_info__acquire_temp_slot(lval(maxfr), MaxfrSlot),
{ HijackInfo = commit_full_hijack(RedoipSlot, RedofrSlot,
MaxfrSlot) },
{ HijackCode = node([
assign(RedoipSlot, lval(redoip(lval(maxfr))))
- "prepare for full commit hijack",
assign(RedofrSlot, lval(redofr(lval(maxfr))))
- "prepare for full commit hijack",
assign(MaxfrSlot, lval(maxfr))
- "prepare for full commit hijack",
assign(redofr(lval(maxfr)), lval(curfr))
- "hijack the redofr slot",
assign(redoip(lval(maxfr)), StackLabelConst)
- "hijack the redoip slot"
]) }
),
code_info__maybe_save_trail_info(MaybeTrailSlots, SaveTrailCode),
{ SemiCommitInfo = semi_commit_info(FailInfo0, NewResumePoint,
HijackInfo, MaybeTrailSlots) },
{ Code = tree(HijackCode, SaveTrailCode) }.
code_info__generate_semi_commit(SemiCommitInfo, Code) -->
{ SemiCommitInfo = semi_commit_info(FailInfo, ResumePoint,
HijackInfo, MaybeTrailSlots) },
code_info__set_fail_info(FailInfo),
% XXX should release the temp slots in each arm of the switch
(
{ HijackInfo = commit_temp_frame(MaxfrSlot, UseMinimalModel) },
{ MaxfrCode = node([
assign(maxfr, lval(MaxfrSlot))
- "restore maxfr for temp frame hijack"
]) },
{
UseMinimalModel = yes,
% See the comment in prepare_for_semi_commit above.
Components = [
pragma_c_raw_code("\tMR_commit_cut();\n")
],
CutCode = node([
pragma_c([], Components, will_not_call_mercury,
no, no, no)
- "commit for temp frame hijack"
])
;
UseMinimalModel = no,
CutCode = empty
},
{ SuccessUndoCode = tree(MaxfrCode, CutCode) },
{ FailureUndoCode = tree(MaxfrCode, CutCode) }
;
{ HijackInfo = commit_quarter_hijack },
{ FailInfo = fail_info(ResumePoints, _, _, _, _) },
{ stack__top_det(ResumePoints, TopResumePoint) },
{ code_info__pick_stack_resume_point(TopResumePoint,
_, StackLabel) },
{ StackLabelConst = const(code_addr_const(StackLabel)) },
{ SuccessUndoCode = node([
assign(maxfr, lval(curfr))
- "restore maxfr for quarter commit hijack",
assign(redoip(lval(maxfr)), StackLabelConst)
- "restore redoip for quarter commit hijack"
]) },
{ FailureUndoCode = node([
assign(redoip(lval(maxfr)), StackLabelConst)
- "restore redoip for quarter commit hijack"
]) }
;
{ HijackInfo = commit_half_hijack(RedoipSlot) },
{ SuccessUndoCode = node([
assign(maxfr, lval(curfr))
- "restore maxfr for half commit hijack",
assign(redoip(lval(maxfr)), lval(RedoipSlot))
- "restore redoip for half commit hijack"
]) },
{ FailureUndoCode = node([
assign(redoip(lval(maxfr)), lval(RedoipSlot))
- "restore redoip for half commit hijack"
]) }
;
{ HijackInfo = commit_full_hijack(RedoipSlot, RedofrSlot,
MaxfrSlot) },
{ SuccessUndoCode = node([
assign(maxfr, lval(MaxfrSlot))
- "restore maxfr for full commit hijack",
assign(redoip(lval(maxfr)), lval(RedoipSlot))
- "restore redoip for full commit hijack",
assign(redofr(lval(maxfr)), lval(RedofrSlot))
- "restore redofr for full commit hijack"
]) },
{ FailureUndoCode = node([
assign(redoip(lval(maxfr)), lval(RedoipSlot))
- "restore redoip for full commit hijack",
assign(redofr(lval(maxfr)), lval(RedofrSlot))
- "restore redofr for full commit hijack"
]) }
),
code_info__remember_position(AfterCommit),
code_info__generate_resume_point(ResumePoint, ResumePointCode),
code_info__generate_failure(FailCode),
code_info__reset_to_position(AfterCommit),
code_info__maybe_restore_trail_info(MaybeTrailSlots,
CommitTrailCode, RestoreTrailCode),
code_info__get_next_label(SuccLabel),
{ GotoSuccLabel = node([
goto(label(SuccLabel)) - "Jump to success continuation"
]) },
{ SuccLabelCode = node([
label(SuccLabel) - "Success continuation"
]) },
{ SuccessCode =
tree(SuccessUndoCode,
CommitTrailCode)
},
{ FailureCode =
tree(ResumePointCode,
tree(FailureUndoCode,
tree(RestoreTrailCode,
FailCode)))
},
{ Code =
tree(SuccessCode,
tree(GotoSuccLabel,
tree(FailureCode,
SuccLabelCode)))
}.
%---------------------------------------------------------------------------%
:- pred code_info__inside_non_condition(code_info::in, code_info::out) is det.
code_info__inside_non_condition -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints, ResumeKnown, CurfrMaxfr,
_, Allow) },
{ FailInfo = fail_info(ResumePoints, ResumeKnown, CurfrMaxfr,
inside_non_condition, Allow) },
code_info__set_fail_info(FailInfo).
:- pred code_info__create_temp_frame(code_addr::in, string::in, code_tree::out,
code_info::in, code_info::out) is det.
code_info__create_temp_frame(Redoip, Comment, Code) -->
code_info__get_proc_model(ProcModel),
{ ProcModel = model_non ->
Kind = nondet_stack_proc
;
Kind = det_stack_proc
},
{ Code = node([
mkframe(temp_frame(Kind), Redoip)
- Comment
]) },
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints, ResumeKnown, _, CondEnv, Allow) },
{ FailInfo = fail_info(ResumePoints, ResumeKnown, may_be_different,
CondEnv, Allow) },
code_info__set_fail_info(FailInfo).
%---------------------------------------------------------------------------%
code_info__effect_resume_point(ResumePoint, CodeModel, Code) -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints0, _ResumeKnown, CurfrMaxfr,
CondEnv, Allow) },
{ stack__top(ResumePoints0, OldResumePoint) ->
code_info__pick_first_resume_point(OldResumePoint, OldMap, _),
code_info__pick_first_resume_point(ResumePoint, NewMap, _),
map__keys(OldMap, OldKeys),
map__keys(NewMap, NewKeys),
set__list_to_set(OldKeys, OldKeySet),
set__list_to_set(NewKeys, NewKeySet),
require(set__subset(OldKeySet, NewKeySet),
"non-nested resume point variable sets")
;
true
},
{ stack__push(ResumePoints0, ResumePoint, ResumePoints) },
( { CodeModel = model_non } ->
{ code_info__pick_stack_resume_point(ResumePoint,
_, StackLabel) },
{ LabelConst = const(code_addr_const(StackLabel)) },
{ Code = node([
assign(redoip(lval(maxfr)), LabelConst)
- "hijack redoip to effect resume point"
]) },
{ RedoipUpdate = has_been_done }
;
{ Code = empty },
{ RedoipUpdate = wont_be_done }
),
{ FailInfo = fail_info(ResumePoints, resume_point_known(RedoipUpdate),
CurfrMaxfr, CondEnv, Allow) },
code_info__set_fail_info(FailInfo).
%---------------------------------------------------------------------------%
code_info__top_resume_point(ResumePoint) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(ResumePoints, _, _, _, _) },
{ stack__top_det(ResumePoints, ResumePoint) }.
code_info__set_resume_point_to_unknown -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints, _, CurfrMaxfr, CondEnv, Allow) },
{ FailInfo = fail_info(ResumePoints, resume_point_unknown,
CurfrMaxfr, CondEnv, Allow) },
code_info__set_fail_info(FailInfo).
code_info__set_resume_point_and_frame_to_unknown -->
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(ResumePoints, _, _, CondEnv, Allow) },
{ FailInfo = fail_info(ResumePoints, resume_point_unknown,
may_be_different, CondEnv, Allow) },
code_info__set_fail_info(FailInfo).
%---------------------------------------------------------------------------%
code_info__generate_failure(Code) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(ResumePoints, ResumeKnown, _, _, _) },
(
{ ResumeKnown = resume_point_known(_) },
{ stack__top_det(ResumePoints, TopResumePoint) },
(
code_info__pick_matching_resume_addr(TopResumePoint,
FailureAddress0)
->
{ FailureAddress = FailureAddress0 },
{ PlaceCode = empty }
;
{ code_info__pick_first_resume_point(TopResumePoint,
Map, FailureAddress) },
{ map__to_assoc_list(Map, AssocList) },
code_info__remember_position(CurPos),
code_info__place_vars(AssocList, PlaceCode),
code_info__reset_to_position(CurPos)
),
{ BranchCode = node([goto(FailureAddress) - "fail"]) },
{ Code = tree(PlaceCode, BranchCode) }
;
{ ResumeKnown = resume_point_unknown },
{ Code = node([goto(do_redo) - "fail"]) }
).
code_info__fail_if_rval_is_false(Rval0, Code) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(ResumePoints, ResumeKnown, _, _, _) },
(
{ ResumeKnown = resume_point_known(_) },
{ stack__top_det(ResumePoints, TopResumePoint) },
(
code_info__pick_matching_resume_addr(TopResumePoint,
FailureAddress0)
->
% We branch away if the test *fails*
{ code_util__neg_rval(Rval0, Rval) },
{ Code = node([
if_val(Rval, FailureAddress0) -
"Test for failure"
]) }
;
{ code_info__pick_first_resume_point(TopResumePoint,
Map, FailureAddress) },
{ map__to_assoc_list(Map, AssocList) },
code_info__get_next_label(SuccessLabel),
code_info__remember_position(CurPos),
code_info__place_vars(AssocList, PlaceCode),
code_info__reset_to_position(CurPos),
{ SuccessAddress = label(SuccessLabel) },
% We branch away if the test *fails*,
% therefore if the test succeeds, we branch
% around the code that moves variables to
% their failure locations and branches away
% to the failure continuation
{ TestCode = node([
if_val(Rval0, SuccessAddress) -
"Test for failure"
]) },
{ TailCode = node([
goto(FailureAddress) -
"Goto failure",
label(SuccessLabel) -
"Success continuation"
]) },
{ Code = tree(TestCode, tree(PlaceCode, TailCode)) }
)
;
{ ResumeKnown = resume_point_unknown },
% We branch away if the test *fails*
{ code_util__neg_rval(Rval0, Rval) },
{ Code = node([
if_val(Rval, do_redo) -
"Test for failure"
]) }
).
%---------------------------------------------------------------------------%
code_info__failure_is_direct_branch(CodeAddr) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(ResumePoints, resume_point_known(_), _, _, _) },
{ stack__top(ResumePoints, TopResumePoint) },
code_info__pick_matching_resume_addr(TopResumePoint, CodeAddr).
code_info__may_use_nondet_tailcall(TailCallStatus) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(ResumePoints0, ResumeKnown, _, _, _) },
{
stack__pop(ResumePoints0, ResumePoint1, ResumePoints1),
stack__is_empty(ResumePoints1),
ResumePoint1 = stack_only(_, do_fail)
->
(
ResumeKnown = resume_point_known(_),
TailCallStatus = unchecked_tail_call
;
ResumeKnown = resume_point_unknown,
TailCallStatus = checked_tail_call
)
;
TailCallStatus = no_tail_call
}.
%---------------------------------------------------------------------------%
% See whether the current locations of variables match the locations
% associated with any of the options in the given failure map.
% If yes, return the code_addr of that option.
:- pred code_info__pick_matching_resume_addr(resume_point_info::in,
code_addr::out, code_info::in, code_info::out) is semidet.
code_info__pick_matching_resume_addr(ResumeMaps, Addr) -->
code_info__variable_locations(Locations),
{
ResumeMaps = orig_only(Map1, Addr1),
( code_info__match_resume_loc(Map1, Locations) ->
Addr = Addr1
;
fail
)
;
ResumeMaps = stack_only(Map1, Addr1),
( code_info__match_resume_loc(Map1, Locations) ->
Addr = Addr1
;
fail
)
;
ResumeMaps = orig_and_stack(Map1, Addr1, Map2, Addr2),
( code_info__match_resume_loc(Map1, Locations) ->
Addr = Addr1
; code_info__match_resume_loc(Map2, Locations) ->
Addr = Addr2
;
fail
)
;
ResumeMaps = stack_and_orig(Map1, Addr1, Map2, Addr2),
( code_info__match_resume_loc(Map1, Locations) ->
Addr = Addr1
; code_info__match_resume_loc(Map2, Locations) ->
Addr = Addr2
;
fail
)
}.
:- pred code_info__match_resume_loc(resume_map::in, resume_map::in) is semidet.
code_info__match_resume_loc(Map, Locations0) :-
map__keys(Map, KeyList),
set__list_to_set(KeyList, Keys),
map__select(Locations0, Keys, Locations),
map__to_assoc_list(Locations, List),
\+ (
list__member(Var - Actual, List),
\+ (
map__search(Map, Var, Rvals),
set__subset(Rvals, Actual)
)
).
:- pred code_info__pick_first_resume_point(resume_point_info::in,
resume_map::out, code_addr::out) is det.
code_info__pick_first_resume_point(orig_only(Map, Addr), Map, Addr).
code_info__pick_first_resume_point(stack_only(Map, Addr), Map, Addr).
code_info__pick_first_resume_point(orig_and_stack(Map, Addr, _, _), Map, Addr).
code_info__pick_first_resume_point(stack_and_orig(Map, Addr, _, _), Map, Addr).
:- pred code_info__pick_stack_resume_point(resume_point_info::in,
resume_map::out, code_addr::out) is det.
code_info__pick_stack_resume_point(ResumePoint, Map, Addr) :-
( code_info__maybe_pick_stack_resume_point(ResumePoint, Map1, Addr1) ->
Map = Map1,
Addr = Addr1
;
error("no stack resume point")
).
:- pred code_info__maybe_pick_stack_resume_point(resume_point_info::in,
resume_map::out, code_addr::out) is semidet.
code_info__maybe_pick_stack_resume_point(stack_only(Map, Addr), Map, Addr).
code_info__maybe_pick_stack_resume_point(orig_and_stack(_, _, Map, Addr),
Map, Addr).
code_info__maybe_pick_stack_resume_point(stack_and_orig(Map, Addr, _, _),
Map, Addr).
%---------------------------------------------------------------------------%
code_info__produce_vars(Vars, Map, Code) -->
{ set__to_sorted_list(Vars, VarList) },
code_info__produce_vars_2(VarList, Map, Code).
:- pred code_info__produce_vars_2(list(prog_var)::in,
map(prog_var, set(rval))::out,
code_tree::out, code_info::in, code_info::out) is det.
code_info__produce_vars_2([], Map, empty) -->
{ map__init(Map) }.
code_info__produce_vars_2([V | Vs], Map, Code) -->
code_info__produce_vars_2(Vs, Map0, Code0),
code_info__produce_variable_in_reg_or_stack(V, Code1, Rval),
{ set__singleton_set(Rvals, Rval) },
{ map__set(Map0, V, Rvals, Map) },
{ Code = tree(Code0, Code1) }.
code_info__flush_resume_vars_to_stack(Code) -->
code_info__get_fail_info(FailInfo),
{ FailInfo = fail_info(ResumePointStack, _, _, _, _) },
{ stack__top_det(ResumePointStack, ResumePoint) },
{ code_info__pick_stack_resume_point(ResumePoint, StackMap, _) },
{ map__to_assoc_list(StackMap, StackLocs) },
code_info__place_vars(StackLocs, Code).
%---------------------------------------------------------------------------%
:- pred code_info__init_fail_info(code_model::in, maybe(set(prog_var))::in,
resume_point_info::out, code_info::in, code_info::out) is det.
code_info__init_fail_info(CodeModel, MaybeFailVars, ResumePoint) -->
(
{ CodeModel = model_det },
code_info__get_next_label(ResumeLabel),
{ ResumeAddress = label(ResumeLabel) },
{ ResumeKnown = resume_point_unknown },
{ CurfrMaxfr = may_be_different }
;
{ CodeModel = model_semi },
% The resume point for this label
% will be part of the procedure epilog.
code_info__get_next_label(ResumeLabel),
{ ResumeAddress = label(ResumeLabel) },
{ ResumeKnown = resume_point_known(wont_be_done) },
{ CurfrMaxfr = may_be_different }
;
{ CodeModel = model_non },
( { MaybeFailVars = yes(_) } ->
code_info__get_next_label(ResumeLabel),
{ ResumeAddress = label(ResumeLabel) }
;
{ ResumeAddress = do_fail }
),
{ ResumeKnown = resume_point_known(has_been_done) },
{ CurfrMaxfr = must_be_equal }
),
( { MaybeFailVars = yes(FailVars) } ->
code_info__get_stack_slots(StackSlots),
{ map__select(StackSlots, FailVars, StackMap0) },
{ map__to_assoc_list(StackMap0, StackList0) },
{ code_info__make_singleton_sets(StackList0, StackList) },
{ map__from_assoc_list(StackList, StackMap) }
;
{ map__init(StackMap) }
),
{ ResumePoint = stack_only(StackMap, ResumeAddress) },
{ stack__init(ResumeStack0) },
{ stack__push(ResumeStack0, ResumePoint, ResumeStack) },
code_info__get_fail_info(FailInfo0),
{ FailInfo0 = fail_info(_, _, _, _, Allow) },
{ FailInfo = fail_info(ResumeStack, ResumeKnown, CurfrMaxfr,
not_inside_non_condition, Allow) },
code_info__set_fail_info(FailInfo).
%---------------------------------------------------------------------------%
code_info__make_resume_point(ResumeVars, ResumeLocs, FullMap, ResumePoint) -->
code_info__get_stack_slots(StackSlots),
{ map__select(FullMap, ResumeVars, OrigMap) },
(
{ ResumeLocs = orig_only },
code_info__get_next_label(OrigLabel),
{ OrigAddr = label(OrigLabel) },
{ ResumePoint = orig_only(OrigMap, OrigAddr) }
;
{ ResumeLocs = stack_only },
{ map__select(StackSlots, ResumeVars, StackMap0) },
{ map__to_assoc_list(StackMap0, StackList0) },
{ code_info__make_singleton_sets(StackList0, StackList) },
{ map__from_assoc_list(StackList, StackMap) },
code_info__get_next_label(StackLabel),
{ StackAddr = label(StackLabel) },
{ ResumePoint = stack_only(StackMap, StackAddr) }
;
{ ResumeLocs = orig_and_stack },
{ map__select(StackSlots, ResumeVars, StackMap0) },
{ map__to_assoc_list(StackMap0, StackList0) },
{ code_info__make_singleton_sets(StackList0, StackList) },
{ map__from_assoc_list(StackList, StackMap) },
code_info__get_next_label(OrigLabel),
{ OrigAddr = label(OrigLabel) },
code_info__get_next_label(StackLabel),
{ StackAddr = label(StackLabel) },
{ ResumePoint = orig_and_stack(OrigMap, OrigAddr,
StackMap, StackAddr) }
;
{ ResumeLocs = stack_and_orig },
{ map__select(StackSlots, ResumeVars, StackMap0) },
{ map__to_assoc_list(StackMap0, StackList0) },
{ code_info__make_singleton_sets(StackList0, StackList) },
{ map__from_assoc_list(StackList, StackMap) },
code_info__get_next_label(StackLabel),
{ StackAddr = label(StackLabel) },
code_info__get_next_label(OrigLabel),
{ OrigAddr = label(OrigLabel) },
{ ResumePoint = stack_and_orig(StackMap, StackAddr,
OrigMap, OrigAddr) }
).
:- pred code_info__make_singleton_sets(assoc_list(prog_var, lval)::in,
assoc_list(prog_var, set(rval))::out) is det.
code_info__make_singleton_sets([], []).
code_info__make_singleton_sets([V - L | Rest0], [V - Rs | Rest]) :-
set__singleton_set(Rs, lval(L)),
code_info__make_singleton_sets(Rest0, Rest).
%---------------------------------------------------------------------------%
% The code we generate for a resumption point looks like this:
%
% label(StackLabel)
% <assume variables are where StackMap says they are>
% <copy variables to their locations according to OrigMap>
% label(OrigLabel)
% <assume variables are where OrigMap says they are>
%
% Failures at different points may cause control to arrive at
% the resumption point via either label, which is why the last
% line is necessary.
%
% The idea is that failures from other procedures will go to
% StackLabel, and that failures from this procedure while
% everything is in its original place will go to OrigLabel.
% Failures from this procedure where not everything is in its
% original place can go to either, after moving the resume variables
% to the places where the label expects them.
%
% The above layout (stack, then orig) is the most common. However,
% liveness.m may decide that one or other of the two labels will
% never be referred to (e.g. because there are no calls inside
% the range of effect of the resumption point or because a call
% follows immediately after the establishment of the resumption
% point), or that it would be more efficient to put the two labels
% in the other order (e.g. because the code after the resumption point
% needs most of the variables in their stack slots).
code_info__generate_resume_point(ResumePoint, Code) -->
(
{ ResumePoint = orig_only(Map1, Addr1) },
{ extract_label_from_code_addr(Addr1, Label1) },
{ Code = node([
label(Label1) -
"orig only failure continuation"
]) },
code_info__set_var_locations(Map1)
;
{ ResumePoint = stack_only(Map1, Addr1) },
{ extract_label_from_code_addr(Addr1, Label1) },
{ Code = node([
label(Label1) -
"stack only failure continuation"
]) },
code_info__set_var_locations(Map1),
code_info__generate_resume_layout(Label1, Map1)
;
{ ResumePoint = stack_and_orig(Map1, Addr1, Map2, Addr2) },
{ extract_label_from_code_addr(Addr1, Label1) },
{ extract_label_from_code_addr(Addr2, Label2) },
{ Label1Code = node([
label(Label1) -
"stack failure continuation before orig"
]) },
code_info__set_var_locations(Map1),
code_info__generate_resume_layout(Label1, Map1),
{ map__to_assoc_list(Map2, AssocList2) },
code_info__place_resume_vars(AssocList2, PlaceCode),
{ Label2Code = node([
label(Label2) -
"orig failure continuation after stack"
]) },
code_info__set_var_locations(Map2),
{ Code = tree(Label1Code, tree(PlaceCode, Label2Code)) }
;
{ ResumePoint = orig_and_stack(Map1, Addr1, Map2, Addr2) },
{ extract_label_from_code_addr(Addr1, Label1) },
{ extract_label_from_code_addr(Addr2, Label2) },
{ Label1Code = node([
label(Label1) -
"orig failure continuation before stack"
]) },
code_info__set_var_locations(Map1),
{ map__to_assoc_list(Map2, AssocList2) },
code_info__place_resume_vars(AssocList2, PlaceCode),
{ Label2Code = node([
label(Label2) -
"stack failure continuation after orig"
]) },
code_info__set_var_locations(Map2),
code_info__generate_resume_layout(Label2, Map2),
{ Code = tree(Label1Code, tree(PlaceCode, Label2Code)) }
).
:- pred extract_label_from_code_addr(code_addr::in, label::out) is det.
extract_label_from_code_addr(CodeAddr, Label) :-
( CodeAddr = label(Label0) ->
Label = Label0
;
error("extract_label_from_code_addr: non-label!")
).
:- pred code_info__place_resume_vars(assoc_list(prog_var, set(rval))::in,
code_tree::out, code_info::in, code_info::out) is det.
code_info__place_resume_vars([], empty) --> [].
code_info__place_resume_vars([Var - TargetSet | Rest], Code) -->
{ set__to_sorted_list(TargetSet, Targets) },
code_info__place_resume_var(Var, Targets, FirstCode),
{ Code = tree(FirstCode, RestCode) },
code_info__place_resume_vars(Rest, RestCode).
:- pred code_info__place_resume_var(prog_var::in, list(rval)::in,
code_tree::out, code_info::in, code_info::out) is det.
code_info__place_resume_var(_Var, [], empty) --> [].
code_info__place_resume_var(Var, [Target | Targets], Code) -->
( { Target = lval(TargetLval) } ->
code_info__place_var(Var, TargetLval, FirstCode)
;
{ error("code_info__place_resume_var: not lval") }
),
{ Code = tree(FirstCode, RestCode) },
code_info__place_resume_var(Var, Targets, RestCode).
% Reset the code generator's database of what is where.
% Remember that the variables in the map are available in their
% associated rvals; forget about all other variables.
:- pred code_info__set_var_locations(resume_map::in,
code_info::in, code_info::out) is det.
code_info__set_var_locations(Map) -->
{ map__to_assoc_list(Map, List0) },
{ code_info__flatten_varlval_list(List0, List) },
code_info__get_exprn_info(Exprn0),
{ code_exprn__reinit_state(List, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
:- pred code_info__flatten_varlval_list(assoc_list(prog_var, set(rval))::in,
assoc_list(prog_var, rval)::out) is det.
code_info__flatten_varlval_list([], []).
code_info__flatten_varlval_list([V - Rvals | Rest0], All) :-
code_info__flatten_varlval_list(Rest0, Rest),
set__to_sorted_list(Rvals, RvalList),
code_info__flatten_varlval_list_2(RvalList, V, Rest1),
list__append(Rest1, Rest, All).
:- pred code_info__flatten_varlval_list_2(list(rval)::in, prog_var::in,
assoc_list(prog_var, rval)::out) is det.
code_info__flatten_varlval_list_2([], _V, []).
code_info__flatten_varlval_list_2([R | Rs], V, [V - R | Rest]) :-
code_info__flatten_varlval_list_2(Rs, V, Rest).
code_info__resume_point_vars(ResumePoint, Vars) :-
code_info__pick_first_resume_point(ResumePoint, ResumeMap, _),
map__keys(ResumeMap, Vars).
code_info__resume_point_stack_addr(ResumePoint, StackAddr) :-
code_info__pick_stack_resume_point(ResumePoint, _, StackAddr).
%---------------------------------------------------------------------------%
:- pred code_info__maybe_save_trail_info(maybe(pair(lval))::out,
code_tree::out, code_info::in, code_info::out) is det.
code_info__maybe_save_trail_info(MaybeTrailSlots, SaveTrailCode) -->
code_info__get_globals(Globals),
{ globals__lookup_bool_option(Globals, use_trail, UseTrail) },
( { UseTrail = yes } ->
code_info__acquire_temp_slot(ticket_counter, CounterSlot),
code_info__acquire_temp_slot(ticket, TrailPtrSlot),
{ MaybeTrailSlots = yes(CounterSlot - TrailPtrSlot) },
{ SaveTrailCode = node([
mark_ticket_stack(CounterSlot)
- "save the ticket counter",
store_ticket(TrailPtrSlot)
- "save the trail pointer"
]) }
;
{ MaybeTrailSlots = no },
{ SaveTrailCode = empty }
).
:- pred code_info__maybe_restore_trail_info(maybe(pair(lval))::in,
code_tree::out, code_tree::out, code_info::in, code_info::out) is det.
code_info__maybe_restore_trail_info(MaybeTrailSlots,
CommitCode, RestoreCode) -->
(
{ MaybeTrailSlots = no },
{ CommitCode = empty },
{ RestoreCode = empty }
;
{ MaybeTrailSlots = yes(CounterSlot - TrailPtrSlot) },
{ CommitTrailCode = node([
reset_ticket(lval(TrailPtrSlot), commit)
- "discard trail entries and restore trail ptr"
]) },
{ RestoreTrailCode = node([
reset_ticket(lval(TrailPtrSlot), undo)
- "apply trail entries and restore trail ptr"
]) },
{ RestoreCounterCode = node([
discard_tickets_to(lval(CounterSlot))
- "restore the ticket counter"
]) },
code_info__release_temp_slot(CounterSlot),
code_info__release_temp_slot(TrailPtrSlot),
{ CommitCode = tree(CommitTrailCode, RestoreCounterCode) },
{ RestoreCode = tree(RestoreTrailCode, RestoreCounterCode) }
).
%---------------------------------------------------------------------------%
:- pred code_info__clone_resume_point(resume_point_info::in,
resume_point_info::out, code_info::in, code_info::out) is det.
code_info__clone_resume_point(ResumePoint0, ResumePoint) -->
(
{ ResumePoint0 = orig_only(_, _) },
{ error("cloning orig_only resume point") }
;
{ ResumePoint0 = stack_only(Map1, _) },
code_info__get_next_label(Label1),
{ Addr1 = label(Label1) },
{ ResumePoint = stack_only(Map1, Addr1) }
;
{ ResumePoint0 = stack_and_orig(Map1, _, Map2, _) },
code_info__get_next_label(Label1),
{ Addr1 = label(Label1) },
code_info__get_next_label(Label2),
{ Addr2 = label(Label2) },
{ ResumePoint = stack_and_orig(Map1, Addr1, Map2, Addr2) }
;
{ ResumePoint0 = orig_and_stack(Map1, _, Map2, _) },
code_info__get_next_label(Label2),
{ Addr2 = label(Label2) },
code_info__get_next_label(Label1),
{ Addr1 = label(Label1) },
{ ResumePoint = stack_and_orig(Map2, Addr2, Map1, Addr1) }
).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule to deal with liveness issues.
% The principles underlying this submodule of code_info.m are
% documented in the file compiler/notes/allocation.html.
:- interface.
:- pred code_info__get_known_variables(list(prog_var), code_info, code_info).
:- mode code_info__get_known_variables(out, in, out) is det.
:- pred code_info__variable_is_forward_live(prog_var, code_info, code_info).
:- mode code_info__variable_is_forward_live(in, in, out) is semidet.
:- pred code_info__make_vars_forward_dead(set(prog_var), code_info, code_info).
:- mode code_info__make_vars_forward_dead(in, in, out) is det.
:- pred code_info__pickup_zombies(set(prog_var), code_info, code_info).
:- mode code_info__pickup_zombies(out, in, out) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- pred code_info__add_forward_live_vars(set(prog_var), code_info, code_info).
:- mode code_info__add_forward_live_vars(in, in, out) is det.
:- pred code_info__rem_forward_live_vars(set(prog_var), code_info, code_info).
:- mode code_info__rem_forward_live_vars(in, in, out) is det.
% Make these variables appear magically live.
% We don't care where they are put.
:- pred code_info__make_vars_forward_live(set(prog_var), code_info, code_info).
:- mode code_info__make_vars_forward_live(in, in, out) is det.
code_info__get_known_variables(VarList) -->
code_info__get_forward_live_vars(ForwardLiveVars),
code_info__current_resume_point_vars(ResumeVars),
{ set__union(ForwardLiveVars, ResumeVars, Vars) },
{ set__to_sorted_list(Vars, VarList) }.
code_info__variable_is_forward_live(Var) -->
code_info__get_forward_live_vars(Liveness),
{ set__member(Var, Liveness) }.
code_info__add_forward_live_vars(Births) -->
code_info__get_forward_live_vars(Liveness0),
{ set__union(Liveness0, Births, Liveness) },
code_info__set_forward_live_vars(Liveness).
code_info__rem_forward_live_vars(Deaths) -->
code_info__get_forward_live_vars(Liveness0),
{ set__difference(Liveness0, Deaths, Liveness) },
code_info__set_forward_live_vars(Liveness).
code_info__make_vars_forward_live(Vars) -->
code_info__get_stack_slots(StackSlots),
code_info__get_exprn_info(Exprn0),
{ set__to_sorted_list(Vars, VarList) },
{ code_info__make_vars_forward_live_2(VarList, StackSlots, 1,
Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
:- pred code_info__make_vars_forward_live_2(list(prog_var), stack_slots, int,
exprn_info, exprn_info).
:- mode code_info__make_vars_forward_live_2(in, in, in, in, out) is det.
code_info__make_vars_forward_live_2([], _, _, Exprn, Exprn).
code_info__make_vars_forward_live_2([V | Vs], StackSlots, N0, Exprn0, Exprn) :-
( map__search(StackSlots, V, Lval0) ->
Lval = Lval0,
N1 = N0
;
code_info__find_unused_reg(N0, Exprn0, N1),
Lval = reg(r, N1)
),
code_exprn__maybe_set_var_location(V, Lval, Exprn0, Exprn1),
code_info__make_vars_forward_live_2(Vs, StackSlots, N1, Exprn1, Exprn).
:- pred code_info__find_unused_reg(int, exprn_info, int).
:- mode code_info__find_unused_reg(in, in, out) is det.
code_info__find_unused_reg(N0, Exprn0, N) :-
( code_exprn__lval_in_use(reg(r, N0), Exprn0, _) ->
N1 is N0 + 1,
code_info__find_unused_reg(N1, Exprn0, N)
;
N = N0
).
code_info__make_vars_forward_dead(Vars0) -->
code_info__current_resume_point_vars(ResumeVars),
{ set__intersect(Vars0, ResumeVars, FlushVars) },
code_info__get_zombies(Zombies0),
{ set__union(Zombies0, FlushVars, Zombies) },
code_info__set_zombies(Zombies),
{ set__difference(Vars0, Zombies, Vars) },
{ set__to_sorted_list(Vars, VarList) },
code_info__make_vars_forward_dead_2(VarList).
:- pred code_info__make_vars_forward_dead_2(list(prog_var),
code_info, code_info).
:- mode code_info__make_vars_forward_dead_2(in, in, out) is det.
code_info__make_vars_forward_dead_2([]) --> [].
code_info__make_vars_forward_dead_2([V | Vs]) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__var_becomes_dead(V, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn),
code_info__make_vars_forward_dead_2(Vs).
code_info__pickup_zombies(Zombies) -->
code_info__get_zombies(Zombies),
{ set__init(Empty) },
code_info__set_zombies(Empty).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule for handling the saving and restoration
% of trail tickets, heap pointers, stack pointers etc.
:- interface.
:- pred code_info__save_hp(code_tree, lval, code_info, code_info).
:- mode code_info__save_hp(out, out, in, out) is det.
:- pred code_info__restore_hp(lval, code_tree, code_info, code_info).
:- mode code_info__restore_hp(in, out, in, out) is det.
:- pred code_info__release_hp(lval, code_info, code_info).
:- mode code_info__release_hp(in, in, out) is det.
:- pred code_info__restore_and_release_hp(lval, code_tree,
code_info, code_info).
:- mode code_info__restore_and_release_hp(in, out, in, out) is det.
:- pred code_info__maybe_save_hp(bool, code_tree, maybe(lval),
code_info, code_info).
:- mode code_info__maybe_save_hp(in, out, out, in, out) is det.
:- pred code_info__maybe_restore_hp(maybe(lval), code_tree,
code_info, code_info).
:- mode code_info__maybe_restore_hp(in, out, in, out) is det.
:- pred code_info__maybe_release_hp(maybe(lval), code_info, code_info).
:- mode code_info__maybe_release_hp(in, in, out) is det.
:- pred code_info__maybe_restore_and_release_hp(maybe(lval), code_tree,
code_info, code_info).
:- mode code_info__maybe_restore_and_release_hp(in, out, in, out) is det.
:- pred code_info__save_ticket(code_tree, lval, code_info, code_info).
:- mode code_info__save_ticket(out, out, in, out) is det.
:- pred code_info__reset_ticket(lval, reset_trail_reason, code_tree,
code_info, code_info).
:- mode code_info__reset_ticket(in, in, out, in, out) is det.
:- pred code_info__release_ticket(lval, code_info, code_info).
:- mode code_info__release_ticket(in, in, out) is det.
:- pred code_info__reset_and_discard_ticket(lval, reset_trail_reason,
code_tree, code_info, code_info).
:- mode code_info__reset_and_discard_ticket(in, in, out, in, out) is det.
:- pred code_info__reset_discard_and_release_ticket(lval, reset_trail_reason,
code_tree, code_info, code_info).
:- mode code_info__reset_discard_and_release_ticket(in, in, out, in, out)
is det.
:- pred code_info__maybe_save_ticket(bool, code_tree, maybe(lval),
code_info, code_info).
:- mode code_info__maybe_save_ticket(in, out, out, in, out) is det.
:- pred code_info__maybe_reset_ticket(maybe(lval), reset_trail_reason,
code_tree, code_info, code_info).
:- mode code_info__maybe_reset_ticket(in, in, out, in, out) is det.
:- pred code_info__maybe_release_ticket(maybe(lval), code_info, code_info).
:- mode code_info__maybe_release_ticket(in, in, out) is det.
:- pred code_info__maybe_reset_and_discard_ticket(maybe(lval),
reset_trail_reason, code_tree, code_info, code_info).
:- mode code_info__maybe_reset_and_discard_ticket(in, in, out, in, out) is det.
:- pred code_info__maybe_reset_discard_and_release_ticket(maybe(lval),
reset_trail_reason, code_tree, code_info, code_info).
:- mode code_info__maybe_reset_discard_and_release_ticket(in, in, out, in, out)
is det.
%---------------------------------------------------------------------------%
:- implementation.
code_info__save_hp(Code, HpSlot) -->
code_info__acquire_temp_slot(lval(hp), HpSlot),
{ Code = node([
mark_hp(HpSlot) - "Save heap pointer"
]) }.
code_info__restore_hp(HpSlot, Code) -->
{ Code = node([
restore_hp(lval(HpSlot)) - "Restore heap pointer"
]) }.
code_info__release_hp(HpSlot) -->
code_info__release_temp_slot(HpSlot).
code_info__restore_and_release_hp(HpSlot, Code) -->
{ Code = node([
restore_hp(lval(HpSlot)) - "Release heap pointer"
]) },
code_info__release_hp(HpSlot).
%---------------------------------------------------------------------------%
code_info__maybe_save_hp(Maybe, Code, MaybeHpSlot) -->
( { Maybe = yes } ->
code_info__save_hp(Code, HpSlot),
{ MaybeHpSlot = yes(HpSlot) }
;
{ Code = empty },
{ MaybeHpSlot = no }
).
code_info__maybe_restore_hp(MaybeHpSlot, Code) -->
( { MaybeHpSlot = yes(HpSlot) } ->
code_info__restore_hp(HpSlot, Code)
;
{ Code = empty }
).
code_info__maybe_release_hp(MaybeHpSlot) -->
( { MaybeHpSlot = yes(HpSlot) } ->
code_info__release_hp(HpSlot)
;
[]
).
code_info__maybe_restore_and_release_hp(MaybeHpSlot, Code) -->
( { MaybeHpSlot = yes(HpSlot) } ->
code_info__restore_and_release_hp(HpSlot, Code)
;
{ Code = empty }
).
%---------------------------------------------------------------------------%
code_info__save_ticket(Code, TicketSlot) -->
code_info__acquire_temp_slot(ticket, TicketSlot),
{ Code = node([
store_ticket(TicketSlot) - "Save trail state"
]) }.
code_info__reset_ticket(TicketSlot, Reason, Code) -->
{ Code = node([
reset_ticket(lval(TicketSlot), Reason) - "Reset trail"
]) }.
code_info__release_ticket(TicketSlot) -->
code_info__release_temp_slot(TicketSlot).
code_info__reset_and_discard_ticket(TicketSlot, Reason, Code) -->
{ Code = node([
reset_ticket(lval(TicketSlot), Reason) - "Restore trail",
discard_ticket - "Pop ticket stack"
]) }.
code_info__reset_discard_and_release_ticket(TicketSlot, Reason, Code) -->
{ Code = node([
reset_ticket(lval(TicketSlot), Reason) - "Release trail",
discard_ticket - "Pop ticket stack"
]) },
code_info__release_temp_slot(TicketSlot).
%---------------------------------------------------------------------------%
code_info__maybe_save_ticket(Maybe, Code, MaybeTicketSlot) -->
( { Maybe = yes } ->
code_info__save_ticket(Code, TicketSlot),
{ MaybeTicketSlot = yes(TicketSlot) }
;
{ Code = empty },
{ MaybeTicketSlot = no }
).
code_info__maybe_reset_ticket(MaybeTicketSlot, Reason, Code) -->
( { MaybeTicketSlot = yes(TicketSlot) } ->
code_info__reset_ticket(TicketSlot, Reason, Code)
;
{ Code = empty }
).
code_info__maybe_release_ticket(MaybeTicketSlot) -->
( { MaybeTicketSlot = yes(TicketSlot) } ->
code_info__release_ticket(TicketSlot)
;
[]
).
code_info__maybe_reset_and_discard_ticket(MaybeTicketSlot, Reason, Code) -->
( { MaybeTicketSlot = yes(TicketSlot) } ->
code_info__reset_and_discard_ticket(TicketSlot, Reason, Code)
;
{ Code = empty }
).
code_info__maybe_reset_discard_and_release_ticket(MaybeTicketSlot, Reason,
Code) -->
( { MaybeTicketSlot = yes(TicketSlot) } ->
code_info__reset_discard_and_release_ticket(TicketSlot, Reason,
Code)
;
{ Code = empty }
).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule to deal with code_exprn.
:- interface.
:- pred code_info__variable_locations(map(prog_var, set(rval)),
code_info, code_info).
:- mode code_info__variable_locations(out, in, out) is det.
:- pred code_info__set_var_location(prog_var, lval, code_info, code_info).
:- mode code_info__set_var_location(in, in, in, out) is det.
:- pred code_info__cache_expression(prog_var, rval, code_info, code_info).
:- mode code_info__cache_expression(in, in, in, out) is det.
:- pred code_info__place_var(prog_var, lval, code_tree, code_info, code_info).
:- mode code_info__place_var(in, in, out, in, out) is det.
:- pred code_info__produce_variable(prog_var, code_tree, rval,
code_info, code_info).
:- mode code_info__produce_variable(in, out, out, in, out) is det.
:- pred code_info__produce_variable_in_reg(prog_var, code_tree, rval,
code_info, code_info).
:- mode code_info__produce_variable_in_reg(in, out, out, in, out) is det.
:- pred code_info__produce_variable_in_reg_or_stack(prog_var, code_tree, rval,
code_info, code_info).
:- mode code_info__produce_variable_in_reg_or_stack(in, out, out, in, out)
is det.
:- pred code_info__materialize_vars_in_rval(rval, rval, code_tree, code_info,
code_info).
:- mode code_info__materialize_vars_in_rval(in, out, out, in, out) is det.
:- pred code_info__lock_reg(lval, code_info, code_info).
:- mode code_info__lock_reg(in, in, out) is det.
:- pred code_info__unlock_reg(lval, code_info, code_info).
:- mode code_info__unlock_reg(in, in, out) is det.
:- pred code_info__acquire_reg_for_var(prog_var, lval, code_info, code_info).
:- mode code_info__acquire_reg_for_var(in, out, in, out) is det.
:- pred code_info__acquire_reg(reg_type, lval, code_info, code_info).
:- mode code_info__acquire_reg(in, out, in, out) is det.
:- pred code_info__release_reg(lval, code_info, code_info).
:- mode code_info__release_reg(in, in, out) is det.
:- pred code_info__clear_r1(code_tree, code_info, code_info).
:- mode code_info__clear_r1(out, in, out) is det.
:- type call_direction ---> caller ; callee.
% Generate code to either setup the input arguments for a call
% (i.e. in the caller), or to setup the output arguments in the
% predicate epilog (i.e. in the callee).
:- pred code_info__setup_call(assoc_list(prog_var, arg_info),
call_direction, code_tree, code_info, code_info).
:- mode code_info__setup_call(in, in, out, in, out) is det.
:- pred code_info__clear_all_registers(code_info, code_info).
:- mode code_info__clear_all_registers(in, out) is det.
:- pred code_info__save_variable_on_stack(prog_var, code_tree,
code_info, code_info).
:- mode code_info__save_variable_on_stack(in, out, in, out) is det.
:- pred code_info__save_variables_on_stack(list(prog_var), code_tree,
code_info, code_info).
:- mode code_info__save_variables_on_stack(in, out, in, out) is det.
:- pred code_info__max_reg_in_use(int, code_info, code_info).
:- mode code_info__max_reg_in_use(out, in, out) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- pred code_info__place_vars(assoc_list(prog_var, set(rval)), code_tree,
code_info, code_info).
:- mode code_info__place_vars(in, out, in, out) is det.
code_info__variable_locations(Locations) -->
code_info__get_exprn_info(Exprn),
{ code_exprn__get_varlocs(Exprn, Locations) }.
code_info__set_var_location(Var, Lval) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__set_var_location(Var, Lval, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__cache_expression(Var, Rval) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__cache_exprn(Var, Rval, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__place_var(Var, Lval, Code) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__place_var(Var, Lval, Code, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__place_vars([], empty) --> [].
code_info__place_vars([V - Rs | RestList], Code) -->
(
{ set__to_sorted_list(Rs, RList) },
{ code_info__lval_in_rval_list(L, RList) }
->
code_info__place_var(V, L, ThisCode)
;
{ ThisCode = empty }
),
code_info__place_vars(RestList, RestCode),
{ Code = tree(ThisCode, RestCode) }.
:- pred code_info__lval_in_rval_list(lval, list(rval)).
:- mode code_info__lval_in_rval_list(out, in) is semidet.
code_info__lval_in_rval_list(Lval, [Rval | Rvals]) :-
( Rval = lval(Lval0) ->
Lval = Lval0
;
code_info__lval_in_rval_list(Lval, Rvals)
).
code_info__produce_variable(Var, Code, Rval) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__produce_var(Var, Rval, Code, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__produce_variable_in_reg(Var, Code, Rval) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__produce_var_in_reg(Var, Rval, Code, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__produce_variable_in_reg_or_stack(Var, Code, Rval) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__produce_var_in_reg_or_stack(Var, Rval, Code,
Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__materialize_vars_in_rval(Rval0, Rval, Code) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__materialize_vars_in_rval(Rval0, Rval, Code,
Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__lock_reg(Reg) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__lock_reg(Reg, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__unlock_reg(Reg) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__unlock_reg(Reg, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__acquire_reg_for_var(Var, Lval) -->
code_info__get_exprn_info(Exprn0),
code_info__get_follow_vars(Follow),
(
{ map__search(Follow, Var, PrefLval) },
{ PrefLval = reg(PrefRegType, PrefRegNum) }
->
{ code_exprn__acquire_reg_prefer_given(PrefRegType, PrefRegNum,
Lval, Exprn0, Exprn) }
;
{ code_exprn__acquire_reg(r, Lval, Exprn0, Exprn) }
),
code_info__set_exprn_info(Exprn).
code_info__acquire_reg(Type, Lval) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__acquire_reg(Type, Lval, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__release_reg(Lval) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__release_reg(Lval, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__clear_r1(Code) -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__clear_r1(Code, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
%---------------------------------------------------------------------------%
code_info__setup_call([], _Direction, empty) --> [].
code_info__setup_call([V - arg_info(Loc, Mode) | Rest], Direction, Code) -->
(
{
Mode = top_in,
Direction = caller
;
Mode = top_out,
Direction = callee
}
->
{ code_util__arg_loc_to_register(Loc, Reg) },
code_info__get_exprn_info(Exprn0),
{ code_exprn__place_var(V, Reg, Code0, Exprn0, Exprn1) },
% We need to test that either the variable
% is live OR it occurs in the remaining arguments
% because of a bug in polymorphism.m which
% causes some compiler generated code to violate
% superhomogeneous form
(
code_info__variable_is_forward_live(V)
->
{ IsLive = yes }
;
{ IsLive = no }
),
{
list__member(Vtmp - _, Rest),
V = Vtmp
->
Occurs = yes
;
Occurs = no
},
(
% We can't simply use a disj here
% because of bugs in modes/det_analysis
{ bool__or(Occurs, IsLive, yes) }
->
{ code_exprn__lock_reg(Reg, Exprn1, Exprn2) },
code_info__set_exprn_info(Exprn2),
code_info__setup_call(Rest, Direction, Code1),
code_info__get_exprn_info(Exprn3),
{ code_exprn__unlock_reg(Reg, Exprn3, Exprn) },
code_info__set_exprn_info(Exprn),
{ Code = tree(Code0, Code1) }
;
{ code_exprn__lock_reg(Reg, Exprn1, Exprn2) },
code_info__set_exprn_info(Exprn2),
{ set__singleton_set(Vset, V) },
code_info__make_vars_forward_dead(Vset),
code_info__setup_call(Rest, Direction, Code1),
code_info__get_exprn_info(Exprn4),
{ code_exprn__unlock_reg(Reg, Exprn4, Exprn) },
code_info__set_exprn_info(Exprn),
{ Code = tree(Code0, Code1) }
)
;
code_info__setup_call(Rest, Direction, Code)
).
% As a sanity check, we could test whether any known variable
% has its only value in a reguster, but we don't.
code_info__clear_all_registers -->
code_info__get_exprn_info(Exprn0),
{ code_exprn__clobber_regs([], Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__save_variable_on_stack(Var, Code) -->
code_info__get_variable_slot(Var, Slot),
code_info__get_exprn_info(Exprn0),
{ code_exprn__place_var(Var, Slot, Code, Exprn0, Exprn) },
code_info__set_exprn_info(Exprn).
code_info__save_variables_on_stack([], empty) --> [].
code_info__save_variables_on_stack([Var | Vars], Code) -->
code_info__save_variable_on_stack(Var, FirstCode),
code_info__save_variables_on_stack(Vars, RestCode),
{ Code = tree(FirstCode, RestCode) }.
code_info__max_reg_in_use(Max) -->
code_info__get_exprn_info(Exprn),
{ code_exprn__max_reg_in_use(Exprn, Max) }.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule for dealing with the recording of variable liveness
% information around calls.
%
% Value numbering needs to know what locations are live before calls;
% the garbage collector and the debugger need to know what locations
% are live containing what types of values after calls.
:- interface.
:- pred code_info__generate_call_stack_vn_livevals(set(prog_var)::in,
set(lval)::out, code_info::in, code_info::out) is det.
:- pred code_info__generate_call_vn_livevals(list(arg_loc)::in,
set(prog_var)::in, set(lval)::out, code_info::in, code_info::out)
is det.
:- pred code_info__generate_return_live_lvalues(
assoc_list(prog_var, arg_loc)::in, instmap::in, list(liveinfo)::out,
code_info::in, code_info::out) is det.
%---------------------------------------------------------------------------%
:- implementation.
code_info__generate_call_stack_vn_livevals(OutputArgs, LiveVals) -->
code_info__get_known_variables(KnownVarList),
{ set__list_to_set(KnownVarList, KnownVars) },
{ set__difference(KnownVars, OutputArgs, LiveVars) },
{ set__to_sorted_list(LiveVars, LiveVarList) },
{ set__init(LiveVals0) },
code_info__generate_stack_var_vn(LiveVarList, LiveVals0, LiveVals1),
code_info__get_active_temps_data(Temps),
{ code_info__generate_call_temp_vn(Temps, LiveVals1, LiveVals) }.
code_info__generate_call_vn_livevals(InputArgLocs, OutputArgs, LiveVals) -->
code_info__generate_call_stack_vn_livevals(OutputArgs, StackLiveVals),
{ code_info__generate_input_var_vn(InputArgLocs, StackLiveVals,
LiveVals) }.
:- pred code_info__generate_stack_var_vn(list(prog_var)::in, set(lval)::in,
set(lval)::out, code_info::in, code_info::out) is det.
code_info__generate_stack_var_vn([], Vals, Vals) --> [].
code_info__generate_stack_var_vn([V | Vs], Vals0, Vals) -->
code_info__get_variable_slot(V, Lval),
{ set__insert(Vals0, Lval, Vals1) },
code_info__generate_stack_var_vn(Vs, Vals1, Vals).
:- pred code_info__generate_call_temp_vn(assoc_list(lval, slot_contents)::in,
set(lval)::in, set(lval)::out) is det.
code_info__generate_call_temp_vn([], Vals, Vals).
code_info__generate_call_temp_vn([Lval - _ | Temps], Vals0, Vals) :-
set__insert(Vals0, Lval, Vals1),
code_info__generate_call_temp_vn(Temps, Vals1, Vals).
:- pred code_info__generate_input_var_vn(list(arg_loc)::in,
set(lval)::in, set(lval)::out) is det.
code_info__generate_input_var_vn([], Vals, Vals).
code_info__generate_input_var_vn([InputArgLoc | InputArgLocs], Vals0, Vals) :-
code_util__arg_loc_to_register(InputArgLoc, Lval),
set__insert(Vals0, Lval, Vals1),
code_info__generate_input_var_vn(InputArgLocs, Vals1, Vals).
%---------------------------------------------------------------------------%
code_info__generate_return_live_lvalues(OutputArgLocs, ReturnInstMap,
LiveLvalues) -->
code_info__variable_locations(VarLocs),
code_info__get_known_variables(Vars),
code_info__get_active_temps_data(Temps),
code_info__get_proc_info(ProcInfo),
code_info__get_globals(Globals),
code_info__get_module_info(ModuleInfo),
{ continuation_info__generate_return_live_lvalues(OutputArgLocs,
ReturnInstMap, Vars, VarLocs, Temps, ProcInfo, ModuleInfo,
Globals, LiveLvalues) }.
:- pred code_info__generate_resume_layout(label::in, resume_map::in,
code_info::in, code_info::out) is det.
code_info__generate_resume_layout(Label, ResumeMap) -->
code_info__get_globals(Globals),
{ globals__lookup_bool_option(Globals, agc_stack_layout,
AgcStackLayout) },
( { AgcStackLayout = yes } ->
code_info__get_active_temps_data(Temps),
code_info__get_instmap(InstMap),
code_info__get_proc_info(ProcInfo),
code_info__get_module_info(ModuleInfo),
{ continuation_info__generate_resume_layout(ResumeMap,
Temps, InstMap, ProcInfo, ModuleInfo, Layout) },
code_info__add_resume_layout_for_label(Label, Layout)
;
[]
).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% Submodule for managing stack slots.
% Det stack frames are organized as follows.
%
% ... unused ...
% sp ---> <first unused slot>
% <space for local var 1>
% ... local vars ...
% <space for local var n>
% <space for temporary 1>
% ... temporaries ...
% <space for temporary n>
% <space for saved succip, if needed>
%
% The stack pointer points to the first free location at the
% top of the stack.
%
% `code_info__succip_is_used' determines whether we need a slot to
% hold the succip.
%
% Nondet stack frames also have the local variables above the
% temporaries, but contain several fixed slots on top, and the
% saved succip is stored in one of these.
%
% For both kinds of stack frames, the slots holding variables
% are allocated during the live_vars pass, while the slots holding
% temporaries are acquired (and if possible, released) on demand
% during code generation.
:- interface.
% Returns the total stackslot count, but not including space for
% succip. This total can change in the future if this call is
% followed by further allocations of temp slots.
:- pred code_info__get_total_stackslot_count(int, code_info, code_info).
:- mode code_info__get_total_stackslot_count(out, in, out) is det.
% Acquire a stack slot for storing a temporary. The slot_contents
% description is for accurate gc.
:- pred code_info__acquire_temp_slot(slot_contents, lval,
code_info, code_info).
:- mode code_info__acquire_temp_slot(in, out, in, out) is det.
% Release a stack slot acquired earlier for a temporary value.
:- pred code_info__release_temp_slot(lval, code_info, code_info).
:- mode code_info__release_temp_slot(in, in, out) is det.
% Return the lval of the stack slot in which the given variable
% is stored. Aborts if the variable does not have a stack slot
% an assigned to it.
:- pred code_info__get_variable_slot(prog_var, lval, code_info, code_info).
:- mode code_info__get_variable_slot(in, out, in, out) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
code_info__acquire_temp_slot(Item, StackVar) -->
code_info__get_temps_in_use(TempsInUse0),
{ IsTempUsable = lambda([TempContent::in, Lval::out] is semidet, (
TempContent = Lval - ContentType,
ContentType = Item,
\+ set__member(Lval, TempsInUse0)
)) },
code_info__get_temp_content_map(TempContentMap0),
{ map__to_assoc_list(TempContentMap0, TempContentList) },
{ list__filter_map(IsTempUsable, TempContentList, UsableLvals) },
(
{ UsableLvals = [UsableLval | _] },
{ StackVar = UsableLval }
;
{ UsableLvals = [] },
code_info__get_var_slot_count(VarSlots),
code_info__get_max_temp_slot_count(TempSlots0),
{ TempSlots is TempSlots0 + 1 },
{ Slot is VarSlots + TempSlots },
code_info__stack_variable(Slot, StackVar),
code_info__set_max_temp_slot_count(TempSlots),
{ map__det_insert(TempContentMap0, StackVar, Item,
TempContentMap) },
code_info__set_temp_content_map(TempContentMap)
),
{ set__insert(TempsInUse0, StackVar, TempsInUse) },
code_info__set_temps_in_use(TempsInUse).
code_info__release_temp_slot(StackVar) -->
code_info__get_temps_in_use(TempsInUse0),
{ set__delete(TempsInUse0, StackVar, TempsInUse) },
code_info__set_temps_in_use(TempsInUse).
%---------------------------------------------------------------------------%
code_info__get_variable_slot(Var, Slot) -->
code_info__get_stack_slots(StackSlots),
( { map__search(StackSlots, Var, SlotPrime) } ->
{ Slot = SlotPrime }
;
code_info__variable_to_string(Var, Name),
{ term__var_to_int(Var, Num) },
{ string__int_to_string(Num, NumStr) },
{ string__append_list([
"code_info__get_variable_slot: variable `",
Name, "' (", NumStr, ") not found"], Str) },
{ error(Str) }
).
code_info__get_total_stackslot_count(NumSlots) -->
code_info__get_var_slot_count(SlotsForVars),
code_info__get_max_temp_slot_count(SlotsForTemps),
{ NumSlots is SlotsForVars + SlotsForTemps }.
:- pred code_info__max_var_slot(stack_slots, int).
:- mode code_info__max_var_slot(in, out) is det.
code_info__max_var_slot(StackSlots, SlotCount) :-
map__values(StackSlots, StackSlotList),
code_info__max_var_slot_2(StackSlotList, 0, SlotCount).
:- pred code_info__max_var_slot_2(list(lval), int, int).
:- mode code_info__max_var_slot_2(in, in, out) is det.
code_info__max_var_slot_2([], Max, Max).
code_info__max_var_slot_2([L | Ls], Max0, Max) :-
( L = stackvar(N) ->
int__max(N, Max0, Max1)
; L = framevar(N) ->
int__max(N, Max0, Max1)
;
Max1 = Max0
),
code_info__max_var_slot_2(Ls, Max1, Max).
:- pred code_info__stack_variable(int, lval, code_info, code_info).
:- mode code_info__stack_variable(in, out, in, out) is det.
code_info__stack_variable(Num, Lval) -->
code_info__get_proc_model(CodeModel),
( { CodeModel = model_non } ->
{ Lval = framevar(Num) }
;
{ Lval = stackvar(Num) }
).
:- pred code_info__stack_variable_reference(int, rval, code_info, code_info).
:- mode code_info__stack_variable_reference(in, out, in, out) is det.
code_info__stack_variable_reference(Num, mem_addr(Ref)) -->
code_info__get_proc_model(CodeModel),
( { CodeModel = model_non } ->
{ Ref = framevar_ref(Num) }
;
{ Ref = stackvar_ref(Num) }
).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
|