1 /*****************************************************************************/
5 /* Optimize addition sequences */
9 /* (C) 2001 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
73 /* Walk over the entries */
75 while (I < CS_GetEntryCount (S)) {
80 CodeEntry* E = CS_GetEntry (S, I);
82 /* Check for the sequence */
83 if (CE_IsCall (E, "pushax") &&
84 CS_GetEntries (S, L, I+1, 5) &&
85 L[0]->OPC == OP65_LDY &&
87 !CE_HasLabel (L[0]) &&
88 L[1]->OPC == OP65_LDX &&
91 !CE_HasLabel (L[1]) &&
92 L[2]->OPC == OP65_LDA &&
93 !CE_HasLabel (L[2]) &&
94 CE_IsCall (L[3], "tosaddax") &&
95 !CE_HasLabel (L[3])) {
100 /* Remove the call to pushax */
103 /* Correct the stack offset (needed since pushax was removed) */
104 CE_SetNumArg (L[0], L[0]->Num - 2);
107 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
108 CS_InsertEntry (S, X, I+1);
110 /* Remove the load */
111 CS_DelEntry (S, I+3); /* lda */
112 CS_DelEntry (S, I+2); /* ldx */
115 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
116 CS_InsertEntry (S, X, I+2);
118 /* Generate the branch label and the branch */
119 Label = CS_GenLabel (S, L[4]);
120 X = NewCodeEntry (OP65_BCC, AM65_BRA, Label->Name, Label, L[3]->LI);
121 CS_InsertEntry (S, X, I+3);
123 /* Generate the increment of the high byte */
124 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[3]->LI);
125 CS_InsertEntry (S, X, I+4);
127 /* Delete the call to tosaddax */
128 CS_DelEntry (S, I+5);
130 /* Remember, we had changes */
140 /* Return the number of changes made */
146 unsigned OptAdd2 (CodeSeg* S)
147 /* Search for the sequence
168 * provided that a/x is not used later.
171 unsigned Changes = 0;
173 /* Walk over the entries */
175 while (I < CS_GetEntryCount (S)) {
180 L[0] = CS_GetEntry (S, I);
182 /* Check for the sequence */
183 if (L[0]->OPC == OP65_LDY &&
184 CE_KnownImm (L[0]) &&
185 !CS_RangeHasLabel (S, I+1, 3) &&
186 CS_GetEntries (S, L+1, I+1, 3) &&
187 CE_IsCall (L[1], "ldaxysp") &&
188 L[2]->OPC == OP65_LDY &&
189 CE_KnownImm (L[2]) &&
190 CE_IsCall (L[3], "addeqysp") &&
191 (GetRegInfo (S, I+4, REG_AX) & REG_AX) == 0) {
193 /* Insert new code behind the addeqysp */
198 xsprintf (Buf, sizeof (Buf), "$%02X", (int)(L[0]->Num-1));
199 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[0]->LI);
200 CS_InsertEntry (S, X, I+4);
203 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
204 CS_InsertEntry (S, X, I+5);
207 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[2]->Arg, 0, L[2]->LI);
208 CS_InsertEntry (S, X, I+6);
211 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
212 CS_InsertEntry (S, X, I+7);
215 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
216 CS_InsertEntry (S, X, I+8);
219 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
220 CS_InsertEntry (S, X, I+9);
223 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
224 CS_InsertEntry (S, X, I+10);
227 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
228 CS_InsertEntry (S, X, I+11);
231 xsprintf (Buf, sizeof (Buf), "$%02X", (int)(L[2]->Num+1));
232 X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[2]->LI);
233 CS_InsertEntry (S, X, I+12);
236 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
237 CS_InsertEntry (S, X, I+13);
240 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
241 CS_InsertEntry (S, X, I+14);
243 /* Delete the old code */
244 CS_DelEntries (S, I, 4);
246 /* Remember, we had changes */
256 /* Return the number of changes made */
262 unsigned OptAdd3 (CodeSeg* S)
263 /* Search for the sequence
270 * and remove the handling of the high byte if X is not used later.
273 unsigned Changes = 0;
275 /* Walk over the entries */
277 while (I < CS_GetEntryCount (S)) {
282 CodeEntry* E = CS_GetEntry (S, I);
284 /* Check for the sequence */
285 if (E->OPC == OP65_ADC &&
286 CS_GetEntries (S, L, I+1, 3) &&
287 (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
289 !CE_HasLabel (L[0]) &&
290 L[1]->OPC == OP65_INX &&
291 !CE_HasLabel (L[1]) &&
292 L[0]->JumpTo->Owner == L[2] &&
293 !RegXUsed (S, I+3)) {
295 /* Remove the bcs/dex */
296 CS_DelEntries (S, I+1, 2);
298 /* Remember, we had changes */
308 /* Return the number of changes made */