Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate if locals/args exist when adding a type declaration #603

Merged
12 changes: 12 additions & 0 deletions smalltalksrc/Slang-Tests/SLBasicTestDeclarationClass.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Class {
#category : #'Slang-Tests'
}

{ #category : #translation }
SLBasicTestDeclarationClass class >> typeForSelf [

^#implicit
]
Comment on lines +7 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? Is it not always implicit (except for structs)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inherited behaviour from SlangClass is to return nil. Or are you saying that at some point that nil defaults to #implicit?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just asking for understanding, because this definition is in the tests, but maybe could be in a more general place? I don't know.
Anyway, it is out of the scope of this PR, so we can continue with our life.


{ #category : #'as yet unclassified' }
SLBasicTestDeclarationClass >> methodWithBlockLocalDeclaration [

Expand Down Expand Up @@ -31,3 +37,9 @@ SLBasicTestDeclarationClass >> methodWithNonExistingLocalDeclaration [
"var does not exist"
<var: 'var' type: 'toto'>
]

{ #category : #'as yet unclassified' }
SLBasicTestDeclarationClass >> methodWithUndefinedLocal [

<var: 'var' type: 'toto'>
]
ivojawer marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions smalltalksrc/Slang-Tests/SLTestDeclarations.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ SLTestDeclarations >> setUp [
ccg addClass: SLBasicTestDeclarationClass
]

{ #category : #tests }
SLTestDeclarations >> testAddTypeForSelfWithDeclarationsDoesNotThrowError [

| method |
method := ccg methodNamed: #methodWithUndefinedLocal.
self shouldnt: [ method addTypeForSelf ] raise: Error
PalumboN marked this conversation as resolved.
Show resolved Hide resolved
]

{ #category : #tests }
SLTestDeclarations >> testAllLocalsReturnsBlockLocals [

Expand Down Expand Up @@ -69,3 +77,11 @@ SLTestDeclarations >> testLocalsReturnsDirectLocals [
assertCollection: (ccg methodNamed: #methodWithLocal) locals
hasSameElements: #( 'var' )
]

{ #category : #tests }
SLTestDeclarations >> testUndefinedLocalWithDeclarationsThrowsError [

| method |
method := ccg methodNamed: #methodWithUndefinedLocal.
self should: [ method recordDeclarationsIn: ccg ] raise: Error
]
40 changes: 32 additions & 8 deletions smalltalksrc/Slang/TMethod.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ TMethod >> addLocal: aString [
self locals add: aString
]

{ #category : #adding }
TMethod >> addLocals: locals [
locals do: [ :local | self addLocal: local ]
]

{ #category : #initialization }
TMethod >> addTypeForSelf [
"If self should be typed then add a suitable type declaration.
Expand All @@ -109,10 +114,12 @@ TMethod >> addTypeForSelf [
TMethod >> addVarsDeclarationsAndLabelsOf: methodToBeInlined except: doNotRename [
"Prepare to inline the body of the given method into the receiver by making the args and locals of the argument to the receiver be locals of the receiver. Record any type declarations for these variables. Record labels. Assumes that the variables have already be renamed to avoid name clashes."

self locals
addAll: (methodToBeInlined args reject: [ :v | doNotRename includes: v]);
addAll: (methodToBeInlined locals reject: [ :v | doNotRename includes: v]);
yourself.
| newLocals |

newLocals := methodToBeInlined args , methodToBeInlined allLocals asOrderedCollection .

self addLocals: (newLocals reject: [ :v | doNotRename includes: v]).
PalumboN marked this conversation as resolved.
Show resolved Hide resolved

methodToBeInlined declarations keysAndValuesDo:
[ :v :decl |
(doNotRename includes: v) ifFalse:
Expand Down Expand Up @@ -651,9 +658,17 @@ TMethod >> declarationAt: aVariableName ifPresent: presentBlock [
]

{ #category : #accessing }
TMethod >> declarationAt: aVariableName "<String>" put: aDeclaration [ "<String>" "^aDeclaration"
TMethod >> declarationAt: aVariableName put: aDeclaration [

^declarations at: aVariableName put: aDeclaration
| isRelevantVariable |
isRelevantVariable := (aVariableName beginsWithAnyOf:
#( #self , #cascade, #toDoLimit )) not.

parseTree notNil and: [
guillep marked this conversation as resolved.
Show resolved Hide resolved
isRelevantVariable and: [
(self declaresVariableWithName: aVariableName ) not ifTrue: [
self error: 'Undefined local type declaration' ] ] ].
^ declarations at: aVariableName put: aDeclaration
]

{ #category : #accessing }
Expand All @@ -679,6 +694,15 @@ TMethod >> declareNonConflictingLocalNamedLike: aString [
^ newVarName
]

{ #category : #testing }
TMethod >> declaresVariableWithName: aVariableName [

| referencesInScope |
referencesInScope := self allLocals , self args.

^ referencesInScope includes: aVariableName
]

{ #category : #testing }
TMethod >> definedAsComplexMacro [
^properties notNil and: [(properties includesKey: #cmacro:)]
Expand Down Expand Up @@ -2784,7 +2808,7 @@ TMethod >> setSelector: sel definingClass: class args: argList locals: localList
definingClass := class.
args := argList asOrderedCollection collect: [:arg | arg name].
declarations := Dictionary new.
self addTypeForSelf.

primitive := aNumber.
properties := methodProperties.
comment := aComment.
Expand All @@ -2797,7 +2821,7 @@ TMethod >> setSelector: sel definingClass: class args: argList locals: localList

self parseTree: (aBlockNode asTranslatorNodeIn: self).
self locals: tempParseTree locals, (localList collect: [:arg | arg name]).

self addTypeForSelf.

complete := false. "set to true when all possible inlining has been done"
export := self extractExportDirective.
Expand Down
16 changes: 8 additions & 8 deletions smalltalksrc/VMMaker/Cogit.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11762,15 +11762,15 @@ Cogit >> targetMethodAndSendTableFor: entryPoint annotation: annotation into: bi
"Evaluate binaryBlock with the targetMethod and relevant send table for a linked-send
to entryPoint. Do so based on the alignment of entryPoint."

<var: #targetMethod type: #'CogMethod *'>

self offsetAndSendTableFor: entryPoint
self
offsetAndSendTableFor: entryPoint
annotation: annotation
into: [:offset :sendTable| | targetCogCode |
targetCogCode := self cCoerceSimple: entryPoint - offset to: #'CogMethod *'.
binaryBlock
value: targetCogCode
value: sendTable]
into: [ :offset :sendTable |
| targetCogCode |
targetCogCode := self
cCoerceSimple: entryPoint - offset
to: #'CogMethod *'.
binaryBlock value: targetCogCode value: sendTable ]
]

{ #category : #debugging }
Expand Down
1 change: 0 additions & 1 deletion smalltalksrc/VMMaker/SpurMemoryManager.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11544,7 +11544,6 @@ SpurMemoryManager >> setMaxOldSpaceSize: limit [
SpurMemoryManager >> setMinimalPermSpaceSize: min [

<api>
<var: #limit type: #usqInt>
guillep marked this conversation as resolved.
Show resolved Hide resolved

self ensureMemoryMap.
self getMemoryMap minPermSpaceSize: min.
Expand Down
17 changes: 7 additions & 10 deletions smalltalksrc/VMMaker/StackInterpreter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -10967,22 +10967,20 @@ StackInterpreter >> primitiveExitCriticalSection [
{ #category : #'simd primitives' }
StackInterpreter >> primitiveFloat64ArrayAdd [

<var: #rcvr type: #'double *'>
<var: #arg type: #'double *'>
| arg1 arg2 res arraySize |
arg1 := self stackValue: 2.
arg2 := self stackValue: 1.
res := self stackTop.
arraySize := objectMemory numSlotsOf: res.

(objectMemory isSixtyFourBitIndexableOop: arg1)
ifFalse: [ ^ self primitiveFailFor: PrimErrBadArgument ].
(objectMemory isSixtyFourBitIndexableOop: arg2)
ifFalse: [ ^ self primitiveFailFor: PrimErrBadArgument ].
(objectMemory isSixtyFourBitIndexableOop: res)
ifFalse: [ ^ self primitiveFailFor: PrimErrBadArgument ].
(objectMemory isSixtyFourBitIndexableOop: arg1) ifFalse: [
^ self primitiveFailFor: PrimErrBadArgument ].
(objectMemory isSixtyFourBitIndexableOop: arg2) ifFalse: [
^ self primitiveFailFor: PrimErrBadArgument ].
(objectMemory isSixtyFourBitIndexableOop: res) ifFalse: [
^ self primitiveFailFor: PrimErrBadArgument ].

0 to: arraySize - 1 do: [ :i |
0 to: arraySize - 1 do: [ :i |
| tmp |
tmp := (objectMemory fetchFloat64: i ofObject: arg1)
+ (objectMemory fetchFloat64: i ofObject: arg2).
Expand Down Expand Up @@ -12954,7 +12952,6 @@ StackInterpreter >> reapAndResetErrorCodeTo: theFP header: methodHeader [
and if so, assign it through theFP into the correct temporary variable.
Then zero the primFailCode. This is infrequent code, so keep it out of the common path."

<var: 'theSP' type: #'char *'>
<inline: #never>
| initialPC |
self assert: primFailCode ~= 0.
Expand Down
4 changes: 2 additions & 2 deletions smalltalksrc/VMMaker/StackToRegisterMappingCogit.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -1341,14 +1341,14 @@ StackToRegisterMappingCogit >> fixupAtIndex: index [
StackToRegisterMappingCogit >> freeAnyFloatRegNotConflictingWith: regMask [
"Spill the closest register on stack not conflicting with regMask.
Assertion Failure if regMask has already all the registers"
<var: #desc type: #'CogSimStackEntry *'>

| reg index |
self assert: needsFrame.
reg := NoReg.
index := simSpillBase max: 0.
self deny: reg = NoReg.
self ssAllocateRequiredFloatReg: reg.
^reg
^ reg
]

{ #category : #'simulation stack' }
Expand Down