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
|
/*
AngelCode Scripting Library
Copyright (c) 2003-2021 Andreas Jonsson
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
The original version of this library can be located at:
http://www.angelcode.com/angelscript/
Andreas Jonsson
andreas@angelcode.com
*/
//
// as_restore.cpp
//
// Functions for saving and restoring module bytecode
// asCRestore was originally written by Dennis Bollyn, dennis@gyrbo.be
#include "as_config.h"
#include "as_restore.h"
#include "as_bytecode.h"
#include "as_scriptobject.h"
#include "as_texts.h"
#include "as_debug.h"
BEGIN_AS_NAMESPACE
// Macros for doing endianess agnostic bitmask serialization
#define SAVE_TO_BIT(dst, val, bit) ((dst) |= ((val) << (bit)))
#define LOAD_FROM_BIT(dst, val, bit) ((dst) = ((val) >> (bit)) & 1)
asCReader::asCReader(asCModule *_module, asIBinaryStream *_stream, asCScriptEngine *_engine)
: module(_module), stream(_stream), engine(_engine) {
error = false;
bytesRead = 0;
}
int asCReader::ReadData(void *data, asUINT size) {
asASSERT(size == 1 || size == 2 || size == 4 || size == 8);
int ret = 0;
#if defined(AS_BIG_ENDIAN)
for (asUINT n = 0; ret >= 0 && n < size; n++)
ret = stream->Read(((asBYTE *)data) + n, 1);
#else
for (int n = size - 1; ret >= 0 && n >= 0; n--)
ret = stream->Read(((asBYTE *)data) + n, 1);
#endif
if (ret < 0)
Error(TXT_UNEXPECTED_END_OF_FILE);
bytesRead += size;
return ret;
}
int asCReader::Read(bool *wasDebugInfoStripped) {
TimeIt("asCReader::Read");
// Before starting the load, make sure that
// any existing resources have been freed
module->InternalReset();
// Call the inner method to do the actual loading
int r = ReadInner();
if (r < 0) {
// Something went wrong while loading the bytecode, so we need
// to clean-up whatever has been created during the process.
// Make sure none of the loaded functions attempt to release
// references that have not yet been increased
asUINT i;
for (i = 0; i < module->m_scriptFunctions.GetLength(); i++)
if (!dontTranslate.MoveTo(0, module->m_scriptFunctions[i]))
if (module->m_scriptFunctions[i]->scriptData)
module->m_scriptFunctions[i]->scriptData->byteCode.SetLength(0);
asCSymbolTable<asCGlobalProperty>::iterator it = module->m_scriptGlobals.List();
for (; it; it++)
if ((*it)->GetInitFunc())
if ((*it)->GetInitFunc()->scriptData)
(*it)->GetInitFunc()->scriptData->byteCode.SetLength(0);
module->InternalReset();
} else {
// Init system functions properly
engine->PrepareEngine();
// Initialize the global variables (unless requested not to)
if (engine->ep.initGlobalVarsAfterBuild)
r = module->ResetGlobalVars(0);
if (wasDebugInfoStripped)
*wasDebugInfoStripped = noDebugInfo;
}
// Clean up the loaded string constants
for (asUINT n = 0; n < usedStringConstants.GetLength(); n++)
engine->stringFactory->ReleaseStringConstant(usedStringConstants[n]);
usedStringConstants.SetLength(0);
return r;
}
int asCReader::Error(const char *msg) {
// Don't write if it has already been reported an error earlier
if (!error) {
asCString str;
str.Format(msg, bytesRead);
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
error = true;
}
return asERROR;
}
int asCReader::ReadInner() {
TimeIt("asCReader::ReadInner");
// This function will load each entity one by one from the stream.
// If any error occurs, it will return to the caller who is
// responsible for cleaning up the partially loaded entities.
engine->deferValidationOfTemplateTypes = true;
unsigned long i, count;
asCScriptFunction *func;
// Read the flag as 1 byte even on platforms with 4byte booleans
noDebugInfo = ReadEncodedUInt() ? VALUE_OF_BOOLEAN_TRUE : 0;
// Read enums
count = SanityCheck(ReadEncodedUInt(), 1000000);
module->m_enumTypes.Allocate(count, false);
for (i = 0; i < count && !error; i++) {
asCEnumType *et = asNEW(asCEnumType)(engine);
if (et == 0) {
error = true;
return asOUT_OF_MEMORY;
}
bool isExternal = false;
ReadTypeDeclaration(et, 1, &isExternal);
// If the type is shared then we should use the original if it exists
bool sharedExists = false;
if (et->IsShared()) {
for (asUINT n = 0; n < engine->sharedScriptTypes.GetLength(); n++) {
asCTypeInfo *t = engine->sharedScriptTypes[n];
if (t &&
t->IsShared() &&
t->name == et->name &&
t->nameSpace == et->nameSpace &&
(t->flags & asOBJ_ENUM)) {
asDELETE(et, asCEnumType);
et = CastToEnumType(t);
sharedExists = true;
break;
}
}
}
if (isExternal && !sharedExists) {
asCString msg;
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, et->name.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
asDELETE(et, asCEnumType);
error = true;
return asERROR;
}
if (sharedExists) {
existingShared.Insert(et, true);
et->AddRefInternal();
} else {
if (et->IsShared()) {
engine->sharedScriptTypes.PushLast(et);
et->AddRefInternal();
}
// Set this module as the owner
et->module = module;
}
module->AddEnumType(et);
if (isExternal)
module->m_externalTypes.PushLast(et);
ReadTypeDeclaration(et, 2);
}
if (error) return asERROR;
// classTypes[]
// First restore the structure names, then the properties
count = SanityCheck(ReadEncodedUInt(), 1000000);
module->m_classTypes.Allocate(count, false);
for (i = 0; i < count && !error; ++i) {
asCObjectType *ot = asNEW(asCObjectType)(engine);
if (ot == 0) {
error = true;
return asOUT_OF_MEMORY;
}
bool isExternal = false;
ReadTypeDeclaration(ot, 1, &isExternal);
// If the type is shared, then we should use the original if it exists
bool sharedExists = false;
if (ot->IsShared()) {
for (asUINT n = 0; n < engine->sharedScriptTypes.GetLength(); n++) {
asCTypeInfo *ti = engine->sharedScriptTypes[n];
asCObjectType *t = CastToObjectType(ti);
if (t &&
t->IsShared() &&
t->name == ot->name &&
t->nameSpace == ot->nameSpace &&
t->IsInterface() == ot->IsInterface()) {
asDELETE(ot, asCObjectType);
ot = CastToObjectType(t);
sharedExists = true;
break;
}
}
}
if (isExternal && !sharedExists) {
asCString msg;
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, ot->name.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
asDELETE(ot, asCObjectType);
error = true;
return asERROR;
}
if (sharedExists) {
existingShared.Insert(ot, true);
ot->AddRefInternal();
} else {
if (ot->IsShared()) {
engine->sharedScriptTypes.PushLast(ot);
ot->AddRefInternal();
}
// Set this module as the owner
ot->module = module;
}
module->AddClassType(ot);
if (isExternal)
module->m_externalTypes.PushLast(ot);
}
if (error) return asERROR;
// Read func defs
count = SanityCheck(ReadEncodedUInt(), 1000000);
module->m_funcDefs.Allocate(count, false);
for (i = 0; i < count && !error; i++) {
bool isNew, isExternal;
asCScriptFunction *funcDef = ReadFunction(isNew, false, true, true, &isExternal);
if (funcDef) {
funcDef->module = module;
asCFuncdefType *fdt = funcDef->funcdefType;
fdt->module = module;
module->AddFuncDef(fdt);
engine->funcDefs.PushLast(fdt);
// TODO: clean up: This is also done by the builder. It should probably be moved to a method in the module
// Check if there is another identical funcdef from another module and if so reuse that instead
if (funcDef->IsShared()) {
for (asUINT n = 0; n < engine->funcDefs.GetLength(); n++) {
asCFuncdefType *f2 = engine->funcDefs[n];
if (f2 == 0 || fdt == f2)
continue;
if (!f2->funcdef->IsShared())
continue;
if (f2->name == fdt->name &&
f2->nameSpace == fdt->nameSpace &&
f2->parentClass == fdt->parentClass &&
f2->funcdef->IsSignatureExceptNameEqual(funcDef)) {
// Replace our funcdef for the existing one
module->ReplaceFuncDef(fdt, f2);
f2->AddRefInternal();
if (isExternal)
module->m_externalTypes.PushLast(f2);
engine->funcDefs.RemoveValue(fdt);
savedFunctions[savedFunctions.IndexOf(funcDef)] = f2->funcdef;
if (fdt->parentClass) {
// The real funcdef should already be in the object
asASSERT(fdt->parentClass->childFuncDefs.IndexOf(f2) >= 0);
fdt->parentClass = 0;
}
fdt->ReleaseInternal();
funcDef = 0;
break;
}
}
}
// Add the funcdef to the parentClass if this is a child funcdef
if (funcDef && fdt->parentClass)
fdt->parentClass->childFuncDefs.PushLast(fdt);
// Check if an external shared funcdef was really found
if (isExternal && funcDef) {
asCString msg;
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, funcDef->name.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
error = true;
return asERROR;
}
} else
Error(TXT_INVALID_BYTECODE_d);
}
// Read interface methods
for (i = 0; i < module->m_classTypes.GetLength() && !error; i++) {
if (module->m_classTypes[i]->IsInterface())
ReadTypeDeclaration(module->m_classTypes[i], 2);
}
// Read class methods and behaviours
for (i = 0; i < module->m_classTypes.GetLength() && !error; ++i) {
if (!module->m_classTypes[i]->IsInterface())
ReadTypeDeclaration(module->m_classTypes[i], 2);
}
// Read class properties
for (i = 0; i < module->m_classTypes.GetLength() && !error; ++i) {
if (!module->m_classTypes[i]->IsInterface())
ReadTypeDeclaration(module->m_classTypes[i], 3);
}
if (error) return asERROR;
// Read typedefs
count = SanityCheck(ReadEncodedUInt(), 1000000);
module->m_typeDefs.Allocate(count, false);
for (i = 0; i < count && !error; i++) {
asCTypedefType *td = asNEW(asCTypedefType)(engine);
if (td == 0) {
error = true;
return asOUT_OF_MEMORY;
}
bool isExternal = false;
ReadTypeDeclaration(td, 1, &isExternal);
td->module = module;
module->AddTypeDef(td);
ReadTypeDeclaration(td, 2);
}
if (error) return asERROR;
// scriptGlobals[]
count = SanityCheck(ReadEncodedUInt(), 1000000);
if (count && engine->ep.disallowGlobalVars) {
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_GLOBAL_VARS_NOT_ALLOWED);
Error(TXT_INVALID_BYTECODE_d);
}
module->m_scriptGlobals.Allocate(count, false);
for (i = 0; i < count && !error; ++i) {
ReadGlobalProperty();
}
// scriptFunctions[]
count = SanityCheck(ReadEncodedUInt(), 1000000);
for (i = 0; i < count && !error; ++i) {
size_t len = module->m_scriptFunctions.GetLength();
bool isNew, isExternal;
func = ReadFunction(isNew, true, true, true, &isExternal);
if (func == 0) {
Error(TXT_INVALID_BYTECODE_d);
break;
}
// Is the function shared and was it created now?
if (func->IsShared() && len != module->m_scriptFunctions.GetLength()) {
// If the function already existed in another module, then
// we need to replace it with previously existing one
for (asUINT n = 0; n < engine->scriptFunctions.GetLength() && !error; n++) {
asCScriptFunction *realFunc = engine->scriptFunctions[n];
if (realFunc &&
realFunc != func &&
realFunc->IsShared() &&
realFunc->nameSpace == func->nameSpace &&
realFunc->IsSignatureEqual(func)) {
// Replace the recently created function with the pre-existing function
module->m_scriptFunctions[module->m_scriptFunctions.GetLength() - 1] = realFunc;
realFunc->AddRefInternal();
savedFunctions[savedFunctions.GetLength() - 1] = realFunc;
engine->RemoveScriptFunction(func);
// Insert the function in the dontTranslate array
dontTranslate.Insert(realFunc, true);
if (isExternal)
module->m_externalFunctions.PushLast(realFunc);
// Release the function, but make sure nothing else is released
func->id = 0;
if (func->scriptData)
func->scriptData->byteCode.SetLength(0);
func->ReleaseInternal();
func = 0;
break;
}
}
}
// Check if an external shared func was really found
if (isExternal && func) {
asCString msg;
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, func->name.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
error = true;
return asERROR;
}
}
// globalFunctions[]
count = SanityCheck(ReadEncodedUInt(), 1000000);
for (i = 0; i < count && !error; ++i) {
bool isNew;
func = ReadFunction(isNew, false, false);
if (func) {
// All the global functions were already loaded while loading the scriptFunctions, here
// we're just re-reading the references to know which goes into the globalFunctions array
asASSERT(!isNew);
module->m_globalFunctions.Put(func);
} else
Error(TXT_INVALID_BYTECODE_d);
}
if (error) return asERROR;
// bindInformations[]
count = SanityCheck(ReadEncodedUInt(), 1000000);
module->m_bindInformations.Allocate(count, false);
for (i = 0; i < count && !error; ++i) {
sBindInfo *info = asNEW(sBindInfo);
if (info == 0) {
error = true;
return asOUT_OF_MEMORY;
}
bool isNew;
info->importedFunctionSignature = ReadFunction(isNew, false, false);
if (info->importedFunctionSignature == 0) {
Error(TXT_INVALID_BYTECODE_d);
break;
}
if (engine->freeImportedFunctionIdxs.GetLength()) {
int id = engine->freeImportedFunctionIdxs.PopLast();
info->importedFunctionSignature->id = int(FUNC_IMPORTED + id);
engine->importedFunctions[id] = info;
} else {
info->importedFunctionSignature->id = int(FUNC_IMPORTED + engine->importedFunctions.GetLength());
engine->importedFunctions.PushLast(info);
}
ReadString(&info->importFromModule);
info->boundFunctionId = -1;
module->m_bindInformations.PushLast(info);
}
if (error) return asERROR;
// usedTypes[]
count = SanityCheck(ReadEncodedUInt(), 1000000);
usedTypes.Allocate(count, false);
for (i = 0; i < count && !error; ++i) {
asCTypeInfo *ti = ReadTypeInfo();
usedTypes.PushLast(ti);
}
// usedTypeIds[]
if (!error)
ReadUsedTypeIds();
// usedFunctions[]
if (!error)
ReadUsedFunctions();
// usedGlobalProperties[]
if (!error)
ReadUsedGlobalProps();
// usedStringConstants[]
if (!error)
ReadUsedStringConstants();
// usedObjectProperties
if (!error)
ReadUsedObjectProps();
// Validate the template types
if (!error) {
for (i = 0; i < usedTypes.GetLength() && !error; i++) {
asCObjectType *ot = CastToObjectType(usedTypes[i]);
if (!ot ||
!(ot->flags & asOBJ_TEMPLATE) ||
!ot->beh.templateCallback)
continue;
bool dontGarbageCollect = false;
asCScriptFunction *callback = engine->scriptFunctions[ot->beh.templateCallback];
if (!engine->CallGlobalFunctionRetBool(ot, &dontGarbageCollect, callback->sysFuncIntf, callback)) {
asCString sub = ot->templateSubTypes[0].Format(ot->nameSpace);
for (asUINT n = 1; n < ot->templateSubTypes.GetLength(); n++) {
sub += ",";
sub += ot->templateSubTypes[n].Format(ot->nameSpace);
}
asCString str;
str.Format(TXT_INSTANCING_INVLD_TMPL_TYPE_s_s, ot->name.AddressOf(), sub.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
} else {
// If the callback said this template instance won't be garbage collected then remove the flag
if (dontGarbageCollect)
ot->flags &= ~asOBJ_GC;
}
}
}
engine->deferValidationOfTemplateTypes = false;
if (error) return asERROR;
// Update the loaded bytecode to point to the correct types, property offsets,
// function ids, etc. This is basically a linking stage.
for (i = 0; i < module->m_scriptFunctions.GetLength() && !error; i++)
if (module->m_scriptFunctions[i]->funcType == asFUNC_SCRIPT)
TranslateFunction(module->m_scriptFunctions[i]);
asCSymbolTable<asCGlobalProperty>::iterator globIt = module->m_scriptGlobals.List();
while (globIt && !error) {
asCScriptFunction *initFunc = (*globIt)->GetInitFunc();
if (initFunc)
TranslateFunction(initFunc);
globIt++;
}
if (error) return asERROR;
// Add references for all functions (except for the pre-existing shared code)
for (i = 0; i < module->m_scriptFunctions.GetLength(); i++)
if (!dontTranslate.MoveTo(0, module->m_scriptFunctions[i]))
module->m_scriptFunctions[i]->AddReferences();
globIt = module->m_scriptGlobals.List();
while (globIt) {
asCScriptFunction *initFunc = (*globIt)->GetInitFunc();
if (initFunc)
initFunc->AddReferences();
globIt++;
}
return error ? asERROR : asSUCCESS;
}
void asCReader::ReadUsedStringConstants() {
TimeIt("asCReader::ReadUsedStringConstants");
asCString str;
asUINT count;
count = SanityCheck(ReadEncodedUInt(), 1000000);
if (count > 0 && engine->stringFactory == 0) {
Error(TXT_STRINGS_NOT_RECOGNIZED);
return;
}
usedStringConstants.Allocate(count, false);
for (asUINT i = 0; i < count; ++i) {
ReadString(&str);
usedStringConstants.PushLast(const_cast<void *>(engine->stringFactory->GetStringConstant(str.AddressOf(), (asUINT)str.GetLength())));
}
}
void asCReader::ReadUsedFunctions() {
TimeIt("asCReader::ReadUsedFunctions");
asUINT count;
count = SanityCheck(ReadEncodedUInt(), 1000000);
usedFunctions.SetLength(count);
if (usedFunctions.GetLength() != count) {
// Out of memory
error = true;
return;
}
memset(usedFunctions.AddressOf(), 0, sizeof(asCScriptFunction *)*count);
for (asUINT n = 0; n < usedFunctions.GetLength(); n++) {
char c;
// Read the data to be able to uniquely identify the function
// Is the function from the module or the application?
ReadData(&c, 1);
if (c == 'n') {
// Null function pointer
usedFunctions[n] = 0;
} else {
asCScriptFunction func(engine, c == 'm' ? module : 0, asFUNC_DUMMY);
asCObjectType *parentClass = 0;
ReadFunctionSignature(&func, &parentClass);
if (error) {
func.funcType = asFUNC_DUMMY;
return;
}
// Find the correct function
if (c == 'm') {
if (func.funcType == asFUNC_IMPORTED) {
for (asUINT i = 0; i < module->m_bindInformations.GetLength(); i++) {
asCScriptFunction *f = module->m_bindInformations[i]->importedFunctionSignature;
if (func.objectType != f->objectType ||
func.funcType != f->funcType ||
func.nameSpace != f->nameSpace ||
!func.IsSignatureEqual(f))
continue;
usedFunctions[n] = f;
break;
}
} else if (func.funcType == asFUNC_FUNCDEF) {
const asCArray<asCFuncdefType *> &funcs = module->m_funcDefs;
for (asUINT i = 0; i < funcs.GetLength(); i++) {
asCScriptFunction *f = funcs[i]->funcdef;
if (f == 0 ||
func.name != f->name ||
!func.IsSignatureExceptNameAndObjectTypeEqual(f) ||
funcs[i]->parentClass != parentClass)
continue;
asASSERT(f->objectType == 0);
usedFunctions[n] = f;
break;
}
} else {
// TODO: optimize: Global functions should be searched for in module->globalFunctions
// TODO: optimize: funcdefs should be searched for in module->funcDefs
// TODO: optimize: object methods should be searched for directly in the object type
for (asUINT i = 0; i < module->m_scriptFunctions.GetLength(); i++) {
asCScriptFunction *f = module->m_scriptFunctions[i];
if (func.objectType != f->objectType ||
func.funcType != f->funcType ||
func.nameSpace != f->nameSpace ||
!func.IsSignatureEqual(f))
continue;
usedFunctions[n] = f;
break;
}
}
} else if (c == 's') {
// Look for shared entities in the engine, as they may not necessarily be part
// of the scope of the module if they have been inhereted from other modules.
if (func.funcType == asFUNC_FUNCDEF) {
const asCArray<asCFuncdefType *> &funcs = engine->funcDefs;
for (asUINT i = 0; i < funcs.GetLength(); i++) {
asCScriptFunction *f = funcs[i]->funcdef;
if (f == 0 ||
func.name != f->name ||
!func.IsSignatureExceptNameAndObjectTypeEqual(f) ||
funcs[i]->parentClass != parentClass)
continue;
asASSERT(f->objectType == 0);
usedFunctions[n] = f;
break;
}
} else {
for (asUINT i = 0; i < engine->scriptFunctions.GetLength(); i++) {
asCScriptFunction *f = engine->scriptFunctions[i];
if (f == 0 || !f->IsShared() ||
func.objectType != f->objectType ||
func.funcType != f->funcType ||
func.nameSpace != f->nameSpace ||
!func.IsSignatureEqual(f))
continue;
usedFunctions[n] = f;
break;
}
}
} else {
asASSERT(c == 'a');
if (func.funcType == asFUNC_FUNCDEF) {
// This is a funcdef (registered or shared)
const asCArray<asCFuncdefType *> &funcs = engine->funcDefs;
for (asUINT i = 0; i < funcs.GetLength(); i++) {
asCScriptFunction *f = funcs[i]->funcdef;
if (f == 0 || func.name != f->name || !func.IsSignatureExceptNameAndObjectTypeEqual(f) || funcs[i]->parentClass != parentClass)
continue;
asASSERT(f->objectType == 0);
usedFunctions[n] = f;
break;
}
} else if (func.name[0] == '$') {
// This is a special function
if (func.name == "$beh0" && func.objectType) {
if (func.objectType->flags & asOBJ_TEMPLATE) {
// Look for the matching constructor inside the factory stubs generated for the template instance
// See asCCompiler::PerformFunctionCall
for (asUINT i = 0; i < func.objectType->beh.constructors.GetLength(); i++) {
asCScriptFunction *f = engine->scriptFunctions[func.objectType->beh.constructors[i]];
// Find the id of the real constructor and not the generated stub
asUINT id = 0;
asDWORD *bc = f->scriptData->byteCode.AddressOf();
while (bc) {
if ((*(asBYTE *)bc) == asBC_CALLSYS) {
id = asBC_INTARG(bc);
break;
}
bc += asBCTypeSize[asBCInfo[*(asBYTE *)bc].type];
}
f = engine->scriptFunctions[id];
if (f == 0 ||
!func.IsSignatureExceptNameAndObjectTypeEqual(f))
continue;
usedFunctions[n] = f;
break;
}
}
if (usedFunctions[n] == 0) {
// This is a class constructor, so we can search directly in the object type's constructors
for (asUINT i = 0; i < func.objectType->beh.constructors.GetLength(); i++) {
asCScriptFunction *f = engine->scriptFunctions[func.objectType->beh.constructors[i]];
if (f == 0 ||
!func.IsSignatureExceptNameAndObjectTypeEqual(f))
continue;
usedFunctions[n] = f;
break;
}
}
} else if (func.name == "$fact" || func.name == "$beh3") {
// This is a factory (or stub), so look for the function in the return type's factories
asCObjectType *objType = CastToObjectType(func.returnType.GetTypeInfo());
if (objType) {
for (asUINT i = 0; i < objType->beh.factories.GetLength(); i++) {
asCScriptFunction *f = engine->scriptFunctions[objType->beh.factories[i]];
if (f == 0 ||
!func.IsSignatureExceptNameAndObjectTypeEqual(f))
continue;
usedFunctions[n] = f;
break;
}
}
} else if (func.name == "$list") {
// listFactory is used for both factory is global and returns a handle and constructor that is a method
asCObjectType *objType = func.objectType ? func.objectType : CastToObjectType(func.returnType.GetTypeInfo());
if (objType) {
asCScriptFunction *f = engine->scriptFunctions[objType->beh.listFactory];
if (f && func.IsSignatureExceptNameAndObjectTypeEqual(f))
usedFunctions[n] = f;
}
} else if (func.name == "$beh2") {
// This is a destructor, so check the object type's destructor
asCObjectType *objType = func.objectType;
if (objType) {
asCScriptFunction *f = engine->scriptFunctions[objType->beh.destruct];
if (f && func.IsSignatureExceptNameAndObjectTypeEqual(f))
usedFunctions[n] = f;
}
} else if (func.name == "$dlgte") {
// This is the delegate factory
asCScriptFunction *f = engine->registeredGlobalFuncs.GetFirst(engine->nameSpaces[0], DELEGATE_FACTORY);
asASSERT(f && func.IsSignatureEqual(f));
usedFunctions[n] = f;
} else {
// Must match one of the above cases
asASSERT(false);
}
} else if (func.objectType == 0) {
// This is a global function
const asCArray<asUINT> &funcs = engine->registeredGlobalFuncs.GetIndexes(func.nameSpace, func.name);
for (asUINT i = 0; i < funcs.GetLength(); i++) {
asCScriptFunction *f = engine->registeredGlobalFuncs.Get(funcs[i]);
if (f == 0 ||
!func.IsSignatureExceptNameAndObjectTypeEqual(f))
continue;
usedFunctions[n] = f;
break;
}
} else if (func.objectType) {
// It is a class member, so we can search directly in the object type's members
// TODO: virtual function is different that implemented method
for (asUINT i = 0; i < func.objectType->methods.GetLength(); i++) {
asCScriptFunction *f = engine->scriptFunctions[func.objectType->methods[i]];
if (f == 0 ||
!func.IsSignatureEqual(f))
continue;
usedFunctions[n] = f;
break;
}
}
if (usedFunctions[n] == 0) {
// TODO: clean up: This part of the code should never happen. All functions should
// be found in the above logic. The only valid reason to come here
// is if the bytecode is wrong and the function doesn't exist anyway.
// This loop is kept temporarily until we can be certain all scenarios
// are covered.
for (asUINT i = 0; i < engine->scriptFunctions.GetLength(); i++) {
asCScriptFunction *f = engine->scriptFunctions[i];
if (f == 0 ||
func.objectType != f->objectType ||
func.nameSpace != f->nameSpace ||
!func.IsSignatureEqual(f))
continue;
usedFunctions[n] = f;
break;
}
// No function is expected to be found
asASSERT(usedFunctions[n] == 0);
}
}
// Set the type to dummy so it won't try to release the id
func.funcType = asFUNC_DUMMY;
if (usedFunctions[n] == 0) {
Error(TXT_INVALID_BYTECODE_d);
return;
}
}
}
}
void asCReader::ReadFunctionSignature(asCScriptFunction *func, asCObjectType **parentClass) {
asUINT i, count;
asCDataType dt;
int num;
ReadString(&func->name);
if (func->name == DELEGATE_FACTORY) {
// It's not necessary to read anymore, everything is known
asCScriptFunction *f = engine->registeredGlobalFuncs.GetFirst(engine->nameSpaces[0], DELEGATE_FACTORY);
asASSERT(f);
func->returnType = f->returnType;
func->parameterTypes = f->parameterTypes;
func->inOutFlags = f->inOutFlags;
func->funcType = f->funcType;
func->defaultArgs = f->defaultArgs;
func->nameSpace = f->nameSpace;
return;
}
ReadDataType(&func->returnType);
count = SanityCheck(ReadEncodedUInt(), 256);
func->parameterTypes.Allocate(count, false);
for (i = 0; i < count; ++i) {
ReadDataType(&dt);
func->parameterTypes.PushLast(dt);
}
func->inOutFlags.SetLength(func->parameterTypes.GetLength());
if (func->inOutFlags.GetLength() != func->parameterTypes.GetLength()) {
// Out of memory
error = true;
return;
}
memset(func->inOutFlags.AddressOf(), 0, sizeof(asETypeModifiers)*func->inOutFlags.GetLength());
if (func->parameterTypes.GetLength() > 0) {
count = ReadEncodedUInt();
if (count > func->parameterTypes.GetLength()) {
// Cannot be more than the number of arguments
Error(TXT_INVALID_BYTECODE_d);
return;
}
for (i = 0; i < count; ++i) {
num = ReadEncodedUInt();
func->inOutFlags[i] = static_cast<asETypeModifiers>(num);
}
}
func->funcType = (asEFuncType)ReadEncodedUInt();
// Read the default args, from last to first
if (func->parameterTypes.GetLength() > 0) {
count = ReadEncodedUInt();
if (count > func->parameterTypes.GetLength()) {
// Cannot be more than the number of arguments
Error(TXT_INVALID_BYTECODE_d);
return;
}
if (count) {
func->defaultArgs.SetLength(func->parameterTypes.GetLength());
if (func->defaultArgs.GetLength() != func->parameterTypes.GetLength()) {
// Out of memory
error = true;
return;
}
memset(func->defaultArgs.AddressOf(), 0, sizeof(asCString *)*func->defaultArgs.GetLength());
for (i = 0; i < count; i++) {
asCString *str = asNEW(asCString);
if (str == 0) {
// Out of memory
error = true;
return;
}
func->defaultArgs[func->defaultArgs.GetLength() - 1 - i] = str;
ReadString(str);
}
}
}
func->objectType = CastToObjectType(ReadTypeInfo());
if (func->objectType) {
func->objectType->AddRefInternal();
asBYTE b;
ReadData(&b, 1);
func->SetReadOnly((b & 1) ? true : false);
func->SetPrivate((b & 2) ? true : false);
func->SetProtected((b & 4) ? true : false);
func->nameSpace = func->objectType->nameSpace;
} else {
if (func->funcType == asFUNC_FUNCDEF) {
asBYTE b;
ReadData(&b, 1);
if (b == 'n') {
asCString ns;
ReadString(&ns);
func->nameSpace = engine->AddNameSpace(ns.AddressOf());
} else if (b == 'o') {
func->nameSpace = 0;
if (parentClass)
*parentClass = CastToObjectType(ReadTypeInfo());
else
error = true;
} else
error = true;
} else {
asCString ns;
ReadString(&ns);
func->nameSpace = engine->AddNameSpace(ns.AddressOf());
}
}
}
asCScriptFunction *asCReader::ReadFunction(bool &isNew, bool addToModule, bool addToEngine, bool addToGC, bool *isExternal) {
isNew = false;
if (isExternal) *isExternal = false;
if (error) return 0;
char c;
ReadData(&c, 1);
if (c == '\0') {
// There is no function, so return a null pointer
return 0;
}
if (c == 'r') {
// This is a reference to a previously saved function
asUINT index = ReadEncodedUInt();
if (index < savedFunctions.GetLength())
return savedFunctions[index];
else {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
}
// Load the new function
isNew = true;
asCScriptFunction *func = asNEW(asCScriptFunction)(engine, 0, asFUNC_DUMMY);
if (func == 0) {
// Out of memory
error = true;
return 0;
}
savedFunctions.PushLast(func);
int i, count;
asCDataType dt;
int num;
asCObjectType *parentClass = 0;
ReadFunctionSignature(func, &parentClass);
if (error) {
func->DestroyHalfCreated();
return 0;
}
if (func->funcType == asFUNC_SCRIPT) {
// Skip this for external shared entities
if (module->m_externalTypes.IndexOf(func->objectType) >= 0) {
// Replace with the real function from the existing entity
isNew = false;
asCObjectType *ot = func->objectType;
for (asUINT n = 0; n < ot->methods.GetLength(); n++) {
asCScriptFunction *func2 = engine->scriptFunctions[ot->methods[n]];
if (func2->funcType == asFUNC_VIRTUAL)
func2 = ot->virtualFunctionTable[func2->vfTableIdx];
if (func->IsSignatureEqual(func2)) {
func->DestroyHalfCreated();
// as this is an existing function it shouldn't be translated as if just loaded
dontTranslate.Insert(func2, true);
// update the saved functions for future references
savedFunctions[savedFunctions.GetLength() - 1] = func2;
// As it is an existing function it shouldn't be added to the module or the engine
return func2;
}
}
} else {
char bits;
ReadData(&bits, 1);
func->SetShared((bits & 1) ? true : false);
func->SetExplicit((bits & 32) ? true : false);
func->dontCleanUpOnException = (bits & 2) ? true : false;
if ((bits & 4) && isExternal)
*isExternal = true;
// for external shared functions the rest is not needed
if (!(bits & 4)) {
func->AllocateScriptFunctionData();
if (func->scriptData == 0) {
// Out of memory
error = true;
func->DestroyHalfCreated();
return 0;
}
if (addToGC && !addToModule)
engine->gc.AddScriptObjectToGC(func, &engine->functionBehaviours);
ReadByteCode(func);
func->scriptData->variableSpace = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->objVariablesOnHeap = 0;
if (bits & 8) {
count = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->objVariablePos.Allocate(count, false);
func->scriptData->objVariableTypes.Allocate(count, false);
for (i = 0; i < count; ++i) {
func->scriptData->objVariableTypes.PushLast(ReadTypeInfo());
num = ReadEncodedUInt();
func->scriptData->objVariablePos.PushLast(num);
if (error) {
// No need to continue (the error has already been reported before)
func->DestroyHalfCreated();
return 0;
}
}
if (count > 0)
func->scriptData->objVariablesOnHeap = SanityCheck(ReadEncodedUInt(), 10000);
int length = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->objVariableInfo.SetLength(length);
for (i = 0; i < length; ++i) {
func->scriptData->objVariableInfo[i].programPos = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->objVariableInfo[i].variableOffset = SanityCheck(ReadEncodedInt(), 10000);
asEObjVarInfoOption option = (asEObjVarInfoOption)ReadEncodedUInt();
func->scriptData->objVariableInfo[i].option = option;
if (option != asOBJ_INIT &&
option != asOBJ_UNINIT &&
option != asBLOCK_BEGIN &&
option != asBLOCK_END &&
option != asOBJ_VARDECL) {
error = true;
func->DestroyHalfCreated();
return 0;
}
}
}
if (bits & 16) {
// Read info on try/catch blocks
int length = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->tryCatchInfo.SetLength(length);
for (i = 0; i < length; ++i) {
// The program position must be adjusted to be in number of instructions
func->scriptData->tryCatchInfo[i].tryPos = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->tryCatchInfo[i].catchPos = SanityCheck(ReadEncodedUInt(), 1000000);
}
}
if (!noDebugInfo) {
int length = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->lineNumbers.SetLength(length);
if (int(func->scriptData->lineNumbers.GetLength()) != length) {
// Out of memory
error = true;
func->DestroyHalfCreated();
return 0;
}
for (i = 0; i < length; ++i)
func->scriptData->lineNumbers[i] = ReadEncodedUInt();
// Read the array of script sections
length = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->sectionIdxs.SetLength(length);
if (int(func->scriptData->sectionIdxs.GetLength()) != length) {
// Out of memory
error = true;
func->DestroyHalfCreated();
return 0;
}
for (i = 0; i < length; ++i) {
if ((i & 1) == 0)
func->scriptData->sectionIdxs[i] = ReadEncodedUInt();
else {
asCString str;
ReadString(&str);
func->scriptData->sectionIdxs[i] = engine->GetScriptSectionNameIndex(str.AddressOf());
}
}
}
// Read the variable information
if (!noDebugInfo) {
int length = SanityCheck(ReadEncodedUInt(), 1000000);
func->scriptData->variables.Allocate(length, false);
for (i = 0; i < length; i++) {
asSScriptVariable *var = asNEW(asSScriptVariable);
if (var == 0) {
// Out of memory
error = true;
func->DestroyHalfCreated();
return 0;
}
func->scriptData->variables.PushLast(var);
var->declaredAtProgramPos = ReadEncodedUInt();
var->stackOffset = SanityCheck(ReadEncodedInt(), 10000);
ReadString(&var->name);
ReadDataType(&var->type);
if (error) {
// No need to continue (the error has already been reported before)
func->DestroyHalfCreated();
return 0;
}
}
}
// Read script section name
if (!noDebugInfo) {
asCString name;
ReadString(&name);
func->scriptData->scriptSectionIdx = engine->GetScriptSectionNameIndex(name.AddressOf());
func->scriptData->declaredAt = ReadEncodedUInt();
}
// Read parameter names
if (!noDebugInfo) {
asUINT countParam = asUINT(ReadEncodedUInt64());
if (countParam > func->parameterTypes.GetLength()) {
error = true;
func->DestroyHalfCreated();
return 0;
}
func->parameterNames.SetLength(countParam);
for (asUINT n = 0; n < countParam; n++)
ReadString(&func->parameterNames[n]);
}
}
}
} else if (func->funcType == asFUNC_VIRTUAL || func->funcType == asFUNC_INTERFACE) {
func->vfTableIdx = ReadEncodedUInt();
} else if (func->funcType == asFUNC_FUNCDEF) {
asBYTE bits;
ReadData(&bits, 1);
if (bits & 1)
func->SetShared(true);
if ((bits & 2) && isExternal)
*isExternal = true;
// The asCFuncdefType constructor adds itself to the func->funcdefType member
asCFuncdefType *fdt = asNEW(asCFuncdefType)(engine, func);
fdt->parentClass = parentClass;
}
// Methods loaded for shared objects, owned by other modules should not be created as new functions
if (func->objectType && func->objectType->module != module) {
// Return the real function from the object
asCScriptFunction *realFunc = 0;
bool found = false;
if (func->funcType == asFUNC_SCRIPT) {
realFunc = engine->scriptFunctions[func->objectType->beh.destruct];
if (realFunc && realFunc->funcType != asFUNC_VIRTUAL && func->IsSignatureEqual(realFunc)) {
found = true;
}
for (asUINT n = 0; !found && n < func->objectType->beh.constructors.GetLength(); n++) {
realFunc = engine->scriptFunctions[func->objectType->beh.constructors[n]];
if (realFunc && realFunc->funcType != asFUNC_VIRTUAL && func->IsSignatureEqual(realFunc)) {
found = true;
break;
}
}
for (asUINT n = 0; !found && n < func->objectType->beh.factories.GetLength(); n++) {
realFunc = engine->scriptFunctions[func->objectType->beh.factories[n]];
if (realFunc && realFunc->funcType != asFUNC_VIRTUAL && func->IsSignatureEqual(realFunc)) {
found = true;
break;
}
}
for (asUINT n = 0; !found && n < func->objectType->methods.GetLength(); n++) {
realFunc = engine->scriptFunctions[func->objectType->methods[n]];
if (realFunc && realFunc->funcType == func->funcType && func->IsSignatureEqual(realFunc)) {
found = true;
break;
}
}
for (asUINT n = 0; !found && n < func->objectType->virtualFunctionTable.GetLength(); n++) {
realFunc = func->objectType->virtualFunctionTable[n];
if (realFunc && realFunc->funcType == func->funcType && func->IsSignatureEqual(realFunc)) {
found = true;
break;
}
}
} else if (func->funcType == asFUNC_VIRTUAL || func->funcType == asFUNC_INTERFACE) {
// If the loaded function is a virtual function, then look for the identical virtual function in the methods array
for (asUINT n = 0; n < func->objectType->methods.GetLength(); n++) {
realFunc = engine->scriptFunctions[func->objectType->methods[n]];
if (realFunc && realFunc->funcType == func->funcType && func->IsSignatureEqual(realFunc)) {
asASSERT(func->vfTableIdx == realFunc->vfTableIdx);
found = true;
break;
}
}
}
if (found) {
// as this is an existing function it shouldn't be translated as if just loaded
dontTranslate.Insert(realFunc, true);
// update the saved functions for future references
savedFunctions[savedFunctions.GetLength() - 1] = realFunc;
if (realFunc->funcType == asFUNC_VIRTUAL && addToModule) {
// Virtual methods must be added to the module's script functions array,
// even if they are not owned by the module
module->m_scriptFunctions.PushLast(realFunc);
realFunc->AddRefInternal();
}
} else {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, func->objectType->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
savedFunctions.PopLast();
realFunc = 0;
}
// Destroy the newly created function instance since it has been replaced by an existing function
isNew = false;
func->DestroyHalfCreated();
// As it is an existing function it shouldn't be added to the module or the engine
return realFunc;
}
if (addToModule) {
// The refCount is already 1
module->m_scriptFunctions.PushLast(func);
func->module = module;
}
if (addToEngine) {
func->id = engine->GetNextScriptFunctionId();
engine->AddScriptFunction(func);
}
if (func->objectType)
func->ComputeSignatureId();
return func;
}
void asCReader::ReadTypeDeclaration(asCTypeInfo *type, int phase, bool *isExternal) {
if (phase == 1) {
asASSERT(isExternal);
if (isExternal)
*isExternal = false;
// Read the initial attributes
ReadString(&type->name);
ReadData(&type->flags, 4);
type->size = SanityCheck(ReadEncodedUInt(), 1000000);
asCString ns;
ReadString(&ns);
type->nameSpace = engine->AddNameSpace(ns.AddressOf());
// Verify that the flags match the asCTypeInfo
if ((CastToEnumType(type) && !(type->flags & asOBJ_ENUM)) ||
(CastToFuncdefType(type) && !(type->flags & asOBJ_FUNCDEF)) ||
(CastToObjectType(type) && !(type->flags & (asOBJ_REF | asOBJ_VALUE)))) {
error = true;
return;
}
// Reset the size of script classes, since it will be recalculated as properties are added
if ((type->flags & asOBJ_SCRIPT_OBJECT) && type->size != 0)
type->size = sizeof(asCScriptObject);
asCObjectType *ot = CastToObjectType(type);
if (ot) {
// Use the default script class behaviours
ot->beh = engine->scriptTypeBehaviours.beh;
ot->beh.construct = 0;
ot->beh.factory = 0;
ot->beh.constructors.PopLast(); // These will be read from the file
ot->beh.factories.PopLast(); // These will be read from the file
engine->scriptFunctions[ot->beh.addref]->AddRefInternal();
engine->scriptFunctions[ot->beh.release]->AddRefInternal();
engine->scriptFunctions[ot->beh.gcEnumReferences]->AddRefInternal();
engine->scriptFunctions[ot->beh.gcGetFlag]->AddRefInternal();
engine->scriptFunctions[ot->beh.gcGetRefCount]->AddRefInternal();
engine->scriptFunctions[ot->beh.gcReleaseAllReferences]->AddRefInternal();
engine->scriptFunctions[ot->beh.gcSetFlag]->AddRefInternal();
engine->scriptFunctions[ot->beh.copy]->AddRefInternal();
// TODO: weak: Should not do this if the class has been declared with 'noweak'
engine->scriptFunctions[ot->beh.getWeakRefFlag]->AddRefInternal();
}
// external shared flag
if (type->flags & asOBJ_SHARED) {
char c;
ReadData(&c, 1);
if (c == 'e')
*isExternal = true;
else if (c != ' ') {
error = true;
return;
}
}
} else if (phase == 2) {
// external shared types doesn't store this
if ((type->flags & asOBJ_SHARED) && module->m_externalTypes.IndexOf(type) >= 0)
return;
if (type->flags & asOBJ_ENUM) {
asCEnumType *t = CastToEnumType(type);
int count = SanityCheck(ReadEncodedUInt(), 1000000);
bool sharedExists = existingShared.MoveTo(0, type);
if (!sharedExists) {
t->enumValues.Allocate(count, false);
for (int n = 0; n < count; n++) {
asSEnumValue *e = asNEW(asSEnumValue);
if (e == 0) {
// Out of memory
error = true;
return;
}
ReadString(&e->name);
ReadData(&e->value, 4); // TODO: Should be encoded
t->enumValues.PushLast(e);
}
} else {
// Verify that the enum values exists in the original
asCString name;
int value;
for (int n = 0; n < count; n++) {
ReadString(&name);
ReadData(&value, 4); // TODO: Should be encoded
bool found = false;
for (asUINT e = 0; e < t->enumValues.GetLength(); e++) {
if (t->enumValues[e]->name == name &&
t->enumValues[e]->value == value) {
found = true;
break;
}
}
if (!found) {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
}
}
} else if (type->flags & asOBJ_TYPEDEF) {
asCTypedefType *td = CastToTypedefType(type);
asASSERT(td);
eTokenType t = (eTokenType)ReadEncodedUInt();
td->aliasForType = asCDataType::CreatePrimitive(t, false);
} else {
asCObjectType *ot = CastToObjectType(type);
asASSERT(ot);
// If the type is shared and pre-existing, we should just
// validate that the loaded methods match the original
bool sharedExists = existingShared.MoveTo(0, type);
if (sharedExists) {
asCObjectType *dt = CastToObjectType(ReadTypeInfo());
if (ot->derivedFrom != dt) {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
} else {
ot->derivedFrom = CastToObjectType(ReadTypeInfo());
if (ot->derivedFrom)
ot->derivedFrom->AddRefInternal();
}
// interfaces[] / interfaceVFTOffsets[]
int size = SanityCheck(ReadEncodedUInt(), 1000000);
if (sharedExists) {
for (int n = 0; n < size; n++) {
asCObjectType *intf = CastToObjectType(ReadTypeInfo());
if (!ot->IsInterface())
ReadEncodedUInt();
if (!type->Implements(intf)) {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
}
} else {
ot->interfaces.Allocate(size, false);
if (!ot->IsInterface())
ot->interfaceVFTOffsets.Allocate(size, false);
for (int n = 0; n < size; n++) {
asCObjectType *intf = CastToObjectType(ReadTypeInfo());
ot->interfaces.PushLast(intf);
if (!ot->IsInterface()) {
asUINT offset = SanityCheck(ReadEncodedUInt(), 1000000);
ot->interfaceVFTOffsets.PushLast(offset);
}
}
}
// behaviours
if (!ot->IsInterface() && type->flags != asOBJ_TYPEDEF && type->flags != asOBJ_ENUM) {
bool isNew;
asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
if (sharedExists) {
// Find the real function in the object, and update the savedFunctions array
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.destruct);
if ((realFunc == 0 && func == 0) || realFunc->IsSignatureEqual(func)) {
// If the function is not the last, then the substitution has already occurred before
if (func && savedFunctions[savedFunctions.GetLength() - 1] == func)
savedFunctions[savedFunctions.GetLength() - 1] = realFunc;
} else {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
if (func) {
if (isNew) {
// Destroy the function without releasing any references
func->id = 0;
func->scriptData->byteCode.SetLength(0);
func->ReleaseInternal();
}
dontTranslate.Insert(realFunc, true);
}
} else {
if (func) {
ot->beh.destruct = func->id;
func->AddRefInternal();
} else
ot->beh.destruct = 0;
}
size = SanityCheck(ReadEncodedUInt(), 1000000);
for (int n = 0; n < size; n++) {
func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
if (func) {
if (sharedExists) {
// Find the real function in the object, and update the savedFunctions array
bool found = false;
for (asUINT f = 0; f < ot->beh.constructors.GetLength(); f++) {
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.constructors[f]);
if (realFunc->IsSignatureEqual(func)) {
// If the function is not the last, then the substitution has already occurred before
if (savedFunctions[savedFunctions.GetLength() - 1] == func)
savedFunctions[savedFunctions.GetLength() - 1] = realFunc;
found = true;
dontTranslate.Insert(realFunc, true);
break;
}
}
if (!found) {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
if (isNew) {
// Destroy the function without releasing any references
func->id = 0;
func->scriptData->byteCode.SetLength(0);
func->ReleaseInternal();
}
} else {
ot->beh.constructors.PushLast(func->id);
func->AddRefInternal();
if (func->parameterTypes.GetLength() == 0)
ot->beh.construct = func->id;
}
} else {
Error(TXT_INVALID_BYTECODE_d);
}
func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
if (func) {
if (sharedExists) {
// Find the real function in the object, and update the savedFunctions array
bool found = false;
for (asUINT f = 0; f < ot->beh.factories.GetLength(); f++) {
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.factories[f]);
if (realFunc->IsSignatureEqual(func)) {
// If the function is not the last, then the substitution has already occurred before
if (savedFunctions[savedFunctions.GetLength() - 1] == func)
savedFunctions[savedFunctions.GetLength() - 1] = realFunc;
found = true;
dontTranslate.Insert(realFunc, true);
break;
}
}
if (!found) {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
if (isNew) {
// Destroy the function without releasing any references
func->id = 0;
func->scriptData->byteCode.SetLength(0);
func->ReleaseInternal();
}
} else {
ot->beh.factories.PushLast(func->id);
func->AddRefInternal();
if (func->parameterTypes.GetLength() == 0)
ot->beh.factory = func->id;
}
} else {
Error(TXT_INVALID_BYTECODE_d);
}
}
}
// methods[]
size = SanityCheck(ReadEncodedUInt(), 1000000);
int n;
for (n = 0; n < size; n++) {
bool isNew;
asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
if (func) {
if (sharedExists) {
// Find the real function in the object, and update the savedFunctions array
bool found = false;
for (asUINT f = 0; f < ot->methods.GetLength(); f++) {
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->methods[f]);
if (realFunc->IsSignatureEqual(func)) {
// If the function is not the last, then the substitution has already occurred before
if (savedFunctions[savedFunctions.GetLength() - 1] == func)
savedFunctions[savedFunctions.GetLength() - 1] = realFunc;
found = true;
dontTranslate.Insert(realFunc, true);
break;
}
}
if (!found) {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
if (isNew) {
// Destroy the function without releasing any references
if (func->id == func->signatureId)
engine->signatureIds.RemoveValue(func);
func->id = 0;
if (func->scriptData)
func->scriptData->byteCode.SetLength(0);
func->ReleaseInternal();
}
} else {
// If the method is the assignment operator we need to replace the default implementation
if (func->name == "opAssign" && func->parameterTypes.GetLength() == 1 &&
func->parameterTypes[0].GetTypeInfo() == func->objectType &&
(func->inOutFlags[0] & asTM_INREF)) {
engine->scriptFunctions[ot->beh.copy]->ReleaseInternal();
ot->beh.copy = func->id;
func->AddRefInternal();
}
ot->methods.PushLast(func->id);
func->AddRefInternal();
}
} else {
Error(TXT_INVALID_BYTECODE_d);
}
}
// virtualFunctionTable[]
size = SanityCheck(ReadEncodedUInt(), 1000000);
for (n = 0; n < size; n++) {
bool isNew;
asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
if (func) {
if (sharedExists) {
// Find the real function in the object, and update the savedFunctions array
bool found = false;
for (asUINT f = 0; f < ot->virtualFunctionTable.GetLength(); f++) {
asCScriptFunction *realFunc = ot->virtualFunctionTable[f];
if (realFunc->IsSignatureEqual(func)) {
// If the function is not the last, then the substitution has already occurred before
if (savedFunctions[savedFunctions.GetLength() - 1] == func)
savedFunctions[savedFunctions.GetLength() - 1] = realFunc;
found = true;
dontTranslate.Insert(realFunc, true);
break;
}
}
if (!found) {
asCString str;
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
}
if (isNew) {
// Destroy the function without releasing any references
func->id = 0;
if (func->scriptData)
func->scriptData->byteCode.SetLength(0);
func->ReleaseInternal();
}
} else {
ot->virtualFunctionTable.PushLast(func);
func->AddRefInternal();
}
} else {
Error(TXT_INVALID_BYTECODE_d);
}
}
}
} else if (phase == 3) {
// external shared types doesn't store this
if ((type->flags & asOBJ_SHARED) && module->m_externalTypes.IndexOf(type) >= 0)
return;
asCObjectType *ot = CastToObjectType(type);
// This is only done for object types
asASSERT(ot);
// properties[]
asUINT size = SanityCheck(ReadEncodedUInt(), 1000000);
for (asUINT n = 0; n < size; n++)
ReadObjectProperty(ot);
}
}
asWORD asCReader::ReadEncodedUInt16() {
asDWORD dw = ReadEncodedUInt();
if ((dw >> 16) != 0 && (dw >> 16) != 0xFFFF) {
Error(TXT_INVALID_BYTECODE_d);
}
return asWORD(dw & 0xFFFF);
}
asUINT asCReader::ReadEncodedUInt() {
asQWORD qw = ReadEncodedUInt64();
if ((qw >> 32) != 0 && (qw >> 32) != 0xFFFFFFFF) {
Error(TXT_INVALID_BYTECODE_d);
}
return asUINT(qw & 0xFFFFFFFFu);
}
int asCReader::ReadEncodedInt() {
return int(ReadEncodedUInt());
}
asQWORD asCReader::ReadEncodedUInt64() {
asQWORD i = 0;
asBYTE b = 0xFF; // set to 0xFF to better catch if the stream doesn't update the value
ReadData(&b, 1);
bool isNegative = (b & 0x80) ? true : false;
b &= 0x7F;
if ((b & 0x7F) == 0x7F) {
ReadData(&b, 1);
i = asQWORD(b) << 56;
ReadData(&b, 1);
i += asQWORD(b) << 48;
ReadData(&b, 1);
i += asQWORD(b) << 40;
ReadData(&b, 1);
i += asQWORD(b) << 32;
ReadData(&b, 1);
i += asUINT(b) << 24;
ReadData(&b, 1);
i += asUINT(b) << 16;
ReadData(&b, 1);
i += asUINT(b) << 8;
ReadData(&b, 1);
i += b;
} else if ((b & 0x7E) == 0x7E) {
i = asQWORD(b & 0x01) << 48;
ReadData(&b, 1);
i += asQWORD(b) << 40;
ReadData(&b, 1);
i += asQWORD(b) << 32;
ReadData(&b, 1);
i += asUINT(b) << 24;
ReadData(&b, 1);
i += asUINT(b) << 16;
ReadData(&b, 1);
i += asUINT(b) << 8;
ReadData(&b, 1);
i += b;
} else if ((b & 0x7C) == 0x7C) {
i = asQWORD(b & 0x03) << 40;
ReadData(&b, 1);
i += asQWORD(b) << 32;
ReadData(&b, 1);
i += asUINT(b) << 24;
ReadData(&b, 1);
i += asUINT(b) << 16;
ReadData(&b, 1);
i += asUINT(b) << 8;
ReadData(&b, 1);
i += b;
} else if ((b & 0x78) == 0x78) {
i = asQWORD(b & 0x07) << 32;
ReadData(&b, 1);
i += asUINT(b) << 24;
ReadData(&b, 1);
i += asUINT(b) << 16;
ReadData(&b, 1);
i += asUINT(b) << 8;
ReadData(&b, 1);
i += b;
} else if ((b & 0x70) == 0x70) {
i = asUINT(b & 0x0F) << 24;
ReadData(&b, 1);
i += asUINT(b) << 16;
ReadData(&b, 1);
i += asUINT(b) << 8;
ReadData(&b, 1);
i += b;
} else if ((b & 0x60) == 0x60) {
i = asUINT(b & 0x1F) << 16;
ReadData(&b, 1);
i += asUINT(b) << 8;
ReadData(&b, 1);
i += b;
} else if ((b & 0x40) == 0x40) {
i = asUINT(b & 0x3F) << 8;
ReadData(&b, 1);
i += b;
} else {
i = b;
}
if (isNegative)
i = (asQWORD)(-asINT64(i));
return i;
}
asUINT asCReader::SanityCheck(asUINT val, asUINT max) {
if (val > max) {
Error(TXT_INVALID_BYTECODE_d);
// Return 0 as default value
return 0;
}
return val;
}
int asCReader::SanityCheck(int val, asUINT max) {
if (val > int(max) || val < -int(max)) {
Error(TXT_INVALID_BYTECODE_d);
// Return 0 as default value
return 0;
}
return val;
}
void asCReader::ReadString(asCString *str) {
asUINT len = SanityCheck(ReadEncodedUInt(), 1000000);
if (len & 1) {
asUINT idx = len / 2;
if (idx < savedStrings.GetLength())
*str = savedStrings[idx];
else
Error(TXT_INVALID_BYTECODE_d);
} else if (len > 0) {
len /= 2;
str->SetLength(len);
int r = stream->Read(str->AddressOf(), len);
if (r < 0)
Error(TXT_UNEXPECTED_END_OF_FILE);
savedStrings.PushLast(*str);
} else
str->SetLength(0);
}
void asCReader::ReadGlobalProperty() {
asCString name;
asCDataType type;
ReadString(&name);
asCString ns;
ReadString(&ns);
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
ReadDataType(&type);
asCGlobalProperty *prop = module->AllocateGlobalProperty(name.AddressOf(), type, nameSpace);
// Read the initialization function
bool isNew;
// Do not add the function to the GC at this time. It will
// only be added to the GC when the module releases the property
asCScriptFunction *func = ReadFunction(isNew, false, true, false);
if (func) {
// Make sure the function knows it is owned by the module
func->module = module;
prop->SetInitFunc(func);
func->ReleaseInternal();
}
}
void asCReader::ReadObjectProperty(asCObjectType *ot) {
asCString name;
ReadString(&name);
asCDataType dt;
ReadDataType(&dt);
int flags = ReadEncodedUInt();
bool isPrivate = (flags & 1) ? true : false;
bool isProtected = (flags & 2) ? true : false;
bool isInherited = (flags & 4) ? true : false;
// TODO: shared: If the type is shared and pre-existing, we should just
// validate that the loaded methods match the original
if (!existingShared.MoveTo(0, ot))
ot->AddPropertyToClass(name, dt, isPrivate, isProtected, isInherited);
}
void asCReader::ReadDataType(asCDataType *dt) {
// Check if this is a previously used type
asUINT idx = ReadEncodedUInt();
if (idx != 0) {
// Get the datatype from the cache
*dt = savedDataTypes[idx - 1];
return;
}
// Read the type definition
eTokenType tokenType = (eTokenType)ReadEncodedUInt();
// Reserve a spot in the savedDataTypes
asUINT saveSlot = savedDataTypes.GetLength();
savedDataTypes.PushLast(asCDataType());
// Read the datatype for the first time
asCTypeInfo *ti = 0;
if (tokenType == ttIdentifier)
ti = ReadTypeInfo();
// Read type flags as a bitmask
// Endian-safe code
bool isObjectHandle, isHandleToConst, isReference, isReadOnly;
char b = 0;
ReadData(&b, 1);
LOAD_FROM_BIT(isObjectHandle, b, 0);
LOAD_FROM_BIT(isHandleToConst, b, 1);
LOAD_FROM_BIT(isReference, b, 2);
LOAD_FROM_BIT(isReadOnly, b, 3);
if (tokenType == ttIdentifier)
*dt = asCDataType::CreateType(ti, false);
else
*dt = asCDataType::CreatePrimitive(tokenType, false);
if (isObjectHandle) {
dt->MakeReadOnly(isHandleToConst ? true : false);
// Here we must allow a scoped type to be a handle
// e.g. if the datatype is for a system function
dt->MakeHandle(true, true);
}
dt->MakeReadOnly(isReadOnly ? true : false);
dt->MakeReference(isReference ? true : false);
// Update the previously saved slot
savedDataTypes[saveSlot] = *dt;
}
asCTypeInfo *asCReader::ReadTypeInfo() {
asCTypeInfo *ot = 0;
char ch;
ReadData(&ch, 1);
if (ch == 'a') {
// Read the name of the template type
asCString typeName, ns;
ReadString(&typeName);
ReadString(&ns);
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
asCTypeInfo *tmp = engine->GetRegisteredType(typeName.AddressOf(), nameSpace);
asCObjectType *tmpl = CastToObjectType(tmp);
if (tmpl == 0) {
asCString str;
str.Format(TXT_TEMPLATE_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
asUINT numSubTypes = SanityCheck(ReadEncodedUInt(), 100);
asCArray<asCDataType> subTypes;
for (asUINT n = 0; n < numSubTypes; n++) {
ReadData(&ch, 1);
if (ch == 's') {
asCDataType dt;
ReadDataType(&dt);
subTypes.PushLast(dt);
} else {
eTokenType tokenType = (eTokenType)ReadEncodedUInt();
asCDataType dt = asCDataType::CreatePrimitive(tokenType, false);
subTypes.PushLast(dt);
}
}
// Return the actual template if the subtypes are the template's dummy types
if (tmpl->templateSubTypes == subTypes)
ot = tmpl;
else {
// Get the template instance type based on the loaded subtypes
ot = engine->GetTemplateInstanceType(tmpl, subTypes, module);
}
if (ot == 0) {
// Show all subtypes in error message
asCString sub = subTypes[0].Format(nameSpace);
for (asUINT n = 1; n < subTypes.GetLength(); n++) {
sub += ",";
sub += subTypes[n].Format(nameSpace);
}
asCString str;
str.Format(TXT_INSTANCING_INVLD_TMPL_TYPE_s_s, typeName.AddressOf(), sub.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
} else if (ch == 'l') {
asCObjectType *st = CastToObjectType(ReadTypeInfo());
if (st == 0 || st->beh.listFactory == 0) {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
ot = engine->GetListPatternType(st->beh.listFactory);
} else if (ch == 's') {
// Read the name of the template subtype
asCString typeName;
ReadString(&typeName);
// Find the template subtype
ot = 0;
for (asUINT n = 0; n < engine->templateSubTypes.GetLength(); n++) {
if (engine->templateSubTypes[n] && engine->templateSubTypes[n]->name == typeName) {
ot = engine->templateSubTypes[n];
break;
}
}
if (ot == 0) {
asCString str;
str.Format(TXT_TEMPLATE_SUBTYPE_s_DOESNT_EXIST, typeName.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
} else if (ch == 'o') {
// Read the object type name
asCString typeName, ns;
ReadString(&typeName);
ReadString(&ns);
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
if (typeName.GetLength() && typeName != "$obj" && typeName != "$func") {
// Find the object type
ot = module->GetType(typeName.AddressOf(), nameSpace);
if (!ot)
ot = engine->GetRegisteredType(typeName.AddressOf(), nameSpace);
if (ot == 0) {
asCString str;
str.Format(TXT_OBJECT_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
} else if (typeName == "$obj") {
ot = &engine->scriptTypeBehaviours;
} else if (typeName == "$func") {
ot = &engine->functionBehaviours;
} else
asASSERT(false);
} else if (ch == 'c') {
// Read the object type name
asCString typeName, ns;
ReadString(&typeName);
// Read the parent class
asCObjectType *parentClass = CastToObjectType(ReadTypeInfo());
if (parentClass == 0) {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
// Find the child type in the parentClass
for (asUINT n = 0; n < parentClass->childFuncDefs.GetLength(); n++) {
if (parentClass->childFuncDefs[n]->name == typeName)
ot = parentClass->childFuncDefs[n];
}
if (ot == 0) {
asCString str;
str.Format(TXT_OBJECT_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
} else {
// No object type
asASSERT(ch == '\0' || error);
ot = 0;
}
return ot;
}
void asCReader::ReadByteCode(asCScriptFunction *func) {
asASSERT(func->scriptData);
// Read number of instructions
asUINT total, numInstructions;
total = numInstructions = SanityCheck(ReadEncodedUInt(), 1000000);
// Reserve some space for the instructions
func->scriptData->byteCode.AllocateNoConstruct(numInstructions, false);
asUINT pos = 0;
while (numInstructions) {
asBYTE b;
ReadData(&b, 1);
// Allocate the space for the instruction
asUINT len = asBCTypeSize[asBCInfo[b].type];
asUINT newSize = asUINT(func->scriptData->byteCode.GetLength()) + len;
if (func->scriptData->byteCode.GetCapacity() < newSize) {
// Determine the average size of the loaded instructions and re-estimate the final size
asUINT size = asUINT(float(newSize) / (total - numInstructions) * total) + 1;
func->scriptData->byteCode.AllocateNoConstruct(size, true);
}
if (!func->scriptData->byteCode.SetLengthNoConstruct(newSize)) {
// Out of memory
error = true;
return;
}
asDWORD *bc = func->scriptData->byteCode.AddressOf() + pos;
pos += len;
switch (asBCInfo[b].type) {
case asBCTYPE_NO_ARG: {
*(asBYTE *)(bc) = b;
bc++;
}
break;
case asBCTYPE_W_ARG:
case asBCTYPE_wW_ARG:
case asBCTYPE_rW_ARG: {
*(asBYTE *)(bc) = b;
// Read the argument
asWORD w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
}
break;
case asBCTYPE_rW_DW_ARG:
case asBCTYPE_wW_DW_ARG:
case asBCTYPE_W_DW_ARG: {
*(asBYTE *)(bc) = b;
// Read the word argument
asWORD w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
// Read the dword argument
*bc++ = ReadEncodedUInt();
}
break;
case asBCTYPE_DW_ARG: {
*(asBYTE *)(bc) = b;
bc++;
// Read the argument
*bc++ = ReadEncodedUInt();
}
break;
case asBCTYPE_DW_DW_ARG: {
*(asBYTE *)(bc) = b;
bc++;
// Read the first argument
*bc++ = ReadEncodedUInt();
// Read the second argument
*bc++ = ReadEncodedUInt();
}
break;
case asBCTYPE_wW_rW_rW_ARG: {
*(asBYTE *)(bc) = b;
// Read the first argument
asWORD w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
// Read the second argument
w = ReadEncodedUInt16();
*(asWORD *)bc = w;
// Read the third argument
w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
}
break;
case asBCTYPE_wW_rW_ARG:
case asBCTYPE_rW_rW_ARG:
case asBCTYPE_wW_W_ARG: {
*(asBYTE *)(bc) = b;
// Read the first argument
asWORD w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
// Read the second argument
w = ReadEncodedUInt16();
*(asWORD *)bc = w;
bc++;
}
break;
case asBCTYPE_wW_rW_DW_ARG:
case asBCTYPE_rW_W_DW_ARG: {
*(asBYTE *)(bc) = b;
// Read the first argument
asWORD w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
// Read the second argument
w = ReadEncodedUInt16();
*(asWORD *)bc = w;
bc++;
// Read the third argument
asDWORD dw = ReadEncodedUInt();
*bc++ = dw;
}
break;
case asBCTYPE_QW_ARG: {
*(asBYTE *)(bc) = b;
bc++;
// Read the argument
asQWORD qw = ReadEncodedUInt64();
*(asQWORD *)bc = qw;
bc += 2;
}
break;
case asBCTYPE_QW_DW_ARG: {
*(asBYTE *)(bc) = b;
bc++;
// Read the first argument
asQWORD qw = ReadEncodedUInt64();
*(asQWORD *)bc = qw;
bc += 2;
// Read the second argument
asDWORD dw = ReadEncodedUInt();
*bc++ = dw;
}
break;
case asBCTYPE_rW_QW_ARG:
case asBCTYPE_wW_QW_ARG: {
*(asBYTE *)(bc) = b;
// Read the first argument
asWORD w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
// Read the argument
asQWORD qw = ReadEncodedUInt64();
*(asQWORD *)bc = qw;
bc += 2;
}
break;
case asBCTYPE_rW_DW_DW_ARG: {
*(asBYTE *)(bc) = b;
// Read the 1st argument
asWORD w = ReadEncodedUInt16();
*(((asWORD *)bc) + 1) = w;
bc++;
// Read the 2nd argument
*bc++ = ReadEncodedUInt();
// Read the 3rd argument
*bc++ = ReadEncodedUInt();
}
break;
default: {
// This should never happen
asASSERT(false);
// Read the next 3 bytes
asDWORD c;
asBYTE t;
#if defined(AS_BIG_ENDIAN)
c = b << 24;
ReadData(&t, 1);
c += t << 16;
ReadData(&t, 1);
c += t << 8;
ReadData(&t, 1);
c += t;
#else
c = b;
ReadData(&t, 1);
c += t << 8;
ReadData(&t, 1);
c += t << 16;
ReadData(&t, 1);
c += t << 24;
#endif
*bc++ = c;
c = *(asBYTE *)&c;
// Read the bc as is
for (int n = 1; n < asBCTypeSize[asBCInfo[c].type]; n++)
ReadData(&*bc++, 4);
}
}
numInstructions--;
}
// Correct the final size in case we over-estimated it
func->scriptData->byteCode.SetLengthNoConstruct(pos);
}
void asCReader::ReadUsedTypeIds() {
TimeIt("asCReader::ReadUsedTypeIds");
asUINT count = SanityCheck(ReadEncodedUInt(), 1000000);
usedTypeIds.Allocate(count, false);
for (asUINT n = 0; n < count; n++) {
asCDataType dt;
ReadDataType(&dt);
usedTypeIds.PushLast(engine->GetTypeIdFromDataType(dt));
}
}
void asCReader::ReadUsedGlobalProps() {
TimeIt("asCReader::ReadUsedGlobalProps");
int c = SanityCheck(ReadEncodedUInt(), 1000000);
usedGlobalProperties.Allocate(c, false);
for (int n = 0; n < c; n++) {
asCString name, ns;
asCDataType type;
char moduleProp;
ReadString(&name);
ReadString(&ns);
ReadDataType(&type);
ReadData(&moduleProp, 1);
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
// Find the real property
asCGlobalProperty *globProp = 0;
if (moduleProp)
globProp = module->m_scriptGlobals.GetFirst(nameSpace, name);
else
globProp = engine->registeredGlobalProps.GetFirst(nameSpace, name);
void *prop = 0;
if (globProp && globProp->type == type)
prop = globProp->GetAddressOfValue();
usedGlobalProperties.PushLast(prop);
if (prop == 0) {
Error(TXT_INVALID_BYTECODE_d);
}
}
}
void asCReader::ReadUsedObjectProps() {
TimeIt("asCReader::ReadUsedObjectProps");
asUINT c = SanityCheck(ReadEncodedUInt(), 1000000);
usedObjectProperties.SetLength(c);
for (asUINT n = 0; n < c; n++) {
asCObjectType *objType = CastToObjectType(ReadTypeInfo());
if (objType == 0) {
Error(TXT_INVALID_BYTECODE_d);
break;
}
asCString name;
ReadString(&name);
// Find the property
bool found = false;
for (asUINT p = 0; p < objType->properties.GetLength(); p++) {
if (objType->properties[p]->name == name) {
usedObjectProperties[n].objType = objType;
usedObjectProperties[n].prop = objType->properties[p];
found = true;
break;
}
}
if (!found) {
Error(TXT_INVALID_BYTECODE_d);
return;
}
}
}
short asCReader::FindObjectPropOffset(asWORD index) {
static asCObjectProperty *lastCompositeProp = 0;
if (lastCompositeProp) {
if (index != 0) {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
short offset = (short)lastCompositeProp->byteOffset;
lastCompositeProp = 0;
return offset;
}
if (index >= usedObjectProperties.GetLength()) {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
if (usedObjectProperties[index].prop->compositeOffset || usedObjectProperties[index].prop->isCompositeIndirect) {
lastCompositeProp = usedObjectProperties[index].prop;
return (short)lastCompositeProp->compositeOffset;
}
return (short)usedObjectProperties[index].prop->byteOffset;
}
asCScriptFunction *asCReader::FindFunction(int idx) {
if (idx >= 0 && idx < (int)usedFunctions.GetLength())
return usedFunctions[idx];
else {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
}
void asCReader::TranslateFunction(asCScriptFunction *func) {
// Skip this if the function is part of an pre-existing shared object
if (dontTranslate.MoveTo(0, func)) return;
asASSERT(func->scriptData);
// Pre-compute the size of each instruction in order to translate jump offsets
asUINT n;
asDWORD *bc = func->scriptData->byteCode.AddressOf();
asUINT bcLength = (asUINT)func->scriptData->byteCode.GetLength();
asCArray<asUINT> bcSizes(bcLength);
asCArray<asUINT> instructionNbrToPos(bcLength);
for (n = 0; n < bcLength;) {
int c = *(asBYTE *)&bc[n];
asUINT size = asBCTypeSize[asBCInfo[c].type];
if (size == 0) {
Error(TXT_INVALID_BYTECODE_d);
return;
}
bcSizes.PushLast(size);
instructionNbrToPos.PushLast(n);
n += size;
}
asUINT bcNum = 0;
for (n = 0; n < bcLength; bcNum++) {
int c = *(asBYTE *)&bc[n];
if (c == asBC_REFCPY ||
c == asBC_RefCpyV ||
c == asBC_OBJTYPE) {
// Translate the index to the true object type
asPWORD *ot = (asPWORD *)&bc[n + 1];
*(asCObjectType **)ot = CastToObjectType(FindType(int(*ot)));
} else if (c == asBC_TYPEID ||
c == asBC_Cast) {
// Translate the index to the type id
int *tid = (int *)&bc[n + 1];
*tid = FindTypeId(*tid);
} else if (c == asBC_ADDSi ||
c == asBC_LoadThisR) {
// Translate the index to the type id
int *tid = (int *)&bc[n + 1];
*tid = FindTypeId(*tid);
// Translate the prop index into the property offset
*(((short *)&bc[n]) + 1) = FindObjectPropOffset(*(((short *)&bc[n]) + 1));
} else if (c == asBC_LoadRObjR ||
c == asBC_LoadVObjR) {
// Translate the index to the type id
int *tid = (int *)&bc[n + 2];
*tid = FindTypeId(*tid);
asCObjectType *ot = engine->GetObjectTypeFromTypeId(*tid);
if (ot && (ot->flags & asOBJ_LIST_PATTERN)) {
// List patterns have a different way of adjusting the offsets
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
*(((short *)&bc[n]) + 2) = (short)listAdj->AdjustOffset(*(((short *)&bc[n]) + 2));
} else {
// Translate the prop index into the property offset
*(((short *)&bc[n]) + 2) = FindObjectPropOffset(*(((short *)&bc[n]) + 2));
}
} else if (c == asBC_COPY) {
// Translate the index to the type id
int *tid = (int *)&bc[n + 1];
*tid = FindTypeId(*tid);
// COPY is used to copy POD types that don't have the opAssign method. It is
// also used to copy references to scoped types during variable initializations.
// Update the number of dwords to copy as it may be different on the target platform
if ((*tid) & asTYPEID_OBJHANDLE) {
// It is the actual reference that is being copied, not the object itself
asBC_SWORDARG0(&bc[n]) = AS_PTR_SIZE;
} else {
asCDataType dt = engine->GetDataTypeFromTypeId(*tid);
if (!dt.IsValid()) {
Error(TXT_INVALID_BYTECODE_d);
} else
asBC_SWORDARG0(&bc[n]) = (short)dt.GetSizeInMemoryDWords();
}
} else if (c == asBC_RET) {
// Determine the correct amount of DWORDs to pop
asWORD dw = (asWORD)func->GetSpaceNeededForArguments();
if (func->DoesReturnOnStack()) dw += AS_PTR_SIZE;
if (func->objectType) dw += AS_PTR_SIZE;
asBC_WORDARG0(&bc[n]) = dw;
} else if (c == asBC_CALL ||
c == asBC_CALLINTF ||
c == asBC_CALLSYS ||
c == asBC_Thiscall1) {
// Translate the index to the func id
int *fid = (int *)&bc[n + 1];
asCScriptFunction *f = FindFunction(*fid);
if (f)
*fid = f->id;
else {
Error(TXT_INVALID_BYTECODE_d);
return;
}
} else if (c == asBC_FuncPtr) {
// Translate the index to the func pointer
asPWORD *fid = (asPWORD *)&bc[n + 1];
*fid = (asPWORD)FindFunction(int(*fid));
} else if (c == asBC_ALLOC) {
// Translate the index to the true object type
asPWORD *arg = (asPWORD *)&bc[n + 1];
*(asCObjectType **)arg = CastToObjectType(FindType(int(*arg)));
// The constructor function id must be translated, unless it is zero
int *fid = (int *)&bc[n + 1 + AS_PTR_SIZE];
if (*fid != 0) {
// Subtract 1 from the id, as it was incremented during the writing
asCScriptFunction *f = FindFunction(*fid - 1);
if (f)
*fid = f->id;
else {
Error(TXT_INVALID_BYTECODE_d);
return;
}
}
} else if (c == asBC_STR) {
Error(TXT_INVALID_BYTECODE_d);
return;
} else if (c == asBC_CALLBND) {
// Translate the function id
asUINT *fid = (asUINT *)&bc[n + 1];
if (*fid < module->m_bindInformations.GetLength()) {
sBindInfo *bi = module->m_bindInformations[*fid];
if (bi)
*fid = bi->importedFunctionSignature->id;
else {
Error(TXT_INVALID_BYTECODE_d);
return;
}
} else {
Error(TXT_INVALID_BYTECODE_d);
return;
}
} else if (c == asBC_PGA ||
c == asBC_PshGPtr ||
c == asBC_LDG ||
c == asBC_PshG4 ||
c == asBC_LdGRdR4 ||
c == asBC_CpyGtoV4 ||
c == asBC_CpyVtoG4 ||
c == asBC_SetG4) {
// Translate the index to pointer
asPWORD *index = (asPWORD *)&bc[n + 1];
if ((*index & 1)) {
if ((asUINT(*index) >> 1) < usedGlobalProperties.GetLength())
*(void **)index = usedGlobalProperties[asUINT(*index) >> 1];
else {
Error(TXT_INVALID_BYTECODE_d);
return;
}
} else {
// Only PGA and PshGPtr can hold string constants
asASSERT(c == asBC_PGA || c == asBC_PshGPtr);
if ((asUINT(*index) >> 1) < usedStringConstants.GetLength())
*(void **)index = usedStringConstants[asUINT(*index) >> 1];
else {
Error(TXT_INVALID_BYTECODE_d);
return;
}
}
} else if (c == asBC_JMP ||
c == asBC_JZ ||
c == asBC_JNZ ||
c == asBC_JLowZ ||
c == asBC_JLowNZ ||
c == asBC_JS ||
c == asBC_JNS ||
c == asBC_JP ||
c == asBC_JNP) { // The JMPP instruction doesn't need modification
// Get the offset
int offset = int(bc[n + 1]);
// Count the instruction sizes to the destination instruction
int size = 0;
if (offset >= 0)
// If moving ahead, then start from next instruction
for (asUINT num = bcNum + 1; offset-- > 0; num++)
size += bcSizes[num];
else
// If moving backwards, then start at current instruction
for (asUINT num = bcNum; offset++ < 0; num--)
size -= bcSizes[num];
// The size is dword offset
bc[n + 1] = size;
} else if (c == asBC_AllocMem) {
// The size of the allocated memory is only known after all the elements has been seen.
// This helper class will collect this information and adjust the size when the
// corresponding asBC_FREE is encountered
// The adjuster also needs to know the list type so it can know the type of the elements
asCObjectType *ot = CastToObjectType(func->GetTypeInfoOfLocalVar(asBC_SWORDARG0(&bc[n])));
listAdjusters.PushLast(asNEW(SListAdjuster)(this, &bc[n], ot));
} else if (c == asBC_FREE) {
// Translate the index to the true object type
asPWORD *pot = (asPWORD *)&bc[n + 1];
*(asCObjectType **)pot = CastToObjectType(FindType(int(*pot)));
asCObjectType *ot = *(asCObjectType **)pot;
if (ot && (ot->flags & asOBJ_LIST_PATTERN)) {
if (listAdjusters.GetLength() == 0) {
Error(TXT_INVALID_BYTECODE_d);
return;
}
// Finalize the adjustment of the list buffer that was initiated with asBC_AllocMem
SListAdjuster *list = listAdjusters.PopLast();
list->AdjustAllocMem();
asDELETE(list, SListAdjuster);
}
} else if (c == asBC_SetListSize) {
// Adjust the offset in the list where the size is informed
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
bc[n + 1] = listAdj->AdjustOffset(bc[n + 1]);
// Inform the list adjuster how many values will be repeated
listAdj->SetRepeatCount(bc[n + 2]);
} else if (c == asBC_PshListElmnt) {
// Adjust the offset in the list where the size is informed
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
bc[n + 1] = listAdj->AdjustOffset(bc[n + 1]);
} else if (c == asBC_SetListType) {
// Adjust the offset in the list where the typeid is informed
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
bc[n + 1] = listAdj->AdjustOffset(bc[n + 1]);
// Translate the type id
bc[n + 2] = FindTypeId(bc[n + 2]);
// Inform the list adjuster the type id of the next element
listAdj->SetNextType(bc[n + 2]);
}
n += asBCTypeSize[asBCInfo[c].type];
}
// Calculate the stack adjustments
CalculateAdjustmentByPos(func);
// Adjust all variable positions in the bytecode
bc = func->scriptData->byteCode.AddressOf();
for (n = 0; n < bcLength;) {
int c = *(asBYTE *)&bc[n];
switch (asBCInfo[c].type) {
case asBCTYPE_wW_ARG:
case asBCTYPE_rW_DW_ARG:
case asBCTYPE_wW_QW_ARG:
case asBCTYPE_rW_ARG:
case asBCTYPE_wW_DW_ARG:
case asBCTYPE_wW_W_ARG:
case asBCTYPE_rW_QW_ARG:
case asBCTYPE_rW_W_DW_ARG:
case asBCTYPE_rW_DW_DW_ARG: {
asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
}
break;
case asBCTYPE_wW_rW_ARG:
case asBCTYPE_wW_rW_DW_ARG:
case asBCTYPE_rW_rW_ARG: {
asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
asBC_SWORDARG1(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG1(&bc[n]));
}
break;
case asBCTYPE_wW_rW_rW_ARG: {
asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
asBC_SWORDARG1(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG1(&bc[n]));
asBC_SWORDARG2(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG2(&bc[n]));
}
break;
default:
// The other types don't treat variables so won't be modified
break;
}
n += asBCTypeSize[asBCInfo[c].type];
}
// Adjust the space needed for local variables
func->scriptData->variableSpace = AdjustStackPosition(func->scriptData->variableSpace);
// Adjust the variable information. This will be used during the adjustment below
for (n = 0; n < func->scriptData->variables.GetLength(); n++) {
func->scriptData->variables[n]->declaredAtProgramPos = instructionNbrToPos[func->scriptData->variables[n]->declaredAtProgramPos];
func->scriptData->variables[n]->stackOffset = AdjustStackPosition(func->scriptData->variables[n]->stackOffset);
}
// objVariablePos
for (n = 0; n < func->scriptData->objVariablePos.GetLength(); n++)
func->scriptData->objVariablePos[n] = AdjustStackPosition(func->scriptData->objVariablePos[n]);
// Adjust the get offsets. This must be done in the second iteration because
// it relies on the function ids and variable position already being correct in the
// bytecodes that come after the GET instructions.
// TODO: optimize: Instead of doing a full extra loop. We can push the GET instructions
// on a stack, and then when a call instruction is found update all of them.
// This will also make the AdjustGetOffset() function quicker as it can
// receive the called function directly instead of having to search for it.
bc = func->scriptData->byteCode.AddressOf();
for (n = 0; n < bcLength;) {
int c = *(asBYTE *)&bc[n];
if (c == asBC_GETREF ||
c == asBC_GETOBJ ||
c == asBC_GETOBJREF ||
c == asBC_ChkNullS) {
asBC_WORDARG0(&bc[n]) = (asWORD)AdjustGetOffset(asBC_WORDARG0(&bc[n]), func, n);
}
n += asBCTypeSize[asBCInfo[c].type];
}
for (n = 0; n < func->scriptData->objVariableInfo.GetLength(); n++) {
// The program position must be adjusted as it is stored in number of instructions
func->scriptData->objVariableInfo[n].programPos = instructionNbrToPos[func->scriptData->objVariableInfo[n].programPos];
func->scriptData->objVariableInfo[n].variableOffset = AdjustStackPosition(func->scriptData->objVariableInfo[n].variableOffset);
}
for (n = 0; n < func->scriptData->tryCatchInfo.GetLength(); n++) {
func->scriptData->tryCatchInfo[n].tryPos = instructionNbrToPos[func->scriptData->tryCatchInfo[n].tryPos];
func->scriptData->tryCatchInfo[n].catchPos = instructionNbrToPos[func->scriptData->tryCatchInfo[n].catchPos];
}
// The program position (every even number) needs to be adjusted
// for the line numbers to be in number of dwords instead of number of instructions
for (n = 0; n < func->scriptData->lineNumbers.GetLength(); n += 2)
func->scriptData->lineNumbers[n] = instructionNbrToPos[func->scriptData->lineNumbers[n]];
for (n = 0; n < func->scriptData->sectionIdxs.GetLength(); n += 2)
func->scriptData->sectionIdxs[n] = instructionNbrToPos[func->scriptData->sectionIdxs[n]];
CalculateStackNeeded(func);
}
asCReader::SListAdjuster::SListAdjuster(asCReader *rd, asDWORD *bc, asCObjectType *listType) :
reader(rd), allocMemBC(bc), maxOffset(0), patternType(listType), repeatCount(0), lastOffset(-1), nextOffset(0), nextTypeId(-1) {
asASSERT(patternType && (patternType->flags & asOBJ_LIST_PATTERN));
// Find the first expected value in the list
asSListPatternNode *node = patternType->engine->scriptFunctions[patternType->templateSubTypes[0].GetBehaviour()->listFactory]->listPattern;
asASSERT(node && node->type == asLPT_START);
patternNode = node->next;
}
int asCReader::SListAdjuster::AdjustOffset(int offset) {
if (offset < lastOffset) {
reader->Error(TXT_INVALID_BYTECODE_d);
return 0;
}
// If it is the same offset being accessed again, just return the same adjusted value
if (lastOffset == offset)
return lastAdjustedOffset;
lastOffset = offset;
lastAdjustedOffset = maxOffset;
// What is being expected at this position?
if (patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME) {
// Align the offset to 4 bytes boundary
if (maxOffset & 0x3) {
maxOffset += 4 - (maxOffset & 0x3);
lastAdjustedOffset = maxOffset;
}
// Don't move the patternNode yet because the caller must make a call to SetRepeatCount too
maxOffset += 4;
nextOffset = offset + 1;
return lastAdjustedOffset;
} else if (patternNode->type == asLPT_TYPE) {
const asCDataType &dt = reinterpret_cast<asSListPatternDataTypeNode *>(patternNode)->dataType;
if (dt.GetTokenType() == ttQuestion) {
if (nextTypeId != -1) {
if (repeatCount > 0)
repeatCount--;
asCDataType nextdt = patternType->engine->GetDataTypeFromTypeId(nextTypeId);
asUINT size;
if (nextdt.IsObjectHandle() || (nextdt.GetTypeInfo() && (nextdt.GetTypeInfo()->flags & asOBJ_REF)))
size = AS_PTR_SIZE * 4;
else
size = nextdt.GetSizeInMemoryBytes();
// Align the offset to 4 bytes boundary
if (size >= 4 && (maxOffset & 0x3)) {
maxOffset += 4 - (maxOffset & 0x3);
lastAdjustedOffset = maxOffset;
}
// Only move the patternNode if we're not expecting any more repeated entries
if (repeatCount == 0)
patternNode = patternNode->next;
nextTypeId = -1;
maxOffset += size;
nextOffset = offset + 1;
return lastAdjustedOffset;
} else {
// Align the offset to 4 bytes boundary
if (maxOffset & 0x3) {
maxOffset += 4 - (maxOffset & 0x3);
lastAdjustedOffset = maxOffset;
}
// The first adjustment is for the typeId
maxOffset += 4;
nextOffset = offset + 1;
return lastAdjustedOffset;
}
} else {
// Determine the size of the element
asUINT size;
if (dt.IsObjectHandle() || (dt.GetTypeInfo() && (dt.GetTypeInfo()->flags & asOBJ_REF)))
size = AS_PTR_SIZE * 4;
else
size = dt.GetSizeInMemoryBytes();
// If values are skipped, the offset needs to be incremented
while (nextOffset <= offset) {
if (repeatCount > 0)
repeatCount--;
// Align the offset to 4 bytes boundary
if (size >= 4 && (maxOffset & 0x3))
maxOffset += 4 - (maxOffset & 0x3);
lastAdjustedOffset = maxOffset;
nextOffset += 1;
maxOffset += size;
}
// Only move the patternNode if we're not expecting any more repeated entries
if (repeatCount == 0)
patternNode = patternNode->next;
nextOffset = offset + 1;
return lastAdjustedOffset;
}
} else if (patternNode->type == asLPT_START) {
if (repeatCount > 0)
repeatCount--;
SInfo info = {repeatCount, patternNode};
stack.PushLast(info);
repeatCount = 0;
patternNode = patternNode->next;
lastOffset--;
return AdjustOffset(offset);
} else if (patternNode->type == asLPT_END) {
if (stack.GetLength() == 0) {
reader->Error(TXT_INVALID_BYTECODE_d);
return 0;
}
SInfo info = stack.PopLast();
repeatCount = info.repeatCount;
if (repeatCount)
patternNode = info.startNode;
else
patternNode = patternNode->next;
lastOffset--;
return AdjustOffset(offset);
} else {
// Something is wrong with the pattern list declaration
reader->Error(TXT_INVALID_BYTECODE_d);
return 0;
}
UNREACHABLE_RETURN;
}
void asCReader::SListAdjuster::SetRepeatCount(asUINT rc) {
// Make sure the list is expecting a repeat at this location
asASSERT(patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME);
// Now move to the next patternNode
patternNode = patternNode->next;
repeatCount = rc;
}
void asCReader::SListAdjuster::AdjustAllocMem() {
allocMemBC[1] = maxOffset;
}
void asCReader::SListAdjuster::SetNextType(int typeId) {
asASSERT(nextTypeId == -1);
nextTypeId = typeId;
}
void asCReader::CalculateStackNeeded(asCScriptFunction *func) {
asASSERT(func->scriptData);
int largestStackUsed = 0;
// Clear the known stack size for each bytecode
asCArray<int> stackSize;
stackSize.SetLength(func->scriptData->byteCode.GetLength());
memset(&stackSize[0], -1, stackSize.GetLength() * 4);
// Add the first instruction to the list of unchecked code
// paths and set the stack size at that instruction to variableSpace
asCArray<asUINT> paths;
paths.PushLast(0);
stackSize[0] = func->scriptData->variableSpace;
// Go through each of the code paths
for (asUINT p = 0; p < paths.GetLength(); ++p) {
asUINT pos = paths[p];
int currStackSize = stackSize[pos];
asBYTE bc = *(asBYTE *)&func->scriptData->byteCode[pos];
if (bc == asBC_RET)
continue;
// Determine the change in stack size for this instruction
int stackInc = asBCInfo[bc].stackInc;
if (stackInc == 0xFFFF) {
// Determine the true delta from the instruction arguments
if (bc == asBC_CALL ||
bc == asBC_CALLSYS ||
bc == asBC_Thiscall1 ||
bc == asBC_CALLBND ||
bc == asBC_ALLOC ||
bc == asBC_CALLINTF ||
bc == asBC_CallPtr) {
asCScriptFunction *called = GetCalledFunction(func, pos);
if (called) {
stackInc = -called->GetSpaceNeededForArguments();
if (called->objectType)
stackInc -= AS_PTR_SIZE;
if (called->DoesReturnOnStack())
stackInc -= AS_PTR_SIZE;
} else {
// It is an allocation for an object without a constructor
asASSERT(bc == asBC_ALLOC);
stackInc = -AS_PTR_SIZE;
}
}
}
currStackSize += stackInc;
asASSERT(currStackSize >= 0);
if (currStackSize > largestStackUsed)
largestStackUsed = currStackSize;
if (bc == asBC_JMP) {
// Find the label that we should jump to
int offset = asBC_INTARG(&func->scriptData->byteCode[pos]);
pos += 2 + offset;
// Add the destination as a new path
if (stackSize[pos] == -1) {
stackSize[pos] = currStackSize;
paths.PushLast(pos);
} else
asASSERT(stackSize[pos] == currStackSize);
continue;
} else if (bc == asBC_JZ || bc == asBC_JNZ ||
bc == asBC_JLowZ || bc == asBC_JLowNZ ||
bc == asBC_JS || bc == asBC_JNS ||
bc == asBC_JP || bc == asBC_JNP) {
// Find the label that is being jumped to
int offset = asBC_INTARG(&func->scriptData->byteCode[pos]);
// Add both paths to the code paths
pos += 2;
if (stackSize[pos] == -1) {
stackSize[pos] = currStackSize;
paths.PushLast(pos);
} else
asASSERT(stackSize[pos] == currStackSize);
pos += offset;
if (stackSize[pos] == -1) {
stackSize[pos] = currStackSize;
paths.PushLast(pos);
} else
asASSERT(stackSize[pos] == currStackSize);
continue;
} else if (bc == asBC_JMPP) {
pos++;
// Add all subsequent JMP instructions to the path
while (*(asBYTE *)&func->scriptData->byteCode[pos] == asBC_JMP) {
if (stackSize[pos] == -1) {
stackSize[pos] = currStackSize;
paths.PushLast(pos);
} else
asASSERT(stackSize[pos] == currStackSize);
pos += 2;
}
continue;
} else {
// Add next instruction to the paths
pos += asBCTypeSize[asBCInfo[bc].type];
if (stackSize[pos] == -1) {
stackSize[pos] = currStackSize;
paths.PushLast(pos);
} else
asASSERT(stackSize[pos] == currStackSize);
continue;
}
}
func->scriptData->stackNeeded = largestStackUsed;
}
void asCReader::CalculateAdjustmentByPos(asCScriptFunction *func) {
// Adjust the offset of all negative variables (parameters) as
// all pointers have been stored as having a size of 1 dword
asUINT n;
asCArray<int> adjustments;
asUINT offset = 0;
if (func->objectType) {
adjustments.PushLast(offset);
adjustments.PushLast(1 - AS_PTR_SIZE);
offset += 1;
}
if (func->DoesReturnOnStack()) {
adjustments.PushLast(offset);
adjustments.PushLast(1 - AS_PTR_SIZE);
offset += 1;
}
for (n = 0; n < func->parameterTypes.GetLength(); n++) {
if (!func->parameterTypes[n].IsPrimitive() ||
func->parameterTypes[n].IsReference()) {
adjustments.PushLast(offset);
adjustments.PushLast(1 - AS_PTR_SIZE);
offset += 1;
} else {
asASSERT(func->parameterTypes[n].IsPrimitive());
offset += func->parameterTypes[n].GetSizeOnStackDWords();
}
}
// Build look-up table with the adjustments for each stack position
adjustNegativeStackByPos.SetLength(offset);
memset(adjustNegativeStackByPos.AddressOf(), 0, adjustNegativeStackByPos.GetLength()*sizeof(int));
for (n = 0; n < adjustments.GetLength(); n += 2) {
int pos = adjustments[n];
int adjust = adjustments[n + 1];
for (asUINT i = pos + 1; i < adjustNegativeStackByPos.GetLength(); i++)
adjustNegativeStackByPos[i] += adjust;
}
// The bytecode has been stored as if all object variables take up only 1 dword.
// It is necessary to adjust to the size according to the current platform.
adjustments.SetLength(0);
int highestPos = 0;
for (n = 0; n < func->scriptData->objVariableTypes.GetLength(); n++) {
// Determine the size the variable currently occupies on the stack
int size = AS_PTR_SIZE;
// objVariableTypes is null if the type is a null pointer
if (func->scriptData->objVariableTypes[n] &&
(func->scriptData->objVariableTypes[n]->GetFlags() & asOBJ_VALUE) &&
n >= func->scriptData->objVariablesOnHeap) {
size = func->scriptData->objVariableTypes[n]->GetSize();
if (size < 4)
size = 1;
else
size /= 4;
}
// Check if type has a different size than stored
if (size > 1) {
if (func->scriptData->objVariablePos[n] > highestPos)
highestPos = func->scriptData->objVariablePos[n];
adjustments.PushLast(func->scriptData->objVariablePos[n]);
adjustments.PushLast(size - 1);
}
}
// Count position 0 too
adjustByPos.SetLength(highestPos + 1);
memset(adjustByPos.AddressOf(), 0, adjustByPos.GetLength()*sizeof(int));
// Build look-up table with the adjustments for each stack position
for (n = 0; n < adjustments.GetLength(); n += 2) {
int pos = adjustments[n];
int adjust = adjustments[n + 1];
for (asUINT i = pos; i < adjustByPos.GetLength(); i++)
adjustByPos[i] += adjust;
}
}
int asCReader::AdjustStackPosition(int pos) {
if (pos >= (int)adjustByPos.GetLength()) {
// It can be higher for primitives allocated on top of highest object variable
if (adjustByPos.GetLength())
pos += (short)adjustByPos[adjustByPos.GetLength() - 1];
} else if (pos >= 0)
pos += (short)adjustByPos[pos];
else if (-pos >= (int)adjustNegativeStackByPos.GetLength())
Error(TXT_INVALID_BYTECODE_d);
else
pos += (short)adjustNegativeStackByPos[-pos];
return pos;
}
asCScriptFunction *asCReader::GetCalledFunction(asCScriptFunction *func, asDWORD programPos) {
asBYTE bc = *(asBYTE *)&func->scriptData->byteCode[programPos];
if (bc == asBC_CALL ||
bc == asBC_CALLSYS ||
bc == asBC_Thiscall1 ||
bc == asBC_CALLINTF) {
// Find the function from the function id in bytecode
int funcId = asBC_INTARG(&func->scriptData->byteCode[programPos]);
return engine->scriptFunctions[funcId];
} else if (bc == asBC_ALLOC) {
// Find the function from the function id in the bytecode
int funcId = asBC_INTARG(&func->scriptData->byteCode[programPos + AS_PTR_SIZE]);
return engine->scriptFunctions[funcId];
} else if (bc == asBC_CALLBND) {
// Find the function from the engine's bind array
int funcId = asBC_INTARG(&func->scriptData->byteCode[programPos]);
return engine->importedFunctions[funcId & ~FUNC_IMPORTED]->importedFunctionSignature;
} else if (bc == asBC_CallPtr) {
asUINT v;
int var = asBC_SWORDARG0(&func->scriptData->byteCode[programPos]);
// Find the funcdef from the local variable
for (v = 0; v < func->scriptData->objVariablePos.GetLength(); v++)
if (func->scriptData->objVariablePos[v] == var)
return CastToFuncdefType(func->scriptData->objVariableTypes[v])->funcdef;
// Look in parameters
int paramPos = 0;
if (func->objectType)
paramPos -= AS_PTR_SIZE;
if (func->DoesReturnOnStack())
paramPos -= AS_PTR_SIZE;
for (v = 0; v < func->parameterTypes.GetLength(); v++) {
if (var == paramPos) {
if (func->parameterTypes[v].IsFuncdef())
return CastToFuncdefType(func->parameterTypes[v].GetTypeInfo())->funcdef;
else {
error = true;
return 0;
}
}
paramPos -= func->parameterTypes[v].GetSizeOnStackDWords();
}
}
return 0;
}
int asCReader::AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos) {
// TODO: optimize: multiple instructions for the same function doesn't need to look for the function everytime
// the function can remember where it found the function and check if the programPos is still valid
// Get offset 0 doesn't need adjustment
if (offset == 0) return 0;
bool bcAlloc = false;
// Find out which function that will be called
asCScriptFunction *calledFunc = 0;
int stackDelta = 0;
for (asUINT n = programPos; func->scriptData->byteCode.GetLength();) {
asBYTE bc = *(asBYTE *)&func->scriptData->byteCode[n];
if (bc == asBC_CALL ||
bc == asBC_CALLSYS ||
bc == asBC_Thiscall1 ||
bc == asBC_CALLINTF ||
bc == asBC_ALLOC ||
bc == asBC_CALLBND ||
bc == asBC_CallPtr) {
// The alloc instruction allocates the object memory
// so it doesn't take the this pointer as input
if (bc == asBC_ALLOC)
bcAlloc = true;
calledFunc = GetCalledFunction(func, n);
break;
} else if (bc == asBC_REFCPY ||
bc == asBC_COPY) {
// In this case we know there is only 1 pointer on the stack above
asASSERT(offset == 1);
return offset - (1 - AS_PTR_SIZE);
}
// Keep track of the stack size between the
// instruction that needs to be adjusted and the call
stackDelta += asBCInfo[bc].stackInc;
n += asBCTypeSize[asBCInfo[bc].type];
}
if (calledFunc == 0) {
Error(TXT_INVALID_BYTECODE_d);
return offset;
}
// Count the number of pointers pushed on the stack above the
// current offset, and then adjust the offset accordingly
asUINT numPtrs = 0;
int currOffset = -stackDelta;
if (offset > currOffset && calledFunc->GetObjectType() && !bcAlloc) {
currOffset++;
if (currOffset > 0)
numPtrs++;
#if AS_PTR_SIZE == 2
// For 64bit platforms it is necessary to increment the currOffset by one more
// DWORD since the stackDelta was counting the full 64bit size of the pointer
else if (stackDelta)
currOffset++;
#endif
}
if (offset > currOffset && calledFunc->DoesReturnOnStack()) {
currOffset++;
if (currOffset > 0)
numPtrs++;
#if AS_PTR_SIZE == 2
// For 64bit platforms it is necessary to increment the currOffset by one more
// DWORD since the stackDelta was counting the full 64bit size of the pointer
else if (stackDelta)
currOffset++;
#endif
}
for (asUINT p = 0; p < calledFunc->parameterTypes.GetLength(); p++) {
if (offset <= currOffset) break;
if (!calledFunc->parameterTypes[p].IsPrimitive() ||
calledFunc->parameterTypes[p].IsReference()) {
currOffset++;
if (currOffset > 0)
numPtrs++;
#if AS_PTR_SIZE == 2
// For 64bit platforms it is necessary to increment the currOffset by one more
// DWORD since the stackDelta was counting the full 64bit size of the pointer
else if (stackDelta)
currOffset++;
#endif
// The variable arg ? has an additiona 32bit integer with the typeid
if (calledFunc->parameterTypes[p].IsAnyType())
currOffset += 1;
} else {
// Enums or built-in primitives are passed by value
asASSERT(calledFunc->parameterTypes[p].IsPrimitive());
currOffset += calledFunc->parameterTypes[p].GetSizeOnStackDWords();
}
}
return offset - numPtrs * (1 - AS_PTR_SIZE);
}
int asCReader::FindTypeId(int idx) {
if (idx >= 0 && idx < (int)usedTypeIds.GetLength())
return usedTypeIds[idx];
else {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
}
asCTypeInfo *asCReader::FindType(int idx) {
if (idx < 0 || idx >= (int)usedTypes.GetLength()) {
Error(TXT_INVALID_BYTECODE_d);
return 0;
}
return usedTypes[idx];
}
#ifndef AS_NO_COMPILER
asCWriter::asCWriter(asCModule *_module, asIBinaryStream *_stream, asCScriptEngine *_engine, bool _stripDebug)
: module(_module), stream(_stream), engine(_engine), stripDebugInfo(_stripDebug), error(false), bytesWritten(0) {
}
int asCWriter::Error(const char *msg) {
// Don't write if it has already been reported an error earlier
if (!error) {
asCString str;
str.Format(msg, bytesWritten);
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
error = true;
}
return asERROR;
}
int asCWriter::WriteData(const void *data, asUINT size) {
asASSERT(size == 1 || size == 2 || size == 4 || size == 8);
int ret = 0;
#if defined(AS_BIG_ENDIAN)
for (asUINT n = 0; ret >= 0 && n < size; n++)
ret = stream->Write(((const asBYTE *)data) + n, 1);
#else
for (int n = size - 1; ret >= 0 && n >= 0; n--)
ret = stream->Write(((const asBYTE *)data) + n, 1);
#endif
if (ret < 0)
Error(TXT_UNEXPECTED_END_OF_FILE);
bytesWritten += size;
return ret;
}
int asCWriter::Write() {
TimeIt("asCWriter::Write");
unsigned long i, count;
// Store everything in the same order that the builder parses scripts
// TODO: Should be possible to skip saving the enum values. They are usually not needed after the script is compiled anyway
// TODO: Should be possible to skip saving the typedefs. They are usually not needed after the script is compiled anyway
// TODO: Should be possible to skip saving constants. They are usually not needed after the script is compiled anyway
// Write the flag as 1byte even on platforms with 4byte booleans
WriteEncodedInt64(stripDebugInfo ? 1 : 0);
// Store enums
{
TimeIt("store enums");
count = (asUINT)module->m_enumTypes.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; i++) {
WriteTypeDeclaration(module->m_enumTypes[i], 1);
WriteTypeDeclaration(module->m_enumTypes[i], 2);
}
}
// Store type declarations first
{
TimeIt("type declarations");
count = (asUINT)module->m_classTypes.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; i++) {
// Store only the name of the class/interface types
WriteTypeDeclaration(module->m_classTypes[i], 1);
}
}
// Store func defs
{
TimeIt("func defs");
count = (asUINT)module->m_funcDefs.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; i++)
WriteFunction(module->m_funcDefs[i]->funcdef);
}
// Now store all interface methods
{
TimeIt("interface methods");
count = (asUINT)module->m_classTypes.GetLength();
for (i = 0; i < count; i++) {
if (module->m_classTypes[i]->IsInterface())
WriteTypeDeclaration(module->m_classTypes[i], 2);
}
}
// Then store the class methods and behaviours
{
TimeIt("class methods and behaviours");
for (i = 0; i < count; ++i) {
if (!module->m_classTypes[i]->IsInterface())
WriteTypeDeclaration(module->m_classTypes[i], 2);
}
}
// Then store the class properties
{
TimeIt("class properties");
for (i = 0; i < count; ++i) {
if (!module->m_classTypes[i]->IsInterface())
WriteTypeDeclaration(module->m_classTypes[i], 3);
}
}
// Store typedefs
{
TimeIt("type defs");
count = (asUINT)module->m_typeDefs.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; i++) {
WriteTypeDeclaration(module->m_typeDefs[i], 1);
WriteTypeDeclaration(module->m_typeDefs[i], 2);
}
}
// scriptGlobals[]
{
TimeIt("script globals");
count = (asUINT)module->m_scriptGlobals.GetSize();
WriteEncodedInt64(count);
asCSymbolTable<asCGlobalProperty>::iterator it = module->m_scriptGlobals.List();
for (; it; it++)
WriteGlobalProperty(*it);
}
// scriptFunctions[]
{
TimeIt("scriptFunctions");
count = 0;
for (i = 0; i < module->m_scriptFunctions.GetLength(); i++)
if (module->m_scriptFunctions[i]->objectType == 0)
count++;
WriteEncodedInt64(count);
for (i = 0; i < module->m_scriptFunctions.GetLength(); ++i)
if (module->m_scriptFunctions[i]->objectType == 0)
WriteFunction(module->m_scriptFunctions[i]);
}
// globalFunctions[]
{
TimeIt("globalFunctions");
count = (int)module->m_globalFunctions.GetSize();
asCSymbolTable<asCScriptFunction>::iterator funcIt = module->m_globalFunctions.List();
WriteEncodedInt64(count);
while (funcIt) {
WriteFunction(*funcIt);
funcIt++;
}
}
// bindInformations[]
{
TimeIt("bindInformations");
count = (asUINT)module->m_bindInformations.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; ++i) {
WriteFunction(module->m_bindInformations[i]->importedFunctionSignature);
WriteString(&module->m_bindInformations[i]->importFromModule);
}
}
// usedTypes[]
{
TimeIt("usedTypes");
count = (asUINT)usedTypes.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; ++i)
WriteTypeInfo(usedTypes[i]);
}
// usedTypeIds[]
WriteUsedTypeIds();
// usedFunctions[]
WriteUsedFunctions();
// usedGlobalProperties[]
WriteUsedGlobalProps();
// usedStringConstants[]
WriteUsedStringConstants();
// usedObjectProperties[]
WriteUsedObjectProps();
return error ? asERROR : asSUCCESS;
}
int asCWriter::FindStringConstantIndex(void *str) {
asSMapNode<void *, int> *cursor = 0;
if (stringToIndexMap.MoveTo(&cursor, str))
return cursor->value;
usedStringConstants.PushLast(str);
int index = int(usedStringConstants.GetLength() - 1);
stringToIndexMap.Insert(str, index);
return index;
}
void asCWriter::WriteUsedStringConstants() {
TimeIt("asCWriter::WriteUsedStringConstants");
asUINT count = (asUINT)usedStringConstants.GetLength();
WriteEncodedInt64(count);
asCString str;
for (asUINT i = 0; i < count; ++i) {
asUINT length;
engine->stringFactory->GetRawStringData(usedStringConstants[i], 0, &length);
str.SetLength(length);
engine->stringFactory->GetRawStringData(usedStringConstants[i], str.AddressOf(), &length);
WriteString(&str);
}
}
void asCWriter::WriteUsedFunctions() {
TimeIt("asCWriter::WriteUsedFunctions");
asUINT count = (asUINT)usedFunctions.GetLength();
WriteEncodedInt64(count);
for (asUINT n = 0; n < usedFunctions.GetLength(); n++) {
char c;
// Write enough data to be able to uniquely identify the function upon load
asCScriptFunction *func = usedFunctions[n];
if (func) {
// Is the function from the module or the application?
c = func->module ? 'm' : 'a';
// Functions and methods that are shared should be stored as 's' as the bytecode
// may be imported from other modules (even if the current module have received ownership)
if (c == 'm' && func->IsShared())
c = 's';
WriteData(&c, 1);
WriteFunctionSignature(func);
} else {
// null function pointer
c = 'n';
WriteData(&c, 1);
}
}
}
void asCWriter::WriteFunctionSignature(asCScriptFunction *func) {
asUINT i, count;
WriteString(&func->name);
if (func->name == DELEGATE_FACTORY) {
// It's not necessary to write anything else
return;
}
WriteDataType(&func->returnType);
count = (asUINT)func->parameterTypes.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; ++i)
WriteDataType(&func->parameterTypes[i]);
// Only write the inout flags if any of them are set
// If the number of parameters is 0, then no need to save this
if (func->parameterTypes.GetLength() > 0) {
count = 0;
for (i = asUINT(func->inOutFlags.GetLength()); i > 0; i--)
if (func->inOutFlags[i - 1] != asTM_NONE) {
count = i;
break;
}
WriteEncodedInt64(count);
for (i = 0; i < count; ++i)
WriteEncodedInt64(func->inOutFlags[i]);
}
WriteEncodedInt64(func->funcType);
// Write the default args, from last to first
// If the number of parameters is 0, then no need to save this
if (func->parameterTypes.GetLength() > 0) {
count = 0;
for (i = (asUINT)func->defaultArgs.GetLength(); i-- > 0;)
if (func->defaultArgs[i])
count++;
WriteEncodedInt64(count);
for (i = (asUINT)func->defaultArgs.GetLength(); i-- > 0;)
if (func->defaultArgs[i])
WriteString(func->defaultArgs[i]);
}
WriteTypeInfo(func->objectType);
if (func->objectType) {
asBYTE b = 0;
b += func->IsReadOnly() ? 1 : 0;
b += func->IsPrivate() ? 2 : 0;
b += func->IsProtected() ? 4 : 0;
WriteData(&b, 1);
} else {
if (func->funcType == asFUNC_FUNCDEF) {
if (func->nameSpace) {
// This funcdef was declared as global entity
asBYTE b = 'n';
WriteData(&b, 1);
WriteString(&func->nameSpace->name);
} else {
// This funcdef was declared as class member
asBYTE b = 'o';
WriteData(&b, 1);
WriteTypeInfo(func->funcdefType->parentClass);
}
} else
WriteString(&func->nameSpace->name);
}
}
void asCWriter::WriteFunction(asCScriptFunction *func) {
char c;
// If there is no function, then store a null char
if (func == 0) {
c = '\0';
WriteData(&c, 1);
return;
}
// First check if the function has been saved already
for (asUINT f = 0; f < savedFunctions.GetLength(); f++) {
if (savedFunctions[f] == func) {
c = 'r';
WriteData(&c, 1);
WriteEncodedInt64(f);
return;
}
}
// Keep a reference to the function in the list
savedFunctions.PushLast(func);
c = 'f';
WriteData(&c, 1);
asUINT i, count;
WriteFunctionSignature(func);
if (func->funcType == asFUNC_SCRIPT) {
// Skip this for external shared entities
if (module->m_externalTypes.IndexOf(func->objectType) >= 0)
return;
char bits = 0;
bits += func->IsShared() ? 1 : 0;
bits += func->dontCleanUpOnException ? 2 : 0;
if (module->m_externalFunctions.IndexOf(func) >= 0)
bits += 4;
if (func->scriptData->objVariablePos.GetLength() || func->scriptData->objVariableInfo.GetLength())
bits += 8;
if (func->scriptData->tryCatchInfo.GetLength())
bits += 16;
bits += func->IsExplicit() ? 32 : 0;
WriteData(&bits, 1);
// For external shared functions the rest is not needed
if (bits & 4)
return;
// Calculate the adjustment by position lookup table
CalculateAdjustmentByPos(func);
WriteByteCode(func);
asDWORD varSpace = AdjustStackPosition(func->scriptData->variableSpace);
WriteEncodedInt64(varSpace);
if (bits & 8) {
count = (asUINT)func->scriptData->objVariablePos.GetLength();
WriteEncodedInt64(count);
for (i = 0; i < count; ++i) {
WriteTypeInfo(func->scriptData->objVariableTypes[i]);
WriteEncodedInt64(AdjustStackPosition(func->scriptData->objVariablePos[i]));
}
if (count > 0)
WriteEncodedInt64(func->scriptData->objVariablesOnHeap);
WriteEncodedInt64((asUINT)func->scriptData->objVariableInfo.GetLength());
for (i = 0; i < func->scriptData->objVariableInfo.GetLength(); ++i) {
// The program position must be adjusted to be in number of instructions
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->objVariableInfo[i].programPos]);
WriteEncodedInt64(AdjustStackPosition(func->scriptData->objVariableInfo[i].variableOffset));
WriteEncodedInt64(func->scriptData->objVariableInfo[i].option);
}
}
if (bits & 16) {
// Write info on try/catch blocks
WriteEncodedInt64((asUINT)func->scriptData->tryCatchInfo.GetLength());
for (i = 0; i < func->scriptData->tryCatchInfo.GetLength(); ++i) {
// The program position must be adjusted to be in number of instructions
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->tryCatchInfo[i].tryPos]);
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->tryCatchInfo[i].catchPos]);
}
}
// The program position (every even number) needs to be adjusted
// to be in number of instructions instead of DWORD offset
if (!stripDebugInfo) {
asUINT length = (asUINT)func->scriptData->lineNumbers.GetLength();
WriteEncodedInt64(length);
for (i = 0; i < length; ++i) {
if ((i & 1) == 0)
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->lineNumbers[i]]);
else
WriteEncodedInt64(func->scriptData->lineNumbers[i]);
}
// Write the array of script sections
length = (asUINT)func->scriptData->sectionIdxs.GetLength();
WriteEncodedInt64(length);
for (i = 0; i < length; ++i) {
if ((i & 1) == 0)
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->sectionIdxs[i]]);
else {
if (func->scriptData->sectionIdxs[i] >= 0)
WriteString(engine->scriptSectionNames[func->scriptData->sectionIdxs[i]]);
else {
c = 0;
WriteData(&c, 1);
}
}
}
}
// Write the variable information
if (!stripDebugInfo) {
WriteEncodedInt64((asUINT)func->scriptData->variables.GetLength());
for (i = 0; i < func->scriptData->variables.GetLength(); i++) {
// The program position must be adjusted to be in number of instructions
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->variables[i]->declaredAtProgramPos]);
// The stack position must be adjusted according to the pointer sizes
WriteEncodedInt64(AdjustStackPosition(func->scriptData->variables[i]->stackOffset));
WriteString(&func->scriptData->variables[i]->name);
WriteDataType(&func->scriptData->variables[i]->type);
}
}
// Store script section name
if (!stripDebugInfo) {
if (func->scriptData->scriptSectionIdx >= 0)
WriteString(engine->scriptSectionNames[func->scriptData->scriptSectionIdx]);
else {
c = 0;
WriteData(&c, 1);
}
WriteEncodedInt64(func->scriptData->declaredAt);
}
// Store the parameter names
if (!stripDebugInfo) {
count = asUINT(func->parameterNames.GetLength());
WriteEncodedInt64(count);
for (asUINT n = 0; n < count; n++)
WriteString(&func->parameterNames[n]);
}
} else if (func->funcType == asFUNC_VIRTUAL || func->funcType == asFUNC_INTERFACE) {
// TODO: Do we really need to store this? It can probably be reconstructed by the reader
WriteEncodedInt64(func->vfTableIdx);
} else if (func->funcType == asFUNC_FUNCDEF) {
char bits = 0;
bits += func->IsShared() ? 1 : 0;
if (module->m_externalTypes.IndexOf(func->funcdefType) >= 0)
bits += 2;
WriteData(&bits, 1);
}
}
void asCWriter::WriteTypeDeclaration(asCTypeInfo *type, int phase) {
if (phase == 1) {
// name
WriteString(&type->name);
// flags
WriteData(&type->flags, 4);
// size
// TODO: Do we really need to store this? The reader should be able to
// determine the correct size from the object type's flags
if ((type->flags & asOBJ_SCRIPT_OBJECT) && type->size > 0) {
// The size for script objects may vary from platform to platform so
// only store 1 to diferentiate from interfaces that have size 0.
WriteEncodedInt64(1);
} else {
// Enums, typedefs, and interfaces have fixed sizes independently
// of platform so it is safe to serialize the size directly.
WriteEncodedInt64(type->size);
}
// namespace
WriteString(&type->nameSpace->name);
// external shared flag
if ((type->flags & asOBJ_SHARED)) {
char c = ' ';
if (module->m_externalTypes.IndexOf(type) >= 0)
c = 'e';
WriteData(&c, 1);
}
} else if (phase == 2) {
// external shared types doesn't need to save this
if ((type->flags & asOBJ_SHARED) && module->m_externalTypes.IndexOf(type) >= 0)
return;
if (type->flags & asOBJ_ENUM) {
// enumValues[]
asCEnumType *t = CastToEnumType(type);
int size = (int)t->enumValues.GetLength();
WriteEncodedInt64(size);
for (int n = 0; n < size; n++) {
WriteString(&t->enumValues[n]->name);
WriteData(&t->enumValues[n]->value, 4);
}
} else if (type->flags & asOBJ_TYPEDEF) {
asCTypedefType *td = CastToTypedefType(type);
eTokenType t = td->aliasForType.GetTokenType();
WriteEncodedInt64(t);
} else {
asCObjectType *t = CastToObjectType(type);
WriteTypeInfo(t->derivedFrom);
// interfaces[] / interfaceVFTOffsets[]
// TOOD: Is it really necessary to store the VFTOffsets? Can't the reader calculate those?
int size = (asUINT)t->interfaces.GetLength();
WriteEncodedInt64(size);
asUINT n;
asASSERT(t->IsInterface() || t->interfaces.GetLength() == t->interfaceVFTOffsets.GetLength());
for (n = 0; n < t->interfaces.GetLength(); n++) {
WriteTypeInfo(t->interfaces[n]);
if (!t->IsInterface())
WriteEncodedInt64(t->interfaceVFTOffsets[n]);
}
// behaviours
// TODO: Default behaviours should just be stored as a indicator
// to avoid storing the actual function object
if (!t->IsInterface() && type->flags != asOBJ_TYPEDEF && type->flags != asOBJ_ENUM) {
WriteFunction(engine->scriptFunctions[t->beh.destruct]);
size = (int)t->beh.constructors.GetLength();
WriteEncodedInt64(size);
for (n = 0; n < t->beh.constructors.GetLength(); n++) {
WriteFunction(engine->scriptFunctions[t->beh.constructors[n]]);
WriteFunction(engine->scriptFunctions[t->beh.factories[n]]);
}
}
// methods[]
// TODO: Avoid storing inherited methods in interfaces, as the reader
// can add those directly from the base interface
size = (int)t->methods.GetLength();
WriteEncodedInt64(size);
for (n = 0; n < t->methods.GetLength(); n++) {
WriteFunction(engine->scriptFunctions[t->methods[n]]);
}
// virtualFunctionTable[]
// TODO: Is it really necessary to store this? Can't it be easily rebuilt by the reader
size = (int)t->virtualFunctionTable.GetLength();
WriteEncodedInt64(size);
for (n = 0; n < (asUINT)size; n++) {
WriteFunction(t->virtualFunctionTable[n]);
}
}
} else if (phase == 3) {
// external shared types doesn't need to save this
if ((type->flags & asOBJ_SHARED) && module->m_externalTypes.IndexOf(type) >= 0)
return;
// properties[]
asCObjectType *t = CastToObjectType(type);
// This is only done for object types
asASSERT(t);
asUINT size = (asUINT)t->properties.GetLength();
WriteEncodedInt64(size);
for (asUINT n = 0; n < t->properties.GetLength(); n++) {
WriteObjectProperty(t->properties[n]);
}
}
}
void asCWriter::WriteEncodedInt64(asINT64 i) {
asBYTE signBit = (i & asINT64(1) << 63) ? 0x80 : 0;
if (signBit) i = -i;
asBYTE b;
if (i < (1 << 6)) {
b = (asBYTE)(signBit + i);
WriteData(&b, 1);
} else if (i < (1 << 13)) {
b = asBYTE(0x40 + signBit + (i >> 8));
WriteData(&b, 1);
b = asBYTE(i & 0xFF);
WriteData(&b, 1);
} else if (i < (1 << 20)) {
b = asBYTE(0x60 + signBit + (i >> 16));
WriteData(&b, 1);
b = asBYTE((i >> 8) & 0xFF);
WriteData(&b, 1);
b = asBYTE(i & 0xFF);
WriteData(&b, 1);
} else if (i < (1 << 27)) {
b = asBYTE(0x70 + signBit + (i >> 24));
WriteData(&b, 1);
b = asBYTE((i >> 16) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 8) & 0xFF);
WriteData(&b, 1);
b = asBYTE(i & 0xFF);
WriteData(&b, 1);
} else if (i < (asINT64(1) << 34)) {
b = asBYTE(0x78 + signBit + (i >> 32));
WriteData(&b, 1);
b = asBYTE((i >> 24) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 16) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 8) & 0xFF);
WriteData(&b, 1);
b = asBYTE(i & 0xFF);
WriteData(&b, 1);
} else if (i < (asINT64(1) << 41)) {
b = asBYTE(0x7C + signBit + (i >> 40));
WriteData(&b, 1);
b = asBYTE((i >> 32) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 24) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 16) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 8) & 0xFF);
WriteData(&b, 1);
b = asBYTE(i & 0xFF);
WriteData(&b, 1);
} else if (i < (asINT64(1) << 48)) {
b = asBYTE(0x7E + signBit + (i >> 48));
WriteData(&b, 1);
b = asBYTE((i >> 40) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 32) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 24) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 16) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 8) & 0xFF);
WriteData(&b, 1);
b = asBYTE(i & 0xFF);
WriteData(&b, 1);
} else {
b = asBYTE(0x7F + signBit);
WriteData(&b, 1);
b = asBYTE((i >> 56) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 48) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 40) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 32) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 24) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 16) & 0xFF);
WriteData(&b, 1);
b = asBYTE((i >> 8) & 0xFF);
WriteData(&b, 1);
b = asBYTE(i & 0xFF);
WriteData(&b, 1);
}
}
void asCWriter::WriteString(asCString *str) {
// First check if the string hasn't been saved already
asSMapNode<asCString, int> *cursor = 0;
if (stringToIdMap.MoveTo(&cursor, *str)) {
// Save a reference to the existing string
// The lowest bit is set to 1 to indicate a reference
WriteEncodedInt64(cursor->value * 2 + 1);
return;
}
// Save a new string
// The lowest bit is set to 0 to indicate a new string
asUINT len = (asUINT)str->GetLength();
WriteEncodedInt64(len * 2);
if (len > 0) {
stream->Write(str->AddressOf(), (asUINT)len);
bytesWritten += len;
savedStrings.PushLast(*str);
stringToIdMap.Insert(*str, int(savedStrings.GetLength()) - 1);
}
}
void asCWriter::WriteGlobalProperty(asCGlobalProperty *prop) {
// TODO: We might be able to avoid storing the name and type of the global
// properties twice if we merge this with the WriteUsedGlobalProperties.
WriteString(&prop->name);
WriteString(&prop->nameSpace->name);
WriteDataType(&prop->type);
// Store the initialization function
WriteFunction(prop->GetInitFunc());
}
void asCWriter::WriteObjectProperty(asCObjectProperty *prop) {
WriteString(&prop->name);
WriteDataType(&prop->type);
int flags = 0;
if (prop->isPrivate) flags |= 1;
if (prop->isProtected) flags |= 2;
if (prop->isInherited) flags |= 4;
WriteEncodedInt64(flags);
}
void asCWriter::WriteDataType(const asCDataType *dt) {
// First check if the datatype has already been saved
for (asUINT n = 0; n < savedDataTypes.GetLength(); n++) {
if (*dt == savedDataTypes[n]) {
WriteEncodedInt64(n + 1);
return;
}
}
// Indicate a new type with a null byte
asUINT c = 0;
WriteEncodedInt64(c);
// Save the new datatype
savedDataTypes.PushLast(*dt);
int t = dt->GetTokenType();
WriteEncodedInt64(t);
if (t == ttIdentifier)
WriteTypeInfo(dt->GetTypeInfo());
// Endianess safe bitmask
char bits = 0;
SAVE_TO_BIT(bits, dt->IsObjectHandle(), 0);
SAVE_TO_BIT(bits, dt->IsHandleToConst(), 1);
SAVE_TO_BIT(bits, dt->IsReference(), 2);
SAVE_TO_BIT(bits, dt->IsReadOnly(), 3);
WriteData(&bits, 1);
}
void asCWriter::WriteTypeInfo(asCTypeInfo *ti) {
char ch;
if (ti) {
// Check for template instances/specializations
asCObjectType *ot = CastToObjectType(ti);
if (ot && ot->templateSubTypes.GetLength()) {
// Check for list pattern type or template type
if (ot->flags & asOBJ_LIST_PATTERN) {
ch = 'l'; // list
WriteData(&ch, 1);
WriteTypeInfo(ot->templateSubTypes[0].GetTypeInfo());
} else {
ch = 'a'; // array
WriteData(&ch, 1);
WriteString(&ot->name);
WriteString(&ot->nameSpace->name);
WriteEncodedInt64(ot->templateSubTypes.GetLength());
for (asUINT n = 0; n < ot->templateSubTypes.GetLength(); n++) {
if (!ot->templateSubTypes[n].IsPrimitive() || ot->templateSubTypes[n].IsEnumType()) {
ch = 's'; // sub type
WriteData(&ch, 1);
WriteDataType(&ot->templateSubTypes[n]);
} else {
ch = 't'; // token
WriteData(&ch, 1);
eTokenType t = ot->templateSubTypes[n].GetTokenType();
WriteEncodedInt64(t);
}
}
}
} else if (ti->flags & asOBJ_TEMPLATE_SUBTYPE) {
ch = 's'; // sub type
WriteData(&ch, 1);
WriteString(&ti->name);
} else if (!ti->GetParentType()) {
ch = 'o'; // object
WriteData(&ch, 1);
WriteString(&ti->name);
WriteString(&ti->nameSpace->name);
} else {
asASSERT(ti->flags & asOBJ_FUNCDEF);
ch = 'c'; // child type
WriteData(&ch, 1);
WriteString(&ti->name);
WriteTypeInfo(CastToFuncdefType(ti)->parentClass);
}
} else {
ch = '\0';
WriteData(&ch, 1);
}
}
void asCWriter::CalculateAdjustmentByPos(asCScriptFunction *func) {
// Adjust the offset of all negative variables (parameters) so all pointers will have a size of 1 dword
asUINT n;
asCArray<int> adjustments;
asUINT offset = 0;
if (func->objectType) {
adjustments.PushLast(offset);
adjustments.PushLast(1 - AS_PTR_SIZE);
offset += AS_PTR_SIZE;
}
if (func->DoesReturnOnStack()) {
adjustments.PushLast(offset);
adjustments.PushLast(1 - AS_PTR_SIZE);
offset += AS_PTR_SIZE;
}
for (n = 0; n < func->parameterTypes.GetLength(); n++) {
if (!func->parameterTypes[n].IsPrimitive() ||
func->parameterTypes[n].IsReference()) {
adjustments.PushLast(offset);
adjustments.PushLast(1 - AS_PTR_SIZE);
offset += AS_PTR_SIZE;
} else {
asASSERT(func->parameterTypes[n].IsPrimitive());
offset += func->parameterTypes[n].GetSizeOnStackDWords();
}
}
// Build look-up table with the adjustments for each stack position
adjustNegativeStackByPos.SetLength(offset);
memset(adjustNegativeStackByPos.AddressOf(), 0, adjustNegativeStackByPos.GetLength()*sizeof(int));
for (n = 0; n < adjustments.GetLength(); n += 2) {
int pos = adjustments[n];
int adjust = adjustments[n + 1];
for (asUINT i = pos + 1; i < adjustNegativeStackByPos.GetLength(); i++)
adjustNegativeStackByPos[i] += adjust;
}
// Adjust the offset of all positive variables so that all object types and handles have a size of 1 dword
// This is similar to how the adjustment is done in the asCReader::TranslateFunction, only the reverse
adjustments.SetLength(0);
for (n = 0; n < func->scriptData->objVariableTypes.GetLength(); n++) {
// Determine the size the variable currently occupies on the stack
int size = AS_PTR_SIZE;
// objVariableTypes is null if the variable type is a null pointer
if (func->scriptData->objVariableTypes[n] &&
(func->scriptData->objVariableTypes[n]->GetFlags() & asOBJ_VALUE) &&
n >= func->scriptData->objVariablesOnHeap) {
size = func->scriptData->objVariableTypes[n]->GetSize();
if (size < 4)
size = 1;
else
size /= 4;
}
// If larger than 1 dword, adjust the offsets accordingly
if (size > 1) {
// How much needs to be adjusted?
adjustments.PushLast(func->scriptData->objVariablePos[n]);
adjustments.PushLast(-(size - 1));
}
}
// Build look-up table with the adjustments for each stack position
adjustStackByPos.SetLength(func->scriptData->stackNeeded);
memset(adjustStackByPos.AddressOf(), 0, adjustStackByPos.GetLength()*sizeof(int));
for (n = 0; n < adjustments.GetLength(); n += 2) {
int pos = adjustments[n];
int adjust = adjustments[n + 1];
for (asUINT i = pos; i < adjustStackByPos.GetLength(); i++)
adjustStackByPos[i] += adjust;
}
// Compute the sequence number of each bytecode instruction in order to update the jump offsets
asUINT length = func->scriptData->byteCode.GetLength();
asDWORD *bc = func->scriptData->byteCode.AddressOf();
bytecodeNbrByPos.SetLength(length);
asUINT num;
for (offset = 0, num = 0; offset < length;) {
bytecodeNbrByPos[offset] = num;
offset += asBCTypeSize[asBCInfo[*(asBYTE *)(bc + offset)].type];
num++;
}
// Store the number of instructions in the last position of bytecodeNbrByPos,
// so this can be easily queried in SaveBytecode. Normally this is already done
// as most functions end with BC_RET, but in some cases the last instruction in
// the function is not a BC_RET, e.g. when a function has a never ending loop.
bytecodeNbrByPos[length - 1] = num - 1;
}
int asCWriter::AdjustStackPosition(int pos) {
if (pos >= (int)adjustStackByPos.GetLength()) {
// This happens for example if the function only have temporary variables
// The adjustByPos can also be empty if the function doesn't have any variables at all, but receive a handle by parameter
if (adjustStackByPos.GetLength() > 0)
pos += adjustStackByPos[adjustStackByPos.GetLength() - 1];
} else if (pos >= 0)
pos += adjustStackByPos[pos];
else {
asASSERT(-pos < (int)adjustNegativeStackByPos.GetLength());
pos -= (short)adjustNegativeStackByPos[-pos];
}
return pos;
}
int asCWriter::AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos) {
// TODO: optimize: multiple instructions for the same function doesn't need to look for the function everytime
// the function can remember where it found the function and check if the programPos is still valid
// Get offset 0 doesn't need adjustment
if (offset == 0) return 0;
bool bcAlloc = false;
// Find out which function that will be called
asCScriptFunction *calledFunc = 0;
int stackDelta = 0;
for (asUINT n = programPos; n < func->scriptData->byteCode.GetLength();) {
asBYTE bc = *(asBYTE *)&func->scriptData->byteCode[n];
if (bc == asBC_CALL ||
bc == asBC_CALLSYS ||
bc == asBC_Thiscall1 ||
bc == asBC_CALLINTF) {
// Find the function from the function id in bytecode
int funcId = asBC_INTARG(&func->scriptData->byteCode[n]);
calledFunc = engine->scriptFunctions[funcId];
break;
} else if (bc == asBC_ALLOC) {
// The alloc instruction doesn't take the object pointer on the stack,
// as the memory will be allocated by the instruction itself
bcAlloc = true;
// Find the function from the function id in the bytecode
int funcId = asBC_INTARG(&func->scriptData->byteCode[n + AS_PTR_SIZE]);
calledFunc = engine->scriptFunctions[funcId];
break;
} else if (bc == asBC_CALLBND) {
// Find the function from the engine's bind array
int funcId = asBC_INTARG(&func->scriptData->byteCode[n]);
calledFunc = engine->importedFunctions[funcId & ~FUNC_IMPORTED]->importedFunctionSignature;
break;
} else if (bc == asBC_CallPtr) {
int var = asBC_SWORDARG0(&func->scriptData->byteCode[n]);
asUINT v;
// Find the funcdef from the local variable
for (v = 0; v < func->scriptData->objVariablePos.GetLength(); v++) {
if (func->scriptData->objVariablePos[v] == var) {
calledFunc = CastToFuncdefType(func->scriptData->objVariableTypes[v])->funcdef;
break;
}
}
if (!calledFunc) {
// Look in parameters
int paramPos = 0;
if (func->objectType)
paramPos -= AS_PTR_SIZE;
if (func->DoesReturnOnStack())
paramPos -= AS_PTR_SIZE;
for (v = 0; v < func->parameterTypes.GetLength(); v++) {
if (var == paramPos) {
calledFunc = CastToFuncdefType(func->parameterTypes[v].GetTypeInfo())->funcdef;
break;
}
paramPos -= func->parameterTypes[v].GetSizeOnStackDWords();
}
}
break;
} else if (bc == asBC_REFCPY ||
bc == asBC_COPY) {
// In this case we know there is only 1 pointer on the stack above
asASSERT(offset == AS_PTR_SIZE);
return offset + (1 - AS_PTR_SIZE);
}
// Keep track of the stack size between the
// instruction that needs to be adjusted and the call
stackDelta += asBCInfo[bc].stackInc;
n += asBCTypeSize[asBCInfo[bc].type];
}
asASSERT(calledFunc);
// Count the number of pointers pushed on the stack above the
// current offset, and then adjust the offset accordingly
asUINT numPtrs = 0;
int currOffset = -stackDelta;
if (offset > currOffset && calledFunc->GetObjectType() && !bcAlloc) {
currOffset += AS_PTR_SIZE;
if (currOffset > 0)
numPtrs++;
}
if (offset > currOffset && calledFunc->DoesReturnOnStack()) {
currOffset += AS_PTR_SIZE;
if (currOffset > 0)
numPtrs++;
}
for (asUINT p = 0; p < calledFunc->parameterTypes.GetLength(); p++) {
if (offset <= currOffset) break;
if (!calledFunc->parameterTypes[p].IsPrimitive() ||
calledFunc->parameterTypes[p].IsReference()) {
// objects and references are passed by pointer
currOffset += AS_PTR_SIZE;
if (currOffset > 0)
numPtrs++;
// The variable arg ? has an additional 32bit int with the typeid
if (calledFunc->parameterTypes[p].IsAnyType())
currOffset += 1;
} else {
// built-in primitives or enums are passed by value
asASSERT(calledFunc->parameterTypes[p].IsPrimitive());
currOffset += calledFunc->parameterTypes[p].GetSizeOnStackDWords();
}
}
// The get offset must match one of the parameter offsets
asASSERT(offset == currOffset);
return offset + numPtrs * (1 - AS_PTR_SIZE);
}
void asCWriter::WriteByteCode(asCScriptFunction *func) {
asDWORD *bc = func->scriptData->byteCode.AddressOf();
size_t length = func->scriptData->byteCode.GetLength();
// The length cannot be stored, because it is platform dependent,
// instead we store the number of instructions
asUINT count = bytecodeNbrByPos[bytecodeNbrByPos.GetLength() - 1] + 1;
WriteEncodedInt64(count);
asDWORD *startBC = bc;
while (length) {
asDWORD tmpBC[4]; // The biggest instructions take up 4 DWORDs
asDWORD c = *(asBYTE *)bc;
// Copy the instruction to a temp buffer so we can work on it before saving
memcpy(tmpBC, bc, asBCTypeSize[asBCInfo[c].type]*sizeof(asDWORD));
if (c == asBC_ALLOC) { // PTR_DW_ARG
// Translate the object type
asCObjectType *ot = *(asCObjectType **)(tmpBC + 1);
*(asPWORD *)(tmpBC + 1) = FindTypeInfoIdx(ot);
// Translate the constructor func id, unless it is 0
if (*(int *)&tmpBC[1 + AS_PTR_SIZE] != 0) {
// Increment 1 to the translated function id, as 0 will be reserved for no function
*(int *)&tmpBC[1 + AS_PTR_SIZE] = 1 + FindFunctionIndex(engine->scriptFunctions[*(int *)&tmpBC[1 + AS_PTR_SIZE]]);
}
} else if (c == asBC_REFCPY || // PTR_ARG
c == asBC_RefCpyV || // wW_PTR_ARG
c == asBC_OBJTYPE) { // PTR_ARG
// Translate object type pointers into indices
*(asPWORD *)(tmpBC + 1) = FindTypeInfoIdx(*(asCObjectType **)(tmpBC + 1));
} else if (c == asBC_JitEntry) { // PTR_ARG
// We don't store the JIT argument
*(asPWORD *)(tmpBC + 1) = 0;
} else if (c == asBC_TYPEID || // DW_ARG
c == asBC_Cast) { // DW_ARG
// Translate type ids into indices
*(int *)(tmpBC + 1) = FindTypeIdIdx(*(int *)(tmpBC + 1));
} else if (c == asBC_ADDSi || // W_DW_ARG
c == asBC_LoadThisR) { // W_DW_ARG
// Translate property offsets into indices
*(((short *)tmpBC) + 1) = (short)FindObjectPropIndex(*(((short *)tmpBC) + 1), *(int *)(tmpBC + 1), bc);
// Translate type ids into indices
*(int *)(tmpBC + 1) = FindTypeIdIdx(*(int *)(tmpBC + 1));
} else if (c == asBC_LoadRObjR || // rW_W_DW_ARG
c == asBC_LoadVObjR) { // rW_W_DW_ARG
asCObjectType *ot = engine->GetObjectTypeFromTypeId(*(int *)(tmpBC + 2));
if (ot->flags & asOBJ_LIST_PATTERN) {
// List patterns have a different way of translating the offsets
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
*(((short *)tmpBC) + 2) = (short)listAdj->AdjustOffset(*(((short *)tmpBC) + 2), ot);
} else {
// Translate property offsets into indices
*(((short *)tmpBC) + 2) = (short)FindObjectPropIndex(*(((short *)tmpBC) + 2), *(int *)(tmpBC + 2), bc);
}
// Translate type ids into indices
*(int *)(tmpBC + 2) = FindTypeIdIdx(*(int *)(tmpBC + 2));
} else if (c == asBC_COPY) { // W_DW_ARG
// Translate type ids into indices
*(int *)(tmpBC + 1) = FindTypeIdIdx(*(int *)(tmpBC + 1));
// Update the WORDARG0 to 0, as this will be recalculated on the target platform
asBC_WORDARG0(tmpBC) = 0;
} else if (c == asBC_RET) { // W_ARG
// Save with arg 0, as this will be recalculated on the target platform
asBC_WORDARG0(tmpBC) = 0;
} else if (c == asBC_CALL || // DW_ARG
c == asBC_CALLINTF || // DW_ARG
c == asBC_CALLSYS || // DW_ARG
c == asBC_Thiscall1) { // DW_ARG
// Translate the function id
*(int *)(tmpBC + 1) = FindFunctionIndex(engine->scriptFunctions[*(int *)(tmpBC + 1)]);
} else if (c == asBC_FuncPtr) { // PTR_ARG
// Translate the function pointer
*(asPWORD *)(tmpBC + 1) = FindFunctionIndex(*(asCScriptFunction **)(tmpBC + 1));
} else if (c == asBC_CALLBND) { // DW_ARG
// Translate the function id
int funcId = tmpBC[1];
for (asUINT n = 0; n < module->m_bindInformations.GetLength(); n++)
if (module->m_bindInformations[n]->importedFunctionSignature->id == funcId) {
funcId = n;
break;
}
tmpBC[1] = funcId;
} else if (c == asBC_PGA || // PTR_ARG
c == asBC_PshGPtr || // PTR_ARG
c == asBC_LDG || // PTR_ARG
c == asBC_PshG4 || // PTR_ARG
c == asBC_LdGRdR4 || // wW_PTR_ARG
c == asBC_CpyGtoV4 || // wW_PTR_ARG
c == asBC_CpyVtoG4 || // rW_PTR_ARG
c == asBC_SetG4) { // PTR_DW_ARG
// Check if the address is a global property or a string constant
void *ptr = *(void **)(tmpBC + 1);
if (engine->varAddressMap.MoveTo(0, ptr)) {
// Translate global variable pointers into indices
// Flag the first bit to signal global property
*(asPWORD *)(tmpBC + 1) = (FindGlobalPropPtrIndex(*(void **)(tmpBC + 1)) << 1) + 1;
} else {
// Only PGA and PshGPtr can hold string constants
asASSERT(c == asBC_PGA || c == asBC_PshGPtr);
// Translate string constants into indices
// Leave the first bit clear to signal string constant
*(asPWORD *)(tmpBC + 1) = FindStringConstantIndex(*(void **)(tmpBC + 1)) << 1;
}
} else if (c == asBC_JMP || // DW_ARG
c == asBC_JZ ||
c == asBC_JNZ ||
c == asBC_JLowZ ||
c == asBC_JLowNZ ||
c == asBC_JS ||
c == asBC_JNS ||
c == asBC_JP ||
c == asBC_JNP) { // The JMPP instruction doesn't need modification
// Get the DWORD offset from arg
int offset = *(int *)(tmpBC + 1);
// Determine instruction number for next instruction and destination
int bcSeqNum = bytecodeNbrByPos[asUINT(bc - startBC)] + 1;
asDWORD *targetBC = bc + 2 + offset;
int targetBcSeqNum = bytecodeNbrByPos[asUINT(targetBC - startBC)];
// Set the offset in number of instructions
*(int *)(tmpBC + 1) = targetBcSeqNum - bcSeqNum;
} else if (c == asBC_GETOBJ || // W_ARG
c == asBC_GETOBJREF ||
c == asBC_GETREF ||
c == asBC_ChkNullS) {
// Adjust the offset according to the function call that comes after
asBC_WORDARG0(tmpBC) = (asWORD)AdjustGetOffset(asBC_WORDARG0(tmpBC), func, asDWORD(bc - startBC));
} else if (c == asBC_AllocMem) {
// It's not necessary to store the size of the list buffer, as it will be recalculated in the reader
asBC_DWORDARG(tmpBC) = 0;
// Determine the type of the list pattern from the variable
short var = asBC_WORDARG0(tmpBC);
asCObjectType *ot = CastToObjectType(func->GetTypeInfoOfLocalVar(var));
// Create this helper object to adjust the offset of the elements accessed in the buffer
listAdjusters.PushLast(asNEW(SListAdjuster)(ot));
} else if (c == asBC_FREE) { // wW_PTR_ARG
// Translate object type pointers into indices
asCObjectType *ot = *(asCObjectType **)(tmpBC + 1);
*(asPWORD *)(tmpBC + 1) = FindTypeInfoIdx(ot);
// Pop and destroy the list adjuster helper that was created with asBC_AllocMem
if (ot && (ot->flags & asOBJ_LIST_PATTERN)) {
SListAdjuster *list = listAdjusters.PopLast();
asDELETE(list, SListAdjuster);
}
} else if (c == asBC_SetListSize) {
// Adjust the offset in the initialization list
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
tmpBC[1] = listAdj->AdjustOffset(tmpBC[1], listAdj->patternType);
// Tell the adjuster how many repeated values there are
listAdj->SetRepeatCount(tmpBC[2]);
} else if (c == asBC_PshListElmnt) { // W_DW_ARG
// Adjust the offset in the initialization list
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
tmpBC[1] = listAdj->AdjustOffset(tmpBC[1], listAdj->patternType);
} else if (c == asBC_SetListType) {
// Adjust the offset in the initialization list
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength() - 1];
tmpBC[1] = listAdj->AdjustOffset(tmpBC[1], listAdj->patternType);
// Inform the adjuster of the type id of the next element
listAdj->SetNextType(tmpBC[2]);
// Translate the type id
tmpBC[2] = FindTypeIdIdx(tmpBC[2]);
}
// Adjust the variable offsets
switch (asBCInfo[c].type) {
case asBCTYPE_wW_ARG:
case asBCTYPE_rW_DW_ARG:
case asBCTYPE_wW_QW_ARG:
case asBCTYPE_rW_ARG:
case asBCTYPE_wW_DW_ARG:
case asBCTYPE_wW_W_ARG:
case asBCTYPE_rW_QW_ARG:
case asBCTYPE_rW_W_DW_ARG:
case asBCTYPE_rW_DW_DW_ARG: {
asBC_SWORDARG0(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG0(tmpBC));
}
break;
case asBCTYPE_wW_rW_ARG:
case asBCTYPE_wW_rW_DW_ARG:
case asBCTYPE_rW_rW_ARG: {
asBC_SWORDARG0(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG0(tmpBC));
asBC_SWORDARG1(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG1(tmpBC));
}
break;
case asBCTYPE_wW_rW_rW_ARG: {
asBC_SWORDARG0(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG0(tmpBC));
asBC_SWORDARG1(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG1(tmpBC));
asBC_SWORDARG2(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG2(tmpBC));
}
break;
default:
// The other types don't treat variables so won't be modified
break;
}
// TODO: bytecode: Must make sure that floats and doubles are always stored the same way regardless of platform.
// Some platforms may not use the IEEE 754 standard, in which case it is necessary to encode the values
// Now store the instruction in the smallest possible way
switch (asBCInfo[c].type) {
case asBCTYPE_NO_ARG: {
// Just write 1 byte
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
}
break;
case asBCTYPE_W_ARG:
case asBCTYPE_wW_ARG:
case asBCTYPE_rW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the argument
short w = *(((short *)tmpBC) + 1);
WriteEncodedInt64(w);
}
break;
case asBCTYPE_rW_DW_ARG:
case asBCTYPE_wW_DW_ARG:
case asBCTYPE_W_DW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the word argument
short w = *(((short *)tmpBC) + 1);
WriteEncodedInt64(w);
// Write the dword argument
WriteEncodedInt64((int)tmpBC[1]);
}
break;
case asBCTYPE_DW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the argument
WriteEncodedInt64((int)tmpBC[1]);
}
break;
case asBCTYPE_DW_DW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the dword argument
WriteEncodedInt64((int)tmpBC[1]);
// Write the dword argument
WriteEncodedInt64((int)tmpBC[2]);
}
break;
case asBCTYPE_wW_rW_rW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the first argument
short w = *(((short *)tmpBC) + 1);
WriteEncodedInt64(w);
// Write the second argument
w = *(((short *)tmpBC) + 2);
WriteEncodedInt64(w);
// Write the third argument
w = *(((short *)tmpBC) + 3);
WriteEncodedInt64(w);
}
break;
case asBCTYPE_wW_rW_ARG:
case asBCTYPE_rW_rW_ARG:
case asBCTYPE_wW_W_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the first argument
short w = *(((short *)tmpBC) + 1);
WriteEncodedInt64(w);
// Write the second argument
w = *(((short *)tmpBC) + 2);
WriteEncodedInt64(w);
}
break;
case asBCTYPE_wW_rW_DW_ARG:
case asBCTYPE_rW_W_DW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the first argument
short w = *(((short *)tmpBC) + 1);
WriteEncodedInt64(w);
// Write the second argument
w = *(((short *)tmpBC) + 2);
WriteEncodedInt64(w);
// Write the third argument
int dw = tmpBC[2];
WriteEncodedInt64(dw);
}
break;
case asBCTYPE_QW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the argument
asQWORD qw = *(asQWORD *)&tmpBC[1];
WriteEncodedInt64(qw);
}
break;
case asBCTYPE_QW_DW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the argument
asQWORD qw = *(asQWORD *)&tmpBC[1];
WriteEncodedInt64(qw);
// Write the second argument
int dw = tmpBC[3];
WriteEncodedInt64(dw);
}
break;
case asBCTYPE_rW_QW_ARG:
case asBCTYPE_wW_QW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the first argument
short w = *(((short *)tmpBC) + 1);
WriteEncodedInt64(w);
// Write the argument
asQWORD qw = *(asQWORD *)&tmpBC[1];
WriteEncodedInt64(qw);
}
break;
case asBCTYPE_rW_DW_DW_ARG: {
// Write the instruction code
asBYTE b = (asBYTE)c;
WriteData(&b, 1);
// Write the short argument
short w = *(((short *)tmpBC) + 1);
WriteEncodedInt64(w);
// Write the dword argument
WriteEncodedInt64((int)tmpBC[1]);
// Write the dword argument
WriteEncodedInt64((int)tmpBC[2]);
}
break;
default: {
// This should never happen
asASSERT(false);
// Store the bc as is
for (int n = 0; n < asBCTypeSize[asBCInfo[c].type]; n++)
WriteData(&tmpBC[n], 4);
}
}
// Move to the next instruction
bc += asBCTypeSize[asBCInfo[c].type];
length -= asBCTypeSize[asBCInfo[c].type];
}
}
asCWriter::SListAdjuster::SListAdjuster(asCObjectType *ot) : patternType(ot), repeatCount(0), entries(0), lastOffset(-1), nextOffset(0), nextTypeId(-1) {
asASSERT(ot && (ot->flags & asOBJ_LIST_PATTERN));
// Find the first expected value in the list
asSListPatternNode *node = ot->engine->scriptFunctions[patternType->templateSubTypes[0].GetBehaviour()->listFactory]->listPattern;
asASSERT(node && node->type == asLPT_START);
patternNode = node->next;
}
int asCWriter::SListAdjuster::AdjustOffset(int offset, asCObjectType *listPatternType) {
// TODO: cleanup: The listPatternType parameter is not needed
asASSERT(patternType == listPatternType);
UNUSED_VAR(listPatternType);
asASSERT(offset >= lastOffset);
// If it is the same offset being accessed again, just return the same adjusted value
if (offset == lastOffset)
return entries - 1;
asASSERT(offset >= nextOffset);
// Update last offset for next call
lastOffset = offset;
// What is being expected at this position?
if (patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME) {
// Don't move the patternNode yet because the caller must make a call to SetRepeatCount too
nextOffset = offset + 4;
return entries++;
} else if (patternNode->type == asLPT_TYPE) {
const asCDataType &dt = reinterpret_cast<asSListPatternDataTypeNode *>(patternNode)->dataType;
if (dt.GetTokenType() == ttQuestion) {
// The bytecode need to inform the type that will
// come next and then adjust that position too before
// we can move to the next node
if (nextTypeId != -1) {
nextOffset = offset + 4;
if (repeatCount > 0)
repeatCount--;
// Only move the patternNode if we're not expecting any more repeated entries
if (repeatCount == 0)
patternNode = patternNode->next;
nextTypeId = -1;
}
} else {
if (repeatCount > 0) {
// Was any value skipped?
asUINT size;
if (dt.IsObjectHandle() || (dt.GetTypeInfo() && (dt.GetTypeInfo()->flags & asOBJ_REF)))
size = AS_PTR_SIZE * 4;
else
size = dt.GetSizeInMemoryBytes();
int count = 0;
while (nextOffset <= offset) {
count++;
nextOffset += size;
// Align the offset on 4 byte boundaries
if (size >= 4 && (nextOffset & 0x3))
nextOffset += 4 - (nextOffset & 0x3);
}
if (--count > 0) {
// Skip these values
repeatCount -= count;
entries += count;
}
nextOffset = offset + size;
repeatCount--;
}
// Only move the patternNode if we're not expecting any more repeated entries
if (repeatCount == 0)
patternNode = patternNode->next;
}
return entries++;
} else if (patternNode->type == asLPT_START) {
if (repeatCount > 0)
repeatCount--;
SInfo info = {repeatCount, patternNode};
stack.PushLast(info);
repeatCount = 0;
patternNode = patternNode->next;
lastOffset--;
return AdjustOffset(offset, listPatternType);
} else if (patternNode->type == asLPT_END) {
SInfo info = stack.PopLast();
repeatCount = info.repeatCount;
if (repeatCount)
patternNode = info.startNode;
else
patternNode = patternNode->next;
lastOffset--;
return AdjustOffset(offset, listPatternType);
} else {
// Something is wrong with the pattern list declaration
asASSERT(false);
}
return 0;
}
void asCWriter::SListAdjuster::SetRepeatCount(asUINT rc) {
// Make sure the list is expecting a repeat at this location
asASSERT(patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME);
// Now move to the next patternNode
patternNode = patternNode->next;
repeatCount = rc;
}
void asCWriter::SListAdjuster::SetNextType(int typeId) {
// Make sure the list is expecting a type at this location
asASSERT(patternNode->type == asLPT_TYPE &&
reinterpret_cast<asSListPatternDataTypeNode *>(patternNode)->dataType.GetTokenType() == ttQuestion);
// Inform the type id for the next adjustment
nextTypeId = typeId;
}
void asCWriter::WriteUsedTypeIds() {
TimeIt("asCWriter::WriteUsedTypeIds");
asUINT count = (asUINT)usedTypeIds.GetLength();
WriteEncodedInt64(count);
for (asUINT n = 0; n < count; n++) {
asCDataType dt = engine->GetDataTypeFromTypeId(usedTypeIds[n]);
WriteDataType(&dt);
}
}
int asCWriter::FindGlobalPropPtrIndex(void *ptr) {
int i = usedGlobalProperties.IndexOf(ptr);
if (i >= 0) return i;
usedGlobalProperties.PushLast(ptr);
return (int)usedGlobalProperties.GetLength() - 1;
}
void asCWriter::WriteUsedGlobalProps() {
TimeIt("asCWriter::WriteUsedGlobalProps");
int c = (int)usedGlobalProperties.GetLength();
WriteEncodedInt64(c);
for (int n = 0; n < c; n++) {
asPWORD *p = (asPWORD *)usedGlobalProperties[n];
// Find the property descriptor from the address
asCGlobalProperty *prop = 0;
asSMapNode<void *, asCGlobalProperty *> *cursor;
if (engine->varAddressMap.MoveTo(&cursor, p)) {
prop = engine->varAddressMap.GetValue(cursor);
}
asASSERT(prop);
// Store the name and type of the property so we can find it again on loading
WriteString(&prop->name);
WriteString(&prop->nameSpace->name);
WriteDataType(&prop->type);
// Also store whether the property is a module property or a registered property
char moduleProp = 0;
if (prop->realAddress == 0)
moduleProp = 1;
WriteData(&moduleProp, 1);
}
}
void asCWriter::WriteUsedObjectProps() {
TimeIt("asCWriter::WriteUsedObjectProps");
int c = (int)usedObjectProperties.GetLength();
WriteEncodedInt64(c);
for (asUINT n = 0; n < usedObjectProperties.GetLength(); n++) {
WriteTypeInfo(usedObjectProperties[n].objType);
WriteString(&usedObjectProperties[n].prop->name);
}
}
int asCWriter::FindObjectPropIndex(short offset, int typeId, asDWORD *bc) {
// If the last property was a composite property, then just return 0, because it won't be translated
static bool lastWasComposite = false;
if (lastWasComposite) {
lastWasComposite = false;
return 0;
}
asCObjectType *objType = engine->GetObjectTypeFromTypeId(typeId);
asCObjectProperty *objProp = 0;
// Look for composite properties first
for (asUINT n = 0; objProp == 0 && n < objType->properties.GetLength(); n++) {
// TODO: Composite: Perhaps it would be better to add metadata to the bytecode instruction to give the exact property.
// That would also allow me to remove the typeId from the bytecode instruction itself
// Or perhaps a new bytecode instruction all together for accessing composite properties
// One that would do both offsets and indirection in a single go.
// TODO: Composite: Need to be able to handle instructions replaced in bytecode optimizations too
if (objType->properties[n]->compositeOffset == offset) {
// This is a potential composite property. Need to check the following instructions to be sure
objProp = objType->properties[n];
asDWORD *bcTemp = bc;
bcTemp += asBCTypeSize[asBCInfo[*(asBYTE *)bcTemp].type];
if (objProp->isCompositeIndirect) {
// The next instruction would be a asBC_RDSPtr
if ((*(asBYTE *)bcTemp) != asBC_RDSPtr) {
objProp = 0;
continue;
}
bcTemp += asBCTypeSize[asBCInfo[*(asBYTE *)bcTemp].type];
}
// The next instruction would be asBC_ADDSi
if ((*(asBYTE *)bcTemp) != asBC_ADDSi) {
objProp = 0;
continue;
}
// Make sure the offset is the expected one
if (*(((short *)bcTemp) + 1) != objProp->byteOffset) {
objProp = 0;
continue;
}
}
}
// If none of the composite properties matched, then look for ordinary property
for (asUINT n = 0; objProp == 0 && n < objType->properties.GetLength(); n++) {
if (objType->properties[n]->byteOffset == offset && !(objType->properties[n]->compositeOffset || objType->properties[n]->isCompositeIndirect))
objProp = objType->properties[n];
}
asASSERT(objProp);
// Remember if this is a composite property as the next call will then be for the same property
if (objProp->compositeOffset || objProp->isCompositeIndirect)
lastWasComposite = true;
// Now check if the same property has already been accessed
for (asUINT n = 0; n < usedObjectProperties.GetLength(); n++) {
if (usedObjectProperties[n].objType == objType &&
usedObjectProperties[n].prop == objProp)
return n;
}
// Insert the new property
SObjProp prop = {objType, objProp};
usedObjectProperties.PushLast(prop);
return (int)usedObjectProperties.GetLength() - 1;
}
int asCWriter::FindFunctionIndex(asCScriptFunction *func) {
for (asUINT n = 0; n < usedFunctions.GetLength(); n++) {
if (usedFunctions[n] == func)
return n;
}
usedFunctions.PushLast(func);
return (int)usedFunctions.GetLength() - 1;
}
int asCWriter::FindTypeIdIdx(int typeId) {
asUINT n;
for (n = 0; n < usedTypeIds.GetLength(); n++) {
if (usedTypeIds[n] == typeId)
return n;
}
usedTypeIds.PushLast(typeId);
return (int)usedTypeIds.GetLength() - 1;
}
int asCWriter::FindTypeInfoIdx(asCTypeInfo *obj) {
asUINT n;
for (n = 0; n < usedTypes.GetLength(); n++) {
if (usedTypes[n] == obj)
return n;
}
usedTypes.PushLast(obj);
return (int)usedTypes.GetLength() - 1;
}
#endif // AS_NO_COMPILER
END_AS_NAMESPACE
|