1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Fixed issues with global var addresses in const arrays

This commit is contained in:
Brian Fiete 2020-07-13 08:51:02 -07:00
parent 52af512b4f
commit b30a72719c
13 changed files with 376 additions and 36 deletions

View file

@ -5911,7 +5911,7 @@ void BfCompiler::CompileReified()
auto typeOptions = scratchModule->GetTypeOptions(typeDef);
if (typeOptions != NULL)
typeOptions->Apply(isAlwaysInclude, BfOptionFlags_ReflectAlwaysIncludeType);
isAlwaysInclude = typeOptions->Apply(isAlwaysInclude, BfOptionFlags_ReflectAlwaysIncludeType);
if (typeDef->mProject->IsTestProject())
{

View file

@ -17660,9 +17660,30 @@ void BfExprEvaluator::PerformUnaryOperation_OnResult(BfExpression* unaryOpExpr,
return;
}
if (mResult.mValue.IsConst())
{
auto constant = mModule->mBfIRBuilder->GetConstant(mResult.mValue);
bool isNull = constant->mTypeCode == BfTypeCode_NullPtr;
if (constant->mConstType == BfConstType_ExtractValue)
{
auto constExtract = (BfConstantExtractValue*)constant;
auto targetConst = mModule->mBfIRBuilder->GetConstantById(constExtract->mTarget);
if (targetConst->mConstType == BfConstType_AggZero)
isNull = true;
}
if (isNull)
{
mModule->Warn(0, "Cannot dereference a null pointer", unaryOpExpr);
mResult = mModule->GetDefaultTypedValue(mResult.mType, false, BfDefaultValueKind_Addr);
mResult = mModule->LoadValue(mResult);
}
}
auto derefTarget = mModule->LoadValue(mResult);
BfPointerType* pointerType = (BfPointerType*)derefTarget.mType;
BfPointerType* pointerType = (BfPointerType*)derefTarget.mType;
auto resolvedType = mModule->ResolveType(pointerType->mElementType);
if (resolvedType == NULL)
{

View file

@ -578,6 +578,8 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
{
BfConstant* copiedConst = NULL;
int chunkId = -1;
if ((fromConst->mConstType == BfConstType_BitCast) || (fromConst->mConstType == BfConstType_BitCastNull))
{
//HMM- This should never happen? Is that true? We always just store string refs as ints
@ -595,6 +597,21 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
ptrToInt->mToType = fromConstBitCast->mToType;
copiedConst = (BfConstant*)ptrToInt;
}
else if (fromConst->mConstType == BfConstType_GlobalVar)
{
auto fromGlobalVar = (BfGlobalVar*)fromConst;
auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
chunkId = mTempAlloc.GetChunkedId(constGV);
constGV->mStreamId = -1;
constGV->mConstType = BfConstType_GlobalVar;
constGV->mType = fromGlobalVar->mType;
constGV->mIsConst = fromGlobalVar->mIsConst;
constGV->mLinkageType = fromGlobalVar->mLinkageType;
constGV->mInitializer = fromGlobalVar->mInitializer;
constGV->mName = AllocStr(fromGlobalVar->mName);
constGV->mIsTLS = fromGlobalVar->mIsTLS;
copiedConst = (BfConstant*)constGV;
}
else if (fromConst->mConstType == BfConstType_GEP32_2)
{
auto fromConstGEP = (BfConstantGEP32_2*)fromConst;
@ -603,8 +620,21 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
auto constGEP = mTempAlloc.Alloc<BfConstantGEP32_2>();
constGEP->mConstType = BfConstType_GEP32_2;
constGEP->mTarget = copiedTarget.mId;
constGEP->mIdx0 = fromConstGEP->mIdx0;
constGEP->mIdx1 = fromConstGEP->mIdx1;
copiedConst = (BfConstant*)constGEP;
}
else if (fromConst->mConstType == BfConstType_ExtractValue)
{
auto fromConstGEP = (BfConstantExtractValue*)fromConst;
auto fromTarget = fromHolder->GetConstantById(fromConstGEP->mTarget);
auto copiedTarget = CreateConst(fromTarget, fromHolder);
auto constGEP = mTempAlloc.Alloc<BfConstantExtractValue>();
constGEP->mConstType = BfConstType_ExtractValue;
constGEP->mTarget = copiedTarget.mId;
constGEP->mIdx0 = fromConstGEP->mIdx0;
copiedConst = (BfConstant*)constGEP;
}
else if (fromConst->mConstType == BfConstType_TypeOf)
{
auto typeOf = (BfTypeOf_Const*)fromConst;
@ -647,7 +677,10 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
BfIRValue retVal;
retVal.mFlags = BfIRValueFlags_Const;
retVal.mId = mTempAlloc.GetChunkedId(copiedConst);
if (chunkId == -1)
chunkId = mTempAlloc.GetChunkedId(copiedConst);
retVal.mId = chunkId;
BF_ASSERT(retVal.mId >= 0);
#ifdef CHECK_CONSTHOLDER
retVal.mHolder = this;
#endif
@ -1243,6 +1276,12 @@ String BfIRBuilder::ToString(BfIRValue irValue)
BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
return ToString(targetConst) + StrFormat(" Gep32 %d,%d", gepConst->mIdx0, gepConst->mIdx1);
}
else if (constant->mConstType == BfConstType_ExtractValue)
{
auto gepConst = (BfConstantExtractValue*)constant;
BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
return ToString(targetConst) + StrFormat(" ExtractValue %d", gepConst->mIdx0);
}
else if (constant->mConstType == BfConstType_PtrToInt)
{
auto ptrToIntConst = (BfConstantPtrToInt*)constant;
@ -1838,6 +1877,14 @@ void BfIRBuilder::Write(const BfIRValue& irValue)
Write(gepConst->mIdx1);
}
break;
case (int)BfConstType_ExtractValue:
{
auto gepConst = (BfConstantExtractValue*)constant;
BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
Write(targetConst);
Write(gepConst->mIdx0);
}
break;
case (int)BfConstType_PtrToInt:
{
auto ptrToIntConst = (BfConstantPtrToInt*)constant;
@ -3964,7 +4011,7 @@ BfIRValue BfIRBuilder::CreateInBoundsGEP(BfIRValue val, int idx0)
BfIRValue BfIRBuilder::CreateInBoundsGEP(BfIRValue val, int idx0, int idx1)
{
if (val.IsConst())
{
{
auto constGEP = mTempAlloc.Alloc<BfConstantGEP32_2>();
constGEP->mConstType = BfConstType_GEP32_2;
constGEP->mTarget = val.mId;
@ -4024,6 +4071,20 @@ BfIRValue BfIRBuilder::CreateExtractValue(BfIRValue val, int idx)
auto arrayConstant = (BfConstantArray*)aggConstant;
return arrayConstant->mValues[idx];
}
auto constGEP = mTempAlloc.Alloc<BfConstantExtractValue>();
constGEP->mConstType = BfConstType_ExtractValue;
constGEP->mTarget = val.mId;
constGEP->mIdx0 = idx;
BfIRValue retVal;
retVal.mFlags = BfIRValueFlags_Const;
retVal.mId = mTempAlloc.GetChunkedId(constGEP);
#ifdef CHECK_CONSTHOLDER
retVal.mHolder = this;
#endif
return retVal;
}
BfIRValue retVal = WriteCmd(BfIRCmd_ExtractValue, val, idx);
@ -4213,7 +4274,7 @@ BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, B
auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
int chunkId = mTempAlloc.GetChunkedId(constGV);
constGV->mStreamId = -1;
constGV->mConstType = BfConstType_GlobalVar;
constGV->mConstType = BfConstType_GlobalVar;
constGV->mType = varType;
constGV->mIsConst = isConstant;
constGV->mLinkageType = linkageType;

View file

@ -134,6 +134,7 @@ enum BfConstType
BfConstType_BitCast,
BfConstType_BitCastNull,
BfConstType_GEP32_2,
BfConstType_ExtractValue,
BfConstType_PtrToInt,
BfConstType_TypeOf,
BfConstType_AggZero,
@ -825,6 +826,13 @@ struct BfConstantGEP32_2
int mIdx1;
};
struct BfConstantExtractValue
{
BfConstType mConstType;
int mTarget;
int mIdx0;
};
struct BfConstantArray
{
BfConstType mConstType;

View file

@ -709,7 +709,7 @@ void BfIRCodeGen::Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry)
CMD_PARAM(int, streamId);
if (streamId == -1)
{
int streamId = mCmdCount++;
int streamId = mCmdCount++;
CMD_PARAM(llvm::Type*, varType);
CMD_PARAM(bool, isConstant);
@ -718,13 +718,17 @@ void BfIRCodeGen::Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry)
CMD_PARAM(String, name);
CMD_PARAM(bool, isTLS);
auto globalVariable = new llvm::GlobalVariable(
*mLLVMModule,
varType,
isConstant,
LLVMMapLinkageType(linkageType),
initializer,
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
llvm::GlobalVariable* globalVariable = mLLVMModule->getGlobalVariable(name.c_str(), true);
if (globalVariable == NULL)
{
globalVariable = new llvm::GlobalVariable(
*mLLVMModule,
varType,
isConstant,
LLVMMapLinkageType(linkageType),
initializer,
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
}
llvmValue = globalVariable;
SetResult(streamId, globalVariable);
@ -791,6 +795,15 @@ void BfIRCodeGen::Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry)
llvmValue = llvm::ConstantExpr::getInBoundsGetElementPtr(NULL, target, gepArgs);
return;
}
else if (constType == BfConstType_ExtractValue)
{
CMD_PARAM(llvm::Constant*, target);
CMD_PARAM(int, idx0);
unsigned int gepArgs[] = {
(unsigned int)idx0 };
llvmValue = llvm::ConstantExpr::getExtractValue(target, gepArgs);
return;
}
else if (constType == BfConstType_PtrToInt)
{
CMD_PARAM(llvm::Constant*, target);

View file

@ -5388,6 +5388,10 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
baseTypeId = typeInstance->mBaseType->mTypeId;
}
BfTypeOptions* typeOptions = NULL;
if (typeInstance->mTypeOptionsIdx >= 0)
typeOptions = mSystem->GetTypeOptions(typeInstance->mTypeOptionsIdx);
SizedArray<BfIRValue, 16> customAttrs;
BfTypeInstance* attributeType = mContext->mUnreifiedModule->ResolveTypeDef(mCompiler->mAttributeTypeDef)->ToTypeInstance();
@ -5716,7 +5720,13 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
if ((!fieldDef->mIsStatic) && ((fieldReflectKind & ReflectKind_NonStaticFields) != 0))
includeField = true;
if ((fieldDef->mIsStatic) && ((fieldReflectKind & ReflectKind_StaticFields) != 0))
includeField = true;
includeField = true;
if ((!fieldDef->mIsStatic) && (typeOptions != NULL))
includeField = typeOptions->Apply(includeField, BfOptionFlags_ReflectNonStaticFields);
if ((fieldDef->mIsStatic) && (typeOptions != NULL))
includeField = typeOptions->Apply(includeField, BfOptionFlags_ReflectStaticFields);
includeField |= forceReflectFields;
if (!includeField)
@ -6031,7 +6041,20 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
includeMethod = true;
if ((methodDef->mIsStatic) && ((methodReflectKind & ReflectKind_StaticMethods) != 0))
includeMethod = true;
if (methodDef->mMethodType == BfMethodType_Ctor)
{
if (typeOptions != NULL)
includeMethod = typeOptions->Apply(includeMethod, BfOptionFlags_ReflectConstructors);
}
else
{
if ((!methodDef->mIsStatic) && (typeOptions != NULL))
includeMethod = typeOptions->Apply(includeMethod, BfOptionFlags_ReflectNonStaticMethods);
if ((methodDef->mIsStatic) && (typeOptions != NULL))
includeMethod = typeOptions->Apply(includeMethod, BfOptionFlags_ReflectStaticMethods);
}
if (!includeMethod)
continue;
@ -9820,6 +9843,11 @@ void BfModule::CurrentAddToConstHolder(BfIRValue& irVal)
return;
}
if (constant->mConstType == BfConstType_GlobalVar)
{
NOP;
}
auto origConst = irVal;
if ((constant->mConstType == BfConstType_BitCast) || (constant->mConstType == BfConstType_BitCastNull))
{

View file

@ -1604,6 +1604,7 @@ public:
void CheckAddFailType();
bool PopulateType(BfType* resolvedTypeRef, BfPopulateType populateType = BfPopulateType_Data);
BfTypeOptions* GetTypeOptions(BfTypeDef* typeDef);
bool CheckTypeOptionMethodFilters(BfMethodDef* methodDef, BfTypeOptions* typeOptions);
int GenerateTypeOptions(BfCustomAttributes* customAttributes, BfTypeInstance* typeInstance, bool checkTypeName);
void SetTypeOptions(BfTypeInstance* typeInstance);
BfModuleOptions GetModuleOptions();

View file

@ -1473,6 +1473,11 @@ BfTypeOptions* BfModule::GetTypeOptions(BfTypeDef* typeDef)
return mSystem->GetTypeOptions( matchedIdx);
}
bool BfModule::CheckTypeOptionMethodFilters(BfMethodDef* methodDef, BfTypeOptions * typeOptions)
{
return true;
}
int BfModule::GenerateTypeOptions(BfCustomAttributes* customAttributes, BfTypeInstance* typeInstance, bool checkTypeName)
{
if (mContext->mSystem->mTypeOptions.size() == 0)
@ -2512,9 +2517,7 @@ bool BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
}
if (typeInstance->mTypeOptionsIdx == -2)
{
SetTypeOptions(typeInstance);
}
SetTypeOptions(typeInstance);
ProcessCustomAttributeData();
bool isPacked = false;
@ -2530,6 +2533,16 @@ bool BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
typeInstance->mIsPacked = isPacked;
typeInstance->mIsCRepr = isCRepr;
if (typeInstance->mTypeOptionsIdx >= 0)
{
auto typeOptions = mSystem->GetTypeOptions(typeInstance->mTypeOptionsIdx);
if (typeOptions != NULL)
{
typeInstance->mIncludeAllMethods = typeOptions->Apply(typeInstance->mIncludeAllMethods, BfOptionFlags_ReflectAlwaysIncludeAll);
typeInstance->mHasBeenInstantiated = typeOptions->Apply(typeInstance->mHasBeenInstantiated, BfOptionFlags_ReflectAssumeInstantiated);
}
}
BfType* unionInnerType = NULL;
bool hadDeferredVars = false;
int dataPos;
@ -3681,6 +3694,10 @@ void BfModule::DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance)
auto typeDef = typeInstance->mTypeDef;
BfTypeOptions* typeOptions = NULL;
if (typeInstance->mTypeOptionsIdx >= 0)
typeOptions = mSystem->GetTypeOptions(typeInstance->mTypeOptionsIdx);
// Generate all methods. Pass 0
for (auto methodDef : typeDef->mMethods)
{
@ -3980,6 +3997,8 @@ void BfModule::DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance)
}
if (typeInstance->mIncludeAllMethods)
implRequired = true;
// if ((typeOptions != NULL) && (CheckTypeOptionMethodFilters(typeOptions, methodDef)))
// implRequired = true;
if (typeInstance->IsInterface())
declRequired = true;