Optimize ldax?sp/sta/stx to ldptr1?sp

This commit is contained in:
Colin Leroy-Mira
2025-07-20 12:24:24 +02:00
parent 5fc300b987
commit 17b8645360
5 changed files with 103 additions and 0 deletions

20
libsrc/runtime/ldptr1sp.s Normal file
View File

@@ -0,0 +1,20 @@
;
; Ullrich von Bassewitz, 31.08.1998
;
; CC65 runtime: Load ptr1 from offset in stack
;
.export ldptr10sp, ldptr1ysp
.importzp c_sp, ptr1
; Beware: The optimizer knows about the value in Y after return!
ldptr10sp:
ldy #1
ldptr1ysp:
lda (c_sp),y ; get high byte
sta ptr1+1 ; and save it
dey ; point to lo byte
lda (c_sp),y ; load low byte
sta ptr1
rts

View File

@@ -190,6 +190,8 @@ static const FuncInfo FuncInfoTable[] = {
{ "ldeaxi", REG_AX, PSTATE_ALL | REG_EAXY | REG_PTR1 },
{ "ldeaxidx", REG_AXY, PSTATE_ALL | REG_EAXY | REG_PTR1 },
{ "ldeaxysp", SLV_IND | REG_Y, PSTATE_ALL | REG_EAXY },
{ "ldptr10sp", SLV_TOP, PSTATE_ALL | REG_AY | REG_PTR1 },
{ "ldptr1ysp", REG_Y | SLV_TOP, PSTATE_ALL | REG_AY | REG_PTR1 },
{ "leaa0sp", REG_SP | REG_A, PSTATE_ALL | REG_AX },
{ "leaaxsp", REG_SP | REG_AX, PSTATE_ALL | REG_AX },
{ "leave", REG_SP, PSTATE_ALL | REG_SP | REG_Y },

View File

@@ -177,6 +177,7 @@ static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0,
static OptFunc DOptPtrLoad18 = { OptPtrLoad18, "OptPtrLoad18", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad19 = { OptPtrLoad19, "OptPtrLoad19", 65, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad20 = { OptPtrLoad20, "OptPtrLoad20", 90, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 };
@@ -301,6 +302,7 @@ static OptFunc* OptFuncs[] = {
&DOptPtrLoad18,
&DOptPtrLoad19,
&DOptPtrLoad2,
&DOptPtrLoad20,
&DOptPtrLoad3,
&DOptPtrLoad4,
&DOptPtrLoad5,
@@ -895,6 +897,8 @@ static unsigned RunOptGroup7 (CodeSeg* S)
Changes += RunOptFunc (S, &DOptTransfers3, 1);
}
Changes += RunOptFunc (S, &DOptPtrLoad20, 1);
/* Adjust branch distances */
Changes += RunOptFunc (S, &DOptBranchDist, 3);

View File

@@ -1671,3 +1671,67 @@ unsigned OptPtrLoad19 (CodeSeg* S)
/* Return the number of changes made */
return Changes;
}
unsigned OptPtrLoad20 (CodeSeg* S)
/* Search for the sequence:
**
** jsr ldax?sp
** sta ptr1
** stx ptr1+1
**
** and replace it by:
**
** jsr ldptr1?sp
*/
{
unsigned Changes = 0;
unsigned I;
/* Walk over the entries */
I = 0;
while (I < CS_GetEntryCount (S)) {
CodeEntry* E[3];
/* Get the next entry */
E[0] = CS_GetEntry (S, I);
if (E[0]->OPC == OP65_JSR &&
(strcmp (E[0]->Arg, "ldax0sp") == 0 ||
strcmp (E[0]->Arg, "ldaxysp") == 0) &&
CS_GetEntries (S, E+1, I+1, 2) != 0 &&
E[1]->OPC == OP65_STA &&
strcmp(E[1]->Arg, "ptr1") == 0 &&
E[2]->OPC == OP65_STX &&
strcmp(E[2]->Arg, "ptr1+1") == 0 &&
!CS_RangeHasLabel (S, I+1, 2)) {
if (strcmp (E[0]->Arg, "ldaxysp") == 0) {
xfree(E[0]->Arg);
E[0]->Arg = xstrdup("ldptr1ysp");
} else {
xfree(E[0]->Arg);
E[0]->Arg = xstrdup("ldptr10sp");
}
/* Delete the sta/stx */
CS_DelEntries (S, I+1, 2);
/* Regenerate register info */
CS_GenRegInfo (S);
/* Remember we had changes */
++Changes;
} else {
/* Next entry */
++I;
}
}
/* Return the number of changes made */
return Changes;
}

View File

@@ -405,6 +405,19 @@ unsigned OptPtrLoad19 (CodeSeg* S);
*/
unsigned OptPtrLoad20 (CodeSeg* S);
/* Search for the sequence:
**
** jsr ldax?sp
** sta ptr1
** stx ptr1+1
**
** and replace it by:
**
** jsr ldptr1?sp
*/
/* End of coptptrload.h */
#endif