LibJS/Bytecode: Add and use copy_if_needed_to_preserve_evaluation_order

This is a new Bytecode::Generator helper that takes an operand and
returns the same operand, or a copy of it, in case a copy is required
to preserve correct evaluation order.

This can be used in a bunch of places where we're worried about
clobbering some value after obtaining it.

Practically, locals are always copied, and temporary registers as well
as constants are returned as-is.
This commit is contained in:
Andreas Kling 2024-06-01 10:00:32 +02:00
commit c372a084a2
Notes: sideshowbarker 2024-07-17 23:02:37 +09:00
3 changed files with 18 additions and 34 deletions

View file

@ -1156,4 +1156,13 @@ void Generator::emit_jump_if(ScopedOperand const& condition, Label true_target,
emit<Op::JumpIf>(condition, true_target, false_target);
}
ScopedOperand Generator::copy_if_needed_to_preserve_evaluation_order(ScopedOperand const& operand)
{
if (!operand.operand().is_local())
return operand;
auto new_register = allocate_register();
emit<Bytecode::Op::Mov>(new_register, operand);
return new_register;
}
}