Skip to content

Commit 70c3bf4

Browse files
committed
Move PrintExpr to IntristicFunction1
1 parent d287d1e commit 70c3bf4

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

crates/codegen/src/compile.rs

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

crates/compiler-core/src/bytecode.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ op_arg_enum!(
511511
#[repr(u8)]
512512
pub enum IntrinsicFunction1 {
513513
// Invalid = 0,
514-
// Print = 1,
514+
Print = 1,
515515
/// Import * operation
516516
ImportStar = 2,
517517
// StopIterationError = 3,
@@ -727,8 +727,6 @@ pub enum Instruction {
727727
PopJumpIfTrue {
728728
target: Arg<Label>,
729729
},
730-
731-
PrintExpr,
732730
Raise {
733731
kind: Arg<RaiseKind>,
734732
},
@@ -1702,7 +1700,6 @@ impl Instruction {
17021700
}
17031701
ListAppend { .. } | SetAdd { .. } => -1,
17041702
MapAdd { .. } => -2,
1705-
PrintExpr => -1,
17061703
LoadBuildClass => 1,
17071704
UnpackSequence { size } => -1 + size.get(arg) as i32,
17081705
UnpackEx { args } => {
@@ -1874,7 +1871,6 @@ impl Instruction {
18741871
PopException => w!(PopException),
18751872
PopJumpIfFalse { target } => w!(PopJumpIfFalse, target),
18761873
PopJumpIfTrue { target } => w!(PopJumpIfTrue, target),
1877-
PrintExpr => w!(PrintExpr),
18781874
Raise { kind } => w!(Raise, ?kind),
18791875
Resume { arg } => w!(Resume, arg),
18801876
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
@@ -1339,8 +1339,6 @@ impl ExecutingFrame<'_> {
13391339
bytecode::Instruction::PopJumpIfTrue { target } => {
13401340
self.pop_jump_if(vm, target.get(arg), true)
13411341
}
1342-
bytecode::Instruction::PrintExpr => self.print_expr(vm),
1343-
13441342
bytecode::Instruction::Raise { kind } => self.execute_raise(vm, kind.get(arg)),
13451343
bytecode::Instruction::Resume { arg: resume_arg } => {
13461344
// Resume execution after yield, await, or at function start
@@ -2196,18 +2194,6 @@ impl ExecutingFrame<'_> {
21962194
Ok(None)
21972195
}
21982196

2199-
fn print_expr(&mut self, vm: &VirtualMachine) -> FrameResult {
2200-
let expr = self.pop_value();
2201-
2202-
let displayhook = vm
2203-
.sys_module
2204-
.get_attr("displayhook", vm)
2205-
.map_err(|_| vm.new_runtime_error("lost sys.displayhook"))?;
2206-
displayhook.call((expr,), vm)?;
2207-
2208-
Ok(None)
2209-
}
2210-
22112197
fn unpack_sequence(&mut self, size: u32, vm: &VirtualMachine) -> FrameResult {
22122198
let value = self.pop_value();
22132199
let elements: Vec<_> = value.try_to_value(vm).map_err(|e| {
@@ -2380,6 +2366,13 @@ impl ExecutingFrame<'_> {
23802366
vm: &VirtualMachine,
23812367
) -> PyResult {
23822368
match func {
2369+
bytecode::IntrinsicFunction1::Print => {
2370+
let displayhook = vm
2371+
.sys_module
2372+
.get_attr("displayhook", vm)
2373+
.map_err(|_| vm.new_runtime_error("lost sys.displayhook"))?;
2374+
displayhook.call((arg,), vm)
2375+
}
23832376
bytecode::IntrinsicFunction1::ImportStar => {
23842377
// arg is the module object
23852378
self.push_value(arg); // Push module back on stack for import_star

0 commit comments

Comments
 (0)