From 932c7ec2225b46b0fdcc1fe6569a14c5f953c1a8 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Fri, 9 Aug 2024 16:18:38 +0200 Subject: [PATCH] Fix static stack in recursion Fix static stack allocation and placement for non recursive sub trees in a recursive call tree --- oscar64/InterCode.cpp | 26 ++++++++++++++++++++++++++ oscar64/InterCode.h | 1 + oscar64/Linker.cpp | 7 ++++++- oscar64/Linker.h | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 98dd625..83a625c 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -13663,6 +13663,23 @@ void InterCodeBasicBlock::ApplyStaticStack(InterOperand & iop, const GrowingVari } } +void InterCodeBasicBlock::CollectStaticStackDependencies(LinkerObject* lobj) +{ + if (!mVisited) + { + mVisited = true; + + for (int i = 0; i < mInstructions.Size(); i++) + { + if ((mInstructions[i]->mCode == IC_CALL || mInstructions[i]->mCode == IC_CALL_NATIVE) && mInstructions[i]->mSrc[0].mLinkerObject && mInstructions[i]->mSrc[0].mLinkerObject->mStackSection) + lobj->mStackSection->mSections.Push(mInstructions[i]->mSrc[0].mLinkerObject->mStackSection); + } + + if (mTrueJump) mTrueJump->CollectStaticStackDependencies(lobj); + if (mFalseJump) mFalseJump->CollectStaticStackDependencies(lobj); + } +} + void InterCodeBasicBlock::CollectStaticStack(LinkerObject* lobj, const GrowingVariableArray& localVars) { if (!mVisited) @@ -22498,6 +22515,15 @@ void InterCodeProcedure::Close(void) DisassembleDebug("Rebuilt traces"); #endif } + else if (!mInterruptCalled && mNativeProcedure) + { +// mLinkerObject->mFlags |= LOBJF_STATIC_STACK; + mLinkerObject->mStackSection = mModule->mLinker->AddSection(mIdent->Mangle("@stack"), LST_STATIC_STACK); + mLinkerObject->mStackSection->mSections.Push(mModule->mParamLinkerSection); + ResetVisited(); + mEntryBlock->CollectStaticStackDependencies(mLinkerObject); + } + #endif DisassembleDebug("PreLoopTemp"); diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index f90e3d4..91ecf2d 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -637,6 +637,7 @@ public: bool CheckStaticStack(void); void ApplyStaticStack(InterOperand& iop, const GrowingVariableArray& localVars); void CollectStaticStack(LinkerObject * lobj, const GrowingVariableArray& localVars); + void CollectStaticStackDependencies(LinkerObject* lobj); void PromoteStaticStackParams(LinkerObject* paramlobj); bool SameExitCode(const InterCodeBasicBlock* block) const; diff --git a/oscar64/Linker.cpp b/oscar64/Linker.cpp index 3def066..051531b 100644 --- a/oscar64/Linker.cpp +++ b/oscar64/Linker.cpp @@ -731,15 +731,20 @@ bool LinkerRegion::Allocate(Linker * linker, LinkerObject* lobj, bool merge, boo void LinkerRegion::PlaceStackSection(LinkerSection* stackSection, LinkerSection* section) { - if (!section->mEnd) + if (!section->mEnd && !(section->mFlags & LSECF_PLACED)) { + section->mFlags |= LSECF_PLACED; + int start = stackSection->mEnd; for (int i = 0; i < section->mSections.Size(); i++) { PlaceStackSection(stackSection, section->mSections[i]); if (section->mSections[i]->mStart < start) + { start = section->mSections[i]->mStart; + section->mStart = start; + } } section->mStart = start; diff --git a/oscar64/Linker.h b/oscar64/Linker.h index 9f06d49..3deb140 100644 --- a/oscar64/Linker.h +++ b/oscar64/Linker.h @@ -124,6 +124,7 @@ public: }; static const uint32 LSECF_PACKED = 0x00000001; +static const uint32 LSECF_PLACED = 0x00000002; class LinkerSection {