ladybird/Userland/Libraries/LibJS/Bytecode/Generator.h
Andreas Kling e7d69c5d3c LibJS: Devirtualize and pack the bytecode stream :^)
This patch changes the LibJS bytecode to be a stream of instructions
packed one-after-the-other in contiguous memory, instead of a vector
of OwnPtr<Instruction>. This should be a lot more cache-friendly. :^)

Instructions are also devirtualized and instead have a type field
using a new Instruction::Type enum.

To iterate over a bytecode stream, one must now use
Bytecode::InstructionStreamIterator.
2021-06-07 18:11:59 +02:00

59 lines
1.3 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/OwnPtr.h>
#include <LibJS/Bytecode/Label.h>
#include <LibJS/Bytecode/Register.h>
#include <LibJS/Forward.h>
namespace JS::Bytecode {
class Generator {
public:
static OwnPtr<Block> generate(ASTNode const&);
Register allocate_register();
template<typename OpType, typename... Args>
OpType& emit(Args&&... args)
{
void* slot = next_slot();
grow(sizeof(OpType));
new (slot) OpType(forward<Args>(args)...);
return *static_cast<OpType*>(slot);
}
template<typename OpType, typename... Args>
OpType& emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args)
{
void* slot = next_slot();
grow(sizeof(OpType) + extra_register_slots * sizeof(Register));
new (slot) OpType(forward<Args>(args)...);
return *static_cast<OpType*>(slot);
}
Label make_label() const;
void begin_continuable_scope();
void end_continuable_scope();
Label nearest_continuable_scope() const;
private:
Generator();
~Generator();
void grow(size_t);
void* next_slot();
OwnPtr<Block> m_block;
u32 m_next_register { 1 };
Vector<Label> m_continuable_scopes;
};
}