Merge pull request #2812 from colinleroy/Opt-ldptr1
Optimize ldax?sp/sta/stx to ldptr1?sp
This commit is contained in:
20
libsrc/runtime/ldptr1sp.s
Normal file
20
libsrc/runtime/ldptr1sp.s
Normal file
@@ -0,0 +1,20 @@
|
||||
;
|
||||
; Colin Leroy-Mira, 2025-07-26
|
||||
;
|
||||
; 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
|
||||
@@ -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 },
|
||||
|
||||
@@ -178,6 +178,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 };
|
||||
@@ -306,6 +307,7 @@ static OptFunc* OptFuncs[] = {
|
||||
&DOptPtrLoad18,
|
||||
&DOptPtrLoad19,
|
||||
&DOptPtrLoad2,
|
||||
&DOptPtrLoad20,
|
||||
&DOptPtrLoad3,
|
||||
&DOptPtrLoad4,
|
||||
&DOptPtrLoad5,
|
||||
@@ -904,6 +906,8 @@ static unsigned RunOptGroup7 (CodeSeg* S)
|
||||
Changes += RunOptFunc (S, &DOptTransfers3, 1);
|
||||
}
|
||||
|
||||
Changes += RunOptFunc (S, &DOptPtrLoad20, 1);
|
||||
|
||||
/* Adjust branch distances */
|
||||
Changes += RunOptFunc (S, &DOptBranchDist, 3);
|
||||
|
||||
|
||||
@@ -1671,3 +1671,64 @@ 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 ((CE_IsCallTo(E[0], "ldax0sp") ||
|
||||
CE_IsCallTo(E[0], "ldaxysp")) &&
|
||||
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) {
|
||||
CE_SetArg (E[0], "ldptr1ysp");
|
||||
} else {
|
||||
CE_SetArg (E[0], "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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user