2021-06-03 08:46:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* 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:
|
2022-02-12 16:18:45 +00:00
|
|
|
enum class SurroundingScopeKind {
|
|
|
|
Global,
|
|
|
|
Function,
|
|
|
|
Block,
|
|
|
|
};
|
2022-02-12 16:24:08 +00:00
|
|
|
static CodeGenerationErrorOr<NonnullOwnPtr<Executable>> generate(ASTNode const&, FunctionKind = FunctionKind::Normal);
|
2021-06-03 08:46:30 +00:00
|
|
|
|
|
|
|
Register allocate_register();
|
|
|
|
|
2021-06-10 21:05:01 +00:00
|
|
|
void ensure_enough_space(size_t size)
|
|
|
|
{
|
|
|
|
// Make sure there's always enough space for a single jump at the end.
|
|
|
|
if (!m_current_basic_block->can_grow(size + sizeof(Op::Jump))) {
|
|
|
|
auto& new_block = make_block();
|
|
|
|
emit<Op::Jump>().set_targets(
|
|
|
|
Label { new_block },
|
|
|
|
{});
|
|
|
|
switch_to_basic_block(new_block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 };
|
|
|
|
};
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
template<typename OpType, typename... Args>
|
2021-06-08 22:50:42 +00:00
|
|
|
OpType& emit(Args&&... args)
|
2021-06-03 08:46:30 +00:00
|
|
|
{
|
2021-06-09 02:19:58 +00:00
|
|
|
VERIFY(!is_current_block_terminated());
|
2021-06-10 21:05:01 +00:00
|
|
|
// If the block doesn't have enough space, switch to another block
|
|
|
|
if constexpr (!OpType::IsTerminator)
|
|
|
|
ensure_enough_space(sizeof(OpType));
|
|
|
|
|
2021-06-08 22:50:42 +00:00
|
|
|
void* slot = next_slot();
|
|
|
|
grow(sizeof(OpType));
|
|
|
|
new (slot) OpType(forward<Args>(args)...);
|
2021-06-09 02:19:58 +00:00
|
|
|
if constexpr (OpType::IsTerminator)
|
2022-11-26 13:42:36 +00:00
|
|
|
m_current_basic_block->terminate({}, static_cast<Instruction const*>(slot));
|
2023-09-01 14:53:55 +00:00
|
|
|
auto* op = static_cast<OpType*>(slot);
|
|
|
|
op->set_source_record({ m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
|
|
|
return *op;
|
2021-06-07 13:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OpType, typename... Args>
|
2021-06-08 22:50:42 +00:00
|
|
|
OpType& emit_with_extra_register_slots(size_t extra_register_slots, 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
|
|
|
|
|
|
|
size_t size_to_allocate = round_up_to_power_of_two(sizeof(OpType) + extra_register_slots * sizeof(Register), alignof(void*));
|
|
|
|
|
2021-06-10 21:05:01 +00:00
|
|
|
// If the block doesn't have enough space, switch to another block
|
|
|
|
if constexpr (!OpType::IsTerminator)
|
2022-09-09 14:47:42 +00:00
|
|
|
ensure_enough_space(size_to_allocate);
|
2021-06-10 21:05:01 +00:00
|
|
|
|
2021-06-08 22:50:42 +00:00
|
|
|
void* slot = next_slot();
|
2022-09-09 14:47:42 +00:00
|
|
|
grow(size_to_allocate);
|
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)
|
2022-11-26 13:42:36 +00:00
|
|
|
m_current_basic_block->terminate({}, static_cast<Instruction const*>(slot));
|
2023-09-01 14:53:55 +00:00
|
|
|
auto* op = static_cast<OpType*>(slot);
|
|
|
|
op->set_source_record({ m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
|
|
|
return *op;
|
2021-06-03 08:46:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 16:24:08 +00:00
|
|
|
CodeGenerationErrorOr<void> emit_load_from_reference(JS::ASTNode const&);
|
|
|
|
CodeGenerationErrorOr<void> emit_store_to_reference(JS::ASTNode const&);
|
2022-03-27 18:50:09 +00:00
|
|
|
CodeGenerationErrorOr<void> emit_delete_reference(JS::ASTNode const&);
|
2021-10-25 13:16:22 +00:00
|
|
|
|
2023-07-06 21:02:06 +00:00
|
|
|
struct ReferenceRegisters {
|
|
|
|
Register base; // [[Base]]
|
|
|
|
Optional<Bytecode::Register> referenced_name; // [[ReferencedName]]
|
|
|
|
Register this_value; // [[ThisValue]]
|
|
|
|
};
|
|
|
|
CodeGenerationErrorOr<ReferenceRegisters> emit_super_reference(MemberExpression const&);
|
|
|
|
|
2023-07-05 00:17:10 +00:00
|
|
|
void emit_set_variable(JS::Identifier const& identifier, Bytecode::Op::SetVariable::InitializationMode initialization_mode = Bytecode::Op::SetVariable::InitializationMode::Set, Bytecode::Op::EnvironmentMode mode = Bytecode::Op::EnvironmentMode::Lexical);
|
|
|
|
|
2023-06-16 08:25:05 +00:00
|
|
|
void push_home_object(Register);
|
|
|
|
void pop_home_object();
|
2023-06-28 16:17:13 +00:00
|
|
|
void emit_new_function(JS::FunctionExpression const&, Optional<IdentifierTableIndex> lhs_name);
|
2023-06-23 12:27:42 +00:00
|
|
|
|
2023-06-28 16:17:13 +00:00
|
|
|
CodeGenerationErrorOr<void> emit_named_evaluation_if_anonymous_function(Expression const&, Optional<IdentifierTableIndex> lhs_name);
|
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; }
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
BasicBlock& make_block(DeprecatedString name = {})
|
2021-06-09 02:19:58 +00:00
|
|
|
{
|
|
|
|
if (name.is_empty())
|
2022-12-04 18:02:33 +00:00
|
|
|
name = DeprecatedString::number(m_next_block++);
|
2021-06-09 02:19:58 +00:00
|
|
|
m_root_basic_blocks.append(BasicBlock::create(name));
|
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
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
StringTableIndex intern_string(DeprecatedString 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));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-06-16 14:34:47 +00:00
|
|
|
void 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,
|
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;
|
|
|
|
};
|
2022-03-13 08:21:02 +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
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-07-08 15:43:26 +00:00
|
|
|
void emit_get_by_id(IdentifierTableIndex);
|
|
|
|
void emit_get_by_id_with_this(IdentifierTableIndex, Register);
|
|
|
|
|
2023-07-12 02:06:59 +00:00
|
|
|
[[nodiscard]] size_t next_global_variable_cache() { return m_next_global_variable_cache++; }
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
private:
|
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
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
Generator();
|
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);
|
|
|
|
void* next_slot();
|
2021-06-03 08:46:30 +00:00
|
|
|
|
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-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;
|
2021-06-09 02:19:58 +00:00
|
|
|
|
2023-09-26 09:57:42 +00:00
|
|
|
u32 m_next_register { 3 };
|
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;
|
2023-06-16 08:25:05 +00:00
|
|
|
Vector<Register> m_home_objects;
|
2021-06-03 08:46:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|