Fix crash with extern object pointer arithmetic

This commit is contained in:
drmortalwombat 2024-06-23 17:05:46 +02:00
parent 1598360b65
commit 9678814654
4 changed files with 61 additions and 18 deletions

View File

@ -205,6 +205,21 @@ void vspr_move(char sp, int xpos, int ypos)
vspriteXHigh[sp] = (char)(xpos >> 8); vspriteXHigh[sp] = (char)(xpos >> 8);
} }
void vspr_movex(char sp, int xpos)
{
vspriteXLow[sp] = (char)xpos;
vspriteXHigh[sp] = (char)(xpos >> 8);
}
void vspr_movey(char sp, int ypos)
{
char yp = (char)ypos;
if (ypos & 0xff00)
yp = 0xff;
vspriteYLow[sp] = yp;
}
void vspr_image(char sp, char image) void vspr_image(char sp, char image)
{ {
vspriteImage[sp] = image; vspriteImage[sp] = image;

View File

@ -78,6 +78,11 @@ void vspr_set(char sp, int xpos, int ypos, char image, char color);
inline void vspr_move(char sp, int xpos, int ypos); inline void vspr_move(char sp, int xpos, int ypos);
inline void vspr_movex(char sp, int xpos);
inline void vspr_movey(char sp, int ypos);
// change the image of a virtual sprite // change the image of a virtual sprite
inline void vspr_image(char sp, char image); inline void vspr_image(char sp, char image);

View File

@ -965,7 +965,7 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio
return ex; return ex;
} }
else if (mType == EX_BINARY && mToken == TK_ADD && mLeft->mType == EX_VARIABLE && mLeft->mDecValue->mType == DT_VARIABLE && (mLeft->mDecValue->mFlags & DTF_CONST) && else if (mType == EX_BINARY && mToken == TK_ADD && mLeft->mType == EX_VARIABLE && mLeft->mDecValue->mType == DT_VARIABLE && (mLeft->mDecValue->mFlags & DTF_CONST) &&
mLeft->mDecValue->mValue->mType == EX_CONSTANT && mLeft->mDecValue && mRight->mDecValue && mLeft->mDecValue->mValue->mType == EX_CONSTANT &&
mLeft->mDecType->mType == DT_TYPE_POINTER && mRight->mType == EX_CONSTANT && mRight->mDecValue->mType == DT_CONST_INTEGER) mLeft->mDecType->mType == DT_TYPE_POINTER && mRight->mType == EX_CONSTANT && mRight->mDecValue->mType == DT_CONST_INTEGER)
{ {
mLeft = mLeft->mDecValue->mValue; mLeft = mLeft->mDecValue->mValue;

View File

@ -15991,6 +15991,15 @@ bool NativeCodeBasicBlock::GlobalLoadStoreForwarding(const NativeCodeInstruction
for (int i = 0; i < mIns.Size(); i++) for (int i = 0; i < mIns.Size(); i++)
{ {
NativeCodeInstruction& ins(mIns[i]); NativeCodeInstruction& ins(mIns[i]);
if (mALSIns.mType != ASMIT_INV && ins.mType == ASMIT_LDA && i + 1 < mIns.Size() &&
mIns[i + 1].SameEffectiveAddress(mALSIns) && mIns[i + 1].IsCommutative())
{
ins.mType = mIns[i + 1].mType;
mIns[i + 1].mType = ASMIT_NOP;
mIns[i + 1].mMode = ASMIM_IMPLIED;
changed = true;
}
if (ins.mType == ASMIT_STA) if (ins.mType == ASMIT_STA)
{ {
if (mALSIns.mType != ASMIT_INV && ins.SameEffectiveAddress(mALSIns) && !(ins.mFlags & NCIF_VOLATILE)) if (mALSIns.mType != ASMIT_INV && ins.SameEffectiveAddress(mALSIns) && !(ins.mFlags & NCIF_VOLATILE))
@ -16001,7 +16010,7 @@ bool NativeCodeBasicBlock::GlobalLoadStoreForwarding(const NativeCodeInstruction
} }
else else
{ {
if ((ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE)) if ((ins.mMode == ASMIM_ZERO_PAGE || ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE))
mALSIns = ins; mALSIns = ins;
if (mXLSIns.mType != ASMIT_INV && ins.MayBeSameAddress(mXLSIns)) if (mXLSIns.mType != ASMIT_INV && ins.MayBeSameAddress(mXLSIns))
mXLSIns.mType = ASMIT_INV; mXLSIns.mType = ASMIT_INV;
@ -16018,7 +16027,7 @@ bool NativeCodeBasicBlock::GlobalLoadStoreForwarding(const NativeCodeInstruction
ins.mAddress = 0; ins.mAddress = 0;
changed = true; changed = true;
} }
else if ((ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE)) else if ((ins.mMode == ASMIM_ZERO_PAGE || ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE))
mALSIns = ins; mALSIns = ins;
else else
mALSIns.mType = ASMIT_INV; mALSIns.mType = ASMIT_INV;
@ -16033,7 +16042,7 @@ bool NativeCodeBasicBlock::GlobalLoadStoreForwarding(const NativeCodeInstruction
} }
else else
{ {
if ((ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE)) if ((ins.mMode == ASMIM_ZERO_PAGE || ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE))
mXLSIns = ins; mXLSIns = ins;
if (mALSIns.mType != ASMIT_INV && ins.MayBeSameAddress(mALSIns)) if (mALSIns.mType != ASMIT_INV && ins.MayBeSameAddress(mALSIns))
mALSIns.mType = ASMIT_INV; mALSIns.mType = ASMIT_INV;
@ -16052,7 +16061,7 @@ bool NativeCodeBasicBlock::GlobalLoadStoreForwarding(const NativeCodeInstruction
} }
else else
{ {
if ((ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE)) if ((ins.mMode == ASMIM_ZERO_PAGE || ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_Y) && !(ins.mFlags & NCIF_VOLATILE))
mXLSIns = ins; mXLSIns = ins;
else else
mXLSIns.mType = ASMIT_INV; mXLSIns.mType = ASMIT_INV;
@ -16072,7 +16081,7 @@ bool NativeCodeBasicBlock::GlobalLoadStoreForwarding(const NativeCodeInstruction
} }
else else
{ {
if ((ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X) && !(ins.mFlags & NCIF_VOLATILE)) if ((ins.mMode == ASMIM_ZERO_PAGE || ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X) && !(ins.mFlags & NCIF_VOLATILE))
mYLSIns = ins; mYLSIns = ins;
if (mALSIns.mType != ASMIT_INV && ins.MayBeSameAddress(mALSIns)) if (mALSIns.mType != ASMIT_INV && ins.MayBeSameAddress(mALSIns))
mALSIns.mType = ASMIT_INV; mALSIns.mType = ASMIT_INV;
@ -16091,7 +16100,7 @@ bool NativeCodeBasicBlock::GlobalLoadStoreForwarding(const NativeCodeInstruction
} }
else else
{ {
if ((ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X) && !(ins.mFlags & NCIF_VOLATILE)) if ((ins.mMode == ASMIM_ZERO_PAGE || ins.mMode == ASMIM_ABSOLUTE || ins.mMode == ASMIM_ABSOLUTE_X) && !(ins.mFlags & NCIF_VOLATILE))
mYLSIns = ins; mYLSIns = ins;
else else
mYLSIns.mType = ASMIT_INV; mYLSIns.mType = ASMIT_INV;
@ -23789,6 +23798,8 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool
i++; i++;
if (i == mTrueJump->mEntryBlocks.Size()) if (i == mTrueJump->mEntryBlocks.Size())
{ {
bool zlive = mTrueJump->mIns[0].mLive & LIVE_CPU_REG_Z;
for (int j = 0; j < mTrueJump->mEntryBlocks.Size(); j++) for (int j = 0; j < mTrueJump->mEntryBlocks.Size(); j++)
{ {
if (mTrueJump->mEntryBlocks[j] != this) if (mTrueJump->mEntryBlocks[j] != this)
@ -23796,24 +23807,36 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool
mTrueJump->mEntryBlocks[j]->mIns.Push(mTrueJump->mIns[0]); mTrueJump->mEntryBlocks[j]->mIns.Push(mTrueJump->mIns[0]);
mTrueJump->mEntryBlocks[j]->mExitRequiredRegs += CPU_REG_A; mTrueJump->mEntryBlocks[j]->mExitRequiredRegs += CPU_REG_A;
} }
mTrueJump->mEntryBlocks[j]->mExitRequiredRegs += CPU_REG_Z;
int k = mTrueJump->mEntryBlocks[j]->mIns.Size(); if (zlive)
while (k > 0)
{ {
k--; mTrueJump->mEntryBlocks[j]->mExitRequiredRegs += CPU_REG_Z;
mTrueJump->mEntryBlocks[j]->mIns[k].mLive |= LIVE_CPU_REG_Z; int k = mTrueJump->mEntryBlocks[j]->mIns.Size();
if (mTrueJump->mEntryBlocks[j]->mIns[k].ChangesZFlag()) while (k > 0)
break; {
k--;
if (mTrueJump->mEntryBlocks[j]->mIns[k].ChangesZFlag())
{
if (mTrueJump->mEntryBlocks[j]->mIns[k].ChangesAccuAndFlag())
mTrueJump->mEntryBlocks[j]->mIns[k].mLive |= LIVE_CPU_REG_Z;
else
mTrueJump->mEntryBlocks[j]->mIns.Insert(k + 1, NativeCodeInstruction(mTrueJump->mIns[0].mIns, ASMIT_ORA, ASMIM_IMMEDIATE, 0));
break;
}
mTrueJump->mEntryBlocks[j]->mIns[k].mLive |= LIVE_CPU_REG_Z;
}
} }
} }
mExitRequiredRegs += CPU_REG_A; mExitRequiredRegs += CPU_REG_A;
mExitRequiredRegs += CPU_REG_Z;
if (zlive)
mExitRequiredRegs += CPU_REG_Z;
mTrueJump->mIns.Remove(0); mTrueJump->mIns.Remove(0);
mTrueJump->mEntryRequiredRegs += CPU_REG_A; mTrueJump->mEntryRequiredRegs += CPU_REG_A;
mTrueJump->mEntryRequiredRegs += CPU_REG_Z;
if (mTrueJump->mIns[0].mLive & LIVE_CPU_REG_Z)
mTrueJump->mEntryRequiredRegs += CPU_REG_Z;
changed = true; changed = true;
@ -48813,7 +48836,7 @@ void NativeCodeProcedure::Compile(InterCodeProcedure* proc)
mInterProc = proc; mInterProc = proc;
mInterProc->mLinkerObject->mNativeProc = this; mInterProc->mLinkerObject->mNativeProc = this;
CheckFunc = !strcmp(mInterProc->mIdent->mString, "enemies_move"); CheckFunc = !strcmp(mInterProc->mIdent->mString, "main");
int nblocks = proc->mBlocks.Size(); int nblocks = proc->mBlocks.Size();
tblocks = new NativeCodeBasicBlock * [nblocks]; tblocks = new NativeCodeBasicBlock * [nblocks];