-
Notifications
You must be signed in to change notification settings - Fork 67
/
DruidJIT.class.st
1977 lines (1802 loc) · 68.9 KB
/
DruidJIT.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
Class {
#name : #DruidJIT,
#superclass : #StackToRegisterMappingCogit,
#instVars : [
'ceDereferenceSelectorIndex',
'ceStoreTrampolines',
'ceNewHashTrampoline',
'ceInlineNewHashTrampoline',
'ceStoreCheckTrampoline',
'ceStoreCheckContextReceiverTrampoline',
'ceScheduleScavengeTrampoline',
'ceSmallActiveContextInMethodTrampoline',
'ceSmallActiveContextInFullBlockTrampoline',
'ceLargeActiveContextInMethodTrampoline',
'ceLargeActiveContextInFullBlockTrampoline'
],
#classVars : [
'CheckRememberedInTrampoline',
'NumStoreTrampolines'
],
#category : #'VMMaker-JIT'
}
{ #category : #translation }
DruidJIT class >> declareCVarsIn: aCodeGen [
"Deal wuth the fact that the number of trampolines depends on IMMUTABILITY
and that IMMUTABILITY can be defined at compile time. Yes, this is a mess."
| current values |
current := InitializationOptions at: #IMMUTABILITY ifAbsent: nil.
values := #(true false) collect:
[:bool|
InitializationOptions at: #IMMUTABILITY put: bool.
self initializeNumTrampolines.
(Cogit classPool at: #NumTrampolines) printString].
current
ifNil: [InitializationOptions removeKey: #IMMUTABILITY]
ifNotNil: [InitializationOptions at: #IMMUTABILITY put: current].
values first ~= values last ifTrue:
[aCodeGen addConstantForBinding: #NumTrampolines -> ('(IMMUTABILITY ? ' , values first , ' : ' , values last , ')')].
aCodeGen
var: #ceStoreTrampolines
declareC: ('#if IMMUTABILITY\sqInt ceStoreTrampolines[', NumStoreTrampolines printString, '];\#endif') withCRs
]
{ #category : #initialization }
DruidJIT class >> initialize [
NumStoreTrampolines := 5
]
{ #category : #'class initialization' }
DruidJIT class >> initializeBytecodeTableForSistaV1 [
<generated>
numPushNilsFunction := #sistaV1:Num:Push:Nils:.
pushNilSizeFunction := #sistaV1PushNilSize:numInitialNils:.
BytecodeSetHasDirectedSuperSend := true.
BytecodeSetHasExtensions := true.
FirstSpecialSelector := 96.
NumSpecialSelectors := 32.
self generatorTableFrom: {
#(1 0 223 unknownBytecode).
#(2 224 247 unknownBytecode).
#(3 248 255 unknownBytecode) }
]
{ #category : #'class initialization' }
DruidJIT class >> initializePrimitiveTable [
<generated>
MaxCompiledPrimitiveIndex := 17.
primitiveTable := CArrayAccessor on:
(Array new: MaxCompiledPrimitiveIndex + 1).
self table: primitiveTable from: self primitiveTableArray.
^ primitiveTable
]
{ #category : #translation }
DruidJIT class >> objectRepresentationAncilliaryClasses [
^ Array empty
]
{ #category : #'accessing class hierarchy' }
DruidJIT class >> objectRepresentationClass [
]
{ #category : #'class initialization' }
DruidJIT class >> primitiveTableArray [
<generated>
^ {
{ 1. #gen_PrimitiveAdd. 1 }.
{ 2. #gen_PrimitiveSubtract. 1 }.
{ 3. #gen_PrimitiveLessThan. 1 }.
{ 4. #gen_PrimitiveGreaterThan. 1 }.
{ 5. #gen_PrimitiveLessOrEqual. 1 }.
{ 6. #gen_PrimitiveGreaterOrEqual. 1 }.
{ 7. #gen_PrimitiveEqual. 1 }.
{ 8. #gen_PrimitiveNotEqual. 1 }.
{ 9. #gen_PrimitiveMultiply. 1 }.
{ 10. #gen_primitiveDivide. 1 }.
{ 73. #genNonImplementedPrimitive. -1. #maycallback }.
{ 117. #genNonImplementedPrimitive. -1. #maycallback }.
{ 120. #genNonImplementedPrimitive. -1. #maycallback }.
{ 148. #genNonImplementedPrimitive. -1. #maycallback }.
{ 160. #genNonImplementedPrimitive. -1. #maycallback }.
{ 173. #genNonImplementedPrimitive. -1. #maycallback }.
{ 216. #genNonImplementedPrimitive. -1. #maycallback } }
]
{ #category : #'trait candidates' }
DruidJIT >> allYoungObjectsAgeInFullGC [
^false
]
{ #category : #'trait candidates' }
DruidJIT >> bitAndByteOffsetOfIsFullBlockBitInto: aBlock [
"This supplies the bitmask for the isFullBlock bit, and the offset of the byte containing
that bit in a CogMethod header to aBlock. We don't have named variables holding this
offset. The following assert tests whether the values are correct by creating a surrogate
on an empty ByteArray, setting the bit, and checking that the expected values are set
in the ByteArray."
self cCode: [] inSmalltalk:
[| m |
m := ByteArray new: 16.
CogBlockMethodSurrogate64 new
at: 0 memory: m headerSize: 8 cogit: nil;
cpicHasMNUCaseOrCMIsFullBlock: true.
self assert: m = #[0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 ].
self assert: (m at: objectMemory baseHeaderSize + 1) = 16].
aBlock value: 16 value: objectMemory baseHeaderSize + 1 "zero-relative"
]
{ #category : #'trait candidates' }
DruidJIT >> cacheTagIsMarked: cacheTag [
"Answer if the cacheTag is not unmarked, i.e. answer true for compact class
indices and immediates; only answer false for unmarked objects. In Spur
linked send cache tags are class indices so effectively they're always marked."
^true
]
{ #category : #'trait candidates' }
DruidJIT >> checkValidObjectReference: anOop [
^(objectMemory isImmediate: anOop) not
and: [(objectMemory heapMapAtWord: (self pointerForOop: anOop)) ~= 0]
]
{ #category : #'trait candidates' }
DruidJIT >> checkValidOopReference: anOop [
^(objectMemory isImmediate: anOop)
or: [(objectMemory heapMapAtWord: (self pointerForOop: anOop)) ~= 0]
]
{ #category : #'trait candidates' }
DruidJIT >> classForInlineCacheTag: classIndex [
^objectMemory classOrNilAtIndex: classIndex
]
{ #category : #'trait candidates' }
DruidJIT >> couldBeObject: literal [
^(objectMemory isNonImmediate: literal)
and: [self oop: literal isGreaterThanOrEqualTo: objectMemory startOfMemory]
]
{ #category : #'trait candidates' }
DruidJIT >> genActiveContextTrampolineLarge: isLarge inBlock: isInBlock called: aString [
<var: #aString type: #'char *'>
"Create a trampoline to answer the active context that will
answer it if a frame is already married, and create it otherwise.
Assume numArgs is in SendNumArgsReg and ClassReg is free."
| startAddress |
startAddress := self methodZoneBase.
self zeroOpcodeIndex.
self genGetActiveContextLarge: isLarge inBlock: isInBlock.
self outputInstructionsForGeneratedRuntimeAt: startAddress.
self recordGeneratedRunTime: aString address: startAddress.
self recordRunTimeObjectReferences.
^startAddress
]
{ #category : #'trait candidates' }
DruidJIT >> genCheckRememberedBitOf: objReg scratch: scratchReg [
"Check the remembered bit of the object in objReg; answer the jump taken if the bit is already set.
Only need to fetch the byte containing it, which reduces the size of the mask constant."
| rememberedBitByteOffset mask |
rememberedBitByteOffset := self backEnd isBigEndian
ifTrue: [objectMemory baseHeaderSize - 1 - (objectMemory rememberedBitShift // 8)]
ifFalse:[objectMemory rememberedBitShift // 8].
mask := 1 << (objectMemory rememberedBitShift \\ 8).
self MoveMb: rememberedBitByteOffset r: objReg R: scratchReg.
self TstCq: mask R: scratchReg.
^self JumpNonZero: 0
]
{ #category : #'trait candidates' }
DruidJIT >> genConvertIntegerToSmallIntegerInReg: reg [
self LogicalShiftLeftCq: objectMemory numTagBits R: reg.
self AddCq: 1 R: reg.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genCreateFullClosure: compiledBlock numArgs: numArgs numCopied: numCopied ignoreContext: ignoreContext contextNumArgs: contextNumArgs large: contextIsLarge inBlock: contextIsBlock [
"Create a full closure with the given values."
| numSlots byteSize header skip |
<var: #skip type: #'AbstractInstruction *'>
"First get thisContext into ReceiverResultReg and thence in ClassReg."
ignoreContext
ifTrue: [ self genMoveNilR: ClassReg ]
ifFalse:
[self genGetActiveContextNumArgs: contextNumArgs large: contextIsLarge inBlock: contextIsBlock.
self MoveR: ReceiverResultReg R: ClassReg ].
numSlots := FullClosureFirstCopiedValueIndex + numCopied.
byteSize := objectMemory smallObjectBytesForSlots: numSlots.
self assert: ClassFullBlockClosureCompactIndex ~= 0.
header := objectMemory
headerForSlots: numSlots
format: objectMemory indexablePointersFormat
classIndex: ClassFullBlockClosureCompactIndex.
self MoveAw: objectMemory freeStartAddress R: ReceiverResultReg.
self genStoreHeader: header intoNewInstance: ReceiverResultReg using: TempReg.
self
LoadEffectiveAddressMw: byteSize r: ReceiverResultReg R: TempReg;
MoveR: TempReg Aw: objectMemory freeStartAddress;
CmpCq: objectMemory getScavengeThreshold R: TempReg.
skip := self JumpBelow: 0.
self CallRT: ceScheduleScavengeTrampoline.
skip jmpTarget: self Label.
self
MoveR: ClassReg Mw: FullClosureOuterContextIndex * objectMemory bytesPerOop + objectMemory baseHeaderSize r: ReceiverResultReg;
genMoveConstant: compiledBlock R: TempReg;
MoveR: TempReg Mw: FullClosureCompiledBlockIndex * objectMemory bytesPerOop + objectMemory baseHeaderSize r: ReceiverResultReg;
MoveCq: (objectMemory integerObjectOf: numArgs) R: TempReg;
MoveR: TempReg Mw: FullClosureNumArgsIndex * objectMemory bytesPerOop + objectMemory baseHeaderSize r: ReceiverResultReg.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genEnsureObjInRegNotForwarded: reg scratchReg: scratch [
"Make sure that the object in reg is not forwarded. This routine assumes the object will
never be forwarded to an immediate, as it is used to unforward literal variables (associations).
Use the fact that isForwardedObjectClassIndexPun is a power of two to save an instruction."
| loop ok |
<var: #ok type: #'AbstractInstruction *'>
<var: #loop type: #'AbstractInstruction *'>
self assert: reg ~= scratch.
loop := self Label.
"notionally
self genGetClassIndexOfNonImm: reg into: scratch.
cogit CmpCq: objectMemory isForwardedObjectClassIndexPun R: TempReg.
but the following is an instruction shorter:"
self MoveMw: 0 r: reg R: scratch.
self
AndCq: objectMemory classIndexMask - objectMemory isForwardedObjectClassIndexPun
R: scratch.
ok := self JumpNonZero: 0.
self genLoadSlot: 0 sourceReg: reg destReg: reg.
self Jump: loop.
ok jmpTarget: self Label.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genEnsureOopInRegNotForwarded: reg scratchReg: scratch [
^ self
genEnsureOopInRegNotForwarded: reg
scratchReg: scratch
jumpBackTo: self Label "label is just before the forwarder check"
]
{ #category : #'trait candidates' }
DruidJIT >> genEnsureOopInRegNotForwarded: reg scratchReg: scratch ifForwarder: fwdJumpTarget ifNotForwarder: nonFwdJumpTargetOrZero [
"Make sure that the oop in reg is not forwarded.
Use the fact that isForwardedObjectClassIndexPun is a power of two to save an instruction."
| imm ok finished |
self assert: reg ~= scratch.
imm := self genJumpImmediate: reg.
"notionally
self genGetClassIndexOfNonImm: reg into: scratch.
cogit CmpCq: objectMemory isForwardedObjectClassIndexPun R: TempReg.
but the following is an instruction shorter:"
self MoveMw: 0 r: reg R: scratch.
self
AndCq: objectMemory classIndexMask - objectMemory isForwardedObjectClassIndexPun
R: scratch.
ok := self JumpNonZero: 0.
self genLoadSlot: 0 sourceReg: reg destReg: reg.
self Jump: fwdJumpTarget.
finished := nonFwdJumpTargetOrZero asUnsignedInteger = 0
ifTrue: [self Label]
ifFalse: [nonFwdJumpTargetOrZero].
imm jmpTarget: (ok jmpTarget: finished).
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genEnsureOopInRegNotForwarded: reg scratchReg: scratch jumpBackTo: instruction [
<var: #instruction type: #'AbstractInstruction *'>
<inline: true>
^ self
genEnsureOopInRegNotForwarded: reg
scratchReg: scratch
ifForwarder: instruction
ifNotForwarder: 0
]
{ #category : #'trait candidates' }
DruidJIT >> genEnsureOopInRegNotForwarded: reg scratchReg: scratch updatingSlot: index in: objReg [
"Make sure that the oop in reg is not forwarded, updating the slot in objReg with the value."
| loop imm ok |
<var: #ok type: #'AbstractInstruction *'>
<var: #imm type: #'AbstractInstruction *'>
<var: #loop type: #'AbstractInstruction *'>
"Open-code
self genEnsureOopInRegNotForwarded: reg
scratchReg: scratch
updatingMw: index * objectMemory wordSize + objectMemory baseHeaderSize
r: objReg.
to avoid calling the store check unless the receiver is forwarded."
self assert: (reg ~= scratch and: [objReg ~= scratch]).
loop := self Label.
imm := self genJumpImmediate: reg.
"notionally
self genGetClassIndexOfNonImm: reg into: scratch.
cogit CmpCq: objectMemory isForwardedObjectClassIndexPun R: TempReg.
but the following is an instruction shorter:"
self MoveMw: 0 r: reg R: scratch.
self
AndCq: objectMemory classIndexMask - objectMemory isForwardedObjectClassIndexPun
R: scratch.
ok := self JumpNonZero: 0.
self genLoadSlot: 0 sourceReg: reg destReg: reg.
self MoveR: reg Mw: index * objectMemory wordSize + objectMemory baseHeaderSize r: objReg.
"Check that we're meeting the contract of ceStoreCheckContextReceiverTrampoline."
self assert: (reg = Arg0Reg and: [scratch = TempReg and: [objReg = ReceiverResultReg]]).
self CallRT: ceStoreCheckContextReceiverTrampoline.
self Jump: loop.
ok jmpTarget: (imm jmpTarget: self Label).
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genGetActiveContextLarge: isLarge inBlock: isInBlock [
"Create a trampoline to answer the active context that will
answer it if a frame is already married, and create it otherwise.
Assume numArgs is in SendNumArgsReg and ClassReg is free."
| header slotSize jumpSingle loopHead jumpNeedScavenge continuation exit |
<var: #jumpNeedScavenge type: #'AbstractInstruction *'>
<var: #continuation type: #'AbstractInstruction *'>
<var: #jumpSingle type: #'AbstractInstruction *'>
<var: #loopHead type: #'AbstractInstruction *'>
<var: #exit type: #'AbstractInstruction *'>
self "load the flag; stash it in both TempReg & ClassReg; do the compare (a prime candidated for use of AndCq:R:R:)"
MoveMw: FoxMethod r: FPReg R: ClassReg;
AndCq: MFMethodFlagHasContextFlag R: ClassReg R: TempReg.
jumpSingle := self JumpZero: 0. "jump if flag bit not set"
self "since the flag bit was set, get the context in the receiver reg and return"
MoveMw: FoxThisContext r: FPReg R: ReceiverResultReg;
RetN: 0.
jumpSingle jmpTarget: self Label.
"OK, it doesn't exist; instantiate and initialize it"
"set the hasContext flag; See CoInterpreter class>>initializeFrameIndices"
self
OrCq: MFMethodFlagHasContextFlag R: ClassReg;
MoveR: ClassReg Mw: FoxMethod r: FPReg.
"now get the home CogMethod into ClassReg and save for post-instantiation."
isInBlock caseOf: {
[InFullBlock] -> [self SubCq: 3 R: ClassReg]. "-3 is -(hasContext+isBlock) flags"
[0] -> [self SubCq: 1 R: ClassReg]. "-1 is hasContext flag" }.
"instantiate the context..."
slotSize := isLarge ifTrue: [LargeContextSlots] ifFalse: [SmallContextSlots].
header := objectMemory
headerForSlots: slotSize
format: objectMemory indexablePointersFormat
classIndex: ClassMethodContextCompactIndex.
self flag: #endianness.
self MoveAw: objectMemory freeStartAddress R: ReceiverResultReg.
self genStoreHeader: header intoNewInstance: ReceiverResultReg using: TempReg.
self
MoveR: ReceiverResultReg R: TempReg;
AddCq: (objectMemory smallObjectBytesForSlots: slotSize) R: TempReg;
MoveR: TempReg Aw: objectMemory freeStartAddress;
CmpCq: objectMemory getScavengeThreshold R: TempReg.
jumpNeedScavenge := self JumpAboveOrEqual: 0.
"Now initialize the fields of the context. See CoInterpreter>>marryFrame:SP:copyTemps:"
"sender gets frame pointer as a SmallInteger"
continuation :=
self MoveR: FPReg R: TempReg.
self genSetSmallIntegerTagsIn: TempReg.
self MoveR: TempReg Mw: objectMemory baseHeaderSize + (SenderIndex * objectMemory bytesPerOop) r: ReceiverResultReg.
"pc gets frame caller as a SmallInteger"
self MoveMw: FoxSavedFP r: FPReg R: TempReg.
self genSetSmallIntegerTagsIn: TempReg.
self MoveR: TempReg Mw: objectMemory baseHeaderSize + (InstructionPointerIndex * objectMemory bytesPerOop) r: ReceiverResultReg.
"Set the method field, freeing up ClassReg again, and frame's context field,"
self
MoveMw: (self offset: CogMethod of: #methodObject) r: ClassReg R: TempReg;
MoveR: TempReg Mw: objectMemory baseHeaderSize + (MethodIndex * objectMemory wordSize) r: ReceiverResultReg;
MoveR: ReceiverResultReg Mw: FoxThisContext r: FPReg.
"Now compute stack pointer; this is stackPointer (- 1 for return pc if a CISC) - framePointer - wordSize (1 each for saved pc, method, context, receiver) + 1 (1-relative) + numArgs"
"TPR note - the code here is actually doing
context stackPointer := ((((fp - sp) / wordSize) - [3|4]) + num args) asSmallInteger"
self
MoveR: FPReg R: TempReg;
SubR: SPReg R: TempReg;
LogicalShiftRightCq: self log2BytesPerWord R: TempReg;
SubCq: (self backEnd hasLinkRegister ifTrue: [3] ifFalse: [4]) R: TempReg;
AddR: SendNumArgsReg R: TempReg.
self genConvertIntegerToSmallIntegerInReg: TempReg.
self MoveR: TempReg Mw: objectMemory baseHeaderSize + (StackPointerIndex * objectMemory bytesPerOop) r: ReceiverResultReg.
"Set closureOrNil to either the stacked receiver or nil"
isInBlock > 0
ifTrue:
[self
MoveR: SendNumArgsReg R: TempReg;
AddCq: 2 R: TempReg; "+2 for saved fp and saved pc"
MoveXwr: TempReg R: FPReg R: TempReg]
ifFalse:
[self genMoveNilR: TempReg].
self MoveR: TempReg Mw: objectMemory baseHeaderSize + (ClosureIndex * objectMemory bytesPerOop) r: ReceiverResultReg.
"Set the receiver"
self
MoveMw: FoxMFReceiver r: FPReg R: TempReg;
MoveR: TempReg Mw: objectMemory baseHeaderSize + (ReceiverIndex * objectMemory bytesPerOop) r: ReceiverResultReg.
"Now copy the arguments. This is tricky because of the shortage of registers,. ClassReg ranges
from 1 to numArgs (SendNumArgsReg), and from ReceiverIndex + 1 to ReceiverIndex + numArgs.
1 to: numArgs do:
[:i|
temp := longAt(FPReg + ((SendNumArgs - i + 2) * wordSize)). +2 for saved pc and savedfp
longAtput(FPReg + FoxMFReceiver + (i * wordSize), temp)]"
"TPR note: this is a prime candidate for passing off to the backend to do at least faintly optimal code"
self MoveCq: 1 R: ClassReg.
loopHead := self CmpR: SendNumArgsReg R: ClassReg.
exit := self JumpGreater: 0.
self
MoveR: SendNumArgsReg R: TempReg;
SubR: ClassReg R: TempReg;
AddCq: 2 R: TempReg; "+2 for saved fp and saved pc"
MoveXwr: TempReg R: FPReg R: TempReg;
AddCq: ReceiverIndex + (objectMemory baseHeaderSize / objectMemory wordSize) R: ClassReg; "Now convert ClassReg from frame index to context index"
MoveR: TempReg Xwr: ClassReg R: ReceiverResultReg;
SubCq: ReceiverIndex + (objectMemory baseHeaderSize / objectMemory wordSize) - 1 R: ClassReg; "convert back adding 1 ;-)"
Jump: loopHead.
exit jmpTarget: self Label.
"Finally nil or copy the non-argument temps.
ClassReg := FPReg + FoxMFReceiver.
SendNumArgsReg := SendNumArgsReg+ReceiverIndex.
[ClassReg := ClassReg - wordSize.
backEnd hasLinkRegister
ifTrue: [ClassReg > SPReg]
ifFalse: [ClassReg >= SPReg]] whileTrue:
[receiver[SendNumArgsReg] := *ClassReg.
SendNumArgsReg := SendNumArgsReg + 1]]"
coInterpreter marryFrameCopiesTemps ifFalse:
[self MoveCq: objectMemory nilObject R: TempReg].
self
MoveR: FPReg R: ClassReg;
AddCq: FoxMFReceiver R: ClassReg;
AddCq: ReceiverIndex + 1 + (objectMemory baseHeaderSize / objectMemory wordSize) R: SendNumArgsReg.
loopHead :=
self SubCq: objectMemory wordSize R: ClassReg.
self CmpR: SPReg R: ClassReg.
"If on a CISC there's a retpc for the trampoline call on top of stack; if on a RISC there isn't."
exit := self backEnd hasLinkRegister
ifTrue: [self JumpBelow: 0]
ifFalse: [self JumpBelowOrEqual: 0].
coInterpreter marryFrameCopiesTemps ifTrue:
[self MoveMw: 0 r: ClassReg R: TempReg].
self
MoveR: TempReg Xwr: SendNumArgsReg R: ReceiverResultReg;
AddCq: 1 R: SendNumArgsReg;
Jump: loopHead.
exit jmpTarget: self Label.
self RetN: 0.
jumpNeedScavenge jmpTarget: self Label.
self backEnd saveAndRestoreLinkRegAround:
[self
CallRT: ceScheduleScavengeTrampoline
registersToBeSavedMask: (self registerMaskFor: ReceiverResultReg and: SendNumArgsReg and: ClassReg)].
self Jump: continuation.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genGetActiveContextNumArgs: numArgs large: isLargeContext inBlock: isInBlock [
"Get the active context into ReceiverResultReg, creating it if necessary."
| routine |
routine := isLargeContext
ifFalse: [isInBlock caseOf: {
[0] -> [ceSmallActiveContextInMethodTrampoline].
[InFullBlock] -> [ceSmallActiveContextInFullBlockTrampoline] }]
ifTrue: [isInBlock caseOf: {
[0] -> [ceLargeActiveContextInMethodTrampoline].
[InFullBlock] -> [ceLargeActiveContextInFullBlockTrampoline] }].
self
MoveCq: numArgs R: SendNumArgsReg;
CallRT: routine.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genGetClassObjectOf: instReg into: destReg scratchReg: scratchReg instRegIsReceiver: instRegIsReceiver [
"Fetch the instance's class into destReg. If the instance is not the receiver and is forwarded, follow forwarding."
| jumpIsImm jumpNotForwarded loop |
<var: #jumpIsImm type: #'AbstractInstruction *'>
<var: #jumpNotForwarded type: #'AbstractInstruction *'>
<var: #loop type: #'AbstractInstruction *'>
(instReg = destReg or: [instReg = scratchReg or: [destReg = scratchReg]]) ifTrue:
[^BadRegisterSet].
loop := self MoveR: instReg R: scratchReg.
self AndCq: objectMemory tagMask R: scratchReg.
jumpIsImm := self JumpNonZero: 0.
self flag: #endianness.
"Get least significant half of header word in destReg"
self MoveMw: 0 r: instReg R: scratchReg.
"mask off class index"
self AndCq: objectMemory classIndexMask R: scratchReg.
instRegIsReceiver ifFalse:
["if it is forwarded..."
self CmpCq: objectMemory isForwardedObjectClassIndexPun R: scratchReg.
jumpNotForwarded := self JumpNonZero: 0.
"...follow the forwarding pointer and loop to fetch its classIndex"
self MoveMw: objectMemory baseHeaderSize r: instReg R: instReg.
self Jump: loop.
jumpNotForwarded jmpTarget: self Label].
jumpIsImm jmpTarget:
(self MoveR: scratchReg R: destReg).
scratchReg = TempReg
ifTrue:
[self PushR: instReg.
self genGetClassObjectOfClassIndex: destReg into: instReg scratchReg: TempReg.
self MoveR: instReg R: destReg.
self PopR: instReg]
ifFalse:
[self genGetClassObjectOfClassIndex: destReg into: scratchReg scratchReg: TempReg.
self MoveR: scratchReg R: destReg].
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genGetClassObjectOfClassIndex: instReg into: destReg scratchReg: scratchReg [
"Fetch the class object whose index is in instReg into destReg.
It is non-obvious, but the Cogit assumes loading a class does not involve
a runtime call, so do not call classAtIndex:"
self assert: instReg ~= destReg.
self assert: instReg ~= scratchReg.
self assert: destReg ~= scratchReg.
self
MoveR: instReg R: scratchReg;
LogicalShiftRightCq: objectMemory classTableMajorIndexShift R: scratchReg;
LogicalShiftLeftCq: objectMemory shiftForWord R: scratchReg.
self assert: (self shouldAnnotateObjectReference: objectMemory classTableRootObj) not.
(self backEnd isWithinMwOffsetRange: objectMemory classTableRootObj + objectMemory baseHeaderSize)
ifTrue: [self MoveMw: objectMemory classTableRootObj + objectMemory baseHeaderSize r: scratchReg R: destReg]
ifFalse: [self
AddCq: objectMemory classTableRootObj R: scratchReg;
MoveMw: objectMemory baseHeaderSize r: scratchReg R: destReg].
self
MoveR: instReg R: scratchReg;
AndCq: objectMemory classTableMinorIndexMask R: scratchReg;
AddCq: objectMemory baseHeaderSize >> objectMemory shiftForWord R: scratchReg;
MoveXwr: scratchReg R: destReg R: destReg.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genGetClassTagOf: instReg into: destReg scratchReg: scratchReg [
^self genGetInlineCacheClassTagFrom: instReg into: destReg forEntry: true
]
{ #category : #'trait candidates' }
DruidJIT >> genGetInlineCacheClassTagFrom: sourceReg into: destReg forEntry: forEntry [
"Extract the inline cache tag for the object in sourceReg into destReg. The inline
cache tag for a given object is the value loaded in inline caches to distinguish
objects of different classes. In Spur this is either the tags for immediates, or
the receiver's classIndex. Answer the label for the start of the sequence."
| jumpImm entryLabel |
<var: #jumpImm type: #'AbstractInstruction *'>
<var: #entryLabel type: #'AbstractInstruction *'>
forEntry ifTrue:
[ self AlignmentNops: objectMemory wordSize].
entryLabel := self Label.
self AndCq: objectMemory tagMask R: sourceReg R: destReg.
jumpImm := self JumpNonZero: 0.
"Get least significant half of header word in destReg"
self flag: #endianness.
self MoveMw: 0 r: sourceReg R: destReg.
self AndCq: objectMemory classIndexMask R: destReg.
"The entry and the noCheckEntry (that will generated after) need to be aligned differently.
We do that by forcing some odd number of extra Nop operations here.
One Nop is not enough because that causes x86-64 entries to have the same allignment.
In ARM64 all instructions are 4 byte long, thus 3 Nops make
- the entry to be aligned to 8 bytes and
- the no check entry aligned to 4 bytes.
In x86-64 instruction size is variable, and 3 Nops take 3 bytes."
forEntry ifTrue: [
self Nop.
self Nop.
self Nop.
].
jumpImm jmpTarget: self Label.
^entryLabel
]
{ #category : #'trait candidates' }
DruidJIT >> genInlinedIdenticalOrNotIfGuts: orNot [
<inline: true>
^ self genForwardersInlinedIdenticalOrNotIf: orNot
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpBaseHeaderImmutable: baseHeaderReg [
"baseHeader holds at least the least significant 32 bits of the object"
<returnTypeC: #'AbstractInstruction *'>
<option: #IMMUTABILITY>
<inline: true>
self TstCq: objectMemory immutableBitMask R: baseHeaderReg.
^ self JumpNonZero: 0
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpBaseHeaderMutable: baseHeaderReg [
"baseHeader holds at least the least significant 32 bits of the object"
<returnTypeC: #'AbstractInstruction *'>
<option: #IMMUTABILITY>
<inline: true>
self TstCq: objectMemory immutableBitMask R: baseHeaderReg.
^ self JumpZero: 0
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpImmediate: aRegister [
<returnTypeC: #'AbstractInstruction *'>
self TstCq: objectMemory tagMask R: aRegister.
^ self JumpNonZero: 0
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpImmutable: sourceReg scratchReg: scratchReg [
<returnTypeC: #'AbstractInstruction *'>
<option: #IMMUTABILITY>
self MoveMw: 0 r: sourceReg R: scratchReg.
^ self genJumpBaseHeaderImmutable: scratchReg
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpInOldSpace: reg [
"Jump if reg is old."
<inline: true>
^ self
CmpCq: objectMemory storeCheckBoundary R: reg; "N.B. FLAGS := destReg - scratchReg"
JumpAboveOrEqual: 0
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpMutable: sourceReg scratchReg: scratchReg [
<returnTypeC: #'AbstractInstruction *'>
<option: #IMMUTABILITY>
self MoveMw: 0 r: sourceReg R: scratchReg.
^ self genJumpBaseHeaderMutable: scratchReg
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpNotSmallInteger: reg [
"Generate a compare and branch to test if aRegister contains other than a SmallInteger."
^ self
TstCq: objectMemory smallIntegerTag R: reg;
JumpZero: 0
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpNotSmallIntegerInScratchReg: aRegister [
<inline: true>
^self genJumpNotSmallInteger: aRegister
]
{ #category : #'trait candidates' }
DruidJIT >> genJumpNotSmallIntegersIn: aRegister and: bRegister scratch: scratchRegister [
"Generate a compare and branch to test if aRegister and bRegister contains other than SmallIntegers,
i.e. don't branch if both aRegister and bRegister contain SmallIntegers.
Answer the jump. Destroy scratchRegister if required."
<returnTypeC: #'AbstractInstruction *'>
<inline: true>
self
MoveR: aRegister R: scratchRegister;
AndR: bRegister R: scratchRegister.
^self genJumpNotSmallIntegerInScratchReg: scratchRegister
]
{ #category : #'trait candidates' }
DruidJIT >> genLoadSlot: index sourceReg: sourceReg destReg: destReg [
self
MoveMw: index * objectMemory wordSize + objectMemory baseHeaderSize
r: sourceReg
R: destReg.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genNewArrayOfSize: size initialized: initialized [
"Generate a call to code that allocates a new Array of size.
The Array should be initialized with nils iff initialized is true.
The size arg is passed in SendNumArgsReg, the result
must come back in ReceiverResultReg."
| header skip |
<var: #skip type: #'AbstractInstruction *'>
self assert: size < objectMemory numSlotsMask.
header := objectMemory
headerForSlots: size
format: objectMemory arrayFormat
classIndex: ClassArrayCompactIndex.
self MoveAw: objectMemory freeStartAddress R: ReceiverResultReg.
self genStoreHeader: header intoNewInstance: ReceiverResultReg using: TempReg.
(initialized and: [size > 0]) ifTrue:
[self genMoveConstant: objectMemory nilObject R: TempReg.
0 to: size - 1 do:
[:i| self MoveR: TempReg
Mw: i * objectMemory wordSize + objectMemory baseHeaderSize
r: ReceiverResultReg]].
self
LoadEffectiveAddressMw: (objectMemory smallObjectBytesForSlots: size) r: ReceiverResultReg R: TempReg;
MoveR: TempReg Aw: objectMemory freeStartAddress;
CmpCq: objectMemory getScavengeThreshold R: TempReg.
skip := self JumpBelow: 0.
self CallRT: ceScheduleScavengeTrampoline.
skip jmpTarget: self Label.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genNewHashTrampoline: saveRegs called: name [
"In non sista VM this is used only from the identityHash primitive, hence only the result of the trampoline, the hash, should be in ReceiverResultReg, other registers can just be ignored.
In the sista VM, the inlined hash operation requires registers to be saved"
<inline: true>
^ self
genTrampolineFor: #ceNewHashOf:
called: name
numArgs: 1
arg: ReceiverResultReg
arg: nil
arg: nil
arg: nil
regsToSave: (saveRegs
ifTrue: [CallerSavedRegisterMask bitClear: (self registerMaskFor: ReceiverResultReg)]
ifFalse: [self emptyRegisterMask])
pushLinkReg: true
resultReg: ReceiverResultReg
appendOpcodes: false
]
{ #category : #'trait candidates' }
DruidJIT >> genRemoveSmallIntegerTagsInScratchReg: scratchReg [
self SubCq: 1 R: scratchReg.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genSetSmallIntegerTagsIn: scratchReg [
self OrCq: 1 R: scratchReg.
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreCheckContextReceiverTrampoline [
"Create a trampoline to store-check the update of the receiver in a closure's
outerContext in compileBlockFrameBuild:."
| startAddress |
startAddress := self methodZoneBase.
self zeroOpcodeIndex.
self genStoreCheckReceiverReg: ReceiverResultReg valueReg: Arg0Reg scratchReg: TempReg inFrame: false.
self RetN: 0.
self outputInstructionsForGeneratedRuntimeAt: startAddress.
self recordGeneratedRunTime: 'ceStoreCheckContextReceiver' address: startAddress.
self recordRunTimeObjectReferences.
^startAddress
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreCheckReceiverReg: destReg valueReg: valueReg scratchReg: scratchReg inFrame: inFrame [
"Generate the code for a store check of valueReg into destReg."
| jmpImmediate jmpDestYoung jmpSourceOld jmpAlreadyRemembered |
<var: #jmpImmediate type: #'AbstractInstruction *'>
<var: #jmpDestYoung type: #'AbstractInstruction *'>
<var: #jmpSourceOld type: #'AbstractInstruction *'>
<var: #jmpAlreadyRemembered type: #'AbstractInstruction *'>
"Is value stored an immediate? If so we're done"
jmpImmediate := self genJumpImmediate: valueReg.
"Get the old/new boundary in scratchReg"
self MoveCw: objectMemory storeCheckBoundary R: scratchReg.
"Is target young? If so we're done"
self CmpR: scratchReg R: destReg. "N.B. FLAGS := destReg - scratchReg"
jmpDestYoung := self JumpBelow: 0.
"Is value stored old? If so we're done."
self CmpR: scratchReg R: valueReg. "N.B. FLAGS := valueReg - scratchReg"
jmpSourceOld := self JumpAboveOrEqual: 0.
"value is young and target is old.
Need to remember this only if the remembered bit is not already set."
CheckRememberedInTrampoline ifFalse:
[jmpAlreadyRemembered := self genCheckRememberedBitOf: destReg scratch: scratchReg].
"Remembered bit is not set. Call store check to insert dest into remembered table."
self assert: destReg = ReceiverResultReg.
self
evaluateTrampolineCallBlock: [self CallRT: ceStoreCheckTrampoline]
protectLinkRegIfNot: inFrame.
jmpImmediate jmpTarget:
(jmpDestYoung jmpTarget:
(jmpSourceOld jmpTarget:
self Label)).
CheckRememberedInTrampoline ifFalse:
[jmpAlreadyRemembered jmpTarget: jmpSourceOld getJmpTarget].
^0
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreCheckTrampoline [
| jumpSC |
<var: #jumpSC type: #'AbstractInstruction *'>
<inline: true>
CheckRememberedInTrampoline ifTrue:
[ self zeroOpcodeIndex.
jumpSC := self genCheckRememberedBitOf: ReceiverResultReg scratch: self backEnd cResultRegister.
self assert: jumpSC opcode = JumpNonZero.
jumpSC opcode: JumpZero.
self RetN: 0.
jumpSC jmpTarget: self Label].
^ self
genTrampolineFor: #remember:
called: 'ceStoreCheckTrampoline'
numArgs: 1
arg: ReceiverResultReg
arg: nil
arg: nil
arg: nil
regsToSave: (CallerSavedRegisterMask bitClear: (self registerMaskFor: ReceiverResultReg))
pushLinkReg: true
resultReg: self returnRegForStoreCheck
appendOpcodes: CheckRememberedInTrampoline
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreHeader: header intoNewInstance: rcvrReg using: scratchReg [
"Generate the instructions to move the constant header into a new instance pointed to by rcvrReg."
<inline: true>
self
MoveCq: header R: scratchReg;
MoveR: scratchReg Mw: 0 r: rcvrReg
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreSourceReg: sourceReg slotIndex: index destReg: destReg scratchReg: scratchReg inFrame: inFrame [
<inline: true>
self
cppIf: IMMUTABILITY
ifTrue:
[ ^ self
genStoreWithImmutabilityCheckSourceReg: sourceReg
slotIndex: index
destReg: destReg
scratchReg: scratchReg
needsStoreCheck: true
needRestoreRcvr: false "RcvrResultReg doesn't need to be live across the instructions" ]
ifFalse:
[ ^ self
genStoreSourceReg: sourceReg
slotIndex: index
destReg: destReg
scratchReg: scratchReg
inFrame: inFrame
needsStoreCheck: true ]
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreSourceReg: sourceReg slotIndex: index destReg: destReg scratchReg: scratchReg inFrame: inFrame needsStoreCheck: needsStoreCheck [
self genTraceStores.
"do the store"
self MoveR: sourceReg
Mw: index * objectMemory wordSize + objectMemory baseHeaderSize
r: destReg.
"now the check. needStoreCheck is false if the JIT has figured out that the value stored does not need the check (immediate, nil, true, false)"
needsStoreCheck ifTrue:
[ ^ self
genStoreCheckReceiverReg: destReg
valueReg: sourceReg
scratchReg: scratchReg
inFrame: inFrame ].
^ 0
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreSourceReg: sourceReg slotIndex: index intoNewObjectInDestReg: destReg [
"This method is used for unchecked stores in objects after their creation (typically, inlined creation of Array, closures and some temp vectors).
Currently there is no need to do the immutability check here"
self MoveR: sourceReg
Mw: index * objectMemory wordSize + objectMemory baseHeaderSize
r: destReg.
^ 0
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreTrampolineCall: instVarIndex [
<inline: true>
self assert: IMMUTABILITY.
instVarIndex >= (NumStoreTrampolines - 1)
ifTrue:
[ self MoveCq: instVarIndex R: TempReg.
self CallRT: (ceStoreTrampolines at: NumStoreTrampolines - 1) ]
ifFalse:
[ self CallRT: (ceStoreTrampolines at: instVarIndex) ].
self annotateBytecode: self Label
]
{ #category : #'trait candidates' }
DruidJIT >> genStoreTrampolineCalled: trampolineName instVarIndex: instVarIndex [
"Convention:
- RcvrResultReg holds the object mutated.
If immutability failure:
- TempReg holds the instance variable index mutated
if instVarIndex > numDedicatedStoreTrampoline
- ClassReg holds the value to store
Registers are not lived across this trampoline as the
immutability failure may need new stack frames."
| jumpSC jumpRC |
<option: #IMMUTABILITY>
<var: #trampolineName type: #'char *'>
<var: #jumpSC type: #'AbstractInstruction *'>
<var: #jumpRC type: #'AbstractInstruction *'>
<inline: false>
self zeroOpcodeIndex.
"SendNumArgsReg is mutated but we don't care as register are not live across the trampoline.
There is no reason why registers cannot be saved over the remember: call, but since the
immutability check is a suspension point, registers cannot remain live."
jumpSC := self genJumpMutable: ReceiverResultReg scratchReg: SendNumArgsReg.
self
compileTrampolineFor: #ceCannotAssignTo:withIndex:valueToAssign:
numArgs: 3
arg: ReceiverResultReg
arg: (instVarIndex < (NumStoreTrampolines - 1)
ifTrue: [self trampolineArgConstant: instVarIndex]
ifFalse: [TempReg])
arg: ClassReg
arg: nil
regsToSave: self emptyRegisterMask
pushLinkReg: true
resultReg: NoReg.
"Store check"
jumpSC jmpTarget: self Label.
"If on 64-bits and doing the remembered bit test here, we can combine the tests to fetch the header once."
CheckRememberedInTrampoline ifTrue:
[objectMemory wordSize = 8
ifTrue:
[self TstCq: 1 << objectMemory rememberedBitShift R: SendNumArgsReg.
jumpRC := self JumpZero: 0.
self RetN: 0]
ifFalse:
[jumpRC := self genCheckRememberedBitOf: ReceiverResultReg scratch: SendNumArgsReg.
self assert: jumpRC opcode = JumpNonZero.
jumpRC opcode: JumpZero.
self RetN: 0].
jumpRC jmpTarget: self Label].
^ self genTrampolineFor: #remember:
called: trampolineName
numArgs: 1
arg: ReceiverResultReg
arg: nil