2022-07-10 09:35:44 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright (c) 2022 merryhime <https://mary.rs>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <variant>
|
|
|
|
|
2023-04-27 22:20:30 +01:00
|
|
|
#include "oaknut/oaknut_exception.hpp"
|
|
|
|
|
2022-07-10 09:35:44 +01:00
|
|
|
namespace oaknut {
|
|
|
|
|
|
|
|
struct Label;
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
constexpr std::uint64_t inverse_mask_from_size(std::size_t size)
|
|
|
|
{
|
|
|
|
return (~std::uint64_t{0}) << size;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::uint64_t mask_from_size(std::size_t size)
|
|
|
|
{
|
|
|
|
return (~std::uint64_t{0}) >> (64 - size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<std::size_t bit_count>
|
|
|
|
constexpr std::uint64_t sign_extend(std::uint64_t value)
|
|
|
|
{
|
|
|
|
static_assert(bit_count != 0, "cannot sign-extend zero-sized value");
|
|
|
|
constexpr size_t shift_amount = 64 - bit_count;
|
|
|
|
return static_cast<std::uint64_t>(static_cast<std::int64_t>(value << shift_amount) >> shift_amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template<std::size_t bitsize, std::size_t alignment>
|
|
|
|
struct AddrOffset {
|
|
|
|
AddrOffset(std::ptrdiff_t diff)
|
|
|
|
: m_payload(encode(diff))
|
|
|
|
{}
|
|
|
|
|
|
|
|
AddrOffset(Label& label)
|
|
|
|
: m_payload(&label)
|
|
|
|
{}
|
|
|
|
|
2024-01-28 14:56:59 +00:00
|
|
|
AddrOffset(const void* ptr)
|
2022-07-10 09:35:44 +01:00
|
|
|
: m_payload(ptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
static std::uint32_t encode(std::ptrdiff_t diff)
|
|
|
|
{
|
|
|
|
const std::uint64_t diff_u64 = static_cast<std::uint64_t>(diff);
|
|
|
|
if (detail::sign_extend<bitsize>(diff_u64) != diff_u64)
|
2023-04-27 22:20:30 +01:00
|
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
2022-07-10 09:35:44 +01:00
|
|
|
if (diff_u64 != (diff_u64 & detail::inverse_mask_from_size(alignment)))
|
2023-04-27 22:20:30 +01:00
|
|
|
throw OaknutException{ExceptionType::OffsetMisaligned};
|
2022-07-10 09:35:44 +01:00
|
|
|
|
|
|
|
return static_cast<std::uint32_t>((diff_u64 & detail::mask_from_size(bitsize)) >> alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename Policy>
|
|
|
|
friend class BasicCodeGenerator;
|
2024-01-28 14:56:59 +00:00
|
|
|
std::variant<std::uint32_t, Label*, const void*> m_payload;
|
2022-07-10 09:35:44 +01:00
|
|
|
};
|
|
|
|
|
2022-11-15 15:36:04 +00:00
|
|
|
template<std::size_t bitsize, std::size_t shift_amount>
|
2022-07-10 09:35:44 +01:00
|
|
|
struct PageOffset {
|
2022-11-26 15:17:46 +00:00
|
|
|
PageOffset(const void* ptr)
|
2022-07-10 09:35:44 +01:00
|
|
|
: m_payload(ptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
PageOffset(Label& label)
|
|
|
|
: m_payload(&label)
|
|
|
|
{}
|
|
|
|
|
|
|
|
static std::uint32_t encode(std::uintptr_t current_addr, std::uintptr_t target)
|
|
|
|
{
|
2024-01-28 14:56:59 +00:00
|
|
|
std::uint64_t diff = static_cast<std::uint64_t>((static_cast<std::int64_t>(target) >> shift_amount) - (static_cast<std::int64_t>(current_addr) >> shift_amount));
|
2022-11-15 15:36:04 +00:00
|
|
|
if (detail::sign_extend<bitsize>(diff) != diff)
|
2023-04-27 22:20:30 +01:00
|
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
2022-11-15 15:36:04 +00:00
|
|
|
diff &= detail::mask_from_size(bitsize);
|
|
|
|
return static_cast<std::uint32_t>(((diff & 3) << (bitsize - 2)) | (diff >> 2));
|
2022-07-10 09:35:44 +01:00
|
|
|
}
|
|
|
|
|
2024-01-28 14:56:59 +00:00
|
|
|
static bool valid(std::uintptr_t current_addr, std::uintptr_t target)
|
|
|
|
{
|
|
|
|
std::uint64_t diff = static_cast<std::uint64_t>((static_cast<std::int64_t>(target) >> shift_amount) - (static_cast<std::int64_t>(current_addr) >> shift_amount));
|
|
|
|
return detail::sign_extend<bitsize>(diff) == diff;
|
|
|
|
}
|
|
|
|
|
2022-07-10 09:35:44 +01:00
|
|
|
private:
|
|
|
|
template<typename Policy>
|
|
|
|
friend class BasicCodeGenerator;
|
2022-11-26 15:17:46 +00:00
|
|
|
std::variant<Label*, const void*> m_payload;
|
2022-07-10 09:35:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<std::size_t bitsize, std::size_t alignment>
|
|
|
|
struct SOffset {
|
|
|
|
SOffset(std::int64_t offset)
|
|
|
|
{
|
|
|
|
const std::uint64_t diff_u64 = static_cast<std::uint64_t>(offset);
|
|
|
|
if (detail::sign_extend<bitsize>(diff_u64) != diff_u64)
|
2023-04-27 22:20:30 +01:00
|
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
2022-07-10 09:35:44 +01:00
|
|
|
if (diff_u64 != (diff_u64 & detail::inverse_mask_from_size(alignment)))
|
2023-04-27 22:20:30 +01:00
|
|
|
throw OaknutException{ExceptionType::OffsetMisaligned};
|
2022-07-10 09:35:44 +01:00
|
|
|
|
|
|
|
m_encoded = static_cast<std::uint32_t>((diff_u64 & detail::mask_from_size(bitsize)) >> alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename Policy>
|
|
|
|
friend class BasicCodeGenerator;
|
|
|
|
std::uint32_t m_encoded;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<std::size_t bitsize, std::size_t alignment>
|
|
|
|
struct POffset {
|
|
|
|
POffset(std::int64_t offset)
|
|
|
|
{
|
|
|
|
const std::uint64_t diff_u64 = static_cast<std::uint64_t>(offset);
|
|
|
|
if (diff_u64 > detail::mask_from_size(bitsize))
|
2023-04-27 22:20:30 +01:00
|
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
2022-07-10 09:35:44 +01:00
|
|
|
if (diff_u64 != (diff_u64 & detail::inverse_mask_from_size(alignment)))
|
2023-04-27 22:20:30 +01:00
|
|
|
throw OaknutException{ExceptionType::OffsetMisaligned};
|
2022-07-10 09:35:44 +01:00
|
|
|
|
|
|
|
m_encoded = static_cast<std::uint32_t>((diff_u64 & detail::mask_from_size(bitsize)) >> alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename Policy>
|
|
|
|
friend class BasicCodeGenerator;
|
|
|
|
std::uint32_t m_encoded;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace oaknut
|