-
Notifications
You must be signed in to change notification settings - Fork 71
/
CoInterpreterPrimitives.class.st
810 lines (770 loc) · 36.1 KB
/
CoInterpreterPrimitives.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
Class {
#name : #CoInterpreterPrimitives,
#superclass : #CoInterpreter,
#category : #'VMMaker-JIT'
}
{ #category : #'object access primitives' }
CoInterpreterPrimitives >> frameIsMarked: theFPInt [
| methodField |
methodField := stackPages longAt: theFPInt + FoxMethod.
^methodField asUnsignedInteger < objectMemory getMemoryMap startOfObjectMemory
ifTrue: [(methodField bitAnd: 4) ~= 0]
ifFalse: [((stackPages longAt: theFPInt + FoxIFrameFlags) bitAnd: 2) ~= 0]
]
{ #category : #'object access primitives' }
CoInterpreterPrimitives >> markFrame: theFPInt [
| methodField |
methodField := stackPages unsignedLongAt: theFPInt + FoxMethod.
methodField asUnsignedInteger < objectMemory getMemoryMap startOfObjectMemory
ifTrue:
[stackPages
unsignedLongAt: theFPInt + FoxMethod
put: (methodField bitOr: 4)]
ifFalse:
[stackPages
unsignedLongAt: theFPInt + FoxIFrameFlags
put: ((stackPages longAt: theFPInt + FoxIFrameFlags) bitOr: 2)]
]
{ #category : #'arithmetic primitives' }
CoInterpreterPrimitives >> mcprimHashMultiply: receiverArg [
"Machine code primitive for hash multiply. c.f. primitiveHashMultiply.
mcprims consume receiver and arguments as parameters and answer the
result on success, or set the primitive error code and answer 0 on failure."
"Implement 28-bit hashMultiply for SmallInteger and LargePositiveInteger receivers."
<api>
| value |
(objectMemory isIntegerObject: receiverArg)
ifTrue: [value := objectMemory integerValueOf: receiverArg]
ifFalse:
[| ok |
ok := objectMemory is: receiverArg instanceOf: (objectMemory splObj: ClassLargePositiveInteger) compactClassIndex: ClassLargePositiveIntegerCompactIndex.
ok ifFalse:
[self primitiveFailFor: PrimErrBadReceiver.
^0].
value := objectMemory byteSwapped32IfBigEndian: (objectMemory fetchLong32: 0 ofObject: receiverArg)].
^objectMemory integerObjectOf: (value * HashMultiplyConstant bitAnd: 16rFFFFFFF)
]
{ #category : #'object access primitives' }
CoInterpreterPrimitives >> pathTo: goal using: stack followWeak: followWeak [
"Trace objects and frames from the root, marking visited objects, pushing the current path on stack, until goal is found.
If found, unmark, leaving path in stack, and answer 0. Otherwise answer an error:
PrimErrBadArgument if stack is not an Array
PrimErrBadIndex if search overflows stack
PrimErrNotFound if goal cannot be found"
| current index next stackSize stackp freeStartAtStart |
<var: #index type: #sqInt> "Force the sign because typeInference does not seem to work"
(objectMemory isArray: stack) ifFalse:
[^PrimErrBadArgument].
self assert: objectMemory allObjectsUnmarked.
freeStartAtStart := objectMemory freeStart. "check no allocations during search"
objectMemory beRootIfOld: stack. "so no store checks are necessary on stack"
stackSize := objectMemory lengthOf: stack.
objectMemory mark: stack.
"no need. the current context is not reachable from the active process (suspendedContext is nil)"
"objectMemory mark: self activeProcess."
current := objectMemory specialObjectsOop.
objectMemory mark: current.
index := objectMemory lengthOf: current.
stackp := 0.
[[(index := index - 1) >= -1] whileTrue:
[(stackPages couldBeFramePointer: current)
ifTrue:
[next := index >= 0
ifTrue: [self field: index ofFrame: (self cCoerceSimple: current to: #'char *')]
ifFalse: [objectMemory nilObject]]
ifFalse:
[index >= 0
ifTrue:
[next := (objectMemory isContextNonImm: current)
ifTrue: [self fieldOrSenderFP: index ofContext: current]
ifFalse: [objectMemory fetchPointer: index ofObject: current]]
ifFalse:
[next := objectMemory fetchClassOfNonImm: current]].
(stackPages couldBeFramePointer: next)
ifTrue: [self assert: (self isFrame: (self cCoerceSimple: next to: #'char *')
onPage: (stackPages stackPageFor: (self cCoerceSimple: next to: #'char *')))]
ifFalse:
[next >= objectMemory getMemoryMap startOfObjectMemory ifTrue: "exclude Cog methods"
[self assert: (self checkOkayOop: next)]].
next = goal ifTrue:
[self assert: freeStartAtStart = objectMemory freeStart.
self unmarkAfterPathTo.
objectMemory storePointer: stackp ofObject: stack withValue: current.
self pruneStack: stack stackp: stackp.
^0].
((objectMemory isNonIntegerObject: next)
and: [(stackPages couldBeFramePointer: next)
ifTrue: [(self frameIsMarked: next) not]
ifFalse:
[next >= objectMemory getMemoryMap startOfObjectMemory "exclude Cog methods"
and: [(objectMemory isMarked: next) not
and: [((objectMemory isPointers: next) or: [objectMemory isCompiledMethod: next])
and: [followWeak or: [(objectMemory isWeakNonImm: next) not]]]]]])
ifTrue:
[stackp + 2 > stackSize ifTrue:
[self assert: freeStartAtStart = objectMemory freeStart.
self unmarkAfterPathTo.
objectMemory nilFieldsOf: stack.
^PrimErrBadIndex]. "PrimErrNoMemory ?"
objectMemory
storePointerUnchecked: stackp ofObject: stack withValue: current;
storePointerUnchecked: stackp + 1 ofObject: stack withValue: (objectMemory integerObjectOf: index).
stackp := stackp + 2.
(stackPages couldBeFramePointer: (self cCoerceSimple: next to: #'char *'))
ifTrue:
[self markFrame: next.
index := self fieldsInFrame: (self cCoerceSimple: next to: #'char *')]
ifFalse:
[objectMemory mark: next.
(objectMemory isCompiledMethod: next)
ifTrue: [index := (objectMemory literalCountOf: next) + LiteralStart]
ifFalse: [index := objectMemory lengthOf: next]].
current := next]].
current = objectMemory specialObjectsOop ifTrue:
[self assert: freeStartAtStart = objectMemory freeStart.
self unmarkAfterPathTo.
objectMemory nilFieldsOf: stack.
^PrimErrNotFound].
index := objectMemory integerValueOf: (objectMemory fetchPointer: stackp - 1 ofObject: stack).
current := objectMemory fetchPointer: stackp - 2 ofObject: stack.
stackp := stackp - 2] repeat
]
{ #category : #'method introspection support' }
CoInterpreterPrimitives >> pcDataFor: cogMethod [
<var: #cogMethod type: #'CogMethod *'>
| cm nSlots nEntries data |
cm := cogMethod methodObject.
nSlots := (objectMemory byteSizeOf: cm) - (self startPCOfMethod: cm) * 2 + objectMemory minSlotsForShortening.
data := objectMemory instantiateClass: (objectMemory splObj: ClassArray) indexableSize: nSlots.
data ifNil: [^-1].
nEntries := cogit mapPCDataFor: cogMethod into: data.
nEntries = 0 ifTrue:
[^0].
nEntries < nSlots ifTrue:
[objectMemory shorten: data toIndexableSize: nEntries].
^data
]
{ #category : #'method introspection primitives' }
CoInterpreterPrimitives >> primitiveAllMethodsCompiledToMachineCode [
<export: true>
<option: #SistaCogit>
| arrayObj nEntries maxNumEntries |
argumentCount ~= 0 ifTrue:
[^self primitiveFailFor: PrimErrBadNumArgs].
maxNumEntries := cogit numMethods + objectMemory minSlotsForShortening.
arrayObj := objectMemory
instantiateClass: (objectMemory splObj: ClassArray)
indexableSize: maxNumEntries.
arrayObj ifNil: [^-1].
nEntries := cogit methodsCompiledToMachineCodeInto: arrayObj.
(nEntries < maxNumEntries) ifTrue:
[objectMemory shorten: arrayObj toIndexableSize: nEntries].
self pop: 1 thenPush: arrayObj
]
{ #category : #'process primitives' }
CoInterpreterPrimitives >> primitiveCollectCogCodeConstituents [
"Answer the contents of the code zone as an array of pair-wise element, address in ascending
address order. Answer a string for a runtime routine or abstract label (beginning, end, etc),
a CompiledMethod for a CMMethod, or a selector (presumably a Symbol) for a PIC.
If there is an argument and it is true, then collect inner information about the CogMethod."
| constituents withDetails |
argumentCount = 0
ifTrue: [withDetails := false]
ifFalse:
[withDetails := self stackTop.
(withDetails = objectMemory trueObject
or: [withDetails = objectMemory falseObject]) ifFalse:
[^self primitiveFailFor: PrimErrBadArgument].
withDetails := withDetails = objectMemory trueObject].
constituents := cogit cogCodeConstituents: withDetails.
constituents ifNil:
[^self primitiveFailFor: PrimErrNoMemory].
self pop: argumentCount + 1 thenPush: constituents
]
{ #category : #'indexing primitives' }
CoInterpreterPrimitives >> primitiveContextXray [
"Lift the veil from a context and answer an integer describing its interior state.
Used for e.g. VM tests so they can verify they're testing what they think they're testing.
0 implies a vanilla heap context.
Bit 0 = is or was married to a frame
Bit 1 = is still married to a frame
Bit 2 = frame is executing machine code
Bit 3 = has machine code pc (as opposed to nil or a bytecode pc)
Bit 4 = method is currently compiled to machine code"
| context pc flags theFP theMethod |
<var: #theFP type: #'char *'>
context := self stackTop.
pc := objectMemory fetchPointer: InstructionPointerIndex ofObject: context.
(self isMarriedOrWidowedContext: context)
ifTrue:
[(self checkIsStillMarriedContext: context currentFP: framePointer)
ifTrue: [theFP := self frameOfMarriedContext: context.
(self isMachineCodeFrame: theFP)
ifTrue: [flags := 7]
ifFalse: [flags := 3]]
ifFalse: [flags := 1]]
ifFalse:
[flags := 0].
((objectMemory isIntegerObject: pc)
and: [(objectMemory integerValueOf: pc) < 0]) ifTrue:
[flags := flags bitOr: 8].
theMethod := objectMemory fetchPointer: MethodIndex ofObject: context.
(self maybeMethodHasCogMethod: theMethod) ifTrue:
[flags := flags bitOr: 16].
self pop: 1 thenPush: (objectMemory integerObjectOf: flags)
]
{ #category : #'system control primitives' }
CoInterpreterPrimitives >> primitiveFlushCacheByMethod [
"The receiver is a compiledMethod. Clear all entries in the method lookup cache that
refer to this method, presumably because it has been redefined, overridden or removed.
Override to flush appropriate machine code caches also."
super primitiveFlushCacheByMethod.
cogit unlinkSendsTo: self stackTop andFreeIf: false
]
{ #category : #'system control primitives' }
CoInterpreterPrimitives >> primitiveFlushCacheBySelector [
"The receiver is a message selector. Clear all entries in the method lookup cache
with this selector, presumably because an associated method has been redefined.
Override to also flush machine code caches."
| selector probe |
selector := self stackTop.
probe := 0.
1 to: MethodCacheEntries do:
[:i | (methodCache at: probe + MethodCacheSelector) = selector ifTrue:
[methodCache at: probe + MethodCacheSelector put: 0].
probe := probe + MethodCacheEntrySize].
(selector = (self specialSelector: 16) "at:"
or: [selector = (self specialSelector: 17) "at:put:"]) ifTrue:
[self flushAtCache].
cogit
unlinkSendsOf: selector
isMNUSelector: (selector = (objectMemory splObj: SelectorDoesNotUnderstand))
]
{ #category : #'trampoline support' }
CoInterpreterPrimitives >> primitiveFunctionPointerAddress [
<api>
<returnTypeC: #usqInt>
^self cCode: [(self addressOf: primitiveFunctionPointer) asUnsignedInteger]
inSmalltalk: [cogit simulatedReadWriteVariableAddress: #primitiveFunctionPointer in: self]
]
{ #category : #'process primitives' }
CoInterpreterPrimitives >> primitiveLongRunningPrimitiveSemaphore [
"Primitive. Install the semaphore to be used for collecting long-running primitives,
or nil if no semaphore should be used."
| sema flushState activeContext |
<export: true>
self methodArgumentCount ~= 1 ifTrue:
[^self primitiveFailFor: PrimErrBadNumArgs].
sema := self stackValue: 0.
sema = objectMemory nilObject
ifTrue:
[flushState := longRunningPrimitiveCheckSemaphore notNil.
longRunningPrimitiveCheckSemaphore := nil]
ifFalse:
[flushState := longRunningPrimitiveCheckSemaphore isNil.
(objectMemory isSemaphoreOop: sema) ifFalse:
[^self primitiveFailFor: PrimErrBadArgument].
longRunningPrimitiveCheckSemaphore := sema].
"If we've switched checking on or off we must void machine code
(and machine code pcs in contexts) since we will start or stop setting
newMethod in machine code primitive invocations, and so generate
slightly different code from here on in."
flushState ifTrue:
[self push: instructionPointer.
activeContext := self voidVMStateForSnapshotFlushingExternalPrimitivesIf: false.
self marryContextInNewStackPageAndInitializeInterpreterRegisters: activeContext.
self assert: (((self stackValue: 0) = objectMemory nilObject and: [longRunningPrimitiveCheckSemaphore isNil])
or: [(self stackValue: 0) = longRunningPrimitiveCheckSemaphore
and: [objectMemory isSemaphoreOop: sema]])].
self voidLongRunningPrimitive: 'install'.
self pop: 1.
flushState ifTrue:
[self siglong: reenterInterpreter jmp: ReturnToInterpreter]
]
{ #category : #'method introspection primitives' }
CoInterpreterPrimitives >> primitiveMethodPCData [
<export: true>
| methodReceiver data |
argumentCount ~= 0 ifTrue:
[^self primitiveFailFor: PrimErrBadNumArgs].
methodReceiver := self stackTop.
data := 0.
(self methodHasCogMethod: methodReceiver) ifTrue:
[data := self pcDataFor: (self cogMethodOf: methodReceiver).
data = -1 ifTrue:
[^self primitiveFailFor: PrimErrNoMemory]].
data = 0 ifTrue:
[data := objectMemory instantiateClass: (objectMemory splObj: ClassArray) indexableSize: 0].
self pop: 1 thenPush: data
]
{ #category : #'method introspection primitives' }
CoInterpreterPrimitives >> primitiveMethodProfilingData [
<export: true>
| methodReceiver data |
argumentCount ~= 0 ifTrue:
[^self primitiveFailFor: PrimErrBadNumArgs].
methodReceiver := self stackTop.
data := 0.
(self methodHasCogMethod: methodReceiver) ifTrue:
[data := self profilingDataFor: (self cogMethodOf: methodReceiver).
data = -1 ifTrue:
[^self primitiveFailFor: PrimErrNoMemory]].
data = 0 ifTrue:
[data := objectMemory instantiateClass: (objectMemory splObj: ClassArray) indexableSize: 0].
self pop: 1 thenPush: data
]
{ #category : #'indexing primitives' }
CoInterpreterPrimitives >> primitiveMethodXray [
"Lift the veil from a method and answer an integer describing the interior state
of its machine code.
Used for e.g. VM tests so they can verify they're testing what they think they're testing.
0 implies a vanilla method.
Bit 0 = method might be compiled to machine code
Bit 1 = method is currently compiled to machine code
Bit 2 = is compiled frameless.
Bit 3 = method refers to young object.
Bit 4 = method too big to be jitted (more than 64k of code, or needs more than 1.5Mb of stack space to compile)
Bit 5 = method contains unknown/unjittable bytecode
Bit 6 = method should not be jitted because it contains a primitive not to be called from machine code (unused)"
<var: #cogMethod type: #'CogMethod *'>
| alreadyCogged flags cogMethod |
(self methodWithHeaderShouldBeCogged:
(objectMemory methodHeaderOf: self stackTop))
ifTrue: [
alreadyCogged := self maybeMethodHasCogMethod: self stackTop.
flags := 1.
alreadyCogged
ifTrue: [ flags := flags + 2 ]
ifFalse: [
cogMethod := cogit
cog: self stackTop
selector: objectMemory nilObject.
(cogMethod = nil and: [ cogCompiledCodeCompactionCalledFor ])
ifTrue: [
self writeBackHeadFramePointers.
self commenceCogCompiledCodeCompaction.
cogMethod := cogit
cog: self stackTop
selector: objectMemory nilObject ].
cogMethod asInteger
caseOf: {
([ MethodTooBig ] -> [ flags := flags + 16 ]).
([ EncounteredUnknownBytecode ] -> [ flags := flags + 32 ]).
([ ShouldNotJIT ] -> [ flags := flags + 64 ]) }
otherwise: [
self deny: (cogMethod asInteger
between: MaxNegativeErrorCode
and: NotFullyInitialized) ] ].
(self maybeMethodHasCogMethod: self stackTop)
ifTrue: [
cogMethod := self cogMethodOf: self stackTop.
cogMethod stackCheckOffset = 0 ifTrue: [ flags := flags + 4 ].
cogMethod cmRefersToYoung ifTrue: [ flags := flags + 8 ].
alreadyCogged ifFalse: [ cogit safeFreeMethod: cogMethod ] ] ]
ifFalse: [ flags := 0 ].
self pop: 1 thenPush: (objectMemory integerObjectOf: flags)
]
{ #category : #'other primitives' }
CoInterpreterPrimitives >> primitiveMinimumUnusedHeadroom [
<export: true>
self methodReturnValue: (self integerObjectOf: self minimumUnusedHeadroom)
]
{ #category : #'object access primitives' }
CoInterpreterPrimitives >> primitiveObjectAt [
"Defined for CompiledMethods only"
| thisReceiver rawHeader realHeader index |
index := self stackIntegerValue: 0.
self successful ifFalse:
[^self primitiveFailFor: PrimErrBadArgument].
thisReceiver := self stackValue: 1.
rawHeader := self rawHeaderOf: thisReceiver.
realHeader := (self isCogMethodReference: rawHeader)
ifTrue: [(self cCoerceSimple: rawHeader to: #'CogMethod *') methodHeader]
ifFalse: [rawHeader].
(index > 0
and: [index <= ((objectMemory literalCountOfMethodHeader: realHeader) + LiteralStart)]) ifFalse:
[^self primitiveFailFor: PrimErrBadIndex].
self pop: 2
thenPush: (index = 1
ifTrue: [realHeader]
ifFalse: [objectMemory fetchPointer: index - 1 ofObject: thisReceiver])
]
{ #category : #'object access primitives' }
CoInterpreterPrimitives >> primitiveObjectAtPut [
"Store a literal into a CompiledMethod at the given index. Defined for CompiledMethods only."
| thisReceiver rawHeader realHeader index newValue |
newValue := self stackValue: 0.
index := self stackValue: 1.
(objectMemory isNonIntegerObject: index) ifTrue:
[^self primitiveFailFor: PrimErrBadArgument].
index := objectMemory integerValueOf: index.
thisReceiver := self stackValue: 2.
(objectMemory isObjImmutable: thisReceiver) ifTrue:
[^self primitiveFailFor: PrimErrNoModification].
rawHeader := self rawHeaderOf: thisReceiver.
realHeader := (self isCogMethodReference: rawHeader)
ifTrue: [(self cCoerceSimple: rawHeader to: #'CogMethod *') methodHeader]
ifFalse: [rawHeader].
(index > 0
and: [index <= ((objectMemory literalCountOfMethodHeader: realHeader) + LiteralStart)]) ifFalse:
[^self primitiveFailFor: PrimErrBadIndex].
index = 1
ifTrue:
[((objectMemory isNonIntegerObject: newValue)
or: [(objectMemory literalCountOfMethodHeader: newValue) ~= (objectMemory literalCountOfMethodHeader: realHeader)]) ifTrue:
[^self primitiveFailFor: PrimErrBadArgument].
(self isCogMethodReference: rawHeader)
ifTrue: [(self cCoerceSimple: rawHeader to: #'CogMethod *') methodHeader: newValue]
ifFalse: [objectMemory storePointerUnchecked: 0 ofObject: thisReceiver withValue: newValue]]
ifFalse:
[objectMemory storePointer: index - 1 ofObject: thisReceiver withValue: newValue].
self pop: 3 thenPush: newValue
]
{ #category : #'process primitives' }
CoInterpreterPrimitives >> primitiveProfileSemaphore [
"Primitive. Install the semaphore to be used for profiling,
or nil if no semaphore should be used.
See also primitiveProfileStart."
| sema flushState activeContext |
<export: true>
self methodArgumentCount ~= 1 ifTrue:
[^self primitiveFailFor: PrimErrBadNumArgs].
sema := self stackValue: 0.
sema = objectMemory nilObject
ifTrue:
[flushState := profileSemaphore ~= objectMemory nilObject]
ifFalse:
[flushState := profileSemaphore = objectMemory nilObject.
(objectMemory isSemaphoreOop: sema) ifFalse:
[^self primitiveFailFor: PrimErrBadArgument]].
profileSemaphore := sema.
"If we've switched profiling on or off we must void machine code
(and machine code pcs in contexts) since we will start or stop
testing the profile clock in machine code primitive invocations,
and so generate slightly different code from here on in."
flushState ifTrue:
[self push: instructionPointer.
activeContext := self voidVMStateForSnapshotFlushingExternalPrimitivesIf: false.
self marryContextInNewStackPageAndInitializeInterpreterRegisters: activeContext.
self assert: (((self stackValue: 0) = objectMemory nilObject and: [profileSemaphore = objectMemory nilObject])
or: [(self stackValue: 0) = profileSemaphore
and: [objectMemory isSemaphoreOop: sema]])].
profileProcess := profileMethod := objectMemory nilObject.
self pop: 1.
flushState ifTrue:
[self siglong: reenterInterpreter jmp: ReturnToInterpreter]
]
{ #category : #'method introspection primitives' }
CoInterpreterPrimitives >> primitiveResetCountersInMethod [
<export: true>
<option: #SistaCogit>
| methodReceiver |
argumentCount ~= 0 ifTrue:
[^self primitiveFailFor: PrimErrBadNumArgs].
methodReceiver := self stackTop.
(self methodHasCogMethod: methodReceiver) ifTrue:
[cogit resetCountersIn: (self cogMethodOf: methodReceiver)]
]
{ #category : #'process primitives' }
CoInterpreterPrimitives >> primitiveSignal [
"Synchronously signal the semaphore.
This may change the active process as a result."
| inInterpreter |
"We may be about to switch process, either to an interpreted frame or a
machine code frame. To know whether to return or enter machine code
we have to know from whence we came. We could have come from the
interpreter, either directly or via a machine code primitive. We could have
come from machine code. The instructionPointer tells us where from:"
inInterpreter := self isInstructionPointerInInterpreter: instructionPointer.
(self synchronousSignal: self stackTop) ifTrue:
[self forProcessPrimitiveReturnToExecutivePostContextSwitch: inInterpreter]
]
{ #category : #'system control primitives' }
CoInterpreterPrimitives >> primitiveSnapshot [
"Save a normal snapshot under the same name as it was loaded
unless it has been renamed by the last primitiveImageName.
Override to jump to the interpreter because the machine code zone is now void."
<inline: false>
self snapshot.
(self iframeMethod: framePointer) = newMethod ifTrue:
["snapshot: has reached the end and built a frame.
In the JIT we need to back-up the pc before reentering the interpreter."
instructionPointer := instructionPointer - 1].
self siglong: reenterInterpreter jmp: ReturnToInterpreter
"NOTREACHED"
]
{ #category : #'control primitives' }
CoInterpreterPrimitives >> primitiveTerminateTo [
"Primitive. Terminate up the context stack from the receiver up to but not including
the argument, if previousContext is on my Context stack. Make previousContext my
sender. This prim has to shadow the code in ContextPart>terminateTo: to be correct.
Override to ensure the caller's saved ip is correct, i.e. if an interpreter frame it may
have to move to iframeSavedIP."
| thisCtx currentCtx aContextOrNil contextsFP contextsSP contextsIP nextCntx stackedReceiverOffset
theFP newFP newSP pageToStopOn thePage frameAbove |
<var: #contextsFP type: #'char *'>
<var: #theFP type: #'char *'>
<var: #newFP type: #'char *'>
<var: #newSP type: #'char *'>
<var: #contextsIP type: #usqInt>
<var: #frameAbove type: #'char *'>
<var: #contextsSP type: #'char *'>
<var: #source type: #'char *'>
<var: #pageToStopOn type: #'StackPage *'>
<var: #thePage type: #'StackPage *'>
aContextOrNil := self stackTop.
(aContextOrNil = objectMemory nilObject or: [objectMemory isContext: aContextOrNil]) ifFalse:
[^self primitiveFail].
thisCtx := self stackValue: 1.
thisCtx = aContextOrNil ifTrue:
[^self primitiveFail].
"All stackPages need to have current head pointers to avoid confusion."
self writeBackHeadFramePointers.
"If we're searching for aContextOrNil it might be on a stack page. Helps to know
if we can free a whole page or not, or if we can short-cut the termination."
(aContextOrNil ~= objectMemory nilObject and: [self isStillMarriedContext: aContextOrNil])
ifTrue: [contextsFP := self frameOfMarriedContext: aContextOrNil.
pageToStopOn := stackPages stackPageFor: contextsFP]
ifFalse: [pageToStopOn := 0].
"if thisCtx is married ensure it is a base frame. Then we can assign its sender."
(self isStillMarriedContext: thisCtx)
ifTrue:
[theFP := self frameOfMarriedContext: thisCtx.
"Optimize terminating thisContext. Move its frame down to be next to
aContextOrNil's frame. Common in the exception system and so helps to be fast."
(theFP = framePointer
and: [pageToStopOn = stackPage]) ifTrue:
[self assertValidStackedInstructionPointersIn: stackPage line: #'__LINE__'.
(self frameCallerFP: theFP) ~= contextsFP ifTrue:
[stackedReceiverOffset := self frameStackedReceiverOffset: theFP.
frameAbove := self findFrameAbove: contextsFP inPage: pageToStopOn.
contextsIP := (self frameCallerSavedIP: frameAbove) asUnsignedInteger.
self assert: ((contextsIP asUnsignedInteger >= objectMemory getMemoryMap startOfObjectMemory)
or: [contextsIP = cogit ceReturnToInterpreterPC]) == (self isMachineCodeFrame: contextsFP) not.
newSP := self frameCallerSP: frameAbove.
newFP := newSP - stackedReceiverOffset - objectMemory wordSize.
theFP + stackedReceiverOffset
to: stackPointer
by: objectMemory wordSize negated
do: [:source|
newSP := newSP - objectMemory wordSize.
stackPages unsignedLongAt: newSP put: (stackPages unsignedLongAt: source)].
stackPages unsignedLongAt: newFP + FoxSavedFP put: contextsFP.
"Ensure contract between machine-code callee and interpreter caller frames is preserved.
Return pc needs to be ceReturnToInterpreterPC."
((self isMachineCodeFrame: newFP)
and: [contextsIP >= objectMemory getMemoryMap startOfObjectMemory]) ifTrue:
[self iframeSavedIP: contextsFP put: contextsIP.
contextsIP := cogit ceReturnToInterpreterPC].
stackPages unsignedLongAt: newFP + FoxCallerSavedIP put: contextsIP.
self assert: (objectMemory isContext: thisCtx).
objectMemory storePointerUnchecked: SenderIndex
ofObject: thisCtx
withValue: (self withSmallIntegerTags: newFP).
objectMemory storePointerUnchecked: InstructionPointerIndex
ofObject: thisCtx
withValue: (self withSmallIntegerTags: contextsFP).
framePointer := newFP.
stackPointer := newSP].
self pop: 1.
self assertValidStackedInstructionPointersIn: stackPage line: #'__LINE__'.
self assert: stackPage = stackPages mostRecentlyUsedPage.
^nil].
self assertValidStackedInstructionPointers: #'__LINE__'.
theFP := self ensureIsBaseFrame: theFP. "May cause a GC!!"
currentCtx := self frameCallerContext: theFP.
"May also reclaim aContextOrNil's page, hence..."
(aContextOrNil ~= objectMemory nilObject and: [self isStillMarriedContext: aContextOrNil])
ifTrue: [contextsFP := self frameOfMarriedContext: aContextOrNil.
pageToStopOn := stackPages stackPageFor: contextsFP]
ifFalse: [pageToStopOn := 0]]
ifFalse:
[currentCtx := objectMemory fetchPointer: SenderIndex ofObject: thisCtx].
self assertValidStackedInstructionPointers: #'__LINE__'.
(self context: thisCtx hasSender: aContextOrNil) ifTrue:
["Need to walk the stack freeing stack pages and nilling contexts."
[currentCtx = aContextOrNil
or: [currentCtx = objectMemory nilObject]] whileFalse:
[self assert: (objectMemory isContext: currentCtx).
(self isMarriedOrWidowedContext: currentCtx)
ifTrue:
[theFP := self frameOfMarriedContext: currentCtx.
thePage := stackPages stackPageFor: theFP.
"If externalEnsureIsBaseFrame: above has moved thisContext to its own stack
then we will always terminate to a frame on a different page. But if we are
terminating some other context to a context somewhere on the current page
we must save the active frames above that context. Things will look e.g. like this:
thisCtx 499383332 s MethodContext(ContextPart)>resume:
499380484 s BlockClosure>ensure:
499377320 s MethodContext(ContextPart)>handleSignal:
499373760 s MethodContext(ContextPart)>handleSignal:
499372772 s MessageNotUnderstood(Exception)>signal
499369068 s CodeSimulationTests(Object)>doesNotUnderstand: absentMethod
499368708 s [] in CodeSimulationTests>testDNU
(sender is 0xbffc2480 I CodeSimulationTests>runSimulated:)
------------
framePointer 0xbffc234c M MethodContext(ContextPart)>doPrimitive:method:receiver:args:
0xbffc2378 M MethodContext(ContextPart)>tryPrimitiveFor:receiver:args:
0xbffc23ac M MethodContext(ContextPart)>send:to:with:super:
0xbffc23e4 M MethodContext(ContextPart)>send:super:numArgs:
0xbffc2418 M MethodContext(InstructionStream)>interpretNextInstructionFor:
0xbffc2434 M MethodContext(ContextPart)>step
0xbffc2458 I MethodContext(ContextPart)>runSimulated:contextAtEachStep:
------------
(499368708's sender) 0xbffc2480 I CodeSimulationTests>runSimulated:
0xbffc249c M CodeSimulationTests>testDNU
0xbffc24bc I CodeSimulationTests(TestCase)>performTest
0xbffc24dc I [] in CodeSimulationTests(TestCase)>runCase
aContextOrNil 0xbffc24fc M BlockClosure>ensure:
0xbffc2520 I CodeSimulationTests(TestCase)>runCase
0xbffc253c M [] in TestResult>runCase:
When we find this case we move the frames above to a new page by making the
frame above currentCtx a base frame, i.e. making 0xbffc2458 in the above example
a base frame. But in this iteration of the loop we don't move down a frame i.e. currentCtx
doesn't change on this iteration."
thePage = stackPage
ifTrue:
[frameAbove := self findFrameAbove: theFP inPage: thePage.
self assert: frameAbove ~= 0.
frameAbove := self ensureIsBaseFrame: frameAbove. "May cause a GC!! May also reclaim aContextOrNil's page, hence..."
(aContextOrNil ~= objectMemory nilObject and: [self isStillMarriedContext: aContextOrNil])
ifTrue: [contextsFP := self frameOfMarriedContext: aContextOrNil.
pageToStopOn := stackPages stackPageFor: contextsFP]
ifFalse: [pageToStopOn := 0]]
ifFalse:
[thePage = pageToStopOn
ifTrue:
["We're here. Cut back the stack to aContextOrNil's frame,
push its instructionPointer if it's not already a head frame,
and we're done."
frameAbove := self findFrameAbove: contextsFP inPage: thePage.
frameAbove ~= 0 ifTrue:
[contextsSP := (self frameCallerSP: frameAbove) - objectMemory wordSize.
stackPages unsignedLongAt: contextsSP put: (self frameCallerSavedIP: frameAbove).
self setHeadFP: contextsFP andSP: contextsSP inPage: thePage].
currentCtx := aContextOrNil]
ifFalse:
["We can free the entire page without further ado."
currentCtx := self frameCallerContext: thePage baseFP.
"for a short time invariant is violated; assert follows"
stackPages freeStackPageNoAssert: thePage]]]
ifFalse:
[nextCntx := objectMemory fetchPointer: SenderIndex ofObject: currentCtx.
self markContextAsDead: currentCtx.
currentCtx := nextCntx]]].
self assert: stackPages pageListIsWellFormed.
(self isMarriedOrWidowedContext: thisCtx)
ifTrue:
[self assert: (self checkIsStillMarriedContext: thisCtx currentFP: framePointer).
self assert: (self isBaseFrame: (self frameOfMarriedContext: thisCtx)).
theFP := self frameOfMarriedContext: thisCtx.
self frameCallerContext: theFP put: aContextOrNil]
ifFalse:
[objectMemory storePointer: SenderIndex ofObject: thisCtx withValue: aContextOrNil].
self pop: 1.
self assertValidStackedInstructionPointers: #'__LINE__'.
self assert: stackPage = stackPages mostRecentlyUsedPage
]
{ #category : #'system control primitives' }
CoInterpreterPrimitives >> primitiveVoidVMState [
"Void all internal VM state in the stack and machine code zones
Override to jump to the interpreter because the machine code zone is now void."
| activeContext |
self push: instructionPointer.
activeContext := self voidVMStateForSnapshotFlushingExternalPrimitivesIf: false.
self marryContextInNewStackPageAndInitializeInterpreterRegisters: activeContext.
self siglong: reenterInterpreter jmp: ReturnToInterpreter
"NOTREACHED"
]
{ #category : #'system control primitives' }
CoInterpreterPrimitives >> primitiveVoidVMStateForMethod [
"The receiver (or first argument) must be a compiledMethod. The optional (or second) argument must be a
boolean. Clear all VM state associated with the method, including any machine code, or machine code pcs
in context objects. If the optional boolean argument is false do not scan the heap looking for contexts."
| activeContext methodObj scanHeapForContexts hasCogMethod theFrame thePage |
<var: #theFrame type: #'char *'>
<var: #thePage type: #'StackPage *'>
scanHeapForContexts := true. "See comment ''One might think...'' below"
"In Smalltalk allow both aMethod voidCogVMState and aMethod voidCogVMStateScanningContextsIf: aBoolean"
argumentCount = 0
ifTrue:
[methodObj := self stackTop]
ifFalse:
[methodObj := self stackValue: 1.
scanHeapForContexts := self booleanValueOf: self stackTop.
self failed ifTrue:
[^self primitiveFailFor: PrimErrBadArgument]].
argumentCount > 1 ifTrue:
[^self primitiveFailFor: PrimErrBadNumArgs].
self flushMethodCacheForMethod: methodObj.
activeContext := self ensureFrameIsMarried: framePointer SP: stackPointer.
self ensurePushedInstructionPointer.
self writeBackHeadFramePointers.
(hasCogMethod := self methodHasCogMethod: methodObj) ifTrue:
[self divorceMachineCodeFramesWithMethod: methodObj].
"One might think (as this author did) that the heap scan is unnecessary if the method does not
have a cog method. But it could be the case that the code zone has recently been reclaimed
and so not having a cog method is no indication that it didn't have a cog method some time in
the recent past, and that there are indeed still contexts with machine code pcs out there. The
only steps that can be avoided are divorcing frames in the stack zone, and scanning to unlink and
free if there isn't a cog method, unless we are told otherwise."
scanHeapForContexts ifTrue:
[self ensureAllContextsWithMethodHaveBytecodePCs: methodObj].
hasCogMethod ifTrue:
[cogit unlinkSendsTo: methodObj andFreeIf: true].
"If flushing led to divorce continue in the interpreter."
(self isStillMarriedContext: activeContext) ifFalse:
[self zeroStackPage. "to avoid assert in marryContextInNewStackPageAndInitializeInterpreterRegisters:"
self marryContextInNewStackPageAndInitializeInterpreterRegisters: activeContext.
self popStack. "pop bogus machine-code instructionPointer"
self assert: (methodObj = self stackTop or: [argumentCount > 0 and: [methodObj = (self stackValue: 1)]]).
self pop: argumentCount.
self siglong: reenterInterpreter jmp: ReturnToInterpreter.
"NOTREACHED"].
"If not, work out where we are and continue"
theFrame := self frameOfMarriedContext: activeContext.
thePage := stackPages stackPageFor: theFrame.
self assert: thePage headFP = theFrame.
self setStackPageAndLimit: thePage.
self setStackPointersFromPage: thePage.
instructionPointer := self popStack.
self assert: (methodObj = self stackTop or: [argumentCount > 0 and: [methodObj = (self stackValue: 1)]]).
self pop: argumentCount
]
{ #category : #'method introspection support' }
CoInterpreterPrimitives >> profilingDataFor: cogMethod [
<var: #cogMethod type: #'CogMethod *'>
| cm nSlots nEntries data |
cm := cogMethod methodObject.
nSlots := (objectMemory byteSizeOf: cm) - (self startPCOfMethod: cm) + objectMemory minSlotsForShortening.
data := objectMemory instantiateClass: (objectMemory splObj: ClassArray) indexableSize: nSlots.
data ifNil: [^-1].
nEntries := cogit profilingDataFor: cogMethod into: data.
nEntries = 0 ifTrue:
[^0].
nEntries < nSlots ifTrue:
[objectMemory shorten: data toIndexableSize: nEntries].
^data
]
{ #category : #'object access primitives' }
CoInterpreterPrimitives >> unmarkAllFrames [
| thePage theFP methodField flags |
<var: #thePage type: #'StackPage *'>
<var: #theFP type: #'char *'>
<inline: false>
0 to: numStackPages - 1 do:
[:i|
thePage := stackPages stackPageAt: i.
(stackPages isFree: thePage) ifFalse:
[theFP := thePage headFP.
[methodField := stackPages unsignedLongAt: theFP + FoxMethod.
methodField asUnsignedInteger < objectMemory getMemoryMap startOfObjectMemory
ifTrue:
[(methodField bitAnd: 4) ~= 0 ifTrue:
[self unsignedLongAt: theFP + FoxMethod put: methodField - 4]]
ifFalse:
[flags := stackPages unsignedLongAt: theFP + FoxIFrameFlags.
(flags bitAnd: 2) ~= 0 ifTrue:
[stackPages unsignedLongAt: theFP + FoxIFrameFlags put: flags - 2]].
(theFP := self frameCallerFP: theFP) ~= 0] whileTrue]]
]