From afd7af28fd01d2e6eb8b6086950a365c899c471c Mon Sep 17 00:00:00 2001 From: Yi Jiang Date: Thu, 11 Jun 2020 21:41:33 -0700 Subject: [PATCH 1/2] Add typeAttrs to MIRPtrType just in case checking fieldID or symbol is not avaliable --- src/maple_ir/include/mir_type.h | 20 ++++++++++++++++---- src/maple_ir/src/bin_mpl_export.cpp | 1 + src/maple_ir/src/bin_mpl_import.cpp | 1 + src/maple_ir/src/global_tables.cpp | 27 +++++++++++++++------------ src/maple_ir/src/mir_type.cpp | 6 +++++- src/maple_ir/src/parser.cpp | 5 +++++ 6 files changed, 43 insertions(+), 17 deletions(-) mode change 100644 => 100755 src/maple_ir/include/mir_type.h mode change 100644 => 100755 src/maple_ir/src/bin_mpl_export.cpp mode change 100644 => 100755 src/maple_ir/src/bin_mpl_import.cpp mode change 100644 => 100755 src/maple_ir/src/global_tables.cpp mode change 100644 => 100755 src/maple_ir/src/mir_type.cpp mode change 100644 => 100755 src/maple_ir/src/parser.cpp diff --git a/src/maple_ir/include/mir_type.h b/src/maple_ir/include/mir_type.h old mode 100644 new mode 100755 index 2870fb36a9..dc0bf699e7 --- a/src/maple_ir/include/mir_type.h +++ b/src/maple_ir/include/mir_type.h @@ -149,12 +149,12 @@ class TypeAttrs { TypeAttrs &operator=(const TypeAttrs &t) = default; ~TypeAttrs() = default; - void SetAlignValue(uint8 flag) { - attrFlag = flag; + void SetAlignValue(uint8 align) { + attrAlign = align; } uint8 GetAlignValue() const { - return attrFlag; + return attrAlign; } void SetAttrFlag(uint64 flag) { @@ -537,6 +537,15 @@ class MIRPtrType : public MIRType { void SetPointedTyIdx(TyIdx idx) { pointedTyIdx = idx; } + TypeAttrs &GetTypeAttrs() { + return typeAttrs; + } + const TypeAttrs &GetTypeAttrs() const { + return typeAttrs; + } + void SetTypeAttrs(TypeAttrs attrs) { + typeAttrs = attrs; + } bool EqualTo(const MIRType &type) const override; @@ -548,7 +557,9 @@ class MIRPtrType : public MIRType { TyIdx GetPointedTyIdxWithFieldID(FieldID fieldID) const; size_t GetHashIndex() const override { constexpr uint8 idxShift = 4; - return ((static_cast(pointedTyIdx) << idxShift) + (typeKind << kShiftNumOfTypeKind)) % kTypeHashLength; + size_t hIdx = (static_cast(pointedTyIdx) << idxShift) + (typeKind << kShiftNumOfTypeKind); + hIdx += (typeAttrs.GetAttrFlag() << 3) + typeAttrs.GetAlignValue(); + return hIdx % kTypeHashLength; } bool PointsToConstString() const override; @@ -558,6 +569,7 @@ class MIRPtrType : public MIRType { std::string GetCompactMplTypeName() const override; private: TyIdx pointedTyIdx; + TypeAttrs typeAttrs; }; class MIRArrayType : public MIRType { diff --git a/src/maple_ir/src/bin_mpl_export.cpp b/src/maple_ir/src/bin_mpl_export.cpp old mode 100644 new mode 100755 index 081d011d38..2545cbd2b8 --- a/src/maple_ir/src/bin_mpl_export.cpp +++ b/src/maple_ir/src/bin_mpl_export.cpp @@ -135,6 +135,7 @@ void OutputTypePointer(const MIRType &ty, BinaryMplExport &mplExport) { const auto &type = static_cast(ty); mplExport.WriteNum(kBinKindTypePointer); mplExport.OutputTypeBase(type); + mplExport.OutputTypeAttrs(type.GetTypeAttrs()); mplExport.OutputType(type.GetPointedTyIdx()); } diff --git a/src/maple_ir/src/bin_mpl_import.cpp b/src/maple_ir/src/bin_mpl_import.cpp old mode 100644 new mode 100755 index 059acecdee..9146db1a86 --- a/src/maple_ir/src/bin_mpl_import.cpp +++ b/src/maple_ir/src/bin_mpl_import.cpp @@ -524,6 +524,7 @@ TyIdx BinaryMplImport::ImportType(bool forPointedType) { size_t idx = typTab.size(); typTab.push_back(nullptr); ++ptrLev; + type.SetTypeAttrs(ImportTypeAttrs()); type.SetPointedTyIdx(ImportType(true)); --ptrLev; MIRType *origType = &InsertInTypeTables(type); diff --git a/src/maple_ir/src/global_tables.cpp b/src/maple_ir/src/global_tables.cpp old mode 100644 new mode 100755 index e51ecd2a67..0afc789f7f --- a/src/maple_ir/src/global_tables.cpp +++ b/src/maple_ir/src/global_tables.cpp @@ -68,7 +68,7 @@ MIRType *TypeTable::CreateAndUpdateMirTypeNode(MIRType &pType) { nType->SetTypeIndex(TyIdx(typeTable.size())); typeTable.push_back(nType); - if (pType.IsMIRPtrType()) { + if (pType.IsMIRPtrType() && static_cast(pType).GetTypeAttrs() == TypeAttrs()) { MIRPtrType &pty = static_cast(pType); if (pty.GetPrimType() == PTY_ptr) { ptrTypeMap[pty.GetPointedTyIdx()] = nType->GetTypeIndex(); @@ -78,25 +78,28 @@ MIRType *TypeTable::CreateAndUpdateMirTypeNode(MIRType &pType) { } else { typeHashTable.insert(nType); } + return nType; } MIRType* TypeTable::GetOrCreateMIRTypeNode(MIRType &pType) { if (pType.IsMIRPtrType()) { auto &type = static_cast(pType); - auto *pMap = (type.GetPrimType() == PTY_ptr ? &ptrTypeMap : &refTypeMap); - auto *otherPMap = (type.GetPrimType() == PTY_ref ? &ptrTypeMap : &refTypeMap); - { - const auto it = pMap->find(type.GetPointedTyIdx()); - if (it != pMap->end()) { - return GetTypeFromTyIdx(it->second); + if (type.GetTypeAttrs() == TypeAttrs()) { + auto *pMap = (type.GetPrimType() == PTY_ptr ? &ptrTypeMap : &refTypeMap); + auto *otherPMap = (type.GetPrimType() == PTY_ref ? &ptrTypeMap : &refTypeMap); + { + const auto it = pMap->find(type.GetPointedTyIdx()); + if (it != pMap->end()) { + return GetTypeFromTyIdx(it->second); + } } + CHECK_FATAL(!(type.GetPointedTyIdx().GetIdx() >= kPtyDerived && type.GetPrimType() == PTY_ref && + otherPMap->find(type.GetPointedTyIdx()) != otherPMap->end()), + "GetOrCreateMIRType: ref pointed-to type %d has previous ptr occurrence", + type.GetPointedTyIdx().GetIdx()); + return CreateAndUpdateMirTypeNode(pType); } - CHECK_FATAL(!(type.GetPointedTyIdx().GetIdx() >= kPtyDerived && type.GetPrimType() == PTY_ref && - otherPMap->find(type.GetPointedTyIdx()) != otherPMap->end()), - "GetOrCreateMIRType: ref pointed-to type %d has previous ptr occurrence", - type.GetPointedTyIdx().GetIdx()); - return CreateAndUpdateMirTypeNode(pType); } { const auto it = typeHashTable.find(&pType); diff --git a/src/maple_ir/src/mir_type.cpp b/src/maple_ir/src/mir_type.cpp old mode 100644 new mode 100755 index 6f1b0944ae..bc74f36e5f --- a/src/maple_ir/src/mir_type.cpp +++ b/src/maple_ir/src/mir_type.cpp @@ -596,6 +596,7 @@ void MIRPtrType::Dump(int indent, bool dontUseName) const { } else { LogInfo::MapleLogger() << "<* "; pointedType->Dump(indent + 1); + typeAttrs.DumpAttributes(); LogInfo::MapleLogger() << ">"; } } @@ -1096,7 +1097,7 @@ bool MIRPtrType::EqualTo(const MIRType &type) const { return false; } const auto &pType = static_cast(type); - return pointedTyIdx == pType.GetPointedTyIdx(); + return pointedTyIdx == pType.GetPointedTyIdx() && typeAttrs == pType.GetTypeAttrs(); } bool MIRArrayType::EqualTo(const MIRType &type) const { @@ -1394,6 +1395,9 @@ FieldPair MIRInterfaceType::TraverseToFieldRef(FieldID&) const { } bool MIRPtrType::IsPointedTypeVolatile(int fieldID) const { + if (typeAttrs.GetAttr(ATTR_volatile)) + return true; + MIRType *pointedTy = GlobalTables::GetTypeTable().GetTypeFromTyIdx(GetPointedTyIdx()); return pointedTy->IsVolatile(fieldID); } diff --git a/src/maple_ir/src/parser.cpp b/src/maple_ir/src/parser.cpp old mode 100644 new mode 100755 index 6388a2bb52..8429081935 --- a/src/maple_ir/src/parser.cpp +++ b/src/maple_ir/src/parser.cpp @@ -1217,6 +1217,11 @@ bool MIRParser::ParsePointType(TyIdx &tyIdx) { pty = PTY_ptr; } MIRPtrType pointType(pointTypeIdx, pty); // use reference type here + if (!ParseTypeAttrs(pointType.GetTypeAttrs())) { + Error("bad type attribute in pointer type specification"); + return false; + } + tyIdx = GlobalTables::GetTypeTable().GetOrCreateMIRType(&pointType); return true; } -- Gitee From ea0a43e7ea5f43444da4e12f78b1a85927611b64 Mon Sep 17 00:00:00 2001 From: Yi Jiang Date: Mon, 15 Jun 2020 16:26:32 -0700 Subject: [PATCH 2/2] Coding style change according to review comments --- src/maple_ir/include/mir_type.h | 3 ++- src/maple_ir/src/mir_type.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/maple_ir/include/mir_type.h b/src/maple_ir/include/mir_type.h index dc0bf699e7..b81301a4d8 100755 --- a/src/maple_ir/include/mir_type.h +++ b/src/maple_ir/include/mir_type.h @@ -557,8 +557,9 @@ class MIRPtrType : public MIRType { TyIdx GetPointedTyIdxWithFieldID(FieldID fieldID) const; size_t GetHashIndex() const override { constexpr uint8 idxShift = 4; + constexpr uint8 flagShift = 3; size_t hIdx = (static_cast(pointedTyIdx) << idxShift) + (typeKind << kShiftNumOfTypeKind); - hIdx += (typeAttrs.GetAttrFlag() << 3) + typeAttrs.GetAlignValue(); + hIdx += (typeAttrs.GetAttrFlag() << flagShift) + typeAttrs.GetAlignValue(); return hIdx % kTypeHashLength; } diff --git a/src/maple_ir/src/mir_type.cpp b/src/maple_ir/src/mir_type.cpp index bc74f36e5f..0dece606c1 100755 --- a/src/maple_ir/src/mir_type.cpp +++ b/src/maple_ir/src/mir_type.cpp @@ -1395,8 +1395,9 @@ FieldPair MIRInterfaceType::TraverseToFieldRef(FieldID&) const { } bool MIRPtrType::IsPointedTypeVolatile(int fieldID) const { - if (typeAttrs.GetAttr(ATTR_volatile)) + if (typeAttrs.GetAttr(ATTR_volatile)) { return true; + } MIRType *pointedTy = GlobalTables::GetTypeTable().GetTypeFromTyIdx(GetPointedTyIdx()); return pointedTy->IsVolatile(fieldID); -- Gitee