1 /*****************************************************************************/
5 /* Optimize subtraction 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 /*****************************************************************************/
43 /*****************************************************************************/
44 /* Optimize subtractions */
45 /*****************************************************************************/
49 unsigned OptSub1 (CodeSeg* S)
50 /* Search for the sequence
57 * and remove the handling of the high byte if X is not used later.
62 /* Walk over the entries */
64 while (I < CS_GetEntryCount (S)) {
69 CodeEntry* E = CS_GetEntry (S, I);
71 /* Check for the sequence */
72 if (E->OPC == OP65_SBC &&
73 CS_GetEntries (S, L, I+1, 3) &&
74 (L[0]->OPC == OP65_BCS || L[0]->OPC == OP65_JCS) &&
76 !CE_HasLabel (L[0]) &&
77 L[1]->OPC == OP65_DEX &&
78 !CE_HasLabel (L[1]) &&
79 L[0]->JumpTo->Owner == L[2] &&
82 /* Remove the bcs/dex */
83 CS_DelEntries (S, I+1, 2);
85 /* Remember, we had changes */
95 /* Return the number of changes made */
101 unsigned OptSub2 (CodeSeg* S)
102 /* Search for the sequence
119 unsigned Changes = 0;
121 /* Walk over the entries */
123 while (I < CS_GetEntryCount (S)) {
128 CodeEntry* E = CS_GetEntry (S, I);
130 /* Check for the sequence */
131 if (E->OPC == OP65_LDA &&
132 !CS_RangeHasLabel (S, I+1, 5) &&
133 CS_GetEntries (S, L, I+1, 5) &&
134 L[0]->OPC == OP65_SEC &&
135 L[1]->OPC == OP65_STA &&
136 strcmp (L[1]->Arg, "tmp1") == 0 &&
137 L[2]->OPC == OP65_LDA &&
138 L[3]->OPC == OP65_SBC &&
139 strcmp (L[3]->Arg, "tmp1") == 0 &&
140 L[4]->OPC == OP65_STA &&
141 strcmp (L[4]->Arg, L[2]->Arg) == 0) {
143 /* Remove the store to tmp1 */
144 CS_DelEntry (S, I+2);
146 /* Remove the subtraction */
147 CS_DelEntry (S, I+3);
149 /* Move the lda to the position of the subtraction and change the
152 CS_MoveEntry (S, I, I+3);
153 CE_ReplaceOPC (E, OP65_SBC);
155 /* If the sequence head had a label, move this label back to the
158 if (CE_HasLabel (E)) {
159 CS_MoveLabels (S, E, L[0]);
162 /* Remember, we had changes */
172 /* Return the number of changes made */