Skip to content

Commit

Permalink
LibJS/Bytecode: Make primitive bigints be constants
Browse files Browse the repository at this point in the history
Instead of emitting a NewBigInt instruction to construct a primitive
bigint from a parsed literal, we now instantiate the BigInt on the heap
during codegen.
  • Loading branch information
awesomekling committed Mar 3, 2024
1 parent 46d209c commit 5813df2
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 40 deletions.
7 changes: 2 additions & 5 deletions Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Bytecode::CodeGenerationErrorOr<Optional<Bytecode::Operand>> NullLiteral::genera
return generator.add_constant(js_null());
}

Bytecode::CodeGenerationErrorOr<Optional<Bytecode::Operand>> BigIntLiteral::generate_bytecode(Bytecode::Generator& generator, Optional<Bytecode::Operand> preferred_dst) const
Bytecode::CodeGenerationErrorOr<Optional<Bytecode::Operand>> BigIntLiteral::generate_bytecode(Bytecode::Generator& generator, [[maybe_unused]] Optional<Bytecode::Operand> preferred_dst) const
{
Bytecode::Generator::SourceLocationScope scope(generator, *this);
// 1. Return the NumericValue of NumericLiteral as defined in 12.8.3.
Expand All @@ -293,10 +293,7 @@ Bytecode::CodeGenerationErrorOr<Optional<Bytecode::Operand>> BigIntLiteral::gene
return MUST(Crypto::SignedBigInteger::from_base(2, m_value.substring(2, m_value.length() - 3)));
return MUST(Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1)));
}();

auto dst = choose_dst(generator, preferred_dst);
generator.emit<Bytecode::Op::NewBigInt>(dst, integer);
return dst;
return generator.add_constant(BigInt::create(generator.vm(), move(integer)), Bytecode::Generator::DeduplicateConstant::No);
}

Bytecode::CodeGenerationErrorOr<Optional<Bytecode::Operand>> StringLiteral::generate_bytecode(Bytecode::Generator& generator, [[maybe_unused]] Optional<Bytecode::Operand> preferred_dst) const
Expand Down
1 change: 0 additions & 1 deletion Userland/Libraries/LibJS/Bytecode/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
O(Mov) \
O(Mul) \
O(NewArray) \
O(NewBigInt) \
O(NewClass) \
O(NewFunction) \
O(NewObject) \
Expand Down
16 changes: 2 additions & 14 deletions Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ static ByteString format_operand(StringView name, Operand operand, Bytecode::Exe
builder.appendff("Int32({})", value.as_i32());
else if (value.is_double())
builder.appendff("Double({})", value.as_double());
else if (value.is_bigint())
builder.appendff("BigInt({})", value.as_bigint().to_byte_string());
else if (value.is_string())
builder.appendff("String(\"{}\")", value.as_string().utf8_string_view());
else if (value.is_undefined())
Expand Down Expand Up @@ -854,13 +856,6 @@ static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)

JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)

ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto& vm = interpreter.vm();
interpreter.set(dst(), BigInt::create(vm, m_bigint));
return {};
}

ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto array = MUST(Array::create(interpreter.realm(), 0));
Expand Down Expand Up @@ -1613,13 +1608,6 @@ ByteString Mov::to_byte_string_impl(Bytecode::Executable const& executable) cons
format_operand("src"sv, m_src, executable));
}

ByteString NewBigInt::to_byte_string_impl(Bytecode::Executable const& executable) const
{
return ByteString::formatted("NewBigInt {}, {}",
format_operand("dst"sv, dst(), executable),
m_bigint.to_base_deprecated(10));
}

ByteString NewArray::to_byte_string_impl(Bytecode::Executable const& executable) const
{
StringBuilder builder;
Expand Down
20 changes: 0 additions & 20 deletions Userland/Libraries/LibJS/Bytecode/Op.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,26 +239,6 @@ class CopyObjectExcludingProperties final : public Instruction {
Operand m_excluded_names[];
};

class NewBigInt final : public Instruction {
public:
NewBigInt(Operand dst, Crypto::SignedBigInteger bigint)
: Instruction(Type::NewBigInt, sizeof(*this))
, m_dst(dst)
, m_bigint(move(bigint))
{
}

ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
ByteString to_byte_string_impl(Bytecode::Executable const&) const;

Operand dst() const { return m_dst; }
Crypto::SignedBigInteger const& bigint() const { return m_bigint; }

private:
Operand m_dst;
Crypto::SignedBigInteger m_bigint;
};

// NOTE: This instruction is variable-width depending on the number of elements!
class NewArray final : public Instruction {
public:
Expand Down

0 comments on commit 5813df2

Please sign in to comment.