#!/usr/local/bin/clispsh

;; Postprocess avcall-m68k-amiga.s.
;; Swap the two instructions
;;        addw #-1056,sp
;;        moveml #0x3f3c,sp@-
;; at the beginning and the two instructions
;;        moveml sp@+,#0x3cfc
;;        addw #1056,sp
;; at the end of the file.

;; This can apparently not been done with `sed'. `perl'? - just say no.

(defun process-file (istream ostream)
  (prog ((eof "EOF") line1 line2)
    next-line1
    (setq line1 (read-line istream nil eof))
    (when (eq line1 eof) (return))
    next-line2
    (setq line2 (read-line istream nil eof))
    (when (eq line2 eof) (write-line line1 ostream) (return))
    (cond ((and (search ",sp$" (concatenate 'string line1 "$"))
                (search "moveml" line2)
           )
           (write-line line2 ostream) (write-line line1 ostream) (go next-line1)
          )
          ((and (search "moveml" line1)
                (search ",sp$" (concatenate 'string line2 "$"))
           )
           (write-line line2 ostream) (write-line line1 ostream) (go next-line1)
          )
          (t (write-line line1 ostream)
             (setq line1 line2) (go next-line2)
    )     )
) )

(process-file *standard-input* *standard-output*)

