From 6ae9346cd39bf6fb01a971dea85159cdd842cece Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 4 Jun 2021 12:07:38 +0200 Subject: LibJS: Add basic support for while loops in the bytecode engine This introduces two new instructions: Jump and JumpIfFalse. Jumps are made to a Bytecode::Label, which is a simple object that represents a location in the bytecode stream. Note that you may not always know the target of a jump when adding the jump instruction itself, but we can just update the instruction later on during codegen once we know where the jump target is. The Bytecode::Interpreter now implements jumping via a jump slot that gets checked after each instruction to see if a jump is pending. If not, we just increment the PC as usual. --- Userland/Libraries/LibJS/Bytecode/Label.h | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Userland/Libraries/LibJS/Bytecode/Label.h (limited to 'Userland/Libraries/LibJS/Bytecode/Label.h') diff --git a/Userland/Libraries/LibJS/Bytecode/Label.h b/Userland/Libraries/LibJS/Bytecode/Label.h new file mode 100644 index 0000000000..b9d9a1c82d --- /dev/null +++ b/Userland/Libraries/LibJS/Bytecode/Label.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace JS::Bytecode { + +class Label { +public: + explicit Label(size_t address) + : m_address(address) + { + } + + size_t address() const { return m_address; } + +private: + size_t m_address { 0 }; +}; + +} + +template<> +struct AK::Formatter : AK::Formatter { + void format(FormatBuilder& builder, JS::Bytecode::Label const& value) + { + return AK::Formatter::format(builder, "@{}", value.address()); + } +}; -- cgit v1.2.3