#lang scheme/unit

;; PIC18 code generator for control flow

;; This is kept separate to be able to use the core PIC18 compiler in
;; the functional concatenative language, which has its own control
;; mechanism.


(import control^ jump^ cfg^ pic18-extra^ memory-extra^ pic18-assembler^)
(export rstack^)


 (([qw l] jw/nz)   ([bpz 1 l])) ;; ( label -- )   ;; FIXME
 ((xdrop)          ([movf POSTDEC1 1 0]))


  (macro) macro:

 ;; (then    m> label: swapbra)  ;; swapbra is an optimization hook for PIC18

 ;; control flow
 ;; simple for..next
 (for0  >x begin)
 (next0 sym label:      ;; split here
        x1- m> jw/nz

 (for for0)
 (next next0)

 ;; control stack
 (+x    PREINC1)
 (x-    POSTDEC1)
 (x     INDF1)

 ;; Note, this could serve as the ANS 'R' stack, since it's cell size.
 (>x    +x !)
 (x@    x  @)
 (x>    x- @)

 ;; target is register, not wreg
 (x1-   x 1-!)

  ;; the control stack is used to help with other data stack jugglings
 (swap>x  2nd- +x movff)
 (over>x  2nd  +x movff)