-
Notifications
You must be signed in to change notification settings - Fork 65
/
InterpreterPrimitives.class.st
5076 lines (4465 loc) · 188 KB
/
InterpreterPrimitives.class.st
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
"
InterpreterPrimitives implements most of the VM's core primitives. It is the root of the interpreter hierarchy so as to share the core primitives amongst the varioius interpreters.
Instance Variables
argumentCount <Integer>
ffiExceptionResponse <Integer>
inFFIFlags <Integer>
messageSelector <Integer>
newMethod <Integer>
nextProfileTick <Integer>
objectMemory <ObjectMemory> (simulation only)
preemptionYields <Boolean>
primFailCode <Integer>
osErrorCode <Integer>
profileMethod <Integer>
profileProcess <Integer>
profileSemaphore <Integer>
secHasEnvironmentAccess <Integer>
argumentCount
- the number of arguments of the current message
ffiExceptionResponse
- controls system response to exceptions during FFI calls. See primitiveFailForFFIException:at:
inFFIFlags
- flags recording currently only whether the system is in an FFI call
messageSelector
- the oop of the selector of the current message
newMethod
- the oop of the result of looking up the current message
nextProfileTick
- the millisecond clock value of the next profile tick (if profiling is in effect)
objectMemory
- the memory manager and garbage collector that manages the heap
preemptionYields
- a boolean controlling the process primitives. If true (old, incorrect, blue-book semantics) a preempted process is sent to the back of its run-queue. If false, a process preempted by a higher-priority process is put back at the head of its run queue, hence preserving cooperative scheduling within priorities.
primFailCode
- primitive success/failure flag, 0 for success, otherwise the reason code for failure
osErrorCode
- a 64-bit value settable by external primitives conveying arbitrary error codes from the operating system and/or system libraries
profileMethod
- the oop of the method at the time nextProfileTick was reached
profileProcess
- the oop of the activeProcess at the time nextProfileTick was reached
profileSemaphore
- the oop of the semaphore to signal when nextProfileTick is reached
secHasEnvironmentAccess
- the function to call to check if access to the envronment should be granted to primitiveGetenv
"
Class {
#name : #InterpreterPrimitives,
#superclass : #AbstractInterpreter,
#instVars : [
'objectMemory',
'messageSelector',
'argumentCount',
'newMethod',
'primFailCode',
'osErrorCode',
'exceptionPC',
'profileMethod',
'profileProcess',
'profileSemaphore',
'nextProfileTick',
'preemptionYields',
'newFinalization',
'ffiExceptionResponse'
],
#classVars : [
'CrossedX',
'EndOfRun',
'MillisecondClockMask'
],
#pools : [
'VMBasicConstants',
'VMBytecodeConstants',
'VMClassIndices',
'VMMethodCacheConstants',
'VMObjectIndices',
'VMStackFrameOffsets'
],
#category : #'VMMaker-Interpreter'
}
{ #category : #'C translation' }
InterpreterPrimitives class >> declareCVarsIn: aCCodeGen [
aCCodeGen
var: 'osErrorCode' type: #sqLong;
var: 'exceptionPC' type: #usqInt
]
{ #category : #'C translation' }
InterpreterPrimitives class >> defineAtCompileTime: anObject [
"Override to define at translation time those variables that need to
be defined at compile time only in plugins, but not in the main VM,
because the VM generated is specific to these varables."
anObject isSymbol ifFalse:
[^false].
(#(STACKVM COGVM SPURVM) includes: anObject) ifTrue:
[^false].
^VMBasicConstants namesDefinedAtCompileTime includes: anObject
]
{ #category : #'class initialization' }
InterpreterPrimitives class >> initializeMiscConstants [
"Initialize the hashMultiply constants."
super initializeMiscConstants.
HashMultiplyConstant := 1664525.
HashMultiplyMask := 16rFFFFFFF "(2 raisedTo: 28) - 1"
]
{ #category : #'primitive support' }
InterpreterPrimitives >> asUnsigned: anInteger [
<inline: true>
^self cCode: [anInteger asUnsignedInteger] inSmalltalk: [anInteger bitAnd: objectMemory maxCInteger]
]
{ #category : #'stack access' }
InterpreterPrimitives >> booleanValueOf: obj [
self subclassResponsibility
]
{ #category : #'primitive support' }
InterpreterPrimitives >> cStringOrNullFor: oop [
"Answer either a malloced string with the null-terminated contents of oop if oop is a string,
or the null pointer if oop is nil, or fail. It is the client's responsibility to free the string later."
<api>
<returnTypeC: #'char *'>
<inline: false>
| isString len cString |
<var: 'cString' type: #'char *'>
isString := self isInstanceOfClassByteString: oop.
isString ifFalse:
[oop ~= objectMemory nilObject ifTrue:
[self primitiveFailFor: PrimErrBadArgument].
^0].
len := objectMemory lengthOf: oop.
len = 0 ifTrue:
[^0].
cString := self malloc: len + 1.
cString ifNil:
[self primitiveFailFor: PrimErrNoCMemory.
^0].
self memcpy: cString _: (objectMemory firstIndexableField: oop) _: len.
cString at: len put: 0.
^cString
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> canBeImmutable: oop [
<option: #IMMUTABILITY>
| scheduler processLists |
self assert: (objectMemory isNonImmediate: oop).
"For now we fail the primitive for contexts to we ensure there are no immutable contexts.
Later we can consider having immutable contexts and send cannotReturn callback
when returning to an immutable context. That would mean that setting a context
to immutable would require a divorce and returns to immutable context are
necessarily across stack pages"
(objectMemory isContext: oop) ifTrue: [ ^ false ].
"Weak structures can't be immutable"
(objectMemory isEphemeron: oop) ifTrue: [^ false].
(objectMemory isWeakNonImm: oop) ifTrue: [^ false].
"No clue what is going on for semaphores so they can't be immutable"
(objectMemory isSemaphoreObj: oop) ifTrue: [^ false].
"Simple version of process management: we forbid Process and LinkedList instances to be immutable
as well as the Processor and the array of activeProcess"
scheduler := objectMemory fetchPointer: ValueIndex ofObject: (objectMemory splObj: SchedulerAssociation).
processLists := objectMemory fetchPointer: ProcessListsIndex ofObject: scheduler.
oop = scheduler ifTrue: [ ^ false ].
oop = processLists ifTrue: [ ^ false ].
"Is it a linkedList ?"
(objectMemory classIndexOf: (objectMemory fetchPointer: 1 ofObject: processLists)) = (objectMemory classIndexOf: oop) ifTrue: [ ^ false ].
"is it a Process ?"
(objectMemory classIndexOf: (objectMemory fetchPointer: ActiveProcessIndex ofObject: scheduler)) = (objectMemory classIndexOf: oop) ifTrue: [ ^ false ].
"The rest of the code is relative to process management: the Processor (the active
process scheduler) can't be immutable, as well as all the objects relative to Process management "
"scheduler := self fetchPointer: ValueIndex ofObject: (self splObj: SchedulerAssociation).
processLists := objectMemory fetchPointer: ProcessListsIndex ofObject: scheduler.
((objectMemory formatOf: oop) = objectMemory nonIndexablePointerFormat)
ifFalse:
[ (objectMemory isArrayNonImm: oop) ifFalse: [ ^ true ].
^ (oop = processLists) not ].
(objectMemory numSlotsOf: oop) >= 2 ifFalse: [ ^ true ].
""is the oop the scheduler itself ?""
oop = scheduler ifTrue: [ ^ false ].
1 to: (objectMemory numSlotsOf: processLists) do: [ :i |
""is the oop one of the linked lists ?""
(list := processLists at: i) = oop ifTrue: [^ false].
""is the oop one of the runnable process ?""
first := objectMemory fetchPointer: FirstLinkIndex ofObject: list.
first = objectMemory nilObject ifFalse:
[ last := objectMemory fetchPointer: LastLinkIndex ofObject: list.
link := first.
[ link = last ] whileFalse:
[ link = oop ifTrue: [ ^ false ].
link := objectMemory fetchPointer: NextLinkIndex ofObject: link. ] ] ]."
^ true
]
{ #category : #'process primitive support' }
InterpreterPrimitives >> doWaitSemaphore: aSemaphoreOop [
| excessSignals activeProc |
<api>
<returnTypeC: #void>
excessSignals := self fetchInteger: ExcessSignalsIndex ofObject: aSemaphoreOop.
excessSignals > 0
ifTrue:
[self storeInteger: ExcessSignalsIndex ofObject: aSemaphoreOop withValue: excessSignals - 1 ]
ifFalse:
[activeProc := self activeProcess.
self addLastLink: activeProc toList: aSemaphoreOop.
self transferTo: self wakeHighestPriority ]
]
{ #category : #'process primitive support' }
InterpreterPrimitives >> doWaitSemaphore: sema reEnterInterpreter: hasToReenter [
<api>
<returnTypeC: #void>
self doWaitSemaphore: sema
]
{ #category : #'primitive support' }
InterpreterPrimitives >> failed [
<api>
<inline: true>
"In C, non-zero is true, so avoid computation by simply answering primFailCode in the C version."
^self cCode: [primFailCode] inSmalltalk: [primFailCode ~= 0]
]
{ #category : #'primitive support' }
InterpreterPrimitives >> floatExponentBits [
"Answer the number of bits in the double-precision exponent. This is an 11-bit field."
<inline: true>
^11
]
{ #category : #'simulation support' }
InterpreterPrimitives >> getenv: aByteStringOrByteArray [
<doNotGenerate>
"The primitiveGetenv: primitive answers nil for undefined variables.
The primitiveGetenv implementation is written to expect getenv: to
answer 0, not nil, for undefined variables. Map nil to 0 for simulation."
"aByteStringOrByteArray is probably null terminated, convert to non-null-terminated"
^(self primitiveGetenv: (self asString: aByteStringOrByteArray)) ifNil: [0]
]
{ #category : #'primitive support' }
InterpreterPrimitives >> initPrimCall [
"Set the failure code/success flag in preparation for calling a primitve.
If primFailCode is non-zero a primitive has failed. If primFailCode is
greater than one then its value indicates the reason for failure."
<inline: true>
primFailCode := 0
]
{ #category : #initialization }
InterpreterPrimitives >> initialize [
"Here we can initialize the variables C initializes to zero. #initialize methods do /not/ get translated."
argumentCount := primFailCode := nextProfileTick := osErrorCode := exceptionPC := ffiExceptionResponse := 0.
newFinalization := false
]
{ #category : #'simulation support' }
InterpreterPrimitives >> ioGetCurrentWorkingDirectory: aCStringHolder maxLength: maxLength [
<doNotGenerate>
| anEncodedString |
anEncodedString := FileLocator workingDirectory fullName utf8Encoded.
aCStringHolder object
replaceFrom: 1
to: anEncodedString size
with: anEncodedString
startingAt: 1.
"No Error"
^ 0
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> isAppropriateForCopyObject: oop [
^objectMemory isPointersNonImm: oop
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isDirectAlien: oop [
^(self sizeFieldOfAlien: oop) > 0
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isFinite: aDouble [
<var: #aDouble type: #double>
<inline: true>
^aDouble - aDouble = 0.0
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isIndirectAlien: oop [
^(self sizeFieldOfAlien: oop) < 0
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isInstanceOfClassArray: oop [
<inline: true>
"N.B. Because Slang always inlines is:instanceOf:compactClassIndex:
(because is:instanceOf:compactClassIndex: has an inline: pragma) the
phrase (objectMemory splObj: ClassArray) is expanded in-place and
is _not_ evaluated if oop has a non-zero CompactClassIndex."
^objectMemory
is: oop
instanceOf: (objectMemory splObj: ClassArray)
compactClassIndex: ClassArrayCompactIndex
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isInstanceOfClassBlockClosure: oop [
<inline: true>
"N.B. Because Slang always inlines is:instanceOf:compactClassIndex:
(because is:instanceOf:compactClassIndex: has an inline: pragma) the
phrase (objectMemory splObj: ClassBlockClosure) is expanded in-place
and is _not_ evaluated if oop has a non-zero CompactClassIndex."
^objectMemory
is: oop
instanceOf: nil
compactClassIndex: ClassFullBlockClosureCompactIndex
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isInstanceOfClassByteString: oop [
<inline: true>
"N.B. Because Slang always inlines is:instanceOf:compactClassIndex:
(because is:instanceOf:compactClassIndex: has an inline: pragma) the
phrase (objectMemory splObj: ClassByteString) is expanded in-place and
is _not_ evaluated if oop has a non-zero CompactClassIndex."
^objectMemory
is: oop
instanceOf: (objectMemory splObj: ClassByteString)
compactClassIndex: ClassByteStringCompactIndex
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isNegativeIntegerValueOf: oop [
"Answer true if integer object is negative.
Fail if object pointed by oop i not an integer."
| ok smallInt |
(objectMemory isIntegerObject: oop) ifTrue:
[smallInt := objectMemory integerValueOf: oop.
^smallInt < 0].
(objectMemory isNonIntegerNonImmediate: oop) ifTrue:
[ok := objectMemory isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargePositiveInteger)
compactClassIndex: ClassLargePositiveIntegerCompactIndex.
ok ifTrue: [^false].
ok := objectMemory isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargeNegativeInteger)
compactClassIndex: ClassLargeNegativeIntegerCompactIndex.
ok ifTrue: [^true]].
self primitiveFail.
^false
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isPointerAlien: oop [
^(self sizeFieldOfAlien: oop) = 0
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isPositiveMachineIntegerObject: oop [
"Answer if oop is a value of an integer in address range, i.e up to the size of a machine word.
The object may be either a positive SmallInteger or a LargePositiveInteger of size <= word size."
| ok |
(objectMemory isIntegerObject: oop) ifTrue:
[^(objectMemory integerValueOf: oop) >= 0].
(objectMemory isNonIntegerImmediate: oop) ifTrue:
[^false].
ok := objectMemory
isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargePositiveInteger)
compactClassIndex: ClassLargePositiveIntegerCompactIndex.
^ok and: [(objectMemory numBytesOfBytes: oop) <= (self sizeof: #'usqIntptr_t')]
]
{ #category : #'primitive support' }
InterpreterPrimitives >> isSignedInteger: integer inRangeForBits: nBits [
"Answer if integer will fit within a variable of nBits, where nBits is 8, 16, 32 or 64.
Signed shift right by nBits - 1 to map in-range values to either 0 or -1.
Add one to map in-range values to 0 or 1.
Perform an unsigned comparison for greater than 1 to eliminate values out of range."
<inline: true>
^self cCode: [(self asUnsigned: (integer signedBitShift: 1 - nBits) + 1) <= 1]
inSmalltalk: [((integer bitShift: 1 - nBits) + 1 bitAnd: objectMemory maxCInteger) <= 1]
]
{ #category : #'primitive support' }
InterpreterPrimitives >> magnitude64BitIntegerFor: magnitude neg: isNegative [
"Return a Large Integer object for the given integer magnitude and sign"
| newLargeInteger largeClass highWord sz isSmall smallVal |
<var: 'magnitude' type: #usqLong>
<var: 'highWord' type: #usqInt>
isSmall := isNegative
ifTrue: [magnitude <= (objectMemory maxSmallInteger + 1)]
ifFalse: [magnitude <= objectMemory maxSmallInteger].
isSmall ifTrue:
[smallVal := self cCoerceSimple: magnitude to: #sqInt.
isNegative ifTrue: [smallVal := 0 - smallVal].
^objectMemory integerObjectOf: smallVal].
largeClass := isNegative
ifTrue: [objectMemory classLargeNegativeInteger]
ifFalse: [objectMemory classLargePositiveInteger].
objectMemory wordSize = 8
ifTrue: [sz := 8]
ifFalse:
[(highWord := magnitude >> 32) = 0
ifTrue: [sz := 4]
ifFalse:
[sz := 5.
(highWord := highWord >> 8) = 0 ifFalse:
[sz := sz + 1.
(highWord := highWord >> 8) = 0 ifFalse:
[sz := sz + 1.
(highWord := highWord >> 8) = 0 ifFalse: [sz := sz + 1]]]]].
newLargeInteger := objectMemory instantiateClass: largeClass indexableSize: sz.
SPURVM
ifTrue:
["Memory is eight byte aligned in SPUR, so we are sure to have room for 64bits word whatever allocated sz"
objectMemory storeLong64: 0 ofObject: newLargeInteger withValue: (objectMemory byteSwapped64IfBigEndian: magnitude)]
ifFalse:
[sz > 4
ifTrue: [objectMemory storeLong64: 0 ofObject: newLargeInteger withValue: (objectMemory byteSwapped64IfBigEndian: magnitude)]
ifFalse: [objectMemory storeLong32: 0 ofObject: newLargeInteger withValue: (objectMemory byteSwapped32IfBigEndian: magnitude)]].
^newLargeInteger
]
{ #category : #'primitive support' }
InterpreterPrimitives >> magnitude64BitValueOf: oop [
"Convert the given object into an integer value.
The object may be either a positive SmallInteger or an eight-byte LargeInteger."
| sz value ok smallIntValue |
<returnTypeC: #usqLong>
<var: #value type: #usqLong>
(objectMemory isIntegerObject: oop) ifTrue:
[smallIntValue := (objectMemory integerValueOf: oop).
smallIntValue < 0 ifTrue: [smallIntValue := 0 - smallIntValue].
^self cCoerce: smallIntValue to: #usqLong].
(objectMemory isNonIntegerImmediate: oop) ifTrue:
[self primitiveFail.
^0].
ok := objectMemory isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargePositiveInteger)
compactClassIndex: ClassLargePositiveIntegerCompactIndex.
ok
ifFalse:
[ok := objectMemory isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargeNegativeInteger)
compactClassIndex: ClassLargeNegativeIntegerCompactIndex.
ok ifFalse:
[self primitiveFail.
^0]].
sz := objectMemory numBytesOfBytes: oop.
sz > (self sizeof: #sqLong) ifTrue:
[self primitiveFail.
^0].
"self cppIf: SPURVM
ifTrue:
[""Memory is 8 byte aligned in Spur and oversized bytes are set to zero, so we can safely fetch 8 bytes""
value := objectMemory byteSwapped64IfBigEndian: (objectMemory fetchLong64: 0 ofObject: oop)]
ifFalse:
["sz > 4
ifTrue: [value := objectMemory byteSwapped64IfBigEndian: (objectMemory fetchLong64: 0 ofObject: oop)]
ifFalse: [value := self cCoerceSimple: (objectMemory byteSwapped32IfBigEndian: (objectMemory fetchLong32: 0 ofObject: oop)) to: #'unsigned int']"]".
^value
]
{ #category : #'primitive support' }
InterpreterPrimitives >> maybeInlinePositive32BitValueOf: oop [
"Convert the given object into an integer value.
The object may be either a positive SmallInteger or a four-byte LargePositiveInteger."
<notOption: #Spur64BitMemoryManager>
<returnTypeC: #'unsigned int'>
| value ok sz |
(objectMemory isIntegerObject: oop) ifTrue:
[value := objectMemory integerValueOf: oop.
(value < 0) ifTrue:
[self primitiveFail. value := 0].
^value].
(objectMemory isNonIntegerImmediate: oop)
ifTrue:
[self primitiveFail.
^0]
ifFalse:
[ok := objectMemory
isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargePositiveInteger)
compactClassIndex: ClassLargePositiveIntegerCompactIndex.
ok ifFalse:
[self primitiveFail.
^0].
sz := objectMemory numBytesOfBytes: oop.
sz > 4 ifTrue:
[self primitiveFail.
^0].
^self cCoerceSimple: (objectMemory byteSwapped32IfBigEndian: (objectMemory fetchLong32: 0 ofObject: oop)) to: #'unsigned int']
]
{ #category : #arithmetic }
InterpreterPrimitives >> multiplySmallInteger: anInteger withSmallInteger: anotherInteger ifOverflow: aBlock [
"Multiply two signed integers and detect signed overflow"
| a b result |
a := objectMemory integerValueOf: anInteger.
b := objectMemory integerValueOf: anotherInteger.
(a > 0
ifTrue: [
b > 0
ifTrue: [ a > (objectMemory maxSmallInteger / b) ]
ifFalse: [ b < (objectMemory minSmallInteger / a) ] ]
ifFalse: [
b > 0
ifTrue: [ a < (objectMemory minSmallInteger / b) ]
ifFalse: [
a ~= 0 and: [ b < (objectMemory maxSmallInteger / a) ] ] ])
ifTrue: aBlock.
result := a * b.
^ objectMemory integerObjectOf: result
]
{ #category : #'simulation support' }
InterpreterPrimitives >> newMethod: oop [
<doNotGenerate>
newMethod := oop
]
{ #category : #'primitive support' }
InterpreterPrimitives >> noInlineSigned32BitValueGutsOf: oop [
"Convert the given object into an integer value.
The object may be a four-byte LargeInteger."
| value negative ok magnitude |
<notOption: #Spur64BitMemoryManager>
<inline: false>
<returnTypeC: #int>
<var: #value type: #int>
<var: #magnitude type: #'unsigned int'>
self deny: objectMemory hasSixtyFourBitImmediates.
self deny: (objectMemory isIntegerObject: oop).
(objectMemory isNonIntegerImmediate: oop) ifTrue:
[self primitiveFail.
^0].
ok := objectMemory
isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargePositiveInteger)
compactClassIndex: ClassLargePositiveIntegerCompactIndex.
ok
ifTrue: [negative := false]
ifFalse:
[negative := true.
ok := objectMemory isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargeNegativeInteger)
compactClassIndex: ClassLargeNegativeIntegerCompactIndex.
ok ifFalse:
[self primitiveFail.
^0]].
(objectMemory numBytesOfBytes: oop) > 4 ifTrue:
[^self primitiveFail].
magnitude := self cCoerceSimple: (objectMemory byteSwapped32IfBigEndian: (objectMemory fetchLong32: 0 ofObject: oop)) to: #'unsigned int'.
(negative
ifTrue: [magnitude > 16r80000000]
ifFalse: [magnitude >= 16r80000000])
ifTrue:
[self primitiveFail.
^0].
negative
ifTrue: [value := 0 - magnitude]
ifFalse: [value := magnitude].
^value
]
{ #category : #'stack access' }
InterpreterPrimitives >> pop: nItems thenPushBool: boolean [
self subclassResponsibility
]
{ #category : #'primitive support' }
InterpreterPrimitives >> positive32BitValueOf: oop [
"Convert the given object into an integer value.
The object may be either a positive SmallInteger or a four-byte LargePositiveInteger."
<returnTypeC: #'unsigned int'>
objectMemory hasSixtyFourBitImmediates
ifTrue:
[(objectMemory isIntegerObject: oop) ifTrue:
[| value64 |
value64 := objectMemory integerValueOf: oop.
(value64 < 0
or: [self cCode: [(self cCoerceSimple: value64 to: #'unsigned int') ~= value64]
inSmalltalk: [value64 >> 32 ~= 0]]) ifTrue:
[self primitiveFail. value64 := 0].
^value64].
self primitiveFail.
^0]
ifFalse:
[^self maybeInlinePositive32BitValueOf: oop]
]
{ #category : #'primitive support' }
InterpreterPrimitives >> positive64BitValueOf: oop [
"Convert the given object into an integer value.
The object may be either a positive SmallInteger or an eight-byte LargePositiveInteger."
<returnTypeC: #usqLong>
| sz value ok |
<var: #value type: #usqLong>
(objectMemory isIntegerObject: oop) ifTrue:
[(objectMemory integerValueOf: oop) < 0 ifTrue:
[^self primitiveFail].
^objectMemory integerValueOf: oop].
(objectMemory isNonIntegerImmediate: oop) ifTrue:
[self primitiveFail.
^0].
ok := objectMemory
isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargePositiveInteger)
compactClassIndex: ClassLargePositiveIntegerCompactIndex.
ok ifFalse:
[self primitiveFail.
^0].
sz := objectMemory numBytesOfBytes: oop.
sz > (self sizeof: #sqLong) ifTrue:
[self primitiveFail.
^0].
"self cppIf: SPURVM
ifTrue:
[""Memory is 8 byte aligned in Spur and oversized bytes are set to zero, so we can safely fetch 8 bytes""
value := objectMemory byteSwapped64IfBigEndian: (objectMemory fetchLong64: 0 ofObject: oop)]
ifFalse:
["sz > 4
ifTrue: [value := objectMemory byteSwapped64IfBigEndian: (objectMemory fetchLong64: 0 ofObject: oop)]
ifFalse: [value := self cCoerceSimple: (objectMemory byteSwapped32IfBigEndian: (objectMemory fetchLong32: 0 ofObject: oop)) to: #'unsigned int']"]".
^value
]
{ #category : #'primitive support' }
InterpreterPrimitives >> positiveMachineIntegerValueOf: oop [
"Answer a value of an integer in address range, i.e up to the size of a machine word.
The object may be either a positive SmallInteger or a LargePositiveInteger of size <= word size."
<returnTypeC: #'usqIntptr_t'>
<inline: true> "only two callers & one is primitiveNewWithArg"
| value bs ok |
(objectMemory isIntegerObject: oop) ifTrue:
[value := objectMemory integerValueOf: oop.
value < 0 ifTrue: [^self primitiveFail].
^value].
"If not a small integer, exit druid compilation"
self druidExitPoint.
(objectMemory isNonIntegerImmediate: oop) ifTrue:
[self primitiveFail.
^0].
ok := objectMemory
isClassOfNonImm: oop
equalTo: (objectMemory splObj: ClassLargePositiveInteger)
compactClassIndex: ClassLargePositiveIntegerCompactIndex.
ok ifFalse:
[self primitiveFail.
^0].
bs := objectMemory numBytesOfBytes: oop.
bs > (self sizeof: #'usqIntptr_t') ifTrue:
[self primitiveFail.
^0].
"self cppIf: SPURVM
ifTrue: [""Memory is 8 byte aligned in Spur and oversized bytes are set to zero, so we can safely fetch 8 bytes""
^objectMemory byteSwapped64IfBigEndian: (objectMemory fetchLong64: 0 ofObject: oop)]
ifFalse: ["((self sizeof: #'usqIntptr_t') = 8
and: [bs > 4])
ifTrue:
[^objectMemory byteSwapped64IfBigEndian: (objectMemory fetchLong64: 0 ofObject: oop)]
ifFalse:
[^self cCoerceSimple: (objectMemory byteSwapped32IfBigEndian: (objectMemory fetchLong32: 0 ofObject: oop)) to: #'unsigned int']"]"
]
{ #category : #'arithmetic integer primitives' }
InterpreterPrimitives >> primitiveAdd [
<numberOfArguments: 1>
<customisedReceiverFor: #smallInteger>
| maybeSmallInteger maybeSmallInteger2 result |
maybeSmallInteger := self stackValue: 0.
maybeSmallInteger2 := self stackValue: 1.
(objectMemory isIntegerObject: maybeSmallInteger)
ifFalse: [ ^ self primitiveFail ].
(objectMemory isIntegerObject: maybeSmallInteger2)
ifFalse: [ ^ self primitiveFail ].
"Check for overflow"
result := self
sumSmallInteger: maybeSmallInteger
withSmallInteger: maybeSmallInteger2
ifOverflow: [ ^ self primitiveFail ].
self pop: 2 thenPush: result
]
{ #category : #'arithmetic largeint primitives' }
InterpreterPrimitives >> primitiveAddLargeIntegers [
"Primitive arithmetic operations for large integers in 64 bit range"
| a b result oopResult aIsNegative bIsNegative resultIsNegative oopArg oopRcvr |
<export: true>
<var: 'a' type: 'usqLong'>
<var: 'b' type: 'usqLong'>
<var: 'result' type: 'usqLong'>
oopArg := self stackValue: 0.
oopRcvr := self stackValue: 1.
aIsNegative := self isNegativeIntegerValueOf: oopRcvr.
bIsNegative := self isNegativeIntegerValueOf: oopArg.
a := self magnitude64BitValueOf: oopRcvr.
b := self magnitude64BitValueOf: oopArg.
self successful ifFalse:[^nil].
(aIsNegative = bIsNegative)
ifTrue:
["Protect against overflow"
a > (16rFFFFFFFFFFFFFFFF - b) ifTrue: [self primitiveFail. ^nil].
result := a + b.
resultIsNegative := aIsNegative]
ifFalse:
[(a >= b)
ifTrue:
[result := a - b.
resultIsNegative := aIsNegative]
ifFalse:
[result := b - a.
resultIsNegative := bIsNegative]].
oopResult := self magnitude64BitIntegerFor: result neg: resultIsNegative.
self successful ifTrue:[self pop: 2 thenPush: oopResult].
]
{ #category : #'arithmetic integer primitives' }
InterpreterPrimitives >> primitiveAddOld [
<numberOfArguments: 1>
self pop2AndPushIntegerIfOK: (self stackIntegerValue: 1) + (self stackIntegerValue: 0)
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveAdoptInstance [
"Primitive. Change the class of the argument to make it an instance of the receiver
given that the format of the receiver matches the format of the argument's class.
Fail if receiver or argument are SmallIntegers, or the receiver is an instance of a
compact class and the argument isn't, or when the argument's class is compact and
the receiver isn't, or when the format of the receiver is different from the format of
the argument's class, or when the arguments class is fixed and the receiver's size
differs from the size that an instance of the argument's class should have."
| rcvr arg err |
arg := self stackTop.
rcvr := self stackValue: 1.
((objectMemory isImmediate: arg)
or: [argumentCount > 1
and: [(objectMemory isImmediate: rcvr)
or: [(self objCouldBeClassObj: rcvr) not]]]) ifTrue:
[^self primitiveFailFor: PrimErrBadArgument].
err := objectMemory changeClassOf: arg to: rcvr.
err = 0
ifTrue:
["Flush at cache because rcvr's class has changed."
self flushAtCache.
self pop: self methodArgumentCount]
ifFalse:
["changeClassOf:to: answers errors as if rcvr (the class) is an argument..."
err = PrimErrBadReceiver
ifTrue:
[err := PrimErrBadArgument]
ifFalse:
[err = PrimErrBadArgument ifTrue:
[err := PrimErrBadReceiver]].
self primitiveFailFor: err].
^nil
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveAllInstances [
"Answer an array of all instances of the receiver that exist
when the primitive is called, excluding any that may be
garbage collected as a side effect of allocating the result array."
<export: true>
| result |
result := objectMemory allInstancesOf: self stackTop.
(objectMemory isIntegerObject: result) ifTrue:
[objectMemory growToAccomodateContainerWithNumSlots: (objectMemory integerValueOf: result).
result := objectMemory allInstancesOf: self stackTop.
(objectMemory isIntegerObject: result) ifTrue:
[^self primitiveFailFor: PrimErrNoMemory]].
self pop: argumentCount+1 thenPush: result
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveAllObjects [
"Answer an array of all objects that exist when the primitive
is called, excluding those that may be garbage collected as
a side effect of allocating the result array."
<export: true>
| result |
result := objectMemory allObjects.
(objectMemory isIntegerObject: result) ifTrue:
[objectMemory growToAccomodateContainerWithNumSlots: (objectMemory integerValueOf: result).
result := objectMemory allObjects.
(objectMemory isIntegerObject: result) ifTrue:
[^self primitiveFailFor: PrimErrNoMemory]].
self pop: argumentCount+1 thenPush: result
]
{ #category : #'arithmetic float primitives' }
InterpreterPrimitives >> primitiveArctan [
"N.B. IMO we should be able to assume the receiver is a float because this primitive is specific to floats. eem 2/13/2017"
| rcvr |
<var: #rcvr type: #double>
rcvr := self stackFloatValue: 0.
self successful ifTrue:
[self stackTopPut: (objectMemory floatObjectOf:
(self cCode: [rcvr atan]
inSmalltalk: [rcvr arcTan]))]
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveArrayBecome [
"We must flush the method cache here, to eliminate stale references
to mutated classes and/or selectors."
| arg rcvr ec |
arg := self stackTop.
rcvr := self stackValue: 1.
ec := objectMemory become: rcvr with: arg twoWay: true copyHash: false.
ec = PrimNoErr
ifTrue: [self pop: 1]
ifFalse: [self primitiveFailFor: ec]
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveArrayBecomeOneWay [
"We must flush the method cache here, to eliminate stale references
to mutated classes and/or selectors."
| arg rcvr ec |
arg := self stackTop.
rcvr := self stackValue: 1.
ec := objectMemory become: rcvr with: arg twoWay: false copyHash: true.
ec = PrimNoErr
ifTrue: [self pop: 1]
ifFalse: [self primitiveFailFor: ec]
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveArrayBecomeOneWayCopyHash [
"Similar to primitiveArrayBecomeOneWay but accepts a third argument deciding whether to
copy the receiver's elements identity hashes over the argument's elements identity hashes."
| copyHashFlag ec |
self stackTop = objectMemory trueObject
ifTrue: [copyHashFlag := true]
ifFalse:
[self stackTop = objectMemory falseObject
ifTrue: [copyHashFlag := false]
ifFalse:
[self primitiveFailFor: PrimErrBadArgument.
^nil]].
ec := objectMemory
become: (self stackValue: 2)
with: (self stackValue: 1)
twoWay: false
copyHash: copyHashFlag.
ec = PrimNoErr
ifTrue: [self pop: argumentCount]
ifFalse: [self primitiveFailFor: ec]
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveArrayBecomeOneWayNoCopyHash [
"Similar to primitiveArrayBecomeOneWay but does /not/ copy the receiver's
elements identity hashes over the argument's elements identity hashes."
| arg rcvr ec |
arg := self stackTop.
rcvr := self stackValue: 1.
ec := objectMemory become: rcvr with: arg twoWay: false copyHash: false.
ec = PrimNoErr
ifTrue: [self pop: 1]
ifFalse: [self primitiveFailFor: ec]
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveAsCharacter [
| characterCode characterObject |
characterCode := self stackTop.
((objectMemory isIntegerObject: characterCode)
and: [characterCode := objectMemory integerValueOf: characterCode.
objectMemory isInRangeCharacterCode: characterCode]) ifFalse:
[^self primitiveFailFor: (argumentCount = 0
ifTrue: [PrimErrBadReceiver]
ifFalse: [PrimErrBadArgument])].
characterObject := self characterForAscii: characterCode.
self pop: argumentCount + 1 thenPush: characterObject
]
{ #category : #'arithmetic float primitives' }
InterpreterPrimitives >> primitiveAsFloat [
"N.B. This will answer inexact results for integers with > 53 bits of magnitude."
| rcvr |
<numberOfArguments: 0>
rcvr := self stackTop.
self assert: (objectMemory isIntegerObject: rcvr).
self pop: 1 thenPushFloat: (objectMemory integerValueOf: rcvr) asFloat
]
{ #category : #'indexing primitives' }
InterpreterPrimitives >> primitiveAt [
<accessorDepth: 0>
<numberOfArguments: 1>
self commonAt: false
]
{ #category : #'indexing primitives' }
InterpreterPrimitives >> primitiveAtPut [
<accessorDepth: 0>
self commonAtPut: false
]
{ #category : #'object access primitives' }
InterpreterPrimitives >> primitiveBehaviorHash [
| hashOrError |
self assert: ((objectMemory isNonImmediate: self stackTop)
and: [self addressCouldBeClassObj: self stackTop]).
hashOrError := objectMemory ensureBehaviorHash: self stackTop.
hashOrError >= 0
ifTrue: [self pop: argumentCount + 1 thenPushInteger: hashOrError]
ifFalse: [self primitiveFailFor: hashOrError negated]
]
{ #category : #'arithmetic integer primitives' }
InterpreterPrimitives >> primitiveBitAnd [
<numberOfArguments: 1>
<inline: false>
<var: 'integerArgumentValue' type: #usqInt>
<var: 'integerReceiverValue' type: #usqInt>
| integerArgumentValue integerReceiverValue |
"Note no short-cut for SmallIntegers. Either the inline interpreter bytecode or the JIT primitive will handle this case."