Skip to content

Commit

Permalink
Cast all values to int64_t for Imm, simplifies passing some values
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Aug 23, 2024
1 parent a18fa34 commit 9cf110f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
23 changes: 23 additions & 0 deletions tests/src/tests/tests.serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1365,4 +1365,27 @@ namespace zasm::tests
}
}

TEST(SerializationTests, Issue_138)
{
Program program(zasm::MachineMode::AMD64);
x86::Assembler assembler(program);

assembler.mov(x86::ecx, Imm(0xFFFFFFFF));

zasm::Serializer serializer{};
ASSERT_EQ(serializer.serialize(program, 0x140015000), ErrorCode::None);

const std::array<uint8_t, 5> expected = {
0xB9, 0xFF, 0xFF, 0xFF, 0xFF,
};
ASSERT_EQ(serializer.getCodeSize(), expected.size());

const auto* data = serializer.getCode();
ASSERT_NE(data, nullptr);
for (std::size_t i = 0; i < expected.size(); i++)
{
ASSERT_EQ(data[i], expected[i]);
}
}

} // namespace zasm::tests
34 changes: 15 additions & 19 deletions zasm/include/zasm/base/immediate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,65 @@

namespace zasm
{
class Imm
class Imm
{
union
{
std::int64_t s;
std::uint64_t u;
};
std::int64_t _value;

public:
constexpr Imm() noexcept
: s{}
: _value{}
{
}
constexpr Imm(std::uint32_t imm) noexcept
: u{ imm }
: _value{ static_cast<std::int32_t>(imm) }
{
}
constexpr Imm(std::int32_t imm) noexcept
: s{ imm }
: _value{ imm }
{
}
constexpr Imm(std::int64_t imm) noexcept
: s{ imm }
: _value{ imm }
{
}
constexpr Imm(std::uint64_t imm) noexcept
: u{ imm }
: _value{ static_cast<std::int64_t>(imm) }
{
}

constexpr bool operator==(const Imm& other) const noexcept
{
return u == other.u;
return _value == other._value;
}

constexpr bool operator!=(const Imm& other) const noexcept
{
return u != other.u;
return _value != other._value;
}

template<typename T> constexpr T value() const noexcept
{
return static_cast<T>(s);
return static_cast<T>(_value);
}

template<typename T> Imm& setValue(const T val)
{
s = static_cast<std::int64_t>(val);
_value = static_cast<std::int64_t>(val);

return *this;
}

constexpr BitSize getBitSize() const noexcept
{
if (math::abs(s) > std::numeric_limits<std::uint32_t>::max())
if (math::abs(_value) > std::numeric_limits<std::uint32_t>::max())
{
return BitSize::_64;
}
if (math::abs(s) > std::numeric_limits<std::uint16_t>::max())
if (math::abs(_value) > std::numeric_limits<std::uint16_t>::max())
{
return BitSize::_32;
}
if (math::abs(s) > std::numeric_limits<std::uint8_t>::max())
if (math::abs(_value) > std::numeric_limits<std::uint8_t>::max())
{
return BitSize::_16;
}
Expand All @@ -85,7 +81,7 @@ namespace zasm

namespace detail
{
template<typename T> class ImmT final : public Imm
template<typename T> class ImmT final : public Imm
{
public:
template<typename T2>
Expand Down

0 comments on commit 9cf110f

Please sign in to comment.