Fix drop of reading from volatile with discarding result

This commit is contained in:
drmortalwombat 2024-11-01 20:32:54 +01:00
parent 3c129ff4e5
commit a6f9c733a1
8 changed files with 56 additions and 41 deletions

View File

@ -236,46 +236,40 @@ w0:
// Clear BSS Segment
#ifndef NOBSSCLEAR
lda #<BSSStart
sta ip
lda #>BSSStart
sta ip + 1
ldx #>BSSStart
ldy #<BSSStart
sec
lda #>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 #<BSSEnd
sbc #<BSSStart
beq w2
tay
lda #0
l2: dey
sta (ip), y
bne l2
w2:
#endif
#ifndef NOZPCLEAR
ldx #<ZeroStart
cpx #<ZeroEnd
beq w3
l3: sta $00, x
inx
cpx #<ZeroEnd
bne l3
w3:
bne l3
l2:
sta (ip), y
iny
w2:
cpy #<BSSEnd
bne l2
#endif
#ifndef NOZPCLEAR
lda #0
ldx #<ZeroStart
bne w3
l4: sta $00, x
inx
w3:
cpx #<ZeroEnd
bne l4
#endif
lda #<StackEnd - 2
sta sp
lda #>StackEnd - 2

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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