1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:27:43 +00:00

LibJS: Start fleshing out a bytecode for the JavaScript engine :^)

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. :^)
This commit is contained in:
Andreas Kling 2021-06-03 10:46:30 +02:00
parent f9395efaac
commit 69dddd4ef5
16 changed files with 487 additions and 24 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -37,6 +37,7 @@ class ASTNode : public RefCounted<ASTNode> {
public:
virtual ~ASTNode() { }
virtual Value execute(Interpreter&, GlobalObject&) const = 0;
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const;
virtual void dump(int indent) const;
const SourceRange& source_range() const { return m_source_range; }
@ -96,6 +97,7 @@ public:
virtual Value execute(Interpreter&, GlobalObject&) const override;
virtual void dump(int indent) const override;
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
const Expression& expression() const { return m_expression; };
@ -120,6 +122,7 @@ public:
const NonnullRefPtrVector<Statement>& children() const { return m_children; }
virtual Value execute(Interpreter&, GlobalObject&) const override;
virtual void dump(int indent) const override;
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
void add_variables(NonnullRefPtrVector<VariableDeclaration>);
void add_functions(NonnullRefPtrVector<FunctionDeclaration>);
@ -522,6 +525,7 @@ public:
virtual Value execute(Interpreter&, GlobalObject&) const override;
virtual void dump(int indent) const override;
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
private:
BinaryOp m_op;
@ -630,6 +634,7 @@ public:
virtual Value execute(Interpreter&, GlobalObject&) const override;
virtual void dump(int indent) const override;
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
private:
Value m_value;