diff --git a/src/mapleall/maple_ir/include/mir_type.h b/src/mapleall/maple_ir/include/mir_type.h index f9eecc12b589323994dfc04cb4e5966d1c85c1a6..86c5da0407b602fed620226224c60f1f7b000375 100644 --- a/src/mapleall/maple_ir/include/mir_type.h +++ b/src/mapleall/maple_ir/include/mir_type.h @@ -892,6 +892,10 @@ class MIRStructType : public MIRType { return TraverseToField(id).second; } + TyIdxFieldAttrPair GetTyidxFieldAttrPair(uint32 n) const { + return fields.at(n).second; + } + TyIdx GetFieldTyIdx(FieldID id) const { const FieldPair &fieldPair = TraverseToField(id); return fieldPair.second.first; @@ -1130,8 +1134,9 @@ class MIRStructType : public MIRType { std::vector genericDeclare; std::map fieldGenericDeclare; std::vector inheritanceGeneric; - private: + public: FieldPair TraverseToField(FieldID fieldID) const ; + private: FieldPair TraverseToField(GStrIdx fieldStrIdx) const ; bool HasVolatileFieldInFields(const FieldVector &fieldsOfStruct) const; bool HasTypeParamInFields(const FieldVector &fieldsOfStruct) const; @@ -1470,9 +1475,19 @@ class MIRBitFieldType : public MIRType { } size_t GetSize() const override { - return 0; + if (fieldSize == 0) { + return 0; + } else if (fieldSize <= 8) { + return 1; + } else { + return (fieldSize + 7) / 8; + } } // size not be in bytes + uint32 GetAlign() const override { + return 0; + } // align not be in bytes + size_t GetHashIndex() const override { return ((static_cast(primType) << fieldSize) + (typeKind << kShiftNumOfTypeKind)) % kTypeHashLength; } diff --git a/src/mapleall/maple_ir/src/mir_type.cpp b/src/mapleall/maple_ir/src/mir_type.cpp index 96c16531808f4b77b955f3e4cd3b0f398970ef3c..00cb573c316a4765c8d9beec8b6d317a91fe445a 100644 --- a/src/mapleall/maple_ir/src/mir_type.cpp +++ b/src/mapleall/maple_ir/src/mir_type.cpp @@ -967,7 +967,7 @@ size_t MIRStructType::GetSize() const { } size_t maxSize = 0; for (size_t i = 0; i < fields.size(); ++i) { - TyIdxFieldAttrPair tfap = GetFieldTyIdxAttrPair(i); + TyIdxFieldAttrPair tfap = GetTyidxFieldAttrPair(i); MIRType *fieldType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tfap.first); size_t size = RoundUp(fieldType->GetSize(), tfap.second.GetAlign()); if (maxSize < size) { @@ -979,35 +979,34 @@ size_t MIRStructType::GetSize() const { // since there may be bitfields, perform a layout process for the fields size_t byteOfst = 0; size_t bitOfst = 0; - constexpr uint32 bitNumPerByte = 8; - constexpr uint32 shiftNum = 3; for (size_t i = 0; i < fields.size(); ++i) { - TyIdxFieldAttrPair tfap = GetFieldTyIdxAttrPair(static_cast(i)); + TyIdxFieldAttrPair tfap = GetTyidxFieldAttrPair(i); MIRType *fieldType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tfap.first); if (fieldType->GetKind() != kTypeBitField) { - if (byteOfst * bitNumPerByte < bitOfst) { - byteOfst = (bitOfst >> shiftNum) + 1; + if (byteOfst * 8 < bitOfst) { + byteOfst = (bitOfst >> 3) + 1; } byteOfst = RoundUp(byteOfst, std::max(fieldType->GetAlign(), tfap.second.GetAlign())); byteOfst += fieldType->GetSize(); - bitOfst = byteOfst * bitNumPerByte; + bitOfst = byteOfst * 8; } else { - MIRBitFieldType *bitfType = static_cast(fieldType); + MIRBitFieldType *bitfType = static_cast(fieldType); if (bitfType->GetFieldSize() == 0) { // special case, for aligning purpose bitOfst = RoundUp(bitOfst, GetPrimTypeBitSize(bitfType->GetPrimType())); - byteOfst = bitOfst >> shiftNum; + byteOfst = bitOfst >> 3; } else { if (RoundDown(bitOfst + bitfType->GetFieldSize() - 1, GetPrimTypeBitSize(bitfType->GetPrimType())) != RoundDown(bitOfst, GetPrimTypeBitSize(bitfType->GetPrimType()))) { bitOfst = RoundUp(bitOfst, GetPrimTypeBitSize(bitfType->GetPrimType())); + byteOfst = bitOfst >> 3; } bitOfst += bitfType->GetFieldSize(); - byteOfst = bitOfst >> shiftNum; + byteOfst = bitOfst >> 3; } } } - if (byteOfst * bitNumPerByte < bitOfst) { - byteOfst = (bitOfst >> shiftNum) + 1; + if (byteOfst * 8 < bitOfst) { + byteOfst = (bitOfst >> 3) + 1; } byteOfst = RoundUp(byteOfst, GetAlign()); if (byteOfst == 0 && isCPlusPlus) { @@ -1443,38 +1442,29 @@ bool MIRGenericInstantType::EqualTo(const MIRType &type) const { // in the search, curfieldid is being decremented until it reaches 1 FieldPair MIRStructType::TraverseToFieldRef(FieldID &fieldID) const { - if (fields.empty()) { + if (!fields.size()) { return FieldPair(GStrIdx(0), TyIdxFieldAttrPair(TyIdx(0), FieldAttrs())); } - uint32 fieldIdx = 0; - FieldPair curPair = fields[0]; + + uint32 fieldidx = 0; + FieldPair curpair = fields[0]; while (fieldID > 1) { - --fieldID; - MIRType *curFieldType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(curPair.second.first); - MIRStructType *curFieldStructType = nullptr; - switch (curFieldType->GetKind()) { - case kTypeStruct: - case kTypeStructIncomplete: - case kTypeClass: - case kTypeClassIncomplete: - case kTypeInterface: - case kTypeInterfaceIncomplete: - curFieldStructType = static_cast(curFieldType); - curPair = curFieldStructType->TraverseToFieldRef(fieldID); - if (fieldID == 1 && curPair.second.first != 0u) { - return curPair; - } - break; - default: - break; + fieldID--; + MIRType *curfieldtype = GlobalTables::GetTypeTable().GetTypeFromTyIdx(curpair.second.first); + MIRStructType *substructty = curfieldtype->EmbeddedStructType(); + if (substructty != nullptr) { + curpair = substructty->TraverseToFieldRef(fieldID); + if (fieldID == 1 && curpair.second.first != TyIdx(0)) { + return curpair; + } } - ++fieldIdx; - if (fieldIdx == fields.size()) { + fieldidx++; + if (fieldidx == fields.size()) { return FieldPair(GStrIdx(0), TyIdxFieldAttrPair(TyIdx(0), FieldAttrs())); } - curPair = fields[fieldIdx]; + curpair = fields[fieldidx]; } - return curPair; + return curpair; } FieldPair MIRStructType::TraverseToField(FieldID fieldID) const { @@ -1624,15 +1614,15 @@ bool MIRPtrType::IsPointedTypeVolatile(int fieldID) const { return pointedTy->IsVolatile(fieldID); } -TyIdxFieldAttrPair MIRPtrType::GetPointedTyIdxFldAttrPairWithFieldID(FieldID fieldID) const { - if (fieldID == 0) { +TyIdxFieldAttrPair MIRPtrType::GetPointedTyIdxFldAttrPairWithFieldID(FieldID fldid) const { + if (fldid == 0) { return TyIdxFieldAttrPair(pointedTyIdx, FieldAttrs()); } - MIRType *pointedType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(pointedTyIdx); - CHECK_FATAL(pointedType->IsStructType(), - "MIRPtrType::GetPointedTyIdxWithFieldID(): cannot have non-zero fieldID for something other than a struct"); - auto *structType = static_cast(pointedType); - return structType->GetFieldTyIdxAttrPair(fieldID); + MIRType *ty = GlobalTables::GetTypeTable().GetTypeFromTyIdx(pointedTyIdx); + MIRStructType *structty = ty->EmbeddedStructType(); + CHECK_FATAL(structty, + "MIRPtrType::GetPointedTyidxWithFieldId(): cannot have non-zero fieldID for something other than a struct"); + return structty->TraverseToField(fldid).second; } TyIdx MIRPtrType::GetPointedTyIdxWithFieldID(FieldID fieldID) const {