split comapre and richcompare instruction

This commit is contained in:
Jeong Yunwon 2022-05-12 02:16:00 +09:00
parent 33ddf76b26
commit f721781499
2 changed files with 35 additions and 19 deletions

View file

@ -946,8 +946,8 @@ impl Compiler {
// Check exception type: // Check exception type:
self.compile_expression(exc_type)?; self.compile_expression(exc_type)?;
self.emit(Instruction::CompareOperation { self.emit(Instruction::TestOperation {
op: bytecode::ComparisonOperator::ExceptionMatch, op: bytecode::TestOperator::ExceptionMatch,
}); });
// We cannot handle this exception type: // We cannot handle this exception type:
@ -1468,16 +1468,36 @@ impl Compiler {
let (last_val, mid_vals) = vals.split_last().unwrap(); let (last_val, mid_vals) = vals.split_last().unwrap();
let compile_cmpop = |op: &ast::Cmpop| match op { let compile_cmpop = |op: &ast::Cmpop| match op {
ast::Cmpop::Eq => bytecode::ComparisonOperator::Equal, ast::Cmpop::Eq => Instruction::CompareOperation {
ast::Cmpop::NotEq => bytecode::ComparisonOperator::NotEqual, op: bytecode::CompareOperator::Equal,
ast::Cmpop::Lt => bytecode::ComparisonOperator::Less, },
ast::Cmpop::LtE => bytecode::ComparisonOperator::LessOrEqual, ast::Cmpop::NotEq => Instruction::CompareOperation {
ast::Cmpop::Gt => bytecode::ComparisonOperator::Greater, op: bytecode::CompareOperator::NotEqual,
ast::Cmpop::GtE => bytecode::ComparisonOperator::GreaterOrEqual, },
ast::Cmpop::In => bytecode::ComparisonOperator::In, ast::Cmpop::Lt => Instruction::CompareOperation {
ast::Cmpop::NotIn => bytecode::ComparisonOperator::NotIn, op: bytecode::CompareOperator::Less,
ast::Cmpop::Is => bytecode::ComparisonOperator::Is, },
ast::Cmpop::IsNot => bytecode::ComparisonOperator::IsNot, ast::Cmpop::LtE => Instruction::CompareOperation {
op: bytecode::CompareOperator::LessOrEqual,
},
ast::Cmpop::Gt => Instruction::CompareOperation {
op: bytecode::CompareOperator::Greater,
},
ast::Cmpop::GtE => Instruction::CompareOperation {
op: bytecode::CompareOperator::GreaterOrEqual,
},
ast::Cmpop::In => Instruction::TestOperation {
op: bytecode::TestOperator::In,
},
ast::Cmpop::NotIn => Instruction::TestOperation {
op: bytecode::TestOperator::NotIn,
},
ast::Cmpop::Is => Instruction::TestOperation {
op: bytecode::TestOperator::Is,
},
ast::Cmpop::IsNot => Instruction::TestOperation {
op: bytecode::TestOperator::IsNot,
},
}; };
// a == b == c == d // a == b == c == d
@ -1506,9 +1526,7 @@ impl Compiler {
self.emit(Instruction::Duplicate); self.emit(Instruction::Duplicate);
self.emit(Instruction::Rotate3); self.emit(Instruction::Rotate3);
self.emit(Instruction::CompareOperation { self.emit(compile_cmpop(op));
op: compile_cmpop(op),
});
// if comparison result is false, we break with this value; if true, try the next one. // if comparison result is false, we break with this value; if true, try the next one.
if let Some((break_block, _)) = end_blocks { if let Some((break_block, _)) = end_blocks {
@ -1520,9 +1538,7 @@ impl Compiler {
// handle the last comparison // handle the last comparison
self.compile_expression(last_val)?; self.compile_expression(last_val)?;
self.emit(Instruction::CompareOperation { self.emit(compile_cmpop(last_op));
op: compile_cmpop(last_op),
});
if let Some((break_block, after_block)) = end_blocks { if let Some((break_block, after_block)) = end_blocks {
self.emit(Instruction::Jump { self.emit(Instruction::Jump {

View file

@ -45,7 +45,7 @@ expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyn
39 Jump (54) 39 Jump (54)
>> 40 Duplicate >> 40 Duplicate
41 LoadNameAny (7, Exception) 41 LoadNameAny (7, Exception)
42 CompareOperation (ExceptionMatch) 42 TestOperation (ExceptionMatch)
43 JumpIfFalse (53) 43 JumpIfFalse (53)
44 StoreLocal (8, ex) 44 StoreLocal (8, ex)
45 LoadNameAny (3, self) 45 LoadNameAny (3, self)