2021-06-03 10:46:30 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/OwnPtr.h>
|
2021-06-08 14:46:46 -07:00
|
|
|
#include <LibJS/Bytecode/Block.h>
|
2021-06-04 12:07:38 +02:00
|
|
|
#include <LibJS/Bytecode/Label.h>
|
2021-06-03 10:46:30 +02:00
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
|
|
|
|
|
class Generator {
|
|
|
|
|
public:
|
|
|
|
|
static OwnPtr<Block> generate(ASTNode const&);
|
|
|
|
|
|
|
|
|
|
Register allocate_register();
|
|
|
|
|
|
|
|
|
|
template<typename OpType, typename... Args>
|
2021-06-08 14:46:46 -07:00
|
|
|
InstructionHandle<OpType> emit(Args&&... args)
|
2021-06-03 10:46:30 +02:00
|
|
|
{
|
2021-06-08 14:46:46 -07:00
|
|
|
return make_instruction<OpType>(0, forward<Args>(args)...);
|
2021-06-07 15:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename OpType, typename... Args>
|
2021-06-08 14:46:46 -07:00
|
|
|
InstructionHandle<OpType> emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args)
|
2021-06-07 15:12:43 +02:00
|
|
|
{
|
2021-06-08 14:46:46 -07:00
|
|
|
return make_instruction<OpType>(extra_register_slots, forward<Args>(args)...);
|
2021-06-03 10:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 12:07:38 +02:00
|
|
|
Label make_label() const;
|
|
|
|
|
|
2021-06-06 13:33:02 +02:00
|
|
|
void begin_continuable_scope();
|
|
|
|
|
void end_continuable_scope();
|
|
|
|
|
|
|
|
|
|
Label nearest_continuable_scope() const;
|
|
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
private:
|
|
|
|
|
Generator();
|
|
|
|
|
~Generator();
|
|
|
|
|
|
2021-06-08 14:46:46 -07:00
|
|
|
template<typename OpType, typename... Args>
|
|
|
|
|
InstructionHandle<OpType> make_instruction(size_t extra_register_slots, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
auto& buffer = m_block->buffer();
|
|
|
|
|
auto offset = buffer.size();
|
|
|
|
|
buffer.resize(buffer.size() + sizeof(OpType) + extra_register_slots * sizeof(Register));
|
|
|
|
|
new (buffer.data() + offset) OpType(forward<Args>(args)...);
|
|
|
|
|
return InstructionHandle<OpType>(offset, m_block);
|
|
|
|
|
}
|
2021-06-03 10:46:30 +02:00
|
|
|
|
|
|
|
|
OwnPtr<Block> m_block;
|
|
|
|
|
u32 m_next_register { 1 };
|
2021-06-06 13:33:02 +02:00
|
|
|
Vector<Label> m_continuable_scopes;
|
2021-06-03 10:46:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|