From 7e111329c785cc88f8e0cb8edf2f80b227633095 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Wed, 16 Sep 2020 07:17:06 -0700 Subject: [PATCH] Trimmed reflected iface method table --- BeefLibs/corlib/src/Reflection/MethodInfo.bf | 5 +++- BeefLibs/corlib/src/Type.bf | 3 +- IDE/mintest/minlib/src/System/Type.bf | 3 +- IDEHelper/Compiler/BfModule.cpp | 31 ++++++++++---------- IDEHelper/DbgExprEvaluator.cpp | 4 +-- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/BeefLibs/corlib/src/Reflection/MethodInfo.bf b/BeefLibs/corlib/src/Reflection/MethodInfo.bf index a90ddb68..fe42468f 100644 --- a/BeefLibs/corlib/src/Reflection/MethodInfo.bf +++ b/BeefLibs/corlib/src/Reflection/MethodInfo.bf @@ -298,7 +298,10 @@ namespace System.Reflection if (interfaceData == null) return .Err(.InvalidTarget); - funcPtr = *(thisType.[Friend]mInterfaceMethodTable + interfaceData.mStartInterfaceTableIdx + mMethodData.mMethodIdx); + int ifaceMethodIdx = interfaceData.mStartInterfaceTableIdx + mMethodData.mMethodIdx; + if (ifaceMethodIdx >= thisType.[Friend]mInterfaceMethodCount) + return .Err(.InvalidTarget); + funcPtr = *(thisType.[Friend]mInterfaceMethodTable + ifaceMethodIdx); } ifaceOffset = mTypeInstance.[Friend]mMemberDataOffset; diff --git a/BeefLibs/corlib/src/Type.bf b/BeefLibs/corlib/src/Type.bf index ef228a58..9483226a 100644 --- a/BeefLibs/corlib/src/Type.bf +++ b/BeefLibs/corlib/src/Type.bf @@ -688,7 +688,8 @@ namespace System.Reflection int32 mInheritanceCount; uint8 mInterfaceSlot; - uint8 mInterfaceCount; + uint8 mInterfaceCount; + int16 mInterfaceMethodCount; int16 mMethodDataCount; int16 mPropertyDataCount; int16 mFieldDataCount; diff --git a/IDE/mintest/minlib/src/System/Type.bf b/IDE/mintest/minlib/src/System/Type.bf index adaeaa01..2ff82dc4 100644 --- a/IDE/mintest/minlib/src/System/Type.bf +++ b/IDE/mintest/minlib/src/System/Type.bf @@ -643,7 +643,8 @@ namespace System.Reflection int32 mInheritanceCount; uint8 mInterfaceSlot; - uint8 mInterfaceCount; + uint8 mInterfaceCount; + int16 mInterfaceMethodCount; int16 mMethodDataCount; int16 mPropertyDataCount; int16 mFieldDataCount; diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index 70a9bc5d..ee5c32b7 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -6308,14 +6308,10 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin } BfIRValue interfaceMethodTable; + int ifaceMethodTableSize = 0; if ((!typeInstance->IsInterface()) && (typeInstance->mIsReified) && (!typeInstance->IsUnspecializedType()) && (!typeInstance->mInterfaceMethodTable.IsEmpty())) - { - if (typeDef->mName->ToString() == "StructA") - { - NOP; - } - + { SizedArray methods; for (auto& methodEntry : typeInstance->mInterfaceMethodTable) { @@ -6333,16 +6329,20 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin methods.Add(funcVal); } -// while ((!methods.IsEmpty()) && (methods.back() == voidPtrNull)) -// methods.pop_back(); + while ((!methods.IsEmpty()) && (methods.back() == voidPtrNull)) + methods.pop_back(); - BfIRType methodDataArrayType = mBfIRBuilder->GetSizedArrayType(mBfIRBuilder->MapType(voidPtrType, BfIRPopulateType_Full), (int)methods.size()); - BfIRValue methodDataConst = mBfIRBuilder->CreateConstArray(methodDataArrayType, methods); - BfIRValue methodDataArray = mBfIRBuilder->CreateGlobalVariable(methodDataArrayType, true, BfIRLinkageType_Internal, - methodDataConst, "imethods." + typeDataName); - interfaceMethodTable = mBfIRBuilder->CreateBitCast(methodDataArray, voidPtrPtrIRType); - } - else + if (!methods.IsEmpty()) + { + BfIRType methodDataArrayType = mBfIRBuilder->GetSizedArrayType(mBfIRBuilder->MapType(voidPtrType, BfIRPopulateType_Full), (int)methods.size()); + BfIRValue methodDataConst = mBfIRBuilder->CreateConstArray(methodDataArrayType, methods); + BfIRValue methodDataArray = mBfIRBuilder->CreateGlobalVariable(methodDataArrayType, true, BfIRLinkageType_Internal, + methodDataConst, "imethods." + typeDataName); + interfaceMethodTable = mBfIRBuilder->CreateBitCast(methodDataArray, voidPtrPtrIRType); + ifaceMethodTableSize = (int)methods.size(); + } + } + if (!interfaceMethodTable) interfaceMethodTable = mBfIRBuilder->CreateConstNull(voidPtrPtrIRType); ///// @@ -6394,6 +6394,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin GetConstValue(typeInstance->mSlotNum, byteType), // mInterfaceSlot GetConstValue(interfaceCount, byteType), // mInterfaceCount + GetConstValue(ifaceMethodTableSize, shortType), // mInterfaceMethodCount GetConstValue((int)methodTypes.size(), shortType), // mMethodDataCount GetConstValue(0, shortType), // mPropertyDataCount GetConstValue((int)fieldTypes.size(), shortType), // mFieldDataCount diff --git a/IDEHelper/DbgExprEvaluator.cpp b/IDEHelper/DbgExprEvaluator.cpp index a6e806b8..495e937a 100644 --- a/IDEHelper/DbgExprEvaluator.cpp +++ b/IDEHelper/DbgExprEvaluator.cpp @@ -1306,7 +1306,7 @@ void DbgExprEvaluator::BeefTypeToString(const DbgTypedValue& val, String& outStr int32 mInheritanceId; int32 mInheritanceCount; - uint8 mInterfaceSlot; + /*uint8 mInterfaceSlot; uint8 mInterfaceCount; int16 mMethodDataCount; int16 mPropertyDataCount; @@ -1317,7 +1317,7 @@ void DbgExprEvaluator::BeefTypeToString(const DbgTypedValue& val, String& outStr addr_target mMethodDataPtr; addr_target mPropertyDataPtr; addr_target mFieldDataPtr; - addr_target mCustomAttrDataPtr; + addr_target mCustomAttrDataPtr;*/ }; struct _SpecializedGenericType : _TypeInstance