-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStInterpreter.cpp
More file actions
3272 lines (2991 loc) · 96.5 KB
/
StInterpreter.cpp
File metadata and controls
3272 lines (2991 loc) · 96.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the Smalltalk parser/compiler library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "StDisplay.h"
#include "StInterpreter.h"
#include "StImageViewer.h"
#include <QtDebug>
#include <math.h>
#include <QDateTime>
#include <QPainter>
#include <QEventLoop>
#include <QVBoxLayout>
using namespace St;
//#define ST_DO_TRACING
#define ST_DO_TRACE2
#define ST_TRACE3_PRIMITIVES
#define ST_TRACE_SYSTEM_ERRORS
//#define ST_DO_SCREEN_RECORDING
#ifdef ST_DO_TRACING
#ifdef ST_DO_TRACE2
#define ST_TRACE_BYTECODE(msg) qDebug() << "Bytecode" << ( "<" + QByteArray::number(currentBytecode) + ">" ).constData() \
<< "\t" << __FUNCTION__ << "\t" << msg;
#define ST_TRACE_PRIMITIVE(msg) qDebug() << "Primitive" << ( "#" + QByteArray::number(primitiveIndex) ).constData() \
<< __FUNCTION__ << msg;
#define ST_TRACE_METHOD_CALL qDebug() << level << ( "[cycle=" + QByteArray::number(cycleNr) + "]" ).constData() << \
memory->prettyValue(stackValue(argumentCount) ).constData() << \
QByteArray::number(memory->getRegister(NewMethod),16).constData() << \
memory->fetchByteArray(memory->getRegister(MessageSelector)).constData() << prettyArgs_().constData();
#define ST_RETURN_BYTECODE(msg)
#else
#define ST_TRACE_METHOD_CALL qDebug() << level /*<< QByteArray(level,'\t').constData() */ << \
( "[cycle=" + QByteArray::number(cycleNr) + "]" ).constData() << \
memory->prettyValue(stackValue(argumentCount) ).constData() << \
QByteArray::number(memory->getRegister(NewMethod),16).constData() << \
memory->fetchByteArray(memory->getRegister(MessageSelector)).constData() << prettyArgs_().constData();
#define ST_RETURN_BYTECODE(msg) qDebug() << level /*<< QByteArray(level,'\t').constData() */ << \
"^" << ( "<" + QByteArray::number(currentBytecode) + ">" ).constData() << msg;
#define ST_TRACE_BYTECODE(msg)
#ifdef ST_TRACE3_PRIMITIVES
#define ST_TRACE_PRIMITIVE(msg) qDebug() << level << "Primitive" << ( "#" + QByteArray::number(primitiveIndex) ).constData() \
<< __FUNCTION__ << msg;
#else
#define ST_TRACE_PRIMITIVE(msg)
#endif
#endif
#else
#define ST_TRACE_BYTECODE(msg)
#define ST_TRACE_PRIMITIVE(msg)
#define ST_TRACE_METHOD_CALL
#define ST_RETURN_BYTECODE(msg)
#endif
Interpreter::Interpreter(QObject* p):QObject(p),memory(0),stackPointer(0),instructionPointer(0),
newProcessWaiting(false),cycleNr(0), level(0), toSignal(0)
{
d_timer.setSingleShot(true);
connect( &d_timer, SIGNAL(timeout()), this, SLOT(onTimeout()) );
}
static ObjectMemory2::OOP findDisplay(ObjectMemory2* memory)
{
ObjectMemory2::OOP sysdict = memory->fetchPointerOfObject(1, ObjectMemory2::smalltalk );
for( int i = 1; i < memory->fetchWordLenghtOf( sysdict ); i++ )
{
ObjectMemory2::OOP assoc = memory->fetchPointerOfObject(i,sysdict);
if( assoc != ObjectMemory2::objectNil )
{
ObjectMemory2::OOP sym = memory->fetchPointerOfObject(0,assoc);
QByteArray name = memory->fetchByteArray(sym);
if( name == "Display" )
return memory->fetchPointerOfObject(1,assoc);
}
}
return 0;
}
static Bitmap fetchBitmap( ObjectMemory2* memory, Interpreter::OOP form )
{
if( form == ObjectMemory2::objectNil )
return Bitmap();
Interpreter::OOP bitmap = memory->fetchPointerOfObject(0,form);
qint16 width = memory->integerValueOf(memory->fetchPointerOfObject(1,form));
qint16 height = memory->integerValueOf(memory->fetchPointerOfObject(2,form));
// Q_ASSERT( memory->fetchByteLenghtOf(bitmap) == width * height / 8 );
ObjectMemory2::ByteString bs = memory->fetchByteString(bitmap);
Q_ASSERT( bs.d_bytes != 0 );
return Bitmap( const_cast<quint8*>(bs.d_bytes), bs.getWordLen(), width, height );
}
void Interpreter::setOm(ObjectMemory2* om)
{
memory = om;
if( om )
{
const OOP display = findDisplay(memory); // 0x340;
Display::inst()->setBitmap( fetchBitmap(memory, display) );
}else
Display::inst()->setBitmap(Bitmap());
disconnect( Display::inst(), SIGNAL(sigEventQueue()), this, SLOT(onEvent()) );
connect( Display::inst(), SIGNAL(sigEventQueue()), this, SLOT(onEvent()) );
}
void Interpreter::interpret()
{
cycleNr = 0;
level = 0;
const quint32 startTime = Display::inst()->getTicks();
//Display::inst()->setLog(true); // TEST
newActiveContext( firstContext() ); // BB: When Smalltalk is started up, ...
// apparently the system is in ScreenController ->startUp->controlLoop->controlActivity->yellowButtonActivity
// SystenDictionary ->quit->saveAs:thenQuit:->snapshotAs:thenQuit:
/* justSnapped & quitIfTrue
ifTrue:
[self quitPrimitive] "last action in 1983"
ifFalse:
[Delay postSnapshot. "first action in 2020"
DisplayScreen displayHeight: height.
self install].*/
// top: BlockContext->newProcess, ControllManager->activeController: SystemDictionary->install
while( Display::s_run ) // && cycleNr < 121000 ) // trace2 < 500 trace3 < 2000
{
cycle();
Display::processEvents();
if( Display::s_break )
onBreak();
}
const quint32 endTime = Display::inst()->getTicks();
qWarning() << "runtime [ms]:" << ( endTime - startTime );
}
qint16 Interpreter::instructionPointerOfContext(Interpreter::OOP contextPointer)
{
return fetchIntegerOfObject(InstructionPointerIndex, contextPointer );
}
void Interpreter::storeInstructionPointerValueInContext(qint16 value, Interpreter::OOP contextPointer)
{
storeIntegerOfObjectWithValue( InstructionPointerIndex, contextPointer, value );
}
qint16 Interpreter::stackPointerOfContext(Interpreter::OOP contextPointer)
{
return fetchIntegerOfObject( StackPointerIndex, contextPointer );
}
void Interpreter::storeStackPointerValueInContext(qint16 value, Interpreter::OOP contextPointer)
{
storeIntegerOfObjectWithValue( StackPointerIndex, contextPointer, value );
}
qint16 Interpreter::argumentCountOfBlock(Interpreter::OOP blockPointer)
{
return fetchIntegerOfObject( BlockArgumentCountIndex, blockPointer );
}
bool Interpreter::isBlockContext(Interpreter::OOP contextPointer)
{
const OOP methodOrArguments = memory->fetchPointerOfObject(MethodIndex, contextPointer);
return memory->isIntegerObject(methodOrArguments);
}
void Interpreter::fetchContextRegisters()
{
OOP activeContext = memory->getRegister(ActiveContext);
OOP homeContext = 0;
if( isBlockContext(activeContext) )
homeContext = memory->fetchPointerOfObject(HomeIndex,activeContext);
else
homeContext = activeContext;
memory->setRegister(HomeContext,homeContext);
memory->setRegister(Receiver, memory->fetchPointerOfObject(ReceiverIndex,homeContext) );
memory->setRegister(Method, memory->fetchPointerOfObject(MethodIndex,homeContext) );
instructionPointer = instructionPointerOfContext(activeContext) - 1;
stackPointer = stackPointerOfContext(activeContext) + TempFrameStart - 1;
}
void Interpreter::storeContextRegisters()
{
OOP activeContext = memory->getRegister(ActiveContext);
if( activeContext ) // deviation from BB since activeContext is null on first call
{
storeInstructionPointerValueInContext( instructionPointer + 1, activeContext );
storeStackPointerValueInContext( stackPointer - TempFrameStart + 1, activeContext );
}
}
void Interpreter::push(Interpreter::OOP object)
{
if( object == 0 )
{
qWarning() << "WARNING: pushing zero oop to stack, replaced by nil";
object = ObjectMemory2::objectNil;
}
stackPointer++;
memory->storePointerOfObject( stackPointer, memory->getRegister(ActiveContext), object);
}
Interpreter::OOP Interpreter::popStack()
{
OOP stackTop = memory->fetchPointerOfObject( stackPointer, memory->getRegister(ActiveContext) );
stackPointer--;
return stackTop;
}
Interpreter::OOP Interpreter::stackTop()
{
return memory->fetchPointerOfObject( stackPointer, memory->getRegister(ActiveContext) );
}
Interpreter::OOP Interpreter::stackValue(qint16 offset)
{
return memory->fetchPointerOfObject( stackPointer - offset, memory->getRegister(ActiveContext) );
}
void Interpreter::pop(quint16 number)
{
stackPointer -= number;
}
void Interpreter::unPop(quint16 number)
{
stackPointer += number;
}
void Interpreter::newActiveContext(Interpreter::OOP aContext)
{
Q_ASSERT( aContext );
storeContextRegisters();
// decreaseReferencesTo: activeContext
memory->setRegister(ActiveContext, aContext);
// increaseReferencesTo: activeContext
fetchContextRegisters();
}
Interpreter::OOP Interpreter::sender()
{
return memory->fetchPointerOfObject(SenderIndex, memory->getRegister(HomeContext) );
}
Interpreter::OOP Interpreter::caller()
{
return memory->fetchPointerOfObject(CallerIndex, memory->getRegister(ActiveContext) ); // BB states SenderIndex instead
}
Interpreter::OOP Interpreter::temporary(qint16 offset)
{
return memory->fetchPointerOfObject( offset + TempFrameStart, memory->getRegister(HomeContext) );
}
Interpreter::OOP Interpreter::literal(qint16 offset)
{
return memory->literalOfMethod( offset, memory->getRegister(Method) );
}
static inline quint16 _hash(Interpreter::OOP objectPointer)
{
return objectPointer >> 1;
}
bool Interpreter::lookupMethodInDictionary(Interpreter::OOP dictionary)
{
const int SelectorStart = 2;
const int MethodArrayIndex = 1;
OOP messageSelector = memory->getRegister(MessageSelector);
#if 0
// Just a trivial linear scan; not the more fancy hash lookup described in the Blue Book
int length = memory->fetchWordLenghtOf(dictionary);
for( int index = SelectorStart; index < length; index++ )
{
OOP selector = memory->fetchPointerOfObject(index,dictionary);
if( selector == messageSelector )
{
OOP methodArray = memory->fetchPointerOfObject(MethodArrayIndex,dictionary);
OOP newMethod = memory->fetchPointerOfObject(index-SelectorStart,methodArray);
memory->setRegister(NewMethod,newMethod);
primitiveIndex = memory->primitiveIndexOf(newMethod);
return true;
}
}
return false;
#else
// this version is about nine times faster than the linear version
const quint16 length = memory->fetchWordLenghtOf(dictionary);
const quint16 mask = length - SelectorStart - 1;
quint16 index = ( mask & _hash(messageSelector) ) + SelectorStart;
bool wrapAround = false;
while( true )
{
OOP nextSelector = memory->fetchPointerOfObject(index, dictionary);
if( nextSelector == ObjectMemory2::objectNil )
return false;
if( nextSelector == messageSelector )
{
const OOP methodArray = memory->fetchPointerOfObject(MethodArrayIndex, dictionary);
OOP newMethod = memory->fetchPointerOfObject(index - SelectorStart, methodArray);
memory->setRegister(NewMethod,newMethod);
primitiveIndex = memory->primitiveIndexOf(newMethod);
return true;
}
index = index + 1;
if( index == length )
{
if( wrapAround )
return false;
wrapAround = true;
index = SelectorStart;
}
}
#endif
}
bool Interpreter::lookupMethodInClass(Interpreter::OOP cls)
{
OOP currentClass = cls;
while( currentClass != ObjectMemory2::objectNil )
{
OOP dictionary = memory->fetchPointerOfObject(MessageDictionaryIndex, currentClass);
if( lookupMethodInDictionary( dictionary ) )
{
//qDebug() << "found method" << memory->fetchByteArray(memory->getRegister(MessageSelector))
// << "in" << memory->fetchClassName(currentClass);
return true;
}
currentClass = superclassOf(currentClass);
}
if( memory->getRegister(MessageSelector) == ObjectMemory2::symbolDoesNotUnderstand )
{
qCritical() << "ERROR: Recursive not understood error encountered";
// BB self error:
return false;
}
createActualMessage();
OOP selector = memory->getRegister(MessageSelector);
memory->setRegister(MessageSelector, ObjectMemory2::symbolDoesNotUnderstand );
qCritical() << "ERROR: class" << memory->prettyValue(cls) << "doesNotUnderstand"
<< memory->prettyValue( selector );
// exit(-1);
return lookupMethodInClass(cls);
}
Interpreter::OOP Interpreter::superclassOf(Interpreter::OOP cls)
{
if( cls == ObjectMemory2::objectNil )
{
qWarning() << "WARNING: asking for superclass of nil";
return cls;
}
return memory->fetchPointerOfObject(SuperClassIndex,cls);
}
Interpreter::OOP Interpreter::instanceSpecificationOf(Interpreter::OOP classPointer)
{
return memory->fetchPointerOfObject(InstanceSpecIndex,classPointer);
}
bool Interpreter::isPointers(Interpreter::OOP classPointer)
{
return instanceSpecificationOf(classPointer) & 0x8000;
}
bool Interpreter::isWords(Interpreter::OOP classPointer)
{
return instanceSpecificationOf(classPointer) & 0x4000;
}
bool Interpreter::isIndexable(Interpreter::OOP classPointer)
{
return instanceSpecificationOf(classPointer) & 0x2000;
}
qint16 Interpreter::fixedFieldsOf(Interpreter::OOP classPointer)
{
return ( ( instanceSpecificationOf(classPointer) >> 1 ) & 0x7ff );
}
quint8 Interpreter::fetchByte()
{
Q_ASSERT( instructionPointer >= 0 );
return memory->fetchByteOfObject(instructionPointer++, memory->getRegister(Method) );
}
void Interpreter::cycle()
{
// not in BB:
if( Display::s_copy )
{
Display::s_copy = false;
OOP text = memory->fetchPointerOfObject(1, ObjectMemory2::currentSelection );
if( text != ObjectMemory2::objectNil )
{
OOP string = memory->fetchPointerOfObject(0, text );
if( string != ObjectMemory2::objectNil )
Display::copyToClipboard( memory->fetchByteArray(string) );
}
}
// end not in BB
checkProcessSwitch();
currentBytecode = fetchByte();
cycleNr++;
dispatchOnThisBytecode();
}
void Interpreter::BREAK(bool immediate)
{
if( immediate )
onBreak();
else
Display::s_break = true;
}
void Interpreter::onEvent()
{
OOP sema = memory->getRegister(InputSemaphore);
if( sema )
{
asynchronousSignal(sema);
}else
Display::inst()->nextEvent();
}
void Interpreter::onTimeout()
{
// qWarning() << "onTimeout";
if( toSignal )
asynchronousSignal(toSignal);
}
void Interpreter::onBreak()
{
memory->collectGarbage();
QEventLoop loop;
ImageViewer v;
connect( &v, SIGNAL(sigClosing()), &loop, SLOT(quit()) );
ImageViewer::Registers r;
r["activeContext"] = memory->getRegister(ActiveContext);
r["homeContext"] = memory->getRegister(HomeContext);
r["method"] = memory->getRegister(Method);
r["receiver"] = memory->getRegister(Receiver);
r["messageSelector"] = memory->getRegister(MessageSelector);
r["newMethod"] = memory->getRegister(NewMethod);
r["newProcess"] = memory->getRegister(NewProcess);
r["inputSemaphore"] = memory->getRegister(InputSemaphore);
r["stackPointer"] = memory->integerObjectOf(stackPointer);
r["instructionPointer"] = memory->integerObjectOf(instructionPointer);
r["argumentCount"] = memory->integerObjectOf(argumentCount);
r["primitiveIndex"] = memory->integerObjectOf(primitiveIndex);
// r["cycleNumber"] = cycleNr;
r["currentBytecode"] = memory->integerObjectOf(currentBytecode);
r["success"] = success ? ObjectMemory2::objectTrue : ObjectMemory2::objectFalse;
r["newProcessWaiting"] = newProcessWaiting ? ObjectMemory2::objectTrue : ObjectMemory2::objectFalse;
v.show(memory, r);
loop.exec();
if( v.isNextStep() )
qWarning() << "next step";
else
{
Display::s_break = false;
qWarning() << "break finished";
}
}
void Interpreter::checkProcessSwitch()
{
while( !semaphoreList.isEmpty() )
{
synchronousSignal( semaphoreList.back() );
semaphoreList.pop_back();
}
if( newProcessWaiting )
{
newProcessWaiting = false;
OOP activeProcess_ = activeProcess();
if( activeProcess_ )
memory->storePointerOfObject(SuspendedContextIndex, activeProcess_, memory->getRegister(ActiveContext));
OOP scheduler = schedulerPointer();
OOP newProcess = memory->getRegister(NewProcess);
memory->storePointerOfObject(ActiveProcessIndex, scheduler, newProcess );
newActiveContext( memory->fetchPointerOfObject( SuspendedContextIndex, newProcess ));
memory->setRegister(NewProcess, 0);
}
}
void Interpreter::dispatchOnThisBytecode()
{
const quint8 b = currentBytecode;
if( ( b >= 0 && b <= 119 ) || ( b >= 128 && b <= 130 ) || ( b >= 135 && b <= 137 ) )
stackBytecode();
else if( b >= 120 && b <= 127 )
returnBytecode();
else if( ( b >= 131 && b <= 134 ) || ( b >= 176 && b <= 255 ) )
sendBytecode();
else if( b >= 144 && b <= 175 )
jumpBytecode();
else if( b >= 138 && b <= 143 )
qWarning() << "WARNING: running unused bytecode" << b;
}
bool Interpreter::stackBytecode()
{
const quint8 b = currentBytecode;
if( b >= 0 && b <= 15 )
return pushReceiverVariableBytecode();
if( b >= 16 && b <= 31 )
return pushTemporaryVariableBytecode();
if( b >= 32 && b <= 63 )
return pushLiteralConstantBytecode();
if( b >= 64 && b <= 95 )
return pushLiteralVariableBytecode();
if( b >= 96 && b <= 103 )
return storeAndPopReceiverVariableBytecode();
if( b >= 104 && b <= 111 )
return storeAndPopTemporaryVariableBytecode();
if( b == 112 )
return pushReceiverBytecode();
if( b >= 113 && b <= 119 )
return pushConstantBytecode();
if( b == 128 )
return extendedPushBytecode();
if( b == 129 )
return extendedStoreBytecode(false);
if( b == 130 )
return extendedStoreAndPopBytecode();
if( b == 135 )
return popStackBytecode(false);
if( b == 136 )
return duplicateTopBytecode();
if( b == 137 )
return pushActiveContextBytecode();
return false;
}
bool Interpreter::returnBytecode()
{
switch( currentBytecode )
{
// "Return (receiver, true, false, nil) [%1] From Message").arg( b & 0x3 ), 1 );
case 120:
returnValue( memory->getRegister(Receiver), sender() );
break;
case 121:
returnValue( ObjectMemory2::objectTrue, sender() );
break;
case 122:
returnValue( ObjectMemory2::objectFalse, sender() );
break;
case 123:
returnValue( ObjectMemory2::objectNil, sender() );
break;
// "Return Stack Top From (Message, Block) [%1]").arg( b & 0x1 ), 1 );
case 124:
returnValue( popStack(), sender() );
break;
case 125:
returnValue( popStack(), caller() );
break;
// unused
default:
qWarning() << "WARNING: executing unused bytecode" << currentBytecode;
return false;
}
return true;
}
bool Interpreter::sendBytecode()
{
const quint8 b = currentBytecode;
if( b >= 131 && b <= 134 )
return extendedSendBytecode();
if( b >= 176 && b <= 207 )
return sendSpecialSelectorBytecode();
if( b >= 208 && b <= 255 )
return sendLiteralSelectorBytecode();
return false;
}
bool Interpreter::jumpBytecode()
{
const quint8 b = currentBytecode;
if( b >= 144 && b <= 151 )
return shortUnconditionalJump();
if( b >= 152 && b <= 159 )
return shortContidionalJump();
if( b >= 160 && b <= 167 )
return longUnconditionalJump();
if( b >= 168 && b <= 175 )
return longConditionalJump();
return false;
}
bool Interpreter::pushReceiverVariableBytecode()
{
OOP receiver = memory->getRegister(Receiver);
ST_TRACE_BYTECODE("receiver:" << memory->prettyValue(receiver).constData());
push( memory->fetchPointerOfObject( extractBits( 12, 15, currentBytecode ), receiver ) );
// "Push Receiver Variable #%1").arg( b & 0xf ), 1 );
return true;
}
bool Interpreter::pushTemporaryVariableBytecode()
{
const quint16 var = extractBits( 12, 15, currentBytecode );
const OOP val = temporary( var );
ST_TRACE_BYTECODE( "variable:" << var << "value:" << memory->prettyValue(val).constData() );
// "Push Temporary Location #%1").arg( b & 0xf ), 1 );
push( val );
return true;
}
bool Interpreter::pushLiteralConstantBytecode()
{
const quint16 fieldIndex = extractBits( 11, 15, currentBytecode );
const OOP literalConstant = literal( fieldIndex );
ST_TRACE_BYTECODE( "literal:" << fieldIndex << "value:" << memory->prettyValue(literalConstant).constData() <<
"of method:" << QByteArray::number( memory->getRegister(Method), 16 ).constData() );
// "Push Literal Constant #%1").arg( b & 0x1f ), 1 );
push( literalConstant );
return true;
}
static const quint16 ValueIndex = 1;
bool Interpreter::pushLiteralVariableBytecode()
{
// "Push Literal Variable #%1").arg( b & 0x1f ), 1 );
const quint16 fieldIndex = extractBits( 11, 15, currentBytecode );
const OOP association = literal( fieldIndex );
const OOP value = memory->fetchPointerOfObject( ValueIndex, association );
ST_TRACE_BYTECODE("literal:" << fieldIndex << "value:" << memory->prettyValue(value).constData() <<
"of method:" << QByteArray::number( memory->getRegister(Method), 16 ).constData());
push( value );
return true;
}
bool Interpreter::storeAndPopReceiverVariableBytecode()
{
// "Pop and Store Receiver Variable #%1").arg( b & 0x7 ), 1 );
const quint16 variableIndex = extractBits( 13, 15, currentBytecode );
OOP val = popStack();
ST_TRACE_BYTECODE("var:" << variableIndex << "val:" << memory->prettyValue(val).constData() );
memory->storePointerOfObject( variableIndex, memory->getRegister(Receiver), val );
return true;
}
bool Interpreter::storeAndPopTemporaryVariableBytecode()
{
// "Pop and Store Temporary Location #%1").arg( b & 0x7 ), 1 );
const quint16 variableIndex = extractBits( 13, 15, currentBytecode );
OOP val = popStack();
ST_TRACE_BYTECODE("var:" << variableIndex << "val:" << memory->prettyValue(val).constData() );
memory->storePointerOfObject( variableIndex + TempFrameStart, memory->getRegister(HomeContext), val );
return true;
}
bool Interpreter::pushReceiverBytecode()
{
OOP val = memory->getRegister(Receiver);
ST_TRACE_BYTECODE("receiver:" << memory->prettyValue(val).constData());
push( val );
return true;
}
static const char* s_constNames[] =
{
"???", "true", "false", "nil", "-1", "0", "1", "2"
};
bool Interpreter::pushConstantBytecode()
{
// "Push (receiver, true, false, nil, -1, 0, 1, 2) [%1]").arg( b & 0x7 ), 1 );
OOP val;
switch( currentBytecode )
{
case 113:
val = ObjectMemory2::objectTrue;
break;
case 114:
val = ObjectMemory2::objectFalse;
break;
case 115:
val = ObjectMemory2::objectNil;
break;
case 116:
val = ObjectMemory2::objectMinusOne;
break;
case 117:
val = ObjectMemory2::objectZero;
break;
case 118:
val = ObjectMemory2::objectOne;
break;
case 119:
val = ObjectMemory2::objectTwo;
break;
default:
Q_ASSERT( false );
break;
}
ST_TRACE_BYTECODE("val:" << memory->prettyValue(val).constData());
push( val );
return true;
}
bool Interpreter::extendedPushBytecode()
{
// "Push (Receiver Variable, Temporary Location, Literal Constant, Literal Variable) [%1] #%2").
// arg( ( bc[pc+1] >> 6 ) & 0x3 ).arg( bc[pc+1] & 0x3f), 2 );
const quint16 descriptor = fetchByte();
const quint16 variableType = extractBits( 8, 9, descriptor );
const quint16 variableIndex = extractBits( 10, 15, descriptor );
OOP val;
switch( variableType )
{
case 0:
val = memory->fetchPointerOfObject( variableIndex, memory->getRegister(Receiver) );
break;
case 1:
val = temporary( variableIndex );
break;
case 2:
val = literal( variableIndex );
break;
case 3:
val = memory->fetchPointerOfObject( ValueIndex, literal( variableIndex ) );
break;
default:
Q_ASSERT( false );
}
ST_TRACE_BYTECODE("val:" << memory->prettyValue(val).constData());
push(val);
return true;
}
bool Interpreter::extendedStoreBytecode(bool subcall)
{
if( !subcall )
{
ST_TRACE_BYTECODE("");
}
// "Store (Receiver Variable, Temporary Location, Illegal, Literal Variable) [%1] #%2").
// arg( ( bc[pc+1] >> 6 ) & 0x3 ).arg( bc[pc+1] & 0x3f), 2 );
const quint16 descriptor = fetchByte();
const quint16 variableType = extractBits( 8, 9, descriptor );
const quint16 variableIndex = extractBits( 10, 15, descriptor );
switch( variableType )
{
case 0:
memory->storePointerOfObject(variableIndex,memory->getRegister(Receiver),stackTop());
break;
case 1:
memory->storePointerOfObject(variableIndex+TempFrameStart,memory->getRegister(HomeContext),stackTop());
break;
case 2:
qCritical() << "ERROR: illegal store";
// BB: self error:
break;
case 3:
memory->storePointerOfObject(ValueIndex, literal(variableIndex), stackTop() );
break;
default:
Q_ASSERT( false );
}
return true;
}
bool Interpreter::extendedStoreAndPopBytecode()
{
ST_TRACE_BYTECODE("");
// "Pop and Store (Receiver Variable, Temporary Location, Illegal, Literal Variable) [%1] #%2").
// arg( ( bc[pc+1] >> 6 ) & 0x3 ).arg( bc[pc+1] & 0x3f), 2 );
extendedStoreBytecode(true);
return popStackBytecode(true);
}
bool Interpreter::popStackBytecode(bool subcall)
{
if( !subcall )
{
ST_TRACE_BYTECODE("");
}
// "Pop Stack Top" ), 1 );
popStack();
return true;
}
bool Interpreter::duplicateTopBytecode()
{
// "Duplicate Stack Top" ), 1 );
OOP val = stackTop();
ST_TRACE_BYTECODE("val:" << memory->prettyValue(val).constData());
push( val );
return true;
}
bool Interpreter::pushActiveContextBytecode()
{
ST_TRACE_BYTECODE("");
// "Push Active Context" ), 1 );
push( memory->getRegister( ActiveContext ) );
return true;
}
bool Interpreter::shortUnconditionalJump()
{
// "Jump %1 + 1 (i.e., 1 through 8)").arg( b & 0x7 ), 1 );
const qint16 offset = extractBits( 13, 15, currentBytecode );
ST_TRACE_BYTECODE("offset:" << offset + 1 );
jump( offset + 1 );
return true;
}
bool Interpreter::shortContidionalJump()
{
// "Pop and Jump 0n False %1 +1 (i.e., 1 through 8)").arg( b & 0x7 ), 1 );
const qint16 offset = extractBits( 13, 15, currentBytecode );
ST_TRACE_BYTECODE("offset:" << offset + 1 );
jumpif( ObjectMemory2::objectFalse, offset + 1 );
return true;
}
bool Interpreter::longUnconditionalJump()
{
// "Jump(%1 - 4) *256+%2").arg( b & 0x7 ).arg( bc[pc+1] ), 2 );
qint16 offset = extractBits( 13, 15, currentBytecode );
offset = ( offset - 4 ) * 256 + fetchByte();
ST_TRACE_BYTECODE("offset:" << offset );
jump( offset );
return true;
}
bool Interpreter::longConditionalJump()
{
// "Pop and Jump On True %1 *256+%2").arg( b & 0x3 ).arg( bc[pc+1] ), 2 );
// "Pop and Jump On False %1 *256+%2").arg( b & 0x3 ).arg( bc[pc+1] ), 2 );
qint16 offset = extractBits( 14, 15, currentBytecode );
offset = offset * 256 + fetchByte();
ST_TRACE_BYTECODE("offset:" << offset );
if( currentBytecode >= 168 && currentBytecode <= 171 )
jumpif( ObjectMemory2::objectTrue, offset );
if( currentBytecode >= 172 && currentBytecode <= 175 )
jumpif( ObjectMemory2::objectFalse, offset );
return true;
}
bool Interpreter::extendedSendBytecode()
{
switch( currentBytecode )
{
case 131:
return singleExtendedSendBytecode();
case 132:
return doubleExtendedSendBytecode();
case 133:
return singleExtendedSuperBytecode();
case 134:
return doubleExtendedSuperBytecode();
default:
Q_ASSERT(false);
return false;
}
}
bool Interpreter::singleExtendedSendBytecode()
{
// "Send Literal Selector #%2 With %1 Arguments").arg( ( bc[pc+1] >> 5 ) & 0x7 ).arg( bc[pc+1] & 0x1f), 2 );
const quint16 descriptor = fetchByte();
const quint16 selectorIndex = extractBits( 11, 15, descriptor );
//Q_ASSERT( selectorIndex == ( descriptor & 0x1f ) );
const quint16 _argumentCount = extractBits( 8, 10, descriptor );
//Q_ASSERT( tmp == ( ( descriptor >> 5 ) & 0x7 ) );
OOP selector = literal(selectorIndex);
ST_TRACE_BYTECODE("selector:"<< memory->prettyValue(selector).constData()
<< "count:" << _argumentCount );
sendSelector( selector, _argumentCount );
return true;
}
bool Interpreter::doubleExtendedSendBytecode()
{
// "Send Literal Selector #%2 With %1 Arguments").arg( bc[pc+1] ).arg( bc[pc+2]), 3 );
const quint8 count = fetchByte();
const OOP selector = literal( fetchByte() );
ST_TRACE_BYTECODE("selector:"<< memory->prettyValue(selector).constData()
<< "count:" << count );
sendSelector( selector, count );
return true;
}
bool Interpreter::singleExtendedSuperBytecode()
{
// "Send Literal Selector #%2 To Superclass With %1 Arguments").arg( ( bc[pc+1] >> 5 ) & 0x7 ).arg( bc[pc+1] & 0x1f), 2 );
const quint16 descriptor = fetchByte();
argumentCount = extractBits( 8, 10, descriptor );
const quint16 selectorIndex = extractBits( 11, 15, descriptor );
const OOP selector = literal( selectorIndex );
memory->setRegister( MessageSelector, selector);
const OOP method = memory->getRegister(Method);
const OOP methodClass = memory->methodClassOf( method );
const OOP super = superclassOf(methodClass);
ST_TRACE_BYTECODE("selector:"<< memory->prettyValue(selector).constData()
<< "super:" << memory->prettyValue(super).constData() );
sendSelectorToClass( super );
return true;
}
bool Interpreter::doubleExtendedSuperBytecode()
{
ST_TRACE_BYTECODE("");
// "Send Literal Selector #%2 To Superclass With %1 Arguments").arg( bc[pc+1] ).arg( bc[pc+2]), 3 );
argumentCount = fetchByte();
const OOP selector = literal( fetchByte() );
memory->setRegister( MessageSelector, selector);
OOP methodClass = memory->methodClassOf( memory->getRegister(Method) );
const OOP super = superclassOf(methodClass);
ST_TRACE_BYTECODE("selector:"<< memory->prettyValue(selector).constData()
<< "super:" << memory->prettyValue(super).constData() );
sendSelectorToClass( super );
return true;
}
bool Interpreter::sendSpecialSelectorBytecode()
{
// see array 0x30 specialSelectors
// "Send Arithmetic Message #%1" ).arg( b & 0xf ), 1 );
// "Send Special Message #%1" ).arg( b & 0xf ), 1 );
if( !specialSelectorPrimitiveResponse() )
{
const quint16 selectorIndex = ( currentBytecode - 176 ) * 2;
OOP selector = memory->fetchPointerOfObject(selectorIndex, ObjectMemory2::specialSelectors );
const quint16 count = fetchIntegerOfObject( selectorIndex + 1, ObjectMemory2::specialSelectors );
ST_TRACE_BYTECODE("selector:"<< memory->prettyValue(selector).constData()
<< "count:" << count );
sendSelector( selector, count );
}else
ST_TRACE_BYTECODE("primitive");
return true;
}
bool Interpreter::sendLiteralSelectorBytecode()
{
// "Send Literal Selector #%1 With No Arguments" ).arg( b & 0xf ), 1 );
// "Send Literal Selector #%1 With 1 Argument" ).arg( b & 0xf ), 1 );
// "Send Literal Selector #%1 With 2 Arguments" ).arg( b & 0xf ), 1 );
const quint16 litNr = extractBits( 12, 15, currentBytecode );
const OOP selector = literal( litNr );
const quint16 argumentCount = extractBits( 10, 11, currentBytecode ) - 1;
ST_TRACE_BYTECODE("selector:"<< memory->prettyValue(selector).constData()
<< "count:" << argumentCount );
sendSelector( selector, argumentCount );
return true;
}
void Interpreter::jump(qint32 offset)
{
instructionPointer += offset;
}
void Interpreter::jumpif(quint16 condition, qint32 offset)
{
const quint16 boolean = popStack();
if( boolean == condition )
jump(offset);
else if( !( boolean == ObjectMemory2::objectTrue || boolean == ObjectMemory2::objectFalse ) )
{
unPop(1);
sendMustBeBoolean();
}
}
void Interpreter::sendSelector(Interpreter::OOP selector, quint16 count)
{
memory->setRegister(MessageSelector, selector );
argumentCount = count;