interpreter.cpp (163912B)
1 // 2 // interpreter.cpp 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 29 #include <cstdint> 30 #include <cassert> 31 #include <cstdlib> 32 #include <cmath> 33 #include <limits> 34 #include "oops.h" 35 #include "interpreter.h" 36 #include "bitblt.h" 37 38 39 inline bool between_and(int value, int min, int max ) 40 { 41 return value >= min && value <= max; 42 } 43 44 Interpreter::Interpreter(IHardwareAbstractionLayer *halInterface, IFileSystem *fileSystemInterface) : hal(halInterface), fileSystem(fileSystemInterface), 45 #ifdef GC_MARK_SWEEP 46 memory(halInterface, this) 47 #else 48 memory(halInterface) 49 #endif 50 { 51 } 52 53 bool Interpreter::init() 54 { 55 initializeMethodCache(); 56 semaphoreIndex = -1; 57 if (!memory.loadSnapshot(fileSystem, hal->get_image_name())) 58 return false; 59 /* 60 When Smalltalk is started up, the initial active context is found through the scheduler's active Process. 61 (G&R pg. 644) 62 */ 63 activeContext = firstContext(); 64 memory.increaseReferencesTo(activeContext); 65 fetchContextRegisters(); 66 checkLowMemory = false; 67 memoryIsLow = false; 68 lowSpaceSemaphore = NilPointer; 69 oopsLeftLimit = 0; 70 wordsLeftLimit = 0; 71 currentDisplay = 0; 72 currentCursor = 0; 73 currentDisplayWidth = 0; 74 currentDisplayHeight = 0; 75 return true; 76 } 77 78 void Interpreter::error(const char *message) 79 { 80 hal->error(message); 81 } 82 83 84 #ifdef GC_MARK_SWEEP 85 void Interpreter::prepareForCollection() 86 { 87 storeContextRegisters(); 88 memory.addRoot(SmalltalkPointer); 89 memory.addRoot(activeContext); 90 if (newProcess != NilPointer) 91 { 92 memory.addRoot(newProcess); 93 } 94 95 } 96 void Interpreter::collectionCompleted() 97 { 98 memory.increaseReferencesTo(activeContext); 99 fetchContextRegisters(); 100 if (newProcessWaiting) 101 { 102 memory.increaseReferencesTo(newProcess); 103 } 104 } 105 #endif 106 107 // primitiveAtEnd 108 void Interpreter::primitiveAtEnd() 109 { 110 #ifndef IMPLEMENT_PRIMITIVE_AT_END 111 primitiveFail(); 112 #else 113 int stream; 114 int array; 115 int arrayClass; 116 int length; 117 int index; 118 int limit; 119 120 /* "source" 121 stream <- self popStack. 122 array <- memory fetchPointer: StreamArrayIndex 123 ofObject: stream. 124 arrayClass <- memory fetchClassOf: array. 125 length <- self lengthOf: array. 126 index <- self fetchInteger: StreamIndexIndex 127 ofObject: stream. 128 limit <- self fetchInteger: StreamReadLimitIndex 129 ofObject: stream. "ERROR dbanay : was Stream" 130 self success: 131 (arrayClass=ClassArrayPointer) | (arrayClass=ClassStringPointer). 132 self success 133 ifTrue: [(index >= limit) | (index >= length) 134 ifTrue: [self push: TruePointer] 135 ifFalse: [self push: FalsePointer]] 136 ifFalse: [self unPop: 1] 137 */ 138 stream = popStack(); 139 array = memory.fetchPointer_ofObject(StreamArrayIndex, stream); 140 arrayClass = memory.fetchClassOf(array); 141 length = lengthOf(array); 142 index = fetchInteger_ofObject(StreamIndexIndex, stream); 143 limit = fetchInteger_ofObject(StreamReadLimitIndex, stream); 144 success(arrayClass == ClassArrayPointer || arrayClass == ClassStringPointer); 145 if (success()) 146 { 147 if (index >= limit || index >= length) 148 push(TruePointer); 149 else 150 push(FalsePointer); 151 } 152 else 153 { 154 unPop(1); 155 } 156 #endif 157 } 158 159 160 // checkIndexableBoundsOf:in: 161 void Interpreter::checkIndexableBoundsOf_in(int index, int array) 162 { 163 164 /* "source" 165 "dbanay - this is totally wrong" 166 class <- memory fetchClassOf: array. 167 self success: index >= 1. 168 self success: index + (self fixedFieldsOf: class) <= (self lengthOf: array) 169 */ 170 /* "source corrected" 171 self success: index >= 1. 172 self success: index <= (self lengthOf: array) 173 */ 174 success(index >= 1); 175 success(index <= lengthOf(array)); 176 } 177 178 179 // primitiveNextPut 180 void Interpreter::primitiveNextPut() 181 { 182 #ifndef IMPLEMENT_PRIMITIVE_NEXT_PUT 183 primitiveFail(); 184 185 #else 186 int value; 187 int stream; 188 int index; 189 int limit; 190 int array; 191 int arrayClass; 192 int ascii; 193 194 /* "source" 195 value <- self popStack. 196 stream <- self popStack. 197 array <- memory fetchPointer: StreamArrayIndex 198 ofObject: stream. 199 arrayClass <- memory fetchClassOf: array. 200 index <- self fetchInteger: StreamIndexIndex 201 ofObject: stream. 202 limit <- self fetchInteger: StreamWriteLimitIndex 203 ofObject: stream. 204 self success: index < limit. 205 self success: 206 (arrayClass=ClassArrayPointer) | (arrayClass=ClassStringPointer). 207 self checkIndexableBoundsOf: index + 1 208 in: array. 209 self success 210 ifTrue: [index <- index + 1. 211 arrayClass = ClassArrayPointer 212 ifTrue: [self subscript: array 213 with: index 214 storing: value] 215 ifFalse: [ascii <- memory fetchPointer: CharacterValueIndex 216 ofObject: value. 217 self subscript: array 218 with: index 219 storing: ascii]]. 220 self success 221 ifTrue: [self storeInteger: StreamIndexIndex 222 ofObject: stream 223 withValue: index]. 224 self success 225 ifTrue: [self push: value] 226 ifFalse: [self unPop: 2] 227 */ 228 229 230 value = popStack(); 231 stream = popStack(); 232 array = memory.fetchPointer_ofObject(StreamArrayIndex, stream); 233 arrayClass = memory.fetchClassOf(array); 234 index = fetchInteger_ofObject(StreamIndexIndex, stream); 235 limit = fetchInteger_ofObject(StreamWriteLimitIndex,stream); 236 success(index < limit); 237 success(arrayClass==ClassArrayPointer || arrayClass==ClassStringPointer); 238 checkIndexableBoundsOf_in(index + 1, array); 239 if (success()) 240 { 241 index = index + 1; 242 if (arrayClass == ClassArrayPointer) 243 { 244 subscript_with_storing(array, index, value); 245 } 246 else 247 { 248 ascii = memory.fetchPointer_ofObject(CharacterValueIndex, value); 249 subscript_with_storing(array, index, ascii); 250 } 251 } 252 if (success()) 253 storeInteger_ofObject_withValue(StreamIndexIndex, stream, index); 254 if (success()) 255 push(value); 256 else 257 unPop(2); 258 #endif 259 } 260 261 262 // lengthOf: 263 int Interpreter::lengthOf(int array) 264 { 265 /* "source" 266 (self isWords: (memory fetchClassOf: array)) 267 ifTrue: [^memory fetchWordLengthOf: array] 268 ifFalse: [^memory fetchByteLengthOf: array] 269 */ 270 271 return isWords(memory.fetchClassOf(array)) 272 ? memory.fetchWordLengthOf(array) : 273 memory.fetchByteLengthOf(array); 274 } 275 276 277 // primitiveNext 278 void Interpreter::primitiveNext() 279 { 280 #ifndef IMPLEMENT_PRIMITIVE_NEXT 281 primitiveFail(); 282 283 #else 284 int stream; 285 int index; 286 int limit; 287 int array; 288 int arrayClass; 289 int result = 0; 290 int ascii; 291 292 /* "source" 293 stream <- self popStack. 294 array <- memory fetchPointer: StreamArrayIndex 295 ofObject: stream. 296 arrayClass <- memory fetchClassOf: array. 297 index <- self fetchInteger: StreamIndexIndex 298 ofObject: stream. 299 limit <- self fetchInteger: StreamReadLimitIndex 300 ofObject: stream. 301 self success: index < limit. 302 self success: 303 (arrayClass=ClassArrayPointer) | (arrayClass=ClassStringPointer). 304 self checkIndexableBoundsOf: index + 1 305 in: array. 306 self success 307 ifTrue: [index <- index + 1. 308 result <- self subscript: array 309 with: index]. 310 self success 311 ifTrue: [self storeInteger: StreamIndexIndex 312 ofObject: stream 313 withValue: index]. 314 self success 315 ifTrue: [arrayClass = ClassArrayPointer 316 ifTrue: [self push: result] 317 ifFalse: [ascii <- memory integerValueOf: result. 318 self push: (memory fetchPointer: ascii 319 ofObject: CharacterTablePointer)]] 320 ifFalse: [self unPop: 1] 321 */ 322 stream = popStack(); 323 array = memory.fetchPointer_ofObject(StreamArrayIndex, stream); 324 arrayClass = memory.fetchClassOf(array); 325 index = fetchInteger_ofObject(StreamIndexIndex, stream); 326 limit = fetchInteger_ofObject(StreamReadLimitIndex, stream); 327 success(index < limit); 328 success(arrayClass == ClassArrayPointer || arrayClass == ClassStringPointer); 329 checkIndexableBoundsOf_in(index + 1, array); 330 if (success()) 331 { 332 index = index + 1; 333 result = subscript_with(array, index); 334 } 335 if (success()) 336 { 337 storeInteger_ofObject_withValue(StreamIndexIndex, stream, index); 338 } 339 if (success()) 340 { 341 if (arrayClass == ClassArrayPointer) 342 { 343 push(result); 344 } 345 else 346 { 347 ascii = memory.integerValueOf(result); 348 push(memory.fetchPointer_ofObject(ascii, CharacterTablePointer)); 349 } 350 } 351 else 352 unPop(1); 353 #endif 354 } 355 356 357 // dispatchSubscriptAndStreamPrimitives 358 void Interpreter::dispatchSubscriptAndStreamPrimitives() 359 { 360 /* "source" 361 primitiveIndex = 60 ifTrue: [^self primitiveAt]. 362 primitiveIndex = 61 ifTrue: [^self primitiveAtPut]. 363 primitiveIndex = 62 ifTrue: [^self primitiveSize]. 364 primitiveIndex = 63 ifTrue: [^self primitiveStringAt]. 365 primitiveIndex = 64 ifTrue: [^self primitiveStringAtPut]. 366 primitiveIndex = 65 ifTrue: [^self primitiveNext]. 367 primitiveIndex = 66 ifTrue: [^self primitiveNextPut]. 368 primitiveIndex = 67 ifTrue: [^self primitiveAtEnd] 369 */ 370 switch (primitiveIndex) 371 { 372 case 60: primitiveAt(); break; 373 case 61: primitiveAtPut(); break; 374 case 62: primitiveSize(); break; 375 case 63: primitiveStringAt(); break; 376 case 64: primitiveStringAtPut(); break; 377 case 65: primitiveNext(); break; 378 case 66: primitiveNextPut(); break; 379 case 67: primitiveAtEnd(); break; 380 } 381 } 382 383 384 // primitiveStringAt 385 void Interpreter::primitiveStringAt() 386 { 387 int index; 388 int array; 389 int ascii; 390 int character = 0; 391 392 /* "source" 393 index <- self positive16BitValueOf: self popStack. 394 array <- self popStack. 395 self checkIndexableBoundsOf: index 396 in: array. 397 self success 398 ifTrue: [ascii <- memory integerValueOf: (self subscript: array 399 with: index). 400 character <- memory fetchPointer: ascii 401 ofObject: CharacterTablePointer]. 402 self success 403 ifTrue: [self push: character] 404 ifFalse: [self unPop: 2] 405 */ 406 407 index = positive16BitValueOf(popStack()); 408 array = popStack(); 409 checkIndexableBoundsOf_in(index, array); 410 if (success()) 411 { 412 ascii = memory.integerValueOf(subscript_with(array, index)); 413 character = memory.fetchPointer_ofObject(ascii, CharacterTablePointer); 414 } 415 416 if (success()) 417 { 418 push(character); 419 } 420 else 421 unPop(2); 422 } 423 424 425 // primitiveAt 426 void Interpreter::primitiveAt() 427 { 428 int index; 429 int array; 430 int arrayClass; 431 int result = 0; 432 433 /* "source" 434 index <- self positive16BitValueOf: self popStack. 435 array <- self popStack. 436 arrayClass <- memory fetchClassOf: array. 437 self checkIndexableBoundsOf: index 438 in: array. 439 self success 440 ifTrue: [index <- index + (self fixedFieldsOf: arrayClass). 441 result <- self subscript: array 442 with: index]. 443 self success 444 ifTrue: [self push: result] 445 ifFalse: [self unPop: 2] 446 */ 447 index = positive16BitValueOf(popStack()); 448 array = popStack(); 449 arrayClass = memory.fetchClassOf(array); 450 checkIndexableBoundsOf_in(index, array); 451 452 if (success()) 453 { 454 index = index + fixedFieldsOf(arrayClass); 455 result = subscript_with(array, index); 456 } 457 458 if (success()) 459 { 460 push(result); 461 } 462 else 463 unPop(2); 464 } 465 466 467 // primitiveSize 468 void Interpreter::primitiveSize() 469 { 470 int array; 471 int cls; 472 int length = 0; 473 474 /* "source" 475 array <- self popStack. 476 "dbanay ERROR make sure it is a pointer!" 477 self success: memory isIntegerObject: array. 478 self success ifTrue: [ class <- memory fetchClassOf: array. 479 length <- self positive16BitIntegerFor: 480 (self lengthOf: array) - (self fixedFieldsOf: class)]. 481 self success 482 ifTrue: [self push: length] 483 ifFalse: [self unPop: 1] 484 */ 485 486 array = popStack(); 487 success(!memory.isIntegerObject(array)); 488 if (success()) 489 { 490 cls = memory.fetchClassOf(array); 491 length = positive16BitIntegerFor(lengthOf(array) - fixedFieldsOf(cls)); 492 } 493 if (success()) 494 push(length); 495 else 496 unPop(1); 497 } 498 499 500 // primitiveStringAtPut 501 void Interpreter::primitiveStringAtPut() 502 { 503 int index; 504 int array; 505 int ascii; 506 int character; 507 508 /* "source" 509 character <- self popStack. 510 index <- self positive16BitValueOf: self popStack. 511 array <- self popStack. 512 self checkIndexableBoundsOf: index 513 in: array. 514 self success: (memory fetchClassOf: character) = ClassCharacterPointer. 515 self success 516 ifTrue: [ascii <- memory fetchPointer: CharacterValueIndex 517 ofObject: character. 518 self subscript: array 519 with: index 520 storing: ascii]. 521 self success 522 ifTrue: [self push: character] 523 ifFalse: [self unPop: 2] "dbanay ERROR should be 3" 524 */ 525 character = popStack(); 526 index = positive16BitValueOf(popStack()); 527 array = popStack(); 528 checkIndexableBoundsOf_in(index, array); 529 success(memory.fetchClassOf(character) == ClassCharacterPointer); 530 if (success()) 531 { 532 ascii = memory.fetchPointer_ofObject(CharacterValueIndex, character); 533 subscript_with_storing(array, index, ascii); 534 } 535 if (success()) 536 push(character); 537 else 538 unPop(3); 539 } 540 541 542 // subscript:with: 543 int Interpreter::subscript_with(int array, int index) 544 { 545 int cls; 546 int value; 547 548 /* "source" 549 class <- memory fetchClassOf: array. 550 (self isWords: class) 551 ifTrue: [(self isPointers: class) 552 ifTrue: [^memory fetchPointer: index - 1 553 ofObject: array] 554 ifFalse: [value <- memory fetchWord: index - 1 555 ofObject: array. 556 ^self positive16BitIntegerFor: value]] 557 ifFalse: [value <- memory fetchByte: index - 1 558 ofObject: array. 559 ^memory integerObjectOf: value] 560 */ 561 cls = memory.fetchClassOf(array); 562 if (isWords(cls)) 563 { 564 if (isPointers(cls)) 565 { 566 return memory.fetchPointer_ofObject(index - 1, array); 567 } 568 else 569 { 570 value = memory.fetchWord_ofObject(index - 1, array); 571 return positive16BitIntegerFor(value); 572 } 573 } 574 575 value = memory.fetchByte_ofObject(index - 1, array); 576 return memory.integerObjectOf(value); 577 } 578 579 580 // primitiveAtPut 581 void Interpreter::primitiveAtPut() 582 { 583 int array; 584 int index; 585 int arrayClass; 586 int value; 587 588 /* "source" 589 value <- self popStack. 590 index <- self positive16BitValueOf: self popStack. 591 array <- self popStack. 592 arrayClass <- memory fetchClassOf: array. 593 self checkIndexableBoundsOf: index. 594 in: array. 595 self success 596 ifTrue: [index <- index + (self fixedFieldsOf: arrayClass). 597 self subscript: array 598 with: index 599 storing: value]. 600 self success 601 ifTrue: [self push: value] 602 ifFalse: [self unPop: 3] 603 */ 604 value = popStack(); 605 index = positive16BitValueOf(popStack()); 606 array = popStack(); 607 arrayClass = memory.fetchClassOf(array); 608 checkIndexableBoundsOf_in(index, array); 609 if (success()) 610 { 611 index = index + fixedFieldsOf(arrayClass); 612 subscript_with_storing(array, index, value); 613 } 614 615 if (success()) 616 push(value); 617 else 618 unPop(3); 619 } 620 621 622 // subscript:with:storing: 623 void Interpreter::subscript_with_storing(int array, int index, int value) 624 { 625 int cls; 626 627 /* "source" 628 class <- memory fetchClassOf: array. 629 (self isWords: class) 630 ifTrue: [(self isPointers: class) 631 ifTrue: [^memory storePointer: index - 1 632 ofObject: array 633 withValue: value] 634 ifFalse: [self success: (memory isIntegerObject: value). 635 self success: ifTrue: 636 [^memory 637 storeWord: index-1 638 ofObject: array 639 withValue: (self positive16BitValueOf: 640 value)]]] 641 ifFalse: [self success: (memory isIntegerObject: value). "ERROR: dbanay check for large pos as well" 642 self success ifTrue: 643 [^memory storeByte: index - 1 644 ofObject: array 645 withValue: (self lowByteOf: 646 (memory integerValueOf: 647 value))]] 648 */ 649 650 cls = memory.fetchClassOf(array); 651 if (isWords(cls)) 652 { 653 if (isPointers(cls)) 654 { 655 memory.storePointer_ofObject_withValue(index - 1, array, value); 656 } 657 else 658 { 659 success(memory.isIntegerObject(value) || memory.fetchClassOf(value) == ClassLargePositiveIntegerPointer); 660 if (success()) 661 { 662 memory.storeWord_ofObject_withValue(index - 1, array, positive16BitValueOf(value)); 663 } 664 } 665 } 666 else 667 { 668 success(memory.isIntegerObject(value)); 669 if (success()) 670 { 671 memory.storeByte_ofObject_withValue(index - 1, array, lowByteOf(memory.integerValueOf(value))); 672 } 673 } 674 } 675 676 677 /* 678 The instruction pointer stored in a context is a one-relative index to the method's fields because subscripting in Smalltalk (i.e., the at: message) takes one-relative indices. The memory, however, uses zero-relative indices; so the fetchContextRegisters routine subtracts one to convert it to a memory index and the storeContextRegisters routine adds the one back in. 679 */ 680 // storeContextRegisters 681 void Interpreter::storeContextRegisters() 682 { 683 /* "source" 684 self storeInstructionPointerValue: instructionPointer + 1 685 inContext: activeContext. 686 self storeStackPointerValue: stackPointer - TempFrameStart + 1 687 inContext: activeContext 688 */ 689 690 storeInstructionPointerValue_inContext(instructionPointer + 1, activeContext); 691 storeStackPointerValue_inContext(stackPointer - TempFrameStart + 1, activeContext); 692 } 693 694 // isBlockContext: 695 bool Interpreter::isBlockContext(int contextPointer) 696 { 697 int methodOrArguments; 698 699 /* "source" 700 methodOrArguments <- memory fetchPointer: MethodIndex 701 ofObject: contextPointer. 702 ^memory isIntegerObject: methodOrArguments 703 */ 704 methodOrArguments = memory.fetchPointer_ofObject(MethodIndex, 705 contextPointer); 706 return memory.isIntegerObject(methodOrArguments); 707 708 } 709 710 // newActiveContext: 711 void Interpreter::newActiveContext(int aContext) 712 { 713 /* "source" 714 self storeContextRegisters. 715 memory decreaseReferencesTo: activeContext. 716 activeContext <- aContext. 717 memory increaseReferencesTo: activeContext. 718 self fetchContextRegisters 719 */ 720 721 storeContextRegisters(); 722 memory.decreaseReferencesTo(activeContext); 723 activeContext = aContext; 724 memory.increaseReferencesTo(activeContext); 725 fetchContextRegisters(); 726 } 727 728 /* 729 The instruction pointer stored in a context is a one-relative index to the method's fields because subscripting in Smalltalk (i.e., the at: message)takes one-relative indices. The memory, however, uses zero-relative indices; so the fetchContextRegisters routine subtracts one to convert it to a memory index and the storeContextRegisters routine adds the one back in. 730 */ 731 732 // fetchContextRegisters 733 void Interpreter::fetchContextRegisters() 734 { 735 /* "source" 736 (self isBlockContext: activeContext) 737 ifTrue: [homeContext <- memory fetchPointer: HomeIndex 738 ofObject: activeContext] 739 ifFalse: [homeContext <- activeContext]. 740 receiver <- memory fetchPointer: ReceiverIndex 741 ofObject: homeContext. 742 method <- memory fetchPointer: MethodIndex 743 ofObject: homeContext. 744 instructionPointer <- (self instructionPointerOfContext: activeContext) - 1. 745 stackPointer <- 746 (self stackPointerOfContext: activeContext) + TempFrameStart - 1 747 */ 748 749 if (isBlockContext(activeContext)) 750 homeContext = memory.fetchPointer_ofObject(HomeIndex, activeContext); 751 else 752 homeContext = activeContext; 753 receiver = memory.fetchPointer_ofObject(ReceiverIndex, homeContext); 754 method = memory.fetchPointer_ofObject(MethodIndex, homeContext); 755 instructionPointer = instructionPointerOfContext(activeContext) - 1; 756 stackPointer = stackPointerOfContext(activeContext) + TempFrameStart - 1; 757 } 758 759 // dispatchInputOutputPrimitives 760 void Interpreter::dispatchInputOutputPrimitives() 761 { 762 /* "source" 763 primitiveIndex = 90 ifTrue: [^self primitiveMousePoint]. 764 primitiveIndex = 91 ifTrue: [^self primitiveCursorLocPut]. 765 primitiveIndex = 92 ifTrue: [^self primitiveCursorLink]. 766 primitiveIndex = 93 ifTrue: [^self primitiveInputSemaphore]. 767 primitiveIndex = 94 ifTrue: [^self primitiveSampleInterval]. 768 primitiveIndex = 95 ifTrue: [^self primitiveInputWord]. 769 primitiveIndex = 96 ifTrue: [^self primitiveCopyBits]. 770 primitiveIndex = 97 ifTrue: [^self primitiveSnapshot]. 771 primitiveIndex = 98 ifTrue: [^self primitiveTimeWordsInto]. 772 primitiveIndex = 99 ifTrue: [^self primitiveTickWordsInto]. 773 primitiveIndex = 100 ifTrue: [^self primitiveSignalAtTick]. 774 primitiveIndex = 101 ifTrue: [^self primitiveBeCursor]. 775 primitiveIndex = 102 ifTrue: [^self primitiveBeDisplay]. 776 primitiveIndex = 103 ifTrue: [^self primitiveScanCharacters]. 777 primitiveIndex = 104 ifTrue: [^self primitiveDrawLoop]. 778 primitiveIndex = 105 ifTrue: [^self primitiveStringReplace] 779 */ 780 781 switch (primitiveIndex) 782 { 783 case 90: primitiveMousePoint(); break; 784 case 91: primitiveCursorLocPut(); break; 785 case 92: primitiveCursorLink(); break; 786 case 93: primitiveInputSemaphore(); break; 787 case 94: primitiveSampleInterval(); break; 788 case 95: primitiveInputWord(); break; 789 case 96: primitiveCopyBits(); break; 790 case 97: primitiveSnapshot(); break; 791 case 98: primitiveTimeWordsInto(); break; 792 case 99: primitiveTickWordsInto(); break; 793 case 100: primitiveSignalAtTick(); break; 794 case 101: primitiveBeCursor(); break; 795 case 102: primitiveBeDisplay(); break; 796 case 103: primitiveScanCharacters(); break; 797 case 104: primitiveDrawLoop(); break; 798 case 105: primitiveStringReplace(); break; 799 } 800 } 801 802 void Interpreter::primitiveMousePoint() 803 { 804 /* 805 Poll the mouse to find out its position. Return a Point. Fail if event-driven 806 tracking is used instead of polling. 807 */ 808 int mouseX, mouseY; 809 810 pop(1); // remove receiver 811 hal->get_cursor_location(&mouseX, &mouseY); 812 int pointResult = memory.instantiateClass_withPointers(ClassPointPointer, ClassPointSize); 813 storeInteger_ofObject_withValue(XIndex, pointResult, mouseX); 814 storeInteger_ofObject_withValue(YIndex, pointResult, mouseY); 815 push(pointResult); 816 } 817 818 void Interpreter::primitiveCursorLocPut() 819 { 820 /* 821 Move the cursor to the screen location specified by the argument. Fail if 822 the argument is not a Point. 823 */ 824 int point = popStack(); 825 success(memory.fetchClassOf(point) == ClassPointPointer); 826 if (success()) 827 { 828 int x = fetchInteger_ofObject(XIndex, point); 829 int y = fetchInteger_ofObject(YIndex, point); 830 hal->set_cursor_location(x, y); 831 } 832 else 833 unPop(1); 834 } 835 836 void Interpreter::primitiveCursorLink() 837 { 838 /* 839 Cause the cursor to track the pointing device location if the argument is true. 840 Decouple the cursor from the pointing device if the argument is false. 841 */ 842 int flag = popStack(); 843 hal->set_link_cursor(flag == TruePointer); 844 } 845 846 void Interpreter::primitiveInputSemaphore() 847 { 848 /* 849 Install the argument (a Semaphore) as the object to be signalled whenever 850 an input event occurs. The semaphore will be signaled once for every 851 word placed in the input buffer by an I/O device. Fail if the argument is 852 neither a Semaphore nor nil. 853 */ 854 int semaphore = popStack(); 855 success(semaphore == NilPointer || memory.fetchClassOf(semaphore) == ClassSemaphorePointer); 856 if (success()) 857 hal->set_input_semaphore(semaphore); 858 else 859 unPop(1); 860 } 861 862 void Interpreter::primitiveSampleInterval() 863 { 864 /* 865 Set the minimum time span between event driven mouse position 866 samples. The argument is a number of milliseconds. Fail if the argument 867 is not a SmallInteger. 868 */ 869 //Not actually used! 870 primitiveFail(); 871 872 } 873 874 void Interpreter::primitiveInputWord() 875 { 876 /* 877 Return the next word from the input buffer and remove the word from the 878 buffer. This message should be sent just after the input semaphore 879 finished a wait (was sent a signal by an I/O device). Fail if the input 880 buffer is empty. 881 */ 882 883 std::uint16_t word; 884 bool result = hal->next_input_word(&word); 885 if (result) 886 { 887 pop(1); // remove receiver 888 push(positive16BitIntegerFor(word)); 889 } 890 else 891 { 892 primitiveFail(); 893 } 894 } 895 896 897 // Return a oop to the display bitmap 898 // Reference may become invalid on the next cycle of the interpreter... 899 int Interpreter::getDisplayBits(int width, int height) 900 { 901 // Return oop of display bits object 902 // sanity checking display width/height 903 if (currentDisplay == 0) return 0; // No display yet 904 int displayBits = memory.fetchPointer_ofObject(BitsInForm, currentDisplay); 905 int computedSize = currentDisplayHeight * ((currentDisplayWidth + 15)/16); 906 int actualSize = memory.fetchWordLengthOf(displayBits); 907 // The sizes do not match... 908 // Smalltalk shrinks the display bitmap to a height of 100 behind our back 909 // when saving for example. 910 if (computedSize != actualSize) 911 return 0; 912 return displayBits; // ok then... 913 } 914 915 void Interpreter::updateDisplay(int destForm, int updatedX, int updatedY, int updatedWidth, int updatedHeight) 916 { 917 int width = fetchInteger_ofObject(WidthInForm, currentDisplay); 918 int height = fetchInteger_ofObject(HeightInForm, currentDisplay); 919 920 if (currentDisplayWidth != width || currentDisplayHeight != height) 921 { 922 // Display bits changed... possibly due to screen size change... 923 if (height <= 100) 924 return; // See note in primitiveBeDisplay... 925 hal->set_display_size(width, height); 926 currentDisplayWidth = width; 927 currentDisplayHeight = height; 928 } 929 930 if (updatedWidth > 0 && updatedHeight > 0) 931 hal->display_changed(updatedX, updatedY, updatedWidth, updatedHeight); 932 } 933 934 void Interpreter::primitiveCopyBits() 935 { 936 937 int bitBltPointer = stackTop(); 938 int destForm = memory.fetchPointer_ofObject(DestFormIndex, bitBltPointer); 939 int sourceForm = memory.fetchPointer_ofObject(SourceFormIndex, bitBltPointer); 940 int destX = fetchInteger_ofObject(DestXIndex, bitBltPointer); 941 int destY = fetchInteger_ofObject(DestYIndex, bitBltPointer); 942 int clipX = fetchInteger_ofObject(ClipXIndex, bitBltPointer); 943 int clipY = fetchInteger_ofObject(ClipYIndex, bitBltPointer); 944 int clipWidth = fetchInteger_ofObject(ClipWidthIndex, bitBltPointer); 945 int clipHeight = fetchInteger_ofObject(ClipHeightIndex, bitBltPointer); 946 int sourceX = fetchInteger_ofObject(SourceXIndex, bitBltPointer); 947 int sourceY = fetchInteger_ofObject(SourceYIndex, bitBltPointer); 948 int width = fetchInteger_ofObject(WidthIndex, bitBltPointer); 949 int height = fetchInteger_ofObject(HeightIndex, bitBltPointer); 950 int rule = fetchInteger_ofObject(CombinationRuleIndex, bitBltPointer); 951 952 success(between_and(rule, 0, 15)); 953 if (success()) 954 { 955 BitBlt bitBlt(memory, 956 destForm, 957 sourceForm, 958 memory.fetchPointer_ofObject(HalftoneFormIndex, bitBltPointer), 959 rule, 960 destX, 961 destY, 962 width, 963 height, 964 sourceX, 965 sourceY, 966 clipX, 967 clipY, 968 clipWidth, 969 clipHeight); 970 971 int updatedX, updatedY, updatedWidth, updatedHeight; 972 bitBlt.copyBits(); 973 if (destForm == currentDisplay) 974 { 975 bitBlt.getUpdatedBounds(&updatedX, &updatedY, &updatedWidth, &updatedHeight); 976 if (updatedWidth > 0 && updatedHeight > 0) 977 updateDisplay(destForm, updatedX, updatedY, updatedWidth, updatedHeight); 978 979 } 980 } 981 } 982 983 void Interpreter::primitiveSnapshot() 984 { 985 /* 986 The primitiveSnapshot routine writes the current state of the 987 object memory on a file of the same format as the Smalltalk-80 release file. 988 This file can be resumed in exactly the same way that the release file was 989 originally started. Note that the pointer of the active context at 990 the time of the primitive call must be stored in the active Process on the file. 991 (G&R) pg 651. 992 */ 993 994 int activeProcess = memory.fetchPointer_ofObject(ActiveProcessIndex, schedulerPointer()); 995 memory.storePointer_ofObject_withValue(SuspendedContextIndex, activeProcess, activeContext); 996 storeContextRegisters(); 997 998 memory.garbageCollect(); 999 memory.saveSnapshot(fileSystem, hal->get_image_name()); 1000 1001 /* This is poorly documented by the Bluebook. There is an actual return value that is important. 1002 see snapshotAs:thenQuit: in the Smalltalk sources. When the system resumes a snapshot the interpreter will be 1003 back at the point of the save, and 'self' will be at the top of the stack. But after saving we 1004 return nil to tell the caller that we just wrote a snapshot. This is how we can distinguish 1005 the case were we save a session (to be continued potentially followed by an exit) or we are 1006 resuming a saved session. 1007 */ 1008 pop(1); // DO NOT POP BEFORE WRITING IMAGE. 1009 push(NilPointer); // return of nil signals we just saved 1010 } 1011 1012 void Interpreter::primitiveTimeWordsInto() 1013 { 1014 /* 1015 The argument is a byte indexable object of length at least four. Store 1016 into the first four bytes of the argument the number of seconds since 1017 00:00 in the morning of January 1, 1901 (a 32-bit unsigned number). 1018 The low-order 8-bits are stored in the byte indexed by 1 and the high- 1019 order 8-bits in the byte indexed 4. 1020 */ 1021 std::uint32_t time = hal->get_smalltalk_epoch_time(); 1022 int byteIndexObject = popStack(); 1023 memory.storeByte_ofObject_withValue(0, byteIndexObject, time & 0xff); 1024 memory.storeByte_ofObject_withValue(1, byteIndexObject, (time >> 8) & 0xff); 1025 memory.storeByte_ofObject_withValue(2, byteIndexObject, (time >> 16) & 0xff); 1026 memory.storeByte_ofObject_withValue(3, byteIndexObject, (time >> 24) & 0xff); 1027 } 1028 1029 void Interpreter::primitiveTickWordsInto() 1030 { 1031 /* 1032 The argument is a byte indexable object of length at least four (a 1033 LargePositiveInteger). Store into the first four bytes of the argument the 1034 number of milliseconds since the millisecond clock was last reset or rolled 1035 over (a 32-bit unsigned number). The low-order 8-bits are stored in 1036 the byte indexed by 1 and the high-order 8-bits in the byte indexed 4. 1037 */ 1038 std::uint32_t time = hal->get_msclock(); 1039 int byteIndexObject = popStack(); 1040 memory.storeByte_ofObject_withValue(0, byteIndexObject, time & 0xff); 1041 memory.storeByte_ofObject_withValue(1, byteIndexObject, (time >> 8) & 0xff); 1042 memory.storeByte_ofObject_withValue(2, byteIndexObject, (time >> 16) & 0xff); 1043 memory.storeByte_ofObject_withValue(3, byteIndexObject, (time >> 24) & 0xff); 1044 } 1045 1046 void Interpreter::primitiveSignalAtTick() 1047 { 1048 /* 1049 1050 1051 The primitiveSignalAtTick routine is associated with the signal:atTick: 1052 messages in ProcessorScheduler. his message takes a Semaphore as the first 1053 argument and a byte indexable object of at least four bytes as the second 1054 argument. The first four bytes of the second argument are interpreted as an 1055 unsigned 32-bit integer of the type stored by the primitiveTickWordsInto 1056 routine. The interpreter should signal the Semaphore argument when the 1057 millisecond clock reaches the value specified by the second argument. 1058 If the specified time has passed,the Semaphore is signaled immediately. 1059 This primitive signals the last Semaphore to be passed to it. If a new 1060 call is made on it before the last timer value has been reached the last 1061 Semaphore will not be signaled. If the first argument is not a Semaphore 1062 any currently waiting Semaphore will be forgotten. (pg. 652 G&R) 1063 1064 Signal the semaphore when the millisecond clock reaches the value of 1065 the second argument. The second argument is a byte indexable object at 1066 least four bytes long (a 32-bit unsigned number with the low order 1067 8-bits stored in the byte with the lowest index). Fail if the first 1068 argument is neither a Semaphore nor nil. 1069 1070 1071 */ 1072 1073 int timePointer = popStack(); 1074 int semaphore = popStack(); 1075 1076 success(semaphore == NilPointer || memory.fetchClassOf(semaphore) == ClassSemaphorePointer); 1077 if (success()) 1078 { 1079 if (semaphore == NilPointer) 1080 semaphore = 0; // Tells HAL to cancel outstanding request 1081 1082 std::uint32_t time = 1083 memory.fetchByte_ofObject(0, timePointer) | 1084 (memory.fetchByte_ofObject(1, timePointer) << 8) | 1085 (memory.fetchByte_ofObject(2, timePointer) << 16) | 1086 (memory.fetchByte_ofObject(3, timePointer) << 24); 1087 1088 hal->signal_at(semaphore, time); 1089 } 1090 else 1091 unPop(2); 1092 } 1093 1094 void Interpreter::primitiveBeCursor() 1095 { 1096 /* 1097 Tell the interpreter to use the receiver as the current cursor image. Fail if the 1098 receiver does not match the size expected by the hardware. 1099 */ 1100 if (currentCursor != stackTop()) 1101 { 1102 currentCursor = stackTop(); 1103 int cursorBitmap = memory.fetchPointer_ofObject(BitsInForm, currentCursor); 1104 std::uint16_t bits[16]; 1105 for(int i = 0; i < 16; i++) 1106 { 1107 bits[i] = memory.fetchWord_ofObject(i, cursorBitmap); 1108 } 1109 hal->set_cursor_image(bits); 1110 } 1111 1112 } 1113 1114 void Interpreter::primitiveBeDisplay() 1115 { 1116 /* 1117 Tell the interpreter to use the receiver as the current display image. Fail if the 1118 form is too wide to fit on the physical display 1119 */ 1120 int newDisplay = stackTop(); 1121 if (currentDisplay != newDisplay) 1122 { 1123 int width = fetchInteger_ofObject(WidthInForm, newDisplay); 1124 int height = fetchInteger_ofObject(HeightInForm, newDisplay); 1125 // Intersting fact: In order to save space when writing the object image 1126 // Smalltalk resizes the display to the current width x 100 just before saving. 1127 // When the system resumes, it will be resized to the last actual size. 1128 // Here we ignore the change to the height of 100. 1129 if (height > 100) 1130 { 1131 if (!hal->set_display_size(width, height)) 1132 { 1133 primitiveFail(); 1134 } 1135 currentDisplay = newDisplay; 1136 currentDisplayWidth = width; 1137 currentDisplayHeight = height; 1138 } 1139 else 1140 currentDisplay = 0; 1141 } 1142 } 1143 1144 // scanCharactersFrom: startIndex to: stopIndex in: sourceString rightX: rightX stopConditions: stops displaying: display 1145 1146 void Interpreter::primitiveScanCharacters() 1147 { 1148 #ifdef IMPLEMENT_PRIMITIVE_SCANCHARS 1149 bool displaying = popStack() == TruePointer; 1150 int stops = popStack(); 1151 int rightX = popInteger(); 1152 int sourceString = popStack(); 1153 int stopIndex = popInteger(); 1154 int startIndex = popInteger(); 1155 int scannerPointer = popStack(); 1156 int destForm = memory.fetchPointer_ofObject(DestFormIndex, scannerPointer); 1157 int sourceForm = memory.fetchPointer_ofObject(SourceFormIndex, scannerPointer); 1158 int destX = fetchInteger_ofObject(DestXIndex, scannerPointer); 1159 int destY; 1160 int clipX; 1161 int clipY; 1162 int clipWidth; 1163 int clipHeight; 1164 int sourceX = 0; // uninitialized... assigned during scanning 1165 int sourceY; 1166 int rule; 1167 int width = 0; 1168 int height; 1169 int xTable = memory.fetchPointer_ofObject(XTableIndexIndex, scannerPointer); 1170 int lastIndex = fetchInteger_ofObject(LastIndexIndex, scannerPointer); 1171 int stopConditions = memory.fetchPointer_ofObject(StopConditionsIndex, scannerPointer); 1172 1173 // If not displaying, some of the fields are nil, so only 1174 // fetch integers that are actually used... 1175 if (displaying) 1176 { 1177 destY = fetchInteger_ofObject(DestYIndex, scannerPointer); 1178 clipX = fetchInteger_ofObject(ClipXIndex, scannerPointer); 1179 clipY = fetchInteger_ofObject(ClipYIndex, scannerPointer); 1180 clipWidth = fetchInteger_ofObject(ClipWidthIndex, scannerPointer); 1181 clipHeight = fetchInteger_ofObject(ClipHeightIndex, scannerPointer); 1182 sourceY = fetchInteger_ofObject(SourceYIndex, scannerPointer); 1183 rule = fetchInteger_ofObject(CombinationRuleIndex, scannerPointer); 1184 height = fetchInteger_ofObject(HeightIndex, scannerPointer); 1185 } 1186 else 1187 { 1188 destY = 0; 1189 clipX = 0; 1190 clipY = 0; 1191 clipWidth = 0; 1192 clipHeight = 0; 1193 sourceY = 0; 1194 rule = 0; 1195 height = 0; 1196 } 1197 1198 if (success()) 1199 { 1200 1201 CharacterScanner scanner(memory, 1202 destForm, 1203 sourceForm, 1204 memory.fetchPointer_ofObject(HalftoneFormIndex, scannerPointer), 1205 rule, 1206 destX, 1207 destY, 1208 width, 1209 height, 1210 sourceX, 1211 sourceY, 1212 clipX, 1213 clipY, 1214 clipWidth, 1215 clipHeight, 1216 xTable, 1217 lastIndex, 1218 stopConditions 1219 ); 1220 1221 1222 int result = scanner.scanCharactersFrom_to_in_rightX_stopConditions_displaying(startIndex, stopIndex, sourceString, rightX, stops, displaying); 1223 /* 1224 Need to pull out the following modified values and store them back into the scanner: 1225 destX, width, sourceX in BitBlt fields 1226 lastIndex in CharacterScanner 1227 */ 1228 1229 storeInteger_ofObject_withValue(DestXIndex, scannerPointer, scanner.updateDestX()); 1230 storeInteger_ofObject_withValue(WidthIndex, scannerPointer, scanner.updatedWidth()); 1231 storeInteger_ofObject_withValue(SourceXIndex, scannerPointer, scanner.updatedSourceX()); 1232 storeInteger_ofObject_withValue(LastIndexIndex, scannerPointer, scanner.updatedLastIndex()); 1233 1234 if (destForm == currentDisplay && displaying) 1235 { 1236 updateDisplay(destForm, clipX, clipY, clipWidth, clipHeight); 1237 } 1238 push(result); 1239 } 1240 else 1241 { 1242 unPop(7); 1243 } 1244 #else 1245 primitiveFail(); 1246 #endif 1247 1248 } 1249 1250 void Interpreter::primitiveDrawLoop() 1251 { 1252 // optional 1253 primitiveFail(); 1254 } 1255 1256 void Interpreter::primitiveStringReplace() 1257 { 1258 // optional 1259 primitiveFail(); 1260 1261 } 1262 1263 // lookupMethodInDictionary: 1264 bool Interpreter::lookupMethodInDictionary(int dictionary) 1265 { 1266 int length; 1267 int index; 1268 int mask; 1269 bool wrapAround; 1270 int nextSelector; 1271 int methodArray; 1272 1273 /* "source" 1274 length <- memory fetchWordLengthOf: dictionary. 1275 mask <- length - SelectorStart - 1. 1276 index <- (mask bitAnd: (self hash: messageSelector)) + SelectorStart. 1277 wrapAround <- false. 1278 [true] whileTrue: 1279 [nextSelector <- memory fetchPointer: index 1280 ofObject: dictionary. 1281 nextSelector=NilPointer ifTrue: [^false]. 1282 nextSelector=messageSelector 1283 ifTrue: [methodArray <- memory fetchPointer: MethodArrayIndex 1284 ofObject: dictionary. 1285 newMethod <- memory fetchPointer: index - SelectorStart 1286 ofObject: methodArray. 1287 primitiveIndex <- self primitiveIndexOf: newMethod. 1288 ^true]. 1289 index <- index + 1. 1290 index = length 1291 ifTrue: [wrapAround ifTrue: [^false]. 1292 wrapAround <- true. 1293 index <- SelectorStart]] 1294 */ 1295 length = memory.fetchWordLengthOf(dictionary); 1296 mask = length - SelectorStart - 1; 1297 index = (mask & hash(messageSelector)) + SelectorStart; 1298 wrapAround = false; 1299 for(;;) 1300 { 1301 nextSelector = memory.fetchPointer_ofObject(index, dictionary); 1302 if (nextSelector == NilPointer) return false; 1303 if (nextSelector == messageSelector) 1304 { 1305 methodArray = memory.fetchPointer_ofObject(MethodArrayIndex, dictionary); 1306 newMethod = memory.fetchPointer_ofObject(index - SelectorStart, methodArray); 1307 primitiveIndex = primitiveIndexOf(newMethod); 1308 return true; 1309 } 1310 index = index + 1; 1311 if (index == length) 1312 { 1313 if (wrapAround) return false; 1314 wrapAround = true; 1315 index = SelectorStart; 1316 } 1317 } 1318 } 1319 1320 // createActualMessage 1321 void Interpreter::createActualMessage() 1322 { 1323 int argumentArray; 1324 int message; 1325 1326 /* "source" 1327 argumentArray <- memory instantiateClass: ClassArrayPointer 1328 withPointers: argumentCount. 1329 "ERROR: messageSize misspelled, self inserted" 1330 message <- memory instantiateClass: ClassMessagePointer 1331 withPointers: MessageSize. 1332 memory storePointer: MessageSelectorIndex 1333 ofObject: message 1334 withValue: messageSelector. 1335 memory storePointer: MessageArgumentsIndex 1336 ofObject: message 1337 withValue: argumentArray. 1338 self transfer: argumentCount 1339 "ERROR in selector in next line and next but one after" 1340 fromIndex: stackPointer - (argumentCount - 1) 1341 ofObject: activeContext 1342 toIndex: 0 1343 ofObject: argumentArray. 1344 self pop: argumentCount. 1345 self push: message. 1346 argumentCount <- 1 1347 */ 1348 1349 argumentArray = memory.instantiateClass_withPointers(ClassArrayPointer, argumentCount); 1350 message = memory.instantiateClass_withPointers(ClassMessagePointer, MessageSize); 1351 memory.storePointer_ofObject_withValue(MessageSelectorIndex, message, messageSelector); 1352 memory.storePointer_ofObject_withValue(MessageArgumentsIndex, message, argumentArray); 1353 transfer_fromIndex_ofObject_toIndex_ofObject(argumentCount, 1354 stackPointer - (argumentCount - 1), 1355 activeContext, 1356 0, 1357 argumentArray); 1358 1359 pop(argumentCount); 1360 push(message); 1361 argumentCount = 1; 1362 } 1363 1364 1365 // lookupMethodInClass: 1366 bool Interpreter::lookupMethodInClass(int cls) 1367 { 1368 int currentClass; 1369 int dictionary; 1370 1371 /* "source" 1372 currentClass <- class. 1373 [currentClass~=NilPointer] whileTrue: 1374 [dictionary <- memory fetchPointer: MessageDictionaryIndex 1375 ofObject: currentClass. 1376 (self lookupMethodInDictionary: dictionary) 1377 ifTrue: [^true]. 1378 currentClass <- self superclassOf: currentClass]. 1379 messageSelector = DoesNotUnderstandSelector. 1380 ifTrue: [self error: 'Recursive not understood error encountered']. 1381 self createActualMessage. 1382 messageSelector <- DoesNotUnderstandSelector. 1383 ^self lookupMethodInClass: class 1384 */ 1385 1386 currentClass = cls; 1387 while (currentClass != NilPointer) 1388 { 1389 dictionary = memory.fetchPointer_ofObject(MessageDictionaryIndex, currentClass); 1390 if (lookupMethodInDictionary(dictionary)) return true; 1391 currentClass = superclassOf(currentClass); 1392 } 1393 if (messageSelector == DoesNotUnderstandSelector) 1394 error("Recursive not understood error encountered'"); 1395 createActualMessage(); 1396 messageSelector = DoesNotUnderstandSelector; 1397 return lookupMethodInClass(cls); 1398 } 1399 1400 1401 // returnBytecode 1402 void Interpreter::returnBytecode() 1403 { 1404 /* "source" 1405 currentBytecode = 120 1406 ifTrue: [^self returnValue: receiver 1407 to: self sender]. 1408 currentBytecode = 121 1409 ifTrue: [^self returnValue: TruePointer 1410 to: self sender]. 1411 currentBytecode = 122 1412 ifTrue: [^self returnValue: FalsePointer 1413 to: self sender]. 1414 currentBytecode = 123 1415 ifTrue: [^self returnValue: NilPointer 1416 to: self sender]. 1417 currentBytecode = 124 1418 ifTrue: [^self returnValue: self popStack 1419 to: self sender]. 1420 currentBytecode = 125 1421 ifTrue: [^self returnValue: self popStack 1422 to: self caller] 1423 */ 1424 switch (currentBytecode) 1425 { 1426 case 120: returnValue_to(receiver, sender()); break; 1427 case 121: returnValue_to(TruePointer, sender()); break; 1428 case 122: returnValue_to(FalsePointer, sender()); break; 1429 case 123: returnValue_to(NilPointer, sender()); break; 1430 case 124: returnValue_to(popStack(), sender()); break; 1431 case 125: returnValue_to(popStack(), caller()); break; 1432 } 1433 } 1434 1435 1436 // nilContextFields 1437 void Interpreter::nilContextFields() 1438 { 1439 /* "source" 1440 memory storePointer: SenderIndex 1441 ofObject: activeContext 1442 withValue: NilPointer. 1443 memory storePointer: InstructionPointerIndex 1444 ofObject: activeContext 1445 withValue: NilPointer 1446 */ 1447 1448 memory.storePointer_ofObject_withValue(SenderIndex, activeContext, NilPointer); 1449 memory.storePointer_ofObject_withValue(InstructionPointerIndex, activeContext, NilPointer); 1450 1451 } 1452 1453 // returnToActiveContext: 1454 void Interpreter::returnToActiveContext(int aContext) 1455 { 1456 /* "source" 1457 memory increaseReferencesTo: aContext. 1458 self nilContextFields. 1459 memory decreaseReferencesTo: activeContext. 1460 activeContext <- aContext. 1461 self fetchContextRegisters 1462 */ 1463 1464 memory.increaseReferencesTo(aContext); 1465 nilContextFields(); 1466 memory.decreaseReferencesTo(activeContext); 1467 activeContext = aContext; 1468 fetchContextRegisters(); 1469 } 1470 1471 1472 // returnValue:to: 1473 void Interpreter::returnValue_to(int resultPointer, int contextPointer) 1474 { 1475 int sendersIP; 1476 1477 /* "source" 1478 contextPointer = NilPointer 1479 ifTrue: [self push: activeContext. 1480 self push: resultPointer. 1481 ^self sendSelector: CannotReturnSelector 1482 argumentCount: 1]. 1483 sendersIP <- memory fetchPointer: InstructionPointerIndex 1484 ofObject: contextPointer. 1485 sendersIP = NilPointer 1486 ifTrue: [self push: activeContext. 1487 self push: resultPointer. 1488 ^self sendSelector: CannotReturnSelector 1489 argumentCount: 1]. 1490 memory increaseReferencesTo: resultPointer. 1491 self returnToActiveContext: contextPointer. 1492 self push: resultPointer. 1493 memory decreaseReferencesTo: resultPointer 1494 */ 1495 if (contextPointer == NilPointer) 1496 { 1497 push(activeContext); 1498 push(resultPointer); 1499 sendSelector_argumentCount(CannotReturnSelector, 1); 1500 return; 1501 } 1502 sendersIP = memory.fetchPointer_ofObject(InstructionPointerIndex, contextPointer); 1503 if (sendersIP == NilPointer) 1504 { 1505 push(activeContext); 1506 push(resultPointer); 1507 sendSelector_argumentCount(CannotReturnSelector, 1); 1508 return; 1509 } 1510 memory.increaseReferencesTo(resultPointer); 1511 returnToActiveContext(contextPointer); 1512 push(resultPointer); 1513 memory.decreaseReferencesTo(resultPointer); 1514 } 1515 1516 1517 // synchronousSignal: 1518 void Interpreter::synchronousSignal(int aSemaphore) 1519 { 1520 int excessSignals; 1521 1522 /* "source" 1523 (self isEmptyList: aSemaphore) 1524 ifTrue: [excessSignals <- self fetchInteger: ExcessSignalsIndex 1525 ofObject: aSemaphore. 1526 self storeInteger: ExcessSignalsIndex 1527 ofObject: aSemaphore 1528 withValue: excessSignals + 1] 1529 ifFalse: [ "dbanay: ref counting issue: removeFirstLinkOfList" 1530 aProcess <- self removeFirstLinkOfList: aSemaphore. 1531 self resume: aProcess. 1532 memory decreaseReferencesTo: aProcess] 1533 */ 1534 1535 if (isEmptyList(aSemaphore)) 1536 { 1537 excessSignals = fetchInteger_ofObject(ExcessSignalsIndex, aSemaphore); 1538 storeInteger_ofObject_withValue(ExcessSignalsIndex, aSemaphore, excessSignals + 1); 1539 } 1540 else 1541 { 1542 // removeFirstLinkOfList returns pointer that must be released 1543 int aProcess = removeFirstLinkOfList(aSemaphore); 1544 resume(aProcess); 1545 memory.decreaseReferencesTo(aProcess); 1546 } 1547 } 1548 1549 1550 // primitiveBlockCopy 1551 void Interpreter::primitiveBlockCopy() 1552 { 1553 int context; 1554 int methodContext; 1555 int blockArgumentCount; 1556 int newContext; 1557 int initialIP; 1558 int contextSize; 1559 1560 1561 /* "source" 1562 blockArgumentCount <- self popStack. 1563 context <- self popStack. 1564 (self isBlockContext: context) 1565 ifTrue: [methodContext <- memory fetchPointer: HomeIndex 1566 ofObject: context] 1567 ifFalse: [methodContext <- context]. 1568 contextSize <- memory fetchWordLengthOf: methodContext. 1569 newContext <- memory instantiateClass: ClassBlockContextPointer 1570 withPointers: contextSize. 1571 initialIP <- memory integerObjectOf: instructionPointer + 3. 1572 memory storePointer: InitialIPIndex 1573 ofObject: newContext 1574 withValue: initialIP. 1575 memory storePointer: InstructionPointerIndex 1576 ofObject: newContext 1577 withValue: initialIP. 1578 self storeStackPointerValue: 0 1579 inContext: newContext. 1580 memory storePointer: BlockArgumentCountIndex 1581 ofObject: newContext 1582 withValue: blockArgumentCount. 1583 memory storePointer: HomeIndex 1584 ofObject: newContext 1585 withValue: methodContext. 1586 self push: newContext 1587 */ 1588 1589 blockArgumentCount = popStack(); 1590 context = popStack(); 1591 if (isBlockContext(context)) 1592 methodContext = memory.fetchPointer_ofObject(HomeIndex, context); 1593 else 1594 methodContext = context; 1595 contextSize = memory.fetchWordLengthOf(methodContext); 1596 newContext = memory.instantiateClass_withPointers(ClassBlockContextPointer, contextSize); 1597 initialIP = memory.integerObjectOf(instructionPointer + 3); 1598 memory.storePointer_ofObject_withValue(InitialIPIndex, newContext, initialIP); 1599 memory.storePointer_ofObject_withValue(InstructionPointerIndex, newContext, initialIP); 1600 storeStackPointerValue_inContext(0, newContext); 1601 memory.storePointer_ofObject_withValue(BlockArgumentCountIndex, newContext, blockArgumentCount); 1602 memory.storePointer_ofObject_withValue(HomeIndex, newContext, methodContext); 1603 push(newContext); 1604 } 1605 1606 1607 // primitiveResume 1608 void Interpreter::primitiveResume() 1609 { 1610 /* "source" 1611 self resume: self stackTop 1612 */ 1613 resume(stackTop()); 1614 } 1615 1616 1617 // primitivePerformWithArgs 1618 void Interpreter::primitivePerformWithArgs() 1619 { 1620 int thisReceiver; 1621 int performSelector; 1622 int argumentArray; 1623 int arrayClass; 1624 int arraySize; 1625 int index; 1626 1627 /* "source" 1628 argumentArray <- self popStack. 1629 arraySize <- memory fetchWordLengthOf: argumentArray. 1630 arrayClass <- memory fetchClassOf: argumentArray. 1631 self success: (stackPointer + arraySize) 1632 < (memory fetchWordLengthOf: activeContext). 1633 self success: (arrayClass = ClassArrayPointer). 1634 self success 1635 ifTrue: [performSelector <- messageSelector. 1636 messageSelector <- self popStack. 1637 thisReceiver <- self stackTop. 1638 argumentCount <- arraySize. 1639 index <- 1. 1640 [index <= argumentCount] 1641 whileTrue: [self push: (memory fetchPointer: index - 1 1642 ofObject: argumentArray). 1643 index <- index + 1]. 1644 self lookupMethodInClass: 1645 (memory fetchClassOf: thisReceiver). 1646 self success: (self argumentCountOf: newMethod) 1647 = argumentCount. 1648 self success 1649 ifTrue: [self executeNewMethod] 1650 ifFalse: [self unPop: argumentCount. 1651 self push: messageSelector. 1652 self push: argumentArray. 1653 argumentCount <- 2. 1654 messageSelector <- performSelector]] 1655 ifFalse: [self unPop: 1] 1656 */ 1657 1658 argumentArray = popStack(); 1659 arraySize = memory.fetchWordLengthOf(argumentArray); 1660 arrayClass = memory.fetchClassOf(argumentArray); 1661 success((stackPointer + arraySize) 1662 < memory.fetchWordLengthOf(activeContext)); 1663 success(arrayClass == ClassArrayPointer); 1664 if (success()) 1665 { 1666 performSelector = messageSelector; 1667 messageSelector = popStack(); 1668 thisReceiver = stackTop(); 1669 argumentCount = arraySize; 1670 index = 1; 1671 while (index <= argumentCount) 1672 { 1673 push(memory.fetchPointer_ofObject(index - 1, argumentArray)); 1674 index = index + 1; 1675 } 1676 lookupMethodInClass(memory.fetchClassOf(thisReceiver)); 1677 success(argumentCountOf(newMethod) == argumentCount); 1678 if (success()) 1679 executeNewMethod(); 1680 else 1681 { 1682 unPop(argumentCount); 1683 push(messageSelector); 1684 push(argumentArray); 1685 argumentCount = 2; 1686 messageSelector = performSelector; 1687 } 1688 } 1689 else 1690 unPop(1); 1691 1692 } 1693 1694 1695 // wakeHighestPriority 1696 //NOTE: Returns a referenced increased pointer that must be decreased by caller 1697 int Interpreter::wakeHighestPriority() 1698 { 1699 int priority; 1700 int processLists; 1701 int processList; 1702 1703 /* "source" 1704 processLists <- memory fetchPointer: ProcessListsIndex 1705 ofObject: self schedulerPointer. 1706 priority <- memory fetchWordLengthOf: processLists. 1707 [processList <- memory fetchPointer: priority - 1 1708 ofObject: processLists. 1709 self isEmptyList: processList] whileTrue: [priority <- priority - 1]. 1710 ^self removeFirstLinkOfList: processList 1711 */ 1712 1713 processLists = memory.fetchPointer_ofObject(ProcessListsIndex, schedulerPointer()); 1714 priority = memory.fetchWordLengthOf(processLists); 1715 for(;;) 1716 { 1717 assert(priority > 0); 1718 processList = memory.fetchPointer_ofObject(priority - 1, processLists); 1719 if (!isEmptyList(processList)) break; 1720 priority = priority - 1; 1721 } 1722 1723 return removeFirstLinkOfList(processList); 1724 } 1725 1726 1727 // primitivePerform 1728 void Interpreter::primitivePerform() 1729 { 1730 int performSelector; 1731 int newReceiver; 1732 int selectorIndex; 1733 1734 1735 /* "source" 1736 performSelector <- messageSelector. 1737 messageSelector <- self stackValue: argumentCount - 1. 1738 newReceiver <- self stackValue: argumentCount. 1739 self lookupMethodInClass: (memory fetchClassOf: newReceiver). 1740 self success: (self argumentCountOf: newMethod) = (argumentCount - 1). 1741 self success 1742 ifTrue: [selectorIndex <- stackPointer - argumentCount + 1. 1743 self transfer: argumentCount - 1 1744 fromIndex: selectorIndex + 1 1745 ofObject: activeContext 1746 toIndex: selectorIndex 1747 ofObject: activeContext. 1748 self pop: 1. 1749 argumentCount <- argumentCount - 1. 1750 self executeNewMethod] 1751 ifFalse: [messageSelector <- performSelector] 1752 */ 1753 1754 performSelector = messageSelector; 1755 messageSelector = stackValue(argumentCount - 1); 1756 newReceiver = stackValue(argumentCount); 1757 lookupMethodInClass(memory.fetchClassOf(newReceiver)); 1758 success(argumentCountOf(newMethod) == argumentCount - 1); 1759 if (success()) 1760 { 1761 selectorIndex = stackPointer - argumentCount + 1; 1762 transfer_fromIndex_ofObject_toIndex_ofObject( argumentCount - 1, 1763 selectorIndex + 1, 1764 activeContext, 1765 selectorIndex, 1766 activeContext); 1767 pop(1); 1768 argumentCount = argumentCount - 1; 1769 executeNewMethod(); 1770 } 1771 else 1772 { 1773 messageSelector = performSelector; 1774 } 1775 } 1776 1777 1778 // primitiveValueWithArgs 1779 void Interpreter::primitiveValueWithArgs() 1780 { 1781 int argumentArray; 1782 int blockContext; 1783 int blockArgumentCount; 1784 int arrayClass; 1785 int arrayArgumentCount = 0; 1786 int initialIP; 1787 1788 /* "source" 1789 argumentArray <- self popStack. 1790 blockContext <- self popStack. 1791 blockArgumentCount <- self argumentCountOfBlock: blockContext. 1792 arrayClass <- memory fetchClassOf: argumentArray. 1793 self success: (arrayClass = ClassArrayPointer). 1794 self success 1795 ifTrue: [arrayArgumentCount <- memory fetchWordLengthOf: argumentArray. 1796 self success: arrayArgumentCount = blockArgumentCount]. 1797 self success 1798 ifTrue: [self transfer: arrayArgumentCount 1799 fromIndex: 0 1800 ofObject: argumentArray 1801 toIndex: TempFrameStart 1802 ofObject: blockContext. 1803 initialIP <- memory fetchPointer: InitialIPIndex 1804 ofObject: blockContext. 1805 memory storePointer: InstructionPointerIndex 1806 ofObject: blockContext 1807 withValue: initialIP. 1808 self storeStackPointerValue: arrayArgumentCount 1809 inContext: blockContext. 1810 memory storePointer: CallerIndex 1811 ofObject: blockContext 1812 withValue: activeContext. 1813 self newActiveContext: blockContext] 1814 ifFalse: [self unPop: 2] 1815 */ 1816 1817 argumentArray = popStack(); 1818 blockContext = popStack(); 1819 blockArgumentCount = argumentCountOfBlock(blockContext); 1820 arrayClass = memory.fetchClassOf(argumentArray); 1821 success(arrayClass == ClassArrayPointer); 1822 if (success()) 1823 { 1824 arrayArgumentCount = memory.fetchWordLengthOf(argumentArray); 1825 success(arrayArgumentCount == blockArgumentCount); 1826 } 1827 if (success()) 1828 { 1829 transfer_fromIndex_ofObject_toIndex_ofObject(arrayArgumentCount, 1830 0, 1831 argumentArray, 1832 TempFrameStart, 1833 blockContext); 1834 initialIP = memory.fetchPointer_ofObject(InitialIPIndex, blockContext); 1835 memory.storePointer_ofObject_withValue(InstructionPointerIndex, blockContext, initialIP); 1836 storeStackPointerValue_inContext(arrayArgumentCount, blockContext); 1837 memory.storePointer_ofObject_withValue(CallerIndex, blockContext, activeContext); 1838 newActiveContext(blockContext); 1839 } 1840 else 1841 unPop(2); 1842 } 1843 1844 1845 // removeFirstLinkOfList: 1846 int Interpreter::removeFirstLinkOfList(int aLinkedList) 1847 { 1848 int firstLink; 1849 int lastLink; 1850 int nextLink; 1851 1852 /* "source" 1853 firstLink <- memory fetchPointer: FirstLinkIndex 1854 ofObject: aLinkedList. 1855 memory increaseReferencesTo: firstLink. "dbanay: protect it in case lastLink = firstLink" 1856 lastLink <- memory fetchPointer: LastLinkIndex 1857 ofObject: aLinkedList. 1858 lastLink = firstLink 1859 ifTrue: [memory storePointer: FirstLinkIndex 1860 ofObject: aLinkedList 1861 withValue: NilPointer. 1862 memory storePointer: LastLinkIndex 1863 ofObject: aLinkedList 1864 withValue: NilPointer] 1865 ifFalse: [nextLink <- memory fetchPointer: NextLinkIndex 1866 ofObject: firstLink. 1867 memory storePointer: FirstLinkIndex 1868 ofObject: aLinkedList 1869 withValue: nextLink]. 1870 memory storePointer: NextLinkIndex 1871 ofObject: firstLink 1872 withValue: NilPointer. 1873 ^firstLink 1874 */ 1875 /* 1876 The routines listed here ignore the reference-counting problem in the interest of clarity. (pg. 644 G&R). 1877 Found and fixed. -dbanay 1878 */ 1879 1880 firstLink = memory.fetchPointer_ofObject(FirstLinkIndex, aLinkedList); 1881 1882 // When the link list is updated the references to fistLink 1883 // will drop to zero. So, we need to return an increased pointer that 1884 // must be decreased by the caller. 1885 // Calls: 1886 // suspendActive->wakeHighestPriority->removeFirstLinkOfList 1887 // synchronousSignal->removeFirstLinkOfList 1888 memory.increaseReferencesTo(firstLink); 1889 1890 lastLink = memory.fetchPointer_ofObject(LastLinkIndex, aLinkedList); 1891 if (lastLink == firstLink) 1892 { 1893 memory.storePointer_ofObject_withValue(FirstLinkIndex, aLinkedList, NilPointer); 1894 memory.storePointer_ofObject_withValue(LastLinkIndex, aLinkedList, NilPointer); 1895 } 1896 else 1897 { 1898 nextLink = memory.fetchPointer_ofObject(NextLinkIndex, firstLink); 1899 memory.storePointer_ofObject_withValue(FirstLinkIndex, aLinkedList, nextLink); 1900 } 1901 memory.storePointer_ofObject_withValue(NextLinkIndex, firstLink, NilPointer); 1902 return firstLink; 1903 } 1904 1905 1906 // addLastLink:toList: 1907 void Interpreter::addLastLink_toList(int aLink, int aLinkedList) 1908 { 1909 int lastLink; 1910 1911 /* "source" 1912 (self isEmptyList: aLinkedList) 1913 ifTrue: [memory storePointer: FirstLinkIndex 1914 ofObject: aLinkedList 1915 withValue: aLink] 1916 ifFalse: [lastLink <- memory fetchPointer: LastLinkIndex 1917 ofObject: aLinkedList. 1918 memory storePointer: NextLinkIndex 1919 ofObject: lastLink 1920 withValue: aLink]. 1921 memory storePointer: LastLinkIndex 1922 ofObject: aLinkedList 1923 withValue: aLink. 1924 memory storePointer: MyListIndex 1925 ofObject: aLink 1926 withValue: aLinkedList 1927 */ 1928 1929 1930 if (isEmptyList(aLinkedList)) 1931 { 1932 memory.storePointer_ofObject_withValue(FirstLinkIndex, aLinkedList, aLink); 1933 } 1934 else 1935 { 1936 lastLink = memory.fetchPointer_ofObject(LastLinkIndex, aLinkedList); 1937 memory.storePointer_ofObject_withValue(NextLinkIndex, lastLink, aLink); 1938 } 1939 memory.storePointer_ofObject_withValue(LastLinkIndex, aLinkedList, aLink); 1940 memory.storePointer_ofObject_withValue(MyListIndex, aLink, aLinkedList); 1941 1942 } 1943 1944 // primitiveWait 1945 void Interpreter::primitiveWait() 1946 { 1947 int thisReceiver; 1948 int excessSignals; 1949 1950 /* "source" 1951 thisReceiver <- self stackTop. 1952 excessSignals <- self fetchInteger: ExcessSignalsIndex 1953 ofObject: thisReceiver. 1954 excessSignals > 0 1955 ifTrue: [self storeInteger: ExcessSignalsIndex 1956 ofObject: thisReceiver 1957 withValue: excessSignals - 1] 1958 ifFalse: [self addLastLink: self activeProcess 1959 toList: thisReceiver. 1960 self suspendActive] 1961 */ 1962 1963 thisReceiver = stackTop(); 1964 excessSignals = fetchInteger_ofObject(ExcessSignalsIndex, thisReceiver); 1965 if (excessSignals > 0) 1966 { 1967 storeInteger_ofObject_withValue(ExcessSignalsIndex, thisReceiver, excessSignals - 1); 1968 } 1969 else 1970 { 1971 addLastLink_toList(activeProcess(), thisReceiver); 1972 suspendActive(); 1973 } 1974 } 1975 1976 1977 // primitiveFlushCache 1978 void Interpreter::primitiveFlushCache() 1979 { 1980 /* "source" 1981 self initializeMethodCache 1982 */ 1983 initializeMethodCache(); 1984 } 1985 1986 1987 // suspendActive 1988 void Interpreter::suspendActive() 1989 { 1990 /* "source" 1991 "dbanay: ref counting issue - wakeHighestPriority" 1992 aProcess = self wakeHighestPriority. 1993 self transferTo: self wakeHighestPriority. 1994 memory decreaseReferencesTo: aProcess 1995 */ 1996 1997 //Note: wakeHighestPriority returns a pointer that must be decreased by caller 1998 int aProcess = wakeHighestPriority(); 1999 transferTo(aProcess); 2000 memory.decreaseReferencesTo(aProcess); 2001 } 2002 2003 2004 // activeProcess 2005 int Interpreter::activeProcess() 2006 { 2007 /* "source" 2008 newProcessWaiting 2009 ifTrue: [^newProcess] 2010 ifFalse: [^memory fetchPointer: ActiveProcessIndex 2011 ofObject: self schedulerPointer] 2012 */ 2013 2014 if (newProcessWaiting) 2015 { 2016 return newProcess; 2017 } 2018 2019 return memory.fetchPointer_ofObject(ActiveProcessIndex, schedulerPointer()); 2020 } 2021 2022 2023 // dispatchControlPrimitives 2024 void Interpreter::dispatchControlPrimitives() 2025 { 2026 /* "source" 2027 primitiveIndex = 80 ifTrue: [^self primitiveBlockCopy]. 2028 primitiveIndex = 81 ifTrue: [^self primitiveValue]. 2029 primitiveIndex = 82 ifTrue: [^self primitiveValueWithArgs]. 2030 primitiveIndex = 83 ifTrue: [^self primitivePerform]. 2031 primitiveIndex = 84 ifTrue: [^self primitivePerformWithArgs]. 2032 primitiveIndex = 85 ifTrue: [^self primitiveSignal]. 2033 primitiveIndex = 86 ifTrue: [^self primitiveWait]. 2034 primitiveIndex = 87 ifTrue: [^self primitiveResume]. 2035 primitiveIndex = 88 ifTrue: [^self primitiveSuspend]. 2036 primitiveIndex = 89 ifTrue: [^self primitiveFlushCache] 2037 */ 2038 switch (primitiveIndex) 2039 { 2040 case 80: primitiveBlockCopy(); break; 2041 case 81: primitiveValue(); break; 2042 case 82: primitiveValueWithArgs(); break; 2043 case 83: primitivePerform(); break; 2044 case 84: primitivePerformWithArgs(); break; 2045 case 85: primitiveSignal(); break; 2046 case 86: primitiveWait(); break; 2047 case 87: primitiveResume(); break; 2048 case 88: primitiveSuspend(); break; 2049 case 89: primitiveFlushCache(); break; 2050 } 2051 2052 } 2053 2054 2055 bool Interpreter::isInLowMemoryCondition() 2056 { 2057 return oopsLeftLimit > 0 && wordsLeftLimit > 0 && 2058 (memory.oopsLeft() < oopsLeftLimit || 2059 memory.coreLeft() < wordsLeftLimit); 2060 } 2061 2062 2063 // checkProcessSwitch 2064 void Interpreter::checkProcessSwitch() 2065 { 2066 int theActiveProcess; 2067 2068 /* "source" 2069 [semaphoreIndex > 0] 2070 whileTrue: 2071 [self synchronousSignal: (semaphoreList at: semaphoreIndex). 2072 semaphoreIndex <- semaphoreIndex - 1]. 2073 newProcessWaiting 2074 ifTrue: [newProcessWaiting <- false. 2075 activeProcess <- self activeProcess. 2076 memory storePointer: SuspendedContextIndex 2077 ofObject: activeProcess 2078 withValue: activeContext. 2079 memory storePointer: ActiveProcessIndex 2080 ofObject: self schedulerPointer 2081 withValue: newProcess. 2082 self newActiveContext: 2083 (memory fetchPointer: SuspendedContextIndex 2084 ofObject: newProcess). 2085 "dbanay: Need to remove reference" 2086 memory decreaseReferencesTo: newProcess. 2087 newProcess <- nil] 2088 */ 2089 2090 2091 //dbanay -- warn about low memory -- once 2092 if (checkLowMemory) 2093 { 2094 bool memoryWasLow = memoryIsLow; 2095 memoryIsLow = false; 2096 // the Smalltalk system treat oops or words limits being zero as don't check 2097 if (lowSpaceSemaphore != NilPointer && oopsLeftLimit > 0 && wordsLeftLimit > 0) 2098 { 2099 if (isInLowMemoryCondition()) 2100 { 2101 memory.garbageCollect(); // Try to get some memory back... 2102 if (isInLowMemoryCondition()) 2103 { 2104 memoryIsLow = true; 2105 if (!memoryWasLow) 2106 asynchronousSignal(lowSpaceSemaphore); 2107 } 2108 } 2109 } 2110 checkLowMemory = false; 2111 } 2112 2113 2114 while (semaphoreIndex >= 0) 2115 { 2116 synchronousSignal(semaphoreList[semaphoreIndex]); 2117 semaphoreIndex = semaphoreIndex - 1; 2118 } 2119 if (newProcessWaiting) 2120 { 2121 newProcessWaiting = false; 2122 theActiveProcess = activeProcess(); 2123 memory.storePointer_ofObject_withValue(SuspendedContextIndex, theActiveProcess, activeContext); 2124 memory.storePointer_ofObject_withValue(ActiveProcessIndex, schedulerPointer(), newProcess); 2125 newActiveContext(memory.fetchPointer_ofObject(SuspendedContextIndex, newProcess)); 2126 memory.decreaseReferencesTo(newProcess); 2127 newProcess = NilPointer; 2128 } 2129 } 2130 2131 2132 // primitiveSignal 2133 void Interpreter::primitiveSignal() 2134 { 2135 /* "source" 2136 self synchronousSignal: self stackTop 2137 */ 2138 synchronousSignal(stackTop()); 2139 } 2140 2141 2142 // isEmptyList: 2143 int Interpreter::isEmptyList(int aLinkedList) 2144 { 2145 /* "source" 2146 ^(memory fetchPointer: FirstLinkIndex 2147 ofObject: aLinkedList) 2148 = NilPointer 2149 */ 2150 2151 return memory.fetchPointer_ofObject(FirstLinkIndex, aLinkedList) == NilPointer; 2152 } 2153 2154 2155 // primitiveSuspend 2156 void Interpreter::primitiveSuspend() 2157 { 2158 /* "source" 2159 self success: self stackTop = self activeProcess. 2160 self success 2161 ifTrue: [self popStack. 2162 self push: NilPointer. 2163 self suspendActive] 2164 */ 2165 success(stackTop() == activeProcess()); 2166 if (success()) 2167 { 2168 popStack(); 2169 push(NilPointer); 2170 suspendActive(); 2171 } 2172 2173 } 2174 2175 2176 // primitiveValue 2177 void Interpreter::primitiveValue() 2178 { 2179 int blockContext; 2180 int blockArgumentCount; 2181 int initialIP; 2182 2183 /* "source" 2184 blockContext <- self stackValue: argumentCount. 2185 blockArgumentCount <- self argumentCountOfBlock: blockContext. 2186 self success: argumentCount = blockArgumentCount. 2187 self success 2188 ifTrue: [self transfer: argumentCount 2189 fromIndex: stackPointer - argumentCount + 1 2190 ofObject: activeContext 2191 toIndex: TempFrameStart 2192 ofObject: blockContext. 2193 self pop: argumentCount + 1. 2194 initialIP <- memory fetchPointer: InitialIPIndex 2195 ofObject: blockContext. 2196 memory storePointer: InstructionPointerIndex 2197 ofObject: blockContext 2198 withValue: initialIP. 2199 self storeStackPointerValue: argumentCount 2200 inContext: blockContext. 2201 memory storePointer: CallerIndex 2202 ofObject: blockContext 2203 withValue: activeContext. 2204 self newActiveContext: blockContext] 2205 */ 2206 2207 blockContext = stackValue(argumentCount); 2208 blockArgumentCount = argumentCountOfBlock(blockContext); 2209 success(argumentCount == blockArgumentCount); 2210 if (success()) 2211 { 2212 transfer_fromIndex_ofObject_toIndex_ofObject(argumentCount, 2213 stackPointer - argumentCount + 1, 2214 activeContext, 2215 TempFrameStart, 2216 blockContext); 2217 pop(argumentCount + 1); 2218 initialIP = memory.fetchPointer_ofObject(InitialIPIndex, blockContext); 2219 memory.storePointer_ofObject_withValue(InstructionPointerIndex, blockContext, initialIP); 2220 storeStackPointerValue_inContext(argumentCount, blockContext); 2221 memory.storePointer_ofObject_withValue(CallerIndex, blockContext, activeContext); 2222 newActiveContext(blockContext); 2223 } 2224 2225 } 2226 2227 2228 // firstContext 2229 int Interpreter::firstContext() 2230 { 2231 /* "source" 2232 newProcessWaiting <- false. 2233 newProcess <- nil. "dbanay: need to initialize" 2234 ^memory fetchPointer: SuspendedContextIndex 2235 ofObject: self activeProcess 2236 */ 2237 2238 newProcessWaiting = false; 2239 newProcess = NilPointer; 2240 return memory.fetchPointer_ofObject(SuspendedContextIndex, activeProcess()); 2241 } 2242 2243 2244 // asynchronousSignal: 2245 void Interpreter::asynchronousSignal(int aSemaphore) 2246 { 2247 /* "source" 2248 semaphoreIndex <- semaphoreIndex + 1. 2249 semaphoreList at: semaphoreIndex put: aSemaphore 2250 */ 2251 semaphoreIndex = semaphoreIndex + 1; 2252 if (semaphoreIndex == sizeof(semaphoreList)/sizeof(semaphoreList[0])) 2253 error("overflow semaphore list"); 2254 semaphoreList[semaphoreIndex] = aSemaphore; 2255 2256 } 2257 2258 2259 // transferTo: 2260 void Interpreter::transferTo(int aProcess) 2261 { 2262 /* "source" 2263 newProcessWaiting <- true. 2264 "dbanay needs ref count newProcess" 2265 newProcess ~~ NilPointer ifTrue: [ memory decreaseReferencesTo: newProcess ]. 2266 newProcess <- aProcess 2267 */ 2268 newProcessWaiting = true; 2269 if (newProcess != NilPointer) 2270 memory.decreaseReferencesTo(newProcess); 2271 newProcess = aProcess; 2272 memory.increaseReferencesTo(newProcess); 2273 2274 } 2275 2276 2277 // resume: 2278 void Interpreter::resume(int aProcess) 2279 { 2280 int theActiveProcess; 2281 int activePriority; 2282 int newPriority; 2283 2284 /* "source" 2285 activeProcess <- self activeProcess. 2286 activePriority <- self fetchInteger: PriorityIndex 2287 ofObject: activeProcess. 2288 newPriority <- success fetchInteger: PriorityIndex 2289 ofObject: aProcess. 2290 newPriority > activePriority 2291 ifTrue: [self sleep: activeProcess 2292 self transferTo: aProcess] 2293 ifFalse: [self sleep: aProcess] 2294 */ 2295 theActiveProcess = activeProcess(); 2296 activePriority = fetchInteger_ofObject(PriorityIndex, theActiveProcess); 2297 newPriority = fetchInteger_ofObject(PriorityIndex, aProcess); 2298 if (newPriority > activePriority) 2299 { 2300 sleep(theActiveProcess); 2301 transferTo(aProcess); 2302 } 2303 else 2304 { 2305 sleep(aProcess); 2306 } 2307 } 2308 2309 2310 // sleep: 2311 void Interpreter::sleep(int aProcess) 2312 { 2313 int priority; 2314 int processLists; 2315 int processList; 2316 2317 /* "source" 2318 priority <- self fetchInteger: PriorityIndex 2319 ofObject: aProcess. 2320 processLists <- memory fetchPointer: ProcessListsIndex 2321 ofObject: self schedulerPointer. 2322 processList <- memory fetchPointer: priority - 1 2323 ofObject: processLists. 2324 self addLastLink: aProcess 2325 toList: processList 2326 */ 2327 priority = fetchInteger_ofObject(PriorityIndex, aProcess); 2328 processLists = memory.fetchPointer_ofObject(ProcessListsIndex, schedulerPointer()); 2329 processList = memory.fetchPointer_ofObject(priority - 1, processLists); 2330 addLastLink_toList(aProcess, processList); 2331 } 2332 2333 2334 // primitiveClass 2335 void Interpreter::primitiveClass() 2336 { 2337 int instance; 2338 2339 /* "source" 2340 instance <- self popStack. 2341 self push: (memory fetchClassOf: instance) 2342 */ 2343 instance = popStack(); 2344 push(memory.fetchClassOf(instance)); 2345 } 2346 2347 2348 void Interpreter::primitiveCoreLeft() 2349 { 2350 pop(1); // remove receiver 2351 push(positive32BitIntegerFor(memory.coreLeft())); 2352 } 2353 2354 void Interpreter::primitiveQuit() 2355 { 2356 hal->signal_quit(); 2357 2358 } 2359 2360 void Interpreter::primitiveExitToDebugger() 2361 { 2362 hal->exit_to_debugger(); 2363 } 2364 2365 void Interpreter::primitiveOopsLeft() 2366 { 2367 pop(1); // remove receiver 2368 push(positive16BitIntegerFor(memory.oopsLeft())); 2369 } 2370 2371 void Interpreter::primitiveSignalAtOopsLeftWordsLeft() 2372 { 2373 /* 2374 (from the sources) 2375 Tell the object memory to signal the Semaphore when either the number 2376 of object pointers remaining drops below numOops, or the number of 2377 words in the object space remaining drops below numWords. Fail if the 2378 first argument is neither a Semaphore nor nil. Fail if numOops is not a 2379 16-bit Integer, or if numWords is not a 32-bit LargePositiveInteger. 2380 */ 2381 2382 std::uint32_t numWords = positive32BitValueOf(popStack()); 2383 int numOops = positive16BitValueOf(popStack()); 2384 int semaphore = popStack(); 2385 // sempahore must either be nil or an instance of Semaphore 2386 success(semaphore == NilPointer || memory.fetchClassOf(semaphore) == ClassSemaphorePointer); 2387 if (success()) 2388 { 2389 // Don't need to take reference since Smalltalk is waiting on it someplace else 2390 lowSpaceSemaphore = semaphore; 2391 oopsLeftLimit = numOops; 2392 wordsLeftLimit = numWords; 2393 } 2394 else 2395 unPop(3); 2396 2397 } 2398 2399 2400 // dispatchSystemPrimitives 2401 void Interpreter::dispatchSystemPrimitives() 2402 { 2403 /* "source" 2404 primitiveIndex = 110 ifTrue: [^self primitiveEquivalent]. 2405 primitiveIndex = 111 ifTrue: [^self primitiveClass]. 2406 primitiveIndex = 112 ifTrue: [^self primitiveCoreLeft]. 2407 primitiveIndex = 113 ifTrue: [^self primitiveQuit]. 2408 primitiveIndex = 114 ifTrue: [^self primitiveExitToDebugger]. 2409 primitiveIndex = 115 ifTrue: [^self primitiveOopsLeft]. 2410 primitiveIndex = 116 ifTrue: [^self primitiveSignalAtOopsLeftWordsLeft] 2411 */ 2412 2413 switch (primitiveIndex) 2414 { 2415 case 110: primitiveEquivalent(); break; 2416 case 111: primitiveClass(); break; 2417 case 112: primitiveCoreLeft(); break; 2418 case 113: primitiveQuit(); break; 2419 case 114: primitiveExitToDebugger(); break; 2420 case 115: primitiveOopsLeft(); break; 2421 case 116: primitiveSignalAtOopsLeftWordsLeft(); break; 2422 } 2423 } 2424 2425 2426 // primitiveEquivalent 2427 void Interpreter::primitiveEquivalent() 2428 { 2429 int thisObject; 2430 int otherObject; 2431 2432 /* "source" 2433 otherObject <- self popStack. 2434 thisObject <- self popStack. 2435 thisObject = otherObject 2436 ifTrue: [self push: TruePointer] 2437 ifFalse: [self push: FalsePointer] 2438 */ 2439 2440 otherObject = popStack(); 2441 thisObject = popStack(); 2442 if (thisObject == otherObject) 2443 push(TruePointer); 2444 else 2445 push(FalsePointer); 2446 } 2447 2448 2449 // dispatchPrimitives 2450 void Interpreter::dispatchPrimitives() 2451 { 2452 /* "source" 2453 primitiveIndex < 60 2454 ifTrue: [^self dispatchArithmeticPrimitives]. 2455 primitiveIndex < 68 2456 ifTrue: [^self dispatchSubscriptAndStreamPrimitives]. 2457 primitiveIndex < 80 2458 ifTrue: [^self dispatchStorageManagementPrimitives]. 2459 primitiveIndex < 90 2460 ifTrue: [^self dispatchControlPrimitives]. 2461 primitiveIndex < 110 2462 ifTrue: [^self dispatchInputOutputPrimitives]. 2463 primitiveIndex < 128 2464 ifTrue: [^self dispatchSystemPrimitives]. 2465 primitiveIndex < 256 2466 ifTrue: [^self dispatchPrivatePrimitives] 2467 */ 2468 if (primitiveIndex < 60) 2469 dispatchArithmeticPrimitives(); 2470 else if (primitiveIndex < 68) 2471 dispatchSubscriptAndStreamPrimitives(); 2472 else if (primitiveIndex < 80) 2473 dispatchStorageManagementPrimitives(); 2474 else if (primitiveIndex < 90) 2475 dispatchControlPrimitives(); 2476 else if (primitiveIndex < 110) 2477 dispatchInputOutputPrimitives(); 2478 else if (primitiveIndex < 128) 2479 dispatchSystemPrimitives(); 2480 else if (primitiveIndex < 256) 2481 { 2482 dispatchPrivatePrimitives(); 2483 } 2484 } 2485 2486 // dispatchPrivatePrimitives 2487 void Interpreter::dispatchPrivatePrimitives() 2488 { 2489 switch (primitiveIndex) 2490 { 2491 case 128: // beSnapshot 2492 primitiveBeSnapshotFile(); 2493 break; 2494 case 130: // Posix File primitives 2495 primitivePosixFileOperation(); 2496 break; 2497 case 131: // Posix Directory primitives 2498 primitivePosixDirectoryOperation(); 2499 break; 2500 case 132: // Posix error 2501 primitivePosixLastErrorOperation(); 2502 break; 2503 case 133: // Posix error string 2504 primitivePosixErrorStringOperation(); 2505 break; 2506 default: 2507 primitiveFail(); 2508 break; 2509 } 2510 } 2511 2512 void Interpreter::primitiveBeSnapshotFile() 2513 { 2514 2515 int fileObjectPointer = stackTop(); 2516 int fileNamePointer = memory.fetchPointer_ofObject(FileNameIndex, fileObjectPointer); // fileName field 2517 std::string fileName = stringFromObject(fileNamePointer); 2518 2519 hal->set_image_name(fileName.c_str()); 2520 } 2521 2522 void Interpreter::primitivePosixLastErrorOperation() 2523 { 2524 pop(1); 2525 pushInteger(fileSystem->last_error()); 2526 } 2527 2528 void Interpreter::primitivePosixErrorStringOperation() 2529 { 2530 int code = popInteger(); 2531 pop(1); // pop receiver 2532 if (success()) 2533 push(stringObjectFor(fileSystem->error_text(code))); 2534 else 2535 unPop(2); 2536 } 2537 2538 void Interpreter::primitivePosixFileOperation() 2539 { 2540 // command id, name, page, file 2541 /* 2542 can get file descriptor from file ('fd' field) 2543 ID Return Remarks 2544 ---- ------- ---------- 2545 0 true/false read page (stores size in bytesInPage) 2546 1 true/false write page (uses bytesInPage) 2547 2 true/false truncate after page or entire file if page is nil 2548 3 size of file/nil file size 2549 4 true/false open file -- sets fd in file 2550 5 true/false close file -- sets fd to -1 2551 */ 2552 2553 int page = popStack(); 2554 int name = popStack(); 2555 int code = popInteger(); 2556 int file = popStack(); 2557 2558 const int DescriptorIndex = 8; // fd field of PosixFile 2559 const int PageNumberIndex = 3; // pageNumber field in PosixFilePage 2560 2561 const int PageInPageIndex = 1; // ByteArray contents in PosixFilePage 2562 const int BytesInPageIndex = 4; // bytesInPage field in PosixFilePage 2563 2564 const int PageSize = 512; // MUST match page size of PosixFilePage 2565 2566 static std::uint8_t pageBuffer[PageSize]; 2567 2568 // Code must be legit 2569 success(code >= 0 && code <= 6); 2570 success(file != NilPointer); 2571 2572 if (success()) 2573 { 2574 switch (code) 2575 { 2576 case 0: // Read page 2577 { 2578 success(memory.fetchPointer_ofObject(DescriptorIndex, file) != NilPointer); 2579 success(page != NilPointer); 2580 if (success()) 2581 { 2582 int fd = (int) positive32BitValueOf(memory.fetchPointer_ofObject(DescriptorIndex, file)); 2583 int pageNumber = fetchInteger_ofObject(PageNumberIndex, page); 2584 assert(pageNumber >= 1); 2585 2586 int byteArray = memory.fetchPointer_ofObject(PageInPageIndex, page); 2587 2588 int position = (pageNumber - 1)*PageSize; 2589 2590 if (fileSystem->seek_to(fd, position) != position) 2591 { 2592 push(FalsePointer); 2593 return; 2594 } 2595 2596 // No direct pointer access! Read full page and store byte by byte 2597 int bytesInPage = fileSystem->read(fd, (char *) &pageBuffer, PageSize); 2598 2599 for(int i = 0; i < bytesInPage; i++) 2600 memory.storeByte_ofObject_withValue(i, byteArray, pageBuffer[i]); 2601 2602 storeInteger_ofObject_withValue(BytesInPageIndex, page, bytesInPage); 2603 push(TruePointer); 2604 } 2605 } 2606 break; 2607 2608 case 1: // Write page 2609 { 2610 success(memory.fetchPointer_ofObject(DescriptorIndex, file) != NilPointer); 2611 success(page != NilPointer); 2612 if (success()) 2613 { 2614 int fd = (int) positive32BitValueOf(memory.fetchPointer_ofObject(DescriptorIndex, file)); 2615 int pageNumber = fetchInteger_ofObject(PageNumberIndex, page); 2616 assert(pageNumber >= 1); 2617 2618 int byteArray = memory.fetchPointer_ofObject(PageInPageIndex, page); 2619 2620 int position = (pageNumber - 1)*PageSize; 2621 2622 if (fileSystem->seek_to(fd, position) != position) 2623 { 2624 push(FalsePointer); 2625 return; 2626 } 2627 2628 // No direct pointer access! Read bytes by byte into staging buffer 2629 int bytesInPage = fetchInteger_ofObject(BytesInPageIndex, page); 2630 // Fill buffer with page data 2631 for(int i = 0; i < bytesInPage; i++) 2632 pageBuffer[i] = memory.fetchByte_ofObject(i, byteArray); 2633 2634 // blast it out 2635 if (fileSystem->write(fd, (char *) pageBuffer, bytesInPage) == bytesInPage) 2636 push(TruePointer); 2637 else 2638 push(FalsePointer); 2639 } 2640 } 2641 break; 2642 2643 case 2: // truncate page (make it the last page). A nil page means truncate to zero bytes 2644 { 2645 success(memory.fetchPointer_ofObject(DescriptorIndex, file) != NilPointer); 2646 success(page != NilPointer); 2647 if (success()) 2648 { 2649 2650 int result; 2651 int fd = (int) positive32BitValueOf(memory.fetchPointer_ofObject(DescriptorIndex, file)); 2652 if (page != NilPointer) 2653 { 2654 int pageNumber = fetchInteger_ofObject(PageNumberIndex, page); 2655 assert(pageNumber >= 1); 2656 2657 int bytesInPage = fetchInteger_ofObject(BytesInPageIndex, page); 2658 int newSize = (pageNumber-1)*PageSize + bytesInPage; 2659 result = fileSystem->truncate_to(fd, newSize); 2660 } 2661 else 2662 result = fileSystem->truncate_to(fd, 0); 2663 push(result != -1 ? TruePointer : FalsePointer); 2664 } 2665 2666 } 2667 break; 2668 2669 case 3: // File size 2670 { 2671 success(memory.fetchPointer_ofObject(DescriptorIndex, file) != NilPointer); 2672 if (success()) 2673 { 2674 int fd = (int) positive32BitValueOf(memory.fetchPointer_ofObject(DescriptorIndex, file)); 2675 int size = fileSystem->file_size(fd); 2676 if (size >= 0) 2677 push(positive32BitIntegerFor(size)); 2678 else 2679 push(NilPointer); 2680 } 2681 } 2682 break; 2683 2684 case 4: // Open File 2685 { 2686 success(memory.fetchClassOf(name) == ClassStringPointer); 2687 if (success()) 2688 { 2689 std::string fileName = stringFromObject(name); 2690 int fd = fileSystem->open_file(fileName.c_str()); 2691 if (fd != -1) 2692 { 2693 memory.storePointer_ofObject_withValue(DescriptorIndex, file, positive32BitIntegerFor(fd)); 2694 push(TruePointer); 2695 } 2696 else 2697 push(FalsePointer); 2698 } 2699 } 2700 2701 break; 2702 2703 case 5: // Close file 2704 { 2705 int fd = (int) positive32BitValueOf(memory.fetchPointer_ofObject(DescriptorIndex, file)); 2706 int result = fileSystem->close_file(fd); 2707 memory.storePointer_ofObject_withValue(DescriptorIndex, file, NilPointer); 2708 push(result != -1 ? TruePointer : FalsePointer); 2709 } 2710 break; 2711 } 2712 } 2713 2714 if (!success()) 2715 unPop(4); 2716 2717 } 2718 2719 void Interpreter::primitivePosixDirectoryOperation() 2720 { 2721 /* 2722 If you allocate an array and then allocate more objects to put into that array, the array pointer has no reference and can go away if GC happens. Be sure to push it on the stack after alloc to protect it. 2723 2724 */ 2725 /* 2726 ID ARG1 ARG2 Return Remarks 2727 ---- -------- ------- ------ ----------- 2728 0 name fd/nil create file (replacing existing one) 2729 1 name true/false delete file 2730 2 new name PosixFile true/false rename file 2731 3 nil Array of strings Array of file names in directory 2732 */ 2733 2734 const int DescriptorIndex = 8; // fd field of PosixFile 2735 2736 2737 int arg2 = popStack(); 2738 int arg1 = popStack(); 2739 int code = popInteger(); 2740 pop(1); // remove receiver 2741 2742 success(code >= 0 && code <= 3); 2743 success(arg1 == NilPointer || memory.fetchClassOf(arg1) == ClassStringPointer); 2744 2745 if (success()) 2746 { 2747 switch (code) 2748 { 2749 case 0: // Create file 2750 { 2751 std::string s = stringFromObject(arg1); 2752 int fd = fileSystem->create_file(s.c_str()); 2753 if (fd != -1) 2754 { 2755 push(positive32BitIntegerFor(fd)); 2756 } 2757 else 2758 push(NilPointer); 2759 } 2760 break; 2761 2762 case 1: // Delete file 2763 { 2764 std::string s = stringFromObject(arg1); 2765 push(fileSystem->delete_file(s.c_str()) ? TruePointer : FalsePointer); 2766 } 2767 break; 2768 2769 case 2: // Rename file 2770 { 2771 int oldPos = 0; // silence uninitialized variable warning 2772 int file = arg2; 2773 int newNameObjectPointer = arg1; 2774 int descriptorObjectPointer = memory.fetchPointer_ofObject(DescriptorIndex, file); 2775 bool wasOpen = (descriptorObjectPointer != NilPointer); 2776 if (wasOpen) 2777 { 2778 int fd = (int) positive32BitValueOf(descriptorObjectPointer); 2779 oldPos = fileSystem->tell(fd); // Save position for restore after we open new file... 2780 // Some operating systems don't let you rename an open file... 2781 fileSystem->close_file(fd); 2782 } 2783 std::string newName = stringFromObject(newNameObjectPointer); 2784 std::string oldName = stringFromObject(memory.fetchPointer_ofObject(FileNameIndex, file)); 2785 if (fileSystem->rename_file(oldName.c_str(), newName.c_str())) 2786 { 2787 // Re-open and update descriptor of File object 2788 if (wasOpen) 2789 { 2790 int fd = fileSystem->open_file(newName.c_str()); 2791 if (fd != -1) 2792 { 2793 memory.storePointer_ofObject_withValue(DescriptorIndex, 2794 file, positive32BitIntegerFor(fd)); 2795 fileSystem->seek_to(fd, oldPos); // Restore position.... 2796 } 2797 else 2798 memory.storePointer_ofObject_withValue(DescriptorIndex, file, NilPointer); 2799 } 2800 // Update with new name 2801 memory.storePointer_ofObject_withValue(FileNameIndex, file, newNameObjectPointer); 2802 push(TruePointer); 2803 } 2804 else 2805 { 2806 if (wasOpen) 2807 { 2808 // Something failed. Open the original file again...if possible 2809 int fd = fileSystem->open_file(oldName.c_str()); 2810 if (fd != -1) 2811 { 2812 memory.storePointer_ofObject_withValue(DescriptorIndex, 2813 file, positive32BitIntegerFor(fd)); 2814 fileSystem->seek_to(fd, oldPos); // Restore position.... 2815 } 2816 else 2817 memory.storePointer_ofObject_withValue(DescriptorIndex, file, NilPointer); 2818 } 2819 push(FalsePointer); 2820 } 2821 } 2822 break; 2823 case 3: // Enumerate files 2824 { 2825 // One pass to get length... 2826 int length = 0; 2827 fileSystem->enumerate_files([&](const char *) { 2828 length++; 2829 }); 2830 2831 int array = memory.instantiateClass_withPointers(ClassArrayPointer, length); 2832 // Push the array first so there is a refernce on it just in case GC takes place when we add the 2833 // entries 2834 push(array); 2835 2836 // A second pass to fetch and store values 2837 int i = 0; 2838 fileSystem->enumerate_files([&](const char *name) { 2839 memory.storePointer_ofObject_withValue(i, array, stringObjectFor(name)); 2840 i++; 2841 }); 2842 } 2843 break; 2844 } 2845 } 2846 else 2847 unPop(4); 2848 2849 } 2850 2851 2852 2853 // positive16BitValueOf: 2854 int Interpreter::positive16BitValueOf(int integerPointer) 2855 { 2856 int value; 2857 2858 /* "source" 2859 (memory isIntegerObject: integerPointer) 2860 ifTrue: [^memory integerValueOf: integerPointer]. 2861 (memory fetchClassOf: integerPointer) = 2862 ClassLargePositiveIntegerPointer 2863 ifFalse: [^self primitiveFail]. 2864 (memory fetchByteLengthOf: integerPointer) = 2 2865 ifFalse: [^self primitiveFail]. 2866 value <- memory fetchByte: 1 2867 ofObject: integerPointer. 2868 value <- value * 256 + (memory fetchByte: 0 2869 ofObject: integerPointer). 2870 ^value 2871 */ 2872 if (memory.isIntegerObject(integerPointer)) 2873 return memory.integerValueOf(integerPointer); 2874 if (memory.fetchClassOf(integerPointer) != ClassLargePositiveIntegerPointer) 2875 return primitiveFail(); 2876 if (memory.fetchByteLengthOf(integerPointer) != 2) 2877 return primitiveFail(); 2878 2879 value = memory.fetchByte_ofObject(1, integerPointer); 2880 value = value * 256 + memory.fetchByte_ofObject(0, integerPointer); 2881 return value; 2882 } 2883 2884 2885 std::uint32_t Interpreter::positive32BitValueOf(int integerPointer) 2886 { 2887 std::uint32_t value; 2888 if (memory.isIntegerObject(integerPointer)) 2889 return memory.integerValueOf(integerPointer); 2890 if (memory.fetchClassOf(integerPointer) != ClassLargePositiveIntegerPointer) 2891 return primitiveFail(); 2892 2893 int bytes = memory.fetchByteLengthOf(integerPointer); 2894 value = memory.fetchByte_ofObject(0, integerPointer); 2895 if (bytes > 1) 2896 value |= memory.fetchByte_ofObject(1, integerPointer) << 8; 2897 if (bytes > 2) 2898 value |= memory.fetchByte_ofObject(2, integerPointer) << 16; 2899 if (bytes > 3) 2900 value |= ((std::uint32_t) memory.fetchByte_ofObject(3, integerPointer)) << 24; 2901 2902 return value; 2903 } 2904 2905 // primitiveResponse 2906 bool Interpreter::primitiveResponse() 2907 { 2908 int flagValue; 2909 2910 /* "source" 2911 primitiveIndex = 0 2912 ifTrue: [flagValue <- self flagValueOf: newMethod. 2913 flagValue = 5 2914 ifTrue: [self quickReturnSelf. 2915 ^true]. 2916 flagValue = 6 2917 ifTrue: [self quickInstanceLoad. 2918 ^true]. 2919 ^false] 2920 ifFalse: [self initPrimitive. 2921 self dispatchPrimitives. 2922 ^self success] 2923 */ 2924 2925 if (primitiveIndex == 0) 2926 { 2927 flagValue = flagValueOf(newMethod); 2928 if (flagValue == 5) 2929 { 2930 quickReturnSelf(); 2931 return true; 2932 } 2933 if (flagValue == 6) 2934 { 2935 quickInstanceLoad(); 2936 return true; 2937 } 2938 return false; 2939 } 2940 initPrimitive(); 2941 dispatchPrimitives(); 2942 return success(); 2943 } 2944 2945 2946 // quickInstanceLoad 2947 void Interpreter::quickInstanceLoad() 2948 { 2949 int thisReceiver; 2950 int fieldIndex; 2951 2952 /* "source" 2953 thisReceiver <- self popStack. 2954 fieldIndex <- self fieldIndexOf: newMethod. 2955 self push: (memory fetchPointer: fieldIndex 2956 ofObject: thisReceiver) 2957 */ 2958 thisReceiver = popStack(); 2959 fieldIndex = fieldIndexOf(newMethod); 2960 push(memory.fetchPointer_ofObject(fieldIndex, thisReceiver)); 2961 } 2962 2963 2964 // arithmeticSelectorPrimitive 2965 void Interpreter::arithmeticSelectorPrimitive() 2966 { 2967 /* "source" 2968 "ERROR: the next line is redundant" 2969 "self success: (memory isIntegerObject: (self stackValue: 1))." 2970 self success 2971 ifTrue:[currentBytecode = 176 ifTrue: [^self primitiveAdd]. 2972 currentBytecode = 177 ifTrue: [^self primitiveSubtract]. 2973 currentBytecode = 178 ifTrue: [^self primitiveLessThan]. 2974 currentBytecode = 179 ifTrue: [^self primitiveGreaterThan]. 2975 currentBytecode = 180 ifTrue: [^self primitiveLessOrEqual]. 2976 currentBytecode = 181 ifTrue: [^self primitiveGreaterOrEqual]. 2977 currentBytecode = 182 ifTrue: [^self primitiveEqual]. 2978 currentBytecode = 183 ifTrue: [^self primitiveNotEqual]. 2979 currentBytecode = 184 ifTrue: [^self primitiveMultiply]. 2980 currentBytecode = 185 ifTrue: [^self primitiveDivide]. 2981 currentBytecode = 186 ifTrue: [^self primitiveMod]. 2982 currentBytecode = 187 ifTrue: [^self primitiveMakePoint]. 2983 currentBytecode = 188 ifTrue: [^self primitiveBitShift]. 2984 currentBytecode = 189 ifTrue: [^self primitiveDiv]. 2985 currentBytecode = 190 ifTrue: [^self primitiveBitAnd]. 2986 currentBytecode = 191 ifTrue: [^self primitiveBitOr]] 2987 */ 2988 2989 //success(memory.isIntegerObject(stackValue(1))); 2990 if (success()) 2991 { 2992 switch (currentBytecode) 2993 { 2994 case 176: primitiveAdd(); break; 2995 case 177: primitiveSubtract(); break; 2996 case 178: primitiveLessThan(); break; 2997 case 179: primitiveGreaterThan(); break; 2998 case 180: primitiveLessOrEqual(); break; 2999 case 181: primitiveGreaterOrEqual(); break; 3000 case 182: primitiveEqual(); break; 3001 case 183: primitiveNotEqual(); break; 3002 case 184: primitiveMultiply(); break; 3003 case 185: primitiveDivide(); break; 3004 case 186: primitiveMod(); break; 3005 case 187: primitiveMakePoint(); break; 3006 case 188: primitiveBitShift(); break; 3007 case 189: primitiveDiv(); break; 3008 case 190: primitiveBitAnd(); break; 3009 case 191: primitiveBitOr(); break; 3010 } 3011 } 3012 } 3013 3014 // positive16BitIntegerFor: 3015 int Interpreter::positive16BitIntegerFor(int integerValue) 3016 { 3017 int newLargeInteger; 3018 3019 /* "source" 3020 integerValue < 0 3021 ifTrue: [^self primitiveFail]. 3022 (memory isIntegerValue: integerValue) 3023 ifTrue: [^memory integerObjectOf: integerValue]. 3024 newLargeInteger <- memory instantiateClass: 3025 ClassLargePositiveIntegerPointer 3026 withBytes: 2. 3027 memory storeByte: 0 3028 ofObject: newLargeInteger 3029 withValue: (self lowByteOf: integerValue). 3030 memory storeByte: 1 3031 ofObject: newLargeInteger 3032 withValue: (self highByteOf: integerValue). 3033 ^newLargeInteger 3034 */ 3035 3036 if (integerValue < 0) 3037 { 3038 return primitiveFail(); 3039 } 3040 if (memory.isIntegerValue(integerValue)) 3041 return memory.integerObjectOf(integerValue); 3042 newLargeInteger = memory.instantiateClass_withBytes(ClassLargePositiveIntegerPointer, 2); 3043 memory.storeByte_ofObject_withValue(0, newLargeInteger, lowByteOf(integerValue)); 3044 memory.storeByte_ofObject_withValue(1, newLargeInteger, highByteOf(integerValue)); 3045 return newLargeInteger; 3046 } 3047 3048 int Interpreter::positive32BitIntegerFor(int integerValue) 3049 { 3050 3051 if (memory.isIntegerValue(integerValue)) 3052 return memory.integerObjectOf(integerValue); 3053 int newLargeInteger; 3054 3055 newLargeInteger = memory.instantiateClass_withBytes(ClassLargePositiveIntegerPointer, 4); 3056 memory.storeByte_ofObject_withValue(0, newLargeInteger, lowByteOf(integerValue)); 3057 memory.storeByte_ofObject_withValue(1, newLargeInteger, highByteOf(integerValue)); 3058 memory.storeByte_ofObject_withValue(2, newLargeInteger, (integerValue >> 16) & 0xff); 3059 memory.storeByte_ofObject_withValue(3, newLargeInteger, (integerValue >> 24) & 0xff); 3060 return newLargeInteger; 3061 3062 } 3063 3064 3065 // popInteger 3066 int Interpreter::popInteger() 3067 { 3068 int integerPointer; 3069 3070 /* "source" 3071 integerPointer <- self popStack. 3072 self success: (memory isIntegerObject: integerPointer). 3073 self success 3074 ifTrue: [^memory integerValueOf: integerPointer] 3075 */ 3076 integerPointer = popStack(); 3077 success(memory.isIntegerObject(integerPointer)); 3078 if (success()) 3079 return memory.integerValueOf(integerPointer); 3080 3081 return std::numeric_limits<int>::min(); 3082 } 3083 3084 3085 // specialSelectorPrimitiveResponse 3086 int Interpreter::specialSelectorPrimitiveResponse() 3087 { 3088 /* "source" 3089 self initPrimitive. 3090 (currentBytecode between: 176 and: 191) 3091 ifTrue: [self arithmeticSelectorPrimitive]. 3092 (currentBytecode between: 192 and: 207) 3093 ifTrue: [self commonSelectorPrimitive]. 3094 ^self success 3095 */ 3096 3097 initPrimitive(); 3098 if (between_and(currentBytecode, 176, 191)) 3099 arithmeticSelectorPrimitive(); 3100 else if (between_and(currentBytecode, 192, 207)) 3101 commonSelectorPrimitive(); 3102 return success(); 3103 } 3104 3105 3106 // commonSelectorPrimitive 3107 void Interpreter::commonSelectorPrimitive() 3108 { 3109 int receiverClass; 3110 3111 /* "source" 3112 argumentCount <- self fetchInteger: (currentBytecode - 176)*2 + 1 3113 ofObject: SpecialSelectorsPointer. 3114 receiverClass <- 3115 memory fetchClassOf: (self stackValue: argumentCount). 3116 currentBytecode = 198 ifTrue: [^self primitiveEquivalent]. 3117 currentBytecode = 199 ifTrue: [^self primitiveClass]. 3118 currentBytecode = 200 3119 ifTrue: [self success: 3120 (receiverClass = ClassMethodContextPointer) 3121 | (receiverClass = ClassBlockContextPointer). 3122 ^self success ifTrue: [self primitiveBlockCopy]]. 3123 (currentBytecode = 201) | (currentBytecode = 202) 3124 ifTrue: [self success: receiverClass = ClassBlockContextPointer. 3125 ^self success ifTrue: [self primitiveValue]]. 3126 self primitiveFail 3127 */ 3128 3129 argumentCount = fetchInteger_ofObject((currentBytecode - 176)*2 + 1, SpecialSelectorsPointer); 3130 receiverClass = memory.fetchClassOf(stackValue(argumentCount)); 3131 if (currentBytecode == 198 ) 3132 { 3133 primitiveEquivalent(); 3134 } 3135 else if (currentBytecode == 199 ) 3136 { 3137 primitiveClass(); 3138 } 3139 else if (currentBytecode == 200) 3140 { 3141 success(receiverClass == ClassMethodContextPointer || receiverClass == ClassBlockContextPointer); 3142 if (success()) 3143 primitiveBlockCopy(); 3144 } 3145 else if (currentBytecode == 201 || currentBytecode == 202) 3146 { 3147 success(receiverClass == ClassBlockContextPointer); 3148 if (success()) 3149 primitiveValue(); 3150 } 3151 else 3152 primitiveFail(); 3153 } 3154 3155 3156 3157 // initializeMethodCache 3158 void Interpreter::initializeMethodCache() 3159 { 3160 /* "source" 3161 methodCacheSize <- 1024. 3162 methodCache <- Array new: methodCacheSize 3163 */ 3164 for(int i = 0; i < sizeof(methodCache)/sizeof(methodCache[0]); i++) 3165 methodCache[i] = NilPointer; 3166 } 3167 3168 3169 // primitiveMod 3170 void Interpreter::primitiveMod() 3171 { 3172 int integerReceiver; 3173 int integerArgument; 3174 int integerResult = 0; //silence warning 3175 3176 /* "source" 3177 integerArgument <- self popInteger. 3178 integerReceiver <- self popInteger. 3179 self success: integerArgument ~= 0. 3180 self success 3181 ifTrue: [integerResult <- integerReceiver \\ integerArgument. 3182 self success: (memory isIntegerValue: integerResult)]. 3183 self success 3184 ifTrue: [self pushInteger: integerResult] 3185 ifFalse: [self unPop: 2] 3186 */ 3187 3188 integerArgument = popInteger(); 3189 integerReceiver = popInteger(); 3190 success(integerArgument != 0); 3191 if (success()) 3192 { 3193 integerResult = integerReceiver % integerArgument; 3194 if (integerArgument < 0) 3195 { 3196 if (integerResult > 0) 3197 { 3198 integerResult += integerArgument; 3199 } 3200 } else 3201 { 3202 if (integerResult < 0) 3203 { 3204 integerResult += integerArgument; 3205 } 3206 } 3207 success(memory.isIntegerValue(integerResult)); 3208 } 3209 if (success()) 3210 pushInteger(integerResult); 3211 else 3212 unPop(2); 3213 } 3214 3215 3216 // dispatchArithmeticPrimitives 3217 void Interpreter::dispatchArithmeticPrimitives() 3218 { 3219 /* "source" 3220 primitiveIndex < 20 3221 ifTrue: [^self dispatchIntegerPrimitives]. 3222 primitiveIndex < 40 3223 ifTrue: [^self dispatchLargeIntegerPrimitives]. 3224 primitiveIndex < 60 3225 ifTrue: [^self dispatchFloatPrimitives] 3226 */ 3227 if (primitiveIndex < 20) 3228 { 3229 dispatchIntegerPrimitives(); 3230 } 3231 else if (primitiveIndex < 40) 3232 { 3233 dispatchLargeIntegerPrimitives(); 3234 } 3235 else if (primitiveIndex < 60) 3236 { 3237 dispatchFloatPrimitives(); 3238 } 3239 } 3240 3241 3242 // primitiveEqual 3243 void Interpreter::primitiveEqual() 3244 { 3245 int integerReceiver; 3246 int integerArgument; 3247 3248 /* "source" 3249 integerArgument <- self popInteger. 3250 integerReceiver <- self popInteger. 3251 self success 3252 ifTrue: [integerReceiver = integerArgument 3253 ifTrue: [self push: TruePointer] 3254 ifFalse: [self push: FalsePointer]] 3255 ifFalse: [self unPop: 2] 3256 */ 3257 3258 integerArgument = popInteger(); 3259 integerReceiver = popInteger(); 3260 3261 if (success()) 3262 { 3263 if (integerReceiver == integerArgument) 3264 push(TruePointer); 3265 else 3266 push(FalsePointer); 3267 } 3268 else 3269 { 3270 unPop(2); 3271 } 3272 } 3273 3274 3275 // primitiveBitOr 3276 void Interpreter::primitiveBitOr() 3277 { 3278 int integerReceiver; 3279 int integerArgument; 3280 int integerResult; 3281 3282 /* "source" 3283 integerArgument <- self popInteger. 3284 integerReceiver <- self popInteger. 3285 self success 3286 ifTrue: [integerResult <- integerReceiver bitOr: integerArgument]. 3287 self success 3288 ifTrue: [self pushInteger: integerResult] 3289 ifFalse: [self unPop: 2] 3290 */ 3291 3292 integerArgument = popInteger(); 3293 integerReceiver = popInteger(); 3294 3295 if (success()) 3296 { 3297 integerResult = integerReceiver | integerArgument; 3298 pushInteger(integerResult); 3299 } 3300 else 3301 unPop(2); 3302 } 3303 3304 3305 // primitiveDivide 3306 // The primitive routine for division (associated with the selector/) is different than 3307 // the other three arithmetic primitives since it only produces a result if the division 3308 // is exact, otherwise the primitive fails. 3309 void Interpreter::primitiveDivide() 3310 { 3311 int integerReceiver; 3312 int integerArgument; 3313 int integerResult = 0; // eliminate warning 3314 3315 /* "source" 3316 integerArgument <- self popInteger. 3317 integerReceiver <- self popInteger. 3318 self success: integerArgument ~= 0. 3319 self success: integerReceiver \\ integerArgument = 0. 3320 self success 3321 ifTrue: [integerResult <- integerReceiver // integerArgument. 3322 self success: (memory isIntegerValue: integerResult)]. 3323 self success 3324 ifTrue: [ self push: (memory integerObjectOf: integerResult)] 3325 ifFalse: [self unPop: 2] 3326 */ 3327 3328 integerArgument = popInteger(); 3329 integerReceiver = popInteger(); 3330 success(integerArgument != 0); 3331 success(integerReceiver % integerArgument == 0); 3332 if (success()) 3333 { 3334 integerResult = integerReceiver / integerArgument; 3335 success(memory.isIntegerValue(integerResult)); 3336 } 3337 if (success()) 3338 push(memory.integerObjectOf(integerResult)); 3339 else 3340 unPop(2); 3341 } 3342 3343 3344 // primitiveMultiply 3345 void Interpreter::primitiveMultiply() 3346 { 3347 int integerReceiver; 3348 int integerArgument; 3349 int integerResult = 0; 3350 3351 /* "source" 3352 integerArgument <- self popInteger. 3353 integerReceiver <- self popInteger. 3354 self success 3355 ifTrue: [integerResult <- integerReceiver * integerArgument. 3356 self success: (memory isIntegerValue: integerResult)]. 3357 self success 3358 ifTrue: [self pushInteger: integerResult] 3359 ifFalse: [self unPop: 2] 3360 */ 3361 3362 integerArgument = popInteger(); 3363 integerReceiver = popInteger(); 3364 3365 if (success()) 3366 { 3367 integerResult = integerReceiver * integerArgument; 3368 success(memory.isIntegerValue(integerResult)); 3369 } 3370 if (success()) 3371 pushInteger(integerResult); 3372 else 3373 unPop(2); 3374 } 3375 3376 // primitiveBitAnd 3377 void Interpreter::primitiveBitAnd() 3378 { 3379 int integerReceiver; 3380 int integerArgument; 3381 int integerResult; 3382 3383 /* "source" 3384 integerArgument <- self popInteger. 3385 integerReceiver <- self popInteger. 3386 self success 3387 ifTrue: [integerResult <- integerReceiver bitAnd: integerArgument]. 3388 self success 3389 ifTrue: [self pushInteger: integerResult] 3390 ifFalse: [self unPop: 2] 3391 */ 3392 3393 3394 integerArgument = popInteger(); 3395 integerReceiver = popInteger(); 3396 3397 if (success()) 3398 { 3399 integerResult = integerReceiver & integerArgument; 3400 pushInteger(integerResult); 3401 } 3402 else 3403 unPop(2); 3404 } 3405 3406 3407 // primitiveSubtract 3408 void Interpreter::primitiveSubtract() 3409 { 3410 int integerReceiver; 3411 int integerArgument; 3412 int integerResult = 0; 3413 3414 /* "source" 3415 integerArgument <- self popInteger. 3416 integerReceiver <- self popInteger. 3417 self success 3418 ifTrue: [integerResult <- integerReceiver - integerArgument. 3419 self success: (memory isIntegerValue: integerResult)]. 3420 self success 3421 ifTrue: [self pushInteger: integerResult] 3422 ifFalse: [self unPop: 2] 3423 */ 3424 3425 integerArgument = popInteger(); 3426 integerReceiver = popInteger(); 3427 3428 if (success()) 3429 { 3430 integerResult = integerReceiver - integerArgument; 3431 success(memory.isIntegerValue(integerResult)); 3432 } 3433 if (success()) 3434 pushInteger(integerResult); 3435 else 3436 unPop(2); 3437 } 3438 3439 3440 // dispatchIntegerPrimitives 3441 void Interpreter::dispatchIntegerPrimitives() 3442 { 3443 /* "source" 3444 primitiveIndex = 1 ifTrue: [^self primitiveAdd]. 3445 primitiveIndex = 2 ifTrue: [^self primitiveSubtract]. 3446 primitiveIndex = 3 ifTrue: [^self primitiveLessThan]. 3447 primitiveIndex = 4 ifTrue: [^self primitiveGreaterThan]. 3448 primitiveIndex = 5 ifTrue: [^self primitiveLessOrEqual]. 3449 primitiveIndex = 6 ifTrue: [^self primitiveGreaterOrEqual]. 3450 primitiveIndex = 7 ifTrue: [^self primitiveEqual]. 3451 primitiveIndex = 8 ifTrue: [^self primitiveNotEqual]. 3452 primitiveIndex = 9 ifTrue: [^self primitiveMultiply]. 3453 primitiveIndex = 10 ifTrue: [^self primitiveDivide]. 3454 primitiveIndex = 11 ifTrue: [^self primitiveMod]. 3455 primitiveIndex = 12 ifTrue: [^self primitiveDiv]. 3456 primitiveIndex = 13 ifTrue: [^self primitiveQuo]. 3457 primitiveIndex = 14 ifTrue: [^self primitiveBitAnd]. 3458 primitiveIndex = 15 ifTrue: [^self primitiveBitOr]. 3459 primitiveIndex = 16 ifTrue: [^self primitiveBitXor]. 3460 primitiveIndex = 17 ifTrue: [^self primitiveBitShift]. 3461 primitiveIndex = 18 ifTrue: [^self primitiveMakePoint] 3462 */ 3463 3464 switch (primitiveIndex) 3465 { 3466 case 1: primitiveAdd(); break; 3467 case 2: primitiveSubtract(); break; 3468 case 3: primitiveLessThan(); break; 3469 case 4: primitiveGreaterThan(); break; 3470 case 5: primitiveLessOrEqual(); break; 3471 case 6: primitiveGreaterOrEqual(); break; 3472 case 7: primitiveEqual(); break; 3473 case 8: primitiveNotEqual(); break; 3474 case 9: primitiveMultiply(); break; 3475 case 10: primitiveDivide(); break; 3476 case 11: primitiveMod(); break; 3477 case 12: primitiveDiv(); break; 3478 case 13: primitiveQuo(); break; 3479 case 14: primitiveBitAnd(); break; 3480 case 15: primitiveBitOr(); break; 3481 case 16: primitiveBitXor(); break; 3482 case 17: primitiveBitShift(); break; 3483 case 18: primitiveMakePoint(); break; 3484 } 3485 } 3486 3487 3488 // primitiveGreaterOrEqual 3489 void Interpreter::primitiveGreaterOrEqual() 3490 { 3491 int integerReceiver; 3492 int integerArgument; 3493 3494 /* "source" 3495 integerArgument <- self popInteger. 3496 integerReceiver <- self popInteger. 3497 self success 3498 ifTrue: [integerReceiver >= integerArgument 3499 ifTrue: [self push: TruePointer] 3500 ifFalse: [self push: FalsePointer]] 3501 ifFalse: [self unPop: 2] 3502 */ 3503 integerArgument = popInteger(); 3504 integerReceiver = popInteger(); 3505 3506 if (success()) 3507 { 3508 if (integerReceiver >= integerArgument) 3509 push(TruePointer); 3510 else 3511 push(FalsePointer); 3512 } 3513 else 3514 unPop(2); 3515 } 3516 3517 3518 // primitiveAdd 3519 void Interpreter::primitiveAdd() 3520 { 3521 int integerReceiver; 3522 int integerArgument; 3523 int integerResult = 0; 3524 3525 /* "source" 3526 integerArgument <- self popInteger. 3527 integerReceiver <- self popInteger. 3528 self success 3529 ifTrue: [integerResult <- integerReceiver + integerArgument. 3530 self success: (memory isIntegerValue: integerResult)]. 3531 self success 3532 ifTrue: [self pushInteger: integerResult] 3533 ifFalse: [self unPop: 2] 3534 */ 3535 integerArgument = popInteger(); 3536 integerReceiver = popInteger(); 3537 3538 if (success()) 3539 { 3540 integerResult = integerReceiver + integerArgument; 3541 success(memory.isIntegerValue(integerResult)); 3542 } 3543 if (success()) 3544 pushInteger(integerResult); 3545 else 3546 unPop(2); 3547 } 3548 3549 3550 // primitiveNotEqual 3551 void Interpreter::primitiveNotEqual() 3552 { 3553 int integerReceiver; 3554 int integerArgument; 3555 3556 /* "source" 3557 integerArgument <- self popInteger. 3558 integerReceiver <- self popInteger. 3559 self success 3560 ifTrue: [integerReceiver ~= integerArgument 3561 ifTrue: [self push: TruePointer] 3562 ifFalse: [self push: FalsePointer]] 3563 ifFalse: [self unPop: 2] 3564 */ 3565 integerArgument = popInteger(); 3566 integerReceiver = popInteger(); 3567 3568 if (success()) 3569 { 3570 if (integerReceiver != integerArgument) 3571 push(TruePointer); 3572 else 3573 push(FalsePointer); 3574 } 3575 else 3576 unPop(2); 3577 } 3578 3579 3580 // primitiveQuo 3581 void Interpreter::primitiveQuo() 3582 { 3583 int integerReceiver; 3584 int integerArgument; 3585 int integerResult = 0; 3586 3587 /* "source" 3588 integerArgument <- self popInteger. 3589 integerReceiver <- self popInteger. 3590 self success: integerArgument ~= 0. 3591 self success 3592 ifTrue: [integerResult <- integerReceiver quo: integerArgument. 3593 self success: (memory isIntegerValue: integerResult)]. 3594 self success 3595 ifTrue: [self pushInteger: integerResult] 3596 ifFalse: [self unPop: 2] 3597 */ 3598 integerArgument = popInteger(); 3599 integerReceiver = popInteger(); 3600 success(integerArgument != 0); 3601 3602 if (success()) 3603 { 3604 integerResult = integerReceiver / integerArgument; 3605 success(memory.isIntegerValue(integerResult)); 3606 } 3607 if (success()) 3608 push(memory.integerObjectOf(integerResult)); 3609 else 3610 unPop(2); 3611 } 3612 3613 3614 // dispatchFloatPrimitives 3615 void Interpreter::dispatchFloatPrimitives() 3616 { 3617 /* "source" 3618 primitiveIndex = 40 ifTrue: [^self primitiveAsFloat]. 3619 primitiveIndex = 41 ifTrue: [^self primitiveFloatAdd]. 3620 primitiveIndex = 42 ifTrue: [^self primitiveFloatSubtract]. 3621 primitiveIndex = 43 ifTrue: [^self primitiveFloatLessThan]. 3622 primitiveIndex = 44 ifTrue: [^self primitiveFloatGreaterThan]. 3623 primitiveIndex = 45 ifTrue: [^self primitiveFloatLessOrEqual]. 3624 primitiveIndex = 46 ifTrue: [^self primitiveFloatGreaterOrEqual]. 3625 primitiveIndex = 47 ifTrue: [^self primitiveFloatEqual]. 3626 primitiveIndex = 48 ifTrue: [^self primitiveFloatNotEqual]. 3627 primitiveIndex = 49 ifTrue: [^self primitiveFloatMultiply]. 3628 primitiveIndex = 50 ifTrue: [^self primitiveFloatDivide]. 3629 primitiveIndex = 51 ifTrue: [^self primitiveTruncated]. 3630 primitiveIndex = 52 ifTrue: [^self primitiveFractionalPart]. 3631 primitiveIndex = 53 ifTrue: [^self primitiveExponent]. 3632 primitiveIndex = 54 ifTrue: [^self primitiveTimesTwoPower] 3633 */ 3634 3635 switch (primitiveIndex) 3636 { 3637 case 40: primitiveAsFloat(); break; 3638 case 41: primitiveFloatAdd(); break; 3639 case 42: primitiveFloatSubtract(); break; 3640 case 43: primitiveFloatLessThan(); break; 3641 case 44: primitiveFloatGreaterThan(); break; 3642 case 45: primitiveFloatLessOrEqual(); break; 3643 case 46: primitiveFloatGreaterOrEqual(); break; 3644 case 47: primitiveFloatEqual(); break; 3645 case 48: primitiveFloatNotEqual(); break; 3646 case 49: primitiveFloatMultiply(); break; 3647 case 50: primitiveFloatDivide(); break; 3648 case 51: primitiveTruncated(); break; 3649 case 52: primitiveFractionalPart(); break; 3650 case 53: primitiveExponent(); break; 3651 case 54: primitiveTimesTwoPower(); break; 3652 } 3653 3654 } 3655 3656 // The primitiveAsFloat routine converts its SmallInteger receiver into a Float. 3657 void Interpreter::primitiveAsFloat() 3658 { 3659 int integerArgument = popInteger(); 3660 if (success()) 3661 { 3662 pushFloat((float) integerArgument); 3663 } 3664 else 3665 unPop(1); 3666 } 3667 3668 void Interpreter::primitiveFloatAdd() 3669 { 3670 float floatReceiver; 3671 float floatArgument; 3672 float floatResult; 3673 3674 floatArgument = popFloat(); 3675 floatReceiver = popFloat(); 3676 if (success()) 3677 { 3678 floatResult = floatReceiver + floatArgument; 3679 pushFloat(floatResult); 3680 } 3681 else 3682 unPop(2); 3683 } 3684 3685 void Interpreter::primitiveFloatSubtract() 3686 { 3687 float floatReceiver; 3688 float floatArgument; 3689 float floatResult; 3690 3691 floatArgument = popFloat(); 3692 floatReceiver = popFloat(); 3693 if (success()) 3694 { 3695 floatResult = floatReceiver - floatArgument; 3696 pushFloat(floatResult); 3697 } 3698 else 3699 unPop(2); 3700 } 3701 3702 void Interpreter::primitiveFloatLessThan() 3703 { 3704 float floatReceiver; 3705 float floatArgument; 3706 3707 floatArgument = popFloat(); 3708 floatReceiver = popFloat(); 3709 3710 if (success()) 3711 { 3712 if (floatReceiver < floatArgument) 3713 push(TruePointer); 3714 else 3715 push(FalsePointer); 3716 } 3717 else 3718 unPop(2); 3719 } 3720 3721 void Interpreter::primitiveFloatGreaterThan() 3722 { 3723 float floatReceiver; 3724 float floatArgument; 3725 3726 floatArgument = popFloat(); 3727 floatReceiver = popFloat(); 3728 3729 if (success()) 3730 { 3731 if (floatReceiver > floatArgument) 3732 push(TruePointer); 3733 else 3734 push(FalsePointer); 3735 } 3736 else 3737 unPop(2); 3738 } 3739 3740 void Interpreter::primitiveFloatLessOrEqual() 3741 { 3742 float floatReceiver; 3743 float floatArgument; 3744 3745 floatArgument = popFloat(); 3746 floatReceiver = popFloat(); 3747 3748 if (success()) 3749 { 3750 if (floatReceiver <= floatArgument) 3751 push(TruePointer); 3752 else 3753 push(FalsePointer); 3754 } 3755 else 3756 unPop(2); 3757 } 3758 3759 void Interpreter::primitiveFloatGreaterOrEqual() 3760 { 3761 float floatReceiver; 3762 float floatArgument; 3763 3764 floatArgument = popFloat(); 3765 floatReceiver = popFloat(); 3766 3767 if (success()) 3768 { 3769 if (floatReceiver >= floatArgument) 3770 push(TruePointer); 3771 else 3772 push(FalsePointer); 3773 } 3774 else 3775 unPop(2); 3776 } 3777 3778 void Interpreter::primitiveFloatEqual() 3779 { 3780 float floatReceiver; 3781 float floatArgument; 3782 3783 floatArgument = popFloat(); 3784 floatReceiver = popFloat(); 3785 3786 if (success()) 3787 { 3788 if (floatReceiver == floatArgument) 3789 push(TruePointer); 3790 else 3791 push(FalsePointer); 3792 } 3793 else 3794 unPop(2); 3795 } 3796 3797 void Interpreter::primitiveFloatNotEqual() 3798 { 3799 float floatReceiver; 3800 float floatArgument; 3801 3802 floatArgument = popFloat(); 3803 floatReceiver = popFloat(); 3804 3805 if (success()) 3806 { 3807 if (floatReceiver != floatArgument) 3808 push(TruePointer); 3809 else 3810 push(FalsePointer); 3811 } 3812 else 3813 unPop(2); 3814 } 3815 3816 void Interpreter::primitiveFloatMultiply() 3817 { 3818 float floatReceiver; 3819 float floatArgument; 3820 float floatResult; 3821 3822 floatArgument = popFloat(); 3823 floatReceiver = popFloat(); 3824 if (success()) 3825 { 3826 floatResult = floatReceiver * floatArgument; 3827 pushFloat(floatResult); 3828 } 3829 else 3830 unPop(2); 3831 } 3832 3833 void Interpreter::primitiveFloatDivide() 3834 { 3835 float floatReceiver; 3836 float floatArgument; 3837 float floatResult; 3838 3839 floatArgument = popFloat(); 3840 floatReceiver = popFloat(); 3841 success(floatArgument != 0); 3842 if (success()) 3843 { 3844 floatResult = floatReceiver / floatArgument; 3845 pushFloat(floatResult); 3846 } 3847 else 3848 unPop(2); 3849 } 3850 3851 void Interpreter::primitiveTruncated() 3852 { 3853 float floatReceiver; 3854 int integerResult = 0; 3855 floatReceiver = popFloat(); 3856 if (success()) 3857 { 3858 integerResult = (int) floatReceiver; 3859 success(memory.isIntegerValue(integerResult)); 3860 } 3861 3862 if (success()) 3863 { 3864 pushInteger(integerResult); 3865 } 3866 else 3867 unPop(1); 3868 } 3869 3870 void Interpreter::primitiveFractionalPart() 3871 { 3872 float floatReceiver; 3873 float floatResult; 3874 floatReceiver = popFloat(); 3875 if (success()) 3876 { 3877 floatResult = floatReceiver - (int) floatReceiver; 3878 pushFloat(floatResult); 3879 } 3880 else 3881 unPop(1); 3882 } 3883 3884 // primitiveLessOrEqual 3885 void Interpreter::primitiveLessOrEqual() 3886 { 3887 int integerReceiver; 3888 int integerArgument; 3889 3890 /* "source" 3891 integerArgument <- self popInteger. 3892 integerReceiver <- self popInteger. 3893 self success 3894 ifTrue: [integerReceiver <= integerArgument 3895 ifTrue: [self push: TruePointer] 3896 ifFalse: [self push: FalsePointer]] 3897 ifFalse: [self unPop: 2] 3898 */ 3899 integerArgument = popInteger(); 3900 integerReceiver = popInteger(); 3901 3902 if (success()) 3903 { 3904 if (integerReceiver <= integerArgument) 3905 push(TruePointer); 3906 else 3907 push(FalsePointer); 3908 } 3909 else 3910 unPop(2); 3911 } 3912 3913 3914 // primitiveMakePoint 3915 void Interpreter::primitiveMakePoint() 3916 { 3917 int integerReceiver; 3918 int integerArgument; 3919 int pointResult; 3920 3921 /* "source" 3922 3923 integerArgument <- self popStack. 3924 integerReceiver <- self popStack. 3925 self success: (memory isIntegerValue: integerReceiver). 3926 self success: (memory isIntegerValue: integerArgument)." 3927 integerArgument <- self popInteger. 3928 integerReceiver <- self popInteger. 3929 self success 3930 ifTrue: [pointResult <- memory instantiateClass: ClassPointPointer 3931 withPointers: ClassPointSize. 3932 "ERROR: dbanay next two changed to be storeInteger:ofObject:withValue" 3933 self storeInteger: XIndex 3934 ofObject: pointResult 3935 withValue: integerReceiver. 3936 self storeInteger: YIndex 3937 ofObject: pointResult 3938 withValue: integerArgument. 3939 self push: pointResult] 3940 ifFalse: [self unPop: 2] 3941 */ 3942 integerArgument = popInteger(); 3943 integerReceiver = popInteger(); 3944 success(memory.isIntegerValue(integerReceiver)); 3945 success(memory.isIntegerValue(integerArgument)); 3946 if (success()) 3947 { 3948 pointResult = memory.instantiateClass_withPointers(ClassPointPointer, ClassPointSize); 3949 storeInteger_ofObject_withValue(XIndex, pointResult, integerReceiver); 3950 storeInteger_ofObject_withValue(YIndex, pointResult, integerArgument); 3951 push(pointResult); 3952 } 3953 else 3954 unPop(2); 3955 } 3956 3957 3958 // primitiveBitXor 3959 void Interpreter::primitiveBitXor() 3960 { 3961 int integerReceiver; 3962 int integerArgument; 3963 int integerResult; 3964 3965 /* "source" 3966 integerArgument <- self popInteger. 3967 integerReceiver <- self popInteger. 3968 self success 3969 ifTrue: [integerResult <- integerReceiver bitXor: integerArgument]. 3970 self success 3971 ifTrue: [self pushInteger: integerResult] 3972 ifFalse: [self unPop: 2] 3973 */ 3974 integerArgument = popInteger(); 3975 integerReceiver = popInteger(); 3976 3977 if (success()) 3978 { 3979 integerResult = integerReceiver ^ integerArgument; 3980 pushInteger(integerResult); 3981 } 3982 else 3983 unPop(2); 3984 } 3985 3986 3987 // primitiveLessThan 3988 void Interpreter::primitiveLessThan() 3989 { 3990 int integerReceiver; 3991 int integerArgument; 3992 3993 /* "source" 3994 integerArgument <- self popInteger. 3995 integerReceiver <- self popInteger. 3996 self success 3997 ifTrue: [integerReceiver < integerArgument 3998 ifTrue: [self push: TruePointer] 3999 ifFalse: [self push: FalsePointer]] 4000 ifFalse: [self unPop: 2] 4001 */ 4002 integerArgument = popInteger(); 4003 integerReceiver = popInteger(); 4004 4005 if (success()) 4006 { 4007 if (integerReceiver < integerArgument) 4008 push(TruePointer); 4009 else 4010 push(FalsePointer); 4011 } 4012 else 4013 unPop(2); 4014 4015 4016 } 4017 4018 4019 // primitiveBitShift 4020 void Interpreter::primitiveBitShift() 4021 { 4022 int integerReceiver; 4023 int integerArgument; 4024 int integerResult = 0; 4025 4026 /* "source" 4027 integerArgument <- self popInteger. 4028 integerReceiver <- self popInteger. 4029 self success 4030 ifTrue: [integerResult <- integerReceiver bitShift: integerArgument. 4031 self success: (memory isIntegerValue: integerResult)]. 4032 self success 4033 ifTrue: [self pushInteger: integerResult] 4034 ifFalse: [self unPop: 2] 4035 */ 4036 integerArgument = popInteger(); 4037 integerReceiver = popInteger(); 4038 if (success()) 4039 { 4040 integerResult = integerArgument >= 0 ? integerReceiver << integerArgument : integerReceiver >> -integerArgument; 4041 success(memory.isIntegerValue(integerResult)); 4042 } 4043 if (success()) 4044 pushInteger(integerResult); 4045 else 4046 unPop(2); 4047 } 4048 4049 4050 // primitiveGreaterThan 4051 void Interpreter::primitiveGreaterThan() 4052 { 4053 int integerReceiver; 4054 int integerArgument; 4055 4056 /* "source" 4057 integerArgument <- self popInteger. 4058 integerReceiver <- self popInteger. 4059 self success 4060 ifTrue: [integerReceiver > integerArgument 4061 ifTrue: [self push: TruePointer] 4062 ifFalse: [self push: FalsePointer]] 4063 ifFalse: [self unPop: 2] 4064 */ 4065 integerArgument = popInteger(); 4066 integerReceiver = popInteger(); 4067 4068 if (success()) 4069 { 4070 if (integerReceiver > integerArgument) 4071 push(TruePointer); 4072 else 4073 push(FalsePointer); 4074 } 4075 else 4076 unPop(2); 4077 } 4078 4079 4080 // primitiveDiv 4081 void Interpreter::primitiveDiv() 4082 { 4083 int integerReceiver; 4084 int integerArgument; 4085 int integerResult = 0; 4086 4087 /* "source" 4088 integerArgument <- self popInteger. 4089 integerReceiver <- self popInteger. 4090 self success: integerArgument ~= 0. 4091 self success 4092 ifTrue: [integerResult <- integerReceiver // integerArgument. 4093 self success: (memory isIntegerValue: integerResult)]. 4094 self success 4095 ifTrue: [self pushInteger: integerResult] 4096 ifFalse: [self unPop: 2] 4097 */ 4098 integerArgument = popInteger(); 4099 integerReceiver = popInteger(); 4100 success(integerArgument != 0); 4101 if (success()) 4102 { 4103 integerResult = integerReceiver / integerArgument; 4104 if (integerReceiver % integerArgument != 0) 4105 { 4106 // Smalltalk truncates to negative infinity ( -7 // 2 == -4 not -3) 4107 if (integerResult < 0) 4108 integerResult--; 4109 } 4110 success(memory.isIntegerValue(integerResult)); 4111 } 4112 if (success()) 4113 push(memory.integerObjectOf(integerResult)); 4114 else 4115 unPop(2); 4116 4117 } 4118 4119 std::string Interpreter::stringFromObject(int stringOrSymbolPointer) 4120 { 4121 4122 int len = memory.fetchByteLengthOf(stringOrSymbolPointer); 4123 std::string s; 4124 s.reserve(len); 4125 4126 for(int i = 0; i < len; i++) 4127 { 4128 std::uint8_t ch = memory.fetchByte_ofObject(i, stringOrSymbolPointer); 4129 s.append(1, ch); 4130 } 4131 return s; 4132 } 4133 4134 int Interpreter::stringObjectFor(const char *s) 4135 { 4136 if (!s) 4137 return NilPointer; 4138 4139 const char *p = s; 4140 while (*p) 4141 p++; 4142 int length = (int) (p - s); 4143 int objectPointer = memory.instantiateClass_withBytes(ClassStringPointer, length); 4144 p = s; 4145 for(int i = 0; i < length; i++) 4146 { 4147 memory.storeByte_ofObject_withValue(i, objectPointer, *p++); 4148 } 4149 return objectPointer; 4150 } 4151 4152 4153 #ifdef DEBUGGING_SUPPORT 4154 4155 std::string Interpreter::selectorName(int selector) 4156 { 4157 return stringFromObject(selector); 4158 } 4159 4160 std::string Interpreter::classNameOfObject(int objectPointer) 4161 { 4162 return className(memory.fetchClassOf(objectPointer)); 4163 } 4164 4165 std::string Interpreter::className(int classPointer) 4166 { 4167 if (classPointer == ClassSmallInteger) 4168 return "SmallInteger"; 4169 4170 if (classPointer == NilPointer) 4171 return "UndefinedObject"; 4172 4173 int symbol = memory.fetchPointer_ofObject(6, classPointer); 4174 if (memory.fetchClassOf(symbol) != ClassSymbolPointer) 4175 return "<unknown>"; 4176 4177 return stringFromObject(symbol); 4178 } 4179 #endif 4180 4181 // sendSelector:argumentCount: 4182 void Interpreter::sendSelector_argumentCount(int selector, int count) 4183 { 4184 int newReceiver; 4185 4186 /* "source" 4187 messageSelector <- selector. 4188 argumentCount <- count. 4189 newReceiver <- self stackValue: argumentCount. 4190 self sendSelectorToClass: (memory fetchClassOf: newReceiver) 4191 */ 4192 messageSelector = selector; 4193 argumentCount = count; 4194 newReceiver = stackValue(argumentCount); 4195 4196 #if 0 4197 #ifdef DEBUGGING_SUPPORT 4198 // Debugging aid that helped me figure out why stuff 4199 // wasn't working - intercept show: and error: messages and 4200 // display them 4201 const int showSelector = 6640; // #show: 4202 const int errorSelector = 282; // #error: 4203 #if 0 4204 bool showSend = true; 4205 4206 if (selector == showSelector) 4207 { 4208 int s = stackValue(argumentCount-1); 4209 assert(memory.fetchClassOf(s) == ClassStringPointer); 4210 std::string text = stringFromObject(s); 4211 printf("Transcript: %s\n", text.c_str()); 4212 showSend = false; 4213 } 4214 else if (selector == errorSelector) 4215 { 4216 int s = stackValue(argumentCount-1); 4217 assert(memory.fetchClassOf(s) == ClassStringPointer); 4218 std::string text = stringFromObject(s); 4219 printf("ERROR: %s\n", text.c_str()); 4220 showSend = false; 4221 } 4222 4223 if (showSend) 4224 { 4225 int cls = memory.fetchClassOf(newReceiver); 4226 std::string className = classNameOfObject(newReceiver); 4227 std::string sel = selectorName(selector); 4228 printf("sendSelector %s [%d] (args = %d) to %d (%s) 0x%d\n", 4229 sel.c_str(), selector, count, newReceiver, className.c_str(), cls); 4230 } 4231 #endif 4232 #endif 4233 #endif 4234 sendSelectorToClass(memory.fetchClassOf(newReceiver)); 4235 } 4236 4237 4238 // findNewMethodInClass: 4239 void Interpreter::findNewMethodInClass(int cls) 4240 { 4241 int hash; 4242 4243 /* "source" 4244 hash <- (((messageSelector bitAnd: class) bitAnd: 16rFF) bitShift: 2) + 1. 4245 ((methodCache at: hash) = messageSelector 4246 and: [(methodCache at: hash + 1) = class]) 4247 ifTrue: [newMethod <- methodCache at: hash + 2. 4248 primitiveIndex <- methodCache at: hash + 3] 4249 ifFalse: [self lookupMethodInClass: class. 4250 methodCache at: hash put: messageSelector. 4251 methodCache at: hash + 1 put: class. 4252 methodCache at: hash + 2 put: newMethod. 4253 methodCache at: hash + 3 put: primitiveIndex] 4254 */ 4255 4256 // This is not a great hash function 4257 // Bits of History, pg.244 has a discussion and a better one 4258 // hash = ((messageSelector ^ cls) & 0xff) << 2; 4259 4260 hash = (messageSelector & cls & 0xff) << 2; // removed +1 since C arrays are zero based 4261 if (methodCache[hash] == messageSelector && methodCache[hash+1] == cls) 4262 { 4263 newMethod = methodCache[hash+2]; 4264 primitiveIndex = methodCache[hash+3]; 4265 } 4266 else 4267 { 4268 4269 lookupMethodInClass(cls); 4270 methodCache[hash] = messageSelector; 4271 methodCache[hash+1] = cls; 4272 methodCache[hash+2] = newMethod; 4273 methodCache[hash+3] = primitiveIndex; 4274 } 4275 4276 } 4277 4278 4279 // activateNewMethod 4280 void Interpreter::activateNewMethod() 4281 { 4282 int contextSize; 4283 int newContext; 4284 4285 /* "source" 4286 (self largeContextFlagOf: newMethod) = 1 4287 ifTrue: [contextSize <- 32 + TempFrameStart] 4288 ifFalse: [contextSize <- 12 + TempFrameStart]. 4289 newContext <- memory instantiateClass: ClassMethodContextPointer 4290 withPointers: contextSize. 4291 memory storePointer: SenderIndex 4292 ofObject: newContext 4293 withValue: activeContext. 4294 self storeInstructionPointerValue: 4295 (self initialInstructionPointerOfMethod: newMethod) 4296 inContext: newContext. 4297 self storeStackPointerValue: (self temporaryCountOf: newMethod) 4298 inContext: newContext. 4299 memory storePointer: MethodIndex 4300 ofObject: newContext 4301 withValue: newMethod. 4302 self transfer: argumentCount + 1 4303 fromIndex: stackPointer - argumentCount 4304 ofObject: activeContext 4305 toIndex: ReceiverIndex 4306 ofObject: newContext. 4307 self pop: argumentCount + 1. 4308 self newActiveContext: newContext 4309 */ 4310 4311 if (largeContextFlagOf(newMethod) == 1) 4312 contextSize = 32 + TempFrameStart; 4313 else 4314 contextSize = 12 + TempFrameStart; 4315 newContext = memory.instantiateClass_withPointers( 4316 ClassMethodContextPointer, contextSize); 4317 memory.storePointer_ofObject_withValue(SenderIndex, 4318 newContext, activeContext); 4319 storeInstructionPointerValue_inContext( 4320 initialInstructionPointerOfMethod(newMethod), newContext); 4321 storeStackPointerValue_inContext(temporaryCountOf(newMethod), newContext); 4322 memory.storePointer_ofObject_withValue(MethodIndex, newContext, newMethod); 4323 transfer_fromIndex_ofObject_toIndex_ofObject(argumentCount + 1, 4324 stackPointer - argumentCount, 4325 activeContext, 4326 ReceiverIndex, 4327 newContext); 4328 pop(argumentCount + 1); 4329 newActiveContext(newContext); 4330 } 4331 4332 4333 // sendSpecialSelectorBytecode 4334 void Interpreter::sendSpecialSelectorBytecode() 4335 { 4336 int selectorIndex; 4337 int selector; 4338 int count; 4339 4340 /* "source" 4341 self specialSelectorPrimitiveResponse 4342 ifFalse: [selectorIndex <- (currentBytecode - 176) * 2. 4343 selector <- memory fetchPointer: selectorIndex 4344 ofObject: SpecialSelectorsPointer. 4345 count <- self fetchInteger: selectorIndex + 1 4346 ofObject: SpecialSelectorsPointer. 4347 self sendSelector: selector 4348 argumentCount: count] 4349 */ 4350 4351 if (!specialSelectorPrimitiveResponse()) 4352 { 4353 selectorIndex = (currentBytecode - 176) * 2; 4354 selector = memory.fetchPointer_ofObject(selectorIndex, SpecialSelectorsPointer); 4355 count = fetchInteger_ofObject(selectorIndex + 1, SpecialSelectorsPointer); 4356 sendSelector_argumentCount(selector, count); 4357 } 4358 } 4359 4360 4361 // doubleExtendedSuperBytecode 4362 void Interpreter::doubleExtendedSuperBytecode() 4363 { 4364 int methodClass; 4365 4366 /* "source" 4367 argumentCount <- self fetchByte. 4368 messageSelector <- self literal: self fetchByte. 4369 methodClass <- self methodClassOf: method. 4370 self sendSelectorToClass: (self superclassOf: methodClass) 4371 */ 4372 argumentCount = fetchByte(); 4373 messageSelector = literal(fetchByte()); 4374 methodClass = methodClassOf(method); 4375 sendSelectorToClass(superclassOf(methodClass)); 4376 } 4377 4378 4379 // sendBytecode 4380 void Interpreter::sendBytecode() 4381 { 4382 /* "source" 4383 (currentBytecode between: 131 and: 134) 4384 ifTrue: [^self extendedSendBytecode]. 4385 (currentBytecode between: 176 and: 207) 4386 ifTrue: [^self sendSpecialSelectorBytecode]. 4387 (currentBytecode between: 208 and: 255) 4388 ifTrue: [^self sendLiteralSelectorBytecode] 4389 */ 4390 if (between_and(currentBytecode, 131, 134)) 4391 extendedSendBytecode(); 4392 else if (between_and(currentBytecode, 176, 207)) 4393 sendSpecialSelectorBytecode(); 4394 else if (between_and(currentBytecode, 208, 255)) 4395 sendLiteralSelectorBytecode(); 4396 4397 } 4398 4399 4400 // doubleExtendedSendBytecode 4401 void Interpreter::doubleExtendedSendBytecode() 4402 { 4403 int count; 4404 int selector; 4405 4406 /* "source" 4407 count <- self fetchByte. 4408 selector <- self literal: self fetchByte. 4409 self sendSelector: selector 4410 argumentCount: count 4411 */ 4412 4413 count = fetchByte(); 4414 selector = literal(fetchByte()); 4415 sendSelector_argumentCount(selector, count); 4416 } 4417 4418 4419 // sendSelectorToClass: 4420 void Interpreter::sendSelectorToClass(int classPointer) 4421 { 4422 /* "source" 4423 self findNewMethodInClass: classPointer. 4424 self executeNewMethod 4425 */ 4426 findNewMethodInClass(classPointer); 4427 executeNewMethod(); 4428 } 4429 4430 4431 // sendLiteralSelectorBytecode 4432 void Interpreter::sendLiteralSelectorBytecode() 4433 { 4434 int selector; 4435 4436 /* "source" 4437 selector <- self literal: (self extractBits: 12 to: 15 4438 of: currentBytecode). 4439 self sendSelector: selector 4440 argumentCount: (self extractBits: 10 to: 11 4441 of: currentBytecode) - 1 4442 */ 4443 selector = literal(extractBits_to_of(12, 15, currentBytecode)); 4444 sendSelector_argumentCount(selector, extractBits_to_of(10, 11, currentBytecode) - 1); 4445 } 4446 4447 4448 // singleExtendedSuperBytecode 4449 void Interpreter::singleExtendedSuperBytecode() 4450 { 4451 int descriptor; 4452 int selectorIndex; 4453 int methodClass; 4454 4455 /* "source" 4456 descriptor <- self fetchByte. 4457 argumentCount <- self extractBits: 8 to: 10 4458 of: descriptor. 4459 selectorIndex <- self extractBits: 11 to: 15 4460 of: descriptor. 4461 messageSelector <- self literal: selectorIndex. 4462 methodClass <- self methodClassOf: method. 4463 self sendSelectorToClass: (self superclassOf: methodClass) 4464 */ 4465 4466 descriptor = fetchByte(); 4467 argumentCount = extractBits_to_of(8, 10, descriptor); 4468 selectorIndex = extractBits_to_of(11, 15, descriptor); 4469 messageSelector = literal(selectorIndex); 4470 methodClass = methodClassOf(method); 4471 sendSelectorToClass(superclassOf(methodClass)); 4472 } 4473 4474 4475 // singleExtendedSendBytecode 4476 void Interpreter::singleExtendedSendBytecode() 4477 { 4478 int descriptor; 4479 int selectorIndex; 4480 4481 /* "source" 4482 descriptor <- self fetchByte. 4483 selectorIndex <- self extractBits: 11 to: 15 4484 of: descriptor. 4485 self sendSelector: (self literal: selectorIndex) 4486 argumentCount: (self extractBits: 8 to: 10 4487 of: descriptor) 4488 */ 4489 descriptor = fetchByte(); 4490 selectorIndex = extractBits_to_of(11, 15, descriptor); 4491 sendSelector_argumentCount(literal(selectorIndex), extractBits_to_of(8, 10, descriptor)); 4492 } 4493 4494 4495 // extendedSendBytecode 4496 void Interpreter::extendedSendBytecode() 4497 { 4498 /* "source" 4499 currentBytecode = 131 ifTrue: [^self singleExtendedSendBytecode]. 4500 currentBytecode = 132 ifTrue: [^self doubleExtendedSendBytecode]. 4501 currentBytecode = 133 ifTrue: [^self singleExtendedSuperBytecode]. 4502 currentBytecode = 134 ifTrue: [^self doubleExtendedSuperBytecode] 4503 */ 4504 4505 switch (currentBytecode) 4506 { 4507 case 131: singleExtendedSendBytecode(); break; 4508 case 132: doubleExtendedSendBytecode(); break; 4509 case 133: singleExtendedSuperBytecode(); break; 4510 case 134: doubleExtendedSuperBytecode(); break; 4511 } 4512 } 4513 4514 4515 // executeNewMethod 4516 void Interpreter::executeNewMethod() 4517 { 4518 /* "source" 4519 self primitiveResponse 4520 ifFalse: [self activateNewMethod] 4521 */ 4522 if (!primitiveResponse()) 4523 activateNewMethod(); 4524 } 4525 4526 4527 // dispatchOnThisBytecode 4528 void Interpreter::dispatchOnThisBytecode() 4529 { 4530 /* "source" 4531 (currentBytecode between: 0 and: 119) ifTrue: [^self stackBytecode]. 4532 (currentBytecode between: 120 and: 127) ifTrue: [^self returnBytecode]. 4533 (currentBytecode between: 128 and: 130) ifTrue: [^self stackBytecode]. 4534 (currentBytecode between: 131 and: 134 ifTrue: [^self sendBytecode]. 4535 (currentBytecode between: 135 and: 137) ifTrue: [^self stackBytecode]. 4536 (currentBytecode between: 144 and: 175) ifTrue: [^self jumpBytecode]. 4537 (currentBytecode between: 176 and: 255) ifTrue: [^self sendBytecode] 4538 */ 4539 if (between_and(currentBytecode, 0, 119)) { stackBytecode(); } 4540 else if (between_and(currentBytecode, 120, 127)) { returnBytecode(); } 4541 else if (between_and(currentBytecode, 128, 130)) { stackBytecode(); } 4542 else if (between_and(currentBytecode, 131, 134)) { sendBytecode(); } 4543 else if (between_and(currentBytecode, 135, 137)) { stackBytecode(); } 4544 else if (between_and(currentBytecode, 144, 175)) { jumpBytecode(); } 4545 else if (between_and(currentBytecode, 176, 255)) { sendBytecode(); } 4546 } 4547 4548 4549 // fetchByte 4550 int Interpreter::fetchByte() 4551 { 4552 int byte; 4553 4554 /* "source" 4555 byte <- memory fetchByte: instructionPointer 4556 ofObject: method. 4557 instructionPointer <- instructionPointer + 1. 4558 ^byte 4559 */ 4560 4561 byte = memory.fetchByte_ofObject(instructionPointer, method); 4562 instructionPointer = instructionPointer + 1; 4563 return byte; 4564 } 4565 4566 4567 // cycle 4568 void Interpreter::cycle() 4569 { 4570 /* "source" 4571 self checkProcessSwitch. 4572 currentBytecode <- self fetchByte. 4573 self dispatchOnThisBytecode 4574 */ 4575 4576 checkProcessSwitch(); 4577 currentBytecode = fetchByte(); 4578 dispatchOnThisBytecode(); 4579 } 4580 4581 4582 4583 // interpret 4584 void Interpreter::interpret() 4585 { 4586 /* "source" 4587 [true] whileTrue: [self cycle] 4588 */ 4589 for(;;) 4590 { 4591 cycle(); 4592 } 4593 } 4594 4595 4596 // primitiveIndexOf: 4597 int Interpreter::primitiveIndexOf(int methodPointer) 4598 { 4599 int flagValue; 4600 4601 /* "source" 4602 flagValue <- self flagValueOf: methodPointer. 4603 flagValue=7 4604 ifTrue: [^self extractBits: 7 to: 14 4605 of: (self headerExtensionOf: methodPointer)] 4606 ifFalse: [^0] 4607 */ 4608 flagValue = flagValueOf(methodPointer); 4609 if (flagValue==7) 4610 { 4611 return extractBits_to_of(7, 14, headerExtensionOf(methodPointer)); 4612 } 4613 return 0; 4614 } 4615 4616 4617 // argumentCountOf: 4618 int Interpreter::argumentCountOf(int methodPointer) 4619 { 4620 int flagValue; 4621 4622 /* "source" 4623 flagValue <- self flagValueOf: methodPointer. 4624 flagValue < 5 4625 ifTrue: [^flagValue]. 4626 flagValue < 7 4627 ifTrue: [^0] 4628 ifFalse: [^self extractBits: 2 to: 6 4629 of: (self headerExtensionOf: methodPointer)] 4630 */ 4631 flagValue = flagValueOf(methodPointer); 4632 if (flagValue < 5) 4633 return flagValue; 4634 if (flagValue < 7) 4635 return 0; 4636 else 4637 return extractBits_to_of(2, 6, headerExtensionOf(methodPointer)); 4638 } 4639 4640 // methodClassOf: 4641 int Interpreter::methodClassOf(int methodPointer) 4642 { 4643 int literalCount; 4644 int association; 4645 4646 /* "source" 4647 literalCount <- self literalCountOf: methodPointer. 4648 association <- self literal: literalCount - 1 4649 ofMethod: methodPointer. 4650 ^memory fetchPointer: ValueIndex 4651 ofObject: association 4652 */ 4653 literalCount = literalCountOf(methodPointer); 4654 association = literal_ofMethod(literalCount - 1, methodPointer); 4655 return memory.fetchPointer_ofObject(ValueIndex, association); 4656 } 4657 4658 4659 // storeAndPopReceiverVariableBytecode 4660 void Interpreter::storeAndPopReceiverVariableBytecode() 4661 { 4662 int variableIndex; 4663 4664 /* "source" 4665 variableIndex <- self extractBits: 13 to: 15 4666 of: currentBytecode. 4667 memory storePointer: variableIndex 4668 ofObject: receiver 4669 withValue: self popStack 4670 */ 4671 4672 variableIndex = extractBits_to_of(13, 15, currentBytecode); 4673 memory.storePointer_ofObject_withValue(variableIndex, receiver, popStack()); 4674 } 4675 4676 4677 // extendedStoreBytecode 4678 void Interpreter::extendedStoreBytecode() 4679 { 4680 int descriptor; 4681 int variableType; 4682 int variableIndex; 4683 int association; 4684 4685 /* "source" 4686 descriptor <- self fetchByte. 4687 variableType <- self extractBits: 8 to: 9 4688 of: descriptor. 4689 variableIndex <- self extractBits: 10 to: 15 4690 of: descriptor. 4691 variableType = 0 ifTrue: 4692 [^memory storePointer: variableIndex 4693 ofObject: receiver 4694 withValue: self stackTop]. 4695 variableType = 1 ifTrue: 4696 [^memory storePointer: variableIndex + TempFrameStart 4697 ofObject: homeContext 4698 withValue: self stackTop]. 4699 variableType = 2 ifTrue: 4700 [^self error: 'illegal store']. 4701 variableType = 3 ifTrue: 4702 [association <- self literal: variableIndex. 4703 ^memory storePointer: ValueIndex 4704 ofObject: association 4705 withValue: self stackTop] 4706 */ 4707 4708 descriptor = fetchByte(); 4709 variableType = extractBits_to_of(8, 9, descriptor); 4710 variableIndex = extractBits_to_of(10, 15, descriptor); 4711 switch (variableType) 4712 { 4713 case 0: memory.storePointer_ofObject_withValue(variableIndex, receiver, stackTop()); break; 4714 case 1: memory.storePointer_ofObject_withValue(variableIndex + TempFrameStart, homeContext, stackTop()); break; 4715 case 2: error("illegal store"); break; 4716 case 3: 4717 association = literal(variableIndex); 4718 memory.storePointer_ofObject_withValue(ValueIndex, association, stackTop()); 4719 break; 4720 } 4721 } 4722 4723 4724 // pushLiteralConstantBytecode 4725 void Interpreter::pushLiteralConstantBytecode() 4726 { 4727 int fieldIndex; 4728 4729 /* "source" 4730 fieldIndex <- self extractBits: 11 to: 15 4731 of: currentBytecode. 4732 self pushLiteralConstant: fieldIndex 4733 */ 4734 4735 fieldIndex = extractBits_to_of(11, 15, currentBytecode); 4736 pushLiteralConstant(fieldIndex); 4737 } 4738 4739 4740 // storeAndPopTemporaryVariableBytecode 4741 void Interpreter::storeAndPopTemporaryVariableBytecode() 4742 { 4743 int variableIndex; 4744 4745 /* "source" 4746 variableIndex <- self extractBits: 13 to: 15 4747 of: currentBytecode. 4748 memory storePointer: variableIndex + TempFrameStart 4749 ofObject: homeContext 4750 withValue: self popStack 4751 */ 4752 variableIndex = extractBits_to_of(13, 15, currentBytecode); 4753 memory.storePointer_ofObject_withValue(variableIndex + TempFrameStart, 4754 homeContext, 4755 popStack()); 4756 } 4757 4758 4759 // extendedStoreAndPopBytecode 4760 void Interpreter::extendedStoreAndPopBytecode() 4761 { 4762 /* "source" 4763 self extendedStoreBytecode. 4764 self popStackBytecode 4765 */ 4766 4767 extendedStoreBytecode(); 4768 popStackBytecode(); 4769 } 4770 4771 4772 // stackBytecode 4773 void Interpreter::stackBytecode() 4774 { 4775 /* "source" 4776 (currentBytecode between: 0 and: 15) 4777 ifTrue: [^self pushReceiverVariableBytecode]. 4778 (currentBytecode between: 16 and: 31) 4779 ifTrue: [^self pushTemporaryVariableBytecode]. 4780 (currentBytecode between: 32 and: 63) 4781 ifTrue: [^self pushLiteralConstantBytecode]. 4782 (currentBytecode between: 64 and: 95) 4783 ifTrue: [^self pushLiteralVariableBytecode]. 4784 (currentBytecode between: 96 and: 103) 4785 ifTrue: [^self storeAndPopReceiverVariableBytecode]. 4786 (currentBytecode between: 104 and: 111) 4787 ifTrue: [^self storeAndPopTemporaryVariableBytecode]. 4788 currentBytecode = 112 4789 ifTrue: [^self pushReceiverBytecode]. 4790 currentBytecode between: 113 and: 119) 4791 ifTrue: [^self pushConstantBytecode]. 4792 currentBytecode = 128 4793 ifTrue: [^self extendedPushBytecode]. 4794 currentBytecode = 129 4795 ifTrue: [^self extendedStoreBytecode]. 4796 currentBytecode = 130 4797 ifTrue: [^self extendedStoreAndPopBytecode]. 4798 currentBytecode = 135 4799 ifTrue: [^self popStackBytecode]. 4800 currentBytecode = 136 4801 ifTrue: [^self duplicateTopBytecode]. 4802 currentBytecode = 137 4803 ifTrue: [^self pushActiveContextBytecode] 4804 */ 4805 4806 if (between_and(currentBytecode, 0, 15)) pushReceiverVariableBytecode(); 4807 else if (between_and(currentBytecode, 16, 31)) pushTemporaryVariableBytecode(); 4808 else if (between_and(currentBytecode, 32, 63)) pushLiteralConstantBytecode(); 4809 else if (between_and(currentBytecode, 64, 95)) pushLiteralVariableBytecode(); 4810 else if (between_and(currentBytecode, 96, 103)) storeAndPopReceiverVariableBytecode(); 4811 else if (between_and(currentBytecode, 104 ,111)) storeAndPopTemporaryVariableBytecode(); 4812 else if (currentBytecode == 112) pushReceiverBytecode(); 4813 else if (between_and(currentBytecode, 113, 119)) pushConstantBytecode(); 4814 else if (currentBytecode == 128) extendedPushBytecode(); 4815 else if (currentBytecode == 129) extendedStoreBytecode(); 4816 else if (currentBytecode == 130) extendedStoreAndPopBytecode(); 4817 else if (currentBytecode == 135) popStackBytecode(); 4818 else if (currentBytecode == 136) duplicateTopBytecode(); 4819 else if (currentBytecode == 137) pushActiveContextBytecode(); 4820 } 4821 4822 4823 // extendedPushBytecode 4824 void Interpreter::extendedPushBytecode() 4825 { 4826 int descriptor; 4827 int variableType; 4828 int variableIndex; 4829 4830 /* "source" 4831 descriptor <- self fetchByte. 4832 variableType <- self extractBits: 8 to: 9 4833 of: descriptor. 4834 variableIndex <- self extractBits: 10 to: 15 4835 of: descriptor. 4836 variableType=0 ifTrue: [^self pushReceiverVariable: variableIndex]. 4837 variableType=1 ifTrue: [^self pushTemporaryVariable: variableIndex]. 4838 variableType=2 ifTrue: [^self pushLiteralConstant: variableIndex]. 4839 variableType=3 ifTrue: [^self pushLiteralVariable: variableIndex] 4840 */ 4841 descriptor = fetchByte(); 4842 variableType = extractBits_to_of(8, 9, descriptor); 4843 variableIndex = extractBits_to_of(10, 15, descriptor); 4844 switch (variableType) 4845 { 4846 case 0: pushReceiverVariable(variableIndex); break; 4847 case 1: pushTemporaryVariable(variableIndex); break; 4848 case 2: pushLiteralConstant(variableIndex); break; 4849 case 3: pushLiteralVariable(variableIndex); break; 4850 } 4851 } 4852 4853 // pushConstantBytecode 4854 void Interpreter::pushConstantBytecode() 4855 { 4856 /* "source" 4857 currentBytecode = 113 ifTrue: [^self push: TruePointer]. 4858 currentBytecode = 114 ifTrue: [^self push: FalsePointer]. 4859 currentBytecode = 115 ifTrue: [^self push: NilPointer]. 4860 currentBytecode = 116 ifTrue: [^self push: MinusOnePointer]. 4861 currentBytecode = 117 ifTrue: [^self push: ZeroPointer]. 4862 currentBytecode = 118 ifTrue: [^self push: OnePointer]. 4863 currentBytecode = 119 ifTrue: [^self push: TwoPointer] 4864 */ 4865 switch (currentBytecode) 4866 { 4867 case 113: push(TruePointer); break; 4868 case 114: push(FalsePointer); break; 4869 case 115: push(NilPointer); break; 4870 case 116: push(MinusOnePointer); break; 4871 case 117: push(ZeroPointer); break; 4872 case 118: push(OnePointer); break; 4873 case 119: push(TwoPointer); break; 4874 } 4875 } 4876 4877 4878 // jumpIf:by: 4879 void Interpreter::jumpIf_by(int condition, int offset) 4880 { 4881 int boolean; 4882 4883 /* "source" 4884 boolean <- self popStack. 4885 boolean = condition 4886 ifTrue: [self jump: offset] 4887 ifFalse: [(boolean=TruePointer) | (boolean=FalsePointer) 4888 ifFalse: [self unPop: 1. 4889 self sendMustBeBoolean]] 4890 */ 4891 4892 boolean = popStack(); 4893 if (boolean == condition) 4894 { 4895 jump(offset); 4896 } 4897 else 4898 { 4899 if (!(boolean == TruePointer || boolean == FalsePointer)) 4900 { 4901 unPop(1); 4902 sendMustBeBoolean(); 4903 } 4904 } 4905 } 4906 4907 4908 // longConditionalJump 4909 void Interpreter::longConditionalJump() 4910 { 4911 int offset; 4912 4913 /* "source" 4914 offset <- self extractBits: 14 to: 15 4915 of: currentBytecode. 4916 offset <- offset * 256 + self fetchByte. 4917 (currentBytecode between: 168 and: 171) 4918 ifTrue: [^self jumpIf: TruePointer 4919 by: offset]. 4920 (currentBytecode between: 172 and: 175) 4921 ifTrue: (^self jumpIf: FalsePointer 4922 by: offset] 4923 */ 4924 offset = extractBits_to_of(14, 15, currentBytecode); 4925 offset = offset * 256 + fetchByte(); 4926 if (between_and(currentBytecode, 168, 171)) 4927 { 4928 jumpIf_by(TruePointer, offset); 4929 } 4930 else if (between_and(currentBytecode, 172, 175)) 4931 { 4932 jumpIf_by(FalsePointer, offset); 4933 } 4934 } 4935 4936 // jumpBytecode 4937 void Interpreter::jumpBytecode() 4938 { 4939 /* "source" 4940 (currentBytecode between: 144 and: 151) 4941 ifTrue: [^self shortUnconditionalJump]. 4942 (currentBytecode between: 152 and: 159) 4943 ifTrue: [^self shortConditionalJump]. 4944 (currentBytecode between: 160 and: 167) 4945 ifTrue: [^self longUnconditionalJump]. 4946 (currentBytecode between: 168 and: 175) 4947 ifTrue: [^self longConditionalJump] 4948 */ 4949 4950 if (between_and( currentBytecode, 144, 151)) shortUnconditionalJump(); 4951 else if (between_and( currentBytecode, 152, 159)) shortConditionalJump(); 4952 else if (between_and( currentBytecode, 160, 167)) longUnconditionalJump(); 4953 else if (between_and( currentBytecode, 168, 175)) longConditionalJump(); 4954 } 4955 4956 // storeInteger:ofObject:withValue: 4957 void Interpreter::storeInteger_ofObject_withValue(int fieldIndex, int objectPointer, int integerValue) 4958 { 4959 int integerPointer; 4960 4961 /* "source" 4962 (memory isIntegerValue: integerValue) 4963 ifTrue: [integerPointer <- memory integerObjectOf: integerValue. 4964 memory storePointer: fieldIndex 4965 ofObject: objectPointer 4966 withValue: integerPointer] 4967 ifFalse: [^self primitiveFail] 4968 */ 4969 if (memory.isIntegerValue(integerValue)) 4970 { 4971 integerPointer = memory.integerObjectOf(integerValue); 4972 memory.storePointer_ofObject_withValue(fieldIndex, objectPointer, integerPointer); 4973 } 4974 else 4975 primitiveFail(); 4976 } 4977 4978 4979 // transfer:fromIndex:ofObject:toIndex:ofObject: 4980 void Interpreter::transfer_fromIndex_ofObject_toIndex_ofObject( 4981 int count, 4982 int firstFrom, 4983 int fromOop, 4984 int firstTo, 4985 int toOop 4986 ) 4987 { 4988 int fromIndex; 4989 int toIndex; 4990 int lastFrom; 4991 int oop; 4992 4993 /* "source" 4994 fromIndex <- firstFrom. 4995 lastFrom <- firstFrom + count. 4996 toIndex <- firstTo. 4997 [fromIndex < lastFrom] whileTrue: 4998 [oop <- memory fetchPointer: fromIndex 4999 ofObject: fromOop. 5000 memory storePointer: toIndex 5001 ofObject: toOop 5002 withValue: oop. 5003 memory storePointer: fromIndex 5004 ofObject: fromOop 5005 withValue: NilPointer. 5006 fromIndex <- fromIndex + 1. 5007 toIndex <- toIndex + 1] 5008 */ 5009 5010 fromIndex = firstFrom; 5011 lastFrom = firstFrom + count; 5012 toIndex = firstTo; 5013 while (fromIndex < lastFrom) 5014 { 5015 oop = memory.fetchPointer_ofObject(fromIndex, fromOop); 5016 memory.storePointer_ofObject_withValue(toIndex, toOop, oop); 5017 memory.storePointer_ofObject_withValue(fromIndex, fromOop, NilPointer); 5018 fromIndex = fromIndex + 1; 5019 toIndex = toIndex + 1; 5020 } 5021 } 5022 5023 // fetchInteger:ofObject: 5024 int Interpreter::fetchInteger_ofObject(int fieldIndex, int objectPointer) 5025 { 5026 int integerPointer; 5027 5028 /* "source" 5029 integerPointer <- memory fetchPointer: fieldIndex 5030 ofObject: objectPointer. 5031 (memory isIntegerObject: integerPointer) 5032 ifTrue: [^memory integerValueOf: integerPointer] 5033 ifFalse: [^self primitiveFail] 5034 */ 5035 integerPointer = memory.fetchPointer_ofObject(fieldIndex, objectPointer); 5036 if (memory.isIntegerObject(integerPointer)) 5037 return memory.integerValueOf(integerPointer); 5038 else 5039 return primitiveFail(); 5040 } 5041 5042 // primitiveNewMethod 5043 void Interpreter::primitiveNewMethod() 5044 { 5045 int header; 5046 int bytecodeCount; 5047 int cls; 5048 int size; 5049 5050 /* "source" 5051 header <- self popStack. 5052 bytecodeCount <- self popInteger. 5053 class <- self popStack. 5054 size <- (self literalCountOfHeader: header) + 1 * 2 + bytecodeCount. 5055 "ERROR: dbanay - need tp initialize literal frame" 5056 self push: (memory instantiateClass: class 5057 withBytes: size) 5058 */ 5059 5060 header = popStack(); 5061 bytecodeCount = popInteger(); 5062 cls = popStack(); 5063 int literalCount = literalCountOfHeader(header); 5064 size = (literalCount + 1) * 2 + bytecodeCount; 5065 int newMethod = memory.instantiateClass_withBytes(cls, size); 5066 for(int i = 0; i < literalCount; i++) 5067 { 5068 memory.storeWord_ofObject_withValue(LiteralStart + i, newMethod, NilPointer); 5069 } 5070 // Note: using storeWord vs storePointer because it memory with 5071 // initialized with zeros and this break ref counting 5072 memory.storeWord_ofObject_withValue(HeaderIndex, newMethod, header); 5073 push(newMethod); 5074 } 5075 5076 5077 // primitiveAsOop 5078 void Interpreter::primitiveAsOop() 5079 { 5080 int thisReceiver; 5081 5082 /* "source" 5083 thisReceiver <- self popStack. 5084 self success: (memory isIntegerObject: thisReceiver) == false. 5085 self success 5086 ifTrue: [self push: (thisReceiver bitOr: 1)] 5087 ifFalse: [self unPop: 1] 5088 */ 5089 thisReceiver = popStack(); 5090 success(memory.isIntegerObject(thisReceiver) == false); 5091 if (success()) 5092 push(thisReceiver | 1); 5093 else 5094 unPop(1); 5095 } 5096 5097 5098 // primitiveSomeInstance 5099 void Interpreter::primitiveSomeInstance() 5100 { 5101 int cls; 5102 int instance; 5103 5104 5105 /* "source" 5106 class <- self popStack. 5107 "ERROR: instancesOf not defined in G&R, body of 5108 method has been changed completely from: 5109 (memory instancesOf: class) 5110 ifTrue: [self push: (memory initialInstanceOf: class)] 5111 ifFalse: [self primitiveFail] 5112 to:" 5113 5114 instance <- memory initialInstanceOf: class. 5115 instance = NilPointer 5116 ifTrue: [self primitiveFail] 5117 ifFalse: [self push: instance] 5118 */ 5119 5120 cls = popStack(); 5121 instance = memory.initialInstanceOf(cls); 5122 if (instance == NilPointer) 5123 primitiveFail(); 5124 else 5125 push(instance); 5126 } 5127 5128 5129 // primitiveObjectAt 5130 void Interpreter::primitiveObjectAt() 5131 { 5132 int thisReceiver; 5133 int index; 5134 5135 /* "source" 5136 index <- self popInteger. 5137 thisReceiver <- self popStack. 5138 self success: index > 0. 5139 self success: index <= (self objectPointerCountOf: thisReceiver). 5140 self success 5141 ifTrue: [self push: (memory fetchPointer: index - 1 5142 ofObject: thisReceiver)] 5143 ifFalse: [self unPop: 2] 5144 */ 5145 index = popInteger(); 5146 thisReceiver = popStack(); 5147 success(index > 0); 5148 success(index <= objectPointerCountOf(thisReceiver)); 5149 if (success()) 5150 { 5151 push(memory.fetchPointer_ofObject(index - 1, thisReceiver)); 5152 } 5153 else 5154 unPop(2); 5155 5156 } 5157 5158 5159 // primitiveNextInstance 5160 void Interpreter::primitiveNextInstance() 5161 { 5162 int object; 5163 int instance; 5164 5165 /* "source" 5166 object <- self popStack. 5167 "ERROR: isLastInstance: not defined in G&R, body of 5168 method has been changed completely from: 5169 (memory isLastInstance: object) 5170 ifTrue: [self primitiveFail] 5171 ifFalse: [self push: (memory instanceAfter: object)] 5172 to:" 5173 5174 instance <- memory instanceAfter: object. 5175 instance = NilPointer 5176 ifTrue: [self primitiveFail] 5177 ifFalse: [self push: instance] 5178 */ 5179 object = popStack(); 5180 instance = memory.instanceAfter(object); 5181 if (instance == NilPointer) 5182 primitiveFail(); 5183 else 5184 push(instance); 5185 } 5186 5187 5188 // primitiveNew 5189 void Interpreter::primitiveNew() 5190 { 5191 int cls; 5192 int size; 5193 5194 /* "source" 5195 class <- self popStack. 5196 size <- self fixedFieldsOf: class. 5197 self success: (self isIndexable: class) == false. 5198 self success 5199 ifTrue: [(self isPointers: class) 5200 ifTrue: [self push: (memory instantiateClass: class 5201 withPointers: size)] 5202 ifFalse: [self push: (memory instantiateClass: class 5203 withWords: size)]] 5204 ifFalse: [self unPop: 1] 5205 */ 5206 5207 cls = popStack(); 5208 size = fixedFieldsOf(cls); 5209 success(isIndexable(cls) == false); 5210 if (success()) 5211 { 5212 if (isPointers(cls)) 5213 { 5214 push(memory.instantiateClass_withPointers(cls, size)); 5215 } 5216 else 5217 { 5218 push(memory.instantiateClass_withWords(cls, size)); 5219 } 5220 } 5221 else 5222 unPop(1); 5223 } 5224 5225 5226 // primitiveAsObject 5227 void Interpreter::primitiveAsObject() 5228 { 5229 int thisReceiver; 5230 int newOop; 5231 5232 /* "source" 5233 thisReceiver <- self popStack. 5234 newOop <- thisReceiver bitAnd: 16rFFFE. 5235 "ERROR: hasObject not defined" 5236 self success: (memory hasObject: newOop). 5237 self success 5238 ifTrue: [self push: newOop] 5239 ifFalse: [self unPop: 1] 5240 */ 5241 5242 thisReceiver = popStack(); 5243 newOop = thisReceiver & 0xFFFE; 5244 success(memory.hasObject(newOop)); 5245 if (success()) 5246 push(newOop); 5247 else 5248 unPop(1); 5249 5250 } 5251 5252 5253 // primitiveNewWithArg 5254 void Interpreter::primitiveNewWithArg() 5255 { 5256 int size; 5257 int cls; 5258 5259 /* "source" 5260 size <- self positive16BitValueOf: self popStack. 5261 self success: size <= 65533. "dbanay: ERROR check max size" 5262 class <- self popStack. 5263 self success: (self isIndexable: class). 5264 self success 5265 ifTrue: [size <- size + (self fixedFieldsOf: class). 5266 (self isPointers: class) 5267 ifTrue: [self push: (memory instantiateClass: class 5268 withPointers: size)] 5269 ifFalse: [(self isWords: class) 5270 ifTrue: [self push: (memory 5271 instantiateClass: class 5272 withWords: size)] 5273 ifFalse: [self push: (memory 5274 instantiateClass: class 5275 withBytes: size)]]] 5276 ifFalse: [self unPop: 2] 5277 */ 5278 5279 size = positive16BitValueOf(popStack()); 5280 success(size <= 65533); 5281 5282 cls = popStack(); 5283 success(isIndexable(cls)); 5284 if (success()) 5285 { 5286 size = size + fixedFieldsOf(cls); 5287 if (isPointers(cls)) 5288 { 5289 push(memory.instantiateClass_withPointers(cls, size)); 5290 } 5291 else 5292 { 5293 if (isWords(cls)) 5294 { 5295 push(memory.instantiateClass_withWords(cls, size)); 5296 } 5297 else 5298 { 5299 push(memory.instantiateClass_withBytes(cls, size)); 5300 } 5301 } 5302 } 5303 else 5304 unPop(2); 5305 } 5306 5307 5308 // primitiveInstVarAtPut 5309 void Interpreter::primitiveInstVarAtPut() 5310 { 5311 int thisReceiver; 5312 int index; 5313 int newValue; 5314 5315 /* "source" 5316 "ERROR: realValue is not used in the method" 5317 newValue <- self popStack. 5318 index <- self popInteger. 5319 thisReceiver <- self popStack. 5320 self checkInstanceVariableBoundsOf: index 5321 in: thisReceiver. 5322 self success 5323 ifTrue: [self subscript: thisReceiver 5324 with: index 5325 storing: newValue]. 5326 self success 5327 ifTrue: [self push: newValue] 5328 ifFalse: [self unPop: 3] 5329 */ 5330 5331 newValue = popStack(); 5332 index = popInteger(); 5333 thisReceiver = popStack(); 5334 checkIndexableBoundsOf_in(index, thisReceiver); 5335 if (success()) 5336 { 5337 subscript_with_storing(thisReceiver, index, newValue); 5338 } 5339 if (success()) 5340 push(newValue); 5341 else 5342 unPop(3); 5343 } 5344 5345 5346 // primitiveObjectAtPut 5347 void Interpreter::primitiveObjectAtPut() 5348 { 5349 int thisReceiver; 5350 int index; 5351 int newValue; 5352 5353 /* "source" 5354 newValue <- self popStack. 5355 index <- self popInteger. 5356 thisReceiver <- self popStack. 5357 self success: index > 0. 5358 self success: index <= (self objectPointerCountOf: thisReceiver). 5359 self success 5360 ifTrue: [memory storePointer: index - 1 5361 ofObject: thisReceiver 5362 withValue: newValue. 5363 self push: newValue] 5364 ifFalse: [self unPop: 3] 5365 */ 5366 5367 newValue = popStack(); 5368 index = popInteger(); 5369 thisReceiver = popStack(); 5370 success(index > 0); 5371 success(index <= objectPointerCountOf(thisReceiver)); 5372 if (success()) 5373 { 5374 memory.storePointer_ofObject_withValue(index - 1, thisReceiver, newValue); 5375 push(newValue); 5376 } 5377 else 5378 unPop(3); 5379 5380 } 5381 5382 5383 // primitiveInstVarAt 5384 void Interpreter::primitiveInstVarAt() 5385 { 5386 int thisReceiver; 5387 int index; 5388 int value = 0; 5389 5390 /* "source" 5391 index <- self popInteger. 5392 thisReceiver <- self popStack. 5393 self checkInstanceVariableBoundsOf: index 5394 in: thisReceiver. 5395 self success 5396 ifTrue: [value <- self subscript: thisReceiver 5397 with: index]. 5398 self success 5399 ifTrue: [self push: value] 5400 ifFalse: [self unPop: 2] 5401 */ 5402 5403 index = popInteger(); 5404 thisReceiver = popStack(); 5405 checkInstanceVariableBoundsOf_in(index, thisReceiver); 5406 if (success()) 5407 value = subscript_with(thisReceiver, index); 5408 if (success()) 5409 push(value); 5410 else 5411 unPop(2); 5412 5413 } 5414 5415 5416 // primitiveBecome 5417 void Interpreter::primitiveBecome() 5418 { 5419 int thisReceiver; 5420 int otherPointer; 5421 5422 /* "source" 5423 otherPointer <- self popStack. 5424 thisReceiver <- self popStack. 5425 self success: (memory isIntegerObject: otherPointer) not. 5426 self success: (memory isIntegerObject: thisReceiver) not. 5427 self success 5428 ifTrue: [memory swapPointersOf: thisReceiver and: otherPointer. 5429 self push: thisReceiver] 5430 ifFalse: [self unPop: 2] 5431 */ 5432 otherPointer = popStack(); 5433 thisReceiver = popStack(); 5434 success(!memory.isIntegerObject(otherPointer)); 5435 success(!memory.isIntegerObject(thisReceiver)); 5436 if (success()) 5437 { 5438 memory.swapPointersOf_and(thisReceiver, otherPointer); 5439 push(thisReceiver); 5440 } 5441 else 5442 unPop(2); 5443 } 5444 5445 5446 // dispatchStorageManagementPrimitives 5447 void Interpreter::dispatchStorageManagementPrimitives() 5448 { 5449 /* "source" 5450 primitiveIndex = 68 ifTrue: [^self primitiveObjectAt]. 5451 primitiveIndex = 69 ifTrue: [^self primitiveObjectAtPut]. 5452 primitiveIndex = 70 ifTrue: [^self primitiveNew]. 5453 primitiveIndex = 71 ifTrue: [^self primitiveNewWithArg]. 5454 primitiveIndex = 72 ifTrue: [^self primitiveBecome]. 5455 primitiveIndex = 73 ifTrue: [^self primitiveInstVarAt]. 5456 primitiveIndex = 74 ifTrue: [^self primitiveInstVarAtPut]. 5457 primitiveIndex = 75 ifTrue: [^self primitiveAsOop]. 5458 primitiveIndex = 76 ifTrue: [^self primitiveAsObject]. 5459 primitiveIndex = 77 ifTrue: [^self primitiveSomeInstance]. 5460 primitiveIndex = 78 ifTrue: [^self primitiveNextInstance]. 5461 primitiveIndex = 79 ifTrue: [^self primitiveNewMethod] 5462 */ 5463 switch (primitiveIndex) 5464 { 5465 case 68: primitiveObjectAt(); break; 5466 case 69: primitiveObjectAtPut(); break; 5467 case 70: primitiveNew(); break; 5468 case 71: primitiveNewWithArg(); break; 5469 case 72: primitiveBecome(); break; 5470 case 73: primitiveInstVarAt(); break; 5471 case 74: primitiveInstVarAtPut(); break; 5472 case 75: primitiveAsOop(); break; 5473 case 76: primitiveAsObject(); break; 5474 case 77: primitiveSomeInstance(); break; 5475 case 78: primitiveNextInstance(); break; 5476 case 79: primitiveNewMethod(); break; 5477 } 5478 } 5479 5480 void Interpreter::pushFloat(float f) 5481 { 5482 std::uint32_t uint32 = *(std::uint32_t *) &f; 5483 int objectPointer = memory.instantiateClass_withWords(ClassFloatPointer, 2); 5484 memory.storeWord_ofObject_withValue(0, objectPointer, uint32 & 0xffff); 5485 memory.storeWord_ofObject_withValue(1, objectPointer, uint32 >> 16); 5486 5487 push(objectPointer); 5488 } 5489 5490 float Interpreter::popFloat() 5491 { 5492 int objectPointer = popStack(); 5493 success(memory.fetchClassOf(objectPointer) == ClassFloatPointer); 5494 if (success()) 5495 { 5496 return extractFloat(objectPointer); 5497 } 5498 5499 return std::nanf(""); 5500 } 5501 5502