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

Added RefType, changed how CRepr types are represented

This commit is contained in:
Brian Fiete 2020-07-02 11:05:17 -07:00
parent 716f7b3638
commit 0c946de3ca
13 changed files with 311 additions and 40 deletions

View file

@ -418,6 +418,7 @@ BfCompiler::BfCompiler(BfSystem* bfSystem, bool isResolveOnly)
mReflectMethodDataDef = NULL;
mReflectParamDataDef = NULL;
mReflectPointerType = NULL;
mReflectRefType = NULL;
mReflectSizedArrayType = NULL;
mReflectSpecializedGenericType = NULL;
mReflectTypeInstanceTypeDef = NULL;
@ -6218,6 +6219,7 @@ bool BfCompiler::DoCompile(const StringImpl& outputDirectory)
mReflectMethodDataDef = _GetRequiredType("System.Reflection.TypeInstance.MethodData");
mReflectParamDataDef = _GetRequiredType("System.Reflection.TypeInstance.ParamData");
mReflectPointerType = _GetRequiredType("System.Reflection.PointerType");
mReflectRefType = _GetRequiredType("System.Reflection.RefType");
mReflectSizedArrayType = _GetRequiredType("System.Reflection.SizedArrayType");
mReflectSpecializedGenericType = _GetRequiredType("System.Reflection.SpecializedGenericType");
mReflectTypeInstanceTypeDef = _GetRequiredType("System.Reflection.TypeInstance");

View file

@ -369,6 +369,7 @@ public:
BfTypeDef* mReflectMethodDataDef;
BfTypeDef* mReflectParamDataDef;
BfTypeDef* mReflectPointerType;
BfTypeDef* mReflectRefType;
BfTypeDef* mReflectSizedArrayType;
BfTypeDef* mReflectSpecializedGenericType;
BfTypeDef* mReflectTypeInstanceTypeDef;

View file

@ -655,6 +655,33 @@ BfIRValue BfIRConstHolder::CreateConstArray(BfIRType type, const BfSizedArray<Bf
return irValue;
}
BfIRValue BfIRConstHolder::CreateConstArrayZero(BfIRType type, int count)
{
BfConstantArrayZero* constant = mTempAlloc.Alloc<BfConstantArrayZero>();
constant->mConstType = BfConstType_ArrayZero;
constant->mType = type = type;
constant->mCount = count;
auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
#ifdef CHECK_CONSTHOLDER
irValue.mHolder = this;
#endif
return irValue;
}
BfIRValue BfIRConstHolder::CreateConstArrayZero(int count)
{
BfConstant* constant = mTempAlloc.Alloc<BfConstant>();
constant->mConstType = BfConstType_ArrayZero8;
constant->mInt64 = count;
auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
#ifdef CHECK_CONSTHOLDER
irValue.mHolder = this;
#endif
return irValue;
}
BfIRValue BfIRConstHolder::CreateTypeOf(BfType* type)
{
BfTypeOf_Const* typeOf = mTempAlloc.Alloc<BfTypeOf_Const>();
@ -1779,6 +1806,11 @@ void BfIRBuilder::Write(const BfIRValue& irValue)
Write(arrayConst->mValues);
}
break;
case (int)BfConstType_ArrayZero8:
{
Write(constant->mInt64);
}
break;
default:
{
BF_FATAL("Unhandled");
@ -3026,7 +3058,8 @@ void BfIRBuilder::CreateTypeDefinition(BfType* type, bool forceDbgDefine)
BfIRType resolvedFieldIRType = MapType(resolvedFieldType);
bool needsExplicitAlignment = !isCRepr || resolvedFieldType->NeedsExplicitAlignment();
//bool needsExplicitAlignment = !isCRepr || resolvedFieldType->NeedsExplicitAlignment();
bool needsExplicitAlignment = true;
if (!needsExplicitAlignment)
{
int alignSize = resolvedFieldType->mAlign;
@ -3075,7 +3108,7 @@ void BfIRBuilder::CreateTypeDefinition(BfType* type, bool forceDbgDefine)
}
if (!typeInstance->IsTypedPrimitive())
StructSetBody(MapTypeInst(typeInstance), irFieldTypes, isPacked || !isCRepr);
StructSetBody(MapTypeInst(typeInstance), irFieldTypes, /*isPacked || !isCRepr*/true);
if (typeInstance->IsNullable())
{

View file

@ -137,6 +137,8 @@ enum BfConstType
BfConstType_TypeOf,
BfConstType_AggZero,
BfConstType_Array,
BfConstType_ArrayZero,
BfConstType_ArrayZero8,
BfConstType_Undef,
BfConstType_SizedArrayType
};
@ -176,6 +178,7 @@ enum BfIRCmd : uint8
BfIRCmd_CreateConstStruct,
BfIRCmd_CreateConstStructZero,
BfIRCmd_CreateConstArray,
BfIRCmd_CreateConstArrayZero,
BfIRCmd_CreateConstString,
BfIRCmd_ConfigConst,
@ -828,6 +831,13 @@ struct BfConstantArray
BfSizedArray<BfIRValue> mValues;
};
struct BfConstantArrayZero
{
BfConstType mConstType;
BfIRType mType;
int mCount;
};
class BfIRConstHolder
{
public:
@ -859,6 +869,8 @@ public:
BfIRValue CreateConstNull(BfIRType nullType);
BfIRValue CreateConstStructZero(BfIRType aggType);
BfIRValue CreateConstArray(BfIRType type, const BfSizedArray<BfIRValue>& values);
BfIRValue CreateConstArrayZero(BfIRType type, int count);
BfIRValue CreateConstArrayZero(int count);
BfIRValue CreateTypeOf(BfType* type);
BfIRValue GetUndefConstValue(BfTypeCode typeCode);
};

View file

@ -377,6 +377,31 @@ void BfIRCodeGen::PrintFunction()
os.flush();
}
void BfIRCodeGen::FixValues(llvm::StructType* structType, llvm::SmallVector<llvm::Value*, 8>& values)
{
if (values.size() >= structType->getNumElements())
return;
int readIdx = (int)values.size() - 1;
values.resize(structType->getNumElements());
for (int i = (int)values.size() - 1; i >= 0; i--)
{
if (values[readIdx]->getType() == structType->getElementType(i))
{
values[i] = values[readIdx];
readIdx--;
}
else if (structType->getElementType(i)->isArrayTy())
{
values[i] = llvm::ConstantAggregateZero::get(structType->getElementType(i));
}
else
{
BF_FATAL("Malformed structure values");
}
}
}
BfTypeCode BfIRCodeGen::GetTypeCode(llvm::Type* type, bool isSigned)
{
if (type->isIntegerTy())
@ -1267,8 +1292,9 @@ void BfIRCodeGen::HandleNextCmd()
CMD_PARAM(llvm::Type*, type);
CMD_PARAM(CmdParamVec<llvm::Value*>, values)
llvm::SmallVector<llvm::Constant*, 8> copyValues;
FixValues((llvm::StructType*)type, values);
for (auto val : values)
copyValues.push_back(llvm::dyn_cast<llvm::Constant>(val));
copyValues.push_back(llvm::dyn_cast<llvm::Constant>(val));
SetResult(curId, llvm::ConstantStruct::get((llvm::StructType*)type, copyValues));
}
break;

View file

@ -88,6 +88,7 @@ public:
Array<llvm::Constant*> mConfigConsts64;
public:
void FixValues(llvm::StructType* structType, llvm::SmallVector<llvm::Value*, 8>& values);
BfTypeCode GetTypeCode(llvm::Type* type, bool isSigned);
llvm::Type* GetLLVMType(BfTypeCode typeCode, bool& isSigned);
BfIRTypeEntry& GetTypeEntry(int typeId);

View file

@ -4335,11 +4335,9 @@ void BfModule::CreateValueTypeEqualsMethod(bool strictEquals)
auto baseTypeInst = compareTypeInst->mBaseType;
if ((baseTypeInst != NULL) && (baseTypeInst->mTypeDef != mCompiler->mValueTypeTypeDef))
{
BfTypedValue leftOrigValue(mCurMethodState->mLocals[0]->mValue, compareTypeInst, true);
BfTypedValue rightOrigValue(mCurMethodState->mLocals[1]->mValue, compareTypeInst, true);
BfTypedValue leftValue = Cast(NULL, leftOrigValue, baseTypeInst);
BfTypedValue rightValue = Cast(NULL, rightOrigValue, baseTypeInst);
{
BfTypedValue leftValue = Cast(NULL, leftTypedVal, baseTypeInst);
BfTypedValue rightValue = Cast(NULL, rightTypedVal, baseTypeInst);
EmitEquals(leftValue, rightValue, exitBB, strictEquals);
}
}
@ -4639,6 +4637,8 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
}
else if (type->IsPointer())
typeDataSource = ResolveTypeDef(mCompiler->mReflectPointerType)->ToTypeInstance();
else if (type->IsRef())
typeDataSource = ResolveTypeDef(mCompiler->mReflectRefType)->ToTypeInstance();
else if (type->IsSizedArray())
typeDataSource = ResolveTypeDef(mCompiler->mReflectSizedArrayType)->ToTypeInstance();
else
@ -4700,6 +4700,11 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
typeCode = BfTypeCode_Pointer;
typeFlags |= BfTypeFlags_Pointer;
}
else if (type->IsRef())
{
typeCode = BfTypeCode_Pointer;
typeFlags |= BfTypeFlags_Pointer;
}
if (type->IsObject())
{
@ -4781,6 +4786,23 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize);
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType));
}
else if (type->IsRef())
{
auto refType = (BfRefType*)type;
SizedArray<BfIRValue, 3> refTypeDataParms =
{
typeData,
GetConstValue(refType->mElementType->mTypeId, typeIdType),
GetConstValue((int8)refType->mRefKind, byteType),
};
auto reflectRefType = ResolveTypeDef(mCompiler->mReflectRefType)->ToTypeInstance();
auto refTypeData = mBfIRBuilder->CreateConstStruct(mBfIRBuilder->MapTypeInst(reflectRefType, BfIRPopulateType_Full), refTypeDataParms);
typeDataVar = mBfIRBuilder->CreateGlobalVariable(mBfIRBuilder->MapTypeInst(reflectRefType), true,
BfIRLinkageType_External, refTypeData, typeDataName);
mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize);
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType));
}
else if (type->IsSizedArray())
{
auto sizedArrayType = (BfSizedArrayType*)type;
@ -6190,6 +6212,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
SizedArray<BfIRValue, 32> typeDataVals =
{
typeData,
castedClassVData, // mTypeClassVData
typeNameConst, // mName
namespaceConst, // mNamespace
@ -12823,9 +12846,10 @@ void BfModule::DoLocalVariableDebugInfo(BfLocalVariable* localVarDef, bool doAli
BfLocalVariable* BfModule::AddLocalVariableDef(BfLocalVariable* localVarDef, bool addDebugInfo, bool doAliasValue, BfIRValue declareBefore, BfIRInitType initType)
{
if ((localVarDef->mValue) && (!localVarDef->mAddr) && (IsTargetingBeefBackend()))
if ((localVarDef->mValue) && (!localVarDef->mAddr) && (IsTargetingBeefBackend()) && (!localVarDef->mResolvedType->IsValuelessType()))
{
if ((!localVarDef->mValue.IsConst()) && (!localVarDef->mValue.IsArg()) && (!localVarDef->mValue.IsFake()))
if ((!localVarDef->mValue.IsConst()) &&
(!localVarDef->mValue.IsArg()) && (!localVarDef->mValue.IsFake()))
{
mBfIRBuilder->CreateValueScopeRetain(localVarDef->mValue);
mCurMethodState->mCurScope->mHadScopeValueRetain = true;

View file

@ -2916,8 +2916,13 @@ bool BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
}
}
for (auto fieldInstance : dataFieldVec)
//bool needsExplicitAlignment = !isCRepr || ((typeInstance->mBaseType != NULL) && (!typeInstance->mBaseType->mIsCRepr));
bool needsExplicitAlignment = true;
for (int fieldIdx = 0; fieldIdx < (int)dataFieldVec.size(); fieldIdx++)
{
auto fieldInstance = dataFieldVec[fieldIdx];
auto resolvedFieldType = fieldInstance->GetResolvedType();
BF_ASSERT(resolvedFieldType->mSize >= 0);
@ -2925,14 +2930,14 @@ bool BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
int alignSize = resolvedFieldType->mAlign;
fieldInstance->mDataSize = dataSize;
bool needsExplicitAlignment = !isCRepr || resolvedFieldType->NeedsExplicitAlignment();
//bool needsExplicitAlignment = !isCRepr || resolvedFieldType->NeedsExplicitAlignment();
int nextDataPos = dataPos;
if (!isPacked)
nextDataPos = (dataPos + (alignSize - 1)) & ~(alignSize - 1);
int padding = nextDataPos - dataPos;
if ((alignSize > 1) && (needsExplicitAlignment) && (padding > 0))
{
{
curFieldDataIdx++;
}
dataPos = nextDataPos;
@ -2941,7 +2946,7 @@ bool BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
if (!isPacked)
typeInstance->mInstAlign = std::max(typeInstance->mInstAlign, alignSize);
dataPos += dataSize;
dataPos += dataSize;
}
if (unionInnerType != NULL)