mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-09 03:52:19 +02:00
Refactored vdata static init/dtor lists
This commit is contained in:
parent
8b01d806d0
commit
9bce9f2ce7
1 changed files with 93 additions and 60 deletions
|
@ -1156,11 +1156,65 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
if (project->mTargetType == BfTargetType_BeefTest)
|
if (project->mTargetType == BfTargetType_BeefTest)
|
||||||
GetTestMethods(bfModule, testMethods, vdataHashCtx);
|
GetTestMethods(bfModule, testMethods, vdataHashCtx);
|
||||||
|
|
||||||
|
Array<String*> typeNameList;
|
||||||
|
defer(
|
||||||
|
{
|
||||||
|
for (auto str : typeNameList)
|
||||||
|
delete str;
|
||||||
|
});
|
||||||
|
|
||||||
|
struct _SortedTypeEntry
|
||||||
|
{
|
||||||
|
BfTypeInstance* mTypeInstance;
|
||||||
|
String* mTypeName;
|
||||||
|
int mPriority;
|
||||||
|
|
||||||
|
_SortedTypeEntry()
|
||||||
|
{
|
||||||
|
mPriority = 0;
|
||||||
|
mTypeInstance = NULL;
|
||||||
|
mTypeName = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_SortedTypeEntry(BfModule* module, BfTypeInstance* typeInstance, Array<String*>& nameList, String*& namePtr)
|
||||||
|
{
|
||||||
|
if (namePtr == NULL)
|
||||||
|
{
|
||||||
|
namePtr = new String(module->TypeToString(typeInstance));
|
||||||
|
nameList.Add(namePtr);
|
||||||
|
}
|
||||||
|
mTypeName = namePtr;
|
||||||
|
mTypeInstance = typeInstance;
|
||||||
|
mPriority = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Sort(Array<_SortedTypeEntry>& list)
|
||||||
|
{
|
||||||
|
list.Sort(
|
||||||
|
[](const _SortedTypeEntry& lhs, const _SortedTypeEntry& rhs)
|
||||||
|
{
|
||||||
|
if (lhs.mPriority != rhs.mPriority)
|
||||||
|
return lhs.mPriority > rhs.mPriority;
|
||||||
|
return *lhs.mTypeName < *rhs.mTypeName;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SortRev(Array<_SortedTypeEntry>& list)
|
||||||
|
{
|
||||||
|
list.Sort(
|
||||||
|
[](const _SortedTypeEntry& lhs, const _SortedTypeEntry& rhs)
|
||||||
|
{
|
||||||
|
if (lhs.mPriority != rhs.mPriority)
|
||||||
|
return lhs.mPriority < rhs.mPriority;
|
||||||
|
return *lhs.mTypeName > *rhs.mTypeName;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Array<_SortedTypeEntry> preStaticInitList;
|
||||||
|
Array<_SortedTypeEntry> staticMarkList;
|
||||||
|
Array<_SortedTypeEntry> staticTLSList;
|
||||||
|
|
||||||
Array<BfType*> vdataTypeList;
|
Array<BfType*> vdataTypeList;
|
||||||
std::multimap<String, BfTypeInstance*> sortedStaticInitMap;
|
|
||||||
std::multimap<String, BfTypeInstance*> sortedStaticDtorMap;
|
|
||||||
std::multimap<String, BfTypeInstance*> sortedStaticMarkMap;
|
|
||||||
std::multimap<String, BfTypeInstance*> sortedStaticTLSMap;
|
|
||||||
HashSet<BfModule*> usedModuleSet;
|
HashSet<BfModule*> usedModuleSet;
|
||||||
HashSet<BfType*> reflectTypeSet;
|
HashSet<BfType*> reflectTypeSet;
|
||||||
HashSet<BfType*> reflectFieldTypeSet;
|
HashSet<BfType*> reflectFieldTypeSet;
|
||||||
|
@ -1265,15 +1319,13 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
// continue;
|
// continue;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (typeInst->mHasStaticInitMethod)
|
String* typeName = NULL;
|
||||||
sortedStaticInitMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
|
if ((typeInst->mHasStaticInitMethod) || (typeInst->mHasStaticDtorMethod))
|
||||||
else if (typeInst->mHasStaticDtorMethod) // Only store types not already in the static init map
|
preStaticInitList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
|
||||||
sortedStaticDtorMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
|
|
||||||
|
|
||||||
if ((typeInst->mHasStaticMarkMethod) && (mOptions.mEnableRealtimeLeakCheck))
|
if ((typeInst->mHasStaticMarkMethod) && (mOptions.mEnableRealtimeLeakCheck))
|
||||||
sortedStaticMarkMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
|
staticMarkList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
|
||||||
if ((typeInst->mHasTLSFindMethod) && (mOptions.mEnableRealtimeLeakCheck))
|
if ((typeInst->mHasTLSFindMethod) && (mOptions.mEnableRealtimeLeakCheck))
|
||||||
sortedStaticTLSMap.insert(std::make_pair(bfModule->TypeToString(type), typeInst));
|
staticTLSList.Add(_SortedTypeEntry(bfModule, typeInst, typeNameList, typeName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1541,13 +1593,7 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
|
BfIRType voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
|
||||||
BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
|
BfIRType int32Type = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_Int32));
|
||||||
|
|
||||||
struct _StaticInitEntry
|
Array<_SortedTypeEntry> staticInitList;
|
||||||
{
|
|
||||||
int mPriority;
|
|
||||||
BfTypeInstance* mTypeInstance;
|
|
||||||
};
|
|
||||||
Array<_StaticInitEntry> staticInitList;
|
|
||||||
|
|
||||||
// Populate staticInitList
|
// Populate staticInitList
|
||||||
{
|
{
|
||||||
Dictionary<int, BfTypeInstance*> pendingIDToInstanceMap;
|
Dictionary<int, BfTypeInstance*> pendingIDToInstanceMap;
|
||||||
|
@ -1558,10 +1604,10 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
for (int pass = 0; true; pass++)
|
for (int pass = 0; true; pass++)
|
||||||
{
|
{
|
||||||
bool hadAdd = false;
|
bool hadAdd = false;
|
||||||
for (auto& mapEntry : sortedStaticInitMap)
|
for (auto& mapEntry : preStaticInitList)
|
||||||
{
|
{
|
||||||
auto typeInst = mapEntry.second;
|
auto typeInst = mapEntry.mTypeInstance;
|
||||||
if ((typeInst != NULL) && (!typeInst->IsUnspecializedType()) && (typeInst->mHasStaticInitMethod))
|
if ((typeInst != NULL) && (!typeInst->IsUnspecializedType()))
|
||||||
{
|
{
|
||||||
if (pass == 0)
|
if (pass == 0)
|
||||||
{
|
{
|
||||||
|
@ -1588,8 +1634,9 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
|
|
||||||
if (!hadInitAfterAttribute)
|
if (!hadInitAfterAttribute)
|
||||||
{
|
{
|
||||||
staticInitList.push_back({ priority, typeInst });
|
mapEntry.mPriority = priority;
|
||||||
mapEntry.second = NULL;
|
staticInitList.Add(mapEntry);
|
||||||
|
mapEntry.mTypeInstance = NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -1628,7 +1675,7 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
|
|
||||||
if (doAdd)
|
if (doAdd)
|
||||||
{
|
{
|
||||||
staticInitList.push_back({ 0, typeInst });
|
staticInitList.Add(mapEntry);
|
||||||
pendingIDToInstanceMap.Remove(typeInst->mTypeId);
|
pendingIDToInstanceMap.Remove(typeInst->mTypeId);
|
||||||
hadAdd = true;
|
hadAdd = true;
|
||||||
}
|
}
|
||||||
|
@ -1639,11 +1686,7 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
|
|
||||||
if (pass == 0)
|
if (pass == 0)
|
||||||
{
|
{
|
||||||
std::sort(staticInitList.begin(), staticInitList.end(),
|
_SortedTypeEntry::Sort(staticInitList);
|
||||||
[](const _StaticInitEntry& lhs, const _StaticInitEntry& rhs)
|
|
||||||
{
|
|
||||||
return lhs.mPriority > rhs.mPriority;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((pass > 0) && (!hadAdd) && (pendingIDToInstanceMap.size() > 0)) // Circular ref?
|
if ((pass > 0) && (!hadAdd) && (pendingIDToInstanceMap.size() > 0)) // Circular ref?
|
||||||
|
@ -1654,24 +1697,6 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We want to call DTORS in reverse order from CTORS
|
|
||||||
Array<BfTypeInstance*> dtorList;
|
|
||||||
|
|
||||||
for (intptr idx = staticInitList.size() - 1; idx >= 0; idx--)
|
|
||||||
{
|
|
||||||
auto typeInst = staticInitList[idx].mTypeInstance;
|
|
||||||
if (typeInst->mHasStaticDtorMethod)
|
|
||||||
{
|
|
||||||
dtorList.push_back(typeInst);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto itr = sortedStaticDtorMap.rbegin(); itr != sortedStaticDtorMap.rend(); itr++)
|
|
||||||
{
|
|
||||||
auto typeInst = itr->second;
|
|
||||||
dtorList.push_back(typeInst);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate "BfCallAllStaticDtors"
|
/// Generate "BfCallAllStaticDtors"
|
||||||
BfIRFunction dtorFunc;
|
BfIRFunction dtorFunc;
|
||||||
{
|
{
|
||||||
|
@ -1684,8 +1709,11 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
|
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
|
||||||
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
|
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
|
||||||
|
|
||||||
for (auto typeInst : dtorList)
|
for (int i = staticInitList.mSize - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
|
auto typeInst = staticInitList[i].mTypeInstance;
|
||||||
|
if (!typeInst->mHasStaticDtorMethod)
|
||||||
|
continue;
|
||||||
for (auto& methodGroup : typeInst->mMethodInstanceGroups)
|
for (auto& methodGroup : typeInst->mMethodInstanceGroups)
|
||||||
{
|
{
|
||||||
auto methodInstance = methodGroup.mDefault;
|
auto methodInstance = methodGroup.mDefault;
|
||||||
|
@ -1871,10 +1899,12 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
|
|
||||||
for (auto& staticInitEntry : staticInitList)
|
for (auto& staticInitEntry : staticInitList)
|
||||||
{
|
{
|
||||||
|
auto typeInst = staticInitEntry.mTypeInstance;
|
||||||
|
if (!typeInst->mHasStaticInitMethod)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (staticInitEntry.mPriority < 100)
|
if (staticInitEntry.mPriority < 100)
|
||||||
_CheckSharedLibLoad();
|
_CheckSharedLibLoad();
|
||||||
|
|
||||||
auto typeInst = staticInitEntry.mTypeInstance;
|
|
||||||
for (auto& methodGroup : typeInst->mMethodInstanceGroups)
|
for (auto& methodGroup : typeInst->mMethodInstanceGroups)
|
||||||
{
|
{
|
||||||
auto methodInstance = methodGroup.mDefault;
|
auto methodInstance = methodGroup.mDefault;
|
||||||
|
@ -2161,9 +2191,11 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
{
|
{
|
||||||
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
|
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
|
||||||
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
|
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
|
||||||
for (auto& mapEntry : sortedStaticMarkMap)
|
|
||||||
|
_SortedTypeEntry::Sort(staticMarkList);
|
||||||
|
for (auto& mapEntry : staticMarkList)
|
||||||
{
|
{
|
||||||
auto typeInst = mapEntry.second;
|
auto typeInst = mapEntry.mTypeInstance;
|
||||||
if (typeInst->IsUnspecializedType())
|
if (typeInst->IsUnspecializedType())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -2203,9 +2235,10 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
|
||||||
{
|
{
|
||||||
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
|
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
|
||||||
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
|
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
|
||||||
for (auto& mapEntry : sortedStaticTLSMap)
|
_SortedTypeEntry::Sort(staticTLSList);
|
||||||
|
for (auto& mapEntry : staticTLSList)
|
||||||
{
|
{
|
||||||
auto typeInst = mapEntry.second;
|
auto typeInst = mapEntry.mTypeInstance;
|
||||||
if (typeInst->IsUnspecializedType())
|
if (typeInst->IsUnspecializedType())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue