2021-06-03 08:46:30 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
|
2021-06-03 08:46:30 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/OwnPtr.h>
|
2021-06-09 02:19:58 +00:00
|
|
|
#include <AK/SinglyLinkedList.h>
|
2023-09-01 14:53:55 +00:00
|
|
|
#include <LibJS/AST.h>
|
2021-06-09 02:19:58 +00:00
|
|
|
#include <LibJS/Bytecode/BasicBlock.h>
|
2022-02-12 16:24:08 +00:00
|
|
|
#include <LibJS/Bytecode/CodeGenerationError.h>
|
2021-10-24 11:30:49 +00:00
|
|
|
#include <LibJS/Bytecode/Executable.h>
|
2021-10-24 13:34:30 +00:00
|
|
|
#include <LibJS/Bytecode/IdentifierTable.h>
|
2021-06-04 10:07:38 +00:00
|
|
|
#include <LibJS/Bytecode/Label.h>
|
2021-06-10 21:05:01 +00:00
|
|
|
#include <LibJS/Bytecode/Op.h>
|
2021-06-08 22:50:42 +00:00
|
|
|
#include <LibJS/Bytecode/Register.h>
|
2021-06-09 08:02:01 +00:00
|
|
|
#include <LibJS/Bytecode/StringTable.h>
|
2021-06-03 08:46:30 +00:00
|
|
|
#include <LibJS/Forward.h>
|
2021-11-10 21:16:07 +00:00
|
|
|
#include <LibJS/Runtime/FunctionKind.h>
|
2023-07-13 08:49:07 +00:00
|
|
|
#include <LibRegex/Regex.h>
|
2021-06-03 08:46:30 +00:00
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
|
|
|
class Generator {
|
|
|
|
public:
|
2024-03-03 10:34:36 +00:00
|
|
|
VM& vm() { return m_vm; }
|
|
|
|
|
2022-02-12 16:18:45 +00:00
|
|
|
enum class SurroundingScopeKind {
|
|
|
|
Global,
|
|
|
|
Function,
|
|
|
|
Block,
|
|
|
|
};
|
2024-05-05 20:06:55 +00:00
|
|
|
|
2024-05-14 08:57:06 +00:00
|
|
|
enum class MustPropagateCompletion {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
static CodeGenerationErrorOr<GC::Ref<Executable>> generate_from_ast_node(VM&, ASTNode const&, FunctionKind = FunctionKind::Normal);
|
|
|
|
static CodeGenerationErrorOr<GC::Ref<Executable>> generate_from_function(VM&, ECMAScriptFunctionObject const& function);
|
2024-05-05 20:06:55 +00:00
|
|
|
|
|
|
|
CodeGenerationErrorOr<void> emit_function_declaration_instantiation(ECMAScriptFunctionObject const& function);
|
2021-06-03 08:46:30 +00:00
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
[[nodiscard]] ScopedOperand allocate_register();
|
|
|
|
[[nodiscard]] ScopedOperand local(u32 local_index);
|
|
|
|
[[nodiscard]] ScopedOperand accumulator();
|
2024-05-31 18:41:29 +00:00
|
|
|
[[nodiscard]] ScopedOperand this_value();
|
2024-05-07 19:36:56 +00:00
|
|
|
|
|
|
|
void free_register(Register);
|
2021-06-03 08:46:30 +00:00
|
|
|
|
2024-02-04 07:00:54 +00:00
|
|
|
void set_local_initialized(u32 local_index);
|
|
|
|
[[nodiscard]] bool is_local_initialized(u32 local_index) const;
|
|
|
|
|
2023-09-01 14:53:55 +00:00
|
|
|
class SourceLocationScope {
|
|
|
|
public:
|
|
|
|
SourceLocationScope(Generator&, ASTNode const& node);
|
|
|
|
~SourceLocationScope();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Generator& m_generator;
|
|
|
|
ASTNode const* m_previous_node { nullptr };
|
|
|
|
};
|
|
|
|
|
2023-10-19 21:18:54 +00:00
|
|
|
class UnwindContext {
|
|
|
|
public:
|
|
|
|
UnwindContext(Generator&, Optional<Label> finalizer);
|
|
|
|
|
|
|
|
UnwindContext const* previous() const { return m_previous_context; }
|
|
|
|
void set_handler(Label handler) { m_handler = handler; }
|
|
|
|
Optional<Label> handler() const { return m_handler; }
|
|
|
|
Optional<Label> finalizer() const { return m_finalizer; }
|
|
|
|
|
|
|
|
~UnwindContext();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Generator& m_generator;
|
|
|
|
Optional<Label> m_finalizer;
|
|
|
|
Optional<Label> m_handler {};
|
|
|
|
UnwindContext const* m_previous_context { nullptr };
|
|
|
|
};
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
template<typename OpType, typename... Args>
|
2024-05-06 21:00:19 +00:00
|
|
|
requires(requires { OpType(declval<Args>()...); })
|
2023-09-28 10:48:29 +00:00
|
|
|
void emit(Args&&... args)
|
2021-06-03 08:46:30 +00:00
|
|
|
{
|
2021-06-09 02:19:58 +00:00
|
|
|
VERIFY(!is_current_block_terminated());
|
2023-09-28 07:29:42 +00:00
|
|
|
size_t slot_offset = m_current_basic_block->size();
|
2024-05-09 13:13:31 +00:00
|
|
|
m_current_basic_block->set_last_instruction_start_offset(slot_offset);
|
2021-06-08 22:50:42 +00:00
|
|
|
grow(sizeof(OpType));
|
2023-09-28 07:29:42 +00:00
|
|
|
void* slot = m_current_basic_block->data() + slot_offset;
|
2021-06-08 22:50:42 +00:00
|
|
|
new (slot) OpType(forward<Args>(args)...);
|
2021-06-09 02:19:58 +00:00
|
|
|
if constexpr (OpType::IsTerminator)
|
2024-03-06 07:36:19 +00:00
|
|
|
m_current_basic_block->terminate({});
|
2024-05-06 05:51:14 +00:00
|
|
|
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
2021-06-07 13:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 11:37:28 +00:00
|
|
|
template<typename OpType, typename ExtraSlotType, typename... Args>
|
2024-05-06 21:00:19 +00:00
|
|
|
requires(requires { OpType(declval<Args>()...); })
|
2024-03-03 11:37:28 +00:00
|
|
|
void emit_with_extra_slots(size_t extra_slot_count, Args&&... args)
|
2021-06-07 13:12:43 +00:00
|
|
|
{
|
2021-06-09 02:19:58 +00:00
|
|
|
VERIFY(!is_current_block_terminated());
|
2022-09-09 14:47:42 +00:00
|
|
|
|
2024-03-03 11:37:28 +00:00
|
|
|
size_t size_to_allocate = round_up_to_power_of_two(sizeof(OpType) + extra_slot_count * sizeof(ExtraSlotType), alignof(void*));
|
2023-09-28 07:29:42 +00:00
|
|
|
size_t slot_offset = m_current_basic_block->size();
|
2024-05-09 13:13:31 +00:00
|
|
|
m_current_basic_block->set_last_instruction_start_offset(slot_offset);
|
2022-09-09 14:47:42 +00:00
|
|
|
grow(size_to_allocate);
|
2023-09-28 07:29:42 +00:00
|
|
|
void* slot = m_current_basic_block->data() + slot_offset;
|
2021-06-08 22:50:42 +00:00
|
|
|
new (slot) OpType(forward<Args>(args)...);
|
2021-06-09 02:19:58 +00:00
|
|
|
if constexpr (OpType::IsTerminator)
|
2024-03-06 07:36:19 +00:00
|
|
|
m_current_basic_block->terminate({});
|
2024-05-06 05:51:14 +00:00
|
|
|
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
2021-06-03 08:46:30 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 11:37:28 +00:00
|
|
|
template<typename OpType, typename... Args>
|
2024-05-06 21:00:19 +00:00
|
|
|
requires(requires { OpType(declval<Args>()...); })
|
2024-03-03 11:37:28 +00:00
|
|
|
void emit_with_extra_operand_slots(size_t extra_operand_slots, Args&&... args)
|
|
|
|
{
|
|
|
|
emit_with_extra_slots<OpType, Operand>(extra_operand_slots, forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OpType, typename... Args>
|
2024-05-06 21:00:19 +00:00
|
|
|
requires(requires { OpType(declval<Args>()...); })
|
2024-03-03 11:37:28 +00:00
|
|
|
void emit_with_extra_value_slots(size_t extra_operand_slots, Args&&... args)
|
|
|
|
{
|
|
|
|
emit_with_extra_slots<OpType, Value>(extra_operand_slots, forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2024-05-09 13:13:31 +00:00
|
|
|
void emit_jump_if(ScopedOperand const& condition, Label true_target, Label false_target);
|
|
|
|
|
2024-02-04 07:00:54 +00:00
|
|
|
struct ReferenceOperands {
|
2024-05-07 19:36:56 +00:00
|
|
|
Optional<ScopedOperand> base {}; // [[Base]]
|
|
|
|
Optional<ScopedOperand> referenced_name {}; // [[ReferencedName]] as an operand
|
2024-02-04 07:00:54 +00:00
|
|
|
Optional<IdentifierTableIndex> referenced_identifier {}; // [[ReferencedName]] as an identifier
|
|
|
|
Optional<IdentifierTableIndex> referenced_private_identifier {}; // [[ReferencedName]] as a private identifier
|
2024-05-07 19:36:56 +00:00
|
|
|
Optional<ScopedOperand> this_value {}; // [[ThisValue]]
|
|
|
|
Optional<ScopedOperand> loaded_value {}; // Loaded value, if we've performed a load.
|
2023-10-04 13:22:07 +00:00
|
|
|
};
|
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
CodeGenerationErrorOr<ReferenceOperands> emit_load_from_reference(JS::ASTNode const&, Optional<ScopedOperand> preferred_dst = {});
|
|
|
|
CodeGenerationErrorOr<void> emit_store_to_reference(JS::ASTNode const&, ScopedOperand value);
|
|
|
|
CodeGenerationErrorOr<void> emit_store_to_reference(ReferenceOperands const&, ScopedOperand value);
|
|
|
|
CodeGenerationErrorOr<Optional<ScopedOperand>> emit_delete_reference(JS::ASTNode const&);
|
2021-10-25 13:16:22 +00:00
|
|
|
|
2024-02-04 07:00:54 +00:00
|
|
|
CodeGenerationErrorOr<ReferenceOperands> emit_super_reference(MemberExpression const&);
|
2023-07-06 21:02:06 +00:00
|
|
|
|
2024-05-14 09:30:30 +00:00
|
|
|
void emit_set_variable(JS::Identifier const& identifier, ScopedOperand value, Bytecode::Op::BindingInitializationMode initialization_mode = Bytecode::Op::BindingInitializationMode::Set, Bytecode::Op::EnvironmentMode mode = Bytecode::Op::EnvironmentMode::Lexical);
|
2023-07-05 00:17:10 +00:00
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
void push_home_object(ScopedOperand);
|
2023-06-16 08:25:05 +00:00
|
|
|
void pop_home_object();
|
2024-05-07 19:36:56 +00:00
|
|
|
void emit_new_function(ScopedOperand dst, JS::FunctionExpression const&, Optional<IdentifierTableIndex> lhs_name);
|
2023-06-23 12:27:42 +00:00
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
CodeGenerationErrorOr<Optional<ScopedOperand>> emit_named_evaluation_if_anonymous_function(Expression const&, Optional<IdentifierTableIndex> lhs_name, Optional<ScopedOperand> preferred_dst = {});
|
2023-06-16 08:25:05 +00:00
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
void begin_continuable_scope(Label continue_target, Vector<DeprecatedFlyString> const& language_label_set);
|
2021-06-06 11:33:02 +00:00
|
|
|
void end_continuable_scope();
|
2023-01-09 00:23:00 +00:00
|
|
|
void begin_breakable_scope(Label breakable_target, Vector<DeprecatedFlyString> const& language_label_set);
|
2021-06-10 12:28:43 +00:00
|
|
|
void end_breakable_scope();
|
2021-06-06 11:33:02 +00:00
|
|
|
|
2021-06-09 02:19:58 +00:00
|
|
|
[[nodiscard]] Label nearest_continuable_scope() const;
|
2021-06-10 12:28:43 +00:00
|
|
|
[[nodiscard]] Label nearest_breakable_scope() const;
|
2021-06-09 02:19:58 +00:00
|
|
|
|
|
|
|
void switch_to_basic_block(BasicBlock& block)
|
|
|
|
{
|
|
|
|
m_current_basic_block = █
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:04:38 +00:00
|
|
|
[[nodiscard]] BasicBlock& current_block() { return *m_current_basic_block; }
|
|
|
|
|
2023-10-27 03:57:51 +00:00
|
|
|
BasicBlock& make_block(String name = {})
|
2021-06-09 02:19:58 +00:00
|
|
|
{
|
|
|
|
if (name.is_empty())
|
2024-10-14 08:05:01 +00:00
|
|
|
name = String::number(m_next_block++);
|
2024-05-06 06:06:56 +00:00
|
|
|
auto block = BasicBlock::create(m_root_basic_blocks.size(), name);
|
2023-10-19 21:18:54 +00:00
|
|
|
if (auto const* context = m_current_unwind_context) {
|
|
|
|
if (context->handler().has_value())
|
2024-05-06 06:06:56 +00:00
|
|
|
block->set_handler(*m_root_basic_blocks[context->handler().value().basic_block_index()]);
|
2023-10-19 21:18:54 +00:00
|
|
|
if (m_current_unwind_context->finalizer().has_value())
|
2024-05-06 06:06:56 +00:00
|
|
|
block->set_finalizer(*m_root_basic_blocks[context->finalizer().value().basic_block_index()]);
|
2023-10-19 21:18:54 +00:00
|
|
|
}
|
|
|
|
m_root_basic_blocks.append(move(block));
|
2023-03-06 16:16:25 +00:00
|
|
|
return *m_root_basic_blocks.last();
|
2021-06-09 02:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_current_block_terminated() const
|
|
|
|
{
|
|
|
|
return m_current_basic_block->is_terminated();
|
|
|
|
}
|
2021-06-06 11:33:02 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
StringTableIndex intern_string(ByteString string)
|
2021-06-09 08:02:01 +00:00
|
|
|
{
|
2021-10-24 13:14:14 +00:00
|
|
|
return m_string_table->insert(move(string));
|
2021-06-09 08:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 08:49:07 +00:00
|
|
|
RegexTableIndex intern_regex(ParsedRegex regex)
|
|
|
|
{
|
|
|
|
return m_regex_table->insert(move(regex));
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
IdentifierTableIndex intern_identifier(DeprecatedFlyString string)
|
2021-10-24 13:34:30 +00:00
|
|
|
{
|
|
|
|
return m_identifier_table->insert(move(string));
|
|
|
|
}
|
|
|
|
|
2024-03-29 15:26:10 +00:00
|
|
|
Optional<IdentifierTableIndex> intern_identifier_for_expression(Expression const& expression);
|
|
|
|
|
2023-07-14 20:44:36 +00:00
|
|
|
bool is_in_generator_or_async_function() const { return m_enclosing_function_kind == FunctionKind::Async || m_enclosing_function_kind == FunctionKind::Generator || m_enclosing_function_kind == FunctionKind::AsyncGenerator; }
|
|
|
|
bool is_in_generator_function() const { return m_enclosing_function_kind == FunctionKind::Generator || m_enclosing_function_kind == FunctionKind::AsyncGenerator; }
|
|
|
|
bool is_in_async_function() const { return m_enclosing_function_kind == FunctionKind::Async || m_enclosing_function_kind == FunctionKind::AsyncGenerator; }
|
2023-07-14 20:45:41 +00:00
|
|
|
bool is_in_async_generator_function() const { return m_enclosing_function_kind == FunctionKind::AsyncGenerator; }
|
2021-06-10 21:08:30 +00:00
|
|
|
|
2022-02-12 16:18:45 +00:00
|
|
|
enum class BindingMode {
|
|
|
|
Lexical,
|
|
|
|
Var,
|
|
|
|
Global,
|
|
|
|
};
|
|
|
|
struct LexicalScope {
|
|
|
|
SurroundingScopeKind kind;
|
|
|
|
};
|
|
|
|
|
2024-05-11 11:25:08 +00:00
|
|
|
// Returns true if a lexical environment was created.
|
|
|
|
bool emit_block_declaration_instantiation(ScopeNode const&);
|
2022-03-14 02:34:01 +00:00
|
|
|
|
2023-06-16 14:34:47 +00:00
|
|
|
void begin_variable_scope();
|
2022-03-14 02:34:01 +00:00
|
|
|
void end_variable_scope();
|
2022-02-12 16:18:45 +00:00
|
|
|
|
2022-03-13 08:21:02 +00:00
|
|
|
enum class BlockBoundaryType {
|
|
|
|
Break,
|
|
|
|
Continue,
|
|
|
|
Unwind,
|
2022-11-13 19:56:53 +00:00
|
|
|
ReturnToFinally,
|
2024-04-11 09:58:18 +00:00
|
|
|
LeaveFinally,
|
2022-03-14 02:34:01 +00:00
|
|
|
LeaveLexicalEnvironment,
|
2022-03-13 08:21:02 +00:00
|
|
|
};
|
|
|
|
template<typename OpType>
|
2022-11-25 15:42:29 +00:00
|
|
|
void perform_needed_unwinds()
|
|
|
|
requires(OpType::IsTerminator && !IsSame<OpType, Op::Jump>)
|
2022-03-13 08:21:02 +00:00
|
|
|
{
|
|
|
|
for (size_t i = m_boundaries.size(); i > 0; --i) {
|
|
|
|
auto boundary = m_boundaries[i - 1];
|
2022-11-13 19:56:53 +00:00
|
|
|
using enum BlockBoundaryType;
|
|
|
|
switch (boundary) {
|
|
|
|
case Unwind:
|
2022-11-25 15:42:29 +00:00
|
|
|
if constexpr (IsSame<OpType, Bytecode::Op::Throw>)
|
|
|
|
return;
|
2022-03-13 08:21:02 +00:00
|
|
|
emit<Bytecode::Op::LeaveUnwindContext>();
|
2022-11-13 19:56:53 +00:00
|
|
|
break;
|
|
|
|
case LeaveLexicalEnvironment:
|
2023-06-16 14:43:24 +00:00
|
|
|
emit<Bytecode::Op::LeaveLexicalEnvironment>();
|
2022-11-13 19:56:53 +00:00
|
|
|
break;
|
|
|
|
case Break:
|
|
|
|
case Continue:
|
|
|
|
break;
|
|
|
|
case ReturnToFinally:
|
|
|
|
return;
|
2024-04-11 09:58:18 +00:00
|
|
|
case LeaveFinally:
|
|
|
|
emit<Bytecode::Op::LeaveFinally>();
|
|
|
|
break;
|
2022-11-13 19:56:53 +00:00
|
|
|
};
|
2022-03-13 08:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:38:43 +00:00
|
|
|
bool is_in_finalizer() const { return m_boundaries.contains_slow(BlockBoundaryType::LeaveFinally); }
|
2024-05-12 09:03:26 +00:00
|
|
|
bool must_enter_finalizer() const { return m_boundaries.contains_slow(BlockBoundaryType::ReturnToFinally); }
|
2024-05-06 20:38:43 +00:00
|
|
|
|
2022-11-25 15:15:34 +00:00
|
|
|
void generate_break();
|
|
|
|
void generate_break(DeprecatedFlyString const& break_label);
|
|
|
|
|
2022-11-25 15:35:39 +00:00
|
|
|
void generate_continue();
|
|
|
|
void generate_continue(DeprecatedFlyString const& continue_label);
|
2022-06-11 22:09:37 +00:00
|
|
|
|
2024-05-12 09:03:26 +00:00
|
|
|
template<typename OpType>
|
|
|
|
void emit_return(ScopedOperand value)
|
|
|
|
requires(IsOneOf<OpType, Op::Return, Op::Yield>)
|
|
|
|
{
|
|
|
|
// FIXME: Tell the call sites about the `saved_return_value` destination
|
|
|
|
// And take that into account in the movs below.
|
|
|
|
perform_needed_unwinds<OpType>();
|
|
|
|
if (must_enter_finalizer()) {
|
|
|
|
VERIFY(m_current_basic_block->finalizer() != nullptr);
|
|
|
|
// Compare to:
|
|
|
|
// * Interpreter::do_return
|
|
|
|
// * Interpreter::run_bytecode::handle_ContinuePendingUnwind
|
|
|
|
// * Return::execute_impl
|
|
|
|
// * Yield::execute_impl
|
2024-05-18 15:25:43 +00:00
|
|
|
if constexpr (IsSame<OpType, Op::Yield>)
|
|
|
|
emit<Bytecode::Op::PrepareYield>(Operand(Register::saved_return_value()), value);
|
|
|
|
else
|
|
|
|
emit<Bytecode::Op::Mov>(Operand(Register::saved_return_value()), value);
|
2024-05-12 09:03:26 +00:00
|
|
|
emit<Bytecode::Op::Mov>(Operand(Register::exception()), add_constant(Value {}));
|
|
|
|
// FIXME: Do we really need to clear the return value register here?
|
|
|
|
emit<Bytecode::Op::Mov>(Operand(Register::return_value()), add_constant(Value {}));
|
|
|
|
emit<Bytecode::Op::Jump>(Label { *m_current_basic_block->finalizer() });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (IsSame<OpType, Op::Return>)
|
|
|
|
emit<Op::Return>(value);
|
|
|
|
else
|
|
|
|
emit<Op::Yield>(nullptr, value);
|
|
|
|
}
|
|
|
|
|
2022-03-13 08:21:02 +00:00
|
|
|
void start_boundary(BlockBoundaryType type) { m_boundaries.append(type); }
|
|
|
|
void end_boundary(BlockBoundaryType type)
|
|
|
|
{
|
|
|
|
VERIFY(m_boundaries.last() == type);
|
|
|
|
m_boundaries.take_last();
|
|
|
|
}
|
|
|
|
|
2024-06-01 08:00:32 +00:00
|
|
|
[[nodiscard]] ScopedOperand copy_if_needed_to_preserve_evaluation_order(ScopedOperand const&);
|
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
[[nodiscard]] ScopedOperand get_this(Optional<ScopedOperand> preferred_dst = {});
|
2024-05-07 10:16:37 +00:00
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
void emit_get_by_id(ScopedOperand dst, ScopedOperand base, IdentifierTableIndex property_identifier, Optional<IdentifierTableIndex> base_identifier = {});
|
2024-02-04 07:00:54 +00:00
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
void emit_get_by_id_with_this(ScopedOperand dst, ScopedOperand base, IdentifierTableIndex, ScopedOperand this_value);
|
2023-07-08 15:43:26 +00:00
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
void emit_iterator_value(ScopedOperand dst, ScopedOperand result);
|
|
|
|
void emit_iterator_complete(ScopedOperand dst, ScopedOperand result);
|
2023-12-07 15:12:12 +00:00
|
|
|
|
2023-07-12 02:06:59 +00:00
|
|
|
[[nodiscard]] size_t next_global_variable_cache() { return m_next_global_variable_cache++; }
|
2023-11-08 19:51:26 +00:00
|
|
|
[[nodiscard]] size_t next_property_lookup_cache() { return m_next_property_lookup_cache++; }
|
2023-07-12 02:06:59 +00:00
|
|
|
|
2024-02-04 07:00:54 +00:00
|
|
|
enum class DeduplicateConstant {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
2024-05-14 12:28:22 +00:00
|
|
|
[[nodiscard]] ScopedOperand add_constant(Value);
|
2024-02-02 08:52:25 +00:00
|
|
|
|
2024-05-31 19:49:46 +00:00
|
|
|
[[nodiscard]] Value get_constant(ScopedOperand const& operand) const
|
|
|
|
{
|
|
|
|
VERIFY(operand.operand().is_constant());
|
|
|
|
return m_constants[operand.operand().index()];
|
|
|
|
}
|
|
|
|
|
2024-04-10 22:25:54 +00:00
|
|
|
UnwindContext const* current_unwind_context() const { return m_current_unwind_context; }
|
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
[[nodiscard]] bool is_finished() const { return m_finished; }
|
|
|
|
|
2024-05-14 08:57:06 +00:00
|
|
|
[[nodiscard]] bool must_propagate_completion() const { return m_must_propagate_completion; }
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
private:
|
2024-03-03 10:34:36 +00:00
|
|
|
VM& m_vm;
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
static CodeGenerationErrorOr<GC::Ref<Executable>> compile(VM&, ASTNode const&, FunctionKind, GC::Ptr<ECMAScriptFunctionObject const>, MustPropagateCompletion, Vector<DeprecatedFlyString> local_variable_names);
|
2024-05-05 20:06:55 +00:00
|
|
|
|
2023-07-19 07:44:10 +00:00
|
|
|
enum class JumpType {
|
|
|
|
Continue,
|
|
|
|
Break,
|
|
|
|
};
|
|
|
|
void generate_scoped_jump(JumpType);
|
2023-07-19 08:06:25 +00:00
|
|
|
void generate_labelled_jump(JumpType, DeprecatedFlyString const& label);
|
2023-07-19 07:44:10 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
Generator(VM&, GC::Ptr<ECMAScriptFunctionObject const>, MustPropagateCompletion);
|
2022-03-14 16:25:06 +00:00
|
|
|
~Generator() = default;
|
2021-06-03 08:46:30 +00:00
|
|
|
|
2021-06-08 22:50:42 +00:00
|
|
|
void grow(size_t);
|
2021-06-03 08:46:30 +00:00
|
|
|
|
2024-05-09 13:13:31 +00:00
|
|
|
// Returns true if a fused instruction was emitted.
|
|
|
|
[[nodiscard]] bool fuse_compare_and_jump(ScopedOperand const& condition, Label true_target, Label false_target);
|
|
|
|
|
2022-06-11 22:09:37 +00:00
|
|
|
struct LabelableScope {
|
|
|
|
Label bytecode_target;
|
2023-01-09 00:23:00 +00:00
|
|
|
Vector<DeprecatedFlyString> language_label_set;
|
2022-06-11 22:09:37 +00:00
|
|
|
};
|
|
|
|
|
2021-06-09 02:19:58 +00:00
|
|
|
BasicBlock* m_current_basic_block { nullptr };
|
2023-09-01 14:53:55 +00:00
|
|
|
ASTNode const* m_current_ast_node { nullptr };
|
2023-10-19 21:18:54 +00:00
|
|
|
UnwindContext const* m_current_unwind_context { nullptr };
|
|
|
|
|
2023-03-06 16:16:25 +00:00
|
|
|
Vector<NonnullOwnPtr<BasicBlock>> m_root_basic_blocks;
|
2021-06-09 08:02:01 +00:00
|
|
|
NonnullOwnPtr<StringTable> m_string_table;
|
2021-10-24 13:34:30 +00:00
|
|
|
NonnullOwnPtr<IdentifierTable> m_identifier_table;
|
2023-07-13 08:49:07 +00:00
|
|
|
NonnullOwnPtr<RegexTable> m_regex_table;
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::MarkedVector<Value> m_constants;
|
2021-06-09 02:19:58 +00:00
|
|
|
|
2024-05-14 12:28:22 +00:00
|
|
|
mutable Optional<ScopedOperand> m_true_constant;
|
|
|
|
mutable Optional<ScopedOperand> m_false_constant;
|
|
|
|
mutable Optional<ScopedOperand> m_null_constant;
|
|
|
|
mutable Optional<ScopedOperand> m_undefined_constant;
|
|
|
|
mutable Optional<ScopedOperand> m_empty_constant;
|
|
|
|
mutable HashMap<i32, ScopedOperand> m_int32_constants;
|
|
|
|
|
2024-05-07 19:36:56 +00:00
|
|
|
ScopedOperand m_accumulator;
|
2024-05-31 18:41:29 +00:00
|
|
|
ScopedOperand m_this_value;
|
2024-05-07 19:36:56 +00:00
|
|
|
Vector<Register> m_free_registers;
|
|
|
|
|
2023-09-26 12:42:30 +00:00
|
|
|
u32 m_next_register { Register::reserved_register_count };
|
2021-06-09 02:19:58 +00:00
|
|
|
u32 m_next_block { 1 };
|
2023-07-08 15:43:26 +00:00
|
|
|
u32 m_next_property_lookup_cache { 0 };
|
2023-07-12 02:06:59 +00:00
|
|
|
u32 m_next_global_variable_cache { 0 };
|
2022-01-14 23:30:02 +00:00
|
|
|
FunctionKind m_enclosing_function_kind { FunctionKind::Normal };
|
2022-06-11 22:09:37 +00:00
|
|
|
Vector<LabelableScope> m_continuable_scopes;
|
|
|
|
Vector<LabelableScope> m_breakable_scopes;
|
2022-03-13 08:21:02 +00:00
|
|
|
Vector<BlockBoundaryType> m_boundaries;
|
2024-05-07 19:36:56 +00:00
|
|
|
Vector<ScopedOperand> m_home_objects;
|
2024-02-04 07:00:54 +00:00
|
|
|
|
|
|
|
HashTable<u32> m_initialized_locals;
|
2024-05-07 19:36:56 +00:00
|
|
|
|
|
|
|
bool m_finished { false };
|
2024-05-14 08:57:06 +00:00
|
|
|
bool m_must_propagate_completion { true };
|
2024-05-31 18:32:29 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<ECMAScriptFunctionObject const> m_function;
|
2024-07-09 09:37:06 +00:00
|
|
|
|
|
|
|
Optional<IdentifierTableIndex> m_length_identifier;
|
2021-06-03 08:46:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|