1 /*****************************************************************************/
5 /* Optimize operations that take operands via the stack */
9 /* (C) 2001-2009 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
49 /*****************************************************************************/
50 /* Load tracking data */
51 /*****************************************************************************/
55 /* LoadRegInfo flags set by DirectOp */
58 LI_DIRECT = 0x01, /* Direct op may be used */
59 LI_RELOAD_Y = 0x02, /* Reload index register Y */
60 LI_REMOVE = 0x04, /* Load may be removed */
63 /* Structure that tells us how to load the lhs values */
64 typedef struct LoadRegInfo LoadRegInfo;
66 LI_FLAGS Flags; /* Tells us how to load */
67 int LoadIndex; /* Index of load insn, -1 if invalid */
68 CodeEntry* LoadEntry; /* The actual entry, 0 if invalid */
69 int XferIndex; /* Index of transfer insn */
70 CodeEntry* XferEntry; /* The actual transfer entry */
71 int Offs; /* Stack offset if data is on stack */
74 /* Now combined for both registers */
75 typedef struct LoadInfo LoadInfo;
77 LoadRegInfo A; /* Info for A register */
78 LoadRegInfo X; /* Info for X register */
79 LoadRegInfo Y; /* Info for Y register */
84 /*****************************************************************************/
86 /*****************************************************************************/
90 /* Flags for the functions */
92 OP_NONE = 0x00, /* Nothing special */
93 OP_A_KNOWN = 0x01, /* Value of A must be known */
94 OP_X_ZERO = 0x02, /* X must be zero */
95 OP_LHS_LOAD = 0x04, /* Must have load insns for LHS */
96 OP_LHS_LOAD_DIRECT = 0x0C, /* Must have direct load insn for LHS */
97 OP_RHS_LOAD = 0x10, /* Must have load insns for RHS */
98 OP_RHS_LOAD_DIRECT = 0x30, /* Must have direct load insn for RHS */
101 /* Structure forward decl */
102 typedef struct StackOpData StackOpData;
104 /* Structure that describes an optimizer subfunction for a specific op */
105 typedef unsigned (*OptFunc) (StackOpData* D);
106 typedef struct OptFuncDesc OptFuncDesc;
108 const char* Name; /* Name of the replaced runtime function */
109 OptFunc Func; /* Function pointer */
110 unsigned UnusedRegs; /* Regs that must not be used later */
111 OP_FLAGS Flags; /* Flags */
114 /* Structure that holds the needed data */
116 CodeSeg* Code; /* Pointer to code segment */
117 unsigned Flags; /* Flags to remember things */
119 /* Pointer to optimizer subfunction description */
120 const OptFuncDesc* OptFunc;
122 /* ZP register usage inside the sequence */
125 /* Register load information for lhs and rhs */
129 /* Several indices of insns in the code segment */
130 int PushIndex; /* Index of call to pushax in codeseg */
131 int OpIndex; /* Index of actual operation */
133 /* Pointers to insns in the code segment */
134 CodeEntry* PrevEntry; /* Entry before the call to pushax */
135 CodeEntry* PushEntry; /* Pointer to entry with call to pushax */
136 CodeEntry* OpEntry; /* Pointer to entry with op */
137 CodeEntry* NextEntry; /* Entry after the op */
139 const char* ZPLo; /* Lo byte of zero page loc to use */
140 const char* ZPHi; /* Hi byte of zero page loc to use */
141 unsigned IP; /* Insertion point used by some routines */
146 /*****************************************************************************/
147 /* Load tracking code */
148 /*****************************************************************************/
152 static void ClearLoadRegInfo (LoadRegInfo* RI)
153 /* Clear a LoadRegInfo struct */
163 static void FinalizeLoadRegInfo (LoadRegInfo* RI, CodeSeg* S)
164 /* Prepare a LoadRegInfo struct for use */
166 /* Get the entries */
167 if (RI->LoadIndex >= 0) {
168 RI->LoadEntry = CS_GetEntry (S, RI->LoadIndex);
172 if (RI->XferIndex >= 0) {
173 RI->XferEntry = CS_GetEntry (S, RI->XferIndex);
181 static void ClearLoadInfo (LoadInfo* LI)
182 /* Clear a LoadInfo struct */
184 ClearLoadRegInfo (&LI->A);
185 ClearLoadRegInfo (&LI->X);
186 ClearLoadRegInfo (&LI->Y);
191 static void AdjustLoadRegInfo (LoadRegInfo* RI, int Index, int Change)
192 /* Adjust a load register info struct after deleting or inserting an entry
196 CHECK (abs (Change) == 1);
199 if (Index < RI->LoadIndex) {
201 } else if (Index == RI->LoadIndex) {
202 /* Has been removed */
206 if (Index < RI->XferIndex) {
208 } else if (Index == RI->XferIndex) {
209 /* Has been removed */
215 if (Index <= RI->LoadIndex) {
218 if (Index <= RI->XferIndex) {
226 static void FinalizeLoadInfo (LoadInfo* LI, CodeSeg* S)
227 /* Prepare a LoadInfo struct for use */
229 /* Get the entries */
230 FinalizeLoadRegInfo (&LI->A, S);
231 FinalizeLoadRegInfo (&LI->X, S);
232 FinalizeLoadRegInfo (&LI->Y, S);
237 static void AdjustLoadInfo (LoadInfo* LI, int Index, int Change)
238 /* Adjust a load info struct after deleting entry with a given index */
240 AdjustLoadRegInfo (&LI->A, Index, Change);
241 AdjustLoadRegInfo (&LI->X, Index, Change);
242 AdjustLoadRegInfo (&LI->Y, Index, Change);
247 static void TrackLoads (LoadInfo* LI, CodeEntry* E, int I)
248 /* Track loads for a code entry */
250 if (E->Info & OF_LOAD) {
254 /* Determine, which register was loaded */
255 if (E->Chg & REG_A) {
257 } else if (E->Chg & REG_X) {
259 } else if (E->Chg & REG_Y) {
264 /* Remember the load */
269 RI->Flags &= ~(LI_DIRECT | LI_RELOAD_Y);
270 if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
271 /* These insns are all ok and replaceable */
272 RI->Flags |= LI_DIRECT;
273 } else if (E->AM == AM65_ZP_INDY &&
274 RegValIsKnown (E->RI->In.RegY) &&
275 strcmp (E->Arg, "sp") == 0) {
276 /* A load from the stack with known offset is also ok, but in this
277 * case we must reload the index register later. Please note that
278 * a load indirect via other zero page locations is not ok, since
279 * these locations may change between the push and the actual
282 RI->Offs = (unsigned char) E->RI->In.RegY;
283 RI->Flags |= (LI_DIRECT | LI_RELOAD_Y);
287 } else if (E->Info & OF_XFR) {
289 /* Determine source and target of the transfer and handle the TSX insn */
293 case OP65_TAX: Src = &LI->A; Tgt = &LI->X; break;
294 case OP65_TAY: Src = &LI->A; Tgt = &LI->Y; break;
295 case OP65_TXA: Src = &LI->X; Tgt = &LI->A; break;
296 case OP65_TYA: Src = &LI->Y; Tgt = &LI->A; break;
297 case OP65_TSX: ClearLoadRegInfo (&LI->X); return;
298 case OP65_TXS: return;
299 default: Internal ("Unknown XFR insn in TrackLoads");
302 /* Transfer the data */
303 Tgt->LoadIndex = Src->LoadIndex;
305 Tgt->Offs = Src->Offs;
306 Tgt->Flags &= ~(LI_DIRECT | LI_RELOAD_Y);
307 Tgt->Flags |= Src->Flags & (LI_DIRECT | LI_RELOAD_Y);
309 } else if (CE_IsCallTo (E, "ldaxysp") && RegValIsKnown (E->RI->In.RegY)) {
311 /* Both registers set, Y changed */
313 LI->A.XferIndex = -1;
314 LI->A.Flags |= (LI_DIRECT | LI_RELOAD_Y);
315 LI->A.Offs = (unsigned char) E->RI->In.RegY - 1;
318 LI->X.XferIndex = -1;
319 LI->X.Flags |= (LI_DIRECT | LI_RELOAD_Y);
320 LI->X.Offs = (unsigned char) E->RI->In.RegY;
322 ClearLoadRegInfo (&LI->Y);
324 if (E->Chg & REG_A) {
325 ClearLoadRegInfo (&LI->A);
327 if (E->Chg & REG_X) {
328 ClearLoadRegInfo (&LI->X);
330 if (E->Chg & REG_Y) {
331 ClearLoadRegInfo (&LI->Y);
338 /*****************************************************************************/
340 /*****************************************************************************/
344 static void InsertEntry (StackOpData* D, CodeEntry* E, int Index)
345 /* Insert a new entry. Depending on Index, D->PushIndex and D->OpIndex will
346 * be adjusted by this function.
349 /* Insert the entry into the code segment */
350 CS_InsertEntry (D->Code, E, Index);
352 /* Adjust register loads if necessary */
353 AdjustLoadInfo (&D->Lhs, Index, 1);
354 AdjustLoadInfo (&D->Rhs, Index, 1);
356 /* Adjust the indices if necessary */
357 if (D->PushEntry && Index <= D->PushIndex) {
360 if (D->OpEntry && Index <= D->OpIndex) {
367 static void DelEntry (StackOpData* D, int Index)
368 /* Delete an entry. Depending on Index, D->PushIndex and D->OpIndex will be
369 * adjusted by this function, and PushEntry/OpEntry may get invalidated.
372 /* Delete the entry from the code segment */
373 CS_DelEntry (D->Code, Index);
375 /* Adjust register loads if necessary */
376 AdjustLoadInfo (&D->Lhs, Index, -1);
377 AdjustLoadInfo (&D->Rhs, Index, -1);
379 /* Adjust the other indices if necessary */
380 if (Index < D->PushIndex) {
382 } else if (Index == D->PushIndex) {
385 if (Index < D->OpIndex) {
387 } else if (Index == D->OpIndex) {
394 static void AdjustStackOffset (StackOpData* D, unsigned Offs)
395 /* Adjust the offset for all stack accesses in the range PushIndex to OpIndex.
396 * OpIndex is adjusted according to the insertions.
399 /* Walk over all entries */
400 int I = D->PushIndex + 1;
401 while (I < D->OpIndex) {
403 CodeEntry* E = CS_GetEntry (D->Code, I);
405 int NeedCorrection = 0;
406 if ((E->Use & REG_SP) != 0) {
408 /* Check for some things that should not happen */
409 CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
410 CHECK (strcmp (E->Arg, "sp") == 0);
412 /* We need to correct this one */
415 } else if (CE_IsCallTo (E, "ldaxysp")) {
417 /* We need to correct this one */
422 if (NeedCorrection) {
424 /* Get the code entry before this one. If it's a LDY, adjust the
427 CodeEntry* P = CS_GetPrevEntry (D->Code, I);
428 if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
430 /* The Y load is just before the stack access, adjust it */
431 CE_SetNumArg (P, P->Num - Offs);
435 /* Insert a new load instruction before the stack access */
436 const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
437 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
438 InsertEntry (D, X, I++);
442 /* If we need the value of Y later, be sure to reload it */
443 if (RegYUsed (D->Code, I+1)) {
444 const char* Arg = MakeHexArg (E->RI->In.RegY);
445 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
446 InsertEntry (D, X, I+1);
448 /* Skip this instruction in the next round */
457 /* If we have rhs load insns that load from stack, we'll have to adjust
458 * the offsets for these also.
460 if (D->Rhs.A.Flags & LI_RELOAD_Y) {
461 D->Rhs.A.Offs -= Offs;
463 if (D->Rhs.X.Flags & LI_RELOAD_Y) {
464 D->Rhs.X.Offs -= Offs;
470 static void AddStoreA (StackOpData* D)
471 /* Add a store to zero page after the push insn */
473 CodeEntry* X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->PushEntry->LI);
474 InsertEntry (D, X, D->PushIndex+1);
479 static void AddStoreX (StackOpData* D)
480 /* Add a store to zero page after the push insn */
482 CodeEntry* X = NewCodeEntry (OP65_STX, AM65_ZP, D->ZPHi, 0, D->PushEntry->LI);
483 InsertEntry (D, X, D->PushIndex+1);
488 static void ReplacePushByStore (StackOpData* D)
489 /* Replace the call to the push subroutine by a store into the zero page
490 * location (actually, the push is not replaced, because we need it for
491 * later, but the name is still ok since the push will get removed at the
492 * end of each routine).
495 /* Store the value into the zeropage instead of pushing it. Check high
496 * byte first so that the store is later in A/X order.
498 if ((D->Lhs.X.Flags & LI_DIRECT) == 0) {
501 if ((D->Lhs.A.Flags & LI_DIRECT) == 0) {
508 static void AddOpLow (StackOpData* D, opc_t OPC, LoadInfo* LI)
509 /* Add an op for the low byte of an operator. This function honours the
510 * OP_DIRECT and OP_RELOAD_Y flags and generates the necessary instructions.
511 * All code is inserted at the current insertion point.
516 if ((LI->A.Flags & LI_DIRECT) != 0) {
517 /* Op with a variable location. If the location is on the stack, we
518 * need to reload the Y register.
520 if ((LI->A.Flags & LI_RELOAD_Y) == 0) {
523 CodeEntry* LoadA = LI->A.LoadEntry;
524 X = NewCodeEntry (OPC, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
525 InsertEntry (D, X, D->IP++);
530 const char* Arg = MakeHexArg (LI->A.Offs);
531 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
532 InsertEntry (D, X, D->IP++);
535 X = NewCodeEntry (OPC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
536 InsertEntry (D, X, D->IP++);
540 /* In both cases, we can remove the load */
541 LI->A.Flags |= LI_REMOVE;
545 /* Op with temp storage */
546 X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
547 InsertEntry (D, X, D->IP++);
554 static void AddOpHigh (StackOpData* D, opc_t OPC, LoadInfo* LI, int KeepResult)
555 /* Add an op for the high byte of an operator. Special cases (constant values
556 * or similar) have to be checked separately, the function covers only the
557 * generic case. Code is inserted at the insertion point.
564 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
565 InsertEntry (D, X, D->IP++);
569 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
570 InsertEntry (D, X, D->IP++);
572 if ((LI->X.Flags & LI_DIRECT) != 0) {
574 if ((LI->X.Flags & LI_RELOAD_Y) == 0) {
577 CodeEntry* LoadX = LI->X.LoadEntry;
578 X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
579 InsertEntry (D, X, D->IP++);
584 const char* Arg = MakeHexArg (LI->X.Offs);
585 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
586 InsertEntry (D, X, D->IP++);
589 X = NewCodeEntry (OPC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
590 InsertEntry (D, X, D->IP++);
593 /* In both cases, we can remove the load */
594 LI->X.Flags |= LI_REMOVE;
598 X = NewCodeEntry (OPC, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
599 InsertEntry (D, X, D->IP++);
604 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
605 InsertEntry (D, X, D->IP++);
608 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
609 InsertEntry (D, X, D->IP++);
615 static void RemoveRegLoads (StackOpData* D, LoadInfo* LI)
616 /* Remove register load insns */
618 /* Both registers may be loaded with one insn, but DelEntry will in this
619 * case clear the other one.
621 if (LI->A.Flags & LI_REMOVE) {
622 if (LI->A.LoadIndex >= 0) {
623 DelEntry (D, LI->A.LoadIndex);
625 if (LI->A.XferIndex >= 0) {
626 DelEntry (D, LI->A.XferIndex);
629 if (LI->X.Flags & LI_REMOVE) {
630 if (LI->X.LoadIndex >= 0) {
631 DelEntry (D, LI->X.LoadIndex);
633 if (LI->X.XferIndex >= 0) {
634 DelEntry (D, LI->X.XferIndex);
641 static void RemoveRemainders (StackOpData* D)
642 /* Remove the code that is unnecessary after translation of the sequence */
644 /* Remove the register loads for lhs and rhs */
645 RemoveRegLoads (D, &D->Lhs);
646 RemoveRegLoads (D, &D->Rhs);
648 /* Remove the push and the operator routine */
649 DelEntry (D, D->OpIndex);
650 DelEntry (D, D->PushIndex);
655 static int IsRegVar (StackOpData* D)
656 /* If the value pushed is that of a zeropage variable, replace ZPLo and ZPHi
657 * in the given StackOpData struct by the variable and return true. Otherwise
658 * leave D untouched and return false.
661 CodeEntry* LoadA = D->Lhs.A.LoadEntry;
662 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
665 /* Must have both load insns */
666 if (LoadA == 0 || LoadX == 0) {
670 /* Must be loads from zp */
671 if (LoadA->AM != AM65_ZP || LoadX->AM != AM65_ZP) {
675 /* Must be the same zp loc with high byte in X */
676 Len = strlen (LoadA->Arg);
677 if (strncmp (LoadA->Arg, LoadX->Arg, Len) != 0 ||
678 strcmp (LoadX->Arg + Len, "+1") != 0) {
682 /* Use the zero page location directly */
683 D->ZPLo = LoadA->Arg;
684 D->ZPHi = LoadX->Arg;
690 /*****************************************************************************/
691 /* Actual optimization functions */
692 /*****************************************************************************/
696 static unsigned Opt_toseqax_tosneax (StackOpData* D, const char* BoolTransformer)
697 /* Optimize the toseqax and tosneax sequences. */
702 /* Create a call to the boolean transformer function and a label for this
703 * insn. This is needed for all variants. Other insns are inserted *before*
706 X = NewCodeEntry (OP65_JSR, AM65_ABS, BoolTransformer, 0, D->OpEntry->LI);
707 InsertEntry (D, X, D->OpIndex + 1);
708 L = CS_GenLabel (D->Code, X);
710 /* If the lhs is direct (but not stack relative), encode compares with lhs
711 * effectively reverting the order (which doesn't matter for ==).
713 if ((D->Lhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
714 (D->Lhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
716 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
717 CodeEntry* LoadA = D->Lhs.A.LoadEntry;
719 D->IP = D->OpIndex+1;
722 X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
723 InsertEntry (D, X, D->IP++);
726 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
727 InsertEntry (D, X, D->IP++);
730 X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
731 InsertEntry (D, X, D->IP++);
733 /* Lhs load entries can be removed */
734 D->Lhs.X.Flags |= LI_REMOVE;
735 D->Lhs.A.Flags |= LI_REMOVE;
737 } else if ((D->Rhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
738 (D->Rhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
740 CodeEntry* LoadX = D->Rhs.X.LoadEntry;
741 CodeEntry* LoadA = D->Rhs.A.LoadEntry;
743 D->IP = D->OpIndex+1;
746 X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
747 InsertEntry (D, X, D->IP++);
750 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
751 InsertEntry (D, X, D->IP++);
754 X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
755 InsertEntry (D, X, D->IP++);
757 /* Rhs load entries can be removed */
758 D->Rhs.X.Flags |= LI_REMOVE;
759 D->Rhs.A.Flags |= LI_REMOVE;
761 } else if ((D->Rhs.A.Flags & LI_DIRECT) != 0 &&
762 (D->Rhs.X.Flags & LI_DIRECT) != 0) {
764 D->IP = D->OpIndex+1;
766 /* Add operand for low byte */
767 AddOpLow (D, OP65_CMP, &D->Rhs);
770 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
771 InsertEntry (D, X, D->IP++);
773 /* Add operand for high byte */
774 AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
778 /* Save lhs into zeropage, then compare */
782 D->IP = D->OpIndex+1;
785 X = NewCodeEntry (OP65_CPX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
786 InsertEntry (D, X, D->IP++);
789 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
790 InsertEntry (D, X, D->IP++);
793 X = NewCodeEntry (OP65_CMP, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
794 InsertEntry (D, X, D->IP++);
798 /* Remove the push and the call to the tosgeax function */
799 RemoveRemainders (D);
801 /* We changed the sequence */
807 static unsigned Opt___bzero (StackOpData* D)
808 /* Optimize the __bzero sequence */
814 /* Check if we're using a register variable */
816 /* Store the value into the zeropage instead of pushing it */
821 /* If the return value of __bzero is used, we have to add code to reload
822 * a/x from the pointer variable.
824 if (RegAXUsed (D->Code, D->OpIndex+1)) {
825 X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
826 InsertEntry (D, X, D->OpIndex+1);
827 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
828 InsertEntry (D, X, D->OpIndex+2);
831 /* X is always zero, A contains the size of the data area to zero.
832 * Note: A may be zero, in which case the operation is null op.
834 if (D->OpEntry->RI->In.RegA != 0) {
837 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
838 InsertEntry (D, X, D->OpIndex+1);
840 /* The value of A is known */
841 if (D->OpEntry->RI->In.RegA <= 0x81) {
843 /* Loop using the sign bit */
846 Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
847 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
848 InsertEntry (D, X, D->OpIndex+2);
851 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
852 InsertEntry (D, X, D->OpIndex+3);
853 L = CS_GenLabel (D->Code, X);
856 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, D->OpEntry->LI);
857 InsertEntry (D, X, D->OpIndex+4);
860 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, D->OpEntry->LI);
861 InsertEntry (D, X, D->OpIndex+5);
865 /* Loop using an explicit compare */
868 X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
869 InsertEntry (D, X, D->OpIndex+2);
872 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
873 InsertEntry (D, X, D->OpIndex+3);
874 L = CS_GenLabel (D->Code, X);
877 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
878 InsertEntry (D, X, D->OpIndex+4);
881 Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
882 X = NewCodeEntry (OP65_CPY, AM65_IMM, Arg, 0, D->OpEntry->LI);
883 InsertEntry (D, X, D->OpIndex+5);
886 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
887 InsertEntry (D, X, D->OpIndex+6);
892 /* Remove the push and the call to the __bzero function */
893 RemoveRemainders (D);
895 /* We changed the sequence */
901 static unsigned Opt_staspidx (StackOpData* D)
902 /* Optimize the staspidx sequence */
906 /* Check if we're using a register variable */
908 /* Store the value into the zeropage instead of pushing it */
913 /* Replace the store subroutine call by a direct op */
914 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
915 InsertEntry (D, X, D->OpIndex+1);
917 /* Remove the push and the call to the staspidx function */
918 RemoveRemainders (D);
920 /* We changed the sequence */
926 static unsigned Opt_staxspidx (StackOpData* D)
927 /* Optimize the staxspidx sequence */
931 /* Check if we're using a register variable */
933 /* Store the value into the zeropage instead of pushing it */
938 /* Inline the store */
941 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
942 InsertEntry (D, X, D->OpIndex+1);
944 if (RegValIsKnown (D->OpEntry->RI->In.RegY)) {
945 /* Value of Y is known */
946 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
947 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
949 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
951 InsertEntry (D, X, D->OpIndex+2);
953 if (RegValIsKnown (D->OpEntry->RI->In.RegX)) {
954 /* Value of X is known */
955 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
956 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
959 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
961 InsertEntry (D, X, D->OpIndex+3);
964 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
965 InsertEntry (D, X, D->OpIndex+4);
967 /* If we remove staxspidx, we must restore the Y register to what the
968 * function would return.
970 X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
971 InsertEntry (D, X, D->OpIndex+5);
973 /* Remove the push and the call to the staxspidx function */
974 RemoveRemainders (D);
976 /* We changed the sequence */
982 static unsigned Opt_tosaddax (StackOpData* D)
983 /* Optimize the tosaddax sequence */
988 /* We need the entry behind the add */
989 CHECK (D->NextEntry != 0);
991 /* Check if the X register is known and zero when the add is done, and
992 * if the add is followed by
995 * jsr ldauidx ; or ldaidx
997 * If this is true, the addition does actually add an offset to a pointer
998 * before it is dereferenced. Since both subroutines take an offset in Y,
999 * we can pass the offset (instead of #$00) and remove the addition
1002 if (D->OpEntry->RI->In.RegX == 0 &&
1003 D->NextEntry->OPC == OP65_LDY &&
1004 CE_IsKnownImm (D->NextEntry, 0) &&
1005 !CE_HasLabel (D->NextEntry) &&
1006 (N = CS_GetNextEntry (D->Code, D->OpIndex + 1)) != 0 &&
1007 (CE_IsCallTo (N, "ldauidx") ||
1008 CE_IsCallTo (N, "ldaidx"))) {
1010 int Signed = (strcmp (N->Arg, "ldaidx") == 0);
1012 /* Store the value into the zeropage instead of pushing it */
1016 /* Replace the ldy by a tay. Be sure to create the new entry before
1017 * deleting the ldy, since we will reference the line info from this
1020 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->NextEntry->LI);
1021 DelEntry (D, D->OpIndex + 1);
1022 InsertEntry (D, X, D->OpIndex + 1);
1024 /* Replace the call to ldaidx/ldauidx. Since X is already zero, and
1025 * the ptr is in the zero page location, we just need to load from
1026 * the pointer, and fix X in case of ldaidx.
1028 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, D->ZPLo, 0, N->LI);
1029 DelEntry (D, D->OpIndex + 2);
1030 InsertEntry (D, X, D->OpIndex + 2);
1035 /* Add sign extension - N is unused now */
1036 N = CS_GetNextEntry (D->Code, D->OpIndex + 2);
1038 L = CS_GenLabel (D->Code, N);
1040 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, X->LI);
1041 InsertEntry (D, X, D->OpIndex + 3);
1043 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, X->LI);
1044 InsertEntry (D, X, D->OpIndex + 4);
1049 /* Store the value into the zeropage instead of pushing it */
1050 ReplacePushByStore (D);
1052 /* Inline the add */
1053 D->IP = D->OpIndex+1;
1056 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, D->OpEntry->LI);
1057 InsertEntry (D, X, D->IP++);
1060 AddOpLow (D, OP65_ADC, &D->Lhs);
1063 if (D->PushEntry->RI->In.RegX == 0) {
1065 /* The high byte is the value in X plus the carry */
1066 CodeLabel* L = CS_GenLabel (D->Code, D->NextEntry);
1069 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1070 InsertEntry (D, X, D->IP++);
1073 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
1074 InsertEntry (D, X, D->IP++);
1076 } else if (D->OpEntry->RI->In.RegX == 0 &&
1077 (RegValIsKnown (D->PushEntry->RI->In.RegX) ||
1078 (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
1080 /* The high byte is that of the first operand plus carry */
1082 if (RegValIsKnown (D->PushEntry->RI->In.RegX)) {
1083 /* Value of first op high byte is known */
1084 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX);
1085 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
1087 /* Value of first op high byte is unknown. Load from ZP or
1090 if (D->Lhs.X.Flags & LI_DIRECT) {
1091 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
1092 X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
1094 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
1097 InsertEntry (D, X, D->IP++);
1100 L = CS_GenLabel (D->Code, D->NextEntry);
1101 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1102 InsertEntry (D, X, D->IP++);
1105 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
1106 InsertEntry (D, X, D->IP++);
1108 /* High byte is unknown */
1109 AddOpHigh (D, OP65_ADC, &D->Lhs, 1);
1113 /* Remove the push and the call to the tosaddax function */
1114 RemoveRemainders (D);
1116 /* We changed the sequence */
1122 static unsigned Opt_tosandax (StackOpData* D)
1123 /* Optimize the tosandax sequence */
1125 /* Store the value into the zeropage instead of pushing it */
1126 ReplacePushByStore (D);
1128 /* Inline the and, low byte */
1129 D->IP = D->OpIndex + 1;
1130 AddOpLow (D, OP65_AND, &D->Lhs);
1133 AddOpHigh (D, OP65_AND, &D->Lhs, 1);
1135 /* Remove the push and the call to the tosandax function */
1136 RemoveRemainders (D);
1138 /* We changed the sequence */
1144 static unsigned Opt_toseqax (StackOpData* D)
1145 /* Optimize the toseqax sequence */
1147 return Opt_toseqax_tosneax (D, "booleq");
1152 static unsigned Opt_tosgeax (StackOpData* D)
1153 /* Optimize the tosgeax sequence */
1158 /* Inline the sbc */
1159 D->IP = D->OpIndex+1;
1161 /* Must be true because of OP_RHS_LOAD */
1162 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1164 /* Add code for low operand */
1165 AddOpLow (D, OP65_CMP, &D->Rhs);
1167 /* Add code for high operand */
1168 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1171 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
1172 InsertEntry (D, X, D->IP++);
1175 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, D->OpEntry->LI);
1176 InsertEntry (D, X, D->IP++);
1177 L = CS_GenLabel (D->Code, X);
1179 /* Insert a bvs L before the eor insn */
1180 X = NewCodeEntry (OP65_BVS, AM65_BRA, L->Name, L, D->OpEntry->LI);
1181 InsertEntry (D, X, D->IP - 2);
1185 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1186 InsertEntry (D, X, D->IP++);
1189 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1190 InsertEntry (D, X, D->IP++);
1193 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1194 InsertEntry (D, X, D->IP++);
1196 /* Remove the push and the call to the tosgeax function */
1197 RemoveRemainders (D);
1199 /* We changed the sequence */
1205 static unsigned Opt_tosltax (StackOpData* D)
1206 /* Optimize the tosltax sequence */
1212 /* Inline the sbc */
1213 D->IP = D->OpIndex+1;
1215 /* Must be true because of OP_RHS_LOAD */
1216 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1218 /* Add code for low operand */
1219 AddOpLow (D, OP65_CMP, &D->Rhs);
1221 /* Add code for high operand */
1222 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1225 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
1226 InsertEntry (D, X, D->IP++);
1229 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, D->OpEntry->LI);
1230 InsertEntry (D, X, D->IP++);
1231 L = CS_GenLabel (D->Code, X);
1233 /* Insert a bvc L before the eor insn */
1234 X = NewCodeEntry (OP65_BVC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1235 InsertEntry (D, X, D->IP - 2);
1239 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1240 InsertEntry (D, X, D->IP++);
1243 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1244 InsertEntry (D, X, D->IP++);
1247 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1248 InsertEntry (D, X, D->IP++);
1250 /* Remove the push and the call to the tosltax function */
1251 RemoveRemainders (D);
1253 /* We changed the sequence */
1259 static unsigned Opt_tosneax (StackOpData* D)
1260 /* Optimize the tosneax sequence */
1262 return Opt_toseqax_tosneax (D, "boolne");
1267 static unsigned Opt_tosorax (StackOpData* D)
1268 /* Optimize the tosorax sequence */
1270 /* Store the value into the zeropage instead of pushing it */
1271 ReplacePushByStore (D);
1273 /* Inline the or, low byte */
1274 D->IP = D->OpIndex + 1;
1275 AddOpLow (D, OP65_ORA, &D->Lhs);
1278 AddOpHigh (D, OP65_ORA, &D->Lhs, 1);
1280 /* Remove the push and the call to the tosorax function */
1281 RemoveRemainders (D);
1283 /* We changed the sequence */
1289 static unsigned Opt_tossubax (StackOpData* D)
1290 /* Optimize the tossubax sequence. Note: subtraction is not commutative! */
1295 /* Inline the sbc */
1296 D->IP = D->OpIndex+1;
1299 X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
1300 InsertEntry (D, X, D->IP++);
1302 /* Must be true because of OP_RHS_LOAD */
1303 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1305 /* Add code for low operand */
1306 AddOpLow (D, OP65_SBC, &D->Rhs);
1308 /* Add code for high operand */
1309 AddOpHigh (D, OP65_SBC, &D->Rhs, 1);
1311 /* Remove the push and the call to the tossubax function */
1312 RemoveRemainders (D);
1314 /* We changed the sequence */
1320 static unsigned Opt_tosugeax (StackOpData* D)
1321 /* Optimize the tosugeax sequence */
1326 /* Inline the sbc */
1327 D->IP = D->OpIndex+1;
1329 /* Must be true because of OP_RHS_LOAD */
1330 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1332 /* Add code for low operand */
1333 AddOpLow (D, OP65_CMP, &D->Rhs);
1335 /* Add code for high operand */
1336 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1339 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1340 InsertEntry (D, X, D->IP++);
1343 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1344 InsertEntry (D, X, D->IP++);
1347 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1348 InsertEntry (D, X, D->IP++);
1350 /* Remove the push and the call to the tosugeax function */
1351 RemoveRemainders (D);
1353 /* We changed the sequence */
1359 static unsigned Opt_tosugtax (StackOpData* D)
1360 /* Optimize the tosugtax sequence */
1365 /* Inline the sbc */
1366 D->IP = D->OpIndex+1;
1368 /* Must be true because of OP_RHS_LOAD */
1369 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1372 X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
1373 InsertEntry (D, X, D->IP++);
1375 /* Add code for low operand */
1376 AddOpLow (D, OP65_SBC, &D->Rhs);
1378 /* We need the zero flag, so remember the immediate result */
1379 X = NewCodeEntry (OP65_STA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1380 InsertEntry (D, X, D->IP++);
1382 /* Add code for high operand */
1383 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1386 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1387 InsertEntry (D, X, D->IP++);
1389 /* Transform to boolean */
1390 X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolugt", 0, D->OpEntry->LI);
1391 InsertEntry (D, X, D->IP++);
1393 /* Remove the push and the call to the operator function */
1394 RemoveRemainders (D);
1396 /* We changed the sequence */
1402 static unsigned Opt_tosuleax (StackOpData* D)
1403 /* Optimize the tosuleax sequence */
1408 /* Inline the sbc */
1409 D->IP = D->OpIndex+1;
1411 /* Must be true because of OP_RHS_LOAD */
1412 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1415 X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
1416 InsertEntry (D, X, D->IP++);
1418 /* Add code for low operand */
1419 AddOpLow (D, OP65_SBC, &D->Rhs);
1421 /* We need the zero flag, so remember the immediate result */
1422 X = NewCodeEntry (OP65_STA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1423 InsertEntry (D, X, D->IP++);
1425 /* Add code for high operand */
1426 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1429 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1430 InsertEntry (D, X, D->IP++);
1432 /* Transform to boolean */
1433 X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolule", 0, D->OpEntry->LI);
1434 InsertEntry (D, X, D->IP++);
1436 /* Remove the push and the call to the operator function */
1437 RemoveRemainders (D);
1439 /* We changed the sequence */
1445 static unsigned Opt_tosultax (StackOpData* D)
1446 /* Optimize the tosultax sequence */
1451 /* Inline the sbc */
1452 D->IP = D->OpIndex+1;
1454 /* Must be true because of OP_RHS_LOAD */
1455 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1457 /* Add code for low operand */
1458 AddOpLow (D, OP65_CMP, &D->Rhs);
1460 /* Add code for high operand */
1461 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1463 /* Transform to boolean */
1464 X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolult", 0, D->OpEntry->LI);
1465 InsertEntry (D, X, D->IP++);
1467 /* Remove the push and the call to the operator function */
1468 RemoveRemainders (D);
1470 /* We changed the sequence */
1476 static unsigned Opt_tosxorax (StackOpData* D)
1477 /* Optimize the tosxorax sequence */
1482 /* Store the value into the zeropage instead of pushing it */
1483 ReplacePushByStore (D);
1485 /* Inline the xor, low byte */
1486 D->IP = D->OpIndex + 1;
1487 AddOpLow (D, OP65_EOR, &D->Lhs);
1490 if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
1491 RegValIsKnown (D->OpEntry->RI->In.RegX)) {
1492 /* Both values known, precalculate the result */
1493 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
1494 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
1495 InsertEntry (D, X, D->IP++);
1496 } else if (D->PushEntry->RI->In.RegX != 0) {
1497 /* High byte is unknown */
1498 AddOpHigh (D, OP65_EOR, &D->Lhs, 1);
1501 /* Remove the push and the call to the tosandax function */
1502 RemoveRemainders (D);
1504 /* We changed the sequence */
1510 /*****************************************************************************/
1512 /*****************************************************************************/
1516 static const OptFuncDesc FuncTable[] = {
1517 { "__bzero", Opt___bzero, REG_NONE, OP_X_ZERO | OP_A_KNOWN },
1518 { "staspidx", Opt_staspidx, REG_NONE, OP_NONE },
1519 { "staxspidx", Opt_staxspidx, REG_AX, OP_NONE },
1520 { "tosaddax", Opt_tosaddax, REG_NONE, OP_NONE },
1521 { "tosandax", Opt_tosandax, REG_NONE, OP_NONE },
1522 { "toseqax", Opt_toseqax, REG_NONE, OP_NONE },
1523 { "tosgeax", Opt_tosgeax, REG_NONE, OP_RHS_LOAD_DIRECT },
1524 { "tosltax", Opt_tosltax, REG_NONE, OP_RHS_LOAD_DIRECT },
1525 { "tosneax", Opt_tosneax, REG_NONE, OP_NONE },
1526 { "tosorax", Opt_tosorax, REG_NONE, OP_NONE },
1527 { "tossubax", Opt_tossubax, REG_NONE, OP_RHS_LOAD_DIRECT },
1528 { "tosugeax", Opt_tosugeax, REG_NONE, OP_RHS_LOAD_DIRECT },
1529 { "tosugtax", Opt_tosugtax, REG_NONE, OP_RHS_LOAD_DIRECT },
1530 { "tosuleax", Opt_tosuleax, REG_NONE, OP_RHS_LOAD_DIRECT },
1531 { "tosultax", Opt_tosultax, REG_NONE, OP_RHS_LOAD_DIRECT },
1532 { "tosxorax", Opt_tosxorax, REG_NONE, OP_NONE },
1534 #define FUNC_COUNT (sizeof(FuncTable) / sizeof(FuncTable[0]))
1538 static int CmpFunc (const void* Key, const void* Func)
1539 /* Compare function for bsearch */
1541 return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
1546 static const OptFuncDesc* FindFunc (const char* Name)
1547 /* Find the function with the given name. Return a pointer to the table entry
1548 * or NULL if the function was not found.
1551 return bsearch (Name, FuncTable, FUNC_COUNT, sizeof(OptFuncDesc), CmpFunc);
1556 static int CmpHarmless (const void* Key, const void* Entry)
1557 /* Compare function for bsearch */
1559 return strcmp (Key, *(const char**)Entry);
1564 static int HarmlessCall (const char* Name)
1565 /* Check if this is a call to a harmless subroutine that will not interrupt
1566 * the pushax/op sequence when encountered.
1569 static const char* Tab[] = {
1592 void* R = bsearch (Name,
1594 sizeof (Tab) / sizeof (Tab[0]),
1602 static void ResetStackOpData (StackOpData* Data)
1603 /* Reset the given data structure */
1606 Data->UsedRegs = REG_NONE;
1608 ClearLoadInfo (&Data->Lhs);
1609 ClearLoadInfo (&Data->Rhs);
1611 Data->PushIndex = -1;
1617 static int PreCondOk (StackOpData* D)
1618 /* Check if the preconditions for a call to the optimizer subfunction are
1619 * satisfied. As a side effect, this function will also choose the zero page
1623 /* Check the flags */
1624 unsigned UnusedRegs = D->OptFunc->UnusedRegs;
1625 if (UnusedRegs != REG_NONE &&
1626 (GetRegInfo (D->Code, D->OpIndex+1, UnusedRegs) & UnusedRegs) != 0) {
1627 /* Cannot optimize */
1630 if ((D->OptFunc->Flags & OP_A_KNOWN) != 0 &&
1631 RegValIsUnknown (D->OpEntry->RI->In.RegA)) {
1632 /* Cannot optimize */
1635 if ((D->OptFunc->Flags & OP_X_ZERO) != 0 &&
1636 D->OpEntry->RI->In.RegX != 0) {
1637 /* Cannot optimize */
1640 if ((D->OptFunc->Flags & OP_LHS_LOAD) != 0) {
1641 if (D->Lhs.A.LoadIndex < 0 || D->Lhs.X.LoadIndex < 0) {
1642 /* Cannot optimize */
1644 } else if ((D->OptFunc->Flags & OP_LHS_LOAD_DIRECT) != 0) {
1645 if ((D->Lhs.A.Flags & D->Lhs.X.Flags & LI_DIRECT) == 0) {
1646 /* Cannot optimize */
1651 if ((D->OptFunc->Flags & OP_RHS_LOAD) != 0) {
1652 if (D->Rhs.A.LoadIndex < 0 || D->Rhs.X.LoadIndex < 0) {
1653 /* Cannot optimize */
1655 } else if ((D->OptFunc->Flags & OP_RHS_LOAD_DIRECT) != 0) {
1656 if ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) == 0) {
1657 /* Cannot optimize */
1663 /* Determine the zero page locations to use */
1664 if ((D->UsedRegs & REG_PTR1) == REG_NONE) {
1667 } else if ((D->UsedRegs & REG_SREG) == REG_NONE) {
1670 } else if ((D->UsedRegs & REG_PTR2) == REG_NONE) {
1674 /* No registers available */
1678 /* Determine if we have a basic block */
1679 return CS_IsBasicBlock (D->Code, D->PushIndex, D->OpIndex);
1684 /*****************************************************************************/
1686 /*****************************************************************************/
1690 unsigned OptStackOps (CodeSeg* S)
1691 /* Optimize operations that take operands via the stack */
1693 unsigned Changes = 0; /* Number of changes in one run */
1702 } State = Initialize;
1705 /* Generate register info */
1708 /* Remember the code segment in the info struct */
1711 /* Look for a call to pushax followed by a call to some other function
1712 * that takes it's first argument on the stack, and the second argument
1713 * in the primary register.
1714 * It depends on the code between the two if we can handle/transform the
1715 * sequence, so check this code for the following list of things:
1717 * - the range must be a basic block (one entry, one exit)
1718 * - there may not be accesses to local variables with unknown
1719 * offsets (because we have to adjust these offsets).
1720 * - no subroutine calls
1723 * Since we need a zero page register later, do also check the
1724 * intermediate code for zero page use.
1727 while (I < CS_GetEntryCount (S)) {
1729 /* Get the next entry */
1730 CodeEntry* E = CS_GetEntry (S, I);
1732 /* Actions depend on state */
1736 ResetStackOpData (&Data);
1741 /* While searching, track register load insns, so we can tell
1742 * what is in a register once pushax is encountered.
1744 if (CE_HasLabel (E)) {
1745 /* Currently we don't track across branches */
1746 ClearLoadInfo (&Data.Lhs);
1748 if (CE_IsCallTo (E, "pushax")) {
1752 /* Track load insns */
1753 TrackLoads (&Data.Lhs, E, I);
1758 /* We' found a pushax before. Search for a stack op that may
1759 * follow and in the meantime, track zeropage usage and check
1760 * for code that will disable us from translating the sequence.
1762 if (CE_HasLabel (E)) {
1763 /* Currently we don't track across branches */
1764 ClearLoadInfo (&Data.Rhs);
1766 if (E->OPC == OP65_JSR) {
1768 /* Subroutine call: Check if this is one of the functions,
1769 * we're going to replace.
1771 Data.OptFunc = FindFunc (E->Arg);
1773 /* Remember the op index and go on */
1778 } else if (!HarmlessCall (E->Arg)) {
1779 /* A call to an unkown subroutine: We need to start
1780 * over after the last pushax. Note: This will also
1781 * happen if we encounter a call to pushax!
1787 /* Track register usage */
1788 Data.UsedRegs |= (E->Use | E->Chg);
1789 TrackLoads (&Data.Rhs, E, I);
1792 } else if (E->Info & OF_STORE && (E->Chg & REG_ZP) == 0) {
1794 /* Too dangerous - there may be a change of a variable
1795 * within the sequence.
1801 } else if ((E->Use & REG_SP) != 0 &&
1802 (E->AM != AM65_ZP_INDY ||
1803 RegValIsUnknown (E->RI->In.RegY) ||
1804 E->RI->In.RegY < 2)) {
1806 /* If we are using the stack, and we don't have "indirect Y"
1807 * addressing mode, or the value of Y is unknown, or less
1808 * than two, we cannot cope with this piece of code. Having
1809 * an unknown value of Y means that we cannot correct the
1810 * stack offset, while having an offset less than two means
1811 * that the code works with the value on stack which is to
1819 /* Other stuff: Track register usage */
1820 Data.UsedRegs |= (E->Use | E->Chg);
1821 TrackLoads (&Data.Rhs, E, I);
1826 /* Track zero page location usage beyond this point */
1827 Data.UsedRegs |= GetRegInfo (S, I, REG_SREG | REG_PTR1 | REG_PTR2);
1829 /* Finalize the load info */
1830 FinalizeLoadInfo (&Data.Lhs, S);
1831 FinalizeLoadInfo (&Data.Rhs, S);
1833 /* If the Lhs loads do load from zeropage, we have to include
1834 * them into UsedRegs registers used. The Rhs loads have already
1837 if (Data.Lhs.A.LoadEntry && Data.Lhs.A.LoadEntry->AM == AM65_ZP) {
1838 Data.UsedRegs |= Data.Lhs.A.LoadEntry->Use;
1840 if (Data.Lhs.X.LoadEntry && Data.Lhs.X.LoadEntry->AM == AM65_ZP) {
1841 Data.UsedRegs |= Data.Lhs.X.LoadEntry->Use;
1844 /* Check the preconditions. If they aren't ok, reset the insn
1845 * pointer to the pushax and start over. We will loose part of
1846 * load tracking but at least a/x has probably lost between
1847 * pushax and here and will be tracked again when restarting.
1849 if (!PreCondOk (&Data)) {
1855 /* Prepare the remainder of the data structure. */
1856 Data.PrevEntry = CS_GetPrevEntry (S, Data.PushIndex);
1857 Data.PushEntry = CS_GetEntry (S, Data.PushIndex);
1858 Data.OpEntry = CS_GetEntry (S, Data.OpIndex);
1859 Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
1861 /* Adjust stack offsets to account for the upcoming removal */
1862 AdjustStackOffset (&Data, 2);
1864 /* Regenerate register info, since AdjustStackOffset changed
1869 /* Call the optimizer function */
1870 Changes += Data.OptFunc->Func (&Data);
1872 /* Regenerate register info */
1886 /* Free the register info */
1889 /* Return the number of changes made */