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