2024-02-02 10:19:17 +01:00
|
|
|
/*
|
2025-11-22 12:27:51 +01:00
|
|
|
* Copyright (c) 2024-2025, Andreas Kling <andreas@ladybird.org>
|
2024-02-02 10:19:17 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
class Operand {
|
2024-02-02 10:19:17 +01:00
|
|
|
public:
|
2025-11-22 12:27:51 +01:00
|
|
|
enum class Type {
|
2024-02-02 10:19:17 +01:00
|
|
|
Register,
|
|
|
|
|
Local,
|
|
|
|
|
Constant,
|
2025-04-24 22:10:41 +02:00
|
|
|
Argument,
|
2024-02-02 10:19:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] bool operator==(Operand const&) const = default;
|
|
|
|
|
|
|
|
|
|
explicit Operand(Type type, u32 index)
|
2025-11-22 12:27:51 +01:00
|
|
|
: m_raw(to_underlying(type) << 29 | index)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum class ShouldMakeInvalid { Indeed };
|
|
|
|
|
explicit Operand(ShouldMakeInvalid)
|
|
|
|
|
: m_raw(0xffffffffu)
|
2024-02-02 10:19:17 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit Operand(Register);
|
|
|
|
|
|
2025-11-22 12:27:51 +01:00
|
|
|
[[nodiscard]] bool is_invalid() const { return m_raw == 0xffffffffu; }
|
|
|
|
|
[[nodiscard]] bool is_register() const { return type() == Type::Register; }
|
|
|
|
|
[[nodiscard]] bool is_local() const { return type() == Type::Local; }
|
|
|
|
|
[[nodiscard]] bool is_constant() const { return type() == Type::Constant; }
|
2024-02-02 10:19:17 +01:00
|
|
|
|
2025-11-22 12:27:51 +01:00
|
|
|
[[nodiscard]] Type type() const { return static_cast<Type>((m_raw & 0xe0000000u) >> 29); }
|
|
|
|
|
[[nodiscard]] u32 index() const { return m_raw & 0x1fffffff; }
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] u32 raw() const { return m_raw; }
|
2024-02-02 10:19:17 +01:00
|
|
|
|
2024-05-07 21:36:56 +02:00
|
|
|
[[nodiscard]] Register as_register() const;
|
|
|
|
|
|
2025-11-22 12:27:51 +01:00
|
|
|
void offset_index_by(u32 offset)
|
|
|
|
|
{
|
|
|
|
|
m_raw &= 0x1fffffff;
|
|
|
|
|
m_raw += offset;
|
|
|
|
|
}
|
2024-05-12 18:49:03 +02:00
|
|
|
|
2024-02-02 10:19:17 +01:00
|
|
|
private:
|
2025-11-22 12:27:51 +01:00
|
|
|
u32 m_raw { 0 };
|
2024-02-02 10:19:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2025-03-27 15:11:15 +00:00
|
|
|
|
|
|
|
|
namespace AK {
|
2025-05-13 07:06:33 -04:00
|
|
|
|
2025-03-27 15:11:15 +00:00
|
|
|
template<>
|
|
|
|
|
class Optional<JS::Bytecode::Operand> : public OptionalBase<JS::Bytecode::Operand> {
|
|
|
|
|
template<typename U>
|
|
|
|
|
friend class Optional;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using ValueType = JS::Bytecode::Operand;
|
|
|
|
|
|
|
|
|
|
Optional() = default;
|
|
|
|
|
|
|
|
|
|
template<SameAs<OptionalNone> V>
|
|
|
|
|
Optional(V) { }
|
|
|
|
|
|
|
|
|
|
Optional(Optional<JS::Bytecode::Operand> const& other)
|
|
|
|
|
{
|
|
|
|
|
if (other.has_value())
|
|
|
|
|
m_value = other.m_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional(Optional&& other)
|
|
|
|
|
: m_value(other.m_value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename U = JS::Bytecode::Operand>
|
|
|
|
|
requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
|
|
|
|
|
explicit(!IsConvertible<U&&, JS::Bytecode::Operand>) Optional(U&& value)
|
|
|
|
|
requires(!IsSame<RemoveCVReference<U>, Optional<JS::Bytecode::Operand>> && IsConstructible<JS::Bytecode::Operand, U &&>)
|
|
|
|
|
: m_value(forward<U>(value))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<SameAs<OptionalNone> V>
|
|
|
|
|
Optional& operator=(V)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional& operator=(Optional const& other)
|
|
|
|
|
{
|
|
|
|
|
if (this != &other) {
|
|
|
|
|
clear();
|
|
|
|
|
m_value = other.m_value;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional& operator=(Optional&& other)
|
|
|
|
|
{
|
|
|
|
|
if (this != &other) {
|
|
|
|
|
clear();
|
|
|
|
|
m_value = other.m_value;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
{
|
2025-11-22 12:27:51 +01:00
|
|
|
m_value = JS::Bytecode::Operand { JS::Bytecode::Operand::ShouldMakeInvalid::Indeed };
|
2025-03-27 15:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] bool has_value() const
|
|
|
|
|
{
|
2025-11-22 12:27:51 +01:00
|
|
|
return !m_value.is_invalid();
|
2025-03-27 15:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] JS::Bytecode::Operand& value() &
|
|
|
|
|
{
|
|
|
|
|
VERIFY(has_value());
|
|
|
|
|
return m_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] JS::Bytecode::Operand const& value() const&
|
|
|
|
|
{
|
|
|
|
|
VERIFY(has_value());
|
|
|
|
|
return m_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] JS::Bytecode::Operand value() &&
|
|
|
|
|
{
|
|
|
|
|
return release_value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] JS::Bytecode::Operand release_value()
|
|
|
|
|
{
|
|
|
|
|
VERIFY(has_value());
|
|
|
|
|
JS::Bytecode::Operand released_value = m_value;
|
|
|
|
|
clear();
|
|
|
|
|
return released_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2025-11-22 12:27:51 +01:00
|
|
|
JS::Bytecode::Operand m_value { JS::Bytecode::Operand::ShouldMakeInvalid::Indeed };
|
2025-03-27 15:11:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|