AddPadding: Pad classes with zero members

This commit is contained in:
Alastair Robertson 2023-07-18 05:38:47 -07:00 committed by Alastair Robertson
parent 3aa52deb71
commit a0acaaea65
3 changed files with 19 additions and 3 deletions

View File

@ -89,6 +89,9 @@ void AddPadding::visit(Class& c) {
if (!c.members.empty()) {
addPadding(c.members.back(), c.size() * 8, paddedMembers);
} else {
// Pad out empty classes
addPadding(0, c.size() * 8, paddedMembers);
}
c.members = std::move(paddedMembers);
@ -109,7 +112,14 @@ void AddPadding::addPadding(const Member& prevMember,
}
uint64_t prevMemberEndBits = prevMember.bitOffset + prevMemberSizeBits;
uint64_t paddingBits = paddingEndBits - prevMemberEndBits;
addPadding(prevMemberEndBits, paddingEndBits, paddedMembers);
}
void AddPadding::addPadding(uint64_t paddingStartBits,
uint64_t paddingEndBits,
std::vector<Member>& paddedMembers) {
uint64_t paddingBits = paddingEndBits - paddingStartBits;
if (paddingBits == 0)
return;
@ -117,11 +127,11 @@ void AddPadding::addPadding(const Member& prevMember,
// Pad with an array of bytes
auto& primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int8);
auto& paddingArray = typeGraph_.makeType<Array>(primitive, paddingBits / 8);
paddedMembers.emplace_back(paddingArray, MemberPrefix, prevMemberEndBits);
paddedMembers.emplace_back(paddingArray, MemberPrefix, paddingStartBits);
} else {
// Pad with a bitfield
auto& primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int64);
paddedMembers.emplace_back(primitive, MemberPrefix, prevMemberEndBits,
paddedMembers.emplace_back(primitive, MemberPrefix, paddingStartBits,
paddingBits);
}
}

View File

@ -58,6 +58,9 @@ class AddPadding final : public RecursiveVisitor {
void addPadding(const Member& prevMember,
uint64_t paddingEndBits,
std::vector<Member>& paddedMembers);
void addPadding(uint64_t paddingStartBits,
uint64_t paddingEndBits,
std::vector<Member>& paddedMembers);
};
} // namespace type_graph

View File

@ -176,6 +176,9 @@ TEST(AddPaddingTest, MemberlessClass) {
)",
R"(
[0] Class: MyClass (size: 12)
Member: __oi_padding (offset: 0)
[1] Array: (length: 12)
Primitive: int8_t
)");
}