mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-11-08 17:51:58 +00:00
Calculate size and alignment in post processor
This commit is contained in:
1 parent
1264be4274
commit
7c51c26255
48 files changed
+966
-246
No files matched your search
@@ -36,27 +36,21 @@ DefinitionWithMembers::DefinitionWithMembers(std::string _namespace, std::string
|
||||
|
||||
unsigned DefinitionWithMembers::GetAlignment() const
|
||||
{
|
||||
assert(m_flags & FLAG_ALIGNMENT_CALCULATED);
|
||||
/*if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();*/
|
||||
assert(m_flags & FLAG_FIELDS_CALCULATED);
|
||||
|
||||
return m_alignment;
|
||||
}
|
||||
|
||||
bool DefinitionWithMembers::GetForceAlignment() const
|
||||
{
|
||||
assert(m_flags & FLAG_ALIGNMENT_CALCULATED);
|
||||
/*if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();*/
|
||||
assert(m_flags & FLAG_FIELDS_CALCULATED);
|
||||
|
||||
return m_flags & FLAG_ALIGNMENT_FORCED;
|
||||
}
|
||||
|
||||
unsigned DefinitionWithMembers::GetSize() const
|
||||
{
|
||||
assert(m_flags & FLAG_SIZE_CALCULATED);
|
||||
/*if ((m_flags & FLAG_SIZE_CALCULATED) == 0)
|
||||
CalculateSize();*/
|
||||
assert(m_flags & FLAG_FIELDS_CALCULATED);
|
||||
|
||||
return m_size;
|
||||
}
|
||||
@@ -9,17 +9,14 @@
|
||||
class DefinitionWithMembers : public DataDefinition
|
||||
{
|
||||
public:
|
||||
static constexpr int FLAG_SIZE_CALCULATED = 1 << 0;
|
||||
static constexpr int FLAG_ALIGNMENT_CALCULATED = 1 << 1;
|
||||
static constexpr int FLAG_FIELDS_CALCULATED = 1 << 0;
|
||||
static constexpr int FLAG_FIELDS_CALCULATING = 1 << 1;
|
||||
static constexpr int FLAG_ALIGNMENT_FORCED = 1 << 2;
|
||||
|
||||
unsigned m_flags;
|
||||
unsigned m_size;
|
||||
unsigned m_alignment;
|
||||
|
||||
/*virtual void CalculateSize() = 0;
|
||||
void CalculateAlignment();*/
|
||||
|
||||
bool m_has_alignment_override;
|
||||
bool m_anonymous;
|
||||
|
||||
|
||||
@@ -1,43 +1,5 @@
|
||||
#include "StructDefinition.h"
|
||||
|
||||
#include "Utils/AlignmentUtils.h"
|
||||
|
||||
//void StructDefinition::CalculateSize()
|
||||
//{
|
||||
// m_size = 0;
|
||||
// auto currentBitOffset = 0u;
|
||||
//
|
||||
// for(const auto& member : m_members)
|
||||
// {
|
||||
// if(member->m_type_declaration->m_has_custom_bit_size)
|
||||
// {
|
||||
// currentBitOffset += member->m_type_declaration->m_custom_bit_size;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (currentBitOffset > 0)
|
||||
// {
|
||||
// currentBitOffset = AlignmentUtils::Align(currentBitOffset, 8u);
|
||||
// m_size += currentBitOffset / 8;
|
||||
// currentBitOffset = 0;
|
||||
// }
|
||||
//
|
||||
// m_size = AlignmentUtils::Align(m_size, member->GetForceAlignment() ? member->GetAlignment() : std::min(member->GetAlignment(), m_pack));
|
||||
// m_size += member->m_type_declaration->GetSize();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (currentBitOffset > 0)
|
||||
// {
|
||||
// currentBitOffset = AlignmentUtils::Align(currentBitOffset, 8u);
|
||||
// m_size += currentBitOffset / 8;
|
||||
// }
|
||||
//
|
||||
// m_size = AlignmentUtils::Align(m_size, GetAlignment());
|
||||
//
|
||||
// m_flags |= FLAG_SIZE_CALCULATED;
|
||||
//}
|
||||
|
||||
StructDefinition::StructDefinition(std::string _namespace, std::string name, const int pack)
|
||||
: DefinitionWithMembers(std::move(_namespace), std::move(name), pack)
|
||||
{
|
||||
|
||||
@@ -4,9 +4,6 @@
|
||||
|
||||
class StructDefinition final : public DefinitionWithMembers
|
||||
{
|
||||
//protected:
|
||||
// void CalculateSize() override;
|
||||
|
||||
public:
|
||||
StructDefinition(std::string _namespace, std::string name, int pack);
|
||||
|
||||
|
||||
@@ -16,75 +16,20 @@ TypeDeclaration::TypeDeclaration(const DataDefinition* type)
|
||||
assert(m_type != nullptr);
|
||||
}
|
||||
|
||||
void TypeDeclaration::CalculateSize()
|
||||
unsigned TypeDeclaration::GetSize() const
|
||||
{
|
||||
auto currentSize = m_type->GetSize();
|
||||
|
||||
for (auto i = m_declaration_modifiers.size(); i > 0; i--)
|
||||
{
|
||||
const auto& declarationModifier = m_declaration_modifiers[i - 1];
|
||||
|
||||
switch (declarationModifier->GetType())
|
||||
{
|
||||
case DeclarationModifierType::POINTER:
|
||||
currentSize = POINTER_SIZE;
|
||||
break;
|
||||
|
||||
case DeclarationModifierType::ARRAY:
|
||||
currentSize *= dynamic_cast<ArrayDeclarationModifier*>(declarationModifier.get())->m_size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_flags |= FLAG_SIZE_CALCULATED;
|
||||
}
|
||||
|
||||
void TypeDeclaration::CalculateAlignment()
|
||||
{
|
||||
auto hasPointerModifier = false;
|
||||
for (const auto& declarationModifier : m_declaration_modifiers)
|
||||
{
|
||||
if (declarationModifier->GetType() == DeclarationModifierType::POINTER)
|
||||
{
|
||||
hasPointerModifier = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasPointerModifier)
|
||||
{
|
||||
m_alignment = POINTER_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_alignment = m_type->GetAlignment();
|
||||
if (m_type->GetForceAlignment())
|
||||
m_flags |= FLAG_ALIGNMENT_FORCED;
|
||||
}
|
||||
|
||||
m_flags |= FLAG_ALIGNMENT_CALCULATED;
|
||||
}
|
||||
|
||||
unsigned TypeDeclaration::GetSize()
|
||||
{
|
||||
if ((m_flags & FLAG_SIZE_CALCULATED) == 0)
|
||||
CalculateSize();
|
||||
|
||||
assert(m_flags & FLAG_FIELDS_CALCULATED);
|
||||
return m_size;
|
||||
}
|
||||
|
||||
unsigned TypeDeclaration::GetAlignment()
|
||||
unsigned TypeDeclaration::GetAlignment() const
|
||||
{
|
||||
if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();
|
||||
|
||||
assert(m_flags & FLAG_FIELDS_CALCULATED);
|
||||
return m_alignment;
|
||||
}
|
||||
|
||||
bool TypeDeclaration::GetForceAlignment()
|
||||
bool TypeDeclaration::GetForceAlignment() const
|
||||
{
|
||||
if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();
|
||||
|
||||
assert(m_flags & FLAG_FIELDS_CALCULATED);
|
||||
return m_flags & FLAG_ALIGNMENT_FORCED;
|
||||
}
|
||||
@@ -1,26 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "DataDefinition.h"
|
||||
#include "DeclarationModifier.h"
|
||||
|
||||
class TypeDeclaration
|
||||
{
|
||||
static constexpr unsigned POINTER_SIZE = sizeof(uint32_t); // TODO: Change this to support 64bit
|
||||
static constexpr int FLAG_SIZE_CALCULATED = 1 << 0;
|
||||
static constexpr int FLAG_ALIGNMENT_CALCULATED = 1 << 1;
|
||||
static constexpr int FLAG_ALIGNMENT_FORCED = 1 << 2;
|
||||
public:
|
||||
static constexpr int FLAG_FIELDS_CALCULATED = 1 << 0;
|
||||
static constexpr int FLAG_ALIGNMENT_FORCED = 1 << 1;
|
||||
|
||||
unsigned m_flags;
|
||||
unsigned m_size;
|
||||
unsigned m_alignment;
|
||||
|
||||
void CalculateSize();
|
||||
void CalculateAlignment();
|
||||
|
||||
public:
|
||||
explicit TypeDeclaration(const DataDefinition* type);
|
||||
|
||||
bool m_is_const;
|
||||
@@ -31,7 +27,7 @@ public:
|
||||
|
||||
std::vector<std::unique_ptr<DeclarationModifier>> m_declaration_modifiers;
|
||||
|
||||
unsigned GetSize();
|
||||
unsigned GetAlignment();
|
||||
bool GetForceAlignment();
|
||||
_NODISCARD unsigned GetSize() const;
|
||||
_NODISCARD unsigned GetAlignment() const;
|
||||
_NODISCARD bool GetForceAlignment() const;
|
||||
};
|
||||
@@ -1,23 +1,5 @@
|
||||
#include "UnionDefinition.h"
|
||||
|
||||
#include "Utils/AlignmentUtils.h"
|
||||
|
||||
//void UnionDefinition::CalculateSize()
|
||||
//{
|
||||
// m_size = 0;
|
||||
//
|
||||
// for(const auto& member : m_members)
|
||||
// {
|
||||
// const auto memberSize = member->m_type_declaration->GetSize();
|
||||
// if (memberSize > m_size)
|
||||
// m_size = memberSize;
|
||||
// }
|
||||
//
|
||||
// m_size = AlignmentUtils::Align(m_size, GetAlignment());
|
||||
//
|
||||
// m_flags |= FLAG_SIZE_CALCULATED;
|
||||
//}
|
||||
|
||||
UnionDefinition::UnionDefinition(std::string _namespace, std::string name, const int pack)
|
||||
: DefinitionWithMembers(std::move(_namespace), std::move(name), pack)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ Variable::Variable(std::string name, std::unique_ptr<TypeDeclaration> typeDeclar
|
||||
{
|
||||
}
|
||||
|
||||
unsigned Variable::GetAlignment()
|
||||
unsigned Variable::GetAlignment() const
|
||||
{
|
||||
if (m_has_alignment_override)
|
||||
return m_alignment_override;
|
||||
@@ -16,7 +16,7 @@ unsigned Variable::GetAlignment()
|
||||
return m_type_declaration->GetAlignment();
|
||||
}
|
||||
|
||||
bool Variable::GetForceAlignment()
|
||||
bool Variable::GetForceAlignment() const
|
||||
{
|
||||
return m_has_alignment_override || m_type_declaration->GetForceAlignment();
|
||||
}
|
||||
@@ -15,6 +15,6 @@ public:
|
||||
|
||||
Variable(std::string name, std::unique_ptr<TypeDeclaration> typeDeclaration);
|
||||
|
||||
unsigned GetAlignment();
|
||||
bool GetForceAlignment();
|
||||
unsigned GetAlignment() const;
|
||||
bool GetForceAlignment() const;
|
||||
};
|
||||
Reference in new issue
Block a user