mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibJS/Bytecode: Turn UnaryMinus(NumericLiteral) into a constant
This commit is contained in:
parent
e37feaa196
commit
56a3ccde1a
Notes:
sideshowbarker
2024-07-16 23:13:25 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/56a3ccde1a Pull-request: https://github.com/SerenityOS/serenity/pull/24276 Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/shannonbooth
1 changed files with 8 additions and 0 deletions
|
@ -269,6 +269,14 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> LogicalExpression::gene
|
|||
Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> UnaryExpression::generate_bytecode(Bytecode::Generator& generator, Optional<ScopedOperand> preferred_dst) const
|
||||
{
|
||||
Bytecode::Generator::SourceLocationScope scope(generator, *this);
|
||||
|
||||
// OPTIMIZATION: Turn expressions like `-1` into a constant.
|
||||
if (m_op == UnaryOp::Minus && is<NumericLiteral>(*m_lhs)) {
|
||||
auto& numeric_literal = static_cast<NumericLiteral const&>(*m_lhs);
|
||||
auto value = numeric_literal.value();
|
||||
return generator.add_constant(Value(-value.as_double()));
|
||||
}
|
||||
|
||||
if (m_op == UnaryOp::Delete)
|
||||
return generator.emit_delete_reference(m_lhs);
|
||||
|
||||
|
|
Loading…
Reference in a new issue