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 /*****************************************************************************/
43 /*****************************************************************************/
44 /* Optimize additions */
45 /*****************************************************************************/
49 unsigned OptAdd1 (CodeSeg* S)
50 /* Search for the sequence
70 /* Walk over the entries */
72 while (I < CS_GetEntryCount (S)) {
77 CodeEntry* E = CS_GetEntry (S, I);
79 /* Check for the sequence */
80 if (CE_IsCall (E, "pushax") &&
81 CS_GetEntries (S, L, I+1, 5) &&
82 L[0]->OPC == OP65_LDY &&
84 !CE_HasLabel (L[0]) &&
85 L[1]->OPC == OP65_LDX &&
88 !CE_HasLabel (L[1]) &&
89 L[2]->OPC == OP65_LDA &&
90 !CE_HasLabel (L[2]) &&
91 CE_IsCall (L[3], "tosaddax") &&
92 !CE_HasLabel (L[3])) {
97 /* Remove the call to pushax */
100 /* Correct the stack offset (needed since pushax was removed) */
101 CE_SetNumArg (L[0], L[0]->Num - 2);
104 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
105 CS_InsertEntry (S, X, I+1);
107 /* Remove the load */
108 CS_DelEntry (S, I+3); /* lda */
109 CS_DelEntry (S, I+2); /* ldx */
112 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
113 CS_InsertEntry (S, X, I+2);
115 /* Generate the branch label and the branch */
116 Label = CS_GenLabel (S, L[4]);
117 X = NewCodeEntry (OP65_BCC, AM65_BRA, Label->Name, Label, L[3]->LI);
118 CS_InsertEntry (S, X, I+3);
120 /* Generate the increment of the high byte */
121 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[3]->LI);
122 CS_InsertEntry (S, X, I+4);
124 /* Delete the call to tosaddax */
125 CS_DelEntry (S, I+5);
127 /* Remember, we had changes */
137 /* Return the number of changes made */
143 unsigned OptAdd2 (CodeSeg* S)
144 /* Search for the sequence
165 * provided that a/x is not used later.
168 unsigned Changes = 0;
170 /* Walk over the entries */
172 while (I < CS_GetEntryCount (S)) {
177 L[0] = CS_GetEntry (S, I);
179 /* Check for the sequence */
180 if (L[0]->OPC == OP65_LDY &&
181 CE_KnownImm (L[0]) &&
182 !CS_RangeHasLabel (S, I+1, 3) &&
183 CS_GetEntries (S, L+1, I+1, 3) &&
184 CE_IsCall (L[1], "ldaxysp") &&
185 L[2]->OPC == OP65_LDY &&
186 CE_KnownImm (L[2]) &&
187 CE_IsCall (L[3], "addeqysp") &&
188 (GetRegInfo (S, I+4, REG_AX) & REG_AX) == 0) {
190 /* Insert new code behind the addeqysp */
195 Arg = MakeHexArg (L[0]->Num-1);
196 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[0]->LI);
197 CS_InsertEntry (S, X, I+4);
200 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
201 CS_InsertEntry (S, X, I+5);
204 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[2]->Arg, 0, L[2]->LI);
205 CS_InsertEntry (S, X, I+6);
208 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
209 CS_InsertEntry (S, X, I+7);
212 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
213 CS_InsertEntry (S, X, I+8);
216 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
217 CS_InsertEntry (S, X, I+9);
220 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
221 CS_InsertEntry (S, X, I+10);
224 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
225 CS_InsertEntry (S, X, I+11);
228 Arg = MakeHexArg (L[2]->Num+1);
229 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[2]->LI);
230 CS_InsertEntry (S, X, I+12);
233 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
234 CS_InsertEntry (S, X, I+13);
237 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
238 CS_InsertEntry (S, X, I+14);
240 /* Delete the old code */
241 CS_DelEntries (S, I, 4);
243 /* Remember, we had changes */
253 /* Return the number of changes made */
259 unsigned OptAdd3 (CodeSeg* S)
260 /* Search for the sequence
267 * and remove the handling of the high byte if X is not used later.
270 unsigned Changes = 0;
272 /* Walk over the entries */
274 while (I < CS_GetEntryCount (S)) {
279 CodeEntry* E = CS_GetEntry (S, I);
281 /* Check for the sequence */
282 if (E->OPC == OP65_ADC &&
283 CS_GetEntries (S, L, I+1, 3) &&
284 (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
286 !CE_HasLabel (L[0]) &&
287 L[1]->OPC == OP65_INX &&
288 !CE_HasLabel (L[1]) &&
289 L[0]->JumpTo->Owner == L[2] &&
290 !RegXUsed (S, I+3)) {
292 /* Remove the bcs/dex */
293 CS_DelEntries (S, I+1, 2);
295 /* Remember, we had changes */
305 /* Return the number of changes made */