From 3520d2a2bd4f6b8b9e7a6f04efc827768e134c05 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 17 Oct 2021 10:40:33 +0200 Subject: [PATCH] Fix bugs found by llvm lint --- autotest/arraytest.c | 48 ++++++++++++++++++- include/crt.c | 6 +-- include/stdio.c | 20 ++++---- oscar64/Array.h | 6 ++- oscar64/ByteCodeGenerator.cpp | 2 +- oscar64/InterCodeGenerator.cpp | 4 +- oscar64/NativeCodeGenerator.cpp | 81 +++++++++++++++++++++++++++++++-- oscar64/NativeCodeGenerator.h | 2 + 8 files changed, 146 insertions(+), 23 deletions(-) diff --git a/autotest/arraytest.c b/autotest/arraytest.c index 64a7c75..714e044 100644 --- a/autotest/arraytest.c +++ b/autotest/arraytest.c @@ -11,13 +11,59 @@ int sum(int * a, int s) return sum; } +void copy(int * d, const int * s, int n) +{ + for(int i=0; idone + lda #>bdone sta (sp), y jmp startup.pexec -done: nop +bdone: nop pla pla pla diff --git a/include/stdio.c b/include/stdio.c index d674f28..15ddffc 100644 --- a/include/stdio.c +++ b/include/stdio.c @@ -81,35 +81,35 @@ char getchar(void) void puts(const char * str) { __asm { - loop: + ploop: ldy #0 lda (str), y - beq done + beq pdone jsr putpch inc str - bne loop + bne ploop inc str + 1 - bne loop - done: + bne ploop + pdone: } } char * gets(char * str) { __asm { - loop: + gloop: jsr getpch ldy #0 cmp #10 - beq done + beq gdone sta (str), y inc str - bne loop + bne gloop inc str + 1 - bne loop - done: + bne gloop + gdone: lda #0 sta (str), y } diff --git a/oscar64/Array.h b/oscar64/Array.h index 3c7df04..9132ac8 100644 --- a/oscar64/Array.h +++ b/oscar64/Array.h @@ -170,7 +170,11 @@ protected: range = range * 2; a2 = new T[range]; - for (i = 0; i < size; i++) a2[i] = array[i]; + if (to > size) + for (i = 0; i < size; i++) a2[i] = array[i]; + else + for (i = 0; i < to; i++) a2[i] = array[i]; + delete[] array; array = a2; } diff --git a/oscar64/ByteCodeGenerator.cpp b/oscar64/ByteCodeGenerator.cpp index f108ad2..212a596 100644 --- a/oscar64/ByteCodeGenerator.cpp +++ b/oscar64/ByteCodeGenerator.cpp @@ -935,7 +935,7 @@ void ByteCodeBasicBlock::StoreDirectValue(InterCodeProcedure* proc, const InterI ByteCodeInstruction bins(BC_STORE_ADDR_32); bins.mRegister = BC_REG_ACCU; - bins.mValue = ins->mSrc[1].mIntConst; + bins.mValue = index; mIns.Push(bins); } } diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index f3f8857..6bb4377 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -1495,7 +1495,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vl.mType->mFlags & DTF_CONST) mErrors->Error(exp->mLocation, EERR_CONST_ASSIGN, "Cannot change const value"); - InterInstruction * cins = new InterInstruction(), * ains = new InterInstruction(), * rins = new InterInstruction(), * sins = new InterInstruction(); + InterInstruction * cins = new InterInstruction(), * ains = new InterInstruction(), * sins = new InterInstruction(); ExValue vdl = Dereference(proc, block, vl); @@ -1555,7 +1555,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vl.mType->mFlags & DTF_CONST) mErrors->Error(exp->mLocation, EERR_CONST_ASSIGN, "Cannot change const value"); - InterInstruction * cins = new InterInstruction(), * ains = new InterInstruction(), * rins = new InterInstruction(), * sins = new InterInstruction(); + InterInstruction * cins = new InterInstruction(), * ains = new InterInstruction(), * sins = new InterInstruction(); ExValue vdl = Dereference(proc, block, vl); diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 4792326..8e2f695 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -567,6 +567,14 @@ bool NativeCodeInstruction::RequiresAccu(void) const } } +bool NativeCodeInstruction::UsesAccu(void) const +{ + if (ChangesAccu()) + return true; + + return mType == ASMIT_STA || mType == ASMIT_CMP || mType == ASMIT_TAX || mType == ASMIT_TAY; +} + bool NativeCodeInstruction::ChangesAccu(void) const { if (mMode == ASMIM_IMPLIED) @@ -595,6 +603,11 @@ bool NativeCodeInstruction::ChangesAddress(void) const return false; } +bool NativeCodeInstruction::IsShift(void) const +{ + return mType == ASMIT_ASL || mType == ASMIT_LSR || mType == ASMIT_ROL || mType == ASMIT_ROR; +} + bool NativeCodeInstruction::IsCommutative(void) const { return mType == ASMIT_ADC || mType == ASMIT_AND || mType == ASMIT_ORA || mType == ASMIT_EOR; @@ -673,6 +686,14 @@ bool NativeCodeInstruction::ApplySimulation(const NativeRegisterDataSet& data) mAddress = data.mRegs[mAddress].mValue; return true; } + else if (mMode == ASMIM_ZERO_PAGE && data.mRegs[mAddress].mMode == NRDM_IMMEDIATE_ADDRESS) + { + mMode = ASMIM_IMMEDIATE_ADDRESS; + mLinkerObject = data.mRegs[mAddress].mLinkerObject; + mFlags = data.mRegs[mAddress].mFlags; + mAddress = data.mRegs[mAddress].mValue; + return true; + } break; } @@ -1185,6 +1206,14 @@ void NativeCodeInstruction::Simulate(NativeRegisterDataSet& data) data.mRegs[CPU_REG_Z].mValue = mAddress; data.mRegs[CPU_REG_Z].mMode = NRDM_IMMEDIATE; } + else if (mMode == ASMIM_IMMEDIATE_ADDRESS) + { + data.mRegs[CPU_REG_A].mValue = mAddress; + data.mRegs[CPU_REG_A].mLinkerObject = mLinkerObject; + data.mRegs[CPU_REG_A].mFlags = mFlags; + data.mRegs[CPU_REG_A].mMode = NRDM_IMMEDIATE_ADDRESS; + data.mRegs[CPU_REG_Z].Reset(); + } else { data.mRegs[CPU_REG_A].Reset(); @@ -1257,10 +1286,9 @@ void NativeCodeInstruction::Simulate(NativeRegisterDataSet& data) case ASMIT_STA: if (reg >= 0) { - if (data.mRegs[CPU_REG_A].mMode == NRDM_IMMEDIATE) + if (data.mRegs[CPU_REG_A].mMode == NRDM_IMMEDIATE || data.mRegs[CPU_REG_A].mMode == NRDM_IMMEDIATE_ADDRESS) { - data.mRegs[reg].mValue = data.mRegs[CPU_REG_A].mValue; - data.mRegs[reg].mMode = NRDM_IMMEDIATE; + data.mRegs[reg] = data.mRegs[CPU_REG_A]; } else { @@ -1393,6 +1421,7 @@ bool NativeCodeInstruction::ValueForwarding(NativeRegisterDataSet& data) data.mRegs[CPU_REG_A].mValue = mAddress; data.mRegs[CPU_REG_A].mLinkerObject = mLinkerObject; data.mRegs[CPU_REG_A].mFlags = mFlags; + data.mRegs[CPU_REG_Z].Reset(); } else { @@ -6625,13 +6654,30 @@ void NativeCodeBasicBlock::BuildEntryDataSet(const NativeRegisterDataSet& set) { if (set.mRegs[i].mMode == NRDM_IMMEDIATE) { - if (mEntryRegisterDataSet.mRegs[i].mMode == NRDM_IMMEDIATE && set.mRegs[i].mValue != mEntryRegisterDataSet.mRegs[i].mValue) + if (mEntryRegisterDataSet.mRegs[i].mMode == NRDM_IMMEDIATE && set.mRegs[i].mValue == mEntryRegisterDataSet.mRegs[i].mValue) + { + } + else if (mEntryRegisterDataSet.mRegs[i].mMode != NRDM_UNKNOWN) { mEntryRegisterDataSet.mRegs[i].Reset(); mVisited = false; } } - else if (mEntryRegisterDataSet.mRegs[i].mMode == NRDM_IMMEDIATE) + else if (set.mRegs[i].mMode == NRDM_IMMEDIATE_ADDRESS) + { + if (mEntryRegisterDataSet.mRegs[i].mMode == NRDM_IMMEDIATE_ADDRESS && + set.mRegs[i].mValue == mEntryRegisterDataSet.mRegs[i].mValue && + set.mRegs[i].mLinkerObject == mEntryRegisterDataSet.mRegs[i].mLinkerObject && + set.mRegs[i].mFlags == mEntryRegisterDataSet.mRegs[i].mFlags) + { + } + else if (mEntryRegisterDataSet.mRegs[i].mMode != NRDM_UNKNOWN) + { + mEntryRegisterDataSet.mRegs[i].Reset(); + mVisited = false; + } + } + else if (mEntryRegisterDataSet.mRegs[i].mMode != NRDM_UNKNOWN) { mEntryRegisterDataSet.mRegs[i].Reset(); mVisited = false; @@ -7812,6 +7858,31 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(void) mIns[i + 1].mType = ASMIT_NOP; mIns[i + 1].mMode = ASMIM_IMPLIED; progress = true; } + else if ( + mIns[i + 0].mType == ASMIT_STA && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && + !mIns[i + 1].UsesZeroPage(mIns[i + 0].mAddress) && !mIns[i + 1].UsesAccu() && + mIns[i + 2].IsShift() && mIns[i + 2].mMode == ASMIM_ZERO_PAGE && mIns[i + 2].mAddress == mIns[i + 0].mAddress && !(mIns[i + 2].mLive & LIVE_CPU_REG_A)) + { + mIns[i + 0] = mIns[i + 1]; + mIns[i + 1] = mIns[i + 2]; + mIns[i + 1].mMode = ASMIM_IMPLIED; + mIns[i + 2].mType = ASMIT_STA; + mIns[i + 2].mLive |= mIns[i + 1].mLive & LIVE_CPU_REG_C; + progress = true; + } + else if ( + mIns[i + 0].mType == ASMIT_STA && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && + mIns[i + 1].mType == ASMIT_LDA && mIns[i + 1].mMode == ASMIM_IMMEDIATE && + mIns[i + 2].IsShift() && mIns[i + 2].mMode == ASMIM_ZERO_PAGE && mIns[i + 2].mAddress == mIns[i + 0].mAddress) + { + mIns[i + 0] = mIns[i + 2]; + mIns[i + 2] = mIns[i + 1]; + mIns[i + 1] = mIns[i + 0]; + mIns[i + 0].mMode = ASMIM_IMPLIED; + mIns[i + 1].mType = ASMIT_STA; + mIns[i + 2].mLive |= mIns[i + 1].mLive & LIVE_CPU_REG_C; + progress = true; + } #if 1 else if ( mIns[i + 0].mMode != ASMIM_RELATIVE && diff --git a/oscar64/NativeCodeGenerator.h b/oscar64/NativeCodeGenerator.h index 7ad09b0..d0086d7 100644 --- a/oscar64/NativeCodeGenerator.h +++ b/oscar64/NativeCodeGenerator.h @@ -68,6 +68,7 @@ public: bool ChangesAccuAndFlag(void) const; bool ChangesAddress(void) const; bool ChangesAccu(void) const; + bool UsesAccu(void) const; bool ChangesCarry(void) const; bool RequiresAccu(void) const; bool RequiresYReg(void) const; @@ -78,6 +79,7 @@ public: bool SameEffectiveAddress(const NativeCodeInstruction& ins) const; bool IsSame(const NativeCodeInstruction& ins) const; bool IsCommutative(void) const; + bool IsShift(void) const; }; class NativeCodeBasicBlock