; Author: Santiago Ontañón
; - Patterns are separated by a blank line
; - Patterns suggested by others are credited to them below.
; - If you do a pull request with more patterns, please credit yourself in a comment before the pattern.

include "pbo-patterns.txt"

pattern: Replace jp ?const1 with jr ?const1
name: jp2jr
0: jp ?const1
replacement:
0: jr ?const1
constraints:
reachableByJr(0,?const1)

pattern: Replace jp ?any,?const1 with jr ?any,?const1
0: jp ?any,?const1
replacement:
0: jr ?any,?const1
constraints:
in(?any,C,NC,Z,NZ)
reachableByJr(0,?const1)

pattern: Replace ld ?reg1l,?reg2l; ld ?reg1h,?reg2h with push ?regpair2; pop ?regpair1
0: ld ?reg1l,?reg2l
1: ld ?reg1h,?reg2h
replacement:
0: push ?regpair2
1: pop ?regpair1
constraints:
regpair(?regpair1,?reg1h,?reg1l)
regpair(?regpair2,?reg2h,?reg2l)
in(?regpair2,IX,IY)
in(?regpair1,BC,DE,HL)

pattern: Replace ld ?reg1h,?reg2h; ld ?reg1l,?reg2l with push ?regpair2; pop ?regpair1
0: ld ?reg1h,?reg2h
1: ld ?reg1l,?reg2l
replacement:
0: push ?regpair2
1: pop ?regpair1
constraints:
regpair(?regpair1,?reg1h,?reg1l)
regpair(?regpair2,?reg2h,?reg2l)
in(?regpair2,IX,IY)
in(?regpair1,BC,DE,HL)

pattern: Replace inc ?regixiy; ... inc ?regixiy with push bc; ld bc,?const; add ?regixiy,bc; pop bc
0: [?const] inc ?regixiy
replacement:
0: push bc
1: ld bc,?const
2: add ?regixiy,bc
3: pop bc
constraints:
in(?regixiy,IX,IY)
equal(?const >= 4, -1) 

pattern: Replace dec ?regixiy; ... dec ?regixiy with push bc; ld bc,-?const; add ?regixiy,bc; pop bc
0: [?const] dec ?regixiy
replacement:
0: push bc
1: ld bc,-?const
2: add ?regixiy,bc
3: pop bc
constraints:
in(?regixiy,IX,IY)
equal(?const >= 4, -1)
 