99c0a73f91
d0488d932 oaknut: 2.0.0 40ad78bbf oaknut: Implement DualCodeBlock and related support 9f131cfb5 oaknut: add configuration for standalone installation 69799b43c oaknut: Test building for Android on CI 1d51f5512 oaknut: 1.2.2 918bd94f0 oaknut: Eliminate -Wconversion warnings 316d8869e oaknut: Fix edgecases in MOVP2R on +/-4GiB boundary d8634eaa1 oaknut: Fix page boundary error in ADP d0ca9a24e oaknut: Update README examples for CPU feature detection dbeec268b oaknut: feature_detection_freebsd: Warn about incompatibility with earlier FreeBSD versions 86e5386e2 oaknut: feature_detect: Support NetBSD df4cf2d48 oaknut: feature_detect: Support OpenBSD 99dfff25a oaknut: feature_detection: Read ID registers 319b3d2c9 oaknut: Add basic CPU feature detection 23e9ddb4c oaknut: CI: Don't run slow tests on OpenBSD 734f1bdb4 oaknut: CI: Use up-to-date qemu f462c9774 oaknut: CI: Build on OpenBSD 19cd42204 oaknut: code_block: Add NetBSD and OpenBSD support 18b86a3ec oaknut: SystemReg: Add more EL0 accessible registers 53c43bf0c oaknut/tests: Reduce iterations for MOVP2R cc37df19e oaknut: Test on FreeBSD a66b32d26 oaknut: Fix crossing sign boundary in PageOffset 206468d72 oaknut: CI: Add macos-arm64 build e6eecc3f9 oaknut: 1.2.1 4252d8f4a oaknut: CMakeLists: Warnings are errors on MSVC 408eed65f oaknut: arm64_encode_helpers: remove unreachable code bfc8eedfb oaknut: arm64_encode_helpers: p maybe unused ff4456eca oaknut: Avoid negation of unsigned values b4ac8fd6c oaknut: Fix MOV for applications of MOVN 0575cadc4 oaknut: Disable certain functionality where absolute addressing is not available 394a3c8f0 oaknut: Appease MSVC 011183670 oaknut: 1.2.0 e83c9f327 oaknut: Add VectorCodeGenerator 5eb122cc5 oaknut: Tidy up public header 45c5a7b25 oaknut: Fix clang-format errors 36243256f oaknut: Add `const` qualifier to `AddrOffset` ctor 4af500cb5 oaknut: Add `ptr` accessor to `Label` bccb06669 oaknut: CodeGenerator const correctness da0590a86 oaknut: github: Update package repositories git-subtree-dir: externals/oaknut git-subtree-split: d0488d9320ae673167dd9117223e3453d5ff102f
138 lines
4.3 KiB
C++
138 lines
4.3 KiB
C++
// SPDX-FileCopyrightText: Copyright (c) 2022 merryhime <https://mary.rs>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <variant>
|
|
|
|
#include "oaknut/oaknut_exception.hpp"
|
|
|
|
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)
|
|
{}
|
|
|
|
AddrOffset(const void* ptr)
|
|
: 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)
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
|
if (diff_u64 != (diff_u64 & detail::inverse_mask_from_size(alignment)))
|
|
throw OaknutException{ExceptionType::OffsetMisaligned};
|
|
|
|
return static_cast<std::uint32_t>((diff_u64 & detail::mask_from_size(bitsize)) >> alignment);
|
|
}
|
|
|
|
private:
|
|
template<typename Policy>
|
|
friend class BasicCodeGenerator;
|
|
std::variant<std::uint32_t, Label*, const void*> m_payload;
|
|
};
|
|
|
|
template<std::size_t bitsize, std::size_t shift_amount>
|
|
struct PageOffset {
|
|
PageOffset(const void* ptr)
|
|
: m_payload(ptr)
|
|
{}
|
|
|
|
PageOffset(Label& label)
|
|
: m_payload(&label)
|
|
{}
|
|
|
|
static std::uint32_t encode(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));
|
|
if (detail::sign_extend<bitsize>(diff) != diff)
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
|
diff &= detail::mask_from_size(bitsize);
|
|
return static_cast<std::uint32_t>(((diff & 3) << (bitsize - 2)) | (diff >> 2));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private:
|
|
template<typename Policy>
|
|
friend class BasicCodeGenerator;
|
|
std::variant<Label*, const void*> m_payload;
|
|
};
|
|
|
|
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)
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
|
if (diff_u64 != (diff_u64 & detail::inverse_mask_from_size(alignment)))
|
|
throw OaknutException{ExceptionType::OffsetMisaligned};
|
|
|
|
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))
|
|
throw OaknutException{ExceptionType::OffsetOutOfRange};
|
|
if (diff_u64 != (diff_u64 & detail::inverse_mask_from_size(alignment)))
|
|
throw OaknutException{ExceptionType::OffsetMisaligned};
|
|
|
|
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
|