Skip to content

Specialize primitiveAt based on format #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Druid-Tests/DRDruidTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Class {
#name : #DRDruidTest,
#superclass : #VMJittedPrimitivesTest,
#instVars : [
'fakeTrampoline'
'fakeTrampoline',
'compilerCompilerClass'
],
#category : #'Druid-Tests'
}
Expand Down Expand Up @@ -43,6 +44,7 @@ DRDruidTest >> jitOptions [
DRDruidTest >> setUp [

super setUp.
compilerCompilerClass := DRPrimitiveCompilerCompiler.

cogit methodZone
setInterpreter: interpreter
Expand Down
6 changes: 3 additions & 3 deletions Druid-Tests/DRPrimitiveCompilationTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ DRPrimitiveCompilationTest >> compileDruidPrimitive: aSelector with: aBlock [
]

{ #category : #'helpers-compiling' }
DRPrimitiveCompilationTest >> compileDruidPrimitiveGenerator: aSelector with: aBlock [
DRPrimitiveCompilationTest >> compileDruidPrimitiveGenerator: aSelector with: aBlockCompilerConfig [

| generatorSelector compiler |
generatorSelector := (#gen , '_' , aSelector) asSymbol.

"First generate druid code"
compiler := DRPrimitiveCompilerCompiler new
compiler := compilerCompilerClass new
primitiveName: aSelector;
interpreter: self interpreter;
primitiveCompilerName: generatorSelector;
perform: cogitStyle;
configureForCompilerClass: compilerClass.

aBlock value: compiler.
aBlockCompilerConfig value: compiler.
compiler compile.
^ compiler
]
Expand Down
19 changes: 19 additions & 0 deletions Druid-Tests/DRProductionPrimitiveCompilationTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ DRProductionPrimitiveCompilationTest >> testCompilePrimitiveAtOnNonArray [
withArguments: { (memory integerObjectOf: 1) }
]

{ #category : #'tests-real primitives' }
DRProductionPrimitiveCompilationTest >> testCompilePrimitiveAtSpecialized [

| class array |

compilerCompilerClass := DRSpecialPrimitiveCompilerCompiler.
self compileDruidPrimitive: #primitiveAt.

class := self newClassInOldSpaceWithSlots: 0 instSpec: memory arrayFormat.
array := memory instantiateClass: class indexableSize: 1.
memory storePointer: 0 ofObject: array withValue: 42.

self executePrimitiveWithReceiver: array
withArguments: { memory integerObjectOf: 1 }.

self assert: machineSimulator receiverRegisterValue equals: 42

]

{ #category : #'tests-real primitives' }
DRProductionPrimitiveCompilationTest >> testCompilePrimitiveClass [

Expand Down
150 changes: 87 additions & 63 deletions Druid-Tests/DruidTestRTLCompiler.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2185,7 +2185,7 @@ DruidTestRTLCompiler >> gen_primitiveAt [
self MoveR: Extra3Reg Mw: 16 r: SPReg.
self MoveMw: 16 r: SPReg R: Extra3Reg.
self MoveR: Extra3Reg R: Extra0Reg.
self ArithmeticShiftRightCq: 24 R: Extra0Reg.
self LogicalShiftRightCq: 24 R: Extra0Reg.
self AndCq: 31 R: Extra0Reg.
self MoveMw: 0 r: SPReg R: Extra3Reg.
self MoveMb: 7 r: Extra3Reg R: Extra2Reg.
Expand All @@ -2195,7 +2195,7 @@ DruidTestRTLCompiler >> gen_primitiveAt [
self MoveMw: 0 r: SPReg R: Extra3Reg.
self MoveM64: -8 r: Extra3Reg R: Extra2Reg.
self LogicalShiftLeftCq: 8 R: Extra2Reg.
self ArithmeticShiftRightCq: 8 R: Extra2Reg.
self LogicalShiftRightCq: 8 R: Extra2Reg.
jump4 := self Jump: 0.
currentBlock := self Label.
jump2 jmpTarget: currentBlock.
Expand Down Expand Up @@ -2331,7 +2331,7 @@ DruidTestRTLCompiler >> gen_primitiveAt [
jump5 jmpTarget: currentBlock.
jump6 jmpTarget: currentBlock.
self MoveR: SendNumArgsReg R: ClassReg.
self ArithmeticShiftRightCq: 10 R: ClassReg.
self LogicalShiftRightCq: 10 R: ClassReg.
s118 := objectMemory hiddenRootsObject.
self LogicalShiftLeftCq: 3 R: ClassReg.
self AddCq: s118 R: ClassReg.
Expand Down Expand Up @@ -2445,8 +2445,30 @@ DruidTestRTLCompiler >> gen_primitiveAt [
self LogicalShiftLeftCq: 3 R: Extra1Reg.
self AddCq: 1 R: Extra1Reg.
self MoveR: Extra1Reg R: ReceiverResultReg.
jump6 := self Jump: 0.
currentBlock := self Label.
jump4 jmpTarget: currentBlock.
self CmpCq: 12 R: Extra0Reg.
jump4 := self JumpBelow: 0.
self MoveMw: 8 r: SPReg R: Extra3Reg.
self SubCq: 1 R: Extra3Reg.
self MoveR: Extra3Reg Mw: 8 r: SPReg.
self MoveMw: 8 r: SPReg R: Extra3Reg.
self LogicalShiftLeftCq: 1 R: Extra3Reg.
self MoveR: Extra3Reg Mw: 8 r: SPReg.
self MoveMw: 0 r: SPReg R: Extra3Reg.
self MoveMw: 8 r: SPReg R: Extra4Reg.
self AddR: Extra4Reg R: Extra3Reg.
self MoveR: Extra3Reg Mw: 0 r: SPReg.
self MoveMw: 0 r: SPReg R: Extra3Reg.
self MoveM16: 8 r: Extra3Reg R: Extra1Reg.
self AndCq: 16rFFFF R: Extra1Reg.
self LogicalShiftLeftCq: 3 R: Extra1Reg.
self AddCq: 1 R: Extra1Reg.
self MoveR: Extra1Reg R: ReceiverResultReg.
currentBlock := self Label.
jump5 jmpTarget: currentBlock.
jump6 jmpTarget: currentBlock.
self AddCq: 24 R: SPReg.
self genPrimReturn.
currentBlock := self Label.
Expand Down Expand Up @@ -2662,67 +2684,69 @@ DruidTestRTLCompiler >> gen_primitiveCaseOfOtherwiseValue [
DruidTestRTLCompiler >> gen_primitiveClass [
"AutoGenerated by Druid"

| s27 jump5 jump3 jump1 s2 currentBlock jump4 jump2 s3 |
| s3 s26 currentBlock s2 |
s2 := self methodNumArgs.
s2 > 0 ifTrue: [ ^ CompletePrimitive ].
self MoveR: ReceiverResultReg R: ClassReg.
self MoveR: ClassReg R: SendNumArgsReg.
self AndCq: 7 R: SendNumArgsReg.
self CmpCq: 0 R: SendNumArgsReg.
jump1 := self JumpZero: 0.
self MoveAw: objectMemory hiddenRootsObject + 8 R: ClassReg.
self LogicalShiftLeftCq: 3 R: SendNumArgsReg.
self AddR: SendNumArgsReg R: ClassReg.
self MoveM64: 8 r: ClassReg R: ClassReg.
self MoveR: ClassReg R: ReceiverResultReg.
jump2 := self Jump: 0.
currentBlock := self Label.
jump1 jmpTarget: currentBlock.
self MoveM64: 0 r: ClassReg R: SendNumArgsReg.
self AndCq: 16r3FFFFF R: SendNumArgsReg.
self CmpCq: 31 R: SendNumArgsReg.
jump1 := self JumpAbove: 0.
self CmpCq: 31 R: SendNumArgsReg.
jump3 := self JumpNonZero: 0.
self MoveR: ClassReg R: SendNumArgsReg.
jump4 := self Jump: 0.
currentBlock := self Label.
jump3 jmpTarget: currentBlock.
self CmpCq: 8 R: SendNumArgsReg.
jump3 := self JumpNonZero: 0.
self genMoveConstant: objectMemory nilObject R: SendNumArgsReg.
jump5 := self Jump: 0.
currentBlock := self Label.
jump1 jmpTarget: currentBlock.
jump3 jmpTarget: currentBlock.
self MoveR: SendNumArgsReg R: ClassReg.
self ArithmeticShiftRightCq: 10 R: ClassReg.
s27 := objectMemory hiddenRootsObject.
self LogicalShiftLeftCq: 3 R: ClassReg.
self AddCq: s27 R: ClassReg.
self MoveM64: 8 r: ClassReg R: ClassReg.
self genMoveConstant: objectMemory nilObject R: Extra0Reg.
self CmpR: Extra0Reg R: ClassReg.
jump3 := self JumpNonZero: 0.
self genMoveConstant: objectMemory nilObject R: Extra0Reg.
jump1 := self Jump: 0.
currentBlock := self Label.
jump3 jmpTarget: currentBlock.
self AndCq: 1023 R: SendNumArgsReg.
self LogicalShiftLeftCq: 3 R: SendNumArgsReg.
self AddR: SendNumArgsReg R: ClassReg.
self MoveM64: 8 r: ClassReg R: Extra0Reg.
currentBlock := self Label.
jump1 jmpTarget: currentBlock.
self MoveR: Extra0Reg R: SendNumArgsReg.
currentBlock := self Label.
jump4 jmpTarget: currentBlock.
jump5 jmpTarget: currentBlock.
self MoveR: SendNumArgsReg R: ReceiverResultReg.
currentBlock := self Label.
jump2 jmpTarget: currentBlock.
self genPrimReturn.
^ CompletePrimitive
s2 = 0 ifTrue: [
| jump5 jump3 jump1 jump4 jump2 |
self MoveR: ReceiverResultReg R: ClassReg.
self MoveR: ClassReg R: SendNumArgsReg.
self AndCq: 7 R: SendNumArgsReg.
self CmpCq: 0 R: SendNumArgsReg.
jump1 := self JumpZero: 0.
self MoveAw: objectMemory hiddenRootsObject + 8 R: ClassReg.
self LogicalShiftLeftCq: 3 R: SendNumArgsReg.
self AddR: SendNumArgsReg R: ClassReg.
self MoveM64: 8 r: ClassReg R: ClassReg.
self MoveR: ClassReg R: ReceiverResultReg.
jump2 := self Jump: 0.
currentBlock := self Label.
jump1 jmpTarget: currentBlock.
self MoveM64: 0 r: ClassReg R: SendNumArgsReg.
self AndCq: 16r3FFFFF R: SendNumArgsReg.
self CmpCq: 31 R: SendNumArgsReg.
jump1 := self JumpAbove: 0.
self CmpCq: 31 R: SendNumArgsReg.
jump3 := self JumpNonZero: 0.
self MoveR: ClassReg R: SendNumArgsReg.
jump4 := self Jump: 0.
currentBlock := self Label.
jump3 jmpTarget: currentBlock.
self CmpCq: 8 R: SendNumArgsReg.
jump3 := self JumpNonZero: 0.
self genMoveConstant: objectMemory nilObject R: SendNumArgsReg.
jump5 := self Jump: 0.
currentBlock := self Label.
jump1 jmpTarget: currentBlock.
jump3 jmpTarget: currentBlock.
self MoveR: SendNumArgsReg R: ClassReg.
self LogicalShiftRightCq: 10 R: ClassReg.
s26 := objectMemory hiddenRootsObject.
self LogicalShiftLeftCq: 3 R: ClassReg.
self AddCq: s26 R: ClassReg.
self MoveM64: 8 r: ClassReg R: ClassReg.
self genMoveConstant: objectMemory nilObject R: Extra0Reg.
self CmpR: Extra0Reg R: ClassReg.
jump3 := self JumpNonZero: 0.
self genMoveConstant: objectMemory nilObject R: Extra0Reg.
jump1 := self Jump: 0.
currentBlock := self Label.
jump3 jmpTarget: currentBlock.
self AndCq: 1023 R: SendNumArgsReg.
self LogicalShiftLeftCq: 3 R: SendNumArgsReg.
self AddR: SendNumArgsReg R: ClassReg.
self MoveM64: 8 r: ClassReg R: Extra0Reg.
currentBlock := self Label.
jump1 jmpTarget: currentBlock.
self MoveR: Extra0Reg R: SendNumArgsReg.
currentBlock := self Label.
jump4 jmpTarget: currentBlock.
jump5 jmpTarget: currentBlock.
self MoveR: SendNumArgsReg R: ReceiverResultReg.
currentBlock := self Label.
jump2 jmpTarget: currentBlock.
self genPrimReturn.
^ 0 ].
^ 0
]

{ #category : #generated }
Expand Down
25 changes: 25 additions & 0 deletions Druid/DRSpecialPrimitiveCompilerCompiler.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Class {
#name : #DRSpecialPrimitiveCompilerCompiler,
#superclass : #DRPrimitiveCompilerCompiler,
#category : #'Druid-CompilerCompiler'
}

{ #category : #'generation-IR' }
DRSpecialPrimitiveCompilerCompiler >> configureIRBuilder: generator forMethod: method [

super configureIRBuilder: generator forMethod: method.

generator specializeForReceiverFormat: (self specializedFormat: method)
]

{ #category : #'generation-IR' }
DRSpecialPrimitiveCompilerCompiler >> newIRGenerator [

^ DRSpecializedPrimitiveIRGenerator new
]

{ #category : #query }
DRSpecialPrimitiveCompilerCompiler >> specializedFormat: method [

^ (method ast pragmaNamed: #specilizeFormat:) arguments first value
]
31 changes: 31 additions & 0 deletions Druid/DRSpecializedPrimitiveIRGenerator.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Class {
#name : #DRSpecializedPrimitiveIRGenerator,
#superclass : #DRPrimitiveIRGenerator,
#instVars : [
'specializedFormat'
],
#category : #'Druid-CompilerCompiler'
}

{ #category : #specialization }
DRSpecializedPrimitiveIRGenerator >> interpretFormatOfHeaderWith: aRBMessageNode [

| headerInstruction loadInstruction obj |
headerInstruction := self visitOperand: aRBMessageNode arguments first.
loadInstruction := headerInstruction simpleConstantFold.

self assert: loadInstruction isLoad.

obj := loadInstruction address expression simpleConstantFold.

obj isLoadReceiver ifFalse: [ 1 halt "?" ].

self pushOperand: specializedFormat asDRValue
]

{ #category : #specialization }
DRSpecializedPrimitiveIRGenerator >> specializeForReceiverFormat: aFormat [

specializedFormat := aFormat.
specialCases at: #formatOfHeader: put: #interpretFormatOfHeaderWith:
]