From fbfd5973065e2b28e2c1b290df50c9adeec7b681 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Fri, 12 Apr 2024 15:50:46 +0200 Subject: [PATCH] Fix float param to const propagation --- oscar64/Declaration.cpp | 70 +++++++++++++++++++++++++++++++-- oscar64/InterCode.cpp | 3 +- oscar64/InterCodeGenerator.cpp | 2 +- oscar64/NativeCodeGenerator.cpp | 4 +- oscar64/Parser.cpp | 2 +- 5 files changed, 73 insertions(+), 8 deletions(-) diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index c7e2c4a..628e02b 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -923,16 +923,78 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio mLeft->mDecType->mType == DT_TYPE_ARRAY && mLeft->mDecType->mStride == 0 && mRight->mType == EX_CONSTANT && mRight->mDecValue->mType == DT_CONST_INTEGER) { + int offset = int(mDecType->mSize * mRight->mDecValue->mInteger); + + if (mDecType->IsSimpleType() && (mLeft->mDecValue->mFlags & DTF_CONST) && mLeft->mDecValue->mValue && mLeft->mDecValue->mValue->mType == EX_CONSTANT && mLeft->mDecValue->mValue->mDecValue->mType == DT_CONST_STRUCT) + { + Declaration* mdec = mLeft->mDecValue->mValue->mDecValue->mParams; + while (mdec && mdec->mOffset != offset) + mdec = mdec->mNext; + + if (mdec) + { + Expression* ex = new Expression(mLocation, EX_CONSTANT); + ex->mDecValue = mdec; + ex->mDecType = mDecType; + return ex; + } + } + Expression* ex = new Expression(mLocation, EX_VARIABLE); Declaration* dec = new Declaration(mLocation, DT_VARIABLE_REF); dec->mFlags = mLeft->mDecValue->mFlags; dec->mBase = mLeft->mDecValue; - dec->mOffset = int(mDecType->mSize * mRight->mDecValue->mInteger); + dec->mOffset = offset; dec->mSize = mDecType->mSize; ex->mDecValue = dec; ex->mDecType = mDecType; return ex; } + else if (mType == EX_INDEX && mLeft->mType == EX_VARIABLE && mLeft->mDecValue->mType == DT_VARIABLE_REF && (mLeft->mDecValue->mFlags & DTF_GLOBAL) && + mLeft->mDecType->mType == DT_TYPE_ARRAY && mLeft->mDecType->mStride == 0 && + mRight->mType == EX_CONSTANT && mRight->mDecValue->mType == DT_CONST_INTEGER) + { + int offset = mLeft->mDecValue->mOffset + int(mDecType->mSize * mRight->mDecValue->mInteger); + + if (mDecType->IsSimpleType() && (mLeft->mDecValue->mFlags & DTF_CONST) && mLeft->mDecValue->mBase->mValue && mLeft->mDecValue->mBase->mValue->mType == EX_CONSTANT && mLeft->mDecValue->mBase->mValue->mDecValue->mType == DT_CONST_STRUCT) + { + Declaration* mdec = mLeft->mDecValue->mBase->mValue->mDecValue->mParams; + + int coffset = offset; + while (mdec) + { + if (mdec->mType == DT_CONST_STRUCT) + { + if (mdec->mOffset > coffset || mdec->mOffset + mdec->mSize <= coffset) + mdec = mdec->mNext; + else + { + coffset -= mdec->mOffset; + mdec = mdec->mParams; + } + } + else if (mdec->mOffset != coffset) + mdec = mdec->mNext; + else + { + Expression* ex = new Expression(mLocation, EX_CONSTANT); + ex->mDecValue = mdec; + ex->mDecType = mDecType; + return ex; + } + } + } + + Expression* ex = new Expression(mLocation, EX_VARIABLE); + Declaration* dec = new Declaration(mLocation, DT_VARIABLE_REF); + dec->mFlags = mLeft->mDecValue->mFlags; + dec->mBase = mLeft->mDecValue->mBase; + dec->mOffset = offset; + dec->mSize = mDecType->mSize; + ex->mDecValue = dec; + ex->mDecType = mDecType; + return ex; + } else if (mType == EX_CALL && mLeft->mType == EX_CONSTANT && (mLeft->mDecValue->mFlags & DTF_INTRINSIC) && mRight->mType == EX_CONSTANT) { Declaration* decf = mLeft->mDecValue, * decp = mRight->mDecValue; @@ -1116,9 +1178,10 @@ Declaration* Declaration::ConstCast(Declaration* ntype) } else if (ntype->mType == DT_TYPE_INTEGER || ntype->mType == DT_TYPE_BOOL || ntype->mType == DT_TYPE_ENUM) { - if (mType == DT_TYPE_FLOAT) + if (mType == DT_CONST_FLOAT) { Declaration* pdec = this->Clone(); + pdec->mType = DT_CONST_INTEGER; pdec->mInteger = int64(mNumber); pdec->mBase = ntype; pdec->mSize = ntype->mSize; @@ -1134,7 +1197,7 @@ Declaration* Declaration::ConstCast(Declaration* ntype) } else if (ntype->mType == DT_TYPE_FLOAT) { - if (mType == DT_TYPE_FLOAT) + if (mType == DT_CONST_FLOAT) { Declaration* pdec = this->Clone(); pdec->mBase = ntype; @@ -1143,6 +1206,7 @@ Declaration* Declaration::ConstCast(Declaration* ntype) else { Declaration* pdec = this->Clone(); + pdec->mType = DT_CONST_FLOAT; pdec->mNumber = float(mInteger); pdec->mBase = ntype; pdec->mSize = ntype->mSize; diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 2e9e33d..7e532f9 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -11638,6 +11638,7 @@ bool InterCodeBasicBlock::LoadStoreForwarding(const GrowingInstructionPtrArray& { InterInstruction* tins = sins->Clone(); tins->mSrc[1] = ins->mSrc[1]; + tins->mSrc[1].mOperandSize = sins->mSrc[1].mOperandSize; tins->mSrc[1].mIntConst = sins->mSrc[1].mIntConst - ins->mSrc[0].mIntConst + ins->mSrc[1].mIntConst; n++; mInstructions.Insert(i + n, tins); @@ -20502,7 +20503,7 @@ void InterCodeProcedure::Close(void) { GrowingTypeArray tstack(IT_NONE); - CheckFunc = !strcmp(mIdent->mString, "main"); + CheckFunc = !strcmp(mIdent->mString, "addGameObjectCannon"); CheckCase = false; mEntryBlock = mBlocks[0]; diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index e3b47d3..6579543 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -5408,7 +5408,7 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod proc->mLinkerObject->mFullIdent = dec->FullIdent(); #if 0 - if (proc->mIdent && !strcmp(proc->mIdent->mString, "main")) + if (proc->mIdent && !strcmp(proc->mIdent->mString, "addGameObjectCircle")) exp->Dump(0); #endif #if 0 diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 067c559..75489a0 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -42727,7 +42727,7 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass else { mIns.Insert(i + 2, NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDY, ASMIM_ZERO_PAGE, ireg)); - mIns[i + 2].mLive = mIns[i + 3].mLive | LIVE_CPU_REG_Y | LIVE_MEM; + mIns[i + 2].mLive = mIns[i + 1].mLive | LIVE_CPU_REG_Y | LIVE_MEM; ypos = i + 2; mIns[i + 3].mAddress = breg; } @@ -42753,7 +42753,7 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass if (FindImmediateStore(i, mIns[i + 2].mAddress + 1, ains)) { mIns.Insert(i + 2, NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDX, ASMIM_ZERO_PAGE, mIns[i + 2].mAddress)); - mIns[i + 2].mLive = mIns[i + 3].mLive | LIVE_CPU_REG_X; + mIns[i + 2].mLive = mIns[i + 1].mLive | LIVE_CPU_REG_X; mIns[i + 3].mMode = ASMIM_ABSOLUTE_X; if (ains->mMode == ASMIM_IMMEDIATE) diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index 3bca8e1..4623aa3 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -6576,7 +6576,7 @@ Expression* Parser::ParsePostfixExpression(bool lhs) if (!nexp->mDecType) nexp->mDecType = TheVoidTypeDeclaration; - exp = nexp; + exp = nexp->ConstantFold(mErrors, mDataSection, mCompilationUnits->mLinker); } else if (mScanner->mToken == TK_OPEN_PARENTHESIS) {