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
|
/******************************* -*- C -*- ****************************
*
* Functions for byte code optimization & analysis
*
*
***********************************************************************/
/***********************************************************************
*
* Copyright 2000, 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
* Written by Paolo Bonzini.
*
* This file is part of GNU Smalltalk.
*
* GNU Smalltalk is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any later
* version.
*
* Linking GNU Smalltalk statically or dynamically with other modules is
* making a combined work based on GNU Smalltalk. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* In addition, as a special exception, the Free Software Foundation
* give you permission to combine GNU Smalltalk with free software
* programs or libraries that are released under the GNU LGPL and with
* independent programs running under the GNU Smalltalk virtual machine.
*
* You may copy and distribute such a system following the terms of the
* GNU GPL for GNU Smalltalk and the licenses of the other code
* concerned, provided that you include the source code of that other
* code when and as the GNU GPL requires distribution of source code.
*
* Note that people who make modified versions of GNU Smalltalk are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version without
* this exception; this exception also makes it possible to release a
* modified version which carries forward this exception.
*
* GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* GNU Smalltalk; see the file COPYING. If not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
***********************************************************************/
#include "gstpriv.h"
#include "match.h"
/* Define this to have verbose messages from the JIT compiler's
basic-block split phase. */
/* #define DEBUG_JIT_TRANSLATOR */
/* Define this to have verbose messages from the bytecode verifier. */
/* #define DEBUG_VERIFIER */
/* Define this to disable the peephole bytecode optimizer. It works
well for decreasing the footprint and increasing the speed, so
there's no reason to do that unless you're debugging the compiler. */
/* #define NO_OPTIMIZE */
/* Define this to disable superoperators in the peephole bytecode
optimizer. Some simple optimizations will still be done, making
the output suitable for searching superoperator candidates. */
/* #define NO_SUPEROPERATORS */
/* Define this to disable bytecode verification. */
/* #define NO_VERIFIER */
/* The JIT compiler prefers optimized bytecodes, because they are
more regular. */
#ifdef ENABLE_JIT_TRANSLATION
#undef NO_OPTIMIZE
#endif
/* This structure and the following one are used by the bytecode
peephole optimizer.
This one, in particular, defines where basic blocks start in the
non- optimized bytecodes. byte is nothing more than an offset in
those bytecodes; id is used to pair jump bytecodes with their
destinations: in the initial scan, when we encounter a jump
bytecode we fill two block_boundaries -- one has positive id and
represents the destination of the jump, one has negative id (but
the same absolute value) and represents the jump bytecode
itself. */
typedef struct block_boundary
{
short byte;
short id;
}
block_boundary;
/* This structure defines how to fix the jumps after the optimized
basic blocks are put together. Everything is done after the
peephole pass because this allows us to handle forward jumps and
backward jumps in the same way.
When single blocks are optimized, the sorted block_boundaries are
examined one at a time. As we process blocks, we fill an array of
jump structures with offsets in the optimized bytecode. We fill a
single field at a time -- the id's sign in the block_boundary says
which field is to be filled, the absolute value gives which jump
structure is to be filled. In the end, block_boundaries whose id's
absolute value is the same are all paired. */
typedef struct jump
{
int from; /* where the jump bytecode lies */
int dest; /* where the jump bytecode lands */
}
jump;
/* Basic block data structure, common to the JIT and the verifier. */
typedef struct basic_block_item {
struct basic_block_item *next;
struct basic_block_item **bb;
gst_uchar *bp;
int sp;
/* Suspended basic blocks are those for which we know the initial
instruction pointer, but not the initial stack pointer. Since
data-flow analysis should walk them, these are put aside
momentarily.
They are generated when there is no basic block for the
bytecode after a jump or a return. If they are unreachable
and they follow a jump, they're supposed to have an initial
SP = 0, else the initial SP is put to the same as the
return bytecode's SP (this is to accomodate comp.c's
behavior when emitting "a ifTrue: [ ^1 ] ifFalse: [ ^2 ]").
However, the initial SP of a suspended block can always be
overridden if a jump to the block is found, in which case
the flag is cleared. Suspended basic blocks are processed
FIFO, not LIFO like the normal worklist. */
mst_Boolean suspended;
OOP stack[1];
} basic_block_item;
#define ALLOCA_BASIC_BLOCK(dest, depth, bp_, sp_) \
do \
{ \
*(dest) = alloca (sizeof (basic_block_item) + \
sizeof (OOP) * ((depth) - 1)); \
(*(dest))->bb = (dest); \
(*(dest))->bp = (bp_); \
(*(dest))->sp = (sp_); \
(*(dest))->suspended = false; \
} \
while (0)
#define INIT_BASIC_BLOCK(bb, temps) \
do \
{ \
int i; \
for (i = 0; i < (temps); i++) \
(bb)->stack[i] = FROM_INT (VARYING); \
for (; i < (bb)->sp; i++) \
(bb)->stack[i] = FROM_INT (UNDEFINED); \
} \
while (0)
/* Use the hash table and function in superop1.inl to look for a
superoperator representing bytecode BC1 with argument ARG, followed
by bytecode BC2. */
static inline int search_superop_fixed_arg_1 (int bc1,
int arg,
int bc2);
/* Use the hash table and function in superop1.inl to look for a
superoperator representing bytecode BC1 followed by bytecode BC2
with argument ARG. */
static inline int search_superop_fixed_arg_2 (int bc1,
int bc2,
int arg);
/* Scan the bytecodes between FROM and TO, performing a handful
of peephole optimizations. As they are overwritten with an
optimized version; then, superoperators are created with
optimize_superoperators and _gst_compile_bytecodes() is
used to append the final bytecodes to the stream of optimized
bytecodes. */
static void optimize_basic_block (gst_uchar * from,
gst_uchar * to);
/* Scan the peephole-optimized bytecodes between FROM and TO. */
gst_uchar *optimize_superoperators (gst_uchar * from,
gst_uchar * to);
/* This compares two block_boundary structures according to their
bytecode position. */
static int compare_blocks (const PTR a, const PTR b) ATTRIBUTE_PURE;
/* This answers how the dirtyness of BLOCKOOP affects
the block that encloses it. */
static inline int check_inner_block (OOP blockOOP);
/* This fills a table that says to which bytecodes a jump lands.
Starting from BP, and for a total of SIZE bytes, bytecodes are
analyzed and on output DEST[i] is non-zero if and
only if BP[i] is the destination of a jump. It is positive
for a forward jump and negative for a backward jump. The number
of jumps is returned. */
static int make_destination_table (gst_uchar * bp,
int size,
char *dest);
/* Helper function to compute the bytecode verifier's `in'
sets from the `out' sets. */
static mst_Boolean merge_stacks (OOP *dest,
int dest_sp,
OOP *src,
int src_sp);
int
_gst_is_simple_return (bc_vector bytecodes)
{
gst_uchar *bytes;
size_t byteCodeLen;
int maybe = MTH_NORMAL;
OOP maybe_object = NULL;
if (bytecodes == NULL)
return (MTH_NORMAL);
byteCodeLen = _gst_bytecode_length (bytecodes);
bytes = bytecodes->base;
if (bytes[0] == LINE_NUMBER_BYTECODE)
{
byteCodeLen -= BYTECODE_SIZE;
bytes += BYTECODE_SIZE;
}
MATCH_BYTECODES (IS_SIMPLE_RETURN, bytes, (
PUSH_SELF { maybe = MTH_RETURN_SELF; }
PUSH_RECEIVER_VARIABLE { maybe = (n << 8) | MTH_RETURN_INSTVAR; }
PUSH_LIT_CONSTANT { maybe = (n << 8) | MTH_RETURN_LITERAL; }
PUSH_INTEGER { maybe_object = FROM_INT (n); maybe = MTH_RETURN_LITERAL; }
PUSH_SPECIAL {
maybe = MTH_RETURN_LITERAL;
switch (n)
{
case NIL_INDEX: maybe_object = _gst_nil_oop; break;
case TRUE_INDEX: maybe_object = _gst_true_oop; break;
case FALSE_INDEX: maybe_object = _gst_false_oop; break;
default: abort ();
}
}
LINE_NUMBER_BYTECODE,
STORE_RECEIVER_VARIABLE,
PUSH_OUTER_TEMP, STORE_OUTER_TEMP,
JUMP, POP_JUMP_TRUE, POP_JUMP_FALSE,
PUSH_TEMPORARY_VARIABLE, PUSH_LIT_VARIABLE,
RETURN_CONTEXT_STACK_TOP,
STORE_TEMPORARY_VARIABLE, STORE_LIT_VARIABLE,
SEND, POP_INTO_NEW_STACKTOP,
POP_STACK_TOP, DUP_STACK_TOP,
SEND_IMMEDIATE, EXIT_INTERPRETER,
SEND_ARITH, SEND_SPECIAL, MAKE_DIRTY_BLOCK,
RETURN_METHOD_STACK_TOP { return (MTH_NORMAL); }
INVALID { abort(); }
));
if (bytes[0] != RETURN_CONTEXT_STACK_TOP)
return (MTH_NORMAL);
if (maybe_object)
_gst_add_forced_object (maybe_object);
return (maybe);
}
int
_gst_check_kind_of_block (bc_vector bc,
OOP * literals)
{
int status, newStatus;
gst_uchar *bp, *end;
status = 0; /* clean block */
for (bp = bc->base, end = bc->ptr; bp != end; )
{
MATCH_BYTECODES (CHECK_KIND_OF_BLOCK, bp, (
PUSH_SELF, PUSH_RECEIVER_VARIABLE,
STORE_RECEIVER_VARIABLE {
if (status == 0)
status = 1;
}
PUSH_LIT_CONSTANT {
newStatus = check_inner_block (literals[n]);
if (newStatus > status)
{
if (newStatus == 31)
return (31);
status = newStatus;
}
}
PUSH_OUTER_TEMP, STORE_OUTER_TEMP {
if (status < 1 + scopes) status = 1 + scopes;
if (status > 31)
/* ouch! how deep!! */
return (31);
}
JUMP,
POP_JUMP_TRUE,
POP_JUMP_FALSE,
PUSH_TEMPORARY_VARIABLE,
PUSH_LIT_VARIABLE,
PUSH_SPECIAL,
PUSH_INTEGER,
RETURN_CONTEXT_STACK_TOP,
LINE_NUMBER_BYTECODE,
STORE_TEMPORARY_VARIABLE,
STORE_LIT_VARIABLE,
SEND,
POP_INTO_NEW_STACKTOP,
POP_STACK_TOP,
DUP_STACK_TOP,
EXIT_INTERPRETER,
SEND_ARITH,
SEND_SPECIAL,
SEND_IMMEDIATE,
MAKE_DIRTY_BLOCK { }
RETURN_METHOD_STACK_TOP { return (31); }
INVALID { abort(); }
));
}
return (status);
}
int
check_inner_block (OOP blockOOP)
{
int newStatus;
gst_compiled_block block;
if (!IS_CLASS (blockOOP, _gst_compiled_block_class))
return (0);
/* Check the cleanness of the inner block and adequately change the status.
full block: no way dude -- exit immediately
clean block: same for us
receiver access: same for us
access to temps in the Xth context: from the perspective of the block
being checked here, it is like an access to temps in the (X-1)th
context access to this block's temps: our outerContext can be nil
either, but for safety we won't be a clean block. */
block = (gst_compiled_block) OOP_TO_OBJ (blockOOP);
newStatus = block->header.clean;
switch (newStatus)
{
case 31:
case 0:
case 1:
return (newStatus);
default:
return (newStatus - 1);
}
}
int
compare_blocks (const PTR a, const PTR b)
{
const block_boundary *ba = (const block_boundary *) a;
const block_boundary *bb = (const block_boundary *) b;
return (ba->byte - bb->byte);
}
bc_vector
_gst_optimize_bytecodes (bc_vector bytecodes)
{
#ifdef NO_OPTIMIZE
return (bytecodes);
#else
bc_vector old_bytecodes;
block_boundary *blocks, *current;
jump *jumps;
gst_uchar *bp;
gst_uchar *end, *first;
int num;
bp = bytecodes->base;
end = bytecodes->ptr;
blocks = alloca (sizeof (block_boundary) * (end - bp + 1));
memset (blocks, 0, sizeof (block_boundary) * (end - bp + 1));
/* 1) Split into basic blocks. This part cheats so that the final
fixup also performs jump optimization. */
for (current = blocks, num = 0; bp != end; bp += BYTECODE_SIZE)
{
gst_uchar *dest = bp;
gst_uchar *first_byte;
mst_Boolean canOptimizeJump, split;
split = false;
do
{
first_byte = dest;
canOptimizeJump = false;
MATCH_BYTECODES (THREAD_JUMPS, dest, (
MAKE_DIRTY_BLOCK,
SEND_SPECIAL,
SEND_ARITH,
SEND_IMMEDIATE,
PUSH_RECEIVER_VARIABLE,
PUSH_TEMPORARY_VARIABLE,
PUSH_LIT_CONSTANT,
PUSH_LIT_VARIABLE,
PUSH_SELF,
PUSH_SPECIAL,
PUSH_INTEGER,
LINE_NUMBER_BYTECODE,
STORE_RECEIVER_VARIABLE,
STORE_TEMPORARY_VARIABLE,
STORE_LIT_VARIABLE,
SEND,
POP_INTO_NEW_STACKTOP,
POP_STACK_TOP,
DUP_STACK_TOP,
PUSH_OUTER_TEMP,
STORE_OUTER_TEMP,
EXIT_INTERPRETER { }
JUMP {
if (ofs == 2
&& dest[0] == LINE_NUMBER_BYTECODE)
{
/* This could not be optimized to a nop, cause the
jump and line number bytecodes lie in different
basic blocks! So we rewrite it to a functionally
equivalent but optimizable bytecode sequence. */
dest[-2] = dest[0];
dest[-1] = dest[1];
}
else if (ofs == 4
&& IS_PUSH_BYTECODE (dest[0])
&& dest[2] == POP_STACK_TOP)
{
/* This could not be optimized to a single pop,
cause the push and pop bytecodes lie in different
basic blocks! Again, rewrite to an optimizable
sequence. */
dest[-2] = POP_STACK_TOP;
dest[-1] = 0;
}
else
{
/* Don't optimize jumps that land on one which has extension
bytes. But if we jump to a return, we can safely optimize:
returns are never extended, and the interpreter ignores the
extension byte. */
canOptimizeJump = (*IP0 != EXT_BYTE);
dest = IP0 + ofs;
current->byte = dest - bytecodes->base;
canOptimizeJump |= IS_RETURN_BYTECODE (*dest);
split = true;
}
}
POP_JUMP_TRUE, POP_JUMP_FALSE {
/* Jumps to CONDITIONAL jumps must not be touched, either because
they were unconditional or because they pop the stack top! */
if (first_byte == bp)
{
dest = IP0 + ofs;
current->byte = dest - bytecodes->base;
split = true;
}
}
RETURN_METHOD_STACK_TOP, RETURN_CONTEXT_STACK_TOP {
/* Return bytecodes - patch the original jump to return
directly */
bp[0] = dest[-2];
bp[1] = 0;
/* This in fact eliminated the jump, don't split in basic
blocks */
split = false;
}
INVALID { abort (); }
));
}
while (canOptimizeJump);
if (split)
{
current->id = ++num;
current++;
current->byte = bp - bytecodes->base;
current->id = -num;
current++;
}
while (*bp == EXT_BYTE)
bp += BYTECODE_SIZE;
}
/* 2) Get the "real" block boundaries by sorting them according to
where they happen in the original bytecode. Note that a simple
bucket sort is not enough because multiple jumps could end on the
same bytecode, and the same bytecode could be both the start and
the destination of a jump! */
qsort (blocks, current - blocks, sizeof (block_boundary), compare_blocks);
/* 3) Optimize the single basic blocks, and reorganize into `jumps'
the data that was put in blocks */
jumps = alloca (sizeof (jump) * num);
old_bytecodes = _gst_save_bytecode_array ();
for (bp = bytecodes->base; blocks != current; blocks++)
{
first = bp;
bp = bytecodes->base + blocks->byte;
optimize_basic_block (first, bp);
if (blocks->id > 0)
jumps[blocks->id - 1].dest = _gst_current_bytecode_length ();
else
jumps[-blocks->id - 1].from = _gst_current_bytecode_length ();
}
optimize_basic_block (bp, end);
_gst_free_bytecodes (bytecodes);
bytecodes = _gst_get_bytecodes ();
/* 4) Fix the jumps so that they correctly point to the start of the
same basic block */
for (; num--; jumps++)
{
int ofs;
bp = bytecodes->base + jumps->from;
ofs = jumps->dest - jumps->from - 2;
/* Fill the bytes from the topmost one. */
while (*bp == EXT_BYTE)
ofs -= 2, bp += BYTECODE_SIZE;
if (ofs == 0 && (*bp & ~1) != JUMP)
{
/* Use pop stack top for conditionals which jump to
the following bytecode. */
bp[0] = POP_STACK_TOP;
bp[1] = 0;
}
else
{
if (ofs < 0)
ofs = -ofs;
do
{
bp[1] = ofs & 255;
bp -= BYTECODE_SIZE;
ofs >>= 8;
}
while UNCOMMON (*bp == EXT_BYTE);
}
}
_gst_restore_bytecode_array (old_bytecodes);
return (bytecodes);
#endif
}
static inline int
search_superop_fixed_arg_1(int bc1, int arg, int bc2)
{
/* ARG is in the range 0..255. The format of the hash table entries is
{ { BC1, BC2, ARG }, SUPEROP } */
struct superop_with_fixed_arg_1_type {
unsigned char bytes[3];
int superop;
};
#include "superop1.inl"
unsigned int key = asso_values[bc1] + asso_values[bc2] + asso_values[arg];
register const struct superop_with_fixed_arg_1_type *k;
if (key > MAX_HASH_VALUE)
return -1;
k = &keylist[key];
if (bc1 == k->bytes[0] && bc2 == k->bytes[1] && arg == k->bytes[2])
return k->superop;
else
return -1;
}
static inline int
search_superop_fixed_arg_2(int bc1, int bc2, int arg)
{
/* ARG is in the range 0..255. The format of the hash table entries is
{ { BC1, BC2, ARG }, SUPEROP } */
struct superop_with_fixed_arg_2_type {
unsigned char bytes[3];
int superop;
};
#include "superop2.inl"
unsigned int key = asso_values[bc1] + asso_values[bc2] + asso_values[arg];
register const struct superop_with_fixed_arg_2_type *k;
if (key > MAX_HASH_VALUE)
return -1;
k = &keylist[key];
if (bc1 == k->bytes[0] && bc2 == k->bytes[1] && arg == k->bytes[2])
return k->superop;
else
return -1;
}
void
optimize_basic_block (gst_uchar * from,
gst_uchar * to)
{
/* Points to the optimized bytecodes that have been written. */
gst_uchar *opt = from;
/* Points to the unoptimized bytecodes as they are read. */
gst_uchar *bp = from;
if (from == to)
return;
/* For simplicity, the optimizations on line number bytecodes
don't take into account the possibility that the line number
bytecode is extended (>256 lines in a method). This almost
never happens, so we don't bother. */
do
{
switch (bp[0])
{
case LINE_NUMBER_BYTECODE:
/* Remove two consecutive line-number bytecode. */
if (bp < to - 2
&& bp[2] == LINE_NUMBER_BYTECODE)
{
bp += 2;
continue;
}
break;
case PUSH_TEMPORARY_VARIABLE:
case PUSH_RECEIVER_VARIABLE:
/* Leave only the store in store/pop/push sequences. Don't do this
for STORE_LIT_VARIABLE, as it fails if #value: is sent and,
for example, self is returned. */
if (opt >= from + 4
&& (opt == from + 4 || opt[-6] != EXT_BYTE)
&& opt[-4] == bp[0] + (STORE_TEMPORARY_VARIABLE - PUSH_TEMPORARY_VARIABLE)
&& opt[-3] == bp[1]
&& opt[-2] == POP_STACK_TOP
&& bp[-2] != EXT_BYTE)
{
opt -= 2;
bp += 2;
continue;
}
/* Also rewrite store/pop/line/push to store/line in the middle. */
if (opt >= from + 6
&& (opt == from + 6 || opt[-8] != EXT_BYTE)
&& opt[-6] == bp[0] + (STORE_TEMPORARY_VARIABLE - PUSH_TEMPORARY_VARIABLE)
&& opt[-5] == bp[1]
&& opt[-4] == POP_STACK_TOP
&& opt[-2] == LINE_NUMBER_BYTECODE
&& bp[-2] != EXT_BYTE)
{
opt[-4] = opt[-2];
opt[-3] = opt[-1];
opt -= 2;
bp += 2;
continue;
}
/* fall through to other pushes. */
case PUSH_OUTER_TEMP:
case PUSH_INTEGER:
case PUSH_SELF:
case PUSH_SPECIAL:
case PUSH_LIT_CONSTANT:
/* Remove a push followed by a pop */
if (bp < to - 2
&& bp[2] == POP_STACK_TOP)
{
bp += 4;
continue;
}
/* Remove the pop in a pop/push/return sequence */
if (opt >= from + 2 && bp < to - 2
&& bp[2] == RETURN_CONTEXT_STACK_TOP
&& opt[-2] == POP_STACK_TOP)
opt -= 2;
/* Rewrite the pop/line number/push sequence to
line number/pop/push because this can be better
optimized by superoperators (making a superoperator
with a nop byte saves on decoding, but not on
scheduling the instructions in the interpreter!). */
if (opt >= from + 4
&& opt[-4] == POP_STACK_TOP
&& opt[-2] == LINE_NUMBER_BYTECODE)
{
opt[-4] = LINE_NUMBER_BYTECODE;
opt[-3] = opt[-1];
opt[-2] = POP_STACK_TOP;
opt[-1] = 0;
}
break;
default:
break;
}
/* Else, just copy the bytecode to the optimized area. */
*opt++ = *bp++;
*opt++ = *bp++;
}
while (bp < to);
#ifndef NO_SUPEROPERATORS
opt = optimize_superoperators (from, opt);
#endif
_gst_compile_bytecodes (from, opt);
}
gst_uchar *
optimize_superoperators (gst_uchar * from,
gst_uchar * to)
{
/* Points to the optimized bytecodes that have been written. */
gst_uchar *opt = from;
/* Points to the unoptimized bytecodes as they are read. */
gst_uchar *bp = from;
int new_bc;
if (from == to)
return from;
*opt++ = *bp++;
*opt++ = *bp++;
while (bp < to)
{
/* Copy two bytecodes to the optimized area. */
*opt++ = *bp++;
*opt++ = *bp++;
do
{
/* Try to match the case when the second argument is fixed.
We try this first because
EXT_BYTE(*), SEND(1)
is more beneficial than
EXT_BYTE(1), SEND(*). */
new_bc = search_superop_fixed_arg_2 (opt[-4], opt[-2], opt[-1]);
if (new_bc != -1)
{
opt[-4] = new_bc;
/* opt[-3] is already ok */
opt -= 2;
/* Look again at the last four bytes. */
continue;
}
/* If the first bytecode is not extended, try matching it with a
fixed argument. We skip this when the first bytecode is
extended because otherwise we might have superoperators like
PUSH_OUTER_TEMP(1), SEND(*)
Suppose we find
EXT_BYTE(1), SUPEROP(2)
Now the argument to SEND is 2, but the interpreter receives
an argument of 258 and has to decode the argument to extract
the real argument of PUSH_OUTER_TEMP (found in the extension
byte). This messes up everything and goes against the very
purpose of introducing superoperators. */
if (opt - from == 4 || opt[-6] != EXT_BYTE)
{
new_bc = search_superop_fixed_arg_1 (opt[-4], opt[-3], opt[-2]);
if (new_bc != -1)
{
opt[-4] = new_bc;
opt[-3] = opt[-1];
opt -= 2;
/* Look again at the last four bytes. */
continue;
}
}
/* Nothing matched. Exit. */
break;
}
while (opt - from >= 4);
}
return opt;
}
void
_gst_compute_stack_positions (gst_uchar * bp,
int size,
PTR * base,
PTR ** pos)
{
basic_block_item **bb_start, *bb_first, *worklist, *susp_head,
**susp_tail = &susp_head;
int bc_len;
bb_start = alloca ((1 + size) * sizeof (basic_block_item *));
memzero (bb_start, (1 + size) * sizeof (basic_block_item *));
memzero (pos, (1 + size) * sizeof (PTR *));
/* Allocate the first and last basic block specially */
ALLOCA_BASIC_BLOCK (bb_start, 0, bp, 0);
ALLOCA_BASIC_BLOCK (bb_start + size, 0, bp + size, 0);
bb_first = bb_start[0];
bb_first->next = NULL;
/* First build the pointers to the basic blocks into BB_START. Then use
of a worklist here is only to find a correct order for visiting the
basic blocks, not because they're visited multiple times. This
works transparently when we have a return in the middle of the method.
Then the basic block is ending, yet it might be that the stack height
for the next bytecode is already known!!! */
for (susp_head = NULL, worklist = bb_first; worklist; )
{
int curr_sp = worklist->sp;
bp = worklist->bp;
bb_start = worklist->bb;
worklist = worklist->next;
#ifdef DEBUG_JIT_TRANSLATOR
printf ("Tracing basic block at %d:\n", bb_start - bb_first->bb);
#endif
do
{
int curr_ip = bb_start - bb_first->bb;
int balance;
gst_uchar *bp_first = bp;
#ifdef DEBUG_JIT_TRANSLATOR
printf ("[SP=%3d]%5d:", curr_sp, curr_ip);
_gst_print_bytecode_name (bp, curr_ip, NULL, "\t");
#endif
balance = 0;
pos[curr_ip] = base + curr_sp;
MATCH_BYTECODES (COMPUTE_STACK_POS, bp, (
RETURN_METHOD_STACK_TOP,
RETURN_CONTEXT_STACK_TOP {
bc_len = bp - bp_first;
/* We cannot fill the basic block right now because the
stack height might be different. */
if (!bb_start[bc_len])
{
ALLOCA_BASIC_BLOCK (bb_start + bc_len, 0,
bp_first + bc_len, curr_sp + balance);
bb_start[bc_len]->suspended = true;
bb_start[bc_len]->next = NULL;
*susp_tail = bb_start[bc_len];
susp_tail = &(bb_start[bc_len]->next);
}
}
POP_INTO_NEW_STACKTOP,
POP_STACK_TOP { balance--; }
PUSH_RECEIVER_VARIABLE,
PUSH_TEMPORARY_VARIABLE,
PUSH_LIT_CONSTANT,
PUSH_LIT_VARIABLE,
PUSH_SELF,
PUSH_SPECIAL,
PUSH_INTEGER,
DUP_STACK_TOP,
PUSH_OUTER_TEMP { balance++; }
LINE_NUMBER_BYTECODE,
STORE_RECEIVER_VARIABLE,
STORE_TEMPORARY_VARIABLE,
STORE_LIT_VARIABLE,
STORE_OUTER_TEMP,
EXIT_INTERPRETER,
MAKE_DIRTY_BLOCK { }
SEND {
balance -= super + num_args;
}
SEND_ARITH {
balance -= _gst_builtin_selectors[n].numArgs;
}
SEND_IMMEDIATE {
balance -= super + _gst_builtin_selectors[n].numArgs;
}
SEND_SPECIAL {
balance -= _gst_builtin_selectors[n + 16].numArgs;
}
INVALID {
abort ();
}
JUMP {
bc_len = bp - bp_first;
/* We cannot fill the basic block right now because the
stack height might be different. */
if (!bb_start[bc_len])
{
ALLOCA_BASIC_BLOCK (bb_start + bc_len, 0,
bp_first + bc_len, 0);
bb_start[bc_len]->suspended = true;
bb_start[bc_len]->next = NULL;
*susp_tail = bb_start[bc_len];
susp_tail = &(bb_start[bc_len]->next);
}
if (!bb_start[ofs])
{
ALLOCA_BASIC_BLOCK (bb_start + ofs, 0,
bp_first + ofs, curr_sp + balance);
bb_start[ofs]->next = worklist;
worklist = bb_start[ofs];
}
else if (bb_start[ofs]->suspended)
{
bb_start[ofs]->suspended = false;
bb_start[ofs]->sp = curr_sp + balance;
}
else if (curr_sp + balance != bb_start[ofs]->sp)
abort ();
}
POP_JUMP_TRUE, POP_JUMP_FALSE {
balance--;
bc_len = bp - bp_first;
if (!bb_start[bc_len])
{
ALLOCA_BASIC_BLOCK (bb_start + bc_len, 0,
bp_first + bc_len, curr_sp + balance);
bb_start[bc_len]->next = worklist;
worklist = bb_start[bc_len];
}
else if (bb_start[bc_len]->suspended)
{
bb_start[bc_len]->suspended = false;
bb_start[bc_len]->sp = curr_sp + balance;
}
else if (curr_sp + balance != bb_start[bc_len]->sp)
abort ();
if (!bb_start[ofs])
{
ALLOCA_BASIC_BLOCK (bb_start + ofs, 0,
bp_first + ofs, curr_sp + balance);
bb_start[ofs]->next = worklist;
worklist = bb_start[ofs];
}
else if (bb_start[ofs]->suspended)
{
bb_start[ofs]->suspended = false;
bb_start[ofs]->sp = curr_sp + balance;
}
else if (curr_sp + balance != bb_start[ofs]->sp)
abort ();
}
));
curr_sp += balance;
bb_start += bp - bp_first;
}
while (!*bb_start);
if (!worklist && susp_head)
{
worklist = susp_head;
susp_head = susp_head->next;
worklist->next = NULL;
if (!susp_head)
susp_tail = &susp_head;
}
}
}
void
_gst_analyze_bytecodes (OOP methodOOP,
int size,
char *dest)
{
gst_uchar *bp;
bp = GET_METHOD_BYTECODES (methodOOP);
make_destination_table (bp, size, dest);
/* Nothing more for now */
}
int
make_destination_table (gst_uchar * bp,
int size,
char *dest)
{
gst_uchar *end, *bp_first;
int count;
memzero (dest, sizeof (char) * size);
for (count = 0, end = bp + size; bp != end;
dest += bp - bp_first)
{
bp_first = bp;
MATCH_BYTECODES (MAKE_DEST_TABLE, bp, (
PUSH_RECEIVER_VARIABLE,
PUSH_TEMPORARY_VARIABLE,
PUSH_LIT_CONSTANT,
PUSH_LIT_VARIABLE,
PUSH_SELF,
PUSH_SPECIAL,
PUSH_INTEGER,
RETURN_METHOD_STACK_TOP,
RETURN_CONTEXT_STACK_TOP,
LINE_NUMBER_BYTECODE,
STORE_RECEIVER_VARIABLE,
STORE_TEMPORARY_VARIABLE,
STORE_LIT_VARIABLE,
SEND,
POP_INTO_NEW_STACKTOP,
POP_STACK_TOP,
DUP_STACK_TOP,
PUSH_OUTER_TEMP,
STORE_OUTER_TEMP,
EXIT_INTERPRETER,
SEND_ARITH,
SEND_SPECIAL,
SEND_IMMEDIATE,
MAKE_DIRTY_BLOCK { }
INVALID { abort(); }
JUMP, POP_JUMP_TRUE, POP_JUMP_FALSE {
dest[ofs] = ofs > 0 ? 1 : -1;
count++;
}
));
}
return (count);
}
#define SELF 0
#define VARYING 1
#define UNDEFINED 2
typedef struct partially_constructed_array {
struct partially_constructed_array *next;
int sp;
int size;
} partially_constructed_array;
#define CHECK_LITERAL(n) \
/* Todo: recurse into BlockClosures! */ \
last_used_literal = literals[n]; \
if ((n) >= num_literals) \
return ("literal out of range");
#define CHECK_TEMP(n) \
last_used_literal = NULL; \
if ((n) >= sp - stack) \
return ("temporary out of range");
#define CHECK_REC_VAR(first, n) \
last_used_literal = NULL; \
if ((n) < (first) || (n) >= num_rec_vars) \
return ("receiver variable out of range");
#define CHECK_LIT_VARIABLE(store, n) \
CHECK_LITERAL (n); \
if (IS_INT (literals[(n)]) || \
!is_a_kind_of (OOP_CLASS (literals[(n)]), _gst_lookup_key_class)) \
return ("LookupKey expected"); \
else if (store \
&& untrusted \
&& !IS_OOP_UNTRUSTED (literals[(n)])) \
return ("Invalid global variable access");
#define LIT_VARIABLE_CLASS(n) \
/* Special case classes because of super and {...} */ \
(IS_A_CLASS (ASSOCIATION_VALUE (literals[(n)])) \
? OOP_CLASS (ASSOCIATION_VALUE (literals[(n)])) \
: FROM_INT (VARYING))
#define LITERAL_CLASS(n) \
OOP_INT_CLASS (literals[(n)])
/* Bytecode verification is a dataflow analysis on types. We perform it
on basic blocks: `in' is the stack when entering the basic block and
`out' is the stack upon exit.
Each member of the stack can be UNDEFINED, a type, or VARYING. When
merging two `out' stacks to compute an `in' stack, we have these
possible situations:
- the stacks are not the same height, and bytecode verification fails
- a slot is the same in both stacks, so it has this type in the output too
- a slot is different in the two stacks, so it is VARYING in the output.
Bytecode verification proceeds forwards, so the worklist is added all the
successors of the basic block whenever merging results in a difference. */
mst_Boolean
merge_stacks (OOP *dest, int dest_sp,
OOP *src, int src_sp)
{
mst_Boolean varied = false;
assert (dest_sp == src_sp);
for (; src_sp--; dest++, src++)
{
OOP newDest = *src;
if (newDest != *src)
{
if (*dest != FROM_INT (UNDEFINED))
/* If different, mark as overdefined. */
newDest = FROM_INT (VARYING);
if (newDest != *dest)
{
*dest = newDest;
varied = true;
}
}
}
return (varied);
}
void
_gst_verify_sent_method (OOP methodOOP)
{
const char *error;
error = _gst_verify_method (methodOOP, NULL, 0);
if (error)
{
_gst_errorf ("Bytecode verification failed: %s", error);
if (OOP_CLASS (methodOOP) == _gst_compiled_block_class)
methodOOP = GET_BLOCK_METHOD (methodOOP);
_gst_errorf ("Method verification failed for %O>>%O",
GET_METHOD_CLASS (methodOOP),
GET_METHOD_SELECTOR (methodOOP));
abort ();
}
}
const char *
_gst_verify_method (OOP methodOOP, int *num_outer_temps, int depth)
{
#ifndef NO_VERIFIER
int size, bc_len, num_temps, stack_depth,
num_literals, num_rec_vars, num_ro_rec_vars;
mst_Boolean untrusted;
const char *error;
gst_uchar *bp;
OOP *literals, methodClass, last_used_literal;
basic_block_item **bb_start, *bb_first, *worklist, *susp_head,
**susp_tail = &susp_head;
partially_constructed_array *arrays = NULL, *arrays_pool = NULL;
if (IS_OOP_VERIFIED (methodOOP))
return (NULL);
size = NUM_INDEXABLE_FIELDS (methodOOP);
bp = GET_METHOD_BYTECODES (methodOOP);
literals = GET_METHOD_LITERALS (methodOOP);
methodClass = GET_METHOD_CLASS (methodOOP);
num_literals = NUM_METHOD_LITERALS (methodOOP);
num_rec_vars = CLASS_FIXED_FIELDS (methodClass);
untrusted = IS_OOP_UNTRUSTED (methodOOP);
if (is_a_kind_of (OOP_CLASS (methodOOP), _gst_compiled_method_class))
{
method_header header;
header = GET_METHOD_HEADER (methodOOP);
num_temps = header.numArgs + header.numTemps;
stack_depth = header.stack_depth << DEPTH_SCALE;
switch (header.headerFlag)
{
case MTH_NORMAL:
case MTH_PRIMITIVE:
case MTH_ANNOTATED:
case MTH_UNUSED:
break;
case MTH_USER_DEFINED:
case MTH_RETURN_SELF:
methodOOP->flags |= F_VERIFIED;
return (NULL);
case MTH_RETURN_INSTVAR:
CHECK_REC_VAR (0, header.primitiveIndex);
methodOOP->flags |= F_VERIFIED;
return (NULL);
case MTH_RETURN_LITERAL:
CHECK_LITERAL (0);
methodOOP->flags |= F_VERIFIED;
return (NULL);
}
}
else if (OOP_CLASS (methodOOP) == _gst_compiled_block_class)
{
block_header header;
header = GET_BLOCK_HEADER (methodOOP);
/* If we're verifying a block but not from a nested call,
restart from the top-level method. */
if (header.clean != 0 && depth == 0)
return _gst_verify_method (GET_BLOCK_METHOD (methodOOP), NULL, 0);
num_temps = header.numArgs + header.numTemps;
stack_depth = header.depth << DEPTH_SCALE;
}
else
return "invalid class";
if (untrusted)
{
OOP class_oop;
for (class_oop = methodClass; IS_OOP_UNTRUSTED (class_oop);
class_oop = SUPERCLASS (class_oop))
;
num_ro_rec_vars = CLASS_FIXED_FIELDS (class_oop);
}
else
num_ro_rec_vars = 0;
#ifdef DEBUG_VERIFIER
printf ("Verifying %O (max. stack depth = %d):\n", methodOOP, stack_depth);
#endif
/* Prepare the NUM_OUTER_TEMPS array for the inner blocks. */
if (depth)
{
int *new_num_outer_temps = alloca (sizeof (int) * (depth + 1));
memcpy (new_num_outer_temps + 1, num_outer_temps, sizeof (int) * depth);
new_num_outer_temps[0] = num_temps;
num_outer_temps = new_num_outer_temps;
}
else
num_outer_temps = &num_temps;
depth++;
bb_start = alloca ((1 + size) * sizeof (basic_block_item *));
memzero (bb_start, (1 + size) * sizeof (basic_block_item *));
/* Allocate the first and last basic block specially */
ALLOCA_BASIC_BLOCK(bb_start, stack_depth, bp, num_temps);
ALLOCA_BASIC_BLOCK(bb_start + size, stack_depth, bp + size, num_temps);
bb_first = bb_start[0];
bb_first->next = NULL;
/* First build the pointers to the basic blocks into BB_START. The use
of a worklist here is only to find a correct order for visiting the
basic blocks, not because they're visited multiple times. This
works transparently when we have a return in the middle of the method.
Then the basic block is ending, yet it might be that the stack height
for the next bytecode is already known!!! */
for (susp_head = NULL, worklist = bb_first; worklist; )
{
int curr_sp = worklist->sp;
bp = worklist->bp;
bb_start = worklist->bb;
worklist = worklist->next;
#ifdef DEBUG_VERIFIER
printf ("Tracing basic block at %d:\n", bb_start - bb_first->bb);
#endif
do
{
int curr_ip = bb_start - bb_first->bb;
int balance;
gst_uchar *bp_first = bp;
#ifdef DEBUG_VERIFIER
printf ("[SP=%3d]%5d:", curr_sp, curr_ip);
_gst_print_bytecode_name (bp, curr_ip, literals, "\t");
#endif
balance = 0;
MATCH_BYTECODES (CREATE_BASIC_BLOCKS, bp, (
RETURN_METHOD_STACK_TOP,
RETURN_CONTEXT_STACK_TOP {
bc_len = bp - bp_first;
/* We cannot fill the basic block right now because the
stack height might be different. */
if (!bb_start[bc_len])
{
ALLOCA_BASIC_BLOCK (bb_start + bc_len, stack_depth,
bp_first + bc_len, curr_sp + balance);
bb_start[bc_len]->suspended = true;
bb_start[bc_len]->next = NULL;
*susp_tail = bb_start[bc_len];
susp_tail = &(bb_start[bc_len]->next);
}
}
POP_STACK_TOP { balance--; }
PUSH_RECEIVER_VARIABLE,
PUSH_TEMPORARY_VARIABLE,
PUSH_LIT_CONSTANT,
PUSH_LIT_VARIABLE,
PUSH_SELF,
PUSH_SPECIAL,
PUSH_INTEGER,
PUSH_OUTER_TEMP { balance++; }
LINE_NUMBER_BYTECODE,
STORE_RECEIVER_VARIABLE,
STORE_TEMPORARY_VARIABLE,
STORE_LIT_VARIABLE,
STORE_OUTER_TEMP,
EXIT_INTERPRETER,
MAKE_DIRTY_BLOCK { }
SEND {
balance -= super + num_args;
/* Sends touch the new stack top, so they require an extra slot. */
if (curr_sp + balance < 1)
return ("stack underflow");
}
SEND_ARITH {
if (!_gst_builtin_selectors[n].symbol)
return ("invalid immediate send");
balance -= _gst_builtin_selectors[n].numArgs;
/* Sends touch the new stack top, so they require an extra slot. */
if (curr_sp + balance < 1)
return ("stack underflow");
}
SEND_SPECIAL {
if (!_gst_builtin_selectors[n + 16].symbol)
return ("invalid immediate send");
balance -= _gst_builtin_selectors[n + 16].numArgs;
/* Sends touch the new stack top, so they require an extra slot. */
if (curr_sp + balance < 1)
return ("stack underflow");
}
SEND_IMMEDIATE {
if (!_gst_builtin_selectors[n].symbol)
return ("invalid immediate send");
balance -= super + _gst_builtin_selectors[n].numArgs;
/* Sends touch the new stack top, so they require an extra slot. */
if (curr_sp + balance < 1)
return ("stack underflow");
}
POP_INTO_NEW_STACKTOP {
balance--;
/* Sends touch the new stack top, so they require an extra slot. */
if (curr_sp + balance < 1)
return ("stack underflow");
}
DUP_STACK_TOP {
balance++;
}
INVALID {
return ("invalid bytecode");
}
JUMP {
if (ofs & 1)
return ("jump to odd offset");
if (ofs + curr_ip < 0 || ofs + curr_ip > size)
return ("jump out of range");
if (ofs + curr_ip > 0 && bp_first[ofs - 2] == EXT_BYTE)
return ("jump skips extension bytecode");
bc_len = bp - bp_first;
/* We cannot fill the basic block right now because the
stack height might be different. */
if (!bb_start[bc_len])
{
ALLOCA_BASIC_BLOCK (bb_start + bc_len, stack_depth,
bp_first + bc_len, 0);
bb_start[bc_len]->suspended = true;
bb_start[bc_len]->next = NULL;
*susp_tail = bb_start[bc_len];
susp_tail = &(bb_start[bc_len]->next);
}
if (!bb_start[ofs])
{
ALLOCA_BASIC_BLOCK (bb_start + ofs, stack_depth,
bp_first + ofs, curr_sp + balance);
bb_start[ofs]->next = worklist;
worklist = bb_start[ofs];
INIT_BASIC_BLOCK (worklist, num_temps);
}
else if (bb_start[ofs]->suspended)
{
bb_start[ofs]->suspended = false;
bb_start[ofs]->sp = curr_sp + balance;
INIT_BASIC_BLOCK (bb_start[ofs], num_temps);
}
else if (curr_sp + balance != bb_start[ofs]->sp)
return ("stack height mismatch");
}
POP_JUMP_TRUE, POP_JUMP_FALSE {
balance--;
if (ofs & 1)
return ("jump to odd offset");
if (ofs + curr_ip < 0 || ofs + curr_ip > size)
return ("jump out of range");
if (ofs + curr_ip > 0 && bp_first[ofs - 2] == EXT_BYTE)
return ("jump skips extension bytecode");
bc_len = bp - bp_first;
if (!bb_start[bc_len])
{
ALLOCA_BASIC_BLOCK (bb_start + bc_len, stack_depth,
bp_first + bc_len, curr_sp + balance);
bb_start[bc_len]->next = worklist;
worklist = bb_start[bc_len];
INIT_BASIC_BLOCK (worklist, num_temps);
}
else if (bb_start[bc_len]->suspended)
{
bb_start[bc_len]->suspended = false;
bb_start[bc_len]->sp = curr_sp + balance;
INIT_BASIC_BLOCK (bb_start[bc_len], num_temps);
}
else if (curr_sp + balance != bb_start[bc_len]->sp)
return ("stack height mismatch");
if (!bb_start[ofs])
{
ALLOCA_BASIC_BLOCK (bb_start + ofs, stack_depth,
bp_first + ofs, curr_sp + balance);
bb_start[ofs]->next = worklist;
worklist = bb_start[ofs];
INIT_BASIC_BLOCK (worklist, num_temps);
}
else if (bb_start[ofs]->suspended)
{
bb_start[ofs]->suspended = false;
bb_start[ofs]->sp = curr_sp + balance;
INIT_BASIC_BLOCK (bb_start[ofs], num_temps);
}
else if (curr_sp + balance != bb_start[ofs]->sp)
return ("stack height mismatch");
}
));
curr_sp += balance;
if (curr_sp >= stack_depth)
return ("stack overflow");
bb_start += bp - bp_first;
}
while (!*bb_start);
if (!worklist && susp_head)
{
worklist = susp_head;
susp_head = susp_head->next;
worklist->next = NULL;
if (!susp_head)
susp_tail = &susp_head;
}
#ifdef DEBUG_VERIFIER
printf ("\n");
#endif
}
for (worklist = bb_first; worklist; )
{
OOP *stack = worklist->stack;
OOP *sp;
/* Look for unreachable basic blocks. */
if (worklist->sp < 0)
abort ();
sp = stack + worklist->sp;
bp = worklist->bp;
bb_start = worklist->bb;
worklist = worklist->next;
#ifdef DEBUG_VERIFIER
printf ("Executing basic block at %d:\n", bb_start - bb_first->bb);
#endif
last_used_literal = NULL;
do
{
gst_uchar *bp_first = bp;
#ifdef DEBUG_VERIFIER
printf ("[SP=%3d]%5d:", sp - stack, bb_start - bb_first->bb);
_gst_print_bytecode_name (bp, bb_start - bb_first->bb, literals, "\t");
#endif
MATCH_BYTECODES (EXEC_BASIC_BLOCK, bp, (
PUSH_RECEIVER_VARIABLE {
CHECK_REC_VAR (0, n);
*sp++ = FROM_INT (VARYING);
}
PUSH_TEMPORARY_VARIABLE {
CHECK_TEMP (n);
*sp++ = stack[n];
}
PUSH_LIT_CONSTANT {
CHECK_LITERAL (n);
*sp++ = LITERAL_CLASS (n);
}
PUSH_LIT_VARIABLE {
CHECK_LIT_VARIABLE (false, n);
*sp++ = LIT_VARIABLE_CLASS (n);
}
PUSH_SELF {
last_used_literal = NULL;
*sp++ = FROM_INT (SELF);
}
PUSH_SPECIAL {
switch (n)
{
case NIL_INDEX: last_used_literal = _gst_nil_oop; break;
case TRUE_INDEX: last_used_literal = _gst_true_oop; break;
case FALSE_INDEX: last_used_literal = _gst_false_oop; break;
default: return "invalid special object index";
}
*sp++ = OOP_CLASS (last_used_literal);
}
PUSH_INTEGER {
last_used_literal = FROM_INT (n);
*sp++ = _gst_small_integer_class;
}
RETURN_METHOD_STACK_TOP,
RETURN_CONTEXT_STACK_TOP { break; }
LINE_NUMBER_BYTECODE { }
STORE_RECEIVER_VARIABLE {
CHECK_REC_VAR (num_ro_rec_vars, n);
}
STORE_TEMPORARY_VARIABLE {
CHECK_TEMP (n);
}
STORE_LIT_VARIABLE {
CHECK_LIT_VARIABLE (true, n);
}
SEND {
if (super
&& (!last_used_literal
|| (!IS_A_CLASS (last_used_literal)
&& !IS_A_METACLASS (last_used_literal))
|| !is_a_kind_of (methodClass, last_used_literal)))
return ("Invalid send to super");
last_used_literal = NULL;
sp -= super + num_args;
if (super && sp[-1] != FROM_INT (SELF))
return ("Invalid send to super");
sp[-1] = FROM_INT (VARYING);
}
POP_INTO_NEW_STACKTOP {
if (sp[-2] != _gst_array_class)
return ("Array expected");
if (!arrays || &sp[-2] - stack != arrays->sp)
return ("Invalid Array constructor");
if (n >= arrays->size)
return ("Out of bounds Array access");
/* Discard arrays whose construction has ended. */
if (n == arrays->size - 1)
{
partially_constructed_array *next = arrays->next;
arrays->next = arrays_pool;
arrays_pool = arrays;
arrays = next;
}
last_used_literal = NULL;
sp--;
}
POP_STACK_TOP {
last_used_literal = NULL;
sp--;
}
DUP_STACK_TOP {
sp++;
sp[-1] = sp[-2];
}
PUSH_OUTER_TEMP {
if (scopes == 0 || scopes > depth || n >= num_outer_temps[scopes])
return ("temporary out of range");
last_used_literal = NULL;
*sp++ = FROM_INT (VARYING);
}
STORE_OUTER_TEMP {
if (scopes == 0 || scopes > depth || n >= num_outer_temps[scopes])
return ("temporary out of range");
}
EXIT_INTERPRETER {
if (size != 4
|| IP0 != GET_METHOD_BYTECODES (methodOOP)
|| *bp != RETURN_CONTEXT_STACK_TOP)
return ("bad termination method");
}
JUMP {
if (merge_stacks (stack, sp - stack, bb_start[ofs]->stack,
bb_start[ofs]->sp))
bb_start[ofs]->next = worklist, worklist = bb_start[ofs];
}
POP_JUMP_TRUE, POP_JUMP_FALSE {
sp--;
bc_len = bp - bp_first;
if (merge_stacks (stack, sp - stack, bb_start[bc_len]->stack,
bb_start[bc_len]->sp))
bb_start[bc_len]->next = worklist, worklist = bb_start[bc_len];
if (merge_stacks (stack, sp - stack, bb_start[ofs]->stack,
bb_start[ofs]->sp))
bb_start[ofs]->next = worklist, worklist = bb_start[ofs];
}
SEND_ARITH {
sp -= _gst_builtin_selectors[n].numArgs;
sp[-1] = FROM_INT (VARYING);
}
SEND_SPECIAL {
sp -= _gst_builtin_selectors[n + 16].numArgs;
sp[-1] = FROM_INT (VARYING);
}
SEND_IMMEDIATE {
if (n == NEW_COLON_SPECIAL
&& IS_INT (last_used_literal)
&& last_used_literal != FROM_INT (0)
&& sp[-2] == OOP_CLASS (_gst_array_class))
{
partially_constructed_array *a;
sp--;
/* If possible, reuse an existing struct, else allocate a new one. */
if (arrays_pool)
{
a = arrays_pool;
arrays_pool = arrays_pool->next;
}
else
a = alloca (sizeof (partially_constructed_array));
a->size = TO_INT (last_used_literal);
a->sp = &sp[-1] - stack;
a->next = arrays;
arrays = a;
sp[-1] = _gst_array_class;
}
else
{
if (super
&& (!last_used_literal
|| (!IS_A_CLASS (last_used_literal)
&& !IS_A_METACLASS (last_used_literal))
|| !is_a_kind_of (methodClass, last_used_literal)))
return (_gst_debug (), "Invalid send to super");
last_used_literal = NULL;
sp -= super + _gst_builtin_selectors[n].numArgs;
if (super && sp[-1] != FROM_INT (SELF))
return ("Invalid send to super");
sp[-1] = FROM_INT (VARYING);
}
}
MAKE_DIRTY_BLOCK {
if (sp[-1] != _gst_compiled_block_class
|| !last_used_literal)
return ("CompiledBlock expected");
error = _gst_verify_method (last_used_literal, num_outer_temps, depth);
if (error)
return (error);
}
INVALID {
abort ();
}
));
bb_start += bp - bp_first;
}
while (!*bb_start);
#ifdef DEBUG_VERIFIER
printf ("\n");
#endif
}
#endif /* !NO_VERIFIER */
methodOOP->flags |= F_VERIFIED;
return (NULL);
}
|