
This commit introduces the concept of an accumulator register to LibJS's bytecode interpreter. The accumulator register is always register 0, and most simple instructions use it for reading and writing. Not only does this slim down the AST, but it also simplifies a lot of the code. For example, the generate_bytecode methods no longer need to return an Optional<Register>, as any opcode which has a "return" value will always put it into the accumulator. This also renames the old Op::Load to Op::LoadImmediate, and uses Op::Load to load from a register into the accumulator. There is also an Op::Store to put the value in the accumulator into another register.
49 lines
1.2 KiB
C++
49 lines
1.2 KiB
C++
/*
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
#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 {
|
|
|
|
using RegisterWindow = Vector<Value>;
|
|
|
|
class Interpreter {
|
|
public:
|
|
explicit Interpreter(GlobalObject&);
|
|
~Interpreter();
|
|
|
|
// FIXME: Remove this thing once we don't need it anymore!
|
|
static Interpreter* current();
|
|
|
|
GlobalObject& global_object() { return m_global_object; }
|
|
VM& vm() { return m_vm; }
|
|
|
|
Value run(Bytecode::Block const&);
|
|
|
|
ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
|
|
Value& reg(Register const& r) { return registers()[r.index()]; }
|
|
|
|
void jump(Label const& label) { m_pending_jump = label.address(); }
|
|
void do_return(Value return_value) { m_return_value = return_value; }
|
|
|
|
private:
|
|
RegisterWindow& registers() { return m_register_windows.last(); }
|
|
|
|
VM& m_vm;
|
|
GlobalObject& m_global_object;
|
|
NonnullOwnPtrVector<RegisterWindow> m_register_windows;
|
|
Optional<size_t> m_pending_jump;
|
|
Value m_return_value;
|
|
};
|
|
|
|
}
|