1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +00:00

LibJS/Bytecode: Implement break/continue labels

This is done by keeping track of all the labels that apply to a given
break/continue scope alongside their bytecode target. When a
break/continue with a label is generated, we scan from the most inner
scope to the most outer scope looking for the label, performing any
necessary unwinds on the way. Once the label is found, it is then
jumped to.
This commit is contained in:
Luke Wilde 2022-06-11 23:09:37 +01:00 committed by Ali Mohammad Pur
parent 455c0b7794
commit c0fadfb9b7
4 changed files with 218 additions and 30 deletions

View file

@ -81,9 +81,9 @@ public:
CodeGenerationErrorOr<void> emit_store_to_reference(JS::ASTNode const&);
CodeGenerationErrorOr<void> emit_delete_reference(JS::ASTNode const&);
void begin_continuable_scope(Label continue_target);
void begin_continuable_scope(Label continue_target, Vector<FlyString> const& language_label_set);
void end_continuable_scope();
void begin_breakable_scope(Label breakable_target);
void begin_breakable_scope(Label breakable_target, Vector<FlyString> const& language_label_set);
void end_breakable_scope();
[[nodiscard]] Label nearest_continuable_scope() const;
@ -186,6 +186,9 @@ public:
}
}
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);
void start_boundary(BlockBoundaryType type) { m_boundaries.append(type); }
void end_boundary(BlockBoundaryType type)
{
@ -200,6 +203,11 @@ private:
void grow(size_t);
void* next_slot();
struct LabelableScope {
Label bytecode_target;
Vector<FlyString> language_label_set;
};
BasicBlock* m_current_basic_block { nullptr };
NonnullOwnPtrVector<BasicBlock> m_root_basic_blocks;
NonnullOwnPtr<StringTable> m_string_table;
@ -208,8 +216,8 @@ private:
u32 m_next_register { 2 };
u32 m_next_block { 1 };
FunctionKind m_enclosing_function_kind { FunctionKind::Normal };
Vector<Label> m_continuable_scopes;
Vector<Label> m_breakable_scopes;
Vector<LabelableScope> m_continuable_scopes;
Vector<LabelableScope> m_breakable_scopes;
Vector<LexicalScope> m_variable_scopes;
Vector<BlockBoundaryType> m_boundaries;
};