1 /*****************************************************************************/
5 /* Optimize operations that take operands via the stack */
9 /* (C) 2001-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
50 /*****************************************************************************/
54 /* Structure that holds the needed data */
55 typedef struct StackOpData StackOpData;
57 CodeSeg* Code; /* Pointer to code segment */
58 unsigned Flags; /* Flags to remember things */
59 unsigned PushIndex; /* Index of call to pushax in codeseg */
60 unsigned OpIndex; /* Index of actual operation */
61 CodeEntry* PrevEntry; /* Entry before the call to pushax */
62 CodeEntry* PushEntry; /* Pointer to entry with call to pushax */
63 CodeEntry* OpEntry; /* Pointer to entry with op */
64 CodeEntry* NextEntry; /* Entry after the op */
65 const char* ZPLo; /* Lo byte of zero page loc to use */
66 const char* ZPHi; /* Hi byte of zero page loc to use */
67 unsigned IP; /* Insertion point used by some routines */
70 /* Flags returned by DirectOp */
71 #define OP_DIRECT 0x01 /* Direct op may be used */
72 #define OP_RELOAD_Y 0x02 /* Must reload index register Y */
76 /*****************************************************************************/
78 /*****************************************************************************/
82 static unsigned AdjustStackOffset (CodeSeg* S, unsigned Start, unsigned Stop,
84 /* Adjust the offset for all stack accesses in the range Start to Stop, both
85 * inclusive. The function returns the number of instructions that have been
89 /* Number of inserted instructions */
90 unsigned Inserted = 0;
92 /* Walk over all entries */
96 CodeEntry* E = CS_GetEntry (S, I);
98 int NeedCorrection = 0;
99 if ((E->Use & REG_SP) != 0) {
101 /* Check for some things that should not happen */
102 CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
103 CHECK (strcmp (E->Arg, "sp") == 0);
105 /* We need to correct this one */
108 } else if (CE_IsCallTo (E, "ldaxysp")) {
110 /* We need to correct this one */
115 if (NeedCorrection) {
119 /* If the Y register value is needed later, we have to reload the
120 * register after changing it.
122 int NeedY = RegYUsed (S, I+1);
123 unsigned YVal = E->RI->In.RegY;
125 /* Get the code entry before this one. If it's a LDY, adjust the
128 P = CS_GetPrevEntry (S, I);
129 if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
131 /* The Y load is just before the stack access, adjust it */
132 CE_SetNumArg (P, P->Num - Offs);
136 /* Insert a new load instruction before the stack access */
137 const char* Arg = MakeHexArg (YVal - Offs);
138 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
139 CS_InsertEntry (S, X, I);
141 /* One more inserted entries */
145 /* Be sure to skip the stack access for the next round */
150 /* If we need the value of Y later, be sure to reload it */
152 const char* Arg = MakeHexArg (YVal);
153 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
154 CS_InsertEntry (S, X, I+1);
156 /* One more inserted entries */
160 /* Skip this instruction int the next round */
169 /* Return the number of inserted entries */
175 static void InsertEntry (StackOpData* D, CodeEntry* E, unsigned Index)
176 /* Insert a new entry. Depending on Index, D->PushIndex and D->OpIndex will
177 * be adjusted by this function.
180 /* Insert the entry into the code segment */
181 CS_InsertEntry (D->Code, E, Index);
183 /* Adjust the indices if necessary */
184 if (D->PushEntry && Index <= D->PushIndex) {
187 if (D->OpEntry && Index <= D->OpIndex) {
194 static void DelEntry (StackOpData* D, unsigned Index)
195 /* Delete an entry. Depending on Index, D->PushIndex and D->OpIndex will be
196 * adjusted by this function, and PushEntry/OpEntry may get invalidated.
199 /* Delete the entry from the code segment */
200 CS_DelEntry (D->Code, Index);
202 /* Adjust the indices if necessary */
203 if (Index < D->PushIndex) {
205 } else if (Index == D->PushIndex) {
208 if (Index < D->OpIndex) {
210 } else if (Index == D->OpIndex) {
217 static void CheckDirectOp (StackOpData* D)
218 /* Check if the given entry is a lda instruction with an addressing mode
219 * that allows us to replace it by another operation (like ora). If so, we may
220 * use this location for the or and must not save the value in the zero
224 /* We need the entry before the push */
226 CHECK ((E = D->PrevEntry) != 0);
228 if (E->OPC == OP65_LDA) {
229 if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
230 /* These insns are all ok and replaceable */
231 D->Flags |= OP_DIRECT;
232 } else if (E->AM == AM65_ZP_INDY && RegValIsKnown (E->RI->In.RegY) &&
233 strcmp (E->Arg, "sp") == 0) {
234 /* A load from the stack with known offset is also ok, but in this
235 * case we must reload the index register later. Please note that
236 * a load indirect via other zero page locations is not ok, since
237 * these locations may change between the push and the actual
240 D->Flags |= (OP_DIRECT | OP_RELOAD_Y);
247 static void ReplacePushByStore (StackOpData* D)
248 /* Replace the call to the push subroutine by a store into the zero page
249 * location (actually, the push is not replaced, because we need it for
250 * later, but the name is still ok since the push will get removed at the
251 * end of each routine).
256 /* Store the value into the zeropage instead of pushing it */
257 X = NewCodeEntry (OP65_STX, AM65_ZP, D->ZPHi, 0, D->PushEntry->LI);
258 InsertEntry (D, X, D->PushIndex+1);
259 if ((D->Flags & OP_DIRECT) == 0) {
260 X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->PushEntry->LI);
261 InsertEntry (D, X, D->PushIndex+1);
267 static void AddOpLow (StackOpData* D, opc_t OPC)
268 /* Add an op for the low byte of an operator. This function honours the
269 * OP_DIRECT and OP_RELOAD_Y flags and generates the necessary instructions.
270 * All code is inserted at the current insertion point.
275 if ((D->Flags & OP_DIRECT) != 0) {
276 /* Op with a variable location. If the location is on the stack, we
277 * need to reload the Y register.
279 if ((D->Flags & OP_RELOAD_Y) != 0) {
280 const char* Arg = MakeHexArg (D->PrevEntry->RI->In.RegY);
281 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
282 InsertEntry (D, X, D->IP++);
284 X = NewCodeEntry (OPC, D->PrevEntry->AM, D->PrevEntry->Arg, 0, D->OpEntry->LI);
286 /* Op with temp storage */
287 X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
289 InsertEntry (D, X, D->IP++);
294 static void AddOpHigh (StackOpData* D, opc_t OPC)
295 /* Add an op for the high byte of an operator. Special cases (constant values
296 * or similar) have to be checked separately, the function covers only the
297 * generic case. Code is inserted at the insertion point.
302 /* High byte is unknown */
303 X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
304 InsertEntry (D, X, D->IP++);
305 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
306 InsertEntry (D, X, D->IP++);
307 X = NewCodeEntry (OPC, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
308 InsertEntry (D, X, D->IP++);
309 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
310 InsertEntry (D, X, D->IP++);
311 X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
312 InsertEntry (D, X, D->IP++);
317 static void RemovePushAndOp (StackOpData* D)
318 /* Remove the call to pushax and the call to the operator subroutine */
320 DelEntry (D, D->OpIndex);
321 DelEntry (D, D->PushIndex);
326 static int IsRegVar (StackOpData* D)
327 /* If the value pushed is that of a register variable, replace ZPLo and ZPHi
328 * in the given StackOpData struct by the register variables and return true.
329 * Otherwise leave D untouched and return false.
334 if (D->PushIndex >= 2 &&
335 (P = D->PrevEntry) != 0 &&
336 P->OPC == OP65_LDX &&
338 strncmp (P->Arg, "regbank+", 7) == 0 &&
339 IsDigit (P->Arg[8]) &&
340 (P = CS_GetEntry (D->Code, D->PushIndex-2)) != 0 &&
341 P->OPC == OP65_LDA &&
343 strncmp (P->Arg, "regbank+", 7) == 0 &&
344 IsDigit (P->Arg[8])) {
345 /* Ok, it loads the register variable */
346 D->ZPHi = D->PrevEntry->Arg;
356 /*****************************************************************************/
357 /* Actual optimization functions */
358 /*****************************************************************************/
362 static unsigned Opt___bzero (StackOpData* D)
363 /* Optimize the __bzero sequence if possible */
369 /* Check if we're using a register variable */
371 /* Store the value into the zeropage instead of pushing it */
372 ReplacePushByStore (D);
375 /* If the return value of __bzero is used, we have to add code to reload
376 * a/x from the pointer variable.
378 if (RegAXUsed (D->Code, D->OpIndex+1)) {
379 X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
380 InsertEntry (D, X, D->OpIndex+1);
381 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
382 InsertEntry (D, X, D->OpIndex+2);
385 /* X is always zero, A contains the size of the data area to zero.
386 * Note: A may be zero, in which case the operation is null op.
388 if (D->OpEntry->RI->In.RegA != 0) {
390 /* The value of A is known */
391 if (D->OpEntry->RI->In.RegA <= 0x81) {
393 /* Loop using the sign bit */
394 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
395 InsertEntry (D, X, D->OpIndex+1);
397 Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
398 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
399 InsertEntry (D, X, D->OpIndex+2);
401 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
402 InsertEntry (D, X, D->OpIndex+3);
403 L = CS_GenLabel (D->Code, X);
405 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, D->OpEntry->LI);
406 InsertEntry (D, X, D->OpIndex+4);
408 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, D->OpEntry->LI);
409 InsertEntry (D, X, D->OpIndex+5);
413 /* Loop using an explicit compare */
414 X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
415 InsertEntry (D, X, D->OpIndex+1);
417 X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
418 InsertEntry (D, X, D->OpIndex+2);
420 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
421 InsertEntry (D, X, D->OpIndex+3);
422 L = CS_GenLabel (D->Code, X);
424 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
425 InsertEntry (D, X, D->OpIndex+4);
427 Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
428 X = NewCodeEntry (OP65_CPY, AM65_IMM, Arg, 0, D->OpEntry->LI);
429 InsertEntry (D, X, D->OpIndex+5);
431 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, D->OpEntry->LI);
432 InsertEntry (D, X, D->OpIndex+6);
437 /* Remove the push and the call to the __bzero function */
440 /* We changed the sequence */
446 static unsigned Opt_staspidx (StackOpData* D)
447 /* Optimize the staspidx sequence if possible */
451 /* Check if we're using a register variable */
453 /* Store the value into the zeropage instead of pushing it */
454 ReplacePushByStore (D);
457 /* Replace the store subroutine call by a direct op */
458 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
459 InsertEntry (D, X, D->OpIndex+1);
461 /* Remove the push and the call to the staspidx function */
464 /* We changed the sequence */
470 static unsigned Opt_staxspidx (StackOpData* D)
471 /* Optimize the staxspidx sequence if possible */
475 /* Check if we're using a register variable */
477 /* Store the value into the zeropage instead of pushing it */
478 ReplacePushByStore (D);
481 /* Inline the store */
482 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
483 InsertEntry (D, X, D->OpIndex+1);
484 if (RegValIsKnown (D->OpEntry->RI->In.RegY)) {
485 /* Value of Y is known */
486 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
487 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
489 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
491 InsertEntry (D, X, D->OpIndex+2);
492 if (RegValIsKnown (D->OpEntry->RI->In.RegX)) {
493 /* Value of X is known */
494 const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
495 X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
498 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
500 InsertEntry (D, X, D->OpIndex+3);
501 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
502 InsertEntry (D, X, D->OpIndex+4);
504 /* Remove the push and the call to the staxspidx function */
507 /* We changed the sequence */
513 static unsigned Opt_tosaddax (StackOpData* D)
514 /* Optimize the tosaddax sequence if possible */
519 /* We need the entry behind the add */
520 CHECK (D->NextEntry != 0);
522 /* Check if the X register is known and zero when the add is done, and
523 * if the add is followed by
526 * jsr ldauidx ; or ldaidx
528 * If this is true, the addition does actually add an offset to a pointer
529 * before it is dereferenced. Since both subroutines take an offset in Y,
530 * we can pass the offset (instead of #$00) and remove the addition
533 if (D->OpEntry->RI->In.RegX == 0 &&
534 D->NextEntry->OPC == OP65_LDY &&
535 CE_IsKnownImm (D->NextEntry, 0) &&
536 !CE_HasLabel (D->NextEntry) &&
537 (N = CS_GetNextEntry (D->Code, D->OpIndex + 1)) != 0 &&
538 (CE_IsCallTo (N, "ldauidx") ||
539 CE_IsCallTo (N, "ldaidx"))) {
541 int Signed = (strcmp (N->Arg, "ldaidx") == 0);
543 /* Store the value into the zeropage instead of pushing it */
544 ReplacePushByStore (D);
546 /* Replace the ldy by a tay. Be sure to create the new entry before
547 * deleting the ldy, since we will reference the line info from this
550 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->NextEntry->LI);
551 DelEntry (D, D->OpIndex + 1);
552 InsertEntry (D, X, D->OpIndex + 1);
554 /* Replace the call to ldaidx/ldauidx. Since X is already zero, and
555 * the ptr is in the zero page location, we just need to load from
556 * the pointer, and fix X in case of ldaidx.
558 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, D->ZPLo, 0, N->LI);
559 DelEntry (D, D->OpIndex + 2);
560 InsertEntry (D, X, D->OpIndex + 2);
565 /* Add sign extension - N is unused now */
566 N = CS_GetNextEntry (D->Code, D->OpIndex + 2);
568 L = CS_GenLabel (D->Code, N);
570 X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, X->LI);
571 InsertEntry (D, X, D->OpIndex + 3);
573 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, X->LI);
574 InsertEntry (D, X, D->OpIndex + 4);
579 /* Check the entry before the push. If it's a lda instruction with an
580 * addressing mode that allows us to replace it, we may use this
581 * location for the op and must not save the value in the zero page
586 /* Store the value into the zeropage instead of pushing it */
587 ReplacePushByStore (D);
590 D->IP = D->OpIndex+1;
591 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, D->OpEntry->LI);
592 InsertEntry (D, X, D->IP++);
595 AddOpLow (D, OP65_ADC);
598 if (D->PushEntry->RI->In.RegX == 0) {
599 /* The high byte is the value in X plus the carry */
600 CodeLabel* L = CS_GenLabel (D->Code, D->NextEntry);
601 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
602 InsertEntry (D, X, D->IP++);
603 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
604 InsertEntry (D, X, D->IP++);
605 } else if (D->OpEntry->RI->In.RegX == 0) {
606 /* The high byte is that of the first operand plus carry */
608 if (RegValIsKnown (D->PushEntry->RI->In.RegX)) {
609 /* Value of first op high byte is known */
610 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX);
611 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
613 /* Value of first op high byte is unknown */
614 X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
616 InsertEntry (D, X, D->IP++);
617 L = CS_GenLabel (D->Code, D->NextEntry);
618 X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
619 InsertEntry (D, X, D->IP++);
620 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
621 InsertEntry (D, X, D->IP++);
623 /* High byte is unknown */
624 AddOpHigh (D, OP65_ADC);
628 /* Remove the push and the call to the tosaddax function */
631 /* We changed the sequence */
637 static unsigned Opt_tosandax (StackOpData* D)
638 /* Optimize the tosandax sequence if possible */
642 /* Check the entry before the push. If it's a lda instruction with an
643 * addressing mode that allows us to replace it, we may use this
644 * location for the op and must not save the value in the zero page
649 /* Store the value into the zeropage instead of pushing it */
650 ReplacePushByStore (D);
652 /* Inline the and, low byte */
653 D->IP = D->OpIndex + 1;
654 AddOpLow (D, OP65_AND);
657 if (D->PushEntry->RI->In.RegX == 0 || D->OpEntry->RI->In.RegX == 0) {
658 /* The high byte is zero */
659 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
660 InsertEntry (D, X, D->IP++);
662 /* High byte is unknown */
663 AddOpHigh (D, OP65_AND);
666 /* Remove the push and the call to the tosandax function */
669 /* We changed the sequence */
675 static unsigned Opt_tosorax (StackOpData* D)
676 /* Optimize the tosorax sequence if possible */
680 /* Check the entry before the push. If it's a lda instruction with an
681 * addressing mode that allows us to replace it, we may use this
682 * location for the op and must not save the value in the zero page
687 /* Store the value into the zeropage instead of pushing it */
688 ReplacePushByStore (D);
690 /* Inline the or, low byte */
691 D->IP = D->OpIndex + 1;
692 AddOpLow (D, OP65_ORA);
695 if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
696 RegValIsKnown (D->OpEntry->RI->In.RegX)) {
697 /* Both values known, precalculate the result */
698 unsigned char Result = D->PushEntry->RI->In.RegX | D->OpEntry->RI->In.RegX;
699 const char* Arg = MakeHexArg (Result);
700 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
701 InsertEntry (D, X, D->IP++);
702 } else if (D->PushEntry->RI->In.RegX != 0) {
703 /* High byte is unknown */
704 AddOpHigh (D, OP65_ORA);
707 /* Remove the push and the call to the tosorax function */
710 /* We changed the sequence */
716 static unsigned Opt_tosxorax (StackOpData* D)
717 /* Optimize the tosxorax sequence if possible */
721 /* Check the entry before the push. If it's a lda instruction with an
722 * addressing mode that allows us to replace it, we may use this
723 * location for the op and must not save the value in the zero page
728 /* Store the value into the zeropage instead of pushing it */
729 ReplacePushByStore (D);
731 /* Inline the xor, low byte */
732 D->IP = D->OpIndex + 1;
733 AddOpLow (D, OP65_EOR);
736 if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
737 RegValIsKnown (D->OpEntry->RI->In.RegX)) {
738 /* Both values known, precalculate the result */
739 const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
740 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
741 InsertEntry (D, X, D->IP++);
742 } else if (D->PushEntry->RI->In.RegX != 0) {
743 /* High byte is unknown */
744 AddOpHigh (D, OP65_EOR);
747 /* Remove the push and the call to the tosandax function */
750 /* We changed the sequence */
756 /*****************************************************************************/
758 /*****************************************************************************/
762 /* Flags for the functions */
764 STOP_NONE = 0x00, /* Nothing special */
765 STOP_A_UNUSED = 0x01, /* Call only if a unused later */
766 STOP_A_KNOWN = 0x02, /* Call only if A is known */
767 STOP_X_ZERO = 0x04 /* Call only if X is zero */
771 typedef unsigned (*OptFunc) (StackOpData* D);
772 typedef struct OptFuncDesc OptFuncDesc;
774 const char* Name; /* Name of the replaced runtime function */
775 OptFunc Func; /* Function pointer */
776 STOP_FLAGS Flags; /* Flags */
779 static const OptFuncDesc FuncTable[] = {
780 { "__bzero", Opt___bzero, STOP_X_ZERO | STOP_A_KNOWN },
781 { "staspidx", Opt_staspidx, STOP_NONE },
782 { "staxspidx", Opt_staxspidx, STOP_A_UNUSED },
783 { "tosaddax", Opt_tosaddax, STOP_NONE },
784 { "tosandax", Opt_tosandax, STOP_NONE },
785 { "tosorax", Opt_tosorax, STOP_NONE },
786 { "tosxorax", Opt_tosxorax, STOP_NONE },
788 #define FUNC_COUNT (sizeof(FuncTable) / sizeof(FuncTable[0]))
792 static int CmpFunc (const void* Key, const void* Func)
793 /* Compare function for bsearch */
795 return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
800 static const OptFuncDesc* FindFunc (const char* Name)
801 /* Find the function with the given name. Return a pointer to the table entry
802 * or NULL if the function was not found.
805 return bsearch (Name, FuncTable, FUNC_COUNT, sizeof(OptFuncDesc), CmpFunc);
810 static int CmpHarmless (const void* Key, const void* Entry)
811 /* Compare function for bsearch */
813 return strcmp (Key, *(const char**)Entry);
818 static int HarmlessCall (const char* Name)
819 /* Check if this is a call to a harmless subroutine that will not interrupt
820 * the pushax/op sequence when encountered.
823 static const char* Tab[] = {
829 void* R = bsearch (Name,
831 sizeof (Tab) / sizeof (Tab[0]),
839 /*****************************************************************************/
841 /*****************************************************************************/
845 unsigned OptStackOps (CodeSeg* S)
846 /* Optimize operations that take operands via the stack */
848 unsigned Changes = 0; /* Number of changes in one run */
849 int InSeq = 0; /* Inside a sequence */
850 unsigned Push = 0; /* Index of pushax */
851 unsigned UsedRegs = 0; /* Zeropage registers used in sequence */
855 /* Generate register info */
858 /* Look for a call to pushax followed by a call to some other function
859 * that takes it's first argument on the stack, and the second argument
860 * in the primary register.
861 * It depends on the code between the two if we can handle/transform the
862 * sequence, so check this code for the following list of things:
864 * - the range must be a basic block (one entry, one exit)
865 * - there may not be accesses to local variables with unknown
866 * offsets (because we have to adjust these offsets).
867 * - no subroutine calls
870 * Since we need a zero page register later, do also check the
871 * intermediate code for zero page use.
874 while (I < CS_GetEntryCount (S)) {
876 /* Get the next entry */
877 CodeEntry* E = CS_GetEntry (S, I);
879 /* Handling depends if we're inside a sequence or not */
882 /* If we are using the stack, and we don't have "indirect Y"
883 * addressing mode, or the value of Y is unknown, or less than
884 * two, we cannot cope with this piece of code. Having an unknown
885 * value of Y means that we cannot correct the stack offset, while
886 * having an offset less than two means that the code works with
887 * the value on stack which is to be removed.
889 if ((E->Use & REG_SP) != 0 &&
890 (E->AM != AM65_ZP_INDY || RegValIsUnknown (E->RI->In.RegY) ||
891 E->RI->In.RegY < 2)) {
893 /* All this stuff is not allowed in a sequence */
896 } else if (E->OPC == OP65_JSR) {
898 /* Subroutine call: Check if this is one of our functions */
899 const OptFuncDesc* F = FindFunc (E->Arg);
905 /* Check the flags */
906 if ((F->Flags & STOP_A_UNUSED) != 0 && RegAUsed (S, I+1)) {
907 /* Cannot optimize */
909 } else if ((F->Flags & STOP_A_KNOWN) != 0 && RegValIsUnknown (E->RI->In.RegA)) {
910 /* Cannot optimize */
912 } else if ((F->Flags & STOP_X_ZERO) != 0 && E->RI->In.RegX != 0) {
913 /* Cannot optimize */
917 /* Determine the zero page locations to use */
919 UsedRegs |= GetRegInfo (S, I+1, REG_SREG | REG_PTR1 | REG_PTR2);
920 if ((UsedRegs & REG_SREG) == REG_NONE) {
921 /* SREG is available */
923 Data.ZPHi = "sreg+1";
924 } else if ((UsedRegs & REG_PTR1) == REG_NONE) {
926 Data.ZPHi = "ptr1+1";
927 } else if ((UsedRegs & REG_PTR2) == REG_NONE) {
929 Data.ZPHi = "ptr2+1";
931 /* No registers available */
936 /* Determine if we have a basic block */
938 PreCondOk = CS_IsBasicBlock (S, Push, I);
941 /* If preconditions are ok, call the optimizer function */
944 /* Adjust stack offsets */
945 Data.OpIndex = I + AdjustStackOffset (S, Push, I, 2);
947 /* Prepare the remainder of the data structure */
950 Data.PushIndex = Push;
951 Data.PrevEntry = CS_GetPrevEntry (S, Data.PushIndex);
952 Data.PushEntry = CS_GetEntry (S, Data.PushIndex);
954 Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
956 /* Call the optimizer function */
957 Changes += F->Func (&Data);
959 /* Regenerate register info */
963 /* End of sequence */
966 } else if (strcmp (E->Arg, "pushax") == 0) {
967 /* Restart the sequence */
970 } else if (HarmlessCall (E->Arg)) {
971 /* Track zeropage register usage */
972 UsedRegs |= (E->Use | E->Chg);
974 /* A call to an unkown subroutine ends the sequence */
979 /* Other stuff: Track zeropage register usage */
980 UsedRegs |= (E->Use | E->Chg);
983 } else if (CE_IsCallTo (E, "pushax")) {
985 /* This starts a sequence */
997 /* Free the register info */
1000 /* Return the number of changes made */