// SPDX-License-Identifier: GPL-3.0-or-later #ifndef RCC_IR_DATALAYOUT_H #define RCC_IR_DATALAYOUT_H #include "rcc/IR/Constants.h" #include "rcc/Support/Error.h" #include #include #include #include #include namespace rcc { class DataLayout final { public: [[nodiscard]] static Expected parse(std::string_view LayoutString); [[nodiscard]] Expected getTypeAllocSize(const GenericConstantType &Type) const; [[nodiscard]] Expected getABITypeAlign(const GenericConstantType &Type) const; [[nodiscard]] Expected getPrefTypeAlign(const GenericConstantType &Type) const; [[nodiscard]] Expected getPointerSizeInBits(std::uint32_t AddressSpace = 0) const; private: struct AlignmentSpec final { std::uint64_t ABI = 1; std::uint64_t Preferred = 1; }; struct PrimitiveSpec final { char Kind = '\0'; std::uint32_t BitWidth = 0; AlignmentSpec Alignment; }; struct PointerSpec final { std::uint32_t AddressSpace = 0; std::uint32_t SizeInBits = 0; AlignmentSpec Alignment; std::string AddressSpaceName; }; struct TypeLayout final { std::uint64_t StoreSize = 0; std::uint64_t AllocationSize = 0; std::uint64_t ABIAlignment = 1; std::uint64_t PreferredAlignment = 1; }; [[nodiscard]] static Expected parseUnsignedField(std::string_view Text, std::string_view Description); [[nodiscard]] static Expected alignmentBytes(std::string_view Text, bool AllowZero, std::string_view Description); [[nodiscard]] static std::vector split(std::string_view Text, char Separator); void setPrimitiveSpec(PrimitiveSpec Entry); void setPointerSpec(PointerSpec Entry); [[nodiscard]] AlignmentSpec naturalAlignment(std::uint64_t StoreSize) const noexcept; [[nodiscard]] AlignmentSpec scalarAlignment(char Kind, std::uint32_t BitWidth, std::uint64_t StoreSize) const noexcept; [[nodiscard]] AlignmentSpec integerAlignment(std::uint32_t BitWidth, std::uint64_t StoreSize) const noexcept; [[nodiscard]] AlignmentSpec floatingAlignment(std::uint32_t BitWidth, std::uint64_t StoreSize) const noexcept; [[nodiscard]] AlignmentSpec vectorAlignment(std::uint32_t BitWidth, std::uint64_t StoreSize) const noexcept; [[nodiscard]] Expected getPointerSpec(std::uint32_t AddressSpace) const; [[nodiscard]] Expected getTypeLayout(const GenericConstantType &Type, std::vector &NamedTypeStack, std::size_t Depth) const; bool Explicit = false; std::vector PrimitiveSpecs; std::vector PointerSpecs; AlignmentSpec AggregateAlignment{1, 8}; }; } // namespace rcc #endif // RCC_IR_DATALAYOUT_H