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 /*****************************************************************************/
48 /*****************************************************************************/
49 /* Load tracking data */
50 /*****************************************************************************/
54 /* LoadRegInfo flags set by DirectOp */
57 LI_DIRECT = 0x01, /* Direct op may be used */
58 LI_RELOAD_Y = 0x02, /* Reload index register Y */
59 LI_REMOVE = 0x04, /* Load may be removed */
62 /* Structure that tells us how to load the lhs values */
63 typedef struct LoadRegInfo LoadRegInfo;
65 int LoadIndex; /* Index of load insn, -1 if invalid */
66 CodeEntry* LoadEntry; /* The actual entry, 0 if invalid */
67 LI_FLAGS Flags; /* Tells us how to load */
68 unsigned char Offs; /* Stack offset if data is on stack */
71 /* Now combined for both registers */
72 typedef struct LoadInfo LoadInfo;
74 LoadRegInfo A; /* Info for A register */
75 LoadRegInfo X; /* Info for X register */
76 LoadRegInfo Y; /* Info for Y register */
81 /*****************************************************************************/
83 /*****************************************************************************/
87 /* Flags for the functions */
89 OP_NONE = 0x00, /* Nothing special */
90 OP_A_KNOWN = 0x01, /* Value of A must be known */
91 OP_X_ZERO = 0x02, /* X must be zero */
92 OP_LHS_LOAD = 0x04, /* Must have load insns for LHS */
93 OP_LHS_LOAD_DIRECT = 0x0C, /* Must have direct load insn for LHS */
94 OP_RHS_LOAD = 0x10, /* Must have load insns for RHS */
95 OP_RHS_LOAD_DIRECT = 0x30, /* Must have direct load insn for RHS */
98 /* Structure forward decl */
99 typedef struct StackOpData StackOpData;
101 /* Structure that describes an optimizer subfunction for a specific op */
102 typedef unsigned (*OptFunc) (StackOpData* D);
103 typedef struct OptFuncDesc OptFuncDesc;
105 const char* Name; /* Name of the replaced runtime function */
106 OptFunc Func; /* Function pointer */
107 unsigned UnusedRegs; /* Regs that must not be used later */
108 OP_FLAGS Flags; /* Flags */
111 /* Structure that holds the needed data */
113 CodeSeg* Code; /* Pointer to code segment */
114 unsigned Flags; /* Flags to remember things */
116 /* Pointer to optimizer subfunction description */
117 const OptFuncDesc* OptFunc;
119 /* ZP register usage inside the sequence */
122 /* Register load information for lhs and rhs */
126 /* Several indices of insns in the code segment */
127 int PushIndex; /* Index of call to pushax in codeseg */
128 int OpIndex; /* Index of actual operation */
130 /* Pointers to insns in the code segment */
131 CodeEntry* PrevEntry; /* Entry before the call to pushax */
132 CodeEntry* PushEntry; /* Pointer to entry with call to pushax */
133 CodeEntry* OpEntry; /* Pointer to entry with op */
134 CodeEntry* NextEntry; /* Entry after the op */
136 const char* ZPLo; /* Lo byte of zero page loc to use */
137 const char* ZPHi; /* Hi byte of zero page loc to use */
138 unsigned IP; /* Insertion point used by some routines */
143 /*****************************************************************************/
144 /* Load tracking code */
145 /*****************************************************************************/
149 static void ClearLoadRegInfo (LoadRegInfo* RI)
150 /* Clear a LoadRegInfo struct */
157 static void FinalizeLoadRegInfo (LoadRegInfo* RI, CodeSeg* S)
158 /* Prepare a LoadRegInfo struct for use */
161 if (RI->LoadIndex >= 0) {
162 RI->LoadEntry = CS_GetEntry (S, RI->LoadIndex);
171 static void ClearLoadInfo (LoadInfo* LI)
172 /* Clear a LoadInfo struct */
174 ClearLoadRegInfo (&LI->A);
175 ClearLoadRegInfo (&LI->X);
176 ClearLoadRegInfo (&LI->Y);
181 static void AdjustLoadRegInfo (LoadRegInfo* RI, int DelIndex, int Change)
182 /* Adjust a load register info struct after deleting or inserting an entry
186 CHECK (abs (Change) == 1);
189 if (DelIndex < RI->LoadIndex) {
191 } else if (DelIndex == RI->LoadIndex) {
192 /* Has been removed */
198 if (DelIndex <= RI->LoadIndex) {
206 static void FinalizeLoadInfo (LoadInfo* LI, CodeSeg* S)
207 /* Prepare a LoadInfo struct for use */
209 /* Get the entries */
210 FinalizeLoadRegInfo (&LI->A, S);
211 FinalizeLoadRegInfo (&LI->X, S);
212 FinalizeLoadRegInfo (&LI->Y, S);
217 static void AdjustLoadInfo (LoadInfo* LI, int DelIndex, int Change)
218 /* Adjust a load info struct after deleting entry with a given index */
220 AdjustLoadRegInfo (&LI->A, DelIndex, Change);
221 AdjustLoadRegInfo (&LI->X, DelIndex, Change);
222 AdjustLoadRegInfo (&LI->Y, DelIndex, Change);
227 static void TrackLoads (LoadInfo* LI, CodeEntry* E, int I)
228 /* Track loads for a code entry */
230 if (E->Info & OF_LOAD) {
231 if (E->Chg & REG_A) {
234 if (E->Chg & REG_X) {
237 if (E->Chg & REG_Y) {
240 } else if (E->Info & OF_XFR) {
242 case OP65_TAX: LI->X.LoadIndex = LI->A.LoadIndex; break;
243 case OP65_TAY: LI->Y.LoadIndex = LI->A.LoadIndex; break;
244 case OP65_TXA: LI->A.LoadIndex = LI->X.LoadIndex; break;
245 case OP65_TYA: LI->A.LoadIndex = LI->Y.LoadIndex; break;
248 } else if (CE_IsCallTo (E, "ldaxysp")) {
249 /* Both registers set, Y changed */
252 LI->Y.LoadIndex = -1;
254 if (E->Chg & REG_A) {
255 LI->A.LoadIndex = -1;
257 if (E->Chg & REG_X) {
258 LI->X.LoadIndex = -1;
260 if (E->Chg & REG_Y) {
261 LI->Y.LoadIndex = -1;
268 /*****************************************************************************/
270 /*****************************************************************************/
274 static void AdjustStackOffset (StackOpData* D, unsigned Offs)
275 /* Adjust the offset for all stack accesses in the range PushIndex to OpIndex.
276 * OpIndex is adjusted according to the insertions.
279 /* Walk over all entries */
280 int I = D->PushIndex + 1;
281 while (I < D->OpIndex) {
283 CodeEntry* E = CS_GetEntry (D->Code, I);
285 int NeedCorrection = 0;
286 if ((E->Use & REG_SP) != 0) {
288 /* Check for some things that should not happen */
289 CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
290 CHECK (strcmp (E->Arg, "sp") == 0);
292 /* We need to correct this one */
295 } else if (CE_IsCallTo (E, "ldaxysp")) {
297 /* We need to correct this one */
302 if (NeedCorrection) {
304 /* Get the code entry before this one. If it's a LDY, adjust the
307 CodeEntry* P = CS_GetPrevEntry (D->Code, I);
308 if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
310 /* The Y load is just before the stack access, adjust it */
311 CE_SetNumArg (P, P->Num - Offs);
315 /* Insert a new load instruction before the stack access */
316 const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
317 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
318 CS_InsertEntry (D->Code, X, I++);
320 /* One more inserted entries */
325 /* If we need the value of Y later, be sure to reload it */
326 if (RegYUsed (D->Code, I+1)) {
327 const char* Arg = MakeHexArg (E->RI->In.RegY);
328 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
329 CS_InsertEntry (D->Code, X, I+1);
331 /* One more inserted entries */
334 /* Skip this instruction in the next round */
343 /* If we have rhs load insns that load from stack, we'll have to adjust
344 * the offsets for these also.
346 if (D->Rhs.A.Flags & LI_RELOAD_Y) {
347 D->Rhs.A.Offs -= Offs;
349 if (D->Rhs.X.Flags & LI_RELOAD_Y) {
350 D->Rhs.X.Offs -= Offs;
356 static void InsertEntry (StackOpData* D, CodeEntry* E, int Index)
357 /* Insert a new entry. Depending on Index, D->PushIndex and D->OpIndex will
358 * be adjusted by this function.
361 /* Insert the entry into the code segment */
362 CS_InsertEntry (D->Code, E, Index);
364 /* Adjust register loads if necessary */
365 AdjustLoadInfo (&D->Lhs, Index, 1);
366 AdjustLoadInfo (&D->Rhs, Index, 1);
368 /* Adjust the indices if necessary */
369 if (D->PushEntry && Index <= D->PushIndex) {
372 if (D->OpEntry && Index <= D->OpIndex) {
379 static void DelEntry (StackOpData* D, int Index)
380 /* Delete an entry. Depending on Index, D->PushIndex and D->OpIndex will be
381 * adjusted by this function, and PushEntry/OpEntry may get invalidated.
384 /* Delete the entry from the code segment */
385 CS_DelEntry (D->Code, Index);
387 /* Adjust register loads if necessary */
388 AdjustLoadInfo (&D->Lhs, Index, -1);
389 AdjustLoadInfo (&D->Rhs, Index, -1);
391 /* Adjust the other indices if necessary */
392 if (Index < D->PushIndex) {
394 } else if (Index == D->PushIndex) {
397 if (Index < D->OpIndex) {
399 } else if (Index == D->OpIndex) {
406 static void CheckOneDirectOp (LoadRegInfo* LI, unsigned char Offs)
407 /* Check if the given entry is a lda instruction with an addressing mode
408 * that allows us to replace it by another operation (like ora). If so, we may
409 * use this location for the or and must not save the value in the zero
413 /* Get the load entry */
414 CodeEntry* E = LI->LoadEntry;
420 /* Check the load entry */
422 /* Must check the call first since addressing mode is ABS, so second
423 * "if" will catch otherwise.
425 if (CE_IsCallTo (E, "ldaxysp")) {
426 /* Same as single loads from stack. Since we must distinguish
427 * between A and X here, the necessary offset is passed to the
428 * function as a parameter.
430 LI->Offs = (unsigned char) E->RI->In.RegY - Offs;
431 LI->Flags |= (LI_DIRECT | LI_RELOAD_Y);
432 } else if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
433 /* These insns are all ok and replaceable */
434 LI->Flags |= LI_DIRECT;
435 } else if (E->AM == AM65_ZP_INDY &&
436 RegValIsKnown (E->RI->In.RegY) &&
437 strcmp (E->Arg, "sp") == 0) {
438 /* A load from the stack with known offset is also ok, but in this
439 * case we must reload the index register later. Please note that
440 * a load indirect via other zero page locations is not ok, since
441 * these locations may change between the push and the actual
444 LI->Offs = (unsigned char) E->RI->In.RegY;
445 LI->Flags |= (LI_DIRECT | LI_RELOAD_Y);
452 static void CheckDirectOp (StackOpData* D)
453 /* Check if the given entry is a lda instruction with an addressing mode
454 * that allows us to replace it by another operation (like ora). If so, we may
455 * use this location for the or and must not save the value in the zero
459 /* Check flags for all load instructions */
460 CheckOneDirectOp (&D->Lhs.A, 1);
461 CheckOneDirectOp (&D->Lhs.X, 0);
462 CheckOneDirectOp (&D->Rhs.A, 1);
463 CheckOneDirectOp (&D->Rhs.X, 0);
468 static void AddStoreA (StackOpData* D)
469 /* Add a store to zero page after the push insn */
471 CodeEntry* X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->PushEntry->LI);
472 InsertEntry (D, X, D->PushIndex+1);
477 static void AddStoreX (StackOpData* D)
478 /* Add a store to zero page after the push insn */
480 CodeEntry* X = NewCodeEntry (OP65_STX, AM65_ZP, D->ZPHi, 0, D->PushEntry->LI);
481 InsertEntry (D, X, D->PushIndex+1);
486 static void ReplacePushByStore (StackOpData* D)
487 /* Replace the call to the push subroutine by a store into the zero page
488 * location (actually, the push is not replaced, because we need it for
489 * later, but the name is still ok since the push will get removed at the
490 * end of each routine).
493 /* Store the value into the zeropage instead of pushing it. Check high
494 * byte first so that the store is later in A/X order.
496 if ((D->Lhs.X.Flags & LI_DIRECT) == 0) {
499 if ((D->Lhs.A.Flags & LI_DIRECT) == 0) {
506 static void AddOpLow (StackOpData* D, opc_t OPC, LoadInfo* LI)
507 /* Add an op for the low byte of an operator. This function honours the
508 * OP_DIRECT and OP_RELOAD_Y flags and generates the necessary instructions.
509 * All code is inserted at the current insertion point.
514 if ((LI->A.Flags & LI_DIRECT) != 0) {
515 /* Op with a variable location. If the location is on the stack, we
516 * need to reload the Y register.
518 if ((LI->A.Flags & LI_RELOAD_Y) == 0) {
521 CodeEntry* LoadA = LI->A.LoadEntry;
522 X = NewCodeEntry (OPC, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
523 InsertEntry (D, X, D->IP++);
528 const char* Arg = MakeHexArg (LI->A.Offs);
529 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
530 InsertEntry (D, X, D->IP++);
533 X = NewCodeEntry (OPC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
534 InsertEntry (D, X, D->IP++);
538 /* In both cases, we can remove the load */
539 LI->A.Flags |= LI_REMOVE;
543 /* Op with temp storage */
544 X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
545 InsertEntry (D, X, D->IP++);
552 static void AddOpHigh (StackOpData* D, opc_t OPC, LoadInfo* LI, int KeepResult)
553 /* Add an op for the high byte of an operator. Special cases (constant values
554 * or similar) have to be checked separately, the function covers only the
555 * generic case. Code is inserted at the insertion point.
562 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
563 InsertEntry (D, X, D->IP++);
567 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
568 InsertEntry (D, X, D->IP++);
570 if ((LI->X.Flags & LI_DIRECT) != 0) {
572 if ((LI->X.Flags & LI_RELOAD_Y) == 0) {
575 CodeEntry* LoadX = LI->X.LoadEntry;
576 X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
577 InsertEntry (D, X, D->IP++);
582 const char* Arg = MakeHexArg (LI->X.Offs);
583 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
584 InsertEntry (D, X, D->IP++);
587 X = NewCodeEntry (OPC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
588 InsertEntry (D, X, D->IP++);
591 /* In both cases, we can remove the load */
592 LI->X.Flags |= LI_REMOVE;
596 X = NewCodeEntry (OPC, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
597 InsertEntry (D, X, D->IP++);
602 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
603 InsertEntry (D, X, D->IP++);
606 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
607 InsertEntry (D, X, D->IP++);
613 static void RemoveRegLoads (StackOpData* D, LoadInfo* LI)
614 /* Remove register load insns */
616 /* Both registers may be loaded with one insn, but DelEntry will in this
617 * case clear the other one.
619 if (LI->A.LoadIndex >= 0 && (LI->A.Flags & LI_REMOVE)) {
620 DelEntry (D, LI->A.LoadIndex);
622 if (LI->X.LoadIndex >= 0 && (LI->X.Flags & LI_REMOVE)) {
623 DelEntry (D, LI->X.LoadIndex);
629 static void RemoveRemainders (StackOpData* D)
630 /* Remove the code that is unnecessary after translation of the sequence */
632 /* Remove the register loads for lhs and rhs */
633 RemoveRegLoads (D, &D->Lhs);
634 RemoveRegLoads (D, &D->Rhs);
636 /* Remove the push and the operator routine */
637 DelEntry (D, D->OpIndex);
638 DelEntry (D, D->PushIndex);
643 static int IsRegVar (StackOpData* D)
644 /* If the value pushed is that of a zeropage variable, replace ZPLo and ZPHi
645 * in the given StackOpData struct by the variable and return true. Otherwise
646 * leave D untouched and return false.
649 CodeEntry* LoadA = D->Lhs.A.LoadEntry;
650 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
653 /* Must have both load insns */
654 if (LoadA == 0 || LoadX == 0) {
658 /* Must be loads from zp */
659 if (LoadA->AM != AM65_ZP || LoadX->AM != AM65_ZP) {
663 /* Must be the same zp loc with high byte in X */
664 Len = strlen (LoadA->Arg);
665 if (strncmp (LoadA->Arg, LoadX->Arg, Len) != 0 ||
666 strcmp (LoadX->Arg + Len, "+1") != 0) {
670 /* Use the zero page location directly */
671 D->ZPLo = LoadA->Arg;
672 D->ZPHi = LoadX->Arg;
678 /*****************************************************************************/
679 /* Actual optimization functions */
680 /*****************************************************************************/
684 static unsigned Opt_toseqax_tosneax (StackOpData* D, const char* BoolTransformer)
685 /* Optimize the toseqax and tosneax sequences. */
690 /* Create a call to the boolean transformer function and a label for this
691 * insn. This is needed for all variants. Other insns are inserted *before*
694 X = NewCodeEntry (OP65_JSR, AM65_ABS, BoolTransformer, 0, D->OpEntry->LI);
695 InsertEntry (D, X, D->OpIndex + 1);
696 L = CS_GenLabel (D->Code, X);
698 /* If the lhs is direct (but not stack relative), encode compares with lhs
699 * effectively reverting the order (which doesn't matter for ==).
701 if ((D->Lhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
702 (D->Lhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
704 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
705 CodeEntry* LoadA = D->Lhs.A.LoadEntry;
707 D->IP = D->OpIndex+1;
710 X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
711 InsertEntry (D, X, D->IP++);
714 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
715 InsertEntry (D, X, D->IP++);
718 X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
719 InsertEntry (D, X, D->IP++);
721 /* Lhs load entries can be removed */
722 D->Lhs.X.Flags |= LI_REMOVE;
723 D->Lhs.A.Flags |= LI_REMOVE;
725 } else if ((D->Rhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
726 (D->Rhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
728 CodeEntry* LoadX = D->Rhs.X.LoadEntry;
729 CodeEntry* LoadA = D->Rhs.A.LoadEntry;
731 D->IP = D->OpIndex+1;
734 X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
735 InsertEntry (D, X, D->IP++);
738 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
739 InsertEntry (D, X, D->IP++);
742 X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
743 InsertEntry (D, X, D->IP++);
745 /* Rhs load entries can be removed */
746 D->Rhs.X.Flags |= LI_REMOVE;
747 D->Rhs.A.Flags |= LI_REMOVE;
749 } else if ((D->Rhs.A.Flags & LI_DIRECT) != 0 &&
750 (D->Rhs.X.Flags & LI_DIRECT) != 0) {
752 D->IP = D->OpIndex+1;
754 /* Add operand for low byte */
755 AddOpLow (D, OP65_CMP, &D->Rhs);
758 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
759 InsertEntry (D, X, D->IP++);
761 /* Add operand for high byte */
762 AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
766 /* Save lhs into zeropage, then compare */
770 D->IP = D->OpIndex+1;
773 X = NewCodeEntry (OP65_CPX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
774 InsertEntry (D, X, D->IP++);
777 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
778 InsertEntry (D, X, D->IP++);
781 X = NewCodeEntry (OP65_CMP, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
782 InsertEntry (D, X, D->IP++);
786 /* Remove the push and the call to the tosgeax function */
787 RemoveRemainders (D);
789 /* We changed the sequence */
795 static unsigned Opt___bzero (StackOpData* D)
796 /* Optimize the __bzero sequence */
802 /* Check if we're using a register variable */
804 /* Store the value into the zeropage instead of pushing it */
809 /* If the return value of __bzero is used, we have to add code to reload
810 * a/x from the pointer variable.
812 if (RegAXUsed (D->Code, D->OpIndex+1)) {
813 X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
814 InsertEntry (D, X, D->OpIndex+1);
815 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
816 InsertEntry (D, X, D->OpIndex+2);
819 /* X is always zero, A contains the size of the data area to zero.
820 * Note: A may be zero, in which case the operation is null op.
822 if (D->OpEntry->RI->In.RegA != 0) {
825 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
826 InsertEntry (D, X, D->OpIndex+1);
828 /* The value of A is known */
829 if (D->OpEntry->RI->In.RegA <= 0x81) {
831 /* Loop using the sign bit */
834 Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
835 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
836 InsertEntry (D, X, D->OpIndex+2);
839 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
840 InsertEntry (D, X, D->OpIndex+3);
841 L = CS_GenLabel (D->Code, X);
844 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, D->OpEntry->LI);
845 InsertEntry (D, X, D->OpIndex+4);
848 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, D->OpEntry->LI);
849 InsertEntry (D, X, D->OpIndex+5);
853 /* Loop using an explicit compare */
856 X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
857 InsertEntry (D, X, D->OpIndex+2);
860 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
861 InsertEntry (D, X, D->OpIndex+3);
862 L = CS_GenLabel (D->Code, X);
865 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
866 InsertEntry (D, X, D->OpIndex+4);
869 Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
870 X = NewCodeEntry (OP65_CPY, AM65_IMM, Arg, 0, D->OpEntry->LI);
871 InsertEntry (D, X, D->OpIndex+5);
874 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
875 InsertEntry (D, X, D->OpIndex+6);
880 /* Remove the push and the call to the __bzero function */
881 RemoveRemainders (D);
883 /* We changed the sequence */
889 static unsigned Opt_staspidx (StackOpData* D)
890 /* Optimize the staspidx sequence */
894 /* Check if we're using a register variable */
896 /* Store the value into the zeropage instead of pushing it */
901 /* Replace the store subroutine call by a direct op */
902 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
903 InsertEntry (D, X, D->OpIndex+1);
905 /* Remove the push and the call to the staspidx function */
906 RemoveRemainders (D);
908 /* We changed the sequence */
914 static unsigned Opt_staxspidx (StackOpData* D)
915 /* Optimize the staxspidx sequence */
919 /* Check if we're using a register variable */
921 /* Store the value into the zeropage instead of pushing it */
926 /* Inline the store */
929 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
930 InsertEntry (D, X, D->OpIndex+1);
932 if (RegValIsKnown (D->OpEntry->RI->In.RegY)) {
933 /* Value of Y is known */
934 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
935 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
937 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
939 InsertEntry (D, X, D->OpIndex+2);
941 if (RegValIsKnown (D->OpEntry->RI->In.RegX)) {
942 /* Value of X is known */
943 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
944 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
947 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
949 InsertEntry (D, X, D->OpIndex+3);
952 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
953 InsertEntry (D, X, D->OpIndex+4);
955 /* If we remove staxspidx, we must restore the Y register to what the
956 * function would return.
958 X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
959 InsertEntry (D, X, D->OpIndex+5);
961 /* Remove the push and the call to the staxspidx function */
962 RemoveRemainders (D);
964 /* We changed the sequence */
970 static unsigned Opt_tosaddax (StackOpData* D)
971 /* Optimize the tosaddax sequence */
976 /* We need the entry behind the add */
977 CHECK (D->NextEntry != 0);
979 /* Check if the X register is known and zero when the add is done, and
980 * if the add is followed by
983 * jsr ldauidx ; or ldaidx
985 * If this is true, the addition does actually add an offset to a pointer
986 * before it is dereferenced. Since both subroutines take an offset in Y,
987 * we can pass the offset (instead of #$00) and remove the addition
990 if (D->OpEntry->RI->In.RegX == 0 &&
991 D->NextEntry->OPC == OP65_LDY &&
992 CE_IsKnownImm (D->NextEntry, 0) &&
993 !CE_HasLabel (D->NextEntry) &&
994 (N = CS_GetNextEntry (D->Code, D->OpIndex + 1)) != 0 &&
995 (CE_IsCallTo (N, "ldauidx") ||
996 CE_IsCallTo (N, "ldaidx"))) {
998 int Signed = (strcmp (N->Arg, "ldaidx") == 0);
1000 /* Store the value into the zeropage instead of pushing it */
1004 /* Replace the ldy by a tay. Be sure to create the new entry before
1005 * deleting the ldy, since we will reference the line info from this
1008 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->NextEntry->LI);
1009 DelEntry (D, D->OpIndex + 1);
1010 InsertEntry (D, X, D->OpIndex + 1);
1012 /* Replace the call to ldaidx/ldauidx. Since X is already zero, and
1013 * the ptr is in the zero page location, we just need to load from
1014 * the pointer, and fix X in case of ldaidx.
1016 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, D->ZPLo, 0, N->LI);
1017 DelEntry (D, D->OpIndex + 2);
1018 InsertEntry (D, X, D->OpIndex + 2);
1023 /* Add sign extension - N is unused now */
1024 N = CS_GetNextEntry (D->Code, D->OpIndex + 2);
1026 L = CS_GenLabel (D->Code, N);
1028 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, X->LI);
1029 InsertEntry (D, X, D->OpIndex + 3);
1031 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, X->LI);
1032 InsertEntry (D, X, D->OpIndex + 4);
1037 /* Store the value into the zeropage instead of pushing it */
1038 ReplacePushByStore (D);
1040 /* Inline the add */
1041 D->IP = D->OpIndex+1;
1044 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, D->OpEntry->LI);
1045 InsertEntry (D, X, D->IP++);
1048 AddOpLow (D, OP65_ADC, &D->Lhs);
1051 if (D->PushEntry->RI->In.RegX == 0) {
1053 /* The high byte is the value in X plus the carry */
1054 CodeLabel* L = CS_GenLabel (D->Code, D->NextEntry);
1057 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1058 InsertEntry (D, X, D->IP++);
1061 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
1062 InsertEntry (D, X, D->IP++);
1064 } else if (D->OpEntry->RI->In.RegX == 0 &&
1065 (RegValIsKnown (D->PushEntry->RI->In.RegX) ||
1066 (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
1068 /* The high byte is that of the first operand plus carry */
1070 if (RegValIsKnown (D->PushEntry->RI->In.RegX)) {
1071 /* Value of first op high byte is known */
1072 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX);
1073 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
1075 /* Value of first op high byte is unknown. Load from ZP or
1078 if (D->Lhs.X.Flags & LI_DIRECT) {
1079 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
1080 X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
1082 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
1085 InsertEntry (D, X, D->IP++);
1088 L = CS_GenLabel (D->Code, D->NextEntry);
1089 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1090 InsertEntry (D, X, D->IP++);
1093 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
1094 InsertEntry (D, X, D->IP++);
1096 /* High byte is unknown */
1097 AddOpHigh (D, OP65_ADC, &D->Lhs, 1);
1101 /* Remove the push and the call to the tosaddax function */
1102 RemoveRemainders (D);
1104 /* We changed the sequence */
1110 static unsigned Opt_tosandax (StackOpData* D)
1111 /* Optimize the tosandax sequence */
1113 /* Store the value into the zeropage instead of pushing it */
1114 ReplacePushByStore (D);
1116 /* Inline the and, low byte */
1117 D->IP = D->OpIndex + 1;
1118 AddOpLow (D, OP65_AND, &D->Lhs);
1121 AddOpHigh (D, OP65_AND, &D->Lhs, 1);
1123 /* Remove the push and the call to the tosandax function */
1124 RemoveRemainders (D);
1126 /* We changed the sequence */
1132 static unsigned Opt_toseqax (StackOpData* D)
1133 /* Optimize the toseqax sequence */
1135 return Opt_toseqax_tosneax (D, "booleq");
1140 static unsigned Opt_tosgeax (StackOpData* D)
1141 /* Optimize the tosgeax sequence */
1146 /* Inline the sbc */
1147 D->IP = D->OpIndex+1;
1149 /* Must be true because of OP_RHS_LOAD */
1150 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1152 /* Add code for low operand */
1153 AddOpLow (D, OP65_CMP, &D->Rhs);
1155 /* Add code for high operand */
1156 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1159 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
1160 InsertEntry (D, X, D->IP++);
1163 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, D->OpEntry->LI);
1164 InsertEntry (D, X, D->IP++);
1165 L = CS_GenLabel (D->Code, X);
1167 /* Insert a bvs L before the eor insn */
1168 X = NewCodeEntry (OP65_BVS, AM65_BRA, L->Name, L, D->OpEntry->LI);
1169 InsertEntry (D, X, D->IP - 2);
1173 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1174 InsertEntry (D, X, D->IP++);
1177 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1178 InsertEntry (D, X, D->IP++);
1181 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1182 InsertEntry (D, X, D->IP++);
1184 /* Remove the push and the call to the tosgeax function */
1185 RemoveRemainders (D);
1187 /* We changed the sequence */
1193 static unsigned Opt_tosltax (StackOpData* D)
1194 /* Optimize the tosltax sequence */
1200 /* Inline the sbc */
1201 D->IP = D->OpIndex+1;
1203 /* Must be true because of OP_RHS_LOAD */
1204 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1206 /* Add code for low operand */
1207 AddOpLow (D, OP65_CMP, &D->Rhs);
1209 /* Add code for high operand */
1210 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1213 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
1214 InsertEntry (D, X, D->IP++);
1217 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, D->OpEntry->LI);
1218 InsertEntry (D, X, D->IP++);
1219 L = CS_GenLabel (D->Code, X);
1221 /* Insert a bvc L before the eor insn */
1222 X = NewCodeEntry (OP65_BVC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1223 InsertEntry (D, X, D->IP - 2);
1227 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1228 InsertEntry (D, X, D->IP++);
1231 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1232 InsertEntry (D, X, D->IP++);
1235 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1236 InsertEntry (D, X, D->IP++);
1238 /* Remove the push and the call to the tosltax function */
1239 RemoveRemainders (D);
1241 /* We changed the sequence */
1247 static unsigned Opt_tosneax (StackOpData* D)
1248 /* Optimize the tosneax sequence */
1250 return Opt_toseqax_tosneax (D, "boolne");
1255 static unsigned Opt_tosorax (StackOpData* D)
1256 /* Optimize the tosorax sequence */
1258 /* Store the value into the zeropage instead of pushing it */
1259 ReplacePushByStore (D);
1261 /* Inline the or, low byte */
1262 D->IP = D->OpIndex + 1;
1263 AddOpLow (D, OP65_ORA, &D->Lhs);
1266 AddOpHigh (D, OP65_ORA, &D->Lhs, 1);
1268 /* Remove the push and the call to the tosorax function */
1269 RemoveRemainders (D);
1271 /* We changed the sequence */
1277 static unsigned Opt_tossubax (StackOpData* D)
1278 /* Optimize the tossubax sequence. Note: subtraction is not commutative! */
1283 /* Inline the sbc */
1284 D->IP = D->OpIndex+1;
1287 X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
1288 InsertEntry (D, X, D->IP++);
1290 /* Must be true because of OP_RHS_LOAD */
1291 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1293 /* Add code for low operand */
1294 AddOpLow (D, OP65_SBC, &D->Rhs);
1296 /* Add code for high operand */
1297 AddOpHigh (D, OP65_SBC, &D->Rhs, 1);
1299 /* Remove the push and the call to the tossubax function */
1300 RemoveRemainders (D);
1302 /* We changed the sequence */
1308 static unsigned Opt_tosugeax (StackOpData* D)
1309 /* Optimize the tosugeax sequence */
1314 /* Inline the sbc */
1315 D->IP = D->OpIndex+1;
1317 /* Must be true because of OP_RHS_LOAD */
1318 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1320 /* Add code for low operand */
1321 AddOpLow (D, OP65_CMP, &D->Rhs);
1323 /* Add code for high operand */
1324 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1327 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1328 InsertEntry (D, X, D->IP++);
1331 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1332 InsertEntry (D, X, D->IP++);
1335 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1336 InsertEntry (D, X, D->IP++);
1338 /* Remove the push and the call to the tosugeax function */
1339 RemoveRemainders (D);
1341 /* We changed the sequence */
1347 static unsigned Opt_tosxorax (StackOpData* D)
1348 /* Optimize the tosxorax sequence */
1353 /* Store the value into the zeropage instead of pushing it */
1354 ReplacePushByStore (D);
1356 /* Inline the xor, low byte */
1357 D->IP = D->OpIndex + 1;
1358 AddOpLow (D, OP65_EOR, &D->Lhs);
1361 if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
1362 RegValIsKnown (D->OpEntry->RI->In.RegX)) {
1363 /* Both values known, precalculate the result */
1364 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
1365 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
1366 InsertEntry (D, X, D->IP++);
1367 } else if (D->PushEntry->RI->In.RegX != 0) {
1368 /* High byte is unknown */
1369 AddOpHigh (D, OP65_EOR, &D->Lhs, 1);
1372 /* Remove the push and the call to the tosandax function */
1373 RemoveRemainders (D);
1375 /* We changed the sequence */
1381 /*****************************************************************************/
1383 /*****************************************************************************/
1387 static const OptFuncDesc FuncTable[] = {
1388 { "__bzero", Opt___bzero, REG_NONE, OP_X_ZERO | OP_A_KNOWN },
1389 { "staspidx", Opt_staspidx, REG_NONE, OP_NONE },
1390 { "staxspidx", Opt_staxspidx, REG_AX, OP_NONE },
1391 { "tosaddax", Opt_tosaddax, REG_NONE, OP_NONE },
1392 { "tosandax", Opt_tosandax, REG_NONE, OP_NONE },
1393 { "toseqax", Opt_toseqax, REG_NONE, OP_NONE },
1394 { "tosgeax", Opt_tosgeax, REG_NONE, OP_RHS_LOAD_DIRECT },
1395 { "tosltax", Opt_tosltax, REG_NONE, OP_RHS_LOAD_DIRECT },
1396 { "tosneax", Opt_tosneax, REG_NONE, OP_NONE },
1397 { "tosorax", Opt_tosorax, REG_NONE, OP_NONE },
1398 { "tossubax", Opt_tossubax, REG_NONE, OP_RHS_LOAD_DIRECT },
1399 { "tosugeax", Opt_tosugeax, REG_NONE, OP_RHS_LOAD_DIRECT },
1400 { "tosxorax", Opt_tosxorax, REG_NONE, OP_NONE },
1402 #define FUNC_COUNT (sizeof(FuncTable) / sizeof(FuncTable[0]))
1406 static int CmpFunc (const void* Key, const void* Func)
1407 /* Compare function for bsearch */
1409 return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
1414 static const OptFuncDesc* FindFunc (const char* Name)
1415 /* Find the function with the given name. Return a pointer to the table entry
1416 * or NULL if the function was not found.
1419 return bsearch (Name, FuncTable, FUNC_COUNT, sizeof(OptFuncDesc), CmpFunc);
1424 static int CmpHarmless (const void* Key, const void* Entry)
1425 /* Compare function for bsearch */
1427 return strcmp (Key, *(const char**)Entry);
1432 static int HarmlessCall (const char* Name)
1433 /* Check if this is a call to a harmless subroutine that will not interrupt
1434 * the pushax/op sequence when encountered.
1437 static const char* Tab[] = {
1460 void* R = bsearch (Name,
1462 sizeof (Tab) / sizeof (Tab[0]),
1470 static void ResetStackOpData (StackOpData* Data)
1471 /* Reset the given data structure */
1474 Data->UsedRegs = REG_NONE;
1476 ClearLoadInfo (&Data->Lhs);
1477 ClearLoadInfo (&Data->Rhs);
1479 Data->PushIndex = -1;
1485 static int PreCondOk (StackOpData* D)
1486 /* Check if the preconditions for a call to the optimizer subfunction are
1487 * satisfied. As a side effect, this function will also choose the zero page
1491 /* Check the flags */
1492 unsigned UnusedRegs = D->OptFunc->UnusedRegs;
1493 if (UnusedRegs != REG_NONE &&
1494 (GetRegInfo (D->Code, D->OpIndex+1, UnusedRegs) & UnusedRegs) != 0) {
1495 /* Cannot optimize */
1498 if ((D->OptFunc->Flags & OP_A_KNOWN) != 0 &&
1499 RegValIsUnknown (D->OpEntry->RI->In.RegA)) {
1500 /* Cannot optimize */
1503 if ((D->OptFunc->Flags & OP_X_ZERO) != 0 &&
1504 D->OpEntry->RI->In.RegX != 0) {
1505 /* Cannot optimize */
1508 if ((D->OptFunc->Flags & OP_LHS_LOAD) != 0) {
1509 if (D->Lhs.A.LoadIndex < 0 || D->Lhs.X.LoadIndex < 0) {
1510 /* Cannot optimize */
1512 } else if ((D->OptFunc->Flags & OP_LHS_LOAD_DIRECT) != 0) {
1513 if ((D->Lhs.A.Flags & D->Lhs.X.Flags & LI_DIRECT) == 0) {
1514 /* Cannot optimize */
1519 if ((D->OptFunc->Flags & OP_RHS_LOAD) != 0) {
1520 if (D->Rhs.A.LoadIndex < 0 || D->Rhs.X.LoadIndex < 0) {
1521 /* Cannot optimize */
1523 } else if ((D->OptFunc->Flags & OP_RHS_LOAD_DIRECT) != 0) {
1524 if ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) == 0) {
1525 /* Cannot optimize */
1531 /* Determine the zero page locations to use */
1532 if ((D->UsedRegs & REG_PTR1) == REG_NONE) {
1535 } else if ((D->UsedRegs & REG_SREG) == REG_NONE) {
1538 } else if ((D->UsedRegs & REG_PTR2) == REG_NONE) {
1542 /* No registers available */
1546 /* Determine if we have a basic block */
1547 return CS_IsBasicBlock (D->Code, D->PushIndex, D->OpIndex);
1552 /*****************************************************************************/
1554 /*****************************************************************************/
1558 unsigned OptStackOps (CodeSeg* S)
1559 /* Optimize operations that take operands via the stack */
1561 unsigned Changes = 0; /* Number of changes in one run */
1570 } State = Initialize;
1573 /* Generate register info */
1576 /* Remember the code segment in the info struct */
1579 /* Look for a call to pushax followed by a call to some other function
1580 * that takes it's first argument on the stack, and the second argument
1581 * in the primary register.
1582 * It depends on the code between the two if we can handle/transform the
1583 * sequence, so check this code for the following list of things:
1585 * - the range must be a basic block (one entry, one exit)
1586 * - there may not be accesses to local variables with unknown
1587 * offsets (because we have to adjust these offsets).
1588 * - no subroutine calls
1591 * Since we need a zero page register later, do also check the
1592 * intermediate code for zero page use.
1595 while (I < CS_GetEntryCount (S)) {
1597 /* Get the next entry */
1598 CodeEntry* E = CS_GetEntry (S, I);
1600 /* Actions depend on state */
1604 ResetStackOpData (&Data);
1609 /* While searching, track register load insns, so we can tell
1610 * what is in a register once pushax is encountered.
1612 if (CE_IsCallTo (E, "pushax")) {
1616 /* Track load insns */
1617 TrackLoads (&Data.Lhs, E, I);
1622 /* We' found a pushax before. Search for a stack op that may
1623 * follow and in the meantime, track zeropage usage and check
1624 * for code that will disable us from translating the sequence.
1626 if (E->OPC == OP65_JSR) {
1628 /* Subroutine call: Check if this is one of the functions,
1629 * we're going to replace.
1631 Data.OptFunc = FindFunc (E->Arg);
1633 /* Remember the op index and go on */
1638 } else if (!HarmlessCall (E->Arg)) {
1639 /* A call to an unkown subroutine: We need to start
1640 * over after the last pushax. Note: This will also
1641 * happen if we encounter a call to pushax!
1647 /* Track register usage */
1648 Data.UsedRegs |= (E->Use | E->Chg);
1649 TrackLoads (&Data.Rhs, E, I);
1652 } else if (E->Info & OF_STORE && (E->Chg & REG_ZP) == 0) {
1654 /* Too dangerous - there may be a change of a variable
1655 * within the sequence.
1661 } else if ((E->Use & REG_SP) != 0 &&
1662 (E->AM != AM65_ZP_INDY ||
1663 RegValIsUnknown (E->RI->In.RegY) ||
1664 E->RI->In.RegY < 2)) {
1666 /* If we are using the stack, and we don't have "indirect Y"
1667 * addressing mode, or the value of Y is unknown, or less
1668 * than two, we cannot cope with this piece of code. Having
1669 * an unknown value of Y means that we cannot correct the
1670 * stack offset, while having an offset less than two means
1671 * that the code works with the value on stack which is to
1679 /* Other stuff: Track register usage */
1680 Data.UsedRegs |= (E->Use | E->Chg);
1681 TrackLoads (&Data.Rhs, E, I);
1686 /* Track zero page location usage beyond this point */
1687 Data.UsedRegs |= GetRegInfo (S, I, REG_SREG | REG_PTR1 | REG_PTR2);
1689 /* Finalize the load info */
1690 FinalizeLoadInfo (&Data.Lhs, S);
1691 FinalizeLoadInfo (&Data.Rhs, S);
1693 /* Set flags for direct operations */
1694 CheckDirectOp (&Data);
1696 /* If the Lhs loads do load from zeropage, we have to include
1697 * them into UsedRegs registers used. The Rhs loads have already
1700 if (Data.Lhs.A.LoadEntry && Data.Lhs.A.LoadEntry->AM == AM65_ZP) {
1701 Data.UsedRegs |= Data.Lhs.A.LoadEntry->Use;
1703 if (Data.Lhs.X.LoadEntry && Data.Lhs.X.LoadEntry->AM == AM65_ZP) {
1704 Data.UsedRegs |= Data.Lhs.X.LoadEntry->Use;
1707 /* Check the preconditions. If they aren't ok, reset the insn
1708 * pointer to the pushax and start over. We will loose part of
1709 * load tracking but at least a/x has probably lost between
1710 * pushax and here and will be tracked again when restarting.
1712 if (!PreCondOk (&Data)) {
1718 /* Adjust stack offsets to account for the upcoming removal */
1719 AdjustStackOffset (&Data, 2);
1721 /* Regenerate register info, since AdjustStackOffset changed
1726 /* Prepare the remainder of the data structure. */
1727 Data.PrevEntry = CS_GetPrevEntry (S, Data.PushIndex);
1728 Data.PushEntry = CS_GetEntry (S, Data.PushIndex);
1729 Data.OpEntry = CS_GetEntry (S, Data.OpIndex);
1730 Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
1732 /* Call the optimizer function */
1733 Changes += Data.OptFunc->Func (&Data);
1735 /* Regenerate register info */
1749 /* Free the register info */
1752 /* Return the number of changes made */