dynarmic/include/oaknut/impl/arm64_encode_helpers.inc.hpp
Merry 99c0a73f91 Squashed 'externals/oaknut/' changes from c24f918e5..d0488d932
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
2024-01-28 14:56:59 +00:00

210 lines
7.1 KiB
C++

// SPDX-FileCopyrightText: Copyright (c) 2022 merryhime <https://mary.rs>
// SPDX-License-Identifier: MIT
template<std::uint32_t mask_>
static constexpr std::uint32_t pdep(std::uint32_t val)
{
std::uint32_t mask = mask_;
std::uint32_t res = 0;
for (std::uint32_t bb = 1; mask; bb += bb) {
if (val & bb)
res |= mask & (~mask + 1);
mask &= mask - 1;
}
return res;
}
#define OAKNUT_STD_ENCODE(TYPE, ACCESS, SIZE) \
template<std::uint32_t splat> \
std::uint32_t encode(TYPE v) \
{ \
static_assert(std::popcount(splat) == SIZE); \
return pdep<splat>(static_cast<std::uint32_t>(ACCESS)); \
}
OAKNUT_STD_ENCODE(RReg, v.index() & 31, 5)
OAKNUT_STD_ENCODE(VReg, v.index() & 31, 5)
OAKNUT_STD_ENCODE(VRegArranged, v.index() & 31, 5)
OAKNUT_STD_ENCODE(AddSubImm, v.m_encoded, 13)
OAKNUT_STD_ENCODE(BitImm32, v.m_encoded, 12)
OAKNUT_STD_ENCODE(BitImm64, v.m_encoded, 13)
OAKNUT_STD_ENCODE(LslShift<32>, v.m_encoded, 12)
OAKNUT_STD_ENCODE(LslShift<64>, v.m_encoded, 12)
OAKNUT_STD_ENCODE(FImm8, v.m_encoded, 8)
OAKNUT_STD_ENCODE(RepImm, v.m_encoded, 8)
OAKNUT_STD_ENCODE(Cond, v, 4)
OAKNUT_STD_ENCODE(AddSubExt, v, 3)
OAKNUT_STD_ENCODE(IndexExt, v, 3)
OAKNUT_STD_ENCODE(AddSubShift, v, 2)
OAKNUT_STD_ENCODE(LogShift, v, 2)
OAKNUT_STD_ENCODE(PstateField, v, 6)
OAKNUT_STD_ENCODE(SystemReg, v, 15)
OAKNUT_STD_ENCODE(AtOp, v, 7)
OAKNUT_STD_ENCODE(BarrierOp, v, 4)
OAKNUT_STD_ENCODE(DcOp, v, 10)
OAKNUT_STD_ENCODE(IcOp, v, 10)
OAKNUT_STD_ENCODE(PrfOp, v, 5)
OAKNUT_STD_ENCODE(TlbiOp, v, 10)
template<std::uint32_t splat>
std::uint32_t encode(MovImm16 v)
{
static_assert(std::popcount(splat) == 17 || std::popcount(splat) == 18);
if constexpr (std::popcount(splat) == 17) {
constexpr std::uint32_t mask = (1 << std::popcount(splat)) - 1;
if ((v.m_encoded & mask) != v.m_encoded)
throw OaknutException{ExceptionType::InvalidMovImm16};
}
return pdep<splat>(v.m_encoded);
}
template<std::uint32_t splat, std::size_t imm_size>
std::uint32_t encode(Imm<imm_size> v)
{
static_assert(std::popcount(splat) >= imm_size);
return pdep<splat>(v.value());
}
template<std::uint32_t splat, int A, int B>
std::uint32_t encode(ImmChoice<A, B> v)
{
static_assert(std::popcount(splat) == 1);
return pdep<splat>(v.m_encoded);
}
template<std::uint32_t splat, int A, int B, int C, int D>
std::uint32_t encode(ImmChoice<A, B, C, D> v)
{
static_assert(std::popcount(splat) == 2);
return pdep<splat>(v.m_encoded);
}
template<std::uint32_t splat, std::size_t size, std::size_t align>
std::uint32_t encode(SOffset<size, align> v)
{
static_assert(std::popcount(splat) == size - align);
return pdep<splat>(v.m_encoded);
}
template<std::uint32_t splat, std::size_t size, std::size_t align>
std::uint32_t encode(POffset<size, align> v)
{
static_assert(std::popcount(splat) == size - align);
return pdep<splat>(v.m_encoded);
}
template<std::uint32_t splat>
std::uint32_t encode(std::uint32_t v)
{
return pdep<splat>(v);
}
template<std::uint32_t splat, typename T, size_t N>
std::uint32_t encode(List<T, N> v)
{
return encode<splat>(v.m_base);
}
template<std::uint32_t splat, std::size_t size, std::size_t align>
std::uint32_t encode(AddrOffset<size, align> v)
{
static_assert(std::popcount(splat) == size - align);
const auto encode_fn = [](std::ptrdiff_t current_offset, std::ptrdiff_t target_offset) {
const std::ptrdiff_t diff = target_offset - current_offset;
return pdep<splat>(AddrOffset<size, align>::encode(diff));
};
return std::visit(detail::overloaded{
[&](std::uint32_t encoding) -> std::uint32_t {
return pdep<splat>(encoding);
},
[&](Label* label) -> std::uint32_t {
if (label->m_offset) {
return encode_fn(Policy::offset(), *label->m_offset);
}
label->m_wbs.emplace_back(Label::Writeback{Policy::offset(), ~splat, static_cast<Label::EmitFunctionType>(encode_fn)});
return 0u;
},
[&](const void* p) -> std::uint32_t {
const std::ptrdiff_t diff = reinterpret_cast<std::uintptr_t>(p) - Policy::template xptr<std::uintptr_t>();
return pdep<splat>(AddrOffset<size, align>::encode(diff));
},
},
v.m_payload);
}
template<std::uint32_t splat, std::size_t size, std::size_t shift_amount>
std::uint32_t encode(PageOffset<size, shift_amount> v)
{
static_assert(std::popcount(splat) == size);
const auto encode_fn = [](std::ptrdiff_t current_offset, std::ptrdiff_t target_offset) {
return pdep<splat>(PageOffset<size, shift_amount>::encode(static_cast<std::uintptr_t>(current_offset), static_cast<std::uintptr_t>(target_offset)));
};
return std::visit(detail::overloaded{
[&](Label* label) -> std::uint32_t {
if (label->m_offset) {
return encode_fn(Policy::offset(), *label->m_offset);
}
label->m_wbs.emplace_back(Label::Writeback{Policy::offset(), ~splat, static_cast<Label::EmitFunctionType>(encode_fn)});
return 0u;
},
[&](const void* p) -> std::uint32_t {
return pdep<splat>(PageOffset<size, shift_amount>::encode(Policy::template xptr<std::uintptr_t>(), reinterpret_cast<std::ptrdiff_t>(p)));
},
},
v.m_payload);
}
#undef OAKNUT_STD_ENCODE
void addsubext_lsl_correction(AddSubExt& ext, XRegSp)
{
if (ext == AddSubExt::LSL)
ext = AddSubExt::UXTX;
}
void addsubext_lsl_correction(AddSubExt& ext, WRegWsp)
{
if (ext == AddSubExt::LSL)
ext = AddSubExt::UXTW;
}
void addsubext_lsl_correction(AddSubExt& ext, XReg)
{
if (ext == AddSubExt::LSL)
ext = AddSubExt::UXTX;
}
void addsubext_lsl_correction(AddSubExt& ext, WReg)
{
if (ext == AddSubExt::LSL)
ext = AddSubExt::UXTW;
}
void addsubext_verify_reg_size(AddSubExt ext, RReg rm)
{
if (rm.bitsize() == 32 && (static_cast<int>(ext) & 0b011) != 0b011)
return;
if (rm.bitsize() == 64 && (static_cast<int>(ext) & 0b011) == 0b011)
return;
throw OaknutException{ExceptionType::InvalidAddSubExt};
}
void indexext_verify_reg_size(IndexExt ext, RReg rm)
{
if (rm.bitsize() == 32 && (static_cast<int>(ext) & 1) == 0)
return;
if (rm.bitsize() == 64 && (static_cast<int>(ext) & 1) == 1)
return;
throw OaknutException{ExceptionType::InvalidIndexExt};
}
void tbz_verify_reg_size(RReg rt, Imm<6> imm)
{
if (rt.bitsize() == 32 && imm.value() >= 32)
throw OaknutException{ExceptionType::BitPositionOutOfRange};
}