From eec4ed45d12fb5dfc03202b78a362f7b2dd1699d Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Mon, 20 Nov 2023 22:53:12 +0100 Subject: [PATCH] Fix global array aliasing error --- include/fixmath.c | 30 +++++++++++++++++++++--------- oscar64/InterCode.cpp | 2 +- oscar64/InterCodeGenerator.cpp | 2 +- oscar64/NativeCodeGenerator.cpp | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/fixmath.c b/include/fixmath.c index 7503a8d..70cf017 100644 --- a/include/fixmath.c +++ b/include/fixmath.c @@ -630,19 +630,31 @@ unsigned long lmul16f16(unsigned long x, unsigned long y) __native long lmul16f16s(long x, long y) { - if (x < 0) + unsigned lox = x; + int hix = x >> 16; + unsigned loy = y; + int hiy = y >> 16; + + long r = (long)(hix * hiy) << 16; + + if (lox) { - if (y < 0) - return lmul16f16(-x, -y); - else - return -lmul16f16(-x, y); + r += lmul16u(lox, hiy); + if (hiy < 0) + r -= (unsigned long)lox << 16; } - else if (y < 0) + if (loy) { - return -lmul16f16(x, -y); + r += lmul16u(loy, hix); + if (hix < 0) + r -= (unsigned long)loy << 16; } - else - return lmul16f16(x, y); + if (lox && loy) + { + r += lmul16u(lox, loy) >> 16; + } + + return r; } __native unsigned long ldiv16f16(unsigned long x, unsigned long y) diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 51509e9..c5503f1 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -18594,7 +18594,7 @@ void InterCodeProcedure::Close(void) { GrowingTypeArray tstack(IT_NONE); - CheckFunc = !strcmp(mIdent->mString, "test"); + CheckFunc = !strcmp(mIdent->mString, "interpret_expression"); CheckCase = false; mEntryBlock = mBlocks[0]; diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 173c371..43afdbe 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -686,7 +686,7 @@ void InterCodeGenerator::InitGlobalVariable(InterCodeModule * mod, Declaration* var->mDeclaration = dec; mod->mGlobalVars.Push(var); - if (dec->mFlags & DTF_VAR_ALIASING) + if ((dec->mFlags & DTF_VAR_ALIASING) || (dec->mBase->mType == DT_TYPE_ARRAY && !(type->mFlags & DTF_CONST))) var->mAliased = true; dec->mVarIndex = var->mIndex; diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index f74c204..194253d 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -40431,6 +40431,20 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass else mIns[i + 4].mType = ASMIT_NOP; + progress = true; + } + else if ( + mIns[i + 0].mType == ASMIT_LDA && + mIns[i + 1].mType == ASMIT_STA && + mIns[i + 2].mType == ASMIT_CLC && + mIns[i + 3].mType == ASMIT_ADC && mIns[i + 3].mMode == ASMIM_IMMEDIATE && mIns[i + 3].mAddress == 1 && + mIns[i + 4].mType == ASMIT_STA && mIns[i + 4].SameEffectiveAddress(mIns[i + 0]) && !(mIns[i + 4].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C)) && + HasAsmInstructionMode(ASMIT_INC, mIns[i + 0].mMode)) + { + mIns[i + 4].mType = ASMIT_INC; + mIns[i + 2].mType = ASMIT_NOP; mIns[i + 3].mMode = ASMIM_IMPLIED; + mIns[i + 3].mType = ASMIT_NOP; mIns[i + 3].mMode = ASMIM_IMPLIED; + progress = true; } #endif