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
|
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/tads3/TCT3STM.CPP,v 1.1 1999/07/11 00:46:57 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
tct3stm.cpp - TADS 3 Compiler - T3 VM Code Generator - statement classes
Function
Generate code for the T3 VM. This file contains statement classes,
in order to segregate the code generation classes required for the
full compiler from those required for subsets that require only
expression parsing (such as debuggers).
Notes
Modified
05/08/99 MJRoberts - Creation
*/
#include <stdio.h>
#include "t3std.h"
#include "os.h"
#include "tcprs.h"
#include "tct3.h"
#include "tcgen.h"
#include "vmtype.h"
#include "vmwrtimg.h"
#include "vmfile.h"
#include "tcmain.h"
#include "tcerr.h"
/* ------------------------------------------------------------------------ */
/*
* 'if' statement
*/
/*
* generate code
*/
void CTPNStmIf::gen_code(int, int)
{
/* add a line record */
add_debug_line_rec();
/*
* if the condition has a constant value, don't bother generating
* code for both branches
*/
if (cond_expr_->is_const())
{
int val;
/* determine whether it's true or false */
val = cond_expr_->get_const_val()->get_val_bool();
/*
* Warn about it if it's always false (in which case the 'then'
* code is unreachable); or it's always true and we have an
* 'else' part (since the 'else' part is unreachable). Don't
* warn if it's true and there's no 'else' part, since this
* merely means that there's some redundant source code, but
* will have no effect on the generated code.
*/
if (!val)
{
/*
* It's false - the 'then' part cannot be executed. If this
* isn't a compile-time constant expression, warn about it.
*/
if (!cond_expr_->get_const_val()->is_ctc())
log_warning(TCERR_IF_ALWAYS_FALSE);
/* generate the 'else' part if there is one */
if (else_part_ != 0)
gen_code_substm(else_part_);
}
else
{
/* it's true - the 'else' part cannot be executed */
if (else_part_ != 0)
log_warning(TCERR_IF_ALWAYS_TRUE);
/* generate the 'then' part */
if (then_part_ != 0)
gen_code_substm(then_part_);
}
/* we're done */
return;
}
/*
* If both the 'then' and 'else' parts are null statements, we're
* evaluating the condition purely for side effects. Simply
* evaluate the condition in this case, since there's no need to so
* much as test the condition once evaluated.
*/
if (then_part_ == 0 && else_part_ == 0)
{
/* generate the condition, discarding the result */
cond_expr_->gen_code(TRUE, TRUE);
/* we're done */
return;
}
/*
* The condition is non-constant, and we have at least one subclause,
* so we must evaluate the condition expression. To minimize the
* amount of jumping, check whether we have a true part, else part, or
* both, and generate the branching accordingly.
*/
if (then_part_ != 0)
{
CTcCodeLabel *lbl_else;
CTcCodeLabel *lbl_end;
/*
* We have a true part, so we will want to evaluate the expression
* and jump past the true part if the expression is false. Create
* a label for the false branch.
*/
lbl_else = G_cs->new_label_fwd();
/* generate the condition expression */
cond_expr_->gen_code_cond(0, lbl_else);
/* generate the 'then' part */
gen_code_substm(then_part_);
/* if there's an 'else' part, generate it */
if (else_part_ != 0)
{
/* at the end of the 'then' part, jump past the 'else' part */
lbl_end = gen_jump_ahead(OPC_JMP);
/* this is the start of the 'else' part */
def_label_pos(lbl_else);
/* generate the 'else' part */
gen_code_substm(else_part_);
/* set the label for the jump over the 'else' part */
def_label_pos(lbl_end);
}
else
{
/*
* there's no 'else' part - set the label for the jump past the
* 'then' part
*/
def_label_pos(lbl_else);
}
}
else
{
CTcCodeLabel *lbl_end;
/*
* There's no 'then' part, so there must be an 'else' part (we
* wouldn't have gotten this far if both 'then' and 'else' are
* empty). To minimize branching, evaluate the condition and jump
* past the 'else' part if the condition is true, falling through
* to the 'else' part otherwise. Create a label for the end of the
* statement, which is also the empty 'then' part.
*/
lbl_end = G_cs->new_label_fwd();
/* evaluate the condition and jump to the end if it's true */
cond_expr_->gen_code_cond(lbl_end, 0);
/* generate the 'else' part */
gen_code_substm(else_part_);
/* set the label for the jump over the 'else' part */
def_label_pos(lbl_end);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'for' statement
*/
/*
* generate code
*/
void CTPNStmFor::gen_code(int, int)
{
CTcCodeLabel *top_lbl;
CTcCodeLabel *end_lbl;
CTcCodeLabel *cont_lbl;
CTPNStmEnclosing *old_enclosing;
CTcPrsSymtab *old_frame;
/* set my local frame if necessary */
old_frame = G_cs->set_local_frame(symtab_);
/*
* add a line record - note that we add the line record after
* setting up the local frame, so that the 'for' statement itself
* appears within its own inner scope
*/
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/* if there's an initializer expression, generate it */
if (init_expr_ != 0)
init_expr_->gen_code(TRUE, TRUE);
/* set the label for the top of the loop */
top_lbl = new_label_here();
/* allocate a forward label for 'continue' jumps */
cont_lbl = G_cs->new_label_fwd();
/* allocate a forward label for the end of the loop */
end_lbl = G_cs->new_label_fwd();
/* generate the implicit conditions for any 'in' clauses */
for (CTPNForIn *i = in_exprs_ ; i != 0 ; i = i->getnxt())
i->gen_forstm_cond(end_lbl);
/*
* If there's an explicit condition clause, generate its code, jumping
* to the end of the loop if the condition is false.
*/
if (cond_expr_ != 0)
cond_expr_->gen_code_cond(0, end_lbl);
/*
* set our labels, so that 'break' and 'continue' statements in our
* body will know where to go
*/
break_lbl_ = end_lbl;
cont_lbl_ = cont_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/*
* add another line record - we're now generating code again for the
* original 'for' line, even though it's after the body
*/
//$$$ add_debug_line_rec();
/* this is where we come for 'continue' statements */
def_label_pos(cont_lbl);
/* generate the reinitializer expression, if we have one */
if (reinit_expr_ != 0)
reinit_expr_->gen_code(TRUE, TRUE);
/* generate the implicit reinitializers for any 'in' clauses */
for (CTPNForIn *i = in_exprs_ ; i != 0 ; i = i->getnxt())
i->gen_forstm_reinit();
/* jump back to the top of the loop */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(top_lbl, 0);
/*
* we're at the end of the loop - this is where we jump for 'break'
* and when the condition becomes false
*/
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
/* restore the enclosing local scope */
G_cs->set_local_frame(old_frame);
}
/* ------------------------------------------------------------------------ */
/*
* 'for var in expr' node
*/
/*
* Generate code. This is called during the initializer phase of the 'for'
* that we're part of. We create the collection enumerator and assign the
* first value from the enumerator to the lvalue.
*/
void CTPNVarIn::gen_code(int, int)
{
/* generate the iterator initializer */
gen_iter_init(expr_, iter_local_id_, "for");
}
/*
* Static method: generate the iterator initializer
*/
void CTPNVarIn::gen_iter_init(CTcPrsNode *coll_expr, int iter_local_id,
const char *kw)
{
/*
* Look up the createIterator property of the Collection metaclass.
* This property is defined by the Collection specification as the
* property in the first (zeroeth) slot in the method table for
* Collection. If Collection isn't defined, or this slot isn't
* defined, it's an error.
*/
CTcPrsNode *create_iter = G_cg->get_metaclass_prop("collection", 0);
/* if we didn't find the property, it's an error */
if (create_iter != VM_INVALID_PROP)
{
/*
* generate a call to the createIterator() property on the
* collection expression
*/
if (coll_expr != 0)
coll_expr->gen_code_member(FALSE, create_iter, FALSE, 0, FALSE, 0);
/* assign the result to the internal iterator stack local */
CTcSymLocal::s_gen_code_setlcl_stk(iter_local_id, FALSE);
}
else
{
/* tell them about the problem */
G_tok->log_error(TCERR_FOREACH_NO_CREATEITER, kw);
}
}
/*
* Generate code for the condition portion of the 'for'. We check to see
* if there's a next value: if so we fetch it into our lvalue, it not we
* break the loop.
*/
void CTPNVarIn::gen_forstm_cond(CTcCodeLabel *endlbl)
{
/* generate the code */
gen_iter_cond(lval_, iter_local_id_, endlbl, "for");
}
/*
* Generate code for the condition portion of a for..in or a foreach..in.
*/
void CTPNVarIn::gen_iter_cond(CTcPrsNode *lval, int iter_local_id,
CTcCodeLabel *&end_lbl, const char *kw)
{
#if 1
/* if the caller didn't provide an end-of-loop label, create one */
if (end_lbl == 0)
end_lbl = G_cs->new_label_fwd();
/*
* Generate the ITERNEXT <iterator local>, <end offset>. This pushes
* the next iterator value if available, otherwise jumps to the end
* label (without pushing anything onto the stack).
*/
G_cg->write_op(OPC_ITERNEXT);
G_cs->write2(iter_local_id);
G_cs->write_ofs2(end_lbl, 0);
/* we're on the looping branch - ITERNEXT pushed the next value */
G_cg->note_push();
/* assign the next value to the loop control lvalue */
if (lval != 0)
lval->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", 0, FALSE, TRUE, 0);
else
{
G_cg->write_op(OPC_DISC);
G_cg->note_pop();
}
#else
/*
* Look up Iterator.getNext() (iterator metaclass method index 0) and
* Iterator.isNextAvailable() (index 1).
*/
CTcPrsNode *get_next = G_cg->get_metaclass_prop("iterator", 0);
CTcPrsNode *next_avail = G_cg->get_metaclass_prop("iterator", 1);
/* generate the isNextAvailable test */
if (next_avail != 0)
{
/* get the internal iterator local */
CTcSymLocal::s_gen_code_getlcl(iter_local_id, FALSE);
/* generate a call to the property */
CTcPrsNode::s_gen_member_rhs(
FALSE, next_avail, FALSE, 0, FALSE, FALSE);
/*
* generate a jump to the end of the loop, creating the label if
* the caller didn't already
*/
if (end_lbl != 0)
{
/* jump to the caller's end label */
G_cg->write_op(OPC_JF);
G_cs->write_ofs2(end_lbl, 0);
}
else
{
/* we need a new label */
end_lbl = gen_jump_ahead(OPC_JF);
}
/* the JF pops an element off the stack */
G_cg->note_pop();
}
else
{
/* this property is required to be defined - this is an error */
G_tok->log_error(TCERR_FOREACH_NO_ISNEXTAVAIL, kw);
/*
* generate an arbitrary 'end' label if the caller didn't already
* create one - we're not going to end up generating valid code
* anyway, but since we're not going to abort code generation,
* it'll avoid problems elsewhere if we have a valid label assigned
*/
if (end_lbl == 0)
end_lbl = new_label_here();
}
/* generate the code to get the next element of the iteration */
if (get_next != 0)
{
/* get the internal iterator local */
CTcSymLocal::s_gen_code_getlcl(iter_local_id, FALSE);
/* generate a call to the property */
CTcPrsNode::s_gen_member_rhs(FALSE, get_next, FALSE, 0, FALSE, FALSE);
/* assign the result to the iterator lvalue */
if (lval != 0)
lval->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", 0, FALSE, TRUE, 0);
}
else
{
/* this property is required to be defined - this is an error */
G_tok->log_error(TCERR_FOREACH_NO_GETNEXT, kw);
}
#endif
}
/*
* Generate code for the reinit portion of the 'for'. We do nothing on
* this step; we do the test and fetch as one operation in the condition
* instead, since the iterator test has to be done ahead of the fetch.
*/
void CTPNVarIn::gen_forstm_reinit()
{
/* do nothing on this phase */
}
/* ------------------------------------------------------------------------ */
/*
* 'for var in from..to' node
*/
/*
* Generate code. This is called during the initializer pharse of the
* 'for' that we're part of. We evaluate the 'from' and 'to' expressions,
* assign the 'from' to the lvalue, and save the 'to' in a private local
* variable.
*/
void CTPNVarInRange::gen_code(int, int)
{
/* assign the 'from' expression to the lvalue */
lval_->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", from_expr_,
FALSE, TRUE, 0);
/*
* if the 'to' expression is non-constant, evaluate it and save it in a
* local, so that we don't have to re-evaluate it on each iteration
*/
if (!to_expr_->is_const())
{
/* evaluate the 'to' expression */
to_expr_->gen_code(FALSE, FALSE);
/* assign it to our stack local */
CTcSymLocal::s_gen_code_setlcl_stk(to_local_id_, FALSE);
}
/*
* likewise, if there's a non-constant 'step' expression, evaluate it
* and store it in our step stack local
*/
if (step_expr_ != 0 && !step_expr_->is_const())
{
/* generate the step expression value */
step_expr_->gen_code(FALSE, FALSE);
/* generate the assignment to our local */
CTcSymLocal::s_gen_code_setlcl_stk(step_local_id_, FALSE);
/* replace the step expression with the local */
step_expr_ = new CTPNSymResolved(new CTcSymLocal(
".step", 5, FALSE, FALSE, step_local_id_));
}
}
/*
* Generate code for the condition portion of the 'for'. We test our
* lvalue to see if we've passed the 'to' limit, in which case we break out
* of the loop.
*/
void CTPNVarInRange::gen_forstm_cond(CTcCodeLabel *endlbl)
{
/* push the lvalue */
lval_->gen_code(FALSE, FALSE);
/*
* push the 'to' expression - if it's constant, generate the constant
* expression value, otherwise get the local
*/
if (to_expr_->is_const())
to_expr_->gen_code(FALSE, FALSE);
else
CTcSymLocal::s_gen_code_getlcl(to_local_id_, FALSE);
/*
* If the step expression is a constant, compare based on the sign of
* the constant expression: if positive, we stop when the lval is
* greater than the 'to' limit; if negative, we stop when the lval is
* less than to 'to' limit. If the expression isn't a constant, we
* have to test the sign at run-time.
*/
int s = 0;
if (step_expr_ == 0)
{
/* no step expression -> default step is 1, sign is positive */
s = 1;
}
else if (step_expr_->is_const())
{
/* explicit step expression with a constant value - check the type */
switch (step_expr_->get_const_val()->get_type())
{
case TC_CVT_INT:
/* integer - get its sign */
s = step_expr_->get_const_val()->get_val_int() < 0 ? -1 : 1;
break;
case TC_CVT_FLOAT:
/* BigNumber value - check for a '-' */
s = step_expr_->get_const_val()->get_val_float()[0] == '-'
? -1 : 1;
break;
default:
/* others will have to be interpreted at run-time */
s = 0;
break;
}
}
/*
* if we have a known sign, generate a fixed test, otherwise generate a
* run-time test
*/
if (s > 0)
{
/* positive constant - we're looping up to the 'to' limit */
G_cg->write_op(OPC_JGT);
G_cs->write_ofs2(endlbl, 0);
/* JGT pops 2 */
G_cg->note_pop(2);
}
else if (s < 0)
{
/* negative constant - we're looping down to the 'to' limit */
G_cg->write_op(OPC_JLT);
G_cs->write_ofs2(endlbl, 0);
/* JLT pops 2 */
G_cg->note_pop(2);
}
else
{
/*
* Variable step - generate a run-time test:
*
*. ; test the sign of the step expression: if step < 0 goto $1
*. push <step>
*. push 0
*. jlt $1
*. ; positive branch: if lval > to goto endlbl else goto $2
*. jgt endlbl
*. jmp $2
*. ; negative branch: if lval < to goto endlbl
*. $1:
*. jlt endlbl
*. ; end of test
*. $2:
*/
/* push <step>; push 0; jlt $1 */
step_expr_->gen_code(FALSE, FALSE);
G_cg->write_op(OPC_PUSH_0);
G_cg->note_push();
CTcCodeLabel *l1 = gen_jump_ahead(OPC_JLT);
G_cg->note_pop(2);
/* positive branch: jgt endlbl; jmp $2 */
G_cg->write_op(OPC_JGT);
G_cs->write_ofs2(endlbl, 0);
CTcCodeLabel *l2 = gen_jump_ahead(OPC_JMP);
/* negative branch: $1: jlt endlbl */
def_label_pos(l1);
G_cg->write_op(OPC_JLT);
G_cs->write_ofs2(endlbl, 0);
/* $2: */
def_label_pos(l2);
/*
* we took one or the other of the positive or negative branch, and
* each one did a JxT that popped two elements
*/
G_cg->note_pop(2);
}
}
/*
* Generate code for the reinit portion of the 'for'. We add 'step' to our
* lvalue.
*/
void CTPNVarInRange::gen_forstm_reinit()
{
/*
* If we have a step expression, generate "lval += step". Otherwise
* just generate "++lval".
*/
if (step_expr_ != 0)
{
/* generate "lval += step" */
CTPNAddAsi_gen_code(lval_, step_expr_, TRUE);
}
else
{
/* increment the control variable */
CTPNPreInc_gen_code(lval_, TRUE);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'foreach' statement
*/
/*
* generate code
*/
void CTPNStmForeach::gen_code(int, int)
{
/* set my local frame if necessary */
CTcPrsSymtab *old_frame = G_cs->set_local_frame(symtab_);
/*
* add a line record - note that we add the line record after
* setting up the local frame, so that the 'for' statement itself
* appears within its own inner scope
*/
add_debug_line_rec();
/* push the enclosing statement */
CTPNStmEnclosing *old_enclosing = G_cs->set_enclosing(this);
/* if there's a collection expression, generate it */
if (coll_expr_ != 0)
CTPNVarIn::gen_iter_init(coll_expr_, iter_local_id_, "foreach");
/* set the label for the top of the loop */
CTcCodeLabel *top_lbl = new_label_here();
/* check for and fetch the next value */
CTcCodeLabel *end_lbl = 0;
CTPNVarIn::gen_iter_cond(iter_expr_, iter_local_id_, end_lbl, "foreach");
/*
* set our labels, so that 'break' and 'continue' statements in our
* body will know where to go
*/
break_lbl_ = end_lbl;
cont_lbl_ = top_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/*
* add another line record - we're now generating code again for the
* original 'foreach' line, even though it's after the body
*/
//$$$ add_debug_line_rec();
/* jump back to the top of the loop */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(top_lbl, 0);
/*
* we're at the end of the loop - this is where we jump for 'break'
* and when the condition becomes false
*/
if (end_lbl != 0)
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
/* restore the enclosing local scope */
G_cs->set_local_frame(old_frame);
}
/* ------------------------------------------------------------------------ */
/*
* 'while' statement
*/
/*
* generate code
*/
void CTPNStmWhile::gen_code(int, int)
{
CTcCodeLabel *top_lbl;
CTcCodeLabel *end_lbl;
CTPNStmEnclosing *old_enclosing;
/* add a line record */
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/* set the label for the top of the loop */
top_lbl = new_label_here();
/* generate a label for the end of the loop */
end_lbl = G_cs->new_label_fwd();
/* generate the condition, jumping to the end of the loop if false */
cond_expr_->gen_code_cond(0, end_lbl);
/*
* set the 'break' and 'continue' label in our node, so that 'break'
* and 'continue' statements in subnodes can find the labels during
* code generation
*/
break_lbl_ = end_lbl;
cont_lbl_ = top_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/*
* add another line record - the jump back to the top of the loop is
* part of the 'while' itself
*/
//$$$ add_debug_line_rec();
/* jump back to the top of the loop */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(top_lbl, 0);
/*
* we're at the end of the loop - this is where we jump for 'break'
* and when the condition becomes false
*/
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
}
/* ------------------------------------------------------------------------ */
/*
* 'do-while' statement
*/
/*
* generate code
*/
void CTPNStmDoWhile::gen_code(int, int)
{
CTcCodeLabel *top_lbl;
CTcCodeLabel *end_lbl;
CTcCodeLabel *cont_lbl;
CTPNStmEnclosing *old_enclosing;
/* add a line record */
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/* set the label for the top of the loop */
top_lbl = new_label_here();
/* create a label for after the loop, for any enclosed 'break's */
end_lbl = G_cs->new_label_fwd();
/*
* create a label for just before the expression, for any enclosed
* 'continue' statements
*/
cont_lbl = G_cs->new_label_fwd();
/* set our 'break' and 'continue' labels in our node */
break_lbl_ = end_lbl;
cont_lbl_ = cont_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/* set the debug source position to the 'while' clause's location */
add_debug_line_rec(while_desc_, while_linenum_);
/* put the 'continue' label here, just before the condition */
def_label_pos(cont_lbl);
/*
* Generate the condition. If the condition is true, jump back to the
* top label; otherwise fall through out of the loop structure.
*/
cond_expr_->gen_code_cond(top_lbl, 0);
/* we're past the end of the loop - this is where we jump for 'break' */
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
}
/* ------------------------------------------------------------------------ */
/*
* 'break' statement
*/
/*
* generate code
*/
void CTPNStmBreak::gen_code(int, int)
{
/* add a line record */
add_debug_line_rec();
/*
* ask the enclosing statement to do the work - if there's no
* enclosing statement, or none of the enclosing statements can
* perform the break, it's an error
*/
if (G_cs->get_enclosing() == 0
|| !G_cs->get_enclosing()->gen_code_break(lbl_, lbl_len_))
{
/*
* log the error - if there's a label, the problem is that we
* couldn't find the label, otherwise it's that we can't perform
* a 'break' here at all
*/
if (lbl_ == 0)
G_tok->log_error(TCERR_INVALID_BREAK);
else
G_tok->log_error(TCERR_INVALID_BREAK_LBL, (int)lbl_len_, lbl_);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'continue' statement
*/
/*
* generate code
*/
void CTPNStmContinue::gen_code(int, int)
{
/* add a line record */
add_debug_line_rec();
/*
* ask the enclosing statement to do the work - if there's no
* enclosing statement, or none of the enclosing statements can
* perform the break, it's an error
*/
if (G_cs->get_enclosing() == 0
|| !G_cs->get_enclosing()->gen_code_continue(lbl_, lbl_len_))
{
/*
* log the error - if there's a label, the problem is that we
* couldn't find the label, otherwise it's that we can't perform
* a 'break' here at all
*/
if (lbl_ == 0)
G_tok->log_error(TCERR_INVALID_CONTINUE);
else
G_tok->log_error(TCERR_INVALID_CONT_LBL, (int)lbl_len_, lbl_);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'switch' statement
*/
/*
* generate code
*/
void CTPNStmSwitch::gen_code(int, int)
{
CTPNStmSwitch *enclosing_switch;
int i;
char buf[VMB_DATAHOLDER + VMB_UINT2];
CTcCodeLabel *end_lbl;
CTPNStmEnclosing *old_enclosing;
/* add a line record */
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/*
* Generate the controlling expression. We want to keep the value,
* hence 'discard' is false, and we need assignment (not 'for
* condition') conversion rules, because we're going to use the
* value in direct comparisons
*/
expr_->gen_code(FALSE, FALSE);
/* make myself the current innermost switch */
enclosing_switch = G_cs->set_switch(this);
/*
* if we can flow out of the switch, allocate a label for the end of
* the switch body
*/
if ((get_control_flow(FALSE) & TCPRS_FLOW_NEXT) != 0)
end_lbl = G_cs->new_label_fwd();
else
end_lbl = 0;
/* the end label is the 'break' location for subnodes */
break_lbl_ = end_lbl;
/*
* Write my SWITCH opcode, and the placeholder case table. We'll
* fill in the case table with its real values as we encounter the
* cases in the course of generating the code. For now, all we know
* is the number of cases we need to put into the table.
*/
G_cg->write_op(OPC_SWITCH);
/* the SWITCH opcode pops the controlling expression value */
G_cg->note_pop();
/* write the number of cases */
G_cs->write2(case_cnt_);
/*
* remember where the first case slot is - the 'case' parse nodes
* will use this to figure out where to write their slot data
*/
case_slot_ofs_ = G_cs->get_ofs();
/*
* Write the placeholder case slots - each case slot gets a
* DATA_HOLDER for the case value, plus an INT2 for the branch
* offset. For now, completely zero each case slot.
*/
memset(buf, 0, VMB_DATAHOLDER + VMB_UINT2);
for (i = 0 ; i < case_cnt_ ; ++i)
G_cs->write(buf, VMB_DATAHOLDER + VMB_UINT2);
/* write a placeholder for the default jump */
if (has_default_)
{
/*
* remember where the 'default' slot is, so that the 'default'
* parse node can figure out where to write its branch offset
*/
default_slot_ofs_ = G_cs->get_ofs();
/*
* Write the placeholder for the 'default' slot - this just gets
* an INT2 for the 'default' jump offset. As with the case
* labels, just zero it for now; we'll fill it in later when we
* encounter the 'default' case.
*/
G_cs->write2(0);
}
else
{
/*
* there's no default slot, so the 'default' slot is simply a
* jump to the end of the switch body - generate a jump ahead to
* our end label
*/
G_cs->write_ofs2(end_lbl, 0);
}
/*
* generate the switch body - this will fill in the case table as we
* encounter the 'case' nodes in the parse tree
*/
if (body_ != 0)
gen_code_substm(body_);
/*
* We're past the body - if we have an end label, set it here. (We
* won't have created an end label if control can't flow out of the
* switch; this allows us to avoid generating unreachable instructions
* after the switch, which would only increase the code size for no
* reason.)
*/
if (end_lbl != 0)
def_label_pos(end_lbl);
/* restore the enclosing switch */
G_cs->set_switch(enclosing_switch);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
}
/* ------------------------------------------------------------------------ */
/*
* 'case' label statement
*/
/*
* generate code
*/
void CTPNStmCase::gen_code(int, int)
{
ulong slot_ofs;
ulong jump_ofs;
/*
* we must have an active 'switch' statement, and our expression
* value must be a constant -- if either of these is not true, we
* have an internal error of some kind, because we should never get
* this far if these conditions weren't true
*/
if (G_cs->get_switch() == 0 || !expr_->is_const())
G_tok->throw_internal_error(TCERR_GEN_BAD_CASE);
/* allocate our case slot from the enclosing 'switch' statement */
slot_ofs = G_cs->get_switch()->alloc_case_slot();
/* write the case table entry as a DATAHOLDER value */
G_cg->write_const_as_dh(G_cs, slot_ofs, expr_->get_const_val());
/*
* Add the jump offset. This is the offset from this INT2 entry in
* our case slot to the current output offset. The INT2 is offset
* from the start of our slot by the DATAHOLDER value.
*/
jump_ofs = G_cs->get_ofs() - (slot_ofs + VMB_DATAHOLDER);
G_cs->write2_at(slot_ofs + VMB_DATAHOLDER, (int)jump_ofs);
/*
* because we can jump here (via the case table), we cannot allow
* peephole optimizations from past instructions - clear the
* peephole
*/
G_cg->clear_peephole();
/* generate our substatement, if we have one */
if (stm_ != 0)
gen_code_substm(stm_);
}
/* ------------------------------------------------------------------------ */
/*
* 'default' label statement
*/
/*
* generate code
*/
void CTPNStmDefault::gen_code(int, int)
{
ulong slot_ofs;
char buf[VMB_UINT2];
ulong jump_ofs;
/*
* we must have an active 'switch' statement -- if we don't, we have
* an internal error of some kind, because we should never have
* gotten this far
*/
if (G_cs->get_switch() == 0)
G_tok->throw_internal_error(TCERR_GEN_BAD_CASE);
/* ask the switch where our slot goes */
slot_ofs = G_cs->get_switch()->get_default_slot();
/*
* Set the jump offset. This is the offset from our slot entry in
* the case table to the current output offset.
*/
jump_ofs = G_cs->get_ofs() - slot_ofs;
oswp2(buf, (int)jump_ofs);
/* write our slot entry to the case table */
G_cs->write_at(slot_ofs, buf, VMB_UINT2);
/*
* because we can jump here (via the case table), we cannot allow
* peephole optimizations from past instructions - clear the
* peephole
*/
G_cg->clear_peephole();
/* generate our substatement, if we have one */
if (stm_ != 0)
gen_code_substm(stm_);
}
/* ------------------------------------------------------------------------ */
/*
* code label statement
*/
/*
* ininitialize
*/
CTPNStmLabel::CTPNStmLabel(CTcSymLabel *lbl, CTPNStmEnclosing *enclosing)
: CTPNStmLabelBase(lbl, enclosing)
{
/*
* we don't have a 'goto' label yet - we'll allocate it on demand
* during code generation (labels are local in scope to a code body
* so we can't allocate this until code generation begins for our
* containing code body)
*/
goto_label_ = 0;
/*
* we don't yet have a 'break' label - we'll allocate this when
* someone first refers to it
*/
break_label_ = 0;
}
/*
* get our code label
*/
CTcCodeLabel *CTPNStmLabel::get_goto_label()
{
/* if we don't have a label already, allocate it */
if (goto_label_ == 0)
goto_label_ = G_cs->new_label_fwd();
/* return the label */
return goto_label_;
}
/*
* generate code
*/
void CTPNStmLabel::gen_code(int, int)
{
CTPNStmEnclosing *old_enclosing;
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/*
* Define our label position - this is where we come if someone does
* a 'goto' to this label. (Note that we might not have a 'goto'
* label defined yet - if we weren't forward-referenced by a 'goto'
* statement, we won't have a label defined. Call get_goto_label()
* to ensure we create a label if it doesn't already exist.)
*/
def_label_pos(get_goto_label());
/*
* add the source location of the label - this probably will have no
* effect, since we don't generate any code for the label itself,
* but it's harmless so do it anyway to guard against weird cases
*/
add_debug_line_rec();
/*
* generate code for the labeled statement, discarding any
* calculated value
*/
if (stm_ != 0)
gen_code_substm(stm_);
/*
* If we have a 'break' label, it means that code within our labeled
* statement (i.e., nested within the label) did a 'break' to leave
* the labeled statement. The target of the break is the next
* statement after the labeled statement, which comes next, so
* define the label here.
*/
if (break_label_ != 0)
def_label_pos(break_label_);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
}
/*
* generate code for a 'break'
*/
int CTPNStmLabel::gen_code_break(const textchar_t *lbl, size_t lbl_len)
{
/*
* If the 'break' doesn't specify a label, inherit the default
* handling, since we're not a default 'break' target. If there's a
* label, and the label isn't our label, also inherit the default,
* since the target lies somewhere else.
*/
if (lbl == 0 || G_cs->get_goto_symtab() == 0
|| G_cs->get_goto_symtab()->find(lbl, lbl_len) != lbl_)
return CTPNStmLabelBase::gen_code_break(lbl, lbl_len);
/*
* if we don't yet have a 'break' label defined, define one now
* (it's a forward declaration, because we won't know where it goes
* until we finish generating the entire body of the statement
* contained in the label)
*/
if (break_label_ == 0)
break_label_ = G_cs->new_label_fwd();
/* jump to the label */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(break_label_, 0);
/* handled */
return TRUE;
}
/*
* generate code for a 'continue'
*/
int CTPNStmLabel::gen_code_continue(const textchar_t *lbl, size_t lbl_len)
{
/*
* If there's no label, inherit the default handling, since we're
* not a default 'continue' target. If there's a label, and the
* label isn't our label, also inherit the default, since the target
* lies somewhere else.
*/
if (lbl == 0 || G_cs->get_goto_symtab() == 0
|| G_cs->get_goto_symtab()->find(lbl, lbl_len) != lbl_)
return CTPNStmLabelBase::gen_code_continue(lbl, lbl_len);
/*
* It's a 'continue' with my label - ask my enclosed statement to do
* the work; return failure if I have no enclosed statement. Note
* that we use a special call - generate a *labeled* continue - to
* let the statement know that it must perform the 'continue'
* itself and cannot defer to enclosing statements.
*/
if (stm_ != 0)
return stm_->gen_code_labeled_continue();
else
return FALSE;
}
/* ------------------------------------------------------------------------ */
/*
* 'try'
*/
/*
* generate code
*/
void CTPNStmTry::gen_code(int, int)
{
ulong start_ofs;
ulong end_ofs;
CTPNStmCatch *cur_catch;
CTcCodeLabel *end_lbl;
CTPNStmEnclosing *old_enclosing;
int finally_never_returns;
/* we have no end label yet */
end_lbl = 0;
/*
* add the source location of the 'try' - it probably won't be
* needed, because we don't generate any code before the protected
* body, but it's harmless and makes sure we have a good source
* location in weird cases
*/
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/*
* If we have a 'finally' clause, we must allocate a
* forward-reference code label for it. We need to be able to reach
* the 'finally' clause throughout generation of the protected code
* and the 'catch' blocks.
*/
if (finally_stm_ != 0)
finally_lbl_ = G_cs->new_label_fwd();
else
finally_lbl_ = 0;
/* note where the protected code begins */
start_ofs = G_cs->get_ofs();
/* generate the protected code */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/*
* Check to see if we have a 'finally' block that never returns. If we
* have a 'finally' block, and it doesn't flow to its next statement,
* then our LJSR's to the 'finally' block will never return.
*/
finally_never_returns =
(finally_stm_ != 0
&& (finally_stm_->get_control_flow(FALSE) & TCPRS_FLOW_NEXT) == 0);
/*
* if there's a "finally" clause, we must generate a local subroutine
* call to the "finally" block
*/
gen_jsr_finally();
/*
* We must now jump past the "catch" and "finally" code blocks. If the
* "finally" block itself doesn't flow to the next statement, then
* there's no need to do this, since we'll never be reached here. If
* there's no "finally" block, then we won't have LJSR'd anywhere, so
* this code is definitely reachable.
*/
if (!finally_never_returns)
end_lbl = gen_jump_ahead(OPC_JMP);
/*
* Note where the protected code ends - it ends at one byte below
* the current write offset, because the current write offset is the
* next byte we'll write. The code range we store in the exception
* table is inclusive of the endpoints.
*/
end_ofs = G_cs->get_ofs() - 1;
/* generate the 'catch' blocks */
for (cur_catch = first_catch_stm_ ; cur_catch != 0 ;
cur_catch = cur_catch->get_next_catch())
{
/* generate the 'catch' block */
cur_catch->gen_code_catch(start_ofs, end_ofs);
/* call the 'finally' block after the 'catch' finishes */
gen_jsr_finally();
/*
* If there's a finally block, or there's another 'catch' after me,
* generate a jump past the remaining catch/finally blocks.
*
* If we do have a finally that doesn't flow to the next statement
* (i.e., we throw or return out of the finally), then there's no
* need to generate a jump, since we'll never come back here from
* the finally block.
*/
if (!finally_never_returns
&& (finally_stm_ != 0 || cur_catch->get_next_catch() != 0))
{
/*
* if we have no end label yet, generate one now - we might
* not have one because we might not have been able to reach
* any previous jump to the end of the catch (because we threw
* or returned out of the end of all blocks to this point, for
* example)
*/
if (end_lbl == 0)
{
/* we have no label - generate a new one now */
end_lbl = gen_jump_ahead(OPC_JMP);
}
else
{
/* we already have an end label - generate a jump to it */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(end_lbl, 0);
}
}
}
/*
* Restore the enclosing statement. We enclose the protected code
* and all of the 'catch' blocks, because all of these must leave
* through the 'finally' handler. We do not, however, enclose the
* 'finally' handler itself - once it's entered, we do not invoke it
* again as it leaves.
*/
G_cs->set_enclosing(old_enclosing);
/* generate the 'finally' block, if we have one */
if (finally_stm_ != 0)
{
/*
* Generate the 'finally' code. The 'finally' block is executed
* for the 'try' block plus all of the 'catch' blocks, so the
* ending offset is the current position (less one byte, since
* the range is inclusive), which encompasses all of the 'catch'
* blocks
*/
finally_stm_->gen_code_finally(start_ofs, G_cs->get_ofs() - 1, this);
}
/*
* we're now past all of the "catch" and "finally" blocks, so we can
* define the jump label for jumping past those blocks (we make this
* jump from the end of the protected code) - note that we might not
* have actually generated the label, since we might never have
* reached any code which jumped to it
*/
if (end_lbl != 0)
def_label_pos(end_lbl);
}
/*
* Generate code for a 'break' within our protected code or a 'catch'.
* We'll first generate a call to our 'finally' block, if we have one,
* then let the enclosing statement handle the break.
*/
int CTPNStmTry::gen_code_break(const textchar_t *lbl, size_t lbl_len)
{
/* if we have a 'finally' block, invoke it as a local subroutine call */
gen_jsr_finally();
/*
* If there's an enclosing statement, let it generate the break; if
* there's not, return failure, because we're not a meaningful
* target for break.
*/
if (enclosing_)
return enclosing_->gen_code_break(lbl, lbl_len);
else
return FALSE;
}
/*
* Generate code for a 'break' within our protected code or a 'catch'.
* We'll first generate a call to our 'finally' block, if we have one,
* then let the enclosing statement handle the break.
*/
int CTPNStmTry::gen_code_continue(const textchar_t *lbl, size_t lbl_len)
{
/* if we have a 'finally' block, invoke it as a local subroutine call */
gen_jsr_finally();
/*
* if there's an enclosing statement, let it generate the continue;
* if there's not, return failure, because we're not a meaningful
* target for continue
*/
if (enclosing_)
return enclosing_->gen_code_continue(lbl, lbl_len);
else
return FALSE;
}
/*
* Generate a local subroutine call to our 'finally' block, if we have
* one. This should be used when executing a break, continue, goto, or
* return out of the protected code or a 'catch' block, or when merely
* falling off the end of the protected code or 'catch' block.
*/
void CTPNStmTry::gen_jsr_finally()
{
/* if we have a 'finally', call it */
if (finally_lbl_ != 0)
{
/* generate the local subroutine call */
G_cg->write_op(OPC_LJSR);
G_cs->write_ofs2(finally_lbl_, 0);
/*
* the LJSR pushes a value, which is then immediately popped (we
* must note the push and pop because it affects our maximum
* stack depth requirement)
*/
G_cg->note_push();
G_cg->note_pop();
/*
* whatever follows the LJSR is logically at the end of the
* 'finally' block
*/
add_debug_line_rec(finally_stm_->get_end_desc(),
finally_stm_->get_end_linenum());
}
}
/* ------------------------------------------------------------------------ */
/*
* 'catch'
*/
/*
* generate code
*/
void CTPNStmCatch::gen_code(int, int)
{
/* this can't be called directly - use gen_code_catch() instead */
G_tok->throw_internal_error(TCERR_CATCH_FINALLY_GEN_CODE);
}
/*
* generate code for the 'catch'
*/
void CTPNStmCatch::gen_code_catch(ulong start_prot_ofs, ulong end_prot_ofs)
{
CTcSymbol *sym;
ulong exc_obj_id;
CTcPrsSymtab *old_frame;
/* add the source location of the 'catch' clause */
add_debug_line_rec();
/* set the local scope */
old_frame = G_cs->set_local_frame(symtab_);
/* look up the object defining the class of exceptions to catch */
sym = G_cs->get_symtab()->find_or_def_undef(exc_class_,
exc_class_len_, FALSE);
/* assume we won't find a valid object ID */
exc_obj_id = VM_INVALID_OBJ;
/* if it's an object, get its ID */
if (sym->get_type() == TC_SYM_OBJ)
{
/* get its object ID */
exc_obj_id = ((CTcSymObj *)sym)->get_obj_id();
}
else if (sym->get_type() != TC_SYM_UNKNOWN)
{
/*
* it's defined, but it's not an object - log an error (note
* that we don't log an error if the symbol is undefined,
* because find_or_def_undef() will already have logged an error
* for us)
*/
log_error(TCERR_CATCH_EXC_NOT_OBJ, (int)exc_class_len_, exc_class_);
}
/* add our exception table entry */
G_cg->get_exc_table()->add_catch(start_prot_ofs, end_prot_ofs,
exc_obj_id, G_cs->get_ofs());
/* don't allow any peephole optimizations to affect this offset */
G_cg->clear_peephole();
/*
* the VM automatically pushes a value onto the stack to perform the
* 'catch'
*/
G_cg->note_push();
/*
* generate a SETLCL for our formal parameter, so that the exception
* object is stored in our local variable
*/
exc_var_->gen_code_setlcl();
/* generate code for our statement, if we have one */
if (body_ != 0)
gen_code_substm(body_);
/* restore the enclosing local scope */
G_cs->set_local_frame(old_frame);
}
/* ------------------------------------------------------------------------ */
/*
* 'finally'
*/
/*
* generate code
*/
void CTPNStmFinally::gen_code(int, int)
{
/* this can't be called directly - use gen_code_finally() instead */
G_tok->throw_internal_error(TCERR_CATCH_FINALLY_GEN_CODE);
}
/*
* generate code for the 'finally'
*/
void CTPNStmFinally::gen_code_finally(ulong start_prot_ofs,
ulong end_prot_ofs,
CTPNStmTry *try_stm)
{
CTPNStmEnclosing *old_enclosing;
/*
* set the source location for our prolog code to the 'finally'
* clause's start
*/
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/*
* add our exception table entry - use the invalid object ID as a
* special flag to indicate that we catch all exceptions
*/
G_cg->get_exc_table()->add_catch(start_prot_ofs, end_prot_ofs,
VM_INVALID_OBJ, G_cs->get_ofs());
/* don't allow any peephole optimizations to affect this offset */
G_cg->clear_peephole();
/* the VM pushes the exception onto the stack before calling us */
G_cg->note_push();
/*
* When we get called due to an exception, we want to run the
* 'finally' code block and then re-throw the exception. First, store
* the exception parameter in our special local stack slot that we
* allocated specifically for the purpose of being a temporary holder
* for this value.
*/
CTcSymLocal::s_gen_code_setlcl_stk(exc_local_id_, FALSE);
/* call the 'finally' block */
try_stm->gen_jsr_finally();
/*
* After the 'finally' block returns, we must re-throw the
* exception. Retrieve the contents of our local where we stashed
* the exception object and re-throw the exception.
*/
CTcSymLocal::s_gen_code_getlcl(exc_local_id_, FALSE);
/* re-throw the exception - this pops the exception object */
G_cg->write_op(OPC_THROW);
G_cg->note_pop();
/*
* set the source location to the 'finally' clause once again, since
* we changed the source location in the course of generating the
* catch-all handler
*/
add_debug_line_rec();
/* this is where the 'finally' code block begins - define our label */
def_label_pos(try_stm->get_finally_lbl());
/*
* The 'finally' block is the target of LJSR instructions, since we
* must run the 'finally' block's code from numerous code paths.
* The first thing we must do is pop the return address and stash it
* in a local variable. (We note an extra push, since the LJSR will
* have pushed the value before transferring control here.)
*/
G_cg->note_push();
CTcSymLocal::s_gen_code_setlcl_stk(jsr_local_id_, FALSE);
/* generate the code block, if there is one */
if (body_ != 0)
gen_code_substm(body_);
/* return from the 'finally' subroutine */
G_cg->write_op(OPC_LRET);
G_cs->write2(jsr_local_id_);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
}
/*
* It is not legal to enter a 'finally' block via a 'goto' statement,
* because there is no valid way to exit the 'finally' block in this
* case.
*/
int CTPNStmFinally::check_enter_by_goto(CTPNStmGoto *goto_stm,
CTPNStmLabel *)
{
/* this is illegal - log an error */
goto_stm->log_error(TCERR_GOTO_INTO_FINALLY);
/* indicate that it's not allowed */
return FALSE;
}
/* ------------------------------------------------------------------------ */
/*
* 'throw' statement
*/
/*
* generate code
*/
void CTPNStmThrow::gen_code(int, int)
{
/* add a line record */
add_debug_line_rec();
/*
* generate our expression - we use the result (discard = false),
* and we are effectively assigning the result, so we can't use the
* 'for condition' rules
*/
expr_->gen_code(FALSE, FALSE);
/* generate the 'throw' */
G_cg->write_op(OPC_THROW);
/* 'throw' pops the expression from the stack */
G_cg->note_pop();
}
/* ------------------------------------------------------------------------ */
/*
* 'goto' statement
*/
/*
* generate code
*/
void CTPNStmGoto::gen_code(int, int)
{
CTcSymbol *sym;
CTPNStmLabel *label_stm;
/* add a line record */
add_debug_line_rec();
/*
* look up our label symbol in the 'goto' table for the function,
* and get the label statement node from the label
*/
if (G_cs->get_goto_symtab() == 0
|| (sym = G_cs->get_goto_symtab()->find(lbl_, lbl_len_)) == 0
|| sym->get_type() != TC_SYM_LABEL
|| (label_stm = ((CTcSymLabel *)sym)->get_stm()) == 0)
{
/* log an error */
log_error(TCERR_INVALID_GOTO_LBL, (int)lbl_len_, lbl_);
/* give up */
return;
}
/*
* Tell any enclosing statements to unwind their 'try' blocks for a
* transfer to the given label. We only need to go as far as the
* most deeply nested enclosing statement we have in common with the
* label, because we'll be transferring control entirely within the
* confines of that enclosing statement.
*/
if (G_cs->get_enclosing() != 0)
{
/* generate the unwinding code */
G_cs->get_enclosing()->gen_code_unwind_for_goto(this, label_stm);
}
else
{
CTPNStmEnclosing *enc;
/*
* The 'goto' isn't enclosed in any statements. This means that
* we are entering every block that contains the target label.
* Some blocks don't allow entering via 'goto', so we must check
* at this point to see if any of the enclosing blocks are
* problematic.
*/
for (enc = label_stm ; enc != 0 ; enc = enc->get_enclosing())
{
/*
* make sure we're allowed to enter this statement - if not,
* stop scanning, so that we display only one such error
*/
if (!enc->check_enter_by_goto(this, label_stm))
break;
}
}
/* generate a jump to the label */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(label_stm->get_goto_label(), 0);
}
/* ------------------------------------------------------------------------ */
/*
* Generic enclosing statement node
*/
/*
* Generate code for a break, given the target code label object and the
* target label symbol, if any. This can be used for any of the looping
* statement types.
*/
int CTPNStmEnclosing::gen_code_break_loop(CTcCodeLabel *code_label,
const textchar_t *lbl,
size_t lbl_len)
{
/*
* If the statement is labeled, let the enclosing statement handle
* it -- since it's labeled, we can't assume the statement refers to
* us without searching for the enclosing label.
*/
if (lbl != 0)
{
/* if there's an enclosing statement, let it handle it */
if (enclosing_ != 0)
return enclosing_->gen_code_break(lbl, lbl_len);
/*
* there's no enclosing statement, and we can't handle this
* because it has an explicit label attached - indicate that no
* break has been generated and give up
*/
return FALSE;
}
/*
* It's unlabeled, so we can take it by default as the nearest
* enclosing statement for which 'break' makes sense -- generate the
* jump to the given code label.
*/
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(code_label, 0);
/* we have generated the break */
return TRUE;
}
/*
* Generate code for a continue, given the target code label object and
* the target label symbol, if any. This can be used for any of the
* looping statement types.
*/
int CTPNStmEnclosing::gen_code_continue_loop(CTcCodeLabel *code_label,
const textchar_t *lbl,
size_t lbl_len)
{
/*
* If the statement is labeled, let the enclosing statement handle
* it -- since it's labeled, we can't assume the statement refers to
* us without searching for the enclosing label.
*/
if (lbl != 0)
{
/* if there's an enclosing statement, let it handle it */
if (enclosing_ != 0)
return enclosing_->gen_code_continue(lbl, lbl_len);
/*
* there's no enclosing statement, and we can't handle this
* because it has an explicit label attached - indicate that no
* continue has been generated and give up
*/
return FALSE;
}
/*
* it's unlabeled, so we can take it by default as the nearest
* enclosing statement for which 'continue' makes sense -- generate
* the jump to the given code label
*/
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(code_label, 0);
/* we have generated the continue */
return TRUE;
}
/*
* Generate the code necessary to unwind the stack for executing a
* 'goto' to the given labeled statement>
*/
void CTPNStmEnclosing::gen_code_unwind_for_goto(CTPNStmGoto *goto_stm,
CTPNStmLabel *target)
{
CTPNStmEnclosing *enc;
/*
* Detmerine if the target statement is enclosed within this
* statement. If it is, we do not need to unwind from this
* statement or any of its enclosing statements, because control
* will remain within this statement.
*
* To make this determination, start at the target label and search
* up its list of enclosing statements. If we reach 'this', we know
* that we enclose the target. If we reach the outermost enclosing
* statement, we know that we do not enclose the taret.
*/
for (enc = target ; enc != 0 ; enc = enc->get_enclosing())
{
/*
* if we found ourself in the list of enclosing statements
* around the target label, the label is contained within us,
* hence we do not need to generate any code to leave, because
* we're not leaving - simply return immediately without looking
* any further
*/
if (enc == this)
{
/*
* 'this' is the common ancestor of both the 'goto' and the
* target label, so we're not transferring control in or out
* of 'this' or any enclosing statement. However, we are
* transfering control IN through all of the statements that
* enclose the label up to but not including 'this'.
*
* Some types of statements do not allow control transfers
* in to enclosed labels - in particular, we can't use
* 'goto' to transfer control into a 'finally' clause.
* Check all statements that enclose the label up to but not
* including 'this', and make sure they will allow a
* transfer in.
*
* Note that we make this check now, only after we've found
* the common ancestor, because we can't tell if we're
* actually entering any blocks until we find the common
* ancestor.
*/
for (enc = target ; enc != 0 && enc != this ;
enc = enc->get_enclosing())
{
/* make sure we're allowed to enter this statement */
if (!enc->check_enter_by_goto(goto_stm, target))
break;
}
/*
* we're not transferring out of this statement or any
* enclosing statement, since the source 'goto' and the
* target label are both contained within 'this' - we're
* done unwinding for the transfer
*/
return;
}
}
/* generate code to transfer out of this statement */
gen_code_for_transfer_out();
/* check for an enclosing statement */
if (enclosing_ != 0)
{
/*
* We are enclosed by another statement or statements. This
* means that we haven't found a common ancestor yet, so we
* might be leaving the enclosing block as well - continue on to
* our enclosing statement.
*/
enclosing_->gen_code_unwind_for_goto(goto_stm, target);
}
else
{
/*
* This is the outermost significant enclosing statement, which
* means that we are transferring control into a completely
* unrelated block. As a result, we will enter every statement
* that encloses the target label.
*
* We must check each block we're entering to see if it allows
* entry by 'goto' statements. Since we now know there is no
* common ancestor, and thus that we're entering every block
* enclosing the target label, we must check every block
* enclosing the target label to see if they allow transfers in
* via 'goto' statements.
*/
for (enc = target ; enc != 0 ; enc = enc->get_enclosing())
{
/* make sure we're allowed to enter this statement */
if (!enc->check_enter_by_goto(goto_stm, target))
break;
}
}
}
/* ------------------------------------------------------------------------ */
/*
* Anonymous function
*/
/*
* generate code
*/
void CTPNAnonFunc::gen_code(int discard, int)
{
CTcCodeBodyCtx *cur_ctx;
int argc;
/* if we're discarding the value, don't bother generating the code */
if (discard)
return;
/*
* Push each context object - these are the additional arguments to
* the anonymous function pointer object's constructor beyond the
* function pointer itself. Note that we must push the arguments in
* reverse order of our list, since arguments are always pushed from
* last to first.
*/
for (argc = 0, cur_ctx = code_body_->get_ctx_tail() ; cur_ctx != 0 ;
cur_ctx = cur_ctx->prv_, ++argc)
{
int our_varnum;
/*
* find our context matching this context - the caller's
* contexts are all one level lower than the callee's contexts,
* because the caller is at the next recursion level out
*/
if (!G_cs->get_code_body()
->get_ctx_var_for_level(cur_ctx->level_ - 1, &our_varnum))
{
/* this should never happen */
assert(FALSE);
}
/*
* push this context object - to do this, simply retrieve the
* value of the local variable in our frame that contains this
* context level
*/
CTcSymLocal::s_gen_code_getlcl(our_varnum, FALSE);
}
/*
* The first argument (and thus last pushed) to the constructor is the
* constant function pointer that refers to the code of the anonymous
* function.
*
* For regular static compilation, the function pointer is a simple
* code offset constant. For run-time compilation (in the debugger or
* interpreter "eval()" facility), it's a DynamicFunc representing the
* dynamically compiled code.
*/
if (G_cg->is_eval_for_dyn())
{
/*
* Dynamic (run-time) compilation - the bytecode is in a
* dynamically created DynamicFunc instance. The dynamic compiler
* assigns object IDs for the anonymous function code body before
* generating any code for any referencing object, so we simply
* generate a PUSHOBJ for the code body's object ID.
*/
G_cg->write_op(OPC_PUSHOBJ);
G_cs->write_obj_id(code_body_->get_dyn_obj_id());
}
else
{
/* conventional static compiler - the bytecode is in the code pool */
G_cg->write_op(OPC_PUSHFNPTR);
code_body_->add_abs_fixup(G_cs);
G_cs->write4(0);
}
/* count the argument */
++argc;
/* note the push of the function pointer argument */
G_cg->note_push();
/*
* If we have a local context, we need to create an anonymous function
* metaclass object that combines the function pointer with the local
* context. If we don't have a local context, this is a simple static
* function that we can call directly, in which case we're already done
* since we've pushed the function pointer.
*/
if (argc > 1 || code_body_->has_local_ctx())
{
/*
* We have a local context, so we need create to create the
* anonymous function metaclass object. Generate the NEW.
*/
if (argc <= 255)
{
G_cg->write_op(OPC_NEW1);
G_cs->write((char)argc);
G_cs->write((char)G_cg->get_predef_meta_idx(TCT3_METAID_ANONFN));
}
else
{
G_cg->write_op(OPC_NEW2);
G_cs->write2(argc);
G_cs->write2(G_cg->get_predef_meta_idx(TCT3_METAID_ANONFN));
}
/* push the object value */
G_cg->write_op(OPC_GETR0);
/* the 'new' popped the arguments, then we pushed the result */
G_cg->note_pop(argc - 1);
}
}
|