Skip to content

Commit f49c185

Browse files
authored
Move PrintExpr to IntristicFunction1 (#6324)
1 parent 59f422d commit f49c185

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

crates/codegen/src/compile.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,14 @@ impl Compiler {
10571057
for statement in body {
10581058
if let Stmt::Expr(StmtExpr { value, .. }) = &statement {
10591059
self.compile_expression(value)?;
1060-
emit!(self, Instruction::PrintExpr);
1060+
emit!(
1061+
self,
1062+
Instruction::CallIntrinsic1 {
1063+
func: bytecode::IntrinsicFunction1::Print
1064+
}
1065+
);
1066+
1067+
emit!(self, Instruction::Pop);
10611068
} else {
10621069
self.compile_statement(statement)?;
10631070
}
@@ -1066,7 +1073,14 @@ impl Compiler {
10661073
if let Stmt::Expr(StmtExpr { value, .. }) = &last {
10671074
self.compile_expression(value)?;
10681075
emit!(self, Instruction::CopyItem { index: 1_u32 });
1069-
emit!(self, Instruction::PrintExpr);
1076+
emit!(
1077+
self,
1078+
Instruction::CallIntrinsic1 {
1079+
func: bytecode::IntrinsicFunction1::Print
1080+
}
1081+
);
1082+
1083+
emit!(self, Instruction::Pop);
10701084
} else {
10711085
self.compile_statement(last)?;
10721086
self.emit_load_const(ConstantData::None);

crates/compiler-core/src/bytecode.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ op_arg_enum!(
551551
#[repr(u8)]
552552
pub enum IntrinsicFunction1 {
553553
// Invalid = 0,
554-
// Print = 1,
554+
Print = 1,
555555
/// Import * operation
556556
ImportStar = 2,
557557
// StopIterationError = 3,
@@ -793,7 +793,6 @@ pub enum Instruction {
793793
PopJumpIfTrue {
794794
target: Arg<Label>,
795795
},
796-
PrintExpr,
797796
Raise {
798797
kind: Arg<RaiseKind>,
799798
},
@@ -1767,7 +1766,6 @@ impl Instruction {
17671766
}
17681767
ListAppend { .. } | SetAdd { .. } => -1,
17691768
MapAdd { .. } => -2,
1770-
PrintExpr => -1,
17711769
LoadBuildClass => 1,
17721770
UnpackSequence { size } => -1 + size.get(arg) as i32,
17731771
UnpackEx { args } => {
@@ -1939,7 +1937,6 @@ impl Instruction {
19391937
PopException => w!(PopException),
19401938
PopJumpIfFalse { target } => w!(PopJumpIfFalse, target),
19411939
PopJumpIfTrue { target } => w!(PopJumpIfTrue, target),
1942-
PrintExpr => w!(PrintExpr),
19431940
Raise { kind } => w!(Raise, ?kind),
19441941
Resume { arg } => w!(Resume, arg),
19451942
ReturnConst { idx } => fmt_const("ReturnConst", arg, f, idx),

crates/vm/src/frame.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,8 +1351,6 @@ impl ExecutingFrame<'_> {
13511351
bytecode::Instruction::PopJumpIfTrue { target } => {
13521352
self.pop_jump_if(vm, target.get(arg), true)
13531353
}
1354-
bytecode::Instruction::PrintExpr => self.print_expr(vm),
1355-
13561354
bytecode::Instruction::Raise { kind } => self.execute_raise(vm, kind.get(arg)),
13571355
bytecode::Instruction::Resume { arg: resume_arg } => {
13581356
// Resume execution after yield, await, or at function start
@@ -2208,18 +2206,6 @@ impl ExecutingFrame<'_> {
22082206
Ok(None)
22092207
}
22102208

2211-
fn print_expr(&mut self, vm: &VirtualMachine) -> FrameResult {
2212-
let expr = self.pop_value();
2213-
2214-
let displayhook = vm
2215-
.sys_module
2216-
.get_attr("displayhook", vm)
2217-
.map_err(|_| vm.new_runtime_error("lost sys.displayhook"))?;
2218-
displayhook.call((expr,), vm)?;
2219-
2220-
Ok(None)
2221-
}
2222-
22232209
fn unpack_sequence(&mut self, size: u32, vm: &VirtualMachine) -> FrameResult {
22242210
let value = self.pop_value();
22252211
let elements: Vec<_> = value.try_to_value(vm).map_err(|e| {
@@ -2390,6 +2376,13 @@ impl ExecutingFrame<'_> {
23902376
vm: &VirtualMachine,
23912377
) -> PyResult {
23922378
match func {
2379+
bytecode::IntrinsicFunction1::Print => {
2380+
let displayhook = vm
2381+
.sys_module
2382+
.get_attr("displayhook", vm)
2383+
.map_err(|_| vm.new_runtime_error("lost sys.displayhook"))?;
2384+
displayhook.call((arg,), vm)
2385+
}
23932386
bytecode::IntrinsicFunction1::ImportStar => {
23942387
// arg is the module object
23952388
self.push_value(arg); // Push module back on stack for import_star

0 commit comments

Comments
 (0)