1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +02:00

ConstEval updates, better const struct support

This commit is contained in:
Brian Fiete 2020-12-22 04:50:37 -08:00
parent 9b80c26d0a
commit be929c3626
21 changed files with 1404 additions and 527 deletions

View file

@ -790,29 +790,47 @@ void BeIRCodeGen::Read(BeValue*& beValue)
BE_MEM_END("ParamType_Const_AggZero");
return;
}
else if (constType == BfConstType_Array)
else if (constType == BfConstType_Agg)
{
CMD_PARAM(BeType*, type);
CMD_PARAM(CmdParamVec<BeConstant*>, values);
auto arrayType = (BeSizedArrayType*)type;
int fillCount = (int)(arrayType->mLength - values.size());
if (fillCount > 0)
if (type->IsSizedArray())
{
auto lastValue = values.back();
for (int i = 0; i < fillCount; i++)
values.push_back(lastValue);
auto arrayType = (BeSizedArrayType*)type;
int fillCount = (int)(arrayType->mLength - values.size());
if (fillCount > 0)
{
auto lastValue = values.back();
for (int i = 0; i < fillCount; i++)
values.push_back(lastValue);
}
}
else
{
BF_ASSERT(type->IsStruct());
}
auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
constStruct->mType = type;
for (auto val : values)
constStruct->mType = type;
for (int i = 0; i < (int)values.size(); i++)
{
auto val = values[i];
BeConstant* constant = BeValueDynCast<BeConstant>(val);
constStruct->mMemberValues.push_back(constant);
#ifdef _DEBUG
auto memberType = constant->GetType();
BF_ASSERT(memberType == arrayType->mElementType);
if (type->IsSizedArray())
{
auto arrayType = (BeSizedArrayType*)type;
auto memberType = constant->GetType();
BF_ASSERT(memberType == arrayType->mElementType);
}
else
{
auto structType = (BeStructType*)type;
auto memberType = constant->GetType();
BF_ASSERT(memberType == structType->mMembers[i].mType);
}
#endif
}
beValue = constStruct;
@ -1140,22 +1158,46 @@ void BeIRCodeGen::HandleNextCmd()
SetResult(curId, mBeContext->CreateVectorType(elementType, length));
}
break;
case BfIRCmd_CreateConstStruct:
case BfIRCmd_CreateConstAgg:
{
CMD_PARAM(BeType*, type);
CMD_PARAM(CmdParamVec<BeValue*>, values)
auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
CMD_PARAM(CmdParamVec<BeValue*>, values);
auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
constStruct->mType = type;
BF_ASSERT(type->IsStruct());
if (type->IsStruct())
{
FixValues((BeStructType*)type, values);
FixValues((BeStructType*)type, values);
BF_ASSERT(((BeStructType*)type)->mMembers.size() == values.size());
for (int i = 0; i < (int)values.size(); i++)
BF_ASSERT(((BeStructType*)type)->mMembers.size() == values.size());
for (int i = 0; i < (int)values.size(); i++)
{
auto val = values[i];
BF_ASSERT(mBeContext->AreTypesEqual(((BeStructType*)type)->mMembers[i].mType, val->GetType()));
constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
}
}
else
{
auto val = values[i];
BF_ASSERT(mBeContext->AreTypesEqual(((BeStructType*)type)->mMembers[i].mType, val->GetType()));
constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
BF_ASSERT(type->IsSizedArray());
auto arrayType = (BeSizedArrayType*)type;
int fillCount = (int)(arrayType->mLength - values.size());
if (fillCount > 0)
{
auto lastValue = values.back();
for (int i = 0; i < fillCount; i++)
values.push_back(lastValue);
}
BF_ASSERT(arrayType->mLength == values.size());
for (int i = 0; i < (int)values.size(); i++)
{
auto val = values[i];
BF_ASSERT(mBeContext->AreTypesEqual(((BeSizedArrayType*)type)->mElementType, val->GetType()));
constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
}
}
SetResult(curId, constStruct);
}
@ -1167,19 +1209,7 @@ void BeIRCodeGen::HandleNextCmd()
beConst->mType = type;
SetResult(curId, beConst);
}
break;
case BfIRCmd_CreateConstArray:
{
CMD_PARAM(BeType*, type);
CMD_PARAM(CmdParamVec<BeConstant*>, values);
auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
constStruct->mType = type;
for (auto val : values)
constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
SetResult(curId, constStruct);
}
break;
break;
case BfIRCmd_CreateConstString:
{
CMD_PARAM(String, str);

View file

@ -2864,6 +2864,39 @@ BeMCOperand BeMCContext::CreateLoad(const BeMCOperand& mcTarget)
return result;
}
static bool NeedsDecompose(BeConstant* constant)
{
if (auto arrayConst = BeValueDynCast<BeStructConstant>(constant))
{
for (auto& val : arrayConst->mMemberValues)
{
if (NeedsDecompose(val))
return true;
}
return false;
}
if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constant))
{
return true;
}
else if (auto castConst = BeValueDynCast<BeCastConstant>(constant))
{
return true;
}
else if (auto castConst = BeValueDynCast<BeBitCastInst>(constant))
{
if (auto targetConstant = BeValueDynCast<BeConstant>(castConst->mValue))
return NeedsDecompose(targetConstant);
}
else if (auto castConst = BeValueDynCast<BeGEPConstant>(constant))
{
return NeedsDecompose(castConst->mTarget);
}
return false;
}
void BeMCContext::CreateStore(BeMCInstKind instKind, const BeMCOperand& val, const BeMCOperand& ptr)
{
BeMCOperand mcVal = val;
@ -2871,33 +2904,30 @@ void BeMCContext::CreateStore(BeMCInstKind instKind, const BeMCOperand& val, con
if (mcVal.mKind == BeMCOperandKind_ConstAgg)
{
bool needsDecompose = false;
if (auto arrayConst = BeValueDynCast<BeStructConstant>(mcVal.mConstant))
if (auto aggConst = BeValueDynCast<BeStructConstant>(mcVal.mConstant))
{
for (auto& val : arrayConst->mMemberValues)
{
if (auto globalVar = BeValueDynCast<BeGlobalVariable>(val))
{
needsDecompose = true;
}
else if (auto castConst = BeValueDynCast<BeCastConstant>(val))
{
needsDecompose = true;
}
}
if (needsDecompose)
if (NeedsDecompose(mcVal.mConstant))
{
int offset = 0;
auto arrayType = arrayConst->GetType();
BEMC_ASSERT(arrayType->IsSizedArray());
auto sizedArrayType = (BeSizedArrayType*)arrayType;
for (auto& val : arrayConst->mMemberValues)
{
auto destOperand = AllocVirtualReg(mModule->mContext->GetPointerTo(sizedArrayType->mElementType));
auto aggType = aggConst->GetType();
for (int memberIdx = 0; memberIdx < (int)aggConst->mMemberValues.size(); memberIdx++)
{
auto val = aggConst->mMemberValues[memberIdx];
BeType* elemType = NULL;
if (aggType->IsSizedArray())
elemType = ((BeSizedArrayType*)aggType)->mElementType;
else
{
auto& memberInfo = ((BeStructType*)aggType)->mMembers[memberIdx];
offset = memberInfo.mByteOffset;
elemType = memberInfo.mType;
}
if (elemType->mSize == 0)
continue;
auto destOperand = AllocVirtualReg(mModule->mContext->GetPointerTo(elemType));
auto vregInfo = GetVRegInfo(destOperand);
vregInfo->mDefOnFirstUse = true;
vregInfo->mRelTo = mcPtr;
@ -2905,12 +2935,13 @@ void BeMCContext::CreateStore(BeMCInstKind instKind, const BeMCOperand& val, con
vregInfo->mRelOffset = BeMCOperand::FromImmediate(offset);
destOperand.mKind = BeMCOperandKind_VRegLoad;
//destOperand.mKind = BeMCOperandKind_VRegLoad;
auto elementVal = GetOperand(val);
AllocInst(instKind, destOperand, elementVal);
//AllocInst(instKind, destOperand, elementVal);
CreateStore(instKind, elementVal, destOperand);
offset += sizedArrayType->mElementType->mSize;
offset += elemType->mSize;
}
return;
}
@ -15742,7 +15773,7 @@ void BeMCContext::Generate(BeFunction* function)
mDbgPreferredRegs[32] = X64Reg_R8;*/
//mDbgPreferredRegs[8] = X64Reg_RAX;
//mDebugging = (function->mName == "DoCallback");
mDebugging = (function->mName == "?SetDefaults@KeySettings@BeefTest@bf@@QEAAXXZ");
// || (function->mName == "?MethodA@TestProgram@BeefTest@bf@@CAXXZ");
// || (function->mName == "?Hey@Blurg@bf@@SAXXZ")
// ;

View file

@ -3111,9 +3111,7 @@ BeBlock* BeModule::CreateBlock(const StringImpl& name)
void BeModule::AddBlock(BeFunction* function, BeBlock* block)
{
block->mFunction = function;
function->mBlocks.push_back(block);
//block->mFuncRelId = function->mCurElementId++;
function->mBlocks.push_back(block);
}
void BeModule::RemoveBlock(BeFunction* function, BeBlock* block)

View file

@ -530,8 +530,7 @@ public:
Array<BeBlock*> mBlocks;
Array<BeFunctionParam> mParams;
BeDbgFunction* mDbgFunction;
BeGlobalVariable* mRemapBindVar;
int mCurElementId;
BeGlobalVariable* mRemapBindVar;
public:
BeFunction()
@ -548,8 +547,7 @@ public:
mNoFramePointerElim = false;
mIsDLLExport = false;
mIsDLLImport = false;
mRemapBindVar = NULL;
mCurElementId = 0;
mRemapBindVar = NULL;
}
BeFunctionType* GetFuncType()