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 /*****************************************************************************/
45 /*****************************************************************************/
46 /* Optimize subtractions */
47 /*****************************************************************************/
51 unsigned OptSub1 (CodeSeg* S)
52 /* Search for the sequence
59 * and remove the handling of the high byte if X is not used later.
64 /* Walk over the entries */
66 while (I < CS_GetEntryCount (S)) {
71 CodeEntry* E = CS_GetEntry (S, I);
73 /* Check for the sequence */
74 if (E->OPC == OP65_SBC &&
75 CS_GetEntries (S, L, I+1, 3) &&
76 (L[0]->OPC == OP65_BCS || L[0]->OPC == OP65_JCS) &&
78 !CE_HasLabel (L[0]) &&
79 L[1]->OPC == OP65_DEX &&
80 !CE_HasLabel (L[1]) &&
81 L[0]->JumpTo->Owner == L[2] &&
84 /* Remove the bcs/dex */
85 CS_DelEntries (S, I+1, 2);
87 /* Remember, we had changes */
97 /* Return the number of changes made */
103 unsigned OptSub2 (CodeSeg* S)
104 /* Search for the sequence
121 unsigned Changes = 0;
123 /* Walk over the entries */
125 while (I < CS_GetEntryCount (S)) {
130 CodeEntry* E = CS_GetEntry (S, I);
132 /* Check for the sequence */
133 if (E->OPC == OP65_LDA &&
134 CS_GetEntries (S, L, I+1, 5) &&
135 L[0]->OPC == OP65_SEC &&
136 !CE_HasLabel (L[0]) &&
137 L[1]->OPC == OP65_STA &&
138 strcmp (L[1]->Arg, "tmp1") == 0 &&
139 !CE_HasLabel (L[1]) &&
140 L[2]->OPC == OP65_LDA &&
141 !CE_HasLabel (L[2]) &&
142 L[3]->OPC == OP65_SBC &&
143 strcmp (L[3]->Arg, "tmp1") == 0 &&
144 !CE_HasLabel (L[3]) &&
145 L[4]->OPC == OP65_STA &&
146 strcmp (L[4]->Arg, L[2]->Arg) == 0 &&
147 !CE_HasLabel (L[4])) {
149 /* Remove the store to tmp1 */
150 CS_DelEntry (S, I+2);
152 /* Remove the subtraction */
153 CS_DelEntry (S, I+3);
155 /* Move the lda to the position of the subtraction and change the
158 CS_MoveEntry (S, I, I+3);
159 CE_ReplaceOPC (E, OP65_SBC);
161 /* If the sequence head had a label, move this label back to the
164 if (CE_HasLabel (E)) {
165 CS_MoveLabels (S, E, L[0]);
168 /* Remember, we had changes */
178 /* Return the number of changes made */