- Notifications
You must be signed in to change notification settings - Fork1.4k
Updateopcode from 3.13.7#6156
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
coderabbitaibot commentedSep 17, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| Self::is_valid(opcode) | ||
| && matches!( | ||
| opcode, | ||
| 63 | 66 |
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.
where the magic numbers came from? will this be changed if we change instruction enum?
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.
Oh, I just ran:
importopcodefornamein ("hasarg","hasconst","hasname","hasjump","hasjrel","hasjabs","hasfree","haslocal","hasexc",):lst=getattr(opcode,name)print(f"{name}\n{lst}")
And copied the results xD
But it's calculated based onhttps://github.com/python/cpython/blob/bcee1c322115c581da27600f2ae55e5439c027eb/Include/internal/pycore_opcode_metadata.h#L966-L1190
I've put a comment athttps://github.com/ShaharNaveh/RustPython/blob/a7b9356da2a85cbc02df1f0fac9eda6a26869b6d/stdlib/src/opcode.rs#L46-L47 stating this:)
We should have our own metadata table, but I think that's a bit out of scope for this PR
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.
At
RustPython/compiler/core/src/bytecode.rs
Lines 534 to 541 inaa0eb4b
| #[derive(Debug,Copy,Clone,PartialEq,Eq)] | |
| #[repr(u8)] | |
| pubenumInstruction{ | |
| Nop, | |
| /// Importing by name | |
| ImportName{ | |
| idx:Arg<NameIdx>, | |
| }, |
I tried doing something like:
#[repr(u8)]pubenumInstruction{Nop =30,ImportName{idx:Arg<NameIdx>,} =75, ...}
Based on:
Nop:https://github.com/python/cpython/blob/bcee1c322115c581da27600f2ae55e5439c027eb/Include/opcode_ids.h#L43ImportName:https://github.com/python/cpython/blob/bcee1c322115c581da27600f2ae55e5439c027eb/Include/opcode_ids.h#L88
But this change alone was enough to break RustPython entirely.
@youknowone can you please point me to where any of:
RustPython/compiler/core/src/bytecode.rs
Lines 796 to 815 inaa0eb4b
| implFrom<Instruction>foru8{ | |
| #[inline] | |
| fnfrom(ins:Instruction) ->Self{ | |
| // SAFETY: there's no padding bits | |
| unsafe{ std::mem::transmute::<Instruction,Self>(ins)} | |
| } | |
| } | |
| implTryFrom<u8>forInstruction{ | |
| typeError =crate::marshal::MarshalError; | |
| #[inline] | |
| fntry_from(value:u8) ->Result<Self,crate::marshal::MarshalError>{ | |
| if value <= u8::from(LAST_INSTRUCTION){ | |
| Ok(unsafe{ std::mem::transmute::<u8,Self>(value)}) | |
| }else{ | |
| Err(crate::marshal::MarshalError::InvalidBytecode) | |
| } | |
| } | |
| } |
are being called? I've tried looking into it and got a bit lost
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.
Thank you for explanation. Since we occasionally break bytecode compatibility, the numbers need to be regenerated.
Could you also attach the updating code aroundenum Instruction not to let it be desynced?
TheFrom<Instruction> part is used form marshal. cc@coolreader18
ShaharNavehSep 22, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Since we occasionally break bytecode compatibility
Is there a reason for that? or it's something that's desired but not implemented yet?
the numbers need to be regenerated. Could you also attach the updating code around
enum Instructionnot to let it be desynced?
I haven't written any code to do that (yet, but was planning to). As I just changed those two enum members and I couldn't even docargo run :/
The
From<Instruction>part is used form marshal. cc@coolreader18
ty:) will look into it.
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.
@youknowone I have opened#6174 so we can discuss the next steps there and to not block this PR
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.
Is there a reason for that? or it's something that's desired but not implemented yet?
No specific reason. We just add or remove instructions by following CPython update. The order changing is unintended side effect. Python doesn't guarantee bytecode compatibiltity anyway.
Does giving specific numbers to each instruction make it easier to be maintained? I guess so.
After#6174, then can we rewrite63 | 66 toInstruction::Abc as u8 | Instruction::Def as u8 and so on?
youknowone commentedSep 20, 2025
finally we get |
| 'INSTRUMENTED_POP_JUMP_IF_FALSE': 251, | ||
| 'INSTRUMENTED_POP_JUMP_IF_NONE': 252, | ||
| 'INSTRUMENTED_POP_JUMP_IF_NOT_NONE': 253, | ||
| 'JUMP': 256, |
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 didn't know it can be over 256, huh
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.
Those are pseudo opcodes:)
Real opcodes can't be more than 256
| Self::is_valid(opcode) | ||
| && matches!( | ||
| opcode, | ||
| 63 | 66 |
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.
Is there a reason for that? or it's something that's desired but not implemented yet?
No specific reason. We just add or remove instructions by following CPython update. The order changing is unintended side effect. Python doesn't guarantee bytecode compatibiltity anyway.
Does giving specific numbers to each instruction make it easier to be maintained? I guess so.
After#6174, then can we rewrite63 | 66 toInstruction::Abc as u8 | Instruction::Def as u8 and so on?
ShaharNaveh commentedSep 25, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
For some reason I can't comment on the thread of#6156 (comment) so answering it here:
Yes! it also makes it easier for writing the helpers of:
but that wouldn't be needed as I intend#6174 to generate those automatically (just like CPython does it)
Well, yes you could. but I thought that we could have an API that looks similar to this: // This is inside the `_opcode` modulefnhas_arg(opcode:i32) ->bool{Instruction::try_from(opcode).map_or(false, |v| !v.is_pseudo() && v.has_arg())} where:
are all auto generated. |
youknowone left a comment
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.
Looks good in general.
I am worrying about the newly introduced magic numbers. I wish it could be quickly resolved in next patch
3a6fda4 intoRustPython:mainUh oh!
There was an error while loading.Please reload this page.
ShaharNaveh commentedOct 5, 2025
I'll send a quick patch to resolve it in the next couple of days |
No description provided.