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