mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
Calculate size and alignment in post processor
This commit is contained in:
@ -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;
|
||||
};
|
||||
|
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
enum class Architecture
|
||||
{
|
||||
UNKNOWN,
|
||||
X86,
|
||||
X64
|
||||
};
|
@ -7,6 +7,13 @@ OperandDynamic::OperandDynamic(StructureInformation* structure)
|
||||
{
|
||||
}
|
||||
|
||||
OperandDynamic::OperandDynamic(StructureInformation* structure, std::vector<MemberInformation*> referencedMemberChain, std::vector<std::unique_ptr<IEvaluation>> arrayIndices)
|
||||
: m_structure(structure),
|
||||
m_referenced_member_chain(std::move(referencedMemberChain)),
|
||||
m_array_indices(std::move(arrayIndices))
|
||||
{
|
||||
}
|
||||
|
||||
EvaluationType OperandDynamic::GetType() const
|
||||
{
|
||||
return EvaluationType::OPERAND_DYNAMIC;
|
||||
|
@ -14,6 +14,7 @@ public:
|
||||
std::vector<std::unique_ptr<IEvaluation>> m_array_indices;
|
||||
|
||||
explicit OperandDynamic(StructureInformation* structure);
|
||||
OperandDynamic(StructureInformation* structure, std::vector<MemberInformation*> referencedMemberChain, std::vector<std::unique_ptr<IEvaluation>> arrayIndices);
|
||||
|
||||
_NODISCARD EvaluationType GetType() const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
|
@ -6,8 +6,8 @@ OperandStatic::OperandStatic(const int value)
|
||||
{
|
||||
}
|
||||
|
||||
OperandStatic::OperandStatic(const int value, EnumMember* enumMember)
|
||||
: m_value(value),
|
||||
OperandStatic::OperandStatic(EnumMember* enumMember)
|
||||
: m_value(enumMember->m_value),
|
||||
m_enum_member(enumMember)
|
||||
{
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public:
|
||||
EnumMember* const m_enum_member;
|
||||
|
||||
explicit OperandStatic(int value);
|
||||
OperandStatic(int value, EnumMember* enumMember);
|
||||
explicit OperandStatic(EnumMember* enumMember);
|
||||
|
||||
_NODISCARD EvaluationType GetType() const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
|
@ -5,6 +5,13 @@ Operation::Operation(const OperationType* type)
|
||||
{
|
||||
}
|
||||
|
||||
Operation::Operation(const OperationType* type, std::unique_ptr<IEvaluation> operand1, std::unique_ptr<IEvaluation> operand2)
|
||||
: m_operation_type(type),
|
||||
m_operand1(std::move(operand1)),
|
||||
m_operand2(std::move(operand2))
|
||||
{
|
||||
}
|
||||
|
||||
EvaluationType Operation::GetType() const
|
||||
{
|
||||
return EvaluationType::OPERATION;
|
||||
|
@ -14,6 +14,7 @@ public:
|
||||
std::unique_ptr<IEvaluation> m_operand2;
|
||||
|
||||
explicit Operation(const OperationType* type);
|
||||
Operation(const OperationType* type, std::unique_ptr<IEvaluation> operand1, std::unique_ptr<IEvaluation> operand2);
|
||||
|
||||
_NODISCARD EvaluationType GetType() const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
|
@ -73,7 +73,7 @@ const OperationType* const OperationType::OPERATION_GREATER_THAN
|
||||
return op1 > op2 ? 1 : 0;
|
||||
});
|
||||
|
||||
const OperationType* const OperationType::OPERATION_GREATER_EQUALS_THAN
|
||||
const OperationType* const OperationType::OPERATION_GREATER_EQUAL_THAN
|
||||
= new OperationType(">=", OperationPrecedence::RELATIONAL_GREATER_LESS_THAN, [](auto op1, auto op2)
|
||||
{
|
||||
return op1 >= op2 ? 1 : 0;
|
||||
@ -85,7 +85,7 @@ const OperationType* const OperationType::OPERATION_LESS_THAN
|
||||
return op1 < op2 ? 1 : 0;
|
||||
});
|
||||
|
||||
const OperationType* const OperationType::OPERATION_LESS_EQUALS_THAN
|
||||
const OperationType* const OperationType::OPERATION_LESS_EQUAL_THAN
|
||||
= new OperationType("<=", OperationPrecedence::RELATIONAL_GREATER_LESS_THAN, [](auto op1, auto op2)
|
||||
{
|
||||
return op1 <= op2 ? 1 : 0;
|
||||
@ -97,7 +97,7 @@ const OperationType* const OperationType::OPERATION_EQUALS
|
||||
return op1 == op2 ? 1 : 0;
|
||||
});
|
||||
|
||||
const OperationType* const OperationType::OPERATION_NOT_EQUALS
|
||||
const OperationType* const OperationType::OPERATION_NOT_EQUAL
|
||||
= new OperationType("!=", OperationPrecedence::RELATIONAL_EQUALS, [](auto op1, auto op2)
|
||||
{
|
||||
return op1 != op2 ? 1 : 0;
|
||||
@ -129,11 +129,11 @@ const OperationType* const OperationType::ALL_OPERATION_TYPES[]
|
||||
OPERATION_SHIFT_LEFT,
|
||||
OPERATION_SHIFT_RIGHT,
|
||||
OPERATION_GREATER_THAN,
|
||||
OPERATION_GREATER_EQUALS_THAN,
|
||||
OPERATION_GREATER_EQUAL_THAN,
|
||||
OPERATION_LESS_THAN,
|
||||
OPERATION_LESS_EQUALS_THAN,
|
||||
OPERATION_LESS_EQUAL_THAN,
|
||||
OPERATION_EQUALS,
|
||||
OPERATION_NOT_EQUALS,
|
||||
OPERATION_NOT_EQUAL,
|
||||
OPERATION_AND,
|
||||
OPERATION_OR
|
||||
};
|
||||
|
@ -40,11 +40,11 @@ public:
|
||||
static const OperationType* const OPERATION_SHIFT_LEFT;
|
||||
static const OperationType* const OPERATION_SHIFT_RIGHT;
|
||||
static const OperationType* const OPERATION_GREATER_THAN;
|
||||
static const OperationType* const OPERATION_GREATER_EQUALS_THAN;
|
||||
static const OperationType* const OPERATION_GREATER_EQUAL_THAN;
|
||||
static const OperationType* const OPERATION_LESS_THAN;
|
||||
static const OperationType* const OPERATION_LESS_EQUALS_THAN;
|
||||
static const OperationType* const OPERATION_LESS_EQUAL_THAN;
|
||||
static const OperationType* const OPERATION_EQUALS;
|
||||
static const OperationType* const OPERATION_NOT_EQUALS;
|
||||
static const OperationType* const OPERATION_NOT_EQUAL;
|
||||
static const OperationType* const OPERATION_AND;
|
||||
static const OperationType* const OPERATION_OR;
|
||||
|
||||
|
Reference in New Issue
Block a user