/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_BASE_ALIGNED_STRUCT_H #define ECMASCRIPT_BASE_ALIGNED_STRUCT_H #include "ecmascript/base/bit_helper.h" namespace panda::ecmascript::base { template struct TypeList {}; template struct TypeList { using Head = T; using Next = TypeList<>; static constexpr size_t Size = 1; }; template struct TypeList { using Head = T; using Next = TypeList; static constexpr size_t Size = 1 + sizeof...(Res); }; template struct AlignedStruct { static constexpr size_t EAS = ElementAlign; using ElemTypeList = TypeList; static constexpr size_t NumOfTypes = ElemTypeList::Size; template struct OffsetAt; template<> struct OffsetAt<0, TypeList<>> { static constexpr size_t OFFSET64 = 0; static constexpr size_t OFFSET32 = 0; }; template struct OffsetAt<0, TypeList> { static constexpr size_t OFFSET64 = 0; static constexpr size_t OFFSET32 = 0; }; template struct OffsetAt> { static_assert(std::is_class::value); static constexpr size_t OFFSET64 = RoundUp(Head::SizeArch64, EAS) + OffsetAt>::OFFSET64; static constexpr size_t OFFSET32 = RoundUp(Head::SizeArch32, EAS) + OffsetAt>::OFFSET32; }; template static size_t GetOffset(bool isArch32) { return isArch32 ? OffsetAt::OFFSET32 : OffsetAt::OFFSET64; }; static constexpr size_t SizeArch32 = OffsetAt::OFFSET32; static constexpr size_t SizeArch64 = OffsetAt::OFFSET64; }; struct AlignedPointer { static constexpr size_t SizeArch32 = sizeof(uint32_t); static constexpr size_t SizeArch64 = sizeof(uint64_t); static constexpr size_t Size() { return sizeof(uintptr_t); } }; struct AlignedSize { static constexpr size_t SizeArch32 = sizeof(uint32_t); static constexpr size_t SizeArch64 = sizeof(uint64_t); static constexpr size_t Size() { return sizeof(uintptr_t); } }; struct AlignedUint64 { static constexpr size_t SizeArch32 = sizeof(uint64_t); static constexpr size_t SizeArch64 = sizeof(uint64_t); static constexpr size_t Size() { return sizeof(uint64_t); } }; struct AlignedUint32 { static constexpr size_t SizeArch32 = sizeof(uint32_t); static constexpr size_t SizeArch64 = sizeof(uint32_t); static constexpr size_t Size() { return sizeof(uint32_t); } }; struct AlignedUint8 { static constexpr size_t SizeArch32 = sizeof(uint8_t); static constexpr size_t SizeArch64 = sizeof(uint8_t); static constexpr size_t Size() { return sizeof(uint8_t); } }; } #endif // ECMASCRIPT_BASE_ALIGNED_STRUCT_H