-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add stack size calculation to code object to pre-allocate value stack #1327
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c810fb5
Add stack size calculation to code object to pre-allocate value stack…
windelbouwman 8d782b1
Merge branch 'master' into stack-size-calculation
coolreader18 7239229
Merge branch 'master' of github.com:RustPython/RustPython into stack-…
windelbouwman e2b6961
Merge branch 'master' of github.com:RustPython/RustPython into stack-…
windelbouwman 7a61401
Fix stack effect calculation to exclude the unpack opcode which was …
windelbouwman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use rustpython_bytecode::bytecode::{CallType, Instruction}; | ||
|
||
/// Determine the effect on the stack of the given instruction. | ||
/// | ||
/// This function must match what is executed in frame.rs | ||
/// The return value is the amount of stack change created by the | ||
/// given opcode. | ||
pub fn stack_effect(instruction: &Instruction) -> isize { | ||
use Instruction::*; | ||
|
||
match instruction { | ||
ImportStar => -1, | ||
Import { .. } => 1, | ||
ImportFrom { .. } => 1, | ||
PopException => 0, | ||
Jump { .. } => 0, | ||
JumpIfFalse { .. } => -1, | ||
JumpIfTrue { .. } => -1, | ||
JumpIfFalseOrPop { .. } => -1, | ||
JumpIfTrueOrPop { .. } => -1, | ||
Break => 0, | ||
Continue => 0, | ||
PopBlock => 0, | ||
GetIter => 0, | ||
ForIter { .. } => 1, | ||
CallFunction { typ } => match typ { | ||
CallType::Positional(amount) => -(*amount as isize), | ||
CallType::Keyword(amount) => -1 - (*amount as isize), | ||
CallType::Ex(has_kwargs) => { | ||
if *has_kwargs { | ||
-2 | ||
} else { | ||
-1 | ||
} | ||
} | ||
}, | ||
UnaryOperation { .. } => 0, | ||
BinaryOperation { .. } => -1, | ||
Pop => -1, | ||
Duplicate => 1, | ||
Rotate { .. } => 0, | ||
Reverse { .. } => 0, | ||
Subscript { .. } => -1, | ||
StoreSubscript { .. } => -3, | ||
DeleteSubscript { .. } => -2, | ||
LoadAttr { .. } => 0, | ||
StoreAttr { .. } => -2, | ||
DeleteAttr { .. } => -1, | ||
LoadName { .. } => 1, | ||
StoreName { .. } => -1, | ||
DeleteName { .. } => 0, | ||
LoadConst { .. } => 1, | ||
ReturnValue { .. } => -1, | ||
YieldValue { .. } => 0, | ||
YieldFrom { .. } => 0, | ||
CompareOperation { .. } => -1, | ||
BuildList { size, .. } | ||
| BuildSet { size, .. } | ||
| BuildTuple { size, .. } | ||
| BuildSlice { size, .. } | ||
| BuildString { size } => 1 - (*size as isize), | ||
BuildMap { size, unpack } => { | ||
let size = *size as isize; | ||
if *unpack { | ||
1 - size | ||
} else { | ||
1 - size * 2 | ||
} | ||
} | ||
ListAppend { .. } => -1, | ||
SetAdd { .. } => -1, | ||
MapAdd { .. } => -2, | ||
UnpackEx { before, after } => -1 + (*before as isize) + (*after as isize) + 1, | ||
UnpackSequence { size } => -1 + (*size as isize), | ||
SetupLoop { .. } => 0, | ||
SetupWith { .. } => 0, | ||
CleanupWith { .. } => 0, | ||
SetupExcept { .. } => 0, | ||
SetupFinally { .. } => 0, | ||
EnterFinally { .. } => 0, | ||
EndFinally { .. } => 0, | ||
LoadBuildClass => 1, | ||
MakeFunction { .. } => 0, | ||
Raise { argc } => -(*argc as isize), | ||
PrintExpr => -1, | ||
FormatValue { .. } => 0, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could get rid of the properties on
CodeObjectStream
and just mutatemax_stack_size
onCodeObject
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. The thing is, I would like to gather all instructions in the stream, and in the end, create the immutable codeobject. This makes the code easier to understand in my opinion. That is the reason why I implemented the code like it is.