From a6f9c733a14a17a3e6bd5688a99574b6139cb7e3 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Fri, 1 Nov 2024 20:32:54 +0100 Subject: [PATCH] Fix drop of reading from volatile with discarding result --- include/crt.c | 62 +++++++++++++++------------------ oscar64/Declaration.cpp | 11 ++++++ oscar64/Declaration.h | 2 ++ oscar64/GlobalOptimizer.cpp | 2 +- oscar64/InterCode.cpp | 2 +- oscar64/InterCodeGenerator.cpp | 1 + oscar64/NativeCodeGenerator.cpp | 11 ++++-- oscar64/Parser.cpp | 6 ++-- 8 files changed, 56 insertions(+), 41 deletions(-) diff --git a/include/crt.c b/include/crt.c index 08a6535..d0111c9 100644 --- a/include/crt.c +++ b/include/crt.c @@ -236,46 +236,40 @@ w0: // Clear BSS Segment #ifndef NOBSSCLEAR - lda #BSSStart - sta ip + 1 + ldx #>BSSStart + ldy #BSSEnd - sbc #>BSSStart - beq w1 - tax lda #0 - ldy #0 -l1: sta (ip), y + sta ip +l3: + stx ip + 1 + cpx #>BSSEnd + beq w2 +l1: + sta (ip), y iny bne l1 - inc ip + 1 - dex - bne l1 -w1: - sec - lda #StackEnd - 2 diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index 9581320..00a816a 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -387,6 +387,17 @@ bool Expression::IsRValue(void) const return true; } +bool Expression::IsVolatile(void) const +{ + if (mDecType->mFlags & DTF_VOLATILE) + return true; + if (mLeft && mLeft->IsVolatile()) + return true; + if (mRight && mRight->IsVolatile()) + return true; + return false; +} + bool Expression::IsConstRef(void) const { if (mDecType->mType == DT_TYPE_RVALUEREF || mDecType->mType == DT_TYPE_REFERENCE) diff --git a/oscar64/Declaration.h b/oscar64/Declaration.h index 5a2bb22..2250d53 100644 --- a/oscar64/Declaration.h +++ b/oscar64/Declaration.h @@ -126,6 +126,7 @@ static const uint64 DTF_VAR_ALIASING = (1ULL << 48); static const uint64 DTF_FPARAM_UNUSED = (1ULL << 49); static const uint64 DTF_DEPRECATED = (1ULL << 50); static const uint64 DTF_FUNC_NO_RETURN = (1ULL << 51); +static const uint64 DTF_PLACED = (1ULL << 52); @@ -267,6 +268,7 @@ public: bool IsRValue(void) const; bool IsLValue(void) const; bool IsConstRef(void) const; + bool IsVolatile(void) const; void Dump(int ident) const; }; diff --git a/oscar64/GlobalOptimizer.cpp b/oscar64/GlobalOptimizer.cpp index 169bb23..062a63d 100644 --- a/oscar64/GlobalOptimizer.cpp +++ b/oscar64/GlobalOptimizer.cpp @@ -189,7 +189,7 @@ bool GlobalOptimizer::CheckUnusedLocals(Expression*& exp) if (vexp->mType == EX_VARIABLE) { Declaration* vdec = vexp->mDecValue; - if (vdec->mType == DT_VARIABLE && !(vdec->mFlags & (DTF_GLOBAL | DTF_STATIC)) && !(vdec->mOptFlags & OPTF_VAR_USED)) + if (vdec->mType == DT_VARIABLE && !(vdec->mFlags & (DTF_GLOBAL | DTF_STATIC)) && !(vdec->mOptFlags & OPTF_VAR_USED) && !exp->mRight->IsVolatile()) { exp = exp->mRight; return true; diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 5cc7848..c80477a 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -22913,7 +22913,7 @@ void InterCodeProcedure::Close(void) { GrowingTypeArray tstack(IT_NONE); - CheckFunc = !strcmp(mIdent->mString, "__computeCharsetSrc__"); + CheckFunc = !strcmp(mIdent->mString, "main"); CheckCase = false; mEntryBlock = mBlocks[0]; diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index cbd078d..59e7be8 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -769,6 +769,7 @@ void InterCodeGenerator::InitGlobalVariable(InterCodeModule * mod, Declaration* { dec->mLinkerObject->mType = LOT_INLAY; dec->mInlayRegion->mInlayObject = dec->mLinkerObject; + dec->mLinkerObject->mFlags &= ~LOBJF_CONST; } uint8* d = var->mLinkerObject->AddSpace(var->mSize); diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 2912f47..8781e14 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -667,6 +667,12 @@ bool NativeCodeInstruction::IsUsedResultInstructions(NumberSet& requiredTemps) // check side effects + if (mFlags & NCIF_VOLATILE) + { + if (mMode != ASMIM_IMPLIED && mMode != ASMIM_ZERO_PAGE) + used = true; + } + switch (mType) { case ASMIT_STA: @@ -43871,7 +43877,7 @@ bool NativeCodeBasicBlock::PeepHoleOptimizerIterate1(int i, int pass) bool NativeCodeBasicBlock::PeepHoleOptimizerIterate2(int i, int pass) { - if (mIns[i].mType == ASMIT_LDA && mIns[i + 1].mType == ASMIT_LDA) + if (mIns[i].mType == ASMIT_LDA && mIns[i + 1].mType == ASMIT_LDA && !(mIns[i].mFlags & NCIF_VOLATILE)) { mIns[i].mType = ASMIT_NOP; mIns[i].mMode = ASMIM_IMPLIED;; return true; @@ -51688,7 +51694,7 @@ void NativeCodeProcedure::Compile(InterCodeProcedure* proc) mInterProc = proc; mInterProc->mLinkerObject->mNativeProc = this; - CheckFunc = !strcmp(mInterProc->mIdent->mString, "__prepareScreen__"); + CheckFunc = !strcmp(mInterProc->mIdent->mString, "cia_init"); int nblocks = proc->mBlocks.Size(); tblocks = new NativeCodeBasicBlock * [nblocks]; @@ -52664,7 +52670,6 @@ void NativeCodeProcedure::Optimize(void) if (mEntryBlock->PeepHoleOptimizer(step)) changed = true; - #endif if (step == 2) { diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index c865909..a90840e 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -5735,7 +5735,8 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex { if (ndec->mBase->mType == DT_TYPE_FUNCTION) { - ndec->mSection = mCodeSection; + if (!(ndec->mFlags & DTF_PLACED)) + ndec->mSection = mCodeSection; if ((ndec->mFlags & DTF_DEFINED) && !(ndec->mFlags & DTF_REQUEST_INLINE)) { @@ -13092,10 +13093,11 @@ void Parser::ParsePragma(void) if (mScanner->mToken == TK_IDENT) { Declaration* dec = mGlobals->Lookup(mScanner->mTokenIdent); - if (dec && dec->mType == DT_VARIABLE && (dec->mFlags & DTF_GLOBAL)) + if (dec && (dec->mType == DT_VARIABLE && (dec->mFlags & DTF_GLOBAL) || dec->mType == DT_CONST_FUNCTION)) { mScanner->NextToken(); + dec->mFlags |= DTF_PLACED; dec->mSection = lsec; if (dec->mLinkerObject) dec->mLinkerObject->MoveToSection(lsec);