Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast all values to int64_t for Imm, simplifies passing some values #139

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading