1 /*****************************************************************************/
5 /* Size optimizations */
9 /* (C) 2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 typedef struct CallDesc CallDesc;
54 const char* LongFunc; /* Long function name */
55 short A, X, Y; /* Register contents */
56 const char* ShortFunc; /* Short function name */
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).
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 { "pushwidx", -1, -1, 1, "pushw" },
86 { "pushwysp", -1, -1, 3, "pushw0sp" },
87 { "staxysp", -1, -1, 0, "stax0sp" },
88 { "tosaddax", -1, 0, -1, "tosadda0" },
89 { "tosandax", -1, 0, -1, "tosanda0" },
90 { "tosdivax", -1, 0, -1, "tosdiva0" },
91 { "toseqax", -1, 0, -1, "toseqa0" },
92 { "tosgeax", -1, 0, -1, "tosgea0" },
93 { "tosgtax", -1, 0, -1, "tosgta0" },
94 { "tosleax", -1, 0, -1, "toslea0" },
95 { "tosorax", -1, 0, -1, "tosora0" },
96 { "lsubeqysp", -1, -1, 0, "lsubeq0sp" },
97 { "steaxysp", -1, -1, 0, "steax0sp" },
98 { "subeqysp", -1, -1, 0, "subeq0sp" },
99 { "tosaslax", -1, 0, -1, "tosasla0" },
100 { "tosasrax", -1, 0, -1, "tosasra0" },
101 { "tosltax", -1, 0, -1, "toslta0" },
102 { "tosmodax", -1, 0, -1, "tosmoda0" },
103 { "tosmulax", -1, 0, -1, "tosmula0" },
104 { "tosneax", -1, 0, -1, "tosnea0" },
105 { "tosrsubax", -1, 0, -1, "tosrsuba0" },
106 { "tosshlax", -1, 0, -1, "tosshla0" },
107 { "tosshrax", -1, 0, -1, "tosshra0" },
108 { "tossubax", -1, 0, -1, "tossuba0" },
109 { "tosudivax", -1, 0, -1, "tosudiva0" },
110 { "tosugeax", -1, 0, -1, "tosugea0" },
111 { "tosugtax", -1, 0, -1, "tosugta0" },
112 { "tosuleax", -1, 0, -1, "tosulea0" },
113 { "tosultax", -1, 0, -1, "tosulta0" },
114 { "tosumodax", -1, 0, -1, "tosumoda0" },
115 { "tosumulax", -1, 0, -1, "tosumula0" },
116 { "tosxorax", -1, 0, -1, "tosxora0" },
119 "tosadd0ax", /* tosaddeax, sreg = 0 */
120 "laddeqa", /* laddeq, sreg = 0, x = 0 */
121 "laddeq1", /* laddeq, sreg = 0, x = 0, a = 1 */
122 "tosand0ax", /* tosandeax, sreg = 0 */
123 "tosdiv0ax", /* tosdiveax, sreg = 0 */
124 "tosmod0ax", /* tosmodeax, sreg = 0 */
125 "tosmul0ax", /* tosmuleax, sreg = 0 */
126 "tosumul0ax", /* tosumuleax, sreg = 0 */
127 "tosor0ax", /* tosoreax, sreg = 0 */
128 "push0ax", /* pusheax, sreg = 0 */
129 "tosrsub0ax", /* tosrsubeax, sreg = 0 */
130 "tosshl0ax", /* tosshleax, sreg = 0 */
131 "tosasl0ax", /* tosasleax, sreg = 0 */
132 "tosshr0ax", /* tosshreax, sreg = 0 */
133 "tosasr0ax", /* tosasreax, sreg = 0 */
134 "tossub0ax", /* tossubeax, sreg = 0 */
135 "lsubeqa", /* lsubeq, sreg = 0, x = 0 */
136 "lsubeq1", /* lsubeq, sreg = 0, x = 0, a = 1 */
137 "tosudiv0ax", /* tosudiveax, sreg = 0 */
138 "tosumod0ax", /* tosumodeax, sreg = 0 */
139 "tosxor0ax", /* tosxoreax, sreg = 0 */
142 #define CALL_COUNT (sizeof(CallTable) / sizeof(CallTable[0]))
146 /*****************************************************************************/
148 /*****************************************************************************/
152 static const CallDesc* FindCall (const char* Name)
153 /* Find the function with the given name. Return a pointer to the table entry
154 * or NULL if the function was not found.
157 /* Do a binary search */
159 int Last = CALL_COUNT - 1;
162 while (First <= Last) {
164 /* Set current to mid of range */
165 int Current = (Last + First) / 2;
168 int Result = strcmp (CallTable[Current].LongFunc, Name);
174 /* Found. Repeat the procedure until the first of all entries
175 * with the same name is found.
182 /* Return the first entry if found, or NULL otherwise */
183 return Found? &CallTable[First] : 0;
188 /*****************************************************************************/
190 /*****************************************************************************/
194 unsigned OptSize1 (CodeSeg* S)
195 /* Do size optimization by calling special subroutines that preload registers.
196 * This routine does not work standalone, it needs a following register load
201 unsigned Changes = 0;
204 /* Generate register info for the following step */
207 /* Walk over the entries */
209 while (I < CS_GetEntryCount (S)) {
214 E = CS_GetEntry (S, I);
216 /* Check if it's a subroutine call */
217 if (E->OPC == OP65_JSR && (D = FindCall (E->Arg)) != 0) {
219 /* Check for any of the known functions. */
222 /* Check the registers */
223 if ((D->A < 0 || D->A == E->RI->In.RegA) &&
224 (D->X < 0 || D->X == E->RI->In.RegX) &&
225 (D->Y < 0 || D->Y == E->RI->In.RegY)) {
227 /* Ok, match for all registers */
229 X = NewCodeEntry (E->OPC, E->AM, D->ShortFunc, 0, E->LI);
230 CS_InsertEntry (S, X, I+1);
233 /* Remember that we had changes */
240 /* Next table entry, bail out if next entry not valid */
241 if (++D >= CallTable + CALL_COUNT ||
242 strcmp (D->LongFunc, E->Arg) != 0) {
243 /* End of table or entries reached */
254 /* Free register info */
257 /* Return the number of changes made */
263 unsigned OptSize2 (CodeSeg* S)
264 /* Do size optimization by using shorter code sequences, even if this
265 * introduces relations between instructions. This step must be one of the
266 * last steps, because it makes further work much more difficult.
269 unsigned Changes = 0;
272 /* Generate register info for the following step */
275 /* Walk over the entries */
277 while (I < CS_GetEntryCount (S)) {
280 CodeEntry* E = CS_GetEntry (S, I);
282 /* Get the input registers */
283 const RegContents* In = &E->RI->In;
285 /* Assume we have no replacement */
288 /* Check the instruction */
292 if (CE_KnownImm (E)) {
293 short Val = (short) E->Num;
294 if (Val == In->RegX) {
295 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, E->LI);
296 } else if (Val == In->RegY) {
297 X = NewCodeEntry (OP65_TYA, AM65_IMP, 0, 0, E->LI);
298 } else if (RegValIsKnown (In->RegA) && CPU >= CPU_65C02) {
299 if (Val == ((In->RegA - 1) & 0xFF)) {
300 X = NewCodeEntry (OP65_DEA, AM65_IMP, 0, 0, E->LI);
301 } else if (Val == ((In->RegA + 1) & 0xFF)) {
302 X = NewCodeEntry (OP65_INA, AM65_IMP, 0, 0, E->LI);
309 if (CE_KnownImm (E)) {
310 short Val = (short) E->Num;
311 if (RegValIsKnown (In->RegX) && Val == ((In->RegX - 1) & 0xFF)) {
312 X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, E->LI);
313 } else if (RegValIsKnown (In->RegX) && Val == ((In->RegX + 1) & 0xFF)) {
314 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
315 } else if (Val == In->RegA) {
316 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, E->LI);
322 if (CE_KnownImm (E)) {
323 short Val = (short) E->Num;
324 if (RegValIsKnown (In->RegY) && Val == ((In->RegY - 1) & 0xFF)) {
325 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, E->LI);
326 } else if (RegValIsKnown (In->RegY) && Val == ((In->RegY + 1) & 0xFF)) {
327 X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, E->LI);
328 } else if (Val == In->RegA) {
329 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, E->LI);
335 /* Avoid gcc warnings */
340 /* Insert the replacement if we have one */
342 CS_InsertEntry (S, X, I+1);
352 /* Free register info */
355 /* Return the number of changes made */