Skip to content

Commit

Permalink
Add a C code generator test for inlinings which has a shift right in …
Browse files Browse the repository at this point in the history
…the left side.

Add a fix (thanks to Guille) which correctly generates the C AST expression.
  • Loading branch information
Hernán Morales Durand committed Aug 7, 2023
1 parent 4f8e2a4 commit 4bf4282
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
30 changes: 30 additions & 0 deletions smalltalksrc/Slang-Tests/SlangBasicTranslationTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,36 @@ SlangBasicTranslationTest >> testGoto [
self assert: translation equals: 'goto lab'
]
{ #category : #'tests-inlinemethod' }
SlangBasicTranslationTest >> testInlineMethodIfExpressionWithShiftRight [
| translation codeGenerator inlinedMethod cast printedString |
translation := (self getTMethodFrom: #methodToBeTranslatedWithIfAndShiftRight).
inlinedMethod := ((SlangBasicTranslationTestClass >> #methodWithIfAndShiftRight:) asTranslationMethodOfClass: TMethod).
codeGenerator := CCodeGeneratorGlobalStructure new.
codeGenerator
addMethod: translation;
addMethod: inlinedMethod;
doInlining: true.
cast := translation asCASTIn: codeGenerator.
printedString := String streamContents: [ :str | cast prettyPrintOn: str ].
self assert: cast isCompoundStatement.
self assert: printedString equals: '/* SlangBasicTranslationTestClass>>#methodToBeTranslatedWithIfAndShiftRight */
static sqInt
methodToBeTranslatedWithIfAndShiftRight(void)
{
/* begin methodWithIfAndShiftRight: */
((usqInt) (((2 < 0)
? 0
: 2)) ) >> ((2 - 1) * 32);
return 0;
}
'.
]
{ #category : #'tests-inlinenode' }
SlangBasicTranslationTest >> testInlineNodeDoesNotInitializeReadBeforeWrittenArrayTemp [
Expand Down
18 changes: 16 additions & 2 deletions smalltalksrc/Slang-Tests/SlangBasicTranslationTestClass.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ SlangBasicTranslationTestClass >> methodDefiningSingleVariable [
| foo |
]

{ #category : #inline }
SlangBasicTranslationTestClass >> methodToBeTranslatedWithIfAndShiftRight [

self methodWithIfAndShiftRight: 2
]

{ #category : #inline }
SlangBasicTranslationTestClass >> methodUsingSingleArrayVariable [

Expand Down Expand Up @@ -126,6 +132,14 @@ SlangBasicTranslationTestClass >> methodWithAnOptionPragma [

]

{ #category : #inline }
SlangBasicTranslationTestClass >> methodWithIfAndShiftRight: var [

^ (var < 0
ifTrue: [ 0 ]
ifFalse: [ var ]) >> (var - 1 * 32)
]

{ #category : #inline }
SlangBasicTranslationTestClass >> methodWithInlinePragma [

Expand Down Expand Up @@ -165,13 +179,13 @@ SlangBasicTranslationTestClass >> methodWithOptionPragma [

]

{ #category : #'as yet unclassified' }
{ #category : #inline }
SlangBasicTranslationTestClass >> methodWithRepeatedCFunctionName [

^ true
]

{ #category : #'as yet unclassified' }
{ #category : #inline }
SlangBasicTranslationTestClass >> methodWithRepeatedCFunctionName: arg1 [

^ true
Expand Down
2 changes: 1 addition & 1 deletion smalltalksrc/Slang/CCodeGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2585,7 +2585,7 @@ CCodeGenerator >> generateCASTShiftLeft: tast [
CCodeGenerator >> generateCASTShiftRight: tast [
| type rcvr |
rcvr := tast receiver asCASTIn: self.
rcvr := tast receiver asCASTExpressionIn: self.
(self
is64BitIntegralVariable: tast receiver
typeInto: [ :t | type := t ])
Expand Down

0 comments on commit 4bf4282

Please sign in to comment.