Add jit/helpers.hpp

This commit is contained in:
Nikita Lisitsa 2026-07-28 23:21:22 +03:00
parent 09f52ab0d2
commit 877efa6981
2 changed files with 53 additions and 32 deletions

View file

@ -0,0 +1,52 @@
#pragma once
#include <cstdint>
#include <vector>
#include <stdexcept>
#include <format>
namespace pslang::jit
{
inline std::uint8_t operator""_ub(unsigned long long arg)
{
return (std::uint8_t)arg;
}
template <typename T>
static void check_bits(T value, std::uint32_t bits, char const * message)
{
auto mask = (T(1) << bits) - T(1);
bool good = true;
if (value >= T(0))
{
good = value == (value & mask);
}
else
{
good = (value & (~mask)) == (T(-1) & (~mask));
}
if (!good)
{
T min, max;
if (std::is_unsigned_v<T>)
{
min = 0;
max = mask;
}
else
{
max = (T(1) << (bits - 1)) - 1;
min = - (max + 1);
}
throw std::runtime_error(std::format("{}: {} not in range [{}..{})", message, value, min, max));
}
}
template <typename ... Args>
void push_bytes(std::vector<std::uint8_t> & code, Args ... args)
{
(code.insert(code.end(), (std::uint8_t const *)(&args), (std::uint8_t const *)(&args) + sizeof(args)), ...);
}
}

View file

@ -1,40 +1,9 @@
#include <pslang/jit/arch/macos_aarch64/instruction_builder.hpp>
#include <format>
#include <pslang/jit/helpers.hpp>
namespace pslang::jit::macos_aarch64
{
template <typename T>
static void check_bits(T value, std::uint32_t bits, char const * message)
{
auto mask = (T(1) << bits) - T(1);
bool good = true;
if (value >= T(0))
{
good = value == (value & mask);
}
else
{
good = (value & (~mask)) == (T(-1) & (~mask));
}
if (!good)
{
T min, max;
if (std::is_unsigned_v<T>)
{
min = 0;
max = mask;
}
else
{
max = (T(1) << (bits - 1)) - 1;
min = - (max + 1);
}
throw std::runtime_error(std::format("{}: {} not in range [{}..{})", message, value, min, max));
}
}
static constexpr std::uint32_t REG_MASK = 0x1fu;
void instruction_builder::nop()