Skip to content

Commit

Permalink
Optimize index chains
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Oct 1, 2023
1 parent 235437b commit c53a288
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/c64/iecbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ void iec_close(char dev, char sec)
iec_unlisten();
}

int iec_write_bytes(const char * data, int num)
{
for(int i=0; i<num; i++)
{
if (!iec_write(data[i]))
return i;
}
return num;
}

int iec_read_bytes(char * data, int num)
{
for(int i=0; i<num; i++)
{
char ch = iec_read();
if (iec_status != IEC_OK)
return i;
data[i] = ch;
}
return num;
}


#pragma optimize(pop)

5 changes: 5 additions & 0 deletions include/c64/iecbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ void iec_open(char dev, char sec, const char * fname);

void iec_close(char dev, char sec);

int iec_write_bytes(const char * data, int num);

int iec_read_bytes(char * data, int num);


#pragma compile("iecbus.c")

#endif
Expand Down
67 changes: 67 additions & 0 deletions oscar64/NativeCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27091,6 +27091,61 @@ bool NativeCodeBasicBlock::FoldShiftORAIntoLoadImmUp(int at)
return false;
}

// CLC
// LDA zp0
// ADC #1, 2, 3
// STA zp1
// ...
// LDX zp1
//
// Convert to INX/DEX

bool NativeCodeBasicBlock::MoveSimpleADCToINCDECDown(int at)
{
if (at >= 4)
{
int si = at - 4;

while (si >= 0)
{
if (mIns[si + 3].ReferencesZeroPage(mIns[at].mAddress))
{
if (mIns[si + 0].mType == ASMIT_CLC &&
mIns[si + 1].mType == ASMIT_LDA && mIns[si + 1].mMode == ASMIM_ZERO_PAGE &&
mIns[si + 2].mType == ASMIT_ADC && mIns[si + 2].mMode == ASMIM_IMMEDIATE && (mIns[si + 2].mAddress >= 0 && mIns[si + 2].mAddress <= 4) &&
mIns[si + 3].mType == ASMIT_STA && !(mIns[si + 3].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C)))
{
int i = si + 4;
while (i < at && !mIns[i].ChangesZeroPage(mIns[si + 1].mAddress))
i++;
if (i != at)
return false;

AsmInsType t = mIns[at].mType == ASMIT_LDX ? ASMIT_INX : ASMIT_INY;

mIns[at].mAddress = mIns[si + 1].mAddress;
for (int i = 0; i < mIns[si + 2].mAddress; i++)
mIns.Insert(at + 1, NativeCodeInstruction(mIns[si].mIns, t));

mIns[si + 0].mType = ASMIT_NOP; mIns[si + 0].mMode = ASMIM_IMPLIED;
mIns[si + 1].mType = ASMIT_NOP; mIns[si + 1].mMode = ASMIM_IMPLIED;
mIns[si + 2].mType = ASMIT_NOP; mIns[si + 2].mMode = ASMIM_IMPLIED;
mIns[si + 3].mType = ASMIT_NOP; mIns[si + 3].mMode = ASMIM_IMPLIED;

return true;
}
else
return false;
}

si--;
}

}

return false;
}

bool NativeCodeBasicBlock::CombineImmediateADCUp(int at)
{
int addr = mIns[at].mAddress;
Expand Down Expand Up @@ -34379,6 +34434,18 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
}
CheckLive();

#endif

#if 1
for (int i = 0; i < mIns.Size(); i++)
{
if ((mIns[i + 0].mType == ASMIT_LDX || mIns[i + 0].mType == ASMIT_LDY) && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && !(mIns[i + 0].mLive & LIVE_MEM))
{
if (MoveSimpleADCToINCDECDown(i))
changed = true;
}
}

#endif

//
Expand Down
2 changes: 2 additions & 0 deletions oscar64/NativeCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ class NativeCodeBasicBlock
bool MoveTXADCDown(int at);
bool FoldShiftORAIntoLoadImmUp(int at);

bool MoveSimpleADCToINCDECDown(int at);

bool MoveZeroPageCrossBlockUp(int at, const NativeCodeInstruction & lins, const NativeCodeInstruction & sins);
bool ShortcutCrossBlockMoves(NativeCodeProcedure* proc);

Expand Down

0 comments on commit c53a288

Please sign in to comment.