2021-06-03 08:46:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-06-09 02:19:58 +00:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2021-06-03 08:46:30 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2021-06-09 02:19:58 +00:00
|
|
|
#include <AK/SinglyLinkedList.h>
|
|
|
|
#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>
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
2021-06-08 22:50:42 +00:00
|
|
|
return *static_cast<OpType*>(slot);
|
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));
|
2021-06-08 22:50:42 +00:00
|
|
|
return *static_cast<OpType*>(slot);
|
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
|
|
|
|
2022-06-11 22:09:37 +00:00
|
|
|
void begin_continuable_scope(Label continue_target, Vector<FlyString> const& language_label_set);
|
2021-06-06 11:33:02 +00:00
|
|
|
void end_continuable_scope();
|
2022-06-11 22:09:37 +00:00
|
|
|
void begin_breakable_scope(Label breakable_target, Vector<FlyString> 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));
|
|
|
|
return m_root_basic_blocks.last();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-24 13:34:30 +00:00
|
|
|
IdentifierTableIndex intern_identifier(FlyString string)
|
|
|
|
{
|
|
|
|
return m_identifier_table->insert(move(string));
|
|
|
|
}
|
|
|
|
|
2021-11-10 21:16:07 +00:00
|
|
|
bool is_in_generator_or_async_function() const { return m_enclosing_function_kind == FunctionKind::Async || m_enclosing_function_kind == FunctionKind::Generator; }
|
|
|
|
bool is_in_generator_function() const { return m_enclosing_function_kind == FunctionKind::Generator; }
|
|
|
|
bool is_in_async_function() const { return m_enclosing_function_kind == FunctionKind::Async; }
|
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;
|
|
|
|
BindingMode mode;
|
|
|
|
HashTable<IdentifierTableIndex> known_bindings;
|
|
|
|
};
|
|
|
|
|
|
|
|
void register_binding(IdentifierTableIndex identifier, BindingMode mode = BindingMode::Lexical)
|
|
|
|
{
|
|
|
|
m_variable_scopes.last_matching([&](auto& x) { return x.mode == BindingMode::Global || x.mode == mode; })->known_bindings.set(identifier);
|
|
|
|
}
|
2022-07-17 18:09:19 +00:00
|
|
|
bool has_binding(IdentifierTableIndex identifier, Optional<BindingMode> const& specific_binding_mode = {}) const
|
2022-02-12 16:18:45 +00:00
|
|
|
{
|
|
|
|
for (auto index = m_variable_scopes.size(); index > 0; --index) {
|
|
|
|
auto& scope = m_variable_scopes[index - 1];
|
|
|
|
|
|
|
|
if (scope.mode != BindingMode::Global && specific_binding_mode.value_or(scope.mode) != scope.mode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (scope.known_bindings.contains(identifier))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-17 18:09:19 +00:00
|
|
|
bool has_binding_in_current_scope(IdentifierTableIndex identifier) const
|
|
|
|
{
|
|
|
|
if (m_variable_scopes.is_empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return m_variable_scopes.last().known_bindings.contains(identifier);
|
|
|
|
}
|
2022-03-14 02:34:01 +00:00
|
|
|
|
|
|
|
void begin_variable_scope(BindingMode mode = BindingMode::Lexical, SurroundingScopeKind kind = SurroundingScopeKind::Block);
|
|
|
|
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,
|
|
|
|
LeaveVariableEnvironment,
|
2022-03-13 08:21:02 +00:00
|
|
|
};
|
|
|
|
template<typename OpType>
|
2022-10-16 22:06:11 +00:00
|
|
|
void perform_needed_unwinds(bool is_break_node = false)
|
|
|
|
requires(OpType::IsTerminator)
|
2022-03-13 08:21:02 +00:00
|
|
|
{
|
|
|
|
Optional<BlockBoundaryType> boundary_to_stop_at;
|
|
|
|
if constexpr (IsSame<OpType, Bytecode::Op::Return> || IsSame<OpType, Bytecode::Op::Yield>)
|
|
|
|
VERIFY(!is_break_node);
|
2022-03-14 02:26:39 +00:00
|
|
|
else if constexpr (IsSame<OpType, Bytecode::Op::Throw>)
|
|
|
|
boundary_to_stop_at = BlockBoundaryType::Unwind;
|
2022-03-13 08:21:02 +00:00
|
|
|
else
|
|
|
|
boundary_to_stop_at = is_break_node ? BlockBoundaryType::Break : BlockBoundaryType::Continue;
|
|
|
|
|
|
|
|
for (size_t i = m_boundaries.size(); i > 0; --i) {
|
|
|
|
auto boundary = m_boundaries[i - 1];
|
|
|
|
if (boundary_to_stop_at.has_value() && boundary == *boundary_to_stop_at)
|
|
|
|
break;
|
2022-11-13 19:56:53 +00:00
|
|
|
using enum BlockBoundaryType;
|
|
|
|
switch (boundary) {
|
|
|
|
case Unwind:
|
2022-03-13 08:21:02 +00:00
|
|
|
emit<Bytecode::Op::LeaveUnwindContext>();
|
2022-11-13 19:56:53 +00:00
|
|
|
break;
|
|
|
|
case LeaveLexicalEnvironment:
|
2022-03-14 02:34:01 +00:00
|
|
|
emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
|
2022-11-13 19:56:53 +00:00
|
|
|
break;
|
|
|
|
case LeaveVariableEnvironment:
|
2022-03-14 02:34:01 +00:00
|
|
|
emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Var);
|
2022-11-13 19:56:53 +00:00
|
|
|
break;
|
|
|
|
case Break:
|
|
|
|
case Continue:
|
|
|
|
break;
|
|
|
|
case ReturnToFinally:
|
|
|
|
// FIXME: In the case of breaks/continues we need to tell the `finally` to break/continue
|
|
|
|
// For now let's ignore the finally to avoid a crash
|
|
|
|
if (IsSame<OpType, Bytecode::Op::Jump>)
|
|
|
|
break;
|
|
|
|
return;
|
|
|
|
};
|
2022-03-13 08:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 22:09:37 +00:00
|
|
|
Label perform_needed_unwinds_for_labelled_break_and_return_target_block(FlyString const& break_label);
|
|
|
|
Label perform_needed_unwinds_for_labelled_continue_and_return_target_block(FlyString const& continue_label);
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
private:
|
|
|
|
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;
|
|
|
|
Vector<FlyString> language_label_set;
|
|
|
|
};
|
|
|
|
|
2021-06-09 02:19:58 +00:00
|
|
|
BasicBlock* m_current_basic_block { nullptr };
|
|
|
|
NonnullOwnPtrVector<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;
|
2021-06-09 02:19:58 +00:00
|
|
|
|
2021-06-09 23:07:53 +00:00
|
|
|
u32 m_next_register { 2 };
|
2021-06-09 02:19:58 +00:00
|
|
|
u32 m_next_block { 1 };
|
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-02-12 16:18:45 +00:00
|
|
|
Vector<LexicalScope> m_variable_scopes;
|
2022-03-13 08:21:02 +00:00
|
|
|
Vector<BlockBoundaryType> m_boundaries;
|
2021-06-03 08:46:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|