Skip to content

Commit

Permalink
Merge pull request #152 from zyantific/encoder-overhaul
Browse files Browse the repository at this point in the history
Encoder overhaul
  • Loading branch information
ZehMatt authored Jan 21, 2025
2 parents b14c1b3 + 090abd8 commit 916f28f
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 219 deletions.
14 changes: 7 additions & 7 deletions tests/src/tests/tests.serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,21 @@ namespace zasm::tests

ASSERT_EQ(assembler.bind(label), ErrorCode::None);
ASSERT_EQ(assembler.int3(), ErrorCode::None);
for (int i = 0; i < 125; i++)
for (int i = 0; i < 126; i++)
ASSERT_EQ(assembler.nop(), ErrorCode::None);
ASSERT_EQ(assembler.jmp(label), ErrorCode::None);

Serializer serializer;
ASSERT_EQ(serializer.serialize(program, 0x0000000000401000), ErrorCode::None);

const std::array<std::uint8_t, 131> expected = {
const std::array<uint8_t, 132> expected = {
0xCC, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xE9, 0x7D, 0xFF, 0xFF, 0xFF,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xE9, 0x7C, 0xFF, 0xFF, 0xFF,
};
ASSERT_EQ(serializer.getCodeSize(), expected.size());

Expand All @@ -342,21 +342,21 @@ namespace zasm::tests

ASSERT_EQ(assembler.bind(label), ErrorCode::None);
ASSERT_EQ(assembler.int3(), ErrorCode::None);
for (int i = 0; i < 125; i++)
for (int i = 0; i < 126; i++)
ASSERT_EQ(assembler.nop(), ErrorCode::None);
ASSERT_EQ(assembler.jmp(label), ErrorCode::None);

Serializer serializer;
ASSERT_EQ(serializer.serialize(program, 0x0000000000401000), ErrorCode::None);

const std::array<std::uint8_t, 131> expected = {
const std::array<uint8_t, 132> expected = {
0xCC, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xE9, 0x7D, 0xFF, 0xFF, 0xFF,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xE9, 0x7C, 0xFF, 0xFF, 0xFF,
};
ASSERT_EQ(serializer.getCodeSize(), expected.size());

Expand Down Expand Up @@ -1245,7 +1245,7 @@ namespace zasm::tests
ASSERT_EQ(res, ErrorCode::AddressOutOfRange);

const auto errMsg = std::string("Error at node \"") + mnemonicName
+ " L0\" with id 0: Label out of range for operand 0";
+ " L0\" with id 0: Address out of range for operand 0";
ASSERT_EQ(res.getErrorMessage(), errMsg);
}

Expand Down
25 changes: 24 additions & 1 deletion zasm/src/zasm/src/encoder/encoder.context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ namespace zasm
Section::Attribs attribs{};
};

enum class EncoderFlags : std::uint32_t
{
none = 0,
temporary = 1U << 0,
};
ZASM_ENABLE_ENUM_OPERATORS(EncoderFlags);

struct EncoderContext
{
public:
EncoderFlags flags{};
detail::ProgramState* program{};
bool needsExtraPass{};
std::size_t nodeIndex{};
Expand All @@ -45,7 +53,6 @@ namespace zasm
std::int64_t va{};
std::int32_t offset{};
std::int32_t instrSize{};


struct LabelLink
{
Expand Down Expand Up @@ -105,6 +112,11 @@ namespace zasm
{
assert(id != Label::Id::Invalid);

if ((flags & EncoderFlags::temporary) != EncoderFlags::none)
{
return std::nullopt;
}

const auto& entry = getOrCreateLabelLink(id);
if (entry.boundVA == -1)
{
Expand All @@ -113,5 +125,16 @@ namespace zasm

return entry.boundVA;
}

std::uint32_t getNodeSize(std::size_t nodeIndex) const
{
if ((flags & EncoderFlags::temporary) != EncoderFlags::none)
{
return 0;
}

assert(nodeIndex < nodes.size());
return nodes[nodeIndex].length;
}
};
} // namespace zasm
Loading

0 comments on commit 916f28f

Please sign in to comment.