1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00

Improved "const" handling of local variable when referencing globals

This commit is contained in:
Brian Fiete 2025-05-22 06:03:18 +02:00
parent 989e8455e8
commit e7a966c1b5
3 changed files with 7 additions and 27 deletions

View file

@ -4341,30 +4341,6 @@ BfTypedValue BfExprEvaluator::LoadLocal(BfLocalVariable* varDecl, bool allowRef)
else if (varDecl->mConstValue) else if (varDecl->mConstValue)
{ {
localResult = BfTypedValue(varDecl->mConstValue, varDecl->mResolvedType, false); localResult = BfTypedValue(varDecl->mConstValue, varDecl->mResolvedType, false);
if ((varDecl->mResolvedType->IsRef()) && (!allowRef))
{
BfRefType* refType = (BfRefType*)varDecl->mResolvedType;
BfType* innerType = refType->mElementType;
if (innerType->IsValuelessNonOpaqueType())
{
if (refType->mRefKind == BfRefType::RefKind_Mut)
return BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), innerType, BfTypedValueKind_MutableValue);
return BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), innerType, varDecl->mIsReadOnly ? BfTypedValueKind_ReadOnlyAddr : BfTypedValueKind_Addr);
}
if (refType->mRefKind == BfRefType::RefKind_Mut)
{
if (innerType->IsGenericParam())
{
localResult = BfTypedValue(varDecl->mConstValue, innerType, BfTypedValueKind_MutableValue);
return localResult;
}
}
localResult = BfTypedValue(varDecl->mConstValue, innerType, varDecl->mIsReadOnly ? BfTypedValueKind_ReadOnlyAddr : BfTypedValueKind_Addr);
}
} }
else if (varDecl->mIsSplat) else if (varDecl->mIsSplat)
{ {

View file

@ -666,6 +666,10 @@ bool BfIRConstHolder::IsConstValue(BfIRValue value)
return false; return false;
if (constant->mConstType == BfConstType_Undef) if (constant->mConstType == BfConstType_Undef)
return false; return false;
if (constant->mConstType == BfConstType_GEP32_2)
return IsConstValue(BfIRValue(BfIRValueFlags_Const, ((BfConstantGEP32_2*)constant)->mTarget));
if (constant->mConstType == BfConstType_BitCast)
return IsConstValue(BfIRValue(BfIRValueFlags_Const, ((BfConstantBitCast*)constant)->mTarget));
return true; return true;
} }

View file

@ -1714,7 +1714,7 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD
localDef->mIsReadOnly = true; localDef->mIsReadOnly = true;
if (initValue) if (initValue)
{ {
if ((initValue.mValue) && (initValue.mValue.IsConst())) if (mBfIRBuilder->IsConstValue(initValue.mValue))
{ {
isConst = true; isConst = true;
} }