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

Supporting proper lowering for CRepr int-sized structs

This commit is contained in:
Brian Fiete 2020-02-20 05:21:23 -08:00
parent 610d472b66
commit 07fd22f9e0
3 changed files with 43 additions and 15 deletions

View file

@ -4627,7 +4627,20 @@ BfTypedValue BfExprEvaluator::CreateCall(BfMethodInstance* methodInstance, BfIRV
if (sret != NULL) if (sret != NULL)
result = *sret; result = *sret;
else if (hasResult) else if (hasResult)
{
if (methodInstance->mReturnType->GetLoweredType() != BfTypeCode_None)
{
auto loweredType = mModule->GetPrimitiveType(methodInstance->mReturnType->GetLoweredType());
auto loweredPtrType = mModule->CreatePointerType(loweredType);
auto retVal = mModule->CreateAlloca(methodInstance->mReturnType);
auto castedRetVal = mModule->mBfIRBuilder->CreateBitCast(retVal, mModule->mBfIRBuilder->MapType(loweredPtrType));
mModule->mBfIRBuilder->CreateStore(callInst, castedRetVal);
result = BfTypedValue(retVal, methodInstance->mReturnType, BfTypedValueKind_TempAddr);
}
else
result = BfTypedValue(callInst, methodInstance->mReturnType); result = BfTypedValue(callInst, methodInstance->mReturnType);
}
else else
result = mModule->GetFakeTypedValue(methodInstance->mReturnType); result = mModule->GetFakeTypedValue(methodInstance->mReturnType);
if (result.mType->IsRef()) if (result.mType->IsRef())

View file

@ -12888,7 +12888,7 @@ void BfModule::MarkScopeLeft(BfScopeData* scopeData)
void BfModule::CreateReturn(BfIRValue val) void BfModule::CreateReturn(BfIRValue val)
{ {
if (mCurMethodInstance->mReturnType->IsComposite()) if (mCurMethodInstance->HasStructRet())
{ {
// Store to sret // Store to sret
BF_ASSERT(val); BF_ASSERT(val);
@ -12901,6 +12901,17 @@ void BfModule::CreateReturn(BfIRValue val)
{ {
mBfIRBuilder->CreateRetVoid(); mBfIRBuilder->CreateRetVoid();
} }
else if (mCurMethodInstance->mReturnType->IsStruct())
{
auto loweredType = GetPrimitiveType(mCurMethodInstance->mReturnType->GetLoweredType());
auto ptrReturnType = CreatePointerType(mCurMethodInstance->mReturnType);
auto ptrLoweredValue = CreateAlloca(loweredType);
auto ptrReturnValue = mBfIRBuilder->CreateBitCast(ptrLoweredValue, mBfIRBuilder->MapType(ptrReturnType));
mBfIRBuilder->CreateStore(val, ptrReturnValue);
auto loadedLoweredValue = mBfIRBuilder->CreateLoad(ptrLoweredValue);
mBfIRBuilder->CreateRet(loadedLoweredValue);
}
else else
{ {
BF_ASSERT(val); BF_ASSERT(val);
@ -13121,11 +13132,6 @@ void BfModule::CreateDelegateInvokeMethod()
callingConv = BfIRCallingConv_CDecl; callingConv = BfIRCallingConv_CDecl;
if (callingConv != BfIRCallingConv_CDecl) if (callingConv != BfIRCallingConv_CDecl)
mBfIRBuilder->SetCallCallingConv(staticResult, callingConv); mBfIRBuilder->SetCallCallingConv(staticResult, callingConv);
// if (!mSystem->IsCompatibleCallingConvention(BfCallingConvention_Unspecified, mCurMethodInstance->mMethodDef->mCallingConvention))
// {
// if (mCurMethodInstance->mMethodDef->mCallingConvention == BfCallingConvention_Stdcall)
//
// }
mCurMethodState->SetHadReturn(false); mCurMethodState->SetHadReturn(false);
mCurMethodState->mLeftBlockUncond = false; mCurMethodState->mLeftBlockUncond = false;
mCurMethodState->mLeftBlockCond = false; mCurMethodState->mLeftBlockCond = false;
@ -13140,7 +13146,10 @@ void BfModule::CreateDelegateInvokeMethod()
} }
else else
{ {
auto phi = mBfIRBuilder->CreatePhi(mBfIRBuilder->MapType(mCurMethodInstance->mReturnType), 2); auto loweredReturnType = mCurMethodInstance->mReturnType;
if (loweredReturnType->GetLoweredType() != BfTypeCode_None)
loweredReturnType = GetPrimitiveType(loweredReturnType->GetLoweredType());
auto phi = mBfIRBuilder->CreatePhi(mBfIRBuilder->MapType(loweredReturnType), 2);
mBfIRBuilder->AddPhiIncoming(phi, nonStaticResult, trueBB); mBfIRBuilder->AddPhiIncoming(phi, nonStaticResult, trueBB);
mBfIRBuilder->AddPhiIncoming(phi, staticResult, falseBB); mBfIRBuilder->AddPhiIncoming(phi, staticResult, falseBB);
mBfIRBuilder->CreateRet(phi); mBfIRBuilder->CreateRet(phi);

View file

@ -607,7 +607,7 @@ bool BfMethodInstance::HasParamsArray()
bool BfMethodInstance::HasStructRet() bool BfMethodInstance::HasStructRet()
{ {
return (mReturnType->IsComposite()) && (!mReturnType->IsValuelessType()); return (mReturnType->IsComposite()) && (!mReturnType->IsValuelessType()) && (mReturnType->GetLoweredType() == BfTypeCode_None);
} }
bool BfMethodInstance::HasSelf() bool BfMethodInstance::HasSelf()
@ -864,29 +864,35 @@ int BfMethodInstance::DbgGetVirtualMethodNum()
void BfMethodInstance::GetIRFunctionInfo(BfModule* module, BfIRType& returnType, SizedArrayImpl<BfIRType>& paramTypes, bool forceStatic) void BfMethodInstance::GetIRFunctionInfo(BfModule* module, BfIRType& returnType, SizedArrayImpl<BfIRType>& paramTypes, bool forceStatic)
{ {
module->PopulateType(mReturnType); module->PopulateType(mReturnType);
if (mReturnType->IsValuelessType())
BfType* loweredReturnType = mReturnType;
auto loweredReturnTypeCode = mReturnType->GetLoweredType();
if (loweredReturnTypeCode != BfTypeCode_None)
loweredReturnType = module->GetPrimitiveType(loweredReturnTypeCode);
if (loweredReturnType->IsValuelessType())
{ {
auto voidType = module->GetPrimitiveType(BfTypeCode_None); auto voidType = module->GetPrimitiveType(BfTypeCode_None);
returnType = module->mBfIRBuilder->MapType(voidType); returnType = module->mBfIRBuilder->MapType(voidType);
} }
else if (mReturnType->IsComposite()) else if (loweredReturnType->IsComposite())
{ {
auto voidType = module->GetPrimitiveType(BfTypeCode_None); auto voidType = module->GetPrimitiveType(BfTypeCode_None);
returnType = module->mBfIRBuilder->MapType(voidType); returnType = module->mBfIRBuilder->MapType(voidType);
auto typeInst = mReturnType->ToTypeInstance(); auto typeInst = loweredReturnType->ToTypeInstance();
if (typeInst != NULL) if (typeInst != NULL)
{ {
paramTypes.push_back(module->mBfIRBuilder->MapTypeInstPtr(typeInst)); paramTypes.push_back(module->mBfIRBuilder->MapTypeInstPtr(typeInst));
} }
else else
{ {
auto ptrType = module->CreatePointerType(mReturnType); auto ptrType = module->CreatePointerType(loweredReturnType);
paramTypes.push_back(module->mBfIRBuilder->MapType(ptrType)); paramTypes.push_back(module->mBfIRBuilder->MapType(ptrType));
} }
} }
else else
{ {
returnType = module->mBfIRBuilder->MapType(mReturnType); returnType = module->mBfIRBuilder->MapType(loweredReturnType);
} }
for (int paramIdx = -1; paramIdx < GetParamCount(); paramIdx++) for (int paramIdx = -1; paramIdx < GetParamCount(); paramIdx++)