
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.
38 lines
810 B
C++
38 lines
810 B
C++
/*
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibJS/Bytecode/Label.h>
|
|
#include <LibJS/Bytecode/Register.h>
|
|
#include <LibJS/Forward.h>
|
|
#include <LibJS/Heap/Cell.h>
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
class Interpreter {
|
|
public:
|
|
explicit Interpreter(GlobalObject&);
|
|
~Interpreter();
|
|
|
|
GlobalObject& global_object() { return m_global_object; }
|
|
VM& vm() { return m_vm; }
|
|
|
|
void run(Bytecode::Block const&);
|
|
|
|
Value& reg(Register const& r) { return m_registers[r.index()]; }
|
|
|
|
void jump(Label const& label) { m_pending_jump = label.address(); }
|
|
|
|
private:
|
|
VM& m_vm;
|
|
GlobalObject& m_global_object;
|
|
Vector<Value> m_registers;
|
|
Optional<size_t> m_pending_jump;
|
|
};
|
|
|
|
}
|