mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 19:48:20 +02:00
Improved string table usage tracking
This commit is contained in:
parent
bb2fe56dc9
commit
9d79db063b
6 changed files with 94 additions and 55 deletions
|
@ -137,11 +137,11 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
|
|||
int stringId = mModule->GetStringPoolIdx(mResult.mValue);
|
||||
if (stringId != -1)
|
||||
{
|
||||
if ((flags & BfConstResolveFlag_RemapFromStringId) != 0)
|
||||
if ((flags & BfConstResolveFlag_ActualizeValues) != 0)
|
||||
{
|
||||
prevIgnoreWrites.Restore();
|
||||
mModule->mBfIRBuilder->PopulateType(mResult.mType);
|
||||
return BfTypedValue(mModule->GetStringObjectValue(stringId), mResult.mType);
|
||||
return BfTypedValue(mModule->GetStringObjectValue(stringId, false, true), mResult.mType);
|
||||
}
|
||||
|
||||
return BfTypedValue(mModule->mBfIRBuilder->CreateConst(BfTypeCode_StringId, stringId), toType);
|
||||
|
@ -232,7 +232,9 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
|
|||
}*/
|
||||
|
||||
mModule->FixIntUnknown(mResult);
|
||||
mModule->FixValueActualization(mResult);
|
||||
|
||||
if ((flags & BfConstResolveFlag_NoActualizeValues) == 0)
|
||||
mModule->FixValueActualization(mResult, !prevIgnoreWrites.mPrevVal || ((flags & BfConstResolveFlag_ActualizeValues) != 0));
|
||||
|
||||
return mResult;
|
||||
}
|
||||
|
|
|
@ -15,9 +15,10 @@ enum BfConstResolveFlags
|
|||
BfConstResolveFlag_ExplicitCast = 1,
|
||||
BfConstResolveFlag_NoCast = 2,
|
||||
BfConstResolveFlag_AllowSoftFail = 4,
|
||||
BfConstResolveFlag_RemapFromStringId = 8,
|
||||
BfConstResolveFlag_ArrayInitSize = 0x10,
|
||||
BfConstResolveFlag_AllowGlobalVariable = 0x20,
|
||||
BfConstResolveFlag_ActualizeValues = 8,
|
||||
BfConstResolveFlag_NoActualizeValues = 0x10,
|
||||
BfConstResolveFlag_ArrayInitSize = 0x20,
|
||||
BfConstResolveFlag_AllowGlobalVariable = 0x40,
|
||||
};
|
||||
|
||||
class BfConstResolver : public BfExprEvaluator
|
||||
|
|
|
@ -1654,8 +1654,11 @@ String* BfModule::GetStringPoolString(BfIRValue constantStr, BfIRConstHolder * c
|
|||
return NULL;
|
||||
}
|
||||
|
||||
BfIRValue BfModule::GetStringCharPtr(int stringId)
|
||||
BfIRValue BfModule::GetStringCharPtr(int stringId, bool force)
|
||||
{
|
||||
if ((mBfIRBuilder->mIgnoreWrites) && (!force))
|
||||
return mBfIRBuilder->CreateConst(BfTypeCode_StringId, stringId);
|
||||
|
||||
BfIRValue* irValue = NULL;
|
||||
if (!mStringCharPtrPool.TryAdd(stringId, NULL, &irValue))
|
||||
return *irValue;
|
||||
|
@ -1670,13 +1673,13 @@ BfIRValue BfModule::GetStringCharPtr(int stringId)
|
|||
return strCharPtrConst;
|
||||
}
|
||||
|
||||
BfIRValue BfModule::GetStringCharPtr(BfIRValue strValue)
|
||||
BfIRValue BfModule::GetStringCharPtr(BfIRValue strValue, bool force)
|
||||
{
|
||||
if (strValue.IsConst())
|
||||
{
|
||||
int stringId = GetStringPoolIdx(strValue);
|
||||
BF_ASSERT(stringId != -1);
|
||||
return GetStringCharPtr(stringId);
|
||||
return GetStringCharPtr(stringId, force);
|
||||
}
|
||||
|
||||
BfIRValue charPtrPtr = mBfIRBuilder->CreateInBoundsGEP(strValue, 0, 1);
|
||||
|
@ -1684,28 +1687,33 @@ BfIRValue BfModule::GetStringCharPtr(BfIRValue strValue)
|
|||
return charPtr;
|
||||
}
|
||||
|
||||
BfIRValue BfModule::GetStringCharPtr(const StringImpl& str)
|
||||
BfIRValue BfModule::GetStringCharPtr(const StringImpl& str, bool force)
|
||||
{
|
||||
return GetStringCharPtr(GetStringObjectValue(str));
|
||||
return GetStringCharPtr(GetStringObjectValue(str, force), force);
|
||||
}
|
||||
|
||||
BfIRValue BfModule::GetStringObjectValue(int strId)
|
||||
BfIRValue BfModule::GetStringObjectValue(int strId, bool define, bool force)
|
||||
{
|
||||
BfIRValue* objValue;
|
||||
if (mStringObjectPool.TryGetValue(strId, &objValue))
|
||||
return *objValue;
|
||||
|
||||
auto stringPoolEntry = mContext->mStringObjectIdMap[strId];
|
||||
return GetStringObjectValue(stringPoolEntry.mString, true);
|
||||
return GetStringObjectValue(stringPoolEntry.mString, define, force);
|
||||
}
|
||||
|
||||
BfIRValue BfModule::GetStringObjectValue(const StringImpl& str, bool define)
|
||||
BfIRValue BfModule::GetStringObjectValue(const StringImpl& str, bool define, bool force)
|
||||
{
|
||||
auto stringType = ResolveTypeDef(mCompiler->mStringTypeDef, define ? BfPopulateType_Data : BfPopulateType_Declaration);
|
||||
mBfIRBuilder->PopulateType(stringType);
|
||||
|
||||
int strId = mContext->GetStringLiteralId(str);
|
||||
|
||||
if ((mBfIRBuilder->mIgnoreWrites) && (!force))
|
||||
{
|
||||
return mBfIRBuilder->CreateConst(BfTypeCode_StringId, strId);
|
||||
}
|
||||
|
||||
BfIRValue* irValuePtr = NULL;
|
||||
if (!mStringObjectPool.TryAdd(strId, NULL, &irValuePtr))
|
||||
return *irValuePtr;
|
||||
|
@ -5900,8 +5908,8 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
|
||||
if (needsTypeNames)
|
||||
{
|
||||
typeNameConst = GetStringObjectValue(typeDef->mName->mString, true);
|
||||
namespaceConst = GetStringObjectValue(typeDef->mNamespace.ToString(), true);
|
||||
typeNameConst = GetStringObjectValue(typeDef->mName->mString, !mIsComptimeModule);
|
||||
namespaceConst = GetStringObjectValue(typeDef->mNamespace.ToString(), !mIsComptimeModule);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -6041,7 +6049,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
*orderedIdPtr = (int)usedStringIdMap.size() - 1;
|
||||
}
|
||||
|
||||
GetStringObjectValue(stringId);
|
||||
GetStringObjectValue(stringId, true, true);
|
||||
PUSH_INT8(0xFF); // String
|
||||
PUSH_INT32(*orderedIdPtr);
|
||||
argIdx++;
|
||||
|
@ -6061,7 +6069,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
*orderedIdPtr = (int)usedStringIdMap.size() - 1;
|
||||
}
|
||||
|
||||
GetStringObjectValue(stringId);
|
||||
GetStringObjectValue(stringId, true, true);
|
||||
PUSH_INT8(0xFF); // String
|
||||
PUSH_INT32(*orderedIdPtr);
|
||||
argIdx++;
|
||||
|
@ -6225,7 +6233,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
BfType* payloadType = typeInstance->GetUnionInnerType();
|
||||
if (!payloadType->IsValuelessType())
|
||||
{
|
||||
BfIRValue payloadNameConst = GetStringObjectValue("$payload", true);
|
||||
BfIRValue payloadNameConst = GetStringObjectValue("$payload", !mIsComptimeModule);
|
||||
SizedArray<BfIRValue, 8> payloadFieldVals =
|
||||
{
|
||||
emptyValueType,
|
||||
|
@ -6240,7 +6248,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
}
|
||||
|
||||
BfType* dscrType = typeInstance->GetDiscriminatorType();
|
||||
BfIRValue dscrNameConst = GetStringObjectValue("$discriminator", true);
|
||||
BfIRValue dscrNameConst = GetStringObjectValue("$discriminator", !mIsComptimeModule);
|
||||
SizedArray<BfIRValue, 8> dscrFieldVals =
|
||||
{
|
||||
emptyValueType,
|
||||
|
@ -6262,7 +6270,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
BfFieldInstance* fieldInstance = &typeInstance->mFieldInstances[fieldIdx];
|
||||
BfFieldDef* fieldDef = fieldInstance->GetFieldDef();
|
||||
|
||||
BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, true);
|
||||
BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, !mIsComptimeModule);
|
||||
|
||||
int typeId = 0;
|
||||
auto fieldType = fieldInstance->GetResolvedType();
|
||||
|
@ -6538,7 +6546,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
funcVal = mBfIRBuilder->CreateBitCast(moduleMethodInstance.mFunc, voidPtrIRType);
|
||||
}
|
||||
|
||||
BfIRValue methodNameConst = GetStringObjectValue(methodDef->mName, true);
|
||||
BfIRValue methodNameConst = GetStringObjectValue(methodDef->mName, !mIsComptimeModule);
|
||||
|
||||
BfMethodFlags methodFlags = defaultMethod->GetMethodFlags();
|
||||
|
||||
|
@ -6561,7 +6569,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
|||
if (defaultMethod->GetParamIsSplat(paramIdx))
|
||||
paramFlags = (ParamFlags)(paramFlags | ParamFlag_Splat);
|
||||
|
||||
BfIRValue paramNameConst = GetStringObjectValue(paramName, true);
|
||||
BfIRValue paramNameConst = GetStringObjectValue(paramName, !mIsComptimeModule);
|
||||
|
||||
SizedArray<BfIRValue, 8> paramDataVals =
|
||||
{
|
||||
|
@ -10621,17 +10629,18 @@ BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConst
|
|||
auto constVal = mBfIRBuilder->CreateConst(constant, constHolder);
|
||||
BfTypedValue typedValue;
|
||||
|
||||
bool allowUnactualized = mBfIRBuilder->mIgnoreWrites;
|
||||
if (constant->mTypeCode == BfTypeCode_StringId)
|
||||
{
|
||||
if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) ||
|
||||
((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8))))
|
||||
{
|
||||
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, wantType), wantType);
|
||||
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, wantType, allowUnactualized), wantType);
|
||||
return typedValue;
|
||||
}
|
||||
|
||||
auto stringType = ResolveTypeDef(mCompiler->mStringTypeDef);
|
||||
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, stringType), stringType);
|
||||
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, stringType, allowUnactualized), stringType);
|
||||
}
|
||||
|
||||
if (!typedValue)
|
||||
|
@ -10675,7 +10684,27 @@ BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConst
|
|||
return BfTypedValue(irValue, wantType, false);
|
||||
}
|
||||
|
||||
BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowStringId)
|
||||
bool BfModule::HasUnactializedConstant(BfConstant* constant, BfIRConstHolder* constHolder)
|
||||
{
|
||||
if ((constant->mConstType == BfConstType_TypeOf) || (constant->mConstType == BfConstType_TypeOf_WithData))
|
||||
return true;
|
||||
if (constant->mTypeCode == BfTypeCode_StringId)
|
||||
return true;
|
||||
|
||||
if (constant->mConstType == BfConstType_Agg)
|
||||
{
|
||||
auto constArray = (BfConstantAgg*)constant;
|
||||
for (auto val : constArray->mValues)
|
||||
{
|
||||
if (HasUnactializedConstant(constHolder->GetConstant(val), constHolder))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowUnactualized)
|
||||
{
|
||||
if (constant->mTypeCode == BfTypeCode_NullPtr)
|
||||
{
|
||||
|
@ -10690,20 +10719,27 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con
|
|||
|
||||
if (constant->mTypeCode == BfTypeCode_StringId)
|
||||
{
|
||||
if (!allowStringId)
|
||||
if (!allowUnactualized)
|
||||
{
|
||||
if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) ||
|
||||
((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8))))
|
||||
{
|
||||
const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString;
|
||||
BfIRValue stringObjConst = GetStringObjectValue(str);
|
||||
BfIRValue stringObjConst = GetStringObjectValue(str, false, true);
|
||||
if (wantType->IsPointer())
|
||||
return GetStringCharPtr(stringObjConst);
|
||||
return GetStringCharPtr(stringObjConst, true);
|
||||
return stringObjConst;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (constant->mConstType == Beefy::BfConstType_TypeOf)
|
||||
{
|
||||
auto constTypeOf = (BfTypeOf_Const*)constant;
|
||||
AddDependency(constTypeOf->mType, mCurTypeInstance, BfDependencyMap::DependencyFlag_ExprTypeReference);
|
||||
return CreateTypeDataRef(constTypeOf->mType);
|
||||
}
|
||||
|
||||
if (constant->mConstType == BfConstType_Agg)
|
||||
{
|
||||
auto constArray = (BfConstantAgg*)constant;
|
||||
|
@ -11013,7 +11049,7 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri
|
|||
auto& fieldTypeInst = checkTypeInst->mFieldInstances[bestField->mIdx];
|
||||
if (assignExpr->mRight != NULL)
|
||||
{
|
||||
BfTypedValue result = constResolver.Resolve(assignExpr->mRight, fieldTypeInst.mResolvedType);
|
||||
BfTypedValue result = constResolver.Resolve(assignExpr->mRight, fieldTypeInst.mResolvedType, BfConstResolveFlag_NoActualizeValues);
|
||||
if (result)
|
||||
{
|
||||
CurrentAddToConstHolder(result.mValue);
|
||||
|
@ -14050,7 +14086,9 @@ void BfModule::DoLocalVariableDebugInfo(BfLocalVariable* localVarDef, bool doAli
|
|||
{
|
||||
if ((isConstant) && (!didConstToMem))
|
||||
{
|
||||
localVarDef->mDbgDeclareInst = mBfIRBuilder->DbgInsertValueIntrinsic(localVarDef->mConstValue, diVariable);
|
||||
BfTypedValue result(localVarDef->mConstValue, localVarDef->mResolvedType);
|
||||
FixValueActualization(result);
|
||||
localVarDef->mDbgDeclareInst = mBfIRBuilder->DbgInsertValueIntrinsic(result.mValue, diVariable);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -1538,11 +1538,11 @@ public:
|
|||
BfIRValue CreateStringCharPtr(const StringImpl& str, int stringId, bool define);
|
||||
int GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHolder = NULL);
|
||||
String* GetStringPoolString(BfIRValue constantStr, BfIRConstHolder* constHolder = NULL);
|
||||
BfIRValue GetStringCharPtr(int stringId);
|
||||
BfIRValue GetStringCharPtr(BfIRValue strValue);
|
||||
BfIRValue GetStringCharPtr(const StringImpl& str);
|
||||
BfIRValue GetStringObjectValue(int idx);
|
||||
BfIRValue GetStringObjectValue(const StringImpl& str, bool define = false);
|
||||
BfIRValue GetStringCharPtr(int stringId, bool force = false);
|
||||
BfIRValue GetStringCharPtr(BfIRValue strValue, bool force = false);
|
||||
BfIRValue GetStringCharPtr(const StringImpl& str, bool force = false);
|
||||
BfIRValue GetStringObjectValue(int idx, bool define, bool force);
|
||||
BfIRValue GetStringObjectValue(const StringImpl& str, bool define = false, bool force = false);
|
||||
BfIRValue CreateGlobalConstValue(const StringImpl& name, BfIRValue constant, BfIRType type, bool external);
|
||||
void VariantToString(StringImpl& str, const BfVariant& variant);
|
||||
StringT<128> TypeToString(BfType* resolvedType, Array<String>* genericMethodParamNameOverrides = NULL);
|
||||
|
@ -1553,8 +1553,9 @@ public:
|
|||
void pm(BfMethodInstance* type);
|
||||
void CurrentAddToConstHolder(BfIRValue& irVal);
|
||||
void ClearConstData();
|
||||
bool HasUnactializedConstant(BfConstant* constant, BfIRConstHolder* constHolder);
|
||||
BfTypedValue GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType);
|
||||
BfIRValue ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowStringId = false);
|
||||
BfIRValue ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowUnactualized = false);
|
||||
void ValidateCustomAttributes(BfCustomAttributes* customAttributes, BfAttributeTargets attrTarget);
|
||||
void GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttributeDirective* attributesDirective, BfAttributeTargets attrType, bool allowNonConstArgs = false, BfCaptureInfo* captureInfo = NULL);
|
||||
BfCustomAttributes* GetCustomAttributes(BfAttributeDirective* attributesDirective, BfAttributeTargets attrType, bool allowNonConstArgs = false, BfCaptureInfo* captureInfo = NULL);
|
||||
|
@ -1772,7 +1773,7 @@ public:
|
|||
BfType* FixIntUnknown(BfType* type);
|
||||
void FixIntUnknown(BfTypedValue& typedVal, BfType* matchType = NULL);
|
||||
void FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs);
|
||||
void FixValueActualization(BfTypedValue& typedVal);
|
||||
void FixValueActualization(BfTypedValue& typedVal, bool force = false);
|
||||
bool TypeEquals(BfTypedValue& val, BfType* type);
|
||||
BfTypeDef* ResolveGenericInstanceDef(BfGenericInstanceTypeRef* genericTypeRef, BfType** outType = NULL, BfResolveTypeRefFlags resolveFlags = BfResolveTypeRefFlag_None);
|
||||
BfType* ResolveType(BfType* lookupType, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = BfResolveTypeRefFlag_None);
|
||||
|
|
|
@ -6439,19 +6439,16 @@ void BfModule::FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs)
|
|||
FixIntUnknown(rhs);
|
||||
}
|
||||
|
||||
void BfModule::FixValueActualization(BfTypedValue& typedVal)
|
||||
void BfModule::FixValueActualization(BfTypedValue& typedVal, bool force)
|
||||
{
|
||||
if (!typedVal.mValue.IsConst())
|
||||
return;
|
||||
if (mBfIRBuilder->mIgnoreWrites)
|
||||
if ((mBfIRBuilder->mIgnoreWrites) && (!force))
|
||||
return;
|
||||
auto constant = mBfIRBuilder->GetConstant(typedVal.mValue);
|
||||
if (constant->mConstType == BfConstType_TypeOf)
|
||||
{
|
||||
auto constTypeOf = (BfTypeOf_Const*)constant;
|
||||
AddDependency(constTypeOf->mType, mCurTypeInstance, BfDependencyMap::DependencyFlag_ExprTypeReference);
|
||||
typedVal.mValue = CreateTypeDataRef(constTypeOf->mType);
|
||||
}
|
||||
if (!HasUnactializedConstant(constant, mBfIRBuilder))
|
||||
return;
|
||||
typedVal.mValue = ConstantToCurrent(constant, mBfIRBuilder, typedVal.mType, false);
|
||||
}
|
||||
|
||||
BfTypeInstance* BfModule::GetPrimitiveStructType(BfTypeCode typeCode)
|
||||
|
|
|
@ -1654,7 +1654,7 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD
|
|||
if (isConst)
|
||||
{
|
||||
BfConstResolver constResolver(this);
|
||||
initValue = constResolver.Resolve(varDecl->mInitializer, resolvedType, BfConstResolveFlag_RemapFromStringId);
|
||||
initValue = constResolver.Resolve(varDecl->mInitializer, resolvedType, BfConstResolveFlag_ActualizeValues);
|
||||
if (!initValue)
|
||||
initValue = GetDefaultTypedValue(resolvedType);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue