From 0826b6d49f30ad46bf6236e18988b01a3b03e817 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Thu, 6 Jan 2022 06:26:56 -0500 Subject: [PATCH] Support for "returnaddress" intrinsic --- BeefLibs/corlib/src/Internal.bf | 2 ++ IDEHelper/Backend/BeMCContext.cpp | 8 ++++++++ IDEHelper/Compiler/BfIRBuilder.h | 1 + IDEHelper/Compiler/BfIRCodeGen.cpp | 2 ++ 4 files changed, 13 insertions(+) diff --git a/BeefLibs/corlib/src/Internal.bf b/BeefLibs/corlib/src/Internal.bf index 882e73fa..f939eb45 100644 --- a/BeefLibs/corlib/src/Internal.bf +++ b/BeefLibs/corlib/src/Internal.bf @@ -92,6 +92,8 @@ namespace System public static extern void* StdMalloc(int size); [LinkName("free")] public static extern void StdFree(void* ptr); + [Intrinsic("returnaddress")] + public static extern void* GetReturnAddress(int32 level = 0); [CallingConvention(.Cdecl)] public static extern void* VirtualAlloc(int size, bool canExecute, bool canWrite); [CallingConvention(.Cdecl)] diff --git a/IDEHelper/Backend/BeMCContext.cpp b/IDEHelper/Backend/BeMCContext.cpp index 7ae5a633..b5184dcb 100644 --- a/IDEHelper/Backend/BeMCContext.cpp +++ b/IDEHelper/Backend/BeMCContext.cpp @@ -17697,6 +17697,14 @@ void BeMCContext::Generate(BeFunction* function) useAltArgs = true; } break; + case BfIRIntrinsic_ReturnAddress: + { + result = AllocVirtualReg(intrin->mReturnType); + CreateDefineVReg(result); + auto vregInfo = GetVRegInfo(result); + vregInfo->mFrameOffset = 0; + } + break; case BfIRIntrinsic_VAArg: { auto mcListPtr = GetOperand(castedInst->mArgs[0].mValue); diff --git a/IDEHelper/Compiler/BfIRBuilder.h b/IDEHelper/Compiler/BfIRBuilder.h index 45c892b9..e09a9b64 100644 --- a/IDEHelper/Compiler/BfIRBuilder.h +++ b/IDEHelper/Compiler/BfIRBuilder.h @@ -451,6 +451,7 @@ enum BfIRIntrinsic : uint8 BfIRIntrinsic_Or, BfIRIntrinsic_Pow, BfIRIntrinsic_PowI, + BfIRIntrinsic_ReturnAddress, BfIRIntrinsic_Round, BfIRIntrinsic_SAR, BfIRIntrinsic_SHL, diff --git a/IDEHelper/Compiler/BfIRCodeGen.cpp b/IDEHelper/Compiler/BfIRCodeGen.cpp index ea4ebd87..209dcf2c 100644 --- a/IDEHelper/Compiler/BfIRCodeGen.cpp +++ b/IDEHelper/Compiler/BfIRCodeGen.cpp @@ -181,6 +181,7 @@ static const BuiltinEntry gIntrinEntries[] = {"or"}, {"pow"}, {"powi"}, + {"returnaddress"}, {"round"}, {"sar"}, {"shl"}, @@ -2684,6 +2685,7 @@ void BfIRCodeGen::HandleNextCmd() { (llvm::Intrinsic::ID)-2, -1}, // or { llvm::Intrinsic::pow, 0, -1}, { llvm::Intrinsic::powi, 0, -1}, + { llvm::Intrinsic::returnaddress, -1}, { llvm::Intrinsic::round, 0, -1}, { (llvm::Intrinsic::ID)-2, -1}, // sar { (llvm::Intrinsic::ID)-2, -1}, // shl