From ba9ce75a0b4d18f83df5f8457ab4ae529366ae0e Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sun, 27 Dec 2020 10:56:14 -0800 Subject: [PATCH] Fixed inlining issues --- IDEHelper/Compiler/BfModule.cpp | 56 +++++++++++++--------- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 2 +- IDEHelper/Compiler/BfResolvedTypeUtils.cpp | 3 +- IDEHelper/Compiler/BfResolvedTypeUtils.h | 3 +- IDEHelper/Compiler/BfSystem.cpp | 26 +++++++++- IDEHelper/Compiler/BfSystem.h | 2 + 6 files changed, 66 insertions(+), 26 deletions(-) diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index a413c78d..495fd384 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -3325,6 +3325,15 @@ void BfModule::AddDependency(BfType* usedType, BfType* userType, BfDependencyMap if ((usedType->mRebuildFlags & BfTypeRebuildFlag_AwaitingReference) != 0) mContext->MarkAsReferenced(checkDType); +#ifdef _DEBUG + // If a MethodRef depends ON US, that means it's a local method that we own + if (userType->IsMethodRef()) + { + auto methodRefType = (BfMethodRefType*)userType; + BF_ASSERT(methodRefType->mOwner == checkDType); + } +#endif + if (!checkDType->mDependencyMap.AddUsedBy(userType, flags)) return; if (checkDType->IsGenericTypeInstance()) @@ -4744,6 +4753,9 @@ BfIRValue BfModule::CreateClassVDataGlobal(BfTypeInstance* typeInstance, int* ou classVDataName); mClassVDataRefs[typeInstance] = globalVariable; + + if (mCurTypeInstance != NULL) + AddDependency(typeInstance, mCurTypeInstance, BfDependencyMap::DependencyFlag_StaticValue); } return globalVariable; } @@ -5554,7 +5566,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin if (!origVTable.empty()) typeInstance->mVirtualMethodTable = origVTable; } - + int ifaceMethodExtStart = (int)typeInstance->GetIFaceVMethodSize(); if (typeInstance->mHotTypeData != NULL) { @@ -5705,7 +5717,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin int idx = interfaceEntry->mStartVirtualIdx; int endVirtualIdx = interfaceEntry->mStartVirtualIdx + interfaceEntry->mInterfaceType->mVirtualMethodTableSize; - if (endVirtualIdx > ifaceMethodExtStart) + if ((endVirtualIdx > ifaceMethodExtStart) && (ifaceMethodExtVar)) vtablePtr = mBfIRBuilder->CreateInBoundsGEP(ifaceMethodExtVar, 0, interfaceEntry->mStartVirtualIdx - ifaceMethodExtStart); else vtablePtr = mBfIRBuilder->CreateInBoundsGEP(classVDataVar, 0, iFaceMethodStartIdx + interfaceEntry->mStartVirtualIdx); @@ -9767,7 +9779,9 @@ BfModule* BfModule::GetSpecializedMethodModule(const SizedArrayImpl& { String specModuleName = mModuleName; for (auto bfProject : projectList) - specModuleName += StrFormat("@%s", bfProject->mName.c_str()); + { + specModuleName += StrFormat("@%s", bfProject->mSafeName.c_str()); + } specModule = new BfModule(mContext, specModuleName); specModule->mProject = mainModule->mProject; specModule->mParentModule = mainModule; @@ -12093,11 +12107,6 @@ void BfModule::AddMethodReference(const BfMethodRef& methodRef, BfGetMethodInsta BfModuleMethodInstance BfModule::ReferenceExternalMethodInstance(BfMethodInstance* methodInstance, BfGetMethodInstanceFlags flags) { - if (mIsConstModule) - { - NOP; - } - if ((flags & BfGetMethodInstanceFlag_ResultNotUsed) != 0) return BfModuleMethodInstance(methodInstance, BfIRValue()); @@ -12291,11 +12300,6 @@ BfModule* BfModule::GetOrCreateMethodModule(BfMethodInstance* methodInstance) BfModuleMethodInstance BfModule::GetMethodInstance(BfTypeInstance* typeInst, BfMethodDef* methodDef, const BfTypeVector& methodGenericArguments, BfGetMethodInstanceFlags flags, BfTypeInstance* foreignType) { - if (mIsConstModule) - { - NOP; - } - if (methodDef->mMethodType == BfMethodType_Init) return BfModuleMethodInstance(); @@ -12417,11 +12421,13 @@ BfModuleMethodInstance BfModule::GetMethodInstance(BfTypeInstance* typeInst, BfM auto defFlags = (BfGetMethodInstanceFlags)(flags & ~BfGetMethodInstanceFlag_ForceInline); + defFlags = (BfGetMethodInstanceFlags)(flags | BfGetMethodInstanceFlag_NoReference); + if (mIsConstModule) { - defFlags = (BfGetMethodInstanceFlags)(flags | BfGetMethodInstanceFlag_NoReference); + defFlags = (BfGetMethodInstanceFlags)(flags | BfGetMethodInstanceFlag_MethodInstanceOnly); if (!mCompiler->mIsResolveOnly) - defFlags = (BfGetMethodInstanceFlags)(flags | BfGetMethodInstanceFlag_NoForceReification | BfGetMethodInstanceFlag_Unreified | BfGetMethodInstanceFlag_NoReference); + defFlags = (BfGetMethodInstanceFlags)(flags | BfGetMethodInstanceFlag_NoForceReification | BfGetMethodInstanceFlag_Unreified | BfGetMethodInstanceFlag_MethodInstanceOnly); } // Not extern @@ -12624,7 +12630,7 @@ BfModuleMethodInstance BfModule::GetMethodInstance(BfTypeInstance* typeInst, BfM { methodInstance = methodInstGroup->mDefault; - if ((methodInstance != NULL) && ((flags & BfGetMethodInstanceFlag_NoReference) != 0)) + if ((methodInstance != NULL) && ((flags & BfGetMethodInstanceFlag_MethodInstanceOnly) != 0)) return methodInstance; if ((methodInstance != NULL) && (isReified) && (!methodInstance->mIsReified)) @@ -12735,7 +12741,7 @@ BfModuleMethodInstance BfModule::GetMethodInstance(BfTypeInstance* typeInst, BfM { methodInstance = *methodInstancePtr; - if ((flags & BfGetMethodInstanceFlag_NoReference) != 0) + if ((flags & BfGetMethodInstanceFlag_MethodInstanceOnly) != 0) return methodInstance; if ((methodInstance->mRequestedByAutocomplete) && (!mCompiler->IsAutocomplete())) @@ -12854,6 +12860,9 @@ BfModuleMethodInstance BfModule::GetMethodInstance(BfTypeInstance* typeInst, BfM return BfModuleMethodInstance(methodInstance, BfIRFunction()); else { + if ((flags & (BfGetMethodInstanceFlag_MethodInstanceOnly | BfGetMethodInstanceFlag_NoReference)) != 0) + return methodInstance; + if (methodInstance->mDeclModule != this) return ReferenceExternalMethodInstance(methodInstance, flags); @@ -12887,6 +12896,8 @@ BfModuleMethodInstance BfModule::GetMethodInstance(BfTypeInstance* typeInst, BfM auto specMethodInstance = specModule->GetMethodInstance(typeInst, methodDef, methodGenericArguments, (BfGetMethodInstanceFlags)(flags | BfGetMethodInstanceFlag_ExplicitSpecializedModule)); if (mAwaitingInitFinish) return BfModuleMethodInstance(specMethodInstance.mMethodInstance, BfIRFunction()); + if ((flags & (BfGetMethodInstanceFlag_MethodInstanceOnly | BfGetMethodInstanceFlag_NoReference)) != 0) + return specMethodInstance.mMethodInstance; return ReferenceExternalMethodInstance(specMethodInstance.mMethodInstance, flags); } @@ -13247,9 +13258,10 @@ BfTypedValue BfModule::ReferenceStaticField(BfFieldInstance* fieldInstance) } } - if (mIsScratchModule) + if ((mIsScratchModule) && (mCompiler->mIsResolveOnly)) { - // Just fake it for the extern and unspecialized modules + // Just fake it for the extern and unspecialized modules + // We can't do this for compilation because unreified methods with default params need to get acutal global variable refs return BfTypedValue(mBfIRBuilder->CreateConstNull(), fieldInstance->GetResolvedType(), true); } @@ -14935,7 +14947,7 @@ BfTypedValue BfModule::CallBaseCtorCalc(bool constOnly) bindResult.mWantsArgs = true; { SetAndRestoreValue prevIgnoreWrites(mBfIRBuilder->mIgnoreWrites, true); - exprEvaluator.ResolveArgValues(argValues, BfResolveArgFlag_DeferParamEval); + exprEvaluator.ResolveArgValues(argValues, BfResolveArgsFlag_DeferParamEval); SetAndRestoreValue prevBindResult(exprEvaluator.mFunctionBindResult, &bindResult); exprEvaluator.MatchConstructor(targetRefNode, NULL, target, targetType, argValues, true, true); } @@ -14978,7 +14990,7 @@ BfTypedValue BfModule::CallBaseCtorCalc(bool constOnly) // Do it again, but without mIgnoreWrites set BfResolvedArgs argValues; argValues.Init(&ctorInvocation->mArguments); - exprEvaluator.ResolveArgValues(argValues, BfResolveArgFlag_DeferParamEval); + exprEvaluator.ResolveArgValues(argValues, BfResolveArgsFlag_DeferParamEval); BfFunctionBindResult bindResult; bindResult.mSkipThis = true; @@ -16272,7 +16284,7 @@ void BfModule::EmitCtorBody(bool& skipBody) OutputDebugStrF("Expr: %@ %d\n", argValues.mArguments->mVals, argValues.mArguments->mSize); } } - exprEvaluator.ResolveArgValues(argValues, BfResolveArgFlag_DeferParamEval); + exprEvaluator.ResolveArgValues(argValues, BfResolveArgsFlag_DeferParamEval); BfTypedValue appendIdxVal; if (methodDef->mHasAppend) diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index b953c21b..c8b077df 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -9768,7 +9768,7 @@ BfIRValue BfModule::CastToFunction(BfAstNode* srcNode, const BfTypedValue& targe auto dataType = GetPrimitiveType(BfTypeCode_IntPtr); if (!methodRefMethod.mFunc) { - if (HasCompiledOutput()) + if ((!methodInstance->mIsUnspecialized) && (HasCompiledOutput())) AssertErrorState(); return GetDefaultValue(dataType); } diff --git a/IDEHelper/Compiler/BfResolvedTypeUtils.cpp b/IDEHelper/Compiler/BfResolvedTypeUtils.cpp index 2d870a64..61bcdebe 100644 --- a/IDEHelper/Compiler/BfResolvedTypeUtils.cpp +++ b/IDEHelper/Compiler/BfResolvedTypeUtils.cpp @@ -552,7 +552,7 @@ BfMethodInstance::~BfMethodInstance() } if (mHasMethodRefType) - { + { auto module = GetOwner()->mModule; if (!module->mContext->mDeleting) { @@ -593,6 +593,7 @@ void BfMethodInstance::CopyFrom(BfMethodInstance* methodInstance) *mMethodInfoEx->mClosureInstanceInfo = *methodInstance->mMethodInfoEx->mClosureInstanceInfo; } } + mHasMethodRefType = false; mHasBeenProcessed = false; mIRFunction = BfIRValue(); mMethodProcessRequest = NULL; diff --git a/IDEHelper/Compiler/BfResolvedTypeUtils.h b/IDEHelper/Compiler/BfResolvedTypeUtils.h index 02fcb20a..7cab9dc4 100644 --- a/IDEHelper/Compiler/BfResolvedTypeUtils.h +++ b/IDEHelper/Compiler/BfResolvedTypeUtils.h @@ -74,7 +74,8 @@ enum BfGetMethodInstanceFlags : uint16 BfGetMethodInstanceFlag_DisableObjectAccessChecks = 0x200, BfGetMethodInstanceFlag_NoInline = 0x400, BfGetMethodInstanceFlag_DepthExceeded = 0x800, - BfGetMethodInstanceFlag_NoReference = 0x1000 + BfGetMethodInstanceFlag_NoReference = 0x1000, + BfGetMethodInstanceFlag_MethodInstanceOnly = 0x2000 }; class BfDependencyMap diff --git a/IDEHelper/Compiler/BfSystem.cpp b/IDEHelper/Compiler/BfSystem.cpp index 09782468..94c6a82a 100644 --- a/IDEHelper/Compiler/BfSystem.cpp +++ b/IDEHelper/Compiler/BfSystem.cpp @@ -3689,6 +3689,29 @@ BF_EXPORT BfProject* BF_CALLTYPE BfSystem_CreateProject(BfSystem* bfSystem, cons bfProject->mSystem = bfSystem; bfProject->mIdx = (int)bfSystem->mProjects.size(); bfSystem->mProjects.push_back(bfProject); + + String safeProjectName = projectName; + for (auto& c : safeProjectName) + { + if (((c >= 'A') && (c <= 'Z')) || + ((c >= 'a') && (c <= 'z')) || + ((c >= '0') && (c <= '9'))) + { + // Leave + } + else + c = '_'; + } + + String tryName = safeProjectName; + for (int i = 2; true; i++) + { + if (bfSystem->mUsedSafeProjectNames.Add(ToUpper(tryName))) + break; + tryName = safeProjectName + StrFormat("_%d", i); + } + bfProject->mSafeName = tryName; + BfLogSys(bfSystem, "Creating project %p\n", bfProject); return bfProject; } @@ -3774,7 +3797,8 @@ BF_EXPORT void BF_CALLTYPE BfProject_Delete(BfProject* bfProject) bfProject->mDeleteStage = BfProject::DeleteStage_Queued; bfSystem->mProjectDeleteQueue.push_back(bfProject); - + bfSystem->mUsedSafeProjectNames.Remove(bfProject->mSafeName); + BF_ASSERT(bfSystem->mProjects[bfProject->mIdx] == bfProject); bool wasRemoved = bfSystem->mProjects.Remove(bfProject); BF_ASSERT(wasRemoved); diff --git a/IDEHelper/Compiler/BfSystem.h b/IDEHelper/Compiler/BfSystem.h index a53698fd..3a736c0e 100644 --- a/IDEHelper/Compiler/BfSystem.h +++ b/IDEHelper/Compiler/BfSystem.h @@ -1079,6 +1079,7 @@ public: public: BfSystem* mSystem; String mName; + String mSafeName; Array mDependencies; BfTargetType mTargetType; BfCodeGenOptions mCodeGenOptions; @@ -1409,6 +1410,7 @@ public: CritSect mDataLock; // short-lived, hold only while active modifying data // The following are protected by mDataLock: + HashSet mUsedSafeProjectNames; Array mProjects; Array mProjectDeleteQueue; Array mParsers;