1 (define bank-1-used? #f)
3 (define (linearize-and-cleanup cfg)
5 (define bbs-vector (cfg->vector cfg))
10 (set! todo (cons bb todo)))
15 (set! rev-code (cons instr rev-code)))
17 (define (outside-bank-0? adr)
18 (if (and (> adr #x5F) (< adr #xF60)) ; not a special register
19 (begin (set! bank-1-used? #t) #t)
21 (define (emit-byte-oriented op file #!optional (d? #t) (w? #f))
22 ;; we might have to access the second bank
23 (emit (if (outside-bank-0? file)
25 (list op (- file 96) (if w? 'w 'f) 'b)
26 (list op (- file 96) 'b))
28 (list op file (if w? 'w 'f) 'a)
30 (define (emit-bit-oriented op file bit)
31 (emit (if (outside-bank-0? file)
32 (list op (- file 96) bit 'b)
33 (list op file bit 'a))))
36 (emit (list 'movlw val)))
38 (emit-byte-oriented 'movwf adr #f))
40 (emit-byte-oriented 'movf adr #t #t))
41 (define (movff src dst)
42 ;; anything over #x5f is in the second bank (at #x100)
43 (let ((src (if (outside-bank-0? src)
46 (dst (if (outside-bank-0? dst)
49 (emit (list 'movff src dst))))
52 (emit-byte-oriented 'clrf adr #f))
54 (emit-byte-oriented 'setf adr #f))
57 (emit-byte-oriented 'incf adr))
59 (emit-byte-oriented 'decf adr))
62 (emit-byte-oriented 'addwf adr))
64 (emit-byte-oriented 'addwfc adr))
67 (emit-byte-oriented 'subwf adr))
69 (emit-byte-oriented 'subwfb adr))
72 (emit (list 'mullw adr)))
74 (emit-byte-oriented 'mulwf adr #f))
77 (emit-byte-oriented 'andwf adr))
79 (emit-byte-oriented 'iorwf adr))
81 (emit-byte-oriented 'xorwf adr))
84 (emit-byte-oriented 'rlcf adr))
86 (emit-byte-oriented 'rrcf adr))
89 (emit-bit-oriented 'bcf adr bit))
91 (emit-bit-oriented 'bsf adr bit))
93 (emit-bit-oriented 'btg adr bit))
96 (emit-byte-oriented 'comf adr))
98 (define (tblrd) ;; TODO support the different modes
102 (emit-byte-oriented 'cpfseq adr #f))
104 (emit-byte-oriented 'cpfslt adr #f))
106 (emit-byte-oriented 'cpfsgt adr #f))
108 (define (bra-or-goto label)
109 (emit (list 'bra-or-goto label)))
111 (emit (list 'goto label)))
113 (define (rcall label)
114 (emit (list 'rcall label)))
117 (if (and #f (and (not (null? rev-code))
118 (eq? (caar rev-code) 'rcall)))
119 (let ((label (cadar rev-code)))
120 (set! rev-code (cdr rev-code))
122 (emit (list 'return))))
125 (if (and #f (and (not (null? rev-code))
126 (eq? (caar rev-code) 'bra-or-goto)
127 (eq? (cadar rev-code) lab)))
129 (set! rev-code (cdr rev-code))
131 (emit (list 'label lab))))
134 (emit (list 'sleep)))
136 (define (move-reg src dst)
145 ;; takes 2 cycles (as much as movfw src ; movwf dst), but takes
146 ;; only 1 instruction
149 (define (bb-linearize bb)
150 (let ((label-num (bb-label-num bb)))
151 (let ((bb (vector-ref bbs-vector label-num)))
153 (define (move-lit n adr)
162 (define (dump-instr instr)
163 (cond ((call-instr? instr)
164 (let* ((def-proc (call-instr-def-proc instr))
165 (entry (def-procedure-entry def-proc)))
169 (let ((label (bb-label entry)))
172 ((return-instr? instr)
175 (let ((src1 (instr-src1 instr))
176 (src2 (instr-src2 instr))
177 (dst (instr-dst instr)))
178 (if (and (or (not (byte-cell? dst))
180 (or (not (byte-cell? src1))
181 (byte-cell-adr src1))
182 (or (not (byte-cell? src2))
183 (byte-cell-adr src2)))
185 (case (instr-id instr)
189 (let ((n (byte-lit-val src1))
190 (z (byte-cell-adr dst)))
192 (let ((x (byte-cell-adr src1))
193 (z (byte-cell-adr dst)))
198 (let ((n (byte-lit-val src2))
199 (z (byte-cell-adr dst)))
201 (move-lit (byte-lit-val src1) z)
202 (move-reg (byte-cell-adr src1) z))
203 (case (instr-id instr)
204 ((add) (cond ((= n 1) (incf z))
205 ((= n #xff) (decf z))
208 ((addc) (movlw n) (addwfc z))
209 ((sub) (cond ((= n 1) (decf z))
210 ((= n #xff) (incf z))
213 ((subb) (movlw n) (subwfb z))))
214 (let ((x (byte-cell-adr src1))
215 (y (byte-cell-adr src2))
216 (z (byte-cell-adr dst)))
217 (cond ((and (not (= x y))
219 (memq (instr-id instr)
221 ;; since this basically swaps the
222 ;; arguments, it can't be used for
227 ;; for subtraction, preserves argument
230 ;; this NEEDS to be done with movff, or
231 ;; else wreg will get clobbered and this
234 (else ;; TODO check if it could be merged with the previous case
237 (case (instr-id instr)
242 (else (error "..."))))))
244 ((mul) ; 8 by 8 multiplication
246 ;; since multiplication is commutative, the
247 ;; arguments are set up so the second one will
248 ;; be a literal if the operator is applied on a
249 ;; literal and a variable
250 (let ((n (byte-lit-val src2)))
252 (movlw (byte-lit-val src1))
253 (move-reg (byte-cell-adr src1) WREG))
254 ;; literal multiplication
256 (let ((x (byte-cell-adr src1))
257 (y (byte-cell-adr src2)))
262 (let ((x (if (byte-lit? src1)
264 (byte-cell-adr src1)))
265 (y (if (byte-lit? src2)
267 (byte-cell-adr src2)))
268 (z (byte-cell-adr dst)))
269 (cond ((byte-lit? src2)
274 ((and (not (= x y)) (= y z))
279 (case (instr-id instr)
283 (else (error "...")))))
286 (let ((x (if (byte-lit? src1)
288 (byte-cell-adr src1)))
289 (z (byte-cell-adr dst)))
290 (cond ((byte-lit? src1) (move-lit x z))
291 ((not (= x z)) (move-reg x z)))
292 (case (instr-id instr)
298 (if (not (byte-lit? src2))
299 (error "bit offset must be a literal"))
300 (let ((x (byte-cell-adr src1))
301 (y (byte-lit-val src2)))
302 (case (instr-id instr)
305 ((toggle) (btg x y)))))
308 (let ((z (byte-cell-adr dst)))
310 (move-lit (byte-lit-val src1) z)
311 (move-reg (byte-cell-adr src1) z))
316 (move-lit (byte-lit-val src1) TBLPTRL)
317 (move-reg (byte-cell-adr src1) TBLPTRL))
319 (move-lit (byte-lit-val src2) TBLPTRH)
320 (move-reg (byte-cell-adr src2) TBLPTRH))
321 ;; TODO the 5 high bits are not used for now
325 (if (null? (bb-succs bb))
326 (error "I think you might have given me an empty source file."))
327 (let* ((succs (bb-succs bb))
329 (bra-or-goto (bb-label dest))
332 (let* ((succs (bb-succs bb))
333 (dest-true (car succs))
334 (dest-false (cadr succs)))
336 (define (compare flip adr)
337 (case (instr-id instr)
338 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
339 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
341 (bra-or-goto (bb-label dest-false))
342 (bra-or-goto (bb-label dest-true))
343 (add-todo dest-false)
344 (add-todo dest-true))
346 (cond ((byte-lit? src1)
347 (let ((n (byte-lit-val src1))
348 (y (byte-cell-adr src2)))
349 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
350 (eq? (instr-id instr) 'x==y))
351 (special-compare-eq-lit n x)
356 (let ((x (byte-cell-adr src1))
357 (n (byte-lit-val src2)))
358 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
359 (eq? (instr-id instr) 'x==y))
360 (special-compare-eq-lit n x)
365 (let ((x (byte-cell-adr src1))
366 (y (byte-cell-adr src2)))
371 (let ((off (byte-cell-adr src1))) ; branch no TODO we can't have literals, we need the space to calculate the address
372 ;; precalculate the low byte of the PC
374 (movff PCL off) ;; TODO at assembly, this can all be known statically
375 ;; we add 4 times the offset, since gotos are 4
381 ;; to compensate for the PC advancing while we calculate
385 (addwfc PCLATH) ; set PCH if we overflow
386 (movff off PCL) ; setting PCL moves PCLATH to PCH
388 ;; create the jump table
389 (for-each (lambda (bb)
396 (emit (list (instr-id instr))))))))))
400 (vector-set! bbs-vector label-num #f)
401 (label (bb-label bb))
402 (for-each dump-instr (reverse (bb-rev-instrs bb)))
403 (for-each add-todo (bb-succs bb)))))))
405 (let ((prog-label (asm-make-label 'PROG)))
410 (add-todo (vector-ref bbs-vector 0))
415 (let ((bb (car todo)))
416 (set! todo (cdr todo))
421 (define (assembler-gen filename cfg)
425 ((eval (car instr)) (cadr instr)))
427 ((eval (car instr)) (cadr instr) (caddr instr)))
429 ((eval (car instr)) (cadr instr) (caddr instr) (cadddr instr)))
433 ((movff movwf clrf setf cpfseq cpfslt cpfsgt mulwf)
435 ((incf decf addwf addwfc subwf subwfb andwf iorwf xorwf rlcf rrcf comf
439 (tblrd*)) ;; TODO support the other modes
445 (bra-or-goto (cadr instr)))
447 (rcall-or-call (cadr instr)))
452 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
453 (asm-label (cadr instr)))
457 (error "unknown instruction" instr))))
461 ;; (pretty-print cfg)
463 (let ((code (linearize-and-cleanup cfg)))
464 ;; (pretty-print code)
465 ;; if we would need a second bank, load the address for the second bank in BSR
467 (begin (gen (list 'movlw 1))
468 (gen (list 'movwf BSR 'a))))
469 (for-each gen code)))