From 6c18ffd607c9da1428f89fd755d6d121b70ab14b Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Tue, 1 Feb 2022 16:35:09 -0500 Subject: [PATCH] Separated comptime mixin into Compiler.Mixin and Compiler.MixinRoot --- BeefLibs/corlib/src/Compiler.bf | 11 ++- IDEHelper/Compiler/CeMachine.cpp | 111 +++++++++++++++++-------------- IDEHelper/Compiler/CeMachine.h | 1 + 3 files changed, 71 insertions(+), 52 deletions(-) diff --git a/BeefLibs/corlib/src/Compiler.bf b/BeefLibs/corlib/src/Compiler.bf index 290c821a..3c0be7ea 100644 --- a/BeefLibs/corlib/src/Compiler.bf +++ b/BeefLibs/corlib/src/Compiler.bf @@ -267,7 +267,7 @@ namespace System static extern void Comptime_EmitAddInterface(int32 typeId, int32 ifaceTypeId); static extern void Comptime_EmitMethodEntry(int64 methodHandle, StringView text); static extern void Comptime_EmitMethodExit(int64 methodHandle, StringView text); - static extern void Comptime_EmitMixin(StringView text); + static extern void Comptime_EmitMixin(String text); [Comptime(OnlyFromComptime=true)] public static MethodBuilder CreateMethod(Type owner, StringView methodName, Type returnType, MethodFlags methodFlags) @@ -301,8 +301,15 @@ namespace System Comptime_EmitMethodExit(methodHandle.mNativeMethodInstance, text); } + [Comptime(ConstEval=true)] + public static void Mixin(String text) + { + if (Compiler.IsComptime) + Comptime_EmitMixin(text); + } + [Comptime] - public static void Mixin(StringView text) + public static void MixinRoot(String text) { if (Compiler.IsComptime) Comptime_EmitMixin(text); diff --git a/IDEHelper/Compiler/CeMachine.cpp b/IDEHelper/Compiler/CeMachine.cpp index 7070b9c8..04e1145a 100644 --- a/IDEHelper/Compiler/CeMachine.cpp +++ b/IDEHelper/Compiler/CeMachine.cpp @@ -3381,6 +3381,57 @@ bool CeContext::CheckMemory(addr_ce addr, int32 size) return true; } +bool CeContext::GetStringFromAddr(addr_ce strInstAddr, StringImpl& str) +{ + if (!CheckMemory(strInstAddr, 0)) + return false; + + BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data); + + auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize; + auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset; + auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset; + auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset; + + uint8* strInst = (uint8*)(strInstAddr + mMemory.mVals); + int32 lenVal = *(int32*)(strInst + lenOffset); + + char* charPtr = NULL; + + if (lenByteCount == 4) + { + int32 allocSizeVal = *(int32*)(strInst + allocSizeOffset); + if ((allocSizeVal & 0x40000000) != 0) + { + int32 ptrVal = *(int32*)(strInst + ptrOffset); + charPtr = (char*)(ptrVal + mMemory.mVals); + } + else + { + charPtr = (char*)(strInst + ptrOffset); + } + } + else + { + int64 allocSizeVal = *(int64*)(strInst + allocSizeOffset); + if ((allocSizeVal & 0x4000000000000000LL) != 0) + { + int32 ptrVal = *(int32*)(strInst + ptrOffset); + charPtr = (char*)(ptrVal + mMemory.mVals); + } + else + { + charPtr = (char*)(strInst + ptrOffset); + } + } + + int32 ptrVal = *(int32*)(strInst + ptrOffset); + + if (charPtr != NULL) + str.Insert(str.length(), charPtr, lenVal); + return true; +} + bool CeContext::GetStringFromStringView(addr_ce addr, StringImpl& str) { int ptrSize = mCeMachine->mCeModule->mSystem->mPtrSize; @@ -4748,52 +4799,9 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* else if (checkFunction->mFunctionKind == CeFunctionKind_FatalError) { int32 strInstAddr = *(int32*)((uint8*)stackPtr + 0); - CE_CHECKADDR(strInstAddr, 0); - - BfTypeInstance* stringTypeInst = (BfTypeInstance*)ceModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data); - - auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize; - auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset; - auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset; - auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset; - - uint8* strInst = (uint8*)(strInstAddr + memStart); - int32 lenVal = *(int32*)(strInst + lenOffset); - - char* charPtr = NULL; - - if (lenByteCount == 4) - { - int32 allocSizeVal = *(int32*)(strInst + allocSizeOffset); - if ((allocSizeVal & 0x40000000) != 0) - { - int32 ptrVal = *(int32*)(strInst + ptrOffset); - charPtr = (char*)(ptrVal + memStart); - } - else - { - charPtr = (char*)(strInst + ptrOffset); - } - } - else - { - int64 allocSizeVal = *(int64*)(strInst + allocSizeOffset); - if ((allocSizeVal & 0x4000000000000000LL) != 0) - { - int32 ptrVal = *(int32*)(strInst + ptrOffset); - charPtr = (char*)(ptrVal + memStart); - } - else - { - charPtr = (char*)(strInst + ptrOffset); - } - } - - int32 ptrVal = *(int32*)(strInst + ptrOffset); String error = "Fatal Error: "; - if (charPtr != NULL) - error.Insert(error.length(), charPtr, lenVal); + GetStringFromAddr(strInstAddr, error); _Fail(error); return false; @@ -4878,7 +4886,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* } SetAndRestoreValue prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance); - SetAndRestoreValue prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance); + SetAndRestoreValue prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance); CeSetAddrVal(stackPtr + 0, GetString(mCeMachine->mCeModule->TypeToString(type)), ptrSize); _FixVariables(); } @@ -5065,13 +5073,16 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* return false; } } - else if (checkFunction->mFunctionKind == CeFunctionKind_EmitMixin) - { - addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr); + else if (checkFunction->mFunctionKind == CeFunctionKind_EmitMixin) + { + SetAndRestoreValue prevMethodInstance(mCurModule->mCurMethodInstance, mCallerMethodInstance); + SetAndRestoreValue prevTypeInstance(mCurModule->mCurTypeInstance, mCallerTypeInstance); + + int32 strInstAddr = *(int32*)((uint8*)stackPtr + 0); String emitStr; - if (!GetStringFromStringView(strViewPtr, emitStr)) + if (!GetStringFromAddr(strInstAddr, emitStr)) { - _Fail("Invalid StringView"); + _Fail("Invalid String"); return false; } diff --git a/IDEHelper/Compiler/CeMachine.h b/IDEHelper/Compiler/CeMachine.h index 9b961ae0..c4c3e63b 100644 --- a/IDEHelper/Compiler/CeMachine.h +++ b/IDEHelper/Compiler/CeMachine.h @@ -901,6 +901,7 @@ public: BfType* GetBfType(int typeId); void PrepareConstStructEntry(CeConstStructData& constStructData); bool CheckMemory(addr_ce addr, int32 size); + bool GetStringFromAddr(addr_ce strInstAddr, StringImpl& str); bool GetStringFromStringView(addr_ce addr, StringImpl& str); bool GetCustomAttribute(BfCustomAttributes* customAttributes, int attributeTypeId, addr_ce resultAddr);