dynarmic/src/frontend/imm.h

161 lines
3.9 KiB
C
Raw Normal View History

2018-01-04 23:05:27 +00:00
/* This file is part of the dynarmic project.
* Copyright (c) 2018 MerryMage
2020-04-23 15:25:11 +01:00
* SPDX-License-Identifier: 0BSD
2018-01-04 23:05:27 +00:00
*/
#pragma once
#include "common/assert.h"
#include "common/bit_util.h"
#include "common/common_types.h"
#include "common/math_util.h"
2018-01-04 23:05:27 +00:00
namespace Dynarmic {
2018-01-04 23:05:27 +00:00
/**
* Imm represents an immediate value in an AArch32/AArch64 instruction.
2018-01-04 23:05:27 +00:00
* Imm is used during translation as a typesafe way of passing around immediates of fixed sizes.
*/
template <size_t bit_size_>
class Imm {
public:
static constexpr size_t bit_size = bit_size_;
explicit Imm(u32 value) : value(value) {
ASSERT_MSG((Common::Bits<0, bit_size-1>(value) == value), "More bits in value than expected");
}
template <typename T = u32>
T ZeroExtend() const {
2018-01-07 00:11:57 +00:00
static_assert(Common::BitSize<T>() >= bit_size);
return static_cast<T>(value);
2018-01-04 23:05:27 +00:00
}
template <typename T = s32>
T SignExtend() const {
2018-01-07 00:11:57 +00:00
static_assert(Common::BitSize<T>() >= bit_size);
return Common::SignExtend<bit_size, T>(value);
2018-01-04 23:05:27 +00:00
}
template <size_t bit>
bool Bit() const {
static_assert(bit < bit_size);
return Common::Bit<bit>(value);
}
template <size_t begin_bit, size_t end_bit, typename T = u32>
T Bits() const {
static_assert(begin_bit <= end_bit && end_bit < bit_size);
static_assert(Common::BitSize<T>() >= end_bit - begin_bit + 1);
return static_cast<T>(Common::Bits<begin_bit, end_bit>(value));
}
bool operator==(Imm other) const {
2018-01-04 23:05:27 +00:00
return value == other.value;
}
bool operator!=(Imm other) const {
return !operator==(other);
}
bool operator<(Imm other) const {
return value < other.value;
}
bool operator<=(Imm other) const {
return value <= other.value;
}
bool operator>(Imm other) const {
return value > other.value;
}
bool operator>=(Imm other) const {
return value >= other.value;
2018-01-04 23:05:27 +00:00
}
private:
static_assert(bit_size != 0, "Cannot have a zero-sized immediate");
static_assert(bit_size <= 32, "Cannot have an immediate larger than the instruction size");
u32 value;
};
template <size_t bit_size>
bool operator==(u32 a, Imm<bit_size> b) {
2018-01-04 23:05:27 +00:00
return Imm<bit_size>{a} == b;
}
template <size_t bit_size>
bool operator==(Imm<bit_size> a, u32 b) {
2018-01-04 23:05:27 +00:00
return Imm<bit_size>{b} == a;
}
template <size_t bit_size>
bool operator!=(u32 a, Imm<bit_size> b) {
return !operator==(a, b);
}
template <size_t bit_size>
bool operator!=(Imm<bit_size> a, u32 b) {
return !operator==(a, b);
}
template <size_t bit_size>
bool operator<(u32 a, Imm<bit_size> b) {
return Imm<bit_size>{a} < b;
}
template <size_t bit_size>
bool operator<(Imm<bit_size> a, u32 b) {
return a < Imm<bit_size>{b};
}
template <size_t bit_size>
bool operator<=(u32 a, Imm<bit_size> b) {
return !operator<(b, a);
}
template <size_t bit_size>
bool operator<=(Imm<bit_size> a, u32 b) {
return !operator<(b, a);
}
template <size_t bit_size>
bool operator>(u32 a, Imm<bit_size> b) {
return operator<(b, a);
}
template <size_t bit_size>
bool operator>(Imm<bit_size> a, u32 b) {
return operator<(b, a);
}
template <size_t bit_size>
bool operator>=(u32 a, Imm<bit_size> b) {
return !operator<(a, b);
2018-01-04 23:05:27 +00:00
}
template <size_t bit_size>
bool operator>=(Imm<bit_size> a, u32 b) {
return !operator<(a, b);
2018-01-04 23:05:27 +00:00
}
/**
* Concatenate immediates together.
* Left to right corresponds to most significant imm to least significant imm.
2018-01-04 23:05:27 +00:00
* This is equivalent to a:b:...:z in ASL.
*/
template <size_t first_bit_size, size_t ...rest_bit_sizes>
auto concatenate(Imm<first_bit_size> first, Imm<rest_bit_sizes> ...rest) {
2018-01-04 23:05:27 +00:00
if constexpr (sizeof...(rest) == 0) {
return first;
} else {
const auto concat_rest = concatenate(rest...);
const u32 value = (first.ZeroExtend() << concat_rest.bit_size) | concat_rest.ZeroExtend();
return Imm<Common::Sum(first_bit_size, rest_bit_sizes...)>{value};
2018-01-04 23:05:27 +00:00
}
}
} // namespace Dynarmic