Skip to content
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
38 changes: 20 additions & 18 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ impl Compiler {

if let Stmt::Expr(StmtExpr { value, .. }) = &last {
self.compile_expression(value)?;
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
emit!(self, Instruction::PrintExpr);
} else {
self.compile_statement(last)?;
Expand Down Expand Up @@ -1094,7 +1094,7 @@ impl Compiler {
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
let pop_instructions = self.current_block().instructions.pop();
let store_inst = compiler_unwrap_option(self, pop_instructions); // pop Instruction::Store
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
self.current_block().instructions.push(store_inst);
}
_ => self.emit_load_const(ConstantData::None),
Expand Down Expand Up @@ -1656,7 +1656,7 @@ impl Compiler {

for (i, target) in targets.iter().enumerate() {
if i + 1 != targets.len() {
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
}
self.compile_store(target)?;
}
Expand Down Expand Up @@ -1917,7 +1917,7 @@ impl Compiler {
);
}

emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
self.store_name(name.as_ref())?;
}
TypeParam::ParamSpec(TypeParamParamSpec { name, default, .. }) => {
Expand All @@ -1943,7 +1943,7 @@ impl Compiler {
);
}

emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
self.store_name(name.as_ref())?;
}
TypeParam::TypeVarTuple(TypeParamTypeVarTuple { name, default, .. }) => {
Expand All @@ -1970,7 +1970,7 @@ impl Compiler {
);
}

emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
self.store_name(name.as_ref())?;
}
};
Expand Down Expand Up @@ -2030,7 +2030,7 @@ impl Compiler {
// check if this handler can handle the exception:
if let Some(exc_type) = type_ {
// Duplicate exception for test:
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });

// Check exception type:
self.compile_expression(exc_type)?;
Expand Down Expand Up @@ -2251,11 +2251,11 @@ impl Compiler {

// Handle docstring if present
if let Some(doc) = doc_str {
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
self.emit_load_const(ConstantData::Str {
value: doc.to_string().into(),
});
emit!(self, Instruction::Rotate2);
emit!(self, Instruction::Swap { index: 2 });
let doc_attr = self.name("__doc__");
emit!(self, Instruction::StoreAttr { idx: doc_attr });
}
Expand Down Expand Up @@ -2726,7 +2726,7 @@ impl Compiler {

if let Some(classcell_idx) = classcell_idx {
emit!(self, Instruction::LoadClosure(classcell_idx.to_u32()));
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
let classcell = self.name("__classcell__");
emit!(self, Instruction::StoreLocal(classcell));
} else {
Expand Down Expand Up @@ -4121,8 +4121,8 @@ impl Compiler {
for (op, val) in mid_ops.iter().zip(mid_exprs) {
self.compile_expression(val)?;
// store rhs for the next comparison in chain
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::Rotate3);
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::CopyItem { index: 2_u32 });

compile_cmpop(self, op);

Expand Down Expand Up @@ -4151,7 +4151,7 @@ impl Compiler {

// early exit left us with stack: `rhs, comparison_result`. We need to clean up rhs.
self.switch_to_block(break_block);
emit!(self, Instruction::Rotate2);
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::Pop);

self.switch_to_block(after_block);
Expand Down Expand Up @@ -4322,15 +4322,16 @@ impl Compiler {
// But we can't use compile_subscript directly because we need DUP_TOP2
self.compile_expression(value)?;
self.compile_expression(slice)?;
emit!(self, Instruction::Duplicate2);
emit!(self, Instruction::CopyItem { index: 2_u32 });
emit!(self, Instruction::CopyItem { index: 2_u32 });
emit!(self, Instruction::Subscript);
AugAssignKind::Subscript
}
Expr::Attribute(ExprAttribute { value, attr, .. }) => {
let attr = attr.as_str();
self.check_forbidden_name(attr, NameUsage::Store)?;
self.compile_expression(value)?;
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
let idx = self.name(attr);
emit!(self, Instruction::LoadAttr { idx });
AugAssignKind::Attr { idx }
Expand All @@ -4350,12 +4351,13 @@ impl Compiler {
}
AugAssignKind::Subscript => {
// stack: CONTAINER SLICE RESULT
emit!(self, Instruction::Rotate3);
emit!(self, Instruction::Swap { index: 3 });
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::StoreSubscript);
}
AugAssignKind::Attr { idx } => {
// stack: CONTAINER RESULT
emit!(self, Instruction::Rotate2);
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::StoreAttr { idx });
}
}
Expand Down Expand Up @@ -4896,7 +4898,7 @@ impl Compiler {
range: _,
}) => {
self.compile_expression(value)?;
emit!(self, Instruction::Duplicate);
emit!(self, Instruction::CopyItem { index: 1_u32 });
self.compile_store(target)?;
}
Expr::FString(fstring) => {
Expand Down

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

11 changes: 0 additions & 11 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,6 @@ pub enum Instruction {
index: Arg<u32>,
},
ToBool,
Rotate2,
Rotate3,
Duplicate,
Duplicate2,
GetIter,
GetLen,
CallIntrinsic1 {
Expand Down Expand Up @@ -1475,9 +1471,6 @@ impl Instruction {
Pop => -1,
Swap { .. } => 0,
ToBool => 0,
Rotate2 | Rotate3 => 0,
Duplicate => 1,
Duplicate2 => 2,
GetIter => 0,
GetLen => 1,
CallIntrinsic1 { .. } => 0, // Takes 1, pushes 1
Expand Down Expand Up @@ -1678,10 +1671,6 @@ impl Instruction {
Pop => w!(Pop),
Swap { index } => w!(Swap, index),
ToBool => w!(ToBool),
Rotate2 => w!(Rotate2),
Rotate3 => w!(Rotate3),
Duplicate => w!(Duplicate),
Duplicate2 => w!(Duplicate2),
GetIter => w!(GetIter),
// GET_LEN
GetLen => w!(GetLen),
Expand Down
12 changes: 0 additions & 12 deletions crates/jit/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,6 @@ impl StackMachine {
self.stack.push(StackValue::Function(func));
}
}
Instruction::Duplicate => {
let value = self.stack.last().unwrap().clone();
self.stack.push(value);
}
Instruction::Rotate2 => {
let i = self.stack.len() - 2;
self.stack[i..].rotate_right(1);
}
Instruction::Rotate3 => {
let i = self.stack.len() - 3;
self.stack[i..].rotate_right(1);
}
Instruction::ReturnConst { idx } => {
let idx = idx.get(arg);
self.stack.push(constants[idx as usize].clone().into());
Expand Down
41 changes: 9 additions & 32 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,32 +719,16 @@ impl ExecutingFrame<'_> {
self.state.stack.swap(i, j);
Ok(None)
}
// bytecode::Instruction::ToBool => {
// dbg!("Shouldn't be called outside of match statements for now")
// let value = self.pop_value();
// // call __bool__
// let result = value.try_to_bool(vm)?;
// self.push_value(vm.ctx.new_bool(result).into());
// Ok(None)
// }
bytecode::Instruction::Duplicate => {
// Duplicate top of stack
let value = self.top_value();
self.push_value(value.to_owned());
Ok(None)
}
bytecode::Instruction::Duplicate2 => {
// Duplicate top 2 of stack
let len = self.state.stack.len();
self.push_value(self.state.stack[len - 2].clone());
self.push_value(self.state.stack[len - 1].clone());
Ok(None)
/*
bytecode::Instruction::ToBool => {
dbg!("Shouldn't be called outside of match statements for now")
let value = self.pop_value();
// call __bool__
let result = value.try_to_bool(vm)?;
self.push_value(vm.ctx.new_bool(result).into());
Ok(None)
}
// splitting the instructions like this offloads the cost of "dynamic" dispatch (on the
// amount to rotate) to the opcode dispatcher, and generates optimized code for the
// concrete cases we actually have
bytecode::Instruction::Rotate2 => self.execute_rotate(2),
bytecode::Instruction::Rotate3 => self.execute_rotate(3),
*/
bytecode::Instruction::BuildString { size } => {
let s = self
.pop_multiple(size.get(arg) as usize)
Expand Down Expand Up @@ -1685,13 +1669,6 @@ impl ExecutingFrame<'_> {
}
}

#[inline(always)]
fn execute_rotate(&mut self, amount: usize) -> FrameResult {
let i = self.state.stack.len() - amount;
self.state.stack[i..].rotate_right(1);
Ok(None)
}

fn execute_subscript(&mut self, vm: &VirtualMachine) -> FrameResult {
let b_ref = self.pop_value();
let a_ref = self.pop_value();
Expand Down
Loading