From 1d87e77834d3159e5bfebafd00538311261c563a Mon Sep 17 00:00:00 2001 From: gdk Date: Sun, 10 Jul 2022 02:20:58 -0300 Subject: [PATCH] Increment call depth on function start rather than before call --- src/ARMeilleure/Instructions/InstEmitFlowHelper.cs | 7 ++++--- src/ARMeilleure/State/NativeContext.cs | 2 +- src/ARMeilleure/Translation/PTC/Ptc.cs | 2 +- src/ARMeilleure/Translation/Translator.cs | 1 + src/ARMeilleure/Translation/TranslatorStubs.cs | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/ARMeilleure/Instructions/InstEmitFlowHelper.cs b/src/ARMeilleure/Instructions/InstEmitFlowHelper.cs index f4f7fe4cb..ec74b051b 100644 --- a/src/ARMeilleure/Instructions/InstEmitFlowHelper.cs +++ b/src/ARMeilleure/Instructions/InstEmitFlowHelper.cs @@ -224,14 +224,14 @@ namespace ARMeilleure.Instructions if (isJump) { + DecreaseCallDepth(context, nativeContext); + context.Tailcall(hostAddress, nativeContext); } else { OpCode op = context.CurrOp; - EmitCallDepthCheckAndIncrement(context, nativeContext, guestAddress); - Operand returnAddress = context.Call(hostAddress, OperandType.I64, nativeContext); context.LoadFromContext(); @@ -253,13 +253,14 @@ namespace ARMeilleure.Instructions } } - private static void EmitCallDepthCheckAndIncrement(EmitterContext context, Operand nativeContext, Operand guestAddress) + public static void EmitCallDepthCheckAndIncrement(EmitterContext context, Operand guestAddress) { if (!Optimizations.EnableDeepCallRecursionProtection) { return; } + Operand nativeContext = context.LoadArgument(OperandType.I64, 0); Operand callDepthAddr = context.Add(nativeContext, Const((ulong)NativeContext.GetCallDepthOffset())); Operand currentCallDepth = context.Load(OperandType.I32, callDepthAddr); Operand lblDoCall = Label(); diff --git a/src/ARMeilleure/State/NativeContext.cs b/src/ARMeilleure/State/NativeContext.cs index d4dcaed15..98f77334d 100644 --- a/src/ARMeilleure/State/NativeContext.cs +++ b/src/ARMeilleure/State/NativeContext.cs @@ -183,7 +183,7 @@ namespace ARMeilleure.State public bool GetRunning() => GetStorage().Running != 0; public void SetRunning(bool value) => GetStorage().Running = value ? 1 : 0; - public void ResetCallDepth() => GetStorage().CallDepth = 1; + public void ResetCallDepth() => GetStorage().CallDepth = 0; public unsafe static int GetRegisterOffset(Register reg) { diff --git a/src/ARMeilleure/Translation/PTC/Ptc.cs b/src/ARMeilleure/Translation/PTC/Ptc.cs index 6f46e3f8c..248ca77ee 100644 --- a/src/ARMeilleure/Translation/PTC/Ptc.cs +++ b/src/ARMeilleure/Translation/PTC/Ptc.cs @@ -29,7 +29,7 @@ namespace ARMeilleure.Translation.PTC private const string OuterHeaderMagicString = "PTCohd\0\0"; private const string InnerHeaderMagicString = "PTCihd\0\0"; - private const uint InternalVersion = 3431; //! To be incremented manually for each change to the ARMeilleure project. + private const uint InternalVersion = 3430; //! To be incremented manually for each change to the ARMeilleure project. private const string ActualDir = "0"; private const string BackupDir = "1"; diff --git a/src/ARMeilleure/Translation/Translator.cs b/src/ARMeilleure/Translation/Translator.cs index 070b856e6..ad4eb7e78 100644 --- a/src/ARMeilleure/Translation/Translator.cs +++ b/src/ARMeilleure/Translation/Translator.cs @@ -271,6 +271,7 @@ namespace ARMeilleure.Translation Logger.StartPass(PassName.Translation); + InstEmitFlowHelper.EmitCallDepthCheckAndIncrement(context, Const(address)); EmitSynchronization(context); if (blocks[0].Address != address) diff --git a/src/ARMeilleure/Translation/TranslatorStubs.cs b/src/ARMeilleure/Translation/TranslatorStubs.cs index a37231f09..1a1ead2b7 100644 --- a/src/ARMeilleure/Translation/TranslatorStubs.cs +++ b/src/ARMeilleure/Translation/TranslatorStubs.cs @@ -271,7 +271,7 @@ namespace ARMeilleure.Translation if (Optimizations.EnableDeepCallRecursionProtection) { // Reset the call depth counter, since this is our first guest function call. - context.Store(callDepthAddress, Const(1)); + context.Store(callDepthAddress, Const(0)); } context.Store(dispatchAddress, guestAddress);