Skip to content

Commit 56ceab4

Browse files
authored
Merge pull request #139 from zyantific/issue-138
Cast all values to int64_t for Imm, simplifies passing some values
2 parents c9364fc + 9cf110f commit 56ceab4

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

tests/src/tests/tests.serialization.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -1365,4 +1365,27 @@ namespace zasm::tests
13651365
}
13661366
}
13671367

1368+
TEST(SerializationTests, Issue_138)
1369+
{
1370+
Program program(zasm::MachineMode::AMD64);
1371+
x86::Assembler assembler(program);
1372+
1373+
assembler.mov(x86::ecx, Imm(0xFFFFFFFF));
1374+
1375+
zasm::Serializer serializer{};
1376+
ASSERT_EQ(serializer.serialize(program, 0x140015000), ErrorCode::None);
1377+
1378+
const std::array<uint8_t, 5> expected = {
1379+
0xB9, 0xFF, 0xFF, 0xFF, 0xFF,
1380+
};
1381+
ASSERT_EQ(serializer.getCodeSize(), expected.size());
1382+
1383+
const auto* data = serializer.getCode();
1384+
ASSERT_NE(data, nullptr);
1385+
for (std::size_t i = 0; i < expected.size(); i++)
1386+
{
1387+
ASSERT_EQ(data[i], expected[i]);
1388+
}
1389+
}
1390+
13681391
} // namespace zasm::tests

zasm/include/zasm/base/immediate.hpp

+15-19
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,65 @@
88

99
namespace zasm
1010
{
11-
class Imm
11+
class Imm
1212
{
13-
union
14-
{
15-
std::int64_t s;
16-
std::uint64_t u;
17-
};
13+
std::int64_t _value;
1814

1915
public:
2016
constexpr Imm() noexcept
21-
: s{}
17+
: _value{}
2218
{
2319
}
2420
constexpr Imm(std::uint32_t imm) noexcept
25-
: u{ imm }
21+
: _value{ static_cast<std::int32_t>(imm) }
2622
{
2723
}
2824
constexpr Imm(std::int32_t imm) noexcept
29-
: s{ imm }
25+
: _value{ imm }
3026
{
3127
}
3228
constexpr Imm(std::int64_t imm) noexcept
33-
: s{ imm }
29+
: _value{ imm }
3430
{
3531
}
3632
constexpr Imm(std::uint64_t imm) noexcept
37-
: u{ imm }
33+
: _value{ static_cast<std::int64_t>(imm) }
3834
{
3935
}
4036

4137
constexpr bool operator==(const Imm& other) const noexcept
4238
{
43-
return u == other.u;
39+
return _value == other._value;
4440
}
4541

4642
constexpr bool operator!=(const Imm& other) const noexcept
4743
{
48-
return u != other.u;
44+
return _value != other._value;
4945
}
5046

5147
template<typename T> constexpr T value() const noexcept
5248
{
53-
return static_cast<T>(s);
49+
return static_cast<T>(_value);
5450
}
5551

5652
template<typename T> Imm& setValue(const T val)
5753
{
58-
s = static_cast<std::int64_t>(val);
54+
_value = static_cast<std::int64_t>(val);
5955

6056
return *this;
6157
}
6258

6359
constexpr BitSize getBitSize() const noexcept
6460
{
65-
if (math::abs(s) > std::numeric_limits<std::uint32_t>::max())
61+
if (math::abs(_value) > std::numeric_limits<std::uint32_t>::max())
6662
{
6763
return BitSize::_64;
6864
}
69-
if (math::abs(s) > std::numeric_limits<std::uint16_t>::max())
65+
if (math::abs(_value) > std::numeric_limits<std::uint16_t>::max())
7066
{
7167
return BitSize::_32;
7268
}
73-
if (math::abs(s) > std::numeric_limits<std::uint8_t>::max())
69+
if (math::abs(_value) > std::numeric_limits<std::uint8_t>::max())
7470
{
7571
return BitSize::_16;
7672
}
@@ -85,7 +81,7 @@ namespace zasm
8581

8682
namespace detail
8783
{
88-
template<typename T> class ImmT final : public Imm
84+
template<typename T> class ImmT final : public Imm
8985
{
9086
public:
9187
template<typename T2>

0 commit comments

Comments
 (0)