mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 04:22:20 +02:00
Fixed duplicate global variables when used as default args
This commit is contained in:
parent
503590cea5
commit
fb0bace727
4 changed files with 73 additions and 60 deletions
|
@ -615,17 +615,7 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
|
||||||
else if (fromConst->mConstType == BfConstType_GlobalVar)
|
else if (fromConst->mConstType == BfConstType_GlobalVar)
|
||||||
{
|
{
|
||||||
auto fromGlobalVar = (BfGlobalVar*)fromConst;
|
auto fromGlobalVar = (BfGlobalVar*)fromConst;
|
||||||
auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
|
return CreateGlobalVariableConstant(fromGlobalVar->mType, fromGlobalVar->mIsConst, fromGlobalVar->mLinkageType, fromGlobalVar->mInitializer, fromGlobalVar->mName, fromGlobalVar->mIsTLS);
|
||||||
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)
|
else if (fromConst->mConstType == BfConstType_GEP32_2)
|
||||||
{
|
{
|
||||||
|
@ -1875,6 +1865,7 @@ void BfIRBuilder::ClearConstData()
|
||||||
mTempAlloc.Clear();
|
mTempAlloc.Clear();
|
||||||
mConstMemMap.Clear();
|
mConstMemMap.Clear();
|
||||||
mFunctionMap.Clear();
|
mFunctionMap.Clear();
|
||||||
|
mGlobalVarMap.Clear();
|
||||||
BF_ASSERT(mMethodTypeMap.GetCount() == 0);
|
BF_ASSERT(mMethodTypeMap.GetCount() == 0);
|
||||||
BF_ASSERT(mTypeMap.GetCount() == 0);
|
BF_ASSERT(mTypeMap.GetCount() == 0);
|
||||||
BF_ASSERT(mDITemporaryTypes.size() == 0);
|
BF_ASSERT(mDITemporaryTypes.size() == 0);
|
||||||
|
@ -1889,6 +1880,7 @@ void BfIRBuilder::ClearNonConstData()
|
||||||
{
|
{
|
||||||
mMethodTypeMap.Clear();
|
mMethodTypeMap.Clear();
|
||||||
mFunctionMap.Clear();
|
mFunctionMap.Clear();
|
||||||
|
mGlobalVarMap.Clear();
|
||||||
mTypeMap.Clear();
|
mTypeMap.Clear();
|
||||||
mConstMemMap.Clear();
|
mConstMemMap.Clear();
|
||||||
mDITemporaryTypes.Clear();
|
mDITemporaryTypes.Clear();
|
||||||
|
@ -2996,11 +2988,6 @@ void BfIRBuilder::CreateDbgTypeDefinition(BfType* type)
|
||||||
|
|
||||||
if (fieldInstance->mConstIdx != -1)
|
if (fieldInstance->mConstIdx != -1)
|
||||||
{
|
{
|
||||||
if (fieldInstance->GetFieldDef()->mName == "mMembers")
|
|
||||||
{
|
|
||||||
NOP;
|
|
||||||
}
|
|
||||||
|
|
||||||
constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
|
constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
|
||||||
staticValue = mModule->ConstantToCurrent(constant, typeInstance->mConstHolder, resolvedFieldType);
|
staticValue = mModule->ConstantToCurrent(constant, typeInstance->mConstHolder, resolvedFieldType);
|
||||||
}
|
}
|
||||||
|
@ -4743,8 +4730,32 @@ BfIRValue BfIRBuilder::CreateStackRestore(BfIRValue stackVal)
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)
|
void BfIRBuilder::CreateGlobalVariable(BfIRValue irValue)
|
||||||
{
|
{
|
||||||
|
auto globalVar = (BfGlobalVar*)GetConstant(irValue);
|
||||||
|
|
||||||
|
if (!mIgnoreWrites)
|
||||||
|
{
|
||||||
|
BF_ASSERT(globalVar->mStreamId == -1);
|
||||||
|
|
||||||
|
if (globalVar->mInitializer)
|
||||||
|
mHasGlobalDefs = true;
|
||||||
|
|
||||||
|
BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVariable, globalVar->mType, globalVar->mIsConst, (uint8)globalVar->mLinkageType, String(globalVar->mName), globalVar->mIsTLS, globalVar->mInitializer);
|
||||||
|
globalVar->mStreamId = retVal.mId;
|
||||||
|
|
||||||
|
NEW_CMD_INSERTED_IRVALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BfIRValue BfIRConstHolder::CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)
|
||||||
|
{
|
||||||
|
BfIRValue* valuePtr = NULL;
|
||||||
|
if ((!mGlobalVarMap.TryAdd(name, NULL, &valuePtr)) && (!initializer))
|
||||||
|
{
|
||||||
|
return *valuePtr;
|
||||||
|
}
|
||||||
|
|
||||||
BF_ASSERT(varType);
|
BF_ASSERT(varType);
|
||||||
|
|
||||||
auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
|
auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
|
||||||
|
@ -4758,23 +4769,15 @@ BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, B
|
||||||
constGV->mName = AllocStr(name);
|
constGV->mName = AllocStr(name);
|
||||||
constGV->mIsTLS = isTLS;
|
constGV->mIsTLS = isTLS;
|
||||||
|
|
||||||
if (!mIgnoreWrites)
|
auto irValue = BfIRValue(BfIRValueFlags_Const, chunkId);;
|
||||||
{
|
*valuePtr = irValue;
|
||||||
if (initializer)
|
return irValue;
|
||||||
mHasGlobalDefs = true;
|
}
|
||||||
|
|
||||||
BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVariable, varType, isConstant, (uint8)linkageType, name, isTLS, initializer);
|
BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)
|
||||||
constGV->mStreamId = retVal.mId;
|
{
|
||||||
retVal.mFlags = BfIRValueFlags_Const;
|
auto irValue = CreateGlobalVariableConstant(varType, isConstant, linkageType, initializer, name);
|
||||||
#ifdef CHECK_CONSTHOLDER
|
CreateGlobalVariable(irValue);
|
||||||
retVal.mHolder = this;
|
|
||||||
#endif
|
|
||||||
retVal.mId = chunkId;
|
|
||||||
NEW_CMD_INSERTED_IRVALUE;
|
|
||||||
return retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto irValue = BfIRValue(BfIRValueFlags_Const, chunkId);
|
|
||||||
return irValue;
|
return irValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -901,6 +901,7 @@ class BfIRConstHolder
|
||||||
public:
|
public:
|
||||||
BumpAllocatorT<256> mTempAlloc;
|
BumpAllocatorT<256> mTempAlloc;
|
||||||
BfModule* mModule;
|
BfModule* mModule;
|
||||||
|
Dictionary<String, BfIRValue> mGlobalVarMap;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void FixTypeCode(BfTypeCode& typeCode);
|
void FixTypeCode(BfTypeCode& typeCode);
|
||||||
|
@ -936,6 +937,7 @@ public:
|
||||||
BfIRValue CreateTypeOf(BfType* type);
|
BfIRValue CreateTypeOf(BfType* type);
|
||||||
BfIRValue CreateTypeOf(BfType* type, BfIRValue typeData);
|
BfIRValue CreateTypeOf(BfType* type, BfIRValue typeData);
|
||||||
BfIRValue GetUndefConstValue(BfIRType type);
|
BfIRValue GetUndefConstValue(BfIRType type);
|
||||||
|
BfIRValue CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
|
||||||
|
|
||||||
bool WriteConstant(BfIRValue val, void* ptr, BfType* type);
|
bool WriteConstant(BfIRValue val, void* ptr, BfType* type);
|
||||||
BfIRValue ReadConstant(void* ptr, BfType* type);
|
BfIRValue ReadConstant(void* ptr, BfType* type);
|
||||||
|
@ -1227,6 +1229,7 @@ public:
|
||||||
BfIRValue CreateStackRestore(BfIRValue stackVal);
|
BfIRValue CreateStackRestore(BfIRValue stackVal);
|
||||||
|
|
||||||
BfIRValue CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
|
BfIRValue CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
|
||||||
|
void CreateGlobalVariable(BfIRValue irValue);
|
||||||
void GlobalVar_SetUnnamedAddr(BfIRValue val, bool unnamedAddr);
|
void GlobalVar_SetUnnamedAddr(BfIRValue val, bool unnamedAddr);
|
||||||
void GlobalVar_SetInitializer(BfIRValue globalVar, BfIRValue initVal);
|
void GlobalVar_SetInitializer(BfIRValue globalVar, BfIRValue initVal);
|
||||||
void GlobalVar_SetAlignment(BfIRValue globalVar, int alignment);
|
void GlobalVar_SetAlignment(BfIRValue globalVar, int alignment);
|
||||||
|
|
|
@ -809,6 +809,9 @@ void BfIRCodeGen::Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry,
|
||||||
|
|
||||||
llvm::GlobalVariable* globalVariable = mLLVMModule->getGlobalVariable(name.c_str(), true);
|
llvm::GlobalVariable* globalVariable = mLLVMModule->getGlobalVariable(name.c_str(), true);
|
||||||
if (globalVariable == NULL)
|
if (globalVariable == NULL)
|
||||||
|
{
|
||||||
|
globalVariable = mLLVMModule->getGlobalVariable(name.c_str());
|
||||||
|
if (globalVariable == NULL)
|
||||||
{
|
{
|
||||||
globalVariable = new llvm::GlobalVariable(
|
globalVariable = new llvm::GlobalVariable(
|
||||||
*mLLVMModule,
|
*mLLVMModule,
|
||||||
|
@ -818,6 +821,7 @@ void BfIRCodeGen::Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry,
|
||||||
initializer,
|
initializer,
|
||||||
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
|
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
llvmValue = globalVariable;
|
llvmValue = globalVariable;
|
||||||
|
|
||||||
SetResult(streamId, globalVariable);
|
SetResult(streamId, globalVariable);
|
||||||
|
@ -2390,13 +2394,17 @@ void BfIRCodeGen::HandleNextCmd()
|
||||||
CMD_PARAM(bool, isTLS);
|
CMD_PARAM(bool, isTLS);
|
||||||
CMD_PARAM(llvm::Constant*, initializer);
|
CMD_PARAM(llvm::Constant*, initializer);
|
||||||
|
|
||||||
auto globalVariable = new llvm::GlobalVariable(
|
auto globalVariable = mLLVMModule->getGlobalVariable(name.c_str());
|
||||||
|
if (globalVariable == NULL)
|
||||||
|
{
|
||||||
|
globalVariable = new llvm::GlobalVariable(
|
||||||
*mLLVMModule,
|
*mLLVMModule,
|
||||||
varType,
|
varType,
|
||||||
isConstant,
|
isConstant,
|
||||||
LLVMMapLinkageType(linkageType),
|
LLVMMapLinkageType(linkageType),
|
||||||
initializer,
|
initializer,
|
||||||
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
|
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
|
||||||
|
}
|
||||||
SetResult(curId, globalVariable);
|
SetResult(curId, globalVariable);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -13722,6 +13722,10 @@ BfTypedValue BfModule::ReferenceStaticField(BfFieldInstance* fieldInstance)
|
||||||
{
|
{
|
||||||
globalValue = *globalValuePtr;
|
globalValue = *globalValuePtr;
|
||||||
BF_ASSERT(globalValue);
|
BF_ASSERT(globalValue);
|
||||||
|
|
||||||
|
auto globalVar = (BfGlobalVar*)mBfIRBuilder->GetConstant(globalValue);
|
||||||
|
if ((globalVar->mStreamId == -1) && (!mBfIRBuilder->mIgnoreWrites))
|
||||||
|
mBfIRBuilder->CreateGlobalVariable(globalValue);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -13754,15 +13758,10 @@ BfTypedValue BfModule::ReferenceStaticField(BfFieldInstance* fieldInstance)
|
||||||
staticVarName,
|
staticVarName,
|
||||||
IsThreadLocal(fieldInstance));
|
IsThreadLocal(fieldInstance));
|
||||||
|
|
||||||
if (!mBfIRBuilder->mIgnoreWrites)
|
|
||||||
{
|
|
||||||
// Only store this if we actually did the creation
|
|
||||||
BF_ASSERT(globalValue);
|
BF_ASSERT(globalValue);
|
||||||
mStaticFieldRefs[fieldInstance] = globalValue;
|
mStaticFieldRefs[fieldInstance] = globalValue;
|
||||||
}
|
|
||||||
|
|
||||||
BfLogSysM("Mod:%p Type:%p ReferenceStaticField %p -> %p\n", this, fieldInstance->mOwner, fieldInstance, globalValue);
|
BfLogSysM("Mod:%p Type:%p ReferenceStaticField %p -> %p\n", this, fieldInstance->mOwner, fieldInstance, globalValue);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue