-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Newtype var_num oparg
#7400
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
Newtype var_num oparg
#7400
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ use cranelift::prelude::*; | |
| use num_traits::cast::ToPrimitive; | ||
| use rustpython_compiler_core::bytecode::{ | ||
| self, BinaryOperator, BorrowedConstant, CodeObject, ComparisonOperator, Instruction, | ||
| IntrinsicFunction1, Label, OpArg, OpArgState, | ||
| IntrinsicFunction1, Label, OpArg, OpArgState, oparg, | ||
| }; | ||
| use std::collections::HashMap; | ||
|
|
||
|
|
@@ -94,7 +94,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { | |
| let params = compiler.builder.func.dfg.block_params(entry_block).to_vec(); | ||
| for (i, (ty, val)) in arg_types.iter().zip(params).enumerate() { | ||
| compiler | ||
| .store_variable(i as u32, JitValue::from_type_and_value(ty.clone(), val)) | ||
| .store_variable( | ||
| (i as u32).into(), | ||
| JitValue::from_type_and_value(ty.clone(), val), | ||
| ) | ||
| .unwrap(); | ||
| } | ||
| compiler | ||
|
|
@@ -105,14 +108,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { | |
| self.stack.drain(stack_len - count..).collect() | ||
| } | ||
|
|
||
| fn store_variable( | ||
| &mut self, | ||
| idx: bytecode::NameIdx, | ||
| val: JitValue, | ||
| ) -> Result<(), JitCompileError> { | ||
| fn store_variable(&mut self, idx: oparg::VarNum, val: JitValue) -> Result<(), JitCompileError> { | ||
| let builder = &mut self.builder; | ||
| let ty = val.to_jit_type().ok_or(JitCompileError::NotSupported)?; | ||
| let local = self.variables[idx as usize].get_or_insert_with(|| { | ||
| let local = self.variables[idx].get_or_insert_with(|| { | ||
| let var = builder.declare_var(ty.to_cranelift()); | ||
|
Comment on lines
+111
to
115
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use checked local-slot access instead of panicking on bad bytecode. Line 114 and Line 651 index 🛠️ Suggested fix fn store_variable(&mut self, idx: oparg::VarNum, val: JitValue) -> Result<(), JitCompileError> {
let builder = &mut self.builder;
let ty = val.to_jit_type().ok_or(JitCompileError::NotSupported)?;
- let local = self.variables[idx].get_or_insert_with(|| {
+ let slot = self
+ .variables
+ .get_mut(idx.as_usize())
+ .ok_or(JitCompileError::BadBytecode)?;
+ let local = slot.get_or_insert_with(|| {
let var = builder.declare_var(ty.to_cranelift());
Local {
var,
ty: ty.clone(),
}
@@
Instruction::LoadFast { var_num } | Instruction::LoadFastBorrow { var_num } => {
- let local = self.variables[var_num.get(arg)]
- .as_ref()
+ let local = self
+ .variables
+ .get(var_num.get(arg).as_usize())
+ .and_then(Option::as_ref)
.ok_or(JitCompileError::BadBytecode)?;
self.stack.push(JitValue::from_type_and_value(
local.ty.clone(),
self.builder.use_var(local.var),
));Also applies to: 650-652 🤖 Prompt for AI Agents |
||
| Local { | ||
| var, | ||
|
|
@@ -649,7 +648,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { | |
| Ok(()) | ||
| } | ||
| Instruction::LoadFast { var_num } | Instruction::LoadFastBorrow { var_num } => { | ||
| let local = self.variables[var_num.get(arg) as usize] | ||
| let local = self.variables[var_num.get(arg)] | ||
| .as_ref() | ||
| .ok_or(JitCompileError::BadBytecode)?; | ||
| self.stack.push(JitValue::from_type_and_value( | ||
|
|
||
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.
Resolve
VarNumagainst the full locals-plus table.Line 1113 still assumes every
VarNumlives inmetadata.varnames, but Line 1797 createsLOAD_FASTslots asvarnames_len + ...when loweringLOAD_CLOSURE. That means displaying valid closure bytecode can walk pastvarnamesand panic. Please fall back intocellvars/freevarshere instead of indexing onlyvarnames.🛠️ Suggested fix
fn get_varname(&self, var_num: oparg::VarNum) -> &str { - self.metadata.varnames[var_num.as_usize()].as_ref() + let idx = var_num.as_usize(); + if let Some(name) = self.metadata.varnames.get_index(idx) { + name.as_ref() + } else { + let cell_idx = idx - self.metadata.varnames.len(); + self.metadata + .cellvars + .get_index(cell_idx) + .or_else(|| self.metadata.freevars.get_index(cell_idx - self.metadata.cellvars.len())) + .map(String::as_ref) + .unwrap_or("<invalid var num>") + } }🤖 Prompt for AI Agents