1 /*****************************************************************************/
5 /* Optimize addition sequences */
9 /* (C) 2001-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 /*****************************************************************************/
47 /* Optimize additions */
48 /*****************************************************************************/
52 unsigned OptAdd1 (CodeSeg* S)
53 /* Search for the sequence
80 /* Walk over the entries */
82 while (I < CS_GetEntryCount (S)) {
87 L[0] = CS_GetEntry (S, I);
89 /* Check for the sequence */
90 if (L[0]->OPC == OP65_LDY &&
92 !CS_RangeHasLabel (S, I+1, 5) &&
93 CS_GetEntries (S, L+1, I+1, 5) &&
94 CE_IsCallTo (L[1], "ldaxysp") &&
95 CE_IsCallTo (L[2], "pushax") &&
96 L[3]->OPC == OP65_LDY &&
98 CE_IsCallTo (L[4], "ldaxysp") &&
99 CE_IsCallTo (L[5], "tosaddax")) {
104 /* Correct the stack of the first Y register load */
105 CE_SetNumArg (L[0], L[0]->Num - 1);
108 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
109 CS_InsertEntry (S, X, I+1);
112 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[5]->LI);
113 CS_InsertEntry (S, X, I+2);
116 Arg = MakeHexArg (L[3]->Num - 3);
117 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[4]->LI);
118 CS_InsertEntry (S, X, I+3);
121 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[5]->LI);
122 CS_InsertEntry (S, X, I+4);
125 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, L[5]->LI);
126 CS_InsertEntry (S, X, I+5);
128 /* ldy #xx (beware: L[0] has changed) */
129 Arg = MakeHexArg (L[0]->Num + 1);
130 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[1]->LI);
131 CS_InsertEntry (S, X, I+6);
134 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
135 CS_InsertEntry (S, X, I+7);
138 Arg = MakeHexArg (L[3]->Num - 2);
139 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[4]->LI);
140 CS_InsertEntry (S, X, I+8);
143 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[5]->LI);
144 CS_InsertEntry (S, X, I+9);
147 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[5]->LI);
148 CS_InsertEntry (S, X, I+10);
151 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, L[5]->LI);
152 CS_InsertEntry (S, X, I+11);
154 /* Delete the old code */
155 CS_DelEntries (S, I+12, 5);
157 /* Remember, we had changes */
167 /* Return the number of changes made */
173 unsigned OptAdd2 (CodeSeg* S)
174 /* Search for the sequence
195 * provided that a/x is not used later.
198 unsigned Changes = 0;
200 /* Walk over the entries */
202 while (I < CS_GetEntryCount (S)) {
207 L[0] = CS_GetEntry (S, I);
209 /* Check for the sequence */
210 if (L[0]->OPC == OP65_LDY &&
211 CE_KnownImm (L[0]) &&
212 !CS_RangeHasLabel (S, I+1, 3) &&
213 CS_GetEntries (S, L+1, I+1, 3) &&
214 CE_IsCallTo (L[1], "ldaxysp") &&
215 L[2]->OPC == OP65_LDY &&
216 CE_KnownImm (L[2]) &&
217 CE_IsCallTo (L[3], "addeqysp") &&
218 (GetRegInfo (S, I+4, REG_AX) & REG_AX) == 0) {
220 /* Insert new code behind the addeqysp */
225 Arg = MakeHexArg (L[0]->Num-1);
226 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[0]->LI);
227 CS_InsertEntry (S, X, I+4);
230 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
231 CS_InsertEntry (S, X, I+5);
234 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[2]->Arg, 0, L[2]->LI);
235 CS_InsertEntry (S, X, I+6);
238 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
239 CS_InsertEntry (S, X, I+7);
242 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
243 CS_InsertEntry (S, X, I+8);
246 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
247 CS_InsertEntry (S, X, I+9);
250 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
251 CS_InsertEntry (S, X, I+10);
254 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
255 CS_InsertEntry (S, X, I+11);
258 Arg = MakeHexArg (L[2]->Num+1);
259 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[2]->LI);
260 CS_InsertEntry (S, X, I+12);
263 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
264 CS_InsertEntry (S, X, I+13);
267 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
268 CS_InsertEntry (S, X, I+14);
270 /* Delete the old code */
271 CS_DelEntries (S, I, 4);
273 /* Remember, we had changes */
283 /* Return the number of changes made */
289 unsigned OptAdd3 (CodeSeg* S)
290 /* Search for the sequence
308 unsigned Changes = 0;
310 /* Walk over the entries */
312 while (I < CS_GetEntryCount (S)) {
317 L[0] = CS_GetEntry (S, I);
319 /* Check for the sequence */
320 if (CE_IsCallTo (L[0], "pushax") &&
321 CS_GetEntries (S, L+1, I+1, 3) &&
322 !CS_RangeHasLabel (S, I+1, 3) &&
323 L[1]->OPC == OP65_LDA &&
324 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
325 L[2]->OPC == OP65_LDX &&
326 (L[2]->AM == AM65_ABS || L[2]->AM == AM65_ZP) &&
327 CE_IsCallTo (L[3], "tosaddax")) {
331 /* Insert new code behind the sequence */
332 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
333 CS_InsertEntry (S, X, I+4);
336 X = NewCodeEntry (OP65_ADC, L[1]->AM, L[1]->Arg, 0, L[3]->LI);
337 CS_InsertEntry (S, X, I+5);
340 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, L[3]->LI);
341 CS_InsertEntry (S, X, I+6);
344 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
345 CS_InsertEntry (S, X, I+7);
348 X = NewCodeEntry (OP65_ADC, L[2]->AM, L[2]->Arg, 0, L[3]->LI);
349 CS_InsertEntry (S, X, I+8);
352 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
353 CS_InsertEntry (S, X, I+9);
356 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, L[3]->LI);
357 CS_InsertEntry (S, X, I+10);
359 /* Delete the old code */
360 CS_DelEntries (S, I, 4);
362 /* Remember, we had changes */
372 /* Return the number of changes made */
378 unsigned OptAdd4 (CodeSeg* S)
379 /* Search for a call to incaxn and replace it by an 8 bit add if the X register
383 unsigned Changes = 0;
385 /* Walk over the entries */
387 while (I < CS_GetEntryCount (S)) {
392 E = CS_GetEntry (S, I);
394 /* Check for the sequence */
395 if (E->OPC == OP65_JSR &&
396 strncmp (E->Arg, "incax", 5) == 0 &&
397 IsDigit (E->Arg[5]) &&
399 !RegXUsed (S, I+1)) {
404 /* Insert new code behind the sequence */
405 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
406 CS_InsertEntry (S, X, I+1);
408 Arg = MakeHexArg (E->Arg[5] - '0');
409 X = NewCodeEntry (OP65_ADC, AM65_IMM, Arg, 0, E->LI);
410 CS_InsertEntry (S, X, I+2);
412 /* Delete the old code */
415 /* Remember, we had changes */
425 /* Return the number of changes made */
431 unsigned OptAdd5 (CodeSeg* S)
432 /* Search for the sequence
439 * and remove the handling of the high byte if X is not used later.
442 unsigned Changes = 0;
444 /* Walk over the entries */
446 while (I < CS_GetEntryCount (S)) {
451 CodeEntry* E = CS_GetEntry (S, I);
453 /* Check for the sequence */
454 if (E->OPC == OP65_ADC &&
455 CS_GetEntries (S, L, I+1, 3) &&
456 (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
458 !CE_HasLabel (L[0]) &&
459 L[1]->OPC == OP65_INX &&
460 !CE_HasLabel (L[1]) &&
461 L[0]->JumpTo->Owner == L[2] &&
462 !RegXUsed (S, I+3)) {
464 /* Remove the bcs/dex */
465 CS_DelEntries (S, I+1, 2);
467 /* Remember, we had changes */
477 /* Return the number of changes made */