#lang scheme/base
(require
"const.ss"
"asm.ss"
"../target.ss"
"../asm.ss"
"../tools.ss"
"../scat.ss"
"../coma.ss"
"../control.ss"
"../comp.ss"
)
(declare-stubs
~bit! ~bit? ~toggle
~@ ~!
~>z
)
(declare-stubs
f->)
(provide
(all-defined-out))
(compositions
(scat) scat:
(truncate #xFF and)
(rot<< truncate dup 7 >>> swap 1 <<< or truncate)
(rot>> truncate dup 7 <<< swap 1 >>> or truncate))
(patterns-class
(macro)
(word opcode)
((1+ incf)
(1- decf)
(rot<<c rlcf)
(rot>>c rrcf)
(rot<< rlncf)
(rot>> rrncf)
(swap-nibble swapf))
(([movf f 0 0] word) ([opcode f 0 0]))
((word) ([opcode WREG 0 0])))
(patterns-class
(macro)
(word opcode)
((1-! decf)
(1+! incf))
(([qw f] word) ([opcode f 1 0])))
(patterns-class
(macro)
(word metafn l-opcode s-opcode)
((+ + addlw addwf)
(and and andlw andwf)
(or or iorlw iorwf)
(xor xor xorlw xorwf)
(pow pow invalid invalid) (>>> >>> invalid invalid)
(<<< <<< invalid invalid)
(/ / invalid invalid)
(* * invalid invalid))
(([qw a ] [qw b] word) ([qw (tscat: a b metafn)]))
(([l-opcode a] [qw b] word) ([l-opcode (tscat: a b metafn)]))
(([qw a] word) ([l-opcode a]))
(([save] [movf a 0 0] word) ([s-opcode a 0 0])) ((word) ([s-opcode POSTDEC0 0 0])))
(patterns-class
(macro)
(word s-opcode)
((++ addwfc))
(([save] [movf a 0 0] word) ([s-opcode a 0 0])) ((word) ([s-opcode POSTDEC0 0 0])))
(patterns-class
(macro)
(word opcode)
((--! subwfb)
(-! subwf)
(++! addwfc)
(+! addwf)
(and! andwf)
(or! iorwf)
(xor! xorwf))
(([dup] [qw f] word) ([opcode f 1 0])) (([qw f] word) ([opcode f 1 0] [drop])))
(patterns-class
(macro)
opcode (movf xorwf andwf iorwf subwf subfwb addwf addwfc comf rrcf rlcf rrncf rlncf)
(([qw f] opcode) ([opcode f 0 0])))
(patterns-class
(macro)
opcode (cpfseq cpfsgt cpfslt clrf setf movwf mulwf)
(([qw f] opcode) ([opcode f 0])))
(patterns-class
(macro)
opcode (push pop sleep reset nop clrwdt daw tblrd* tblrd*- tblrd*+ tblwt* tblwt*- tblwt*+)
((opcode) ([opcode])))
(patterns
(macro)
((,word backspace) ())
(([qw a] pseudo) ([save] [movlw a]))
(([cw a] pseudo) ([jsr 0 a]))
(([jw a] pseudo) ([jsr 1 a]))
(([movlw a] [exit] pseudo) ([retlw a]))
(([exit] pseudo) ([return 0]))
((pseudo) ())
(([drop] [save] opti-save) ())
(([,op (? (target = POSTDEC0)) 0 0] [save] opti-save) ([,op INDF0 1 0]))
(([save] opti-save) ([dup]))
((opti-save) ())
(([qw a ] [qw b] -) ([qw (tscat: a b -)]))
(([addlw a] [qw b] -) ([addlw (tscat: a b -)]))
(([qw a] -) ([addlw (tscat: a -1 *)]))
(([save] [movf a 0 0] -) ([bpf 0 STATUS 0 0] [subfwb a 0 0]))
((-) ([subwf POSTDEC0 0 0]))
(([save] [movf a 0 0] --) ([subfwb a 0 0])) ((--) ([subwfb POSTDEC0 0 0]))
(([movlw a] @) ([movf a 0 0])) (([qw a] @) ([save] [movf a 0 0]))
((@) (macro: ~@))
(([qw 0] [qw a] !) ([clrf a 0])) (([qw -1] [qw a] !) ([setf a 0]))
(([dup] [qw a] !) ([movwf a 0])) (([qw x] [qw y] [qw a] !)
(if (eq? x y)
(asm [qw x] [movwf a 0]) (asm [qw y] [movwf a 0] [drop] [qw x]))) (([qw a] !) ([movwf a 0] [drop])) ((!) (macro: ~!))
(([qw lo] [qw hi] a!!) ([~lfsr 2 hi] [~nop lo]))
((a!!) (macro: ~a!!))
(([qw r] swap!) ([xorwf r 0 0] [xorwf r 1 0]
[xorwf r 0 0]))
(([qw a] dup) ([qw a] [qw a]))
(([drop] dup) ([movf INDF0 0 0]))
((dup) ([dup]))
(([qw a] drop) ())
((drop ) ([drop]))
(([qw a] [qw b] swap) ([qw b] [qw a]))
((swap) ([xorwf INDF0 0 0] [xorwf INDF0 1 0]
[xorwf INDF0 0 0]))
(([,opc f d a] d=reg) ([,opc f 1 a]))
(([,opc f d a] d=w) ([,opc f 0 a]))
((return) ([return 0]))
(([qw a] movlw) ([movlw a]))
(([qw a] retlw) ([retlw a]))
(([qw a] sublw) ([sublw a])) (([qw s] [qw d] movff) ([~movff s] [~nop d]))
(([qw s] retfie) ([retfie s]))
(([qw addr] [qw reg] lfsr) ([~lfsr reg (tscat: addr 8 >>>)]
[~nop addr]))
(([db lo] [qw hi] |,|) ([d2 lo hi]))
(([qw lo] |,|) ([db lo]))
(([qw w] |,,|) ([dw w]))
(([qw f] [qw b] [qw p] bit?) ([bit? f b p]))
((bit?) (macro: ~bit?))
(([qw p] pz?) ([flag? 'bpz p]))
(([qw p] pc?) ([flag? 'bpc p]))
(([qw p] pn?) ([flag? 'bpn p]))
(([qw l] nzjump) ([bpz 1 l]))
(([btfsp p f b a] [bra l1] ,ins [label l2] swapbra)
(if (eq? l1 l2)
`([btfsp ,(flip p) ,f ,b ,a] ,ins)
(error 'then-opti-error)))
((swapbra) ())
(([bit? f b p] [qw l] or-jump) ([btfsp (flip p) f b 0] [bra l]))
(([flag? opc p] [qw l] or-jump) ([,opc (flip p) l]))
(([cmp? opc f a 0] [qw l] or-jump) ([,opc f a] [bra 1] [bra l]))
(([cmp? opc f a 1] [qw l] or-jump) ([,opc f a] [bra l]))
(([qw l] or-jump) (macro: ~>z ,(insert `([bpz 0 ,l]))))
(([bit? f b p] not) ([bit? f b (flip p)]))
(([flag? opc p] not) ([flag? opc (flip p)]))
(([cmp? opc f a p] not) ([cmp? opc f a (flip p)]))
((=?) ([cmp? 'cpfseq INDF0 0 1]))
((>?) ([cmp? 'cpfsgt INDF0 0 1]))
((<?) ([cmp? 'cpfslt INDF0 0 1]))
(([drop] [qw f] [qw b] [qw c] bit!) ([bpf (flip c) f b 0] [drop]))
(([qw f] [qw b] [qw c] bit!) ([bpf (flip c) f b 0]))
((bit!) (macro: ~bit!))
(([qw f] [qw b] toggle) ([btg f b 0]))
((toggle) (macro: ~toggle))
(([qw a] neg) ([qw (tscat: a -1 *)]))
((neg) ([negf WREG 0]))
(([movf f 0 0] preinc) ([incf f 1 0] [movf f 0 0]))
(([movf f 0 0] postinc) ([movf f 0 0] [incf f 1 0]))
((umul>PROD) ([mulwf POSTDEC0 0] [drop]))
((xdrop) ([movf POSTDEC1 1 0]))
((max) ([cpfsgt INDF0 0] [movwf INDF0 0] [drop]))
((min) ([cpfslt INDF0 0] [movwf INDF0 0] [drop]))
((badnop) ([~nop #xBAD]))
)
(compositions
(macro) macro:
(org 1 >>> word-org)
(org-push 1 >>> word-org-push)
(then m> label: swapbra)
(for0 >x begin)
(next0 sym label: x1- m> nzjump
xdrop)
(for for0)
(next next0)
(+x PREINC1)
(x- POSTDEC1)
(x INDF1)
(>x +x !)
(x@ x @)
(x> x- @)
(x1- x 1-!)
(1st WREG)
(2nd INDF0)
(2nd- POSTDEC0)
(@! movff)
(nfdrop 2nd- 1st @!) (test #xff and)
(>flags test nfdrop)
(even? 1st 0 low?)
(odd? 1st 0 high?)
(>=? <? not)
(<=? >? not)
(swap>x 2nd- +x movff)
(over>x 2nd +x movff)
(pick neg PLUSW0 movf)
(over 1 pick)
(nip POSTDEC0 movwf)
(high 1 bit!)
(low 0 bit!)
(clc STATUS C low)
(stc STATUS C high)
(c@ 0 rot<<c)
(c! STATUS !) (sign>c STATUS movwf STATUS rot<<!)
(high? 1 bit?)
(low? 0 bit?)
(z? 0 pz?)
(nz? 1 pz?)
(c? 0 pc?)
(nc? 1 pc?)
(n? 0 pn?)
(nn? 1 pn?)
(<< clc rot<<c)
(2/ #x80 + rot>>c #x40 xor)
(>> clc rot>>c)
(rot<<c! rlcf d=reg)
(rot>>c! rrcf d=reg)
(rot<<! rlncf d=reg)
(rot>>! rrncf d=reg)
(u* umul>PROD PRODL @)
(u** u* PRODH @)
(ah FSR2H)
(al FSR2L)
(fh TBLPTRH)
(fl TBLPTRL)
(a@@ al @ ah @) (f@@ fl @ fh @)
(~a!! ah ! al !)
(f!! fh ! fl !)
(@a+ POSTINC2 @)
(!a+ POSTINC2 !)
(@a- POSTDEC2 @)
(!a- POSTDEC2 !)
(!+a PREINC2 !)
(@+a PREINC2 @)
(@a INDF2 @)
(!a INDF2 !)
(@i PLUSW2 movf)
(!i POSTDEC0 PLUSW2 movff drop)
(@f+ save tblrd*+ TABLAT movf)
(@f save tblrd* TABLAT movf)
(!f+ TABLAT movwf tblwt*+ drop)
(abs 1st 7 high? if neg then)
(~not z? if -1 else 0 then)
)
(define pic18-postprocess
(macros->postprocess (macro)
pseudo
opti-save))
(target-postprocess
pic18-postprocess)
(ir-ops
(cmp? opcode reg a d)
(flag? opcode inverted)
(bit? f b p)
(invalid . _))
(check-opcodes asm-find)