]> git.sur5r.net Git - cc65/blob - src/cc65/coptsize.c
6a04240d6640bb0213b20d27766f342bd1860a3e
[cc65] / src / cc65 / coptsize.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 coptsize.c                                */
4 /*                                                                           */
5 /*                              Size optimizations                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 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 "cpu.h"
42 #include "coptsize.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Data                                    */
48 /*****************************************************************************/
49
50
51
52 typedef struct CallDesc CallDesc;
53 struct CallDesc {
54     const char*     LongFunc;       /* Long function name */
55     short           A, X, Y;        /* Register contents */
56     const char*     ShortFunc;      /* Short function name */
57 };
58
59 /* Note: The table is sorted. If there is more than one entry with the same
60  * name, entries are sorted best match first, so when searching linear for
61  * a match, the first one can be used because it is also the best one (or
62  * at least none of the following ones are better).
63  */
64 static const CallDesc CallTable [] = {
65     { "addeqysp",   -1,   -1,    0, "addeq0sp"      },
66     { "laddeqysp",  -1,   -1,    0, "laddeq0sp"     },
67     { "ldaxidx",    -1,   -1,    1, "ldaxi"         },
68     { "ldaxysp",    -1,   -1,    1, "ldax0sp"       },
69     { "ldeaxidx",   -1,   -1,    3, "ldeaxi"        },
70     { "ldeaxysp",   -1,   -1,    3, "ldeax0sp"      },
71     { "pusha",       0,   -1,   -1, "pushc0"        },
72     { "pusha",       1,   -1,   -1, "pushc1"        },
73     { "pusha",       2,   -1,   -1, "pushc2"        },
74     { "pushax",      0,    0,   -1, "push0"         },
75     { "pushax",      1,    0,   -1, "push1"         },
76     { "pushax",      2,    0,   -1, "push2"         },
77     { "pushax",      3,    0,   -1, "push3"         },
78     { "pushax",      4,    0,   -1, "push4"         },
79     { "pushax",      5,    0,   -1, "push5"         },
80     { "pushax",      6,    0,   -1, "push6"         },
81     { "pushax",      7,    0,   -1, "push7"         },
82     { "pushax",     -1,    0,   -1, "pusha0"        },
83     { "pushax",     -1, 0xFF,   -1, "pushaFF"       },
84     { "pushaysp",   -1,   -1,    0, "pusha0sp"      },
85     { "staxysp",    -1,   -1,    0, "stax0sp"       },
86     { "tosaddax",   -1,    0,   -1, "tosadda0"      },
87     { "tosandax",   -1,    0,   -1, "tosanda0"      },
88     { "tosdivax",   -1,    0,   -1, "tosdiva0"      },
89     { "toseqax",    -1,    0,   -1, "toseqa0"       },
90     { "tosgeax",    -1,    0,   -1, "tosgea0"       },
91     { "tosgtax",    -1,    0,   -1, "tosgta0"       },
92     { "tosleax",    -1,    0,   -1, "toslea0"       },
93     { "tosorax",    -1,    0,   -1, "tosora0"       },
94     { "lsubeqysp",  -1,   -1,    0, "lsubeq0sp"     },
95     { "steaxysp",   -1,   -1,    0, "steax0sp"      },
96     { "subeqysp",   -1,   -1,    0, "subeq0sp"      },
97     { "tosaslax",   -1,    0,   -1, "tosasla0"      },
98     { "tosasrax",   -1,    0,   -1, "tosasra0"      },
99     { "tosltax",    -1,    0,   -1, "toslta0"       },
100     { "tosmodax",   -1,    0,   -1, "tosmoda0"      },
101     { "tosmulax",   -1,    0,   -1, "tosmula0"      },
102     { "tosneax",    -1,    0,   -1, "tosnea0"       },
103     { "tosrsubax",  -1,    0,   -1, "tosrsuba0"     },
104     { "tosshlax",   -1,    0,   -1, "tosshla0"      },
105     { "tosshrax",   -1,    0,   -1, "tosshra0"      },
106     { "tossubax",   -1,    0,   -1, "tossuba0"      },
107     { "tosudivax",  -1,    0,   -1, "tosudiva0"     },
108     { "tosugeax",   -1,    0,   -1, "tosugea0"      },
109     { "tosugtax",   -1,    0,   -1, "tosugta0"      },
110     { "tosuleax",   -1,    0,   -1, "tosulea0"      },
111     { "tosultax",   -1,    0,   -1, "tosulta0"      },
112     { "tosumodax",  -1,    0,   -1, "tosumoda0"     },
113     { "tosumulax",  -1,    0,   -1, "tosumula0"     },
114     { "tosxorax",   -1,    0,   -1, "tosxora0"      },
115
116 #if 0
117     "tosadd0ax",         /* tosaddeax, sreg = 0 */
118     "laddeqa",           /* laddeq, sreg = 0, x = 0 */
119     "laddeq1",           /* laddeq, sreg = 0, x = 0, a = 1 */
120     "tosand0ax",         /* tosandeax, sreg = 0 */
121     "tosdiv0ax",         /* tosdiveax, sreg = 0 */
122     "tosmod0ax",         /* tosmodeax, sreg = 0 */
123     "tosmul0ax",         /* tosmuleax, sreg = 0 */
124     "tosumul0ax",        /* tosumuleax, sreg = 0 */
125     "tosor0ax",          /* tosoreax, sreg = 0 */
126     "push0ax",           /* pusheax, sreg = 0 */
127     "tosrsub0ax",        /* tosrsubeax, sreg = 0 */
128     "tosshl0ax",         /* tosshleax, sreg = 0 */
129     "tosasl0ax",         /* tosasleax, sreg = 0 */
130     "tosshr0ax",         /* tosshreax, sreg = 0 */
131     "tosasr0ax",         /* tosasreax, sreg = 0 */
132     "tossub0ax",         /* tossubeax, sreg = 0 */
133     "lsubeqa",           /* lsubeq, sreg = 0, x = 0 */
134     "lsubeq1",           /* lsubeq, sreg = 0, x = 0, a = 1 */
135     "tosudiv0ax",        /* tosudiveax, sreg = 0 */
136     "tosumod0ax",        /* tosumodeax, sreg = 0 */
137     "tosxor0ax",         /* tosxoreax, sreg = 0 */
138 #endif
139 };
140 #define CALL_COUNT (sizeof(CallTable) / sizeof(CallTable[0]))
141
142
143
144 /*****************************************************************************/
145 /*                                  Helpers                                  */
146 /*****************************************************************************/
147
148
149
150 static const CallDesc* FindCall (const char* Name)
151 /* Find the function with the given name. Return a pointer to the table entry
152  * or NULL if the function was not found.
153  */
154 {
155     /* Do a binary search */
156     int First = 0;
157     int Last = CALL_COUNT - 1;
158     int Found = 0;
159
160     while (First <= Last) {
161
162         /* Set current to mid of range */
163         int Current = (Last + First) / 2;
164
165         /* Do a compare */
166         int Result = strcmp (CallTable[Current].LongFunc, Name);
167         if (Result < 0) {
168             First = Current + 1;
169         } else {
170             Last = Current - 1;
171             if (Result == 0) {
172                 /* Found. Repeat the procedure until the first of all entries
173                  * with the same name is found.
174                  */
175                 Found = 1;
176             }
177         }
178     }
179
180     /* Return the first entry if found, or NULL otherwise */
181     return Found? &CallTable[First] : 0;
182 }
183
184
185
186 /*****************************************************************************/
187 /*                                   Code                                    */
188 /*****************************************************************************/
189
190
191
192 unsigned OptSize1 (CodeSeg* S)
193 /* Do size optimization by calling special subroutines that preload registers.
194  * This routine does not work standalone, it needs a following register load
195  * removal pass.
196  */
197 {
198     CodeEntry* E;
199     unsigned Changes = 0;
200     unsigned I;
201
202     /* Generate register info for the following step */
203     CS_GenRegInfo (S);
204
205     /* Walk over the entries */
206     I = 0;
207     while (I < CS_GetEntryCount (S)) {
208
209         const CallDesc* D;
210
211         /* Get next entry */
212         E = CS_GetEntry (S, I);
213
214         /* Check if it's a subroutine call */
215         if (E->OPC == OP65_JSR && (D = FindCall (E->Arg)) != 0) {
216
217             /* Check for any of the known functions. */
218             while (1) {
219
220                 /* Check the registers */
221                 if ((D->A < 0 || D->A == E->RI->In.RegA) &&
222                     (D->X < 0 || D->X == E->RI->In.RegX) &&
223                     (D->Y < 0 || D->Y == E->RI->In.RegY)) {
224                     /* Ok, match for all registers */
225                     CodeEntry* X;
226                     X = NewCodeEntry (E->OPC, E->AM, D->ShortFunc, 0, E->LI);
227                     CS_InsertEntry (S, X, I+1);
228                     CS_DelEntry (S, I);
229
230                     /* Remember that we had changes */
231                     ++Changes;
232                 }
233
234                 /* Next table entry, bail out if next entry not valid */
235                 if (++D >= CallTable + CALL_COUNT ||
236                     strcmp (D->LongFunc, E->Arg) != 0) {
237                     /* End of table or entries reached */
238                     break;
239                 }
240             }
241         }
242
243         /* Next entry */
244         ++I;
245
246     }
247
248     /* Free register info */
249     CS_FreeRegInfo (S);
250
251     /* Return the number of changes made */
252     return Changes;
253 }
254
255
256
257 unsigned OptSize2 (CodeSeg* S)
258 /* Do size optimization by using shorter code sequences, even if this
259  * introduces relations between instructions. This step must be one of the
260  * last steps, because it makes further work much more difficult.
261  */
262 {
263     unsigned Changes = 0;
264     unsigned I;
265
266     /* Generate register info for the following step */
267     CS_GenRegInfo (S);
268
269     /* Walk over the entries */
270     I = 0;
271     while (I < CS_GetEntryCount (S)) {
272
273         /* Get next entry */
274         CodeEntry* E = CS_GetEntry (S, I);
275
276         /* Get the input registers */
277         const RegContents* In = &E->RI->In;
278
279         /* Assume we have no replacement */
280         CodeEntry* X = 0;
281
282         /* Check the instruction */
283         switch (E->OPC) {
284
285             case OP65_LDA:
286                 if (CE_KnownImm (E)) {
287                     short Val = (short) E->Num;
288                     if (Val == In->RegX) {
289                         X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
290                     } else if (Val == In->RegY) {
291                         X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
292                     } else if (RegValIsKnown (In->RegA) && CPU >= CPU_65C02) {
293                         if (Val == ((In->RegA - 1) & 0xFF)) {
294                             X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
295                         } else if (Val == ((In->RegA + 1) & 0xFF)) {
296                             X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
297                         }
298                     }
299                 }
300                 break;
301
302             case OP65_LDX:
303                 if (CE_KnownImm (E)) {
304                     short Val = (short) E->Num;
305                     if (RegValIsKnown (In->RegX) && Val == ((In->RegX - 1) & 0xFF)) {
306                         X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
307                     } else if (RegValIsKnown (In->RegX) && Val == ((In->RegX + 1) & 0xFF)) {
308                         X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
309                     } else if (Val == In->RegA) {
310                         X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
311                     }
312                 }
313                 break;
314
315             case OP65_LDY:
316                 if (CE_KnownImm (E)) {
317                     short Val = (short) E->Num;
318                     if (RegValIsKnown (In->RegY) && Val == ((In->RegY - 1) & 0xFF)) {
319                         X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
320                     } else if (RegValIsKnown (In->RegY) && Val == ((In->RegY + 1) & 0xFF)) {
321                         X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
322                     } else if (Val == In->RegA) {
323                         X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
324                     }
325                 }
326                 break;
327
328             default:
329                 /* Avoid gcc warnings */
330                 break;
331
332         }
333
334         /* Insert the replacement if we have one */
335         if (X) {
336             CS_InsertEntry (S, X, I+1);
337             CS_DelEntry (S, I);
338             ++Changes;
339         }
340
341         /* Next entry */
342         ++I;
343
344     }
345
346     /* Free register info */
347     CS_FreeRegInfo (S);
348
349     /* Return the number of changes made */
350     return Changes;
351 }
352
353
354