1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38: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

@ -36,7 +36,7 @@ namespace System
NFKC = Stable | Compose | Compat,
}
[CRepr]
[Ordered]
class String : IHashable, IFormattable, IPrintable, IOpComparable
{
enum CreateFlags

View file

@ -12,7 +12,7 @@ namespace System
// including the vtable and interface slots
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class Type
{
extern const Type* sTypes;
@ -558,7 +558,7 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class TypeInstance : Type
{
[CRepr, AlwaysInclude]
@ -770,7 +770,7 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class PointerType : Type
{
TypeId mElementType;
@ -790,7 +790,43 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class RefType : Type
{
public enum RefKind
{
Ref,
Out,
Mut
}
TypeId mElementType;
RefKind mRefKind;
public RefKind RefKind => mRefKind;
public override Type UnderlyingType
{
get
{
return Type.[Friend]GetType(mElementType);
}
}
public override void GetFullName(String strBuffer)
{
switch (mRefKind)
{
case .Ref: strBuffer.Append("ref ");
case .Out: strBuffer.Append("out ");
case .Mut: strBuffer.Append("mut ");
}
UnderlyingType.GetFullName(strBuffer);
}
}
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class SizedArrayType : Type
{
TypeId mElementType;
@ -821,7 +857,7 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class UnspecializedGenericType : TypeInstance
{
[CRepr, AlwaysInclude]
@ -834,7 +870,7 @@ namespace System.Reflection
}
// Only for resolved types
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class SpecializedGenericType : TypeInstance
{
TypeId mUnspecializedType;
@ -880,7 +916,7 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class ArrayType : SpecializedGenericType
{
int32 mElementSize;

View file

@ -12,7 +12,7 @@ namespace System
// including the vtable and interface slots
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class Type
{
extern const Type* sTypes;
@ -35,6 +35,14 @@ namespace System
}
}
public static Enumerator Types
{
get
{
return .();
}
}
public int32 Size
{
get
@ -461,6 +469,35 @@ namespace System
{
return FieldInfo.Enumerator(null, bindingFlags);
}
public Result<T> GetCustomAttribute<T>() where T : Attribute
{
if (var typeInstance = this as TypeInstance)
return typeInstance.[Friend]GetCustomAttribute<T>(typeInstance.[Friend]mCustomAttributesIdx);
return .Err;
}
public override void ToString(String strBuffer)
{
GetFullName(strBuffer);
}
public struct Enumerator : IEnumerator<Type>
{
int32 mCurId;
public Result<Type> GetNext() mut
{
while (true)
{
if (mCurId >= sTypeCount)
return .Err;
let type = sTypes[mCurId++];
if (type != null)
return .Ok(type);
}
}
}
}
enum TypeCode : uint8
@ -521,7 +558,7 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class TypeInstance : Type
{
[CRepr, AlwaysInclude]
@ -596,7 +633,6 @@ namespace System.Reflection
FieldData* mFieldDataPtr;
void** mCustomAttrDataPtr;
public override int32 InstanceSize
{
get
@ -716,9 +752,25 @@ namespace System.Reflection
{
return FieldInfo.Enumerator(this, bindingFlags);
}
Result<T> GetCustomAttribute<T>(int customAttributeIdx) where T : Attribute
{
if (customAttributeIdx == -1)
return .Err;
void* data = mCustomAttrDataPtr[customAttributeIdx];
T attrInst = ?;
switch (AttributeInfo.GetCustomAttribute(data, typeof(T), &attrInst))
{
case .Ok: return .Ok(attrInst);
default:
return .Err;
}
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class PointerType : Type
{
TypeId mElementType;
@ -738,7 +790,43 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class RefType : Type
{
public enum RefKind
{
Ref,
Out,
Mut
}
TypeId mElementType;
RefKind mRefKind;
public RefKind RefKind => mRefKind;
public override Type UnderlyingType
{
get
{
return Type.[Friend]GetType(mElementType);
}
}
public override void GetFullName(String strBuffer)
{
switch (mRefKind)
{
case .Ref: strBuffer.Append("ref ");
case .Out: strBuffer.Append("out ");
case .Mut: strBuffer.Append("mut ");
}
UnderlyingType.GetFullName(strBuffer);
}
}
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class SizedArrayType : Type
{
TypeId mElementType;
@ -769,7 +857,7 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class UnspecializedGenericType : TypeInstance
{
[CRepr, AlwaysInclude]
@ -782,7 +870,7 @@ namespace System.Reflection
}
// Only for resolved types
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class SpecializedGenericType : TypeInstance
{
TypeId mUnspecializedType;
@ -828,7 +916,7 @@ namespace System.Reflection
}
}
[CRepr, AlwaysInclude(AssumeInstantiated=true)]
[Ordered, AlwaysInclude(AssumeInstantiated=true)]
class ArrayType : SpecializedGenericType
{
int32 mElementSize;
@ -890,7 +978,7 @@ namespace System.Reflection
EnumDiscriminator = 0x0200
}
public enum MethodFlags : int16
public enum MethodFlags : uint16
{
MethodAccessMask = 0x0007,
PrivateScope = 0x0000, // Member not referenceable.
@ -923,6 +1011,7 @@ namespace System.Reflection
StdCall = 0x1000,
FastCall = 0x2000,
ThisCall = 0x3000, // Purposely resuing StdCall|FastCall
Mutating = 0x4000
Mutating = 0x4000,
Constructor = 0x8000,
}
}

View file

@ -216,10 +216,6 @@ BF_STATIC_ASSERT(BF_ARRAY_COUNT(gIRCmdNames) == BfIRCmd_COUNT);
#define CMD_PARAM(ty, name) ty name; Read(name);
template <typename T>
class CmdParamVec : public SizedArray<T, 8>
{};
BeIRCodeGen::BeIRCodeGen()
{
mBfIRBuilder = NULL;
@ -390,6 +386,33 @@ BeIRTypeEntry& BeIRCodeGen::GetTypeEntry(int typeId)
return typeEntry;
}
void BeIRCodeGen::FixValues(BeStructType* structType, CmdParamVec<BeValue*>& values)
{
if (values.size() >= structType->mMembers.size())
return;
int readIdx = values.size() - 1;
values.resize(structType->mMembers.size());
for (int i = (int)values.size() - 1; i >= 0; i--)
{
if (mBeContext->AreTypesEqual(values[readIdx]->GetType(), structType->mMembers[i].mType))
{
values[i] = values[readIdx];
readIdx--;
}
else if (structType->mMembers[i].mType->IsSizedArray())
{
auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
beConst->mType = structType->mMembers[i].mType;
values[i] = beConst;
}
else
{
FatalError("Malformed structure values");
}
}
}
void BeIRCodeGen::Init(const BfSizedArray<uint8>& buffer)
{
BP_ZONE("BeIRCodeGen::Init");
@ -754,6 +777,15 @@ void BeIRCodeGen::Read(BeValue*& beValue)
BE_MEM_END("ParamType_Const_Array");
return;
}
else if (constType == BfConstType_ArrayZero8)
{
CMD_PARAM(int, count);
auto beType = mBeContext->CreateSizedArrayType(mBeContext->GetPrimitiveType(BeTypeCode_Int8), count);
auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
beConst->mType = beType;
beValue = beConst;
return;
}
bool isSigned = false;
BeType* llvmConstType = GetBeType(typeCode, isSigned);
@ -1054,11 +1086,14 @@ void BeIRCodeGen::HandleNextCmd()
auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
constStruct->mType = type;
BF_ASSERT(type->IsStruct());
FixValues((BeStructType*)type, values);
BF_ASSERT(((BeStructType*)type)->mMembers.size() == values.size());
for (int i = 0; i < (int)values.size(); i++)
{
auto val = values[i];
BF_ASSERT(((BeStructType*)type)->mMembers[i].mType == val->GetType());
BF_ASSERT(mBeContext->AreTypesEqual(((BeStructType*)type)->mMembers[i].mType, val->GetType()));
constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
}
SetResult(curId, constStruct);

View file

@ -53,6 +53,11 @@ public:
}
};
template <typename T>
class CmdParamVec : public SizedArray<T, 8>
{};
class BeIRCodeGen : public BfIRCodeGenBase
{
public:
@ -85,6 +90,8 @@ public:
BeType* GetBeType(BfTypeCode typeCode, bool& isSigned);
BeIRTypeEntry& GetTypeEntry(int typeId);
void FixValues(BeStructType* structType, CmdParamVec<BeValue*>& values);
public:
BeIRCodeGen();
~BeIRCodeGen();

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,6 +1292,7 @@ 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));
SetResult(curId, llvm::ConstantStruct::get((llvm::StructType*)type, copyValues));

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

@ -4336,10 +4336,8 @@ 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,7 +2930,7 @@ 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)