1 /*****************************************************************************/
5 /* Optimize operations that take operands via the stack */
9 /* (C) 2001-2013, 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 */
61 LI_DONT_REMOVE = 0x08, /* Load may not be removed */
62 LI_DUP_LOAD = 0x10, /* Duplicate load */
65 /* Structure that tells us how to load the lhs values */
66 typedef struct LoadRegInfo LoadRegInfo;
68 LI_FLAGS Flags; /* Tells us how to load */
69 int LoadIndex; /* Index of load insn, -1 if invalid */
70 CodeEntry* LoadEntry; /* The actual entry, 0 if invalid */
71 int XferIndex; /* Index of transfer insn */
72 CodeEntry* XferEntry; /* The actual transfer entry */
73 int Offs; /* Stack offset if data is on stack */
76 /* Now combined for both registers */
77 typedef struct LoadInfo LoadInfo;
79 LoadRegInfo A; /* Info for A register */
80 LoadRegInfo X; /* Info for X register */
81 LoadRegInfo Y; /* Info for Y register */
86 /*****************************************************************************/
88 /*****************************************************************************/
92 /* Flags for the functions */
94 OP_NONE = 0x00, /* Nothing special */
95 OP_A_KNOWN = 0x01, /* Value of A must be known */
96 OP_X_ZERO = 0x02, /* X must be zero */
97 OP_LHS_LOAD = 0x04, /* Must have load insns for LHS */
98 OP_LHS_LOAD_DIRECT = 0x0C, /* Must have direct load insn for LHS */
99 OP_RHS_LOAD = 0x10, /* Must have load insns for RHS */
100 OP_RHS_LOAD_DIRECT = 0x30, /* Must have direct load insn for RHS */
103 /* Structure forward decl */
104 typedef struct StackOpData StackOpData;
106 /* Structure that describes an optimizer subfunction for a specific op */
107 typedef unsigned (*OptFunc) (StackOpData* D);
108 typedef struct OptFuncDesc OptFuncDesc;
110 const char* Name; /* Name of the replaced runtime function */
111 OptFunc Func; /* Function pointer */
112 unsigned UnusedRegs; /* Regs that must not be used later */
113 OP_FLAGS Flags; /* Flags */
116 /* Structure that holds the needed data */
118 CodeSeg* Code; /* Pointer to code segment */
119 unsigned Flags; /* Flags to remember things */
121 /* Pointer to optimizer subfunction description */
122 const OptFuncDesc* OptFunc;
124 /* ZP register usage inside the sequence */
127 /* Register load information for lhs and rhs */
131 /* Several indices of insns in the code segment */
132 int PushIndex; /* Index of call to pushax in codeseg */
133 int OpIndex; /* Index of actual operation */
135 /* Pointers to insns in the code segment */
136 CodeEntry* PrevEntry; /* Entry before the call to pushax */
137 CodeEntry* PushEntry; /* Pointer to entry with call to pushax */
138 CodeEntry* OpEntry; /* Pointer to entry with op */
139 CodeEntry* NextEntry; /* Entry after the op */
141 const char* ZPLo; /* Lo byte of zero page loc to use */
142 const char* ZPHi; /* Hi byte of zero page loc to use */
143 unsigned IP; /* Insertion point used by some routines */
148 /*****************************************************************************/
149 /* Load tracking code */
150 /*****************************************************************************/
154 static void ClearLoadRegInfo (LoadRegInfo* RI)
155 /* Clear a LoadRegInfo struct */
165 static void FinalizeLoadRegInfo (LoadRegInfo* RI, CodeSeg* S)
166 /* Prepare a LoadRegInfo struct for use */
168 /* Get the entries */
169 if (RI->LoadIndex >= 0) {
170 RI->LoadEntry = CS_GetEntry (S, RI->LoadIndex);
174 if (RI->XferIndex >= 0) {
175 RI->XferEntry = CS_GetEntry (S, RI->XferIndex);
183 static void ClearLoadInfo (LoadInfo* LI)
184 /* Clear a LoadInfo struct */
186 ClearLoadRegInfo (&LI->A);
187 ClearLoadRegInfo (&LI->X);
188 ClearLoadRegInfo (&LI->Y);
193 static void AdjustLoadRegInfo (LoadRegInfo* RI, int Index, int Change)
194 /* Adjust a load register info struct after deleting or inserting an entry
195 ** with a given index
198 CHECK (abs (Change) == 1);
201 if (Index < RI->LoadIndex) {
203 } else if (Index == RI->LoadIndex) {
204 /* Has been removed */
208 if (Index < RI->XferIndex) {
210 } else if (Index == RI->XferIndex) {
211 /* Has been removed */
217 if (Index <= RI->LoadIndex) {
220 if (Index <= RI->XferIndex) {
228 static void FinalizeLoadInfo (LoadInfo* LI, CodeSeg* S)
229 /* Prepare a LoadInfo struct for use */
231 /* Get the entries */
232 FinalizeLoadRegInfo (&LI->A, S);
233 FinalizeLoadRegInfo (&LI->X, S);
234 FinalizeLoadRegInfo (&LI->Y, S);
239 static void AdjustLoadInfo (LoadInfo* LI, int Index, int Change)
240 /* Adjust a load info struct after deleting entry with a given index */
242 AdjustLoadRegInfo (&LI->A, Index, Change);
243 AdjustLoadRegInfo (&LI->X, Index, Change);
244 AdjustLoadRegInfo (&LI->Y, Index, Change);
249 static void HonourUseAndChg (LoadRegInfo* RI, unsigned Reg, const CodeEntry* E)
250 /* Honour use and change flags for an instruction */
253 ClearLoadRegInfo (RI);
254 } else if ((E->Use & Reg) && RI->LoadIndex >= 0) {
255 RI->Flags |= LI_DONT_REMOVE;
261 static void TrackLoads (LoadInfo* LI, CodeEntry* E, int I)
262 /* Track loads for a code entry */
264 if (E->Info & OF_LOAD) {
268 /* Determine, which register was loaded */
269 if (E->Chg & REG_A) {
271 } else if (E->Chg & REG_X) {
273 } else if (E->Chg & REG_Y) {
278 /* If we had a load or xfer op before, this is a duplicate load which
279 ** can cause problems if it encountered between the pushax and the op,
282 if (RI->LoadIndex >= 0 || RI->XferIndex >= 0) {
283 RI->Flags |= LI_DUP_LOAD;
286 /* Remember the load */
291 RI->Flags &= ~(LI_DIRECT | LI_RELOAD_Y);
292 if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
293 /* These insns are all ok and replaceable */
294 RI->Flags |= LI_DIRECT;
295 } else if (E->AM == AM65_ZP_INDY &&
296 RegValIsKnown (E->RI->In.RegY) &&
297 strcmp (E->Arg, "sp") == 0) {
298 /* A load from the stack with known offset is also ok, but in this
299 ** case we must reload the index register later. Please note that
300 ** a load indirect via other zero page locations is not ok, since
301 ** these locations may change between the push and the actual
304 RI->Offs = (unsigned char) E->RI->In.RegY;
305 RI->Flags |= (LI_DIRECT | LI_RELOAD_Y);
309 } else if (E->Info & OF_XFR) {
311 /* Determine source and target of the transfer and handle the TSX insn */
315 case OP65_TAX: Src = &LI->A; Tgt = &LI->X; break;
316 case OP65_TAY: Src = &LI->A; Tgt = &LI->Y; break;
317 case OP65_TXA: Src = &LI->X; Tgt = &LI->A; break;
318 case OP65_TYA: Src = &LI->Y; Tgt = &LI->A; break;
319 case OP65_TSX: ClearLoadRegInfo (&LI->X); return;
320 case OP65_TXS: return;
321 default: Internal ("Unknown XFR insn in TrackLoads");
324 /* If we had a load or xfer op before, this is a duplicate load which
325 ** can cause problems if it encountered between the pushax and the op,
328 if (Tgt->LoadIndex >= 0 || Tgt->XferIndex >= 0) {
329 Tgt->Flags |= LI_DUP_LOAD;
332 /* Transfer the data */
333 Tgt->LoadIndex = Src->LoadIndex;
335 Tgt->Offs = Src->Offs;
336 Tgt->Flags &= ~(LI_DIRECT | LI_RELOAD_Y);
337 Tgt->Flags |= Src->Flags & (LI_DIRECT | LI_RELOAD_Y);
339 } else if (CE_IsCallTo (E, "ldaxysp") && RegValIsKnown (E->RI->In.RegY)) {
341 /* If we had a load or xfer op before, this is a duplicate load which
342 ** can cause problems if it encountered between the pushax and the op,
343 ** so remember it for both registers involved.
345 if (LI->A.LoadIndex >= 0 || LI->A.XferIndex >= 0) {
346 LI->A.Flags |= LI_DUP_LOAD;
348 if (LI->X.LoadIndex >= 0 || LI->X.XferIndex >= 0) {
349 LI->X.Flags |= LI_DUP_LOAD;
352 /* Both registers set, Y changed */
354 LI->A.XferIndex = -1;
355 LI->A.Flags |= (LI_DIRECT | LI_RELOAD_Y);
356 LI->A.Offs = (unsigned char) E->RI->In.RegY - 1;
359 LI->X.XferIndex = -1;
360 LI->X.Flags |= (LI_DIRECT | LI_RELOAD_Y);
361 LI->X.Offs = (unsigned char) E->RI->In.RegY;
363 ClearLoadRegInfo (&LI->Y);
365 HonourUseAndChg (&LI->A, REG_A, E);
366 HonourUseAndChg (&LI->X, REG_X, E);
367 HonourUseAndChg (&LI->Y, REG_Y, E);
373 /*****************************************************************************/
375 /*****************************************************************************/
379 static void InsertEntry (StackOpData* D, CodeEntry* E, int Index)
380 /* Insert a new entry. Depending on Index, D->PushIndex and D->OpIndex will
381 ** be adjusted by this function.
384 /* Insert the entry into the code segment */
385 CS_InsertEntry (D->Code, E, Index);
387 /* Adjust register loads if necessary */
388 AdjustLoadInfo (&D->Lhs, Index, 1);
389 AdjustLoadInfo (&D->Rhs, Index, 1);
391 /* Adjust the indices if necessary */
392 if (D->PushEntry && Index <= D->PushIndex) {
395 if (D->OpEntry && Index <= D->OpIndex) {
402 static void DelEntry (StackOpData* D, int Index)
403 /* Delete an entry. Depending on Index, D->PushIndex and D->OpIndex will be
404 ** adjusted by this function, and PushEntry/OpEntry may get invalidated.
407 /* Delete the entry from the code segment */
408 CS_DelEntry (D->Code, Index);
410 /* Adjust register loads if necessary */
411 AdjustLoadInfo (&D->Lhs, Index, -1);
412 AdjustLoadInfo (&D->Rhs, Index, -1);
414 /* Adjust the other indices if necessary */
415 if (Index < D->PushIndex) {
417 } else if (Index == D->PushIndex) {
420 if (Index < D->OpIndex) {
422 } else if (Index == D->OpIndex) {
429 static void AdjustStackOffset (StackOpData* D, unsigned Offs)
430 /* Adjust the offset for all stack accesses in the range PushIndex to OpIndex.
431 ** OpIndex is adjusted according to the insertions.
434 /* Walk over all entries */
435 int I = D->PushIndex + 1;
436 while (I < D->OpIndex) {
438 CodeEntry* E = CS_GetEntry (D->Code, I);
440 int NeedCorrection = 0;
441 if ((E->Use & REG_SP) != 0) {
443 /* Check for some things that should not happen */
444 CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
445 CHECK (strcmp (E->Arg, "sp") == 0);
447 /* We need to correct this one */
450 } else if (CE_IsCallTo (E, "ldaxysp")) {
452 /* We need to correct this one */
457 if (NeedCorrection) {
459 /* Get the code entry before this one. If it's a LDY, adjust the
462 CodeEntry* P = CS_GetPrevEntry (D->Code, I);
463 if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
465 /* The Y load is just before the stack access, adjust it */
466 CE_SetNumArg (P, P->Num - Offs);
470 /* Insert a new load instruction before the stack access */
471 const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
472 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
473 InsertEntry (D, X, I++);
477 /* If we need the value of Y later, be sure to reload it */
478 if (RegYUsed (D->Code, I+1)) {
479 const char* Arg = MakeHexArg (E->RI->In.RegY);
480 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
481 InsertEntry (D, X, I+1);
483 /* Skip this instruction in the next round */
492 /* If we have rhs load insns that load from stack, we'll have to adjust
493 ** the offsets for these also.
495 if (D->Rhs.A.Flags & LI_RELOAD_Y) {
496 D->Rhs.A.Offs -= Offs;
498 if (D->Rhs.X.Flags & LI_RELOAD_Y) {
499 D->Rhs.X.Offs -= Offs;
505 static void AddStoreA (StackOpData* D)
506 /* Add a store to zero page after the push insn */
508 CodeEntry* X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->PushEntry->LI);
509 InsertEntry (D, X, D->PushIndex+1);
514 static void AddStoreX (StackOpData* D)
515 /* Add a store to zero page after the push insn */
517 CodeEntry* X = NewCodeEntry (OP65_STX, AM65_ZP, D->ZPHi, 0, D->PushEntry->LI);
518 InsertEntry (D, X, D->PushIndex+1);
523 static void ReplacePushByStore (StackOpData* D)
524 /* Replace the call to the push subroutine by a store into the zero page
525 ** location (actually, the push is not replaced, because we need it for
526 ** later, but the name is still ok since the push will get removed at the
527 ** end of each routine).
530 /* Store the value into the zeropage instead of pushing it. Check high
531 ** byte first so that the store is later in A/X order.
533 if ((D->Lhs.X.Flags & LI_DIRECT) == 0) {
536 if ((D->Lhs.A.Flags & LI_DIRECT) == 0) {
543 static void AddOpLow (StackOpData* D, opc_t OPC, LoadInfo* LI)
544 /* Add an op for the low byte of an operator. This function honours the
545 ** OP_DIRECT and OP_RELOAD_Y flags and generates the necessary instructions.
546 ** All code is inserted at the current insertion point.
551 if ((LI->A.Flags & LI_DIRECT) != 0) {
552 /* Op with a variable location. If the location is on the stack, we
553 ** need to reload the Y register.
555 if ((LI->A.Flags & LI_RELOAD_Y) == 0) {
558 CodeEntry* LoadA = LI->A.LoadEntry;
559 X = NewCodeEntry (OPC, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
560 InsertEntry (D, X, D->IP++);
565 const char* Arg = MakeHexArg (LI->A.Offs);
566 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
567 InsertEntry (D, X, D->IP++);
570 X = NewCodeEntry (OPC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
571 InsertEntry (D, X, D->IP++);
575 /* In both cases, we can remove the load */
576 LI->A.Flags |= LI_REMOVE;
580 /* Op with temp storage */
581 X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
582 InsertEntry (D, X, D->IP++);
589 static void AddOpHigh (StackOpData* D, opc_t OPC, LoadInfo* LI, int KeepResult)
590 /* Add an op for the high byte of an operator. Special cases (constant values
591 ** or similar) have to be checked separately, the function covers only the
592 ** generic case. Code is inserted at the insertion point.
599 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
600 InsertEntry (D, X, D->IP++);
604 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
605 InsertEntry (D, X, D->IP++);
607 if ((LI->X.Flags & LI_DIRECT) != 0) {
609 if ((LI->X.Flags & LI_RELOAD_Y) == 0) {
612 CodeEntry* LoadX = LI->X.LoadEntry;
613 X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
614 InsertEntry (D, X, D->IP++);
619 const char* Arg = MakeHexArg (LI->X.Offs);
620 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
621 InsertEntry (D, X, D->IP++);
624 X = NewCodeEntry (OPC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
625 InsertEntry (D, X, D->IP++);
628 /* In both cases, we can remove the load */
629 LI->X.Flags |= LI_REMOVE;
633 X = NewCodeEntry (OPC, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
634 InsertEntry (D, X, D->IP++);
639 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
640 InsertEntry (D, X, D->IP++);
643 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
644 InsertEntry (D, X, D->IP++);
650 static void RemoveRegLoads (StackOpData* D, LoadInfo* LI)
651 /* Remove register load insns */
653 /* Both registers may be loaded with one insn, but DelEntry will in this
654 ** case clear the other one.
656 if ((LI->A.Flags & (LI_REMOVE | LI_DONT_REMOVE)) == LI_REMOVE) {
657 if (LI->A.LoadIndex >= 0) {
658 DelEntry (D, LI->A.LoadIndex);
660 if (LI->A.XferIndex >= 0) {
661 DelEntry (D, LI->A.XferIndex);
664 if ((LI->X.Flags & (LI_REMOVE | LI_DONT_REMOVE)) == LI_REMOVE) {
665 if (LI->X.LoadIndex >= 0) {
666 DelEntry (D, LI->X.LoadIndex);
668 if (LI->X.XferIndex >= 0) {
669 DelEntry (D, LI->X.XferIndex);
676 static void RemoveRemainders (StackOpData* D)
677 /* Remove the code that is unnecessary after translation of the sequence */
679 /* Remove the register loads for lhs and rhs */
680 RemoveRegLoads (D, &D->Lhs);
681 RemoveRegLoads (D, &D->Rhs);
683 /* Remove the push and the operator routine */
684 DelEntry (D, D->OpIndex);
685 DelEntry (D, D->PushIndex);
690 static int IsRegVar (StackOpData* D)
691 /* If the value pushed is that of a zeropage variable, replace ZPLo and ZPHi
692 ** in the given StackOpData struct by the variable and return true. Otherwise
693 ** leave D untouched and return false.
696 CodeEntry* LoadA = D->Lhs.A.LoadEntry;
697 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
700 /* Must have both load insns */
701 if (LoadA == 0 || LoadX == 0) {
705 /* Must be loads from zp */
706 if (LoadA->AM != AM65_ZP || LoadX->AM != AM65_ZP) {
710 /* Must be the same zp loc with high byte in X */
711 Len = strlen (LoadA->Arg);
712 if (strncmp (LoadA->Arg, LoadX->Arg, Len) != 0 ||
713 strcmp (LoadX->Arg + Len, "+1") != 0) {
717 /* Use the zero page location directly */
718 D->ZPLo = LoadA->Arg;
719 D->ZPHi = LoadX->Arg;
725 /*****************************************************************************/
726 /* Actual optimization functions */
727 /*****************************************************************************/
731 static unsigned Opt_toseqax_tosneax (StackOpData* D, const char* BoolTransformer)
732 /* Optimize the toseqax and tosneax sequences. */
737 /* Create a call to the boolean transformer function and a label for this
738 ** insn. This is needed for all variants. Other insns are inserted *before*
741 X = NewCodeEntry (OP65_JSR, AM65_ABS, BoolTransformer, 0, D->OpEntry->LI);
742 InsertEntry (D, X, D->OpIndex + 1);
743 L = CS_GenLabel (D->Code, X);
745 /* If the lhs is direct (but not stack relative), encode compares with lhs
746 ** effectively reverting the order (which doesn't matter for ==).
748 if ((D->Lhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
749 (D->Lhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
751 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
752 CodeEntry* LoadA = D->Lhs.A.LoadEntry;
754 D->IP = D->OpIndex+1;
757 X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
758 InsertEntry (D, X, D->IP++);
761 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
762 InsertEntry (D, X, D->IP++);
765 X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
766 InsertEntry (D, X, D->IP++);
768 /* Lhs load entries can be removed */
769 D->Lhs.X.Flags |= LI_REMOVE;
770 D->Lhs.A.Flags |= LI_REMOVE;
772 } else if ((D->Rhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
773 (D->Rhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
775 CodeEntry* LoadX = D->Rhs.X.LoadEntry;
776 CodeEntry* LoadA = D->Rhs.A.LoadEntry;
778 D->IP = D->OpIndex+1;
781 X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
782 InsertEntry (D, X, D->IP++);
785 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
786 InsertEntry (D, X, D->IP++);
789 X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
790 InsertEntry (D, X, D->IP++);
792 /* Rhs load entries can be removed */
793 D->Rhs.X.Flags |= LI_REMOVE;
794 D->Rhs.A.Flags |= LI_REMOVE;
796 } else if ((D->Rhs.A.Flags & LI_DIRECT) != 0 &&
797 (D->Rhs.X.Flags & LI_DIRECT) != 0) {
799 D->IP = D->OpIndex+1;
801 /* Add operand for low byte */
802 AddOpLow (D, OP65_CMP, &D->Rhs);
805 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
806 InsertEntry (D, X, D->IP++);
808 /* Add operand for high byte */
809 AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
813 /* Save lhs into zeropage, then compare */
817 D->IP = D->OpIndex+1;
820 X = NewCodeEntry (OP65_CPX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
821 InsertEntry (D, X, D->IP++);
824 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
825 InsertEntry (D, X, D->IP++);
828 X = NewCodeEntry (OP65_CMP, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
829 InsertEntry (D, X, D->IP++);
833 /* Remove the push and the call to the tosgeax function */
834 RemoveRemainders (D);
836 /* We changed the sequence */
842 static unsigned Opt_tosshift (StackOpData* D, const char* Name)
843 /* Optimize shift sequences. */
847 /* Store the value into the zeropage instead of pushing it */
848 ReplacePushByStore (D);
850 /* If the lhs is direct (but not stack relative), we can just reload the
853 if ((D->Lhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
854 (D->Lhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
856 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
857 CodeEntry* LoadA = D->Lhs.A.LoadEntry;
859 /* Inline the shift */
860 D->IP = D->OpIndex+1;
863 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->OpEntry->LI);
864 InsertEntry (D, X, D->IP++);
867 X = NewCodeEntry (OP65_LDA, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
868 InsertEntry (D, X, D->IP++);
871 X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
872 InsertEntry (D, X, D->IP++);
874 /* Lhs load entries can be removed */
875 D->Lhs.X.Flags |= LI_REMOVE;
876 D->Lhs.A.Flags |= LI_REMOVE;
880 /* Save lhs into zeropage and reload later */
884 /* Be sure to setup IP after adding the stores, otherwise it will get
887 D->IP = D->OpIndex+1;
890 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->OpEntry->LI);
891 InsertEntry (D, X, D->IP++);
894 X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
895 InsertEntry (D, X, D->IP++);
898 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
899 InsertEntry (D, X, D->IP++);
903 /* jsr shlaxy/aslaxy/whatever */
904 X = NewCodeEntry (OP65_JSR, AM65_ABS, Name, 0, D->OpEntry->LI);
905 InsertEntry (D, X, D->IP++);
907 /* Remove the push and the call to the shift function */
908 RemoveRemainders (D);
910 /* We changed the sequence */
916 static unsigned Opt___bzero (StackOpData* D)
917 /* Optimize the __bzero sequence */
923 /* Check if we're using a register variable */
925 /* Store the value into the zeropage instead of pushing it */
930 /* If the return value of __bzero is used, we have to add code to reload
931 ** a/x from the pointer variable.
933 if (RegAXUsed (D->Code, D->OpIndex+1)) {
934 X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
935 InsertEntry (D, X, D->OpIndex+1);
936 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
937 InsertEntry (D, X, D->OpIndex+2);
940 /* X is always zero, A contains the size of the data area to zero.
941 ** Note: A may be zero, in which case the operation is null op.
943 if (D->OpEntry->RI->In.RegA != 0) {
946 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
947 InsertEntry (D, X, D->OpIndex+1);
949 /* The value of A is known */
950 if (D->OpEntry->RI->In.RegA <= 0x81) {
952 /* Loop using the sign bit */
955 Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
956 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
957 InsertEntry (D, X, D->OpIndex+2);
960 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
961 InsertEntry (D, X, D->OpIndex+3);
962 L = CS_GenLabel (D->Code, X);
965 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, D->OpEntry->LI);
966 InsertEntry (D, X, D->OpIndex+4);
969 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, D->OpEntry->LI);
970 InsertEntry (D, X, D->OpIndex+5);
974 /* Loop using an explicit compare */
977 X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
978 InsertEntry (D, X, D->OpIndex+2);
981 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
982 InsertEntry (D, X, D->OpIndex+3);
983 L = CS_GenLabel (D->Code, X);
986 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
987 InsertEntry (D, X, D->OpIndex+4);
990 Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
991 X = NewCodeEntry (OP65_CPY, AM65_IMM, Arg, 0, D->OpEntry->LI);
992 InsertEntry (D, X, D->OpIndex+5);
995 X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
996 InsertEntry (D, X, D->OpIndex+6);
1001 /* Remove the push and the call to the __bzero function */
1002 RemoveRemainders (D);
1004 /* We changed the sequence */
1010 static unsigned Opt_staspidx (StackOpData* D)
1011 /* Optimize the staspidx sequence */
1015 /* Check if we're using a register variable */
1016 if (!IsRegVar (D)) {
1017 /* Store the value into the zeropage instead of pushing it */
1022 /* Replace the store subroutine call by a direct op */
1023 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
1024 InsertEntry (D, X, D->OpIndex+1);
1026 /* Remove the push and the call to the staspidx function */
1027 RemoveRemainders (D);
1029 /* We changed the sequence */
1035 static unsigned Opt_staxspidx (StackOpData* D)
1036 /* Optimize the staxspidx sequence */
1040 /* Check if we're using a register variable */
1041 if (!IsRegVar (D)) {
1042 /* Store the value into the zeropage instead of pushing it */
1047 /* Inline the store */
1050 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
1051 InsertEntry (D, X, D->OpIndex+1);
1053 if (RegValIsKnown (D->OpEntry->RI->In.RegY)) {
1054 /* Value of Y is known */
1055 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
1056 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
1058 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
1060 InsertEntry (D, X, D->OpIndex+2);
1062 if (RegValIsKnown (D->OpEntry->RI->In.RegX)) {
1063 /* Value of X is known */
1064 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
1065 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
1068 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
1070 InsertEntry (D, X, D->OpIndex+3);
1073 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
1074 InsertEntry (D, X, D->OpIndex+4);
1076 /* If we remove staxspidx, we must restore the Y register to what the
1077 ** function would return.
1079 X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
1080 InsertEntry (D, X, D->OpIndex+5);
1082 /* Remove the push and the call to the staxspidx function */
1083 RemoveRemainders (D);
1085 /* We changed the sequence */
1091 static unsigned Opt_tosaddax (StackOpData* D)
1092 /* Optimize the tosaddax sequence */
1097 /* We need the entry behind the add */
1098 CHECK (D->NextEntry != 0);
1100 /* Check if the X register is known and zero when the add is done, and
1101 ** if the add is followed by
1104 ** jsr ldauidx ; or ldaidx
1106 ** If this is true, the addition does actually add an offset to a pointer
1107 ** before it is dereferenced. Since both subroutines take an offset in Y,
1108 ** we can pass the offset (instead of #$00) and remove the addition
1111 if (D->OpEntry->RI->In.RegX == 0 &&
1112 D->NextEntry->OPC == OP65_LDY &&
1113 CE_IsKnownImm (D->NextEntry, 0) &&
1114 !CE_HasLabel (D->NextEntry) &&
1115 (N = CS_GetNextEntry (D->Code, D->OpIndex + 1)) != 0 &&
1116 (CE_IsCallTo (N, "ldauidx") ||
1117 CE_IsCallTo (N, "ldaidx"))) {
1119 int Signed = (strcmp (N->Arg, "ldaidx") == 0);
1121 /* Store the value into the zeropage instead of pushing it */
1125 /* Replace the ldy by a tay. Be sure to create the new entry before
1126 ** deleting the ldy, since we will reference the line info from this
1129 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->NextEntry->LI);
1130 DelEntry (D, D->OpIndex + 1);
1131 InsertEntry (D, X, D->OpIndex + 1);
1133 /* Replace the call to ldaidx/ldauidx. Since X is already zero, and
1134 ** the ptr is in the zero page location, we just need to load from
1135 ** the pointer, and fix X in case of ldaidx.
1137 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, D->ZPLo, 0, N->LI);
1138 DelEntry (D, D->OpIndex + 2);
1139 InsertEntry (D, X, D->OpIndex + 2);
1144 /* Add sign extension - N is unused now */
1145 N = CS_GetNextEntry (D->Code, D->OpIndex + 2);
1147 L = CS_GenLabel (D->Code, N);
1149 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, X->LI);
1150 InsertEntry (D, X, D->OpIndex + 3);
1152 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, X->LI);
1153 InsertEntry (D, X, D->OpIndex + 4);
1158 /* Store the value into the zeropage instead of pushing it */
1159 ReplacePushByStore (D);
1161 /* Inline the add */
1162 D->IP = D->OpIndex+1;
1165 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, D->OpEntry->LI);
1166 InsertEntry (D, X, D->IP++);
1169 AddOpLow (D, OP65_ADC, &D->Lhs);
1172 if (D->PushEntry->RI->In.RegX == 0) {
1174 /* The high byte is the value in X plus the carry */
1175 CodeLabel* L = CS_GenLabel (D->Code, D->NextEntry);
1178 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1179 InsertEntry (D, X, D->IP++);
1182 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
1183 InsertEntry (D, X, D->IP++);
1185 } else if (D->OpEntry->RI->In.RegX == 0 &&
1186 (RegValIsKnown (D->PushEntry->RI->In.RegX) ||
1187 (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
1189 /* The high byte is that of the first operand plus carry */
1191 if (RegValIsKnown (D->PushEntry->RI->In.RegX)) {
1192 /* Value of first op high byte is known */
1193 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX);
1194 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
1196 /* Value of first op high byte is unknown. Load from ZP or
1197 ** original storage.
1199 if (D->Lhs.X.Flags & LI_DIRECT) {
1200 CodeEntry* LoadX = D->Lhs.X.LoadEntry;
1201 X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
1203 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
1206 InsertEntry (D, X, D->IP++);
1209 L = CS_GenLabel (D->Code, D->NextEntry);
1210 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1211 InsertEntry (D, X, D->IP++);
1214 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
1215 InsertEntry (D, X, D->IP++);
1217 /* High byte is unknown */
1218 AddOpHigh (D, OP65_ADC, &D->Lhs, 1);
1222 /* Remove the push and the call to the tosaddax function */
1223 RemoveRemainders (D);
1225 /* We changed the sequence */
1231 static unsigned Opt_tosandax (StackOpData* D)
1232 /* Optimize the tosandax sequence */
1234 /* Store the value into the zeropage instead of pushing it */
1235 ReplacePushByStore (D);
1237 /* Inline the and, low byte */
1238 D->IP = D->OpIndex + 1;
1239 AddOpLow (D, OP65_AND, &D->Lhs);
1242 AddOpHigh (D, OP65_AND, &D->Lhs, 1);
1244 /* Remove the push and the call to the tosandax function */
1245 RemoveRemainders (D);
1247 /* We changed the sequence */
1253 static unsigned Opt_tosaslax (StackOpData* D)
1254 /* Optimize the tosaslax sequence */
1256 return Opt_tosshift (D, "aslaxy");
1261 static unsigned Opt_tosasrax (StackOpData* D)
1262 /* Optimize the tosasrax sequence */
1264 return Opt_tosshift (D, "asraxy");
1269 static unsigned Opt_toseqax (StackOpData* D)
1270 /* Optimize the toseqax sequence */
1272 return Opt_toseqax_tosneax (D, "booleq");
1277 static unsigned Opt_tosgeax (StackOpData* D)
1278 /* Optimize the tosgeax sequence */
1283 /* Inline the sbc */
1284 D->IP = D->OpIndex+1;
1286 /* Must be true because of OP_RHS_LOAD */
1287 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1289 /* Add code for low operand */
1290 AddOpLow (D, OP65_CMP, &D->Rhs);
1292 /* Add code for high operand */
1293 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1296 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
1297 InsertEntry (D, X, D->IP++);
1300 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, D->OpEntry->LI);
1301 InsertEntry (D, X, D->IP++);
1302 L = CS_GenLabel (D->Code, X);
1304 /* Insert a bvs L before the eor insn */
1305 X = NewCodeEntry (OP65_BVS, AM65_BRA, L->Name, L, D->OpEntry->LI);
1306 InsertEntry (D, X, D->IP - 2);
1310 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1311 InsertEntry (D, X, D->IP++);
1314 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1315 InsertEntry (D, X, D->IP++);
1318 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1319 InsertEntry (D, X, D->IP++);
1321 /* Remove the push and the call to the tosgeax function */
1322 RemoveRemainders (D);
1324 /* We changed the sequence */
1330 static unsigned Opt_tosltax (StackOpData* D)
1331 /* Optimize the tosltax sequence */
1337 /* Inline the compare */
1338 D->IP = D->OpIndex+1;
1340 /* Must be true because of OP_RHS_LOAD */
1341 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1343 /* Add code for low operand */
1344 AddOpLow (D, OP65_CMP, &D->Rhs);
1346 /* Add code for high operand */
1347 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1350 X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
1351 InsertEntry (D, X, D->IP++);
1354 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, D->OpEntry->LI);
1355 InsertEntry (D, X, D->IP++);
1356 L = CS_GenLabel (D->Code, X);
1358 /* Insert a bvc L before the eor insn */
1359 X = NewCodeEntry (OP65_BVC, AM65_BRA, L->Name, L, D->OpEntry->LI);
1360 InsertEntry (D, X, D->IP - 2);
1364 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1365 InsertEntry (D, X, D->IP++);
1368 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1369 InsertEntry (D, X, D->IP++);
1372 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1373 InsertEntry (D, X, D->IP++);
1375 /* Remove the push and the call to the tosltax function */
1376 RemoveRemainders (D);
1378 /* We changed the sequence */
1384 static unsigned Opt_tosneax (StackOpData* D)
1385 /* Optimize the tosneax sequence */
1387 return Opt_toseqax_tosneax (D, "boolne");
1392 static unsigned Opt_tosorax (StackOpData* D)
1393 /* Optimize the tosorax sequence */
1395 /* Store the value into the zeropage instead of pushing it */
1396 ReplacePushByStore (D);
1398 /* Inline the or, low byte */
1399 D->IP = D->OpIndex + 1;
1400 AddOpLow (D, OP65_ORA, &D->Lhs);
1403 AddOpHigh (D, OP65_ORA, &D->Lhs, 1);
1405 /* Remove the push and the call to the tosorax function */
1406 RemoveRemainders (D);
1408 /* We changed the sequence */
1414 static unsigned Opt_tosshlax (StackOpData* D)
1415 /* Optimize the tosshlax sequence */
1417 return Opt_tosshift (D, "shlaxy");
1422 static unsigned Opt_tosshrax (StackOpData* D)
1423 /* Optimize the tosshrax sequence */
1425 return Opt_tosshift (D, "shraxy");
1430 static unsigned Opt_tossubax (StackOpData* D)
1431 /* Optimize the tossubax sequence. Note: subtraction is not commutative! */
1436 /* Inline the sbc */
1437 D->IP = D->OpIndex+1;
1440 X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
1441 InsertEntry (D, X, D->IP++);
1443 /* Must be true because of OP_RHS_LOAD */
1444 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1446 /* Add code for low operand */
1447 AddOpLow (D, OP65_SBC, &D->Rhs);
1449 /* Add code for high operand */
1450 AddOpHigh (D, OP65_SBC, &D->Rhs, 1);
1452 /* Remove the push and the call to the tossubax function */
1453 RemoveRemainders (D);
1455 /* We changed the sequence */
1461 static unsigned Opt_tosugeax (StackOpData* D)
1462 /* Optimize the tosugeax sequence */
1467 /* Inline the sbc */
1468 D->IP = D->OpIndex+1;
1470 /* Must be true because of OP_RHS_LOAD */
1471 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1473 /* Add code for low operand */
1474 AddOpLow (D, OP65_CMP, &D->Rhs);
1476 /* Add code for high operand */
1477 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1480 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
1481 InsertEntry (D, X, D->IP++);
1484 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
1485 InsertEntry (D, X, D->IP++);
1488 X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
1489 InsertEntry (D, X, D->IP++);
1491 /* Remove the push and the call to the tosugeax function */
1492 RemoveRemainders (D);
1494 /* We changed the sequence */
1500 static unsigned Opt_tosugtax (StackOpData* D)
1501 /* Optimize the tosugtax sequence */
1506 /* Inline the sbc */
1507 D->IP = D->OpIndex+1;
1509 /* Must be true because of OP_RHS_LOAD */
1510 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1513 X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
1514 InsertEntry (D, X, D->IP++);
1516 /* Add code for low operand */
1517 AddOpLow (D, OP65_SBC, &D->Rhs);
1519 /* We need the zero flag, so remember the immediate result */
1520 X = NewCodeEntry (OP65_STA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1521 InsertEntry (D, X, D->IP++);
1523 /* Add code for high operand */
1524 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1527 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1528 InsertEntry (D, X, D->IP++);
1530 /* Transform to boolean */
1531 X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolugt", 0, D->OpEntry->LI);
1532 InsertEntry (D, X, D->IP++);
1534 /* Remove the push and the call to the operator function */
1535 RemoveRemainders (D);
1537 /* We changed the sequence */
1543 static unsigned Opt_tosuleax (StackOpData* D)
1544 /* Optimize the tosuleax sequence */
1549 /* Inline the sbc */
1550 D->IP = D->OpIndex+1;
1552 /* Must be true because of OP_RHS_LOAD */
1553 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1556 X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
1557 InsertEntry (D, X, D->IP++);
1559 /* Add code for low operand */
1560 AddOpLow (D, OP65_SBC, &D->Rhs);
1562 /* We need the zero flag, so remember the immediate result */
1563 X = NewCodeEntry (OP65_STA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1564 InsertEntry (D, X, D->IP++);
1566 /* Add code for high operand */
1567 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1570 X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
1571 InsertEntry (D, X, D->IP++);
1573 /* Transform to boolean */
1574 X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolule", 0, D->OpEntry->LI);
1575 InsertEntry (D, X, D->IP++);
1577 /* Remove the push and the call to the operator function */
1578 RemoveRemainders (D);
1580 /* We changed the sequence */
1586 static unsigned Opt_tosultax (StackOpData* D)
1587 /* Optimize the tosultax sequence */
1592 /* Inline the sbc */
1593 D->IP = D->OpIndex+1;
1595 /* Must be true because of OP_RHS_LOAD */
1596 CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
1598 /* Add code for low operand */
1599 AddOpLow (D, OP65_CMP, &D->Rhs);
1601 /* Add code for high operand */
1602 AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
1604 /* Transform to boolean */
1605 X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolult", 0, D->OpEntry->LI);
1606 InsertEntry (D, X, D->IP++);
1608 /* Remove the push and the call to the operator function */
1609 RemoveRemainders (D);
1611 /* We changed the sequence */
1617 static unsigned Opt_tosxorax (StackOpData* D)
1618 /* Optimize the tosxorax sequence */
1623 /* Store the value into the zeropage instead of pushing it */
1624 ReplacePushByStore (D);
1626 /* Inline the xor, low byte */
1627 D->IP = D->OpIndex + 1;
1628 AddOpLow (D, OP65_EOR, &D->Lhs);
1631 if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
1632 RegValIsKnown (D->OpEntry->RI->In.RegX)) {
1633 /* Both values known, precalculate the result */
1634 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
1635 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
1636 InsertEntry (D, X, D->IP++);
1637 } else if (D->PushEntry->RI->In.RegX != 0) {
1638 /* High byte is unknown */
1639 AddOpHigh (D, OP65_EOR, &D->Lhs, 1);
1642 /* Remove the push and the call to the tosandax function */
1643 RemoveRemainders (D);
1645 /* We changed the sequence */
1651 /*****************************************************************************/
1653 /*****************************************************************************/
1657 static const OptFuncDesc FuncTable[] = {
1658 { "__bzero", Opt___bzero, REG_NONE, OP_X_ZERO | OP_A_KNOWN },
1659 { "staspidx", Opt_staspidx, REG_NONE, OP_NONE },
1660 { "staxspidx", Opt_staxspidx, REG_AX, OP_NONE },
1661 { "tosaddax", Opt_tosaddax, REG_NONE, OP_NONE },
1662 { "tosandax", Opt_tosandax, REG_NONE, OP_NONE },
1663 { "tosaslax", Opt_tosaslax, REG_NONE, OP_NONE },
1664 { "tosasrax", Opt_tosasrax, REG_NONE, OP_NONE },
1665 { "toseqax", Opt_toseqax, REG_NONE, OP_NONE },
1666 { "tosgeax", Opt_tosgeax, REG_NONE, OP_RHS_LOAD_DIRECT },
1667 { "tosltax", Opt_tosltax, REG_NONE, OP_RHS_LOAD_DIRECT },
1668 { "tosneax", Opt_tosneax, REG_NONE, OP_NONE },
1669 { "tosorax", Opt_tosorax, REG_NONE, OP_NONE },
1670 { "tosshlax", Opt_tosshlax, REG_NONE, OP_NONE },
1671 { "tosshrax", Opt_tosshrax, REG_NONE, OP_NONE },
1672 { "tossubax", Opt_tossubax, REG_NONE, OP_RHS_LOAD_DIRECT },
1673 { "tosugeax", Opt_tosugeax, REG_NONE, OP_RHS_LOAD_DIRECT },
1674 { "tosugtax", Opt_tosugtax, REG_NONE, OP_RHS_LOAD_DIRECT },
1675 { "tosuleax", Opt_tosuleax, REG_NONE, OP_RHS_LOAD_DIRECT },
1676 { "tosultax", Opt_tosultax, REG_NONE, OP_RHS_LOAD_DIRECT },
1677 { "tosxorax", Opt_tosxorax, REG_NONE, OP_NONE },
1679 #define FUNC_COUNT (sizeof(FuncTable) / sizeof(FuncTable[0]))
1683 static int CmpFunc (const void* Key, const void* Func)
1684 /* Compare function for bsearch */
1686 return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
1691 static const OptFuncDesc* FindFunc (const char* Name)
1692 /* Find the function with the given name. Return a pointer to the table entry
1693 ** or NULL if the function was not found.
1696 return bsearch (Name, FuncTable, FUNC_COUNT, sizeof(OptFuncDesc), CmpFunc);
1701 static int CmpHarmless (const void* Key, const void* Entry)
1702 /* Compare function for bsearch */
1704 return strcmp (Key, *(const char**)Entry);
1709 static int HarmlessCall (const char* Name)
1710 /* Check if this is a call to a harmless subroutine that will not interrupt
1711 ** the pushax/op sequence when encountered.
1714 static const char* const Tab[] = {
1760 void* R = bsearch (Name,
1762 sizeof (Tab) / sizeof (Tab[0]),
1770 static void ResetStackOpData (StackOpData* Data)
1771 /* Reset the given data structure */
1774 Data->UsedRegs = REG_NONE;
1776 ClearLoadInfo (&Data->Lhs);
1777 ClearLoadInfo (&Data->Rhs);
1779 Data->PushIndex = -1;
1785 static int PreCondOk (StackOpData* D)
1786 /* Check if the preconditions for a call to the optimizer subfunction are
1787 ** satisfied. As a side effect, this function will also choose the zero page
1791 /* Check the flags */
1792 unsigned UnusedRegs = D->OptFunc->UnusedRegs;
1793 if (UnusedRegs != REG_NONE &&
1794 (GetRegInfo (D->Code, D->OpIndex+1, UnusedRegs) & UnusedRegs) != 0) {
1795 /* Cannot optimize */
1798 if ((D->OptFunc->Flags & OP_A_KNOWN) != 0 &&
1799 RegValIsUnknown (D->OpEntry->RI->In.RegA)) {
1800 /* Cannot optimize */
1803 if ((D->OptFunc->Flags & OP_X_ZERO) != 0 &&
1804 D->OpEntry->RI->In.RegX != 0) {
1805 /* Cannot optimize */
1808 if ((D->OptFunc->Flags & OP_LHS_LOAD) != 0) {
1809 if (D->Lhs.A.LoadIndex < 0 || D->Lhs.X.LoadIndex < 0) {
1810 /* Cannot optimize */
1812 } else if ((D->OptFunc->Flags & OP_LHS_LOAD_DIRECT) != 0) {
1813 if ((D->Lhs.A.Flags & D->Lhs.X.Flags & LI_DIRECT) == 0) {
1814 /* Cannot optimize */
1819 if ((D->OptFunc->Flags & OP_RHS_LOAD) != 0) {
1820 if (D->Rhs.A.LoadIndex < 0 || D->Rhs.X.LoadIndex < 0) {
1821 /* Cannot optimize */
1823 } else if ((D->OptFunc->Flags & OP_RHS_LOAD_DIRECT) != 0) {
1824 if ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) == 0) {
1825 /* Cannot optimize */
1830 if ((D->Rhs.A.Flags | D->Rhs.X.Flags) & LI_DUP_LOAD) {
1831 /* Cannot optimize */
1835 /* Determine the zero page locations to use */
1836 if ((D->UsedRegs & REG_PTR1) == REG_NONE) {
1839 } else if ((D->UsedRegs & REG_SREG) == REG_NONE) {
1842 } else if ((D->UsedRegs & REG_PTR2) == REG_NONE) {
1846 /* No registers available */
1850 /* Determine if we have a basic block */
1851 return CS_IsBasicBlock (D->Code, D->PushIndex, D->OpIndex);
1856 /*****************************************************************************/
1858 /*****************************************************************************/
1862 unsigned OptStackOps (CodeSeg* S)
1863 /* Optimize operations that take operands via the stack */
1865 unsigned Changes = 0; /* Number of changes in one run */
1868 int OldEntryCount; /* Old number of entries */
1869 unsigned UsedRegs = 0; /* Registers used */
1870 unsigned ChangedRegs = 0;/* Registers changed */
1878 } State = Initialize;
1881 /* Remember the code segment in the info struct */
1884 /* Look for a call to pushax followed by a call to some other function
1885 ** that takes it's first argument on the stack, and the second argument
1886 ** in the primary register.
1887 ** It depends on the code between the two if we can handle/transform the
1888 ** sequence, so check this code for the following list of things:
1890 ** - the range must be a basic block (one entry, one exit)
1891 ** - there may not be accesses to local variables with unknown
1892 ** offsets (because we have to adjust these offsets).
1893 ** - no subroutine calls
1896 ** Since we need a zero page register later, do also check the
1897 ** intermediate code for zero page use.
1900 while (I < (int)CS_GetEntryCount (S)) {
1902 /* Get the next entry */
1903 CodeEntry* E = CS_GetEntry (S, I);
1905 /* Actions depend on state */
1909 ResetStackOpData (&Data);
1910 UsedRegs = ChangedRegs = REG_NONE;
1915 /* While searching, track register load insns, so we can tell
1916 ** what is in a register once pushax is encountered.
1918 if (CE_HasLabel (E)) {
1919 /* Currently we don't track across branches */
1920 ClearLoadInfo (&Data.Lhs);
1922 if (CE_IsCallTo (E, "pushax")) {
1926 /* Track load insns */
1927 TrackLoads (&Data.Lhs, E, I);
1932 /* We' found a pushax before. Search for a stack op that may
1933 ** follow and in the meantime, track zeropage usage and check
1934 ** for code that will disable us from translating the sequence.
1936 if (CE_HasLabel (E)) {
1937 /* Currently we don't track across branches */
1938 ClearLoadInfo (&Data.Rhs);
1940 if (E->OPC == OP65_JSR) {
1942 /* Subroutine call: Check if this is one of the functions,
1943 ** we're going to replace.
1945 Data.OptFunc = FindFunc (E->Arg);
1947 /* Remember the op index and go on */
1952 } else if (!HarmlessCall (E->Arg)) {
1953 /* A call to an unkown subroutine: We need to start
1954 ** over after the last pushax. Note: This will also
1955 ** happen if we encounter a call to pushax!
1961 /* Track register usage */
1962 Data.UsedRegs |= (E->Use | E->Chg);
1963 TrackLoads (&Data.Rhs, E, I);
1966 } else if (E->Info & OF_STORE && (E->Chg & REG_ZP) == 0) {
1968 /* Too dangerous - there may be a change of a variable
1969 ** within the sequence.
1975 } else if ((E->Use & REG_SP) != 0 &&
1976 (E->AM != AM65_ZP_INDY ||
1977 RegValIsUnknown (E->RI->In.RegY) ||
1978 E->RI->In.RegY < 2)) {
1980 /* If we are using the stack, and we don't have "indirect Y"
1981 ** addressing mode, or the value of Y is unknown, or less
1982 ** than two, we cannot cope with this piece of code. Having
1983 ** an unknown value of Y means that we cannot correct the
1984 ** stack offset, while having an offset less than two means
1985 ** that the code works with the value on stack which is to
1993 /* Other stuff: Track register usage */
1994 Data.UsedRegs |= (E->Use | E->Chg);
1995 TrackLoads (&Data.Rhs, E, I);
1997 /* If the registers from the push (A/X) are used before they're
1998 ** changed, we cannot change the sequence, because this would
1999 ** with a high probability change the register contents.
2002 if ((UsedRegs & ~ChangedRegs) & REG_AX) {
2007 ChangedRegs |= E->Chg;
2011 /* Track zero page location usage beyond this point */
2012 Data.UsedRegs |= GetRegInfo (S, I, REG_SREG | REG_PTR1 | REG_PTR2);
2014 /* Finalize the load info */
2015 FinalizeLoadInfo (&Data.Lhs, S);
2016 FinalizeLoadInfo (&Data.Rhs, S);
2018 /* If the Lhs loads do load from zeropage, we have to include
2019 ** them into UsedRegs registers used. The Rhs loads have already
2022 if (Data.Lhs.A.LoadEntry && Data.Lhs.A.LoadEntry->AM == AM65_ZP) {
2023 Data.UsedRegs |= Data.Lhs.A.LoadEntry->Use;
2025 if (Data.Lhs.X.LoadEntry && Data.Lhs.X.LoadEntry->AM == AM65_ZP) {
2026 Data.UsedRegs |= Data.Lhs.X.LoadEntry->Use;
2029 /* Check the preconditions. If they aren't ok, reset the insn
2030 ** pointer to the pushax and start over. We will loose part of
2031 ** load tracking but at least a/x has probably lost between
2032 ** pushax and here and will be tracked again when restarting.
2034 if (!PreCondOk (&Data)) {
2040 /* Prepare the remainder of the data structure. */
2041 Data.PrevEntry = CS_GetPrevEntry (S, Data.PushIndex);
2042 Data.PushEntry = CS_GetEntry (S, Data.PushIndex);
2043 Data.OpEntry = CS_GetEntry (S, Data.OpIndex);
2044 Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
2046 /* Remember the current number of code lines */
2047 OldEntryCount = CS_GetEntryCount (S);
2049 /* Adjust stack offsets to account for the upcoming removal */
2050 AdjustStackOffset (&Data, 2);
2052 /* Regenerate register info, since AdjustStackOffset changed
2057 /* Call the optimizer function */
2058 Changes += Data.OptFunc->Func (&Data);
2060 /* Since the function may have added or deleted entries,
2061 ** correct the index.
2063 I += CS_GetEntryCount (S) - OldEntryCount;
2065 /* Regenerate register info */
2079 /* Return the number of changes made */