1//replace restart {
2//        pop  %1
3//        push %1
4//} by {
5//        ; Peephole 1     removed pop %1 push %1 (not push pop)
6//}
7
8//replace restart {
9//        pop  %1
10//        mov  %2,%3
11//        push %1
12//} by {
13//        ; Peephole 2     removed pop %1 push %1 (not push pop)
14//        mov  %2,%3
15//}
16
17//
18// added by Jean Louis VERN for
19// his shift stuff
20replace restart {
21        xch  a,%1
22        xch  a,%1
23} by {
24        ; Peephole 2.a   removed redundant xch xch
25}
26
27replace restart {
28// saving 2 byte
29        mov  %1,#0x00
30        mov  a,#0x00
31} by {
32        ; Peephole 3.a   changed mov to clr
33        clr  a
34        mov  %1,a
35}
36
37replace restart {
38// saving 1 byte
39        mov  %1,#0x00
40        clr  a
41} by {
42        ; Peephole 3.b   changed mov to clr
43        clr  a
44        mov  %1,a
45}
46
47replace restart {
48// saving 1 byte, loosing 1 cycle but maybe allowing peephole 3.b to start
49        mov  %1,#0x00
50        mov  %2,#0x00
51        mov  a,%3
52} by {
53        ; Peephole 3.c   changed mov to clr
54        clr  a
55        mov  %1,a
56        mov  %2,a
57        mov  a,%3
58}
59
60replace restart {
61	mov	a,#0
62} by {
63        ; Peephole 3.d   changed mov to clr
64	clr	a
65}
66
67replace {
68        mov  %1,a
69        mov  dptr,#%2
70        mov  a,%1
71        movx @dptr,a
72} by {
73        ; Peephole 100   removed redundant mov
74        mov  %1,a
75        mov  dptr,#%2
76        movx @dptr,a
77}
78
79replace {
80	mov  a,acc
81} by {
82	;  Peephole 100.a   removed redundant mov
83}
84
85replace {
86        mov  a,%1
87        movx @dptr,a
88        inc  dptr
89        mov  a,%1
90        movx @dptr,a
91} by {
92        ; Peephole 101   removed redundant mov
93        mov  a,%1
94        movx @dptr,a
95        inc  dptr
96        movx @dptr,a
97}
98
99replace {
100        mov  %1,%2
101        ljmp %3
102%4:
103        mov  %1,%5
104%3:
105        mov  dpl,%1
106%7:
107        mov  sp,bp
108        pop  bp
109} by {
110        ; Peephole 102   removed redundant mov
111        mov  dpl,%2
112        ljmp %3
113%4:
114        mov  dpl,%5
115%3:
116%7:
117        mov  sp,bp
118        pop  bp
119}
120
121replace {
122        mov  %1,%2
123        ljmp %3
124%4:
125        mov  a%1,%5
126%3:
127        mov  dpl,%1
128%7:
129        mov  sp,bp
130        pop  bp
131} by {
132        ; Peephole 103   removed redundant mov
133        mov  dpl,%2
134        ljmp %3
135%4:
136        mov  dpl,%5
137%3:
138%7:
139        mov  sp,bp
140        pop  bp
141}
142
143replace {
144        mov  a,bp
145        clr  c
146        add  a,#0x01
147        mov  r%1,a
148} by {
149        ; Peephole 104   optimized increment (acc not set to r%1, flags undefined)
150        mov  r%1,bp
151        inc  r%1
152}
153
154replace {
155        mov  %1,a
156        mov  a,%1
157} by {
158        ; Peephole 105   removed redundant mov
159        mov  %1,a
160}
161
162replace {
163        mov  %1,a
164        clr  c
165        mov  a,%1
166} by {
167        ; Peephole 106   removed redundant mov
168        mov  %1,a
169        clr  c
170}
171
172replace {
173        ljmp %1
174%1:
175} by {
176        ; Peephole 107   removed redundant ljmp
177%1:
178}
179
180replace {
181        jc   %1
182        ljmp %5
183%1:
184} by {
185        ; Peephole 108   removed ljmp by inverse jump logic
186        jnc  %5
187%1:
188} if labelInRange(%5)
189
190replace {
191        jz   %1
192        ljmp %5
193%1:
194} by {
195        ; Peephole 109   removed ljmp by inverse jump logic
196        jnz  %5
197%1:
198} if labelInRange(%5)
199
200replace {
201        jnz  %1
202        ljmp %5
203%1:
204} by {
205        ; Peephole 110   removed ljmp by inverse jump logic
206        jz  %5
207%1:
208} if labelInRange(%5)
209
210replace {
211        jb   %1,%2
212        ljmp %5
213%2:
214} by {
215        ; Peephole 111   removed ljmp by inverse jump logic
216        jnb  %1,%5
217%2:
218} if labelInRange(%5)
219
220replace {
221        jnb  %1,%2
222        ljmp %5
223%2:
224} by {
225       ; Peephole 112   removed ljmp by inverse jump logic
226        jb   %1,%5
227%2:
228} if labelInRange(%5)
229
230replace {
231        ljmp %5
232%1:
233} by {
234        ; Peephole 132   changed ljmp to sjmp
235        sjmp %5
236%1:
237} if labelInRange(%5)
238
239
240replace {
241        clr  a
242        cjne %1,%2,%3
243        cpl  a
244%3:
245        rrc  a
246        mov  %4,c
247} by {
248        ; Peephole 113   optimized misc sequence
249        clr  %4
250        cjne %1,%2,%3
251        setb %4
252%3:
253} if labelRefCount %3 1
254
255replace {
256        clr  a
257        cjne %1,%2,%3
258        cjne %10,%11,%3
259        cpl  a
260%3:
261        rrc  a
262        mov  %4,c
263} by {
264        ; Peephole 114   optimized misc sequence
265        clr  %4
266        cjne %1,%2,%3
267        cjne %10,%11,%3
268        setb %4
269%3:
270} if labelRefCount %3 2
271
272replace {
273        clr  a
274        cjne %1,%2,%3
275        cpl  a
276%3:
277        jnz  %4
278} by {
279        ; Peephole 115   jump optimization
280        cjne %1,%2,%3
281        sjmp %4
282%3:
283} if labelRefCount %3 1
284
285replace {
286        clr  a
287        cjne %1,%2,%3
288        cjne %9,%10,%3
289        cpl  a
290%3:
291        jnz  %4
292} by {
293        ; Peephole 116   jump optimization
294        cjne %1,%2,%3
295        cjne %9,%10,%3
296        sjmp %4
297%3:
298} if labelRefCount %3 2
299
300replace {
301        clr  a
302        cjne %1,%2,%3
303        cjne %9,%10,%3
304        cjne %11,%12,%3
305        cpl  a
306%3:
307        jnz %4
308} by {
309        ; Peephole 117   jump optimization
310        cjne %1,%2,%3
311        cjne %9,%10,%3
312        cjne %11,%12,%3
313        sjmp %4
314%3:
315} if labelRefCount %3 3
316
317replace {
318        clr  a
319        cjne %1,%2,%3
320        cjne %9,%10,%3
321        cjne %11,%12,%3
322        cjne %13,%14,%3
323        cpl  a
324%3:
325        jnz %4
326} by {
327        ; Peephole 118   jump optimization
328        cjne %1,%2,%3
329        cjne %9,%10,%3
330        cjne %11,%12,%3
331        cjne %13,%14,%3
332        sjmp %4
333%3:
334} if labelRefCount %3 4
335
336replace {
337        mov  a,#0x01
338        cjne %1,%2,%3
339        clr  a
340%3:
341        jnz  %4
342} by {
343        ; Peephole 119   jump optimization
344        cjne %1,%2,%4
345%3:
346} if labelRefCount %3 1
347
348replace {
349        mov  a,#0x01
350        cjne %1,%2,%3
351        cjne %10,%11,%3
352        clr  a
353%3:
354        jnz  %4
355} by {
356        ; Peephole 120   jump optimization
357        cjne %1,%2,%4
358        cjne %10,%11,%4
359%3:
360} if labelRefCount %3 2
361
362replace {
363        mov  a,#0x01
364        cjne %1,%2,%3
365        cjne %10,%11,%3
366        cjne %12,%13,%3
367        clr  a
368%3:
369        jnz  %4
370} by {
371        ; Peephole 121   jump optimization
372        cjne %1,%2,%4
373        cjne %10,%11,%4
374        cjne %12,%13,%4
375%3:
376} if labelRefCount %3 3
377
378replace {
379        mov  a,#0x01
380        cjne %1,%2,%3
381        cjne %10,%11,%3
382        cjne %12,%13,%3
383        cjne %14,%15,%3
384        clr  a
385%3:
386        jnz  %4
387} by {
388        ; Peephole 122   jump optimization
389        cjne %1,%2,%4
390        cjne %10,%11,%4
391        cjne %12,%13,%4
392        cjne %14,%15,%4
393%3:
394} if labelRefCount %3 4
395
396replace {
397        mov  a,#0x01
398        cjne %1,%2,%3
399        clr  a
400%3:
401        jz   %4
402} by {
403        ; Peephole 123   jump optimization
404        cjne %1,%2,%3
405        smp  %4
406%3:
407} if labelRefCount %3 1
408
409replace {
410        mov  a,#0x01
411        cjne %1,%2,%3
412        cjne %10,%11,%3
413        clr  a
414%3:
415        jz   %4
416} by {
417        ; Peephole 124   jump optimization
418        cjne %1,%2,%3
419        cjne %10,%11,%3
420        sjmp  %4
421%3:
422} if labelRefCount %3 2
423
424replace {
425        mov  a,#0x01
426        cjne %1,%2,%3
427        cjne %10,%11,%3
428        cjne %12,%13,%3
429        clr  a
430%3:
431        jz   %4
432} by {
433        ; Peephole 125   jump optimization
434        cjne %1,%2,%3
435        cjne %10,%11,%3
436        cjne %12,%13,%3
437        sjmp %4
438%3:
439} if labelRefCount %3 3
440
441replace {
442        mov  a,#0x01
443        cjne %1,%2,%3
444        cjne %10,%11,%3
445        cjne %12,%13,%3
446        cjne %14,%15,%3
447        clr  a
448%3:
449        jz   %4
450} by {
451        ; Peephole 126   jump optimization
452        cjne %1,%2,%3
453        cjne %10,%11,%3
454        cjne %12,%13,%3
455        cjne %14,%15,%3
456        sjmp %4
457%3:
458} if labelRefCount %3 4
459
460replace {
461        push psw
462        mov  psw,%1
463        push bp
464        mov  bp,%2
465%3:
466        mov  %2,bp
467        pop  bp
468        pop  psw
469        ret
470} by {
471        ; Peephole 127   removed misc sequence
472        ret
473} if labelRefCount %3 0
474
475replace {
476        clr  a
477        rlc  a
478        jz   %1
479} by {
480        ; Peephole 128   jump optimization
481        jnc  %1
482}
483
484replace {
485        clr  a
486        rlc  a
487        jnz  %1
488} by {
489        ; Peephole 129   jump optimization
490        jc   %1
491}
492
493replace {
494        mov  r%1,@r%2
495} by {
496        ; Peephole 130   changed target address mode r%1 to ar%1
497        mov  ar%1,@r%2
498}
499
500replace {
501        mov  a,%1
502        subb a,#0x01
503        mov  %2,a
504        mov  %1,%2
505} by {
506        ; Peephole 131   optimized decrement (not caring for c)
507        dec  %1
508        mov  %2,%1
509}
510
511replace {
512        mov  r%1,%2
513        mov  ar%3,@r%1
514        inc  r%3
515        mov  r%4,%2
516        mov  @r%4,ar%3
517} by {
518        ; Peephole 133   removed redundant moves
519        mov  r%1,%2
520        inc  @r%1
521        mov  ar%3,@r%1
522}
523
524replace {
525        mov  r%1,%2
526        mov  ar%3,@r%1
527        dec  r%3
528        mov  r%4,%2
529        mov  @r%4,ar%3
530} by {
531        ; Peephole 134   removed redundant moves
532        mov  r%1,%2
533        dec  @r%1
534        mov  ar%3,@r%1
535}
536
537replace {
538        mov  r%1,a
539        mov  a,r%2
540        orl  a,r%1
541} by {
542        ; Peephole 135   removed redundant mov
543        mov  r%1,a
544        orl  a,r%2
545}
546
547replace {
548        mov  %1,a
549        mov  dpl,%2
550        mov  dph,%3
551	mov  dpx,%4
552        mov  a,%1
553} by {
554        ; Peephole 136a   removed redundant moves
555        mov  %1,a
556        mov  dpl,%2
557        mov  dph,%3
558	mov  dpx,%4
559} if 24bitMode
560
561replace {
562        mov  %1,a
563        mov  dpl,%2
564        mov  dph,%3
565        mov  a,%1
566} by {
567        ; Peephole 136   removed redundant moves
568        mov  %1,a
569        mov  dpl,%2
570        mov  dph,%3
571}
572
573// WTF? Doesn't look sensible to me...
574//replace {
575//        mov  b,#0x00
576//        mov  a,%1
577//        cjne %2,%3,%4
578//        mov  b,#0x01
579//%4:
580//        mov  a,b
581//        jz   %5
582//} by {
583//        ; Peephole 137   optimized misc jump sequence
584//        mov  a,%1
585//        cjne %2,%3,%5
586//%4:
587//} if labelRefCount %4 1
588//
589//replace {
590//        mov  b,#0x00
591//        mov  a,%1
592//        cjne %2,%3,%4
593//        mov  b,#0x01
594//%4:
595//        mov  a,b
596//        jnz  %5
597//} by {
598//        ; Peephole 138   optimized misc jump sequence
599//        mov  a,%1
600//        cjne %2,%3,%4
601//        sjmp %5
602//%4:
603//} if labelRefCount %4 1
604
605replace {
606        mov  r%1,a
607        anl  ar%1,%2
608        mov  a,r%1
609} by {
610        ; Peephole 139   removed redundant mov
611        anl  a,%2
612        mov  r%1,a
613}
614
615replace {
616        mov  r%1,a
617        orl  ar%1,%2
618        mov  a,r%1
619} by {
620        ; Peephole 140   removed redundant mov
621        orl  a,%2
622        mov  r%1,a }
623
624replace {
625        mov  r%1,a
626        xrl  ar%1,%2
627        mov  a,r%1
628} by {
629        ; Peephole 141   removed redundant mov
630        xrl  a,%2
631        mov  r%1,a
632}
633
634replace {
635        mov  r%1,a
636        mov  r%2,ar%1
637        mov  ar%1,@r%2
638} by {
639        ; Peephole 142   removed redundant moves
640        mov  r%2,a
641        mov  ar%1,@r%2
642}
643
644replace {
645        rlc  a
646        mov  acc.0,c
647} by {
648        ; Peephole 143   converted rlc to rl
649        rl   a
650}
651
652replace {
653        rrc  a
654        mov  acc.7,c
655} by {
656        ; Peephole 144   converted rrc to rc
657        rr   a
658}
659
660replace {
661        clr  c
662        addc a,%1
663} by {
664        ; Peephole 145   changed to add without carry
665        add  a,%1
666}
667
668replace {
669        clr  c
670        mov  a,%1
671        addc a,%2
672} by {
673        ; Peephole 146   changed to add without carry
674        mov  a,%1
675        add  a,%2
676}
677
678replace {
679        orl  r%1,a
680} by {
681        ; Peephole 147   changed target address mode r%1 to ar%1
682        orl  ar%1,a
683}
684
685replace {
686        anl  r%1,a
687} by {
688        ; Peephole 148   changed target address mode r%1 to ar%1
689        anl  ar%1,a
690}
691
692replace {
693        xrl  r%1,a
694} by {
695        ; Peephole 149   changed target address mode r%1 to ar%1
696        xrl  ar%1,a
697}
698
699replace {
700        mov  %1,dpl
701        mov  dpl,%1
702%9:
703        ret
704} by {
705        ; Peephole 150   removed misc moves via dpl before return
706%9:
707        ret
708}
709
710replace {
711        mov  %1,dpl
712        mov  %2,dph
713        mov  dpl,%1
714        mov  dph,%2
715%9:
716        ret
717} by {
718        ; Peephole 151   removed misc moves via dph, dpl before return
719%9:
720        ret
721}
722
723replace {
724        mov  %1,dpl
725        mov  %2,dph
726        mov  dpl,%1
727%9:
728        ret
729} by {
730        ; Peephole 152   removed misc moves via dph, dpl before return
731%9:
732        ret
733}
734
735replace {
736        mov  %1,dpl
737        mov  %2,dph
738        mov  %3,b
739        mov  dpl,%1
740        mov  dph,%2
741        mov  b,%3
742%9:
743        ret
744} by {
745        ; Peephole 153   removed misc moves via dph, dpl, b before return
746%9:
747        ret
748}
749
750replace {
751        mov  %1,dpl
752        mov  %2,dph
753        mov  %3,b
754        mov  dpl,%1
755%9:
756        ret
757} by {
758        ; Peephole 154   removed misc moves via dph, dpl, b before return
759%9:
760        ret
761}
762
763replace {
764        mov  %1,dpl
765        mov  %2,dph
766        mov  %3,b
767        mov  dpl,%1
768        mov  dph,%2
769%9:
770        ret
771} by {
772        ; Peephole 155   removed misc moves via dph, dpl, b before return
773%9:
774        ret
775}
776
777replace {
778        mov  %1,dpl
779        mov  %2,dph
780        mov  %3,b
781        mov  %4,a
782        mov  dpl,%1
783        mov  dph,%2
784        mov  b,%3
785        mov  a,%4
786%9:
787        ret
788} by {
789        ; Peephole 156   removed misc moves via dph, dpl, b, a before return
790%9:
791        ret
792}
793
794replace {
795        mov  %1,dpl
796        mov  %2,dph
797        mov  %3,b
798        mov  %4,a
799        mov  dpl,%1
800        mov  dph,%2
801%9:
802        ret
803} by {
804        ; Peephole 157   removed misc moves via dph, dpl, b, a before return
805%9:
806        ret
807}
808
809replace {
810        mov  %1,dpl
811        mov  %2,dph
812        mov  %3,b
813        mov  %4,a
814        mov  dpl,%1
815%9:
816        ret } by {
817        ; Peephole 158   removed misc moves via dph, dpl, b, a before return
818%9:
819        ret }
820
821replace {
822        mov  %1,#%2
823        xrl  %1,#0x80
824} by {
825        ; Peephole 159   avoided xrl during execution
826        mov  %1,#(%2 ^ 0x80)
827}
828
829replace {
830        jnc  %1
831        sjmp %2
832%1:
833} by {
834        ; Peephole 160   removed sjmp by inverse jump logic
835        jc   %2
836%1:}
837
838replace {
839        jc   %1
840        sjmp %2
841%1:
842} by {
843        ; Peephole 161   removed sjmp by inverse jump logic
844        jnc  %2
845%1:}
846
847replace {
848        jnz  %1
849        sjmp %2
850%1:
851} by {
852        ; Peephole 162   removed sjmp by inverse jump logic
853        jz   %2
854%1:}
855
856replace {
857        jz   %1
858        sjmp %2
859%1:
860} by {
861        ; Peephole 163   removed sjmp by inverse jump logic
862        jnz  %2
863%1:}
864
865replace {
866        jnb  %3,%1
867        sjmp %2
868%1:
869} by {
870        ; Peephole 164   removed sjmp by inverse jump logic
871        jb   %3,%2
872%1:
873}
874
875replace {
876        jb   %3,%1
877        sjmp %2
878%1:
879} by {
880        ; Peephole 165   removed sjmp by inverse jump logic
881        jnb  %3,%2
882%1:
883}
884
885replace {
886        mov  %1,%2
887        mov  %3,%1
888        mov  %2,%1
889} by {
890        ; Peephole 166   removed redundant mov
891        mov  %1,%2
892        mov  %3,%1 }
893
894replace {
895        mov  c,%1
896        cpl  c
897        mov  %1,c
898} by {
899        ; Peephole 167   removed redundant bit moves (c not set to %1)
900        cpl  %1 }
901
902replace {
903        jnb  %1,%2
904        sjmp %3
905%2:} by {
906        ; Peephole 168   jump optimization
907        jb   %1,%3
908%2:}
909
910replace {
911        jb   %1,%2
912        sjmp %3
913%2:} by {
914        ; Peephole 169   jump optimization
915        jnb  %1,%3
916%2:}
917
918replace {
919        clr  a
920        cjne %1,%2,%3
921        cpl  a
922%3:
923        jz   %4
924} by {
925        ; Peephole 170   jump optimization
926        cjne %1,%2,%4
927%3:
928} if labelRefCount %3 1
929
930replace {
931        clr  a
932        cjne %1,%2,%3
933        cjne %9,%10,%3
934        cpl  a
935%3:
936        jz   %4
937} by {
938        ; Peephole 171   jump optimization
939        cjne %1,%2,%4
940        cjne %9,%10,%4
941%3:
942} if labelRefCount %3 2
943
944replace {
945        clr  a
946        cjne %1,%2,%3
947        cjne %9,%10,%3
948        cjne %11,%12,%3
949        cpl  a
950%3:
951        jz   %4
952} by {
953        ; Peephole 172   jump optimization
954        cjne %1,%2,%4
955        cjne %9,%10,%4
956        cjne %11,%12,%4
957%3:
958} if labelRefCount %3 3
959
960replace {
961        clr  a
962        cjne %1,%2,%3
963        cjne %9,%10,%3
964        cjne %11,%12,%3
965        cjne %13,%14,%3
966        cpl  a
967%3:
968        jz   %4
969} by {
970        ; Peephole 173   jump optimization
971        cjne %1,%2,%4
972        cjne %9,%10,%4
973        cjne %11,%12,%4
974        cjne %13,%14,%4
975%3:
976} if labelRefCount %3 4
977
978replace {
979        mov  r%1,%2
980        clr  c
981        mov  a,r%1
982        subb a,#0x01
983        mov  %2,a
984} by {
985        ; Peephole 174   optimized decrement (acc not set to %2, flags undefined)
986        mov  r%1,%2
987        dec  %2
988}
989
990
991replace {
992        mov  r%1,%2
993        mov  a,r%1
994        add  a,#0x01
995        mov  %2,a
996} by {
997        ; Peephole 175   optimized increment (acc not set to %2, flags undefined)
998        mov  r%1,%2
999        inc  %2
1000}
1001
1002replace {
1003        mov  %1,@r%2
1004        inc  %1
1005        mov  @r%2,%1
1006} by {
1007        ; Peephole 176   optimized increment, removed redundant mov
1008        inc  @r%2
1009        mov  %1,@r%2
1010}
1011
1012// this one will screw assignes to volatile/sfr's
1013//replace {
1014//        mov  %1,%2
1015//        mov  %2,%1
1016//} by {
1017//        ; Peephole 177   removed redundant mov
1018//        mov  %1,%2
1019//}
1020
1021replace {
1022        mov  a,%1
1023        mov  b,a
1024        mov  a,%2
1025} by {
1026        ; Peephole 178   removed redundant mov
1027        mov  b,%1
1028        mov  a,%2
1029}
1030
1031// rules 179-182 provided by : Frieder <fe@lake.iup.uni-heidelberg.de>
1032// saving 2 byte, 1 cycle
1033replace {
1034        mov  b,#0x00
1035        mov  a,#0x00
1036} by {
1037        ; Peephole 179   changed mov to clr
1038        clr  a
1039        mov  b,a
1040}
1041
1042// saving 1 byte, 0 cycles
1043replace {
1044        mov  a,#0x00
1045} by {
1046        ; Peephole 180   changed mov to clr
1047        clr  a
1048}
1049
1050replace {
1051        mov  dpl,#0x00
1052        mov  dph,#0x00
1053 	mov  dpx,#0x00
1054} by {
1055        ; Peephole 181a   used 24 bit load of dptr
1056        mov  dptr,#0x0000
1057} if 24bitMode
1058
1059// saving 3 byte, 2 cycles, return(NULL) profits here
1060replace {
1061        mov  dpl,#0x00
1062        mov  dph,#0x00
1063} by {
1064        ; Peephole 181   used 16 bit load of dptr
1065        mov  dptr,#0x0000
1066}
1067
1068replace {
1069	mov dpl,#%1
1070	mov dph,#(%1 >> 8)
1071	mov dpx,#(%1 >> 16)
1072} by {
1073	; Peephole 182b used 24 bit load of DPTR
1074	mov dptr,#%1
1075}
1076
1077// saves 2 bytes, ?? cycles.
1078replace {
1079        mov  dpl,#0x%1
1080        mov  dph,#0x%2
1081	mov  dpx,#0x%3
1082} by {
1083        ; Peephole 182a   used 24 bit load of dptr
1084        mov	dptr,#0x%3%2%1
1085} if 24bitMode(), portIsDS390
1086
1087// saves 2 bytes, ?? cycles.
1088replace {
1089        mov  dpl,#%1
1090        mov  dph,#%2
1091	mov  dpx,#%3
1092} by {
1093        ; Peephole 182b   used 24 bit load of dptr
1094        mov  dptr,#((%3 << 16) + (%2 << 8) + %1)
1095} if 24bitMode(), portIsDS390
1096
1097// saving 3 byte, 2 cycles, return(float_constant) profits here
1098replace {
1099        mov  dpl,#0x%1
1100        mov  dph,#0x%2
1101} by {
1102        ; Peephole 182c   used 16 bit load of dptr
1103        mov  dptr,#0x%2%1
1104}
1105
1106// saving 3 byte, 2 cycles, return(float_constant) profits here
1107replace {
1108        mov  dpl,#%1
1109        mov  dph,#%2
1110} by {
1111        ; Peephole 182   used 16 bit load of dptr
1112        mov  dptr,#(((%2)<<8) + %1)
1113}
1114
1115replace {
1116        anl  %1,#%2
1117        anl  %1,#%3
1118} by {
1119        ; Peephole 183   avoided anl during execution
1120        anl  %1,#(%2 & %3)
1121}
1122
1123replace {
1124        mov  %1,a
1125        cpl  a
1126        mov  %1,a
1127} by {
1128        ; Peephole 184   removed redundant mov
1129        cpl  a
1130        mov  %1,a
1131}
1132
1133replace {
1134// acc being incremented might cause problems
1135        mov  %1,a
1136        inc  %1
1137} by {
1138        ; Peephole 185   changed order of increment (acc incremented also!)
1139        inc  a
1140        mov  %1,a
1141}
1142
1143// char indexed access to: long code table[] = {4,3,2,1};
1144replace restart {
1145	add  a,#%1
1146	mov  dpl,a
1147	clr  a
1148	addc a,#(%1 >> 8)
1149	mov  dph,a
1150	clr  a
1151	movc a,@a+dptr
1152	mov  %2,a
1153	inc  dptr
1154	clr  a
1155	movc a,@a+dptr
1156	mov  %3,a
1157	inc  dptr
1158	clr  a
1159	movc a,@a+dptr
1160	mov  %4,a
1161	inc  dptr
1162	clr  a
1163	movc a,@a+dptr
1164} by {
1165	; Peephole 186.a   optimized movc sequence
1166	mov  b,a
1167	mov  dptr,#%1
1168	movc a,@a+dptr
1169	mov  %2,a
1170	inc  dptr
1171	mov  a,b
1172	movc a,@a+dptr
1173	mov  %3,a
1174	inc  dptr
1175	mov  a,b
1176	movc a,@a+dptr
1177	mov  %4,a
1178	inc  dptr
1179	mov  a,b
1180	movc a,@a+dptr
1181}
1182
1183// char indexed access to: void* code table[] = {4,3,2,1};
1184replace restart {
1185	add  a,#%1
1186	mov  dpl,a
1187	clr  a
1188	addc a,#(%1 >> 8)
1189	mov  dph,a
1190	clr  a
1191	movc a,@a+dptr
1192	mov  %2,a
1193	inc  dptr
1194	clr  a
1195	movc a,@a+dptr
1196	mov  %3,a
1197	inc  dptr
1198	clr  a
1199	movc a,@a+dptr
1200} by {
1201	; Peephole 186.b   optimized movc sequence
1202	mov  b,a
1203	mov  dptr,#%1
1204	movc a,@a+dptr
1205	mov  %2,a
1206	inc  dptr
1207	mov  a,b
1208	movc a,@a+dptr
1209	mov  %3,a
1210	inc  dptr
1211	mov  a,b
1212	movc a,@a+dptr
1213}
1214
1215// char indexed access to: int code table[] = {4,3,2,1};
1216replace restart {
1217	add  a,#%1
1218	mov  dpl,a
1219	clr  a
1220	addc a,#(%1 >> 8)
1221	mov  dph,a
1222	clr  a
1223	movc a,@a+dptr
1224	mov  %2,a
1225	inc  dptr
1226	clr  a
1227	movc a,@a+dptr
1228} by {
1229	; Peephole 186.c   optimized movc sequence
1230	mov  %2,a
1231	mov  dptr,#%1
1232	movc a,@a+dptr
1233	xch  a,%2
1234	inc  dptr
1235	movc a,@a+dptr
1236}
1237
1238// char indexed access to: char code table[] = {4,3,2,1};
1239replace {
1240	add  a,#%1
1241	mov  dpl,a
1242	clr  a
1243	addc a,#(%1 >> 8)
1244	mov  dph,a
1245	clr  a
1246	movc a,@a+dptr
1247} by {
1248	; Peephole 186.d  optimized movc sequence
1249	mov  dptr,#%1
1250	movc a,@a+dptr
1251}
1252
1253replace {
1254        mov  r%1,%2
1255        anl  ar%1,#%3
1256        mov  a,r%1
1257} by {
1258        ; Peephole 187   used a instead of ar%1 for anl
1259        mov  a,%2
1260        anl  a,#%3
1261        mov  r%1,a
1262}
1263
1264replace {
1265        mov  %1,a
1266        mov  dptr,%2
1267        movc a,@a+dptr
1268        mov  %1,a
1269} by {
1270        ; Peephole 188   removed redundant mov
1271        mov  dptr,%2
1272        movc a,@a+dptr
1273        mov  %1,a
1274}
1275
1276replace {
1277        anl  a,#0x0f
1278        mov  %1,a
1279        mov  a,#0x0f
1280        anl  a,%1
1281} by {
1282        ; Peephole 189   removed redundant mov and anl
1283        anl  a,#0x0f
1284        mov  %1,a
1285}
1286
1287// rules 190 & 191 need to be in order
1288replace {
1289        mov  a,%1
1290        lcall __gptrput
1291        mov  a,%1
1292} by {
1293        ; Peephole 190   removed redundant mov
1294        mov  a,%1
1295        lcall __gptrput
1296}
1297
1298replace {
1299        mov  %1,a
1300        mov  dpl,%2
1301        mov  dph,%3
1302        mov  b,%4
1303        mov  a,%1
1304} by {
1305        ; Peephole 191   removed redundant mov
1306        mov  %1,a
1307        mov  dpl,%2
1308        mov  dph,%3
1309        mov  b,%4
1310}
1311
1312replace {
1313        mov  r%1,a
1314        mov  @r%2,ar%1
1315} by {
1316        ; Peephole 192   used a instead of ar%1 as source
1317        mov  r%1,a
1318        mov  @r%2,a
1319}
1320
1321replace {
1322        jnz  %3
1323        mov  a,%4
1324        jnz  %3
1325        mov  a,%9
1326        jnz  %3
1327        mov  a,%12
1328        cjne %13,%14,%3
1329        sjmp %7
1330%3:
1331        sjmp %8
1332} by {
1333        ; Peephole 193.a optimized misc jump sequence
1334        jnz  %8
1335        mov  a,%4
1336        jnz  %8
1337        mov  a,%9
1338        jnz  %8
1339        mov  a,%12
1340        cjne %13,%14,%8
1341        sjmp %7
1342;%3:
1343} if labelRefCount %3 4
1344
1345replace {
1346        cjne %1,%2,%3
1347        mov  a,%4
1348        cjne %5,%6,%3
1349        mov  a,%9
1350        cjne %10,%11,%3
1351        mov  a,%12
1352        cjne %13,%14,%3
1353        sjmp %7
1354%3:
1355        sjmp %8
1356} by {
1357        ; Peephole 193   optimized misc jump sequence
1358        cjne %1,%2,%8
1359        mov  a,%4
1360        cjne %5,%6,%8
1361        mov  a,%9
1362        cjne %10,%11,%8
1363        mov  a,%12
1364        cjne %13,%14,%8
1365        sjmp %7
1366;%3:
1367} if labelRefCount %3 4
1368
1369replace {
1370        cjne @%1,%2,%3
1371        inc  %1
1372        cjne @%1,%6,%3
1373        inc  %1
1374        cjne @%1,%11,%3
1375       	inc  %1
1376        cjne @%1,%14,%3
1377        sjmp %7
1378%3:
1379        sjmp %8
1380} by {
1381        ; Peephole 193.a   optimized misc jump sequence
1382        cjne @%1,%2,%8
1383        inc  %1
1384        cjne @%1,%6,%8
1385        inc  %1
1386        cjne @%1,%11,%8
1387        inc  %1
1388        cjne @%1,%14,%8
1389        sjmp %7
1390;%3:
1391} if labelRefCount %3 4
1392
1393replace {
1394        cjne %1,%2,%3
1395        cjne %5,%6,%3
1396        cjne %10,%11,%3
1397        cjne %13,%14,%3
1398        sjmp %7
1399%3:
1400        sjmp %8
1401} by {
1402        ; Peephole 194   optimized misc jump sequence
1403        cjne %1,%2,%8
1404        cjne %5,%6,%8
1405        cjne %10,%11,%8
1406        cjne %13,%14,%8
1407        sjmp %7
1408;%3:
1409} if labelRefCount %3 4
1410
1411replace {
1412        jnz  %3
1413        mov  a,%4
1414        jnz  %3
1415        mov  a,%9
1416        cjne %10,%11,%3
1417        sjmp %7
1418%3:
1419        sjmp %8
1420} by {
1421        ; Peephole 195.a optimized misc jump sequence
1422        jnz  %8
1423        mov  a,%4
1424        jnz  %8
1425        mov  a,%9
1426        cjne %10,%11,%8
1427        sjmp %7
1428;%3:
1429} if labelRefCount %3 3
1430
1431replace {
1432        cjne %1,%2,%3
1433        mov  a,%4
1434        cjne %5,%6,%3
1435        mov  a,%9
1436        cjne %10,%11,%3
1437        sjmp %7
1438%3:
1439        sjmp %8
1440} by {
1441        ; Peephole 195   optimized misc jump sequence
1442        cjne %1,%2,%8
1443        mov  a,%4
1444        cjne %5,%6,%8
1445        mov  a,%9
1446        cjne %10,%11,%8
1447        sjmp %7
1448;%3:
1449} if labelRefCount %3 3
1450
1451replace {
1452        cjne @%1,%2,%3
1453        inc  %1
1454        cjne @%1,%6,%3
1455        inc  %1
1456        cjne @%1,%11,%3
1457        sjmp %7
1458%3:
1459        sjmp %8
1460} by {
1461        ; Peephole 195.a   optimized misc jump sequence
1462        cjne @%1,%2,%8
1463        inc  %1
1464        cjne @%1,%6,%8
1465        inc  %1
1466        cjne @%1,%11,%8
1467        sjmp %7
1468;%3:
1469} if labelRefCount %3 3
1470
1471replace {
1472        cjne %1,%2,%3
1473        cjne %5,%6,%3
1474        cjne %10,%11,%3
1475        sjmp %7
1476%3:
1477        sjmp %8
1478} by {
1479        ; Peephole 196   optimized misc jump sequence
1480        cjne %1,%2,%8
1481        cjne %5,%6,%8
1482        cjne %10,%11,%8
1483        sjmp %7
1484;%3:
1485} if labelRefCount %3 3
1486
1487replace {
1488        jnz  %3
1489        mov  a,%4
1490        cjne %5,%6,%3
1491        sjmp %7
1492%3:
1493        sjmp %8
1494} by {
1495        ; Peephole 197.a optimized misc jump sequence
1496        jnz  %8
1497        mov  a,%4
1498        cjne %5,%6,%8
1499        sjmp %7
1500;%3:
1501} if labelRefCount %3 2
1502
1503replace {
1504        cjne %1,%2,%3
1505        mov  a,%4
1506        cjne %5,%6,%3
1507        sjmp %7
1508%3:
1509        sjmp %8
1510} by {
1511        ; Peephole 197   optimized misc jump sequence
1512        cjne %1,%2,%8
1513        mov  a,%4
1514        cjne %5,%6,%8
1515        sjmp %7
1516;%3:
1517} if labelRefCount %3 2
1518
1519replace {
1520        cjne @%1,%2,%3
1521        inc  %1
1522        cjne @%1,%6,%3
1523        sjmp %7
1524%3:
1525        sjmp %8
1526} by {
1527        ; Peephole 197.a   optimized misc jump sequence
1528        cjne @%1,%2,%8
1529        inc   %1
1530        cjne @%1,%6,%8
1531        sjmp %7
1532;%3:
1533} if labelRefCount %3 2
1534
1535replace {
1536        cjne %1,%2,%3
1537        cjne %5,%6,%3
1538        sjmp %7
1539%3:
1540        sjmp %8
1541} by {
1542        ; Peephole 198   optimized misc jump sequence
1543        cjne %1,%2,%8
1544        cjne %5,%6,%8
1545        sjmp %7
1546;%3:
1547} if labelRefCount %3 2
1548
1549replace {
1550        cjne %1,%2,%3
1551        sjmp %4
1552%3:
1553        sjmp %5
1554} by {
1555        ; Peephole 199   optimized misc jump sequence
1556        cjne %1,%2,%5
1557        sjmp %4
1558;%3:
1559} if labelRefCount %3 1
1560
1561replace {
1562        sjmp %1
1563%1:
1564} by {
1565        ; Peephole 200   removed redundant sjmp
1566%1:
1567}
1568
1569replace {
1570        sjmp %1
1571%2:
1572%1:
1573} by {
1574        ; Peephole 201   removed redundant sjmp
1575%2:
1576%1:
1577}
1578
1579replace {
1580        push  acc
1581        mov   dptr,%1
1582        pop   acc
1583} by {
1584        ; Peephole 202   removed redundant push pop
1585        mov   dptr,%1
1586}
1587
1588replace {
1589        push  acc
1590        pop   acc
1591} by {
1592        ; Peephole 202b   removed redundant push pop
1593}
1594
1595replace {
1596        mov  r%1,_spx
1597        lcall %2
1598        mov  r%1,_spx
1599} by {
1600        ; Peephole 203   removed mov  r%1,_spx
1601        lcall %2
1602}
1603
1604replace {
1605        mov  %1,a
1606        add  a,acc
1607        mov  %1,a
1608} by {
1609        ; Peephole 204   removed redundant mov
1610        add  a,acc
1611        mov  %1,a
1612}
1613
1614replace {
1615        djnz %1,%2
1616        sjmp  %3
1617%2:
1618        sjmp  %4
1619%3:
1620} by {
1621        ; Peephole 205   optimized misc jump sequence
1622        djnz %1,%4
1623%2:
1624%3:
1625} if labelRefCount %2 1
1626
1627replace {
1628        mov  %1,%1
1629} by {
1630        ; Peephole 206   removed redundant mov %1,%1
1631}
1632
1633replace {
1634        mov  a,_bp
1635        add  a,#0x00
1636        mov  %1,a
1637} by {
1638        ; Peephole 207   removed zero add (acc not set to %1, flags undefined)
1639        mov  %1,_bp
1640}
1641
1642replace {
1643        push  acc
1644        mov   r%1,_bp
1645        pop   acc
1646} by {
1647        ; Peephole 208   removed redundant push pop
1648        mov   r%1,_bp
1649}
1650
1651replace {
1652        mov  a,_bp
1653        add  a,#0x00
1654        inc  a
1655        mov  %1,a
1656} by {
1657        ; Peephole 209   optimized increment (acc not set to %1, flags undefined)
1658        mov  %1,_bp
1659        inc  %1
1660}
1661
1662replace {
1663        mov  dptr,#((((%1 >> 16)) <<16) + (((%1 >> 8)) <<8) + %1)
1664} by {
1665        ; Peephole 210a   simplified expression
1666        mov  dptr,#%1
1667} if 24bitMode
1668
1669replace {
1670        mov  dptr,#((((%1 >> 8)) <<8) + %1)
1671} by {
1672        ; Peephole 210   simplified expression
1673        mov  dptr,#%1
1674}
1675
1676replace restart {
1677        push ar%1
1678        pop  ar%1
1679} by {
1680        ; Peephole 211   removed redundant push r%1 pop r%1
1681}
1682
1683replace {
1684	mov  a,_bp
1685	add  a,#0x01
1686	mov  r%1,a
1687} by {
1688	; Peephole 212  reduced add sequence to inc
1689	mov  r%1,_bp
1690	inc  r%1
1691}
1692
1693replace {
1694	mov  %1,#(( %2 >> 8 ) ^ 0x80)
1695} by {
1696	; Peephole 213.a inserted fix
1697	mov  %1,#(%2 >> 8)
1698	xrl  %1,#0x80
1699} if portIsDS390
1700
1701replace {
1702	mov  %1,#(( %2 >> 16 ) ^ 0x80)
1703} by {
1704	; Peephole 213.b inserted fix
1705	mov  %1,#(%2 >> 16)
1706	xrl  %1,#0x80
1707} if portIsDS390
1708
1709replace {
1710	mov  %1,#(( %2 + %3 >> 8 ) ^ 0x80)
1711} by {
1712	; Peephole 213.c inserted fix
1713	mov  %1,#((%2 + %3) >> 8)
1714	xrl  %1,#0x80
1715} if portIsDS390
1716
1717replace  {
1718	mov  %1,a
1719	mov  a,%2
1720	add  a,%1
1721} by {
1722	; Peephole 214 reduced some extra movs
1723	mov  %1,a
1724	add  a,%2
1725} if notSame(%1 %2)
1726
1727replace {
1728	mov  %1,a
1729	add  a,%2
1730	mov  %1,a
1731} by {
1732	; Peephole 215 removed some movs
1733	add  a,%2
1734	mov  %1,a
1735} if notSame(%1 %2)
1736
1737replace {
1738	mov   r%1,%2
1739	clr   a
1740	inc   r%1
1741	mov   @r%1,a
1742	dec   r%1
1743	mov   @r%1,a
1744} by {
1745	; Peephole 216 simplified clear (2bytes)
1746	mov   r%1,%2
1747	clr   a
1748	mov   @r%1,a
1749	inc   r%1
1750	mov   @r%1,a
1751}
1752
1753replace {
1754	mov   r%1,%2
1755	clr   a
1756	inc   r%1
1757	inc   r%1
1758	mov   @r%1,a
1759	dec   r%1
1760	mov   @r%1,a
1761	dec   r%1
1762	mov   @r%1,a
1763} by {
1764	; Peephole 217 simplified clear (3bytes)
1765	mov   r%1,%2
1766	clr   a
1767	mov   @r%1,a
1768	inc   r%1
1769	mov   @r%1,a
1770	inc   r%1
1771	mov   @r%1,a
1772}
1773
1774replace {
1775	mov   r%1,%2
1776	clr   a
1777	inc   r%1
1778	inc   r%1
1779	inc   r%1
1780	mov   @r%1,a
1781	dec   r%1
1782	mov   @r%1,a
1783	dec   r%1
1784	mov   @r%1,a
1785	dec   r%1
1786	mov   @r%1,a
1787} by {
1788	; Peephole 218 simplified clear (4bytes)
1789	mov   r%1,%2
1790	clr   a
1791	mov   @r%1,a
1792	inc   r%1
1793	mov   @r%1,a
1794	inc   r%1
1795	mov   @r%1,a
1796	inc   r%1
1797	mov   @r%1,a
1798}
1799
1800replace {
1801	clr   a
1802	movx  @dptr,a
1803	mov   dptr,%1
1804	clr   a
1805	movx  @dptr,a
1806} by {
1807	; Peephole 219 removed redundant clear
1808	clr   a
1809	movx  @dptr,a
1810	mov   dptr,%1
1811	movx  @dptr,a
1812}
1813
1814replace {
1815	clr   a
1816	movx  @dptr,a
1817	mov   dptr,%1
1818	movx  @dptr,a
1819	mov   dptr,%2
1820	clr   a
1821	movx  @dptr,a
1822} by {
1823	; Peephole 219a removed redundant clear
1824	clr   a
1825	movx  @dptr,a
1826	mov   dptr,%1
1827	movx  @dptr,a
1828	mov   dptr,%2
1829	movx  @dptr,a
1830}
1831
1832replace {
1833        mov     dps, #0
1834        mov     dps, #1
1835} by {
1836        ; Peephole 220a removed bogus DPS set
1837        mov     dps, #1
1838}
1839
1840replace {
1841        mov     dps, #1
1842        mov     dps, #0
1843} by {
1844        ; Peephole 220b removed bogus DPS set
1845        mov     dps, #0
1846}
1847
1848replace {
1849        mov     dps, #0
1850        mov     dps, #0x01
1851} by {
1852        ; Peephole 220c removed bogus DPS set
1853}
1854
1855replace {
1856        mov     dps,#1
1857	inc	dptr
1858        mov     dps,#1
1859} by {
1860        ; Peephole 220d removed bogus DPS set
1861        mov     dps,#1
1862	inc	dptr
1863}
1864
1865replace {
1866	mov	%1 + %2,(%2 + %1)
1867} by {
1868	; Peephole 221a remove redundant move
1869}
1870
1871replace {
1872	mov	(%1 + %2 + %3),((%2 + %1) + %3)
1873} by {
1874	; Peephole 221b remove redundant move
1875}
1876
1877replace {
1878	dec	r%1
1879	inc	r%1
1880} by {
1881	; removed dec/inc pair
1882}
1883
1884replace {
1885	mov     dps, #0
1886	mov     %1,a
1887	mov     dps, #1
1888} by {
1889	; Peephole 222 removed DPS abuse.
1890	mov     %1,a
1891        mov     dps, #1
1892}
1893
1894replace {
1895	mov	dps, #0
1896	xch	a, ap
1897	mov 	%1, ap
1898	mov	dps, #1
1899} by {
1900	; Peephole 222a removed DPS abuse.
1901	xch     a, ap
1902        mov     %1, ap
1903        mov     dps, #1
1904}
1905
1906replace {
1907	mov 	dps, #%1
1908	inc	dptr
1909	movx	a,@dptr
1910	mov	%2,a
1911	mov	dps, #%1
1912} by {
1913	mov     dps, #%1
1914        inc     dptr
1915        movx    a,@dptr
1916        mov     %2,a
1917; Peephole 223: yet more DPS abuse removed.
1918}
1919
1920replace {
1921	mov	dps, #0
1922	inc	dps
1923} by {
1924	mov	dps, #1
1925}
1926
1927replace {
1928	mov	dps, #%1
1929	mov	dptr, %2
1930	mov	dps, #%1
1931} by {
1932	mov     dps, #%1
1933        mov     dptr, %2
1934}
1935
1936replace {
1937	mov	dps, #1
1938	mov	dptr, %1
1939	mov	dps, #0
1940	mov	dptr, %2
1941	inc	dps
1942} by {
1943	mov	dps, #0
1944	mov	dptr, %2
1945	inc	dps
1946	mov	dptr, %1
1947; Peephole 224a: DPS usage re-arranged.
1948}
1949
1950replace {
1951	mov	dps, #%1
1952	mov	dptr, %2
1953	mov	dps, #%3
1954	mov	dptr, %4
1955	mov	dps, #%1
1956} by {
1957	mov	dps, #%3
1958	mov	dptr, %4
1959	mov	dps, #%1
1960	mov	dptr, %2
1961; Peephole 224: DPS usage re-arranged.
1962}
1963
1964replace {
1965	mov	dps, #1
1966	mov	dptr, %1
1967	mov	dps, #0
1968} by {
1969	mov	dps, #1
1970	mov	dptr, %1
1971	dec	dps
1972}
1973
1974replace {
1975	xch	a, ap
1976	add	a, ap
1977} by {
1978	add 	a, ap
1979}
1980
1981replace {
1982	xch	a, ap
1983	addc	a, ap
1984} by {
1985	addc 	a, ap
1986}
1987
1988replace {
1989	inc 	dps
1990	mov	dps, #%1
1991} by {
1992	mov     dps, #%1
1993}
1994
1995replace {
1996	dec 	dps
1997	mov	dps, #%1
1998} by {
1999	mov     dps, #%1
2000}
2001
2002
2003// char indexed access to: long code table[] = {4,3,2,1};
2004replace restart {
2005	add	a,#%1
2006	mov	dpl,a
2007	clr  a
2008	addc	a,#(%1 >> 8)
2009	mov	dph,a
2010	clr  a
2011	addc	a,#(%1 >> 16)
2012	mov	dpx,a
2013	clr	a
2014	movc	a,@a+dptr
2015	inc	dptr
2016	mov	%2,a
2017	clr	a
2018	movc	a,@a+dptr
2019	inc	dptr
2020	mov	%3,a
2021	clr	a
2022	movc	a,@a+dptr
2023	inc	dptr
2024	mov	%4,a
2025	clr	a
2026	movc	a,@a+dptr
2027} by {
2028	; Peephole 227.a movc optimize
2029	mov	b,a
2030	mov	dptr,#%1
2031	movc	a,@a+dptr
2032	inc	dptr
2033	mov	%2,a
2034	mov	a,b
2035	movc	a,@a+dptr
2036	inc	dptr
2037	mov	%3,a
2038	mov	a,b
2039	movc	a,@a+dptr
2040	inc	dptr
2041	mov	%4,a
2042	mov	a,b
2043	movc	a,@a+dptr
2044}
2045
2046// char indexed access to: void* code table[] = {4,3,2,1};
2047replace restart {
2048	add	a,#%1
2049	mov	dpl,a
2050	clr  a
2051	addc	a,#(%1 >> 8)
2052	mov	dph,a
2053	clr  a
2054	addc	a,#(%1 >> 16)
2055	mov	dpx,a
2056	clr	a
2057	movc	a,@a+dptr
2058	inc	dptr
2059	mov	%2,a
2060	clr	a
2061	movc	a,@a+dptr
2062	inc	dptr
2063	mov	%3,a
2064	clr	a
2065	movc	a,@a+dptr
2066} by {
2067	; Peephole 227.b movc optimize
2068	mov	b,a
2069	mov	dptr,#%1
2070	movc	a,@a+dptr
2071	inc	dptr
2072	mov	%2,a
2073	mov	a,b
2074	movc	a,@a+dptr
2075	inc	dptr
2076	mov	%3,a
2077	mov	a,b
2078	movc	a,@a+dptr
2079}
2080
2081// char indexed access to: int code table[] = {4,3,2,1};
2082replace restart {
2083	add	a,#%1
2084	mov	dpl,a
2085	clr  a
2086	addc	a,#(%1 >> 8)
2087	mov	dph,a
2088	clr  a
2089	addc	a,#(%1 >> 16)
2090	mov	dpx,a
2091	clr	a
2092	movc	a,@a+dptr
2093	inc	dptr
2094	mov	%2,a
2095	clr	a
2096	movc	a,@a+dptr
2097} by {
2098	; Peephole 227.c movc optimize
2099	mov	%2,a
2100	mov	dptr,#%1
2101	movc	a,@a+dptr
2102	inc	dptr
2103	xch	a,%2
2104	movc	a,@a+dptr
2105}
2106
2107// char indexed access to: char code table[] = {4,3,2,1};
2108replace {
2109	add	a,#%1
2110	mov	dpl,a
2111	clr  a
2112	addc	a,#(%1 >> 8)
2113	mov	dph,a
2114	clr  a
2115	addc	a,#(%1 >> 16)
2116	mov	dpx,a
2117	clr	a
2118	movc	a,@a+dptr
2119} by {
2120	; Peephole 227.d movc optimize
2121	mov	dptr,#%1
2122	movc	a,@a+dptr
2123}
2124
2125replace {
2126	mov	r%1,%2
2127	mov	ar%1,%3
2128} by {
2129	; Peephole 228 redundant move
2130	mov	ar%1,%3
2131}
2132
2133replace {
2134	mov	r%1,a
2135	dec	r%1
2136	mov	a,r%1
2137} by {
2138	; Peephole 229.a redundant move
2139	dec	a
2140	mov	r%1,a
2141}
2142
2143replace {
2144	mov	r%1,a
2145	mov	r%2,b
2146	mov	a,r%1
2147} by {
2148	; Peephole 229.b redundant move
2149	mov	r%1,a
2150	mov	r%2,b
2151}
2152
2153replace {
2154	mov	r%1,a
2155	mov	r%2,b
2156	add	a,#%3
2157	mov	r%1,a
2158	mov	a,r%2
2159	addc	a,#(%3 >> 8)
2160	mov	r%2,a
2161} by {
2162	; Peephole 229.c redundant move
2163	add	a,#%3
2164	mov	r%1,a
2165	mov	a,b
2166	addc	a,#(%3 >> 8)
2167	mov	r%2,a
2168}
2169
2170replace {
2171	mov	a,%1
2172	mov	b,a
2173	movx	a,%2
2174} by {
2175	; Peephole 229.d redundant move
2176	mov	b,%1
2177	movx	a,%2
2178}
2179
2180replace {
2181	mov	dpl,r%1
2182	mov	dph,r%2
2183	mov	dpx,r%3
2184	movx	a,@dptr
2185	mov  	r%4,a
2186	add  	a,#0x01
2187	mov  	r%5,a
2188	mov  	dpl,r%1
2189	mov  	dph,r%2
2190	mov  	dpx,r%3
2191	movx	@dptr,a
2192} by {
2193	; Peephole 230.a save reload dptr
2194	mov	dpl,r%1
2195	mov	dph,r%2
2196	mov	dpx,r%3
2197	movx	a,@dptr
2198	mov  	r%4,a
2199	add  	a,#0x01
2200	mov  	r%5,a
2201	movx	@dptr,a
2202}
2203
2204replace {
2205	mov	dpl,r%1
2206	mov	dph,r%2
2207	mov	dpx,r%3
2208	movx	a,@dptr
2209	mov	r%4,a
2210	dec	r%4
2211	mov	dpl,r%1
2212	mov	dph,r%2
2213	mov	dpx,r%3
2214	mov	a,r%4
2215	movx	@dptr,a
2216} by {
2217	; Peephole 230.b save reload dptr
2218	mov	dpl,r%1
2219	mov	dph,r%2
2220	mov	dpx,r%3
2221	movx	a,@dptr
2222	dec	a
2223	mov	r%4,a
2224	movx	@dptr,a
2225}
2226
2227replace {
2228	mov	dpl,r%1
2229	mov	dph,r%2
2230	mov	dpx,r%3
2231	movx	a,@dptr
2232	inc  	a
2233	mov  	r%4,a
2234	mov	dpl,r%1
2235	mov	dph,r%2
2236	mov	dpx,r%3
2237	mov	a,r%4
2238	movx	@dptr,a
2239} by {
2240	; Peephole 230.c save reload dptr
2241	mov	dpl,r%1
2242	mov	dph,r%2
2243	mov	dpx,r%3
2244	movx	a,@dptr
2245	inc  	a
2246	mov  	r%4,a
2247	movx	@dptr,a
2248}
2249
2250replace {
2251	mov	r%1,dpl
2252	mov	r%2,dph
2253	mov	r%3,dpx
2254	mov	dpl,r%1
2255	mov	dph,r%2
2256	mov	dpx,r%3
2257} by {
2258	; Peephole 230.d save reload dptr
2259	mov	r%1,dpl
2260	mov	r%2,dph
2261	mov	r%3,dpx
2262}
2263
2264replace {
2265        mov     dpl,r%1
2266        mov     dph,r%2
2267        mov     dpx,r%3
2268        movx    a,@dptr
2269        mov     r%4,a
2270        orl     ar%4,#%5
2271        mov     dpl,r%1
2272        mov     dph,r%2
2273        mov     dpx,r%3
2274        mov     a,r1
2275        movx    @dptr,a
2276} by {
2277	; Peephole 230.e save reload dptr
2278        mov     dpl,r%1
2279        mov     dph,r%2
2280        mov     dpx,r%3
2281        movx    a,@dptr
2282        orl     a,#%5
2283        mov     r%4,a
2284        movx    @dptr,a
2285}
2286
2287replace {
2288        mov     dpl,r%1
2289        mov     dph,r%2
2290        mov     dpx,r%3
2291        movx    a,@dptr
2292        mov     r%4,a
2293        anl     ar%4,#%5
2294        mov     dpl,r%1
2295        mov     dph,r%2
2296        mov     dpx,r%3
2297        mov     a,r1
2298        movx    @dptr,a
2299} by {
2300	; Peephole 230.e save reload dptr
2301        mov     dpl,r%1
2302        mov     dph,r%2
2303        mov     dpx,r%3
2304        movx    a,@dptr
2305        anl     a,#%5
2306        mov     r%4,a
2307        movx    @dptr,a
2308}
2309
2310replace {
2311	mov	r%1,dpl
2312	mov	r%2,dph
2313	mov	r%3,dpx
2314	mov	a,r%4
2315	inc	dps
2316	movx	@dptr,a
2317	inc	dptr
2318	mov	dps,#0
2319	mov	dpl,r%1
2320	mov	dph,r%2
2321	mov	dpx,r%3
2322} by {
2323	; Peephole 230.f save reload dptr
2324	mov	r%1,dpl
2325	mov	r%2,dph
2326	mov	r%3,dpx
2327	mov	a,r%4
2328	inc	dps
2329	movx	@dptr,a
2330	inc	dptr
2331	mov	dps,#0
2332}
2333
2334replace {
2335	mov	ar%1,r%2
2336	mov	ar%3,r%1
2337	mov	r%1,#0x00
2338	mov	ar%2,r%4
2339	mov	r3,#0x00
2340} by {
2341	; Peephole 231.a simplified moves
2342	mov	ar%3,r%2
2343	mov	ar%2,r%4
2344	mov	r%4,#0
2345	mov	r%1,#0
2346}
2347
2348replace {
2349	mov	r%1,#0
2350	mov	r%2,#0
2351	mov	a,r%2
2352	orl	a,r%3
2353	mov	%4,a
2354	mov	a,r%5
2355	orl	a,r%1
2356	mov	%6,a
2357} by {
2358	; Peephole 231.b simplified or
2359	mov	r%1,#0
2360	mov	r%2,#0
2361	mov	a,r%3
2362	mov	%4,a
2363	mov	a,r%5
2364	mov	%6,a
2365}
2366
2367replace {
2368	mov	a,r%1
2369	mov	b,r%2
2370	mov	r%1,b
2371	mov	r%2,a
2372} by {
2373	; Peehole 232.a simplified xch
2374	mov	a,r%1
2375	xch	a,r%2
2376	mov	r%1,a
2377}
2378
2379replace {
2380	mov	a,#%1
2381	mov	b,#%2
2382	mov	r%3,b
2383	mov	r%4,a
2384} by {
2385	; Peehole 232.b simplified xch
2386	mov	r%3,#%2
2387	mov	r%4,#%1
2388}
2389
2390replace {
2391	mov	dpl1,#%1
2392	mov	dph1,#(%1 >> 8)
2393	mov	dpx1,#(%1 >> 16)
2394} by {
2395	; Peephole 233 24 bit load of dptr1
2396	inc	dps
2397	mov	dptr,#%1
2398	dec	dps
2399}
2400
2401// 14 rules by Fiorenzo D. Ramaglia <fd.ramaglia@tin.it>
2402
2403replace {
2404        add  a,ar%1
2405} by {
2406        ; Peephole 236a
2407        add  a,r%1
2408}
2409
2410replace {
2411        addc  a,ar%1
2412} by {
2413        ; Peephole 236b
2414        addc  a,r%1
2415}
2416
2417replace {
2418        anl  a,ar%1
2419} by {
2420        ; Peephole 236c
2421        anl  a,r%1
2422}
2423
2424replace {
2425        dec  ar%1
2426} by {
2427        ; Peephole 236d
2428        dec  r%1
2429}
2430
2431replace {
2432        djnz  ar%1,%2
2433} by {
2434        ; Peephole 236e
2435        djnz  r%1,%2
2436}
2437
2438replace {
2439        inc  ar%1
2440} by {
2441        ; Peephole 236f
2442        inc  r%1
2443}
2444
2445replace {
2446        mov  a,ar%1
2447} by {
2448        ; Peephole 236g
2449        mov  a,r%1
2450}
2451
2452replace {
2453        mov  ar%1,#%2
2454} by {
2455        ; Peephole 236h
2456        mov  r%1,#%2
2457}
2458
2459replace {
2460        mov  ar%1,a
2461} by {
2462        ; Peephole 236i
2463        mov  r%1,a
2464}
2465
2466replace {
2467        mov  ar%1,ar%2
2468} by {
2469        ; Peephole 236j
2470        mov  r%1,ar%2
2471}
2472
2473replace {
2474        orl  a,ar%1
2475} by {
2476        ; Peephole 236k
2477        orl  a,r%1
2478}
2479
2480replace {
2481        subb  a,ar%1
2482} by {
2483        ; Peephole 236l
2484        subb  a,r%1
2485}
2486
2487replace {
2488        xch  a,ar%1
2489} by {
2490        ; Peephole 236m
2491        xch  a,r%1
2492}
2493
2494replace {
2495        xrl  a,ar%1
2496} by {
2497        ; Peephole 236n
2498        xrl  a,r%1
2499}
2500
2501replace {
2502        sjmp    %1
2503%2:
2504        mov     %3,%4
2505%1:
2506        ret
2507} by {
2508        ; Peephole 237a  removed sjmp to ret
2509        ret
2510%2:
2511        mov     %3,%4
2512%1:
2513        ret
2514}
2515
2516replace {
2517        sjmp    %1
2518%2:
2519        mov     %3,%4
2520        mov     dpl,%5
2521        mov     dph,%6
2522%1:
2523        ret
2524} by {
2525        ; Peephole 237b  removed sjmp to ret
2526        ret
2527%2:
2528        mov     %3,%4
2529        mov     dpl,%5
2530        mov     dph,%6
2531%1:
2532        ret
2533}
2534
2535// applies to f.e. device/lib/log10f.c
2536replace {
2537        mov     %1,%9
2538        mov     %2,%10
2539        mov     %3,%11
2540        mov     %4,%12
2541
2542        mov     %5,%13
2543        mov     %6,%14
2544        mov     %7,%15
2545        mov     %8,%16
2546
2547        mov     %9,%1
2548        mov     %10,%2
2549        mov     %11,%3
2550        mov     %12,%4
2551} by {
2552        mov     %1,%9
2553        mov     %2,%10
2554        mov     %3,%11
2555        mov     %4,%12
2556
2557        mov     %5,%13
2558        mov     %6,%14
2559        mov     %7,%15
2560        mov     %8,%16
2561        ;       Peephole 238.a  removed 4 redundant moves
2562} if notSame(%1 %2 %3 %4 %5 %6 %7 %8)
2563
2564// applies to device/lib/log10f.c
2565replace {
2566        mov     %1,%5
2567        mov     %2,%6
2568        mov     %3,%7
2569        mov     %4,%8
2570
2571        mov     %5,%1
2572        mov     %6,%2
2573        mov     %7,%3
2574} by {
2575        mov     %1,%5
2576        mov     %2,%6
2577        mov     %3,%7
2578        mov     %4,%8
2579        ;       Peephole 238.b  removed 3 redundant moves
2580} if notSame(%1 %2 %3 %4 %5 %6 %7)
2581
2582// applies to f.e. device/lib/time.c
2583replace {
2584        mov     %1,%5
2585        mov     %2,%6
2586
2587        mov     %3,%7
2588        mov     %4,%8
2589
2590        mov     %5,%1
2591        mov     %6,%2
2592} by {
2593        mov     %1,%5
2594        mov     %2,%6
2595
2596        mov     %3,%7
2597        mov     %4,%8
2598        ;       Peephole 238.c  removed 2 redundant moves
2599} if notSame(%1 %2 %3 %4)
2600
2601// applies to f.e. support/regression/tests/bug-524209.c
2602replace {
2603        mov     %1,%4
2604        mov     %2,%5
2605        mov     %3,%6
2606
2607        mov     %4,%1
2608        mov     %5,%2
2609        mov     %6,%3
2610} by {
2611        mov     %1,%4
2612        mov     %2,%5
2613        mov     %3,%6
2614        ;       Peephole 238.d  removed 3 redundant moves
2615} if notSame(%1 %2 %3 %4 %5 %6)
2616
2617// applies to f.e. ser_ir.asm
2618replace {
2619        mov     r%1,acc
2620} by {
2621        ;       Peephole 239    used a instead of acc
2622        mov     r%1,a
2623}
2624
2625replace restart {
2626	mov	a,%1
2627	addc	a,#0x00
2628} by {
2629	;	Peephole 240    use clr instead of addc a,#0
2630	clr	a
2631	addc	a,%1
2632}
2633
2634// peepholes 241.a to 241.c and 241.d to 241.f need to be in order
2635replace {
2636        cjne    r%1,#%2,%3
2637        cjne    r%4,#%5,%3
2638        cjne    r%6,#%7,%3
2639        cjne    r%8,#%9,%3
2640        mov     a,#0x01
2641        sjmp    %10
2642%3:
2643        clr     a
2644%10:
2645} by {
2646        ;       Peephole 241.a  optimized compare
2647        clr     a
2648        cjne    r%1,#%2,%3
2649        cjne    r%4,#%5,%3
2650        cjne    r%6,#%7,%3
2651        cjne    r%8,#%9,%3
2652        inc     a
2653%3:
2654%10:
2655}
2656
2657// applies to f.e. time.c
2658replace {
2659        cjne    r%1,#%2,%3
2660        cjne    r%4,#%5,%3
2661        mov     a,#0x01
2662        sjmp    %6
2663%3:
2664        clr     a
2665%6:
2666} by {
2667        ;       Peephole 241.b  optimized compare
2668        clr     a
2669        cjne    r%1,#%2,%3
2670        cjne    r%4,#%5,%3
2671        inc     a
2672%3:
2673%6:
2674}
2675
2676// applies to f.e. malloc.c
2677replace {
2678        cjne    r%1,#%2,%3
2679        mov     a,#0x01
2680        sjmp    %4
2681%3:
2682        clr     a
2683%4:
2684} by {
2685        ;       Peephole 241.c  optimized compare
2686        clr     a
2687        cjne    r%1,#%2,%3
2688        inc     a
2689%3:
2690%4:
2691}
2692
2693// applies to f.e. j = (k!=0x1000);
2694// with volatile idata long k;
2695replace {
2696        cjne    @r%1,#%2,%3
2697	inc	r%1
2698        cjne    @r%1,#%4,%3
2699	inc	r%1
2700        cjne    @r%1,#%5,%3
2701	inc	r%1
2702        cjne    @r%1,#%6,%3
2703        mov     a,#0x01
2704        sjmp    %7
2705%3:
2706        clr     a
2707%7:
2708} by {
2709        ;       Peephole 241.d  optimized compare
2710        clr     a
2711        cjne    @r%1,#%2,%3
2712	inc	r%1
2713        cjne    @r%1,#%4,%3
2714	inc	r%1
2715        cjne    @r%1,#%5,%3
2716	inc	r%1
2717        cjne    @r%1,#%6,%3
2718        inc     a
2719%3:
2720%7:
2721}
2722
2723// applies to f.e. j = (k!=0x1000);
2724// with volatile idata int k;
2725replace {
2726        cjne    @r%1,#%2,%3
2727	inc	r%1
2728        cjne    @r%1,#%4,%3
2729        mov     a,#0x01
2730        sjmp    %7
2731%3:
2732        clr     a
2733%7:
2734} by {
2735        ;       Peephole 241.e  optimized compare
2736        clr     a
2737        cjne    @r%1,#%2,%3
2738	inc	r%1
2739        cjne    @r%1,#%4,%3
2740        inc     a
2741%3:
2742%7:
2743}
2744
2745// applies to f.e. vprintf.asm (--stack-auto)
2746replace {
2747        cjne    @r%1,#%2,%3
2748        mov     a,#0x01
2749        sjmp    %7
2750%3:
2751        clr     a
2752%7:
2753} by {
2754        ;       Peephole 241.f  optimized compare
2755        clr     a
2756        cjne    @r%1,#%2,%3
2757        inc     a
2758%3:
2759%7:
2760}
2761
2762// applies to f.e. scott-bool1.c
2763replace {
2764        jnz     %1
2765        mov     %2,%3
2766%1:
2767        jz      %4
2768} by {
2769        ;       Peephole 242.a  avoided branch jnz to jz
2770        jnz     %1
2771        mov     %2,%3
2772        jz      %4
2773%1:
2774} if labelRefCount %1 1
2775
2776// applies to f.e. scott-bool1.c
2777replace {
2778        jnz     %1
2779        mov     %2,%3
2780        orl     a,%5
2781%1:
2782        jz      %4
2783} by {
2784        ;       Peephole 242.b  avoided branch jnz to jz
2785        jnz     %1
2786        mov     %2,%3
2787        orl     a,%5
2788        jz      %4
2789%1:
2790} if labelRefCount %1 1
2791
2792// applies to f.e. logic.c
2793replace {
2794        jnz     %1
2795        mov     %2,%3
2796        orl     a,%5
2797        orl     a,%6
2798        orl     a,%7
2799%1:
2800        jz      %4
2801} by {
2802        ;       Peephole 242.c  avoided branch jnz to jz
2803        jnz     %1
2804        mov     %2,%3
2805        orl     a,%5
2806        orl     a,%6
2807        orl     a,%7
2808        jz      %4
2809%1:
2810} if labelRefCount %1 1
2811
2812replace {
2813        jnz     %1
2814%1:
2815} by {
2816        ;       Peephole 243a    jump optimization
2817} if labelRefCount %1 1
2818
2819replace {
2820        jz      %1
2821%1:
2822} by {
2823        ;       Peephole 243b    jump optimization
2824} if labelRefCount %1 1
2825
2826
2827// This allows non-interrupt and interrupt code to safely compete
2828// for a resource without the non-interrupt code having to disable
2829// interrupts:
2830// volatile bit resource_is_free;
2831// if( resource_is_free ) {
2832//     resource_is_free=0; do_something; resource_is_free=1;
2833// }
2834replace {
2835        jnb     %1,%2
2836%3:
2837        clr     %1
2838} by {
2839        ;       Peephole 244.a  using atomic test and clear
2840        jbc     %1,%3
2841        sjmp    %2
2842%3:
2843} if labelRefCount %3 0
2844
2845replace {
2846        jb      %1,%2
2847        ljmp    %3
2848%2:
2849        clr     %1
2850} by {
2851        ;       Peephole 244.b  using atomic test and clear
2852        jbc     %1,%2
2853        ljmp    %3
2854%2:
2855} if labelRefCount %2 1
2856
2857