-
Notifications
You must be signed in to change notification settings - Fork 67
/
VMSimpleStackBasedCogitAbstractTest.class.st
1000 lines (735 loc) · 28.5 KB
/
VMSimpleStackBasedCogitAbstractTest.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 : #VMSimpleStackBasedCogitAbstractTest,
#superclass : #VMSpurMemoryManagerTest,
#instVars : [
'cogit',
'codeSize',
'callerAddress',
'registerValue',
'opcodes',
'stackMemory',
'stackMemoryStartAddress',
'stackMemoryEndAddress',
'isa',
'obj',
'selectorIndex',
'sendAddress',
'sendTrampolineAddress',
'sentSelector',
'selectorAtIndex',
'inlineCacheMissTrampoline',
'machineSimulator',
'send1TrampolineAddress',
'send2TrampolineAddress',
'send3TrampolineAddress',
'cogInitialAddress'
],
#pools : [
'CogAbstractRegisters',
'CogCompilationConstants',
'CogMethodConstants',
'VMBytecodeConstants',
'VMClassIndices'
],
#category : #'VMMakerTests-JitTests'
}
{ #category : #'building suites' }
VMSimpleStackBasedCogitAbstractTest class >> imageFormatParameters [
^ { { (#useComposedImageFormat -> true) } }
]
{ #category : #'building suites' }
VMSimpleStackBasedCogitAbstractTest class >> wordSize32Parameters [
^ ParametrizedTestMatrix new
addCase: { #ISA -> #'IA32'. #wordSize -> 4};
addCase: { #ISA -> #'ARMv5'. #wordSize -> 4};
yourself
]
{ #category : #'building suites' }
VMSimpleStackBasedCogitAbstractTest class >> wordSize64Parameters [
^ ParametrizedTestMatrix new
addCase: { #ISA -> #'aarch64'. #wordSize -> 8};
addCase: { #ISA -> #'X64'. #wordSize -> 8};
yourself
]
{ #category : #'building suites' }
VMSimpleStackBasedCogitAbstractTest class >> wordSizeParameters [
^ self wordSize64Parameters + self wordSize32Parameters
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> ISA: anISA [
isa := anISA
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> abstractInstructions [
^ (0 to: cogit getOpcodeIndex - 1)
collect: [ :i | cogit abstractOpcodes at: i ]
]
{ #category : #testing }
VMSimpleStackBasedCogitAbstractTest >> argumentsOnRegisters [
^ cogit isKindOf: StackToRegisterMappingCogit
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> assertPushed: anOop after: aBlockClosure [
| before after |
before := machineSimulator smalltalkStackPointerRegisterValue.
aBlockClosure value.
after := machineSimulator smalltalkStackPointerRegisterValue.
self assert: after equals: before - self wordSize.
self assert: (self readMemoryAt: after) equals: anOop
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> assertStackRemainsUnchangedDuring: aBlockClosure [
| before |
before := machineSimulator stackPointerRegisterValue.
aBlockClosure value.
self
assert: machineSimulator stackPointerRegisterValue
equals: before
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> callerAddress [
^ callerAddress
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> cogit [
^ cogit
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> compile: aBlockClosure [
"Compiles some native code using the code inside the block closure as builder.
This version assumes the block has a single 1-bytecode statement
=> this is used to estimate the amount of memory to preallocate for the geneated code.
Return the address to the generated piece of code."
^ self compile: aBlockClosure bytecodes: 2
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> compile: aBlockClosure bytecodes: bytecodes [
^ self compile: aBlockClosure bytecodes: bytecodes headerSize: 0
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> compile: aBlockClosure bytecodes: bytecodes headerSize: headerSize [
"Compiles some native code using the code inside the block closure as builder.
The bytecodes variable contains an estimate of the number of bytecodes generated
=> this is used to estimate the amount of memory to preallocate for the geneated code.
Return the address to the generated piece of code."
| allocatedAddress |
"Generate the IR"
self compileWithoutOutput: aBlockClosure bytecodes: bytecodes.
"Then output the machine code"
cogit computeMaximumSizes.
"methodLabel address + headerSize?"
codeSize := cogit generateInstructionsAt: cogit methodZone freeStart + headerSize.
allocatedAddress := cogit methodZone allocate: (cogit methodZone roundUpLength: codeSize + headerSize).
cogit outputInstructionsAt: allocatedAddress + headerSize.
opcodes := cogit getOpcodeIndex.
^ allocatedAddress
]
{ #category : #'tests - special sends' }
VMSimpleStackBasedCogitAbstractTest >> compileSpecialSend [
"We will call to this address"
sendTrampolineAddress := self compile: [ cogit RetN: 0 ].
cogit ordinarySendTrampolineAt: sentSelector numArgs put: sendTrampolineAddress.
self createFramefulCallFrom: callerAddress.
cogit byte0: 96 + (selectorIndex - 1 / 2).
sendAddress := self compile: [ cogit genSpecialSelectorSend ].
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> compileTrampoline: aBlock named: aTrampolineName [
| startAddress |
cogit methodZone freeStart: cogit methodZoneBase.
startAddress := self compile: aBlock.
cogit outputInstructionsForGeneratedRuntimeAt: startAddress.
cogit recordGeneratedRunTime: aTrampolineName address: startAddress.
cogit recordRunTimeObjectReferences.
cogit methodZoneBase: cogit methodZone freeStart.
cogInitialAddress := cogit methodZoneBase.
^ startAddress
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> compileWithoutOutput: aBlockClosure bytecodes: bytecodes [
"Call a compilation block initializing the compiler before.
This method does not generate the machine code, just the IR.
This is useful in two cases:
- compiler intrinsics (routines and trampolines) are written to output the machine code directly
- this is used as the first part of the #compile:bytecodes:header: to initialize the compiler"
cogit allocateOpcodes: (bytecodes + 10) * 10 bytecodes: bytecodes ifFail: [ self fail ].
cogit zeroOpcodeIndex.
cogit maxLitIndex: -1.
cogit methodLabel address: cogit methodZone freeStart.
cogit deadCode: false.
^ aBlockClosure value
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> createBaseFrame [
"Create the root context with a valid method"
"Let's create a method with enough size. It should have at least a literal (4 or 8 bytes depending the word size) and some bytecodes, so we can put the IP inside the method"
| ctx page baseMethod baseMethodIP baseFrame |
baseMethod := methodBuilder newMethod
literals: {
memory nilObject.
memory nilObject };
buildMethod.
baseMethodIP := memory lastPointerOf: baseMethod.
"The context has 5 (in 32 bits) or 9 (in 64 bits) as initial IP, as method has at least one literal"
ctx := self
newSmallContextReceiver: memory nilObject
method: baseMethod
arguments: #( )
temporaries: #( )
ip: baseMethodIP + 1.
"The base context has a trampoline as the savedIP so it can return to a context that is in the heap"
cogit ceBaseFrameReturnTrampoline: 16rAABBCCDD.
page := interpreter makeBaseFrameFor: ctx.
interpreter setStackPointersFromPage: page.
interpreter setStackPageAndLimit: page.
interpreter instructionPointer: interpreter popStack.
"keeping track of the base frame."
baseFrame := interpreter framePointer
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> createFramefulCallFrom: anAddress [
self
createFramefulCallFrom: anAddress
receiver: memory nilObject
arguments: #()
temporaries: #()
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> createFramefulCallFrom: returnAddress receiver: receiver arguments: arguments [
self createFramefulCallFrom: returnAddress receiver: receiver arguments: arguments temporaries: #()
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> createFramefulCallFrom: returnAddress receiver: receiver arguments: arguments temporaries: temps [
"I create a frameful call for machine code.
Stacks grow down:
receiver for method activations/closure for block activations
arg0
...
argN
caller's method ip/base frame's sender context
fp-> saved fp
method
context (uninitialized)
receiver
first temp
...
sp-> Nth temp"
"The first part is the same with or without frame, as it is pushed by the caller"
| builder |
builder := self machineCodeFrameBuilder.
builder returnAddress: returnAddress.
builder receiver: receiver.
builder arguments: arguments.
builder temporaries: temps.
builder buildFrame
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> createFramelessCallFrom: anAddress [
self
createFramelessCallFrom: anAddress
receiver: memory nilObject
arguments: #()
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> createFramelessCallFrom: returnAddress receiver: receiver arguments: arguments [.
"I create a frameless call
Frameless method activation looks like
receiver
args
sp-> ret pc.
"
self pushAddress: receiver.
arguments do: [ :anArgument | self pushAddress: anArgument ].
self prepareCall.
machineSimulator receiverRegisterValue: receiver.
"We initialize the internal Cogit state by hand because we don't have a method to cog"
" cogit methodOrBlockNumTemps: 0.
cogit initSimStackForFramelessMethod: 1".
cogit methodOrBlockNumArgs: arguments size.
cogit needsFrame: false.
cogit inBlock: 0.
" cogit regArgsHaveBeenPushed: false".
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> createInterpreterFramefulCallFrom: returnAddress receiver: receiver arguments: arguments temporaries: temps [
"I create a frameful call for interpreter.
Pay attention I am different than a machine code framefull context!!
Stacks grow down:
receiver for method activations/closure for block activations
arg0
...
argN
caller's method ip/base frame's sender context
fp-> saved fp
method
context (initialized to nil)
frame flags (interpreter only)
saved method ip (initialized to 0; interpreter only)
first temp
...
sp-> Nth temp"
"The first part is the same with or without frame, as it is pushed by the caller"
self createFramelessCallFrom: returnAddress receiver: receiver arguments: arguments.
"These are pushed when activating the method"
self pushAddress: machineSimulator framePointerRegisterValue.
machineSimulator framePointerRegisterValue: machineSimulator stackPointerRegisterValue.
self pushAddress: (memory nilObject bitOr: 1). "method"
self pushAddress: memory nilObject. "context"
self pushAddress: 0. "flags"
self pushAddress: 0. "method ip"
self pushAddress: receiver.
temps do: [ :aTemporary | self pushAddress: aTemporary ].
cogit needsFrame: true.
]
{ #category : #'tests - special sends' }
VMSimpleStackBasedCogitAbstractTest >> createSpecialSelectorArray [
| specialObjectsOop |
specialObjectsOop := self newArrayWithSlots: 24.
selectorIndex := Smalltalk specialSelectors indexOf: sentSelector.
obj := self newArrayWithSlots: selectorIndex + 1.
memory
storePointer: selectorIndex - 1
ofObject: obj withValue: (selectorAtIndex := memory integerObjectOf: 77 "Some object to test").
memory
storePointer: selectorIndex
ofObject: obj withValue: (memory integerObjectOf: sentSelector numArgs).
memory storePointer: 23 ofObject: specialObjectsOop withValue: obj.
memory specialObjectsOop: specialObjectsOop.
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> disassemble [
^ self disassembleFrom: cogInitialAddress
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> disassembleFrom: anIndex [
^ self disassembleFrom: anIndex opcodes: opcodes
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> disassembleFrom: anIndex opcodes: numberOfInstructions [
^ machineSimulator disassembler
printImmediatesInHexa;
disassembleNext: numberOfInstructions
instructionsIn: (memory memory memoryObject
copyFrom: anIndex - cogInitialAddress + 1
to: memory memory memoryObject size)
startAddress: anIndex
pc: 0
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> framePointerRegisterValue [
^ machineSimulator framePointerRegisterValue
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> framePointerRegisterValue: aValue [
machineSimulator framePointerRegisterValue: aValue
]
{ #category : #configuration }
VMSimpleStackBasedCogitAbstractTest >> generateCaptureCStackPointers [
^ true
]
{ #category : #'helpers - cog methods' }
VMSimpleStackBasedCogitAbstractTest >> generateCogClosure: aBlockClosure selector: anSelectorOop [
| targetCog allocatedAddress |
allocatedAddress := self compile: [
cogit compileFullBlockEntry.
aBlockClosure value ]
bytecodes: 1 headerSize: (cogit sizeof: CogMethod).
targetCog := cogit cogMethodSurrogateAt: allocatedAddress.
targetCog selector: anSelectorOop.
targetCog blockSize: cogit methodZone freeStart - allocatedAddress.
^ targetCog
]
{ #category : #'helpers - cog methods' }
VMSimpleStackBasedCogitAbstractTest >> generateCogMethod: aBlockClosure selector: anSelectorOop [
| targetCog allocatedAddress |
allocatedAddress := self compile: [
cogit compileAbort.
cogit compileEntry.
aBlockClosure value ]
bytecodes: 1 headerSize: (cogit sizeof: CogMethod).
targetCog := cogit cogMethodSurrogateAt: allocatedAddress.
targetCog selector: anSelectorOop.
targetCog blockSize: cogit methodZone freeStart - allocatedAddress.
^ targetCog
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> getLastAddress [
^ machineSimulator getLastAddress: self abstractInstructions.
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> initialCodeSize [
^ 4 * 1024
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> initializeInitialStackFrame [
"I will create the initial stack with a well-known caller address so we know if the code comes back
correctly. The address should be in a valid space of the memory if not unicorn complains.
So we just put it in C RumpStack"
callerAddress := interpreter rumpCStackAddress.
self createFramelessCallFrom: callerAddress receiver: memory nilObject arguments: #().
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> initializeStackMemory [
| page |
stackMemoryStartAddress := self interpreter stackPages minStackAddress.
page := memory coInterpreter stackPages newStackPage.
"The stack pointer is at the top of the stack"
machineSimulator smalltalkStackPointerRegisterValue: page baseAddress.
machineSimulator framePointerRegisterValue: page baseAddress.
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> instructionPointer [
^ self readRegister: machineSimulator instructionPointerRegister
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> interpreterClass [
^ CogVMSimulatorLSB
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> jitCompilerClass [
^ SimpleStackBasedCogit
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> jitMethod: aHostCompiledMethod [
^ self jitMethod: aHostCompiledMethod selector: memory nilObject
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> jitMethod: aHostCompiledMethod selector: aSelectorOop [
| methodOop |
methodOop := self createMethodOopFromHostMethod:aHostCompiledMethod.
^ cogit cog: methodOop selector: aSelectorOop
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> jitOptions [
^ Dictionary newFromPairs: {
#ISA. isa.
#IMMUTABILITY. false.
#bytecodeTableInitializer. #initializeBytecodeTableForSistaV1.
#ObjectMemory. self memoryClass name }
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> machineCodeFrameBuilder [
| builder |
builder := VMMachineCodeFrameBuilderForTest new.
builder initializeWithTest: self.
^ builder
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> machineSimulator [
^ machineSimulator
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> memoryClass [
^ self wordSize = 4
ifTrue: [ Spur32BitCoMemoryManager ]
ifFalse: [ Spur64BitCoMemoryManager ]
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> newJitCompiler [
| compiler |
self jitCompilerClass initializeWithOptions: (Cogit asOptionsDictionary: self jitOptions).
compiler := self jitCompilerClass new
setInterpreter: memory coInterpreter;
yourself.
interpreter cogMethodZone: compiler methodZone.
^ compiler
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> openMachineDebugger [
self openMachineDebuggerAt: cogInitialAddress
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> openMachineDebuggerAt: anAddress [
^ VMMachineCodeDebugger new
cogit: cogit;
abstractInstructions: (self abstractInstructions);
machineSimulator: machineSimulator;
initialInstructionToDisassemble: anAddress;
openWithSpec.
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> pop [
| stackAddressIntegerValue poppedByteArray |
"Getting address from stack register"
stackAddressIntegerValue := machineSimulator smalltalkStackPointerRegisterValue.
"Putting the value in the stack memory"
poppedByteArray := self top.
"Updating SP"
machineSimulator smalltalkStackPointerRegisterValue: stackAddressIntegerValue + self wordSize.
^ poppedByteArray
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> popAddress [
^ self pop integerAt: 1 size: self wordSize signed: false
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> prepareCall [
"Caller side"
machineSimulator hasLinkRegister
ifTrue: [
"The caller IP in ARM is in the linkRegister"
machineSimulator linkRegisterValue: callerAddress ]
ifFalse: [
"I need to push the caller IP, as this is a message send"
self pushAddress: callerAddress ].
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> prepareStackForPrimitiveReceiver: aReceiver arguments: arguments method: aMethod [
machineSimulator baseRegisterValue: cogit varBaseAddress.
"The receiver always goes in registers"
self machineSimulator receiverRegisterValue: aReceiver.
"If the cogit is the StackToRegisterMapping the arguments should go registers, else by stack"
self argumentsOnRegisters
ifTrue: [
arguments withIndexDo: [ :a :i |
i = 1 ifTrue: [ self machineSimulator arg0RegisterValue: a ].
i = 2 ifTrue: [ self machineSimulator arg1RegisterValue: a ].
i > 2 ifTrue: [
self error:
'Cogit calling convention only supports 2 arguments by registers' ] ] ]
ifFalse: [
self pushAddress: aReceiver.
arguments do: [ :anArgument | self pushAddress: anArgument ] ].
"Caller side"
self prepareCall
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> prepareStackForSendReceiver: aReceiver [
self prepareStackForSendReceiver: aReceiver arguments: #()
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> prepareStackForSendReceiver: aReceiver arguments: arguments [
self prepareStackForSendReceiver: aReceiver arguments: arguments method: 0 "No method by default"
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> prepareStackForSendReceiver: aReceiver arguments: arguments method: aMethod [
machineSimulator baseRegisterValue: cogit varBaseAddress.
"The receiver always goes in registers"
self machineSimulator receiverRegisterValue: aReceiver.
self pushAddress: aReceiver.
arguments do: [ :anArgument | self pushAddress: anArgument ].
"If the cogit is the StackToRegisterMapping the arguments should go registers, else by stack"
self argumentsOnRegisters
ifTrue: [
arguments withIndexDo: [ :a :i |
i = 1 ifTrue: [ self machineSimulator arg0RegisterValue: a ].
i = 2 ifTrue: [ self machineSimulator arg1RegisterValue: a ].
i > 2 ifTrue: [
self error:
'Cogit calling convention only supports 2 arguments by registers' ] ] ].
"Caller side"
self prepareCall
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> primitiveTraceLogSize [
^ 256 * 8 "word size"
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> push: aByteArray [
| stackAddressIntegerValue |
self assert: aByteArray size equals: self wordSize.
"Getting address from stack register"
stackAddressIntegerValue := machineSimulator smalltalkStackPointerRegisterValue.
"Updating SP"
stackAddressIntegerValue := stackAddressIntegerValue - self wordSize.
machineSimulator smalltalkStackPointerRegisterValue: stackAddressIntegerValue.
"Putting the value in the stack memory"
machineSimulator
memoryAt: stackAddressIntegerValue
write: aByteArray
size: self wordSize.
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> pushAddress: anInteger [
| aByteArray |
aByteArray := ByteArray new: self wordSize.
aByteArray integerAt: 1 put: anInteger size: self wordSize signed: false.
self push: aByteArray
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> readMemoryAt: anAddress [
| bytes |
bytes := machineSimulator memoryAt: anAddress readNext: self wordSize.
^ bytes integerAt: 1 size: self wordSize signed: false
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> readRegister: aRegisterID [
registerValue := ByteArray new: self wordSize.
machineSimulator register: aRegisterID value readInto: registerValue.
^ registerValue integerAt: 1 size: self wordSize signed: false
]
{ #category : #'helpers - frames' }
VMSimpleStackBasedCogitAbstractTest >> readTemporaryValueAt: anIndex [
"
fp-> saved fp
method
context (initialized to nil)
receiver
first temp
...
sp-> Nth temp
The first temporary is the 6th value from the FP to the top of the stack.
It is negative because the stack begins from a high address and after each push the SP is goingDown
THIS METHOD ONLY WORK IN FRAMEFUL MACHINE FRAMES
"
self assert: cogit needsFrame.
^ self readMemoryAt: machineSimulator framePointerRegisterValue - ((3 + anIndex) * self wordSize)
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> receiverRegister: anInteger [
self writeRegister: UcX86Registers rdx value: anInteger
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> returnValue [
^ self readRegister: machineSimulator receiverRegister
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> runFrom: startAddress until: endAddress [
^ self runFrom: startAddress until: endAddress timeout: 100000. "microseconds"
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> runFrom: startAddress until: endAddress timeout: microseconds [
machineSimulator startAt: startAddress
until: endAddress
timeout: microseconds
count: 0.
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> runGeneratedCode [
"The different platforms generates code in a different way, so the number of opcodes can not be valid. Eg: ARM generates more instructions per opcode. It has to calculate the instructions to run differently"
machineSimulator
startAt: cogInitialAddress
until: self getLastAddress
timeout: 0
count: 0
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> runUntil: address [
machineSimulator startAt: cogInitialAddress
until: address
timeout: 0
count: 3000.
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> runUntilReturn [
machineSimulator startAt: cogInitialAddress
until: callerAddress
timeout: 0
count: 3000.
]
{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> runUntilReturnFrom: anAddress [
machineSimulator startAt: anAddress
until: callerAddress
timeout: 0
count: 0.
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> sentSelector [
^ sentSelector
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> sentSelector: anObject [
sentSelector := anObject
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> setUp [
super setUp.
cogit := self newJitCompiler.
memory coInterpreter cogit: cogit.
memory cogit: cogit.
cogit objectRepresentation class initializeMiscConstants.
cogit entryOffset: (cogit sizeof: CogMethod).
memory nilObject: (self newObjectWithSlots: 0).
memory trueObject: (self newObjectWithSlots: 0).
memory falseObject: (self newObjectWithSlots: 0).
"We don't access its contents, but we need it to be after nil, true and false"
memory hiddenRootsObject: (self newObjectWithSlots: 0).
cogit setStackAlignment: 16 expectedSPOffset: 0 expectedFPOffset: 0.
cogit initializeBackend.
cogInitialAddress := memory getMemoryMap codeZoneStart.
cogit methodZone manageFrom: cogInitialAddress to: memory getMemoryMap codeZoneEnd.
cogit methodZoneBase: cogInitialAddress.
cogit minCallAddress: cogInitialAddress.
cogit cogCodeBase: cogInitialAddress.
memory coInterpreter cogCodeSize: self initialCodeSize.
cogit initializeProcessor.
machineSimulator := cogit processor machineSimulator.
self generateCaptureCStackPointers ifTrue: [cogit generateStackPointerCapture].
cogit methodZone manageFrom: cogit cogCodeBase to: memory getMemoryMap codeZoneEnd.
memory coInterpreter numStackPages: 8.
machineSimulator flushICacheFrom: cogit cogCodeBase to: memory getMemoryMap codeZoneEnd.
self initializeOldSpaceForScavenger.
self initializeStackMemory.
self initializeInitialStackFrame.
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> setUpCogMethodEntry [
cogit allocateOpcodes: 80 bytecodes: 0 ifFail: [ self error: 'Could not allocate opcodes' ].
cogit generateEnilopmarts.
cogit maybeGenerateCheckFeatures.
self setUpTrampolines.
"This is the function a megamorphic PIC will jump when there is a miss"
inlineCacheMissTrampoline := cogit simulatedAddressFor: #ceSendFromInLineCacheMiss:.
cogit computeEntryOffsets.
cogit methodZone manageFrom: cogit methodZoneBase to: memory getMemoryMap codeZoneEnd.
]
{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> setUpTrampolines [
"Create a send trampoline so we can stop..."
sendTrampolineAddress := self compileTrampoline: [ cogit RetN: 0 ] named: #send0argsTrampoline.
cogit ordinarySendTrampolineAt: 0 "args" put: sendTrampolineAddress.
send1TrampolineAddress := self compileTrampoline: [ cogit RetN: 0 ] named: #send1argsTrampoline.
cogit ordinarySendTrampolineAt: 1 "args" put: send1TrampolineAddress.
send2TrampolineAddress := self compileTrampoline: [ cogit RetN: 0 ] named: #send2argsTrampoline.
cogit ordinarySendTrampolineAt: 2 "args" put: send2TrampolineAddress.
send3TrampolineAddress := self compileTrampoline: [ cogit RetN: 0 ] named: #send3argsTrampoline.
cogit ordinarySendTrampolineAt: 3 "args" put: send3TrampolineAddress.
cogit ceCPICMissTrampoline: (self compileTrampoline: [ cogit RetN: 0 ] named:#ceCPICMissTrampoline).
cogit cePICAbortTrampoline: (self compileTrampoline: [ cogit RetN: 0 ] named:#cePICAbortTrampoline).
cogit ceMethodAbortTrampoline: (self compileTrampoline: [ cogit RetN: 0 ] named:#ceMethodAbortTrampoline).
"Set an address as store check trampoline.
The bytecode below will jump to it if it is a old -> young store"
cogit objectRepresentation ceStoreCheckTrampoline: (self compileTrampoline: [ cogit RetN: 0 ] named:#ceStoreCheckTrampoline).
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> stackAddressAt: index [
^ (self stackAt: index)
integerAt: 1
size: self wordSize
signed: false
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> stackAt: index [
| stackAddressIntegerValue |
"Getting address from stack register"
stackAddressIntegerValue := machineSimulator smalltalkStackPointerRegisterValue + (index * self wordSize).
^ machineSimulator memoryAt: stackAddressIntegerValue readNext: self wordSize.
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> stackPointerRegisterValue [
^ machineSimulator stackPointerRegisterValue
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> stackPointerRegisterValue: aValue [
machineSimulator stackPointerRegisterValue: aValue
]
{ #category : #accessing }
VMSimpleStackBasedCogitAbstractTest >> temporaryRegisterValue [
^ self machineSimulator temporaryRegisterValue
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> top [
^ self stackAt: 0
]
{ #category : #'helpers - stack' }
VMSimpleStackBasedCogitAbstractTest >> topAddress [
^ self top integerAt: 1 size: self wordSize signed: false
]
{ #category : #'helpers - registers' }
VMSimpleStackBasedCogitAbstractTest >> writeRegister: aRegister value: anInteger [
| value |
value := ByteArray new: self wordSize.
value integerAt: 1 put: anInteger size: self wordSize signed: false.
machineSimulator register: aRegister value write: value.
]