1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +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, NFKC = Stable | Compose | Compat,
} }
[CRepr] [Ordered]
class String : IHashable, IFormattable, IPrintable, IOpComparable class String : IHashable, IFormattable, IPrintable, IOpComparable
{ {
enum CreateFlags enum CreateFlags

View file

@ -12,7 +12,7 @@ namespace System
// including the vtable and interface slots // including the vtable and interface slots
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class Type public class Type
{ {
extern const Type* sTypes; extern const Type* sTypes;
@ -558,7 +558,7 @@ namespace System.Reflection
} }
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class TypeInstance : Type public class TypeInstance : Type
{ {
[CRepr, AlwaysInclude] [CRepr, AlwaysInclude]
@ -770,7 +770,7 @@ namespace System.Reflection
} }
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
class PointerType : Type class PointerType : Type
{ {
TypeId mElementType; 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 class SizedArrayType : Type
{ {
TypeId mElementType; TypeId mElementType;
@ -821,7 +857,7 @@ namespace System.Reflection
} }
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
class UnspecializedGenericType : TypeInstance class UnspecializedGenericType : TypeInstance
{ {
[CRepr, AlwaysInclude] [CRepr, AlwaysInclude]
@ -834,7 +870,7 @@ namespace System.Reflection
} }
// Only for resolved types // Only for resolved types
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
class SpecializedGenericType : TypeInstance class SpecializedGenericType : TypeInstance
{ {
TypeId mUnspecializedType; TypeId mUnspecializedType;
@ -880,7 +916,7 @@ namespace System.Reflection
} }
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
class ArrayType : SpecializedGenericType class ArrayType : SpecializedGenericType
{ {
int32 mElementSize; int32 mElementSize;

View file

@ -12,7 +12,7 @@ namespace System
// including the vtable and interface slots // including the vtable and interface slots
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class Type public class Type
{ {
extern const Type* sTypes; extern const Type* sTypes;
@ -35,6 +35,14 @@ namespace System
} }
} }
public static Enumerator Types
{
get
{
return .();
}
}
public int32 Size public int32 Size
{ {
get get
@ -461,6 +469,35 @@ namespace System
{ {
return FieldInfo.Enumerator(null, bindingFlags); 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 enum TypeCode : uint8
@ -521,7 +558,7 @@ namespace System.Reflection
} }
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
public class TypeInstance : Type public class TypeInstance : Type
{ {
[CRepr, AlwaysInclude] [CRepr, AlwaysInclude]
@ -588,15 +625,14 @@ namespace System.Reflection
uint8 mInterfaceCount; uint8 mInterfaceCount;
int16 mMethodDataCount; int16 mMethodDataCount;
int16 mPropertyDataCount; int16 mPropertyDataCount;
int16 mFieldDataCount; int16 mFieldDataCount;
void* mInterfaceDataPtr; void* mInterfaceDataPtr;
MethodData* mMethodDataPtr; MethodData* mMethodDataPtr;
void* mPropertyDataPtr; void* mPropertyDataPtr;
FieldData* mFieldDataPtr; FieldData* mFieldDataPtr;
void** mCustomAttrDataPtr; void** mCustomAttrDataPtr;
public override int32 InstanceSize public override int32 InstanceSize
{ {
get get
@ -716,9 +752,25 @@ namespace System.Reflection
{ {
return FieldInfo.Enumerator(this, bindingFlags); 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 class PointerType : Type
{ {
TypeId mElementType; 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 class SizedArrayType : Type
{ {
TypeId mElementType; TypeId mElementType;
@ -769,7 +857,7 @@ namespace System.Reflection
} }
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
class UnspecializedGenericType : TypeInstance class UnspecializedGenericType : TypeInstance
{ {
[CRepr, AlwaysInclude] [CRepr, AlwaysInclude]
@ -782,7 +870,7 @@ namespace System.Reflection
} }
// Only for resolved types // Only for resolved types
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
class SpecializedGenericType : TypeInstance class SpecializedGenericType : TypeInstance
{ {
TypeId mUnspecializedType; TypeId mUnspecializedType;
@ -828,7 +916,7 @@ namespace System.Reflection
} }
} }
[CRepr, AlwaysInclude(AssumeInstantiated=true)] [Ordered, AlwaysInclude(AssumeInstantiated=true)]
class ArrayType : SpecializedGenericType class ArrayType : SpecializedGenericType
{ {
int32 mElementSize; int32 mElementSize;
@ -890,7 +978,7 @@ namespace System.Reflection
EnumDiscriminator = 0x0200 EnumDiscriminator = 0x0200
} }
public enum MethodFlags : int16 public enum MethodFlags : uint16
{ {
MethodAccessMask = 0x0007, MethodAccessMask = 0x0007,
PrivateScope = 0x0000, // Member not referenceable. PrivateScope = 0x0000, // Member not referenceable.
@ -923,6 +1011,7 @@ namespace System.Reflection
StdCall = 0x1000, StdCall = 0x1000,
FastCall = 0x2000, FastCall = 0x2000,
ThisCall = 0x3000, // Purposely resuing StdCall|FastCall 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); #define CMD_PARAM(ty, name) ty name; Read(name);
template <typename T>
class CmdParamVec : public SizedArray<T, 8>
{};
BeIRCodeGen::BeIRCodeGen() BeIRCodeGen::BeIRCodeGen()
{ {
mBfIRBuilder = NULL; mBfIRBuilder = NULL;
@ -390,6 +386,33 @@ BeIRTypeEntry& BeIRCodeGen::GetTypeEntry(int typeId)
return typeEntry; 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) void BeIRCodeGen::Init(const BfSizedArray<uint8>& buffer)
{ {
BP_ZONE("BeIRCodeGen::Init"); BP_ZONE("BeIRCodeGen::Init");
@ -754,6 +777,15 @@ void BeIRCodeGen::Read(BeValue*& beValue)
BE_MEM_END("ParamType_Const_Array"); BE_MEM_END("ParamType_Const_Array");
return; 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; bool isSigned = false;
BeType* llvmConstType = GetBeType(typeCode, isSigned); BeType* llvmConstType = GetBeType(typeCode, isSigned);
@ -1054,11 +1086,14 @@ void BeIRCodeGen::HandleNextCmd()
auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>(); auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
constStruct->mType = type; constStruct->mType = type;
BF_ASSERT(type->IsStruct()); BF_ASSERT(type->IsStruct());
FixValues((BeStructType*)type, values);
BF_ASSERT(((BeStructType*)type)->mMembers.size() == values.size()); BF_ASSERT(((BeStructType*)type)->mMembers.size() == values.size());
for (int i = 0; i < (int)values.size(); i++) for (int i = 0; i < (int)values.size(); i++)
{ {
auto val = values[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)); constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
} }
SetResult(curId, constStruct); SetResult(curId, constStruct);

View file

@ -53,6 +53,11 @@ public:
} }
}; };
template <typename T>
class CmdParamVec : public SizedArray<T, 8>
{};
class BeIRCodeGen : public BfIRCodeGenBase class BeIRCodeGen : public BfIRCodeGenBase
{ {
public: public:
@ -83,7 +88,9 @@ public:
void SetResult(int id, BeMDNode* md); void SetResult(int id, BeMDNode* md);
BeType* GetBeType(BfTypeCode typeCode, bool& isSigned); BeType* GetBeType(BfTypeCode typeCode, bool& isSigned);
BeIRTypeEntry& GetTypeEntry(int typeId); BeIRTypeEntry& GetTypeEntry(int typeId);
void FixValues(BeStructType* structType, CmdParamVec<BeValue*>& values);
public: public:
BeIRCodeGen(); BeIRCodeGen();

View file

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

View file

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

View file

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

View file

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

View file

@ -377,6 +377,31 @@ void BfIRCodeGen::PrintFunction()
os.flush(); 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) BfTypeCode BfIRCodeGen::GetTypeCode(llvm::Type* type, bool isSigned)
{ {
if (type->isIntegerTy()) if (type->isIntegerTy())
@ -1267,8 +1292,9 @@ void BfIRCodeGen::HandleNextCmd()
CMD_PARAM(llvm::Type*, type); CMD_PARAM(llvm::Type*, type);
CMD_PARAM(CmdParamVec<llvm::Value*>, values) CMD_PARAM(CmdParamVec<llvm::Value*>, values)
llvm::SmallVector<llvm::Constant*, 8> copyValues; llvm::SmallVector<llvm::Constant*, 8> copyValues;
FixValues((llvm::StructType*)type, values);
for (auto val : 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)); SetResult(curId, llvm::ConstantStruct::get((llvm::StructType*)type, copyValues));
} }
break; break;

View file

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

View file

@ -4335,11 +4335,9 @@ void BfModule::CreateValueTypeEqualsMethod(bool strictEquals)
auto baseTypeInst = compareTypeInst->mBaseType; auto baseTypeInst = compareTypeInst->mBaseType;
if ((baseTypeInst != NULL) && (baseTypeInst->mTypeDef != mCompiler->mValueTypeTypeDef)) if ((baseTypeInst != NULL) && (baseTypeInst->mTypeDef != mCompiler->mValueTypeTypeDef))
{ {
BfTypedValue leftOrigValue(mCurMethodState->mLocals[0]->mValue, compareTypeInst, true); BfTypedValue leftValue = Cast(NULL, leftTypedVal, baseTypeInst);
BfTypedValue rightOrigValue(mCurMethodState->mLocals[1]->mValue, compareTypeInst, true); BfTypedValue rightValue = Cast(NULL, rightTypedVal, baseTypeInst);
BfTypedValue leftValue = Cast(NULL, leftOrigValue, baseTypeInst);
BfTypedValue rightValue = Cast(NULL, rightOrigValue, baseTypeInst);
EmitEquals(leftValue, rightValue, exitBB, strictEquals); EmitEquals(leftValue, rightValue, exitBB, strictEquals);
} }
} }
@ -4639,6 +4637,8 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
} }
else if (type->IsPointer()) else if (type->IsPointer())
typeDataSource = ResolveTypeDef(mCompiler->mReflectPointerType)->ToTypeInstance(); typeDataSource = ResolveTypeDef(mCompiler->mReflectPointerType)->ToTypeInstance();
else if (type->IsRef())
typeDataSource = ResolveTypeDef(mCompiler->mReflectRefType)->ToTypeInstance();
else if (type->IsSizedArray()) else if (type->IsSizedArray())
typeDataSource = ResolveTypeDef(mCompiler->mReflectSizedArrayType)->ToTypeInstance(); typeDataSource = ResolveTypeDef(mCompiler->mReflectSizedArrayType)->ToTypeInstance();
else else
@ -4700,6 +4700,11 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
typeCode = BfTypeCode_Pointer; typeCode = BfTypeCode_Pointer;
typeFlags |= BfTypeFlags_Pointer; typeFlags |= BfTypeFlags_Pointer;
} }
else if (type->IsRef())
{
typeCode = BfTypeCode_Pointer;
typeFlags |= BfTypeFlags_Pointer;
}
if (type->IsObject()) if (type->IsObject())
{ {
@ -4781,6 +4786,23 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize); mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize);
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType)); 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()) else if (type->IsSizedArray())
{ {
auto sizedArrayType = (BfSizedArrayType*)type; auto sizedArrayType = (BfSizedArrayType*)type;
@ -6190,6 +6212,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
SizedArray<BfIRValue, 32> typeDataVals = SizedArray<BfIRValue, 32> typeDataVals =
{ {
typeData, typeData,
castedClassVData, // mTypeClassVData castedClassVData, // mTypeClassVData
typeNameConst, // mName typeNameConst, // mName
namespaceConst, // mNamespace 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) 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); mBfIRBuilder->CreateValueScopeRetain(localVarDef->mValue);
mCurMethodState->mCurScope->mHadScopeValueRetain = true; 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(); auto resolvedFieldType = fieldInstance->GetResolvedType();
BF_ASSERT(resolvedFieldType->mSize >= 0); BF_ASSERT(resolvedFieldType->mSize >= 0);
@ -2925,14 +2930,14 @@ bool BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
int alignSize = resolvedFieldType->mAlign; int alignSize = resolvedFieldType->mAlign;
fieldInstance->mDataSize = dataSize; fieldInstance->mDataSize = dataSize;
bool needsExplicitAlignment = !isCRepr || resolvedFieldType->NeedsExplicitAlignment(); //bool needsExplicitAlignment = !isCRepr || resolvedFieldType->NeedsExplicitAlignment();
int nextDataPos = dataPos; int nextDataPos = dataPos;
if (!isPacked) if (!isPacked)
nextDataPos = (dataPos + (alignSize - 1)) & ~(alignSize - 1); nextDataPos = (dataPos + (alignSize - 1)) & ~(alignSize - 1);
int padding = nextDataPos - dataPos; int padding = nextDataPos - dataPos;
if ((alignSize > 1) && (needsExplicitAlignment) && (padding > 0)) if ((alignSize > 1) && (needsExplicitAlignment) && (padding > 0))
{ {
curFieldDataIdx++; curFieldDataIdx++;
} }
dataPos = nextDataPos; dataPos = nextDataPos;
@ -2941,7 +2946,7 @@ bool BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
if (!isPacked) if (!isPacked)
typeInstance->mInstAlign = std::max(typeInstance->mInstAlign, alignSize); typeInstance->mInstAlign = std::max(typeInstance->mInstAlign, alignSize);
dataPos += dataSize; dataPos += dataSize;
} }
if (unionInnerType != NULL) if (unionInnerType != NULL)