Skip to content

Commit

Permalink
Merge pull request #164 from Alamvic/improve-code-generation
Browse files Browse the repository at this point in the history
Improve code generation
  • Loading branch information
PalumboN authored Feb 24, 2025
2 parents ccdc16f + 2ae24d8 commit 04e022f
Show file tree
Hide file tree
Showing 15 changed files with 813 additions and 854 deletions.
31 changes: 31 additions & 0 deletions Druid-Tests/DRCogitRegisterAllocatorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,37 @@ DRCogitRegisterAllocatorTest >> testSecondArgumentUsedButNotFirst [
self assert: ({ firstRegister . secondRegister } includes: basicBlock instructions fourth result).
]

{ #category : #tests }
DRCogitRegisterAllocatorTest >> testStackDependeciesReuseRegister [

| cfg firstRegister secondRegister r0 r1 r2 phi push |

cfg := self setUpCFGWithConditional.

r0 := cfg b1 loadStackValueAt: 0.
cfg b1 endInstruction replaceOperandAtIndex: 1 by: r0.

r1 := cfg b2 copy: DRInterpreterReference trueObject.
r1 markAsStaged.

r2 := cfg b3 copy: DRInterpreterReference falseObject.
r2 markAsStaged.

phi := cfg b4 phiWith: r1 with: r2.
push := cfg b4 push: phi.
push stackDependencies: { r0 }. "!!!"
cfg b4 endInstruction: (DRContinueNextBytecode result: DRNoRegister new).


firstRegister := DRPhysicalGeneralPurposeRegister name: 'PR1'.
secondRegister := DRPhysicalGeneralPurposeRegister name: 'PR2'.
DRCogitLinearScanRegisterAllocator new
integerRegisters: { firstRegister. secondRegister };
allocateRegistersIn: cfg.

self assert: r0 result equals: phi result
]

{ #category : #tests }
DRCogitRegisterAllocatorTest >> testStagedInstructionAllocatesStagedRegister [

Expand Down
4 changes: 2 additions & 2 deletions Druid-Tests/DRCogitStackToRegisterGeneratorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ DRCogitStackToRegisterGeneratorTest >> testGenerateMoveConstant [

| cfg r1 generatedASTInstruction |
cfg := DRControlFlowGraph new.
r1 := cfg initialBasicBlock copy: DRInterpreterReference nilObject.
r1 := cfg initialBasicBlock copy: DRInterpreterReference methodObj.
DRStager new applyTo: cfg.
self generateCogitForInstruction: r1.
generatedASTInstruction := self statements last.

self assert: generatedASTInstruction selector equals: #genMoveConstant:R:.
self
assert: generatedASTInstruction arguments first name
equals: 'objectMemory nilObject'
equals: #methodObj
]

{ #category : #tests }
Expand Down
53 changes: 18 additions & 35 deletions Druid-Tests/DRInterpreterCompilationUnitTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,18 @@ DRInterpreterCompilationUnitTest >> testBytecodeCompilation [
"Here the bytecode range is a single range sharing the same selector.
That selector will be used for compilation"
bytecodes := (self compiler newBytecodes:
(self compiler interpreterClass lookupSelector:
#storeAndPopReceiverVariableBytecode)) first: 4.
(self compiler interpreterClass lookupSelector: #storeAndPopReceiverVariableBytecode)) first:
4.
compilationUnit bytecodes: bytecodes.
self compile: compilationUnit.

self assert: self targetClass bytecodeTable equals: {
#( 1 200 200 #gen_StoreAndPopReceiverVariableBytecode0
#isInstVarRef #isMappedIfImmutability #needsFrameIfImmutability:
-1 ).
#( 1 201 201 #gen_StoreAndPopReceiverVariableBytecode1
#isInstVarRef #isMappedIfImmutability #needsFrameIfImmutability:
-1 ).
#( 1 202 202 #gen_StoreAndPopReceiverVariableBytecode2
#isInstVarRef #isMappedIfImmutability #needsFrameIfImmutability:
-1 ).
#( 1 203 203 #gen_StoreAndPopReceiverVariableBytecode3
#isInstVarRef #isMappedIfImmutability #needsFrameIfImmutability:
-1 ) }.
#( 1 200 200 #gen_StoreAndPopReceiverVariableBytecode0 #isInstVarRef #isMapped ).
#( 1 201 201 #gen_StoreAndPopReceiverVariableBytecode1 #isInstVarRef #isMapped ).
#( 1 202 202 #gen_StoreAndPopReceiverVariableBytecode2 #isInstVarRef #isMapped ).
#( 1 203 203 #gen_StoreAndPopReceiverVariableBytecode3 #isInstVarRef #isMapped ) }.

self targetClass bytecodeTable do: [ :e |
self assert: (self targetClass canUnderstand: e fourth) ]
self targetClass bytecodeTable do: [ :e | self assert: (self targetClass canUnderstand: e fourth) ]
]

{ #category : #'tests - bytecodes' }
Expand Down Expand Up @@ -118,33 +109,25 @@ DRInterpreterCompilationUnitTest >> testManyBytecodesCompilation [
"Here the bytecode range is a single range sharing the same selector.
That selector will be used for compilation"
bytecodes := ((self compiler newBytecodes:
(self compiler interpreterClass lookupSelector:
#storeAndPopReceiverVariableBytecode)) first: 3)
, { (DRBytecodeObject new
bytecodeSize: 2;
bytecodeNumberStart: 3;
bytecodeNumberEnd: 5;
supported: false;
yourself) }.
(self compiler interpreterClass lookupSelector: #storeAndPopReceiverVariableBytecode))
first: 3) , { (DRBytecodeObject new
bytecodeSize: 2;
bytecodeNumberStart: 3;
bytecodeNumberEnd: 5;
supported: false;
yourself) }.
compilationUnit bytecodes: bytecodes.
self compile: compilationUnit.

self assert: self targetClass bytecodeTable equals: {
#( 2 3 3 #unknownBytecode ).
#( 2 4 4 #unknownBytecode ).
#( 2 5 5 #unknownBytecode ).
#( 1 200 200 #gen_StoreAndPopReceiverVariableBytecode0
#isInstVarRef #isMappedIfImmutability #needsFrameIfImmutability:
-1 ).
#( 1 201 201 #gen_StoreAndPopReceiverVariableBytecode1
#isInstVarRef #isMappedIfImmutability #needsFrameIfImmutability:
-1 ).
#( 1 202 202 #gen_StoreAndPopReceiverVariableBytecode2
#isInstVarRef #isMappedIfImmutability #needsFrameIfImmutability:
-1 ) }.
#( 1 200 200 #gen_StoreAndPopReceiverVariableBytecode0 #isInstVarRef #isMapped ).
#( 1 201 201 #gen_StoreAndPopReceiverVariableBytecode1 #isInstVarRef #isMapped ).
#( 1 202 202 #gen_StoreAndPopReceiverVariableBytecode2 #isInstVarRef #isMapped ) }.

self targetClass bytecodeTable do: [ :e |
self assert: (self targetClass canUnderstand: e fourth) ]
self targetClass bytecodeTable do: [ :e | self assert: (self targetClass canUnderstand: e fourth) ]
]

{ #category : #'tests - primitives' }
Expand Down
10 changes: 3 additions & 7 deletions Druid-Tests/DRLinearScanRegisterAllocatorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ DRLinearScanRegisterAllocatorTest >> testDoNotInsertCopyForStagedNonMovableConst
cfg := DRControlFlowGraph new.

topBasicBlock := cfg newBasicBlockWith: [ :block |
r0 := block copy:
(DRInterpreterReference objectReference:
'methodObj').
r0 := block copy: DRInterpreterReference methodObj.
r1 := (block copy: 2) markAsStaged.
r2 := block add: r0 to: r1 ].
cfg initialBasicBlock jumpTo: topBasicBlock.
Expand All @@ -272,9 +270,7 @@ DRLinearScanRegisterAllocatorTest >> testDoNotInsertCopyForStagedNonMovableConst
cfg := DRControlFlowGraph new.

topBasicBlock := cfg newBasicBlockWith: [ :block |
r0 := block copy:
(DRInterpreterReference objectReference:
'methodObj').
r0 := block copy: DRInterpreterReference methodObj.
r1 := (block copy: 2) markAsStaged.
r2 := block add: r1 to: r0 ].
cfg initialBasicBlock jumpTo: topBasicBlock.
Expand All @@ -283,7 +279,7 @@ DRLinearScanRegisterAllocatorTest >> testDoNotInsertCopyForStagedNonMovableConst

firstGeneratedCopy := topBasicBlock instructions allButLast last.
self assert: firstGeneratedCopy isAdd.

"Here we don't have the choice: when transforming to 2AC the first operand should be extracted in a copy"
self assert: firstGeneratedCopy operand1 isCopy.
self assert: firstGeneratedCopy operand1 operand1 isStaged
Expand Down
8 changes: 6 additions & 2 deletions Druid-Tests/DRProductionBytecodeTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2903,7 +2903,7 @@ DRProductionBytecodeTest >> testStoreAndPopReceiverVariableBytecode: index [
{ #category : #tests }
DRProductionBytecodeTest >> testStoreAndPopReceiverVariableBytecodeCallImmutableTrampoline [

| object value immutableTrampoline bytecodePC |
| object value immutableTrampoline bytecodePC valueToStore |

cogit ceStoreCheckTrampoline: fakeTrampoline.
immutableTrampoline := self compile: [ cogit RetN: 0 ].
Expand Down Expand Up @@ -2939,7 +2939,11 @@ DRProductionBytecodeTest >> testStoreAndPopReceiverVariableBytecodeCallImmutable
self runFrom: cogInitialAddress until: immutableTrampoline.

self assert: machineSimulator receiverRegisterValue equals: bytecodePC.
self assert: machineSimulator smalltalkStackPointerValue equals: value

valueToStore := machineSimulator hasLinkRegister
ifTrue: [ machineSimulator smalltalkStackPointerValue ]
ifFalse: [ machineSimulator smalltalkStackPointerValueAt: 1 ].
self assert: valueToStore equals: value
]

{ #category : #tests }
Expand Down
Loading

0 comments on commit 04e022f

Please sign in to comment.