summaryrefslogtreecommitdiff
path: root/src/pic14/peeph.def
blob: b82d3a0d21d01fd008e66e6bc52a985dfebd8bc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// PIC Port Peep rules
//
//
// INTRODUCTION:
//
// The peep hole optimizer searchs the
// the SDCC generated code for small snippets
// that can be optimized. As a user, you have
// control over this optimization process without
// having to learn the SDCC source code. (However
// you'll still need access to the source since
// these rules are compiled into the source.)
//
// The way it works is you specify the target
// snippet that you want replaced with a more 
// efficient snippet that you write. Wild card
// variables allow the rules to be parameterized.
// 
// In all of the SDCC ports, labels and operands
// can be wild cards. However, in the PIC even the
// instructions can be wild cards.
//
// EXAMPLE:
//
// Consider Peep Rule 1 as an example. This rule
// replaces some code like:
//
//   skpz           ;i.e. btfss status,Z
//    goto    lab1
//   clrw
//lab1:
//
// with:
//
//   skpnz     ;i.e. btfsc status,Z
//    clrw
//lab1
//
// However, the Rule has four wild cards.
// The first allows the btfss instruction operator
// be anything, not just the Z bit in status register.
// The second wild card applies to a label.
// The third wild card is for an instruction - any
// single instruction can be substituted.
// The fourth wild card is also an instruction. It's
// just an instruction place holder associated with
// a label (think of it as the PIC Port author's laziness
// imposed upon the user).
//
//
// CONDITIONS
//
// There are certain instances where a peep rule may not
// be applicable. Consider this subtle example:
//
//     movwf    R0
//     movf     R0,W
//
// It would seem that the second move is unnecessary. But
// be careful! The movf instruction affects the 'Z' bit.
// So if this sequence is followed by a btfsc status,Z, you
// will have to leave the second move in.
//
// To get around this proble, the peep rule can be followed
// by a conditon:  "if NZ". Which is to say, apply the rule
// if Z bit is not needed in the code that follows. The optimizer
// is smart enough to look more than one instruction past the
// target block...
//
// Special commands
//
//
//   _NOTBITSKIP_   %1   - Creates a wild card instruction that
//                         will match all instructions except for
//                         bit skip instructions (btfsc or btfss)
//   _BITSKIP_  %1 - Creates a wild card instruction that only
//                   will match a bit skip instruction (btfsc
//                   or btfss)
//   _INVERTBITSKIP_ %1  - For the wild card instruction %1, invert
//                         the state of the bit skip. If %1 is not
//                         a bit skip instruction, then there's an
//                         error in the peep rule.
//
// 
//


// Peep 1 
//   Converts 
//
//    btfss   reg1,bit
//     goto   label
//    incf    reg2,f
//label
//
// Into:
//
//    btfsc   reg1,bit
//     incf   reg2,f
//label
//
// Notice that wild cards will allow any instruction
// besides incf to be used in the above.
//
// Also, notice that this snippet is not valid if
// it follows another skip

replace restart {
        _NOTBITSKIP_	%1
	_BITSKIP_	%2
        goto	%3
	%4
%3:	%5
} by {
	;     peep 1 - test/jump to test/skip
        %1
	_INVERTBITSKIP_	%2
	%4
%3:	%5
} 

replace restart {
        _NOTBITSKIP_	%1
	_BITSKIP_	%2
        goto	%3
%4:	%5
%3:	%6
} by {
	;     peep 1b - test/jump to test/skip
        %1
	_INVERTBITSKIP_	%2
%4:	%5
%3:	%6
} 


//bogus test for pcode
//replace restart {
//	movf	%1,w	;comment at end
//%4:	movf	%1,w
//	RETURN
//	clrf	INDF
//	movlw	0xa5
//	movf	fsr,w
//	incf	indf,f
//	%2
//} by {
//	; peep test remove redundant move
//%4:	movf	%1,w	;another comment
//	%2
//} if AYBABTU %3


// peep 2
replace restart {
	movwf	%1
	movf	%1,w
} by {
	;     peep 2 - Removed redundant move
	movwf	%1
} if NZ

// peep 3
replace restart {
	decf	%1,f
	movf	%1,w
	btfss	STATUS,z
	goto	%2
} by {
	;     peep 3 - decf/mov/skpz to decfsz
	decfsz	%1,f
	 goto	%2
}


replace restart {
	movf	%1,w
	movf	%1,w
} by {
	;     peep 4 - Removed redundant move
	movf	%1,w
}


replace restart {
	movlw	%1
	movwf	%2
	movlw	%1
} by {
	;     peep 5 - Removed redundant move
	movlw	%1
	movwf	%2
}

replace restart {
	movwf	%1
	movwf	%1
} by {
	;     peep 6 - Removed redundant move
	movwf	%1
}

replace restart {
	movlw	0
	iorwf	%1,w
} by {
	;     peep 7 - Removed redundant move
	movf	%1,w
}

replace restart {
	movf	%1,w
	movwf	%2
	decf	%2,f
} by {
	;     peep 8 - Removed redundant move
	decf	%1,w
	movwf	%2
}

replace restart {
	movwf	%1
	movf	%2,w
	xorwf	%1,w
} by {
	;     peep 9a - Removed redundant move
	movwf	%1
	xorwf	%2,w
}

replace restart {
	movwf	%1
	movf	%2,w
	iorwf	%1,w
} by {
	;     peep 9b - Removed redundant move
	movwf	%1
	iorwf	%2,w
}

replace restart {
	movf	%1,w
	movwf	%2
	movf	%2,w
} by {
	;     peep 9c - Removed redundant move
	movf	%1,w
	movwf	%2
}

replace restart {
	movwf	%1
	movf	%1,w
	movwf	%2
} by {
	;     peep 9d - Removed redundant move
	movwf	%1
	movwf	%2
} if NZ

// From: Frieder Ferlemann

replace restart {
        iorlw   0
} by {
        ;     peep 10a - Removed unnecessary iorlw
} if NZ

// From: Frieder Ferlemann

replace restart {
        xorlw   0
} by {
        ;     peep 10b - Removed unnecessary xorlw
} if NZ

// From: Frieder Ferlemann

replace restart {
        movf    %1,w
        movwf   %1
} by {
        ;     peep 11 - Removed redundant move
        movf    %1,w
}

replace restart {
	clrf	%1
	rlf	%1,f
	movlw	0x01
	xorwf	%1,f
	movf	%1,w
	btfss	STATUS,2
	goto	%2

} by {
        ;     peep 13 - Optimized carry sequence
	clrf	%1
	incf	%1,F
	btfss	status,C
	goto	%2
	clrf	%1
	
}

replace restart {
	clrf	%1
	rlf	%1,f
	movlw	0x01
	xorwf	%1,f
	movf	%1,w
	btfsc	STATUS,2
	goto	%2

} by {
        ;     peep 13a - Optimized carry sequence
	clrf	%1
	incf	%1,F
	btfsc	status,C
	goto	%2
	clrf	%1
	
}