* src/port.h: added TARGET_MCS51_LIKE
[fw/sdcc] / src / z80 / peeph-z80.def
1 // peeph-z80.def - Z80 peephole rules
2 //
3 //
4 // (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2007
5 //
6 // This program is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option) any
9 // later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 replace restart {
21         ld      %1,#%2
22         ld      a,0(%1)
23         ld      %3,a
24         ld      %1,#%5
25 } by {
26         ld      a,(#%2)
27         ; peephole z1 used #%2 directly instead of going through %1 using indirect addressing.
28         ld      %3,a
29         ld      %1,#%5
30 }
31
32 replace restart {
33         ld      %1,#%2
34         ld      0(%1),a
35 %4:
36         ld      %1,%5
37 } by {
38         ld      (#%2),a
39         ; peephole z2 directly used #%2 instead of going through %1 using indirect addressing.
40 %4:
41         ld      %1,%5
42 }
43
44 replace restart {
45         pop     af
46         ld      %1,#%2
47         ld      0(%1),%3
48         ld      %1,#%4
49 } by {
50         ld      a,%3
51         ld      (#%2),a
52         ; peephole z3 used #%2 directly instead of going through %1 using indirect addressing.
53         pop     af
54         ld      %1,#%4
55 } if operandsNotRelated(%3 'a')
56
57 replace restart {
58         ld      bc,#%1 + %2
59         ld      a,%3
60         ld      (bc),a
61         ld      bc,#%4 + %5
62 } by {
63         ld      a,%3
64         ld      (#%1 + %2),a
65         ; peephole z4 directly used address #%1 + %2 instead of placing it in bc first.
66         ld      bc,#%4 + %5
67 }
68
69 replace restart {
70         ld      bc,#%1 + %2 + %6
71         ld      a,%3
72         ld      (bc),a
73         ld      bc,#%4 + %5
74 } by {
75         ld      a,%3
76         ld      (#%1 + %2 + %6),a
77         ; peephole z5 directly used address #%1 + %2 + %6 instead of placing it in bc first.
78         ld      bc,#%4 + %5
79 }
80
81 replace restart {
82         ld      bc,#%1 + %2
83         ld      a,%3
84         ld      (bc),a
85 %7:
86         ld      bc,#%4 + %5
87 } by {
88         ld      a,%3
89         ld      (#%1 + %2),a
90         ; peephole z6 directly used address #%1 + %2 instead of placing it in bc first.
91 %7:
92         ld      bc,#%4 + %5
93 }
94
95 replace restart {
96         ld      bc,#%1 + %2 + %6
97         ld      a,%3
98         ld      (bc),a
99 %7:
100         ld      bc,#%4 + %5
101 } by {
102         ld      a,%3
103         ld      (#%1 + %2 + %6),a
104         ; peephole z7 directly used address #%1 + %2 + %6 instead of placing it in bc first.
105 %7:
106         ld      bc,#%4 + %5
107 }
108
109 replace restart {
110         ld      c,%1
111         ld      l,c
112         ret
113 } by {
114         ld      l,%1
115         ; peephole z8 moved %1 directly into l instead of going through c.
116         ret
117 }
118
119 replace restart {
120         ld      b,h
121         ld      c,l
122         pop     af
123         push    bc
124         call    %1
125 } by {
126         ex      (sp),hl
127         ; peephole z9 moved hl directly to the stack instead of going through bc.
128         call    %1
129 }
130
131 replace restart {
132         ld      d,h
133         ld      e,l
134         pop     af
135         push    de
136         call    %1
137 } by {
138         ex      (sp),hl
139         ; peephole z10 moved hl directly to the stack instead of going through de.
140         call    %1
141 }
142
143 replace restart {
144         jp      %5
145 } by {
146         ret
147         ; peephole z11 replaced jump by return.
148 } if labelIsReturnOnly(), labelRefCountChange(%5 -1)
149
150 replace restart {
151         jp      %1,%5
152 } by {
153         ret     %1
154         ; peephole z12 replaced jump by return.
155 } if labelIsReturnOnly(), labelRefCountChange(%5 -1)
156
157 // Should be one of the last ones. Opens the code to further peephole optimization.
158 replace restart {
159 %1:
160 } by {
161         ; peephole z13 removed unused label %1.
162 } if labelRefCount(%1 0)
163
164 // These should be the last rules, so that the peepholes above need to look at jp only.
165 replace {
166         jp      %5
167 } by {
168         jr      %5
169         ; peephole z14 changed absolute to relative conditional jump.
170 } if labelInRange()
171
172 replace {
173         jp      Z,%5
174 } by {
175         jr      Z,%5
176         ; peephole z15 changed absolute to relative conditional jump.
177 } if labelInRange()
178
179 replace {
180         jp      NZ,%5
181 } by {
182         jr      NZ,%5
183         ; peephole z16 changed absolute to relative conditional jump.
184 } if labelInRange()
185
186 replace {
187         jp      C,%5
188 } by {
189         jr      C,%5
190         ; peephole z17 changed absolute to relative conditional jump.
191 } if labelInRange()
192
193 replace {
194         jp      NC,%5
195 } by {
196         jr      NC,%5
197         ; peephole z18 changed absolute to relative conditional jump.
198 } if labelInRange()