aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_schasm/macros.scm
blob: 31cc91ecc44fdce241b1a4eabc038966daf3e6f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
;; Assume that core.scm is loaded

(define *unique-counter* 0)
(define (next-unique)
  (inc! *unique-counter*)
  *unique-counter*)
(define *macro-scratch-reg* R12)

(define (%packed-string s)
  ;; emit length
  (define sl (string-length s))
  (u16 sl)			     ; cast to u16 to get bounds check
  (emit-word sl)

  ;; compute packed words
  (for-each (@ emit-byte) (string->ascii-list s)))

(define-syntax emit-test-eq
  (syntax-rules ()
    ((_ lhs rhs dest set)
     (begin (sub lhs rhs)
	    (br flag-zero (label dest) set: set)))))

(define-syntax emit-test-lt
  (syntax-rules ()
    ((_ lhs rhs dest set)
     (begin (sub lhs rhs)
	    (br flag-sign (label dest) set: set)))))

(define-syntax emit-test-lte
  (syntax-rules ()
    ((_ lhs rhs dest set)
     (begin (sub lhs rhs)
	    (sub lhs (u16 1))
	    (br flag-sign (label dest) set: set)))))

(define-syntax emit-inverted-test
  ;; jump to dest if <lhs op rhs> is NOT true.
  ;; as such the behaviour of this macro can be considered to
  ;; be inverted.
  ;; the reason for this is to allow having the true branch
  ;; before the false branch in memory (arbitrary decision).
  (syntax-rules ()
    ((_ lhs op rhs dest)
     (cond
      ((eq? '== op) (emit-test-eq  lhs rhs dest #f))
      ((eq? '!= op) (emit-test-eq  lhs rhs dest #t))
      ((eq? '<  op) (emit-test-lt  lhs rhs dest #f))
      ((eq? '>= op) (emit-test-lt  lhs rhs dest #t))
      ((eq? '<= op) (emit-test-lte lhs rhs dest #f))
      ((eq? '>  op) (emit-test-lte lhs rhs dest #t))
      (else (error "unsupported operator" op))))))

(define-syntax %if-else
  (syntax-rules ()
    ((_ pred tb fb)
     (let* ((lhs  (eval (car `pred)))
	    (op   (cadr 'pred))
	    (rhs  (eval (caddr `pred)))
	    (n    (next-unique))
	    (sym-false (string->symbol (format "~A-false" n)))
	    (sym-end   (string->symbol (format "~A-end"   n))))
       (ld *macro-scratch-reg* lhs)

       (emit-inverted-test *macro-scratch-reg* op rhs sym-false)

       tb
       (ld PC (label sym-end))
       (def-label sym-false)
       fb
       (def-label sym-end)))))

(define-syntax %when
  (syntax-rules ()
    ((_ pred body body* ...)
     (let* ((lhs  (eval (car `pred)))
	    (op   (cadr 'pred))
	    (rhs  (eval (caddr `pred)))
	    (n    (next-unique))
	    (sym-end   (string->symbol (format "~A-end"   n))))
       (ld *macro-scratch-reg* lhs)

       (emit-inverted-test *macro-scratch-reg* op rhs sym-end)

       body body* ...

       (def-label sym-end)))))

(define-syntax %while
  (syntax-rules ()
    ((_ pred body body* ...)
     (let* ((lhs (eval (car `pred)))
	    (op  (cadr 'pred))
	    (rhs (eval (caddr `pred)))
	    (n   (next-unique))
	    (sym-test (string->symbol (format "~A-test" n)))
	    (sym-end  (string->symbol (format "~A-end"  n))))
       (ld *macro-scratch-reg* lhs)

       (def-label sym-test)
       (emit-inverted-test *macro-scratch-reg* op rhs sym-end)

       body body* ...

       (ld PC (label sym-test))
       (def-label sym-end)))))

(define *procedures* '())

(define-syntax %decl-proc
  (syntax-rules ()
    ((_ signature)
     (set! *procedures*
       (cons `signature *procedures*))
     )))

(define *proc-scope* (make-parameter #f))

(define (param pname)
  (or (assocdr pname (*proc-scope*))
      (error "no such param" pname)))

(define-syntax %def-proc
  (syntax-rules ()
    ((_ signature body* ...)
     (let* ((name     (car `signature))
	    (params   (cdr `signature))
	    (bindings (zip params
                           (map (@ reg) (iota (length params) 0)))))
       (%decl-proc signature)
       (print name)
       (def-label name)
       (parameterize ((*proc-scope* bindings))
	 body* ...)
       ))))

(define-syntax %call
  (syntax-rules ()
    ((_ name args* ...)
     (let* ((params  (or (assocdr 'name *procedures*)
			 (error "proc not defined" 'name)))
	    (args    (list args* ...))
	    (n       (next-unique))
	    (sym-ret (string->symbol (format "~A-ret" n))))

       (unless (= (length params) (length args))
	 (error (format "incorrect args, expected ~A, got ~A" params args)))

       (let loop ((i 0))
	 (when (< i (length args))
	   (let* ((rn (reg i)))
	     (ld rn (list-ref args i)))
	   (loop (+ 1 i))))

       (st SP (label sym-ret) push: #t)
       (ld PC (label 'name))
       (def-label sym-ret)
       ))))