mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00

This patch begins the work of implementing JavaScript execution in a bytecode VM instead of an AST tree-walk interpreter. It's probably quite naive, but we have to start somewhere. The basic idea is that you call Bytecode::Generator::generate() on an AST node and it hands you back a Bytecode::Block filled with instructions that can then be interpreted by a Bytecode::Interpreter. This first version only implements two instructions: Load and Add. :^) Each bytecode block has infinity registers, and the interpreter resizes its register file to fit the block being executed. Two new `js` options are added in this patch as well: `-d` will dump the generated bytecode `-b` will execute the generated bytecode Note that unless `-d` and/or `-b` are specified, none of the bytecode related stuff in LibJS runs at all. This is implemented in parallel with the existing AST interpreter. :^)
52 lines
1 KiB
C++
52 lines
1 KiB
C++
/*
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibJS/Bytecode/Instruction.h>
|
|
#include <LibJS/Bytecode/Register.h>
|
|
#include <LibJS/Heap/Cell.h>
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
namespace JS::Bytecode::Op {
|
|
|
|
class Load final : public Instruction {
|
|
public:
|
|
Load(Register dst, Value value)
|
|
: m_dst(dst)
|
|
, m_value(value)
|
|
{
|
|
}
|
|
|
|
virtual ~Load() override { }
|
|
virtual void execute(Bytecode::Interpreter&) const override;
|
|
virtual String to_string() const override;
|
|
|
|
private:
|
|
Register m_dst;
|
|
Value m_value;
|
|
};
|
|
|
|
class Add final : public Instruction {
|
|
public:
|
|
Add(Register dst, Register src1, Register src2)
|
|
: m_dst(dst)
|
|
, m_src1(src1)
|
|
, m_src2(src2)
|
|
{
|
|
}
|
|
|
|
virtual ~Add() override { }
|
|
virtual void execute(Bytecode::Interpreter&) const override;
|
|
virtual String to_string() const override;
|
|
|
|
private:
|
|
Register m_dst;
|
|
Register m_src1;
|
|
Register m_src2;
|
|
};
|
|
|
|
}
|