1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00

32-bit consteval fixes

This commit is contained in:
Brian Fiete 2020-12-23 11:27:46 -08:00
parent 31c89ab8e1
commit e4fa6776a8
4 changed files with 91 additions and 38 deletions

View file

@ -1018,6 +1018,7 @@ void BeIRCodeGen::HandleNextCmd()
mBeContext = new BeContext();
mBeModule = new BeModule(moduleName, mBeContext);
mBeModule->mBeIRCodeGen = this;
mBeContext->mPointerSize = ptrSize;
for (auto constInt : mConfigConsts)
{

View file

@ -19437,7 +19437,10 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup)
// If we hot swap, we want to make sure at least one method refers to this extern method so it gets pulled in
// incase it gets called later by some hot-loaded coded
if ((mCompiler->mOptions.mAllowHotSwapping) && (mCurMethodInstance->mIRFunction) && (!mCurMethodInstance->mIRFunction.IsFake()) && (mCurTypeInstance != mContext->mBfObjectType))
{
if (!mCurMethodInstance->mMethodDef->mName.StartsWith("ConstEval_"))
CreateFakeCallerMethod(mangledName);
}
mBfIRBuilder->Func_DeleteBody(mCurMethodInstance->mIRFunction);
}
else if ((methodInstance->GetImportKind() == BfImportKind_Import_Dynamic) && (!mCompiler->IsHotCompile()))

View file

@ -208,6 +208,7 @@ static_assert(BF_ARRAY_COUNT(gOpInfo) == (int)CeOp_COUNT, "gOpName incorrect siz
CeFunction::~CeFunction()
{
BF_ASSERT(mId == -1);
for (auto innerFunc : mInnerFunctions)
delete innerFunc;
}
@ -586,6 +587,7 @@ void CeBuilder::EmitSizedOp(CeOp baseOp, const CeOperand& operand, CeOperand* ou
{
bool isStdSize = true;
CeOp op = CeOp_InvalidOp;
if (operand.mType->mSize == 1)
op = baseOp;
else if (operand.mType->mSize == 2)
@ -1201,6 +1203,7 @@ void CeBuilder::Build()
innerFunction->mCeInnerFunctionInfo->mOwner = mCeFunction;
mInnerFunctionMap[beFunction] = (int)mCeFunction->mInnerFunctions.size();
mCeFunction->mInnerFunctions.Add(innerFunction);
mCeMachine->MapFunctionId(innerFunction);
}
// for (int globalVarIdx = startGlobalVariableCount; globalVarIdx < (int)beModule->mGlobalVariables.size(); globalVarIdx++)
@ -2601,6 +2604,7 @@ CeMachine::CeMachine(BfCompiler* compiler)
mCeModule = NULL;
mRevision = 0;
mExecuteId = 0;
mCurFunctionId = 0;
mRevisionExecuteTime = 0;
mCurTargetSrc = NULL;
mCurModule = NULL;
@ -2620,6 +2624,14 @@ CeMachine::~CeMachine()
for (auto kv : mFunctions)
{
auto functionInfo = kv.mValue;
if (functionInfo->mCeFunction != NULL)
{
// We don't need to actually unmap it at this point
functionInfo->mCeFunction->mId = -1;
for (auto innerFunction : functionInfo->mCeFunction->mInnerFunctions)
innerFunction->mId = -1;
}
delete functionInfo;
}
}
@ -2967,6 +2979,17 @@ void CeMachine::RemoveMethod(BfMethodInstance* methodInstance)
if (callEntry.mFunctionInfo != NULL)
DerefMethodInfo(callEntry.mFunctionInfo);
}
if (ceFunction->mId != -1)
{
mFunctionIdMap.Remove(ceFunction->mId);
ceFunction->mId = -1;
for (auto innerFunction : ceFunction->mInnerFunctions)
{
mFunctionIdMap.Remove(innerFunction->mId);
innerFunction->mId = -1;
}
}
delete ceFunction;
ceFunctionInfo->mCeFunction = NULL;
ceFunctionInfo->mMethodInstance = NULL;
@ -2986,8 +3009,6 @@ void CeMachine::RemoveMethod(BfMethodInstance* methodInstance)
mFunctions.Remove(itr);
}
CheckFunctions();
}
//#define CE_GETC(T) *((T*)(addr += sizeof(T)) - 1)
@ -3733,9 +3754,11 @@ bool CeMachine::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
uint8* instPtr = (ceFunction->mCode.IsEmpty()) ? NULL : &ceFunction->mCode[0];
uint8* stackPtr = startStackPtr;
uint8* framePtr = startFramePtr;
bool needsFunctionIds = mCeModule->mSystem->mPtrSize != 8;
volatile bool* cancelPtr = &mCompiler->mCanceling;
auto _GetCurFrame = [&]()
{
CeFrame ceFrame;
@ -4196,6 +4219,13 @@ bool CeMachine::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
memcpy(memStart + destAddr, memStart + srcAddr, size);
}
break;
case CeOp_FrameAddr_32:
{
auto& result = CE_GETFRAME(int32);
auto addr = &CE_GETFRAME(uint8);
result = addr - memStart;
}
break;
case CeOp_FrameAddr_64:
{
auto& result = CE_GETFRAME(int64);
@ -4492,7 +4522,7 @@ bool CeMachine::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
{
BF_ASSERT(memStart == mMemory.mVals);
auto& result = CE_GETFRAME(CeFunction*);
auto resultFrameIdx = CE_GETINST(int32);
int32 callIdx = CE_GETINST(int32);
auto& callEntry = ceFunction->mCallTable[callIdx];
if (callEntry.mBindRevision != mRevision)
@ -4535,29 +4565,31 @@ bool CeMachine::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
}
BF_ASSERT(memStart == mMemory.mVals);
result = callEntry.mFunction;
// if (callEntry.mFunction->mName.Contains("__static_dump"))
// {
// int32 val = *(int32*)(stackPtr);
// OutputDebugStrF("__static_dump: %d\n", val);
// }
auto callFunction = callEntry.mFunction;
if (needsFunctionIds)
*(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
else
*(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
}
break;
case CeOp_GetMethod_Inner:
{
auto& result = CE_GETFRAME(CeFunction*);
auto resultFrameIdx = CE_GETINST(int32);
int32 innerIdx = CE_GETINST(int32);
auto outerFunction = ceFunction;
if (outerFunction->mCeInnerFunctionInfo != NULL)
outerFunction = outerFunction->mCeInnerFunctionInfo->mOwner;
auto& callEntry = outerFunction->mInnerFunctions[innerIdx];
result = callEntry;
auto callFunction = outerFunction->mInnerFunctions[innerIdx];
if (needsFunctionIds)
*(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
else
*(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
}
break;
case CeOp_GetMethod_Virt:
{
auto& result = CE_GETFRAME(CeFunction*);
auto resultFrameIdx = CE_GETINST(int32);
auto valueAddr = CE_GETFRAME(addr_ce);
int32 virtualIdx = CE_GETINST(int32);
@ -4569,12 +4601,15 @@ bool CeMachine::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
auto methodInstance = (BfMethodInstance*)valueType->mVirtualMethodTable[virtualIdx].mImplementingMethod;
auto callFunction = GetPreparedFunction(methodInstance);
result = callFunction;
if (needsFunctionIds)
*(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
else
*(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
}
break;
case CeOp_GetMethod_IFace:
{
auto& result = CE_GETFRAME(CeFunction*);
auto resultFrameIdx = CE_GETINST(int32);
auto valueAddr = CE_GETFRAME(addr_ce);
int32 ifaceId = CE_GETINST(int32);
int32 methodIdx = CE_GETINST(int32);
@ -4610,13 +4645,23 @@ bool CeMachine::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
}
auto callFunction = GetPreparedFunction(methodInstance);
result = callFunction;
if (needsFunctionIds)
*(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
else
*(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
}
break;
case CeOp_Call:
{
callCount++;
auto callFunction = CE_GETFRAME(CeFunction*);
CeFunction* callFunction;
if (needsFunctionIds)
{
int32 functionId = CE_GETFRAME(int32);
callFunction = mFunctionIdMap[functionId];
}
else
callFunction = CE_GETFRAME(CeFunction*);
bool handled = false;
if (!_CheckFunction(callFunction, handled))
@ -5247,7 +5292,7 @@ void CeMachine::PrepareFunction(CeFunction* ceFunction, CeBuilder* parentBuilder
ceBuilder.mCeFunction = ceFunction;
ceBuilder.Build();
/*if (!ceFunction->mCode.IsEmpty())
if (!ceFunction->mCode.IsEmpty())
{
CeDumpContext dumpCtx;
dumpCtx.mCeFunction = ceFunction;
@ -5257,15 +5302,15 @@ void CeMachine::PrepareFunction(CeFunction* ceFunction, CeBuilder* parentBuilder
dumpCtx.Dump();
OutputDebugStrF("Code for %s:\n%s\n", ceBuilder.mBeFunction->mName.c_str(), dumpCtx.mStr.c_str());
}*/
}
}
void CeMachine::CheckFunctions()
void CeMachine::MapFunctionId(CeFunction* ceFunction)
{
for (auto kv : mFunctions)
{
BF_ASSERT((((intptr)(void*)kv.mKey->mMethodDef) & 0xFF) != 0xDD);
}
if (mCeModule->mSystem->mPtrSize == 8)
return;
ceFunction->mId = ++mCurFunctionId;
mFunctionIdMap[ceFunction->mId] = ceFunction;
}
CeFunction* CeMachine::GetFunction(BfMethodInstance* methodInstance, BfIRValue func, bool& added)
@ -5276,8 +5321,6 @@ CeFunction* CeMachine::GetFunction(BfMethodInstance* methodInstance, BfIRValue f
return NULL;
}
CheckFunctions();
CeFunctionInfo** functionInfoPtr = NULL;
CeFunctionInfo* ceFunctionInfo = NULL;
CeFunction* ceFunction = NULL;
@ -5337,6 +5380,7 @@ CeFunction* CeMachine::GetFunction(BfMethodInstance* methodInstance, BfIRValue f
ceFunctionInfo->mMethodInstance = methodInstance;
ceFunctionInfo->mCeFunction = ceFunction;
MapFunctionId(ceFunction);
}

View file

@ -344,6 +344,7 @@ public:
Array<CeFunction*> mInnerFunctions;
String mGenError;
int mFrameSize;
int mId;
public:
CeFunction()
@ -355,6 +356,7 @@ public:
mMethodInstance = NULL;
mFailed = false;
mFrameSize = 0;
mId = -1;
}
~CeFunction();
@ -595,12 +597,14 @@ class CeMachine
public:
Dictionary<BfMethodInstance*, CeFunctionInfo*> mFunctions;
Dictionary<String, CeFunctionInfo*> mNamedFunctionMap;
Dictionary<int, CeFunction*> mFunctionIdMap; // Only used for 32-bit
BfCompiler* mCompiler;
BfModule* mCeModule;
int mRevision;
int mRevisionExecuteTime;
int mExecuteId;
int mCurFunctionId;
// These are only valid for the current execution
ContiguousHeap* mHeap;
@ -647,6 +651,7 @@ public:
bool Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* startFramePtr);
void PrepareFunction(CeFunction* methodInstance, CeBuilder* parentBuilder);
void MapFunctionId(CeFunction* ceFunction);
void CheckFunctions();
CeFunction* GetFunction(BfMethodInstance* methodInstance, BfIRValue func, bool& added);