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
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Microsoft Public License. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Microsoft Public License, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Threading;
namespace Microsoft.Scripting.Interpreter {
public interface IInstructionProvider {
void AddInstructions(LightCompiler compiler);
}
public abstract class Instruction {
public virtual int ConsumedStack { get { return 0; } }
public virtual int ProducedStack { get { return 0; } }
public abstract int Run(InterpretedFrame frame);
public virtual string InstructionName {
get { return GetType().Name.Replace("Instruction", ""); }
}
public override string ToString() {
return InstructionName + "()";
}
}
#region Basic Stack Operations
public class PushInstruction : Instruction {
private object _value;
public PushInstruction(object value) {
this._value = value;
}
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Data[frame.StackIndex++] = _value;
return +1;
}
public override string ToString() {
return "Push(" + (_value ?? "null") + ")";
}
}
public class PopInstruction : Instruction {
public static PopInstruction Instance = new PopInstruction();
private PopInstruction() { }
public override int ConsumedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Pop();
return +1;
}
public override string ToString() {
return "Pop()";
}
}
public class DupInstruction : Instruction {
public static DupInstruction Instance = new DupInstruction();
private DupInstruction() { }
public override int ConsumedStack { get { return 0; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Data[frame.StackIndex++] = frame.Peek();
return +1;
}
public override string ToString() {
return "Dup()";
}
}
#endregion
#region Local Variables
public interface IBoxableInstruction {
Instruction BoxIfIndexMatches(int index);
}
public abstract class LocalAccessInstruction : Instruction {
internal readonly int _index;
public LocalAccessInstruction(int index) {
_index = index;
}
#if DEBUG
public override string ToString() {
return InstructionName + "(" + _name + ": " + _index + ")";
}
private string _name;
#endif
[Conditional("DEBUG")]
public void SetName(string name) {
#if DEBUG
_name = name;
#endif
}
[Conditional("DEBUG")]
public void SetName(LocalAccessInstruction other) {
#if DEBUG
_name = other._name;
#endif
}
}
public sealed class GetLocalInstruction : LocalAccessInstruction, IBoxableInstruction {
public GetLocalInstruction(int index)
: base(index) {
}
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Data[frame.StackIndex++] = frame.Data[_index];
//frame.Push(frame.Data[_index]);
return +1;
}
public Instruction BoxIfIndexMatches(int index) {
if (index == _index) {
var result = new GetBoxedLocalInstruction(index);
result.SetName(this);
return result;
} else {
return null;
}
}
}
public sealed class GetBoxedLocalInstruction : LocalAccessInstruction {
public GetBoxedLocalInstruction(int index)
: base(index) {
}
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
var box = (StrongBox<object>)frame.Data[_index];
frame.Data[frame.StackIndex++] = box.Value;
return +1;
}
}
public sealed class GetClosureInstruction : LocalAccessInstruction {
public GetClosureInstruction(int index)
: base(index) {
}
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
var box = frame.Closure[_index];
frame.Data[frame.StackIndex++] = box.Value;
return +1;
}
}
public sealed class GetBoxedClosureInstruction : LocalAccessInstruction {
public GetBoxedClosureInstruction(int index)
: base(index) {
}
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
var box = frame.Closure[_index];
frame.Data[frame.StackIndex++] = box;
return +1;
}
}
public sealed class SetLocalInstruction : LocalAccessInstruction, IBoxableInstruction {
public SetLocalInstruction(int index)
: base(index) {
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Data[_index] = frame.Peek();
return +1;
}
public Instruction BoxIfIndexMatches(int index) {
if (index == _index) {
var result = new SetBoxedLocalInstruction(index);
result.SetName(this);
return result;
} else {
return null;
}
}
}
public sealed class SetBoxedLocalInstruction : LocalAccessInstruction {
public SetBoxedLocalInstruction(int index)
: base(index) {
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
var box = (StrongBox<object>)frame.Data[_index];
box.Value = frame.Peek();
return +1;
}
}
public sealed class SetBoxedLocalVoidInstruction : LocalAccessInstruction {
public SetBoxedLocalVoidInstruction(int index)
: base(index) {
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 0; } }
public override int Run(InterpretedFrame frame) {
var box = (StrongBox<object>)frame.Data[_index];
box.Value = frame.Data[--frame.StackIndex];
return +1;
}
}
public sealed class SetClosureInstruction : LocalAccessInstruction {
public SetClosureInstruction(int index)
: base(index) {
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
var box = frame.Closure[_index];
box.Value = frame.Peek();
return +1;
}
}
public sealed class SetLocalVoidInstruction : LocalAccessInstruction, IBoxableInstruction {
public SetLocalVoidInstruction(int index)
: base(index) {
}
public override int ConsumedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Data[_index] = frame.Data[--frame.StackIndex];
//frame.Data[_index] = frame.Pop();
return +1;
}
public Instruction BoxIfIndexMatches(int index) {
if (index == _index) {
var result = new SetBoxedLocalVoidInstruction(index);
result.SetName(this);
return result;
} else {
return null;
}
}
}
public abstract class InitializeLocalInstruction : LocalAccessInstruction {
public InitializeLocalInstruction(int index)
: base(index) {
}
private sealed class Reference : InitializeLocalInstruction, IBoxableInstruction {
public Reference(int index)
: base(index) {
}
public override int Run(InterpretedFrame frame) {
// nop
return 1;
}
public Instruction BoxIfIndexMatches(int index) {
if (index == _index) {
var result = new ImmutableBox(index, null);
result.SetName(this);
return result;
} else {
return null;
}
}
public override string InstructionName {
get { return "InitRef"; }
}
}
private sealed class ImmutableValue : InitializeLocalInstruction, IBoxableInstruction {
private readonly object _defaultValue;
public ImmutableValue(int index, object defaultValue)
: base(index) {
_defaultValue = defaultValue;
}
public override int Run(InterpretedFrame frame) {
frame.Data[_index] = _defaultValue;
return 1;
}
public Instruction BoxIfIndexMatches(int index) {
if (index == _index) {
var result = new ImmutableBox(index, _defaultValue);
result.SetName(this);
return result;
} else {
return null;
}
}
public override string InstructionName {
get { return "InitImmutableValue"; }
}
}
private sealed class ImmutableBox : InitializeLocalInstruction {
// immutable value:
private readonly object _defaultValue;
public ImmutableBox(int index, object defaultValue)
: base(index) {
_defaultValue = defaultValue;
}
public override int Run(InterpretedFrame frame) {
frame.Data[_index] = new StrongBox<object>() { Value = _defaultValue };
return 1;
}
public override string InstructionName {
get { return "InitImmutableBox"; }
}
}
private sealed class MutableValue : InitializeLocalInstruction, IBoxableInstruction {
private readonly Type _type;
public MutableValue(int index, Type type)
: base(index) {
_type = type;
}
public override int Run(InterpretedFrame frame) {
frame.Data[_index] = Activator.CreateInstance(_type);
return 1;
}
public Instruction BoxIfIndexMatches(int index) {
if (index == _index) {
var result = new MutableBox(index, _type);
result.SetName(this);
return result;
} else {
return null;
}
}
public override string InstructionName {
get { return "InitMutableValue"; }
}
}
private sealed class MutableBox : InitializeLocalInstruction {
private readonly Type _type;
public MutableBox(int index, Type type)
: base(index) {
_type = type;
}
public override int Run(InterpretedFrame frame) {
frame.Data[_index] = new StrongBox<object>() { Value = Activator.CreateInstance(_type) };
return 1;
}
public override string InstructionName {
get { return "InitMutableBox"; }
}
}
public static Instruction Create(int index, ParameterExpression local) {
var result = CreateInstance(index, local);
result.SetName(local.Name);
return result;
}
private static LocalAccessInstruction CreateInstance(int index, ParameterExpression local) {
switch (Type.GetTypeCode(local.Type)) {
case TypeCode.Boolean: return new ImmutableValue(index, ScriptingRuntimeHelpers.False);
case TypeCode.SByte: return new ImmutableValue(index, default(SByte));
case TypeCode.Byte: return new ImmutableValue(index, default(Byte));
case TypeCode.Char: return new ImmutableValue(index, default(Char));
case TypeCode.Int16: return new ImmutableValue(index, default(Int16));
case TypeCode.Int32: return new ImmutableValue(index, ScriptingRuntimeHelpers.Int32ToObject(0));
case TypeCode.Int64: return new ImmutableValue(index, default(Int64));
case TypeCode.UInt16: return new ImmutableValue(index, default(UInt16));
case TypeCode.UInt32: return new ImmutableValue(index, default(UInt32));
case TypeCode.UInt64: return new ImmutableValue(index, default(UInt64));
case TypeCode.Single: return new ImmutableValue(index, default(Single));
case TypeCode.Double: return new ImmutableValue(index, default(Double));
case TypeCode.DBNull: return new ImmutableValue(index, default(DBNull));
case TypeCode.DateTime: return new ImmutableValue(index, default(DateTime));
case TypeCode.Decimal: return new ImmutableValue(index, default(Decimal));
case TypeCode.String:
case TypeCode.Object:
if (local.Type.IsValueType) {
return new MutableValue(index, local.Type);
} else {
return new Reference(index);
}
default:
throw Assert.Unreachable;
}
}
}
#endregion
#region Branches
public abstract class OffsetInstruction : Instruction {
internal const int Unknown = Int32.MinValue;
// the offset to jump to (relative to this instruction):
[CLSCompliant(false)]
protected int _offset = Unknown;
public virtual void Fixup(int offset, int targetStackDepth) {
Debug.Assert(_offset == Unknown && offset != Unknown);
_offset = offset;
}
public override string ToString() {
return InstructionName + (_offset == Unknown ? "(?)" : "(" + _offset + ")");
}
}
public sealed class BranchFalseInstruction : OffsetInstruction {
public BranchFalseInstruction() {
}
public override int ConsumedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
Debug.Assert(_offset != Unknown);
if (!(bool)frame.Pop()) {
return _offset;
}
return +1;
}
}
public sealed class BranchTrueInstruction : OffsetInstruction {
public BranchTrueInstruction() {
}
public override int ConsumedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
Debug.Assert(_offset != Unknown);
if ((bool)frame.Pop()) {
return _offset;
}
return +1;
}
}
public sealed class CoalescingBranchInstruction : OffsetInstruction {
public CoalescingBranchInstruction() {
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
Debug.Assert(_offset != Unknown);
if (frame.Peek() != null) {
return _offset;
}
return +1;
}
}
public class BranchInstruction : OffsetInstruction {
internal readonly bool _hasResult;
internal readonly bool _hasValue;
public BranchInstruction()
: this(false, false) {
}
public BranchInstruction(bool hasResult, bool hasValue) {
_hasResult = hasResult;
_hasValue = hasValue;
}
public override int ConsumedStack {
get { return _hasValue ? 1 : 0; }
}
public override int ProducedStack {
get { return _hasResult ? 1 : 0; }
}
public override int Run(InterpretedFrame frame) {
Debug.Assert(_offset != Unknown);
return _offset;
}
}
/// <summary>
/// This instruction implements a goto expression that can jump out of any expression.
/// It pops values (arguments) from the evaluation stack that the expression tree nodes in between
/// the goto expression and the target label node pushed and not consumed yet.
/// A goto expression can jump into a node that evaluates arguments only if it carries
/// a value and jumps right after the first argument (the carried value will be used as the first argument).
/// Goto can jump into an arbitrary child of a BlockExpression since the block doesnt accumulate values
/// on evaluation stack as its child expressions are being evaluated.
///
/// Goto needs to execute any finally blocks on the way to the target label.
/// <example>
/// {
/// f(1, 2, try { g(3, 4, try { goto L } finally { ... }, 6) } finally { ... }, 7, 8)
/// L: ...
/// }
/// </example>
/// The goto expression here jumps to label L while having 4 items on evaluation stack (1, 2, 3 and 4).
/// The jump needs to execute both finally blocks, the first one on stack level 4 the
/// second one on stack level 2. So, it needs to jump the first finally block, pop 2 items from the stack,
/// run second finally block and pop another 2 items from the stack and set instruction pointer to label L.
///
/// Goto also needs to rethrow ThreadAbortException iff it jumps out of a catch handler and
/// the current thread is in "abort requested" state.
/// </summary>
public sealed class GotoInstruction : BranchInstruction {
private struct FinallyBlock {
public int Start, End, StackDepth;
public FinallyBlock(int start, int end, int stackDepth) {
Start = start;
End = end;
StackDepth = stackDepth;
}
}
// the target label stack depth
private int _targetStackDepth;
// index of this instruction in instruction array (used only during compilation):
private int _instructionIndex;
// A list of finally blocks that need to be executed as we jump to the target label.
private List<FinallyBlock> _finallyBlocks;
public GotoInstruction(int instructionIndex, bool hasResult, bool hasValue)
: base(hasResult, hasValue) {
Debug.Assert(instructionIndex >= 0);
_instructionIndex = instructionIndex;
}
public override void Fixup(int offset, int targetStackDepth) {
base.Fixup(offset, targetStackDepth);
_targetStackDepth = targetStackDepth;
}
internal bool AddFinally(int tryStart, int finallyStackDepth, int finallyStart, int finallyEnd) {
if (!JumpsOutOfRange(tryStart, finallyStart)) {
return false;
}
if (_finallyBlocks == null) {
_finallyBlocks = new List<FinallyBlock>();
}
_finallyBlocks.Add(new FinallyBlock(finallyStart, finallyEnd, finallyStackDepth));
return true;
}
private bool JumpsOutOfRange(int start, int end) {
// we haven't visited target label for a forward jump => it is out of range
if (_offset != Unknown) {
int targetIndex = _instructionIndex + _offset;
if (targetIndex >= start && targetIndex < end) {
// target is within try body or catch handlers:
return false;
}
}
return true;
}
public override int Run(InterpretedFrame frame) {
Debug.Assert(_offset != Unknown);
Interpreter.AbortThreadIfRequested(frame, _offset);
object value = _hasValue ? frame.Pop() : null;
// run finally blocks:
if (_finallyBlocks != null) {
int oldIndex = frame.InstructionIndex;
for (int i = 0; i < _finallyBlocks.Count; i++) {
var finallyBlock = _finallyBlocks[i];
frame.SetStackDepth(finallyBlock.StackDepth);
frame.InstructionIndex = finallyBlock.Start;
// If an exception is thrown and caught in finally the we go on.
// If an exception is thrown but not handled within finally block it is propagated.
frame.Interpreter.RunBlock(frame, finallyBlock.End);
}
frame.InstructionIndex = oldIndex;
}
frame.SetStackDepth(_targetStackDepth);
if (_hasValue) {
frame.Data[frame.StackIndex - 1] = value;
}
// keep the return value on the stack
return _offset;
}
}
// no-op: we need this just to balance the stack depth.
public sealed class EnterExceptionHandlerInstruction : Instruction {
public static readonly EnterExceptionHandlerInstruction Void = new EnterExceptionHandlerInstruction(false);
public static readonly EnterExceptionHandlerInstruction NonVoid = new EnterExceptionHandlerInstruction(true);
// True if try-expression is non-void.
private readonly bool _hasValue;
private EnterExceptionHandlerInstruction(bool hasValue) {
_hasValue = hasValue;
}
// Try body and catch handlers leave a value of try-expression on the stack.
// Each handler "consumes" the value possibly pushed in try body.
public override int ConsumedStack { get { return _hasValue ? 1 : 0; } }
// A variable storing the current exception is pushed to the stack by exception handling.
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
// nop (the exception value is pushed by the interpreter in HandleCatch)
return 1;
}
}
public sealed class LeaveExceptionHandlerInstruction : BranchInstruction {
public LeaveExceptionHandlerInstruction(bool hasValue)
: base(hasValue, hasValue) {
}
public override int Run(InterpretedFrame frame) {
// CLR rethrows ThreadAbortException when leaving catch handler if abort is requested on the current thread.
Interpreter.AbortThreadIfRequested(frame, _offset);
return _offset;
}
}
public sealed class SwitchInstruction : Instruction {
//TODO this is probably much more efficient as an int[] for simple cases
private Dictionary<int, int> _cases = new Dictionary<int, int>();
private int _defaultOffset;
public SwitchInstruction() { }
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 0; } } //???
public override int Run(InterpretedFrame frame) {
var test = frame.Pop();
if (test == null) return _defaultOffset;
int offset;
if (_cases.TryGetValue((int)test, out offset)) {
return offset;
} else {
return _defaultOffset;
}
}
internal void AddCase(int test, int offset) {
// First one wins if keys are duplicated
if (!_cases.ContainsKey(test)) {
_cases.Add(test, offset);
}
}
internal void AddDefault(int offset) {
_defaultOffset = offset;
}
}
public sealed class ThrowInstruction : Instruction {
public static readonly ThrowInstruction Throw = new ThrowInstruction(true);
public static readonly ThrowInstruction VoidThrow = new ThrowInstruction(false);
private readonly bool _hasResult;
private ThrowInstruction(bool hasResult) {
_hasResult = hasResult;
}
public override int ProducedStack {
get { return _hasResult ? 1 : 0; }
}
public override int ConsumedStack {
get { return 1; }
}
public override int Run(InterpretedFrame frame) {
throw (Exception)frame.Pop();
}
}
#endregion
#region operations, i.e. calls, arithmetic, comparisons, etc.
//TODO generate the Func and Action equivalent overloads for better performance
public class CallInstruction : Instruction {
private ReflectedCaller _target;
private MethodInfo _methodInfo;
private bool _isVoid;
private int _argCount;
public CallInstruction(MethodInfo target) {
_methodInfo = target;
_isVoid = target.ReturnType == typeof(void);
_argCount = target.GetParameters().Length;
if (!target.IsStatic) _argCount += 1;
_target = ReflectedCaller.Create(target);
}
public override int ProducedStack { get { return _isVoid ? 0 : 1; } }
public override int ConsumedStack { get { return _argCount; } }
public override int Run(InterpretedFrame frame) {
object[] args = new object[_argCount];
for (int i = _argCount - 1; i >= 0; i--) {
args[i] = frame.Pop();
}
object ret = _target.Invoke(args);
if (!_isVoid) frame.Push(ret);
return +1;
}
public override string ToString() {
return "Call(" + _methodInfo + ")";
}
}
public class CreateDelegateInstruction : Instruction {
private readonly LightDelegateCreator _creator;
internal CreateDelegateInstruction(LightDelegateCreator delegateCreator) {
this._creator = delegateCreator;
}
public override int ConsumedStack { get { return _creator.ClosureVariables.Count; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
StrongBox<object>[] closure;
if (ConsumedStack > 0) {
closure = new StrongBox<object>[ConsumedStack];
for (int i = closure.Length - 1; i >= 0; i--) {
closure[i] = (StrongBox<object>)frame.Pop();
}
} else {
closure = null;
}
Delegate d = _creator.CreateDelegate(closure);
frame.Push(d);
return +1;
}
}
public class NewArrayInitInstruction : Instruction {
private Type _elementType;
private int _elementCount;
public NewArrayInitInstruction(Type elementType, int elementCount) {
this._elementType = elementType;
this._elementCount = elementCount;
}
public override int ConsumedStack { get { return _elementCount; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
var array = Array.CreateInstance(_elementType, _elementCount);
for (int i = _elementCount - 1; i >= 0; i--) {
array.SetValue(frame.Pop(), i);
}
frame.Push(array);
return +1;
}
}
public class NewArrayBoundsInstruction1 : Instruction {
private Type _elementType;
public NewArrayBoundsInstruction1(Type elementType) {
this._elementType = elementType;
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
int length = (int)frame.Pop();
var array = Array.CreateInstance(_elementType, length);
frame.Push(array);
return +1;
}
}
public class NewArrayBoundsInstructionN : Instruction {
private Type _elementType;
private int _boundsCount;
public NewArrayBoundsInstructionN(Type elementType, int boundsCount) {
this._elementType = elementType;
this._boundsCount = boundsCount;
}
public override int ConsumedStack { get { return _boundsCount; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
var bounds = new int[_boundsCount];
for (int i = _boundsCount - 1; i >= 0; i--) {
bounds[i] = (int)frame.Pop();
}
var array = Array.CreateInstance(_elementType, bounds);
frame.Push(array);
return +1;
}
}
public sealed class NewInstruction : Instruction {
private readonly ConstructorInfo _constructor;
private readonly int _argCount;
public NewInstruction(ConstructorInfo constructor) {
_constructor = constructor;
_argCount = constructor.GetParameters().Length;
}
public override int ConsumedStack { get { return _argCount; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
object[] args = new object[_argCount];
for (int i = _argCount - 1; i >= 0; i--) {
args[i] = frame.Pop();
}
object ret;
try {
ret = _constructor.Invoke(args);
} catch (TargetInvocationException e) {
ExceptionHelpers.UpdateForRethrow(e.InnerException);
throw e.InnerException;
}
frame.Push(ret);
return +1;
}
public override string ToString() {
return "New " + _constructor.DeclaringType.Name + "(" + _constructor + ")";
}
}
public class StaticFieldAccessInstruction : Instruction {
private readonly FieldInfo _field;
public StaticFieldAccessInstruction(FieldInfo field) {
Debug.Assert(field.IsStatic);
_field = field;
}
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Push(_field.GetValue(null));
return +1;
}
}
public class FieldAccessInstruction : Instruction {
private readonly FieldInfo _field;
public FieldAccessInstruction(FieldInfo field) {
Assert.NotNull(field);
_field = field;
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Push(_field.GetValue(frame.Pop()));
return +1;
}
}
public class FieldAssignInstruction : Instruction {
private readonly FieldInfo _field;
public FieldAssignInstruction(FieldInfo field) {
Assert.NotNull(field);
_field = field;
}
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 0; } }
public override int Run(InterpretedFrame frame) {
object value = frame.Pop();
object self = frame.Pop();
_field.SetValue(self, value);
return +1;
}
}
public class GetArrayItemInstruction<T> : Instruction {
public static readonly Instruction Instance = new GetArrayItemInstruction<T>();
private GetArrayItemInstruction() { }
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
int index = (int)frame.Pop();
T[] array = (T[])frame.Pop();
frame.Push(array[index]);
return +1;
}
public override string InstructionName {
get { return "GetArrayItem"; }
}
}
public class SetArrayItemInstruction<T> : Instruction {
public static readonly Instruction Instance = new SetArrayItemInstruction<T>();
private SetArrayItemInstruction() { }
public override int ConsumedStack { get { return 3; } }
public override int ProducedStack { get { return 0; } }
public override int Run(InterpretedFrame frame) {
int index = (int)frame.Pop();
T[] array = (T[])frame.Pop();
T value = (T)frame.Pop();
array[index] = value;
return +1;
}
public override string InstructionName {
get { return "SetArrayItem"; }
}
}
public abstract class NumericConvertInstruction : Instruction {
internal readonly TypeCode _from, _to;
public NumericConvertInstruction(TypeCode from, TypeCode to) {
_from = from;
_to = to;
}
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override string ToString() {
return InstructionName + "(" + _from + "->" + _to + ")";
}
public sealed class Unchecked : NumericConvertInstruction {
public override string InstructionName { get { return "UncheckedConvert"; } }
public Unchecked(TypeCode from, TypeCode to)
: base(from, to) {
}
public override int Run(InterpretedFrame frame) {
frame.Push(Convert(frame.Pop()));
return +1;
}
private object Convert(object obj) {
switch (_from) {
case TypeCode.Byte: return ConvertInt32((Byte)obj);
case TypeCode.SByte: return ConvertInt32((SByte)obj);
case TypeCode.Int16: return ConvertInt32((Int16)obj);
case TypeCode.Char: return ConvertInt32((Char)obj);
case TypeCode.Int32: return ConvertInt32((Int32)obj);
case TypeCode.Int64: return ConvertInt64((Int64)obj);
case TypeCode.UInt16: return ConvertInt32((UInt16)obj);
case TypeCode.UInt32: return ConvertInt64((UInt32)obj);
case TypeCode.UInt64: return ConvertUInt64((UInt64)obj);
case TypeCode.Single: return ConvertDouble((Single)obj);
case TypeCode.Double: return ConvertDouble((Double)obj);
default: throw Assert.Unreachable;
}
}
private object ConvertInt32(int obj) {
unchecked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
private object ConvertInt64(Int64 obj) {
unchecked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
private object ConvertUInt64(UInt64 obj) {
unchecked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
private object ConvertDouble(Double obj) {
unchecked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
}
public sealed class Checked : NumericConvertInstruction {
public override string InstructionName { get { return "CheckedConvert"; } }
public Checked(TypeCode from, TypeCode to)
: base(from, to) {
}
public override int Run(InterpretedFrame frame) {
frame.Push(Convert(frame.Pop()));
return +1;
}
private object Convert(object obj) {
switch (_from) {
case TypeCode.Byte: return ConvertInt32((Byte)obj);
case TypeCode.SByte: return ConvertInt32((SByte)obj);
case TypeCode.Int16: return ConvertInt32((Int16)obj);
case TypeCode.Char: return ConvertInt32((Char)obj);
case TypeCode.Int32: return ConvertInt32((Int32)obj);
case TypeCode.Int64: return ConvertInt64((Int64)obj);
case TypeCode.UInt16: return ConvertInt32((UInt16)obj);
case TypeCode.UInt32: return ConvertInt64((UInt32)obj);
case TypeCode.UInt64: return ConvertUInt64((UInt64)obj);
case TypeCode.Single: return ConvertDouble((Single)obj);
case TypeCode.Double: return ConvertDouble((Double)obj);
default: throw Assert.Unreachable;
}
}
private object ConvertInt32(int obj) {
checked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
private object ConvertInt64(Int64 obj) {
checked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
private object ConvertUInt64(UInt64 obj) {
checked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
private object ConvertDouble(Double obj) {
checked {
switch (_to) {
case TypeCode.Byte: return (Byte)obj;
case TypeCode.SByte: return (SByte)obj;
case TypeCode.Int16: return (Int16)obj;
case TypeCode.Char: return (Char)obj;
case TypeCode.Int32: return (Int32)obj;
case TypeCode.Int64: return (Int64)obj;
case TypeCode.UInt16: return (UInt16)obj;
case TypeCode.UInt32: return (UInt32)obj;
case TypeCode.UInt64: return (UInt64)obj;
case TypeCode.Single: return (Single)obj;
case TypeCode.Double: return (Double)obj;
default: throw Assert.Unreachable;
}
}
}
}
}
public class NotInstruction : Instruction {
public static readonly Instruction Instance = new NotInstruction();
private NotInstruction() { }
public override int ConsumedStack { get { return 1; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Push(!(bool)frame.Pop());
return +1;
}
}
public class AddIntInstruction : Instruction {
public static readonly Instruction Instance = new AddIntInstruction();
private AddIntInstruction() { }
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 1; } }
public override int Run(InterpretedFrame frame) {
frame.Push((int)frame.Pop() + (int)frame.Pop());
return +1;
}
}
public abstract class EqualInstruction : Instruction {
// Perf: EqualityComparer<T> but is 3/2 to 2 times slower.
private static Instruction _Reference, _Boolean, _SByte, _Int16, _Char, _Int32, _Int64, _Byte, _UInt16, _UInt32, _UInt64, _Single, _Double;
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 1; } }
private EqualInstruction() {
}
internal sealed class EqualBoolean : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Boolean)frame.Pop()) == ((Boolean)frame.Pop()));
return +1;
}
}
internal sealed class EqualSByte : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((SByte)frame.Pop()) == ((SByte)frame.Pop()));
return +1;
}
}
internal sealed class EqualInt16 : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Int16)frame.Pop()) == ((Int16)frame.Pop()));
return +1;
}
}
internal sealed class EqualChar : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Char)frame.Pop()) == ((Char)frame.Pop()));
return +1;
}
}
internal sealed class EqualInt32 : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Int32)frame.Pop()) == ((Int32)frame.Pop()));
return +1;
}
}
internal sealed class EqualInt64 : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Int64)frame.Pop()) == ((Int64)frame.Pop()));
return +1;
}
}
internal sealed class EqualByte : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Byte)frame.Pop()) == ((Byte)frame.Pop()));
return +1;
}
}
internal sealed class EqualUInt16 : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((UInt16)frame.Pop()) == ((UInt16)frame.Pop()));
return +1;
}
}
internal sealed class EqualUInt32 : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((UInt32)frame.Pop()) == ((UInt32)frame.Pop()));
return +1;
}
}
internal sealed class EqualUInt64 : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((UInt64)frame.Pop()) == ((UInt64)frame.Pop()));
return +1;
}
}
internal sealed class EqualSingle : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Single)frame.Pop()) == ((Single)frame.Pop()));
return +1;
}
}
internal sealed class EqualDouble : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Double)frame.Pop()) == ((Double)frame.Pop()));
return +1;
}
}
internal sealed class EqualReference : EqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(frame.Pop() == frame.Pop());
return +1;
}
}
public static Instruction Create(Type type) {
// Boxed enums can be unboxed as their underlying types:
switch (Type.GetTypeCode(type.IsEnum ? Enum.GetUnderlyingType(type) : type)) {
case TypeCode.Boolean: return _Boolean ?? (_Boolean = new EqualBoolean());
case TypeCode.SByte: return _SByte ?? (_SByte = new EqualSByte());
case TypeCode.Byte: return _Byte ?? (_Byte = new EqualByte());
case TypeCode.Char: return _Char ?? (_Char = new EqualChar());
case TypeCode.Int16: return _Int16 ?? (_Int16 = new EqualInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new EqualInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new EqualInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new EqualInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new EqualInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new EqualInt64());
case TypeCode.Single: return _Single ?? (_Single = new EqualSingle());
case TypeCode.Double: return _Double ?? (_Double = new EqualDouble());
case TypeCode.Object:
if (!type.IsValueType) {
return _Reference ?? (_Reference = new EqualReference());
}
// TODO: Nullable<T>
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}
public override string ToString() {
return "Equal()";
}
}
public abstract class NotEqualInstruction : Instruction {
// Perf: EqualityComparer<T> but is 3/2 to 2 times slower.
private static Instruction _Reference, _Boolean, _SByte, _Int16, _Char, _Int32, _Int64, _Byte, _UInt16, _UInt32, _UInt64, _Single, _Double;
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 1; } }
private NotEqualInstruction() {
}
internal sealed class NotEqualBoolean : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Boolean)frame.Pop()) != ((Boolean)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualSByte : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((SByte)frame.Pop()) != ((SByte)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualInt16 : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Int16)frame.Pop()) != ((Int16)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualChar : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Char)frame.Pop()) != ((Char)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualInt32 : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Int32)frame.Pop()) != ((Int32)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualInt64 : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Int64)frame.Pop()) != ((Int64)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualByte : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Byte)frame.Pop()) != ((Byte)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualUInt16 : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((UInt16)frame.Pop()) != ((UInt16)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualUInt32 : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((UInt32)frame.Pop()) != ((UInt32)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualUInt64 : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((UInt64)frame.Pop()) != ((UInt64)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualSingle : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Single)frame.Pop()) != ((Single)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualDouble : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(((Double)frame.Pop()) != ((Double)frame.Pop()));
return +1;
}
}
internal sealed class NotEqualReference : NotEqualInstruction {
public override int Run(InterpretedFrame frame) {
frame.Push(frame.Pop() != frame.Pop());
return +1;
}
}
public static Instruction Instance(Type type) {
// Boxed enums can be unboxed as their underlying types:
switch (Type.GetTypeCode(type.IsEnum ? Enum.GetUnderlyingType(type) : type)) {
case TypeCode.Boolean: return _Boolean ?? (_Boolean = new NotEqualBoolean());
case TypeCode.SByte: return _SByte ?? (_SByte = new NotEqualSByte());
case TypeCode.Byte: return _Byte ?? (_Byte = new NotEqualByte());
case TypeCode.Char: return _Char ?? (_Char = new NotEqualChar());
case TypeCode.Int16: return _Int16 ?? (_Int16 = new NotEqualInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new NotEqualInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new NotEqualInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new NotEqualInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new NotEqualInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new NotEqualInt64());
case TypeCode.Single: return _Single ?? (_Single = new NotEqualSingle());
case TypeCode.Double: return _Double ?? (_Double = new NotEqualDouble());
case TypeCode.Object:
if (!type.IsValueType) {
return _Reference ?? (_Reference = new NotEqualReference());
}
// TODO: Nullable<T>
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}
public override string ToString() {
return "NotEqual()";
}
}
public abstract class LessThanInstruction : Instruction {
private static Instruction _SByte, _Int16, _Char, _Int32, _Int64, _Byte, _UInt16, _UInt32, _UInt64, _Single, _Double;
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 1; } }
private LessThanInstruction() {
}
internal sealed class LessThanSByte : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
SByte right = (SByte)frame.Pop();
frame.Push(((SByte)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanInt16 : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
Int16 right = (Int16)frame.Pop();
frame.Push(((Int16)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanChar : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
Char right = (Char)frame.Pop();
frame.Push(((Char)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanInt32 : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
Int32 right = (Int32)frame.Pop();
frame.Push(((Int32)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanInt64 : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
Int64 right = (Int64)frame.Pop();
frame.Push(((Int64)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanByte : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
Byte right = (Byte)frame.Pop();
frame.Push(((Byte)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanUInt16 : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
UInt16 right = (UInt16)frame.Pop();
frame.Push(((UInt16)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanUInt32 : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
UInt32 right = (UInt32)frame.Pop();
frame.Push(((UInt32)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanUInt64 : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
UInt64 right = (UInt64)frame.Pop();
frame.Push(((UInt64)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanSingle : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
Single right = (Single)frame.Pop();
frame.Push(((Single)frame.Pop()) < right);
return +1;
}
}
internal sealed class LessThanDouble : LessThanInstruction {
public override int Run(InterpretedFrame frame) {
Double right = (Double)frame.Pop();
frame.Push(((Double)frame.Pop()) < right);
return +1;
}
}
public static Instruction Instance(Type type) {
Debug.Assert(!type.IsEnum);
switch (Type.GetTypeCode(type)) {
case TypeCode.SByte: return _SByte ?? (_SByte = new LessThanSByte());
case TypeCode.Byte: return _Byte ?? (_Byte = new LessThanByte());
case TypeCode.Char: return _Char ?? (_Char = new LessThanChar());
case TypeCode.Int16: return _Int16 ?? (_Int16 = new LessThanInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new LessThanInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new LessThanInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new LessThanUInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new LessThanUInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new LessThanUInt64());
case TypeCode.Single: return _Single ?? (_Single = new LessThanSingle());
case TypeCode.Double: return _Double ?? (_Double = new LessThanDouble());
default:
throw Assert.Unreachable;
}
}
public override string ToString() {
return "LessThan()";
}
}
public abstract class GreaterThanInstruction : Instruction {
private static Instruction _SByte, _Int16, _Char, _Int32, _Int64, _Byte, _UInt16, _UInt32, _UInt64, _Single, _Double;
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 1; } }
private GreaterThanInstruction() {
}
internal sealed class GreaterThanSByte : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
SByte right = (SByte)frame.Pop();
frame.Push(((SByte)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanInt16 : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
Int16 right = (Int16)frame.Pop();
frame.Push(((Int16)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanChar : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
Char right = (Char)frame.Pop();
frame.Push(((Char)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanInt32 : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
Int32 right = (Int32)frame.Pop();
frame.Push(((Int32)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanInt64 : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
Int64 right = (Int64)frame.Pop();
frame.Push(((Int64)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanByte : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
Byte right = (Byte)frame.Pop();
frame.Push(((Byte)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanUInt16 : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
UInt16 right = (UInt16)frame.Pop();
frame.Push(((UInt16)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanUInt32 : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
UInt32 right = (UInt32)frame.Pop();
frame.Push(((UInt32)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanUInt64 : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
UInt64 right = (UInt64)frame.Pop();
frame.Push(((UInt64)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanSingle : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
Single right = (Single)frame.Pop();
frame.Push(((Single)frame.Pop()) > right);
return +1;
}
}
internal sealed class GreaterThanDouble : GreaterThanInstruction {
public override int Run(InterpretedFrame frame) {
Double right = (Double)frame.Pop();
frame.Push(((Double)frame.Pop()) > right);
return +1;
}
}
public static Instruction Instance(Type type) {
Debug.Assert(!type.IsEnum);
switch (Type.GetTypeCode(type)) {
case TypeCode.SByte: return _SByte ?? (_SByte = new GreaterThanSByte());
case TypeCode.Byte: return _Byte ?? (_Byte = new GreaterThanByte());
case TypeCode.Char: return _Char ?? (_Char = new GreaterThanChar());
case TypeCode.Int16: return _Int16 ?? (_Int16 = new GreaterThanInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new GreaterThanInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new GreaterThanInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new GreaterThanUInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new GreaterThanUInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new GreaterThanUInt64());
case TypeCode.Single: return _Single ?? (_Single = new GreaterThanSingle());
case TypeCode.Double: return _Double ?? (_Double = new GreaterThanDouble());
default:
throw Assert.Unreachable;
}
}
public override string ToString() {
return "GreaterThan()";
}
}
public sealed class TypeEqualsInstruction : Instruction {
public static readonly TypeEqualsInstruction Instance = new TypeEqualsInstruction();
public override int ConsumedStack { get { return 2; } }
public override int ProducedStack { get { return 1; } }
private TypeEqualsInstruction() {
}
public override int Run(InterpretedFrame frame) {
Type type = (Type)frame.Pop();
object obj = frame.Pop();
frame.Push(ScriptingRuntimeHelpers.BooleanToObject(obj != null && obj.GetType() == type));
return +1;
}
public override string InstructionName {
get { return "TypeEquals()"; }
}
}
#endregion
public class RuntimeVariablesInstruction : Instruction {
private readonly int _count;
public RuntimeVariablesInstruction(int count) {
_count = count;
}
public override int ProducedStack { get { return 1; } }
public override int ConsumedStack { get { return _count; } }
public override int Run(InterpretedFrame frame) {
var ret = new IStrongBox[_count];
for (int i = ret.Length - 1; i >= 0; i--) {
ret[i] = (IStrongBox)frame.Pop();
}
frame.Push(RuntimeVariables.Create(ret));
return +1;
}
public override string ToString() {
return "GetRuntimeVariables()";
}
}
}
|