1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22: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(); int typeId = (int)ReadSLEB128();
if (typeKind == BfIRType::TypeKind::TypeKind_TypeCode)
{
bool isSigned = false;
beType = GetBeType((BfTypeCode)typeId, isSigned);
return;
}
auto& typeEntry = GetTypeEntry(typeId); auto& typeEntry = GetTypeEntry(typeId);
if (typeKind == BfIRType::TypeKind::TypeKind_TypeId) if (typeKind == BfIRType::TypeKind::TypeKind_TypeId)
beType = typeEntry.mBeType; beType = typeEntry.mBeType;

View file

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

View file

@ -66,6 +66,11 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
if (memberRefExpr->mTarget == NULL) if (memberRefExpr->mTarget == NULL)
arraySize = (int)invocationExpr->mArguments.size(); 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; return mResult;
} }
else else
{ {
mResult = BfTypedValue(mModule->mBfIRBuilder->GetUndefConstValue(mModule->mBfIRBuilder->GetPrimitiveType(BfTypeCode_IntPtr)), mModule->GetPrimitiveType(BfTypeCode_IntPtr)); mResult = BfTypedValue(mModule->mBfIRBuilder->GetUndefConstValue(mModule->mBfIRBuilder->GetPrimitiveType(BfTypeCode_IntPtr)), mModule->GetPrimitiveType(BfTypeCode_IntPtr));
return mResult; return mResult;
} }

View file

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

View file

@ -580,6 +580,7 @@ struct BfIRTypeData
{ {
TypeKind_None, TypeKind_None,
TypeKind_TypeId, TypeKind_TypeId,
TypeKind_TypeCode,
TypeKind_TypeInstId, TypeKind_TypeInstId,
TypeKind_TypeInstPtrId, TypeKind_TypeInstPtrId,
TypeKind_Stream, TypeKind_Stream,
@ -1102,7 +1103,7 @@ public:
void Module_SetTargetTriple(const StringImpl& targetTriple); void Module_SetTargetTriple(const StringImpl& targetTriple);
void Module_AddModuleFlag(const StringImpl& flag, int val); void Module_AddModuleFlag(const StringImpl& flag, int val);
BfIRType GetPrimitiveType(BfTypeCode typeCode); BfIRType GetPrimitiveType(BfTypeCode typeCode);
BfIRType CreateStructType(const StringImpl& name); BfIRType CreateStructType(const StringImpl& name);
BfIRType CreateStructType(const BfSizedArray<BfIRType>& memberTypes); BfIRType CreateStructType(const BfSizedArray<BfIRType>& memberTypes);
void StructSetBody(BfIRType type, const BfSizedArray<BfIRType>& memberTypes, bool isPacked); 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(); int typeId = (int)ReadSLEB128();
if (typeKind == BfIRType::TypeKind::TypeKind_TypeCode)
{
bool isSigned = false;
llvmType = GetLLVMType((BfTypeCode)typeId, isSigned);
return;
}
auto& typeEntry = GetTypeEntry(typeId); auto& typeEntry = GetTypeEntry(typeId);
if (typeKind == BfIRType::TypeKind::TypeKind_TypeId) if (typeKind == BfIRType::TypeKind::TypeKind_TypeId)
llvmType = typeEntry.mLLVMType; llvmType = typeEntry.mLLVMType;

View file

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

View file

@ -2901,7 +2901,7 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
continue; continue;
SetAndRestoreValue<BfFieldDef*> prevTypeRef(mContext->mCurTypeState->mCurFieldDef, field); SetAndRestoreValue<BfFieldDef*> prevTypeRef(mContext->mCurTypeState->mCurFieldDef, field);
BfType* resolvedFieldType = NULL; BfType* resolvedFieldType = NULL;
if (field->IsEnumCaseEntry()) if (field->IsEnumCaseEntry())
@ -2938,7 +2938,7 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
// For 'let', make read-only // For 'let', make read-only
} }
else else
{ {
resolvedFieldType = ResolveTypeRef(field->mTypeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_NoResolveGenericParam); resolvedFieldType = ResolveTypeRef(field->mTypeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_NoResolveGenericParam);
if (resolvedFieldType == NULL) 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()) if (resolvedFieldType->IsMethodRef())
{ {
auto methodRefType = (BfMethodRefType*)resolvedFieldType; auto methodRefType = (BfMethodRefType*)resolvedFieldType;