interpreter.h (36236B)
1 // 2 // interpreter.h 3 // Smalltalk-80 4 // 5 // Created by Dan Banay on 3/31/20. 6 // Copyright © 2020 Dan Banay. All rights reserved. 7 // 8 // MIT License 9 // Permission is hereby granted, free of charge, to any person obtaining a copy 10 // of this software and associated documentation files (the "Software"), to deal 11 // in the Software without restriction, including without limitation the rights 12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 // copies of the Software, and to permit persons to whom the Software is 14 // furnished to do so, subject to the following conditions: 15 // 16 // The above copyright notice and this permission notice shall be included in all 17 // copies or substantial portions of the Software. 18 // 19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 // SOFTWARE. 26 // 27 28 #pragma once 29 #include <string> 30 #include "objmemory.h" 31 #include "filesystem.h" 32 #include "hal.h" 33 34 // Add some helpful methods if defined 35 //#define DEBUGGING_SUPPORT 36 37 // implement optional primitiveNext 38 #define IMPLEMENT_PRIMITIVE_NEXT 39 40 // implement optional primitiveAtEnd 41 #define IMPLEMENT_PRIMITIVE_AT_END 42 43 // implement optional primitiveNextPut 44 #define IMPLEMENT_PRIMITIVE_NEXT_PUT 45 46 // implement optional primitiveScanCharacters 47 #define IMPLEMENT_PRIMITIVE_SCANCHARS 48 49 class Interpreter 50 #ifdef GC_MARK_SWEEP 51 : IGCNotification 52 #endif 53 { 54 public: 55 56 Interpreter(IHardwareAbstractionLayer *halInterface, IFileSystem *fileSystemInterface); 57 58 bool init(); 59 60 61 // cycle 62 void cycle(); 63 64 inline void checkLowMemoryConditions() 65 { 66 checkLowMemory = true; 67 } 68 69 // asynchronousSignal: 70 void asynchronousSignal(int aSemaphore); 71 72 73 // Debug/testing 74 int lastBytecode() { return currentBytecode; } 75 76 77 int getDisplayBits(int width, int height); 78 79 // Allow read-only access to display form data 80 inline int fetchWord_ofDislayBits(int wordIndex, int displayBits) 81 { 82 return memory.fetchWord_ofObject(wordIndex, displayBits); 83 } 84 85 86 87 private: 88 89 void error(const char *message); 90 91 // --- ArrayStrmPrims --- 92 93 // primitiveAtEnd 94 void primitiveAtEnd(); 95 96 // checkIndexableBoundsOf:in: 97 void checkIndexableBoundsOf_in(int index, int array); 98 99 // primitiveNextPut 100 void primitiveNextPut(); 101 102 // lengthOf: 103 int lengthOf(int array); 104 105 // primitiveNext 106 void primitiveNext(); 107 108 // dispatchSubscriptAndStreamPrimitives 109 void dispatchSubscriptAndStreamPrimitives(); 110 111 // primitiveStringAt 112 void primitiveStringAt(); 113 114 // primitiveAt 115 void primitiveAt(); 116 117 // primitiveSize 118 void primitiveSize(); 119 120 // primitiveStringAtPut 121 void primitiveStringAtPut(); 122 123 // subscript:with: 124 int subscript_with(int array, int index); 125 126 // primitiveAtPut 127 void primitiveAtPut(); 128 129 // subscript:with:storing: 130 void subscript_with_storing(int array, int index, int value); 131 132 133 // --- Contexts --- 134 135 // storeContextRegisters 136 void storeContextRegisters(); 137 138 // unPop: 139 inline void unPop(int number) 140 { 141 /* "source" 142 stackPointer <- stackPointer + number 143 */ 144 145 stackPointer = stackPointer + number; 146 } 147 148 // isBlockContext: 149 bool isBlockContext(int contextPointer); 150 151 // push: 152 inline void push(int object) 153 { 154 /* "source" 155 stackPointer <- stackPointer + 1. 156 memory storePointer: stackPointer 157 ofObject: activeContext 158 withValue: object 159 */ 160 161 stackPointer = stackPointer + 1; 162 memory.storePointer_ofObject_withValue(stackPointer, activeContext, object); 163 164 } 165 166 // instructionPointerOfContext: 167 inline int instructionPointerOfContext(int contextPointer) 168 { 169 /* "source" 170 ^self fetchInteger: InstructionPointerIndex 171 ofObject: contextPointer 172 */ 173 174 return fetchInteger_ofObject(InstructionPointerIndex, contextPointer); 175 } 176 177 // newActiveContext: 178 void newActiveContext(int aContext); 179 180 // argumentCountOfBlock: 181 inline int argumentCountOfBlock(int blockPointer) 182 { 183 /* "source" 184 ^self fetchInteger: BlockArgumentCountIndex 185 ofObject: blockPointer 186 */ 187 188 return fetchInteger_ofObject(BlockArgumentCountIndex, blockPointer); 189 } 190 191 // literal: 192 inline int literal(int offset) 193 { 194 /* "source" 195 ^self literal: offset 196 ofMethod: method 197 */ 198 199 return literal_ofMethod(offset, method); 200 } 201 202 // sender 203 inline int sender() 204 { 205 /* "source" 206 ^memory fetchPointer: SenderIndex 207 ofObject: homeContext 208 */ 209 210 return memory.fetchPointer_ofObject(SenderIndex, homeContext); 211 } 212 213 // temporary: 214 inline int temporary(int offset) 215 { 216 /* "source" 217 ^memory fetchPointer: offset + TempFrameStart 218 ofObject: homeContext 219 */ 220 221 return memory.fetchPointer_ofObject(offset + TempFrameStart, homeContext); 222 } 223 224 // caller 225 inline int caller() 226 { 227 /* "source" 228 ^memory fetchPointer: SenderIndex 229 ofObject: activeContext 230 */ 231 return memory.fetchPointer_ofObject(SenderIndex, activeContext); 232 } 233 234 // pop: 235 inline void pop(int number) 236 { 237 /* "source" 238 stackPointer <- stackPointer - number 239 */ 240 241 stackPointer = stackPointer - number; 242 } 243 244 // storeStackPointerValue:inContext: 245 inline void storeStackPointerValue_inContext(int value, int contextPointer) 246 { 247 /* "source" 248 self storeInteger: StackPointerIndex 249 ofObject: contextPointer 250 withValue: value 251 */ 252 253 storeInteger_ofObject_withValue(StackPointerIndex, contextPointer, value); 254 } 255 256 // stackValue: 257 inline int stackValue(int offset) 258 { 259 /* "source" 260 ^memory fetchPointer: stackPointer - offset 261 ofObject: activeContext 262 */ 263 return memory.fetchPointer_ofObject(stackPointer - offset, activeContext); 264 } 265 266 // stackTop 267 inline int stackTop() 268 { 269 /* "source" 270 ^memory fetchPointer: stackPointer 271 ofObject: activeContext 272 */ 273 274 return memory.fetchPointer_ofObject(stackPointer, activeContext); 275 } 276 277 // popStack 278 inline int popStack() 279 { 280 int stackTop; 281 282 /* "source" 283 stackTop <- memory fetchPointer: stackPointer 284 ofObject: activeContext. 285 stackPointer <- stackPointer - 1. 286 ^stackTop 287 */ 288 289 stackTop = memory.fetchPointer_ofObject(stackPointer, activeContext); 290 stackPointer = stackPointer - 1; 291 return stackTop; 292 } 293 294 // fetchContextRegisters 295 void fetchContextRegisters(); 296 297 // storeInstructionPointerValue:inContext: 298 inline void storeInstructionPointerValue_inContext(int value, int contextPointer) 299 { 300 /* "source" 301 self storeInteger: InstructionPointerIndex 302 ofObject: contextPointer 303 withValue: value 304 */ 305 306 storeInteger_ofObject_withValue(InstructionPointerIndex, contextPointer, value); 307 } 308 309 // stackPointerOfContext: 310 inline int stackPointerOfContext(int contextPointer) 311 { 312 /* "source" 313 ^self fetchInteger: StackPointerIndex 314 ofObject: contextPointer 315 */ 316 317 return fetchInteger_ofObject(StackPointerIndex, contextPointer); 318 } 319 320 321 // --- IOPrims --- 322 323 // dispatchInputOutputPrimitives 324 void dispatchInputOutputPrimitives(); 325 326 void primitiveMousePoint(); 327 void primitiveCursorLocPut(); 328 void primitiveCursorLink(); 329 void primitiveInputSemaphore(); 330 void primitiveSampleInterval(); 331 void primitiveInputWord(); 332 void updateDisplay(int destForm, int updatedHeight, int updatedWidth, int updatedX, int updatedY); 333 334 void primitiveCopyBits(); 335 void primitiveSnapshot(); 336 void primitiveTimeWordsInto(); 337 void primitiveTickWordsInto(); 338 void primitiveSignalAtTick(); 339 void primitiveBeCursor(); 340 void primitiveBeDisplay(); 341 void primitiveScanCharacters(); 342 void primitiveDrawLoop(); 343 void primitiveStringReplace(); 344 345 346 // --- Classes --- 347 348 // lookupMethodInDictionary: 349 bool lookupMethodInDictionary(int dictionary); 350 351 // isPointers: 352 inline bool isPointers(int classPointer) 353 { 354 int pointersFlag; 355 356 /* "source" 357 pointersFlag <- self extractBits: 0 to: 0 358 of: (self instanceSpecificationOf: classPointer). 359 ^pointersFlag = 1 360 */ 361 pointersFlag = extractBits_to_of(0, 0, instanceSpecificationOf(classPointer)); 362 return pointersFlag == 1; 363 } 364 365 // superclassOf: 366 inline int superclassOf(int classPointer) 367 { 368 /* "source" 369 ^memory fetchPointer: SuperclassIndex 370 ofObject: classPointer 371 */ 372 return memory.fetchPointer_ofObject(SuperclassIndex, classPointer); 373 } 374 375 // fixedFieldsOf: 376 inline int fixedFieldsOf(int classPointer) 377 { 378 /* "source" 379 ^self extractBits: 4 to: 14 380 of: (self instanceSpecificationOf: classPointer) 381 */ 382 383 return extractBits_to_of(4, 14, instanceSpecificationOf(classPointer)); 384 } 385 386 // isWords: 387 inline bool isWords(int classPointer) 388 { 389 int wordsFlag; 390 391 /* "source" 392 wordsFlag <- self extractBits: 1 to: 1 393 of: (self instanceSpecificationOf: classPointer). 394 ^wordsFlag = 1 395 */ 396 397 wordsFlag = extractBits_to_of(1, 1, instanceSpecificationOf(classPointer)); 398 return wordsFlag == 1; 399 } 400 401 // hash: 402 inline int hash(int objectPointer) 403 { 404 /* "source" 405 ^objectPointer bitShift: -1 406 */ 407 408 return objectPointer >> 1; 409 410 } 411 412 // isIndexable: 413 inline bool isIndexable(int classPointer) 414 { 415 int indexableFlag; 416 417 /* "source" 418 indexableFlag <- self extractBits: 2 to: 2 419 of: (self instanceSpecificationOf: classPointer). 420 ^indexableFlag = 1 421 */ 422 423 indexableFlag = extractBits_to_of(2, 2, instanceSpecificationOf(classPointer)); 424 return indexableFlag == 1; 425 } 426 427 // instanceSpecificationOf: 428 inline int instanceSpecificationOf(int classPointer) 429 { 430 /* "source" 431 ^memory fetchPointer: InstanceSpecificationIndex 432 ofObject: classPointer 433 */ 434 435 return memory.fetchPointer_ofObject(InstanceSpecificationIndex, classPointer); 436 } 437 438 // createActualMessage 439 void createActualMessage(); 440 441 // lookupMethodInClass: 442 bool lookupMethodInClass(int cls); 443 444 445 // --- ReturnBytecode --- 446 447 // returnToActiveContext: 448 void returnToActiveContext(int aContext); 449 450 // returnBytecode 451 void returnBytecode(); 452 453 // nilContextFields 454 void nilContextFields(); 455 456 // returnValue:to: 457 void returnValue_to(int resultPointer, int contextPointer); 458 459 460 // --- ControlPrims --- 461 462 // synchronousSignal: 463 void synchronousSignal(int aSemaphore); 464 465 // primitiveBlockCopy 466 void primitiveBlockCopy(); 467 468 // primitiveResume 469 void primitiveResume(); 470 471 // primitivePerformWithArgs 472 void primitivePerformWithArgs(); 473 474 // wakeHighestPriority 475 int wakeHighestPriority(); 476 477 // primitivePerform 478 void primitivePerform(); 479 480 // primitiveValueWithArgs 481 void primitiveValueWithArgs(); 482 483 // removeFirstLinkOfList: 484 int removeFirstLinkOfList(int aLinkedList); 485 486 // primitiveWait 487 void primitiveWait(); 488 489 // primitiveFlushCache 490 void primitiveFlushCache(); 491 492 // suspendActive 493 void suspendActive(); 494 495 // activeProcess 496 int activeProcess(); 497 498 // schedulerPointer 499 inline int schedulerPointer() 500 { 501 /* "source" 502 ^memory fetchPointer: ValueIndex 503 ofObject: SchedulerAssociationPointer 504 */ 505 return memory.fetchPointer_ofObject(ValueIndex, SchedulerAssociationPointer); 506 } 507 508 // addLastLink:toList: 509 void addLastLink_toList(int aLink, int aLinkedList); 510 511 // dispatchControlPrimitives 512 void dispatchControlPrimitives(); 513 514 // checkProcessSwitch 515 void checkProcessSwitch(); 516 517 // primitiveSignal 518 void primitiveSignal(); 519 520 // isEmptyList: 521 int isEmptyList(int aLinkedList); 522 523 // primitiveSuspend 524 void primitiveSuspend(); 525 526 // primitiveValue 527 void primitiveValue(); 528 529 // firstContext 530 int firstContext(); 531 532 533 // transferTo: 534 void transferTo(int aProcess); 535 536 // resume: 537 void resume(int aProcess); 538 539 // sleep: 540 void sleep(int aProcess); 541 542 543 // --- SystemPrims --- 544 545 // primitiveClass 546 void primitiveClass(); 547 548 // dispatchSystemPrimitives 549 void dispatchSystemPrimitives(); 550 551 // primitiveEquivalent 552 void primitiveEquivalent(); 553 554 void primitiveCoreLeft(); 555 void primitiveQuit(); 556 void primitiveExitToDebugger(); 557 void primitiveOopsLeft(); 558 void primitiveSignalAtOopsLeftWordsLeft(); 559 560 561 // Vendor specific 562 void dispatchPrivatePrimitives(); 563 564 // Posix filesystem primitives -- dbanay 565 void primitiveBeSnapshotFile(); 566 void primitivePosixFileOperation(); 567 void primitivePosixDirectoryOperation(); 568 void primitivePosixLastErrorOperation(); 569 void primitivePosixErrorStringOperation(); 570 571 572 // --- PrimitiveTest --- 573 574 // success: 575 inline void success(bool successValue) 576 { 577 /* "source" 578 success <- successValue & success 579 */ 580 successFlag = successFlag && successValue; 581 } 582 583 // dispatchPrimitives 584 void dispatchPrimitives(); 585 586 // positive16BitValueOf: 587 int positive16BitValueOf(int integerPointer); 588 std::uint32_t positive32BitValueOf(int integerPointer); 589 590 // initPrimitive 591 inline void initPrimitive() 592 { 593 /* "source" 594 success <- true 595 */ 596 597 successFlag = true; 598 } 599 600 // success 601 inline bool success() 602 { 603 /* "source" 604 ^success 605 */ 606 607 return successFlag; 608 } 609 610 // primitiveResponse 611 bool primitiveResponse(); 612 613 // quickInstanceLoad 614 void quickInstanceLoad(); 615 616 // arithmeticSelectorPrimitive 617 void arithmeticSelectorPrimitive(); 618 619 // primitiveFail 620 int primitiveFail() 621 { 622 /* "source" 623 success <- false 624 */ 625 626 successFlag = false; 627 return 0; // invalid oop 628 } 629 630 // pushInteger: 631 inline void pushInteger(int integerValue) 632 { 633 /* "source" 634 self push: (memory integerObjectOf: integerValue) 635 */ 636 push(memory.integerObjectOf(integerValue)); 637 } 638 639 // quickReturnSelf 640 inline void quickReturnSelf() 641 { 642 /* "source" 643 */ 644 645 // self is on stack top 646 } 647 648 // positive16BitIntegerFor: 649 int positive16BitIntegerFor(int integerValue); 650 int positive32BitIntegerFor(int integerValue); 651 652 // popInteger 653 int popInteger(); 654 655 // specialSelectorPrimitiveResponse 656 int specialSelectorPrimitiveResponse(); 657 658 // commonSelectorPrimitive 659 void commonSelectorPrimitive(); 660 661 662 // --- Initialization --- 663 664 665 // initializeMethodCache 666 void initializeMethodCache(); 667 668 669 // --- ArithmeticPrim --- 670 671 // primitiveMod 672 void primitiveMod(); 673 674 // dispatchArithmeticPrimitives 675 void dispatchArithmeticPrimitives(); 676 677 // primitiveEqual 678 void primitiveEqual(); 679 680 // primitiveBitOr 681 void primitiveBitOr(); 682 683 // primitiveDivide 684 void primitiveDivide(); 685 686 // primitiveMultiply 687 void primitiveMultiply(); 688 689 // dispatchLargeIntegerPrimitives 690 inline void dispatchLargeIntegerPrimitives() 691 { 692 /* "source" 693 self primitiveFail 694 */ 695 696 primitiveFail(); 697 } 698 699 // primitiveBitAnd 700 void primitiveBitAnd(); 701 702 // primitiveSubtract 703 void primitiveSubtract(); 704 705 // dispatchIntegerPrimitives 706 void dispatchIntegerPrimitives(); 707 708 // primitiveGreaterOrEqual 709 void primitiveGreaterOrEqual(); 710 711 // primitiveAdd 712 void primitiveAdd(); 713 714 // primitiveNotEqual 715 void primitiveNotEqual(); 716 717 // primitiveQuo 718 void primitiveQuo(); 719 720 // dispatchFloatPrimitives 721 void dispatchFloatPrimitives(); 722 723 void primitiveAsFloat(); 724 void primitiveFloatAdd(); 725 void primitiveFloatSubtract(); 726 void primitiveFloatLessThan(); 727 void primitiveFloatGreaterThan(); 728 void primitiveFloatLessOrEqual(); 729 void primitiveFloatGreaterOrEqual(); 730 void primitiveFloatEqual(); 731 void primitiveFloatNotEqual(); 732 void primitiveFloatMultiply(); 733 void primitiveFloatDivide(); 734 void primitiveTruncated(); 735 void primitiveFractionalPart(); 736 inline void primitiveExponent() 737 { 738 primitiveFail(); // optional 739 } 740 inline void primitiveTimesTwoPower() 741 { 742 primitiveFail(); // optional 743 } 744 745 // primitiveLessOrEqual 746 void primitiveLessOrEqual(); 747 748 // primitiveMakePoint 749 void primitiveMakePoint(); 750 751 // primitiveBitXor 752 void primitiveBitXor(); 753 754 // primitiveLessThan 755 void primitiveLessThan(); 756 757 // primitiveBitShift 758 void primitiveBitShift(); 759 760 // primitiveGreaterThan 761 void primitiveGreaterThan(); 762 763 // primitiveDiv 764 void primitiveDiv(); 765 766 767 // --- SendBytecodes --- 768 769 // sendSelector:argumentCount: 770 void sendSelector_argumentCount(int selector, int count); 771 772 // findNewMethodInClass: 773 void findNewMethodInClass(int cls); 774 775 // activateNewMethod 776 void activateNewMethod(); 777 778 // sendSpecialSelectorBytecode 779 void sendSpecialSelectorBytecode(); 780 781 // doubleExtendedSuperBytecode 782 void doubleExtendedSuperBytecode(); 783 784 // sendBytecode 785 void sendBytecode(); 786 787 // doubleExtendedSendBytecode 788 void doubleExtendedSendBytecode(); 789 790 // sendSelectorToClass: 791 void sendSelectorToClass(int classPointer); 792 793 // sendLiteralSelectorBytecode 794 void sendLiteralSelectorBytecode(); 795 796 // singleExtendedSuperBytecode 797 void singleExtendedSuperBytecode(); 798 799 // singleExtendedSendBytecode 800 void singleExtendedSendBytecode(); 801 802 // extendedSendBytecode 803 void extendedSendBytecode(); 804 805 // executeNewMethod 806 void executeNewMethod(); 807 808 809 // --- MainLoop --- 810 811 // dispatchOnThisBytecode 812 void dispatchOnThisBytecode(); 813 814 // fetchByte 815 int fetchByte(); 816 817 // interpret 818 void interpret(); 819 820 private: 821 // --- CompiledMethod --- 822 823 // headerOf: 824 inline int headerOf(int methodPointer) 825 { 826 /* "source" 827 ^memory fetchPointer: HeaderIndex 828 ofObject: methodPointer 829 */ 830 831 return memory.fetchPointer_ofObject(HeaderIndex, methodPointer); 832 } 833 834 // literalCountOf: 835 inline int literalCountOf(int methodPointer) 836 { 837 /* "source" 838 ^self literalCountOfHeader: (self headerOf: methodPointer) 839 */ 840 841 return literalCountOfHeader(headerOf(methodPointer)); 842 } 843 844 // primitiveIndexOf: 845 int primitiveIndexOf(int methodPointer); 846 847 // argumentCountOf: 848 int argumentCountOf(int methodPointer); 849 850 // literalCountOfHeader: 851 inline int literalCountOfHeader(int headerPointer) 852 { 853 /* "source" 854 ^self extractBits: 9 to: 14 855 of: headerPointer 856 */ 857 858 return extractBits_to_of(9, 14, headerPointer); 859 } 860 861 // fieldIndexOf: 862 inline int fieldIndexOf(int methodPointer) 863 { 864 /* "source" 865 ^self extractBits: 3 to: 7 866 of: (self headerOf: methodPointer) 867 */ 868 return extractBits_to_of(3, 7, headerOf(methodPointer)); 869 } 870 871 // methodClassOf: 872 int methodClassOf(int methodPointer); 873 874 // literal:ofMethod: 875 inline int literal_ofMethod(int offset, int methodPointer) 876 { 877 /* "source" 878 ^memory fetchPointer: offset + LiteralStart 879 ofObject: methodPointer 880 */ 881 882 return memory.fetchPointer_ofObject(offset + LiteralStart, methodPointer); 883 } 884 885 // temporaryCountOf: 886 inline int temporaryCountOf(int methodPointer) 887 { 888 /* "source" 889 ^self extractBits: 3 to: 7 890 of: (self headerOf: methodPointer) 891 */ 892 893 return extractBits_to_of(3, 7, headerOf(methodPointer)); 894 } 895 896 // largeContextFlagOf: 897 inline int largeContextFlagOf(int methodPointer) 898 { 899 /* "source" 900 ^self extractBits: 8 to: 8 901 of: (self headerOf: methodPointer) 902 */ 903 904 return extractBits_to_of(8, 8, headerOf(methodPointer)); 905 } 906 907 // objectPointerCountOf: 908 inline int objectPointerCountOf(int methodPointer) 909 { 910 /* "source" 911 ^(self literalCountOf: methodPointer) + LiteralStart 912 */ 913 914 return literalCountOf(methodPointer) + LiteralStart; 915 } 916 917 // headerExtensionOf: 918 inline int headerExtensionOf(int methodPointer) 919 { 920 int literalCount; 921 922 /* "source" 923 literalCount <- self literalCountOf: methodPointer. 924 ^self literal: literalCount - 2 925 ofMethod: methodPointer 926 */ 927 928 literalCount = literalCountOf(methodPointer); 929 return literal_ofMethod(literalCount - 2, methodPointer); 930 } 931 932 // flagValueOf: 933 inline int flagValueOf(int methodPointer) 934 { 935 /* "source" 936 ^self extractBits: 0 to: 2 937 of: (self headerOf: methodPointer) 938 */ 939 940 return extractBits_to_of(0, 2, headerOf(methodPointer)); 941 } 942 943 // initialInstructionPointerOfMethod: 944 inline int initialInstructionPointerOfMethod(int methodPointer) 945 { 946 /* "source" 947 ^((self literalCountOf: methodPointer) + LiteralStart) * 2 + 1 948 */ 949 950 return (literalCountOf(methodPointer) + LiteralStart) * 2 + 1; 951 } 952 953 954 // --- StackBytecodes --- 955 956 // pushLiteralVariableBytecode 957 inline void pushLiteralVariableBytecode() 958 { 959 int fieldIndex; 960 961 /* "source" 962 fieldIndex <- self extractBits: 11 to: 15 963 of: currentBytecode. 964 self pushLiteralVariable: fieldIndex 965 */ 966 fieldIndex = extractBits_to_of(11, 15, currentBytecode); 967 pushLiteralVariable(fieldIndex); 968 } 969 970 // pushLiteralConstant: 971 inline void pushLiteralConstant(int literalIndex) 972 { 973 /* "source" 974 self push: (self literal: literalIndex) 975 */ 976 977 push(literal(literalIndex)); 978 } 979 980 // popStackBytecode 981 inline void popStackBytecode() 982 { 983 /* "source" 984 self popStack 985 */ 986 popStack(); 987 } 988 989 // storeAndPopReceiverVariableBytecode 990 void storeAndPopReceiverVariableBytecode(); 991 992 // extendedStoreBytecode 993 void extendedStoreBytecode(); 994 995 // pushLiteralConstantBytecode 996 void pushLiteralConstantBytecode(); 997 998 // storeAndPopTemporaryVariableBytecode 999 void storeAndPopTemporaryVariableBytecode(); 1000 1001 // extendedStoreAndPopBytecode 1002 void extendedStoreAndPopBytecode(); 1003 1004 // pushReceiverBytecode 1005 inline void pushReceiverBytecode() 1006 { 1007 /* "source" 1008 self push: receiver 1009 */ 1010 1011 push(receiver); 1012 } 1013 1014 // duplicateTopBytecode 1015 inline void duplicateTopBytecode() 1016 { 1017 /* "source" 1018 ^self push: self stackTop 1019 */ 1020 1021 push(stackTop()); 1022 } 1023 1024 // pushReceiverVariableBytecode 1025 inline void pushReceiverVariableBytecode() 1026 { 1027 int fieldIndex; 1028 1029 /* "source" 1030 fieldIndex <- self extractBits: 12 to: 15 1031 of: currentBytecode. 1032 self pushReceiverVariable: fieldIndex 1033 */ 1034 1035 fieldIndex = extractBits_to_of(12, 15, currentBytecode); 1036 pushReceiverVariable(fieldIndex); 1037 } 1038 1039 // pushActiveContextBytecode 1040 inline void pushActiveContextBytecode() 1041 { 1042 /* "source" 1043 self push: activeContext 1044 */ 1045 push(activeContext); 1046 } 1047 1048 // stackBytecode 1049 void stackBytecode(); 1050 1051 // extendedPushBytecode 1052 void extendedPushBytecode(); 1053 1054 // pushTemporaryVariable: 1055 inline void pushTemporaryVariable(int temporaryIndex) 1056 { 1057 /* "source" 1058 self push: (self temporary: temporaryIndex) 1059 */ 1060 1061 push(temporary(temporaryIndex)); 1062 } 1063 1064 // pushReceiverVariable: 1065 inline void pushReceiverVariable(int fieldIndex) 1066 { 1067 /* "source" 1068 self push: (memory fetchPointer: fieldIndex 1069 ofObject: receiver) 1070 */ 1071 push(memory.fetchPointer_ofObject(fieldIndex, receiver)); 1072 } 1073 1074 // pushConstantBytecode 1075 void pushConstantBytecode(); 1076 1077 // pushTemporaryVariableBytecode 1078 inline void pushTemporaryVariableBytecode() 1079 { 1080 int fieldIndex; 1081 1082 /* "source" 1083 fieldIndex <- self extractBits: 12 to: 15 1084 of: currentBytecode. 1085 self pushTemporaryVariable: fieldIndex 1086 */ 1087 1088 fieldIndex = extractBits_to_of(12, 15, currentBytecode); 1089 pushTemporaryVariable(fieldIndex); 1090 } 1091 1092 // pushLiteralVariable: 1093 inline void pushLiteralVariable(int literalIndex) 1094 { 1095 int association; 1096 1097 /* "source" 1098 association <- self literal: literalIndex. 1099 self push: (memory fetchPointer: ValueIndex 1100 ofObject: association) 1101 */ 1102 association = literal(literalIndex); 1103 push(memory.fetchPointer_ofObject(ValueIndex, association)); 1104 } 1105 1106 1107 // --- JumpBytecodes --- 1108 1109 // sendMustBeBoolean 1110 inline void sendMustBeBoolean() 1111 { 1112 /* "source" 1113 self sendSelector: MustBeBooleanSelector 1114 argumentCount: 0 1115 */ 1116 1117 sendSelector_argumentCount(MustBeBooleanSelector, 0); 1118 } 1119 1120 // shortUnconditionalJump 1121 inline void shortUnconditionalJump() 1122 { 1123 int offset; 1124 1125 /* "source" 1126 offset <- self extractBits: 13 to: 15 1127 of: currentBytecode. 1128 self jump: offset + 1 1129 */ 1130 1131 offset = extractBits_to_of(13, 15, currentBytecode); 1132 jump(offset + 1); 1133 } 1134 1135 // jump: 1136 inline void jump(int offset) 1137 { 1138 /* "source" 1139 instructionPointer <- instructionPointer + offset 1140 */ 1141 1142 instructionPointer = instructionPointer + offset; 1143 } 1144 1145 // jumpIf:by: 1146 void jumpIf_by(int condition, int offset); 1147 1148 // longConditionalJump 1149 void longConditionalJump(); 1150 1151 // longUnconditionalJump 1152 inline void longUnconditionalJump() 1153 { 1154 int offset; 1155 1156 /* "source" 1157 offset <- self extractBits: 13 to: 15 1158 of: currentBytecode. 1159 self jump: offset - 4 * 256 + self fetchByte 1160 */ 1161 offset = extractBits_to_of(13, 15, currentBytecode); 1162 jump((offset - 4) * 256 + fetchByte()); 1163 } 1164 1165 // jumpBytecode 1166 void jumpBytecode(); 1167 1168 // shortConditionalJump 1169 inline void shortConditionalJump() 1170 { 1171 int offset; 1172 1173 /* "source" 1174 offset <- self extractBits: 13 to: 15 1175 of: currentBytecode. 1176 self jumpIf: FalsePointer 1177 by: offset + 1 1178 */ 1179 1180 offset = extractBits_to_of(13, 15, currentBytecode); 1181 jumpIf_by(FalsePointer, offset + 1); 1182 } 1183 1184 1185 // --- IntegerAccess --- 1186 1187 // storeInteger:ofObject:withValue: 1188 void storeInteger_ofObject_withValue(int fieldIndex, int objectPointer, int integerValue); 1189 1190 // extractBits:to:of: 1191 inline int extractBits_to_of(int firstBitIndex, int lastBitIndex, int anInteger) 1192 { 1193 /* "source" 1194 ^(anInteger bitShift: lastBitIndex - 15) 1195 bitAnd: (2 raisedTo: lastBitIndex - firstBitIndex + 1) - 1 1196 */ 1197 1198 std::uint16_t mask = (1 << (lastBitIndex - firstBitIndex + 1)) - 1; 1199 std::uint16_t shift = anInteger >> (15-lastBitIndex); 1200 return shift & mask; 1201 1202 } 1203 1204 // transfer:fromIndex:ofObject:toIndex:ofObject: 1205 void transfer_fromIndex_ofObject_toIndex_ofObject( 1206 int count, 1207 int firstFrom, 1208 int fromOop, 1209 int firstTo, 1210 int toOop 1211 ); 1212 1213 // lowByteOf: 1214 inline int lowByteOf(int anInteger) 1215 { 1216 /* "source" 1217 ^self extractBits: 8 to: 15 1218 of: anInteger 1219 */ 1220 1221 return extractBits_to_of(8, 15, anInteger); 1222 } 1223 1224 // fetchInteger:ofObject: 1225 int fetchInteger_ofObject(int fieldIndex, int objectPointer); 1226 1227 // highByteOf: 1228 inline int highByteOf(int anInteger) 1229 { 1230 /* "source" 1231 ^self extractBits: 0 to: 7 1232 of: anInteger 1233 */ 1234 return extractBits_to_of(0, 7, anInteger); 1235 } 1236 1237 1238 // --- StoreMgmtPrims --- 1239 1240 // checkInstanceVariableBoundsOf:in: 1241 inline void checkInstanceVariableBoundsOf_in(int index, int object) 1242 { 1243 /* "source" 1244 class <- memory fetchClassOf: object. 1245 self success: index >= 1. 1246 self success: index <= (self lengthOf: object) 1247 */ 1248 //cls = memory.fetchClassOf(object); 1249 success(index >= 1); 1250 success(index <= lengthOf(object)); 1251 } 1252 1253 // primitiveNewMethod 1254 void primitiveNewMethod(); 1255 1256 // primitiveAsOop 1257 void primitiveAsOop(); 1258 1259 // primitiveSomeInstance 1260 void primitiveSomeInstance(); 1261 1262 // primitiveObjectAt 1263 void primitiveObjectAt(); 1264 1265 // primitiveNextInstance 1266 void primitiveNextInstance(); 1267 1268 // primitiveNew 1269 void primitiveNew(); 1270 1271 // primitiveAsObject 1272 void primitiveAsObject(); 1273 1274 // primitiveNewWithArg 1275 void primitiveNewWithArg(); 1276 1277 // primitiveInstVarAtPut 1278 void primitiveInstVarAtPut(); 1279 1280 // primitiveObjectAtPut 1281 void primitiveObjectAtPut(); 1282 1283 // primitiveInstVarAt 1284 void primitiveInstVarAt(); 1285 1286 // primitiveBecome 1287 void primitiveBecome(); 1288 1289 // dispatchStorageManagementPrimitives 1290 void dispatchStorageManagementPrimitives(); 1291 1292 // --Float Access-- 1293 void pushFloat(float f); 1294 1295 inline float extractFloat(int objectPointer) 1296 { 1297 std::uint32_t uint32 = (memory.fetchWord_ofObject(1, objectPointer) << 16) | memory.fetchWord_ofObject(0, objectPointer); 1298 return * (float *) &uint32; 1299 } 1300 1301 float popFloat(); 1302 1303 1304 bool isInLowMemoryCondition(); 1305 1306 #ifdef GC_MARK_SWEEP 1307 void prepareForCollection(); 1308 void collectionCompleted(); 1309 #endif 1310 1311 private: 1312 // initializePointIndices 1313 static const int XIndex = 0; 1314 static const int YIndex = 1; 1315 static const int ClassPointSize = 2; 1316 1317 // initializeStreamIndices 1318 static const int StreamArrayIndex = 0; 1319 static const int StreamIndexIndex = 1; 1320 static const int StreamReadLimitIndex = 2; 1321 static const int StreamWriteLimitIndex = 3; 1322 1323 // initializeSchedulerIndices 1324 // Class ProcessorScheduler 1325 static const int ProcessListsIndex = 0; 1326 static const int ActiveProcessIndex = 1; 1327 // Class LinkedList 1328 static const int FirstLinkIndex = 0; 1329 static const int LastLinkIndex = 1; 1330 // Class Semaphore 1331 static const int ExcessSignalsIndex = 2; 1332 // Class Link 1333 static const int NextLinkIndex = 0; 1334 // Class Process 1335 static const int SuspendedContextIndex = 1; 1336 static const int PriorityIndex = 2; 1337 static const int MyListIndex = 3; 1338 1339 // initializeMessageIndices 1340 static const int MessageSelectorIndex = 0; 1341 static const int MessageArgumentsIndex = 1; 1342 static const int MessageSize = 2; 1343 1344 // initializeClassIndices 1345 // Class Class 1346 static const int SuperclassIndex = 0; 1347 static const int MessageDictionaryIndex = 1; 1348 static const int InstanceSpecificationIndex = 2; 1349 // Fields of a message dictionary 1350 static const int MethodArrayIndex = 1; 1351 static const int SelectorStart = 2; 1352 1353 // initializeSmallIntegers 1354 // SmallIntegers" 1355 static const int MinusOnePointer = 65535; 1356 static const int ZeroPointer = 1; 1357 static const int OnePointer = 3; 1358 static const int TwoPointer = 5; 1359 1360 // initializeContextIndices 1361 // Class MethodContext 1362 static const int SenderIndex = 0; 1363 static const int InstructionPointerIndex = 1; 1364 static const int StackPointerIndex = 2; 1365 static const int MethodIndex = 3; 1366 static const int ReceiverIndex = 5; 1367 static const int TempFrameStart = 6; 1368 // Class BlockContext 1369 static const int CallerIndex = 0; 1370 static const int BlockArgumentCountIndex = 3; 1371 static const int InitialIPIndex = 4; 1372 static const int HomeIndex = 5; 1373 1374 // initializeAssociationIndex 1375 static const int ValueIndex = 1; 1376 1377 // initializeCharacterIndex 1378 static const int CharacterValueIndex = 0; 1379 1380 // initializeMethodIndices 1381 // Class CompiledMethod 1382 static const int HeaderIndex = 0; 1383 static const int LiteralStart = 1; 1384 1385 // Forms 1386 static const int BitsInForm = 0; 1387 static const int WidthInForm = 1; 1388 static const int HeightInForm = 2; 1389 static const int OffsetInForm = 3; 1390 1391 // Files 1392 static const int FileNameIndex = 1; // fileName field of File 1393 1394 // "Registers" 1395 int activeContext; 1396 int homeContext; 1397 int method; 1398 int receiver; 1399 int instructionPointer; 1400 int stackPointer; 1401 int currentBytecode; 1402 bool successFlag; 1403 1404 // Class related registers 1405 int messageSelector; 1406 int argumentCount; 1407 int newMethod; 1408 int primitiveIndex; 1409 1410 1411 // Process-related Registers (pg 642) 1412 bool newProcessWaiting; // The newProcessWaiting register will be true if a process switch is called for and false otherwise. 1413 int newProcess; // If newProcessWaiting is true then the newProcess register will point to the Process to be transferred to. 1414 /* 1415 The semaphoreList register points to an Array used by the interpreter to buffer Semaphores that should be signaled. This is an Array in Interpreter, not in the object memory. It will be a table in a machine-language interpreter. 1416 */ 1417 1418 int semaphoreList[4096]; 1419 1420 int semaphoreIndex; // The semaphoreIndex register hold the index of the last Semaphore in the semaphoreList buffer. 1421 1422 // Using an array of int for method cache to remain faithful as possible to the bluebook 1423 // Any size change will require changes to hash function in findNewMethodInClass 1424 int methodCache[1024]; 1425 1426 ObjectMemory memory; 1427 1428 // dbanay - primitiveSignalAtOopsLeftWordsLeft support 1429 bool checkLowMemory; 1430 bool memoryIsLow; 1431 int lowSpaceSemaphore; 1432 int oopsLeftLimit; 1433 std::uint32_t wordsLeftLimit; 1434 1435 IHardwareAbstractionLayer *hal; 1436 IFileSystem *fileSystem; 1437 int currentDisplay; 1438 int currentDisplayWidth; 1439 int currentDisplayHeight; 1440 int currentCursor; 1441 1442 // Return a std::string for a string or symbol oop 1443 std::string stringFromObject(int strOop); 1444 int stringObjectFor(const char *s); 1445 1446 #ifdef DEBUGGING_SUPPORT 1447 std::string selectorName(int selector); 1448 std::string classNameOfObject(int objectPointer); 1449 std::string className(int classPointer); 1450 #endif 1451 1452 }; 1453