Skip to content

Commit 392f9c2

Browse files
authored
Instruction::Resume (#5944)
* ImportStar * Instruction::Resume
1 parent 0ae6b45 commit 392f9c2

File tree

5 files changed

+161
-58
lines changed

5 files changed

+161
-58
lines changed

compiler/codegen/src/compile.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,12 @@ impl Compiler<'_> {
784784

785785
if import_star {
786786
// from .... import *
787-
emit!(self, Instruction::ImportStar);
787+
emit!(
788+
self,
789+
Instruction::CallIntrinsic1 {
790+
func: bytecode::IntrinsicFunction1::ImportStar
791+
}
792+
);
788793
} else {
789794
// from mod import a, b as c
790795

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

1564+
// Emit RESUME instruction at function start
1565+
emit!(
1566+
self,
1567+
Instruction::Resume {
1568+
arg: bytecode::ResumeType::AtFuncStart as u32
1569+
}
1570+
);
1571+
15591572
self.compile_statements(body)?;
15601573

15611574
// Emit None at end:
@@ -1971,6 +1984,12 @@ impl Compiler<'_> {
19711984
emit!(self, Instruction::GetAwaitable);
19721985
self.emit_load_const(ConstantData::None);
19731986
emit!(self, Instruction::YieldFrom);
1987+
emit!(
1988+
self,
1989+
Instruction::Resume {
1990+
arg: bytecode::ResumeType::AfterAwait as u32
1991+
}
1992+
);
19741993
emit!(self, Instruction::SetupAsyncWith { end: final_block });
19751994
} else {
19761995
emit!(self, Instruction::SetupWith { end: final_block });
@@ -2012,6 +2031,12 @@ impl Compiler<'_> {
20122031
emit!(self, Instruction::GetAwaitable);
20132032
self.emit_load_const(ConstantData::None);
20142033
emit!(self, Instruction::YieldFrom);
2034+
emit!(
2035+
self,
2036+
Instruction::Resume {
2037+
arg: bytecode::ResumeType::AfterAwait as u32
2038+
}
2039+
);
20152040
}
20162041

20172042
emit!(self, Instruction::WithCleanupFinish);
@@ -2050,6 +2075,12 @@ impl Compiler<'_> {
20502075
emit!(self, Instruction::GetANext);
20512076
self.emit_load_const(ConstantData::None);
20522077
emit!(self, Instruction::YieldFrom);
2078+
emit!(
2079+
self,
2080+
Instruction::Resume {
2081+
arg: bytecode::ResumeType::AfterAwait as u32
2082+
}
2083+
);
20532084
self.compile_store(target)?;
20542085
emit!(self, Instruction::PopBlock);
20552086
} else {
@@ -3521,6 +3552,12 @@ impl Compiler<'_> {
35213552
Option::None => self.emit_load_const(ConstantData::None),
35223553
};
35233554
emit!(self, Instruction::YieldValue);
3555+
emit!(
3556+
self,
3557+
Instruction::Resume {
3558+
arg: bytecode::ResumeType::AfterYield as u32
3559+
}
3560+
);
35243561
}
35253562
Expr::Await(ExprAwait { value, .. }) => {
35263563
if self.ctx.func != FunctionContext::AsyncFunction {
@@ -3530,6 +3567,12 @@ impl Compiler<'_> {
35303567
emit!(self, Instruction::GetAwaitable);
35313568
self.emit_load_const(ConstantData::None);
35323569
emit!(self, Instruction::YieldFrom);
3570+
emit!(
3571+
self,
3572+
Instruction::Resume {
3573+
arg: bytecode::ResumeType::AfterAwait as u32
3574+
}
3575+
);
35333576
}
35343577
Expr::YieldFrom(ExprYieldFrom { value, .. }) => {
35353578
match self.ctx.func {
@@ -3546,6 +3589,12 @@ impl Compiler<'_> {
35463589
emit!(self, Instruction::GetIter);
35473590
self.emit_load_const(ConstantData::None);
35483591
emit!(self, Instruction::YieldFrom);
3592+
emit!(
3593+
self,
3594+
Instruction::Resume {
3595+
arg: bytecode::ResumeType::AfterYieldFrom as u32
3596+
}
3597+
);
35493598
}
35503599
Expr::Name(ExprName { id, .. }) => self.load_name(id.as_str())?,
35513600
Expr::Lambda(ExprLambda {
@@ -3672,6 +3721,12 @@ impl Compiler<'_> {
36723721
compiler.compile_comprehension_element(elt)?;
36733722
compiler.mark_generator();
36743723
emit!(compiler, Instruction::YieldValue);
3724+
emit!(
3725+
compiler,
3726+
Instruction::Resume {
3727+
arg: bytecode::ResumeType::AfterYield as u32
3728+
}
3729+
);
36753730
emit!(compiler, Instruction::Pop);
36763731

36773732
Ok(())
@@ -4067,6 +4122,12 @@ impl Compiler<'_> {
40674122
emit!(self, Instruction::GetANext);
40684123
self.emit_load_const(ConstantData::None);
40694124
emit!(self, Instruction::YieldFrom);
4125+
emit!(
4126+
self,
4127+
Instruction::Resume {
4128+
arg: bytecode::ResumeType::AfterAwait as u32
4129+
}
4130+
);
40704131
self.compile_store(&generator.target)?;
40714132
emit!(self, Instruction::PopBlock);
40724133
} else {
@@ -4145,6 +4206,12 @@ impl Compiler<'_> {
41454206
emit!(self, Instruction::GetAwaitable);
41464207
self.emit_load_const(ConstantData::None);
41474208
emit!(self, Instruction::YieldFrom);
4209+
emit!(
4210+
self,
4211+
Instruction::Resume {
4212+
arg: bytecode::ResumeType::AfterAwait as u32
4213+
}
4214+
);
41484215
}
41494216

41504217
Ok(())

compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap

Lines changed: 51 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/core/src/bytecode.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ pub enum ConversionFlag {
2424
Repr = b'r' as i8,
2525
}
2626

27+
/// Resume type for the RESUME instruction
28+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
29+
#[repr(u32)]
30+
pub enum ResumeType {
31+
AtFuncStart = 0,
32+
AfterYield = 1,
33+
AfterYieldFrom = 2,
34+
AfterAwait = 3,
35+
}
36+
2737
pub trait Constant: Sized {
2838
type Name: AsRef<str>;
2939

@@ -382,6 +392,8 @@ op_arg_enum!(
382392
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
383393
#[repr(u8)]
384394
pub enum IntrinsicFunction1 {
395+
/// Import * operation
396+
ImportStar = 2,
385397
/// Type parameter related
386398
TypeVar = 7,
387399
ParamSpec = 8,
@@ -419,8 +431,6 @@ pub enum Instruction {
419431
},
420432
/// Importing without name
421433
ImportNameless,
422-
/// Import *
423-
ImportStar,
424434
/// from ... import ...
425435
ImportFrom {
426436
idx: Arg<NameIdx>,
@@ -549,6 +559,12 @@ pub enum Instruction {
549559
},
550560
YieldValue,
551561
YieldFrom,
562+
563+
/// Resume execution (e.g., at function start, after yield, etc.)
564+
Resume {
565+
arg: Arg<u32>,
566+
},
567+
552568
SetupAnnotation,
553569
SetupLoop,
554570

@@ -1240,7 +1256,6 @@ impl Instruction {
12401256
match self {
12411257
Nop => 0,
12421258
ImportName { .. } | ImportNameless => -1,
1243-
ImportStar => -1,
12441259
ImportFrom { .. } => 1,
12451260
LoadFast(_) | LoadNameAny(_) | LoadGlobal(_) | LoadDeref(_) | LoadClassDeref(_) => 1,
12461261
StoreFast(_) | StoreLocal(_) | StoreGlobal(_) | StoreDeref(_) => -1,
@@ -1305,6 +1320,7 @@ impl Instruction {
13051320
}
13061321
ReturnValue => -1,
13071322
ReturnConst { .. } => 0,
1323+
Resume { .. } => 0,
13081324
YieldValue => 0,
13091325
YieldFrom => -1,
13101326
SetupAnnotation | SetupLoop | SetupFinally { .. } | EnterFinally | EndFinally => 0,
@@ -1433,7 +1449,6 @@ impl Instruction {
14331449
Nop => w!(Nop),
14341450
ImportName { idx } => w!(ImportName, name = idx),
14351451
ImportNameless => w!(ImportNameless),
1436-
ImportStar => w!(ImportStar),
14371452
ImportFrom { idx } => w!(ImportFrom, name = idx),
14381453
LoadFast(idx) => w!(LoadFast, varname = idx),
14391454
LoadNameAny(idx) => w!(LoadNameAny, name = idx),
@@ -1493,6 +1508,7 @@ impl Instruction {
14931508
ForIter { target } => w!(ForIter, target),
14941509
ReturnValue => w!(ReturnValue),
14951510
ReturnConst { idx } => fmt_const("ReturnConst", arg, f, idx),
1511+
Resume { arg } => w!(Resume, arg),
14961512
YieldValue => w!(YieldValue),
14971513
YieldFrom => w!(YieldFrom),
14981514
SetupAnnotation => w!(SetupAnnotation),

jit/src/instructions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
612612
self.stack.pop();
613613
Ok(())
614614
}
615+
Instruction::Resume { arg: _resume_arg } => {
616+
// TODO: Implement the resume instruction
617+
Ok(())
618+
}
615619
_ => Err(JitCompileError::NotSupported),
616620
}
617621
}

0 commit comments

Comments
 (0)