ladybird/Userland/Libraries/LibJS/Bytecode/Label.h
Andreas Kling 3a73eb99ac LibJS/Bytecode: Store labels as basic block index during compilation
Instead of storing a BasicBlock* and forcing the size of Label to be
sizeof(BasicBlock*), we now store the basic block index as a u32.

This means the final version of the bytecode is able to keep labels
at sizeof(u32), shrinking the size of many instructions. :^)
2024-05-07 09:15:40 +02:00

44 lines
1,000 B
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Format.h>
namespace JS::Bytecode {
class BasicBlock;
class Label {
public:
explicit Label(BasicBlock const&);
explicit Label(u32 basic_block_index)
: m_address_or_basic_block_index(basic_block_index)
{
}
// Used while compiling.
size_t basic_block_index() const { return m_address_or_basic_block_index; }
// Used after compiling.
size_t address() const { return m_address_or_basic_block_index; }
void set_address(size_t address) { m_address_or_basic_block_index = address; }
private:
u32 m_address_or_basic_block_index { 0 };
};
}
template<>
struct AK::Formatter<JS::Bytecode::Label> : AK::Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Label const& label)
{
return AK::Formatter<FormatString>::format(builder, "@{:x}"sv, label.address());
}
};