Skip to content

Instruction::Resume #5944

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

Merged
merged 2 commits into from
Jul 11, 2025
Merged
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
69 changes: 68 additions & 1 deletion compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,12 @@ impl Compiler<'_> {

if import_star {
// from .... import *
emit!(self, Instruction::ImportStar);
emit!(
self,
Instruction::CallIntrinsic1 {
func: bytecode::IntrinsicFunction1::ImportStar
}
);
} else {
// from mod import a, b as c

Expand Down Expand Up @@ -1556,6 +1561,14 @@ impl Compiler<'_> {
.constants
.insert_full(ConstantData::None);

// Emit RESUME instruction at function start
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AtFuncStart as u32
}
);

self.compile_statements(body)?;

// Emit None at end:
Expand Down Expand Up @@ -1971,6 +1984,12 @@ impl Compiler<'_> {
emit!(self, Instruction::GetAwaitable);
self.emit_load_const(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterAwait as u32
}
);
emit!(self, Instruction::SetupAsyncWith { end: final_block });
} else {
emit!(self, Instruction::SetupWith { end: final_block });
Expand Down Expand Up @@ -2012,6 +2031,12 @@ impl Compiler<'_> {
emit!(self, Instruction::GetAwaitable);
self.emit_load_const(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterAwait as u32
}
);
}

emit!(self, Instruction::WithCleanupFinish);
Expand Down Expand Up @@ -2050,6 +2075,12 @@ impl Compiler<'_> {
emit!(self, Instruction::GetANext);
self.emit_load_const(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterAwait as u32
}
);
self.compile_store(target)?;
emit!(self, Instruction::PopBlock);
} else {
Expand Down Expand Up @@ -3521,6 +3552,12 @@ impl Compiler<'_> {
Option::None => self.emit_load_const(ConstantData::None),
};
emit!(self, Instruction::YieldValue);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterYield as u32
}
);
}
Expr::Await(ExprAwait { value, .. }) => {
if self.ctx.func != FunctionContext::AsyncFunction {
Expand All @@ -3530,6 +3567,12 @@ impl Compiler<'_> {
emit!(self, Instruction::GetAwaitable);
self.emit_load_const(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterAwait as u32
}
);
}
Expr::YieldFrom(ExprYieldFrom { value, .. }) => {
match self.ctx.func {
Expand All @@ -3546,6 +3589,12 @@ impl Compiler<'_> {
emit!(self, Instruction::GetIter);
self.emit_load_const(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterYieldFrom as u32
}
);
}
Expr::Name(ExprName { id, .. }) => self.load_name(id.as_str())?,
Expr::Lambda(ExprLambda {
Expand Down Expand Up @@ -3672,6 +3721,12 @@ impl Compiler<'_> {
compiler.compile_comprehension_element(elt)?;
compiler.mark_generator();
emit!(compiler, Instruction::YieldValue);
emit!(
compiler,
Instruction::Resume {
arg: bytecode::ResumeType::AfterYield as u32
}
);
emit!(compiler, Instruction::Pop);

Ok(())
Expand Down Expand Up @@ -4067,6 +4122,12 @@ impl Compiler<'_> {
emit!(self, Instruction::GetANext);
self.emit_load_const(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterAwait as u32
}
);
self.compile_store(&generator.target)?;
emit!(self, Instruction::PopBlock);
} else {
Expand Down Expand Up @@ -4145,6 +4206,12 @@ impl Compiler<'_> {
emit!(self, Instruction::GetAwaitable);
self.emit_load_const(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(
self,
Instruction::Resume {
arg: bytecode::ResumeType::AfterAwait as u32
}
);
}

Ok(())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ pub enum ConversionFlag {
Repr = b'r' as i8,
}

/// Resume type for the RESUME instruction
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(u32)]
pub enum ResumeType {
AtFuncStart = 0,
AfterYield = 1,
AfterYieldFrom = 2,
AfterAwait = 3,
}

pub trait Constant: Sized {
type Name: AsRef<str>;

Expand Down Expand Up @@ -382,6 +392,8 @@ op_arg_enum!(
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum IntrinsicFunction1 {
/// Import * operation
ImportStar = 2,
/// Type parameter related
TypeVar = 7,
ParamSpec = 8,
Expand Down Expand Up @@ -419,8 +431,6 @@ pub enum Instruction {
},
/// Importing without name
ImportNameless,
/// Import *
ImportStar,
/// from ... import ...
ImportFrom {
idx: Arg<NameIdx>,
Expand Down Expand Up @@ -549,6 +559,12 @@ pub enum Instruction {
},
YieldValue,
YieldFrom,

/// Resume execution (e.g., at function start, after yield, etc.)
Resume {
arg: Arg<u32>,
},

SetupAnnotation,
SetupLoop,

Expand Down Expand Up @@ -1240,7 +1256,6 @@ impl Instruction {
match self {
Nop => 0,
ImportName { .. } | ImportNameless => -1,
ImportStar => -1,
ImportFrom { .. } => 1,
LoadFast(_) | LoadNameAny(_) | LoadGlobal(_) | LoadDeref(_) | LoadClassDeref(_) => 1,
StoreFast(_) | StoreLocal(_) | StoreGlobal(_) | StoreDeref(_) => -1,
Expand Down Expand Up @@ -1305,6 +1320,7 @@ impl Instruction {
}
ReturnValue => -1,
ReturnConst { .. } => 0,
Resume { .. } => 0,
YieldValue => 0,
YieldFrom => -1,
SetupAnnotation | SetupLoop | SetupFinally { .. } | EnterFinally | EndFinally => 0,
Expand Down Expand Up @@ -1433,7 +1449,6 @@ impl Instruction {
Nop => w!(Nop),
ImportName { idx } => w!(ImportName, name = idx),
ImportNameless => w!(ImportNameless),
ImportStar => w!(ImportStar),
ImportFrom { idx } => w!(ImportFrom, name = idx),
LoadFast(idx) => w!(LoadFast, varname = idx),
LoadNameAny(idx) => w!(LoadNameAny, name = idx),
Expand Down Expand Up @@ -1493,6 +1508,7 @@ impl Instruction {
ForIter { target } => w!(ForIter, target),
ReturnValue => w!(ReturnValue),
ReturnConst { idx } => fmt_const("ReturnConst", arg, f, idx),
Resume { arg } => w!(Resume, arg),
YieldValue => w!(YieldValue),
YieldFrom => w!(YieldFrom),
SetupAnnotation => w!(SetupAnnotation),
Expand Down
4 changes: 4 additions & 0 deletions jit/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
self.stack.pop();
Ok(())
}
Instruction::Resume { arg: _resume_arg } => {
// TODO: Implement the resume instruction
Ok(())
}
_ => Err(JitCompileError::NotSupported),
}
}
Expand Down
Loading
Loading