1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
|
/*
Title: An interpreter for a compact instruction set.
Author: Dave Matthews, Cambridge University Computer Laboratory
Copyright (c) 2000-7
Cambridge University Technical Services Limited
Further development Copyright David C.J. Matthews 2015-17.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_WIN32)
#include "winconfig.h"
#else
#error "No configuration file"
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(x) assert(x)
#else
#define ASSERT(x) 0
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#ifdef HAVE_MATH_H
#include <math.h>
#endif
#include "globals.h"
#include "int_opcodes.h"
#include "machine_dep.h"
#include "sys.h"
#include "profiling.h"
#include "arb.h"
#include "processes.h"
#include "run_time.h"
#include "mpoly.h"
#include "gc.h"
#include "basicio.h"
#include "timing.h"
#include "arb.h"
#include "reals.h"
#include "objsize.h"
#include "xwindows.h"
#include "foreign.h"
#include "process_env.h"
#include "network.h"
#include "basicio.h"
#include "sighandler.h"
#include "os_specific.h"
#include "diagnostics.h"
#include "polystring.h"
#include "save_vec.h"
#include "memmgr.h"
#include "poly_specific.h"
#include "scanaddrs.h"
#include "polyffi.h"
#include "rtsentry.h"
#define arg1 (pc[0] + pc[1]*256)
#define arg2 (pc[2] + pc[3]*256)
#define arg3 (pc[4] + pc[5]*256)
#define arg4 (pc[6] + pc[7]*256)
const PolyWord True = TAGGED(1);
const PolyWord False = TAGGED(0);
const PolyWord Zero = TAGGED(0);
#define CHECKED_REGS 2
#define UNCHECKED_REGS 0
#define EXTRA_STACK 0 // Don't need any extra - signals aren't handled on the Poly stack.
/* the amount of ML stack space to reserve for registers,
C exception handling etc. The compiler requires us to
reserve 2 stack-frames worth (2 * 20 words) plus whatever
we require for the register save area. We actually reserve
slightly more than this. SPF 3/3/97
*/
#define OVERFLOW_STACK_SIZE \
(50 + \
CHECKED_REGS + \
UNCHECKED_REGS + \
EXTRA_STACK)
// This duplicates some code in reals.cpp but is now updated.
#define DOUBLESIZE (sizeof(double)/sizeof(POLYUNSIGNED))
union realdb { double dble; POLYUNSIGNED puns[DOUBLESIZE]; };
class IntTaskData: public TaskData {
public:
IntTaskData(): interrupt_requested(false), overflowPacket(0), dividePacket(0) {}
virtual void GarbageCollect(ScanAddress *process);
void ScanStackAddress(ScanAddress *process, PolyWord &val, StackSpace *stack);
virtual Handle EnterPolyCode(); // Start running ML
// Switch to Poly and return with the io function to call.
int SwitchToPoly();
virtual void SetException(poly_exn *exc);
virtual void InterruptCode();
// AddTimeProfileCount is used in time profiling.
virtual bool AddTimeProfileCount(SIGNALCONTEXT *context);
virtual void InitStackFrame(TaskData *newTask, Handle proc, Handle arg);
// These aren't implemented in the interpreted version.
virtual Handle EnterCallbackFunction(Handle func, Handle args) { ASSERT(0); return 0; }
// Increment or decrement the first word of the object pointed to by the
// mutex argument and return the new value.
virtual Handle AtomicIncrement(Handle mutexp);
// Set a mutex to one.
virtual void AtomicReset(Handle mutexp);
// Return the minimum space occupied by the stack. Used when setting a limit.
virtual POLYUNSIGNED currentStackSpace(void) const { return (this->stack->top - this->taskSp) + OVERFLOW_STACK_SIZE; }
virtual void addProfileCount(POLYUNSIGNED words) { add_count(this, taskPc, words); }
virtual void CopyStackFrame(StackObject *old_stack, POLYUNSIGNED old_length, StackObject *new_stack, POLYUNSIGNED new_length);
bool interrupt_requested;
// Allocate memory on the heap. Returns with the address of the cell. Does not set the
// length word or any of the data.
PolyObject *allocateMemory(POLYUNSIGNED words, POLYCODEPTR &pc, PolyWord *&sp)
{
words++; // Add the size of the length word.
// N.B. The allocation area may be empty so that both of these are zero.
if (this->allocPointer >= this->allocLimit + words)
{
this->allocPointer -= words;
return (PolyObject *)(this->allocPointer+1);
}
// Insufficient space.
SaveInterpreterState(pc, sp);
// Find some space to allocate in. Returns a pointer to the newly allocated space.
// N.B. This may return zero if the heap is exhausted and it has set this
// up for an exception. Generally it allocates by decrementing allocPointer
// but if the required memory is large it may allocate in a separate area.
PolyWord *space = processes->FindAllocationSpace(this, words, true);
LoadInterpreterState(pc, sp);
if (space == 0) return 0;
return (PolyObject *)(space+1);
}
// Put a real result in a "box"
PolyObject *boxDouble(double d, POLYCODEPTR &pc, PolyWord *&sp)
{
PolyObject *mem = this->allocateMemory(DOUBLESIZE, pc, sp);
if (mem == 0) return 0;
mem->SetLengthWord(DOUBLESIZE, F_BYTE_OBJ);
union realdb uniondb;
uniondb.dble = d;
// Copy the words. Depending on the word length this may copy one or more words.
for (unsigned i = 0; i < DOUBLESIZE; i++)
mem->Set(i, PolyWord::FromUnsigned(uniondb.puns[i]));
return mem;
}
// Extract a double value from a box.
double unboxDouble(PolyWord p)
{
union realdb uniondb;
for (unsigned i = 0; i < DOUBLESIZE; i++)
uniondb.puns[i] = p.AsObjPtr()->Get(i).AsUnsigned();
return uniondb.dble;
}
// Update the copies in the task object
void SaveInterpreterState(POLYCODEPTR pc, PolyWord *sp)
{
taskPc = pc;
taskSp = sp;
}
// Update the local state
void LoadInterpreterState(POLYCODEPTR &pc, PolyWord *&sp)
{
pc = taskPc;
sp = taskSp;
}
POLYCODEPTR taskPc; /* Program counter. */
PolyWord *taskSp; /* Stack pointer. */
PolyWord *hr;
PolyWord exception_arg;
bool raiseException;
PolyWord *sl; /* Stack limit register. */
PolyObject *overflowPacket, *dividePacket;
};
// This lock is used to synchronise all atomic operations.
// It is not needed in the X86 version because that can use a global
// memory lock.
static PLock mutexLock;
// Special value for return address.
#define SPECIAL_PC_END_THREAD TAGGED(1)
class Interpreter : public MachineDependent {
public:
Interpreter() {}
// Create a task data object.
virtual TaskData *CreateTaskData(void) { return new IntTaskData(); }
virtual Architectures MachineArchitecture(void) { return MA_Interpreted; }
};
void IntTaskData::InitStackFrame(TaskData *parentTask, Handle proc, Handle arg)
/* Initialise stack frame. */
{
StackSpace *space = this->stack;
StackObject *stack = (StackObject *)space->stack();
PolyObject *closure = DEREFWORDHANDLE(proc);
POLYUNSIGNED stack_size = space->spaceSize();
this->taskPc = *(byte**)(closure);
// this->taskSp = (PolyWord*)stack + stack_size-3; /* sp */
this->exception_arg = TAGGED(0); /* Used for exception argument. */
this->taskSp = (PolyWord*)stack + stack_size;
this->raiseException = false;
/* Set up exception handler */
/* No previous handler so point it at itself. */
this->taskSp--;
*(this->taskSp) = PolyWord::FromStackAddr(this->taskSp);
*(--this->taskSp) = SPECIAL_PC_END_THREAD; /* Default return address. */
this->hr = this->taskSp;
/* If this function takes an argument store it on the stack. */
if (arg != 0) *(--this->taskSp) = DEREFWORD(arg);
*(--this->taskSp) = SPECIAL_PC_END_THREAD; /* Return address. */
*(--this->taskSp) = closure; /* Closure address */
// Make packets for exceptions.
overflowPacket = makeExceptionPacket(parentTask, EXC_overflow);
dividePacket = makeExceptionPacket(parentTask, EXC_divide);
}
extern "C" {
typedef POLYUNSIGNED(*callFastRts0)();
typedef POLYUNSIGNED(*callFastRts1)(PolyWord);
typedef POLYUNSIGNED(*callFastRts2)(PolyWord, PolyWord);
typedef POLYUNSIGNED(*callFastRts3)(PolyWord, PolyWord, PolyWord);
typedef POLYUNSIGNED(*callFastRts4)(PolyWord, PolyWord, PolyWord, PolyWord);
typedef POLYUNSIGNED(*callFastRts5)(PolyWord, PolyWord, PolyWord, PolyWord, PolyWord);
typedef POLYUNSIGNED(*callFullRts0)(PolyObject *);
typedef POLYUNSIGNED(*callFullRts1)(PolyObject *, PolyWord);
typedef POLYUNSIGNED(*callFullRts2)(PolyObject *, PolyWord, PolyWord);
typedef POLYUNSIGNED(*callFullRts3)(PolyObject *, PolyWord, PolyWord, PolyWord);
typedef double (*callRTSFtoF) (double);
typedef double (*callRTSGtoF) (PolyWord);
}
void IntTaskData::InterruptCode()
/* Stop the Poly code at a suitable place. */
/* We may get an asynchronous interrupt at any time. */
{
IntTaskData *itd = (IntTaskData *)this;
itd->interrupt_requested = true;
}
void IntTaskData::SetException(poly_exn *exc)
/* Set up the stack of a process to raise an exception. */
{
this->raiseException = true;
*(--this->taskSp) = (PolyWord)exc; /* push exception data */
}
int IntTaskData::SwitchToPoly()
/* (Re)-enter the Poly code from C. */
{
// These are temporary values used where one instruction jumps to
// common code.
POLYUNSIGNED tailCount;
PolyWord *tailPtr;
POLYUNSIGNED returnCount;
POLYUNSIGNED storeWords;
POLYUNSIGNED stackCheck;
// Local values. These are copies of member variables but are used so frequently that
// it is important that access should be fast.
POLYCODEPTR pc;
PolyWord *sp;
LoadInterpreterState(pc, sp);
sl = (PolyWord*)this->stack->stack()+OVERFLOW_STACK_SIZE;
// We may have taken an interrupt which has set an exception.
if (this->raiseException) goto RAISE_EXCEPTION;
for(;;){ /* Each instruction */
switch(*pc++) {
case INSTR_enter_int: pc++; /* Skip the argument. */ break;
case INSTR_jump8false:
{
PolyWord u = *sp++; /* Pop argument */
if (u == True) { pc += 1; break; }
/* else - false - take the jump */
}
case INSTR_jump8: pc += *pc + 1; break;
case INSTR_jump16false:
{
PolyWord u = *sp++; /* Pop argument */
if (u == True) { pc += 2; break; }
/* else - false - take the jump */
}
case INSTR_jump16:
pc += arg1 + 2; break;
case INSTR_jump32False:
{
PolyWord u = *sp++; /* Pop argument */
if (u == True) { pc += 4; break; }
/* else - false - take the jump */
}
case INSTR_jump32:
{
// This is a 32-bit signed quantity on both 64-bits and 32-bits.
POLYSIGNED offset = pc[3] & 0x80 ? -1 : 0;
offset = (offset << 8) | pc[3];
offset = (offset << 8) | pc[2];
offset = (offset << 8) | pc[1];
offset = (offset << 8) | pc[0];
pc += offset + 4;
break;
}
case INSTR_push_handler: /* Save the old handler value. */
*(--sp) = PolyWord::FromStackAddr(this->hr); /* Push old handler */
break;
case INSTR_setHandler8: /* Set up a handler */
*(--sp) = PolyWord::FromCodePtr(pc + *pc + 1); /* Address of handler */
this->hr = sp;
pc += 1;
break;
case INSTR_setHandler16: /* Set up a handler */
*(--sp) = PolyWord::FromCodePtr(pc + arg1 + 2); /* Address of handler */
this->hr = sp;
pc += 2;
break;
case INSTR_setHandler32: /* Set up a handler */
{
POLYUNSIGNED offset = pc[0] + (pc[1] << 8) + (pc[2] << 16) + (pc[3] << 24);
*(--sp) = PolyWord::FromCodePtr(pc + offset + 4); /* Address of handler */
this->hr = sp;
pc += 4;
break;
}
case INSTR_del_handler: /* Delete handler retaining the result. */
{
PolyWord u = *sp++;
sp = this->hr;
if (*sp == TAGGED(0)) sp++; // Legacy
sp++; // Skip handler entry point
// Restore old handler
this->hr = (*sp).AsStackAddr();
*sp = u; // Put back the result
pc += *pc + 1; /* Skip the handler */
break;
}
case INSTR_deleteHandler: /* Delete handler retaining the result. */
{
PolyWord u = *sp++;
sp = this->hr;
sp++; // Remove handler entry point
this->hr = (*sp).AsStackAddr(); // Restore old handler
*sp = u; // Put back the result
break;
}
case INSTR_jump_i_false:
if (*sp++ == True) { pc += 1; break; }
/* else - false - take the jump */
case INSTR_jump_i_u: /* Indirect jump */
{
// This is always a forward jump
pc += *pc + 1;
pc += arg1 + 2;
break;
}
case INSTR_set_handler_new_i: /* Set up a handler */
{
byte *u = pc + *pc + 1;
*(--sp) = /* Address of handler */
PolyWord::FromCodePtr(u + u[0] + u[1]*256 + 2);
this->hr = sp;
pc += 1;
break;
}
case INSTR_del_handler_i: /* Delete handler retaining the result. */
{
PolyWord u = *sp++;
PolyWord *t;
sp = this->hr;
PolyWord *endStack = this->stack->top;
while((t = (*sp).AsStackAddr()) < sp || t > endStack) sp++;
this->hr = t;
*sp = u;
pc += *pc + 1; /* Skip the handler */
pc += arg1 + 2;
break;
}
case INSTR_case16:
{
// arg1 is the largest value that is in the range
POLYSIGNED u = UNTAGGED(*sp++); /* Get the value */
if (u > arg1 || u < 0) pc += (arg1+2)*2; /* Out of range */
else {
pc += 2;
pc += /* Index */pc[u*2]+pc[u*2 + 1]*256; }
break;
}
case INSTR_case32:
{
// arg1 is the number of cases i.e. one more than the largest value
// This is followed by that number of 32-bit offsets.
// If the value is out of range the default case is immediately after the table.
POLYSIGNED u = UNTAGGED(*sp++); /* Get the value */
if (u >= arg1 || u < 0) pc += 2 + arg1 * 4; /* Out of range */
else
{
pc += 2;
pc += /* Index */pc[u*4] + (pc[u*4+1] << 8) + (pc[u*4+2] << 16) + (pc[u*4+3] << 24);
}
break;
}
case INSTR_tail_3_b:
tailCount = 3;
tailPtr = sp + tailCount;
sp = tailPtr + *pc;
goto TAIL_CALL;
case INSTR_tail_3_2:
tailCount = 3;
tailPtr = sp + tailCount;
sp = tailPtr + 2;
goto TAIL_CALL;
case INSTR_tail_3_3:
tailCount = 3;
tailPtr = sp + tailCount;
sp = tailPtr + 3;
goto TAIL_CALL;
case INSTR_tail_4_b:
tailCount = 4;
tailPtr = sp + tailCount;
sp = tailPtr + *pc;
goto TAIL_CALL;
case INSTR_tail_b_b:
tailCount = *pc;
tailPtr = sp + tailCount;
sp = tailPtr + pc[1];
goto TAIL_CALL;
case INSTR_tail:
/* Tail recursive call. */
/* Move items up the stack. */
/* There may be an overlap if the function we are calling
has more args than this one. */
tailCount = arg1;
tailPtr = sp + tailCount;
sp = tailPtr + arg2;
TAIL_CALL: /* For general case. */
if (tailCount < 2) Crash("Invalid argument\n");
for (; tailCount > 0; tailCount--) *(--sp) = *(--tailPtr);
pc = (*sp++).AsCodePtr(); /* Pop the original return address. */
/* And drop through. */
case INSTR_call_closure: /* Closure call. */
{
PolyWord *t = (*sp).AsStackAddr(); /* Closure */
PolyWord u = *t; /* Get code address. (1st word of closure) */
sp--;
*sp = sp[1]; /* Move closure up. */
sp[1] = PolyWord::FromCodePtr(pc); /* Save return address. */
pc = u.AsCodePtr(); /* Get entry point. */
this->taskPc = pc; // Update in case we're profiling
// Legacy: Check for stack overflow. This is needed because
// old code does not have stack check instructions.
if (sp < sl)
{
POLYUNSIGNED min_size = this->stack->top - sp + OVERFLOW_STACK_SIZE;
SaveInterpreterState(pc, sp);
CheckAndGrowStack(this, min_size);
LoadInterpreterState(pc, sp);
sl = (PolyWord*)this->stack->stack() + OVERFLOW_STACK_SIZE;
}
if (this->interrupt_requested)
{
// Check for interrupts
this->interrupt_requested = false;
SaveInterpreterState(pc, sp);
return -1;
}
break;
}
case INSTR_return_w:
returnCount = arg1; /* Get no. of args to remove. */
RETURN: /* Common code for return. */
{
PolyWord result = *sp++; /* Result */
sp++; /* Remove the link/closure */
pc = (*sp++).AsCodePtr(); /* Return address */
sp += returnCount; /* Add on number of args. */
if (pc == (SPECIAL_PC_END_THREAD).AsCodePtr())
exitThread(this); // This thread is exiting.
*(--sp) = result; /* Result */
this->taskPc = pc; // Update in case we're profiling
}
break;
case INSTR_return_b: returnCount = *pc; goto RETURN;
case INSTR_return_0: returnCount = 0; goto RETURN;
case INSTR_return_1: returnCount = 1; goto RETURN;
case INSTR_return_2: returnCount = 2; goto RETURN;
case INSTR_return_3: returnCount = 3; goto RETURN;
case INSTR_stackSize8:
stackCheck = *pc++;
goto STACKCHECK;
case INSTR_stackSize16:
{
stackCheck = arg1; pc += 2;
STACKCHECK:
// Check there is space on the stack
if (sp - stackCheck < sl)
{
POLYUNSIGNED min_size = this->stack->top - sp + OVERFLOW_STACK_SIZE + stackCheck;
SaveInterpreterState(pc, sp);
CheckAndGrowStack(this, min_size);
LoadInterpreterState(pc, sp);
sl = (PolyWord*)this->stack->stack() + OVERFLOW_STACK_SIZE;
}
// Also check for interrupts
if (this->interrupt_requested)
{
// Check for interrupts
this->interrupt_requested = false;
SaveInterpreterState(pc, sp);
return -1;
}
break;
}
case INSTR_pad: /* No-op */ break;
case INSTR_raise_ex:
{
RAISE_EXCEPTION:
this->raiseException = false;
PolyException *exn = (PolyException*)((*sp).AsObjPtr());
this->exception_arg = exn; /* Get exception data */
sp = this->hr;
if (*sp == SPECIAL_PC_END_THREAD)
exitThread(this); // Default handler for thread.
pc = (*sp++).AsCodePtr();
this->hr = (*sp++).AsStackAddr();
break;
}
case INSTR_get_store_w:
// Get_store is now only used for mutually recursive closures. It allocates mutable store
// initialised to zero.
{
storeWords = arg1;
pc += 2;
GET_STORE:
PolyObject *p = this->allocateMemory(storeWords, pc, sp);
if (p == 0) goto RAISE_EXCEPTION;
p->SetLengthWord(storeWords, F_MUTABLE_BIT);
for(; storeWords > 0; ) p->Set(--storeWords, TAGGED(0)); /* Must initialise store! */
*(--sp) = p;
break;
}
case INSTR_get_store_2: storeWords = 2; goto GET_STORE;
case INSTR_get_store_3: storeWords = 3; goto GET_STORE;
case INSTR_get_store_4: storeWords = 4; goto GET_STORE;
case INSTR_get_store_b: storeWords = *pc; pc++; goto GET_STORE;
case INSTR_tuple_w:
{
storeWords = arg1; pc += 2;
TUPLE: /* Common code for tupling. */
PolyObject *p = this->allocateMemory(storeWords, pc, sp);
if (p == 0) goto RAISE_EXCEPTION; // Exception
p->SetLengthWord(storeWords, 0);
for(; storeWords > 0; ) p->Set(--storeWords, *sp++);
*(--sp) = p;
break;
}
case INSTR_tuple_2: storeWords = 2; goto TUPLE;
case INSTR_tuple_3: storeWords = 3; goto TUPLE;
case INSTR_tuple_4: storeWords = 4; goto TUPLE;
case INSTR_tuple_b: storeWords = *pc; pc++; goto TUPLE;
case INSTR_non_local:
{
PolyWord *t = sp+arg1;
POLYSIGNED uu;
for(uu = 1; uu <= arg2; uu++) t = (t[-1]).AsStackAddr();
uu = arg3; /* Can be negative. */
if (uu > 32767) uu -= 65536;
*(--sp) = t[uu];
pc += 6;
break;
}
case INSTR_local_w:
{
PolyWord u = sp[arg1];
*(--sp) = u;
pc += 2;
break;
}
case INSTR_indirect_w:
*sp = (*sp).AsObjPtr()->Get(arg1); pc += 2; break;
case INSTR_move_to_vec_w:
{
PolyWord u = *sp++;
(*sp).AsObjPtr()->Set(arg1, u);
pc += 2;
break;
}
case INSTR_set_stack_val_w:
{
PolyWord u = *sp++;
sp[arg1-1] = u;
pc += 2;
break;
}
case INSTR_reset_w: sp += arg1; pc += 2; break;
case INSTR_reset_r_w:
{
PolyWord u = *sp;
sp += arg1;
*sp = u;
pc += 2;
break;
}
case INSTR_constAddr8:
*(--sp) = *(PolyWord*)(pc + pc[0] + 1); pc += 1; break;
case INSTR_constAddr16:
*(--sp) = *(PolyWord*)(pc + arg1 + 2); pc += 2; break;
case INSTR_constAddr32:
{
POLYUNSIGNED offset = pc[0] + (pc[1] << 8) + (pc[2] << 16) + (pc[3] << 24);
*(--sp) = *(PolyWord*)(pc + offset + 4);
pc += 4;
break;
}
case INSTR_const_int_w: *(--sp) = TAGGED(arg1); pc += 2; break;
case INSTR_jump_back8:
pc -= *pc + 1;
if (this->interrupt_requested)
{
// Check for interrupt in case we're in a loop
this->interrupt_requested = false;
SaveInterpreterState(pc, sp);
return -1;
}
break;
case INSTR_jump_back16:
pc -= arg1 + 1;
if (this->interrupt_requested)
{
// Check for interrupt in case we're in a loop
this->interrupt_requested = false;
SaveInterpreterState(pc, sp);
return -1;
}
break;
case INSTR_lock:
{
PolyObject *obj = (*sp).AsObjPtr();
obj->SetLengthWord(obj->LengthWord() & ~_OBJ_MUTABLE_BIT);
break;
}
case INSTR_ldexc: *(--sp) = this->exception_arg; break;
case INSTR_local_b: { PolyWord u = sp[*pc]; *(--sp) = u; pc += 1; break; }
case INSTR_indirect_b:
*sp = (*sp).AsObjPtr()->Get(*pc); pc += 1; break;
case INSTR_move_to_vec_b:
{ PolyWord u = *sp++; (*sp).AsObjPtr()->Set(*pc, u); pc += 1; break; }
case INSTR_set_stack_val_b:
{ PolyWord u = *sp++; sp[*pc-1] = u; pc += 1; break; }
case INSTR_reset_b: sp += *pc; pc += 1; break;
case INSTR_reset_r_b:
{ PolyWord u = *sp; sp += *pc; *sp = u; pc += 1; break; }
case INSTR_const_int_b: *(--sp) = TAGGED(*pc); pc += 1; break;
case INSTR_local_0: { PolyWord u = sp[0]; *(--sp) = u; break; }
case INSTR_local_1: { PolyWord u = sp[1]; *(--sp) = u; break; }
case INSTR_local_2: { PolyWord u = sp[2]; *(--sp) = u; break; }
case INSTR_local_3: { PolyWord u = sp[3]; *(--sp) = u; break; }
case INSTR_local_4: { PolyWord u = sp[4]; *(--sp) = u; break; }
case INSTR_local_5: { PolyWord u = sp[5]; *(--sp) = u; break; }
case INSTR_local_6: { PolyWord u = sp[6]; *(--sp) = u; break; }
case INSTR_local_7: { PolyWord u = sp[7]; *(--sp) = u; break; }
case INSTR_local_8: { PolyWord u = sp[8]; *(--sp) = u; break; }
case INSTR_local_9: { PolyWord u = sp[9]; *(--sp) = u; break; }
case INSTR_local_10: { PolyWord u = sp[10]; *(--sp) = u; break; }
case INSTR_local_11: { PolyWord u = sp[11]; *(--sp) = u; break; }
case INSTR_indirect_0:
*sp = (*sp).AsObjPtr()->Get(0); break;
case INSTR_indirect_1:
*sp = (*sp).AsObjPtr()->Get(1); break;
case INSTR_indirect_2:
*sp = (*sp).AsObjPtr()->Get(2); break;
case INSTR_indirect_3:
*sp = (*sp).AsObjPtr()->Get(3); break;
case INSTR_indirect_4:
*sp = (*sp).AsObjPtr()->Get(4); break;
case INSTR_indirect_5:
*sp = (*sp).AsObjPtr()->Get(5); break;
case INSTR_const_0: *(--sp) = Zero; break;
case INSTR_const_1: *(--sp) = TAGGED(1); break;
case INSTR_const_2: *(--sp) = TAGGED(2); break;
case INSTR_const_3: *(--sp) = TAGGED(3); break;
case INSTR_const_4: *(--sp) = TAGGED(4); break;
case INSTR_const_10: *(--sp) = TAGGED(10); break;
case INSTR_move_to_vec_0: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(0, u); break; }
case INSTR_move_to_vec_1: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(1, u); break; }
case INSTR_move_to_vec_2: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(2, u); break; }
case INSTR_move_to_vec_3: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(3, u); break; }
case INSTR_move_to_vec_4: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(4, u); break; }
case INSTR_move_to_vec_5: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(5, u); break; }
case INSTR_move_to_vec_6: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(6, u); break; }
case INSTR_move_to_vec_7: { PolyWord u = *sp++; (*sp).AsObjPtr()->Set(7, u); break; }
case INSTR_reset_r_1: { PolyWord u = *sp; sp += 1; *sp = u; break; }
case INSTR_reset_r_2: { PolyWord u = *sp; sp += 2; *sp = u; break; }
case INSTR_reset_r_3: { PolyWord u = *sp; sp += 3; *sp = u; break; }
case INSTR_reset_1: sp += 1; break;
case INSTR_reset_2: sp += 2; break;
case INSTR_non_local_l_1:
{
POLYSIGNED uu = *pc;
PolyWord u = (sp[uu >> 4]).AsStackAddr()[(uu & 0xf) - 6];
*(--sp) = u;
pc += 1;
break;
}
case INSTR_non_local_l_2:
{
POLYSIGNED uu = *pc;
PolyWord *t = sp[uu >> 4].AsStackAddr() -1;
*(--sp) = (*t).AsStackAddr()[(uu & 0xf) - 6];
pc += 1;
break;
}
case INSTR_non_local_l_3:
{
POLYSIGNED uu = *pc;
PolyWord *t = sp[uu >> 4].AsStackAddr() -1;
t = (*t).AsStackAddr() - 1;
*(--sp) = (*t).AsStackAddr()[(uu & 0xf) - 6];
pc += 1; break;
}
case INSTR_stack_container:
{
POLYUNSIGNED words = arg1; pc += 2;
while (words-- > 0) *(--sp) = Zero;
sp--;
*sp = PolyWord::FromStackAddr(sp+1);
break;
}
case INSTR_tuple_container: /* Create a tuple from a container. */
{
storeWords = arg1;
PolyObject *t = this->allocateMemory(storeWords, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(storeWords, 0);
for(; storeWords > 0; )
{
storeWords--;
t->Set(storeWords, (*sp).AsObjPtr()->Get(storeWords));
}
*sp = t;
pc += 2;
break;
}
case INSTR_callFastRTS0:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
callFastRts0 doCall = (callFastRts0)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall();
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFastRTS1:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg1 = *sp++;
callFastRts1 doCall = (callFastRts1)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(rtsArg1);
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFastRTS2:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg2 = *sp++; // Pop off the args, last arg first.
PolyWord rtsArg1 = *sp++;
callFastRts2 doCall = (callFastRts2)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(rtsArg1, rtsArg2);
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFastRTS3:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg3 = *sp++; // Pop off the args, last arg first.
PolyWord rtsArg2 = *sp++;
PolyWord rtsArg1 = *sp++;
callFastRts3 doCall = (callFastRts3)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(rtsArg1, rtsArg2, rtsArg3);
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFastRTS4:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg4 = *sp++; // Pop off the args, last arg first.
PolyWord rtsArg3 = *sp++;
PolyWord rtsArg2 = *sp++;
PolyWord rtsArg1 = *sp++;
callFastRts4 doCall = (callFastRts4)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(rtsArg1, rtsArg2, rtsArg3, rtsArg4);
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFastRTS5:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg5 = *sp++; // Pop off the args, last arg first.
PolyWord rtsArg4 = *sp++;
PolyWord rtsArg3 = *sp++;
PolyWord rtsArg2 = *sp++;
PolyWord rtsArg1 = *sp++;
callFastRts5 doCall = (callFastRts5)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(rtsArg1, rtsArg2, rtsArg3, rtsArg4, rtsArg5);
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFullRTS0:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
callFullRts0 doCall = (callFullRts0)rtsCall.AsCodePtr();
this->raiseException = false;
SaveInterpreterState(pc, sp);
POLYUNSIGNED result = doCall(this->threadObject);
LoadInterpreterState(pc, sp);
// If this raised an exception
if (this->raiseException) goto RAISE_EXCEPTION;
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFullRTS1:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg1 = *sp++;
this->raiseException = false;
SaveInterpreterState(pc, sp);
callFullRts1 doCall = (callFullRts1)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(this->threadObject, rtsArg1);
LoadInterpreterState(pc, sp);
// If this raised an exception
if (this->raiseException) goto RAISE_EXCEPTION;
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFullRTS2:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg2 = *sp++; // Pop off the args, last arg first.
PolyWord rtsArg1 = *sp++;
this->raiseException = false;
SaveInterpreterState(pc, sp);
callFullRts2 doCall = (callFullRts2)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(this->threadObject, rtsArg1, rtsArg2);
LoadInterpreterState(pc, sp);
// If this raised an exception
if (this->raiseException) goto RAISE_EXCEPTION;
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFullRTS3:
{
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg3 = *sp++; // Pop off the args, last arg first.
PolyWord rtsArg2 = *sp++;
PolyWord rtsArg1 = *sp++;
this->raiseException = false;
SaveInterpreterState(pc, sp);
callFullRts3 doCall = (callFullRts3)rtsCall.AsCodePtr();
POLYUNSIGNED result = doCall(this->threadObject, rtsArg1, rtsArg2, rtsArg3);
LoadInterpreterState(pc, sp);
// If this raised an exception
if (this->raiseException) goto RAISE_EXCEPTION;
*(--sp) = PolyWord::FromUnsigned(result);
break;
}
case INSTR_callFastFtoF:
{
// Floating point call. The call itself does not allocate but we
// need to put the result into a "box".
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg1 = *sp++;
callRTSFtoF doCall = (callRTSFtoF)rtsCall.AsCodePtr();
double argument = unboxDouble(rtsArg1);
// Allocate memory for the result.
double result = doCall(argument);
PolyObject *t = boxDouble(result, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*(--sp) = t;
break;
}
case INSTR_callFastGtoF:
{
// Call that takes a POLYUNSIGNED argument and returns a double.
PolyWord rtsCall = (*sp++).AsObjPtr()->Get(0); // Value holds address.
PolyWord rtsArg1 = *sp++;
callRTSGtoF doCall = (callRTSGtoF)rtsCall.AsCodePtr();
// Allocate memory for the result.
double result = doCall(rtsArg1);
PolyObject *t = boxDouble(result, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*(--sp) = t;
break;
}
case INSTR_notBoolean:
*sp = (*sp == True) ? False : True; break;
case INSTR_isTagged:
*sp = IS_INT(*sp) ? True : False; break;
case INSTR_cellLength:
/* Return the length word. */
*sp = TAGGED((*sp).AsObjPtr()->Length());
break;
case INSTR_cellFlags:
{
PolyObject *p = (*sp).AsObjPtr();
POLYUNSIGNED f = (p->LengthWord()) >> OBJ_PRIVATE_FLAGS_SHIFT;
*sp = TAGGED(f);
break;
}
case INSTR_clearMutable:
{
PolyObject *obj = (*sp).AsObjPtr();
POLYUNSIGNED lengthW = obj->LengthWord();
/* Clear the mutable bit. */
obj->SetLengthWord(lengthW & ~_OBJ_MUTABLE_BIT);
*sp = Zero;
break;
}
case INSTR_stringLength: // Now replaced by loadUntagged
*sp = TAGGED(((PolyStringObject*)(*sp).AsObjPtr())->length);
break;
case INSTR_atomicIncr:
{
PLocker l(&mutexLock);
PolyObject *p = (*sp).AsObjPtr();
PolyWord newValue = TAGGED(UNTAGGED(p->Get(0))+1);
p->Set(0, newValue);
*sp = newValue;
break;
}
case INSTR_atomicDecr:
{
PLocker l(&mutexLock);
PolyObject *p = (*sp).AsObjPtr();
PolyWord newValue = TAGGED(UNTAGGED(p->Get(0))-1);
p->Set(0, newValue);
*sp = newValue;
break;
}
case INSTR_atomicReset:
{
// This is needed in the interpreted version otherwise there
// is a chance that we could set the value to zero while another
// thread is between getting the old value and setting it to the new value.
PLocker l(&mutexLock);
PolyObject *p = (*sp).AsObjPtr();
p->Set(0, TAGGED(1)); // Set this to released.
*sp = TAGGED(0); // Push the unit result
break;
}
case INSTR_longWToTagged:
{
// Extract the first word and return it as a tagged value. This loses the top-bit
POLYUNSIGNED wx = (*sp).AsObjPtr()->Get(0).AsUnsigned();
*sp = TAGGED(wx);
break;
}
case INSTR_signedToLongW:
{
// Shift the tagged value to remove the tag and put it into the first word.
// The original sign bit is copied in the shift.
POLYSIGNED wx = (*sp).UnTagged();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromSigned(wx));
*sp = t;
break;
}
case INSTR_unsignedToLongW:
{
// As with the above except the value is treated as an unsigned
// value and the top bit is zero.
POLYUNSIGNED wx = (*sp).UnTaggedUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wx));
*sp = t;
break;
}
case INSTR_realAbs:
{
PolyObject *t = this->boxDouble(fabs(unboxDouble(*sp)), pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_realNeg:
{
PolyObject *t = this->boxDouble(-(unboxDouble(*sp)), pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_floatFixedInt:
{
POLYSIGNED u = UNTAGGED(*sp);
PolyObject *t = this->boxDouble((double)u, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_equalWord:
{
PolyWord u = *sp++;
*sp = u == *sp ? True : False;
break;
}
case INSTR_lessSigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsSigned() < u.AsSigned()) ? True : False;
break;
}
case INSTR_lessUnsigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsUnsigned() < u.AsUnsigned()) ? True : False;
break;
}
case INSTR_lessEqSigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsSigned() <= u.AsSigned()) ? True : False;
break;
}
case INSTR_lessEqUnsigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsUnsigned() <= u.AsUnsigned()) ? True : False;
break;
}
case INSTR_greaterSigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsSigned() > u.AsSigned()) ? True : False;
break;
}
case INSTR_greaterUnsigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsUnsigned() > u.AsUnsigned()) ? True : False;
break;
}
case INSTR_greaterEqSigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsSigned() >= u.AsSigned()) ? True : False;
break;
}
case INSTR_greaterEqUnsigned:
{
PolyWord u = *sp++;
*sp = ((*sp).AsUnsigned() >= u.AsUnsigned()) ? True : False;
break;
}
case INSTR_fixedAdd:
{
PolyWord x = *sp++;
PolyWord y = *sp;
POLYSIGNED t = UNTAGGED(x) + UNTAGGED(y);
if (t <= MAXTAGGED && t >= -MAXTAGGED-1)
*sp = TAGGED(t);
else
{
*(--sp) = overflowPacket;
goto RAISE_EXCEPTION;
}
break;
}
case INSTR_fixedSub:
{
PolyWord x = *sp++;
PolyWord y = *sp;
POLYSIGNED t = UNTAGGED(y) - UNTAGGED(x);
if (t <= MAXTAGGED && t >= -MAXTAGGED-1)
*sp = TAGGED(t);
else
{
*(--sp) = overflowPacket;
goto RAISE_EXCEPTION;
}
break;
}
case INSTR_fixedMult:
{
// We need to detect overflow. There doesn't seem to be any convenient way to do
// this so we use the arbitrary precision package and check whether the result is short.
// Clang and GCC 5.0 have __builtin_mul_overflow which will do this but GCC 5.0 is not
// currently (July 2016) in Debian stable.
Handle reset = this->saveVec.mark();
Handle pushedArg1 = this->saveVec.push(*sp++);
Handle pushedArg2 = this->saveVec.push(*sp);
Handle result = mult_longc(this, pushedArg2, pushedArg1);
PolyWord res = result->Word();
this->saveVec.reset(reset);
if (! res.IsTagged())
{
*(--sp) = overflowPacket;
goto RAISE_EXCEPTION;
}
*sp = res;
break;
}
case INSTR_fixedQuot:
{
// Zero and overflow are checked for in ML.
POLYSIGNED u = UNTAGGED(*sp++);
PolyWord y = *sp;
*sp = TAGGED(UNTAGGED(y) / u);
break;
}
case INSTR_fixedRem:
{
// Zero and overflow are checked for in ML.
POLYSIGNED u = UNTAGGED(*sp++);
PolyWord y = *sp;
*sp = TAGGED(UNTAGGED(y) % u);
break;
}
case INSTR_wordAdd:
{
PolyWord u = *sp++;
// Because we're not concerned with overflow we can just add the values and subtract the tag.
*sp = PolyWord::FromUnsigned((*sp).AsUnsigned() + u.AsUnsigned() - TAGGED(0).AsUnsigned());
break;
}
case INSTR_wordSub:
{
PolyWord u = *sp++;
*sp = PolyWord::FromUnsigned((*sp).AsUnsigned() - u.AsUnsigned() + TAGGED(0).AsUnsigned());
break;
}
case INSTR_wordMult:
{
PolyWord u = *sp++;
*sp = TAGGED(UNTAGGED_UNSIGNED(*sp) * UNTAGGED_UNSIGNED(u));
break;
}
case INSTR_wordDiv:
{
POLYUNSIGNED u = UNTAGGED_UNSIGNED(*sp++);
// Detection of zero is done in ML
*sp = TAGGED(UNTAGGED_UNSIGNED(*sp) / u); break;
}
case INSTR_wordMod:
{
POLYUNSIGNED u = UNTAGGED_UNSIGNED(*sp++);
*sp = TAGGED(UNTAGGED_UNSIGNED(*sp) % u);
break;
}
case INSTR_wordAnd:
{
PolyWord u = *sp++;
// Since both of these should be tagged the tag bit will be preserved.
*sp = PolyWord::FromUnsigned((*sp).AsUnsigned() & u.AsUnsigned());
break;
}
case INSTR_wordOr:
{
PolyWord u = *sp++;
// Since both of these should be tagged the tag bit will be preserved.
*sp = PolyWord::FromUnsigned((*sp).AsUnsigned() | u.AsUnsigned());
break;
}
case INSTR_wordXor:
{
PolyWord u = *sp++;
// This will remove the tag bit so it has to be reinstated.
*sp = PolyWord::FromUnsigned(((*sp).AsUnsigned() ^ u.AsUnsigned()) | TAGGED(0).AsUnsigned());
break;
}
case INSTR_wordShiftLeft:
{
// ML requires shifts greater than a word to return zero.
// That's dealt with at the higher level.
PolyWord u = *sp++;
*sp = TAGGED(UNTAGGED_UNSIGNED(*sp) << UNTAGGED_UNSIGNED(u));
break;
}
case INSTR_wordShiftRLog:
{
PolyWord u = *sp++;
*sp = TAGGED(UNTAGGED_UNSIGNED(*sp) >> UNTAGGED_UNSIGNED(u));
break;
}
case INSTR_wordShiftRArith:
{
PolyWord u = *sp++;
// Strictly speaking, C does not require that this uses
// arithmetic shifting so we really ought to set the
// high-order bits explicitly.
*sp = TAGGED(UNTAGGED(*sp) >> UNTAGGED(u));
break;
}
case INSTR_allocByteMem:
{
// Allocate byte segment. This does not need to be initialised.
POLYUNSIGNED flags = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp);
PolyObject *t = this->allocateMemory(length, pc, sp);
if (t == 0) goto RAISE_EXCEPTION; // Exception
t->SetLengthWord(length, (byte)flags);
*sp = t;
break;
}
case INSTR_lgWordEqual:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
*sp = wx == wy ? True : False;
break;
}
case INSTR_lgWordNotequal:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
*sp = wx != wy ? True : False;
break;
}
case INSTR_lgWordLess:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
*sp = (wy < wx) ? True : False;
break;
}
case INSTR_lgWordLessEq:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
*sp = (wy <= wx) ? True : False;
break;
}
case INSTR_lgWordGreater:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
*sp = (wy > wx) ? True : False;
break;
}
case INSTR_lgWordGreaterEq:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
*sp = (wy >= wx) ? True : False;
break;
}
case INSTR_lgWordAdd:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy+wx));
*sp = t;
break;
}
case INSTR_lgWordSub:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy-wx));
*sp = t;
break;
}
case INSTR_lgWordMult:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy*wx));
*sp = t;
break;
}
case INSTR_lgWordDiv:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy/wx));
*sp = t;
break;
}
case INSTR_lgWordMod:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy%wx));
*sp = t;
break;
}
case INSTR_lgWordAnd:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy&wx));
*sp = t;
break;
}
case INSTR_lgWordOr:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy|wx));
*sp = t;
break;
}
case INSTR_lgWordXor:
{
POLYUNSIGNED wx = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy^wx));
*sp = t;
break;
}
case INSTR_lgWordShiftLeft:
{
// The shift amount is a tagged word not a boxed large word
POLYUNSIGNED wx = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy << wx));
*sp = t;
break;
}
case INSTR_lgWordShiftRLog:
{
// The shift amount is a tagged word not a boxed large word
POLYUNSIGNED wx = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED wy = (*sp).AsObjPtr()->Get(0).AsUnsigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(wy >> wx));
*sp = t;
break;
}
case INSTR_lgWordShiftRArith:
{
// The shift amount is a tagged word not a boxed large word
POLYUNSIGNED wx = UNTAGGED_UNSIGNED(*sp++);
POLYSIGNED wy = (*sp).AsObjPtr()->Get(0).AsSigned();
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromSigned(wy >> wx));
*sp = t;
break;
}
case INSTR_realEqual:
{
double u = unboxDouble(*sp++);
*sp = u == unboxDouble(*sp) ? True: False;
break;
}
case INSTR_realLess:
{
double u = unboxDouble(*sp++);
*sp = unboxDouble(*sp) < u ? True: False;
break;
}
case INSTR_realLessEq:
{
double u = unboxDouble(*sp++);
*sp = unboxDouble(*sp) <= u ? True: False;
break;
}
case INSTR_realGreater:
{
double u = unboxDouble(*sp++);
*sp = unboxDouble(*sp) > u ? True: False;
break;
}
case INSTR_realGreaterEq:
{
double u = unboxDouble(*sp++);
*sp = unboxDouble(*sp) >= u ? True: False;
break;
}
case INSTR_realAdd:
{
double u = unboxDouble(*sp++);
double v = unboxDouble(*sp);
PolyObject *t = this->boxDouble(v+u, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_realSub:
{
double u = unboxDouble(*sp++);
double v = unboxDouble(*sp);
PolyObject *t = this->boxDouble(v-u, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_realMult:
{
double u = unboxDouble(*sp++);
double v = unboxDouble(*sp);
PolyObject *t = this->boxDouble(v*u, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_realDiv:
{
double u = unboxDouble(*sp++);
double v = unboxDouble(*sp);
PolyObject *t = this->boxDouble(v/u, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_getThreadId:
*(--sp) = this->threadObject;
break;
case INSTR_allocWordMemory:
{
// Allocate word segment. This must be initialised.
// We mustn't pop the initialiser until after any potential GC.
POLYUNSIGNED length = UNTAGGED_UNSIGNED(sp[2]);
PolyObject *t = this->allocateMemory(length, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
PolyWord initialiser = *sp++;
POLYUNSIGNED flags = UNTAGGED_UNSIGNED(*sp++);
t->SetLengthWord(length, (byte)flags);
*sp = t;
// Have to initialise the data.
for (; length > 0; ) t->Set(--length, initialiser);
break;
}
case INSTR_alloc_ref:
{
// Allocate a single word mutable cell. This is more common than allocWordMemory on its own.
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
PolyWord initialiser = *sp;
t->SetLengthWord(1, F_MUTABLE_BIT);
t->Set(0, initialiser);
*sp = t;
break;
}
case INSTR_loadMLWord:
{
// The values on the stack are base, index and offset.
POLYUNSIGNED offset = UNTAGGED(*sp++);
POLYUNSIGNED index = UNTAGGED(*sp++);
PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset);
*sp = p->Get(index);
break;
}
case INSTR_loadMLByte:
{
// The values on the stack are base and index.
POLYUNSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsCodePtr();
*sp = TAGGED(p[index]); // Have to tag the result
break;
}
case INSTR_loadC8:
{
// This is similar to loadMLByte except that the base address is a boxed large-word.
// Also the index is SIGNED.
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr();
*sp = TAGGED(p[index]); // Have to tag the result
break;
}
case INSTR_loadC16:
{
// This and the other loads are similar to loadMLWord with separate
// index and offset values.
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
POLYUNSIGNED r = ((uint16_t*)p)[index];
*sp = TAGGED(r);
break;
}
case INSTR_loadC32:
{
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
POLYUNSIGNED r = ((uint32_t*)p)[index];
#if (SIZEOF_VOIDP == 8)
// This is tagged in 64-bit mode
*sp = TAGGED(r);
#else
// But boxed in 32-bit mode.
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(r));
*sp = t;
#endif
break;
}
#if (SIZEOF_VOIDP == 8)
case INSTR_loadC64:
{
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
POLYUNSIGNED r = ((uint64_t*)p)[index];
// This must be boxed.
PolyObject *t = this->allocateMemory(1, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
t->SetLengthWord(1, F_BYTE_OBJ);
t->Set(0, PolyWord::FromUnsigned(r));
*sp = t;
break;
}
#endif
case INSTR_loadCFloat:
{
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
double r = ((float*)p)[index];
// This must be boxed.
PolyObject *t = this->boxDouble(r, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_loadCDouble:
{
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
double r = ((double*)p)[index];
// This must be boxed.
PolyObject *t = this->boxDouble(r, pc, sp);
if (t == 0) goto RAISE_EXCEPTION;
*sp = t;
break;
}
case INSTR_loadUntagged:
{
// The values on the stack are base, index and offset.
POLYUNSIGNED offset = UNTAGGED(*sp++);
POLYUNSIGNED index = UNTAGGED(*sp++);
PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset);
*sp = TAGGED(p->Get(index).AsUnsigned());
break;
}
case INSTR_storeMLWord:
{
PolyWord toStore = *sp++;
POLYUNSIGNED offset = UNTAGGED(*sp++);
POLYUNSIGNED index = UNTAGGED(*sp++);
PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset);
p->Set(index, toStore);
*sp = Zero;
break;
}
case INSTR_storeMLByte:
{
POLYUNSIGNED toStore = UNTAGGED(*sp++);
POLYUNSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsCodePtr();
p[index] = (byte)toStore;
*sp = Zero;
break;
}
case INSTR_storeC8:
{
// Similar to storeMLByte except that the base address is a boxed large-word.
POLYUNSIGNED toStore = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr();
p[index] = (byte)toStore;
*sp = Zero;
break;
}
case INSTR_storeC16:
{
uint16_t toStore = (uint16_t)UNTAGGED(*sp++);
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
((uint16_t*)p)[index] = toStore;
*sp = Zero;
break;
}
case INSTR_storeC32:
{
#if (SIZEOF_VOIDP == 8)
// This is a tagged value in 64-bit mode.
uint32_t toStore = (uint32_t)UNTAGGED(*sp++);
#else
// but a boxed value in 32-bit mode.
uint32_t toStore = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
#endif
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
((uint32_t*)p)[index] = toStore;
*sp = Zero;
break;
}
#if (SIZEOF_VOIDP == 8)
case INSTR_storeC64:
{
// This is a boxed value.
uint64_t toStore = (*sp++).AsObjPtr()->Get(0).AsUnsigned();
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
((uint64_t*)p)[index] = toStore;
*sp = Zero;
break;
}
#endif
case INSTR_storeCFloat:
{
// This is a boxed value.
float toStore = (float)unboxDouble(*sp++);
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
((float*)p)[index] = toStore;
*sp = Zero;
break;
}
case INSTR_storeCDouble:
{
// This is a boxed value.
double toStore = unboxDouble(*sp++);
POLYSIGNED offset = UNTAGGED(*sp++);
POLYSIGNED index = UNTAGGED(*sp++);
POLYCODEPTR p = (*sp).AsObjPtr()->Get(0).AsCodePtr() + offset;
((double*)p)[index] = toStore;
*sp = Zero;
break;
}
case INSTR_storeUntagged:
{
PolyWord toStore = PolyWord::FromUnsigned(UNTAGGED_UNSIGNED(*sp++));
POLYUNSIGNED offset = UNTAGGED(*sp++);
POLYUNSIGNED index = UNTAGGED(*sp++);
PolyObject *p = (PolyObject*)((*sp).AsCodePtr() + offset);
p->Set(index, toStore);
*sp = Zero;
break;
}
case INSTR_blockMoveWord:
{
// The offsets are byte counts but the the indexes are in words.
POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED destOffset = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED destIndex = UNTAGGED_UNSIGNED(*sp++);
PolyObject *dest = (PolyObject*)((*sp++).AsCodePtr() + destOffset);
POLYUNSIGNED srcOffset = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED srcIndex = UNTAGGED_UNSIGNED(*sp++);
PolyObject *src = (PolyObject*)((*sp).AsCodePtr() + srcOffset);
for (POLYUNSIGNED u = 0; u < length; u++) dest->Set(destIndex+u, src->Get(srcIndex+u));
*sp = Zero;
break;
}
case INSTR_blockMoveByte:
{
POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED destOffset = UNTAGGED_UNSIGNED(*sp++);
POLYCODEPTR dest = (*sp++).AsCodePtr();
POLYUNSIGNED srcOffset = UNTAGGED_UNSIGNED(*sp++);
POLYCODEPTR src = (*sp).AsCodePtr();
memcpy(dest+destOffset, src+srcOffset, length);
*sp = Zero;
break;
}
case INSTR_blockEqualByte:
{
POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED arg2Offset = UNTAGGED_UNSIGNED(*sp++);
POLYCODEPTR arg2Ptr = (*sp++).AsCodePtr();
POLYUNSIGNED arg1Offset = UNTAGGED_UNSIGNED(*sp++);
POLYCODEPTR arg1Ptr = (*sp).AsCodePtr();
*sp = memcmp(arg1Ptr+arg1Offset, arg2Ptr+arg2Offset, length) == 0 ? True : False;
break;
}
case INSTR_blockCompareByte:
{
POLYUNSIGNED length = UNTAGGED_UNSIGNED(*sp++);
POLYUNSIGNED arg2Offset = UNTAGGED_UNSIGNED(*sp++);
POLYCODEPTR arg2Ptr = (*sp++).AsCodePtr();
POLYUNSIGNED arg1Offset = UNTAGGED_UNSIGNED(*sp++);
POLYCODEPTR arg1Ptr = (*sp).AsCodePtr();
int result = memcmp(arg1Ptr+arg1Offset, arg2Ptr+arg2Offset, length);
*sp = result == 0 ? TAGGED(0) : result < 0 ? TAGGED(-1) : TAGGED(1);
break;
}
default: Crash("Unknown instruction %x\n", pc[-1]);
} /* switch */
} /* for */
return 0;
} /* MD_switch_to_poly */
void IntTaskData::GarbageCollect(ScanAddress *process)
{
TaskData::GarbageCollect(process);
overflowPacket = process->ScanObjectAddress(overflowPacket);
dividePacket = process->ScanObjectAddress(dividePacket);
if (stack != 0)
{
StackSpace *stackSpace = stack;
PolyWord *stackPtr = this->taskSp;
// The exception arg if any
ScanStackAddress(process, this->exception_arg, stackSpace);
// Now the values on the stack.
for (PolyWord *q = stackPtr; q < stackSpace->top; q++)
ScanStackAddress(process, *q, stackSpace);
}
}
// Process a value within the stack.
void IntTaskData::ScanStackAddress(ScanAddress *process, PolyWord &val, StackSpace *stack)
{
if (! val.IsDataPtr()) return;
MemSpace *space = gMem.LocalSpaceForAddress(val.AsStackAddr()-1);
if (space != 0)
val = process->ScanObjectAddress(val.AsObjPtr());
}
// Copy a stack
void IntTaskData::CopyStackFrame(StackObject *old_stack, POLYUNSIGNED old_length, StackObject *new_stack, POLYUNSIGNED new_length)
{
/* Moves a stack, updating all references within the stack */
PolyWord *old_base = (PolyWord *)old_stack;
PolyWord *new_base = (PolyWord*)new_stack;
PolyWord *old_top = old_base + old_length;
/* Calculate the offset of the new stack from the old. If the frame is
being extended objects in the new frame will be further up the stack
than in the old one. */
POLYSIGNED offset = new_base - old_base + new_length - old_length;
PolyWord *oldSp = this->taskSp;
this->taskSp = oldSp + offset;
this->hr = this->hr + offset;
/* Skip the unused part of the stack. */
POLYUNSIGNED i = oldSp - old_base;
ASSERT (i <= old_length);
i = old_length - i;
PolyWord *old = oldSp;
PolyWord *newp= this->taskSp;
while (i--)
{
// ASSERT(old >= old_base && old < old_base+old_length);
// ASSERT(newp >= new_base && newp < new_base+new_length);
PolyWord old_word = *old++;
if (old_word.IsTagged() || old_word.AsStackAddr() < old_base || old_word.AsStackAddr() >= old_top)
*newp++ = old_word;
else
*newp++ = PolyWord::FromStackAddr(old_word.AsStackAddr() + offset);
}
ASSERT(old == ((PolyWord*)old_stack)+old_length);
ASSERT(newp == ((PolyWord*)new_stack)+new_length);
}
Handle IntTaskData::EnterPolyCode()
/* Called from "main" to enter the code. */
{
Handle hOriginal = this->saveVec.mark(); // Set this up for the IO calls.
while (1)
{
this->saveVec.reset(hOriginal); // Remove old RTS arguments and results.
// Run the ML code and return with the function to call.
this->inML = true;
int ioFunction = SwitchToPoly();
this->inML = false;
try {
switch (ioFunction)
{
case -1:
// We've been interrupted. This usually involves simulating a
// stack overflow so we could come here because of a genuine
// stack overflow.
// Previously this code was executed on every RTS call but there
// were problems on Mac OS X at least with contention on schedLock.
// Process any asynchronous events i.e. interrupts or kill
processes->ProcessAsynchRequests(this);
// Release and re-acquire use of the ML memory to allow another thread
// to GC.
processes->ThreadReleaseMLMemory(this);
processes->ThreadUseMLMemory(this);
break;
case -2: // A callback has returned.
ASSERT(0); // Callbacks aren't implemented
default:
Crash("Unknown io operation %d\n", ioFunction);
}
}
catch (IOException &) {
}
}
}
// As far as possible we want locking and unlocking an ML mutex to be fast so
// we try to implement the code in the assembly code using appropriate
// interlocked instructions. That does mean that if we need to lock and
// unlock an ML mutex in this code we have to use the same, machine-dependent,
// code to do it. These are defaults that are used where there is no
// machine-specific code.
static Handle ProcessAtomicIncrement(TaskData *taskData, Handle mutexp)
{
PLocker l(&mutexLock);
PolyObject *p = DEREFHANDLE(mutexp);
// A thread can only call this once so the values will be short
PolyWord newValue = TAGGED(UNTAGGED(p->Get(0))+1);
p->Set(0, newValue);
return taskData->saveVec.push(newValue);
}
// Release a mutex. We need to lock the mutex to ensure we don't
// reset it in the time between one of atomic operations reading
// and writing the mutex.
static Handle ProcessAtomicReset(TaskData *taskData, Handle mutexp)
{
PLocker l(&mutexLock);
DEREFHANDLE(mutexp)->Set(0, TAGGED(1)); // Set this to released.
return taskData->saveVec.push(TAGGED(0)); // Push the unit result
}
Handle IntTaskData::AtomicIncrement(Handle mutexp)
{
return ProcessAtomicIncrement(this, mutexp);
}
void IntTaskData::AtomicReset(Handle mutexp)
{
(void)ProcessAtomicReset(this, mutexp);
}
bool IntTaskData::AddTimeProfileCount(SIGNALCONTEXT *context)
{
if (taskPc != 0)
{
// See if the PC we've got is an ML code address.
MemSpace *space = gMem.SpaceForAddress(taskPc);
if (space != 0 && (space->spaceType == ST_CODE || space->spaceType == ST_PERMANENT))
{
add_count(this, taskPc, 1);
return true;
}
}
return false;
}
static Interpreter interpreterObject;
MachineDependent *machineDependent = &interpreterObject;
// PolySetCodeConstant is not actually used in the interpreted version.
// It is used in the X86 code-generator to insert inline constants.
// Compat560 creates an RTS function unconditionally and rather than change
// that it's easier to add it here for the time being.
extern "C" {
POLYEXTERNALSYMBOL POLYUNSIGNED PolySetCodeConstant(byte *pointer, PolyWord offset, PolyWord c, PolyWord flags);
}
POLYUNSIGNED PolySetCodeConstant(byte *pointer, PolyWord offset, PolyWord c, PolyWord flags)
{
return TAGGED(0).AsUnsigned();
}
struct _entrypts machineSpecificEPT[] =
{
{ "PolySetCodeConstant", (polyRTSFunction)&PolySetCodeConstant},
{ NULL, NULL} // End of list.
};
|