From 6fe6b8f2e696e89dfca73c2ebf565fe3e6ee95f1 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Wed, 29 Dec 2021 13:01:13 -0500 Subject: [PATCH] Fixed 64-bit field data for 32-bit builds --- BeefLibs/corlib/src/Enum.bf | 6 +- BeefLibs/corlib/src/Type.bf | 5 +- IDEHelper/Compiler/BfModule.cpp | 122 ++++++++++++++++++++++++-------- 3 files changed, 98 insertions(+), 35 deletions(-) diff --git a/BeefLibs/corlib/src/Enum.bf b/BeefLibs/corlib/src/Enum.bf index aeea782d..47656878 100644 --- a/BeefLibs/corlib/src/Enum.bf +++ b/BeefLibs/corlib/src/Enum.bf @@ -10,7 +10,7 @@ namespace System for (var field in type.GetFields()) { if (field.[Friend]mFieldData.mFlags.HasFlag(.EnumCase) && - field.[Friend]mFieldData.[Friend]mData == iVal) + *(int64*)&field.[Friend]mFieldData.[Friend]mData == iVal) { strBuffer.Append(field.Name); return; @@ -132,7 +132,7 @@ namespace System { get { - return ((.)base.Current.[Friend]mFieldData.[Friend]mName, (.)base.Current.[Friend]mFieldData.[Friend]mData); + return ((.)base.Current.[Friend]mFieldData.[Friend]mName, (.)*(int64*)&base.Current.[Friend]mFieldData.[Friend]mData); } } @@ -151,7 +151,7 @@ namespace System { get { - return (.)base.Current.[Friend]mFieldData.[Friend]mData; + return (.)*(int64*)&base.Current.[Friend]mFieldData.[Friend]mData; } } diff --git a/BeefLibs/corlib/src/Type.bf b/BeefLibs/corlib/src/Type.bf index 9c76b702..e78108f8 100644 --- a/BeefLibs/corlib/src/Type.bf +++ b/BeefLibs/corlib/src/Type.bf @@ -694,7 +694,10 @@ namespace System.Reflection { public String mName; public TypeId mFieldTypeId; - public int64 mData; + public int mData; +#if BF_32_BIT + public int mDataHi; +#endif public FieldFlags mFlags; public int32 mCustomAttributesIdx; } diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index b833666c..47ba55c3 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -6372,36 +6372,72 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin FieldFlags_EnumCase = 0x400 }; + bool is32Bit = mCompiler->mSystem->mPtrSize == 4; + if ((typeInstance->IsPayloadEnum()) && (!typeInstance->IsBoxed())) { BfType* payloadType = typeInstance->GetUnionInnerType(); if (!payloadType->IsValuelessType()) { BfIRValue payloadNameConst = GetStringObjectValue("$payload", !mIsComptimeModule); - SizedArray payloadFieldVals = + SizedArray payloadFieldVals; + if (is32Bit) { - emptyValueType, - payloadNameConst, // mName - GetConstValue(payloadType->mTypeId, typeIdType), // mFieldTypeId - GetConstValue(0, longType), // mData - GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumPayload, shortType), // mFlags - GetConstValue(-1, intType), // mCustomAttributesIdx - }; + payloadFieldVals = + { + emptyValueType, + payloadNameConst, // mName + GetConstValue(payloadType->mTypeId, typeIdType), // mFieldTypeId + GetConstValue(0, intPtrType), // mData + GetConstValue(0, intPtrType), // mDataHi + GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumPayload, shortType), // mFlags + GetConstValue(-1, intType), // mCustomAttributesIdx + }; + } + else + { + payloadFieldVals = + { + emptyValueType, + payloadNameConst, // mName + GetConstValue(payloadType->mTypeId, typeIdType), // mFieldTypeId + GetConstValue(0, intPtrType), // mData + GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumPayload, shortType), // mFlags + GetConstValue(-1, intType), // mCustomAttributesIdx + }; + } auto payloadFieldData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance(), BfIRPopulateType_Full), payloadFieldVals); fieldTypes.push_back(payloadFieldData); } BfType* dscrType = typeInstance->GetDiscriminatorType(); BfIRValue dscrNameConst = GetStringObjectValue("$discriminator", !mIsComptimeModule); - SizedArray dscrFieldVals = + SizedArray dscrFieldVals; + if (is32Bit) { - emptyValueType, - dscrNameConst, // mName - GetConstValue(dscrType->mTypeId, typeIdType), // mFieldTypeId - GetConstValue(BF_ALIGN(payloadType->mSize, dscrType->mAlign), longType), // mData - GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumDiscriminator, shortType), // mFlags - GetConstValue(-1, intType), // mCustomAttributesIdx - }; + dscrFieldVals = + { + emptyValueType, + dscrNameConst, // mName + GetConstValue(dscrType->mTypeId, typeIdType), // mFieldTypeId + GetConstValue(BF_ALIGN(payloadType->mSize, dscrType->mAlign), intPtrType), // mData + GetConstValue(0, intPtrType), // mDataHi + GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumDiscriminator, shortType), // mFlags + GetConstValue(-1, intType), // mCustomAttributesIdx + }; + } + else + { + dscrFieldVals = + { + emptyValueType, + dscrNameConst, // mName + GetConstValue(dscrType->mTypeId, typeIdType), // mFieldTypeId + GetConstValue(BF_ALIGN(payloadType->mSize, dscrType->mAlign), intPtrType), // mData + GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumDiscriminator, shortType), // mFlags + GetConstValue(-1, intType), // mCustomAttributesIdx + }; + } auto dscrFieldData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance(), BfIRPopulateType_Full), dscrFieldVals); fieldTypes.push_back(dscrFieldData); } @@ -6414,7 +6450,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin BfFieldInstance* fieldInstance = &typeInstance->mFieldInstances[fieldIdx]; BfFieldDef* fieldDef = fieldInstance->GetFieldDef(); - BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, !mIsComptimeModule); + BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, !mIsComptimeModule); int typeId = 0; auto fieldType = fieldInstance->GetResolvedType(); @@ -6439,13 +6475,16 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin fieldFlags = (FieldFlags)(fieldFlags | FieldFlags_EnumCase); int customAttrIdx = _HandleCustomAttrs(fieldInstance->mCustomAttributes); - BfIRValue constValue; + BfIRValue constValue; + BfIRValue constValue2; if (fieldInstance->GetFieldDef()->mIsConst) { if (fieldInstance->mConstIdx != -1) { auto constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx); - constValue = mBfIRBuilder->CreateConst(BfTypeCode_Int64, constant->mUInt64); + constValue = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, constant->mUInt64); + if (is32Bit) + constValue2 = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, constant->mUInt64 >> 32); } } else if (fieldInstance->GetFieldDef()->mIsStatic) @@ -6466,24 +6505,45 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin if (refVal.IsAddr()) { - constValue = mBfIRBuilder->CreatePtrToInt(refVal.mValue, BfTypeCode_Int64); + constValue = mBfIRBuilder->CreatePtrToInt(refVal.mValue, BfTypeCode_IntPtr); } } if (!constValue) - constValue = mBfIRBuilder->CreateConst(BfTypeCode_Int64, fieldInstance->mDataOffset); + constValue = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, fieldInstance->mDataOffset); - SizedArray fieldVals = + if (is32Bit) { - emptyValueType, - fieldNameConst, // mName - GetConstValue(typeId, typeIdType), // mFieldTypeId - constValue, // mConstValue - GetConstValue(fieldFlags, shortType), // mFlags - GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx - }; - auto fieldData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance(), BfIRPopulateType_Full), fieldVals); - fieldTypes.push_back(fieldData); + if (!constValue2) + constValue2 = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, 0); + + SizedArray fieldVals = + { + emptyValueType, + fieldNameConst, // mName + GetConstValue(typeId, typeIdType), // mFieldTypeId + constValue, // mData + constValue2, // mDataHi + GetConstValue(fieldFlags, shortType), // mFlags + GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx + }; + auto fieldData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance(), BfIRPopulateType_Full), fieldVals); + fieldTypes.push_back(fieldData); + } + else + { + SizedArray fieldVals = + { + emptyValueType, + fieldNameConst, // mName + GetConstValue(typeId, typeIdType), // mFieldTypeId + constValue, // mData + GetConstValue(fieldFlags, shortType), // mFlags + GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx + }; + auto fieldData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance(), BfIRPopulateType_Full), fieldVals); + fieldTypes.push_back(fieldData); + } } auto reflectFieldDataIRType = mBfIRBuilder->MapType(reflectFieldDataType);