Fix float param to const propagation

This commit is contained in:
drmortalwombat 2024-04-12 15:50:46 +02:00
parent 0184a550c5
commit fbfd597306
5 changed files with 73 additions and 8 deletions

View File

@ -923,11 +923,73 @@ 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;
@ -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;

View File

@ -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];

View File

@ -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

View File

@ -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)

View File

@ -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)
{