1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493
|
/* Generator Modula-2 to C.
Copyright (C) 1993-1997 (see more details in file COPYING).
This file is part of Modula-2 translator. */
#include "m2-common.h"
#include "m2-generator.h"
#include <setjmp.h>
static void output_file_error ();
static void generate_necessary_type_definitions ();
static void generate_call ();
static void generate_expression ();
static void generate_block ();
static int block_code_may_be_used ();
/* This page contains C representations of primitive modula-2 types. */
#ifdef MODULA_CHAR_IS_IMPLEMENTED_BY_C_UNSIGNED_CHAR
char char_type_representation[] = "unsigned char";
#else
char char_type_representation[] = "char";
#endif
char cardinal_type_representation[] = "unsigned int";
char real_type_representation[] = "float";
char integer_type_representation[] = "int";
#ifdef MODULA_SHORT_IS_IMPLEMENTED_BY_C_CHAR
char shortcard_type_representation[] = "unsigned char";
#else
char shortcard_type_representation[] = "unsigned short";
#endif
char shortreal_type_representation[] = "float";
#ifdef MODULA_SHORT_IS_IMPLEMENTED_BY_C_CHAR
char shortint_type_representation[] = "char";
#else
char shortint_type_representation[] = "short";
#endif
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
char longcard_type_representation[] = "unsigned long";
#else
char longcard_type_representation[] = "unsigned int";
#endif
char longreal_type_representation[] = "double";
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
char longint_type_representation[] = "long";
#else
char longint_type_representation[] = "int";
#endif
char byte_type_representation[] = "char";
char word_type_representation[] = "int";
char set_type_representation[] = "unsigned int";
#ifdef C_VOID_IS_IMPLEMENTED
char void_type_representation[] = "void";
#else
char void_type_representation[] = "int";
#endif
/* The same implementation as cardinal_type_representation or
longcard_type_representation. */
#ifdef MODULA_ADDRESS_IS_IMPLEMENTED_BY_LONGCARD
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
static char address_type_representation[] = "unsigned long";
#else
static char address_type_representation[] = "unsigned int";
#endif
#else
static char address_type_representation[] = "unsigned int";
#endif
/* This page contains names (or its prefixes) of objects used in generated C
program. The page also contains commentaries about generated C program. */
/* Labels are used in the generated program. Its names is the following string
and label number. */
static char label_name_prefix[] = "label";
/* The following string is name of static variable in generated code of
implementation module. Nonzero value of the variable means that
the module body has been executed. The implementation module body
is to be executed only once. */
static char implementation_module_initiation_flag_name[]
= "module_initiation_flag";
/* The following string is name of C parameter of function which implements
Modula-2 procedure without parameters declared on the uppest level.
Such function may be used as coroutine. Nonzero parameter value means
that given call is used coroutine creation (by standard procedure
NEWPROCESS). */
static char coroutine_parameter_name[] = "coroutine_creation_flag";
/* The following string var_prefix is used in many cases:
1) as name of structured local C variable which contains of all variables of
given procedure;
2) as prefix of name of structured global C variable which contains of all
unit compilation variables on the uppest declaration level (the prefix
is followed by compilation unit name);
3) as prefix of name of C structure which declares procedure variables (the
C variable mentioned in the first case is declared by the structure) and
as prefix of name of static C variable which is pointer to this structure
(if given procedure has nested procedures).
In third case the prefix is followed by full procedure name. For example,
module m;
var i: INTEGER;
module m1;
var c: CHAR;
procedure p;
var r: real;
procedure p1;
var s: BITSET;
begin end p1;
begin end p;
end m1;
end m.
will generate the following declarations
struct {int _i;
char _c_m1;
}var_m;
static struct var_p_m1_m{float _r;
}*var_p_m1_m;
static int _p1_p_m1_m()
{struct{unsigned int _s;
}var;
...
}
static int _p_m1_m(coroutine_creation_flag)int coroutine_creation_flag;
{struct var_p_m1_m var
...
}
*/
static char var_prefix[] = "var";
/* The following string par_prefix is used in many cases:
1) as name of structured C parameter which contains of all parameters of
given procedure;
2) as prefix of name of C structure which declares procedure parameters (the
C parameter mentioned in the first case is declared by the pointer to
this structure) and as prefix of name of static C variable which is
pointer to this structure (if given procedure has nested procedures).
In second case the prefix is followed by full procedure name. For example,
module m;
procedure p (var r: real);
procedure p1 (s: BITSET);
begin end p1;
begin end p;
end m.
will generate the following declarations
struct par_p_m{float* _r;};
struct par_p1_p_m{unsigned int _s;};
static struct par_p_m *par_p_m;
static int _p1_p_m(par)register struct par_p1_p_m *par;
{
...
}
static int _p_m(par)register struct par_p_m *par;
{
...
}
*/
static char par_prefix[] = "par";
/* The two following strings
name_of_saved_pointer_to_procedure_variables and
name_of_saved_pointer_to_procedure_parameters
are used as name of local C variables in procedure
which has nested procedures. These variables are pointers to the structures
representing all procedure variables or parameters. These variables are
used for saving and restoring corresponding static variables.
For example,
module m;
procedure p (var r: real);
var i: integer;
procedure p1 (s: BITSET);
var c: char;
begin end p1;
begin end p;
end m.
will generate the following declarations
struct par_p_m{float* _r;};
struct par_p1_p_m{unsigned int _s;};
static struct var_p_m{int _i;}*var_p_m;
static struct par_p_m *par_p_m;
static int _p1_p_m(par)register struct par_p1_p_m *par;
{struct{char _c;}var;
...
}
static int _p_m(par)register struct par_p_m *par;
{struct var_p_m var, *saved_var_ptr; !!!
struct par_p_m *saved_par_ptr; !!!
saved_var_ptr=var_p_m; var_p_m=(&var);
saved_par_ptr=par_p_m; par_p_m=par;
...
var_p_m=saved_var_ptr; par_p_m=saved_par_ptr;
}
*/
static char name_of_saved_pointer_to_procedure_variables[] = "saved_var_ptr";
static char name_of_saved_pointer_to_procedure_parameters[] = "saved_par_ptr";
/* The two following strings are used as prefix of name of C structure which
declares procedure parameters and as prefix of name of parameter which is
declared in the C structure. These strings are used only procedure whose
name is not known.
The prefixes are followed by unique numbers which are defined by
addresses of icode representations of corresponding procedure type
and parameter type. For example,
module m;
var p: procedure(cardinal, char);
end m.
will generate the following declaration
struct par_anonim_proc26480
{unsigned int anonim_par264ac;
char anonim_par264dc;};
*/
static char anonim_procedure_type_name_prefix[] = "anonim_proc";
static char anonim_parameter_type_name_prefix[] = "anonim_par";
/* Two following strings are names of members of C structure which
implements open array parameter. The first member value is address
of passed array. The second member value is value of standard procedure
HIGH for the actual parameter. The way of passing open array parameter is
the same for value and variable parameters. For example,
module m;
procedure p (a: array of char);
begin end p;
end m.
will generate the following declarations
struct par_p_m{
struct{char* adr;unsigned int high;}_a;
};
static int _p_m(par)register struct par_p_m *par;
{
...
}
*/
static char open_array_address_name[] = "adr";
static char open_array_high_value_name[] = "high";
/* The following name is used for local variable which represents function
result. */
static char function_result_name[] = "result";
/* This prefix is used for name of variable which contains string constant.
The prefix is followed by string number. Such implementation of string
constant is needed for that the constant string is represented by the
only copy. */
static char string_name_prefix[] = "string";
/* The following string is used as prefix of name of C structure which
implements Modula-2 record within other type definition.
The prefix is followed by unique number which is defined by
address of icode representation of corresponding record. For example,
module m;
type
t = record i: integer;
r: record c: char; end;
end;
end m.
will generate the following declarations
struct record26504{char _c;};
struct _t_m{int _i;
struct record26504 _r;
};
*/
static char anonim_record_name_prefix[] = "record";
/* Cyclic pointer is represented as C structure. It is the only way of
implementation. To implement cyclic pointer definition the two following
strings are used as prefix of name of C structure and as name of the
structure member. The prefix is followed by unique number which is defined
by address of icode representation of corresponding pointer type definition.
Reference to pointer value is always the member value.
For example,
module m;
type
t = pointer to t1;
t1 = pointer to t;
end m.
will generate the following declarations
struct ptr_struct26480{struct ptr_struct26480** ptr_value;};
struct ptr_struct264bc{struct ptr_struct26480* ptr_value;};
*/
static char pointer_structure_name_prefix[] = "ptr_struct";
static char pointer_member_name[] = "ptr_value";
/* Record case part is represented as C union and record case variant is
represented as C structure. Such implementation is used even if
the record case part contains only one variants or the record case variant
contains only one field. The following prefixes
record_case_part_name_prefix and record_case_variant_name_prefix are used
as prefixes of names of the C union and the C structure. The prefixes are
followed by number of record case part (from end of the record) or of case
variant (from the end of the record case part).
For example,
module m;
type
r = record
case :boolean of
false: ch: char|
true: i:integer;
case b:boolean of
false: c: cardinal|
true: f: real
end;
end;
end;
end m.
will generate the following declaration
struct _r_m{
union{
struct{char _ch;}variant2;
struct{
int _i;unsigned short _b;
union{
struct{unsigned int _c;}variant2;
struct{float _f;}variant1;
}case1;
}variant1;
}case1;
};
*/
static char record_case_part_name_prefix[] = "case";
static char record_case_variant_name_prefix[] = "variant";
/* The following prefix is used for name of static function which tests value
of range. This function is generated if option `-test' is in command line
and the test is really needed. The prefix is followed by unique number
which is defined by address of icode representation of corresponding
range type definition.
For example, under mentioned above conditions
var i: [1..10];
will generate the following code
static unsigned short
test26490 (e)
register unsigned short e;
{
if ((unsigned) 01 <= e && e <= (unsigned) 012)
return e;
m2_rngovf ();
}
*/
static char range_test_function_name_prefix[] = "test";
/* Modula-2 actual parameters are passed as single structure. All such
structures (having the same name as name structure representing the
corresponding procedure formal parameters -- see commentaries for
par_prefix) for called procedures are in local C union. The following
string all_actuals_name is name of the C union.
For example,
module m;
var k: integer;
procedure p (c: cardinal; var i: integer);
begin end p;
procedure p1 (ch: char): integer;
begin return 10; end p1;
begin
p (10, k);
k := p1 (10C);
end m.
will generate the following code
...
struct par_p_m{unsigned int _c;int* _i;};
struct par_p1_m{char _ch;};
...
main (...)
{
union
{
struct par_p_m par_p_m;
struct par_p1_m par_p1_m;
} actuals;
(actuals.par_p_m._c = (10), actuals.par_p_m._i = (&(var_m._k)),
_p_m (&actuals.par_p_m));
(var_m._k)
= ((actuals.par_p1_m._ch = ('\10'), _p1_m (&actuals.par_p1_m)));
}
*/
static char all_actuals_name[] = "actuals";
/* The value open array parameter is passed by its address (see commentaries
for open_array_address_name). If address of actual parameter can not be in
C then the local C union temp_actuals_name is generated. The union
contains C structures for each such call. The C structure contains
members for each such parameter. The following strings are used as name
of the C union, as prefix of name of the C structure and as prefix of
name of the C member. Prefix of name of the C structure and as prefix of
name of the C member are followed by order number of corresponding call and
order number of such parameter in the call.
For example,
module m;
from SYSTEM import BYTE;
procedure p (a: array of BYTE);
begin end p;
begin
p (10C);
end m.
will generate the following code
struct par_p_m {struct {char *adr; unsigned int high;} _a;};
...
main ()
{
union {struct par_p_m par_p_m;} actuals;
union {
struct {char temp_actual1;} temp_call_actuals1;
} temp_actuals;
(temp_actuals.temp_call_actuals1.temp_actual1 = ('\10'),
actuals.par_p_m._a.adr
= (char *) (&temp_actuals.temp_call_actuals1.temp_actual1),
actuals.par_p_m._a.high = 00,
_p_m (&actuals.par_p_m));
...
}
*/
static char temp_actuals_name[] = "temp_actuals";
static char temp_call_actuals_name_prefix[] = "temp_call_actuals";
static char actual_name_prefix[] = "temp_actual";
/* The three following strings temp_variables_name,
temp_expression_variables_names_prefix and temp_variable_name_prefix
are used as C union name, as prefix of C structure name and as prefix
of name of C member in the C structure. These data are used as temporary
variables. The C structure corresponds to Modula-2 expression in which
temporary variables are needed. The C member corresponds to a temporary
variable in the expression. The prefixes are followed by number of
corresponding Modula-2 expression in current block and number of temporary
variable in the expression.
Temporary variables are needed in following cases:
1) call of type transfer function with constant (e.g. integer (10.0)
generates (temp_vars.temp_expr_vars0.temp_var1=(1.0e+01),
*(int*)&temp_vars.temp_expr_vars0.temp_var1));
2) pass of real constant as parameter of type WORD;
3) implementation of functions with structured results (this Modula-2
implementation permits structured results of function).
Example of third case.
module m;
var b: boolean;
type t = array [0..10] of char;
procedure f (i: integer): t;
begin end f;
begin
b := f(0) = f (1);
end m.
will generate the following code
struct {unsigned short _b;} var_m;
static int _f_m (par, result)
register struct par_f_m *par; char result[013];
{...}
main ()
{
...
union {
struct {
char temp_var1[013];
char temp_var2[013];
} temp_expr_vars0;
} temp_vars;
(var_m._b)
= (m2_eq
(((actuals.par_f_m._i = (0),
_f_m (&actuals.par_f_m,
temp_vars.temp_expr_vars0.temp_var1),
temp_vars.temp_expr_vars0.temp_var1)),
((actuals.par_f_m._i = (1),
_f_m (&actuals.par_f_m,
temp_vars.temp_expr_vars0.temp_var2),
temp_vars.temp_expr_vars0.temp_var2)), 013));
}
*/
static char temp_variables_name[] = "temp_vars";
static char temp_expression_variables_names_prefix[] = "temp_expr_vars";
static char temp_variable_name_prefix[] = "temp_var";
/* The following string is prefix of name of local variable which is used for
implementation of with statement. The type of variable is pointer
to type of corresponding designator after keyword with. The prefix is
followed by number of level of nesting with statement.
For example,
with r1 do
with r2 do end;
end;
will generate the following statements
{register struct record264ac* with_var1=(&(var_m._r1));
{register struct record264ac* with_var2=(&(var_m._r2));
...
}
...
}
*/
static char with_variable_name_prefix[] = "with_var";
/* The following string is name of global variable which is used for
implementation of standard procedure ABS. The variable is defined in
main module and represented as C union. */
static char union_name_for_abs_implementation[] = "abs_union";
/* The following array contains names of union
UNION_NAME_FOR_ABS_IMPLEMENTATION members which are used for implementation
of standard procedure ABS. The order in the array is the same as
in enumeration basic_type. */
static char *names_of_variables_for_abs_implementation[] =
{NULL, NULL, NULL, /*BTM_SHORT_CARDINAL-BTM_LONG_REAL*/
"abs_short_int", "abs_int", "abs_long_int",
"abs_short_real", "abs_real", "abs_long_real"
};
/* This page contains declarations of the generator static variables and
of the generator passes on blocks. */
/* Name of C file which is translation of current compiled module. It may be
temporary file depending on given command flags. */
static char *modula_output_file_name;
/* This is file with name MODULA_OUTPUT_FILE_NAME. */
static FILE *output_file;
/* The following enumeration declares the names of the generator passes on
Modula-2 blocks. The passes STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS
and STATEMENTS_GENERATION_PASS are always executed. Necessity of the order
passes are determined on the pass
STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS. The generator starts
processing of next block only when all necessary passes on the current
block are executed. */
enum generator_pass
{
/* See commentaries for string_name_prefix. */
STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS,
/* See commentaries for all_actuals_name. */
ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS,
/* See commentaries for temp_actuals_name. */
TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS,
/* See commentaries for temp_variables_name. */
TEMPORARY_VARIABLES_DECLARATION_GENERATION_PASS,
STATEMENTS_GENERATION_PASS
};
/* Current block pass of the generator. */
static enum generator_pass current_generator_pass;
/* Line number in last generated C construction `#line ...'. The variable
value is undefined when it is zero or current generator pass is
not STATEMENTS_GENERATION_PASS. */
static int last_generated_line_number;
/* Order number (1, 2, ...) of current block among blocks for which
ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS is needed. */
static int current_block_number_among_blocks_with_actuals;
/* The following variable value is TRUE if current generator pass is
STATEMENTS_GENERATION_PASS. */
static int it_is_statements_generation_pass;
/* See commentaries for generation_pass. */
static int actuals_structures_declaration_generation_is_needed;
static int temp_array_actuals_structures_declaration_generation_is_needed;
static int temporary_variables_declaration_generation_is_needed;
/* Number of string constant declarations so far generated in program (see
commentaries for string_name_prefix. The variable is used only on
STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS. */
static int number_of_generated_string_constant_declarations;
/* Order number of expression among expressions with temporary variables in
current block (see commentaries for temp_actuals_name). */
static int number_of_expression_with_temp_variables;
/* Order number of the current call in current block (see commentaries for
temp_actuals_name). */
static int number_of_current_call;
/* Order number of the temporary variable in current expression of current
block (see commentaries for temp_variables_name). */
static int number_of_current_temporary_variable;
/* Order number of the temporary variable in current call of current
block (see commentaries for temp_actuals_name. The variable is used only on
TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS and
STATEMENTS_GENERATION_PASS. */
static int number_of_temp_variable_in_current_call;
/* The following value is TRUE if current expression needs temporary
variables for implementation (see commentaries for
temp_expression_variables_names_prefix). The variable is used
only on TEMPORARY_VARIABLES_DECLARATION_GENERATION_PASS. */
static int current_expression_needs_temp_variables;
/* The following value is number of current level of nesting with
statements (see commentaries for with_variable_name_prefix). */
static int current_level_of_nesting_with_statements;
/* The following values are number of generated labels (see commentaries
for label_name_prefix), number of exit label for current loop statement
and number of return label for current block. The variables are used
only on STATEMENTS_GENERATION_PASS. */
static int number_of_generated_labels;
static int number_of_exit_label_for_current_loop;
static int number_of_return_label_for_current_block;
/* This page contains macros and function for generation of expression
varieties. These varieties are the following or combinations of the
following.
- expression of type ADDRESS is represented as expression of type CARDINAL
(vs. POINTER TO WORD as generally);
- expression of type pointer implemented by C structure (see commentaries
for pointer_structure_name_prefix) is represented by member of the
corresponding structure;
- object is represented by its address (by not its value);
- int value is generated for character which is represented by one-character
string. */
/* The following macros is used for encoding all flags of the expression
variety by the single integer value. */
#define ENCODE_GENERATION_FLAGS(adr_as_card, through_struct_ptr, adr, ch)\
(adr_as_card | through_struct_ptr << 1 | adr << 2 | ch << 3)
/* The following macros is used for decoding all flags of the expression
variety from the single integer value. */
#define DECODE_GENERATION_FLAGS(flags, adr_as_card, through_str_ptr, adr, ch)\
(adr_as_card = flags & 01, through_str_ptr = flags >> 1 & 01,\
adr = flags >> 2 & 01, ch = flags >> 3 & 01)
/* The following macros define codes of all used expression varieties (see
commentaries at the page begin). */
#define ADDRESS_AS_CARDINAL_CODE\
ENCODE_GENERATION_FLAGS (TRUE, FALSE, FALSE, FALSE)
#define THROUGH_STRUCT_POINTER_CODE\
ENCODE_GENERATION_FLAGS (FALSE, TRUE, FALSE, FALSE)
#define ADDRESS_CODE\
ENCODE_GENERATION_FLAGS (FALSE, FALSE, TRUE, FALSE)
#define CHARACTER_CODE ENCODE_GENERATION_FLAGS (FALSE, FALSE, FALSE, TRUE)
#define ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_CODE\
ENCODE_GENERATION_FLAGS (TRUE, TRUE, FALSE, FALSE)
#define THROUGH_STRUCT_POINTER_OR_ADDRESS_CODE\
ENCODE_GENERATION_FLAGS (FALSE, TRUE, TRUE, FALSE)
#define ADDRESS_AS_CARDINAL_OR_CHARACTER_CODE\
ENCODE_GENERATION_FLAGS (TRUE, FALSE, FALSE, TRUE)
#define THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE\
ENCODE_GENERATION_FLAGS (FALSE, TRUE, FALSE, TRUE)
#define ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE\
ENCODE_GENERATION_FLAGS (TRUE, TRUE, FALSE, TRUE)
/* The following macros are used for generation of all possible expression
varieties (see commentaries at the page begin). */
#define GEN_ADDRESS_AS_CARDINAL(expr)\
generate_expression_according_to_flags(expr, ADDRESS_AS_CARDINAL_CODE)
#define GEN_THROUGH_STRUCT_POINTER(expr)\
generate_expression_according_to_flags(expr, THROUGH_STRUCT_POINTER_CODE)
#define GEN_ADDRESS(expr)\
generate_expression_according_to_flags(expr, ADDRESS_CODE)
#define GEN_CHARACTER(expr)\
generate_expression_according_to_flags(expr, CHARACTER_CODE)
#define GEN_ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER(expr)\
generate_expression_according_to_flags\
(expr, ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_CODE)
#define GEN_THROUGH_STRUCT_POINTER_OR_ADDRESS(expr)\
generate_expression_according_to_flags\
(expr, THROUGH_STRUCT_POINTER_OR_ADDRESS_CODE)
#define GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER(expr)\
generate_expression_according_to_flags\
(expr, ADDRESS_AS_CARDINAL_OR_CHARACTER_CODE)
#define GEN_THROUGH_STRUCT_POINTER_OR_CHARACTER(expr)\
generate_expression_according_to_flags\
(expr, THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE)
#define GEN_ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_OR_CHARACTER(expr)\
generate_expression_according_to_flags\
(expr, ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE)
/* The following function generates access to member name when ICODE_PTR
represents type pointer implemented by C structure (see commentaries
for pointer_structure_name_prefix). The function is used for
generation of expression of such type. ICODE_PTR is to be not NULL. */
add_pointer_member_name (icode_ptr)
register ICN_pointer icode_ptr;
{
if (MODE (icode_ptr) == ICNM_POINTER_TYPE
&&
POINTER_TYPE_NODE (icode_ptr)->corresponding_C_structure_was_generated)
{
if (fprintf (output_file, ".%s", pointer_member_name) < 0)
output_file_error ();
}
}
/* The following function generates variety of expression EXPR. The
expression itself is generated only on statement generation pass. On
the other pass the processing expression may generates various structures
(see commentaries for generator_pass). The variety is defined by FLAGS.
EXPR is to represent expression. */
static void
generate_expression_according_to_flags (expr, flags)
ICN_pointer expr;
int flags;
{
register int right_bracket_is_needed, address_as_cardinal_flag;
register int through_struct_pointer_flag, address_flag, character_flag;
semantic_information sinf;
if (it_is_statements_generation_pass)
{
subgraph_is_expression (expr, &sinf);
DECODE_GENERATION_FLAGS
(flags, address_as_cardinal_flag, through_struct_pointer_flag,
address_flag, character_flag);
right_bracket_is_needed = TRUE;
if (address_as_cardinal_flag
&& sinf.type == ICN_POINTER (&ICN_TD_address))
{
/* Transform pointer to type of cardinal representation. */
if (fprintf (output_file, "((%s)", address_type_representation) < 0)
output_file_error ();
}
else if (address_flag && MODE (sinf.type) != ICNM_ARRAY_TYPE
&& !it_is_string_type (sinf.type)
&& !sinf.it_is_array_parameter)
{
if (fputs ("(&", output_file) == EOF)
output_file_error ();
}
else if (character_flag && it_is_string_type (sinf.type))
{
/* Character is represented by string. */
if (fputs ("(*", output_file) == EOF)
output_file_error ();
}
#ifndef MODULA_CHAR_IS_IMPLEMENTED_BY_C_UNSIGNED_CHAR
else if (character_flag && sinf.type == ICN_POINTER (&ICN_TD_char))
{
if (fputc ('(', output_file) == EOF)
output_file_error ();
}
#endif
else
right_bracket_is_needed = FALSE;
}
generate_expression (expr);
if (it_is_statements_generation_pass)
{
if (sinf.it_is_designator && through_struct_pointer_flag)
add_pointer_member_name (sinf.type);
#ifndef MODULA_CHAR_IS_IMPLEMENTED_BY_C_UNSIGNED_CHAR
else if (character_flag && (it_is_string_type (sinf.type)
|| sinf.type == ICN_POINTER (&ICN_TD_char)))
{
if (fprintf (output_file, "&0%o", CHARACTER_MAX) < 0)
output_file_error ();
}
#endif
if (right_bracket_is_needed)
{
if (fputc (')', output_file) == EOF)
output_file_error ();
}
if (address_flag && sinf.it_is_array_parameter)
{
if (fprintf (output_file, ".%s", open_array_address_name) < 0)
output_file_error ();
}
}
}
/* The page contains auxiliary functions mainly for icode navigation for
generation time. */
/* The following function fixes error of unsuccessful output to the object
file. */
static void
output_file_error ()
{
error_with_parameter (ERR_output_to_object_file, modula_output_file_name);
}
/* The function returns TRUE if OBJECT is nested in scope given by PARENT.
OBJECT is to be NULL or refer to denotation. */
static int
it_is_parent (parent, object)
register ICN_pointer parent, object;
{
while (object != NULL)
if (SCOPE (object) == parent)
return TRUE;
else
object = SCOPE (object);
return FALSE;
}
/* The following function returns the nearest (after DENOTATION) textually
module which is declared in the same scope as DENOTATION. If such module
does not exist the function return NULL. DENOTATION is to refer to
denotation. */
static ICN_pointer
next_brother_module (denotation)
register ICN_pointer denotation;
{
register ICN_pointer declaration_element;
for (declaration_element = NEXT_DECLARATION_ELEMENT (denotation);
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (MODE (declaration_element) == ICNM_MODULE)
{
if (SCOPE (declaration_element) == SCOPE (denotation))
return declaration_element;
else if (!it_is_parent (SCOPE (denotation), declaration_element))
break;
}
return NULL;
}
/* The following function returns the first (after non-null DENOTATION)
textually module which is declared in scope defined by DENOTATION. If such
module does not exist the function return NULL. */
static ICN_pointer
first_son_module (denotation)
register ICN_pointer denotation;
{
register ICN_pointer declaration_element;
for (declaration_element = NEXT_DECLARATION_ELEMENT (denotation);
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (MODE (declaration_element) == ICNM_MODULE)
{
if (SCOPE (declaration_element) == denotation)
return declaration_element;
else if (!it_is_parent (denotation, declaration_element))
break;
}
return NULL;
}
/* The following function returns the most nested scope which defines new
declaration level (i.e. procedure or compilation unit) and which contains
DENOTATION. If DENOTATION itself defines new declaration level then the
function returns DENOTATION. DENOTATION is to be NULL or to refer to
denotation. */
static ICN_pointer
declaration_level_scope (denotation)
register ICN_pointer denotation;
{
for (; denotation != NULL && MODE (denotation) != ICNM_PROCEDURE
&& (MODE (denotation) != ICNM_MODULE
|| ((enum module_mode) MODULE_NODE (denotation)->module_mode
== MM_LOCAL_MODULE));
denotation = SCOPE (denotation)) ;
return denotation;
}
/* The following function returns TRUE if scope SCOPE defines new declaration
level (i.e. procedure or compilation unit) and contains any variable
declaration on this declaration level. SCOPE is to refer to
denotation. */
static int
declaration_level_scope_has_variable_declarations (scope)
ICN_pointer scope;
{
register ICN_pointer declaration_element;
for (declaration_element = NEXT_DECLARATION_ELEMENT (scope);
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (MODE (declaration_element) == ICNM_VARIABLE_DECLARATION
&& declaration_level_scope (declaration_element) == scope)
return TRUE;
return FALSE;
}
/* The following function returns textually next (after declaration element
START) variable declared immediately within SCOPE. */
static ICN_pointer
next_variable_declaration (scope, start)
ICN_pointer scope, start;
{
register ICN_pointer declaration_element;
for (declaration_element = NEXT_DECLARATION_ELEMENT (start);;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (declaration_element == NULL
|| (MODE (declaration_element) == ICNM_VARIABLE_DECLARATION
&& SCOPE (declaration_element) == scope))
return declaration_element;
}
/* The function returns TRUE if PROCEDURE_PTR is exported from current
compilation unit. PROCEDURE_PTR is to refer procedure node. */
static int
procedure_is_exported_from_compilation_unit (procedure_ptr)
ICN_pointer procedure_ptr;
{
if (MODE (procedure_ptr) != ICNM_PROCEDURE)
abort ();
return
(IN_DEFINITION_MODULE (procedure_ptr)
|| (find_denotation_in_second_space (current_compilation_unit,
IDENTIFIER (procedure_ptr))
!= NULL
&& find_identifier_declaration (current_compilation_unit,
IDENTIFIER (procedure_ptr))
== procedure_ptr));
}
/* This page contains functions for generation of names and declarations
and some auxiliary functions. */
/* The function outputs generated name of OBJECT to object file. If
VAR_PREFIX_IS_NEEDED is TRUE than var_prefix (see commentaries for it) is
output before the name. OBJECT is to refer to field, formal parameter,
variable, procedure, type declaration or to non-local module.
All generated names start with `_'. As a consequence C keyword can not be
generated. The generated name of field or formal parameter is the
corresponding identifier. The other kind object name is the corresponding
identifier and identifiers of all scopes (procedure or module) which
contain given object. All the identifiers are separated by `_'.
An exception of the rules is the procedure exported from the compilation
unit. For such procedure only the compilation unit name is output.
Remember that the following situation may exist.
DEFINITION MODULE a; PROCEDURE proc; END a.
IMPLEMENTATION MODULE a;
MODULE b; EXPORT proc;...; END b;
END a.
*/
static void
output_object_full_name (object, var_prefix_is_needed)
register ICN_pointer object;
int var_prefix_is_needed;
{
register enum icode_node_mode object_mode;
register int export_flag;
register ICN_pointer scope;
if (var_prefix_is_needed)
{
if (fputs (var_prefix, output_file) == EOF)
output_file_error ();
}
if (fputc ('_', output_file) == EOF)
output_file_error ();
object_mode = MODE (object);
if (object_mode == ICNM_FIELD || object_mode == ICNM_FORMAL_PARAMETER)
{
if (fputs (IDENTIFIER_NODE (IDENTIFIER (object))->identifier_string,
output_file) == EOF)
output_file_error ();
}
else if (object_mode == ICNM_VARIABLE_DECLARATION)
{
if (fputs (IDENTIFIER_NODE (IDENTIFIER (object))->identifier_string,
output_file) == EOF)
output_file_error ();
/* Output also names of all upper scopes. */
for (scope = SCOPE (object);
scope != NULL && MODE (scope) != ICNM_PROCEDURE
&& SCOPE (scope) != NULL;
scope = SCOPE (scope))
{
if (fputc ('_', output_file) == EOF)
output_file_error ();
if (fputs (IDENTIFIER_NODE (IDENTIFIER (scope))->identifier_string,
output_file) == EOF)
output_file_error ();
}
}
else if (object_mode == ICNM_PROCEDURE)
{
if (fputs (IDENTIFIER_NODE (IDENTIFIER (object))->identifier_string,
output_file) == EOF)
output_file_error ();
export_flag = procedure_is_exported_from_compilation_unit (object);
/* Output also names of all upper scopes if the procedure is not
exported from the compilation unit. For such procedures only the
compilation unit name is output. Remember that the following
situation may exist.
DEFINITION MODULE a; PROCEDURE proc; END a.
IMPLEMENTATION MODULE a;
MODULE b; EXPORT proc;...; END b;
END a. */
for (scope = SCOPE (object); scope != NULL; scope = SCOPE (scope))
if (!export_flag || SCOPE (scope) == NULL)
{
if (fputc ('_', output_file) == EOF)
output_file_error ();
if (fputs (IDENTIFIER_NODE (IDENTIFIER (scope))->identifier_string,
output_file) == EOF)
output_file_error ();
}
}
else if (object_mode == ICNM_TYPE_DECLARATION
|| (object_mode == ICNM_MODULE
&& ((enum module_mode) MODULE_NODE (object)->module_mode
!= MM_LOCAL_MODULE)))
{
if (fputs (IDENTIFIER_NODE (IDENTIFIER (object))->identifier_string,
output_file) == EOF)
output_file_error ();
/* Output also names of all upper scopes. */
for (scope = SCOPE (object); scope != NULL; scope = SCOPE (scope))
{
if (fputc ('_', output_file) == EOF)
output_file_error ();
if (fputs (IDENTIFIER_NODE (IDENTIFIER (scope))->identifier_string,
output_file) == EOF)
output_file_error ();
}
}
else
abort ();
}
/* The following recursive function generates access to RECORD_ELEMENT.
RECORD_ELEMENT is to refer to field in the original call. In other
(recursive) calls RECORD_ELEMENT is to refer to field, record case part or
to record case variant. Modula-2 record implementation is described in
commentaries for record_case_part_name_prefix and
record_case_variant_name_prefix. */
static void
generate_field_access (record_element)
register ICN_pointer record_element;
{
register ICN_pointer current_record_element;
register enum icode_node_mode mode;
register int member_index;
char *member_prefix;
mode = MODE (record_element);
if (mode == ICNM_RECORD_CASE_VARIANT)
{
member_index = 0;
for (current_record_element = record_element;
MODE (current_record_element) == ICNM_RECORD_CASE_VARIANT;
current_record_element
= (RECORD_CASE_VARIANT_NODE (current_record_element)
->next_case_variant))
member_index++;
member_prefix = record_case_variant_name_prefix;
}
else
{
member_index = 0;
for (current_record_element = record_element;;)
if (MODE (current_record_element) == ICNM_FIELD)
current_record_element
= FIELD_NODE (current_record_element)->next_field;
else if (MODE (current_record_element) == ICNM_RECORD_CASE_PART)
{
member_index++;
current_record_element
= (RECORD_CASE_PART_NODE (current_record_element)
->record_element_after_case_part);
}
else
break;
member_prefix = record_case_part_name_prefix;
}
if (MODE (current_record_element) != ICNM_RECORD)
generate_field_access (current_record_element);
if (mode == ICNM_FIELD)
{
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_object_full_name (record_element, FALSE);
}
else
{
if (fprintf (output_file, ".%s%x", member_prefix,
(unsigned) member_index) < 0)
output_file_error ();
}
}
/* The function outputs STRING only when current pass is statements
generation. */
static void
output_string_when_statements_generation (string)
char *string;
{
if (it_is_statements_generation_pass)
{
if (fputs (string, output_file) == EOF)
output_file_error ();
}
}
/* The table contains basic C type which implements corresponding discrete
(except for enumeration types) Modula-2 type. The discrete type is any
basic type (except for float types) or any enumeration type. The
table element order is the same is enumeration basic_type. */
static char
*discrete_type_representation_table[ /* BTM_SHORT_CARDINAL - BTM_CHAR */ ] =
{shortcard_type_representation, cardinal_type_representation,
longcard_type_representation, shortint_type_representation,
integer_type_representation, longint_type_representation,
NULL, NULL, NULL, char_type_representation
};
/* The function returns C type name which represents corresponding discrete
Modula-2 type. TYPE is to refer to discrete type or to range of it. */
static char *
discrete_type_representation (type)
register ICN_pointer type;
{
type = through_range_type (type);
if (MODE (type) == ICNM_ENUMERATION_TYPE)
type
= type_of_cardinal_value
(CARDINAL_NODE (min_or_max (type, FALSE))->cardinal_value);
return (discrete_type_representation_table
[(int) BASIC_TYPE_NODE (type)->basic_type]);
}
/* The following recursive function generates left or right part (parameter
it_is_left_part) of definition of C type which represents Modula-2
{POINTER TO}* TYPE. TYPE_INDIRECTION (>0) determines number of
constructions `POINTER TO'. TYPE is to refer to type definition.
For example MODULA-2 type `ARRAY [2..10] OF CARDINAL' (with
type_indirection == 2) has
the left part `unsigned int (**' and right part `)[9]'. */
static void
output_type_definition_part (type, type_indirection, it_is_left_part)
register ICN_pointer type;
int type_indirection;
register int it_is_left_part;
{
register enum icode_node_mode type_mode;
char *primitive_type_representation;
if (type == NULL)
{
if (it_is_left_part)
{
if (fputs (void_type_representation, output_file) == EOF)
output_file_error ();
}
}
else
{
type_mode = MODE (type);
if (type_mode == ICNM_POINTER_TYPE)
{
if (POINTER_TYPE_NODE (type)
->corresponding_C_structure_was_generated)
{
/* It is cyclic pointer type which is implemented by C struct. */
if (it_is_left_part)
{
if (fprintf (output_file, "struct %s%lx",
pointer_structure_name_prefix,
(unsigned long) type) < 0)
output_file_error ();
}
}
else
{
++type_indirection;
output_type_definition_part (POINTER_TYPE_NODE (type)->base_type,
type_indirection, it_is_left_part);
return;
}
}
else if (type_mode == ICNM_ARRAY_TYPE)
{
if (it_is_left_part)
{
output_type_definition_part (ARRAY_TYPE_NODE (type)->base_type,
0, it_is_left_part);
if (type_indirection != 0)
{
if (fputc ('(', output_file) == EOF)
output_file_error ();
}
}
else
{
if (type_indirection != 0)
{
if (fputc (')', output_file) == EOF)
output_file_error ();
}
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, "[0%lo]",
array_index_span (ARRAY_TYPE_NODE (type)
->array_index_type) + 1) < 0)
output_file_error ();
#else
if (fprintf (output_file, "[0%o]",
array_index_span (ARRAY_TYPE_NODE (type)
->array_index_type) + 1) < 0)
output_file_error ();
#endif
output_type_definition_part (ARRAY_TYPE_NODE (type)->base_type,
0, it_is_left_part);
}
}
else if (type_mode == ICNM_PROCEDURE_TYPE)
{
type = PROCEDURE_TYPE_NODE (type)->procedure_result_type;
if (type != NULL && (MODE (type) == ICNM_ARRAY_TYPE
|| MODE (type) == ICNM_RECORD))
type = NULL;
if (it_is_left_part)
{
output_type_definition_part (type, 0, it_is_left_part);
if (fputs ("(*", output_file) == EOF)
output_file_error ();
if (type_indirection != 0)
{
if (fputc ('(', output_file) == EOF)
output_file_error ();
}
}
else
{
if (type_indirection != 0)
{
if (fputc (')', output_file) == EOF)
output_file_error ();
}
if (fputs (")()", output_file) == EOF)
output_file_error ();
output_type_definition_part (type, 0, it_is_left_part);
}
}
else if (type_mode == ICNM_RECORD)
{
if (it_is_left_part)
{
if (fputs ("struct ", output_file) == EOF)
output_file_error ();
if (RECORD_NODE (type)->record_type_identifier != NULL)
output_object_full_name
(RECORD_NODE (type)->record_type_identifier, FALSE);
else
{
if (fprintf (output_file, "%s%lx",
anonim_record_name_prefix,
(unsigned long) type) < 0)
output_file_error ();
}
}
}
else if (type_mode == ICNM_QUALIFIED_EXPORT)
{
if (it_is_left_part)
{
if (fprintf (output_file, "%s *", word_type_representation) < 0)
output_file_error ();
}
}
else
{
if (type == ICN_POINTER (&ICN_TD_short_real))
primitive_type_representation = shortreal_type_representation;
else if (type == ICN_POINTER (&ICN_TD_real))
primitive_type_representation = real_type_representation;
else if (type == ICN_POINTER (&ICN_TD_long_real))
primitive_type_representation = longreal_type_representation;
else if (type == ICN_POINTER (&ICN_TD_byte))
primitive_type_representation = byte_type_representation;
else if (type == ICN_POINTER (&ICN_TD_word))
primitive_type_representation = word_type_representation;
else if (type_mode == ICNM_SET_TYPE)
primitive_type_representation = set_type_representation;
else
primitive_type_representation
= discrete_type_representation (type);
if (it_is_left_part)
{
if (fputs (primitive_type_representation, output_file) == EOF)
output_file_error ();
}
}
}
/* Process indirections. */
if (it_is_left_part)
while (type_indirection-- != 0)
if (fputc ('*', output_file) == EOF)
output_file_error ();
}
/* The following recursive function is analogous to function
output_type_definition_part. The first difference is that the function
suggests that type_indirection == 0 (see previous function's
commentaries. The second difference is that the function tests type
on cyclic pointer type implemented by a C structure. And if the last
is true then the function generates declaration of the member type
(not the structure type itself). */
static void
output_type_definition_part_through_struct_pointer (type, it_is_left_part)
register ICN_pointer type;
register int it_is_left_part;
{
if (MODE (type) == ICNM_POINTER_TYPE
&& POINTER_TYPE_NODE (type)->corresponding_C_structure_was_generated)
output_type_definition_part (POINTER_TYPE_NODE (type)->base_type, 1,
it_is_left_part);
else
output_type_definition_part (type, 0, it_is_left_part);
}
/* The following functions generates declaration of object of type
TYPE or POINTER TO TYPE (if INDIRECTION_FLAG is TRUE). The name
of declaration is NAME (if INDEX==0) or NAME followed by INDEX.
TYPE is to refer to type definition. */
static void
generate_declaration_by_name (type, indirection_flag, name, name_index)
ICN_pointer type;
int indirection_flag;
unsigned long name_index;
char *name;
{
output_type_definition_part (type, (indirection_flag ? 1 : 0), TRUE);
if (name_index == 0)
{
if (fprintf (output_file, " %s", name) < 0)
output_file_error ();
}
else
{
if (fprintf (output_file, " %s%lx", name, name_index) < 0)
output_file_error ();
}
output_type_definition_part (type, (indirection_flag ? 1 : 0), FALSE);
}
/* The following function is analogous to generate_declaration_by_object but
the name is determined by OBJECT (see function output_object_full_name).
OBJECT is to refer to field, formal parameter, variable, procedure, type
declaration or to non-local module. */
static void
generate_declaration_by_object (type, indirection_flag, object)
ICN_pointer type;
int indirection_flag;
ICN_pointer object;
{
output_type_definition_part (type, (indirection_flag ? 1 : 0), TRUE);
if (fputc (' ', output_file) == EOF)
output_file_error ();
output_object_full_name (object, FALSE);
output_type_definition_part (type, (indirection_flag ? 1 : 0), FALSE);
}
/* The recursive function generates all C type definitions needed for
declaration of fields which are accessible from RECORD_ELEMENT (see also
commentaries for generate_necessary_type_definitions). */
static void
generate_necessary_field_type_definitions (record_element)
register ICN_pointer record_element;
{
register ICN_pointer variant;
register enum icode_node_mode record_element_mode;
for (;;)
{
record_element_mode = MODE (record_element);
if (record_element_mode == ICNM_FIELD)
{
generate_necessary_type_definitions
(FIELD_NODE (record_element)->field_type);
record_element = FIELD_NODE (record_element)->next_field;
}
else if (record_element_mode == ICNM_RECORD_CASE_PART)
{
for (variant = (RECORD_CASE_PART_NODE (record_element)
->record_case_variant_list);
MODE (variant) == ICNM_RECORD_CASE_VARIANT;
variant = RECORD_CASE_VARIANT_NODE (variant)->next_case_variant)
generate_necessary_field_type_definitions
(RECORD_CASE_VARIANT_NODE (variant)->case_variant_elements);
record_element = (RECORD_CASE_PART_NODE (record_element)
->record_element_after_case_part);
}
else
break;
}
}
/* The function generates all declarations of all fields which are accessible
from RECORD_ELEMENT (see also commentaries for record_case_part_name_prefix
and record_case_variant_name_prefix). The C structures which implements
Modula-2 record case variants are generated here. */
static void
generate_field_declarations (record_element)
register ICN_pointer record_element;
{
register ICN_pointer variant, saved_record_element;
register enum icode_node_mode record_element_mode;
register int case_number, variant_number;
case_number = 0;
saved_record_element = record_element;
/* Fix number of case parts on current level. */
for (;;)
{
record_element_mode = MODE (record_element);
if (record_element_mode == ICNM_FIELD)
record_element = FIELD_NODE (record_element)->next_field;
else if (record_element_mode == ICNM_RECORD_CASE_PART)
{
case_number++;
record_element = (RECORD_CASE_PART_NODE (record_element)
->record_element_after_case_part);
}
else
break;
}
record_element = saved_record_element;
/* Generate declarations of fields. */
for (;;)
{
record_element_mode = MODE (record_element);
if (record_element_mode == ICNM_FIELD)
{
generate_declaration_by_object
(FIELD_NODE (record_element)->field_type, FALSE, record_element);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
record_element = FIELD_NODE (record_element)->next_field;
}
else if (record_element_mode == ICNM_RECORD_CASE_PART)
{
if (fputs ("union{", output_file) == EOF)
output_file_error ();
variant_number = 0;
/* Fix number of variants of current case part. */
for (variant = (RECORD_CASE_PART_NODE (record_element)
->record_case_variant_list);
MODE (variant) == ICNM_RECORD_CASE_VARIANT;
variant = RECORD_CASE_VARIANT_NODE (variant)->next_case_variant)
variant_number++;
/* Generate structures for each variant. */
for (variant = (RECORD_CASE_PART_NODE (record_element)
->record_case_variant_list);
MODE (variant) == ICNM_RECORD_CASE_VARIANT;
variant = RECORD_CASE_VARIANT_NODE (variant)->next_case_variant)
{
if (fputs ("struct{", output_file) == EOF)
output_file_error ();
generate_field_declarations
(RECORD_CASE_VARIANT_NODE (variant)->case_variant_elements);
if (fprintf (output_file, "}%s%x;\n",
record_case_variant_name_prefix,
(unsigned) variant_number--) < 0)
output_file_error ();
}
if (fprintf (output_file, "}%s%x;\n", record_case_part_name_prefix,
(unsigned) case_number--) < 0)
output_file_error ();
record_element = (RECORD_CASE_PART_NODE (record_element)
->record_element_after_case_part);
}
else
break;
}
}
/* The function outputs name of formal parameter (if FORMAL_PARAMETER_TYPE is
type of this formal parameter) or anonim_parameter_type_name_prefix
(if corresponding formal parameter does not exist, e.g. in procedure
type definition). FORMAL_PARAMETER_TYPE is to refer to formal parameter or
procedure parameter type node (see commentaries for icode). */
static
void
output_formal_parameter_type_name (formal_parameter_type)
ICN_pointer formal_parameter_type;
{
if (MODE (formal_parameter_type) != ICNM_FORMAL_PARAMETER)
{
if (fprintf (output_file, "%s%lx", anonim_parameter_type_name_prefix,
(unsigned long) formal_parameter_type) < 0)
output_file_error ();
}
else
output_object_full_name (formal_parameter_type, FALSE);
}
/* The function outputs name of C structure which contains parameters of
procedure of type PROCEDURE_TYPE. The name is par_prefix followed by
procedure name (if PROCEDURE_TYPE is procedure type of this procedure) or
anonim_procedure_type_name_prefix (if corresponding procedure does not exist
, e.g. in procedure type definition). PROCEDURE_TYPE is to refer to type
of procedure with parameters. */
static
void
output_parameter_structure_name (procedure_type)
register ICN_pointer procedure_type;
{
register ICN_pointer first_parameter_type;
first_parameter_type = next_parameter_type (procedure_type);
if (first_parameter_type == NULL)
abort ();
if (fprintf (output_file, "%s", par_prefix) < 0)
output_file_error ();
if (MODE (first_parameter_type) != ICNM_FORMAL_PARAMETER)
{
if (fprintf (output_file, "_%s%lx", anonim_procedure_type_name_prefix,
(unsigned long) procedure_type) < 0)
output_file_error ();
}
else
/* PROCEDURE_TYPE is procedure type of real procedure because
first_parameter_type is represented by real formal parameter. */
output_object_full_name (SCOPE (first_parameter_type), FALSE);
}
/* The function generates all C type definitions (if it has not been generated
yet) needed for declaration of object of TYPE. These definitions are
structures for record type, for cyclic pointer type implemented by C struct
and C structure which contains formal parameters of procedure type.
TYPE is to be NULL or to refer to type definition. */
static void
generate_necessary_type_definitions (type)
register ICN_pointer type;
{
register enum icode_node_mode type_mode;
register ICN_pointer formal_parameter;
int first_parameter_was_processed;
if (type == NULL)
return;
else
{
type_mode = MODE (type);
if (type_mode == ICNM_ARRAY_TYPE)
generate_necessary_type_definitions
(ARRAY_TYPE_NODE (type)->base_type);
else if (type_mode == ICNM_PROCEDURE_TYPE)
{
if (PROCEDURE_TYPE_NODE (type)
->corresponding_C_parameter_structure_was_generated)
return;
/* It will be needed on the following passes. */
LAST_BLOCK_USING_PROCEDURE_TYPE (type) = 0;
PROCEDURE_TYPE_NODE (type)
->corresponding_C_parameter_structure_was_generated = TRUE;
generate_necessary_type_definitions
(PROCEDURE_TYPE_NODE (type)->procedure_result_type);
for (formal_parameter = next_parameter_type (type);
formal_parameter != NULL;
formal_parameter = next_parameter_type (formal_parameter))
generate_necessary_type_definitions
(FORMAL_PARAMETER_TYPE (formal_parameter));
/* Generate C structure which contains formal parameters of given
procedure or procedure type. */
for (first_parameter_was_processed = FALSE,
formal_parameter = next_parameter_type (type);
formal_parameter != NULL;
formal_parameter = next_parameter_type (formal_parameter))
{
if (!first_parameter_was_processed)
{
if (fputs ("struct ", output_file) == EOF)
output_file_error ();
output_parameter_structure_name (type);
if (fputc ('{', output_file) == EOF)
output_file_error ();
first_parameter_was_processed = TRUE;
}
if (ARRAY_FORMAL_PARAMETER (formal_parameter))
{
if (fputs ("struct{", output_file) == EOF)
output_file_error ();
generate_declaration_by_name
(FORMAL_PARAMETER_TYPE (formal_parameter), TRUE,
open_array_address_name, 0);
if (fprintf (output_file, ";%s %s;}",
cardinal_type_representation,
open_array_high_value_name) < 0)
output_file_error ();
output_formal_parameter_type_name (formal_parameter);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
else
{
if (MODE (formal_parameter) != ICNM_FORMAL_PARAMETER)
generate_declaration_by_name
(FORMAL_PARAMETER_TYPE (formal_parameter),
VAR_FORMAL_PARAMETER (formal_parameter),
anonim_parameter_type_name_prefix,
(unsigned long) formal_parameter);
else
{
output_type_definition_part
(FORMAL_PARAMETER_TYPE (formal_parameter),
(VAR_FORMAL_PARAMETER (formal_parameter) ? 1 : 0),
TRUE);
if (fputc (' ', output_file) == EOF)
output_file_error ();
output_object_full_name (formal_parameter, FALSE);
output_type_definition_part
(FORMAL_PARAMETER_TYPE (formal_parameter),
(VAR_FORMAL_PARAMETER (formal_parameter) ? 1 : 0),
FALSE);
}
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
}
if (first_parameter_was_processed)
{
if (fputs ("};\n", output_file) == EOF)
output_file_error ();
}
}
else if (type_mode == ICNM_POINTER_TYPE)
{
if (!POINTER_TYPE_NODE (type)->pointer_is_being_processed)
{
POINTER_TYPE_NODE (type)->pointer_is_being_processed = TRUE;
generate_necessary_type_definitions (POINTER_TYPE_NODE (type)
->base_type);
POINTER_TYPE_NODE (type)->pointer_is_being_processed = FALSE;
}
/* This is cyclic pointer type which is implemented by C struct. */
else if (!(POINTER_TYPE_NODE (type)
->corresponding_C_structure_was_generated))
{
POINTER_TYPE_NODE (type)->corresponding_C_structure_was_generated
= TRUE;
if (fprintf (output_file, "struct %s%lx{",
pointer_structure_name_prefix,
(unsigned long) type) < 0)
output_file_error ();
generate_declaration_by_name
(POINTER_TYPE_NODE (type)->base_type, TRUE,
pointer_member_name, 0);
if (fputs (";};\n", output_file) == EOF)
output_file_error ();
}
}
else if (type_mode == ICNM_RECORD
&& !RECORD_NODE (type)->corresponding_C_structure_was_generated)
{
RECORD_NODE (type)->corresponding_C_structure_was_generated = TRUE;
generate_necessary_field_type_definitions
(RECORD_NODE (type)->record_element_list);
if (fputs ("struct ", output_file) == EOF)
output_file_error ();
if (RECORD_NODE (type)->record_type_identifier != NULL)
output_object_full_name
(RECORD_NODE (type)->record_type_identifier, FALSE);
else
{
if (fprintf (output_file, "%s%lx", anonim_record_name_prefix,
(unsigned long) type) < 0)
output_file_error ();
}
if (fputc ('{', output_file) == EOF)
output_file_error ();
generate_field_declarations
(RECORD_NODE (type)->record_element_list);
if (fputs ("};\n", output_file) == EOF)
output_file_error ();
}
}
}
/* The function returns TRUE if DENOTATION contains procedure. Generally
DENOTATION is a procedure but it may refer to any denotation. */
static int
it_has_nested_procedures (denotation)
register ICN_pointer denotation;
{
register ICN_pointer declaration_element;
for (declaration_element = NEXT_DECLARATION_ELEMENT (denotation);
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (MODE (declaration_element) == ICNM_MODULE
|| MODE (declaration_element) == ICNM_PROCEDURE)
{
if (!it_is_parent (denotation, declaration_element))
break;
else if (MODE (declaration_element) == ICNM_PROCEDURE
&& block_code_may_be_used (declaration_element))
return TRUE;
}
return FALSE;
}
/* The recursive function generates declarations of all variables of all
local modules in SCOPE at the same declaration level (only procedures or
compilation unit define new declaration level). SCOPE is to be procedure or
module (originally only compilation unit). */
static void
generate_variable_declarations_of_local_modules (scope)
ICN_pointer scope;
{
register ICN_pointer local_module, variable;
for (local_module = first_son_module (scope);
local_module != NULL;
local_module = next_brother_module (local_module))
if (MODE (local_module) == ICNM_MODULE)
{
for (variable = next_variable_declaration (local_module, local_module);
variable != NULL;
variable = next_variable_declaration (local_module, variable))
{
generate_declaration_by_object
(VARIABLE_DECLARATION_NODE (variable)->variable_type, FALSE,
variable);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
generate_variable_declarations_of_local_modules (local_module);
}
}
/* The function returns type the representation of which is to be result type
of generated C function. The result type is to be representation of pointer
to the type if INDIRECTION_IS_NEEDED is TRUE after the call. If the type
is NULL then the result type is to be void. The latter case occurs when
the procedure is proper procedure or function procedure with array or
record result type. PROCEDURE is to refer to procedure. */
static ICN_pointer
get_C_function_type (procedure, indirection_is_needed)
register ICN_pointer procedure;
register int *indirection_is_needed;
{
register ICN_pointer result_type;
if (MODE (procedure) != ICNM_PROCEDURE)
abort ();
result_type
= PROCEDURE_TYPE_NODE (PROCEDURE_NODE (procedure)->procedure_type)
->procedure_result_type;
*indirection_is_needed = FALSE;
if (result_type == NULL || MODE (result_type) == ICNM_ARRAY_TYPE
|| MODE (result_type) == ICNM_RECORD)
return NULL;
else if (MODE (result_type) == ICNM_POINTER_TYPE
&& (POINTER_TYPE_NODE (result_type)
->corresponding_C_structure_was_generated))
{
*indirection_is_needed = TRUE;
return POINTER_TYPE_NODE (result_type)->base_type;
}
else
return result_type;
}
/* The function generates interface of COMPILATION_UNIT. The interface
contains extern declarations of exported procedures (if
IT_IS_IMPORTED_MODULE is TRUE) and C struture which contains exported
variables of the compilation unit (and internal variables if
IT_IS_IMPORTED_MODULE is FALSE). COMPILATION_UNIT is to refer to a
compilation unit. */
static void
generate_compilation_unit_interface (compilation_unit, it_is_imported_module)
register ICN_pointer compilation_unit;
int it_is_imported_module;
{
register ICN_pointer declaration_element, C_type, object, variable;
int indirection_is_needed;
if (it_is_imported_module)
/* Generate extern declarations of functions exported by the compilation
unit. */
for (declaration_element = compilation_unit;
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
{
if (IN_DEFINITION_MODULE (declaration_element))
object
= find_identifier_declaration (compilation_unit,
IDENTIFIER (declaration_element));
if (IN_DEFINITION_MODULE (declaration_element)
&& object != NULL
&& MODE (object) == ICNM_PROCEDURE
&& block_code_may_be_used (object)
&& (!it_is_imported_module
|| PROCEDURE_NODE (object)->procedure_is_used))
{
if (fputs ("extern ", output_file) == EOF)
output_file_error ();
C_type = get_C_function_type (object, &indirection_is_needed);
output_type_definition_part
(C_type, (indirection_is_needed ? 1 : 0), TRUE);
if (fputc (' ', output_file) == EOF)
output_file_error ();
output_object_full_name (object, FALSE);
if (fputs ("()", output_file) == EOF)
output_file_error ();
output_type_definition_part
(C_type, (indirection_is_needed ? 1 : 0), FALSE);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
}
/* Generate sructure containing th declarations of variables exported by
the compilation unit. */
if (declaration_level_scope_has_variable_declarations (compilation_unit))
{
if (it_is_imported_module)
{
if (fputs ("extern ", output_file) == EOF)
output_file_error ();
}
if (fputs ("struct {", output_file) == EOF)
output_file_error ();
for (declaration_element = compilation_unit;
declaration_element != NULL;
declaration_element
= NEXT_DECLARATION_ELEMENT (declaration_element))
if (IN_DEFINITION_MODULE (declaration_element)
&& MODE (declaration_element) == ICNM_VARIABLE_DECLARATION)
{
generate_declaration_by_object
(VARIABLE_DECLARATION_NODE (declaration_element)->variable_type,
FALSE, declaration_element);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
if (!it_is_imported_module)
{
/* Generate the structure members which implement non-exported
(internal) variables of the compilation unit. */
for (variable = next_variable_declaration (compilation_unit,
compilation_unit);
variable != NULL;
variable = next_variable_declaration (compilation_unit,
variable))
if (!IN_DEFINITION_MODULE (variable))
{
generate_declaration_by_object
(VARIABLE_DECLARATION_NODE (variable)->variable_type,
FALSE, variable);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
generate_variable_declarations_of_local_modules (compilation_unit);
}
if (fputc ('}', output_file) == EOF)
output_file_error ();
output_object_full_name (compilation_unit, TRUE);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
}
/* The function generates all declarations defined in COMPILATION_UNIT
(needed type definitions, C code fragments in declaration parts, variables
and C union used for implementation of standard procedure ABS).
COMPILATION_UNIT is to refer to a compilation unit. */
static void
generate_all_compilation_unit_declarations (compilation_unit)
register ICN_pointer compilation_unit;
{
register ICN_pointer declaration_element, variable, type;
enum icode_node_mode declaration_element_mode;
/* Generate type definitions of the compilation unit objects
and output C code fragments from the compilation unit declarations. */
for (declaration_element = NEXT_DECLARATION_ELEMENT (compilation_unit);
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
{
declaration_element_mode = MODE (declaration_element);
if (declaration_element_mode == ICNM_VARIABLE_DECLARATION
|| (declaration_element_mode == ICNM_PROCEDURE
&& !IN_DEFINITION_MODULE (declaration_element)
&& block_code_may_be_used (declaration_element))
|| declaration_element_mode == ICNM_TYPE_DECLARATION)
{
if (declaration_element_mode == ICNM_VARIABLE_DECLARATION)
type
= VARIABLE_DECLARATION_NODE (declaration_element)->variable_type;
else if (declaration_element_mode == ICNM_PROCEDURE)
type = PROCEDURE_NODE (declaration_element)->procedure_type;
else
type
= TYPE_DECLARATION_NODE (declaration_element)->type_definition;
generate_necessary_type_definitions (type);
}
else if (declaration_element_mode == ICNM_C_CODE_IN_DECLARATIONS)
{
if (fprintf
(output_file, "\n%s\n",
C_CODE_IN_DECLARATIONS_NODE (declaration_element)->C_text) < 0)
output_file_error ();
}
}
/* Generate C struture containing all variables on declaration level of
the compilation unit. */
if ((enum module_mode) MODULE_NODE (compilation_unit)->module_mode
!= MM_MAIN_MODULE)
generate_compilation_unit_interface (compilation_unit, FALSE);
else if (declaration_level_scope_has_variable_declarations
(compilation_unit))
{
/* It is main module, i.e. exported variables do not exist. */
if (fputs ("struct {", output_file) == EOF)
output_file_error ();
for (variable = next_variable_declaration (compilation_unit,
compilation_unit);
variable != NULL;
variable = next_variable_declaration (compilation_unit, variable))
{
generate_declaration_by_object
(VARIABLE_DECLARATION_NODE (variable)->variable_type, FALSE,
variable);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
generate_variable_declarations_of_local_modules (compilation_unit);
if (fputc ('}', output_file) == EOF)
output_file_error ();
output_object_full_name (compilation_unit, TRUE);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
if (fputs ("extern char *m2_testptr();\n", output_file) == EOF)
output_file_error ();
/* Output C union used for implementation standard procedure ABS. */
if ((enum module_mode) MODULE_NODE (compilation_unit)->module_mode
!= MM_MAIN_MODULE)
{
if (fputs ("extern ", output_file) == EOF)
output_file_error ();
}
if (fprintf
(output_file, "union{%s %s;%s %s;%s %s;\n%s %s;%s %s;%s %s;}%s;\n",
shortint_type_representation,
names_of_variables_for_abs_implementation[(int) BTM_SHORT_INTEGER],
integer_type_representation,
names_of_variables_for_abs_implementation[(int) BTM_INTEGER],
longint_type_representation,
names_of_variables_for_abs_implementation[(int) BTM_LONG_INTEGER],
shortreal_type_representation,
names_of_variables_for_abs_implementation[(int) BTM_SHORT_REAL],
real_type_representation,
names_of_variables_for_abs_implementation[(int) BTM_REAL],
longreal_type_representation,
names_of_variables_for_abs_implementation[(int) BTM_LONG_REAL],
union_name_for_abs_implementation) < 0)
output_file_error ();
}
/* The function generates (if it is necessary) all declarations defined in
imported COMPILATION_UNIT (needed type definitions and exported variables).
COMPILATION_UNIT is to refer to a compilation unit. */
static void
generate_all_imported_module_declarations (compilation_unit)
register ICN_pointer compilation_unit;
{
register ICN_pointer declaration_element, object;
enum icode_node_mode declaration_element_mode;
if (MODULE_NODE (compilation_unit)->module_declaration_was_generated)
return;
MODULE_NODE (compilation_unit)->module_declaration_was_generated = TRUE;
for (declaration_element = compilation_unit;
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
{
if (IN_DEFINITION_MODULE (declaration_element))
object
= find_identifier_declaration
(compilation_unit, IDENTIFIER (declaration_element));
if (IN_DEFINITION_MODULE (declaration_element) && object != NULL)
{
declaration_element_mode = MODE (object);
if (declaration_element_mode == ICNM_VARIABLE_DECLARATION
|| (declaration_element_mode == ICNM_PROCEDURE
&& block_code_may_be_used (object)
&& PROCEDURE_NODE (object)->procedure_is_used)
|| declaration_element_mode == ICNM_TYPE_DECLARATION)
if (declaration_element_mode == ICNM_VARIABLE_DECLARATION)
generate_necessary_type_definitions
(VARIABLE_DECLARATION_NODE (object)->variable_type);
else if (declaration_element_mode == ICNM_PROCEDURE)
generate_necessary_type_definitions
(PROCEDURE_NODE (object)->procedure_type);
else
generate_necessary_type_definitions
(TYPE_DECLARATION_NODE (object)->type_definition);
}
}
generate_compilation_unit_interface (compilation_unit, TRUE);
}
/* The function generates various structures representing variables of
PROCEDURE (see commentaries for var_prefix). If IT_IS_WITHIN_PROCEDURE_BODY
is TRUE than structures is to be in procedure body. The function return
FALSE if the procedure has not variables. In this case the structures are
not generated. PROCEDURE is to refer to a procedure. */
static int
generate_structures_for_procedure_variables (procedure,
it_is_within_procedure_body)
register ICN_pointer procedure;
register int it_is_within_procedure_body;
{
register ICN_pointer variable;
int nested_procedures_exist;
if (!declaration_level_scope_has_variable_declarations (procedure))
return FALSE;
nested_procedures_exist = it_has_nested_procedures (procedure);
if (it_is_within_procedure_body && nested_procedures_exist)
{
if (fputs ("struct ", output_file) == EOF)
output_file_error ();
output_object_full_name (procedure, TRUE);
if (fprintf (output_file, " %s, *%s;\n", var_prefix,
name_of_saved_pointer_to_procedure_variables) < 0)
output_file_error ();
}
if ((it_is_within_procedure_body && !nested_procedures_exist)
|| (!it_is_within_procedure_body && nested_procedures_exist))
{
if (nested_procedures_exist)
{
if (fprintf (output_file, "static struct ") < 0)
output_file_error ();
output_object_full_name (procedure, TRUE);
if (fputc ('{', output_file) == EOF)
output_file_error ();
}
else
{
if (fputs ("struct{", output_file) == EOF)
output_file_error ();
}
for (variable = next_variable_declaration (procedure, procedure);
variable != NULL;
variable = next_variable_declaration (procedure, variable))
{
generate_declaration_by_object
(VARIABLE_DECLARATION_NODE (variable)->variable_type, FALSE,
variable);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
generate_variable_declarations_of_local_modules (procedure);
if (!it_is_within_procedure_body)
{
if (fputs ("}*", output_file) == EOF)
output_file_error ();
output_object_full_name (procedure, TRUE);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
else
{
if (fprintf (output_file, "}%s;\n", var_prefix) < 0)
output_file_error ();
}
}
return TRUE;
}
/* The function generates various structures representing formal parameters of
PROCEDURE (see commentaries for par_prefix). If IT_IS_WITHIN_PROCEDURE_BODY
is TRUE than structures is to be in procedure body. PROCEDURE is to refer
to a procedure. */
static void
generate_structures_for_procedure_parameters (procedure,
it_is_within_procedure_body)
register ICN_pointer procedure;
int it_is_within_procedure_body;
{
register ICN_pointer procedure_type;
procedure_type = PROCEDURE_NODE (procedure)->procedure_type;
if (next_parameter_type (procedure_type) != NULL
&& it_has_nested_procedures (procedure))
{
if (!it_is_within_procedure_body)
{
if (fputs ("static ", output_file) == EOF)
output_file_error ();
}
if (fputs ("struct ", output_file) == EOF)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputs (" *", output_file) == EOF)
output_file_error ();
if (it_is_within_procedure_body)
{
if (fputs (name_of_saved_pointer_to_procedure_parameters,
output_file) == EOF)
output_file_error ();
}
else
output_parameter_structure_name (procedure_type);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
}
/* The function generates all declarations corresponding to PROCEDURE (forward
declaration of the procedure, structures for variables and parameters).
PROCEDURE is to refer to a procedure. */
static void
generate_procedure_declarations (procedure)
register ICN_pointer procedure;
{
register ICN_pointer result_type;
int indirection_is_needed;
result_type = get_C_function_type (procedure, &indirection_is_needed);
if (!procedure_is_exported_from_compilation_unit (procedure))
{
if (fputs ("static ", output_file) == EOF)
output_file_error ();
}
output_type_definition_part (result_type, (indirection_is_needed ? 1 : 0),
TRUE);
if (fputc (' ', output_file) == EOF)
output_file_error ();
output_object_full_name (procedure, FALSE);
if (fputs ("()", output_file) == EOF)
output_file_error ();
output_type_definition_part (result_type, (indirection_is_needed ? 1 : 0),
FALSE);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
generate_structures_for_procedure_variables (procedure, FALSE);
generate_structures_for_procedure_parameters (procedure, FALSE);
}
/* The function initiates data and generates all declarations needed for
generation of CURRENT_COMPILATION_UNIT. CURRENT_COMPILATION_UNIT is to be
refer to the compiled unit. */
static void
generate_all_declarations (current_compilation_unit)
register ICN_pointer current_compilation_unit;
{
register ICN_pointer declaration_element, imported_module;
current_block_number_among_blocks_with_actuals = 0;
last_generated_line_number = 0;
current_level_of_nesting_with_statements = 0;
number_of_generated_labels = 0;
number_of_generated_string_constant_declarations = 0;
generate_all_compilation_unit_declarations (current_compilation_unit);
for (declaration_element
= NEXT_DECLARATION_ELEMENT (current_compilation_unit);
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (MODE (declaration_element) == ICNM_IMPORT)
{
if (MODULE_EXPORTER (declaration_element) == NULL)
imported_module = find_denotation (ICN_POINTER (NULL),
IDENTIFIER (declaration_element));
else
imported_module
= find_denotation (ICN_POINTER (NULL),
MODULE_EXPORTER (declaration_element));
if (imported_module != NULL && MODE (imported_module) == ICNM_MODULE
&& imported_module != ICN_POINTER (&ICN_D_system)
&& imported_module != ICN_POINTER (&ICN_D_cproc))
generate_all_imported_module_declarations (imported_module);
}
for (declaration_element = current_compilation_unit;
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (MODE (declaration_element) == ICNM_PROCEDURE
&& !IN_DEFINITION_MODULE (declaration_element)
&& block_code_may_be_used (declaration_element))
generate_procedure_declarations (declaration_element);
}
/* This page contains functions for generation of expression. */
/* The function generates line directive corresponding to source line of
CONSTRUCTION (it is statement now). The generation is executed only if
statement generation pass is current and it is new line (see commentaries
for last_generated_line_number). */
static void
generate_line_directive (construction)
register ICN_pointer construction;
{
if (it_is_statements_generation_pass
&& LINE (construction) != last_generated_line_number)
{
last_generated_line_number = LINE (construction);
if (fprintf (output_file, "\n#line %d \"%s\"\n",
last_generated_line_number, input_file_name) < 0)
output_file_error ();
}
}
/* The function outputs C representation of Modula-2 constant. CONSTANT is
to refer to constant. */
static void
output_constant_value (constant)
register ICN_pointer constant;
{
register enum icode_node_mode constant_mode;
constant_mode = MODE (constant);
if (constant_mode == ICNM_INTEGER)
{
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, (INTEGER_NODE (constant)->value_type
== ICN_POINTER (&ICN_TD_long_integer)
? "%ldl" : "%ld"),
INTEGER_NODE (constant)->integer_value) < 0)
output_file_error ();
#else
if (fprintf (output_file, "%d", INTEGER_NODE (constant)->integer_value)
< 0)
output_file_error ();
#endif
}
else if (constant_mode == ICNM_CARDINAL)
{
if (it_is_integer_cardinal_type (CARDINAL_NODE (constant)->value_type))
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
{
if (fprintf (output_file, "%ld",
CARDINAL_NODE (constant)->cardinal_value) < 0)
output_file_error ();
}
#else
{
if (fprintf (output_file, "%d",
CARDINAL_NODE (constant)->cardinal_value) < 0)
output_file_error ();
}
#endif
else if (CARDINAL_NODE (constant)->value_type
== ICN_POINTER (&ICN_TD_char))
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
{
if (fprintf (output_file, "\'\\%lo\'",
CARDINAL_NODE (constant)->cardinal_value) < 0)
output_file_error ();
}
#else
{
if (fprintf (output_file, "\'\\%o\'",
CARDINAL_NODE (constant)->cardinal_value) < 0)
output_file_error ();
}
#endif
else
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
{
if (fprintf (output_file, (CARDINAL_NODE (constant)->value_type
== ICN_POINTER (&ICN_TD_long_cardinal)
? "(unsigned long)0%lol"
: "(unsigned)0%lo"),
CARDINAL_NODE (constant)->cardinal_value) < 0)
output_file_error ();
}
#else
{
if (fprintf (output_file, "(unsigned)0%o",
CARDINAL_NODE (constant)->cardinal_value) < 0)
output_file_error ();
}
#endif
}
else if (constant_mode == ICNM_REAL)
{
if (fprintf (output_file, "%.20e", REAL_NODE (constant)->real_value) < 0)
output_file_error ();
}
else if (constant_mode == ICNM_STRING)
{
if (fprintf (output_file, "(%s *)%s%x",
char_type_representation, string_name_prefix,
(unsigned) C_STRING_CONSTANT_NUMBER (constant)) < 0)
output_file_error ();
}
else if (constant_mode == ICNM_SET)
{
if (fprintf (output_file, "0%o", SET_NODE (constant)->set_value) < 0)
output_file_error ();
}
}
/* The function generates C function for test that value belongs to range or
enumeration. The generation is executed only if test flag is on, first pass
(string constants declarations generation) is current and TYPE is type of
range or enumeration. The C function for given type is generated only once.
TYPE is to be not NULL. */
static void
generate_test_function (type)
register ICN_pointer type;
{
register char *str;
if (test_flag
&&
current_generator_pass == STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS
&& ((MODE (type) == ICNM_RANGE_TYPE
&& !RANGE_TYPE_NODE (type)->range_test_function_was_generated)
|| (MODE (type) == ICNM_ENUMERATION_TYPE
&& !(ENUMERATION_TYPE_NODE (type)
->enumeration_value_test_function_was_generated))))
{
if (MODE (type) == ICNM_RANGE_TYPE)
RANGE_TYPE_NODE (type)->range_test_function_was_generated = TRUE;
else
(ENUMERATION_TYPE_NODE (type)
->enumeration_value_test_function_was_generated) = TRUE;
str = discrete_type_representation (type);
if (fprintf (output_file, "static %s %s%lx(e)register %s e;{\n",
str, range_test_function_name_prefix, (unsigned long) type,
str)
< 0)
output_file_error ();
if (fputs ("if(", output_file) == EOF)
output_file_error ();
output_constant_value (min_or_max (type, TRUE));
if (fputs ("<=", output_file) == EOF)
output_file_error ();
#ifndef MODULA_CHAR_IS_IMPLEMENTED_BY_C_UNSIGNED_CHAR
if (through_range_type (type) == ICN_POINTER (&ICN_TD_char))
{
if (fprintf (output_file, "e&0%o && e&0%o", CHARACTER_MAX,
CHARACTER_MAX) < 0)
output_file_error ();
}
else
#endif
{
if (fputs ("e && e", output_file) == EOF)
output_file_error ();
}
if (fputs ("<=", output_file) == EOF)
output_file_error ();
output_constant_value (min_or_max (type, FALSE));
if (fputs (")return e;\nm2_rngovf();}\n", output_file) == EOF)
output_file_error ();
}
}
/* The following function generates EXPRESSION according to FLAGS and possibly
with the call of range test function. The call of procedure for test of
value is generated only when the test flag is on, the expression is not
constant and EXPRESSION_TYPE is of range type. The expression itself
(possibly with the test procedure call) is generated only on statement
generation pass. On the other pass the processing expression may generates
various structures (see commentaries for generator_pass). EXPRESSION is
to represent expression. */
static void
generate_expression_with_test (expression_type, expression, flags)
register ICN_pointer expression_type, expression;
int flags;
{
semantic_information sinf;
if (test_flag && MODE (expression_type) == ICNM_RANGE_TYPE)
{
subgraph_is_expression (expression, &sinf);
if (!sinf.it_is_constant)
{
generate_test_function (expression_type); /*only for range type*/
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "%s%lx((%s)(",
range_test_function_name_prefix,
(unsigned long) expression_type,
discrete_type_representation (expression_type)) < 0)
output_file_error ();
}
}
}
generate_expression_according_to_flags (expression, flags);
if (test_flag && MODE (expression_type) == ICNM_RANGE_TYPE
&& it_is_statements_generation_pass && !sinf.it_is_constant)
{
if (fputs ("))", output_file) == EOF)
output_file_error ();
}
}
/* String constant which is longer then MAX_STRING_PIECE_ON_A_LINE is
divided on several lines. */
#define MAX_STRING_PIECE_ON_A_LINE 45
/* The following function generates EXPRESSION. The expression itself is
generated only on statement generation pass. On the other pass
the processing expression may generates various structures (see
commentaries for generator_pass). EXPRESSION is to represent expression. */
static void
generate_expression (expression)
register ICN_pointer expression;
{
register ICN_pointer ic_ptr;
register enum icode_node_mode expression_mode;
register int n, it_is_set_operation, string_char;
int align;
Tcard size;
char *operation_representation;
Tcard min_index;
semantic_information sinf;
if (expression == NULL)
abort ();
expression_mode = MODE (expression);
if (expression_mode != ICNM_PROCEDURE)
output_string_when_statements_generation ("(");
switch (expression_mode)
{
case ICNM_INTEGER:
case ICNM_CARDINAL:
case ICNM_REAL:
case ICNM_SET:
if (it_is_statements_generation_pass)
output_constant_value (expression);
break;
case ICNM_STRING:
if (it_is_statements_generation_pass)
output_constant_value (expression);
else if ((current_generator_pass
== STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS)
&& !STRING_NODE (expression)->string_declaration_was_generated)
{
STRING_NODE (expression)->string_declaration_was_generated = TRUE;
C_STRING_CONSTANT_NUMBER (expression)
= ++number_of_generated_string_constant_declarations;
if (fprintf (output_file, "static char %s%x[]=\"",
string_name_prefix,
(unsigned) C_STRING_CONSTANT_NUMBER (expression)) < 0)
output_file_error ();
for (n = 0;; n++)
{
string_char
= CHAR_TO_INT (STRING_NODE (expression)->string_value[n]);
if (string_char == '\0')
break;
if ((n + 1) % MAX_STRING_PIECE_ON_A_LINE == 0)
{
if (fputs ("\\\n", output_file) == EOF)
output_file_error ();
}
if (fprintf (output_file,
(isprint (string_char) && string_char != '\''
&& string_char != '\"' && string_char != '\\'
? "%c"
: (string_char < 8
? "\\00%o"
: (string_char < 64 ? "\\0%o" : "\\%o"))),
(unsigned int) string_char) < 0)
output_file_error ();
}
if (fputs ("\";\n", output_file) == EOF)
output_file_error ();
}
break;
case ICNM_VARIABLE_DECLARATION:
if (it_is_statements_generation_pass)
{
ic_ptr = declaration_level_scope (expression);
if (MODE (ic_ptr) == ICNM_PROCEDURE)
{
if (ic_ptr == procedure_over_denotation (current_scope))
{
if (fprintf (output_file, "%s.", var_prefix) < 0)
output_file_error ();
}
else
{
output_object_full_name (ic_ptr, TRUE);
if (fputs ("->", output_file) == EOF)
output_file_error ();
}
}
else
{
output_object_full_name (ic_ptr, TRUE);
if (fputc ('.', output_file) == EOF)
output_file_error ();
}
output_object_full_name (expression, FALSE);
}
break;
case ICNM_FORMAL_PARAMETER:
if (it_is_statements_generation_pass)
{
ic_ptr = PARAMETER_TYPE_OF_FORMAL_PARAMETER (expression);
if (VAR_FORMAL_PARAMETER (ic_ptr)
&& !ARRAY_FORMAL_PARAMETER (ic_ptr))
{
if (fputc ('*', output_file) == EOF)
output_file_error ();
}
ic_ptr = PROCEDURE_NODE (SCOPE (expression))->procedure_type;
if (SCOPE (expression) == procedure_over_denotation (current_scope))
{
if (fputs (par_prefix, output_file) == EOF)
output_file_error ();
}
else
output_parameter_structure_name (ic_ptr);
if (fputs ("->", output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (expression);
}
break;
case ICNM_PROCEDURE:
if (it_is_statements_generation_pass)
output_object_full_name (expression, FALSE);
break;
case ICNM_FIELD:
if (it_is_statements_generation_pass)
{
for (n = 0, ic_ptr = current_scope;
SCOPE (expression) != ic_ptr; ic_ptr = SCOPE (ic_ptr))
n++;
if (fprintf
(output_file, "(*%s%x)", with_variable_name_prefix,
(unsigned) (current_level_of_nesting_with_statements - n)) < 0)
output_file_error ();
generate_field_access (expression);
}
break;
case ICNM_LESS_THAN:
case ICNM_GREATER_THAN:
case ICNM_OR:
case ICNM_AND:
case ICNM_REAL_DIVISION:
case ICNM_ADDITION:
case ICNM_SUBTRACTION:
case ICNM_MULT:
case ICNM_DIV:
case ICNM_MOD:
subgraph_is_expression (LEFT_OPERAND (expression), &sinf);
it_is_set_operation = MODE (sinf.type) == ICNM_SET_TYPE;
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER (LEFT_OPERAND (expression));
if (expression_mode == ICNM_LESS_THAN)
operation_representation = "<";
else if (expression_mode == ICNM_GREATER_THAN)
operation_representation = ">";
else if (expression_mode == ICNM_OR)
operation_representation = "||";
else if (expression_mode == ICNM_AND)
operation_representation = "&&";
else if (expression_mode == ICNM_REAL_DIVISION)
operation_representation = (it_is_set_operation ? "^" : "/");
else if (expression_mode == ICNM_ADDITION)
operation_representation = (it_is_set_operation ? "|" : "+");
else if (expression_mode == ICNM_SUBTRACTION)
operation_representation = (it_is_set_operation ? "&~" : "-");
else if (expression_mode == ICNM_MULT)
operation_representation = (it_is_set_operation ? "&" : "*");
else if (expression_mode == ICNM_DIV)
operation_representation = "/";
else
operation_representation = "%";
output_string_when_statements_generation (operation_representation);
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER (RIGHT_OPERAND (expression));
break;
case ICNM_EQUAL:
case ICNM_UNEQUAL:
subgraph_is_expression (LEFT_OPERAND (expression), &sinf);
if (MODE (sinf.type) == ICNM_ARRAY_TYPE
|| MODE (sinf.type) == ICNM_RECORD
|| (it_is_string_type (sinf.type)
&& (subgraph_is_expression (RIGHT_OPERAND (expression), &sinf),
it_is_string_type (sinf.type))))
{
output_string_when_statements_generation
((expression_mode == ICNM_EQUAL ? "m2_eq(" : "m2_ne("));
if (MODE (sinf.type) == ICNM_RECORD)
output_string_when_statements_generation ("&");
GEN_ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER
(LEFT_OPERAND (expression));
output_string_when_statements_generation
((MODE (sinf.type) == ICNM_RECORD ? ",&" : ","));
GEN_ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER
(RIGHT_OPERAND (expression));
if (it_is_statements_generation_pass)
{
get_type_size_and_alignment (sinf.type, &size, &align);
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, ",0%lo)", size / BYTE_SIZE) < 0)
output_file_error ();
#else
if (fprintf (output_file, ",0%o)", size / BYTE_SIZE) < 0)
output_file_error ();
#endif
}
}
else
{
GEN_ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_OR_CHARACTER
(LEFT_OPERAND (expression));
output_string_when_statements_generation
((expression_mode == ICNM_EQUAL ? "==" : "!="));
GEN_ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_OR_CHARACTER
(RIGHT_OPERAND (expression));
}
break;
case ICNM_LESS_OR_EQUAL:
case ICNM_GREATER_OR_EQUAL:
subgraph_is_expression (LEFT_OPERAND (expression), &sinf);
if (MODE (sinf.type) == ICNM_SET_TYPE)
{
output_string_when_statements_generation
((expression_mode == ICNM_LESS_OR_EQUAL
? "m2_setin(" : "m2_nites("));
GEN_ADDRESS_AS_CARDINAL (LEFT_OPERAND (expression));
output_string_when_statements_generation (",");
GEN_ADDRESS_AS_CARDINAL (RIGHT_OPERAND (expression));
output_string_when_statements_generation (")");
}
else
{
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER (LEFT_OPERAND (expression));
output_string_when_statements_generation
((expression_mode == ICNM_LESS_OR_EQUAL ? "<=" : ">="));
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER (RIGHT_OPERAND (expression));
}
break;
case ICNM_IN:
output_string_when_statements_generation ("in(");
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER (LEFT_OPERAND (expression));
output_string_when_statements_generation (",");
GEN_ADDRESS_AS_CARDINAL (RIGHT_OPERAND (expression));
output_string_when_statements_generation (")");
break;
case ICNM_SIGN_CONVERSION:
case ICNM_NOT:
output_string_when_statements_generation
((expression_mode == ICNM_SIGN_CONVERSION ? "-" : "!"));
GEN_ADDRESS_AS_CARDINAL (OPERAND (expression));
break;
case ICNM_DEREFERENCING:
output_string_when_statements_generation ("*");
if (test_flag && it_is_statements_generation_pass)
{
if (fputc ('(', output_file) == EOF)
output_file_error ();
output_type_definition_part
(DEREFERENCING_NODE (expression)->value_type, 1, TRUE);
output_type_definition_part
(DEREFERENCING_NODE (expression)->value_type, 1, FALSE);
if (fputs (")m2_testptr(", output_file) == EOF)
output_file_error ();
}
GEN_THROUGH_STRUCT_POINTER (OPERAND (expression));
if (test_flag && it_is_statements_generation_pass)
{
if (fputc (')', output_file) == EOF)
output_file_error ();
}
break;
case ICNM_QUALIFICATION:
generate_expression (LEFT_OPERAND (expression));
if (it_is_statements_generation_pass)
generate_field_access (RIGHT_OPERAND (expression));
break;
case ICNM_INDEXING:
subgraph_is_expression (LEFT_OPERAND (expression), &sinf);
generate_expression (LEFT_OPERAND (expression));
operation_representation = "-";
if (it_is_statements_generation_pass)
{
if (sinf.it_is_array_parameter)
{
min_index = 0;
if (fprintf (output_file, ".%s", open_array_address_name) < 0)
output_file_error ();
}
else
{
ic_ptr
= min_or_max (ARRAY_TYPE_NODE (sinf.type)->array_index_type,
TRUE);
if (MODE (ic_ptr) == ICNM_INTEGER)
if (INTEGER_NODE (ic_ptr)->integer_value < 0)
{
operation_representation = "+";
min_index = (-INTEGER_NODE (ic_ptr)->integer_value);
}
else
min_index = INTEGER_NODE (ic_ptr)->integer_value;
else
min_index = CARDINAL_NODE (ic_ptr)->cardinal_value;
}
}
output_string_when_statements_generation ("[");
generate_expression_with_test
(ARRAY_TYPE_NODE (sinf.type)->array_index_type,
RIGHT_OPERAND (expression), ADDRESS_AS_CARDINAL_OR_CHARACTER_CODE);
if (it_is_statements_generation_pass)
{
if (min_index == 0)
{
if (fputc (']', output_file) == EOF)
output_file_error ();
}
else
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
{
if (fprintf (output_file, "%s0%lo]", operation_representation,
min_index) < 0)
output_file_error ();
}
#else
{
if (fprintf (output_file, "%s0%o]", operation_representation,
min_index) < 0)
output_file_error ();
}
#endif
}
break;
case ICNM_ELEMENT_IN_SET_VALUE:
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "(%s)1<<", set_type_representation) < 0)
output_file_error ();
}
generate_expression_with_test
(SET_TYPE_NODE (VALUE_TYPE (expression))->base_type,
OPERAND (expression), ADDRESS_AS_CARDINAL_OR_CHARACTER_CODE);
break;
case ICNM_RANGE_IN_SET_VALUE:
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "~(%s)0<<", set_type_representation) < 0)
output_file_error ();
}
generate_expression_with_test
(SET_TYPE_NODE (VALUE_TYPE (expression))->base_type,
LEFT_OPERAND (expression), ADDRESS_AS_CARDINAL_OR_CHARACTER_CODE);
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "&~(%s)0>>%d-", set_type_representation,
BITS_IN_SET - 1) < 0)
output_file_error ();
}
generate_expression_with_test
(SET_TYPE_NODE (VALUE_TYPE (expression))->base_type,
RIGHT_OPERAND (expression), ADDRESS_AS_CARDINAL_OR_CHARACTER_CODE);
break;
case ICNM_FUNCTION_CALL:
generate_call (expression);
break;
default:
abort ();
}
if (expression_mode != ICNM_PROCEDURE)
output_string_when_statements_generation (")");
}
/* The page contains function for generation of call and other functions
corresponding to temporary variables (the variables may be used for the
generation of the call). */
/* The function finishes generation of structure which contains temporary
variables needed for current expression. The generation is executed only
on temporary variables declaration generation pass and when the current
expression really needs temporary variables. The function also counts
number of temporary variables in block. The function is to be called
after processing all expression. */
static void
finish_expression ()
{
if (current_generator_pass == TEMPORARY_VARIABLES_DECLARATION_GENERATION_PASS
&& current_expression_needs_temp_variables)
{
if (fprintf (output_file, "}%s%x;\n",
temp_expression_variables_names_prefix,
(unsigned) number_of_expression_with_temp_variables) < 0)
output_file_error ();
}
number_of_expression_with_temp_variables++;
current_expression_needs_temp_variables = FALSE;
}
/* The function generate of declaration of temporary variable of TYPE (see
commentaries for temp_variable_name_prefix). The temporary variables of
expression are contained in C structure. The generation is executed only
on temporary variables declaration generation pass (see commentaries for
generator_pass). The call of the function on string constants declarations
generation pass results in necessity of pass of temporary variable
declaration generation. TYPE is to refer to type definition. */
static void
generate_temp_variable_declaration (type)
register ICN_pointer type;
{
if (current_generator_pass == STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS)
temporary_variables_declaration_generation_is_needed = TRUE;
else if (current_generator_pass
== TEMPORARY_VARIABLES_DECLARATION_GENERATION_PASS)
{
if (!current_expression_needs_temp_variables)
{
if (fputs ("struct{", output_file) == EOF)
output_file_error ();
}
current_expression_needs_temp_variables = TRUE;
generate_declaration_by_name
(through_integer_cardinal_type (type), FALSE,
temp_variable_name_prefix,
(unsigned int) number_of_current_temporary_variable);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
}
/* The following function generates function or procedure call.
The call itself is generated only on statement generation pass. On
the other pass the processing call may generate various structures
(see commentaries for generator_pass). CALL is to refer to function
or procedure call. */
static void
generate_call (call)
register ICN_pointer call;
{
register ICN_pointer actual, procedure_designator, formal, first_actual;
register ICN_pointer procedure_type, result_type;
enum icode_node_mode mode;
int struct_for_temp_vars_is_needed, fltemp, it_is_designator;
int it_is_local_procedure, it_may_be_used_for_coroutine;
Tcard formal_size, actual_size;
int align;
semantic_information sinf;
if (MODE (call) == ICNM_PROCEDURE_CALL)
{
procedure_designator = PROCEDURE_CALL_NODE (call)->procedure_designator;
first_actual
= PROCEDURE_CALL_NODE (call)->procedure_actual_parameter_list;
}
else
{
procedure_designator = FUNCTION_CALL_NODE (call)->function_designator;
first_actual = FUNCTION_CALL_NODE (call)->function_actual_parameter_list;
}
number_of_current_call++;
mode = MODE (procedure_designator);
if (mode == ICNM_C_FUNCTION)
/* Call of C function. */
{
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "%s(",
IDENTIFIER_NODE (IDENTIFIER (procedure_designator))
->identifier_string) < 0)
output_file_error ();
}
for (actual = first_actual;
actual != NULL;
actual = ACTUAL_PARAMETER_NODE (actual)->next_actual_parameter)
{
if (actual != first_actual && it_is_statements_generation_pass)
output_string_when_statements_generation (",");
GEN_THROUGH_STRUCT_POINTER
(ACTUAL_PARAMETER_NODE (actual)->actual_parameter_expression);
}
output_string_when_statements_generation
((MODE (call) == ICNM_PROCEDURE_CALL ? ");" : ")"));
}
else if (subgraph_is_type (procedure_designator, &sinf))
/* Call of type transformation. */
{
int temporary_variable_is_needed;
register ICN_pointer temp_variable_type, result_type;
procedure_type = sinf.type;
output_string_when_statements_generation ("(");
if (!it_is_statements_generation_pass)
generate_expression
(ACTUAL_PARAMETER_NODE (first_actual)->actual_parameter_expression);
subgraph_is_expression
(ACTUAL_PARAMETER_NODE (first_actual)->actual_parameter_expression,
&sinf);
temp_variable_type = sinf.type;
/* This code is needed for correct generation INTEGER (2) or
analogous. Otherwise temporary variable will be of type SHORTCARD. */
if (temp_variable_type == ICN_POINTER (&ICN_TD_short_cardinal))
temp_variable_type = ICN_POINTER (&ICN_TD_cardinal);
else if (temp_variable_type == ICN_POINTER (&ICN_TD_short_integer))
temp_variable_type = ICN_POINTER (&ICN_TD_integer);
else if (temp_variable_type
== ICN_POINTER (&ICN_TD_short_cardinal_or_integer))
temp_variable_type = ICN_POINTER (&ICN_TD_cardinal_or_integer);
temporary_variable_is_needed
= !sinf.it_is_designator && !it_is_string_type (temp_variable_type);
if (temporary_variable_is_needed)
{
number_of_current_temporary_variable++;
generate_temp_variable_declaration (temp_variable_type);
if (it_is_statements_generation_pass)
{
if (fprintf
(output_file, "%s.%s%x.%s%x", temp_variables_name,
temp_expression_variables_names_prefix,
(unsigned) number_of_expression_with_temp_variables,
temp_variable_name_prefix,
(unsigned) number_of_current_temporary_variable) < 0)
output_file_error ();
add_pointer_member_name (temp_variable_type);
if (fputc ('=', output_file) == EOF)
output_file_error ();
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
if (fputc (',', output_file) == EOF)
output_file_error ();
}
}
if (it_is_statements_generation_pass)
{
if (MODE (procedure_type) == ICNM_ARRAY_TYPE)
procedure_type = ARRAY_TYPE_NODE (procedure_type)->base_type;
if (fputs ("*(", output_file) == EOF)
output_file_error ();
output_type_definition_part (procedure_type, 1, TRUE);
output_type_definition_part (procedure_type, 1, FALSE);
if (fputc (')', output_file) == EOF)
output_file_error ();
if (MODE (temp_variable_type) != ICNM_ARRAY_TYPE
&& !it_is_string_type (temp_variable_type))
{
if (fputc ('&', output_file) == EOF)
output_file_error ();
}
if (temporary_variable_is_needed)
{
if (fprintf
(output_file, "%s.%s%x.%s%x",
temp_variables_name, temp_expression_variables_names_prefix,
(unsigned) number_of_expression_with_temp_variables,
temp_variable_name_prefix,
(unsigned) number_of_current_temporary_variable) < 0)
output_file_error ();
add_pointer_member_name (temp_variable_type);
}
else
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
}
output_string_when_statements_generation (")");
}
else
/* All except for type tarnsformation and C function. */
{
subgraph_is_expression (procedure_designator, &sinf);
procedure_type = sinf.type;
it_is_designator = sinf.it_is_designator;
if (procedure_type != NULL)
result_type
= PROCEDURE_TYPE_NODE (procedure_type)->procedure_result_type;
it_is_local_procedure
= (!it_is_designator
&& procedure_over_denotation (SCOPE (procedure_designator)) != NULL);
if (procedure_designator == ICN_POINTER (&ICN_D_abs))
/* ABS. */
{
enum basic_type basic_type;
int generation_flag;
subgraph_is_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression, &sinf);
if (it_is_statements_generation_pass)
sinf.type = through_range_type (sinf.type);
generation_flag = (it_is_statements_generation_pass
&& (it_is_integer_type (sinf.type)
|| it_is_real_type (sinf.type)));
if (generation_flag)
{
basic_type = BASIC_TYPE_NODE (sinf.type)->basic_type;
if (fprintf
(output_file, "(%s.%s=", union_name_for_abs_implementation,
names_of_variables_for_abs_implementation[(int) basic_type])
< 0)
output_file_error ();
}
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
if (generation_flag)
{
if (fprintf
(output_file, ")<0?-%s.%s:%s.%s",
union_name_for_abs_implementation,
names_of_variables_for_abs_implementation[(int) basic_type],
union_name_for_abs_implementation,
names_of_variables_for_abs_implementation[(int) basic_type])
< 0)
output_file_error ();
}
}
else if (procedure_designator == ICN_POINTER (&ICN_D_adr))
/* ADR. */
{
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "((%s *)", word_type_representation)
< 0)
output_file_error ();
}
GEN_ADDRESS (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
output_string_when_statements_generation (")");
}
else if (procedure_designator == ICN_POINTER (&ICN_D_cap))
/* CAP. */
{
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "(%s)m2_cap(",
char_type_representation) < 0)
output_file_error ();
}
GEN_CHARACTER (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
output_string_when_statements_generation (")");
}
else if (procedure_designator == ICN_POINTER (&ICN_D_chr)
|| procedure_designator == ICN_POINTER (&ICN_D_float)
|| procedure_designator == ICN_POINTER (&ICN_D_ord)
|| procedure_designator == ICN_POINTER (&ICN_D_trunc))
/* CHR, FLOAT, ORD and TRUNC. */
{
if (it_is_statements_generation_pass)
{
if (fprintf
(output_file, "(%s)",
(procedure_designator == ICN_POINTER (&ICN_D_chr)
? char_type_representation
: (procedure_designator == ICN_POINTER (&ICN_D_float)
? real_type_representation
: (third_edition_flag
? cardinal_type_representation
: integer_type_representation)))) < 0)
output_file_error ();
}
GEN_CHARACTER (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
}
else if (procedure_designator == ICN_POINTER (&ICN_D_dec)
|| procedure_designator == ICN_POINTER (&ICN_D_inc))
/* DEC, INC. */
{
subgraph_is_expression
(ACTUAL_PARAMETER_NODE (first_actual)->actual_parameter_expression,
&sinf);
actual = ACTUAL_PARAMETER_NODE (first_actual)->next_actual_parameter;
if (test_flag
&& (MODE (sinf.type) == ICNM_RANGE_TYPE
|| MODE (sinf.type) == ICNM_ENUMERATION_TYPE))
{
generate_test_function (sinf.type);
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "%s%lx((%s)(",
range_test_function_name_prefix,
(unsigned long) sinf.type,
discrete_type_representation (sinf.type)) < 0)
output_file_error ();
}
}
if (sinf.type == ICN_POINTER (&ICN_TD_address))
output_string_when_statements_generation ("(*(char **)&");
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
if (sinf.type == ICN_POINTER (&ICN_TD_address))
output_string_when_statements_generation (")");
output_string_when_statements_generation
((procedure_designator == ICN_POINTER (&ICN_D_dec)
? (actual == NULL ? "--" : "-=")
: (actual == NULL ? "++" : "+=")));
if (actual != NULL)
/* Second parameter is given. */
if (sinf.type == ICN_POINTER (&ICN_TD_address))
GEN_ADDRESS_AS_CARDINAL (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
else
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
if (test_flag
&& (MODE (sinf.type) == ICNM_RANGE_TYPE
|| MODE (sinf.type) == ICNM_ENUMERATION_TYPE))
output_string_when_statements_generation ("))");
output_string_when_statements_generation (";");
}
else if (procedure_designator == ICN_POINTER (&ICN_D_incl)
|| procedure_designator == ICN_POINTER (&ICN_D_excl))
/* INCL, EXCL. */
{
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
subgraph_is_expression
(ACTUAL_PARAMETER_NODE (first_actual)->actual_parameter_expression,
&sinf);
actual = ACTUAL_PARAMETER_NODE (first_actual)->next_actual_parameter;
if (it_is_statements_generation_pass)
{
if (fprintf (output_file,
(procedure_designator == ICN_POINTER (&ICN_D_excl)
? "&=~((%s)1<<" : "|=((%s)1<<"),
set_type_representation) < 0)
output_file_error ();
}
generate_expression_with_test
(SET_TYPE_NODE (sinf.type)->base_type,
ACTUAL_PARAMETER_NODE (actual)->actual_parameter_expression,
CHARACTER_CODE);
output_string_when_statements_generation (");");
}
else if (procedure_designator == ICN_POINTER (&ICN_D_halt))
/* HALT. */
output_string_when_statements_generation ("m2_halt();");
else if (procedure_designator == ICN_POINTER (&ICN_D_high))
{
if (it_is_statements_generation_pass && !third_edition_flag)
fprintf (output_file, "(%s)", integer_type_representation);
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, ".%s", open_array_high_value_name) < 0)
output_file_error ();
}
}
else if (procedure_designator == ICN_POINTER (&ICN_D_odd))
/* ODD. */
{
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
output_string_when_statements_generation ("%2!=0");
}
else if (procedure_designator == ICN_POINTER (&ICN_D_val))
/* VAL. */
{
register ICN_pointer first_actual_expression;
first_actual_expression = (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
actual = ACTUAL_PARAMETER_NODE (first_actual)->next_actual_parameter;
if (test_flag
&& MODE (first_actual_expression) == ICNM_ENUMERATION_TYPE)
{
subgraph_is_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression, &sinf);
if (!sinf.it_is_constant)
{
generate_test_function (first_actual_expression);
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "%s%lx((%s)(",
range_test_function_name_prefix,
(unsigned long) first_actual_expression,
discrete_type_representation
(first_actual_expression))
< 0)
output_file_error ();
}
}
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
if (!sinf.it_is_constant)
output_string_when_statements_generation ("))");
}
else
{
if (it_is_statements_generation_pass)
{
if (fprintf
(output_file, "(%s)",
discrete_type_representation (first_actual_expression))
< 0)
output_file_error ();
}
generate_expression
(ACTUAL_PARAMETER_NODE (actual)->actual_parameter_expression);
}
}
else if (procedure_designator == ICN_POINTER (&ICN_D_transfer))
/* TRANSFER. */
{
output_string_when_statements_generation ("m2_transfer(");
GEN_THROUGH_STRUCT_POINTER_OR_ADDRESS
(ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
actual = ACTUAL_PARAMETER_NODE (first_actual)->next_actual_parameter;
output_string_when_statements_generation (",");
GEN_THROUGH_STRUCT_POINTER_OR_ADDRESS
(ACTUAL_PARAMETER_NODE (actual)->actual_parameter_expression);
output_string_when_statements_generation (");");
}
else if (procedure_designator == ICN_POINTER (&ICN_D_newprocess))
/* NEWPROCESS. */
{
output_string_when_statements_generation ("m2_newprocess(");
generate_expression (ACTUAL_PARAMETER_NODE (first_actual)
->actual_parameter_expression);
actual = ACTUAL_PARAMETER_NODE (first_actual)->next_actual_parameter;
output_string_when_statements_generation (",");
GEN_THROUGH_STRUCT_POINTER
(ACTUAL_PARAMETER_NODE (actual)->actual_parameter_expression);
actual = ACTUAL_PARAMETER_NODE (actual)->next_actual_parameter;
output_string_when_statements_generation (",");
generate_expression
(ACTUAL_PARAMETER_NODE (actual)->actual_parameter_expression);
actual = ACTUAL_PARAMETER_NODE (actual)->next_actual_parameter;
output_string_when_statements_generation (",");
GEN_THROUGH_STRUCT_POINTER_OR_ADDRESS
(ACTUAL_PARAMETER_NODE (actual)->actual_parameter_expression);
output_string_when_statements_generation (");");
}
else
/* User-defined procedures. */
{
ICN_pointer type_of_formal;
int two_brackets_are_needed, temp_variable_flag;
struct_for_temp_vars_is_needed = FALSE;
output_string_when_statements_generation ("(");
for (formal = next_parameter_type (procedure_type),
actual = first_actual;
actual != NULL;
formal = next_parameter_type (formal),
actual = ACTUAL_PARAMETER_NODE (actual)->next_actual_parameter)
{
subgraph_is_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression, &sinf);
sinf.type = through_integer_cardinal_type (sinf.type);
temp_variable_flag
= (ARRAY_FORMAL_PARAMETER (formal)
&& !VAR_FORMAL_PARAMETER (formal)
&& !sinf.it_is_array_parameter && !sinf.it_is_designator
&& MODE (sinf.type) != ICNM_ARRAY_TYPE
&& !it_is_string_type (sinf.type));
if (temp_variable_flag)
{
type_of_formal = FORMAL_PARAMETER_TYPE (formal);
temp_variable_flag
= (temp_variable_flag
&& (type_of_formal == ICN_POINTER (&ICN_TD_word)
|| type_of_formal == ICN_POINTER (&ICN_TD_byte)));
}
if ((it_is_statements_generation_pass
||
current_generator_pass
==
TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS)
&& temp_variable_flag)
number_of_temp_variable_in_current_call++;
if (current_generator_pass
== STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS)
actuals_structures_declaration_generation_is_needed = TRUE;
else if (current_generator_pass
== ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS)
{
/* Generate struct for procedure actuals. */
if (LAST_BLOCK_USING_PROCEDURE_TYPE (procedure_type)
!= current_block_number_among_blocks_with_actuals)
{
LAST_BLOCK_USING_PROCEDURE_TYPE (procedure_type)
= current_block_number_among_blocks_with_actuals;
if (fputs ("struct ", output_file) == EOF)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc (' ', output_file) == EOF)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
}
if ((current_generator_pass
== TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS
|| (current_generator_pass
== STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS))
&& temp_variable_flag)
{
if (current_generator_pass
== STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS)
temp_array_actuals_structures_declaration_generation_is_needed
= TRUE;
else
/* Generate temp var for procedure actual. */
{
if (!struct_for_temp_vars_is_needed)
{
if (fputs ("struct{", output_file) == EOF)
output_file_error ();
}
struct_for_temp_vars_is_needed = TRUE;
generate_declaration_by_name
(sinf.type, FALSE, actual_name_prefix,
(unsigned int) number_of_temp_variable_in_current_call);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
}
/* Generate one actual pass (assignment to member of structure
for actual and usage of temp variable if it is needed).
The following code implements only one case (see if guard). */
if (!ARRAY_FORMAL_PARAMETER (formal)
&& !VAR_FORMAL_PARAMETER (formal)
&& MODE (FORMAL_PARAMETER_TYPE (formal)) != ICNM_ARRAY_TYPE)
{
temp_variable_flag
= (it_is_real_type (sinf.type)
&& (FORMAL_PARAMETER_TYPE (formal)
== ICN_POINTER (&ICN_TD_word)
|| (FORMAL_PARAMETER_TYPE (formal)
== ICN_POINTER (&ICN_TD_byte))));
if (!sinf.it_is_designator && temp_variable_flag)
{
number_of_current_temporary_variable++;
generate_temp_variable_declaration (sinf.type);
if (it_is_statements_generation_pass)
{
if (fprintf
(output_file, "%s.%s%x.%s%x=",
temp_variables_name,
temp_expression_variables_names_prefix,
(unsigned) number_of_expression_with_temp_variables,
temp_variable_name_prefix,
(unsigned) number_of_current_temporary_variable)
< 0)
output_file_error ();
}
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
output_string_when_statements_generation (",");
}
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "%s.", all_actuals_name) < 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
add_pointer_member_name (FORMAL_PARAMETER_TYPE (formal));
if (fputc ('=', output_file) == EOF)
output_file_error ();
}
if (temp_variable_flag)
{
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "(*(%s *)&",
(FORMAL_PARAMETER_TYPE (formal)
== ICN_POINTER (&ICN_TD_word)
? word_type_representation
: byte_type_representation)) < 0)
output_file_error ();
}
if (sinf.it_is_designator)
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
else if (it_is_statements_generation_pass)
{
if (fprintf
(output_file, "%s.%s%x.%s%x",
temp_variables_name,
temp_expression_variables_names_prefix,
(unsigned) number_of_expression_with_temp_variables,
temp_variable_name_prefix,
(unsigned) number_of_current_temporary_variable)
< 0)
output_file_error ();
}
output_string_when_statements_generation (")");
}
else
{
if (it_is_statements_generation_pass
&& (FORMAL_PARAMETER_TYPE (formal)
== ICN_POINTER (&ICN_TD_address)))
{
if (fprintf (output_file, "(%s *)",
word_type_representation) < 0)
output_file_error ();
}
else if (it_is_statements_generation_pass
&& ((FORMAL_PARAMETER_TYPE (formal)
== ICN_POINTER (&ICN_TD_word))
|| (FORMAL_PARAMETER_TYPE (formal)
== ICN_POINTER (&ICN_TD_byte))))
{
if (fprintf (output_file, "(%s)",
(FORMAL_PARAMETER_TYPE (formal)
== ICN_POINTER (&ICN_TD_word))
? word_type_representation
: byte_type_representation) < 0)
output_file_error ();
}
else if (it_is_statements_generation_pass
&& (sinf.type == ICN_POINTER (&ICN_TD_address)))
{
if (fputs ("(", output_file) == EOF)
output_file_error ();
output_type_definition_part_through_struct_pointer
(FORMAL_PARAMETER_TYPE (formal), TRUE);
output_type_definition_part_through_struct_pointer
(FORMAL_PARAMETER_TYPE (formal), FALSE);
if (fputs (")", output_file) == EOF)
output_file_error ();
}
generate_expression_with_test
(FORMAL_PARAMETER_TYPE (formal),
ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression,
THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE);
}
}
else if (!it_is_statements_generation_pass)
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
/* Generate one actual pass (assignment to member of structure
for actual and usage of temp variable if it is needed).
The following code implements all others cases (see analogous
commentary upper). */
if (it_is_statements_generation_pass)
{
if (!ARRAY_FORMAL_PARAMETER (formal))
{
if (VAR_FORMAL_PARAMETER (formal))
{
if (fprintf (output_file, "%s.", all_actuals_name)
< 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
if (fputc ('=', output_file) == EOF)
output_file_error ();
type_of_formal = FORMAL_PARAMETER_TYPE (formal);
if (type_of_formal == ICN_POINTER (&ICN_TD_address))
{
if (fprintf (output_file, "(%s **)",
word_type_representation) < 0)
output_file_error ();
}
else if (type_of_formal
== ICN_POINTER (&ICN_TD_word))
{
if (fprintf (output_file, "(%s *)",
word_type_representation) < 0)
output_file_error ();
}
else if (type_of_formal
== ICN_POINTER (&ICN_TD_byte))
{
if (fprintf (output_file, "(%s *)",
byte_type_representation) < 0)
output_file_error ();
}
else if (MODE (type_of_formal) == ICNM_ARRAY_TYPE)
{
if (fputc ('(', output_file) == EOF)
output_file_error ();
output_type_definition_part
(type_of_formal, 1, TRUE);
output_type_definition_part
(type_of_formal, 1, FALSE);
if (fputc (')', output_file) == EOF)
output_file_error ();
}
GEN_ADDRESS (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
}
else if (MODE (FORMAL_PARAMETER_TYPE (formal))
== ICNM_ARRAY_TYPE)
{
if (fprintf (output_file, "m2_assarr(%s.",
all_actuals_name) < 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
if (fputc (',', output_file) == EOF)
output_file_error ();
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
get_type_size_and_alignment
(FORMAL_PARAMETER_TYPE (formal), &formal_size,
&align);
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, ",0%lo)", formal_size) < 0)
output_file_error ();
#else
if (fprintf (output_file, ",0%o)", formal_size) < 0)
output_file_error ();
#endif
}
}
else if (sinf.it_is_array_parameter)
{
if (fprintf (output_file, "%s.", all_actuals_name) < 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
if (fprintf (output_file, ".%s=",
open_array_address_name) < 0)
output_file_error ();
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
if (fprintf (output_file, ".%s,%s.",
open_array_address_name, all_actuals_name)
< 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
if (fprintf (output_file, ".%s=",
open_array_high_value_name) < 0)
output_file_error ();
generate_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
if (fprintf (output_file, ".%s",
open_array_high_value_name) < 0)
output_file_error ();
}
else
{
if (temp_variable_flag)
{
if (fprintf
(output_file, "%s.%s%x.%s%x",
temp_actuals_name,
temp_call_actuals_name_prefix,
(unsigned) number_of_current_call,
actual_name_prefix,
(unsigned) number_of_temp_variable_in_current_call)
< 0)
output_file_error ();
add_pointer_member_name (sinf.type);
if (fputc ('=', output_file) == EOF)
output_file_error ();
GEN_THROUGH_STRUCT_POINTER
(ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
if (fprintf (output_file, ",%s.", all_actuals_name)
< 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
if (fprintf (output_file, ".%s=",
open_array_address_name) < 0)
output_file_error ();
type_of_formal = FORMAL_PARAMETER_TYPE (formal);
if (type_of_formal == ICN_POINTER (&ICN_TD_word))
{
if (fprintf (output_file, "(%s *)",
word_type_representation) < 0)
output_file_error ();
}
else if (type_of_formal
== ICN_POINTER (&ICN_TD_byte))
{
if (fprintf (output_file, "(%s *)",
byte_type_representation) < 0)
output_file_error ();
}
if (fprintf
(output_file, "(&%s.%s%x.%s%x)",
temp_actuals_name,
temp_call_actuals_name_prefix,
(unsigned) number_of_current_call,
actual_name_prefix,
(unsigned) number_of_temp_variable_in_current_call)
< 0)
output_file_error ();
}
else
{
if (fprintf (output_file, "%s.", all_actuals_name)
< 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
if (fprintf (output_file, ".%s=",
open_array_address_name) < 0)
output_file_error ();
type_of_formal = FORMAL_PARAMETER_TYPE (formal);
if (type_of_formal == ICN_POINTER (&ICN_TD_address))
{
if (fprintf (output_file, "(%s **)",
word_type_representation) < 0)
output_file_error ();
}
else if (type_of_formal
== ICN_POINTER (&ICN_TD_word))
{
if (fprintf (output_file, "(%s *)",
word_type_representation) < 0)
output_file_error ();
}
else if (type_of_formal
== ICN_POINTER (&ICN_TD_byte))
{
if (fprintf (output_file, "(%s *)",
byte_type_representation) < 0)
output_file_error ();
}
GEN_ADDRESS (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
}
get_type_size_and_alignment
(FORMAL_PARAMETER_TYPE (formal), &formal_size, &align);
get_type_size_and_alignment
(sinf.type, &actual_size, &align);
if (fprintf (output_file, ",%s.", all_actuals_name) < 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_formal_parameter_type_name (formal);
if (fprintf (output_file, ".%s",
open_array_high_value_name) < 0)
output_file_error ();
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, "=0%lo",
actual_size / formal_size - 1) < 0)
output_file_error ();
#else
if (fprintf (output_file, "=0%o",
actual_size / formal_size - 1) < 0)
output_file_error ();
#endif
}
}
output_string_when_statements_generation (",");
}
/* End of processing parameters. */
if (current_generator_pass
== TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS
&& struct_for_temp_vars_is_needed)
{
if (fprintf (output_file, "}%s%x;\n",
temp_call_actuals_name_prefix,
(unsigned) number_of_current_call) < 0)
output_file_error ();
}
if (it_is_designator)
output_string_when_statements_generation ("(*");
generate_expression (procedure_designator);
if (it_is_designator)
output_string_when_statements_generation (")");
if (it_is_statements_generation_pass)
{
#ifdef COROUTINE_ENABLE
it_may_be_used_for_coroutine
= (!it_is_local_procedure && result_type == NULL
&& next_parameter_type (procedure_type) == NULL);
#else
it_may_be_used_for_coroutine = FALSE;
#endif
if (fputs ((it_may_be_used_for_coroutine ? "(0" : "("),
output_file) == EOF)
output_file_error ();
if (next_parameter_type (procedure_type) != NULL)
{
if (fprintf (output_file, "&%s", all_actuals_name) < 0)
output_file_error ();
if (fputc ('.', output_file) == EOF)
output_file_error ();
output_parameter_structure_name (procedure_type);
}
}
two_brackets_are_needed = TRUE;
if (result_type != NULL)
mode = MODE (result_type);
if (result_type != NULL
&& (mode == ICNM_ARRAY_TYPE || mode == ICNM_RECORD))
{
number_of_current_temporary_variable++;
generate_temp_variable_declaration (result_type);
if (it_is_statements_generation_pass)
{
if (it_may_be_used_for_coroutine
|| next_parameter_type (procedure_type) != NULL)
{
if (fputc (',', output_file) == EOF)
output_file_error ();
}
two_brackets_are_needed = FALSE;
if (mode != ICNM_ARRAY_TYPE)
{
if (fputc ('&', output_file) == EOF)
output_file_error ();
}
if (fprintf
(output_file, "%s.%s%x.%s%x),%s.%s%x.%s%x)",
temp_variables_name,
temp_expression_variables_names_prefix,
(unsigned) number_of_expression_with_temp_variables,
temp_variable_name_prefix,
(unsigned) number_of_current_temporary_variable,
temp_variables_name,
temp_expression_variables_names_prefix,
(unsigned) number_of_expression_with_temp_variables,
temp_variable_name_prefix,
(unsigned) number_of_current_temporary_variable) < 0)
output_file_error ();
}
}
if (two_brackets_are_needed)
output_string_when_statements_generation
((MODE (call) == ICNM_PROCEDURE_CALL ? "));" : "))"));
}
}
}
/* This page contains function for generation of statements. */
/* The following function generates list statement which starts with STATEMENT.
The statements itself are generated only on statement generation pass. On
the other pass the processing statements may generate various structures
(see commentaries for generator_pass). STATEMENT is to refer to
statement. */
static void
generate_statement (statement)
register ICN_pointer statement;
{
int align;
semantic_information sinf1, sinf2;
for (; statement != NULL; statement = NEXT_STATEMENT (statement))
switch (MODE (statement))
{
case ICNM_BLOCK_BEGIN:
generate_line_directive (statement);
current_scope = BLOCK_BEGIN_NODE (statement)->block_scope;
break;
case ICNM_RETURN_WITHOUT_RESULT:
generate_line_directive (statement);
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "goto %s%x;", label_name_prefix,
(unsigned) number_of_return_label_for_current_block)
< 0)
output_file_error ();
}
break;
case ICNM_RETURN_WITH_RESULT:
{
enum icode_node_mode result_type_mode;
Tcard return_expression_type_size;
register ICN_pointer upper_level_procedure, result_type;
generate_line_directive (statement);
subgraph_is_expression
(RETURN_WITH_RESULT_NODE (statement)->returned_expression, &sinf1);
upper_level_procedure = procedure_over_denotation (current_scope);
result_type
= PROCEDURE_TYPE_NODE (PROCEDURE_NODE (upper_level_procedure)
->procedure_type)
->procedure_result_type;
result_type_mode = MODE (result_type);
if (result_type_mode == ICNM_ARRAY_TYPE
|| result_type_mode == ICNM_RECORD)
{
if (it_is_statements_generation_pass)
{
if (fprintf (output_file,
(result_type_mode == ICNM_RECORD
? "%s=" : "m2_assarr(%s,"),
function_result_name) < 0)
output_file_error ();
}
GEN_THROUGH_STRUCT_POINTER
(RETURN_WITH_RESULT_NODE (statement)->returned_expression);
if (it_is_statements_generation_pass
&& result_type_mode == ICNM_ARRAY_TYPE)
{
get_type_size_and_alignment
(sinf1.type, &return_expression_type_size, &align);
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, ",0%lo)",
return_expression_type_size / BYTE_SIZE) < 0)
output_file_error ();
#else
if (fprintf (output_file, ",0%o)",
return_expression_type_size / BYTE_SIZE) < 0)
output_file_error ();
#endif
}
}
else
{
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "%s=", function_result_name) < 0)
output_file_error ();
}
if (result_type != ICN_POINTER (&ICN_TD_address)
&& sinf1.type == ICN_POINTER (&ICN_TD_address))
{
if (it_is_statements_generation_pass)
{
if (fputc ('(', output_file) == EOF)
output_file_error ();
output_type_definition_part_through_struct_pointer
(result_type, TRUE);
output_type_definition_part_through_struct_pointer
(result_type, FALSE);
if (fputc (')', output_file) == EOF)
output_file_error ();
}
generate_expression_with_test
(result_type,
RETURN_WITH_RESULT_NODE (statement)->returned_expression,
ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE);
}
else
{
if (it_is_statements_generation_pass
&& result_type == ICN_POINTER (&ICN_TD_address)
&& sinf1.type != ICN_POINTER (&ICN_TD_address))
{
if (fprintf (output_file, "(%s *)",
word_type_representation) < 0)
output_file_error ();
}
generate_expression_with_test
(result_type,
RETURN_WITH_RESULT_NODE (statement)->returned_expression,
THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE);
}
}
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, ";goto %s%x;", label_name_prefix,
(unsigned) number_of_return_label_for_current_block)
< 0)
output_file_error ();
}
finish_expression ();
}
break;
case ICNM_LOOP_STATEMENT:
{
int saved_number_of_exit_label_for_current_loop;
generate_line_directive (statement);
if (it_is_statements_generation_pass)
{
saved_number_of_exit_label_for_current_loop
= number_of_exit_label_for_current_loop;
number_of_exit_label_for_current_loop
= ++number_of_generated_labels;
if (fputs ("for(;;){", output_file) == EOF)
output_file_error ();
}
generate_statement
(LOOP_STATEMENT_NODE (statement)->loop_statements);
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "}%s%x:;", label_name_prefix,
(unsigned) number_of_exit_label_for_current_loop)
< 0)
output_file_error ();
}
number_of_exit_label_for_current_loop
= saved_number_of_exit_label_for_current_loop;
}
break;
case ICNM_EXIT_STATEMENT:
generate_line_directive (statement);
if (it_is_statements_generation_pass)
{
if (fprintf (output_file, "goto %s%x;", label_name_prefix,
(unsigned) number_of_exit_label_for_current_loop) < 0)
output_file_error ();
}
break;
case ICNM_WHILE_STATEMENT:
generate_line_directive (statement);
output_string_when_statements_generation ("while(");
generate_expression
(WHILE_STATEMENT_NODE (statement)->while_expression);
finish_expression ();
output_string_when_statements_generation ("){");
generate_statement
(WHILE_STATEMENT_NODE (statement)->while_statements);
output_string_when_statements_generation ("}");
break;
case ICNM_REPEAT_STATEMENT:
generate_line_directive (statement);
output_string_when_statements_generation ("do{");
generate_statement
(REPEAT_STATEMENT_NODE (statement)->repeat_statements);
output_string_when_statements_generation ("}while(!");
generate_expression
(REPEAT_STATEMENT_NODE (statement)->until_expression);
finish_expression ();
output_string_when_statements_generation (");");
break;
case ICNM_IF_STATEMENT:
{
register ICN_pointer continuation;
generate_line_directive (statement);
output_string_when_statements_generation ("if(");
generate_expression (IF_STATEMENT_NODE (statement)->if_expression);
finish_expression ();
output_string_when_statements_generation ("){");
generate_statement (IF_STATEMENT_NODE (statement)->if_statements);
for (continuation = IF_STATEMENT_NODE (statement)->if_continuation;
continuation != NULL
&& MODE (continuation) == ICNM_ELSIF_CLAUSE;
continuation
= ELSIF_CLAUSE_NODE (continuation)->elsif_continuation)
{
output_string_when_statements_generation ("}else if(");
generate_expression
(ELSIF_CLAUSE_NODE (continuation)->elsif_expression);
finish_expression ();
output_string_when_statements_generation ("){");
generate_statement
(ELSIF_CLAUSE_NODE (continuation)->elsif_statements);
}
if (continuation != NULL)
{
output_string_when_statements_generation ("}else{");
generate_statement (continuation);
}
output_string_when_statements_generation ("}");
}
break;
case ICNM_FOR_STATEMENT:
{
int increment_is_negative;
Tcard increment;
register ICN_pointer control_variable_type;
generate_line_directive (statement);
output_string_when_statements_generation ("for(");
generate_expression
(FOR_STATEMENT_NODE (statement)->for_control_variable);
output_string_when_statements_generation ("=");
control_variable_type
= VARIABLE_DECLARATION_NODE (FOR_STATEMENT_NODE (statement)
->for_control_variable)
->variable_type;
if (it_is_statements_generation_pass
&& control_variable_type == ICN_POINTER (&ICN_TD_address))
{
if (fprintf (output_file, "(%s *)", word_type_representation)
< 0)
output_file_error ();
}
generate_expression_with_test
(control_variable_type,
FOR_STATEMENT_NODE (statement)->for_starting_value,
ADDRESS_AS_CARDINAL_OR_CHARACTER_CODE);
output_string_when_statements_generation (";");
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER
(FOR_STATEMENT_NODE (statement)->for_control_variable);
increment_is_negative = FALSE;
if (FOR_STATEMENT_NODE (statement)->for_increment == NULL)
increment = 1;
else if (MODE (FOR_STATEMENT_NODE (statement)->for_increment)
== ICNM_CARDINAL)
increment
= CARDINAL_NODE (FOR_STATEMENT_NODE (statement)
->for_increment)
->cardinal_value;
else if (INTEGER_NODE (FOR_STATEMENT_NODE (statement)
->for_increment)
->integer_value >= 0)
increment
= INTEGER_NODE (FOR_STATEMENT_NODE (statement)
->for_increment)
->integer_value;
else
{
increment_is_negative = TRUE;
increment
= (-INTEGER_NODE (FOR_STATEMENT_NODE (statement)
->for_increment)
->integer_value);
}
output_string_when_statements_generation
((increment_is_negative ? ">=" : "<="));
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER
(FOR_STATEMENT_NODE (statement)->for_limit);
output_string_when_statements_generation (";");
generate_expression
(FOR_STATEMENT_NODE (statement)->for_control_variable);
if (MODE (control_variable_type) == ICNM_RANGE_TYPE)
generate_test_function (control_variable_type);
if (it_is_statements_generation_pass)
{
if (test_flag && MODE (control_variable_type) == ICNM_RANGE_TYPE)
{
if (fprintf
(output_file, "=%s%lx((%s)(",
range_test_function_name_prefix,
(unsigned long) control_variable_type,
discrete_type_representation (control_variable_type))
< 0)
output_file_error ();
generate_expression (FOR_STATEMENT_NODE (statement)
->for_control_variable);
if (fputc ((increment_is_negative ? '-' : '+'), output_file)
== EOF)
output_file_error ();
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, "%lu))){", increment) < 0)
output_file_error ();
#else
if (fprintf (output_file, "%u))){", increment) < 0)
output_file_error ();
#endif
}
else if (control_variable_type == ICN_POINTER (&ICN_TD_address))
{
if (fprintf (output_file, "=(%s *)(",
word_type_representation) < 0)
output_file_error ();
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER
(FOR_STATEMENT_NODE (statement)->for_control_variable);
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file,
(increment_is_negative
? "-%lu)){" : "+%lu)){\n"), increment) < 0)
output_file_error ();
#else
if (fprintf (output_file,
(increment_is_negative
? "-%u)){" : "+%u)){"), increment) < 0)
output_file_error ();
#endif
}
else if (increment == 1)
{
if (fputs ((increment_is_negative ? "--){" : "++){"),
output_file) == EOF)
output_file_error ();
}
else
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
{
if (fprintf (output_file,
(increment_is_negative
? "-=%lu){" : "+=%lu){"), increment) < 0)
output_file_error ();
}
#else
{
if (fprintf (output_file,
(increment_is_negative
? "-=%u){" : "+=%u){"), increment) < 0)
output_file_error ();
}
#endif
}
finish_expression ();
generate_statement (FOR_STATEMENT_NODE (statement)->for_statements);
output_string_when_statements_generation ("}");
}
break;
case ICNM_WITH_STATEMENT:
{
register ICN_pointer saved_current_scope, scope, saved_record_scope;
generate_line_directive (statement);
subgraph_is_expression
(WITH_STATEMENT_NODE (statement)->with_designator, &sinf1);
if (it_is_statements_generation_pass)
{
if (fputs ("{register ", output_file) == EOF)
output_file_error ();
generate_declaration_by_name
(sinf1.type, TRUE, with_variable_name_prefix,
(unsigned int) current_level_of_nesting_with_statements + 1);
if (fputs ("=(&", output_file) == EOF)
output_file_error ();
}
generate_expression
(WITH_STATEMENT_NODE (statement)->with_designator);
current_level_of_nesting_with_statements++;
finish_expression ();
output_string_when_statements_generation (");");
saved_record_scope = RECORD_NODE (sinf1.type)->scope;
RECORD_NODE (sinf1.type)->scope = current_scope;
current_scope = sinf1.type;
for (scope = current_scope;
scope != NULL && SCOPE (scope) != sinf1.type;
scope = SCOPE (scope)) ;
if (scope != NULL)
SCOPE (scope) = saved_record_scope;
generate_statement
(WITH_STATEMENT_NODE (statement)->with_statements);
current_level_of_nesting_with_statements--;
saved_current_scope = current_scope;
if (scope == NULL
|| RECORD_NODE (current_scope)->scope != saved_record_scope)
current_scope = RECORD_NODE (current_scope)->scope;
RECORD_NODE (saved_current_scope)->scope = saved_record_scope;
if (scope != NULL)
SCOPE (scope) = SCOPE (saved_current_scope);
output_string_when_statements_generation ("}");
}
break;
case ICNM_C_CODE_IN_STATEMENTS:
if (it_is_statements_generation_pass)
{
if (fputs (C_CODE_IN_STATEMENTS_NODE (statement)->C_text,
output_file) == EOF)
output_file_error ();
}
break;
case ICNM_CASE_STATEMENT:
{
register ICN_pointer case_variant, case_label;
register ICN_pointer left_case_range_bound, right_case_range_bound;
Tcard cardinal_case_value;
Tint integer_case_value;
generate_line_directive (statement);
subgraph_is_expression
(CASE_STATEMENT_NODE (statement)->case_expression, &sinf1);
output_string_when_statements_generation ("switch(");
GEN_ADDRESS_AS_CARDINAL_OR_CHARACTER
(CASE_STATEMENT_NODE (statement)->case_expression);
finish_expression ();
output_string_when_statements_generation ("){");
for (case_variant
= CASE_STATEMENT_NODE (statement)->case_statement_variant_list;
MODE (case_variant) == ICNM_CASE_STATEMENT_VARIANT;
case_variant
= CASE_STATEMENT_VARIANT_NODE (case_variant)->next_case_variant)
{
if (it_is_statements_generation_pass)
for (case_label = (CASE_STATEMENT_VARIANT_NODE (case_variant)
->case_label_list);
case_label != NULL;
case_label = CASE_LABEL_LIST (case_label))
{
if (MODE (case_label) == ICNM_CASE_STATEMENT_LABEL_ELEMENT)
left_case_range_bound
= right_case_range_bound
= (CASE_STATEMENT_LABEL_ELEMENT_NODE (case_label)
->case_label_value);
else
{
left_case_range_bound
= (CASE_STATEMENT_LABEL_RANGE_NODE (case_label)
->case_label_range_left_bound);
right_case_range_bound
= (CASE_STATEMENT_LABEL_RANGE_NODE (case_label)
->case_label_range_right_bound);
}
if (MODE (left_case_range_bound) == ICNM_INTEGER)
{
for (integer_case_value
= (INTEGER_NODE (left_case_range_bound)
->integer_value);
integer_case_value
<= (INTEGER_NODE (right_case_range_bound)
->integer_value);
integer_case_value++)
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, "case %ld:",
integer_case_value) < 0)
output_file_error ();
#else
if (fprintf (output_file, "case %d:",
integer_case_value) < 0)
output_file_error ();
#endif
}
else
{
for (cardinal_case_value
= cardinal_value (left_case_range_bound);
cardinal_case_value
<= cardinal_value (right_case_range_bound);
cardinal_case_value++)
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, "case 0%lo:",
cardinal_case_value) < 0)
output_file_error ();
#else
if (fprintf (output_file, "case 0%o:",
cardinal_case_value) < 0)
output_file_error ();
#endif
}
}
if (CASE_STATEMENT_VARIANT_NODE (case_variant)->case_label_list
== NULL)
output_string_when_statements_generation ("default:");
generate_statement (CASE_STATEMENT_VARIANT_NODE (case_variant)
->case_variant_elements);
output_string_when_statements_generation ("break;");
}
output_string_when_statements_generation ("}");
}
break;
case ICNM_ASSIGNMENT:
{
Tcard assignment_variable_size, assignment_expression_size;
generate_line_directive (statement);
subgraph_is_expression
(ASSIGNMENT_NODE (statement)->assignment_variable, &sinf1);
subgraph_is_expression
(ASSIGNMENT_NODE (statement)->assignment_expression, &sinf2);
if (MODE (sinf1.type) == ICNM_ARRAY_TYPE)
{
if (it_is_statements_generation_pass)
{
get_type_size_and_alignment
(sinf1.type, &assignment_variable_size, &align);
get_type_size_and_alignment
(sinf2.type, &assignment_expression_size, &align);
if (fputs
((assignment_variable_size > assignment_expression_size
&& third_edition_flag
? "m2_assstr(" : "m2_assarr("), output_file) == EOF)
output_file_error ();
}
GEN_THROUGH_STRUCT_POINTER
(ASSIGNMENT_NODE (statement)->assignment_variable);
output_string_when_statements_generation (",");
GEN_THROUGH_STRUCT_POINTER
(ASSIGNMENT_NODE (statement)->assignment_expression);
if (it_is_statements_generation_pass)
{
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, ",0%lo",
assignment_variable_size / BYTE_SIZE) < 0)
output_file_error ();
#else
if (fprintf (output_file, ",0%o",
assignment_variable_size / BYTE_SIZE) < 0)
output_file_error ();
#endif
if (assignment_variable_size > assignment_expression_size
&& third_edition_flag)
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
{
if (fprintf (output_file, ",0%lo",
assignment_expression_size / BYTE_SIZE) < 0)
output_file_error ();
}
#else
{
if (fprintf (output_file, ",0%o",
assignment_expression_size / BYTE_SIZE) < 0)
output_file_error ();
}
#endif
if (fputs (");", output_file) == EOF)
output_file_error ();
}
}
else
{
GEN_THROUGH_STRUCT_POINTER
(ASSIGNMENT_NODE (statement)->assignment_variable);
output_string_when_statements_generation ("=");
if (sinf1.type != ICN_POINTER (&ICN_TD_address)
&& sinf2.type == ICN_POINTER (&ICN_TD_address))
{
if (it_is_statements_generation_pass)
{
if (fputc ('(', output_file) == EOF)
output_file_error ();
output_type_definition_part_through_struct_pointer
(sinf1.type, TRUE);
output_type_definition_part_through_struct_pointer
(sinf1.type, FALSE);
if (fputc (')', output_file) == EOF)
output_file_error ();
}
generate_expression_with_test
(sinf1.type,
ASSIGNMENT_NODE (statement)->assignment_expression,
ADDRESS_AS_CARDINAL_OR_THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE);
}
else
{
if (it_is_statements_generation_pass
&& sinf1.type == ICN_POINTER (&ICN_TD_address)
&& sinf2.type != ICN_POINTER (&ICN_TD_address))
{
if (fprintf (output_file, "(%s *)",
word_type_representation) < 0)
output_file_error ();
}
generate_expression_with_test
(sinf1.type,
ASSIGNMENT_NODE (statement)->assignment_expression,
THROUGH_STRUCT_POINTER_OR_CHARACTER_CODE);
}
output_string_when_statements_generation (";");
}
finish_expression ();
}
break;
case ICNM_PROCEDURE_CALL:
generate_line_directive (statement);
generate_call (statement);
finish_expression ();
break;
default:
abort ();
}
}
/* This page contains functions for generation (all passes) of implementations
of modules or procedures. */
/* The function executes PASS (see commentaries for generator_pass) on
the block of MODULE_OR_PROCEDURE. MODULE_OR_PROCEDURE is to refer to
any module or procedure. */
static void
execute_pass (module_or_procedure, pass)
ICN_pointer module_or_procedure;
register enum generator_pass pass;
{
register char *union_name;
current_generator_pass = pass;
it_is_statements_generation_pass = pass == STATEMENTS_GENERATION_PASS;
number_of_expression_with_temp_variables = 0;
number_of_current_call = 0;
number_of_current_temporary_variable = 0;
number_of_temp_variable_in_current_call = 0;
current_expression_needs_temp_variables = FALSE;
if (pass == ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS
|| pass == TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS
|| pass == TEMPORARY_VARIABLES_DECLARATION_GENERATION_PASS)
{
if (fputs ("union{", output_file) == EOF)
output_file_error ();
}
if (pass == ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS)
current_block_number_among_blocks_with_actuals++;
generate_statement (BLOCK_BEGIN (module_or_procedure));
if (pass == ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS)
union_name = all_actuals_name;
else if (pass == TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS)
union_name = temp_actuals_name;
else if (pass == TEMPORARY_VARIABLES_DECLARATION_GENERATION_PASS)
union_name = temp_variables_name;
else
union_name = NULL;
if (union_name != NULL)
{
if (fprintf (output_file, "}%s;\n", union_name) < 0)
output_file_error ();
}
}
/* The recursive function executes block generation for all local modules which
are immediately contained in MODULE_OR_PROCEDURE. MODULE_OR_PROCEDURE is to
refer to any module or procedure. */
static void
generate_local_module_blocks (module_or_procedure)
register ICN_pointer module_or_procedure;
{
for (module_or_procedure = LOCAL_MODULE_LIST (module_or_procedure);
module_or_procedure != NULL;
module_or_procedure = MODULE_NODE (module_or_procedure)->module_brother)
generate_block (module_or_procedure);
}
/* The function generates calls of bodies of compilation units which are
immediately imported by COMPILATION_UNIT. COMPILATION_UNIT is to refer to
main or implementation module. The imported module body may be called
once more (the body will be executed only once in any case). */
static void
generate_imported_compilation_unit_calls (compilation_unit)
ICN_pointer compilation_unit;
{
register ICN_pointer imported_module, declaration_element;
register ICN_pointer pred_imported_module, imported_module_identifier;
pred_imported_module = NULL;
for (declaration_element = NEXT_DECLARATION_ELEMENT (compilation_unit);
declaration_element != NULL;
declaration_element = NEXT_DECLARATION_ELEMENT (declaration_element))
if (MODE (declaration_element) == ICNM_IMPORT)
{
if (MODULE_EXPORTER (declaration_element) == NULL)
imported_module_identifier = IDENTIFIER (declaration_element);
else
imported_module_identifier = MODULE_EXPORTER (declaration_element);
imported_module
= find_denotation (ICN_POINTER (NULL), imported_module_identifier);
if (imported_module != NULL
&& imported_module != ICN_POINTER (&ICN_D_system)
&& imported_module != ICN_POINTER (&ICN_D_cproc)
&& imported_module != pred_imported_module)
{
output_object_full_name (imported_module, FALSE);
if (fputs ("();", output_file) == EOF)
output_file_error ();
pred_imported_module = imported_module;
}
}
}
/* The function executes necessary passes of generation of declarations (except
for STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS). Necessity of such
passes is defined on STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS. The
passes are executed on MODULE_OR_PROCEDURE. MODULE_OR_PROCEDURE is to refer
to any module or procedure. */
static void
execute_declaration_generation_passes (module_or_procedure)
register ICN_pointer module_or_procedure;
{
if (actuals_structures_declaration_generation_is_needed)
execute_pass (module_or_procedure,
ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS);
if (temp_array_actuals_structures_declaration_generation_is_needed)
execute_pass (module_or_procedure,
TEMP_ARRAY_ACTUALS_STRUCTURES_DECLARATION_GENERATION_PASS);
if (temporary_variables_declaration_generation_is_needed)
execute_pass (module_or_procedure,
TEMPORARY_VARIABLES_DECLARATION_GENERATION_PASS);
}
/* The recursive function generates body (without own declarations, prologue
but with epilogue and blocks of local modules) of module MODULE.
IT_IS_MAIN_MODULE is TRUE if the module is main module.
NUMBER_OF_RETURN_LABEL_FOR_CURRENT_BLOCK defines name of return label (see
commentaries for label_prefix). MODULE is to refer to any module. */
static void
generate_module_body (module, number_of_return_label_for_current_block,
it_is_main_module)
register ICN_pointer module;
int number_of_return_label_for_current_block, it_is_main_module;
{
generate_local_module_blocks (module);
execute_pass (module, STATEMENTS_GENERATION_PASS);
if (fprintf (output_file, "%s%x:", label_name_prefix,
(unsigned) number_of_return_label_for_current_block) < 0)
output_file_error ();
if (fputs ((it_is_main_module ? "exit(0);}\n" : ";}\n"), output_file) == EOF)
output_file_error ();
}
/* The recursive function generates full code for block of MODULE_OR_PROCEDURE.
Non-local modules and procedures are implemented by C functions. Local
modules are implemented by nested C blocks. The following function executes
all necessary passes (except for
STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS -- see commentaries for
generator_pass). MODULE_OR_PROCEDURE is to refer to any module or
procedure. */
static void
generate_block (module_or_procedure)
register ICN_pointer module_or_procedure;
{
if (!block_code_may_be_used (module_or_procedure))
return;
number_of_return_label_for_current_block = ++number_of_generated_labels;
if (MODE (module_or_procedure) == ICNM_PROCEDURE)
{
register ICN_pointer result_type_implementation, procedure_type;
register ICN_pointer result_type, formal_parameter;
int indirection_is_needed, it_may_be_used_as_coroutine;
int nested_procedures_exist, it_has_parameteres, align;
int semicolon_is_not_needed, it_has_variables;
Tcard parameter_size;
result_type_implementation
= get_C_function_type (module_or_procedure, &indirection_is_needed);
procedure_type = PROCEDURE_NODE (module_or_procedure)->procedure_type;
result_type
= PROCEDURE_TYPE_NODE (procedure_type)->procedure_result_type;
it_has_parameteres = next_parameter_type (procedure_type) != NULL;
if (!procedure_is_exported_from_compilation_unit (module_or_procedure))
{
if (fputs ("static ", output_file) == EOF)
output_file_error ();
}
output_type_definition_part (result_type_implementation,
(indirection_is_needed ? 1 : 0), TRUE);
if (fputc (' ', output_file) == EOF)
output_file_error ();
output_object_full_name (module_or_procedure, FALSE);
if (fputc ('(', output_file) == EOF)
output_file_error ();
#ifdef COROUTINE_ENABLE
it_may_be_used_as_coroutine
= (result_type == NULL && !it_has_parameteres
&& procedure_over_denotation (SCOPE (module_or_procedure)) == NULL);
#else
it_may_be_used_as_coroutine = FALSE;
#endif
if (it_may_be_used_as_coroutine)
{
if (fputs (coroutine_parameter_name, output_file) == EOF)
output_file_error ();
}
if (it_has_parameteres)
{
if (fputs (par_prefix, output_file) == EOF)
output_file_error ();
}
if (result_type != NULL && result_type_implementation == NULL)
{
if (fprintf (output_file,
(it_has_parameteres || it_may_be_used_as_coroutine
? ",%s" : "%s"),
function_result_name) < 0)
output_file_error ();
}
if (fputc (')', output_file) == EOF)
output_file_error ();
output_type_definition_part (result_type_implementation,
(indirection_is_needed ? 1 : 0), FALSE);
if (it_may_be_used_as_coroutine)
{
if (fprintf (output_file, "int %s;", coroutine_parameter_name) < 0)
output_file_error ();
}
if (it_has_parameteres)
{
if (fputs ("register struct ", output_file) == EOF)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fprintf (output_file, " *%s;", par_prefix) < 0)
output_file_error ();
}
if (result_type != NULL && result_type_implementation == NULL)
{
generate_declaration_by_name
(result_type, MODE (result_type) == ICNM_RECORD,
function_result_name, 0);
if (fputc (';', output_file) == EOF)
output_file_error ();
}
if (fputs ("\n{", output_file) == EOF)
output_file_error ();
it_has_variables
= generate_structures_for_procedure_variables (module_or_procedure,
TRUE);
generate_structures_for_procedure_parameters (module_or_procedure, TRUE);
if (result_type != NULL && result_type_implementation != NULL)
{
if (fputs ("register ", output_file) == EOF)
output_file_error ();
generate_declaration_by_name (result_type_implementation,
indirection_is_needed,
function_result_name, 0);
if (fputs (";\n", output_file) == EOF)
output_file_error ();
}
execute_declaration_generation_passes (module_or_procedure);
nested_procedures_exist = it_has_nested_procedures (module_or_procedure);
if (it_may_be_used_as_coroutine)
{
if (fprintf (output_file, "if(%s)m2_newpr1(",
coroutine_parameter_name) < 0)
output_file_error ();
if (it_has_variables)
{
if (fprintf (output_file, "&%s,", var_prefix) < 0)
output_file_error ();
}
else
{
if (fputs ("0,", output_file) == EOF)
output_file_error ();
}
if (nested_procedures_exist && it_has_variables)
{
if (fputc ('&', output_file) == EOF)
output_file_error ();
output_object_full_name (module_or_procedure, TRUE);
}
else
{
if (fputc ('0', output_file) == EOF)
output_file_error ();
}
if (fputs (");else{", output_file) == EOF)
output_file_error ();
}
if (nested_procedures_exist)
{
if (it_has_variables)
{
if (fprintf (output_file, "%s=",
name_of_saved_pointer_to_procedure_variables) < 0)
output_file_error ();
output_object_full_name (module_or_procedure, TRUE);
if (fputs (";", output_file) == EOF)
output_file_error ();
output_object_full_name (module_or_procedure, TRUE);
if (fprintf (output_file, "=(&%s);", var_prefix) < 0)
output_file_error ();
}
if (it_has_parameteres)
{
if (fprintf (output_file, "%s=",
name_of_saved_pointer_to_procedure_parameters) < 0)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fputs (";", output_file) == EOF)
output_file_error ();
output_parameter_structure_name (procedure_type);
if (fprintf (output_file, "=%s;", par_prefix) < 0)
output_file_error ();
}
}
if (it_may_be_used_as_coroutine)
{
if (fputc ('}', output_file) == EOF)
output_file_error ();
}
/* Generate allocating memory for array parameters. */
for (formal_parameter = next_parameter_type (procedure_type);
formal_parameter != NULL;
formal_parameter = next_parameter_type (formal_parameter))
if (ARRAY_FORMAL_PARAMETER (formal_parameter)
&& !VAR_FORMAL_PARAMETER (formal_parameter))
{
get_type_size_and_alignment
(FORMAL_PARAMETER_TYPE (formal_parameter), ¶meter_size,
&align);
if (fprintf (output_file, "m2_arrpar(&%s->", par_prefix) < 0)
output_file_error ();
output_formal_parameter_type_name (formal_parameter);
if (fprintf (output_file, ".%s,%s->", open_array_address_name,
par_prefix) < 0)
output_file_error ();
output_formal_parameter_type_name (formal_parameter);
if (fprintf (output_file, ".%s", open_array_high_value_name) < 0)
output_file_error ();
#ifdef MODULA_LONG_IS_IMPLEMENTED_BY_C_LONG
if (fprintf (output_file, ",0%lo);", parameter_size / BYTE_SIZE)
< 0)
output_file_error ();
#else
if (fprintf (output_file, ",0%o);", parameter_size / BYTE_SIZE)
< 0)
output_file_error ();
#endif
}
generate_local_module_blocks (module_or_procedure);
execute_pass (module_or_procedure, STATEMENTS_GENERATION_PASS);
if (fprintf (output_file, "%s%x:", label_name_prefix,
(unsigned) number_of_return_label_for_current_block) < 0)
output_file_error ();
semicolon_is_not_needed = FALSE;
if (nested_procedures_exist)
{
if (it_has_variables)
{
output_object_full_name (module_or_procedure, TRUE);
if (fprintf (output_file, "=%s;\n",
name_of_saved_pointer_to_procedure_variables) < 0)
output_file_error ();
semicolon_is_not_needed = TRUE;
}
if (it_has_parameteres)
{
output_parameter_structure_name (procedure_type);
if (fputs ("=", output_file) == EOF)
output_file_error ();
if (fprintf (output_file, "%s;\n",
name_of_saved_pointer_to_procedure_parameters) < 0)
output_file_error ();
semicolon_is_not_needed = TRUE;
}
}
/* Generate freeing memory for array parameters. */
for (formal_parameter = next_parameter_type (procedure_type);
formal_parameter != NULL;
formal_parameter = next_parameter_type (formal_parameter))
if (ARRAY_FORMAL_PARAMETER (formal_parameter)
&& !VAR_FORMAL_PARAMETER (formal_parameter))
{
if (fprintf (output_file, "free(%s->", par_prefix) < 0)
output_file_error ();
output_formal_parameter_type_name (formal_parameter);
if (fprintf (output_file, ".%s);", open_array_address_name) < 0)
output_file_error ();
semicolon_is_not_needed = TRUE;
}
if (result_type != NULL && result_type_implementation != NULL)
{
if (fprintf (output_file, "return %s;", function_result_name) < 0)
output_file_error ();
semicolon_is_not_needed = TRUE;
}
if (fputs ((semicolon_is_not_needed ? "}\n" : ";}\n"), output_file)
== EOF)
output_file_error ();
}
else if ((enum module_mode) MODULE_NODE (module_or_procedure)->module_mode
== MM_LOCAL_MODULE)
{
if (fputc ('{', output_file) == EOF)
output_file_error ();
execute_declaration_generation_passes (module_or_procedure);
generate_module_body (module_or_procedure,
number_of_return_label_for_current_block, FALSE);
}
else if ((enum module_mode) MODULE_NODE (module_or_procedure)->module_mode
== MM_DEFINITION_WITH_IMPLEMENTATION_MODULE)
{
if (fprintf (output_file, "static int %s=0;\n%s ",
implementation_module_initiation_flag_name,
void_type_representation) < 0)
output_file_error ();
output_object_full_name (module_or_procedure, FALSE);
if (fputs ("(){\n", output_file) == EOF)
output_file_error ();
execute_declaration_generation_passes (module_or_procedure);
if (fprintf (output_file, "if(%s)return;%s=1;",
implementation_module_initiation_flag_name,
implementation_module_initiation_flag_name) < 0)
output_file_error ();
generate_imported_compilation_unit_calls (module_or_procedure);
generate_module_body (module_or_procedure,
number_of_return_label_for_current_block, FALSE);
}
else if ((enum module_mode) MODULE_NODE (module_or_procedure)->module_mode
== MM_MAIN_MODULE)
{
if (fputs ("int m2argc;char **m2argv,**m2envp;\n", output_file) == EOF)
output_file_error ();
/* The following generates warning for some compilers. Therefore
the exit declaration is not generated. */
/* if (fputs ("extern exit();\n", output_file) == EOF)
output_file_error (); */
if (fputs ("main(argc,argv,envp)int argc;char **argv,**envp;{\n",
output_file) == EOF)
output_file_error ();
execute_declaration_generation_passes (module_or_procedure);
if (fputs ("m2argc=argc;m2argv=argv;m2envp=envp;\n", output_file) == EOF)
output_file_error ();
generate_imported_compilation_unit_calls (module_or_procedure);
generate_module_body (module_or_procedure,
number_of_return_label_for_current_block, TRUE);
}
else
abort ();
}
/* The following recursive function executes
STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS (see commentaries for
generator_pass) for MODULE_OR_PROCEDURE and its local modules.
MODULE_OR_PROCEDURE is to refer to any module or procedure. */
static void
execute_string_constants_declarations_generation_pass (module_or_procedure)
ICN_pointer module_or_procedure;
{
register ICN_pointer local_module;
if (!block_code_may_be_used (module_or_procedure))
return;
for (local_module = LOCAL_MODULE_LIST (module_or_procedure);
local_module != NULL;
local_module = MODULE_NODE (local_module)->module_brother)
execute_string_constants_declarations_generation_pass (local_module);
actuals_structures_declaration_generation_is_needed = FALSE;
temp_array_actuals_structures_declaration_generation_is_needed = FALSE;
temporary_variables_declaration_generation_is_needed = FALSE;
execute_pass (module_or_procedure,
STRING_CONSTANTS_DECLARATIONS_GENERATION_PASS);
}
/* This page contains functions which are needed for generation with
flag `-all'. */
/* The function places unique name of OBJECT in *UNIQUE_NAME. The unique
name is object identifier, scope, upper scope, ... and compilation unit
identifier separated by dot. OBJECT is to refer to denotation (or NULL).
*/
static void
make_unique_object_name (unique_name, object)
register VLS *unique_name;
ICN_pointer object;
{
VLS_ADD_CHAR (*unique_name, '\0');
for (; object != NULL; object = SCOPE (object))
{
object = declaration_level_scope (object);
VLS_ADD_STRING
(*unique_name,
IDENTIFIER_NODE (IDENTIFIER (object))->identifier_string);
if (SCOPE (object) != NULL)
VLS_ADD_STRING (*unique_name, ".");
}
}
/* The following structure contains a reference to block. */
struct block_link
{
struct block_link *next_block_link;
struct block_structure *block;
};
/* The following structure contains information about references to blocks
from given block. The structure does not exist for local modules (although
the local modules are processed by set_current_block_structure and
insert_block_link calls). Because necessity of local module code is
determined only by the necessity of code of procedure or compilation unit
which contains the local module. */
struct block_structure
{
/* TRUE if block code must be present in executable program (i.e. the
block is achievable through references to blocks from the blocks
of compilation units. */
char block_may_be_used;
/* Unique name of block (see commentaries for make_unique_object_name. */
char *block_name;
/* Pointer to first structure containing a reference to block from
given block. */
struct block_link *first_block_link;
};
/* The hash table of block structures. The key is block_name. */
static hash_table block_structure_table;
/* The hash function for block_structure_table (see commentaries for
hash_table). */
static unsigned
hash_function (el_ptr)
hash_table_entry el_ptr;
{
register char *block_name;
register unsigned hash_value, block_name_length, i;
block_name = ((struct block_structure *) el_ptr)->block_name;
block_name_length = strlen (block_name);
hash_value = 0;
for (i = 0; i < block_name_length; i++, block_name++)
hash_value += (*block_name);
return hash_value;
}
/* The compare function for block_structure_table (see commentaries for
hash_table). */
static int
compare_function (el1_ptr, el2_ptr)
hash_table_entry *el1_ptr, *el2_ptr;
{
return !strcmp (((struct block_structure *) el1_ptr)->block_name,
((struct block_structure *) el2_ptr)->block_name);
}
/* Create and initiate block_structure_table (see commentaries for
create_hash_table). */
void
create_block_structure_table ()
{
block_structure_table = create_hash_table (1000, hash_function,
compare_function);
}
/* The function returns hash table entry which contains pointer to
block_structure with BLOCK_NAME. If the pointer is NULL then such
block_structure does not exist. In this case the entry is reserved
for insertion pointer to such block_structure if RESERVATION_IS_NEEDED is
TRUE (see also commentaries for find_hash_table_entry). */
static struct block_structure **
find_block_structure_table_entry (block_name, reservation_is_needed)
char *block_name;
int reservation_is_needed;
{
struct block_structure block_structure;
block_structure.block_name = block_name;
return ((struct block_structure **)
find_hash_table_entry (block_structure_table,
(hash_table_entry) & block_structure,
reservation_is_needed));
}
/* The variable contains pointer to block_structure corresponding to the
block being processed (if the block being processed is local module than
more exactly to procedure or compilation unit containing the local module).
Remember that block_structure does not exist for local modules. Because
necessity of local module code is determined only by the necessity of code
of procedure or compilation unit which contains the local module. */
static struct block_structure *current_block_structure;
/* The functions set up current_block_structure (see commentaries upper) for
BLOCK. If the corresponding current_block_structure does not
exist it is created. BLOCK is to refer to any module or
procedure. */
static void
set_current_block_structure (block)
ICN_pointer block;
{
register struct block_structure **entry_ptr;
register char *block_structure_memory;
VLS unique_name;
block = declaration_level_scope (block);
VLS_CREATE (unique_name, 100);
make_unique_object_name (&unique_name, block);
entry_ptr = find_block_structure_table_entry (VLS_BEGIN (unique_name), TRUE);
if (*entry_ptr == NULL)
{
M2C_ALLOC (block_structure_memory, sizeof (**entry_ptr));
*entry_ptr = (struct block_structure *) block_structure_memory;
(*entry_ptr)->first_block_link = NULL;
M2C_ALLOC ((*entry_ptr)->block_name, VLS_LENGTH (unique_name));
strcpy ((*entry_ptr)->block_name, VLS_BEGIN (unique_name));
(*entry_ptr)->block_may_be_used = SCOPE (block) == NULL;
}
VLS_DELETE (unique_name);
current_block_structure = (*entry_ptr);
}
/* The function inserts block_link from the current block (i.e block
corresponding to current_block_structure) to block BLOCK (if block BLOCK is
local module than more exactly to procedure or compilation unit containing
the local module). BLOCK is to refer to any module or procedure. */
static void
insert_block_link (block)
ICN_pointer block;
{
register struct block_structure **entry_ptr;
VLS unique_name;
struct block_link *block_link_ptr;
char *block_structure_memory;
block = declaration_level_scope (block);
VLS_CREATE (unique_name, 100);
make_unique_object_name (&unique_name, block);
entry_ptr = find_block_structure_table_entry (VLS_BEGIN (unique_name), TRUE);
if (*entry_ptr == NULL)
/* It is new block_structure. */
{
M2C_ALLOC (block_structure_memory, sizeof (**entry_ptr));
*entry_ptr = (struct block_structure *) block_structure_memory;
(*entry_ptr)->first_block_link = NULL;
M2C_ALLOC ((*entry_ptr)->block_name, VLS_LENGTH (unique_name));
strcpy ((*entry_ptr)->block_name, VLS_BEGIN (unique_name));
/* Compilation unit block is always needed. */
(*entry_ptr)->block_may_be_used = SCOPE (block) == NULL;
}
VLS_DELETE (unique_name);
for (block_link_ptr = current_block_structure->first_block_link;
block_link_ptr != NULL && block_link_ptr->block != (*entry_ptr);)
block_link_ptr = block_link_ptr->next_block_link;
if (block_link_ptr == NULL)
/* Link to block BLOCK from current block does not exist yet. Add the link
at the end of the block link list. */
{
M2C_ALLOC (block_structure_memory, sizeof (struct block_link));
block_link_ptr = (struct block_link *) block_structure_memory;
block_link_ptr->block = (*entry_ptr);
block_link_ptr->next_block_link
= current_block_structure->first_block_link;
current_block_structure->first_block_link = block_link_ptr;
}
}
/* The recursive function is used for determination of necessary blocks by
passing through all block links from block ORIGINAL_BLOCK the necessity of
which has been determined. */
static void
process_block_links (original_block)
register struct block_structure *original_block;
{
register struct block_link *block_link;
if (original_block->block_may_be_used)
return;
original_block->block_may_be_used = TRUE;
for (block_link = original_block->first_block_link;
block_link != NULL;
block_link = block_link->next_block_link)
process_block_links (block_link->block);
}
/* The function determines which blocks in block_structure_table are to be
present in executable program (i.e. the block is achievable through
references to blocks from the blocks of compilation units). Originally
compilation unit block is always needed. */
void
define_necessary_blocks ()
{
register unsigned int i;
register struct block_structure *block_structure_ptr;
for (i = 0; i < block_structure_table->size; i++)
{
block_structure_ptr
= (struct block_structure *) block_structure_table->entries[i];
if (block_structure_ptr != NULL
&& block_structure_ptr->block_may_be_used)
{
block_structure_ptr->block_may_be_used = FALSE;
process_block_links (block_structure_ptr);
}
}
}
/* The recursive function processes expression EXPR to find reference to a
block (procedure) and to insert the corresponding block link for the
current block (i.e block corresponding to current_block_structure). */
static void
process_expression (expr)
register ICN_pointer expr;
{
register ICN_pointer actual;
if (expr == NULL)
return;
switch (MODE (expr))
{
case ICNM_INTEGER:
case ICNM_CARDINAL:
case ICNM_REAL:
case ICNM_SET:
case ICNM_STRING:
case ICNM_VARIABLE_DECLARATION:
case ICNM_FORMAL_PARAMETER:
case ICNM_FIELD:
break;
case ICNM_PROCEDURE:
insert_block_link (expr);
break;
case ICNM_LESS_THAN:
case ICNM_GREATER_THAN:
case ICNM_OR:
case ICNM_AND:
case ICNM_REAL_DIVISION:
case ICNM_ADDITION:
case ICNM_SUBTRACTION:
case ICNM_MULT:
case ICNM_DIV:
case ICNM_MOD:
case ICNM_EQUAL:
case ICNM_UNEQUAL:
case ICNM_LESS_OR_EQUAL:
case ICNM_GREATER_OR_EQUAL:
case ICNM_IN:
case ICNM_QUALIFICATION:
case ICNM_INDEXING:
case ICNM_RANGE_IN_SET_VALUE:
process_expression (LEFT_OPERAND (expr));
process_expression (RIGHT_OPERAND (expr));
break;
case ICNM_SIGN_CONVERSION:
case ICNM_NOT:
case ICNM_DEREFERENCING:
case ICNM_ELEMENT_IN_SET_VALUE:
process_expression (OPERAND (expr));
break;
case ICNM_FUNCTION_CALL:
process_expression (FUNCTION_CALL_NODE (expr)->function_designator);
for (actual = FUNCTION_CALL_NODE (expr)->function_actual_parameter_list;
actual != NULL;
actual = ACTUAL_PARAMETER_NODE (actual)->next_actual_parameter)
process_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
break;
default:
break; /* may be C procedure, types & etc.*/
}
}
/* The recursive function processes statement list starting with STATEMENT to
find reference to a block (procedure) and to insert the corresponding block
link for the current block (i.e block corresponding to
current_block_structure). STATEMENT is to refer to statement (or NULL). */
static void
process_statement_list (statement)
register ICN_pointer statement;
{
ICN_pointer if_cont, variant, actual, case_label;
for (; statement != NULL; statement = NEXT_STATEMENT (statement))
switch (MODE (statement))
{
case ICNM_BLOCK_BEGIN:
case ICNM_RETURN_WITHOUT_RESULT:
break;
case ICNM_RETURN_WITH_RESULT:
process_expression (RETURN_WITH_RESULT_NODE (statement)
->returned_expression);
break;
case ICNM_LOOP_STATEMENT:
process_statement_list (LOOP_STATEMENT_NODE (statement)
->loop_statements);
break;
case ICNM_EXIT_STATEMENT:
break;
case ICNM_WHILE_STATEMENT:
process_expression (WHILE_STATEMENT_NODE (statement)
->while_expression);
process_statement_list (WHILE_STATEMENT_NODE (statement)
->while_statements);
break;
case ICNM_REPEAT_STATEMENT:
process_statement_list (REPEAT_STATEMENT_NODE (statement)
->repeat_statements);
process_expression (REPEAT_STATEMENT_NODE (statement)
->until_expression);
break;
case ICNM_IF_STATEMENT:
process_expression (IF_STATEMENT_NODE (statement)->if_expression);
process_statement_list (IF_STATEMENT_NODE (statement)->if_statements);
for (if_cont = IF_STATEMENT_NODE (statement)->if_continuation;
if_cont != NULL && MODE (if_cont) == ICNM_ELSIF_CLAUSE;
if_cont = ELSIF_CLAUSE_NODE (if_cont)->elsif_continuation)
{
process_expression (ELSIF_CLAUSE_NODE (if_cont)->elsif_expression);
process_statement_list (ELSIF_CLAUSE_NODE (if_cont)
->elsif_statements);
}
if (if_cont != NULL)
process_statement_list (if_cont);
break;
case ICNM_FOR_STATEMENT:
process_expression (FOR_STATEMENT_NODE (statement)
->for_control_variable);
process_expression (FOR_STATEMENT_NODE (statement)
->for_starting_value);
process_expression (FOR_STATEMENT_NODE (statement)->for_increment);
process_expression (FOR_STATEMENT_NODE (statement)->for_limit);
process_statement_list (FOR_STATEMENT_NODE (statement)
->for_statements);
break;
case ICNM_WITH_STATEMENT:
process_expression (WITH_STATEMENT_NODE (statement)->with_designator);
process_statement_list (WITH_STATEMENT_NODE (statement)
->with_statements);
break;
case ICNM_C_CODE_IN_STATEMENTS:
break;
case ICNM_CASE_STATEMENT:
process_expression (CASE_STATEMENT_NODE (statement)->case_expression);
for (variant
= CASE_STATEMENT_NODE (statement)->case_statement_variant_list;
MODE (variant) == ICNM_CASE_STATEMENT_VARIANT;
variant
= CASE_STATEMENT_VARIANT_NODE (variant)->next_case_variant)
{
for (case_label
= CASE_STATEMENT_VARIANT_NODE (variant)->case_label_list;
case_label != NULL;
case_label = CASE_LABEL_LIST (case_label))
if (MODE (case_label) == ICNM_CASE_STATEMENT_LABEL_ELEMENT)
process_expression
(CASE_STATEMENT_LABEL_ELEMENT_NODE (case_label)
->case_label_value);
else
{
process_expression
(CASE_STATEMENT_LABEL_RANGE_NODE (case_label)
->case_label_range_left_bound);
process_expression
(CASE_STATEMENT_LABEL_RANGE_NODE (case_label)
->case_label_range_right_bound);
}
process_statement_list (CASE_STATEMENT_VARIANT_NODE (variant)
->case_variant_elements);
}
break;
case ICNM_ASSIGNMENT:
process_expression (ASSIGNMENT_NODE (statement)->assignment_variable);
process_expression (ASSIGNMENT_NODE (statement)
->assignment_expression);
break;
case ICNM_PROCEDURE_CALL:
process_expression (PROCEDURE_CALL_NODE (statement)
->procedure_designator);
for (actual = (PROCEDURE_CALL_NODE (statement)
->procedure_actual_parameter_list);
actual != NULL;
actual = ACTUAL_PARAMETER_NODE (actual)->next_actual_parameter)
process_expression (ACTUAL_PARAMETER_NODE (actual)
->actual_parameter_expression);
break;
default:
abort ();
}
}
/* The function returns TRUE if BLOCK is needed in executable program or
flag `-all' is not given. BLOCK is to refer to compilation unit or
procedure. If procedure code is necessary for the program then all local
modules contained in the procedure are also necessary for the program. */
static int
block_code_may_be_used (block)
ICN_pointer block;
{
VLS unique_name;
register struct block_structure **entry;
if (!pass_of_generation_when_all_flag)
return TRUE;
VLS_CREATE (unique_name, 100);
make_unique_object_name (&unique_name, block);
entry = find_block_structure_table_entry (VLS_BEGIN (unique_name), FALSE);
VLS_DELETE (unique_name);
return *entry != NULL && (*entry)->block_may_be_used;
}
/* This page contains main function of the generator. */
/* The variable contains place where the generator finishes its work. */
static jmp_buf jump_to_finish;
/* The variable contains code of generation (0 - success). */
static int code_of_generation;
/* The function closes output file and finishes generation with CODE
(0 - success). */
static void
finish_generation (code)
int code;
{
if (output_file != NULL)
{
if (fclose (output_file) != 0)
{
output_file = NULL;
error_with_parameter (ERR_file_closing, modula_output_file_name);
}
}
code_of_generation = code;
longjmp (jump_to_finish, TRUE);
}
/* This is main function of Modula-2 generator of C code. Object file name
of current compilation unit is OUTPUT_FILE_NAME. The function returns TRUE
if the generation is successful. */
int
generator (output_file_name)
char *output_file_name;
{
ICN_pointer ref, scref;
modula_output_file_name = output_file_name;
if (setjmp (jump_to_finish))
return !code_of_generation;
output_file = NULL;
finish_procedure = finish_generation; /* for fatal error processing */
if (pass_of_picking_used_objects_when_all_flag)
/* Pick up of dependences between all objects. */
{
ref = first_block_begin ();
for (;;)
{
if (ref == NULL)
break;
scref = BLOCK_BEGIN_NODE (ref)->block_scope;
BLOCK_BEGIN (scref) = ref;
set_current_block_structure (scref);
process_statement_list (ref);
ref = BLOCK_BEGIN_NODE (ref)->next_block_begin;
}
}
else
{
output_file = fopen (modula_output_file_name, "w");
if (output_file == NULL)
error_with_parameter (ERR_object_file_opening,
modula_output_file_name);
/* for gdb, dbx. */
if (fprintf (output_file, "#line 1 \"%s\"\n#line 1 \"%s\"\n",
input_file_name, modula_output_file_name) < 0)
output_file_error ();
generate_all_declarations (current_compilation_unit);
ref = first_block_begin ();
for (;;)
{
if (ref == NULL)
break;
scref = BLOCK_BEGIN_NODE (ref)->block_scope;
BLOCK_BEGIN (scref) = ref;
if (MODE (scref) != ICNM_MODULE
|| ((enum module_mode) MODULE_NODE (scref)->module_mode
!= MM_LOCAL_MODULE))
{
execute_string_constants_declarations_generation_pass (scref);
generate_block (scref);
}
ref = BLOCK_BEGIN_NODE (ref)->next_block_begin;
}
}
finish_generation (0);
}
|