]> git.sur5r.net Git - cc65/blob - src/cc65/coptstop.c
New/changed optimizations
[cc65] / src / cc65 / coptstop.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 coptstop.c                                */
4 /*                                                                           */
5 /*           Optimize operations that take operands via the stack            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37
38 /* cc65 */
39 #include "codeent.h"
40 #include "codeinfo.h"
41 #include "codeopt.h"
42 #include "error.h"
43 #include "coptstop.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Data                                    */
49 /*****************************************************************************/
50
51
52
53 /* Structure that holds the needed data */
54 typedef struct StackOpData StackOpData;
55 struct StackOpData {
56     CodeSeg*    Code;                   /* Pointer to code segment */
57     unsigned    Flags;                  /* Flags to remember things */
58     unsigned    PushIndex;              /* Index of call to pushax in codeseg */
59     unsigned    OpIndex;                /* Index of actual operation */
60     CodeEntry*  PrevEntry;              /* Entry before the call to pushax */
61     CodeEntry*  PushEntry;              /* Pointer to entry with call to pushax */
62     CodeEntry*  OpEntry;                /* Pointer to entry with op */
63     CodeEntry*  NextEntry;              /* Entry after the op */
64     const char* ZPLo;                   /* Lo byte of zero page loc to use */
65     const char* ZPHi;                   /* Hi byte of zero page loc to use */
66     unsigned    IP;                     /* Insertion point used by some routines */
67 };
68
69 /* Flags returned by DirectOp */
70 #define OP_DIRECT       0x01            /* Direct op may be used */
71 #define OP_ONSTACK      0x02            /* Operand is on stack */
72
73
74
75 /*****************************************************************************/
76 /*                                  Helpers                                  */
77 /*****************************************************************************/
78
79
80
81 static unsigned AdjustStackOffset (CodeSeg* S, unsigned Start, unsigned Stop,
82                                    unsigned Offs)
83 /* Adjust the offset for all stack accesses in the range Start to Stop, both
84  * inclusive. The function returns the number of instructions that have been
85  * inserted.
86  */
87 {
88     /* Number of inserted instructions */
89     unsigned Inserted = 0;
90
91     /* Walk over all entries */
92     unsigned I = Start;
93     while (I <= Stop) {
94
95         CodeEntry* E = CS_GetEntry (S, I);
96
97         if (E->Use & REG_SP) {
98
99             CodeEntry* P;
100
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);
104
105             /* Get the code entry before this one. If it's a LDY, adjust the
106              * value.
107              */
108             P = CS_GetPrevEntry (S, I);
109             if (P && P->OPC == OP65_LDY && CE_KnownImm (P)) {
110
111                 /* The Y load is just before the stack access, adjust it */
112                 CE_SetNumArg (P, P->Num - Offs);
113
114             } else {
115
116                 /* Insert a new load instruction before the stack access */
117                 const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
118                 CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
119                 CS_InsertEntry (S, X, I);
120
121                 /* One more inserted entries */
122                 ++Inserted;
123                 ++Stop;
124
125                 /* Be sure to skip the stack access for the next round */
126                 ++I;
127
128             }
129
130         }
131
132         /* Next entry */
133         ++I;
134     }
135
136     /* Return the number of inserted entries */
137     return Inserted;
138 }
139
140
141
142 static void InsertEntry (StackOpData* D, CodeEntry* E, unsigned Index)
143 /* Insert a new entry. Depending on Index, D->PushIndex and D->OpIndex will
144  * be adjusted by this function.
145  */
146 {
147     /* Insert the entry into the code segment */
148     CS_InsertEntry (D->Code, E, Index);
149
150     /* Adjust the indices if necessary */
151     if (D->PushEntry && Index <= D->PushIndex) {
152         ++D->PushIndex;
153     }
154     if (D->OpEntry && Index <= D->OpIndex) {
155         ++D->OpIndex;
156     }
157 }
158
159
160
161 static void DelEntry (StackOpData* D, unsigned Index)
162 /* Delete an entry. Depending on Index, D->PushIndex and D->OpIndex will be
163  * adjusted by this function, and PushEntry/OpEntry may get invalidated.
164  */
165 {
166     /* Delete the entry from the code segment */
167     CS_DelEntry (D->Code, Index);
168
169     /* Adjust the indices if necessary */
170     if (Index < D->PushIndex) {
171         --D->PushIndex;
172     } else if (Index == D->PushIndex) {
173         D->PushEntry = 0;
174     }
175     if (Index < D->OpIndex) {
176         --D->OpIndex;
177     } else if (Index == D->OpIndex) {
178         D->OpEntry = 0;
179     }
180 }
181
182
183
184 static void CheckDirectOp (StackOpData* D)
185 /* Check if the given entry is a lda instruction with an addressing mode
186  * that allows us to replace it by another operation (like ora). If so, we may
187  * use this location for the or and must not save the value in the zero
188  * page location.
189  */
190 {
191     /* We need the entry before the push */
192     CodeEntry* E;
193     CHECK ((E = D->PrevEntry) != 0);
194
195     if (E->OPC == OP65_LDA) {
196         if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
197             /* These insns are all ok and replaceable */
198             D->Flags |= OP_DIRECT;
199         } else if (E->AM == AM65_ZP_INDY &&
200                    E->RI->In.RegY >= 0   &&
201                    (E->Use & REG_SP) != 0) {
202             /* Load from stack with known offset is also ok */
203             D->Flags |= (OP_DIRECT | OP_ONSTACK);
204         }
205     }
206 }
207
208
209
210 static void ReplacePushByStore (StackOpData* D)
211 /* Replace the call to the push subroutine by a store into the zero page
212  * location (actually, the push is not replaced, because we need it for
213  * later, but the name is still ok since the push will get removed at the
214  * end of each routine.
215  */
216 {
217     CodeEntry* X;
218
219     /* Store the value into the zeropage instead of pushing it */
220     X = NewCodeEntry (OP65_STX, AM65_ZP, D->ZPHi, 0, D->PushEntry->LI);
221     InsertEntry (D, X, D->PushIndex+1);
222     if ((D->Flags & OP_DIRECT) == 0) {
223         X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->PushEntry->LI);
224         InsertEntry (D, X, D->PushIndex+1);
225     }
226 }
227
228
229
230 static void AddOpLow (StackOpData* D, opc_t OPC)
231 /* Add an op for the low byte of an operator. This function honours the
232  * OP_DIRECT and OP_ONSTACK flags and generates the necessary instructions.
233  * All code is inserted at the current insertion point.
234  */
235 {
236     CodeEntry* X;
237
238     if ((D->Flags & OP_DIRECT) != 0) {
239         /* Op with a variable location. If the location is on the stack, we
240          * need to reload the Y register.
241          */
242         if ((D->Flags & OP_ONSTACK) != 0) {
243             const char* Arg = MakeHexArg (D->PrevEntry->RI->In.RegY);
244             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
245             InsertEntry (D, X, D->IP++);
246         }
247         X = NewCodeEntry (OPC, D->PrevEntry->AM, D->PrevEntry->Arg, 0, D->OpEntry->LI);
248     } else {
249         /* Op with temp storage */
250         X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
251     }
252     InsertEntry (D, X, D->IP++);
253 }
254
255
256
257 static void AddOpHigh (StackOpData* D, opc_t OPC)
258 /* Add an op for the high byte of an operator. Special cases (constant values
259  * or similar have to be checked separately, the function covers only the
260  * generic case. Code is inserted at the insertion point.
261  */
262 {
263     CodeEntry* X;
264
265     /* High byte is unknown */
266     X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
267     InsertEntry (D, X, D->IP++);
268     X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
269     InsertEntry (D, X, D->IP++);
270     X = NewCodeEntry (OPC, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
271     InsertEntry (D, X, D->IP++);
272     X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
273     InsertEntry (D, X, D->IP++);
274     X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
275     InsertEntry (D, X, D->IP++);
276 }
277
278
279
280 static void RemovePushAndOp (StackOpData* D)
281 /* Remove the call to pushax and the call to the operator subroutine */
282 {
283     DelEntry (D, D->OpIndex);
284     DelEntry (D, D->PushIndex);
285 }
286
287
288
289 /*****************************************************************************/
290 /*                       Actual optimization functions                       */
291 /*****************************************************************************/
292
293
294
295 static unsigned Opt_staspidx (StackOpData* D)
296 /* Optimize the staspidx sequence if possible */
297 {
298     CodeEntry* X;
299
300     /* Store the value into the zeropage instead of pushing it */
301     ReplacePushByStore (D);
302
303     /* Replace the store subroutine call by a direct op */
304     X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
305     InsertEntry (D, X, D->OpIndex+1);
306
307     /* Remove the push and the call to the staspidx function */
308     RemovePushAndOp (D);
309
310     /* We changed the sequence */
311     return 1;
312 }
313
314
315
316 static unsigned Opt_staxspidx (StackOpData* D)
317 /* Optimize the staxspidx sequence if possible */
318 {
319     CodeEntry* X;
320
321     /* Store the value into the zeropage instead of pushing it */
322     ReplacePushByStore (D);
323
324     /* Inline the store */
325     X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
326     InsertEntry (D, X, D->OpIndex+1);
327     X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
328     InsertEntry (D, X, D->OpIndex+2);
329     if (D->OpEntry->RI->In.RegX >= 0) {
330         /* Value of X is known */
331         const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
332         X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
333     } else {
334         /* Value unknown */
335         X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
336     }
337     InsertEntry (D, X, D->OpIndex+3);
338     X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
339     InsertEntry (D, X, D->OpIndex+4);
340
341     /* Remove the push and the call to the staspidx function */
342     RemovePushAndOp (D);
343
344     /* We changed the sequence */
345     return 1;
346 }
347
348
349
350 static unsigned Opt_tosaddax (StackOpData* D)
351 /* Optimize the tosaddax sequence if possible */
352 {
353     CodeEntry*  X;
354
355
356     /* We need the entry behind the add */
357     CHECK (D->NextEntry != 0);
358
359     /* Check the entry before the push. If it's a lda instruction with an
360      * addressing mode that allows us to replace it, we may use this
361      * location for the op and must not save the value in the zero page
362      * location.
363      */
364     CheckDirectOp (D);
365
366     /* Store the value into the zeropage instead of pushing it */
367     ReplacePushByStore (D);
368
369     /* Inline the add */
370     D->IP = D->OpIndex+1;
371     X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, D->OpEntry->LI);
372     InsertEntry (D, X, D->IP++);
373
374     /* Low byte */
375     AddOpLow (D, OP65_ADC);
376
377     /* High byte */
378     if (D->PushEntry->RI->In.RegX == 0) {
379         /* The high byte is the value in X plus the carry */
380         CodeLabel* L = CS_GenLabel (D->Code, D->NextEntry);
381         X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
382         InsertEntry (D, X, D->IP++);
383         X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
384         InsertEntry (D, X, D->IP++);
385     } else if (D->OpEntry->RI->In.RegX == 0) {
386         /* The high byte is that of the first operand plus carry */
387         CodeLabel* L;
388         if (D->PushEntry->RI->In.RegX >= 0) {
389             /* Value of first op high byte is known */
390             const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX);
391             X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
392         } else {
393             /* Value of first op high byte is unknown */
394             X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
395         }
396         InsertEntry (D, X, D->IP++);
397         L = CS_GenLabel (D->Code, D->NextEntry);
398         X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
399         InsertEntry (D, X, D->IP++);
400         X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
401         InsertEntry (D, X, D->IP++);
402     } else {
403         /* High byte is unknown */
404         AddOpHigh (D, OP65_ADC);
405     }
406
407     /* Remove the push and the call to the tosaddax function */
408     RemovePushAndOp (D);
409
410     /* We changed the sequence */
411     return 1;
412 }
413
414
415
416 static unsigned Opt_tosandax (StackOpData* D)
417 /* Optimize the tosandax sequence if possible */
418 {
419     CodeEntry*  X;
420
421     /* Check the entry before the push. If it's a lda instruction with an
422      * addressing mode that allows us to replace it, we may use this
423      * location for the op and must not save the value in the zero page
424      * location.
425      */
426     CheckDirectOp (D);
427
428     /* Store the value into the zeropage instead of pushing it */
429     ReplacePushByStore (D);
430
431     /* Inline the and, low byte */
432     D->IP = D->OpIndex + 1;
433     AddOpLow (D, OP65_AND);
434
435     /* High byte */
436     if (D->PushEntry->RI->In.RegX == 0 || D->OpEntry->RI->In.RegX == 0) {
437         /* The high byte is zero */
438         X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
439         InsertEntry (D, X, D->IP++);
440     } else {
441         /* High byte is unknown */
442         AddOpHigh (D, OP65_AND);
443     }
444
445     /* Remove the push and the call to the tosandax function */
446     RemovePushAndOp (D);
447
448     /* We changed the sequence */
449     return 1;
450 }
451
452
453
454 static unsigned Opt_tosorax (StackOpData* D)
455 /* Optimize the tosorax sequence if possible */
456 {
457     CodeEntry*  X;
458
459     /* Check the entry before the push. If it's a lda instruction with an
460      * addressing mode that allows us to replace it, we may use this
461      * location for the op and must not save the value in the zero page
462      * location.
463      */
464     CheckDirectOp (D);
465
466     /* Store the value into the zeropage instead of pushing it */
467     ReplacePushByStore (D);
468
469     /* Inline the or, low byte */
470     D->IP = D->OpIndex + 1;
471     AddOpLow (D, OP65_ORA);
472
473     /* High byte */
474     if (D->PushEntry->RI->In.RegX >= 0 && D->OpEntry->RI->In.RegX >= 0) {
475         /* Both values known, precalculate the result */
476         const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX | D->OpEntry->RI->In.RegX);
477         X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
478         InsertEntry (D, X, D->IP++);
479     } else if (D->PushEntry->RI->In.RegX != 0) {
480         /* High byte is unknown */
481         AddOpHigh (D, OP65_ORA);
482     }
483
484     /* Remove the push and the call to the tosorax function */
485     RemovePushAndOp (D);
486
487     /* We changed the sequence */
488     return 1;
489 }
490
491
492
493 static unsigned Opt_tosxorax (StackOpData* D)
494 /* Optimize the tosxorax sequence if possible */
495 {
496     CodeEntry*  X;
497
498     /* Check the entry before the push. If it's a lda instruction with an
499      * addressing mode that allows us to replace it, we may use this
500      * location for the op and must not save the value in the zero page
501      * location.
502      */
503     CheckDirectOp (D);
504
505     /* Store the value into the zeropage instead of pushing it */
506     ReplacePushByStore (D);
507
508     /* Inline the xor, low byte */
509     D->IP = D->OpIndex + 1;
510     AddOpLow (D, OP65_EOR);
511
512     /* High byte */
513     if (D->PushEntry->RI->In.RegX >= 0 && D->OpEntry->RI->In.RegX >= 0) {
514         /* Both values known, precalculate the result */
515         const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
516         X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
517         InsertEntry (D, X, D->IP++);
518     } else if (D->PushEntry->RI->In.RegX != 0) {
519         /* High byte is unknown */
520         AddOpHigh (D, OP65_EOR);
521     }
522
523     /* Remove the push and the call to the tosandax function */
524     RemovePushAndOp (D);
525
526     /* We changed the sequence */
527     return 1;
528 }
529
530
531
532 /*****************************************************************************/
533 /*                                   Code                                    */
534 /*****************************************************************************/
535
536
537
538 /* Flags for the functions */
539 typedef enum {
540     STOP_NONE,              /* Nothing special */
541     STOP_A_UNUSED           /* Call only if a unused later */
542 } STOP_FLAGS;
543
544
545 typedef unsigned (*OptFunc) (StackOpData* D);
546 typedef struct OptFuncDesc OptFuncDesc;
547 struct OptFuncDesc {
548     const char*     Name;   /* Name of the replaced runtime function */
549     OptFunc         Func;   /* Function pointer */
550     STOP_FLAGS      Flags;  /* Flags */
551 };
552
553 static const OptFuncDesc FuncTable[] = {
554     { "staspidx",   Opt_staspidx,  STOP_NONE },
555     { "staxspidx",  Opt_staxspidx, STOP_A_UNUSED },
556     { "tosaddax",   Opt_tosaddax,  STOP_NONE },
557     { "tosandax",   Opt_tosandax,  STOP_NONE },
558     { "tosorax",    Opt_tosorax,   STOP_NONE },
559     { "tosxorax",   Opt_tosxorax,  STOP_NONE },
560 };
561 #define FUNC_COUNT (sizeof(FuncTable) / sizeof(FuncTable[0]))
562
563
564
565 static int CmpFunc (const void* Key, const void* Func)
566 /* Compare function for bsearch */
567 {
568     return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
569 }
570
571
572
573 static const OptFuncDesc* FindFunc (const char* Name)
574 /* Find the function with the given name. Return a pointer to the table entry
575  * or NULL if the function was not found.
576  */
577 {
578     return bsearch (Name, FuncTable, FUNC_COUNT, sizeof(OptFuncDesc), CmpFunc);
579 }
580
581
582
583 static int CmpHarmless (const void* Key, const void* Entry)
584 /* Compare function for bsearch */
585 {
586     return strcmp (Key, *(const char**)Entry);
587 }
588
589
590
591 static int HarmlessCall (const char* Name)
592 /* Check if this is a call to a harmless subroutine that will not interrupt
593  * the pushax/op sequence when encountered.
594  */
595 {
596     static const char* Tab[] = {
597         "ldaxysp",
598     };
599
600     void* R = bsearch (Name,
601                        Tab,
602                        sizeof (Tab) / sizeof (Tab[0]),
603                        sizeof (Tab[0]),
604                        CmpHarmless);
605     return (R != 0);
606 }
607
608
609
610 /*****************************************************************************/
611 /*                                   Code                                    */
612 /*****************************************************************************/
613
614
615
616 unsigned OptStackOps (CodeSeg* S)
617 /* Optimize operations that take operands via the stack */
618 {
619     unsigned    Changes = 0;    /* Number of changes in one run */
620     int         InSeq = 0;      /* Inside a sequence */
621     unsigned    Push = 0;       /* Index of pushax */
622     unsigned    UsedRegs = 0;   /* Zeropage registers used in sequence */
623     unsigned    I;
624
625
626     /* Generate register info */
627     CS_GenRegInfo (S);
628
629     /* Look for a call to pushax followed by a call to some other function
630      * that takes it's first argument on the stack, and the second argument
631      * in the primary register.
632      * It depends on the code between the two if we can handle/transform the
633      * sequence, so check this code for the following list of things:
634      *
635      *  - there must not be a jump or conditional branch (this may
636      *    get relaxed later).
637      *  - there may not be accesses to local variables with unknown
638      *    offsets (because we have to adjust these offsets).
639      *  - no subroutine calls
640      *  - no jump labels
641      *
642      * Since we need a zero page register later, do also check the
643      * intermediate code for zero page use.
644      */
645     I = 0;
646     while (I < CS_GetEntryCount (S)) {
647
648         /* Get the next entry */
649         CodeEntry* E = CS_GetEntry (S, I);
650
651         /* Handling depends if we're inside a sequence or not */
652         if (InSeq) {
653
654             if ((E->Info & OF_BRA) != 0                              ||
655                 ((E->Use & REG_SP) != 0                         &&
656                  (E->AM != AM65_ZP_INDY || E->RI->In.RegY < 0))      ||
657                 CE_HasLabel (E)) {
658
659                 /* All this stuff is not allowed in a sequence */
660                 InSeq = 0;
661
662             } else if (E->OPC == OP65_JSR) {
663
664                 /* Subroutine call: Check if this is one of our functions */
665                 const OptFuncDesc* F = FindFunc (E->Arg);
666                 if (F) {
667
668                     StackOpData Data;
669                     int PreCondOk = 1;
670
671                     /* Check the flags */
672                     if (F->Flags & STOP_A_UNUSED) {
673                         /* a must be unused later */
674                         if (RegAUsed (S, I+1)) {
675                             /* Cannot optimize */
676                             PreCondOk = 0;
677                         }
678                     }
679
680                     /* Determine the zero page locations to use */
681                     if (PreCondOk) {
682                         UsedRegs |= GetRegInfo (S, I+1, REG_SREG | REG_PTR1 | REG_PTR2);
683                         if ((UsedRegs & REG_SREG) == REG_NONE) {
684                             /* SREG is available */
685                             Data.ZPLo = "sreg";
686                             Data.ZPHi = "sreg+1";
687                         } else if ((UsedRegs & REG_PTR1) == REG_NONE) {
688                             Data.ZPLo = "ptr1";
689                             Data.ZPHi = "ptr1+1";
690                         } else if ((UsedRegs & REG_PTR2) == REG_NONE) {
691                             Data.ZPLo = "ptr2";
692                             Data.ZPHi = "ptr2+1";
693                         } else {
694                             /* No registers available */
695                             PreCondOk = 0;
696                         }
697                     }
698
699                     /* If preconditions are ok, call the optimizer function */
700                     if (PreCondOk) {
701
702                         /* Adjust stack offsets */
703                         Data.OpIndex = I + AdjustStackOffset (S, Push, I, 2);
704
705                         /* Prepare the remainder of the data structure */
706                         Data.Code      = S;
707                         Data.Flags     = 0;
708                         Data.PushIndex = Push;
709                         Data.PrevEntry = CS_GetPrevEntry (S, Data.PushIndex);
710                         Data.PushEntry = CS_GetEntry (S, Data.PushIndex);
711                         Data.OpEntry   = E;
712                         Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
713
714                         /* Call the optimizer function */
715                         Changes += F->Func (&Data);
716
717                         /* Regenerate register info */
718                         CS_GenRegInfo (S);
719                     }
720
721                     /* End of sequence */
722                     InSeq = 0;
723
724                 } else if (strcmp (E->Arg, "pushax") == 0) {
725                     /* Restart the sequence */
726                     Push     = I;
727                     UsedRegs = REG_NONE;
728                 } else if (!HarmlessCall (E->Arg)) {
729                     /* A call to an unkown subroutine ends the sequence */
730                     InSeq = 0;
731                 }
732
733             } else {
734
735                 /* Other stuff: Track zeropage register usage */
736                 UsedRegs |= (E->Use | E->Chg);
737
738             }
739
740         } else if (CE_IsCall (E, "pushax")) {
741
742             /* This starts a sequence */
743             Push     = I;
744             UsedRegs = REG_NONE;
745             InSeq    = 1;
746
747         }
748
749         /* Next entry */
750         ++I;
751
752     }
753
754     /* Free the register info */
755     CS_FreeRegInfo (S);
756
757     /* Return the number of changes made */
758     return Changes;
759 }
760
761
762