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

Fixed sized array size inference, primitive type handling in ir

This commit is contained in:
Brian Fiete 2020-12-24 06:58:38 -08:00
parent 8894430f98
commit a20519ee04
8 changed files with 92 additions and 21 deletions

View file

@ -633,6 +633,14 @@ void BeIRCodeGen::Read(BeType*& beType)
}
int typeId = (int)ReadSLEB128();
if (typeKind == BfIRType::TypeKind::TypeKind_TypeCode)
{
bool isSigned = false;
beType = GetBeType((BfTypeCode)typeId, isSigned);
return;
}
auto& typeEntry = GetTypeEntry(typeId);
if (typeKind == BfIRType::TypeKind::TypeKind_TypeId)
beType = typeEntry.mBeType;

View file

@ -2462,6 +2462,13 @@ public:
BfTokenNode* mOpenBracket;
BfSizedArray<ASTREF(BfAstNode*)> mParams; // Either commas or constant size expression
BfTokenNode* mCloseBracket;
bool IsInferredSize()
{
if (mParams.mSize > 0)
return BfNodeIsA<BfUninitializedExpression>(mParams[0]);
return false;
}
}; BF_AST_DECL(BfArrayTypeRef, BfElementedTypeRef);
class BfNullableTypeRef : public BfElementedTypeRef

View file

@ -66,6 +66,11 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
if (memberRefExpr->mTarget == NULL)
arraySize = (int)invocationExpr->mArguments.size();
}
else if (auto indexerExpr = BfNodeDynCast<BfIndexerExpression>(invocationExpr->mTarget))
{
// Sized array initializer
arraySize = (int)invocationExpr->mArguments.size();
}
}
}
@ -75,7 +80,7 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
return mResult;
}
else
{
{
mResult = BfTypedValue(mModule->mBfIRBuilder->GetUndefConstValue(mModule->mBfIRBuilder->GetPrimitiveType(BfTypeCode_IntPtr)), mModule->GetPrimitiveType(BfTypeCode_IntPtr));
return mResult;
}

View file

@ -1368,6 +1368,11 @@ String BfIRBuilder::ToString(BfIRType irType)
{
llvmType = mBfIRCodeGen->GetLLVMType(irType.mId);
}
else if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeCode)
{
bool isSigned = false;
llvmType = mBfIRCodeGen->GetLLVMType((BfTypeCode)irType.mId, isSigned);
}
else
{
auto& typeEntry = mBfIRCodeGen->GetTypeEntry(irType.mId);
@ -1400,9 +1405,14 @@ String BfIRBuilder::ToString(BfIRType irType)
{
beType = mBeIRCodeGen->GetBeType(irType.mId);
}
else if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeCode)
{
bool isSigned = false;
beType = mBeIRCodeGen->GetBeType((BfTypeCode)irType.mId, isSigned);
}
else
{
auto& typeEntry = mBeIRCodeGen->GetTypeEntry(irType.mId);
auto& typeEntry = mBeIRCodeGen->GetTypeEntry(irType.mId);
if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeId)
beType = typeEntry.mBeType;
else if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeInstId)
@ -3469,9 +3479,11 @@ BfIRFunction BfIRBuilder::GetFakeFunction()
BfIRType BfIRBuilder::GetPrimitiveType(BfTypeCode typeCode)
{
FixTypeCode(typeCode);
BfIRType retType = WriteCmd(BfIRCmd_PrimitiveType, typeCode);
NEW_CMD_INSERTED_IRTYPE;
return retType;
BfIRType irType;
irType.mKind = BfIRTypeData::TypeKind_TypeCode;
irType.mId = (int)typeCode;
return irType;
}
BfIRType BfIRBuilder::CreateStructType(const StringImpl& name)

View file

@ -580,6 +580,7 @@ struct BfIRTypeData
{
TypeKind_None,
TypeKind_TypeId,
TypeKind_TypeCode,
TypeKind_TypeInstId,
TypeKind_TypeInstPtrId,
TypeKind_Stream,
@ -1102,7 +1103,7 @@ public:
void Module_SetTargetTriple(const StringImpl& targetTriple);
void Module_AddModuleFlag(const StringImpl& flag, int val);
BfIRType GetPrimitiveType(BfTypeCode typeCode);
BfIRType GetPrimitiveType(BfTypeCode typeCode);
BfIRType CreateStructType(const StringImpl& name);
BfIRType CreateStructType(const BfSizedArray<BfIRType>& memberTypes);
void StructSetBody(BfIRType type, const BfSizedArray<BfIRType>& memberTypes, bool isPacked);

View file

@ -744,6 +744,14 @@ void BfIRCodeGen::Read(llvm::Type*& llvmType)
}
int typeId = (int)ReadSLEB128();
if (typeKind == BfIRType::TypeKind::TypeKind_TypeCode)
{
bool isSigned = false;
llvmType = GetLLVMType((BfTypeCode)typeId, isSigned);
return;
}
auto& typeEntry = GetTypeEntry(typeId);
if (typeKind == BfIRType::TypeKind::TypeKind_TypeId)
llvmType = typeEntry.mLLVMType;

View file

@ -1134,7 +1134,7 @@ void BfModule::EnsureIRBuilder(bool dbgVerifyCodeGen)
// code as we walk the AST
//mBfIRBuilder->mDbgVerifyCodeGen = true;
if (
(mModuleName == "System_StringView")
(mModuleName == "-")
//|| (mModuleName == "BeefTest2_ClearColorValue")
//|| (mModuleName == "Tests_FuncRefs")
)
@ -4078,12 +4078,19 @@ BfTypedValue BfModule::GetFieldInitializerValue(BfFieldInstance* fieldInstance,
if (fieldInstance != NULL)
MarkFieldInitialized(fieldInstance);
if ((doStore) && (result))
{
result = LoadValue(result);
if (!result.mType->IsValuelessType())
mBfIRBuilder->CreateStore(result.mValue, staticVarRef.mValue);
if (fieldInstance->mResolvedType->IsUnknownSizedArray())
{
AssertErrorState();
}
else
{
result = LoadValue(result);
if (!result.mType->IsValuelessType())
mBfIRBuilder->CreateStore(result.mValue, staticVarRef.mValue);
}
}
return result;
@ -13613,14 +13620,14 @@ void BfModule::DoLocalVariableDebugInfo(BfLocalVariable* localVarDef, bool doAli
mBfIRBuilder->PopulateType(localVarDef->mResolvedType);
auto constMem = mBfIRBuilder->ConstToMemory(localVarDef->mConstValue);
if (IsTargetingBeefBackend())
{
diValue = mBfIRBuilder->CreateAliasValue(constMem);
didConstToMem = true;
diType = mBfIRBuilder->DbgCreateReferenceType(diType);
}
else
// if (IsTargetingBeefBackend())
// {
// diValue = mBfIRBuilder->CreateAliasValue(constMem);
// didConstToMem = true;
//
// diType = mBfIRBuilder->DbgCreateReferenceType(diType);
// }
//else
{
isByAddr = true;
mBfIRBuilder->SaveDebugLocation();

View file

@ -2901,7 +2901,7 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
continue;
SetAndRestoreValue<BfFieldDef*> prevTypeRef(mContext->mCurTypeState->mCurFieldDef, field);
BfType* resolvedFieldType = NULL;
if (field->IsEnumCaseEntry())
@ -2938,7 +2938,7 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
// For 'let', make read-only
}
else
{
{
resolvedFieldType = ResolveTypeRef(field->mTypeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_NoResolveGenericParam);
if (resolvedFieldType == NULL)
{
@ -2948,6 +2948,29 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
}
}
if (resolvedFieldType->IsUndefSizedArray())
{
if (auto arrayTypeRef = BfNodeDynCast<BfArrayTypeRef>(field->mTypeRef))
{
if (arrayTypeRef->IsInferredSize())
{
if (field->mInitializer != NULL)
{
DeferredResolveEntry resolveEntry;
resolveEntry.mFieldDef = field;
resolveEntry.mTypeArrayIdx = (int)llvmFieldTypes.size();
deferredVarResolves.push_back(resolveEntry);
fieldInstance->mIsInferredType = true;
}
else
{
AssertErrorState();
}
}
}
}
if (resolvedFieldType->IsMethodRef())
{
auto methodRefType = (BfMethodRefType*)resolvedFieldType;