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

Encoder overhaul #152

Merged
merged 2 commits into from
Jan 21, 2025
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
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