-
Notifications
You must be signed in to change notification settings - Fork 68
/
Cogit.class.st
12361 lines (11066 loc) · 445 KB
/
Cogit.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
"
I am the code generator for the Cog VM. My job is to produce machine code versions of methods for faster execution and to manage inline caches for faster send performance. I can be tested in the current image using my class-side in-image compilation facilities. e.g. try
StackToRegisterMappingCogit genAndDis: (Integer >> #benchFib)
I have concrete subclasses that implement different levels of optimization:
SimpleStackBasedCogit is the simplest code generator.
StackToRegisterMappingCogit is the current production code generator It defers pushing operands
to the stack until necessary and implements a register-based calling convention for low-arity sends.
SistaCogit is an experimental code generator with support for counting
conditional branches, intended to support adaptive optimization.
RegisterAllocatingCogit is an experimental code generator with support for allocating temporary variables
to registers. It is inended to serve as the superclass to SistaCogit once it is working.
SistaRegisterAllocatingCogit and SistaCogitClone are temporary classes that allow testing a clone of
SistaCogit that inherits from RegisterAllocatingCogit. Once things work these will be merged and
will replace SistaCogit.
coInterpreter <CoInterpreterSimulator>
the VM's interpreter with which I cooperate
methodZoneManager <CogMethodZoneManager>
the manager of the machine code zone
objectRepresentation <CogObjectRepresentation>
the object used to generate object accesses
processor <BochsIA32Alien|?>
the simulator that executes the IA32/x86 machine code I generate when simulating execution in Smalltalk
simulatedTrampolines <Dictionary of Integer -> MessageSend>
the dictionary mapping trap jump addresses to run-time routines used to warp from simulated machine code in to the Smalltalk run-time.
simulatedVariableGetters <Dictionary of Integer -> MessageSend>
the dictionary mapping trap read addresses to variables in run-time objects used to allow simulated machine code to read variables in the Smalltalk run-time.
simulatedVariableSetters <Dictionary of Integer -> MessageSend>
the dictionary mapping trap write addresses to variables in run-time objects used to allow simulated machine code to write variables in the Smalltalk run-time.
printRegisters printInstructions clickConfirm <Boolean>
flags controlling debug printing and code simulation
breakPC <Integer>
machine code pc breakpoint
cFramePointer cStackPointer <Integer>
the variables representing the C stack & frame pointers, which must change on FFI callback and return
selectorOop <sqInt>
the oop of the methodObj being compiled
methodObj <sqInt>
the bytecode method being compiled
initialPC endPC <Integer>
the start and end pcs of the methodObj being compiled
methodOrBlockNumArgs <Integer>
argument count of current method or block being compiled
needsFrame <Boolean>
whether methodObj or block needs a frame to execute
primitiveIndex <Integer>
primitive index of current method being compiled
methodLabel <CogAbstractOpcode>
label for the method header
blockEntryLabel <CogAbstractOpcode>
label for the start of the block dispatch code
stackOverflowCall <CogAbstractOpcode>
label for the call of ceStackOverflow in the method prolog
sendMissCall <CogAbstractOpcode>
label for the call of ceSICMiss in the method prolog
entryOffset <Integer>
offset of method entry code from start (header) of method
entry <CogAbstractOpcode>
label for the first instruction of the method entry code
noCheckEntryOffset <Integer>
offset of the start of a method proper (after the method entry code) from start (header) of method
noCheckEntry <CogAbstractOpcode>
label for the first instruction of start of a method proper
fixups <Array of <AbstractOpcode Label | nil>>
the labels for forward jumps that will be fixed up when reaching the relevant bytecode. fixups has one element per byte in methodObj's bytecode; initialPC maps to fixups[0].
abstractOpcodes <Array of <AbstractOpcode>>
the code generated when compiling methodObj
byte0 byte1 byte2 byte3 <Integer>
individual bytes of current bytecode being compiled in methodObj
bytecodePointer <Integer>
bytecode pc (same as Smalltalk) of the current bytecode being compiled
opcodeIndex <Integer>
the index of the next free entry in abstractOpcodes (this code is translated into C where OrderedCollection et al do not exist)
numAbstractOpcodes <Integer>
the number of elements in abstractOpcocdes
blockStarts <Array of <BlockStart>>
the starts of blocks in the current method
blockCount
the index into blockStarts as they are being noted, and hence eventually the total number of blocks in the current method
labelCounter <Integer>
a nicety for numbering labels not needed in the production system but probably not expensive enough to worry about
ceStackOverflowTrampoline <Integer>
ceSend0ArgsTrampoline <Integer>
ceSend1ArgsTrampoline <Integer>
ceSend2ArgsTrampoline <Integer>
ceSendNArgsTrampoline <Integer>
ceSendSuper0ArgsTrampoline <Integer>
ceSendSuper1ArgsTrampoline <Integer>
ceSendSuper2ArgsTrampoline <Integer>
ceSendSuperNArgsTrampoline <Integer>
ceSICMissTrampoline <Integer>
ceCPICMissTrampoline <Integer>
ceStoreCheckTrampoline <Integer>
ceReturnToInterpreterTrampoline <Integer>
ceBaseFrameReturnTrampoline <Integer>
ceSendMustBeBooleanTrampoline <Integer>
ceClosureCopyTrampoline <Integer>
the various trampolines (system-call-like jumps from machine code to the run-time).
See Cogit>>generateTrampolines for the mapping from trampoline to run-time
routine and then read the run-time routine for a funcitonal description.
ceEnterCogCodePopReceiverReg <Integer>
the enilopmart (jump from run-time to machine-code)
methodZoneBase <Integer>
"
Class {
#name : #Cogit,
#superclass : #VMClass,
#instVars : [
'coInterpreter',
'objectMemory',
'objectRepresentation',
'processor',
'methodZone',
'methodZoneBase',
'codeBase',
'minValidCallAddress',
'lastNInstructions',
'simulatedAddresses',
'simulatedTrampolines',
'simulatedVariableGetters',
'simulatedVariableSetters',
'printRegisters',
'printInstructions',
'compilationTrace',
'clickConfirm',
'breakPC',
'breakBlock',
'singleStep',
'guardPageSize',
'traceFlags',
'traceStores',
'breakMethod',
'methodObj',
'enumeratingCogMethod',
'methodHeader',
'initialPC',
'endPC',
'methodOrBlockNumArgs',
'inBlock',
'needsFrame',
'hasYoungReferent',
'primitiveIndex',
'backEnd',
'literalsManager',
'postCompileHook',
'methodLabel',
'stackCheckLabel',
'stackOverflowCall',
'sendMiss',
'missOffset',
'entryPointMask',
'checkedEntryAlignment',
'uncheckedEntryAlignment',
'cmEntryOffset',
'entry',
'cmNoCheckEntryOffset',
'noCheckEntry',
'fullBlockEntry',
'cbEntryOffset',
'fullBlockNoContextSwitchEntry',
'cbNoSwitchEntryOffset',
'picMNUAbort',
'picInterpretAbort',
'endCPICCase0',
'endCPICCase1',
'firstCPICCaseOffset',
'cPICCaseSize',
'cPICEndSize',
'closedPICSize',
'openPICSize',
'fixups',
'abstractOpcodes',
'generatorTable',
'byte0',
'byte1',
'byte2',
'byte3',
'bytecodePC',
'opcodeIndex',
'numAbstractOpcodes',
'labelCounter',
'cStackAlignment',
'expectedSPAlignment',
'expectedFPAlignment',
'codeModified',
'maxLitIndex',
'ceMethodAbortTrampoline',
'cePICAbortTrampoline',
'ceCheckForInterruptTrampoline',
'ceCPICMissTrampoline',
'ceReturnToInterpreterTrampoline',
'ceBaseFrameReturnTrampoline',
'ceReapAndResetErrorCodeTrampoline',
'ceSendMustBeBooleanAddTrueTrampoline',
'ceSendMustBeBooleanAddFalseTrampoline',
'ceCannotResumeTrampoline',
'ceEnterCogCodePopReceiverReg',
'ceCallCogCodePopReceiverReg',
'ceCallCogCodePopReceiverAndClassRegs',
'cePrimReturnEnterCogCode',
'cePrimReturnEnterCogCodeProfiling',
'ceNonLocalReturnTrampoline',
'ceFetchContextInstVarTrampoline',
'ceStoreContextInstVarTrampoline',
'ceEnclosingObjectTrampoline',
'ceFlushICache',
'ceCheckFeaturesFunction',
'ceTraceLinkedSendTrampoline',
'ceTraceBlockActivationTrampoline',
'ceTraceStoreTrampoline',
'ceGetFP',
'ceGetSP',
'ceCaptureCStackPointers',
'ordinarySendTrampolines',
'superSendTrampolines',
'directedSuperSendTrampolines',
'directedSuperBindingSendTrampolines',
'dynamicSuperSendTrampolines',
'outerSendTrampolines',
'selfSendTrampolines',
'firstSend',
'lastSend',
'realCEEnterCogCodePopReceiverReg',
'realCECallCogCodePopReceiverReg',
'realCECallCogCodePopReceiverAndClassRegs',
'trampolineTableIndex',
'trampolineAddresses',
'objectReferencesInRuntime',
'runtimeObjectRefIndex',
'cFramePointerInUse',
'debugPrimCallStackOffset',
'ceTryLockVMOwner',
'ceUnlockVMOwner',
'extA',
'extB',
'numExtB',
'tempOop',
'numIRCs',
'indexOfIRC',
'theIRCs',
'receiverTags',
'implicitReceiverSendTrampolines',
'cogMethodSurrogateClass',
'cogBlockMethodSurrogateClass',
'CStackPointer',
'CFramePointer',
'cPICPrototype',
'cPICEndOfCodeOffset',
'cPICEndOfCodeLabel',
'ceMallocTrampoline',
'ceFreeTrampoline',
'debugBytecodePointers',
'debugOpcodeIndices',
'disassemblingMethod',
'cogConstituentIndex',
'directedSendUsesBinding',
'simulateFPInUse',
'statCompileFullBlockCount',
'statCompileFullBlockUsecs',
'statCompileMethodCount',
'statCompileMethodUsecs',
'ceSendMustBeBooleanTrampoline',
'jitCodeZoneWriteEnabled',
'ceDeoptimiseFrameTrampoline'
],
#classVars : [
'AnnotationConstantNames',
'AnnotationShift',
'AnnotationsWithBytecodePCs',
'Debug',
'DisplacementMask',
'DisplacementX2N',
'EagerInstructionDecoration',
'FirstAnnotation',
'FirstSpecialSelector',
'HasBytecodePC',
'IsAbsPCReference',
'IsAnnotationExtension',
'IsDirectedSuperBindingSend',
'IsDirectedSuperSend',
'IsDisplacementX2N',
'IsObjectReference',
'IsRelativeCall',
'IsSendCall',
'IsSuperSend',
'MapEnd',
'MaxCPICCases',
'MaxCompiledPrimitiveIndex',
'MaxNumberOfAbstractOpcodes',
'MaxX2NDisplacement',
'NumObjRefsInRuntime',
'NumOopsPerNSC',
'NumSpecialSelectors',
'NumTrampolines',
'ProcessorClass',
'RRRName'
],
#pools : [
'CogAbstractRegisters',
'CogCompilationConstants',
'CogMethodConstants',
'CogRTLOpcodes',
'VMBasicConstants',
'VMBytecodeConstants',
'VMObjectIndices',
'VMStackFrameOffsets'
],
#classInstVars : [
'generatorTable',
'primitiveTable'
],
#category : #'VMMaker-JIT'
}
{ #category : #translation }
Cogit class >> activeCompilerClass [
^InitializationOptions
at: #CogCompilerClass
ifPresent: [:compilerClassName| Smalltalk classNamed: compilerClassName]
ifAbsent:
[(CogAbstractInstruction subclasses detect:
[:compilerClass|
compilerClass ISA == (InitializationOptions at: #ISA ifAbsent: [#IA32])]) defaultCompilerClass]
]
{ #category : #translation }
Cogit class >> additionalHeadersDo: aBinaryBlock [
"Evaluate aBinaryBlock with the names and contents of
any additional header files that need to be generated."
aBinaryBlock
value: 'cogmethod.h'
value: CogMethod cogMethodHeader
]
{ #category : #translation }
Cogit class >> additionalSelectorTables [
^self tableFunctions
]
{ #category : #translation }
Cogit class >> ancilliaryClasses [
"Answer any extra classes to be included in the translation."
ProcessorClass ifNil:
[Cogit initializeMiscConstants].
^(self activeCompilerClass withAllSuperclasses copyUpThrough: CogAbstractInstruction),
{ CogMethodZone.
CogBytecodeDescriptor.
CogBytecodeFixup.
CogPrimitiveDescriptor.
CogMethod.
CogPrimitiveCallState.
self activeCompilerClass literalsManagerClass},
((InitializationOptions at: #SistaVM ifAbsent: [false])
ifTrue: [{SistaMethodZone}]
ifFalse: [#()])
]
{ #category : #'class initialization' }
Cogit class >> annotationConstantNames [
"for printing..."
^AnnotationConstantNames
]
{ #category : #translation }
Cogit class >> apiExportHeaderName [
^'cogit.h'
]
{ #category : #'in-image compilation support' }
Cogit class >> asOptionsDictionary: optionsDictionaryOrArray [
optionsDictionaryOrArray isArray ifTrue:
[| dict |
dict := Dictionary new.
self assert: optionsDictionaryOrArray size even.
1 to: optionsDictionaryOrArray size by: 2 do:
[:i| | key |
key := optionsDictionaryOrArray at: i.
self assert: key isSymbol.
dict at: key put: (optionsDictionaryOrArray at: i + 1)].
^dict].
^optionsDictionaryOrArray
]
{ #category : #documentation }
Cogit class >> assumptions [
"Byte architecture; no 9-bit bit bytes etc.
Stacks grow down. No support for HPPA.
No delay slots. No support for SPARC.
Relative jumps.
All long conditional branches are of the same size.
All long unconditional branches are of the same size.
All MoveCwRs are of the same size.
A long jump and a call have the same displacement layout so both can be labelled with IsRelativeCall.
A PushCw and a MoveCwR have the same constant layout so both can be labelled with IsObjectReference or IsMethodReference.
64k of code per method more than enough"
]
{ #category : #'in-image compilation support' }
Cogit class >> attemptToComputeInstVarNamesFor: aCompiledMethod [
(aCompiledMethod methodClass instSize > 0) ifTrue:
[InitializationOptions
at: #instVarNames
put: (aCompiledMethod methodClass allInstVarNames)]
]
{ #category : #'in-image compilation support' }
Cogit class >> attemptToComputeTempNamesFor: aCompiledMethod [
(aCompiledMethod respondsTo: #tempNames) ifTrue:
[| schematicTemps blocks |
schematicTemps := aCompiledMethod methodNode schematicTempNamesString.
blocks := aCompiledMethod embeddedBlockClosures.
InitializationOptions
at: #tempNames
put: (Dictionary newFrom: {aCompiledMethod initialPC -> (self decomposeSchematicTemps: (schematicTemps copyUpTo: $[))},
(blocks
ifEmpty: [#()]
ifNotEmpty:
[aCompiledMethod embeddedBlockClosures
with: (schematicTemps first = $[
ifTrue: [schematicTemps piecesCutWhere: [:a :b| b = $[]]
ifFalse: [(schematicTemps piecesCutWhere: [:a :b| b = $[]) allButFirst])
collect: [:c :s| c startpc -> (self decomposeSchematicTemps: (s copyWithoutAll: '[]'))]]))]
]
{ #category : #'accessing class hierarchy' }
Cogit class >> chooseCogitClass [
^Smalltalk at: ([:choices| choices at: (UIManager default chooseFrom: choices) ifAbsent: [^nil]]
value: (Cogit allSubclasses collect: [:ea| ea name]) sorted)
]
{ #category : #'in-image compilation' }
Cogit class >> cog: aCompiledMethod [
^self cog: aCompiledMethod options: #()
]
{ #category : #'in-image compilation' }
Cogit class >> cog: aCompiledMethod options: optionsArray [
^self cog: aCompiledMethod selectorOrNumCopied: aCompiledMethod selector options: optionsArray
]
{ #category : #'in-image compilation' }
Cogit class >> cog: aCompiledMethod selector: aSelector [
^self cog: aCompiledMethod selectorOrNumCopied: aSelector options: #()
]
{ #category : #'in-image compilation' }
Cogit class >> cog: aCompiledMethod selectorOrNumCopied: selectorOrNumCopied options: optionsDictionaryOrArray [
"StackToRegisterMappingCogit cog: (Integer >> #benchFib) selector: #benchFib options: #()"
| cogit coInterpreter |
cogit := self initializedInstanceForTests: optionsDictionaryOrArray.
coInterpreter := cogit coInterpreter.
self attemptToComputeTempNamesFor: aCompiledMethod.
self attemptToComputeInstVarNamesFor: aCompiledMethod.
^{ coInterpreter.
cogit.
selectorOrNumCopied isInteger
ifTrue: [ cogit cogFullBlockMethod: (coInterpreter oopForObject: aCompiledMethod) numCopied: selectorOrNumCopied ]
ifFalse: [ cogit cog: (coInterpreter oopForObject: aCompiledMethod) selector: (coInterpreter oopForObject: selectorOrNumCopied) ] }
]
{ #category : #documentation }
Cogit class >> cogToBytecodeMethodMapping [
"When first written the VM maintained a one-to-one mapping between bytecoded methods and cog methods.
The map is implemented with a direct reference form the methodObject field of a CogMethod to the bytecoded
method and by setting the header word of the bytecoded method to the CogMethod. Newspeak forced a change
in this since Newspeak shares anonymous (nil methodClassAssociation) accessors (setters and getters) amongst
classes and mixins throughout the system (i.e. these accessors are in dictionaries under selector keys that don't
match the accessor methods' selectors). But the inline cacheing relinking/un;linking machinery depends on
CogMethods having the correct selector, since to unlink/relink the selector is fetched from the inline cache's
target method. So the VM has been modified to not insist on a one-to-one mapping. Instead, when a bytecoded
method is to be used with a different selector it is attached to the new CogMethod.
This choice means that CogMethods do refer to a valid bytecoded method, but the bytecoded method might
not necessarily refer back to a CogMethod referring to it. So when CogMethods are freed care must be taken
to reset the bytecoded method's header if it is referring to the freed CogMethod."
]
{ #category : #translation }
Cogit class >> declareCVarsIn: aCCodeGenerator [
#( 'coInterpreter' 'objectMemory' 'methodZone' 'objectRepresentation'
'cogBlockMethodSurrogateClass' 'cogMethodSurrogateClass'
'processor' 'lastNInstructions' 'simulatedAddresses'
'simulatedTrampolines' 'simulatedVariableGetters'
'simulatedVariableSetters' 'printRegisters' 'printInstructions'
'clickConfirm' 'singleStep' ) do: [
:simulationVariableNotNeededForRealVM |
aCCodeGenerator removeVariable: simulationVariableNotNeededForRealVM ].
#( 'selfSendTrampolines' 'dynamicSuperSendTrampolines'
'implicitReceiverSendTrampolines'
'outerSendTrampolines' 'ceEnclosingObjectTrampoline'
'numIRCs' 'indexOfIRC' 'theIRCs' ) do: [
:variableNotNeededInNormalVM |
aCCodeGenerator removeVariable: variableNotNeededInNormalVM ].
aCCodeGenerator
addHeaderFile: '<stddef.h>';
"for e.g. offsetof"addHeaderFile: '"jit/jit.h"';
addHeaderFile: '"debug.h"';
addHeaderFile: '"sqCogStackAlignment.h"';
addHeaderFile: '"dispdbg.h"';
"must precede cointerp.h & cogit.h otherwise NoDbgRegParms gets screwed up"
addHeaderFile: '"cogmethod.h"'.
aCCodeGenerator
addHeaderFile: '"vmMemoryMap.h"';
addHeaderFile: '"vmRememberedSet.h"';
addHeaderFile: '"cointerp.h"';
addHeaderFile: '"cogit.h"';
addHeaderFile: '<stdint.h>'.
aCCodeGenerator addHeaderFile: '<pthread.h>'.
aCCodeGenerator
var: #ceGetFP declareC: 'usqIntptr_t (*ceGetFP)(void)';
var: #ceGetSP declareC: 'usqIntptr_t (*ceGetSP)(void)';
var: #ceCaptureCStackPointers
declareC: 'void (*ceCaptureCStackPointers)(void)';
var: #ceEnterCogCodePopReceiverReg
declareC: 'void (*ceEnterCogCodePopReceiverReg)(void)';
var: #realCEEnterCogCodePopReceiverReg
declareC: 'void (*realCEEnterCogCodePopReceiverReg)(void)';
var: #ceCallCogCodePopReceiverReg
declareC: 'void (*ceCallCogCodePopReceiverReg)(void)';
var: #realCECallCogCodePopReceiverReg
declareC: 'void (*realCECallCogCodePopReceiverReg)(void)';
var: #ceCallCogCodePopReceiverAndClassRegs
declareC: 'void (*ceCallCogCodePopReceiverAndClassRegs)(void)';
var: #realCECallCogCodePopReceiverAndClassRegs
declareC: 'void (*realCECallCogCodePopReceiverAndClassRegs)(void)';
var: #ceFlushICache
declareC:
'static void (*ceFlushICache)(usqIntptr_t from, usqIntptr_t to)';
var: #ceCheckFeaturesFunction
declareC: 'static usqIntptr_t (*ceCheckFeaturesFunction)(void)';
var: #ceTryLockVMOwner
declareC: 'usqIntptr_t (*ceTryLockVMOwner)(void)';
var: #ceUnlockVMOwner declareC: 'void (*ceUnlockVMOwner)(void)';
var: #postCompileHook
declareC: 'void (*postCompileHook)(CogMethod *)';
var: #openPICList declareC: 'CogMethod *openPICList = 0';
var: #maxMethodBefore type: #'CogMethod *';
var: 'enumeratingCogMethod' type: #'CogMethod *'.
aCCodeGenerator
declareVar: 'aMethodLabel' type: #AbstractInstruction;
"Has to come lexicographically before backEnd & methodLabel"var:
#backEnd
declareC: 'AbstractInstruction * const backEnd = &aMethodLabel';
var: #methodLabel
declareC: 'AbstractInstruction * const methodLabel = &aMethodLabel'.
self
declareC:
#( abstractOpcodes stackCheckLabel blockEntryLabel blockEntryNoContextSwitch
stackOverflowCall sendMiss entry noCheckEntry selfSendEntry
dynSuperEntry fullBlockNoContextSwitchEntry
fullBlockEntry picMNUAbort picInterpretAbort endCPICCase0
endCPICCase1 cPICEndOfCodeLabel )
as: #'AbstractInstruction *'
in: aCCodeGenerator.
aCCodeGenerator declareVar: #fixups type: #'BytecodeFixup *'.
aCCodeGenerator
var: #jitCodeZoneWriteEnabled
declareC: 'int jitCodeZoneWriteEnabled = 0';
var: #ordinarySendTrampolines
declareC: 'sqInt ordinarySendTrampolines[NumSendTrampolines]';
var: #superSendTrampolines
declareC: 'sqInt superSendTrampolines[NumSendTrampolines]'.
BytecodeSetHasDirectedSuperSend ifTrue: [
aCCodeGenerator
var: #directedSuperSendTrampolines
declareC: 'sqInt directedSuperSendTrampolines[NumSendTrampolines]';
var: #directedSuperBindingSendTrampolines
declareC:
'sqInt directedSuperBindingSendTrampolines[NumSendTrampolines]' ].
aCCodeGenerator
var: #trampolineAddresses
declareC: 'static char *trampolineAddresses[NumTrampolines*2]';
var: #objectReferencesInRuntime
declareC:
'static usqInt objectReferencesInRuntime[NumObjRefsInRuntime+1]';
var: #labelCounter type: #int;
var: #traceFlags
declareC: 'int traceFlags = 8 /* prim trace log on by default */';
var: #cStackAlignment
declareC: 'const int cStackAlignment = STACK_ALIGN_BYTES'.
aCCodeGenerator
declareVar: #CFramePointer type: #'void *';
declareVar: #CStackPointer type: #'void *';
declareVar: #minValidCallAddress type: #usqIntptr_t;
declareVar: #debugPrimCallStackOffset type: #usqIntptr_t.
aCCodeGenerator vmClass generatorTable ifNotNil: [ :bytecodeGenTable |
aCCodeGenerator
var: #generatorTable
declareC: 'static BytecodeDescriptor generatorTable['
, bytecodeGenTable size printString , ']'
, (self tableInitializerFor: bytecodeGenTable in: aCCodeGenerator) ].
"In C the abstract opcode names clash with the Smalltak generator syntactic sugar.
Most of the syntactic sugar is inlined, but alas some remains. Rename the syntactic
sugar to avoid the clash."
(self organization listAtCategoryNamed:
#'abstract instructions') do: [ :s |
aCCodeGenerator
addSelectorTranslation: s
to: 'g' , (aCCodeGenerator cFunctionNameFor: s) ].
aCCodeGenerator addSelectorTranslation: #halt: to: 'haltmsg'
]
{ #category : #'in-image compilation support' }
Cogit class >> decomposeSchematicTemps: aString [
^Array streamContents:
[:ws| | rs |
rs := aString readStream.
[rs atEnd] whileFalse:
[ws nextPut: (rs peek = $(
ifTrue: [[rs upThrough: $)]
on: MessageNotUnderstood
do: [:ex| (rs upTo: $)), ')']]
ifFalse: [rs upTo: Character space])]]
]
{ #category : #'C translation' }
Cogit class >> defineAtCompileTime: anObject [
"Override to define at translation time those variables that need to
be defined at compile time only in plugins, but not in the main VM,
because the VM generated is specific to these varables."
anObject isSymbol ifFalse:
[^false].
(#(STACKVM COGVM SPURVM) includes: anObject) ifTrue:
[^false].
^VMBasicConstants namesDefinedAtCompileTime includes: anObject
]
{ #category : #translation }
Cogit class >> doInlining [
"inline only those methods that are marked with <inline: true>"
^#asSpecifiedOrQuick
]
{ #category : #'in-image compilation' }
Cogit class >> genAndDisPICoptions: optionsDictionaryOrArray [
"StackToRegisterMappingCogit genAndDisPICoptions: #(ISA ARMv5 CogCompilerClass CogOutOfLineLiteralsARMCompiler)"
| cogit |
cogit := self initializedInstanceForTests: optionsDictionaryOrArray.
cogit disassembleFrom: cogit cPICPrototype + (cogit sizeof: CogMethod) to: cogit cPICPrototype + cogit closedPICSize
]
{ #category : #translation }
Cogit class >> generateCodeStringForCogitDotC [
"Generate a skeletal cogit.c that includes the relevant cogitFOO.c
for the appropriate subclasses of CogAbstractInstruction.
self generateCodeStringForCogitDotC"
| string insertPosition abis defaultDef |
abis := OrderedCollection new.
string := String streamContents:
[:s|
s nextPutAll: '/* Automatically generated by\ ' withCRs.
s nextPutAll: (CCodeGenerator monticelloDescriptionFor: self).
s cr; nextPutAll: ' */'.
s cr; cr; nextPut: $#.
insertPosition := s position.
self translateableInstructionSubclassesAndInstalledOptionsDo:
[:class | | abi |
s nextPutAll: 'if '.
(abi := InitializationOptions at: #ABI ifAbsent: []) ifNotNil:
[s nextPutAll: (abis addLast: abi); nextPutAll: ' && ('].
class identifyingPredefinedMacros
do: [:predefinedMacro| s nextPutAll: 'defined('; nextPutAll: predefinedMacro; nextPut: $)]
separatedBy: [s nextPutAll: ' || '].
abi ifNotNil: [s nextPut: $)].
s cr; cr; nextPutAll: '# include "'; nextPutAll: class moduleName; nextPutAll: '.c"'.
s cr; cr; nextPutAll: '#el'].
s nextPutAll: 'se'.
#( 'As yet no Cogit implementation appears to exist for your platform.'
'Consider implementing it, starting by adding a subclass of CogAbstractInstruction.') do:
[:msg| s cr; nextPutAll: '# error '; nextPutAll: msg].
s cr; nextPutAll: '#endif'; cr].
abis isEmpty ifTrue:
[^string].
defaultDef := String streamContents:
[:s|
s nextPutAll: '#if !'.
abis do: [:abi| s nextPutAll: abi] separatedBy: [s nextPutAll: ' && !'].
s cr; nextPutAll: '# define '; nextPutAll: abis first; nextPutAll: ' 1'; cr.
s nextPutAll: '#endif'; cr; cr].
^string copyReplaceFrom: insertPosition to: insertPosition - 1 with: defaultDef
]
{ #category : #accessing }
Cogit class >> generatorTable [
^generatorTable
]
{ #category : #'class initialization' }
Cogit class >> generatorTableFrom: anArray [
generatorTable := CArrayAccessor on: (Array new: 256).
anArray do:
[:tuple| | descriptor |
(descriptor := CogBytecodeDescriptor new)
numBytes: tuple first;
generator: tuple fourth;
isReturn: (tuple includes: #return);
hasUnsafeJump: (tuple includes: #hasUnsafeJump);
isMapped: ((tuple includes: #isMappedIfImmutability)
ifTrue: [self bindingOf: #IMMUTABILITY]
ifFalse: [tuple includes: #isMapped]);
isMappedInBlock: (tuple includes: #isMappedInBlock);
spanFunction: ((tuple includes: #branch)
ifTrue: [ tuple detect: [:thing| thing isSymbol and: [
thing numArgs = 2 or: [thing numArgs = 4]]]]);
isBranchTrue: (tuple includes: #isBranchTrue);
isBranchFalse: (tuple includes: #isBranchFalse);
isExtension: (tuple includes: #extension);
isInstVarRef: (tuple includes: #isInstVarRef); "for Spur"
is1ByteInstVarStore: (tuple includes: #is1ByteInstVarStore); "for Spur"
yourself.
PartialCompilation ifFalse: [
"As a hack to cut down on descriptor flags, use opcode to tag unusedBytecode for scanning.
Currently descriptors are exactly 16 bytes with all 8 flag bits used.
As another hack to eliminate a test in scanMethod mark unknowns as extensions."
descriptor generator == #unknownBytecode ifTrue:
[descriptor opcode: Nop; isExtension: true].
].
tuple do:
[:thing|
thing isSymbol ifTrue:
[(thing beginsWith: #needsFrame) ifTrue:
[descriptor needsFrameFunction: thing].
(CogRTLOpcodes classPool at: thing ifAbsent: []) ifNotNil:
[:opcode| descriptor opcode: opcode]]].
tuple last isInteger
ifTrue: [descriptor stackDelta: tuple last]
ifFalse:
[descriptor needsFrameFunction ifNotNil:
[self error: 'frameless block bytecodes must specify a stack delta']].
tuple second to: tuple third do:
[:index|
generatorTable at: index put: descriptor]].
^generatorTable
]
{ #category : #accessing }
Cogit class >> guardPageSize [
^1024
]
{ #category : #translation }
Cogit class >> implicitReturnTypeFor: aSelector [
"Answer the return type for methods that don't have an explicit return."
^#void
]
{ #category : #'class initialization' }
Cogit class >> initializeAnnotationConstants [
"These form the method map for a cog method. The map defines which addresses
in a machine code method are ones with important functions, such as being a send
site or being a reference to a heap object. Each annotated instruction has a byte
in the map, and each byte in the map has two parts. In the least signficant bits are
a distance in codeGranularity units from the start of the method or the previous
map entry, except for the IsAnnotationExtension type. In the most signficant bits
are the type of annotation at the point reached. A null byte ends the map. The
first mapped location is a distance from the cmNoCheckEntryOffset.
The map occurs at the end of a method (*), in reverse, so that its start is found
by adding the method's block size. If the distance between two mapped
instructions will not fit in the displacement field then one or more displacement
entries are placed in the map to bridge the gap. There is a * 32 displacement
units type for spanning large gaps. The displacements are in codeGranularity
units so that processors like e.g. ARM, with 4-byte instructions, do not have overly
large maps. In [practice maps are very compact, but they should be as quick to
navigate as possible, and hence be as compact as possible.
There is only one kind of call annotation that serves for all calls from machine
code. There are several kinds of call, sends, super sends, calls of the generated
run-time, and direct calls of primitive functions in the interpreter. These need
different treatment at different times. For example, when the send cache is
flushed or the method zone is shrunk some sends must be unlinked and some
sends must be relocated. But to be able to parse bytecoded methods and match
their pcs with corresponding machine code pcs the map needs to differentiate
between sends and run-time calls.
Sends can be distinguished from run-time or direct primitive calls based on address;
only sends have their target between methodZoneBase and methodZone freeStart.
We used to distinguish normal sends from super sends based on alignment of
entry-point, because normal sends link to the checked entry-point, whereas super sends
link to the unchecked entry-point, and both entry points have different alignments.
But now we use the IsAnnotationExtension to label sends other than normal sends.
For these ``exotic'' sends there is both an IsAnnotationExtension annotation and an
IsSendCall annotation.
While run-time calls can be distinguished from direct primitive calls on the basis
of address there is no need to do so. They are merely calls to locations that
don't move during method zone compaction.
Absolute PC references are used for method references and counter references.
These are references from within a particular method to absolute pcs in that same
method that must be relocated when the method moves."
"self initializeAnnotationConstants"
AnnotationShift := 5.
IsDisplacementX2N := 0. "N.B. A 0 byte ends the map"
IsAnnotationExtension := 1. "Used to extend IsSendCall with different codes for exotic send types."
IsObjectReference := 2.
IsAbsPCReference := 3.
IsRelativeCall := 4.
HasBytecodePC := 5.
IsSendCall := 7.
"These are formed by combining IsSendCall and IsAnnotationExtension annotations."
IsSuperSend := 8.
IsDirectedSuperSend := BytecodeSetHasDirectedSuperSend ifTrue: [9].
IsDirectedSuperBindingSend := BytecodeSetHasDirectedSuperSend ifTrue: [10].
DisplacementMask := (1 << AnnotationShift) - 1.
DisplacementX2N := IsDisplacementX2N << AnnotationShift.
FirstAnnotation := IsObjectReference << AnnotationShift.
MaxX2NDisplacement := DisplacementMask << AnnotationShift.
MapEnd := 0.
AnnotationConstantNames := #( IsDisplacementX2N
IsAnnotationExtension
IsObjectReference
IsAbsPCReference
IsRelativeCall
HasBytecodePC
IsNSSendCall
IsSendCall
IsSuperSend
IsDirectedSuperSend
IsDirectedSuperBindingSend
IsNSSelfSend
IsNSDynamicSuperSend
IsNSImplicitReceiverSend).
AnnotationsWithBytecodePCs := #(HasBytecodePC
IsNSSendCall
IsSendCall
IsSuperSend
IsDirectedSuperSend
IsDirectedSuperBindingSend
IsNSSelfSend
IsNSDynamicSuperSend
IsNSImplicitReceiverSend),
{'IsRelativeCall:\HasBytecodePC' withCRs}
]
{ #category : #'class initialization' }
Cogit class >> initializeBytecodeTable [
"SimpleStackBasedCogit initializeBytecodeTableWith: Dictionary new"
"StackToRegisterMappingCogit initializeBytecodeTableWith: Dictionary new"
| initializer |
BytecodeSetHasDirectedSuperSend := BytecodeSetHasExtensions := false.
PartialCompilation := InitializationOptions at: #PartialCompilation ifAbsent: [ false ].
initializer := InitializationOptions
at: #bytecodeTableInitializer
ifAbsent: [ #initializeBytecodeTableForSistaV1 ].
"Now make sure all classes in the hierarchy have initialized to the same bytecode table."
(self withAllSuperclasses copyUpTo: Cogit) reverseDo: [ :cogitClass | "i.e. exclude Cogit"
cogitClass perform: initializer ]
]
{ #category : #'class initialization' }
Cogit class >> initializeCogMethodConstants [
CMMegamorphicIC := 1 + (CMPolymorphicIC := 1 + (CMMethod := 1 + (CMFree := 1)))
]
{ #category : #'class initialization' }
Cogit class >> initializeErrorCodes [
"External errors, returned to or from cog:selector:"
NotFullyInitialized := -1.
InsufficientCodeSpace := -2.
MethodTooBig := -4.
YoungSelectorInPIC := -5.
EncounteredUnknownBytecode := -6.
UnimplementedPrimitive := -7.
ShouldNotJIT := -8.
MaxNegativeErrorCode := ShouldNotJIT.
"Internal errors returned by generator routines to other generator routines"
BadRegisterSet := 1.
UnimplementedOperation := 2.
"Internal successes answered by CogObjectRepresentation to JIT, etc"
UnfailingPrimitive := 3. "Answered by a primitive generator for a primitive that will never fail"
CompletePrimitive := 4 "Answered by a primitive generator that does not bneed to fall back on the interpreter primitive except for an error code."
]
{ #category : #'class initialization' }
Cogit class >> initializeMiscConstants [
super initializeMiscConstants.
Debug := InitializationOptions at: #Debug ifAbsent: [false].
(InitializationOptions includesKey: #EagerInstructionDecoration)
ifTrue:
[EagerInstructionDecoration := InitializationOptions at: #EagerInstructionDecoration]
ifFalse:
[EagerInstructionDecoration ifNil:
[EagerInstructionDecoration := false]]. "speeds up single stepping but could lose fidelity"
ProcessorClass := InitializationOptions at: #ProcessorClass ifAbsentPut: [ UnicornProcessor ].
CogCompilerClass := self activeCompilerClass.
(CogCompilerClass withAllSuperclasses copyUpTo: CogAbstractInstruction) reverseDo:
[:compilerClass| compilerClass initialize; initializeAbstractRegisters].
self objectMemoryClass objectRepresentationClass initializeMiscConstants.
"Our criterion for which methods to JIT is literal count. The default value is 60 literals or less."
MaxLiteralCountForCompile := InitializationOptions at: #MaxLiteralCountForCompile ifAbsent: [60].
"we special-case 0, 1 & 2 argument sends, N is numArgs >= 3"
NumSendTrampolines := 4.
"Currently not even the ceImplicitReceiverTrampoline contains object references."
NumObjRefsInRuntime := 0.
"6 is a fine number for the max number of PCI entries. 8 is too large."
MaxCPICCases := 6.
"One variable defines whether in a block and whether in a vanilla or full block."
InFullBlock := 2.
"The MaxNumberOfAbstractOpcodes should fits in a 16 bit integer (e.g. CogBytecodeFixup's instructionIndex)."
MaxNumberOfAbstractOpcodes := 32768.
]
{ #category : #'class initialization' }
Cogit class >> initializeNumTrampolines [
NumTrampolines := self numTrampolines
+ self objectRepresentationClass numTrampolines
+ (BytecodeSetHasDirectedSuperSend ifTrue: [NumSendTrampolines * 2] ifFalse: [0])
]
{ #category : #'class initialization' }
Cogit class >> initializePrimitiveTable [
"Initialize the table of primitive generators. This does not include normal primitives implemented in the coInterpreter.
N.B. primitives that don't have an explicit arg count (the integer following the generator) may be variadic."
"SimpleStackBasedCogit initializePrimitiveTable"
MaxCompiledPrimitiveIndex := self objectRepresentationClass wordSize = 8
ifTrue: [659]
ifFalse: [222].
primitiveTable := CArrayAccessor on: (Array new: MaxCompiledPrimitiveIndex + 1).
self table: primitiveTable from:
#( "Integer Primitives (0-19)"
(1 genPrimitiveAdd 1)
(2 genPrimitiveSubtract 1)
(3 genPrimitiveLessThan 1)
(4 genPrimitiveGreaterThan 1)
(5 genPrimitiveLessOrEqual 1)
(6 genPrimitiveGreaterOrEqual 1)
(7 genPrimitiveEqual 1)
(8 genPrimitiveNotEqual 1)
(9 genPrimitiveMultiply 1)
(10 genPrimitiveDivide 1)
(11 genPrimitiveMod 1)
(12 genPrimitiveDiv 1)
(13 genPrimitiveQuo 1)
(14 genPrimitiveBitAnd 1)
(15 genPrimitiveBitOr 1)
(16 genPrimitiveBitXor 1)
(17 genPrimitiveBitShift 1)
"(18 primitiveMakePoint)"
"(19 primitiveFail)" "Guard primitive for simulation -- *must* fail"
"LargeInteger Primitives (20-39)"
"(20 primitiveFail)"
"(21 primitiveAddLargeIntegers)"
"(22 primitiveSubtractLargeIntegers)"
"(23 primitiveLessThanLargeIntegers)"
"(24 primitiveGreaterThanLargeIntegers)"
"(25 primitiveLessOrEqualLargeIntegers)"
"(26 primitiveGreaterOrEqualLargeIntegers)"
"(27 primitiveEqualLargeIntegers)"
"(28 primitiveNotEqualLargeIntegers)"
"(29 primitiveMultiplyLargeIntegers)"
"(30 primitiveDivideLargeIntegers)"
"(31 primitiveModLargeIntegers)"
"(32 primitiveDivLargeIntegers)"
"(33 primitiveQuoLargeIntegers)"
"(34 primitiveBitAndLargeIntegers)"
"(35 primitiveBitOrLargeIntegers)"
"(36 primitiveBitXorLargeIntegers)"
"(37 primitiveBitShiftLargeIntegers)"
"Float Primitives (38-59)"
"(38 genPrimitiveFloatAt)"
"(39 genPrimitiveFloatAtPut)"
(40 genPrimitiveAsFloat 0)
(41 genPrimitiveFloatAdd 1)