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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16927-todo-optimization-evaluates-argument-before-receiver #16956

Open
wants to merge 1 commit into
base: Pharo13
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions src/OpalCompiler-Core/OCASTTranslator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ OCASTTranslator >> emitToDo: aMessageNode step: step [
block := aMessageNode arguments last.
iterator := block arguments first variable.


"evaluate the receiver first"
self visitNode: aMessageNode receiver.
iterator emitStore: methodBuilder.

"now the limit argument"
limitEmit := [self visitNode: limit].
"if the limit is not just a literal or a non-writable variable, make a temp store it there"
(limit isLiteralNode or: [limit isVariable and: [limit variable isWritable not]]) ifFalse: [
Expand All @@ -381,8 +387,7 @@ OCASTTranslator >> emitToDo: aMessageNode step: step [
limitEmit := [methodBuilder pushTemp: ('0limit_', iterator name)]].

"push start. allocate and initialize iterator"
self visitNode: aMessageNode receiver.
iterator emitStore: methodBuilder.

methodBuilder jumpBackTarget: #start.
iterator emitValue: methodBuilder.
limitEmit value.
Expand Down
9 changes: 9 additions & 0 deletions src/OpalCompiler-Tests/OCCompilerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ OCCompilerTest >> testNoShadowing [
^nil ]
]

{ #category : 'tests' }
OCCompilerTest >> testOptimizedToDo [
| stream results |
results := OrderedCollection new.
stream := #( 1 5 ) readStream.
stream next to: stream next do: [ :i | results add: i ].
self assertCollection: results hasSameElements: #(1 2 3 4 5)
]

{ #category : 'tests - shadowing' }
OCCompilerTest >> testPseudoVariableShadowing [

Expand Down